From: Qican Ma <goonsomeway@gmail.com>
To: marcel@holtmann.org, luiz.dentz@gmail.com
Cc: linux-bluetooth@vger.kernel.org
Subject: [PATCH v2] Bluetooth: RFCOMM: serialize RFCOMM_CONNINFO dlc->session dereference
Date: Tue, 24 Nov 2026 15:53:55 +0800 [thread overview]
Message-ID: <179550683515.38654.13328780695859519079@gmail.com> (raw)
rfcomm_sock_getsockopt_old() checks rfcomm_pi(sk)->dlc->session and then
dereferences dlc->session->sock->sk while holding only lock_sock(sk).
rfcomm_dlc_unlink() clears d->session and rfcomm_session_del() frees the
session under rfcomm_mutex without taking the socket lock, leaving a
check-then-dereference race against concurrent session teardown.
Commit 46f8ffd0a1f1 ("Bluetooth: RFCOMM: fix NULL dereference of
dlc->session in RFCOMM_CONNINFO") added the NULL check for the
post-disconnect case on deferred-setup sockets, but did not serialize
the dereference against concurrent unlink and kfree().
Add rfcomm_dlc_get_conninfo() to read the underlying L2CAP connection
fields under rfcomm_lock(). Hold a reference on the DLC and drop the
socket lock around the helper call to preserve the established
rfcomm_mutex -> lock_sock(sk) lock ordering used by krfcommd.
Found with an LLM-assisted code review tool and verified by inspection on
v7.4-rc4 (b6a9fa34ce4). Build-tested only; no runtime reproducer was
constructed for the narrow disconnect race window.
Fixes: bb23c0ab8246 ("Bluetooth: Add support for deferring RFCOMM connection setup")
Fixes: 46f8ffd0a1f1 ("Bluetooth: RFCOMM: fix NULL dereference of dlc->session in RFCOMM_CONNINFO")
Cc: stable@vger.kernel.org
Signed-off-by: Qican Ma <goonsomeway@gmail.com>
---
v2: resend via direct SMTP without line-wrapping corruption in the diff.
include/net/bluetooth/rfcomm.h | 2 ++
net/bluetooth/rfcomm/core.c | 23 +++++++++++++++++++++++
net/bluetooth/rfcomm/sock.c | 24 ++++++++++++------------
3 files changed, 37 insertions(+), 12 deletions(-)
diff --git a/include/net/bluetooth/rfcomm.h b/include/net/bluetooth/rfcomm.h
index 9dc5fd4cb8c..2e2cd873dd3 100644
--- a/include/net/bluetooth/rfcomm.h
+++ b/include/net/bluetooth/rfcomm.h
@@ -290,6 +290,8 @@ struct rfcomm_conninfo {
__u8 dev_class[3];
};
+int rfcomm_dlc_get_conninfo(struct rfcomm_dlc *d, struct rfcomm_conninfo *ci);
+
#define RFCOMM_LM 0x03
#define RFCOMM_LM_MASTER 0x0001
#define RFCOMM_LM_AUTH 0x0002
diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index 8a4205347c4..e6af6f5f591 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -1427,6 +1427,29 @@ void rfcomm_dlc_accept(struct rfcomm_dlc *d)
rfcomm_unlock();
}
+int rfcomm_dlc_get_conninfo(struct rfcomm_dlc *d, struct rfcomm_conninfo *ci)
+{
+ struct l2cap_conn *conn;
+ struct sock *l2cap_sk;
+ int err = 0;
+
+ rfcomm_lock();
+
+ if (!d->session) {
+ err = -ENOTCONN;
+ } else {
+ l2cap_sk = d->session->sock->sk;
+ conn = l2cap_pi(l2cap_sk)->chan->conn;
+
+ memset(ci, 0, sizeof(*ci));
+ ci->hci_handle = conn->hcon->handle;
+ memcpy(ci->dev_class, conn->hcon->dev_class, 3);
+ }
+
+ rfcomm_unlock();
+ return err;
+}
+
static void rfcomm_check_accept(struct rfcomm_dlc *d)
{
if (rfcomm_check_security(d)) {
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 3fb8b547494..e91a129a67b 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -741,9 +741,8 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname,
sockopt_t *sopt)
{
struct sock *sk = sock->sk;
- struct sock *l2cap_sk;
- struct l2cap_conn *conn;
struct rfcomm_conninfo cinfo;
+ struct rfcomm_dlc *d;
int err = 0;
size_t len;
u32 opt;
@@ -786,20 +785,21 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname,
break;
case RFCOMM_CONNINFO:
- if ((sk->sk_state != BT_CONNECTED &&
- !(sk->sk_state == BT_CONNECT2 &&
- rfcomm_pi(sk)->dlc->defer_setup)) ||
- !rfcomm_pi(sk)->dlc->session) {
+ if (sk->sk_state != BT_CONNECTED &&
+ !(sk->sk_state == BT_CONNECT2 &&
+ rfcomm_pi(sk)->dlc->defer_setup)) {
err = -ENOTCONN;
break;
}
- l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
- conn = l2cap_pi(l2cap_sk)->chan->conn;
-
- memset(&cinfo, 0, sizeof(cinfo));
- cinfo.hci_handle = conn->hcon->handle;
- memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
+ d = rfcomm_pi(sk)->dlc;
+ rfcomm_dlc_hold(d);
+ release_sock(sk);
+ err = rfcomm_dlc_get_conninfo(d, &cinfo);
+ rfcomm_dlc_put(d);
+ lock_sock(sk);
+ if (err)
+ break;
len = min(len, sizeof(cinfo));
if (copy_to_iter(&cinfo, len, &sopt->iter_out) != len)
--
2.50.1 (Apple Git-155)
reply other threads:[~2026-11-24 7:53 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=179550683515.38654.13328780695859519079@gmail.com \
--to=goonsomeway@gmail.com \
--cc=linux-bluetooth@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.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