• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "sys_installer_stub.h"
17 
18 #include <string_ex.h>
19 
20 #include "accesstoken_kit.h"
21 #include "hap_token_info.h"
22 #include "ipc_skeleton.h"
23 #include "log/log.h"
24 #include "securec.h"
25 #include "sys_installer_sa_ipc_interface_code.h"
26 #include "utils.h"
27 
28 namespace OHOS {
29 namespace SysInstaller {
30 using namespace std;
31 using namespace Updater;
32 using namespace std::placeholders;
33 
SysInstallerStub()34 SysInstallerStub::SysInstallerStub()
35 {
36     requestFuncMap_.emplace(SysInstallerInterfaceCode::SYS_INSTALLER_INIT,
37         bind(&SysInstallerStub::SysInstallerInitStub, this, _1, _2, _3, _4));
38     requestFuncMap_.emplace(SysInstallerInterfaceCode::UPDATE_PACKAGE,
39         bind(&SysInstallerStub::StartUpdatePackageZipStub, this, _1, _2, _3, _4));
40     requestFuncMap_.emplace(SysInstallerInterfaceCode::START_STREAM_UPDATE,
41         bind(&SysInstallerStub::StartStreamUpdateStub, this, _1, _2, _3, _4));
42     requestFuncMap_.emplace(SysInstallerInterfaceCode::STOP_STREAM_UPDATE,
43         bind(&SysInstallerStub::StopStreamUpdateStub, this, _1, _2, _3, _4));
44     requestFuncMap_.emplace(SysInstallerInterfaceCode::PROCESS_STREAM_DATA,
45         bind(&SysInstallerStub::ProcessStreamDataStub, this, _1, _2, _3, _4));
46     requestFuncMap_.emplace(SysInstallerInterfaceCode::SET_UPDATE_CALLBACK,
47         bind(&SysInstallerStub::SetUpdateCallbackStub, this, _1, _2, _3, _4));
48     requestFuncMap_.emplace(SysInstallerInterfaceCode::GET_UPDATE_STATUS,
49         bind(&SysInstallerStub::GetUpdateStatusStub, this, _1, _2, _3, _4));
50     requestFuncMap_.emplace(SysInstallerInterfaceCode::UPDATE_PARA_PACKAGE,
51         bind(&SysInstallerStub::StartUpdateParaZipStub, this, _1, _2, _3, _4));
52     requestFuncMap_.emplace(SysInstallerInterfaceCode::DELETE_PARA_PACKAGE,
53         bind(&SysInstallerStub::StartDeleteParaZipStub, this, _1, _2, _3, _4));
54     requestFuncMap_.emplace(SysInstallerInterfaceCode::DECOMPRESS_ACC_PACKAGE,
55         bind(&SysInstallerStub::AccDecompressAndVerifyPkgStub, this, _1, _2, _3, _4));
56     requestFuncMap_.emplace(SysInstallerInterfaceCode::DELETE_ACC_PACKAGE,
57         bind(&SysInstallerStub::AccDeleteDirStub, this, _1, _2, _3, _4));
58 }
59 
~SysInstallerStub()60 SysInstallerStub::~SysInstallerStub()
61 {
62     requestFuncMap_.clear();
63 }
64 
SysInstallerInitStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)65 int32_t SysInstallerStub::SysInstallerInitStub(SysInstallerStub *service,
66     MessageParcel &data, MessageParcel &reply, MessageOption &option)
67 {
68     if (service == nullptr) {
69         LOG(ERROR) << "Invalid param";
70         return -1;
71     }
72     bool bStreamUpgrade = data.ReadBool();
73     int ret = service->SysInstallerInit(bStreamUpgrade);
74     reply.WriteInt32(ret);
75     return 0;
76 }
77 
StartUpdatePackageZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)78 int32_t SysInstallerStub::StartUpdatePackageZipStub(SysInstallerStub *service,
79     MessageParcel &data, MessageParcel &reply, MessageOption &option)
80 {
81     if (service == nullptr) {
82         LOG(ERROR) << "Invalid param";
83         return -1;
84     }
85     string pkgPath = Str16ToStr8(data.ReadString16());
86     int ret = service->StartUpdatePackageZip(pkgPath);
87     reply.WriteInt32(ret);
88     return 0;
89 }
90 
StartStreamUpdateStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)91 int32_t SysInstallerStub::StartStreamUpdateStub(SysInstallerStub *service,
92     MessageParcel &data, MessageParcel &reply, MessageOption &option)
93 {
94     if (service == nullptr) {
95         LOG(ERROR) << "Invalid param";
96         return -1;
97     }
98     int ret = service->StartStreamUpdate();
99     reply.WriteInt32(ret);
100     return 0;
101 }
102 
StopStreamUpdateStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)103 int32_t SysInstallerStub::StopStreamUpdateStub(SysInstallerStub *service,
104     MessageParcel &data, MessageParcel &reply, MessageOption &option)
105 {
106     if (service == nullptr) {
107         LOG(ERROR) << "Invalid param";
108         return -1;
109     }
110     int ret = service->StopStreamUpdate();
111     reply.WriteInt32(ret);
112     return 0;
113 }
114 
ProcessStreamDataStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)115 int32_t SysInstallerStub::ProcessStreamDataStub(SysInstallerStub *service,
116     MessageParcel &data, MessageParcel &reply, MessageOption &option)
117 {
118     if (service == nullptr) {
119         LOG(ERROR) << "Invalid param";
120         return -1;
121     }
122     uint32_t size = data.ReadUint32();
123     const uint8_t *buffer = data.ReadBuffer(size);
124     int ret = service->ProcessStreamData(buffer, size);
125     reply.WriteInt32(ret);
126     return 0;
127 }
128 
SetUpdateCallbackStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)129 int32_t SysInstallerStub::SetUpdateCallbackStub(SysInstallerStub *service,
130     MessageParcel &data, MessageParcel &reply, MessageOption &option)
131 {
132     if (service == nullptr) {
133         LOG(ERROR) << "Invalid param";
134         return -1;
135     }
136     auto remote = data.ReadRemoteObject();
137     if (remote == nullptr) {
138         LOG(ERROR) << "Failed to read remote obj";
139         return -1;
140     }
141     int ret = service->SetUpdateCallback(iface_cast<ISysInstallerCallback>(remote));
142     reply.WriteInt32(ret);
143     return 0;
144 }
145 
GetUpdateStatusStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)146 int32_t SysInstallerStub::GetUpdateStatusStub(SysInstallerStub *service,
147     MessageParcel &data, MessageParcel &reply, MessageOption &option)
148 {
149     if (service == nullptr) {
150         LOG(ERROR) << "Invalid param";
151         return -1;
152     }
153     int32_t ret = service->GetUpdateStatus();
154     reply.WriteInt32(ret);
155     return 0;
156 }
157 
StartUpdateParaZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)158 int32_t SysInstallerStub::StartUpdateParaZipStub(SysInstallerStub *service,
159     MessageParcel &data, MessageParcel &reply, MessageOption &option)
160 {
161     if (service == nullptr) {
162         LOG(ERROR) << "Invalid param";
163         return -1;
164     }
165     string pkgPath = Str16ToStr8(data.ReadString16());
166     string location = Str16ToStr8(data.ReadString16());
167     string cfgDir = Str16ToStr8(data.ReadString16());
168     LOG(INFO) << "StartUpdateParaZipStub path:" << pkgPath << " location:" << location << " cfgDir:" << cfgDir;
169 
170     int32_t ret = service->StartUpdateParaZip(pkgPath, location, cfgDir);
171     reply.WriteInt32(ret);
172     return 0;
173 }
174 
StartDeleteParaZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)175 int32_t SysInstallerStub::StartDeleteParaZipStub(SysInstallerStub *service,
176     MessageParcel &data, MessageParcel &reply, MessageOption &option)
177 {
178     if (service == nullptr) {
179         LOG(ERROR) << "Invalid param";
180         return -1;
181     }
182     string location = Str16ToStr8(data.ReadString16());
183     string cfgDir = Str16ToStr8(data.ReadString16());
184     LOG(INFO) << "StartDeleteParaZipStub location:" << location << " cfgDir:" << cfgDir;
185 
186     int32_t ret = service->StartDeleteParaZip(location, cfgDir);
187     reply.WriteInt32(ret);
188     return 0;
189 }
190 
AccDecompressAndVerifyPkgStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)191 int32_t SysInstallerStub::AccDecompressAndVerifyPkgStub(SysInstallerStub *service,
192     MessageParcel &data, MessageParcel &reply, MessageOption &option)
193 {
194     if (service == nullptr) {
195         LOG(ERROR) << "Invalid param";
196         return -1;
197     }
198     string srcPath = Str16ToStr8(data.ReadString16());
199     string dstPath = Str16ToStr8(data.ReadString16());
200     uint32_t type = static_cast<uint32_t>(data.ReadInt32());
201     LOG(INFO) << "StartUpdateParaZipStub srcPath:" << srcPath << " dstPath:" << dstPath;
202 
203     int32_t ret = service->AccDecompressAndVerifyPkg(srcPath, dstPath, type);
204     reply.WriteInt32(ret);
205     return 0;
206 }
207 
AccDeleteDirStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)208 int32_t SysInstallerStub::AccDeleteDirStub(SysInstallerStub *service,
209     MessageParcel &data, MessageParcel &reply, MessageOption &option)
210 {
211     if (service == nullptr) {
212         LOG(ERROR) << "Invalid param";
213         return -1;
214     }
215     string dstPath = Str16ToStr8(data.ReadString16());
216     LOG(INFO) << "AccDeleteDirStub dstPath:" << dstPath;
217 
218     int32_t ret = service->AccDeleteDir(dstPath);
219     reply.WriteInt32(ret);
220     return 0;
221 }
222 
IsPermissionGranted(void)223 bool SysInstallerStub::IsPermissionGranted(void)
224 {
225     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
226     std::string permission = "ohos.permission.UPDATE_SYSTEM";
227 
228     int verifyResult = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
229     bool isPermissionGranted = (verifyResult == Security::AccessToken::PERMISSION_GRANTED);
230     if (!isPermissionGranted) {
231         LOG(ERROR) << "not granted " << permission.c_str();
232     }
233     return isPermissionGranted;
234 }
235 
CheckCallingPerm(void)236 bool SysInstallerStub::CheckCallingPerm(void)
237 {
238     int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid();
239     LOG(INFO) << "CheckCallingPerm callingUid:" << callingUid;
240     if (callingUid == 0) {
241         return true;
242     }
243     return callingUid == Updater::Utils::USER_UPDATE_AUTHORITY && IsPermissionGranted();
244 }
245 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)246 int32_t SysInstallerStub::OnRemoteRequest(uint32_t code,
247     MessageParcel &data, MessageParcel &reply, MessageOption &option)
248 {
249     if (data.ReadInterfaceToken() != GetDescriptor()) {
250         LOG(ERROR) << "SysInstallerStub ReadInterfaceToken fail";
251         return -1;
252     }
253 
254     LOG(INFO) << "OnRemoteRequest func code " << code;
255     auto inter = requestFuncMap_.find(code);
256     if (inter != requestFuncMap_.end()) {
257         if (!CheckCallingPerm()) {
258             LOG(ERROR) << "SysInstallerStub CheckCallingPerm fail";
259             return -1;
260         }
261         return inter->second(this, data, reply, option);
262     }
263 
264     LOG(INFO) << "UpdateServiceStub OnRemoteRequest code " << code << "not found";
265     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
266 }
267 } // namespace SysInstaller
268 } // namespace OHOS