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

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