Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When setting execution bit on PT_GNU_STACK program header, why do all segments of the process become executable

Tags:

linux

glibc

ld

elf

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?

like image 543
Neowizard Avatar asked May 20 '20 09:05

Neowizard


1 Answers

This is simply something that the Linux kernel (not userspace) does on some architectures. From fs/binfmt_elf.c in the kernel sources (executable_stack is set based on PT_GNU_STACK protection flags):

    if (elf_read_implies_exec(*elf_ex, executable_stack))
        current->personality |= READ_IMPLIES_EXEC;

And on x86, we have this (from arch/x86/include/asm/elf.h):

/*
 * An executable for which elf_read_implies_exec() returns TRUE will
 * have the READ_IMPLIES_EXEC personality flag set automatically.
 */
#define elf_read_implies_exec(ex, executable_stack) \
  (executable_stack != EXSTACK_DISABLE_X)

So PT_GNU_STACK does not control the protection flags of the stack directly, but the READ_IMPLIES_EXEC personally.

I assume the kernel does this because many architectures (amd64, i386, s390x) were read-implies-exec initially, so it is conceivable that people have old binaries which rely on this behavior. With a way to flag such old binaries, people can still run their systems with increased memory protection enforcement, only opting out a subset of old binaries as required.

You should see just an executable stack if you run the binary with an explicit loader invocation, e.g. with a suitably patched cat program:

$ /lib64/ld-linux-x86-64.so.2 ./cat /proc/self/maps
555555cc7000-555555ce8000 rw-p 00000000 00:00 0                          [heap]
7fbdc90cb000-7fbdc90ed000 rw-p 00000000 00:00 0 
7fbdc90ed000-7fbdd60d2000 r--p 00000000 fd:02 1688290164                 /usr/lib/locale/locale-archive
7fbdd60d2000-7fbdd60f7000 r--p 00000000 fd:02 690045363                  /usr/lib64/libc-2.30.so
7fbdd60f7000-7fbdd6246000 r-xp 00025000 fd:02 690045363                  /usr/lib64/libc-2.30.so
7fbdd6246000-7fbdd6290000 r--p 00174000 fd:02 690045363                  /usr/lib64/libc-2.30.so
7fbdd6290000-7fbdd6291000 ---p 001be000 fd:02 690045363                  /usr/lib64/libc-2.30.so
7fbdd6291000-7fbdd6294000 r--p 001be000 fd:02 690045363                  /usr/lib64/libc-2.30.so
7fbdd6294000-7fbdd6297000 rw-p 001c1000 fd:02 690045363                  /usr/lib64/libc-2.30.so
7fbdd6297000-7fbdd629d000 rw-p 00000000 00:00 0 
7fbdd62bb000-7fbdd62bd000 r--p 00000000 00:24 3253068                    /tmp/cat
7fbdd62bd000-7fbdd62c2000 r-xp 00002000 00:24 3253068                    /tmp/cat
7fbdd62c2000-7fbdd62c5000 r--p 00007000 00:24 3253068                    /tmp/cat
7fbdd62c5000-7fbdd62c6000 r--p 00009000 00:24 3253068                    /tmp/cat
7fbdd62c6000-7fbdd62c7000 rw-p 0000a000 00:24 3253068                    /tmp/cat
7fbdd62c7000-7fbdd62c9000 r--p 00000000 fd:02 690045355                  /usr/lib64/ld-2.30.so
7fbdd62c9000-7fbdd62e9000 r-xp 00002000 fd:02 690045355                  /usr/lib64/ld-2.30.so
7fbdd62e9000-7fbdd62f1000 r--p 00022000 fd:02 690045355                  /usr/lib64/ld-2.30.so
7fbdd62f2000-7fbdd62f3000 r--p 0002a000 fd:02 690045355                  /usr/lib64/ld-2.30.so
7fbdd62f3000-7fbdd62f4000 rw-p 0002b000 fd:02 690045355                  /usr/lib64/ld-2.30.so
7fbdd62f4000-7fbdd62f5000 rw-p 00000000 00:00 0 
7ffdabe6a000-7ffdabe89000 rwxp 00000000 00:00 0                          [stack]
7ffdabe89000-7ffdabe8b000 rw-p 00000000 00:00 0 
7ffdabfed000-7ffdabff1000 r--p 00000000 00:00 0                          [vvar]
7ffdabff1000-7ffdabff3000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
like image 109
Florian Weimer Avatar answered Sep 25 '22 16:09

Florian Weimer