Skip to content

Commit

Permalink
mm: larger stack guard gap, between vmas
Browse files Browse the repository at this point in the history
commit 1be7107fbe18eed3e319a6c3e83c78254b693acb upstream.

Stack guard page is a useful feature to reduce a risk of stack smashing
into a different mapping. We have been using a single page gap which
is sufficient to prevent having stack adjacent to a different mapping.
But this seems to be insufficient in the light of the stack usage in
userspace. E.g. glibc uses as large as 64kB alloca() in many commonly
used functions. Others use constructs liks gid_t buffer[NGROUPS_MAX]
which is 256kB or stack strings with MAX_ARG_STRLEN.

This will become especially dangerous for suid binaries and the default
no limit for the stack size limit because those applications can be
tricked to consume a large portion of the stack and a single glibc call
could jump over the guard page. These attacks are not theoretical,
unfortunatelly.

Make those attacks less probable by increasing the stack guard gap
to 1MB (on systems with 4k pages; but make it depend on the page size
because systems with larger base pages might cap stack allocations in
the PAGE_SIZE units) which should cover larger alloca() and VLA stack
allocations. It is obviously not a full fix because the problem is
somehow inherent, but it should reduce attack space a lot.

One could argue that the gap size should be configurable from userspace,
but that can be done later when somebody finds that the new 1MB is wrong
for some special case applications.  For now, add a kernel command line
option (stack_guard_gap) to specify the stack gap size (in page units).

Implementation wise, first delete all the old code for stack guard page:
because although we could get away with accounting one extra page in a
stack vma, accounting a larger gap can break userspace - case in point,
a program run with "ulimit -S -v 20000" failed when the 1MB gap was
counted for RLIMIT_AS; similar problems could come with RLIMIT_MLOCK
and strict non-overcommit mode.

Instead of keeping gap inside the stack vma, maintain the stack guard
gap as a gap between vmas: using vm_start_gap() in place of vm_start
(or vm_end_gap() in place of vm_end if VM_GROWSUP) in just those few
places which need to respect the gap - mainly arch_get_unmapped_area(),
and and the vma tree's subtree_gap support for that.

Original-patch-by: Oleg Nesterov <[email protected]>
Original-patch-by: Michal Hocko <[email protected]>
Signed-off-by: Hugh Dickins <[email protected]>
Acked-by: Michal Hocko <[email protected]>
Tested-by: Helge Deller <[email protected]> # parisc
Signed-off-by: Linus Torvalds <[email protected]>
[Hugh Dickins: Backported to 3.2]
[bwh: Fix more instances of vma->vm_start in sparc64 impl. of
 arch_get_unmapped_area_topdown() and generic impl. of
 hugetlb_get_unmapped_area()]
Signed-off-by: Ben Hutchings <[email protected]>

Change-Id: Ic6897a71683a9ac2c220103fb4abcb6c5f92fc14
Signed-off-by: Francisco Franco <[email protected]>
  • Loading branch information
Hugh Dickins authored and franciscofranco committed Nov 7, 2017
1 parent 60b7a74 commit 30e7e52
Show file tree
Hide file tree
Showing 21 changed files with 306 additions and 237 deletions.
7 changes: 7 additions & 0 deletions Documentation/kernel-parameters.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,13 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
spia_pedr=
spia_peddr=

stack_guard_gap= [MM]
override the default stack gap protection. The value
is in page units and it defines how many pages prior
to (for stacks growing down) resp. after (for stacks
growing up) the main stack are reserved for no other
mapping. Default value is 256 pages.

stacktrace [FTRACE]
Enabled the stack tracer on boot up.

Expand Down
2 changes: 1 addition & 1 deletion arch/alpha/kernel/osf_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ arch_get_unmapped_area_1(unsigned long addr, unsigned long len,
/* At this point: (!vma || addr < vma->vm_end). */
if (limit - len < addr)
return -ENOMEM;
if (!vma || addr + len <= vma->vm_start)
if (!vma || addr + len <= vm_start_gap(vma))
return addr;
addr = vma->vm_end;
vma = vma->vm_next;
Expand Down
12 changes: 7 additions & 5 deletions arch/arm/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
{
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma;
unsigned long start_addr;
unsigned long start_addr, vm_start;
int do_align = 0;
int aliasing = cache_is_vipt_aliasing();

Expand Down Expand Up @@ -101,7 +101,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,

vma = find_vma(mm, addr);
if (TASK_SIZE - len >= addr &&
(!vma || addr + len <= vma->vm_start))
(!vma || addr + len <= vm_start_gap(vma)))
return addr;
}
if (len > mm->cached_hole_size) {
Expand Down Expand Up @@ -131,15 +131,17 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
}
return -ENOMEM;
}
if (!vma || addr + len <= vma->vm_start) {
if (vma)
vm_start = vm_start_gap(vma);
if (!vma || addr + len <= vm_start) {
/*
* Remember the place where we stopped the search:
*/
mm->free_area_cache = addr + len;
return addr;
}
if (addr + mm->cached_hole_size < vma->vm_start)
mm->cached_hole_size = vma->vm_start - addr;
if (addr + mm->cached_hole_size < vm_start)
mm->cached_hole_size = vm_start - addr;
addr = vma->vm_end;
if (do_align)
addr = COLOUR_ALIGN(addr, pgoff);
Expand Down
6 changes: 3 additions & 3 deletions arch/frv/mm/elf-fdpic.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi
addr = PAGE_ALIGN(addr);
vma = find_vma(current->mm, addr);
if (TASK_SIZE - len >= addr &&
(!vma || addr + len <= vma->vm_start))
(!vma || addr + len <= vm_start_gap(vma)))
goto success;
}

Expand All @@ -89,7 +89,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi
for (; vma; vma = vma->vm_next) {
if (addr > limit)
break;
if (addr + len <= vma->vm_start)
if (addr + len <= vm_start_gap(vma))
goto success;
addr = vma->vm_end;
}
Expand All @@ -104,7 +104,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi
for (; vma; vma = vma->vm_next) {
if (addr > limit)
break;
if (addr + len <= vma->vm_start)
if (addr + len <= vm_start_gap(vma))
goto success;
addr = vma->vm_end;
}
Expand Down
18 changes: 14 additions & 4 deletions arch/ia64/kernel/sys_ia64.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ arch_get_unmapped_area (struct file *filp, unsigned long addr, unsigned long len
long map_shared = (flags & MAP_SHARED);
unsigned long start_addr, align_mask = PAGE_SIZE - 1;
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma;
struct vm_area_struct *vma, *prev;
unsigned long prev_end;

if (len > RGN_MAP_LIMIT)
return -ENOMEM;
Expand Down Expand Up @@ -58,7 +59,17 @@ arch_get_unmapped_area (struct file *filp, unsigned long addr, unsigned long len
full_search:
start_addr = addr = (addr + align_mask) & ~align_mask;

for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
for (vma = find_vma_prev(mm, addr, &prev); ; prev = vma,
vma = vma->vm_next) {
if (prev) {
prev_end = vm_end_gap(prev);
if (addr < prev_end) {
addr = (prev_end + align_mask) & ~align_mask;
/* If vma already violates gap, forget it */
if (vma && addr > vma->vm_start)
addr = vma->vm_start;
}
}
/* At this point: (!vma || addr < vma->vm_end). */
if (TASK_SIZE - len < addr || RGN_MAP_LIMIT - len < REGION_OFFSET(addr)) {
if (start_addr != TASK_UNMAPPED_BASE) {
Expand All @@ -68,12 +79,11 @@ arch_get_unmapped_area (struct file *filp, unsigned long addr, unsigned long len
}
return -ENOMEM;
}
if (!vma || addr + len <= vma->vm_start) {
if (!vma || addr + len <= vm_start_gap(vma)) {
/* Remember the address where we stopped this search: */
mm->free_area_cache = addr + len;
return addr;
}
addr = (vma->vm_end + align_mask) & ~align_mask;
}
}

Expand Down
4 changes: 2 additions & 2 deletions arch/ia64/mm/hugetlbpage.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ unsigned long hugetlb_get_unmapped_area(struct file *file, unsigned long addr, u
/* At this point: (!vmm || addr < vmm->vm_end). */
if (REGION_OFFSET(addr) + len > RGN_MAP_LIMIT)
return -ENOMEM;
if (!vmm || (addr + len) <= vmm->vm_start)
if (!vmm || (addr + len) <= vm_start_gap(vmm))
return addr;
addr = ALIGN(vmm->vm_end, HPAGE_SIZE);
addr = ALIGN(vm_end_gap(vmm), HPAGE_SIZE);
}
}

Expand Down
19 changes: 11 additions & 8 deletions arch/mips/mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,
struct mm_struct *mm = current->mm;
struct vm_area_struct *vma;
unsigned long addr = addr0;
unsigned long vm_start;
int do_color_align;

if (unlikely(len > TASK_SIZE))
Expand Down Expand Up @@ -103,7 +104,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,

vma = find_vma(mm, addr);
if (TASK_SIZE - len >= addr &&
(!vma || addr + len <= vma->vm_start))
(!vma || addr + len <= vm_start_gap(vma)))
return addr;
}

Expand All @@ -118,7 +119,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,
/* At this point: (!vma || addr < vma->vm_end). */
if (TASK_SIZE - len < addr)
return -ENOMEM;
if (!vma || addr + len <= vma->vm_start)
if (!vma || addr + len <= vm_start_gap(vma))
return addr;
addr = vma->vm_end;
if (do_color_align)
Expand All @@ -145,7 +146,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,
/* make sure it can fit in the remaining address space */
if (likely(addr > len)) {
vma = find_vma(mm, addr - len);
if (!vma || addr <= vma->vm_start) {
if (!vma || addr <= vm_start_gap(vma)) {
/* cache the address as a hint for next time */
return mm->free_area_cache = addr - len;
}
Expand All @@ -165,20 +166,22 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,
* return with success:
*/
vma = find_vma(mm, addr);
if (likely(!vma || addr + len <= vma->vm_start)) {
if (vma)
vm_start = vm_start_gap(vma);
if (likely(!vma || addr + len <= vm_start)) {
/* cache the address as a hint for next time */
return mm->free_area_cache = addr;
}

/* remember the largest hole we saw so far */
if (addr + mm->cached_hole_size < vma->vm_start)
mm->cached_hole_size = vma->vm_start - addr;
if (addr + mm->cached_hole_size < vm_start)
mm->cached_hole_size = vm_start - addr;

/* try just below the current vma->vm_start */
addr = vma->vm_start - len;
addr = vm_start - len;
if (do_color_align)
addr = COLOUR_ALIGN_DOWN(addr, pgoff);
} while (likely(len < vma->vm_start));
} while (likely(len < vm_start));

bottomup:
/*
Expand Down
40 changes: 30 additions & 10 deletions arch/parisc/kernel/sys_parisc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,27 @@

static unsigned long get_unshared_area(unsigned long addr, unsigned long len)
{
struct vm_area_struct *vma;
struct vm_area_struct *vma, *prev;
unsigned long prev_end;

addr = PAGE_ALIGN(addr);

for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
for (vma = find_vma_prev(current->mm, addr, &prev); ; prev = vma,
vma = vma->vm_next) {
if (prev) {
prev_end = vm_end_gap(prev);
if (addr < prev_end) {
addr = prev_end;
/* If vma already violates gap, forget it */
if (vma && addr > vma->vm_start)
addr = vma->vm_start;
}
}
/* At this point: (!vma || addr < vma->vm_end). */
if (TASK_SIZE - len < addr)
return -ENOMEM;
if (!vma || addr + len <= vma->vm_start)
if (!vma || addr + len <= vm_start_gap(vma))
return addr;
addr = vma->vm_end;
}
}

Expand All @@ -70,20 +80,30 @@ static int get_offset(struct address_space *mapping)
static unsigned long get_shared_area(struct address_space *mapping,
unsigned long addr, unsigned long len, unsigned long pgoff)
{
struct vm_area_struct *vma;
struct vm_area_struct *vma, *prev;
unsigned long prev_end;
int offset = mapping ? get_offset(mapping) : 0;

addr = DCACHE_ALIGN(addr - offset) + offset;

for (vma = find_vma(current->mm, addr); ; vma = vma->vm_next) {
for (vma = find_vma_prev(current->mm, addr, &prev); ; prev = vma,
vma = vma->vm_next) {
if (prev) {
prev_end = vm_end_gap(prev);
if (addr < prev_end) {
addr = DCACHE_ALIGN(prev_end - offset) + offset;
if (addr < prev_end) /* handle wraparound */
return -ENOMEM;
/* If vma already violates gap, forget it */
if (vma && addr > vma->vm_start)
addr = vma->vm_start;
}
}
/* At this point: (!vma || addr < vma->vm_end). */
if (TASK_SIZE - len < addr)
return -ENOMEM;
if (!vma || addr + len <= vma->vm_start)
if (!vma || addr + len <= vm_start_gap(vma))
return addr;
addr = DCACHE_ALIGN(vma->vm_end - offset) + offset;
if (addr < vma->vm_end) /* handle wraparound */
return -ENOMEM;
}
}

Expand Down
24 changes: 14 additions & 10 deletions arch/powerpc/mm/slice.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
if ((mm->task_size - len) < addr)
return 0;
vma = find_vma(mm, addr);
return (!vma || (addr + len) <= vma->vm_start);
return (!vma || (addr + len) <= vm_start_gap(vma));
}

static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
Expand Down Expand Up @@ -227,7 +227,7 @@ static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
int psize, int use_cache)
{
struct vm_area_struct *vma;
unsigned long start_addr, addr;
unsigned long start_addr, addr, vm_start;
struct slice_mask mask;
int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);

Expand Down Expand Up @@ -256,16 +256,18 @@ static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
addr = _ALIGN_UP(addr + 1, 1ul << SLICE_HIGH_SHIFT);
continue;
}
if (!vma || addr + len <= vma->vm_start) {
if (vma)
vm_start = vm_start_gap(vma);
if (!vma || addr + len <= vm_start) {
/*
* Remember the place where we stopped the search:
*/
if (use_cache)
mm->free_area_cache = addr + len;
return addr;
}
if (use_cache && (addr + mm->cached_hole_size) < vma->vm_start)
mm->cached_hole_size = vma->vm_start - addr;
if (use_cache && (addr + mm->cached_hole_size) < vm_start)
mm->cached_hole_size = vm_start - addr;
addr = vma->vm_end;
}

Expand All @@ -284,7 +286,7 @@ static unsigned long slice_find_area_topdown(struct mm_struct *mm,
int psize, int use_cache)
{
struct vm_area_struct *vma;
unsigned long addr;
unsigned long addr, vm_start;
struct slice_mask mask;
int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);

Expand Down Expand Up @@ -336,19 +338,21 @@ static unsigned long slice_find_area_topdown(struct mm_struct *mm,
* return with success:
*/
vma = find_vma(mm, addr);
if (!vma || (addr + len) <= vma->vm_start) {
if (vma)
vm_start = vm_start_gap(vma);
if (!vma || (addr + len) <= vm_start) {
/* remember the address as a hint for next time */
if (use_cache)
mm->free_area_cache = addr;
return addr;
}

/* remember the largest hole we saw so far */
if (use_cache && (addr + mm->cached_hole_size) < vma->vm_start)
mm->cached_hole_size = vma->vm_start - addr;
if (use_cache && (addr + mm->cached_hole_size) < vm_start)
mm->cached_hole_size = vm_start - addr;

/* try just below the current vma->vm_start */
addr = vma->vm_start;
addr = vm_start;
}

/*
Expand Down
Loading

0 comments on commit 30e7e52

Please sign in to comment.