Commit 803bf5ec authored by Michael Neuling's avatar Michael Neuling Committed by Linus Torvalds

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 b6a2fea3 ("mm:
variable length argument support") and unmasked by
fc63cf23 ("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.

[ cleanup]
[ cleanup cleanup]
Signed-off-by: default avatarMichael Neuling <>
Signed-off-by: default avatarKOSAKI Motohiro <>
Cc: Americo Wang <>
Cc: Anton Blanchard <>
Cc: Oleg Nesterov <>
Cc: James Morris <>
Cc: Ingo Molnar <>
Cc: Serge Hallyn <>
Cc: Benjamin Herrenschmidt <>
Cc: <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent 4cfbafd3
......@@ -571,6 +571,9 @@ int setup_arg_pages(struct linux_binprm *bprm,
struct vm_area_struct *prev = NULL;
unsigned long vm_flags;
unsigned long stack_base;
unsigned long stack_size;
unsigned long stack_expand;
unsigned long rlim_stack;
/* Limit stack size to 1GB */
......@@ -627,10 +630,24 @@ int setup_arg_pages(struct linux_binprm *bprm,
goto out_unlock;
stack_size = vma->vm_end - vma->vm_start;
* Align this down to a page boundary as expand_stack
* will align it up.
rlim_stack = rlimit(RLIMIT_STACK) & PAGE_MASK;
rlim_stack = min(rlim_stack, stack_size);
stack_base = vma->vm_end + EXTRA_STACK_VM_PAGES * PAGE_SIZE;
if (stack_size + stack_expand > rlim_stack)
stack_base = vma->vm_start + rlim_stack;
stack_base = vma->vm_end + stack_expand;
stack_base = vma->vm_start - EXTRA_STACK_VM_PAGES * PAGE_SIZE;
if (stack_size + stack_expand > rlim_stack)
stack_base = vma->vm_end - rlim_stack;
stack_base = vma->vm_start - stack_expand;
ret = expand_stack(vma, stack_base);
if (ret)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment