Skip to content

Commit a033940

Browse files
committed
Merge tag 'tpmdd-next-6.12-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd
Pull tpm fix from Jarkko Sakkinen: "Fix a race condition between tpm_pm_suspend() and tpm_hwrng_read() (I think for good now)" * tag 'tpmdd-next-6.12-rc7' of git://git.kernel.org/pub/scm/linux/kernel/git/jarkko/linux-tpmdd: tpm: Lock TPM chip in tpm_pm_suspend() first
2 parents 59b723c + 9265fed commit a033940

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

drivers/char/tpm/tpm-chip.c

-4
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

+22-10
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)