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