Skip to contentSkip to conversation

tpm: infineon: add bounds check in tpm_inf_recv

PATCH In review Shahriyar Jalayeri 3 participants 4 messages Oct 3, 2025 – Oct 4, 2025

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

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

Ensure tpm_inf_recv() does not overflow the provided buffer when the TPM reports more data than the caller expects.

---
 drivers/char/tpm/tpm_infineon.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/char/tpm/tpm_infineon.c +12 -0
index 7638b65b8..eb6dd55ff 100644
@@ -250,6 +250,12 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 	number_of_wtx = 0;
 
 recv_begin:
+	if (count < 4) {
+		dev_err(&chip->dev,
+			"count less than the header size!\n");
+		return -EIO;
+	}
+
 	/* start receiving header */
 	for (i = 0; i < 4; i++) {
 		ret = wait(chip, STAT_RDA);
@@ -268,6 +274,12 @@ 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!\n");
+			return -EIO;
+		}
+
 		for (i = 0; i < size; i++) {
 			wait(chip, STAT_RDA);
 			buf[i] = tpm_data_in(RDFIFO);
Signature · 1 line
2.43.0
#1 lore raw

Dear Shahriyar,

Thank you for your patch.

Am 03.10.25 um 11:25 schrieb Shahriyar Jalayeri:

Quoted 2 lines

Ensure tpm_inf_recv() does not overflow the provided buffer when the TPM reports more data than the caller expects.

Is it possible to enforce this situation to ensure your patch works?

Quoted 10 lines
---
  drivers/char/tpm/tpm_infineon.c | 12 ++++++++++++
  1 file changed, 12 insertions(+)
drivers/char/tpm/tpm_infineon.c +0 -0
index 7638b65b8..eb6dd55ff 100644

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

Quoted 6 lines
drivers/char/tpm/tpm_infineon.c +3 -0
@@ -250,6 +250,12 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
  	number_of_wtx = 0;
 
  recv_begin:
+	if (count < 4) {
+		dev_err(&chip->dev,
+			"count less than the header size!\n");

Mention both values count and 4?

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

Quoted 3 lines
drivers/char/tpm/tpm_infineon.c +3 -0
+		return -EIO;
+	}
+

This is not described in the commit message.

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

Quoted 10 lines
drivers/char/tpm/tpm_infineon.c +3 -0
  	/* start receiving header */
  	for (i = 0; i < 4; i++) {
  		ret = wait(chip, STAT_RDA);
@@ -268,6 +274,12 @@ 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!\n");

I’d log both values, and also specify that the operation is aborted.

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

Quoted 6 lines
drivers/char/tpm/tpm_infineon.c +3 -0
+			return -EIO;
+		}
+
  		for (i = 0; i < size; i++) {
  			wait(chip, STAT_RDA);
  			buf[i] = tpm_data_in(RDFIFO);

Kind regards,

Paul

author
#2 lore raw

Dear Paul,

Quoted 3 lines

Is it possible to enforce this situation to ensure your patch works?

Triggering the issue requires interposing the hardware or a MCU to emulate the TPM and send malformed TPM replies, I might be able to test this using Qemu, but that takes some time.

For the rest of your suggestions, I'll send another patch.

BR,
/shj

maintainer
#3 lore raw
In reply to Shahriyar Jalayeri
Quoted 4 lines · Shahriyar Jalayeri

Ensure tpm_inf_recv() does not overflow the provided buffer when the TPM reports more data than the caller expects.

missing:

Fixes ebb81fdb3dd0 ("[PATCH] tpm: Support for Infineon TPM")
Quoted 9 lines
---
 drivers/char/tpm/tpm_infineon.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
drivers/char/tpm/tpm_infineon.c +0 -0
index 7638b65b8..eb6dd55ff 100644

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

Quoted 8 lines
drivers/char/tpm/tpm_infineon.c +5 -0
@@ -250,6 +250,12 @@ static int tpm_inf_recv(struct tpm_chip *chip, u8 * buf, size_t count)
 	number_of_wtx = 0;
 
 recv_begin:
+	if (count < 4) {
+		dev_err(&chip->dev,
+			"count less than the header size!\n");
+		return -EIO;
+	}

Please remove dev_err()

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

Quoted 13 lines
drivers/char/tpm/tpm_infineon.c +6 -0
+
 	/* start receiving header */
 	for (i = 0; i < 4; i++) {
 		ret = wait(chip, STAT_RDA);
@@ -268,6 +274,12 @@ 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!\n");
+			return -EIO;
+		}

Ditto

Quoted 6 lines
drivers/char/tpm/tpm_infineon.c +0 -0
 		for (i = 0; i < size; i++) {
 			wait(chip, STAT_RDA);
 			buf[i] = tpm_data_in(RDFIFO);
Signature · 1 line
2.43.0

BR, Jarkko