HID: core: serialize report_descriptor_read() against probe and remove
Subsystems (2)
PATCH 1/1 HID: core: serialize report_descriptor_read() against probe and remove
Since commit 54f32fd5dff9 ("HID: Make report_descriptor available for all devices"), the "report_descriptor" sysfs attribute is part of the device's attribute group, so it exists from device_add() until device_del(). It reads hdev->rdesc, but that buffer only lives from hid_open_report() until hid_close_report(), that is, for the duration of a driver binding. For a driver with a report_fixup() callback, rdesc is a private copy that hid_close_report() frees, so a read that races with the driver being unbound accesses freed memory. syzbot reports this as:
BUG: KASAN: slab-use-after-free in report_descriptor_read+0xb5/0x100 Read of size 24 at addr ffff888078d61f00 by task fido_id/7281 ... Allocated by task 10: kmemdup_noprof+0x2b/0x70 hid_open_report+0x1fa/0xf00 lg_probe+0x29c/0x890 hid_device_probe+0x46a/0x870 ...
Because hid_close_report() also clears rdesc and rsize, the same missing serialization can make the reader see a NULL rdesc with a non-zero rsize.
Hold the device lock in report_descriptor_read(). The driver core already holds it across probe and remove, so a read can no longer overlap with them.
--- drivers/hid/hid-core.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/hid/hid-core.c +2 -0
@@ -2270,6 +2270,8 @@ report_descriptor_read(struct file *filp, struct kobject *kobj,struct device *dev =kobj_to_dev (kobj);struct hid_device *hdev =to_hid_device (dev); +guard (device)(dev); +if (off >= hdev->rsize)return 0 ;
Signature · 1 line
2.56.0
Hi,
Gentle ping on this patch.
Please let me know if you’d like any changes.
Thanks,
Krish Gulati
Quoted 28 lines · Krish Gulati
Since commit
54f32fd5dff9("HID: Makereport_descriptoravailable for all devices"), the "report_descriptor" sysfs attribute is part of the device's attribute group, so it exists fromdevice_add()untildevice_del(). It readshdev->rdesc, but that buffer only lives fromhid_open_report()untilhid_close_report(), that is, for the duration of a driver binding. For a driver with areport_fixup()callback, rdesc is a private copy thathid_close_report()frees, so a read that races with the driver being unbound accesses freed memory. syzbot reports this as:BUG: KASAN: slab-use-after-free in report_descriptor_read+0xb5/0x100 Read of size 24 at addr ffff888078d61f00 by task fido_id/7281 ... Allocated by task 10: kmemdup_noprof+0x2b/0x70 hid_open_report+0x1fa/0xf00 lg_probe+0x29c/0x890 hid_device_probe+0x46a/0x870 ...Because
hid_close_report()also clears rdesc and rsize, the same missing serialization can make the reader see a NULL rdesc with a non-zero rsize.Hold the device lock in
report_descriptor_read(). The driver core already holds it across probe and remove, so a read can no longer overlap with them.
Quoted 6 lines
Reported-by [email protected]Tested-by [email protected]Fixes 54f32fd5dff9 ("HID: Make report_descriptor available for all devices")Signed-off-by Krish Gulati <[email protected]>
Applied, thanks.
Signature · 2 lines
Jiri Kosina SUSE Labs