From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796386199; b=Zfa/0E5oRRzhQTZrF3wyInlHcs7BYg+TVj+A7tRBvBakzRk0q5W/ZKs+erygk4ybEgip m2w/Vd1Tr2PWlBIrR0Q3n4vLlFleCtbtzQDEJIGcOLwUP9dm6wttShGHA7ONr/BNCm03Q xqNlFZws8BM4emgmpEmqW+2FTsvX61X/A+jWUNkzXra1qpTI4be1D7d5JDNQYJnXFM7qO bUnZrZAWzDyUChWsk/M2TS2jwEEXymYmT33Xz3AyfInC3qIvQvJxVcoq3A7JYo4pw3d7i AnePqjy9kkUEqo832STYRaM1gHLLbi+2aZ9rH6O+NI6MHF8MV7KFY6J5F4hlmqC1Vwg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796386199; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=2dd7ZuzryI4OKiizVKAVSbK4tPsvk1SErh7bMf81VNs=; b=4J8cGQ/fo7p4NKbT2ciU4z7XSylrI+E1kTtLZtIN37HZ4+zqeIWkTMeSc5LNTpvY4N0U 12W0mlOQkag3sUqqGWV3uVDM6HRmaPX9AVmgRxU/3aC7l1yovtejr6NI0K7EV/cuIkC9j wTwI3x5g0R5jdade1hB2ANoTaFF5rKexnD+e6YCerZx9Vu8Ox3Bp/xn32qNa1mpHjRfYf I3E/qwR7XgT/hDRRViofbrIlhnhmK18Ymxo7XXsRvOy1BLFENAx4X+P0TYzEDR1OMGWbA DqYyZDAbG6Wr8cypCdn3n2mbt2m47R3pfFUn2dZiKSgC0wCW2SvjPN7lWLQ4GvJ1tQA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b="lu1MO/ig"; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B492D606A322; Fri, 4 Dec 2026 12:09:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=lu1MO/ig Received: by smtp.kernel.org (Postfix) with ESMTPSA id 341571F000CE; Fri, 4 Dec 2026 12:09:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796386199; bh=2dd7ZuzryI4OKiizVKAVSbK4tPsvk1SErh7bMf81VNs=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=lu1MO/igTidF7pmn1hArNlb/clFJy53GD9PCgd3QuB17iIZXjulyqsbZoDsjQRpLa ReeHA36cUPp6iokvXVPhhWJCG0LZ2Xg5nE4HegOY2tRz5cepeNxl25ONFTXVsNYvV6 945+0ekOH2xifsaNrs/QX5Y9nXs7TOBV0dG08Gfg= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Sashiko , Luiz Augusto von Dentz , Sasha Levin Subject: [PATCH 6.6 0868/1167] Bluetooth: SMP: Serialize link key updates from smp_notify_keys() Date: Fri, 4 Dec 2026 19:04:55 +0900 Message-ID: <20261204100724.727320555@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.6-stable review patch. If anyone has any objections, please let me know. ------------------ From: Luiz Augusto von Dentz [ Upstream commit e428ab70fb3398e835e5d70ba4839e7c06583b83 ] 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). SMP holds it while it adds the key, notifies mgmt and removes a debug key, so the key can't go away meanwhile, and the debug key removed by hci_link_key_notify_evt() is deleted by reference with hci_del_link_key() since a lookup by address skips the keys that are blocked. Reported-by: Sashiko Link: https://sashiko.dev/#/bug/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 Signed-off-by: Sasha Levin --- include/net/bluetooth/hci_core.h | 6 ++++ net/bluetooth/hci_core.c | 62 +++++++++++++++++++++++++++++--- net/bluetooth/hci_event.c | 3 +- net/bluetooth/smp.c | 11 ++++-- 4 files changed, 74 insertions(+), 8 deletions(-) diff --git a/include/net/bluetooth/hci_core.h b/include/net/bluetooth/hci_core.h index 4ac6f5547e6dd..af424b9ef0a03 100644 --- a/include/net/bluetooth/hci_core.h +++ b/include/net/bluetooth/hci_core.h @@ -553,6 +553,8 @@ struct hci_dev { struct list_head accept_list; struct list_head uuids; struct list_head link_keys; + /* Serializes the writers of link_keys, see hci_add_link_key() */ + struct mutex link_keys_lock; struct list_head long_term_keys; struct list_head identity_resolving_keys; struct mutex remote_oob_lock; @@ -1753,6 +1755,10 @@ 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); +void hci_del_link_key(struct hci_dev *hdev, struct link_key *key); 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 7b198f73599e3..68c585fd5d945 100644 --- a/net/bluetooth/hci_core.c +++ b/net/bluetooth/hci_core.c @@ -1047,10 +1047,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) @@ -1269,13 +1273,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; @@ -1318,6 +1325,26 @@ struct link_key *hci_add_link_key(struct hci_dev *hdev, struct hci_conn *conn, return key; } +/* The writers of hdev->link_keys are serialized with hdev->link_keys_lock + * so the list can be modified without hdev->lock (e.g. from SMP, where it + * can't be taken after the l2cap channel lock). The returned key is only + * safe to use while hdev->lock is held, callers without it must use + * __hci_add_link_key() and keep hdev->link_keys_lock while using the key. + */ +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) @@ -1379,18 +1406,44 @@ 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; } +/* Remove a key previously returned by hci_add_link_key(), unlike + * hci_remove_link_key() this also works for keys that are blocked. + */ +void hci_del_link_key(struct hci_dev *hdev, struct link_key *key) +{ + struct link_key *k; + + mutex_lock(&hdev->link_keys_lock); + + list_for_each_entry(k, &hdev->link_keys, list) { + if (k == key) { + list_del_rcu(&key->list); + kfree_rcu(key, rcu); + break; + } + } + + mutex_unlock(&hdev->link_keys_lock); +} + int hci_remove_ltk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 bdaddr_type) { struct smp_ltk *k, *tmp; @@ -2530,6 +2583,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 ffb4a268cc25e..f9763f73d4ae4 100644 --- a/net/bluetooth/hci_event.c +++ b/net/bluetooth/hci_event.c @@ -4744,8 +4744,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_del_link_key(hdev, key); goto unlock; } diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c index cd224215e08c4..dc556a998ee6e 100644 --- a/net/bluetooth/smp.c +++ b/net/bluetooth/smp.c @@ -1113,8 +1113,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); @@ -1127,6 +1132,8 @@ static void smp_notify_keys(struct l2cap_conn *conn) kfree_rcu(key, rcu); } } + + mutex_unlock(&hdev->link_keys_lock); } } -- 2.53.0