1 From 6b44f156198491839655077fc7f3a469c67e1f8d Mon Sep 17 00:00:00 2001
2 From: Kees Cook <keescook@chromium.org>
3 Date: Wed, 12 Jul 2017 15:30:39 +0200
4 Subject: [PATCH 2/3] fs/exec.c: account for argv/envp pointers
6 When limiting the argv/envp strings during exec to 1/4 of the stack limit,
7 the storage of the pointers to the strings was not included. This means
8 that an exec with huge numbers of tiny strings could eat 1/4 of the stack
9 limit in strings and then additional space would be later used by the
10 pointers to the strings.
12 For example, on 32-bit with a 8MB stack rlimit, an exec with 1677721
13 single-byte strings would consume less than 2MB of stack, the max (8MB /
14 4) amount allowed, but the pointers to the strings would consume the
15 remaining additional stack space (1677721 * 4 == 6710884).
17 The result (1677721 + 6710884 == 8388605) would exhaust stack space
18 entirely. Controlling this stack exhaustion could result in
19 pathological behavior in setuid binaries (CVE-2017-1000365).
21 [akpm@linux-foundation.org: additional commenting from Kees]
22 Fixes: b6a2fea39318 ("mm: variable length argument support")
23 Link: http://lkml.kernel.org/r/20170622001720.GA32173@beast
24 Signed-off-by: Kees Cook <keescook@chromium.org>
25 Acked-by: Rik van Riel <riel@redhat.com>
26 Acked-by: Michal Hocko <mhocko@suse.com>
27 Cc: Alexander Viro <viro@zeniv.linux.org.uk>
28 Cc: Qualys Security Advisory <qsa@qualys.com>
29 Cc: <stable@vger.kernel.org>
30 Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
31 Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
35 (cherry-picked from commit 98da7d08850fb8bdeb395d6368ed15753304aa0c)
36 Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
37 Acked-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
38 Acked-by: Seth Forshee <seth.forshee@canonical.com>
39 Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
41 Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
43 fs/exec.c | 28 ++++++++++++++++++++++++----
44 1 file changed, 24 insertions(+), 4 deletions(-)
46 diff --git a/fs/exec.c b/fs/exec.c
47 index 9fb90c37c37f..8cf76e2a0b83 100644
50 @@ -225,8 +225,26 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
53 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
54 + unsigned long ptr_size;
58 + * Since the stack will hold pointers to the strings, we
59 + * must account for them as well.
61 + * The size calculation is the entire vma while each arg page is
62 + * built, so each time we get here it's calculating how far it
63 + * is currently (rather than each call being just the newly
64 + * added size from the arg page). As a result, we need to
65 + * always add the entire size of the pointers, so that on the
66 + * last call to get_arg_page() we'll actually have the entire
69 + ptr_size = (bprm->argc + bprm->envc) * sizeof(void *);
70 + if (ptr_size > ULONG_MAX - size)
74 acct_arg_size(bprm, size / PAGE_SIZE);
77 @@ -244,13 +262,15 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
80 rlim = current->signal->rlim;
81 - if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) {
85 + if (size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4)
96 static void put_arg_page(struct page *page)