fs/exec.c: restrict initial stack space expansion to rlimit

When reserving stack space for a new process, make sure we're not
attempting to expand the stack by more than rlimit allows.

This fixes a bug caused by b6a2fea39318e43fee84fa7b0b90d68bed92d2ba ("mm:
variable length argument support") and unmasked by
fc63cf237078c86214abcb2ee9926d8ad289da9b ("exec: setup_arg_pages() fails
to return errors").

This bug means that when limiting the stack to less the 20*PAGE_SIZE (eg.
80K on 4K pages or 'ulimit -s 79') all processes will be killed before
they start. This is particularly bad with 64K pages, where a ulimit below
1280K will kill every process.

To test, do:

'ulimit -s 15; ls'

before and after the patch is applied. Before it's applied, 'ls' should
be killed. After the patch is applied, 'ls' should no longer be killed.

A stack limit of 15KB since it's small enough to trigger 20*PAGE_SIZE.
Also 15KB not a multiple of PAGE_SIZE, which is a trickier case to handle
correctly with this code.

4K pages should be fine to test with.

[kosaki.motohiro@jp.fujitsu.com: cleanup]
[akpm@linux-foundation.org: cleanup cleanup]
Signed-off-by: Michael Neuling <mikey@neuling.org>
Signed-off-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
Cc: Americo Wang <xiyou.wangcong@gmail.com>
Cc: Anton Blanchard <anton@samba.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: James Morris <jmorris@namei.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Serge Hallyn <serue@us.ibm.com>
Cc: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Cc: <stable@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Michael Neuling and committed by
Linus Torvalds
803bf5ec 4cfbafd3

+19 -2
+19 -2
fs/exec.c
··· 571 struct vm_area_struct *prev = NULL; 572 unsigned long vm_flags; 573 unsigned long stack_base; 574 575 #ifdef CONFIG_STACK_GROWSUP 576 /* Limit stack size to 1GB */ ··· 630 goto out_unlock; 631 } 632 633 #ifdef CONFIG_STACK_GROWSUP 634 - stack_base = vma->vm_end + EXTRA_STACK_VM_PAGES * PAGE_SIZE; 635 #else 636 - stack_base = vma->vm_start - EXTRA_STACK_VM_PAGES * PAGE_SIZE; 637 #endif 638 ret = expand_stack(vma, stack_base); 639 if (ret)
··· 571 struct vm_area_struct *prev = NULL; 572 unsigned long vm_flags; 573 unsigned long stack_base; 574 + unsigned long stack_size; 575 + unsigned long stack_expand; 576 + unsigned long rlim_stack; 577 578 #ifdef CONFIG_STACK_GROWSUP 579 /* Limit stack size to 1GB */ ··· 627 goto out_unlock; 628 } 629 630 + stack_expand = EXTRA_STACK_VM_PAGES * PAGE_SIZE; 631 + stack_size = vma->vm_end - vma->vm_start; 632 + /* 633 + * Align this down to a page boundary as expand_stack 634 + * will align it up. 635 + */ 636 + rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK; 637 + rlim_stack = min(rlim_stack, stack_size); 638 #ifdef CONFIG_STACK_GROWSUP 639 + if (stack_size + stack_expand > rlim_stack) 640 + stack_base = vma->vm_start + rlim_stack; 641 + else 642 + stack_base = vma->vm_end + stack_expand; 643 #else 644 + if (stack_size + stack_expand > rlim_stack) 645 + stack_base = vma->vm_end - rlim_stack; 646 + else 647 + stack_base = vma->vm_start - stack_expand; 648 #endif 649 ret = expand_stack(vma, stack_base); 650 if (ret)