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