Skip to content

Commit 99d4645

Browse files
committed
tpm: Prevent hwrng from activating during resume
Set TPM_CHIP_FLAG_SUSPENDED in tpm_pm_suspend() and reset in tpm_pm_resume(). While the flag is set, tpm_hwrng() gives back zero bytes. This prevents hwrng from racing during resume. Cc: [email protected] Fixes: 6e592a0 ("tpm: Move Linux RNG connection to hwrng") Reviewed-by: Jerry Snitselaar <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 1398aa8 commit 99d4645

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

drivers/char/tpm/tpm-chip.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,10 @@ static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
571571
{
572572
struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
573573

574+
/* Give back zero bytes, as TPM chip has not yet fully resumed: */
575+
if (chip->flags & TPM_CHIP_FLAG_SUSPENDED)
576+
return 0;
577+
574578
return tpm_get_random(chip, data, max);
575579
}
576580

drivers/char/tpm/tpm-interface.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,8 @@ int tpm_pm_suspend(struct device *dev)
412412
}
413413

414414
suspended:
415+
chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
416+
415417
if (rc)
416418
dev_err(dev, "Ignoring error %d while suspending\n", rc);
417419
return 0;
@@ -429,6 +431,14 @@ int tpm_pm_resume(struct device *dev)
429431
if (chip == NULL)
430432
return -ENODEV;
431433

434+
chip->flags &= ~TPM_CHIP_FLAG_SUSPENDED;
435+
436+
/*
437+
* Guarantee that SUSPENDED is written last, so that hwrng does not
438+
* activate before the chip has been fully resumed.
439+
*/
440+
wmb();
441+
432442
return 0;
433443
}
434444
EXPORT_SYMBOL_GPL(tpm_pm_resume);

include/linux/tpm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ enum tpm_chip_flags {
282282
TPM_CHIP_FLAG_ALWAYS_POWERED = BIT(5),
283283
TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED = BIT(6),
284284
TPM_CHIP_FLAG_FIRMWARE_UPGRADE = BIT(7),
285+
TPM_CHIP_FLAG_SUSPENDED = BIT(8),
285286
};
286287

287288
#define to_tpm_chip(d) container_of(d, struct tpm_chip, dev)

0 commit comments

Comments
 (0)