TPM 2.0 trusted keys with attached policy
Subsystems (7)
James Bottomley
maintainerv9 cover TPM 2.0 trusted keys with attached policy
This is a respin on v8 to make the encoder selectable and address David's comments. The trusted key part hasn't changed except to add a now necessary select for ASN1_ENCODER to patch 4 and the changelog of patch 6 has been updated to correct the cut and paste error in the keyctl statement.
General cover letter:
This patch updates the trusted key code to export keys in the ASN.1 format used by current TPM key tools (openssl_tpm2_engine and openconnect). It also simplifies the use of policy with keys because the ASN.1 format is designed to carry a description of how to construct the policy, with the result that simple policies (like authorization and PCR locking) can now be constructed and used in the kernel, bringing the TPM 2.0 policy use into line with how TPM 1.2 works.
The key format is designed to be compatible with our two openssl engine implementations as well as with the format used by openconnect. I've added seal/unseal to my engine so I can use it for interoperability testing and I'll later use this for sealed symmetric keys via engine:
https://git.kernel.org/pub/scm/linux/kernel/git/jejb/openssl_tpm2_engine.git/
James
---
James Bottomley (8):
lib: add ASN.1 encoder
oid_registry: Add TCG defined OIDS for TPM keys
security: keys: trusted: fix TPM2 authorizations
security: keys: trusted: use ASN.1 TPM2 key format for the blobs
security: keys: trusted: Make sealed key properly interoperable
security: keys: trusted: add PCR policy to TPM2 keys
security: keys: trusted: add ability to specify arbitrary policy
security: keys: trusted: implement counter/timer policy
.../security/keys/trusted-encrypted.rst | 64 ++- include/keys/trusted-type.h | 7 +- include/linux/asn1_encoder.h | 32 ++ include/linux/oid_registry.h | 5 + include/linux/tpm.h | 8 + lib/Kconfig | 3 + lib/Makefile | 1 + lib/asn1_encoder.c | 454 +++++++++++++++++ security/keys/Kconfig | 3 + security/keys/trusted-keys/Makefile | 2 +- security/keys/trusted-keys/tpm2-policy.c | 463 ++++++++++++++++++ security/keys/trusted-keys/tpm2-policy.h | 31 ++ security/keys/trusted-keys/tpm2key.asn1 | 23 + security/keys/trusted-keys/trusted_tpm1.c | 56 ++- security/keys/trusted-keys/trusted_tpm2.c | 370 +++++++++++++- 15 files changed, 1486 insertions(+), 36 deletions(-) create mode 100644 include/linux/asn1_encoder.h create mode 100644 lib/asn1_encoder.c create mode 100644 security/keys/trusted-keys/tpm2-policy.c create mode 100644 security/keys/trusted-keys/tpm2-policy.h create mode 100644 security/keys/trusted-keys/tpm2key.asn1
Signature · 1 line
2.26.1
James Bottomley
maintainerPATCH v9 1/8 lib: add ASN.1 encoder
We have a need in the TPM2 trusted keys to return the ASN.1 form of the TPM key blob so it can be operated on by tools outside of the kernel. The specific tools are the openssl_tpm2_engine, openconnect and the Intel tpm2-tss-engine. To do that, we have to be able to read and write the same binary key format the tools use. The current ASN.1 decoder does fine for reading, but we need pieces of an ASN.1 encoder to write the key blob in binary compatible form.
For backwards compatibility, the trusted key reader code will still accept the two TPM2B quantities that it uses today, but the writer will only output the ASN.1 form.
The current implementation only encodes the ASN.1 bits we actually need.
---
v2: updated API to use indefinite length, and made symbol exports gpl
v3: add data length error handling
v4: use end_data instead of data_len pointer
v5: mention tools and space out code
v7: reverse christmas tree variable definitions and better spacing
v9: update comments, remove indefinite length, make encoder config dependent
--- include/linux/asn1_encoder.h | 32 +++ lib/Kconfig | 3 + lib/Makefile | 1 + lib/asn1_encoder.c | 454 +++++++++++++++++++++++++++++++++++ 4 files changed, 490 insertions(+) create mode 100644 include/linux/asn1_encoder.h create mode 100644 lib/asn1_encoder.c
include/linux/asn1_encoder.h new file +32 -0
@@ -0,0 +1,32 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef _LINUX_ASN1_ENCODER_H +#define _LINUX_ASN1_ENCODER_H + +#include <linux/types.h> +#include <linux/asn1.h> +#include <linux/asn1_ber_bytecode.h> +#include <linux/bug.h> + +#define asn1_oid_len (oid) (sizeof(oid)/sizeof(u32)) +unsigned char * +asn1_encode_integer (unsigned char *data,const unsigned char *end_data, +s64 integer); +unsigned char * +asn1_encode_oid (unsigned char *data,const unsigned char *end_data, +u32 oid[],int oid_len); +unsigned char * +asn1_encode_tag (unsigned char *data,const unsigned char *end_data, +u32 tag,const unsigned char *string,int len); +unsigned char * +asn1_encode_octet_string (unsigned char *data, +const unsigned char *end_data, +const unsigned char *string,u32 len); +unsigned char * +asn1_encode_sequence (unsigned char *data,const unsigned char *end_data, +const unsigned char *seq,int len); +unsigned char * +asn1_encode_boolean (unsigned char *data,const unsigned char *end_data, +bool val); + +#endif
lib/Kconfig +3 -0
@@ -670,3 +670,6 @@ config GENERIC_LIB_CMPDI2 config GENERIC_LIB_UCMPDI2 bool + +config ASN1_ENCODER + tristate
lib/Makefile +1 -0
@@ -245,6 +245,7 @@ obj-$(CONFIG_INTERVAL_TREE_TEST) += interval_tree_test.o obj-$(CONFIG_PERCPU_TEST) += percpu_test.o obj-$(CONFIG_ASN1) += asn1_decoder.o +obj-$(CONFIG_ASN1_ENCODER) += asn1_encoder.o obj-$(CONFIG_FONT_SUPPORT) += fonts/
lib/asn1_encoder.c new file +454 -0
454 lines not on this page · raw message
Signature · 1 line
2.26.1
Re: [PATCH v9 1/8] lib: add ASN.1 encoder
T24gVGh1LCAyMDIwLTA1LTA3IGF0IDE2OjExIC0wNzAwLCBKYW1lcyBCb3R0b21sZXkgd3JvdGU6 DQo+IFdlIGhhdmUgYSBuZWVkIGluIHRoZSBUUE0yIHRydXN0ZWQga2V5cyB0byByZXR1cm4gdGhl IEFTTi4xIGZvcm0gb2YNCj4gdGhlIFRQTSBrZXkgYmxvYiBzbyBpdCBjYW4gYmUgb3BlcmF0ZWQg b24gYnkgdG9vbHMgb3V0c2lkZSBvZiB0aGUNCj4ga2VybmVsLiAgVGhlIHNwZWNpZmljIHRvb2xz IGFyZSB0aGUgb3BlbnNzbF90cG0yX2VuZ2luZSwgb3BlbmNvbm5lY3QNCj4gYW5kIHRoZSBJbnRl bCB0cG0yLXRzcy1lbmdpbmUuICBUbyBkbyB0aGF0LCB3ZSBoYXZlIHRvIGJlIGFibGUgdG8gcmVh ZA0KPiBhbmQgd3JpdGUgdGhlIHNhbWUgYmluYXJ5IGtleSBmb3JtYXQgdGhlIHRvb2xzIHVzZS4g IFRoZSBjdXJyZW50IEFTTi4xDQo+IGRlY29kZXIgZG9lcyBmaW5lIGZvciByZWFkaW5nLCBidXQg d2UgbmVlZCBwaWVjZXMgb2YgYW4gQVNOLjEgZW5jb2Rlcg0KPiB0byB3cml0ZSB0aGUga2V5IGJs b2IgaW4gYmluYXJ5IGNvbXBhdGlibGUgZm9ybS4NCj4gDQo+IEZvciBiYWNrd2FyZHMgY29tcGF0 aWJpbGl0eSwgdGhlIHRydXN0ZWQga2V5IHJlYWRlciBjb2RlIHdpbGwgc3RpbGwNCj4gYWNjZXB0 IHRoZSB0d28gVFBNMkIgcXVhbnRpdGllcyB0aGF0IGl0IHVzZXMgdG9kYXksIGJ1dCB0aGUgd3Jp dGVyDQo+IHdpbGwgb25seSBvdXRwdXQgdGhlIEFTTi4xIGZvcm0uDQo+IA0KPiBUaGUgY3VycmVu dCBpbXBsZW1lbnRhdGlvbiBvbmx5IGVuY29kZXMgdGhlIEFTTi4xIGJpdHMgd2UgYWN0dWFsbHkg bmVlZC4NCj4gDQo+IFNpZ25lZC1vZmYtYnk6IEphbWVzIEJvdHRvbWxleSA8SmFtZXMuQm90dG9t bGV5QEhhbnNlblBhcnRuZXJzaGlwLmNvbT4NCj4gUmV2aWV3ZWQtYnk6IEphcmtrbyBTYWtraW5l biA8amFya2tvLnNha2tpbmVuQGxpbnV4LmludGVsLmNvbT4NCg0KVGVzdGVkLWJ5OiBKYXJra28g U2Fra2luZW4gPGphcmtrby5zYWtraW5lbkBsaW51eC5pbnRlbC5jb20+DQoNCi9KYXJra28NCg=
James Bottomley
maintainerPATCH v9 2/8 oid_registry: Add TCG defined OIDS for TPM keys
In reply to James BottomleyThe TCG has defined an OID prefix "2.23.133.10.1" for the various TPM key uses. We've defined three of the available numbers:
2.23.133.10.1.3 TPM Loadable key. This is an asymmetric key (Usually
RSA2048 or Elliptic Curve) which can be imported by a TPM2_Load() operation.
2.23.133.10.1.4 TPM Importable Key. This is an asymmetric key (Usually
RSA2048 or Elliptic Curve) which can be imported by a TPM2_Import() operation.
Both loadable and importable keys are specific to a given TPM, the difference is that a loadable key is wrapped with the symmetric secret, so must have been created by the TPM itself. An importable key is wrapped with a DH shared secret, and may be created without access to the TPM provided you know the public part of the parent key.
2.23.133.10.1.5 TPM Sealed Data. This is a set of data (up to 128
bytes) which is sealed by the TPM. It usually represents a symmetric key and must be unsealed before use.
The ASN.1 binary key form starts of with this OID as the first element of a sequence, giving the binary form a unique recognizable identity marker regardless of encoding.
---
v3: correct OID_TPMImportableKey name
v7: add ack
v9: add review
--- include/linux/oid_registry.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/oid_registry.h +5 -0
@@ -107,6 +107,11 @@ enum OID { OID_gostTC26Sign512B,/* 1.2.643.7.1.2.1.2.2 */ OID_gostTC26Sign512C,/* 1.2.643.7.1.2.1.2.3 */ +/* TCG defined OIDS for TPM based keys */ + OID_TPMLoadableKey,/* 2.23.133.10.1.3 */ + OID_TPMImportableKey,/* 2.23.133.10.1.4 */ + OID_TPMSealedData,/* 2.23.133.10.1.5 */ + OID__NR };
Signature · 1 line
2.26.1
James Bottomley
maintainerPATCH v9 3/8 security: keys: trusted: fix TPM2 authorizations
In reply to James BottomleyIn TPM 1.2 an authorization was a 20 byte number. The spec actually recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.
so before
keyctl add trusted kmk "new 32 blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"
after we will accept both the old hex sha1 form as well as a new directly supplied password:
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Since a sha1 hex code must be exactly 40 bytes long and a direct password must be 20 or less, we use the length as the discriminator for which form is input.
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.
---
v6: change comment, eliminate else clauses and add fixes tag
v7: fixes before signoff
--- include/keys/trusted-type.h | 1 + security/keys/trusted-keys/trusted_tpm1.c | 32 ++++++++++++++++++----- security/keys/trusted-keys/trusted_tpm2.c | 10 ++++--- 3 files changed, 33 insertions(+), 10 deletions(-)
include/keys/trusted-type.h +1 -0
@@ -30,6 +30,7 @@ struct trusted_key_options {uint16_t keytype;uint32_t keyhandle;unsigned char keyauth[TPM_DIGEST_SIZE ]; +uint32_t blobauth_len;unsigned char blobauth[TPM_DIGEST_SIZE ];uint32_t pcrinfo_len;unsigned char pcrinfo[MAX_PCRINFO_SIZE ];
security/keys/trusted-keys/trusted_tpm1.c +26 -6
@@ -781,13 +781,33 @@ static int getoptions(char *c, struct trusted_key_payload *pay,return -EINVAL ;break ;case Opt_blobauth: -if (strlen (args[0 ].from) !=2 *SHA1_DIGEST_SIZE ) -return -EINVAL ; - res =hex2bin (opt->blobauth, args[0 ].from, -SHA1_DIGEST_SIZE ); -if (res <0 ) -return -EINVAL ; +/* +* TPM 1.2 authorizations are sha1 hashes passed in as +* hex strings. TPM 2.0 authorizations are simple +* passwords (although it can take a hash as well) +*/ + opt->blobauth_len =strlen (args[0 ].from); + +if (opt->blobauth_len =2 *TPM_DIGEST_SIZE ) { + res =hex2bin (opt->blobauth, args[0 ].from, +TPM_DIGEST_SIZE ); +if (res <0 ) +return -EINVAL ; + + opt->blobauth_len =TPM_DIGEST_SIZE ; +return 0 ; + } + +if (tpm2 && opt->blobauth_len <=sizeof (opt->blobauth)) { +memcpy (opt->blobauth, args[0 ].from, + opt->blobauth_len); +return 0 ; + } + +return -EINVAL ; +break ; +case Opt_migratable:if (*args[0 ].from ='0' ) pay->migratable =0 ;
security/keys/trusted-keys/trusted_tpm2.c +6 -4
@@ -91,10 +91,12 @@ int tpm2_seal_trusted(struct tpm_chip *chip, TPM_DIGEST_SIZE);/* sensitive */ -tpm_buf_append_u16 (&buf,4 +TPM_DIGEST_SIZE + payload->key_len +1 ); +tpm_buf_append_u16 (&buf,4 + options->blobauth_len + payload->key_len +1 ); + +tpm_buf_append_u16 (&buf, options->blobauth_len); +if (options->blobauth_len) +tpm_buf_append (&buf, options->blobauth, options->blobauth_len); -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);
@@ -258,7 +260,7 @@ static int tpm2_unseal_cmd(struct tpm_chip *chip,NULL /* nonce */ ,0 ,TPM2_SA_CONTINUE_SESSION , options->blobauth/* hmac */ , - TPM_DIGEST_SIZE); + options->blobauth_len); rc =tpm_send (chip, buf.data,tpm_buf_length (&buf));if (rc >0 )
Signature · 1 line
2.26.1
Re: [PATCH v9 3/8] security: keys: trusted: fix TPM2 authorizations
Quoted 29 lines · James Bottomley
In TPM 1.2 an authorization was a 20 byte number. The spec actually recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.
so before
keyctl add trusted kmk "new 32 blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"
after we will accept both the old hex sha1 form as well as a new directly supplied password:
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Since a sha1 hex code must be exactly 40 bytes long and a direct password must be 20 or less, we use the length as the discriminator for which form is input.
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with
TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.
Quoted 3 lines
Fixes 0fe5480303a1 ("keys, trusted: seal/unseal with TPM 2.0 chips")Signed-off-by James Bottomley <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>
Have not checked yet the tail. Probably won't check before PR for v5.8 is out.
Just wondering would it hurt to merge everything up until this patch?
/Jarkko
Re: [PATCH v9 3/8] security: keys: trusted: fix TPM2 authorizations
Quoted 30 lines · Jarkko Sakkinen
Quoted 29 lines · James Bottomley
In TPM 1.2 an authorization was a 20 byte number. The spec actually recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.
so before
keyctl add trusted kmk "new 32 blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"
after we will accept both the old hex sha1 form as well as a new directly supplied password:
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Since a sha1 hex code must be exactly 40 bytes long and a direct password must be 20 or less, we use the length as the discriminator for which form is input.
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with
TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.
Quoted 8 lines
Quoted 3 lines
Fixes 0fe5480303a1 ("keys, trusted: seal/unseal with TPM 2.0 chips")Signed-off-by James Bottomley <[email protected]>Reviewed-by Jarkko Sakkinen <[email protected]>Have not checked yet the tail. Probably won't check before PR for v5.8 is out.
Just wondering would it hurt to merge everything up until this patch?
I.e. could land it also to the release.
/Jarkko
James Bottomley
maintainerRe: [PATCH v9 3/8] security: keys: trusted: fix TPM2 authorizations
Quoted 40 lines · Jarkko Sakkinen
Quoted 39 lines · Jarkko Sakkinen
Quoted 37 lines · James Bottomley
In TPM 1.2 an authorization was a 20 byte number. The spec actually recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.
so before
keyctl add trusted kmk "new 32
blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"after we will accept both the old hex sha1 form as well as a new directly supplied password:
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Since a sha1 hex code must be exactly 40 bytes long and a direct password must be 20 or less, we use the length as the discriminator for which form is input.
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with
TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.Fixes 0fe5480303a1 ("keys, trusted: seal/unseal with TPM 2.0chips")
Signed-off-by James Bottomley <James.Bottomley@HansenPartnership.com>
Reviewed-by Jarkko Sakkinen <[email protected]>
Quoted 5 lines
Quoted 5 lines
Have not checked yet the tail. Probably won't check before PR for v5.8 is out.
Just wondering would it hurt to merge everything up until this patch?
Everything would be OK if you applied 1, 2 and 3. Except we'd have an ASN.1 API in the tree with no consumers, which excites some people.
Quoted 1 line
I.e. could land it also to the release.
That would likely be fine and should satisfy the API with no consumers issue.
James
Re: [PATCH v9 3/8] security: keys: trusted: fix TPM2 authorizations
Quoted 47 lines · James Bottomley
Quoted 40 lines · Jarkko Sakkinen
Quoted 39 lines · Jarkko Sakkinen
Quoted 37 lines · James Bottomley
In TPM 1.2 an authorization was a 20 byte number. The spec actually recommended you to hash variable length passwords and use the sha1 hash as the authorization. Because the spec doesn't require this hashing, the current authorization for trusted keys is a 40 digit hex number. For TPM 2.0 the spec allows the passing in of variable length passwords and passphrases directly, so we should allow that in trusted keys for ease of use. Update the 'blobauth' parameter to take this into account, so we can now use plain text passwords for the keys.
so before
keyctl add trusted kmk "new 32
blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"after we will accept both the old hex sha1 form as well as a new directly supplied password:
keyctl add trusted kmk "new 32 blobauth=hello keyhandle�000001"
Since a sha1 hex code must be exactly 40 bytes long and a direct password must be 20 or less, we use the length as the discriminator for which form is input.
Note this is both and enhancement and a potential bug fix. The TPM 2.0 spec requires us to strip leading zeros, meaning empyty authorization is a zero length HMAC whereas we're currently passing in 20 bytes of zeros. A lot of TPMs simply accept this as OK, but the Microsoft TPM emulator rejects it with
TPM_RC_BAD_AUTH, so this patch makes the Microsoft TPM emulator work with trusted keys.Fixes 0fe5480303a1 ("keys, trusted: seal/unseal with TPM 2.0chips")
Signed-off-by James Bottomley <James.Bottomley@HansenPartnership.com>
Reviewed-by Jarkko Sakkinen <[email protected]>Quoted 5 lines
Quoted 5 lines
Have not checked yet the tail. Probably won't check before PR for v5.8 is out.
Just wondering would it hurt to merge everything up until this patch?
Quoted 7 lines
Everything would be OK if you applied 1, 2 and 3. Except we'd have an ASN.1 API in the tree with no consumers, which excites some people.
Quoted 1 line
I.e. could land it also to the release.
That would likely be fine and should satisfy the API with no consumers issue.
Hmm. Right, it does not sense to merge unused API.
I'd like to still pick this patch (3/8) but you need to fix these first:
WARNING: Possible unwrapped commit description (prefer a maximum 75 chars per line) #17:
keyctl add trusted kmk "new 32 blobauthõ72d396fae9206628714fb2ce00f72e94f2258f"
WARNING: line over 80 characters
#89: FILE: security/keys/trusted-keys/trusted_tpm1.c:801:
+ if (tpm2 && opt->blobauth_len <= sizeof(opt->blobauth)) {
WARNING: line over 80 characters
#111: FILE: security/keys/trusted-keys/trusted_tpm2.c:94:
+ tpm_buf_append_u16(&buf, 4 + options->blobauth_len + payload->key_len + 1);
The best way is probably to encapsulate into helper function. It is more or less a sign that the code is too complicated to live inside a switch-case statement.
Can you do that and send it as a separate patch?
/Jarkko
James Bottomley
maintainerPATCH v9 4/8 security: keys: trusted: use ASN.1 TPM2 key format for the blobs
In reply to James BottomleyModify the TPM2 key format blob output to export and import in the ASN.1 form for TPM2 sealed object keys. For compatibility with prior trusted keys, the importer will also accept two TPM2B quantities representing the public and private parts of the key. However, the export via keyctl pipe will only output the ASN.1 format.
The benefit of the ASN.1 format is that it's a standard and thus the exported key can be used by userspace tools (openssl_tpm2_engine, openconnect and tpm2-tss-engine). The format includes policy specifications, thus it gets us out of having to construct policy handles in userspace and the format includes the parent meaning you don't have to keep passing it in each time.
This patch only implements basic handling for the ASN.1 format, so keys with passwords but no policy.
---
v2: Updated encode API, added length checks
v5: correct export format after doing interoperability checks
v7: use prefix tpm2_key_ instead of tpmkey_ for functions
v8: resplit commit
v9: select ASN1_ENCODER
--- include/keys/trusted-type.h | 1 + security/keys/Kconfig | 1 + security/keys/trusted-keys/Makefile | 2 +- security/keys/trusted-keys/tpm2key.asn1 | 23 +++ security/keys/trusted-keys/trusted_tpm1.c | 2 +- security/keys/trusted-keys/trusted_tpm2.c | 207 +++++++++++++++++++++- 6 files changed, 228 insertions(+), 8 deletions(-) create mode 100644 security/keys/trusted-keys/tpm2key.asn1
include/keys/trusted-type.h +1 -0
@@ -22,6 +22,7 @@ struct trusted_key_payload {unsigned int key_len;unsigned int blob_len;unsigned char migratable; +unsigned char old_format;unsigned char key[MAX_KEY_SIZE +1 ];unsigned char blob[MAX_BLOB_SIZE ]; };
security/keys/Kconfig +1 -0
@@ -77,6 +77,7 @@ config TRUSTED_KEYS select CRYPTO_HMAC select CRYPTO_SHA1 select CRYPTO_HASH_INFO + select ASN1_ENCODER help This option provides support for creating, sealing, and unsealing keys in the kernel. Trusted keys are random number symmetric keys,
security/keys/trusted-keys/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_TRUSTED_KEYS) += trusted.o trusted-y += trusted_tpm1.o -trusted-y += trusted_tpm2.o +trusted-y += trusted_tpm2.o tpm2key.asn1.o
security/keys/trusted-keys/tpm2key.asn1 new file +23 -0
@@ -0,0 +1,23 @@ +--- +--- Note: This isn't quite the definition in the standard +--- However, the Linux asn.1 parser doesn't understand +--- [2] EXPLICIT SEQUENCE OF OPTIONAL +--- So there's an extra intermediate TPMPolicySequence +--- definition to work around this + +TPMKey ::= SEQUENCE { + type OBJECT IDENTIFIER ({tpm2_key_type}), + emptyAuth [0] EXPLICIT BOOLEAN OPTIONAL, + policy [1] EXPLICIT TPMPolicySequence OPTIONAL, + secret [2] EXPLICIT OCTET STRING OPTIONAL, + parent INTEGER ({tpm2_key_parent}), + pubkey OCTET STRING ({tpm2_key_pub}), + privkey OCTET STRING ({tpm2_key_priv}) + } + +TPMPolicySequence ::= SEQUENCE OF TPMPolicy + +TPMPolicy ::= SEQUENCE { + commandCode [0] EXPLICIT INTEGER, + commandPolicy [1] EXPLICIT OCTET STRING + }
security/keys/trusted-keys/trusted_tpm1.c +1 -1
@@ -1011,7 +1011,7 @@ static int trusted_instantiate(struct key *key, goto out; } -if (!options->keyhandle) { +if (!options->keyhandle && !tpm2) { ret = -EINVAL ; goto out; }
security/keys/trusted-keys/trusted_tpm2.c +200 -6
264 lines not on this page · raw message
Signature · 1 line
2.26.1
Re: [PATCH v9 4/8] security: keys: trusted: use ASN.1 TPM2 key format for the blobs
T24gVGh1LCAyMDIwLTA1LTA3IGF0IDE2OjExIC0wNzAwLCBKYW1lcyBCb3R0b21sZXkgd3JvdGU6 DQo+IE1vZGlmeSB0aGUgVFBNMiBrZXkgZm9ybWF0IGJsb2Igb3V0cHV0IHRvIGV4cG9ydCBhbmQg aW1wb3J0IGluIHRoZQ0KPiBBU04uMSBmb3JtIGZvciBUUE0yIHNlYWxlZCBvYmplY3Qga2V5cy4g IEZvciBjb21wYXRpYmlsaXR5IHdpdGggcHJpb3INCj4gdHJ1c3RlZCBrZXlzLCB0aGUgaW1wb3J0 ZXIgd2lsbCBhbHNvIGFjY2VwdCB0d28gVFBNMkIgcXVhbnRpdGllcw0KPiByZXByZXNlbnRpbmcg dGhlIHB1YmxpYyBhbmQgcHJpdmF0ZSBwYXJ0cyBvZiB0aGUga2V5LiAgSG93ZXZlciwgdGhlDQo+ IGV4cG9ydCB2aWEga2V5Y3RsIHBpcGUgd2lsbCBvbmx5IG91dHB1dCB0aGUgQVNOLjEgZm9ybWF0 Lg0KPiANCj4gVGhlIGJlbmVmaXQgb2YgdGhlIEFTTi4xIGZvcm1hdCBpcyB0aGF0IGl0J3MgYSBz dGFuZGFyZCBhbmQgdGh1cyB0aGUNCj4gZXhwb3J0ZWQga2V5IGNhbiBiZSB1c2VkIGJ5IHVzZXJz cGFjZSB0b29scyAob3BlbnNzbF90cG0yX2VuZ2luZSwNCj4gb3BlbmNvbm5lY3QgYW5kIHRwbTIt dHNzLWVuZ2luZSkuICBUaGUgZm9ybWF0IGluY2x1ZGVzIHBvbGljeQ0KPiBzcGVjaWZpY2F0aW9u cywgdGh1cyBpdCBnZXRzIHVzIG91dCBvZiBoYXZpbmcgdG8gY29uc3RydWN0IHBvbGljeQ0KPiBo YW5kbGVzIGluIHVzZXJzcGFjZSBhbmQgdGhlIGZvcm1hdCBpbmNsdWRlcyB0aGUgcGFyZW50IG1l YW5pbmcgeW91DQo+IGRvbid0IGhhdmUgdG8ga2VlcCBwYXNzaW5nIGl0IGluIGVhY2ggdGltZS4N Cj4gDQo+IFRoaXMgcGF0Y2ggb25seSBpbXBsZW1lbnRzIGJhc2ljIGhhbmRsaW5nIGZvciB0aGUg QVNOLjEgZm9ybWF0LCBzbw0KPiBrZXlzIHdpdGggcGFzc3dvcmRzIGJ1dCBubyBwb2xpY3kuDQo+ IA0KPiBTaWduZWQtb2ZmLWJ5OiBKYW1lcyBCb3R0b21sZXkgPEphbWVzLkJvdHRvbWxleUBIYW5z ZW5QYXJ0bmVyc2hpcC5jb20+DQoNClJldmlld2VkLWJ5OiBKYXJra28gU2Fra2luZW4gPGphcmtr by5zYWtraW5lbkBsaW51eC5pbnRlbC5jb20+DQpUZXN0ZWQtYnk6IEphcmtrbyBTYWtraW5lbiA8 amFya2tvLnNha2tpbmVuQGxpbnV4LmludGVsLmNvbT4NCg0KL0phcmtrbw0K
James Bottomley
maintainerPATCH v9 5/8 security: keys: trusted: Make sealed key properly interoperable
In reply to James BottomleyThe current implementation appends a migratable flag to the end of a key, meaning the format isn't exactly interoperable because the using party needs to know to strip this extra byte. However, all other consumers of TPM sealed blobs expect the unseal to return exactly the key. Since TPM2 keys have a key property flag that corresponds to migratable, use that flag instead and make the actual key the only sealed quantity. This is secure because the key properties are bound to a hash in the private part, so if they're altered the key won't load.
Backwards compatibility is implemented by detecting whether we're loading a new format key or not and correctly setting migratable from the last byte of old format keys.
---
v2: added length checks to untrusted payload
v8: recover patch
--- include/linux/tpm.h | 2 + security/keys/trusted-keys/trusted_tpm2.c | 53 ++++++++++++++++------- 2 files changed, 40 insertions(+), 15 deletions(-)
include/linux/tpm.h +2 -0
@@ -297,6 +297,8 @@ struct tpm_buf { };enum tpm2_object_attributes { +TPM2_OA_FIXED_TPM =BIT (1 ), +TPM2_OA_FIXED_PARENT =BIT (4 ),TPM2_OA_USER_WITH_AUTH =BIT (6 ), };
security/keys/trusted-keys/trusted_tpm2.c +38 -15
105 lines not on this page · raw message
Signature · 1 line
2.26.1
James Bottomley
maintainerPATCH v9 6/8 security: keys: trusted: add PCR policy to TPM2 keys
In reply to James BottomleyThis commit adds the ability to specify a PCR lock policy to TPM2 keys. There is a complexity in that the creator of the key must chose either to use a PCR lock policy or to use authentication. At the current time they can't use both due to a complexity with the way authentication works when policy registers are in use. The way to construct a pcrinfo statement for a key is simply to use the TPMS_PCR_SELECT structure to specify the PCRs and follow this by a hash of all their values in order of ascending PCR number.
For simplicity, we require the policy name hash and the hash used for the PCRs to be the same. Thus to construct a policy around the value of the resettable PCR 16 using the sha1 bank, first reset the pcr to zero giving a hash of all zeros as:
6768033e216468247bd031a0a2d9876d79818f8f
Then the TPMS_PCR_SELECT value for PCR 16 is
03000001
So create a new 32 byte key with a policy policy locking the key to this value of PCR 16 with a parent key of 81000001 would be:
keyctl add trusted kmk "new 32 keyhandle=0x81000001 hash=sha1 pcrinfo0000016768033e216468247bd031a0a2d9876d79818f8f" @u
---
v2: fix for new ASN.1 API eliminating hack in place and check lengths
v8: recover patch and fix offsets for hash and size
v9: fix cut and paste error in commit log
--- .../security/keys/trusted-encrypted.rst | 19 +- include/keys/trusted-type.h | 5 +- include/linux/tpm.h | 5 + security/keys/Kconfig | 2 + security/keys/trusted-keys/Makefile | 2 +- security/keys/trusted-keys/tpm2-policy.c | 372 ++++++++++++++++++ security/keys/trusted-keys/tpm2-policy.h | 30 ++ security/keys/trusted-keys/tpm2key.asn1 | 4 +- security/keys/trusted-keys/trusted_tpm1.c | 7 +- security/keys/trusted-keys/trusted_tpm2.c | 86 +++- 10 files changed, 516 insertions(+), 16 deletions(-) create mode 100644 security/keys/trusted-keys/tpm2-policy.c create mode 100644 security/keys/trusted-keys/tpm2-policy.h
Documentation/security/keys/trusted-encrypted.rst +18 -1
@@ -60,7 +60,10 @@ Usage:: (40 ascii zeros) blobauth= ascii hex auth for sealed data default 0x00... (40 ascii zeros) - pcrinfo= ascii hex of PCR_INFO or PCR_INFO_LONG (no default) + pcrinfo= ascii hex of PCR_INFO or PCR_INFO_LONG (no + default) on TPM 1.2 and a TPMS_PCR_SELECTION + coupled with a hash of all the selected PCRs on + TPM 2.0 using the selected hash. pcrlock= pcr number to be extended to "lock" blob migratable= 0|1 indicating permission to reseal to new PCR values, default 1 (resealing allowed)
@@ -151,6 +154,20 @@ Load a trusted key from the saved blob:: f1f8fff03ad0acb083725535636addb08d73dedb9832da198081e5deae84bfaf0409c22b e4a8aea2b607ec96931e6f4d4fe563ba +Create a trusted key on TPM 2.0 using an all zero value of PCR16 and +using the NV storage root 81000001 as the parent:: + + $ keyctl add trusted kmk "new 32 keyhandle=0x81000001 hash=sha1 pcrinfo0000016768033e216468247bd031a0a2d9876d79818f8f" @u + +Note the TPMS_PCR_SELECT value for PCR 16 is 03000001 because all +current TPMs have 24 PCRs, so the initial 03 says there are three +following bytes of selection and then because the bytes are big +endian, 16 is bit zero of byte 2. the hash is the sha1 sum of all +zeros (the value of PCR 16):: + + $ dd if=/dev/zero bs=1 count 2>/dev/null|sha1sum + 6768033e216468247bd031a0a2d9876d79818f8f + Reseal a trusted key under new pcr values:: $ keyctl update 268728824 "update pcrinfo=`cat pcr.blob`"
include/keys/trusted-type.h +4 -1
@@ -14,9 +14,11 @@#define MIN_KEY_SIZE 32#define MAX_KEY_SIZE 128#define MAX_BLOB_SIZE 512 -#define MAX_PCRINFO_SIZE 64 +#define MAX_PCRINFO_SIZE 128#define MAX_DIGEST_SIZE 64 +#define TPM2_MAX_POLICIES 16 +struct trusted_key_payload {struct rcu_head rcu;unsigned int key_len;
@@ -25,6 +27,7 @@ struct trusted_key_payload {unsigned char old_format;unsigned char key[MAX_KEY_SIZE +1 ];unsigned char blob[MAX_BLOB_SIZE ]; +struct tpm2_policies *policies; };struct trusted_key_options {
include/linux/tpm.h +5 -0
@@ -222,10 +222,14 @@ enum tpm2_command_codes {TPM2_CC_CONTEXT_LOAD =0x0161 ,TPM2_CC_CONTEXT_SAVE =0x0162 ,TPM2_CC_FLUSH_CONTEXT =0x0165 , +TPM2_CC_POLICY_AUTHVALUE =0x016B , +TPM2_CC_POLICY_COUNTER_TIMER =0x016D , +TPM2_CC_START_AUTH_SESS =0x0176 ,TPM2_CC_VERIFY_SIGNATURE =0x0177 ,TPM2_CC_GET_CAPABILITY =0x017A ,TPM2_CC_GET_RANDOM =0x017B ,TPM2_CC_PCR_READ =0x017E , +TPM2_CC_POLICY_PCR =0x017F ,TPM2_CC_PCR_EXTEND =0x0182 ,TPM2_CC_EVENT_SEQUENCE_COMPLETE =0x0185 , TPM2_CC_HASH_SEQUENCE_START = 0x0186,
@@ -234,6 +238,7 @@ enum tpm2_command_codes { };enum tpm2_permanent_handles { +TPM2_RH_NULL =0x40000007 ,TPM2_RS_PW =0x40000009 , };
security/keys/Kconfig +2 -0
@@ -76,6 +76,8 @@ config TRUSTED_KEYS select CRYPTO select CRYPTO_HMAC select CRYPTO_SHA1 + select CRYPTO_SHA256 + select CRYPTO_SHA512 select CRYPTO_HASH_INFO select ASN1_ENCODER help
security/keys/trusted-keys/Makefile +1 -1
@@ -5,4 +5,4 @@ obj-$(CONFIG_TRUSTED_KEYS) += trusted.o trusted-y += trusted_tpm1.o -trusted-y += trusted_tpm2.o tpm2key.asn1.o +trusted-y += trusted_tpm2.o tpm2key.asn1.o tpm2-policy.o
security/keys/trusted-keys/tpm2-policy.c new file +372 -0
372 lines not on this page · raw message
security/keys/trusted-keys/tpm2-policy.h new file +30 -0
@@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +struct tpm2_key_context { +u32 parent; +const u8 *pub; +u32 pub_len; +const u8 *priv; +u32 priv_len; +const u8 *policies[TPM2_MAX_POLICIES ]; +u32 policy_code[TPM2_MAX_POLICIES ]; +u16 policy_len[TPM2_MAX_POLICIES ]; +u8 policy_count; +}; + +struct tpm2_policies { +u32 code[TPM2_MAX_POLICIES ]; +u8 *policies[TPM2_MAX_POLICIES ]; +u16 len[TPM2_MAX_POLICIES ]; +u8 count; +u16 hash; +u16 hash_size; +}; + +int tpm2_key_policy_process (struct tpm2_key_context *ctx, +struct trusted_key_payload *payload); +int tpm2_generate_policy_digest (struct tpm2_policies *pols,u32 hash, +u8 *policydigest,u32 *plen); +int tpm2_encode_policy (struct tpm2_policies *pols,u8 **data,u32 *len); +int tpm2_get_policy_session (struct tpm_chip *chip,struct tpm2_policies *pols, +u32 *handle);
security/keys/trusted-keys/tpm2key.asn1 +2 -2
@@ -18,6 +18,6 @@ TPMKey ::= SEQUENCE { TPMPolicySequence ::= SEQUENCE OF TPMPolicy TPMPolicy ::= SEQUENCE { - commandCode [0] EXPLICIT INTEGER, - commandPolicy [1] EXPLICIT OCTET STRING + commandCode [0] EXPLICIT INTEGER ({tpm2_key_code}), + commandPolicy [1] EXPLICIT OCTET STRING ({tpm2_key_policy}) }
security/keys/trusted-keys/trusted_tpm1.c +6 -1
@@ -1065,6 +1065,7 @@ static void trusted_rcu_free(struct rcu_head *rcu)struct trusted_key_payload *p; p = container_of(rcu, struct trusted_key_payload, rcu); +kzfree (p->policies);kzfree (p); }
@@ -1174,7 +1175,11 @@ static long trusted_read(const struct key *key, char *buffer,*/ static void trusted_destroy (struct key *key) { -kzfree (key->payload.data[0 ]); +struct trusted_key_payload *p; + + p = key->payload.data[0 ]; +kzfree (p->policies); +kzfree (p); }struct key_type key_type_trusted = {
security/keys/trusted-keys/trusted_tpm2.c +76 -10
135 lines not on this page · raw message
Signature · 1 line
2.26.1
James Bottomley
maintainerPATCH v9 7/8 security: keys: trusted: add ability to specify arbitrary policy
In reply to James BottomleyThis patch adds a policy= argument to key creation. The policy is the standard tss policymaker format and each separate policy line must have a newline after it.
Thus to construct a policy requiring authorized value and pcr 16 locking using a sha256 hash, the policy (policy.txt) file would be two lines:
0000017F00000001000B03000001303095B49BE85E381E5B20E557E46363EF55B0F43B132C2D8E3DE9AC436656F2 0000016b
This can be inserted into the key with
keyctl add trusted kmk "new 32 policy=cat policy.txt keyhandle=0x81000001 hash=sha256" @u
Note that although a few policies work like this, most require special handling which must be added to the kernel policy construction routine.
--- .../security/keys/trusted-encrypted.rst | 16 ++++++ security/keys/trusted-keys/tpm2-policy.c | 53 +++++++++++++++++++ security/keys/trusted-keys/tpm2-policy.h | 1 + security/keys/trusted-keys/trusted_tpm1.c | 15 ++++++ 4 files changed, 85 insertions(+)
Documentation/security/keys/trusted-encrypted.rst +16 -0
@@ -76,6 +76,9 @@ Usage:: policyhandle= handle to an authorization policy session that defines the same policy and with the same hash algorithm as was used to seal the key. + policy= specify an arbitrary set of policies. These must + be in policymaker format with each separate + policy line newline terminated. "keyctl print" returns an ascii hex copy of the sealed key, which is in standard TPM_STORED_DATA format. The key length for new keys are always in bytes.
@@ -168,6 +171,19 @@ zeros (the value of PCR 16):: $ dd if=/dev/zero bs=1 count 2>/dev/null|sha1sum 6768033e216468247bd031a0a2d9876d79818f8f +You can also specify arbitrary policy in policymaker format, so a two +value policy (the pcr example above and authvalue) would look like +this in policymaker format:: + + 0000017F000000010004030000016768033e216468247bd031a0a2d9876d79818f8f + 0000016b + +This can be placed in a file (say policy.txt) and then added to the key as:: + + $ keyctl add trusted kmk "new 32 keyhandle=0x81000001 hash=sha1 policy=`cat policy.txt`" @u + +The newlines in the file policy.txt will be automatically processed. + Reseal a trusted key under new pcr values:: $ keyctl update 268728824 "update pcrinfo=`cat pcr.blob`"
security/keys/trusted-keys/tpm2-policy.c +53 -0
56 lines not on this page · raw message
security/keys/trusted-keys/tpm2-policy.h +1 -0
@@ -28,3 +28,4 @@ int tpm2_generate_policy_digest(struct tpm2_policies *pols, u32 hash,int tpm2_encode_policy (struct tpm2_policies *pols,u8 **data,u32 *len);int tpm2_get_policy_session (struct tpm_chip *chip,struct tpm2_policies *pols,u32 *handle); +int tpm2_parse_policies (struct tpm2_policies **ppols,char *str);
security/keys/trusted-keys/trusted_tpm1.c +15 -0
@@ -29,6 +29,8 @@#include <keys/trusted_tpm.h> +#include "tpm2-policy.h" +static const char hmac_alg[] ="hmac(sha1)" ;static const char hash_alg[] ="sha1" ;static struct tpm_chip *chip;
@@ -709,6 +711,7 @@ enum { Opt_hash, Opt_policydigest, Opt_policyhandle, + Opt_policy, }; static const match_table_t key_tokens = {
@@ -724,6 +727,7 @@ static const match_table_t key_tokens = { {Opt_hash, "hash=%s"}, {Opt_policydigest, "policydigest=%s"}, {Opt_policyhandle, "policyhandle=%s"}, + {Opt_policy, "policy=%s"}, {Opt_err, NULL} };
@@ -854,6 +858,17 @@ static int getoptions(char *c, struct trusted_key_payload *pay,return -EINVAL ; opt->policyhandle = handle;break ; + +case Opt_policy: +if (pay->policies) +return -EINVAL ; +if (!tpm2) +return -EINVAL ; + res =tpm2_parse_policies (&pay->policies, args[0 ].from); +if (res) +return res; +break ; +default :return -EINVAL ; }
Signature · 1 line
2.26.1
James Bottomley
maintainerPATCH v9 8/8 security: keys: trusted: implement counter/timer policy
In reply to James BottomleyThis is actually a generic policy allowing a range of comparisons against any value set in the TPM Clock, which includes things like the reset count, a monotonic millisecond count and the restart count. The most useful comparison is against the millisecond count for expiring keys. However, you have to remember that currently Linux doesn't try to sync the epoch timer with the TPM, so the expiration is actually measured in how long the TPM itself has been powered on ... the TPM timer doesn't count while the system is powered down. The millisecond counter is a u64 quantity found at offset 8 in the timer structure, and the <= comparision operand is 9, so a policy set to expire after the TPM has been up for 100 seconds would look like
0000016d00000000000f424000080009
Where 0x16d is the counter timer policy code and 0xf4240 is 100 000 in hex.
--- .../security/keys/trusted-encrypted.rst | 29 ++++++++++++++ include/linux/tpm.h | 1 + security/keys/trusted-keys/tpm2-policy.c | 40 ++++++++++++++++++- security/keys/trusted-keys/trusted_tpm2.c | 36 ++++++++++++++++- 4 files changed, 104 insertions(+), 2 deletions(-)
Documentation/security/keys/trusted-encrypted.rst +29 -0
@@ -241,3 +241,32 @@ about the usage can be found in the file Another new format 'enc32' has been defined in order to support encrypted keys with payload size of 32 bytes. This will initially be used for nvdimm security but may expand to other usages that require 32 bytes payload. + +Appendix +-------- + +TPM 2.0 Policies +---------------- + +The current TPM supports PCR lock policies as documented above and +CounterTimer policies which can be used to create expiring keys. One +caveat with expiring keys is that the TPM millisecond counter does not +update while a system is powered off and Linux does not sync the TPM +millisecond count with its internal clock, so the best you can expire +in is in terms of how long any given TPM has been powered on. (FIXME: +Linux should simply update the millisecond clock to the current number +of seconds past the epoch on boot). + +A CounterTimer policy is expressed in terms of length and offset +against the TPM clock structure (TPMS_TIME_INFO), which looks like the +packed structure:: + + struct tpms_time_info { + u64 uptime; /* time in ms since last start or reset */ + u64 clock; /* cumulative uptime in ms */ + u32 resetcount; /* numer of times the TPM has been reset */ + u32 restartcount; /* number of times the TPM has been restarted */ + u8 safe /* time was safely loaded from NVRam */ + }; + +The usual comparison for expiring keys is against clock, at offset 8.
include/linux/tpm.h +1 -0
@@ -233,6 +233,7 @@ enum tpm2_command_codes {TPM2_CC_PCR_EXTEND =0x0182 ,TPM2_CC_EVENT_SEQUENCE_COMPLETE =0x0185 ,TPM2_CC_HASH_SEQUENCE_START =0x0186 , +TPM2_CC_POLICY_PASSWORD =0x018c ,TPM2_CC_CREATE_LOADED =0x0191 , TPM2_CC_LAST = 0x0193, /* Spec 1.36 */ };
security/keys/trusted-keys/tpm2-policy.c +39 -1
@@ -197,7 +197,8 @@ int tpm2_generate_policy_digest(struct tpm2_policies *pols, len = *plen; } -crypto_shash_update (sdesc, policy, len); +if (len) +crypto_shash_update (sdesc, policy, len);/* now output the intermediate to the policydigest */ crypto_shash_final (sdesc, policydigest);
@@ -332,6 +333,16 @@ int tpm2_get_policy_session(struct tpm_chip *chip, struct tpm2_policies *pols,u32 cmd = pols->code[i];struct tpm_buf buf; +if (cmd =TPM2_CC_POLICY_AUTHVALUE ) +/* +* both PolicyAuthValue and PolicyPassword +* hash to the same thing, but one triggers +* HMAC authentication and the other simple +* authentication. Since we have no HMAC +* code, we're choosing the simple +*/ + cmd =TPM2_CC_POLICY_PASSWORD ; + rc =tpm_buf_init (&buf,TPM2_ST_NO_SESSIONS , cmd);if (rc)return rc;
@@ -352,8 +363,35 @@ int tpm2_get_policy_session(struct tpm_chip *chip, struct tpm2_policies *pols,tpm_buf_append (&buf, pols->policies[i], pols->len[i] - pols->hash_size);break ; + +case TPM2_CC_POLICY_COUNTER_TIMER : { +/* +* the format of this is the last two u16 +* quantities are the offset and operation +* respectively. The rest is operandB which +* must be zero padded in a hash digest +*/ +u16 opb_len = pols->len[i] -4 ; + +if (opb_len > pols->hash_size) +return -EINVAL ; + +tpm_buf_append_u16 (&buf, opb_len); +tpm_buf_append (&buf, pols->policies[i], opb_len); + +/* offset and operand*/ +tpm_buf_append (&buf, pols->policies[i] + opb_len,4 ); + failure ="Counter Timer" ; + +break ; + } +default : failure ="unknown policy" ; +if (pols->len[i]) +tpm_buf_append (&buf, pols->policies[i], + pols->len[i]); +break ; }
security/keys/trusted-keys/trusted_tpm2.c +35 -1
@@ -248,6 +248,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,u32 flags;int i;int rc; +static const int POLICY_SIZE =2 *PAGE_SIZE ;for (i =0 ; i <ARRAY_SIZE (tpm2_hash_map); i++) {if (options->hash = tpm2_hash_map[i].crypto_id) {
@@ -268,7 +269,7 @@ int tpm2_seal_trusted(struct tpm_chip *chip,/* 4 array len, 2 hash alg */ const int len =4 +2 + options->pcrinfo_len; - pols =kmalloc (sizeof (*pols) + len,GFP_KERNEL ); + pols =kmalloc (POLICY_SIZE ,GFP_KERNEL );if (!pols)return -ENOMEM ;
@@ -289,6 +290,39 @@ int tpm2_seal_trusted(struct tpm_chip *chip,return -EINVAL ; } +/* +* if we already have a policy, we have to add authorization +* to it. If we don't, we can simply follow the usual +* non-policy route. +*/ +if (options->blobauth_len !=0 && payload->policies) { +struct tpm2_policies *pols; +static u8 *scratch; +int i; +bool found = false; + + pols = payload->policies; + +/* make sure it's not already in policy */ +for (i =0 ; i < pols->count; i++) { +if (pols->code[i] =TPM2_CC_POLICY_AUTHVALUE ) { + found = true; + +break ; + } + } + +if (!found) { + i = pols->count++; + scratch = pols->policies[i -1 ] + pols->len[i -1 ]; + +/* the TPM2_PolicyPassword command has no payload */ + pols->policies[i] = scratch; + pols->len[i] =0 ; + pols->code[i] =TPM2_CC_POLICY_AUTHVALUE ; + } + } +if (payload->policies) { rc = tpm2_generate_policy_digest(payload->policies, options->hash,
Signature · 1 line
2.26.1
Quoted 23 lines · James Bottomley
This is a respin on v8 to make the encoder selectable and address David's comments. The trusted key part hasn't changed except to add a now necessary select for
ASN1_ENCODERto patch 4 and the changelog of patch 6 has been updated to correct the cut and paste error in the keyctl statement.General cover letter:
This patch updates the trusted key code to export keys in the ASN.1 format used by current TPM key tools (
openssl_tpm2_engineand openconnect). It also simplifies the use of policy with keys because the ASN.1 format is designed to carry a description of how to construct the policy, with the result that simple policies (like authorization and PCR locking) can now be constructed and used in the kernel, bringing the TPM 2.0 policy use into line with how TPM 1.2 works.The key format is designed to be compatible with our two openssl engine implementations as well as with the format used by openconnect. I've added seal/unseal to my engine so I can use it for interoperability testing and I'll later use this for sealed symmetric keys via engine:
I'm compiling now kernel with all series included.
Kind of checking if I could just take the whole series. Let see.
In all cases I want the style errors in 3/8 to be fixes with a helper but maybe better to hold before sending anything. Possibly that is all needed I'll just carve that patch myself.
Please don't do anything for the moment.
/Jarkko
Quoted 9 lines · Jarkko Sakkinen
I'm compiling now kernel with all series included.
Kind of checking if I could just take the whole series. Let see.
In all cases I want the style errors in 3/8 to be fixes with a helper but maybe better to hold before sending anything. Possibly that is all needed I'll just carve that patch myself.
Please don't do anything for the moment.
This is what I tried first (with the full series applied):
#!/bin/sh
die()
{
keyctl clear @u
./tpm2-flush --all-transient
exit $1
}KEYHANDLE=$(./tpm2-root-key || die 1)
KEYID=$(keyctl add trusted kmk "new 32 keyhandle=$KEYHANDLE hash=sha256" @u || die 1)
echo "$KEYID ($KEYHANDLE)"
keyctl pipe $KEYID > blob.hex || die 1
keyctl clear @u || die 1
echo "Import key from blob"
keyctl add trusted kmk "load cat blob.hex keyhandle=$KEYHANDLE" @u || die 1
die 0
Result:
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission denied
Any ideas what I might have done wrong? Have not tried auth value yet but afaik the above should fully test import and export.
Uses tpm2-scripts:
https://github.com/jsakkine-intel/tpm2-scripts
I'll probably move these to git.infradead.org because I don't like really like at all Github and my kernel tree is there anyway.
/Jarkko
James Bottomley
maintainerQuoted 38 lines · Jarkko Sakkinen
Quoted 11 lines · Jarkko Sakkinen
I'm compiling now kernel with all series included.
Kind of checking if I could just take the whole series. Let see.
In all cases I want the style errors in 3/8 to be fixes with a helper
but maybe better to hold before sending anything. Possibly that is all
needed I'll just carve that patch myself.Please don't do anything for the moment.
This is what I tried first (with the full series applied):
#!/bin/sh
die(){ keyctl clear @u ./tpm2-flush --all-transient exit $1 }KEYHANDLE=$(./tpm2-root-key || die 1) KEYID=$(keyctl add trusted kmk "new 32 keyhandle=$KEYHANDLE hash=sha256" @u || die 1)
echo "$KEYID ($KEYHANDLE)"
keyctl pipe $KEYID > blob.hex || die 1
keyctl clear @u || die 1echo "Import key from blob"
keyctl add trusted kmk "load
cat blob.hexkeyhandle=$KEYHANDLE" @u || die 1
Quoted 7 lines
die 0
Result:
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission denied
Well, it's clearly failing in keyctl pipe
I do confess to never having tested a volatile primary, but I just did so and it works for me. I will also add the keyhandle in the load isn't necessary, because it should be in the blob, but there should also be no harm (just tested).
However, I don't have keyctl_read_alloc in my tree, so it may be an incompatibility with another patch set. What's your base and what other patches do you have applied?
James
Quoted 12 lines
Any ideas what I might have done wrong? Have not tried auth value yet but afaik the above should fully test import and export.
Uses tpm2-scripts:
https://github.com/jsakkine-intel/tpm2-scripts
I'll probably move these to git.infradead.org because I don't like really like at all Github and my kernel tree is there anyway.
/Jarkko
Quoted 49 lines · James Bottomley
Quoted 38 lines · Jarkko Sakkinen
Quoted 11 lines · Jarkko Sakkinen
I'm compiling now kernel with all series included.
Kind of checking if I could just take the whole series. Let see.
In all cases I want the style errors in 3/8 to be fixes with a helper
but maybe better to hold before sending anything. Possibly that is all
needed I'll just carve that patch myself.Please don't do anything for the moment.
This is what I tried first (with the full series applied):
#!/bin/sh
die(){ keyctl clear @u ./tpm2-flush --all-transient exit $1 }KEYHANDLE=$(./tpm2-root-key || die 1) KEYID=$(keyctl add trusted kmk "new 32 keyhandle=$KEYHANDLE hash=sha256" @u || die 1)
echo "$KEYID ($KEYHANDLE)"
keyctl pipe $KEYID > blob.hex || die 1
keyctl clear @u || die 1echo "Import key from blob"
keyctl add trusted kmk "load
cat blob.hexkeyhandle=$KEYHANDLE" @u || die 1Quoted 7 lines
die 0
Result:
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission deniedWell, it's clearly failing in keyctl pipe
Quoted 8 lines
I do confess to never having tested a volatile primary, but I just did so and it works for me. I will also add the keyhandle in the load isn't necessary, because it should be in the blob, but there should also be no harm (just tested).
However, I don't have
keyctl_read_allocin my tree, so it may be an incompatibility with another patch set. What's your base and what other patches do you have applied?
http://git.infradead.org/users/jjs/linux-tpmdd.git
Or exactly:
git://git.infradead.org/users/jjs/linux-tpmdd.git (master)
/Jarkko
Quoted 59 lines · Jarkko Sakkinen
Quoted 49 lines · James Bottomley
Quoted 38 lines · Jarkko Sakkinen
Quoted 11 lines · Jarkko Sakkinen
I'm compiling now kernel with all series included.
Kind of checking if I could just take the whole series. Let see.
In all cases I want the style errors in 3/8 to be fixes with a helper
but maybe better to hold before sending anything. Possibly that is all
needed I'll just carve that patch myself.Please don't do anything for the moment.
This is what I tried first (with the full series applied):
#!/bin/sh
die(){ keyctl clear @u ./tpm2-flush --all-transient exit $1 }KEYHANDLE=$(./tpm2-root-key || die 1) KEYID=$(keyctl add trusted kmk "new 32 keyhandle=$KEYHANDLE hash=sha256" @u || die 1)
echo "$KEYID ($KEYHANDLE)"
keyctl pipe $KEYID > blob.hex || die 1
keyctl clear @u || die 1echo "Import key from blob"
keyctl add trusted kmk "load
cat blob.hexkeyhandle=$KEYHANDLE" @u || die 1Quoted 7 lines
die 0
Result:
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission deniedWell, it's clearly failing in keyctl pipe
Quoted 8 lines
I do confess to never having tested a volatile primary, but I just did so and it works for me. I will also add the keyhandle in the load isn't necessary, because it should be in the blob, but there should also be no harm (just tested).
However, I don't have
keyctl_read_allocin my tree, so it may be an incompatibility with another patch set. What's your base and what other patches do you have applied?
Quoted 8 lines
http://git.infradead.org/users/jjs/linux-tpmdd.git
Or exactly:
git://git.infradead.org/users/jjs/linux-tpmdd.git(master)/Jarkko
keyctl_read_alloc is in the userspace keyctl program, right?
git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/keyutils.git
James Bottomley
maintainerQuoted 2 lines · Jerry Snitselaar
Quoted 1 line · Jarkko Sakkinen
On Thu, May 14, 2020 at 08:44:23PM -0700, James Bottomley wrote:
[...]
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
However, I don't have
keyctl_read_allocin my tree, so it may be an incompatibility with another patch set. What's your base and what other patches do you have applied?http://git.infradead.org/users/jjs/linux-tpmdd.git
Or exactly:
Quoted 8 lines
Quoted 4 lines
git://git.infradead.org/users/jjs/linux-tpmdd.git(master)/Jarkko
keyctl_read_allocis in the userspace keyctl program, right?
git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/keyutils.git
Hm, right thanks! I just confirmed that linux-tpmdd.git with these patches applied still works for me. I'm using the keyctl in debian testing, which identifies itself as version 1.6-6
I'll try building from git.
James
Quoted 19 lines · James Bottomley
Quoted 2 lines · Jerry Snitselaar
Quoted 1 line · Jarkko Sakkinen
On Thu, May 14, 2020 at 08:44:23PM -0700, James Bottomley wrote:
[...]
Quoted 15 lines
Quoted 4 lines
Quoted 3 lines
However, I don't have
keyctl_read_allocin my tree, so it may be an incompatibility with another patch set. What's your base and what other patches do you have applied?Quoted 8 lines
http://git.infradead.org/users/jjs/linux-tpmdd.git
Or exactly:
git://git.infradead.org/users/jjs/linux-tpmdd.git(master)/Jarkko
keyctl_read_allocis in the userspace keyctl program, right?
Quoted 8 lines
Quoted 1 line
git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/keyutils.gitHm, right thanks! I just confirmed that linux-tpmdd.git with these patches applied still works for me. I'm using the keyctl in debian testing, which identifies itself as version 1.6-6
I'll try building from git.
James
Can you run the same exact command sequence that I posted?
/Jarkko
Quoted 64 lines · Jerry Snitselaar
Quoted 63 lines · Jarkko Sakkinen
Quoted 49 lines · James Bottomley
Quoted 38 lines · Jarkko Sakkinen
Quoted 11 lines · Jarkko Sakkinen
I'm compiling now kernel with all series included.
Kind of checking if I could just take the whole series. Let see.
In all cases I want the style errors in 3/8 to be fixes with a helper
but maybe better to hold before sending anything. Possibly that is all
needed I'll just carve that patch myself.Please don't do anything for the moment.
This is what I tried first (with the full series applied):
#!/bin/sh
die(){ keyctl clear @u ./tpm2-flush --all-transient exit $1 }KEYHANDLE=$(./tpm2-root-key || die 1) KEYID=$(keyctl add trusted kmk "new 32 keyhandle=$KEYHANDLE hash=sha256" @u || die 1)
echo "$KEYID ($KEYHANDLE)"
keyctl pipe $KEYID > blob.hex || die 1
keyctl clear @u || die 1echo "Import key from blob"
keyctl add trusted kmk "load
cat blob.hexkeyhandle=$KEYHANDLE" @uQuoted 1 line
Quoted 1 line
die 1
Quoted 7 lines
die 0
Result:
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission deniedWell, it's clearly failing in keyctl pipe
Quoted 8 lines
I do confess to never having tested a volatile primary, but I just did so and it works for me. I will also add the keyhandle in the load isn't necessary, because it should be in the blob, but there should also be no harm (just tested).
However, I don't have
keyctl_read_allocin my tree, so it may be an incompatibility with another patch set. What's your base and what other patches do you have applied?http://git.infradead.org/users/jjs/linux-tpmdd.git
Or exactly:
Quoted 8 lines
Quoted 4 lines
git://git.infradead.org/users/jjs/linux-tpmdd.git(master)/Jarkko
keyctl_read_allocis in the userspace keyctl program, right?
git://git.kernel.org/pub/scm/linux/kernel/git/dhowells/keyutils.git
Yes.
/Jarkko
Quoted 40 lines · Jarkko Sakkinen
Quoted 39 lines · James Bottomley
Quoted 38 lines · Jarkko Sakkinen
Quoted 11 lines · Jarkko Sakkinen
I'm compiling now kernel with all series included.
Kind of checking if I could just take the whole series. Let see.
In all cases I want the style errors in 3/8 to be fixes with a helper
but maybe better to hold before sending anything. Possibly that is all
needed I'll just carve that patch myself.Please don't do anything for the moment.
This is what I tried first (with the full series applied):
#!/bin/sh
die(){ keyctl clear @u ./tpm2-flush --all-transient exit $1 }KEYHANDLE=$(./tpm2-root-key || die 1) KEYID=$(keyctl add trusted kmk "new 32 keyhandle=$KEYHANDLE hash=sha256" @u || die 1)
echo "$KEYID ($KEYHANDLE)"
keyctl pipe $KEYID > blob.hex || die 1
keyctl clear @u || die 1echo "Import key from blob"
keyctl add trusted kmk "load
cat blob.hexkeyhandle=$KEYHANDLE" @u || die 1
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
die 0
Result:
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission denied
I get keyctl_read_alloc -EPERM when I 'sudo su' and try to play with keyctl print. If I 'sudo su -' and then try it works as expected. Also works for normal user.
Quoted 20 lines
Quoted 11 lines
Well, it's clearly failing in keyctl pipe
I do confess to never having tested a volatile primary, but I just did so and it works for me. I will also add the keyhandle in the load isn't necessary, because it should be in the blob, but there should also be no harm (just tested).
However, I don't have
keyctl_read_allocin my tree, so it may be an incompatibility with another patch set. What's your base and what other patches do you have applied?http://git.infradead.org/users/jjs/linux-tpmdd.git
Or exactly:
git://git.infradead.org/users/jjs/linux-tpmdd.git(master)/Jarkko
James Bottomley
maintainerQuoted 3 lines · Jerry Snitselaar
Quoted 2 lines · Jarkko Sakkinen
Quoted 1 line · James Bottomley
On Fri, 2020-05-15 at 05:22 +0300, Jarkko Sakkinen wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission deniedI get
keyctl_read_alloc-EPERMwhen I 'sudo su' and try to play with keyctl print.
If I 'sudo su -' and then try it works as expected. Also works for normal user.
OK, I confirm on debian as well. If I create a key as real root and then try to sudo su keyctl pipe it as an ordinary user, I get EPERM.
It smells like a cockup in real vs effective permissions somewhere in the keyctl handler.
James
Mimi Zohar
maintainerQuoted 14 lines · James Bottomley
Quoted 3 lines · Jerry Snitselaar
Quoted 2 lines · Jarkko Sakkinen
Quoted 1 line · James Bottomley
On Fri, 2020-05-15 at 05:22 +0300, Jarkko Sakkinen wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission deniedI get
keyctl_read_alloc-EPERMwhen I 'sudo su' and try to play with keyctl print.
If I 'sudo su -' and then try it works as expected. Also works for normal user.
Quoted 5 lines
OK, I confirm on debian as well. If I create a key as real root and then try to sudo su keyctl pipe it as an ordinary user, I get EPERM.
It smells like a cockup in real vs effective permissions somewhere in the keyctl handler.
Doing "sudo su -" has always been required. "su -" must set some environment variables. This isn't a problem for dracut as it is running as root.
Mimi
James Bottomley
maintainerQuoted 16 lines · James Bottomley
Quoted 3 lines · Jerry Snitselaar
Quoted 2 lines · Jarkko Sakkinen
Quoted 1 line · James Bottomley
On Fri, 2020-05-15 at 05:22 +0300, Jarkko Sakkinen wrote:
[...]
Quoted 9 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission deniedI get
keyctl_read_alloc-EPERMwhen I 'sudo su' and try to play with
keyctl print.
If I 'sudo su -' and then try it works as expected. Also works for normal user.
Quoted 5 lines
OK, I confirm on debian as well. If I create a key as real root and then try to sudo su keyctl pipe it as an ordinary user, I get EPERM.
It smells like a cockup in real vs effective permissions somewhere in the keyctl handler.
OK, so the problem is
sudo keyctl list @s
Still shows the session keys of the previous user
that causes sudo keyctl show on a root owned key to fail the is_key_possessed() check, returning -EACCESS which gets translated to EPERM
if you do
sudo su -
Then keyctl list @s shows the root session keyring and everything works
I think that means the solution is not to run the smoke test under sudo but to do sudo -s and then run it.
James
Quoted 7 lines · James Bottomley
I think that means the solution is not to run the smoke test under sudo but to do sudo -s and then run it.
James
How about "sudo -i":
Mehmet
James Bottomley
maintainerQuoted 7 lines · Mehmet
Quoted 7 lines
On May 15, 2020, at 4:10 PM, James Bottomley <James.Bottomley@hanse npartnership.com> wrote:
I think that means the solution is not to run the smoke test under sudo
but to do sudo -s and then run it.
Quoted 6 lines
Quoted 1 line
James
How about "sudo -i":
https://askubuntu.com/questions/376199/sudo-su-vs-sudo-i-vs-sudo-bin-
bash-when-does-it-matter-which-is-used
Actually, no that doesn't work either:
jejb@testdeb> sudo -i keyctl list @s
1 key in keyring:
1041514063: ---lswrv 1000 65534 keyring: _uid.1000
I suspect this might be a very subtle bug to do with when you get a new session keyring.
James
James Bottomley
maintainerQuoted 15 lines · James Bottomley
Quoted 7 lines · Mehmet
Quoted 7 lines
On May 15, 2020, at 4:10 PM, James Bottomley <James.Bottomley@han se
npartnership.com> wrote:I think that means the solution is not to run the smoke test under sudo but to do sudo -s and then run it.
Quoted 6 lines
Quoted 1 line
James
How about "sudo -i":
https://askubuntu.com/questions/376199/sudo-su-vs-sudo-i-vs-sudo-
bin-bash-when-does-it-matter-which-is-used
Quoted 8 lines
Actually, no that doesn't work either:
jejb@testdeb> sudo -i keyctl list @s
1 key in keyring:
1041514063: ---lswrv 1000 65534 keyring: _uid.1000I suspect this might be a very subtle bug to do with when you get a new session keyring.
It turns out to be incredibly subtle, but I'm not sure if it's a bug or not. the util-linux sudo like commands have special pam profiles
/etc/pam.d/su-l/etc/pam.d/runuser-l
These special profiles call pam_keyinit.so with flags to install a new session keyring. sudo doesn't have this, so it never, on its own, even when called with -i or -s, installs a new session keyring. This does strike me as a bizarre oversight which leads directly to this weird keyctl pipe behaviour.
I'm also not sure the keyctl pipe behaviour is correct: why should keyctl pipe deny access to root to read a key just because it's in a different session keyring? It defintely seems intentional, because if I create a key as a non root user and try to print it by id as root I get EPERM.
The weirdest behaviour, though seems to be keyctl: keyctl add ... @u will add to the session keyrings of the actual uid regardless of what session keyring the creator actually has, which is why keyctl add ... @u works under sudo but you get EPERM back trying to pipe it by id.
James
Quoted 43 lines · James Bottomley
Quoted 15 lines · James Bottomley
Quoted 7 lines · Mehmet
Quoted 7 lines
On May 15, 2020, at 4:10 PM, James Bottomley <James.Bottomley@han se
npartnership.com> wrote:I think that means the solution is not to run the smoke test under sudo but to do sudo -s and then run it.
Quoted 6 lines
Quoted 1 line
James
How about "sudo -i":
https://askubuntu.com/questions/376199/sudo-su-vs-sudo-i-vs-sudo-
bin-bash-when-does-it-matter-which-is-usedQuoted 8 lines
Actually, no that doesn't work either:
jejb@testdeb> sudo -i keyctl list @s
1 key in keyring:
1041514063: ---lswrv 1000 65534 keyring: _uid.1000I suspect this might be a very subtle bug to do with when you get a new session keyring.
It turns out to be incredibly subtle, but I'm not sure if it's a bug or not. the util-linux sudo like commands have special pam profiles
/etc/pam.d/su-l/etc/pam.d/runuser-lThese special profiles call pam_keyinit.so with flags to install a new session keyring. sudo doesn't have this, so it never, on its own, even when called with -i or -s, installs a new session keyring. This does strike me as a bizarre oversight which leads directly to this weird keyctl pipe behaviour.
I'm also not sure the keyctl pipe behaviour is correct: why should keyctl pipe deny access to root to read a key just because it's in a different session keyring? It defintely seems intentional, because if I create a key as a non root user and try to print it by id as root I get EPERM.
Quoted 6 lines
The weirdest behaviour, though seems to be keyctl: keyctl add ... @u will add to the session keyrings of the actual uid regardless of what session keyring the creator actually has, which is why keyctl add ... @u works under sudo but you get EPERM back trying to pipe it by id.
James
I think I construct a low priority bug to kernel bugzilla and link these from l.k.o. Thanks for digging this all up.
/Jarkko
T24gRnJpLCAyMDIwLTA1LTE1IGF0IDEzOjEwIC0wNzAwLCBKYW1lcyBCb3R0b21sZXkgd3JvdGU6 DQo+IE9uIEZyaSwgMjAyMC0wNS0xNSBhdCAxMjozNCAtMDcwMCwgSmFtZXMgQm90dG9tbGV5IHdy b3RlOg0KPiA+IE9uIEZyaSwgMjAyMC0wNS0xNSBhdCAxMjoxNyAtMDcwMCwgSmVycnkgU25pdHNl bGFhciB3cm90ZToNCj4gPiA+IE9uIEZyaSBNYXkgMTUgMjAsIEphcmtrbyBTYWtraW5lbiB3cm90 ZToNCj4gPiA+ID4gT24gVGh1LCBNYXkgMTQsIDIwMjAgYXQgMDg6NDQ6MjNQTSAtMDcwMCwgSmFt ZXMgQm90dG9tbGV5IHdyb3RlOg0KPiA+ID4gPiA+IE9uIEZyaSwgMjAyMC0wNS0xNSBhdCAwNToy MiArMDMwMCwgSmFya2tvIFNha2tpbmVuIHdyb3RlOg0KPiA+IA0KPiA+IFsuLi5dDQo+ID4gPiA+ ID4gPiBzdWRvIC4va2V5Y3RsLXNtb2tlLnNoDQo+ID4gPiA+ID4gPiA1NjYyMDEwNTMgKDB4ODAw MDAwMDApDQo+ID4gPiA+ID4gPiBrZXljdGxfcmVhZF9hbGxvYzogUGVybWlzc2lvbiBkZW5pZWQN Cj4gPiA+IA0KPiA+ID4gSSBnZXQga2V5Y3RsX3JlYWRfYWxsb2MgLUVQRVJNIHdoZW4gSSAnc3Vk byBzdScgYW5kIHRyeSB0byBwbGF5DQo+ID4gPiB3aXRoDQo+ID4gPiBrZXljdGwgcHJpbnQuDQo+ ID4gPiBJZiBJICdzdWRvIHN1IC0nIGFuZCB0aGVuIHRyeSBpdCB3b3JrcyBhcyBleHBlY3RlZC4g QWxzbyB3b3JrcyBmb3INCj4gPiA+IG5vcm1hbCB1c2VyLg0KPiA+IA0KPiA+IE9LLCBJIGNvbmZp cm0gb24gZGViaWFuIGFzIHdlbGwuICBJZiBJIGNyZWF0ZSBhIGtleSBhcyByZWFsIHJvb3QgYW5k DQo+ID4gdGhlbiB0cnkgdG8gc3VkbyBzdSBrZXljdGwgcGlwZSBpdCBhcyBhbiBvcmRpbmFyeSB1 c2VyLCBJIGdldCBFUEVSTS4NCj4gPiANCj4gPiBJdCBzbWVsbHMgbGlrZSBhIGNvY2t1cCBpbiBy ZWFsIHZzIGVmZmVjdGl2ZSBwZXJtaXNzaW9ucyBzb21ld2hlcmUgaW4NCj4gPiB0aGUga2V5Y3Rs IGhhbmRsZXIuDQo+IA0KPiBPSywgc28gdGhlIHByb2JsZW0gaXMNCj4gDQo+IHN1ZG8ga2V5Y3Rs IGxpc3QgQHMNCj4gDQo+IFN0aWxsIHNob3dzIHRoZSBzZXNzaW9uIGtleXMgb2YgdGhlIHByZXZp b3VzIHVzZXINCj4gDQo+IHRoYXQgY2F1c2VzIHN1ZG8ga2V5Y3RsIHNob3cgb24gYSByb290IG93 bmVkIGtleSB0byBmYWlsIHRoZQ0KPiBpc19rZXlfcG9zc2Vzc2VkKCkgY2hlY2ssIHJldHVybmlu ZyAtRUFDQ0VTUyB3aGljaCBnZXRzIHRyYW5zbGF0ZWQgdG8NCj4gRVBFUk0NCj4gDQo+IGlmIHlv dSBkbw0KPiANCj4gc3VkbyBzdSAtDQo+IA0KPiBUaGVuIGtleWN0bCBsaXN0IEBzIHNob3dzIHRo ZSByb290IHNlc3Npb24ga2V5cmluZyBhbmQgZXZlcnl0aGluZyB3b3Jrcw0KPiANCj4gSSB0aGlu ayB0aGF0IG1lYW5zIHRoZSBzb2x1dGlvbiBpcyBub3QgdG8gcnVuIHRoZSBzbW9rZSB0ZXN0IHVu ZGVyIHN1ZG8NCj4gYnV0IHRvIGRvIHN1ZG8gLXMgYW5kIHRoZW4gcnVuIGl0Lg0KDQpSaWdodCwg bWFrZXMgc2Vuc2UgYW5kIEkgY2FuIGFsc28gY29uZmlybSB0aGlzIGluIG15IGVudmlyb25tZW50 Lg0KVGhhbmtzIQ0KDQovSmFya2tvDQo
Quoted 6 lines · Jerry Snitselaar
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
sudo ./keyctl-smoke.sh
566201053 (0x80000000)keyctl_read_alloc: Permission deniedI get
keyctl_read_alloc-EPERMwhen I 'sudo su' and try to play with keyctl print. If I 'sudo su -' and then try it works as expected. Also works for normal user.
Can confirm these results with NUC7PJYH (Gemini Lake).
/Jarkko