From: Lorenzo Ferrari <loreferro01@gmail.com>
To: Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
Marcel Holtmann <marcel@holtmann.org>
Cc: linux-bluetooth@vger.kernel.org,
Lorenzo Ferrari <loreferro01@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH] Bluetooth: RFCOMM: Serialize tty unparenting with hci_conn_del_sysfs()
Date: Mon, 30 Nov 2026 11:35:29 +0100 [thread overview]
Message-ID: <20261130103529.65147-1-loreferro01@gmail.com> (raw)
In-Reply-To: <20261117185737.3758255-17-luiz.dentz@gmail.com>
An RFCOMM tty device is reparented under the hci_conn device of its peer
by rfcomm_reparent_device(), which calls device_move() with hdev->lock
held. When the ACL link goes away, hci_conn_del_sysfs(), also under
hdev->lock, moves the rfcomm tty children of the connection to no
parent. Two other paths touch the tty's parent without that lock:
- rfcomm_dev_shutdown() moves the tty to no parent itself;
- rfcomm_dev_destruct() unregisters the tty device. device_del() reads
dev->parent once on entry and later calls
klist_del(&dev->p->knode_parent) for that parent.
If hci_conn_del_sysfs() moves the tty while device_del() is between
those two points, the node has already been removed from the parent's
klist and its klist pointer cleared, and device_del() unlinks it again:
RFCOMMRELEASEDEV HCI Disconnection Complete
device_del(tty)
parent = dev->parent (hci0:1)
hci_conn_del_sysfs()
device_move(tty, NULL)
klist_remove(knode_parent)
klist_del(&dev->p->knode_parent)
klist_put(): knode_klist() is NULL
KASAN: null-ptr-deref in range [0x0000000000000058-0x000000000000005f]
RIP: 0010:klist_put+0x47/0x160
Call Trace:
device_del+0x1ee/0xc90
device_destroy+0x8a/0xd0
tty_unregister_device+0x79/0x1c0
rfcomm_dev_destruct+0x11a/0x280
rfcomm_dev_ioctl+0x1b7/0x1a70
Commit 9b851b09b392 ("Bluetooth: hci_sysfs: Fix NULL pointer dereference
in device_del()") removed the same race for BNEP network devices by
limiting hci_conn_del_sysfs() to rfcomm ttys again; for the ttys it is
still there.
Detach the tty from its parent under hdev->lock, found with the same
route lookup as rfcomm_reparent_device(), in rfcomm_dev_shutdown() and
before tty_unregister_device() in rfcomm_dev_destruct(). Every move of
the tty and its unregistration are then serialized with
hci_conn_del_sysfs(): either it has already moved the tty, or the tty is
no longer a child of the connection when it looks for it, and
device_del() always runs with no parent.
Reproduced on bluetooth.git (77bc0f253ff9) with a vhci controller: an
L2CAP connect brings up an ACL link, RFCOMMCREATEDEV creates a tty that
is reparented under hci0:1, and RFCOMMRELEASEDEV races an HCI
Disconnection Complete event. The window is narrow, so it was widened
with a 200 ms msleep() in device_del() for rfcomm devices; with it the
first round crashes as above. With this patch, with and without the
delay, 100 rounds of each ordering, 100 rounds that also open and close
the tty (rfcomm_dev_shutdown()) and the syzbot BNEP reproducer ran with
no KASAN or lockdep report.
The race was analyzed with the help of an AI
assistant (Claude); the reproducer and the tests above were run locally.
Fixes: acea6852f32b ("[BLUETOOTH]: Move children of connection device to NULL before connection down.")
Cc: stable@vger.kernel.org
Signed-off-by: Lorenzo Ferrari <loreferro01@gmail.com>
---
This overlaps with "[PATCH v1 16/16] Bluetooth: RFCOMM: Serialize tty
device reparenting", which was not applied with the rest of the series.
An RFCOMM-local mutex serializes shutdown with reparent, but the tty is
also moved by hci_conn_del_sysfs() under hdev->lock and unregistered by
rfcomm_dev_destruct() with no lock held. Taking hdev->lock, which
reparent and hci_conn_del_sysfs() already hold, covers all of these
paths, so this could replace 16/16. I can share the reproducer if useful.
net/bluetooth/rfcomm/tty.c | 39 ++++++++++++++++++++++++++++++++++----
1 file changed, 35 insertions(+), 4 deletions(-)
diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c
index 745cf1e4bb..98ae59a7a4 100644
--- a/net/bluetooth/rfcomm/tty.c
+++ b/net/bluetooth/rfcomm/tty.c
@@ -74,6 +74,37 @@ static void rfcomm_dev_modem_status(struct rfcomm_dlc *dlc, u8 v24_sig);
/* ---- Device functions ---- */
+/* Detach the tty device from the hci_conn device it was reparented under.
+ *
+ * hci_conn_del_sysfs() moves the rfcomm tty children of a connection that
+ * goes away to no parent, under hdev->lock, and rfcomm_reparent_device()
+ * moves the tty under a connection with the same lock held. Any other move,
+ * and the unregistration of the tty device, must be serialized with those:
+ * device_del() reads dev->parent once and later unlinks the device from that
+ * parent's klist, so a concurrent device_move() makes it unlink a node that
+ * is no longer on any list.
+ */
+static void rfcomm_dev_unparent(struct rfcomm_dev *dev)
+{
+ struct device *tty_dev = READ_ONCE(dev->tty_dev);
+ struct hci_dev *hdev;
+
+ if (!tty_dev)
+ return;
+
+ hdev = hci_get_route(&dev->dst, &dev->src, BDADDR_BREDR);
+ if (hdev)
+ hci_dev_lock(hdev);
+
+ if (tty_dev->parent)
+ device_move(tty_dev, NULL, DPM_ORDER_DEV_LAST);
+
+ if (hdev) {
+ hci_dev_unlock(hdev);
+ hci_dev_put(hdev);
+ }
+}
+
static void rfcomm_dev_destruct(struct tty_port *port)
{
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
@@ -89,8 +120,10 @@ static void rfcomm_dev_destruct(struct tty_port *port)
rfcomm_dlc_put(dlc);
- if (dev->tty_dev)
+ if (dev->tty_dev) {
+ rfcomm_dev_unparent(dev);
tty_unregister_device(rfcomm_tty_driver, dev->id);
+ }
mutex_lock(&rfcomm_dev_lock);
list_del(&dev->list);
@@ -127,10 +160,8 @@ static bool rfcomm_dev_carrier_raised(struct tty_port *port)
static void rfcomm_dev_shutdown(struct tty_port *port)
{
struct rfcomm_dev *dev = container_of(port, struct rfcomm_dev, port);
- struct device *tty_dev = READ_ONCE(dev->tty_dev);
- if (tty_dev && tty_dev->parent)
- device_move(tty_dev, NULL, DPM_ORDER_DEV_LAST);
+ rfcomm_dev_unparent(dev);
/* close the dlc */
rfcomm_dlc_close(dev->dlc, 0);
--
2.54.0
prev parent reply other threads:[~2026-11-30 10:36 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 18:57 [PATCH v1 00/16] Bluetooth: Fixes for issues reported by sashiko.dev Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 01/16] Bluetooth: mgmt: Fix read_index_list() buffer overflow Luiz Augusto von Dentz
2026-11-17 20:13 ` Bluetooth: Fixes for issues reported by sashiko.dev bluez.test.bot
2026-11-17 18:57 ` [PATCH v1 02/16] Bluetooth: hci_core: Take req_sync lock in hci_power_on() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 03/16] Bluetooth: hci_sync: Lock the lookup in hci_disable_ext_adv_instance_sync() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 04/16] Bluetooth: hci_sync: Hold hdev->lock in hci_schedule_adv_instance_sync() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 05/16] Bluetooth: SMP: Serialize link key updates from smp_notify_keys() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 06/16] Bluetooth: SMP: Check smp_data before using SMP_DEV() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 07/16] Bluetooth: SMP: Don't create the BR/EDR channel while unregistered Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 08/16] Bluetooth: hci_event: Validate sent command length in LE Set Ext Adv Enable Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 09/16] Bluetooth: mgmt: Reject zero multi advertising rotation duration Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 10/16] Bluetooth: L2CAP: Don't use uninitialized EFS in l2cap_parse_conf_rsp() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 11/16] Bluetooth: mgmt: Fix mesh transmission locking and lifetime Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 12/16] Bluetooth: MGMT: Fix use-after-free in mgmt_powering_down() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 13/16] Bluetooth: MSFT: Protect the event prefix with hdev->lock Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 14/16] Bluetooth: hci_debugfs: Fix racy toggling of debugfs controlled flags Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 15/16] Bluetooth: hci_core: Hold hdev->lock when removing advertisement monitors Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 16/16] Bluetooth: RFCOMM: Serialize tty device reparenting Luiz Augusto von Dentz
2026-11-30 10:35 ` Lorenzo Ferrari [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=20261130103529.65147-1-loreferro01@gmail.com \
--to=loreferro01@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox