From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795961221; b=ah/NdLl+AaYNa6QBLL36wO+ECoeS5gE6jQ9b2flABND7geDDT75JlGoM9c8ZgLb34oWi fm2BefkQcGjQ+erMJmLFYwmJQNhgcqOfmA+3G+xQTmdSdM6rS16gZuOkUGDEnK1rnmwAF aOnRZ98j6biMbH4Rwp7M333f30F3g60gWyTbCXp2ifBlsDJqClGgS2s1OPKrPEbi5JGj0 pBuEw+yvUPOxmwFq5gt8tB/56BRHgIIFTs3WzX16C6g+Eb3yTjTsCah9AASO9okrqFmhK 9G/Zwduliq+mfKwQOAa0yPHKcWsZsy0L5Rx5kjJTnXvWIiAWuOrZvjxcAtFsBIa6URg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795961221; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=MrSbZYFLVLz3EG3uEQMV23LCW9CQ2AW01ailPLejfr4=; b=JODV5g2GgeFYWEy8+qzANedKZWh6Kw3g3lmdSzuvyupwQ/HMkoiGANJB42QklEVYT05i naEe5Gq0uzT9T7WdIBt4LVLjjemeQ0kbr7o4UESFBDcu+avdneChmJsNf8MK0uwekM3Az ksgSMjM41Qfku6wZJLqACEeDyBWiPFOuyPHJ+1tPGqVm3FsUsEKIj6fRBq+Du5Fa+GjDU 3xF76wQMWvY3CwoxcrXBt7UpRpl4YzhtPOKmkv1+cawy2oSsOCIuNh80hzgq3IEZy3XbF mY9REM+nziNndT4ZwSe32PuY1WJtpgQ6JlqicH9m/GBk8RN0ZbBW6g3m+5F/HLYeJ1g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=J3EYZrRu; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 33DDF606A31C; Sun, 29 Nov 2026 14:07:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=J3EYZrRu Received: by smtp.kernel.org (Postfix) with ESMTPSA id 551DF1F000C1; Sun, 29 Nov 2026 14:06:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795961220; bh=MrSbZYFLVLz3EG3uEQMV23LCW9CQ2AW01ailPLejfr4=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=J3EYZrRub5ApOBxPSAR6Fp5QcrjuQmD0nVOV+GrMeCpzPLq2KnMEUFKgeZgWg00aH uTF688UjD8UsivDyQlTUiPE80xoGW7YVUTlEB/PC7rR3WfzmohZtIP4rR75QFqbwge 4hJy7LWfGoAOn7ND11f9Xiv1WQPZF0Ha9R5pKD9w= Date: Sun, 29 Nov 2026 15:06:55 +0100 From: Greg Kroah-Hartman To: Jaegyun Lee Cc: Neill Kapron , linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH] usb: gadget: f_printer: take dev->lock on copy_from_user() failure Message-ID: <2026112917-starship-petty-26cf@gregkh> References: <20261129135824.450263-1-dreams200260@gmail.com> X-Mailing-List: linux-usb@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261129135824.450263-1-dreams200260@gmail.com> On Sun, Nov 29, 2026 at 10:58:24PM +0900, Jaegyun Lee wrote: > printer_write() drops dev->lock around copy_from_user(). When the > copy fails it puts the request back on dev->tx_reqs without taking > the lock again, racing with tx_complete(), which adds completed > requests to the same list under dev->lock from the UDC giveback. > > Reproduced: a printer function where one gadget thread writes 64 KiB > buffers to /dev/g_printer0 and another writes from a PROT_NONE buffer, > while the host drains bulk IN: > > [ 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] > [ 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] > [ 41.009121][ C1] > [ 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 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] > [ 41.021301][ C1] Modules linked in: > [ 41.021644][ C1] ---[ end trace 0000000000000000 ]--- > > sashiko pointed this out in its review of "usb: gadget: f_printer: Fix > fsync waiting for active transfers". > > Take dev->lock around the list_add() on the failure path. > > Fixes: 25a010c8c1a5 ("USB: add Printer Gadget Driver") > Link: https://lore.kernel.org/all/sashiko-outbox-206117@kernel.org/ > Cc: stable@vger.kernel.org > Assisted-by: ZCode:GLM-5.4 > Signed-off-by: Jaegyun Lee > --- > drivers/usb/gadget/function/f_printer.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/drivers/usb/gadget/function/f_printer.c b/drivers/usb/gadget/function/f_printer.c > --- a/drivers/usb/gadget/function/f_printer.c > +++ b/drivers/usb/gadget/function/f_printer.c > @@ -665,7 +665,9 @@ printer_write(struct file *fd, const cha > spin_unlock_irqrestore(&dev->lock, flags); > > if (copy_from_user(req->buf, buf, size)) { > + spin_lock_irqsave(&dev->lock, flags); > list_add(&req->list, &dev->tx_reqs); > + spin_unlock_irqrestore(&dev->lock, flags); > mutex_unlock(&dev->lock_printer_io); A spinlock and a mutex? Ugh, are you sure that is right? And if so, please use a guard, and test this? thanks, greg k-h