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