From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796211971; b=ubYkcZQemjbJs49OnrPkA8kCPCTcndMzN84EeRWOyazzhpy4NMo8gH/Br+oy4s5EZC0v NmylteXYpvPaaMkK7uXVQM6B8OPe/920uGwqZrcq7wz+CApQGMiovNRU9FTpyymDCLQk9 sW+ZYqjIfBKpM68+k1jvBugySAfv0KCZpEYVBagFaiZ/FuOZO0ARNyq8KvjQ4y9ZWmD6i wkKqwH/2mUxGBQGe4tDeFd7/MEaV0psgVwgsSHkhyY8LjlbJ90E0f4/oX5ao2jBf0jV09 KksxnCndkb1vImEr5RXEm52pGOi/fXZ0adptn1SvtJyKn8uZpHnyLfHA7fFaM8iF5AA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796211971; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Subject:Date: Message-ID:X-Mailer:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=zcGfnzLRU7KOKK3sVnr2NGs/T6oI9S9ZC+7VM9qFwwU=; b=dJBZhInRoETLMdVAs3t5aRe0xYHPayM2guFZKUoC4rsar5x/lkBl8QHHoxyh78ypKZHX c/WJNe1ONE28gI4AcpxFewnkYILi3A3UFtqB/WbOH2IF4E7BAEP2hL/c3Nm2DbDz1ttX6 g0aHXLuzzToUxRxb0gPB2wxL9io1HQokEptVDRr4piZIsyK96JSQ+CNMTUwR9cJ0SkVGw B7GMR/DTl2jV9HA2dXegdOUYeDEjQWFINybZhwCK2pg/nuqRP1GCgcRGX7OwuWImjuR3N 9W6wawhAYcapzWU3M/B0RJ/1e69/OFHEJdv7CvS4V+i2Bj9TeJGVAOfKIaWoD3In2HA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bMtOaMy4; arc=none smtp.remote-ip=209.85.128.46 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CBB49606A31A for ; Wed, 2 Dec 2026 11:46:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CBB49606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=bMtOaMy4 Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-4bb55cda775so78494405e9.0 for ; Wed, 02 Dec 2026 03:46:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796211969; x=1796816769; darn=vger.kernel.org; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:to:from:from:to:cc:subject:date:message-id:reply-to :content-type; bh=zcGfnzLRU7KOKK3sVnr2NGs/T6oI9S9ZC+7VM9qFwwU=; b=bMtOaMy4xv70gukTEHHFjXlDexFcMdl4kHzTa4HVaZ6YEqLrVJsKbYS7FNVLVXUrVv 72QTkGr8O1JW86b2kuQZQeghgsN6N3/OIOOt9pCqEaMio9uQGlzhuLg1ryHdxqSnwg4W l2IT1LNJT0QTS8PZwU5/itnCtJdy4LKFRxIC7ewOETBwTJpdZ5oCfmWoPImxAJjXeXZU u5kjGKLKvNlYeSsTGom+viLjs/oP6Bk1aggnWGDfbhRrYwdg5wywxWbTItd+jO46VVXG kzsuUjmFp9nEZSVjKBOH/NZ7h/sByryURHUgHxwzaQvZKOZEamAqYNetx2ViXlYYlRMj t2Qw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796211969; x=1796816769; h=content-transfer-encoding:content-type:mime-version:message-id:date :subject:to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=zcGfnzLRU7KOKK3sVnr2NGs/T6oI9S9ZC+7VM9qFwwU=; b=V7A3uIN/dbJ2cU15sfPiSSu2VvZF2Af0fRt4DaepCsY2XrrR2A8wX0E/TX7+tkeof6 apk5Mkhm2DeTDpPQEsKg5tsKRd7nxc2Vh7Aw9SasHD2p27X+6++pHfnPslzxXYUwbdO0 Tra8sFxRjJ6Omw1PF/MZX/0AuY9rMUoU1bqae02Haceaqxgqvtz6qmoT9Aocv/CtJe7+ ua0OKXAEdR8uLi/FPiZwfPJnWZDsr4pzc/Q5AKqzHVU7Hk8kx5ILjPcMSPGZXCawVSPB +vfb3uezSfqGN4BLZREzO1K19kiYs13yV3SQI1AMvyJ/Oac+3Zqsq/mSlgT8SJNUePzL 77CA== X-Gm-Message-State: APuU4Z9Teqci8PirHrz+pRGwMPzHMwNllSk2QbPW6CfpusAE9MsbP38I Tj4e4XV4qKlKzDUTFNRZZ9XKp4jz5XiPGqTHkhPTP6VEdGtZg82UUFDd3P8RySUh X-Gm-Gg: AZFdTZedtY406q2J5sSGUXkRwOERrl26DOrh5FFjUfZJoNmGYJtULqzF3J0up8tDLGJ S2r46Ih1M5RI4GqyCEoLgAs1/XlHtQFYr36WBwLPa64pZvvy+UR9Yeb/UX4k1fBLk100e5O6Bk9 Y4xikf6pc8oaX4ANPD0C5xPQstbq2klnu7YbNQXlvpFOZDvK5JjdCv85EjkVRuiD+rVWPmCgV58 zSiyu0uIELIXm4/D+XhAtdhDtM+A/mxUc0C0JGJbirNESpVU/LJm3muI/guASx/qqtfP/8wYDfL 9/BlUZCTkp667kNtNsdTev/35IhVLFN2EVa9P6kMzzn+i2jNgBGFZgHueZOmu13zi/f/OaBmZcG KAhg6tCeTLLRpzdJu4QFss/D/RQukIIrIb5QNCRSzIUXeywMU0bY+jFagoiGH0BVYWFWiFw8JiT Ocl5Lijtrye7VgLxUyY4EHm57sAqlNGaYp/UidPVpuD37gJ+inQrjHwbWSN9gmfU29NbZnIfNhF bfu39BuZJN+DY5B3fkvDeTy0zPytOrkB1Iu4Gu9I9+a9rO9CX8m2POb6eHnpQeqRp4CQ6WTYyBB Tk3g2oHQxvNa/RQH+FthI0kq3mq7F7LsuU12HqBWzpgd2eYzD6X2oKpDzliBXanC34E= X-Received: by 2002:a05:600c:4e94:b0:4bb:b796:66f5 with SMTP id 5b1f17b1804b1-4bbb79667d8mr337686465e9.6.1796211968803; Wed, 02 Dec 2026 03:46:08 -0800 (PST) Received: from fdanis-tpp14s (67.227.121.78.rev.sfr.net. [78.121.227.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb853a1e8bsm887702635e9.9.2026.12.02.03.46.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 03:46:08 -0800 (PST) From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Danis?= To: linux-bluetooth@vger.kernel.org Subject: [PATCH BlueZ v2] audio/hfp-hf: Destroy MediaTransport1 on codec change/disconnect Date: Wed, 2 Dec 2026 12:46:07 +0100 Message-ID: <20261202114607.126342-1-frederic.danis.oss@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit - hfp_hf_clear_configuration() (mirrors a2dp's clear_config()) destroys a codec's MediaTransport1. Call it from hfp_hf_select_codec() on codec change (previous codec) and from device_destroy() (current codec), via new find_server_endpoint() helper. Previously the transport was only left IDLE, so it stayed in e.g. `bluetoothctl transport.list` after a call ended or the device disconnected. - Fix use-after-free: transport_hfp_cancel() called g_source_remove() on the id from hfp_hf_sco_start(), but that id is dev->resume_id, an internal counter, not a GLib source id, so it never detached the in-flight SCO connect/codec negotiation from its owner. Clearing a previous codec's transport could then free that owner while the connect was still pending, leaving a dangling pointer for the completion callback to dereference. Track dev->pending_connect across all hfp_hf_sco_start() paths and add hfp_hf_sco_cancel() to detach the callback from it when the id matches. transport_hfp_cancel() tries this first, then falls back to g_source_remove() for the suspend-path id. - sco_start_cb() now always frees connect_data, not only when its callback is set, fixing a leak the above introduces. - dev->resume_id/pending_connect are shared by every codec's transport of a device, so a pending resume's id can collide with an unrelated sibling transport's suspend id. hfp_hf_sco_start()/hfp_hf_sco_cancel() take a resume_token (the transport pointer) to disambiguate; the transport pointer is used instead of transport->owner because media_transport_remove_owner() clears transport->owner before freeing the owner. - Guard find_server_endpoint() against a NULL server, and make hfp_hf_select_codec() bail out with an error if find_server() finds none. Assisted-by: Copilot:claude-sonnet-5 --- v1->v2: - Fix use-after-free on codec change: cancelling a pending SCO Acquire only removed a GLib source for an id that was never one, so the in-flight connect's callback could dereference a freed owner. Track dev->pending_connect and add hfp_hf_sco_cancel() to detach it instead. - Disambiguate cancellation with a resume_token (the transport pointer), since dev->resume_id/pending_connect are shared by every codec transport of a device. - Fix sco_start_cb() to always free connect_data. - Guard find_server_endpoint() and hfp_hf_select_codec() against a NULL server. profiles/audio/hfp-hf.c | 113 +++++++++++++++++++++++++++++++------ profiles/audio/hfp-hf.h | 4 +- profiles/audio/media.c | 15 +++++ profiles/audio/media.h | 2 + profiles/audio/transport.c | 32 +++++++++-- 5 files changed, 143 insertions(+), 23 deletions(-) diff --git a/profiles/audio/hfp-hf.c b/profiles/audio/hfp-hf.c index bc6d7229f..855fc206b 100644 --- a/profiles/audio/hfp-hf.c +++ b/profiles/audio/hfp-hf.c @@ -76,6 +76,7 @@ struct connect_data { struct hfp_device *dev; void (*cb)(int status, void *data); void *cb_user_data; + const void *resume_token; }; struct hfp_device { @@ -138,6 +139,22 @@ static struct hfp_server *find_server(GSList *list, struct btd_adapter *a) return NULL; } +static struct media_endpoint *find_server_endpoint(struct hfp_server *server, + uint8_t codec) +{ + GSList *l; + + if (!server) + return NULL; + + for (l = server->endpoints; l; l = l->next) { + if (media_endpoint_get_codec(l->data) == codec) + return l->data; + } + + return NULL; +} + static void unregister_endpoint(gpointer data, gpointer user_data) { struct media_endpoint *ep = data; @@ -248,9 +265,21 @@ static void device_destroy(struct hfp_device *dev) server = find_server(servers, device_get_adapter(telephony_get_device(dev->telephony))); - if (server) + if (server) { + struct media_endpoint *ep; + + /* Destroy the MediaTransport1 created for this device's SCO + * audio, if any, so it does not linger after the device is + * gone. + */ + ep = find_server_endpoint(server, dev->codec); + if (ep) + hfp_hf_clear_configuration(ep, + telephony_get_device(dev->telephony)); + g_slist_foreach(server->endpoints, unregister_endpoint, dev->telephony); + } telephony_unregister_interface(dev->telephony); } @@ -422,21 +451,38 @@ static bool hfp_hf_select_codec(uint8_t codec, void *user_data) struct hfp_device *dev = user_data; struct btd_adapter *adapter; struct hfp_server *server; - GSList *l; + struct media_endpoint *ep; DBG("Selecting codec: %u", codec); adapter = device_get_adapter(telephony_get_device(dev->telephony)); server = find_server(servers, adapter); + if (!server) { + error("No server found for adapter"); + return false; + } - for (l = server->endpoints; l; l = l->next) { - if (media_endpoint_get_codec(l->data) == codec) { - dev->codec = codec; - return true; - } + ep = find_server_endpoint(server, codec); + if (!ep) { + DBG("Unsupported codec: %u", codec); + return false; } - DBG("Unsupported codec: %u", codec); - return false; + if (dev->codec != codec) { + /* A device only ever has one SCO connection at a time, so + * the previous codec's transport, if any, is no longer + * valid. + */ + struct media_endpoint *prev; + + prev = find_server_endpoint(server, dev->codec); + if (prev) + hfp_hf_clear_configuration(prev, + telephony_get_device(dev->telephony)); + } + + dev->codec = codec; + + return true; } static struct hfp_hf_callbacks hf_session_callbacks = { @@ -1359,15 +1405,19 @@ static gboolean sco_start_cb(gpointer data) { struct connect_data *connect_data = data; - if (connect_data && connect_data->cb) { + if (connect_data->dev->pending_connect == connect_data) + connect_data->dev->pending_connect = NULL; + + if (connect_data->cb) connect_data->cb(0, connect_data->cb_user_data); - g_free(connect_data); - } + + g_free(connect_data); return FALSE; } -unsigned int hfp_hf_sco_start(struct hfp_device *dev, void *cb, void *user_data) +unsigned int hfp_hf_sco_start(struct hfp_device *dev, void *cb, + void *user_data, const void *resume_token) { bdaddr_t src, dst; struct connect_data *connect_data; @@ -1385,16 +1435,16 @@ unsigned int hfp_hf_sco_start(struct hfp_device *dev, void *cb, void *user_data) connect_data->dev = dev; connect_data->cb = cb; connect_data->cb_user_data = user_data; + connect_data->resume_token = resume_token; + dev->pending_connect = connect_data; src = telephony_get_src(dev->telephony); dst = telephony_get_dst(dev->telephony); if (!dev->sco_io) { if (hfp_hf_request_codec_connection(dev->hf, req_codec_connection_complete, - connect_data)) { - dev->pending_connect = connect_data; + connect_data)) goto done; - } io = bt_io_connect(sco_connect_cb, connect_data, NULL, &err, BT_IO_OPT_SOURCE_BDADDR, &src, @@ -1404,6 +1454,7 @@ unsigned int hfp_hf_sco_start(struct hfp_device *dev, void *cb, void *user_data) if (!io) { error("%s", err->message); g_error_free(err); + dev->pending_connect = NULL; g_free(connect_data); return 0; } @@ -1415,6 +1466,36 @@ done: return (++dev->resume_id); } +bool hfp_hf_sco_cancel(struct hfp_device *dev, unsigned int id, + const void *resume_token) +{ + if (!dev->pending_connect || dev->resume_id != id) + return false; + + /* + * dev->resume_id is a device-wide counter shared by every codec's + * transport, while id may also be an unrelated g_idle_add() source + * id from a sibling transport's pending suspend. Require the caller + * to also present the same resume_token that was passed to + * hfp_hf_sco_start(), so a numeric collision between the two id + * spaces can never cause this resume to be mistaken for that + * suspend (or vice versa). + */ + if (dev->pending_connect->resume_token != resume_token) + return false; + + /* + * The underlying SCO connect/codec negotiation cannot be aborted + * cleanly, so just detach the callback: the operation still runs + * to completion, but will no longer touch the (about to be freed) + * owner once hfp_hf_sco_start() handed out this id. + */ + dev->pending_connect->cb = NULL; + dev->pending_connect->cb_user_data = NULL; + + return true; +} + unsigned int hfp_hf_sco_stop(struct hfp_device *dev) { if (!dev->sco_io) diff --git a/profiles/audio/hfp-hf.h b/profiles/audio/hfp-hf.h index 6dfa3b12f..b4daa9b21 100644 --- a/profiles/audio/hfp-hf.h +++ b/profiles/audio/hfp-hf.h @@ -15,8 +15,10 @@ typedef void (*hfp_hf_sco_closed_cb) (struct hfp_device *dev, void *user_data); bool hfp_hf_sco_listen(struct btd_adapter *adapter, void *endpoint); void hfp_hf_sco_remove(struct btd_adapter *adapter, void *endpoint); unsigned int hfp_hf_sco_start(struct hfp_device *dev, void *cb, - void *user_data); + void *user_data, const void *resume_token); unsigned int hfp_hf_sco_stop(struct hfp_device *dev); +bool hfp_hf_sco_cancel(struct hfp_device *dev, unsigned int id, + const void *resume_token); void hfp_hf_set_sco_closed_cb(struct hfp_device *dev, hfp_hf_sco_closed_cb cb, void *user_data); diff --git a/profiles/audio/media.c b/profiles/audio/media.c index 13fead886..f1c8906e4 100644 --- a/profiles/audio/media.c +++ b/profiles/audio/media.c @@ -1501,6 +1501,21 @@ gboolean hfp_hf_set_configuration(struct media_endpoint *endpoint, cb, user_data, destroy, -1); } +void hfp_hf_clear_configuration(struct media_endpoint *endpoint, + struct btd_device *device) +{ + struct media_transport *transport; + + if (!endpoint) + return; + + transport = find_device_transport(endpoint, device); + if (!transport) + return; + + clear_configuration(endpoint, transport); +} + static bool endpoint_init_sco(struct media_endpoint *endpoint, int *err) { diff --git a/profiles/audio/media.h b/profiles/audio/media.h index eb95a8b06..c20238f1a 100644 --- a/profiles/audio/media.h +++ b/profiles/audio/media.h @@ -89,3 +89,5 @@ gboolean hfp_hf_set_configuration(struct media_endpoint *endpoint, media_endpoint_cb_t cb, void *user_data, GDestroyNotify destroy); +void hfp_hf_clear_configuration(struct media_endpoint *endpoint, + struct btd_device *device); diff --git a/profiles/audio/transport.c b/profiles/audio/transport.c index ad2fc2c1c..61de14ce0 100644 --- a/profiles/audio/transport.c +++ b/profiles/audio/transport.c @@ -2887,7 +2887,8 @@ static guint transport_hfp_resume(struct media_transport *transport, { struct hfp_device *hfp_dev = transport->data; - return hfp_hf_sco_start(hfp_dev, hfp_transport_resume_cb, owner); + return hfp_hf_sco_start(hfp_dev, hfp_transport_resume_cb, owner, + transport); } static gboolean hfp_transport_suspend_cb(void *user_data) @@ -2937,12 +2938,31 @@ static guint transport_hfp_suspend(struct media_transport *transport, static void transport_hfp_cancel(struct media_transport *transport, guint id) { - /* id is the g_idle_add() source id returned by transport_hfp_suspend() - * for its still-pending hfp_transport_suspend_cb() callback. Cancel it - * so it can never fire with a dangling owner pointer if owner is torn - * down through another path first. + struct hfp_device *hfp_dev = transport->data; + + /* + * id can be either a dev->resume_id handed out by hfp_hf_sco_start() + * for a still-pending resume, or the g_idle_add() source id returned + * by transport_hfp_suspend() for its still-pending + * hfp_transport_suspend_cb() callback. Since dev->resume_id is + * shared by every codec transport of this device, it can collide + * numerically with an unrelated sibling transport's suspend id; + * hfp_hf_sco_cancel() also checks that the resume_token matches + * this transport (the token hfp_hf_sco_start() was given when the + * pending resume was started) before treating it as a match. transport + * is always the transport whose owner is being cancelled (passed + * down fresh from media_owner_remove()'s owner->transport), unlike + * transport->owner, which may already have been cleared by the time + * this runs (e.g. via media_transport_remove_owner()). Try the + * resume case first; hfp_hf_sco_cancel() only detaches the callback + * (the id space isn't a GLib source id, and the in-flight SCO + * connect/codec negotiation cannot be aborted), so the owner can be + * freed safely afterwards. Otherwise fall back to cancelling the + * suspend idle source, so it never fires with a dangling owner + * pointer if owner is torn down through another path first. */ - g_source_remove(id); + if (!hfp_hf_sco_cancel(hfp_dev, id, transport)) + g_source_remove(id); } #endif /* HAVE_HFP */ -- 2.56.1