From: Hang Nan <2122295973@qq.com>
To: linux-bluetooth@vger.kernel.org
Cc: marcel@holtmann.org, luiz.dentz@gmail.com,
luiz.von.dentz@intel.com, pav@iki.fi, zzzccc427@gmail.com,
enderaoelyther@gmail.com
Subject: [PATCH v4] Bluetooth: RFCOMM: hold the listener across child teardown
Date: Fri, 20 Nov 2026 23:15:13 +0800 [thread overview]
Message-ID: <tencent_F776F667C4504F55486A5FFFE91A655E1708@qq.com> (raw)
In-Reply-To: <CABBYNZJWXoTxVYKEhQfZ82-P=8ogS-yBrMPR1sNxo3C6yQG-2g@mail.gmail.com>
Bluetooth: RFCOMM: hold the listener across child teardown
rfcomm_sk_state_change() takes the listener from bt_sk(sk)->parent,
unlinks a closing child from the accept queue and then notifies the
listener through that pointer. bt_accept_enqueue() retains only the
child, so once bt_accept_unlink() has dropped the child's queue reference
a concurrent close() of the listener can run rfcomm_sock_cleanup_listen(),
find an empty queue, and free the listener:
krfcommd close(listener)
rfcomm_sk_state_change()
parent = bt_sk(sk)->parent
bt_accept_unlink(sk)
cleanup_listen(): queue empty
rfcomm_sock_kill() -> kfree
parent->sk_data_ready(parent) <- use-after-free
KASAN reports it as an 8-byte read 264 bytes into a freed kmalloc-1k
object, i.e. the listener sock:
BUG: KASAN: slab-use-after-free in rfcomm_sk_state_change+0x2ed/0x320
Read of size 8 at addr ffff888002716908 by task krfcommd/60
CPU: 1 PID: 60 Comm: krfcommd Tainted: G B PREEMPT(full)
rfcomm_sk_state_change+0x2ed/0x320
__rfcomm_dlc_close+0x16a/0x3d0
rfcomm_run+0x1824/0x4000
The window needs full preemption: the only preemption point is the
spin_unlock_bh() at the tail of bt_accept_unlink(). It was reproduced on
7.2-rc6 with two vhci controllers, one side listening and closing, the
other connecting and dropping the child, with an mdelay(3) inserted
between the unlink and the notification for the test (full report below
the --- line).
Same class as commit 4020751fd0df ("Bluetooth: L2CAP: Hold the listener
while notifying child teardown") and commit b4a00dedc5cc ("Bluetooth: ISO:
Hold the listener during disconnect notification"): take a reference
before the unlink and drop it after the notification.
The reference cannot be dropped where it is taken. Once
bt_accept_unlink() has run, close() may have released the fd reference, so
the sock_put() can be the final put and runs rfcomm_sock_destruct() ->
rfcomm_dlc_lock(listener->dlc) while the child's socket lock and the
child's DLC mutex are still held. All DLC mutexes share one lockdep class
(rfcomm_dlc_alloc() does a plain mutex_init()) and this function already
runs under d->lock and takes lock_sock(sk), so that nests a same-class
mutex and inverts the d->lock -> sk_lock order.
Drop it in the existing SOCK_ZAPPED tail instead, where the child's socket
lock has been released and its DLC lock is dropped for
rfcomm_sock_kill() - the same place the code already drops the DLC lock so
that the child's own destructor does not deadlock. The put is gated on
the flag set where the reference is taken rather than on SOCK_ZAPPED
alone, because __rfcomm_sock_close() sets SOCK_ZAPPED directly for
BT_CONNECT, BT_CONNECT2, BT_CONFIG and BT_CONNECTED.
Cc: stable@vger.kernel.org
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Hang Nan <2122295973@qq.com>
---
v4: add the commit description, Signed-off-by, Fixes tag and the KASAN
report; v3 went out as a bare diff.
v3: drop the reference in the SOCK_ZAPPED tail and gate it on a flag, so
that a final put cannot run rfcomm_sock_destruct() under the child's
sk_lock and d->lock (Zhao Li).
v2: cite the two reference commits as 'commit <sha> ("<title>")', as
checkpatch.pl requires.
Full report (the mdelay(3) only widens the window; the code path is
unchanged):
BUG: KASAN: slab-use-after-free in rfcomm_sk_state_change+0x2ed/0x320
Read of size 8 at addr ffff888002716908 by task krfcommd/60
CPU: 1 PID: 60 Comm: krfcommd Tainted: G B PREEMPT(full)
rfcomm_sk_state_change+0x2ed/0x320
__rfcomm_dlc_close+0x16a/0x3d0
rfcomm_run+0x1824/0x4000
kthread
Allocated by task 72:
rfcomm_sock_alloc.constprop.0+0x47/0x240
rfcomm_sock_create+0xa6/0x2a0
__sys_socket+0x116/0x1d0
Freed by task 72:
__sk_destruct+0x499/0x5d0
rfcomm_sock_release+0x101/0x140
__sock_release+0xa1/0x260
__x64_sys_close+0x78/0xd0
The buggy address is located 264 bytes inside of a freed 1024-byte region
(kmalloc-1k).
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 3fb8b5474948..94a495e86057 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -66,6 +66,7 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
__must_hold(&d->lock)
{
struct sock *sk = d->owner, *parent;
+ bool held = false;
if (!sk)
return;
@@ -83,6 +84,12 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
if (parent) {
if (d->state == BT_CLOSED) {
sock_set_flag(sk, SOCK_ZAPPED);
+ /* close() may already have dropped its reference and the
+ * queue holds none, so keep the listener alive across the
+ * unlink and the notification.
+ */
+ sock_hold(parent);
+ held = true;
bt_accept_unlink(sk);
}
parent->sk_data_ready(parent);
@@ -100,6 +107,12 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
* rfcomm_sock_destruct() will dead lock. */
rfcomm_dlc_unlock(d);
rfcomm_sock_kill(sk);
+ /* Drop the listener reference with neither the child's socket
+ * lock nor its DLC lock held: this put can be the final one and
+ * rfcomm_sock_destruct() takes the listener's own DLC mutex.
+ */
+ if (held)
+ sock_put(parent);
rfcomm_dlc_lock(d);
}
}
next prev parent reply other threads:[~2026-11-20 15:15 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20261115191908.3646-enderaoelyther@gmail.com>
2026-11-20 12:18 ` [PATCH v3] Bluetooth: RFCOMM: hold the listener across child teardown Hang Nan
2026-11-20 13:28 ` [v3] " bluez.test.bot
2026-11-20 15:10 ` [PATCH v3] " Luiz Augusto von Dentz
2026-11-20 15:15 ` Hang Nan [this message]
[not found] <179517710658.56988.15563657158904954302@qq.com>
2026-11-20 13:29 ` [PATCH v4] " Hang Nan
2026-11-20 13:40 ` Hang Nan
2026-11-20 13:48 ` Hang Nan
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=tencent_F776F667C4504F55486A5FFFE91A655E1708@qq.com \
--to=2122295973@qq.com \
--cc=enderaoelyther@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=luiz.von.dentz@intel.com \
--cc=marcel@holtmann.org \
--cc=pav@iki.fi \
--cc=zzzccc427@gmail.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