]> git.proxmox.com Git - pve-kernel.git/blame - CVE-2017-1000365-fs-exec.c-account-for-argv-envp-pointers.patch
add CVE fixes
[pve-kernel.git] / CVE-2017-1000365-fs-exec.c-account-for-argv-envp-pointers.patch
CommitLineData
4c390211
TL
1From 6b44f156198491839655077fc7f3a469c67e1f8d Mon Sep 17 00:00:00 2001
2From: Kees Cook <keescook@chromium.org>
3Date: Wed, 12 Jul 2017 15:30:39 +0200
4Subject: [PATCH 2/3] fs/exec.c: account for argv/envp pointers
5
6When limiting the argv/envp strings during exec to 1/4 of the stack limit,
7the storage of the pointers to the strings was not included. This means
8that an exec with huge numbers of tiny strings could eat 1/4 of the stack
9limit in strings and then additional space would be later used by the
10pointers to the strings.
11
12For example, on 32-bit with a 8MB stack rlimit, an exec with 1677721
13single-byte strings would consume less than 2MB of stack, the max (8MB /
144) amount allowed, but the pointers to the strings would consume the
15remaining additional stack space (1677721 * 4 == 6710884).
16
17The result (1677721 + 6710884 == 8388605) would exhaust stack space
18entirely. Controlling this stack exhaustion could result in
19pathological behavior in setuid binaries (CVE-2017-1000365).
20
21[akpm@linux-foundation.org: additional commenting from Kees]
22Fixes: b6a2fea39318 ("mm: variable length argument support")
23Link: http://lkml.kernel.org/r/20170622001720.GA32173@beast
24Signed-off-by: Kees Cook <keescook@chromium.org>
25Acked-by: Rik van Riel <riel@redhat.com>
26Acked-by: Michal Hocko <mhocko@suse.com>
27Cc: Alexander Viro <viro@zeniv.linux.org.uk>
28Cc: Qualys Security Advisory <qsa@qualys.com>
29Cc: <stable@vger.kernel.org>
30Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
31Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
32
33CVE-2017-1000365
34
35(cherry-picked from commit 98da7d08850fb8bdeb395d6368ed15753304aa0c)
36Signed-off-by: Stefan Bader <stefan.bader@canonical.com>
37Acked-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
38Acked-by: Seth Forshee <seth.forshee@canonical.com>
39Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@canonical.com>
40
41Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
42---
43 fs/exec.c | 28 ++++++++++++++++++++++++----
44 1 file changed, 24 insertions(+), 4 deletions(-)
45
46diff --git a/fs/exec.c b/fs/exec.c
47index 9fb90c37c37f..8cf76e2a0b83 100644
48--- a/fs/exec.c
49+++ b/fs/exec.c
50@@ -225,8 +225,26 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
51
52 if (write) {
53 unsigned long size = bprm->vma->vm_end - bprm->vma->vm_start;
54+ unsigned long ptr_size;
55 struct rlimit *rlim;
56
57+ /*
58+ * Since the stack will hold pointers to the strings, we
59+ * must account for them as well.
60+ *
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
67+ * correct size.
68+ */
69+ ptr_size = (bprm->argc + bprm->envc) * sizeof(void *);
70+ if (ptr_size > ULONG_MAX - size)
71+ goto fail;
72+ size += ptr_size;
73+
74 acct_arg_size(bprm, size / PAGE_SIZE);
75
76 /*
77@@ -244,13 +262,15 @@ static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
78 * to work from.
79 */
80 rlim = current->signal->rlim;
81- if (size > ACCESS_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4) {
82- put_page(page);
83- return NULL;
84- }
85+ if (size > READ_ONCE(rlim[RLIMIT_STACK].rlim_cur) / 4)
86+ goto fail;
87 }
88
89 return page;
90+
91+fail:
92+ put_page(page);
93+ return NULL;
94 }
95
96 static void put_arg_page(struct page *page)
97--
982.11.0
99