mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Cen Zhang <zzzccc427@gmail.com>
To: marcel@holtmann.org, luiz.dentz@gmail.com
Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
	baijiaju1990@gmail.com, jjzuming@gmail.com, zzzccc427@gmail.com
Subject: [PATCH 1/3] Bluetooth: MGMT: Fix use-after-free in set_le_sync
Date: Sat, 21 Nov 2026 15:15:16 +0800	[thread overview]
Message-ID: <pm-bluetooth-objects-candidate-0167-v9-45c99868af18e41665a3-1@gmail.com> (raw)
In-Reply-To: <pm-bluetooth-objects-candidate-0167-v9-45c99868af18e41665a3-0@gmail.com>

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;
 }
 

  reply	other threads:[~2026-11-21  7:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-21  7:15 [PATCH 0/3] Bluetooth: MGMT: Fix pending command races Cen Zhang
2026-11-21  7:15 ` Cen Zhang [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pm-bluetooth-objects-candidate-0167-v9-45c99868af18e41665a3-1@gmail.com \
    --to=zzzccc427@gmail.com \
    --cc=baijiaju1990@gmail.com \
    --cc=jjzuming@gmail.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®