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 "setremotehaptokeninfostub_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_info_manager.h"
25 #include "accesstoken_kit.h"
26 #include "accesstoken_manager_service.h"
27 #include "i_accesstoken_manager.h"
28 #include "permission_state_full.h"
29 #include "token_setproc.h"
30
31 using namespace std;
32 using namespace OHOS::Security::AccessToken;
33 #ifdef TOKEN_SYNC_ENABLE
34 const int CONSTANTS_NUMBER_TWO = 2;
35 #endif
36
37 namespace OHOS {
38 #ifdef TOKEN_SYNC_ENABLE
ConstructorParam(AccessTokenFuzzData & fuzzData,AccessTokenID tokenId,HapTokenInfoForSyncParcel & hapSyncParcel)39 void ConstructorParam(
40 AccessTokenFuzzData& fuzzData, AccessTokenID tokenId, HapTokenInfoForSyncParcel& hapSyncParcel)
41 {
42 std::string permissionName(fuzzData.GenerateStochasticString());
43 HapTokenInfo baseInfo = {
44 .ver = 1,
45 .userID = 1,
46 .bundleName = fuzzData.GenerateStochasticString(),
47 .instIndex = 1,
48 .tokenID = tokenId,
49 .tokenAttr = 0
50 };
51 PermissionStatus infoManagerTestState = {
52 .grantFlag = PermissionFlag::PERMISSION_SYSTEM_FIXED,
53 .grantStatus = PermissionState::PERMISSION_GRANTED,
54 .permissionName = permissionName};
55 PermissionStatus infoManagerTestState2 = {
56 .grantFlag = PermissionFlag::PERMISSION_USER_SET,
57 .grantStatus = PermissionState::PERMISSION_DENIED,
58 .permissionName = permissionName};
59 std::vector<PermissionStatus> permStateList;
60 permStateList.emplace_back(infoManagerTestState);
61 HapTokenInfoForSync remoteTokenInfo = {
62 .baseInfo = baseInfo,
63 .permStateList = permStateList
64 };
65 hapSyncParcel.hapTokenInfoForSyncParams = remoteTokenInfo;
66 }
67 #endif
68
SetRemoteHapTokenInfoStubFuzzTest(const uint8_t * data,size_t size)69 bool SetRemoteHapTokenInfoStubFuzzTest(const uint8_t* data, size_t size)
70 {
71 #ifdef TOKEN_SYNC_ENABLE
72 if ((data == nullptr) || (size == 0)) {
73 return false;
74 }
75
76 AccessTokenFuzzData fuzzData(data, size);
77 AccessTokenID tokenId = fuzzData.GetData<AccessTokenID>();
78 HapTokenInfoForSyncParcel hapSyncParcel;
79 ConstructorParam(fuzzData, tokenId, hapSyncParcel);
80
81 MessageParcel datas;
82 datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
83 if (!datas.WriteString(fuzzData.GenerateStochasticString())) {
84 return false;
85 }
86 if (!datas.WriteParcelable(&hapSyncParcel)) {
87 return false;
88 }
89
90 uint32_t code = static_cast<uint32_t>(
91 AccessTokenInterfaceCode::SET_REMOTE_HAP_TOKEN_INFO);
92
93 MessageParcel reply;
94 MessageOption option;
95 bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
96 if (enable) {
97 AccessTokenID accesstoken = AccessTokenKit::GetNativeTokenId("token_sync_service");
98 SetSelfTokenID(accesstoken);
99 AccessTokenInfoManager::GetInstance().Init();
100 }
101 DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
102 AccessTokenID hdcd = AccessTokenKit::GetNativeTokenId("hdcd");
103 SetSelfTokenID(hdcd);
104
105 return true;
106 #else
107 return true;
108 #endif
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::SetRemoteHapTokenInfoStubFuzzTest(data, size);
117 return 0;
118 }
119