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 21:29:46 +0800 [thread overview]
Message-ID: <tencent_9FE25F20E92F65CD175FF65FC4094FFD4808@qq.com> (raw)
In-Reply-To: <179517710658.56988.15563657158904954302@qq.com>
rfcomm_sk_state_change() notifies the listener through bt_sk(sk)->parent
after bt_accept_unlink() has dropped the child's queue reference. The
accept queue never held a reference on the listener - bt_accept_enqueue()
only takes one on the child - so a concurrent close() of the listener can
reach rfcomm_sock_cleanup_listen() with an empty queue and free it before
the notification, leaving krfcommd to dereference freed memory in
parent->sk_data_ready().
Take a reference on the listener before the unlink and drop it after the
notification. Same bug class as 4020751fd0df ("Bluetooth: L2CAP: hold the
listener while notifying child teardown") and its ISO counterpart.
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.
Release 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.
Gate the put on the condition that took the reference rather than on
SOCK_ZAPPED alone: __rfcomm_sock_close() sets SOCK_ZAPPED directly for
BT_CONNECT/BT_CONNECT2/BT_CONFIG/BT_CONNECTED, so this tail can also run
when this invocation did not unlink the child (d->state != BT_CLOSED) and
never took a reference.
v3: move the sock_put() out of the notification block (Zhao Li), and gate
it on a local flag.
v4: resend - v3 went out with the diff only (no commit message and no
Signed-off-by), caught by bluez.test.bot.
Suggested-by: Zhao Li <enderaoelyther@gmail.com>
Signed-off-by: Hang Nan <2122295973@qq.com>
---
net/bluetooth/rfcomm/sock.c | 13 +++++++++++++
1 file changed, 13 insertions(+)
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);
}
}
--
2.47.3
next parent reply other threads:[~2026-11-20 13:29 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <179517710658.56988.15563657158904954302@qq.com>
2026-11-20 13:29 ` Hang Nan [this message]
2026-11-20 13:40 ` [PATCH v4] Bluetooth: RFCOMM: hold the listener across child teardown Hang Nan
2026-11-20 13:48 ` Hang Nan
2026-11-20 15:10 [PATCH v3] " Luiz Augusto von Dentz
2026-11-20 15:15 ` [PATCH v4] " 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_9FE25F20E92F65CD175FF65FC4094FFD4808@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