• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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 "datatransmitmgr_fuzzer.h"
17 
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include "securec.h"
22 #include "dev_slinfo_mgr.h"
23 #include "softbus_bus_center.h"
24 #include "nativetoken_kit.h"
25 #include "token_setproc.h"
26 #include "accesstoken_kit.h"
27 
28 namespace OHOS {
tmpCallback(DEVSLQueryParams * queryParams,int32_t result,uint32_t levelInfo)29     static void tmpCallback(DEVSLQueryParams *queryParams, int32_t result, uint32_t levelInfo)
30     {
31         return;
32     }
33 
GetLocalUdid(DEVSLQueryParams * queryParams)34     static void GetLocalUdid(DEVSLQueryParams *queryParams)
35     {
36         const char *pkgName = "ohos.dslm";
37         NodeBasicInfo info;
38         (void)GetLocalNodeDeviceInfo(pkgName, &info);
39         if (GetNodeKeyInfo(pkgName, info.networkId, NODE_KEY_UDID, (uint8_t *)(queryParams->udid), UDID_BUF_LEN) != 0) {
40             return;
41         }
42         queryParams->udidLen = MAX_UDID_LENGTH;
43     }
44 
FuzzDoDataTransmitMgr(const uint8_t * data,size_t size)45     void FuzzDoDataTransmitMgr(const uint8_t* data, size_t size)
46     {
47         if (data == nullptr) {
48             return;
49         }
50         if (size < MAX_UDID_LENGTH) {
51             return;
52         }
53         uint32_t levelInfo = 0;
54         DEVSLQueryParams queryParams;
55         (void)memset_s(&queryParams, sizeof(DEVSLQueryParams), 0, sizeof(DEVSLQueryParams));
56         queryParams.udidLen = MAX_UDID_LENGTH;
57         (void)memcpy_s(queryParams.udid, queryParams.udidLen, data, MAX_UDID_LENGTH);
58 
59         int32_t ret = DATASL_OnStart();
60         if (ret != DEVSL_SUCCESS) {
61             return;
62         }
63         (void)DATASL_GetHighestSecLevelAsync(&queryParams, tmpCallback);
64         (void)DATASL_GetHighestSecLevelAsync(nullptr, tmpCallback);
65         (void)DATASL_GetHighestSecLevelAsync(&queryParams, nullptr);
66         (void)DATASL_GetHighestSecLevel(&queryParams, &levelInfo);
67         (void)DATASL_GetHighestSecLevel(nullptr, &levelInfo);
68         (void)DATASL_GetHighestSecLevel(&queryParams, nullptr);
69         GetLocalUdid(&queryParams);
70         (void)DATASL_GetHighestSecLevelAsync(&queryParams, tmpCallback);
71         (void)DATASL_GetHighestSecLevel(&queryParams, &levelInfo);
72         DATASL_OnStop();
73     }
74 }
75 
NativeTokenGetFUZZ()76 static void NativeTokenGetFUZZ()
77 {
78     uint64_t tokenId;
79     const char **perms = new const char *[1];
80     perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
81     NativeTokenInfoParams infoInstance = {
82         .acls = nullptr,
83         .dcaps = nullptr,
84         .aclsNum = 0,
85         .dcapsNum = 0,
86         .permsNum = 1,
87         .perms = perms,
88         .aplStr = "system_basic",
89     };
90 
91     infoInstance.processName = "DataTransmitMgrFuzzTest";
92     tokenId = GetAccessTokenId(&infoInstance);
93     SetSelfTokenID(tokenId);
94     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
95     delete[] perms;
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     NativeTokenGetFUZZ();
103     OHOS::FuzzDoDataTransmitMgr(data, size);
104     return 0;
105 }