• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "service_router_mgr_stub.h"
17 
18 #include <vector>
19 
20 #include "accesstoken_kit.h"
21 #include "appexecfwk_errors.h"
22 #include "app_log_wrapper.h"
23 #include "bundle_constants.h"
24 #include "ipc_skeleton.h"
25 #include "service_info.h"
26 #include "tokenid_kit.h"
27 
28 namespace OHOS {
29 namespace AbilityRuntime {
ServiceRouterMgrStub()30 ServiceRouterMgrStub::ServiceRouterMgrStub()
31 {
32     APP_LOGD("ServiceRouterMgrStub instance is created");
33 }
34 
~ServiceRouterMgrStub()35 ServiceRouterMgrStub::~ServiceRouterMgrStub()
36 {
37     APP_LOGD("ServiceRouterMgrStub instance is destroyed");
38 }
39 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int ServiceRouterMgrStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
41     MessageOption &option)
42 {
43     std::u16string descriptor = ServiceRouterMgrStub::GetDescriptor();
44     std::u16string remoteDescriptor = data.ReadInterfaceToken();
45     if (descriptor != remoteDescriptor) {
46         APP_LOGE("local descriptor is not equal to remote");
47         return ERR_INVALID_STATE;
48     }
49 
50     switch (code) {
51         case static_cast<uint32_t>(IServiceRouterManager::Message::QUERY_BUSINESS_ABILITY_INFOS):
52             return HandleQueryBusinessAbilityInfos(data, reply);
53         case static_cast<uint32_t>(IServiceRouterManager::Message::QUERY_PURPOSE_INFOS):
54             return HandleQueryPurposeInfos(data, reply);
55         default:
56             APP_LOGW("ServiceRouterMgrStub receives unknown code, code = %{public}d", code);
57             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58     }
59 }
60 
HandleQueryBusinessAbilityInfos(MessageParcel & data,MessageParcel & reply)61 int ServiceRouterMgrStub::HandleQueryBusinessAbilityInfos(MessageParcel &data, MessageParcel &reply)
62 {
63     APP_LOGD("ServiceRouterMgrStub handle query service infos");
64     if (!VerifySystemApp()) {
65         APP_LOGE("verify system app failed");
66         return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED;
67     }
68     if (!VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
69         APP_LOGE("verify GET_BUNDLE_INFO_PRIVILEGED failed");
70         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
71     }
72     BusinessAbilityFilter *filter = data.ReadParcelable<BusinessAbilityFilter>();
73     if (filter == nullptr) {
74         APP_LOGE("ReadParcelable<filter> failed");
75         return ERR_APPEXECFWK_PARCEL_ERROR;
76     }
77     std::vector<BusinessAbilityInfo> infos;
78     int ret = QueryBusinessAbilityInfos(*filter, infos);
79     if (filter != nullptr) {
80         delete filter;
81     }
82     if (!reply.WriteInt32(ret)) {
83         APP_LOGE("write ret failed");
84         return ERR_APPEXECFWK_PARCEL_ERROR;
85     }
86     if (ret == ERR_OK) {
87         if (!WriteParcelableVector<BusinessAbilityInfo>(infos, reply)) {
88             APP_LOGE("QueryBusinessAbilityInfos write failed");
89             return ERR_APPEXECFWK_PARCEL_ERROR;
90         }
91     }
92     return ERR_OK;
93 }
94 
HandleQueryPurposeInfos(MessageParcel & data,MessageParcel & reply)95 int ServiceRouterMgrStub::HandleQueryPurposeInfos(MessageParcel &data, MessageParcel &reply)
96 {
97     APP_LOGD("ServiceRouterMgrStub handle query purpose infos");
98     if (!VerifyCallingPermission(Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED)) {
99         APP_LOGE("verify GET_BUNDLE_INFO_PRIVILEGED failed");
100         return ERR_BUNDLE_MANAGER_PERMISSION_DENIED;
101     }
102     Want *want = data.ReadParcelable<Want>();
103     if (want == nullptr) {
104         APP_LOGE("ReadParcelable<want> failed");
105         return ERR_APPEXECFWK_PARCEL_ERROR;
106     }
107     std::string purposeName = data.ReadString();
108     std::vector<PurposeInfo> infos;
109     int ret = QueryPurposeInfos(*want, purposeName, infos);
110     if (want != nullptr) {
111         delete want;
112     }
113     if (!reply.WriteInt32(ret)) {
114         APP_LOGE("write ret failed");
115         return ERR_APPEXECFWK_PARCEL_ERROR;
116     }
117     if (ret == ERR_OK) {
118         if (!WriteParcelableVector<PurposeInfo>(infos, reply)) {
119             APP_LOGE("QueryPurposeInfos write failed");
120             return ERR_APPEXECFWK_PARCEL_ERROR;
121         }
122     }
123     return ERR_OK;
124 }
125 
HandleStartUIExtensionAbility(MessageParcel & data,MessageParcel & reply)126 int ServiceRouterMgrStub::HandleStartUIExtensionAbility(MessageParcel &data, MessageParcel &reply)
127 {
128     APP_LOGD("ServiceRouterMgrStub handle start ui extension ability");
129     sptr<SessionInfo> sessionInfo = nullptr;
130     if (data.ReadBool()) {
131         sessionInfo = data.ReadParcelable<SessionInfo>();
132     }
133     int32_t userId = data.ReadInt32();
134     int32_t result = StartUIExtensionAbility(sessionInfo, userId);
135     if (!reply.WriteInt32(result)) {
136         APP_LOGE("write result failed");
137         return ERR_APPEXECFWK_PARCEL_ERROR;
138     }
139     return ERR_OK;
140 }
141 
HandleConnectUIExtensionAbility(MessageParcel & data,MessageParcel & reply)142 int ServiceRouterMgrStub::HandleConnectUIExtensionAbility(MessageParcel &data, MessageParcel &reply)
143 {
144     APP_LOGD("ServiceRouterMgrStub handle connect ui extension ability");
145     std::unique_ptr<Want> want(data.ReadParcelable<Want>());
146     if (want == nullptr) {
147         APP_LOGE("ReadParcelable<want> failed");
148         return ERR_APPEXECFWK_PARCEL_ERROR;
149     }
150     sptr<IAbilityConnection> callback = nullptr;
151     if (data.ReadBool()) {
152         callback = iface_cast<IAbilityConnection>(data.ReadRemoteObject());
153     }
154     sptr<SessionInfo> sessionInfo = nullptr;
155     if (data.ReadBool()) {
156         sessionInfo = data.ReadParcelable<SessionInfo>();
157     }
158     int32_t userId = data.ReadInt32();
159     int32_t result = ConnectUIExtensionAbility(*want, callback, sessionInfo, userId);
160     if (!reply.WriteInt32(result)) {
161         APP_LOGE("write result failed");
162         return ERR_APPEXECFWK_PARCEL_ERROR;
163     }
164     return ERR_OK;
165 }
166 
VerifyCallingPermission(const std::string & permissionName)167 bool ServiceRouterMgrStub::VerifyCallingPermission(const std::string &permissionName)
168 {
169     APP_LOGD("VerifyCallingPermission permission %{public}s", permissionName.c_str());
170     OHOS::Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
171     OHOS::Security::AccessToken::ATokenTypeEnum tokenType =
172         OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
173     if (tokenType == OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
174         return true;
175     }
176     int32_t ret = OHOS::Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
177     if (ret == OHOS::Security::AccessToken::PermissionState::PERMISSION_DENIED) {
178         APP_LOGE("PERMISSION_DENIED: %{public}s", permissionName.c_str());
179         return false;
180     }
181     return true;
182 }
183 
VerifySystemApp()184 bool ServiceRouterMgrStub::VerifySystemApp()
185 {
186     APP_LOGD("verifying systemApp");
187     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
188     Security::AccessToken::ATokenTypeEnum tokenType =
189         Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
190     if (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE
191         || IPCSkeleton::GetCallingUid() == Constants::ROOT_UID) {
192         return true;
193     }
194     uint64_t accessTokenIdEx = IPCSkeleton::GetCallingFullTokenID();
195     if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenIdEx)) {
196         APP_LOGE("non-system app calling system api");
197         return false;
198     }
199     return true;
200 }
201 
202 template <typename T>
WriteParcelableVector(std::vector<T> & parcelableVector,Parcel & reply)203 bool ServiceRouterMgrStub::WriteParcelableVector(std::vector<T> &parcelableVector, Parcel &reply)
204 {
205     if (!reply.WriteInt32(parcelableVector.size())) {
206         APP_LOGE("write ParcelableVector size failed");
207         return false;
208     }
209 
210     for (auto &parcelable : parcelableVector) {
211         if (!reply.WriteParcelable(&parcelable)) {
212             APP_LOGE("write ParcelableVector failed");
213             return false;
214         }
215     }
216     return true;
217 }
218 }  // namespace AbilityRuntime
219 }  // namespace OHOS
220