1 /*
2  * Copyright (c) 2024 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 "inithaptokenstub_fuzzer.h"
17 #include <sys/types.h>
18 #include <unistd.h>
19 #include <iostream>
20 #include <string>
21 #include <thread>
22 #include <vector>
23 #include "accesstoken_fuzzdata.h"
24 #undef private
25 #include "accesstoken_manager_service.h"
26 #include "hap_info_parcel.h"
27 #include "i_accesstoken_manager.h"
28 
29 using namespace std;
30 using namespace OHOS::Security::AccessToken;
31 const int CONSTANTS_NUMBER_TWO = 2;
32 static const int32_t ROOT_UID = 0;
33 
34 namespace OHOS {
ConstructorParam(AccessTokenFuzzData & fuzzData,HapInfoParcel & hapInfoParcel,HapPolicyParcel & hapPolicyParcel)35     void ConstructorParam(AccessTokenFuzzData& fuzzData, HapInfoParcel& hapInfoParcel, HapPolicyParcel& hapPolicyParcel)
36     {
37         std::string permissionName = fuzzData.GenerateRandomString();
38         std::string bundleName = fuzzData.GenerateRandomString();
39         PermissionDef testPermDef = {
40             .permissionName = permissionName,
41             .bundleName = bundleName,
42             .grantMode = 1,
43             .availableLevel = APL_NORMAL,
44             .label = fuzzData.GenerateRandomString(),
45             .labelId = 1,
46             .description = fuzzData.GenerateRandomString(),
47             .descriptionId = 1};
48         PermissionStateFull TestState = {
49             .permissionName = permissionName,
50             .isGeneral = true,
51             .resDeviceID = {fuzzData.GenerateRandomString()},
52             .grantStatus = {PermissionState::PERMISSION_GRANTED},
53             .grantFlags = {1},
54         };
55         HapInfoParams TestInfoParms = {
56             .userID = 1,
57             .bundleName = bundleName,
58             .instIndex = 0,
59             .appIDDesc = fuzzData.GenerateRandomString()};
60         PreAuthorizationInfo info1 = {
61             .permissionName = permissionName,
62             .userCancelable = true
63         };
64         HapPolicyParams TestPolicyPrams = {
65             .apl = APL_NORMAL,
66             .domain = fuzzData.GenerateRandomString(),
67             .permList = {testPermDef},
68             .permStateList = {TestState},
69             .aclRequestedList = {permissionName},
70             .preAuthorizationInfo = {info1}
71         };
72 
73         hapInfoParcel.hapInfoParameter = TestInfoParms;
74         hapPolicyParcel.hapPolicyParameter = TestPolicyPrams;
75     }
76 
InitHapTokenStubFuzzTest(const uint8_t * data,size_t size)77     bool InitHapTokenStubFuzzTest(const uint8_t* data, size_t size)
78     {
79         if ((data == nullptr) || (size == 0)) {
80             return false;
81         }
82 
83         AccessTokenFuzzData fuzzData(data, size);
84         HapInfoParcel hapInfoParcel;
85         HapPolicyParcel hapPolicyParcel;
86         ConstructorParam(fuzzData, hapInfoParcel, hapPolicyParcel);
87 
88         MessageParcel datas;
89         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
90         if (!datas.WriteParcelable(&hapInfoParcel)) {
91             return false;
92         }
93         if (!datas.WriteParcelable(&hapPolicyParcel)) {
94             return false;
95         }
96 
97         uint32_t code = static_cast<uint32_t>(
98             AccessTokenInterfaceCode::INIT_TOKEN_HAP);
99 
100         MessageParcel reply;
101         MessageOption option;
102         bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
103         if (enable) {
104             setuid(CONSTANTS_NUMBER_TWO);
105         }
106         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
107         setuid(ROOT_UID);
108 
109         return true;
110     }
111 } // namespace OHOS
112 
113 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)114 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
115 {
116     /* Run your code on data */
117     OHOS::InitHapTokenStubFuzzTest(data, size);
118     return 0;
119 }
120