1 /*
2 * Copyright (c) 2022 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 <cstddef>
17 #include <cstdint>
18 #include <unistd.h>
19
20 #include "UsbSubscriberTest.h"
21 #include "hdf_log.h"
22 #include "message_parcel.h"
23 #include "usb_impl.h"
24 #include "usbhost_fuzzer.h"
25 #include "v1_0/usb_interface_stub.h"
26
27 using namespace OHOS::HDI::Usb::V1_0;
28 using OHOS::HDI::Usb::V1_0::UsbDev;
29 using namespace OHOS::USB;
30
31 #define INDEX_ZERO 0
32 #define INDEX_ONE 1
33 #define INDEX_TWO 2
34 #define INDEX_THREE 3
35 #define DIGIT_MAX 24
36 #define DIGIT_MID 16
37 #define DIGIT_SML 8
38
39 namespace OHOS {
40 constexpr size_t THRESHOLD = 10;
41 constexpr int32_t OFFSET = 4;
42 const std::u16string USB_INTERFACE_TOKEN = u"ohos.hdi.usb.v1_0.IUsbInterface";
43 const int32_t SLEEP_TIME = 3;
44 const int32_t DEFAULT_PORT_ID = 1;
45 const int32_t DEFAULT_ROLE_HOST = 1;
46
Convert2Uint32(const uint8_t * ptr)47 uint32_t Convert2Uint32(const uint8_t *ptr)
48 {
49 if (ptr == nullptr) {
50 return 0;
51 }
52 /*
53 * Move the 0th digit 24 to the left, the first digit 16 to the left, the second digit 8 to the left,
54 * and the third digit no left
55 */
56 return (ptr[INDEX_ZERO] << DIGIT_MAX) | (ptr[INDEX_ONE] << DIGIT_MID)
57 | (ptr[INDEX_TWO] << DIGIT_SML) | (ptr[INDEX_THREE]);
58 }
59
DoSomethingInterestingWithMyAPI(const uint8_t * rawData,size_t size)60 bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
61 {
62 if (rawData == nullptr) {
63 return false;
64 }
65 uint32_t code = Convert2Uint32(rawData);
66 rawData = rawData + OFFSET;
67 size = size - OFFSET;
68
69 MessageParcel data;
70 data.WriteInterfaceToken(USB_INTERFACE_TOKEN);
71 data.WriteBuffer(rawData, size);
72 data.RewindRead(0);
73 MessageParcel reply;
74 MessageOption option;
75
76 sptr<IUsbInterface> usbInterface = IUsbInterface::Get(false);
77 if (usbInterface == nullptr) {
78 HDF_LOGE("%{public}s:IUsbInterface::Get() failed.", __func__);
79 return false;
80 }
81
82 int32_t ret = usbInterface->SetPortRole(DEFAULT_PORT_ID, DEFAULT_ROLE_HOST, DEFAULT_ROLE_HOST);
83 sleep(SLEEP_TIME);
84 if (ret != HDF_SUCCESS) {
85 HDF_LOGE("%{public}s: set port role as host failed", __func__);
86 return ret;
87 }
88
89 sptr<UsbSubscriberTest> subscriber = new UsbSubscriberTest();
90 ret = usbInterface->BindUsbdSubscriber(subscriber);
91 if (ret != HDF_SUCCESS) {
92 HDF_LOGE("%{public}s: bind usbd subscriber failed", __func__);
93 return ret;
94 }
95
96 sleep(1);
97 UsbDev dev;
98 dev.busNum = subscriber->busNum_;
99 dev.devAddr = subscriber->devAddr_;
100 ret = usbInterface->OpenDevice(dev);
101 if (ret != HDF_SUCCESS) {
102 HDF_LOGE("%{public}s: open device failed", __func__);
103 return ret;
104 }
105
106 sptr<UsbInterfaceStub> impl = new UsbInterfaceStub(usbInterface);
107 if (impl == nullptr) {
108 HDF_LOGE("%{public}s:new UsbInterfaceStub failed.", __func__);
109 return false;
110 }
111 impl->OnRemoteRequest(code, data, reply, option);
112
113 return true;
114 }
115 } // namespace OHOS
116
117 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)118 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
119 {
120 if (size < OHOS::THRESHOLD) {
121 return 0;
122 }
123
124 /* Run your code on data */
125 OHOS::DoSomethingInterestingWithMyAPI(data, size);
126 return 0;
127 }
128