tpm: Check for integer overflow in tpm2_map_response_body()
authorDan Carpenter <dan.carpenter@oracle.com>
Wed, 8 Sep 2021 05:33:57 +0000 (08:33 +0300)
committerJarkko Sakkinen <jarkko@kernel.org>
Tue, 26 Oct 2021 02:03:33 +0000 (05:03 +0300)
The "4 * be32_to_cpu(data->count)" multiplication can potentially
overflow which would lead to memory corruption.  Add a check for that.

Cc: stable@vger.kernel.org
Fixes: 745b361e989a ("tpm: infrastructure for TPM spaces")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
drivers/char/tpm/tpm2-space.c

index 784b8b3..97e9168 100644 (file)
@@ -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;