1 /*
2 * Copyright (c) 2024 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 #include "accesstoken_fuzzdata.h"
23 #undef private
24 #include "accesstoken_kit.h"
25 #include "i_privacy_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 const char *[1];
40 perms[0] = "ohos.permission.SET_FOREGROUND_HAP_REMINDER"; // 3 means the third permission
41
42 NativeTokenInfoParams infoInstance = {
43 .dcapsNum = 0,
44 .permsNum = 1,
45 .aclsNum = 0,
46 .dcaps = nullptr,
47 .perms = perms,
48 .acls = nullptr,
49 .processName = "sethapwithfgreminderstub_fuzzer_test",
50 .aplStr = "system_core",
51 };
52
53 tokenId = GetAccessTokenId(&infoInstance);
54 SetSelfTokenID(tokenId);
55 AccessTokenKit::ReloadNativeTokenInfo();
56 delete[] perms;
57 }
58
SetHapWithFGReminderStubFuzzTest(const uint8_t * data,size_t size)59 bool SetHapWithFGReminderStubFuzzTest(const uint8_t* data, size_t size)
60 {
61 if ((data == nullptr) || (size == 0)) {
62 return false;
63 }
64 GetNativeToken();
65 AccessTokenFuzzData fuzzData(data, size);
66
67 if (size > sizeof(uint32_t) + sizeof(bool)) {
68 uint32_t tokenId = fuzzData.GetData<uint32_t>();
69 bool isAllowed = fuzzData.GenerateStochasticBool();
70
71 MessageParcel datas;
72 datas.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
73 if (!datas.WriteUint32(tokenId)) {
74 return false;
75 }
76 if (!datas.WriteBool(isAllowed)) {
77 return false;
78 }
79
80 uint32_t code = static_cast<uint32_t>(
81 PrivacyInterfaceCode::SET_HAP_WITH_FOREGROUND_REMINDER);
82
83 MessageParcel reply;
84 MessageOption option;
85 bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
86 if (enable) {
87 setuid(CONSTANTS_NUMBER_TWO);
88 }
89 DelayedSingleton<PrivacyManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
90 setuid(ROOT_UID);
91 }
92 return true;
93 }
94 } // namespace OHOS
95
96 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99 /* Run your code on data */
100 OHOS::SetHapWithFGReminderStubFuzzTest(data, size);
101 return 0;
102 }
103