linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Berger <stefanb@linux.ibm.com>
To: Kees Cook <keescook@chromium.org>, Peter Huewe <peterhuewe@gmx.de>
Cc: Jarkko Sakkinen <jarkko@kernel.org>,
	Jason Gunthorpe <jgg@ziepe.ca>,
	linux-integrity@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org
Subject: Re: [PATCH] tpm: vtpm_proxy: Avoid device-originated buffer overflow
Date: Thu, 13 Jan 2022 16:19:32 -0500	[thread overview]
Message-ID: <4b59d305-6858-1514-751a-37853ad777be@linux.ibm.com> (raw)
In-Reply-To: <20220113002727.3709495-1-keescook@chromium.org>


On 1/12/22 19:27, Kees Cook wrote:
> When building with -Warray-bounds, this warning was emitted:
>
> In function 'memset',
>      inlined from 'vtpm_proxy_fops_read' at drivers/char/tpm/tpm_vtpm_proxy.c:102:2:
> ./include/linux/fortify-string.h:43:33: warning: '__builtin_memset' pointer overflow between offset 164 and size [2147483648, 4294967295]
> [-Warray-bounds]
>     43 | #define __underlying_memset     __builtin_memset
>        |                                 ^
>
> There was no checking of the req_len value from the device. A malicious
> (or buggy) device could end up leaking (and when wiping) memory contents
> beyond the end of the proxy buffer.
>
> Cc: Peter Huewe <peterhuewe@gmx.de>
> Cc: Jarkko Sakkinen <jarkko@kernel.org>
> Cc: Jason Gunthorpe <jgg@ziepe.ca>
> Cc: linux-integrity@vger.kernel.org
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>   drivers/char/tpm/tpm_vtpm_proxy.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/char/tpm/tpm_vtpm_proxy.c b/drivers/char/tpm/tpm_vtpm_proxy.c
> index 91c772e38bb5..5c865987ba5c 100644
> --- a/drivers/char/tpm/tpm_vtpm_proxy.c
> +++ b/drivers/char/tpm/tpm_vtpm_proxy.c
> @@ -91,7 +91,7 @@ static ssize_t vtpm_proxy_fops_read(struct file *filp, char __user *buf,
>   
>   	len = proxy_dev->req_len;
>   
> -	if (count < len) {
> +	if (count < len || len > sizeof(proxy_dev->buffer)) {
>   		mutex_unlock(&proxy_dev->buf_lock);
>   		pr_debug("Invalid size in recv: count=%zd, req_len=%zd\n",
>   			 count, len);


Thanks for this patch.

I just want to clarify this. In vtpm_proxy_tpm_op_send() we have the 
only place that sets req_len to a value larger than 0:

static int vtpm_proxy_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t 
count)
{
     struct proxy_dev *proxy_dev = dev_get_drvdata(&chip->dev);

     if (count > sizeof(proxy_dev->buffer)) {
         dev_err(&chip->dev,
             "Invalid size in send: count=%zd, buffer size=%zd\n",
             count, sizeof(proxy_dev->buffer));
         return -EIO;
     }

[...]

     proxy_dev->req_len = count;
     memcpy(proxy_dev->buffer, buf, count);

[...]

}


The above makes sure that we cannot copy more bytes into the 
proxy_dev->buffer than the what the buffer has bytes for.

It then sets req_len to a valid value that is less or equal to the 
buffer size.

Considering this your check above seems to only be there to make the 
compiler happy but otherwise I don't see that this is a real problem 
with a buffer overflow?!

Nevertheless, let all those compilers be happy:

Reviewed-by: Stefan Berger <stefanb@linux.ibm.com>



  reply	other threads:[~2022-01-13 21:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-13  0:27 [PATCH] tpm: vtpm_proxy: Avoid device-originated buffer overflow Kees Cook
2022-01-13 21:19 ` Stefan Berger [this message]
2022-01-18 18:35   ` Kees Cook

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=4b59d305-6858-1514-751a-37853ad777be@linux.ibm.com \
    --to=stefanb@linux.ibm.com \
    --cc=jarkko@kernel.org \
    --cc=jgg@ziepe.ca \
    --cc=keescook@chromium.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-integrity@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterhuewe@gmx.de \
    /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 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).