• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "scsireadcapacity10_fuzzer.h"
17 #include <cstring>
18 #include <securec.h>
19 #include <scsi/sg.h>
20 #include <unistd.h>
21 #include "hdf_log.h"
22 #include "scsicommonfunction_fuzzer.h"
23 
24 using namespace OHOS::HDI::Usb::ScsiDdk::V1_0;
25 
26 namespace OHOS {
27 namespace SCSI {
ConstructParamsFromData(ScsiPeripheralReadCapacityRequest & request,const uint8_t * data,size_t size)28 static void ConstructParamsFromData(ScsiPeripheralReadCapacityRequest& request, const uint8_t* data, size_t size)
29 {
30     if (data == nullptr || size < sizeof(uint32_t)) {
31         return;
32     }
33 
34     (void)memcpy_s(&request.lbAddress, sizeof(uint32_t), data, sizeof(uint32_t));
35     size_t offset = sizeof(uint32_t);
36 
37     if (size >= offset + sizeof(uint8_t)) {
38         request.control = *(data + offset);
39         offset += sizeof(uint8_t);
40     }
41 
42     if (size >= offset + sizeof(uint8_t)) {
43         request.byte8 = *(data + offset);
44         offset += sizeof(uint8_t);
45     }
46 
47     if (size >= offset + sizeof(uint32_t)) {
48         (void)memcpy_s(&request.timeout, sizeof(uint32_t), data + offset, sizeof(uint32_t));
49     }
50 }
51 
ScsiReadCapacity10FuzzTest(const uint8_t * data,size_t size)52 bool ScsiReadCapacity10FuzzTest(const uint8_t *data, size_t size)
53 {
54     sptr<IScsiPeripheralDdk> scsiPeripheralDdk = IScsiPeripheralDdk::Get();
55     if (scsiPeripheralDdk == nullptr) {
56         HDF_LOGE("%{public}s: get ddk failed", __func__);
57         return false;
58     }
59     int32_t ret = scsiPeripheralDdk->Init();
60     if (ret != HDF_SUCCESS) {
61         HDF_LOGE("%{public}s: init failed, ret = %{public}d", __func__, ret);
62         return false;
63     }
64 
65     ScsiPeripheralDevice device;
66     ret = ScsiFuzzTestHostModeInit(scsiPeripheralDdk, device);
67     if (ret != HDF_SUCCESS) {
68         HDF_LOGE("%{public}s: scsi peripheral open device failed, ret = %{public}d", __func__, ret);
69         return false;
70     }
71     ScsiPeripheralReadCapacityRequest request = {};
72     ConstructParamsFromData(request, data, size);
73     ScsiPeripheralCapacityInfo capacityInfo;
74     ScsiPeripheralResponse response;
75     ret = scsiPeripheralDdk->ReadCapacity10(device, request, capacityInfo, response);
76 
77     ret = scsiPeripheralDdk->Close(device);
78     if (ret != HDF_SUCCESS) {
79         HDF_LOGE("%{public}s: close device failed", __func__);
80         scsiPeripheralDdk->Release();
81         return false;
82     }
83     ret = scsiPeripheralDdk->Release();
84     if (ret != HDF_SUCCESS) {
85         HDF_LOGE("%{public}s: release failed", __func__);
86         return false;
87     }
88     return true;
89 }
90 } // namespace SCSI
91 } // namespace OHOS
92 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
94 {
95     OHOS::SCSI::ScsiReadCapacity10FuzzTest(data, size);
96     return 0;
97 }