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