public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ v2 0/1] Fix use-after-free in BAP broadcast cleanup
@ 2026-02-14 15:36 Sarveshwar Bajaj
  2026-02-14 15:36 ` [PATCH BlueZ v2 1/1] bap: Fix use-after-free in broadcast sink cleanup Sarveshwar Bajaj
  2026-02-17 14:50 ` [PATCH BlueZ v2 0/1] " patchwork-bot+bluetooth
  0 siblings, 2 replies; 5+ 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

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.

Changes in v2:
- Use safe cleanup pattern to avoid nested queue operations (Pauli Virtanen)
- Apply fix to both bap_bcast_remove() and bap_bcast_disconnect()

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

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

-- 
2.51.0


^ permalink raw reply	[flat|nested] 5+ messages in thread
* [PATCH BlueZ v1 1/1] bap: Fix use-after-free in broadcast sink cleanup
@ 2026-02-13 16:41 Sarveshwar Bajaj
  2026-02-13 17:50 ` Fix use-after-free in BAP broadcast cleanup bluez.test.bot
  0 siblings, 1 reply; 5+ 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

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. This matches the cleanup order already used
for unicast.

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 | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/profiles/audio/bap.c b/profiles/audio/bap.c
index 90a978667..9108bf729 100644
--- a/profiles/audio/bap.c
+++ b/profiles/audio/bap.c
@@ -3822,6 +3822,12 @@ static void bap_bcast_remove(struct btd_service *service)
 		return;
 	}
 
+	/* Clean up before bis_remove and data_remove */
+	if (data->bcast_snks) {
+		queue_destroy(data->bcast_snks, setup_free);
+		data->bcast_snks = NULL;
+	}
+
 	bt_bap_bis_remove(data->bap);
 
 	bap_data_remove(data);
@@ -3938,6 +3944,11 @@ 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) {
+		queue_destroy(data->bcast_snks, setup_free);
+		data->bcast_snks = NULL;
+	}
 
 	bt_bap_detach(data->bap);
 
-- 
2.51.0


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

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

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-14 15:36 [PATCH BlueZ v2 0/1] Fix use-after-free in BAP broadcast cleanup Sarveshwar Bajaj
2026-02-14 15:36 ` [PATCH BlueZ v2 1/1] bap: Fix use-after-free in broadcast sink cleanup Sarveshwar Bajaj
2026-02-14 16:34   ` Fix use-after-free in BAP broadcast cleanup bluez.test.bot
2026-02-17 14:50 ` [PATCH BlueZ v2 0/1] " patchwork-bot+bluetooth
  -- strict thread matches above, loose matches on Subject: below --
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

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