Skip to contentSkip to conversation

shared/bap: Remove broadcast sink PACs in bt_bap_remove_pac()

PATCH Superseded Simon Mikuda 2 participants 5 messages Nov 22 Bluetooth drivers

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 shared/bap: Remove broadcast sink PACs in bt_bap_remove_pac()

bt_bap_remove_pac() looked for the PAC in the sink, source and broadcast source lists only, so a broadcast sink PAC stayed in broadcast_sinks after its endpoint was freed. When the endpoint owner goes away, like WirePlumber restarting, the next BIGInfo matches the stale PAC and bluetoothd crashes:

pac_bcast_config (endpoint=<freed>) at profiles/audio/media.c pac_config () at profiles/audio/media.c bap_bcast_config () at src/shared/bap.c bt_bap_stream_config () at src/shared/bap.c create_stream_for_bis () at profiles/audio/bap.c bis_handler () at profiles/audio/bap.c bt_bap_parse_base () at src/shared/bap.c big_info_report_cb () at profiles/audio/bap.c

Fixes d49ea14f70da ("shared/bap: Add support for BAP broadcast sink")
Assisted-by Claude:claude-opus-5-5
---
 src/shared/bap.c | 3 +++
 1 file changed, 3 insertions(+)
src/shared/bap.c +3 -0
index 8fc28de52..9404bddba 100644
@@ -4864,6 +4864,9 @@ bool bt_bap_remove_pac(struct bt_bap_pac *pac)
 	if (queue_remove_if(pac->bdb->broadcast_sources, NULL, pac))
 		goto found;
 
+	if (queue_remove_if(pac->bdb->broadcast_sinks, NULL, pac))
+		goto found;
+
 	return false;
 
 found:

base-commit: 1adfe662efeff12997df9ddd0960dbd0de9c8ea7

Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/4 unit/test-bap: Check that a broadcast sink PAC can be removed

bt_bap_remove_pac() did not find a broadcast sink PAC, which left the PAC of an unregistered endpoint in use. Check in the teardown of every test that registers one that its removal succeeds.

Assisted-by Claude:claude-opus-5-5
---
 unit/test-bap.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
unit/test-bap.c +4 -1
index bfe98a885..9508c503e 100644
@@ -1307,7 +1307,10 @@ static void test_teardown(const void *user_data)
 	bt_bap_remove_pac(data->snk);
 	bt_bap_remove_pac(data->src);
 	bt_bap_remove_pac(data->bsrc);
-	bt_bap_remove_pac(data->bsnk);
+
+	if (data->bsnk)
+		g_assert(bt_bap_remove_pac(data->bsnk));
+
 	gatt_db_unref(data->db);
 	bt_gatt_server_unref(data->server);
 	data->server = NULL;
Signature · 1 line
2.43.0
#2 lore raw

PATCH 3/4 bap: Don't leak the io of a PA sync that failed

In reply to Simon Mikuda

From: Simon Mikuda <[email protected]>

The kernel reports a PA sync that times out twice. iso_pa_sync_confirm_cb() replaced data->io_id for the second io, so the watch of the first one stayed, holding the last reference of its io and keeping the failed sync in the kernel. Where PA syncs are BIS connections on BIG 0, a broadcast source on BIG 0 then fails to bind with EADDRINUSE until bluetoothd restarts.

Remove the previous watch, which also closes its io.

Fixes 2748c60a2c6b ("bap: Wait for BIG Info report event before creating streams")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/bap.c | 7 +++++++
 1 file changed, 7 insertions(+)
profiles/audio/bap.c +7 -0
index 457ba5de8..5e717cbef 100644
@@ -1896,6 +1896,13 @@ static void iso_pa_sync_confirm_cb(GIOChannel *io, void *user_data)
 	 * encryption flag is also available.
 	 */
 	DBG("PA Sync done");
+
+	/* A failed PA sync is reported twice. Dropping the previous watch also
+	 * closes its io, as the watch holds the last reference.
+	 */
+	if (data->io_id)
+		g_source_remove(data->io_id);
+
 	data->io_id = g_io_add_watch(io, G_IO_OUT, big_info_report_cb,
 								user_data);
 }
Signature · 1 line
2.43.0
#3 lore raw

PATCH 4/4 bap: Close a pending PA or BIG sync when a broadcast sink session ends

In reply to Simon Mikuda

bap_bcast_disconnect() frees the broadcast sink setups, but listen_io stays open with a callback on one of them. A PA sync that completes after the session ended crashes bluetoothd:

    #4  iso_do_big_sync (io=0xaaaad1ab3a30, user_data=0xaaaad1a87110)
        at profiles/audio/bap.c:3689
    #5  server_cb (io=0xaaaad1aabd30, cond=<optimized out>,
        user_data=0xaaaad1ad1310) at btio/btio.c:285

Close listen_io before freeing the setups.

Fixes feb4ee9dcd4b ("bap: Fix use-after-free in broadcast sink cleanup")
Assisted-by Claude:claude-opus-5-5
---
 profiles/audio/bap.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
profiles/audio/bap.c +10 -0
index 5e717cbef..af532537b 100644
@@ -4284,6 +4284,16 @@ static int bap_bcast_disconnect(struct btd_service *service)
 		error("BAP service not handled by profile");
 		return -EINVAL;
 	}
+
+	/* A pending PA or BIG sync calls back with one of the setups freed
+	 * below, close it.
+	 */
+	if (data->listen_io) {
+		g_io_channel_shutdown(data->listen_io, TRUE, NULL);
+		g_io_channel_unref(data->listen_io);
+		data->listen_io = NULL;
+	}
+
 	/* Clean up broadcast sinks before detach (like unicast does) */
 	if (data->bcast_snks) {
 		bcast_snks = data->bcast_snks;
Signature · 1 line
2.43.0
#4 lore raw
In reply to Simon Mikuda

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1218594/

---Test result---

Test Summary:

CheckPatch                    PASS      2.01 seconds
GitLint                       FAIL      1.40 seconds
BuildEll                      PASS      21.50 seconds
BluezMake                     PASS      321.48 seconds
MakeCheck                     PASS      14.99 seconds
MakeDistcheck                 PASS      154.29 seconds
CheckValgrind                 PASS      361.07 seconds
CheckSmatch                   WARNING   352.83 seconds
bluezmakeextell               PASS      102.40 seconds
TestFunctional                FAIL      1306.56 seconds
IncrementalBuild              PASS      354.59 seconds
ScanBuild                     PASS      1252.88 seconds

Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[BlueZ,4/4] bap: Close a pending PA or BIG sync when a broadcast sink session ends

1: T1 Title exceeds max length (82>80): "[BlueZ,4/4] bap: Close a pending PA or BIG sync when a broadcast sink session ends"
##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
src/shared/bap.c:317:25: warning: array of flexible structuressrc/shared/bap.c: note: in included file:./src/shared/ascs.h:88:25: warning: array of flexible structuressrc/shared/bap.c:317:25: warning: array of flexible structuressrc/shared/bap.c: note: in included file:./src/shared/ascs.h:88:25: warning: array of flexible structuressrc/shared/bap.c:317:25: warning: array of flexible structuressrc/shared/bap.c: note: in included file:./src/shared/ascs.h:88:25: warning: array of flexible structures
##############################
Test: TestFunctional - FAIL
Desc: Run test-functional
Output:
FAIL functional.test_bap::test_bass_past_earbuds_transport_acquire[hosts11-vm3]: AssertionError: Failed to acquire: org.bluez.Error.Failed

https://github.com/bluez/bluez/pull/2875

---
Regards,
Linux Bluetooth