svn commit: r230784 - head/libexec/rtld-elf

Konstantin Belousov kib at FreeBSD.org
Mon Jan 30 19:52:18 UTC 2012


Author: kib
Date: Mon Jan 30 19:52:17 2012
New Revision: 230784
URL: http://svn.freebsd.org/changeset/base/230784

Log:
  Add support for GNU RELRO.
  
  Submitted by:	John Marino <draco marino st>
  MFC after:	2 weeks

Modified:
  head/libexec/rtld-elf/map_object.c
  head/libexec/rtld-elf/rtld.c
  head/libexec/rtld-elf/rtld.h

Modified: head/libexec/rtld-elf/map_object.c
==============================================================================
--- head/libexec/rtld-elf/map_object.c	Mon Jan 30 19:50:13 2012	(r230783)
+++ head/libexec/rtld-elf/map_object.c	Mon Jan 30 19:52:17 2012	(r230784)
@@ -84,6 +84,8 @@ map_object(int fd, const char *path, con
     Elf_Addr bss_vlimit;
     caddr_t bss_addr;
     Elf_Word stack_flags;
+    Elf_Addr relro_page;
+    size_t relro_size;
 
     hdr = get_elf_header(fd, path);
     if (hdr == NULL)
@@ -100,6 +102,8 @@ map_object(int fd, const char *path, con
     nsegs = -1;
     phdyn = phinterp = phtls = NULL;
     phdr_vaddr = 0;
+    relro_page = 0;
+    relro_size = 0;
     segs = alloca(sizeof(segs[0]) * hdr->e_phnum);
     stack_flags = RTLD_DEFAULT_STACK_PF_EXEC | PF_R | PF_W;
     while (phdr < phlimit) {
@@ -134,6 +138,11 @@ map_object(int fd, const char *path, con
 	case PT_GNU_STACK:
 	    stack_flags = phdr->p_flags;
 	    break;
+
+	case PT_GNU_RELRO:
+	    relro_page = phdr->p_vaddr;
+	    relro_size = phdr->p_memsz;
+	    break;
 	}
 
 	++phdr;
@@ -269,6 +278,9 @@ map_object(int fd, const char *path, con
 	obj->tlsinit = mapbase + phtls->p_vaddr;
     }
     obj->stack_flags = stack_flags;
+    obj->relro_page = obj->relocbase + trunc_page(relro_page);
+    obj->relro_size = round_page(relro_size);
+
     return obj;
 }
 

Modified: head/libexec/rtld-elf/rtld.c
==============================================================================
--- head/libexec/rtld-elf/rtld.c	Mon Jan 30 19:50:13 2012	(r230783)
+++ head/libexec/rtld-elf/rtld.c	Mon Jan 30 19:52:17 2012	(r230784)
@@ -1114,6 +1114,11 @@ digest_phdr(const Elf_Phdr *phdr, int ph
 	case PT_GNU_STACK:
 	    obj->stack_flags = ph->p_flags;
 	    break;
+
+	case PT_GNU_RELRO:
+	    obj->relro_page = obj->relocbase + trunc_page(ph->p_vaddr);
+	    obj->relro_size = round_page(ph->p_memsz);
+	    break;
 	}
     }
     if (nsegs < 1) {
@@ -2007,6 +2012,14 @@ relocate_objects(Obj_Entry *first, bool 
 	    if (reloc_jmpslots(obj, lockstate) == -1)
 		return -1;
 
+	if (obj->relro_size > 0) {
+	    if (mprotect(obj->relro_page, obj->relro_size, PROT_READ) == -1) {
+		_rtld_error("%s: Cannot enforce relro protection: %s",
+		  obj->path, strerror(errno));
+		return -1;
+	    }
+	}
+
 	/*
 	 * Set up the magic number and version in the Obj_Entry.  These
 	 * were checked in the crt1.o from the original ElfKit, so we

Modified: head/libexec/rtld-elf/rtld.h
==============================================================================
--- head/libexec/rtld-elf/rtld.h	Mon Jan 30 19:50:13 2012	(r230783)
+++ head/libexec/rtld-elf/rtld.h	Mon Jan 30 19:52:17 2012	(r230784)
@@ -168,6 +168,9 @@ typedef struct Struct_Obj_Entry {
     size_t tlsoffset;		/* Offset of static TLS block for this module */
     size_t tlsalign;		/* Alignment of static TLS block */
 
+    caddr_t relro_page;
+    size_t relro_size;
+
     /* Items from the dynamic section. */
     Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
     const Elf_Rel *rel;		/* Relocation entries */


More information about the svn-src-all mailing list