Skip to contentSkip to conversation

Bluetooth: hci_core: Fix use-after-free in vhci_flush()

PATCH Superseded Kuniyuki Iwashima 4 participants 11 messages Jun 16, 2025 – Jun 17, 2025

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Bluetooth: hci_core: Fix use-after-free in vhci_flush()

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

From the splat, a thread close()d a vhci file descriptor while its device was being used by iotcl() on another thread.

Once the last fd refcnt is released, vhci_release() calls hci_unregister_dev(), hci_free_dev(), and kfree() for struct vhci_data, which is set to hci_dev->dev->driver_data.

The problem is that there is no synchronisation after unlinking hdev from hci_dev_list in hci_unregister_dev(). There might be another thread still accessing the hdev which was fetched before the unlink operation.

We can use SRCU for such synchronisation.

Let's run hci_dev_reset() under SRCU and wait for its completion in hci_unregister_dev().

Another option would be to restore hci_dev->destruct(), which was removed in commit 587ae086f6e4 ("Bluetooth: Remove unused hci-destruct cb"). However, this would not be a good solution, as we should not run hci_unregister_dev() while there are in-flight ioctl() requests, which could lead to another data-race KCSAN splat.

Note that other drivers seem to have the same problem, for exmaple, virtbt_remove().

[0]:

BUG: KASAN: slab-use-after-free in skb_queue_empty_lockless include/linux/skbuff.h:1891 [inline]
BUG: KASAN: slab-use-after-free in skb_queue_purge_reason+0x99/0x360 net/core/skbuff.c:3937
Read of size 8 at addr ffff88807cb8d858 by task syz.1.219/6718

CPU: 1 UID: 0 PID: 6718 Comm: syz.1.219 Not tainted 6.16.0-rc1-syzkaller-00196-g08207f42d3ff #0 PREEMPT(full)
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 05/07/2025
Call Trace:
 <TASK>
 dump_stack_lvl+0x189/0x250 lib/dump_stack.c:120
 print_address_description mm/kasan/report.c:408 [inline]
 print_report+0xd2/0x2b0 mm/kasan/report.c:521
 kasan_report+0x118/0x150 mm/kasan/report.c:634
 skb_queue_empty_lockless include/linux/skbuff.h:1891 [inline]
 skb_queue_purge_reason+0x99/0x360 net/core/skbuff.c:3937
 skb_queue_purge include/linux/skbuff.h:3368 [inline]
 vhci_flush+0x44/0x50 drivers/bluetooth/hci_vhci.c:69
 hci_dev_do_reset net/bluetooth/hci_core.c:552 [inline]
 hci_dev_reset+0x420/0x5c0 net/bluetooth/hci_core.c:592
 sock_do_ioctl+0xd9/0x300 net/socket.c:1190
 sock_ioctl+0x576/0x790 net/socket.c:1311
 vfs_ioctl fs/ioctl.c:51 [inline]
 __do_sys_ioctl fs/ioctl.c:907 [inline]
 __se_sys_ioctl+0xf9/0x170 fs/ioctl.c:893
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7fcf5b98e929
Code: ff ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 40 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 a8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007fcf5c7b9038 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007fcf5bbb6160 RCX: 00007fcf5b98e929
RDX: 0000000000000000 RSI: 00000000400448cb RDI: 0000000000000009
RBP: 00007fcf5ba10b39 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 0000000000000000 R14: 00007fcf5bbb6160 R15: 00007ffd6353d528
 </TASK>

Allocated by task 6535:
 kasan_save_stack mm/kasan/common.c:47 [inline]
 kasan_save_track+0x3e/0x80 mm/kasan/common.c:68
 poison_kmalloc_redzone mm/kasan/common.c:377 [inline]
 __kasan_kmalloc+0x93/0xb0 mm/kasan/common.c:394
 kasan_kmalloc include/linux/kasan.h:260 [inline]
 __kmalloc_cache_noprof+0x230/0x3d0 mm/slub.c:4359
 kmalloc_noprof include/linux/slab.h:905 [inline]
 kzalloc_noprof include/linux/slab.h:1039 [inline]
 vhci_open+0x57/0x360 drivers/bluetooth/hci_vhci.c:635
 misc_open+0x2bc/0x330 drivers/char/misc.c:161
 chrdev_open+0x4c9/0x5e0 fs/char_dev.c:414
 do_dentry_open+0xdf0/0x1970 fs/open.c:964
 vfs_open+0x3b/0x340 fs/open.c:1094
 do_open fs/namei.c:3887 [inline]
 path_openat+0x2ee5/0x3830 fs/namei.c:4046
 do_filp_open+0x1fa/0x410 fs/namei.c:4073
 do_sys_openat2+0x121/0x1c0 fs/open.c:1437
 do_sys_open fs/open.c:1452 [inline]
 __do_sys_openat fs/open.c:1468 [inline]
 __se_sys_openat fs/open.c:1463 [inline]
 __x64_sys_openat+0x138/0x170 fs/open.c:1463
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

Freed by task 6535:
 kasan_save_stack mm/kasan/common.c:47 [inline]
 kasan_save_track+0x3e/0x80 mm/kasan/common.c:68
 kasan_save_free_info+0x46/0x50 mm/kasan/generic.c:576
 poison_slab_object mm/kasan/common.c:247 [inline]
 __kasan_slab_free+0x62/0x70 mm/kasan/common.c:264
 kasan_slab_free include/linux/kasan.h:233 [inline]
 slab_free_hook mm/slub.c:2381 [inline]
 slab_free mm/slub.c:4643 [inline]
 kfree+0x18e/0x440 mm/slub.c:4842
 vhci_release+0xbc/0xd0 drivers/bluetooth/hci_vhci.c:671
 __fput+0x44c/0xa70 fs/file_table.c:465
 task_work_run+0x1d1/0x260 kernel/task_work.c:227
 exit_task_work include/linux/task_work.h:40 [inline]
 do_exit+0x6ad/0x22e0 kernel/exit.c:955
 do_group_exit+0x21c/0x2d0 kernel/exit.c:1104
 __do_sys_exit_group kernel/exit.c:1115 [inline]
 __se_sys_exit_group kernel/exit.c:1113 [inline]
 __x64_sys_exit_group+0x3f/0x40 kernel/exit.c:1113
 x64_sys_call+0x21ba/0x21c0 arch/x86/include/generated/asm/syscalls_64.h:232
 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
 do_syscall_64+0xfa/0x3b0 arch/x86/entry/syscall_64.c:94
 entry_SYSCALL_64_after_hwframe+0x77/0x7f

The buggy address belongs to the object at ffff88807cb8d800
 which belongs to the cache kmalloc-1k of size 1024

The buggy address is located 88 bytes inside of
freed 1024-byte region [ffff88807cb8d800, ffff88807cb8dc00)

Fixes bf18c7118cf8 ("Bluetooth: vhci: Free driver_data on file release")
Reported-by [email protected]
---
 include/net/bluetooth/hci_core.h |  2 ++
 net/bluetooth/hci_core.c         | 40 +++++++++++++++++++++++++-------
 2 files changed, 34 insertions(+), 8 deletions(-)
include/net/bluetooth/hci_core.h +2 -0
index a760f05fa3fb..9fc8f544e20e 100644
@@ -29,6 +29,7 @@
 #include <linux/idr.h>
 #include <linux/leds.h>
 #include <linux/rculist.h>
+#include <linux/srcu.h>
 
 #include <net/bluetooth/hci.h>
 #include <net/bluetooth/hci_drv.h>
@@ -347,6 +348,7 @@ struct adv_monitor {
 
 struct hci_dev {
 	struct list_head list;
+	struct srcu_struct srcu;
 	struct mutex	lock;
 
 	struct ida	unset_handle_ida;
net/bluetooth/hci_core.c +32 -8
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-	struct hci_dev *hdev = NULL, *d;
+	struct hci_dev *hdev = NULL;
 
 	BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
 		return NULL;
 
 	read_lock(&hci_dev_list_lock);
-	list_for_each_entry(d, &hci_dev_list, list) {
-		if (d->id == index) {
-			hdev = hci_dev_hold(d);
+	list_for_each_entry(hdev, &hci_dev_list, list) {
+		if (hdev->id == index) {
+			hci_dev_hold(hdev);
+			if (srcu_index)
+				*srcu_index = srcu_read_lock(&hdev->srcu);
 			break;
 		}
 	}
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
 	return hdev;
 }
 
+struct hci_dev *hci_dev_get(int index)
+{
+	return __hci_dev_get(index, NULL);
+}
+
+static struct hci_dev *hci_dev_get_srcu(int index, int *srcu_index)
+{
+	return __hci_dev_get(index, srcu_index);
+}
+
+static void hci_dev_put_srcu(struct hci_dev *hdev, int srcu_index)
+{
+	srcu_read_unlock(&hdev->srcu, srcu_index);
+	hci_dev_put(hdev);
+}
+
 /* ---- Inquiry support ---- */
 
 bool hci_discovery_active(struct hci_dev *hdev)
@@ -568,9 +586,9 @@ static int hci_dev_do_reset(struct hci_dev *hdev)
 int hci_dev_reset(__u16 dev)
 {
 	struct hci_dev *hdev;
-	int err;
+	int err, srcu_index;
 
-	hdev = hci_dev_get(dev);
+	hdev = hci_dev_get_srcu(dev, &srcu_index);
 	if (!hdev)
 		return -ENODEV;
 
@@ -592,7 +610,7 @@ int hci_dev_reset(__u16 dev)
 	err = hci_dev_do_reset(hdev);
 
 done:
-	hci_dev_put(hdev);
+	hci_dev_put_srcu(hdev, srcu_index);
 	return err;
 }
 
@@ -2433,6 +2451,9 @@ struct hci_dev *hci_alloc_dev_priv(int sizeof_priv)
 	if (!hdev)
 		return NULL;
 
+	if (init_srcu_struct(&hdev->srcu))
+		return NULL;
+
 	hdev->pkt_type  = (HCI_DM1 | HCI_DH1 | HCI_HV1);
 	hdev->esco_type = (ESCO_HV1);
 	hdev->link_mode = (HCI_LM_ACCEPT);
@@ -2678,6 +2699,9 @@ void hci_unregister_dev(struct hci_dev *hdev)
 	list_del(&hdev->list);
 	write_unlock(&hci_dev_list_lock);
 
+	synchronize_srcu(&hdev->srcu);
+	cleanup_srcu_struct(&hdev->srcu);
+
 	disable_work_sync(&hdev->rx_work);
 	disable_work_sync(&hdev->cmd_work);
 	disable_work_sync(&hdev->tx_work);
Signature · 1 line
2.49.0
#1 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/project/bluetooth/list/?series=972625

---Test result---

Test Summary:

CheckPatch                    PENDING   0.41 seconds
GitLint                       PENDING   0.33 seconds
SubjectPrefix                 PASS      0.06 seconds
BuildKernel                   PASS      24.34 seconds
CheckAllWarning               PASS      26.59 seconds
CheckSparse                   WARNING   29.82 seconds
BuildKernel32                 PASS      24.01 seconds
TestRunnerSetup               PASS      455.63 seconds
TestRunner_l2cap-tester       PASS      24.91 seconds
TestRunner_iso-tester         FAIL      7.81 seconds
TestRunner_bnep-tester        PASS      5.75 seconds
TestRunner_mgmt-tester        FAIL      130.99 seconds
TestRunner_rfcomm-tester      PASS      9.15 seconds
TestRunner_sco-tester         PASS      14.63 seconds
TestRunner_ioctl-tester       PASS      9.81 seconds
TestRunner_mesh-tester        PASS      7.26 seconds
TestRunner_smp-tester         PASS      8.34 seconds
TestRunner_userchan-tester    PASS      6.11 seconds
IncrementalBuild              PENDING   0.84 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: CheckSparse - WARNING
Desc: Run sparse tool with linux kernel
Output:
net/bluetooth/hci_core.c:85:9: warning: context imbalance in '__hci_dev_get' - different lock contexts for basic blocknet/bluetooth/hci_core.c: note: in included file (through include/linux/notifier.h, include/linux/memory_hotplug.h, include/linux/mmzone.h, include/linux/gfp.h, include/linux/xarray.h, include/linux/radix-tree.h, ...):
##############################
Test: TestRunner_iso-tester - FAIL
Desc: Run iso-tester with test-runner
Output:
No test result found
##############################
Test: TestRunner_mgmt-tester - FAIL
Desc: Run mgmt-tester with test-runner
Output:
Total: 490, Passed: 483 (98.6%), Failed: 3, Not Run: 4

Failed Test Cases

LL Privacy - Set Flags 1 (Add to RL)                 Failed       0.179 seconds
LL Privacy - Set Flags 2 (Enable RL)                 Failed       0.162 seconds
LL Privacy - Set Device Flag 1 (Device Privacy)      Failed       0.165 seconds

##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:

---
Regards,
Linux Bluetooth

#2 lore raw
In reply to Kuniyuki Iwashima

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 32 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-	struct hci_dev *hdev = NULL, *d;
+	struct hci_dev *hdev = NULL;
 
 	BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
 		return NULL;
 
 	read_lock(&hci_dev_list_lock);
-	list_for_each_entry(d, &hci_dev_list, list) {
-		if (d->id == index) {
-			hdev = hci_dev_hold(d);
+	list_for_each_entry(hdev, &hci_dev_list, list) {
+		if (hdev->id == index) {
+			hci_dev_hold(hdev);
+			if (srcu_index)
+				*srcu_index = srcu_read_lock(&hdev->srcu);
 			break;
 		}
 	}

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 2 lines
net/bluetooth/hci_core.c +0 -0
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
 	return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Signature · 1 line
Pauli Virtanen
#3 lore raw

From: Pauli Virtanen <[email protected]>
Date: Mon, 16 Jun 2025 21:12:10 +0300

Quoted 37 lines

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 29 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-	struct hci_dev *hdev = NULL, *d;
+	struct hci_dev *hdev = NULL;
 
 	BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
 		return NULL;
 
 	read_lock(&hci_dev_list_lock);
-	list_for_each_entry(d, &hci_dev_list, list) {
-		if (d->id == index) {
-			hdev = hci_dev_hold(d);
+	list_for_each_entry(hdev, &hci_dev_list, list) {
+		if (hdev->id == index) {
+			hci_dev_hold(hdev);
+			if (srcu_index)
+				*srcu_index = srcu_read_lock(&hdev->srcu);
 			break;
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
 		}
 	}
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
 	return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Oh right, thanks for catching this!

I placed read_lock() outside of __hci_dev_get() and returned NULL in v0, probably that's why.

I'll keep the *d as is in v3.

Thanks!

#4 lore raw
In reply to Pauli Virtanen

Hi Pauli, Kuniyuki,

Quoted 38 lines · Pauli Virtanen

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 29 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-     struct hci_dev *hdev = NULL, *d;
+     struct hci_dev *hdev = NULL;
 
      BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
              return NULL;
 
      read_lock(&hci_dev_list_lock);
-     list_for_each_entry(d, &hci_dev_list, list) {
-             if (d->id == index) {
-                     hdev = hci_dev_hold(d);
+     list_for_each_entry(hdev, &hci_dev_list, list) {
+             if (hdev->id == index) {
+                     hci_dev_hold(hdev);
+                     if (srcu_index)
+                             *srcu_index = srcu_read_lock(&hdev->srcu);
                      break;
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
              }
      }
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
      return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Seems wrong to me as well, *d was used to hold the current entry but that has been removed so hdev would be used instead which may return a valid/non-NULL entry even when its index doesn't match. Btw, are there any documentation regarding the usage of SRCU in such cases where there are still references?

Usually the hci_unregister_dev is called by the driver to inform the hardware has been unplugged from the system, so we do want to be able to abort any ongoing usage of the hci_dev so in this particular case perhaps it is easier to just check if HCI_UNREGISTER has been set before attempting to flush.

Quoted 4 lines

--
Pauli Virtanen

Signature · 1 line
Luiz Augusto von Dentz
#5 lore raw

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 14:56:26 -0400

Quoted 50 lines

Hi Pauli, Kuniyuki,

Quoted 38 lines · Pauli Virtanen

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 29 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-     struct hci_dev *hdev = NULL, *d;
+     struct hci_dev *hdev = NULL;
 
      BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
              return NULL;
 
      read_lock(&hci_dev_list_lock);
-     list_for_each_entry(d, &hci_dev_list, list) {
-             if (d->id == index) {
-                     hdev = hci_dev_hold(d);
+     list_for_each_entry(hdev, &hci_dev_list, list) {
+             if (hdev->id == index) {
+                     hci_dev_hold(hdev);
+                     if (srcu_index)
+                             *srcu_index = srcu_read_lock(&hdev->srcu);
                      break;
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
              }
      }
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
      return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Quoted 5 lines

Seems wrong to me as well, *d was used to hold the current entry but that has been removed so hdev would be used instead which may return a valid/non-NULL entry even when its index doesn't match. Btw, are there any documentation regarding the usage of SRCU in such cases where there are still references?

I think that's the main use case of SRCU, and basically the use case is similar to the normal RCU. There are a bunch of doc under Documentations/ and I'm not sure what's the best one, but LWN article would be a nice one to start.

https://lwn.net/Articles/202847/

Also, one good example would be rtnl_link_ops_get(), rtnl_link_ops_put(), and rtnl_link_unregister().

Quoted 6 lines

Usually the hci_unregister_dev is called by the driver to inform the hardware has been unplugged from the system, so we do want to be able to abort any ongoing usage of the hci_dev so in this particular case perhaps it is easier to just check if HCI_UNREGISTER has been set before attempting to flush.

Maybe like below ?

I think the downside of this approach would be we need to apply this change to each driver that has the same issue.

Note that we need to hold mutex here otherwise there is no guarantee that another thread does not complete kfree(). I'm not sure if touching unregister_dev in non-bluetooth-core code is something we should avoid.

What do you think ?

---8<---

drivers/bluetooth/hci_vhci.c +4 -1
index 59f4d7bdffdc..3dea1292da2d 100644
@@ -66,7 +66,10 @@ static int vhci_flush(struct hci_dev *hdev)
 {
 	struct vhci_data *data = hci_get_drvdata(hdev);
 
-	skb_queue_purge(&data->readq);
+	mutex_lock(&hdev->unregister_lock);
+	if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
+		skb_queue_purge(&data->readq);
+	mutex_unlock(&hdev->unregister_lock);
 
 	return 0;
 }

---8<---

#6 lore raw

Hi Kuniyuki,

Quoted 64 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 14:56:26 -0400

Quoted 50 lines

Hi Pauli, Kuniyuki,

Quoted 38 lines · Pauli Virtanen

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 29 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-     struct hci_dev *hdev = NULL, *d;
+     struct hci_dev *hdev = NULL;
 
      BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
              return NULL;
 
      read_lock(&hci_dev_list_lock);
-     list_for_each_entry(d, &hci_dev_list, list) {
-             if (d->id == index) {
-                     hdev = hci_dev_hold(d);
+     list_for_each_entry(hdev, &hci_dev_list, list) {
+             if (hdev->id == index) {
+                     hci_dev_hold(hdev);
+                     if (srcu_index)
+                             *srcu_index = srcu_read_lock(&hdev->srcu);
                      break;
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
              }
      }
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
      return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Quoted 5 lines

Seems wrong to me as well, *d was used to hold the current entry but that has been removed so hdev would be used instead which may return a valid/non-NULL entry even when its index doesn't match. Btw, are there any documentation regarding the usage of SRCU in such cases where there are still references?

I think that's the main use case of SRCU, and basically the use case is similar to the normal RCU. There are a bunch of doc under Documentations/ and I'm not sure what's the best one, but LWN article would be a nice one to start.

Quoted 4 lines

https://lwn.net/Articles/202847/

Also, one good example would be rtnl_link_ops_get(), rtnl_link_ops_put(), and rtnl_link_unregister().

Oh, this indeed looks pretty similar.

Quoted 27 lines
Quoted 7 lines

Usually the hci_unregister_dev is called by the driver to inform the hardware has been unplugged from the system, so we do want to be able to abort any ongoing usage of the hci_dev so in this particular case perhaps it is easier to just check if HCI_UNREGISTER has been set before attempting to flush.

Maybe like below ?

I think the downside of this approach would be we need to apply this change to each driver that has the same issue.

Note that we need to hold mutex here otherwise there is no guarantee that another thread does not complete kfree(). I'm not sure if touching unregister_dev in non-bluetooth-core code is something we should avoid.

What do you think ?

---8<---

drivers/bluetooth/hci_vhci.c +0 -0
index 59f4d7bdffdc..3dea1292da2d 100644
@@ -66,7 +66,10 @@ static int vhci_flush(struct hci_dev *hdev)
 {
        struct vhci_data *data = hci_get_drvdata(hdev);

drivers/bluetooth/hci_vhci.c static int vhci_flush(struct hci_dev *hdev)

Quoted 10 lines
drivers/bluetooth/hci_vhci.c +4 -2
 
-       skb_queue_purge(&data->readq);
+       mutex_lock(&hdev->unregister_lock);
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
+               skb_queue_purge(&data->readq);
+       mutex_unlock(&hdev->unregister_lock);
 
        return 0;
 }
---8<---

Not quite, I was thinking more along this lines:

net/bluetooth/hci_core.c +1 -1
index 07a8b4281a39..4c8105ad4885 100644
@@ -548,7 +548,7 @@ static int hci_dev_do_reset(struct hci_dev *hdev)
        hci_conn_hash_flush(hdev);
        hci_dev_unlock(hdev);
 
-       if (hdev->flush)
+       if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && hdev->flush)
                hdev->flush(hdev);
 
        hci_dev_clear_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE);

Note though, the whole hci_dev_do_reset is actually buggy, to do a proper reset we should probably do hci_power_off_sync + hci_power_on_sync so all the states are properly cleared.

Signature · 1 line
Luiz Augusto von Dentz
#7 lore raw

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 15:30:19 -0400

Quoted 119 lines

Hi Kuniyuki,

Quoted 64 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 14:56:26 -0400

Quoted 50 lines

Hi Pauli, Kuniyuki,

Quoted 38 lines · Pauli Virtanen

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 29 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-     struct hci_dev *hdev = NULL, *d;
+     struct hci_dev *hdev = NULL;
 
      BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
              return NULL;
 
      read_lock(&hci_dev_list_lock);
-     list_for_each_entry(d, &hci_dev_list, list) {
-             if (d->id == index) {
-                     hdev = hci_dev_hold(d);
+     list_for_each_entry(hdev, &hci_dev_list, list) {
+             if (hdev->id == index) {
+                     hci_dev_hold(hdev);
+                     if (srcu_index)
+                             *srcu_index = srcu_read_lock(&hdev->srcu);
                      break;
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
              }
      }
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
      return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Quoted 5 lines

Seems wrong to me as well, *d was used to hold the current entry but that has been removed so hdev would be used instead which may return a valid/non-NULL entry even when its index doesn't match. Btw, are there any documentation regarding the usage of SRCU in such cases where there are still references?

I think that's the main use case of SRCU, and basically the use case is similar to the normal RCU. There are a bunch of doc under Documentations/ and I'm not sure what's the best one, but LWN article would be a nice one to start.

Quoted 4 lines

https://lwn.net/Articles/202847/

Also, one good example would be rtnl_link_ops_get(), rtnl_link_ops_put(), and rtnl_link_unregister().

Oh, this indeed looks pretty similar.

Quoted 27 lines
Quoted 7 lines

Usually the hci_unregister_dev is called by the driver to inform the hardware has been unplugged from the system, so we do want to be able to abort any ongoing usage of the hci_dev so in this particular case perhaps it is easier to just check if HCI_UNREGISTER has been set before attempting to flush.

Maybe like below ?

I think the downside of this approach would be we need to apply this change to each driver that has the same issue.

Note that we need to hold mutex here otherwise there is no guarantee that another thread does not complete kfree(). I'm not sure if touching unregister_dev in non-bluetooth-core code is something we should avoid.

What do you think ?

---8<---

drivers/bluetooth/hci_vhci.c +0 -0
index 59f4d7bdffdc..3dea1292da2d 100644
@@ -66,7 +66,10 @@ static int vhci_flush(struct hci_dev *hdev)
 {
        struct vhci_data *data = hci_get_drvdata(hdev);

drivers/bluetooth/hci_vhci.c static int vhci_flush(struct hci_dev *hdev)

Quoted 10 lines
drivers/bluetooth/hci_vhci.c +4 -2
 
-       skb_queue_purge(&data->readq);
+       mutex_lock(&hdev->unregister_lock);
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
+               skb_queue_purge(&data->readq);
+       mutex_unlock(&hdev->unregister_lock);
 
        return 0;
 }
---8<---

Not quite, I was thinking more along this lines:

net/bluetooth/hci_core.c +0 -0
index 07a8b4281a39..4c8105ad4885 100644

net/bluetooth/hci_core.c static int hci_dev_do_reset(struct hci_dev *hdev) · line 552

Quoted 6 lines
net/bluetooth/hci_core.c +1 -1
@@ -548,7 +548,7 @@ static int hci_dev_do_reset(struct hci_dev *hdev)
        hci_conn_hash_flush(hdev);
        hci_dev_unlock(hdev);
 
-       if (hdev->flush)
+       if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && hdev->flush)
                hdev->flush(hdev);

I think technically there is still a small race window (and it could be large enough with _RT kernel).

CPU1 CPU2
hci_unregister_dev
hci_dev_get
hci_dev_test_flag(hdev, HCI_UNREGISTER)

                            hci_dev_set_flag(hdev, HCI_UNREGISTER)
                            list_del(&hdev->list)
                            kfree(vhci_data)

hdev->flush(hdev)

I'll keep the current form in v3 to fix the use-after-free with the mentioned list head bug fixed.

Quoted 6 lines
net/bluetooth/hci_core.c +0 -0
        hci_dev_clear_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE);
 

Note though, the whole hci_dev_do_reset is actually buggy, to do a proper reset we should probably do hci_power_off_sync + hci_power_on_sync so all the states are properly cleared.

#8 lore raw

Hi Kuniyuki,

Quoted 136 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 15:30:19 -0400

Quoted 119 lines

Hi Kuniyuki,

Quoted 64 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 14:56:26 -0400

Quoted 50 lines

Hi Pauli, Kuniyuki,

Quoted 38 lines · Pauli Virtanen

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 29 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-     struct hci_dev *hdev = NULL, *d;
+     struct hci_dev *hdev = NULL;
 
      BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
              return NULL;
 
      read_lock(&hci_dev_list_lock);
-     list_for_each_entry(d, &hci_dev_list, list) {
-             if (d->id == index) {
-                     hdev = hci_dev_hold(d);
+     list_for_each_entry(hdev, &hci_dev_list, list) {
+             if (hdev->id == index) {
+                     hci_dev_hold(hdev);
+                     if (srcu_index)
+                             *srcu_index = srcu_read_lock(&hdev->srcu);
                      break;
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
              }
      }
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
      return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Quoted 5 lines

Seems wrong to me as well, *d was used to hold the current entry but that has been removed so hdev would be used instead which may return a valid/non-NULL entry even when its index doesn't match. Btw, are there any documentation regarding the usage of SRCU in such cases where there are still references?

I think that's the main use case of SRCU, and basically the use case is similar to the normal RCU. There are a bunch of doc under Documentations/ and I'm not sure what's the best one, but LWN article would be a nice one to start.

Quoted 4 lines

https://lwn.net/Articles/202847/

Also, one good example would be rtnl_link_ops_get(), rtnl_link_ops_put(), and rtnl_link_unregister().

Oh, this indeed looks pretty similar.

Quoted 27 lines
Quoted 7 lines

Usually the hci_unregister_dev is called by the driver to inform the hardware has been unplugged from the system, so we do want to be able to abort any ongoing usage of the hci_dev so in this particular case perhaps it is easier to just check if HCI_UNREGISTER has been set before attempting to flush.

Maybe like below ?

I think the downside of this approach would be we need to apply this change to each driver that has the same issue.

Note that we need to hold mutex here otherwise there is no guarantee that another thread does not complete kfree(). I'm not sure if touching unregister_dev in non-bluetooth-core code is something we should avoid.

What do you think ?

---8<---

drivers/bluetooth/hci_vhci.c +0 -0
index 59f4d7bdffdc..3dea1292da2d 100644
@@ -66,7 +66,10 @@ static int vhci_flush(struct hci_dev *hdev)
 {
        struct vhci_data *data = hci_get_drvdata(hdev);

drivers/bluetooth/hci_vhci.c static int vhci_flush(struct hci_dev *hdev)

Quoted 10 lines
drivers/bluetooth/hci_vhci.c +4 -2
 
-       skb_queue_purge(&data->readq);
+       mutex_lock(&hdev->unregister_lock);
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
+               skb_queue_purge(&data->readq);
+       mutex_unlock(&hdev->unregister_lock);
 
        return 0;
 }
---8<---

Not quite, I was thinking more along this lines:

net/bluetooth/hci_core.c +0 -0
index 07a8b4281a39..4c8105ad4885 100644

net/bluetooth/hci_core.c static int hci_dev_do_reset(struct hci_dev *hdev) · line 552

Quoted 6 lines
net/bluetooth/hci_core.c +1 -1
@@ -548,7 +548,7 @@ static int hci_dev_do_reset(struct hci_dev *hdev)
        hci_conn_hash_flush(hdev);
        hci_dev_unlock(hdev);
 
-       if (hdev->flush)
+       if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && hdev->flush)
                hdev->flush(hdev);

I think technically there is still a small race window (and it could be large enough with _RT kernel).

CPU1 CPU2
hci_unregister_dev
hci_dev_get
hci_dev_test_flag(hdev, HCI_UNREGISTER)

Quoted 8 lines
                            hci_dev_set_flag(hdev, HCI_UNREGISTER)
                            list_del(&hdev->list)
                            kfree(vhci_data)

hdev->flush(hdev)

I'll keep the current form in v3 to fix the use-after-free with the mentioned list head bug fixed.

Ok, and how the v3 helps with the situation above? It seems the assumption is that synchronize_srcu would wait for the thread holding hci_dev_get_srcu, but what if the order is the reversed? It does seem like we still need to prevent hci_dev_get_srcu to return the hdev it is has been unregistered already.

Quoted 8 lines
Quoted 8 lines
net/bluetooth/hci_core.c +0 -0
        hci_dev_clear_flag(hdev, HCI_CMD_DRAIN_WORKQUEUE);
 

Note though, the whole hci_dev_do_reset is actually buggy, to do a proper reset we should probably do hci_power_off_sync + hci_power_on_sync so all the states are properly cleared.

Signature · 1 line
Luiz Augusto von Dentz
#9 lore raw

From: Luiz Augusto von Dentz <[email protected]>
Date: Tue, 17 Jun 2025 10:52:11 -0400

Quoted 145 lines

Hi Kuniyuki,

Quoted 142 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 15:30:19 -0400

Quoted 119 lines

Hi Kuniyuki,

Quoted 64 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 14:56:26 -0400

Quoted 50 lines

Hi Pauli, Kuniyuki,

Quoted 38 lines · Pauli Virtanen

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 29 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-     struct hci_dev *hdev = NULL, *d;
+     struct hci_dev *hdev = NULL;
 
      BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
              return NULL;
 
      read_lock(&hci_dev_list_lock);
-     list_for_each_entry(d, &hci_dev_list, list) {
-             if (d->id == index) {
-                     hdev = hci_dev_hold(d);
+     list_for_each_entry(hdev, &hci_dev_list, list) {
+             if (hdev->id == index) {
+                     hci_dev_hold(hdev);
+                     if (srcu_index)
+                             *srcu_index = srcu_read_lock(&hdev->srcu);
                      break;
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
              }
      }
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
      return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Quoted 5 lines

Seems wrong to me as well, *d was used to hold the current entry but that has been removed so hdev would be used instead which may return a valid/non-NULL entry even when its index doesn't match. Btw, are there any documentation regarding the usage of SRCU in such cases where there are still references?

I think that's the main use case of SRCU, and basically the use case is similar to the normal RCU. There are a bunch of doc under Documentations/ and I'm not sure what's the best one, but LWN article would be a nice one to start.

Quoted 4 lines

https://lwn.net/Articles/202847/

Also, one good example would be rtnl_link_ops_get(), rtnl_link_ops_put(), and rtnl_link_unregister().

Oh, this indeed looks pretty similar.

Quoted 27 lines
Quoted 7 lines

Usually the hci_unregister_dev is called by the driver to inform the hardware has been unplugged from the system, so we do want to be able to abort any ongoing usage of the hci_dev so in this particular case perhaps it is easier to just check if HCI_UNREGISTER has been set before attempting to flush.

Maybe like below ?

I think the downside of this approach would be we need to apply this change to each driver that has the same issue.

Note that we need to hold mutex here otherwise there is no guarantee that another thread does not complete kfree(). I'm not sure if touching unregister_dev in non-bluetooth-core code is something we should avoid.

What do you think ?

---8<---

drivers/bluetooth/hci_vhci.c +0 -0
index 59f4d7bdffdc..3dea1292da2d 100644
@@ -66,7 +66,10 @@ static int vhci_flush(struct hci_dev *hdev)
 {
        struct vhci_data *data = hci_get_drvdata(hdev);

drivers/bluetooth/hci_vhci.c static int vhci_flush(struct hci_dev *hdev)

Quoted 10 lines
drivers/bluetooth/hci_vhci.c +4 -2
 
-       skb_queue_purge(&data->readq);
+       mutex_lock(&hdev->unregister_lock);
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
+               skb_queue_purge(&data->readq);
+       mutex_unlock(&hdev->unregister_lock);
 
        return 0;
 }
---8<---

Not quite, I was thinking more along this lines:

net/bluetooth/hci_core.c +0 -0
index 07a8b4281a39..4c8105ad4885 100644

net/bluetooth/hci_core.c static int hci_dev_do_reset(struct hci_dev *hdev) · line 552

Quoted 6 lines
net/bluetooth/hci_core.c +1 -1
@@ -548,7 +548,7 @@ static int hci_dev_do_reset(struct hci_dev *hdev)
        hci_conn_hash_flush(hdev);
        hci_dev_unlock(hdev);
 
-       if (hdev->flush)
+       if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && hdev->flush)
                hdev->flush(hdev);

I think technically there is still a small race window (and it could be large enough with _RT kernel).

CPU1 CPU2
hci_unregister_dev
hci_dev_get
hci_dev_test_flag(hdev, HCI_UNREGISTER)

                            hci_dev_set_flag(hdev, HCI_UNREGISTER)
                            list_del(&hdev->list)
                            kfree(vhci_data)

hdev->flush(hdev)

Quoted 8 lines
Quoted 2 lines

I'll keep the current form in v3 to fix the use-after-free with the mentioned list head bug fixed.

Ok, and how the v3 helps with the situation above? It seems the assumption is that synchronize_srcu would wait for the thread holding hci_dev_get_srcu, but what if the order is the reversed? It does seem like we still need to prevent hci_dev_get_srcu to return the hdev it is has been unregistered already.

It is never reversed as hci_dev_get_srcu() and hci_unregister_dev() hold hci_dev_list_lock.

So, if hci_dev_get_srcu() finds a hdev, it's always after that that the hdev is un-listed by hci_unregister_dev(), and synchronize_srcu() follows it.

#10 lore raw

Hi Kuniyuki,

Quoted 157 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Tue, 17 Jun 2025 10:52:11 -0400

Quoted 145 lines

Hi Kuniyuki,

Quoted 142 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 15:30:19 -0400

Quoted 119 lines

Hi Kuniyuki,

Quoted 64 lines · Kuniyuki Iwashima

From: Luiz Augusto von Dentz <[email protected]>
Date: Mon, 16 Jun 2025 14:56:26 -0400

Quoted 50 lines

Hi Pauli, Kuniyuki,

Quoted 38 lines · Pauli Virtanen

Hi,

ma, 2025-06-16 kello 10:37 -0700, Kuniyuki Iwashima kirjoitti:

Quoted 4 lines

From: Kuniyuki Iwashima <[email protected]>

syzbot reported use-after-free in vhci_flush() without repro. [0]

[clip]

Quoted 29 lines
net/bluetooth/hci_core.c +7 -5
index 07a8b4281a39..d648b514e2df 100644
@@ -64,9 +64,9 @@ static DEFINE_IDA(hci_index_ida);
 
 /* Get HCI device by index.
  * Device is held on return. */
-struct hci_dev *hci_dev_get(int index)
+static struct hci_dev *__hci_dev_get(int index, int *srcu_index)
 {
-     struct hci_dev *hdev = NULL, *d;
+     struct hci_dev *hdev = NULL;
 
      BT_DBG("%d", index);
 
@@ -74,9 +74,11 @@ struct hci_dev *hci_dev_get(int index)
              return NULL;
 
      read_lock(&hci_dev_list_lock);
-     list_for_each_entry(d, &hci_dev_list, list) {
-             if (d->id == index) {
-                     hdev = hci_dev_hold(d);
+     list_for_each_entry(hdev, &hci_dev_list, list) {
+             if (hdev->id == index) {
+                     hci_dev_hold(hdev);
+                     if (srcu_index)
+                             *srcu_index = srcu_read_lock(&hdev->srcu);
                      break;
Quoted 8 lines

net/bluetooth/hci_core.c struct hci_dev *hci_dev_get(int index) · line 87

Quoted 5 lines
net/bluetooth/hci_core.c +0 -0
              }
      }
@@ -84,6 +86,22 @@ struct hci_dev *hci_dev_get(int index)
      return hdev;
 }

If no list item has hdev->id == index, doesn't this now return the list head -> crash?

Quoted 5 lines

Seems wrong to me as well, *d was used to hold the current entry but that has been removed so hdev would be used instead which may return a valid/non-NULL entry even when its index doesn't match. Btw, are there any documentation regarding the usage of SRCU in such cases where there are still references?

I think that's the main use case of SRCU, and basically the use case is similar to the normal RCU. There are a bunch of doc under Documentations/ and I'm not sure what's the best one, but LWN article would be a nice one to start.

Quoted 4 lines

https://lwn.net/Articles/202847/

Also, one good example would be rtnl_link_ops_get(), rtnl_link_ops_put(), and rtnl_link_unregister().

Oh, this indeed looks pretty similar.

Quoted 27 lines
Quoted 7 lines

Usually the hci_unregister_dev is called by the driver to inform the hardware has been unplugged from the system, so we do want to be able to abort any ongoing usage of the hci_dev so in this particular case perhaps it is easier to just check if HCI_UNREGISTER has been set before attempting to flush.

Maybe like below ?

I think the downside of this approach would be we need to apply this change to each driver that has the same issue.

Note that we need to hold mutex here otherwise there is no guarantee that another thread does not complete kfree(). I'm not sure if touching unregister_dev in non-bluetooth-core code is something we should avoid.

What do you think ?

---8<---

drivers/bluetooth/hci_vhci.c +0 -0
index 59f4d7bdffdc..3dea1292da2d 100644
@@ -66,7 +66,10 @@ static int vhci_flush(struct hci_dev *hdev)
 {
        struct vhci_data *data = hci_get_drvdata(hdev);

drivers/bluetooth/hci_vhci.c static int vhci_flush(struct hci_dev *hdev)

Quoted 10 lines
drivers/bluetooth/hci_vhci.c +4 -2
 
-       skb_queue_purge(&data->readq);
+       mutex_lock(&hdev->unregister_lock);
+       if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
+               skb_queue_purge(&data->readq);
+       mutex_unlock(&hdev->unregister_lock);
 
        return 0;
 }
---8<---

Not quite, I was thinking more along this lines:

net/bluetooth/hci_core.c +0 -0
index 07a8b4281a39..4c8105ad4885 100644

net/bluetooth/hci_core.c static int hci_dev_do_reset(struct hci_dev *hdev) · line 552

Quoted 6 lines
net/bluetooth/hci_core.c +1 -1
@@ -548,7 +548,7 @@ static int hci_dev_do_reset(struct hci_dev *hdev)
        hci_conn_hash_flush(hdev);
        hci_dev_unlock(hdev);
 
-       if (hdev->flush)
+       if (!hci_dev_test_flag(hdev, HCI_UNREGISTER) && hdev->flush)
                hdev->flush(hdev);

I think technically there is still a small race window (and it could be large enough with _RT kernel).

CPU1 CPU2
hci_unregister_dev
hci_dev_get
hci_dev_test_flag(hdev, HCI_UNREGISTER)

                            hci_dev_set_flag(hdev, HCI_UNREGISTER)
                            list_del(&hdev->list)
                            kfree(vhci_data)

hdev->flush(hdev)

Quoted 8 lines
Quoted 2 lines

I'll keep the current form in v3 to fix the use-after-free with the mentioned list head bug fixed.

Ok, and how the v3 helps with the situation above? It seems the assumption is that synchronize_srcu would wait for the thread holding hci_dev_get_srcu, but what if the order is the reversed? It does seem like we still need to prevent hci_dev_get_srcu to return the hdev it is has been unregistered already.

Quoted 6 lines

It is never reversed as hci_dev_get_srcu() and hci_unregister_dev() hold hci_dev_list_lock.

So, if hci_dev_get_srcu() finds a hdev, it's always after that that the hdev is un-listed by hci_unregister_dev(), and synchronize_srcu() follows it.

Right, I forgot hci_unregister_dev does actually remove the hdev from the list hci_dev_list, alright then we just need to sort out the removal of the temporary variable pointed by Pauli, so please spin a v4 once you fix that.

Signature · 1 line
Luiz Augusto von Dentz