add integrity and security to TPM2 transactions
Subsystems (5)
James Bottomley
maintainerv6 cover add integrity and security to TPM2 transactions
Link to previous cover letter:
https://lore.kernel.org/linux-integrity/[email protected]/
This is marked v6 instead of v5 because I did a v5 after feedback on v4 but didn't get around to posting it and then had to rework the whole of the kernel space handling while I was on holiday. I also added the documentation of how the whole thing works and the rationale for doing it in tpm-security.rst (patch 11). The main reason for doing this now is so we have something to discuss at Plumbers.
The new patch set implements the various splits requested, but the main changes are that the kernel space is gone and is replaced by a context save and restore of the generated null seed. This is easier to handle than a full kernel space given the new threading for TPM spaces, but conceptually it is still very like a space. I've also made whether integrity and encryption is turned on a Kconfig option.
James
---
James Bottomley (12):
tpm-buf: move from static inlines to real functions
tpm-buf: add handling for TPM2B types
tpm-buf: add cursor based functions for response parsing
tpm2-space: export the context save and load commands
tpm2-sessions: Add full HMAC and encrypt/decrypt session handling
tpm-buf: add tpm_buf_parameters()
tpm2: add hmac checks to tpm2_pcr_extend()
tpm2: add session encryption protection to tpm2_get_random() trusted keys: Add session encryption protection to the seal/unseal
path
tpm: add the null key name as a tpm2 sysfs variable
Documentation: add tpm-security.rst
tpm2-sessions: NOT FOR COMMITTING add sessions testing
Documentation/security/tpm/tpm-security.rst | 204 +++++ drivers/char/tpm/Kconfig | 11 + drivers/char/tpm/Makefile | 4 + drivers/char/tpm/tpm-buf.c | 202 +++++ drivers/char/tpm/tpm-chip.c | 1 + drivers/char/tpm/tpm-sysfs.c | 27 +- drivers/char/tpm/tpm.h | 117 +-- drivers/char/tpm/tpm2-cmd.c | 202 +++-- drivers/char/tpm/tpm2-sessions-test.c | 795 ++++++++++++++++++ drivers/char/tpm/tpm2-sessions.c | 1204 +++++++++++++++++++++++++++ drivers/char/tpm/tpm2-sessions.h | 138 +++ drivers/char/tpm/tpm2-space.c | 8 +- include/linux/tpm.h | 29 + 13 files changed, 2787 insertions(+), 155 deletions(-) create mode 100644 Documentation/security/tpm/tpm-security.rst create mode 100644 drivers/char/tpm/tpm-buf.c create mode 100644 drivers/char/tpm/tpm2-sessions-test.c create mode 100644 drivers/char/tpm/tpm2-sessions.c create mode 100644 drivers/char/tpm/tpm2-sessions.h
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 1/12 tpm-buf: move from static inlines to real functions
This separates out the old tpm_buf_... handling functions from static inlines in tpm.h and makes them their own tpm-buf.c file. This is a precursor so we can add new functions for other TPM type handling
---
v2: added this patch to separate out the API changes
v3: added tpm_buf_reset_cmd()
v6: extract first then add functions
--- drivers/char/tpm/Makefile | 1 + drivers/char/tpm/tpm-buf.c | 118 +++++++++++++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm.h | 90 ++++------------------------------ 3 files changed, 129 insertions(+), 80 deletions(-) create mode 100644 drivers/char/tpm/tpm-buf.c
drivers/char/tpm/Makefile +1 -0
@@ -15,6 +15,7 @@ tpm-y += tpm-sysfs.o tpm-y += eventlog/common.o tpm-y += eventlog/tpm1.o tpm-y += eventlog/tpm2.o +tpm-y += tpm-buf.o tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o tpm-$(CONFIG_EFI) += eventlog/efi.o
drivers/char/tpm/tpm-buf.c new file +118 -0
118 lines not on this page · raw message
drivers/char/tpm/tpm.h +10 -80
102 lines not on this page · raw message
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 2/12 tpm-buf: add handling for TPM2B types
In reply to James BottomleyMost complex TPM commands require appending TPM2B buffers to the command body. Since TPM2B types are essentially variable size arrays, it makes it impossible to represent these complex command arguments as structures and we simply have to build them up using append primitives like these.
--- drivers/char/tpm/tpm-buf.c | 47 ++++++++++++++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm.h | 2 ++ 2 files changed, 49 insertions(+)
drivers/char/tpm/tpm-buf.c +47 -0
@@ -8,6 +8,8 @@#include <linux/module.h> +#include <asm/unaligned.h> +static int __tpm_buf_init (struct tpm_buf *buf) { buf->data_page =alloc_page (GFP_HIGHUSER );
@@ -46,6 +48,24 @@ int tpm_buf_init(struct tpm_buf *buf, u16 tag, u32 ordinal) }EXPORT_SYMBOL_GPL (tpm_buf_init); +int tpm_buf_init_2b (struct tpm_buf *buf) +{ +struct tpm_header *head; +int rc; + + rc =__tpm_buf_init (buf); +if (rc) +return rc; + + head = (struct tpm_header *) buf->data; + + head->length =cpu_to_be32 (sizeof (*head)); + + buf->flags =TPM_BUF_2B ; +return 0 ; +} +EXPORT_SYMBOL_GPL (tpm_buf_init_2b); +void tpm_buf_destroy (struct tpm_buf *buf) {kunmap (buf->data_page);
@@ -53,6 +73,13 @@ void tpm_buf_destroy(struct tpm_buf *buf) }EXPORT_SYMBOL_GPL (tpm_buf_destroy); +static void *tpm_buf_data (struct tpm_buf *buf) +{ +if (buf->flags &TPM_BUF_2B ) +return buf->data +TPM_HEADER_SIZE ; +return buf->data; +} +u32 tpm_buf_length (struct tpm_buf *buf) {struct tpm_header *head = (struct tpm_header *)buf->data;
@@ -116,3 +143,23 @@ void tpm_buf_append_u32(struct tpm_buf *buf, const u32 value)tpm_buf_append (buf, (u8 *) &value2,4 ); }EXPORT_SYMBOL_GPL (tpm_buf_append_u32); + +static void tpm_buf_reset_int (struct tpm_buf *buf) +{ +struct tpm_header *head; + + head = (struct tpm_header *)buf->data; + head->length =cpu_to_be32 (sizeof (*head)); +} + +void tpm_buf_append_2b (struct tpm_buf *buf,struct tpm_buf *tpm2b) +{ +u16 len =tpm_buf_length (tpm2b); + +tpm_buf_append_u16 (buf, len); +tpm_buf_append (buf,tpm_buf_data (tpm2b), len); +/* clear the buf for reuse */ +tpm_buf_reset_int (tpm2b); +} +EXPORT_SYMBOL_GPL (tpm_buf_append_2b); +
drivers/char/tpm/tpm.h +2 -0
@@ -292,6 +292,7 @@ struct tpm_buf {int tpm_buf_init (struct tpm_buf *buf,u16 tag,u32 ordinal);void tpm_buf_reset (struct tpm_buf *buf,u16 tag,u32 ordinal); +int tpm_buf_init_2b (struct tpm_buf *buf);void tpm_buf_destroy (struct tpm_buf *buf);u32 tpm_buf_length (struct tpm_buf *buf); void tpm_buf_append(struct tpm_buf *buf, const unsigned char *new_data,
@@ -299,6 +300,7 @@ void tpm_buf_append(struct tpm_buf *buf, const unsigned char *new_data,void tpm_buf_append_u8 (struct tpm_buf *buf,const u8 value);void tpm_buf_append_u16 (struct tpm_buf *buf,const u16 value);void tpm_buf_append_u32 (struct tpm_buf *buf,const u32 value); +void tpm_buf_append_2b (struct tpm_buf *buf,struct tpm_buf *tpm2b);extern struct class *tpm_class;extern struct class *tpmrm_class;
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 3/12 tpm-buf: add cursor based functions for response parsing
In reply to James BottomleyIt's very convenient when parsing responses to have a cursor you simply move over the response extracting the data. Add such cursor functions for the TPM unsigned integer types.
--- drivers/char/tpm/tpm-buf.c | 26 ++++++++++++++++++++++++++ drivers/char/tpm/tpm.h | 4 ++++ 2 files changed, 30 insertions(+)
drivers/char/tpm/tpm-buf.c +26 -0
@@ -163,3 +163,29 @@ void tpm_buf_append_2b(struct tpm_buf *buf, struct tpm_buf *tpm2b) }EXPORT_SYMBOL_GPL (tpm_buf_append_2b); +/* functions for unmarshalling data and moving the cursor */ +u8 tpm_get_inc_u8 (const u8 **ptr) +{ +return *((*ptr)++); +} +EXPORT_SYMBOL_GPL (tpm_get_inc_u8); + +u16 tpm_get_inc_u16 (const u8 **ptr) +{ +u16 val; + + val =get_unaligned_be16 (*ptr); + *ptr +=sizeof (val); +return val; +} +EXPORT_SYMBOL_GPL (tpm_get_inc_u16); + +u32 tpm_get_inc_u32 (const u8 **ptr) +{ +u32 val; + + val =get_unaligned_be32 (*ptr); + *ptr +=sizeof (val); +return val; +} +EXPORT_SYMBOL_GPL (tpm_get_inc_u32);
drivers/char/tpm/tpm.h +4 -0
@@ -302,6 +302,10 @@ void tpm_buf_append_u16(struct tpm_buf *buf, const u16 value);void tpm_buf_append_u32 (struct tpm_buf *buf,const u32 value);void tpm_buf_append_2b (struct tpm_buf *buf,struct tpm_buf *tpm2b); +u8 tpm_get_inc_u8 (const u8 **ptr); +u16 tpm_get_inc_u16 (const u8 **ptr); +u32 tpm_get_inc_u32 (const u8 **ptr); +extern struct class *tpm_class;extern struct class *tpmrm_class;extern dev_t tpm_devt;
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 4/12 tpm2-space: export the context save and load commands
In reply to James BottomleyThe TPM2 session handling code needs to save and restore a single volatile context for the elliptic curve version of the NULL seed, so export the APIs which do this for internal use.
--- drivers/char/tpm/tpm.h | 4 ++++ drivers/char/tpm/tpm2-space.c | 8 ++++---- 2 files changed, 8 insertions(+), 4 deletions(-)
drivers/char/tpm/tpm.h +4 -0
@@ -399,6 +399,10 @@ int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u8 *cmd, size_t cmdsiz);int tpm2_commit_space (struct tpm_chip *chip,struct tpm_space *space,void *buf,size_t *bufsiz); +int tpm2_save_context (struct tpm_chip *chip,u32 handle,u8 *buf, +unsigned int buf_size,unsigned int *offset); +int tpm2_load_context (struct tpm_chip *chip,u8 *buf, +unsigned int *offset,u32 *handle);int tpm_bios_log_setup (struct tpm_chip *chip);void tpm_bios_log_teardown (struct tpm_chip *chip);
drivers/char/tpm/tpm2-space.c +4 -4
@@ -65,8 +65,8 @@ void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space)kfree (space->session_buf); } -static int tpm2_load_context (struct tpm_chip *chip,u8 *buf, -unsigned int *offset,u32 *handle) +int tpm2_load_context (struct tpm_chip *chip,u8 *buf, +unsigned int *offset,u32 *handle) {struct tpm_buf tbuf;struct tpm2_context *ctx;
@@ -116,8 +116,8 @@ static int tpm2_load_context(struct tpm_chip *chip, u8 *buf,return 0 ; } -static int tpm2_save_context (struct tpm_chip *chip,u32 handle,u8 *buf, -unsigned int buf_size,unsigned int *offset) +int tpm2_save_context (struct tpm_chip *chip,u32 handle,u8 *buf, +unsigned int buf_size,unsigned int *offset) {struct tpm_buf tbuf;unsigned int body_size;
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 5/12 tpm2-sessions: Add full HMAC and encrypt/decrypt session handling
In reply to James BottomleyThis code adds true session based HMAC authentication plus parameter decryption and response encryption using AES.
The basic design of this code is to segregate all the nasty crypto, hash and hmac code into tpm2-sessions.c and export a usable API.
The API first of all starts off by gaining a session with
tpm2_start_auth_session()
Which initiates a session with the TPM and allocates an opaque tpm2_auth structure to handle the session parameters. Then the use is simply:
* tpm_buf_append_name() in place of the tpm_buf_append_u32 for the handles
* tpm_buf_append_hmac_session() where tpm2_append_auth() would go
* tpm_buf_fill_hmac_session() called after the entire command buffer is finished but before tpm_transmit_cmd() is called which computes the correct HMAC and places it in the command at the correct location.
Finally, after tpm_transmit_cmd() is called, tpm_buf_check_hmac_response() is called to check that the returned HMAC matched and collect the new state for the next use of the session, if any.
The features of the session is controlled by the session attributes set in tpm_buf_append_hmac_session(). If TPM2_SA_CONTINUE_SESSION is not specified, the session will be flushed and the tpm2_auth structure freed in tpm_buf_check_hmac_response(); otherwise the session may be used again. Parameter encryption is specified by or'ing the flag TPM2_SA_DECRYPT and response encryption by or'ing the flag TPM2_SA_ENCRYPT. the various encryptions will be taken care of by tpm_buf_fill_hmac_session() and tpm_buf_check_hmac_response() respectively.
To get all of this to work securely, the Kernel now needs a primary key to encrypt the session salt to, so we derive an EC key from the NULL seed and save its context in the tpm_chip structure. The context is loaded on demand into an available volatile handle when tpm_start_auth_session() is called, but is flushed before that function exits to conserve handles.
---
v2: Added docbook and improved response check API
v3: Add readpublic, fix hmac length, add API for close on error
allow for the hmac session not being first in the sessions
v4: Make NULL seed template exactly match the SRK ECC template.
Also check the NULL primary key name is what getpublic returns to prevent spoofing. Also parametrise the name size for reuse
v5: Move to sync_skcipher API
v6: eliminate kernel space and use context save for null seed and
make feature conditional on CONFIG_TPM_BUS_SECURITY
--- drivers/char/tpm/Kconfig | 11 + drivers/char/tpm/Makefile | 1 + drivers/char/tpm/tpm-buf.c | 1 + drivers/char/tpm/tpm.h | 20 +- drivers/char/tpm/tpm2-cmd.c | 22 +- drivers/char/tpm/tpm2-sessions.c | 1203 ++++++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm2-sessions.h | 137 +++++ include/linux/tpm.h | 29 + 8 files changed, 1411 insertions(+), 13 deletions(-) create mode 100644 drivers/char/tpm/tpm2-sessions.c create mode 100644 drivers/char/tpm/tpm2-sessions.h
drivers/char/tpm/Kconfig +11 -0
@@ -9,6 +9,9 @@ menuconfig TCG_TPM imply SECURITYFS select CRYPTO select CRYPTO_HASH_INFO + select CRYPTO_ECDH + select CRYPTO_AES + select CRYPTO_CFB ---help--- If you have a TPM security chip in your system, which implements the Trusted Computing Group's specification,
@@ -27,6 +30,14 @@ menuconfig TCG_TPM if TCG_TPM +config TPM_BUS_SECURITY + bool "Use secure transactions on the TPM bus" + default y + ---help--- + Setting this causes us to deploy a tamper resistent scheme + for communicating with the TPM to prevent or detect bus snooping + attacks like TPM Genie. Saying Y here adds some encryption overhead + to all kernel to TPM transactions. config HW_RANDOM_TPM bool "TPM HW Random Number Generator support" depends on TCG_TPM && HW_RANDOM && !(TCG_TPM=y && HW_RANDOM=m)
drivers/char/tpm/Makefile +1 -0
@@ -17,6 +17,7 @@ tpm-y += eventlog/tpm1.o tpm-y += eventlog/tpm2.o tpm-y += tpm-buf.o +tpm-$(CONFIG_TPM_BUS_SECURITY) += tpm2-sessions.o tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o tpm-$(CONFIG_EFI) += eventlog/efi.o tpm-$(CONFIG_OF) += eventlog/of.o
drivers/char/tpm/tpm-buf.c +1 -0
@@ -31,6 +31,7 @@ void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) head->tag =cpu_to_be16 (tag); head->length =cpu_to_be32 (sizeof (*head)); head->ordinal =cpu_to_be32 (ordinal); + buf->handles =0 ; }EXPORT_SYMBOL_GPL (tpm_buf_reset);
drivers/char/tpm/tpm.h +19 -1
@@ -80,6 +80,7 @@ enum tpm2_timeouts {enum tpm2_structures {TPM2_ST_NO_SESSIONS =0x8001 ,TPM2_ST_SESSIONS =0x8002 , +TPM2_ST_CREATION =0x8021 , };/* Indicates from what layer of the software stack the error comes from */
@@ -116,6 +117,8 @@ enum tpm2_command_codes {TPM2_CC_CONTEXT_LOAD =0x0161 ,TPM2_CC_CONTEXT_SAVE =0x0162 ,TPM2_CC_FLUSH_CONTEXT =0x0165 , +TPM2_CC_READ_PUBLIC =0x0173 , +TPM2_CC_START_AUTH_SESS =0x0176 ,TPM2_CC_VERIFY_SIGNATURE =0x0177 ,TPM2_CC_GET_CAPABILITY =0x017A , TPM2_CC_GET_RANDOM = 0x017B,
@@ -128,6 +131,7 @@ enum tpm2_command_codes { };enum tpm2_permanent_handles { +TPM2_RH_NULL =0x40000007 ,TPM2_RS_PW =0x40000009 , };
@@ -286,7 +290,8 @@ enum tpm_buf_flags {struct tpm_buf {struct page *data_page; -unsigned int flags; +u8 flags; +u8 handles;u8 *data; };
@@ -306,6 +311,9 @@ u8 tpm_get_inc_u8(const u8 **ptr);u16 tpm_get_inc_u16 (const u8 **ptr);u32 tpm_get_inc_u32 (const u8 **ptr); +/* opaque structure, holds auth session parameters like the session key */ +struct tpm2_auth ; +extern struct class *tpm_class;extern struct class *tpmrm_class;extern dev_t tpm_devt;
@@ -408,4 +416,14 @@ int tpm_bios_log_setup(struct tpm_chip *chip);void tpm_bios_log_teardown (struct tpm_chip *chip);int tpm_dev_common_init (void );void tpm_dev_common_exit (void ); + +#ifdef CONFIG_TPM_BUS_SECURITY +int tpm2_sessions_init (struct tpm_chip *chip); +#else +static inline int tpm2_sessions_init (struct tpm_chip *chip) +{ +return 0 ; +} +#endif +#endif
drivers/char/tpm/tpm2-cmd.c +10 -12
@@ -12,17 +12,10 @@*/ #include "tpm.h" +#include "tpm2-sessions.h" #include <crypto/hash_info.h> #include <keys/trusted-type.h> -enum tpm2_object_attributes { -TPM2_OA_USER_WITH_AUTH =BIT (6 ), -}; - -enum tpm2_session_attributes { -TPM2_SA_CONTINUE_SESSION =BIT (0 ), -}; -struct tpm2_hash {unsigned int crypto_id;unsigned int tpm_id;
@@ -388,10 +381,10 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle)* @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) +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);
@@ -1042,6 +1035,11 @@ int tpm2_auto_startup(struct tpm_chip *chip) rc =tpm2_get_cc_attrs_tbl (chip); +if (rc) + goto out; + + rc =tpm2_sessions_init (chip); + out:if (rc >0 ) rc = -ENODEV ;
drivers/char/tpm/tpm2-sessions.c new file +1203 -0
1,203 lines not on this page · raw message
drivers/char/tpm/tpm2-sessions.h new file +137 -0
137 lines not on this page · raw message
include/linux/tpm.h +29 -0
@@ -33,14 +33,23 @@ struct trusted_key_options;enum tpm_algorithms {TPM_ALG_ERROR =0x0000 ,TPM_ALG_SHA1 =0x0004 , +TPM_ALG_AES =0x0006 ,TPM_ALG_KEYEDHASH =0x0008 ,TPM_ALG_SHA256 =0x000B ,TPM_ALG_SHA384 =0x000C ,TPM_ALG_SHA512 =0x000D ,TPM_ALG_NULL =0x0010 ,TPM_ALG_SM3_256 =0x0012 , +TPM_ALG_ECC =0x0023 , +TPM_ALG_CFB =0x0043 , }; +enum tpm2_curves { +TPM2_ECC_NONE =0x0000 , +TPM2_ECC_NIST_P256 =0x0003 , +}; + +struct tpm_digest {u16 alg_id;u8 digest[TPM_MAX_DIGEST_SIZE ];
@@ -105,6 +114,20 @@ struct tpm_chip_seqops {const struct seq_operations *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 2048 +struct tpm_chip {struct device dev;struct device devs;
@@ -159,6 +182,12 @@ struct tpm_chip {/* active locality */ int locality; + +/* details for communication security via sessions */ +u8 tpmkeycontext[TPM2_MAX_CONTEXT_SIZE ];/* context for NULL seed */ +u8 tpmkeyname[TPM2_NAME_SIZE ];/* name of NULL seed */ +u8 ec_point_x[EC_PT_SZ ]; +u8 ec_point_y[EC_PT_SZ ]; };#if defined(CONFIG_TCG_TPM ) || defined(CONFIG_TCG_TPM_MODULE )
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 6/12 tpm-buf: add tpm_buf_parameters()
In reply to James BottomleyIntroducing encryption sessions changes where the return parameters are located in the buffer because if a return session is present they're 4 bytes beyond the header with those 4 bytes showing the parameter length. If there is no return session, then they're in the usual place immediately after the header. The tpm_buf_parameters() encapsulates this calculation and should be used everywhere &buf.data[TPM_HEADER_SIZE] is used now
--- drivers/char/tpm/tpm-buf.c | 10 ++++++++++ drivers/char/tpm/tpm.h | 2 ++ 2 files changed, 12 insertions(+)
drivers/char/tpm/tpm-buf.c +10 -0
@@ -190,3 +190,13 @@ u32 tpm_get_inc_u32(const u8 **ptr)return val; }EXPORT_SYMBOL_GPL (tpm_get_inc_u32); + +u8 *tpm_buf_parameters (struct tpm_buf *buf) +{ +int offset =TPM_HEADER_SIZE ; + +if (tpm_buf_tag (buf) ==TPM2_ST_SESSIONS ) + offset +=4 ; + +return &buf->data[offset]; +}
drivers/char/tpm/tpm.h +2 -0
@@ -311,6 +311,8 @@ u8 tpm_get_inc_u8(const u8 **ptr);u16 tpm_get_inc_u16 (const u8 **ptr);u32 tpm_get_inc_u32 (const u8 **ptr); +u8 *tpm_buf_parameters (struct tpm_buf *buf); +/* opaque structure, holds auth session parameters like the session key */ struct tpm2_auth ;
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 7/12 tpm2: add hmac checks to tpm2_pcr_extend()
In reply to James BottomleyWe use tpm2_pcr_extend() in trusted keys to extend a PCR to prevent a key from being re-loaded until the next reboot. To use this functionality securely, that extend must be protected by a session hmac.
---
v3: add error handling to sessions
--- drivers/char/tpm/tpm2-cmd.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-)
drivers/char/tpm/tpm2-cmd.c +11 -17
@@ -223,13 +223,6 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,return rc; } -struct tpm2_null_auth_area { -__be32 handle; -__be16 nonce_size; -u8 attributes; -__be16 auth_size; -} __packed; -/** * tpm2_pcr_extend() - extend a PCR value *
@@ -243,24 +236,23 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, struct tpm_digest *digests) {struct tpm_buf buf; -struct tpm2_null_auth_area auth_area; +struct tpm2_auth *auth;int rc;int i; - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_PCR_EXTEND ); + rc =tpm2_start_auth_session (chip, &auth);if (rc)return rc; -tpm_buf_append_u32 (&buf, pcr_idx); + rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_PCR_EXTEND ); +if (rc) { +tpm2_end_auth_session (auth); +return rc; + } - auth_area.handle =cpu_to_be32 (TPM2_RS_PW ); - auth_area.nonce_size =0 ; - auth_area.attributes =0 ; - auth_area.auth_size =0 ; +tpm_buf_append_name (&buf, auth, pcr_idx,NULL ); +tpm_buf_append_hmac_session (&buf, auth,0 ,NULL ,0 ); -tpm_buf_append_u32 (&buf,sizeof (struct tpm2_null_auth_area )); -tpm_buf_append (&buf, (const unsigned char *)&auth_area, -sizeof (auth_area));tpm_buf_append_u32 (&buf, chip->nr_allocated_banks);for (i =0 ; i < chip->nr_allocated_banks; i++) {
@@ -269,7 +261,9 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx, chip->allocated_banks[i].digest_size); } +tpm_buf_fill_hmac_session (&buf, auth); rc =tpm_transmit_cmd (chip, &buf,0 ,"attempting extend a PCR value" ); + rc =tpm_buf_check_hmac_response (&buf, auth, rc);tpm_buf_destroy (&buf);
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 8/12 tpm2: add session encryption protection to tpm2_get_random()
In reply to James BottomleyIf some entity is snooping the TPM bus, they can see the random numbers we're extracting from the TPM and do prediction attacks against their consumers. Foil this attack by using response encryption to prevent the attacker from seeing the random sequence.
---
v3: add error handling to sessions and redo to be outside loop
--- drivers/char/tpm/tpm2-cmd.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
drivers/char/tpm/tpm2-cmd.c +17 -4
@@ -296,29 +296,40 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)int total =0 ;int retries =5 ;u8 *dest_ptr = dest; +struct tpm2_auth *auth;if (!num_bytes || max >TPM_MAX_RNG_DATA )return -EINVAL ; - err =tpm_buf_init (&buf,0 ,0 ); + err =tpm2_start_auth_session (chip, &auth);if (err)return err; + err =tpm_buf_init (&buf,0 ,0 ); +if (err) { +tpm2_end_auth_session (auth); +return err; + } + do { - tpm_buf_reset(&buf, TPM2_ST_NO_SESSIONS, TPM2_CC_GET_RANDOM); + tpm_buf_reset(&buf, TPM2_ST_SESSIONS, TPM2_CC_GET_RANDOM); + tpm_buf_append_hmac_session_opt(&buf, auth, TPM2_SA_ENCRYPT + | TPM2_SA_CONTINUE_SESSION, + NULL, 0); tpm_buf_append_u16(&buf, num_bytes); + tpm_buf_fill_hmac_session(&buf, auth); err = tpm_transmit_cmd(chip, &buf, offsetof(struct tpm2_get_random_out, buffer), "attempting get random"); + err = tpm_buf_check_hmac_response(&buf, auth, err); if (err) { if (err > 0) err = -EIO; goto out; } - out = (struct tpm2_get_random_out *) - &buf.data[TPM_HEADER_SIZE]; + out = (struct tpm2_get_random_out *)tpm_buf_parameters(&buf); recd = min_t(u32, be16_to_cpu(out->size), num_bytes); if (tpm_buf_length(&buf) < TPM_HEADER_SIZE +
@@ -335,6 +346,8 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max) }while (retries-- && total < max);tpm_buf_destroy (&buf); +tpm2_end_auth_session (auth); +return total ? total : -EIO ; out:tpm_buf_destroy (&buf);
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 9/12 trusted keys: Add session encryption protection to the seal/unseal path
In reply to James BottomleyIf some entity is snooping the TPM bus, the can see the data going in to be sealed and the data coming out as it is unsealed. Add parameter and response encryption to these cases to ensure that no secrets are leaked even if the bus is snooped.
As part of doing this conversion it was discovered that policy sessions can't work with HMAC protected authority because of missing pieces (the tpm Nonce). I've added code to work the same way as before, which will result in potential authority exposure (while still adding security for the command and the returned blob), and a fixme to redo the API to get rid of this security hole.
--- drivers/char/tpm/tpm2-cmd.c | 124 ++++++++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 39 deletions(-)
drivers/char/tpm/tpm2-cmd.c +85 -39
@@ -422,7 +422,9 @@ int tpm2_seal_trusted(struct tpm_chip *chip, {unsigned int blob_len;struct tpm_buf buf; +struct tpm_buf t2b;u32 hash; +struct tpm2_auth *auth;int i;int rc;
@@ -436,45 +438,56 @@ int tpm2_seal_trusted(struct tpm_chip *chip,if (i ==ARRAY_SIZE (tpm2_hash_map))return -EINVAL ; - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_CREATE ); + rc =tpm2_start_auth_session (chip, &auth);if (rc)return rc; -tpm_buf_append_u32 (&buf, options->keyhandle); -tpm2_buf_append_auth (&buf,TPM2_RS_PW , -NULL /* nonce */ ,0 , -0 /* session_attributes */ , - options->keyauth/* hmac */ , -TPM_DIGEST_SIZE ); + rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_CREATE ); +if (rc) { +tpm2_end_auth_session (auth); +return rc; + } + + rc =tpm_buf_init_2b (&t2b); +if (rc) { +tpm_buf_destroy (&buf); +tpm2_end_auth_session (auth); +return rc; + } +tpm_buf_append_name (&buf, auth, options->keyhandle,NULL ); +tpm_buf_append_hmac_session (&buf, auth,TPM2_SA_DECRYPT , + options->keyauth,TPM_DIGEST_SIZE );/* sensitive */ -tpm_buf_append_u16 (&buf,4 +TPM_DIGEST_SIZE + payload->key_len +1 ); +tpm_buf_append_u16 (&t2b,TPM_DIGEST_SIZE ); +tpm_buf_append (&t2b, options->blobauth,TPM_DIGEST_SIZE ); +tpm_buf_append_u16 (&t2b, payload->key_len +1 ); +tpm_buf_append (&t2b, payload->key, payload->key_len); +tpm_buf_append_u8 (&t2b, payload->migratable); -tpm_buf_append_u16 (&buf,TPM_DIGEST_SIZE ); -tpm_buf_append (&buf, options->blobauth,TPM_DIGEST_SIZE ); -tpm_buf_append_u16 (&buf, payload->key_len +1 ); -tpm_buf_append (&buf, payload->key, payload->key_len); -tpm_buf_append_u8 (&buf, payload->migratable); +tpm_buf_append_2b (&buf, &t2b);/* public */ -tpm_buf_append_u16 (&buf,14 + options->policydigest_len); -tpm_buf_append_u16 (&buf,TPM_ALG_KEYEDHASH ); -tpm_buf_append_u16 (&buf, hash); +tpm_buf_append_u16 (&t2b,TPM_ALG_KEYEDHASH ); +tpm_buf_append_u16 (&t2b, hash);/* policy */ if (options->policydigest_len) { -tpm_buf_append_u32 (&buf,0 ); -tpm_buf_append_u16 (&buf, options->policydigest_len); -tpm_buf_append (&buf, options->policydigest, +tpm_buf_append_u32 (&t2b,0 ); +tpm_buf_append_u16 (&t2b, options->policydigest_len); +tpm_buf_append (&t2b, options->policydigest, options->policydigest_len); }else { -tpm_buf_append_u32 (&buf,TPM2_OA_USER_WITH_AUTH ); -tpm_buf_append_u16 (&buf,0 ); +tpm_buf_append_u32 (&t2b,TPM2_OA_USER_WITH_AUTH ); +tpm_buf_append_u16 (&t2b,0 ); }/* public parameters */ -tpm_buf_append_u16 (&buf,TPM_ALG_NULL ); -tpm_buf_append_u16 (&buf,0 ); +tpm_buf_append_u16 (&t2b,TPM_ALG_NULL ); +/* unique (zero) */ +tpm_buf_append_u16 (&t2b,0 ); + +tpm_buf_append_2b (&buf, &t2b);/* outside info */ tpm_buf_append_u16 (&buf,0 );
@@ -487,7 +500,9 @@ int tpm2_seal_trusted(struct tpm_chip *chip, goto out; } +tpm_buf_fill_hmac_session (&buf, auth); rc =tpm_transmit_cmd (chip, &buf,4 ,"sealing data" ); + rc =tpm_buf_check_hmac_response (&buf, auth, rc);if (rc) goto out;
@@ -505,6 +520,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip, payload->blob_len = blob_len; out: +tpm_buf_destroy (&t2b);tpm_buf_destroy (&buf);if (rc >0 ) {
@@ -536,6 +552,7 @@ static int tpm2_load_cmd(struct tpm_chip *chip, u32 *blob_handle) {struct tpm_buf buf; +struct tpm2_auth *auth;unsigned int private_len;unsigned int public_len;unsigned int blob_len;
@@ -550,17 +567,18 @@ static int tpm2_load_cmd(struct tpm_chip *chip,if (blob_len > payload->blob_len)return -E2BIG ; - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_LOAD ); + rc =tpm2_start_auth_session (chip, &auth);if (rc)return rc; -tpm_buf_append_u32 (&buf, options->keyhandle); -tpm2_buf_append_auth (&buf,TPM2_RS_PW , -NULL /* nonce */ ,0 , -0 /* session_attributes */ , - options->keyauth/* hmac */ , -TPM_DIGEST_SIZE ); + rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_LOAD ); +if (rc) { +tpm2_end_auth_session (auth); +return rc; + } +tpm_buf_append_name (&buf, auth, options->keyhandle,NULL ); +tpm_buf_append_hmac_session (&buf, auth,0 , options->keyauth,TPM_DIGEST_SIZE );tpm_buf_append (&buf, payload->blob, blob_len);if (buf.flags &TPM_BUF_OVERFLOW ) {
@@ -568,7 +586,9 @@ static int tpm2_load_cmd(struct tpm_chip *chip, goto out; } +tpm_buf_fill_hmac_session (&buf, auth); rc =tpm_transmit_cmd (chip, &buf,4 ,"loading blob" ); + rc =tpm_buf_check_hmac_response (&buf, auth, rc);if (!rc) *blob_handle =be32_to_cpup ( (__be32 *) &buf.data[TPM_HEADER_SIZE ]);
@@ -600,24 +620,50 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip, u32 blob_handle) {struct tpm_buf buf; +struct tpm2_auth *auth;u16 data_len;u8 *data;int rc; - rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_UNSEAL ); + rc =tpm2_start_auth_session (chip, &auth);if (rc)return rc; -tpm_buf_append_u32 (&buf, blob_handle); -tpm2_buf_append_auth (&buf, - options->policyhandle ? - options->policyhandle :TPM2_RS_PW , -NULL /* nonce */ ,0 , -TPM2_SA_CONTINUE_SESSION , - options->blobauth/* hmac */ , -TPM_DIGEST_SIZE ); + rc =tpm_buf_init (&buf,TPM2_ST_SESSIONS ,TPM2_CC_UNSEAL ); +if (rc) { +tpm2_end_auth_session (auth); +return rc; + } + +tpm_buf_append_name (&buf, auth, blob_handle,NULL ); + +if (!options->policyhandle) { +tpm_buf_append_hmac_session (&buf, auth,TPM2_SA_ENCRYPT , + options->blobauth,TPM_DIGEST_SIZE ); + }else { +/* +* FIXME: the policy can't be used for HMAC protection +* of the authorization because it must be generated +* with the initial nonces which isn't passed in, so +* append a second encryption session to at least HMAC +* protect the command and encrypt the sealed blob on +* return. +* +* Note: this also means the caller either has to use +* an empty password or compute the password HMAC +* based on the policy nonces and place it in blobauth +*/ +tpm2_buf_append_auth (&buf, options->policyhandle, +NULL /* nonce */ ,0 ,0 , + options->blobauth/* hmac */ , +TPM_DIGEST_SIZE ); +tpm_buf_append_hmac_session_opt (&buf, auth,TPM2_SA_ENCRYPT , +NULL ,0 ); + } +tpm_buf_fill_hmac_session (&buf, auth); rc =tpm_transmit_cmd (chip, &buf,6 ,"unsealing" ); + rc =tpm_buf_check_hmac_response (&buf, auth, rc);if (rc >0 ) rc = -EPERM ;
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 10/12 tpm: add the null key name as a tpm2 sysfs variable
In reply to James BottomleyThis is the last component of encrypted tpm2 session handling that allows us to verify from userspace that the key derived from the NULL seed genuinely belongs to the TPM and has not been spoofed.
The procedure for doing this involves creating an attestation identity key (which requires verification of the TPM EK certificate) and then using that AIK to sign a certification of the Elliptic Curve key over the NULL seed. Userspace must create this EC Key using the parameters prescribed in TCG TPM v2.0 Provisioning Guidance for the SRK ECC; if this is done correctly the names will match and the TPM can then run a TPM2_Certify operation on this derived primary key using the newly created AIK.
--- drivers/char/tpm/tpm-sysfs.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
drivers/char/tpm/tpm-sysfs.c +26 -1
@@ -309,6 +309,19 @@ static ssize_t timeouts_show(struct device *dev, struct device_attribute *attr, }static DEVICE_ATTR_RO (timeouts); +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 ; + +bin2hex (buf, chip->tpmkeyname, size); + size *=2 ; + buf[size++] ='\n' ; +return size; +} +static DEVICE_ATTR_RO (null_name); +static struct attribute *tpm_dev_attrs[] = { &dev_attr_pubek.attr, &dev_attr_pcrs.attr,
@@ -323,17 +336,29 @@ static struct attribute *tpm_dev_attrs[] = { NULL, }; +static struct attribute *tpm2_dev_attrs[] = { + &dev_attr_null_name.attr, +NULL , +}; +static const struct attribute_group tpm_dev_group = { .attrs = tpm_dev_attrs, }; +static const struct attribute_group tpm2_dev_group = { + .attrs = tpm2_dev_attrs, +}; +void tpm_sysfs_add_device (struct tpm_chip *chip) {/* XXX: If you wish to remove this restriction, you must first update * tpm_sysfs to explicitly lock chip->ops. */ -if (chip->flags &TPM_CHIP_FLAG_TPM2 ) +if (chip->flags &TPM_CHIP_FLAG_TPM2 ) { +WARN_ON (chip->groups_cnt !=0 ); + chip->groups[chip->groups_cnt++] = &tpm2_dev_group;return ; + }/* The sysfs routines rely on an implicit tpm_try_get_ops, device_del * is called before ops is null'd and the sysfs core synchronizes this
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 11/12 Documentation: add tpm-security.rst
In reply to James BottomleyDocument how the new encrypted secure interface for TPM2 works and how security can be assured after boot by certifying the NULL seed.
---
v6: replace kernel space with null seed context save
--- Documentation/security/tpm/tpm-security.rst | 204 ++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 Documentation/security/tpm/tpm-security.rst
Documentation/security/tpm/tpm-security.rst new file +204 -0
@@ -0,0 +1,204 @@ +TPM Security +============ + +The object of this document is to describe how we make the kernel's +use of the TPM reasonably robust in the face of external snooping and +packet alteration attacks. The current security document is for TPM +2.0. + +Introduction +------------ + +The TPM is usually a discrete chip attached to a PC via some type of +low bandwidth bus. There are exceptions to this such as the Intel +PTT, which is a software TPM running inside a software environment +close to the CPU, which are subject to different attacks, but right at +the moment, most hardened security environments require a discrete +hardware TPM, which is the use case discussed here. + +Snooping and Alteration Attacks against the bus +----------------------------------------------- + +The current state of the art for snooping the TPM Genie hardware +interposer https://www.nccgroup.trust/us/our-research/tpm-genie/ which +is a simple external device that can be installed in a couple of +seconds on any system or laptop. However, the next phase of research +seems to be hacking existing devices on the bus to act as interposers, +so the fact that the attacker requires physical access for a few +seconds might evaporate. However, the goal of this document is to +protect TPM secrets and integrity as far as we are able in this +environment and to try to insure that if we can't prevent the attack +then at least we can detect it. + +Unfortunately, most of the TPM functionality, including the hardware +reset capability can be controlled by an attacker who has access to +the bus, so we'll discuss some of the disruption possibilities below. + +Measurement (PCR) Integrity +--------------------------- + +Since the attacker can send their own commands to the TPM, they can +send arbitrary PCR extends and thus disrupt the measurement system, +which would be an annoying denial of service attack. However, there +are two, more serious, classes of attack aimed at entities sealed to +trust measurements. + +1. The attacker could intercept all PCR extends coming from the system + and completely substitute their own values, producing a replay of + an untampered state that would cause PCR measurements to attest to + a trusted state and release secrets + +2. At some point in time the attacker could reset the TPM, clearing + the PCRs and then send down their own measurements which would + effectively overwrite the boot time measurements the TPM has + already done. + +The first can be thwarted by always doing HMAC protection of the PCR +extend and read command meaning measurement values cannot be +substituted without producing a detectable HMAC failure in the +response. However, the second can only really be detected by relying +on some sort of mechanism for protection which would change over TPM +reset. + +Secrets Guarding +---------------- + +Certain information passing in and out of the TPM, such as key sealing +and private key import and random number generation, is vulnerable to +interception which HMAC protection alone cannot protect, so for these +types of command we must also employ request and response encryption +to prevent the loss of secret information. + +Establishing Initial Trust with the TPM +--------------------------------------- + +In order to provide security from the beginning, an initial shared or +asymmetric secret must be established which must also be unknown to +the attacker. The most obvious avenues for this are the endorsement +and storage seeds, which can be used to derive asymmetric keys. +However, using these keys is difficult because the only way to pass +them into the kernel would be on the command line, which requires +extensive support in the boot system, and there's no guarantee that +either hierarchy would not have some type of authorization. + +The mechanism chosen for the Linux Kernel is to derive the primary +elliptic curve key from the null seed using the standard storage seed +parameters. The null seed has two advantages: firstly the hierarchy +physically cannot have an authorization, so we are always able to use +it and secondly, the null seed changes across TPM resets, meaning if +we establish trust on the null seed at start of day, all sessions +salted with the derived key will fail if the TPM is reset and the seed +changes. + +Obviously using the null seed without any other prior shared secrets, +we have to create and read the initial public key which could, of +course, be intercepted and substituted by the bus interposer. +However, the TPM has a key certification mechanism (using the EK +endorsement certificate, creating an attestation identity key and +certifying the null seed primary with that key) which is too complex +to run within the kernel, so we keep a copy of the null primary key +name, which is what is certified so user-space can run the full +certification when it boots. The definitive guarantee here is that if +the null primary key certifies correctly, you know all your TPM +transactions since start of day were secure and if it doesn't, you +know there's an interposer on your system (and that any secret used +during boot may have been leaked). + +Stacking Trust +-------------- + +In the current null primary scenario, the TPM must be completely +cleared before handing it on to the next consumer. However the kernel +hands to user-space the name of the derived null seed key which can +then be verified by certification in user-space. Therefore, this chain +of name handoff can be used between the various boot components as +well (via an unspecified mechanism). For instance, grub could use the +null seed scheme for security and hand the name off to the kernel in +the boot area. The kernel could make its own derivation of the key +and the name and know definitively that if they differ from the handed +off version that tampering has occurred. Thus it becomes possible to +chain arbitrary boot components together (UEFI to grub to kernel) via +the name handoff provided each successive component knows how to +collect the name and verifies it against its derived key. + +Session Properties +------------------ + +All TPM commands the kernel uses allow sessions. HMAC sessions may be +used to check the integrity of requests and responses and decrypt and +encrypt flags may be used to shield parameters and responses. The +HMAC and encryption keys are usually derived from the shared +authorization secret, but for a lot of kernel operations that is well +known (and usually empty). Thus, every HMAC session used by the +kernel must be created using the null primary key as the salt key +which thus provides a cryptographic input into the session key +derivation. Thus, the kernel creates the null primary key once (as a +volatile TPM handle) and keeps it around in a saved context stored in +tpm_chip for every in-kernel use of the TPM. Currently, because of a +lack of de-gapping in the in-kernel resource manager, the session must +be created and destroyed for each operation, but, in future, a single +session may also be reused for the in-kernel HMAC, encryption and +decryption sessions. + +Protection Types +---------------- + +For every in-kernel operation we use null primary salted HMAC to +protect the integrity. Additionally, we use parameter encryption to +protect key sealing and parameter decryption to protect key unsealing +and random number generation. + +Null Primary Key Certification in Userspace +=========================================== + +Every TPM comes shipped with a couple of X.509 certificates for the +primary endorsement key. This document assumes that the Elliptic +Curve version of the certificate exists at 01C00002, but will work +equally well with the RSA certificate (at 01C00001). + +The first step in the certification is primary creation using the +template from the `TCG EK Credential Profile`_ which allows comparison +of the generated primary key against the one in the certificate (the +public key must match). Note that generation of the EK primary +requires the EK hierarchy password, but a pre-generated version of the +EC primary should exist at 81010002 and a TPM2_ReadPublic() may be +performed on this without needing the key authority. Next, the +certificate itself must be verified to chain back to the manufacturer +root (which should be published on the manufacturer website). Once +this is done, the generated EK primary key may now be used to run an +attestation on the null seed. The specific problem here is that the +EK primary is not a signing key so cannot on its own be used to sign +the key certification, hence the complex process below. + +Note: this process is a simplified abbreviation of the usual privacy +CA based attestation process. The assumption here is that the +attestation is done by the TPM owner who thus has access to only the +owner hierarchy. The owner creates an external public/private key +pair (assume elliptic curve in this case) and wraps the private key +for import using an inner wrapping process and parented to the EC +derived storage primary. The TPM2_Import() is done using a parameter +decryption HMAC session salted to the EK primary (which also does not +require the EK key authority) meaning that the inner wrapping key is +the encrypted parameter and thus the TPM will not be able to perform +the import unless is possesses the certified EK so if the command +succeeds and the HMAC verifies on return we know we have a loadable +copy of the private key only for the certified TPM. This key is now +loaded into the TPM and the Storage primary flushed (to free up space +for the null key generation). + +The null EC primary is now generated using the Storage profile +outlined in the `TCG TPM v2.0 Provisioning Guidance`_; the name of +this key (the hash of the public area) is computed and compared to the +null seed name presented by the kernel in +/sys/class/tpm/tpm0/null_name. If the names do not match, the TPM is +compromised. If the names match, the user performs a TPM2_Certify() +using the null primary as the object handle and the loaded private key +as the sign handle and providing randomized qualifying data. The +signature of the returned certifyInfo is verified against the public +part of the loaded private key and the qualifying data checked to +prevent replay. If all of these tests pass, the user is now assured +that TPM integrity and privacy was preserved across the entire boot +sequence of this kernel. + +.. _TCG EK Credential Profile: https://trustedcomputinggroup.org/resource/tcg-ek-credential-profile-for-tpm-family-2-0/ +.. _TCG TPM v2.0 Provisioning Guidance: https://trustedcomputinggroup.org/resource/tcg-tpm-v2-0-provisioning-guidance/
Signature · 1 line
2.16.4
James Bottomley
maintainerPATCH v6 12/12 tpm2-sessions: NOT FOR COMMITTING add sessions testing
In reply to James BottomleyThis runs through a preset sequence using sessions to demonstrate that the session handling code functions. It does both HMAC, encryption and decryption by testing an encrypted sealing operation with authority and proving that the same sealed data comes back again via an HMAC and response encryption. It also does policy unsealing which mimics the more complex of the trusted key scenarios.
---
v3: add policy unseal testing with two sessions
v6: move to new null seed framework
--- drivers/char/tpm/Makefile | 2 + drivers/char/tpm/tpm-chip.c | 1 + drivers/char/tpm/tpm.h | 1 + drivers/char/tpm/tpm2-cmd.c | 7 + drivers/char/tpm/tpm2-sessions-test.c | 795 ++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm2-sessions.c | 1 + drivers/char/tpm/tpm2-sessions.h | 3 +- 7 files changed, 809 insertions(+), 1 deletion(-) create mode 100644 drivers/char/tpm/tpm2-sessions-test.c
drivers/char/tpm/Makefile +2 -0
@@ -35,3 +35,5 @@ obj-$(CONFIG_TCG_TIS_ST33ZP24) += st33zp24/ obj-$(CONFIG_TCG_XEN) += xen-tpmfront.o obj-$(CONFIG_TCG_CRB) += tpm_crb.o obj-$(CONFIG_TCG_VTPM_PROXY) += tpm_vtpm_proxy.o + +obj-m += tpm2-sessions-test.o
drivers/char/tpm/tpm-chip.c +1 -0
@@ -252,6 +252,7 @@ struct tpm_chip *tpm_find_get_ops(struct tpm_chip *chip)return NULL ;return chip; } +EXPORT_SYMBOL (tpm_find_get_ops);/** * tpm_dev_release() - free chip memory and the device number
drivers/char/tpm/tpm.h +1 -0
@@ -117,6 +117,7 @@ enum tpm2_command_codes {TPM2_CC_CONTEXT_LOAD =0x0161 ,TPM2_CC_CONTEXT_SAVE =0x0162 ,TPM2_CC_FLUSH_CONTEXT =0x0165 , +TPM2_CC_POLICY_COMMAND_CODE =0x16c ,TPM2_CC_READ_PUBLIC =0x0173 ,TPM2_CC_START_AUTH_SESS =0x0176 , TPM2_CC_VERIFY_SIGNATURE = 0x0177,
drivers/char/tpm/tpm2-cmd.c +7 -0
@@ -222,6 +222,7 @@ int tpm2_pcr_read(struct tpm_chip *chip, u32 pcr_idx,tpm_buf_destroy (&buf);return rc; } +EXPORT_SYMBOL_GPL (tpm2_pcr_read);/** * tpm2_pcr_extend() - extend a PCR value
@@ -269,6 +270,7 @@ int tpm2_pcr_extend(struct tpm_chip *chip, u32 pcr_idx,return rc; } +EXPORT_SYMBOL_GPL (tpm2_pcr_extend);struct tpm2_get_random_out {__be16 size;
@@ -353,6 +355,7 @@ int tpm2_get_random(struct tpm_chip *chip, u8 *dest, size_t max)tpm_buf_destroy (&buf);return err; } +EXPORT_SYMBOL_GPL (tpm2_get_random);/** * tpm2_flush_context() - execute a TPM2_FlushContext command
@@ -376,6 +379,7 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle)tpm_transmit_cmd (chip, &buf,0 ,"flushing context" );tpm_buf_destroy (&buf); } +EXPORT_SYMBOL_GPL (tpm2_flush_context);/** * tpm_buf_append_auth() - append TPMS_AUTH_COMMAND to the buffer.
@@ -406,6 +410,7 @@ void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,if (hmac && hmac_len)tpm_buf_append (buf, hmac, hmac_len); } +EXPORT_SYMBOL_GPL (tpm2_buf_append_auth);/** * tpm2_seal_trusted() - seal the payload of a trusted key
@@ -532,6 +537,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,return rc; } +EXPORT_SYMBOL_GPL (tpm2_seal_trusted);/** * tpm2_load_cmd() - execute a TPM2_Load command
@@ -715,6 +721,7 @@ int tpm2_unseal_trusted(struct tpm_chip *chip,tpm2_flush_context (chip, blob_handle);return rc; } +EXPORT_SYMBOL_GPL (tpm2_unseal_trusted);struct tpm2_get_cap_out {u8 more_data;
drivers/char/tpm/tpm2-sessions-test.c new file +795 -0
795 lines not on this page · raw message
drivers/char/tpm/tpm2-sessions.c +1 -0
@@ -865,6 +865,7 @@ static int parse_start_auth_session(struct tpm2_auth *auth, const u8 *data)* tpm2_start_auth_session - create a HMAC authentication session with the TPM * @chip: the TPM chip structure to create the session with * @authp: A pointer to an opaque tpm2_auth structure to be allocated +* @key: A pointer to return the NULL key or NULL if key should be flushed * * This function loads the NULL seed from its saved context and starts * an authentication session on the null seed, allocates a tpm2_auth
drivers/char/tpm/tpm2-sessions.h +2 -1
@@ -45,7 +45,8 @@ void tpm2_buf_append_auth(struct tpm_buf *buf, u32 session_handle,#ifdef CONFIG_TPM_BUS_SECURITY -int tpm2_start_auth_session (struct tpm_chip *chip,struct tpm2_auth **authp); +int tpm2_start_auth_session (struct tpm_chip *chip, +struct tpm2_auth **authp);void tpm_buf_append_name (struct tpm_buf *buf,struct tpm2_auth *auth,u32 handle,u8 *name); void tpm_buf_append_hmac_session(struct tpm_buf *buf, struct tpm2_auth *auth,
Signature · 1 line
2.16.4
Quoted 11 lines · James Bottomley
Link to previous cover letter:
https://lore.kernel.org/linux-integrity/[email protected]/
This is marked v6 instead of v5 because I did a v5 after feedback on v4 but didn't get around to posting it and then had to rework the whole of the kernel space handling while I was on holiday. I also added the documentation of how the whole thing works and the rationale for doing it in
tpm-security.rst(patch 11). The main reason for doing this now is so we have something to discuss at Plumbers.
Quoted 8 lines
The new patch set implements the various splits requested, but the main changes are that the kernel space is gone and is replaced by a context save and restore of the generated null seed. This is easier to handle than a full kernel space given the new threading for TPM spaces, but conceptually it is still very like a space. I've also made whether integrity and encryption is turned on a
Kconfigoption.James
So... is there a changelog for the revisions?
/Jarkko
James Bottomley
maintainerQuoted 28 lines · Jarkko Sakkinen
Quoted 27 lines · James Bottomley
Link to previous cover letter:
https://lore.kernel.org/linux-integrity/1540193596.3202.7.camel@Han
senPartnership.com/This is marked v6 instead of v5 because I did a v5 after feedback on v4
but didn't get around to posting it and then had to rework the whole of
the kernel space handling while I was on holiday. I also added the documentation of how the whole thing works and the rationale for doing
it intpm-security.rst(patch 11). The main reason for doing this now
is so we have something to discuss at Plumbers.The new patch set implements the various splits requested, but the main
changes are that the kernel space is gone and is replaced by a context
save and restore of the generated null seed. This is easier to handle
than a full kernel space given the new threading for TPM spaces, but
conceptually it is still very like a space. I've also made whether integrity and encryption is turned on aKconfigoption.
Quoted 3 lines
Quoted 1 line
James
So... is there a changelog for the revisions?
Well, yes, standard way: they're in the individual patches under the '- --' prefixed with v6:
James
Quoted 19 lines · Jarkko Sakkinen
Quoted 18 lines · James Bottomley
Link to previous cover letter:
https://lore.kernel.org/linux-integrity/[email protected]/
This is marked v6 instead of v5 because I did a v5 after feedback on v4 but didn't get around to posting it and then had to rework the whole of the kernel space handling while I was on holiday. I also added the documentation of how the whole thing works and the rationale for doing it in
tpm-security.rst(patch 11). The main reason for doing this now is so we have something to discuss at Plumbers.The new patch set implements the various splits requested, but the main changes are that the kernel space is gone and is replaced by a context save and restore of the generated null seed. This is easier to handle than a full kernel space given the new threading for TPM spaces, but conceptually it is still very like a space. I've also made whether integrity and encryption is turned on a
Kconfigoption.
Quoted 3 lines
Quoted 1 line
James
So... is there a changelog for the revisions?
This also desperately needs a cover letter with the full rationale and not just a link to an aged cover letter. I have bigger problems with the form than the function ATM.
TPM's threat model does not cover hardware attacks. It is hardware designed to give some protection against software attacks. If I were sending these patches I would start to look for an angle from that perspective.
/Jarkko
Quoted 24 lines · Jarkko Sakkinen
Quoted 19 lines · Jarkko Sakkinen
Quoted 18 lines · James Bottomley
Link to previous cover letter:
https://lore.kernel.org/linux-integrity/[email protected]/
This is marked v6 instead of v5 because I did a v5 after feedback on v4 but didn't get around to posting it and then had to rework the whole of the kernel space handling while I was on holiday. I also added the documentation of how the whole thing works and the rationale for doing it in
tpm-security.rst(patch 11). The main reason for doing this now is so we have something to discuss at Plumbers.The new patch set implements the various splits requested, but the main changes are that the kernel space is gone and is replaced by a context save and restore of the generated null seed. This is easier to handle than a full kernel space given the new threading for TPM spaces, but conceptually it is still very like a space. I've also made whether integrity and encryption is turned on a
Kconfigoption.Quoted 3 lines
Quoted 1 line
James
So... is there a changelog for the revisions?
Quoted 8 lines
This also desperately needs a cover letter with the full rationale and not just a link to an aged cover letter. I have bigger problems with the form than the function ATM.
TPM's threat model does not cover hardware attacks. It is hardware designed to give some protection against software attacks. If I were sending these patches I would start to look for an angle from that perspective.
The rationale can be essentially just that since there is often lots of *software* running outside the CPU on different cores all around the HW platform, this will add to defense in depth. I'm not looking for anything more rockety sciency than that.
I think that was the key lesson from TPM Genie.
/Jarkko
Re: [PATCH v6 01/12] tpm-buf: move from static inlines to real functions
In reply to James BottomleyQuoted 5 lines · James Bottomley
This separates out the old
tpm_buf_... handling functions from static inlines intpm.hand makes them their owntpm-buf.cfile. This is a precursor so we can add new functions for other TPM type handlingSigned-off-by James Bottomley <[email protected]>
What about TPM_BUF_2B that gets added in this commit?
/Jarkko
Re: [PATCH v6 01/12] tpm-buf: move from static inlines to real functions
Quoted 8 lines · Jarkko Sakkinen
Quoted 5 lines · James Bottomley
This separates out the old
tpm_buf_... handling functions from static inlines intpm.hand makes them their owntpm-buf.cfile. This is a precursor so we can add new functions for other TPM type handlingSigned-off-by James Bottomley <[email protected]>What about
TPM_BUF_2Bthat gets added in this commit?
Probably just a glitch in rebasing/squashing?
/Jarkko
Re: [PATCH v6 02/12] tpm-buf: add handling for TPM2B types
In reply to James BottomleyQuoted 7 lines · James Bottomley
Most complex TPM commands require appending TPM2B buffers to the command body. Since TPM2B types are essentially variable size arrays, it makes it impossible to represent these complex command arguments as structures and we simply have to build them up using append primitives like these.
Signed-off-by James Bottomley <[email protected]>
I think a better idea would be to have headerless TPM buffers and also it makes sense to have a separate length field in the struct to keep the code sane given that sometimes the buffer does not store the length.
E.g.
enum tpm_buf_flags {
TPM_BUF_OVERFLOW = BIT(0),
TPM_BUF_HEADERLESS = BIT(1),
};struct tpm_buf {
unsigned int length;
struct page *data_page;
unsigned int flags;
u8 *data;
};/Jarkko
Re: [PATCH v6 05/12] tpm2-sessions: Add full HMAC and encrypt/decrypt session handling
In reply to James BottomleyQuoted 246 lines · James Bottomley
This code adds true session based HMAC authentication plus parameter decryption and response encryption using AES.
The basic design of this code is to segregate all the nasty crypto, hash and hmac code into
tpm2-sessions.cand export a usable API.The API first of all starts off by gaining a session with
tpm2_start_auth_session()Which initiates a session with the TPM and allocates an opaque
tpm2_authstructure to handle the session parameters. Then the use is simply:*
tpm_buf_append_name()in place of thetpm_buf_append_u32for the handles*
tpm_buf_append_hmac_session()wheretpm2_append_auth()would go*
tpm_buf_fill_hmac_session()called after the entire command buffer is finished but beforetpm_transmit_cmd()is called which computes the correct HMAC and places it in the command at the correct location.Finally, after
tpm_transmit_cmd()is called,tpm_buf_check_hmac_response()is called to check that the returned HMAC matched and collect the new state for the next use of the session, if any.The features of the session is controlled by the session attributes set in
tpm_buf_append_hmac_session(). IfTPM2_SA_CONTINUE_SESSIONis not specified, the session will be flushed and thetpm2_authstructure freed intpm_buf_check_hmac_response(); otherwise the session may be used again. Parameter encryption is specified by or'ing the flagTPM2_SA_DECRYPTand response encryption by or'ing the flagTPM2_SA_ENCRYPT. the various encryptions will be taken care of bytpm_buf_fill_hmac_session()andtpm_buf_check_hmac_response()respectively.To get all of this to work securely, the Kernel now needs a primary key to encrypt the session salt to, so we derive an EC key from the NULL seed and save its context in the
tpm_chipstructure. The context is loaded on demand into an available volatile handle whentpm_start_auth_session()is called, but is flushed before that function exits to conserve handles.Signed-off-by James Bottomley <[email protected]>---
v2: Added docbook and improved response check API
v3: Add readpublic, fix hmac length, add API for close on error
allow for the hmac session not being first in the sessions
v4: Make NULL seed template exactly match the SRK ECC template.
Also check the NULL primary key name is what getpublic returns to prevent spoofing. Also parametrise the name size for reuse
v5: Move tosync_skcipherAPI
v6: eliminate kernel space and use context save for null seed and
make feature conditional onCONFIG_TPM_BUS_SECURITY--- drivers/char/tpm/Kconfig | 11 + drivers/char/tpm/Makefile | 1 + drivers/char/tpm/tpm-buf.c | 1 + drivers/char/tpm/tpm.h | 20 +- drivers/char/tpm/tpm2-cmd.c | 22 +- drivers/char/tpm/tpm2-sessions.c | 1203 ++++++++++++++++++++++++++++++++++++++ drivers/char/tpm/tpm2-sessions.h | 137 +++++ include/linux/tpm.h | 29 + 8 files changed, 1411 insertions(+), 13 deletions(-) create mode 100644 drivers/char/tpm/tpm2-sessions.c create mode 100644 drivers/char/tpm/tpm2-sessions.hdrivers/char/tpm/Kconfig +11
-0@@ -9,6 +9,9 @@ menuconfig TCG_TPM imply SECURITYFS select CRYPTO select CRYPTO_HASH_INFO + select CRYPTO_ECDH + select CRYPTO_AES + select CRYPTO_CFB ---help--- If you have a TPM security chip in your system, which implements the Trusted Computing Group's specification,@@ -27,6 +30,14 @@ menuconfig TCG_TPM if TCG_TPM +config TPM_BUS_SECURITY + bool "Use secure transactions on the TPM bus" + default y + ---help--- + Setting this causes us to deploy a tamper resistent scheme + for communicating with the TPM to prevent or detect bus snooping + attacks like TPM Genie. Saying Y here adds some encryption overhead + to all kernel to TPM transactions. config HW_RANDOM_TPM bool "TPM HW Random Number Generator support" depends on TCG_TPM && HW_RANDOM && !(TCG_TPM=y && HW_RANDOM=m)drivers/char/tpm/Makefile +1
-0@@ -17,6 +17,7 @@ tpm-y += eventlog/tpm1.o tpm-y += eventlog/tpm2.o tpm-y += tpm-buf.o +tpm-$(CONFIG_TPM_BUS_SECURITY) += tpm2-sessions.o tpm-$(CONFIG_ACPI) += tpm_ppi.o eventlog/acpi.o tpm-$(CONFIG_EFI) += eventlog/efi.o tpm-$(CONFIG_OF) += eventlog/of.odrivers/char/tpm/tpm-buf.c +1
-0@@ -31,6 +31,7 @@ void tpm_buf_reset(struct tpm_buf *buf, u16 tag, u32 ordinal) head->tag =cpu_to_be16 (tag); head->length =cpu_to_be32 (sizeof (*head)); head->ordinal =cpu_to_be32 (ordinal); + buf->handles =0 ; }EXPORT_SYMBOL_GPL (tpm_buf_reset);drivers/char/tpm/tpm.h +19
-1@@ -80,6 +80,7 @@ enum tpm2_timeouts {enum tpm2_structures {TPM2_ST_NO_SESSIONS =0x8001 ,TPM2_ST_SESSIONS =0x8002 , +TPM2_ST_CREATION =0x8021 , };/* Indicates from what layer of the software stack the error comes from */ @@ -116,6 +117,8 @@ enum tpm2_command_codes {TPM2_CC_CONTEXT_LOAD =0x0161 ,TPM2_CC_CONTEXT_SAVE =0x0162 ,TPM2_CC_FLUSH_CONTEXT =0x0165 , +TPM2_CC_READ_PUBLIC =0x0173 , +TPM2_CC_START_AUTH_SESS =0x0176 ,TPM2_CC_VERIFY_SIGNATURE =0x0177 ,TPM2_CC_GET_CAPABILITY =0x017A , TPM2_CC_GET_RANDOM = 0x017B,@@ -128,6 +131,7 @@ enum tpm2_command_codes { };enum tpm2_permanent_handles { +TPM2_RH_NULL =0x40000007 ,TPM2_RS_PW =0x40000009 , };@@ -286,7 +290,8 @@ enum tpm_buf_flags {struct tpm_buf {struct page *data_page; -unsigned int flags; +u8 flags; +u8 handles;u8 *data; };@@ -306,6 +311,9 @@ u8 tpm_get_inc_u8(const u8 **ptr);u16 tpm_get_inc_u16 (const u8 **ptr);u32 tpm_get_inc_u32 (const u8 **ptr); +/* opaque structure, holds auth session parameters like the session key */ +struct tpm2_auth ; +extern struct class *tpm_class;extern struct class *tpmrm_class;extern dev_t tpm_devt;@@ -408,4 +416,14 @@ int tpm_bios_log_setup(struct tpm_chip *chip);void tpm_bios_log_teardown (struct tpm_chip *chip);int tpm_dev_common_init (void );void tpm_dev_common_exit (void ); + +#ifdef CONFIG_TPM_BUS_SECURITY +int tpm2_sessions_init (struct tpm_chip *chip); +#else +static inline int tpm2_sessions_init (struct tpm_chip *chip) +{ +return 0 ; +} +#endif +#endif drivers/char/tpm/tpm2-cmd.c +10
-12@@ -12,17 +12,10 @@*/ #include "tpm.h" +#include "tpm2-sessions.h" #include <crypto/hash_info.h> #include <keys/trusted-type.h> -enum tpm2_object_attributes { -TPM2_OA_USER_WITH_AUTH =BIT (6 ), -}; - -enum tpm2_session_attributes { -TPM2_SA_CONTINUE_SESSION =BIT (0 ), -}; -struct tpm2_hash {unsigned int crypto_id;unsigned int tpm_id;@@ -388,10 +381,10 @@ void tpm2_flush_context(struct tpm_chip *chip, u32 handle)* @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) +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);@@ -1042,6 +1035,11 @@ int tpm2_auto_startup(struct tpm_chip *chip) rc =tpm2_get_cc_attrs_tbl (chip); +if (rc) + goto out; + + rc =tpm2_sessions_init (chip); + out:if (rc >0 ) rc = -ENODEV ;drivers/char/tpm/tpm2-sessions.c new file +0
-0
drivers/char/tpm/tpm2-sessions.c line 4
Quoted 4 lines
drivers/char/tpm/tpm2-sessions.c new file +4
-0@@ -0,0 +1,1203 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright (C) 2018 [email protected]
*/
/**
* DOC: TPM2 Sessions
Quoted 54 lines
drivers/char/tpm/tpm2-sessions.c new file +54
-0+* +* Cryptographic helper routines for handling TPM2 sessions for +* authorization HMAC and request response encryption. +* +* The idea is to ensure that every TPM command is HMAC protected by a +* session, meaning in-flight tampering would be detected and in +* addition all sensitive inputs and responses should be encrypted. +* +* The basic way this works is to use a TPM feature called salted +* sessions where a random secret used in session construction is +* encrypted to the public part of a known TPM key. The problem is we +* have no known keys, so initially a primary Elliptic Curve key is +* derived from the NULL seed (we use EC because most TPMs generate +* these keys much faster than RSA ones). The curve used is NIST_P256 +* because that's now mandated to be present in 'TCG TPM v2.0 +* Provisioning Guidance' +* +* Threat problems: the initial TPM2_CreatePrimary is not (and cannot +* be) session protected, so a clever Man in the Middle could return a +* public key they control to this command and from there intercept +* and decode all subsequent session based transactions. The kernel +* cannot mitigate this threat but, after boot, userspace can get +* proof this has not happened by asking the TPM to certify the NULL +* key. This certification would chain back to the TPM Endorsement +* Certificate and prove the NULL seed primary had not been tampered +* with and thus all sessions must have been cryptographically secure. +* To assist with this, the initial NULL seed public key name is made +* available in a sysfs file. +* +* Use of these functions: +* +* The design is all the crypto, hash and hmac gunk is confined in this +* file and never needs to be seen even by the kernel internal user. To +* the user there's an init function tpm2_sessions_init() that needs to +* be called once per TPM which generates the NULL seed primary key. +* +* Then there are six usage functions: +* +* tpm2_start_auth_session() which allocates the opaque auth structure +* and gets a session from the TPM. This must be called before +* any of the following functions. The session is protected by a +* session_key which is derived from a random salt value +* encrypted to the NULL seed. +* tpm2_end_auth_session() kills the session and frees the resources. +* Under normal operation this function is done by +* tpm_buf_check_hmac_response(), so this is only to be used on +* error legs where the latter is not executed. +* tpm_buf_append_name() to add a handle to the buffer. This must be +* used in place of the usual tpm_buf_append_u32() for adding +* handles because handles have to be processed specially when +* calculating the HMAC. In particular, for NV, volatile and +* permanent objects you now need to provide the name. +* tpm_buf_append_hmac_session() which appends the hmac session to the +* buf in the same way tpm_buf_append_auth does().
drivers/char/tpm/tpm2-sessions.c line 68
Quoted 10 lines
drivers/char/tpm/tpm2-sessions.c new file +10
-0+* tpm_buf_fill_hmac_session() This calculates the correct hash and +* places it in the buffer. It must be called after the complete +* command buffer is finalized so it can fill in the correct HMAC +* based on the parameters. +* tpm_buf_check_hmac_response() which checks the session response in +* the buffer and calculates what it should be. If there's a +* mismatch it will log a warning and return an error. If +* tpm_buf_append_hmac_session() did not specify +* TPM_SA_CONTINUE_SESSION then the session will be closed (if it +* hasn't been consumed) and the auth structure freed.
Should be reformatted to use kdoc e.g.
1. Documentation block in the file header (the last section in [1]).
2. Proper kdoc's for the functions.
This will allow generation of the documentation from this if done right (e.g. like DRM people do).
Quoted 9 lines
drivers/char/tpm/tpm2-sessions.c new file +9
-0+*/ + +#include "tpm.h" +#include "tpm2-sessions.h" + +#include <linux/random.h> +#include <linux/scatterlist.h> + +#include <asm/unaligned.h>
drivers/char/tpm/tpm2-sessions.c line 87
Quoted 10 lines
drivers/char/tpm/tpm2-sessions.c new file +10
-0+ +#include <crypto/aes.h> +#include <crypto/kpp.h> +#include <crypto/ecdh.h> +#include <crypto/hash.h> +#include <crypto/hmac.h> +#include <crypto/skcipher.h> + +/* if you change to AES256, you only need change this */ +#define AES_KEYBYTES AES_KEYSIZE_128
Ugh, this just masks things, please remove.
Quoted 51 lines
drivers/char/tpm/tpm2-sessions.c new file +51
-0+ +#define AES_KEYBITS (AES_KEYBYTES*8) +#define AUTH_MAX_NAMES 3 + +/* +* This is the structure that carries all the auth information (like +* session handle, nonces, session key and auth) from use to use it is +* designed to be opaque to anything outside. +*/ +struct tpm2_auth { +u32 handle; +/* +* This has two meanings: before tpm_buf_fill_hmac_session() +* it marks the offset in the buffer of the start of the +* sessions (i.e. after all the handles). Once the buffer has +* been filled it markes the session number of our auth +* session so we can find it again in the response buffer. +* +* The two cases are distinguished because the first offset +* must always be greater than TPM_HEADER_SIZE and the second +* must be less than or equal to 5. +*/ +u32 session; +/* +* the size here is variable and set by the size of our_nonce +* which must be between 16 and the name hash length. we set +* the maximum sha256 size for the greatest protection +*/ +u8 our_nonce[SHA256_DIGEST_SIZE ]; +u8 tpm_nonce[SHA256_DIGEST_SIZE ]; +/* +* the salt is only used across the session command/response +* after that it can be used as a scratch area +*/ +union { +u8 salt[EC_PT_SZ ]; +/* scratch for key + IV */ +u8 scratch[AES_KEYBYTES +AES_BLOCK_SIZE ]; + }; +/* +* the session key and passphrase are the same size as the +* name digest (sha256 again). The session key is constant +* for the use of the session and the passphrase can change +* with every invocation. +* +* Note: these fields must be adjacent and in this order +* because several HMAC/KDF schemes use the combination of the +* session_key and passphrase. +*/ +u8 session_key[SHA256_DIGEST_SIZE ]; +u8 passphrase[SHA256_DIGEST_SIZE ];
drivers/char/tpm/tpm2-sessions.c line 148
Quoted 10 lines
drivers/char/tpm/tpm2-sessions.c new file +10
-0+int passphraselen; +/* saved session attributes */ +u8 attrs; +__be32 ordinal; +struct crypto_sync_skcipher *aes; +struct tpm_chip *chip; +/* 3 names of handles: name_h is handle, name is name of handle */ +u32 name_h[AUTH_MAX_NAMES ]; +u8 name[AUTH_MAX_NAMES ][2 +SHA256_DIGEST_SIZE ]; +};
Check "kernel-doc for structs, unions, enums, and typedefs" in [1].
Look at iit this way: you've done a lot of work to explain why you are doing what you are doing and that is great. If we can generate documentation from it, it will be more accessible to everyone and does not get buried inside the source tree :-)
[1] https://www.kernel.org/doc/Documentation/kernel-doc-nano-HOWTO.txt
/Jarkko
Re: [PATCH v6 05/12] tpm2-sessions: Add full HMAC and encrypt/decrypt session handling
Quoted 1 line · Jarkko Sakkinen
On Mon, Sep 09, 2019 at 01:20:57PM +0100, James Bottomley wrote:
Forgot to ask: what is the new field handles?
/Jarkko
James Bottomley
maintainerRe: [PATCH v6 01/12] tpm-buf: move from static inlines to real functions
In reply to Jarkko SakkinenQuoted 8 lines · Jarkko Sakkinen
Quoted 7 lines · Jarkko Sakkinen
Quoted 6 lines · James Bottomley
This separates out the old
tpm_buf_... handling functions from static
inlines intpm.hand makes them their owntpm-buf.cfile. This is a
precursor so we can add new functions for other TPM type handling
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Signed-off-by James Bottomley <James.Bottomley@HansenPartnership.com>
What about
TPM_BUF_2Bthat gets added in this commit?Probably just a glitch in rebasing/squashing?
Well a glitch in splitting one patch into three, yes. I'll fix it up.
James
James Bottomley
maintainerRe: [PATCH v6 02/12] tpm-buf: add handling for TPM2B types
In reply to Jarkko SakkinenQuoted 11 lines · Jarkko Sakkinen
Quoted 8 lines · James Bottomley
Most complex TPM commands require appending TPM2B buffers to the command body. Since TPM2B types are essentially variable size arrays,it makes it impossible to represent these complex command arguments as structures and we simply have to build them up using append primitives like these.
Signed-off-by James Bottomley <[email protected]om>
I think a better idea would be to have headerless TPM buffers
I thought about that. The main problem is that most of the construct/append functions use the header, and these are the functions most useful to the TPM2B operation.
The other thing that argues against this is that the TPM2B case would save nothing if we eliminated the header, because we allocate a page for all the data regardless.
Quoted 3 lines
and also it makes sense to have a separate length field in the
struct to keep the code sane given that sometimes the buffer does not store the length.
I'm really not sure about that one. The header length has to be filled in for the non-TPM2B case but right at the moment we have no finish function for the buf where it could be, so we'd end up having to maintain two lengths in every update operation on non-TPM2B buffers. That seems inefficient and the only slight efficiency we get in the TPM2B case is not having to do the big endian conversion from the header which doesn't seem to be worth the added complexity.
James
Quoted 16 lines
E.g.
enum tpm_buf_flags { TPM_BUF_OVERFLOW = BIT(0), TPM_BUF_HEADERLESS = BIT(1), };struct tpm_buf { unsigned int length; struct page *data_page; unsigned int flags; u8 *data; };/Jarkko
James Bottomley
maintainerRe: [PATCH v6 05/12] tpm2-sessions: Add full HMAC and encrypt/decrypt session handling
In reply to Jarkko SakkinenQuoted 4 lines · Jarkko Sakkinen
Quoted 1 line · Jarkko Sakkinen
On Mon, Sep 09, 2019 at 01:20:57PM +0100, James Bottomley wrote:
Forgot to ask: what is the new field handles?
You mean for the null seed or for the virtual handles?
For the former, there isn't one since the null seed is maintained as a context when not in use, although since the null seed context is loaded before an operation it will mostly get 80000000 for the brief time it is used. For the latter, there's no change in the way virtual handles are processed.
James
Re: [PATCH v6 02/12] tpm-buf: add handling for TPM2B types
In reply to James BottomleyQuoted 7 lines · James Bottomley
I thought about that. The main problem is that most of the construct/append functions use the header, and these are the functions most useful to the TPM2B operation.
The other thing that argues against this is that the TPM2B case would save nothing if we eliminated the header, because we allocate a page for all the data regardless.
It would be way more clean. There is absolutely nothing TPM2B specific.
Quoted 11 lines
Quoted 3 lines
and also it makes sense to have a separate length field in the
struct to keep the code sane given that sometimes the buffer does not store the length.I'm really not sure about that one. The header length has to be filled in for the non-TPM2B case but right at the moment we have no finish function for the buf where it could be, so we'd end up having to maintain two lengths in every update operation on non-TPM2B buffers. That seems inefficient and the only slight efficiency we get in the TPM2B case is not having to do the big endian conversion from the header which doesn't seem to be worth the added complexity.
It would be way more clean and an insignificant concern when it comes to performance. I don't see any problem updating two lengths.
/Jarkko
Re: [PATCH v6 02/12] tpm-buf: add handling for TPM2B types
Quoted 10 lines · Jarkko Sakkinen
Quoted 7 lines · James Bottomley
I thought about that. The main problem is that most of the construct/append functions use the header, and these are the functions most useful to the TPM2B operation.
The other thing that argues against this is that the TPM2B case would save nothing if we eliminated the header, because we allocate a page for all the data regardless.
It would be way more clean. There is absolutely nothing TPM2B specific.
Given the recent regression I'm detaching allocation from tpm_buf and make it purely a decorator (sending patch today).
/Jarkko