Bluetooth: bfusb: Fix use-after-free and memory leak in device lifecycle
PATCH 1/1 Bluetooth: bfusb: Fix use-after-free and memory leak in device lifecycle
The driver stores a reference to the usb_device structure (udev) in its private data (data->udev), which can persist beyond the immediate context of the bfusb_probe() function.
Without proper reference count management, this can lead to two issues:
1. A use-after-free scenario if udev is accessed after its main reference count drops to zero (e.g., if the device is disconnected and the data structure is still active).
2. A memory leak if udev's reference count is not properly decremented during driver disconnect, preventing the usb_device object from being freed.
To correctly manage the udev lifetime, explicitly increment its reference count with usb_get_dev(udev) when storing it in the driver's private data. Correspondingly, decrement the reference count with usb_put_dev(data->udev) in the bfusb_disconnect() callback.
This ensures udev remains valid while referenced by the driver's private data and is properly released when no longer needed.
--- drivers/bluetooth/bfusb.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/bluetooth/bfusb.c +3 -1
@@ -622,7 +622,7 @@ static int bfusb_probe(struct usb_interface *intf, const struct usb_device_id *iif (!data)return -ENOMEM ; - data->udev = udev; + data->udev =usb_get_dev (udev); data->bulk_in_ep = bulk_in_ep->desc.bEndpointAddress; data->bulk_out_ep = bulk_out_ep->desc.bEndpointAddress; data->bulk_pkt_size =le16_to_cpu (bulk_out_ep->desc.wMaxPacketSize);
@@ -701,6 +701,8 @@ static void bfusb_disconnect(struct usb_interface *intf)usb_set_intfdata (intf,NULL ); +usb_put_dev (data->udev); +bfusb_close (hdev);hci_unregister_dev (hdev);
Signature · 1 line
2.43.0
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=986088
---Test result---
Test Summary:
CheckPatch PENDING 0.31 seconds GitLint PENDING 0.25 seconds SubjectPrefix PASS 0.10 seconds BuildKernel PASS 24.04 seconds CheckAllWarning PASS 26.67 seconds CheckSparse PASS 30.04 seconds BuildKernel32 PASS 24.31 seconds TestRunnerSetup PASS 475.79 seconds TestRunner_l2cap-tester PASS 24.80 seconds TestRunner_iso-tester PASS 35.94 seconds TestRunner_bnep-tester PASS 5.95 seconds TestRunner_mgmt-tester PASS 127.54 seconds TestRunner_rfcomm-tester PASS 9.30 seconds TestRunner_sco-tester PASS 14.57 seconds TestRunner_ioctl-tester PASS 10.00 seconds TestRunner_mesh-tester FAIL 11.34 seconds TestRunner_smp-tester PASS 8.61 seconds TestRunner_userchan-tester PASS 6.13 seconds IncrementalBuild PENDING 0.77 seconds
Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script Output:
##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:
##############################
Test: TestRunner_mesh-tester - FAIL
Desc: Run mesh-tester with test-runner Output:
Total: 10, Passed: 8 (80.0%), Failed: 2, Not Run: 0
Failed Test Cases
Mesh - Send cancel - 1 Timed out 2.076 seconds Mesh - Send cancel - 2 Timed out 1.996 seconds
##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:
---
Regards,
Linux Bluetooth
…
Quoted 2 lines
This ensures
udevremains valid while referenced by the driver's private data and is properly released when no longer needed.
How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.16-rc7#n145
Regards,
Markus