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 "accesstoken_kit.h"
19 #include "errors.h"
20 #include "ipc_skeleton.h"
21 #include "ipc_types.h"
22 #include "sam_log.h"
23 #include "string_ex.h"
24 #include "system_ability_manager.h"
25 #include "tools.h"
26
27 using namespace OHOS::Security;
28
29 namespace OHOS {
SystemAbilityManagerStub()30 SystemAbilityManagerStub::SystemAbilityManagerStub()
31 {
32 memberFuncMap_[GET_SYSTEM_ABILITY_TRANSACTION] =
33 &SystemAbilityManagerStub::GetSystemAbilityInner;
34 memberFuncMap_[CHECK_SYSTEM_ABILITY_TRANSACTION] =
35 &SystemAbilityManagerStub::CheckSystemAbilityInner;
36 memberFuncMap_[ADD_SYSTEM_ABILITY_TRANSACTION] =
37 &SystemAbilityManagerStub::AddSystemAbilityInner;
38 memberFuncMap_[REMOVE_SYSTEM_ABILITY_TRANSACTION] =
39 &SystemAbilityManagerStub::RemoveSystemAbilityInner;
40 memberFuncMap_[LIST_SYSTEM_ABILITY_TRANSACTION] =
41 &SystemAbilityManagerStub::ListSystemAbilityInner;
42 memberFuncMap_[SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION] =
43 &SystemAbilityManagerStub::SubsSystemAbilityInner;
44 memberFuncMap_[CHECK_REMOTE_SYSTEM_ABILITY_TRANSACTION] =
45 &SystemAbilityManagerStub::CheckRemtSystemAbilityInner;
46 memberFuncMap_[ADD_ONDEMAND_SYSTEM_ABILITY_TRANSACTION] =
47 &SystemAbilityManagerStub::AddOndemandSystemAbilityInner;
48 memberFuncMap_[CHECK_SYSTEM_ABILITY_IMMEDIATELY_TRANSACTION] =
49 &SystemAbilityManagerStub::CheckSystemAbilityImmeInner;
50 memberFuncMap_[UNSUBSCRIBE_SYSTEM_ABILITY_TRANSACTION] =
51 &SystemAbilityManagerStub::UnSubsSystemAbilityInner;
52 memberFuncMap_[ADD_SYSTEM_PROCESS_TRANSACTION] =
53 &SystemAbilityManagerStub::AddSystemProcessInner;
54 memberFuncMap_[LOAD_SYSTEM_ABILITY_TRANSACTION] =
55 &SystemAbilityManagerStub::LoadSystemAbilityInner;
56 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)57 int32_t SystemAbilityManagerStub::OnRemoteRequest(uint32_t code,
58 MessageParcel& data, MessageParcel& reply, MessageOption &option)
59 {
60 HILOGI("SystemAbilityManagerStub::OnReceived, code = %{public}d, flags= %{public}d",
61 code, option.GetFlags());
62 if (code != GET_SYSTEM_ABILITY_TRANSACTION && code != CHECK_SYSTEM_ABILITY_TRANSACTION) {
63 if (!EnforceInterceToken(data)) {
64 HILOGI("SystemAbilityManagerStub::OnReceived, code = %{public}d, check interfaceToken failed", code);
65 return ERR_PERMISSION_DENIED;
66 }
67 }
68 auto itFunc = memberFuncMap_.find(code);
69 if (itFunc != memberFuncMap_.end()) {
70 auto memberFunc = itFunc->second;
71 if (memberFunc != nullptr) {
72 return (this->*memberFunc)(data, reply);
73 }
74 }
75 HILOGW("SystemAbilityManagerStub: default case, need check.");
76 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
77 }
78
EnforceInterceToken(MessageParcel & data)79 bool SystemAbilityManagerStub::EnforceInterceToken(MessageParcel& data)
80 {
81 std::u16string interfaceToken = data.ReadInterfaceToken();
82 return interfaceToken == SAMANAGER_INTERFACE_TOKEN;
83 }
84
ListSystemAbilityInner(MessageParcel & data,MessageParcel & reply)85 int32_t SystemAbilityManagerStub::ListSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
86 {
87 if (!CanRequest()) {
88 HILOGE("ListSystemAbilityInner PERMISSION DENIED!");
89 return ERR_PERMISSION_DENIED;
90 }
91 int32_t dumpFlag = 0;
92 bool ret = data.ReadInt32(dumpFlag);
93 if (!ret) {
94 HILOGW("SystemAbilityManagerStub::ListSystemAbilityInner read dumpflag failed!");
95 return ERR_FLATTEN_OBJECT;
96 }
97
98 std::vector<std::u16string> saNameVector = ListSystemAbilities(dumpFlag);
99 if (saNameVector.empty()) {
100 HILOGI("List System Abilities list errors");
101 ret = reply.WriteInt32(ERR_INVALID_VALUE);
102 } else {
103 HILOGI("SystemAbilityManagerStub::ListSystemAbilityInner list success");
104 ret = reply.WriteInt32(ERR_NONE);
105 if (ret) {
106 ret = reply.WriteString16Vector(saNameVector);
107 }
108 }
109
110 if (!ret) {
111 HILOGW("SystemAbilityManagerStub::ListSystemAbilityInner write reply failed.");
112 return ERR_FLATTEN_OBJECT;
113 }
114
115 return ERR_NONE;
116 }
117
SubsSystemAbilityInner(MessageParcel & data,MessageParcel & reply)118 int32_t SystemAbilityManagerStub::SubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
119 {
120 if (!CanRequest()) {
121 HILOGE("SubsSystemAbilityInner PERMISSION DENIED!");
122 return ERR_PERMISSION_DENIED;
123 }
124 int32_t systemAbilityId = data.ReadInt32();
125 if (!CheckInputSysAbilityId(systemAbilityId)) {
126 HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner read systemAbilityId failed!");
127 return ERR_NULL_OBJECT;
128 }
129 sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
130 if (remoteObject == nullptr) {
131 HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner read listener failed!");
132 return ERR_NULL_OBJECT;
133 }
134 sptr<ISystemAbilityStatusChange> listener = iface_cast<ISystemAbilityStatusChange>(remoteObject);
135 if (listener == nullptr) {
136 HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner iface_cast failed!");
137 return ERR_NULL_OBJECT;
138 }
139 int32_t result = SubscribeSystemAbility(systemAbilityId, listener);
140 HILOGI("SystemAbilityManagerStub::SubsSystemAbilityInner result is %d", result);
141 bool ret = reply.WriteInt32(result);
142 if (!ret) {
143 HILOGW("SystemAbilityManagerStub::SubsSystemAbilityInner write reply failed.");
144 return ERR_FLATTEN_OBJECT;
145 }
146
147 return result;
148 }
149
UnSubsSystemAbilityInner(MessageParcel & data,MessageParcel & reply)150 int32_t SystemAbilityManagerStub::UnSubsSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
151 {
152 if (!CanRequest()) {
153 HILOGE("UnSubsSystemAbilityInner PERMISSION DENIED!");
154 return ERR_PERMISSION_DENIED;
155 }
156 int32_t systemAbilityId = data.ReadInt32();
157 if (!CheckInputSysAbilityId(systemAbilityId)) {
158 HILOGW("SystemAbilityManagerStub::UnSubsSystemAbilityInner read systemAbilityId failed!");
159 return ERR_NULL_OBJECT;
160 }
161 sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
162 if (remoteObject == nullptr) {
163 HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility read listener failed!");
164 return ERR_NULL_OBJECT;
165 }
166 sptr<ISystemAbilityStatusChange> listener = iface_cast<ISystemAbilityStatusChange>(remoteObject);
167 if (listener == nullptr) {
168 HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility iface_cast failed!");
169 return ERR_NULL_OBJECT;
170 }
171 int32_t result = UnSubscribeSystemAbility(systemAbilityId, listener);
172 HILOGI("SystemAbilityManagerStub::UnSubscribeSystemAbility result is %d", result);
173 bool ret = reply.WriteInt32(result);
174 if (!ret) {
175 HILOGW("SystemAbilityManagerStub::UnSubscribeSystemAbility write reply failed.");
176 return ERR_FLATTEN_OBJECT;
177 }
178
179 return result;
180 }
181
CheckRemtSystemAbilityInner(MessageParcel & data,MessageParcel & reply)182 int32_t SystemAbilityManagerStub::CheckRemtSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
183 {
184 if (!CanRequest()) {
185 HILOGE("CheckRemoteSystemAbilityInner PERMISSION DENIED!");
186 return ERR_PERMISSION_DENIED;
187 }
188 int32_t systemAbilityId = data.ReadInt32();
189 if (!CheckInputSysAbilityId(systemAbilityId)) {
190 HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner read systemAbilityId failed!");
191 return ERR_NULL_OBJECT;
192 }
193
194 std::string deviceId;
195 bool ret = data.ReadString(deviceId);
196 if (!ret) {
197 HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner read deviceId failed!");
198 return ERR_FLATTEN_OBJECT;
199 }
200 std::string uuid = SystemAbilityManager::GetInstance()->TransformDeviceId(deviceId, UUID, false);
201 ret = reply.WriteRemoteObject(GetSystemAbility(systemAbilityId, uuid));
202 if (!ret) {
203 HILOGW("SystemAbilityManagerStub::CheckRemtSystemAbilityInner write reply failed.");
204 return ERR_FLATTEN_OBJECT;
205 }
206
207 return ERR_NONE;
208 }
209
AddOndemandSystemAbilityInner(MessageParcel & data,MessageParcel & reply)210 int32_t SystemAbilityManagerStub::AddOndemandSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
211 {
212 if (!CanRequest()) {
213 HILOGE("AddOndemandSystemAbilityInner PERMISSION DENIED!");
214 return ERR_PERMISSION_DENIED;
215 }
216 int32_t systemAbilityId = data.ReadInt32();
217 if (!CheckInputSysAbilityId(systemAbilityId)) {
218 HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner read systemAbilityId failed!");
219 return ERR_NULL_OBJECT;
220 }
221 std::u16string localManagerName = data.ReadString16();
222 if (localManagerName.empty()) {
223 HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner read localName failed!");
224 return ERR_NULL_OBJECT;
225 }
226
227 int32_t result = AddOnDemandSystemAbilityInfo(systemAbilityId, localManagerName);
228 HILOGI("SystemAbilityManagerStub::AddOndemandSystemAbilityInner result is %d", result);
229 bool ret = reply.WriteInt32(result);
230 if (!ret) {
231 HILOGW("SystemAbilityManagerStub::AddOndemandSystemAbilityInner write reply failed.");
232 return ERR_FLATTEN_OBJECT;
233 }
234
235 return result;
236 }
237
CheckSystemAbilityImmeInner(MessageParcel & data,MessageParcel & reply)238 int32_t SystemAbilityManagerStub::CheckSystemAbilityImmeInner(MessageParcel& data, MessageParcel& reply)
239 {
240 int32_t systemAbilityId = data.ReadInt32();
241 if (!CheckInputSysAbilityId(systemAbilityId)) {
242 HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner read systemAbilityId failed!");
243 return ERR_NULL_OBJECT;
244 }
245 bool isExist = false;
246 bool ret = data.ReadBool(isExist);
247 if (!ret) {
248 HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner read isExist failed!");
249 return ERR_FLATTEN_OBJECT;
250 }
251 ret = reply.WriteRemoteObject(CheckSystemAbility(systemAbilityId, isExist));
252 if (!ret) {
253 HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner write reply failed.");
254 return ERR_FLATTEN_OBJECT;
255 }
256
257 ret = reply.WriteBool(isExist);
258 if (!ret) {
259 HILOGW("SystemAbilityManagerStub::CheckSystemAbilityImmeInner write reply failed.");
260 return ERR_FLATTEN_OBJECT;
261 }
262
263 return ERR_NONE;
264 }
265
UnmarshalingSaExtraProp(MessageParcel & data,SAExtraProp & extraProp)266 int32_t SystemAbilityManagerStub::UnmarshalingSaExtraProp(MessageParcel& data, SAExtraProp& extraProp)
267 {
268 bool isDistributed = false;
269 bool ret = data.ReadBool(isDistributed);
270 if (!ret) {
271 HILOGW("SystemAbilityManagerStub::UnmarshalingSaExtraProp read isDistributed failed!");
272 return ERR_FLATTEN_OBJECT;
273 }
274
275 int32_t dumpFlags = 0;
276 ret = data.ReadInt32(dumpFlags);
277 if (!ret || dumpFlags < 0) {
278 HILOGW("SystemAbilityManagerStub::UnmarshalingSaExtraProp dumpFlags failed!");
279 return ERR_FLATTEN_OBJECT;
280 }
281 std::u16string capability = data.ReadString16();
282 std::u16string permission = data.ReadString16();
283 extraProp.isDistributed = isDistributed;
284 extraProp.dumpFlags = static_cast<uint32_t>(dumpFlags);
285 extraProp.capability = capability;
286 extraProp.permission = permission;
287 return ERR_OK;
288 }
289
AddSystemAbilityInner(MessageParcel & data,MessageParcel & reply)290 int32_t SystemAbilityManagerStub::AddSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
291 {
292 if (!CanRequest()) {
293 HILOGE("AddSystemAbilityInner PERMISSION DENIED!");
294 return ERR_PERMISSION_DENIED;
295 }
296 int32_t systemAbilityId = data.ReadInt32();
297 if (!CheckInputSysAbilityId(systemAbilityId)) {
298 HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner read systemAbilityId failed!");
299 return ERR_NULL_OBJECT;
300 }
301 auto object = data.ReadRemoteObject();
302 if (object == nullptr) {
303 HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner readParcelable failed!");
304 return ERR_NULL_OBJECT;
305 }
306 SAExtraProp extraProp;
307 int32_t result = UnmarshalingSaExtraProp(data, extraProp);
308 if (result != ERR_OK) {
309 HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner UnmarshalingSaExtraProp failed!");
310 return result;
311 }
312 result = AddSystemAbility(systemAbilityId, object, extraProp);
313 HILOGI("SystemAbilityManagerStub::AddSystemAbilityExtraInner result is %d", result);
314 bool ret = reply.WriteInt32(result);
315 if (!ret) {
316 HILOGW("SystemAbilityManagerStub::AddSystemAbilityExtraInner write reply failed.");
317 return ERR_FLATTEN_OBJECT;
318 }
319 return result;
320 }
321
GetSystemAbilityInner(MessageParcel & data,MessageParcel & reply)322 int32_t SystemAbilityManagerStub::GetSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
323 {
324 int32_t systemAbilityId = data.ReadInt32();
325 if (!CheckInputSysAbilityId(systemAbilityId)) {
326 HILOGW("SystemAbilityManagerStub::GetSystemAbilityInner read systemAbilityId failed!");
327 return ERR_NULL_OBJECT;
328 }
329 bool ret = reply.WriteRemoteObject(GetSystemAbility(systemAbilityId));
330 if (!ret) {
331 HILOGW("SystemAbilityManagerStub:GetSystemAbilityInner write reply failed.");
332 return ERR_FLATTEN_OBJECT;
333 }
334 return ERR_NONE;
335 }
336
CheckSystemAbilityInner(MessageParcel & data,MessageParcel & reply)337 int32_t SystemAbilityManagerStub::CheckSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
338 {
339 int32_t systemAbilityId = data.ReadInt32();
340 if (!CheckInputSysAbilityId(systemAbilityId)) {
341 HILOGW("SystemAbilityManagerStub::CheckSystemAbilityInner read systemAbilityId failed!");
342 return ERR_NULL_OBJECT;
343 }
344 bool ret = reply.WriteRemoteObject(CheckSystemAbility(systemAbilityId));
345 if (!ret) {
346 HILOGW("SystemAbilityManagerStub:CheckSystemAbilityInner write reply failed.");
347 return ERR_FLATTEN_OBJECT;
348 }
349 return ERR_NONE;
350 }
351
RemoveSystemAbilityInner(MessageParcel & data,MessageParcel & reply)352 int32_t SystemAbilityManagerStub::RemoveSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
353 {
354 if (!CanRequest()) {
355 HILOGE("RemoveSystemAbilityInner PERMISSION DENIED!");
356 return ERR_PERMISSION_DENIED;
357 }
358 int32_t systemAbilityId = data.ReadInt32();
359 if (!CheckInputSysAbilityId(systemAbilityId)) {
360 HILOGW("SystemAbilityManagerStub::RemoveSystemAbilityInner read systemAbilityId failed!");
361 return ERR_NULL_OBJECT;
362 }
363 int32_t result = RemoveSystemAbility(systemAbilityId);
364 HILOGI("SystemAbilityManagerStub::RemoveSystemAbilityInner result is %{public}d", result);
365 bool ret = reply.WriteInt32(result);
366 if (!ret) {
367 HILOGW("SystemAbilityManagerStub::RemoveSystemAbilityInner write reply failed.");
368 return ERR_FLATTEN_OBJECT;
369 }
370 return result;
371 }
372
373
AddSystemProcessInner(MessageParcel & data,MessageParcel & reply)374 int32_t SystemAbilityManagerStub::AddSystemProcessInner(MessageParcel& data, MessageParcel& reply)
375 {
376 if (!CanRequest()) {
377 HILOGE("AddSystemProcessInner PERMISSION DENIED!");
378 return ERR_PERMISSION_DENIED;
379 }
380 std::u16string procName = data.ReadString16();
381 if (procName.empty()) {
382 HILOGW("SystemAbilityManagerStub::AddSystemProcessInner read process name failed!");
383 return ERR_NULL_OBJECT;
384 }
385
386 sptr<IRemoteObject> procObject = data.ReadRemoteObject();
387 if (procObject == nullptr) {
388 HILOGW("SystemAbilityManagerStub::AddSystemProcessInner readParcelable failed!");
389 return ERR_NULL_OBJECT;
390 }
391
392 int32_t result = AddSystemProcess(procName, procObject);
393 HILOGI("SystemAbilityManagerStub::AddSystemProcessInner result is %{public}d", result);
394 bool ret = reply.WriteInt32(result);
395 if (!ret) {
396 HILOGW("SystemAbilityManagerStub::AddSystemProcessInner write reply failed.");
397 return ERR_FLATTEN_OBJECT;
398 }
399 return result;
400 }
401
LoadSystemAbilityInner(MessageParcel & data,MessageParcel & reply)402 int32_t SystemAbilityManagerStub::LoadSystemAbilityInner(MessageParcel& data, MessageParcel& reply)
403 {
404 int32_t systemAbilityId = data.ReadInt32();
405 if (!CheckInputSysAbilityId(systemAbilityId)) {
406 HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read systemAbilityId failed!");
407 return ERR_INVALID_VALUE;
408 }
409 sptr<IRemoteObject> remoteObject = data.ReadRemoteObject();
410 if (remoteObject == nullptr) {
411 HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read callback failed!");
412 return ERR_INVALID_VALUE;
413 }
414 sptr<ISystemAbilityLoadCallback> callback = iface_cast<ISystemAbilityLoadCallback>(remoteObject);
415 if (callback == nullptr) {
416 HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner iface_cast failed!");
417 return ERR_INVALID_VALUE;
418 }
419 int32_t result = LoadSystemAbility(systemAbilityId, callback);
420 HILOGI("SystemAbilityManagerStub::LoadSystemAbilityInner result is %{public}d", result);
421 bool ret = reply.WriteInt32(result);
422 if (!ret) {
423 HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner write reply failed.");
424 return ERR_FLATTEN_OBJECT;
425 }
426 return result;
427 }
428
CanRequest()429 bool SystemAbilityManagerStub::CanRequest()
430 {
431 auto accessTokenId = IPCSkeleton::GetCallingTokenID();
432 // accessTokenId equals 0 means kernel not supported
433 if (accessTokenId == 0) {
434 HILOGW("SystemAbilityManagerStub::CanRequest GetCallingTokenID kernel not supported!");
435 return true;
436 }
437 AccessToken::ATokenTypeEnum tokenType = AccessToken::AccessTokenKit::GetTokenTypeFlag(accessTokenId);
438 HILOGD("SystemAbilityManagerStub::CanRequest tokenId:%{public}u, tokenType:%{public}d",
439 accessTokenId, tokenType);
440 return (tokenType == AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
441 }
442 } // namespace OHOS
443