1 /*
2 * Copyright (c) 2022-2024 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 "uri_permission_manager_stub.h"
17
18 #include "ability_manager_errors.h"
19 #include "hilog_tag_wrapper.h"
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace AAFwk {
24 namespace {
25 const int MAX_URI_COUNT = 500;
26 }
27
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 int UriPermissionManagerStub::OnRemoteRequest(
29 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
30 {
31 if (data.ReadInterfaceToken() != IUriPermissionManager::GetDescriptor()) {
32 TAG_LOGE(AAFwkTag::URIPERMMGR, "InterfaceToken invalid");
33 return ERR_INVALID_VALUE;
34 }
35 ErrCode errCode = ERR_OK;
36 switch (code) {
37 case UriPermMgrCmd::ON_GRANT_URI_PERMISSION : {
38 return HandleGrantUriPermission(data, reply);
39 }
40 case UriPermMgrCmd::ON_BATCH_GRANT_URI_PERMISSION : {
41 return HandleBatchGrantUriPermission(data, reply);
42 }
43 case UriPermMgrCmd::ON_GRANT_URI_PERMISSION_PRIVILEGED : {
44 return HandleGrantUriPermissionPrivileged(data, reply);
45 }
46 case UriPermMgrCmd::ON_REVOKE_ALL_URI_PERMISSION : {
47 return HandleRevokeAllUriPermission(data, reply);
48 }
49 case UriPermMgrCmd::ON_REVOKE_URI_PERMISSION_MANUALLY : {
50 return HandleRevokeUriPermissionManually(data, reply);
51 }
52 case UriPermMgrCmd::ON_VERIFY_URI_PERMISSION : {
53 return HandleVerifyUriPermission(data, reply);
54 }
55 case UriPermMgrCmd::ON_CHECK_URI_AUTHORIZATION : {
56 return HandleCheckUriAuthorization(data, reply);
57 }
58 case UriPermMgrCmd::ON_CLEAR_PERMISSION_TOKEN_BY_MAP : {
59 return HandleClearPermissionTokenByMap(data, reply);
60 }
61 #ifdef ABILITY_RUNTIME_FEATURE_SANDBOXMANAGER
62 case UriPermMgrCmd::ON_ACTIVE : {
63 return HandleActive(data, reply);
64 }
65 #endif // ABILITY_RUNTIME_FEATURE_SANDBOXMANAGER
66 default:
67 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
68 }
69 return errCode;
70 }
71
ReadBatchUris(MessageParcel & data,std::vector<Uri> & uriVec)72 int32_t UriPermissionManagerStub::ReadBatchUris(MessageParcel &data, std::vector<Uri> &uriVec)
73 {
74 uint32_t size = data.ReadUint32();
75 if (size == 0 || size > MAX_URI_COUNT) {
76 TAG_LOGE(AAFwkTag::URIPERMMGR, "out of range: %{public}u", size);
77 return ERR_URI_LIST_OUT_OF_RANGE;
78 }
79 std::vector<std::string> uris;
80 if (!data.ReadStringVector(&uris)) {
81 TAG_LOGE(AAFwkTag::URIPERMMGR, "read uris failed");
82 return ERR_DEAD_OBJECT;
83 }
84 for (auto &uri : uris) {
85 uriVec.emplace_back(uri);
86 }
87 return ERR_OK;
88 }
89
HandleRevokeAllUriPermission(MessageParcel & data,MessageParcel & reply)90 int UriPermissionManagerStub::HandleRevokeAllUriPermission(MessageParcel &data, MessageParcel &reply)
91 {
92 auto tokenId = data.ReadUint32();
93 int result = RevokeAllUriPermissions(tokenId);
94 reply.WriteInt32(result);
95 return ERR_OK;
96 }
97
HandleGrantUriPermission(MessageParcel & data,MessageParcel & reply)98 int UriPermissionManagerStub::HandleGrantUriPermission(MessageParcel &data, MessageParcel &reply)
99 {
100 std::unique_ptr<Uri> uri(data.ReadParcelable<Uri>());
101 if (!uri) {
102 TAG_LOGE(AAFwkTag::URIPERMMGR, "read uri failed");
103 return ERR_DEAD_OBJECT;
104 }
105 auto flag = data.ReadUint32();
106 auto targetBundleName = data.ReadString();
107 auto appIndex = data.ReadInt32();
108 auto initiatorTokenId = data.ReadUint32();
109 int result = GrantUriPermission(*uri, flag, targetBundleName, appIndex, initiatorTokenId);
110 reply.WriteInt32(result);
111 return ERR_OK;
112 }
113
HandleBatchGrantUriPermission(MessageParcel & data,MessageParcel & reply)114 int UriPermissionManagerStub::HandleBatchGrantUriPermission(MessageParcel &data, MessageParcel &reply)
115 {
116 std::vector<Uri> uriVec;
117 auto ret = ReadBatchUris(data, uriVec);
118 if (ret != ERR_OK) {
119 return ret;
120 }
121 auto flag = data.ReadUint32();
122 auto targetBundleName = data.ReadString();
123 auto appIndex = data.ReadInt32();
124 auto initiatorTokenId = data.ReadUint32();
125 int result = GrantUriPermission(uriVec, flag, targetBundleName, appIndex, initiatorTokenId);
126 reply.WriteInt32(result);
127 return ERR_OK;
128 }
129
HandleGrantUriPermissionPrivileged(MessageParcel & data,MessageParcel & reply)130 int32_t UriPermissionManagerStub::HandleGrantUriPermissionPrivileged(MessageParcel &data, MessageParcel &reply)
131 {
132 std::vector<Uri> uriVec;
133 auto ret = ReadBatchUris(data, uriVec);
134 if (ret != ERR_OK) {
135 return ret;
136 }
137 auto flag = data.ReadUint32();
138 auto targetBundleName = data.ReadString();
139 auto appIndex = data.ReadInt32();
140 auto initiatorTokenId = data.ReadUint32();
141 auto hideSensitiveType = data.ReadInt32();
142 int32_t result = GrantUriPermissionPrivileged(uriVec, flag, targetBundleName, appIndex,
143 initiatorTokenId, hideSensitiveType);
144 reply.WriteInt32(result);
145 return ERR_OK;
146 }
147
HandleRevokeUriPermissionManually(MessageParcel & data,MessageParcel & reply)148 int UriPermissionManagerStub::HandleRevokeUriPermissionManually(MessageParcel &data, MessageParcel &reply)
149 {
150 std::unique_ptr<Uri> uri(data.ReadParcelable<Uri>());
151 if (!uri) {
152 TAG_LOGE(AAFwkTag::URIPERMMGR, "read uri failed");
153 return ERR_DEAD_OBJECT;
154 }
155 auto bundleName = data.ReadString();
156 auto appIndex = data.ReadInt32();
157 int result = RevokeUriPermissionManually(*uri, bundleName, appIndex);
158 reply.WriteInt32(result);
159 return ERR_OK;
160 }
161
HandleVerifyUriPermission(MessageParcel & data,MessageParcel & reply)162 int UriPermissionManagerStub::HandleVerifyUriPermission(MessageParcel &data, MessageParcel &reply)
163 {
164 std::unique_ptr<Uri> uri(data.ReadParcelable<Uri>());
165 if (!uri) {
166 TAG_LOGE(AAFwkTag::URIPERMMGR, "read uri failed");
167 return ERR_DEAD_OBJECT;
168 }
169 auto flag = data.ReadUint32();
170 auto tokenId = data.ReadUint32();
171 bool result = VerifyUriPermission(*uri, flag, tokenId);
172 reply.WriteBool(result);
173 return ERR_OK;
174 }
175
HandleCheckUriAuthorization(MessageParcel & data,MessageParcel & reply)176 int32_t UriPermissionManagerStub::HandleCheckUriAuthorization(MessageParcel &data, MessageParcel &reply)
177 {
178 auto size = data.ReadUint32();
179 if (size == 0 || size > MAX_URI_COUNT) {
180 TAG_LOGE(AAFwkTag::URIPERMMGR, "uriVec empty or exceed maxSize %{public}d", MAX_URI_COUNT);
181 return ERR_URI_LIST_OUT_OF_RANGE;
182 }
183 std::vector<std::string> uriVec;
184 if (!data.ReadStringVector(&uriVec)) {
185 TAG_LOGE(AAFwkTag::URIPERMMGR, "read uris failed");
186 return ERR_DEAD_OBJECT;
187 }
188 auto flag = data.ReadUint32();
189 auto tokenId = data.ReadUint32();
190 auto result = CheckUriAuthorization(uriVec, flag, tokenId);
191 if (!reply.WriteUint32(result.size())) {
192 TAG_LOGE(AAFwkTag::URIPERMMGR, "Write uriVec size failed");
193 return ERR_DEAD_OBJECT;
194 }
195 for (auto res: result) {
196 if (!reply.WriteBool(res)) {
197 TAG_LOGE(AAFwkTag::URIPERMMGR, "Write res failed");
198 return ERR_DEAD_OBJECT;
199 }
200 }
201 return ERR_OK;
202 }
203
HandleClearPermissionTokenByMap(MessageParcel & data,MessageParcel & reply)204 int UriPermissionManagerStub::HandleClearPermissionTokenByMap(MessageParcel &data, MessageParcel &reply)
205 {
206 auto tokenId = data.ReadUint32();
207 int result = ClearPermissionTokenByMap(tokenId);
208 reply.WriteInt32(result);
209 return ERR_OK;
210 }
211
212 #ifdef ABILITY_RUNTIME_FEATURE_SANDBOXMANAGER
HandleActive(MessageParcel & data,MessageParcel & reply)213 int UriPermissionManagerStub::HandleActive(MessageParcel &data, MessageParcel &reply)
214 {
215 auto policySize = data.ReadUint32();
216 if (policySize == 0 || policySize > MAX_URI_COUNT) {
217 TAG_LOGE(AAFwkTag::URIPERMMGR, "policy empty or exceed maxSize %{public}d", MAX_URI_COUNT);
218 return ERR_URI_LIST_OUT_OF_RANGE;
219 }
220 std::vector<PolicyInfo> policy;
221 for (uint32_t i = 0; i < policySize; i++) {
222 PolicyInfo info = {data.ReadString(), data.ReadUint64()};
223 policy.emplace_back(info);
224 }
225 std::vector<uint32_t> result;
226 int res = Active(policy, result);
227 if (!reply.WriteUInt32Vector(result)) {
228 TAG_LOGE(AAFwkTag::URIPERMMGR, "Write result failed");
229 return ERR_DEAD_OBJECT;
230 }
231 if (!reply.WriteInt32(res)) {
232 TAG_LOGE(AAFwkTag::URIPERMMGR, "Write res failed");
233 return ERR_DEAD_OBJECT;
234 }
235 return ERR_OK;
236 }
237 #endif // ABILITY_RUNTIME_FEATURE_SANDBOXMANAGER
238 } // namespace AAFwk
239 } // namespace OHOS
240