selinux: allow multiple opens of /sys/fs/selinux/policy
Stephen Smalley
maintainerPATCH v2 1/1 selinux: allow multiple opens of /sys/fs/selinux/policy
Currently there can only be a single open of /sys/fs/selinux/policy at any time. This allows any process to block any other process from reading the kernel policy. The original motivation seems to have been a mix of preventing an inconsistent view of the policy size and preventing userspace from allocating kernel memory without bound, but this is arguably equally bad. Eliminate the policy_opened flag and shrink the critical section that the policy mutex is held. While we are making changes here, drop a couple of extraneous BUG_ONs.
---
v2 shrinks the critical section that the policy mutex is held and removes a couple of extraneous BUG_ONs.
security/selinux/selinuxfs.c | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-)
security/selinux/selinuxfs.c +4 -23
@@ -76,7 +76,6 @@ struct selinux_fs_info {int *bool_pending_values;struct dentry *class_dir;unsigned long last_class_ino; -bool policy_opened;unsigned long last_ino;struct super_block *sb; };
@@ -360,44 +359,31 @@ struct policy_load_memory {static int sel_open_policy (struct inode *inode,struct file *filp) { -struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;struct policy_load_memory *plm =NULL ;int rc; -BUG_ON (filp->private_data); - -mutex_lock (&selinux_state.policy_mutex); - rc =avc_has_perm (current_sid (),SECINITSID_SECURITY ,SECCLASS_SECURITY ,SECURITY__READ_POLICY ,NULL );if (rc) - goto err; - - rc = -EBUSY ; -if (fsi->policy_opened) - goto err; +return rc; - rc = -ENOMEM ; plm =kzalloc_obj (*plm);if (!plm) - goto err; +return -ENOMEM ; +mutex_lock (&selinux_state.policy_mutex); rc =security_read_policy (&plm->data, &plm->len);if (rc) goto err; -if ((size_t )i_size_read (inode) != plm->len) {inode_lock (inode);i_size_write (inode, plm->len);inode_unlock (inode); } - - fsi->policy_opened =1 ; +mutex_unlock (&selinux_state.policy_mutex); filp->private_data = plm; -mutex_unlock (&selinux_state.policy_mutex); -return 0 ; err:mutex_unlock (&selinux_state.policy_mutex);
@@ -410,13 +396,8 @@ static int sel_open_policy(struct inode *inode, struct file *filp)static int sel_release_policy (struct inode *inode,struct file *filp) { -struct selinux_fs_info *fsi = inode->i_sb->s_fs_info;struct policy_load_memory *plm = filp->private_data; -BUG_ON (!plm); - - fsi->policy_opened =0 ; -vfree (plm->data);kfree (plm);
Signature · 1 line
2.54.0
Paul Moore
maintainerQuoted 10 lines · On May 5, 2026 Stephen Smalley <[email protected]> wrote:
Currently there can only be a single open of
/sys/fs/selinux/policyat any time. This allows any process to block any other process from reading the kernel policy. The original motivation seems to have been a mix of preventing an inconsistent view of the policy size and preventing userspace from allocating kernel memory without bound, but this is arguably equally bad. Eliminate thepolicy_openedflag and shrink the critical section that the policy mutex is held. While we are making changes here, drop a couple of extraneousBUG_ONs.
Quoted 8 lines
Signed-off-by Stephen Smalley <[email protected]>---
v2 shrinks the critical section that the policy mutex is held and removes a couple of extraneousBUG_ONs.security/selinux/selinuxfs.c | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-)
I agree the single reader restriction likely is doing more harm than good. Merged into selinux/stable-7.1, thanks.
--
paul-moore.com
Stephen Smalley
maintainerQuoted 18 lines · Paul Moore
Quoted 16 lines · On May 5, 2026 Stephen Smalley <[email protected]> wrote:
Currently there can only be a single open of
/sys/fs/selinux/policyat any time. This allows any process to block any other process from reading the kernel policy. The original motivation seems to have been a mix of preventing an inconsistent view of the policy size and preventing userspace from allocating kernel memory without bound, but this is arguably equally bad. Eliminate thepolicy_openedflag and shrink the critical section that the policy mutex is held. While we are making changes here, drop a couple of extraneousBUG_ONs.Signed-off-by Stephen Smalley <[email protected]>---
v2 shrinks the critical section that the policy mutex is held and removes a couple of extraneousBUG_ONs.
Quoted 5 lines
Quoted 2 lines
security/selinux/selinuxfs.c | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-)I agree the single reader restriction likely is doing more harm than good. Merged into selinux/stable-7.1, thanks.
One option if we are concerned with the kernel memory allocations would be to save a reference-counted copy of the policy data when it is first loaded (or upon first open of /sys/fs/selinux/policy) and just have sel_open_policy() reuse that rather than allocating memory and regenerating the serialized policy on each sel_open_policy(). NB This would mean however that if the policy data structures were modified at runtime (either via booleans or kernel write vuln), that wouldn't be reflected in reads of /sys/fs/selinux/policy.