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 "getremotenativetokenidstub_fuzzer.h"
17
18 #include <string>
19 #include <thread>
20 #include <vector>
21 #undef private
22
23 #include "accesstoken_info_manager.h"
24 #include "accesstoken_kit.h"
25 #include "accesstoken_manager_service.h"
26 #include "fuzzer/FuzzedDataProvider.h"
27 #include "iaccess_token_manager.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 {
GetRemoteNativeTokenIDStubFuzzTest(const uint8_t * data,size_t size)37 bool GetRemoteNativeTokenIDStubFuzzTest(const uint8_t* data, size_t size)
38 {
39 #ifdef TOKEN_SYNC_ENABLE
40 if ((data == nullptr) || (size == 0)) {
41 return false;
42 }
43
44 FuzzedDataProvider provider(data, size);
45 std::string deviceID = provider.ConsumeRandomLengthString();
46 AccessTokenID tokenId = provider.ConsumeIntegral<AccessTokenID>();
47
48 MessageParcel datas;
49 datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
50 if (!datas.WriteString(deviceID)) {
51 return false;
52 }
53 if (!datas.WriteUint32(tokenId)) {
54 return false;
55 }
56
57 uint32_t code = static_cast<uint32_t>(
58 IAccessTokenManagerIpcCode::COMMAND_GET_REMOTE_NATIVE_TOKEN_I_D);
59
60 MessageParcel reply;
61 MessageOption option;
62 bool enable = ((provider.ConsumeIntegral<int32_t>() % CONSTANTS_NUMBER_TWO) == 0);
63 if (enable) {
64 AccessTokenID accesstoken = AccessTokenKit::GetNativeTokenId("token_sync_service");
65 SetSelfTokenID(accesstoken);
66 uint32_t hapSize = 0;
67 uint32_t nativeSize = 0;
68 uint32_t pefDefSize = 0;
69 uint32_t dlpSize = 0;
70 std::map<int32_t, TokenIdInfo> tokenIdAplMap;
71 AccessTokenInfoManager::GetInstance().Init(hapSize, nativeSize, pefDefSize, dlpSize, tokenIdAplMap);
72 }
73 DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
74 AccessTokenID hdcd = AccessTokenKit::GetNativeTokenId("hdcd");
75 SetSelfTokenID(hdcd);
76
77 return true;
78 #else
79 return true;
80 #endif
81 }
82 } // namespace OHOS
83
84 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)85 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
86 {
87 /* Run your code on data */
88 OHOS::GetRemoteNativeTokenIDStubFuzzTest(data, size);
89 return 0;
90 }
91