Skip to contentSkip to conversation

tpm: sessions: bound the primary public area by the response size

PATCH Superseded Guo Zihao 2 participants 8 messages Nov 6 – Nov 9

Subsystems (3)
lore
Share & tools
lore

Guo Zihao

author
#0 lore raw

PATCH 1/2 tpm: sessions: bound the primary public area by the response size

tpm2_parse_create_primary() reads the size of the TPM2B_PUBLIC area from the response and hashes it to compute the object name:

	len = tpm_buf_read_u16(buf, &offset_r);
	offset_t = offset_r;
	if (name) {
		/*
		 * now we have the public area, compute the name of
		 * the object
		 */
		put_unaligned_be16(TPM_ALG_SHA256, name);
		sha256(&buf->data[offset_r], len, name + 2);
	}

The length is only checked against the response later, after the hash has been taken. A TPM that reports a public area longer than what it actually sent makes the sha256() run past the end of the received data and hash whatever follows the response in the buffer.

Check that the public area fits in the response before hashing it.

Fixes d2add27cf2b8 ("tpm: Add NULL primary creation")
---
 drivers/char/tpm/tpm2-sessions.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/char/tpm/tpm2-sessions.c +2 -0
index ca1e2bf42..3524c1d50 100644
@@ -1126,6 +1126,8 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 		return -EINVAL;
 	len = tpm_buf_read_u16(buf, &offset_r);
 	offset_t = offset_r;
+	if (offset_r + len > tpm_buf_length(buf))
+		return -EINVAL;
 	if (name) {
 		/*
 		 * now we have the public area, compute the name of
Signature · 1 line
2.50.1
author
#1 lore raw

PATCH 2/2 tpm: sessions: bound the parameter decryption by the response size

tpm_buf_check_hmac_response() decrypts the parameter area of a response for sessions with TPM2_SA_ENCRYPT set:

	len = tpm_buf_read_u16(buf, &offset_p);
	aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
	aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
		       &buf->data[offset_p], len,
		       auth->scratch + AES_KEY_BYTES);

The length is read from the response itself and never checked against the buffer: every other size taken from a session response is compared to tpm_buf_length(), but not this one. A TPM that reports an oversized parameter area makes aescfb_decrypt() walk past the end of the buffer, and since the decryption is done in place, this both reads and writes whatever follows the received data.

Check that the encrypted parameters fit in the response before decrypting them.

Fixes 1085b8276bb4 ("tpm: Add the rest of the session HMAC API")
---
 drivers/char/tpm/tpm2-sessions.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/char/tpm/tpm2-sessions.c +4 -0
index 3524c1d50..6245a6baa 100644
@@ -872,6 +872,10 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 			  auth->scratch);
 
 		len = tpm_buf_read_u16(buf, &offset_p);
+		if (offset_p + len > tpm_buf_length(buf)) {
+			rc = -EINVAL;
+			goto out;
+		}
 		aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
 		aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
 			       &buf->data[offset_p], len,
Signature · 1 line
2.50.1
maintainer
#2 lore raw

Re: [PATCH 2/2] tpm: sessions: bound the parameter decryption by the response size

Quoted 17 lines · Guo Zihao

tpm_buf_check_hmac_response() decrypts the parameter area of a response for sessions with TPM2_SA_ENCRYPT set:

	len = tpm_buf_read_u16(buf, &offset_p);
	aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
	aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
		       &buf->data[offset_p], len,
		       auth->scratch + AES_KEY_BYTES);

The length is read from the response itself and never checked against the buffer: every other size taken from a session response is compared to tpm_buf_length(), but not this one. A TPM that reports an oversized parameter area makes aescfb_decrypt() walk past the end of the buffer, and since the decryption is done in place, this both reads and writes whatever follows the received data.

Quoted 6 lines

Check that the encrypted parameters fit in the response before decrypting them.

Fixes 1085b8276bb4 ("tpm: Add the rest of the session HMAC API")

Ditto.

Br, Jarkko

maintainer
#3 lore raw
In reply to Guo Zihao
Quoted 19 lines · Guo Zihao

tpm2_parse_create_primary() reads the size of the TPM2B_PUBLIC area from the response and hashes it to compute the object name:

	len = tpm_buf_read_u16(buf, &offset_r);
	offset_t = offset_r;
	if (name) {
		/*
		 * now we have the public area, compute the name of
		 * the object
		 */
		put_unaligned_be16(TPM_ALG_SHA256, name);
		sha256(&buf->data[offset_r], len, name + 2);
	}

The length is only checked against the response later, after the hash has been taken. A TPM that reports a public area longer than what it actually sent makes the sha256() run past the end of the received data and hash whatever follows the response in the buffer.

Quoted 5 lines

Check that the public area fits in the response before hashing it.

Fixes d2add27cf2b8 ("tpm: Add NULL primary creation")

I think it is not a bug fix without a source.

I.e., unless this is TCG defined behavior, I'd not add fixes tag.

Br, Jarkko

author
#4 lore raw

PATCH v2 1/2 tpm: sessions: bound the primary public area by the response size

tpm2_parse_create_primary() reads the size of the TPM2B_PUBLIC area from the response and hashes it to compute the object name:

	len = tpm_buf_read_u16(buf, &offset_r);
	offset_t = offset_r;
	if (name) {
		/*
		 * now we have the public area, compute the name of
		 * the object
		 */
		put_unaligned_be16(TPM_ALG_SHA256, name);
		sha256(&buf->data[offset_r], len, name + 2);
	}

The length is only checked against the response later, after the hash has been taken. A TPM that reports a public area longer than what it actually sent makes the sha256() run past the end of the received data and hash whatever follows the response in the buffer.

Check that the public area fits in the response before hashing it.

---

v2: Drop the Fixes and Cc: stable tags, as suggested by Jarkko
Sakkinen: a TPM reporting an oversized public area is not known to violate the TCG specification, so this is hardening rather than a bug fix. No code changes.

 drivers/char/tpm/tpm2-sessions.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/char/tpm/tpm2-sessions.c +2 -0
index cf8f1fd6790b..e140079dbc13 100644
@@ -1125,6 +1125,8 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 		return -EINVAL;
 	len = tpm_buf_read_u16(buf, &offset_r);
 	offset_t = offset_r;
+	if (offset_r + len > tpm_buf_length(buf))
+		return -EINVAL;
 	if (name) {
 		/*
 		 * now we have the public area, compute the name of
Signature · 1 line
2.50.1
author
#5 lore raw

PATCH v2 2/2 tpm: sessions: bound the parameter decryption by the response size

tpm_buf_check_hmac_response() decrypts the parameter area of a response for sessions with TPM2_SA_ENCRYPT set:

	len = tpm_buf_read_u16(buf, &offset_p);
	aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
	aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
		       &buf->data[offset_p], len,
		       auth->scratch + AES_KEY_BYTES);

The length is read from the response itself and never checked against the buffer: every other size taken from a session response is compared to tpm_buf_length(), but not this one. A TPM that reports an oversized parameter area makes aescfb_decrypt() walk past the end of the buffer, and since the decryption is done in place, this both reads and writes whatever follows the received data.

Check that the encrypted parameters fit in the response before decrypting them.

---

v2: Drop the Fixes and Cc: stable tags, as suggested by Jarkko
Sakkinen: a TPM reporting an oversized parameter area is not known to violate the TCG specification, so this is hardening rather than a bug fix. No code changes.

 drivers/char/tpm/tpm2-sessions.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/char/tpm/tpm2-sessions.c +4 -0
index e140079dbc13..59c7fdc0c88e 100644
@@ -872,6 +872,10 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 			  auth->scratch);
 
 		len = tpm_buf_read_u16(buf, &offset_p);
+		if (offset_p + len > tpm_buf_length(buf)) {
+			rc = -EINVAL;
+			goto out;
+		}
 		aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
 		aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
 			       &buf->data[offset_p], len,
Signature · 1 line
2.50.1
maintainer Reviewed-by on v2 2/2
#6 lore raw

Re: [PATCH v2 2/2] tpm: sessions: bound the parameter decryption by the response size

Quoted 49 lines · Guo Zihao

tpm_buf_check_hmac_response() decrypts the parameter area of a response for sessions with TPM2_SA_ENCRYPT set:

	len = tpm_buf_read_u16(buf, &offset_p);
	aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
	aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
		       &buf->data[offset_p], len,
		       auth->scratch + AES_KEY_BYTES);

The length is read from the response itself and never checked against the buffer: every other size taken from a session response is compared to tpm_buf_length(), but not this one. A TPM that reports an oversized parameter area makes aescfb_decrypt() walk past the end of the buffer, and since the decryption is done in place, this both reads and writes whatever follows the received data.

Check that the encrypted parameters fit in the response before decrypting them.

---

v2: Drop the Fixes and Cc: stable tags, as suggested by Jarkko
Sakkinen: a TPM reporting an oversized parameter area is not known to violate the TCG specification, so this is hardening rather than a bug fix. No code changes.

 drivers/char/tpm/tpm2-sessions.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/char/tpm/tpm2-sessions.c +4 -0
index e140079dbc13..59c7fdc0c88e 100644
@@ -872,6 +872,10 @@ int tpm_buf_check_hmac_response(struct tpm_chip *chip, struct tpm_buf *buf,
 			  auth->scratch);
 
 		len = tpm_buf_read_u16(buf, &offset_p);
+		if (offset_p + len > tpm_buf_length(buf)) {
+			rc = -EINVAL;
+			goto out;
+		}
 		aes_prepareenckey(&auth->aes_key, auth->scratch, AES_KEY_BYTES);
 		aescfb_decrypt(&auth->aes_key, &buf->data[offset_p],
 			       &buf->data[offset_p], len,
Signature · 1 line
2.50.1

Br, Jarkko

maintainer Reviewed-by on v2 1/2
#7 lore raw
In reply to Guo Zihao
Quoted 48 lines · Guo Zihao

tpm2_parse_create_primary() reads the size of the TPM2B_PUBLIC area from the response and hashes it to compute the object name:

	len = tpm_buf_read_u16(buf, &offset_r);
	offset_t = offset_r;
	if (name) {
		/*
		 * now we have the public area, compute the name of
		 * the object
		 */
		put_unaligned_be16(TPM_ALG_SHA256, name);
		sha256(&buf->data[offset_r], len, name + 2);
	}

The length is only checked against the response later, after the hash has been taken. A TPM that reports a public area longer than what it actually sent makes the sha256() run past the end of the received data and hash whatever follows the response in the buffer.

Check that the public area fits in the response before hashing it.

---

v2: Drop the Fixes and Cc: stable tags, as suggested by Jarkko
Sakkinen: a TPM reporting an oversized public area is not known to violate the TCG specification, so this is hardening rather than a bug fix. No code changes.

 drivers/char/tpm/tpm2-sessions.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/char/tpm/tpm2-sessions.c +2 -0
index cf8f1fd6790b..e140079dbc13 100644
@@ -1125,6 +1125,8 @@ static int tpm2_parse_create_primary(struct tpm_chip *chip, struct tpm_buf *buf,
 		return -EINVAL;
 	len = tpm_buf_read_u16(buf, &offset_r);
 	offset_t = offset_r;
+	if (offset_r + len > tpm_buf_length(buf))
+		return -EINVAL;
 	if (name) {
 		/*
 		 * now we have the public area, compute the name of
Signature · 1 line
2.50.1

Thanks this change itself is great and well described.

Br, Jarkko