• 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 "setremotehaptokeninfostub_fuzzer.h"
17 
18 #include <string>
19 #include <thread>
20 #include <vector>
21 #undef private
22 #include "access_token.h"
23 #include "accesstoken_manager_service.h"
24 #include "i_accesstoken_manager.h"
25 #include "permission_state_full.h"
26 
27 using namespace std;
28 using namespace OHOS::Security::AccessToken;
29 
30 namespace OHOS {
SetRemoteHapTokenInfoStubFuzzTest(const uint8_t * data,size_t size)31     bool SetRemoteHapTokenInfoStubFuzzTest(const uint8_t* data, size_t size)
32     {
33     #ifdef TOKEN_SYNC_ENABLE
34         if ((data == nullptr) || (size == 0)) {
35             return false;
36         }
37 
38         std::string testName(reinterpret_cast<const char*>(data), size);
39         AccessTokenID tokenId = static_cast<AccessTokenID>(size);
40         HapTokenInfo baseInfo = {
41             .apl = APL_NORMAL,
42             .ver = 1,
43             .userID = 1,
44             .bundleName = testName,
45             .instIndex = 1,
46             .appID = testName,
47             .deviceID = testName,
48             .tokenID = tokenId,
49             .tokenAttr = 0
50         };
51         PermissionStateFull infoManagerTestState = {
52             .grantFlags = {PermissionFlag::PERMISSION_SYSTEM_FIXED},
53             .grantStatus = {PermissionState::PERMISSION_GRANTED},
54             .isGeneral = true,
55             .permissionName = testName,
56             .resDeviceID = {testName}};
57         std::vector<PermissionStateFull> permStateList;
58         permStateList.emplace_back(infoManagerTestState);
59         HapTokenInfoForSync remoteTokenInfo = {
60             .baseInfo = baseInfo,
61             .permStateList = permStateList
62         };
63 
64         HapTokenInfoForSyncParcel hapSyncParcel;
65         hapSyncParcel.hapTokenInfoForSyncParams = remoteTokenInfo;
66 
67         MessageParcel datas;
68         datas.WriteInterfaceToken(IAccessTokenManager::GetDescriptor());
69         if (!datas.WriteString(testName)) {
70             return false;
71         }
72         if (!datas.WriteParcelable(&hapSyncParcel)) {
73             return false;
74         }
75 
76         uint32_t code = static_cast<uint32_t>(
77             AccessTokenInterfaceCode::SET_REMOTE_HAP_TOKEN_INFO);
78 
79         MessageParcel reply;
80         MessageOption option;
81         DelayedSingleton<AccessTokenManagerService>::GetInstance()->OnRemoteRequest(code, datas, reply, option);
82 
83         return true;
84     #else
85         return true;
86     #endif
87     }
88 } // namespace OHOS
89 
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93     /* Run your code on data */
94     OHOS::SetRemoteHapTokenInfoStubFuzzTest(data, size);
95     return 0;
96 }
97