binfmt_elf: fix PIE execution with randomization disabled
The case of address space randomization being disabled in runtime through
randomize_va_space sysctl is not treated properly in load_elf_binary(),
resulting in SIGKILL coming at exec() time for certain PIE-linked binaries
in case the randomization has been disabled at runtime prior to calling
Handle the randomize_va_space == 0 case the same way as if we were not
supporting .text randomization at all.
Based on original patch by H.J. Lu and Josh Boyer.
Signed-off-by: Jiri Kosina <firstname.lastname@example.org>
Cc: Ingo Molnar <email@example.com>
Cc: Russell King <firstname.lastname@example.org>
Cc: H.J. Lu <email@example.com>
Tested-by: Josh Boyer <firstname.lastname@example.org>
Acked-by: Nicolas Pitre <email@example.com>
Signed-off-by: Andrew Morton <firstname.lastname@example.org>
Signed-off-by: Linus Torvalds <email@example.com>
diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index dd0fdfc..21ac5ee 100644
@@ -795,7 +795,16 @@
* might try to exec. This is because the brk will
* follow the loader, and is not movable. */
#if defined(CONFIG_X86) || defined(CONFIG_ARM)
- load_bias = 0;
+ /* Memory randomization might have been switched off
+ * in runtime via sysctl.
+ * If that is the case, retain the original non-zero
+ * load_bias value in order to establish proper
+ * non-randomized mappings.
+ if (current->flags & PF_RANDOMIZE)
+ load_bias = 0;
+ load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);