* [PATCH v2 1/2] selinux: recheck backing files on remap_file_pages()
@ 2026-12-04 21:11 Stephen Smalley
2026-12-04 21:11 ` [PATCH v2 2/2] selinux: check FILE__MAP when rechecking remapped backing files Stephen Smalley
2026-12-04 21:31 ` [PATCH v2 1/2] selinux: recheck backing files on remap_file_pages() sashiko-bot
0 siblings, 2 replies; 3+ messages in thread
From: Stephen Smalley @ 2026-12-04 21:11 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
---
v2 fixes missing parentheses in the second patch of the series that
somehow didn't get picked up (note to self: always check for
uncommitted changes before sending!). I did not make any changes for
the low severity comment from sashiko because FD__USE is only checked
when the subject and open file SIDs differ, which is not the common
case, and the order in which the checks occur should not matter. Open
to further optimization if someone finds it matters, but I don't think
it will.
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] 3+ messages in thread
* [PATCH v2 2/2] selinux: check FILE__MAP when rechecking remapped backing files
2026-12-04 21:11 [PATCH v2 1/2] selinux: recheck backing files on remap_file_pages() Stephen Smalley
@ 2026-12-04 21:11 ` Stephen Smalley
2026-12-04 21:31 ` [PATCH v2 1/2] selinux: recheck backing files on remap_file_pages() sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: Stephen Smalley @ 2026-12-04 21:11 UTC (permalink / raw)
To: selinux
Cc: paul, omosnacek, Stephen Smalley, miklos, akpm, 18670333770,
guoziyang25
remap_file_pages() establishes a new mapping at a caller-chosen page
offset, so it must pass the FILE__MAP check that mmap() performs.
Give selinux_backing_file_recheck() a bool map argument:
remap_file_pages() requires map on the user-visible file with the
caller's credentials, and on every saved layer and the lowest backing
file with the respective mounter's credentials. mprotect() keeps its
existing checks, where map is not required on any layer.
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: Stephen Smalley <stephen.smalley.work@gmail.com>
cc: miklos@szeredi.hu
cc: akpm@linux-foundation.org
cc: 18670333770@163.com
cc: guoziyang25@nudt.edu.cn
---
v2 fixes missing parentheses in the second patch of the series that
somehow didn't get picked up (note to self: always check for
uncommitted changes before sending!). I did not make any changes for
the low severity comment from sashiko because FD__USE is only checked
when the subject and open file SIDs differ, which is not the common
case, and the order in which the checks occur should not matter. Open
to further optimization if someone finds it matters, but I don't think
it will.
security/selinux/hooks.c | 33 +++++++++++++++++++++++++++------
1 file changed, 27 insertions(+), 6 deletions(-)
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 4b494802b964..6447f99e56a8 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -4147,22 +4147,43 @@ static inline int file_map_prot_check(const struct file *file,
* 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.
+ * the user file with the caller's credentials, then every saved mounter
+ * layer and the backing file itself with the mounter's credentials.
+ *
+ * remap_file_pages() establishes a new mapping, so it must also pass the
+ * FILE__MAP checks that mmap() performs, on the same subjects and objects;
+ * mprotect() only changes an existing mapping and must not require them.
*/
static int selinux_backing_file_recheck(const struct file *file,
- unsigned long prot, bool shared)
+ unsigned long prot, bool shared,
+ bool map)
{
+ u32 av = file_map_prot_to_av(prot, shared);
int rc;
+ /* the user-visible file with the caller's credentials */
+ if (map) {
+ rc = __file_has_perm(current_cred(), file, FILE__MAP, true);
+ if (rc)
+ return 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));
+ /* every saved intermediate layer with its mounter's credentials */
+ if (map)
+ av |= FILE__MAP;
+ rc = backing_mounters_has_perm(file, av);
if (rc)
return rc;
+ /* the backing file itself with its mounter's credentials */
+ if (map) {
+ rc = file_has_perm(file->f_cred, file, FILE__MAP);
+ if (rc)
+ return rc;
+ }
return file_map_prot_check(file, prot, shared, true);
}
@@ -4215,7 +4236,7 @@ static int selinux_mmap_file(struct file *file,
* 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_backing_file_recheck(file, prot, shared, true);
return selinux_mmap_file_common(file, prot, shared, false);
}
@@ -4317,7 +4338,7 @@ static int selinux_file_mprotect(struct vm_area_struct *vma,
}
if (backing_file)
- rc = selinux_backing_file_recheck(file, prot, shared);
+ rc = selinux_backing_file_recheck(file, prot, shared, false);
else
rc = file_map_prot_check(file, prot, shared, false);
if (rc)
--
2.56.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2 1/2] selinux: recheck backing files on remap_file_pages()
2026-12-04 21:11 [PATCH v2 1/2] selinux: recheck backing files on remap_file_pages() Stephen Smalley
2026-12-04 21:11 ` [PATCH v2 2/2] selinux: check FILE__MAP when rechecking remapped backing files Stephen Smalley
@ 2026-12-04 21:31 ` sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-12-04 21:31 UTC (permalink / raw)
To: selinux
Sashiko has reviewed this 2-patch series and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204211116.32439-2-stephen.smalley.work@gmail.com
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-04 21:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 21:11 [PATCH v2 1/2] selinux: recheck backing files on remap_file_pages() Stephen Smalley
2026-12-04 21:11 ` [PATCH v2 2/2] selinux: check FILE__MAP when rechecking remapped backing files Stephen Smalley
2026-12-04 21:31 ` [PATCH v2 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.