Commit 40813f18 authored by Jarkko Sakkinen's avatar Jarkko Sakkinen

KEYS: trusted: tpm2: Use struct tpm_buf for sized buffers

Take advantage of the new sized buffer (TPM2B) mode of struct tpm_buf in
tpm2_seal_trusted(). This allows to add robustness to the command
construction without requiring to calculate buffer sizes manually.
Signed-off-by: default avatarJames Bottomley <James.Bottomley@HansenPartnership.com>
Reviewed-by: default avatarStefan Berger <stefanb@linux.ibm.com>
Reviewed-by: default avatarJarkko Sakkinen <jarkko@kernel.org>
Tested-by: default avatarJarkko Sakkinen <jarkko@kernel.org>
Signed-off-by: default avatarJarkko Sakkinen <jarkko@kernel.org>
parent acd5eb4f
...@@ -228,8 +228,9 @@ int tpm2_seal_trusted(struct tpm_chip *chip, ...@@ -228,8 +228,9 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
struct trusted_key_payload *payload, struct trusted_key_payload *payload,
struct trusted_key_options *options) struct trusted_key_options *options)
{ {
off_t offset = TPM_HEADER_SIZE;
struct tpm_buf buf, sized;
int blob_len = 0; int blob_len = 0;
struct tpm_buf buf;
u32 hash; u32 hash;
u32 flags; u32 flags;
int i; int i;
...@@ -258,6 +259,14 @@ int tpm2_seal_trusted(struct tpm_chip *chip, ...@@ -258,6 +259,14 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
return rc; return rc;
} }
rc = tpm_buf_init_sized(&sized);
if (rc) {
tpm_buf_destroy(&buf);
tpm_put_ops(chip);
return rc;
}
tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_CREATE);
tpm_buf_append_u32(&buf, options->keyhandle); tpm_buf_append_u32(&buf, options->keyhandle);
tpm2_buf_append_auth(&buf, TPM2_RS_PW, tpm2_buf_append_auth(&buf, TPM2_RS_PW,
NULL /* nonce */, 0, NULL /* nonce */, 0,
...@@ -266,36 +275,36 @@ int tpm2_seal_trusted(struct tpm_chip *chip, ...@@ -266,36 +275,36 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
TPM_DIGEST_SIZE); TPM_DIGEST_SIZE);
/* sensitive */ /* sensitive */
tpm_buf_append_u16(&buf, 4 + options->blobauth_len + payload->key_len); tpm_buf_append_u16(&sized, options->blobauth_len);
tpm_buf_append_u16(&buf, options->blobauth_len);
if (options->blobauth_len) if (options->blobauth_len)
tpm_buf_append(&buf, options->blobauth, options->blobauth_len); tpm_buf_append(&sized, options->blobauth, options->blobauth_len);
tpm_buf_append_u16(&buf, payload->key_len); tpm_buf_append_u16(&sized, payload->key_len);
tpm_buf_append(&buf, payload->key, payload->key_len); tpm_buf_append(&sized, payload->key, payload->key_len);
tpm_buf_append(&buf, sized.data, sized.length);
/* public */ /* public */
tpm_buf_append_u16(&buf, 14 + options->policydigest_len); tpm_buf_reset_sized(&sized);
tpm_buf_append_u16(&buf, TPM_ALG_KEYEDHASH); tpm_buf_append_u16(&sized, TPM_ALG_KEYEDHASH);
tpm_buf_append_u16(&buf, hash); tpm_buf_append_u16(&sized, hash);
/* key properties */ /* key properties */
flags = 0; flags = 0;
flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH; flags |= options->policydigest_len ? 0 : TPM2_OA_USER_WITH_AUTH;
flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | flags |= payload->migratable ? 0 : (TPM2_OA_FIXED_TPM | TPM2_OA_FIXED_PARENT);
TPM2_OA_FIXED_PARENT); tpm_buf_append_u32(&sized, flags);
tpm_buf_append_u32(&buf, flags);
/* policy */ /* policy */
tpm_buf_append_u16(&buf, options->policydigest_len); tpm_buf_append_u16(&sized, options->policydigest_len);
if (options->policydigest_len) if (options->policydigest_len)
tpm_buf_append(&buf, options->policydigest, tpm_buf_append(&sized, options->policydigest, options->policydigest_len);
options->policydigest_len);
/* public parameters */ /* public parameters */
tpm_buf_append_u16(&buf, TPM_ALG_NULL); tpm_buf_append_u16(&sized, TPM_ALG_NULL);
tpm_buf_append_u16(&buf, 0); tpm_buf_append_u16(&sized, 0);
tpm_buf_append(&buf, sized.data, sized.length);
/* outside info */ /* outside info */
tpm_buf_append_u16(&buf, 0); tpm_buf_append_u16(&buf, 0);
...@@ -312,21 +321,20 @@ int tpm2_seal_trusted(struct tpm_chip *chip, ...@@ -312,21 +321,20 @@ int tpm2_seal_trusted(struct tpm_chip *chip,
if (rc) if (rc)
goto out; goto out;
blob_len = be32_to_cpup((__be32 *) &buf.data[TPM_HEADER_SIZE]); blob_len = tpm_buf_read_u32(&buf, &offset);
if (blob_len > MAX_BLOB_SIZE) { if (blob_len > MAX_BLOB_SIZE || buf.flags & TPM_BUF_BOUNDARY_ERROR) {
rc = -E2BIG; rc = -E2BIG;
goto out; goto out;
} }
if (tpm_buf_length(&buf) < TPM_HEADER_SIZE + 4 + blob_len) { if (buf.length - offset < blob_len) {
rc = -EFAULT; rc = -EFAULT;
goto out; goto out;
} }
blob_len = tpm2_key_encode(payload, options, blob_len = tpm2_key_encode(payload, options, &buf.data[offset], blob_len);
&buf.data[TPM_HEADER_SIZE + 4],
blob_len);
out: out:
tpm_buf_destroy(&sized);
tpm_buf_destroy(&buf); tpm_buf_destroy(&buf);
if (rc > 0) { if (rc > 0) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment