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 "scsirequestsense_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 {
ConstructParamsFromData(ScsiPeripheralRequestSenseRequest & request,const uint8_t * data,size_t size)27 static void ConstructParamsFromData(ScsiPeripheralRequestSenseRequest& request, const uint8_t* data, size_t size)
28 {
29 if (data == nullptr || size < sizeof(uint8_t)) {
30 return;
31 }
32
33 request.allocationLength = *data;
34 size_t offset = sizeof(uint8_t);
35
36 if (size >= offset + sizeof(uint8_t)) {
37 request.control = *(data + offset);
38 offset += sizeof(uint8_t);
39 }
40
41 if (size >= offset + sizeof(uint8_t)) {
42 request.byte1 = *(data + offset);
43 offset += sizeof(uint8_t);
44 }
45
46 if (size >= offset + sizeof(uint32_t)) {
47 (void)memcpy_s(&request.timeout, sizeof(uint32_t), data + offset, sizeof(uint32_t));
48 }
49 }
50
ScsiRequestSenseFuzzTest(const uint8_t * data,size_t size)51 bool ScsiRequestSenseFuzzTest(const uint8_t *data, size_t size)
52 {
53 sptr<IScsiPeripheralDdk> scsiPeripheralDdk = IScsiPeripheralDdk::Get();
54 if (scsiPeripheralDdk == nullptr) {
55 HDF_LOGE("%{public}s: get ddk failed", __func__);
56 return false;
57 }
58 int32_t ret = scsiPeripheralDdk->Init();
59 if (ret != HDF_SUCCESS) {
60 HDF_LOGE("%{public}s: init failed, ret = %{public}d", __func__, ret);
61 return false;
62 }
63
64 ScsiPeripheralDevice device;
65 ret = ScsiFuzzTestHostModeInit(scsiPeripheralDdk, device);
66 if (ret != HDF_SUCCESS) {
67 HDF_LOGE("%{public}s: scsi peripheral open device failed, ret = %{public}d", __func__, ret);
68 return false;
69 }
70 ScsiPeripheralRequestSenseRequest request = {};
71 ConstructParamsFromData(request, data, size);
72 ScsiPeripheralResponse response;
73 ret = scsiPeripheralDdk->RequestSense(device, request, response);
74
75 ret = scsiPeripheralDdk->Close(device);
76 if (ret != HDF_SUCCESS) {
77 HDF_LOGE("%{public}s: close device failed", __func__);
78 scsiPeripheralDdk->Release();
79 return false;
80 }
81 ret = scsiPeripheralDdk->Release();
82 if (ret != HDF_SUCCESS) {
83 HDF_LOGE("%{public}s: release failed", __func__);
84 return false;
85 }
86 return true;
87 }
88 } // namespace SCSI
89 } // namespace OHOS
90
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
92 {
93 OHOS::SCSI::ScsiRequestSenseFuzzTest(data, size);
94 return 0;
95 }