1 /*
2 * Copyright (c) 2021 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 <string_ex.h>
17
18 #include "accesstoken_kit.h"
19 #include "access_token.h"
20 #include "ipc_skeleton.h"
21 #include "message_parcel_helper.h"
22 #include "securec.h"
23 #include "tokenid_kit.h"
24 #include "update_define.h"
25 #include "update_log.h"
26 #include "update_service_stub.h"
27 #include "update_system_event.h"
28 #include "updater_sa_ipc_interface_code.h"
29 #include "../../../interfaces/inner_api/modulemgr/include/module_manager.h"
30
31 using namespace std;
32
33 namespace OHOS {
34 namespace UpdateEngine {
35 constexpr const pid_t ROOT_UID = 0;
36 constexpr const pid_t EDM_UID = 3057;
37 static constexpr int32_t MAX_VECTOR_SIZE = 128;
38
39 #define CALL_RESULT_TO_IPC_RESULT(callResult) ((callResult) + CALL_RESULT_OFFSET)
40
41 #define RETURN_FAIL_WHEN_SERVICE_NULL(service) \
42 ENGINE_CHECK((service) != nullptr, return INT_CALL_IPC_ERR, "service null")
43
44 #define CALL_RESULT_TO_IPC_RESULT(callResult) ((callResult) + CALL_RESULT_OFFSET)
45
UpdateServiceStub()46 UpdateServiceStub::UpdateServiceStub()
47 {
48 requestFuncMap_ = {
49 {CAST_UINT(UpdaterSaInterfaceCode::CHECK_VERSION), &UpdateServiceStub::CheckNewVersionStub},
50 {CAST_UINT(UpdaterSaInterfaceCode::DOWNLOAD), &UpdateServiceStub::DownloadVersionStub},
51 {CAST_UINT(UpdaterSaInterfaceCode::PAUSE_DOWNLOAD), &UpdateServiceStub::PauseDownloadStub},
52 {CAST_UINT(UpdaterSaInterfaceCode::RESUME_DOWNLOAD), &UpdateServiceStub::ResumeDownloadStub},
53 {CAST_UINT(UpdaterSaInterfaceCode::UPGRADE), &UpdateServiceStub::DoUpdateStub},
54 {CAST_UINT(UpdaterSaInterfaceCode::CLEAR_ERROR), &UpdateServiceStub::ClearErrorStub},
55 {CAST_UINT(UpdaterSaInterfaceCode::TERMINATE_UPGRADE), &UpdateServiceStub::TerminateUpgradeStub},
56 {CAST_UINT(UpdaterSaInterfaceCode::SET_POLICY), &UpdateServiceStub::SetUpgradePolicyStub},
57 {CAST_UINT(UpdaterSaInterfaceCode::GET_POLICY), &UpdateServiceStub::GetUpgradePolicyStub},
58 {CAST_UINT(UpdaterSaInterfaceCode::GET_NEW_VERSION), &UpdateServiceStub::GetNewVersionStub},
59 {CAST_UINT(UpdaterSaInterfaceCode::GET_NEW_VERSION_DESCRIPTION),
60 &UpdateServiceStub::GetNewVersionDescriptionStub},
61 {CAST_UINT(UpdaterSaInterfaceCode::GET_CURRENT_VERSION), &UpdateServiceStub::GetCurrentVersionStub},
62 {CAST_UINT(UpdaterSaInterfaceCode::GET_CURRENT_VERSION_DESCRIPTION),
63 &UpdateServiceStub::GetCurrentVersionDescriptionStub},
64 {CAST_UINT(UpdaterSaInterfaceCode::GET_TASK_INFO), &UpdateServiceStub::GetTaskInfoStub},
65 {CAST_UINT(UpdaterSaInterfaceCode::REGISTER_CALLBACK), &UpdateServiceStub::RegisterUpdateCallbackStub},
66 {CAST_UINT(UpdaterSaInterfaceCode::UNREGISTER_CALLBACK), &UpdateServiceStub::UnregisterUpdateCallbackStub},
67 {CAST_UINT(UpdaterSaInterfaceCode::CANCEL), &UpdateServiceStub::CancelStub},
68 {CAST_UINT(UpdaterSaInterfaceCode::FACTORY_RESET), &UpdateServiceStub::FactoryResetStub},
69 {CAST_UINT(UpdaterSaInterfaceCode::APPLY_NEW_VERSION), &UpdateServiceStub::ApplyNewVersionStub},
70 {CAST_UINT(UpdaterSaInterfaceCode::VERIFY_UPGRADE_PACKAGE), &UpdateServiceStub::VerifyUpgradePackageStub}
71 };
72 }
73
GetNewVersionStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)74 int32_t UpdateServiceStub::GetNewVersionStub(UpdateServiceStubPtr service,
75 MessageParcel& data, MessageParcel& reply, MessageOption &option)
76 {
77 RETURN_FAIL_WHEN_SERVICE_NULL(service);
78 UpgradeInfo upgradeInfo {};
79 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
80 NewVersionInfo newVersionInfo = {};
81 BusinessError businessError = {};
82 int32_t ret = service->GetNewVersionInfo(upgradeInfo, newVersionInfo, businessError);
83 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to GetNewVersionInfo");
84 MessageParcelHelper::WriteBusinessError(reply, businessError);
85 MessageParcelHelper::WriteNewVersionInfo(reply, newVersionInfo);
86 return INT_CALL_SUCCESS;
87 }
88
GetNewVersionDescriptionStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)89 int32_t UpdateServiceStub::GetNewVersionDescriptionStub(UpdateServiceStubPtr service,
90 MessageParcel& data, MessageParcel& reply, MessageOption &option)
91 {
92 RETURN_FAIL_WHEN_SERVICE_NULL(service);
93 UpgradeInfo upgradeInfo;
94 VersionDigestInfo versionDigestInfo;
95 DescriptionOptions descriptionOptions;
96 VersionDescriptionInfo newVersionDescriptionInfo;
97 BusinessError businessError;
98 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
99 MessageParcelHelper::ReadVersionDigestInfo(data, versionDigestInfo);
100 MessageParcelHelper::ReadDescriptionOptions(data, descriptionOptions);
101
102 int32_t ret = service->GetNewVersionDescription(upgradeInfo, versionDigestInfo, descriptionOptions,
103 newVersionDescriptionInfo, businessError);
104 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to GetNewVersionDescription");
105 MessageParcelHelper::WriteBusinessError(reply, businessError);
106 MessageParcelHelper::WriteVersionDescriptionInfo(reply, newVersionDescriptionInfo);
107 return INT_CALL_SUCCESS;
108 }
109
GetCurrentVersionStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)110 int32_t UpdateServiceStub::GetCurrentVersionStub(UpdateServiceStubPtr service,
111 MessageParcel &data, MessageParcel &reply, MessageOption &option)
112 {
113 RETURN_FAIL_WHEN_SERVICE_NULL(service);
114 UpgradeInfo upgradeInfo {};
115 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
116 CurrentVersionInfo currentVersionInfo = {};
117 BusinessError businessError = {};
118 int32_t ret = service->GetCurrentVersionInfo(upgradeInfo, currentVersionInfo, businessError);
119 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to GetCurrentVersion");
120 MessageParcelHelper::WriteBusinessError(reply, businessError);
121 MessageParcelHelper::WriteCurrentVersionInfo(reply, currentVersionInfo);
122 return INT_CALL_SUCCESS;
123 }
124
GetCurrentVersionDescriptionStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)125 int32_t UpdateServiceStub::GetCurrentVersionDescriptionStub(UpdateServiceStubPtr service,
126 MessageParcel& data, MessageParcel& reply, MessageOption &option)
127 {
128 RETURN_FAIL_WHEN_SERVICE_NULL(service);
129 UpgradeInfo upgradeInfo;
130 DescriptionOptions descriptionOptions;
131 VersionDescriptionInfo currentVersionDescriptionInfo;
132 BusinessError businessError;
133 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
134 MessageParcelHelper::ReadDescriptionOptions(data, descriptionOptions);
135
136 int32_t ret = service->GetCurrentVersionDescription(upgradeInfo, descriptionOptions, currentVersionDescriptionInfo,
137 businessError);
138 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to GetCurrentVersionDescription");
139 MessageParcelHelper::WriteBusinessError(reply, businessError);
140 MessageParcelHelper::WriteVersionDescriptionInfo(reply, currentVersionDescriptionInfo);
141 return INT_CALL_SUCCESS;
142 }
143
GetTaskInfoStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)144 int32_t UpdateServiceStub::GetTaskInfoStub(UpdateServiceStubPtr service,
145 MessageParcel &data, MessageParcel &reply, MessageOption &option)
146 {
147 RETURN_FAIL_WHEN_SERVICE_NULL(service);
148 UpgradeInfo upgradeInfo {};
149 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
150 TaskInfo taskInfo = {};
151 BusinessError businessError = {};
152 int32_t ret = service->GetTaskInfo(upgradeInfo, taskInfo, businessError);
153 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to GetTaskInfo");
154 MessageParcelHelper::WriteBusinessError(reply, businessError);
155 MessageParcelHelper::WriteTaskInfo(reply, taskInfo);
156 return INT_CALL_SUCCESS;
157 }
158
CheckNewVersionStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)159 int32_t UpdateServiceStub::CheckNewVersionStub(UpdateServiceStub::UpdateServiceStubPtr service,
160 MessageParcel& data, MessageParcel& reply, MessageOption &option)
161 {
162 RETURN_FAIL_WHEN_SERVICE_NULL(service);
163 UpgradeInfo upgradeInfo {};
164 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
165 BusinessError businessError;
166 CheckResult checkResult;
167 int res = service->CheckNewVersion(upgradeInfo, businessError, checkResult);
168 ENGINE_CHECK(res == INT_CALL_SUCCESS, return res, "Failed to CheckNewVersion");
169 MessageParcelHelper::WriteBusinessError(reply, businessError);
170 MessageParcelHelper::WriteCheckResult(reply, checkResult);
171 return INT_CALL_SUCCESS;
172 }
173
DownloadVersionStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)174 int32_t UpdateServiceStub::DownloadVersionStub(UpdateServiceStubPtr service,
175 MessageParcel &data, MessageParcel &reply, MessageOption &option)
176 {
177 RETURN_FAIL_WHEN_SERVICE_NULL(service);
178 UpgradeInfo upgradeInfo;
179 VersionDigestInfo versionDigestInfo;
180 DownloadOptions downloadOptions;
181 BusinessError businessError;
182 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
183 MessageParcelHelper::ReadVersionDigestInfo(data, versionDigestInfo);
184 MessageParcelHelper::ReadDownloadOptions(data, downloadOptions);
185 int32_t ret = service->Download(upgradeInfo, versionDigestInfo, downloadOptions, businessError);
186 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to DownloadVersion");
187 MessageParcelHelper::WriteBusinessError(reply, businessError);
188 return INT_CALL_SUCCESS;
189 }
190
PauseDownloadStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)191 int32_t UpdateServiceStub::PauseDownloadStub(UpdateServiceStubPtr service,
192 MessageParcel& data, MessageParcel& reply, MessageOption &option)
193 {
194 RETURN_FAIL_WHEN_SERVICE_NULL(service);
195 UpgradeInfo upgradeInfo;
196 VersionDigestInfo versionDigestInfo;
197 PauseDownloadOptions pauseDownloadOptions;
198 BusinessError businessError;
199 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
200 MessageParcelHelper::ReadVersionDigestInfo(data, versionDigestInfo);
201 MessageParcelHelper::ReadPauseDownloadOptions(data, pauseDownloadOptions);
202
203 int32_t ret = service->PauseDownload(upgradeInfo, versionDigestInfo, pauseDownloadOptions, businessError);
204 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to PauseDownload");
205 MessageParcelHelper::WriteBusinessError(reply, businessError);
206 return INT_CALL_SUCCESS;
207 }
208
ResumeDownloadStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)209 int32_t UpdateServiceStub::ResumeDownloadStub(UpdateServiceStubPtr service,
210 MessageParcel& data, MessageParcel& reply, MessageOption &option)
211 {
212 RETURN_FAIL_WHEN_SERVICE_NULL(service);
213 UpgradeInfo upgradeInfo;
214 VersionDigestInfo versionDigestInfo;
215 ResumeDownloadOptions resumeDownloadOptions;
216 BusinessError businessError;
217 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
218 MessageParcelHelper::ReadVersionDigestInfo(data, versionDigestInfo);
219 MessageParcelHelper::ReadResumeDownloadOptions(data, resumeDownloadOptions);
220
221 int32_t ret = service->ResumeDownload(upgradeInfo, versionDigestInfo, resumeDownloadOptions, businessError);
222 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to ResumeDownload");
223 MessageParcelHelper::WriteBusinessError(reply, businessError);
224 return INT_CALL_SUCCESS;
225 }
226
DoUpdateStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)227 int32_t UpdateServiceStub::DoUpdateStub(UpdateServiceStubPtr service,
228 MessageParcel& data, MessageParcel& reply, MessageOption &option)
229 {
230 RETURN_FAIL_WHEN_SERVICE_NULL(service);
231 UpgradeInfo upgradeInfo;
232 VersionDigestInfo versionDigestInfo;
233 UpgradeOptions upgradeOptions;
234 BusinessError businessError;
235 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
236 MessageParcelHelper::ReadVersionDigestInfo(data, versionDigestInfo);
237 MessageParcelHelper::ReadUpgradeOptions(data, upgradeOptions);
238
239 int32_t ret = service->Upgrade(upgradeInfo, versionDigestInfo, upgradeOptions, businessError);
240 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to Upgrade");
241 MessageParcelHelper::WriteBusinessError(reply, businessError);
242 return INT_CALL_SUCCESS;
243 }
244
ClearErrorStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)245 int32_t UpdateServiceStub::ClearErrorStub(UpdateServiceStubPtr service,
246 MessageParcel& data, MessageParcel& reply, MessageOption &option)
247 {
248 RETURN_FAIL_WHEN_SERVICE_NULL(service);
249 UpgradeInfo upgradeInfo;
250 VersionDigestInfo versionDigestInfo;
251 ClearOptions clearOptions;
252 BusinessError businessError;
253 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
254 MessageParcelHelper::ReadVersionDigestInfo(data, versionDigestInfo);
255 MessageParcelHelper::ReadClearOptions(data, clearOptions);
256
257 int32_t ret = service->ClearError(upgradeInfo, versionDigestInfo, clearOptions, businessError);
258 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to ClearError");
259 MessageParcelHelper::WriteBusinessError(reply, businessError);
260 return INT_CALL_SUCCESS;
261 }
262
TerminateUpgradeStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)263 int32_t UpdateServiceStub::TerminateUpgradeStub(UpdateServiceStubPtr service, MessageParcel &data,
264 MessageParcel &reply, MessageOption &option)
265 {
266 RETURN_FAIL_WHEN_SERVICE_NULL(service);
267 UpgradeInfo upgradeInfo;
268 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
269
270 BusinessError businessError;
271 int32_t ret = service->TerminateUpgrade(upgradeInfo, businessError);
272 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to TerminateUpgrade");
273 MessageParcelHelper::WriteBusinessError(reply, businessError);
274 return INT_CALL_SUCCESS;
275 }
276
SetUpgradePolicyStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)277 int32_t UpdateServiceStub::SetUpgradePolicyStub(UpdateServiceStubPtr service,
278 MessageParcel &data, MessageParcel &reply, MessageOption &option)
279 {
280 RETURN_FAIL_WHEN_SERVICE_NULL(service);
281 UpgradeInfo upgradeInfo {};
282 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
283 UpgradePolicy policy = {};
284 MessageParcelHelper::ReadUpgradePolicy(data, policy);
285 BusinessError businessError = {};
286 int32_t ret = service->SetUpgradePolicy(upgradeInfo, policy, businessError);
287 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to SetUpgradePolicy");
288 MessageParcelHelper::WriteBusinessError(reply, businessError);
289 return INT_CALL_SUCCESS;
290 }
291
GetUpgradePolicyStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)292 int32_t UpdateServiceStub::GetUpgradePolicyStub(UpdateServiceStubPtr service,
293 MessageParcel& data, MessageParcel& reply, MessageOption &option)
294 {
295 RETURN_FAIL_WHEN_SERVICE_NULL(service);
296 UpgradeInfo upgradeInfo {};
297 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
298 UpgradePolicy policy = {};
299 BusinessError businessError = {};
300 int32_t ret = service->GetUpgradePolicy(upgradeInfo, policy, businessError);
301 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to GetUpgradePolicy");
302 MessageParcelHelper::WriteBusinessError(reply, businessError);
303 MessageParcelHelper::WriteUpgradePolicy(reply, policy);
304 return INT_CALL_SUCCESS;
305 }
306
RegisterUpdateCallbackStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)307 int32_t UpdateServiceStub::RegisterUpdateCallbackStub(UpdateServiceStubPtr service,
308 MessageParcel& data, MessageParcel& reply, MessageOption &option)
309 {
310 ENGINE_LOGI("RegisterUpdateCallbackStub");
311 RETURN_FAIL_WHEN_SERVICE_NULL(service);
312 UpgradeInfo upgradeInfo {};
313 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
314 auto remote = data.ReadRemoteObject();
315 ENGINE_CHECK(remote != nullptr, return INT_CALL_IPC_ERR, "Failed to read remote obj");
316 return service->RegisterUpdateCallback(upgradeInfo, iface_cast<IUpdateCallback>(remote));
317 }
318
UnregisterUpdateCallbackStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)319 int32_t UpdateServiceStub::UnregisterUpdateCallbackStub(UpdateServiceStubPtr service,
320 MessageParcel& data, MessageParcel& reply, MessageOption &option)
321 {
322 RETURN_FAIL_WHEN_SERVICE_NULL(service);
323 UpgradeInfo upgradeInfo {};
324 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
325 return service->UnregisterUpdateCallback(upgradeInfo);
326 }
327
CancelStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)328 int32_t UpdateServiceStub::CancelStub(UpdateServiceStubPtr service,
329 MessageParcel& data, MessageParcel& reply, MessageOption &option)
330 {
331 RETURN_FAIL_WHEN_SERVICE_NULL(service);
332 UpgradeInfo upgradeInfo {};
333 BusinessError businessError = {};
334 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
335 int32_t ret = service->Cancel(upgradeInfo, data.ReadInt32(), businessError);
336 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to cancel");
337 MessageParcelHelper::WriteBusinessError(reply, businessError);
338 return INT_CALL_SUCCESS;
339 }
340
FactoryResetStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)341 int32_t UpdateServiceStub::FactoryResetStub(UpdateServiceStubPtr service,
342 MessageParcel& data, MessageParcel& reply, MessageOption &option)
343 {
344 RETURN_FAIL_WHEN_SERVICE_NULL(service);
345 SYS_EVENT_SYSTEM_RESET(0, UpdateSystemEvent::RESET_START);
346 BusinessError businessError;
347 int32_t ret = service->FactoryReset(businessError);
348 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to FactoryReset");
349 MessageParcelHelper::WriteBusinessError(reply, businessError);
350 return INT_CALL_SUCCESS;
351 }
352
ApplyNewVersionStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)353 int32_t UpdateServiceStub::ApplyNewVersionStub(UpdateServiceStubPtr service,
354 MessageParcel& data, MessageParcel& reply, MessageOption &option)
355 {
356 RETURN_FAIL_WHEN_SERVICE_NULL(service);
357 UpgradeInfo upgradeInfo;
358 MessageParcelHelper::ReadUpgradeInfo(data, upgradeInfo);
359 string miscFile = Str16ToStr8(data.ReadString16());
360
361 vector<string> packageNames;
362 int32_t size = data.ReadInt32();
363 if (size > MAX_VECTOR_SIZE) {
364 ENGINE_LOGE("ReadComponentDescriptions size is over, size=%{public}d", size);
365 return INT_CALL_FAIL;
366 }
367 for (size_t i = 0; i < static_cast<size_t>(size); i++) {
368 packageNames.emplace_back(Str16ToStr8(data.ReadString16()));
369 }
370 BusinessError businessError;
371 int32_t ret = service->ApplyNewVersion(upgradeInfo, miscFile, packageNames, businessError);
372 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to ApplyNewVersion");
373 MessageParcelHelper::WriteBusinessError(reply, businessError);
374 return INT_CALL_SUCCESS;
375 }
376
VerifyUpgradePackageStub(UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)377 int32_t UpdateServiceStub::VerifyUpgradePackageStub(UpdateServiceStubPtr service,
378 MessageParcel& data, MessageParcel& reply, MessageOption &option)
379 {
380 RETURN_FAIL_WHEN_SERVICE_NULL(service);
381 string packagePath = Str16ToStr8(data.ReadString16());
382 string keyPath = Str16ToStr8(data.ReadString16());
383 BusinessError businessError;
384 int32_t ret = service->VerifyUpgradePackage(packagePath, keyPath, businessError);
385 ENGINE_CHECK(ret == INT_CALL_SUCCESS, return ret, "Failed to VerifyUpgradePackage");
386 MessageParcelHelper::WriteBusinessError(reply, businessError);
387 return INT_CALL_SUCCESS;
388 }
389
IsCallerValid()390 bool UpdateServiceStub::IsCallerValid()
391 {
392 OHOS::Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
393 auto callerTokenType = OHOS::Security::AccessToken::AccessTokenKit::GetTokenType(callerToken);
394 switch (callerTokenType) {
395 case OHOS::Security::AccessToken::TypeATokenTypeEnum::TOKEN_HAP: {
396 uint64_t callerFullTokenID = IPCSkeleton::GetCallingFullTokenID();
397 // hap进程只允许系统应用调用
398 return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(callerFullTokenID);
399 }
400 case OHOS::Security::AccessToken::TypeATokenTypeEnum::TOKEN_NATIVE: {
401 pid_t callerUid = IPCSkeleton::GetCallingUid();
402 // native进程只允许root权限和edm调用
403 return callerUid == ROOT_UID || callerUid == EDM_UID;
404 }
405 default:
406 // 其他情况调用予以禁止
407 return false;
408 }
409 }
410
IsPermissionGranted(uint32_t code)411 bool UpdateServiceStub::IsPermissionGranted(uint32_t code)
412 {
413 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
414 string permission = "ohos.permission.UPDATE_SYSTEM";
415 if (code == CAST_UINT(UpdaterSaInterfaceCode::FACTORY_RESET)) {
416 permission = "ohos.permission.FACTORY_RESET";
417 }
418 int verifyResult = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
419 bool isPermissionGranted = verifyResult == Security::AccessToken::PERMISSION_GRANTED;
420 if (!isPermissionGranted) {
421 ENGINE_LOGE("%{public}s not granted, code:%{public}d", permission.c_str(), code);
422 }
423 return isPermissionGranted;
424 }
425
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)426 int32_t UpdateServiceStub::OnRemoteRequest(uint32_t code,
427 MessageParcel& data, MessageParcel& reply, MessageOption &option)
428 {
429 ENGINE_LOGI("UpdateServiceStub func code %{public}u", code);
430 if (!ModuleManager::GetInstance().IsMapFuncExist(code)) {
431 ENGINE_LOGE("UpdateServiceStub OnRemoteRequest code %{public}u not found", code);
432 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
433 }
434 return ModuleManager::GetInstance().HandleFunc(code, data, reply, option);
435 }
436
HandleRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)437 int32_t UpdateServiceStub::HandleRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
438 MessageOption &option)
439 {
440 if (data.ReadInterfaceToken() != GetDescriptor()) {
441 ENGINE_LOGE("UpdateServiceStub ReadInterfaceToken fail");
442 return CALL_RESULT_TO_IPC_RESULT(INT_CALL_IPC_ERR);
443 }
444
445 if (!IsCallerValid()) {
446 ENGINE_LOGE("UpdateServiceStub IsCallerValid false");
447 return CALL_RESULT_TO_IPC_RESULT(INT_NOT_SYSTEM_APP);
448 }
449 if (!IsPermissionGranted(code)) {
450 ENGINE_LOGE("UpdateServiceStub code %{public}d IsPermissionGranted false", code);
451 UpgradeInfo tmpInfo;
452 MessageParcelHelper::ReadUpgradeInfo(data, tmpInfo);
453 return CALL_RESULT_TO_IPC_RESULT(INT_APP_NOT_GRANTED);
454 }
455 auto iter = requestFuncMap_.find(code);
456 return CALL_RESULT_TO_IPC_RESULT((this->*(iter->second))(this, data, reply, option));
457 }
458 } // namespace UpdateEngine
459 } // namespace OHOS
460