Bluetooth: RFCOMM: Protect sockets during teardown and TTY handoff
Subsystems (2)
cover Bluetooth: RFCOMM: Protect sockets during teardown and TTY handoff
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(-)
---
PATCH 1/2 Bluetooth: RFCOMM: Hold the listener across child unlink
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---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/rfcomm/sock.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-)
net/bluetooth/rfcomm/sock.c +12 -5
@@ -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); } }
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/1217946/
---Test result---
Test Summary:
CheckPatch PASS 1.56 seconds VerifyFixes PASS 0.13 seconds VerifySignedoff PASS 0.13 seconds GitLint PASS 0.69 seconds SubjectPrefix PASS 0.25 seconds BuildKernel PASS 31.31 seconds CheckAllWarning PASS 35.37 seconds CheckSparse PASS 37.75 seconds BuildKernel32 PASS 30.85 seconds CheckKernelLLVM PASS 34.69 seconds TestRunnerSetup PASS 749.93 seconds TestRunner_rfcomm-tester PASS 5.64 seconds IncrementalBuild PASS 30.60 seconds
https://github.com/bluez/bluetooth-next/pull/1173
---
Regards,
Linux Bluetooth
PATCH 2/2 Bluetooth: RFCOMM: Close the socket before transferring its DLC
In reply to Cen ZhangRFCOMM_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---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/rfcomm/tty.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-)
net/bluetooth/rfcomm/tty.c +8 -12
@@ -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)
Luiz Augusto von Dentz
maintainerHi Cen,
Quoted 10 lines · Cen Zhang
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
Quoted 5 lines
net/bluetooth/rfcomm/sock.c | 17 ++++++++++++----- net/bluetooth/rfcomm/tty.c | 20 ++++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-)---
Netdev sashiko found a couple of problems:
Signature · 1 line
Luiz Augusto von Dentz
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Hello:
This series was applied to bluetooth/bluetooth-next.git (master) by Luiz Augusto von Dentz <[email protected]>:
Quoted 10 lines · On Sat, 21 Nov 2026 15:02:50 +0800 you wrote:
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[...]
Here is the summary with links:
- [1/2] Bluetooth: RFCOMM: Hold the listener across child unlink
https://git.kernel.org/bluetooth/bluetooth-next/c/22d1972f6834 - [2/2] Bluetooth: RFCOMM: Close the socket before transferring its DLC https://git.kernel.org/bluetooth/bluetooth-next/c/56bc551ef79c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html