• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "allochaptoken_fuzzer.h"
17 
18 #include <iostream>
19 #include <thread>
20 #include <string>
21 #include <vector>
22 #undef private
23 #include "accesstoken_kit.h"
24 
25 using namespace std;
26 using namespace OHOS::Security::AccessToken;
27 
28 namespace OHOS {
AllocHapTokenFuzzTest(const uint8_t * data,size_t size)29     bool AllocHapTokenFuzzTest(const uint8_t* data, size_t size)
30     {
31         AccessTokenIDEx tokenIdEx = {0};
32         if ((data == nullptr) || (size == 0)) {
33             return false;
34         }
35 
36         std::string testName(reinterpret_cast<const char*>(data), size);
37         PermissionDef testPermDef;
38         testPermDef.permissionName = testName;
39         testPermDef.bundleName = testName;
40         testPermDef.grantMode = 1;
41         testPermDef.availableLevel = APL_NORMAL;
42         testPermDef.label = testName;
43         testPermDef.labelId = 1;
44         testPermDef.description = testName;
45         testPermDef.descriptionId = 1;
46 
47         PermissionStateFull testState;
48         testState.permissionName = testName;
49         testState.isGeneral = true;
50         testState.resDeviceID = {testName};
51         testState.grantStatus = {PermissionState::PERMISSION_GRANTED};
52         testState.grantFlags = {1};
53         HapInfoParams TestInfoParms = {
54             .userID = 1,
55             .bundleName = testName,
56             .instIndex = 0,
57             .appIDDesc = testName
58         };
59         HapPolicyParams TestPolicyPrams = {
60             .apl = APL_NORMAL,
61             .domain = testName,
62             .permList = {testPermDef},
63             .permStateList = {testState}
64         };
65 
66         tokenIdEx = AccessTokenKit::AllocHapToken(TestInfoParms, TestPolicyPrams);
67 
68         return tokenIdEx.tokenIdExStruct.tokenID != 0;
69     }
70 }
71 
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75     /* Run your code on data */
76     OHOS::AllocHapTokenFuzzTest(data, size);
77     return 0;
78 }
79