Re: [PATCH] usb: gadget: u_serial: implement flush_buffer

From: Mehmet Fide

Date: Thu Oct 15 2026 - 15:41:41 EST


The window is real, but the use-after-free in it is older than this
patch.

I reproduced it on a Verdin AM62 with 6.12.113 and KASAN, with a 3 s
delay added at the top of gs_close() to widen the window. At shutdown
systemd closes ttyGS0 while the configfs teardown unbinds the gadget
and removes the acm function:

BUG: KASAN: slab-use-after-free in check_tty_count+0x230/0x278
Workqueue: events do_tty_hangup
__tty_hangup.part.0
do_tty_hangup
Allocated by: gserial_alloc_line_no_console <- acm_alloc_instance
Freed by: gserial_free_port <- gserial_free_line <- acm_free_instance

In one of the runs the closing task then faulted in release_tty() ->
tty_buffer_cancel_work(). The KASAN report is the same with this patch
not applied.

gserial_free_port() frees the gs_port, and with it the embedded
tty_port, while a tty still refers to it: the tty core keeps using
tty->port until release_tty(), and u_serial has no install/cleanup pair
that holds a reference on the port meanwhile. I will send a v2 as a
series: first keep the gs_port alive through the tty_port reference
count (get in install, put in cleanup, free in the destructor), then
flush_buffer on top of it.

Greg, please drop this version.

Thanks,
Mehmet