Skip to contentSkip to conversation

tpm: Check for integer overflow in tpm2_map_response_body()

PATCH In review Dan Carpenter 2 participants 2 messages Sep 8, 2021 – Sep 9, 2021

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

PATCH 1/1 tpm: Check for integer overflow in tpm2_map_response_body()

The "4 * be32_to_cpu(data->count)" multiplication can potentially overflow which would lead to memory corruption. Add a check for that.

Fixes 745b361e989a ("tpm: infrastructure for TPM spaces")
---
 drivers/char/tpm/tpm2-space.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/char/tpm/tpm2-space.c +3 -0
index 784b8b3cb903..97e916856cf3 100644
@@ -455,6 +455,9 @@ static int tpm2_map_response_body(struct tpm_chip *chip, u32 cc, u8 *rsp,
 	if (be32_to_cpu(data->capability) != TPM2_CAP_HANDLES)
 		return 0;
 
+	if (be32_to_cpu(data->count) > (UINT_MAX - TPM_HEADER_SIZE - 9) / 4)
+		return -EFAULT;
+
 	if (len != TPM_HEADER_SIZE + 9 + 4 * be32_to_cpu(data->count))
 		return -EFAULT;
 
Signature · 1 line
2.20.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Dan Carpenter

The "4 * be32_to_cpu(data->count)" multiplication can potentially overflow which would lead to memory corruption. Add a check for that.

Fixes 745b361e989a ("tpm: infrastructure for TPM spaces")
---
 drivers/char/tpm/tpm2-space.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/char/tpm/tpm2-space.c +0 -0
index 784b8b3cb903..97e916856cf3 100644

drivers/char/tpm/tpm2-space.c static int tpm2_map_response_body(struct tpm_chip *chip, u32 cc, u8 *rsp, · line 463

Quoted 9 lines
drivers/char/tpm/tpm2-space.c +3 -0
@@ -455,6 +455,9 @@ static int tpm2_map_response_body(struct tpm_chip *chip, u32 cc, u8 *rsp,
 	if (be32_to_cpu(data->capability) != TPM2_CAP_HANDLES)
 		return 0;
 
+	if (be32_to_cpu(data->count) > (UINT_MAX - TPM_HEADER_SIZE - 9) / 4)
+		return -EFAULT;
+
 	if (len != TPM_HEADER_SIZE + 9 + 4 * be32_to_cpu(data->count))
 		return -EFAULT;
 

Thanks!

/Jarkko