CVE-2023-52478
Publication date:
29/02/2024
In the Linux kernel, the following vulnerability has been resolved:<br />
<br />
HID: logitech-hidpp: Fix kernel crash on receiver USB disconnect<br />
<br />
hidpp_connect_event() has *four* time-of-check vs time-of-use (TOCTOU)<br />
races when it races with itself.<br />
<br />
hidpp_connect_event() primarily runs from a workqueue but it also runs<br />
on probe() and if a "device-connected" packet is received by the hw<br />
when the thread running hidpp_connect_event() from probe() is waiting on<br />
the hw, then a second thread running hidpp_connect_event() will be<br />
started from the workqueue.<br />
<br />
This opens the following races (note the below code is simplified):<br />
<br />
1. Retrieving + printing the protocol (harmless race):<br />
<br />
if (!hidpp->protocol_major) {<br />
hidpp_root_get_protocol_version()<br />
hidpp->protocol_major = response.rap.params[0];<br />
}<br />
<br />
We can actually see this race hit in the dmesg in the abrt output<br />
attached to rhbz#2227968:<br />
<br />
[ 3064.624215] logitech-hidpp-device 0003:046D:4071.0049: HID++ 4.5 device connected.<br />
[ 3064.658184] logitech-hidpp-device 0003:046D:4071.0049: HID++ 4.5 device connected.<br />
<br />
Testing with extra logging added has shown that after this the 2 threads<br />
take turn grabbing the hw access mutex (send_mutex) so they ping-pong<br />
through all the other TOCTOU cases managing to hit all of them:<br />
<br />
2. Updating the name to the HIDPP name (harmless race):<br />
<br />
if (hidpp->name == hdev->name) {<br />
...<br />
hidpp->name = new_name;<br />
}<br />
<br />
3. Initializing the power_supply class for the battery (problematic!):<br />
<br />
hidpp_initialize_battery()<br />
{<br />
if (hidpp->battery.ps)<br />
return 0;<br />
<br />
probe_battery(); /* Blocks, threads take turns executing this */<br />
<br />
hidpp->battery.desc.properties =<br />
devm_kmemdup(dev, hidpp_battery_props, cnt, GFP_KERNEL);<br />
<br />
hidpp->battery.ps =<br />
devm_power_supply_register(&hidpp->hid_dev->dev,<br />
&hidpp->battery.desc, cfg);<br />
}<br />
<br />
4. Creating delayed input_device (potentially problematic):<br />
<br />
if (hidpp->delayed_input)<br />
return;<br />
<br />
hidpp->delayed_input = hidpp_allocate_input(hdev);<br />
<br />
The really big problem here is 3. Hitting the race leads to the following<br />
sequence:<br />
<br />
hidpp->battery.desc.properties =<br />
devm_kmemdup(dev, hidpp_battery_props, cnt, GFP_KERNEL);<br />
<br />
hidpp->battery.ps =<br />
devm_power_supply_register(&hidpp->hid_dev->dev,<br />
&hidpp->battery.desc, cfg);<br />
<br />
...<br />
<br />
hidpp->battery.desc.properties =<br />
devm_kmemdup(dev, hidpp_battery_props, cnt, GFP_KERNEL);<br />
<br />
hidpp->battery.ps =<br />
devm_power_supply_register(&hidpp->hid_dev->dev,<br />
&hidpp->battery.desc, cfg);<br />
<br />
So now we have registered 2 power supplies for the same battery,<br />
which looks a bit weird from userspace&#39;s pov but this is not even<br />
the really big problem.<br />
<br />
Notice how:<br />
<br />
1. This is all devm-maganaged<br />
2. The hidpp->battery.desc struct is shared between the 2 power supplies<br />
3. hidpp->battery.desc.properties points to the result from the second<br />
devm_kmemdup()<br />
<br />
This causes a use after free scenario on USB disconnect of the receiver:<br />
1. The last registered power supply class device gets unregistered<br />
2. The memory from the last devm_kmemdup() call gets freed,<br />
hidpp->battery.desc.properties now points to freed memory<br />
3. The first registered power supply class device gets unregistered,<br />
this involves sending a remove uevent to userspace which invokes<br />
power_supply_uevent() to fill the uevent data<br />
4. power_supply_uevent() uses hidpp->battery.desc.properties which<br />
now points to freed memory leading to backtraces like this one:<br />
<br />
Sep 22 20:01:35 eric kernel: BUG: unable to handle page fault for address: ffffb2140e017f08<br />
...<br />
Sep 22 20:01:35 eric kernel: Workqueue: usb_hub_wq hub_event<br />
Sep 22 20:01:35 eric kernel: RIP: 0010:power_supply_uevent+0xee/0x1d0<br />
...<br />
Sep 22 20:01:35 eric kernel: ? asm_exc_page_fault+0x26/0x30<br />
Sep 22 20:01:35 eric kernel: ? power_supply_uevent+0xee/0x1d0<br />
Sep 22 20:01:35 eric kernel: ? power_supply_uevent+0x10d/0x1d0<br />
Sep 22 20:01:35 eric kernel: dev_uevent+0x10f/0x2d0<br />
Sep 22 20:01:35 eric kernel: kobject_uevent_env+0x291/0x680<br />
Sep 22 20:01:35 eric kernel: <br />
---truncated---
Severity CVSS v4.0: Pending analysis
Last modification:
10/01/2025