All of lore.kernel.org
 help / color / mirror / Atom feed
From: syzbot <syzbot+8496ab5e117502750445@syzkaller.appspotmail.com>
To: linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com
Subject: Forwarded: Re: WARNING: refcount bug in trace_suspend_resume (2)
Date: Mon, 10 Aug 2026 21:49:10 -0700	[thread overview]
Message-ID: <6a7aa9c6.01d0871a.3a0d52.00bd.GAE@google.com> (raw)
In-Reply-To: <6a78d160.01d0871a.3a0d52.0090.GAE@google.com>

For archival purposes, forwarding an incoming command email to
linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com.

***

Subject: Re: WARNING: refcount bug in trace_suspend_resume (2)
Author: khiemtranzo532001@gmail.com

#syz test: https://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb.git usb-next

From dc76c5bf35d3e11a56c1402294d11297165fec21 Mon Sep 17 00:00:00 2001
From: Nguyen Quang Le Kien <khiemtranzo532001@gmail.com>
Date: Tue, 11 Aug 2026 12:46:21 +0800
Subject: [PATCH] usb: gadget: fix refcount race in gadget_dev_open()

gadget_dev_open() accesses dev->lock and calls get_dev() without
holding sb_mutex, which is held by gadgetfs_kill_sb() when it calls
put_dev(the_device) and frees the device.  This creates a race where
open() reads dev->lock after kill_sb() has already freed the dev_data
object, resulting in a refcount underflow and use-after-free.

Fix by wrapping gadget_dev_open() with scoped_guard(mutex, &sb_mutex)
to serialise against gadgetfs_kill_sb(), and check the_device inside
the guard before dereferencing dev.

Reported-by: syzbot+8496ab5e117502750445@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=8496ab5e117502750445
Signed-off-by: Nguyen Quang Le Kien <khiemtranzo532001@gmail.com>
---
 drivers/usb/gadget/legacy/inode.c | 20 ++++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/drivers/usb/gadget/legacy/inode.c b/drivers/usb/gadget/legacy/inode.c
index d87a8ab51..719ea5c18 100644
--- a/drivers/usb/gadget/legacy/inode.c
+++ b/drivers/usb/gadget/legacy/inode.c
@@ -1916,15 +1916,19 @@ gadget_dev_open (struct inode *inode, struct file *fd)
 	struct dev_data		*dev = inode->i_private;
 	int			value = -EBUSY;
 
-	spin_lock_irq(&dev->lock);
-	if (dev->state == STATE_DEV_DISABLED) {
-		dev->ev_next = 0;
-		dev->state = STATE_DEV_OPENED;
-		fd->private_data = dev;
-		get_dev (dev);
-		value = 0;
+	scoped_guard(mutex, &sb_mutex) {
+		if (!the_device)
+			return -ENODEV;
+		spin_lock_irq(&dev->lock);
+		if (dev->state == STATE_DEV_DISABLED) {
+			dev->ev_next = 0;
+			dev->state = STATE_DEV_OPENED;
+			fd->private_data = dev;
+			get_dev (dev);
+			value = 0;
+		}
+		spin_unlock_irq(&dev->lock);
 	}
-	spin_unlock_irq(&dev->lock);
 	return value;
 }
 
-- 
2.34.1

  reply	other threads:[~2026-08-11  4:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-09 19:13 [syzbot] [usb?] WARNING: refcount bug in trace_suspend_resume (2) syzbot
2026-08-11  4:49 ` syzbot [this message]
2026-08-11  5:21 ` Forwarded: " syzbot

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=6a7aa9c6.01d0871a.3a0d52.00bd.GAE@google.com \
    --to=syzbot+8496ab5e117502750445@syzkaller.appspotmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=syzkaller-bugs@googlegroups.com \
    /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.