* [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* [PATCH 2/2] selinux: check FILE__MAP when rechecking remapped backing files
2026-12-04 20:43 [PATCH 1/2] selinux: recheck backing files on remap_file_pages() Stephen Smalley
@ 2026-12-04 20:43 ` 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
1 sibling, 1 reply; 4+ messages in thread
From: Stephen Smalley @ 2026-12-04 20:43 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
---
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..59345f5a41f8 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] 4+ messages in thread* Re: [PATCH 2/2] selinux: check FILE__MAP when rechecking remapped backing files
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
0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-04 20:57 UTC (permalink / raw)
To: selinux
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] selinux: missing parentheses on if map in
selinux_backing_file_recheck()
- [Low] selinux: duplicate FD__USE checks in
selinux_backing_file_recheck()
--
commit d93cc885d4fd9bb55b9bed3112dd35ed2093c015
Author: Stephen Smalley <stephen.smalley.work@gmail.com>
selinux: check FILE__MAP when rechecking remapped backing files
This commit adds a map argument to selinux_backing_file_recheck() so that
remap_file_pages() checks FILE__MAP on the user-visible file, saved
mounter layers, and the lowest backing file, while mprotect() keeps its
existing checks.
Link: https://lore.kernel.org/lkml/CAEjxPJ5tOwBfXpqbb_pFkjtHGtrHYL_idAj=es9QDGE1qPLDBA@mail.gmail.com/#r
> diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
> index c19ad72727062..f68a4a345345a 100644
> --- a/security/selinux/hooks.c
> +++ b/security/selinux/hooks.c
> @@ -4149,22 +4149,43 @@ static inline int file_map_prot_check(const struct file *file,
[ ... ]
> 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;
[Severity: High]
Are parentheses missing around map here? Without them, "if map" fails to
compile when CONFIG_SECURITY_SELINUX is enabled.
> + 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);
> }
[Severity: Low]
Does calling __file_has_perm() before __file_map_prot_check() on the
user-visible file, and file_has_perm() before file_map_prot_check() on
the lowest backing file, evaluate FD__USE twice on each file?
Both __file_has_perm() and __file_map_prot_check() check FD__USE via
avc_has_perm() (and call bpf_fd_pass()). During remap_file_pages(),
when the subject SID differs from the fd SID (such as after a domain
transition), this emits duplicate granted audit records under auditallow.
It also checks FD__USE before FILE__MAP, reversing the ordering used by
selinux_mmap_file_common(). Checking FILE__MAP with inode_has_perm() to
match selinux_mmap_file_common(), or folding FILE__MAP into a single
__file_has_perm() call, would avoid the duplicate checks.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204204354.18371-2-stephen.smalley.work@gmail.com?part=2
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] selinux: recheck backing files on remap_file_pages()
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 21:00 ` sashiko-bot
1 sibling, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-12-04 21:00 UTC (permalink / raw)
To: selinux
Sashiko has finished reviewing this 2-patch series:
- Patch 1: no issues found.
- Patch 2 ("selinux: check FILE__MAP when rechecking remapped ba..."): 2
potential issues (1 High, 1 Low)
A detailed review has been sent as a separate reply to patch 2.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204204354.18371-2-stephen.smalley.work@gmail.com
^ permalink raw reply [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.