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