Skip to content

Commit dd72bb2

Browse files
Prashanth KTreehugger Robot
authored andcommitted
UPSTREAM: 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> Bug: 270271430 (cherry picked from commit 5ec63fdbca604568890c577753c6f66c5b3ef0b5 https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git/ usb-next) Change-Id: I61e89445ef9cf72ac14b532c098e36c8ff6c1200 Signed-off-by: Prashanth K <quic_prashk@quicinc.com>
1 parent a2ee0d8 commit dd72bb2

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

drivers/usb/gadget/function/u_serial.c

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

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

1380+
spin_lock_irqsave(&serial_port_lock, flags);
1381+
13771382
/* tell the TTY glue not to do I/O here any more */
1378-
spin_lock_irqsave(&port->port_lock, flags);
1383+
spin_lock(&port->port_lock);
13791384

13801385
gs_console_disconnect(port);
13811386

@@ -1390,7 +1395,8 @@ void gserial_disconnect(struct gserial *gser)
13901395
tty_hangup(port->port.tty);
13911396
}
13921397
port->suspended = false;
1393-
spin_unlock_irqrestore(&port->port_lock, flags);
1398+
spin_unlock(&port->port_lock);
1399+
spin_unlock_irqrestore(&serial_port_lock, flags);
13941400

13951401
/* disable endpoints, aborting down any active I/O */
13961402
usb_ep_disable(gser->out);
@@ -1424,10 +1430,19 @@ EXPORT_SYMBOL_GPL(gserial_suspend);
14241430

14251431
void gserial_resume(struct gserial *gser)
14261432
{
1427-
struct gs_port *port = gser->ioport;
1433+
struct gs_port *port;
14281434
unsigned long flags;
14291435

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

0 commit comments

Comments
 (0)