Skip to contentSkip to conversation

tpm: fix response size validation in tpm_get_random()

PATCH In review Jarkko Sakkinen 2 participants 3 messages Sep 3, 2018 – Sep 5, 2018

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

PATCH 1/1 tpm: fix response size validation in tpm_get_random()

When checking whether the response is large enough to be able to contain the received random bytes in tpm_get_random() and tpm2_get_random(), they fail to take account the header size, which should be added to the minimum size. This commit fixes this issue.

Cc stable at vger.kernel.org
Fixes c659af78eb7b ("tpm: Check size of response before accessing data")
---
 drivers/char/tpm/tpm-interface.c | 3 ++-
 drivers/char/tpm/tpm2-cmd.c      | 4 +++-
 2 files changed, 5 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm-interface.c +2 -1
index 1a803b0cf980..318a7078b2ba 100644
@@ -1321,7 +1321,8 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
 		}
 
 		rlength = be32_to_cpu(tpm_cmd.header.out.length);
-		if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
+		if (rlength < TPM_HEADER_SIZE +
+			      offsetof(struct tpm_getrandom_out, rng_data) +
 			      recd) {
 			total = -EFAULT;
 			break;
drivers/char/tpm/tpm2-cmd.c +3 -1
index c31b490bd41d..3acf4fd4e5a5 100644
@@ -329,7 +329,9 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 			&buf.data[TPM_HEADER_SIZE];
 		recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
 		if (tpm_buf_length(&buf) <
-		    offsetof(struct tpm2_get_random_out, buffer) + recd) {
+		    TPM_HEADER_SIZE +
+		    offsetof(struct tpm2_get_random_out, buffer) +
+		    recd) {
 			err = -EFAULT;
 			goto out;
 		}
Signature · 1 line
2.17.1
#1 lore raw
Quoted 8 lines · Jarkko Sakkinen

When checking whether the response is large enough to be able to contain the received random bytes in tpm_get_random() and tpm2_get_random(), they fail to take account the header size, which should be added to the minimum size. This commit fixes this issue.

Cc stable at vger.kernel.org
Fixes c659af78eb7b ("tpm: Check size of response before accessing data")

Stefan, you did the original commit. Does this look right to you?

/Jarkko

Reviewed-by on v1 1/1
#2 lore raw
In reply to Jarkko Sakkinen
Quoted 32 lines · Jarkko Sakkinen

When checking whether the response is large enough to be able to contain the received random bytes in tpm_get_random() and tpm2_get_random(), they fail to take account the header size, which should be added to the minimum size. This commit fixes this issue.

Cc stable at vger.kernel.org
Fixes c659af78eb7b ("tpm: Check size of response before accessing data")
---
  drivers/char/tpm/tpm-interface.c | 3 ++-
  drivers/char/tpm/tpm2-cmd.c      | 4 +++-
  2 files changed, 5 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm-interface.c +2 -1
index 1a803b0cf980..318a7078b2ba 100644
@@ -1321,7 +1321,8 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  		}
 
  		rlength = be32_to_cpu(tpm_cmd.header.out.length);
-		if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
+		if (rlength < TPM_HEADER_SIZE +
+			      offsetof(struct tpm_getrandom_out, rng_data) +
  			      recd) {
  			total = -EFAULT;
  			break;
drivers/char/tpm/tpm2-cmd.c +0 -0
index c31b490bd41d..3acf4fd4e5a5 100644

drivers/char/tpm/tpm2-cmd.c int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) · line 337

Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +3 -1
@@ -329,7 +329,9 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
  			&buf.data[TPM_HEADER_SIZE];
  		recd = min_t(u32, be16_to_cpu(out->size), num_bytes);
  		if (tpm_buf_length(&buf) <
-		    offsetof(struct tpm2_get_random_out, buffer) + recd) {
+		    TPM_HEADER_SIZE +
+		    offsetof(struct tpm2_get_random_out, buffer) +
+		    recd) {
  			err = -EFAULT;
  			goto out;
  		}