Skip to contentSkip to conversation

selinux: mark status and policy inodes as immutable, drop status mmap write checks

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

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v3 1/1 selinux: mark status and policy inodes as immutable, drop status mmap write checks

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
security/selinux/selinuxfs.c +14 -5
index 292302eb60f3..0941ce79ea0b 100644
@@ -247,11 +247,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),
@@ -1818,6 +1813,17 @@ static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino)
 
 #define NULL_FILE_NAME "null"
 
+static void sel_mark_immutable(struct dentry *root, const char *name)
+{
+	struct qstr q = QSTR(name);
+	struct dentry *dentry = try_lookup_noperm(&q, root);
+
+	if (!IS_ERR_OR_NULL(dentry)) {
+		d_inode(dentry)->i_flags |= S_IMMUTABLE;
+		dput(dentry);
+	}
+}
+
 static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 {
 	struct selinux_fs_info *fsi;
@@ -1857,6 +1863,9 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
+	sel_mark_immutable(sb->s_root, "status");
+	sel_mark_immutable(sb->s_root, "policy");
+
 	fsi = sb->s_fs_info;
 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
 	if (IS_ERR(fsi->bool_dir)) {
Signature · 1 line
2.55.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 20 lines

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 8 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
Reviewed-by on v3 1/1
#2 lore raw
In reply to Stephen Smalley
Quoted 20 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 8 lines

Looks good to me. Feel free to add:

Honza

Quoted 54 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
security/selinux/selinuxfs.c +14 -5
index 292302eb60f3..0941ce79ea0b 100644
@@ -247,11 +247,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),
@@ -1818,6 +1813,17 @@ static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino)
 
 #define NULL_FILE_NAME "null"
 
+static void sel_mark_immutable(struct dentry *root, const char *name)
+{
+	struct qstr q = QSTR(name);
+	struct dentry *dentry = try_lookup_noperm(&q, root);
+
+	if (!IS_ERR_OR_NULL(dentry)) {
+		d_inode(dentry)->i_flags |= S_IMMUTABLE;
+		dput(dentry);
+	}
+}
+
 static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 {
 	struct selinux_fs_info *fsi;
@@ -1857,6 +1863,9 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
+	sel_mark_immutable(sb->s_root, "status");
+	sel_mark_immutable(sb->s_root, "policy");
+
 	fsi = sb->s_fs_info;
 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
 	if (IS_ERR(fsi->bool_dir)) {
Signature · 1 line
2.55.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
Acked-by on v3 1/1
#3 lore raw
In reply to Stephen Smalley
Quoted 9 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear

Micro nit: clear -> clears.

Quoted 11 lines

VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 8 lines

With nits addressed, LGTM so:

Quoted 27 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
security/selinux/selinuxfs.c +0 -5
index 292302eb60f3..0941ce79ea0b 100644
@@ -247,11 +247,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),
@@ -1818,6 +1813,17 @@ static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino)
 
 #define NULL_FILE_NAME "null"
 

security/selinux/selinuxfs.c static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino) · line 1825

Quoted 10 lines
security/selinux/selinuxfs.c +10 -0
+static void sel_mark_immutable(struct dentry *root, const char *name)
+{
+	struct qstr q = QSTR(name);
+	struct dentry *dentry = try_lookup_noperm(&q, root);
+
+	if (!IS_ERR_OR_NULL(dentry)) {
+		d_inode(dentry)->i_flags |= S_IMMUTABLE;
+		dput(dentry);
+	}
+}

Hmm, maybe worth returning an error here on lookup failure? I think probably it can't happen in reality right now but that'd make it consistent with the other functions here.

Quoted 15 lines
security/selinux/selinuxfs.c +4 -1
+
 static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 {
 	struct selinux_fs_info *fsi;
@@ -1857,6 +1863,9 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 	if (ret)
 		goto err;
 
+	sel_mark_immutable(sb->s_root, "status");
+	sel_mark_immutable(sb->s_root, "policy");
+
 	fsi = sb->s_fs_info;
 	fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
 	if (IS_ERR(fsi->bool_dir)) {
--
Quoted 2 lines

2.55.0

--
Cheers, Lorenzo

author maintainer
#4 lore raw
Quoted 7 lines · Lorenzo Stoakes (ARM)
Quoted 5 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Quoted 6 lines
Quoted 4 lines

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear

Micro nit: clear -> clears.

Thanks, will fix if Paul requests a re-spin.

Quoted 52 lines
Quoted 12 lines

VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 8 lines

With nits addressed, LGTM so:

Quoted 27 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
security/selinux/selinuxfs.c +0 -5
index 292302eb60f3..0941ce79ea0b 100644
@@ -247,11 +247,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),
@@ -1818,6 +1813,17 @@ static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino)
 
 #define NULL_FILE_NAME "null"
 
Quoted 14 lines

security/selinux/selinuxfs.c static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino) · line 1825

Quoted 10 lines
security/selinux/selinuxfs.c +10 -0
+static void sel_mark_immutable(struct dentry *root, const char *name)
+{
+     struct qstr q = QSTR(name);
+     struct dentry *dentry = try_lookup_noperm(&q, root);
+
+     if (!IS_ERR_OR_NULL(dentry)) {
+             d_inode(dentry)->i_flags |= S_IMMUTABLE;
+             dput(dentry);
+     }
+}

Hmm, maybe worth returning an error here on lookup failure? I think probably it can't happen in reality right now but that'd make it consistent with the other functions here.

I don't think so - 1. It can't fail, and 2. if by some happenstance it did fail, we would
NOT want to stop populating selinuxfs and return the error to the caller because that
would turn something that is non-fatal into a fatal policy load/reload error and halt
the system if this is the initial policy load and SELinux is configured to be enforcing.
At most maybe a pr_err() or similar here, defer to Paul on whether that warrants a re-spin. Thanks.

Quoted 21 lines
Quoted 16 lines
security/selinux/selinuxfs.c +4 -1
+
 static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 {
      struct selinux_fs_info *fsi;
@@ -1857,6 +1863,9 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
      if (ret)
              goto err;
 
+     sel_mark_immutable(sb->s_root, "status");
+     sel_mark_immutable(sb->s_root, "policy");
+
      fsi = sb->s_fs_info;
      fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
      if (IS_ERR(fsi->bool_dir)) {
--
Quoted 2 lines

2.55.0

--
Cheers, Lorenzo

#5 lore raw
Quoted 8 lines · Stephen Smalley
Quoted 7 lines · Lorenzo Stoakes (ARM)
Quoted 5 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Quoted 31 lines
Quoted 6 lines
Quoted 4 lines

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear

Micro nit: clear -> clears.

Thanks, will fix if Paul requests a re-spin.

Quoted 22 lines
Quoted 12 lines

VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 8 lines

With nits addressed, LGTM so:

You can keep my tag without these 2 nits addressed actually, they are so minor as to not matter really!

Quoted 45 lines
Quoted 30 lines
Quoted 27 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
security/selinux/selinuxfs.c +0 -5
index 292302eb60f3..0941ce79ea0b 100644
@@ -247,11 +247,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),
@@ -1818,6 +1813,17 @@ static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino)
 
 #define NULL_FILE_NAME "null"
 
Quoted 14 lines

security/selinux/selinuxfs.c static struct dentry *sel_make_swapover_dir(struct super_block *sb, u64 *ino) · line 1825

Quoted 10 lines
security/selinux/selinuxfs.c +10 -0
+static void sel_mark_immutable(struct dentry *root, const char *name)
+{
+     struct qstr q = QSTR(name);
+     struct dentry *dentry = try_lookup_noperm(&q, root);
+
+     if (!IS_ERR_OR_NULL(dentry)) {
+             d_inode(dentry)->i_flags |= S_IMMUTABLE;
+             dput(dentry);
+     }
+}

Hmm, maybe worth returning an error here on lookup failure? I think probably it can't happen in reality right now but that'd make it consistent with the other functions here.

Quoted 10 lines

I don't think so - 1. It can't fail, and 2. if by some happenstance it did fail, we would
NOT want to stop populating selinuxfs and return the error to the caller because that
would turn something that is non-fatal into a fatal policy load/reload error and halt
the system if this is the initial policy load and SELinux is configured to be enforcing.
At most maybe a pr_err() or similar here, defer to Paul on whether that warrants a re-spin. Thanks.

Yup not a big deal!

As above, you can keep the tag regardless of these nits being addressed :)

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
security/selinux/selinuxfs.c +1 -0
+
 static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
 {
      struct selinux_fs_info *fsi;
@@ -1857,6 +1863,9 @@ static int sel_fill_super(struct super_block *sb, struct fs_context *fc)
      if (ret)
              goto err;
 
Quoted 12 lines
Quoted 10 lines
Quoted 9 lines
security/selinux/selinuxfs.c +3 -1
+     sel_mark_immutable(sb->s_root, "status");
+     sel_mark_immutable(sb->s_root, "policy");
+
      fsi = sb->s_fs_info;
      fsi->bool_dir = sel_make_dir(sb->s_root, BOOL_DIR_NAME, &fsi->last_ino);
      if (IS_ERR(fsi->bool_dir)) {
--

2.55.0

Quoted 2 lines

--
Cheers, Lorenzo

--
Cheers, Lorenzo

maintainer
#6 lore raw
In reply to Stephen Smalley
Quoted 29 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 6 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

I'm mildly concerned about marking these files as immutable when neither of them is actually immutable. Yes, userspace shouldn't be able to write to either, but the contents of these files *do* change. While it doesn't look S_IMMUTABLE should currently pose any real problems, I do worry about the future and an innocent looking VFS change causing problems with one of these files because it was assumed the contents of a S_IMMUTABLE file wouldn't change.

Unless someone can promise me that would never happen, I think I would prefer the !FMODE_WRITE approach taken in v2.

Signature · 1 line
paul-moore.com
#7 lore raw
Quoted 38 lines · Paul Moore
Quoted 29 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 6 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
Quoted 7 lines

I'm mildly concerned about marking these files as immutable when neither of them is actually immutable. Yes, userspace shouldn't be able to write to either, but the contents of these files *do* change. While it doesn't look S_IMMUTABLE should currently pose any real problems, I do worry about the future and an innocent looking VFS change causing problems with one of these files because it was assumed the contents of a S_IMMUTABLE file wouldn't change.

S_IMMUTABLE bit is about permission checks. I have hard time imagining some VFS code would do some assumptions regarding data based on that. But maybe my imagination is just lacking :).

Quoted 2 lines

Unless someone can promise me that would never happen, I think I would prefer the !FMODE_WRITE approach taken in v2.

If you feel better that way, then sure, go for it. It works as well but you have to separately handle truncate(2). Overall I wouldn't consider this less fragile than using S_IMMUTABLE but it's reasonably robust as well.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
#8 lore raw
Quoted 47 lines · Jan Kara
Quoted 38 lines · Paul Moore
Quoted 29 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 6 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
Quoted 7 lines

I'm mildly concerned about marking these files as immutable when neither of them is actually immutable. Yes, userspace shouldn't be able to write to either, but the contents of these files *do* change. While it doesn't look S_IMMUTABLE should currently pose any real problems, I do worry about the future and an innocent looking VFS change causing problems with one of these files because it was assumed the contents of a S_IMMUTABLE file wouldn't change.

Quoted 3 lines

S_IMMUTABLE bit is about permission checks. I have hard time imagining some VFS code would do some assumptions regarding data based on that. But maybe my imagination is just lacking :).

Said code would be rather buggy...

maintainer
#9 lore raw
In reply to Jan Kara
Quoted 51 lines · Jan Kara
Quoted 38 lines · Paul Moore
Quoted 29 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 6 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
Quoted 7 lines

I'm mildly concerned about marking these files as immutable when neither of them is actually immutable. Yes, userspace shouldn't be able to write to either, but the contents of these files *do* change. While it doesn't look S_IMMUTABLE should currently pose any real problems, I do worry about the future and an innocent looking VFS change causing problems with one of these files because it was assumed the contents of a S_IMMUTABLE file wouldn't change.

S_IMMUTABLE bit is about permission checks. I have hard time imagining some VFS code would do some assumptions regarding data based on that. But maybe my imagination is just lacking :).

Quoted 6 lines
Quoted 2 lines

Unless someone can promise me that would never happen, I think I would prefer the !FMODE_WRITE approach taken in v2.

If you feel better that way, then sure, go for it. It works as well but you have to separately handle truncate(2). Overall I wouldn't consider this less fragile than using S_IMMUTABLE but it's reasonably robust as well.

Okay, since my concerns are only a gut feeling and the S_IMMUTABLE patch does take care of a few issues at once, I'm going to go ahead and merge this into selinux/dev; I just hope I don't end up regretting this in a few years ;)

Thanks everyone!

Signature · 1 line
paul-moore.com
author maintainer
#10 lore raw
In reply to Paul Moore
Quoted 47 lines · Paul Moore
Quoted 29 lines · Stephen Smalley

The selinuxfs "status" and "policy" files are read-only interfaces that are also mmap'd by userspace. They are created 0444 by simple_fill_super() but a CAP_DAC_OVERRIDE caller can still open them O_WRONLY/O_RDWR, open(O_RDONLY|O_TRUNC) them, or truncate(2) them.

Mark both inodes S_IMMUTABLE at fill_super time. inode_permission() tests IS_IMMUTABLE before the DAC / capability checks, so all of the above are rejected at the VFS layer without ever reaching the file operations. Since a writable file can no longer exist, do_mmap() clear VM_MAYWRITE for MAP_SHARED mappings on its own, and the sel_mmap_handle_status() write/mprotect guards are dead; drop them. MAP_PRIVATE writable mappings become permitted (they were previously -EPERM) and CoW harmlessly to a private page, matching how sel_mmap_policy() has always treated the private case.

The sel_mmap_policy() VM_SHARED guard becomes redundant for the same reason; leave dropping it to the pending "selinux: reject writable opens of policy file, drop mmap shared/write check" patch so that the patches do not conflict.

Quoted 6 lines

---
v3 implements Jan's suggestion to mark the inodes immutable rather than implementing open-time checks.

 security/selinux/selinuxfs.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

I'm mildly concerned about marking these files as immutable when neither of them is actually immutable. Yes, userspace shouldn't be able to write to either, but the contents of these files *do* change. While it doesn't look S_IMMUTABLE should currently pose any real problems, I do worry about the future and an innocent looking VFS change causing problems with one of these files because it was assumed the contents of a S_IMMUTABLE file wouldn't change.

Quoted 2 lines

Unless someone can promise me that would never happen, I think I would prefer the !FMODE_WRITE approach taken in v2.

In that case, we would still need to separately address truncate(2) in some manner, which requires something like cgzones' earlier patch. Up to you but the immutable flag seems cleaner to me and I don't think the concern above is likely to be a real issue.