Takashi Iwai c1e5df
From eabad7ba2c752392ae50f24a795093fb115b686d Mon Sep 17 00:00:00 2001
Takashi Iwai c1e5df
From: Lino Sanfilippo <LinoSanfilippo@gmx.de>
Takashi Iwai c1e5df
Date: Mon, 20 Dec 2021 16:06:35 +0100
Takashi Iwai c1e5df
Subject: [PATCH] tpm: fix potential NULL pointer access in tpm_del_char_device
Takashi Iwai c1e5df
Git-commit: eabad7ba2c752392ae50f24a795093fb115b686d
Takashi Iwai c1e5df
Patch-mainline: v5.17-rc1
Michal Suchanek 11ac3f
References: git-fixes bsc#1193660 ltc#195634
Takashi Iwai c1e5df
Takashi Iwai c1e5df
Some SPI controller drivers unregister the controller in the shutdown
Takashi Iwai c1e5df
handler (e.g. BCM2835). If such a controller is used with a TPM 2 slave
Takashi Iwai c1e5df
chip->ops may be accessed when it is already NULL:
Takashi Iwai c1e5df
Takashi Iwai c1e5df
At system shutdown the pre-shutdown handler tpm_class_shutdown() shuts down
Takashi Iwai c1e5df
TPM 2 and sets chip->ops to NULL. Then at SPI controller unregistration
Takashi Iwai c1e5df
tpm_tis_spi_remove() is called and eventually calls tpm_del_char_device()
Takashi Iwai c1e5df
which tries to shut down TPM 2 again. Thereby it accesses chip->ops again:
Takashi Iwai c1e5df
(tpm_del_char_device calls tpm_chip_start which calls tpm_clk_enable which
Takashi Iwai c1e5df
calls chip->ops->clk_enable).
Takashi Iwai c1e5df
Takashi Iwai c1e5df
Avoid the NULL pointer access by testing if chip->ops is valid and skipping
Takashi Iwai c1e5df
the TPM 2 shutdown procedure in case it is NULL.
Takashi Iwai c1e5df
Takashi Iwai c1e5df
Cc: stable@vger.kernel.org
Takashi Iwai c1e5df
Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
Takashi Iwai c1e5df
Fixes: 39d0099f9439 ("powerpc/pseries: Add shutdown() to vio_driver and vio_bus")
Takashi Iwai c1e5df
Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>
Takashi Iwai c1e5df
Tested-by: Stefan Berger <stefanb@linux.ibm.com>
Takashi Iwai c1e5df
Reviewed-by: Jarkko Sakkinen <jarkko@kernel.org>
Takashi Iwai c1e5df
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
Takashi Iwai c1e5df
Acked-by: Takashi Iwai <tiwai@suse.de>
Takashi Iwai c1e5df
Takashi Iwai c1e5df
---
Takashi Iwai c1e5df
 drivers/char/tpm/tpm-chip.c | 18 +++++++++++++-----
Takashi Iwai c1e5df
 1 file changed, 13 insertions(+), 5 deletions(-)
Takashi Iwai c1e5df
Takashi Iwai c1e5df
diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
Takashi Iwai c1e5df
index b4ed3ae67a4b..b009e7479b70 100644
Takashi Iwai c1e5df
--- a/drivers/char/tpm/tpm-chip.c
Takashi Iwai c1e5df
+++ b/drivers/char/tpm/tpm-chip.c
Takashi Iwai c1e5df
@@ -474,13 +474,21 @@ static void tpm_del_char_device(struct tpm_chip *chip)
Takashi Iwai c1e5df
 
Takashi Iwai c1e5df
 	/* Make the driver uncallable. */
Takashi Iwai c1e5df
 	down_write(&chip->ops_sem);
Takashi Iwai c1e5df
-	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
Takashi Iwai c1e5df
-		if (!tpm_chip_start(chip)) {
Takashi Iwai c1e5df
-			tpm2_shutdown(chip, TPM2_SU_CLEAR);
Takashi Iwai c1e5df
-			tpm_chip_stop(chip);
Takashi Iwai c1e5df
+
Takashi Iwai c1e5df
+	/*
Takashi Iwai c1e5df
+	 * Check if chip->ops is still valid: In case that the controller
Takashi Iwai c1e5df
+	 * drivers shutdown handler unregisters the controller in its
Takashi Iwai c1e5df
+	 * shutdown handler we are called twice and chip->ops to NULL.
Takashi Iwai c1e5df
+	 */
Takashi Iwai c1e5df
+	if (chip->ops) {
Takashi Iwai c1e5df
+		if (chip->flags & TPM_CHIP_FLAG_TPM2) {
Takashi Iwai c1e5df
+			if (!tpm_chip_start(chip)) {
Takashi Iwai c1e5df
+				tpm2_shutdown(chip, TPM2_SU_CLEAR);
Takashi Iwai c1e5df
+				tpm_chip_stop(chip);
Takashi Iwai c1e5df
+			}
Takashi Iwai c1e5df
 		}
Takashi Iwai c1e5df
+		chip->ops = NULL;
Takashi Iwai c1e5df
 	}
Takashi Iwai c1e5df
-	chip->ops = NULL;
Takashi Iwai c1e5df
 	up_write(&chip->ops_sem);
Takashi Iwai c1e5df
 }
Takashi Iwai c1e5df
 
Takashi Iwai c1e5df
-- 
Takashi Iwai c1e5df
2.31.1
Takashi Iwai c1e5df