[PATCH 0/2] Cover Letter: HID: drop assumptions on non-empty report lists

From: Pietro Borrello
Date: Mon Jan 16 2023 - 06:12:09 EST


We found potential misuses of list_entry() on lists in hid driver
code that are not checked.
Issuing a list_entry() on an empty list causes a type confusion making
the list_entry point to the list_head itself.
The most impactful seems the missing check for an empty list in
hid_validate_values() which is supposed to check the validity of the
reports themselves, potentially affecting all the drivers that rely on it.

The problem is caused by the driver's assumption that the device must
have valid report_list. While this will be true for all normal HID
devices, a suitably malicious device can violate the assumption.

This patch fixes the issue by checking that the lists are nonempty
before allowing them to be used.

At a first glance it may seem that the patches have security implications.
However, when plugging a device which provides a descriptor with no output
report, the type confusions will create a fake struct hid_report*
which points to ((struct hid_device *)hid).report_enum[type].report_list.
This, by chance, makes the type confused structure to span
the `struct hid_report* report_id_hash[256]` array in the
((struct hid_device *)hid).report_enum[type] field.

Then, due to their semantics hid_validate_values() will check
(report->maxfield > field_index) on the type-confused report,
and the maxfield field happens to overlap on the
report_id_hash[] array in the report_enum[type] field
which are all NULL since we provided no reports.
Similarly, for bigben_probe(), the confused report entry is
used in the bigben_worker() function which checks
(report->field[0] != NULL) that, again, overlaps with a NULL
pointer.
It seems there is a commit (918aa1ef104d: "HID: bigbenff: prevent
null pointer dereference") which added the check for report_field
being NULL to bigben_worker() to prevent crashing, but without
checking the actual root cause.

Thus, while being type confusions bugs, they are not exploitable.
I still believe list checks should be added, and the patches on
hid_validate_values() and bigben_probe() merged,
to prevent future exploitability if the shape of the structure changes,
and they do not overlap anymore with NULL pointers.
In this case, it is not exploitable just by the pure chance of struct
member ordering.

To: Jiri Kosina <jikos@xxxxxxxxxx>
To: Benjamin Tissoires <benjamin.tissoires@xxxxxxxxxx>
To: Kees Cook <keescook@xxxxxxxxxxxx>
To: Hanno Zulla <abos@xxxxxxxx>
Cc: Cristiano Giuffrida <c.giuffrida@xxxxx>
Cc: "Bos, H.J." <h.j.bos@xxxxx>
Cc: Jakob Koschel <jkl820.git@xxxxxxxxx>
Cc: Jiri Kosina <jkosina@xxxxxxx>
Cc: linux-input@xxxxxxxxxxxxxxx
Cc: linux-kernel@xxxxxxxxxxxxxxx
Signed-off-by: Pietro Borrello <borrello@xxxxxxxxxxxxxxxx>

---
Pietro Borrello (2):
HID: check empty report_list in hid_validate_values()
HID: check empty report_list in bigben_probe()

drivers/hid/hid-bigbenff.c | 5 +++++
drivers/hid/hid-core.c | 4 ++--
2 files changed, 7 insertions(+), 2 deletions(-)
---
base-commit: 5dc4c995db9eb45f6373a956eb1f69460e69e6d4
change-id: 20230114-hid-fix-emmpty-report-list-0d9ab58b234d

Best regards,
--
Pietro Borrello <borrello@xxxxxxxxxxxxxxxx>