Add CA enforcement keyring restrictions
Subsystems (8)
Eric Snowberg
reviewerv2 cover Add CA enforcement keyring restrictions
Prior to the introduction of the machine keyring, most distros simply allowed all keys contained within the platform keyring to be used for both kernel and module verification. This was done by an out of tree patch. Some distros took it even further and loaded all these keys into the secondary trusted keyring. This also allowed the system owner to add their own key for IMA usage.
Each distro contains similar documentation on how to sign kernel modules and enroll the key into the MOK. The process is fairly straightforward. With the introduction of the machine keyring, the process remains basically the same, without the need for any out of tree patches.
The machine keyring allowed distros to eliminate the out of tree patches for kernel module signing. However, it falls short in allowing the end user to add their own keys for IMA. Currently the machine keyring can not be used as another trust anchor for adding keys to the ima keyring, since CA enforcement does not currently exist. This would expand the current integrity gap. The IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY Kconfig states that keys may be added to the ima keyrings if the key is validly signed by a CA cert in the system built-in or secondary trusted keyring. Currently there is not code that enforces the "validly signed" CA cert. Any key in the builtin or secondary keyring can be used. Also, the definition of "validly signed" has never been stated, leaving this up to multiple interpretations.
To allow IMA to be enabled with the machine keyring, this series introduces enforcement of key usage in the certificate. This series also defines a "validly signed" CA cert and applies the same definition across all kernel keyrings.
The machine keyring shares similarities with both the builtin and secondary keyrings. Similar to the builtin, no keys may be added to the machine keyring following boot. The secondary keyring allows user provided keys to be added following boot; however, a previously enrolled kernel key must vouch for the key before it may be included. The system owner may include their own keys into the machine keyring prior to boot. If the end-user is not the system owner, they may not add their own keys to the machine keyring.
The machine keyring is only populated when Secure Boot is enabled. A system owner has the ability to control the entire Secure Boot keychain (PK, KEK, DB, and DBX). The system owner can also turn Secure Boot off. With this control, they may use insert-sys-cert to include their own key and re-sign their kernel and have it boot. The system owner also has control to include or exclude MOK keys. This series does not try to interpret how a system owner has configured their machine. If the system owner has taken the steps to add their own MOK keys, they will be included in the machine keyring and used for verification, exactly the same way as keys contained in the builtin and secondary keyrings. Since the system owner has the ability to add keys before booting to either the machine or builtin keyrings, it is viewed as inconsequential if the key originated from one or the other.
This series introduces two different ways to configure the machine keyring. By default, nothing changes and all MOK keys are loaded into it. Whenever a "validly signed" key is found within the machine, builtin, or secondary, a flag indicating this is stored in the public key struct. The other option is if the new Kconfig INTEGRITY_CA_MACHINE_KEYRING is enabled, only "validly signed" CA certs will be loaded into the machine keyring. All remaining MOK keys will be loaded into the platform keyring.
A "validly signed" CA cert shall be defined as any X509 certificate that is self signed, contains the keyCertSign key usage and has the CA bit set to true.
With this series applied, "validly signed" CA enforcement is in place whenever IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY is enabled. Meaning, before any key can be included into the ima keyring, it must be vouched for by a "validly signed" CA key contained within the builtin, secondary, or machine keyrings.
IMA allows userspace applications to be signed. The enduser may sign their own application, however they may also want to use an application provided by a 3rd party. The entity building the kernel, may not be the same entity building the userspace program. The system owner may also be a third entity. If the system owner trusts the entity building the userspace program, they will include their public key within the MOK. This key would be used to sign the key added to the ima keyring. Not all 3rd party userspace providers have the capability to properly manage a root CA. Some may outsource to a different code signing provider. Many code signing providers use Intermediate CA certificates. Therefore, this series also includes support for Intermediate CA certificates. If a "validly signed" CA can vouch for an Intermediate CA, and it contains the keyCertSign key usage, it will also be flagged as being a "validly signed" CA. The Intermediate CA certificates could be loaded into the secondary keyring following boot. Afterwards, CA enforcement is maintained for any key added to the ima keyring.
This series could be broken up into 3 different parts. The first two patches could be taken now. They solve current issues that will be triggered by the build robots. Patches 3-8 add CA enforcement for the ima keyring. Patches 9-10 restrict the machine keyring to only load CA certs into it. Patches 9-10 require all the previous patches since Intermediate CA enforcement is required.
Eric Snowberg (10):
KEYS: Create static version of public_key_verify_signature
KEYS: Add missing function documentation
KEYS: X.509: Parse Basic Constraints for CA
KEYS: X.509: Parse Key Usage
KEYS: Introduce a CA endorsed flag
KEYS: Introduce keyring restriction that validates ca trust
KEYS: X.509: Flag Intermediate CA certs as endorsed
integrity: Use root of trust signature restriction
KEYS: CA link restriction
integrity: restrict INTEGRITY_KEYRING_MACHINE to restrict_link_by_ca
certs/system_keyring.c | 30 ++++++++- crypto/asymmetric_keys/restrict.c | 81 +++++++++++++++++++++++ crypto/asymmetric_keys/x509_cert_parser.c | 31 +++++++++ crypto/asymmetric_keys/x509_parser.h | 2 + crypto/asymmetric_keys/x509_public_key.c | 16 +++++ include/crypto/public_key.h | 30 +++++++++ include/keys/system_keyring.h | 12 +++- include/linux/ima.h | 11 +++ include/linux/key-type.h | 3 + include/linux/key.h | 2 + security/integrity/Kconfig | 11 ++- security/integrity/digsig.c | 12 ++-- security/integrity/ima/Kconfig | 6 +- security/keys/key.c | 13 ++++ 14 files changed, 249 insertions(+), 11 deletions(-)
base-commit: 76dcd734eca23168cb008912c0f69ff408905235
Signature · 1 line
2.27.0
Eric Snowberg
reviewerPATCH v2 1/10 KEYS: Create static version of public_key_verify_signature
The kernel test robot reports undefined reference to public_key_verify_signature when CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE is not defined. Create a static version in this case and return -EINVAL.
--- include/crypto/public_key.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/crypto/public_key.h +9 -0
@@ -80,7 +80,16 @@ extern int create_signature(struct kernel_pkey_params *, const void *, void *);extern int verify_signature (const struct key *,const struct public_key_signature *); +#if IS_REACHABLE (CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE )int public_key_verify_signature (const struct public_key *pkey,const struct public_key_signature *sig); +#else +static inline +int public_key_verify_signature (const struct public_key *pkey, +const struct public_key_signature *sig) +{ +return -EINVAL ; +} +#endif #endif /* _LINUX_PUBLIC_KEY_H */
Signature · 1 line
2.27.0
Re: [PATCH v2 01/10] KEYS: Create static version of public_key_verify_signature
Eric Snowberg
reviewerPATCH v2 2/10 KEYS: Add missing function documentation
In reply to Eric SnowbergCompiling with 'W=1' results in warnings that 'Function parameter or member not described'
Add the missing parameters for restrict_link_by_builtin_and_secondary_trusted and restrict_link_to_builtin_trusted.
Use /* instead of /** for get_builtin_and_secondary_restriction, since it is a static function.
--- certs/system_keyring.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
certs/system_keyring.c +10 -2
@@ -33,7 +33,11 @@ extern __initconst const unsigned long system_certificate_list_size;extern __initconst const unsigned long module_cert_size;/** -* restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA +* restrict_link_by_builtin_trusted - Restrict keyring addition by built in CA +* @dest_keyring: Keyring being linked to. +* @type: The type of key being added. +* @payload: The payload of the new key. +* @restriction_key: A ring of keys that can be used to vouch for the new cert. * * Restrict the addition of keys into a keyring based on the key-to-be-added * being vouched for by a key in the built in system keyring.
@@ -51,6 +55,10 @@ int restrict_link_by_builtin_trusted(struct key *dest_keyring,/** * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring * addition by both builtin and secondary keyrings +* @dest_keyring: Keyring being linked to. +* @type: The type of key being added. +* @payload: The payload of the new key. +* @restrict_key: A ring of keys that can be used to vouch for the new cert. * * Restrict the addition of keys into a keyring based on the key-to-be-added * being vouched for by a key in either the built-in or the secondary system
@@ -75,7 +83,7 @@ int restrict_link_by_builtin_and_secondary_trusted( secondary_trusted_keys); } -/** +/* * Allocate a struct key_restriction for the "builtin and secondary trust" * keyring. Only for use in system_trusted_keyring_init(). */
Signature · 1 line
2.27.0
Re: [PATCH v2 02/10] KEYS: Add missing function documentation
Hi Eric,
Quoted 20 lines
Compiling with 'W=1' results in warnings that 'Function parameter or member not described'
Add the missing parameters for
restrict_link_by_builtin_and_secondary_trustedandrestrict_link_to_builtin_trusted.Use /* instead of /** for
get_builtin_and_secondary_restriction, since it is a static function.Signed-off-by Eric Snowberg <[email protected]>--- certs/system_keyring.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)certs/system_keyring.c +0
-0
certs/system_keyring.c extern __initconst const unsigned long system_certificate_list_size; · line 36
Quoted 5 lines
certs/system_keyring.c +1
-1@@ -33,7 +33,11 @@ extern __initconst const unsigned long system_certificate_list_size;extern __initconst const unsigned long module_cert_size;/** -* restrict_link_to_builtin_trusted - Restrict keyring addition by built in CA +* restrict_link_by_builtin_trusted - Restrict keyring addition by built in CA
Hm, wrong function name restrict_link_to_builtin_trusted brought by:d3bfe84129f6 ("certs: Add a secondary system keyring that can be added to dynamically")
certs/system_keyring.c int restrict_link_by_builtin_trusted(struct key *dest_keyring, · line 57
Quoted 11 lines
certs/system_keyring.c +4
-0+* @dest_keyring: Keyring being linked to. +* @type: The type of key being added. +* @payload: The payload of the new key. +* @restriction_key: A ring of keys that can be used to vouch for the new cert. * * Restrict the addition of keys into a keyring based on the key-to-be-added * being vouched for by a key in the built in system keyring. @@ -51,6 +55,10 @@ int restrict_link_by_builtin_trusted(struct key *dest_keyring,/** * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring * addition by both builtin and secondary keyrings
nit: missing dot at the end of the sentence (maybe can be fixed before merge).
Obviously correct.
Kind regards,
Petr
Quoted 16 lines
certs/system_keyring.c +5
-1+* @dest_keyring: Keyring being linked to. +* @type: The type of key being added. +* @payload: The payload of the new key. +* @restrict_key: A ring of keys that can be used to vouch for the new cert. * * Restrict the addition of keys into a keyring based on the key-to-be-added * being vouched for by a key in either the built-in or the secondary system @@ -75,7 +83,7 @@ int restrict_link_by_builtin_and_secondary_trusted( secondary_trusted_keys); } -/** +/* * Allocate a struct key_restriction for the "builtin and secondary trust" * keyring. Only for use in system_trusted_keyring_init(). */
Eric Snowberg
reviewerPATCH v2 3/10 KEYS: X.509: Parse Basic Constraints for CA
In reply to Eric SnowbergParse the X.509 Basic Constraints. The basic constraints extension identifies whether the subject of the certificate is a CA.
BasicConstraints ::= SEQUENCE {
cA BOOLEAN DEFAULT FALSE,
pathLenConstraint INTEGER (0..MAX) OPTIONAL }
If the CA is true, store it in the x509_certificate. This will be used in a follow on patch that requires knowing if the public key is a CA.
--- crypto/asymmetric_keys/x509_cert_parser.c | 9 +++++++++ crypto/asymmetric_keys/x509_parser.h | 1 + 2 files changed, 10 insertions(+)
crypto/asymmetric_keys/x509_cert_parser.c +9 -0
@@ -586,6 +586,15 @@ int x509_process_extension(void *context, size_t hdrlen,return 0 ; } +if (ctx->last_oid == OID_basicConstraints) { +if (vlen <2 || v[0 ] != (ASN1_CONS_BIT |ASN1_SEQ )) +return -EBADMSG ; +if (v[1 ] != vlen -2 ) +return -EBADMSG ; +if (vlen >=4 && v[1 ] !=0 && v[2 ] ==ASN1_BOOL && v[3 ] ==1 ) + ctx->cert->root_ca = true; + } +return 0 ; }
crypto/asymmetric_keys/x509_parser.h +1 -0
@@ -38,6 +38,7 @@ struct x509_certificate {bool self_signed;/* T if self-signed (check unsupported_sig too) */ bool unsupported_sig;/* T if signature uses unsupported crypto */ bool blacklisted; +bool root_ca;/* T if basic constraints CA is set */ };/*
Signature · 1 line
2.27.0
Eric Snowberg
reviewerPATCH v2 4/10 KEYS: X.509: Parse Key Usage
In reply to Eric SnowbergParse the X.509 Key Usage. The key usage extension defines the purpose of the key contained in the certificate.
id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
KeyUsage ::= BIT STRING {
digitalSignature (0),
contentCommitment (1),
keyEncipherment (2),
dataEncipherment (3),
keyAgreement (4),
keyCertSign (5),
cRLSign (6),
encipherOnly (7),
decipherOnly (8) }If the keyCertSign is set, store it in the x509_certificate structure. This will be used in a follow on patch that requires knowing the certificate key usage type.
--- crypto/asymmetric_keys/x509_cert_parser.c | 22 ++++++++++++++++++++++ crypto/asymmetric_keys/x509_parser.h | 1 + 2 files changed, 23 insertions(+)
crypto/asymmetric_keys/x509_cert_parser.c +22 -0
@@ -579,6 +579,28 @@ int x509_process_extension(void *context, size_t hdrlen,return 0 ; } +if (ctx->last_oid == OID_keyUsage) { +/* +* Get hold of the keyUsage bit string to validate keyCertSign +* v[1] is the encoding size +* (Expect either 0x02 or 0x03, making it 1 or 2 bytes) +* v[2] is the number of unused bits in the bit string +* (If >= 3 keyCertSign is missing) +* v[3] and possibly v[4] contain the bit string +* 0x04 is where KeyCertSign lands in this bit string (from +* RFC 5280 4.2.1.3) +*/ +if (v[0 ] !=ASN1_BTS ) +return -EBADMSG ; +if (vlen <4 ) +return -EBADMSG ; +if (v[1 ] ==0x02 && v[2 ] <=2 && (v[3 ] &0x04 )) + ctx->cert->kcs_set = true; +else if (vlen >4 && v[1 ] ==0x03 && (v[3 ] &0x04 )) + ctx->cert->kcs_set = true; +return 0 ; + } +if (ctx->last_oid == OID_authorityKeyIdentifier) {/* Get hold of the CA key fingerprint */ ctx->raw_akid = v;
crypto/asymmetric_keys/x509_parser.h +1 -0
@@ -39,6 +39,7 @@ struct x509_certificate {bool unsupported_sig;/* T if signature uses unsupported crypto */ bool blacklisted;bool root_ca;/* T if basic constraints CA is set */ +bool kcs_set;/* T if keyCertSign is set */ };/*
Signature · 1 line
2.27.0
Eric Snowberg
reviewerPATCH v2 5/10 KEYS: Introduce a CA endorsed flag
In reply to Eric SnowbergSome subsystems are interested in knowing if a key has been endorsed as a Certificate Authority (CA). From the data contained in struct key, it is not possible to make this determination after the key parsing is complete. Introduce a new Endorsed Certificate Authority flag called KEY_FLAG_ECA.
The first type of key to use this is X.509. When a X.509 certificate is self signed, has the keyCertSign Key Usage set and contains the CA bit set, this new flag is set. In the future, other usage fields could be added as flags, i.e. digitialSignature.
--- crypto/asymmetric_keys/x509_public_key.c | 3 +++ include/linux/key-type.h | 2 ++ include/linux/key.h | 2 ++ security/keys/key.c | 8 ++++++++ 4 files changed, 15 insertions(+)
crypto/asymmetric_keys/x509_public_key.c +3 -0
@@ -208,6 +208,9 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) goto error_free_kids; } +if (cert->kcs_set && cert->self_signed && cert->root_ca) + prep->payload_flags |=KEY_ALLOC_PECA ; +/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner); prep->payload.data[asym_subtype] = &public_key_subtype;
include/linux/key-type.h +2 -0
@@ -36,6 +36,8 @@ struct key_preparsed_payload {size_t datalen;/* Raw datalen */ size_t quotalen;/* Quota length for proposed payload */ time64_t expiry;/* Expiry time of key */ +unsigned int payload_flags;/* Proposed payload flags */ +#define KEY_ALLOC_PECA 0x0001/* Proposed Endorsed CA (ECA) key */ } __randomize_layout;typedef int (*request_key_actor_t )(struct key *auth_key,void *aux);
include/linux/key.h +2 -0
@@ -236,6 +236,7 @@ struct key {#define KEY_FLAG_ROOT_CAN_INVAL 7/* set if key can be invalidated by root without permission */ #define KEY_FLAG_KEEP 8/* set if key should not be removed */ #define KEY_FLAG_UID_KEYRING 9/* set if key is a user or user session keyring */ +#define KEY_FLAG_ECA 10/* set if key is an Endorsed CA key */ /* the key type and key description string * - the desc is used to match a key against search criteria
@@ -296,6 +297,7 @@ extern struct key *key_alloc(struct key_type *type,#define KEY_ALLOC_BYPASS_RESTRICTION 0x0008/* Override the check on restricted keyrings */ #define KEY_ALLOC_UID_KEYRING 0x0010/* allocating a user or user session keyring */ #define KEY_ALLOC_SET_KEEP 0x0020/* Set the KEEP flag on the key/keyring */ +#define KEY_ALLOC_ECA 0x0040/* Add Endorsed CA key */ extern void key_revoke (struct key *key);extern void key_invalidate (struct key *key);
security/keys/key.c +8 -0
@@ -305,6 +305,8 @@ struct key *key_alloc(struct key_type *type, const char *desc, key->flags |=1 <<KEY_FLAG_UID_KEYRING ;if (flags &KEY_ALLOC_SET_KEEP ) key->flags |=1 <<KEY_FLAG_KEEP ; +if (flags &KEY_ALLOC_ECA ) + key->flags |=1 <<KEY_FLAG_ECA ;#ifdef KEY_DEBUGGING key->magic =KEY_DEBUG_MAGIC ;
@@ -929,6 +931,12 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, perm |=KEY_POS_WRITE ; } +/* Only allow KEY_ALLOC_ECA flag to be set by preparser contents */ +if (prep.payload_flags &KEY_ALLOC_PECA ) + flags |=KEY_ALLOC_ECA ; +else + flags &= ~KEY_ALLOC_ECA ; +/* allocate a new key */ key =key_alloc (index_key.type, index_key.description, cred->fsuid, cred->fsgid, cred, perm, flags,NULL );
Signature · 1 line
2.27.0
Eric Snowberg
reviewerPATCH v2 6/10 KEYS: Introduce keyring restriction that validates ca trust
In reply to Eric SnowbergThe current keyring restrictions validate if a key can be vouched for by another key already contained in a keyring. Add a new restriction called restrict_link_by_ca_and_signature that both vouches for the new key and validates the vouching key is an endorsed certificate authority.
Two new system keyring restrictions are added to use restrict_link_by_ca_and_signature. The first restriction called restrict_link_by_ca_builtin_trusted uses the builtin_trusted_keys as the restricted keyring. The second system keyring restriction called restrict_link_by_ca_builtin_and_secondary_trusted uses the secondary_trusted_keys as the restricted keyring. Should the machine keyring be defined, it shall be validated too, since it is linked to the secondary_trusted_keys keyring.
--- certs/system_keyring.c | 18 ++++++++++++++ crypto/asymmetric_keys/restrict.c | 41 +++++++++++++++++++++++++++++++ include/crypto/public_key.h | 5 ++++ include/keys/system_keyring.h | 12 ++++++++- 4 files changed, 75 insertions(+), 1 deletion(-)
certs/system_keyring.c +18 -0
@@ -51,6 +51,14 @@ int restrict_link_by_builtin_trusted(struct key *dest_keyring, builtin_trusted_keys); } +int restrict_link_by_ca_builtin_trusted (struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *unused) +{ +return restrict_link_by_ca_and_signature (dest_keyring, type, payload, + builtin_trusted_keys); +}#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING /** * restrict_link_by_builtin_and_secondary_trusted - Restrict keyring
@@ -83,6 +91,16 @@ int restrict_link_by_builtin_and_secondary_trusted( secondary_trusted_keys); } +int restrict_link_by_ca_builtin_and_secondary_trusted ( +struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *unused) +{ +return restrict_link_by_ca_and_signature (dest_keyring, type, payload, + secondary_trusted_keys); +} +/* * Allocate a struct key_restriction for the "builtin and secondary trust" * keyring. Only for use in system_trusted_keyring_init().
crypto/asymmetric_keys/restrict.c +41 -0
47 lines not on this page · raw message
include/crypto/public_key.h +5 -0
@@ -71,6 +71,11 @@ extern int restrict_link_by_key_or_keyring_chain(struct key *trust_keyring,const union key_payload *payload, struct key *trusted); +extern int restrict_link_by_ca_and_signature (struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *unused); +extern int query_asymmetric_key (const struct kernel_pkey_params *,struct kernel_pkey_query *);
include/keys/system_keyring.h +11 -1
@@ -24,9 +24,13 @@ extern int restrict_link_by_builtin_trusted(struct key *keyring,const union key_payload *payload, struct key *restriction_key);extern __init int load_module_cert (struct key *keyring); - +extern int restrict_link_by_ca_builtin_trusted (struct key *keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *unused);#else #define restrict_link_by_builtin_trusted restrict_link_reject +#define restrict_link_by_ca_builtin_trusted restrict_link_rejectstatic inline __init int load_module_cert (struct key *keyring) {
@@ -41,8 +45,14 @@ extern int restrict_link_by_builtin_and_secondary_trusted(const struct key_type *type, const union key_payload *payload, struct key *restriction_key); +extern int restrict_link_by_ca_builtin_and_secondary_trusted ( +struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *restrict_key);#else #define restrict_link_by_builtin_and_secondary_trusted restrict_link_by_builtin_trusted +#define restrict_link_by_ca_builtin_and_secondary_trusted restrict_link_by_builtin_trusted#endif #ifdef CONFIG_INTEGRITY_MACHINE_KEYRING
Signature · 1 line
2.27.0
Eric Snowberg
reviewerPATCH v2 7/10 KEYS: X.509: Flag Intermediate CA certs as endorsed
In reply to Eric SnowbergCurrently X.509 Intermediate CA certs do not have the endorsed CA (KEY_FLAG_ECA) set. Allow intermediate CA certs to be added. Requirements for an intermediate CA include: Usage extension defined as keyCertSign, Basic Constrains for CA is false, and Intermediate CA cert is signed by a current endorsed CA.
--- crypto/asymmetric_keys/x509_public_key.c | 14 ++++++++++++-- include/linux/ima.h | 11 +++++++++++ include/linux/key-type.h | 1 + security/keys/key.c | 5 +++++ 4 files changed, 29 insertions(+), 2 deletions(-)
crypto/asymmetric_keys/x509_public_key.c +12 -2
@@ -208,8 +208,18 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) goto error_free_kids; } -if (cert->kcs_set && cert->self_signed && cert->root_ca) - prep->payload_flags |=KEY_ALLOC_PECA ; +if (cert->kcs_set) { +if (cert->self_signed && cert->root_ca) + prep->payload_flags |=KEY_ALLOC_PECA ; +/* +* In this case it could be an Intermediate CA. Set +* KEY_MAYBE_PECA for now. If the restriction check +* passes later, the key will be allocated with the +* correct CA flag +*/ +else if (!cert->self_signed && !cert->root_ca) + prep->payload_flags |=KEY_MAYBE_PECA ; + }/* We're pinning the module by being linked against it */ __module_get (public_key_subtype.owner);
include/linux/ima.h +11 -0
@@ -12,6 +12,7 @@#include <linux/security.h> #include <linux/kexec.h> #include <crypto/hash_info.h> +#include <keys/system_keyring.h> struct linux_binprm ;#ifdef CONFIG_IMA
@@ -181,6 +182,16 @@ static inline void ima_post_key_create_or_update(struct key *keyring, bool create) {}#endif /* CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS */ +#ifdef CONFIG_ASYMMETRIC_KEY_TYPE +#ifdef CONFIG_SECONDARY_TRUSTED_KEYRING +#define ima_validate_builtin_ca restrict_link_by_ca_builtin_and_secondary_trusted +#else +#define ima_validate_builtin_ca restrict_link_by_ca_builtin_trusted +#endif +#else +#define ima_validate_builtin_ca restrict_link_reject +#endif +#ifdef CONFIG_IMA_APPRAISE extern bool is_ima_appraise_enabled (void ); extern void ima_inode_post_setattr(struct user_namespace *mnt_userns,
include/linux/key-type.h +1 -0
@@ -38,6 +38,7 @@ struct key_preparsed_payload {time64_t expiry;/* Expiry time of key */ unsigned int payload_flags;/* Proposed payload flags */ #define KEY_ALLOC_PECA 0x0001/* Proposed Endorsed CA (ECA) key */ +#define KEY_MAYBE_PECA 0x0002/* Proposed possible ECA key */ } __randomize_layout;typedef int (*request_key_actor_t )(struct key *auth_key,void *aux);
security/keys/key.c +5 -0
@@ -900,6 +900,11 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, } } +/* Previous restriction check passed therefore try to validate endorsed ca */ +if ((prep.payload_flags &KEY_MAYBE_PECA ) && + !(ima_validate_builtin_ca (keyring, index_key.type, &prep.payload,NULL ))) + prep.payload_flags |=KEY_ALLOC_PECA ; +/* if we're going to allocate a new key, we're going to have * to modify the keyring */ ret =key_permission (keyring_ref,KEY_NEED_WRITE );
Signature · 1 line
2.27.0
Eric Snowberg
reviewerPATCH v2 8/10 integrity: Use root of trust signature restriction
In reply to Eric SnowbergKeys added to the IMA keyring must be vouched for by keys contained within the builtin or secondary keyrings. These keys must also be self signed, have the CA bit set and have the keyCertSign KeyUsage bit set. Or they could be validated by a properly formed intermediate CA. Currently these restrictions are not enforced. Use the new restrict_link_by_ca_builtin_and_secondary_trusted and restrict_link_by_ca_builtin_trusted to enforce the missing CA restrictions when adding keys to the IMA keyring. With the CA restrictions enforced, allow the machine keyring to be enabled with IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY.
--- security/integrity/Kconfig | 1 - security/integrity/digsig.c | 4 ++-- security/integrity/ima/Kconfig | 6 +++--- 3 files changed, 5 insertions(+), 6 deletions(-)
security/integrity/Kconfig +0 -1
@@ -68,7 +68,6 @@ config INTEGRITY_MACHINE_KEYRING depends on INTEGRITY_ASYMMETRIC_KEYS depends on SYSTEM_BLACKLIST_KEYRING depends on LOAD_UEFI_KEYS - depends on !IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY help If set, provide a keyring to which Machine Owner Keys (MOK) may be added. This keyring shall contain just MOK keys. Unlike keys
security/integrity/digsig.c +2 -2
@@ -34,9 +34,9 @@ static const char * const keyring_name[INTEGRITY_KEYRING_MAX] = { };#ifdef CONFIG_IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY -#define restrict_link_to_ima restrict_link_by_builtin_and_secondary_trusted +#define restrict_link_to_ima restrict_link_by_ca_builtin_and_secondary_trusted#else -#define restrict_link_to_ima restrict_link_by_builtin_trusted +#define restrict_link_to_ima restrict_link_by_ca_builtin_trusted#endif static struct key *integrity_keyring_from_id (const unsigned int id)
security/integrity/ima/Kconfig +3 -3
@@ -269,13 +269,13 @@ config IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARY default n help Keys may be added to the IMA or IMA blacklist keyrings, if the - key is validly signed by a CA cert in the system built-in or - secondary trusted keyrings. + key is validly signed by a CA cert in the system built-in, + secondary trusted, or machine keyrings. Intermediate keys between those the kernel has compiled in and the IMA keys to be added may be added to the system secondary keyring, provided they are validly signed by a key already resident in the - built-in or secondary trusted keyrings. + built-in, secondary trusted or machine keyrings. config IMA_BLACKLIST_KEYRING bool "Create IMA machine owner blacklist keyrings (EXPERIMENTAL)"
Signature · 1 line
2.27.0
Eric Snowberg
reviewerPATCH v2 9/10 KEYS: CA link restriction
In reply to Eric SnowbergAdd a new link restriction. Restrict the addition of keys in a keyring based on the key to be added being a CA.
--- crypto/asymmetric_keys/restrict.c | 40 ++++++++++++++++++++++++ crypto/asymmetric_keys/x509_public_key.c | 5 ++- include/crypto/public_key.h | 16 ++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-)
crypto/asymmetric_keys/restrict.c +40 -0
@@ -108,6 +108,46 @@ int restrict_link_by_signature(struct key *dest_keyring,return ret; } +/** +* restrict_link_by_ca - Restrict additions to a ring of CA keys +* @dest_keyring: Keyring being linked to. +* @type: The type of key being added. +* @payload: The payload of the new key. +* @trust_keyring: Unused. +* +* Check if the new certificate is a CA. If it is a CA, then mark the new +* certificate as being ok to link. +* +* Returns 0 if the new certificate was accepted, -ENOKEY if the +* certificate is not a CA. -ENOPKG if the signature uses unsupported +* crypto, or some other error if there is a matching certificate but +* the signature check cannot be performed. +*/ +int restrict_link_by_ca (struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *trust_keyring) +{ +const struct public_key_signature *sig; +const struct public_key *pkey; + +if (type != &key_type_asymmetric) +return -EOPNOTSUPP ; + + sig = payload->data[asym_auth]; +if (!sig) +return -ENOPKG ; + + pkey = payload->data[asym_crypto]; +if (!pkey) +return -ENOPKG ; + +if (!pkey->key_is_ca) +return -ENOKEY ; + +return public_key_verify_signature (pkey, sig); +} + int restrict_link_by_ca_and_signature(struct key *dest_keyring, const struct key_type *type, const union key_payload *payload,
crypto/asymmetric_keys/x509_public_key.c +4 -1
@@ -209,8 +209,11 @@ static int x509_key_preparse(struct key_preparsed_payload *prep) }if (cert->kcs_set) { -if (cert->self_signed && cert->root_ca) +if (cert->self_signed && cert->root_ca) { prep->payload_flags |=KEY_ALLOC_PECA ; + cert->pub->key_is_ca = true; + } +/* * In this case it could be an Intermediate CA. Set * KEY_MAYBE_PECA for now. If the restriction check
include/crypto/public_key.h +16 -0
@@ -26,6 +26,7 @@ struct public_key {void *params;u32 paramlen;bool key_is_private; +bool key_is_ca;const char *id_type;const char *pkey_algo; };
@@ -76,6 +77,21 @@ extern int restrict_link_by_ca_and_signature(struct key *dest_keyring,const union key_payload *payload, struct key *unused); +#if IS_REACHABLE (CONFIG_ASYMMETRIC_KEY_TYPE ) +extern int restrict_link_by_ca (struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *trust_keyring); +#else +static inline int restrict_link_by_ca (struct key *dest_keyring, +const struct key_type *type, +const union key_payload *payload, +struct key *trust_keyring) +{ +return 0 ; +} +#endif +extern int query_asymmetric_key (const struct kernel_pkey_params *,struct kernel_pkey_query *);
Signature · 1 line
2.27.0
Eric Snowberg
reviewerPATCH v2 10/10 integrity: restrict INTEGRITY_KEYRING_MACHINE to restrict_link_by_ca
In reply to Eric SnowbergSet the restriction check for INTEGRITY_KEYRING_MACHINE keys to restrict_link_by_ca. This will only allow CA keys into the machine keyring.
--- security/integrity/Kconfig | 10 ++++++++++ security/integrity/digsig.c | 8 ++++++-- 2 files changed, 16 insertions(+), 2 deletions(-)
security/integrity/Kconfig +10 -0
@@ -74,6 +74,16 @@ config INTEGRITY_MACHINE_KEYRING in the platform keyring, keys contained in the .machine keyring will be trusted within the kernel. +config INTEGRITY_CA_MACHINE_KEYRING + bool "Only allow CA keys into the Machine Keyring" + depends on INTEGRITY_MACHINE_KEYRING + help + If set, only Machine Owner Keys (MOK) that are Certificate + Authority (CA) keys will be added to the .machine keyring. All + other MOK keys will be added to the .platform keyring. After + booting, any other key signed by the CA key can be added to the + secondary_trusted_keys keyring. + config LOAD_UEFI_KEYS depends on INTEGRITY_PLATFORM_KEYRING depends on EFI
security/integrity/digsig.c +6 -2
@@ -131,7 +131,8 @@ int __init integrity_init_keyring(const unsigned int id) | KEY_USR_READ | KEY_USR_SEARCH;if (id ==INTEGRITY_KEYRING_PLATFORM || - id ==INTEGRITY_KEYRING_MACHINE ) { + (id ==INTEGRITY_KEYRING_MACHINE && + !IS_ENABLED (CONFIG_INTEGRITY_CA_MACHINE_KEYRING ))) { restriction =NULL ; goto out; }
@@ -143,7 +144,10 @@ int __init integrity_init_keyring(const unsigned int id)if (!restriction)return -ENOMEM ; - restriction->check = restrict_link_to_ima; +if (id ==INTEGRITY_KEYRING_MACHINE ) + restriction->check = restrict_link_by_ca; +else + restriction->check = restrict_link_to_ima;/* * MOK keys can only be added through a read-only runtime services
Signature · 1 line
2.27.0
Hi Eric,
Thanks for your work! The patch set looks good to me except for the requirement of an intermediate CA certificate should be vouched for by a root CA certificate before it can vouch for other certificates. What if users only want to enroll an intermediate CA certificate into the MOK? If this requirement could be dropped, the code could be simplified and some issues could be resolved automatically,
1. "[PATCH v2 03/10] KEYS: X.509: Parse Basic Constraints for CA" added a root_ca filed to a certificate to indicate the subject of the certificate is a CA. The name root_ca implies it's also a root CA. But according to [1], both an intermediate and root CA will have root_ca=True. For example, the intermediate certificate of
https://www.kernel.org/ has "Certificate Authority=Yes" in the basic constraints. Btw, a root CA certificate by definition is self-signed, so the following code in "[PATCH v2 05/10] KEYS: Introduce a CA endorsed flag" looks a bit strange to me,
if (cert->kcs_set && cert->self_signed && cert->root_ca)
prep->payload_flags |= KEY_ALLOC_PECA;2. Since an intermediate CA certificate also has root_ca=True, "[PATCH v2 07/10] KEYS: X.509: Flag Intermediate CA certs as endorsed" won't work as intended i.e. this following else branch will never be reached,
else if (!cert->self_signed && !cert->root_ca)
prep->payload_flags |= KEY_MAYBE_PECA;
3. I see nowhere public_key->key_is_ca is set to true for an intermediate CA certificate after it gains the KEY_ALLOC_PECA flag. So it will fail restrict_link_by_ca even if the KEY_MAYBE_PECA flag is added.
[1] https://www.rfc-editor.org/rfc/rfc5280#section-4.2.1.9
Quoted 128 lines · Eric Snowberg
Prior to the introduction of the machine keyring, most distros simply allowed all keys contained within the platform keyring to be used for both kernel and module verification. This was done by an out of tree patch. Some distros took it even further and loaded all these keys into the secondary trusted keyring. This also allowed the system owner to add their own key for IMA usage.
Each distro contains similar documentation on how to sign kernel modules and enroll the key into the MOK. The process is fairly straightforward. With the introduction of the machine keyring, the process remains basically the same, without the need for any out of tree patches.
The machine keyring allowed distros to eliminate the out of tree patches for kernel module signing. However, it falls short in allowing the end user to add their own keys for IMA. Currently the machine keyring can not be used as another trust anchor for adding keys to the ima keyring, since CA enforcement does not currently exist. This would expand the current integrity gap. The
IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARYKconfigstates that keys may be added to the ima keyrings if the key is validly signed by a CA cert in the system built-in or secondary trusted keyring. Currently there is not code that enforces the "validly signed" CA cert. Any key in the builtin or secondary keyring can be used. Also, the definition of "validly signed" has never been stated, leaving this up to multiple interpretations.To allow IMA to be enabled with the machine keyring, this series introduces enforcement of key usage in the certificate. This series also defines a "validly signed" CA cert and applies the same definition across all kernel keyrings.
The machine keyring shares similarities with both the builtin and secondary keyrings. Similar to the builtin, no keys may be added to the machine keyring following boot. The secondary keyring allows user provided keys to be added following boot; however, a previously enrolled kernel key must vouch for the key before it may be included. The system owner may include their own keys into the machine keyring prior to boot. If the end-user is not the system owner, they may not add their own keys to the machine keyring.
The machine keyring is only populated when Secure Boot is enabled. A system owner has the ability to control the entire Secure Boot keychain (PK, KEK, DB, and DBX). The system owner can also turn Secure Boot off. With this control, they may use insert-sys-cert to include their own key and re-sign their kernel and have it boot. The system owner also has control to include or exclude MOK keys. This series does not try to interpret how a system owner has configured their machine. If the system owner has taken the steps to add their own MOK keys, they will be included in the machine keyring and used for verification, exactly the same way as keys contained in the builtin and secondary keyrings. Since the system owner has the ability to add keys before booting to either the machine or builtin keyrings, it is viewed as inconsequential if the key originated from one or the other.
This series introduces two different ways to configure the machine keyring. By default, nothing changes and all MOK keys are loaded into it. Whenever a "validly signed" key is found within the machine, builtin, or secondary, a flag indicating this is stored in the public key struct. The other option is if the new
KconfigINTEGRITY_CA_MACHINE_KEYRINGis enabled, only "validly signed" CA certs will be loaded into the machine keyring. All remaining MOK keys will be loaded into the platform keyring.A "validly signed" CA cert shall be defined as any X509 certificate that is self signed, contains the keyCertSign key usage and has the CA bit set to true.
With this series applied, "validly signed" CA enforcement is in place whenever
IMA_KEYRINGS_PERMIT_SIGNED_BY_BUILTIN_OR_SECONDARYis enabled. Meaning, before any key can be included into the ima keyring, it must be vouched for by a "validly signed" CA key contained within the builtin, secondary, or machine keyrings.IMA allows userspace applications to be signed. The enduser may sign their own application, however they may also want to use an application provided by a 3rd party. The entity building the kernel, may not be the same entity building the userspace program. The system owner may also be a third entity. If the system owner trusts the entity building the userspace program, they will include their public key within the MOK. This key would be used to sign the key added to the ima keyring. Not all 3rd party userspace providers have the capability to properly manage a root CA. Some may outsource to a different code signing provider. Many code signing providers use Intermediate CA certificates. Therefore, this series also includes support for Intermediate CA certificates. If a "validly signed" CA can vouch for an Intermediate CA, and it contains the keyCertSign key usage, it will also be flagged as being a "validly signed" CA. The Intermediate CA certificates could be loaded into the secondary keyring following boot. Afterwards, CA enforcement is maintained for any key added to the ima keyring.
This series could be broken up into 3 different parts. The first two patches could be taken now. They solve current issues that will be triggered by the build robots. Patches 3-8 add CA enforcement for the ima keyring. Patches 9-10 restrict the machine keyring to only load CA certs into it. Patches 9-10 require all the previous patches since Intermediate CA enforcement is required.
Eric Snowberg (10):
KEYS: Create static version ofpublic_key_verify_signature
KEYS: Add missing function documentation
KEYS: X.509: Parse Basic Constraints for CA
KEYS: X.509: Parse Key Usage
KEYS: Introduce a CA endorsed flag
KEYS: Introduce keyring restriction that validates ca trust
KEYS: X.509: Flag Intermediate CA certs as endorsed
integrity: Use root of trust signature restriction
KEYS: CA link restriction
integrity: restrictINTEGRITY_KEYRING_MACHINEtorestrict_link_by_cacerts/system_keyring.c | 30 ++++++++- crypto/asymmetric_keys/restrict.c | 81 +++++++++++++++++++++++ crypto/asymmetric_keys/x509_cert_parser.c | 31 +++++++++ crypto/asymmetric_keys/x509_parser.h | 2 + crypto/asymmetric_keys/x509_public_key.c | 16 +++++ include/crypto/public_key.h | 30 +++++++++ include/keys/system_keyring.h | 12 +++- include/linux/ima.h | 11 +++ include/linux/key-type.h | 3 + include/linux/key.h | 2 + security/integrity/Kconfig | 11 ++- security/integrity/digsig.c | 12 ++-- security/integrity/ima/Kconfig | 6 +- security/keys/key.c | 13 ++++ 14 files changed, 249 insertions(+), 11 deletions(-)base-commit:
76dcd734eca23168cb008912c0f69ff408905235Signature · 1 line
2.27.0
Signature · 2 lines
Best regards, Coiby
Eric Snowberg
reviewerQuoted 7 lines · Coiby Xu
Thanks for your work! The patch set looks good to me except for the requirement of an intermediate CA certificate should be vouched for by a root CA certificate before it can vouch for other certificates. What if users only want to enroll an intermediate CA certificate into the MOK?
This question would need to be answered by the maintainers. The intermediate requirement was based on my understanding of previous discussions requiring there be a way to validate root of trust all the way back to the root CA.
Quoted 2 lines
If this requirement could be dropped, the code could be simplified and some issues could be resolved automatically,
Agreed. I will make sure the issue below is resolved one way or the other, once we have an agreement on the requirements.
Quoted 17 lines
1. "[PATCH v2 03/10] KEYS: X.509: Parse Basic Constraints for CA" added a
root_cafiled to a certificate to indicate the subject of the certificate is a CA. The nameroot_caimplies it's also a root CA. But according to [1], both an intermediate and root CA will have root_ca=True. For example, the intermediate certificate of
https://www.kernel.org/ has "Certificate Authority=Yes" in the basic constraints. Btw, a root CA certificate by definition is self-signed, so the following code in "[PATCH v2 05/10] KEYS: Introduce a CA endorsed flag" looks a bit strange to me,
if (cert->kcs_set&&cert->self_signed&&cert->root_ca)prep->payload_flags |= KEY_ALLOC_PECA;2. Since an intermediate CA certificate also has root_ca=True, "[PATCH v2 07/10] KEYS: X.509: Flag Intermediate CA certs as endorsed" won't work as intended i.e. this following else branch will never be reached,
else if (!cert->self_signed && !cert->root_ca)
Quoted 7 lines
prep->payload_flags |= KEY_MAYBE_PECA;3. I see nowhere
public_key->key_is_cais set to true for an intermediate CA certificate after it gains theKEY_ALLOC_PECAflag. So it will failrestrict_link_by_caeven if theKEY_MAYBE_PECAflag is added.
Thanks for reviewing the series.
Mimi Zohar
maintainerHi Eric, Coiby,
Quoted 10 lines · Eric Snowberg
Quoted 6 lines · Coiby Xu
Thanks for your work! The patch set looks good to me except for the requirement of an intermediate CA certificate should be vouched for by a root CA certificate before it can vouch for other certificates. What if users only want to enroll an intermediate CA certificate into the MOK?
This question would need to be answered by the maintainers. The intermediate requirement was based on my understanding of previous discussions requiring there be a way to validate root of trust all the way back to the root CA.
That definitely did not come from me. My requirement all along has been to support a single self-signed CA certificate for the end user/customer use case, so that they could create and load their own public key, signed by that CA, onto the trusted IMA/EVM keyrings.
Quoted 6 lines
Quoted 3 lines
If this requirement could be dropped, the code could be simplified and some issues could be resolved automatically,
Agreed. I will make sure the issue below is resolved one way or the other, once we have an agreement on the requirements.
I totally agree with Coiby that there is no need for intermediate CA certificates be vouched for by a root CA certificate. In fact the closer the CA certificate is to the leaf code signing certificate, the better. As much as possible we want to limit the CA keys being loaded onto the machine keyring to those that are absolutely required.
thanks,
Mimi
Quoted 28 lines
Quoted 18 lines
1. "[PATCH v2 03/10] KEYS: X.509: Parse Basic
aroot_cafiled to a certificate to indicate the subject of the certificate is a CA. The nameroot_caimplies it's also a root CA. But according to [1], both an intermediate and root CA will have root_ca=True. For example, the intermediate certificate of
https://www.kernel.org/ has "Certificate Authority=Yes" in the basic constraints. Btw, a root CA certificate by definition is self-signed, so the following code in "[PATCH v2 05/10] KEYS: Introduce a CA endorsed flag" looks a bit strange to me,
if (cert->kcs_set&&cert->self_signed&&cert->root_ca)prep->payload_flags |= KEY_ALLOC_PECA;2. Since an intermediate CA certificate also has root_ca=True, "[PATCH v2 07/10] KEYS: X.509: Flag Intermediate CA certs as endorsed" won't work as intended i.e. this following else branch will never be reached,
else if (!cert->self_signed && !cert->root_ca)Quoted 7 lines
prep->payload_flags |= KEY_MAYBE_PECA;3. I see nowhere
public_key->key_is_cais set to true for an intermediate CA certificate after it gains theKEY_ALLOC_PECAflag. So it will failrestrict_link_by_caeven if theKEY_MAYBE_PECAflag is added.Thanks for reviewing the series.
Eric Snowberg
reviewerQuoted 27 lines · Mimi Zohar
Hi Eric, Coiby,
Quoted 10 lines · Eric Snowberg
Quoted 6 lines · Coiby Xu
Thanks for your work! The patch set looks good to me except for the requirement of an intermediate CA certificate should be vouched for by a root CA certificate before it can vouch for other certificates. What if users only want to enroll an intermediate CA certificate into the MOK?
This question would need to be answered by the maintainers. The intermediate requirement was based on my understanding of previous discussions requiring there be a way to validate root of trust all the way back to the root CA.
That definitely did not come from me. My requirement all along has been to support a single self-signed CA certificate for the end user/customer use case, so that they could create and load their own public key, signed by that CA, onto the trusted IMA/EVM keyrings.
Quoted 4 lines
Quoted 3 lines
If this requirement could be dropped, the code could be simplified and some issues could be resolved automatically,
Quoted 8 lines
Quoted 2 lines
Agreed. I will make sure the issue below is resolved one way or the other, once we have an agreement on the requirements.
I totally agree with Coiby that there is no need for intermediate CA certificates be vouched for by a root CA certificate. In fact the closer the CA certificate is to the leaf code signing certificate, the better. As much as possible we want to limit the CA keys being loaded onto the machine keyring to those that are absolutely required.
Ok, I will change this in the next round. The confusion around the requirement comes from the request to validate the cert is self-signed. The intermediate in this case will not be self signed. As long as this check is not necessary, I will drop it from the code and allow the intermediate to vouch for the ima key without the root being present. Thanks for clearing this up.