Skip to content

Commit 4b35943

Browse files
Hugh Dickinsgregkh
authored andcommitted
mm: larger stack guard gap, between vmas
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 <oleg@redhat.com> Original-patch-by: Michal Hocko <mhocko@suse.com> Signed-off-by: Hugh Dickins <hughd@google.com> Acked-by: Michal Hocko <mhocko@suse.com> Tested-by: Helge Deller <deller@gmx.de> # parisc Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> [wt: backport to 4.11: adjust context] [wt: backport to 4.9: adjust context ; kernel doc was not in admin-guide] [wt: backport to 4.4: adjust context ; drop ppc hugetlb_radix changes] Signed-off-by: Willy Tarreau <w@1wt.eu> [gkh: minor build fixes for 4.4] Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 26605a0 commit 4b35943

21 files changed

Lines changed: 149 additions & 160 deletions

File tree

Documentation/kernel-parameters.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3580,6 +3580,13 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
35803580
spia_pedr=
35813581
spia_peddr=
35823582

3583+
stack_guard_gap= [MM]
3584+
override the default stack gap protection. The value
3585+
is in page units and it defines how many pages prior
3586+
to (for stacks growing down) resp. after (for stacks
3587+
growing up) the main stack are reserved for no other
3588+
mapping. Default value is 256 pages.
3589+
35833590
stacktrace [FTRACE]
35843591
Enabled the stack tracer on boot up.
35853592

arch/arc/mm/mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
6464

6565
vma = find_vma(mm, addr);
6666
if (TASK_SIZE - len >= addr &&
67-
(!vma || addr + len <= vma->vm_start))
67+
(!vma || addr + len <= vm_start_gap(vma)))
6868
return addr;
6969
}
7070

arch/arm/mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
8989

9090
vma = find_vma(mm, addr);
9191
if (TASK_SIZE - len >= addr &&
92-
(!vma || addr + len <= vma->vm_start))
92+
(!vma || addr + len <= vm_start_gap(vma)))
9393
return addr;
9494
}
9595

@@ -140,7 +140,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
140140
addr = PAGE_ALIGN(addr);
141141
vma = find_vma(mm, addr);
142142
if (TASK_SIZE - len >= addr &&
143-
(!vma || addr + len <= vma->vm_start))
143+
(!vma || addr + len <= vm_start_gap(vma)))
144144
return addr;
145145
}
146146

arch/frv/mm/elf-fdpic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi
7474
addr = PAGE_ALIGN(addr);
7575
vma = find_vma(current->mm, addr);
7676
if (TASK_SIZE - len >= addr &&
77-
(!vma || addr + len <= vma->vm_start))
77+
(!vma || addr + len <= vm_start_gap(vma)))
7878
goto success;
7979
}
8080

arch/mips/mm/mmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static unsigned long arch_get_unmapped_area_common(struct file *filp,
9292

9393
vma = find_vma(mm, addr);
9494
if (TASK_SIZE - len >= addr &&
95-
(!vma || addr + len <= vma->vm_start))
95+
(!vma || addr + len <= vm_start_gap(vma)))
9696
return addr;
9797
}
9898

arch/parisc/kernel/sys_parisc.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
8888
unsigned long len, unsigned long pgoff, unsigned long flags)
8989
{
9090
struct mm_struct *mm = current->mm;
91-
struct vm_area_struct *vma;
91+
struct vm_area_struct *vma, *prev;
9292
unsigned long task_size = TASK_SIZE;
9393
int do_color_align, last_mmap;
9494
struct vm_unmapped_area_info info;
@@ -115,9 +115,10 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
115115
else
116116
addr = PAGE_ALIGN(addr);
117117

118-
vma = find_vma(mm, addr);
118+
vma = find_vma_prev(mm, addr, &prev);
119119
if (task_size - len >= addr &&
120-
(!vma || addr + len <= vma->vm_start))
120+
(!vma || addr + len <= vm_start_gap(vma)) &&
121+
(!prev || addr >= vm_end_gap(prev)))
121122
goto found_addr;
122123
}
123124

@@ -141,7 +142,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
141142
const unsigned long len, const unsigned long pgoff,
142143
const unsigned long flags)
143144
{
144-
struct vm_area_struct *vma;
145+
struct vm_area_struct *vma, *prev;
145146
struct mm_struct *mm = current->mm;
146147
unsigned long addr = addr0;
147148
int do_color_align, last_mmap;
@@ -175,9 +176,11 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
175176
addr = COLOR_ALIGN(addr, last_mmap, pgoff);
176177
else
177178
addr = PAGE_ALIGN(addr);
178-
vma = find_vma(mm, addr);
179+
180+
vma = find_vma_prev(mm, addr, &prev);
179181
if (TASK_SIZE - len >= addr &&
180-
(!vma || addr + len <= vma->vm_start))
182+
(!vma || addr + len <= vm_start_gap(vma)) &&
183+
(!prev || addr >= vm_end_gap(prev)))
181184
goto found_addr;
182185
}
183186

arch/powerpc/mm/slice.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
105105
if ((mm->task_size - len) < addr)
106106
return 0;
107107
vma = find_vma(mm, addr);
108-
return (!vma || (addr + len) <= vma->vm_start);
108+
return (!vma || (addr + len) <= vm_start_gap(vma));
109109
}
110110

111111
static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)

arch/s390/mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ arch_get_unmapped_area(struct file *filp, unsigned long addr,
9797
addr = PAGE_ALIGN(addr);
9898
vma = find_vma(mm, addr);
9999
if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
100-
(!vma || addr + len <= vma->vm_start))
100+
(!vma || addr + len <= vm_start_gap(vma)))
101101
return addr;
102102
}
103103

@@ -135,7 +135,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
135135
addr = PAGE_ALIGN(addr);
136136
vma = find_vma(mm, addr);
137137
if (TASK_SIZE - len >= addr && addr >= mmap_min_addr &&
138-
(!vma || addr + len <= vma->vm_start))
138+
(!vma || addr + len <= vm_start_gap(vma)))
139139
return addr;
140140
}
141141

arch/sh/mm/mmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr,
6363

6464
vma = find_vma(mm, addr);
6565
if (TASK_SIZE - len >= addr &&
66-
(!vma || addr + len <= vma->vm_start))
66+
(!vma || addr + len <= vm_start_gap(vma)))
6767
return addr;
6868
}
6969

@@ -113,7 +113,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
113113

114114
vma = find_vma(mm, addr);
115115
if (TASK_SIZE - len >= addr &&
116-
(!vma || addr + len <= vma->vm_start))
116+
(!vma || addr + len <= vm_start_gap(vma)))
117117
return addr;
118118
}
119119

arch/sparc/kernel/sys_sparc_64.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi
118118

119119
vma = find_vma(mm, addr);
120120
if (task_size - len >= addr &&
121-
(!vma || addr + len <= vma->vm_start))
121+
(!vma || addr + len <= vm_start_gap(vma)))
122122
return addr;
123123
}
124124

@@ -181,7 +181,7 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
181181

182182
vma = find_vma(mm, addr);
183183
if (task_size - len >= addr &&
184-
(!vma || addr + len <= vma->vm_start))
184+
(!vma || addr + len <= vm_start_gap(vma)))
185185
return addr;
186186
}
187187

0 commit comments

Comments
 (0)