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.GenerateRandomString());
43 HapTokenInfo baseInfo = {
44 .apl = APL_NORMAL,
45 .ver = 1,
46 .userID = 1,
47 .bundleName = fuzzData.GenerateRandomString(),
48 .instIndex = 1,
49 .appID = fuzzData.GenerateRandomString(),
50 .deviceID = fuzzData.GenerateRandomString(),
51 .tokenID = tokenId,
52 .tokenAttr = 0
53 };
54 PermissionStateFull infoManagerTestState = {
55 .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED},
56 .grantStatus = {PermissionState::PERMISSION_GRANTED},
57 .isGeneral = true,
58 .permissionName = permissionName,
59 .resDeviceID = {fuzzData.GenerateRandomString()}};
60 PermissionStateFull infoManagerTestState2 = {
61 .grantFlags = {PermissionFlag::PERMISSION_USER_SET},
62 .grantStatus = {PermissionState::PERMISSION_DENIED},
63 .isGeneral = true,
64 .permissionName = permissionName,
65 .resDeviceID = {fuzzData.GenerateRandomString()}};
66 std::vector<PermissionStateFull> permStateList;
67 permStateList.emplace_back(infoManagerTestState);
68 HapTokenInfoForSync remoteTokenInfo = {
69 .baseInfo = baseInfo,
70 .permStateList = permStateList
71 };
72 hapSyncParcel.hapTokenInfoForSyncParams = remoteTokenInfo;
73 }
74 #endif
75
SetRemoteHapTokenInfoStubFuzzTest(const uint8_t * data,size_t size)76 bool SetRemoteHapTokenInfoStubFuzzTest(const uint8_t* data, size_t size)
77 {
78 #ifdef TOKEN_SYNC_ENABLE
79 if ((data == nullptr) || (size == 0)) {
80 return false;
81 }
82
83 AccessTokenFuzzData fuzzData(data, size);
84 AccessTokenID tokenId = fuzzData.GetData<AccessTokenID>();
85 HapTokenInfoForSyncParcel hapSyncParcel;
86 ConstructorParam(fuzzData, tokenId, hapSyncParcel);
87
88 MessageParcel datas;
89 datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
90 if (!datas.WriteString(fuzzData.GenerateRandomString())) {
91 return false;
92 }
93 if (!datas.WriteParcelable(&hapSyncParcel)) {
94 return false;
95 }
96
97 uint32_t code = static_cast<uint32_t>(
98 AccessTokenInterfaceCode::SET_REMOTE_HAP_TOKEN_INFO);
99
100 MessageParcel reply;
101 MessageOption option;
102 bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
103 if (enable) {
104 AccessTokenID accesstoken = AccessTokenKit::GetNativeTokenId("token_sync_service");
105 SetSelfTokenID(accesstoken);
106 AccessTokenInfoManager::GetInstance().Init();
107 }
108 DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
109 AccessTokenID hdcd = AccessTokenKit::GetNativeTokenId("hdcd");
110 SetSelfTokenID(hdcd);
111
112 return true;
113 #else
114 return true;
115 #endif
116 }
117 } // namespace OHOS
118
119 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)120 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
121 {
122 /* Run your code on data */
123 OHOS::SetRemoteHapTokenInfoStubFuzzTest(data, size);
124 return 0;
125 }
126