Re: [PATCH v3] binfmt_elf: Fix PIE execution with randomizationdisabled (was Re: [RFC PATCH] binfmt_elf: Fix PIE execution with randomizationdisabled)

From: Jiri Kosina
Date: Mon Oct 03 2011 - 18:57:02 EST


On Tue, 4 Oct 2011, Jiri Kosina wrote:

> > Guys, it took several people several days and 10+ emails to work out
> > what's happening in there, and the first attempt to fix it was buggy.
> > This is all a huuuuge signal that the code is unobvious, hard to
> > understand, hard to maintain.
>
> It's unfortunately true. Rewriting the ELF binary loader is on my list of
> very-long-term TODOs. It has became quite a monster over decades.
>
> > Please, let's get a good code comment in there while it's fresh in your
> > minds. So the next person who comes along doesn't have the same amount
> > of difficulty?

Andrew,

find the updated patch below. Please consider applying.

I believe Cc to stable is appropriate as well.

Thanks a lot Josh for verification of the fix.



From: Jiri Kosina <jkosina@xxxxxxx>
Subject: [PATCH] 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
exec().

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 <hongjiu.lu@xxxxxxxxx> and.
Josh Boyer <jwboyer@xxxxxxxxxx>

Cc: Ingo Molnar <mingo@xxxxxxx>
Cc: Russell King <rmk@xxxxxxxxxxxxxxxx>
Cc: stable@xxxxxxxxxx
Tested-by: Josh Boyer <jwboyer@xxxxxxxxxx>
Acked-by: Nicolas Pitre <nicolas.pitre@xxxxxxxxxx>
Signed-off-by: Jiri Kosina <jkosina@xxxxxxx>
---
fs/binfmt_elf.c | 11 ++++++++++-
1 files changed, 10 insertions(+), 1 deletions(-)

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index dd0fdfc..21ac5ee 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -795,7 +795,16 @@ static int load_elf_binary(struct linux_binprm *bprm, struct pt_regs *regs)
* 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;
+ else
+ load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
#else
load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
#endif

--
Jiri Kosina
SUSE Labs
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/