public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ v1 0/1] Fix use-after-free in BAP broadcast cleanup
@ 2026-02-13 16:41 Sarveshwar Bajaj
  2026-02-13 16:41 ` [PATCH BlueZ v1 1/1] bap: Fix use-after-free in broadcast sink cleanup Sarveshwar Bajaj
  0 siblings, 1 reply; 6+ messages in thread
From: Sarveshwar Bajaj @ 2026-02-13 16:41 UTC (permalink / raw)
  To: linux-bluetooth, luiz.dentz
  Cc: vinit.mehta, sarveshwar.bajaj, devyani.godbole

This fixes a use-after-free crash when broadcast audio sources 
disconnect or undergo RPA rotation as reported in issue #1866.

The crash occurs because bap_data_free() was freeing streams before 
destroying the broadcast sink setups that still held references to them.

Tested with AddressSanitizer on latest 6.19 kernel with NXPs 
controller as broadcast sink and Samsung S23 broadcast source. 
No crashes observed with disconnect or RPA rotation after fix.


Sarveshwar Bajaj (1):
  bap: Fix use-after-free in broadcast sink cleanup

 profiles/audio/bap.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

-- 
2.51.0


^ permalink raw reply	[flat|nested] 6+ messages in thread
* [PATCH BlueZ v2 1/1] bap: Fix use-after-free in broadcast sink cleanup
@ 2026-02-14 15:36 Sarveshwar Bajaj
  2026-02-14 16:34 ` Fix use-after-free in BAP broadcast cleanup bluez.test.bot
  0 siblings, 1 reply; 6+ messages in thread
From: Sarveshwar Bajaj @ 2026-02-14 15:36 UTC (permalink / raw)
  To: linux-bluetooth, luiz.dentz
  Cc: vinit.mehta, sarveshwar.bajaj, devyani.godbole

bap_data_free() was calling bt_bap_detach() before destroying
bcast_snks queue. bt_bap_detach() frees all streams but broadcast
sink setups in bcast_snks queue were still holding pointers to these
streams. When queue_destroy() calls setup_free() as its destructor,
it attempts to access these already-freed stream pointers, causing
a use-after-free.

Fix this by destroying the bcast_snks queue before calling
bt_bap_detach() and ensuring stream references are released
while the streams are still valid.

Used safe cleanup pattern to avoid nested queue operations during
destruction as setup_free()may attempt to remove items from queue
being destroyed.

Crash trace:
  AddressSanitizer: heap-use-after-free
  #0 bt_bap_stream_unlock src/shared/bap.c:6384
  #1 setup_free profiles/audio/bap.c:1123
  #2 queue_destroy src/shared/queue.c:60
  #3 bap_data_free profiles/audio/bap.c:210

https://github.com/bluez/bluez/issues/1866
---
 profiles/audio/bap.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/profiles/audio/bap.c b/profiles/audio/bap.c
index 90a978667..375026440 100644
--- a/profiles/audio/bap.c
+++ b/profiles/audio/bap.c
@@ -3808,6 +3808,7 @@ static void bap_bcast_remove(struct btd_service *service)
 {
 	struct btd_device *device = btd_service_get_device(service);
 	struct bap_data *data;
+	struct queue *bcast_snks;
 	char addr[18];
 
 	ba2str(device_get_address(device), addr);
@@ -3822,6 +3823,13 @@ static void bap_bcast_remove(struct btd_service *service)
 		return;
 	}
 
+	/* Clean up before bis_remove and data_remove */
+	if (data->bcast_snks) {
+		bcast_snks = data->bcast_snks;
+		data->bcast_snks = NULL;
+		queue_destroy(bcast_snks, setup_free);
+	}
+
 	bt_bap_bis_remove(data->bap);
 
 	bap_data_remove(data);
@@ -3929,6 +3937,7 @@ static int bap_disconnect(struct btd_service *service)
 static int bap_bcast_disconnect(struct btd_service *service)
 {
 	struct bap_data *data;
+	struct queue *bcast_snks;
 
 	/* Lookup the bap session for this service since in case of
 	 * bass_delegator its user data is set by bass plugin.
@@ -3938,6 +3947,12 @@ static int bap_bcast_disconnect(struct btd_service *service)
 		error("BAP service not handled by profile");
 		return -EINVAL;
 	}
+	/* Clean up broadcast sinks before detach (like unicast does) */
+	if (data->bcast_snks) {
+		bcast_snks = data->bcast_snks;
+		data->bcast_snks = NULL;
+		queue_destroy(bcast_snks, setup_free);
+	}
 
 	bt_bap_detach(data->bap);
 
-- 
2.51.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-02-14 16:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-13 16:41 [PATCH BlueZ v1 0/1] Fix use-after-free in BAP broadcast cleanup Sarveshwar Bajaj
2026-02-13 16:41 ` [PATCH BlueZ v1 1/1] bap: Fix use-after-free in broadcast sink cleanup Sarveshwar Bajaj
2026-02-13 17:50   ` Fix use-after-free in BAP broadcast cleanup bluez.test.bot
2026-02-13 18:16   ` [PATCH BlueZ v1 1/1] bap: Fix use-after-free in broadcast sink cleanup Pauli Virtanen
2026-02-14 14:51     ` [EXT] " Sarveshwar Bajaj
  -- strict thread matches above, loose matches on Subject: below --
2026-02-14 15:36 [PATCH BlueZ v2 " Sarveshwar Bajaj
2026-02-14 16:34 ` Fix use-after-free in BAP broadcast cleanup bluez.test.bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox