From: Pauli Virtanen <pav@iki.fi>
To: linux-bluetooth@vger.kernel.org
Cc: Pauli Virtanen <pav@iki.fi>
Subject: [RFC PATCH 00/24] Bluetooth: add locks to hci_conn accesses
Date: Sun, 21 Sep 2025 22:14:15 +0300 [thread overview]
Message-ID: <cover.1758481869.git.pav@iki.fi> (raw)
(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
next reply other threads:[~2025-09-21 19:14 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-21 19:14 Pauli Virtanen [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=cover.1758481869.git.pav@iki.fi \
--to=pav@iki.fi \
--cc=linux-bluetooth@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox