HID: magicmouse: fix battery reporting for Bluetooth Magic Trackpad USB-C
Subsystems (2)
PATCH 1/1 HID: magicmouse: fix battery reporting for Bluetooth Magic Trackpad USB-C
The Apple Magic Trackpad 2 (USB-C) reports a wildly wrong battery capacity over Bluetooth, for example a constant 4% for a pack that is actually at 74%.
The device's battery input report (0x90) is laid out as [report-id][status][charge]. hid-input's synchronous capacity query, hidinput_query_battery_capacity(), assumes the common [report-id][capacity] layout and returns buf[1], which for this device is the status byte rather than the charge (buf[2]).
magicmouse_fetch_battery(), which requests the battery report through hid_hw_request() so the reply is decoded via the report descriptor at the correct field offset, is gated to the USB models and never runs over Bluetooth. The device does not push battery reports on its own either, except a single one at connect time, which is delivered while probe holds driver_input_lock and is silently dropped. All userspace reads therefore go through the misparsing query, and the device is stuck reporting its status byte as the capacity.
Enabling the fetch for Bluetooth is not sufficient on its own: user space reacts to the power_supply registration immediately, so a query is typically already in flight when the fetch reply is parsed. hidinput_get_battery_property() stores the query result and marks the battery as queried without rechecking whether a report arrived while it was waiting, clobbering the just-reported correct value with the misparsed one.
Fix this by adding HID_BATTERY_QUIRK_AVOID_QUERY for the Bluetooth Magic Trackpad USB-C so the misparsing query path is never used, and by fetching the battery at the end of probe for this device. hidp has no asynchronous request() callback, so the fetch is serviced synchronously via __hid_request() while probe still holds driver_input_lock; call hid_device_io_start() first so the reply is processed instead of being discarded.
Tested with a Magic Trackpad USB-C (004c:0324) over Bluetooth on 6.18.37: the reported capacity now matches the device (verified against a raw GET_REPORT of report 0x90) and updates on reconnect.
--- drivers/hid/hid-input.c | 3 +++ drivers/hid/hid-magicmouse.c | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-)
drivers/hid/hid-input.c +3 -0
@@ -375,6 +375,9 @@ static const struct hid_device_id hid_battery_quirks[] = { {HID_BLUETOOTH_DEVICE (USB_VENDOR_ID_APPLE ,USB_DEVICE_ID_APPLE_MAGICTRACKPAD ), HID_BATTERY_QUIRK_IGNORE }, + { HID_BLUETOOTH_DEVICE(BT_VENDOR_ID_APPLE, +USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC ), + HID_BATTERY_QUIRK_AVOID_QUERY }, { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_ELECOM,USB_DEVICE_ID_ELECOM_BM084 ), HID_BATTERY_QUIRK_IGNORE },
drivers/hid/hid-magicmouse.c +18 -1
@@ -828,6 +828,12 @@ static bool is_usb_magictrackpad2(__u32 vendor, __u32 product) product ==USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC ; } +static bool is_bt_magictrackpad2 (__u32 vendor,__u32 product) +{ +return vendor ==BT_VENDOR_ID_APPLE && + product ==USB_DEVICE_ID_APPLE_MAGICTRACKPAD2_USBC ; +} +static int magicmouse_fetch_battery (struct hid_device *hdev) {#ifdef CONFIG_HID_BATTERY_STRENGTH
@@ -838,7 +844,8 @@ static int magicmouse_fetch_battery(struct hid_device *hdev) bat =hid_get_battery (hdev);if (!bat || (!is_usb_magicmouse2 (hdev->vendor, hdev->product) && - !is_usb_magictrackpad2 (hdev->vendor, hdev->product))) + !is_usb_magictrackpad2 (hdev->vendor, hdev->product) && + !is_bt_magictrackpad2 (hdev->vendor, hdev->product)))return -1 ; report_enum = &hdev->report_enum[bat->report_type];
@@ -971,6 +978,16 @@ static int magicmouse_probe(struct hid_device *hdev,schedule_delayed_work (&msc->work,msecs_to_jiffies (500 )); } +/* +* Query the Bluetooth Magic Trackpad USB-C battery as done for USB. +* Start io first: probe holds driver_input_lock and the synchronous +* GET_REPORT reply would otherwise be dropped. +*/ +if (is_bt_magictrackpad2 (id->vendor, id->product)) { +hid_device_io_start (hdev); +magicmouse_fetch_battery (hdev); + } +return 0 ; err_stop_hw: if (is_usb_magicmouse2(id->vendor, id->product) ||
Signature · 1 line
2.54.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Critical] Unbounded recursion in magicmouse_raw_event when parsing malicious DOUBLE_REPORT_ID packets can cause a kernel stack overflow. --
commit 58987e236bab550dc05b66346833ee21ed261110
Author: Andrei Fed <[email protected]>
HID: magicmouse: fix battery reporting for Bluetooth Magic Trackpad USB-C
This commit fixes battery reporting for the Bluetooth Magic Trackpad USB-C by avoiding the misparsing query path. It introduces a battery quirk to skip the query and instead fetches the battery at the end of the device probe.
drivers/hid/hid-magicmouse.c
Quoted 4 lines
drivers/hid/hid-magicmouse.c +0
-0
[ ... ]
[Severity: Critical]
This is a pre-existing issue, but while reviewing this file I noticed that magicmouse_raw_event() doesn't seem to enforce a depth limit when recursively calling itself to parse DOUBLE_REPORT_ID packets:
drivers/hid/hid-magicmouse.c:magicmouse_raw_event() {
...
case DOUBLE_REPORT_ID:
/* Sometimes the trackpad sends two touch reports in one
* packet.
*/
/* Ensure that we have at least 2 elements (report type and size) */
if (size < 2)
return 0;
if (size < data[1] + 2) {
hid_warn(hdev,
"received report length (%d) was smaller than specified (%d)",
size, data[1] + 2);
return 0;
}
magicmouse_raw_event(hdev, report, data + 2, data[1]);
magicmouse_raw_event(hdev, report, data + 2 + data[1],
size - 2 - data[1]);
return 0;
...
}If a malicious Bluetooth or USB device sends a HID report starting with DOUBLE_REPORT_ID (0xf7) and filled with the sequence [0xf7, 0x00], the inner size byte data[1] evaluates to 0.
Could this cause magicmouse_raw_event() to recursively call itself with size 0 (which returns immediately) and size - 2?
A 16KB malicious report could force ~8192 nested recursions, which might overflow the standard kernel stack and lead to a denial of service panic.
Does this need an upper bound on recursion depth or an iterative approach to prevent stack exhaustion?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Hi Andrei,
Thanks for tackling this -- I hit the same bug on the Magic Trackpad 2 (USB-C, 004c:0324) over Bluetooth and posted a fix a few days earlier:
[PATCH] HID: input: read battery capacity from its actual report offset
https://lore.kernel.org/linux-input/[email protected]/
The two approaches fix the problem in different places:
- Mine fixes the root cause in the generic query path. hidinput_query_battery_capacity() hard-codes buf[1] and ignores the battery field's offset within the report. I store the field offset at setup and read from it, so any device whose state-of-charge byte is not the first field -- like this trackpad, which prefixes it with a status byte -- is parsed correctly, with no per-device quirk.
- Yours keeps the generic path as-is and routes this specific device around it via HID_BATTERY_QUIRK_AVOID_QUERY plus a probe-time fetch.
If the generic query is fixed, the AVOID_QUERY quirk shouldn't be needed for this device. I'm happy to go with whichever the maintainers prefer; if the generic fix is acceptable I'll respin it with a Fixes: tag and a stable Cc, as you did. Your note about the connect-time report being dropped under driver_input_lock is a good catch either way.
Thanks,
Jose
Quoted 3 lines · Andrei Fed
The Apple Magic Trackpad 2 (USB-C) reports a wildly wrong battery capacity over Bluetooth, for example a constant 4% for a pack that is actually at 74%.
[ ... snip ... ]
Applied, thanks.
Signature · 2 lines
Jiri Kosina SUSE Labs