Takashi Iwai adfd84
From ba316be1b6a00db7126ed9a39f9bee434a508043 Mon Sep 17 00:00:00 2001
Takashi Iwai adfd84
From: Desmond Cheong Zhi Xi <desmondcheongzx@gmail.com>
Takashi Iwai adfd84
Date: Tue, 10 Aug 2021 12:14:05 +0800
Takashi Iwai adfd84
Subject: [PATCH] Bluetooth: schedule SCO timeouts with delayed_work
Takashi Iwai adfd84
Git-commit: ba316be1b6a00db7126ed9a39f9bee434a508043
Takashi Iwai adfd84
Patch-mainline: v5.15-rc1
Takashi Iwai adfd84
References: CVE-2021-3640 bsc#1188172
Takashi Iwai adfd84
Takashi Iwai adfd84
struct sock.sk_timer should be used as a sock cleanup timer. However,
Takashi Iwai adfd84
SCO uses it to implement sock timeouts.
Takashi Iwai adfd84
Takashi Iwai adfd84
This causes issues because struct sock.sk_timer's callback is run in
Takashi Iwai adfd84
an IRQ context, and the timer callback function sco_sock_timeout takes
Takashi Iwai adfd84
a spin lock on the socket. However, other functions such as
Takashi Iwai adfd84
sco_conn_del and sco_conn_ready take the spin lock with interrupts
Takashi Iwai adfd84
enabled.
Takashi Iwai adfd84
Takashi Iwai adfd84
This inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} lock usage could
Takashi Iwai adfd84
lead to deadlocks as reported by Syzbot [1]:
Takashi Iwai adfd84
       CPU0
Takashi Iwai adfd84
       ----
Takashi Iwai adfd84
  lock(slock-AF_BLUETOOTH-BTPROTO_SCO);
Takashi Iwai adfd84
  <Interrupt>
Takashi Iwai adfd84
    lock(slock-AF_BLUETOOTH-BTPROTO_SCO);
Takashi Iwai adfd84
Takashi Iwai adfd84
To fix this, we use delayed work to implement SCO sock timouts
Takashi Iwai adfd84
instead. This allows us to avoid taking the spin lock on the socket in
Takashi Iwai adfd84
an IRQ context, and corrects the misuse of struct sock.sk_timer.
Takashi Iwai adfd84
Takashi Iwai adfd84
As a note, cancel_delayed_work is used instead of
Takashi Iwai adfd84
cancel_delayed_work_sync in sco_sock_set_timer and
Takashi Iwai adfd84
sco_sock_clear_timer to avoid a deadlock. In the future, the call to
Takashi Iwai adfd84
bh_lock_sock inside sco_sock_timeout should be changed to lock_sock to
Takashi Iwai adfd84
synchronize with other functions using lock_sock. However, since
Takashi Iwai adfd84
sco_sock_set_timer and sco_sock_clear_timer are sometimes called under
Takashi Iwai adfd84
the locked socket (in sco_connect and __sco_sock_close),
Takashi Iwai adfd84
cancel_delayed_work_sync might cause them to sleep until an
Takashi Iwai adfd84
sco_sock_timeout that has started finishes running. But
Takashi Iwai adfd84
sco_sock_timeout would also sleep until it can grab the lock_sock.
Takashi Iwai adfd84
Takashi Iwai adfd84
Using cancel_delayed_work is fine because sco_sock_timeout does not
Takashi Iwai adfd84
change from run to run, hence there is no functional difference
Takashi Iwai adfd84
Between: 
Takashi Iwai adfd84
1. waiting for a timeout to finish running before scheduling another
Takashi Iwai adfd84
timeout
Takashi Iwai adfd84
2. scheduling another timeout while a timeout is running.
Takashi Iwai adfd84
Takashi Iwai adfd84
Link: https://syzkaller.appspot.com/bug?id=9089d89de0502e120f234ca0fc8a703f7368b31e [1]
Takashi Iwai adfd84
Reported-by: syzbot+2f6d7c28bb4bf7e82060@syzkaller.appspotmail.com
Takashi Iwai adfd84
Tested-by: syzbot+2f6d7c28bb4bf7e82060@syzkaller.appspotmail.com
Takashi Iwai adfd84
Signed-off-by: Desmond Cheong Zhi Xi <desmondcheongzx@gmail.com>
Takashi Iwai adfd84
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
Takashi Iwai adfd84
Acked-by: Takashi Iwai <tiwai@suse.de>
Takashi Iwai adfd84
Takashi Iwai adfd84
---
Takashi Iwai adfd84
 net/bluetooth/sco.c |   35 +++++++++++++++++++++++++++++------
Takashi Iwai adfd84
 1 file changed, 29 insertions(+), 6 deletions(-)
Takashi Iwai adfd84
Takashi Iwai adfd84
--- a/net/bluetooth/sco.c
Takashi Iwai adfd84
+++ b/net/bluetooth/sco.c
Takashi Iwai adfd84
@@ -48,6 +48,8 @@ struct sco_conn {
Takashi Iwai adfd84
 	spinlock_t	lock;
Takashi Iwai adfd84
 	struct sock	*sk;
Takashi Iwai adfd84
 
Takashi Iwai adfd84
+	struct delayed_work	timeout_work;
Takashi Iwai adfd84
+
Takashi Iwai adfd84
 	unsigned int    mtu;
Takashi Iwai adfd84
 };
Takashi Iwai adfd84
 
Takashi Iwai adfd84
@@ -73,9 +75,20 @@ struct sco_pinfo {
Takashi Iwai adfd84
 #define SCO_CONN_TIMEOUT	(HZ * 40)
Takashi Iwai adfd84
 #define SCO_DISCONN_TIMEOUT	(HZ * 2)
Takashi Iwai adfd84
 
Takashi Iwai adfd84
-static void sco_sock_timeout(unsigned long arg)
Takashi Iwai adfd84
+static void sco_sock_timeout(struct work_struct *work)
Takashi Iwai adfd84
 {
Takashi Iwai adfd84
-	struct sock *sk = (struct sock *)arg;
Takashi Iwai adfd84
+	struct sco_conn *conn = container_of(work, struct sco_conn,
Takashi Iwai adfd84
+					     timeout_work.work);
Takashi Iwai adfd84
+	struct sock *sk;
Takashi Iwai adfd84
+
Takashi Iwai adfd84
+	sco_conn_lock(conn);
Takashi Iwai adfd84
+	sk = conn->sk;
Takashi Iwai adfd84
+	if (sk)
Takashi Iwai adfd84
+		sock_hold(sk);
Takashi Iwai adfd84
+	sco_conn_unlock(conn);
Takashi Iwai adfd84
+
Takashi Iwai adfd84
+	if (!sk)
Takashi Iwai adfd84
+		return;
Takashi Iwai adfd84
 
Takashi Iwai adfd84
 	BT_DBG("sock %p state %d", sk, sk->sk_state);
Takashi Iwai adfd84
 
Takashi Iwai adfd84
@@ -90,14 +103,21 @@ static void sco_sock_timeout(unsigned lo
Takashi Iwai adfd84
 
Takashi Iwai adfd84
 static void sco_sock_set_timer(struct sock *sk, long timeout)
Takashi Iwai adfd84
 {
Takashi Iwai adfd84
+	if (!sco_pi(sk)->conn)
Takashi Iwai adfd84
+		return;
Takashi Iwai adfd84
+
Takashi Iwai adfd84
 	BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
Takashi Iwai adfd84
-	sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
Takashi Iwai adfd84
+	cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
Takashi Iwai adfd84
+	schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout);
Takashi Iwai adfd84
 }
Takashi Iwai adfd84
 
Takashi Iwai adfd84
 static void sco_sock_clear_timer(struct sock *sk)
Takashi Iwai adfd84
 {
Takashi Iwai adfd84
+	if (!sco_pi(sk)->conn)
Takashi Iwai adfd84
+		return;
Takashi Iwai adfd84
+
Takashi Iwai adfd84
 	BT_DBG("sock %p state %d", sk, sk->sk_state);
Takashi Iwai adfd84
-	sk_stop_timer(sk, &sk->sk_timer);
Takashi Iwai adfd84
+	cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
Takashi Iwai adfd84
 }
Takashi Iwai adfd84
 
Takashi Iwai adfd84
 /* ---- SCO connections ---- */
Takashi Iwai adfd84
@@ -178,6 +198,9 @@ static void sco_conn_del(struct hci_conn
Takashi Iwai adfd84
 		bh_unlock_sock(sk);
Takashi Iwai adfd84
 		sco_sock_kill(sk);
Takashi Iwai adfd84
 		sock_put(sk);
Takashi Iwai adfd84
+
Takashi Iwai adfd84
+		/* Ensure no more work items will run before freeing conn. */
Takashi Iwai adfd84
+		cancel_delayed_work_sync(&conn->timeout_work);
Takashi Iwai adfd84
 	}
Takashi Iwai adfd84
 
Takashi Iwai adfd84
 	hcon->sco_data = NULL;
Takashi Iwai adfd84
@@ -192,6 +215,8 @@ static void __sco_chan_add(struct sco_co
Takashi Iwai adfd84
 	sco_pi(sk)->conn = conn;
Takashi Iwai adfd84
 	conn->sk = sk;
Takashi Iwai adfd84
 
Takashi Iwai adfd84
+	INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
Takashi Iwai adfd84
+
Takashi Iwai adfd84
 	if (parent)
Takashi Iwai adfd84
 		bt_accept_enqueue(parent, sk, true);
Takashi Iwai adfd84
 }
Takashi Iwai adfd84
@@ -488,8 +513,6 @@ static struct sock *sco_sock_alloc(struc
Takashi Iwai adfd84
 
Takashi Iwai adfd84
 	sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
Takashi Iwai adfd84
 
Takashi Iwai adfd84
-	setup_timer(&sk->sk_timer, sco_sock_timeout, (unsigned long)sk);
Takashi Iwai adfd84
-
Takashi Iwai adfd84
 	bt_sock_link(&sco_sk_list, sk);
Takashi Iwai adfd84
 	return sk;
Takashi Iwai adfd84
 }