All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC] fs/binfmt_elf: fix memory map for PIE applications
@ 2013-10-02  7:19 ` Timo Teräs
  0 siblings, 0 replies; 8+ messages in thread
From: Timo Teräs @ 2013-10-02  7:19 UTC (permalink / raw)
  To: linux-fsdevel, linux-kernel, Jiri Kosina; +Cc: Timo Teräs

arch/*/include/asm/elf.h comments say:
  ELF_ET_DYN_BASE is the location that an ET_DYN program is loaded
  if exec'ed.  Typical use of this is to invoke "./ld.so someprog"
  to test out a new version of the loader.  We need to make sure
  that it is out of the way of the program that it will "exec",
  and that there is sufficient room for the brk.

In case we have main application linked as PIE, this can cause
problems as the main program itself is being loaded to this
alternate address. And this allows limited heap size. While
this is inevitable when exec'ing the interpreter directly,
we should do better for PIE applications.

This fixes the loader to detect PIE application by checking if
elf_interpreter is requested. This images are loaded to beginning
of the address space instead of the specially crafted place for elf
interpreter. This allows full heap address space for PIE applications
and fixes random "out of memory" errors.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
---
 fs/binfmt_elf.c | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

It might make sense to define ELF_ET_DYN_APP_BASE or similar
so that architectures can specify the load address of ET_DYN
applications.

diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c
index 100edcc..f1508c7 100644
--- a/fs/binfmt_elf.c
+++ b/fs/binfmt_elf.c
@@ -802,21 +802,19 @@ static int load_elf_binary(struct linux_binprm *bprm)
 			 * default mmap base, as well as whatever program they
 			 * might try to exec.  This is because the brk will
 			 * follow the loader, and is not movable.  */
+			if (elf_interpreter)
+				load_bias = 0x00400000UL;
+			else
+				load_bias = ELF_ET_DYN_BASE;
 #ifdef CONFIG_ARCH_BINFMT_ELF_RANDOMIZE_PIE
 			/* Memory randomization might have been switched off
 			 * in runtime via sysctl or explicit setting of
 			 * personality flags.
-			 * 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);
+				load_bias += (get_random_int() & STACK_RND_MASK) << PAGE_SHIFT;
 #endif
+			load_bias = ELF_PAGESTART(vaddr + load_bias);
 		}
 
 		error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
-- 
1.8.4

--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply related	[flat|nested] 8+ messages in thread
* Re: [PATCH RFC] fs/binfmt_elf: fix memory map for PIE applications
@ 2014-03-08 22:44 David Heidelberger
  0 siblings, 0 replies; 8+ messages in thread
From: David Heidelberger @ 2014-03-08 22:44 UTC (permalink / raw)
  To: linux-fsdevel

> Hmm, that's really unfortunate. Applications really should be prepared 
> for
> failing brk(), as there is no guarantee provided by anyobody whatsoever
> about the space allocated for the program break.
> 
> Heck, even my manpage tells me:
> 
> "Avoid using brk() and sbrk(): the malloc(3) memory allocation package 
> is
>  the portable and comfortable  way  of allocating memory."
> 
> --
> Jiri Kosina
> SUSE Labs

Hello Jiri,

I slightly looked at patch, does it have any drawbacks?

If I understand correctly, this simply fix could avoid
bloat from musl-libc and should be better way than actual status.
Is there any reason to not include this patch?

Thank you
David

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2014-03-08 21:51 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-10-02  7:19 [PATCH RFC] fs/binfmt_elf: fix memory map for PIE applications Timo Teräs
2013-10-02  7:19 ` Timo Teräs
2013-12-19 14:17 ` Jiri Kosina
2013-12-19 14:42   ` Timo Teras
2013-12-19 15:02     ` Jiri Kosina
2013-12-19 15:26       ` Timo Teras
2013-12-19 15:33         ` Jiri Kosina
  -- strict thread matches above, loose matches on Subject: below --
2014-03-08 22:44 David Heidelberger

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.