• 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 "inituserpolicystub_fuzzer.h"
17 
18 #include <string>
19 #include <thread>
20 #include <vector>
21 #undef private
22 #include "access_token.h"
23 #include "accesstoken_fuzzdata.h"
24 #include "accesstoken_kit.h"
25 #include "accesstoken_manager_service.h"
26 #include "i_accesstoken_manager.h"
27 #include "nativetoken_kit.h"
28 #include "token_setproc.h"
29 
30 using namespace std;
31 using namespace OHOS::Security::AccessToken;
32 static AccessTokenID g_selfTokenId = 0;
33 static uint64_t g_mockTokenId = 0;
34 const int32_t CONSTANTS_NUMBER_TWO = 2;
35 
36 namespace OHOS {
GetNativeToken()37     void GetNativeToken()
38     {
39         if (g_mockTokenId != 0) {
40             SetSelfTokenID(g_mockTokenId);
41             return;
42         }
43         const char **perms = new const char *[1];
44         perms[0] = "ohos.permission.GET_SENSITIVE_PERMISSIONS";
45 
46         NativeTokenInfoParams infoInstance = {
47             .dcapsNum = 0,
48             .permsNum = 1,
49             .aclsNum = 0,
50             .dcaps = nullptr,
51             .perms = perms,
52             .acls = nullptr,
53             .processName = "inituserpolicystub_fuzzer_test",
54             .aplStr = "system_core",
55         };
56 
57         g_mockTokenId = GetAccessTokenId(&infoInstance);
58         g_selfTokenId = GetSelfTokenID();
59         SetSelfTokenID(g_mockTokenId);
60         AccessTokenKit::ReloadNativeTokenInfo();
61         delete[] perms;
62     }
63 
InitUserPolicyStubFuzzTest(const uint8_t * data,size_t size)64     bool InitUserPolicyStubFuzzTest(const uint8_t* data, size_t size)
65     {
66         if ((data == nullptr) || (size == 0)) {
67             return false;
68         }
69 
70         AccessTokenFuzzData fuzzData(data, size);
71         std::string testName(fuzzData.GenerateStochasticString());
72 
73         UserState userList;
74         userList.userId = fuzzData.GetData<int32_t>();
75         userList.isActive = fuzzData.GenerateStochasticBool();
76 
77         MessageParcel datas;
78         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
79         if (!datas.WriteUint32(1)) {
80             return false;
81         }
82         if (!datas.WriteUint32(1)) {
83             return false;
84         }
85         if (!datas.WriteInt32(userList.userId)) {
86             return false;
87         }
88         if (!datas.WriteBool(userList.isActive)) {
89             return false;
90         }
91         if (!datas.WriteString(testName)) {
92             return false;
93         }
94 
95         uint32_t code = static_cast<uint32_t>(
96             AccessTokenInterfaceCode::INIT_USER_POLICY);
97 
98         MessageParcel reply;
99         MessageOption option;
100         bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
101         if (enable) {
102             GetNativeToken();
103         } else {
104             SetSelfTokenID(g_selfTokenId);
105         }
106         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
107 
108         return true;
109     }
110 } // namespace OHOS
111 
112 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)113 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
114 {
115     /* Run your code on data */
116     OHOS::InitUserPolicyStubFuzzTest(data, size);
117     return 0;
118 }
119