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 "getnativetokenidstub_fuzzer.h"
17
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <string>
21 #include <thread>
22 #include <vector>
23 #undef private
24
25 #include "accesstoken_manager_service.h"
26 #include "fuzzer/FuzzedDataProvider.h"
27 #include "iaccess_token_manager.h"
28 #include "permission_def_parcel.h"
29
30 using namespace std;
31 using namespace OHOS;
32 using namespace OHOS::Security::AccessToken;
33 const int CONSTANTS_NUMBER_TWO = 2;
34 static const int32_t ROOT_UID = 0;
35
36 namespace OHOS {
GetNativeTokenIdStubFuzzTest(const uint8_t * data,size_t size)37 bool GetNativeTokenIdStubFuzzTest(const uint8_t* data, size_t size)
38 {
39 if ((data == nullptr) || (size == 0)) {
40 return false;
41 }
42
43 FuzzedDataProvider provider(data, size);
44 std::string processName = provider.ConsumeRandomLengthString();
45
46 MessageParcel datas;
47 datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
48 if (!datas.WriteString(processName)) {
49 return false;
50 }
51
52 uint32_t code = static_cast<uint32_t>(
53 IAccessTokenManagerIpcCode::COMMAND_GET_NATIVE_TOKEN_ID);
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 }
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::GetNativeTokenIdStubFuzzTest(data, size);
73 return 0;
74 }
75
76