Search code examples
linuxkernelmmapmmu

mprotect PROT_EXEC on noexec partition


I tried mmap-ing (more specifically dlopen fails on mmap) a file from disk partition that was mounted with the noexec flag and it resulted in

failed to map segment from shared object

How does the kernel know once the file is mapped to memory if it's a noexec partition. I couldn't find the code in the kernel source that checks this condition.

I'm using:Linux version 5.7.11-1-default (geeko@buildhost) (gcc version 10.1.1 20200625 [revision c91e43e9363bd119a695d64505f96539fa451bf2] (SUSE Linux), GNU ld (GNU Binutils; openSUSE Tumbleweed) 2.34.0.20200325-1) #1 SMP Wed Jul 29 09:32:21 UTC 2020 (5015994)


Solution

  • A mmap() request can lead to the routine do_mmap() which conditionally calls path_noexec(); PROT_EXEC may be enabled or skipped based on a mount flag MNT_NOEXEC