linux-integrity.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] tpm: Actually fail on TPM errors during "get random"
@ 2019-04-01 18:32 Kees Cook
  2019-04-01 18:39 ` Jason Gunthorpe
  2019-04-01 18:52 ` James Bottomley
  0 siblings, 2 replies; 5+ messages in thread
From: Kees Cook @ 2019-04-01 18:32 UTC (permalink / raw)
  To: Jarkko Sakkinen, Tomas Winkler
  Cc: Phil Baker, Craig Robson, Laura Abbott, linux-kernel,
	Peter Huewe, Jason Gunthorpe, Arnd Bergmann, linux-integrity

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 an kernel
heap memory exposure and over-read.

This fixes tpm1_get_random() to mask positive TPM errors into -EIO, as
before.

[   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")
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>
---
 drivers/char/tpm/tpm1-cmd.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
index 85dcf2654d11..faeb78ecf960 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,7 +559,10 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 	rc = total ? (int)total : -EIO;
 out:
 	tpm_buf_destroy(&buf);
-	return rc;
+fail:
+	if (rc < 0)
+		return rc;
+	return -EIO;
 }
 
 #define TPM_ORD_PCRREAD 21
-- 
2.17.1


-- 
Kees Cook

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] tpm: Actually fail on TPM errors during "get random"
  2019-04-01 18:32 [PATCH] tpm: Actually fail on TPM errors during "get random" Kees Cook
@ 2019-04-01 18:39 ` Jason Gunthorpe
  2019-04-01 18:43   ` Kees Cook
  2019-04-01 18:52 ` James Bottomley
  1 sibling, 1 reply; 5+ messages in thread
From: Jason Gunthorpe @ 2019-04-01 18:39 UTC (permalink / raw)
  To: Kees Cook
  Cc: Jarkko Sakkinen, Tomas Winkler, Phil Baker, Craig Robson,
	Laura Abbott, linux-kernel, Peter Huewe, Arnd Bergmann,
	linux-integrity

On Mon, Apr 01, 2019 at 11:32:11AM -0700, Kees Cook wrote:
> 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 an kernel
> heap memory exposure and over-read.
> 
> This fixes tpm1_get_random() to mask positive TPM errors into -EIO, as
> before.
> 
> [   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")
> 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>
>  drivers/char/tpm/tpm1-cmd.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
> index 85dcf2654d11..faeb78ecf960 100644
> +++ 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;

?? Does tpm_buf_init() return positive?? Shouldn't..

I think you actually want this:

diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
index 85dcf2654d1102..a01e6fba1aacb6 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
  */
 int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 {
@@ -531,8 +531,10 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
 
                rc = tpm_transmit_cmd(chip, &buf, sizeof(out->rng_data_len),
                                      "attempting get random");
-               if (rc)
+               if (rc) {
+                       rc = -EIO
                        goto out;
+               }
 
                out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE];
 
diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
index e74c5b7b64bfbd..9fa498b4cf8816 100644
--- a/drivers/char/tpm/tpm2-cmd.c
+++ b/drivers/char/tpm/tpm2-cmd.c
@@ -328,8 +328,10 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
                                       offsetof(struct tpm2_get_random_out,
                                                buffer),
                                       "attempting get random");
-               if (err)
+               if (err) {
+                       rc = -EIO;
                        goto out;
+               }
 
                out = (struct tpm2_get_random_out *)
                        &buf.data[TPM_HEADER_SIZE];

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] tpm: Actually fail on TPM errors during "get random"
  2019-04-01 18:39 ` Jason Gunthorpe
@ 2019-04-01 18:43   ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2019-04-01 18:43 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Jarkko Sakkinen, Tomas Winkler, Phil Baker, Craig Robson,
	Laura Abbott, LKML, Peter Huewe, Arnd Bergmann, linux-integrity

On Mon, Apr 1, 2019 at 11:39 AM Jason Gunthorpe <jgg@ziepe.ca> wrote:
>
> On Mon, Apr 01, 2019 at 11:32:11AM -0700, Kees Cook wrote:
> > 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 an kernel
> > heap memory exposure and over-read.
> >
> > This fixes tpm1_get_random() to mask positive TPM errors into -EIO, as
> > before.
> >
> > [   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")
> > 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>
> >  drivers/char/tpm/tpm1-cmd.c | 9 ++++++---
> >  1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
> > index 85dcf2654d11..faeb78ecf960 100644
> > +++ 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;
>
> ?? Does tpm_buf_init() return positive?? Shouldn't..
>
> I think you actually want this:

I didn't check that, but it was tpm_transmit_cmd() that was failing
(and would be covered by the error path change I sent).

>
> diff --git a/drivers/char/tpm/tpm1-cmd.c b/drivers/char/tpm/tpm1-cmd.c
> index 85dcf2654d1102..a01e6fba1aacb6 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
>   */
>  int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
>  {
> @@ -531,8 +531,10 @@ int tpm1_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
>
>                 rc = tpm_transmit_cmd(chip, &buf, sizeof(out->rng_data_len),
>                                       "attempting get random");
> -               if (rc)
> +               if (rc) {
> +                       rc = -EIO
>                         goto out;
> +               }
>
>                 out = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE];
>
> diff --git a/drivers/char/tpm/tpm2-cmd.c b/drivers/char/tpm/tpm2-cmd.c
> index e74c5b7b64bfbd..9fa498b4cf8816 100644
> --- a/drivers/char/tpm/tpm2-cmd.c
> +++ b/drivers/char/tpm/tpm2-cmd.c
> @@ -328,8 +328,10 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)
>                                        offsetof(struct tpm2_get_random_out,
>                                                 buffer),
>                                        "attempting get random");
> -               if (err)
> +               if (err) {
> +                       rc = -EIO;
>                         goto out;
> +               }
>
>                 out = (struct tpm2_get_random_out *)
>                         &buf.data[TPM_HEADER_SIZE];

Eeks, I missed tpm2. In both cases, though a negative error would be
masked. I was trying to leave those as-is and only mask positive
errors.


-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tpm: Actually fail on TPM errors during "get random"
  2019-04-01 18:32 [PATCH] tpm: Actually fail on TPM errors during "get random" Kees Cook
  2019-04-01 18:39 ` Jason Gunthorpe
@ 2019-04-01 18:52 ` James Bottomley
  2019-04-01 18:54   ` Kees Cook
  1 sibling, 1 reply; 5+ messages in thread
From: James Bottomley @ 2019-04-01 18:52 UTC (permalink / raw)
  To: Kees Cook, Jarkko Sakkinen, Tomas Winkler
  Cc: Phil Baker, Craig Robson, Laura Abbott, linux-kernel,
	Peter Huewe, Jason Gunthorpe, Arnd Bergmann, linux-integrity

On Mon, 2019-04-01 at 11:32 -0700, Kees Cook wrote:
[...]
> --- 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,7 +559,10 @@ int tpm1_get_random(struct tpm_chip *chip, u8
> *dest, size_t max)
>  	rc = total ? (int)total : -EIO;
>  out:
>  	tpm_buf_destroy(&buf);
> -	return rc;

You can't remove this otherwise the only return will ever be a failure.

I think what you're trying to catch is tpm_transmit_cmd returning a
positive failure, So you need to check the output of tpm_transmit_cmd
as well and goto failure leaving the above return in place.

James


> +fail:
> +	if (rc < 0)
> +		return rc;
> +	return -EIO;
>  }
>  
>  #define TPM_ORD_PCRREAD 21
> -- 
> 2.17.1
> 
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] tpm: Actually fail on TPM errors during "get random"
  2019-04-01 18:52 ` James Bottomley
@ 2019-04-01 18:54   ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2019-04-01 18:54 UTC (permalink / raw)
  To: James Bottomley
  Cc: Jarkko Sakkinen, Tomas Winkler, Phil Baker, Craig Robson,
	Laura Abbott, LKML, Peter Huewe, Jason Gunthorpe, Arnd Bergmann,
	linux-integrity

On Mon, Apr 1, 2019 at 11:52 AM James Bottomley
<James.Bottomley@hansenpartnership.com> wrote:
>
> On Mon, 2019-04-01 at 11:32 -0700, Kees Cook wrote:
> [...]
> > --- 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,7 +559,10 @@ int tpm1_get_random(struct tpm_chip *chip, u8
> > *dest, size_t max)
> >       rc = total ? (int)total : -EIO;
> >  out:
> >       tpm_buf_destroy(&buf);
> > -     return rc;
>
> You can't remove this otherwise the only return will ever be a failure.
>
> I think what you're trying to catch is tpm_transmit_cmd returning a
> positive failure, So you need to check the output of tpm_transmit_cmd
> as well and goto failure leaving the above return in place.

eek, yes. I double-checked this in tpm2, but tpm1 is different...

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-04-01 18:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-04-01 18:32 [PATCH] tpm: Actually fail on TPM errors during "get random" Kees Cook
2019-04-01 18:39 ` Jason Gunthorpe
2019-04-01 18:43   ` Kees Cook
2019-04-01 18:52 ` James Bottomley
2019-04-01 18:54   ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).