2 # commit a39208bd7fb76c1b01c127b4c61f9bfd915bfe7c
3 # Author: Carlos O'Donell <carlos@redhat.com>
4 # Date: Wed Nov 19 11:44:12 2014 -0500
6 # CVE-2014-7817: wordexp fails to honour WRDE_NOCMD.
8 # The function wordexp() fails to properly handle the WRDE_NOCMD
9 # flag when processing arithmetic inputs in the form of "$((... ``))"
10 # where "..." can be anything valid. The backticks in the arithmetic
11 # epxression are evaluated by in a shell even if WRDE_NOCMD forbade
12 # command substitution. This allows an attacker to attempt to pass
13 # dangerous commands via constructs of the above form, and bypass
14 # the WRDE_NOCMD flag. This patch fixes this by checking for WRDE_NOCMD
15 # in exec_comm(), the only place that can execute a shell. All other
16 # checks for WRDE_NOCMD are superfluous and removed.
18 # We expand the testsuite and add 3 new regression tests of roughly
19 # the same form but with a couple of nested levels.
21 # On top of the 3 new tests we add fork validation to the WRDE_NOCMD
22 # testing. If any forks are detected during the execution of a wordexp()
23 # call with WRDE_NOCMD, the test is marked as failed. This is slightly
24 # heuristic since vfork might be used in the future, but it provides a
25 # higher level of assurance that no shells were executed as part of
26 # command substitution with WRDE_NOCMD in effect. In addition it doesn't
27 # require libpthread or libdl, instead we use the public implementation
28 # namespace function __register_atfork (already part of the public ABI
31 # Tested on x86_64 with no regressions.
33 diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
34 index 4957006..bdd65e4 100644
35 --- a/posix/wordexp-test.c
36 +++ b/posix/wordexp-test.c
41 +extern void *__dso_handle __attribute__ ((__weak__, __visibility__ ("hidden")));
42 +extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *);
44 +static int __app_register_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void))
46 + return __register_atfork (prepare, parent, child,
47 + &__dso_handle == NULL ? NULL : __dso_handle);
50 +/* Number of forks seen. */
51 +static int registered_forks;
53 +/* For each fork increment the fork count. */
60 struct test_case_struct
63 @@ -206,6 +225,12 @@ struct test_case_struct
64 { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
65 { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
66 { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS },
67 + /* Test for CVE-2014-7817. We test 3 combinations of command
68 + substitution inside an arithmetic expression to make sure that
69 + no commands are executed and error is returned. */
70 + { WRDE_CMDSUB, NULL, "$((`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
71 + { WRDE_CMDSUB, NULL, "$((1+`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
72 + { WRDE_CMDSUB, NULL, "$((1+$((`echo 1`))))", WRDE_NOCMD, 0, { NULL, }, IFS },
74 { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
76 @@ -258,6 +283,15 @@ main (int argc, char *argv[])
80 + /* If we are not allowed to do command substitution, we install
81 + fork handlers to verify that no forks happened. No forks should
82 + happen at all if command substitution is disabled. */
83 + if (__app_register_atfork (register_fork, NULL, NULL) != 0)
85 + printf ("Failed to register fork handler.\n");
89 for (test = 0; test_case[test].retval != -1; test++)
90 if (testit (&test_case[test]))
92 @@ -367,6 +401,9 @@ testit (struct test_case_struct *tc)
94 printf ("Test %d (%s): ", ++tests, tc->words);
96 + if (tc->flags & WRDE_NOCMD)
97 + registered_forks = 0;
99 if (tc->flags & WRDE_APPEND)
101 /* initial wordexp() call, to be appended to */
102 @@ -378,6 +415,13 @@ testit (struct test_case_struct *tc)
104 retval = wordexp (tc->words, &we, tc->flags);
106 + if ((tc->flags & WRDE_NOCMD)
107 + && (registered_forks > 0))
109 + printf ("FAILED fork called for WRDE_NOCMD\n");
113 if (tc->flags & WRDE_DOOFFS)
114 start_offs = sav_we.we_offs;
116 diff --git a/posix/wordexp.c b/posix/wordexp.c
117 index b6b65dd..26f3a26 100644
118 --- a/posix/wordexp.c
119 +++ b/posix/wordexp.c
120 @@ -893,6 +893,10 @@ exec_comm (char *comm, char **word, size_t *word_length, size_t *max_length,
124 + /* Do nothing if command substitution should not succeed. */
125 + if (flags & WRDE_NOCMD)
126 + return WRDE_CMDSUB;
128 /* Don't fork() unless necessary */
131 @@ -2082,9 +2086,6 @@ parse_dollars (char **word, size_t *word_length, size_t *max_length,
135 - if (flags & WRDE_NOCMD)
136 - return WRDE_CMDSUB;
139 return parse_comm (word, word_length, max_length, words, offset, flags,
140 quoted? NULL : pwordexp, ifs, ifs_white);
141 @@ -2196,9 +2197,6 @@ parse_dquote (char **word, size_t *word_length, size_t *max_length,
145 - if (flags & WRDE_NOCMD)
146 - return WRDE_CMDSUB;
149 error = parse_backtick (word, word_length, max_length, words,
150 offset, flags, NULL, NULL, NULL);
151 @@ -2357,12 +2355,6 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
155 - if (flags & WRDE_NOCMD)
157 - error = WRDE_CMDSUB;
162 error = parse_backtick (&word, &word_length, &max_length, words,
163 &words_offset, flags, pwordexp, ifs,