Skip to content

Commit 9265fed

Browse files
committed
tpm: Lock TPM chip in tpm_pm_suspend() first
Setting TPM_CHIP_FLAG_SUSPENDED in the end of tpm_pm_suspend() can be racy according, as this leaves window for tpm_hwrng_read() to be called while the operation is in progress. The recent bug report gives also evidence of this behaviour. Aadress this by locking the TPM chip before checking any chip->flags both in tpm_pm_suspend() and tpm_hwrng_read(). Move TPM_CHIP_FLAG_SUSPENDED check inside tpm_get_random() so that it will be always checked only when the lock is reserved. Cc: [email protected] # v6.4+ Fixes: 99d4645 ("tpm: Prevent hwrng from activating during resume") Reported-by: Mike Seo <[email protected]> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=219383 Reviewed-by: Jerry Snitselaar <[email protected]> Tested-by: Mike Seo <[email protected]> Signed-off-by: Jarkko Sakkinen <[email protected]>
1 parent 1106680 commit 9265fed

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

drivers/char/tpm/tpm-chip.c

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

528-
/* Give back zero bytes, as TPM chip has not yet fully resumed: */
529-
if (chip->flags & TPM_CHIP_FLAG_SUSPENDED)
530-
return 0;
531-
532528
return tpm_get_random(chip, data, max);
533529
}
534530

drivers/char/tpm/tpm-interface.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -370,28 +370,33 @@ int tpm_pm_suspend(struct device *dev)
370370
if (!chip)
371371
return -ENODEV;
372372

373+
rc = tpm_try_get_ops(chip);
374+
if (rc) {
375+
/* Can be safely set out of locks, as no action cannot race: */
376+
chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
377+
goto out;
378+
}
379+
373380
if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
374381
goto suspended;
375382

376383
if ((chip->flags & TPM_CHIP_FLAG_FIRMWARE_POWER_MANAGED) &&
377384
!pm_suspend_via_firmware())
378385
goto suspended;
379386

380-
rc = tpm_try_get_ops(chip);
381-
if (!rc) {
382-
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
383-
tpm2_end_auth_session(chip);
384-
tpm2_shutdown(chip, TPM2_SU_STATE);
385-
} else {
386-
rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
387-
}
388-
389-
tpm_put_ops(chip);
387+
if (chip->flags & TPM_CHIP_FLAG_TPM2) {
388+
tpm2_end_auth_session(chip);
389+
tpm2_shutdown(chip, TPM2_SU_STATE);
390+
goto suspended;
390391
}
391392

393+
rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
394+
392395
suspended:
393396
chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
397+
tpm_put_ops(chip);
394398

399+
out:
395400
if (rc)
396401
dev_err(dev, "Ignoring error %d while suspending\n", rc);
397402
return 0;
@@ -440,11 +445,18 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
440445
if (!chip)
441446
return -ENODEV;
442447

448+
/* Give back zero bytes, as TPM chip has not yet fully resumed: */
449+
if (chip->flags & TPM_CHIP_FLAG_SUSPENDED) {
450+
rc = 0;
451+
goto out;
452+
}
453+
443454
if (chip->flags & TPM_CHIP_FLAG_TPM2)
444455
rc = tpm2_get_random(chip, out, max);
445456
else
446457
rc = tpm1_get_random(chip, out, max);
447458

459+
out:
448460
tpm_put_ops(chip);
449461
return rc;
450462
}

0 commit comments

Comments
 (0)