Linux bluetooth development
 help / color / mirror / Atom feed
From: Cen Zhang <zzzccc427@gmail.com>
To: Marcel Holtmann <marcel@holtmann.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Cc: linux-bluetooth@vger.kernel.org, baijiaju1990@gmail.com,
	zzzccc427@gmail.com
Subject: [PATCH] Bluetooth: MGMT: Fix ADD_EXT_ADV_DATA rollback of committed instances
Date: Fri,  3 Jul 2026 14:00:33 +0800	[thread overview]
Message-ID: <20260703060033.570932-1-zzzccc427@gmail.com> (raw)

add_ext_adv_data() uses clear_new_instance to roll back failures after
preparing advertising data. That path is valid for a newly created pending
instance, but a malformed ADD_EXT_ADV_DATA request can also reach it for
an already committed advertising instance. In that case clear_new_instance
frees the live adv_info while hci_cmd_sync_work() may still be using it.

The buggy scenario involves two paths, with each column showing the order
within that path:

  advertising enable path:           ADD_EXT_ADV_DATA error path:
  1. look up the committed adv_info  1. accept an update for that instance
  2. wait for the controller         2. hit clear_new_instance on failure
     response                        3. remove and free the live adv_info
  3. resume and access adv_info

Validation reproduced this kernel report:
BUG: KASAN: slab-use-after-free in hci_enable_ext_advertising_sync+0x99/0x190 [bluetooth]
Read of size 1 at addr ffff888104e3f010 by task kworker/u17:1/2502
Workqueue: hci0 hci_cmd_sync_work [bluetooth]

Call Trace:
 <TASK>
 dump_stack_lvl+0x66/0xa0
 print_report+0xce/0x5f0
 ? hci_enable_ext_advertising_sync+0x99/0x190 [bluetooth]
 ? __virt_addr_valid+0x19f/0x330
 ? hci_enable_ext_advertising_sync+0x99/0x190 [bluetooth]
 kasan_report+0xe0/0x110
 ? hci_enable_ext_advertising_sync+0x99/0x190 [bluetooth]
 ? __pfx_add_ext_adv_data_sync+0x10/0x10 [bluetooth]
 hci_enable_ext_advertising_sync+0x99/0x190 [bluetooth]
 ? __pfx_hci_enable_ext_advertising_sync+0x10/0x10 [bluetooth]
 hci_cmd_sync_work+0x187/0x210 [bluetooth]
 process_one_work+0x4fd/0xbc0
 worker_thread+0x2d8/0x570
 kthread+0x1ad/0x1f0
 ret_from_fork+0x3c9/0x540
 ret_from_fork_asm+0x1a/0x30

Allocated by task 2515:
 kasan_save_stack+0x33/0x60
 kasan_save_track+0x17/0x60
 __kasan_kmalloc+0xaa/0xb0
 hci_add_adv_instance+0x35d/0x440 [bluetooth]
 add_ext_adv_params+0x291/0x510 [bluetooth]
 hci_sock_sendmsg+0x96b/0xf80 [bluetooth]
 __sys_sendto+0x2bc/0x2d0
 __x64_sys_sendto+0x76/0x90
 do_syscall_64+0x115/0x6a0
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 2515:
 kasan_save_stack+0x33/0x60
 kasan_save_track+0x17/0x60
 kasan_save_free_info+0x3b/0x60
 __kasan_slab_free+0x5f/0x80
 kfree+0x313/0x590
 hci_remove_adv_instance+0x130/0x1b0 [bluetooth]
 add_ext_adv_data+0x263/0x640 [bluetooth]
 hci_sock_sendmsg+0x96b/0xf80 [bluetooth]
 __sys_sendto+0x2bc/0x2d0
 __x64_sys_sendto+0x76/0x90
 do_syscall_64+0x115/0x6a0
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Only pending instances should be rolled back from ADD_EXT_ADV_DATA
failures. Leave committed instances in place so queued controller work
cannot retain a freed adv_info pointer.

Fixes: 12410572833a2 ("Bluetooth: Break add adv into two mgmt commands")
Assisted-by: Codex:gpt-5.5
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---
 net/bluetooth/mgmt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 733a4b70e10c..a4126d616efa 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -9220,7 +9220,8 @@ static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data,
 	goto unlock;
 
 clear_new_instance:
-	hci_remove_adv_instance(hdev, cp->instance);
+	if (adv_instance->pending)
+		hci_remove_adv_instance(hdev, cp->instance);
 
 unlock:
 	hci_dev_unlock(hdev);
-- 
2.43.0


             reply	other threads:[~2026-07-03  6:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-03  6:00 Cen Zhang [this message]
2026-07-03  6:41 ` Bluetooth: MGMT: Fix ADD_EXT_ADV_DATA rollback of committed instances bluez.test.bot

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=20260703060033.570932-1-zzzccc427@gmail.com \
    --to=zzzccc427@gmail.com \
    --cc=baijiaju1990@gmail.com \
    --cc=linux-bluetooth@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