Module Name:    src
Committed By:   chs
Date:           Sat Feb 18 01:29:09 UTC 2017

Modified Files:
        src/sys/kern: exec_elf.c kern_pax.c

Log Message:
obey the executable's ELF alignment constraints for PIE.
this fixes gdb of PIE binaries on mac68k (and other platforms
which use an ELF alignment that is larger than PAGE_SIZE).


To generate a diff of this commit:
cvs rdiff -u -r1.88 -r1.89 src/sys/kern/exec_elf.c
cvs rdiff -u -r1.57 -r1.58 src/sys/kern/kern_pax.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/exec_elf.c
diff -u src/sys/kern/exec_elf.c:1.88 src/sys/kern/exec_elf.c:1.89
--- src/sys/kern/exec_elf.c:1.88	Sun Feb 12 21:52:46 2017
+++ src/sys/kern/exec_elf.c	Sat Feb 18 01:29:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec_elf.c,v 1.88 2017/02/12 21:52:46 uwe Exp $	*/
+/*	$NetBSD: exec_elf.c,v 1.89 2017/02/18 01:29:09 chs Exp $	*/
 
 /*-
  * Copyright (c) 1994, 2000, 2005, 2015 The NetBSD Foundation, Inc.
@@ -57,7 +57,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.88 2017/02/12 21:52:46 uwe Exp $");
+__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.89 2017/02/18 01:29:09 chs Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pax.h"
@@ -134,7 +134,9 @@ elf_placedynexec(struct exec_package *ep
 			align = ph[i].p_align;
 
 	offset = (Elf_Addr)pax_aslr_exec_offset(epp, align);
-	offset += epp->ep_vm_minaddr;
+	if (offset < epp->ep_vm_minaddr)
+		offset = roundup(epp->ep_vm_minaddr, align);
+	KASSERT((offset & (align - 1)) == 0);
 
 	for (i = 0; i < eh->e_phnum; i++)
 		ph[i].p_vaddr += offset;

Index: src/sys/kern/kern_pax.c
diff -u src/sys/kern/kern_pax.c:1.57 src/sys/kern/kern_pax.c:1.58
--- src/sys/kern/kern_pax.c:1.57	Sat Sep 17 02:29:11 2016
+++ src/sys/kern/kern_pax.c	Sat Feb 18 01:29:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_pax.c,v 1.57 2016/09/17 02:29:11 christos Exp $	*/
+/*	$NetBSD: kern_pax.c,v 1.58 2017/02/18 01:29:09 chs Exp $	*/
 
 /*
  * Copyright (c) 2015 The NetBSD Foundation, Inc.
@@ -57,7 +57,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_pax.c,v 1.57 2016/09/17 02:29:11 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_pax.c,v 1.58 2017/02/18 01:29:09 chs Exp $");
 
 #include "opt_pax.h"
 
@@ -578,7 +578,7 @@ pax_aslr_offset(vaddr_t align)
 	uint32_t rand;
 	vaddr_t offset;
 
-	pax_align = align == 0 ? PGSHIFT : align;
+	pax_align = align == 0 ? PAGE_SIZE : align;
 	l2 = ilog2(pax_align);
 
 	rand = cprng_fast32();
@@ -590,7 +590,8 @@ pax_aslr_offset(vaddr_t align)
 #define	PAX_TRUNC(a, b)	((a) & ~((b) - 1))
 
 	delta = PAX_ASLR_DELTA(rand, l2, PAX_ASLR_DELTA_EXEC_LEN);
-	offset = PAX_TRUNC(delta, pax_align) + PAGE_SIZE;
+	offset = PAX_TRUNC(delta, pax_align);
+	offset = MAX(offset, pax_align);
 
 	PAX_DPRINTF("rand=%#x l2=%#zx pax_align=%#zx delta=%#zx offset=%#jx",
 	    rand, l2, pax_align, delta, (uintmax_t)offset);
@@ -608,9 +609,9 @@ pax_aslr_exec_offset(struct exec_package
 	if (pax_aslr_flags & PAX_ASLR_EXEC_OFFSET)
 		goto out;
 #endif
-	return pax_aslr_offset(align) + PAGE_SIZE;
+	return pax_aslr_offset(align);
 out:
-	return MAX(align, PAGE_SIZE);
+	return 0;
 }
 
 voff_t

Reply via email to