Skip to content

RFC Bluetooth: add locks to hci_conn accesses

Pauli Virtanen linux-bluetooth 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest24 patches 3 repliesIn review 2 of 24 applied

Patches in v1 2 of 24 applied

#Subject and filesSizeRepliesApplied
1/24Bluetooth: ISO: free rx_skb if not consumednet/bluetooth/iso.c+2 -00 repliesApplied
2/24Bluetooth: ISO: don't leak skb in ISO_CONT RXnet/bluetooth/iso.c+1 -10 repliesApplied
3/24Bluetooth: hci_sync: make hci_cmd_sync_run* indicate if item was addednet/bluetooth/hci_conn.c net/bluetooth/hci_sync.c+13 -40 replies
4/24Bluetooth: hci_sync: hci_cmd_sync_queue_once() return -EEXIST if existsnet/bluetooth/hci_sync.c+26 -110 replies
5/24Bluetooth: hci_conn: avoid ABA error in abort_conn_syncnet/bluetooth/hci_conn.c+11 -10 replies
6/24Bluetooth: hci_sync: avoid ABA/UAF in hci_sync callbacksnet/bluetooth/hci_sync.c+43 -120 replies
7/24Bluetooth: hci_event: extend conn_hash lookup RCU critical sectionsnet/bluetooth/hci_event.c+20 -130 replies
8/24Bluetooth: hci_sync: extend conn_hash lookup RCU critical sectionsnet/bluetooth/hci_sync.c+49 -100 replies
9/24Bluetooth: mgmt: extend conn_hash lookup RCU critical sectionsnet/bluetooth/mgmt.c+30 -80 replies
10/24Bluetooth: hci_conn: extend conn_hash lookup RCU critical sectionsnet/bluetooth/hci_conn.c+7 -10 replies
11/24Bluetooth: hci_core: add lockdep check to hci_conn_hash lookupsinclude/net/bluetooth/hci_core.h+42 -00 replies
12/24Bluetooth: hci_core: add lockdep check to hci_conn_valid()include/net/bluetooth/hci_core.h+2 -00 replies
13/24Bluetooth: hci_sync: fix hdev locking in hci_le_create_conn_syncnet/bluetooth/hci_sync.c+4 -10 replies
14/24Bluetooth: hci_core: hold hdev lock in packet TX schedulernet/bluetooth/hci_core.c+30 -200 replies
15/24Bluetooth: lookup hci_conn on RX path on protocol sideinclude/net/bluetooth/hci_core.h net/bluetooth/hci_core.c net/bluetooth/iso.c +2 more+99 -740 replies
16/24Bluetooth: L2CAP: fix hci_conn_valid() usagenet/bluetooth/l2cap_core.c+4 -00 replies
17/24Bluetooth: hci_sync: add helper for hdev locking across waitsinclude/net/bluetooth/hci_core.h include/net/bluetooth/hci_sync.h net/bluetooth/hci_sync.c+32 -03 replies
18/24Bluetooth: hci_sync: hold lock in hci_acl_create_conn_syncnet/bluetooth/hci_sync.c+16 -20 replies
19/24Bluetooth: hci_sync: hold lock in hci_le_create_conn_syncnet/bluetooth/hci_sync.c+12 -60 replies
20/24Bluetooth: hci_sync: add hdev lock lockdep asserts in subroutinesnet/bluetooth/hci_sync.c+20 -00 replies
21/24Bluetooth: fix locking for hci_abort_conn_sync()net/bluetooth/hci_conn.c net/bluetooth/hci_sync.c+27 -280 replies
22/24Bluetooth: hci_sync: lock properly in hci_le_pa/big_create_syncnet/bluetooth/hci_sync.c+35 -40 replies
23/24Bluetooth: hci_sync: fix locking in hci_disconnect_syncnet/bluetooth/hci_sync.c+0 -30 replies
24/24Bluetooth: hci_conn: fix ABA and locking in hci_enhanced_setup_syncnet/bluetooth/hci_conn.c+37 -110 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH 00/24] Bluetooth: add locks to hci_conn accesses

(RFC since this needs to be tested much better.)

Each hdev has two ordered workqueues that run in parallel, in addition to user tasks and some timers in global workqueues.

Both workqueues may delete hci_conn* and modify their state. The current situation is there are races and UAF due to this. In older kernels, it used to be much of the work was done from a single ordered hdev->workqueue, so one could be more lax with locking. I don't think what used to be safe earlier is necessarily so now, so some simple rules are probably needed.

Set some rules for hci_conn* locking and try follow them:

- lookups: hdev->lock or rcu_read_lock() shall be held by caller

- field access: hdev->lock shall be held, unless lockless operation is explained in comments, in which case rcu_read_lock() is enough

- hci_conn pointers remain valid after exiting critical section only if hci_conn_get() refcount remains held

- before field access, if hci_conn* was sustained only by refcount, hci_conn_valid() shall be checked before dereferencing

***

Add lockdep asserts to lookup functions and hci_conn_valid() to catch some bad callsites.

In hci_sync, the critical sections cannot extend across HCI event waits. There, add helpers hci_dev_lock/unlock_sync(hdev) that release/acquire hdev->lock before/after the wait.

Following the rules above then means checking hci_conn* validity after each call to a waiting subroutine.

This series also contains some fixes to ABA issues: if hci_conn pointer is used across critical sections, one should hold a refcount, and not use hci_conn_valid() on potentially wild pointer even though it doesn't dereference.

Pauli Virtanen (24):
Bluetooth: ISO: free rx_skb if not consumed
Bluetooth: ISO: don't leak skb in ISO_CONT RX
Bluetooth: hci_sync: make hci_cmd_sync_run* indicate if item was added
Bluetooth: hci_sync: hci_cmd_sync_queue_once() return -EEXIST if
exists
Bluetooth: hci_conn: avoid ABA error in abort_conn_sync
Bluetooth: hci_sync: avoid ABA/UAF in hci_sync callbacks
Bluetooth: hci_event: extend conn_hash lookup RCU critical sections
Bluetooth: hci_sync: extend conn_hash lookup RCU critical sections
Bluetooth: mgmt: extend conn_hash lookup RCU critical sections
Bluetooth: hci_conn: extend conn_hash lookup RCU critical sections
Bluetooth: hci_core: add lockdep check to hci_conn_hash lookups
Bluetooth: hci_core: add lockdep check to hci_conn_valid()
Bluetooth: hci_sync: fix hdev locking in hci_le_create_conn_sync
Bluetooth: hci_core: hold hdev lock in packet TX scheduler
Bluetooth: lookup hci_conn on RX path on protocol side
Bluetooth: L2CAP: fix hci_conn_valid() usage
Bluetooth: hci_sync: add helper for hdev locking across waits
Bluetooth: hci_sync: hold lock in hci_acl_create_conn_sync
Bluetooth: hci_sync: hold lock in hci_le_create_conn_sync
Bluetooth: hci_sync: add hdev lock lockdep asserts in subroutines
Bluetooth: fix locking for hci_abort_conn_sync()
Bluetooth: hci_sync: lock properly in hci_le_pa/big_create_sync
Bluetooth: hci_sync: fix locking in hci_disconnect_sync
Bluetooth: hci_conn: fix ABA and locking in hci_enhanced_setup_sync

 include/net/bluetooth/hci_core.h |  66 ++++++-
 include/net/bluetooth/hci_sync.h |   4 +
 net/bluetooth/hci_conn.c         |  83 ++++++--
 net/bluetooth/hci_core.c         | 114 +++++------
 net/bluetooth/hci_event.c        |  33 ++--
 net/bluetooth/hci_sync.c         | 315 ++++++++++++++++++++++++-------
 net/bluetooth/iso.c              |  34 +++-
 net/bluetooth/l2cap_core.c       |  28 ++-
 net/bluetooth/mgmt.c             |  38 +++-
 net/bluetooth/sco.c              |  35 +++-
 10 files changed, 551 insertions(+), 199 deletions(-)
Signature · 1 line
2.51.0