1 /*
2  * Copyright (c) 2023 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 "setmutepolicystub_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.GET_SENSITIVE_PERMISSIONS"; // 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 = "getpermissionsstatusstub_fuzzer_test",
53             .aplStr = "system_core",
54         };
55 
56         tokenId = GetAccessTokenId(&infoInstance);
57         SetSelfTokenID(tokenId);
58         AccessTokenKit::ReloadNativeTokenInfo();
59         delete[] perms;
60     }
61 
SetMutePolicyStubFuzzTest(const uint8_t * data,size_t size)62     bool SetMutePolicyStubFuzzTest(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 policyType = fuzzData.GetData<uint32_t>();
72             uint32_t callerType = fuzzData.GetData<uint32_t>();
73             bool isMute = fuzzData.GenerateRandomBool();
74 
75             MessageParcel datas;
76             datas.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
77             if (!datas.WriteUint32(policyType)) {
78                 return false;
79             }
80             if (!datas.WriteUint32(callerType)) {
81                 return false;
82             }
83             if (!datas.WriteBool(isMute)) {
84                 return false;
85             }
86 
87             uint32_t code = static_cast<uint32_t>(
88                 PrivacyInterfaceCode::SET_MUTE_POLICY);
89 
90             MessageParcel reply;
91             MessageOption option;
92             bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
93             if (enable) {
94                 setuid(CONSTANTS_NUMBER_TWO);
95             }
96             DelayedSingleton<PrivacyManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
97             setuid(ROOT_UID);
98         }
99         return true;
100     }
101 } // namespace OHOS
102 
103 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
105 {
106     /* Run your code on data */
107     OHOS::SetMutePolicyStubFuzzTest(data, size);
108     return 0;
109 }
110