Skip to content

Commit 86949eb

Browse files
keesgregkh
authored andcommitted
exec: Limit arg stack to at most 75% of _STK_LIM
commit da029c11e6b12f321f36dac8771e833b65cec962 upstream. To avoid pathological stack usage or the need to special-case setuid execs, just limit all arg stack usage to at most 75% of _STK_LIM (6MB). Signed-off-by: Kees Cook <keescook@chromium.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7888c02 commit 86949eb

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

fs/exec.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
206206

207207
if (write) {
208208
unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
209-
unsigned long ptr_size;
210-
struct rlimit *rlim;
209+
unsigned long ptr_size, limit;
211210

212211
/*
213212
* Since the stack will hold pointers to the strings, we
@@ -236,14 +235,16 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
236235
return page;
237236

238237
/*
239-
* Limit to 1/4-th the stack size for the argv+env strings.
238+
* Limit to 1/4 of the max stack size or 3/4 of _STK_LIM
239+
* (whichever is smaller) for the argv+env strings.
240240
* This ensures that:
241241
* - the remaining binfmt code will not run out of stack space,
242242
* - the program will have a reasonable amount of stack left
243243
* to work from.
244244
*/
245-
rlim = current->signal->rlim;
246-
if (size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4)
245+
limit = _STK_LIM / 4 * 3;
246+
limit = min(limit, rlimit(RLIMIT_STACK) / 4);
247+
if (size > limit)
247248
goto fail;
248249
}
249250

0 commit comments

Comments
 (0)