arrfab / rpms / glibc

Forked from rpms/glibc 5 years ago
Clone

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

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