Skip to contentSkip to conversation

selinux: add permission checks for loading other kinds of kernel files

PATCH Superseded [email protected] 3 participants 3 messages Feb 5, 2025 – Feb 21, 2025 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selinux: add permission checks for loading other kinds of kernel files

From: "Kipp N. Davis" <[email protected]>

Although the LSM hooks for loading kernel modules were later generalized to cover loading other kinds of files, SELinux didn't implement corresponding permission checks, leaving only the module case covered. Define and add new permission checks for these other cases.

---
 security/selinux/hooks.c            | 54 ++++++++++++++++++++++++-----
 security/selinux/include/classmap.h |  4 ++-
 2 files changed, 49 insertions(+), 9 deletions(-)
security/selinux/hooks.c +46 -8
index 7b867dfec88b..e96ade50c137 100644
@@ -4096,7 +4096,7 @@ static int selinux_kernel_module_request(char *kmod_name)
 			    SYSTEM__MODULE_REQUEST, &ad);
 }
 
-static int selinux_kernel_module_from_file(struct file *file)
+static int selinux_kernel_load_from_file(struct file *file, u32 requested)
 {
 	struct common_audit_data ad;
 	struct inode_security_struct *isec;
@@ -4104,12 +4104,9 @@ static int selinux_kernel_module_from_file(struct file *file)
 	u32 sid = current_sid();
 	int rc;
 
-	/* init_module */
 	if (file == NULL)
 		return avc_has_perm(sid, sid, SECCLASS_SYSTEM,
-					SYSTEM__MODULE_LOAD, NULL);
-
-	/* finit_module */
+					requested, NULL);
 
 	ad.type = LSM_AUDIT_DATA_FILE;
 	ad.u.file = file;
@@ -4123,7 +4120,7 @@ static int selinux_kernel_module_from_file(struct file *file)
 
 	isec = inode_security(file_inode(file));
 	return avc_has_perm(sid, isec->sid, SECCLASS_SYSTEM,
-				SYSTEM__MODULE_LOAD, &ad);
+				requested, &ad);
 }
 
 static int selinux_kernel_read_file(struct file *file,
@@ -4133,10 +4130,33 @@ static int selinux_kernel_read_file(struct file *file,
 	int rc = 0;
 
 	switch (id) {
+	case READING_FIRMWARE:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__FIRMWARE_LOAD);
+		break;
 	case READING_MODULE:
-		rc = selinux_kernel_module_from_file(contents ? file : NULL);
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__MODULE_LOAD);
+		break;
+	case READING_KEXEC_IMAGE:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__KEXEC_IMAGE_LOAD);
+		break;
+	case READING_KEXEC_INITRAMFS:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__KEXEC_INITRAMFS_LOAD);
+		break;
+	case READING_POLICY:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__POLICY_LOAD);
+		break;
+	case READING_X509_CERTIFICATE:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__X509_CERTIFICATE_LOAD);
 		break;
 	default:
+		pr_err("SELinux:  kernel_read_file_id unknown");
+		rc = -EACCES;
 		break;
 	}
 
@@ -4148,10 +4168,28 @@ static int selinux_kernel_load_data(enum kernel_load_data_id id, bool contents)
 	int rc = 0;
 
 	switch (id) {
+	case LOADING_FIRMWARE:
+		rc = selinux_kernel_load_from_file(NULL, SYSTEM__FIRMWARE_LOAD);
+		break;
 	case LOADING_MODULE:
-		rc = selinux_kernel_module_from_file(NULL);
+		rc = selinux_kernel_load_from_file(NULL, SYSTEM__MODULE_LOAD);
+		break;
+	case LOADING_KEXEC_IMAGE:
+		rc = selinux_kernel_load_from_file(NULL, SYSTEM__KEXEC_IMAGE_LOAD);
+		break;
+	case LOADING_KEXEC_INITRAMFS:
+		rc = selinux_kernel_load_from_file(NULL, SYSTEM__KEXEC_INITRAMFS_LOAD);
+		break;
+	case LOADING_POLICY:
+		rc = selinux_kernel_load_from_file(NULL, SYSTEM__POLICY_LOAD);
+		break;
+	case LOADING_X509_CERTIFICATE:
+		rc = selinux_kernel_load_from_file(NULL,
+				SYSTEM__X509_CERTIFICATE_LOAD);
 		break;
 	default:
+		pr_err("SELinux:  kernel_read_file_id unknown");
+		rc = -EACCES;
 		break;
 	}
 
security/selinux/include/classmap.h +3 -1
index 03e82477dce9..cfac41d12f7d 100644
@@ -63,7 +63,9 @@ const struct security_class_mapping secclass_map[] = {
 	{ "process2", { "nnp_transition", "nosuid_transition", NULL } },
 	{ "system",
 	  { "ipc_info", "syslog_read", "syslog_mod", "syslog_console",
-	    "module_request", "module_load", NULL } },
+	    "module_request", "module_load", "firmware_load",
+	    "kexec_image_load", "kexec_initramfs_load", "policy_load",
+	    "x509_certificate_load", NULL } },
 	{ "capability", { COMMON_CAP_PERMS, NULL } },
 	{ "filesystem",
 	  { "mount", "remount", "unmount", "getattr", "relabelfrom",

--
2.48.1

maintainer
#1 lore raw
Quoted 6 lines · On Feb 5, 2025 [email protected] wrote:

Although the LSM hooks for loading kernel modules were later generalized to cover loading other kinds of files, SELinux didn't implement corresponding permission checks, leaving only the module case covered. Define and add new permission checks for these other cases.

Quoted 6 lines
---
 security/selinux/hooks.c            | 54 ++++++++++++++++++++++++-----
 security/selinux/include/classmap.h |  4 ++-
 2 files changed, 49 insertions(+), 9 deletions(-)

Thanks for putting this patch together, and double thank you for the tests too! If you've got the time, it would be great if you could submit a patch/PR to update notebook too:

* https://github.com/SELinuxProject/selinux-notebook

Some small comments below ...

Quoted 26 lines
security/selinux/hooks.c +16 -1
index 7b867dfec88b..e96ade50c137 100644
@@ -4133,10 +4130,33 @@ static int selinux_kernel_read_file(struct file *file,
 	int rc = 0;
 
 	switch (id) {
+	case READING_FIRMWARE:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__FIRMWARE_LOAD);
+		break;
 	case READING_MODULE:
-		rc = selinux_kernel_module_from_file(contents ? file : NULL);
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__MODULE_LOAD);
+		break;
+	case READING_KEXEC_IMAGE:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__KEXEC_IMAGE_LOAD);
+		break;
+	case READING_KEXEC_INITRAMFS:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__KEXEC_INITRAMFS_LOAD);
+		break;
+	case READING_POLICY:

security/selinux/hooks.c static int selinux_kernel_read_file(struct file *file, · line 4159

Quoted 10 lines
security/selinux/hooks.c +8 -0
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__POLICY_LOAD);
+		break;
+	case READING_X509_CERTIFICATE:
+		rc = selinux_kernel_load_from_file(contents ? file : NULL,
+				SYSTEM__X509_CERTIFICATE_LOAD);
 		break;
 	default:
+		pr_err("SELinux:  kernel_read_file_id unknown");
+		rc = -EACCES;

If we can't come up with a way to catch new READING_XXX entries at compile time, e.g. socket address families or capabilities (grep for "#error" in security/selinux/hooks.c), I'm not sure we want to return an error here, both because we haven't previously and it ignores the loaded policy's UNK_PERMS setting.

This also applies to selinux_kernel_load_data().

security/selinux/hooks.c static int selinux_kernel_read_file(struct file *file, · line 4161

Quoted 2 lines
security/selinux/hooks.c +0 -0
 		break;
 	}

--
paul-moore.com

#2 lore raw
Quoted 14 lines · Paul Moore
Quoted 6 lines · On Feb 5, 2025 [email protected] wrote:

Although the LSM hooks for loading kernel modules were later generalized to cover loading other kinds of files, SELinux didn't implement corresponding permission checks, leaving only the module case covered. Define and add new permission checks for these other cases.

Quoted 6 lines
---
  security/selinux/hooks.c            | 54 ++++++++++++++++++++++++-----
  security/selinux/include/classmap.h |  4 ++-
  2 files changed, 49 insertions(+), 9 deletions(-)
Quoted 5 lines

Thanks for putting this patch together, and double thank you for the tests too! If you've got the time, it would be great if you could submit a patch/PR to update notebook too:

* https://github.com/SELinuxProject/selinux-notebook

I went ahead and put together a notebook PR here: https://github.com/SELinuxProject/selinux-notebook/pull/45

Kipp, if you are able to take a look and make sure I have the details right, that would be appreciated.

I'll keep the notebook PR updated if the permissions change and merge it after the code is in -next.

-Daniel