Skip to content

Commit ce81bb2

Browse files
ckennellytorvalds
authored andcommitted
fs/binfmt_elf: use PT_LOAD p_align values for suitable start address
Patch series "Selecting Load Addresses According to p_align", v3. The current ELF loading mechancism provides page-aligned mappings. This can lead to the program being loaded in a way unsuitable for file-backed, transparent huge pages when handling PIE executables. While specifying -z,max-page-size=0x200000 to the linker will generate suitably aligned segments for huge pages on x86_64, the executable needs to be loaded at a suitably aligned address as well. This alignment requires the binary's cooperation, as distinct segments need to be appropriately paddded to be eligible for THP. For binaries built with increased alignment, this limits the number of bits usable for ASLR, but provides some randomization over using fixed load addresses/non-PIE binaries. This patch (of 2): The current ELF loading mechancism provides page-aligned mappings. This can lead to the program being loaded in a way unsuitable for file-backed, transparent huge pages when handling PIE executables. For binaries built with increased alignment, this limits the number of bits usable for ASLR, but provides some randomization over using fixed load addresses/non-PIE binaries. Tested by verifying program with -Wl,-z,max-page-size=0x200000 loading. [[email protected]: fix max() warning] [[email protected]: augment comment] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Chris Kennelly <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Cc: Alexander Viro <[email protected]> Cc: Alexey Dobriyan <[email protected]> Cc: Song Liu <[email protected]> Cc: David Rientjes <[email protected]> Cc: Ian Rogers <[email protected]> Cc: Hugh Dickens <[email protected]> Cc: Suren Baghdasaryan <[email protected]> Cc: Sandeep Patil <[email protected]> Cc: Fangrui Song <[email protected]> Cc: Nick Desaulniers <[email protected]> Cc: "Kirill A. Shutemov" <[email protected]> Cc: Mike Kravetz <[email protected]> Cc: Shuah Khan <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Linus Torvalds <[email protected]>
1 parent 48ca2d8 commit ce81bb2

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

fs/binfmt_elf.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/module.h>
1414
#include <linux/kernel.h>
1515
#include <linux/fs.h>
16+
#include <linux/log2.h>
1617
#include <linux/mm.h>
1718
#include <linux/mman.h>
1819
#include <linux/errno.h>
@@ -421,6 +422,26 @@ static int elf_read(struct file *file, void *buf, size_t len, loff_t pos)
421422
return 0;
422423
}
423424

425+
static unsigned long maximum_alignment(struct elf_phdr *cmds, int nr)
426+
{
427+
unsigned long alignment = 0;
428+
int i;
429+
430+
for (i = 0; i < nr; i++) {
431+
if (cmds[i].p_type == PT_LOAD) {
432+
unsigned long p_align = cmds[i].p_align;
433+
434+
/* skip non-power of two alignments as invalid */
435+
if (!is_power_of_2(p_align))
436+
continue;
437+
alignment = max(alignment, p_align);
438+
}
439+
}
440+
441+
/* ensure we align to at least one page */
442+
return ELF_PAGEALIGN(alignment);
443+
}
444+
424445
/**
425446
* load_elf_phdrs() - load ELF program headers
426447
* @elf_ex: ELF header of the binary whose program headers should be loaded
@@ -1008,6 +1029,7 @@ static int load_elf_binary(struct linux_binprm *bprm)
10081029
int elf_prot, elf_flags;
10091030
unsigned long k, vaddr;
10101031
unsigned long total_size = 0;
1032+
unsigned long alignment;
10111033

10121034
if (elf_ppnt->p_type != PT_LOAD)
10131035
continue;
@@ -1086,6 +1108,9 @@ static int load_elf_binary(struct linux_binprm *bprm)
10861108
load_bias = ELF_ET_DYN_BASE;
10871109
if (current->flags & PF_RANDOMIZE)
10881110
load_bias += arch_mmap_rnd();
1111+
alignment = maximum_alignment(elf_phdata, elf_ex->e_phnum);
1112+
if (alignment)
1113+
load_bias &= ~(alignment - 1);
10891114
elf_flags |= MAP_FIXED;
10901115
} else
10911116
load_bias = 0;

0 commit comments

Comments
 (0)