Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: RFCOMM: hold port reference during TTY registration
@ 2026-11-22 13:18 Haoli Zhang
  2026-11-22 17:22 ` bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Haoli Zhang @ 2026-11-22 13:18 UTC (permalink / raw)
  To: marcel, luiz.dentz
  Cc: linux-bluetooth, linux-kernel, nicoyip.dev, tim.bird,
	raghunathpalla.0209, peter, gregkh, jirislaby, 25031212117, byu,
	syzbot+11542ff1d167fdffa1fc

rfcomm_dev_add() registers the TTY after __rfcomm_dev_add() has made
the device visible through rfcomm_dev_list. A concurrent TTY open can
acquire a reference to the newly listed device while registration is
still in progress. For RFCOMM_RELEASE_ONHUP devices,
rfcomm_tty_install() can drop the initial tty_port reference. A
subsequent close can then free dev while rfcomm_dev_add() is still
using it.

Hold an extra tty_port reference across TTY registration and the
remaining initialization. If registration fails, drop the initial
reference only if the TTY has not already taken it, then drop the
registration reference. Save the device id before dropping the
registration reference so the return path does not access dev after it
may have been freed.

The DLC state callback can run before tty_dev is published. Publish
tty_dev with WRITE_ONCE() and use READ_ONCE() in the shutdown and
reparent paths. Skip reparenting while tty_dev is unavailable;
rfcomm_dev_add() calls the reparent helper again after publishing it.

Fixes: 5b899241874d ("Bluetooth: Release RFCOMM port when the last user closes the TTY")
Reported-by: syzbot+11542ff1d167fdffa1fc@syzkaller.appspotmail.com
Closes: https://syzbot.org/bug?extid=11542ff1d167fdffa1fc
Co-developed-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
Signed-off-by: Jinning Zhang <25031212117@stu.xidian.edu.cn>
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Haoli Zhang <26031212468@stu.xidian.edu.cn>
---
 net/bluetooth/rfcomm/tty.c | 42 ++++++++++++++++++++++++++++--------------
 1 file changed, 28 insertions(+), 14 deletions(-)
diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c
--- a/net/bluetooth/rfcomm/tty.c
+++ b/net/bluetooth/rfcomm/tty.c
@@ -127,9 +127,10 @@
 static void rfcomm_dev_shutdown(struct tty_port *port)
 {
 	struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
-
-	if (dev->tty_dev && dev->tty_dev->parent)
-		device_move(dev->tty_dev, NULL, DPM_ORDER_DEV_LAST);
+	struct device *tty_dev = READ_ONCE(dev->tty_dev);
+
+	if (tty_dev && tty_dev->parent)
+		device_move(tty_dev, NULL, DPM_ORDER_DEV_LAST);
 
 	/* close the dlc */
 	rfcomm_dlc_close(dev->dlc, 0);
@@ -173,6 +174,11 @@
 {
 	struct hci_dev *hdev;
 	struct hci_conn *conn;
+	struct device *tty_dev = READ_ONCE(dev->tty_dev);
+
+	/* The connection can become active before TTY registration completes. */
+	if (!tty_dev)
+		return;
 
 	hdev = hci_get_route(&dev->dst, &dev->src, BDADDR_BREDR);
 	if (!hdev)
@@ -188,7 +194,7 @@
 	 * guarantee the sysfs device has been added ...
 	 */
 	if (conn && device_is_registered(&conn->dev))
-		device_move(dev->tty_dev, &conn->dev, DPM_ORDER_DEV_AFTER_PARENT);
+		device_move(tty_dev, &conn->dev, DPM_ORDER_DEV_AFTER_PARENT);
 
 	hci_dev_unlock(hdev);
 	hci_dev_put(hdev);
@@ -267,6 +273,8 @@
 		((1 << RFCOMM_RELEASE_ONHUP) | (1 << RFCOMM_REUSE_DLC));
 
 	tty_port_init(&dev->port);
+	/* Keep a reference until rfcomm_dev_add() completes TTY registration. */
+	tty_port_get(&dev->port);
 	dev->port.ops = &rfcomm_port_ops;
 
 	skb_queue_head_init(&dev->pending);
@@ -316,6 +324,7 @@
 {
 	struct rfcomm_dev *dev;
 	struct device *tty;
+	int err, id;
 
 	BT_DBG("id %d channel %d", req->dev_id, req->channel);
 
@@ -325,24 +334,29 @@
 		return PTR_ERR(dev);
 	}
 
+	id = dev->id;
 	tty = tty_port_register_device(&dev->port, rfcomm_tty_driver,
 			dev->id, NULL);
 	if (IS_ERR(tty)) {
-		tty_port_put(&dev->port);
-		return PTR_ERR(tty);
-	}
-
-	dev->tty_dev = tty;
+		err = PTR_ERR(tty);
+		if (!test_and_set_bit(RFCOMM_TTY_OWNED, &dev->status))
+			tty_port_put(&dev->port); /* Drop the initial reference. */
+		tty_port_put(&dev->port); /* Drop the registration reference. */
+		return err;
+	}
+
+	WRITE_ONCE(dev->tty_dev, tty);
 	rfcomm_reparent_device(dev);
-	dev_set_drvdata(dev->tty_dev, dev);
-
-	if (device_create_file(dev->tty_dev, &dev_attr_address) < 0)
+	dev_set_drvdata(tty, dev);
+
+	if (device_create_file(tty, &dev_attr_address) < 0)
 		BT_ERR("Failed to create address attribute");
 
-	if (device_create_file(dev->tty_dev, &dev_attr_channel) < 0)
+	if (device_create_file(tty, &dev_attr_channel) < 0)
 		BT_ERR("Failed to create channel attribute");
 
-	return dev->id;
+	tty_port_put(&dev->port); /* Drop the registration reference. */
+	return id;
 }
 
 /* ---- Send buffer ---- */


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-22 17:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-22 13:18 [PATCH] Bluetooth: RFCOMM: hold port reference during TTY registration Haoli Zhang
2026-11-22 17:22 ` bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox