Reduce ELF_ET_DYN_BASE
Subsystems (6)
cover Reduce ELF_ET_DYN_BASE
This is a follow-up to "binfmt_elf: Use ELF_ET_DYN_BASE only for PIE"[1], which allow ELF_ET_DYN_BASE to be reduced from high in the address space. That patch only changed x86, and this series changes arm, arm64, powerpc, and s390.
Since these depend on the mentioned patch (which I'm hoping akpm will pick up), I'm hoping to get arch-maintainer Acks for these (so they can be carried all together).
Thanks!
-Kees
PATCH 1/4 arm: Reduce ELF_ET_DYN_BASE
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions.
--- arch/arm/include/asm/elf.h | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
arch/arm/include/asm/elf.h +2 -6
@@ -112,12 +112,8 @@ int dump_task_regs(struct task_struct *t, elf_gregset_t *elfregs);#define CORE_DUMP_USE_REGSET #define ELF_EXEC_PAGESIZE 4096 -/* This 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. */ - -#define ELF_ET_DYN_BASE (TASK_SIZE / 3 * 2) +/* This is the base location for PIE (ET_DYN with INTERP) loads. */ +#define ELF_ET_DYN_BASE 0x400000UL/* When the program starts, a1 contains a pointer to a function to be registered with atexit, as per the SVR4 ABI. A value of 0 means we
Signature · 1 line
2.7.4
PATCH 2/4 arm64: Reduce ELF_ET_DYN_BASE
In reply to Kees CookNow that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
--- arch/arm64/include/asm/elf.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)
arch/arm64/include/asm/elf.h +6 -7
@@ -113,12 +113,13 @@#define ELF_EXEC_PAGESIZE PAGE_SIZE/* -* This 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. +* This is the base location for PIE (ET_DYN with INTERP) loads. On +* 64-bit, this is raised to 4GB to leave the entire 32-bit address +* space open for things that want to use the area for 32-bit pointers. */ -#define ELF_ET_DYN_BASE (2 * TASK_SIZE_64 / 3) +#define ELF_ET_DYN_BASE (test_thread_flag(TIF_32BIT) ? \ + 0x000400000UL : \ + 0x100000000UL)#ifndef __ASSEMBLY__
@@ -173,8 +174,6 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm,#ifdef CONFIG_COMPAT -#define COMPAT_ELF_ET_DYN_BASE (2 * TASK_SIZE_32 / 3) -/* AArch32 registers. */ #define COMPAT_ELF_NGREG 18typedef unsigned int compat_elf_greg_t ;
Signature · 1 line
2.7.4
PATCH 3/4 powerpc: Reduce ELF_ET_DYN_BASE
In reply to Kees CookNow that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
--- arch/powerpc/include/asm/elf.h | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
arch/powerpc/include/asm/elf.h +7 -6
@@ -23,12 +23,13 @@#define CORE_DUMP_USE_REGSET #define ELF_EXEC_PAGESIZE PAGE_SIZE -/* This 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. */ - -#define ELF_ET_DYN_BASE 0x20000000 +/* +* This is the base location for PIE (ET_DYN with INTERP) loads. On +* 64-bit, this is raised to 4GB to leave the entire 32-bit address +* space open for things that want to use the area for 32-bit pointers. +*/ +#define ELF_ET_DYN_BASE (is_32bit_task() ? 0x000400000UL : \ + 0x100000000UL)#define ELF_CORE_EFLAGS (is_elf2_task() ? 2 : 0)
Signature · 1 line
2.7.4
PATCH 4/4 s390: Reduce ELF_ET_DYN_BASE
In reply to Kees CookNow that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
--- arch/s390/include/asm/elf.h | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-)
arch/s390/include/asm/elf.h +7 -8
@@ -161,14 +161,13 @@ extern unsigned int vdso_enabled;#define CORE_DUMP_USE_REGSET #define ELF_EXEC_PAGESIZE 4096 -/* This 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. 64-bit -tasks are aligned to 4GB. */ -#define ELF_ET_DYN_BASE (is_compat_task() ? \ - (STACK_TOP / 3 * 2) : \ - (STACK_TOP / 3 * 2) & ~((1UL << 32) - 1)) +/* +* This is the base location for PIE (ET_DYN with INTERP) loads. On +* 64-bit, this is raised to 4GB to leave the entire 32-bit address +* space open for things that want to use the area for 32-bit pointers. +*/ +#define ELF_ET_DYN_BASE (is_compat_task() ? 0x000400000UL : \ + 0x100000000UL)/* This yields a mask that user programs can use to figure out what instruction set this CPU supports. */
Signature · 1 line
2.7.4
Re: [kernel-hardening] [PATCH 2/4] arm64: Reduce ELF_ET_DYN_BASE
In reply to Kees CookHi Kees,
Quoted 22 lines · Kees Cook
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
Signed-off-by Kees Cook <[email protected]>--- arch/arm64/include/asm/elf.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)arch/arm64/include/asm/elf.h +0
-3@@ -113,12 +113,13 @@#define ELF_EXEC_PAGESIZE PAGE_SIZE/* -* This 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
arch/arm64/include/asm/elf.h line 123
Quoted 10 lines
arch/arm64/include/asm/elf.h +6
-2-* that it will "exec", and that there is sufficient room for the brk. +* This is the base location for PIE (ET_DYN with INTERP) loads. On +* 64-bit, this is raised to 4GB to leave the entire 32-bit address +* space open for things that want to use the area for 32-bit pointers. */ -#define ELF_ET_DYN_BASE (2 * TASK_SIZE_64 / 3) +#define ELF_ET_DYN_BASE (test_thread_flag(TIF_32BIT) ? \ + 0x000400000UL : \ + 0x100000000UL)
Why are you merging this with the COMPAT definition?
Quoted 14 lines
arch/arm64/include/asm/elf.h +0
-3#ifndef __ASSEMBLY__@@ -173,8 +174,6 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm,#ifdef CONFIG_COMPAT -#define COMPAT_ELF_ET_DYN_BASE (2 * TASK_SIZE_32 / 3) -/* AArch32 registers. */ #define COMPAT_ELF_NGREG 18typedef unsigned int compat_elf_greg_t ; --2.7.4
Re: [PATCH 3/4] powerpc: Reduce ELF_ET_DYN_BASE
In reply to Kees CookQuoted 5 lines · Kees Cook
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
The change log and subject are a bit out of whack with the actual patch because previously we used 512MB.
How about?
powerpc: Move ELF_ET_DYN_BASE to 4GB / 4MB
Now that explicitly executed loaders are loaded in the mmap region, we have more freedom to decide where we position PIE binaries in the address space to avoid possible collisions with mmap or stack regions.
For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers. On 32-bit use 4MB.
Is there any particular reasoning behind the 4MB value on 32-bit?
I gave this a quick spin and it booted OK on all my test boxes, which covers 64-bit/32-bit kernel and userspace. So seems to work!
cheers
Re: [kernel-hardening] [PATCH 2/4] arm64: Reduce ELF_ET_DYN_BASE
In reply to Ard BiesheuvelQuoted 21 lines · Ard Biesheuvel
Hi Kees,
Quoted 18 lines · Kees Cook
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
Signed-off-by Kees Cook <[email protected]>--- arch/arm64/include/asm/elf.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)arch/arm64/include/asm/elf.h +0
-0@@ -113,12 +113,13 @@#define ELF_EXEC_PAGESIZE PAGE_SIZE
Quoted 16 lines
Quoted 4 lines
arch/arm64/include/asm/elf.h +0
-3/* -* This 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 arch/arm64/include/asm/elf.h line 123
Quoted 10 lines
arch/arm64/include/asm/elf.h +6
-2-* that it will "exec", and that there is sufficient room for the brk. +* This is the base location for PIE (ET_DYN with INTERP) loads. On +* 64-bit, this is raised to 4GB to leave the entire 32-bit address +* space open for things that want to use the area for 32-bit pointers. */ -#define ELF_ET_DYN_BASE (2 * TASK_SIZE_64 / 3) +#define ELF_ET_DYN_BASE (test_thread_flag(TIF_32BIT) ? \ + 0x000400000UL : \ + 0x100000000UL)Why are you merging this with the COMPAT definition?
It seemed like the right thing to do since a single definition could handle both cases. Is there something I'm overlooking in the arm64 case?
-Kees
Quoted 15 lines
Quoted 15 lines
arch/arm64/include/asm/elf.h +0
-3#ifndef __ASSEMBLY__@@ -173,8 +174,6 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm,#ifdef CONFIG_COMPAT -#define COMPAT_ELF_ET_DYN_BASE (2 * TASK_SIZE_32 / 3) -/* AArch32 registers. */ #define COMPAT_ELF_NGREG 18typedef unsigned int compat_elf_greg_t ; --2.7.4
Signature · 2 lines
Kees Cook Pixel Security
Re: [kernel-hardening] [PATCH 2/4] arm64: Reduce ELF_ET_DYN_BASE
Quoted 40 lines · Kees Cook
Quoted 21 lines · Ard Biesheuvel
Hi Kees,
Quoted 18 lines · Kees Cook
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
Signed-off-by Kees Cook <[email protected]>--- arch/arm64/include/asm/elf.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)arch/arm64/include/asm/elf.h +0
-0@@ -113,12 +113,13 @@#define ELF_EXEC_PAGESIZE PAGE_SIZEQuoted 16 lines
Quoted 4 lines
arch/arm64/include/asm/elf.h +0
-3/* -* This 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 arch/arm64/include/asm/elf.h line 123
Quoted 10 lines
arch/arm64/include/asm/elf.h +6
-2-* that it will "exec", and that there is sufficient room for the brk. +* This is the base location for PIE (ET_DYN with INTERP) loads. On +* 64-bit, this is raised to 4GB to leave the entire 32-bit address +* space open for things that want to use the area for 32-bit pointers. */ -#define ELF_ET_DYN_BASE (2 * TASK_SIZE_64 / 3) +#define ELF_ET_DYN_BASE (test_thread_flag(TIF_32BIT) ? \ + 0x000400000UL : \ + 0x100000000UL)Why are you merging this with the COMPAT definition?
Quoted 3 lines
It seemed like the right thing to do since a single definition could handle both cases. Is there something I'm overlooking in the arm64 case?
And like 5 minutes later there was a loud head-slapping noise in my office. Durr, yeah, arm64 doesn't have to deal with a "native 32-bit" mode, so the merge isn't needed. Yes yes, I will split it back up and drop the thread flag test.
Thanks!
-Kees
Quoted 20 lines
Quoted 6 lines
Quoted 6 lines
arch/arm64/include/asm/elf.h +0
-0#ifndef __ASSEMBLY__@@ -173,8 +174,6 @@ extern int arch_setup_additional_pages(struct linux_binprm *bprm,#ifdef CONFIG_COMPAT Quoted 8 lines
Quoted 8 lines
arch/arm64/include/asm/elf.h +0
-3-#define COMPAT_ELF_ET_DYN_BASE (2 * TASK_SIZE_32 / 3) -/* AArch32 registers. */ #define COMPAT_ELF_NGREG 18typedef unsigned int compat_elf_greg_t ; --2.7.4
--
Kees Cook
Pixel Security
Signature · 2 lines
Kees Cook Pixel Security
Re: [kernel-hardening] [PATCH 2/4] arm64: Reduce ELF_ET_DYN_BASE
Quoted 41 lines · Kees Cook
Quoted 40 lines · Kees Cook
Quoted 37 lines · Ard Biesheuvel
Hi Kees,
Quoted 22 lines · Kees Cook
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
Signed-off-by Kees Cook <[email protected]>--- arch/arm64/include/asm/elf.h | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-)arch/arm64/include/asm/elf.h +0
-3@@ -113,12 +113,13 @@#define ELF_EXEC_PAGESIZE PAGE_SIZE/* -* This 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 arch/arm64/include/asm/elf.h line 123
Quoted 10 lines
arch/arm64/include/asm/elf.h +6
-2-* that it will "exec", and that there is sufficient room for the brk. +* This is the base location for PIE (ET_DYN with INTERP) loads. On +* 64-bit, this is raised to 4GB to leave the entire 32-bit address +* space open for things that want to use the area for 32-bit pointers. */ -#define ELF_ET_DYN_BASE (2 * TASK_SIZE_64 / 3) +#define ELF_ET_DYN_BASE (test_thread_flag(TIF_32BIT) ? \ + 0x000400000UL : \ + 0x100000000UL)Why are you merging this with the COMPAT definition?
Quoted 9 lines
Quoted 3 lines
It seemed like the right thing to do since a single definition could handle both cases. Is there something I'm overlooking in the arm64 case?
And like 5 minutes later there was a loud head-slapping noise in my office. Durr, yeah, arm64 doesn't have to deal with a "native 32-bit" mode, so the merge isn't needed. Yes yes, I will split it back up and drop the thread flag test.
Oh, is that what I heard?
Re: [PATCH 3/4] powerpc: Reduce ELF_ET_DYN_BASE
In reply to Michael EllermanQuoted 13 lines · Michael Ellerman
Quoted 5 lines · Kees Cook
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
The change log and subject are a bit out of whack with the actual patch because previously we used 512MB.
How about?
Quoted 8 lines
powerpc: Move
ELF_ET_DYN_BASEto 4GB / 4MBNow that explicitly executed loaders are loaded in the mmap region, we have more freedom to decide where we position PIE binaries in the address space to avoid possible collisions with mmap or stack regions.
For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers. On 32-bit use 4MB.
Good idea, thanks. I'll resend the series with the commit logs updated.
Quoted 1 line
Is there any particular reasoning behind the 4MB value on 32-bit?
So, I've dug around a bit on this, and I *think* the rationale is to avoid mapping a possible 4MB page table entry when it won't be using at least a portion near the lower end (NULL address area covered blocked by mmap_min_addr). It seems to be mainly tradition, though.
Quoted 2 lines
I gave this a quick spin and it booted OK on all my test boxes, which covers 64-bit/32-bit kernel and userspace. So seems to work!
Awesome, thanks for the testing!
Quoted 3 lines
Acked-by Michael Ellerman <[email protected]>cheers
-Kees
Signature · 2 lines
Kees Cook Pixel Security
Michael Ellerman
reviewerRe: [PATCH 3/4] powerpc: Reduce ELF_ET_DYN_BASE
Quoted 23 lines · Kees Cook
Quoted 13 lines · Michael Ellerman
Quoted 5 lines · Kees Cook
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
The change log and subject are a bit out of whack with the actual patch because previously we used 512MB.
How about?
Quoted 8 lines
powerpc: Move
ELF_ET_DYN_BASEto 4GB / 4MBNow that explicitly executed loaders are loaded in the mmap region, we have more freedom to decide where we position PIE binaries in the address space to avoid possible collisions with mmap or stack regions.
For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers. On 32-bit use 4MB.
Quoted 8 lines
Good idea, thanks. I'll resend the series with the commit logs updated.
Quoted 1 line
Is there any particular reasoning behind the 4MB value on 32-bit?
So, I've dug around a bit on this, and I *think* the rationale is to avoid mapping a possible 4MB page table entry when it won't be using at least a portion near the lower end (NULL address area covered blocked by
mmap_min_addr). It seems to be mainly tradition, though.
OK, that is obscure, especially for CPUs that don't have a 4MB page size. But consistency across arches is probably best regardless.
cheers
Re: [PATCH 3/4] powerpc: Reduce ELF_ET_DYN_BASE
Quoted 29 lines · Michael Ellerman
Quoted 27 lines · Kees Cook
Quoted 13 lines · Michael Ellerman
Quoted 5 lines · Kees Cook
Now that explicitly executed loaders are loaded in the mmap region, position PIE binaries lower in the address space to avoid possible collisions with mmap or stack regions. For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers.
The change log and subject are a bit out of whack with the actual patch because previously we used 512MB.
How about?
Quoted 8 lines
powerpc: Move
ELF_ET_DYN_BASEto 4GB / 4MBNow that explicitly executed loaders are loaded in the mmap region, we have more freedom to decide where we position PIE binaries in the address space to avoid possible collisions with mmap or stack regions.
For 64-bit, align to 4GB to allow runtimes to use the entire 32-bit address space for 32-bit pointers. On 32-bit use 4MB.
Good idea, thanks. I'll resend the series with the commit logs updated.
Quoted 1 line
Is there any particular reasoning behind the 4MB value on 32-bit?
Quoted 7 lines
Quoted 4 lines
So, I've dug around a bit on this, and I *think* the rationale is to avoid mapping a possible 4MB page table entry when it won't be using at least a portion near the lower end (NULL address area covered blocked by
mmap_min_addr). It seems to be mainly tradition, though.OK, that is obscure, especially for CPUs that don't have a 4MB page size. But consistency across arches is probably best regardless.
Yeah, I like being not "close" to the NULL address, though the definition of "close" has been various values like 64K (mmap_min_addr) and 1M (x86 BIOS junk and new stack-gap size). 4MB is above even that, so, I think we're fine there. It's what Windows has used, so it's familiar and any new attack methodologies would at least be shared across OSes and architectures, so we should "notice" any problem with the value, and then we can adjust it if we need to.
-Kees
Signature · 2 lines
Kees Cook Pixel Security