Skip to contentSkip to conversation

Fix SELinux denials against target driver

PATCH Superseded Maurizio Lombardi 1 participant 3 messages Feb 15, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Fix SELinux denials against target driver

Steps to reproduce:

1) install the ibacm, rdma-core and targetcli
2) service ibacm start (ignore the errors)
3) Look at the dmesg, you will see an error message like "db_root: cannot open: /etc/target"

4) Execute $ sudo ausearch -m AVC,USER_AVC -ts recent

type=AVC msg=audit(1707990698.893:610): avc: denied { read } for pid=26447 comm="systemd-modules" name="target" dev="dm-0" ino=973050 scontext=system_u:system_r:systemd_modules_load_t:s0 tcontext=system_u:object_r:targetd_etc_rw_t:s0 tclass=dir permissive=0

Fix inspired by commit 581dd69830341d299b0c097fc366097ab497d679

Maurizio Lombardi (1):
target: fix selinux error when systemd-modules loads the target module

 drivers/target/target_core_configfs.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
Signature · 1 line
2.39.3
#1 lore raw

PATCH 1/1 target: fix selinux error when systemd-modules loads the target module

If the systemd-modules service loads the target module, the credentials of that userspace process will be used to validate the access to the target db directory.
selinux will prevent it, reporting an error like the following:

kernel: audit: type=1400 audit(1676301082.205:4): avc: denied { read } for pid=1020 comm="systemd-modules" name="target" dev="dm-3" ino=4657583 scontext=system_u:system_r:systemd_modules_load_t:s0 tcontext=system_u:object_r:targetd_etc_rw_t:s0 tclass=dir permissive=0

Fix the error by using the kernel credentials to access the db directory

---
 drivers/target/target_core_configfs.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/target/target_core_configfs.c +11 -0
index a5f58988130a..26c6f1cac677 100644
@@ -3656,6 +3656,8 @@ static int __init target_core_init_configfs(void)
 {
 	struct configfs_subsystem *subsys = &target_core_fabrics;
 	struct t10_alua_lu_gp *lu_gp;
+	struct cred *kern_cred;
+	const struct cred *old_cred;
 	int ret;
 
 	pr_debug("TARGET_CORE[0]: Loading Generic Kernel Storage"
@@ -3732,7 +3734,16 @@ static int __init target_core_init_configfs(void)
 	if (ret < 0)
 		goto out;
 
+	/* We use the kernel credentials to access the target directory */
+	kern_cred = prepare_kernel_cred(&init_task);
+	if (!kern_cred) {
+		ret = -ENOMEM;
+		goto out;
+	}
+	old_cred = override_creds(kern_cred);
 	target_init_dbroot();
+	revert_creds(old_cred);
+	put_cred(kern_cred);
 
 	return 0;
 
Signature · 1 line
2.39.3
#2 lore raw

Re: [PATCH 1/1] target: fix selinux error when systemd-modules loads the target module

čt 15. 2. 2024 v 11:43 odesílatel Maurizio Lombardi <[email protected]> napsal:

drivers/target/target_core_configfs.c static int __init target_core_init_configfs(void) · line 3746

Quoted 12 lines
drivers/target/target_core_configfs.c +9 -0
+       /* We use the kernel credentials to access the target directory */
+       kern_cred = prepare_kernel_cred(&init_task);
+       if (!kern_cred) {
+               ret = -ENOMEM;
+               goto out;
+       }
+       old_cred = override_creds(kern_cred);
        target_init_dbroot();
+       revert_creds(old_cred);
+       put_cred(kern_cred);

I've noticed there is a leak in the error path, I am sending a v2

Maurizio