selinux: shrink critical section in sel_write_load()
Stephen Smalley
maintainerPATCH v2 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. This avoids holding it unnecessarily across the vmalloc() and copy_from_user() of the policy data.
---
v2 moves the taking of the policy mutex just before security_load_policy() is called. This is required by security_load_policy() to protect the old policy during its own operation and until the new policy is committed. There is already a lockdep_is_held() check for this inside of security_load_policy(). v2 also simplifies and unifies the error paths, at a cost of not releasing the policy mutex until after the audit message is logged.
security/selinux/selinuxfs.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
security/selinux/selinuxfs.c +8 -10
@@ -592,34 +592,31 @@ 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) - goto out; +return length; data =vmalloc (count); -if (!data) { - length = -ENOMEM ; - goto out; - } +if (!data) +return -ENOMEM ;if (copy_from_user (data, buf, count) !=0 ) { length = -EFAULT ; goto out; } +mutex_lock (&selinux_state.policy_mutex); length =security_load_policy (data, count, &load_state);if (length) {pr_warn_ratelimited ("SELinux: failed to load policy\n" ); - goto out; + goto out_unlock; } fsi =file_inode (file)->i_sb->s_fs_info; 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); - goto out; + goto out_unlock; }selinux_policy_commit (&load_state);
@@ -629,8 +626,9 @@ static ssize_t sel_write_load(struct file *file, const char __user *buf,from_kuid (&init_user_ns,audit_get_loginuid (current)), audit_get_sessionid(current)); -out: +out_unlock:mutex_unlock (&selinux_state.policy_mutex); +out:vfree (data);return length; }
Signature · 1 line
2.53.0
Quoted 17 lines · On Apr 30, 2026 Stephen Smalley <[email protected]> wrote:
Currently
sel_write_load()takes the policy mutex earlier than necessary. Move the taking of the mutex later. This avoids holding it unnecessarily across thevmalloc()andcopy_from_user()of the policy data.Signed-off-by Stephen Smalley <[email protected]>---
v2 moves the taking of the policy mutex just beforesecurity_load_policy()is called. This is required bysecurity_load_policy()to protect the old policy during its own operation and until the new policy is committed. There is already alockdep_is_held()check for this inside ofsecurity_load_policy(). v2 also simplifies and unifies the error paths, at a cost of not releasing the policy mutex until after the audit message is logged.
Quoted 2 lines
security/selinux/selinuxfs.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-)
Merged into selinux/stable-7.1, thanks.
--
paul-moore.com