Streamline TPM2 HMAC sessions
Subsystems (4)
Jarkko Sakkinen
maintainerv7 cover Streamline TPM2 HMAC sessions
This patch set contains accumulated patches, which gradually improve TPM2 HMAC session management and TPM driver memory management.
RNG test
========
I run this test both TPM1 and TPM2 chips using QEMU and swtpm:
#!/bin/sh
ctrl_c() {
kill -15 $TEST_PID
echo 0 > tracing_on
echo nop > current_tracer
echo > kprobe_events
echo > set_ftrace_filter
echo BYE
exit
}
trap ctrl_c EXIT INT
mount -t tracefs none /sys/kernel/tracing
set -e
cd /sys/kernel/tracing
echo function > current_tracer
echo p:tpm_get_random tpm_get_random > kprobe_events echo tpm_get_random > set_ftrace_filter
echo 1 > tracing_on
TEST_PID=$(cat /dev/hwrng > /dev/null &) echo > trace
cat trace_pipe &
sleep 10
Change Log
==========
v7:
- Updated cover letter to match better the current state of the patch set.
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 (12):
KEYS: trusted: Use get_random-fallback for TPM
KEYS: trusted: Use get_random_bytes_wait() instead of tpm_get_random()
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: Change tpm_get_random() opportunistic
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-interface.c | 145 ++++++++- 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 | 77 +++-- security/keys/trusted-keys/trusted_tpm1.c | 70 ++-- security/keys/trusted-keys/trusted_tpm2.c | 329 ++++++++++--------- 12 files changed, 776 insertions(+), 940 deletions(-)
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v7 1/12 KEYS: trusted: Use get_random-fallback for TPM
1. tpm2_get_random() is costly when TCG_TPM2_HMAC is enabled and thus its use should be pooled rather than directly used. This both reduces latency and improves its predictability.
2. Linux is better off overall if every subsystem uses the same source for generating the random numbers required.
Thus, unset '.get_random', which causes fallback to kernel_get_random().
One might argue that TPM RNG should be used for the generated trusted keys, so that they have matching entropy with the TPM internally generated objects.
This argument does have some weight into it but as far cryptography goes, FIPS certification sets the exact bar, not which exact FIPS certified RNG will be used. Thus, the rational choice is obviously to pick the lowest latency path, which is kernel RNG.
Finally, there is an actual defence in depth benefit when using kernel RNG as it helps to mitigate TPM firmware bugs concerning RNG implementation, given the obfuscation by the other entropy sources.
---
v7:
- A new patch. Simplifies follow up patches.
--- security/keys/trusted-keys/trusted_tpm1.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +10 -6
@@ -6,6 +6,16 @@* See Documentation/security/keys/trusted-encrypted.rst */ +/** +* DOC: Random Number Generation +* +* tpm_get_random() was previously used here as the RNG in order to have equal +* entropy with the objects fully inside the TPM. However, as far as goes, +* kernel RNG is equally fine, as long as long as it is FIPS certified. Also, +* using kernel RNG has the benefit of mitigating bugs in the TPM firmware +* associated with the RNG. +*/ +#include <crypto/hash_info.h> #include <crypto/sha1.h> #include <crypto/utils.h>
@@ -936,11 +946,6 @@ static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)return ret; } -static int trusted_tpm_get_random (unsigned char *key,size_t key_len) -{ -return tpm_get_random (chip, key, key_len); -} -static int __init init_digests (void ) {int i;
@@ -992,6 +997,5 @@ struct trusted_key_ops trusted_key_tpm_ops = { .init = trusted_tpm_init, .seal = trusted_tpm_seal, .unseal = trusted_tpm_unseal, - .get_random = trusted_tpm_get_random, .exit = trusted_tpm_exit, };
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v7 2/12 KEYS: trusted: Use get_random_bytes_wait() instead of tpm_get_random()
In reply to Jarkko SakkinenSubstitute remaining tpm_get_random() calls in trusted_tpm1.c with get_random_bytes_wait() thus aligning random number generation for TPM 1.2 with the removal of '.get_random' callback.
--- security/keys/trusted-keys/trusted_tpm1.c | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-)
security/keys/trusted-keys/trusted_tpm1.c +3 -15
@@ -371,13 +371,10 @@ 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 =get_random_bytes_wait (ononce,TPM_NONCE_SIZE );if (ret <0 )return ret; -if (ret !=TPM_NONCE_SIZE ) -return -EIO ; -tpm_buf_reset (tb,TPM_TAG_RQU_COMMAND ,TPM_ORD_OSAP );tpm_buf_append_u16 (tb, type);tpm_buf_append_u32 (tb, handle);
@@ -464,15 +461,10 @@ 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 =get_random_bytes_wait (td->nonceodd,TPM_NONCE_SIZE );if (ret <0 ) goto out; -if (ret !=TPM_NONCE_SIZE ) { - ret = -EIO ; - goto out; - } - ordinal =htonl (TPM_ORD_SEAL ); datsize =htonl (datalen); pcrsize =htonl (pcrinfosize);
@@ -575,14 +567,10 @@ static int tpm_unseal(struct tpm_buf *tb, } ordinal =htonl (TPM_ORD_UNSEAL ); - ret =tpm_get_random (chip, nonceodd,TPM_NONCE_SIZE ); + ret =get_random_bytes_wait (nonceodd,TPM_NONCE_SIZE );if (ret <0 )return ret; -if (ret !=TPM_NONCE_SIZE ) { -pr_info ("tpm_get_random failed (%d)\n" , ret); -return -EIO ; - } ret =TSS_authhmac (authdata1, keyauth,TPM_NONCE_SIZE , enonce1, nonceodd, cont,sizeof (uint32_t ), &ordinal, bloblen, blob,0 ,0 );
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v7 3/12 tpm2-sessions: Define TPM2_NAME_MAX_SIZE
In reply to Jarkko SakkinenDefine 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 v7 4/12 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 v7 5/12 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 v7 6/12 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 v7 7/12 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
Jarkko Sakkinen
maintainerPATCH v7 8/12 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 v7 9/12 tpm: Change tpm_get_random() opportunistic
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.
Thus, send TPM command only once and return bytes received instead of committing to the number of requested bytes.
---
v7:
- Given that hwrng is now only caller for tpm_get_random(), remove the wait parameter.
v4:
- Fixed grammar mistakes.
--- drivers/char/tpm/tpm-interface.c | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-)
drivers/char/tpm/tpm-interface.c +5 -23
@@ -626,10 +626,6 @@ static int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)*/ 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 || max >TPM_MAX_RNG_DATA )
@@ -646,28 +642,14 @@ int tpm_get_random(struct tpm_chip *chip, u8 *out, size_t max) 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 ; + rc =tpm2_get_random (chip, out, max); + }else { + rc =tpm1_get_random (chip, out, max); + } -err:tpm_put_ops (chip); -return rc; +return rc !=0 ? rc : -EIO ; }EXPORT_SYMBOL_GPL (tpm_get_random);
Signature · 1 line
2.39.5
Jarkko Sakkinen
maintainerPATCH v7 10/12 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 v7 11/12 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
@@ -320,9 +320,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)
@@ -332,11 +333,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 )
@@ -622,23 +622,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; }
@@ -648,14 +648,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);
@@ -663,7 +664,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 v7 12/12 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
Jarkko Sakkinen
maintainerQuoted 25 lines · Jarkko Sakkinen
This patch set contains accumulated patches, which gradually improve TPM2 HMAC session management and TPM driver memory management.
RNG test
========I run this test both TPM1 and TPM2 chips using QEMU and swtpm:
#!/bin/sh
ctrl_c(){
kill -15 $TEST_PID
echo 0 >tracing_on
echo nop >current_tracer
echo >kprobe_events
echo >set_ftrace_filter
echo BYE
exit
}trap
ctrl_cEXIT INT
mount -t tracefs none/sys/kernel/tracingset -e
Quoted 9 lines
cd
/sys/kernel/tracing
echo function >current_tracer
echo p:tpm_get_randomtpm_get_random>kprobe_eventsechotpm_get_random>set_ftrace_filter
echo 1 >tracing_on
TEST_PID=$(cat/dev/hwrng>/dev/null&) echo > trace
cattrace_pipe&
sleep 10
Oops, and old version. This was the one I run:
#!/bin/sh
ctrl_c() {
set +e
echo 0 > tracing_on
echo nop > current_tracer
echo BYE
exit
}
trap ctrl_c EXIT INT
mount -t tracefs none /sys/kernel/tracing
set -e
cd /sys/kernel/tracing
echo function > current_tracer
echo p:tpm_get_random tpm_get_random > kprobe_events echo tpm_get_random > set_ftrace_filter
echo 1 > tracing_on
cat /dev/hwrng > /dev/null &
echo > trace
cat trace_pipe &
sleep 10
Quoted 31 lines
Change Log
==========v7:
- Updated cover letter to match better the current state of the patch set.
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 (12):
KEYS: trusted: Use get_random-fallback for TPM
KEYS: trusted: Useget_random_bytes_wait()instead oftpm_get_random()
tpm2-sessions: DefineTPM2_NAME_MAX_SIZE
KEYS: trusted: Open codetpm2_buf_append()
KEYS: trusted: Remove dead branch fromtpm2_unseal_cmd
KEYS: trusted: Re-orchestratetpm2_read_public()calls
tpm2-sessions: RemoveAUTH_MAX_NAMES
tpm: Orchestrate TPM commands intpm_get_random()
tpm: Changetpm_get_random()opportunistic
tpm-buf: MergeTPM_BUF_BOUNDARY_ERRORandTPM_BUF_OVERFLOW
tpm-buf: Implement managed allocations
tpm-buf: Removetpm_buf_append_handle
Quoted 17 lines
drivers/char/tpm/tpm-buf.c | 154 ++++----- drivers/char/tpm/tpm-interface.c | 145 ++++++++- 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 | 77 +++-- security/keys/trusted-keys/trusted_tpm1.c | 70 ++-- security/keys/trusted-keys/trusted_tpm2.c | 329 ++++++++++--------- 12 files changed, 776 insertions(+), 940 deletions(-)Signature · 1 line
2.39.5
BR, Jarkko