Playing around with controlling the executable-bit on segments, I've found a massive quirk in how PT_GNU_STACK
is used by the loader.
According to the elf(5)
manpage, PT_GNU_STACK
is used as a:
GNU extension which is used by the Linux kernel to control the state of the stack via the flags set in the p_flags member.
The execstack
manpage, also supports this:
... ELF binaries and shared libraries now can be marked as requiring executable stack or not requiring it. This marking is done through the p_flags field in the PT_GNU_STACK program header entry.
However, in addition to setting the stack executable, when I set that bit, nearly all segments turn executable.
For instance, when I run sleep
, I get this memory map
sleep 100 & cat /proc/$!/maps
[1] 1260
561460d8d000-561460d94000 r-xp 00000000 08:01 524383 /bin/sleep
561460f94000-561460f95000 r--p 00007000 08:01 524383 /bin/sleep
561460f95000-561460f96000 rw-p 00008000 08:01 524383 /bin/sleep
561462eca000-561462eeb000 rw-p 00000000 00:00 0 [heap]
7f02b08b9000-7f02b0b97000 r--p 00000000 08:01 1966102 /usr/lib/locale/locale-archive
7f02b0b97000-7f02b0d7e000 r-xp 00000000 08:01 655384 /lib/x86_64-linux-gnu/libc-2.27.so
7f02b0d7e000-7f02b0f7e000 ---p 001e7000 08:01 655384 /lib/x86_64-linux-gnu/libc-2.27.so
7f02b0f7e000-7f02b0f82000 r--p 001e7000 08:01 655384 /lib/x86_64-linux-gnu/libc-2.27.so
7f02b0f82000-7f02b0f84000 rw-p 001eb000 08:01 655384 /lib/x86_64-linux-gnu/libc-2.27.so
7f02b0f84000-7f02b0f88000 rw-p 00000000 00:00 0
7f02b0f88000-7f02b0faf000 r-xp 00000000 08:01 655375 /lib/x86_64-linux-gnu/ld-2.27.so
7f02b11a3000-7f02b11a5000 rw-p 00000000 00:00 0
7f02b11af000-7f02b11b0000 r--p 00027000 08:01 655375 /lib/x86_64-linux-gnu/ld-2.27.so
7f02b11b0000-7f02b11b1000 rw-p 00028000 08:01 655375 /lib/x86_64-linux-gnu/ld-2.27.so
7f02b11b1000-7f02b11b2000 rw-p 00000000 00:00 0
7ffc74d95000-7ffc74db6000 rw-p 00000000 00:00 0 [stack]
7ffc74dfa000-7ffc74dfd000 r--p 00000000 00:00 0 [vvar]
7ffc74dfd000-7ffc74dff000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
But if I first set PT_GNU_STACK
execution bit, I get
execstack -s `which sleep` ; sleep 100 & cat /proc/$!/maps
[1] 1282
55b27b14f000-55b27b156000 r-xp 00000000 08:01 537509 /bin/sleep
55b27b356000-55b27b357000 r-xp 00007000 08:01 537509 /bin/sleep
55b27b357000-55b27b358000 rwxp 00008000 08:01 537509 /bin/sleep
55b27bae6000-55b27bb07000 rwxp 00000000 00:00 0 [heap]
7f99b5359000-7f99b5637000 r-xp 00000000 08:01 1966102 /usr/lib/locale/locale-archive
7f99b5637000-7f99b581e000 r-xp 00000000 08:01 655384 /lib/x86_64-linux-gnu/libc-2.27.so
7f99b581e000-7f99b5a1e000 ---p 001e7000 08:01 655384 /lib/x86_64-linux-gnu/libc-2.27.so
7f99b5a1e000-7f99b5a22000 r-xp 001e7000 08:01 655384 /lib/x86_64-linux-gnu/libc-2.27.so
7f99b5a22000-7f99b5a24000 rwxp 001eb000 08:01 655384 /lib/x86_64-linux-gnu/libc-2.27.so
7f99b5a24000-7f99b5a28000 rwxp 00000000 00:00 0
7f99b5a28000-7f99b5a4f000 r-xp 00000000 08:01 655375 /lib/x86_64-linux-gnu/ld-2.27.so
7f99b5c43000-7f99b5c45000 rwxp 00000000 00:00 0
7f99b5c4f000-7f99b5c50000 r-xp 00027000 08:01 655375 /lib/x86_64-linux-gnu/ld-2.27.so
7f99b5c50000-7f99b5c51000 rwxp 00028000 08:01 655375 /lib/x86_64-linux-gnu/ld-2.27.so
7f99b5c51000-7f99b5c52000 rwxp 00000000 00:00 0
7fffc6a4d000-7fffc6a6e000 rwxp 00000000 00:00 0 [stack]
7fffc6b36000-7fffc6b39000 r--p 00000000 00:00 0 [vvar]
7fffc6b39000-7fffc6b3b000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Every segment except [vvar]
and one segment backed by libc
are now executable.
I've tried it my Arch setup, an Ubuntu Server VM (Bionic) and a Mint Desktop (Tessa) - all with the same results.
Why does the loader do this?