rcolebaugh / rpms / bash

Forked from rpms/bash 2 years ago
Clone

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

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