Skip to contentSkip to conversation

LSM: Measure security module state

PATCH Superseded Lakshmi Ramasubramanian 2 participants 8 messages Jul 15, 2020

Subsystems (5)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover LSM: Measure security module state

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 security modules when they are initialized, when they are updated at runtime, and also periodically to detect any tampering.

This change set is based off of Linux Kernel version 5.8-rc5.

The following patch needs to be applied first before applying the patches in this patch set:

    https://patchwork.kernel.org/patch/11612989/

Change log:
v1:
=> Per Stephen Smalley's suggestion added selinux_state booleans
and hash of SELinux policy in the measured data for SELinux.
=> Call IMA hook from the security module directly instead of
redirecting through the LSM.

Lakshmi Ramasubramanian (5):
IMA: Add LSM_STATE func to measure LSM data
IMA: Define an IMA hook to measure LSM data
LSM: Add security_state function pointer in lsm_info struct
LSM: Define SELinux function to measure security state
LSM: Define workqueue for measuring security module state

 Documentation/ABI/testing/ima_policy |   6 +-
 include/linux/ima.h                  |   4 +
 include/linux/lsm_hooks.h            |   3 +
 security/integrity/ima/ima.h         |   1 +
 security/integrity/ima/ima_api.c     |   2 +-
 security/integrity/ima/ima_main.c    |  17 ++++
 security/integrity/ima/ima_policy.c  |  29 +++++--
 security/security.c                  |  74 +++++++++++++++-
 security/selinux/Makefile            |   2 +
 security/selinux/hooks.c             |   2 +
 security/selinux/include/security.h  |  19 +++++
 security/selinux/measure.c           | 122 +++++++++++++++++++++++++++
 security/selinux/selinuxfs.c         |   1 +
 security/selinux/ss/services.c       |  23 ++++-
 14 files changed, 293 insertions(+), 12 deletions(-)
 create mode 100644 security/selinux/measure.c
Signature · 1 line
2.27.0
#4 lore raw

PATCH 4/5 LSM: Define SELinux function to measure security state

SELinux configuration and policy are some of the critical data for this security module that needs to be measured. To enable this measurement SELinux needs to implement the interface function, security_state(), that the LSM can call.

Define the security_state() function in SELinux to measure SELinux configuration and policy. Call this function to measure SELinux data when there is a change in the security module's state.

Sample measurement of SELinux state and hash of the policy:

10 e32e...5ac3 ima-buf sha256:86e8...4594 selinux-state 656e61626c65643d313b656e666f7263696e673d303b636865636b72657170726f743d313b6e6574706565723d313b6f70656e7065726d3d313b657874736f636b636c6173733d313b616c776179736e6574776f726b3d303b6367726f75707365636c6162656c3d313b6e6e706e6f737569647472616e736974696f6e3d313b67656e66737365636c6162656c73796d6c696e6b3d303b 10 f4a7...9408 ima-buf sha256:4941...68fc selinux-policy-hash 8d1d...1834

The data for selinux-state in the above measurement is:
enabled=1;enforcing=0;checkreqprot=1;netpeer=1;openperm=1;extsockclass=1;alwaysnetwork=0;cgroupseclabel=1;nnpnosuidtransition=1;genfsseclabelsymlink=0;

The data for selinux-policy-hash in the above measurement is the SHA256 hash of the SELinux policy.

---
 security/selinux/Makefile           |   2 +
 security/selinux/hooks.c            |   2 +
 security/selinux/include/security.h |  19 +++++
 security/selinux/measure.c          | 122 ++++++++++++++++++++++++++++
 security/selinux/selinuxfs.c        |   1 +
 security/selinux/ss/services.c      |  23 +++++-
 6 files changed, 165 insertions(+), 4 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 +2 -0
index efa6108b1ce9..1f270206f0ee 100644
@@ -7284,6 +7284,7 @@ DEFINE_LSM(selinux) = {
 	.enabled = &selinux_enabled_boot,
 	.blobs = &selinux_blob_sizes,
 	.init = selinux_init,
+	.security_state = selinux_security_state,
 };
 
 #if defined(CONFIG_NETFILTER)
@@ -7394,6 +7395,7 @@ int selinux_disable(struct selinux_state *state)
 	}
 
 	selinux_mark_disabled(state);
+	selinux_security_state();
 
 	pr_info("SELinux:  Disabled at runtime.\n");
 
security/selinux/include/security.h +19 -0
index b0e02cfe3ce1..691c7e35f82a 100644
@@ -222,16 +222,35 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
 	return state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS];
 }
 
+static inline bool selinux_checkreqprot(void)
+{
+	struct selinux_state *state = &selinux_state;
+
+	return 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_selinux_policy(struct selinux_state *state,
+				 void **data, size_t *len,
+				 bool alloc_kernel_memory);
 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 int selinux_security_state(void);
+#else
+static inline int selinux_security_state(void)
+{
+	return 0;
+}
+#endif
+
 #define SEL_VEC_MAX 32
 struct av_decision {
 	u32 allowed;
security/selinux/measure.c new file +122 -0

122 lines not on this page · raw message

security/selinux/selinuxfs.c +1 -0
index 4781314c2510..8f68964e4ba8 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_security_state();
 		if (new_value)
 			avc_ss_reset(state->avc, 0);
 		selnl_notify_setenforce(new_value);
security/selinux/ss/services.c +19 -4
index ef0afd878bfc..0c289d13ef6a 100644
@@ -3724,10 +3724,11 @@ int security_netlbl_sid_to_secattr(struct selinux_state *state,
  * security_read_policy - read the policy.
  * @data: binary policy data
  * @len: length of data in bytes
- *
+ * @alloc_kernel_memory: flag to indicate memory allocation
  */
-int security_read_policy(struct selinux_state *state,
-			 void **data, size_t *len)
+int security_read_selinux_policy(struct selinux_state *state,
+				 void **data, size_t *len,
+				 bool alloc_kernel_memory)
 {
 	struct policydb *policydb = &state->ss->policydb;
 	int rc;
@@ -3738,7 +3739,10 @@ int security_read_policy(struct selinux_state *state,
 
 	*len = security_policydb_len(state);
 
-	*data = vmalloc_user(*len);
+	if (alloc_kernel_memory)
+		*data = kzalloc(*len, GFP_KERNEL);
+	else
+		*data = vmalloc_user(*len);
 	if (!*data)
 		return -ENOMEM;
 
@@ -3754,5 +3758,16 @@ int security_read_policy(struct selinux_state *state,
 
 	*len = (unsigned long)fp.data - (unsigned long)*data;
 	return 0;
+}
 
+/**
+ * security_read_policy - read the policy.
+ * @data: binary policy data
+ * @len: length of data in bytes
+ *
+ */
+int security_read_policy(struct selinux_state *state,
+			 void **data, size_t *len)
+{
+	return security_read_selinux_policy(state, data, len, false);
 }
Signature · 1 line
2.27.0
#3 lore raw

PATCH 3/5 LSM: Add security_state function pointer in lsm_info struct

In reply to Lakshmi Ramasubramanian

The security modules that require their data to be measured using the IMA subsystem need to define a function that the LSM can call to trigger the measurement.

Add a function pointer field namely security_state in lsm_info structure. Update LSM to call this security module function, if defined, to measure the security module's data using the IMA subsystem.

---
 include/linux/lsm_hooks.h |  3 +++
 security/security.c       | 48 ++++++++++++++++++++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)
include/linux/lsm_hooks.h +3 -0
index 95b7c1d32062..a688eb29e5d8 100644
@@ -1591,6 +1591,9 @@ struct lsm_info {
 	int *enabled;		/* Optional: controlled by CONFIG_LSM */
 	int (*init)(void);	/* Required. */
 	struct lsm_blob_sizes *blobs; /* Optional: for blob sharing. */
+	int (*security_state)(void); /* Optional: for measuring the state
+				      * of the security module.
+				      */
 };
 
 extern struct lsm_info __start_lsm_info[], __end_lsm_info[];
security/security.c +47 -1

67 lines not on this page · raw message

Signature · 1 line
2.27.0
#7 lore raw

PATCH 5/5 LSM: Define workqueue for measuring security module state

In reply to Lakshmi Ramasubramanian

Data structures critical to the functioning of a security module could be tampered with by malware or changed inadvertently at runtime thereby disabling or reducing the security guarantees provided by the security module. Such critical data need to be periodically checked and measured, if there is any change. This would enable an attestation service, for instance, to verify that the security modules are operating with the configuration and policy setup by the system administrator.

Define a workqueue in the LSM and invoke the security modules in the workqueue handler to check their data and measure.

Note that the data given by the security module would be measured by the IMA subsystem only if it has changed since the last time it was measured.

---
 security/security.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
security/security.c +26 -0
index 3fbabc2e6ddb..6a0ceff815a2 100644
@@ -89,6 +89,11 @@ static __initdata struct lsm_info *exclusive;
 static struct lsm_info *security_state_lsms;
 static int security_state_lsms_count;
 
+static long security_state_timeout = 300000; /* 5 Minutes */
+static void security_state_handler(struct work_struct *work);
+static DECLARE_DELAYED_WORK(security_state_delayed_work,
+			    security_state_handler);
+
 static __initdata bool debug;
 #define init_debug(...)						\
 	do {							\
@@ -277,6 +282,26 @@ static void __init initialize_security_state_lsms(void)
 	security_state_lsms_count = count;
 }
 
+static void initialize_security_state_monitor(void)
+{
+	if (security_state_lsms_count == 0)
+		return;
+
+	schedule_delayed_work(&security_state_delayed_work,
+			      msecs_to_jiffies(security_state_timeout));
+}
+
+static void security_state_handler(struct work_struct *work)
+{
+	int inx;
+
+	for (inx = 0; inx < security_state_lsms_count; inx++)
+		measure_security_state(&(security_state_lsms[inx]));
+
+	schedule_delayed_work(&security_state_delayed_work,
+			      msecs_to_jiffies(security_state_timeout));
+}
+
 /* Populate ordered LSMs list from comma-separated LSM name list. */
 static void __init ordered_lsm_parse(const char *order, const char *origin)
 {
@@ -400,6 +425,7 @@ static void __init ordered_lsm_init(void)
 	}
 
 	initialize_security_state_lsms();
+	initialize_security_state_monitor();
 
 	kfree(ordered_lsms);
 }
Signature · 1 line
2.27.0
#1 lore raw

PATCH 1/5 IMA: Add LSM_STATE func to measure LSM data

In reply to Lakshmi Ramasubramanian

Critical data structures of security modules need to be measured to enable an attestation service to verify if the policies and configuration have been setup correctly and that they haven't been tampered with at runtime. A new IMA policy is required for handling this measurement.

Define a new IMA policy func namely LSM_STATE to measure data provided by security modules. Update ima_match_rules() to check for LSM_STATE and ima_parse_rule() to handle LSM_STATE.

---
 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  | 29 +++++++++++++++++++++++-----
 4 files changed, 31 insertions(+), 7 deletions(-)
Documentation/ABI/testing/ima_policy +5 -1
index cd572912c593..355bc3eade33 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] [LSM_STATE]
 			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 LSM_STATE to measure LSM data:
+
+			measure func=LSM_STATE
security/integrity/ima/ima.h +1 -0
index 4515975cc540..880fda11a61b 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(LSM_STATE, lsm_state)			\
 	hook(MAX_CHECK, none)
 
 #define __ima_hook_enumify(ENUM, str)	ENUM,
security/integrity/ima/ima_api.c +1 -1
index bf22de8b7ce0..0cebd2404dcf 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 | LSM_STATE
  *	mask: contains the permission mask
  *	fsmagic: hex value
  *
security/integrity/ima/ima_policy.c +24 -5
index 66aa3e17a888..fc8457d9242b 100644
@@ -417,15 +417,31 @@ static bool ima_match_rules(struct ima_rule_entry *rule, struct inode *inode,
 			    const char *keyring)
 {
 	int i;
+	int funcmatch = 0;
 
-	if ((func == KEXEC_CMDLINE) || (func == KEY_CHECK)) {
+	switch (func) {
+	case KEXEC_CMDLINE:
+	case KEY_CHECK:
+	case LSM_STATE:
 		if ((rule->flags & IMA_FUNC) && (rule->func == func)) {
 			if (func == KEY_CHECK)
-				return ima_match_keyring(rule, keyring, cred);
-			return true;
-		}
-		return false;
+				funcmatch = ima_match_keyring(rule, keyring,
+							      cred) ? 1 : -1;
+			else
+				funcmatch = 1;
+		} else
+			funcmatch = -1;
+
+		break;
+
+	default:
+		funcmatch = 0;
+		break;
 	}
+
+	if (funcmatch)
+		return (funcmatch == 1) ? true : false;
+
 	if ((rule->flags & IMA_FUNC) &&
 	    (rule->func != func && func != POST_SETATTR))
 		return false;
@@ -1068,6 +1084,9 @@ static int ima_parse_rule(char *rule, struct ima_rule_entry *entry)
 				entry->func = KEXEC_CMDLINE;
 			else if (strcmp(args[0].from, "KEY_CHECK") == 0)
 				entry->func = KEY_CHECK;
+			else if (strcmp(args[0].from, "LSM_STATE") == 0)
+				entry->func = LSM_STATE;
+
 			else
 				result = -EINVAL;
 			if (!result)
Signature · 1 line
2.27.0
#2 lore raw

PATCH 2/5 IMA: Define an IMA hook to measure LSM data

In reply to Lakshmi Ramasubramanian

IMA subsystem needs to define an IMA hook that the security modules can call to measure critical data of the security modules.

Define a new IMA hook, namely ima_lsm_state(), that the security modules can call to measure data.

---
 include/linux/ima.h               |  4 ++++
 security/integrity/ima/ima_main.c | 17 +++++++++++++++++
 2 files changed, 21 insertions(+)
include/linux/ima.h +4 -0
index 9164e1534ec9..7e2686f4953a 100644
@@ -26,6 +26,7 @@ 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(const void *buf, int size);
+extern void ima_lsm_state(const char *lsm_event_name, const void *buf, int size);
 
 #ifdef CONFIG_IMA_KEXEC
 extern void ima_add_kexec_buffer(struct kimage *image);
@@ -104,6 +105,9 @@ static inline int ima_file_hash(struct file *file, char *buf, size_t buf_size)
 }
 
 static inline void ima_kexec_cmdline(const void *buf, int size) {}
+
+static inline void ima_lsm_state(const char *lsm_event_name,
+				 const void *buf, int size) {}
 #endif /* CONFIG_IMA */
 
 #ifndef CONFIG_IMA_KEXEC
security/integrity/ima/ima_main.c +17 -0
index 8351b2fd48e0..04d9a1d35300 100644
@@ -835,6 +835,23 @@ void ima_kexec_cmdline(const void *buf, int size)
 					   KEXEC_CMDLINE, 0, NULL);
 }
 
+/**
+ * ima_lsm_state - measure LSM specific state
+ * @lsm_event_name: LSM event
+ * @buf: pointer to buffer containing LSM specific state
+ * @size: Number of bytes in buf
+ *
+ * Buffers can only be measured, not appraised.
+ */
+void ima_lsm_state(const char *lsm_event_name, const void *buf, int size)
+{
+	if (!lsm_event_name || !buf || !size)
+		return;
+
+	process_buffer_measurement(buf, size, lsm_event_name,
+				   LSM_STATE, 0, NULL);
+}
+
 static int __init init_ima(void)
 {
 	int error;
Signature · 1 line
2.27.0
maintainer
#5 lore raw

Re: [PATCH v1 4/5] LSM: Define SELinux function to measure security state

In reply to Lakshmi Ramasubramanian
Quoted 30 lines · Lakshmi Ramasubramanian

SELinux configuration and policy are some of the critical data for this security module that needs to be measured. To enable this measurement SELinux needs to implement the interface function, security_state(), that the LSM can call.

Define the security_state() function in SELinux to measure SELinux configuration and policy. Call this function to measure SELinux data when there is a change in the security module's state.

Sample measurement of SELinux state and hash of the policy:

10 e32e...5ac3 ima-buf sha256:86e8...4594 selinux-state 656e61626c65643d313b656e666f7263696e673d303b636865636b72657170726f743d313b6e6574706565723d313b6f70656e7065726d3d313b657874736f636b636c6173733d313b616c776179736e6574776f726b3d303b6367726f75707365636c6162656c3d313b6e6e706e6f737569647472616e736974696f6e3d313b67656e66737365636c6162656c73796d6c696e6b3d303b 10 f4a7...9408 ima-buf sha256:4941...68fc selinux-policy-hash 8d1d...1834

The data for selinux-state in the above measurement is:
enabled=1;enforcing=0;checkreqprot=1;netpeer=1;openperm=1;extsockclass=1;alwaysnetwork=0;cgroupseclabel=1;nnpnosuidtransition=1;genfsseclabelsymlink=0;

The data for selinux-policy-hash in the above measurement is the SHA256 hash of the SELinux policy.

---

security/selinux/include/security.h +0 -0
index b0e02cfe3ce1..691c7e35f82a 100644

security/selinux/include/security.h static inline bool selinux_policycap_genfs_seclabel_symlinks(void) · line 230

Quoted 9 lines
security/selinux/include/security.h +6 -0
@@ -222,16 +222,35 @@ static inline bool selinux_policycap_genfs_seclabel_symlinks(void)
        return state->policycap[POLICYDB_CAPABILITY_GENFS_SECLABEL_SYMLINKS];
 }
 
+static inline bool selinux_checkreqprot(void)
+{
+       struct selinux_state *state = &selinux_state;
+
+       return state->checkreqprot;
+}

Probably should use READ_ONCE().

security/selinux/measure.c line 1

Quoted 7 lines
security/selinux/measure.c new file +1 -0
new file mode 100644
index 000000000000..b909e8e61542
@@ -0,0 +1,122 @@
+int selinux_security_state(void)

Let's call this selinux_measure_state() or similar. Needs a verb. And pass it a struct selinux_state * pointer argument to be measured, even though initially it will always be passed &selinux_state. The encapsulation of selinux state within selinux_state was to support multiple selinux namespaces in the future, each with their own state.

security/selinux/measure.c line 73

Quoted 13 lines
security/selinux/measure.c new file +13 -0
+{
+       int rc = 0;
+       int count;
+       size_t buflen;
+       int policy_hash_len;
+       char *state = NULL;
+       void *policy = NULL;
+       void *policy_hash = NULL;
+       static char *security_state_string =
+                       "enabled=%d;enforcing=%d;checkreqprot=%d;"        \
+                       "netpeer=%d;openperm=%d;extsockclass=%d;"         \
+                       "alwaysnetwork=%d;cgroupseclabel=%d;"             \
+                       "nnpnosuidtransition=%d;genfsseclabelsymlink=%d;";

Rather than hardcoding policy capability names here, I would recommend using the selinux_policycap_names[] array for the names and the selinux_state.policycap[] array for the values. Also recommend passing in a struct selinux_state * here to allow for future case where there are multiple selinux states, one per selinux namespace.

Quoted 6 lines
security/selinux/ss/services.c +0 -0
index ef0afd878bfc..0c289d13ef6a 100644
@@ -3724,10 +3724,11 @@ int security_netlbl_sid_to_secattr(struct selinux_state *state,
  * security_read_policy - read the policy.

security/selinux/ss/services.c int security_netlbl_sid_to_secattr(struct selinux_state *state, · line 3731

Quoted 10 lines
security/selinux/ss/services.c +4 -3
  * @data: binary policy data
  * @len: length of data in bytes
- *
+ * @alloc_kernel_memory: flag to indicate memory allocation
  */
-int security_read_policy(struct selinux_state *state,
-                        void **data, size_t *len)
+int security_read_selinux_policy(struct selinux_state *state,
+                                void **data, size_t *len,
+                                bool alloc_kernel_memory)

Instead of passing in a boolean to control how the memory is allocated, split this into a helper function that takes an already-allocated buffer and two
different front-end wrappers, one for kernel-internal use and one for userspace use.

security/selinux/ss/services.c int security_read_policy(struct selinux_state *state, · line 3743

Quoted 7 lines
security/selinux/ss/services.c +2 -1
@@ -3738,7 +3739,10 @@ int security_read_policy(struct selinux_state *state,
 
        *len = security_policydb_len(state);
 
-       *data = vmalloc_user(*len);
+       if (alloc_kernel_memory)
+               *data = kzalloc(*len, GFP_KERNEL);

You need vmalloc() since policy image size may exceed kmalloc max (or at least that used to be the case).

#6 lore raw

Re: [PATCH v1 4/5] LSM: Define SELinux function to measure security state

Quoted 8 lines · Stephen Smalley

security/selinux/include/security.h static inline bool selinux_policycap_genfs_seclabel_symlinks(void) · line 230

Quoted 6 lines
security/selinux/include/security.h +6 -0
+static inline bool selinux_checkreqprot(void)
+{
+       struct selinux_state *state = &selinux_state;
+
+       return state->checkreqprot;
+}

Probably should use READ_ONCE().

Will do.

Quoted 14 lines

security/selinux/measure.c line 1

Quoted 8 lines
security/selinux/measure.c new file +1 -0
new file mode 100644
index 000000000000..b909e8e61542
@@ -0,0 +1,122 @@
+int selinux_security_state(void)

Let's call this selinux_measure_state() or similar. Needs a verb. And pass it a struct selinux_state * pointer argument to be measured, even though initially it will always be passed &selinux_state. The encapsulation of selinux state within selinux_state was to support multiple selinux namespaces in the future, each with their own state.

Will do.

Quoted 11 lines

security/selinux/measure.c line 73

Quoted 5 lines
security/selinux/measure.c new file +5 -0
+       static char *security_state_string =
+                       "enabled=%d;enforcing=%d;checkreqprot=%d;"        \
+                       "netpeer=%d;openperm=%d;extsockclass=%d;"         \
+                       "alwaysnetwork=%d;cgroupseclabel=%d;"             \
+                       "nnpnosuidtransition=%d;genfsseclabelsymlink=%d;";

Rather than hardcoding policy capability names here, I would recommend using the selinux_policycap_names[] array for the names and the selinux_state.policycap[] array for the values. Also recommend passing in a struct selinux_state * here to allow for future case where there are multiple selinux states, one per selinux namespace.

Will do.

Quoted 15 lines
Quoted 15 lines
security/selinux/ss/services.c +2 -3
index ef0afd878bfc..0c289d13ef6a 100644
@@ -3724,10 +3724,11 @@ int security_netlbl_sid_to_secattr(struct selinux_state *state,
   * security_read_policy - read the policy.
   * @data: binary policy data
   * @len: length of data in bytes
- *
+ * @alloc_kernel_memory: flag to indicate memory allocation
   */
-int security_read_policy(struct selinux_state *state,
-                        void **data, size_t *len)
+int security_read_selinux_policy(struct selinux_state *state,
Quoted 8 lines

security/selinux/ss/services.c int security_netlbl_sid_to_secattr(struct selinux_state *state, · line 3731

Quoted 2 lines
security/selinux/ss/services.c +2 -0
+                                void **data, size_t *len,
+                                bool alloc_kernel_memory)

Instead of passing in a boolean to control how the memory is allocated, split this into a helper function that takes an already-allocated buffer and two
different front-end wrappers, one for kernel-internal use and one for userspace use.

Will do.

Quoted 11 lines

security/selinux/ss/services.c int security_read_policy(struct selinux_state *state, · line 3743

Quoted 8 lines
security/selinux/ss/services.c +2 -1
@@ -3738,7 +3739,10 @@ int security_read_policy(struct selinux_state *state,
 
         *len = security_policydb_len(state);
 
-       *data = vmalloc_user(*len);
+       if (alloc_kernel_memory)
+               *data = kzalloc(*len, GFP_KERNEL);

You need vmalloc() since policy image size may exceed kmalloc max (or at least that used to be the case).

Will do.

thanks,
-lakshmi