Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/libexec/ld.elf_so When loading a non-PIE main binary, the vi...



details:   https://anonhg.NetBSD.org/src/rev/3c1865a64992
branches:  trunk
changeset: 447292:3c1865a64992
user:      joerg <joerg%NetBSD.org@localhost>
date:      Sun Jan 06 19:44:54 2019 +0000

description:
When loading a non-PIE main binary, the virtual address must match.
Use MAP_TRYFIXED and verify that the result matches the expectation.

diffstat:

 libexec/ld.elf_so/map_object.c |  20 ++++++++++++++------
 1 files changed, 14 insertions(+), 6 deletions(-)

diffs (56 lines):

diff -r d4bf090a00ad -r 3c1865a64992 libexec/ld.elf_so/map_object.c
--- a/libexec/ld.elf_so/map_object.c    Sun Jan 06 18:56:52 2019 +0000
+++ b/libexec/ld.elf_so/map_object.c    Sun Jan 06 19:44:54 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: map_object.c,v 1.59 2019/01/04 19:54:56 joerg Exp $     */
+/*     $NetBSD: map_object.c,v 1.60 2019/01/06 19:44:54 joerg Exp $     */
 
 /*
  * Copyright 1996 John D. Polstra.
@@ -34,7 +34,7 @@
 
 #include <sys/cdefs.h>
 #ifndef lint
-__RCSID("$NetBSD: map_object.c,v 1.59 2019/01/04 19:54:56 joerg Exp $");
+__RCSID("$NetBSD: map_object.c,v 1.60 2019/01/06 19:44:54 joerg Exp $");
 #endif /* not lint */
 
 #include <errno.h>
@@ -82,7 +82,7 @@
        Elf_Addr         base_vlimit;
        Elf_Addr         text_vlimit;
        int              text_flags;
-       caddr_t          base_addr;
+       void            *base_addr;
        Elf_Off          data_offset;
        Elf_Addr         data_vaddr;
        Elf_Addr         data_vlimit;
@@ -339,10 +339,12 @@
                mapflags = MAP_ALIGNED(log2);
        }
 
+       base_addr = NULL;
 #ifdef RTLD_LOADER
-       base_addr = obj->isdynamic ? NULL : (caddr_t)base_vaddr;
-#else
-       base_addr = NULL;
+       if (!obj->isdynamic) {
+               mapflags |= MAP_TRYFIXED;
+               base_addr = (void *)(uintptr_t)base_vaddr;
+       }
 #endif
        mapsize = base_vlimit - base_vaddr;
        mapbase = mmap(base_addr, mapsize, text_flags,
@@ -352,6 +354,12 @@
                    xstrerror(errno));
                goto bad;
        }
+#ifdef RTLD_LOADER
+       if (!obj->isdynamic && mapbase != base_addr) {
+               _rtld_error("mmap of executable at correct address failed");
+               goto bad;
+       }
+#endif
 
        /* Overlay the data segment onto the proper region. */
        data_addr = mapbase + (data_vaddr - base_vaddr);



Home | Main Index | Thread Index | Old Index