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 "scsiread10_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
23 using namespace OHOS::HDI::Usb::ScsiDdk::V1_0;
24
25 namespace OHOS {
26 namespace SCSI {
27 constexpr uint32_t DEVICE_MEM_MAP_SIZE = 1024;
ConstructParamsFromData(ScsiPeripheralIORequest & request,const uint8_t * data,size_t size)28 static void ConstructParamsFromData(ScsiPeripheralIORequest& request, const uint8_t* data, size_t size)
29 {
30 if (data == nullptr || size < sizeof(uint32_t)) {
31 return;
32 }
33
34 size_t offset = 0;
35 (void)memcpy_s(&request.lbAddress, sizeof(uint32_t), data + offset, sizeof(uint32_t));
36 offset += sizeof(uint32_t);
37
38 if (size >= offset + sizeof(uint32_t)) {
39 (void)memcpy_s(&request.transferLength, sizeof(uint32_t), data + offset, sizeof(uint32_t));
40 offset += sizeof(uint32_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(uint8_t)) {
54 request.byte6 = *(data + offset);
55 offset += sizeof(uint8_t);
56 }
57
58 if (size >= offset + sizeof(uint32_t)) {
59 (void)memcpy_s(&request.timeout, sizeof(uint32_t), data + offset, sizeof(uint32_t));
60 }
61 }
62
ScsiRead10FuzzTest(const uint8_t * data,size_t size)63 bool ScsiRead10FuzzTest(const uint8_t *data, size_t size)
64 {
65 sptr<IScsiPeripheralDdk> scsiPeripheralDdk = IScsiPeripheralDdk::Get();
66 if (scsiPeripheralDdk == nullptr) {
67 HDF_LOGE("%{public}s: get ddk failed", __func__);
68 return false;
69 }
70 int32_t ret = scsiPeripheralDdk->Init();
71 if (ret != HDF_SUCCESS) {
72 HDF_LOGE("%{public}s: init failed, ret = %{public}d", __func__, ret);
73 return false;
74 }
75
76 ScsiPeripheralDevice device;
77 ret = ScsiFuzzTestHostModeInit(scsiPeripheralDdk, device);
78 if (ret != HDF_SUCCESS) {
79 HDF_LOGE("%{public}s: scsi peripheral open device failed, ret = %{public}d", __func__, ret);
80 return false;
81 }
82
83 ScsiPeripheralIORequest request;
84 ConstructParamsFromData(request, data, size);
85 request.memMapSize = DEVICE_MEM_MAP_SIZE;
86 std::vector<uint8_t> capacityInfo;
87 ScsiPeripheralResponse response;
88 ret = scsiPeripheralDdk->Read10(device, request, response);
89
90 ret = scsiPeripheralDdk->Close(device);
91 if (ret != HDF_SUCCESS) {
92 HDF_LOGE("%{public}s: close device failed", __func__);
93 scsiPeripheralDdk->Release();
94 return false;
95 }
96 ret = scsiPeripheralDdk->Release();
97 if (ret != HDF_SUCCESS) {
98 HDF_LOGE("%{public}s: release failed", __func__);
99 return false;
100 }
101 return true;
102 }
103 } // namespace SCSI
104 } // namespace OHOS
105
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
107 {
108 OHOS::SCSI::ScsiRead10FuzzTest(data, size);
109 return 0;
110 }