1 /*
2 * Copyright (c) 2023-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 "setmutepolicystub_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 {
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 (std::nothrow) const char *[1];
43 if (perms == nullptr) {
44 return;
45 }
46
47 perms[0] = "ohos.permission.GET_SENSITIVE_PERMISSIONS"; // 3 means the third permission
48
49 NativeTokenInfoParams infoInstance = {
50 .dcapsNum = 0,
51 .permsNum = 1,
52 .aclsNum = 0,
53 .dcaps = nullptr,
54 .perms = perms,
55 .acls = nullptr,
56 .processName = "getpermissionsstatusstub_fuzzer_test",
57 .aplStr = "system_core",
58 };
59
60 tokenId = GetAccessTokenId(&infoInstance);
61 SetSelfTokenID(tokenId);
62 AccessTokenKit::ReloadNativeTokenInfo();
63 delete[] perms;
64 }
65
SetMutePolicyStubFuzzTest(const uint8_t * data,size_t size)66 bool SetMutePolicyStubFuzzTest(const uint8_t* data, size_t size)
67 {
68 if ((data == nullptr) || (size == 0)) {
69 return false;
70 }
71
72 FuzzedDataProvider provider(data, size);
73 GetNativeToken();
74
75 if (size > sizeof(uint32_t) + sizeof(bool) + sizeof(uint32_t)) {
76 uint32_t policyType = provider.ConsumeIntegral<uint32_t>();
77 uint32_t callerType = provider.ConsumeIntegral<uint32_t>();
78 bool isMute = provider.ConsumeBool();
79 uint32_t tokenID = provider.ConsumeIntegral<uint32_t>();
80
81 MessageParcel datas;
82 datas.WriteInterfaceToken(IPrivacyManager::GetDescriptor());
83 if (!datas.WriteUint32(policyType)) {
84 return false;
85 }
86 if (!datas.WriteUint32(callerType)) {
87 return false;
88 }
89 if (!datas.WriteInt32(isMute ? 1 : 0)) {
90 return false;
91 }
92 if (!datas.WriteUint32(tokenID)) {
93 return false;
94 }
95
96 uint32_t code = static_cast<uint32_t>(
97 IPrivacyManagerIpcCode::COMMAND_SET_MUTE_POLICY);
98
99 MessageParcel reply;
100 MessageOption option;
101 bool enable = ((provider.ConsumeIntegral<int32_t>() % CONSTANTS_NUMBER_TWO) == 0);
102 if (enable) {
103 setuid(CONSTANTS_NUMBER_TWO);
104 }
105 DelayedSingleton<PrivacyManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
106 setuid(ROOT_UID);
107 }
108 return true;
109 }
110 } // namespace OHOS
111
112 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)113 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
114 {
115 /* Run your code on data */
116 OHOS::SetMutePolicyStubFuzzTest(data, size);
117 return 0;
118 }
119