History

Bluetooth: SCO: give the socket its own sco_conn reference

sco_conn_del() drops a reference it does not own. It takes one transient reference via sco_conn_hold_unless_zero() and releases it with the sco_conn_put() that follows sco_sock_hold(); the additional put in the !sk branch releases a second one: conn = sco_conn_hold_unless_zero(conn); ... sk = sco_sock_hold(conn); sco_conn_unlock(conn); sco_conn_put(conn); if (!sk) { sco_conn_put(conn); return; } When close() races the controller's Disconnection Complete, sco_chan_del() clears conn->sk and drops the socket's reference while sco_conn_del() is running. sco_conn_del() then sees sk == NULL, its own put drops the count to zero and frees the conn, and the second put writes to the freed kref: BUG: KASAN: slab-use-after-free in sco_conn_put.part.0+0x1a/0x190 Write of size 4 at addr ffff8881099dec74 by task kworker/u17:3/413 Workqueue: hci1 hci_rx_work Call Trace: sco_conn_put.part.0+0x1a/0x190 hci_disconn_complete_evt+0x1ee/0x3e0 hci_event_packet+0x54a/0x650 hci_rx_work+0x321/0x3d0 Allocated by task 413: sco_conn_add+0x72/0x1a0 sco_connect_cfm+0x88/0x670 Freed by task 413: sco_conn_del.isra.0+0x3f/0xf0 hci_disconn_complete_evt+0x1ee/0x3e0 refcount_t: underflow; use-after-free. The root cause is that the socket stores the connection without holding a reference of its own. __sco_chan_add() does: sco_pi(sk)->conn = conn; so the socket borrows whatever reference its caller happened to hold, and the callers paper over that with ad-hoc holds and puts. Give the socket a counted reference instead: __sco_chan_add() takes one and it is released together with the channel (sco_chan_del()) and in sco_sock_destruct(). With the socket holding its own reference, sco_conn_del() no longer needs the extra put and the redundant hold in sco_conn_ready() goes away. Making the socket own its reference means the connection is now actually freed on the error paths of sco_connect() where it used to leak, which in turn runs sco_conn_free() and its hci_conn_drop(conn->hcon). To keep the hci_conn accounting balanced, make that ownership explicit as well: sco_conn_add() consumes one hci_conn reference and the sco_conn owns it for its lifetime. sco_connect() hands over the reference returned by hci_connect_sco() and no longer drops it on the error paths; sco_connect_cfm(), which is not given a reference, takes one with hci_conn_hold() before handing it to sco_conn_add() (and drops it again if the allocation fails); and the explicit hci_conn_hold() in sco_conn_ready() is removed. Every reference then has a single, clear owner.

Aldo Ariel Panzardo committed 3 months ago

Commit abd93c81 parent cac43d31 file · +13 −9Browse files

Unified · Split

net/bluetooth/sco.c+13−9
@@ -188,6 +188,9 @@ static void sco_sock_clear_timer(struct sock *sk) · 187 unmodified lines
188}188}
189 189
190/* ---- SCO connections ---- */190/* ---- SCO connections ---- */
191/* Consumes a reference on @hcon, which the returned sco_conn owns until it is
192 * freed. On failure (NULL return) the reference is left for the caller to drop.
193 */
191static struct sco_conn *sco_conn_add(struct hci_conn *hcon)194static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
192{195{
193 struct sco_conn *conn = hcon->sco_data;196 struct sco_conn *conn = hcon->sco_data;
@@ -198,6 +201,9 @@ static struct sco_conn *sco_conn_add(struct hci_conn *hcon) · 4 unmodified lines
198 sco_conn_lock(conn);201 sco_conn_lock(conn);
199 conn->hcon = hcon;202 conn->hcon = hcon;
200 sco_conn_unlock(conn);203 sco_conn_unlock(conn);
204 } else {
205 /* conn already owns a reference on hcon */
206 hci_conn_drop(hcon);
201 }207 }
202 return conn;208 return conn;
203 }209 }
@@ -265,10 +271,8 @@ static void sco_conn_del(struct hci_conn *hcon, int err) · 61 unmodified lines
265 sco_conn_unlock(conn);271 sco_conn_unlock(conn);
266 sco_conn_put(conn);272 sco_conn_put(conn);
267 273
268 if (!sk) {274 if (!sk)
269 sco_conn_put(conn);
270 return;275 return;
271 }
272 276
273 /* Kill socket */277 /* Kill socket */
274 lock_sock(sk);278 lock_sock(sk);
@@ -283,7 +287,7 @@ static void __sco_chan_add(struct sco_conn *conn, struct sock *sk, · 8 unmodified lines
283{287{
284 BT_DBG("conn %p", conn);288 BT_DBG("conn %p", conn);
285 289
286 sco_pi(sk)->conn = conn;290 sco_pi(sk)->conn = sco_conn_hold(conn);
287 conn->sk = sk;291 conn->sk = sk;
288 292
289 if (parent)293 if (parent)
@@ -366,15 +370,15 @@ static int sco_connect(struct sock *sk) · 76 unmodified lines
366 */370 */
367 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {371 if (sk->sk_state != BT_OPEN && sk->sk_state != BT_BOUND) {
368 release_sock(sk);372 release_sock(sk);
369 hci_conn_drop(hcon);373 sco_conn_put(conn);
370 err = -EBADFD;374 err = -EBADFD;
371 goto unlock;375 goto unlock;
372 }376 }
373 377
374 err = sco_chan_add(conn, sk, NULL);378 err = sco_chan_add(conn, sk, NULL);
379 sco_conn_put(conn);
375 if (err) {380 if (err) {
376 release_sock(sk);381 release_sock(sk);
377 hci_conn_drop(hcon);
378 goto unlock;382 goto unlock;
379 }383 }
380 384
@@ -1452,8 +1456,6 @@ static void sco_conn_ready(struct sco_conn *conn) · 1071 unmodified lines
1452 bacpy(&sco_pi(sk)->src, &conn->hcon->src);1456 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1453 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);1457 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1454 1458
1455 sco_conn_hold(conn);
1456 hci_conn_hold(conn->hcon);
1457 __sco_chan_add(conn, sk, parent);1459 __sco_chan_add(conn, sk, parent);
1458 1460
1459 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))1461 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(parent)->flags))
@@ -1509,10 +1511,12 @@ static void sco_connect_cfm(struct hci_conn *hcon, __u8 status) · 49 unmodified lines
1509 if (!status) {1511 if (!status) {
1510 struct sco_conn *conn;1512 struct sco_conn *conn;
1511 1513
1512 conn = sco_conn_add(hcon);1514 conn = sco_conn_add(hci_conn_hold(hcon));
1513 if (conn) {1515 if (conn) {
1514 sco_conn_ready(conn);1516 sco_conn_ready(conn);
1515 sco_conn_put(conn);1517 sco_conn_put(conn);
1518 } else {
1519 hci_conn_drop(hcon);
1516 }1520 }
1517 } else1521 } else
1518 sco_conn_del(hcon, bt_to_errno(status));1522 sco_conn_del(hcon, bt_to_errno(status));

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t