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