]> www.infradead.org Git - users/jedix/linux-maple.git/commitdiff
tpm: Lock TPM chip in tpm_pm_suspend() first
authorJarkko Sakkinen <jarkko@kernel.org>
Thu, 31 Oct 2024 00:16:09 +0000 (02:16 +0200)
committerJarkko Sakkinen <jarkko@kernel.org>
Sun, 3 Nov 2024 23:59:08 +0000 (01:59 +0200)
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: stable@vger.kernel.org # v6.4+
Fixes: 99d464506255 ("tpm: Prevent hwrng from activating during resume")
Reported-by: Mike Seo <mikeseohyungjin@gmail.com>
Closes: https://bugzilla.kernel.org/show_bug.cgi?id=219383
Reviewed-by: Jerry Snitselaar <jsnitsel@redhat.com>
Tested-by: Mike Seo <mikeseohyungjin@gmail.com>
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
drivers/char/tpm/tpm-chip.c
drivers/char/tpm/tpm-interface.c

index 1ff99a7091bbbacff18aa45d76f806b951d94b28..7df7abaf3e526bf7e85ac9dfbaa1087a51d2ab7e 100644 (file)
@@ -525,10 +525,6 @@ static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait)
 {
        struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng);
 
-       /* Give back zero bytes, as TPM chip has not yet fully resumed: */
-       if (chip->flags & TPM_CHIP_FLAG_SUSPENDED)
-               return 0;
-
        return tpm_get_random(chip, data, max);
 }
 
index 8134f002b121f80ba8fe3f28218a27be1497bc4b..b1daa0d7b341b1a4c71a200115f0d29d2e87512d 100644 (file)
@@ -370,6 +370,13 @@ int tpm_pm_suspend(struct device *dev)
        if (!chip)
                return -ENODEV;
 
+       rc = tpm_try_get_ops(chip);
+       if (rc) {
+               /* Can be safely set out of locks, as no action cannot race: */
+               chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
+               goto out;
+       }
+
        if (chip->flags & TPM_CHIP_FLAG_ALWAYS_POWERED)
                goto suspended;
 
@@ -377,21 +384,19 @@ int tpm_pm_suspend(struct device *dev)
            !pm_suspend_via_firmware())
                goto suspended;
 
-       rc = tpm_try_get_ops(chip);
-       if (!rc) {
-               if (chip->flags & TPM_CHIP_FLAG_TPM2) {
-                       tpm2_end_auth_session(chip);
-                       tpm2_shutdown(chip, TPM2_SU_STATE);
-               } else {
-                       rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
-               }
-
-               tpm_put_ops(chip);
+       if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+               tpm2_end_auth_session(chip);
+               tpm2_shutdown(chip, TPM2_SU_STATE);
+               goto suspended;
        }
 
+       rc = tpm1_pm_suspend(chip, tpm_suspend_pcr);
+
 suspended:
        chip->flags |= TPM_CHIP_FLAG_SUSPENDED;
+       tpm_put_ops(chip);
 
+out:
        if (rc)
                dev_err(dev, "Ignoring error %d while suspending\n", rc);
        return 0;
@@ -440,11 +445,18 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)
        if (!chip)
                return -ENODEV;
 
+       /* Give back zero bytes, as TPM chip has not yet fully resumed: */
+       if (chip->flags & TPM_CHIP_FLAG_SUSPENDED) {
+               rc = 0;
+               goto out;
+       }
+
        if (chip->flags & TPM_CHIP_FLAG_TPM2)
                rc = tpm2_get_random(chip, out, max);
        else
                rc = tpm1_get_random(chip, out, max);
 
+out:
        tpm_put_ops(chip);
        return rc;
 }