• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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) + sizeof(uint32_t)) {
71             uint32_t policyType = fuzzData.GetData<uint32_t>();
72             uint32_t callerType = fuzzData.GetData<uint32_t>();
73             bool isMute = fuzzData.GenerateRandomBool();
74             uint32_t tokenID = fuzzData.GetData<uint32_t>();
75 
76             MessageParcel datas;
77             datas.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
78             if (!datas.WriteUint32(policyType)) {
79                 return false;
80             }
81             if (!datas.WriteUint32(callerType)) {
82                 return false;
83             }
84             if (!datas.WriteBool(isMute)) {
85                 return false;
86             }
87             if (!datas.WriteUint32(tokenID)) {
88                 return false;
89             }
90 
91             uint32_t code = static_cast<uint32_t>(
92                 PrivacyInterfaceCode::SET_MUTE_POLICY);
93 
94             MessageParcel reply;
95             MessageOption option;
96             bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
97             if (enable) {
98                 setuid(CONSTANTS_NUMBER_TWO);
99             }
100             DelayedSingleton<PrivacyManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
101             setuid(ROOT_UID);
102         }
103         return true;
104     }
105 } // namespace OHOS
106 
107 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
109 {
110     /* Run your code on data */
111     OHOS::SetMutePolicyStubFuzzTest(data, size);
112     return 0;
113 }
114