Skip to contentSkip to conversation

tpm: add buffer access validation in tpm2_get_pcr_allocation()

PATCH In review Nayna Jain 2 participants 6 messages Jan 27, 2017 – Jan 30, 2017 TPM device driver

lore
Share & tools
lore

Nayna Jain

author
#0 lore raw

PATCH 1/1 tpm: add buffer access validation in tpm2_get_pcr_allocation()

This patch add validation in tpm2_get_pcr_allocation to avoid access beyond response buffer length.

---
 drivers/char/tpm/tpm2-cmd.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm2-cmd.c +23 -5
index 4aad84c..02c1ea7 100644
@@ -1008,9 +1008,13 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
 	struct tpm2_pcr_selection pcr_selection;
 	struct tpm_buf buf;
 	void *marker;
-	unsigned int count = 0;
+	void *end;
+	void *pcr_select_offset;
+	unsigned int count;
+	u32 sizeof_pcr_selection;
+	u32 resp_len;
 	int rc;
-	int i;
+	int i = 0;
 
 	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
 	if (rc)
@@ -1034,15 +1038,29 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
 	}
 
 	marker = &buf.data[TPM_HEADER_SIZE + 9];
+
+	resp_len = be32_to_cpup((__be32 *)&buf.data[2]);
+	end = &buf.data[resp_len];
+
 	for (i = 0; i < count; i++) {
+		pcr_select_offset = marker +
+			offsetof(struct tpm2_pcr_selection, size_of_select);
+		if (pcr_select_offset >= end) {
+			rc = -EFAULT;
+			break;
+		}
+
 		memcpy(&pcr_selection, marker, sizeof(pcr_selection));
 		chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
-		marker = marker + sizeof(struct tpm2_pcr_selection);
+		sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
+			sizeof(pcr_selection.size_of_select) +
+			sizeof(u8) * pcr_selection.size_of_select;
+		marker = marker + sizeof_pcr_selection;
 	}
 
 out:
-	if (count < ARRAY_SIZE(chip->active_banks))
-		chip->active_banks[count] = TPM2_ALG_ERROR;
+	if (i < ARRAY_SIZE(chip->active_banks))
+		chip->active_banks[i] = TPM2_ALG_ERROR;
 
 	tpm_buf_destroy(&buf);
 
Signature · 6 lines
2.5.0


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
#1 lore raw
Quoted 5 lines · Nayna Jain

This patch add validation in tpm2_get_pcr_allocation to avoid access beyond response buffer length.

This validation looks broken to me.

Quoted 9 lines
---
 drivers/char/tpm/tpm2-cmd.c | 28 +++++++++++++++++++++++-----
 1 file changed, 23 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm2-cmd.c +0 -0
index 4aad84c..02c1ea7 100644

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1015

Quoted 9 lines
drivers/char/tpm/tpm2-cmd.c +5 -1
@@ -1008,9 +1008,13 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
 	struct tpm2_pcr_selection pcr_selection;
 	struct tpm_buf buf;
 	void *marker;
-	unsigned int count = 0;
+	void *end;
+	void *pcr_select_offset;
+	unsigned int count;
+	u32 sizeof_pcr_selection;
+	u32 resp_len;

Very cosmetic but we almos almost universally use the acronym 'rsp' in the TPM driver.

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1017

Quoted 3 lines
drivers/char/tpm/tpm2-cmd.c +1 -1
 	int rc;
-	int i;
+	int i = 0;

Why do you need to initialize it?

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1043

Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +3 -0
 	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
 	if (rc)
@@ -1034,15 +1038,29 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
 	}
 
 	marker = &buf.data[TPM_HEADER_SIZE + 9];
+
+	resp_len = be32_to_cpup((__be32 *)&buf.data[2]);
+	end = &buf.data[resp_len];

What if the response contains larger length than the buffer size?

Quoted 5 lines
drivers/char/tpm/tpm2-cmd.c +4 -0
+
 	for (i = 0; i < count; i++) {
+		pcr_select_offset = marker +
+			offsetof(struct tpm2_pcr_selection, size_of_select);
+		if (pcr_select_offset >= end) {

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1057

Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +7 -1
+			rc = -EFAULT;
+			break;
+		}
+
 		memcpy(&pcr_selection, marker, sizeof(pcr_selection));
 		chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
-		marker = marker + sizeof(struct tpm2_pcr_selection);
+		sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
+			sizeof(pcr_selection.size_of_select) +
+			sizeof(u8) * pcr_selection.size_of_select;

Remove "sizeof(u8) * ".

Quoted 14 lines
drivers/char/tpm/tpm2-cmd.c +3 -2
+		marker = marker + sizeof_pcr_selection;
 	}
 
 out:
-	if (count < ARRAY_SIZE(chip->active_banks))
-		chip->active_banks[count] = TPM2_ALG_ERROR;
+	if (i < ARRAY_SIZE(chip->active_banks))
+		chip->active_banks[i] = TPM2_ALG_ERROR;
 
 	tpm_buf_destroy(&buf);
 
Signature · 1 line
2.5.0

I'm sorry but this commit is changing too much. You need to redo the whole commit and resend the patch set with these fixes. You can keep Reviewed-by and Tested-by in 1/2 but have to remove them from 2/2.

/Jarkko

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

author
#2 lore raw
Quoted 22 lines · Jarkko Sakkinen
Quoted 5 lines · Nayna Jain

This patch add validation in tpm2_get_pcr_allocation to avoid access beyond response buffer length.

This validation looks broken to me.

Quoted 13 lines
---
  drivers/char/tpm/tpm2-cmd.c | 28 +++++++++++++++++++++++-----
  1 file changed, 23 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm2-cmd.c +0 -1
index 4aad84c..02c1ea7 100644
@@ -1008,9 +1008,13 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
  	struct tpm2_pcr_selection pcr_selection;
  	struct tpm_buf buf;
  	void *marker;
-	unsigned int count = 0;
Quoted 8 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1015

Quoted 5 lines
drivers/char/tpm/tpm2-cmd.c +5 -0
+	void *end;
+	void *pcr_select_offset;
+	unsigned int count;
+	u32 sizeof_pcr_selection;
+	u32 resp_len;

Very cosmetic but we almos almost universally use the acronym 'rsp' in the TPM driver.

Sure will update.

Quoted 6 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1017

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +1 -1
  	int rc;
-	int i;
+	int i = 0;

Why do you need to initialize it?

Because in out: count is replaced with i.
And it is replaced because now for loop can break even before reaching count, because of new buffer checks.

Quoted 7 lines
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +0 -0
  	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  	if (rc)
@@ -1034,15 +1038,29 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
  	}
 
Quoted 6 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1043

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +3 -0
  	marker = &buf.data[TPM_HEADER_SIZE + 9];
+
+	resp_len = be32_to_cpup((__be32 *)&buf.data[2]);
+	end = &buf.data[resp_len];

What if the response contains larger length than the buffer size?

Isn't this check need to be done in tpm_transmit_cmd for all responses ? Though, it seems it is not done there as well.

And to understand what do we expect max buffer length. PAGE_SIZE or TPM_BUFSIZE ?

Quoted 10 lines
Quoted 10 lines
drivers/char/tpm/tpm2-cmd.c +8 -0
+
  	for (i = 0; i < count; i++) {
+		pcr_select_offset = marker +
+			offsetof(struct tpm2_pcr_selection, size_of_select);
+		if (pcr_select_offset >= end) {
+			rc = -EFAULT;
+			break;
+		}
+
Quoted 8 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1057

Quoted 6 lines
drivers/char/tpm/tpm2-cmd.c +3 -1
  		memcpy(&pcr_selection, marker, sizeof(pcr_selection));
  		chip->active_banks[i] = be16_to_cpu(pcr_selection.hash_alg);
-		marker = marker + sizeof(struct tpm2_pcr_selection);
+		sizeof_pcr_selection = sizeof(pcr_selection.hash_alg) +
+			sizeof(pcr_selection.size_of_select) +
+			sizeof(u8) * pcr_selection.size_of_select;

Remove "sizeof(u8) * ".

Sure.

Quoted 12 lines
Quoted 12 lines
drivers/char/tpm/tpm2-cmd.c +3 -2
+		marker = marker + sizeof_pcr_selection;
  	}
 
  out:
-	if (count < ARRAY_SIZE(chip->active_banks))
-		chip->active_banks[count] = TPM2_ALG_ERROR;
+	if (i < ARRAY_SIZE(chip->active_banks))
+		chip->active_banks[i] = TPM2_ALG_ERROR;
 
  	tpm_buf_destroy(&buf);
 
Quoted 7 lines
Quoted 3 lines

--
2.5.0

I'm sorry but this commit is changing too much. You need to redo the whole commit and resend the patch set with these fixes. You can keep Reviewed-by and Tested-by in 1/2 but have to remove them from 2/2.

Sure, will do.

Thanks & Regards,
- Nayna

Quoted 3 lines

/Jarkko

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

#3 lore raw
Quoted 60 lines · Nayna
Quoted 22 lines · Jarkko Sakkinen
Quoted 5 lines · Nayna Jain

This patch add validation in tpm2_get_pcr_allocation to avoid access beyond response buffer length.

This validation looks broken to me.

Quoted 13 lines
---
  drivers/char/tpm/tpm2-cmd.c | 28 +++++++++++++++++++++++-----
  1 file changed, 23 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm2-cmd.c +0 -1
index 4aad84c..02c1ea7 100644
@@ -1008,9 +1008,13 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
  	struct tpm2_pcr_selection pcr_selection;
  	struct tpm_buf buf;
  	void *marker;
-	unsigned int count = 0;
Quoted 8 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1015

Quoted 5 lines
drivers/char/tpm/tpm2-cmd.c +5 -0
+	void *end;
+	void *pcr_select_offset;
+	unsigned int count;
+	u32 sizeof_pcr_selection;
+	u32 resp_len;

Very cosmetic but we almos almost universally use the acronym 'rsp' in the TPM driver.

Sure will update.

Quoted 6 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1017

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +1 -1
  	int rc;
-	int i;
+	int i = 0;

Why do you need to initialize it?

Because in out: count is replaced with i.
And it is replaced because now for loop can break even before reaching count, because of new buffer checks.

Quoted 7 lines
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +0 -0
  	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
  	if (rc)
@@ -1034,15 +1038,29 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
  	}
 
Quoted 6 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1043

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +3 -0
  	marker = &buf.data[TPM_HEADER_SIZE + 9];
+
+	resp_len = be32_to_cpup((__be32 *)&buf.data[2]);
+	end = &buf.data[resp_len];

What if the response contains larger length than the buffer size?

Quoted 5 lines

Isn't this check need to be done in tpm_transmit_cmd for all responses ? Though, it seems it is not done there as well.

And to understand what do we expect max buffer length. PAGE_SIZE or TPM_BUFSIZE ?

Oops. You are correct it is done there:

if (len != be32_to_cpu(header->length))

	return -EFAULT;

So need to do this.

/Jarkko

/Jarkko

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

author
#4 lore raw
Quoted 67 lines · Jarkko Sakkinen
Quoted 60 lines · Nayna
Quoted 22 lines · Jarkko Sakkinen
Quoted 5 lines · Nayna Jain

This patch add validation in tpm2_get_pcr_allocation to avoid access beyond response buffer length.

This validation looks broken to me.

Quoted 13 lines
---
   drivers/char/tpm/tpm2-cmd.c | 28 +++++++++++++++++++++++-----
   1 file changed, 23 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm2-cmd.c +0 -1
index 4aad84c..02c1ea7 100644
@@ -1008,9 +1008,13 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
   	struct tpm2_pcr_selection pcr_selection;
   	struct tpm_buf buf;
   	void *marker;
-	unsigned int count = 0;
Quoted 8 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1015

Quoted 5 lines
drivers/char/tpm/tpm2-cmd.c +5 -0
+	void *end;
+	void *pcr_select_offset;
+	unsigned int count;
+	u32 sizeof_pcr_selection;
+	u32 resp_len;

Very cosmetic but we almos almost universally use the acronym 'rsp' in the TPM driver.

Sure will update.

Quoted 6 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1017

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +1 -1
   	int rc;
-	int i;
+	int i = 0;

Why do you need to initialize it?

Because in out: count is replaced with i.
And it is replaced because now for loop can break even before reaching count, because of new buffer checks.

Quoted 7 lines
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +0 -0
   	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
   	if (rc)
@@ -1034,15 +1038,29 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
   	}
 
Quoted 6 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1043

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +3 -0
   	marker = &buf.data[TPM_HEADER_SIZE + 9];
+
+	resp_len = be32_to_cpup((__be32 *)&buf.data[2]);
+	end = &buf.data[resp_len];

What if the response contains larger length than the buffer size?

Quoted 5 lines

Isn't this check need to be done in tpm_transmit_cmd for all responses ? Though, it seems it is not done there as well.

And to understand what do we expect max buffer length. PAGE_SIZE or TPM_BUFSIZE ?

Quoted 6 lines

Oops. You are correct it is done there:

if (len != be32_to_cpu(header->length))

	return -EFAULT;

So need to do this.

To be sure, means nothing need to be done in this. Right ?

And guess this was the only thing you meant by broken for this patch.

I will do other two smaller changes as I send the whole new patchset.

Thanks & Regards,
- Nayna

Quoted 5 lines

/Jarkko

/Jarkko

#5 lore raw
Quoted 70 lines · Nayna
Quoted 67 lines · Jarkko Sakkinen
Quoted 65 lines · Nayna
Quoted 22 lines · Jarkko Sakkinen
Quoted 5 lines · Nayna Jain

This patch add validation in tpm2_get_pcr_allocation to avoid access beyond response buffer length.

This validation looks broken to me.

Quoted 13 lines
---
   drivers/char/tpm/tpm2-cmd.c | 28 +++++++++++++++++++++++-----
   1 file changed, 23 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm2-cmd.c +0 -1
index 4aad84c..02c1ea7 100644
@@ -1008,9 +1008,13 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
   	struct tpm2_pcr_selection pcr_selection;
   	struct tpm_buf buf;
   	void *marker;
-	unsigned int count = 0;
Quoted 8 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1015

Quoted 5 lines
drivers/char/tpm/tpm2-cmd.c +5 -0
+	void *end;
+	void *pcr_select_offset;
+	unsigned int count;
+	u32 sizeof_pcr_selection;
+	u32 resp_len;

Very cosmetic but we almos almost universally use the acronym 'rsp' in the TPM driver.

Sure will update.

Quoted 6 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1017

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +1 -1
   	int rc;
-	int i;
+	int i = 0;

Why do you need to initialize it?

Because in out: count is replaced with i.
And it is replaced because now for loop can break even before reaching count, because of new buffer checks.

Quoted 7 lines
Quoted 7 lines
drivers/char/tpm/tpm2-cmd.c +0 -0
   	rc = tpm_buf_init(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_CAPABILITY);
   	if (rc)
@@ -1034,15 +1038,29 @@ static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip)
   	}
 
Quoted 6 lines

drivers/char/tpm/tpm2-cmd.c static ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) · line 1043

Quoted 4 lines
drivers/char/tpm/tpm2-cmd.c +3 -0
   	marker = &buf.data[TPM_HEADER_SIZE + 9];
+
+	resp_len = be32_to_cpup((__be32 *)&buf.data[2]);
+	end = &buf.data[resp_len];

What if the response contains larger length than the buffer size?

Isn't this check need to be done in tpm_transmit_cmd for all responses ? Though, it seems it is not done there as well.

And to understand what do we expect max buffer length. PAGE_SIZE or TPM_BUFSIZE ?

Quoted 8 lines
Quoted 6 lines

Oops. You are correct it is done there:

if (len != be32_to_cpu(header->length))

	return -EFAULT;

So need to do this.

To be sure, means nothing need to be done in this. Right ?

This is correct.

Quoted 6 lines

And guess this was the only thing you meant by broken for this patch.

I will do other two smaller changes as I send the whole new patchset.

Thanks & Regards,
- Nayna

/Jarkko