diff mbox

tpm: fix buffer overflow in /dev/tpm0

Message ID 1473596340-11376-1-git-send-email-jarkko.sakkinen@linux.intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jarkko Sakkinen Sept. 11, 2016, 12:19 p.m. UTC
tpm_write() does not check whether the buffer has at least enough space
for the header before passing it to tpm_transmit() so an overflow can
happen.

Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
---
 drivers/char/tpm/tpm-interface.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Jarkko Sakkinen Sept. 11, 2016, 12:51 p.m. UTC | #1
On Sun, Sep 11, 2016 at 03:19:00PM +0300, Jarkko Sakkinen wrote:
> tpm_write() does not check whether the buffer has at least enough space
> for the header before passing it to tpm_transmit() so an overflow can
> happen.
> 
> Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

This is usable neither as read nor write primitive for an exploit. Still
it makes sense to validate the input here.

/Jarkko

> ---
>  drivers/char/tpm/tpm-interface.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
> index fd863ff..6a67f7f 100644
> --- a/drivers/char/tpm/tpm-interface.c
> +++ b/drivers/char/tpm/tpm-interface.c
> @@ -337,6 +337,9 @@ ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
>  	u32 count, ordinal;
>  	unsigned long stop;
>  
> +	if (bufsiz < TPM_HEADER_SIZE)
> +		return -EINVAL;
> +
>  	if (bufsiz > TPM_BUFSIZE)
>  		bufsiz = TPM_BUFSIZE;
>  
> -- 
> 2.7.4
> 

------------------------------------------------------------------------------
Jarkko Sakkinen Sept. 11, 2016, 6:57 p.m. UTC | #2
On Sun, Sep 11, 2016 at 03:51:42PM +0300, Jarkko Sakkinen wrote:
> On Sun, Sep 11, 2016 at 03:19:00PM +0300, Jarkko Sakkinen wrote:
> > tpm_write() does not check whether the buffer has at least enough space
> > for the header before passing it to tpm_transmit() so an overflow can
> > happen.
> > 
> > Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> 
> This is usable neither as read nor write primitive for an exploit. Still
> it makes sense to validate the input here.

Cannot add fixes line for this one as it's from pre-GIT times...

/Jarkko

------------------------------------------------------------------------------
Jason Gunthorpe Sept. 12, 2016, 4:05 a.m. UTC | #3
On Sun, Sep 11, 2016 at 03:19:00PM +0300, Jarkko Sakkinen wrote:
> tpm_write() does not check whether the buffer has at least enough space
> for the header before passing it to tpm_transmit() so an overflow can
> happen.

Eh?

tpm_write uses a hard wired buffer size of TPM_BUFSIZE when working
with tpm_transmit.

in_size is never used except for the copy. We should probably fix that
to sanity check the header length vs in_size.

That doesn't seem to be a security issue however because the header
length is propery limited to TPM_BUFSIZE and the data buffer is
allocated specifically for that process using kzalloc.

Jason

------------------------------------------------------------------------------
Jarkko Sakkinen Sept. 12, 2016, 7:50 a.m. UTC | #4
On Sun, Sep 11, 2016 at 10:05:46PM -0600, Jason Gunthorpe wrote:
> On Sun, Sep 11, 2016 at 03:19:00PM +0300, Jarkko Sakkinen wrote:
> > tpm_write() does not check whether the buffer has at least enough space
> > for the header before passing it to tpm_transmit() so an overflow can
> > happen.
> 
> Eh?
> 
> tpm_write uses a hard wired buffer size of TPM_BUFSIZE when working
> with tpm_transmit.
> 
> in_size is never used except for the copy. We should probably fix that
> to sanity check the header length vs in_size.
> 
> That doesn't seem to be a security issue however because the header
> length is propery limited to TPM_BUFSIZE and the data buffer is
> allocated specifically for that process using kzalloc.

I was working on something else when I bumped into this. The commit
message is not the best possible but still the issue is valid although
it does not cause any imaginable harm because there is always
TPM_BUFSIZE of room in the buffer passed by tpm_write.

I'll update the commit message not to speak about tpm_write.

"tpm_transmit() does not check whether the bufsiz can contain the TPM
header. Add check for this and return -EINVAL if it the buffer is too
small."

The check should be in tpm_transmit() and also the commit message should
be only about that.

> Jason

/Jarkko

------------------------------------------------------------------------------
diff mbox

Patch

diff --git a/drivers/char/tpm/tpm-interface.c b/drivers/char/tpm/tpm-interface.c
index fd863ff..6a67f7f 100644
--- a/drivers/char/tpm/tpm-interface.c
+++ b/drivers/char/tpm/tpm-interface.c
@@ -337,6 +337,9 @@  ssize_t tpm_transmit(struct tpm_chip *chip, const u8 *buf, size_t bufsiz,
 	u32 count, ordinal;
 	unsigned long stop;
 
+	if (bufsiz < TPM_HEADER_SIZE)
+		return -EINVAL;
+
 	if (bufsiz > TPM_BUFSIZE)
 		bufsiz = TPM_BUFSIZE;