svashisht / rpms / bash

Forked from rpms/bash 4 years ago
Clone

Blame SOURCES/bash-4.2-cve-2014-7169-1.patch

ff19ae
--- ../bash-4.2-orig/variables.c	2014-09-25 13:07:59.313209541 +0200
ff19ae
+++ variables.c	2014-09-25 13:15:29.869420719 +0200
ff19ae
@@ -268,7 +268,7 @@
ff19ae
 static void propagate_temp_var __P((PTR_T));
ff19ae
 static void dispose_temporary_env __P((sh_free_func_t *));     
ff19ae
 
ff19ae
-static inline char *mk_env_string __P((const char *, const char *));
ff19ae
+static inline char *mk_env_string __P((const char *, const char *, int));
ff19ae
 static char **make_env_array_from_var_list __P((SHELL_VAR **));
ff19ae
 static char **make_var_export_array __P((VAR_CONTEXT *));
ff19ae
 static char **make_func_export_array __P((void));
ff19ae
@@ -301,6 +301,14 @@
ff19ae
 #endif
ff19ae
 }
ff19ae
 
ff19ae
+/* Prefix and suffix for environment variable names which contain
ff19ae
+   shell functions. */
ff19ae
+#define FUNCDEF_PREFIX "BASH_FUNC_"
ff19ae
+#define FUNCDEF_PREFIX_LEN (strlen (FUNCDEF_PREFIX))
ff19ae
+#define FUNCDEF_SUFFIX "()"
ff19ae
+#define FUNCDEF_SUFFIX_LEN (strlen (FUNCDEF_SUFFIX))
ff19ae
+
ff19ae
+
ff19ae
 /* Initialize the shell variables from the current environment.
ff19ae
    If PRIVMODE is nonzero, don't import functions from ENV or
ff19ae
    parse $SHELLOPTS. */
ff19ae
@@ -338,28 +346,40 @@
ff19ae
 
ff19ae
       /* If exported function, define it now.  Don't import functions from
ff19ae
 	 the environment in privileged mode. */
ff19ae
-      if (privmode == 0 && read_but_dont_execute == 0 && STREQN ("() {", string, 4))
ff19ae
-	{
ff19ae
-	  string_length = strlen (string);
ff19ae
-	  temp_string = (char *)xmalloc (3 + string_length + char_index);
ff19ae
+      if (privmode == 0 && read_but_dont_execute == 0
ff19ae
+	  && STREQN (FUNCDEF_PREFIX, name, FUNCDEF_PREFIX_LEN)
ff19ae
+	  && STREQ (name + char_index - FUNCDEF_SUFFIX_LEN, FUNCDEF_SUFFIX)
ff19ae
+	  && STREQN ("() {", string, 4))
ff19ae
+	{
ff19ae
+	  size_t name_length
ff19ae
+	    = char_index - (FUNCDEF_PREFIX_LEN + FUNCDEF_SUFFIX_LEN);
ff19ae
+	  char *temp_name = name + FUNCDEF_PREFIX_LEN;
ff19ae
+	  /* Temporarily remove the suffix. */
ff19ae
+	  temp_name[name_length] = '\0';
ff19ae
 
ff19ae
-	  strcpy (temp_string, name);
ff19ae
-	  temp_string[char_index] = ' ';
ff19ae
-	  strcpy (temp_string + char_index + 1, string);
ff19ae
+	  string_length = strlen (string);
ff19ae
+	  temp_string = (char *)xmalloc (name_length + 1 + string_length + 1);
ff19ae
+	  memcpy (temp_string, temp_name, name_length);
ff19ae
+	  temp_string[name_length] = ' ';
ff19ae
+	  memcpy (temp_string + name_length + 1, string, string_length + 1);
ff19ae
 
ff19ae
 	  /* Don't import function names that are invalid identifiers from the
ff19ae
 	     environment, though we still allow them to be defined as shell
ff19ae
	     variables. */
ff19ae
-	  if (legal_identifier (name))
ff19ae
-	    parse_and_execute (temp_string, name, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
ff19ae
+	  if (legal_identifier (temp_name))
ff19ae
+	    parse_and_execute (temp_string, temp_name,
ff19ae
+			       SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
ff19ae
 
ff19ae
-	  if (temp_var = find_function (name))
ff19ae
+	  if (temp_var = find_function (temp_name))
ff19ae
 	    {
ff19ae
 	      VSETATTR (temp_var, (att_exported|att_imported));
ff19ae
 	      array_needs_making = 1;
ff19ae
 	    }
ff19ae
 	  else
ff19ae
 	    report_error (_("error importing function definition for `%s'"), name);
ff19ae
+
ff19ae
+	  /* Restore the original suffix. */
ff19ae
+	  temp_name[name_length] = FUNCDEF_SUFFIX[0];
ff19ae
 	}
ff19ae
 #if defined (ARRAY_VARS)
ff19ae
 #  if 0
ff19ae
@@ -2537,7 +2557,7 @@
ff19ae
   var->context = variable_context;	/* XXX */
ff19ae
 
ff19ae
   INVALIDATE_EXPORTSTR (var);
ff19ae
-  var->exportstr = mk_env_string (name, value);
ff19ae
+  var->exportstr = mk_env_string (name, value, 0);
ff19ae
 
ff19ae
   array_needs_making = 1;
ff19ae
 
ff19ae
@@ -3388,22 +3408,43 @@
ff19ae
 /*								    */
ff19ae
 /* **************************************************************** */
ff19ae
 
ff19ae
+/* Returns the string NAME=VALUE if !FUNCTIONP or if VALUE == NULL (in
ff19ae
+   which case it is treated as empty).  Otherwise, decorate NAME with
ff19ae
+   FUNCDEF_PREFIX and FUNCDEF_SUFFIX, and return a string of the form
ff19ae
+   FUNCDEF_PREFIX NAME FUNCDEF_SUFFIX = VALUE (without spaces).  */
ff19ae
 static inline char *
ff19ae
-mk_env_string (name, value)
ff19ae
+mk_env_string (name, value, functionp)
ff19ae
      const char *name, *value;
ff19ae
+     int functionp;
ff19ae
 {
ff19ae
-  int name_len, value_len;
ff19ae
-  char	*p;
ff19ae
+  size_t name_len, value_len;
ff19ae
+  char *p, *q;
ff19ae
 
ff19ae
   name_len = strlen (name);
ff19ae
   value_len = STRLEN (value);
ff19ae
-  p = (char *)xmalloc (2 + name_len + value_len);
ff19ae
-  strcpy (p, name);
ff19ae
-  p[name_len] = '=';
ff19ae
+  if (functionp && value != NULL)
ff19ae
+    {
ff19ae
+      p = (char *)xmalloc (FUNCDEF_PREFIX_LEN + name_len + FUNCDEF_SUFFIX_LEN
ff19ae
+			   + 1 + value_len + 1);
ff19ae
+      q = p;
ff19ae
+      memcpy (q, FUNCDEF_PREFIX, FUNCDEF_PREFIX_LEN);
ff19ae
+      q += FUNCDEF_PREFIX_LEN;
ff19ae
+      memcpy (q, name, name_len);
ff19ae
+      q += name_len;
ff19ae
+      memcpy (q, FUNCDEF_SUFFIX, FUNCDEF_SUFFIX_LEN);
ff19ae
+      q += FUNCDEF_SUFFIX_LEN;
ff19ae
+    }
ff19ae
+  else
ff19ae
+    {
ff19ae
+      p = (char *)xmalloc (name_len + 1 + value_len + 1);
ff19ae
+      memcpy (p, name, name_len);
ff19ae
+      q = p + name_len;
ff19ae
+    }
ff19ae
+  q[0] = '=';
ff19ae
   if (value && *value)
ff19ae
-    strcpy (p + name_len + 1, value);
ff19ae
+    memcpy (q + 1, value, value_len + 1);
ff19ae
   else
ff19ae
-    p[name_len + 1] = '\0';
ff19ae
+    q[1] = '\0';
ff19ae
   return (p);
ff19ae
 }
ff19ae
 
ff19ae
@@ -3489,7 +3530,7 @@
ff19ae
 	  /* Gee, I'd like to get away with not using savestring() if we're
ff19ae
 	     using the cached exportstr... */
ff19ae
 	  list[list_index] = USE_EXPORTSTR ? savestring (value)
ff19ae
-					   : mk_env_string (var->name, value);
ff19ae
+	    : mk_env_string (var->name, value, function_p (var));
ff19ae
 
ff19ae
 	  if (USE_EXPORTSTR == 0)
ff19ae
 	    SAVE_EXPORTSTR (var, list[list_index]);