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 "sethapwithfgreminderstub_fuzzer.h"
17
18 #include <string>
19 #include <thread>
20 #include <vector>
21
22 #undef private
23 #include "accesstoken_kit.h"
24 #include "fuzzer/FuzzedDataProvider.h"
25 #include "iprivacy_manager.h"
26 #include "privacy_manager_service.h"
27 #include "nativetoken_kit.h"
28 #include "token_setproc.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 {
GetNativeToken()36 void GetNativeToken()
37 {
38 uint64_t tokenId;
39 const char** perms = new (std::nothrow) const char *[1];
40 if (perms == nullptr) {
41 return;
42 }
43
44 perms[0] = "ohos.permission.SET_FOREGROUND_HAP_REMINDER"; // 3 means the third permission
45
46 NativeTokenInfoParams infoInstance = {
47 .dcapsNum = 0,
48 .permsNum = 1,
49 .aclsNum = 0,
50 .dcaps = nullptr,
51 .perms = perms,
52 .acls = nullptr,
53 .processName = "sethapwithfgreminderstub_fuzzer_test",
54 .aplStr = "system_core",
55 };
56
57 tokenId = GetAccessTokenId(&infoInstance);
58 SetSelfTokenID(tokenId);
59 AccessTokenKit::ReloadNativeTokenInfo();
60 delete[] perms;
61 }
62
SetHapWithFGReminderStubFuzzTest(const uint8_t * data,size_t size)63 bool SetHapWithFGReminderStubFuzzTest(const uint8_t* data, size_t size)
64 {
65 if ((data == nullptr) || (size == 0)) {
66 return false;
67 }
68
69 FuzzedDataProvider provider(data, size);
70 GetNativeToken();
71
72 if (size > sizeof(uint32_t) + sizeof(bool)) {
73 AccessTokenID tokenId = provider.ConsumeIntegral<AccessTokenID>();
74 bool isAllowed = provider.ConsumeBool();
75
76 MessageParcel datas;
77 datas.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
78 if (!datas.WriteUint32(tokenId)) {
79 return false;
80 }
81 if (!datas.WriteInt32(isAllowed ? 1 : 0)) {
82 return false;
83 }
84
85 uint32_t code = static_cast<uint32_t>(
86 IPrivacyManagerIpcCode::COMMAND_SET_HAP_WITH_F_G_REMINDER);
87
88 MessageParcel reply;
89 MessageOption option;
90 bool enable = ((provider.ConsumeIntegral<int32_t>() % CONSTANTS_NUMBER_TWO) == 0);
91 if (enable) {
92 setuid(CONSTANTS_NUMBER_TWO);
93 }
94 DelayedSingleton<PrivacyManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
95 setuid(ROOT_UID);
96 }
97 return true;
98 }
99 } // namespace OHOS
100
101 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)102 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
103 {
104 /* Run your code on data */
105 OHOS::SetHapWithFGReminderStubFuzzTest(data, size);
106 return 0;
107 }
108