From: Kees Cook <keescook@chromium.org>
To: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Cc: Jason Gunthorpe <jgg@ziepe.ca>, Phil Baker <baker1tex@gmail.com>,
Craig Robson <craig@zhatt.com>, Laura Abbott <labbott@redhat.com>,
Tomas Winkler <tomas.winkler@intel.com>,
Nayna Jain <nayna@linux.vnet.ibm.com>,
linux-kernel@vger.kernel.org, Peter Huewe <peterhuewe@gmx.de>,
Arnd Bergmann <arnd@arndb.de>,
linux-integrity@vger.kernel.org
Subject: [PATCH v2] tpm: Actually fail on TPM errors during "get random"
Date: Mon, 1 Apr 2019 11:52:19 -0700 [thread overview]
Message-ID: <20190401185219.GA22215@beast> (raw)
A "get random" may fail with a TPM error, but those codes were returned
as-is to the caller, which assumed the result was the number of bytes
that had been written to the target buffer, which could lead to a kernel
heap memory exposure and over-read.
This fixes tpm1_get_random() and tpm2_get_random() to mask positive TPM
errors into -EIO while still passing through any negative errors.
[ 18.092103] tpm tpm0: A TPM error (379) occurred attempting get random
[ 18.092106] usercopy: Kernel memory exposure attempt detected from SLUB object 'kmalloc-64' (offset 0, size 379)!
Link: https://bugzilla.redhat.com/show_bug.cgi?id=1650989
Reported-by: Phil Baker <baker1tex@gmail.com>
Reported-by: Craig Robson <craig@zhatt.com>
Fixes: 7aee9c52d7ac ("tpm: tpm1: rewrite tpm1_get_random() using tpm_buf structure")
Fixes: ce63c05b664e ("tpm: migrate tpm2_get_random() to use struct tpm_buf")
Cc: Laura Abbott <labbott@redhat.com>
Cc: Tomas Winkler <tomas.winkler@intel.com>
Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Cc: stable@vger.kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
v2: - also fix tpm2 implementation (Jason Gunthorpe)
---
drivers/char/tpm/tpm1-cmd.c | 7 +++++--
drivers/char/tpm/tpm2-cmd.c | 7 +++++--
2 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
index 85dcf2654d11..8e17d4f30aeb 100644
--- a/drivers/char/tpm/tpm1-cmd.c
+++ b/drivers/char/tpm/tpm1-cmd.c
@@ -510,7 +510,7 @@ struct tpm1_get_random_out {
*
* Return:
* * number of bytes read
- * * -errno or a TPM return code otherwise
+ * * -errno (positive TPM return codes are masked to -EIO)
*/
int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
{
@@ -524,7 +524,7 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
rc = tpm_buf_init(&buf, TPM_TAG_RQU_COMMAND, TPM_ORD_GET_RANDOM);
if (rc)
- return rc;
+ goto fail;
do {
tpm_buf_append_u32(&buf, num_bytes);
@@ -559,6 +559,9 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
rc = total ? (int)total : -EIO;
out:
tpm_buf_destroy(&buf);
+fail:
+ if (rc > 0)
+ rc = -EIO;
return rc;
}
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index e74c5b7b64bf..dd0677f50f9c 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -301,7 +301,7 @@ struct tpm2_get_random_out {
*
* Return:
* size of the buffer on success,
- * -errno otherwise
+ * -errno otherwise ((positive TPM return codes are masked to -EIO)
*/
int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
{
@@ -319,7 +319,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
err = tpm_buf_init(&buf, 0, 0);
if (err)
- return err;
+ goto fail;
do {
tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM);
@@ -352,6 +352,9 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
return total ? total : -EIO;
out:
tpm_buf_destroy(&buf);
+fail:
+ if (err > 0)
+ erro = -EIO;
return err;
}
--
2.17.1
--
Kees Cook
next reply other threads:[~2019-04-01 18:52 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-04-01 18:52 Kees Cook [this message]
2019-04-01 18:54 ` [PATCH v2] tpm: Actually fail on TPM errors during "get random" James Bottomley
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=20190401185219.GA22215@beast \
--to=keescook@chromium.org \
--cc=arnd@arndb.de \
--cc=baker1tex@gmail.com \
--cc=craig@zhatt.com \
--cc=jarkko.sakkinen@linux.intel.com \
--cc=jgg@ziepe.ca \
--cc=labbott@redhat.com \
--cc=linux-integrity@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nayna@linux.vnet.ibm.com \
--cc=peterhuewe@gmx.de \
--cc=tomas.winkler@intel.com \
/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.