Skip to contentSkip to conversation

HID: logitech-dj: Prevent REPORT_ID_DJ_SHORT related user initiated OOB write

PATCH In review Lee Jones 3 participants 5 messages Mar 17 – Mar 19

Subsystems (2)
lore
Share & tools
lore

Lee Jones

author
#0 lore raw

PATCH 1/1 HID: logitech-dj: Prevent REPORT_ID_DJ_SHORT related user initiated OOB write

logi_dj_recv_send_report() assumes that all incoming REPORT_ID_DJ_SHORT reports are 14 Bytes (DJREPORT_SHORT_LENGTH - 1) long. It uses that assumption to load the associated field's 'value' array with 14 Bytes of data. However, if a malicious user only sends say 1 Byte of data, 'report_count' will be 1 and only 1 Byte of memory will be allocated to the 'value' Byte array. When we come to populate 'value[1-13]' we will experience an OOB write.

---
 drivers/hid/hid-logitech-dj.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/hid/hid-logitech-dj.c +6 -0
index 44b716697510..885b986c7a12 100644
@@ -1282,6 +1282,12 @@ static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
 		return -ENODEV;
 	}
 
+	if (report->maxfield < 1 || report->field[0]->report_count != DJREPORT_SHORT_LENGTH - 1) {
+		hid_err(hdev, "Expected size of dj report is %d, but got %d",
+			DJREPORT_SHORT_LENGTH - 1, report->field[0]->report_count);
+		return -EINVAL;
+	}
+
 	for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
 		report->field[0]->value[i] = data[i];
 
Signature · 1 line
2.53.0.851.ga537e3e6e9-goog
#1 lore raw
Quoted 18 lines · Lee Jones

logi_dj_recv_send_report() assumes that all incoming REPORT_ID_DJ_SHORT reports are 14 Bytes (DJREPORT_SHORT_LENGTH - 1) long. It uses that assumption to load the associated field's 'value' array with 14 Bytes of data. However, if a malicious user only sends say 1 Byte of data, 'report_count' will be 1 and only 1 Byte of memory will be allocated to the 'value' Byte array. When we come to populate 'value[1-13]' we will experience an OOB write.

---
 drivers/hid/hid-logitech-dj.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/hid/hid-logitech-dj.c +0 -0
index 44b716697510..885b986c7a12 100644

drivers/hid/hid-logitech-dj.c static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, · line 1285

Quoted 4 lines
drivers/hid/hid-logitech-dj.c +1 -0
@@ -1282,6 +1282,12 @@ static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
 		return -ENODEV;
 	}
 
+	if (report->maxfield < 1 || report->field[0]->report_count != DJREPORT_SHORT_LENGTH - 1) {

This is all static information. So this should be checked in the .probe(), once the device has been parsed, not for every single call of logi_dj_recv_send_report().

Cheers,
Benjamin

Quoted 12 lines
drivers/hid/hid-logitech-dj.c +5 -0
+		hid_err(hdev, "Expected size of dj report is %d, but got %d",
+			DJREPORT_SHORT_LENGTH - 1, report->field[0]->report_count);
+		return -EINVAL;
+	}
+
 	for (i = 0; i < DJREPORT_SHORT_LENGTH - 1; i++)
 		report->field[0]->value[i] = data[i];
 
Signature · 1 line
2.53.0.851.ga537e3e6e9-goog
author
#2 lore raw
Quoted 22 lines · Benjamin Tissoires
Quoted 21 lines · Lee Jones

logi_dj_recv_send_report() assumes that all incoming REPORT_ID_DJ_SHORT reports are 14 Bytes (DJREPORT_SHORT_LENGTH - 1) long. It uses that assumption to load the associated field's 'value' array with 14 Bytes of data. However, if a malicious user only sends say 1 Byte of data, 'report_count' will be 1 and only 1 Byte of memory will be allocated to the 'value' Byte array. When we come to populate 'value[1-13]' we will experience an OOB write.

---
 drivers/hid/hid-logitech-dj.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/hid/hid-logitech-dj.c +0 -0
index 44b716697510..885b986c7a12 100644
@@ -1282,6 +1282,12 @@ static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
 		return -ENODEV;
 	}
 
Quoted 5 lines

drivers/hid/hid-logitech-dj.c static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, · line 1285

Quoted 1 line
drivers/hid/hid-logitech-dj.c +1 -0
+	if (report->maxfield < 1 || report->field[0]->report_count != DJREPORT_SHORT_LENGTH - 1) {

This is all static information. So this should be checked in the .probe(), once the device has been parsed, not for every single call of logi_dj_recv_send_report().

Doesn't report_count come from the device?

I can manipulate it with my user app.

Signature · 1 line
Lee Jones [李琼斯]
maintainer
#3 lore raw
Quoted 8 lines · Lee Jones
Quoted 8 lines
Quoted 8 lines
drivers/hid/hid-logitech-dj.c +0 -0
index 44b716697510..885b986c7a12 100644
@@ -1282,6 +1282,12 @@ static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
 		return -ENODEV;
 	}
 
Quoted 7 lines
Quoted 5 lines

drivers/hid/hid-logitech-dj.c static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, · line 1285

Quoted 1 line
drivers/hid/hid-logitech-dj.c +1 -0
+	if (report->maxfield < 1 || report->field[0]->report_count != DJREPORT_SHORT_LENGTH - 1) {

This is all static information. So this should be checked in the .probe(), once the device has been parsed, not for every single call of logi_dj_recv_send_report().

Doesn't report_count come from the device?

The point is -- maxfield and report_count can't change once parsed unless the report descriptor would be re-read and re-parsed (which doesn't happen in runtime, only during probe).

So checking during probe/parse time just once should be sufficient, instead of checking it upon every received report.

Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs
author
#4 lore raw
Quoted 16 lines · Jiri Kosina
Quoted 14 lines · Lee Jones
Quoted 13 lines

drivers/hid/hid-logitech-dj.c static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev, · line 1285

Quoted 9 lines
drivers/hid/hid-logitech-dj.c +1 -0
index 44b716697510..885b986c7a12 100644
@@ -1282,6 +1282,12 @@ static int logi_dj_recv_send_report(struct dj_receiver_dev *djrcv_dev,
 		return -ENODEV;
 	}
 
+	if (report->maxfield < 1 || report->field[0]->report_count != DJREPORT_SHORT_LENGTH - 1) {

This is all static information. So this should be checked in the .probe(), once the device has been parsed, not for every single call of logi_dj_recv_send_report().

Quoted 8 lines
Quoted 1 line

Doesn't report_count come from the device?

The point is -- maxfield and report_count can't change once parsed unless the report descriptor would be re-read and re-parsed (which doesn't happen in runtime, only during probe).

So checking during probe/parse time just once should be sufficient, instead of checking it upon every received report.

Okay, thanks for the explanation. I'll give it a shot.

Signature · 1 line
Lee Jones [李琼斯]