Skip to contentSkip to conversation

tpm: fix buffer overflow in /dev/tpm0

PATCH In review Jarkko Sakkinen 2 participants 5 messages Sep 11, 2016 – Sep 12, 2016 TPM device driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: fix buffer overflow in /dev/tpm0

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.

---
 drivers/char/tpm/tpm-interface.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/char/tpm/tpm-interface.c +3 -0
index fd863ff..6a67f7f 100644
@@ -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;
 
Signature · 1 line
2.7.4
#1 lore raw
Quoted 5 lines · Jarkko Sakkinen

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.

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

/Jarkko

Quoted 21 lines
---
 drivers/char/tpm/tpm-interface.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/char/tpm/tpm-interface.c +3 -0
index fd863ff..6a67f7f 100644
@@ -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;
 
Signature · 1 line
2.7.4
#2 lore raw
Quoted 9 lines · Jarkko Sakkinen
Quoted 5 lines · Jarkko Sakkinen

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.

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

------------------------------------------------------------------------------

#3 lore raw
In reply to Jarkko Sakkinen
Quoted 3 lines · Jarkko Sakkinen

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

------------------------------------------------------------------------------

#4 lore raw
Quoted 10 lines · Jason Gunthorpe
Quoted 3 lines · Jarkko Sakkinen

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.

Quoted 6 lines

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.

Quoted 1 line

Jason

/Jarkko

------------------------------------------------------------------------------