Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH] Bluetooth: RFCOMM: serialize RFCOMM_CONNINFO dlc->session dereference
@ 2026-11-24  3:21 Qican Ma
  2026-11-24  6:37 ` bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Qican Ma @ 2026-11-24  3:21 UTC (permalink / raw)
  To: marcel, luiz.dentz; +Cc: linux-bluetooth

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>
---
 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)

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

* RE: Bluetooth: RFCOMM: serialize RFCOMM_CONNINFO dlc->session dereference
  2026-11-24  3:21 [PATCH] Bluetooth: RFCOMM: serialize RFCOMM_CONNINFO dlc->session dereference Qican Ma
@ 2026-11-24  6:37 ` bluez.test.bot
  0 siblings, 0 replies; 2+ messages in thread
From: bluez.test.bot @ 2026-11-24  6:37 UTC (permalink / raw)
  To: linux-bluetooth, goonsomeway

[-- Attachment #1: Type: text/plain, Size: 857 bytes --]

This is an automated email and please do not reply to this email.

Dear Submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
While preparing the CI tests, the patches you submitted couldn't be applied to the current HEAD of the repository.

----- Output -----

error: patch fragment without header at line 8: @@ -786,20 +785,21 @@ static int rfcomm_sock_getsockopt_old(struct
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"

Please resolve the issue and submit the patches again.


---
Regards,
Linux Bluetooth


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

end of thread, other threads:[~2026-11-24  6:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24  3:21 [PATCH] Bluetooth: RFCOMM: serialize RFCOMM_CONNINFO dlc->session dereference Qican Ma
2026-11-24  6:37 ` 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