fs/binfmt_elf: use ELF_ET_DYN_BASE for PIE (ET_DYN with INTERP) binaries
Subsystems (2)
PATCH 1/1 fs/binfmt_elf: use ELF_ET_DYN_BASE for PIE (ET_DYN with INTERP) binaries
Since 9630f0d60fec ELF_ET_DYN_BASE is not used as a load_bias anymore and this breaks PIE binaries, since after this change data segment became too nearby the stack:
Before 9630f0d60fec:
$ strace -febrk /tmp/test-stack |& head
brk(NULL) = 0x555555559000
$ /tmp/test-stack
bottom_of_stack = 0x7fffffffc5c0
recursion depth: 1 (stack diff: 32)
...
recursion depth: 7690 (stack diff: 8365664)
Segmentation fault (core dumped)After 9630f0d60fec:
$ strace -ebrk /tmp/test-stack |& head
brk(NULL) = 0x7ffff7fff000 $ /tmp/test-stack
bottom_of_stack = 0x7fffffffc640
recursion depth: 1 (stack diff: 32)
...
recursion depth: 146 (stack diff: 157792)
Segmentation fault (core dumped)Found this during compiling with clang, that started to randomly SIGSEGV when it eats some heap.
Reproducer:
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
static int depth = 0;
static void* bottom_of_stack;
int inner()
{
char buffer[1024];
ptrdiff_t diff;
int ret;
++depth;
diff = bottom_of_stack - __builtin_frame_address(0);
fprintf(stderr,
"recursion depth: %i (stack diff: %zu)\n",
depth, (size_t)diff);
for (size_t i = 0; i < 1024; ++i)
ret += buffer[i];
ret += inner();
return ret;
}
int main()
{
for (size_t size = 0; size < 128<<20; size += 16<<10)
malloc(16<<10);
bottom_of_stack = __builtin_frame_address(0);
fprintf(stderr, "bottom_of_stack = %p\n", bottom_of_stack);
inner();
}--- fs/binfmt_elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/binfmt_elf.c +1 -1
@@ -1117,7 +1117,7 @@ static int load_elf_binary(struct linux_binprm *bprm)* without MAP_FIXED nor MAP_FIXED_NOREPLACE). */ alignment =maximum_alignment (elf_phdata, elf_ex->e_phnum); -if (alignment >ELF_MIN_ALIGN ) { +if (interpreter || alignment >ELF_MIN_ALIGN ) { load_bias =ELF_ET_DYN_BASE ;if (current->flags &PF_RANDOMIZE ) load_bias +=arch_mmap_rnd ();
Signature · 1 line
2.35.1
Quoted 28 lines · Azat Khuzhin
Since
9630f0d60fecELF_ET_DYN_BASEis not used as aload_biasanymore and this breaks PIE binaries, since after this change data segment became too nearby the stack:Before
9630f0d60fec:$ strace -febrk /tmp/test-stack |& head brk(NULL) = 0x555555559000 $ /tmp/test-stack bottom_of_stack = 0x7fffffffc5c0 recursion depth: 1 (stack diff: 32) ... recursion depth: 7690 (stack diff: 8365664) Segmentation fault (core dumped)After
9630f0d60fec:$ strace -ebrk /tmp/test-stack |& head brk(NULL) = 0x7ffff7fff000$ /tmp/test-stack bottom_of_stack = 0x7fffffffc640 recursion depth: 1 (stack diff: 32) ... recursion depth: 146 (stack diff: 157792) Segmentation fault (core dumped)
Quoted 2 lines
Found this during compiling with clang, that started to randomly SIGSEGV when it eats some heap.
How do I reproduce it on x86-64?
Quoted 77 lines
Reproducer:
#include <stdio.h> #include <stddef.h> #include <stdlib.h> static int depth = 0; static void* bottom_of_stack; int inner() { char buffer[1024]; ptrdiff_t diff; int ret; ++depth; diff = bottom_of_stack - __builtin_frame_address(0); fprintf(stderr, "recursion depth: %i (stack diff: %zu)\n", depth, (size_t)diff); for (size_t i = 0; i < 1024; ++i) ret += buffer[i]; ret += inner(); return ret; } int main() { for (size_t size = 0; size < 128<<20; size += 16<<10) malloc(16<<10); bottom_of_stack = __builtin_frame_address(0); fprintf(stderr, "bottom_of_stack = %p\n", bottom_of_stack); inner(); }Fixes 9630f0d60fec ("fs/binfmt_elf: use PT_LOAD p_align values for static PIE")Signed-off-by Azat Khuzhin <[email protected]>Cc Chris Kennelly <[email protected]>Cc Al Viro <[email protected]>Cc Alexey Dobriyan <[email protected]>Cc Song Liu <[email protected]>Cc David Rientjes <[email protected]>Cc Ian Rogers <[email protected]>Cc Hugh Dickins <[email protected]>Cc Suren Baghdasaryan <[email protected]>Cc Sandeep Patil <[email protected]>Cc Fangrui Song <[email protected]>Cc Nick Desaulniers <[email protected]>Cc Kirill A. Shutemov <[email protected]>Cc Mike Kravetz <[email protected]>Cc Shuah Khan <[email protected]>Cc H.J. Lu <[email protected]>--- fs/binfmt_elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)fs/binfmt_elf.c +1
-2@@ -1117,7 +1117,7 @@ static int load_elf_binary(struct linux_binprm *bprm)* without MAP_FIXED nor MAP_FIXED_NOREPLACE). */ alignment =maximum_alignment (elf_phdata, elf_ex->e_phnum); -if (alignment >ELF_MIN_ALIGN ) { +if (interpreter || alignment >ELF_MIN_ALIGN ) { load_bias =ELF_ET_DYN_BASE ;if (current->flags &PF_RANDOMIZE ) load_bias +=arch_mmap_rnd (); --2.35.1
Signature · 1 line
H.J.
Quoted 29 lines · H.J. Lu
Quoted 28 lines · Azat Khuzhin
Since
9630f0d60fecELF_ET_DYN_BASEis not used as aload_biasanymore and this breaks PIE binaries, since after this change data segment became too nearby the stack:Before
9630f0d60fec:$ strace -febrk /tmp/test-stack |& head brk(NULL) = 0x555555559000 $ /tmp/test-stack bottom_of_stack = 0x7fffffffc5c0 recursion depth: 1 (stack diff: 32) ... recursion depth: 7690 (stack diff: 8365664) Segmentation fault (core dumped)After
9630f0d60fec:$ strace -ebrk /tmp/test-stack |& head brk(NULL) = 0x7ffff7fff000$ /tmp/test-stack bottom_of_stack = 0x7fffffffc640 recursion depth: 1 (stack diff: 32) ... recursion depth: 146 (stack diff: 157792) Segmentation fault (core dumped)
Quoted 4 lines
Quoted 2 lines
Found this during compiling with clang, that started to randomly SIGSEGV when it eats some heap.
How do I reproduce it on x86-64?
It fails for me for pretty big C++ unit, so I don't have a simple reproducer with clang, but the attached reproducer below should show the problem.
Quoted 39 lines
Quoted 39 lines
Reproducer:
#include <stdio.h> #include <stddef.h> #include <stdlib.h> static int depth = 0; static void* bottom_of_stack; int inner() { char buffer[1024]; ptrdiff_t diff; int ret; ++depth; diff = bottom_of_stack - __builtin_frame_address(0); fprintf(stderr, "recursion depth: %i (stack diff: %zu)\n", depth, (size_t)diff); for (size_t i = 0; i < 1024; ++i) ret += buffer[i]; ret += inner(); return ret; } int main() { for (size_t size = 0; size < 128<<20; size += 16<<10) malloc(16<<10); bottom_of_stack = __builtin_frame_address(0); fprintf(stderr, "bottom_of_stack = %p\n", bottom_of_stack); inner(); }
Quoted 31 lines · Azat Khuzhin
Quoted 29 lines · H.J. Lu
Quoted 28 lines · Azat Khuzhin
Since
9630f0d60fecELF_ET_DYN_BASEis not used as aload_biasanymore and this breaks PIE binaries, since after this change data segment became too nearby the stack:Before
9630f0d60fec:$ strace -febrk /tmp/test-stack |& head brk(NULL) = 0x555555559000 $ /tmp/test-stack bottom_of_stack = 0x7fffffffc5c0 recursion depth: 1 (stack diff: 32) ... recursion depth: 7690 (stack diff: 8365664) Segmentation fault (core dumped)After
9630f0d60fec:$ strace -ebrk /tmp/test-stack |& head brk(NULL) = 0x7ffff7fff000$ /tmp/test-stack bottom_of_stack = 0x7fffffffc640 recursion depth: 1 (stack diff: 32) ... recursion depth: 146 (stack diff: 157792) Segmentation fault (core dumped)
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Found this during compiling with clang, that started to randomly SIGSEGV when it eats some heap.
How do I reproduce it on x86-64?
It fails for me for pretty big C++ unit, so I don't have a simple reproducer with clang, but the attached reproducer below should show the problem.
The reproducer doesn't fail for me under 5.17-rc2 on Fedora 35/x86-64 with 32GB RAM. Did you turn off PF_RANDOMIZE?
Quoted 39 lines
Quoted 39 lines
Quoted 39 lines
Reproducer:
#include <stdio.h> #include <stddef.h> #include <stdlib.h> static int depth = 0; static void* bottom_of_stack; int inner() { char buffer[1024]; ptrdiff_t diff; int ret; ++depth; diff = bottom_of_stack - __builtin_frame_address(0); fprintf(stderr, "recursion depth: %i (stack diff: %zu)\n", depth, (size_t)diff); for (size_t i = 0; i < 1024; ++i) ret += buffer[i]; ret += inner(); return ret; } int main() { for (size_t size = 0; size < 128<<20; size += 16<<10) malloc(16<<10); bottom_of_stack = __builtin_frame_address(0); fprintf(stderr, "bottom_of_stack = %p\n", bottom_of_stack); inner(); }
Signature · 1 line
H.J.
Quoted 35 lines · H.J. Lu
Quoted 34 lines · Azat Khuzhin
Quoted 32 lines · H.J. Lu
Quoted 30 lines · Azat Khuzhin
Since
9630f0d60fecELF_ET_DYN_BASEis not used as aload_biasanymore and this breaks PIE binaries, since after this change data segment became too nearby the stack:Before
9630f0d60fec:$ strace -febrk /tmp/test-stack |& head brk(NULL) = 0x555555559000 $ /tmp/test-stack bottom_of_stack = 0x7fffffffc5c0 recursion depth: 1 (stack diff: 32) ... recursion depth: 7690 (stack diff: 8365664) Segmentation fault (core dumped)After
9630f0d60fec:$ strace -ebrk /tmp/test-stack |& head brk(NULL) = 0x7ffff7fff000$ /tmp/test-stack bottom_of_stack = 0x7fffffffc640 recursion depth: 1 (stack diff: 32) ... recursion depth: 146 (stack diff: 157792) Segmentation fault (core dumped)Found this during compiling with clang, that started to randomly SIGSEGV when it eats some heap.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
How do I reproduce it on x86-64?
It fails for me for pretty big C++ unit, so I don't have a simple reproducer with clang, but the attached reproducer below should show the problem.
The reproducer doesn't fail for me under 5.17-rc2 on Fedora 35/x86-64 with 32GB RAM. Did you turn off
PF_RANDOMIZE?
Oh, yep, forgot to mention that I have kernel.randomize_va_space=0.
Quoted 39 lines · Azat Khuzhin
Quoted 37 lines · H.J. Lu
Quoted 36 lines · Azat Khuzhin
Quoted 33 lines · H.J. Lu
Quoted 28 lines · Azat Khuzhin
Since
9630f0d60fecELF_ET_DYN_BASEis not used as aload_biasanymore and this breaks PIE binaries, since after this change data segment became too nearby the stack:Before
9630f0d60fec:$ strace -febrk /tmp/test-stack |& head brk(NULL) = 0x555555559000 $ /tmp/test-stack bottom_of_stack = 0x7fffffffc5c0 recursion depth: 1 (stack diff: 32) ... recursion depth: 7690 (stack diff: 8365664) Segmentation fault (core dumped)After
9630f0d60fec:$ strace -ebrk /tmp/test-stack |& head brk(NULL) = 0x7ffff7fff000$ /tmp/test-stack bottom_of_stack = 0x7fffffffc640 recursion depth: 1 (stack diff: 32) ... recursion depth: 146 (stack diff: 157792) Segmentation fault (core dumped)Quoted 2 lines
Found this during compiling with clang, that started to randomly SIGSEGV when it eats some heap.
How do I reproduce it on x86-64?
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
It fails for me for pretty big C++ unit, so I don't have a simple reproducer with clang, but the attached reproducer below should show the problem.
The reproducer doesn't fail for me under 5.17-rc2 on Fedora 35/x86-64 with 32GB RAM. Did you turn off
PF_RANDOMIZE?Oh, yep, forgot to mention that I have kernel.randomize_va_space=0.
PIE with interpreter and PIE with alignment > ELF_MIN_ALIGN should always be loaded from ELF_ET_DYN_BASE. Otherwise, either PIE is loaded at an address which is too low or isn't properly aligned.
Signature · 1 line
H.J.
Quoted 47 lines · H.J. Lu
Quoted 46 lines · Azat Khuzhin
Quoted 43 lines · H.J. Lu
Quoted 31 lines · Azat Khuzhin
Quoted 29 lines · H.J. Lu
Quoted 28 lines · Azat Khuzhin
Since
9630f0d60fecELF_ET_DYN_BASEis not used as aload_biasanymore and this breaks PIE binaries, since after this change data segment became too nearby the stack:Before
9630f0d60fec:$ strace -febrk /tmp/test-stack |& head brk(NULL) = 0x555555559000 $ /tmp/test-stack bottom_of_stack = 0x7fffffffc5c0 recursion depth: 1 (stack diff: 32) ... recursion depth: 7690 (stack diff: 8365664) Segmentation fault (core dumped)After
9630f0d60fec:$ strace -ebrk /tmp/test-stack |& head brk(NULL) = 0x7ffff7fff000$ /tmp/test-stack bottom_of_stack = 0x7fffffffc640 recursion depth: 1 (stack diff: 32) ... recursion depth: 146 (stack diff: 157792) Segmentation fault (core dumped)Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Found this during compiling with clang, that started to randomly SIGSEGV when it eats some heap.
How do I reproduce it on x86-64?
It fails for me for pretty big C++ unit, so I don't have a simple reproducer with clang, but the attached reproducer below should show the problem.
The reproducer doesn't fail for me under 5.17-rc2 on Fedora 35/x86-64 with 32GB RAM. Did you turn off
PF_RANDOMIZE?
Quoted 6 lines
Quoted 1 line
Oh, yep, forgot to mention that I have kernel.randomize_va_space=0.
PIE with interpreter and PIE with alignment >
ELF_MIN_ALIGNshould always be loaded fromELF_ET_DYN_BASE. Otherwise, either PIE is loaded at an address which is too low or isn't properly aligned.
So, this is what this patch does, right?
Any news on this patch?