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