* [PATCH 1/3] Bluetooth: MGMT: Fix use-after-free in set_le_sync
2026-11-21 7:15 [PATCH 0/3] Bluetooth: MGMT: Fix pending command races Cen Zhang
@ 2026-11-21 7:15 ` Cen Zhang
2026-11-21 7:15 ` [PATCH 2/3] Bluetooth: MGMT: Claim pending pairing before immediate completion Cen Zhang
2026-11-21 7:15 ` [PATCH 3/3] Bluetooth: MGMT: Reject advertising data while parameters are pending Cen Zhang
2 siblings, 0 replies; 4+ messages in thread
From: Cen Zhang @ 2026-11-21 7:15 UTC (permalink / raw)
To: marcel, luiz.dentz
Cc: linux-bluetooth, linux-kernel, baijiaju1990, jjzuming, zzzccc427
set_le_sync() checks its pending command and copies the mode under
mgmt_pending_lock, but later reads cmd->sk after dropping that mutex.
An HCI USER bind can remove the index and free a running request's
pending command; dequeueing queued work cannot stop an already running
set_le_sync():
SET_LE request Index removal
validate cmd; copy mode
unlock mgmt_pending_lock
remove and free cmd
read cmd->sk for advertising removal
Copy the socket under the pending-list lock and hold its reference until
the request's last use. Pass that socket to advertising removal and drop
the temporary reference on the common exit path.
The excerpt below retains the headline and stack traces.
KASAN report as below:
BUG: KASAN: slab-use-after-free in set_le_sync+0x44a/0x450
Read of size 8 at addr ffff88810679d8b0 by task kworker/u17:2/497
Workqueue: hci1 hci_cmd_sync_work
Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
? set_le_sync+0x44a/0x450
? srso_alias_return_thunk+0x5/0xfbef5
? __virt_addr_valid+0x20d/0x410
? set_le_sync+0x44a/0x450
kasan_report+0xe0/0x110
? set_le_sync+0x44a/0x450
set_le_sync+0x44a/0x450
hci_cmd_sync_work+0x1b3/0x450
? srso_alias_return_thunk+0x5/0xfbef5
process_one_work+0x908/0x19c0
? __pfx_process_one_work+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? lock_is_held_type+0x8f/0x100
? srso_alias_return_thunk+0x5/0xfbef5
worker_thread+0x65c/0xe40
? srso_alias_return_thunk+0x5/0xfbef5
? __kthread_parkme+0x175/0x220
? srso_alias_return_thunk+0x5/0xfbef5
? __pfx_worker_thread+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 527:
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
set_le+0x8bc/0xb10
hci_sock_sendmsg+0x128b/0x22e0
sock_write_iter+0x4c0/0x550
do_iter_readv_writev+0x59c/0x860
vfs_writev+0x30e/0xc10
do_writev+0x241/0x2f0
do_syscall_64+0x115/0x6a0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Freed by task 491:
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_pending_foreach+0x1ea/0x2c0
mgmt_index_removed+0x13b/0x2e0
hci_sock_bind+0xcf3/0x1790
__sys_bind+0x18f/0x240
__x64_sys_bind+0x77/0xc0
do_syscall_64+0x115/0x6a0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Fixes: d81a494c43df ("Bluetooth: hci_sync: Convert MGMT_OP_SET_LE")
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/mgmt.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index a5eaa1670b46d..68fc33542645d 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -2141,6 +2141,7 @@ static int set_le_sync(struct hci_dev *hdev, void *data)
{
struct mgmt_pending_cmd *cmd = data;
struct mgmt_mode cp;
+ struct sock *sk;
u8 val;
int err;
@@ -2154,6 +2155,10 @@ static int set_le_sync(struct hci_dev *hdev, void *data)
memcpy(&cp, cmd->param, sizeof(cp));
val = !!cp.val;
+ /* Keep the socket alive if the pending command is removed. */
+ sk = cmd->sk;
+ sock_hold(sk);
+
mutex_unlock(&hdev->mgmt_pending_lock);
if (!val) {
@@ -2163,7 +2168,7 @@ static int set_le_sync(struct hci_dev *hdev, void *data)
hci_disable_advertising_sync(hdev);
if (ext_adv_capable(hdev))
- hci_remove_ext_adv_instance_sync(hdev, 0, cmd->sk);
+ hci_remove_ext_adv_instance_sync(hdev, 0, sk);
} else {
hci_dev_set_flag(hdev, HCI_LE_ENABLED);
}
@@ -2190,6 +2195,8 @@ static int set_le_sync(struct hci_dev *hdev, void *data)
hci_update_passive_scan(hdev);
}
+ sock_put(sk);
+
return err;
}
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 2/3] Bluetooth: MGMT: Claim pending pairing before immediate completion
2026-11-21 7:15 [PATCH 0/3] Bluetooth: MGMT: Fix pending command races Cen Zhang
2026-11-21 7:15 ` [PATCH 1/3] Bluetooth: MGMT: Fix use-after-free in set_le_sync Cen Zhang
@ 2026-11-21 7:15 ` Cen Zhang
2026-11-21 7:15 ` [PATCH 3/3] Bluetooth: MGMT: Reject advertising data while parameters are pending Cen Zhang
2 siblings, 0 replies; 4+ messages in thread
From: Cen Zhang @ 2026-11-21 7:15 UTC (permalink / raw)
To: marcel, luiz.dentz
Cc: linux-bluetooth, linux-kernel, baijiaju1990, jjzuming, zzzccc427
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@gmail.com>
---
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;
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH 3/3] Bluetooth: MGMT: Reject advertising data while parameters are pending
2026-11-21 7:15 [PATCH 0/3] Bluetooth: MGMT: Fix pending command races Cen Zhang
2026-11-21 7:15 ` [PATCH 1/3] Bluetooth: MGMT: Fix use-after-free in set_le_sync Cen Zhang
2026-11-21 7:15 ` [PATCH 2/3] Bluetooth: MGMT: Claim pending pairing before immediate completion Cen Zhang
@ 2026-11-21 7:15 ` Cen Zhang
2 siblings, 0 replies; 4+ messages in thread
From: Cen Zhang @ 2026-11-21 7:15 UTC (permalink / raw)
To: marcel, luiz.dentz
Cc: linux-bluetooth, linux-kernel, baijiaju1990, jjzuming, zzzccc427
An ADD_EXT_ADV_PARAMS request retains an instance number while its
completion runs later in request work. ADD_EXT_ADV_DATA can remove that
pending instance on a validation error and a later request can reuse the
number. The old completion then resolves the number to the new instance:
Parameters request Management handler
queue parameters for A
invalid data removes A
create B with A's number
complete parameters for A
look up the instance number
read or remove B
List the parameters command as pending, validate and claim it before
completion, and copy its instance number under the pending-list lock in
the request function. Reject advertising data while parameters are
pending so the data handler cannot retire the instance in that interval.
The report below was captured before the separate RPA work initialization
fix. The stale completion can still select and remove a replacement
instance after that fix.
The excerpt below retains the headline and stack traces.
KASAN report as below:
BUG: KASAN: slab-use-after-free in adv_instance_rpa_expired+0x2c0/0x2f0
Read of size 1 at addr ffff888110924b2e by task kworker/u17:1/501
Workqueue: hci0 adv_instance_rpa_expired
Call Trace:
<TASK>
dump_stack_lvl+0x93/0xd0
print_report+0xce/0x630
? adv_instance_rpa_expired+0x2c0/0x2f0
? srso_alias_return_thunk+0x5/0xfbef5
? __virt_addr_valid+0x20d/0x410
? adv_instance_rpa_expired+0x2c0/0x2f0
kasan_report+0xe0/0x110
? adv_instance_rpa_expired+0x2c0/0x2f0
adv_instance_rpa_expired+0x2c0/0x2f0
process_one_work+0x908/0x19c0
? __pfx_process_one_work+0x10/0x10
? srso_alias_return_thunk+0x5/0xfbef5
? lock_is_held_type+0x8f/0x100
? srso_alias_return_thunk+0x5/0xfbef5
worker_thread+0x65c/0xe40
? __pfx_worker_thread+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
? save_fpregs_to_fpstate+0xd7/0x230
? srso_alias_return_thunk+0x5/0xfbef5
? __switch_to+0x74f/0xf80
? __pfx_kthread+0x10/0x10
ret_from_fork_asm+0x1a/0x30
</TASK>
Allocated by task 519:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
__kasan_kmalloc+0xaa/0xb0
__kmalloc_cache_noprof+0x251/0x610
hci_add_adv_instance+0x756/0xae0
add_advertising+0x85c/0xff0
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 500:
kasan_save_stack+0x33/0x60
kasan_save_track+0x14/0x30
kasan_save_free_info+0x3b/0x60
__kasan_slab_free+0x5f/0x80
kfree+0x307/0x580
hci_remove_adv_instance+0x222/0x550
add_ext_adv_params_complete+0x2b9/0x880
hci_cmd_sync_work+0x207/0x450
process_one_work+0x908/0x19c0
worker_thread+0x65c/0xe40
kthread+0x34f/0x460
ret_from_fork+0x659/0x940
ret_from_fork_asm+0x1a/0x30
Fixes: cba6b758711c ("Bluetooth: hci_sync: Make use of hci_cmd_sync_queue set 2")
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---
This patch was written with assistance from Codex (OpenAI).
net/bluetooth/mgmt.c | 39 +++++++++++++++++++++++++++++++++------
1 file changed, 33 insertions(+), 6 deletions(-)
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 947d308145e37..dfc2d276a8107 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -9286,7 +9286,7 @@ static void add_ext_adv_params_complete(struct hci_dev *hdev, void *data,
int err)
{
struct mgmt_pending_cmd *cmd = data;
- struct mgmt_cp_add_ext_adv_params *cp = cmd->param;
+ struct mgmt_cp_add_ext_adv_params *cp;
struct mgmt_rp_add_ext_adv_params rp;
struct adv_info *adv;
u32 flags;
@@ -9295,6 +9295,12 @@ static void add_ext_adv_params_complete(struct hci_dev *hdev, void *data,
hci_dev_lock(hdev);
+ if (err == -ECANCELED || !mgmt_pending_valid(hdev, cmd)) {
+ hci_dev_unlock(hdev);
+ return;
+ }
+
+ cp = cmd->param;
adv = hci_find_adv_instance(hdev, cp->instance);
if (!adv)
goto unlock;
@@ -9335,9 +9341,22 @@ static void add_ext_adv_params_complete(struct hci_dev *hdev, void *data,
static int add_ext_adv_params_sync(struct hci_dev *hdev, void *data)
{
struct mgmt_pending_cmd *cmd = data;
- struct mgmt_cp_add_ext_adv_params *cp = cmd->param;
+ struct mgmt_cp_add_ext_adv_params *cp;
+ u8 instance;
+
+ mutex_lock(&hdev->mgmt_pending_lock);
+
+ if (!__mgmt_pending_listed(hdev, cmd)) {
+ mutex_unlock(&hdev->mgmt_pending_lock);
+ return -ECANCELED;
+ }
+
+ cp = cmd->param;
+ instance = cp->instance;
- return hci_setup_ext_adv_instance_sync(hdev, cp->instance);
+ mutex_unlock(&hdev->mgmt_pending_lock);
+
+ return hci_setup_ext_adv_instance_sync(hdev, instance);
}
static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev,
@@ -9431,8 +9450,8 @@ static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev,
* will be programmed once the instance is scheduled.
*/
if (ext_adv_capable(hdev) && !adv_overridden_by_global(hdev, adv)) {
- cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_PARAMS, hdev,
- data, data_len);
+ cmd = mgmt_pending_add(sk, MGMT_OP_ADD_EXT_ADV_PARAMS,
+ hdev, data, data_len);
if (!cmd) {
err = -ENOMEM;
goto remove;
@@ -9441,7 +9460,7 @@ static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev,
err = hci_cmd_sync_queue(hdev, add_ext_adv_params_sync, cmd,
add_ext_adv_params_complete);
if (err < 0) {
- mgmt_pending_free(cmd);
+ mgmt_pending_remove(cmd);
goto remove;
}
} else {
@@ -9545,6 +9564,14 @@ static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data,
goto unlock;
}
+ /* Preserve the instance until its parameters complete. */
+ if (pending_find(MGMT_OP_ADD_EXT_ADV_PARAMS, hdev)) {
+ err = mgmt_cmd_status(sk, hdev->id,
+ MGMT_OP_ADD_EXT_ADV_DATA,
+ MGMT_STATUS_BUSY);
+ goto unlock;
+ }
+
/* In new interface, we require that we are powered to register */
if (!hdev_is_powered(hdev)) {
err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
^ permalink raw reply [flat|nested] 4+ messages in thread