• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "setpermdialogcap_fuzzer.h"
17 
18 #include <string>
19 #include <thread>
20 #include <vector>
21 
22 #undef private
23 #include "access_token.h"
24 #include "accesstoken_kit.h"
25 #include "accesstoken_manager_service.h"
26 #include "fuzzer/FuzzedDataProvider.h"
27 #include "iaccess_token_manager.h"
28 #include "nativetoken_kit.h"
29 #include "securec.h"
30 #include "token_setproc.h"
31 
32 using namespace std;
33 using namespace OHOS::Security::AccessToken;
34 
35 namespace OHOS {
GetNativeToken()36     void GetNativeToken()
37     {
38         uint64_t tokenId;
39         const char** perms = new (std::nothrow) const char *[1];
40         if (perms == nullptr) {
41             return;
42         }
43 
44         perms[0] = "ohos.permission.DISABLE_PERMISSION_DIALOG"; // 3 means the third permission
45 
46         NativeTokenInfoParams infoInstance = {
47             .dcapsNum = 0,
48             .permsNum = 1,
49             .aclsNum = 0,
50             .dcaps = nullptr,
51             .perms = perms,
52             .acls = nullptr,
53             .processName = "setpermdialogcap_fuzzer",
54             .aplStr = "system_core",
55         };
56 
57         tokenId = GetAccessTokenId(&infoInstance);
58         SetSelfTokenID(tokenId);
59         AccessTokenKit::ReloadNativeTokenInfo();
60         delete[] perms;
61     }
62 
SetPermDialogCapFuzzTest(const uint8_t * data,size_t size)63     bool SetPermDialogCapFuzzTest(const uint8_t* data, size_t size)
64     {
65         if ((data == nullptr) || (size == 0)) {
66             return false;
67         }
68 
69         FuzzedDataProvider provider(data, size);
70         HapBaseInfoParcel baseInfoParcel;
71         baseInfoParcel.hapBaseInfo.userID = provider.ConsumeIntegral<int32_t>();
72         baseInfoParcel.hapBaseInfo.bundleName = provider.ConsumeRandomLengthString();
73         baseInfoParcel.hapBaseInfo.instIndex = provider.ConsumeIntegral<int32_t>();
74 
75         MessageParcel datas;
76         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
77         if (!datas.WriteParcelable(&baseInfoParcel)) {
78             return false;
79         }
80         uint32_t code = static_cast<uint32_t>(IAccessTokenManagerIpcCode::COMMAND_SET_PERM_DIALOG_CAP);
81 
82         MessageParcel reply;
83         MessageOption option;
84 
85         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
86         return true;
87     }
88 }
89 
LLVMFuzzerInitialize(int * argc,char *** argv)90 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv)
91 {
92     OHOS::GetNativeToken();
93     return 0;
94 }
95 
96 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)97 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
98 {
99     /* Run your code on data */
100     OHOS::SetPermDialogCapFuzzTest(data, size);
101     return 0;
102 }
103