linux/security/selinux/include
Karl Mehltretter 78fc54b934 selinux: recheck intermediate backing files on mprotect()
mprotect() can be used to bypass the SELinux checks that mmap() performs
against the intermediate layers of a stacked filesystem.

mmap() checks every backing layer as the request descends through the
stack.  mprotect() only has the lowest backing file in vma->vm_file, so it
rechecks the top-level user and the lowest mounter, but skips the mounters
of every layer in between.  With two nested overlayfs mounts and a policy
denying mounter_t -> middle_file_t:file { execute }, a direct
mmap(PROT_EXEC) is denied:

  avc:  denied  { execute } for  pid=71 comm="nested_exec"
    path="/payload" dev="overlay" ino=9
    scontext=user_u:base_r:mounter_t
    tcontext=user_u:object_r:middle_file_t tclass=file permissive=0

while mmap(PROT_NONE) followed by mprotect(PROT_EXEC) succeeds.

Preserve each intermediate path, mounter SID and file-description SID in
the backing-file security blob, copying the saved entries when another
backing layer is opened.  Allocate the array only for nested backing files,
and release it and the path references in the backing_file_free hook.

During mprotect(), recheck fd { use } and the requested inode permissions
for every saved mounter, and include the intermediate layers in the execmod
checks.  Policy for nested stacking may then need to grant intermediate
mounters what a direct mmap() already requires, and execmod on intermediate
labels for binaries using text relocations.

Tested on arm64 QEMU with a small BusyBox initramfs and a purpose-built
SELinux policy, on a mainline tree containing
commit f2381b546e ("fs: fix user path of nested backing files").

Cc: stable@vger.kernel.org
Fixes: 82544d36b1 ("selinux: fix overlayfs mmap() and mprotect() access checks")
Assisted-by: LLM
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
Reviewed-by: Stephen Smalley <stephen.smalley.work@gmail.com>
[PM: subject tweak]
Signed-off-by: Paul Moore <paul@paul-moore.com>
2026-09-15 23:30:07 -04:00
..
audit.h selinux: move initcalls to the LSM framework 2025-10-22 19:24:28 -04:00
avc_ss.h selinux: fix style issues in security/selinux/include/avc_ss.h 2023-12-22 18:09:26 -05:00
avc.h selinux: introduce neveraudit types 2025-06-19 17:23:04 -04:00
classmap.h selinux: add support for BPF token access control 2026-01-13 15:42:37 -05:00
conditional.h selinux: constify and reconcile function parameter names 2025-01-07 23:14:38 -05:00
hash.h selinux: improve bucket distribution uniformity of avc_hash() 2025-10-23 18:24:30 -04:00
ibpkey.h selinux: make header files self-including 2023-05-18 14:12:43 -04:00
ima.h selinux: compute the IMA configuration settings string length once at boot 2026-07-15 21:42:01 -04:00
initcalls.h selinux: move initcalls to the LSM framework 2025-10-22 19:24:28 -04:00
initial_sid_to_string.h selinux: do not include <linux/*.h> headers from host programs 2024-10-03 15:34:24 -04:00
netif.h selinux: fix style issues in security/selinux/include/netif.h 2023-12-22 18:09:28 -05:00
netlabel.h selinux: fix style issues with security/selinux/include/netlabel.h 2023-12-22 18:09:28 -05:00
netnode.h selinux: constify network address pointer 2025-04-11 16:29:50 -04:00
netport.h selinux: include necessary headers in headers 2022-05-03 14:11:13 -04:00
objsec.h selinux: recheck intermediate backing files on mprotect() 2026-09-15 23:30:07 -04:00
policycap_names.h selinux: add support for BPF token access control 2026-01-13 15:42:37 -05:00
policycap.h selinux: add support for BPF token access control 2026-01-13 15:42:37 -05:00
security.h selinux/stable-7.2 PR 20260615 2026-06-17 12:41:00 +01:00
xfrm.h selinux: fix style issues in security/selinux/include/xfrm.h 2023-12-22 18:09:30 -05:00