• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "sam_log.h"
28 #include "string_ex.h"
29 #include "system_ability_manager.h"
30 #include "tools.h"
31 
32 #ifdef WITH_SELINUX
33 #include "service_checker.h"
34 #endif
35 
36 namespace {
37 #ifdef WITH_SELINUX
38     using namespace OHOS::HiviewDFX;
39     OHOS::HiviewDFX::HiLogLabel label_ = { LOG_CORE, 0xD001800, "SA_SELINUX" };
40     std::unique_ptr<ServiceChecker> selinuxChecker_ = std::make_unique<ServiceChecker>(false);
41 #endif
42 
CheckGetSAPermission(const int32_t said)43     bool CheckGetSAPermission(const int32_t said)
44     {
45 #ifdef WITH_SELINUX
46         int64_t begin = OHOS::GetTickCount();
47         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
48         auto ret = selinuxChecker_->GetServiceCheck(callingPid, std::to_string(said)) == 0;
49         HiLog::Debug(label_, "[Performance] GetServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
50             said, OHOS::GetTickCount() - begin);
51         return  ret;
52 #else
53         return true; // if not support selinux, not check selinux permission
54 #endif
55     }
56 
CheckAddOrRemovePermission(const int32_t said)57     bool CheckAddOrRemovePermission(const int32_t said)
58     {
59 #ifdef WITH_SELINUX
60         int64_t begin = OHOS::GetTickCount();
61         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
62         auto ret = selinuxChecker_->AddServiceCheck(callingPid, std::to_string(said)) == 0;
63         HiLog::Debug(label_, "[Performance] AddServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
64             said, OHOS::GetTickCount() - begin);
65         return ret;
66 #else
67         return true; // if not support selinux, not check selinux permission
68 #endif
69     }
70 
CheckGetRemoteSAPermission(const int32_t said)71     bool CheckGetRemoteSAPermission(const int32_t said)
72     {
73 #ifdef WITH_SELINUX
74         int64_t begin = OHOS::GetTickCount();
75         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
76         auto ret = selinuxChecker_->GetRemoteServiceCheck(callingPid, std::to_string(said)) == 0;
77         HiLog::Debug(label_, "[Performance] GetRemoteServiceCheck SA : %{public}d spend %{public}" PRId64 " ms",
78             said, OHOS::GetTickCount() - begin);
79         return ret;
80 #else
81         return true; // if not support selinux, not check selinux permission
82 #endif
83     }
84 
CheckListSAPermission()85     bool CheckListSAPermission()
86     {
87 #ifdef WITH_SELINUX
88         int64_t begin = OHOS::GetTickCount();
89         auto callingPid = OHOS::IPCSkeleton::GetCallingPid();
90         auto ret = selinuxChecker_->ListServiceCheck(callingPid) == 0;
91         HiLog::Debug(label_, "[Performance] ListServiceCheck spend %{public}" PRId64 " ms",
92             OHOS::GetTickCount() - begin);
93         return ret;
94 #else
95         return true; // if not support selinux, not check selinux permission
96 #endif
97     }
98 }
99 
100 using namespace OHOS::Security;
101 namespace OHOS {
SystemAbilityManagerStub()102 SystemAbilityManagerStub::SystemAbilityManagerStub()
103 {
104     memberFuncMap_[GET_SYSTEM_ABILITY_TRANSACTION] =
105         &SystemAbilityManagerStub::GetSystemAbilityInner;
106     memberFuncMap_[CHECK_SYSTEM_ABILITY_TRANSACTION] =
107         &SystemAbilityManagerStub::CheckSystemAbilityInner;
108     memberFuncMap_[ADD_SYSTEM_ABILITY_TRANSACTION] =
109         &SystemAbilityManagerStub::AddSystemAbilityInner;
110     memberFuncMap_[REMOVE_SYSTEM_ABILITY_TRANSACTION] =
111         &SystemAbilityManagerStub::RemoveSystemAbilityInner;
112     memberFuncMap_[LIST_SYSTEM_ABILITY_TRANSACTION] =
113         &SystemAbilityManagerStub::ListSystemAbilityInner;
114     memberFuncMap_[SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION] =
115         &SystemAbilityManagerStub::SubsSystemAbilityInner;
116     memberFuncMap_[CHECK_REMOTE_SYSTEM_ABILITY_TRANSACTION] =
117         &SystemAbilityManagerStub::CheckRemtSystemAbilityInner;
118     memberFuncMap_[ADD_ONDEMAND_SYSTEM_ABILITY_TRANSACTION] =
119         &SystemAbilityManagerStub::AddOndemandSystemAbilityInner;
120     memberFuncMap_[CHECK_SYSTEM_ABILITY_IMMEDIATELY_TRANSACTION] =
121         &SystemAbilityManagerStub::CheckSystemAbilityImmeInner;
122     memberFuncMap_[UNSUBSCRIBE_SYSTEM_ABILITY_TRANSACTION] =
123         &SystemAbilityManagerStub::UnSubsSystemAbilityInner;
124     memberFuncMap_[ADD_SYSTEM_PROCESS_TRANSACTION] =
125         &SystemAbilityManagerStub::AddSystemProcessInner;
126     memberFuncMap_[LOAD_SYSTEM_ABILITY_TRANSACTION] =
127         &SystemAbilityManagerStub::LoadSystemAbilityInner;
128     memberFuncMap_[LOAD_REMOTE_SYSTEM_ABILITY_TRANSACTION] =
129         &SystemAbilityManagerStub::LoadRemoteSystemAbilityInner;
130 }
131 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)132 int32_t SystemAbilityManagerStub::OnRemoteRequest(uint32_t code,
133     MessageParcel& data, MessageParcel& reply, MessageOption &option)
134 {
135     HILOGI("SystemAbilityManagerStub::OnReceived, code = %{public}u, callerPid = %{public}d, flags= %{public}d",
136         code, IPCSkeleton::GetCallingPid(), option.GetFlags());
137     if (!EnforceInterceToken(data)) {
138         HILOGE("SystemAbilityManagerStub::OnReceived, code = %{public}u, check interfaceToken failed", code);
139         return ERR_PERMISSION_DENIED;
140     }
141     auto itFunc = memberFuncMap_.find(code);
142     if (itFunc != memberFuncMap_.end()) {
143         auto memberFunc = itFunc->second;
144         if (memberFunc != nullptr) {
145             return (this->*memberFunc)(data, reply);
146         }
147     }
148     HILOGW("SystemAbilityManagerStub: default case, need check.");
149     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
150 }
151 
EnforceInterceToken(MessageParcel & data)152 bool SystemAbilityManagerStub::EnforceInterceToken(MessageParcel& data)
153 {
154     std::u16string interfaceToken = data.ReadInterfaceToken();
155     return interfaceToken == SAMANAGER_INTERFACE_TOKEN;
156 }
157 
ListSystemAbilityInner(MessageParcel & data,MessageParcel & reply)158 int32_t SystemAbilityManagerStub::ListSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
159 {
160     if (!CanRequest()) {
161         HILOGE("ListSystemAbilityInner PERMISSION DENIED!");
162         return ERR_PERMISSION_DENIED;
163     }
164 
165     if (!CheckListSAPermission()) {
166         HILOGE("ListSystemAbilityInner selinux permission denied!");
167         return ERR_PERMISSION_DENIED;
168     }
169 
170     int32_t dumpFlag = 0;
171     bool ret = data.ReadInt32(dumpFlag);
172     if (!ret) {
173         HILOGW("SystemAbilityManagerStub::ListSystemAbilityInner read dumpflag failed!");
174         return ERR_FLATTEN_OBJECT;
175     }
176 
177     std::vector<std::u16string> saNameVector = ListSystemAbilities(dumpFlag);
178     if (saNameVector.empty()) {
179         HILOGI("List System Abilities list errors");
180         ret = reply.WriteInt32(ERR_INVALID_VALUE);
181     } else {
182         HILOGI("SystemAbilityManagerStub::ListSystemAbilityInner list success");
183         ret = reply.WriteInt32(ERR_NONE);
184         if (ret) {
185             ret = reply.WriteString16Vector(saNameVector);
186         }
187     }
188 
189     if (!ret) {
190         HILOGW("SystemAbilityManagerStub::ListSystemAbilityInner write reply failed.");
191         return ERR_FLATTEN_OBJECT;
192     }
193 
194     return ERR_NONE;
195 }
196 
SubsSystemAbilityInner(MessageParcel & data,MessageParcel & reply)197 int32_t SystemAbilityManagerStub::SubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
198 {
199     if (!CanRequest()) {
200         HILOGE("SubsSystemAbilityInner PERMISSION DENIED!");
201         return ERR_PERMISSION_DENIED;
202     }
203     int32_t systemAbilityId = data.ReadInt32();
204     if (!CheckInputSysAbilityId(systemAbilityId)) {
205         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner read systemAbilityId failed!");
206         return ERR_NULL_OBJECT;
207     }
208     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
209     if (remoteObject == nullptr) {
210         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner read listener failed!");
211         return ERR_NULL_OBJECT;
212     }
213     sptr<ISystemAbilityStatusChange> listener = iface_cast<ISystemAbilityStatusChange>(remoteObject);
214     if (listener == nullptr) {
215         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner iface_cast failed!");
216         return ERR_NULL_OBJECT;
217     }
218     int32_t result = SubscribeSystemAbility(systemAbilityId, listener);
219     HILOGD("SystemAbilityManagerStub::SubsSystemAbilityInner result is %d", result);
220     bool ret = reply.WriteInt32(result);
221     if (!ret) {
222         HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner write reply failed.");
223         return ERR_FLATTEN_OBJECT;
224     }
225 
226     return result;
227 }
228 
UnSubsSystemAbilityInner(MessageParcel & data,MessageParcel & reply)229 int32_t SystemAbilityManagerStub::UnSubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
230 {
231     if (!CanRequest()) {
232         HILOGE("UnSubsSystemAbilityInner PERMISSION DENIED!");
233         return ERR_PERMISSION_DENIED;
234     }
235     int32_t systemAbilityId = data.ReadInt32();
236     if (!CheckInputSysAbilityId(systemAbilityId)) {
237         HILOGW("SystemAbilityManagerStub::UnSubsSystemAbilityInner read systemAbilityId failed!");
238         return ERR_NULL_OBJECT;
239     }
240     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
241     if (remoteObject == nullptr) {
242         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility read listener failed!");
243         return ERR_NULL_OBJECT;
244     }
245     sptr<ISystemAbilityStatusChange> listener = iface_cast<ISystemAbilityStatusChange>(remoteObject);
246     if (listener == nullptr) {
247         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility iface_cast failed!");
248         return ERR_NULL_OBJECT;
249     }
250     int32_t result = UnSubscribeSystemAbility(systemAbilityId, listener);
251     HILOGD("SystemAbilityManagerStub::UnSubscribeSystemAbility result is %d", result);
252     bool ret = reply.WriteInt32(result);
253     if (!ret) {
254         HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility write reply failed.");
255         return ERR_FLATTEN_OBJECT;
256     }
257 
258     return result;
259 }
260 
CheckRemtSystemAbilityInner(MessageParcel & data,MessageParcel & reply)261 int32_t SystemAbilityManagerStub::CheckRemtSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
262 {
263     if (!CanRequest()) {
264         HILOGE("CheckRemoteSystemAbilityInner PERMISSION DENIED!");
265         return ERR_PERMISSION_DENIED;
266     }
267     int32_t systemAbilityId = data.ReadInt32();
268     if (!CheckInputSysAbilityId(systemAbilityId)) {
269         HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner read systemAbilityId failed!");
270         return ERR_NULL_OBJECT;
271     }
272 
273     if (!CheckGetRemoteSAPermission(systemAbilityId)) {
274         HILOGE("CheckRemtSystemAbilityInner selinux permission denied!, SA : %{public}d", systemAbilityId);
275         return ERR_PERMISSION_DENIED;
276     }
277 
278     std::string deviceId;
279     bool ret = data.ReadString(deviceId);
280     if (!ret) {
281         HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner read deviceId failed!");
282         return ERR_FLATTEN_OBJECT;
283     }
284     std::string uuid = SystemAbilityManager::GetInstance()->TransformDeviceId(deviceId, UUID, false);
285     ret = reply.WriteRemoteObject(GetSystemAbility(systemAbilityId, uuid));
286     if (!ret) {
287         HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner write reply failed.");
288         return ERR_FLATTEN_OBJECT;
289     }
290 
291     return ERR_NONE;
292 }
293 
AddOndemandSystemAbilityInner(MessageParcel & data,MessageParcel & reply)294 int32_t SystemAbilityManagerStub::AddOndemandSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
295 {
296     if (!CanRequest()) {
297         HILOGE("AddOndemandSystemAbilityInner PERMISSION DENIED!");
298         return ERR_PERMISSION_DENIED;
299     }
300     int32_t systemAbilityId = data.ReadInt32();
301     if (!CheckInputSysAbilityId(systemAbilityId)) {
302         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner read systemAbilityId failed!");
303         return ERR_NULL_OBJECT;
304     }
305 
306     if (!CheckAddOrRemovePermission(systemAbilityId)) {
307         HILOGE("AddOndemandSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
308         return ERR_PERMISSION_DENIED;
309     }
310 
311     std::u16string localManagerName = data.ReadString16();
312     if (localManagerName.empty()) {
313         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner read localName failed!");
314         return ERR_NULL_OBJECT;
315     }
316 
317     int32_t result = AddOnDemandSystemAbilityInfo(systemAbilityId, localManagerName);
318     HILOGD("SystemAbilityManagerStub::AddOndemandSystemAbilityInner result is %d", result);
319     bool ret = reply.WriteInt32(result);
320     if (!ret) {
321         HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner write reply failed.");
322         return ERR_FLATTEN_OBJECT;
323     }
324 
325     return result;
326 }
327 
CheckSystemAbilityImmeInner(MessageParcel & data,MessageParcel & reply)328 int32_t SystemAbilityManagerStub::CheckSystemAbilityImmeInner(MessageParcel& data, MessageParcel& reply)
329 {
330     int32_t systemAbilityId = data.ReadInt32();
331     if (!CheckInputSysAbilityId(systemAbilityId)) {
332         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner read systemAbilityId failed!");
333         return ERR_NULL_OBJECT;
334     }
335 
336     if (!CheckGetSAPermission(systemAbilityId)) {
337         HILOGE("CheckSystemAbilityImmeInner selinux permission denied! SA : %{public}d", systemAbilityId);
338         return ERR_PERMISSION_DENIED;
339     }
340 
341     bool isExist = false;
342     bool ret = data.ReadBool(isExist);
343     if (!ret) {
344         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner read isExist failed!");
345         return ERR_FLATTEN_OBJECT;
346     }
347     ret = reply.WriteRemoteObject(CheckSystemAbility(systemAbilityId, isExist));
348     if (!ret) {
349         return ERR_FLATTEN_OBJECT;
350     }
351 
352     ret = reply.WriteBool(isExist);
353     if (!ret) {
354         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner write reply failed.");
355         return ERR_FLATTEN_OBJECT;
356     }
357 
358     return ERR_NONE;
359 }
360 
UnmarshalingSaExtraProp(MessageParcel & data,SAExtraProp & extraProp)361 int32_t SystemAbilityManagerStub::UnmarshalingSaExtraProp(MessageParcel& data, SAExtraProp& extraProp)
362 {
363     bool isDistributed = false;
364     bool ret = data.ReadBool(isDistributed);
365     if (!ret) {
366         HILOGW("SystemAbilityManagerStub::UnmarshalingSaExtraProp read isDistributed failed!");
367         return ERR_FLATTEN_OBJECT;
368     }
369 
370     int32_t dumpFlags = 0;
371     ret = data.ReadInt32(dumpFlags);
372     if (!ret || dumpFlags < 0) {
373         HILOGW("SystemAbilityManagerStub::UnmarshalingSaExtraProp dumpFlags failed!");
374         return ERR_FLATTEN_OBJECT;
375     }
376     std::u16string capability = data.ReadString16();
377     std::u16string permission = data.ReadString16();
378     extraProp.isDistributed = isDistributed;
379     extraProp.dumpFlags = static_cast<uint32_t>(dumpFlags);
380     extraProp.capability = capability;
381     extraProp.permission = permission;
382     return ERR_OK;
383 }
384 
AddSystemAbilityInner(MessageParcel & data,MessageParcel & reply)385 int32_t SystemAbilityManagerStub::AddSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
386 {
387     if (!CanRequest()) {
388         HILOGE("AddSystemAbilityInner PERMISSION DENIED!");
389         return ERR_PERMISSION_DENIED;
390     }
391     int32_t systemAbilityId = data.ReadInt32();
392     if (!CheckInputSysAbilityId(systemAbilityId)) {
393         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner read systemAbilityId failed!");
394         return ERR_NULL_OBJECT;
395     }
396 
397     if (!CheckAddOrRemovePermission(systemAbilityId)) {
398         HILOGE("AddSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
399         return ERR_PERMISSION_DENIED;
400     }
401 
402     auto object = data.ReadRemoteObject();
403     if (object == nullptr) {
404         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner readParcelable failed!");
405         return ERR_NULL_OBJECT;
406     }
407     SAExtraProp extraProp;
408     int32_t result = UnmarshalingSaExtraProp(data, extraProp);
409     if (result != ERR_OK) {
410         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner UnmarshalingSaExtraProp failed!");
411         return result;
412     }
413     result = AddSystemAbility(systemAbilityId, object, extraProp);
414     bool ret = reply.WriteInt32(result);
415     if (!ret) {
416         HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner write reply failed.");
417         return ERR_FLATTEN_OBJECT;
418     }
419     return result;
420 }
421 
GetSystemAbilityInner(MessageParcel & data,MessageParcel & reply)422 int32_t SystemAbilityManagerStub::GetSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
423 {
424     int32_t systemAbilityId = data.ReadInt32();
425     if (!CheckInputSysAbilityId(systemAbilityId)) {
426         HILOGW("SystemAbilityManagerStub::GetSystemAbilityInner read systemAbilityId failed!");
427         return ERR_NULL_OBJECT;
428     }
429 
430     if (!CheckGetSAPermission(systemAbilityId)) {
431         HILOGE("GetSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
432         return ERR_PERMISSION_DENIED;
433     }
434 
435     bool ret = reply.WriteRemoteObject(GetSystemAbility(systemAbilityId));
436     if (!ret) {
437         HILOGW("SystemAbilityManagerStub:GetSystemAbilityInner write reply failed.");
438         return ERR_FLATTEN_OBJECT;
439     }
440     return ERR_NONE;
441 }
442 
CheckSystemAbilityInner(MessageParcel & data,MessageParcel & reply)443 int32_t SystemAbilityManagerStub::CheckSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
444 {
445     int32_t systemAbilityId = data.ReadInt32();
446     if (!CheckInputSysAbilityId(systemAbilityId)) {
447         HILOGW("SystemAbilityManagerStub::CheckSystemAbilityInner read systemAbilityId failed!");
448         return ERR_NULL_OBJECT;
449     }
450 
451     if (!CheckGetSAPermission(systemAbilityId)) {
452         HILOGE("CheckSystemAbilityInner selinux permission denied! SA : %{public}d", systemAbilityId);
453         return ERR_PERMISSION_DENIED;
454     }
455 
456     bool ret = reply.WriteRemoteObject(CheckSystemAbility(systemAbilityId));
457     if (!ret) {
458         return ERR_FLATTEN_OBJECT;
459     }
460     return ERR_NONE;
461 }
462 
RemoveSystemAbilityInner(MessageParcel & data,MessageParcel & reply)463 int32_t SystemAbilityManagerStub::RemoveSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
464 {
465     if (!CanRequest()) {
466         HILOGE("RemoveSystemAbilityInner PERMISSION DENIED!");
467         return ERR_PERMISSION_DENIED;
468     }
469     int32_t systemAbilityId = data.ReadInt32();
470     if (!CheckInputSysAbilityId(systemAbilityId)) {
471         HILOGW("SystemAbilityManagerStub::RemoveSystemAbilityInner read systemAbilityId failed!");
472         return ERR_NULL_OBJECT;
473     }
474 
475     if (!CheckAddOrRemovePermission(systemAbilityId)) {
476         HILOGE("RemoveSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
477         return ERR_PERMISSION_DENIED;
478     }
479 
480     int32_t result = RemoveSystemAbility(systemAbilityId);
481     bool ret = reply.WriteInt32(result);
482     if (!ret) {
483         HILOGW("SystemAbilityManagerStub::RemoveSystemAbilityInner write reply failed.");
484         return ERR_FLATTEN_OBJECT;
485     }
486     return result;
487 }
488 
489 
AddSystemProcessInner(MessageParcel & data,MessageParcel & reply)490 int32_t SystemAbilityManagerStub::AddSystemProcessInner(MessageParcel& data, MessageParcel& reply)
491 {
492     if (!CanRequest()) {
493         HILOGE("AddSystemProcessInner PERMISSION DENIED!");
494         return ERR_PERMISSION_DENIED;
495     }
496     std::u16string procName = data.ReadString16();
497     if (procName.empty()) {
498         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner read process name failed!");
499         return ERR_NULL_OBJECT;
500     }
501 
502     sptr<IRemoteObject> procObject = data.ReadRemoteObject();
503     if (procObject == nullptr) {
504         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner readParcelable failed!");
505         return ERR_NULL_OBJECT;
506     }
507 
508     int32_t result = AddSystemProcess(procName, procObject);
509     bool ret = reply.WriteInt32(result);
510     if (!ret) {
511         HILOGW("SystemAbilityManagerStub::AddSystemProcessInner write reply failed.");
512         return ERR_FLATTEN_OBJECT;
513     }
514     return result;
515 }
516 
LoadSystemAbilityInner(MessageParcel & data,MessageParcel & reply)517 int32_t SystemAbilityManagerStub::LoadSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
518 {
519     int32_t systemAbilityId = data.ReadInt32();
520     std::string loadSystemAbilityTag = ToString(systemAbilityId) + "_LoadSystemAbility";
521     HITRACE_METER_NAME(HITRACE_TAG_SAMGR, loadSystemAbilityTag);
522     if (!CheckInputSysAbilityId(systemAbilityId)) {
523         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read systemAbilityId failed!");
524         return ERR_INVALID_VALUE;
525     }
526 
527     if (!CheckGetSAPermission(systemAbilityId)) {
528         HILOGE("LoadSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
529         return ERR_PERMISSION_DENIED;
530     }
531 
532     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
533     if (remoteObject == nullptr) {
534         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read callback failed!");
535         return ERR_INVALID_VALUE;
536     }
537     sptr<ISystemAbilityLoadCallback> callback = iface_cast<ISystemAbilityLoadCallback>(remoteObject);
538     if (callback == nullptr) {
539         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner iface_cast failed!");
540         return ERR_INVALID_VALUE;
541     }
542     int32_t result = LoadSystemAbility(systemAbilityId, callback);
543     HILOGD("SystemAbilityManagerStub::LoadSystemAbilityInner result is %{public}d", result);
544     bool ret = reply.WriteInt32(result);
545     if (!ret) {
546         HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner write reply failed.");
547         return ERR_FLATTEN_OBJECT;
548     }
549     return result;
550 }
551 
LoadRemoteSystemAbilityInner(MessageParcel & data,MessageParcel & reply)552 int32_t SystemAbilityManagerStub::LoadRemoteSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
553 {
554     int32_t systemAbilityId = data.ReadInt32();
555     if (!CheckInputSysAbilityId(systemAbilityId)) {
556         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner systemAbilityId invalid");
557         return ERR_INVALID_VALUE;
558     }
559 
560     if (!CheckGetRemoteSAPermission(systemAbilityId)) {
561         HILOGE("LoadRemoteSystemAbilityInner selinux permission denied!SA : %{public}d", systemAbilityId);
562         return ERR_PERMISSION_DENIED;
563     }
564 
565     std::string deviceId = data.ReadString();
566     if (deviceId.empty()) {
567         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner read deviceId failed");
568         return ERR_INVALID_VALUE;
569     }
570     sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
571     if (remoteObject == nullptr) {
572         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner read callback failed!");
573         return ERR_INVALID_VALUE;
574     }
575     sptr<ISystemAbilityLoadCallback> callback = iface_cast<ISystemAbilityLoadCallback>(remoteObject);
576     if (callback == nullptr) {
577         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner iface_cast failed!");
578         return ERR_INVALID_VALUE;
579     }
580     int32_t result = LoadSystemAbility(systemAbilityId, deviceId, callback);
581     HILOGD("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner result is %{public}d", result);
582     bool ret = reply.WriteInt32(result);
583     if (!ret) {
584         HILOGW("SystemAbilityManagerStub::LoadRemoteSystemAbilityInner write reply failed.");
585         return ERR_FLATTEN_OBJECT;
586     }
587     return result;
588 }
589 
CanRequest()590 bool SystemAbilityManagerStub::CanRequest()
591 {
592     auto accessTokenId = IPCSkeleton::GetCallingTokenID();
593     AccessToken::ATokenTypeEnum tokenType = AccessToken::AccessTokenKit::GetTokenTypeFlag(accessTokenId);
594     HILOGD("SystemAbilityManagerStub::CanRequest tokenId:%{private}u, tokenType:%{public}d",
595         accessTokenId, tokenType);
596     return (tokenType == AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
597 }
598 } // namespace OHOS
599