1 Patch from https://bugzilla.redhat.com/show_bug.cgi?id=1157689
3 Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
5 WARNING !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!!
6 EMBARGOED !!! EMBARGOED !!! EMARGOED !!! EMBARGOED !!! EMBARGOED !!!
7 SECURITY !!! SECURITY !!! SECURITY !!! SECURITY !!! SECURITY !!!
11 The function wordexp() fails to properly handle the WRDE_NOCMD
12 flag when processing arithmetic inputs in the form of "$((... ``))"
13 where "..." can be anything valid. The backticks in the arithmetic
14 epxression are evaluated by in a shell even if WRDE_NOCMD forbade
15 command substitution. This allows an attacker to attempt to pass
16 dangerous commands via constructs of the above form, and bypass
17 the WRDE_NOCMD flag. This patch fixes this by checking for WRDE_NOCMD
18 in parse_arith(). The patch also hardens parse_backticks() and
19 parse_comm() to check for WRDE_NOCMD flag and return an error instead
20 of ever running a shell.
22 We expand the testsuite and add 3 new regression tests of roughtly
23 the same form but with a couple of nested levels.
25 On top of the 3 new tests we add fork validation to the WRDE_NOCMD
26 testing. If any forks are detected during the execution of a wordexp()
27 call with WRDE_NOCMD, the test is marked as failed. This is slightly
28 heuristic since vfork might be used, but it provides a higher level
29 of assurance that no shells were executed as part of command substitution
30 with WRDE_NOCMD in effect. In addition it doesn't require libpthread or
31 libdl, instead we use the public implementation namespace function
32 __register_atfork (already part of the public ABI for libpthread).
34 Tested on x86_64 with no regressions.
36 2014-10-27 Carlos O'Donell <carlos@redhat.com>
38 * wordexp-test.c (__dso_handle): Add prototype.
39 (__register_atfork): Likewise.
40 (__app_register_atfork): New function.
41 (registered_forks): New global.
42 (register_fork): New function.
43 (test_case): Add 3 new tests for WRDE_CMDSUB.
44 (main): Call __app_register_atfork.
45 (testit): If WRDE_NOCMD set registered_forks to zero, run test, and
46 if fork count is non-zero fail the test.
47 * posix/wordexp.c (parse_arith): Return WRDE_NOCMD if WRDE_NOCMD flag
48 is set and parsing '`'.
49 (parse_comm): Return WRDE_NOCMD if WRDE_NOCMD flag is set.
50 (parse_backtick): Return WRDE_NOCMD if WRDE_NOCMD flag is set and
53 diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
54 index 4957006..5ce2a1b 100644
55 --- a/posix/wordexp-test.c
56 +++ b/posix/wordexp-test.c
61 +extern void *__dso_handle __attribute__ ((__weak__, __visibility__ ("hidden")));
62 +extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *);
64 +static int __app_register_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void))
66 + return __register_atfork (prepare, parent, child,
67 + &__dso_handle == NULL ? NULL : __dso_handle);
70 +/* Number of forks seen. */
71 +static int registered_forks;
73 +/* For each fork increment the fork count. */
80 struct test_case_struct
83 @@ -206,6 +225,12 @@ struct test_case_struct
84 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
85 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
86 { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS },
87 + /* Test for CVE-2014-7817. We test 3 combinations of command
88 + substitution inside an arithmetic expression to make sure that
89 + no commands are executed and error is returned. */
90 + { WRDE_CMDSUB, NULL, "$((`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
91 + { WRDE_CMDSUB, NULL, "$((1+`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
92 + { WRDE_CMDSUB, NULL, "$((1+$((`echo 1`))))", WRDE_NOCMD, 0, { NULL, }, IFS },
94 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
96 @@ -258,6 +283,15 @@ main (int argc, char *argv[])
100 + /* If we are not allowed to do command substitution, we install
101 + fork handlers to verify that no forks happened. No forks should
102 + happen at all if command substitution is disabled. */
103 + if (__app_register_atfork (register_fork, NULL, NULL) != 0)
105 + printf ("Failed to register fork handler.\n");
109 for (test = 0; test_case[test].retval != -1; test++)
110 if (testit (&test_case[test]))
112 @@ -367,6 +401,9 @@ testit (struct test_case_struct *tc)
114 printf ("Test %d (%s): ", ++tests, tc->words);
116 + if (tc->flags & WRDE_NOCMD)
117 + registered_forks = 0;
119 if (tc->flags & WRDE_APPEND)
121 /* initial wordexp() call, to be appended to */
122 @@ -378,6 +415,13 @@ testit (struct test_case_struct *tc)
124 retval = wordexp (tc->words, &we, tc->flags);
126 + if ((tc->flags & WRDE_NOCMD)
127 + && (registered_forks > 0))
129 + printf ("FAILED fork called for WRDE_NOCMD\n");
133 if (tc->flags & WRDE_DOOFFS)
134 start_offs = sav_we.we_offs;
136 diff --git a/posix/wordexp.c b/posix/wordexp.c
137 index b6b65dd..d6a158f 100644
138 --- a/posix/wordexp.c
139 +++ b/posix/wordexp.c
140 @@ -693,6 +693,12 @@ parse_arith (char **word, size_t *word_length, size_t *max_length,
144 + if (flags & WRDE_NOCMD)
151 error = parse_backtick (&expr, &expr_length, &expr_maxlen,
152 words, offset, flags, NULL, NULL, NULL);
153 @@ -1144,6 +1150,10 @@ parse_comm (char **word, size_t *word_length, size_t *max_length,
155 char *comm = w_newword (&comm_length, &comm_maxlen);
157 + /* Do nothing if command substitution should not succeed. */
158 + if (flags & WRDE_NOCMD)
159 + return WRDE_CMDSUB;
161 for (; words[*offset]; ++(*offset))
163 switch (words[*offset])
164 @@ -2121,6 +2131,9 @@ parse_backtick (char **word, size_t *word_length, size_t *max_length,
165 switch (words[*offset])
168 + if (flags & WRDE_NOCMD)
171 /* Go -- give the script to the shell */
172 error = exec_comm (comm, word, word_length, max_length, flags,
173 pwordexp, ifs, ifs_white);