KEYS: Measure keys when they are created or updated
Subsystems (5)
cover KEYS: Measure keys when they are created or updated
commit 88e70da170e8 ("IMA: Define an IMA hook to measure keys") in next-integrity added an IMA hook to measure keys when they are created or updated in the system. This hook is defined in ima_asymmetric_keys.c which was built if CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE was defined.
But this config is a tristate (and not a bool type). If CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE was set to "m" in the .config, ima_asymmetric_keys.c was built as a kernel module when it is not a kernel module. This issue was reported by "kbuild test robot <[email protected]>".
This change defines a new config namely CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS to enable building ima_asymmetric_keys.c. This new config is enabled when both CONFIG_IMA and CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE are defined.
Lakshmi Ramasubramanian (4):
IMA: Define an IMA hook to measure keys
KEYS: Call the IMA hook to measure keys
IMA: Add support to limit measuring keys
IMA: Read keyrings= option from the IMA policy
Documentation/ABI/testing/ima_policy | 10 ++- include/linux/ima.h | 14 +++ security/integrity/ima/Kconfig | 9 ++ security/integrity/ima/Makefile | 1 + security/integrity/ima/ima.h | 8 +- security/integrity/ima/ima_api.c | 8 +- security/integrity/ima/ima_appraise.c | 4 +- security/integrity/ima/ima_asymmetric_keys.c | 58 +++++++++++++ security/integrity/ima/ima_main.c | 9 +- security/integrity/ima/ima_policy.c | 91 ++++++++++++++++++-- security/keys/key.c | 10 +++ 11 files changed, 204 insertions(+), 18 deletions(-) create mode 100644 security/integrity/ima/ima_asymmetric_keys.c
Signature · 1 line
2.17.1
PATCH 1/4 IMA: Define an IMA hook to measure keys
Measure asymmetric keys used for verifying file signatures, certificates, etc.
This patch defines a new IMA hook namely ima_post_key_create_or_update() to measure the payload used to create a new asymmetric key or update an existing asymmetric key.
Added a new config CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS that is enabled when CONFIG_IMA and CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE are defined. Asymmetric key structure is defined only when CONFIG_ASYMMETRIC_PUBLIC_KEY_SUBTYPE is defined. Since the IMA hook measures asymmetric keys, the IMA hook is defined in a new file namely ima_asymmetric_keys.c which is built only if CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS is defined.
is built as a kernel module when it is actually not.
--- security/integrity/ima/Kconfig | 9 ++++ security/integrity/ima/Makefile | 1 + security/integrity/ima/ima_asymmetric_keys.c | 52 ++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 security/integrity/ima/ima_asymmetric_keys.c
security/integrity/ima/Kconfig +9 -0
@@ -310,3 +310,12 @@ config IMA_APPRAISE_SIGNED_INIT default n help This option requires user-space init to be signed. + +config IMA_MEASURE_ASYMMETRIC_KEYS + bool "Enable measuring asymmetric keys on key create or update" + depends on IMA=y + depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE=y + default y + help + This option enables measuring asymmetric keys when + the key is created or updated.
security/integrity/ima/Makefile +1 -0
@@ -12,3 +12,4 @@ ima-$(CONFIG_IMA_APPRAISE) += ima_appraise.o ima-$(CONFIG_IMA_APPRAISE_MODSIG) += ima_modsig.o ima-$(CONFIG_HAVE_IMA_KEXEC) += ima_kexec.o obj-$(CONFIG_IMA_BLACKLIST_KEYRING) += ima_mok.o +obj-$(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) += ima_asymmetric_keys.o
security/integrity/ima/ima_asymmetric_keys.c new file +52 -0
52 lines not on this page · raw message
Signature · 1 line
2.17.1
James Bottomley
maintainerRe: [PATCH 1/4] IMA: Define an IMA hook to measure keys
On Tue, 2020-01-07 at 11:43 -0800, Lakshmi Ramasubramanian wrote:
[...]
Quoted 12 lines
security/integrity/ima/Kconfig +3
-0@@ -310,3 +310,12 @@ config IMA_APPRAISE_SIGNED_INIT default n help This option requires user-space init to be signed. + +config IMA_MEASURE_ASYMMETRIC_KEYS + bool "Enable measuring asymmetric keys on key create or
Quoted 1 line
update"
I don't believe there's a need to expose this to the person configuring the kernel, is there? It's just one more option no-one really wants to have to understand. Without the text following bool and the help, this becomes a hidden config option, which is what I think it should be.
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 316
Quoted 1 line
security/integrity/ima/Kconfig +1
-0+ depends on IMA=y
Not that it matters, but IMA is a bool, so this can be simply depends on IMA
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 317
Quoted 1 line
security/integrity/ima/Kconfig +1
-0+ depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE=y
We only need the =y here becase the variable is a tristate, so this becomes n for both the n and m cases.
security/integrity/ima/Kconfig config IMA_APPRAISE_SIGNED_INIT · line 321
Quoted 4 lines
security/integrity/ima/Kconfig +4
-0+ default y + help + This option enables measuring asymmetric keys when + the key is created or updated.
And drop the help entry. For future information, help text must be tab followed by two spaces, not three ... checkpatch doesn't actually catch this, unfortunately.
James
PATCH 2/4 KEYS: Call the IMA hook to measure keys
In reply to Lakshmi RamasubramanianCall the IMA hook from key_create_or_update() function to measure the payload when a new key is created or an existing key is updated.
This patch adds the call to the IMA hook from key_create_or_update() function to measure the key on key create or update.
is built as a kernel module when it is actually not.
--- include/linux/ima.h | 14 ++++++++++++++ security/keys/key.c | 10 ++++++++++ 2 files changed, 24 insertions(+)
include/linux/ima.h +14 -0
@@ -101,6 +101,20 @@ static inline void ima_add_kexec_buffer(struct kimage *image) {}#endif +#ifdef CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS +extern void ima_post_key_create_or_update (struct key *keyring, +struct key *key, +const void *payload,size_t plen, +unsigned long flags,bool create); +#else +static inline void ima_post_key_create_or_update (struct key *keyring, +struct key *key, +const void *payload, +size_t plen, +unsigned long flags, +bool create) {} +#endif /* CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS */ +#ifdef CONFIG_IMA_APPRAISE extern bool is_ima_appraise_enabled (void );extern void ima_inode_post_setattr (struct dentry *dentry);
security/keys/key.c +10 -0
@@ -13,6 +13,7 @@#include <linux/security.h> #include <linux/workqueue.h> #include <linux/random.h> +#include <linux/ima.h> #include <linux/err.h> #include "internal.h"
@@ -936,6 +937,9 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, goto error_link_end; } +ima_post_key_create_or_update (keyring, key, payload, plen, + flags, true); + key_ref =make_key_ref (key,is_key_possessed (keyring_ref)); error_link_end:
@@ -965,6 +969,12 @@ key_ref_t key_create_or_update(key_ref_t keyring_ref, } key_ref =__key_update (key_ref, &prep); + +if (!IS_ERR (key_ref)) +ima_post_key_create_or_update (keyring, key, + payload, plen, + flags, false); + goto error_free_prep; }EXPORT_SYMBOL (key_create_or_update);
Signature · 1 line
2.17.1
Mimi Zohar
maintainerRe: [PATCH 2/4] KEYS: Call the IMA hook to measure keys
Quoted 6 lines · Lakshmi Ramasubramanian
Call the IMA hook from
key_create_or_update()function to measure the payload when a new key is created or an existing key is updated.This patch adds the call to the IMA hook from
key_create_or_update()function to measure the key on key create or update.
Quoted 6 lines
Signed-off-by Lakshmi Ramasubramanian <[email protected]>Cc David Howells <[email protected]>Cc Jarkko Sakkinen <[email protected]>is built as a kernel module when it is actually not.
Fixes cb1aa3823c92 ("KEYS: Call the IMA hook to measure keys")
There are two ways of addressing a bug report. One is by fixing the original patch, while the other addresses the bug as a separate patch. If the fix is squashed into the original patch, the commit number will change. Only if the fix is a separate patch, would you include the "Fixes" tag.
Mimi
PATCH 3/4 IMA: Add support to limit measuring keys
In reply to Lakshmi RamasubramanianLimit measuring keys to those keys being loaded onto a given set of keyrings only and when the user id (uid) matches if uid is specified in the policy.
This patch defines a new IMA policy option namely "keyrings=" that can be used to specify a set of keyrings. If this option is specified in the policy for "measure func=KEY_CHECK" then only the keys loaded onto a keyring given in the "keyrings=" option are measured.
If uid is specified in the policy then the key is measured only if the current user id matches the one specified in the policy.
Added a new parameter namely "keyring" (name of the keyring) to process_buffer_measurement(). The keyring name is passed to ima_get_action() to determine the required action. ima_match_rules() is updated to check keyring in the policy, if specified, for KEY_CHECK function.
--- Documentation/ABI/testing/ima_policy | 10 +++- security/integrity/ima/ima.h | 8 ++- security/integrity/ima/ima_api.c | 8 ++- security/integrity/ima/ima_appraise.c | 4 +- security/integrity/ima/ima_asymmetric_keys.c | 8 ++- security/integrity/ima/ima_main.c | 9 +-- security/integrity/ima/ima_policy.c | 62 ++++++++++++++++++-- 7 files changed, 91 insertions(+), 18 deletions(-)
Documentation/ABI/testing/ima_policy +9 -1
@@ -25,7 +25,7 @@ Description: lsm: [[subj_user=] [subj_role=] [subj_type=] [obj_user=] [obj_role=] [obj_type=]] option: [[appraise_type=]] [template=] [permit_directio] - [appraise_flag=] + [appraise_flag=] [keyrings=] base: func:= [BPRM_CHECK][MMAP_CHECK][CREDS_CHECK][FILE_CHECK][MODULE_CHECK] [FIRMWARE_CHECK] [KEXEC_KERNEL_CHECK] [KEXEC_INITRAMFS_CHECK]
@@ -42,6 +42,9 @@ Description: appraise_flag:= [check_blacklist] Currently, blacklist check is only for files signed with appended signature. + keyrings:= list of keyrings + (eg, .builtin_trusted_keys|.ima). Only valid + when action is "measure" and func is KEY_CHECK. template:= name of a defined IMA template type (eg, ima-ng). Only valid when action is "measure". pcr:= decimal value
@@ -117,3 +120,8 @@ Description: Example of measure rule using KEY_CHECK to measure all keys: measure func=KEY_CHECK + + Example of measure rule using KEY_CHECK to only measure + keys added to .builtin_trusted_keys or .ima keyring: + + measure func=KEY_CHECK keyrings=.builtin_trusted_keys|.ima
security/integrity/ima/ima.h +5 -3
@@ -208,7 +208,8 @@ struct modsig;/* LIM API function definitions */ int ima_get_action (struct inode *inode,const struct cred *cred,u32 secid,int mask,enum ima_hooks func,int *pcr, -struct ima_template_desc **template_desc); +struct ima_template_desc **template_desc, +const char *keyring);int ima_must_measure (struct inode *inode,int mask,enum ima_hooks func); int ima_collect_measurement(struct integrity_iint_cache *iint, struct file *file, void *buf, loff_t size,
@@ -220,7 +221,7 @@ void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file, struct ima_template_desc *template_desc);void process_buffer_measurement (const void *buf,int size,const char *eventname,enum ima_hooks func, -int pcr); +int pcr,const char *keyring);void ima_audit_measurement (struct integrity_iint_cache *iint,const unsigned char *filename); int ima_alloc_init_template(struct ima_event_data *event_data,
@@ -235,7 +236,8 @@ const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);/* IMA policy related functions */ int ima_match_policy (struct inode *inode,const struct cred *cred,u32 secid,enum ima_hooks func,int mask,int flags,int *pcr, -struct ima_template_desc **template_desc); +struct ima_template_desc **template_desc, +const char *keyring);void ima_init_policy (void );void ima_update_policy (void );void ima_update_policy_flag (void );
security/integrity/ima/ima_api.c +4 -3
@@ -169,12 +169,13 @@ void ima_add_violation(struct file *file, const unsigned char *filename,* @func: caller identifier * @pcr: pointer filled in if matched measure policy sets pcr * @template_desc: pointer filled in if matched measure policy sets template+ * @keyring: keyring name used to determine the action * * The policy is defined in terms of keypairs: * subj=, obj=, type=, func=, mask=, fsmagic * subj,obj, and type: are LSM specific. * func: FILE_CHECK | BPRM_CHECK | CREDS_CHECK | MMAP_CHECK | MODULE_CHECK -* | KEXEC_CMDLINE +* | KEXEC_CMDLINE | KEY_CHECK * mask: contains the permission mask * fsmagic: hex value *
@@ -183,14 +184,15 @@ void ima_add_violation(struct file *file, const unsigned char *filename,*/ int ima_get_action (struct inode *inode,const struct cred *cred,u32 secid,int mask,enum ima_hooks func,int *pcr, -struct ima_template_desc **template_desc) +struct ima_template_desc **template_desc, +const char *keyring) {int flags =IMA_MEASURE |IMA_AUDIT |IMA_APPRAISE |IMA_HASH ; flags &= ima_policy_flag;return ima_match_policy (inode, cred, secid, func, mask, flags, pcr, - template_desc); + template_desc, keyring); }/*
security/integrity/ima/ima_appraise.c +2 -2
@@ -55,7 +55,7 @@ int ima_must_appraise(struct inode *inode, int mask, enum ima_hooks func)security_task_getsecid (current, &secid);return ima_match_policy (inode,current_cred (), secid, func, mask, -IMA_APPRAISE |IMA_HASH ,NULL ,NULL ); +IMA_APPRAISE |IMA_HASH ,NULL ,NULL ,NULL ); } static int ima_fix_xattr(struct dentry *dentry,
@@ -330,7 +330,7 @@ int ima_check_blacklist(struct integrity_iint_cache *iint,if ((rc = -EPERM ) && (iint->flags &IMA_MEASURE ))process_buffer_measurement (digest, digestsize,"blacklisted-hash" ,NONE , - pcr); + pcr,NULL ); }return rc;
security/integrity/ima/ima_asymmetric_keys.c +7 -1
@@ -46,7 +46,13 @@ void ima_post_key_create_or_update(struct key *keyring, struct key *key,* parameter to process_buffer_measurement() and is set * in the "eventname" field in ima_event_data for * the key measurement IMA event. +* +* The name of the keyring is also passed in the "keyring" +* parameter to process_buffer_measurement() to check +* if the IMA policy is configured to measure a key linked +* to the given keyring. */ process_buffer_measurement (payload, payload_len, - keyring->description,KEY_CHECK ,0 ); + keyring->description,KEY_CHECK ,0 , + keyring->description); }
security/integrity/ima/ima_main.c +5 -4
@@ -215,7 +215,7 @@ static int process_measurement(struct file *file, const struct cred *cred,* Included is the appraise submask. */ action =ima_get_action (inode, cred, secid, mask, func, &pcr, - &template_desc); + &template_desc,NULL ); violation_check = ((func =FILE_CHECK || func =MMAP_CHECK ) && (ima_policy_flag &IMA_MEASURE ));if (!action && !violation_check)
@@ -632,12 +632,13 @@ int ima_load_data(enum kernel_load_data_id id)* @eventname: event name to be used for the buffer entry. * @func: IMA hook * @pcr: pcr to extend the measurement +* @keyring: keyring name to determine the action to be performed * * Based on policy, the buffer is measured into the ima log. */ void process_buffer_measurement (const void *buf,int size,const char *eventname,enum ima_hooks func, -int pcr) +int pcr,const char *keyring) {int ret =0 ;struct ima_template_entry *entry =NULL ;
@@ -668,7 +669,7 @@ void process_buffer_measurement(const void *buf, int size,if (func) {security_task_getsecid (current, &secid); action =ima_get_action (NULL ,current_cred (), secid,0 , func, - &pcr, &template); + &pcr, &template, keyring);if (!(action &IMA_MEASURE ))return ; }
@@ -721,7 +722,7 @@ void ima_kexec_cmdline(const void *buf, int size) {if (buf && size !=0 )process_buffer_measurement (buf, size,"kexec-cmdline" , -KEXEC_CMDLINE ,0 ); +KEXEC_CMDLINE ,0 ,NULL ); }static int __init init_ima (void )
security/integrity/ima/ima_policy.c +58 -4
108 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH 4/4 IMA: Read keyrings= option from the IMA policy
In reply to Lakshmi RamasubramanianRead "keyrings=" option, if specified in the IMA policy, and store in the list of IMA rules when the configured IMA policy is read.
This patch defines a new policy token enum namely Opt_keyrings and an option flag IMA_KEYRINGS for reading "keyrings=" option from the IMA policy.
Updated ima_parse_rule() to parse "keyrings=" option in the policy. Updated ima_policy_show() to display "keyrings=" option.
The following example illustrates how key measurement can be verified.
Sample "key" measurement rule in the IMA policy:
measure func=KEY_CHECK uid=0 keyrings=.ima|.evm template=ima-buf
Display "key" measurement in the IMA measurement list:
cat /sys/kernel/security/ima/ascii_runtime_measurements
10 faf3...e702 ima-buf sha256:27c915b8ddb9fae7214cf0a8a7043cc3eeeaa7539bcb136f8427067b5f6c3b7b .ima 308202863082...4aee
Verify "key" measurement data for a key added to ".ima" keyring:
cat /sys/kernel/security/integrity/ima/ascii_runtime_measurements | grep -m 1 "\.ima" | cut -d' ' -f 6 | xxd -r -p |tee ima-cert.der | sha256sum | cut -d' ' -f 1
The output of the above command should match the template hash of the first "key" measurement entry in the IMA measurement list for the key added to ".ima" keyring.
The file namely "ima-cert.der" generated by the above command should be a valid x509 certificate (in DER format) and should match the one that was used to import the key to the ".ima" keyring. The certificate file can be verified using openssl tool.
--- security/integrity/ima/ima_policy.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)
security/integrity/ima/ima_policy.c +28 -1
@@ -34,6 +34,7 @@#define IMA_EUID 0x0080#define IMA_PCR 0x0100#define IMA_FSNAME 0x0200 +#define IMA_KEYRINGS 0x0400#define UNKNOWN 0#define MEASURE 0x0001/* same as IMA_MEASURE */
@@ -820,7 +821,8 @@ enum { Opt_uid_gt, Opt_euid_gt, Opt_fowner_gt, Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt, Opt_appraise_type, Opt_appraise_flag, - Opt_permit_directio, Opt_pcr, Opt_template, Opt_err + Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings, + Opt_err }; static const match_table_t policy_tokens = {
@@ -856,6 +858,7 @@ static const match_table_t policy_tokens = { {Opt_permit_directio, "permit_directio"}, {Opt_pcr, "pcr=%s"}, {Opt_template, "template=%s"}, + {Opt_keyrings, "keyrings=%s"}, {Opt_err, NULL} };
@@ -1105,6 +1108,23 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) result =0 ; entry->flags |=IMA_FSNAME ;break ; +case Opt_keyrings: +ima_log_string (ab,"keyrings" , args[0 ].from); + +if ((entry->keyrings) || + (entry->action !=MEASURE ) || + (entry->func !=KEY_CHECK )) { + result = -EINVAL ; +break ; + } + entry->keyrings =kstrdup (args[0 ].from,GFP_KERNEL ); +if (!entry->keyrings) { + result = -ENOMEM ; +break ; + } + result =0 ; + entry->flags |=IMA_KEYRINGS ; +break ;case Opt_fsuuid:ima_log_string (ab,"fsuuid" , args[0 ].from);
@@ -1480,6 +1500,13 @@ int ima_policy_show(struct seq_file *m, void *v)seq_puts (m," " ); } +if (entry->flags &IMA_KEYRINGS ) { +if (entry->keyrings !=NULL ) +snprintf (tbuf,sizeof (tbuf),"%s" , entry->keyrings); +seq_printf (m,pt (Opt_keyrings), tbuf); +seq_puts (m," " ); + } +if (entry->flags &IMA_PCR ) {snprintf (tbuf,sizeof (tbuf),"%d" , entry->pcr);seq_printf (m,pt (Opt_pcr), tbuf);
Signature · 1 line
2.17.1