From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [PATCH v1 05/16] Bluetooth: SMP: Serialize link key updates from smp_notify_keys()
Date: Tue, 17 Nov 2026 13:57:26 -0500 [thread overview]
Message-ID: <20261117185737.3758255-6-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20261117185737.3758255-1-luiz.dentz@gmail.com>
From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
smp_notify_keys() runs from the L2CAP RX path without hdev->lock, yet
hci_add_link_key() and the debug key removal modify hdev->link_keys and
dereference entries returned by hci_find_link_key(), so a concurrent
unpair or Load Link Keys can free the entry being updated.
hdev->lock can't be taken there since it is ordered before the l2cap
channel lock held by the SMP receive path (see smp_user_confirm_reply()).
Add a dedicated link_keys_lock protecting the writers of hdev->link_keys
(add, remove and clear), keep the returned key under RCU in SMP and remove
debug keys by address through hci_remove_link_key().
Reported-by: sashiko.dev (linux-7482a666-d2a0-44a4-9e0d-05354860a5fa)
Fixes: 6a77083af57f ("Bluetooth: Add support for LE SC key generation")
Assisted-by: OpenCode:claude-fable-5.2
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>
---
include/net/bluetooth/hci_core.h | 4 ++++
net/bluetooth/hci_core.c | 40 ++++++++++++++++++++++++++++----
net/bluetooth/hci_event.c | 3 +--
net/bluetooth/smp.c | 11 +++++++--
4 files changed, 50 insertions(+), 8 deletions(-)
diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h
index 6e07bf206e4a..40bec3808000 100644
--- a/include/net/bluetooth/hci_core.h
+++ b/include/net/bluetooth/hci_core.h
@@ -575,6 +575,7 @@ struct hci_dev {
struct list_head accept_list;
struct list_head uuids;
struct list_head link_keys;
+ struct mutex link_keys_lock;
struct list_head long_term_keys;
struct list_head identity_resolving_keys;
struct mutex remote_oob_lock;
@@ -1943,6 +1944,9 @@ struct hci_conn_params *hci_pend_le_action_lookup(struct list_head *list,
void hci_uuids_clear(struct hci_dev *hdev);
void hci_link_keys_clear(struct hci_dev *hdev);
+struct link_key *__hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
+ bdaddr_t *bdaddr, u8 *val, u8 type,
+ u8 pin_len, bool *persistent);
u8 *hci_conn_key_enc_size(struct hci_conn *conn);
struct link_key *hci_find_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr);
struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 9acfee486be0..d543526a3622 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -1112,10 +1112,14 @@ void hci_link_keys_clear(struct hci_dev *hdev)
{
struct link_key *key, *tmp;
+ mutex_lock(&hdev->link_keys_lock);
+
list_for_each_entry_safe(key, tmp, &hdev->link_keys, list) {
list_del_rcu(&key->list);
kfree_rcu(key, rcu);
}
+
+ mutex_unlock(&hdev->link_keys_lock);
}
void hci_smp_ltks_clear(struct hci_dev *hdev)
@@ -1332,13 +1336,16 @@ struct smp_irk *hci_find_irk_by_addr(struct hci_dev *hdev, bdaddr_t *bdaddr,
return irk_to_return;
}
-struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
- bdaddr_t *bdaddr, u8 *val, u8 type,
- u8 pin_len, bool *persistent)
+/* This function requires the caller holds hdev->link_keys_lock */
+struct link_key *__hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
+ bdaddr_t *bdaddr, u8 *val, u8 type,
+ u8 pin_len, bool *persistent)
{
struct link_key *key, *old_key;
u8 old_key_type;
+ lockdep_assert_held(&hdev->link_keys_lock);
+
old_key = hci_find_link_key(hdev, bdaddr);
if (old_key) {
old_key_type = old_key->type;
@@ -1381,6 +1388,24 @@ struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
return key;
}
+/* Can be called without hdev->lock (e.g. from SMP, where it can't be taken
+ * after the l2cap channel lock), the writers of hdev->link_keys are
+ * serialized with their own lock.
+ */
+struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn,
+ bdaddr_t *bdaddr, u8 *val, u8 type,
+ u8 pin_len, bool *persistent)
+{
+ struct link_key *key;
+
+ mutex_lock(&hdev->link_keys_lock);
+ key = __hci_add_link_key(hdev, conn, bdaddr, val, type, pin_len,
+ persistent);
+ mutex_unlock(&hdev->link_keys_lock);
+
+ return key;
+}
+
struct smp_ltk *hci_add_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr,
u8 addr_type, u8 type, u8 authenticated,
u8 tk[16], u8 enc_size, __le16 ediv, __le64 rand)
@@ -1442,15 +1467,21 @@ int hci_remove_link_key(struct hci_dev *hdev, bdaddr_t *bdaddr)
{
struct link_key *key;
+ mutex_lock(&hdev->link_keys_lock);
+
key = hci_find_link_key(hdev, bdaddr);
- if (!key)
+ if (!key) {
+ mutex_unlock(&hdev->link_keys_lock);
return -ENOENT;
+ }
BT_DBG("%s removing %pMR", hdev->name, bdaddr);
list_del_rcu(&key->list);
kfree_rcu(key, rcu);
+ mutex_unlock(&hdev->link_keys_lock);
+
return 0;
}
@@ -2606,6 +2637,7 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)
hdev->def_page_scan_window = 0x0012;
mutex_init(&hdev->lock);
+ mutex_init(&hdev->link_keys_lock);
mutex_init(&hdev->req_lock);
mutex_init(&hdev->mgmt_pending_lock);
mutex_init(&hdev->remote_oob_lock);
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 419a7e674850..e93ecb4016ae 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -5336,8 +5336,7 @@ static void hci_link_key_notify_evt(struct hci_dev *hdev, void *data,
*/
if (key->type == HCI_LK_DEBUG_COMBINATION &&
!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
- list_del_rcu(&key->list);
- kfree_rcu(key, rcu);
+ hci_remove_link_key(hdev, &ev->bdaddr);
goto unlock;
}
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index f8f86b8f73f0..88cd86f742a6 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -1097,8 +1097,13 @@ static void smp_notify_keys(struct l2cap_conn *conn)
else
type = HCI_LK_UNAUTH_COMBINATION_P256;
- key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
- smp->link_key, type, 0, &persistent);
+ /* hdev->lock can't be taken here since it is ordered before
+ * the l2cap channel lock held by the caller.
+ */
+ mutex_lock(&hdev->link_keys_lock);
+
+ key = __hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
+ smp->link_key, type, 0, &persistent);
if (key) {
mgmt_new_link_key(hdev, key, persistent);
@@ -1111,6 +1116,8 @@ static void smp_notify_keys(struct l2cap_conn *conn)
kfree_rcu(key, rcu);
}
}
+
+ mutex_unlock(&hdev->link_keys_lock);
}
}
--
2.56.0
next prev parent reply other threads:[~2026-11-17 18:57 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 18:57 [PATCH v1 00/16] Bluetooth: Fixes for issues reported by sashiko.dev Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 01/16] Bluetooth: mgmt: Fix read_index_list() buffer overflow Luiz Augusto von Dentz
2026-11-17 20:13 ` Bluetooth: Fixes for issues reported by sashiko.dev bluez.test.bot
2026-11-17 18:57 ` [PATCH v1 02/16] Bluetooth: hci_core: Take req_sync lock in hci_power_on() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 03/16] Bluetooth: hci_sync: Lock the lookup in hci_disable_ext_adv_instance_sync() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 04/16] Bluetooth: hci_sync: Hold hdev->lock in hci_schedule_adv_instance_sync() Luiz Augusto von Dentz
2026-11-17 18:57 ` Luiz Augusto von Dentz [this message]
2026-11-17 18:57 ` [PATCH v1 06/16] Bluetooth: SMP: Check smp_data before using SMP_DEV() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 07/16] Bluetooth: SMP: Don't create the BR/EDR channel while unregistered Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 08/16] Bluetooth: hci_event: Validate sent command length in LE Set Ext Adv Enable Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 09/16] Bluetooth: mgmt: Reject zero multi advertising rotation duration Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 10/16] Bluetooth: L2CAP: Don't use uninitialized EFS in l2cap_parse_conf_rsp() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 11/16] Bluetooth: mgmt: Fix mesh transmission locking and lifetime Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 12/16] Bluetooth: MGMT: Fix use-after-free in mgmt_powering_down() Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 13/16] Bluetooth: MSFT: Protect the event prefix with hdev->lock Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 14/16] Bluetooth: hci_debugfs: Fix racy toggling of debugfs controlled flags Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 15/16] Bluetooth: hci_core: Hold hdev->lock when removing advertisement monitors Luiz Augusto von Dentz
2026-11-17 18:57 ` [PATCH v1 16/16] Bluetooth: RFCOMM: Serialize tty device reparenting Luiz Augusto von Dentz
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=20261117185737.3758255-6-luiz.dentz@gmail.com \
--to=luiz.dentz@gmail.com \
--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