Skip to contentSkip to conversation

selinux: reject writable opens of status file, drop mmap write checks

PATCH In review Stephen Smalley 4 participants 6 messages Sep 11 – Sep 14 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v2 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. This does NOT prevent truncation via open(O_RDONLY|O_TRUNC) or truncate(), which is left to another patch.

---
v2 corrects my previous incorrect claim re preventing truncation, deferring that to a separate patch like the still-pending one from cgzones, and also moves the FMODE_WRITE check before the status page allocation.

 security/selinux/selinuxfs.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
security/selinux/selinuxfs.c +5 -6
index 292302eb60f3..835594d221fc 100644
@@ -214,8 +214,12 @@ static const struct file_operations sel_handle_unknown_ops = {
 
 static int sel_open_handle_status(struct inode *inode, struct file *filp)
 {
-	struct page    *status = selinux_kernel_status_page();
+	struct page    *status;
 
+	if (filp->f_mode & FMODE_WRITE)
+		return -EACCES;
+
+	status = selinux_kernel_status_page();
 	if (!status)
 		return -ENOMEM;
 
@@ -247,11 +251,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
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

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. This does NOT prevent truncation via open(O_RDONLY|O_TRUNC) or truncate(), which is left to another patch.

Quoted 7 lines

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Acked-by on v2 1/1
#2 lore raw
In reply to Stephen Smalley
Quoted 2 lines · Stephen Smalley

Similar to what
https://lore.kernel.org/selinux/aqPUqU3eZeFrykj3@gremlin/ does for the

NIT: Probably better to have that as a Link: tag ?

Quoted 5 lines

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. This does NOT prevent truncation via open(O_RDONLY|O_TRUNC) or truncate(), which is left to another patch.

Makes sense!

Quoted 8 lines

Seems reasonable to me so:

Quoted 28 lines

---
v2 corrects my previous incorrect claim re preventing truncation, deferring that to a separate patch like the still-pending one from cgzones, and also moves the FMODE_WRITE check before the status page allocation.

 security/selinux/selinuxfs.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
security/selinux/selinuxfs.c +5 -1
index 292302eb60f3..835594d221fc 100644
@@ -214,8 +214,12 @@ static const struct file_operations sel_handle_unknown_ops = {
 
 static int sel_open_handle_status(struct inode *inode, struct file *filp)
 {
-	struct page    *status = selinux_kernel_status_page();
+	struct page    *status;
 
+	if (filp->f_mode & FMODE_WRITE)
+		return -EACCES;
+
+	status = selinux_kernel_status_page();
 	if (!status)
 		return -ENOMEM;
 

security/selinux/selinuxfs.c static int sel_mmap_handle_status(struct file *filp, · line 254

Quoted 8 lines
security/selinux/selinuxfs.c +0 -5
@@ -247,11 +251,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);

A note for any curious as to why I didn't touch this in my patch - it's because it's a PFN remap.

This means it's a kernel-owned mapping, where dropping VMA_MAYWRITE_BIT is permitted, so it wouldn't have fallen foul of my series' checks which disallow this for non-kernel-owned mappings.

Quoted 6 lines
security/selinux/selinuxfs.c +0 -1
 	return remap_pfn_range(vma, vma->vm_start,
 			       page_to_pfn(status),
--

2.55.0

--
Cheers, Lorenzo

#3 lore raw
In reply to Stephen Smalley
Quoted 8 lines · Stephen Smalley

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. This does NOT prevent truncation via open(O_RDONLY|O_TRUNC) or truncate(), which is left to another patch.

Quoted 7 lines

Hum, so rather than having special checks in ->open, why don't you set S_IMMUTABLE bit for the inodes which you really don't want anybody to modify? That will block both writeable opens as well as truncate...

Honza

Quoted 42 lines

---
v2 corrects my previous incorrect claim re preventing truncation, deferring that to a separate patch like the still-pending one from cgzones, and also moves the FMODE_WRITE check before the status page allocation.

 security/selinux/selinuxfs.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
security/selinux/selinuxfs.c +5 -6
index 292302eb60f3..835594d221fc 100644
@@ -214,8 +214,12 @@ static const struct file_operations sel_handle_unknown_ops = {
 
 static int sel_open_handle_status(struct inode *inode, struct file *filp)
 {
-	struct page    *status = selinux_kernel_status_page();
+	struct page    *status;
 
+	if (filp->f_mode & FMODE_WRITE)
+		return -EACCES;
+
+	status = selinux_kernel_status_page();
 	if (!status)
 		return -ENOMEM;
 
@@ -247,11 +251,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
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#4 lore raw
Quoted 18 lines · Jan Kara
Quoted 8 lines · Stephen Smalley

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. This does NOT prevent truncation via open(O_RDONLY|O_TRUNC) or truncate(), which is left to another patch.

Quoted 7 lines
Quoted 3 lines

Hum, so rather than having special checks in ->open, why don't you set S_IMMUTABLE bit for the inodes which you really don't want anybody to modify? That will block both writeable opens as well as truncate...

Great idea. I will do that in v3, drop the .open checking, but still remove the .mmap checking that will then be obsoleted, unless someone objects?

#5 lore raw
Quoted 19 lines · Stephen Smalley
Quoted 18 lines · Jan Kara
Quoted 15 lines · Stephen Smalley

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. This does NOT prevent truncation via open(O_RDONLY|O_TRUNC) or truncate(), which is left to another patch.

Quoted 7 lines
Quoted 3 lines

Hum, so rather than having special checks in ->open, why don't you set S_IMMUTABLE bit for the inodes which you really don't want anybody to modify? That will block both writeable opens as well as truncate...

Great idea. I will do that in v3, drop the .open checking, but still remove the .mmap checking that will then be obsoleted, unless someone objects?

That sounds good to me.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR