public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [RFC PATCH 00/24] Bluetooth: add locks to hci_conn accesses
@ 2025-09-21 19:14 Pauli Virtanen
  2025-09-21 19:14 ` [RFC PATCH 01/24] Bluetooth: ISO: free rx_skb if not consumed Pauli Virtanen
                   ` (24 more replies)
  0 siblings, 25 replies; 29+ messages in thread
From: Pauli Virtanen @ 2025-09-21 19:14 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Pauli Virtanen

(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(-)

-- 
2.51.0


^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2025-09-23 13:50 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-21 19:14 [RFC PATCH 00/24] Bluetooth: add locks to hci_conn accesses Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 01/24] Bluetooth: ISO: free rx_skb if not consumed Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 02/24] Bluetooth: ISO: don't leak skb in ISO_CONT RX Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 03/24] Bluetooth: hci_sync: make hci_cmd_sync_run* indicate if item was added Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 04/24] Bluetooth: hci_sync: hci_cmd_sync_queue_once() return -EEXIST if exists Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 05/24] Bluetooth: hci_conn: avoid ABA error in abort_conn_sync Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 06/24] Bluetooth: hci_sync: avoid ABA/UAF in hci_sync callbacks Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 07/24] Bluetooth: hci_event: extend conn_hash lookup RCU critical sections Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 08/24] Bluetooth: hci_sync: " Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 09/24] Bluetooth: mgmt: " Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 10/24] Bluetooth: hci_conn: " Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 11/24] Bluetooth: hci_core: add lockdep check to hci_conn_hash lookups Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 12/24] Bluetooth: hci_core: add lockdep check to hci_conn_valid() Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 13/24] Bluetooth: hci_sync: fix hdev locking in hci_le_create_conn_sync Pauli Virtanen
2025-09-21 19:14 ` [RFC PATCH 14/24] Bluetooth: hci_core: hold hdev lock in packet TX scheduler Pauli Virtanen
2025-09-21 19:15 ` [RFC PATCH 15/24] Bluetooth: lookup hci_conn on RX path on protocol side Pauli Virtanen
2025-09-21 19:16 ` [RFC PATCH 16/24] Bluetooth: L2CAP: fix hci_conn_valid() usage Pauli Virtanen
2025-09-21 19:16 ` [RFC PATCH 17/24] Bluetooth: hci_sync: add helper for hdev locking across waits Pauli Virtanen
2025-09-22 14:51   ` Luiz Augusto von Dentz
2025-09-22 16:43     ` Pauli Virtanen
2025-09-22 20:40       ` Luiz Augusto von Dentz
2025-09-21 19:16 ` [RFC PATCH 18/24] Bluetooth: hci_sync: hold lock in hci_acl_create_conn_sync Pauli Virtanen
2025-09-21 19:16 ` [RFC PATCH 19/24] Bluetooth: hci_sync: hold lock in hci_le_create_conn_sync Pauli Virtanen
2025-09-21 19:16 ` [RFC PATCH 20/24] Bluetooth: hci_sync: add hdev lock lockdep asserts in subroutines Pauli Virtanen
2025-09-21 19:16 ` [RFC PATCH 21/24] Bluetooth: fix locking for hci_abort_conn_sync() Pauli Virtanen
2025-09-21 19:16 ` [RFC PATCH 22/24] Bluetooth: hci_sync: lock properly in hci_le_pa/big_create_sync Pauli Virtanen
2025-09-21 19:16 ` [RFC PATCH 23/24] Bluetooth: hci_sync: fix locking in hci_disconnect_sync Pauli Virtanen
2025-09-21 19:16 ` [RFC PATCH 24/24] Bluetooth: hci_conn: fix ABA and locking in hci_enhanced_setup_sync Pauli Virtanen
2025-09-23 13:50 ` [RFC PATCH 00/24] Bluetooth: add locks to hci_conn accesses patchwork-bot+bluetooth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox