Prepare TPM driver for Trenchboot
Subsystems (4)
Jarkko Sakkinen
maintainerv5 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.
Goals and scope
===============
This patch set contains only changes, which are acceptable to the mainline driver without existence of Trenchboot feature.
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 (9):
tpm: Cap the number of PCR banks
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: Unmask 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 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 | 241 +++++++---------- 12 files changed, 544 insertions(+), 674 deletions(-)
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v5 1/9 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.
---
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
@@ -27,7 +27,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 ;
@@ -69,7 +71,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 +192,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
Jarkko Sakkinen
maintainerPATCH v5 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.
---
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
@@ -457,8 +457,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 v5 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.
---
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
@@ -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 v5 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.
---
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
@@ -535,7 +535,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 v5 5/9 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().
---
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
@@ -536,29 +536,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 v5 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").
---
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
@@ -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 v5 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.
---
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
@@ -370,12 +370,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 v5 8/9 tpm-buf: Remove chip parameter 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.
---
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
@@ -427,7 +427,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 v5 9/9 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));
---
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 | 172 ++++++------- 11 files changed, 484 insertions(+), 541 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
@@ -26,7 +26,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
@@ -377,13 +378,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 {
@@ -414,11 +417,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 -93
304 lines not on this page · raw message
Signature · 1 line
2.39.5