Skip to contentSkip to conversation

tpm: vtpm_proxy: Avoid device-originated buffer overflow

PATCH In review Kees Cook 2 participants 3 messages Jan 13, 2022 – Jan 18, 2022

Subsystems (2)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 tpm: vtpm_proxy: Avoid device-originated buffer overflow

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 <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
---
 drivers/char/tpm/tpm_vtpm_proxy.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/tpm_vtpm_proxy.c +1 -1
index 91c772e38bb5..5c865987ba5c 100644
@@ -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);
Signature · 1 line
2.30.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 27 lines · Kees Cook

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 <[email protected]>
Cc Jarkko Sakkinen <[email protected]>
Cc Jason Gunthorpe <[email protected]>
---
  drivers/char/tpm/tpm_vtpm_proxy.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/tpm/tpm_vtpm_proxy.c +0 -0
index 91c772e38bb5..5c865987ba5c 100644

drivers/char/tpm/tpm_vtpm_proxy.c static ssize_t vtpm_proxy_fops_read(struct file *filp, char __user *buf, · line 97

Quoted 8 lines
drivers/char/tpm/tpm_vtpm_proxy.c +1 -1
@@ -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:

author
#2 lore raw
Quoted 31 lines · Stefan Berger

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.

Quoted 7 lines

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:

Ah yes, thanks! I'll reword the commit log for v2. :)

Signature · 1 line
Kees Cook