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 "scsiwrite10_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
ScsiWrite10FuzzTest(const uint8_t * data,size_t size)63 bool ScsiWrite10FuzzTest(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 ScsiPeripheralResponse response;
87 ret = scsiPeripheralDdk->Write10(device, request, response);
88
89 ret = scsiPeripheralDdk->Close(device);
90 if (ret != HDF_SUCCESS) {
91 HDF_LOGE("%{public}s: close device failed", __func__);
92 scsiPeripheralDdk->Release();
93 return false;
94 }
95 ret = scsiPeripheralDdk->Release();
96 if (ret != HDF_SUCCESS) {
97 HDF_LOGE("%{public}s: release failed", __func__);
98 return false;
99 }
100 return true;
101 }
102 } // namespace SCSI
103 } // namespace OHOS
104
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)105 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
106 {
107 OHOS::SCSI::ScsiWrite10FuzzTest(data, size);
108 return 0;
109 }