Skip to contentSkip to conversation

IMA: Infrastructure for measurement of critical kernel data

PATCH Superseded Tushar Sugandhi 2 participants 8 messages Aug 21, 2020 – Aug 25, 2020

Subsystems (6)

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v2 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.

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

This series also has a dependency on the following patch series:
https://patchwork.kernel.org/patch/11709527/

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 (3):
IMA: generalize keyring specific measurement constructs
IMA: add policy to support measuring critical data from kernel
components
IMA: define IMA hook to measure critical data from kernel components

 Documentation/ABI/testing/ima_policy         |   6 +-
 include/linux/ima.h                          |  11 ++
 security/integrity/ima/ima.h                 |  12 ++-
 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            |  69 +++++++++++--
 security/integrity/ima/ima_policy.c          | 100 +++++++++++++++----
 security/integrity/ima/ima_queue_keys.c      |   3 +-
 9 files changed, 167 insertions(+), 46 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/3 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
index 38043074ce5e..8875085db689 100644
@@ -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
index 4f39fb93f278..af218babd198 100644
@@ -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
index 8a91711ca79b..c870fd6d2f83 100644
@@ -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
#2 lore raw

PATCH v2 2/3 IMA: add policy to support measuring critical data from kernel components

In reply to 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.

Add a new IMA policy CRITICAL_DATA+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 |  6 ++-
 security/integrity/ima/ima.h         |  1 +
 security/integrity/ima/ima_api.c     |  2 +-
 security/integrity/ima/ima_policy.c  | 62 +++++++++++++++++++++++++---
 4 files changed, 63 insertions(+), 8 deletions(-)
Documentation/ABI/testing/ima_policy +5 -1
index cd572912c593..a0dd0f108555 100644
@@ -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
@@ -125,3 +125,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 data_sources=selinux|apparmor|dm-crypt
security/integrity/ima/ima.h +1 -0
index 8875085db689..0f4209a92bfb 100644
@@ -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
index af218babd198..9917e1730cb6 100644
@@ -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_policy.c +56 -6

119 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#3 lore raw

Re: [PATCH v2 2/3] IMA: add policy to support measuring critical data from kernel components

Quoted 34 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.

Add a new IMA policy CRITICAL_DATA+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 |  6 ++-
 security/integrity/ima/ima.h         |  1 +
 security/integrity/ima/ima_api.c     |  2 +-
 security/integrity/ima/ima_policy.c  | 62 +++++++++++++++++++++++++---
 4 files changed, 63 insertions(+), 8 deletions(-)
Documentation/ABI/testing/ima_policy +1 -1
index cd572912c593..a0dd0f108555 100644
@@ -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

Documentation/ABI/testing/ima_policy Description: · line 131

Quoted 7 lines
Documentation/ABI/testing/ima_policy +4 -0
@@ -125,3 +125,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 data_sources=selinux|apparmor|dm-crypt

This example uses "data_sources" without first defining it in the "option:" section. Defining two new options is an indication that this patch should be split up. One which defines the "CRITICAL_DATA" and another one which defines the new key value pair. The term "data_sources" is pretty generic. Perhaps constrain it a bit by re- naming it "critical_data=". Or was such using a generic name intentional?

Normally "CRITICAL_DATA" would be defined with the critical data hook, but that seems to be defined in patch 3/3 "IMA: define IMA hook to measure critical data from kernel components".

Quoted 94 lines
security/integrity/ima/ima.h +1 -0
index 8875085db689..0f4209a92bfb 100644
@@ -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
index af218babd198..9917e1730cb6 100644
@@ -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_policy.c +18 -6
index 8866e84d0062..7b649095ac7a 100644
@@ -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;
 };
 
@@ -508,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;
@@ -911,7 +922,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 +959,7 @@ static const match_table_t policy_tokens = {
 	{Opt_pcr, "pcr=%s"},
 	{Opt_template, "template=%s"},
 	{Opt_keyrings, "keyrings=%s"},
+	{Opt_data_sources, "data_sources=%s"},
 	{Opt_err, NULL}
 };
 
@@ -1110,6 +1122,19 @@ static bool ima_validate_rule(struct ima_rule_entry *entry)
 		if (ima_rule_contains_lsm_cond(entry))
 			return false;

security/integrity/ima/ima_policy.c static bool ima_validate_rule(struct ima_rule_entry *entry) · line 1133

Quoted 10 lines
security/integrity/ima/ima_policy.c +9 -0
 
+		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;

Requiring IMA_FUNC and IMA_DATA_SOURCES makes sense, but why are IMA_UID and IMA_PCR required?

Quoted 25 lines
security/integrity/ima/ima_policy.c +11 -0
+
+		if (ima_rule_contains_lsm_cond(entry))
+			return false;
+
 		break;
 	default:
 		return false;
@@ -1242,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)
@@ -1312,6 +1339,23 @@ 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, "data_sources", args[0].from);
+
+			if (entry->data_sources) {
+				result = -EINVAL;

security/integrity/ima/ima_policy.c static int ima_parse_rule(char *rule, struct ima_rule_entry *entry) · line 1356

Quoted 10 lines
security/integrity/ima/ima_policy.c +10 -0
+				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;
+			}
+

"keyrings=" isn't bounded because keyrings can be created by userspace. Perhaps keyring names has a minimum/maximum length. IMA isn't measuring userspace construsts. Shouldn't the list of critical data being measured be bounded and verified?

Mimi

Quoted 18 lines
security/integrity/ima/ima_policy.c +8 -0
+			entry->flags |= IMA_DATA_SOURCES;
+			break;
 		case Opt_fsuuid:
 			ima_log_string(ab, "fsuuid", args[0].from);
 
@@ -1692,6 +1736,12 @@ int ima_policy_show(struct seq_file *m, void *v)
 		seq_puts(m, " ");
 	}
 
+	if (entry->flags & IMA_DATA_SOURCES) {
+		seq_puts(m, "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);
#4 lore raw

Re: [PATCH v2 2/3] IMA: add policy to support measuring critical data from kernel components

Quoted 37 lines · Mimi Zohar
Quoted 36 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.

Add a new IMA policy CRITICAL_DATA+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 |  6 ++-
  security/integrity/ima/ima.h         |  1 +
  security/integrity/ima/ima_api.c     |  2 +-
  security/integrity/ima/ima_policy.c  | 62 +++++++++++++++++++++++++---
  4 files changed, 63 insertions(+), 8 deletions(-)
Documentation/ABI/testing/ima_policy +1 -1
index cd572912c593..a0dd0f108555 100644
@@ -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
@@ -125,3 +125,7 @@ Description:
  		keys added to .builtin_trusted_keys or .ima keyring:
 
Quoted 8 lines

Documentation/ABI/testing/ima_policy Description: · line 131

Quoted 5 lines
Documentation/ABI/testing/ima_policy +4 -0
  			measure func=KEY_CHECK keyrings=.builtin_trusted_keys|.ima
+
+		Example of measure rule using CRITICAL_DATA to measure critical data
+
+			measure func=CRITICAL_DATA data_sources=selinux|apparmor|dm-crypt

This example uses "data_sources" without first defining it in the "option:" section. Defining two new options is an indication that this

Thanks. I will define "data_sources" first in "option:" section.

Quoted 2 lines

patch should be split up. One which defines the "CRITICAL_DATA" and another one which defines the new key value pair. The term

I intentionally kept the "CRITICAL_DATA" and "data_sources" in the same patch.

CRITICAL_DATA is different than KEY_CHECK because in case of KEY_CHECK, "keyrings=" is optional. If "keyrings=" is not specified, then we measure all keyrings.

Where for CRITICAL_DATA, "data_sources=" is mandatory.

Because the data sources would be diverse and orthogonal to each other, (unlike "keyrings=") - not specifying "data_sources=" shouldn't result in IMA blindly measuring all data sources.

Since CRITICAL_DATA, and "data_sources=" go hand in hand, I wanted them to be part of the same patch.

Quoted 4 lines

"data_sources" is pretty generic. Perhaps constrain it a bit by re- naming it "critical_data=". Or was such using a generic name intentional?

We intentionally kept the name generic because the data to be measured could be coming from any kernel component with any granularity (from a single bool to megabytes of data). The kernel component is also loosely defined here. It could be an LSM (like SELinux), or a broader base layer (like device-mapper), or a specific module (like dm-crypt), or it could be different parts of a single module.

Also, we didn't want to name "data_sources" as "critical_data" to avoid confusion with func "CRITICAL_DATA".

Quoted 4 lines

Normally "CRITICAL_DATA" would be defined with the critical data hook, but that seems to be defined in patch 3/3 "IMA: define IMA hook to measure critical data from kernel components".

I can make the "CRITICAL_DATA" and the hook as part of the same patch. That would mean combining patch 2 and 3 into a single one.

Does it sound ok?

Quoted 95 lines
Quoted 95 lines
security/integrity/ima/ima.h +1 -0
index 8875085db689..0f4209a92bfb 100644
@@ -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
index af218babd198..9917e1730cb6 100644
@@ -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_policy.c +18 -6
index 8866e84d0062..7b649095ac7a 100644
@@ -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;
  };
 
@@ -508,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;
@@ -911,7 +922,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 +959,7 @@ static const match_table_t policy_tokens = {
  	{Opt_pcr, "pcr=%s"},
  	{Opt_template, "template=%s"},
  	{Opt_keyrings, "keyrings=%s"},
+	{Opt_data_sources, "data_sources=%s"},
  	{Opt_err, NULL}
  };
 
@@ -1110,6 +1122,19 @@ static bool ima_validate_rule(struct ima_rule_entry *entry)
  		if (ima_rule_contains_lsm_cond(entry))
  			return false;
 
Quoted 13 lines

security/integrity/ima/ima_policy.c static bool ima_validate_rule(struct ima_rule_entry *entry) · line 1133

Quoted 9 lines
security/integrity/ima/ima_policy.c +9 -0
+		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;

Requiring IMA_FUNC and IMA_DATA_SOURCES makes sense, but why are IMA_UID and IMA_PCR required?

Since the data to be measured could be for any scenario, I didn't want to restrict the kernel components from choosing UID to measure the data for, or restrict them from choosing PCR to store the measurements in. But as the consumers are kernel components, perhaps support for IMA_UID is not required. But we should still support IMA_PCR. Please let me know what do you think, and I can update the logic accordingly.

Quoted 32 lines
Quoted 32 lines
security/integrity/ima/ima_policy.c +18 -0
+
+		if (ima_rule_contains_lsm_cond(entry))
+			return false;
+
  		break;
  	default:
  		return false;
@@ -1242,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)
@@ -1312,6 +1339,23 @@ 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, "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;
Quoted 8 lines

security/integrity/ima/ima_policy.c static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode, · line 529

Quoted 3 lines
security/integrity/ima/ima_policy.c +3 -0
+				break;
+			}
+

"keyrings=" isn't bounded because keyrings can be created by userspace. Perhaps keyring names has a minimum/maximum length. IMA isn't measuring userspace construsts. Shouldn't the list of critical data being measured be bounded and verified?

The comment is not entirely clear.
Do you mean there should be some sort of allow_list in IMA, against which the values in "data_sources=" should be vetted? And if the value is present in the IMA allow_list, then only the measurements for that data source are allowed?

Or do you mean something else?

~Tushar

Quoted 22 lines

Mimi

Quoted 18 lines
security/integrity/ima/ima_policy.c +8 -0
+			entry->flags |= IMA_DATA_SOURCES;
+			break;
  		case Opt_fsuuid:
  			ima_log_string(ab, "fsuuid", args[0].from);
 
@@ -1692,6 +1736,12 @@ int ima_policy_show(struct seq_file *m, void *v)
  		seq_puts(m, " ");
  	}
 
+	if (entry->flags & IMA_DATA_SOURCES) {
+		seq_puts(m, "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);
maintainer
#5 lore raw

Re: [PATCH v2 2/3] IMA: add policy to support measuring critical data from kernel components

Quoted 57 lines · Tushar Sugandhi
Quoted 37 lines · Mimi Zohar
Quoted 36 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.

Add a new IMA policy CRITICAL_DATA+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 |  6 ++-
  security/integrity/ima/ima.h         |  1 +
  security/integrity/ima/ima_api.c     |  2 +-
  security/integrity/ima/ima_policy.c  | 62 +++++++++++++++++++++++++---
  4 files changed, 63 insertions(+), 8 deletions(-)
Documentation/ABI/testing/ima_policy +1 -1
index cd572912c593..a0dd0f108555 100644
@@ -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
@@ -125,3 +125,7 @@ Description:
  		keys added to .builtin_trusted_keys or .ima keyring:
 
Quoted 8 lines

Documentation/ABI/testing/ima_policy Description: · line 131

Quoted 5 lines
Documentation/ABI/testing/ima_policy +4 -0
  			measure func=KEY_CHECK keyrings=.builtin_trusted_keys|.ima
+
+		Example of measure rule using CRITICAL_DATA to measure critical data
+
+			measure func=CRITICAL_DATA data_sources=selinux|apparmor|dm-crypt

This example uses "data_sources" without first defining it in the "option:" section. Defining two new options is an indication that this

Thanks. I will define "data_sources" first in "option:" section.

Quoted 2 lines

patch should be split up. One which defines the "CRITICAL_DATA" and another one which defines the new key value pair. The term

I intentionally kept the "CRITICAL_DATA" and "data_sources" in the same patch.

CRITICAL_DATA is different than KEY_CHECK because in case of KEY_CHECK, "keyrings=" is optional. If "keyrings=" is not specified, then we measure all keyrings.

Quoted 5 lines

Where for CRITICAL_DATA, "data_sources=" is mandatory.

Because the data sources would be diverse and orthogonal to each other, (unlike "keyrings=") - not specifying "data_sources=" shouldn't result in IMA blindly measuring all data sources.

Good point.

Quoted 3 lines

Since CRITICAL_DATA, and "data_sources=" go hand in hand, I wanted them to be part of the same patch.

Separating them will help clarify the patch description. There's no harm in defining the critical data source first.

Quoted 11 lines
Quoted 4 lines

"data_sources" is pretty generic. Perhaps constrain it a bit by re- naming it "critical_data=". Or was such using a generic name intentional?

We intentionally kept the name generic because the data to be measured could be coming from any kernel component with any granularity (from a single bool to megabytes of data). The kernel component is also loosely defined here. It could be an LSM (like SELinux), or a broader base layer (like device-mapper), or a specific module (like dm-crypt), or it could be different parts of a single module.

Quoted 2 lines

Also, we didn't want to name "data_sources" as "critical_data" to avoid confusion with func "CRITICAL_DATA".

The point is that you're measuring critical data, not just any data from any source. Whatever term is used, it needs to be added to the Documentation/ABI/testing/ima_policy. I think something that is self describing will help. See what makes the most sense.

Quoted 8 lines
Quoted 4 lines

Normally "CRITICAL_DATA" would be defined with the critical data hook, but that seems to be defined in patch 3/3 "IMA: define IMA hook to measure critical data from kernel components".

I can make the "CRITICAL_DATA" and the hook as part of the same patch. That would mean combining patch 2 and 3 into a single one.

Does it sound ok?

In the other thread, we discussed separating out "measure_payload_hash"from other changes. The end result you want one logical change per patch. Each patch builds upon the previous one. (Look at how Tyler does it.)

Quoted 108 lines
Quoted 95 lines
Quoted 95 lines
security/integrity/ima/ima.h +1 -0
index 8875085db689..0f4209a92bfb 100644
@@ -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
index af218babd198..9917e1730cb6 100644
@@ -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_policy.c +18 -6
index 8866e84d0062..7b649095ac7a 100644
@@ -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;
  };
 
@@ -508,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;
@@ -911,7 +922,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 +959,7 @@ static const match_table_t policy_tokens = {
  	{Opt_pcr, "pcr=%s"},
  	{Opt_template, "template=%s"},
  	{Opt_keyrings, "keyrings=%s"},
+	{Opt_data_sources, "data_sources=%s"},
  	{Opt_err, NULL}
  };
 
@@ -1110,6 +1122,19 @@ static bool ima_validate_rule(struct ima_rule_entry *entry)
  		if (ima_rule_contains_lsm_cond(entry))
  			return false;
 
Quoted 13 lines

security/integrity/ima/ima_policy.c static bool ima_validate_rule(struct ima_rule_entry *entry) · line 1133

Quoted 9 lines
security/integrity/ima/ima_policy.c +9 -0
+		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;

Requiring IMA_FUNC and IMA_DATA_SOURCES makes sense, but why are IMA_UID and IMA_PCR required?

Quoted 7 lines

Since the data to be measured could be for any scenario, I didn't want to restrict the kernel components from choosing UID to measure the data for, or restrict them from choosing PCR to store the measurements in. But as the consumers are kernel components, perhaps support for IMA_UID is not required. But we should still support IMA_PCR. Please let me know what do you think, and I can update the logic accordingly.

I think I misinterpreted this code. As long as IMA_UID and IMA_PCR aren't required, then it is fine.

Quoted 40 lines
Quoted 32 lines
Quoted 32 lines
security/integrity/ima/ima_policy.c +18 -0
+
+		if (ima_rule_contains_lsm_cond(entry))
+			return false;
+
  		break;
  	default:
  		return false;
@@ -1242,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)
@@ -1312,6 +1339,23 @@ 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, "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;
Quoted 8 lines

security/integrity/ima/ima_policy.c static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode, · line 529

Quoted 3 lines
security/integrity/ima/ima_policy.c +3 -0
+				break;
+			}
+

"keyrings=" isn't bounded because keyrings can be created by userspace. Perhaps keyring names has a minimum/maximum length. IMA isn't measuring userspace construsts. Shouldn't the list of critical data being measured be bounded and verified?

Quoted 7 lines

The comment is not entirely clear.
Do you mean there should be some sort of allow_list in IMA, against which the values in "data_sources=" should be vetted? And if the value is present in the IMA allow_list, then only the measurements for that data source are allowed?

Or do you mean something else?

Yes, something along those lines. Does the list of critical data need to be vetted? And if so, against what?

Mimi

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
security/integrity/ima/ima_policy.c +8 -0
+			entry->flags |= IMA_DATA_SOURCES;
+			break;
  		case Opt_fsuuid:
  			ima_log_string(ab, "fsuuid", args[0].from);
 
@@ -1692,6 +1736,12 @@ int ima_policy_show(struct seq_file *m, void *v)
  		seq_puts(m, " ");
  	}
 
+	if (entry->flags & IMA_DATA_SOURCES) {
+		seq_puts(m, "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);
#6 lore raw

Re: [PATCH v2 2/3] IMA: add policy to support measuring critical data from kernel components

Quoted 64 lines · Mimi Zohar
Quoted 57 lines · Tushar Sugandhi
Quoted 37 lines · Mimi Zohar
Quoted 36 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.

Add a new IMA policy CRITICAL_DATA+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 |  6 ++-
   security/integrity/ima/ima.h         |  1 +
   security/integrity/ima/ima_api.c     |  2 +-
   security/integrity/ima/ima_policy.c  | 62 +++++++++++++++++++++++++---
   4 files changed, 63 insertions(+), 8 deletions(-)
Documentation/ABI/testing/ima_policy +1 -1
index cd572912c593..a0dd0f108555 100644
@@ -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
@@ -125,3 +125,7 @@ Description:
   		keys added to .builtin_trusted_keys or .ima keyring:
 
Quoted 8 lines

Documentation/ABI/testing/ima_policy Description: · line 131

Quoted 5 lines
Documentation/ABI/testing/ima_policy +4 -0
   			measure func=KEY_CHECK keyrings=.builtin_trusted_keys|.ima
+
+		Example of measure rule using CRITICAL_DATA to measure critical data
+
+			measure func=CRITICAL_DATA data_sources=selinux|apparmor|dm-crypt

This example uses "data_sources" without first defining it in the "option:" section. Defining two new options is an indication that this

Thanks. I will define "data_sources" first in "option:" section.

Quoted 2 lines

patch should be split up. One which defines the "CRITICAL_DATA" and another one which defines the new key value pair. The term

I intentionally kept the "CRITICAL_DATA" and "data_sources" in the same patch.

CRITICAL_DATA is different than KEY_CHECK because in case of KEY_CHECK, "keyrings=" is optional. If "keyrings=" is not specified, then we measure all keyrings.

Quoted 5 lines

Where for CRITICAL_DATA, "data_sources=" is mandatory.

Because the data sources would be diverse and orthogonal to each other, (unlike "keyrings=") - not specifying "data_sources=" shouldn't result in IMA blindly measuring all data sources.

Quoted 8 lines

Good point.

Quoted 3 lines

Since CRITICAL_DATA, and "data_sources=" go hand in hand, I wanted them to be part of the same patch.

Separating them will help clarify the patch description. There's no harm in defining the critical data source first.

I will put func=CRITICAL_DATA into one patch, and "data_sources=" into the next patch. Coding wise, the reverse order of patches (where "data_sources=" goes in the first patch, before func=CRITICAL_DATA)
doesn't make sense. Because ima_match_rules() etc. have switch cases
built around func=CRITICAL_DATA etc.

Quoted 11 lines
Quoted 11 lines
Quoted 4 lines

"data_sources" is pretty generic. Perhaps constrain it a bit by re- naming it "critical_data=". Or was such using a generic name intentional?

We intentionally kept the name generic because the data to be measured could be coming from any kernel component with any granularity (from a single bool to megabytes of data). The kernel component is also loosely defined here. It could be an LSM (like SELinux), or a broader base layer (like device-mapper), or a specific module (like dm-crypt), or it could be different parts of a single module.

Quoted 7 lines
Quoted 2 lines

Also, we didn't want to name "data_sources" as "critical_data" to avoid confusion with func "CRITICAL_DATA".

The point is that you're measuring critical data, not just any data from any source. Whatever term is used, it needs to be added to the Documentation/ABI/testing/ima_policy. I think something that is self describing will help. See what makes the most sense.

Fair enough.
Does "critical_kernel_data_sources=" sound ok?

Quoted 11 lines
Quoted 9 lines
Quoted 5 lines

Normally "CRITICAL_DATA" would be defined with the critical data hook, but that seems to be defined in patch 3/3 "IMA: define IMA hook to measure critical data from kernel components".

I can make the "CRITICAL_DATA" and the hook as part of the same patch. That would mean combining patch 2 and 3 into a single one.

Does it sound ok?

In the other thread, we discussed separating out "measure_payload_hash"from other changes. The end result you want one logical change per patch. Each patch builds upon the previous one. (Look at how Tyler does it.)

Will do.

Quoted 117 lines
Quoted 109 lines
Quoted 96 lines
Quoted 96 lines
security/integrity/ima/ima.h +1 -0
index 8875085db689..0f4209a92bfb 100644
@@ -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
index af218babd198..9917e1730cb6 100644
@@ -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_policy.c +18 -6
index 8866e84d0062..7b649095ac7a 100644
@@ -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;
   };
 
@@ -508,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;
@@ -911,7 +922,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 +959,7 @@ static const match_table_t policy_tokens = {
   	{Opt_pcr, "pcr=%s"},
   	{Opt_template, "template=%s"},
   	{Opt_keyrings, "keyrings=%s"},
+	{Opt_data_sources, "data_sources=%s"},
   	{Opt_err, NULL}
   };
 
@@ -1110,6 +1122,19 @@ static bool ima_validate_rule(struct ima_rule_entry *entry)
   		if (ima_rule_contains_lsm_cond(entry))
   			return false;
 
Quoted 13 lines

security/integrity/ima/ima_policy.c static bool ima_validate_rule(struct ima_rule_entry *entry) · line 1133

Quoted 9 lines
security/integrity/ima/ima_policy.c +9 -0
+		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;

Requiring IMA_FUNC and IMA_DATA_SOURCES makes sense, but why are IMA_UID and IMA_PCR required?

Quoted 7 lines

Since the data to be measured could be for any scenario, I didn't want to restrict the kernel components from choosing UID to measure the data for, or restrict them from choosing PCR to store the measurements in. But as the consumers are kernel components, perhaps support for IMA_UID is not required. But we should still support IMA_PCR. Please let me know what do you think, and I can update the logic accordingly.

Quoted 2 lines

I think I misinterpreted this code. As long as IMA_UID and IMA_PCR aren't required, then it is fine.

Yes, IMA_UID and IMA_PCR are not mandatory. Only IMA_DATA_SOURCES is. I will keep both of them.
Thanks for confirming.

Quoted 47 lines
Quoted 47 lines
Quoted 33 lines
Quoted 33 lines
security/integrity/ima/ima_policy.c +18 -0
+
+		if (ima_rule_contains_lsm_cond(entry))
+			return false;
+
   		break;
   	default:
   		return false;
@@ -1242,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)
@@ -1312,6 +1339,23 @@ 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, "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;
Quoted 8 lines

security/integrity/ima/ima_policy.c static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode, · line 529

Quoted 3 lines
security/integrity/ima/ima_policy.c +3 -0
+				break;
+			}
+

"keyrings=" isn't bounded because keyrings can be created by userspace. Perhaps keyring names has a minimum/maximum length. IMA isn't measuring userspace construsts. Shouldn't the list of critical data being measured be bounded and verified?

The comment is not entirely clear.
Do you mean there should be some sort of allow_list in IMA, against which the values in "data_sources=" should be vetted? And if the value is present in the IMA allow_list, then only the measurements for that data source are allowed?

Quoted 4 lines
Quoted 1 line

Or do you mean something else?

Yes, something along those lines. Does the list of critical data need to be vetted? And if so, against what?

I am thinking of having an enum and string array - just like ima_hooks
and ima_hooks_measure_str in ima.h.
And any new kernel component that would support generic IMA measurements
in future would have to add itself to the enum/array.
And the param *event_data_source in ima_measure_critical_data() will be vetted against the above enum/string array.

I will implement it in the next iteration, and hopefully the vetting workflow will be more clear.

~Tushar

Quoted 23 lines

Mimi

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
security/integrity/ima/ima_policy.c +8 -0
+			entry->flags |= IMA_DATA_SOURCES;
+			break;
   		case Opt_fsuuid:
   			ima_log_string(ab, "fsuuid", args[0].from);
 
@@ -1692,6 +1736,12 @@ int ima_policy_show(struct seq_file *m, void *v)
   		seq_puts(m, " ");
   	}
 
+	if (entry->flags & IMA_DATA_SOURCES) {
+		seq_puts(m, "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);
#7 lore raw

PATCH v2 3/3 IMA: define IMA hook to measure critical data from kernel components

In reply to Tushar Sugandhi

Currently, IMA does not provide a generic function to 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_DATA+data_sources across the kernel.

Define a generic IMA function ima_measure_critical_data() to measure data from various kernel components. Limit the measurement to the components that are specified in the IMA policy - CRITICAL_DATA+data_sources.
Update process_buffer_measurement() to return the status code of the operation.
Introduce a boolean parameter measure_buf_hash to support measuring hash of a buffer, instead of the buffer itself. This is useful when the buffer being measured is too large, which may result in bloated IMA logs.

---
 include/linux/ima.h                          | 11 ++++
 security/integrity/ima/ima.h                 |  7 ++-
 security/integrity/ima/ima_appraise.c        |  2 +-
 security/integrity/ima/ima_asymmetric_keys.c |  2 +-
 security/integrity/ima/ima_main.c            | 65 +++++++++++++++++---
 security/integrity/ima/ima_queue_keys.c      |  3 +-
 6 files changed, 75 insertions(+), 15 deletions(-)
include/linux/ima.h +11 -0
index d15100de6cdd..136fc02580db 100644
@@ -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 +4 -3
index 0f4209a92bfb..00b84052c8f1 100644
@@ -266,9 +266,10 @@ 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,
+			       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
index 372d16382960..20adffe5bf58 100644
@@ -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
index 1c68c500c26f..a74095793936 100644
@@ -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 +56 -9

123 lines not on this page · raw message

security/integrity/ima/ima_queue_keys.c +2 -1
index 69a8626a35c0..c2f2ad34f9b7 100644
@@ -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