Skip to content

Commit

Permalink
usb: gadget: f_hid: fixed NULL pointer dereference
Browse files Browse the repository at this point in the history
commit 2867652e4766360adf14dfda3832455e04964f2a upstream.

Disconnecting and reconnecting the USB cable can lead to crashes
and a variety of kernel log spam.

The problem was found and reproduced on the Raspberry Pi [1]
and the original fix was created in Raspberry's own fork [2].

Link: raspberrypi/linux#3870 [1]
Link: raspberrypi/linux@a6e47d5 [2]
Signed-off-by: Maxim Devaev <[email protected]>
Signed-off-by: Phil Elwell <[email protected]>
Cc: stable <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
pelwell authored and acoffeerunner committed Sep 28, 2021
1 parent 0818dbe commit ead61ca
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions drivers/usb/gadget/function/f_hid.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,11 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,

spin_lock_irqsave(&hidg->write_spinlock, flags);

if (!hidg->req) {
spin_unlock_irqrestore(&hidg->write_spinlock, flags);
return -ESHUTDOWN;
}

#define WRITE_COND (!hidg->write_pending)
try_again:
/* write queue */
Expand All @@ -365,8 +370,14 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
count = min_t(unsigned, count, hidg->report_length);

spin_unlock_irqrestore(&hidg->write_spinlock, flags);
status = copy_from_user(req->buf, buffer, count);

if (!req) {
ERROR(hidg->func.config->cdev, "hidg->req is NULL\n");
status = -ESHUTDOWN;
goto release_write_pending;
}

status = copy_from_user(req->buf, buffer, count);
if (status != 0) {
ERROR(hidg->func.config->cdev,
"copy_from_user error\n");
Expand Down Expand Up @@ -394,14 +405,17 @@ static ssize_t f_hidg_write(struct file *file, const char __user *buffer,

spin_unlock_irqrestore(&hidg->write_spinlock, flags);

if (!hidg->in_ep->enabled) {
ERROR(hidg->func.config->cdev, "in_ep is disabled\n");
status = -ESHUTDOWN;
goto release_write_pending;
}

status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
if (status < 0) {
ERROR(hidg->func.config->cdev,
"usb_ep_queue error on int endpoint %zd\n", status);
if (status < 0)
goto release_write_pending;
} else {
else
status = count;
}

return status;
release_write_pending:
Expand Down

0 comments on commit ead61ca

Please sign in to comment.