This is v2 (to refresh the 5 patches in -mm) for moving ELF_ET_DYN_BASE
safely lower. Changes are clarifications in the commit logs (suggested
by mpe), a compat think-o fix for arm64 (thanks to Ard), and to add
Rik and mpe's Acks.
Quoting patch 1/5:
The ELF_ET_DYN_BASE position was originally intended to keep loaders
away from ET_EXEC binaries. (For example, running "/lib/ld-linux.so.2
/bin/cat" might cause the subsequent load of /bin/cat into where the
loader had been loaded.) With the advent of PIE (ET_DYN binaries with
an INTERP Program Header), ELF_ET_DYN_BASE continued to be used since
the kernel was only looking at ET_DYN. However, since ELF_ET_DYN_BASE
is traditionally set at the top 1/3rd of the TASK_SIZE, a substantial
portion of the address space is unused.
For 32-bit tasks when RLIMIT_STACK is set to RLIM_INFINITY, programs
are loaded below the mmap region. This means they can be made to collide
(CVE-2017-1000370) or nearly collide (CVE-2017-1000371) with pathological
stack regions. Lowering ELF_ET_DYN_BASE solves both by moving programs
above the mmap region in all cases, and will now additionally avoid
programs falling back to the mmap region by enforcing MAP_FIXED for
program loads (i.e. if it would have collided with the stack, now it
will fail to load instead of falling back to the mmap region).
To allow for a lower ELF_ET_DYN_BASE, loaders (ET_DYN without INTERP)
are loaded into the mmap region, leaving space available for either an
ET_EXEC binary with a fixed location or PIE being loaded into mmap by the
loader. Only PIE programs are loaded offset from ELF_ET_DYN_BASE, which
means architectures can now safely lower their values without risk of
loaders colliding with their subsequently loaded programs.
For 64-bit, ELF_ET_DYN_BASE is best set to 4GB to allow runtimes to
use the entire 32-bit address space for 32-bit pointers. For 32-bit,
4MB is used as the traditional minimum load location, likely to avoid
historically requiring a 4MB page table entry when only a portion of the
first 4MB would be used (since the NULL address is avoided).
Thanks to PaX Team, Daniel Micay, and Rik van Riel for inspiration and
suggestions on how to implement this solution.
-Kees
The ELF_ET_DYN_BASE position was originally intended to keep loaders
away from ET_EXEC binaries. (For example, running "/lib/ld-linux.so.2
/bin/cat" might cause the subsequent load of /bin/cat into where the
loader had been loaded.) With the advent of PIE (ET_DYN binaries with
an INTERP Program Header), ELF_ET_DYN_BASE continued to be used since
the kernel was only looking at ET_DYN. However, since ELF_ET_DYN_BASE
is traditionally set at the top 1/3rd of the TASK_SIZE, a substantial
portion of the address space is unused.
For 32-bit tasks when RLIMIT_STACK is set to RLIM_INFINITY, programs
are loaded below the mmap region. This means they can be made to collide
(CVE-2017-1000370) or nearly collide (CVE-2017-1000371) with pathological
stack regions. Lowering ELF_ET_DYN_BASE solves both by moving programs
above the mmap region in all cases, and will now additionally avoid
programs falling back to the mmap region by enforcing MAP_FIXED for
program loads (i.e. if it would have collided with the stack, now it
will fail to load instead of falling back to the mmap region).
To allow for a lower ELF_ET_DYN_BASE, loaders (ET_DYN without INTERP)
are loaded into the mmap region, leaving space available for either an
ET_EXEC binary with a fixed location or PIE being loaded into mmap by the
loader. Only PIE programs are loaded offset from ELF_ET_DYN_BASE, which
means architectures can now safely lower their values without risk of
loaders colliding with their subsequently loaded programs.
For 64-bit, ELF_ET_DYN_BASE is best set to 4GB to allow runtimes to
use the entire 32-bit address space for 32-bit pointers. For 32-bit,
4MB is used as the traditional minimum load location, likely to avoid
historically requiring a 4MB page table entry when only a portion of the
first 4MB would be used (since the NULL address is avoided).
Thanks to PaX Team, Daniel Micay, and Rik van Riel for inspiration and
suggestions on how to implement this solution.
Fixes: d1fd836dcf00 ("mm: split ET_DYN ASLR from mmap ASLR")
Cc: stable@vger.kernel.org
Cc: x86@kernel.org
Signed-off-by: Kees Cook <redacted>
Acked-by: Rik van Riel <redacted>
---
arch/x86/include/asm/elf.h | 13 +++++-----
fs/binfmt_elf.c | 59 +++++++++++++++++++++++++++++++++++++++-------
2 files changed, 58 insertions(+), 14 deletions(-)
@@ -245,12 +245,13 @@ extern int force_personality32;#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-useofthisistoinvoke"./ld.so someprog"totestoutanewversionof-theloader.Weneedtomakesurethatitisoutofthewayoftheprogram-thatitwill"exec",andthatthereissufficientroomforthebrk.*/--#define ELF_ET_DYN_BASE (TASK_SIZE / 3 * 2)+/*+*ThisisthebaselocationforPIE(ET_DYNwithINTERP)loads.On+*64-bit,thisisraisedto4GBtoleavetheentire32-bitaddress+*spaceopenforthingsthatwanttousetheareafor32-bitpointers.+*/+#define ELF_ET_DYN_BASE (mmap_is_ia32() ? 0x000400000UL : \+0x100000000UL)/* This yields a mask that user programs can use to figure out whatinstructionsetthisCPUsupports.Thiscouldbedoneinuserspace,
@@ -925,17 +925,60 @@ static int load_elf_binary(struct linux_binprm *bprm)elf_flags=MAP_PRIVATE|MAP_DENYWRITE|MAP_EXECUTABLE;vaddr=elf_ppnt->p_vaddr;+/*+*IfweareloadingET_EXECorwehavealreadyperformed+*theET_DYNload_addrcalculations,proceednormally.+*/if(loc->elf_ex.e_type==ET_EXEC||load_addr_set){elf_flags|=MAP_FIXED;}elseif(loc->elf_ex.e_type==ET_DYN){-/* Try and get dynamic programs out of the way of the-*defaultmmapbase,aswellaswhateverprogramthey-*mighttrytoexec.Thisisbecausethebrkwill-*followtheloader,andisnotmovable.*/-load_bias=ELF_ET_DYN_BASE-vaddr;-if(current->flags&PF_RANDOMIZE)-load_bias+=arch_mmap_rnd();-load_bias=ELF_PAGESTART(load_bias);+/*+*ThislogicisrunonceforthefirstLOADProgram+*HeaderforET_DYNbinariestocalculatethe+*randomization(load_bias)foralltheLOAD+*ProgramHeaders,andtocalculatetheentire+*sizeoftheELFmapping(total_size).(Notethat+*load_addr_setissettotruelateroncethe+*initialmappingisperformed.)+*+*ThereareeffectivelytwotypesofET_DYN+*binaries:programs(i.e.PIE:ET_DYNwithINTERP)+*andloaders(ET_DYNwithoutINTERP,sincethey+*_are_theELFinterpreter).Theloadersmust+*beloadedawayfromprogramssincetheprogram+*mayotherwisecollidewiththeloader(especially+*forET_EXECwhichdoesnothavearandomized+*position).Forexampletohandleinvocationsof+*"./ld.so someprog"totestoutanewversionof+*theloader,thesubsequentprogramthatthe+*loaderloadsmustavoidtheloaderitself,so+*theycannotsharethesameloadrange.Sufficient+*roomforthebrkmustbeallocatedwiththe+*loaderaswell,sincebrkmustbeavailablewith+*theloader.+*+*Therefore,programsareloadedoffsetfrom+*ELF_ET_DYN_BASEandloadersareloadedintothe+*independentlyrandomizedmmapregion(0load_bias+*withoutMAP_FIXED).+*/+if(elf_interpreter){+load_bias=ELF_ET_DYN_BASE;+if(current->flags&PF_RANDOMIZE)+load_bias+=arch_mmap_rnd();+elf_flags|=MAP_FIXED;+}else+load_bias=0;++/*+*Sinceload_biasisusedforallsubsequentloading+*calculations,wemustloweritbythefirstvaddr+*sothattheremainingcalculationsbasedonthe+*ELFvaddrswillbecorrectlyoffset.Theresult+*isthenpagealigned.+*/+load_bias=ELF_PAGESTART(load_bias-vaddr);+total_size=total_mapping_size(elf_phdata,loc->elf_ex.e_phnum);if(!total_size){
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.
4MB is chosen here mainly to have parity with x86, where this is the
traditional minimum load location, likely to avoid historically requiring
a 4MB page table entry when only a portion of the first 4MB would be used
(since the NULL address is avoided). For ARM the position could be 0x8000,
the standard ET_EXEC load address, but that is needlessly close to the
NULL address, and anyone running PIE on 32-bit ARM will have an MMU, so
the tight mapping is not needed.
Cc: stable@vger.kernel.org
Cc: Russell King <redacted>
Signed-off-by: Kees Cook <redacted>
---
arch/arm/include/asm/elf.h | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
@@ -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-useofthisistoinvoke"./ld.so someprog"totestoutanewversionof-theloader.Weneedtomakesurethatitisoutofthewayoftheprogram-thatitwill"exec",andthatthereissufficientroomforthebrk.*/--#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 registeredwithatexit,aspertheSVR4ABI.Avalueof0meanswe
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, which is the
traditional x86 minimum load location, likely to avoid historically
requiring a 4MB page table entry when only a portion of the first 4MB
would be used (since the NULL address is avoided).
Cc: stable@vger.kernel.org
Signed-off-by: Kees Cook <redacted>
Acked-by: Michael Ellerman <mpe@ellerman.id.au>
---
arch/powerpc/include/asm/elf.h | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
@@ -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-useofthisistoinvoke"./ld.so someprog"totestoutanewversionof-theloader.Weneedtomakesurethatitisoutofthewayoftheprogram-thatitwill"exec",andthatthereissufficientroomforthebrk.*/--#define ELF_ET_DYN_BASE 0x20000000+/*+*ThisisthebaselocationforPIE(ET_DYNwithINTERP)loads.On+*64-bit,thisisraisedto4GBtoleavetheentire32-bitaddress+*spaceopenforthingsthatwanttousetheareafor32-bitpointers.+*/+#define ELF_ET_DYN_BASE (is_32bit_task() ? 0x000400000UL : \+0x100000000UL)#define ELF_CORE_EFLAGS (is_elf2_task() ? 2 : 0)
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, which is the
traditional x86 minimum load location, likely to avoid historically
requiring a 4MB page table entry when only a portion of the first 4MB
would be used (since the NULL address is avoided). For s390 the position
could be 0x10000, but that is needlessly close to the NULL address.
Cc: stable@vger.kernel.org
Cc: Heiko Carstens <redacted>
Cc: Martin Schwidefsky <redacted>
Signed-off-by: Kees Cook <redacted>
---
arch/s390/include/asm/elf.h | 15 +++++++--------
1 file changed, 7 insertions(+), 8 deletions(-)
@@ -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-useofthisistoinvoke"./ld.so someprog"totestoutanewversionof-theloader.Weneedtomakesurethatitisoutofthewayoftheprogram-thatitwill"exec",andthatthereissufficientroomforthebrk.64-bit-tasksarealignedto4GB.*/-#define ELF_ET_DYN_BASE (is_compat_task() ? \-(STACK_TOP/3*2):\-(STACK_TOP/3*2)&~((1UL<<32)-1))+/*+*ThisisthebaselocationforPIE(ET_DYNwithINTERP)loads.On+*64-bit,thisisraisedto4GBtoleavetheentire32-bitaddress+*spaceopenforthingsthatwanttousetheareafor32-bitpointers.+*/+#define ELF_ET_DYN_BASE (is_compat_task() ? 0x000400000UL : \+0x100000000UL)/* This yields a mask that user programs can use to figure out whatinstructionsetthisCPUsupports.*/
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, to match ARM. This
could be 0x8000, the standard ET_EXEC load address, but that is needlessly
close to the NULL address, and anyone running arm compat PIE will have an
MMU, so the tight mapping is not needed.
Cc: stable@vger.kernel.org
Cc: Ard Biesheuvel <redacted>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Kees Cook <redacted>
---
arch/arm64/include/asm/elf.h | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
From: Russell King - ARM Linux <linux@armlinux.org.uk> Date: 2017-06-24 09:11:58
On Fri, Jun 23, 2017 at 01:59:55PM -0700, Kees Cook wrote:
This is v2 (to refresh the 5 patches in -mm) for moving ELF_ET_DYN_BASE
safely lower. Changes are clarifications in the commit logs (suggested
by mpe), a compat think-o fix for arm64 (thanks to Ard), and to add
Rik and mpe's Acks.
Quoting patch 1/5:
The ELF_ET_DYN_BASE position was originally intended to keep loaders
away from ET_EXEC binaries. (For example, running "/lib/ld-linux.so.2
/bin/cat" might cause the subsequent load of /bin/cat into where the
loader had been loaded.) With the advent of PIE (ET_DYN binaries with
an INTERP Program Header), ELF_ET_DYN_BASE continued to be used since
the kernel was only looking at ET_DYN. However, since ELF_ET_DYN_BASE
is traditionally set at the top 1/3rd of the TASK_SIZE, a substantial
portion of the address space is unused.
With existing kernels on ARM:
00010000-00017000 r-xp 00000000 08:01 270810 /bin/cat
00026000-00027000 r--p 00006000 08:01 270810 /bin/cat
00027000-00028000 rw-p 00007000 08:01 270810 /bin/cat
7f661000-7f679000 r-xp 00000000 08:01 281659 /lib/arm-linux-gnueabihf/ld-2.23.so
7f688000-7f689000 r--p 00017000 08:01 281659 /lib/arm-linux-gnueabihf/ld-2.23.so
7f689000-7f68a000 rw-p 00018000 08:01 281659 /lib/arm-linux-gnueabihf/ld-2.23.so
If the loader is loaded at 4MB, this means the size of an ET_EXEC
program is limited to less than 4MB - and distros aren't yet
building everything as PIE on ARM.
--
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.
On Sat, Jun 24, 2017 at 2:11 AM, Russell King - ARM Linux
[off-list ref] wrote:
On Fri, Jun 23, 2017 at 01:59:55PM -0700, Kees Cook wrote:
quoted
This is v2 (to refresh the 5 patches in -mm) for moving ELF_ET_DYN_BASE
safely lower. Changes are clarifications in the commit logs (suggested
by mpe), a compat think-o fix for arm64 (thanks to Ard), and to add
Rik and mpe's Acks.
Quoting patch 1/5:
The ELF_ET_DYN_BASE position was originally intended to keep loaders
away from ET_EXEC binaries. (For example, running "/lib/ld-linux.so.2
/bin/cat" might cause the subsequent load of /bin/cat into where the
loader had been loaded.) With the advent of PIE (ET_DYN binaries with
an INTERP Program Header), ELF_ET_DYN_BASE continued to be used since
the kernel was only looking at ET_DYN. However, since ELF_ET_DYN_BASE
is traditionally set at the top 1/3rd of the TASK_SIZE, a substantial
portion of the address space is unused.
With existing kernels on ARM:
00010000-00017000 r-xp 00000000 08:01 270810 /bin/cat
00026000-00027000 r--p 00006000 08:01 270810 /bin/cat
00027000-00028000 rw-p 00007000 08:01 270810 /bin/cat
7f661000-7f679000 r-xp 00000000 08:01 281659 /lib/arm-linux-gnueabihf/ld-2.23.so
7f688000-7f689000 r--p 00017000 08:01 281659 /lib/arm-linux-gnueabihf/ld-2.23.so
7f689000-7f68a000 rw-p 00018000 08:01 281659 /lib/arm-linux-gnueabihf/ld-2.23.so
If the loader is loaded at 4MB, this means the size of an ET_EXEC
program is limited to less than 4MB - and distros aren't yet
building everything as PIE on ARM.
The loader isn't loaded at 4MB; that's what patch 1 changes: loaders
are moved into the mmap region so they will not collide with either
ET_EXEC nor PIE (ET_DYN-with-INTERP).
(After this patch, the name "ELF_ET_DYN_BASE" becomes a bit misleading...)
-Kees
--
Kees Cook
Pixel Security
On Fri, Jun 23, 2017 at 1:59 PM, Kees Cook [off-list ref] wrote:
For 32-bit tasks when RLIMIT_STACK is set to RLIM_INFINITY, programs
are loaded below the mmap region. This means they can be made to collide
(CVE-2017-1000370) or nearly collide (CVE-2017-1000371) with pathological
stack regions. Lowering ELF_ET_DYN_BASE solves both by moving programs
above the mmap region in all cases, and will now additionally avoid
programs falling back to the mmap region by enforcing MAP_FIXED for
program loads (i.e. if it would have collided with the stack, now it
will fail to load instead of falling back to the mmap region).
It was pointed out by rmk that I described this inaccurately. I mix up
my own visualization of the address space (above/below in
/proc/$pid/maps) with actual value comparisons (above/below
numerically). This paragraph should read:
For 32-bit tasks when RLIMIT_STACK is set to RLIM_INFINITY, programs
are loaded above the mmap region. This means they can be made to collide
(CVE-2017-1000370) or nearly collide (CVE-2017-1000371) with pathological
stack regions. Lowering ELF_ET_DYN_BASE solves both by moving programs
below the mmap region in all cases, and will now additionally avoid
programs falling back to the mmap region by enforcing MAP_FIXED for
program loads (i.e. if it would have collided with the stack, now it
will fail to load instead of falling back to the mmap region).
Andrew, are you able to manually adjust this commit log in -mm, or
should I resend the patch with this paragraph corrected?
Thanks!
-Kees
--
Kees Cook
Pixel Security