mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: syzbot <syzbot+bca09f5d8b843bbf7571@syzkaller.appspotmail.com>
To: linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com
Subject: Forwarded: [PATCH] usb: gadget: u_serial: fix use-after-free in release_tty
Date: Wed, 09 Sep 2026 13:29:20 -0700	[thread overview]
Message-ID: <6aa1c1a0.f2639fcc.29487d.000d.GAE@google.com> (raw)
In-Reply-To: <6a7a82f8.01d0871a.3a0d52.00b9.GAE@google.com>

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

***

Subject: [PATCH] usb: gadget: u_serial: fix use-after-free in release_tty
Author: adrianox@gmail.com

#syz test
gserial_free_line() frees the whole struct gs_port with kfree() once its
own open counter reaches zero. But the tty core keeps tty->port (which is
the embedded struct tty_port) for the lifetime of each tty; a concurrent
struct tty release can still dereference it from release_tty() after the
port has been freed, on a workqueue, racing the configfs teardown:

  release_tty -> tty->port->itty = NULL   (UAF)

Give the tty_port a proper reference count so it is only destroyed once
the last open tty releases it:
  . gs_install holds a tty_port_get() for every opened tty,
  . .cleanup drops it via tty_port_put(),
  . the port's ops->destruct kfree()s the enclosing gs_port, and
  . gserial_free_port just drops the base reference instead of kfree().
---
 drivers/usb/gadget/function/u_serial.c | 55 +++++++++++++++++++++++++-
 1 file changed, 53 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/function/u_serial.c b/drivers/usb/gadget/function/u_serial.c
index cdd1dfc666c4..5a78deea2904 100644
--- a/drivers/usb/gadget/function/u_serial.c
+++ b/drivers/usb/gadget/function/u_serial.c
@@ -908,7 +908,53 @@ static int gs_get_icount(struct tty_struct *tty,
 	return 0;
 }
 
+static void gs_port_destruct(struct tty_port *port)
+{
+	struct gs_port	*gs = container_of(port, struct gs_port, port);
+
+	kfree(gs);
+}
+
+static const struct tty_port_operations gs_port_ops = {
+	.destruct =		gs_port_destruct,
+};
+
+/*
+ * The tty core stores tty->port and later dereferences it from release_tty()
+ * on a workqueue, possibly after gserial_free_line() tore the gadget line
+ * down. Keep a reference to the port for the whole life of each tty so the
+ * embedded struct tty_port (and the enclosing gs_port) is only freed once no
+ * tty can reach it.
+ */
+static int gs_install(struct tty_driver *driver, struct tty_struct *tty)
+{
+	struct gs_port	*port;
+	int		status;
+
+	mutex_lock(&ports[tty->index].lock);
+	port = ports[tty->index].port;
+	if (!port) {
+		mutex_unlock(&ports[tty->index].lock);
+		return -ENODEV;
+	}
+	tty_port_get(&port->port);
+	mutex_unlock(&ports[tty->index].lock);
+
+	status = tty_port_install(&port->port, driver, tty);
+	if (status)
+		tty_port_put(&port->port);
+
+	return status;
+}
+
+static void gs_cleanup(struct tty_struct *tty)
+{
+	tty_port_put(tty->port);
+}
+
 static const struct tty_operations gs_tty_ops = {
+	.install =		gs_install,
+	.cleanup =		gs_cleanup,
 	.open =			gs_open,
 	.close =		gs_close,
 	.write =		gs_write,
@@ -1222,6 +1268,7 @@ gs_port_alloc(unsigned port_num, struct usb_cdc_line_coding *coding)
 	}
 
 	tty_port_init(&port->port);
+	port->port.ops = &gs_port_ops;
 	spin_lock_init(&port->port_lock);
 	init_waitqueue_head(&port->drain_wait);
 	init_waitqueue_head(&port->close_wait);
@@ -1258,8 +1305,12 @@ static void gserial_free_port(struct gs_port *port)
 	/* wait for old opens to finish */
 	wait_event(port->close_wait, gs_closed(port));
 	WARN_ON(port->port_usb != NULL);
-	tty_port_destroy(&port->port);
-	kfree(port);
+	/*
+	 * Drop the base reference. The tty port is only freed (via
+	 * gs_port_destruct) once the last open tty also released its
+	 * install-time reference in gs_cleanup().
+	 */
+	tty_port_put(&port->port);
 }
 
 void gserial_free_line(unsigned char port_num)
-- 
2.51.0


      parent reply	other threads:[~2026-09-09 20:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-11  2:03 [syzbot] [serial?] KASAN: slab-use-after-free Write " syzbot
2026-08-14  3:07 ` syzbot
2026-08-29 12:34 ` Forwarded: [PATCH] usb: gadget: u_serial: fix use-after-free of gs_port on concurrent open syzbot
2026-09-09 20:29 ` syzbot [this message]

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=6aa1c1a0.f2639fcc.29487d.000d.GAE@google.com \
    --to=syzbot+bca09f5d8b843bbf7571@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®