Skip to contentSkip to conversation

tpm: check size of response before accessing data

PATCH In review Stefan Berger 6 participants 8 messages Jan 5, 2017 – Jan 10, 2017 2 messages not in the archive TPM device driver

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tpm: check size of response before accessing data

Check the size of the response before accesing data in the response packet. This is to avoid accessing data beyond the end of the response.

---
 drivers/char/tpm/tpm2-cmd.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/char/tpm/tpm2-cmd.c +6 -0
index abaa355..98e591b 100644
@@ -394,6 +394,10 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
 	(sizeof(struct tpm_input_header) + \
 	 sizeof(struct tpm2_get_tpm_pt_in))
 
+#define TPM2_GET_TPM_PT_OUT_SIZE \
+	(sizeof(struct tpm_output_header) + \
+	 sizeof(struct tpm2_get_tpm_pt_out))
+
 static const struct tpm_input_header tpm2_get_tpm_pt_header = {
 	.tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
 	.length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
@@ -713,6 +717,8 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,  u32 *value,
 	cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
 
 	rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, desc);
+	if (be32_to_cpu(cmd.header.out.length) < TPM2_GET_TPM_PT_OUT_SIZE)
+		return -EFAULT;
 	if (!rc)
 		*value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
 
Signature · 6 lines
2.4.3


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
#2 lore raw

Re: [PATCH] tpm: check size of response before accessingdata

In reply to a message not in the archive

Stefan Berger <[email protected]> wrote on 01/05/2017 07:11:24 AM:

Quoted 4 lines

Check the size of the response before accesing data in the response packet. This is to avoid accessing data beyond the end of the response.

This patch applies on top of Jarkko's tabrm tree.
There are of course many more places where such checks should be done, if we agree that we want them to be done.

Stefan

Quoted 37 lines
---
 drivers/char/tpm/tpm2-cmd.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/char/tpm/tpm2-cmd.c +4 -0
index abaa355..98e591b 100644
@@ -394,6 +394,10 @@ int tpm2_get_random(struct tpm_chip *chip, u8 
 *out, size_t max)
    (sizeof(struct tpm_input_header) + \
     sizeof(struct tpm2_get_tpm_pt_in))
 
+#define TPM2_GET_TPM_PT_OUT_SIZE \
+   (sizeof(struct tpm_output_header) + \
+    sizeof(struct tpm2_get_tpm_pt_out))
+
 static const struct tpm_input_header tpm2_get_tpm_pt_header = {
    .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
    .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
drivers/char/tpm/tpm2-cmd.c +2 -0
@@ -713,6 +717,8 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, 
 u32 property_id,  u32 *value,
    cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
 
    rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, desc);
+   if (be32_to_cpu(cmd.header.out.length) < TPM2_GET_TPM_PT_OUT_SIZE)
+      return -EFAULT;
    if (!rc)
       *value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
Signature · 2 lines
-- 
2.4.3

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

Quoted 7 lines

Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
tpmdd-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/tpmdd-devel

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot

_______________________________________________
tpmdd-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/tpmdd-devel

#3 lore raw
In reply to a message not in the archive
Quoted 5 lines · Stefan Berger

Check the size of the response before accesing data in the response packet. This is to avoid accessing data beyond the end of the response.

How on earth this could happen if we request only one property?

/Jarkko

Quoted 31 lines
---
 drivers/char/tpm/tpm2-cmd.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/char/tpm/tpm2-cmd.c +6 -0
index abaa355..98e591b 100644
@@ -394,6 +394,10 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
 	(sizeof(struct tpm_input_header) + \
 	 sizeof(struct tpm2_get_tpm_pt_in))
 
+#define TPM2_GET_TPM_PT_OUT_SIZE \
+	(sizeof(struct tpm_output_header) + \
+	 sizeof(struct tpm2_get_tpm_pt_out))
+
 static const struct tpm_input_header tpm2_get_tpm_pt_header = {
 	.tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
 	.length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
@@ -713,6 +717,8 @@ ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id,  u32 *value,
 	cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
 
 	rc = tpm_transmit_cmd(chip, NULL, &cmd, sizeof(cmd), 0, desc);
+	if (be32_to_cpu(cmd.header.out.length) < TPM2_GET_TPM_PT_OUT_SIZE)
+		return -EFAULT;
 	if (!rc)
 		*value = be32_to_cpu(cmd.params.get_tpm_pt_out.value);
 
Signature · 1 line
2.4.3

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot

#5 lore raw
In reply to a message not in the archive
Quoted 8 lines · Jarkko Sakkinen
Quoted 5 lines · Stefan Berger

Check the size of the response before accesing data in the response packet. This is to avoid accessing data beyond the end of the response.

How on earth this could happen if we request only one property?

His (software) TPM is broken.

Now that we have the vtpm stuff it is super-critical that the kernel unmarshal path be bomb proof - it needs to treat the TPM itself as a hostile entity.

You should look at all of it and make sure the proper bounds checks are done, multiples can't overflow, and so forth.

Jason

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most engaging tech sites, SlashDot.org! http://sdm.link/slashdot

#6 lore raw
In reply to Jarkko Sakkinen
Quoted 7 lines · Jarkko Sakkinen
Quoted 5 lines · Stefan Berger

Check the size of the response before accesing data in the response packet. This is to avoid accessing data beyond the end of the response.

How on earth this could happen if we request only one property?

My test program vtpmctrl ( https://github.com/stefanberger/linux-vtpm-tests ) didn't feed the kernel a proper response to a TPM command and that's why this code blew up. We do have a very basic check in the driver and otherwise assume that the TPM is a trusted device responding with an expected response.

http://lxr.free-electrons.com/source/drivers/char/tpm/tpm-interface.c#L417

414 len = tpm_transmit(chip, (const u8 *)cmd, len, flags);
415 if (len < 0)
416 return len;
417 else if (len < TPM_HEADER_SIZE)
418 return -EFAULT;

Now should we expand on this check or just assume the device is flawless?

This particular check above should probably check whether the len is also what the len in the packet indicates.

Stefan

#7 lore raw
Quoted 6 lines · Stefan Berger
Quoted 5 lines · Jarkko Sakkinen
Quoted 4 lines · Stefan Berger

Check the size of the response before accesing data in the response packet. This is to avoid accessing data beyond the end of the response.

Quoted 8 lines
Quoted 2 lines
Quoted 1 line

How on earth this could happen if we request only one property?

My test program vtpmctrl ( https://github.com/stefanberger/linux-vtpm-tests
) didn't feed the kernel a proper response to a TPM command and that's why this code blew up. We do have a very basic check in the driver and otherwise assume that the TPM is a trusted device responding with an expected response.

Hmm.... I guess I could add this check but I'll have to probably do a similar check at least in one other place in this patch set where I grab the metadata for commands.

I guess similar issues will arise as the virtual TPMs get more common. For now I think a good guideline is

1. For new code check that validation for message size is in place.
2. Fix the old code as you bump into issus.

/Jarkko

#8 lore raw
Quoted 14 lines · Jarkko Sakkinen
Quoted 6 lines · Stefan Berger
Quoted 5 lines · Jarkko Sakkinen
Quoted 4 lines · Stefan Berger

Check the size of the response before accesing data in the response packet. This is to avoid accessing data beyond the end of the response.

Quoted 7 lines
Quoted 2 lines
Quoted 1 line

How on earth this could happen if we request only one property?

My test program vtpmctrl ( https://github.com/stefanberger/linux-vtpm-tests
) didn't feed the kernel a proper response to a TPM command and that's why this code blew up. We do have a very basic check in the driver and otherwise assume that the TPM is a trusted device responding with an expected response.

Quoted 8 lines

Hmm.... I guess I could add this check but I'll have to probably do a similar check at least in one other place in this patch set where I grab the metadata for commands.

I guess similar issues will arise as the virtual TPMs get more common. For now I think a good guideline is

1. For new code check that validation for message size is in place.

Before accessing data in the response, make sure we don't access beyond the number of bytes returned.

Quoted 1 line

2. Fix the old code as you bump into issus.

It doesn't look too bad. I would rebase my current patch on your master tree and submit a few small other ones with it. Agrred?

Stefan

Quoted 3 lines

/Jarkko

#9 lore raw
Quoted 24 lines · Stefan Berger
Quoted 14 lines · Jarkko Sakkinen
Quoted 6 lines · Stefan Berger
Quoted 5 lines · Jarkko Sakkinen
Quoted 4 lines · Stefan Berger

Check the size of the response before accesing data in the response packet. This is to avoid accessing data beyond the end of the response.

Quoted 7 lines
Quoted 2 lines
Quoted 1 line

How on earth this could happen if we request only one property?

My test program vtpmctrl ( https://github.com/stefanberger/linux-vtpm-tests
) didn't feed the kernel a proper response to a TPM command and that's why this code blew up. We do have a very basic check in the driver and otherwise assume that the TPM is a trusted device responding with an expected response.

Quoted 8 lines

Hmm.... I guess I could add this check but I'll have to probably do a similar check at least in one other place in this patch set where I grab the metadata for commands.

I guess similar issues will arise as the virtual TPMs get more common. For now I think a good guideline is

1. For new code check that validation for message size is in place.

Quoted 7 lines

Before accessing data in the response, make sure we don't access beyond the number of bytes returned.

Quoted 1 line

2. Fix the old code as you bump into issus.

It doesn't look too bad. I would rebase my current patch on your master tree and submit a few small other ones with it. Agrred?

Hmm. Are you talking about stuff you are adding to the tpm2-space.c. For me it is less trouble to add checks myself than applying 3rd party patches while preparing the next patch set version. This is only overhead for me and I will anyway would squash these checks to my own commits.

Quoted 3 lines

Stefan

/Jarkko

Quoted 4 lines
Quoted 3 lines

/Jarkko