• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "system_ability_manager_stub.h"
17 
18 #include <unistd.h>
19 #include <cinttypes>
20 
21 #include "accesstoken_kit.h"
22 #include "datetime_ex.h"
23 #include "errors.h"
24 #include "hitrace_meter.h"
25 #include "ipc_skeleton.h"
26 #include "ipc_types.h"
27 #include "memory_guard.h"
28 #include "sam_log.h"
29 #include "string_ex.h"
30 #include "system_ability_manager.h"
31 #include "system_ability_on_demand_event.h"
32 #include "tools.h"
33 
34 #ifdef WITH_SELINUX
35 #include "service_checker.h"
36 #endif
37 
38 namespace {
39 #ifdef WITH_SELINUX
40     using namespace OHOS::HiviewDFX;
41     OHOS::HiviewDFX::HiLogLabel label_ = { LOG_CORE, 0xD001800, "SA_SELINUX" };
42     std::unique_ptr<ServiceChecker> selinuxChecker_ = std::make_unique<ServiceChecker>(false);
43 #endif
44 
CheckGetSAPermission(const int32_t said)45     bool CheckGetSAPermission(const int32_t said)
46     {
47 #ifdef WITH_SELINUX
48         int64_t begin = OHOS::GetTickCount();
49         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
50         auto ret = selinuxChecker_->GetServiceCheck(callingPid, std::to_string(said)) == 0;
51         HiLog::Debug(label_, "[Performance] GetServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
52             said, OHOS::GetTickCount() - begin);
53         return  ret;
54 #else
55         return true; // if not support selinux, not check selinux permission
56 #endif
57     }
58 
CheckAddOrRemovePermission(const int32_t said)59     bool CheckAddOrRemovePermission(const int32_t said)
60     {
61 #ifdef WITH_SELINUX
62         int64_t begin = OHOS::GetTickCount();
63         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
64         auto ret = selinuxChecker_->AddServiceCheck(callingPid, std::to_string(said)) == 0;
65         HiLog::Debug(label_, "[Performance] AddServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
66             said, OHOS::GetTickCount() - begin);
67         return ret;
68 #else
69         return true; // if not support selinux, not check selinux permission
70 #endif
71     }
72 
CheckGetRemoteSAPermission(const int32_t said)73     bool CheckGetRemoteSAPermission(const int32_t said)
74     {
75 #ifdef WITH_SELINUX
76         int64_t begin = OHOS::GetTickCount();
77         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
78         auto ret = selinuxChecker_->GetRemoteServiceCheck(callingPid, std::to_string(said)) == 0;
79         HiLog::Debug(label_, "[Performance] GetRemoteServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
80             said, OHOS::GetTickCount() - begin);
81         return ret;
82 #else
83         return true; // if not support selinux, not check selinux permission
84 #endif
85     }
86 
CheckListSAPermission()87     bool CheckListSAPermission()
88     {
89 #ifdef WITH_SELINUX
90         int64_t begin = OHOS::GetTickCount();
91         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
92         auto ret = selinuxChecker_->ListServiceCheck(callingPid) == 0;
93         HiLog::Debug(label_, "[Performance] ListServiceCheck spend %{public}" PRId64 " ms",
94             OHOS::GetTickCount() - begin);
95         return ret;
96 #else
97         return true; // if not support selinux, not check selinux permission
98 #endif
99     }
100 }
101 
102 using namespace OHOS::Security;
103 namespace OHOS {
SystemAbilityManagerStub()104 SystemAbilityManagerStub::SystemAbilityManagerStub()
105 {
106     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_SYSTEM_ABILITY_TRANSACTION)] =
107         &SystemAbilityManagerStub::GetSystemAbilityInner;
108     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::CHECK_SYSTEM_ABILITY_TRANSACTION)] =
109         &SystemAbilityManagerStub::CheckSystemAbilityInner;
110     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_ABILITY_TRANSACTION)] =
111         &SystemAbilityManagerStub::AddSystemAbilityInner;
112     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::REMOVE_SYSTEM_ABILITY_TRANSACTION)] =
113         &SystemAbilityManagerStub::RemoveSystemAbilityInner;
114     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::LIST_SYSTEM_ABILITY_TRANSACTION)] =
115         &SystemAbilityManagerStub::ListSystemAbilityInner;
116     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION)] =
117         &SystemAbilityManagerStub::SubsSystemAbilityInner;
118     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::CHECK_REMOTE_SYSTEM_ABILITY_TRANSACTION)] =
119         &SystemAbilityManagerStub::CheckRemtSystemAbilityInner;
120     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::ADD_ONDEMAND_SYSTEM_ABILITY_TRANSACTION)] =
121         &SystemAbilityManagerStub::AddOndemandSystemAbilityInner;
122     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::CHECK_SYSTEM_ABILITY_IMMEDIATELY_TRANSACTION)] =
123         &SystemAbilityManagerStub::CheckSystemAbilityImmeInner;
124     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_ABILITY_TRANSACTION)] =
125         &SystemAbilityManagerStub::UnSubsSystemAbilityInner;
126     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::ADD_SYSTEM_PROCESS_TRANSACTION)] =
127         &SystemAbilityManagerStub::AddSystemProcessInner;
128     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::LOAD_SYSTEM_ABILITY_TRANSACTION)] =
129         &SystemAbilityManagerStub::LoadSystemAbilityInner;
130     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::LOAD_REMOTE_SYSTEM_ABILITY_TRANSACTION)] =
131         &SystemAbilityManagerStub::LoadRemoteSystemAbilityInner;
132     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UNLOAD_SYSTEM_ABILITY_TRANSACTION)] =
133         &SystemAbilityManagerStub::UnloadSystemAbilityInner;
134     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::CANCEL_UNLOAD_SYSTEM_ABILITY_TRANSACTION)] =
135         &SystemAbilityManagerStub::CancelUnloadSystemAbilityInner;
136     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_RUNNING_SYSTEM_PROCESS_TRANSACTION)] =
137         &SystemAbilityManagerStub::GetRunningSystemProcessInner;
138     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::SUBSCRIBE_SYSTEM_PROCESS_TRANSACTION)] =
139         &SystemAbilityManagerStub::SubscribeSystemProcessInner;
140     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UNSUBSCRIBE_SYSTEM_PROCESS_TRANSACTION)] =
141         &SystemAbilityManagerStub::UnSubscribeSystemProcessInner;
142     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEMAND_REASON_EXTRA_DATA_TRANSACTION)] =
143         &SystemAbilityManagerStub::GetOnDemandReasonExtraDataInner;
144     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::GET_ONDEAMND_POLICY_TRANSACTION)] =
145         &SystemAbilityManagerStub::GetOnDemandPolicyInner;
146     memberFuncMap_[static_cast<uint32_t>(SamgrInterfaceCode::UPDATE_ONDEAMND_POLICY_TRANSACTION)] =
147         &SystemAbilityManagerStub::UpdateOnDemandPolicyInner;
148 }
149 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)150 int32_t SystemAbilityManagerStub::OnRemoteRequest(uint32_t code,
151     MessageParcel& data, MessageParcel& reply, MessageOption &option)
152 {
153     HILOGD("SAMStub::OnReceived, code = %{public}u, callerPid = %{public}d",
154         code, IPCSkeleton::GetCallingPid());
155     Samgr::MemoryGuard cacheGuard;
156     if (!EnforceInterceToken(data)) {
157         HILOGE("SAMStub::OnReceived, code = %{public}u, check interfaceToken failed", code);
158         return ERR_PERMISSION_DENIED;
159     }
160     auto itFunc = memberFuncMap_.find(code);
161     if (itFunc != memberFuncMap_.end()) {
162         auto memberFunc = itFunc->second;
163         if (memberFunc != nullptr) {
164             return (this->*memberFunc)(data, reply);
165         }
166     }
167     HILOGW("SAMStub: default case, need check.");
168     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
169 }
170 
EnforceInterceToken(MessageParcel & data)171 bool SystemAbilityManagerStub::EnforceInterceToken(MessageParcel& data)
172 {
173     std::u16string interfaceToken = data.ReadInterfaceToken();
174     return interfaceToken == SAMANAGER_INTERFACE_TOKEN;
175 }
176 
ListSystemAbilityInner(MessageParcel & data,MessageParcel & reply)177 int32_t SystemAbilityManagerStub::ListSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
178 {
179     if (!CanRequest()) {
180         HILOGE("ListSystemAbilityInner PERMISSION DENIED!");
181         return ERR_PERMISSION_DENIED;
182     }
183 
184     if (!CheckListSAPermission()) {
185         HILOGE("ListSystemAbilityInner selinux permission denied!");
186         return ERR_PERMISSION_DENIED;
187     }
188 
189     int32_t dumpFlag = 0;
190     bool ret = data.ReadInt32(dumpFlag);
191     if (!ret) {
192         HILOGW("SystemAbilityManagerStub::ListSystemAbilityInner read dumpflag failed!");
193         return ERR_FLATTEN_OBJECT;
194     }
195 
196     std::vector<std::u16string> saNameVector = ListSystemAbilities(dumpFlag);
197     if (saNameVector.empty()) {
198         HILOGI("List System Abilities list errors");
199         ret = reply.WriteInt32(ERR_INVALID_VALUE);
200     } else {
201         HILOGI("SystemAbilityManagerStub::ListSystemAbilityInner list success");
202         ret = reply.WriteInt32(ERR_NONE);
203         if (ret) {
204             ret = reply.WriteString16Vector(saNameVector);
205         }
206     }
207 
208     if (!ret) {
209         HILOGW("SystemAbilityManagerStub::ListSystemAbilityInner write reply failed.");
210         return ERR_FLATTEN_OBJECT;
211     }
212 
213     return ERR_NONE;
214 }
215 
SubsSystemAbilityInner(MessageParcel & data,MessageParcel & reply)216 int32_t SystemAbilityManagerStub::SubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
217 {
218     int32_t systemAbilityId = data.ReadInt32();
219     if (!CheckInputSysAbilityId(systemAbilityId)) {
220         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner read systemAbilityId failed!");
221         return ERR_NULL_OBJECT;
222     }
223     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
224     if (remoteObject == nullptr) {
225         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner read listener failed!");
226         return ERR_NULL_OBJECT;
227     }
228     sptr<ISystemAbilityStatusChange> listener = iface_cast<ISystemAbilityStatusChange>(remoteObject);
229     if (listener == nullptr) {
230         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner iface_cast failed!");
231         return ERR_NULL_OBJECT;
232     }
233     int32_t result = SubscribeSystemAbility(systemAbilityId, listener);
234     HILOGD("SystemAbilityManagerStub::SubsSystemAbilityInner result is %d", result);
235     bool ret = reply.WriteInt32(result);
236     if (!ret) {
237         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner write reply failed.");
238         return ERR_FLATTEN_OBJECT;
239     }
240 
241     return result;
242 }
243 
UnSubsSystemAbilityInner(MessageParcel & data,MessageParcel & reply)244 int32_t SystemAbilityManagerStub::UnSubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
245 {
246     int32_t systemAbilityId = data.ReadInt32();
247     if (!CheckInputSysAbilityId(systemAbilityId)) {
248         HILOGW("SystemAbilityManagerStub::UnSubsSystemAbilityInner read systemAbilityId failed!");
249         return ERR_NULL_OBJECT;
250     }
251     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
252     if (remoteObject == nullptr) {
253         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility read listener failed!");
254         return ERR_NULL_OBJECT;
255     }
256     sptr<ISystemAbilityStatusChange> listener = iface_cast<ISystemAbilityStatusChange>(remoteObject);
257     if (listener == nullptr) {
258         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility iface_cast failed!");
259         return ERR_NULL_OBJECT;
260     }
261     int32_t result = UnSubscribeSystemAbility(systemAbilityId, listener);
262     HILOGD("SystemAbilityManagerStub::UnSubscribeSystemAbility result is %d", result);
263     bool ret = reply.WriteInt32(result);
264     if (!ret) {
265         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility write reply failed.");
266         return ERR_FLATTEN_OBJECT;
267     }
268 
269     return result;
270 }
271 
CheckRemtSystemAbilityInner(MessageParcel & data,MessageParcel & reply)272 int32_t SystemAbilityManagerStub::CheckRemtSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
273 {
274     if (!CanRequest()) {
275         HILOGE("CheckRemoteSystemAbilityInner PERMISSION DENIED!");
276         return ERR_PERMISSION_DENIED;
277     }
278     int32_t systemAbilityId = data.ReadInt32();
279     if (!CheckInputSysAbilityId(systemAbilityId)) {
280         HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner read systemAbilityId failed!");
281         return ERR_NULL_OBJECT;
282     }
283 
284     if (!CheckGetRemoteSAPermission(systemAbilityId)) {
285         HILOGE("CheckRemtSystemAbilityInner selinux permission denied!, SA : %{public}d", systemAbilityId);
286         return ERR_PERMISSION_DENIED;
287     }
288 
289     std::string deviceId;
290     bool ret = data.ReadString(deviceId);
291     if (!ret) {
292         HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner read deviceId failed!");
293         return ERR_FLATTEN_OBJECT;
294     }
295     std::string uuid = SystemAbilityManager::GetInstance()->TransformDeviceId(deviceId, UUID, false);
296     ret = reply.WriteRemoteObject(GetSystemAbility(systemAbilityId, uuid));
297     if (!ret) {
298         HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner write reply failed.");
299         return ERR_FLATTEN_OBJECT;
300     }
301 
302     return ERR_NONE;
303 }
304 
AddOndemandSystemAbilityInner(MessageParcel & data,MessageParcel & reply)305 int32_t SystemAbilityManagerStub::AddOndemandSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
306 {
307     if (!CanRequest()) {
308         HILOGE("AddOndemandSystemAbilityInner PERMISSION DENIED!");
309         return ERR_PERMISSION_DENIED;
310     }
311     int32_t systemAbilityId = data.ReadInt32();
312     if (!CheckInputSysAbilityId(systemAbilityId)) {
313         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner read systemAbilityId failed!");
314         return ERR_NULL_OBJECT;
315     }
316 
317     if (!CheckAddOrRemovePermission(systemAbilityId)) {
318         HILOGE("AddOndemandSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
319         return ERR_PERMISSION_DENIED;
320     }
321 
322     std::u16string localManagerName = data.ReadString16();
323     if (localManagerName.empty()) {
324         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner read localName failed!");
325         return ERR_NULL_OBJECT;
326     }
327 
328     int32_t result = AddOnDemandSystemAbilityInfo(systemAbilityId, localManagerName);
329     HILOGD("SystemAbilityManagerStub::AddOndemandSystemAbilityInner result is %d", result);
330     bool ret = reply.WriteInt32(result);
331     if (!ret) {
332         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner write reply failed.");
333         return ERR_FLATTEN_OBJECT;
334     }
335 
336     return result;
337 }
338 
CheckSystemAbilityImmeInner(MessageParcel & data,MessageParcel & reply)339 int32_t SystemAbilityManagerStub::CheckSystemAbilityImmeInner(MessageParcel& data, MessageParcel& reply)
340 {
341     int32_t systemAbilityId = data.ReadInt32();
342     if (!CheckInputSysAbilityId(systemAbilityId)) {
343         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner read systemAbilityId failed!");
344         return ERR_NULL_OBJECT;
345     }
346 
347     if (!CheckGetSAPermission(systemAbilityId)) {
348         HILOGE("CheckSystemAbilityImmeInner selinux permission denied! SA : %{public}d", systemAbilityId);
349         return ERR_PERMISSION_DENIED;
350     }
351 
352     bool isExist = false;
353     bool ret = data.ReadBool(isExist);
354     if (!ret) {
355         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner read isExist failed!");
356         return ERR_FLATTEN_OBJECT;
357     }
358     ret = reply.WriteRemoteObject(CheckSystemAbility(systemAbilityId, isExist));
359     if (!ret) {
360         return ERR_FLATTEN_OBJECT;
361     }
362 
363     ret = reply.WriteBool(isExist);
364     if (!ret) {
365         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner write reply failed.");
366         return ERR_FLATTEN_OBJECT;
367     }
368 
369     return ERR_NONE;
370 }
371 
UnmarshalingSaExtraProp(MessageParcel & data,SAExtraProp & extraProp)372 int32_t SystemAbilityManagerStub::UnmarshalingSaExtraProp(MessageParcel& data, SAExtraProp& extraProp)
373 {
374     bool isDistributed = false;
375     bool ret = data.ReadBool(isDistributed);
376     if (!ret) {
377         HILOGW("SystemAbilityManagerStub::UnmarshalingSaExtraProp read isDistributed failed!");
378         return ERR_FLATTEN_OBJECT;
379     }
380 
381     int32_t dumpFlags = 0;
382     ret = data.ReadInt32(dumpFlags);
383     if (!ret || dumpFlags < 0) {
384         HILOGW("SystemAbilityManagerStub::UnmarshalingSaExtraProp dumpFlags failed!");
385         return ERR_FLATTEN_OBJECT;
386     }
387     std::u16string capability = data.ReadString16();
388     std::u16string permission = data.ReadString16();
389     extraProp.isDistributed = isDistributed;
390     extraProp.dumpFlags = static_cast<uint32_t>(dumpFlags);
391     extraProp.capability = capability;
392     extraProp.permission = permission;
393     return ERR_OK;
394 }
395 
AddSystemAbilityInner(MessageParcel & data,MessageParcel & reply)396 int32_t SystemAbilityManagerStub::AddSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
397 {
398     if (!CanRequest()) {
399         HILOGE("AddSystemAbilityInner PERMISSION DENIED!");
400         return ERR_PERMISSION_DENIED;
401     }
402     int32_t systemAbilityId = data.ReadInt32();
403     if (!CheckInputSysAbilityId(systemAbilityId)) {
404         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner read systemAbilityId failed!");
405         return ERR_NULL_OBJECT;
406     }
407 
408     if (!CheckAddOrRemovePermission(systemAbilityId)) {
409         HILOGE("AddSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
410         return ERR_PERMISSION_DENIED;
411     }
412 
413     auto object = data.ReadRemoteObject();
414     if (object == nullptr) {
415         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner readParcelable failed!");
416         return ERR_NULL_OBJECT;
417     }
418     SAExtraProp extraProp;
419     int32_t result = UnmarshalingSaExtraProp(data, extraProp);
420     if (result != ERR_OK) {
421         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner UnmarshalingSaExtraProp failed!");
422         return result;
423     }
424     result = AddSystemAbility(systemAbilityId, object, extraProp);
425     bool ret = reply.WriteInt32(result);
426     if (!ret) {
427         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner write reply failed.");
428         return ERR_FLATTEN_OBJECT;
429     }
430     return result;
431 }
432 
GetSystemAbilityInner(MessageParcel & data,MessageParcel & reply)433 int32_t SystemAbilityManagerStub::GetSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
434 {
435     int32_t systemAbilityId = data.ReadInt32();
436     if (!CheckInputSysAbilityId(systemAbilityId)) {
437         HILOGW("SystemAbilityManagerStub::GetSystemAbilityInner read systemAbilityId failed!");
438         return ERR_NULL_OBJECT;
439     }
440 
441     if (!CheckGetSAPermission(systemAbilityId)) {
442         HILOGE("GetSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
443         return ERR_PERMISSION_DENIED;
444     }
445 
446     bool ret = reply.WriteRemoteObject(GetSystemAbility(systemAbilityId));
447     if (!ret) {
448         HILOGW("SystemAbilityManagerStub:GetSystemAbilityInner write reply failed.");
449         return ERR_FLATTEN_OBJECT;
450     }
451     return ERR_NONE;
452 }
453 
CheckSystemAbilityInner(MessageParcel & data,MessageParcel & reply)454 int32_t SystemAbilityManagerStub::CheckSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
455 {
456     int32_t systemAbilityId = data.ReadInt32();
457     if (!CheckInputSysAbilityId(systemAbilityId)) {
458         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityInner read systemAbilityId failed!");
459         return ERR_NULL_OBJECT;
460     }
461 
462     if (!CheckGetSAPermission(systemAbilityId)) {
463         HILOGE("CheckSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
464         return ERR_PERMISSION_DENIED;
465     }
466 
467     bool ret = reply.WriteRemoteObject(CheckSystemAbility(systemAbilityId));
468     if (!ret) {
469         return ERR_FLATTEN_OBJECT;
470     }
471     return ERR_NONE;
472 }
473 
RemoveSystemAbilityInner(MessageParcel & data,MessageParcel & reply)474 int32_t SystemAbilityManagerStub::RemoveSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
475 {
476     if (!CanRequest()) {
477         HILOGE("RemoveSystemAbilityInner PERMISSION DENIED!");
478         return ERR_PERMISSION_DENIED;
479     }
480     int32_t systemAbilityId = data.ReadInt32();
481     if (!CheckInputSysAbilityId(systemAbilityId)) {
482         HILOGW("SystemAbilityManagerStub::RemoveSystemAbilityInner read systemAbilityId failed!");
483         return ERR_NULL_OBJECT;
484     }
485 
486     if (!CheckAddOrRemovePermission(systemAbilityId)) {
487         HILOGE("RemoveSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
488         return ERR_PERMISSION_DENIED;
489     }
490 
491     int32_t result = RemoveSystemAbility(systemAbilityId);
492     bool ret = reply.WriteInt32(result);
493     if (!ret) {
494         HILOGW("SystemAbilityManagerStub::RemoveSystemAbilityInner write reply failed.");
495         return ERR_FLATTEN_OBJECT;
496     }
497     return result;
498 }
499 
500 
AddSystemProcessInner(MessageParcel & data,MessageParcel & reply)501 int32_t SystemAbilityManagerStub::AddSystemProcessInner(MessageParcel& data, MessageParcel& reply)
502 {
503     if (!CanRequest()) {
504         HILOGE("AddSystemProcessInner PERMISSION DENIED!");
505         return ERR_PERMISSION_DENIED;
506     }
507     std::u16string procName = data.ReadString16();
508     if (procName.empty()) {
509         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner read process name failed!");
510         return ERR_NULL_OBJECT;
511     }
512 
513     sptr<IRemoteObject> procObject = data.ReadRemoteObject();
514     if (procObject == nullptr) {
515         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner readParcelable failed!");
516         return ERR_NULL_OBJECT;
517     }
518 
519     int32_t result = AddSystemProcess(procName, procObject);
520     bool ret = reply.WriteInt32(result);
521     if (!ret) {
522         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner write reply failed.");
523         return ERR_FLATTEN_OBJECT;
524     }
525     return result;
526 }
527 
LoadSystemAbilityInner(MessageParcel & data,MessageParcel & reply)528 int32_t SystemAbilityManagerStub::LoadSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
529 {
530     int32_t systemAbilityId = data.ReadInt32();
531     std::string loadSystemAbilityTag = ToString(systemAbilityId) + "_LoadSystemAbility";
532     HITRACE_METER_NAME(HITRACE_TAG_SAMGR, loadSystemAbilityTag);
533     if (!CheckInputSysAbilityId(systemAbilityId)) {
534         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read systemAbilityId failed!");
535         return ERR_INVALID_VALUE;
536     }
537 
538     if (!CheckGetSAPermission(systemAbilityId)) {
539         HILOGE("LoadSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
540         return ERR_PERMISSION_DENIED;
541     }
542 
543     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
544     if (remoteObject == nullptr) {
545         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read callback failed!");
546         return ERR_INVALID_VALUE;
547     }
548     sptr<ISystemAbilityLoadCallback> callback = iface_cast<ISystemAbilityLoadCallback>(remoteObject);
549     if (callback == nullptr) {
550         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner iface_cast failed!");
551         return ERR_INVALID_VALUE;
552     }
553     int32_t result = LoadSystemAbility(systemAbilityId, callback);
554     HILOGD("SystemAbilityManagerStub::LoadSystemAbilityInner result is %{public}d", result);
555     bool ret = reply.WriteInt32(result);
556     if (!ret) {
557         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner write reply failed.");
558         return ERR_FLATTEN_OBJECT;
559     }
560     return result;
561 }
562 
LoadRemoteSystemAbilityInner(MessageParcel & data,MessageParcel & reply)563 int32_t SystemAbilityManagerStub::LoadRemoteSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
564 {
565     int32_t systemAbilityId = data.ReadInt32();
566     if (!CheckInputSysAbilityId(systemAbilityId)) {
567         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner systemAbilityId invalid");
568         return ERR_INVALID_VALUE;
569     }
570 
571     if (!CheckGetRemoteSAPermission(systemAbilityId)) {
572         HILOGE("LoadRemoteSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
573         return ERR_PERMISSION_DENIED;
574     }
575 
576     std::string deviceId = data.ReadString();
577     if (deviceId.empty()) {
578         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner read deviceId failed");
579         return ERR_INVALID_VALUE;
580     }
581     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
582     if (remoteObject == nullptr) {
583         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner read callback failed!");
584         return ERR_INVALID_VALUE;
585     }
586     sptr<ISystemAbilityLoadCallback> callback = iface_cast<ISystemAbilityLoadCallback>(remoteObject);
587     if (callback == nullptr) {
588         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner iface_cast failed!");
589         return ERR_INVALID_VALUE;
590     }
591     int32_t result = LoadSystemAbility(systemAbilityId, deviceId, callback);
592     HILOGD("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner result is %{public}d", result);
593     bool ret = reply.WriteInt32(result);
594     if (!ret) {
595         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner write reply failed.");
596         return ERR_FLATTEN_OBJECT;
597     }
598     return result;
599 }
600 
UnloadSystemAbilityInner(MessageParcel & data,MessageParcel & reply)601 int32_t SystemAbilityManagerStub::UnloadSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
602 {
603     int32_t systemAbilityId = data.ReadInt32();
604     if (!CheckInputSysAbilityId(systemAbilityId)) {
605         HILOGW("SystemAbilityManagerStub::UnloadSystemAbilityInner systemAbilityId invalid");
606         return ERR_INVALID_VALUE;
607     }
608     int32_t result = UnloadSystemAbility(systemAbilityId);
609     HILOGD("SystemAbilityManagerStub::UnloadSystemAbilityInner result is %{public}d", result);
610     bool ret = reply.WriteInt32(result);
611     if (!ret) {
612         HILOGW("SystemAbilityManagerStub::UnloadSystemAbilityInner write reply failed.");
613         return ERR_FLATTEN_OBJECT;
614     }
615     return result;
616 }
617 
GetRunningSystemProcessInner(MessageParcel & data,MessageParcel & reply)618 int32_t SystemAbilityManagerStub::GetRunningSystemProcessInner(MessageParcel& data, MessageParcel& reply)
619 {
620     HILOGI("GetRunningSystemProcessInner called");
621     if (!CanRequest()) {
622         HILOGE("GetRunningSystemProcessInner PERMISSION DENIED!");
623         return ERR_PERMISSION_DENIED;
624     }
625     std::list<SystemProcessInfo> systemProcessInfos;
626     int32_t result = GetRunningSystemProcess(systemProcessInfos);
627     bool ret = reply.WriteInt32(result);
628     if (!ret) {
629         HILOGW("GetRunningSystemProcessInner write reply failed.");
630         return ERR_FLATTEN_OBJECT;
631     }
632     if (result != ERR_OK) {
633         return ERR_OK;
634     }
635 
636     size_t size = systemProcessInfos.size();
637     ret = reply.WriteInt32(size);
638     if (!ret) {
639         HILOGW("GetRunningSystemProcessInner write systemProcessInfos size failed.");
640         return ERR_FLATTEN_OBJECT;
641     }
642     for (auto& systemProcessInfo : systemProcessInfos) {
643         ret = reply.WriteString(systemProcessInfo.processName);
644         if (!ret) {
645             HILOGW("GetRunningSystemProcessInner write processName failed.");
646             return ERR_FLATTEN_OBJECT;
647         }
648         ret = reply.WriteInt32(systemProcessInfo.pid);
649         if (!ret) {
650             HILOGW("GetRunningSystemProcessInner write pid failed.");
651             return ERR_FLATTEN_OBJECT;
652         }
653         ret = reply.WriteInt32(systemProcessInfo.uid);
654         if (!ret) {
655             HILOGW("GetRunningSystemProcessInner write uid failed.");
656             return ERR_FLATTEN_OBJECT;
657         }
658     }
659     return ERR_OK;
660 }
661 
SubscribeSystemProcessInner(MessageParcel & data,MessageParcel & reply)662 int32_t SystemAbilityManagerStub::SubscribeSystemProcessInner(MessageParcel& data, MessageParcel& reply)
663 {
664     if (!CanRequest()) {
665         HILOGE("SubscribeSystemProcessInner PERMISSION DENIED!");
666         return ERR_PERMISSION_DENIED;
667     }
668     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
669     if (remoteObject == nullptr) {
670         HILOGW("SubscribeSystemProcessInner read listener failed!");
671         return ERR_NULL_OBJECT;
672     }
673     sptr<ISystemProcessStatusChange> listener = iface_cast<ISystemProcessStatusChange>(remoteObject);
674     if (listener == nullptr) {
675         HILOGW("SubscribeSystemProcessInner iface_cast failed!");
676         return ERR_NULL_OBJECT;
677     }
678     int32_t result = SubscribeSystemProcess(listener);
679     HILOGD("SubscribeSystemProcess result is %{public}d", result);
680     bool ret = reply.WriteInt32(result);
681     if (!ret) {
682         HILOGW("SubscribeSystemProcessInner write reply failed.");
683         return ERR_FLATTEN_OBJECT;
684     }
685     return result;
686 }
687 
UnSubscribeSystemProcessInner(MessageParcel & data,MessageParcel & reply)688 int32_t SystemAbilityManagerStub::UnSubscribeSystemProcessInner(MessageParcel& data, MessageParcel& reply)
689 {
690     if (!CanRequest()) {
691         HILOGE("UnSubscribeSystemProcessInner PERMISSION DENIED!");
692         return ERR_PERMISSION_DENIED;
693     }
694     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
695     if (remoteObject == nullptr) {
696         HILOGW("UnSubscribeSystemProcessInner read listener failed!");
697         return ERR_NULL_OBJECT;
698     }
699     sptr<ISystemProcessStatusChange> listener = iface_cast<ISystemProcessStatusChange>(remoteObject);
700     if (listener == nullptr) {
701         HILOGW("UnSubscribeSystemProcessInner iface_cast failed!");
702         return ERR_NULL_OBJECT;
703     }
704     int32_t result = UnSubscribeSystemProcess(listener);
705     HILOGD("UnSubscribeSystemProcessInner result is %{public}d", result);
706     bool ret = reply.WriteInt32(result);
707     if (!ret) {
708         HILOGW("UnSubscribeSystemProcessInner write reply failed.");
709         return ERR_FLATTEN_OBJECT;
710     }
711     return result;
712 }
713 
CancelUnloadSystemAbilityInner(MessageParcel & data,MessageParcel & reply)714 int32_t SystemAbilityManagerStub::CancelUnloadSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
715 {
716     int32_t systemAbilityId = data.ReadInt32();
717     if (!CheckInputSysAbilityId(systemAbilityId)) {
718         HILOGW("SystemAbilityManagerStub::CancelUnloadSystemAbilityInner systemAbilityId invalid");
719         return ERR_INVALID_VALUE;
720     }
721     int32_t result = CancelUnloadSystemAbility(systemAbilityId);
722     HILOGD("SystemAbilityManagerStub::CancelUnloadSystemAbilityInner result is %{public}d", result);
723     bool ret = reply.WriteInt32(result);
724     if (!ret) {
725         HILOGW("SystemAbilityManagerStub::CancelUnloadSystemAbilityInner write reply failed.");
726         return ERR_FLATTEN_OBJECT;
727     }
728     return result;
729 }
730 
GetOnDemandReasonExtraDataInner(MessageParcel & data,MessageParcel & reply)731 int32_t SystemAbilityManagerStub::GetOnDemandReasonExtraDataInner(MessageParcel& data, MessageParcel& reply)
732 {
733     if (!CanRequest()) {
734         HILOGE("GetOnDemandReasonExtraData PERMISSION DENIED!");
735         return ERR_PERMISSION_DENIED;
736     }
737     int64_t extraDataId = -1;
738     if (!data.ReadInt64(extraDataId)) {
739         HILOGW("SystemAbilityManagerStub::GetOnDemandReasonExtraData read extraDataId failed.");
740         return ERR_FLATTEN_OBJECT;
741     }
742     MessageParcel extraDataParcel;
743     int32_t result = GetOnDemandReasonExtraData(extraDataId, extraDataParcel);
744     HILOGD("SystemAbilityManagerStub::GetOnDemandReasonExtraData result is %{public}d", result);
745     if (!reply.WriteInt32(result)) {
746         HILOGW("SystemAbilityManagerStub::GetOnDemandReasonExtraData write reply failed.");
747         return ERR_FLATTEN_OBJECT;
748     }
749     sptr<OnDemandReasonExtraData> extraData;
750     extraData = extraDataParcel.ReadParcelable<OnDemandReasonExtraData>();
751     if (extraData == nullptr) {
752         HILOGW("SystemAbilityManagerStub::GetOnDemandReasonExtraData read extraData failed.");
753         return ERR_FLATTEN_OBJECT;
754     }
755     if (!reply.WriteParcelable(extraData)) {
756         HILOGW("SystemAbilityManagerStub::GetOnDemandReasonExtraData write extraData failed.");
757         return ERR_FLATTEN_OBJECT;
758     }
759     return ERR_OK;
760 }
761 
GetOnDemandPolicyInner(MessageParcel & data,MessageParcel & reply)762 int32_t SystemAbilityManagerStub::GetOnDemandPolicyInner(MessageParcel& data, MessageParcel& reply)
763 {
764     if (!CanRequest()) {
765         HILOGE("GetOnDemandPolicyInner PERMISSION DENIED!");
766         return ERR_PERMISSION_DENIED;
767     }
768     int32_t systemAbilityId = 0;
769     if (!data.ReadInt32(systemAbilityId)) {
770         HILOGW("GetOnDemandPolicyInner read saId failed.");
771         return ERR_FLATTEN_OBJECT;
772     }
773     int32_t type = 0;
774     if (!data.ReadInt32(type)) {
775         HILOGW("GetOnDemandPolicyInner read type failed.");
776         return ERR_FLATTEN_OBJECT;
777     }
778     OnDemandPolicyType typeEnum = static_cast<OnDemandPolicyType>(type);
779     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
780     int32_t result = GetOnDemandPolicy(systemAbilityId, typeEnum, abilityOnDemandEvents);
781     if (!reply.WriteInt32(result)) {
782         HILOGW("GetOnDemandPolicyInner write result failed.");
783         return ERR_FLATTEN_OBJECT;
784     }
785     if (!OnDemandEventToParcel::WriteOnDemandEventsToParcel(abilityOnDemandEvents, reply)) {
786         HILOGW("GetOnDemandPolicyInner write on demand event failed.");
787         return ERR_FLATTEN_OBJECT;
788     }
789     return ERR_OK;
790 }
791 
UpdateOnDemandPolicyInner(MessageParcel & data,MessageParcel & reply)792 int32_t SystemAbilityManagerStub::UpdateOnDemandPolicyInner(MessageParcel& data, MessageParcel& reply)
793 {
794     if (!CanRequest()) {
795         HILOGE("UpdateOnDemandPolicyInner PERMISSION DENIED!");
796         return ERR_PERMISSION_DENIED;
797     }
798     int32_t systemAbilityId = 0;
799     if (!data.ReadInt32(systemAbilityId)) {
800         HILOGW("UpdateOnDemandPolicyInner read saId failed.");
801         return ERR_FLATTEN_OBJECT;
802     }
803     int32_t type = 0;
804     if (!data.ReadInt32(type)) {
805         HILOGW("GetOnDemandPolicyInner read type failed.");
806         return ERR_FLATTEN_OBJECT;
807     }
808     OnDemandPolicyType typeEnum = static_cast<OnDemandPolicyType>(type);
809     std::vector<SystemAbilityOnDemandEvent> abilityOnDemandEvents;
810     if (!OnDemandEventToParcel::ReadOnDemandEventsFromParcel(abilityOnDemandEvents, data)) {
811         HILOGW("UpdateOnDemandPolicyInner read on demand event failed.");
812         return ERR_FLATTEN_OBJECT;
813     }
814     int32_t result = UpdateOnDemandPolicy(systemAbilityId, typeEnum, abilityOnDemandEvents);
815     if (!reply.WriteInt32(result)) {
816         HILOGW("UpdateOnDemandPolicyInner write result failed.");
817         return ERR_FLATTEN_OBJECT;
818     }
819     return ERR_OK;
820 }
821 
CanRequest()822 bool SystemAbilityManagerStub::CanRequest()
823 {
824     auto accessTokenId = IPCSkeleton::GetCallingTokenID();
825     AccessToken::ATokenTypeEnum tokenType = AccessToken::AccessTokenKit::GetTokenTypeFlag(accessTokenId);
826     HILOGD("SystemAbilityManagerStub::CanRequest tokenId:%{private}u, tokenType:%{public}d",
827         accessTokenId, tokenType);
828     return (tokenType == AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
829 }
830 } // namespace OHOS
831