Skip to contentSkip to conversation

HID: magicmouse: Prevent out-of-bounds (OOB) read during DOUBLE_REPORT_ID

PATCH Superseded Lee Jones 2 participants 2 messages Apr 14 – Apr 15 HID

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Lee Jones

author
#0 lore raw

PATCH 1/1 HID: magicmouse: Prevent out-of-bounds (OOB) read during DOUBLE_REPORT_ID

It is currently possible for a malicious or misconfigured USB device to cause an out-of-bounds (OOB) read when submitting reports using DOUBLE_REPORT_ID by specifying a large report length and providing a smaller one.

Let's prevent that by comparing the specified report length with the actual size of the data read in from userspace. If the actual data length ends up being smaller than specified, we'll politely warn the user and prevent any further processing.

---
 drivers/hid/hid-magicmouse.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/hid/hid-magicmouse.c +8 -0
index 91f621ceb924..5f44129e6dcc 100644
@@ -490,6 +490,14 @@ static int magicmouse_raw_event(struct hid_device *hdev,
 		/* Sometimes the trackpad sends two touch reports in one
 		 * packet.
 		 */
+
+		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]);
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog
#1 lore raw

Hello Lee!

Quoted 36 lines · Lee Jones

It is currently possible for a malicious or misconfigured USB device to cause an out-of-bounds (OOB) read when submitting reports using DOUBLE_REPORT_ID by specifying a large report length and providing a smaller one.

Let's prevent that by comparing the specified report length with the actual size of the data read in from userspace. If the actual data length ends up being smaller than specified, we'll politely warn the user and prevent any further processing.

---
 drivers/hid/hid-magicmouse.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/hid/hid-magicmouse.c +8 -0
index 91f621ceb924..5f44129e6dcc 100644
@@ -490,6 +490,14 @@ static int magicmouse_raw_event(struct hid_device *hdev,
 		/* Sometimes the trackpad sends two touch reports in one
 		 * packet.
 		 */
+
+		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]);
Signature · 1 line
2.54.0.rc0.605.g598a273b03-goog

I am afraid this still looks buggy to me.

With your check, size can still be *equal* to data[1] + 2. In that case, the second recursive call becomes

  magicmouse_raw_event(hdev, report, data + size, 0);

so this points just after the original "data" buffer with zero size.

But the magicmouse_raw_event() function accesses data[0] directly at the beginning. It assumes that size is >= 1, but it does not check that before that first access.

—Günther