Skip to content

Commit 446daaf

Browse files
keesgregkh
authored andcommitted
tpm: Actually fail on TPM errors during "get random"
commit 782779b upstream. A "get random" may fail with a TPM error, but those codes were returned as-is to the caller, which assumed the result was the number of bytes that had been written to the target buffer, which could lead to a kernel heap memory exposure and over-read. This fixes tpm1_get_random() to mask positive TPM errors into -EIO, as before. [ 18.092103] tpm tpm0: A TPM error (379) occurred attempting get random [ 18.092106] usercopy: Kernel memory exposure attempt detected from SLUB object 'kmalloc-64' (offset 0, size 379)! Link: https://bugzilla.redhat.com/show_bug.cgi?id=1650989 Reported-by: Phil Baker <baker1tex@gmail.com> Reported-by: Craig Robson <craig@zhatt.com> Fixes: 7aee9c5 ("tpm: tpm1: rewrite tpm1_get_random() using tpm_buf structure") Cc: Laura Abbott <labbott@redhat.com> Cc: Tomas Winkler <tomas.winkler@intel.com> Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Cc: stable@vger.kernel.org Signed-off-by: Kees Cook <keescook@chromium.org> Reviewed-by: Tomas Winkler <tomas.winkler@intel.com> Tested-by: Bartosz Szczepanek <bsz@semihalf.com> Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent b487df8 commit 446daaf

2 files changed

Lines changed: 10 additions & 4 deletions

File tree

‎drivers/char/tpm/tpm1-cmd.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ struct tpm1_get_random_out {
510510
*
511511
* Return:
512512
* * number of bytes read
513-
* * -errno or a TPM return code otherwise
513+
* * -errno (positive TPM return codes are masked to -EIO)
514514
*/
515515
int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
516516
{
@@ -531,8 +531,11 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
531531

532532
rc = tpm_transmit_cmd(chip, &buf, sizeof(out->rng_data_len),
533533
"attempting get random");
534-
if (rc)
534+
if (rc) {
535+
if (rc > 0)
536+
rc = -EIO;
535537
goto out;
538+
}
536539

537540
out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE];
538541

‎drivers/char/tpm/tpm2-cmd.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ struct tpm2_get_random_out {
301301
*
302302
* Return:
303303
* size of the buffer on success,
304-
* -errno otherwise
304+
* -errno otherwise (positive TPM return codes are masked to -EIO)
305305
*/
306306
int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
307307
{
@@ -328,8 +328,11 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
328328
offsetof(struct tpm2_get_random_out,
329329
buffer),
330330
"attempting get random");
331-
if (err)
331+
if (err) {
332+
if (err > 0)
333+
err = -EIO;
332334
goto out;
335+
}
333336

334337
out = (struct tpm2_get_random_out *)
335338
&buf.data[TPM_HEADER_SIZE];

0 commit comments

Comments
 (0)