Blame SOURCES/glibc-rh1170118-CVE-2014-7817.patch

985194
#
985194
# commit a39208bd7fb76c1b01c127b4c61f9bfd915bfe7c
985194
# Author: Carlos O'Donell <carlos@redhat.com>
985194
# Date:   Wed Nov 19 11:44:12 2014 -0500
985194
# 
985194
#     CVE-2014-7817: wordexp fails to honour WRDE_NOCMD.
985194
#     
985194
#     The function wordexp() fails to properly handle the WRDE_NOCMD
985194
#     flag when processing arithmetic inputs in the form of "$((... ``))"
985194
#     where "..." can be anything valid. The backticks in the arithmetic
985194
#     epxression are evaluated by in a shell even if WRDE_NOCMD forbade
985194
#     command substitution. This allows an attacker to attempt to pass
985194
#     dangerous commands via constructs of the above form, and bypass
985194
#     the WRDE_NOCMD flag. This patch fixes this by checking for WRDE_NOCMD
985194
#     in exec_comm(), the only place that can execute a shell. All other
985194
#     checks for WRDE_NOCMD are superfluous and removed.
985194
#     
985194
#     We expand the testsuite and add 3 new regression tests of roughly
985194
#     the same form but with a couple of nested levels.
985194
#     
985194
#     On top of the 3 new tests we add fork validation to the WRDE_NOCMD
985194
#     testing. If any forks are detected during the execution of a wordexp()
985194
#     call with WRDE_NOCMD, the test is marked as failed. This is slightly
985194
#     heuristic since vfork might be used in the future, but it provides a
985194
#     higher level of assurance that no shells were executed as part of
985194
#     command substitution with WRDE_NOCMD in effect. In addition it doesn't
985194
#     require libpthread or libdl, instead we use the public implementation
985194
#     namespace function __register_atfork (already part of the public ABI
985194
#     for libpthread).
985194
#     
985194
#     Tested on x86_64 with no regressions.
985194
# 
12745e
diff --git glibc-2.17-c758a686/posix/wordexp-test.c glibc-2.17-c758a686/posix/wordexp-test.c
985194
index 4957006..bdd65e4 100644
12745e
--- glibc-2.17-c758a686/posix/wordexp-test.c
12745e
+++ glibc-2.17-c758a686/posix/wordexp-test.c
985194
@@ -27,6 +27,25 @@
985194
 
985194
 #define IFS " \n\t"
985194
 
985194
+extern void *__dso_handle __attribute__ ((__weak__, __visibility__ ("hidden")));
985194
+extern int __register_atfork (void (*) (void), void (*) (void), void (*) (void), void *);
985194
+
985194
+static int __app_register_atfork (void (*prepare) (void), void (*parent) (void), void (*child) (void))
985194
+{
985194
+  return __register_atfork (prepare, parent, child,
985194
+			    &__dso_handle == NULL ? NULL : __dso_handle);
985194
+}
985194
+
985194
+/* Number of forks seen.  */
985194
+static int registered_forks;
985194
+
985194
+/* For each fork increment the fork count.  */
985194
+static void
985194
+register_fork (void)
985194
+{
985194
+  registered_forks++;
985194
+}
985194
+
985194
 struct test_case_struct
985194
 {
985194
   int retval;
985194
@@ -206,6 +225,12 @@ struct test_case_struct
985194
     { WRDE_SYNTAX, NULL, "$((2+))", 0, 0, { NULL, }, IFS },
985194
     { WRDE_SYNTAX, NULL, "`", 0, 0, { NULL, }, IFS },
985194
     { WRDE_SYNTAX, NULL, "$((010+4+))", 0, 0, { NULL }, IFS },
985194
+    /* Test for CVE-2014-7817. We test 3 combinations of command
985194
+       substitution inside an arithmetic expression to make sure that
985194
+       no commands are executed and error is returned.  */
985194
+    { WRDE_CMDSUB, NULL, "$((`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
985194
+    { WRDE_CMDSUB, NULL, "$((1+`echo 1`))", WRDE_NOCMD, 0, { NULL, }, IFS },
985194
+    { WRDE_CMDSUB, NULL, "$((1+$((`echo 1`))))", WRDE_NOCMD, 0, { NULL, }, IFS },
985194
 
985194
     { -1, NULL, NULL, 0, 0, { NULL, }, IFS },
985194
   };
985194
@@ -258,6 +283,15 @@ main (int argc, char *argv[])
985194
 	  return -1;
985194
     }
985194
 
985194
+  /* If we are not allowed to do command substitution, we install
985194
+     fork handlers to verify that no forks happened.  No forks should
985194
+     happen at all if command substitution is disabled.  */
985194
+  if (__app_register_atfork (register_fork, NULL, NULL) != 0)
985194
+    {
985194
+      printf ("Failed to register fork handler.\n");
985194
+      return -1;
985194
+    }
985194
+
985194
   for (test = 0; test_case[test].retval != -1; test++)
985194
     if (testit (&test_case[test]))
985194
       ++fail;
985194
@@ -367,6 +401,9 @@ testit (struct test_case_struct *tc)
985194
 
985194
   printf ("Test %d (%s): ", ++tests, tc->words);
985194
 
985194
+  if (tc->flags & WRDE_NOCMD)
985194
+    registered_forks = 0;
985194
+
985194
   if (tc->flags & WRDE_APPEND)
985194
     {
985194
       /* initial wordexp() call, to be appended to */
985194
@@ -378,6 +415,13 @@ testit (struct test_case_struct *tc)
985194
     }
985194
   retval = wordexp (tc->words, &we, tc->flags);
985194
 
985194
+  if ((tc->flags & WRDE_NOCMD)
985194
+      && (registered_forks > 0))
985194
+    {
985194
+	  printf ("FAILED fork called for WRDE_NOCMD\n");
985194
+	  return 1;
985194
+    }
985194
+
985194
   if (tc->flags & WRDE_DOOFFS)
985194
       start_offs = sav_we.we_offs;
985194
 
12745e
diff --git glibc-2.17-c758a686/posix/wordexp.c glibc-2.17-c758a686/posix/wordexp.c
985194
index b6b65dd..26f3a26 100644
12745e
--- glibc-2.17-c758a686/posix/wordexp.c
12745e
+++ glibc-2.17-c758a686/posix/wordexp.c
985194
@@ -893,6 +893,10 @@ exec_comm (char *comm, char **word, size_t *word_length, size_t *max_length,
985194
   pid_t pid;
985194
   int noexec = 0;
985194
 
985194
+  /* Do nothing if command substitution should not succeed.  */
985194
+  if (flags & WRDE_NOCMD)
985194
+    return WRDE_CMDSUB;
985194
+
985194
   /* Don't fork() unless necessary */
985194
   if (!comm || !*comm)
985194
     return 0;
985194
@@ -2082,9 +2086,6 @@ parse_dollars (char **word, size_t *word_length, size_t *max_length,
985194
 	    }
985194
 	}
985194
 
985194
-      if (flags & WRDE_NOCMD)
985194
-	return WRDE_CMDSUB;
985194
-
985194
       (*offset) += 2;
985194
       return parse_comm (word, word_length, max_length, words, offset, flags,
985194
 			 quoted? NULL : pwordexp, ifs, ifs_white);
985194
@@ -2196,9 +2197,6 @@ parse_dquote (char **word, size_t *word_length, size_t *max_length,
985194
 	  break;
985194
 
985194
 	case '`':
985194
-	  if (flags & WRDE_NOCMD)
985194
-	    return WRDE_CMDSUB;
985194
-
985194
 	  ++(*offset);
985194
 	  error = parse_backtick (word, word_length, max_length, words,
985194
 				  offset, flags, NULL, NULL, NULL);
985194
@@ -2357,12 +2355,6 @@ wordexp (const char *words, wordexp_t *pwordexp, int flags)
985194
 	break;
985194
 
985194
       case '`':
985194
-	if (flags & WRDE_NOCMD)
985194
-	  {
985194
-	    error = WRDE_CMDSUB;
985194
-	    goto do_error;
985194
-	  }
985194
-
985194
 	++words_offset;
985194
 	error = parse_backtick (&word, &word_length, &max_length, words,
985194
 				&words_offset, flags, pwordexp, ifs,