Can anyone explain in details what is going on with the following. Let's imagine I am mounting a directory with noexec
option as follows:
mount -o noexec /dev/mapper/fedora-data /data
So to verify this I ran mount | grep data
:
/dev/mapper/fedora-data on /data type ext4 (rw,noexec,relatime,seclabel,data=ordered)
Now within /data
I'm creating a simple script called hello_world
as follows:
#!/bin/bash
echo "Hello World"
whoami
So I made the script executable by chmod u+x hello_world
(this will however have no effect on a file system with noexec
options) and I tried running it:
# ./hello_world
-bash: ./hello_world: Permission denied
However, prepanding bash
to the file yields to:
# bash hello_world
Hello World
root
So then I created a simple hello_world.c
with the following contents:
#include <stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
Compiled it using cc -o hello_world hello_world.c
Now running:
# ./hello_world
-bash: ./hello_world: Permission denied
So I tried to run it using
/lib64/ld-linux-x86-64.so.2 hello_world
The error:
./hello_world: error while loading shared libraries: ./hello_world: failed to map segment from shared object: Operation not permitted
So this is of course true since ldd
returns the following:
ldd hello_world
ldd: warning: you do not have execution permission for `./hello_world'
not a dynamic executable
On another system where noexec
mount option doesn't apply I see:
ldd hello_world
linux-vdso.so.1 (0x00007ffc1c127000)
libc.so.6 => /lib64/libc.so.6 (0x00007facd9d5a000)
/lib64/ld-linux-x86-64.so.2 (0x00007facd9f3e000)
Now my question is this: Why does running a bash script on a file system with noexec
option work but not a c
compiled program? What is happening under the hood?
chmod o+x «filename»
will not give execute permission to file owning user, or group. Usechmod ugo+x «filename»
orchmod +x «filename»
chmod u+x hello_world
but forgot to change that in the question. Thanks for pointing that out.