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 "updatehaptokenstub_fuzzer.h"
17 
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <iostream>
21 #include <memory>
22 #include <string>
23 #include <thread>
24 #include <vector>
25 #undef private
26 #include "accesstoken_fuzzdata.h"
27 #include "service/accesstoken_manager_service.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,HapPolicyParcel & hapPolicyParcel)35     void ConstructorParam(AccessTokenFuzzData& fuzzData, HapPolicyParcel& hapPolicyParcel)
36     {
37         std::string permissionName(fuzzData.GenerateRandomString());
38         PermissionDef testPermDef = {.permissionName = permissionName,
39             .bundleName = fuzzData.GenerateRandomString(),
40             .grantMode = 1,
41             .availableLevel = APL_NORMAL,
42             .label = fuzzData.GenerateRandomString(),
43             .labelId = 1,
44             .description = fuzzData.GenerateRandomString(),
45             .descriptionId = 1};
46         PermissionStateFull testState = {.permissionName = permissionName,
47             .isGeneral = true,
48             .resDeviceID = {fuzzData.GenerateRandomString()},
49             .grantStatus = {PermissionState::PERMISSION_GRANTED},
50             .grantFlags = {1}};
51         HapPolicyParams policy = {.apl = APL_NORMAL,
52             .domain = fuzzData.GenerateRandomString(),
53             .permList = {testPermDef},
54             .permStateList = {testState}};
55         hapPolicyParcel.hapPolicyParameter = policy;
56     }
UpdateHapTokenStubFuzzTest(const uint8_t * data,size_t size)57     bool UpdateHapTokenStubFuzzTest(const uint8_t* data, size_t size)
58     {
59         if ((data == nullptr) || (size == 0)) {
60             return false;
61         }
62         AccessTokenFuzzData fuzzData(data, size);
63         AccessTokenID tokenId = fuzzData.GetData<AccessTokenID>();
64         int32_t apiVersion = 8;
65         HapPolicyParcel hapPolicyParcel;
66         ConstructorParam(fuzzData, hapPolicyParcel);
67 
68         MessageParcel datas;
69         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
70         if (!datas.WriteUint32(tokenId)) {
71             return false;
72         }
73         if (!datas.WriteBool(false)) {
74             return false;
75         }
76         if (!datas.WriteString(fuzzData.GenerateRandomString())) {
77             return false;
78         }
79         if (!datas.WriteInt32(apiVersion)) {
80             return false;
81         }
82         if (!datas.WriteParcelable(&hapPolicyParcel)) {
83             return false;
84         }
85         uint32_t code = static_cast<uint32_t>(AccessTokenInterfaceCode::UPDATE_HAP_TOKEN);
86         MessageParcel reply;
87         MessageOption option;
88         bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
89         if (enable) {
90             setuid(CONSTANTS_NUMBER_TWO);
91         }
92         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
93         setuid(ROOT_UID);
94         return true;
95     }
96 }
97 
98 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)99 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
100 {
101     /* Run your code on data */
102     OHOS::UpdateHapTokenStubFuzzTest(data, size);
103     return 0;
104 }
105