• 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 "scsiinquiry_fuzzer.h"
17 #include <cstring>
18 #include <securec.h>
19 #include <scsi/sg.h>
20 #include "hdf_log.h"
21 #include "scsicommonfunction_fuzzer.h"
22 #define HDF_LOG_TAG scsi_ddk_fuzzer
23 
24 using namespace OHOS::HDI::Usb::ScsiDdk::V1_0;
25 
26 namespace OHOS {
27 namespace SCSI {
28 constexpr uint32_t DEVICE_MEM_MAP_SIZE = 1024;
ConstructParamsFromData(ScsiPeripheralInquiryRequest & request,const uint8_t * data,size_t size)29 static void ConstructParamsFromData(ScsiPeripheralInquiryRequest& request, const uint8_t* data, size_t size)
30 {
31     if (data == nullptr || size < sizeof(uint8_t)) {
32         return;
33     }
34 
35     request.pageCode = *(data);
36     size_t offset = sizeof(uint8_t);
37 
38     if (size >= offset + sizeof(uint16_t)) {
39         (void)memcpy_s(&request.allocationLength, sizeof(uint16_t), data + offset, sizeof(uint16_t));
40         offset += sizeof(uint16_t);
41     }
42 
43     if (size >= offset + sizeof(uint8_t)) {
44         request.control = *(data + offset);
45         offset += sizeof(uint8_t);
46     }
47 
48     if (size >= offset + sizeof(uint8_t)) {
49         request.byte1 = *(data + offset);
50         offset += sizeof(uint8_t);
51     }
52 
53     if (size >= offset + sizeof(uint32_t)) {
54         (void)memcpy_s(&request.timeout, sizeof(uint32_t), data + offset, sizeof(uint32_t));
55     }
56 }
57 
ScsiInquiryFuzzTest(const uint8_t * data,size_t size)58 bool ScsiInquiryFuzzTest(const uint8_t *data, size_t size)
59 {
60     sptr<IScsiPeripheralDdk> scsiPeripheralDdk = IScsiPeripheralDdk::Get();
61     if (scsiPeripheralDdk == nullptr) {
62         HDF_LOGE("%{public}s: get ddk failed", __func__);
63         return false;
64     }
65     int32_t ret = scsiPeripheralDdk->Init();
66     if (ret != HDF_SUCCESS) {
67         HDF_LOGE("%{public}s: init failed, ret = %{public}d", __func__, ret);
68         return false;
69     }
70     ScsiPeripheralDevice device;
71     ret = ScsiFuzzTestHostModeInit(scsiPeripheralDdk, device);
72     if (ret != HDF_SUCCESS) {
73         HDF_LOGE("%{public}s: scsi peripheral open device failed, ret = %{public}d", __func__, ret);
74         return false;
75     }
76 
77     ScsiPeripheralInquiryRequest request = {};
78     ConstructParamsFromData(request, data, size);
79     request.memMapSize = DEVICE_MEM_MAP_SIZE;
80     ScsiPeripheralInquiryInfo inquiryInfo;
81     ScsiPeripheralResponse response;
82     ret = scsiPeripheralDdk->Inquiry(device, request, inquiryInfo, response);
83 
84     ret = scsiPeripheralDdk->Close(device);
85     if (ret != HDF_SUCCESS) {
86         HDF_LOGE("%{public}s: close device failed", __func__);
87         scsiPeripheralDdk->Release();
88         return false;
89     }
90     ret = scsiPeripheralDdk->Release();
91     if (ret != HDF_SUCCESS) {
92         HDF_LOGE("%{public}s: release failed", __func__);
93         return false;
94     }
95     return true;
96 }
97 } // namespace SCSI
98 } // namespace OHOS
99 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)100 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
101 {
102     OHOS::SCSI::ScsiInquiryFuzzTest(data, size);
103     return 0;
104 }