All of lore.kernel.org
 help / color / mirror / Atom feed
From: Lino Sanfilippo <LinoSanfilippo@gmx.de>
To: peterhuewe@gmx.de, jarkko@kernel.org, jgg@ziepe.ca
Cc: p.rosenberger@kunbus.com, linux-integrity@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Lino Sanfilippo <LinoSanfilippo@gmx.de>,
	stable@vger.kernel.org
Subject: [PATCH] tpm: fix potential NULL pointer access in tpm_del_char_device()
Date: Fri, 10 Sep 2021 20:04:51 +0200	[thread overview]
Message-ID: <20210910180451.19314-1-LinoSanfilippo@gmx.de> (raw)

In tpm_del_char_device() make sure that chip->ops is still valid.
This check is needed since in case of a system shutdown
tpm_class_shutdown() has already been called and set chip->ops to NULL.
This leads to a NULL pointer access as soon as tpm_del_char_device()
tries to access chip->ops in case of TPM 2.

Fixes: dcbeab1946454 ("tpm: fix crash in tpm_tis deinitialization")
Cc: stable@vger.kernel.org
Signed-off-by: Lino Sanfilippo <LinoSanfilippo@gmx.de>
---
 drivers/char/tpm/tpm-chip.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/drivers/char/tpm/tpm-chip.c b/drivers/char/tpm/tpm-chip.c
index ddaeceb7e109..ed1fb5d82caf 100644
--- a/drivers/char/tpm/tpm-chip.c
+++ b/drivers/char/tpm/tpm-chip.c
@@ -474,13 +474,19 @@ static void tpm_del_char_device(struct tpm_chip *chip)
 
 	/* Make the driver uncallable. */
 	down_write(&chip->ops_sem);
-	if (chip->flags & TPM_CHIP_FLAG_TPM2) {
-		if (!tpm_chip_start(chip)) {
-			tpm2_shutdown(chip, TPM2_SU_CLEAR);
-			tpm_chip_stop(chip);
+	/* Check if chip->ops is still valid since in case of a shutdown
+	 * tpm_class_shutdown() has already sent the TPM2_Shutdown command
+	 * and set chip->ops to NULL.
+	 */
+	if (chip->ops) {
+		if (chip->flags & TPM_CHIP_FLAG_TPM2) {
+			if (!tpm_chip_start(chip)) {
+				tpm2_shutdown(chip, TPM2_SU_CLEAR);
+				tpm_chip_stop(chip);
+			}
 		}
+		chip->ops = NULL;
 	}
-	chip->ops = NULL;
 	up_write(&chip->ops_sem);
 }
 

base-commit: a3fa7a101dcff93791d1b1bdb3affcad1410c8c1
-- 
2.33.0


             reply	other threads:[~2021-09-10 18:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-10 18:04 Lino Sanfilippo [this message]
2021-09-13 20:25 ` [PATCH] tpm: fix potential NULL pointer access in tpm_del_char_device() Jarkko Sakkinen
2021-09-13 20:53   ` Aw: " Lino Sanfilippo
2021-09-14  0:31     ` Jarkko Sakkinen
2021-09-24 13:29       ` Lino Sanfilippo
2021-09-24 13:33         ` Jason Gunthorpe
2021-09-24 14:17           ` Lino Sanfilippo
2021-09-24 14:20             ` Jason Gunthorpe
2021-09-29  8:58               ` Lino Sanfilippo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210910180451.19314-1-LinoSanfilippo@gmx.de \
    --to=linosanfilippo@gmx.de \
    --cc=jarkko@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=p.rosenberger@kunbus.com \
    --cc=peterhuewe@gmx.de \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.