* [PATCH 0/2] Bluetooth: RFCOMM: Protect sockets during teardown and TTY handoff
@ 2026-11-21 7:02 Cen Zhang
2026-11-21 7:02 ` [PATCH 1/2] Bluetooth: RFCOMM: Hold the listener across child unlink Cen Zhang
2026-11-21 7:02 ` [PATCH 2/2] Bluetooth: RFCOMM: Close the socket before transferring its DLC Cen Zhang
0 siblings, 2 replies; 3+ messages in thread
From: Cen Zhang @ 2026-11-21 7:02 UTC (permalink / raw)
To: marcel, luiz.dentz
Cc: linux-bluetooth, linux-kernel, baijiaju1990, jjzuming, zzzccc427
Socket notification and TTY handoff can outlive the object which made
the socket's borrowed pointers valid. Keep the listener alive across
child unlink and notification, and finish old socket users before the
TTY replaces its DLC callbacks.
Cen Zhang (2):
Bluetooth: RFCOMM: Hold the listener across child unlink
Bluetooth: RFCOMM: Close the socket before transferring its DLC
net/bluetooth/rfcomm/sock.c | 17 ++++++++++++-----
net/bluetooth/rfcomm/tty.c | 20 ++++++++------------
2 files changed, 20 insertions(+), 17 deletions(-)
---
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 1/2] Bluetooth: RFCOMM: Hold the listener across child unlink
2026-11-21 7:02 [PATCH 0/2] Bluetooth: RFCOMM: Protect sockets during teardown and TTY handoff Cen Zhang
@ 2026-11-21 7:02 ` Cen Zhang
2026-11-21 7:02 ` [PATCH 2/2] Bluetooth: RFCOMM: Close the socket before transferring its DLC Cen Zhang
1 sibling, 0 replies; 3+ messages in thread
From: Cen Zhang @ 2026-11-21 7:02 UTC (permalink / raw)
To: marcel, luiz.dentz
Cc: linux-bluetooth, linux-kernel, baijiaju1990, jjzuming, zzzccc427
rfcomm_sk_state_change() saves the listener pointer, unlinks a closed
unaccepted child, and then notifies the listener. Once the child is no
longer on the accept queue, listener release can finish without waiting
for that callback:
RFCOMM callback Listener release
lock child socket
save parent
bt_accept_unlink(child)
drain the empty accept queue
drop the last listener reference
parent->sk_data_ready(parent)
Take a listener reference before unlinking and release it after the
notification. Track that reference separately from SOCK_ZAPPED so a
different state does not release a reference it never acquired. Drop
the DLC lock before either socket can be destroyed.
The excerpt below retains the headline and stack traces.
Keep the socket reference across accept-queue unlink, as commit
4bd0b274054f ("Bluetooth: fix UAF in bt_accept_dequeue()") does for
the dequeued socket.
KASAN report as below:
BUG: KASAN: slab-use-after-free in rfcomm_sk_state_change+0x3a4/0x3f0
Read of size 8 at addr ffff88810bc8b188 by task krfcommd/75
Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
? rfcomm_sk_state_change+0x3a4/0x3f0
? srso_alias_return_thunk+0x5/0xfbef5
? __virt_addr_valid+0x20d/0x410
? rfcomm_sk_state_change+0x3a4/0x3f0
kasan_report+0xe0/0x110
? rfcomm_sk_state_change+0x3a4/0x3f0
rfcomm_sk_state_change+0x3a4/0x3f0
__rfcomm_dlc_close+0x1a6/0x6d0
rfcomm_run+0x1b7c/0x5390
? __pfx_rfcomm_run+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_woken_wake_function+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? lock_release+0xc8/0x280
? srso_alias_return_thunk+0x5/0xfbef5
? trace_hardirqs_on+0x18/0x160
? srso_alias_return_thunk+0x5/0xfbef5
? __kthread_parkme+0x175/0x220
? __pfx_rfcomm_run+0x10/0x10
kthread+0x34f/0x460
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_kthread+0x10/0x10
ret_from_fork+0x659/0x940
? __pfx_ret_from_fork+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? __switch_to+0x74f/0xf80
? __pfx_kthread+0x10/0x10
ret_from_fork_asm+0x1a/0x30
</TASK>
Allocated by task 513:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
__kasan_kmalloc+0xaa/0xb0
__kmalloc_noprof+0x2d7/0x770
sk_prot_alloc+0x13e/0x260
sk_alloc+0x37/0xb40
bt_sock_alloc+0x40/0x3b0
rfcomm_sock_alloc.constprop.0+0x56/0x290
rfcomm_sock_create+0xe6/0x170
bt_sock_create+0x171/0x330
__sock_create+0x2c4/0x730
__sys_socket+0x141/0x210
__x64_sys_socket+0x77/0xc0
do_syscall_64+0x115/0x6a0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Freed by task 515:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x5f/0x80
kfree+0x307/0x580
__sk_destruct+0x64f/0x790
sk_destruct+0xb3/0xd0
__sk_free+0xdd/0x370
sk_free+0x51/0x80
rfcomm_sock_kill+0x108/0x310
rfcomm_sock_release+0x122/0x1a0
__sock_release+0xb8/0x270
sock_close+0x21/0x30
__fput+0x39f/0xa60
fput_close_sync+0xff/0x200
__x64_sys_close+0x8c/0xf0
do_syscall_64+0x115/0x6a0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/rfcomm/sock.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 3ec2fb4ce5804..948f0da417bdc 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -65,7 +65,8 @@ static void rfcomm_sk_data_ready(struct rfcomm_dlc *d, struct sk_buff *skb)
static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
__must_hold(&d->lock)
{
- struct sock *sk = d->owner, *parent;
+ struct sock *sk = d->owner, *parent, *parent_ref = NULL;
+ bool kill;
if (!sk)
return;
@@ -82,6 +83,9 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
parent = bt_sk(sk)->parent;
if (parent) {
if (d->state == BT_CLOSED) {
+ /* Keep the listener alive across unlink. */
+ sock_hold(parent);
+ parent_ref = parent;
sock_set_flag(sk, SOCK_ZAPPED);
bt_accept_unlink(sk);
}
@@ -95,11 +99,14 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
release_sock(sk);
- if (parent && sock_flag(sk, SOCK_ZAPPED)) {
- /* We have to drop DLC lock here, otherwise
- * rfcomm_sock_destruct() will dead lock. */
+ kill = parent && sock_flag(sk, SOCK_ZAPPED);
+ if (kill || parent_ref) {
+ /* Socket destruction takes a DLC lock. */
rfcomm_dlc_unlock(d);
- rfcomm_sock_kill(sk);
+ if (kill)
+ rfcomm_sock_kill(sk);
+ if (parent_ref)
+ sock_put(parent_ref);
rfcomm_dlc_lock(d);
}
}
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH 2/2] Bluetooth: RFCOMM: Close the socket before transferring its DLC
2026-11-21 7:02 [PATCH 0/2] Bluetooth: RFCOMM: Protect sockets during teardown and TTY handoff Cen Zhang
2026-11-21 7:02 ` [PATCH 1/2] Bluetooth: RFCOMM: Hold the listener across child unlink Cen Zhang
@ 2026-11-21 7:02 ` Cen Zhang
1 sibling, 0 replies; 3+ messages in thread
From: Cen Zhang @ 2026-11-21 7:02 UTC (permalink / raw)
To: marcel, luiz.dentz
Cc: linux-bluetooth, linux-kernel, baijiaju1990, jjzuming, zzzccc427
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@gmail.com>
---
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)
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-21 7:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-21 7:02 [PATCH 0/2] Bluetooth: RFCOMM: Protect sockets during teardown and TTY handoff Cen Zhang
2026-11-21 7:02 ` [PATCH 1/2] Bluetooth: RFCOMM: Hold the listener across child unlink Cen Zhang
2026-11-21 7:02 ` [PATCH 2/2] Bluetooth: RFCOMM: Close the socket before transferring its DLC Cen Zhang
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®