• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.GenerateStochasticString());
38         PermissionDef testPermDef = {.permissionName = permissionName,
39             .bundleName = fuzzData.GenerateStochasticString(),
40             .grantMode = 1,
41             .availableLevel = APL_NORMAL,
42             .label = fuzzData.GenerateStochasticString(),
43             .labelId = 1,
44             .description = fuzzData.GenerateStochasticString(),
45             .descriptionId = 1};
46         PermissionStatus testState = {.permissionName = permissionName,
47             .grantStatus = PermissionState::PERMISSION_GRANTED,
48             .grantFlag = 1};
49         HapPolicy policy = {.apl = APL_NORMAL,
50             .domain = fuzzData.GenerateStochasticString(),
51             .permList = {testPermDef},
52             .permStateList = {testState}};
53         hapPolicyParcel.hapPolicy = policy;
54     }
UpdateHapTokenStubFuzzTest(const uint8_t * data,size_t size)55     bool UpdateHapTokenStubFuzzTest(const uint8_t* data, size_t size)
56     {
57         if ((data == nullptr) || (size == 0)) {
58             return false;
59         }
60         AccessTokenFuzzData fuzzData(data, size);
61         AccessTokenID tokenId = fuzzData.GetData<AccessTokenID>();
62         int32_t apiVersion = 8;
63         HapPolicyParcel hapPolicyParcel;
64         ConstructorParam(fuzzData, hapPolicyParcel);
65 
66         MessageParcel datas;
67         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
68         if (!datas.WriteUint32(tokenId)) {
69             return false;
70         }
71         if (!datas.WriteBool(false)) {
72             return false;
73         }
74         if (!datas.WriteString(fuzzData.GenerateStochasticString())) {
75             return false;
76         }
77         if (!datas.WriteInt32(apiVersion)) {
78             return false;
79         }
80         if (!datas.WriteParcelable(&hapPolicyParcel)) {
81             return false;
82         }
83         uint32_t code = static_cast<uint32_t>(AccessTokenInterfaceCode::UPDATE_HAP_TOKEN);
84         MessageParcel reply;
85         MessageOption option;
86         bool enable = ((size % CONSTANTS_NUMBER_TWO) == 0);
87         if (enable) {
88             setuid(CONSTANTS_NUMBER_TWO);
89         }
90         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
91         setuid(ROOT_UID);
92         return true;
93     }
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::UpdateHapTokenStubFuzzTest(data, size);
101     return 0;
102 }
103