Skip to contentSkip to conversation

Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt

PATCH In review Henry Martin 2 participants 2 messages Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt

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.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---
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(-)
include/net/bluetooth/rfcomm.h +4 -0
index 9dc5fd4cb8c81..251ae43b44003 100644
@@ -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;
net/bluetooth/rfcomm/core.c +2 -4
index 8a4205347c4ac..063a66b28cf7c 100644
@@ -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);
 
net/bluetooth/rfcomm/sock.c +49 -24

97 lines not on this page · raw message

Signature · 1 line
2.53.0
#1 lore raw

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