Skip to contentSkip to conversation

tpm: infineon: add bounds check in tpm_inf_recv

PATCH In review Shahriyar Jalayeri 2 participants 2 messages Oct 4, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: infineon: add bounds check in tpm_inf_recv

Add two buffer size validations to prevent buffer overflows in tpm_inf_recv():

1. Validate that the provided buffer can hold at least the 4-byte header before attempting to read it.
2. Validate that the buffer is large enough to hold the data size reported by the TPM before reading the payload.

Without these checks, a malicious or malfunctioning TPM could cause buffer overflows by reporting data sizes larger than the provided buffer, leading to memory corruption.

The error messages include both the expected and actual buffer sizes to indicate that the operation is aborted.

---
 drivers/char/tpm/tpm_infineon.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm_infineon.c +18 -1
index 7638b65b8..385bac46a 100644
@@ -247,11 +247,20 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 	int i;
 	int ret;
 	u32 size = 0;
+	u32 header_size = 4;
 	number_of_wtx = 0;
 
 recv_begin:
+	if (count < header_size) {
+		dev_err(&chip->dev,
+			"Buffer too small (count=%zd, header_size=%u), "
+			"operation aborted\n",
+			count, header_size);
+		return -EIO;
+	}
+
 	/* start receiving header */
-	for (i = 0; i < 4; i++) {
+	for (i = 0; i < header_size; i++) {
 		ret = wait(chip, STAT_RDA);
 		if (ret)
 			return -EIO;
@@ -268,6 +277,14 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 		/* size of the data received */
 		size = ((buf[2] << 8) | buf[3]);
 
+		if (size > count) {
+			dev_err(&chip->dev,
+				"Buffer too small for incoming data "
+				"(count=%zd, size=%u), operation aborted\n",
+				count, size);
+			return -EIO;
+		}
+
 		for (i = 0; i < size; i++) {
 			wait(chip, STAT_RDA);
 			buf[i] = tpm_data_in(RDFIFO);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 6 lines · Shahriyar Jalayeri

Add two buffer size validations to prevent buffer overflows in tpm_inf_recv():

1. Validate that the provided buffer can hold at least the 4-byte header before attempting to read it.

What is this 4 byte header? Please describe what it is.

Quoted 4 lines

2. Validate that the buffer is large enough to hold the data size reported by the TPM before reading the payload.

Quoted 8 lines

Without these checks, a malicious or malfunctioning TPM could cause buffer overflows by reporting data sizes larger than the provided buffer, leading to memory corruption.

The error messages include both the expected and actual buffer sizes to indicate that the operation is aborted.

Paragraphs should be 75 chars per line.

Quoted 3 lines

---

Please version your patches e.g., "git format-patch -v2"

And before diff stat it would be good to have a changelog.

drivers/char/tpm/tpm_infineon.c static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count) · line 250

Quoted 12 lines
 drivers/char/tpm/tpm_infineon.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm_infineon.c +1 -0
index 7638b65b8..385bac46a 100644
@@ -247,11 +247,20 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 	int i;
 	int ret;
 	u32 size = 0;
+	u32 header_size = 4;

Please don't use local variable for a constant.

Quoted 35 lines
drivers/char/tpm/tpm_infineon.c +17 -1
 	number_of_wtx = 0;
 
 recv_begin:
+	if (count < header_size) {
+		dev_err(&chip->dev,
+			"Buffer too small (count=%zd, header_size=%u), "
+			"operation aborted\n",
+			count, header_size);
+		return -EIO;
+	}
+
 	/* start receiving header */
-	for (i = 0; i < 4; i++) {
+	for (i = 0; i < header_size; i++) {
 		ret = wait(chip, STAT_RDA);
 		if (ret)
 			return -EIO;
@@ -268,6 +277,14 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 		/* size of the data received */
 		size = ((buf[2] << 8) | buf[3]);
 
+		if (size > count) {
+			dev_err(&chip->dev,
+				"Buffer too small for incoming data "
+				"(count=%zd, size=%u), operation aborted\n",
+				count, size);
+			return -EIO;
+		}
+
 		for (i = 0; i < size; i++) {
 			wait(chip, STAT_RDA);
 			buf[i] = tpm_data_in(RDFIFO);
Signature · 1 line
2.43.0

Other than that, same comments apply as for the previous version.

BR, Jarkko