Skip to contentSkip to conversation

Bluetooth: SMP: Avoid stale pairing key reads

PATCH In review Cen Zhang 2 participants 4 messages Nov 21

Subsystems (2)
lore
Share & tools
lore

Cen Zhang

author
#0 lore raw

cover Bluetooth: SMP: Avoid stale pairing key reads

Management can reload a key registry while a pairing context is still alive. These changes use the pairing's own flags and identity data for the debug-key decision and IRK notification, avoiding reads through two saved registry pointers.

Cen Zhang (2):
Bluetooth: SMP: Use pairing state for the debug key check
Bluetooth: SMP: Use pairing data for IRK notifications

 net/bluetooth/smp.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

---

author
#1 lore raw

PATCH 1/2 Bluetooth: SMP: Use pairing state for the debug key check

A management LTK reload can reclaim the key saved in smp->ltk after notification and before the successful pairing destroys its context. load_long_term_keys() holds hdev->lock, but SMP receive processing runs after the ACL receive path drops that lock:

SMP receive Management / RCU
save smp->ltk
notify the key
clear the LTK list reclaim the saved key
smp_chan_destroy()

    read smp->ltk->type

The destructor reads the key type even for an ordinary successful pairing which needs no key removal. Derive the debug-key decision from the pairing's Secure Connections and debug-key flags instead. Requiring both flags preserves legacy fallback behavior without reading the registry entry just to decide whether it is a debug key.

The excerpt below retains the headline and stack traces.

KASAN report as below:

    BUG: KASAN: slab-use-after-free in smp_chan_destroy.isra.0+0xafd/0xe80
    Read of size 1 at addr ffff88810c140928 by task kworker/u17:0/501

    Workqueue: hci0 hci_rx_work
    Call Trace:
     <TASK>
     dump_stack_lvl+0x93/0xd0
     print_report+0xce/0x630
     ? smp_chan_destroy.isra.0+0xafd/0xe80
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __virt_addr_valid+0x20d/0x410
     ? smp_chan_destroy.isra.0+0xafd/0xe80
     kasan_report+0xe0/0x110
     ? smp_chan_destroy.isra.0+0xafd/0xe80
     smp_chan_destroy.isra.0+0xafd/0xe80
     smp_distribute_keys+0xc57/0x29a0
     ? __pfx_smp_distribute_keys+0x10/0x10
     ? kasan_save_track+0x14/0x30
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __kasan_kmalloc+0xaa/0xb0
     ? smp_recv_cb+0x2feb/0x8970
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __kmalloc_cache_noprof+0x251/0x610
     ? smp_recv_cb+0x2feb/0x8970
     ? smp_recv_cb+0x30de/0x8970
     ? srso_alias_return_thunk+0x5/0xfbef5
     smp_recv_cb+0x30de/0x8970
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __pfx_smp_recv_cb+0x10/0x10
     ? __pfx___might_resched+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? find_held_lock+0x2b/0x80
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __mutex_lock+0x182c/0x2590
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? l2cap_recv_frame+0x9ef/0x8830
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? _raw_spin_unlock_irqrestore+0x27/0x60
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? try_to_wake_up+0xf98/0x1b80
     ? __pfx___mutex_lock+0x10/0x10
     ? __pfx_try_to_wake_up+0x10/0x10
     ? __lock_acquire+0x466/0x2260
     ? __pfx_l2cap_chan_hold_unless_zero+0x10/0x10
     ? l2cap_recv_frame+0x13cc/0x8830
     ? __pfx_smp_recv_cb+0x10/0x10
     l2cap_recv_frame+0x13cc/0x8830
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __mutex_lock+0x182c/0x2590
     ? __pfx_l2cap_recv_frame+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? find_held_lock+0x2b/0x80
     ? l2cap_recv_acldata+0x1eb/0x10e0
     ? __pfx___mutex_lock+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __mutex_unlock_slowpath+0x147/0x920
     ? __pfx_l2cap_conn_hold_unless_zero+0x10/0x10
     ? __pfx_hci_conn_enter_active_mode+0x10/0x10
     ? l2cap_recv_acldata+0x13d/0x10e0
     ? srso_alias_return_thunk+0x5/0xfbef5
     l2cap_recv_acldata+0xe9f/0x10e0
     ? srso_alias_return_thunk+0x5/0xfbef5
     hci_rx_work+0x5a6/0xf00
     ? srso_alias_return_thunk+0x5/0xfbef5
     process_one_work+0x908/0x19c0
     ? __pfx_process_one_work+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? lock_is_held_type+0x8f/0x100
     ? srso_alias_return_thunk+0x5/0xfbef5
     worker_thread+0x65c/0xe40
     ? __pfx_worker_thread+0x10/0x10
     kthread+0x34f/0x460
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __pfx_kthread+0x10/0x10
     ret_from_fork+0x659/0x940
     ? __pfx_ret_from_fork+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __switch_to+0x74f/0xf80
     ? __pfx_kthread+0x10/0x10
     ret_from_fork_asm+0x1a/0x30
     </TASK>

    Allocated by task 502:
     kasan_save_stack+0x33/0x60
     kasan_save_track+0x14/0x30
     __kasan_kmalloc+0xaa/0xb0
     __kmalloc_cache_noprof+0x251/0x610
     hci_add_ltk+0x1f5/0x3a0
     sc_add_ltk+0x174/0x350
     smp_recv_cb+0x62e8/0x8970
     l2cap_recv_frame+0x13cc/0x8830
     l2cap_recv_acldata+0xe9f/0x10e0
     hci_rx_work+0x5a6/0xf00
     process_one_work+0x908/0x19c0
     worker_thread+0x65c/0xe40
     kthread+0x34f/0x460
     ret_from_fork+0x659/0x940
     ret_from_fork_asm+0x1a/0x30

    Freed by task 520:
     kasan_save_stack+0x33/0x60
     kasan_save_track+0x14/0x30
     kasan_save_free_info+0x3b/0x60
     __kasan_slab_free+0x5f/0x80
     __rcu_free_sheaf_prepare+0x65/0x2a0
     rcu_free_sheaf+0x1f/0xe0
     rcu_core+0x661/0x1d10
     handle_softirqs+0x201/0x930
     __irq_exit_rcu+0x110/0x1e0
     irq_exit_rcu+0xe/0x20
     sysvec_apic_timer_interrupt+0x37/0x80
     asm_sysvec_apic_timer_interrupt+0x1a/0x20
Fixes 923e24143efe ("Bluetooth: Fix SMP debug key handling")

---
This patch was written with assistance from Codex (OpenAI).

 net/bluetooth/smp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
net/bluetooth/smp.c +2 -1
index f8f86b8f73f05..ead1b8e3e361a 100644
@@ -745,7 +745,8 @@ static void smp_chan_destroy(struct l2cap_conn *conn)
 	/* Ensure that we don't leave any debug key around if debug key
 	 * support hasn't been explicitly enabled.
 	 */
-	if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
+	if (smp->ltk && test_bit(SMP_FLAG_SC, &smp->flags) &&
+	    test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags) &&
 	    !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
 		list_del_rcu(&smp->ltk->list);
 		kfree_rcu(smp->ltk, rcu);
#2 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1217941/

---Test result---

Test Summary:

CheckPatch                    PASS      1.20 seconds
VerifyFixes                   PASS      0.12 seconds
VerifySignedoff               PASS      0.12 seconds
GitLint                       PASS      0.60 seconds
SubjectPrefix                 PASS      0.26 seconds
BuildKernel                   PASS      20.45 seconds
CheckAllWarning               PASS      23.43 seconds
CheckSparse                   PASS      24.73 seconds
BuildKernel32                 PASS      20.39 seconds
CheckKernelLLVM               PASS      21.77 seconds
TestRunnerSetup               PASS      481.22 seconds
TestRunner_smp-tester         PASS      5.55 seconds
IncrementalBuild              PASS      21.11 seconds

https://github.com/bluez/bluetooth-next/pull/1172

---
Regards,
Linux Bluetooth

author
#3 lore raw

PATCH 2/2 Bluetooth: SMP: Use pairing data for IRK notifications

In reply to Cen Zhang

The IRK saved in smp->remote_irk can be reclaimed by Load IRKs before pairing completes. The management device lock does not serialize with the SMP receive callback, and the finder's RCU section has already ended:

SMP pairing Management / RCU
save IRK A in remote_irk
clear A and reclaim it allocate B at A's address clear B and reclaim it
smp_notify_keys()
mgmt_new_irk(remote_irk)

The notification can read freed storage or report a replacement key. The later connection-address update also follows the saved pointer.

Build a temporary IRK from the identity address, key and RPA already owned by the pairing context, and use that data for both the event and the connection-address update. No registry pointer is dereferenced by these notification operations.

The excerpt below retains the headline and stack traces.

Use a copy of the IRK for the later notification, as commit 9ff737119177 ("Bluetooth: hci_sync: Copy the resolving-list IRK under RCU") does for the resolving list. The pairing context supplies the identity data and key used here.

KASAN report as below:

    BUG: KASAN: slab-use-after-free in mgmt_new_irk+0x296/0x2b0
    Read of size 6 at addr ffff888110c0b8a0 by task kworker/u17:0/495

    Workqueue: hci1 hci_rx_work
    Call Trace:
     <TASK>
     dump_stack_lvl+0x93/0xd0
     print_report+0xce/0x630
     ? mgmt_new_irk+0x296/0x2b0
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __virt_addr_valid+0x20d/0x410
     ? mgmt_new_irk+0x296/0x2b0
     kasan_report+0xe0/0x110
     ? mgmt_new_irk+0x296/0x2b0
     mgmt_new_irk+0x296/0x2b0
     ? __pfx_mgmt_new_irk+0x10/0x10
     ? __pfx_pmbd_probe_hit_cookie+0x10/0x10
     smp_distribute_keys+0x54b/0x28a0
     ? __pfx_smp_distribute_keys+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? kasan_save_track+0x14/0x30
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __kasan_kmalloc+0xaa/0xb0
     ? smp_recv_cb+0x4d31/0x8de0
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __kmalloc_cache_noprof+0x251/0x610
     ? smp_recv_cb+0x4d31/0x8de0
     ? smp_recv_cb+0x4e24/0x8de0
     ? srso_alias_return_thunk+0x5/0xfbef5
     smp_recv_cb+0x4e24/0x8de0
     ? __pfx_smp_recv_cb+0x10/0x10
     ? __pfx___might_resched+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? stack_depot_save_flags+0x29/0x9a0
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __mutex_lock+0x182c/0x2590
     ? kasan_save_stack+0x42/0x60
     ? l2cap_recv_frame+0x9ef/0x8830
     ? __kasan_slab_free+0x5f/0x80
     ? kmem_cache_free+0x242/0x580
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? find_held_lock+0x2b/0x80
     ? l2cap_recv_frame+0xaef/0x8830
     ? __pfx___mutex_lock+0x10/0x10
     ? __lock_acquire+0x466/0x2260
     ? __pfx_l2cap_chan_hold_unless_zero+0x10/0x10
     ? l2cap_recv_frame+0x13cc/0x8830
     ? __pfx_smp_recv_cb+0x10/0x10
     l2cap_recv_frame+0x13cc/0x8830
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __mutex_lock+0x182c/0x2590
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __pfx_l2cap_recv_frame+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? find_held_lock+0x2b/0x80
     ? l2cap_recv_acldata+0x1eb/0x10e0
     ? __pfx___mutex_lock+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __mutex_unlock_slowpath+0x147/0x920
     ? __pfx_l2cap_conn_hold_unless_zero+0x10/0x10
     ? __pfx_hci_conn_enter_active_mode+0x10/0x10
     ? l2cap_recv_acldata+0x13d/0x10e0
     ? srso_alias_return_thunk+0x5/0xfbef5
     l2cap_recv_acldata+0xe9f/0x10e0
     ? srso_alias_return_thunk+0x5/0xfbef5
     hci_rx_work+0x5a6/0xf00
     ? srso_alias_return_thunk+0x5/0xfbef5
     process_one_work+0x908/0x19c0
     ? __pfx_process_one_work+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? lock_is_held_type+0x8f/0x100
     ? srso_alias_return_thunk+0x5/0xfbef5
     worker_thread+0x65c/0xe40
     ? __pfx_worker_thread+0x10/0x10
     kthread+0x34f/0x460
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __pfx_kthread+0x10/0x10
     ret_from_fork+0x659/0x940
     ? __pfx_ret_from_fork+0x10/0x10
     ? srso_alias_return_thunk+0x5/0xfbef5
     ? __switch_to+0x74f/0xf80
     ? __pfx_kthread+0x10/0x10
     ret_from_fork_asm+0x1a/0x30
     </TASK>

    Allocated by task 802:
     kasan_save_stack+0x33/0x60
     kasan_save_track+0x14/0x30
     __kasan_kmalloc+0xaa/0xb0
     __kmalloc_cache_noprof+0x251/0x610
     hci_add_irk+0x13e/0x2d0
     load_irks+0x5bc/0x7d0
     hci_sock_sendmsg+0x128b/0x22e0
     __sys_sendto+0x425/0x470
     __x64_sys_sendto+0xe5/0x1c0
     do_syscall_64+0x115/0x6a0
     entry_SYSCALL_64_after_hwframe+0x77/0x7f

    Freed by task 802:
     kasan_save_stack+0x33/0x60
     kasan_save_track+0x14/0x30
     kasan_save_free_info+0x3b/0x60
     __kasan_slab_free+0x5f/0x80
     __rcu_free_sheaf_prepare+0x65/0x2a0
     rcu_free_sheaf+0x1f/0xe0
     rcu_core+0x661/0x1d10
     handle_softirqs+0x201/0x930
     __irq_exit_rcu+0x110/0x1e0
     irq_exit_rcu+0xe/0x20
     sysvec_apic_timer_interrupt+0x6c/0x80
     asm_sysvec_apic_timer_interrupt+0x1a/0x20
Fixes 95fbac8a8e45 ("Bluetooth: Add support for sending New IRK event")

---
This patch was written with assistance from Codex (OpenAI).

 net/bluetooth/smp.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
net/bluetooth/smp.c +15 -3
index ead1b8e3e361a..c758f7c927af6 100644
@@ -1045,15 +1045,27 @@ static void smp_notify_keys(struct l2cap_conn *conn)
 	}
 
 	if (smp->remote_irk) {
-		mgmt_new_irk(hdev, smp->remote_irk, persistent);
+		struct smp_irk irk = {
+			.addr_type = smp->id_addr_type,
+		};
+
+		/* The IRK list may have been replaced during pairing.
+		 * Report the pairing data instead of the saved list entry.
+		 */
+		bacpy(&irk.bdaddr, &smp->id_addr);
+		memcpy(irk.val, smp->irk, sizeof(irk.val));
+		if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
+			bacpy(&irk.rpa, &hcon->dst);
+
+		mgmt_new_irk(hdev, &irk, persistent);
 
 		/* Now that user space can be considered to know the
 		 * identity address track the connection based on it
 		 * from now on (assuming this is an LE link).
 		 */
 		if (hcon->type == LE_LINK) {
-			bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
-			hcon->dst_type = smp->remote_irk->addr_type;
+			bacpy(&hcon->dst, &smp->id_addr);
+			hcon->dst_type = smp->id_addr_type;
 			/* Use a short delay to make sure the new address is
 			 * propagated _before_ the channels.
 			 */