Re: [PATCH v3] tpm: Lock TPM chip in tpm_pm_suspend() first
From: Jarkko Sakkinen
Date: Thu Oct 31 2024 - 20:27:11 EST
On Fri Nov 1, 2024 at 2:21 AM EET, Jarkko Sakkinen wrote:
> 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@xxxxxxxxxxxxxxx # v6.4+
> Fixes: 99d464506255 ("tpm: Prevent hwrng from activating during resume")
> Reported-by: Mike Seo <mikeseohyungjin@xxxxxxxxx>
> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=219383
> Signed-off-by: Jarkko Sakkinen <jarkko@xxxxxxxxxx>
A basic smoke test in QEMU:
# rtcwake -m mem -s 15
rtcwake -m mem -s 15
rtcwake: assuming RTC uses UTC ...
rtcwake: wakeup from "mem" using /dev/rtc0 at Fri Nov 1 02:21:06 2024
PM: suspend entry (deep)
Filesystems sync: 0.017 seconds
Freezing user space processes
Freezing user space processes completed (elapsed 0.004 seconds)
OOM killer disabled.
Freezing remaining freezable tasks
Freezing remaining freezable tasks completed (elapsed 0.004 seconds)
printk: Suspending console(s) (use no_console_suspend to debug)
ata2.00: Check power mode failed (err_mask=0x1)
ACPI: PM: Preparing to enter system sleep state S3
ACPI: PM: Saving platform NVS memory
Disabling non-boot CPUs ...
ACPI: PM: Low-level resume complete
ACPI: PM: Restoring platform NVS memory
ACPI: PM: Waking up from system sleep state S3
pci 0000:00:01.0: PIIX3: Enabling Passive Release
virtio_blk virtio1: 1/0/0 default/read/poll queues
OOM killer enabled.
Restarting tasks ... done.
random: crng reseeded on system resumption
PM: suspend exit
# ata2: found unknown device (class 0)
# dmesg|tail -20
dmesg|tail -20
[ 28.199150] Freezing user space processes
[ 28.205393] Freezing user space processes completed (elapsed 0.004
seconds)
[ 28.206780] OOM killer disabled.
[ 28.207858] Freezing remaining freezable tasks
[ 28.213224] Freezing remaining freezable tasks completed (elapsed
0.004 seconds)
[ 28.214591] printk: Suspending console(s) (use no_console_suspend to
debug)
[ 28.222203] ata2.00: Check power mode failed (err_mask=0x1)
[ 28.240808] ACPI: PM: Preparing to enter system sleep state S3
[ 28.241218] ACPI: PM: Saving platform NVS memory
[ 28.241390] Disabling non-boot CPUs ...
[ 28.243011] ACPI: PM: Low-level resume complete
[ 28.243273] ACPI: PM: Restoring platform NVS memory
[ 28.246191] ACPI: PM: Waking up from system sleep state S3
[ 28.250415] pci 0000:00:01.0: PIIX3: Enabling Passive Release
[ 28.256539] virtio_blk virtio1: 1/0/0 default/read/poll queues
[ 28.280715] OOM killer enabled.
[ 28.281766] Restarting tasks ... done.
[ 28.287096] random: crng reseeded on system resumption
[ 28.288181] PM: suspend exit
[ 28.410073] ata2: found unknown device (class 0)
Testing done with https://codeberg.org/jarkko/linux-tpmdd-test
cmake -Bbuild -Dbuildroot_defconfig=busybox_x86_64_defconfig && make -Cbuild buildroot-prepare
make -Cbuild/buildroot/build
pushd build/buildroot/build
images/run-qemu.sh &
socat - UNIX-CONNECT:images/serial.sock
BR, Jarkko