Skip to content

Commit 805f25c

Browse files
Hector Marco-Gisbertgregkh
authored andcommitted
x86, mm/ASLR: Fix stack randomization on 64-bit systems
commit 4e7c22d upstream. The issue is that the stack for processes is not properly randomized on 64 bit architectures due to an integer overflow. The affected function is randomize_stack_top() in file "fs/binfmt_elf.c": static unsigned long randomize_stack_top(unsigned long stack_top) { unsigned int random_variable = 0; if ((current->flags & PF_RANDOMIZE) && !(current->personality & ADDR_NO_RANDOMIZE)) { random_variable = get_random_int() & STACK_RND_MASK; random_variable <<= PAGE_SHIFT; } return PAGE_ALIGN(stack_top) + random_variable; return PAGE_ALIGN(stack_top) - random_variable; } Note that, it declares the "random_variable" variable as "unsigned int". Since the result of the shifting operation between STACK_RND_MASK (which is 0x3fffff on x86_64, 22 bits) and PAGE_SHIFT (which is 12 on x86_64): random_variable <<= PAGE_SHIFT; then the two leftmost bits are dropped when storing the result in the "random_variable". This variable shall be at least 34 bits long to hold the (22+12) result. These two dropped bits have an impact on the entropy of process stack. Concretely, the total stack entropy is reduced by four: from 2^28 to 2^30 (One fourth of expected entropy). This patch restores back the entropy by correcting the types involved in the operations in the functions randomize_stack_top() and stack_maxrandom_size(). The successful fix can be tested with: $ for i in `seq 1 10`; do cat /proc/self/maps | grep stack; done 7ffeda566000-7ffeda587000 rw-p 00000000 00:00 0 [stack] 7fff5a332000-7fff5a353000 rw-p 00000000 00:00 0 [stack] 7ffcdb7a1000-7ffcdb7c2000 rw-p 00000000 00:00 0 [stack] 7ffd5e2c4000-7ffd5e2e5000 rw-p 00000000 00:00 0 [stack] ... Once corrected, the leading bytes should be between 7ffc and 7fff, rather than always being 7fff. Signed-off-by: Hector Marco-Gisbert <hecmargi@upv.es> Signed-off-by: Ismael Ripoll <iripoll@upv.es> [ Rebased, fixed 80 char bugs, cleaned up commit message, added test example and CVE ] Signed-off-by: Kees Cook <keescook@chromium.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Al Viro <viro@zeniv.linux.org.uk> Fixes: CVE-2015-1593 Link: http://lkml.kernel.org/r/20150214173350.GA18393@www.outflux.net Signed-off-by: Borislav Petkov <bp@suse.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 55c0226 commit 805f25c

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

‎arch/x86/mm/mmap.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ struct va_alignment __read_mostly va_align = {
3535
.flags = -1,
3636
};
3737

38-
static unsigned int stack_maxrandom_size(void)
38+
static unsigned long stack_maxrandom_size(void)
3939
{
40-
unsigned int max = 0;
40+
unsigned long max = 0;
4141
if ((current->flags & PF_RANDOMIZE) &&
4242
!(current->personality & ADDR_NO_RANDOMIZE)) {
43-
max = ((-1U) & STACK_RND_MASK) << PAGE_SHIFT;
43+
max = ((-1UL) & STACK_RND_MASK) << PAGE_SHIFT;
4444
}
4545

4646
return max;

‎fs/binfmt_elf.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,11 +554,12 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex,
554554

555555
static unsigned long randomize_stack_top(unsigned long stack_top)
556556
{
557-
unsigned int random_variable = 0;
557+
unsigned long random_variable = 0;
558558

559559
if ((current->flags & PF_RANDOMIZE) &&
560560
!(current->personality & ADDR_NO_RANDOMIZE)) {
561-
random_variable = get_random_int() & STACK_RND_MASK;
561+
random_variable = (unsigned long) get_random_int();
562+
random_variable &= STACK_RND_MASK;
562563
random_variable <<= PAGE_SHIFT;
563564
}
564565
#ifdef CONFIG_STACK_GROWSUP

0 commit comments

Comments
 (0)