• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "dinput_permission_check_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 #include <unistd.h>
22 #include <refbase.h>
23 
24 #include "constants_dinput.h"
25 #include "distributed_input_transport_base.h"
26 #include "softbus_permission_check.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
CheckSinkPermissionFuzzTest(const uint8_t * data,size_t size)30 void CheckSinkPermissionFuzzTest(const uint8_t *data, size_t size)
31 {
32     if ((data == nullptr) || (size < (sizeof(int32_t) + sizeof(uint64_t)))) {
33         return;
34     }
35     FuzzedDataProvider fdp(data, size);
36     int32_t userId = fdp.ConsumeIntegral<int32_t>();
37     uint64_t tokenId = fdp.ConsumeIntegral<uint64_t>();
38     std::string accountId = fdp.ConsumeRandomLengthString();
39     std::string networkId = fdp.ConsumeRandomLengthString();
40     DistributedInput::AccountInfo callerAccountInfo = {
41         .userId_ = userId,
42         .tokenId_ = tokenId,
43         .accountId_ = accountId,
44         .networkId_ = networkId,
45     };
46     DistributedInput::SoftBusPermissionCheck::CheckSinkPermission(callerAccountInfo);
47 }
48 
CheckSrcAccessControlFuzzTest(const uint8_t * data,size_t size)49 void CheckSrcAccessControlFuzzTest(const uint8_t *data, size_t size)
50 {
51     if ((data == nullptr) || (size == 0)) {
52         return;
53     }
54 
55     std::string sinkNetworkId(reinterpret_cast<const char*>(data), size);
56     DistributedInput::AccountInfo localAccountInfo;
57     DistributedInput::SoftBusPermissionCheck::CheckSrcAccessControl(sinkNetworkId, localAccountInfo);
58 }
59 
CheckSinkAccessControlFuzzTest(const uint8_t * data,size_t size)60 void CheckSinkAccessControlFuzzTest(const uint8_t *data, size_t size)
61 {
62     if ((data == nullptr) || (size < (sizeof(int32_t) + sizeof(uint64_t)))) {
63         return;
64     }
65     FuzzedDataProvider fdp(data, size);
66     int32_t userId = fdp.ConsumeIntegral<int32_t>();
67     uint64_t tokenId = fdp.ConsumeIntegral<uint64_t>();
68     std::string accountId = fdp.ConsumeRandomLengthString();
69     std::string networkId = fdp.ConsumeRandomLengthString();
70     DistributedInput::AccountInfo callerAccountInfo = {
71         .userId_ = userId,
72         .tokenId_ = tokenId,
73         .accountId_ = accountId,
74         .networkId_ = networkId,
75     };
76     DistributedInput::AccountInfo calleeAccountInfo = {
77         .userId_ = userId,
78         .tokenId_ = tokenId,
79         .accountId_ = accountId,
80         .networkId_ = networkId,
81     };
82     DistributedInput::SoftBusPermissionCheck::CheckSinkAccessControl(callerAccountInfo, calleeAccountInfo);
83 }
84 
GetLocalNetworkIdFuzzTest(const uint8_t * data,size_t size)85 void GetLocalNetworkIdFuzzTest(const uint8_t *data, size_t size)
86 {
87     if ((data == nullptr) || (size == 0)) {
88         return;
89     }
90 
91     std::string networkId(reinterpret_cast<const char*>(data), size);
92     DistributedInput::SoftBusPermissionCheck::GetLocalNetworkId(networkId);
93 }
94 
TransCallerInfoOneFuzzTest(const uint8_t * data,size_t size)95 void TransCallerInfoOneFuzzTest(const uint8_t *data, size_t size)
96 {
97     if ((data == nullptr) || (size < (sizeof(int32_t) + sizeof(uint64_t)))) {
98         return;
99     }
100     FuzzedDataProvider fdp(data, size);
101     int32_t userId = fdp.ConsumeIntegral<int32_t>();
102     uint64_t tokenId = fdp.ConsumeIntegral<uint64_t>();
103     std::string accountId = fdp.ConsumeRandomLengthString();
104     std::string accessInfo = "{\"accountId\":\"123\"}";
105     SocketAccessInfo callerInfo = {
106         .userId = userId,
107         .localTokenId = tokenId,
108         .businessAccountId = const_cast<char*>(accountId.c_str()),
109         .extraAccessInfo = const_cast<char*>(accessInfo.c_str()),
110     };
111     DistributedInput::AccountInfo callerAccountInfo;
112     std::string networkId = fdp.ConsumeRandomLengthString();
113     DistributedInput::SoftBusPermissionCheck::TransCallerInfo(&callerInfo, callerAccountInfo, networkId);
114 }
115 
TransCallerInfoSecondFuzzTest(const uint8_t * data,size_t size)116 void TransCallerInfoSecondFuzzTest(const uint8_t *data, size_t size)
117 {
118     if ((data == nullptr) || (size < (sizeof(int32_t) + sizeof(uint64_t)))) {
119         return;
120     }
121     FuzzedDataProvider fdp(data, size);
122     int32_t userId = fdp.ConsumeIntegral<int32_t>();
123     uint64_t tokenId = fdp.ConsumeIntegral<uint64_t>();
124     std::string accountId = fdp.ConsumeRandomLengthString();
125     std::string accessInfo = fdp.ConsumeRandomLengthString();
126     SocketAccessInfo callerInfo = {
127         .userId = userId,
128         .localTokenId = tokenId,
129         .businessAccountId = const_cast<char*>(accountId.c_str()),
130         .extraAccessInfo = const_cast<char*>(accessInfo.c_str()),
131     };
132     DistributedInput::AccountInfo callerAccountInfo;
133     std::string networkId = fdp.ConsumeRandomLengthString();
134     DistributedInput::SoftBusPermissionCheck::TransCallerInfo(&callerInfo, callerAccountInfo, networkId);
135 }
136 
FillLocalInfoOneFuzzTest(const uint8_t * data,size_t size)137 void FillLocalInfoOneFuzzTest(const uint8_t *data, size_t size)
138 {
139     if ((data == nullptr) || (size == 0)) {
140         return;
141     }
142     SocketAccessInfo *localInfo = nullptr;
143     DistributedInput::SoftBusPermissionCheck::FillLocalInfo(localInfo);
144 }
145 
FillLocalInfoSecondFuzzTest(const uint8_t * data,size_t size)146 void FillLocalInfoSecondFuzzTest(const uint8_t *data, size_t size)
147 {
148     if ((data == nullptr) || (size < (sizeof(int32_t) + sizeof(uint64_t)))) {
149         return;
150     }
151     FuzzedDataProvider fdp(data, size);
152     int32_t userId = fdp.ConsumeIntegral<int32_t>();
153     uint64_t tokenId = fdp.ConsumeIntegral<uint64_t>();
154     SocketAccessInfo localInfo = {
155         .userId = userId,
156         .localTokenId = tokenId,
157     };
158     DistributedInput::SoftBusPermissionCheck::FillLocalInfo(&localInfo);
159 }
160 } // namespace DistributedHardware
161 } // namespace OHOS
162 
163 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)164 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
165 {
166     /* Run your code on data */
167     OHOS::DistributedHardware::CheckSinkPermissionFuzzTest(data, size);
168     OHOS::DistributedHardware::CheckSrcAccessControlFuzzTest(data, size);
169     OHOS::DistributedHardware::CheckSinkAccessControlFuzzTest(data, size);
170     OHOS::DistributedHardware::GetLocalNetworkIdFuzzTest(data, size);
171     OHOS::DistributedHardware::TransCallerInfoOneFuzzTest(data, size);
172     OHOS::DistributedHardware::TransCallerInfoSecondFuzzTest(data, size);
173     OHOS::DistributedHardware::FillLocalInfoOneFuzzTest(data, size);
174     OHOS::DistributedHardware::FillLocalInfoSecondFuzzTest(data, size);
175     return 0;
176 }