[PATCH v3 2/7] tpm: Return on tpm2_create_null_primary() failure

Jarkko Sakkinen posted 7 patches 2 months, 1 week ago
There is a newer version of this series
[PATCH v3 2/7] tpm: Return on tpm2_create_null_primary() failure
Posted by Jarkko Sakkinen 2 months, 1 week ago
tpm2_sessions_init() does not ignores the result of saving the null key.
Address this by printing either TPM or POSIX error code, and returning
-ENODEV back to the caller.

Cc: stable@vger.kernel.org # v6.11+
Fixes: d2add27cf2b8 ("tpm: Add NULL primary creation")
Signed-off-by: Jarkko Sakkinen <jarkko@kernel.org>
---
v3:
- Handle TPM and POSIX error separately and return -ENODEV always back
  to the caller.
v2:
- Refined the commit message.
---
 drivers/char/tpm/tpm2-sessions.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/char/tpm/tpm2-sessions.c b/drivers/char/tpm/tpm2-sessions.c
index 6cc1ea81c57c..0993d18ee886 100644
--- a/drivers/char/tpm/tpm2-sessions.c
+++ b/drivers/char/tpm/tpm2-sessions.c
@@ -1273,7 +1273,13 @@ static int tpm2_create_null_primary(struct tpm_chip *chip)
 		tpm2_flush_context(chip, null_key);
 	}
 
-	return rc;
+	if (rc < 0)
+		dev_err(&chip->dev, "saving the null key failed with error %d\n", rc);
+	else if (rc > 0)
+		dev_err(&chip->dev, "saving the null key failed with TPM error 0x%04X\n", rc);
+
+	/* Map all errors to -ENODEV: */
+	return rc ? -ENODEV : rc;
 }
 
 /**
@@ -1289,7 +1295,7 @@ int tpm2_sessions_init(struct tpm_chip *chip)
 
 	rc = tpm2_create_null_primary(chip);
 	if (rc)
-		dev_err(&chip->dev, "TPM: security failed (NULL seed derivation): %d\n", rc);
+		return rc;
 
 	chip->auth = kmalloc(sizeof(*chip->auth), GFP_KERNEL);
 	if (!chip->auth)
-- 
2.46.0