tpm: Decouple PCR extend from driver
Subsystems (4)
Jarkko Sakkinen
maintainerv2 cover tpm: Decouple PCR extend from driver
Decouple tpm2-sessions enough from implementation so that building for PCR extend commands can be decoupled from rest of the implementation. This is a mandatory for Trenchboot series, and including all these changes for that series would over-complicate it.
This is first part of refactorizations for make grounds for Trenchboot, and still aimed for 6.18. The second part includes robustness updates for tpm-buf.
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 (9):
tpm: cap PCR bank in tpm2_get_pcr_allocations()
tpm: Use -EPERM as fallback error code in tpm_ret_to_err
KEYS: trusted: Use tpm_ret_to_err() in trusted_tpm2
tpm2-sessions: Remove 'attributes' from tpm_buf_append_auth
tpm2-sessions: Umask tpm_buf_append_hmac_session()
KEYS: trusted: Open code tpm2_buf_append()
tpm-buf: check for corruption in tpm_buf_append_handle()
tpm-buf: Remove chip parameeter from tpm_buf_append_handle
tpm-buf: Build PCR extend commands
drivers/char/tpm/tpm-buf.c | 85 +++++++++++++++++--- drivers/char/tpm/tpm-chip.c | 13 +++- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 40 ++-------- drivers/char/tpm/tpm2-cmd.c | 39 ++++++---- drivers/char/tpm/tpm2-sessions.c | 7 +- include/linux/tpm.h | 61 +++++---------- include/linux/tpm_command.h | 5 +- security/keys/trusted-keys/trusted_tpm2.c | 95 +++++++---------------- 9 files changed, 170 insertions(+), 176 deletions(-)
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v2 1/9 tpm: cap PCR bank in tpm2_get_pcr_allocations()
From: Jarkko Sakkinen <[email protected]>
tpm2_get_pcr_allocation() does not cap any upper limit for the number of banks received from external hardware device. This could lead into resource over-consumption with a fauly TPM device.
---
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 | 18 ++++++++---------- 5 files changed, 20 insertions(+), 45 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_BANKS ) { +pr_err ("tpm:: unexpected large number of banks: %u > %u" , + nr_possible_banks,TPM2_MAX_BANKS ); rc = -ENOMEM ; goto out; }
include/linux/tpm.h +8 -10
@@ -27,7 +27,12 @@#include <crypto/aes.h> #define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ -#define TPM_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM_HEADER_SIZE 10 + +#define TPM2_PLATFORM_PCR 24 +#define TPM2_PCR_SELECT_MIN 3 +#define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE +#define TPM2_MAX_BANKS 4struct tpm_chip ;struct trusted_key_payload ;
@@ -69,7 +74,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 {
@@ -190,7 +195,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_BANKS ];#ifdef CONFIG_ACPI acpi_handle acpi_dev_handle;char ppi_version[TPM_PPI_VERSION_LEN +1 ];
@@ -217,13 +222,6 @@ struct tpm_chip {#endif }; -#define TPM_HEADER_SIZE 10 - -enum tpm2_const { -TPM2_PLATFORM_PCR =24 , -TPM2_PCR_SELECT_MIN = ((TPM2_PLATFORM_PCR +7 ) /8 ), -}; -enum tpm2_timeouts {TPM2_TIMEOUT_A =750 ,TPM2_TIMEOUT_B =4000 ,
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v2 2/9 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.
---
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
@@ -453,8 +453,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 v2 3/9 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.
---
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
@@ -348,25 +348,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); }/**
@@ -468,10 +462,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip,kfree (blob);tpm_buf_destroy (&buf); -if (rc >0 ) - rc = -EPERM ; - -return rc; +return tpm_ret_to_err (rc); }/**
@@ -534,8 +525,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(
@@ -568,7 +557,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, out:tpm_buf_destroy (&buf); -return rc; +return tpm_ret_to_err (rc); }/**
@@ -600,6 +589,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 v2 4/9 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.
---
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
@@ -531,7 +531,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 v2 5/9 tpm2-sessions: Umask 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().
---
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
@@ -532,29 +532,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
@@ -482,8 +482,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;
@@ -518,8 +520,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 v2 6/9 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").
---
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
@@ -198,36 +198,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 *
@@ -507,19 +477,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 v2 7/9 tpm-buf: check for corruption in tpm_buf_append_handle()
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Unify TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW into TPM_BUF_INVALID flag because semantically they are identical.
Test and set TPM_BUF_INVALID in tpm_buf_append_handle() following the pattern from other functions in tpm-buf.c.
---
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,7 +156,11 @@ 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 ) { + buf->flags |=TPM_BUF_INVALID ;dev_err (&chip->dev,"Invalid buffer type (TPM2B)\n" );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
@@ -366,12 +366,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
@@ -295,7 +295,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;
@@ -308,7 +308,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; }
@@ -414,7 +414,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 v2 8/9 tpm-buf: Remove chip parameeter from tpm_buf_append_handle
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Remove chip parameter from tpm_buf_append_handle() in order to maintain decoupled state with tpm-buf. This is mandatory change in order to re-use the module in early boot code of Trenchboot, and the binding itself brings no benefit. Use WARN like in other functions, as the error condition can happen only as a net effect of a trivial programming mistake.
---
v2:
- A new patch.
--- drivers/char/tpm/tpm-buf.c | 6 ++---- drivers/char/tpm/tpm2-cmd.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 2 +- include/linux/tpm.h | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-)
drivers/char/tpm/tpm-buf.c +2 -4
@@ -147,21 +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 ) { - buf->flags |=TPM_BUF_INVALID ; -dev_err (&chip->dev,"Invalid buffer type (TPM2B)\n" ); +WARN (1 ,"tpm-buf: invalid type: TPM2B\n" );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
@@ -423,7 +423,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 v2 9/9 tpm-buf: Build PCR extend commands
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Build and append TPM_ORD_EXTEND and TPM2_CC_PCR_EXTEND command bodies with the two new functions:
1. tpm1_buf_append_extend()
2. tpm2_buf_append_pcr_extend()
These changes make the fallback more informative of the situation, as the underlying programming error is catched at the call site, instead of masking it as a tpm_transmit() failure.
Further, decoupling the build of the command bodies for extending PCRs will be mandatory for the Trenchboot early boot code.
---
v2:
- A new patch.
--- drivers/char/tpm/tpm-buf.c | 67 +++++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm1-cmd.c | 15 +++++---- drivers/char/tpm/tpm2-cmd.c | 13 ++++--- include/linux/tpm.h | 4 +++ include/linux/tpm_command.h | 5 +-- 5 files changed, 88 insertions(+), 16 deletions(-)
drivers/char/tpm/tpm-buf.c +67 -0
@@ -243,4 +243,71 @@ u32 tpm_buf_read_u32(struct tpm_buf *buf, off_t *offset) }EXPORT_SYMBOL_GPL (tpm_buf_read_u32); +static bool tpm1_buf_is_command (struct tpm_buf *buf,u32 ordinal) +{ +struct tpm_header *head = (struct tpm_header *)buf->data; + +return !(buf->flags &TPM_BUF_TPM2B ) && +be16_to_cpu (head->tag) ==TPM_TAG_RQU_COMMAND && +be32_to_cpu (head->ordinal) == ordinal; +} + +/** +* tpm1_buf_append_extend() - Append command body for TPM_Extend +* @buf: &tpm_buf instance +* @pcr_idx: index of the PCR +* @hash: SHA1 hash +*/ +void tpm1_buf_append_extend (struct tpm_buf *buf,u32 pcr_idx,const u8 *hash) +{ +if (buf->flags &TPM_BUF_INVALID ) +return ; + +if (!tpm1_buf_is_command (buf,TPM_ORD_EXTEND )) { +WARN (1 ,"tpm_buf: invalid TPM_Extend command\n" ); + buf->flags |=TPM_BUF_INVALID ; +return ; + } + +tpm_buf_append_u32 (buf, pcr_idx); +tpm_buf_append (buf, hash,TPM_DIGEST_SIZE ); +} + +static bool tpm2_buf_is_command (struct tpm_buf *buf,u32 ordinal) +{ +struct tpm_header *head = (struct tpm_header *)buf->data; +u16 tag =be16_to_cpu (head->tag); + +return !(buf->flags &TPM_BUF_TPM2B ) && + (tag ==TPM2_ST_SESSIONS || tag ==TPM2_ST_NO_SESSIONS ) && +be32_to_cpu (head->ordinal) == ordinal; +} + +/** +* tpm2_buf_append_pcr_extend() - Append command body for TPM2_PCR_Extend +* @buf: &tpm_buf instance +* @digests: list of PCR digests +* @banks: PCR bank descriptors +* @nr_banks: number of PCR banks +*/ +void tpm2_buf_append_pcr_extend (struct tpm_buf *buf,struct tpm_digest *digests, +struct tpm_bank_info *banks, +unsigned int nr_banks) +{ +int i; +if (buf->flags &TPM_BUF_INVALID ) +return ; + +if (!tpm2_buf_is_command (buf,TPM2_CC_PCR_EXTEND )) { +WARN (1 ,"tpm_buf: invalid TPM2_PCR_Extend command\n" ); + buf->flags |=TPM_BUF_INVALID ; +return ; + } + +tpm_buf_append_u32 (buf, nr_banks); +for (i =0 ; i < nr_banks; i++) { +tpm_buf_append_u16 (buf, digests[i].alg_id); +tpm_buf_append (buf, digests[i].digest, banks[i].digest_size); + } +}
drivers/char/tpm/tpm1-cmd.c +8 -7
@@ -18,8 +18,8 @@#include <linux/mutex.h> #include <linux/spinlock.h> #include <linux/freezer.h> +#include <linux/tpm_command.h> #include <linux/tpm_eventlog.h> -#include "tpm.h" #define TPM_MAX_ORDINAL 243
@@ -459,21 +459,23 @@ int tpm1_get_timeouts(struct tpm_chip *chip)return 0 ; } -#define TPM_ORD_PCR_EXTEND 20int tpm1_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,const u8 *hash,const char *log_msg) {struct tpm_buf buf;int rc; - rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_PCR_EXTEND ); + rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_EXTEND );if (rc)return rc; -tpm_buf_append_u32 (&buf, pcr_idx); -tpm_buf_append (&buf, hash,TPM_DIGEST_SIZE ); +tpm1_buf_append_extend (&buf, pcr_idx, hash); + +if (buf.flags &TPM_BUF_INVALID ) + rc = -EINVAL ; +else + rc =tpm_transmit_cmd (chip, &buf,TPM_DIGEST_SIZE , log_msg); - rc =tpm_transmit_cmd (chip, &buf,TPM_DIGEST_SIZE , log_msg);tpm_buf_destroy (&buf);return rc; }
@@ -511,7 +513,6 @@ ssize_t tpm1_getcap(struct tpm_chip *chip, u32 subcap_id, cap_t *cap, }EXPORT_SYMBOL_GPL (tpm1_getcap); -#define TPM_ORD_GET_RANDOM 70struct tpm1_get_random_out {__be32 rng_data_len;u8 rng_data[TPM_MAX_RNG_DATA ];
drivers/char/tpm/tpm2-cmd.c +6 -7
@@ -171,7 +171,6 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, {struct tpm_buf buf;int rc; -int i;if (!disable_pcr_integrity) { rc =tpm2_start_auth_session (chip);
@@ -194,12 +193,12 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,tpm_buf_append_auth (chip, &buf,NULL ,0 ); } -tpm_buf_append_u32 (&buf, chip->nr_allocated_banks); +tpm2_buf_append_pcr_extend (&buf, digests, chip->allocated_banks, + chip->nr_allocated_banks); -for (i =0 ; i < chip->nr_allocated_banks; i++) { -tpm_buf_append_u16 (&buf, digests[i].alg_id); -tpm_buf_append (&buf, (const unsigned char *)&digests[i].digest, - chip->allocated_banks[i].digest_size); +if (buf.flags &TPM_BUF_INVALID ) { + rc = -EINVAL ; + goto out; }if (!disable_pcr_integrity)
@@ -208,8 +207,8 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,if (!disable_pcr_integrity) rc =tpm_buf_check_hmac_response (chip, &buf, rc); +out:tpm_buf_destroy (&buf); -return rc; }
include/linux/tpm.h +4 -0
@@ -424,6 +424,10 @@ 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_buf *buf,u32 handle); +void tpm1_buf_append_extend (struct tpm_buf *buf,u32 pcr_idx,const u8 *hash); +void tpm2_buf_append_pcr_extend (struct tpm_buf *buf,struct tpm_digest *digests, +struct tpm_bank_info *banks, +unsigned int nr_banks);/* * Check if TPM device is in the firmware upgrade mode.
include/linux/tpm_command.h +3 -2
@@ -16,11 +16,12 @@#define TPM_TAG_RSP_AUTH2_COMMAND 198/* Command Ordinals */ -#define TPM_ORD_GETRANDOM 70 -#define TPM_ORD_OSAP 11#define TPM_ORD_OIAP 10 +#define TPM_ORD_OSAP 11 +#define TPM_ORD_EXTEND 20#define TPM_ORD_SEAL 23#define TPM_ORD_UNSEAL 24 +#define TPM_ORD_GET_RANDOM 70/* Other constants */ #define SRKHANDLE 0x40000000
Signature · 1 line
2.39.5