From: Ali Firas <alishmery18@gmail.com>
To: luiz.dentz@gmail.com, marcel@holtmann.org
Cc: linux-bluetooth@vger.kernel.org, linux-kernel@vger.kernel.org,
iam@sung-woo.kim, pav@iki.fi, stable@vger.kernel.org
Subject: [PATCH v2] Bluetooth: SCO: avoid deadlock in sco_conn_free()
Date: Sun, 6 Dec 2026 03:32:03 +0300 [thread overview]
Message-ID: <20261206003203.76893-1-alishmery18@gmail.com> (raw)
sco_conn_free(), the struct sco_conn kref release, cancels the connection
timeout work synchronously with disable_delayed_work_sync(). It runs in
whatever lock context drops the last reference. sco_sock_timeout(), the
work it cancels, takes lock_sock(sk). When the last reference is dropped
under lock_sock(sk) while the work is running, disable_delayed_work_sync()
waits for the work and the work waits for the socket lock: an AB/BA
deadlock.
Commit 0f8a5dcc6664 ("Bluetooth: sco: Fix a race condition in
sco_sock_timeout()") moved the cancel out of lock_sock() on the
sco_sock_close() path. sco_conn_del() still drops the last reference under
the socket lock: it holds lock_sock(sk) across sco_chan_del(), which
releases the socket's sco_conn reference, and on the teardown paths that is
the last one. This happens on a failed outgoing link, shown here as
hci_conn_complete_evt() -> hci_sco_setup() -> sco_connect_cfm() ->
sco_conn_del(). The Disconnection Complete path (sco_disconn_cfm()) and
sco_sock_shutdown() reach sco_conn_free() under lock_sock(sk) the same way,
by inspection.
This is reachable by an unprivileged local user. The stuck hci_rx_work
holds hdev->lock and hci_cb_list_lock, so Bluetooth stays unusable until
reboot.
On a PROVE_LOCKING kernel (bluetooth-next, 7.3.0-18264-gf57f78086ab5)
lockdep reports:
WARNING: possible circular locking dependency detected
kworker/u9:0 is trying to acquire lock:
((work_completion)(&(&conn->timeout_work)->work)), at: __flush_work
but task is already holding lock:
(sk_lock-AF_BLUETOOTH-BTPROTO_SCO), at: sco_conn_del
-> #1 (sk_lock-AF_BLUETOOTH-BTPROTO_SCO):
lock_sock_nested
sco_sock_timeout
process_one_work
-> #0 ((work_completion)(&(&conn->timeout_work)->work)):
__flush_work
disable_delayed_work_sync
sco_conn_put
sco_chan_del
sco_conn_del
sco_connect_cfm
hci_sco_setup
hci_conn_complete_evt
hci_event_packet
hci_rx_work
process_one_work
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(sk_lock-AF_BLUETOOTH-BTPROTO_SCO);
lock((work_completion)(&(&conn->timeout_work)->work));
lock(sk_lock-AF_BLUETOOTH-BTPROTO_SCO);
lock((work_completion)(&(&conn->timeout_work)->work));
*** DEADLOCK ***
locks held by kworker/u9:0:
#2 (&hdev->lock), at: hci_conn_complete_evt
#3 (hci_cb_list_lock), at: hci_sco_setup
#4 (sk_lock-AF_BLUETOOTH-BTPROTO_SCO), at: sco_conn_del
Workqueue: hci0 hci_rx_work
Fix it the way the ISO transport did in commit 200fa1629c57 ("Bluetooth:
ISO: avoid deadlocks in iso_sock_timeout"): move timeout_work out of the
refcounted struct sco_conn, which is freed under the socket lock, and into
struct sco_pinfo, so its lifetime follows the socket. sco_conn_free() no
longer cancels the work; sco_sock_disable_timer() does the synchronous
cancel outside lock_sock() in sco_conn_del(), sco_sock_kill() and
sco_sock_close(), and asserts the socket lock is not held.
Pauli Virtanen proposed moving timeout_work into sco_pinfo in an RFC
("Bluetooth: SCO: remove sco_conn and simplify locking scheme").
Reproducer available on request.
Fixes: e6720779ae61 ("Bluetooth: SCO: Use kref to track lifetime of sco_conn")
Cc: stable@vger.kernel.org
Signed-off-by: Ali Firas <alishmery18@gmail.com>
---
Notes:
v2 base: bluetooth-next f57f78086ab5
v1: https://patchwork.kernel.org/project/bluetooth/list/?series=1149255
Since v1: rebased; the changelog explains the remaining teardown
paths; the lockdep trace is from the current tree. The only
conflict was comment-only, against 816fb1590a4c.
Tested in QEMU (PROVE_LOCKING, KASAN) with a /dev/vhci controller:
unpatched, the deadlock reproduced 20/20 with a Page Timeout on an
outgoing connect, and 1/6 with an injected early link failure;
patched, 0/6 and 0/12. A debug WARN for a socket freed with its
timeout_work still armed fired 0/12 (4/4 with the cancel removed).
sco-tester was not run locally.
net/bluetooth/sco.c | 76 +++++++++++++++++----------------------------
1 file changed, 28 insertions(+), 48 deletions(-)
diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c
index 8be48ca9f4c5..b553f3ab57a8 100644
--- a/net/bluetooth/sco.c
+++ b/net/bluetooth/sco.c
@@ -46,8 +46,6 @@ struct sco_conn {
spinlock_t lock;
struct sock *sk;
- struct delayed_work timeout_work;
-
unsigned int mtu;
struct kref ref;
};
@@ -69,6 +67,7 @@ struct sco_pinfo {
__u16 setting;
struct bt_codec codec;
struct sco_conn *conn;
+ struct delayed_work timeout_work;
};
/* ---- SCO timers ---- */
@@ -91,9 +90,6 @@ static void sco_conn_free(struct kref *ref)
* left to release towards hcon here.
*/
- /* Ensure no more work items will run before the connection is freed */
- disable_delayed_work_sync(&conn->timeout_work);
-
kfree(conn);
}
@@ -140,53 +136,45 @@ static struct sock *sco_sock_hold(struct sco_conn *conn)
static void sco_sock_timeout(struct work_struct *work)
{
- struct sco_conn *conn = container_of(work, struct sco_conn,
- timeout_work.work);
- struct sock *sk;
-
- conn = sco_conn_hold_unless_zero(conn);
- if (!conn)
- return;
-
- sco_conn_lock(conn);
- if (!conn->hcon) {
- sco_conn_unlock(conn);
- sco_conn_put(conn);
- return;
- }
- sk = sco_sock_hold(conn);
- sco_conn_unlock(conn);
- sco_conn_put(conn);
-
- if (!sk)
- return;
+ struct sco_pinfo *pi = container_of(work, struct sco_pinfo,
+ timeout_work.work);
+ struct sock *sk = &pi->bt.sk;
BT_DBG("sock %p state %d", sk, sk->sk_state);
lock_sock(sk);
- sk->sk_err = ETIMEDOUT;
- sk->sk_state_change(sk);
+ if (!sock_flag(sk, SOCK_ZAPPED)) {
+ sk->sk_err = ETIMEDOUT;
+ sk->sk_state_change(sk);
+ }
release_sock(sk);
- sock_put(sk);
}
static void sco_sock_set_timer(struct sock *sk, long timeout)
{
+ lockdep_assert(lockdep_sock_is_held(sk));
+
+ cancel_delayed_work(&sco_pi(sk)->timeout_work);
+
if (!sco_pi(sk)->conn)
return;
BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
- cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
- schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout);
+ schedule_delayed_work(&sco_pi(sk)->timeout_work, timeout);
}
static void sco_sock_clear_timer(struct sock *sk)
{
- if (!sco_pi(sk)->conn)
- return;
+ BT_DBG("sock %p state %d", sk, sk->sk_state);
+ cancel_delayed_work(&sco_pi(sk)->timeout_work);
+}
+
+static void sco_sock_disable_timer(struct sock *sk)
+{
+ lockdep_assert(!lockdep_sock_is_held(sk));
BT_DBG("sock %p state %d", sk, sk->sk_state);
- cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
+ disable_delayed_work_sync(&sco_pi(sk)->timeout_work);
}
/* ---- SCO connections ---- */
@@ -210,7 +198,6 @@ static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
kref_init(&conn->ref);
spin_lock_init(&conn->lock);
- INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
hcon->sco_data = conn;
conn->hcon = hcon;
@@ -293,9 +280,10 @@ static void sco_conn_del(struct hci_conn *hcon, int err)
if (!sk)
return;
+ sco_sock_disable_timer(sk);
+
/* Kill socket */
lock_sock(sk);
- sco_sock_clear_timer(sk);
sco_chan_del(sk, err);
release_sock(sk);
sock_put(sk);
@@ -577,6 +565,8 @@ static void sco_sock_kill(struct sock *sk)
BT_DBG("sk %p state %d", sk, sk->sk_state);
+ sco_sock_disable_timer(sk);
+
/* Sock is dead, so set conn->sk to NULL to avoid possible UAF */
lock_sock(sk);
if (sco_pi(sk)->conn) {
@@ -619,23 +609,11 @@ static void __sco_sock_close(struct sock *sk)
/* Must be called on unlocked socket. */
static void sco_sock_close(struct sock *sk)
{
- struct sco_conn *conn;
-
- lock_sock(sk);
- conn = sco_pi(sk)->conn;
- if (conn)
- sco_conn_hold(conn);
- release_sock(sk);
-
- if (conn)
- disable_delayed_work_sync(&conn->timeout_work);
+ sco_sock_disable_timer(sk);
lock_sock(sk);
__sco_sock_close(sk);
release_sock(sk);
-
- if (conn)
- sco_conn_put(conn);
}
static void sco_sock_init(struct sock *sk, struct sock *parent)
@@ -667,6 +645,8 @@ static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
sk->sk_destruct = sco_sock_destruct;
sk->sk_sndtimeo = SCO_CONN_TIMEOUT;
+ INIT_DELAYED_WORK(&sco_pi(sk)->timeout_work, sco_sock_timeout);
+
sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
sco_pi(sk)->codec.id = BT_CODEC_CVSD;
sco_pi(sk)->codec.cid = 0xffff;
base-commit: f57f78086ab54f3b3122fbaeba2c842ba7bb6d00
--
2.53.0
reply other threads:[~2026-12-06 0:32 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261206003203.76893-1-alishmery18@gmail.com \
--to=alishmery18@gmail.com \
--cc=iam@sung-woo.kim \
--cc=linux-bluetooth@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=luiz.dentz@gmail.com \
--cc=marcel@holtmann.org \
--cc=pav@iki.fi \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®