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 3/3] Bluetooth: MGMT: Reject advertising data while parameters are pending
Date: Sat, 21 Nov 2026 15:15:18 +0800	[thread overview]
Message-ID: <pm-bluetooth-objects-candidate-0167-v9-45c99868af18e41665a3-3@gmail.com> (raw)
In-Reply-To: <pm-bluetooth-objects-candidate-0167-v9-45c99868af18e41665a3-0@gmail.com>

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,

      parent 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 ` [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 [this message]

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-3@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®