Home

Awesome

Raw Gadget

Raw Gadget is a Linux kernel module that implements a low-level interface for the Linux USB Gadget subsystem.

Raw Gadget can be used to emulate USB devices, both physical and virtual ones. Emulating physical devices requires a Linux-based board with a USB Device Controller (UDC), such as a Raspberry Pi. Emulating virtual devices requires no hardware and instead relies on the Dummy HCD/UDC module (such devices get connected to the kernel Raw Gadget is running on).

This repository contains instructions, examples, and tests for Raw Gadget. In addition, this repository hosts a copy of the Dummy HCD/UDC kernel module for out-of-tree building.

See the Fuzzing USB with Raw Gadget talk [video] for details about the Linux Host and Gadget USB subsystems and Raw Gadget.

Note: Do not use Raw Gadget in production for emulating USB devices with specific classes. Instead, use the Composite Framework or the legacy gadget driver modules.

Comparison to other interfaces

The Linux kernel provides a number of interfaces for the USB Gadget subsystem that allow emulating USB devices. Most notably there is the Composite Framework (including the FunctionFS-based composite function) and the legacy gadget drivers modules (including GadgetFS).

Most of the Gadset subsystem interfaces (with the exception of GadgetFS and the FunctionFS-based composite function) only allow emulating USB devices of specific classes. Compared to them, Raw Gadget allows emulating USB devices of arbitary classes.

GadgetFS and the FunctionFS-based composite function do allow emulating USB devices of arbitrary classes. However, these interfaces allow only a limited control over the responses to some USB requests, as they perform sanity checks on the responses provided from userspace. This limits their ability of emulating improper USB devices, which might be useful for fuzzing or exploitation. Compared to them, Raw Gadget has minimal checks on the provided responses.

Raw Gadget is thus the perfect choice for fuzzing and exploiting USB hosts or for software proxying of USB devices.

You can find more details about the difference between Raw Gadget and GadgetFS in the kernel documentation.

Limitations

While Raw Gadget does support emulating a wide range of USB device types, it has a set of known limitations:

These are not foundational limitations of the technology but rather just features missing from the implementation. They might addressed in the future.

Usage

Raw Gadget can be used on any Linux-based board that has a USB Device Controller (UDC) — a hardware component that allows the board to act as a USB peripheral device. Consult the documentation for your board on whether it has a UDC (often marketed as USB OTG) and how to enable it.

To set up Raw Gadget, you need to:

  1. Enable the UDC on your board;
  2. Find out the UDC device and driver names;
  3. Build and load the Raw Gadget module.

Once the setup is done, you can try running the provided examples.

See Raw Gadget on Raspberry Pi for end-to-end instructions on how to set up Raw Gadget on a Raspberry Pi board.

Building

There are two options of building Raw Gadget:

USB Device Controllers

Raw Gadget requires the user to provide the UDC device and driver names. This allows using Raw Gadget with a particular UDC if a few of them are present on the system.

UDC device names can be found in /sys/class/udc/:

$ ls /sys/class/udc/
dummy_udc.0

The UDC driver name is usually present in /sys/class/udc/$UDC_DEVICE_NAME/uevent:

$ cat /sys/class/udc/dummy_udc.0/uevent
USB_UDC_NAME=dummy_udc

Below is a table of hardware with various UDCs that was tested with Raw Gadget.

HardwareKernelDriverDeviceWorks?
Any5.3.0-45-genericdummy_udcdummy_udc.0Yes
Raspberry Pi Zero4.14.97+20980000.usb20980000.usb (dwc2)Yes
Raspberry Pi 45.10.63-v7l+fe980000.usbfe980000.usb (dwc2)Yes
Raspberry Pi 56.8.0-1007-raspi1000480000.usb1000480000.usb (dwc2)Likely
USB Armory Mk II5.4.87-02184000.usbci_hdrc.0Yes
Orange Pi PC5.10.60musb-hdrcmusb-hdrc.4.autoYes
Orange Pi PC 25.10.60musb-hdrcmusb-hdrc.4.autoYes
Khadas VIM15.10.60-meson64c9100000.usbc9100000.usbYes
ThinkPad X1 Carbon Gen 65.15.0-107-genericdwc3-gadgetdwc3.1.autoYes
NXP i.MX8MP6.6.23dwc3-gadget38100000.usbYes
BeagleBone Black4.19.94-ti-r42musb-hdrcmusb-hdrc.0Probably
BeagleBone AI4.14.108-ti-r131dwc3-gadget48890000.usbProbably
EC3380-AB5.3.0-45-genericnet22800000:04:00.0 (e.g.)Partially,<br />net2280 buggy
Odroid C23.14.79-116dwc_otg_pcddwc2_aNo, kernel too old

"Works" in the table above means that the setup passed the provided tests. However, note that those only cover a subset of functionality.

syzkaller integration

Raw Gadget powers the syzkaller's ability to fuzz the Linux kernel USB stack.

See Running syzkaller USB reproducers for instructions on running syzkaller USB reproducers on a Linux-based board plugged into a physical USB host.

You also set up syzkaller in the isolated mode to fuzz physical USB hosts with the help of Raw Gadget. Instructions for this are not provided.

Facedancer backend

There's a prototype of a Facedancer backend based on Raw Gadget.

This backend relies on a few out-of-tree Raw Gadget patches present in the dev branch. Once the backend is thoroughly tested, these patches will be submitted to the mainline.

Raw Gadget–based backend accepts a few parameters through environment variables:

ParameterDescriptionDefault value
RG_UDC_DRIVERUDC driver namedummy_udc
RG_UDC_DEVICEUDC device namedummy_udc.0
RG_USB_SPEEDUSB device speed3 (High Speed)

Example of using Facedancer with Raw Gadget to emulate a USB keyboard on a Raspberry Pi 4:

export BACKEND=rawgadget
export RG_UDC_DRIVER=fe980000.usb
export RG_UDC_DEVICE=fe980000.usb
./legacy-applets/facedancer-keyboard.py

Note: Some Facedancer examples might fail if a wrong USB speed is specified. Failures happen either with EINVAL in USB_RAW_IOCTL_EP_ENABLE, with ESHUTDOWN in USB_RAW_IOCTL_EP_READ/WRITE, or can be completely random. For example, with Dummy UDC, examples/ftdi-echo.py requires RG_USB_SPEED=2 and legacy-applets/facedancer-ftdi.py requires RG_USB_SPEED=3. In turn, legacy-applets/facedancer-umass.py requires RG_USB_SPEED=2.

Note: This backend is still a prototype. Outstanding tasks:

  1. Rebase the backend onto Facedancer 3.0 release;
  2. Make sure that all required backend callbacks are implemented. For example, read_from_endpoint should probably be implemented;
  3. Provide a common Python wrapper for Raw Gadget ioctls, and use it in the backend;
  4. Finalize and submit out-of-tree Raw Gadget patches to the mainline.

Note: Facedancer assumes that every backend supports non-blocking I/O, which is not the case for Raw Gadget. To work around this limitation, the backend prototype relies on timeouts. The proper solution to this issue would be to add non-blocking I/O support to Raw Gadget.

Troubleshooting

As a generic guidance to troubleshooting Raw Gadget errors:

  1. Consider switching to the dev branch.

    This branch might contain fixes for some known issues;

  2. Enable debug output for Raw Gadget (and Dummy HCD/UDC if you're using it).

    To do this, add the following line to the very beginning of raw_gadget/raw_gadget.c:

    #define DEBUG
    

    Then, rebuild and reinsert the Raw Gadget module;

  3. Check the kernel log via dmesg to find out what is failing.

No such device

USB_RAW_IOCTL_RUN returns ENODEV, error code 19:

ioctl(USB_RAW_IOCTL_RUN): No such device

This error means that bad UDC driver/device names were provided. Make sure that the UDC driver module is loaded. Also see USB Device Controllers about UDC names.

Cannot send after transport endpoint shutdown

Endpoint operations return ESHUTDOWN, error code 108:

ioctl(USB_RAW_IOCTL_EP0_WRITE): Cannot send after transport endpoint shutdown

This error means that the emulated USB device tried to perform a read or write operation on a disabled endpoint. This error can happen due to a variety of reasons, but it is commonly observed when the host decides to reset the device during its operation, following which the UDC driver disables all enabled endpoints.

Often, a reset happens when the device emulation code does something wrong. For example, provides a bad USB descriptor that is either malformed or inconsistent with the emulated device speed or other parameters. As a result, either the UDC driver or the host resets or disconnects the device.

However, a reset can happen during a normal device operation. For example, the host might decide to reconfigure the device and thus will reset it. The UDC driver will then deactivate all endpoints and any attempt to perform an endpoint operation will fail with ESHUTDOWN. When this happens, Raw Gadget will issue a USB_RAW_EVENT_RESET event (or USB_RAW_EVENT_DISCONNECT for dwc2). The device emulation code needs to gracefully handle ESHUTDOWN, disable all Raw Gadget endpoints when hanlding the USB_RAW_EVENT_RESET event, restart enumeration, and reenable the endpoints when handling a new SET_CONFIGURATION request. See the keyboard example for a reference implementation.

Projects based on Raw Gadget

Fuzzing-related:

Proxies:

Emulators:

Other:

TODO

License

The parts of code in this repository that are derived from the Linux kernel are covered by GPL-2.0. Everything else is covered by Apache-2.0. SPDX-License-Identifier marks the used license in each file.