All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] selinux: recheck backing files on remap_file_pages()
@ 2026-12-04 20:43 Stephen Smalley
  2026-12-04 20:43 ` [PATCH 2/2] selinux: check FILE__MAP when rechecking remapped backing files Stephen Smalley
  2026-12-04 21:00 ` [PATCH 1/2] selinux: recheck backing files on remap_file_pages() sashiko-bot
  0 siblings, 2 replies; 4+ messages in thread
From: Stephen Smalley @ 2026-12-04 20:43 UTC (permalink / raw)
  To: selinux; +Cc: paul, omosnacek, Ziyang Guo, miklos, akpm, 18670333770

From: Ziyang Guo <guoziyang25@nudt.edu.cn>

remap_file_pages() re-runs the mmap permission check on the file that is
already installed in the VMA.  On a stacked filesystem that file is the
backing file, but the check is still the plain mmap_file hook: it checks
the backing inode with the caller's credentials instead of the mounter's,
skips the intermediate backing layers, and can deny a valid remap with a
spurious fd { use } check between the caller and the mounter domain.

Reuse the backing-file recheck that mprotect() performs: check the user
file with the caller's credentials, then every saved mounter and the
lowest backing file with the mounter's credentials.  The helper is shared
with selinux_file_mprotect() so the two paths cannot drift apart again.

With an overlayfs mount and a policy that grants the user access to the
top-level file but no fd { use } to the mounter domain,
remap_file_pages() fails with EACCES and logs the following denial, while
it succeeds after this change:

  avc:  denied  { use } for pid=123 comm="remap"
    scontext=...user_t tcontext=...mounter_t tclass=fd

Policies that deny the user file or a backing layer still deny the remap.

Link: https://lore.kernel.org/lkml/CAEjxPJ5tOwBfXpqbb_pFkjtHGtrHYL_idAj=es9QDGE1qPLDBA@mail.gmail.com/#r
Fixes: 82544d36b172 ("selinux: fix overlayfs mmap() and mprotect() access checks")
Signed-off-by: Ziyang Guo <guoziyang25@nudt.edu.cn>

cc: miklos@szeredi.hu
cc: akpm@linux-foundation.org
cc: 18670333770@163.com
cc: guoziyang25@nudt.edu.cn
---
This is on v7.4-rc5 in case you want it for stable; if you'd rather it
be re-based on selinux/dev or next, let me know and I'll re-spin. I
did not yet Ack this first patch because it needs the next one to be
fully correct; if you want them squashed, let me know and I'll re-send
a single patch with Ziyang as the author and me as a secondary
Signed-off-by.

 security/selinux/hooks.c | 56 +++++++++++++++++++++++++++++-----------
 1 file changed, 41 insertions(+), 15 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 6c4c64d6e904..4b494802b964 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -4143,6 +4143,29 @@ static inline int file_map_prot_check(const struct file *file,
 	return __file_map_prot_check(file, prot, shared, mounter_check, false);
 }
 
+/*
+ * Recheck a backing file that is already installed in a VMA.  Operations
+ * which do not go through backing_file_mmap() (mprotect() and
+ * remap_file_pages()) only have the lowest backing file at hand, so check
+ * the user file with the caller's credentials and every saved layer, then
+ * the backing file itself, with the mounter's credentials.
+ */
+static int selinux_backing_file_recheck(const struct file *file,
+					unsigned long prot, bool shared)
+{
+	int rc;
+
+	rc = __file_map_prot_check(file, prot, shared, false, true);
+	if (rc)
+		return rc;
+
+	rc = backing_mounters_has_perm(file, file_map_prot_to_av(prot, shared));
+	if (rc)
+		return rc;
+
+	return file_map_prot_check(file, prot, shared, true);
+}
+
 static int selinux_mmap_addr(unsigned long addr)
 {
 	int rc = 0;
@@ -4180,11 +4203,21 @@ static int selinux_mmap_file(struct file *file,
 			     unsigned long prot, unsigned long flags)
 {
 	const unsigned long map_type = (flags & MAP_TYPE);
+	bool shared = (map_type == MAP_SHARED ||
+		       map_type == MAP_SHARED_VALIDATE);
+
+	/*
+	 * remap_file_pages() passes the backing file that is installed in the
+	 * VMA.  The regular mmap() path checks the user file and then calls
+	 * selinux_mmap_backing_file() from backing_file_mmap(); re-run the
+	 * backing checks here as well, otherwise the backing inode is checked
+	 * with the caller's credentials, the intermediate layers are skipped
+	 * and a valid mapping can be denied by a spurious fd { use } check.
+	 */
+	if (file && (file->f_mode & FMODE_BACKING))
+		return selinux_backing_file_recheck(file, prot, shared);
 
-	return selinux_mmap_file_common(file, prot,
-					(map_type == MAP_SHARED ||
-					 map_type == MAP_SHARED_VALIDATE),
-					false);
+	return selinux_mmap_file_common(file, prot, shared, false);
 }
 
 /**
@@ -4228,7 +4261,6 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,
 	int rc;
 	const struct cred *cred = current_cred();
 	u32 sid = cred_sid(cred);
-	u32 av;
 	const struct file *file = vma->vm_file;
 	bool backing_file;
 	bool shared = vma->vm_flags & VM_SHARED;
@@ -4284,18 +4316,12 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,
 		}
 	}
 
-	rc = __file_map_prot_check(file, prot, shared, false, backing_file);
+	if (backing_file)
+		rc = selinux_backing_file_recheck(file, prot, shared);
+	else
+		rc = file_map_prot_check(file, prot, shared, false);
 	if (rc)
 		return rc;
-	if (backing_file) {
-		av = file_map_prot_to_av(prot, shared);
-		rc = backing_mounters_has_perm(file, av);
-		if (rc)
-			return rc;
-		rc = file_map_prot_check(file, prot, shared, true);
-		if (rc)
-			return rc;
-	}
 
 	return 0;
 }
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-04 21:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 20:43 [PATCH 1/2] selinux: recheck backing files on remap_file_pages() Stephen Smalley
2026-12-04 20:43 ` [PATCH 2/2] selinux: check FILE__MAP when rechecking remapped backing files Stephen Smalley
2026-12-04 20:57   ` sashiko-bot
2026-12-04 21:00 ` [PATCH 1/2] selinux: recheck backing files on remap_file_pages() sashiko-bot

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.