• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <MtpPacketFuzzerUtils.h>
19 #include <MtpResponsePacket.h>
20 #include <functional>
21 #include <fuzzer/FuzzedDataProvider.h>
22 
23 using namespace android;
24 
25 class MtpResponsePacketFuzzer : MtpPacketFuzzerUtils {
26   public:
MtpResponsePacketFuzzer(const uint8_t * data,size_t size)27     MtpResponsePacketFuzzer(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     };
~MtpResponsePacketFuzzer()31     ~MtpResponsePacketFuzzer() { free(mUsbDevFsUrb); };
32     void process();
33 
34   private:
35     FuzzedDataProvider mFdp;
36 };
37 
process()38 void MtpResponsePacketFuzzer::process() {
39     MtpResponsePacket mtpResponsePacket;
40     while (mFdp.remaining_bytes() > 0) {
41         auto mtpResponseAPI = mFdp.PickValueInArray<const std::function<void()>>({
42                 [&]() {
43                     mtpResponsePacket.allocate(
44                             mFdp.ConsumeIntegralInRange(kMinSize, kMaxSize)); /*size*/
45                 },
46                 [&]() { mtpResponsePacket.reset(); },
47                 [&]() { writeHandle(&mtpResponsePacket, &mFdp); },
48                 [&]() {
49                     fillFilePath(&mFdp);
50                     int32_t fd = memfd_create(mPath.c_str(), MFD_ALLOW_SEALING);
51                     fillUsbRequest(fd, &mFdp);
52                     mUsbRequest.dev = usb_device_new(mPath.c_str(), fd);
53                     mtpResponsePacket.read(&mUsbRequest);
54                     usb_device_close(mUsbRequest.dev);
55                 },
56         });
57         mtpResponseAPI();
58     }
59 }
60 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
62     MtpResponsePacketFuzzer mtpResponsePacketFuzzer(data, size);
63     mtpResponsePacketFuzzer.process();
64     return 0;
65 }
66