Skip to content

Commit e03597a

Browse files
melbinkmgregkh
authored andcommitted
usb: gadget: printer: fix infinite loop in printer_read()
commit c2e819b upstream. printer_read() uses the same variable for the requested copy size and the number of bytes actually copied to user space. copy_to_user() returns the number of bytes not copied, so when it fails to copy anything, the computed copied length becomes zero. In that case len, buf, current_rx_bytes and current_rx_buf are left unchanged. If RX data is available and the user buffer remains unwritable, the read loop can repeat indefinitely. Track the copied length separately and return -EFAULT, or the number of bytes already copied, if an iteration makes no progress. Fixes: b185f01 ("usb: gadget: printer: factor out f_printer") Cc: stable <stable@kernel.org> Reviewed-by: Peter Chen <peter.chen@kernel.org> Signed-off-by: Melbin K Mathew <mlbnkm1@gmail.com> Link: https://patch.msgid.link/20260709205622.55700-1-mlbnkm1@gmail.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 87bc316 commit e03597a

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

drivers/usb/gadget/function/f_printer.c

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
431431
{
432432
struct printer_dev *dev = fd->private_data;
433433
unsigned long flags;
434-
size_t size;
434+
size_t size, not_copied, copied;
435435
size_t bytes_copied;
436436
struct usb_request *req;
437437
/* This is a pointer to the current USB rx request. */
@@ -524,10 +524,12 @@ printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
524524
else
525525
size = len;
526526

527-
size -= copy_to_user(buf, current_rx_buf, size);
528-
bytes_copied += size;
529-
len -= size;
530-
buf += size;
527+
not_copied = copy_to_user(buf, current_rx_buf, size);
528+
copied = size - not_copied;
529+
530+
bytes_copied += copied;
531+
len -= copied;
532+
buf += copied;
531533

532534
spin_lock_irqsave(&dev->lock, flags);
533535

@@ -542,6 +544,17 @@ printer_read(struct file *fd, char __user *buf, size_t len, loff_t *ptr)
542544
if (dev->interface < 0)
543545
goto out_disabled;
544546

547+
if (!copied) {
548+
dev->current_rx_req = current_rx_req;
549+
dev->current_rx_bytes = current_rx_bytes;
550+
dev->current_rx_buf = current_rx_buf;
551+
spin_unlock_irqrestore(&dev->lock, flags);
552+
mutex_unlock(&dev->lock_printer_io);
553+
return bytes_copied ? bytes_copied : -EFAULT;
554+
}
555+
556+
size = copied;
557+
545558
/* If we not returning all the data left in this RX request
546559
* buffer then adjust the amount of data left in the buffer.
547560
* Othewise if we are done with this RX request buffer then

0 commit comments

Comments
 (0)