All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] Bluetooth: SCO: avoid deadlock in sco_conn_free()
@ 2026-12-06  0:32 Ali Firas
  2026-12-06  1:54 ` [v2] " bluez.test.bot
  0 siblings, 1 reply; 2+ messages in thread
From: Ali Firas @ 2026-12-06  0:32 UTC (permalink / raw)
  To: luiz.dentz, marcel; +Cc: linux-bluetooth, linux-kernel, iam, pav, stable

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


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

* RE: [v2] Bluetooth: SCO: avoid deadlock in sco_conn_free()
  2026-12-06  0:32 [PATCH v2] Bluetooth: SCO: avoid deadlock in sco_conn_free() Ali Firas
@ 2026-12-06  1:54 ` bluez.test.bot
  0 siblings, 0 replies; 2+ messages in thread
From: bluez.test.bot @ 2026-12-06  1:54 UTC (permalink / raw)
  To: linux-bluetooth, alishmery18

[-- Attachment #1: Type: text/plain, Size: 1535 bytes --]

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/1233132/

---Test result---

Test Summary:
CheckPatch                    PASS      0.52 seconds
VerifyFixes                   PASS      0.06 seconds
VerifySignedoff               PASS      0.06 seconds
GitLint                       FAIL      0.18 seconds
SubjectPrefix                 PASS      0.05 seconds
BuildKernel                   PASS      23.63 seconds
CheckAllWarning               PASS      26.87 seconds
CheckSparse                   WARNING   28.86 seconds
BuildKernel32                 PASS      23.73 seconds
CheckKernelLLVM               PASS      24.48 seconds
TestRunnerSetup               PASS      556.33 seconds
TestRunner_sco-tester         PASS      7.69 seconds
IncrementalBuild              PASS      22.07 seconds

Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[v2] Bluetooth: SCO: avoid deadlock in sco_conn_free()

88: B2 Line has trailing whitespace: "    "
92: B2 Line has trailing whitespace: "    "
##############################
Test: CheckSparse - WARNING
Desc: Run sparse tool with linux kernel
Output:
net/bluetooth/sco.c: note: in included file:./include/net/bluetooth/hci_core.h:151:35: warning: array of flexible structures


https://github.com/bluez/bluetooth-next/pull/1295

---
Regards,
Linux Bluetooth


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

end of thread, other threads:[~2026-12-06  1:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06  0:32 [PATCH v2] Bluetooth: SCO: avoid deadlock in sco_conn_free() Ali Firas
2026-12-06  1:54 ` [v2] " bluez.test.bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.