IMA: Infrastructure for measurement of critical kernel data
Subsystems (6)
v3 cover IMA: Infrastructure for measurement of critical kernel data
There are several kernel components that contain critical data which if accidentally or maliciously altered, can compromise the security of the kernel. Example of such components would include LSMs like SELinux, or AppArmor; or device-mapper targets like dm-crypt, dm-verity etc.
Many of these components do not use the capabilities provided by kernel integrity subsystem (IMA), and thus they don't use the benefits of extended TPM PCR quotes and ultimately the benefits of remote attestation.
This series bridges this gap, so that potential kernel components that contain data critical to the security of the kernel could take advantage of IMA's measuring and quoting abilities - thus ultimately enabling remote attestation for their specific data.
System administrators may want to pick and choose which kernel components they would want to enable for measurements, quoting, and remote attestation. To enable that, a new IMA policy is introduced.
And lastly, the functionality is exposed through a function ima_measure_critical_data(). The functionality is generic enough to measure the data of any kernel component at run-time. To ensure that only data from supported sources are measured, the kernel component needs to be added to a compile-time list of supported sources (an "allowed list of components"). IMA validates the source passed to ima_measure_critical_data() against this allowed list at run-time.
This series is based on the following repo/branch:
repo: https://git.kernel.org/pub/scm/linux/kernel/git/zohar/linux-integrity.git
branch: next-integrity
commit d012a7190fc1 ("Linux 5.9-rc2")
This series also has a dependency on the following patch series:
https://patchwork.kernel.org/patch/11709527/
Change Log v3:
Incorporated feedback from Mimi on v2.
- Renamed the policy "data_sources" to "critical_kernel_data_sources".
- Added "critical_kernel_data_sources" description in Documentation/ima-policy.
- Split CRITICAL_DATA + critical_kernel_data_sources into two separate patches.
- Merged hook ima_measure_critical_data() + CRITICAL_DATA into a single patch.
- Added functionality to validate data sources before measurement.
Change Log v2:
- Reverted the unnecessary indentations in existing #define.
- Updated the description to replace the word 'enlightened' with 'supported'.
- Reverted the unnecessary rename of attribute size to buf_len.
- Introduced a boolean parameter measure_buf_hash as per community feedback to support measuring hash of the buffer, instead of the buffer itself.
Tushar Sugandhi (6):
IMA: generalize keyring specific measurement constructs
IMA: change process_buffer_measurement return type from void to int
IMA: update process_buffer_measurement to measure buffer hash
IMA: add policy to measure critical data from kernel components
IMA: add hook to measure critical data from kernel components
IMA: validate supported kernel data sources before measurement
Documentation/ABI/testing/ima_policy | 11 +- include/linux/ima.h | 11 ++ security/integrity/ima/ima.h | 41 +++++++- security/integrity/ima/ima_api.c | 8 +- security/integrity/ima/ima_appraise.c | 2 +- security/integrity/ima/ima_asymmetric_keys.c | 2 +- security/integrity/ima/ima_main.c | 72 +++++++++++-- security/integrity/ima/ima_policy.c | 101 +++++++++++++++---- security/integrity/ima/ima_queue_keys.c | 3 +- 9 files changed, 205 insertions(+), 46 deletions(-)
Signature · 1 line
2.17.1
PATCH v3 1/6 IMA: generalize keyring specific measurement constructs
IMA functions such as ima_match_keyring(), process_buffer_measurement(), ima_match_policy() etc. handle data specific to keyrings. Currently, these constructs are not generic to handle any func specific data. This makes it harder to extend without code duplication.
Refactor the keyring specific measurement constructs to be generic and reusable in other measurement scenarios.
--- security/integrity/ima/ima.h | 6 ++--- security/integrity/ima/ima_api.c | 6 ++--- security/integrity/ima/ima_main.c | 6 ++--- security/integrity/ima/ima_policy.c | 42 ++++++++++++++++------------- 4 files changed, 33 insertions(+), 27 deletions(-)
security/integrity/ima/ima.h +3 -3
@@ -255,7 +255,7 @@ static inline void ima_process_queued_keys(void) {}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, -const char *keyring); +const char *func_data);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,
@@ -267,7 +267,7 @@ void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file, struct ima_template_desc *template_desc);void process_buffer_measurement (struct inode *inode,const void *buf,int size,const char *eventname,enum ima_hooks func, -int pcr,const char *keyring); +int pcr,const char *func_data);void ima_audit_measurement (struct integrity_iint_cache *iint,const unsigned char *filename); int ima_alloc_init_template(struct ima_event_data *event_data,
@@ -283,7 +283,7 @@ const char *ima_d_path(const struct path *path, char **pathbuf, char *filename);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, -const char *keyring); +const char *func_data);void ima_init_policy (void );void ima_update_policy (void );void ima_update_policy_flag (void );
security/integrity/ima/ima_api.c +3 -3
@@ -170,7 +170,7 @@ 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 +* @func_data: private data specific to @func, can be NULL. * * The policy is defined in terms of keypairs: * subj=, obj=, type=, func=, mask=, fsmagic=
@@ -186,14 +186,14 @@ 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, -const char *keyring) +const char *func_data) {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, keyring); + template_desc, func_data); }/*
security/integrity/ima/ima_main.c +3 -3
@@ -732,13 +732,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 +* @func_data: private data specific to @func, can be NULL. * * Based on policy, the buffer is measured into the ima log. */ void process_buffer_measurement (struct inode *inode,const void *buf,int size,const char *eventname,enum ima_hooks func, -int pcr,const char *keyring) +int pcr,const char *func_data) {int ret =0 ;const char *audit_cause ="ENOMEM" ;
@@ -770,7 +770,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size,if (func) {security_task_getsecid (current, &secid); action =ima_get_action (inode,current_cred (), secid,0 , func, - &pcr, &template, keyring); + &pcr, &template, func_data);if (!(action &IMA_MEASURE ))return ; }
security/integrity/ima/ima_policy.c +24 -18
95 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v3 2/6 IMA: change process_buffer_measurement return type from void to int
In reply to Tushar Sugandhiprocess_buffer_measurement() does not return the result of the operation. Therefore, the consumers of this function cannot act on it, if needed.
Update return type of process_buffer_measurement() from void to int.
--- security/integrity/ima/ima.h | 6 +++--- security/integrity/ima/ima_main.c | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-)
security/integrity/ima/ima.h +3 -3
@@ -265,9 +265,9 @@ void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,struct evm_ima_xattr_data *xattr_value, int xattr_len, const struct modsig *modsig, int pcr, struct ima_template_desc *template_desc); -void process_buffer_measurement (struct inode *inode,const void *buf,int size, -const char *eventname,enum ima_hooks func, -int pcr,const char *func_data); +int process_buffer_measurement (struct inode *inode,const void *buf,int size, +const char *eventname,enum ima_hooks func, +int pcr,const char *func_data);void ima_audit_measurement (struct integrity_iint_cache *iint,const unsigned char *filename); int ima_alloc_init_template(struct ima_event_data *event_data,
security/integrity/ima/ima_main.c +7 -7
@@ -736,9 +736,9 @@ int ima_load_data(enum kernel_load_data_id id)* * Based on policy, the buffer is measured into the ima log. */ -void process_buffer_measurement (struct inode *inode,const void *buf,int size, -const char *eventname,enum ima_hooks func, -int pcr,const char *func_data) +int process_buffer_measurement (struct inode *inode,const void *buf,int size, +const char *eventname,enum ima_hooks func, +int pcr,const char *func_data) {int ret =0 ;const char *audit_cause ="ENOMEM" ;
@@ -758,7 +758,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size,u32 secid;if (!ima_policy_flag) -return ; +return 0 ;/* * Both LSM hooks and auxilary based buffer measurements are
@@ -772,7 +772,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size, action =ima_get_action (inode,current_cred (), secid,0 , func, &pcr, &template, func_data);if (!(action &IMA_MEASURE )) -return ; +return 0 ; }if (!pcr)
@@ -787,7 +787,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size,pr_err ("template %s init failed, result: %d\n" , (strlen (template->name) ? template->name : template->fmt), ret); -return ; +return ret; } }
@@ -819,7 +819,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size,func_measure_str (func), audit_cause, ret, 0, ret); -return ; +return ret; }/**
Signature · 1 line
2.17.1
PATCH v3 3/6 IMA: update process_buffer_measurement to measure buffer hash
In reply to Tushar Sugandhiprocess_buffer_measurement() currently only measures the input buffer. When the buffer being measured is too large, it may result in bloated IMA logs.
Introduce a boolean parameter measure_buf_hash to support measuring hash of a buffer, which would be much smaller, instead of the buffer itself.
--- security/integrity/ima/ima.h | 3 +- security/integrity/ima/ima_appraise.c | 2 +- security/integrity/ima/ima_asymmetric_keys.c | 2 +- security/integrity/ima/ima_main.c | 29 ++++++++++++++++++-- security/integrity/ima/ima_queue_keys.c | 3 +- 5 files changed, 32 insertions(+), 7 deletions(-)
security/integrity/ima/ima.h +2 -1
@@ -267,7 +267,8 @@ void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file, struct ima_template_desc *template_desc);int process_buffer_measurement (struct inode *inode,const void *buf,int size,const char *eventname,enum ima_hooks func, -int pcr,const char *func_data); +int pcr,const char *func_data, +bool measure_buf_hash);void ima_audit_measurement (struct integrity_iint_cache *iint,const unsigned char *filename); int ima_alloc_init_template(struct ima_event_data *event_data,
security/integrity/ima/ima_appraise.c +1 -1
@@ -336,7 +336,7 @@ int ima_check_blacklist(struct integrity_iint_cache *iint,if ((rc == -EPERM ) && (iint->flags &IMA_MEASURE ))process_buffer_measurement (NULL , digest, digestsize,"blacklisted-hash" ,NONE , - pcr,NULL ); + pcr,NULL , false); }return rc;
security/integrity/ima/ima_asymmetric_keys.c +1 -1
@@ -60,5 +60,5 @@ void ima_post_key_create_or_update(struct key *keyring, struct key *key,*/ process_buffer_measurement (NULL , payload, payload_len, keyring->description,KEY_CHECK ,0 , - keyring->description); + keyring->description, false); }
security/integrity/ima/ima_main.c +26 -3
@@ -733,17 +733,21 @@ int ima_load_data(enum kernel_load_data_id id)* @func: IMA hook * @pcr: pcr to extend the measurement * @func_data: private data specific to @func, can be NULL. +* @measure_buf_hash: if set to true - will measure hash of the buf, +* instead of buf * * Based on policy, the buffer is measured into the ima log. */ int process_buffer_measurement (struct inode *inode,const void *buf,int size,const char *eventname,enum ima_hooks func, -int pcr,const char *func_data) +int pcr,const char *func_data, +bool measure_buf_hash) {int ret =0 ;const char *audit_cause ="ENOMEM" ;struct ima_template_entry *entry =NULL ;struct integrity_iint_cache iint = {}; +struct integrity_iint_cache digest_iint = {};struct ima_event_data event_data = {.iint = &iint, .filename = eventname, .buf = buf,
@@ -752,7 +756,7 @@ int process_buffer_measurement(struct inode *inode, const void *buf, int size,struct {struct ima_digest_data hdr;char digest[IMA_MAX_DIGEST_SIZE ]; - } hash = {}; + } hash = {}, digest_hash = {};int violation =0 ;int action =0 ;u32 secid;
@@ -801,6 +805,24 @@ int process_buffer_measurement(struct inode *inode, const void *buf, int size, goto out; } +if (measure_buf_hash) { + digest_iint.ima_hash = &digest_hash.hdr; + digest_iint.ima_hash->algo = ima_hash_algo; + digest_iint.ima_hash->length = hash_digest_size[ima_hash_algo]; + + ret =ima_calc_buffer_hash (hash.hdr.digest, + iint.ima_hash->length, + digest_iint.ima_hash); +if (ret <0 ) { + audit_cause ="digest_hashing_error" ; + goto out; + } + + event_data.iint = &digest_iint; + event_data.buf = hash.hdr.digest; + event_data.buf_len = iint.ima_hash->length; + } + ret =ima_alloc_init_template (&event_data, &entry, template);if (ret <0 ) { audit_cause ="alloc_entry" ;
@@ -842,7 +864,8 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)return ;process_buffer_measurement (file_inode (f.file), buf, size, -"kexec-cmdline" ,KEXEC_CMDLINE ,0 ,NULL ); +"kexec-cmdline" ,KEXEC_CMDLINE ,0 ,NULL , + false);fdput (f); }
security/integrity/ima/ima_queue_keys.c +2 -1
@@ -162,7 +162,8 @@ void ima_process_queued_keys(void) entry->payload_len, entry->keyring_name,KEY_CHECK ,0 , - entry->keyring_name); + entry->keyring_name, + false);list_del (&entry->list);ima_free_key_entry (entry); }
Signature · 1 line
2.17.1
PATCH v3 4/6 IMA: add policy to measure critical data from kernel components
In reply to Tushar SugandhiThere would be several candidate kernel components suitable for IMA measurement. Not all of them would have support for IMA measurement. Also, system administrators may not want to measure data for all of them, even when they support IMA measurement. An IMA policy specific to various kernel components is needed to measure their respective critical data.
Add a new IMA policy "critical_kernel_data_sources" to support measuring various critical kernel components. This policy would enable the system administrators to limit the measurement to the components, if the components support IMA measurement.
--- Documentation/ABI/testing/ima_policy | 3 +++ security/integrity/ima/ima_policy.c | 29 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-)
Documentation/ABI/testing/ima_policy +3 -0
@@ -48,6 +48,9 @@ Description: template:= name of a defined IMA template type (eg, ima-ng). Only valid when action is "measure". pcr:= decimal value + critical_kernel_data_sources:= list of kernel + components (eg, selinux|apparmor|dm-crypt) that + contain data critical to the security of the kernel. default policy: # PROC_SUPER_MAGIC
security/integrity/ima/ima_policy.c +28 -1
@@ -33,6 +33,7 @@#define IMA_PCR 0x0100#define IMA_FSNAME 0x0200#define IMA_KEYRINGS 0x0400 +#define IMA_DATA_SOURCES 0x0800#define UNKNOWN 0#define MEASURE 0x0001/* same as IMA_MEASURE */
@@ -84,6 +85,7 @@ struct ima_rule_entry { } lsm[MAX_LSM_RULES ];char *fsname;struct ima_rule_opt_list *keyrings;/* Measure keys added to these keyrings */ +struct ima_rule_opt_list *data_sources;/* Measure data from these sources */ struct ima_template_desc *template; };
@@ -911,7 +913,7 @@ enum { Opt_uid_lt, Opt_euid_lt, Opt_fowner_lt, Opt_appraise_type, Opt_appraise_flag, Opt_permit_directio, Opt_pcr, Opt_template, Opt_keyrings, - Opt_err + Opt_data_sources, Opt_err }; static const match_table_t policy_tokens = {
@@ -948,6 +950,7 @@ static const match_table_t policy_tokens = { {Opt_pcr, "pcr=%s"}, {Opt_template, "template=%s"}, {Opt_keyrings, "keyrings=%s"}, + {Opt_data_sources, "critical_kernel_data_sources=%s"}, {Opt_err, NULL} };
@@ -1312,6 +1315,24 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) entry->flags |=IMA_KEYRINGS ;break ; +case Opt_data_sources: +ima_log_string (ab,"critical_kernel_data_sources" , + args[0 ].from); + +if (entry->data_sources) { + result = -EINVAL ; +break ; + } + + entry->data_sources =ima_alloc_rule_opt_list (args); +if (IS_ERR (entry->data_sources)) { + result =PTR_ERR (entry->data_sources); + entry->data_sources =NULL ; +break ; + } + + entry->flags |=IMA_DATA_SOURCES ; +break ;case Opt_fsuuid:ima_log_string (ab,"fsuuid" , args[0 ].from);
@@ -1692,6 +1713,12 @@ int ima_policy_show(struct seq_file *m, void *v)seq_puts (m," " ); } +if (entry->flags &IMA_DATA_SOURCES ) { +seq_puts (m,"critical_kernel_data_sources=" ); +ima_show_rule_opt_list (m, entry->data_sources); +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
PATCH v3 5/6 IMA: add hook to measure critical data from kernel components
In reply to Tushar SugandhiCurrently, IMA does not provide a generic function for kernel components to measure their data. A generic function provided by IMA would enable various parts of the kernel with easier and faster on-boarding to use IMA infrastructure, would avoid code duplication, and consistent usage of IMA policy "critical_kernel_data_sources" across the kernel.
Add a new IMA func CRITICAL_DATA and a corresponding IMA hook ima_measure_critical_data() to support measuring various critical kernel components. Limit the measurement to the components that are specified in the IMA policy - CRITICAL_DATA+critical_kernel_data_sources.
--- Documentation/ABI/testing/ima_policy | 8 ++++++- include/linux/ima.h | 11 +++++++++ security/integrity/ima/ima.h | 1 + security/integrity/ima/ima_api.c | 2 +- security/integrity/ima/ima_main.c | 24 ++++++++++++++++++++ security/integrity/ima/ima_policy.c | 34 ++++++++++++++++++++++++---- 6 files changed, 73 insertions(+), 7 deletions(-)
Documentation/ABI/testing/ima_policy +7 -1
@@ -29,7 +29,7 @@ Description: base: func:= [BPRM_CHECK][MMAP_CHECK][CREDS_CHECK][FILE_CHECK][MODULE_CHECK] [FIRMWARE_CHECK] [KEXEC_KERNEL_CHECK] [KEXEC_INITRAMFS_CHECK] - [KEXEC_CMDLINE] [KEY_CHECK] + [KEXEC_CMDLINE] [KEY_CHECK] [CRITICAL_DATA] mask:= [[^]MAY_READ] [[^]MAY_WRITE] [[^]MAY_APPEND] [[^]MAY_EXEC] fsmagic:= hex value
@@ -51,6 +51,8 @@ Description: critical_kernel_data_sources:= list of kernel components (eg, selinux|apparmor|dm-crypt) that contain data critical to the security of the kernel. + Only valid when action is "measure" and func is + CRITICAL_DATA. default policy: # PROC_SUPER_MAGIC
@@ -128,3 +130,7 @@ Description: keys added to .builtin_trusted_keys or .ima keyring: measure func=KEY_CHECK keyrings=.builtin_trusted_keys|.ima + + Example of measure rule using CRITICAL_DATA to measure critical data + + measure func=CRITICAL_DATA critical_kernel_data_sources=selinux|apparmor|dm-crypt
include/linux/ima.h +11 -0
@@ -26,6 +26,10 @@ extern int ima_post_read_file(struct file *file, void *buf, loff_t size,extern void ima_post_path_mknod (struct dentry *dentry);extern int ima_file_hash (struct file *file,char *buf,size_t buf_size);extern void ima_kexec_cmdline (int kernel_fd,const void *buf,int size); +extern int ima_measure_critical_data (const char *event_name, +const char *event_data_source, +const void *buf,int buf_len, +bool measure_buf_hash);#ifdef CONFIG_IMA_KEXEC extern void ima_add_kexec_buffer (struct kimage *image);
@@ -104,6 +108,13 @@ static inline int ima_file_hash(struct file *file, char *buf, size_t buf_size) }static inline void ima_kexec_cmdline (int kernel_fd,const void *buf,int size) {} +static inline int ima_measure_critical_data (const char *event_name, +const char *event_data_source, +const void *buf,int buf_len, +bool measure_buf_hash) +{ +return -EOPNOTSUPP ; +}#endif /* CONFIG_IMA */ #ifndef CONFIG_IMA_KEXEC
security/integrity/ima/ima.h +1 -0
@@ -200,6 +200,7 @@ static inline unsigned int ima_hash_key(u8 *digest) hook(POLICY_CHECK, policy) \ hook(KEXEC_CMDLINE, kexec_cmdline) \ hook(KEY_CHECK, key) \ + hook(CRITICAL_DATA, critical_data) \ hook(MAX_CHECK, none)#define __ima_hook_enumify (ENUM , str) ENUM,
security/integrity/ima/ima_api.c +1 -1
@@ -176,7 +176,7 @@ void ima_add_violation(struct file *file, const unsigned char *filename,* 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 | KEY_CHECK +* | KEXEC_CMDLINE | KEY_CHECK | CRITICAL_DATA * mask: contains the permission mask * fsmagic: hex value *
security/integrity/ima/ima_main.c +24 -0
@@ -869,6 +869,30 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)fdput (f); } +/** +* ima_measure_critical_data - measure critical data +* @event_name: name for the given data +* @event_data_source: name of the event data source +* @buf: pointer to buffer containing data to measure +* @buf_len: length of buffer(in bytes) +* @measure_buf_hash: if set to true - will measure hash of the buf, +* instead of buf +* +* Buffers can only be measured, not appraised. +*/ +int ima_measure_critical_data (const char *event_name, +const char *event_data_source, +const void *buf,int buf_len, +bool measure_buf_hash) +{ +if (!event_name || !event_data_source || !buf || !buf_len) +return -EINVAL ; + +return process_buffer_measurement (NULL , buf, buf_len, event_name, +CRITICAL_DATA ,0 , event_data_source, + measure_buf_hash); +} +static int __init init_ima (void ) {int error;
security/integrity/ima/ima_policy.c +29 -5
@@ -510,14 +510,23 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode, {int i; -if (func ==KEY_CHECK ) { -return (rule->flags &IMA_FUNC ) && (rule->func == func) && -ima_match_rule_data (rule, rule->keyrings, func_data, - true, cred); - }if ((rule->flags &IMA_FUNC ) && (rule->func != func && func !=POST_SETATTR ))return false; + +switch (func) { +case KEY_CHECK : +return ((rule->func == func) && +ima_match_rule_data (rule, rule->keyrings, + func_data, true, cred)); +case CRITICAL_DATA : +return ((rule->func == func) && +ima_match_rule_data (rule, rule->data_sources, + func_data, false, cred)); +default : +break ; + } +if ((rule->flags &IMA_MASK ) && (rule->mask != mask && func !=POST_SETATTR ))return false;
@@ -1113,6 +1122,19 @@ static bool ima_validate_rule(struct ima_rule_entry *entry)if (ima_rule_contains_lsm_cond (entry))return false; +break ; +case CRITICAL_DATA : +if (entry->action & ~(MEASURE |DONT_MEASURE )) +return false; + +if (!(entry->flags &IMA_DATA_SOURCES ) || + (entry->flags & ~(IMA_FUNC |IMA_UID |IMA_PCR | +IMA_DATA_SOURCES ))) +return false; + +if (ima_rule_contains_lsm_cond (entry)) +return false; +break ;default :return false;
@@ -1245,6 +1267,8 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)else if (IS_ENABLED (CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS ) &&strcmp (args[0 ].from,"KEY_CHECK" ) ==0 ) entry->func =KEY_CHECK ; +else if (strcmp (args[0 ].from,"CRITICAL_DATA" ) ==0 ) + entry->func =CRITICAL_DATA ;else result = -EINVAL ;if (!result)
Signature · 1 line
2.17.1
PATCH v3 6/6 IMA: validate supported kernel data sources before measurement
In reply to Tushar SugandhiCurrently, IMA does not restrict random data sources from measuring their data using ima_measure_critical_data(). Any kernel data source can call the function, and it's data will get measured as long as the input event_data_source is part of the IMA policy - CRITICAL_DATA+critical_kernel_data_sources. This may result in IMA log getting bloated by random data sources. Supporting random data sources at run-time may also impact the reliability of the system.
To ensure that only data from supported sources are measured, the kernel component needs to be added to a compile-time list of supported sources (an "allowed list of components") in ima.h. IMA then validates the input parameter - event_data_source passed to ima_measure_critical_data() against this allowed list at run-time.
Provide an infrastructure for kernel data sources to be added to the supported data sources list at compile-time. Update ima_measure_critical_data() to validate, at run-time, that the data source is supported before measuring the data.
--- security/integrity/ima/ima.h | 29 +++++++++++++++++++++++++++++ security/integrity/ima/ima_main.c | 3 +++ 2 files changed, 32 insertions(+)
security/integrity/ima/ima.h +29 -0
@@ -228,6 +228,35 @@ extern const char *const func_tokens[];struct modsig ; +#define __ima_supported_kernel_data_sources (source) \ + source(MIN_SOURCE, min_source) \ + source(MAX_SOURCE, max_source) + +#define __ima_enum_stringify (ENUM , str) (#str), + +enum ima_supported_kernel_data_sources { + __ima_supported_kernel_data_sources(__ima_hook_enumify) +}; + +static const char *const ima_supported_kernel_data_sources_str[] = { +__ima_supported_kernel_data_sources (__ima_enum_stringify) +}; + +static inline bool ima_kernel_data_source_is_supported (const char *source) +{ +int i; + +if (!source) +return false; + +for (i =MIN_SOURCE +1 ; i <MAX_SOURCE ; i++) { +if (!strcmp (ima_supported_kernel_data_sources_str[i], source)) +return true; + } + +return false; +} +#ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS /* * To track keys that need to be measured.
security/integrity/ima/ima_main.c +3 -0
@@ -888,6 +888,9 @@ int ima_measure_critical_data(const char *event_name,if (!event_name || !event_data_source || !buf || !buf_len)return -EINVAL ; +if (!ima_kernel_data_source_is_supported (event_data_source)) +return -EPERM ; +return process_buffer_measurement (NULL , buf, buf_len, event_name,CRITICAL_DATA ,0 , event_data_source, measure_buf_hash);
Signature · 1 line
2.17.1
Mimi Zohar
maintainerRe: [PATCH v3 2/6] IMA: change process_buffer_measurement return type from void to int
In reply to Tushar SugandhiQuoted 4 lines · Tushar Sugandhi
process_buffer_measurement()does not return the result of the operation. Therefore, the consumers of this function cannot act on it, if needed.Update return type of
process_buffer_measurement()from void to int.
Failure to measure may be audited, but should never fail. This is one of the main differences between secure and trusted boot concepts. Notice in process_measurement() that -EACCES is only returned for appraisal.
Returning a failure from process_buffer_measurement() in itself isn't a problem, as long as the failure isn't returned to the LSM/IMA hook. However, just as the callers of process_measurement() originally processed the result, that processing was moved into process_measurement() [1].
Mimi
[1] 750943a30714 ima: remove enforce checking duplication
Quoted 77 lines
Signed-off-by Tushar Sugandhi <[email protected]>--- security/integrity/ima/ima.h | 6 +++--- security/integrity/ima/ima_main.c | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-)security/integrity/ima/ima.h +3
-3@@ -265,9 +265,9 @@ void ima_store_measurement(struct integrity_iint_cache *iint, struct file *file,struct evm_ima_xattr_data *xattr_value, int xattr_len, const struct modsig *modsig, int pcr, struct ima_template_desc *template_desc); -void process_buffer_measurement (struct inode *inode,const void *buf,int size, -const char *eventname,enum ima_hooks func, -int pcr,const char *func_data); +int process_buffer_measurement (struct inode *inode,const void *buf,int size, +const char *eventname,enum ima_hooks func, +int pcr,const char *func_data);void ima_audit_measurement (struct integrity_iint_cache *iint,const unsigned char *filename); int ima_alloc_init_template(struct ima_event_data *event_data,security/integrity/ima/ima_main.c +7
-7@@ -736,9 +736,9 @@ int ima_load_data(enum kernel_load_data_id id)* * Based on policy, the buffer is measured into the ima log. */ -void process_buffer_measurement (struct inode *inode,const void *buf,int size, -const char *eventname,enum ima_hooks func, -int pcr,const char *func_data) +int process_buffer_measurement (struct inode *inode,const void *buf,int size, +const char *eventname,enum ima_hooks func, +int pcr,const char *func_data) {int ret =0 ;const char *audit_cause ="ENOMEM" ;@@ -758,7 +758,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size,u32 secid;if (!ima_policy_flag) -return ; +return 0 ;/* * Both LSM hooks and auxilary based buffer measurements are @@ -772,7 +772,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size, action =ima_get_action (inode,current_cred (), secid,0 , func, &pcr, &template, func_data);if (!(action &IMA_MEASURE )) -return ; +return 0 ; }if (!pcr)@@ -787,7 +787,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size,pr_err ("template %s init failed, result: %d\n" , (strlen (template->name) ? template->name : template->fmt), ret); -return ; +return ret; } }@@ -819,7 +819,7 @@ void process_buffer_measurement(struct inode *inode, const void *buf, int size,func_measure_str (func), audit_cause, ret, 0, ret); -return ; +return ret; }/**
Mimi Zohar
maintainerRe: [PATCH v3 1/6] IMA: generalize keyring specific measurement constructs
In reply to Tushar SugandhiQuoted 7 lines · Tushar Sugandhi
IMA functions such as
ima_match_keyring(),process_buffer_measurement(),ima_match_policy()etc. handle data specific to keyrings. Currently, these constructs are not generic to handle any func specific data. This makes it harder to extend without code duplication.Refactor the keyring specific measurement constructs to be generic and reusable in other measurement scenarios.
Mostly this patch changes the variable name from keyring to func_data, which is good. Other changes should be minimized.
Quoted 3 lines
Signed-off-by Tushar Sugandhi <[email protected]>---
<snip>
Quoted 16 lines
security/integrity/ima/ima_policy.c +5
-3@@ -451,15 +451,21 @@ int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, }/** -* ima_match_keyring - determine whether the keyring matches the measure rule -* @rule: a pointer to a rule -* @keyring: name of the keyring to match against the measure rule +* ima_match_rule_data - determine whether the given func_data matches +* the measure rule data +* @rule: IMA policy rule +* @opt_list: rule data to match func_data against +* @func_data: data to match against the measure rule data
security/integrity/ima/ima_policy.c int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, · line 465
Quoted 10 lines
security/integrity/ima/ima_policy.c +4
-3+* @allow_empty_opt_list: If true matches all func_data * @cred: a pointer to a credentials structure for user validation * -* Returns true if keyring matches one in the rule, false otherwise. +* Returns true if func_data matches one in the rule, false otherwise. */ -static bool ima_match_keyring (struct ima_rule_entry *rule, -const char *keyring,const struct cred *cred) +static bool ima_match_rule_data(struct ima_rule_entry *rule, + const struct ima_rule_opt_list *opt_list,
Ok
security/integrity/ima/ima_policy.c int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, · line 467
Quoted 2 lines
security/integrity/ima/ima_policy.c +2
-0+const char *func_data, + bool allow_empty_opt_list,
As the policy is loaded, shouldn't the rules should be checked, not here on usage?
Mimi
Quoted 5 lines
security/integrity/ima/ima_policy.c +1
-0+ const struct cred *cred) {bool matched = false;size_t i;
Mimi Zohar
maintainerRe: [PATCH v3 3/6] IMA: update process_buffer_measurement to measure buffer hash
In reply to Tushar SugandhiQuoted 3 lines · Tushar Sugandhi
process_buffer_measurement()currently only measures the input buffer. When the buffer being measured is too large, it may result in bloated IMA logs.
The subject of this sentence refers to an individual record, while "bloated" refers to the measurement list. A "bloated" measurement list would contain too many or unnecessary records. Your concern seems to be with the size of the individual record, not the number of measurement list entries.
Measuring the hash of the buffer data is similar to measuring the file data. In the case of the file data, however, the attestation server may rely on a white list manifest/DB or the file signature to verify the file data hash. For buffer measurements, how will the attestation server ascertain what is a valid buffer hash?
Hint: I assume, correct me if I'm wrong, the measurement list record
template data is not meant to be verified, but used to detect if the "critical data" changed.
Please update the patch description accordingly.
Quoted 6 lines
Introduce a boolean parameter
measure_buf_hashto support measuring hash of a buffer, which would be much smaller, instead of the buffer itself.Signed-off-by Tushar Sugandhi <[email protected]>---
<snip>
Quoted 45 lines
+++
b/security/integrity/ima/ima_main.csecurity/integrity/ima/ima_main.c +14
-2@@ -733,17 +733,21 @@ int ima_load_data(enum kernel_load_data_id id)* @func: IMA hook * @pcr: pcr to extend the measurement * @func_data: private data specific to @func, can be NULL. +* @measure_buf_hash: if set to true - will measure hash of the buf, +* instead of buf * * Based on policy, the buffer is measured into the ima log. */ int process_buffer_measurement (struct inode *inode,const void *buf,int size,const char *eventname,enum ima_hooks func, -int pcr,const char *func_data) +int pcr,const char *func_data, +bool measure_buf_hash) {int ret =0 ;const char *audit_cause ="ENOMEM" ;struct ima_template_entry *entry =NULL ;struct integrity_iint_cache iint = {}; +struct integrity_iint_cache digest_iint = {};struct ima_event_data event_data = {.iint = &iint, .filename = eventname, .buf = buf,@@ -752,7 +756,7 @@ int process_buffer_measurement(struct inode *inode, const void *buf, int size,struct {struct ima_digest_data hdr;char digest[IMA_MAX_DIGEST_SIZE ]; - } hash = {}; + } hash = {}, digest_hash = {};int violation =0 ;int action =0 ;u32 secid;@@ -801,6 +805,24 @@ int process_buffer_measurement(struct inode *inode, const void *buf, int size, goto out; } +if (measure_buf_hash) { + digest_iint.ima_hash = &digest_hash.hdr; + digest_iint.ima_hash->algo = ima_hash_algo; + digest_iint.ima_hash->length = hash_digest_size[ima_hash_algo]; + + ret =ima_calc_buffer_hash (hash.hdr.digest, + iint.ima_hash->length, + digest_iint.ima_hash);
security/integrity/ima/ima_main.c int process_buffer_measurement(struct inode *inode, const void *buf, int size, · line 825
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+if (ret <0 ) { + audit_cause ="digest_hashing_error" ; + goto out; + } + + event_data.iint = &digest_iint; + event_data.buf = hash.hdr.digest; + event_data.buf_len = iint.ima_hash->length; + } +
There seems to be some code and variable duplication by doing it this way. Copying the caluclated buffer data hash to a temporary buffer might eliminate it.
Quoted 3 lines
security/integrity/ima/ima_main.c +0
-0ret =ima_alloc_init_template (&event_data, &entry, template);if (ret <0 ) { audit_cause ="alloc_entry" ;
Mimi Zohar
maintainerRe: [PATCH v3 4/6] IMA: add policy to measure critical data from kernel components
In reply to Tushar SugandhiQuoted 6 lines · Tushar Sugandhi
There would be several candidate kernel components suitable for IMA measurement. Not all of them would have support for IMA measurement. Also, system administrators may not want to measure data for all of them, even when they support IMA measurement. An IMA policy specific to various kernel components is needed to measure their respective critical data.
The base policy rules are wide, but may be constrained by specifying different options. For example the builtin policy rules cannot be written in terms LSM labels, which would constrain them. A policy rule may measure all keyrings or may constrain which keyrings need to be measured. Measuring critical data is not any different.
Please rewrite the above paragraph accordingly.
Quoted 5 lines
Add a new IMA policy "
critical_kernel_data_sources" to support measuring various critical kernel components. This policy would enable the system administrators to limit the measurement to the components, if the components support IMA measurement.
"critical_kernel_data_sources" is really wordy. Find a better, self
defining term for describing the type of data, one that isn't so wordy, and reflect it in the code.
Quoted 12 lines
Signed-off-by Tushar Sugandhi <[email protected]>--- Documentation/ABI/testing/ima_policy | 3 +++ security/integrity/ima/ima_policy.c | 29 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-)Documentation/ABI/testing/ima_policy +0
-0
Documentation/ABI/testing/ima_policy Description: · line 53
Quoted 6 lines
Documentation/ABI/testing/ima_policy +3
-0@@ -48,6 +48,9 @@ Description: template:= name of a defined IMA template type (eg, ima-ng). Only valid when action is "measure". pcr:= decimal value + critical_kernel_data_sources:= list of kernel + components (eg, selinux|apparmor|dm-crypt) that + contain data critical to the security of the kernel.
This original policy definition, for the most part, is in Backus–Naur
format. The keyring names is an exception, because it is not limited
to pre-defined kernel objects. The critical data hook is measuring things in kernel memory. As new calls to measure critical data are added, new identifiers would be added here.
For example, if SELinux is the first example of measuring critical data, then the SELinux critical data patch would include "critical_data:= [selinux]". Each subsequent critical data being measured would extend this list. At the same time, the list of known "critical data" defined in patch 6/6 would be updated.
Normally a new feature and the first usage of that feature are included in the same patch set. Separating them like this makes it difficult to write, review and upstream.
Mimi
Mimi Zohar
maintainerRe: [PATCH v3 5/6] IMA: add hook to measure critical data from kernel components
In reply to Tushar SugandhiQuoted 20 lines
security/integrity/ima/ima_main.c +12
-0@@ -869,6 +869,30 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)fdput (f); } +/** +* ima_measure_critical_data - measure critical data +* @event_name: name for the given data +* @event_data_source: name of the event data source +* @buf: pointer to buffer containing data to measure +* @buf_len: length of buffer(in bytes) +* @measure_buf_hash: if set to true - will measure hash of the buf, +* instead of buf +* +* Buffers can only be measured, not appraised. +*/ +int ima_measure_critical_data(const char *event_name,
security/integrity/ima/ima_main.c void ima_kexec_cmdline(int kernel_fd, const void *buf, int size) · line 893
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+const char *event_data_source, + const void *buf, int buf_len, + bool measure_buf_hash) +{ + if (!event_name || !event_data_source || !buf || !buf_len) + return -EINVAL; + +return process_buffer_measurement (NULL , buf, buf_len, event_name, +CRITICAL_DATA ,0 , event_data_source, + measure_buf_hash);
This is exactly what I'm concerned about. Failure to measure data may be audited, but should never fail.
Mimi
Quoted 1 line
security/integrity/ima/ima_main.c +1
-0+}
Re: [PATCH v3 1/6] IMA: generalize keyring specific measurement constructs
In reply to Mimi ZoharQuoted 6 lines · Mimi Zohar
Quoted 5 lines · Tushar Sugandhi
IMA functions such as
ima_match_keyring(),process_buffer_measurement(),ima_match_policy()etc. handle data specific to keyrings. Currently, these constructs are not generic to handle any func specific data. This makes it harder to extend without code duplication.
Quoted 6 lines
Quoted 2 lines
Refactor the keyring specific measurement constructs to be generic and reusable in other measurement scenarios.
Mostly this patch changes the variable name from keyring to
func_data, which is good. Other changes should be minimized.
The only other change in this patch is introduction of bool allow_empty_opt_list, which is needed as per my comment below.
Maybe I can move "allow_empty_opt_list" to a new patch after this one in this series.
Quoted 35 lines
Quoted 3 lines
Signed-off-by Tushar Sugandhi <[email protected]>---
<snip>
Quoted 16 lines
security/integrity/ima/ima_policy.c +5
-3@@ -451,15 +451,21 @@ int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, }/** -* ima_match_keyring - determine whether the keyring matches the measure rule -* @rule: a pointer to a rule -* @keyring: name of the keyring to match against the measure rule +* ima_match_rule_data - determine whether the given func_data matches +* the measure rule data +* @rule: IMA policy rule +* @opt_list: rule data to match func_data against +* @func_data: data to match against the measure rule data security/integrity/ima/ima_policy.c int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, · line 465
Quoted 10 lines
security/integrity/ima/ima_policy.c +4
-3+* @allow_empty_opt_list: If true matches all func_data * @cred: a pointer to a credentials structure for user validation * -* Returns true if keyring matches one in the rule, false otherwise. +* Returns true if func_data matches one in the rule, false otherwise. */ -static bool ima_match_keyring (struct ima_rule_entry *rule, -const char *keyring,const struct cred *cred) +static bool ima_match_rule_data(struct ima_rule_entry *rule, + const struct ima_rule_opt_list *opt_list,Ok
Quoted 7 lines
security/integrity/ima/ima_policy.c int ima_lsm_policy_change(struct notifier_block *nb, unsigned long event, · line 467
Quoted 2 lines
security/integrity/ima/ima_policy.c +2
-0+const char *func_data, + bool allow_empty_opt_list,As the policy is loaded, shouldn't the rules should be checked, not here on usage?
Mimi
Since "keyrings=" is optional, I cannot check the rule at load time for keyrings. func=KEY_CHECK may or may not have "keyrings=", and both are valid scenarios.
However "critical_kernel_data_sources=" is mandatory for func=CRITICAL_DATA.
So I am already making that check at policy load time.
See patch 5/6 – function ima_match_rules(), where I check for IMA_DATA_SOURCES.
+ case CRITICAL_DATA:
<snip>
+ if (!(entry->flags & IMA_DATA_SOURCES) ||
<snip>
+ return false;
+
Since ima_match_rule_data (this function) handles both func=KEY_CHECK and func=CRITICAL_DATA, we have to use the bool "allow_empty_opt_list" to differentiate between the two scenarios – whether the rule is
optional or not for a given func.
Quoted 6 lines
Quoted 6 lines
security/integrity/ima/ima_policy.c +1
-0+ const struct cred *cred) {bool matched = false;size_t i;
Re: [PATCH v3 2/6] IMA: change process_buffer_measurement return type from void to int
In reply to Mimi ZoharQuoted 17 lines · Mimi Zohar
Quoted 4 lines · Tushar Sugandhi
process_buffer_measurement()does not return the result of the operation. Therefore, the consumers of this function cannot act on it, if needed.Update return type of
process_buffer_measurement()from void to int.Failure to measure may be audited, but should never fail. This is one of the main differences between secure and trusted boot concepts. Notice in
process_measurement()that-EACCESis only returned for appraisal.Returning a failure from
process_buffer_measurement()in itself isn't a problem, as long as the failure isn't returned to the LSM/IMA hook. However, just as the callers ofprocess_measurement()originally processed the result, that processing was moved intoprocess_measurement()[1].
Quoted 4 lines
Mimi
[1]
750943a30714ima: remove enforce checking duplication
I can ignore the result of process_buffer_measurement() in ima_measure_critical_data(), and make ima_measure_critical_data() return type "void".
But currently ima_measure_critical_data() is the only place where the results of p_b_m() are being used.
So I might as well just revert back the return type of p_b_m() to the original "void".
Re: [PATCH v3 3/6] IMA: update process_buffer_measurement to measure buffer hash
In reply to Mimi ZoharQuoted 11 lines · Mimi Zohar
Quoted 3 lines · Tushar Sugandhi
process_buffer_measurement()currently only measures the input buffer. When the buffer being measured is too large, it may result in bloated IMA logs.The subject of this sentence refers to an individual record, while "bloated" refers to the measurement list. A "bloated" measurement list would contain too many or unnecessary records. Your concern seems to be with the size of the individual record, not the number of measurement list entries.
The intent behind that description was twofold. One, as you mentioned, the individual record size being large; and two, multiple large-sized individual records will eventually bloat the measurement list too.
It can happen in SeLinux case as we discovered. The SeLinux policy (which can be a few MBs) can change from 'foo', to 'bar', back to 'foo'. And the requirement from SeLinux is that 'foo' should be measured the second time too. When 'foo' and 'bar' are large, the individual records in the ima log will be large, which will also result in measurement list being bloated.
But I understand your concern with the current wording. I will update the patch description accordingly.
Quoted 5 lines
Measuring the hash of the buffer data is similar to measuring the file data. In the case of the file data, however, the attestation server may rely on a white list manifest/DB or the file signature to verify the file data hash. For buffer measurements, how will the attestation server ascertain what is a valid buffer hash?
The client and the server implementation will go hand in hand. The client/kernel would know what data is measured as-is
(e.g. KEXEC_CMDLINE), and what data has it’s hash measured (e.g. SeLinux Policy). And the attestation server would verify data/hash accordingly.
Just like the data being measured in other cases, the attestation server will know what are possible values of the large buffers being measured. It will have to maintain the hash of those buffer values.
Quoted 4 lines
Hint: I assume, correct me if I'm wrong, the measurement list record
template data is not meant to be verified, but used to detect if the "critical data" changed.
As mentioned above, the use case for this feature is in-memory loaded SeLinux policy, which can be quite large (several MBs) – that's why this functionality. The data is meant to be verified by the attestation server.
Quoted 1 line
Please update the patch description accordingly.
I will update the patch description to clarify all this.
Quoted 61 lines
Quoted 7 lines
Introduce a boolean parameter
measure_buf_hashto support measuring hash of a buffer, which would be much smaller, instead of the buffer itself.Signed-off-by Tushar Sugandhi <[email protected]>---
<snip>
Quoted 51 lines
+++
b/security/integrity/ima/ima_main.csecurity/integrity/ima/ima_main.c +20
-2@@ -733,17 +733,21 @@ int ima_load_data(enum kernel_load_data_id id)* @func: IMA hook * @pcr: pcr to extend the measurement * @func_data: private data specific to @func, can be NULL. +* @measure_buf_hash: if set to true - will measure hash of the buf, +* instead of buf * * Based on policy, the buffer is measured into the ima log. */ int process_buffer_measurement (struct inode *inode,const void *buf,int size,const char *eventname,enum ima_hooks func, -int pcr,const char *func_data) +int pcr,const char *func_data, +bool measure_buf_hash) {int ret =0 ;const char *audit_cause ="ENOMEM" ;struct ima_template_entry *entry =NULL ;struct integrity_iint_cache iint = {}; +struct integrity_iint_cache digest_iint = {};struct ima_event_data event_data = {.iint = &iint, .filename = eventname, .buf = buf,@@ -752,7 +756,7 @@ int process_buffer_measurement(struct inode *inode, const void *buf, int size,struct {struct ima_digest_data hdr;char digest[IMA_MAX_DIGEST_SIZE ]; - } hash = {}; + } hash = {}, digest_hash = {};int violation =0 ;int action =0 ;u32 secid;@@ -801,6 +805,24 @@ int process_buffer_measurement(struct inode *inode, const void *buf, int size, goto out; } +if (measure_buf_hash) { + digest_iint.ima_hash = &digest_hash.hdr; + digest_iint.ima_hash->algo = ima_hash_algo; + digest_iint.ima_hash->length = hash_digest_size[ima_hash_algo]; + + ret =ima_calc_buffer_hash (hash.hdr.digest, + iint.ima_hash->length, + digest_iint.ima_hash); +if (ret <0 ) { + audit_cause ="digest_hashing_error" ; + goto out; + } + + event_data.iint = &digest_iint;
Quoted 9 lines
security/integrity/ima/ima_main.c int process_buffer_measurement(struct inode *inode, const void *buf, int size, · line 825
Quoted 4 lines
security/integrity/ima/ima_main.c +4
-0+ event_data.buf = hash.hdr.digest; + event_data.buf_len = iint.ima_hash->length; + } +There seems to be some code and variable duplication by doing it this way. Copying the caluclated buffer data hash to a temporary buffer might eliminate it.
With the way ima_calc_buffer_hash() is implemented, I was convinced that
the variable duplication was needed. I didn't want to write a helper function in order to minimize the unnecessary code churn in p_b_m(). But I will revisit this to see how I can reduce the code and variable duplication.
Thanks for the feedback.
Quoted 3 lines
Quoted 3 lines
security/integrity/ima/ima_main.c +0
-0ret =ima_alloc_init_template (&event_data, &entry, template);if (ret <0 ) { audit_cause ="alloc_entry" ;
Re: [PATCH v3 4/6] IMA: add policy to measure critical data from kernel components
In reply to Mimi ZoharQuoted 8 lines · Mimi Zohar
Quoted 6 lines · Tushar Sugandhi
There would be several candidate kernel components suitable for IMA measurement. Not all of them would have support for IMA measurement. Also, system administrators may not want to measure data for all of them, even when they support IMA measurement. An IMA policy specific to various kernel components is needed to measure their respective critical data.
Quoted 8 lines
The base policy rules are wide, but may be constrained by specifying different options. For example the builtin policy rules cannot be written in terms LSM labels, which would constrain them. A policy rule may measure all keyrings or may constrain which keyrings need to be measured. Measuring critical data is not any different.
Please rewrite the above paragraph accordingly.
Ok. Will do.
Quoted 10 lines
Quoted 5 lines
Add a new IMA policy "
critical_kernel_data_sources" to support measuring various critical kernel components. This policy would enable the system administrators to limit the measurement to the components, if the components support IMA measurement."
critical_kernel_data_sources" is really wordy. Find a better, self
defining term for describing the type of data, one that isn't so wordy, and reflect it in the code.
Will do. I will go with "critical_data". You also have suggested it in the comment below.
"critical_data_sources" also seems right, but that's more wordy than "critical_data".
Some more options we considered, but they don’t sound right. Please let us know what do you think.
1. "critical_data_sources="
2. "critical_kernel_components=" -or- "crit_krnl_comps="
3. "critical_data_providers="
4. "critical_kernel_data_providers=" -or- "crit_krnl_dt_provs="
5. "critical_kernel_data_sources=" -or- "crit_krnl_dt_srcs="
6. "security_critical_data="
7. "protectable_data="
8. "protected_data="
9. "vital_protected_data="
Quoted 31 lines
Quoted 12 lines
Signed-off-by Tushar Sugandhi <[email protected]>--- Documentation/ABI/testing/ima_policy | 3 +++ security/integrity/ima/ima_policy.c | 29 +++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-)Documentation/ABI/testing/ima_policy +0
-0Documentation/ABI/testing/ima_policy Description: · line 53
Quoted 6 lines
Documentation/ABI/testing/ima_policy +3
-0@@ -48,6 +48,9 @@ Description: template:= name of a defined IMA template type (eg, ima-ng). Only valid when action is "measure". pcr:= decimal value + critical_kernel_data_sources:= list of kernel + components (eg, selinux|apparmor|dm-crypt) that + contain data critical to the security of the kernel.This original policy definition, for the most part, is in Backus–Naur
format. The keyring names is an exception, because it is not limited
to pre-defined kernel objects. The critical data hook is measuring things in kernel memory. As new calls to measure critical data are added, new identifiers would be added here.For example, if SELinux is the first example of measuring critical data, then the SELinux critical data patch would include "critical_data:= [selinux]". Each subsequent critical data being measured would extend this list. At the same time, the list of known "critical data" defined in patch 6/6 would be updated.
Quoted 6 lines
Normally a new feature and the first usage of that feature are included in the same patch set. Separating them like this makes it difficult to write, review and upstream.
Mimi
I agree. But the unique issue we are facing here is there are two "first users" of this new "base series".
One, SeLinux work (driven by Lakshmi); and two, device-mapper/dm-crypt work (driven by me).
Both of them need to be reviewed by different maintainers, may go through several iterations before getting accepted.
That’s why we wanted to keep this "base series" independent of the "first users"; and called the "base series" as a dependency in the dm-crypt[1] / SeLinux[2] series.
We would appreciate your guidance on how we can better author these three series - 1.this base series 2. dm-crypt series and 3. SeLinux series.
[1]dm-crypt Series: https://patchwork.kernel.org/patch/11743715/
[2]SeLinux Series: https://patchwork.kernel.org/patch/11762287/
Re: [PATCH v3 5/6] IMA: add hook to measure critical data from kernel components
In reply to Mimi ZoharQuoted 34 lines · Mimi Zohar
Quoted 20 lines
security/integrity/ima/ima_main.c +12
-0@@ -869,6 +869,30 @@ void ima_kexec_cmdline(int kernel_fd, const void *buf, int size)fdput (f); } +/** +* ima_measure_critical_data - measure critical data +* @event_name: name for the given data +* @event_data_source: name of the event data source +* @buf: pointer to buffer containing data to measure +* @buf_len: length of buffer(in bytes) +* @measure_buf_hash: if set to true - will measure hash of the buf, +* instead of buf +* +* Buffers can only be measured, not appraised. +*/ +int ima_measure_critical_data(const char *event_name,security/integrity/ima/ima_main.c void ima_kexec_cmdline(int kernel_fd, const void *buf, int size) · line 893
Quoted 10 lines
security/integrity/ima/ima_main.c +10
-0+const char *event_data_source, + const void *buf, int buf_len, + bool measure_buf_hash) +{ + if (!event_name || !event_data_source || !buf || !buf_len) + return -EINVAL; + +return process_buffer_measurement (NULL , buf, buf_len, event_name, +CRITICAL_DATA ,0 , event_data_source, + measure_buf_hash);This is exactly what I'm concerned about. Failure to measure data may be audited, but should never fail.
Quoted 2 lines
Mimi
As I responded in patch 2, I can ignore the result of process_buffer_measurement() in ima_measure_critical_data(), and make ima_measure_critical_data() return type as "void".
But that’s the only place where the results of p_b_m() are being used. So I might as well just revert the return type of p_b_m() to the original "void".
Quoted 2 lines
Quoted 1 line
security/integrity/ima/ima_main.c +1
-0+}