Skip to contentSkip to conversation

Fix kernel buffer overruns caused by bit flips

PATCH Superseded James Bottomley 2 participants 9 messages Feb 8, 2018 – Feb 14, 2018

Subsystems (2)
lore
Share & tools
lore

James Bottomley

author maintainer
#0 lore raw

v2 cover Fix kernel buffer overruns caused by bit flips

If a TPM is attached to a system via a serial bus on a platform that suffers bit flips, we can get back dangerously wrong data. This patch series aims never to do a direct copy into a kernel buffer based on an unchecked size value returned from the TPM.

Jeremy Boone (5):
tpm: fix potential buffer overruns caused by bit glitches on the bus
tpm: st33zp24: fix potential buffer overruns caused by bit glitches on
the bus
tpm_i2c_infineon: fix potential buffer overruns caused by bit glitches
on the bus
tpm_i2c_nuvoton: fix potential buffer overruns caused by bit glitches
on the bus
tpm_tis: fix potential buffer overruns caused by bit glitches on the
bus

 drivers/char/tpm/st33zp24/st33zp24.c | 4 ++--
 drivers/char/tpm/tpm-interface.c     | 4 ++++
 drivers/char/tpm/tpm2-cmd.c          | 4 ++++
 drivers/char/tpm/tpm_i2c_infineon.c  | 5 +++--
 drivers/char/tpm/tpm_i2c_nuvoton.c   | 5 +++--
 drivers/char/tpm/tpm_tis_core.c      | 5 +++--
 6 files changed, 19 insertions(+), 8 deletions(-)
Signature · 1 line
2.12.3
author maintainer
#1 lore raw

PATCH v2 2/5 tpm: st33zp24: fix potential buffer overruns caused by bit glitches on the bus

From: Jeremy Boone <[email protected]>

Discrete TPMs are often connected over slow serial buses which, on some platforms, can have glitches causing bit flips. In all the driver _recv() functions, we need to use a u32 to unmarshal the response size, otherwise a bit flip of the 31st bit would cause the expected variable to go negative, which would then try to read a huge amount of data. Also sanity check that the expected amount of data is large enough for the TPM header.

---
 drivers/char/tpm/st33zp24/st33zp24.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/char/tpm/st33zp24/st33zp24.c +2 -2
index 4d1dc8b46877..f95b9c75175b 100644
@@ -457,7 +457,7 @@ static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
 			    size_t count)
 {
 	int size = 0;
-	int expected;
+	u32 expected;
 
 	if (!chip)
 		return -EBUSY;
@@ -474,7 +474,7 @@ static int st33zp24_recv(struct tpm_chip *chip, unsigned char *buf,
 	}
 
 	expected = be32_to_cpu(*(__be32 *)(buf + 2));
-	if (expected > count) {
+	if (expected > count || expected < TPM_HEADER_SIZE) {
 		size = -EIO;
 		goto out;
 	}
Signature · 1 line
2.12.3
author maintainer
#2 lore raw

PATCH v2 3/5 tpm_i2c_infineon: fix potential buffer overruns caused by bit glitches on the bus

In reply to James Bottomley

From: Jeremy Boone <[email protected]>

Discrete TPMs are often connected over slow serial buses which, on some platforms, can have glitches causing bit flips. In all the driver _recv() functions, we need to use a u32 to unmarshal the response size, otherwise a bit flip of the 31st bit would cause the expected variable to go negative, which would then try to read a huge amount of data. Also sanity check that the expected amount of data is large enough for the TPM header.

---
 drivers/char/tpm/tpm_i2c_infineon.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm_i2c_infineon.c +3 -2
index 79d6bbb58e39..d5b44cadac56 100644
@@ -473,7 +473,8 @@ static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 {
 	int size = 0;
-	int expected, status;
+	int status;
+	u32 expected;
 
 	if (count < TPM_HEADER_SIZE) {
 		size = -EIO;
@@ -488,7 +489,7 @@ static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 	}
 
 	expected = be32_to_cpu(*(__be32 *)(buf + 2));
-	if ((size_t) expected > count) {
+	if (((size_t) expected > count) || (expected < TPM_HEADER_SIZE)) {
 		size = -EIO;
 		goto out;
 	}
Signature · 1 line
2.12.3
author maintainer
#3 lore raw

PATCH v2 4/5 tpm_i2c_nuvoton: fix potential buffer overruns caused by bit glitches on the bus

In reply to James Bottomley

From: Jeremy Boone <[email protected]>

Discrete TPMs are often connected over slow serial buses which, on some platforms, can have glitches causing bit flips. In all the driver _recv() functions, we need to use a u32 to unmarshal the response size, otherwise a bit flip of the 31st bit would cause the expected variable to go negative, which would then try to read a huge amount of data. Also sanity check that the expected amount of data is large enough for the TPM header.

---
 drivers/char/tpm/tpm_i2c_nuvoton.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm_i2c_nuvoton.c +3 -2
index c6428771841f..17cf7af9a2c5 100644
@@ -281,7 +281,8 @@ static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 	struct device *dev = chip->dev.parent;
 	struct i2c_client *client = to_i2c_client(dev);
 	s32 rc;
-	int expected, status, burst_count, retries, size = 0;
+	int status, burst_count, retries, size = 0;
+	u32 expected;
 
 	if (count < TPM_HEADER_SIZE) {
 		i2c_nuvoton_ready(chip);    /* return to idle */
@@ -323,7 +324,7 @@ static int i2c_nuvoton_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 		 * to machine native
 		 */
 		expected = be32_to_cpu(*(__be32 *) (buf + 2));
-		if (expected > count) {
+		if (expected > count || expected < size) {
 			dev_err(dev, "%s() expected > count\n", __func__);
 			size = -EIO;
 			continue;
Signature · 1 line
2.12.3
author maintainer
#4 lore raw

PATCH v2 5/5 tpm_tis: fix potential buffer overruns caused by bit glitches on the bus

In reply to James Bottomley

From: Jeremy Boone <[email protected]>

Discrete TPMs are often connected over slow serial buses which, on some platforms, can have glitches causing bit flips. In all the driver _recv() functions, we need to use a u32 to unmarshal the response size, otherwise a bit flip of the 31st bit would cause the expected variable to go negative, which would then try to read a huge amount of data. Also sanity check that the expected amount of data is large enough for the TPM header.

---
 drivers/char/tpm/tpm_tis_core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/char/tpm/tpm_tis_core.c +3 -2
index fdde971bc810..7561922bc8f8 100644
@@ -202,7 +202,8 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 {
 	struct tpm_tis_data *priv = dev_get_drvdata(&chip->dev);
 	int size = 0;
-	int expected, status;
+	int status;
+	u32 expected;
 
 	if (count < TPM_HEADER_SIZE) {
 		size = -EIO;
@@ -217,7 +218,7 @@ static int tpm_tis_recv(struct tpm_chip *chip, u8 *buf, size_t count)
 	}
 
 	expected = be32_to_cpu(*(__be32 *) (buf + 2));
-	if (expected > count) {
+	if (expected > count || expected < TPM_HEADER_SIZE) {
 		size = -EIO;
 		goto out;
 	}
Signature · 1 line
2.12.3
Reviewed-by on the v2 series
#5 lore raw
In reply to James Bottomley
Quoted 16 lines · James Bottomley

If a TPM is attached to a system via a serial bus on a platform that suffers bit flips, we can get back dangerously wrong data. This patch series aims never to do a direct copy into a kernel buffer based on an unchecked size value returned from the TPM.

Jeremy Boone (5):
tpm: fix potential buffer overruns caused by bit glitches on the bus
tpm: st33zp24: fix potential buffer overruns caused by bit glitches on
the bus
tpm_i2c_infineon: fix potential buffer overruns caused by bit glitches
on the bus
tpm_i2c_nuvoton: fix potential buffer overruns caused by bit glitches
on the bus
tpm_tis: fix potential buffer overruns caused by bit glitches on the
bus

Quoted 10 lines
 drivers/char/tpm/st33zp24/st33zp24.c | 4 ++--
 drivers/char/tpm/tpm-interface.c     | 4 ++++
 drivers/char/tpm/tpm2-cmd.c          | 4 ++++
 drivers/char/tpm/tpm_i2c_infineon.c  | 5 +++--
 drivers/char/tpm/tpm_i2c_nuvoton.c   | 5 +++--
 drivers/char/tpm/tpm_tis_core.c      | 5 +++--
 6 files changed, 19 insertions(+), 8 deletions(-)
Signature · 1 line
o 2.12.3

I cannot test all of these but I'll put these to linux-next anyway. The changes are obvious and small scoped so if no one shouts they'll be part of the next PR.

I've tested the changes that affect tpm2-cmd.c, tpm-interface.c and tpm_tis_core.c. For HW specific changes tested-by's would be much appreciated but I don't think they will break anything.

Because these are quite critical fixes I wonder if I could do one more PR to 4.16?

/Jarkko

author maintainer
#6 lore raw
Quoted 41 lines · Jarkko Sakkinen
Quoted 23 lines · James Bottomley

If a TPM is attached to a system via a serial bus on a platform that suffers bit flips, we can get back dangerously wrong data. This patch series aims never to do a direct copy into a kernel buffer based on an unchecked size value returned from the TPM.

Jeremy Boone (5):
tpm: fix potential buffer overruns caused by bit glitches on the
bus
tpm: st33zp24: fix potential buffer overruns caused by bit
glitches on
the bus
tpm_i2c_infineon: fix potential buffer overruns caused by bit
glitches
on the bus
tpm_i2c_nuvoton: fix potential buffer overruns caused by bit
glitches
on the bus
tpm_tis: fix potential buffer overruns caused by bit glitches on
the
bus

Quoted 10 lines
 drivers/char/tpm/st33zp24/st33zp24.c | 4 ++--
 drivers/char/tpm/tpm-interface.c     | 4 ++++
 drivers/char/tpm/tpm2-cmd.c          | 4 ++++
 drivers/char/tpm/tpm_i2c_infineon.c  | 5 +++--
 drivers/char/tpm/tpm_i2c_nuvoton.c   | 5 +++--
 drivers/char/tpm/tpm_tis_core.c      | 5 +++--
 6 files changed, 19 insertions(+), 8 deletions(-)
Signature · 1 line
o 2.12.3

I cannot test all of these but I'll put these to linux-next anyway. The changes are obvious and small scoped so if no one shouts they'll be part of the next PR.

Quoted 6 lines

I've tested the changes that affect tpm2-cmd.c, tpm-interface.c and tpm_tis_core.c. For HW specific changes tested-by's would be much appreciated but I don't think they will break anything.

Because these are quite critical fixes I wonder if I could do one more PR to 4.16?

They're all cc'd to stable, so they'd make the stable updates to 4.16 regardless of when they're pulled. Since the merge window will close on Sunday and you have to go via James' tree, I'd say it would cause a lot of stress to try to make 4.16 but it's your call.

James

#7 lore raw
Quoted 49 lines · James Bottomley
Quoted 41 lines · Jarkko Sakkinen
Quoted 23 lines · James Bottomley

If a TPM is attached to a system via a serial bus on a platform that suffers bit flips, we can get back dangerously wrong data. This patch series aims never to do a direct copy into a kernel buffer based on an unchecked size value returned from the TPM.

Jeremy Boone (5):
tpm: fix potential buffer overruns caused by bit glitches on the
bus
tpm: st33zp24: fix potential buffer overruns caused by bit
glitches on
the bus
tpm_i2c_infineon: fix potential buffer overruns caused by bit
glitches
on the bus
tpm_i2c_nuvoton: fix potential buffer overruns caused by bit
glitches
on the bus
tpm_tis: fix potential buffer overruns caused by bit glitches on
the
bus

Quoted 10 lines
 drivers/char/tpm/st33zp24/st33zp24.c | 4 ++--
 drivers/char/tpm/tpm-interface.c     | 4 ++++
 drivers/char/tpm/tpm2-cmd.c          | 4 ++++
 drivers/char/tpm/tpm_i2c_infineon.c  | 5 +++--
 drivers/char/tpm/tpm_i2c_nuvoton.c   | 5 +++--
 drivers/char/tpm/tpm_tis_core.c      | 5 +++--
 6 files changed, 19 insertions(+), 8 deletions(-)
Signature · 1 line
o 2.12.3

I cannot test all of these but I'll put these to linux-next anyway. The changes are obvious and small scoped so if no one shouts they'll be part of the next PR.

Quoted 6 lines

I've tested the changes that affect tpm2-cmd.c, tpm-interface.c and tpm_tis_core.c. For HW specific changes tested-by's would be much appreciated but I don't think they will break anything.

Because these are quite critical fixes I wonder if I could do one more PR to 4.16?

Quoted 6 lines

They're all cc'd to stable, so they'd make the stable updates to 4.16 regardless of when they're pulled. Since the merge window will close on Sunday and you have to go via James' tree, I'd say it would cause a lot of stress to try to make 4.16 but it's your call.

James

Agreed, thanks for input!

/Jarkko

#8 lore raw
Quoted 55 lines · Jarkko Sakkinen
Quoted 54 lines · James Bottomley
Quoted 41 lines · Jarkko Sakkinen
Quoted 23 lines · James Bottomley

If a TPM is attached to a system via a serial bus on a platform that suffers bit flips, we can get back dangerously wrong data. This patch series aims never to do a direct copy into a kernel buffer based on an unchecked size value returned from the TPM.

Jeremy Boone (5):
tpm: fix potential buffer overruns caused by bit glitches on the
bus
tpm: st33zp24: fix potential buffer overruns caused by bit
glitches on
the bus
tpm_i2c_infineon: fix potential buffer overruns caused by bit
glitches
on the bus
tpm_i2c_nuvoton: fix potential buffer overruns caused by bit
glitches
on the bus
tpm_tis: fix potential buffer overruns caused by bit glitches on
the
bus

Quoted 10 lines
 drivers/char/tpm/st33zp24/st33zp24.c | 4 ++--
 drivers/char/tpm/tpm-interface.c     | 4 ++++
 drivers/char/tpm/tpm2-cmd.c          | 4 ++++
 drivers/char/tpm/tpm_i2c_infineon.c  | 5 +++--
 drivers/char/tpm/tpm_i2c_nuvoton.c   | 5 +++--
 drivers/char/tpm/tpm_tis_core.c      | 5 +++--
 6 files changed, 19 insertions(+), 8 deletions(-)
Signature · 1 line
o 2.12.3

I cannot test all of these but I'll put these to linux-next anyway. The changes are obvious and small scoped so if no one shouts they'll be part of the next PR.

Quoted 6 lines

I've tested the changes that affect tpm2-cmd.c, tpm-interface.c and tpm_tis_core.c. For HW specific changes tested-by's would be much appreciated but I don't think they will break anything.

Because these are quite critical fixes I wonder if I could do one more PR to 4.16?

They're all cc'd to stable, so they'd make the stable updates to 4.16 regardless of when they're pulled. Since the merge window will close on Sunday and you have to go via James' tree, I'd say it would cause a lot of stress to try to make 4.16 but it's your call.

Quoted 5 lines
Quoted 1 line

James

Agreed, thanks for input!

/Jarkko

They are now in master/next.

/Jarkko