[PATCH v2 1/2] usb: gadget: u_serial: Publish the console only under port_lock
From: Fabricio Akio via B4 Relay
Date: Sun Nov 22 2026 - 20:48:55 EST
From: Fabricio Akio <fabricioakio@xxxxxxxxx>
gserial_connect() and gserial_disconnect() find the console through
port->console under port_lock, but gs_console_init() sets it before
taking the lock, and gs_console_exit() clears it after dropping it.
A host connect while the console is being turned on through configfs
can then connect it twice, leaking the first request. While it is
being turned off, a connect can attach a request to the console being
freed and schedule its work after cancel_work_sync().
Set and clear port->console under port_lock, together with connecting
and disconnecting the console.
Fixes: d7cb8fb7aa7d ("usb: gadget: u_serial: allow more console gadget ports")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Fabricio Akio <fabricioakio@xxxxxxxxx>
---
drivers/usb/gadget/function/u_serial.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
index 0a304f508bc3..1f0110ea7f6a 100644
--- a/drivers/usb/gadget/function/u_serial.c
+++ b/drivers/usb/gadget/function/u_serial.c
@@ -1173,10 +1173,10 @@ static int gs_console_init(struct gs_port *port)
return err;
}
- port->console = cons;
register_console(&cons->console);
spin_lock_irq(&port->port_lock);
+ port->console = cons;
if (port->port_usb)
gs_console_connect(port);
spin_unlock_irq(&port->port_lock);
@@ -1196,12 +1196,12 @@ static void gs_console_exit(struct gs_port *port)
spin_lock_irq(&port->port_lock);
if (cons->req)
gs_console_disconnect(port);
+ port->console = NULL;
spin_unlock_irq(&port->port_lock);
cancel_work_sync(&cons->work);
kfifo_free(&cons->buf);
kfree(cons);
- port->console = NULL;
}
ssize_t gserial_set_console(unsigned char port_num, const char *page, size_t count)
--
2.56.1