Streamline TPM2 HMAC sessions
Subsystems (4)
Jarkko Sakkinen
maintainerv6 cover Streamline TPM2 HMAC sessions
Since we cannot at this point cache names of the keys given limitations of the ASN.1 file format, I'll start a fresh patch set. Let's fixup what we can right now.
This patch set addresses two major issues in the feature:
1. Dynamic resolution without gain. All kernel sites have at most single handle to authorize. Even if this changes some day this is how it is as of today and we definitely do not want to dictate the future but instead downscale code to the metrics that we have as of today.
2. Eliminate at least one unnnecessary tpm2_read_public() call.
Change Log
==========
v6:
- OK, so I decided to send one more update with managed allocations moved to the tail so that it does not block reviewing more trivial patches.
- Trimmed some of the patches and improved commit messages. v5:
- I decided to add the managed allocation patch to this and take it from the master branch for the time being, as it needs more eyes despite having already one reviewed-by tag (especially tested-by tags).
Jarkko Sakkinen (11):
tpm2-sessions: Define TPM2_NAME_MAX_SIZE
KEYS: trusted: Open code tpm2_buf_append()
KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
KEYS: trusted: Re-orchestrate tpm2_read_public() calls
tpm2-sessions: Remove AUTH_MAX_NAMES
tpm: Orchestrate TPM commands in tpm_get_random()
tpm: Send only one at most TPM2_GetRandom command
tpm: In tpm_get_random() replace 'retries' with a zero check
tpm-buf: Merge TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW
tpm-buf: Implement managed allocations
tpm-buf: Remove tpm_buf_append_handle
drivers/char/tpm/tpm-buf.c | 154 ++++----- drivers/char/tpm/tpm-chip.c | 2 +- drivers/char/tpm/tpm-interface.c | 177 ++++++++++- drivers/char/tpm/tpm-sysfs.c | 23 +- drivers/char/tpm/tpm.h | 3 - drivers/char/tpm/tpm1-cmd.c | 198 ++++-------- drivers/char/tpm/tpm2-cmd.c | 371 +++++++--------------- drivers/char/tpm/tpm2-sessions.c | 272 ++++++---------- drivers/char/tpm/tpm2-space.c | 44 ++- drivers/char/tpm/tpm_vtpm_proxy.c | 30 +- include/linux/tpm.h | 79 +++-- security/keys/trusted-keys/trusted_tpm1.c | 44 +-- security/keys/trusted-keys/trusted_tpm2.c | 329 ++++++++++--------- 13 files changed, 799 insertions(+), 927 deletions(-)
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 1/11 tpm2-sessions: Define TPM2_NAME_MAX_SIZE
Define TPM2_NAME_MAX_SIZE, which describes the maximum size for hashes encoded as TPMT_HA, which the prime identifier used for persistent and transient keys in TPM2 protocol.
Set its value to 'SHA512_DIGEST_SIZE + 2', as SHA512 has the largest digest size of the algorithms in TCG algorithm repository.
In additionl, rename TPM2_NAME_SIZE as TPM2_NULL_NAME_SIZE in order to avoid any possible confusion.
---
v6:
- Rewrote the commit message.
v2:
- Rename TPM2_NAME_SIZE as TPM2_NULL_NAME_SIZE.
--- drivers/char/tpm/tpm-sysfs.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 2 +- include/linux/tpm.h | 37 +++++++++++++++++++++----------- 3 files changed, 27 insertions(+), 14 deletions(-)
drivers/char/tpm/tpm-sysfs.c +1 -1
@@ -314,7 +314,7 @@ static ssize_t null_name_show(struct device *dev, struct device_attribute *attr, char *buf) {struct tpm_chip *chip =to_tpm_chip (dev); -int size =TPM2_NAME_SIZE ; +int size =TPM2_NULL_NAME_SIZE ;bin2hex (buf, chip->null_key_name, size); size *=2 ;
drivers/char/tpm/tpm2-sessions.c +1 -1
@@ -137,7 +137,7 @@ struct tpm2_auth {* we must compute and remember */ u32 name_h[AUTH_MAX_NAMES ]; -u8 name[AUTH_MAX_NAMES ][2 +SHA512_DIGEST_SIZE ]; +u8 name[AUTH_MAX_NAMES ][TPM2_MAX_NAME_SIZE ]; };#ifdef CONFIG_TCG_TPM2_HMAC
include/linux/tpm.h +25 -12
@@ -27,9 +27,33 @@#define TPM_DIGEST_SIZE 20/* Max TPM v1.2 PCR size */ +/* +* SHA-512 is, as of today, the largest digest in the TCG algorithm repository. +*/ #define TPM2_MAX_DIGEST_SIZE SHA512_DIGEST_SIZE + +/* +* A TPM name digest i.e., TPMT_HA, is a concatenation of TPM_ALG_ID of the +* name algorithm and hash of TPMT_PUBLIC. +*/ +#define TPM2_MAX_NAME_SIZE (TPM2_MAX_DIGEST_SIZE + 2) + +/* +* The maximum number of PCR banks. +*/ #define TPM2_MAX_PCR_BANKS 8 +/* +* fixed define for the size of a name. This is actually HASHALG size +* plus 2, so 32 for SHA256 +*/ +#define TPM2_NULL_NAME_SIZE 34 + +/* +* The maximum size for an object context +*/ +#define TPM2_MAX_CONTEXT_SIZE 4096 +struct tpm_chip ;struct trusted_key_payload ;struct trusted_key_options ;
@@ -139,17 +163,6 @@ struct tpm_chip_seqops {/* fixed define for the curve we use which is NIST_P256 */ #define EC_PT_SZ 32 -/* -* fixed define for the size of a name. This is actually HASHALG size -* plus 2, so 32 for SHA256 -*/ -#define TPM2_NAME_SIZE 34 - -/* -* The maximum size for an object context -*/ -#define TPM2_MAX_CONTEXT_SIZE 4096 -struct tpm_chip {struct device dev;struct device devs;
@@ -211,7 +224,7 @@ struct tpm_chip {/* saved context for NULL seed */ u8 null_key_context[TPM2_MAX_CONTEXT_SIZE ];/* name of NULL seed */ -u8 null_key_name[TPM2_NAME_SIZE ]; +u8 null_key_name[TPM2_NULL_NAME_SIZE ];u8 null_ec_key_x[EC_PT_SZ ];u8 null_ec_key_y[EC_PT_SZ ];struct tpm2_auth *auth;
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 2/11 KEYS: trusted: Open code tpm2_buf_append()
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
tpm2_buf_append_auth() has a single call site and most of its parameters are redundant. Open code it to the call site so that less cross-referencing is required while browsing the source code.
---
v6:
- Trimmed the patch by removing comment update as it is out of scope.
--- security/keys/trusted-keys/trusted_tpm2.c | 40 ++++------------------- 1 file changed, 7 insertions(+), 33 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +7 -33
@@ -190,36 +190,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 *
@@ -518,9 +488,13 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,* could repeat our actions with the exfiltrated * password. */ -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 3/11 KEYS: trusted: Remove dead branch from tpm2_unseal_cmd
In reply to Jarkko SakkinenTPM2_Unseal requires TPM2_ST_SESSIONS, and tpm2_unseal_cmd() always does set up either password or HMAC session.
Remove the branch in tpm2_unseal_cmd() conditionally setting TPM2_ST_NO_SESSIONS. It is faulty but luckily it is never exercised at run-time, and thus does not cause regressions.
--- security/keys/trusted-keys/trusted_tpm2.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-)
security/keys/trusted-keys/trusted_tpm2.c +1 -9
@@ -451,10 +451,8 @@ 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;
@@ -495,14 +493,8 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,tpm_buf_append_u16 (&buf, options->blobauth_len);tpm_buf_append (&buf, options->blobauth, options->blobauth_len); -if (tpm2_chip_auth (chip)) { +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 ); - } } rc =tpm_buf_fill_hmac_session (chip, &buf);
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 4/11 KEYS: trusted: Re-orchestrate tpm2_read_public() calls
In reply to Jarkko Sakkinentpm2_load_cmd() and tpm2_unseal_cmd() use the same parent, and calls to tpm_buf_append_name() cause the exact same TPM2_ReadPublic command to be sent to the chip, causing unnecessary traffic.
1. Export tpm2_read_public in order to make it callable from 'trusted_tpm2'.
2. Re-orchestrate tpm2_seal_trusted() and tpm2_unseal_trusted() in order to halve the name resolutions required:
2a. Move tpm2_read_public() calls into trusted_tpm2.
2b. Pass TPM name to tpm_buf_append_name().
2c. Rework tpm_buf_append_name() to use the pre-resolved name.
--- drivers/char/tpm/tpm2-cmd.c | 3 +- drivers/char/tpm/tpm2-sessions.c | 95 +++++------------ include/linux/tpm.h | 10 +- security/keys/trusted-keys/trusted_tpm2.c | 124 ++++++++++++++-------- 4 files changed, 118 insertions(+), 114 deletions(-)
drivers/char/tpm/tpm2-cmd.c +2 -1
@@ -202,7 +202,8 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, }if (!disable_pcr_integrity) { - rc =tpm_buf_append_name (chip, &buf, pcr_idx,NULL ); + rc =tpm_buf_append_name (chip, &buf, pcr_idx, (u8 *)&pcr_idx, +sizeof (u32));if (rc) {tpm_buf_destroy (&buf);return rc;
drivers/char/tpm/tpm2-sessions.c +28 -67
153 lines not on this page · raw message
include/linux/tpm.h +9 -1
@@ -543,7 +543,7 @@ static inline struct tpm2_auth *tpm2_chip_auth(struct tpm_chip *chip) }int tpm_buf_append_name (struct tpm_chip *chip,struct tpm_buf *buf, -u32 handle,u8 *name); +u32 handle,u8 *name,u16 name_size);void tpm_buf_append_hmac_session (struct tpm_chip *chip,struct tpm_buf *buf,u8 attributes,u8 *passphrase,int passphraselen);
@@ -557,6 +557,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf);int tpm_buf_check_hmac_response (struct tpm_chip *chip,struct tpm_buf *buf,int rc);void tpm2_end_auth_session (struct tpm_chip *chip); +int tpm2_read_public (struct tpm_chip *chip,u32 handle,void *name);#else #include <linux/unaligned.h>
@@ -580,6 +581,13 @@ static inline int tpm_buf_check_hmac_response(struct tpm_chip *chip, {return rc; } + +static inline int tpm2_read_public (struct tpm_chip *chip,u32 handle, +void *name) +{ +memcpy (name, &handle,sizeof (u32)); +return sizeof (u32); +}#endif /* CONFIG_TCG_TPM2_HMAC */ #endif
security/keys/trusted-keys/trusted_tpm2.c +79 -45
212 lines not on this page · raw message
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 5/11 tpm2-sessions: Remove AUTH_MAX_NAMES
In reply to Jarkko SakkinenIn all of the call sites only one session is ever append. Thus, reduce AUTH_MAX_NAMES, which leads into removing constant completely.
--- drivers/char/tpm/tpm2-sessions.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-)
drivers/char/tpm/tpm2-sessions.c +11 -20
@@ -72,9 +72,6 @@#include <crypto/sha2.h> #include <crypto/utils.h> -/* maximum number of names the TPM must remember for authorization */ -#define AUTH_MAX_NAMES 3 -#define AES_KEY_BYTES AES_KEYSIZE_128#define AES_KEY_BITS (AES_KEY_BYTES*8)
@@ -136,8 +133,8 @@ struct tpm2_auth {* handle, but they are part of the session by name, which * we must compute and remember */ -u8 name[AUTH_MAX_NAMES ][TPM2_MAX_NAME_SIZE ]; -u16 name_size_tbl[AUTH_MAX_NAMES ]; +u8 name[TPM2_MAX_NAME_SIZE ]; +u16 name_size; };#ifdef CONFIG_TCG_TPM2_HMAC
@@ -261,11 +258,14 @@ EXPORT_SYMBOL_GPL(tpm2_read_public);int tpm_buf_append_name (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle,u8 *name,u16 name_size) { -#ifdef CONFIG_TCG_TPM2_HMAC struct tpm2_auth *auth; -int slot;int ret; -#endif + +if (tpm_buf_length (buf) !=TPM_HEADER_SIZE ) { +dev_err (&chip->dev,"too many handles\n" ); + ret = -EIO ; + goto err; + }if (!tpm2_chip_auth (chip)) {tpm_buf_append_handle (chip, buf, handle);
@@ -273,12 +273,6 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }#ifdef CONFIG_TCG_TPM2_HMAC - slot = (tpm_buf_length (buf) -TPM_HEADER_SIZE ) /4 ; -if (slot >=AUTH_MAX_NAMES ) { -dev_err (&chip->dev,"too many handles\n" ); - ret = -EIO ; - goto err; - } auth = chip->auth;if (auth->session !=tpm_buf_length (buf)) {dev_err (&chip->dev,"session state malformed" );
@@ -287,16 +281,14 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }tpm_buf_append_u32 (buf, handle); auth->session +=4 ; -memcpy (auth->name[slot], name, name_size); - auth->name_size_tbl[slot] = name_size; +memcpy (auth->name, name, name_size); + auth->name_size = name_size;#endif return 0 ; -#ifdef CONFIG_TCG_TPM2_HMAC err:tpm2_end_auth_session (chip);return ret; -#endif }EXPORT_SYMBOL_GPL (tpm_buf_append_name);
@@ -665,8 +657,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)/* ordinal is already BE */ sha256_update (&sctx, (u8 *)&head->ordinal,sizeof (head->ordinal));/* add the handle names */ -for (i =0 ; i < handles; i++) -sha256_update (&sctx, auth->name[i], auth->name_size_tbl[i]); +sha256_update (&sctx, auth->name, auth->name_size);if (offset_s !=tpm_buf_length (buf))sha256_update (&sctx, &buf->data[offset_s],tpm_buf_length (buf) - offset_s);
Signature · 1 line
2.39.5
kernel test robot
build botRe: [PATCH v6 05/11] tpm2-sessions: Remove AUTH_MAX_NAMES
kernel test robot noticed the following build warnings:
Hi Jarkko,
kernel test robot noticed the following build warnings:
[auto build test WARNING on char-misc/char-misc-testing]
[also build test WARNING on char-misc/char-misc-next char-misc/char-misc-linus linus/master v6.19-rc2 next-20251219] [cannot apply to zohar-integrity/next-integrity dhowells-fs/fscache-next dhowells-fs/keys-next] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jarkko-Sakkinen/tpm2-sessions-Define-TPM2_NAME_MAX_SIZE/20251214-234436 base: char-misc/char-misc-testing patch link: https://lore.kernel.org/r/20251214153808.73831-6-jarkko%40kernel.org patch subject: [PATCH v6 05/11] tpm2-sessions: Remove AUTH_MAX_NAMES config: arm64-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20251222/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project ecaf673850beb241957352bd61e95ed34256635f) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251222/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from drivers/char/tpm/tpm2-sessions.c:66:
In file included from drivers/char/tpm/tpm.h:28:
./include/linux/tpm_eventlog.h:167:6: warning: variable 'mapping_size' set but not used [-Wunused-but-set-variable]
167 | int mapping_size;
| ^
>> drivers/char/tpm/tpm2-sessions.c:261:20: warning: unused variable 'auth' [-Wunused-variable]
261 | struct tpm2_auth *auth;
| ^~~~2 warnings generated.
vim +/auth +261 drivers/char/tpm/tpm2-sessions.c
a61809a3323982 Jarkko Sakkinen 2024-07-03 231
a61809a3323982 Jarkko Sakkinen 2024-07-03 232 /**
708971b661887e Jarkko Sakkinen 2025-12-14 233 * tpm_buf_append_name() - Append a handle and store TPM name
708971b661887e Jarkko Sakkinen 2025-12-14 234 * @chip: TPM chip to use.
708971b661887e Jarkko Sakkinen 2025-12-14 235 * @buf: TPM buffer containing the TPM command in-transit.
708971b661887e Jarkko Sakkinen 2025-12-14 236 * @handle: TPM handle to be appended.
708971b661887e Jarkko Sakkinen 2025-12-14 237 * @name: TPM name of the handle
708971b661887e Jarkko Sakkinen 2025-12-14 238 * @name_size: Size of the TPM name.
a61809a3323982 Jarkko Sakkinen 2024-07-03 239 *
a61809a3323982 Jarkko Sakkinen 2024-07-03 240 * In order to compute session HMACs, we need to know the names of the
a61809a3323982 Jarkko Sakkinen 2024-07-03 241 * objects pointed to by the handles. For most objects, this is simply
a61809a3323982 Jarkko Sakkinen 2024-07-03 242 * the actual 4 byte handle or an empty buf (in these cases @name
a61809a3323982 Jarkko Sakkinen 2024-07-03 243 * should be NULL) but for volatile objects, permanent objects and NV
a61809a3323982 Jarkko Sakkinen 2024-07-03 244 * areas, the name is defined as the hash (according to the name
a61809a3323982 Jarkko Sakkinen 2024-07-03 245 * algorithm which should be set to sha256) of the public area to
a61809a3323982 Jarkko Sakkinen 2024-07-03 246 * which the two byte algorithm id has been appended. For these
a61809a3323982 Jarkko Sakkinen 2024-07-03 247 * objects, the @name pointer should point to this. If a name is
a61809a3323982 Jarkko Sakkinen 2024-07-03 248 * required but @name is NULL, then TPM2_ReadPublic() will be called
a61809a3323982 Jarkko Sakkinen 2024-07-03 249 * on the handle to obtain the name.
a61809a3323982 Jarkko Sakkinen 2024-07-03 250 *
a61809a3323982 Jarkko Sakkinen 2024-07-03 251 * As with most tpm_buf operations, success is assumed because failure
a61809a3323982 Jarkko Sakkinen 2024-07-03 252 * will be caused by an incorrect programming model and indicated by a
a61809a3323982 Jarkko Sakkinen 2024-07-03 253 * kernel message.
6e9722e9a7bfe1 Jarkko Sakkinen 2025-11-30 254 *
708971b661887e Jarkko Sakkinen 2025-12-14 255 * Returns zero on success.
708971b661887e Jarkko Sakkinen 2025-12-14 256 * Returns -EIO when the authorization area state is malformed.
a61809a3323982 Jarkko Sakkinen 2024-07-03 257 */
6e9722e9a7bfe1 Jarkko Sakkinen 2025-11-30 258 int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf,
708971b661887e Jarkko Sakkinen 2025-12-14 259 u32 handle, u8 *name, u16 name_size)
a61809a3323982 Jarkko Sakkinen 2024-07-03 260 {
a61809a3323982 Jarkko Sakkinen 2024-07-03 @261 struct tpm2_auth *auth;
6e9722e9a7bfe1 Jarkko Sakkinen 2025-11-30 262 int ret;
db19f5a5bd7be5 Jarkko Sakkinen 2025-12-14 263
db19f5a5bd7be5 Jarkko Sakkinen 2025-12-14 264 if (tpm_buf_length(buf) != TPM_HEADER_SIZE) {
db19f5a5bd7be5 Jarkko Sakkinen 2025-12-14 265 dev_err(&chip->dev, "too many handles\n");
db19f5a5bd7be5 Jarkko Sakkinen 2025-12-14 266 ret = -EIO;
db19f5a5bd7be5 Jarkko Sakkinen 2025-12-14 267 goto err;
db19f5a5bd7be5 Jarkko Sakkinen 2025-12-14 268 }
a61809a3323982 Jarkko Sakkinen 2024-07-03 269
a61809a3323982 Jarkko Sakkinen 2024-07-03 270 if (!tpm2_chip_auth(chip)) {
27184f8905ba68 Jarkko Sakkinen 2024-11-13 271 tpm_buf_append_handle(chip, buf, handle);
6e9722e9a7bfe1 Jarkko Sakkinen 2025-11-30 272 return 0;
a61809a3323982 Jarkko Sakkinen 2024-07-03 273 }
a61809a3323982 Jarkko Sakkinen 2024-07-03 274 Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v6 05/11] tpm2-sessions: Remove AUTH_MAX_NAMES
In reply to Jarkko SakkinenQuoted 34 lines · Jarkko Sakkinen
In all of the call sites only one session is ever append. Thus, reduce
AUTH_MAX_NAMES, which leads into removing constant completely.Signed-off-by Jarkko Sakkinen <[email protected]>--- drivers/char/tpm/tpm2-sessions.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-)drivers/char/tpm/tpm2-sessions.c +2
-5@@ -72,9 +72,6 @@#include <crypto/sha2.h> #include <crypto/utils.h> -/* maximum number of names the TPM must remember for authorization */ -#define AUTH_MAX_NAMES 3 -#define AES_KEY_BYTES AES_KEYSIZE_128#define AES_KEY_BITS (AES_KEY_BYTES*8)@@ -136,8 +133,8 @@ struct tpm2_auth {* handle, but they are part of the session by name, which * we must compute and remember */ -u8 name[AUTH_MAX_NAMES ][TPM2_MAX_NAME_SIZE ]; -u16 name_size_tbl[AUTH_MAX_NAMES ]; +u8 name[TPM2_MAX_NAME_SIZE ]; +u16 name_size; };#ifdef CONFIG_TCG_TPM2_HMAC
drivers/char/tpm/tpm2-sessions.c EXPORT_SYMBOL_GPL(tpm2_read_public); · line 264
Quoted 4 lines
drivers/char/tpm/tpm2-sessions.c +0
-1@@ -261,11 +258,14 @@ EXPORT_SYMBOL_GPL(tpm2_read_public);int tpm_buf_append_name (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle,u8 *name,u16 name_size) { -#ifdef CONFIG_TCG_TPM2_HMAC
Removing CONFIG_TCG_TPM2_HMAC here causes a warning during compile since the auth variable is only used in the CONFIG_TCG_TPM2_HMAC block below.
Ross
Quoted 55 lines
drivers/char/tpm/tpm2-sessions.c +9
-14struct tpm2_auth *auth; -int slot;int ret; -#endif + +if (tpm_buf_length (buf) !=TPM_HEADER_SIZE ) { +dev_err (&chip->dev,"too many handles\n" ); + ret = -EIO ; + goto err; + }if (!tpm2_chip_auth (chip)) {tpm_buf_append_handle (chip, buf, handle);@@ -273,12 +273,6 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }#ifdef CONFIG_TCG_TPM2_HMAC - slot = (tpm_buf_length (buf) -TPM_HEADER_SIZE ) /4 ; -if (slot >=AUTH_MAX_NAMES ) { -dev_err (&chip->dev,"too many handles\n" ); - ret = -EIO ; - goto err; - } auth = chip->auth;if (auth->session !=tpm_buf_length (buf)) {dev_err (&chip->dev,"session state malformed" );@@ -287,16 +281,14 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }tpm_buf_append_u32 (buf, handle); auth->session +=4 ; -memcpy (auth->name[slot], name, name_size); - auth->name_size_tbl[slot] = name_size; +memcpy (auth->name, name, name_size); + auth->name_size = name_size;#endif return 0 ; -#ifdef CONFIG_TCG_TPM2_HMAC err:tpm2_end_auth_session (chip);return ret; -#endif }EXPORT_SYMBOL_GPL (tpm_buf_append_name);@@ -665,8 +657,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)/* ordinal is already BE */ sha256_update (&sctx, (u8 *)&head->ordinal,sizeof (head->ordinal));/* add the handle names */ -for (i =0 ; i < handles; i++) -sha256_update (&sctx, auth->name[i], auth->name_size_tbl[i]); +sha256_update (&sctx, auth->name, auth->name_size);if (offset_s !=tpm_buf_length (buf))sha256_update (&sctx, &buf->data[offset_s],tpm_buf_length (buf) - offset_s);
Jarkko Sakkinen
maintainerRe: [PATCH v6 05/11] tpm2-sessions: Remove AUTH_MAX_NAMES
Quoted 31 lines · On Mon, Jan 12, 2026 at 04:22:24PM -0800, [email protected] wrote:
Quoted 30 lines · Jarkko Sakkinen
In all of the call sites only one session is ever append. Thus, reduce
AUTH_MAX_NAMES, which leads into removing constant completely.Signed-off-by Jarkko Sakkinen <[email protected]>--- drivers/char/tpm/tpm2-sessions.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-)drivers/char/tpm/tpm2-sessions.c +2
-5@@ -72,9 +72,6 @@#include <crypto/sha2.h> #include <crypto/utils.h> -/* maximum number of names the TPM must remember for authorization */ -#define AUTH_MAX_NAMES 3 -#define AES_KEY_BYTES AES_KEYSIZE_128#define AES_KEY_BITS (AES_KEY_BYTES*8)@@ -136,8 +133,8 @@ struct tpm2_auth {* handle, but they are part of the session by name, which * we must compute and remember */ -u8 name[AUTH_MAX_NAMES ][TPM2_MAX_NAME_SIZE ]; -u16 name_size_tbl[AUTH_MAX_NAMES ]; +u8 name[TPM2_MAX_NAME_SIZE ]; +u16 name_size; };#ifdef CONFIG_TCG_TPM2_HMAC
Quoted 8 lines
drivers/char/tpm/tpm2-sessions.c EXPORT_SYMBOL_GPL(tpm2_read_public); · line 264
Quoted 5 lines
drivers/char/tpm/tpm2-sessions.c +0
-1@@ -261,11 +258,14 @@ EXPORT_SYMBOL_GPL(tpm2_read_public);int tpm_buf_append_name (struct tpm_chip *chip,struct tpm_buf *buf,u32 handle,u8 *name,u16 name_size) { -#ifdef CONFIG_TCG_TPM2_HMAC Removing
CONFIG_TCG_TPM2_HMAChere causes a warning during compile since the auth variable is only used in theCONFIG_TCG_TPM2_HMACblock below.
Thanks for the remark, I'll look into this.
I should have next week bandwidth to look into your patch set too (still rebooting from the holidays)
Quoted 46 lines
Ross
Quoted 43 lines
drivers/char/tpm/tpm2-sessions.c +8
-12struct tpm2_auth *auth; -int slot;int ret; -#endif + +if (tpm_buf_length (buf) !=TPM_HEADER_SIZE ) { +dev_err (&chip->dev,"too many handles\n" ); + ret = -EIO ; + goto err; + }if (!tpm2_chip_auth (chip)) {tpm_buf_append_handle (chip, buf, handle);@@ -273,12 +273,6 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }#ifdef CONFIG_TCG_TPM2_HMAC - slot = (tpm_buf_length (buf) -TPM_HEADER_SIZE ) /4 ; -if (slot >=AUTH_MAX_NAMES ) { -dev_err (&chip->dev,"too many handles\n" ); - ret = -EIO ; - goto err; - } auth = chip->auth;if (auth->session !=tpm_buf_length (buf)) {dev_err (&chip->dev,"session state malformed" );@@ -287,16 +281,14 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }tpm_buf_append_u32 (buf, handle); auth->session +=4 ; -memcpy (auth->name[slot], name, name_size); - auth->name_size_tbl[slot] = name_size; +memcpy (auth->name, name, name_size); + auth->name_size = name_size;#endif return 0 ; -#ifdef CONFIG_TCG_TPM2_HMAC err:tpm2_end_auth_session (chip);return ret; -#endif }EXPORT_SYMBOL_GPL (tpm_buf_append_name);@@ -665,8 +657,7 @@ int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf)/* ordinal is already BE */
Quoted 9 lines
drivers/char/tpm/tpm2-sessions.c int tpm_buf_fill_hmac_session(struct tpm_chip *chip, struct tpm_buf *buf) · line 663
Quoted 8 lines
drivers/char/tpm/tpm2-sessions.c +1
-2sha256_update (&sctx, (u8 *)&head->ordinal,sizeof (head->ordinal));/* add the handle names */ -for (i =0 ; i < handles; i++) -sha256_update (&sctx, auth->name[i], auth->name_size_tbl[i]); +sha256_update (&sctx, auth->name, auth->name_size);if (offset_s !=tpm_buf_length (buf))sha256_update (&sctx, &buf->data[offset_s],tpm_buf_length (buf) - offset_s);
BR, Jarkko
Jarkko Sakkinen
maintainerPATCH v6 6/11 tpm: Orchestrate TPM commands in tpm_get_random()
In reply to Jarkko Sakkinentpm1_get_random() and tpm2_get_random() contain duplicate orchestration code. Consolidate orchestration to tpm_get_random().
--- drivers/char/tpm/tpm-interface.c | 175 +++++++++++++++++++++++++++++-- drivers/char/tpm/tpm.h | 2 - drivers/char/tpm/tpm1-cmd.c | 69 ------------ drivers/char/tpm/tpm2-cmd.c | 104 ------------------ 4 files changed, 164 insertions(+), 186 deletions(-)
drivers/char/tpm/tpm-interface.c +164 -11
@@ -26,7 +26,7 @@#include <linux/suspend.h> #include <linux/freezer.h> #include <linux/tpm_eventlog.h> - +#include <linux/tpm_command.h> #include "tpm.h" /*
@@ -486,19 +486,153 @@ int tpm_pm_resume(struct device *dev) }EXPORT_SYMBOL_GPL (tpm_pm_resume); +struct tpm1_get_random_out { +__be32 rng_data_len; +u8 rng_data[TPM_MAX_RNG_DATA ]; +} __packed; + +static int tpm1_get_random (struct tpm_chip *chip,u8 *out,size_t max) +{ +struct tpm1_get_random_out *resp; +struct tpm_buf buf; +u32 recd; +int rc; + +if (!out || !max || max >TPM_MAX_RNG_DATA ) +return -EINVAL ; + + rc =tpm_buf_init (&buf,TPM_TAG_RQU_COMMAND ,TPM_ORD_GETRANDOM ); +if (rc) +return rc; + +tpm_buf_append_u32 (&buf, max); + + rc =tpm_transmit_cmd (chip, &buf,sizeof (resp->rng_data_len),"TPM_GetRandom" ); +if (rc) { +if (rc >0 ) + rc = -EIO ; + goto err; + } + + resp = (struct tpm1_get_random_out *)&buf.data[TPM_HEADER_SIZE ]; + + recd =be32_to_cpu (resp->rng_data_len); +if (recd > max) { + rc = -EIO ; + goto err; + } + +if (buf.length <TPM_HEADER_SIZE +sizeof (resp->rng_data_len) + recd) { + rc = -EIO ; + goto err; + } + +memcpy (out, resp->rng_data, recd); +tpm_buf_destroy (&buf); +return recd; + +err: +tpm_buf_destroy (&buf); +return rc; +} + +struct tpm2_get_random_out { +__be16 size; +u8 buffer[TPM_MAX_RNG_DATA ]; +} __packed; + +static int tpm2_get_random (struct tpm_chip *chip,u8 *out,size_t max) +{ +struct tpm2_get_random_out *resp; +struct tpm_header *head; +struct tpm_buf buf; +off_t offset; +u32 recd; +int ret; + +if (!out || !max || max >TPM_MAX_RNG_DATA ) +return -EINVAL ; + + ret =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_GET_RANDOM ); +if (ret) +return ret; + +if (tpm2_chip_auth (chip)) { +tpm_buf_append_hmac_session (chip, &buf, +TPM2_SA_ENCRYPT |TPM2_SA_CONTINUE_SESSION , +NULL ,0 ); + }else { + head = (struct tpm_header *)buf.data; + head->tag =cpu_to_be16 (TPM2_ST_NO_SESSIONS ); + } +tpm_buf_append_u16 (&buf, max); + + ret =tpm_buf_fill_hmac_session (chip, &buf); +if (ret) { +tpm_buf_destroy (&buf); +return ret; + } + + ret =tpm_transmit_cmd (chip, &buf, offsetof(struct tpm2_get_random_out , buffer), +"TPM2_GetRandom" ); + + ret =tpm_buf_check_hmac_response (chip, &buf, ret); +if (ret) { +if (ret >0 ) + ret = -EIO ; + + goto out; + } + + head = (struct tpm_header *)buf.data; + offset =TPM_HEADER_SIZE ; + +/* Skip the parameter size field: */ +if (be16_to_cpu (head->tag) ==TPM2_ST_SESSIONS ) + offset +=4 ; + + resp = (struct tpm2_get_random_out *)&buf.data[offset]; + recd =min_t (u32,be16_to_cpu (resp->size), max); + +if (tpm_buf_length (&buf) < +TPM_HEADER_SIZE + offsetof(struct tpm2_get_random_out , buffer) + recd) { + ret = -EIO ; + goto out; + } + +memcpy (out, resp->buffer, recd); +return recd; + +out: +tpm2_end_auth_session (chip); +tpm_buf_destroy (&buf); +return ret; +} +/** -* tpm_get_random() - get random bytes from the TPM's RNG -* @chip: a &struct tpm_chip instance, %NULL for the default chip -* @out: destination buffer for the random bytes -* @max: the max number of bytes to write to @out +* tpm_get_random() - Get random bytes from the TPM's RNG +* @chip: A &tpm_chip instance. Whenset to %NULL, the default chip is used. +* @out: Destination buffer for the acquired random bytes. +* @max: The maximum number of bytes to write to @out. +* +* Iterates pulling more bytes from TPM up until all of the @max bytes have been +* received. * -* Return: number of random bytes read or a negative error value. +* Returns the number of random bytes read on success. +* Returns -EINVAL when @out is NULL, or @max is not between zero and +* %TPM_MAX_RNG_DATA. +* Returns tpm_transmit_cmd() error codes when the TPM command results an +* error. */ int tpm_get_random (struct tpm_chip *chip,u8 *out,size_t max) { +u32 num_bytes = max; +u8 *out_ptr = out; +int retries =5 ; +int total =0 ;int rc; -if (!out || max >TPM_MAX_RNG_DATA ) +if (!out || !max || max >TPM_MAX_RNG_DATA )return -EINVAL ;if (!chip)
@@ -508,11 +642,30 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)if (rc)return rc; -if (chip->flags &TPM_CHIP_FLAG_TPM2 ) - rc =tpm2_get_random (chip, out, max); -else - rc =tpm1_get_random (chip, out, max); +if (chip->flags &TPM_CHIP_FLAG_TPM2 ) { + rc =tpm2_start_auth_session (chip); +if (rc) +return rc; + } + +do { +if (chip->flags &TPM_CHIP_FLAG_TPM2 ) + rc =tpm2_get_random (chip, out_ptr, num_bytes); +else + rc =tpm1_get_random (chip, out_ptr, num_bytes); + +if (rc <0 ) + goto err; + + out_ptr += rc; + total += rc; + num_bytes -= rc; + }while (retries-- && total < max); + +tpm_put_ops (chip); +return total ? total : -EIO ; +err:tpm_put_ops (chip);return rc; }
drivers/char/tpm/tpm.h +0 -2
@@ -251,7 +251,6 @@ int tpm1_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, const u8 *hash,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);
@@ -291,7 +290,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digest, u16 *digest_size_ptr);int tpm2_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digests); -int tpm2_get_random (struct tpm_chip *chip,u8 *dest,size_t max);ssize_t tpm2_get_tpm_pt (struct tpm_chip *chip,u32 property_id,u32 *value,const char *desc);
drivers/char/tpm/tpm1-cmd.c +0 -69
75 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +0 -104
110 lines not on this page · raw message
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 7/11 tpm: Send only one at most TPM2_GetRandom command
In reply to Jarkko Sakkinenhwrng framework does not have a requirement that the all bytes requested need to be provided. By enforcing such a requirement internally, TPM driver can cause unpredictability in latency, as a single tpm_get_random() call can result multiple TPM commands.
Especially, when TCG_TPM2_HMAC is enabled, extra roundtrips could have significant effect to the system latency.
Add a wait-parameter to enforce the old behavior and set it to true only at the call sites for TPM 1.2 keys. At the call sites of hwrng, set @wait to false.
---
v4:
- Fixed grammar mistakes.
--- drivers/char/tpm/tpm-chip.c | 2 +- drivers/char/tpm/tpm-interface.c | 11 +++++++++-- include/linux/tpm.h | 2 +- security/keys/trusted-keys/trusted_tpm1.c | 8 ++++---- 4 files changed, 15 insertions(+), 8 deletions(-)
drivers/char/tpm/tpm-chip.c +1 -1
@@ -494,7 +494,7 @@ static int tpm_hwrng_read(struct hwrng *rng, void *data, size_t max, bool wait) { struct tpm_chip *chip = container_of(rng, struct tpm_chip, hwrng); -return tpm_get_random (chip, data, max); +return tpm_get_random (chip, data, max, false); }static bool tpm_is_hwrng_enabled (struct tpm_chip *chip)
drivers/char/tpm/tpm-interface.c +9 -2
@@ -614,9 +614,11 @@ static int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)* @chip: A &tpm_chip instance. Whenset to %NULL, the default chip is used. * @out: Destination buffer for the acquired random bytes. * @max: The maximum number of bytes to write to @out. +* @wait: Set to true when all of the @max bytes need to be acquired. * * Iterates pulling more bytes from TPM up until all of the @max bytes have been -* received. +* received, when @wait it sets true. Otherwise, the queries for @max bytes from +* TPM exactly once, and returns the bytes that were received. * * Returns the number of random bytes read on success. * Returns -EINVAL when @out is NULL, or @max is not between zero and
@@ -624,7 +626,7 @@ static int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)* Returns tpm_transmit_cmd() error codes when the TPM command results an * error. */ -int tpm_get_random (struct tpm_chip *chip,u8 *out,size_t max) +int tpm_get_random (struct tpm_chip *chip,u8 *out,size_t max,bool wait) {u32 num_bytes = max;u8 *out_ptr = out;
@@ -657,6 +659,11 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max)if (rc <0 ) goto err; +if (!wait) { + total = rc; +break ; + } + out_ptr += rc; total += rc; num_bytes -= rc;
include/linux/tpm.h +1 -1
@@ -487,7 +487,7 @@ extern int tpm_pcr_read(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digest);extern int tpm_pcr_extend (struct tpm_chip *chip,u32 pcr_idx,struct tpm_digest *digests); -extern int tpm_get_random (struct tpm_chip *chip,u8 *data,size_t max); +int tpm_get_random (struct tpm_chip *chip,u8 *out,size_t max,bool wait);extern struct tpm_chip *tpm_default_chip (void );void tpm2_flush_context (struct tpm_chip *chip,u32 handle);int tpm2_find_hash_alg (unsigned int crypto_id);
security/keys/trusted-keys/trusted_tpm1.c +4 -4
@@ -361,7 +361,7 @@ static int osap(struct tpm_buf *tb, struct osapsess *s,unsigned char ononce[TPM_NONCE_SIZE ];int ret; - ret =tpm_get_random (chip, ononce,TPM_NONCE_SIZE ); + ret =tpm_get_random (chip, ononce,TPM_NONCE_SIZE , true);if (ret <0 )return ret;
@@ -454,7 +454,7 @@ static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,memcpy (td->xorwork +SHA1_DIGEST_SIZE , sess.enonce,SHA1_DIGEST_SIZE );sha1 (td->xorwork,SHA1_DIGEST_SIZE *2 , td->xorhash); - ret =tpm_get_random (chip, td->nonceodd,TPM_NONCE_SIZE ); + ret =tpm_get_random (chip, td->nonceodd,TPM_NONCE_SIZE , true);if (ret <0 ) goto out;
@@ -565,7 +565,7 @@ static int tpm_unseal(struct tpm_buf *tb, } ordinal =htonl (TPM_ORD_UNSEAL ); - ret =tpm_get_random (chip, nonceodd,TPM_NONCE_SIZE ); + ret =tpm_get_random (chip, nonceodd,TPM_NONCE_SIZE , true);if (ret <0 )return ret;
@@ -938,7 +938,7 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)static int trusted_tpm_get_random (unsigned char *key,size_t key_len) { -return tpm_get_random (chip, key, key_len); +return tpm_get_random (chip, key, key_len, true); }static int __init init_digests (void )
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 8/11 tpm: In tpm_get_random() replace 'retries' with a zero check
In reply to Jarkko SakkinenCheck for zero byte read instead of having retries counter in order to make wait flag properly enforcing. Progress is still guaranteed given the zero check and iterations are capped up to TPM_MAX_RNG_DATA iterations at most (as per theoretical limit).
--- drivers/char/tpm/tpm-interface.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-)
drivers/char/tpm/tpm-interface.c +6 -3
@@ -630,7 +630,6 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max, bool wait) {u32 num_bytes = max;u8 *out_ptr = out; -int retries =5 ;int total =0 ;int rc;
@@ -656,8 +655,12 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max, bool wait)else rc =tpm1_get_random (chip, out_ptr, num_bytes); -if (rc <0 ) +if (rc <=0 ) { +if (!rc) + rc = -EIO ; + goto err; + }if (!wait) { total = rc;
@@ -667,7 +670,7 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max, bool wait) out_ptr += rc; total += rc; num_bytes -= rc; - }while (retries-- && total < max); + }while (total < max);tpm_put_ops (chip);return total ? total : -EIO ;
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 9/11 tpm-buf: Merge TPM_BUF_BOUNDARY_ERROR and TPM_BUF_OVERFLOW
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Merge 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.
--- 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
@@ -382,12 +382,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
@@ -292,7 +292,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; }
@@ -412,7 +412,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 10/11 tpm-buf: Implement managed allocations
In reply to Jarkko SakkinenFrom: Jarkko Sakkinen <[email protected]>
Decouple kzalloc from buffer creation, so that a managed allocation can be used:
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));
This is achieved by embedding buffer's header inside the allocated blob, instead of having an outer wrapper.
--- drivers/char/tpm/tpm-buf.c | 124 ++++++---- drivers/char/tpm/tpm-interface.c | 66 +++--- drivers/char/tpm/tpm-sysfs.c | 21 +- drivers/char/tpm/tpm.h | 1 - drivers/char/tpm/tpm1-cmd.c | 129 +++++------ drivers/char/tpm/tpm2-cmd.c | 266 ++++++++++------------ drivers/char/tpm/tpm2-sessions.c | 142 ++++++------ drivers/char/tpm/tpm2-space.c | 44 ++-- drivers/char/tpm/tpm_vtpm_proxy.c | 30 ++- include/linux/tpm.h | 20 +- security/keys/trusted-keys/trusted_tpm1.c | 36 +-- security/keys/trusted-keys/trusted_tpm2.c | 173 +++++++------- 12 files changed, 500 insertions(+), 552 deletions(-)
drivers/char/tpm/tpm-buf.c +78 -46
@@ -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
@@ -90,8 +117,11 @@ EXPORT_SYMBOL_GPL(tpm_buf_destroy);* * Return: The number of bytes consumed by the buffer */ -u32 tpm_buf_length (struct tpm_buf *buf) +u16 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-interface.c +28 -38
135 lines not on this page · raw message
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 +56 -73
208 lines not on this page · raw message
drivers/char/tpm/tpm2-cmd.c +120 -146
461 lines not on this page · raw message
drivers/char/tpm/tpm2-sessions.c +65 -77
266 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 +11 -9
@@ -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/* * SHA-512 is, as of today, the largest digest in the TCG algorithm repository.
@@ -389,13 +390,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 {
@@ -426,12 +429,11 @@ 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); +u16 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);void tpm_buf_append_u16 (struct tpm_buf *buf,const u16 value);
security/keys/trusted-keys/trusted_tpm1.c +18 -18
@@ -310,9 +310,10 @@ 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 cmd_len) { -struct tpm_buf buf; +u8 buf_data[512 ]; +struct tpm_buf *buf = (struct tpm_buf *)buf_data;int rc;if (!chip)
@@ -322,11 +323,10 @@ static int trusted_tpm_send(unsigned char *cmd, size_t buflen)if (rc)return rc; - buf.flags =0 ; - buf.length = buflen; - buf.data = cmd; +tpm_buf_init (buf,sizeof (buf_data)); +tpm_buf_append (buf, cmd, cmd_len);dump_tpm_buf (cmd); - rc =tpm_transmit_cmd (chip, &buf,4 ,"sending data" ); + rc =tpm_transmit_cmd (chip, buf,4 ,"sending data" );dump_tpm_buf (cmd);if (rc >0 )
@@ -624,23 +624,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 +650,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 +666,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 +83 -90
308 lines not on this page · raw message
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v6 11/11 tpm-buf: Remove tpm_buf_append_handle
In reply to Jarkko SakkinenSince the number of handles is fixed to a single handle, eliminate all uses of buf->handles and deduce values during compile-time.
---
v2:
- Streamline the code change and remove dead code.
--- drivers/char/tpm/tpm-buf.c | 26 -------------------------- drivers/char/tpm/tpm2-cmd.c | 2 +- drivers/char/tpm/tpm2-sessions.c | 14 ++------------ include/linux/tpm.h | 2 -- 4 files changed, 3 insertions(+), 41 deletions(-)
drivers/char/tpm/tpm-buf.c +0 -26
@@ -40,7 +40,6 @@ static void __tpm_buf_reset(struct tpm_buf *buf, u16 buf_size, u16 tag, u32 ordi 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);
@@ -56,7 +55,6 @@ static void __tpm_buf_reset_sized(struct tpm_buf *buf, u16 buf_size) 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 ; }
@@ -177,30 +175,6 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value) }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) -{ -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 ; - } - -tpm_buf_append_u32 (buf, handle); - buf->handles++; -} -/** * tpm_buf_read() - Read from a TPM buffer * @buf: &tpm_buf instance
drivers/char/tpm/tpm2-cmd.c +1 -1
@@ -205,7 +205,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,return rc;tpm_buf_append_hmac_session (chip, buf,0 ,NULL ,0 ); }else { -tpm_buf_append_handle (chip,buf, pcr_idx); +tpm_buf_append_u32 (buf, pcr_idx);tpm_buf_append_auth (chip, buf,NULL ,0 ); }
drivers/char/tpm/tpm2-sessions.c +2 -12
@@ -261,7 +261,7 @@ int tpm_buf_append_name(struct tpm_chip *chip, struct tpm_buf *buf, }if (!tpm2_chip_auth (chip)) { -tpm_buf_append_handle (chip, buf, handle); +tpm_buf_append_u32 (buf, handle);return 0 ; }
@@ -288,17 +288,7 @@ EXPORT_SYMBOL_GPL(tpm_buf_append_name);void tpm_buf_append_auth (struct tpm_chip *chip,struct tpm_buf *buf,u8 *passphrase,int passphrase_len) { -/* offset tells us where the sessions area begins */ -int offset = buf->handles *4 +TPM_HEADER_SIZE ; -u32 len =9 + passphrase_len; - -if (tpm_buf_length (buf) != offset) { -/* not the first session so update the existing length */ - len +=get_unaligned_be32 (&buf->data[offset]); -put_unaligned_be32 (len, &buf->data[offset]); - }else { -tpm_buf_append_u32 (buf, len); - } +tpm_buf_append_u32 (buf,9 + passphrase_len);/* auth handle */ tpm_buf_append_u32 (buf,TPM2_RS_PW );/* nonce */
include/linux/tpm.h +0 -2
@@ -395,7 +395,6 @@ enum tpm_buf_flags {*/ struct tpm_buf {u8 flags; -u8 handles;u16 length;u16 capacity;u8 data[];
@@ -441,7 +440,6 @@ 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);/* * Check if TPM device is in the firmware upgrade mode.
Signature · 1 line
2.39.5