Skip to content

Commit 7cc08f2

Browse files
ivpravdingregkh
authored andcommitted
Bluetooth: vhci: Prevent use-after-free by removing debugfs files early
[ Upstream commit 2801079 ] Move the creation of debugfs files into a dedicated function, and ensure they are explicitly removed during vhci_release(), before associated data structures are freed. Previously, debugfs files such as "force_suspend", "force_wakeup", and others were created under hdev->debugfs but not removed in vhci_release(). Since vhci_release() frees the backing vhci_data structure, any access to these files after release would result in use-after-free errors. Although hdev->debugfs is later freed in hci_release_dev(), user can access files after vhci_data is freed but before hdev->debugfs is released. Fixes: ab4e438 ("Bluetooth: Add vhci devcoredump support") Signed-off-by: Ivan Pravdin <ipravdin.official@gmail.com> Reviewed-by: Paul Menzel <pmenzel@molgen.mpg.de> Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent b9b9e95 commit 7cc08f2

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

drivers/bluetooth/hci_vhci.c

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,28 @@ static const struct file_operations force_devcoredump_fops = {
380380
.write = force_devcd_write,
381381
};
382382

383+
static void vhci_debugfs_init(struct vhci_data *data)
384+
{
385+
struct hci_dev *hdev = data->hdev;
386+
387+
debugfs_create_file("force_suspend", 0644, hdev->debugfs, data,
388+
&force_suspend_fops);
389+
390+
debugfs_create_file("force_wakeup", 0644, hdev->debugfs, data,
391+
&force_wakeup_fops);
392+
393+
if (IS_ENABLED(CONFIG_BT_MSFTEXT))
394+
debugfs_create_file("msft_opcode", 0644, hdev->debugfs, data,
395+
&msft_opcode_fops);
396+
397+
if (IS_ENABLED(CONFIG_BT_AOSPEXT))
398+
debugfs_create_file("aosp_capable", 0644, hdev->debugfs, data,
399+
&aosp_capable_fops);
400+
401+
debugfs_create_file("force_devcoredump", 0644, hdev->debugfs, data,
402+
&force_devcoredump_fops);
403+
}
404+
383405
static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
384406
{
385407
struct hci_dev *hdev;
@@ -434,22 +456,8 @@ static int __vhci_create_device(struct vhci_data *data, __u8 opcode)
434456
return -EBUSY;
435457
}
436458

437-
debugfs_create_file("force_suspend", 0644, hdev->debugfs, data,
438-
&force_suspend_fops);
439-
440-
debugfs_create_file("force_wakeup", 0644, hdev->debugfs, data,
441-
&force_wakeup_fops);
442-
443-
if (IS_ENABLED(CONFIG_BT_MSFTEXT))
444-
debugfs_create_file("msft_opcode", 0644, hdev->debugfs, data,
445-
&msft_opcode_fops);
446-
447-
if (IS_ENABLED(CONFIG_BT_AOSPEXT))
448-
debugfs_create_file("aosp_capable", 0644, hdev->debugfs, data,
449-
&aosp_capable_fops);
450-
451-
debugfs_create_file("force_devcoredump", 0644, hdev->debugfs, data,
452-
&force_devcoredump_fops);
459+
if (!IS_ERR_OR_NULL(hdev->debugfs))
460+
vhci_debugfs_init(data);
453461

454462
hci_skb_pkt_type(skb) = HCI_VENDOR_PKT;
455463

@@ -651,6 +659,21 @@ static int vhci_open(struct inode *inode, struct file *file)
651659
return 0;
652660
}
653661

662+
static void vhci_debugfs_remove(struct hci_dev *hdev)
663+
{
664+
debugfs_lookup_and_remove("force_suspend", hdev->debugfs);
665+
666+
debugfs_lookup_and_remove("force_wakeup", hdev->debugfs);
667+
668+
if (IS_ENABLED(CONFIG_BT_MSFTEXT))
669+
debugfs_lookup_and_remove("msft_opcode", hdev->debugfs);
670+
671+
if (IS_ENABLED(CONFIG_BT_AOSPEXT))
672+
debugfs_lookup_and_remove("aosp_capable", hdev->debugfs);
673+
674+
debugfs_lookup_and_remove("force_devcoredump", hdev->debugfs);
675+
}
676+
654677
static int vhci_release(struct inode *inode, struct file *file)
655678
{
656679
struct vhci_data *data = file->private_data;
@@ -662,6 +685,8 @@ static int vhci_release(struct inode *inode, struct file *file)
662685
hdev = data->hdev;
663686

664687
if (hdev) {
688+
if (!IS_ERR_OR_NULL(hdev->debugfs))
689+
vhci_debugfs_remove(hdev);
665690
hci_unregister_dev(hdev);
666691
hci_free_dev(hdev);
667692
}

0 commit comments

Comments
 (0)