The Linux Kernel Mailing List
 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 v2 2/2] Bluetooth: hci_sync: Use controller handles for periodic advertising
Date: Thu, 19 Nov 2026 08:32:46 +0800	[thread overview]
Message-ID: <pm-bluetooth-objects-candidate-0245-v14-e37270232af59a877004-2@gmail.com> (raw)
In-Reply-To: <pm-bluetooth-objects-candidate-0245-v14-e37270232af59a877004-0@gmail.com>

Management instance numbers and controller advertising handles differ on
controllers with a single set: instance 1 uses handle 0x00. Periodic
advertising data already sends adv->handle, but the parameters and
enable commands send the instance number. The completion handler also
treats the command's handle as an instance number:

  Request worker                    Controller / completion
  create instance 1 on handle 0
  send periodic parameters for 1
                                    Unknown Advertising Identifier

Snapshot the handle under hdev->lock for periodic parameters, enable and
disable. For instance 1 using handle 0, update periodic_enabled under
the mutex after the command succeeds. Its completion handler cannot
distinguish that instance from global advertising by the handle alone;
keep the software instance number in the request, as the existing
remove-set and random-address paths do. Use the same mapping for the
advertising set in periodic transfer and both BIG creation commands,
which obtain the instance number from the same broadcast QoS field as
hci_start_per_adv_sync(). Keep the instance number in software state,
and send no HCI command with the mutex held.

Fixes: eca0ae4aea66 ("Bluetooth: Add initial implementation of BIS connections")
Signed-off-by: Cen Zhang <zzzccc427@gmail.com>
---

 net/bluetooth/hci_sync.c | 72 +++++++++++++++++++++++++++++++++++-----
 1 file changed, 63 insertions(+), 9 deletions(-)

diff --git a/net/bluetooth/hci_sync.c b/net/bluetooth/hci_sync.c
index 4475d674375fd..42a577f201a19 100644
--- a/net/bluetooth/hci_sync.c
+++ b/net/bluetooth/hci_sync.c
@@ -1817,11 +1817,15 @@ int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
 	struct hci_cp_le_set_per_adv_enable cp;
 	struct adv_info *adv;
 	bool enabled;
+	u8 handle = instance;
+	int err;
 
 	/* If periodic advertising already disabled there is nothing to do. */
 	hci_dev_lock(hdev);
 	adv = hci_find_adv_instance(hdev, instance);
 	enabled = adv && adv->periodic_enabled;
+	if (adv)
+		handle = adv->handle;
 	hci_dev_unlock(hdev);
 	if (!enabled)
 		return 0;
@@ -1829,10 +1833,39 @@ int hci_disable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
 	memset(&cp, 0, sizeof(cp));
 
 	cp.enable = 0x00;
-	cp.handle = instance;
+	cp.handle = handle;
 
-	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
-				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
+				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+	if (err || handle == instance)
+		return err;
+
+	/* Handle 0 is shared with global advertising, so the completion
+	 * cannot identify the software instance from the command alone.
+	 */
+	hci_dev_lock(hdev);
+	adv = hci_find_adv_instance(hdev, instance);
+	if (adv)
+		adv->periodic_enabled = false;
+	hci_dev_unlock(hdev);
+
+	return 0;
+}
+
+/* Map a management instance number to its controller handle. */
+static u8 hci_adv_instance_handle(struct hci_dev *hdev, u8 instance)
+	__must_not_hold(&hdev->lock)
+{
+	struct adv_info *adv;
+	u8 handle = instance;
+
+	hci_dev_lock(hdev);
+	adv = hci_find_adv_instance(hdev, instance);
+	if (adv)
+		handle = adv->handle;
+	hci_dev_unlock(hdev);
+
+	return handle;
 }
 
 static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
@@ -1848,7 +1881,7 @@ static int hci_set_per_adv_params_sync(struct hci_dev *hdev, u8 instance,
 	if (!max_interval)
 		max_interval = DISCOV_LE_PER_ADV_INT_MAX;
 
-	cp.handle = instance;
+	cp.handle = hci_adv_instance_handle(hdev, instance);
 	cp.min_interval = cpu_to_le16(min_interval);
 	cp.max_interval = cpu_to_le16(max_interval);
 	cp.periodic_properties = 0x0000;
@@ -1894,11 +1927,15 @@ static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
 	struct hci_cp_le_set_per_adv_enable cp;
 	struct adv_info *adv;
 	bool enabled;
+	u8 handle = instance;
+	int err;
 
 	/* If periodic advertising already enabled there is nothing to do. */
 	hci_dev_lock(hdev);
 	adv = hci_find_adv_instance(hdev, instance);
 	enabled = adv && adv->periodic_enabled;
+	if (adv)
+		handle = adv->handle;
 	hci_dev_unlock(hdev);
 	if (enabled)
 		return 0;
@@ -1906,10 +1943,23 @@ static int hci_enable_per_advertising_sync(struct hci_dev *hdev, u8 instance)
 	memset(&cp, 0, sizeof(cp));
 
 	cp.enable = 0x01;
-	cp.handle = instance;
+	cp.handle = handle;
 
-	return __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
-				     sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+	err = __hci_cmd_sync_status(hdev, HCI_OP_LE_SET_PER_ADV_ENABLE,
+				    sizeof(cp), &cp, HCI_CMD_TIMEOUT);
+	if (err || handle == instance)
+		return err;
+
+	/* Handle 0 is shared with global advertising, so the completion
+	 * cannot identify the software instance from the command alone.
+	 */
+	hci_dev_lock(hdev);
+	adv = hci_find_adv_instance(hdev, instance);
+	if (adv)
+		adv->periodic_enabled = true;
+	hci_dev_unlock(hdev);
+
+	return 0;
 }
 
 /* Checks if periodic advertising data contains a Basic Announcement and if it
@@ -8458,6 +8508,7 @@ static int hci_le_past_set_info_sync(struct hci_dev *hdev, void *data)
 {
 	struct past_data *past = data;
 	struct hci_cp_le_past_set_info cp;
+	struct adv_info *adv;
 
 	hci_dev_lock(hdev);
 
@@ -8470,6 +8521,9 @@ static int hci_le_past_set_info_sync(struct hci_dev *hdev, void *data)
 	memset(&cp, 0, sizeof(cp));
 	cp.handle = cpu_to_le16(past->le->handle);
 	cp.adv_handle = past->conn->iso_qos.bcast.bis;
+	adv = hci_find_adv_instance(hdev, cp.adv_handle);
+	if (adv)
+		cp.adv_handle = adv->handle;
 
 	hci_dev_unlock(hdev);
 
@@ -8858,7 +8912,7 @@ static int hci_le_create_big_v2_sync(struct hci_conn *conn,
 
 	memset(&cp, 0, sizeof(cp));
 	cp.handle = qos->bcast.big;
-	cp.adv_handle = qos->bcast.bis;
+	cp.adv_handle = hci_adv_instance_handle(hdev, qos->bcast.bis);
 	cp.num_bis  = num_bis;
 	hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval);
 	cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu);
@@ -8894,7 +8948,7 @@ int hci_le_create_big_sync(struct hci_conn *conn, struct bt_iso_qos_ext *qos,
 
 	memset(&cp, 0, sizeof(cp));
 	cp.handle = qos->bcast.big;
-	cp.adv_handle = qos->bcast.bis;
+	cp.adv_handle = hci_adv_instance_handle(hdev, qos->bcast.bis);
 	cp.num_bis  = num_bis;
 	hci_cpu_to_le24(qos->bcast.out.interval, cp.bis.sdu_interval);
 	cp.bis.sdu = cpu_to_le16(qos->bcast.out.sdu);

      parent reply	other threads:[~2026-11-19  0:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  0:32 [PATCH v2 0/2] Bluetooth: Fix advertising instance accesses without hdev->lock Cen Zhang
2026-11-19  0:32 ` [PATCH v2 1/2] Bluetooth: hci_sync: Fix remaining advertising instance UAFs Cen Zhang
2026-11-19  0:32 ` 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-0245-v14-e37270232af59a877004-2@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