Re: [PATCH v2 0/7] usb: fix UAF related to dynamic ID

From: Greg Kroah-Hartman

Date: Fri Jul 10 2026 - 09:16:08 EST


On Tue, Jul 07, 2026 at 01:26:39PM +0100, Gary Guo wrote:
> This is the USB version of the dynamic ID UAF fix similar to that of PCI
> [1]. usb_match_dynamic_id returns a pointer to field of usb_dynid, which
> can be freed when dynamic ID is removed via sysfs. It can be triggered with
> the following sequence:
>
> echo <vid> <pid> > /sys/bus/usb/drivers/<name>/new_id
> <probe start>
> echo <vid> <pid> > /sys/bus/usb/drivers/<name>/remove_id
> <probe use ID>
>
> Fix it by making a stack copy of the ID. This does mean that the lifetime
> of ID is scoped to probe (which is already the case but never spelled out
> explicitly). Drivers use these device IDs creatively, so this series also
> fix these drivers.
>
> The following coccinelle script is used to find all cases that are deemed
> suspicious. Only useful case for IDs should be access its fields, or
> forwarding (without type cast) to functions that do so.
>
> @usage@
> identifier fn, id;
> position p;
> @@
> fn(..., struct usb_device_id *id, ...)
> {
> ...
> id@p
> ...
> }
>
> // Due to cocci isomorphism this needs to be explicit
> @bad@
> identifier fn, id;
> type T;
> position usage.p;
> @@
> fn(..., struct usb_device_id *id, ...)
> {
> ...
> (T*)id@p
> ...
> }
>
> // Good use cases
> @good@
> identifier fn, id, fld;
> expression E;
> position usage.p;
> @@
> fn(..., struct usb_device_id *id, ...)
> {
> ...
> (
> id@p->fld
> |
> E(..., id@p, ...)
> |
> // Redundant checks, but ignore
> !id@p
> |
> // Redundant checks, but ignore
> id ? ... : ...
> )
> ...
> }
>
> @script:python depends on usage && (bad || !good)@
> p << usage.p;
> @@
> coccilib.report.print_report(p[0], "suspicious use of device ID")
>
> There're 3 drivers that store usb_device_id, and they're converted to just
> use driver_info instead. The other fields of usb_device_id can be easily
> retrieved from usb_device via descriptor.id{Vendor,Product}.
>
> There're also a few users that rely on pointer arithmetics. Pegaus and
> xusbatm are converted to use driver_info. All unusal USB mass storage
> drivers rely on pointer arithemtic to index into a side table, because USB
> storage subsystem is using the driver_data for flags. Luckily all these
> drivers set no_dynamic_id. Ideally these could be fixed too but their
> maintainers probably have a better idea of how.
>
> Link: https://lore.kernel.org/driver-core/20260706-pci_id_fix-v3-0-2d48fc025acc@xxxxxxxxxxx [1]
>
> Signed-off-by: Gary Guo <gary@xxxxxxxxxxx>

Thanks for these. I had an old patch series that attempted to do some
locking in this area to fix this up, but this version is much nicer.
I'll go queue it up now.

greg k-h