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 "getpermissionsstatusstub_fuzzer.h"
17
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <string>
21 #include <thread>
22 #include <vector>
23 #undef private
24 #include "access_token.h"
25 #include "accesstoken_kit.h"
26 #include "accesstoken_manager_service.h"
27 #include "fuzzer/FuzzedDataProvider.h"
28 #include "iaccess_token_manager.h"
29 #include "nativetoken_kit.h"
30 #include "securec.h"
31 #include "token_setproc.h"
32
33 using namespace std;
34 using namespace OHOS;
35 using namespace OHOS::Security::AccessToken;
36 const int CONSTANTS_NUMBER_TWO = 2;
37 static const int32_t ROOT_UID = 0;
38
39 namespace OHOS {
40 const uint8_t *g_baseFuzzData = nullptr;
41 size_t g_baseFuzzSize = 0;
42 size_t g_baseFuzzPos = 0;
GetPermissionsStatusStubFuzzTest(const uint8_t * data,size_t size)43 bool GetPermissionsStatusStubFuzzTest(const uint8_t* data, size_t size)
44 {
45 if ((data == nullptr) || (size == 0)) {
46 return false;
47 }
48
49 FuzzedDataProvider provider(data, size);
50 AccessTokenID tokenId = provider.ConsumeIntegral<AccessTokenID>();
51 std::string permissionName = provider.ConsumeRandomLengthString();
52 PermissionListState perm = {
53 .permissionName = permissionName,
54 .state = static_cast<PermissionOper>(provider.ConsumeIntegralInRange<uint32_t>(
55 0, static_cast<uint32_t>(PermissionOper::BUTT_OPER))),
56 };
57 PermissionListStateParcel permParcel;
58 permParcel.permsState = perm;
59 MessageParcel datas;
60 datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
61 if (!datas.WriteUint32(tokenId)) {
62 return false;
63 }
64 if (!datas.WriteParcelable(&permParcel)) {
65 return false;
66 }
67
68 uint32_t code = static_cast<uint32_t>(
69 IAccessTokenManagerIpcCode::COMMAND_GET_PERMISSIONS_STATUS);
70 MessageParcel reply;
71 MessageOption option;
72 bool enable = ((provider.ConsumeIntegral<int32_t>() % CONSTANTS_NUMBER_TWO) == 0);
73 if (enable) {
74 setuid(CONSTANTS_NUMBER_TWO);
75 }
76 DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
77 setuid(ROOT_UID);
78 return true;
79 }
80 }
81
LLVMFuzzerInitialize(int * argc,char *** argv)82 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
83 {
84 uint64_t tokenId;
85 const char** perms = new (std::nothrow) const char *[1];
86 if (perms == nullptr) {
87 return -1;
88 }
89
90 perms[0] = "ohos.permission.GET_SENSITIVE_PERMISSIONS"; // 3 means the third permission
91
92 NativeTokenInfoParams infoInstance = {
93 .dcapsNum = 0,
94 .permsNum = 1,
95 .aclsNum = 0,
96 .dcaps = nullptr,
97 .perms = perms,
98 .acls = nullptr,
99 .processName = "getpermissionsstatusstub_fuzzer_test",
100 .aplStr = "system_core",
101 };
102
103 tokenId = GetAccessTokenId(&infoInstance);
104 SetSelfTokenID(tokenId);
105 AccessTokenKit::ReloadNativeTokenInfo();
106 delete[] perms;
107
108 return 0;
109 }
110
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114 /* Run your code on data */
115 OHOS::GetPermissionsStatusStubFuzzTest(data, size);
116 return 0;
117 }
118
119