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 "gethaptokeninfostub_fuzzer.h"
17
18 #include "accesstoken_manager_service.h"
19 #include "fuzzer/FuzzedDataProvider.h"
20 #include "iaccess_token_manager.h"
21 #include "permission_def_parcel.h"
22
23 using namespace std;
24 using namespace OHOS;
25 using namespace OHOS::Security::AccessToken;
26 const int CONSTANTS_NUMBER_TWO = 2;
27 static const int32_t ROOT_UID = 0;
28
29 namespace OHOS {
GetHapTokenInfoStubFuzzTest(const uint8_t * data,size_t size)30 bool GetHapTokenInfoStubFuzzTest(const uint8_t* data, size_t size)
31 {
32 if ((data == nullptr) || (size == 0)) {
33 return false;
34 }
35
36 FuzzedDataProvider provider(data, size);
37 AccessTokenID tokenId = provider.ConsumeIntegral<AccessTokenID>();
38 MessageParcel datas;
39 datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
40 if (!datas.WriteUint32(tokenId)) {
41 return false;
42 }
43
44 uint32_t code = static_cast<uint32_t>(
45 IAccessTokenManagerIpcCode::COMMAND_GET_HAP_TOKEN_INFO);
46
47 MessageParcel reply;
48 MessageOption option;
49 bool enable = ((provider.ConsumeIntegral<int32_t>() % CONSTANTS_NUMBER_TWO) == 0);
50 if (enable) {
51 setuid(CONSTANTS_NUMBER_TWO);
52 }
53 DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
54 setuid(ROOT_UID);
55
56 return true;
57 }
58 }
59
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63 /* Run your code on data */
64 OHOS::GetHapTokenInfoStubFuzzTest(data, size);
65 return 0;
66 }
67
68