keys/trusted/tpm2: Validate TPM2_Create object sizes separately
Subsystems (3)
PATCH v3 1/1 keys/trusted/tpm2: Validate TPM2_Create object sizes separately
TPM2_Create returns outPrivate, outPublic, creationData, creationHash and creationTicket in its response parameter area. However, only outPrivate and outPublic are included in the trusted key blob. The size of the blob is therefore not determined by the size of the complete response parameter area.
tpm2_seal_trusted() currently compares the size of the complete response parameter area against MAX_BLOB_SIZE. This can reject a valid response when the remaining response outputs cause the entire response parameter area to exceed MAX_BLOB_SIZE, even though the outPrivate and outPublic TPM2B structures consumed by tpm2_key_encode() remain small enough to be encoded in the key blob.
This is observed when creating larger trusted keys using the swtpm TPM 2.0 emulator backed by libtpms.
For example, requesting a 113-byte key succeeds, 114 fails.
~$ keyctl add trusted trusted_key1 "new 113 keyhandle=0x81000001" @u 520504613
~$ keyctl add trusted trusted_key2 "new 114 keyhandle=0x81000001" @uadd_key: Argument list too long
~$
Remove the MAX_BLOB_SIZE check on the complete response parameter area. Instead, use the response length passed to tpm2_key_encode() to validate that the outPrivate and outPublic TPM2B structures are fully contained in the response before accessing them.
Previously, a response parameter area larger than MAX_BLOB_SIZE was rejected with -E2BIG before ASN.1 encoding. With this change, if the resulting encoded blob does not fit in payload->blob, the error returned by asn1_encode_sequence() is propagated instead.
Also, use scope-based cleanup to simplify resource management.
---
Changelog:
v3:
- Simplify the TPM2B_PRIVATE and TPM2B_PUBLIC bounds checks
- Use appropriate error codes for failure returns
- Use scope-based cleanup to simplify resource management
v2:
- Exclude a comment pointed out by Jarkko
security/keys/trusted-keys/trusted_tpm2.c | 42 +++++++++++++---------- 1 file changed, 24 insertions(+), 18 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +24 -18
@@ -25,24 +25,36 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, {struct trusted_key_tpm *private = options->private;const int SCRATCH_SIZE =PAGE_SIZE ; -u8 *scratch =kmalloc (SCRATCH_SIZE ,GFP_KERNEL ); -u8 *work = scratch, *work1; -u8 *end_work = scratch +SCRATCH_SIZE ; + u8 *scratch __free(kfree) = NULL; +u8 *work, *work1; +u8 *end_work;u8 *priv, *pub; -u16 priv_len, pub_len; +u32 priv_len, pub_len;int ret; +if (len <4 ) +return -EINVAL ; + priv_len =get_unaligned_be16 (src) +2 ; - priv = src; +if (priv_len +2 > len) +return -EIO ; + priv = src; src += priv_len; pub_len =get_unaligned_be16 (src) +2 ; +if (pub_len + priv_len > len) +return -EIO ; + pub = src; + scratch =kmalloc (SCRATCH_SIZE ,GFP_KERNEL );if (!scratch)return -ENOMEM ; + work = scratch; + end_work = scratch +SCRATCH_SIZE ; + work =asn1_encode_oid (work, end_work, tpm2key_oid,asn1_oid_len (tpm2key_oid));
@@ -50,10 +62,9 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,unsigned char bool[3 ], *w = bool;/* tag 0 is emptyAuth */ w =asn1_encode_boolean (w, w +sizeof (bool ), true); -if (WARN (IS_ERR (w),"BUG: Boolean failed to encode" )) { - ret =PTR_ERR (w); - goto err; - } +if (WARN (IS_ERR (w),"BUG: Boolean failed to encode" )) +return PTR_ERR (w); + work =asn1_encode_tag (work, end_work,0 , bool, w - bool); }
@@ -65,8 +76,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,*/ if (WARN (work - scratch + pub_len + priv_len +14 >SCRATCH_SIZE ,"BUG: scratch buffer is too small" )) { - ret = -EINVAL ; - goto err; +return -EINVAL ; } work =asn1_encode_integer (work, end_work, private->keyhandle);
@@ -79,15 +89,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,if (IS_ERR (work1)) { ret =PTR_ERR (work1);pr_err ("BUG: ASN.1 encoder failed with %d\n" , ret); - goto err; +return ret; } -kfree (scratch);return work1 - payload->blob; - -err: -kfree (scratch); -return ret; }struct tpm2_key_context {
@@ -340,10 +345,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; blob_len =tpm_buf_read_u32 (buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf->flags &TPM_BUF_INVALID ) { +if (buf->flags &TPM_BUF_INVALID ) { rc = -E2BIG ; goto out; } +if (buf->length - offset < blob_len) { rc = -EFAULT ; goto out;
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] cleanup: mixing goto-based error handling with scope-based cleanup --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] cleanup: mixing goto-based error handling with scope-based cleanup --
commit 6bc711f0f487069cc2bd8437493ff78b9ed1f263
Author: Srish Srinivasan <[email protected]>
keys/trusted/tpm2: Validate TPM2_Create object sizes separately
This commit updates trusted key creation to validate TPM2_Create object sizes separately from the complete response area. It also adopts scope-based cleanup to simplify resource management.
Quoted 4 lines
security/keys/trusted-keys/trusted_tpm2.c +0
-0
[ ... ]
security/keys/trusted-keys/trusted_tpm2.c int tpm2_seal_trusted(struct tpm_chip *chip, · line 355
Quoted 13 lines
security/keys/trusted-keys/trusted_tpm2.c +2
-1@@ -340,10 +345,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; blob_len =tpm_buf_read_u32 (buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf->flags &TPM_BUF_INVALID ) { +if (buf->flags &TPM_BUF_INVALID ) { rc = -E2BIG ; goto out; } +if (buf->length - offset < blob_len) { rc = -EFAULT ; goto out;
[Severity: Low]
Does this function mix goto-based error handling with scope-based cleanup?
Looking at tpm2_seal_trusted() in security/keys/trusted-keys/trusted_tpm2.c, it defines resources using scope-based cleanup:
struct tpm_buf *buf __free(kfree) = NULL; struct tpm_buf *sized __free(kfree) = NULL;
But the function still uses goto labels for TPM operations cleanup:
out:
if (!rc) payload->blob_len = blob_len;
out_put:
tpm_put_ops(chip); return tpm_ret_to_err(rc);
The cleanup subsystem guidelines expect that usage of goto and cleanup helpers are never mixed in the same function.
Could the remaining goto labels in tpm2_seal_trusted() be converted to scope-based cleanup to avoid mixing paradigms?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 154 lines · Srish Srinivasan
TPM2_Createreturns outPrivate, outPublic, creationData, creationHash and creationTicket in its response parameter area. However, only outPrivate and outPublic are included in the trusted key blob. The size of the blob is therefore not determined by the size of the complete response parameter area.
tpm2_seal_trusted()currently compares the size of the complete response parameter area againstMAX_BLOB_SIZE. This can reject a valid response when the remaining response outputs cause the entire response parameter area to exceedMAX_BLOB_SIZE, even though the outPrivate and outPublic TPM2B structures consumed bytpm2_key_encode()remain small enough to be encoded in the key blob.This is observed when creating larger trusted keys using the swtpm TPM 2.0 emulator backed by libtpms.
For example, requesting a 113-byte key succeeds, 114 fails.
~$ keyctl add trusted
trusted_key1"new 113 keyhandle=0x81000001" @u 520504613
~$ keyctl add trustedtrusted_key2"new 114 keyhandle=0x81000001" @uadd_key: Argument list too long
~$Remove the
MAX_BLOB_SIZEcheck on the complete response parameter area. Instead, use the response length passed totpm2_key_encode()to validate that the outPrivate and outPublic TPM2B structures are fully contained in the response before accessing them.Previously, a response parameter area larger than
MAX_BLOB_SIZEwas rejected with-E2BIGbefore ASN.1 encoding. With this change, if the resulting encoded blob does not fit inpayload->blob, the error returned byasn1_encode_sequence()is propagated instead.Also, use scope-based cleanup to simplify resource management.
Signed-off-by Srish Srinivasan <[email protected]>---
Changelog:v3:
- Simplify theTPM2B_PRIVATEandTPM2B_PUBLICbounds checks
- Use appropriate error codes for failure returns
- Use scope-based cleanup to simplify resource managementv2:
- Exclude a comment pointed out by Jarkkosecurity/keys/trusted-keys/trusted_tpm2.c | 42 +++++++++++++---------- 1 file changed, 24 insertions(+), 18 deletions(-)security/keys/trusted-keys/trusted_tpm2.c +24
-18@@ -25,24 +25,36 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, {struct trusted_key_tpm *private = options->private;const int SCRATCH_SIZE =PAGE_SIZE ; -u8 *scratch =kmalloc (SCRATCH_SIZE ,GFP_KERNEL ); -u8 *work = scratch, *work1; -u8 *end_work = scratch +SCRATCH_SIZE ; + u8 *scratch __free(kfree) = NULL; +u8 *work, *work1; +u8 *end_work;u8 *priv, *pub; -u16 priv_len, pub_len; +u32 priv_len, pub_len;int ret; +if (len <4 ) +return -EINVAL ; + priv_len =get_unaligned_be16 (src) +2 ; - priv = src; +if (priv_len +2 > len) +return -EIO ; + priv = src; src += priv_len; pub_len =get_unaligned_be16 (src) +2 ; +if (pub_len + priv_len > len) +return -EIO ; + pub = src; + scratch =kmalloc (SCRATCH_SIZE ,GFP_KERNEL );if (!scratch)return -ENOMEM ; + work = scratch; + end_work = scratch +SCRATCH_SIZE ; + work =asn1_encode_oid (work, end_work, tpm2key_oid,asn1_oid_len (tpm2key_oid));@@ -50,10 +62,9 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,unsigned char bool[3 ], *w = bool;/* tag 0 is emptyAuth */ w =asn1_encode_boolean (w, w +sizeof (bool ), true); -if (WARN (IS_ERR (w),"BUG: Boolean failed to encode" )) { - ret =PTR_ERR (w); - goto err; - } +if (WARN (IS_ERR (w),"BUG: Boolean failed to encode" )) +return PTR_ERR (w); + work =asn1_encode_tag (work, end_work,0 , bool, w - bool); }@@ -65,8 +76,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,*/ if (WARN (work - scratch + pub_len + priv_len +14 >SCRATCH_SIZE ,"BUG: scratch buffer is too small" )) { - ret = -EINVAL ; - goto err; +return -EINVAL ; } work =asn1_encode_integer (work, end_work, private->keyhandle);@@ -79,15 +89,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,if (IS_ERR (work1)) { ret =PTR_ERR (work1);pr_err ("BUG: ASN.1 encoder failed with %d\n" , ret); - goto err; +return ret; } -kfree (scratch);return work1 - payload->blob; - -err: -kfree (scratch); -return ret; }struct tpm2_key_context {@@ -340,10 +345,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; blob_len =tpm_buf_read_u32 (buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf->flags &TPM_BUF_INVALID ) { +if (buf->flags &TPM_BUF_INVALID ) { rc = -E2BIG ; goto out; } +if (buf->length - offset < blob_len) { rc = -EFAULT ; goto out;Signature · 1 line
2.53.0
Now we can say that it leaves the tree to cleaner state than it was before applying this patch. The first version, despite doing the right thing was simply too convoluted.
Br, Jarkko
Quoted 159 lines · Jarkko Sakkinen
Quoted 154 lines · Srish Srinivasan
TPM2_Createreturns outPrivate, outPublic, creationData, creationHash and creationTicket in its response parameter area. However, only outPrivate and outPublic are included in the trusted key blob. The size of the blob is therefore not determined by the size of the complete response parameter area.
tpm2_seal_trusted()currently compares the size of the complete response parameter area againstMAX_BLOB_SIZE. This can reject a valid response when the remaining response outputs cause the entire response parameter area to exceedMAX_BLOB_SIZE, even though the outPrivate and outPublic TPM2B structures consumed bytpm2_key_encode()remain small enough to be encoded in the key blob.This is observed when creating larger trusted keys using the swtpm TPM 2.0 emulator backed by libtpms.
For example, requesting a 113-byte key succeeds, 114 fails.
~$ keyctl add trusted
trusted_key1"new 113 keyhandle=0x81000001" @u 520504613
~$ keyctl add trustedtrusted_key2"new 114 keyhandle=0x81000001" @uadd_key: Argument list too long
~$Remove the
MAX_BLOB_SIZEcheck on the complete response parameter area. Instead, use the response length passed totpm2_key_encode()to validate that the outPrivate and outPublic TPM2B structures are fully contained in the response before accessing them.Previously, a response parameter area larger than
MAX_BLOB_SIZEwas rejected with-E2BIGbefore ASN.1 encoding. With this change, if the resulting encoded blob does not fit inpayload->blob, the error returned byasn1_encode_sequence()is propagated instead.Also, use scope-based cleanup to simplify resource management.
Signed-off-by Srish Srinivasan <[email protected]>---
Changelog:v3:
- Simplify theTPM2B_PRIVATEandTPM2B_PUBLICbounds checks
- Use appropriate error codes for failure returns
- Use scope-based cleanup to simplify resource managementv2:
- Exclude a comment pointed out by Jarkkosecurity/keys/trusted-keys/trusted_tpm2.c | 42 +++++++++++++---------- 1 file changed, 24 insertions(+), 18 deletions(-)security/keys/trusted-keys/trusted_tpm2.c +24
-18@@ -25,24 +25,36 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, {struct trusted_key_tpm *private = options->private;const int SCRATCH_SIZE =PAGE_SIZE ; -u8 *scratch =kmalloc (SCRATCH_SIZE ,GFP_KERNEL ); -u8 *work = scratch, *work1; -u8 *end_work = scratch +SCRATCH_SIZE ; + u8 *scratch __free(kfree) = NULL; +u8 *work, *work1; +u8 *end_work;u8 *priv, *pub; -u16 priv_len, pub_len; +u32 priv_len, pub_len;int ret; +if (len <4 ) +return -EINVAL ; + priv_len =get_unaligned_be16 (src) +2 ; - priv = src; +if (priv_len +2 > len) +return -EIO ; + priv = src; src += priv_len; pub_len =get_unaligned_be16 (src) +2 ; +if (pub_len + priv_len > len) +return -EIO ; + pub = src; + scratch =kmalloc (SCRATCH_SIZE ,GFP_KERNEL );if (!scratch)return -ENOMEM ; + work = scratch; + end_work = scratch +SCRATCH_SIZE ; + work =asn1_encode_oid (work, end_work, tpm2key_oid,asn1_oid_len (tpm2key_oid));@@ -50,10 +62,9 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,unsigned char bool[3 ], *w = bool;/* tag 0 is emptyAuth */ w =asn1_encode_boolean (w, w +sizeof (bool ), true); -if (WARN (IS_ERR (w),"BUG: Boolean failed to encode" )) { - ret =PTR_ERR (w); - goto err; - } +if (WARN (IS_ERR (w),"BUG: Boolean failed to encode" )) +return PTR_ERR (w); + work =asn1_encode_tag (work, end_work,0 , bool, w - bool); }@@ -65,8 +76,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,*/ if (WARN (work - scratch + pub_len + priv_len +14 >SCRATCH_SIZE ,"BUG: scratch buffer is too small" )) { - ret = -EINVAL ; - goto err; +return -EINVAL ; } work =asn1_encode_integer (work, end_work, private->keyhandle);@@ -79,15 +89,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,if (IS_ERR (work1)) { ret =PTR_ERR (work1);pr_err ("BUG: ASN.1 encoder failed with %d\n" , ret); - goto err; +return ret; } -kfree (scratch);return work1 - payload->blob; - -err: -kfree (scratch); -return ret; }struct tpm2_key_context {@@ -340,10 +345,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; blob_len =tpm_buf_read_u32 (buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf->flags &TPM_BUF_INVALID ) { +if (buf->flags &TPM_BUF_INVALID ) { rc = -E2BIG ; goto out; } +if (buf->length - offset < blob_len) { rc = -EFAULT ; goto out;Signature · 1 line
2.53.0Now we can say that it leaves the tree to cleaner state than it was before applying this patch. The first version, despite doing the right thing was simply too convoluted.
Quoted 1 line
Reviewed-by Jarkko Sakkinen <[email protected]>
Thanks for the review and for helping improve the patch, Jarkko.
Quoted 1 line
Br, Jarkko
Thanks,
Srish.
Jarkko Sakkinen
maintainerQuoted 150 lines · Srish Srinivasan
Quoted 148 lines · Jarkko Sakkinen
Quoted 143 lines · Srish Srinivasan
TPM2_Createreturns outPrivate, outPublic, creationData, creationHash and creationTicket in its response parameter area. However, only outPrivate and outPublic are included in the trusted key blob. The size of the blob is therefore not determined by the size of the complete response parameter area.
tpm2_seal_trusted()currently compares the size of the complete response parameter area againstMAX_BLOB_SIZE. This can reject a valid response when the remaining response outputs cause the entire response parameter area to exceedMAX_BLOB_SIZE, even though the outPrivate and outPublic TPM2B structures consumed bytpm2_key_encode()remain small enough to be encoded in the key blob.This is observed when creating larger trusted keys using the swtpm TPM 2.0 emulator backed by libtpms.
For example, requesting a 113-byte key succeeds, 114 fails.
~$ keyctl add trusted
trusted_key1"new 113 keyhandle=0x81000001" @u 520504613
~$ keyctl add trustedtrusted_key2"new 114 keyhandle=0x81000001" @uadd_key: Argument list too long
~$Remove the
MAX_BLOB_SIZEcheck on the complete response parameter area. Instead, use the response length passed totpm2_key_encode()to validate that the outPrivate and outPublic TPM2B structures are fully contained in the response before accessing them.Previously, a response parameter area larger than
MAX_BLOB_SIZEwas rejected with-E2BIGbefore ASN.1 encoding. With this change, if the resulting encoded blob does not fit inpayload->blob, the error returned byasn1_encode_sequence()is propagated instead.Also, use scope-based cleanup to simplify resource management.
Signed-off-by Srish Srinivasan <[email protected]>---
Changelog:v3:
- Simplify theTPM2B_PRIVATEandTPM2B_PUBLICbounds checks
- Use appropriate error codes for failure returns
- Use scope-based cleanup to simplify resource managementv2:
- Exclude a comment pointed out by Jarkkosecurity/keys/trusted-keys/trusted_tpm2.c | 42 +++++++++++++---------- 1 file changed, 24 insertions(+), 18 deletions(-)security/keys/trusted-keys/trusted_tpm2.c +24
-18@@ -25,24 +25,36 @@ static int tpm2_key_encode(struct trusted_key_payload *payload, {struct trusted_key_tpm *private = options->private;const int SCRATCH_SIZE =PAGE_SIZE ; -u8 *scratch =kmalloc (SCRATCH_SIZE ,GFP_KERNEL ); -u8 *work = scratch, *work1; -u8 *end_work = scratch +SCRATCH_SIZE ; + u8 *scratch __free(kfree) = NULL; +u8 *work, *work1; +u8 *end_work;u8 *priv, *pub; -u16 priv_len, pub_len; +u32 priv_len, pub_len;int ret; +if (len <4 ) +return -EINVAL ; + priv_len =get_unaligned_be16 (src) +2 ; - priv = src; +if (priv_len +2 > len) +return -EIO ; + priv = src; src += priv_len; pub_len =get_unaligned_be16 (src) +2 ; +if (pub_len + priv_len > len) +return -EIO ; + pub = src; + scratch =kmalloc (SCRATCH_SIZE ,GFP_KERNEL );if (!scratch)return -ENOMEM ; + work = scratch; + end_work = scratch +SCRATCH_SIZE ; + work =asn1_encode_oid (work, end_work, tpm2key_oid,asn1_oid_len (tpm2key_oid));@@ -50,10 +62,9 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,unsigned char bool[3 ], *w = bool;/* tag 0 is emptyAuth */ w =asn1_encode_boolean (w, w +sizeof (bool ), true); -if (WARN (IS_ERR (w),"BUG: Boolean failed to encode" )) { - ret =PTR_ERR (w); - goto err; - } +if (WARN (IS_ERR (w),"BUG: Boolean failed to encode" )) +return PTR_ERR (w); + work =asn1_encode_tag (work, end_work,0 , bool, w - bool); }@@ -65,8 +76,7 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,*/ if (WARN (work - scratch + pub_len + priv_len +14 >SCRATCH_SIZE ,"BUG: scratch buffer is too small" )) { - ret = -EINVAL ; - goto err; +return -EINVAL ; } work =asn1_encode_integer (work, end_work, private->keyhandle);@@ -79,15 +89,10 @@ static int tpm2_key_encode(struct trusted_key_payload *payload,if (IS_ERR (work1)) { ret =PTR_ERR (work1);pr_err ("BUG: ASN.1 encoder failed with %d\n" , ret); - goto err; +return ret; } -kfree (scratch);return work1 - payload->blob; - -err: -kfree (scratch); -return ret; }struct tpm2_key_context {@@ -340,10 +345,11 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; blob_len =tpm_buf_read_u32 (buf, &offset); -if (blob_len >MAX_BLOB_SIZE || buf->flags &TPM_BUF_INVALID ) { +if (buf->flags &TPM_BUF_INVALID ) { rc = -E2BIG ; goto out; } +if (buf->length - offset < blob_len) { rc = -EFAULT ; goto out;Signature · 1 line
2.53.0Now we can say that it leaves the tree to cleaner state than it was before applying this patch. The first version, despite doing the right thing was simply too convoluted.
Quoted 4 lines
Quoted 1 line
Reviewed-by Jarkko Sakkinen <[email protected]>Thanks for the review and for helping improve the patch, Jarkko.
sure, np :-)
Br, Jarkko