1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <MtpDevHandle.h>
18 #include <MtpEventPacket.h>
19 #include <MtpPacketFuzzerUtils.h>
20 #include <functional>
21 #include <fuzzer/FuzzedDataProvider.h>
22 
23 using namespace android;
24 
25 class MtpEventPacketFuzzer : MtpPacketFuzzerUtils {
26   public:
MtpEventPacketFuzzer(const uint8_t * data,size_t size)27     MtpEventPacketFuzzer(const uint8_t* data, size_t size) : mFdp(data, size) {
28         mUsbDevFsUrb = (struct usbdevfs_urb*)malloc(sizeof(struct usbdevfs_urb) +
29                                                    sizeof(struct usbdevfs_iso_packet_desc));
30     };
~MtpEventPacketFuzzer()31     ~MtpEventPacketFuzzer() { free(mUsbDevFsUrb); };
32     void process();
33 
34   private:
35     FuzzedDataProvider mFdp;
36 };
37 
process()38 void MtpEventPacketFuzzer::process() {
39     MtpEventPacket mtpEventPacket;
40     while (mFdp.remaining_bytes() > 0) {
41         auto mtpEventAPI = mFdp.PickValueInArray<const std::function<void()>>({
42                 [&]() { mtpEventPacket.allocate(mFdp.ConsumeIntegralInRange(kMinSize, kMaxSize)); },
43                 [&]() { mtpEventPacket.reset(); },
44                 [&]() { writeHandle(&mtpEventPacket, &mFdp); },
45                 [&]() {
46                     fillFilePath(&mFdp);
47                     int32_t fd = memfd_create(mPath.c_str(), MFD_ALLOW_SEALING);
48                     fillUsbRequest(fd, &mFdp);
49                     mUsbRequest.dev = usb_device_new(mPath.c_str(), fd);
50                     mtpEventPacket.sendRequest(&mUsbRequest);
51                     usb_device_close(mUsbRequest.dev);
52                 },
53                 [&]() {
54                     fillFilePath(&mFdp);
55                     int32_t fd = memfd_create(mPath.c_str(), MFD_ALLOW_SEALING);
56                     fillFd(fd, &mFdp);
57                     struct usb_device* device = usb_device_new(mPath.c_str(), fd);
58                     mtpEventPacket.readResponse(device);
59                     usb_device_close(device);
60                 },
61         });
62         mtpEventAPI();
63     }
64 }
65 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)66 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
67     MtpEventPacketFuzzer mtpEventPacketFuzzer(data, size);
68     mtpEventPacketFuzzer.process();
69     return 0;
70 }
71