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 "gethaptokeninfofromremotestub_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 {
GetHapTokenInfoFromRemoteStubFuzzTest(const uint8_t * data,size_t size)37 bool GetHapTokenInfoFromRemoteStubFuzzTest(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 AccessTokenID tokenId = provider.ConsumeIntegral<AccessTokenID>();
46
47 MessageParcel datas;
48 datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
49 if (!datas.WriteUint32(tokenId)) {
50 return false;
51 }
52
53 uint32_t code = static_cast<uint32_t>(
54 IAccessTokenManagerIpcCode::COMMAND_GET_HAP_TOKEN_INFO_FROM_REMOTE);
55
56 MessageParcel reply;
57 MessageOption option;
58 bool enable = ((provider.ConsumeIntegral<int32_t>() % CONSTANTS_NUMBER_TWO) == 0);
59 if (enable) {
60 AccessTokenID accesstoken = AccessTokenKit::GetNativeTokenId("token_sync_service");
61 SetSelfTokenID(accesstoken);
62 uint32_t hapSize = 0;
63 uint32_t nativeSize = 0;
64 uint32_t pefDefSize = 0;
65 uint32_t dlpSize = 0;
66 std::map<int32_t, TokenIdInfo> tokenIdAplMap;
67 AccessTokenInfoManager::GetInstance().Init(hapSize, nativeSize, pefDefSize, dlpSize, tokenIdAplMap);
68 }
69 DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
70 AccessTokenID hdcd = AccessTokenKit::GetNativeTokenId("hdcd");
71 SetSelfTokenID(hdcd);
72
73 return true;
74 #else
75 return true;
76 #endif
77 }
78 } // namespace OHOS
79
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83 /* Run your code on data */
84 OHOS::GetHapTokenInfoFromRemoteStubFuzzTest(data, size);
85 return 0;
86 }
87