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