Skip to content

usb: gadget: u_serial: fix gs_port use-after-free on open/unbind race

Varun Singh linux-usb lkml 2 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest2 patches 0 repliesNew
  2. v11 patch 0 repliesSuperseded

Changes in v3 from the cover letter

- new 1/2: hold ports[n].lock across slot assignment and device registration, and across slot clearing and device unregistration
- 2/2: unregister-before-free moved into 1/2; otherwise unchanged

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/2usb: gadget: u_serial: serialise line setup against line teardowndrivers/usb/gadget/function/u_serial.c+30 -240 replies
2/2usb: gadget: u_serial: fix gs_port use-after-free on open/unbind racedrivers/usb/gadget/function/u_serial.c+56 -240 replies

Reviews trailers per patch

None of the 2 patches has a Reviewed-by or Acked-by.

Patch[email protected]
1/2
2/2T-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH v3 0/2] usb: gadget: u_serial: fix gs_port use-after-free on open/unbind race

2/2 fixes the use-after-free reported by syzbot in check_tty_count(). It is based on v2, with the device unregistration change moved into 1/2.

1/2 is new. While reviewing v2 I noticed that ports[n].lock protected the ports[n].port slot but not the matching tty device registration, letting concurrent function creation and removal race so that creation fails with -EEXIST. That is independent of the reported use-after-free, but 2/2 relies on the slot and the device changing together, so it goes first.

v2 -> v3:
- new 1/2: hold ports[n].lock across slot assignment and device registration, and across slot clearing and device unregistration
- 2/2: unregister-before-free moved into 1/2; otherwise unchanged

v1 -> v2:
- look the port up in .install and hold a tty_port reference for the lifetime of the tty, instead of clearing tty->driver_data on close
- drop the close_wait handshake: with a correct port count it blocks rmdir until userspace closes /dev/ttyGS*, seen as a hung task
- hang up any tty still installed on a freed line, and recheck in gs_open() that tty->driver_data matches the registered port

Testing, in QEMU against a configfs mkdir/rmdir and open() race reproducer, A/B against unmodified master each time:
- clean under KASAN and, separately, under lockdep (PROVE_LOCKING, DEBUG_ATOMIC_SLEEP, DETECT_HUNG_TASK); kmemleak reported no leaks
- slab fault injection exercised 1/2's registration-failure rollback 107 times, after which the line was still allocatable and openable
- 1/2 was built and booted on its own, to check bisectability
- the series passed #syz test on 2026-11-08 against syzbot's own reproducer, which covers the acm path the local one does not:
"testing succeeded, no crash reproduced"

Not addressed here: gserial_alloc_line() re-reads ports[0].port after the line's lock has been dropped and passes it to gs_console_init(), which has no NULL check, so a concurrent gserial_free_line(0) can leave it NULL. This predates the series. I have left it out rather than pull register_console() under ports[n].lock without investigating that further, and can send a separate patch for it.

Varun Singh (2):
usb: gadget: u_serial: serialise line setup against line teardown
usb: gadget: u_serial: fix gs_port use-after-free on open/unbind race

 drivers/usb/gadget/function/u_serial.c | 132 ++++++++++++++++---------
 1 file changed, 85 insertions(+), 47 deletions(-)
Signature · 1 line
2.34.1