Linux bluetooth development
 help / color / mirror / Atom feed
From: Henry Martin <bsdhenrymartin@gmail.com>
To: marcel@holtmann.org, luiz.dentz@gmail.com,
	jcperdomo100@gmail.com, leitao@debian.org, rollkingzzc@gmail.com,
	oss@fourdim.xyz, skokovmaksimevg@gmail.com, pav@iki.fi,
	safa.karakus@secunnix.com, benquike@gmail.com, peng.fan@nxp.com,
	tim.bird@sony.com, nicoyip.dev@gmail.com, aliamemis@disroot.org,
	pengpeng@iscas.ac.cn, imv4bel@gmail.com,
	mikhail.v.gavrilov@gmail.com, suunj1331@gmail.com
Cc: linux-bluetooth@vger.kernel.org,
	Henry Martin <bsdhenrymartin@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH v2] Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt
Date: Wed, 25 Nov 2026 15:58:51 +0800	[thread overview]
Message-ID: <20261125075852.641537-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, and the check and the dereference lose the race, e.g.:

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

If the free lands between the check and the dereference instead, the
dereference faults on a NULL l2cap socket.

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.

Move the CONNINFO branch into its own helper so the rfcomm_mutex
acquire/release is unconditional on every path (clang thread-safety),
and copy the result to user space after dropping both locks, so that a
faulting user buffer cannot stall the session thread.

This issue was discovered by Tencent CodeBuddy Security.
Cc: stable@vger.kernel.org
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
Fixes: points at the initial git commit because both the CONNINFO
dereference and the REUSE_DLC wiring predate git history.

v1 -> v2: add the Fixes: tag (checkpatch); untabify the quoted line
          (gitlint); move the CONNINFO branch into a helper so the
          rfcomm_mutex acquire/release is unconditional (clang
          thread-safety analysis failed the conditional form on v1).
---
 include/net/bluetooth/rfcomm.h |  4 ++
 net/bluetooth/rfcomm/core.c    |  6 +--
 net/bluetooth/rfcomm/sock.c    | 73 +++++++++++++++++++++++-----------
 3 files changed, 55 insertions(+), 28 deletions(-)

diff --git a/include/net/bluetooth/rfcomm.h b/include/net/bluetooth/rfcomm.h
index 9dc5fd4cb8c81..251ae43b44003 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 8a4205347c4ac..063a66b28cf7c 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -42,10 +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 3fb8b54749482..ecaffc408e736 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -737,8 +737,7 @@ static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname,
 	return err;
 }
 
-static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname,
-				      sockopt_t *sopt)
+static int rfcomm_sock_getconninfo(struct socket *sock, sockopt_t *sopt)
 {
 	struct sock *sk = sock->sk;
 	struct sock *l2cap_sk;
@@ -746,12 +745,60 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname,
 	struct rfcomm_conninfo cinfo;
 	int err = 0;
 	size_t len;
+
+	len = min_t(size_t, sopt->optlen, sizeof(cinfo));
+
+	/* The 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().
+	 */
+	rfcomm_lock();
+	lock_sock(sk);
+
+	if ((sk->sk_state != BT_CONNECTED &&
+	     !(sk->sk_state == BT_CONNECT2 &&
+	       rfcomm_pi(sk)->dlc->defer_setup)) ||
+	    !rfcomm_pi(sk)->dlc->session) {
+		err = -ENOTCONN;
+		goto out;
+	}
+
+	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);
+
+out:
+	release_sock(sk);
+	rfcomm_unlock();
+
+	/* Copy out after dropping the locks: a faulting user buffer must
+	 * not stall the session thread.
+	 */
+	if (!err && copy_to_iter(&cinfo, len, &sopt->iter_out) != len)
+		err = -EFAULT;
+
+	return err;
+}
+
+static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname,
+				      sockopt_t *sopt)
+{
+	struct sock *sk = sock->sk;
+	int err = 0;
+	size_t len;
 	u32 opt;
 
 	BT_DBG("sk %p", sk);
 
 	len = sopt->optlen;
 
+	if (optname == RFCOMM_CONNINFO)
+		return rfcomm_sock_getconninfo(sock, sopt);
+
 	lock_sock(sk);
 
 	switch (optname) {
@@ -785,28 +832,6 @@ 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) {
-			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);
-
-		len = min(len, sizeof(cinfo));
-		if (copy_to_iter(&cinfo, len, &sopt->iter_out) != len)
-			err = -EFAULT;
-
-		break;
-
 	default:
 		err = -ENOPROTOOPT;
 		break;
-- 
2.53.0


             reply	other threads:[~2026-11-25  7:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  7:58 Henry Martin [this message]
2026-11-25 11:25 ` [v2] Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt bluez.test.bot

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=20261125075852.641537-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