Skip to contentSkip to conversation

SELinux: Measure state and hash of policy using IMA

PATCH In review Lakshmi Ramasubramanian 4 participants 13 messages Aug 13, 2020 – Aug 17, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover SELinux: Measure state and hash of policy using IMA

Critical data structures of security modules are currently not measured. Therefore an attestation service, for instance, would not be able to attest whether the security modules are always operating with the policies and configuration that the system administrator had setup. The policies and configuration for the security modules could be tampered with by malware by exploiting kernel vulnerabilities or modified through some inadvertent actions on the system. Measuring such critical data would enable an attestation service to better assess the state of the system.

IMA subsystem measures system files, command line arguments passed to kexec, boot aggregate, keys, etc. It can be used to measure critical data structures of security modules as well.

This change aims to address measuring critical data structures of SELinux when they are initialized and when they are updated at runtime.

This series is based on commit 3db0d0c276a7 ("integrity: remove redundant initialization of variable ret") in next-integrity

This series also has a dependency on the following patch series and should be applied in the following order:
https://patchwork.kernel.org/patch/11709527/
https://patchwork.kernel.org/patch/11711249/

Lakshmi Ramasubramanian (2):
IMA: Handle early boot data measurement
SELinux: Measure state and hash of policy using IMA

 security/integrity/ima/Kconfig               |   5 +-
 security/integrity/ima/Makefile              |   2 +-
 security/integrity/ima/ima.h                 |  37 ++--
 security/integrity/ima/ima_asymmetric_keys.c |   6 +-
 security/integrity/ima/ima_init.c            |   2 +-
 security/integrity/ima/ima_main.c            |   9 +
 security/integrity/ima/ima_policy.c          |   2 +-
 security/integrity/ima/ima_queue_data.c      | 190 +++++++++++++++++
 security/integrity/ima/ima_queue_keys.c      | 174 ----------------
 security/selinux/Makefile                    |   2 +
 security/selinux/hooks.c                     |   1 +
 security/selinux/include/security.h          |  15 ++
 security/selinux/measure.c                   | 204 +++++++++++++++++++
 security/selinux/selinuxfs.c                 |   3 +
 security/selinux/ss/services.c               |  71 ++++++-
 15 files changed, 516 insertions(+), 207 deletions(-)
 create mode 100644 security/integrity/ima/ima_queue_data.c
 delete mode 100644 security/integrity/ima/ima_queue_keys.c
 create mode 100644 security/selinux/measure.c
Signature · 1 line
2.28.0
#2 lore raw

PATCH 2/2 SELinux: Measure state and hash of policy using IMA

SELinux configuration and policy are some of the critical data for this security module that needs to be measured. This measurement can be used by an attestation service, for instance, to verify if the configuration and policies have been setup correctly and that they haven't been tampered with at runtime.

Measure SELinux configuration, policy capabilities settings, and the hash of the loaded policy by calling the IMA hook ima_measure_critical_data().

Sample measurement of SELinux state and hash of the policy:

10 e32e...5ac3 ima-buf sha256:86e8...4594 selinux-state-1595389364:287899386 696e697469616c697a65643d313b656e61626c65643d313b656e666f7263696e673d303b636865636b72657170726f743d313b6e6574776f726b5f706565725f636f6e74726f6c733d313b6f70656e5f7065726d733d313b657874656e6465645f736f636b65745f636c6173733d313b616c776179735f636865636b5f6e6574776f726b3d303b6367726f75705f7365636c6162656c3d313b6e6e705f6e6f737569645f7472616e736974696f6e3d313b67656e66735f7365636c6162656c5f73796d6c696e6b733d303 10 9e81...0857 ima-buf sha256:4941...68fc selinux-policy-hash-1597335667:462051628 8d1d...1834

To verify the measurement check the following:

Execute the following command to extract the measured data from the IMA log for SELinux configuration (selinux-state).

grep -m 1 "selinux-state" /sys/kernel/security/integrity/ima/ascii_runtime_measurements | cut -d' ' -f 6 | xxd -r -p

The output should be the list of key-value pairs. For example,
initialized=1;enabled=1;enforcing=0;checkreqprot=1;network_peer_controls=1;open_perms=1;extended_socket_class=1;always_check_network=0;cgroup_seclabel=1;nnp_nosuid_transition=1;genfs_seclabel_symlinks=0;

To verify the measured data with the current SELinux state:

=> enabled should be set to 1 if /sys/fs/selinux folder exists,
0 otherwise

For other entries, compare the integer value in the files
=> /sys/fs/selinux/enforce
=> /sys/fs/selinux/checkreqprot
And, each of the policy capabilities files under
=> /sys/fs/selinux/policy_capabilities

For selinux-policy-hash, the hash of SELinux policy is included in the IMA log entry.

To verify the measured data with the current SELinux policy run the following commands and verify the output hash values match.

sha256sum /sys/fs/selinux/policy | cut -d' ' -f 1

grep -m 1 "selinux-policy-hash" /sys/kernel/security/integrity/ima/ascii_runtime_measurements | cut -d' ' -f 6

---
 security/selinux/Makefile           |   2 +
 security/selinux/hooks.c            |   1 +
 security/selinux/include/security.h |  15 ++
 security/selinux/measure.c          | 204 ++++++++++++++++++++++++++++
 security/selinux/selinuxfs.c        |   3 +
 security/selinux/ss/services.c      |  71 ++++++++--
 6 files changed, 287 insertions(+), 9 deletions(-)
 create mode 100644 security/selinux/measure.c
security/selinux/Makefile +2 -0
index 4d8e0e8adf0b..83d512116341 100644
@@ -16,6 +16,8 @@ selinux-$(CONFIG_NETLABEL) += netlabel.o
 
 selinux-$(CONFIG_SECURITY_INFINIBAND) += ibpkey.o
 
+selinux-$(CONFIG_IMA) += measure.o
+
 ccflags-y := -I$(srctree)/security/selinux -I$(srctree)/security/selinux/include
 
 $(addprefix $(obj)/,$(selinux-y)): $(obj)/flask.h
security/selinux/hooks.c +1 -0
index efa6108b1ce9..5521dfc1900b 100644
@@ -7394,6 +7394,7 @@ int selinux_disable(struct selinux_state *state)
 	}
 
 	selinux_mark_disabled(state);
+	selinux_measure_state(state);
 
 	pr_info("SELinux:  Disabled at runtime.\n");
 
security/selinux/include/security.h +15 -0
index b0e02cfe3ce1..77f42d9b544b 100644
@@ -222,16 +222,31 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
 	return state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS];
 }
 
+static inline bool selinux_checkreqprot(const struct selinux_state *state)
+{
+	return READ_ONCE(state->checkreqprot);
+}
+
 int security_mls_enabled(struct selinux_state *state);
 int security_load_policy(struct selinux_state *state,
 			 void *data, size_t len);
 int security_read_policy(struct selinux_state *state,
 			 void **data, size_t *len);
+int security_read_policy_kernel(struct selinux_state *state,
+				void **data, size_t *len);
 size_t security_policydb_len(struct selinux_state *state);
 
 int security_policycap_supported(struct selinux_state *state,
 				 unsigned int req_cap);
 
+#ifdef CONFIG_IMA
+extern void selinux_measure_state(struct selinux_state *selinux_state);
+#else
+static inline void selinux_measure_state(struct selinux_state *selinux_state)
+{
+}
+#endif
+
 #define SEL_VEC_MAX 32
 struct av_decision {
 	u32 allowed;
security/selinux/measure.c new file +204 -0

204 lines not on this page · raw message

security/selinux/selinuxfs.c +3 -0
index 4781314c2510..6d46eaef5c92 100644
@@ -173,6 +173,7 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,
 			from_kuid(&init_user_ns, audit_get_loginuid(current)),
 			audit_get_sessionid(current));
 		enforcing_set(state, new_value);
+		selinux_measure_state(state);
 		if (new_value)
 			avc_ss_reset(state->avc, 0);
 		selnl_notify_setenforce(new_value);
@@ -678,6 +679,8 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf,
 
 	fsi->state->checkreqprot = new_value ? 1 : 0;
 	length = count;
+
+	selinux_measure_state(fsi->state);
 out:
 	kfree(page);
 	return length;
security/selinux/ss/services.c +62 -9

111 lines not on this page · raw message

Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/2 IMA: Handle early boot data measurement

In reply to Lakshmi Ramasubramanian

The current implementation of early boot measurement in the IMA subsystem is very specific to asymmetric keys. It does not handle measurement of other data such as Linux Security Module (LSM) data. Since most security modules are initialized very early in the boot cycle, data provided by those modules are not measured by IMA. Any other subsystem that initializes early in the boot cycle and needs IMA to measure their data would suffer from the same issue.

Update the early boot key measurement to handle any early boot data. Change the kernel configuration CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS to CONFIG_IMA_QUEUE_EARLY_BOOT_DATA so it can be used for enabling early boot data measurement. Change this new configuration to support SECURITY_SELINUX subsystem in addition to KEYS subsystem, which is currently supported. This can be extended to include more subsystems in the future by updating this kernel configuration.

Update the IMA hook namely ima_measure_critical_data() to utilize early boot measurement support.

---
 security/integrity/ima/Kconfig               |   5 +-
 security/integrity/ima/Makefile              |   2 +-
 security/integrity/ima/ima.h                 |  37 ++--
 security/integrity/ima/ima_asymmetric_keys.c |   6 +-
 security/integrity/ima/ima_init.c            |   2 +-
 security/integrity/ima/ima_main.c            |   9 +
 security/integrity/ima/ima_policy.c          |   2 +-
 security/integrity/ima/ima_queue_data.c      | 190 +++++++++++++++++++
 security/integrity/ima/ima_queue_keys.c      | 174 -----------------
 9 files changed, 229 insertions(+), 198 deletions(-)
 create mode 100644 security/integrity/ima/ima_queue_data.c
 delete mode 100644 security/integrity/ima/ima_queue_keys.c
security/integrity/ima/Kconfig +2 -3
index 080c53545ff0..e4fb1761d64a 100644
@@ -322,10 +322,9 @@ config IMA_MEASURE_ASYMMETRIC_KEYS
 	depends on ASYMMETRIC_PUBLIC_KEY_SUBTYPE=y
 	default y
 
-config IMA_QUEUE_EARLY_BOOT_KEYS
+config IMA_QUEUE_EARLY_BOOT_DATA
 	bool
-	depends on IMA_MEASURE_ASYMMETRIC_KEYS
-	depends on SYSTEM_TRUSTED_KEYRING
+	depends on SECURITY_SELINUX || (IMA_MEASURE_ASYMMETRIC_KEYS && SYSTEM_TRUSTED_KEYRING)
 	default y
 
 config IMA_SECURE_AND_OR_TRUSTED_BOOT
security/integrity/ima/Makefile +1 -1
index 67dabca670e2..cbbbc9848d2f 100644
@@ -13,4 +13,4 @@ ima-$(CONFIG_IMA_APPRAISE_MODSIG) += ima_modsig.o
 ima-$(CONFIG_HAVE_IMA_KEXEC) += ima_kexec.o
 ima-$(CONFIG_IMA_BLACKLIST_KEYRING) += ima_mok.o
 ima-$(CONFIG_IMA_MEASURE_ASYMMETRIC_KEYS) += ima_asymmetric_keys.o
-ima-$(CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS) += ima_queue_keys.o
+ima-$(CONFIG_IMA_QUEUE_EARLY_BOOT_DATA) += ima_queue_data.o
security/integrity/ima/ima.h +21 -16
index e65ab067e700..8822ad1ef4eb 100644
@@ -228,29 +228,34 @@ extern const char *const func_tokens[];
 
 struct modsig;
 
-#ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS
+#ifdef CONFIG_IMA_QUEUE_EARLY_BOOT_DATA
 /*
- * To track keys that need to be measured.
+ * To track data that needs to be measured.
  */
-struct ima_key_entry {
+struct ima_data_entry {
 	struct list_head list;
 	void *payload;
 	size_t payload_len;
-	char *keyring_name;
+	const char *event_name;
+	const char *event_data;
+	enum ima_hooks func;
 };
-void ima_init_key_queue(void);
-bool ima_should_queue_key(void);
-bool ima_queue_key(struct key *keyring, const void *payload,
-		   size_t payload_len);
-void ima_process_queued_keys(void);
+void ima_init_data_queue(void);
+bool ima_should_queue_data(void);
+bool ima_queue_data(const char *event_name, const void *payload,
+		    size_t payload_len, const char *event_data,
+		    enum ima_hooks func);
+void ima_process_queued_data(void);
 #else
-static inline void ima_init_key_queue(void) {}
-static inline bool ima_should_queue_key(void) { return false; }
-static inline bool ima_queue_key(struct key *keyring,
-				 const void *payload,
-				 size_t payload_len) { return false; }
-static inline void ima_process_queued_keys(void) {}
-#endif /* CONFIG_IMA_QUEUE_EARLY_BOOT_KEYS */
+static inline void ima_init_data_queue(void) {}
+static inline bool ima_should_queue_data(void) { return false; }
+static inline bool ima_queue_data(const char *event_name,
+				  const void *payload,
+				  size_t payload_len,
+				  const char *event_data,
+				  enum ima_hooks func) { return false; }
+static inline void ima_process_queued_data(void) {}
+#endif /* CONFIG_IMA_QUEUE_EARLY_BOOT_DATA */
 
 /* LIM API function definitions */
 int ima_get_action(struct inode *inode, const struct cred *cred, u32 secid,
security/integrity/ima/ima_asymmetric_keys.c +4 -2
index 1c68c500c26f..8f8431f8b096 100644
@@ -37,8 +37,10 @@ void ima_post_key_create_or_update(struct key *keyring, struct key *key,
 	if (!payload || (payload_len == 0))
 		return;
 
-	if (ima_should_queue_key())
-		queued = ima_queue_key(keyring, payload, payload_len);
+	if (ima_should_queue_data())
+		queued = ima_queue_data(keyring->description, payload,
+					payload_len, keyring->description,
+					KEY_CHECK);
 
 	if (queued)
 		return;
security/integrity/ima/ima_init.c +1 -1
index 4902fe7bd570..892894bf4af3 100644
@@ -145,7 +145,7 @@ int __init ima_init(void)
 	if (rc != 0)
 		return rc;
 
-	ima_init_key_queue();
+	ima_init_data_queue();
 
 	return rc;
 }
security/integrity/ima/ima_main.c +9 -0
index 129bcaaf13e2..8b27d9dafd6b 100644
@@ -861,9 +861,18 @@ int ima_measure_critical_data(const char *event_name,
 			      const char *event_data_source,
 			      const void *buf, int buf_len)
 {
+	bool queued = false;
+
 	if (!event_name || !event_data_source || !buf || !buf_len)
 		return -EINVAL;
 
+	if (ima_should_queue_data())
+		queued = ima_queue_data(event_name, buf, buf_len,
+					event_data_source, CRITICAL_DATA);
+
+	if (queued)
+		return 0;
+
 	return process_buffer_measurement(NULL, buf, buf_len, event_name,
 					  CRITICAL_DATA, 0, event_data_source);
 }
security/integrity/ima/ima_policy.c +1 -1
index 8451ccb2a775..8f1155a99e89 100644
@@ -906,7 +906,7 @@ void ima_update_policy(void)
 	ima_update_policy_flag();
 
 	/* Custom IMA policy has been loaded */
-	ima_process_queued_keys();
+	ima_process_queued_data();
 }
 
 /* Keep the enumeration in sync with the policy_tokens! */
security/integrity/ima/ima_queue_data.c new file +190 -0

190 lines not on this page · raw message

security/integrity/ima/ima_queue_keys.c deleted +0 -174

174 lines not on this page · raw message

Signature · 1 line
2.28.0
maintainer
#3 lore raw
In reply to Lakshmi Ramasubramanian
Quoted 61 lines · Lakshmi Ramasubramanian

SELinux configuration and policy are some of the critical data for this security module that needs to be measured. This measurement can be used by an attestation service, for instance, to verify if the configuration and policies have been setup correctly and that they haven't been tampered with at runtime.

Measure SELinux configuration, policy capabilities settings, and the hash of the loaded policy by calling the IMA hook ima_measure_critical_data().

Sample measurement of SELinux state and hash of the policy:

10 e32e...5ac3 ima-buf sha256:86e8...4594 selinux-state-1595389364:287899386 696e697469616c697a65643d313b656e61626c65643d313b656e666f7263696e673d303b636865636b72657170726f743d313b6e6574776f726b5f706565725f636f6e74726f6c733d313b6f70656e5f7065726d733d313b657874656e6465645f736f636b65745f636c6173733d313b616c776179735f636865636b5f6e6574776f726b3d303b6367726f75705f7365636c6162656c3d313b6e6e705f6e6f737569645f7472616e736974696f6e3d313b67656e66735f7365636c6162656c5f73796d6c696e6b733d303 10 9e81...0857 ima-buf sha256:4941...68fc selinux-policy-hash-1597335667:462051628 8d1d...1834

To verify the measurement check the following:

Execute the following command to extract the measured data from the IMA log for SELinux configuration (selinux-state).

grep -m 1 "selinux-state" /sys/kernel/security/integrity/ima/ascii_runtime_measurements | cut -d' ' -f 6 | xxd -r -p

The output should be the list of key-value pairs. For example,
initialized=1;enabled=1;enforcing=0;checkreqprot=1;network_peer_controls=1;open_perms=1;extended_socket_class=1;always_check_network=0;cgroup_seclabel=1;nnp_nosuid_transition=1;genfs_seclabel_symlinks=0;

To verify the measured data with the current SELinux state:

=> enabled should be set to 1 if /sys/fs/selinux folder exists,
0 otherwise

For other entries, compare the integer value in the files
=> /sys/fs/selinux/enforce
=> /sys/fs/selinux/checkreqprot
And, each of the policy capabilities files under
=> /sys/fs/selinux/policy_capabilities

For selinux-policy-hash, the hash of SELinux policy is included in the IMA log entry.

To verify the measured data with the current SELinux policy run the following commands and verify the output hash values match.

sha256sum /sys/fs/selinux/policy | cut -d' ' -f 1

grep -m 1 "selinux-policy-hash" /sys/kernel/security/integrity/ima/ascii_runtime_measurements | cut -d' ' -f 6

---

security/selinux/measure.c new file +3 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+			       void **buf_hash, int *buf_hash_len)
+{

security/selinux/measure.c line 13

Quoted 10 lines
security/selinux/measure.c new file +10 -0
+	struct crypto_shash *tfm;
+	struct shash_desc *desc = NULL;
+	void *digest = NULL;
+	int desc_size;
+	int digest_size;
+	int ret = 0;
+
+	tfm = crypto_alloc_shash("sha256", 0, 0);
+	if (IS_ERR(tfm))
+		return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

#4 lore raw
Quoted 12 lines · Stephen Smalley
Quoted 12 lines
security/selinux/measure.c new file +6 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
Quoted 8 lines

security/selinux/measure.c line 101

Quoted 7 lines
security/selinux/measure.c new file +7 -0
+    int desc_size;
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

I can add a kernel parameter to select this hash algorithm.

-lakshmi

maintainer
#5 lore raw
Quoted 25 lines · Lakshmi Ramasubramanian
Quoted 13 lines · Stephen Smalley
Quoted 13 lines
security/selinux/measure.c new file +7 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
+    int desc_size;
Quoted 8 lines

security/selinux/measure.c line 101

Quoted 6 lines
security/selinux/measure.c new file +6 -0
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

Quoted 1 line

I can add a kernel parameter to select this hash algorithm.

Also can we provide a Kconfig option for the default value like IMA does?

#6 lore raw
Quoted 27 lines · Stephen Smalley
Quoted 25 lines · Lakshmi Ramasubramanian
Quoted 21 lines · Stephen Smalley
Quoted 9 lines
security/selinux/measure.c new file +3 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{

security/selinux/measure.c line 13

Quoted 10 lines
security/selinux/measure.c new file +10 -0
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
+    int desc_size;
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

Quoted 4 lines
Quoted 1 line

I can add a kernel parameter to select this hash algorithm.

Also can we provide a Kconfig option for the default value like IMA does?

Would we need both - Kconfig and kernel param?

The other option is to provide an IMA function to return the current hash algorithm used for measurement. That way a consistent hash algorithm can be employed by both IMA and the callers. Would that be better?

-lakshmi

maintainer
#7 lore raw
Quoted 31 lines · Lakshmi Ramasubramanian
Quoted 29 lines · Stephen Smalley
Quoted 26 lines · Lakshmi Ramasubramanian
Quoted 13 lines · Stephen Smalley
Quoted 13 lines
security/selinux/measure.c new file +7 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
+    int desc_size;
Quoted 8 lines

security/selinux/measure.c line 101

Quoted 6 lines
security/selinux/measure.c new file +6 -0
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

I can add a kernel parameter to select this hash algorithm.

Quoted 8 lines
Quoted 2 lines

Also can we provide a Kconfig option for the default value like IMA does?

Would we need both - Kconfig and kernel param?

The other option is to provide an IMA function to return the current hash algorithm used for measurement. That way a consistent hash algorithm can be employed by both IMA and the callers. Would that be better?

This is why I preferred just passing the serialized policy buffer to IMA and letting it handle the hashing. But apparently that approach wouldn't fly. IMA appears to support both a Kconfig option for selecting a default algorithm and a kernel parameter for overriding it. I assume the idea is that the distros can pick a reasonable default and then the end users can override that if they have specific requirements. I'd want the same for SELinux. If IMA is willing to export its hash algorithm to external components, then I'm willing to reuse that but not sure if that's a layering violation.

maintainer
#8 lore raw
Quoted 40 lines · Stephen Smalley
Quoted 29 lines · Lakshmi Ramasubramanian
Quoted 28 lines · Stephen Smalley
Quoted 25 lines · Lakshmi Ramasubramanian
Quoted 13 lines · Stephen Smalley
Quoted 13 lines
security/selinux/measure.c new file +7 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
+    int desc_size;
Quoted 8 lines

security/selinux/measure.c line 101

Quoted 6 lines
security/selinux/measure.c new file +6 -0
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

I can add a kernel parameter to select this hash algorithm.

Quoted 8 lines
Quoted 2 lines

Also can we provide a Kconfig option for the default value like IMA does?

Would we need both - Kconfig and kernel param?

The other option is to provide an IMA function to return the current hash algorithm used for measurement. That way a consistent hash algorithm can be employed by both IMA and the callers. Would that be better?

Quoted 9 lines

This is why I preferred just passing the serialized policy buffer to IMA and letting it handle the hashing. But apparently that approach wouldn't fly. IMA appears to support both a Kconfig option for selecting a default algorithm and a kernel parameter for overriding it. I assume the idea is that the distros can pick a reasonable default and then the end users can override that if they have specific requirements. I'd want the same for SELinux. If IMA is willing to export its hash algorithm to external components, then I'm willing to reuse that but not sure if that's a layering violation.

With the new ima_measure_critical_data() hook, I agree with you and Casey it doesn't make sense for each caller to have to write their own function. Casey suggested exporting IMA's hash function or defining a
new common hash function. There's nothing specific to IMA. Should
the common hash function be prefixed with "security_"?

Like when we add a new security hook call, the new LSM call is separate
from any other change. Please break up this patch with the SELinux
specific pieces separated from the ima_measure_critical_data() call as much as possible.

thanks,

Mimi

#9 lore raw
Quoted 46 lines · Mimi Zohar
Quoted 36 lines · Stephen Smalley
Quoted 26 lines · Lakshmi Ramasubramanian
Quoted 25 lines · Stephen Smalley
Quoted 23 lines · Lakshmi Ramasubramanian
Quoted 21 lines · Stephen Smalley
Quoted 9 lines
security/selinux/measure.c new file +3 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{

security/selinux/measure.c line 13

Quoted 10 lines
security/selinux/measure.c new file +10 -0
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
+    int desc_size;
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I can add a kernel parameter to select this hash algorithm.

Also can we provide a Kconfig option for the default value like IMA does?

Would we need both - Kconfig and kernel param?

The other option is to provide an IMA function to return the current hash algorithm used for measurement. That way a consistent hash algorithm can be employed by both IMA and the callers. Would that be better?

Quoted 9 lines

This is why I preferred just passing the serialized policy buffer to IMA and letting it handle the hashing. But apparently that approach wouldn't fly. IMA appears to support both a Kconfig option for selecting a default algorithm and a kernel parameter for overriding it. I assume the idea is that the distros can pick a reasonable default and then the end users can override that if they have specific requirements. I'd want the same for SELinux. If IMA is willing to export its hash algorithm to external components, then I'm willing to reuse that but not sure if that's a layering violation.

Quoted 4 lines

With the new ima_measure_critical_data() hook, I agree with you and Casey it doesn't make sense for each caller to have to write their own function. Casey suggested exporting IMA's hash function or defining a
new common hash function. There's nothing specific to IMA.

Except that no one is going to use the function unless they're doing an IMA operation.

Quoted 2 lines

Should
the common hash function be prefixed with "security_"?

Yuck. That prefix is for interfaces that are exported outside the security sub-system. We're talking about a function that is provided for use within the security sub-system, but not for any one particular security module or non-module feature. We're currently using the lsm_ prefix for interfaces used within the security subsystem, so I suggest lsm_hash_brown_potatoes() might be the way to go.

Quoted 10 lines

Like when we add a new security hook call, the new LSM call is separate
from any other change. Please break up this patch with the SELinux
specific pieces separated from the ima_measure_critical_data() call as much as possible.

thanks,

Mimi

#10 lore raw
Quoted 47 lines · Casey Schaufler
Quoted 46 lines · Mimi Zohar
Quoted 45 lines · Stephen Smalley
Quoted 26 lines · Lakshmi Ramasubramanian
Quoted 25 lines · Stephen Smalley
Quoted 23 lines · Lakshmi Ramasubramanian
Quoted 21 lines · Stephen Smalley
Quoted 9 lines
security/selinux/measure.c new file +3 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{

security/selinux/measure.c line 13

Quoted 10 lines
security/selinux/measure.c new file +10 -0
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
+    int desc_size;
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I can add a kernel parameter to select this hash algorithm.

Also can we provide a Kconfig option for the default value like IMA does?

Would we need both - Kconfig and kernel param?

The other option is to provide an IMA function to return the current hash algorithm used for measurement. That way a consistent hash algorithm can be employed by both IMA and the callers. Would that be better?

This is why I preferred just passing the serialized policy buffer to IMA and letting it handle the hashing. But apparently that approach wouldn't fly. IMA appears to support both a Kconfig option for selecting a default algorithm and a kernel parameter for overriding it. I assume the idea is that the distros can pick a reasonable default and then the end users can override that if they have specific requirements. I'd want the same for SELinux. If IMA is willing to export its hash algorithm to external components, then I'm willing to reuse that but not sure if that's a layering violation.

Quoted 7 lines
Quoted 4 lines

With the new ima_measure_critical_data() hook, I agree with you and Casey it doesn't make sense for each caller to have to write their own function. Casey suggested exporting IMA's hash function or defining a
new common hash function. There's nothing specific to IMA.

Except that no one is going to use the function unless they're doing an IMA operation.

Can we do the following instead:

In ima_measure_critical_data() IMA hook, we can add another param for the caller to indicate whether

=> The contents of "buf" needs to be measured
OR
=> Hash of the contents of "buf" needs to be measured.

This way IMA doesn't need to export any new function to meet the hashing requirement.

-lakshmi

Quoted 19 lines
Quoted 3 lines

Should
the common hash function be prefixed with "security_"?

Yuck. That prefix is for interfaces that are exported outside the security sub-system. We're talking about a function that is provided for use within the security sub-system, but not for any one particular security module or non-module feature. We're currently using the lsm_ prefix for interfaces used within the security subsystem, so I suggest lsm_hash_brown_potatoes() might be the way to go.

Quoted 6 lines

Like when we add a new security hook call, the new LSM call is separate
from any other change. Please break up this patch with the SELinux
specific pieces separated from the ima_measure_critical_data() call as much as possible.

maintainer
#11 lore raw
Quoted 61 lines · Lakshmi Ramasubramanian
Quoted 47 lines · Casey Schaufler
Quoted 46 lines · Mimi Zohar
Quoted 45 lines · Stephen Smalley
Quoted 26 lines · Lakshmi Ramasubramanian
Quoted 25 lines · Stephen Smalley
Quoted 23 lines · Lakshmi Ramasubramanian
Quoted 21 lines · Stephen Smalley
Quoted 9 lines
security/selinux/measure.c new file +3 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{

security/selinux/measure.c line 13

Quoted 10 lines
security/selinux/measure.c new file +10 -0
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
+    int desc_size;
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I can add a kernel parameter to select this hash algorithm.

Also can we provide a Kconfig option for the default value like IMA does?

Would we need both - Kconfig and kernel param?

The other option is to provide an IMA function to return the current hash algorithm used for measurement. That way a consistent hash algorithm can be employed by both IMA and the callers. Would that be better?

This is why I preferred just passing the serialized policy buffer to IMA and letting it handle the hashing. But apparently that approach wouldn't fly. IMA appears to support both a Kconfig option for selecting a default algorithm and a kernel parameter for overriding it. I assume the idea is that the distros can pick a reasonable default and then the end users can override that if they have specific requirements. I'd want the same for SELinux. If IMA is willing to export its hash algorithm to external components, then I'm willing to reuse that but not sure if that's a layering violation.

Quoted 7 lines
Quoted 4 lines

With the new ima_measure_critical_data() hook, I agree with you and Casey it doesn't make sense for each caller to have to write their own function. Casey suggested exporting IMA's hash function or defining a
new common hash function. There's nothing specific to IMA.

Except that no one is going to use the function unless they're doing an IMA operation.

Can we do the following instead:

In ima_measure_critical_data() IMA hook, we can add another param for the caller to indicate whether

Quoted 6 lines

=> The contents of "buf" needs to be measured
OR
=> Hash of the contents of "buf" needs to be measured.

This way IMA doesn't need to export any new function to meet the hashing requirement.

I'm not sure overloading the parameters is a good idea, but extending ima_measure_critical_data() to calculate a simple buffer hash should be fine.

Mimi

#12 lore raw
Quoted 66 lines · Mimi Zohar
Quoted 65 lines · Lakshmi Ramasubramanian
Quoted 47 lines · Casey Schaufler
Quoted 46 lines · Mimi Zohar
Quoted 45 lines · Stephen Smalley
Quoted 26 lines · Lakshmi Ramasubramanian
Quoted 25 lines · Stephen Smalley
Quoted 23 lines · Lakshmi Ramasubramanian
Quoted 21 lines · Stephen Smalley
Quoted 9 lines
security/selinux/measure.c new file +3 -0
new file mode 100644
index 000000000000..f21b7de4e2ae
@@ -0,0 +1,204 @@
+static int selinux_hash_buffer(void *buf, size_t buf_len,
+                   void **buf_hash, int *buf_hash_len)
+{

security/selinux/measure.c line 13

Quoted 10 lines
security/selinux/measure.c new file +10 -0
+    struct crypto_shash *tfm;
+    struct shash_desc *desc = NULL;
+    void *digest = NULL;
+    int desc_size;
+    int digest_size;
+    int ret = 0;
+
+    tfm = crypto_alloc_shash("sha256", 0, 0);
+    if (IS_ERR(tfm))
+        return PTR_ERR(tfm);

Can we make the algorithm selectable via kernel parameter and/or writing to a new selinuxfs node?

Quoted 8 lines
Quoted 3 lines
Quoted 1 line

I can add a kernel parameter to select this hash algorithm.

Also can we provide a Kconfig option for the default value like IMA does?

Would we need both - Kconfig and kernel param?

The other option is to provide an IMA function to return the current hash algorithm used for measurement. That way a consistent hash algorithm can be employed by both IMA and the callers. Would that be better?

This is why I preferred just passing the serialized policy buffer to IMA and letting it handle the hashing. But apparently that approach wouldn't fly. IMA appears to support both a Kconfig option for selecting a default algorithm and a kernel parameter for overriding it. I assume the idea is that the distros can pick a reasonable default and then the end users can override that if they have specific requirements. I'd want the same for SELinux. If IMA is willing to export its hash algorithm to external components, then I'm willing to reuse that but not sure if that's a layering violation.

Quoted 7 lines
Quoted 4 lines

With the new ima_measure_critical_data() hook, I agree with you and Casey it doesn't make sense for each caller to have to write their own function. Casey suggested exporting IMA's hash function or defining a
new common hash function. There's nothing specific to IMA.

Except that no one is going to use the function unless they're doing an IMA operation.

Can we do the following instead:

In ima_measure_critical_data() IMA hook, we can add another param for the caller to indicate whether

=> The contents of "buf" needs to be measured
OR
=> Hash of the contents of "buf" needs to be measured.

Quoted 7 lines
Quoted 2 lines

This way IMA doesn't need to export any new function to meet the hashing requirement.

I'm not sure overloading the parameters is a good idea, but extending ima_measure_critical_data() to calculate a simple buffer hash should be fine.

Sorry I wasn't clear - I didn't mean to say overload existing parameters, but extending the IMA hook to calculate the hash of the buffer - like the following:

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 measure_buf_hash is true, IMA will calculate the hash of contents of "buf" and measure the hash. Else, IMA will measure the contents of "buf".

-lakshmi