diff options
author | Linus Torvalds <torvalds@linux-foundation.org> | 2013-02-27 11:36:04 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2013-02-27 11:36:04 -0500 |
commit | 09884964335e85e897876d17783c2ad33cf8a2e0 (patch) | |
tree | 4cf0ea71bb20c8d81147614e90e53df3368405e5 /mm/mmap.c | |
parent | d895cb1af15c04c522a25c79cc429076987c089b (diff) |
mm: do not grow the stack vma just because of an overrun on preceding vma
The stack vma is designed to grow automatically (marked with VM_GROWSUP
or VM_GROWSDOWN depending on architecture) when an access is made beyond
the existing boundary. However, particularly if you have not limited
your stack at all ("ulimit -s unlimited"), this can cause the stack to
grow even if the access was really just one past *another* segment.
And that's wrong, especially since we first grow the segment, but then
immediately later enforce the stack guard page on the last page of the
segment. So _despite_ first growing the stack segment as a result of
the access, the kernel will then make the access cause a SIGSEGV anyway!
So do the same logic as the guard page check does, and consider an
access to within one page of the next segment to be a bad access, rather
than growing the stack to abut the next segment.
Reported-and-tested-by: Heiko Carstens <heiko.carstens@de.ibm.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'mm/mmap.c')
-rw-r--r-- | mm/mmap.c | 27 |
1 files changed, 27 insertions, 0 deletions
@@ -2185,9 +2185,28 @@ int expand_downwards(struct vm_area_struct *vma, | |||
2185 | return error; | 2185 | return error; |
2186 | } | 2186 | } |
2187 | 2187 | ||
2188 | /* | ||
2189 | * Note how expand_stack() refuses to expand the stack all the way to | ||
2190 | * abut the next virtual mapping, *unless* that mapping itself is also | ||
2191 | * a stack mapping. We want to leave room for a guard page, after all | ||
2192 | * (the guard page itself is not added here, that is done by the | ||
2193 | * actual page faulting logic) | ||
2194 | * | ||
2195 | * This matches the behavior of the guard page logic (see mm/memory.c: | ||
2196 | * check_stack_guard_page()), which only allows the guard page to be | ||
2197 | * removed under these circumstances. | ||
2198 | */ | ||
2188 | #ifdef CONFIG_STACK_GROWSUP | 2199 | #ifdef CONFIG_STACK_GROWSUP |
2189 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | 2200 | int expand_stack(struct vm_area_struct *vma, unsigned long address) |
2190 | { | 2201 | { |
2202 | struct vm_area_struct *next; | ||
2203 | |||
2204 | address &= PAGE_MASK; | ||
2205 | next = vma->vm_next; | ||
2206 | if (next && next->vm_start == address + PAGE_SIZE) { | ||
2207 | if (!(next->vm_flags & VM_GROWSUP)) | ||
2208 | return -ENOMEM; | ||
2209 | } | ||
2191 | return expand_upwards(vma, address); | 2210 | return expand_upwards(vma, address); |
2192 | } | 2211 | } |
2193 | 2212 | ||
@@ -2209,6 +2228,14 @@ find_extend_vma(struct mm_struct *mm, unsigned long addr) | |||
2209 | #else | 2228 | #else |
2210 | int expand_stack(struct vm_area_struct *vma, unsigned long address) | 2229 | int expand_stack(struct vm_area_struct *vma, unsigned long address) |
2211 | { | 2230 | { |
2231 | struct vm_area_struct *prev; | ||
2232 | |||
2233 | address &= PAGE_MASK; | ||
2234 | prev = vma->vm_prev; | ||
2235 | if (prev && prev->vm_end == address) { | ||
2236 | if (!(prev->vm_flags & VM_GROWSDOWN)) | ||
2237 | return -ENOMEM; | ||
2238 | } | ||
2212 | return expand_downwards(vma, address); | 2239 | return expand_downwards(vma, address); |
2213 | } | 2240 | } |
2214 | 2241 | ||