selinux: reject writable opens of status file, drop mmap write checks
Stephen Smalley
maintainerPATCH 1/1 selinux: reject writable opens of status file, drop mmap write checks
Similar to what
https://lore.kernel.org/selinux/aqPUqU3eZeFrykj3@gremlin/ does for the policy file, update the .open handler for the status file to reject writable opens, and update the .mmap handler to stop clearing VM_MAYWRITE and checking for VM_WRITE. As a side effect, I believe this will also prevent the issue previously reported with libselinux users like systemd seg-faulting on the mmap'd status file if it is truncated.
--- security/selinux/selinuxfs.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
security/selinux/selinuxfs.c +3 -5
@@ -219,6 +219,9 @@ static int sel_open_handle_status(struct inode *inode, struct file *filp)if (!status)return -ENOMEM ; +if (filp->f_mode &FMODE_WRITE ) +return -EACCES ; + filp->private_data = status;return 0 ;
@@ -247,11 +250,6 @@ static int sel_mmap_handle_status(struct file *filp,/* only allows one page from the head */ if (vma->vm_pgoff >0 || size !=PAGE_SIZE )return -EIO ; -/* disallow writable mapping */ -if (vma->vm_flags &VM_WRITE ) -return -EPERM ; -/* disallow mprotect() turns it into writable */ -vm_flags_clear (vma,VM_MAYWRITE ); return remap_pfn_range(vma, vma->vm_start, page_to_pfn(status),
Signature · 1 line
2.55.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch fails to fully prevent truncation of the SELinux status file, leaving it vulnerable to path-based truncate() and open(O_RDONLY | O_TRUNC). --
commit 4651117ec33990214895f2560efc1a4e7aea0f83
Author: Stephen Smalley <[email protected]>
selinux: reject writable opens of status file, drop mmap write checks
This commit updates the .open handler for the SELinux status file to reject writable opens, while removing mmap write checks. The stated goal is to prevent userspace tools like systemd from segfaulting when the mmap'd status file is truncated.
security/selinux/selinuxfs.c static int sel_open_handle_status(struct inode *inode, struct file *filp) · line 224
Quoted 11 lines
security/selinux/selinuxfs.c +3
-0@@ -219,6 +219,9 @@ static int sel_open_handle_status(struct inode *inode, struct file *filp)if (!status)return -ENOMEM ; +if (filp->f_mode &FMODE_WRITE ) +return -EACCES ; +
[Severity: High]
This is a pre-existing issue, but it looks like this might be an incomplete fix for the truncation problem. Does this check fully prevent truncation of the SELinux status file?
Because open(O_RDONLY | O_TRUNC) only sets FMODE_READ (and not FMODE_WRITE), it bypasses this check in sel_open_handle_status() while still triggering VFS truncation.
Additionally, a privileged process can invoke the path-based truncate() syscall, which bypasses the .open() handler completely and falls back to simple_setattr() to truncate the pagecache.
Since the goal of this patch is to prevent truncation to stop libselinux users like systemd from segfaulting, it seems both of these paths still allow the file to be truncated and trigger the crash.
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1