Linux bluetooth development
 help / color / mirror / Atom feed
From: Henry Martin <bsdhenrymartin@gmail.com>
To: Marcel Holtmann <marcel@holtmann.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
	Juan Perdomo <jcperdomo100@gmail.com>,
	Breno Leitao <leitao@debian.org>,
	Zhang Cen <rollkingzzc@gmail.com>, Siwei Zhang <oss@fourdim.xyz>,
	Maxim Skokov <skokovmaksimevg@gmail.com>,
	Pauli Virtanen <pav@iki.fi>,
	safa.karakus@secunnix.com, Hui Peng <benquike@gmail.com>,
	Peng Fan <peng.fan@nxp.com>, Tim Bird <tim.bird@sony.com>,
	Chengfeng Ye <nicoyip.dev@gmail.com>,
	aliamemis@disroot.org, Pengpeng Hou <pengpeng@iscas.ac.cn>,
	Hyunwoo Kim <imv4bel@gmail.com>,
	Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>,
	SeungJu Cheon <suunj1331@gmail.com>
Cc: linux-bluetooth@vger.kernel.org,
	Henry Martin <bsdhenrymartin@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH] Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt
Date: Fri, 20 Nov 2026 14:33:43 +0800	[thread overview]
Message-ID: <20261120063343.3875317-1-bsdhenrymartin@gmail.com> (raw)

The RFCOMM_CONNINFO getsockopt handler checks dlc->session under
lock_sock() and then follows it:

	l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;

Nothing serializes this against rfcomm_session_close(). The session
thread unlinks every dlc with __rfcomm_dlc_close() - which sets
d->session = NULL - and then frees the session in rfcomm_session_del(),
all under rfcomm_mutex and without ever touching lock_sock.

An ioctl(RFCOMMCREATEDEV) with REUSE_DLC replaces the dlc state_change
callback with rfcomm_dev_state_change(), which does not take
lock_sock(), and sets sk_state to BT_CLOSED only after rfcomm_dev_add()
returns. During that window every check in the CONNINFO branch passes
while a full-session shutdown from the peer unlinks and frees the
session. The check and the dereference then lose the race:

  KASAN: slab-use-after-free in rfcomm_sock_getsockopt
  Read of size 8 (session->sock, offset 0x10)
  Allocated by rfcomm_session_add, freed by rfcomm_session_close

or, if the session is freed between the check and the dereference, the
dereference observes a cleared session->sock->sk and faults on a NULL
l2cap socket:

  KASAN: null-ptr-deref in range [0x350-0x357]
  (l2cap_pi(NULL)->chan)

rfcomm_session_close() runs under rfcomm_mutex from the session
thread. Take rfcomm_mutex before lock_sock() in the CONNINFO path so
that the session cannot be unlinked or freed between the check and the
dereference. This is the order the rest of the driver uses:
rfcomm_connect_ind() takes lock_sock(parent) with rfcomm_mutex held,
rfcomm_sk_state_change() runs from __rfcomm_dlc_close() with it held,
and the connect and shutdown paths drop the socket lock before calling
into the core.

Copy the result to user space after dropping both locks, so that a
faulting user buffer cannot stall the session thread.

e.g. on a KASAN build, with a userspace RFCOMM peer sending DISC(0)
during the REUSE_DLC window:

  KASAN: slab-use-after-free in rfcomm_sock_getsockopt+0x714
  Read of size 8 at addr ffff88810dfce210 by task poc/1748
  Allocated by rfcomm_session_add:
   rfcomm_run (krfcommd)
  Freed by rfcomm_session_close:
   rfcomm_run (krfcommd)

This issue was discovered by Tencent CodeBuddy Security.
Cc: stable@vger.kernel.org
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
No Fixes: tag: both the CONNINFO dereference and the REUSE_DLC wiring
predate git history (they are present in 1da177e4c3f4), so there is no
commit to point at.  Cc: stable is added because the race affects
every released kernel.

 include/net/bluetooth/rfcomm.h |  4 ++++
 net/bluetooth/rfcomm/core.c    |  5 ++---
 net/bluetooth/rfcomm/sock.c    | 23 +++++++++++++++++++++--
 3 files changed, 27 insertions(+), 5 deletions(-)

diff --git a/include/net/bluetooth/rfcomm.h b/include/net/bluetooth/rfcomm.h
index 9dc5fd4cb8c8..251ae43b4400 100644
--- a/include/net/bluetooth/rfcomm.h
+++ b/include/net/bluetooth/rfcomm.h
@@ -148,6 +148,10 @@ struct rfcomm_msc {
 
 /* ---- Core structures, flags etc ---- */
 
+extern struct mutex rfcomm_mutex;
+#define rfcomm_lock()	mutex_lock(&rfcomm_mutex)
+#define rfcomm_unlock()	mutex_unlock(&rfcomm_mutex)
+
 struct rfcomm_session {
 	struct list_head list;
 	struct socket   *sock;
diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index 8a4205347c4a..655c55e1f2ce 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -42,9 +42,8 @@ static int channel_mtu = -1;
 
 static struct task_struct *rfcomm_thread;
 
-static DEFINE_MUTEX(rfcomm_mutex);
-#define rfcomm_lock()	mutex_lock(&rfcomm_mutex)
-#define rfcomm_unlock()	mutex_unlock(&rfcomm_mutex)
+/* Serializes session_list walks and dlc/session teardown. */
+DEFINE_MUTEX(rfcomm_mutex);
 
 
 static LIST_HEAD(session_list);
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 3fb8b5474948..23bafb42e685 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -747,11 +747,20 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname,
 	int err = 0;
 	size_t len;
 	u32 opt;
+	bool copy_conninfo = false;
 
 	BT_DBG("sk %p", sk);
 
 	len = sopt->optlen;
 
+	/* The RFCOMM_CONNINFO branch follows dlc->session, which
+	 * rfcomm_session_close() unlinks and frees under rfcomm_mutex.
+	 * Take the mutex first, like every other path that walks
+	 * dlc->session: it must be held before lock_sock().
+	 */
+	if (optname == RFCOMM_CONNINFO)
+		rfcomm_lock();
+
 	lock_sock(sk);
 
 	switch (optname) {
@@ -802,8 +811,7 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname,
 		memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
 
 		len = min(len, sizeof(cinfo));
-		if (copy_to_iter(&cinfo, len, &sopt->iter_out) != len)
-			err = -EFAULT;
+		copy_conninfo = true;
 
 		break;
 
@@ -813,6 +821,17 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname,
 	}
 
 	release_sock(sk);
+
+	if (optname == RFCOMM_CONNINFO)
+		rfcomm_unlock();
+
+	/* Copy out after dropping the locks: a faulting user buffer must
+	 * not stall the session thread.
+	 */
+	if (copy_conninfo &&
+	    copy_to_iter(&cinfo, len, &sopt->iter_out) != len)
+		err = -EFAULT;
+
 	return err;
 }
 
-- 
2.43.7


                 reply	other threads:[~2026-11-20  6:34 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=20261120063343.3875317-1-bsdhenrymartin@gmail.com \
    --to=bsdhenrymartin@gmail.com \
    --cc=aliamemis@disroot.org \
    --cc=benquike@gmail.com \
    --cc=imv4bel@gmail.com \
    --cc=jcperdomo100@gmail.com \
    --cc=leitao@debian.org \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    --cc=mikhail.v.gavrilov@gmail.com \
    --cc=nicoyip.dev@gmail.com \
    --cc=oss@fourdim.xyz \
    --cc=pav@iki.fi \
    --cc=peng.fan@nxp.com \
    --cc=pengpeng@iscas.ac.cn \
    --cc=rollkingzzc@gmail.com \
    --cc=safa.karakus@secunnix.com \
    --cc=skokovmaksimevg@gmail.com \
    --cc=stable@vger.kernel.org \
    --cc=suunj1331@gmail.com \
    --cc=tim.bird@sony.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