Fix kernel buffer overruns caused by bit flips
Subsystems (2)
James Bottomley
maintainercover 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 (2):
tpm: fix potential buffer overruns caused by bit glitches on the bus tpm drivers: fix potential buffer overruns caused by bit glitches on
the bus
drivers/char/tpm/st33zp24/st33zp24.c | 4 ++-- drivers/char/tpm/tpm-interface.c | 1 + 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, 16 insertions(+), 8 deletions(-)
Signature · 1 line
2.12.3
James Bottomley
maintainerPATCH 1/2 tpm: 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. If a bit does flip it could cause an overrun if it's in one of the size parameters, so sanity check that we're not overrunning the provided buffer when doing a memcpy().
--- drivers/char/tpm/tpm-interface.c | 1 + drivers/char/tpm/tpm2-cmd.c | 4 ++++ 2 files changed, 5 insertions(+)
drivers/char/tpm/tpm-interface.c +1 -0
@@ -1228,6 +1228,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)break ; recd =be32_to_cpu (tpm_cmd.params.getrandom_out.rng_data_len); + recd =min_t (u32, recd, num_bytes); rlength =be32_to_cpu (tpm_cmd.header.out.length); if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +
drivers/char/tpm/tpm2-cmd.c +4 -0
@@ -683,6 +683,10 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,if (!rc) { data_len =be16_to_cpup ( (__be16 *) &buf.data[TPM_HEADER_SIZE +4 ]); +if (data_len <MIN_KEY_SIZE || data_len >MAX_KEY_SIZE +1 ) { + rc = -EFAULT ; + goto out; + } rlength =be32_to_cpu (((struct tpm2_cmd *)&buf) ->header.out.length);
Signature · 1 line
2.12.3
James Bottomley
maintainerPATCH 2/2 tpm drivers: fix potential buffer overruns caused by bit glitches on the bus
In reply to James BottomleyFrom: 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 ++-- drivers/char/tpm/tpm_i2c_infineon.c | 5 +++-- drivers/char/tpm/tpm_i2c_nuvoton.c | 5 +++-- drivers/char/tpm/tpm_tis_core.c | 5 +++-- 4 files changed, 11 insertions(+), 8 deletions(-)
drivers/char/tpm/st33zp24/st33zp24.c +2 -2
@@ -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; }
drivers/char/tpm/tpm_i2c_infineon.c +3 -2
@@ -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; }
drivers/char/tpm/tpm_i2c_nuvoton.c +3 -2
@@ -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 ;
drivers/char/tpm/tpm_tis_core.c +3 -2
@@ -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
Quoted 9 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 (2):
tpm: fix potential buffer overruns caused by bit glitches on the bus tpm drivers: fix potential buffer overruns caused by bit glitches on
the bus
Jason
Re: [PATCH 1/2] tpm: fix potential buffer overruns caused by bit glitches on the bus
In reply to James BottomleyQuoted 8 lines · 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. If a bit does flip it could cause an overrun if it's in one of the size parameters, so sanity check that we're not overrunning the provided buffer when doing a
memcpy().
Quoted 7 lines
Signed-off-by Jeremy Boone <[email protected]>Signed-off-by James Bottomley <[email protected]>--- drivers/char/tpm/tpm-interface.c | 1 + drivers/char/tpm/tpm2-cmd.c | 4 ++++ 2 files changed, 5 insertions(+)
Please add me to to-field in the future. I'm also wondering where is the cover letter.
drivers/char/tpm/tpm-interface.c int tpm_get_random(u32 chip_num, u8 *out, size_t max) · line 1231
Quoted 10 lines
drivers/char/tpm/tpm-interface.c +1
-0@@ -1228,6 +1228,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)break ; recd =be32_to_cpu (tpm_cmd.params.getrandom_out.rng_data_len); + recd =min_t (u32, recd, num_bytes);
Shouldn't this be rather a check whether num_bytes is surpassed and return an error if that happens and maybe a klog message?
Quoted 7 lines
drivers/char/tpm/tpm-interface.c +0
-0rlength =be32_to_cpu (tpm_cmd.header.out.length); if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +drivers/char/tpm/tpm2-cmd.c +0
-0
drivers/char/tpm/tpm2-cmd.c static int tpm2_unseal_cmd(struct tpm_chip *chip, · line 689
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +4
-0@@ -683,6 +683,10 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,if (!rc) { data_len =be16_to_cpup ( (__be16 *) &buf.data[TPM_HEADER_SIZE +4 ]); +if (data_len <MIN_KEY_SIZE || data_len >MAX_KEY_SIZE +1 ) { + rc = -EFAULT ; + goto out; + }
This change looks good to me but I'm thinking if this commit should split into two?
/Jarkko
Re: [PATCH 2/2] tpm drivers: fix potential buffer overruns caused by bit glitches on the bus
In reply to James BottomleyQuoted 113 lines · 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.Signed-off-by Jeremy Boone <[email protected]>Signed-off-by James Bottomley <[email protected]>--- drivers/char/tpm/st33zp24/st33zp24.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 +++-- 4 files changed, 11 insertions(+), 8 deletions(-)drivers/char/tpm/st33zp24/st33zp24.c +2
-2@@ -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; }drivers/char/tpm/tpm_i2c_infineon.c +3
-2@@ -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; }drivers/char/tpm/tpm_i2c_nuvoton.c +3
-2@@ -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 ;drivers/char/tpm/tpm_tis_core.c +3
-2@@ -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
LGTM but should be split into four commits.
/Jarkko
RE: EXTERNAL: Re: [PATCH 1/2] tpm: fix potential buffer overruns caused by bit glitches on the bus
In reply to Jarkko SakkinenQuoted 2 lines
Shouldn't this be rather a check whether
num_bytesis surpassed and return an error if that happens and maybe a klog message?
Jarkko,
I had originally suggested the use of min_t() in an attempt to make this code symmetric with the behaviour seen in tpm2_get_random() in tpm2-cmd.c. But I see the value in your suggestion.
Jeremy
-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Jarkko Sakkinen
Sent: Thursday, February 08, 2018 8:34 AM
To: James Bottomley <[email protected]>
Cc: [email protected]; Jeremy Boone <[email protected]>
Subject: EXTERNAL: Re: [PATCH 1/2] tpm: fix potential buffer overruns caused by bit glitches on the bus
Quoted 8 lines · 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. If a bit does flip it could cause an overrun if it's in one of the size parameters, so sanity check that we're not overrunning the provided buffer when doing a
memcpy().
Quoted 7 lines
Signed-off-by Jeremy Boone <[email protected]>Signed-off-by James Bottomley <[email protected]>--- drivers/char/tpm/tpm-interface.c | 1 + drivers/char/tpm/tpm2-cmd.c | 4 ++++ 2 files changed, 5 insertions(+)
Please add me to to-field in the future. I'm also wondering where is the cover letter.
drivers/char/tpm/tpm-interface.c int tpm_get_random(u32 chip_num, u8 *out, size_t max) · line 1231
Quoted 10 lines
drivers/char/tpm/tpm-interface.c +1
-0@@ -1228,6 +1228,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max)break ; recd =be32_to_cpu (tpm_cmd.params.getrandom_out.rng_data_len); +recd =min_t (u32, recd, num_bytes);
Shouldn't this be rather a check whether num_bytes is surpassed and return an error if that happens and maybe a klog message?
Quoted 7 lines
drivers/char/tpm/tpm-interface.c +0
-0rlength =be32_to_cpu (tpm_cmd.header.out.length); if (rlength < offsetof(struct tpm_getrandom_out, rng_data) +drivers/char/tpm/tpm2-cmd.c +0
-0
drivers/char/tpm/tpm2-cmd.c static int tpm2_unseal_cmd(struct tpm_chip *chip, · line 689
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +4
-0@@ -683,6 +683,10 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,if (!rc) { data_len =be16_to_cpup ( (__be16 *) &buf.data[TPM_HEADER_SIZE +4 ]); +if (data_len <MIN_KEY_SIZE || data_len >MAX_KEY_SIZE +1 ) { +rc = -EFAULT ; +goto out; +}
This change looks good to me but I'm thinking if this commit should split into two?
/Jarkko
________________________________
Jeremy Boone
Principal Security Consultant
NCC Group
51 Breithaupt Street,
Suite 100, Kitchener, N2H 5G5
Telephone: +1 226 606 8318<tel:+1 226 606 8318>
Mobile: +1 226 606 8318<tel:+1 226 606 8318>
Website: www.nccgroup.trust<http://www.nccgroup.trust>
Twitter: @NCCGroupplc<https://twitter.com/NCCGroupplc>
[https://www.nccgroup.trust/static/img/emaillogo/ncc-group-logo.png] <http://www.nccgroup.trust/>
________________________________
This email is sent for and on behalf of NCC Group. NCC Group is the trading name of NCC Services Limited (Registered in England CRN: 2802141). The ultimate holding company is NCC Group plc (Registered in England CRN: 4627044). This email may be confidential and/or legally privileged.
James Bottomley
maintainerRe: [PATCH 1/2] tpm: fix potential buffer overruns caused by bit glitches on the bus
In reply to Jarkko SakkinenQuoted 20 lines · Jarkko Sakkinen
Quoted 10 lines · 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. If a bit does flip it could cause an overrun if it's in one of the size parameters,
so sanity check that we're not overrunning the provided buffer when doing amemcpy().Quoted 8 lines
Signed-off-by Jeremy Boone <[email protected]>Signed-off-by James Bottomley <[email protected]om>
--- drivers/char/tpm/tpm-interface.c | 1 + drivers/char/tpm/tpm2-cmd.c | 4 ++++ 2 files changed, 5 insertions(+)
Quoted 1 line
Please add me to to-field in the future.
Will do. I tend to assume everyone uses my workflow which means I junk the additional cc to me since I'll read it on the list anyway.
Quoted 1 line
I'm also wondering where is the cover letter.
https://marc.info/?l=linux-integrity&m=151759223601566
Quoted 9 lines
Quoted 9 lines
drivers/char/tpm/tpm-interface.c +0
-0@@ -1228,6 +1228,7 @@ int tpm_get_random(u32 chip_num, u8 *out, size_t max) break;
Quoted 6 lines
drivers/char/tpm/tpm-interface.c int tpm_get_random(u32 chip_num, u8 *out, · line 1233
Quoted 3 lines
drivers/char/tpm/tpm-interface.c +1
-0recd =be32_to_cpu (tpm_cmd.params.getrandom_out.rng_data_len); + recd =min_t (u32, recd, num_bytes);Shouldn't this be rather a check whether
num_bytesis surpassed and return an error if that happens and maybe a klog message?
I can do that. The aim of the patch series is to make sure we don't overrun buffers and the min achieves that. A message might help, but there are still many forms of corruption we could get that won't be detected without using HMACs.
Quoted 15 lines
Quoted 15 lines
drivers/char/tpm/tpm-interface.c +0
-0rlength =be32_to_cpu (tpm_cmd.header.out.length);if (rlength < offsetof(struct
tpm_getrandom_out,rng_data) +drivers/char/tpm/tpm2- renamed from drivers/char/tpm/tpm2-cmd.c +0
-0
cmd.c
indexf40d20671a78..f6be08483ae6100644drivers/char/tpm/tpm2-cmd.c +0
-0@@ -683,6 +683,10 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, if (!rc) { data_len =be16_to_cpup ( (__be16 *) &buf.data[TPM_HEADER_SIZE +4 ]);
Quoted 8 lines
drivers/char/tpm/tpm2-cmd.c static int tpm2_unseal_cmd(struct tpm_chip *chip, · line 689
Quoted 5 lines
drivers/char/tpm/tpm2-cmd.c +4
-0+if (data_len <MIN_KEY_SIZE || data_len >MAX_KEY_SIZE +1 ) { + rc = -EFAULT ; + goto out; + }This change looks good to me but I'm thinking if this commit should split into two?
You mean one for each driver? I can do that.
James
Quoted 2 lines
/Jarkko
Re: [PATCH 1/2] tpm: fix potential buffer overruns caused by bit glitches on the bus
Quoted 4 lines · James Bottomley
Quoted 1 line
Please add me to to-field in the future.
Will do. I tend to assume everyone uses my workflow which means I junk the additional cc to me since I'll read it on the list anyway.
It's just that if there's lot of stuff going on those messages I'll prioritize. Patches will get reviewed in all cases but there might be more latency :-)
/Jarkko