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