Re: [PATCH v2] tpm: Rework open/close/shutdown to avoid races
From: Jarkko Sakkinen
Date: Mon Mar 07 2022 - 10:42:25 EST
On Tue, Dec 15, 2020 at 10:51:43AM -0800, James Bottomley wrote:
> On Tue, 2020-12-15 at 16:38 +0300, Sergey Temerkhanov wrote:
> > Avoid race condition at shutdown by shutting downn the TPM 2.0
> > devices synchronously. This eliminates the condition when the
> > shutdown sequence sets chip->ops to NULL leading to the following:
> >
> > [ 1586.593561][ T8669] tpm2_del_space+0x28/0x73
> > [ 1586.598718][ T8669] tpmrm_release+0x27/0x33wq
> > [ 1586.603774][ T8669] __fput+0x109/0x1d
> > [ 1586.608380][ T8669] task_work_run+0x7c/0x90
> > [ 1586.613414][ T8669] prepare_exit_to_usermode+0xb8/0x128
> > [ 1586.619522][ T8669] entry_SYSCALL_64_after_hwframe+0x44/0xa9
> > [ 1586.626068][ T8669] RIP: 0033:0x4cb4bb
>
> An actual bug report would have been helpful. However, from this trace
> it's easy to deduce that tpm2_del_space() didn't get converted to the
> get/put of the chip ops ... it's still trying to do its own half arsed
> thing with tpm_chip_start() and the mutex. So isn't a much simpler fix
> simply to convert it as below? compile tested only, but if you can
> test it out I'll send a proper patch.
>
> James
>
> ---
>
> diff --git a/drivers/char/tpm/tpm2-space.c b/drivers/char/tpm/tpm2-space.c
> index 784b8b3cb903..0c0cd225046f 100644
> --- a/drivers/char/tpm/tpm2-space.c
> +++ b/drivers/char/tpm/tpm2-space.c
> @@ -58,12 +58,12 @@ int tpm2_init_space(struct tpm_space *space, unsigned int buf_size)
>
> void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space)
> {
> - mutex_lock(&chip->tpm_mutex);
> - if (!tpm_chip_start(chip)) {
> +
> + if (tpm_try_get_ops(chip) == 0) {
> tpm2_flush_sessions(chip, space);
> - tpm_chip_stop(chip);
> + tpm_put_ops(chip);
> }
> - mutex_unlock(&chip->tpm_mutex);
> +
> kfree(space->context_buf);
> kfree(space->session_buf);
> }
>
James, would it possible for you to construct a proper patch from this and
send it so we could include it to the next PR (use my tree as baseline)?
BR, Jarkko