All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jaegyun Lee <dreams200260@gmail.com>
To: gregkh@linuxfoundation.org, Neill Kapron <nkapron@google.com>
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	Jaegyun Lee <dreams200260@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH v2] usb: gadget: f_printer: take dev->lock on copy_from_user() failure
Date: Thu,  3 Dec 2026 09:20:08 +0900	[thread overview]
Message-ID: <20261203002008.1704433-1-dreams200260@gmail.com> (raw)
In-Reply-To: <2026112917-starship-petty-26cf@gregkh>

In printer_write(), dev->lock is dropped before copy_from_user().
When copy_from_user() fails, the error path returns the request to
dev->tx_reqs without retaking dev->lock:

	spin_unlock_irqrestore(&dev->lock, flags);

	if (copy_from_user(req->buf, buf, size)) {
		list_add(&req->list, &dev->tx_reqs);
		mutex_unlock(&dev->lock_printer_io);
		return bytes_copied;
	}

dev->lock_printer_io only serializes userspace I/O calls and does not
protect dev->tx_reqs from tx_complete(), which concurrently appends
completed requests to the same list under dev->lock from UDC giveback.

Reproduced on dummy_hcd with one thread streaming writes to
/dev/g_printer0 while another writes from a PROT_NONE buffer to make
copy_from_user() fail:

[   40.986249][    C1]  slab kmalloc-128 start ffff888024617600 pointer offset 88 size 128
[   40.986284][    C1] list_add corruption. next->prev should be prev (ffff88802afa9170), but was ffff888024ee6d58. (next=ffff888024617658).
[   40.986774][    C1] ------------[ cut here ]------------
[   40.986779][    C1] kernel BUG at lib/list_debug.c:29!
[   40.986795][    C1] Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
[   40.990506][    C1] CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full) 
[   40.991508][    C1] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[   40.992428][    C1] RIP: 0010:__list_add_valid_or_report+0xf0/0x1a0
[   40.993076][    C1] Code: 00 00 00 00 fc ff df 48 c1 ea 03 80 3c 02 00 0f 85 a6 00 00 00 49 8b 54 24 08 4c 89 e1 48 c7 c7 a0 bc 66 8c e8 31 46 a3 fc 90 <0f> 0b 48 89 f7 48 89 34 24 e8 72 7e 17 fd 48 8b 34 24 48 b8 00 00
[   40.994976][    C1] RSP: 0018:ffffc900001f8c10 EFLAGS: 00010086
[   40.995575][    C1] RAX: 0000000000000075 RBX: ffff888024617e10 RCX: 0000000000000102
[   40.996356][    C1] RDX: 0000000000000000 RSI: ffffffff81a10b90 RDI: 0000000000000005
[   40.997138][    C1] RBP: ffff88802afa9000 R08: ffffffff81a092f1 R09: 0000000000000000
[   40.997917][    C1] R10: ffffffff8fcfa1ab R11: 0000000000000000 R12: ffff888024617658
[   40.998695][    C1] R13: ffff888024617e58 R14: ffff888024617660 R15: ffff88802afa9170
[   40.999474][    C1] FS:  0000000000000000(0000) GS:ffff8880e9493000(0000) knlGS:0000000000000000
[   41.000346][    C1] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[   41.000995][    C1] CR2: 0000561c51e97730 CR3: 0000000050d84000 CR4: 0000000000350ef0
[   41.001767][    C1] Call Trace:
[   41.002107][    C1]  <IRQ>
[   41.002392][    C1]  tx_complete+0x16d/0x340
[   41.002839][    C1]  dummy_timer+0x19a3/0x35c0
[   41.003308][    C1]  ? find_held_lock+0x2b/0x80
[   41.003781][    C1]  ? find_held_lock+0x2b/0x80
[   41.004255][    C1]  ? __pfx_dummy_timer+0x10/0x10
[   41.004747][    C1]  ? _raw_spin_unlock_irqrestore+0x5a/0x70
[   41.005337][    C1]  __hrtimer_run_queues+0x17f/0xa00
[   41.005859][    C1]  ? __pfx_dummy_timer+0x10/0x10
[   41.006358][    C1]  hrtimer_run_softirq+0x1c9/0x360
[   41.006872][    C1]  handle_softirqs+0x20e/0x9e0
[   41.007359][    C1]  __irq_exit_rcu+0x184/0x240
[   41.007829][    C1]  irq_exit_rcu+0x9/0x30
[   41.008262][    C1]  sysvec_apic_timer_interrupt+0xa8/0xc0
[   41.008823][    C1]  </IRQ>
[   41.009121][    C1]  <TASK>
[   41.009413][    C1]  asm_sysvec_apic_timer_interrupt+0x1a/0x20
[   41.010011][    C1] RIP: 0010:pv_native_safe_halt+0x1e/0x30
[   41.010581][    C1] Code: 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 0f 1f 44 00 00 eb 0c 0f 1f 44 00 00 0f 00 2d 29 f8 15 00 0f 1f 44 00 00 fb f4 <c3> cc cc cc cc 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 00 90 90 90 90
[   41.012330][    C1] RSP: 0018:ffffc9000047fdf8 EFLAGS: 00000202
[   41.012855][    C1] RAX: 0000000000011ba1 RBX: ffff8880404b0000 RCX: ffffffff8bd63249
[   41.013541][    C1] RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000000
[   41.014227][    C1] RBP: ffffed1008096000 R08: 0000000000000001 R09: ffffed100fdc6825
[   41.014906][    C1] R10: ffff88807ee3412b R11: 0000000000000001 R12: dffffc0000000000
[   41.015591][    C1] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
[   41.016279][    C1]  ? ct_kernel_exit+0x1d9/0x240
[   41.016705][    C1]  default_idle+0xe/0x20
[   41.017086][    C1]  default_idle_call+0x6c/0xb0
[   41.017505][    C1]  do_idle+0x3c1/0x5c0
[   41.017866][    C1]  ? __pfx_do_idle+0x10/0x10
[   41.018281][    C1]  ? _raw_spin_unlock_irq_enable+0x120/0x160
[   41.018806][    C1]  ? lockdep_hardirqs_on+0x7d/0x110
[   41.019272][    C1]  cpu_startup_entry+0x4f/0x60
[   41.019691][    C1]  start_secondary+0x21d/0x2d0
[   41.020122][    C1]  ? __pfx_start_secondary+0x10/0x10
[   41.020585][    C1]  common_startup_64+0x13e/0x158
[   41.021034][    C1]  </TASK>
[   41.021301][    C1] Modules linked in:
[   41.021644][    C1] ---[ end trace 0000000000000000 ]---

Take dev->lock with scoped_guard() around list_add(&req->list,
&dev->tx_reqs) on the copy_from_user() failure path.

Fixes: 25a010c8c1a5 ("USB: add Printer Gadget Driver")
Cc: stable@vger.kernel.org
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
Changes in v2:
- Use scoped_guard(spinlock_irqsave, &dev->lock) as requested by Greg.
- Clarify that dev->lock_printer_io does not serialize against tx_complete().

 drivers/usb/gadget/function/f_printer.c |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/gadget/function/f_printer.c b/drivers/usb/gadget/function/f_printer.c
index 10eeeed..2bf3e5b 100644
--- a/drivers/usb/gadget/function/f_printer.c
+++ b/drivers/usb/gadget/function/f_printer.c
@@ -665,7 +665,8 @@ printer_write(struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
 		spin_unlock_irqrestore(&dev->lock, flags);
 
 		if (copy_from_user(req->buf, buf, size)) {
-			list_add(&req->list, &dev->tx_reqs);
+			scoped_guard(spinlock_irqsave, &dev->lock)
+				list_add(&req->list, &dev->tx_reqs);
 			mutex_unlock(&dev->lock_printer_io);
 			return bytes_copied;
 		}

  reply	other threads:[~2026-12-03  0:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 13:58 [PATCH] usb: gadget: f_printer: take dev->lock on copy_from_user() failure Jaegyun Lee
2026-11-29 14:06 ` Greg Kroah-Hartman
2026-12-03  0:20   ` Jaegyun Lee [this message]
2026-12-03  0:26     ` [PATCH v2] " sashiko-bot
2026-11-29 14:08 ` [PATCH] " sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203002008.1704433-1-dreams200260@gmail.com \
    --to=dreams200260@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.