From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1054392836 for ; Sat, 25 Jul 2026 19:52:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.42 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785009163; cv=none; b=FcUb5yqolW1j1CrEzVstqO+3hEJk+RdyC0XMDwgA5NJr5rusR6MnZL0nAvz9Yx9sFu2GwrhhBSLa5AjUBEgjVcqHHM2ExwjWQa9lds7qjM+I5PHz4eAQQInFzRB9cIS1GkzfNw54L0hW+5s4u02JwmNjHzomVjr54Grt44NwiTg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785009163; c=relaxed/simple; bh=sQUfWYbBbGAFGnb9f4oxJ1jqLUWP/syj4X/dNhx4/rE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JUtfegdtmtiNX5sfSq+o/E5nu0BarPJ5AmckpBB5YvPYijGQ7M+kuX4TMnmudsc7NiMaTWMqkOlCdlB4314OS5tdlZhLTBpxmOtEWZmCuXZ65gJNBoF8lt4yUXEvXak+vI9JAq8ocrV0ra9K8bM10i0cvRkqui8H0RWdyt8aCjc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Pw5bWoRN; arc=none smtp.client-ip=209.85.210.42 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) header.d=gmail.com header.i=@gmail.com header.b="Pw5bWoRN" Received: by mail-ot1-f42.google.com with SMTP id 46e09a7af769-7e9d7464b71so590258a34.0 for ; Sat, 25 Jul 2026 12:52:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785009161; x=1785613961; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=oPEwWoUe7xfClrZacEsfK1+k1qltAEaeL9m2pmWgCQ0=; b=Pw5bWoRNv/lKoBRTxOA4e63rbToJ44cYju8RzfV+guIY5V+NBDOMx+ZoJlWJDXAcbZ YRbbCJ0ESe5yINkmV1C+lDIHZbPhi6i0Q/94I7i7fkX/KYqZcfmFpg4luvE1y23iwINE 1hM5VRNMMxHEo94b7AluKBV4Wzxoe7s+0lNi4fHT0EY2oNfdFjZPuUJj1GmLKMrOa3DP JCT2qZTNxfMGdCVKGsjjVPRFAz04F61owMpkKaMcqtQVgkL65qWazV33uVbLb8iIghHO 9/THNylmLd9bU3rdTVaNQTt1eMhirLf+tIOrimSGedm+XL36uigntDtCcAb7B2K3YVyD pxIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785009161; x=1785613961; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=oPEwWoUe7xfClrZacEsfK1+k1qltAEaeL9m2pmWgCQ0=; b=OwKlFxMTyalBZt3lggPpy8QK5AuG5RReLbk5k8w/HZTOr3aH/uUxCSCmhqhs7OgVnq MnVKPaejfjiuGXq63PUjFYz4W13gaZOkO+rzpA1HFvmbC5Qaf4XFPKfZZupfbvmn87ab BuJJIT9291+cChqVtBXgcxLwaloS7LXodHM9YRQ0gxFw2UbyZQc8nmh96KPDWjkeq5Fa mBFRtO8xLiyvWy+ulwhIsZ8TivzNQcHrQBH20oxJBafxdTXslip7Ij/hFXMOJd34lqEf FvmkzkRVxXr4Q6ygWcVtG6V95i7rbHsaywBacMsOgTNQqNsm1gO9jwg6X/krZ/jQzuZr zU4w== X-Forwarded-Encrypted: i=1; AHgh+Ro0PNf1Ealxz2Ud5yN+GrLCXrz2OX/nxxPA5ADemEt5R7Ej7KUmWaGy1oKBpcFm7cU1hZUvdv21tnHURzUgztM=@vger.kernel.org X-Gm-Message-State: AOJu0YxN8RKI2MaCg7AkHD12fnw3UWa5zM3yLwLuM0u2iCAbOW+3vIWY k6G1TBNS/SPzNN4YCMcFc6KjdJExkD3rIG7hn8YnWuhxv/jpYl7pVwpVbBl/FuAOayNnGWSe X-Gm-Gg: AR+sD12h2OvnIf9GIs5kmnOtbsF9/ytzVSImbIeUZr66aXO0U3B47QQISEW1x8dPwNt 74/DFzgv4Ind4uNM2qpIYsnS+a0WMcFoC4XONJuI9Of2SEhqEfB1RebyUgyyLBC0Sf9srQzMPac wM+QqZ8mC/h5Rsm30R8k9+YD2lRqN9tTWhB8j1QjcNyT6MR3r410nw1cPZUK4jfnwY2JDoENmdn kojF4iEv2zxMRTayZdAf1eRsCzDVHdupLoarzY91iNKjXaPHmG8MGNC+sJo0ClRiIjbncAGiFi4 l1w4v5j9/TGp7n1MOt8wUrKUve57lxZObGOuWZK5xRVMlfbPOG2hqi/7DimuqLZFl35QUR/5e2d dPC6T99w5KJ68TqD1IvYQT0BlGJR2B59e0mSHXbTdoNKlDy5MFYurPsShyCIbi2QJjNyqyrnS X-Received: by 2002:a05:6830:4181:b0:7e7:76f:3ec0 with SMTP id 46e09a7af769-7ee78c77bc8mr2670398a34.15.1785009160499; Sat, 25 Jul 2026 12:52:40 -0700 (PDT) Received: from beelink.. ([155.133.15.206]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-7ee29e0ec5bsm8837163a34.10.2026.07.25.12.52.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Jul 2026 12:52:40 -0700 (PDT) From: Aldo Ariel Panzardo To: Pauli Virtanen , Luiz Augusto von Dentz Cc: marcel@holtmann.org, linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, Aldo Ariel Panzardo Subject: [PATCH v3] Bluetooth: SCO: give the socket its own sco_conn reference Date: Sat, 25 Jul 2026 16:52:30 -0300 Message-ID: <20260725195230.967546-1-qwe.aldo@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit sco_conn_del() drops a reference it does not own. It takes one transient reference via sco_conn_hold_unless_zero() and releases it with the sco_conn_put() that follows sco_sock_hold(); the additional put in the !sk branch releases a second one: conn = sco_conn_hold_unless_zero(conn); ... sk = sco_sock_hold(conn); sco_conn_unlock(conn); sco_conn_put(conn); if (!sk) { sco_conn_put(conn); return; } When close() races the controller's Disconnection Complete, sco_chan_del() clears conn->sk and drops the socket's reference while sco_conn_del() is running. sco_conn_del() then sees sk == NULL, its own put drops the count to zero and frees the conn, and the second put writes to the freed kref: BUG: KASAN: slab-use-after-free in sco_conn_put.part.0+0x1a/0x190 Write of size 4 at addr ffff8881099dec74 by task kworker/u17:3/413 Workqueue: hci1 hci_rx_work Call Trace: sco_conn_put.part.0+0x1a/0x190 hci_disconn_complete_evt+0x1ee/0x3e0 hci_event_packet+0x54a/0x650 hci_rx_work+0x321/0x3d0 Allocated by task 413: sco_conn_add+0x72/0x1a0 sco_connect_cfm+0x88/0x670 Freed by task 413: sco_conn_del.isra.0+0x3f/0xf0 hci_disconn_complete_evt+0x1ee/0x3e0 refcount_t: underflow; use-after-free. The root cause is that the socket stores the connection without holding a reference of its own. __sco_chan_add() does: sco_pi(sk)->conn = conn; so the socket borrows whatever reference its caller happened to hold, and the callers paper over that with ad-hoc holds and puts. Give the socket a counted reference instead: __sco_chan_add() takes one and it is released together with the channel (sco_chan_del()) and in sco_sock_destruct(). With the socket holding its own reference, sco_conn_del() no longer needs the extra put and the redundant hold in sco_conn_ready() goes away. Making the socket own its reference means the connection is now actually freed on the error paths of sco_connect() where it used to leak, which in turn runs sco_conn_free() and its hci_conn_drop(conn->hcon). To keep the hci_conn accounting balanced, make that ownership explicit as well: sco_conn_add() consumes one hci_conn reference and the sco_conn owns it for its lifetime. sco_connect() hands over the reference returned by hci_connect_sco() and no longer drops it on the error paths; sco_connect_cfm(), which is not given a reference, takes one with hci_conn_hold() before handing it to sco_conn_add() (and drops it again if the allocation fails); and the explicit hci_conn_hold() in sco_conn_ready() is removed. Every reference then has a single, clear owner. Fixes: e6720779ae61 ("Bluetooth: SCO: Use kref to track lifetime of sco_conn") Cc: stable@vger.kernel.org Suggested-by: Pauli Virtanen Signed-off-by: Aldo Ariel Panzardo --- v3: - Incorporate Pauli Virtanen's review: make sco_conn own one hci_conn reference for its whole lifetime -- sco_conn_add() consumes an hci_conn reference, sco_connect() no longer drops hcon on its error paths, sco_connect_cfm() holds one before sco_conn_add(), and the hci_conn_hold() in sco_conn_ready() is removed. This avoids the double hci_conn_drop() on sco_connect()'s error paths that v2 would otherwise introduce. - Keep the sco_pi(conn->sk)->conn = NULL clearing in sco_conn_free(). v2 removed it as unreachable, but KASAN testing of close() racing the Disconnection Complete showed that dropping it reintroduces a use-after-free on the sco_sock_release() path, so it is retained. - Drop the hcon reference in sco_connect_cfm() when sco_conn_add() fails, so the allocation-failure path does not leak it. v2: - Make the socket own its sco_conn reference rather than only deleting the extra put, per Pauli Virtanen's review. Testing: on v7.2-rc4 with KASAN and a /dev/vhci reproducer that races close() of an SCO socket against an injected Disconnection Complete, the unpatched kernel hits the refcount_t underflow / use-after-free above within a few thousand iterations; with this patch the sco_conn_del() over-put on the Disconnection Complete path no longer reproduces. net/bluetooth/sco.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c index fcc597be5bbd..aa9f61a748ab 100644 --- a/net/bluetooth/sco.c +++ b/net/bluetooth/sco.c @@ -188,6 +188,9 @@ static void sco_sock_clear_timer(struct sock *sk) } /* ---- SCO connections ---- */ +/* Consumes a reference on @hcon, which the returned sco_conn owns until it is + * freed. On failure (NULL return) the reference is left for the caller to drop. + */ static struct sco_conn *sco_conn_add(struct hci_conn *hcon) { struct sco_conn *conn = hcon->sco_data; @@ -198,6 +201,9 @@ static struct sco_conn *sco_conn_add(struct hci_conn *hcon) sco_conn_lock(conn); conn->hcon = hcon; sco_conn_unlock(conn); + } else { + /* conn already owns a reference on hcon */ + hci_conn_drop(hcon); } return conn; } @@ -265,10 +271,8 @@ static void sco_conn_del(struct hci_conn *hcon, int err) sco_conn_unlock(conn); sco_conn_put(conn); - if (!sk) { - sco_conn_put(conn); + if (!sk) return; - } /* Kill socket */ lock_sock(sk); @@ -283,7 +287,7 @@ static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, { BT_DBG("conn %p", conn); - sco_pi(sk)->conn = conn; + sco_pi(sk)->conn = sco_conn_hold(conn); conn->sk = sk; if (parent) @@ -366,15 +370,15 @@ static int sco_connect(struct sock *sk) */ if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) { release_sock(sk); - hci_conn_drop(hcon); + sco_conn_put(conn); err = -EBADFD; goto unlock; } err = sco_chan_add(conn, sk, NULL); + sco_conn_put(conn); if (err) { release_sock(sk); - hci_conn_drop(hcon); goto unlock; } @@ -1439,8 +1443,6 @@ static void sco_conn_ready(struct sco_conn *conn) bacpy(&sco_pi(sk)->src, &conn->hcon->src); bacpy(&sco_pi(sk)->dst, &conn->hcon->dst); - sco_conn_hold(conn); - hci_conn_hold(conn->hcon); __sco_chan_add(conn, sk, parent); if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags)) @@ -1496,10 +1498,12 @@ static void sco_connect_cfm(struct hci_conn *hcon, __u8 status) if (!status) { struct sco_conn *conn; - conn = sco_conn_add(hcon); + conn = sco_conn_add(hci_conn_hold(hcon)); if (conn) { sco_conn_ready(conn); sco_conn_put(conn); + } else { + hci_conn_drop(hcon); } } else sco_conn_del(hcon, bt_to_errno(status)); -- 2.43.0