Linux bluetooth development
 help / color / mirror / Atom feed
* [PATCH BlueZ] bap: Fix use-after-free of device referenced by session
@ 2026-09-19  0:14 Pauli Virtanen
  2026-09-19  1:24 ` [BlueZ] " bluez.test.bot
  2026-09-21 14:30 ` [PATCH BlueZ] " patchwork-bot+bluetooth
  0 siblings, 2 replies; 3+ messages in thread
From: Pauli Virtanen @ 2026-09-19  0:14 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Pauli Virtanen

bap_attached() creates bap_data that are not bound to btd_service, and
can outlive the device (eg if ATT POLLHUP -> bap_detached is late).
This then crashes when accessing bap_data::device later.

Fix by having bap_data hold btd_device_ref(). This is similar to what
avdtp.c etc. do.

Remove no-op assign to ep->data->device in ep_register_bcast() +
bap_bcast_probe() -- these just assign the same device pointer.

Log:

ERROR: AddressSanitizer: heap-use-after-free on address 0x7d6513de0208
READ of size 8 at 0x7d6513de0208 thread T0
    #0 0x5594209b7e9b in device_get_adapter src/device.c:6917
    #1 0x55942086020e in find_cig_session profiles/audio/bap.c:2500
    #2 0x559420a3f070 in queue_find src/shared/queue.c:230
    #3 0x559420866cd3 in find_cig_enumerate profiles/audio/bap.c:2539
    #4 0x559420866cd3 in bap_update_cigs_cb profiles/audio/bap.c:2688
    #5 0x7ff515a46b9a
    #6 0x7ff515a481d6
    #7 0x7ff515a48576 in g_main_loop_run
    #8 0x559420b9af48 in mainloop_run src/shared/mainloop-glib.c:65
    #9 0x559420b9b87a in mainloop_run_with_signal src/shared/mainloop-notify.c:196
    #10 0x5594207192ba in main src/main.c:1713

0x7d6513de0208 is located 392 bytes inside of 752-byte region [0x7d6513de0080,0x7d6513de0370)
freed by thread T0 here:
    #0 0x7ff515d2d3ff in free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:51
    #1 0x559420a29a39 in remove_interface gdbus/object.c:742

previously allocated by thread T0 here:
    #0 0x7ff515d2e40f in calloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:74
    #1 0x5594209cd302 in device_new src/device.c:5008

Assisted-by: opencode:qwen3.8-flash-next
---

Notes:
    This is another test-functional crash seen in CI.  Triaged with LLM,
    reviewed by myself.

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

diff --git a/profiles/audio/bap.c b/profiles/audio/bap.c
index 5771c9ae0..928796e1e 100644
--- a/profiles/audio/bap.c
+++ b/profiles/audio/bap.c
@@ -207,6 +207,9 @@ static void bap_data_free(struct bap_data *data)
 	if (data->cig_update_id)
 		g_source_remove(data->cig_update_id);
 
+	if (data->device)
+		btd_device_unref(data->device);
+
 	free(data);
 }
 
@@ -1785,9 +1788,6 @@ static struct bap_ep *ep_register_bcast(struct bap_data *data,
 	ep->lpac = lpac;
 	ep->rpac = rpac;
 
-	if (device)
-		ep->data->device = device;
-
 	switch (bt_bap_pac_get_type(lpac)) {
 	case BT_BAP_BCAST_SOURCE:
 		err = asprintf(&ep->path, "%s/pac_%s%d",
@@ -3463,7 +3463,8 @@ static struct bap_data *bap_data_new(struct btd_device *device)
 	struct bap_data *data;
 
 	data = new0(struct bap_data, 1);
-	data->device = device;
+	/* Hold reference: sessions may outlive device removal */
+	data->device = device ? btd_device_ref(device) : NULL;
 	data->srcs = queue_new();
 	data->snks = queue_new();
 	data->bcast = queue_new();
@@ -3888,7 +3889,6 @@ static int bap_bcast_probe(struct btd_service *service)
 	data = bap_data_new(device);
 	data->service = service;
 	data->adapter = adapter;
-	data->device = device;
 	data->bap = bap;
 	data->bcast_snks = queue_new();
 
@@ -3985,6 +3985,7 @@ static int bap_probe(struct btd_service *service)
 					btd_device_get_gatt_db(device));
 	if (!data->bap) {
 		error("Unable to create BAP instance");
+		btd_device_unref(data->device);
 		free(data);
 		return -EINVAL;
 	}
-- 
2.55.0


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

end of thread, other threads:[~2026-09-21 14:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-19  0:14 [PATCH BlueZ] bap: Fix use-after-free of device referenced by session Pauli Virtanen
2026-09-19  1:24 ` [BlueZ] " bluez.test.bot
2026-09-21 14:30 ` [PATCH BlueZ] " patchwork-bot+bluetooth

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