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 "scsisendrequestbycdb_fuzzer.h"
17 #include <scsi/sg.h>
18 #include "hdf_log.h"
19 #include "scsicommonfunction_fuzzer.h"
20
21 using namespace OHOS::HDI::Usb::ScsiDdk::V1_0;
22
23 namespace OHOS {
24 namespace SCSI {
25
26 constexpr uint32_t TIMEOUT = 5000;
27
ScsiSendRequestByCDBFuzzTest(const uint8_t * data,size_t size)28 bool ScsiSendRequestByCDBFuzzTest(const uint8_t *data, size_t size)
29 {
30 sptr<IScsiPeripheralDdk> scsiPeripheralDdk = IScsiPeripheralDdk::Get();
31 if (scsiPeripheralDdk == nullptr) {
32 HDF_LOGE("%{public}s: get ddk failed", __func__);
33 return false;
34 }
35 int32_t ret = scsiPeripheralDdk->Init();
36 if (ret != HDF_SUCCESS) {
37 HDF_LOGE("%{public}s: init failed, ret = %{public}d", __func__, ret);
38 return false;
39 }
40
41 ScsiPeripheralDevice device;
42 ret = ScsiFuzzTestHostModeInit(scsiPeripheralDdk, device);
43 if (ret != HDF_SUCCESS) {
44 HDF_LOGE("%{public}s: scsi peripheral open device failed, ret = %{public}d", __func__, ret);
45 return false;
46 }
47
48 ScsiPeripheralRequest request = {};
49 if (size > 0) {
50 request.commandDescriptorBlock.assign(data, data + size);
51 }
52 request.dataTransferDirection = SG_DXFER_NONE;
53 request.memMapSize = size;
54 request.timeout = TIMEOUT;
55
56 ScsiPeripheralResponse response;
57 ret = scsiPeripheralDdk->SendRequestByCDB(device, request, response);
58
59 ret = scsiPeripheralDdk->Close(device);
60 if (ret != HDF_SUCCESS) {
61 HDF_LOGE("%{public}s: close device failed", __func__);
62 scsiPeripheralDdk->Release();
63 return false;
64 }
65 ret = scsiPeripheralDdk->Release();
66 if (ret != HDF_SUCCESS) {
67 HDF_LOGE("%{public}s: release failed", __func__);
68 return false;
69 }
70 return true;
71 }
72 } // namespace SCSI
73 } // namespace OHOS
74
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)75 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
76 {
77 OHOS::SCSI::ScsiSendRequestByCDBFuzzTest(data, size);
78 return 0;
79 }