* [PATCH v2] Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt
@ 2026-11-25 7:58 Henry Martin
2026-11-25 11:25 ` [v2] " bluez.test.bot
0 siblings, 1 reply; 2+ messages in thread
From: Henry Martin @ 2026-11-25 7:58 UTC (permalink / raw)
To: marcel, luiz.dentz, jcperdomo100, leitao, rollkingzzc, oss,
skokovmaksimevg, pav, safa.karakus, benquike, peng.fan, tim.bird,
nicoyip.dev, aliamemis, pengpeng, imv4bel, mikhail.v.gavrilov,
suunj1331
Cc: linux-bluetooth, Henry Martin, stable
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
^ permalink raw reply related [flat|nested] 2+ messages in thread* RE: [v2] Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt
2026-11-25 7:58 [PATCH v2] Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt Henry Martin
@ 2026-11-25 11:25 ` bluez.test.bot
0 siblings, 0 replies; 2+ messages in thread
From: bluez.test.bot @ 2026-11-25 11:25 UTC (permalink / raw)
To: linux-bluetooth, bsdhenrymartin
[-- Attachment #1: Type: text/plain, Size: 852 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 failed: net/bluetooth/rfcomm/sock.c:737
error: net/bluetooth/rfcomm/sock.c: patch does not apply
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-25 11:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 7:58 [PATCH v2] Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt Henry Martin
2026-11-25 11:25 ` [v2] " 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