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