Skip to contentSkip to conversation

selinux: shrink critical section in sel_write_load()

PATCH Superseded Stephen Smalley 1 participant 2 messages Apr 30 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 selinux: shrink critical section in sel_write_load()

Currently sel_write_load() takes the policy mutex earlier than necessary. Move the taking of the mutex later and drop it as soon as we are done committing the new policy. This avoids holding it unnecessarily across the vmalloc() and copy_from_user() for the policy data.

---
 security/selinux/selinuxfs.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
security/selinux/selinuxfs.c +6 -3
index c4a68d623d40..df8181a496c8 100644
@@ -592,8 +592,6 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
 	if (!count)
 		return -EINVAL;
 
-	mutex_lock(&selinux_state.policy_mutex);
-
 	length = avc_has_perm(current_sid(), SECINITSID_SECURITY,
 			      SECCLASS_SECURITY, SECURITY__LOAD_POLICY, NULL);
 	if (length)
@@ -614,15 +612,21 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
 		pr_warn_ratelimited("SELinux: failed to load policy\n");
 		goto out;
 	}
+
 	fsi = file_inode(file)->i_sb->s_fs_info;
+
+	mutex_lock(&selinux_state.policy_mutex);
 	length = sel_make_policy_nodes(fsi, load_state.policy);
 	if (length) {
 		pr_warn_ratelimited("SELinux: failed to initialize selinuxfs\n");
 		selinux_policy_cancel(&load_state);
+		mutex_unlock(&selinux_state.policy_mutex);
 		goto out;
 	}
 
 	selinux_policy_commit(&load_state);
+	mutex_unlock(&selinux_state.policy_mutex);
+
 	length = count;
 	audit_log(audit_context(), GFP_KERNEL, AUDIT_MAC_POLICY_LOAD,
 		"auid=%u ses=%u lsm=selinux res=1",
@@ -630,7 +634,6 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,
 		audit_get_sessionid(current));
 
 out:
-	mutex_unlock(&selinux_state.policy_mutex);
 	vfree(data);
 	return length;
 }
Signature · 1 line
2.53.0
author maintainer
#1 lore raw
Quoted 9 lines · Stephen Smalley

Currently sel_write_load() takes the policy mutex earlier than necessary. Move the taking of the mutex later and drop it as soon as we are done committing the new policy. This avoids holding it unnecessarily across the vmalloc() and copy_from_user() for the policy data.

---

Sorry, this is wrong - please ignore.
v2 coming up.