Skip to content

Commit bec338b

Browse files
montfortgregkh
authored andcommitted
HID: magicmouse: prevent unbounded recursion in magicmouse_raw_event()
commit db8d634 upstream. magicmouse_raw_event() handles DOUBLE_REPORT_ID (0xf7) packets, which pack two touch reports into one, by splitting the packet and calling itself on each half. The only guard against runaway recursion is a "size < 1" check, which stops zero-sized calls but does not bound the recursion depth. A malicious HID device that matches this driver can send a report starting with DOUBLE_REPORT_ID and filled with the sequence [0xf7, 0x00]. Each level consumes two bytes and recurses on the remainder, so an incoming report of up to HID_MAX_BUFFER_SIZE (16 KiB) drives roughly 8000 nested calls. That easily exhausts the 16 KiB kernel stack, leading to a stack overflow: a panic with CONFIG_VMAP_STACK, or memory corruption without it. A double report only ever wraps two normal reports; it is never legitimately nested. Refuse to re-enter the DOUBLE_REPORT_ID case from a recursive call so the recursion depth is bounded to two, while all valid packets keep being parsed exactly as before. Fixes: a462230 ("HID: magicmouse: enable Magic Trackpad support") Link: https://lore.kernel.org/linux-input/20260706181347.700DB1F00A3F@smtp.kernel.org/ Cc: stable@vger.kernel.org Signed-off-by: Jose Villaseñor Montfort <pepemontfort@gmail.com> Reviewed-by: Alec Hall <signshop.alec@gmail.com> Tested-by: Alec Hall <signshop.alec@gmail.com> Signed-off-by: Jiri Kosina <jkosina@suse.com> Signed-off-by: Sasha Levin <sashal@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 87ad116 commit bec338b

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

drivers/hid/hid-magicmouse.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,8 @@ static void magicmouse_emit_touch(struct magicmouse_sc *msc, int raw_id, u8 *tda
380380
}
381381
}
382382

383-
static int magicmouse_raw_event(struct hid_device *hdev,
384-
struct hid_report *report, u8 *data, int size)
383+
static int __magicmouse_raw_event(struct hid_device *hdev,
384+
struct hid_report *report, u8 *data, int size, bool nested)
385385
{
386386
struct magicmouse_sc *msc = hid_get_drvdata(hdev);
387387
struct input_dev *input = msc->input;
@@ -492,6 +492,15 @@ static int magicmouse_raw_event(struct hid_device *hdev,
492492
* packet.
493493
*/
494494

495+
/*
496+
* A double report only ever wraps two normal reports, so it is
497+
* never nested. Refuse to recurse a second time; otherwise a
498+
* malicious device could chain DOUBLE_REPORT_ID packets to drive
499+
* unbounded recursion and overflow the kernel stack.
500+
*/
501+
if (nested)
502+
return 0;
503+
495504
/* Ensure that we have at least 2 elements (report type and size) */
496505
if (size < 2)
497506
return 0;
@@ -503,9 +512,9 @@ static int magicmouse_raw_event(struct hid_device *hdev,
503512
return 0;
504513
}
505514

506-
magicmouse_raw_event(hdev, report, data + 2, data[1]);
507-
magicmouse_raw_event(hdev, report, data + 2 + data[1],
508-
size - 2 - data[1]);
515+
__magicmouse_raw_event(hdev, report, data + 2, data[1], true);
516+
__magicmouse_raw_event(hdev, report, data + 2 + data[1],
517+
size - 2 - data[1], true);
509518
return 0;
510519
default:
511520
return 0;
@@ -530,6 +539,12 @@ static int magicmouse_raw_event(struct hid_device *hdev,
530539
return 1;
531540
}
532541

542+
static int magicmouse_raw_event(struct hid_device *hdev,
543+
struct hid_report *report, u8 *data, int size)
544+
{
545+
return __magicmouse_raw_event(hdev, report, data, size, false);
546+
}
547+
533548
static int magicmouse_event(struct hid_device *hdev, struct hid_field *field,
534549
struct hid_usage *usage, __s32 value)
535550
{

0 commit comments

Comments
 (0)