Skip to contentSkip to conversation

HID: Intel-thc-hid: Fix potential buffer overflow

PATCH Applied Even Xu 3 participants 5 messages Sep 22 – Sep 23

Subsystems (2)
lore
Share & tools
lore

Even Xu

author maintainer
#0 lore raw

cover HID: Intel-thc-hid: Fix potential buffer overflow

This patch set allocate enough space for both the payload and the packet header for quicki2c and quickspi input and output buffers to prevent a buffer overflow when the payload reaches its maximum size, as their sizes are currently calculated from the payload length only.

Even Xu (2):
HID: Intel-thc-hid: Intel-quicki2c: Fix buffer overflow
HID: Intel-thc-hid: Intel-quickspi: Fix buffer overflow

 drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c | 6 ++++--
 drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c | 3 ++-
 2 files changed, 6 insertions(+), 3 deletions(-)
Signature · 1 line
2.43.0
author maintainer
#1 lore raw

PATCH 1/2 HID: Intel-thc-hid: Intel-quicki2c: Fix buffer overflow

The input and output buffers are used to store complete HID-over-I2C packets, including their protocol headers. However, their sizes are currently calculated from the payload length only.

Allocate enough space for both the payload and the packet header to prevent a buffer overflow when the payload reaches its maximum size.

---
 drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c +4 -2
index 0d2ad7bc3648..2537288b5026 100644
@@ -606,7 +606,8 @@ static int quicki2c_alloc_report_buf(struct quicki2c_device *qcdev)
 	 */
 	max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_input_len), SZ_4K);
 
-	qcdev->input_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL);
+	qcdev->input_buf = devm_kzalloc(qcdev->dev,
+					HIDI2C_PACKET_LEN(max_report_len), GFP_KERNEL);
 	if (!qcdev->input_buf)
 		return -ENOMEM;
 
@@ -616,7 +617,8 @@ static int quicki2c_alloc_report_buf(struct quicki2c_device *qcdev)
 	max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_output_len),
 			     max_report_len);
 
-	qcdev->report_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL);
+	qcdev->report_buf = devm_kzalloc(qcdev->dev,
+					 HIDI2C_PACKET_LEN(max_report_len), GFP_KERNEL);
 	if (!qcdev->report_buf)
 		return -ENOMEM;
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] HID: Intel-thc-hid: Intel-quicki2c: Fix buffer overflow

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] hid: intel-quicki2c: incomplete buffer fix and stale report_len causes -EINVAL for large…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] hid: intel-quicki2c: incomplete buffer fix and stale report_len causes -EINVAL for large payloads --

commit 3ae5fe57bf2559497c69a1a1f4feb9ea45f51690
Author: Even Xu <[email protected]>

HID: Intel-thc-hid: Intel-quicki2c: Fix buffer overflow

The patch modifies buffer allocations in the intel-quicki2c driver to account for packet headers in addition to the payload length. This is intended to prevent buffer overflows when the payload reaches its maximum size.

Quoted 15 lines
drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c +2 -1
index 0d2ad7bc3648a..2537288b50261 100644
@@ -606,7 +606,8 @@ static int quicki2c_alloc_report_buf(struct quicki2c_device *qcdev)
 	 */
 	max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_input_len), SZ_4K);
 
-	qcdev->input_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL);
+	qcdev->input_buf = devm_kzalloc(qcdev->dev,
+					HIDI2C_PACKET_LEN(max_report_len), GFP_KERNEL);
 	if (!qcdev->input_buf)
 		return -ENOMEM;
 

drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c static int quicki2c_alloc_report_buf(struct quicki2c_device *qcdev) · line 621

Quoted 6 lines
drivers/hid/intel-thc-hid/intel-quicki2c/pci-quicki2c.c +2 -1
@@ -616,7 +617,8 @@ static int quicki2c_alloc_report_buf(struct quicki2c_device *qcdev)
 	max_report_len = max(le16_to_cpu(qcdev->dev_desc.max_output_len),
 			     max_report_len);
 
-	qcdev->report_buf = devm_kzalloc(qcdev->dev, max_report_len, GFP_KERNEL);
+	qcdev->report_buf = devm_kzalloc(qcdev->dev,
+					 HIDI2C_PACKET_LEN(max_report_len), GFP_KERNEL);

[Severity: Medium]
Does HIDI2C_PACKET_LEN() add enough space for the headers? It appears to only add 2 bytes, but output operations and SET_REPORT might require up to 9 bytes of command headers (e.g., cmd_reg, cmd, data_reg, length).

Also, qcdev->report_len is not updated to match the new size, remaining as max_report_len just below this allocation.

Will this cause maximum-sized payloads to be incorrectly rejected with -EINVAL? In quicki2c_init_write_buf(), there is a bounds check:

if (buf_len > write_buf_len)
return -EINVAL;

This evaluates the total packet length (including headers) against the unadjusted report_len (passed as write_buf_len), causing valid maximum-sized payloads to be rejected.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH 2/2 HID: Intel-thc-hid: Intel-quickspi: Fix buffer overflow

In reply to Even Xu

The input buffers are used to store complete HID-over-SPI packets, including their protocol headers. However, their sizes are currently calculated from the payload length only.

Allocate enough space for both the payload and the packet header to prevent a buffer overflow when the payload reaches its maximum size.

---
 drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/hid/intel-thc-hid/intel-quickspi/pci-quickspi.c +2 -1
index 89226f5ce45e..ad676fde211b 100644
@@ -552,7 +552,8 @@ static int quickspi_alloc_report_buf(struct quickspi_device *qsdev)
 	max_input_len = max(le16_to_cpu(qsdev->dev_desc.rep_desc_len),
 			    le16_to_cpu(qsdev->dev_desc.max_input_len));
 
-	qsdev->input_buf = devm_kzalloc(qsdev->dev, max_input_len, GFP_KERNEL);
+	qsdev->input_buf = devm_kzalloc(qsdev->dev,
+					HIDSPI_INPUT_BODY_SIZE(max_input_len), GFP_KERNEL);
 	if (!qsdev->input_buf)
 		return -ENOMEM;
 
Signature · 1 line
2.43.0
maintainer Applied
#4 lore raw
In reply to Even Xu
Quoted 8 lines · Even Xu

This patch set allocate enough space for both the payload and the packet header for quicki2c and quickspi input and output buffers to prevent a buffer overflow when the payload reaches its maximum size, as their sizes are currently calculated from the payload length only.

Even Xu (2):
HID: Intel-thc-hid: Intel-quicki2c: Fix buffer overflow
HID: Intel-thc-hid: Intel-quickspi: Fix buffer overflow

Applied to hid.git#for-7.3/upstream-fixes, thanks.

Signature · 2 lines
Jiri Kosina
SUSE Labs