Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt
Subsystems (3)
PATCH 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. 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.
---
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(-)
include/net/bluetooth/rfcomm.h +4 -0
@@ -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 -3
@@ -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);
net/bluetooth/rfcomm/sock.c +21 -2
@@ -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; }
Signature · 1 line
2.43.7
This is automated email and please do not reply to this email!
Dear submitter,
Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1216976/
---Test result---
Test Summary:
CheckPatch FAIL 1.12 seconds VerifyFixes PASS 0.07 seconds VerifySignedoff PASS 0.08 seconds GitLint FAIL 0.24 seconds SubjectPrefix PASS 0.07 seconds BuildKernel PASS 32.33 seconds CheckAllWarning PASS 37.18 seconds CheckSparse PASS 39.30 seconds BuildKernel32 PASS 32.79 seconds CheckKernelLLVM FAIL 34.47 seconds TestRunnerSetup PASS 763.09 seconds TestRunner_l2cap-tester PASS 14.77 seconds TestRunner_iso-tester PASS 13.11 seconds TestRunner_bnep-tester PASS 3.15 seconds TestRunner_mgmt-tester PASS 89.65 seconds TestRunner_rfcomm-tester PASS 4.52 seconds TestRunner_sco-tester PASS 5.69 seconds TestRunner_ioctl-tester PASS 4.68 seconds TestRunner_mesh-tester PASS 4.53 seconds TestRunner_smp-tester PASS 4.41 seconds TestRunner_userchan-tester PASS 3.23 seconds TestRunner_6lowpan-tester PASS 4.47 seconds IncrementalBuild PASS 30.03 seconds
Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt
WARNING: The commit message has 'stable@', perhaps it also needs a 'Fixes:' tag?
total: 0 errors, 1 warnings, 0 checks, 67 lines checked
NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.
/home/runner/work/bluetooth-next/bluetooth-next/src/patch/15004442.patch has style problems, please review.
NOTE: Ignored message types: UNKNOWN_COMMIT_ID
NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
Bluetooth: RFCOMM: fix race on dlc->session in RFCOMM_CONNINFO getsockopt
6: B3 Line contains hard tab characters (\t): " l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;"
##############################
Test: CheckKernelLLVM - FAIL
Desc: Build kernel with LLVM + context analysis
Output:
net/bluetooth/rfcomm/sock.c:771:2: error: mutex 'rfcomm_mutex' is not held on every path through here [-Werror,-Wthread-safety-analysis]
771 | lock_sock(sk);
| ^
net/bluetooth/rfcomm/sock.c:769:3: note: mutex acquired here
769 | rfcomm_lock();
| ^
./include/net/bluetooth/rfcomm.h:152:23: note: expanded from macro 'rfcomm_lock'
152 | #define rfcomm_lock() mutex_lock(&rfcomm_mutex)
| ^
net/bluetooth/rfcomm/sock.c:833:3: error: releasing mutex 'rfcomm_mutex' that was not held [-Werror,-Wthread-safety-analysis]
833 | rfcomm_unlock();
| ^./include/net/bluetooth/rfcomm.h:153:25: note: expanded from macro 'rfcomm_unlock'
153 | #define rfcomm_unlock() mutex_unlock(&rfcomm_mutex)
| ^
2 errors generated.
make[5]: *** [scripts/Makefile.build:290: net/bluetooth/rfcomm/sock.o] Error 1 make[4]: *** [scripts/Makefile.build:572: net/bluetooth/rfcomm] Error 2
make[4]: *** Waiting for unfinished jobs....
make[3]: *** [scripts/Makefile.build:572: net/bluetooth] Error 2
make[2]: *** [scripts/Makefile.build:572: net] Error 2
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [/home/runner/work/bluetooth-next/bluetooth-next/src/src/Makefile:2243: .] Error 2
make: *** [Makefile:248: __sub-make] Error 2
https://github.com/bluez/bluetooth-next/pull/1165
---
Regards,
Linux Bluetooth