Skip to contentSkip to conversation

KEYS: trusted: Reject short TPM2 public areas

PATCH Superseded Daehyeon Ko 3 participants 7 messages Sep 25 – Oct 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KEYS: trusted: Reject short TPM2 public areas

tpm2_load_cmd() reads TPMA_OBJECT with get_unaligned_be32(pub + 4), but does not require public_len to cover that field.

A new-format blob with public_len zero makes the read begin at the end of the B + 4-byte decoded allocation, causing a four-byte heap out-of-bounds read before the TPM command is transmitted. This is reachable from an unprivileged add_key() call when TPM trusted keys and a TPM2 device are available.

This affects v5.13-rc1 and later kernels built with CONFIG_TRUSTED_KEYS=y and CONFIG_TRUSTED_KEYS_TPM=y when a TPM2 device is present. A KASAN run as UID 1000 with no effective capabilities reported:

BUG: KASAN: slab-out-of-bounds in tpm2_unseal_trusted
Read of size 4 at addr ffff888106273b48 by task exploit/160
CPU: 1 UID: 1000 PID: 160 Comm: exploit
The buggy address belongs to the object at ffff888106273b40
 which belongs to the cache kmalloc-8 of size 8

The buggy address is located 0 bytes to the right of
allocated 8-byte region [ffff888106273b40, ffff888106273b48)

TPMT_PUBLIC starts with the two-byte type, two-byte nameAlg and four-byte objectAttributes fields. Require public_len to cover all eight bytes before reading the attributes.

The exact input produced the KASAN read in 3/3 fresh boots. The fixed build rejected it with -E2BIG and no KASAN report in 3/3 boots; valid new- and old-format trusted-key loads continued to succeed.

Fixes e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")
Assisted-by LLM

---
Tested with QEMU tpm-tis and swtpm: vulnerable 3/3 KASAN reports, fixed 3/3 clean -E2BIG rejections, with public_len 7/8 and new/old-format controls passing. Stable 5.15+ requires 114f00d738f1 first; both patches apply cleanly in that order.

The source reproducer and full logs are available privately on request.

 security/keys/trusted-keys/trusted_tpm2.c | 2 ++
 1 file changed, 2 insertions(+)
security/keys/trusted-keys/trusted_tpm2.c +2 -0
index c2a69bcf381d7..b3109e0a924f5 100644
@@ -418,6 +418,8 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	public_len = get_unaligned_be16(blob + 2 + private_len);
 	if (private_len + 2 + public_len + 2 > blob_len)
 		return -E2BIG;
+	if (public_len < 8)
+		return -E2BIG;
 
 	pub = blob + 2 + private_len + 2;
 	/* key attributes are always at offset 4 */

base-commit: 27d14d3b15d5691bcbf0683883a2ea12469edbea

#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

tpm2_load_cmd() reads TPMA_OBJECT with get_unaligned_be32(pub + 4), but does not require public_len to cover that field.

A new-format blob with public_len zero makes the read begin at the end of the B + 4-byte decoded allocation, causing a four-byte heap out-of-bounds read before the TPM command is transmitted. This is reachable from an unprivileged add_key() call when TPM trusted keys and a TPM2 device are available.

This affects v5.13-rc1 and later kernels built with CONFIG_TRUSTED_KEYS=y and CONFIG_TRUSTED_KEYS_TPM=y when a TPM2 device is present. A KASAN run as UID 1000 with no effective capabilities reported:

Quoted 8 lines
BUG: KASAN: slab-out-of-bounds in tpm2_unseal_trusted
Read of size 4 at addr ffff888106273b48 by task exploit/160

[ ... ]

Fixes e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")
Assisted-by LLM

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/1
#2 lore raw
In reply to Daehyeon Ko
Quoted 58 lines · Daehyeon Ko

tpm2_load_cmd() reads TPMA_OBJECT with get_unaligned_be32(pub + 4), but does not require public_len to cover that field.

A new-format blob with public_len zero makes the read begin at the end of the B + 4-byte decoded allocation, causing a four-byte heap out-of-bounds read before the TPM command is transmitted. This is reachable from an unprivileged add_key() call when TPM trusted keys and a TPM2 device are available.

This affects v5.13-rc1 and later kernels built with CONFIG_TRUSTED_KEYS=y and CONFIG_TRUSTED_KEYS_TPM=y when a TPM2 device is present. A KASAN run as UID 1000 with no effective capabilities reported:

BUG: KASAN: slab-out-of-bounds in tpm2_unseal_trusted
Read of size 4 at addr ffff888106273b48 by task exploit/160
CPU: 1 UID: 1000 PID: 160 Comm: exploit
The buggy address belongs to the object at ffff888106273b40
 which belongs to the cache kmalloc-8 of size 8

The buggy address is located 0 bytes to the right of
allocated 8-byte region [ffff888106273b40, ffff888106273b48)

TPMT_PUBLIC starts with the two-byte type, two-byte nameAlg and four-byte objectAttributes fields. Require public_len to cover all eight bytes before reading the attributes.

The exact input produced the KASAN read in 3/3 fresh boots. The fixed build rejected it with -E2BIG and no KASAN report in 3/3 boots; valid new- and old-format trusted-key loads continued to succeed.

Fixes e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")
Assisted-by LLM

---
Tested with QEMU tpm-tis and swtpm: vulnerable 3/3 KASAN reports, fixed 3/3 clean -E2BIG rejections, with public_len 7/8 and new/old-format controls passing. Stable 5.15+ requires 114f00d738f1 first; both patches apply cleanly in that order.

The source reproducer and full logs are available privately on request.

 security/keys/trusted-keys/trusted_tpm2.c | 2 ++
 1 file changed, 2 insertions(+)
security/keys/trusted-keys/trusted_tpm2.c +2 -0
index c2a69bcf381d7..b3109e0a924f5 100644
@@ -418,6 +418,8 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	public_len = get_unaligned_be16(blob + 2 + private_len);
 	if (private_len + 2 + public_len + 2 > blob_len)
 		return -E2BIG;
+	if (public_len < 8)
+		return -E2BIG;
 
 	pub = blob + 2 + private_len + 2;
 	/* key attributes are always at offset 4 */
Quoted 1 line

base-commit: 27d14d3b15d5691bcbf0683883a2ea12469edbea

Yeah, makes sense as those eight bytes is what covers the data needed to identify the key.

Thanks.

Br, Jarkko

maintainer
#3 lore raw
In reply to Daehyeon Ko
Quoted 4 lines · Daehyeon Ko

tpm2_load_cmd() reads TPMA_OBJECT with get_unaligned_be32(pub + 4), but does not require public_len to cover that field.

A new-format blob with public_len zero makes the read begin at the end of

New-format is not anything. TPM2 protocol format might be better here (nit).

Quoted 45 lines

the B + 4-byte decoded allocation, causing a four-byte heap out-of-bounds read before the TPM command is transmitted. This is reachable from an unprivileged add_key() call when TPM trusted keys and a TPM2 device are available.

This affects v5.13-rc1 and later kernels built with CONFIG_TRUSTED_KEYS=y and CONFIG_TRUSTED_KEYS_TPM=y when a TPM2 device is present. A KASAN run as UID 1000 with no effective capabilities reported:

BUG: KASAN: slab-out-of-bounds in tpm2_unseal_trusted
Read of size 4 at addr ffff888106273b48 by task exploit/160
CPU: 1 UID: 1000 PID: 160 Comm: exploit
The buggy address belongs to the object at ffff888106273b40
 which belongs to the cache kmalloc-8 of size 8

The buggy address is located 0 bytes to the right of
allocated 8-byte region [ffff888106273b40, ffff888106273b48)

TPMT_PUBLIC starts with the two-byte type, two-byte nameAlg and four-byte objectAttributes fields. Require public_len to cover all eight bytes before reading the attributes.

The exact input produced the KASAN read in 3/3 fresh boots. The fixed build rejected it with -E2BIG and no KASAN report in 3/3 boots; valid new- and old-format trusted-key loads continued to succeed.

Fixes e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")
Assisted-by LLM

---
Tested with QEMU tpm-tis and swtpm: vulnerable 3/3 KASAN reports, fixed 3/3 clean -E2BIG rejections, with public_len 7/8 and new/old-format controls passing. Stable 5.15+ requires 114f00d738f1 first; both patches apply cleanly in that order.

The source reproducer and full logs are available privately on request.

 security/keys/trusted-keys/trusted_tpm2.c | 2 ++
 1 file changed, 2 insertions(+)
security/keys/trusted-keys/trusted_tpm2.c +0 -0
index c2a69bcf381d7..b3109e0a924f5 100644

security/keys/trusted-keys/trusted_tpm2.c static int tpm2_load_cmd(struct tpm_chip *chip, · line 420

Quoted 3 lines
security/keys/trusted-keys/trusted_tpm2.c +0 -0
@@ -418,6 +418,8 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	public_len = get_unaligned_be16(blob + 2 + private_len);
 	if (private_len + 2 + public_len + 2 > blob_len)
 		return -E2BIG;

I was writing summary of the changes for my v7.4-rc1 PR but this does not look right.

This would really need an explanation like:

/*
* Check that TPMT_PUBLIC fields type, nameAlg and objectAttributes are covered
* in order to avoid out-of-bounds read.
*/

security/keys/trusted-keys/trusted_tpm2.c static int tpm2_load_cmd(struct tpm_chip *chip, · line 422

Quoted 2 lines
security/keys/trusted-keys/trusted_tpm2.c +2 -0
+	if (public_len < 8)
+		return -E2BIG;

Why would you pick -E2BIG as I guess here that is not the problem?

Just checking before proposing anything.

Quoted 5 lines
security/keys/trusted-keys/trusted_tpm2.c +0 -0
 	pub = blob + 2 + private_len + 2;
 	/* key attributes are always at offset 4 */
 

base-commit: 27d14d3b15d5691bcbf0683883a2ea12469edbea

I'll drop this patch and postpone v7.4-rc1 at least one day so there is still time.

Br, Jarkko

#4 lore raw

PATCH v2 1/1 KEYS: trusted: Reject short TPM2 public areas

tpm2_load_cmd() reads TPMA_OBJECT with get_unaligned_be32(pub + 4), but does not require public_len to cover that field.

A TPM 2.0 ASN.1 key blob with public_len zero makes the read begin at the end of the B + 4-byte decoded allocation, causing a four-byte heap out-of-bounds read before the TPM command is transmitted. This is reachable from an unprivileged add_key() call when TPM trusted keys and a TPM2 device are available.

This affects v5.13-rc1 and later kernels built with CONFIG_TRUSTED_KEYS=y and CONFIG_TRUSTED_KEYS_TPM=y when a TPM2 device is present. A KASAN run as UID 1000 with no effective capabilities reported:

BUG: KASAN: slab-out-of-bounds in tpm2_unseal_trusted
Read of size 4 at addr ffff888106273b48 by task exploit/160
CPU: 1 UID: 1000 PID: 160 Comm: exploit
The buggy address belongs to the object at ffff888106273b40
 which belongs to the cache kmalloc-8 of size 8

The buggy address is located 0 bytes to the right of
allocated 8-byte region [ffff888106273b40, ffff888106273b48)

TPMT_PUBLIC starts with the two-byte type, two-byte nameAlg and four-byte objectAttributes fields. Require public_len to cover all eight bytes before reading the attributes.

The exact input produced the KASAN read in 3/3 fresh boots. The fixed build rejected it with -EINVAL and no KASAN report in 3/3 boots; valid TPM 2.0 ASN.1 and legacy trusted-key loads continued to succeed.

The issue was found with an LLM-assisted source audit and validated with an end-to-end QEMU and swtpm reproducer.

Fixes e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")

---
Changes in v2:
- Describe the wrapper as the TPM 2.0 ASN.1 key format.
- Explain the eight-byte minimum next to the check.
- Return -EINVAL for a malformed TPMT_PUBLIC instead of -E2BIG.
- Replace the obsolete Assisted-by trailer with changelog disclosure.

v1: https://lore.kernel.org/r/[email protected]

 security/keys/trusted-keys/trusted_tpm2.c | 7 +++++++
 1 file changed, 7 insertions(+)
security/keys/trusted-keys/trusted_tpm2.c +7 -0
index 30188a0ec6766..fb45795b53327 100644
@@ -425,6 +425,13 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (private_len + 2 + public_len + 2 > blob_len)
 		return -E2BIG;
 
+	/*
+	 * TPMT_PUBLIC starts with type (2), nameAlg (2), and
+	 * objectAttributes (4), which is read below.
+	 */
+	if (public_len < 8)
+		return -EINVAL;
+
 	pub = blob + 2 + private_len + 2;
 	/* key attributes are always at offset 4 */
 	attrs = get_unaligned_be32(pub + 4);

base-commit: 722658cac48a6ba49f5506fd74d010ad9dbb2f05

Signature · 1 line
2.55.0
#5 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines

tpm2_load_cmd() reads TPMA_OBJECT with get_unaligned_be32(pub + 4), but does not require public_len to cover that field.

A TPM 2.0 ASN.1 key blob with public_len zero makes the read begin at the end of the B + 4-byte decoded allocation, causing a four-byte heap out-of-bounds read before the TPM command is transmitted. This is reachable from an unprivileged add_key() call when TPM trusted keys and a TPM2 device are available.

This affects v5.13-rc1 and later kernels built with CONFIG_TRUSTED_KEYS=y and CONFIG_TRUSTED_KEYS_TPM=y when a TPM2 device is present. A KASAN run as UID 1000 with no effective capabilities reported:

Quoted 8 lines
BUG: KASAN: slab-out-of-bounds in tpm2_unseal_trusted
Read of size 4 at addr ffff888106273b48 by task exploit/160

[ ... ]
end-to-end QEMU and swtpm reproducer.

Fixes e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#6 lore raw
In reply to Daehyeon Ko
Quoted 25 lines · Daehyeon Ko

tpm2_load_cmd() reads TPMA_OBJECT with get_unaligned_be32(pub + 4), but does not require public_len to cover that field.

A TPM 2.0 ASN.1 key blob with public_len zero makes the read begin at the end of the B + 4-byte decoded allocation, causing a four-byte heap out-of-bounds read before the TPM command is transmitted. This is reachable from an unprivileged add_key() call when TPM trusted keys and a TPM2 device are available.

This affects v5.13-rc1 and later kernels built with CONFIG_TRUSTED_KEYS=y and CONFIG_TRUSTED_KEYS_TPM=y when a TPM2 device is present. A KASAN run as UID 1000 with no effective capabilities reported:

BUG: KASAN: slab-out-of-bounds in tpm2_unseal_trusted
Read of size 4 at addr ffff888106273b48 by task exploit/160
CPU: 1 UID: 1000 PID: 160 Comm: exploit
The buggy address belongs to the object at ffff888106273b40
 which belongs to the cache kmalloc-8 of size 8

The buggy address is located 0 bytes to the right of
allocated 8-byte region [ffff888106273b40, ffff888106273b48)

TPMT_PUBLIC starts with the two-byte type, two-byte nameAlg and four-byte objectAttributes fields. Require public_len to cover all eight bytes before reading the attributes.

Quoted 6 lines

The exact input produced the KASAN read in 3/3 fresh boots. The fixed build rejected it with -EINVAL and no KASAN report in 3/3 boots; valid TPM 2.0 ASN.1 and legacy trusted-key loads continued to succeed.

The issue was found with an LLM-assisted source audit and validated with an end-to-end QEMU and swtpm reproducer.

I did not get any response from you so I'll put one more nit here.

I think it would be better swap these two paragraphs with description how you address the issue, and why -EINVAL is the right return value here.

Quoted 21 lines
Fixes e5fb5d2c5a03 ("security: keys: trusted: Make sealed key properly interoperable")

---
Changes in v2:
- Describe the wrapper as the TPM 2.0 ASN.1 key format.
- Explain the eight-byte minimum next to the check.
- Return -EINVAL for a malformed TPMT_PUBLIC instead of -E2BIG.
- Replace the obsolete Assisted-by trailer with changelog disclosure.

v1: https://lore.kernel.org/r/[email protected]

 security/keys/trusted-keys/trusted_tpm2.c | 7 +++++++
 1 file changed, 7 insertions(+)
security/keys/trusted-keys/trusted_tpm2.c +0 -0
index 30188a0ec6766..fb45795b53327 100644

security/keys/trusted-keys/trusted_tpm2.c static int tpm2_load_cmd(struct tpm_chip *chip, · line 434

Quoted 10 lines
security/keys/trusted-keys/trusted_tpm2.c +7 -0
@@ -425,6 +425,13 @@ static int tpm2_load_cmd(struct tpm_chip *chip,
 	if (private_len + 2 + public_len + 2 > blob_len)
 		return -E2BIG;
 
+	/*
+	 * TPMT_PUBLIC starts with type (2), nameAlg (2), and
+	 * objectAttributes (4), which is read below.
+	 */
+	if (public_len < 8)
+		return -EINVAL;
+

I don't think that this nails it as well what this protects against as what I proposed and it does not wrap at 80 chacters.

However, I think what is better here are the sizes in parentheses so take my comment and add sizes to it (and wrap to 80 characters).

Quoted 8 lines
security/keys/trusted-keys/trusted_tpm2.c +0 -0
 	pub = blob + 2 + private_len + 2;
 	/* key attributes are always at offset 4 */
 	attrs = get_unaligned_be32(pub + 4);

base-commit: 722658cac48a6ba49f5506fd74d010ad9dbb2f05

Signature · 1 line
2.55.0

Br, Jarkko