• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "setpermissionstatuswithpolicystub_fuzzer.h"
17 
18 #include <vector>
19 
20 #include "access_token.h"
21 #include "accesstoken_manager_service.h"
22 #include "fuzzer/FuzzedDataProvider.h"
23 #include "iaccess_token_manager.h"
24 
25 using namespace std;
26 using namespace OHOS::Security::AccessToken;
27 
28 const int CONSTANTS_NUMBER_ONE = 1;
29 const int CONSTANTS_NUMBER_TEN = 10;
30 const uint32_t MAX_PERMISSION_LIST_SIZE = 1024;
31 static const int32_t ROOT_UID = 0;
32 
33 namespace OHOS {
SetPermissionStatusWithPolicyStubFuzzTest(const uint8_t * data,size_t size)34 bool SetPermissionStatusWithPolicyStubFuzzTest(const uint8_t* data, size_t size)
35 {
36     if ((data == nullptr) || (size == 0)) {
37         return false;
38     }
39 
40     FuzzedDataProvider provider(data, size);
41     AccessTokenID tokenId = provider.ConsumeIntegral<AccessTokenID>();
42     uint32_t permListSize =
43         provider.ConsumeIntegralInRange<uint32_t>(CONSTANTS_NUMBER_ONE, MAX_PERMISSION_LIST_SIZE);
44     std::vector<std::string> permissionList;
45     for (size_t i = 0; i < permListSize; i++) {
46         std::string permission = provider.ConsumeRandomLengthString();
47         if (!permission.empty()) {
48             permissionList.push_back(permission);
49         }
50     }
51     uint32_t flag = static_cast<uint32_t>(TypePermissionFlag::PERMISSION_FIXED_BY_ADMIN_POLICY);
52     int32_t status = TypePermissionState::PERMISSION_DENIED;
53     MessageParcel datas;
54     if (!datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor())) {
55         return false;
56     }
57     if (!datas.WriteUint32(tokenId) || !datas.WriteStringVector(permissionList) ||
58         !datas.WriteUint32(status) || !datas.WriteInt32(flag)) {
59         return false;
60     }
61 
62     uint32_t code = static_cast<uint32_t>(IAccessTokenManagerIpcCode::COMMAND_SET_PERMISSION_STATUS_WITH_POLICY);
63 
64     MessageParcel reply;
65     MessageOption option;
66     if (((provider.ConsumeIntegral<int32_t>() % CONSTANTS_NUMBER_TEN) == 0)) {
67         setuid(CONSTANTS_NUMBER_TEN);
68     }
69     DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
70     setuid(ROOT_UID);
71     return true;
72 }
73 } // namespace OHOS
74 
75 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)76 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
77 {
78     /* Run your code on data */
79     OHOS::SetPermissionStatusWithPolicyStubFuzzTest(data, size);
80     return 0;
81 }
82