Prepare TPM driver for Trenchboot
Subsystems (5)
Jarkko Sakkinen
maintainerv6 cover Prepare TPM driver for Trenchboot
Overview
========
Prepare TPM driver for Trenchboot by making tpm-buf easy to compile as an independent compilation unit, and generally be IO and memory agnostic. This allows Trenchboot easily use this code to build and parse TPM 1.2 and TPM 2.0 commands.
Backlog (of stuff that will end up to a follow-up patch sets):
1. Static flat duration table for TPM 1.2. Such feature already landed in v6.17 for TPM 2.0. We probably can figure out some sane roof values for the legacy standard, right? This allows to share more code "via data".
2. Not a blocker for sending subsequent versions of Trenchboot patch set but we could look at slicing tpm_tis code at some point of future. It could be even that redundant parts could be "merged" after the feature lands but this is neither a promise (depends on how things look overall).
This patch set does not do a lot heavy-lifting but is beefed enough that it is better not fatten it up too much anymore. Sane buffering is a great starting point.
v6:
- Fixed a memory leak that I found trom trusted keys.
v5:
- Order sobs correctly.
- Fixed a minor glitch in 9/9.
v4:
- PCR patch has been removed because it does not fly without Trenchboot context.
v3:
- I think 6.19 is a better goal for this and thus expanded the series to be a generic Trenchboot enablers series. This version also consolidates my two separate ongoing series.
v2:
- While including fixes from v1, this patch set has a refocus in order to do minimal changes to make code base more compatible Trenchboot.
Jarkko Sakkinen (10):
tpm: Cap the number of PCR banks
tpm: Use -EPERM as fallback error code in tpm_ret_to_err
KEYS: trusted: Fix memory leak in tpm2_load()
KEYS: trusted: Use tpm_ret_to_err() in trusted_tpm2
tpm2-sessions: Remove 'attributes' from tpm_buf_append_auth
tpm2-sessions: Unmask tpm_buf_append_hmac_session()
KEYS: trusted: Open code tpm2_buf_append()
tpm-buf: unify TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW
tpm-buf: Remove chip parameter from tpm_buf_append_handle
tpm-buf: Enable managed and stack allocations.
drivers/char/tpm/tpm-buf.c | 141 ++++++---- drivers/char/tpm/tpm-chip.c | 13 +- drivers/char/tpm/tpm-sysfs.c | 21 +- drivers/char/tpm/tpm.h | 2 - drivers/char/tpm/tpm1-cmd.c | 185 +++++-------- drivers/char/tpm/tpm2-cmd.c | 313 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 129 +++++---- drivers/char/tpm/tpm2-space.c | 44 ++- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +-- include/linux/tpm.h | 65 ++--- security/keys/trusted-keys/trusted_tpm1.c | 34 ++- security/keys/trusted-keys/trusted_tpm2.c | 267 ++++++++---------- 12 files changed, 554 insertions(+), 690 deletions(-)
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 1/10 tpm: Cap the number of PCR banks
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation() does not cap any upper limit for the number of banks. Cap the limit to four banks so that out of bounds values coming from external I/O cause on only limited harm.
---
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes from include/linux/tpm.h.
- Increase TPM2_MAX_BANKS to 8.
- Rename TPM2_MAX_BANKS as TPM2_MAX_PCR_BANKS for the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixed pr_err() message.
v2:
- A new patch.
--- drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)
drivers/char/tpm/tpm-chip.c +9 -4
@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;
drivers/char/tpm/tpm.h +0 -1
@@ -252,7 +252,6 @@ int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);ssize_t tpm1_getcap (struct tpm_chip *chip,u32 subcap_id,cap_t *cap,const char *desc,size_t min_cap_length);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);
drivers/char/tpm/tpm1-cmd.c +0 -25
@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}
drivers/char/tpm/tpm2-cmd.c +3 -5
@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }
include/linux/tpm.h +5 -3
@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;
@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {
@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];
Signature · 1 line
2.39.5
Re: [PATCH v6 01/10] tpm: Cap the number of PCR banks
Quoted 5 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to four banks so that out of bounds values coming from external I/O cause on only limited harm.
Comment no longer matches code - we cap at 8. With the comment fixed:
Quoted 152 lines
Cc Roberto Sassu <[email protected]>Fixes bcfff8384f6c ("tpm: dynamically allocate the allocated_banks array")Signed-off-by Jarkko Sakkinen <[email protected]>---
v6
- No changes.
v5:
- No changes.
v4:
- Revert spurious changes frominclude/linux/tpm.h.
- IncreaseTPM2_MAX_BANKSto 8.
- RenameTPM2_MAX_BANKSasTPM2_MAX_PCR_BANKSfor the sake of clarity. v3:
- Wrote a more clear commit message.
- Fixedpr_err()message.
v2:
- A new patch.
---drivers/char/tpm/tpm-chip.c | 13 +++++++++---- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 25 ------------------------- drivers/char/tpm/tpm2-cmd.c | 8 +++----- include/linux/tpm.h | 8 +++++--- 5 files changed, 17 insertions(+), 38 deletions(-)drivers/char/tpm/tpm-chip.c +9
-4@@ -559,14 +559,19 @@ static int tpm_add_hwrng(struct tpm_chip *chip)static int tpm_get_pcr_allocation (struct tpm_chip *chip) { -int rc; +int rc =0 ;if (tpm_is_firmware_upgrade (chip))return 0 ; - rc = (chip->flags &TPM_CHIP_FLAG_TPM2 ) ? -tpm2_get_pcr_allocation (chip) : -tpm1_get_pcr_allocation (chip); +if (!(chip->flags &TPM_CHIP_FLAG_TPM2 )) { + chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; + chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; + chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; + chip->nr_allocated_banks =1 ; + }else { + rc =tpm2_get_pcr_allocation (chip); + }if (rc >0 )return -ENODEV ;drivers/char/tpm/tpm.h +0
-1@@ -252,7 +252,6 @@ int tpm1_pcr_read(struct tpm_chip *chip, u32 pcr_idx, u8 *res_buf);ssize_t tpm1_getcap (struct tpm_chip *chip,u32 subcap_id,cap_t *cap,const char *desc,size_t min_cap_length);int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max); -int tpm1_get_pcr_allocation (struct tpm_chip *chip);unsigned long tpm_calc_ordinal_duration (struct tpm_chip *chip,u32 ordinal);int tpm_pm_suspend (struct device *dev);int tpm_pm_resume (struct device *dev);drivers/char/tpm/tpm1-cmd.c +0
-25@@ -786,28 +786,3 @@ int tpm1_pm_suspend(struct tpm_chip *chip, u32 tpm_suspend_pcr)return rc; } - -/** -* tpm1_get_pcr_allocation() - initialize the allocated bank -* @chip: TPM chip to use. -* -* The function initializes the SHA1 allocated bank to extend PCR -* -* Return: -* * 0 on success, -* * < 0 on error. -*/ -int tpm1_get_pcr_allocation (struct tpm_chip *chip) -{ - chip->allocated_banks =kcalloc (1 ,sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) -return -ENOMEM ; - - chip->allocated_banks[0 ].alg_id =TPM_ALG_SHA1 ; - chip->allocated_banks[0 ].digest_size = hash_digest_size[HASH_ALGO_SHA1 ]; - chip->allocated_banks[0 ].crypto_id =HASH_ALGO_SHA1 ; - chip->nr_allocated_banks =1 ; - -return 0 ; -}drivers/char/tpm/tpm2-cmd.c +3
-5@@ -538,11 +538,9 @@ ssize_t tpm2_get_pcr_allocation(struct tpm_chip *chip) nr_possible_banks =be32_to_cpup ( (__be32 *)&buf.data[TPM_HEADER_SIZE +5 ]); - - chip->allocated_banks =kcalloc (nr_possible_banks, -sizeof (*chip->allocated_banks), -GFP_KERNEL ); -if (!chip->allocated_banks) { +if (nr_possible_banks >TPM2_MAX_PCR_BANKS ) { +pr_err ("tpm: unexpected number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_PCR_BANKS ); rc = -ENOMEM ; goto out; }include/linux/tpm.h +5
-3@@ -26,7 +26,9 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_PCR_BANKS 8struct tpm_chip ;struct trusted_key_payload ;@@ -68,7 +70,7 @@ enum tpm2_curves {struct tpm_digest {u16 alg_id; -u8 digest[TPM_MAX_DIGEST_SIZE ]; +u8 digest[TPM2_MAX_DIGEST_SIZE ]; } __packed;struct tpm_bank_info {@@ -189,7 +191,7 @@ struct tpm_chip {unsigned int groups_cnt;u32 nr_allocated_banks; -struct tpm_bank_info *allocated_banks; +struct tpm_bank_info allocated_banks[TPM2_MAX_PCR_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];Signature · 1 line
2.39.5
J.
Signature · 3 lines
Is it real, or is it Mimozine? This .sig brought to you by the letter N and the number 9 Product of the Republic of HuggieTag
Jarkko Sakkinen
maintainerRe: [PATCH v6 01/10] tpm: Cap the number of PCR banks
Quoted 10 lines · Jonathan McDowell
Quoted 5 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation()does not cap any upper limit for the number of banks. Cap the limit to four banks so that out of bounds values coming from external I/O cause on only limited harm.Comment no longer matches code - we cap at 8. With the comment fixed:
Reviewed-by Jonathan McDowell <[email protected]>
Oops, sorry I'll address that and add your tag! Thank you.
BR, Jarkko
Jarkko Sakkinen
maintainerPATCH v6 2/10 tpm: Use -EPERM as fallback error code in tpm_ret_to_err
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Using -EFAULT as the tpm_ret_to_err() fallback error code causes makes it incompatible on how trusted keys transmute TPM return codes.
Change the fallback as -EPERM in order to gain compatibility with trusted keys. In addition, map TPM_RC_HASH to -EINVAL in order to be compatible with tpm2_seal_trusted() return values.
---
v6:
- No changes.
v5:
- No changes.
v4:
- No changes.
v3:
- Removed fixes tag as it hardly categorizes as a bug fix. v2:
- Split trusted_tpm2 change to a separate patch.
--- include/linux/tpm.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/tpm.h +3 -1
@@ -456,8 +456,10 @@ static inline ssize_t tpm_ret_to_err(ssize_t ret)return 0 ;case TPM2_RC_SESSION_MEMORY :return -ENOMEM ; +case TPM2_RC_HASH : +return -EINVAL ;default : -return -EFAULT ; +return -EPERM ; } }
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 3/10 KEYS: trusted: Fix memory leak in tpm2_load()
In reply to Jarkko Sakkinentpm2_load() allocates a blob indirectly via tpm2_key_decode() but it is not freed in all failure paths. Address this with a scope-based cleanup helper __free(). For legacy blobs, the implicit de-allocation is gets disable by no_free_ptr().
---
v6:
- A new patch in this version.
--- security/keys/trusted-keys/trusted_tpm2.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +10 -13
@@ -106,9 +106,8 @@ struct tpm2_key_context {u32 priv_len; }; -static int tpm2_key_decode (struct trusted_key_payload *payload, -struct trusted_key_options *options, -u8 **buf) +static void *tpm2_key_decode (struct trusted_key_payload *payload, +struct trusted_key_options *options) {int ret;struct tpm2_key_context ctx;
@@ -119,16 +118,15 @@ static int tpm2_key_decode(struct trusted_key_payload *payload, ret =asn1_ber_decoder (&tpm2key_decoder, &ctx, payload->blob, payload->blob_len);if (ret <0 ) -return ret; +return ERR_PTR (ret);if (ctx.priv_len + ctx.pub_len >MAX_BLOB_SIZE ) -return -EINVAL ; +return ERR_PTR (-EINVAL ); blob =kmalloc (ctx.priv_len + ctx.pub_len +4 ,GFP_KERNEL );if (!blob) -return -ENOMEM ; +return ERR_PTR (-ENOMEM ); - *buf = blob; options->keyhandle = ctx.parent;memcpy (blob, ctx.priv, ctx.priv_len);
@@ -136,7 +134,7 @@ static int tpm2_key_decode(struct trusted_key_payload *payload,memcpy (blob, ctx.pub, ctx.pub_len); -return 0 ; +return blob; } int tpm2_key_parent(void *context, size_t hdrlen,
@@ -391,13 +389,14 @@ static int tpm2_load_cmd(struct tpm_chip *chip,unsigned int private_len;unsigned int public_len;unsigned int blob_len; -u8 *blob, *pub; +u8 *pub;int rc;u32 attrs; - rc =tpm2_key_decode (payload, options, &blob); -if (rc) { + u8 *blob __free(kfree) = tpm2_key_decode(payload, options); +if (IS_ERR (blob)) {/* old form */ +no_free_ptr (blob); blob = payload->blob; payload->old_format =1 ; }
@@ -464,8 +463,6 @@ static int tpm2_load_cmd(struct tpm_chip *chip, (__be32 *) &buf.data[TPM_HEADER_SIZE]); out: -if (blob != payload->blob) -kfree (blob);tpm_buf_destroy (&buf);if (rc >0 )
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 4/10 KEYS: trusted: Use tpm_ret_to_err() in trusted_tpm2
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Use tpm_ret_to_err() to transmute TPM return codes in trusted_tpm2.
---
v6:
- No changes.
v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
v2:
- New patch split out from the fix.
--- security/keys/trusted-keys/trusted_tpm2.c | 26 ++++++----------------- 1 file changed, 7 insertions(+), 19 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +7 -19
@@ -346,25 +346,19 @@ int tpm2_seal_trusted(struct tpm_chip *chip, } blob_len =tpm2_key_encode (payload, options, &buf.data[offset], blob_len); +if (blob_len <0 ) + rc = blob_len; out:tpm_buf_destroy (&sized);tpm_buf_destroy (&buf); -if (rc >0 ) { -if (tpm2_rc_value (rc) ==TPM2_RC_HASH ) - rc = -EINVAL ; -else - rc = -EPERM ; - } -if (blob_len <0 ) - rc = blob_len; -else +if (!rc) payload->blob_len = blob_len; out_put:tpm_put_ops (chip); -return rc; +return tpm_ret_to_err (rc); }/**
@@ -465,10 +459,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip, out:tpm_buf_destroy (&buf); -if (rc >0 ) - rc = -EPERM ; - -return rc; +return tpm_ret_to_err (rc); }/**
@@ -531,8 +522,6 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,tpm_buf_fill_hmac_session (chip, &buf); rc =tpm_transmit_cmd (chip, &buf,6 ,"unsealing" ); rc =tpm_buf_check_hmac_response (chip, &buf, rc); -if (rc >0 ) - rc = -EPERM ;if (!rc) { data_len = be16_to_cpup(
@@ -565,7 +554,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, out:tpm_buf_destroy (&buf); -return rc; +return tpm_ret_to_err (rc); }/**
@@ -597,6 +586,5 @@ int tpm2_unseal_trusted(struct tpm_chip *chip, out:tpm_put_ops (chip); - -return rc; +return tpm_ret_to_err (rc); }
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 5/10 tpm2-sessions: Remove 'attributes' from tpm_buf_append_auth
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
In a previous bug fix, 'attributes' was added by mistake to tpm_buf_append_auth(). Remove the parameter.
---
v6:
- No changes.
v5:
- No changes.
v4:
- No changes.
v3:
- No changes
v2:
- Uncorrupt the patch.
--- drivers/char/tpm/tpm2-cmd.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 5 ++--- include/linux/tpm.h | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-)
drivers/char/tpm/tpm2-cmd.c +1 -1
@@ -191,7 +191,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,tpm_buf_append_hmac_session (chip, &buf,0 ,NULL ,0 ); }else {tpm_buf_append_handle (chip, &buf, pcr_idx); -tpm_buf_append_auth (chip, &buf,0 ,NULL ,0 ); +tpm_buf_append_auth (chip, &buf,NULL ,0 ); }tpm_buf_append_u32 (&buf, chip->nr_allocated_banks);
drivers/char/tpm/tpm2-sessions.c +2 -3
@@ -266,7 +266,7 @@ void tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,EXPORT_SYMBOL_GPL (tpm_buf_append_name);void tpm_buf_append_auth (struct tpm_chip *chip,struct tpm_buf *buf, -u8 attributes,u8 *passphrase,int passphrase_len) +u8 *passphrase,int passphrase_len) {/* offset tells us where the sessions area begins */ int offset = buf->handles *4 +TPM_HEADER_SIZE ;
@@ -327,8 +327,7 @@ void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf,#endif if (!tpm2_chip_auth (chip)) { -tpm_buf_append_auth (chip, buf, attributes, passphrase, - passphrase_len); +tpm_buf_append_auth (chip, buf, passphrase, passphrase_len);return ; }
include/linux/tpm.h +1 -1
@@ -534,7 +534,7 @@ void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf, u8 attributes, u8 *passphrase, int passphraselen);void tpm_buf_append_auth (struct tpm_chip *chip,struct tpm_buf *buf, -u8 attributes,u8 *passphrase,int passphraselen); +u8 *passphrase,int passphraselen); static inline void tpm_buf_append_hmac_session_opt(struct tpm_chip *chip, struct tpm_buf *buf, u8 attributes,
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 6/10 tpm2-sessions: Unmask tpm_buf_append_hmac_session()
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Open code tpm_buf_append_hmac_session_opt() in order to unmask the code paths in the call sites of tpm_buf_append_hmac_session().
---
v6:
- No changes.
v5:
- No changes.
v4:
- Fixed typo in short summary.
v3:
- No changes.
v2:
- Uncorrupt the patch.
--- drivers/char/tpm/tpm2-cmd.c | 14 +++++++++++--- include/linux/tpm.h | 23 ----------------------- security/keys/trusted-keys/trusted_tpm2.c | 12 ++++++++++-- 3 files changed, 21 insertions(+), 28 deletions(-)
drivers/char/tpm/tpm2-cmd.c +11 -3
@@ -257,9 +257,17 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) do { tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_GET_RANDOM); - tpm_buf_append_hmac_session_opt(chip, &buf, TPM2_SA_ENCRYPT - | TPM2_SA_CONTINUE_SESSION, - NULL, 0); + if (tpm2_chip_auth(chip)) { + tpm_buf_append_hmac_session(chip, &buf, + TPM2_SA_ENCRYPT | + TPM2_SA_CONTINUE_SESSION, + NULL, 0); + } else { + offset = buf.handles * 4 + TPM_HEADER_SIZE; + head = (struct tpm_header *)buf.data; + if (tpm_buf_length(&buf) == offset) + head->tag = cpu_to_be16(TPM2_ST_NO_SESSIONS); + } tpm_buf_append_u16(&buf, num_bytes); tpm_buf_fill_hmac_session(chip, &buf); err = tpm_transmit_cmd(chip, &buf,
include/linux/tpm.h +0 -23
@@ -535,29 +535,6 @@ void tpm_buf_append_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf, int passphraselen);void tpm_buf_append_auth (struct tpm_chip *chip,struct tpm_buf *buf,u8 *passphrase,int passphraselen); -static inline void tpm_buf_append_hmac_session_opt (struct tpm_chip *chip, -struct tpm_buf *buf, -u8 attributes, -u8 *passphrase, -int passphraselen) -{ -struct tpm_header *head; -int offset; - -if (tpm2_chip_auth (chip)) { -tpm_buf_append_hmac_session (chip, buf, attributes, passphrase, passphraselen); - }else { - offset = buf->handles *4 +TPM_HEADER_SIZE ; - head = (struct tpm_header *)buf->data; - -/* -* If the only sessions are optional, the command tag must change to -* TPM2_ST_NO_SESSIONS. -*/ -if (tpm_buf_length (buf) == offset) - head->tag =cpu_to_be16 (TPM2_ST_NO_SESSIONS ); - } -}#ifdef CONFIG_TCG_TPM2_HMAC
security/keys/trusted-keys/trusted_tpm2.c +10 -2
@@ -479,8 +479,10 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, struct trusted_key_options *options, u32 blob_handle) { +struct tpm_header *head;struct tpm_buf buf;u16 data_len; +int offset;u8 *data;int rc;
@@ -515,8 +517,14 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,tpm2_buf_append_auth (&buf, options->policyhandle,NULL /* nonce */ ,0 ,0 , options->blobauth, options->blobauth_len); -tpm_buf_append_hmac_session_opt (chip, &buf,TPM2_SA_ENCRYPT , -NULL ,0 ); +if (tpm2_chip_auth (chip)) { +tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_ENCRYPT ,NULL ,0 ); + }else { + offset = buf.handles *4 +TPM_HEADER_SIZE ; + head = (struct tpm_header *)buf.data; +if (tpm_buf_length (&buf) == offset) + head->tag =cpu_to_be16 (TPM2_ST_NO_SESSIONS ); + } }tpm_buf_fill_hmac_session (chip, &buf);
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 7/10 KEYS: trusted: Open code tpm2_buf_append()
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
tpm2_buf_append_auth() has only single call site and most of its parameters are redundant. Open code it to the call site. Remove illegit FIXME comment as there is no categorized bug and replace it with more sane comment about implementation (i.e. "non-opionated inline comment").
---
v6:
- No changes.
v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
v2:
- No changes.
--- security/keys/trusted-keys/trusted_tpm2.c | 51 ++++------------------- 1 file changed, 9 insertions(+), 42 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +9 -42
@@ -196,36 +196,6 @@ int tpm2_key_priv(void *context, size_t hdrlen,return 0 ; } -/** -* tpm2_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer. -* -* @buf: an allocated tpm_buf instance -* @session_handle: session handle -* @nonce: the session nonce, may be NULL if not used -* @nonce_len: the session nonce length, may be 0 if not used -* @attributes: the session attributes -* @hmac: the session HMAC or password, may be NULL if not used -* @hmac_len: the session HMAC or password length, maybe 0 if not used -*/ -static void tpm2_buf_append_auth (struct tpm_buf *buf,u32 session_handle, -const u8 *nonce,u16 nonce_len, -u8 attributes, -const u8 *hmac,u16 hmac_len) -{ -tpm_buf_append_u32 (buf,9 + nonce_len + hmac_len); -tpm_buf_append_u32 (buf, session_handle); -tpm_buf_append_u16 (buf, nonce_len); - -if (nonce && nonce_len) -tpm_buf_append (buf, nonce, nonce_len); - -tpm_buf_append_u8 (buf, attributes); -tpm_buf_append_u16 (buf, hmac_len); - -if (hmac && hmac_len) -tpm_buf_append (buf, hmac, hmac_len); -} -/** * tpm2_seal_trusted() - seal the payload of a trusted key *
@@ -504,19 +474,16 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, options->blobauth_len); }else {/* -* FIXME: The policy session was generated outside the -* kernel so we don't known the nonce and thus can't -* calculate a HMAC on it. Therefore, the user can -* only really use TPM2_PolicyPassword and we must -* send down the plain text password, which could be -* intercepted. We can still encrypt the returned -* key, but that's small comfort since the interposer -* could repeat our actions with the exfiltrated -* password. +* The policy session is generated outside the kernel, and thus +* the password will end up being unencrypted on the bus, as +* HMAC nonce cannot be calculated for it. */ -tpm2_buf_append_auth (&buf, options->policyhandle, -NULL /* nonce */ ,0 ,0 , - options->blobauth, options->blobauth_len); +tpm_buf_append_u32 (&buf,9 + options->blobauth_len); +tpm_buf_append_u32 (&buf, options->policyhandle); +tpm_buf_append_u16 (&buf,0 ); +tpm_buf_append_u8 (&buf,0 ); +tpm_buf_append_u16 (&buf, options->blobauth_len); +tpm_buf_append (&buf, options->blobauth, options->blobauth_len);if (tpm2_chip_auth (chip)) {tpm_buf_append_hmac_session (chip, &buf,TPM2_SA_ENCRYPT ,NULL ,0 ); }else {
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 8/10 tpm-buf: unify TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Unify TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW into TPM_BUF_INVALID, given that they are identical. The only difference are the log messages.
In addition, add a missing TPM_BUF_INVALID check to tpm_buf_append_handle() following the pattern from other functions in tpm-buf.c.
---
v6:
- Change short summary to something more descriptive. v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
v2:
- A new patch.
--- drivers/char/tpm/tpm-buf.c | 14 ++++++++------ include/linux/tpm.h | 8 +++----- security/keys/trusted-keys/trusted_tpm2.c | 6 +++--- 3 files changed, 14 insertions(+), 14 deletions(-)
drivers/char/tpm/tpm-buf.c +8 -6
@@ -104,13 +104,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_length);*/ void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length) { -/* Return silently if overflow has already happened. */ -if (buf->flags &TPM_BUF_OVERFLOW ) +if (buf->flags &TPM_BUF_INVALID )return ;if ((buf->length + new_length) >PAGE_SIZE ) {WARN (1 ,"tpm_buf: write overflow\n" ); - buf->flags |=TPM_BUF_OVERFLOW ; + buf->flags |=TPM_BUF_INVALID ;return ; }
@@ -157,8 +156,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_u32);*/ void tpm_buf_append_handle (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle) { +if (buf->flags &TPM_BUF_INVALID ) +return ; +if (buf->flags &TPM_BUF_TPM2B ) {dev_err (&chip->dev,"Invalid buffer type (TPM2B)\n" ); + buf->flags |=TPM_BUF_INVALID ;return ; }
@@ -177,14 +180,13 @@ static void tpm_buf_read(struct tpm_buf *buf, off_t *offset, size_t count, void {off_t next_offset; -/* Return silently if overflow has already happened. */ -if (buf->flags &TPM_BUF_BOUNDARY_ERROR ) +if (buf->flags &TPM_BUF_INVALID )return ; next_offset = *offset + count;if (next_offset > buf->length) {WARN (1 ,"tpm_buf: read out of boundary\n" ); - buf->flags |=TPM_BUF_BOUNDARY_ERROR ; + buf->flags |=TPM_BUF_INVALID ;return ; }
include/linux/tpm.h +3 -5
@@ -369,12 +369,10 @@ struct tpm_header { } __packed;enum tpm_buf_flags { -/* the capacity exceeded: */ -TPM_BUF_OVERFLOW =BIT (0 ),/* TPM2B format: */ -TPM_BUF_TPM2B =BIT (1 ), -/* read out of boundary: */ -TPM_BUF_BOUNDARY_ERROR =BIT (2 ), +TPM_BUF_TPM2B =BIT (0 ), +/* The buffer is in invalid and unusable state: */ +TPM_BUF_INVALID =BIT (1 ), };/*
security/keys/trusted-keys/trusted_tpm2.c +3 -3
@@ -293,7 +293,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,/* creation PCR */ tpm_buf_append_u32 (&buf,0 ); -if (buf.flags &TPM_BUF_OVERFLOW ) { +if (buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ;tpm2_end_auth_session (chip); goto out;
@@ -306,7 +306,7 @@ 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_BOUNDARY_ERROR ) { +if (blob_len >MAX_BLOB_SIZE || buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ; goto out; }
@@ -413,7 +413,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,tpm_buf_append (&buf, blob, blob_len); -if (buf.flags &TPM_BUF_OVERFLOW ) { +if (buf.flags &TPM_BUF_INVALID ) { rc = -E2BIG ;tpm2_end_auth_session (chip); goto out;
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 9/10 tpm-buf: Remove chip parameter from tpm_buf_append_handle
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Remove @chip from tpm_buf_append_handle() in order to decouple it from driver. This allows tpm-buf.c to be compiled as part of early boot code, thus providing a mechanism to build and parse TPM data.
E.g., Trenchboot can use this to manage TPM2_PcrExtend protocol data as part of its early boot blob simply by compiling and linking the object file.
---
v6:
- Rewrote the commit message.
v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
v2:
- No changes.
--- drivers/char/tpm/tpm-buf.c | 5 ++--- drivers/char/tpm/tpm2-cmd.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 2 +- include/linux/tpm.h | 2 +- 4 files changed, 5 insertions(+), 6 deletions(-)
drivers/char/tpm/tpm-buf.c +2 -3
@@ -147,20 +147,19 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_u32);/** * tpm_buf_append_handle() - Add a handle -* @chip: &tpm_chip instance * @buf: &tpm_buf instance * @handle: a TPM object handle * * Add a handle to the buffer, and increase the count tracking the number of * handles in the command buffer. Works only for command buffers. */ -void tpm_buf_append_handle (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle) +void tpm_buf_append_handle (struct tpm_buf *buf,u32 handle) {if (buf->flags &TPM_BUF_INVALID )return ;if (buf->flags &TPM_BUF_TPM2B ) { -dev_err (&chip->dev,"Invalid buffer type (TPM2B)\n" ); +WARN (1 ,"tpm-buf: invalid type: TPM2B\n" ); buf->flags |=TPM_BUF_INVALID ;return ; }
drivers/char/tpm/tpm2-cmd.c +1 -1
@@ -190,7 +190,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,tpm_buf_append_name (chip, &buf, pcr_idx,NULL );tpm_buf_append_hmac_session (chip, &buf,0 ,NULL ,0 ); }else { -tpm_buf_append_handle (chip, &buf, pcr_idx); +tpm_buf_append_handle (&buf, pcr_idx);tpm_buf_append_auth (chip, &buf,NULL ,0 ); }
drivers/char/tpm/tpm2-sessions.c +1 -1
@@ -232,7 +232,7 @@ void tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,#endif if (!tpm2_chip_auth (chip)) { -tpm_buf_append_handle (chip, buf, handle); +tpm_buf_append_handle (buf, handle);return ; }
include/linux/tpm.h +1 -1
@@ -426,7 +426,7 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value);u8 tpm_buf_read_u8 (struct tpm_buf *buf,off_t *offset);u16 tpm_buf_read_u16 (struct tpm_buf *buf,off_t *offset);u32 tpm_buf_read_u32 (struct tpm_buf *buf,off_t *offset); -void tpm_buf_append_handle (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle); +void tpm_buf_append_handle (struct tpm_buf *buf,u32 handle);/* * Check if TPM device is in the firmware upgrade mode.
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 10/10 tpm-buf: Enable managed and stack allocations.
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Decouple kzalloc from buffer creation, so that a managed allocation can be done trivially:
struct tpm_buf *buf __free(kfree) buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); if (!buf) return -ENOMEM; tpm_buf_init(buf, TPM_BUFSIZE);
Alternatively, stack allocations are also possible:
u8 buf_data[512]; struct tpm_buf *buf = (struct tpm_buf *)buf_data; tpm_buf_init(buf, sizeof(buf_data));
Given that every single tpm_transmit_cmd() call site needs to be changed, place command names from TCG 1.2 and 2.0 specifications to the @dest parameter, which will e.g., help tracing bugs.
---
v6
- Update commit message.
v5:
- There was a spurious change in tpm2_seal_trusted() error code handling introduce by this patch.
v4:
- Since every single tpm_transmit_cmd() call site needs to be changed anyhow, use 'dest' parameter more structured and actually useful way, and pick the string TCG 1.2 and 2.0 specifications.
- tpm1-cmd: Remove useless rc declarations and repliace them with trivial "return tpm_transmit_cmd" statement.
- Reverted spurious changes in include/linux/tpm.h.
- Use concisely TPM_BUFSIZE instead of PAGE_SIZE.
v3:
- A new patch from the earlier series with more scoped changes and less abstract commit message.
--- drivers/char/tpm/tpm-buf.c | 122 +++++---- drivers/char/tpm/tpm-sysfs.c | 21 +- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 162 +++++------- drivers/char/tpm/tpm2-cmd.c | 299 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 122 +++++---- drivers/char/tpm/tpm2-space.c | 44 ++-- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +-- include/linux/tpm.h | 18 +- security/keys/trusted-keys/trusted_tpm1.c | 34 ++- security/keys/trusted-keys/trusted_tpm2.c | 175 ++++++------- 11 files changed, 484 insertions(+), 544 deletions(-)
drivers/char/tpm/tpm-buf.c +77 -45
@@ -7,82 +7,109 @@#include <linux/module.h> #include <linux/tpm.h> -/** -* tpm_buf_init() - Allocate and initialize a TPM command -* @buf: A &tpm_buf -* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS -* @ordinal: A command ordinal -* -* Return: 0 or -ENOMEM -*/ -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal) +static void __tpm_buf_size_invariant (struct tpm_buf *buf,u16 buf_size) { - buf->data = (u8 *)__get_free_page (GFP_KERNEL ); -if (!buf->data) -return -ENOMEM ; - -tpm_buf_reset (buf, tag, ordinal); -return 0 ; +u32 buf_size_2 = (u32 )buf->capacity + (u32 )sizeof (*buf); + +if (!buf->capacity) { +if (buf_size >TPM_BUFSIZE ) { +WARN (1 ,"%s: size overflow: %u\n" , __func__, buf_size); + buf->flags |=TPM_BUF_INVALID ; + } + }else { +if (buf_size != buf_size_2) { +WARN (1 ,"%s: size mismatch: %u != %u\n" , __func__, buf_size, + buf_size_2); + buf->flags |=TPM_BUF_INVALID ; + } + } } -EXPORT_SYMBOL_GPL (tpm_buf_init); -/** -* tpm_buf_reset() - Initialize a TPM command -* @buf: A &tpm_buf -* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS -* @ordinal: A command ordinal -*/ -void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal) +static void __tpm_buf_reset (struct tpm_buf *buf,u16 buf_size,u16 tag,u32 ordinal) {struct tpm_header *head = (struct tpm_header *)buf->data; +__tpm_buf_size_invariant (buf, buf_size); + +if (buf->flags &TPM_BUF_INVALID ) +return ; +WARN_ON (tag !=TPM_TAG_RQU_COMMAND && tag !=TPM2_ST_NO_SESSIONS && tag !=TPM2_ST_SESSIONS && tag !=0 ); buf->flags =0 ; buf->length =sizeof (*head); + buf->capacity = buf_size -sizeof (*buf); + buf->handles =0 ; head->tag =cpu_to_be16 (tag); head->length =cpu_to_be32 (sizeof (*head)); head->ordinal =cpu_to_be32 (ordinal); +} + +static void __tpm_buf_reset_sized (struct tpm_buf *buf,u16 buf_size) +{ +__tpm_buf_size_invariant (buf, buf_size); + +if (buf->flags &TPM_BUF_INVALID ) +return ; + + buf->flags =TPM_BUF_TPM2B ; + buf->length =2 ; + buf->capacity = buf_size -sizeof (*buf); buf->handles =0 ; + buf->data[0 ] =0 ; + buf->data[1 ] =0 ; } -EXPORT_SYMBOL_GPL (tpm_buf_reset);/** -* tpm_buf_init_sized() - Allocate and initialize a sized (TPM2B) buffer -* @buf: A @tpm_buf -* -* Return: 0 or -ENOMEM +* tpm_buf_init() - Initialize a TPM command +* @buf: A &tpm_buf +* @buf_size: Size of the buffer. */ -int tpm_buf_init_sized (struct tpm_buf *buf) +void tpm_buf_init (struct tpm_buf *buf,u16 buf_size) { - buf->data = (u8 *)__get_free_page (GFP_KERNEL ); -if (!buf->data) -return -ENOMEM ; +memset (buf,0 , buf_size); +__tpm_buf_reset (buf, buf_size,TPM_TAG_RQU_COMMAND ,0 ); +} +EXPORT_SYMBOL_GPL (tpm_buf_init); -tpm_buf_reset_sized (buf); -return 0 ; +/** +* tpm_buf_init_sized() - Initialize a sized buffer +* @buf: A &tpm_buf +* @buf_size: Size of the buffer. +*/ +void tpm_buf_init_sized (struct tpm_buf *buf,u16 buf_size) +{ +memset (buf,0 , buf_size); +__tpm_buf_reset_sized (buf, buf_size); }EXPORT_SYMBOL_GPL (tpm_buf_init_sized);/** -* tpm_buf_reset_sized() - Initialize a sized buffer +* tpm_buf_reset() - Re-initialize a TPM command * @buf: A &tpm_buf +* @tag: TPM_TAG_RQU_COMMAND, TPM2_ST_NO_SESSIONS or TPM2_ST_SESSIONS +* @ordinal: A command ordinal */ -void tpm_buf_reset_sized (struct tpm_buf *buf) +void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal) { - buf->flags =TPM_BUF_TPM2B ; - buf->length =2 ; - buf->data[0 ] =0 ; - buf->data[1 ] =0 ; +u16 buf_size = buf->capacity +sizeof (*buf); + +__tpm_buf_reset (buf, buf_size, tag, ordinal); } -EXPORT_SYMBOL_GPL (tpm_buf_reset_sized); +EXPORT_SYMBOL_GPL (tpm_buf_reset); -void tpm_buf_destroy (struct tpm_buf *buf) +/** +* tpm_buf_reset_sized() - Re-initialize a sized buffer +* @buf: A &tpm_buf +*/ +void tpm_buf_reset_sized (struct tpm_buf *buf) { -free_page ((unsigned long )buf->data); +u16 buf_size = buf->capacity +sizeof (*buf); + +__tpm_buf_reset_sized (buf, buf_size); } -EXPORT_SYMBOL_GPL (tpm_buf_destroy); +EXPORT_SYMBOL_GPL (tpm_buf_reset_sized);/** * tpm_buf_length() - Return the number of bytes consumed by the data
@@ -92,6 +119,9 @@ EXPORT_SYMBOL_GPL(tpm_buf_destroy);*/ u32 tpm_buf_length (struct tpm_buf *buf) { +if (buf->flags &TPM_BUF_INVALID ) +return 0 ; +return buf->length; }EXPORT_SYMBOL_GPL (tpm_buf_length);
@@ -104,10 +134,12 @@ EXPORT_SYMBOL_GPL(tpm_buf_length);*/ void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length) { +u32 total_length = (u32 )buf->length + (u32 )new_length; +if (buf->flags &TPM_BUF_INVALID )return ; -if ((buf->length + new_length) >PAGE_SIZE ) { +if (total_length > (u32 )buf->capacity) {WARN (1 ,"tpm_buf: write overflow\n" ); buf->flags |=TPM_BUF_INVALID ;return ;
drivers/char/tpm/tpm-sysfs.c +10 -11
@@ -32,28 +32,29 @@ struct tpm_readpubek_out {static ssize_t pubek_show (struct device *dev,struct device_attribute *attr,char *buf) { -struct tpm_buf tpm_buf;struct tpm_readpubek_out *out;int i;char *str = buf;struct tpm_chip *chip =to_tpm_chip (dev);char anti_replay[20 ]; + struct tpm_buf *tpm_buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tpm_buf) +return -ENOMEM ; +memset (&anti_replay,0 ,sizeof (anti_replay));if (tpm_try_get_ops (chip))return 0 ; -if (tpm_buf_init (&tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK )) - goto out_ops; - -tpm_buf_append (&tpm_buf, anti_replay,sizeof (anti_replay)); +tpm_buf_init (tpm_buf,TPM_BUFSIZE ); +tpm_buf_reset (tpm_buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_READPUBEK ); +tpm_buf_append (tpm_buf, anti_replay,sizeof (anti_replay)); -if (tpm_transmit_cmd (chip, &tpm_buf,READ_PUBEK_RESULT_MIN_BODY_SIZE , -"attempting to read the PUBEK" )) - goto out_buf; +if (tpm_transmit_cmd (chip, tpm_buf,READ_PUBEK_RESULT_MIN_BODY_SIZE ,"TPM_ReadPubek" )) + goto out_ops; - out = (struct tpm_readpubek_out *)&tpm_buf.data[10 ]; + out = (struct tpm_readpubek_out *)&tpm_buf->data[10 ]; str +=sprintf (str, "Algorithm: %4ph\n"
@@ -71,8 +72,6 @@ static ssize_t pubek_show(struct device *dev, struct device_attribute *attr,for (i =0 ; i <256 ; i +=16 ) str +=sprintf (str,"%16ph\n" , &out->modulus[i]); -out_buf: -tpm_buf_destroy (&tpm_buf); out_ops:tpm_put_ops (chip);return str - buf;
drivers/char/tpm/tpm.h +0 -1
@@ -32,7 +32,6 @@#endif #define TPM_MINOR 224/* officially assigned */ -#define TPM_BUFSIZE 4096#define TPM_NUM_DEVICES 65536#define TPM_RETRY 50
drivers/char/tpm/tpm1-cmd.c +68 -94
271 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +136 -163
535 lines not on this page · raw message
drivers/char/tpm/tpm2-sessions.c +58 -64
224 lines not on this page · raw message
drivers/char/tpm/tpm2-space.c +18 -26
106 lines not on this page · raw message
drivers/char/tpm/tpm_vtpm_proxy.c +13 -17
@@ -395,40 +395,36 @@ static bool vtpm_proxy_tpm_req_canceled(struct tpm_chip *chip, u8 status)static int vtpm_proxy_request_locality (struct tpm_chip *chip,int locality) { -struct tpm_buf buf;int rc;const struct tpm_header *header;struct proxy_dev *proxy_dev =dev_get_drvdata (&chip->dev); + struct tpm_buf *buf __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!buf) +return -ENOMEM ; + +tpm_buf_init (buf,TPM_BUFSIZE );if (chip->flags &TPM_CHIP_FLAG_TPM2 ) - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS , -TPM2_CC_SET_LOCALITY ); +tpm_buf_reset (buf,TPM2_ST_SESSIONS ,TPM2_CC_SET_LOCALITY );else - rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND , -TPM_ORD_SET_LOCALITY ); -if (rc) -return rc; -tpm_buf_append_u8 (&buf, locality); +tpm_buf_reset (buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_SET_LOCALITY ); + +tpm_buf_append_u8 (buf, locality); proxy_dev->state |=STATE_DRIVER_COMMAND ; - rc =tpm_transmit_cmd (chip, &buf,0 ,"attempting to set locality" ); + rc =tpm_transmit_cmd (chip, buf,0 ,"attempting to set locality" ); proxy_dev->state &= ~STATE_DRIVER_COMMAND ; -if (rc <0 ) { - locality = rc; - goto out; - } +if (rc <0 ) +return rc; - header = (const struct tpm_header *)buf.data; + header = (const struct tpm_header *)buf->data; rc =be32_to_cpu (header->return_code);if (rc) locality =-1 ; -out: -tpm_buf_destroy (&buf); -return locality; }
include/linux/tpm.h +10 -8
@@ -25,7 +25,8 @@#include <crypto/hash_info.h> #include <crypto/aes.h> -#define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ +#define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ +#define TPM_BUFSIZE 4096#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE#define TPM2_MAX_PCR_BANKS 8
@@ -376,13 +377,15 @@ enum tpm_buf_flags { };/* -* A string buffer type for constructing TPM commands. +* A buffer for constructing and parsing TPM commands, responses and sized +* (TPM2B) buffers. */ struct tpm_buf { -u32 flags; -u32 length; -u8 *data; +u8 flags;u8 handles; +u16 length; +u16 capacity; +u8 data[]; };enum tpm2_object_attributes {
@@ -413,11 +416,10 @@ struct tpm2_hash {unsigned int tpm_id; }; -int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal); +void tpm_buf_init (struct tpm_buf *buf,u16 buf_size); +void tpm_buf_init_sized (struct tpm_buf *buf,u16 buf_size);void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal); -int tpm_buf_init_sized (struct tpm_buf *buf);void tpm_buf_reset_sized (struct tpm_buf *buf); -void tpm_buf_destroy (struct tpm_buf *buf);u32 tpm_buf_length (struct tpm_buf *buf);void tpm_buf_append (struct tpm_buf *buf,const u8 *new_data,u16 new_length);void tpm_buf_append_u8 (struct tpm_buf *buf,const u8 value);
security/keys/trusted-keys/trusted_tpm1.c +15 -19
@@ -310,9 +310,8 @@ static int TSS_checkhmac2(unsigned char *buffer,* For key specific tpm requests, we will generate and send our * own TPM command packets using the drivers send function. */ -static int trusted_tpm_send (unsigned char *cmd,size_t buflen) +static int trusted_tpm_send (void *cmd,size_t buflen) { -struct tpm_buf buf;int rc;if (!chip)
@@ -322,15 +321,12 @@ static int trusted_tpm_send(unsigned char *cmd, size_t buflen)if (rc)return rc; - buf.flags =0 ; - buf.length = buflen; - buf.data = cmd;dump_tpm_buf (cmd); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sending data" ); + rc =tpm_transmit_cmd (chip, cmd,4 ,"sending data" );dump_tpm_buf (cmd); +/* Convert TPM error to -EPERM. */ if (rc >0 ) -/* TPM error */ rc = -EPERM ;tpm_put_ops (chip);
@@ -624,23 +620,23 @@ static int tpm_unseal(struct tpm_buf *tb,static int key_seal (struct trusted_key_payload *p,struct trusted_key_options *o) { -struct tpm_buf tb;int ret; - ret =tpm_buf_init (&tb,0 ,0 ); -if (ret) -return ret; + struct tpm_buf *tb __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tb) +return -ENOMEM ; + +tpm_buf_init (tb,TPM_BUFSIZE );/* include migratable flag at end of sealed key */ p->key[p->key_len] = p->migratable; - ret =tpm_seal (&tb, o->keytype, o->keyhandle, o->keyauth, + ret =tpm_seal (tb, o->keytype, o->keyhandle, o->keyauth, p->key, p->key_len +1 , p->blob, &p->blob_len, o->blobauth, o->pcrinfo, o->pcrinfo_len);if (ret <0 )pr_info ("srkseal failed (%d)\n" , ret); -tpm_buf_destroy (&tb);return ret; }
@@ -650,14 +646,15 @@ static int key_seal(struct trusted_key_payload *p,static int key_unseal (struct trusted_key_payload *p,struct trusted_key_options *o) { -struct tpm_buf tb;int ret; - ret =tpm_buf_init (&tb,0 ,0 ); -if (ret) -return ret; + struct tpm_buf *tb __free(kfree) = kzalloc(TPM_BUFSIZE, GFP_KERNEL); +if (!tb) +return -ENOMEM ; + +tpm_buf_init (tb,TPM_BUFSIZE ); - ret =tpm_unseal (&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, + ret =tpm_unseal (tb, o->keyhandle, o->keyauth, p->blob, p->blob_len, o->blobauth, p->key, &p->key_len);if (ret <0 )pr_info ("srkunseal failed (%d)\n" , ret);
@@ -665,7 +662,6 @@ static int key_unseal(struct trusted_key_payload *p,/* pull migratable flag out of sealed key */ p->migratable = p->key[--p->key_len]; -tpm_buf_destroy (&tb);return ret; }
security/keys/trusted-keys/trusted_tpm2.c +79 -96
296 lines not on this page · raw message
Signature · 1 line
2.39.5
Re: [PATCH v6 10/10] tpm-buf: Enable managed and stack allocations.
Quoted 13 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
Decouple kzalloc from buffer creation, so that a managed allocation can be done trivially:
struct tpm_buf *buf __free(kfree) buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); if (!buf) return -ENOMEM; tpm_buf_init(buf, TPM_BUFSIZE);Alternatively, stack allocations are also possible:
Quoted 7 lines
u8 buf_data[512]; struct tpm_buf *buf = (struct tpm_buf *)buf_data; tpm_buf_init(buf, sizeof(buf_data));Given that every single
tpm_transmit_cmd()call site needs to be changed, place command names from TCG 1.2 and 2.0 specifications to the @dest parameter, which will e.g., help tracing bugs.
Perhaps my previous message fell through the cracks, but I still have a couple of comments (perhaps trivial, sorry in that case) that have not been answered about this patch:
Quoted 39 lines
Signed-off-by Jarkko Sakkinen <[email protected]>Reviewed-by Stefan Berger <[email protected]>---
v6
- Update commit message.
v5:
- There was a spurious change intpm2_seal_trusted()error code handling introduce by this patch.
v4:
- Since every singletpm_transmit_cmd()call site needs to be changed anyhow, use 'dest' parameter more structured and actually useful way, and pick the string TCG 1.2 and 2.0 specifications.
- tpm1-cmd: Remove useless rc declarations and repliace them with trivial "returntpm_transmit_cmd" statement.
- Reverted spurious changes ininclude/linux/tpm.h.
- Use conciselyTPM_BUFSIZEinstead ofPAGE_SIZE.
v3:
- A new patch from the earlier series with more scoped changes and less abstract commit message.
---drivers/char/tpm/tpm-buf.c | 122 +++++---- drivers/char/tpm/tpm-sysfs.c | 21 +- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 162 +++++------- drivers/char/tpm/tpm2-cmd.c | 299 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 122 +++++---- drivers/char/tpm/tpm2-space.c | 44 ++-- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +-- include/linux/tpm.h | 18 +- security/keys/trusted-keys/trusted_tpm1.c | 34 ++- security/keys/trusted-keys/trusted_tpm2.c | 175 ++++++------- 11 files changed, 484 insertions(+), 544 deletions(-)drivers/char/tpm/tpm-buf.c +0
-0
[...]
drivers/char/tpm/tpm-buf.c EXPORT_SYMBOL_GPL(tpm_buf_destroy); · line 120
Quoted 3 lines
drivers/char/tpm/tpm-buf.c +0
-0@@ -92,6 +119,9 @@ EXPORT_SYMBOL_GPL(tpm_buf_destroy);*/ u32 tpm_buf_length (struct tpm_buf *buf)
Should we update the return value to u16?
Quoted 7 lines
drivers/char/tpm/tpm-buf.c +3
-0{ +if (buf->flags &TPM_BUF_INVALID ) +return 0 ; +return buf->length; }EXPORT_SYMBOL_GPL (tpm_buf_length);
[...]
Quoted 16 lines
security/keys/trusted-keys/trusted_tpm1.c +1
-2@@ -310,9 +310,8 @@ static int TSS_checkhmac2(unsigned char *buffer,* For key specific tpm requests, we will generate and send our * own TPM command packets using the drivers send function. */ -static int trusted_tpm_send (unsigned char *cmd,size_t buflen) +static int trusted_tpm_send (void *cmd,size_t buflen) { -struct tpm_buf buf;int rc;if (!chip)
security/keys/trusted-keys/trusted_tpm1.c static int trusted_tpm_send(unsigned char *cmd, size_t buflen) · line 325
Quoted 9 lines
security/keys/trusted-keys/trusted_tpm1.c +1
-4@@ -322,15 +321,12 @@ static int trusted_tpm_send(unsigned char *cmd, size_t buflen)if (rc)return rc; - buf.flags =0 ; - buf.length = buflen; - buf.data = cmd;dump_tpm_buf (cmd); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sending data" ); + rc =tpm_transmit_cmd (chip, cmd,4 ,"sending data" );
Is it fine here to remove the intermediate tpm_buf ?
IIUC tpm_transmit_cmd() needs a tpm_buf, while here we are passing just the "data", or in some way it's a nested tpm_buf?
security/keys/trusted-keys/trusted_tpm1.c static int trusted_tpm_send(unsigned char *cmd, size_t buflen) · line 332
Quoted 8 lines
security/keys/trusted-keys/trusted_tpm1.c +1
-1dump_tpm_buf (cmd); +/* Convert TPM error to -EPERM. */ if (rc >0 ) -/* TPM error */ rc = -EPERM ;tpm_put_ops (chip);
Thanks,
Stefano
Jarkko Sakkinen
maintainerRe: [PATCH v6 10/10] tpm-buf: Enable managed and stack allocations.
Quoted 18 lines · Stefano Garzarella
Quoted 17 lines · Jarkko Sakkinen
From: Jarkko Sakkinen <[email protected]>
Decouple kzalloc from buffer creation, so that a managed allocation can be done trivially:
struct tpm_buf *buf __free(kfree) buf = kzalloc(TPM_BUFSIZE, GFP_KERNEL); if (!buf) return -ENOMEM; tpm_buf_init(buf, TPM_BUFSIZE);Alternatively, stack allocations are also possible:
u8 buf_data[512]; struct tpm_buf *buf = (struct tpm_buf *)buf_data; tpm_buf_init(buf, sizeof(buf_data));
Quoted 7 lines
Quoted 3 lines
Given that every single
tpm_transmit_cmd()call site needs to be changed, place command names from TCG 1.2 and 2.0 specifications to the @dest parameter, which will e.g., help tracing bugs.Perhaps my previous message fell through the cracks, but I still have a couple of comments (perhaps trivial, sorry in that case) that have not been answered about this patch:
I think what happened is that there was enough time that I forgot what I had or hadn't done :-) I'll address your comments.
Quoted 43 lines
Quoted 40 lines
Signed-off-by Jarkko Sakkinen <[email protected]>Reviewed-by Stefan Berger <[email protected]>---
v6
- Update commit message.
v5:
- There was a spurious change intpm2_seal_trusted()error code handling introduce by this patch.
v4:
- Since every singletpm_transmit_cmd()call site needs to be changed anyhow, use 'dest' parameter more structured and actually useful way, and pick the string TCG 1.2 and 2.0 specifications.
- tpm1-cmd: Remove useless rc declarations and repliace them with trivial "returntpm_transmit_cmd" statement.
- Reverted spurious changes ininclude/linux/tpm.h.
- Use conciselyTPM_BUFSIZEinstead ofPAGE_SIZE.
v3:
- A new patch from the earlier series with more scoped changes and less abstract commit message.
---drivers/char/tpm/tpm-buf.c | 122 +++++---- drivers/char/tpm/tpm-sysfs.c | 21 +- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 162 +++++------- drivers/char/tpm/tpm2-cmd.c | 299 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 122 +++++---- drivers/char/tpm/tpm2-space.c | 44 ++-- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +-- include/linux/tpm.h | 18 +- security/keys/trusted-keys/trusted_tpm1.c | 34 ++- security/keys/trusted-keys/trusted_tpm2.c | 175 ++++++------- 11 files changed, 484 insertions(+), 544 deletions(-)drivers/char/tpm/tpm-buf.c +0
-0[...]
Quoted 5 lines
drivers/char/tpm/tpm-buf.c EXPORT_SYMBOL_GPL(tpm_buf_destroy); · line 120
Quoted 3 lines
drivers/char/tpm/tpm-buf.c +0
-0@@ -92,6 +119,9 @@ EXPORT_SYMBOL_GPL(tpm_buf_destroy);*/ u32 tpm_buf_length (struct tpm_buf *buf)Should we update the return value to u16?
Ack.
Quoted 40 lines
Quoted 9 lines
drivers/char/tpm/tpm-buf.c +3
-0{ +if (buf->flags &TPM_BUF_INVALID ) +return 0 ; +return buf->length; }EXPORT_SYMBOL_GPL (tpm_buf_length);[...]
Quoted 16 lines
security/keys/trusted-keys/trusted_tpm1.c +1
-2@@ -310,9 +310,8 @@ static int TSS_checkhmac2(unsigned char *buffer,* For key specific tpm requests, we will generate and send our * own TPM command packets using the drivers send function. */ -static int trusted_tpm_send (unsigned char *cmd,size_t buflen) +static int trusted_tpm_send (void *cmd,size_t buflen) { -struct tpm_buf buf;int rc;if (!chip)security/keys/trusted-keys/trusted_tpm1.c static int trusted_tpm_send(unsigned char *cmd, size_t buflen) · line 325
Quoted 9 lines
security/keys/trusted-keys/trusted_tpm1.c +1
-4@@ -322,15 +321,12 @@ static int trusted_tpm_send(unsigned char *cmd, size_t buflen)if (rc)return rc; - buf.flags =0 ; - buf.length = buflen; - buf.data = cmd;dump_tpm_buf (cmd); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sending data" ); + rc =tpm_transmit_cmd (chip, cmd,4 ,"sending data" );Is it fine here to remove the intermediate
tpm_buf?
Quoted 2 lines
IIUC
tpm_transmit_cmd()needs atpm_buf, while here we are passing just the "data", or in some way it's a nestedtpm_buf?
This does not look right at all. I'll fix it and add automated test suite for TPM 1.x to my standard test shenanigans so that I can continuously test its correctness [1]. It anyhow should have TPM 1.2 tests (especially for trusted keys). As it becomes more rare accidental bugs could easily hover in.
Thanks, this was really good catch. I'll address both bug and make sure that I don't do the same mistake twice :-)
Quoted 8 lines
Quoted 8 lines
security/keys/trusted-keys/trusted_tpm1.c +1
-1dump_tpm_buf (cmd); +/* Convert TPM error to -EPERM. */ if (rc >0 ) -/* TPM error */ rc = -EPERM ;
Quoted 5 lines
security/keys/trusted-keys/trusted_tpm1.c static int trusted_tpm_send(unsigned char *cmd, size_t buflen) · line 332
Quoted 1 line
security/keys/trusted-keys/trusted_tpm1.c +0
-0tpm_put_ops (chip);Thanks,
Stefano
[1] https://codeberg.org/jarkko/linux-tpmdd-test
BR, Jarkko