Skip to content

Commit 3dcaa19

Browse files
pvVudentz
authored andcommitted
Bluetooth: SCO: fix sco_conn related locking and validity issues
Operations that check/update sk_state and access conn should hold lock_sock, otherwise they can race. The order of taking locks is hci_dev_lock > lock_sock > sco_conn_lock, which is how it is in connect/disconnect_cfm -> sco_conn_del -> sco_chan_del. Fix locking in sco_connect to take lock_sock around updating sk_state and conn. sco_conn_del must not occur during sco_connect, as it frees the sco_conn. Hold hdev->lock longer to prevent that. sco_conn_add shall return sco_conn with valid hcon. Make it so also when reusing an old SCO connection waiting for disconnect timeout (see __sco_sock_close where conn->hcon is set to NULL). This should not reintroduce the issue fixed in the earlier commit 9a8ec9e ("Bluetooth: SCO: Fix possible circular locking dependency on sco_connect_cfm"), the relevant fix of releasing lock_sock in sco_sock_connect before acquiring hdev->lock is retained. These changes mirror similar fixes earlier in ISO sockets. Fixes: 9a8ec9e ("Bluetooth: SCO: Fix possible circular locking dependency on sco_connect_cfm") Signed-off-by: Pauli Virtanen <pav@iki.fi> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
1 parent b4066eb commit 3dcaa19

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

net/bluetooth/sco.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,11 @@ static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
126126
struct hci_dev *hdev = hcon->hdev;
127127
struct sco_conn *conn = hcon->sco_data;
128128

129-
if (conn)
129+
if (conn) {
130+
if (!conn->hcon)
131+
conn->hcon = hcon;
130132
return conn;
133+
}
131134

132135
conn = kzalloc(sizeof(struct sco_conn), GFP_KERNEL);
133136
if (!conn)
@@ -268,21 +271,21 @@ static int sco_connect(struct sock *sk)
268271
goto unlock;
269272
}
270273

271-
hci_dev_unlock(hdev);
272-
hci_dev_put(hdev);
273-
274274
conn = sco_conn_add(hcon);
275275
if (!conn) {
276276
hci_conn_drop(hcon);
277-
return -ENOMEM;
277+
err = -ENOMEM;
278+
goto unlock;
278279
}
279280

280-
err = sco_chan_add(conn, sk, NULL);
281-
if (err)
282-
return err;
283-
284281
lock_sock(sk);
285282

283+
err = sco_chan_add(conn, sk, NULL);
284+
if (err) {
285+
release_sock(sk);
286+
goto unlock;
287+
}
288+
286289
/* Update source addr of the socket */
287290
bacpy(&sco_pi(sk)->src, &hcon->src);
288291

@@ -296,8 +299,6 @@ static int sco_connect(struct sock *sk)
296299

297300
release_sock(sk);
298301

299-
return err;
300-
301302
unlock:
302303
hci_dev_unlock(hdev);
303304
hci_dev_put(hdev);

0 commit comments

Comments
 (0)