• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "setremotehaptokeninfostub_fuzzer.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include "access_token.h"
22 #include "accesstoken_info_manager.h"
23 #include "accesstoken_kit.h"
24 #include "accesstoken_manager_service.h"
25 #include "fuzzer/FuzzedDataProvider.h"
26 #include "iaccess_token_manager.h"
27 #include "permission_state_full.h"
28 #include "token_setproc.h"
29 
30 using namespace std;
31 using namespace OHOS::Security::AccessToken;
32 #ifdef TOKEN_SYNC_ENABLE
33 const int CONSTANTS_NUMBER_TWO = 2;
34 #endif
35 
36 namespace OHOS {
37     #ifdef TOKEN_SYNC_ENABLE
InitRemoteTokenInfo(FuzzedDataProvider & provider,HapTokenInfoForSync & remoteTokenInfo)38     void InitRemoteTokenInfo(FuzzedDataProvider& provider, HapTokenInfoForSync& remoteTokenInfo)
39     {
40         HapTokenInfo baseInfo = {
41             .ver = '1',
42             .userID = provider.ConsumeIntegral<AccessTokenID>(),
43             .bundleName = provider.ConsumeRandomLengthString(),
44             .apiVersion = provider.ConsumeIntegral<int32_t>(),
45             .instIndex = provider.ConsumeIntegral<int32_t>(),
46             .dlpType = static_cast<int32_t>(
47                 provider.ConsumeIntegralInRange<uint32_t>(0, static_cast<uint32_t>(HapDlpType::BUTT_DLP_TYPE))),
48             .tokenID = provider.ConsumeIntegral<AccessTokenID>(),
49             .tokenAttr = provider.ConsumeIntegral<uint32_t>(),
50         };
51 
52         PermissionStatus state = {
53             .permissionName = provider.ConsumeRandomLengthString(),
54             .grantStatus = static_cast<int32_t>(provider.ConsumeIntegralInRange<uint32_t>(
55                 0, static_cast<uint32_t>(PermissionState::PERMISSION_GRANTED))),
56             .grantFlag = provider.ConsumeIntegralInRange<uint32_t>(
57                 0, static_cast<uint32_t>(PermissionFlag::PERMISSION_ALLOW_THIS_TIME))
58         };
59         std::vector<PermissionStatus> permStateList = { state };
60 
61         remoteTokenInfo.baseInfo = baseInfo;
62         remoteTokenInfo.permStateList = permStateList;
63     }
64     #endif
65 
SetRemoteHapTokenInfoStubFuzzTest(const uint8_t * data,size_t size)66     bool SetRemoteHapTokenInfoStubFuzzTest(const uint8_t* data, size_t size)
67     {
68         if ((data == nullptr) || (size == 0)) {
69             return false;
70         }
71 
72 #ifdef TOKEN_SYNC_ENABLE
73         FuzzedDataProvider provider(data, size);
74         std::string deviceID = provider.ConsumeRandomLengthString();
75 
76         HapTokenInfoForSyncParcel hapSyncParcel;
77         InitRemoteTokenInfo(provider, hapSyncParcel.hapTokenInfoForSyncParams);
78 
79         MessageParcel datas;
80         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
81         if (!datas.WriteString(deviceID)) {
82             return false;
83         }
84         if (!datas.WriteParcelable(&hapSyncParcel)) {
85             return false;
86         }
87 
88         uint32_t code = static_cast<uint32_t>(
89             IAccessTokenManagerIpcCode::COMMAND_SET_REMOTE_HAP_TOKEN_INFO);
90 
91         MessageParcel reply;
92         MessageOption option;
93         bool enable = ((provider.ConsumeIntegral<int32_t>() % CONSTANTS_NUMBER_TWO) == 0);
94         if (enable) {
95             AccessTokenID accesstoken = AccessTokenKit::GetNativeTokenId("token_sync_service");
96             SetSelfTokenID(accesstoken);
97             uint32_t hapSize = 0;
98             uint32_t nativeSize = 0;
99             uint32_t pefDefSize = 0;
100             uint32_t dlpSize = 0;
101             std::map<int32_t, TokenIdInfo> tokenIdAplMap;
102             AccessTokenInfoManager::GetInstance().Init(hapSize, nativeSize, pefDefSize, dlpSize, tokenIdAplMap);
103         }
104         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
105         AccessTokenID hdcd = AccessTokenKit::GetNativeTokenId("hdcd");
106         SetSelfTokenID(hdcd);
107 
108         return true;
109     #else
110         return true;
111     #endif
112     }
113 } // namespace OHOS
114 
115 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)116 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
117 {
118     /* Run your code on data */
119     OHOS::SetRemoteHapTokenInfoStubFuzzTest(data, size);
120     return 0;
121 }
122