Skip to content

Commit ba316be

Browse files
desmondcheongzxVudentz
authored andcommitted
Bluetooth: schedule SCO timeouts with delayed_work
struct sock.sk_timer should be used as a sock cleanup timer. However, SCO uses it to implement sock timeouts. This causes issues because struct sock.sk_timer's callback is run in an IRQ context, and the timer callback function sco_sock_timeout takes a spin lock on the socket. However, other functions such as sco_conn_del and sco_conn_ready take the spin lock with interrupts enabled. This inconsistent {SOFTIRQ-ON-W} -> {IN-SOFTIRQ-W} lock usage could lead to deadlocks as reported by Syzbot [1]: CPU0 ---- lock(slock-AF_BLUETOOTH-BTPROTO_SCO); <Interrupt> lock(slock-AF_BLUETOOTH-BTPROTO_SCO); To fix this, we use delayed work to implement SCO sock timouts instead. This allows us to avoid taking the spin lock on the socket in an IRQ context, and corrects the misuse of struct sock.sk_timer. As a note, cancel_delayed_work is used instead of cancel_delayed_work_sync in sco_sock_set_timer and sco_sock_clear_timer to avoid a deadlock. In the future, the call to bh_lock_sock inside sco_sock_timeout should be changed to lock_sock to synchronize with other functions using lock_sock. However, since sco_sock_set_timer and sco_sock_clear_timer are sometimes called under the locked socket (in sco_connect and __sco_sock_close), cancel_delayed_work_sync might cause them to sleep until an sco_sock_timeout that has started finishes running. But sco_sock_timeout would also sleep until it can grab the lock_sock. Using cancel_delayed_work is fine because sco_sock_timeout does not change from run to run, hence there is no functional difference between: 1. waiting for a timeout to finish running before scheduling another timeout 2. scheduling another timeout while a timeout is running. Link: https://syzkaller.appspot.com/bug?id=9089d89de0502e120f234ca0fc8a703f7368b31e [1] Reported-by: [email protected] Tested-by: [email protected] Signed-off-by: Desmond Cheong Zhi Xi <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 146af22 commit ba316be

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

net/bluetooth/sco.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ struct sco_conn {
4848
spinlock_t lock;
4949
struct sock *sk;
5050

51+
struct delayed_work timeout_work;
52+
5153
unsigned int mtu;
5254
};
5355

@@ -74,9 +76,20 @@ struct sco_pinfo {
7476
#define SCO_CONN_TIMEOUT (HZ * 40)
7577
#define SCO_DISCONN_TIMEOUT (HZ * 2)
7678

77-
static void sco_sock_timeout(struct timer_list *t)
79+
static void sco_sock_timeout(struct work_struct *work)
7880
{
79-
struct sock *sk = from_timer(sk, t, sk_timer);
81+
struct sco_conn *conn = container_of(work, struct sco_conn,
82+
timeout_work.work);
83+
struct sock *sk;
84+
85+
sco_conn_lock(conn);
86+
sk = conn->sk;
87+
if (sk)
88+
sock_hold(sk);
89+
sco_conn_unlock(conn);
90+
91+
if (!sk)
92+
return;
8093

8194
BT_DBG("sock %p state %d", sk, sk->sk_state);
8295

@@ -91,14 +104,21 @@ static void sco_sock_timeout(struct timer_list *t)
91104

92105
static void sco_sock_set_timer(struct sock *sk, long timeout)
93106
{
107+
if (!sco_pi(sk)->conn)
108+
return;
109+
94110
BT_DBG("sock %p state %d timeout %ld", sk, sk->sk_state, timeout);
95-
sk_reset_timer(sk, &sk->sk_timer, jiffies + timeout);
111+
cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
112+
schedule_delayed_work(&sco_pi(sk)->conn->timeout_work, timeout);
96113
}
97114

98115
static void sco_sock_clear_timer(struct sock *sk)
99116
{
117+
if (!sco_pi(sk)->conn)
118+
return;
119+
100120
BT_DBG("sock %p state %d", sk, sk->sk_state);
101-
sk_stop_timer(sk, &sk->sk_timer);
121+
cancel_delayed_work(&sco_pi(sk)->conn->timeout_work);
102122
}
103123

104124
/* ---- SCO connections ---- */
@@ -179,6 +199,9 @@ static void sco_conn_del(struct hci_conn *hcon, int err)
179199
bh_unlock_sock(sk);
180200
sco_sock_kill(sk);
181201
sock_put(sk);
202+
203+
/* Ensure no more work items will run before freeing conn. */
204+
cancel_delayed_work_sync(&conn->timeout_work);
182205
}
183206

184207
hcon->sco_data = NULL;
@@ -193,6 +216,8 @@ static void __sco_chan_add(struct sco_conn *conn, struct sock *sk,
193216
sco_pi(sk)->conn = conn;
194217
conn->sk = sk;
195218

219+
INIT_DELAYED_WORK(&conn->timeout_work, sco_sock_timeout);
220+
196221
if (parent)
197222
bt_accept_enqueue(parent, sk, true);
198223
}
@@ -500,8 +525,6 @@ static struct sock *sco_sock_alloc(struct net *net, struct socket *sock,
500525

501526
sco_pi(sk)->setting = BT_VOICE_CVSD_16BIT;
502527

503-
timer_setup(&sk->sk_timer, sco_sock_timeout, 0);
504-
505528
bt_sock_link(&sco_sk_list, sk);
506529
return sk;
507530
}

0 commit comments

Comments
 (0)