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