Re: [PATCH v2] tpm: vtpm_proxy: Double-check to avoid buffer overflow

From: Jason Gunthorpe
Date: Tue Jan 18 2022 - 14:39:36 EST


On Tue, Jan 18, 2022 at 08:32:43PM +0100, Jann Horn wrote:
> On Tue, Jan 18, 2022 at 7:37 PM Kees Cook <keescook@xxxxxxxxxxxx> 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
> > | ^
>
> Can you explain what that compiler warning actually means, and which
> compiler it is from? Is this from a 32-bit or a 64-bit architecture?
>
> It sounds like the compiler (GCC?) is hallucinating a codepath on
> which "len" is guaranteed to be >=2147483648, right? Why is it doing
> that? Is this some kinda side effect from the fortify code?

I agree, this looks bogus, or at least the commit message neeeds alot
more explaining.

static int vtpm_proxy_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t count)

if (count > sizeof(proxy_dev->buffer))
[...]
proxy_dev->req_len = count;

Not clear how req_len can be larger than sizeof(buffer)?

Jason