[PATCH 2/2] Bluetooth: RFCOMM: Close the socket before transferring its DLC

From: Cen Zhang

Date: Sat Nov 21 2026 - 02:03:19 EST


RFCOMM_REUSE_DLC changes the DLC owner and callbacks to the TTY before
marking the old socket closed, without waiting for that socket's lock.
RFCOMM_CONNINFO can have passed its state check before this handoff:

Socket getter TTY handoff / RFCOMM worker
lock socket; check CONNECTED
replace owner and callbacks
close and free the session
follow dlc->session->sock

The replacement TTY callback does not serialize with the old socket, so
the getter's socket lock no longer protects session teardown. Close the
old socket under its lock before replacing the callbacks. Existing
getters finish first and later getters reject the closed socket.

The excerpt below retains the headline and stack traces.

KASAN report as below:

KASAN: null-ptr-deref in range [0x0000000000000010-0x0000000000000017]

RIP: 0010:rfcomm_sock_getsockopt+0x60a/0x950

Call Trace:
<TASK>
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_rfcomm_sock_getsockopt+0x10/0x10
? __might_fault+0x135/0x190
? srso_alias_return_thunk+0x5/0xfbef5
? free_zapped_rcu+0xd0/0x1e0
? __might_fault+0xdd/0x190
? srso_alias_return_thunk+0x5/0xfbef5
? __might_fault+0x14e/0x190
do_sock_getsockopt+0x342/0xa20
? __pfx_do_sock_getsockopt+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? srso_alias_return_thunk+0x5/0xfbef5
? _copy_to_user+0x7b/0x90
? srso_alias_return_thunk+0x5/0xfbef5
__sys_getsockopt+0x113/0x1a0
? srso_alias_return_thunk+0x5/0xfbef5
? __x64_sys_getsockopt+0xc2/0x160
? srso_alias_return_thunk+0x5/0xfbef5
__x64_sys_getsockopt+0xc2/0x160
? srso_alias_return_thunk+0x5/0xfbef5
? trace_hardirqs_on+0x18/0x160
do_syscall_64+0x115/0x6a0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7ff8b2e9007a

</TASK>

RIP: 0010:rfcomm_sock_getsockopt+0x60a/0x950

Fixes: 08d1914293da ("Bluetooth: RFCOMM: FIX possible deadlock in rfcomm_sk_state_change")
Signed-off-by: Cen Zhang <zzzccc427@xxxxxxxxx>
---
This patch was written with assistance from Codex (OpenAI).

net/bluetooth/rfcomm/tty.c | 20 ++++++++------------
1 file changed, 8 insertions(+), 12 deletions(-)

diff --git a/net/bluetooth/rfcomm/tty.c b/net/bluetooth/rfcomm/tty.c
index 198c7dd1a95b4..676fade1ee8dd 100644
--- a/net/bluetooth/rfcomm/tty.c
+++ b/net/bluetooth/rfcomm/tty.c
@@ -286,6 +286,13 @@ static struct rfcomm_dev *__rfcomm_dev_add(struct rfcomm_dev_req *req,
skb_queue_tail(&dev->pending, skb);
atomic_sub(skb->len, &sk->sk_rmem_alloc);
}
+
+ /* Finish RFCOMM_CONNINFO before replacing the callback
+ * which serializes session teardown with the socket.
+ */
+ lock_sock(sk);
+ sk->sk_state = BT_CLOSED;
+ release_sock(sk);
}

dlc->data_ready = rfcomm_dev_data_ready;
@@ -389,7 +396,6 @@ static int __rfcomm_create_dev(struct sock *sk, void __user *arg)
{
struct rfcomm_dev_req req;
struct rfcomm_dlc *dlc;
- int id;

if (copy_from_user(&req, arg, sizeof(req)))
return -EFAULT;
@@ -418,17 +424,7 @@ static int __rfcomm_create_dev(struct sock *sk, void __user *arg)
return -ENOMEM;
}

- id = rfcomm_dev_add(&req, dlc);
- if (id < 0)
- return id;
-
- if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
- /* DLC is now used by device.
- * Socket must be disconnected */
- sk->sk_state = BT_CLOSED;
- }
-
- return id;
+ return rfcomm_dev_add(&req, dlc);
}

static int __rfcomm_release_dev(void __user *arg)