da2f49de10dbedde330a5e26e4e308c097ae8cbe
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / package / glibc / 2.19-svnr25243 / 0001-CVE-2014-7817-eglibc.patch
1 From https://bugzilla.redhat.com/show_bug.cgi?id=1157689
2 Modified for eglibc.
3
4 Signed-off-by: Gustavo Zacarias <gustavo@zacarias.com.ar>
5
6 WARNING !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!! WARNING !!!
7 EMBARGOED !!! EMBARGOED !!! EMARGOED !!! EMBARGOED !!! EMBARGOED !!!
8 SECURITY !!! SECURITY !!! SECURITY !!! SECURITY !!! SECURITY !!!
9
10 CVE-2014-7817:
11
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.
22
23 We expand the testsuite and add 3 new regression tests of roughtly
24 the same form but with a couple of nested levels. 
25
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).
34
35 Tested on x86_64 with no regressions.
36
37 2014-10-27  Carlos O'Donell  <carlos@redhat.com>
38
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
52         parsing '`'.
53
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
58 @@ -27,6 +27,25 @@
59  
60  #define IFS " \n\t"
61  
62 +extern void *__dso_handle __attribute__ ((__weak__, __visibility__ ("hidden")));
63 +extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *);
64 +
65 +static int __app_register_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void))
66 +{
67 +  return __register_atfork (prepare, parent, child,
68 +                           &__dso_handle == NULL ? NULL : __dso_handle);
69 +}
70 +
71 +/* Number of forks seen.  */
72 +static int registered_forks;
73 +
74 +/* For each fork increment the fork count.  */
75 +static void
76 +register_fork (void)
77 +{
78 +  registered_forks++;
79 +}
80 +
81  struct test_case_struct
82  {
83    int retval;
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 },
94  
95      { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
96    };
97 @@ -258,6 +283,15 @@ main (int argc, char *argv[])
98           return -1;
99      }
100  
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)
105 +    {
106 +      printf ("Failed to register fork handler.\n");
107 +      return -1;
108 +    }
109 +
110    for (test = 0; test_case[test].retval != -1; test++)
111      if (testit (&test_case[test]))
112        ++fail;
113 @@ -367,6 +401,9 @@ testit (struct test_case_struct *tc)
114  
115    printf ("Test %d (%s): ", ++tests, tc->words);
116  
117 +  if (tc->flags & WRDE_NOCMD)
118 +    registered_forks = 0;
119 +
120    if (tc->flags & WRDE_APPEND)
121      {
122        /* initial wordexp() call, to be appended to */
123 @@ -378,6 +415,13 @@ testit (struct test_case_struct *tc)
124      }
125    retval = wordexp (tc->words, &we, tc->flags);
126  
127 +  if ((tc->flags & WRDE_NOCMD)
128 +      && (registered_forks > 0))
129 +    {
130 +      printf ("FAILED fork called for WRDE_NOCMD\n");
131 +      return 1;
132 +    }
133 +
134    if (tc->flags & WRDE_DOOFFS)
135        start_offs = sav_we.we_offs;
136  
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,
142           break;
143  
144         case '`':
145 +          if (flags & WRDE_NOCMD)
146 +            {
147 +              free (expr);
148 +              return WRDE_NOCMD;
149 +            }
150 +
151           (*offset)++;
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,
155    size_t comm_maxlen;
156    char *comm = w_newword (&comm_length, &comm_maxlen);
157  
158 +  /* Do nothing if command substitution should not succeed.  */
159 +  if (flags & WRDE_NOCMD)
160 +    return WRDE_CMDSUB;
161 +
162    for (; words[*offset]; ++(*offset))
163      {
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])
167         {
168         case '`':
169 +         if (flags & WRDE_NOCMD)
170 +           return WRDE_NOCMD;
171 +
172           /* Go -- give the script to the shell */
173           error = exec_comm (comm, word, word_length, max_length, flags,
174                              pwordexp, ifs, ifs_white);