Skip to content

Commit 5ec63fd

Browse files
Prashanth Kgregkh
authored andcommitted
usb: gadget: u_serial: Add null pointer check in gserial_resume
Consider a case where gserial_disconnect has already cleared gser->ioport. And if a wakeup interrupt triggers afterwards, gserial_resume gets called, which will lead to accessing of gser->ioport and thus causing null pointer dereference.Add a null pointer check to prevent this. Added a static spinlock to prevent gser->ioport from becoming null after the newly added check. Fixes: aba3a8d ("usb: gadget: u_serial: add suspend resume callbacks") Cc: stable <stable@kernel.org> Signed-off-by: Prashanth K <quic_prashk@quicinc.com> Acked-by: Alan Stern <stern@rowland.harvard.edu> Link: https://lore.kernel.org/r/1676309438-14922-1-git-send-email-quic_prashk@quicinc.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7ebb605 commit 5ec63fd

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

drivers/usb/gadget/function/u_serial.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@
8282
#define WRITE_BUF_SIZE 8192 /* TX only */
8383
#define GS_CONSOLE_BUF_SIZE 8192
8484

85+
/* Prevents race conditions while accessing gser->ioport */
86+
static DEFINE_SPINLOCK(serial_port_lock);
87+
8588
/* console info */
8689
struct gs_console {
8790
struct console console;
@@ -1375,8 +1378,10 @@ void gserial_disconnect(struct gserial *gser)
13751378
if (!port)
13761379
return;
13771380

1381+
spin_lock_irqsave(&serial_port_lock, flags);
1382+
13781383
/* tell the TTY glue not to do I/O here any more */
1379-
spin_lock_irqsave(&port->port_lock, flags);
1384+
spin_lock(&port->port_lock);
13801385

13811386
gs_console_disconnect(port);
13821387

@@ -1391,7 +1396,8 @@ void gserial_disconnect(struct gserial *gser)
13911396
tty_hangup(port->port.tty);
13921397
}
13931398
port->suspended = false;
1394-
spin_unlock_irqrestore(&port->port_lock, flags);
1399+
spin_unlock(&port->port_lock);
1400+
spin_unlock_irqrestore(&serial_port_lock, flags);
13951401

13961402
/* disable endpoints, aborting down any active I/O */
13971403
usb_ep_disable(gser->out);
@@ -1425,10 +1431,19 @@ EXPORT_SYMBOL_GPL(gserial_suspend);
14251431

14261432
void gserial_resume(struct gserial *gser)
14271433
{
1428-
struct gs_port *port = gser->ioport;
1434+
struct gs_port *port;
14291435
unsigned long flags;
14301436

1431-
spin_lock_irqsave(&port->port_lock, flags);
1437+
spin_lock_irqsave(&serial_port_lock, flags);
1438+
port = gser->ioport;
1439+
1440+
if (!port) {
1441+
spin_unlock_irqrestore(&serial_port_lock, flags);
1442+
return;
1443+
}
1444+
1445+
spin_lock(&port->port_lock);
1446+
spin_unlock(&serial_port_lock);
14321447
port->suspended = false;
14331448
if (!port->start_delayed) {
14341449
spin_unlock_irqrestore(&port->port_lock, flags);

0 commit comments

Comments
 (0)