[PATCH 2/3] Bluetooth: MGMT: Claim pending pairing before immediate completion

From: Cen Zhang

Date: Sat Nov 21 2026 - 02:15:37 EST


pair_device() publishes a pending command and installs the connection
callbacks before checking whether security is already sufficient. SMP
completion runs without the handler's device mutex and can consume the
same command during this check:

Pair Device handler SMP completion
publish cmd and callbacks
hci_conn_security()
remove_pairing()
complete and free cmd
read cmd->cmd_complete

Use pairing_complete_cb() for immediate completion too. It claims the
request through remove_pairing() under mgmt_pending_lock before using
it. If SMP has already claimed the command, the callback finds nothing
and returns; otherwise it owns completion and release.

The excerpt below retains the headline and stack traces.

KASAN report as below:

BUG: KASAN: slab-use-after-free in pair_device+0xfcd/0x1070
Read of size 8 at addr ffff88810b001f48 by task python3/561

Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
? pair_device+0xfcd/0x1070
? srso_alias_return_thunk+0x5/0xfbef5
? __virt_addr_valid+0x20d/0x410
? pair_device+0xfcd/0x1070
kasan_report+0xe0/0x110
? pair_device+0xfcd/0x1070
pair_device+0xfcd/0x1070
? __pfx_pair_device+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? do_raw_read_unlock+0x49/0xe0
? srso_alias_return_thunk+0x5/0xfbef5
? _raw_read_unlock+0x23/0x40
? __pfx_mgmt_init_hdev+0x10/0x10
hci_sock_sendmsg+0x128b/0x22e0
? __pfx_hci_sock_sendmsg+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? selinux_socket_sendmsg+0x160/0x280
__sys_sendto+0x425/0x470
? __pfx_hci_sock_sendmsg+0x10/0x10
? __pfx___sys_sendto+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? lockdep_hardirqs_on_prepare+0xea/0x1a0
? srso_alias_return_thunk+0x5/0xfbef5
? __x64_sys_poll+0x11a/0x430
? __pfx___x64_sys_poll+0x10/0x10
__x64_sys_sendto+0xe5/0x1c0
? lockdep_hardirqs_on_prepare+0xea/0x1a0
? 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:0x7f2c2833e687

</TASK>

Allocated by task 561:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
__kasan_kmalloc+0xaa/0xb0
__kmalloc_cache_noprof+0x251/0x610
mgmt_pending_new+0x60/0x230
mgmt_pending_add+0x3b/0x180
pair_device+0x633/0x1070
hci_sock_sendmsg+0x128b/0x22e0
__sys_sendto+0x425/0x470
__x64_sys_sendto+0xe5/0x1c0
do_syscall_64+0x115/0x6a0
entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 522:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x5f/0x80
kfree+0x307/0x580
mgmt_auth_failed+0x238/0x3a0
smp_recv_cb+0x1ccc/0x8d80
l2cap_recv_frame+0x16f4/0x8b10
l2cap_recv_acldata+0xe9f/0x10e0
hci_rx_work+0x5a6/0xf00
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30

Fixes: a511b35ba4f0 ("Bluetooth: Fix incorrect pending cmd removal in pairing_complete()")
Signed-off-by: Cen Zhang <zzzccc427@xxxxxxxxx>
---
This patch was written with assistance from Codex (OpenAI).

net/bluetooth/mgmt.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 68fc33542645d..947d308145e37 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -3853,10 +3853,8 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
cmd->user_data = hci_conn_get(conn);

if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
- hci_conn_security(conn, sec_level, auth_type, true)) {
- cmd->cmd_complete(cmd, 0);
- mgmt_pending_remove(cmd);
- }
+ hci_conn_security(conn, sec_level, auth_type, true))
+ pairing_complete_cb(conn, 0);

err = 0;