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