Skip to content

Commit a87938b

Browse files
calcodiontorvalds
authored andcommitted
fs/binfmt_elf.c: fix bug in loading of PIE binaries
With CONFIG_ARCH_BINFMT_ELF_RANDOMIZE_PIE enabled, and a normal top-down address allocation strategy, load_elf_binary() will attempt to map a PIE binary into an address range immediately below mm->mmap_base. Unfortunately, load_elf_ binary() does not take account of the need to allocate sufficient space for the entire binary which means that, while the first PT_LOAD segment is mapped below mm->mmap_base, the subsequent PT_LOAD segment(s) end up being mapped above mm->mmap_base into the are that is supposed to be the "gap" between the stack and the binary. Since the size of the "gap" on x86_64 is only guaranteed to be 128MB this means that binaries with large data segments > 128MB can end up mapping part of their data segment over their stack resulting in corruption of the stack (and the data segment once the binary starts to run). Any PIE binary with a data segment > 128MB is vulnerable to this although address randomization means that the actual gap between the stack and the end of the binary is normally greater than 128MB. The larger the data segment of the binary the higher the probability of failure. Fix this by calculating the total size of the binary in the same way as load_elf_interp(). Signed-off-by: Michael Davidson <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Jiri Kosina <[email protected]> Cc: Kees Cook <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent b1b0dea commit a87938b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

fs/binfmt_elf.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
862862
i < loc->elf_ex.e_phnum; i++, elf_ppnt++) {
863863
int elf_prot = 0, elf_flags;
864864
unsigned long k, vaddr;
865+
unsigned long total_size = 0;
865866

866867
if (elf_ppnt->p_type != PT_LOAD)
867868
continue;
@@ -924,10 +925,16 @@ static int load_elf_binary(struct linux_binprm *bprm)
924925
#else
925926
load_bias = ELF_PAGESTART(ELF_ET_DYN_BASE - vaddr);
926927
#endif
928+
total_size = total_mapping_size(elf_phdata,
929+
loc->elf_ex.e_phnum);
930+
if (!total_size) {
931+
error = -EINVAL;
932+
goto out_free_dentry;
933+
}
927934
}
928935

929936
error = elf_map(bprm->file, load_bias + vaddr, elf_ppnt,
930-
elf_prot, elf_flags, 0);
937+
elf_prot, elf_flags, total_size);
931938
if (BAD_ADDR(error)) {
932939
retval = IS_ERR((void *)error) ?
933940
PTR_ERR((void*)error) : -EINVAL;

0 commit comments

Comments
 (0)