Blame SOURCES/binutils-libiberty-demangler.patch

43c6b8
diff -rup binutils.orig/include/demangle.h binutils-2.27/include/demangle.h
43c6b8
--- binutils.orig/include/demangle.h	2019-01-08 13:52:32.990714670 +0000
43c6b8
+++ binutils-2.27/include/demangle.h	2019-01-08 13:58:42.269743403 +0000
43c6b8
@@ -64,9 +64,20 @@ extern "C" {
43c6b8
 #define DMGL_GNAT	 (1 << 15)
43c6b8
 #define DMGL_DLANG	 (1 << 16)
43c6b8
 
43c6b8
+/* Disable a limit on the depth of recursion in mangled strings.
43c6b8
+   Note if this limit is disabled then stack exhaustion is possible when
43c6b8
+   demangling pathologically complicated strings.  Bug reports about stack
43c6b8
+   exhaustion when the option is enabled will be rejected.  */  
43c6b8
+#define DMGL_NO_RECURSE_LIMIT (1 << 17)	
43c6b8
+
43c6b8
 /* If none of these are set, use 'current_demangling_style' as the default. */
43c6b8
 #define DMGL_STYLE_MASK (DMGL_AUTO|DMGL_GNU|DMGL_LUCID|DMGL_ARM|DMGL_HP|DMGL_EDG|DMGL_GNU_V3|DMGL_JAVA|DMGL_GNAT|DMGL_DLANG)
43c6b8
 
43c6b8
+/* If DMGL_NO_RECURSE_LIMIT is not enabled, then this is the value used as
43c6b8
+   the maximum depth of recursion allowed.  It should be enough for any
43c6b8
+   real-world mangled name.  */
43c6b8
+#define DEMANGLE_RECURSION_LIMIT 2048
43c6b8
+  
43c6b8
 /* Enumeration of possible demangling styles.
43c6b8
 
43c6b8
    Lucid and ARM styles are still kept logically distinct, even though
43c6b8
Only in binutils-2.27/include: demangle.h.orig
43c6b8
Only in binutils-2.27/include: #demangle.h.rej#
43c6b8
Only in binutils-2.27/include: .#demangle.h.rej
43c6b8
Only in binutils-2.27/include: demangle.h.rej
43c6b8
diff -rup binutils.orig/libiberty/cp-demangle.c binutils-2.27/libiberty/cp-demangle.c
43c6b8
--- binutils.orig/libiberty/cp-demangle.c	2019-01-08 13:52:33.161713294 +0000
43c6b8
+++ binutils-2.27/libiberty/cp-demangle.c	2019-01-08 13:53:30.002255949 +0000
43c6b8
@@ -2725,21 +2725,35 @@ d_ref_qualifier (struct d_info *di, stru
43c6b8
 static struct demangle_component *
43c6b8
 d_function_type (struct d_info *di)
43c6b8
 {
43c6b8
-  struct demangle_component *ret;
43c6b8
+  struct demangle_component *ret = NULL;
43c6b8
 
43c6b8
-  if (! d_check_char (di, 'F'))
43c6b8
-    return NULL;
43c6b8
-  if (d_peek_char (di) == 'Y')
43c6b8
+  if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
43c6b8
     {
43c6b8
-      /* Function has C linkage.  We don't print this information.
43c6b8
-	 FIXME: We should print it in verbose mode.  */
43c6b8
-      d_advance (di, 1);
43c6b8
+      if (di->recursion_level > DEMANGLE_RECURSION_LIMIT)
43c6b8
+	/* FIXME: There ought to be a way to report
43c6b8
+	   that the recursion limit has been reached.  */
43c6b8
+	return NULL;
43c6b8
+
43c6b8
+      di->recursion_level ++;
43c6b8
     }
43c6b8
-  ret = d_bare_function_type (di, 1);
43c6b8
-  ret = d_ref_qualifier (di, ret);
43c6b8
 
43c6b8
-  if (! d_check_char (di, 'E'))
43c6b8
-    return NULL;
43c6b8
+  if (d_check_char (di, 'F'))
43c6b8
+    {
43c6b8
+      if (d_peek_char (di) == 'Y')
43c6b8
+	{
43c6b8
+	  /* Function has C linkage.  We don't print this information.
43c6b8
+	     FIXME: We should print it in verbose mode.  */
43c6b8
+	  d_advance (di, 1);
43c6b8
+	}
43c6b8
+      ret = d_bare_function_type (di, 1);
43c6b8
+      ret = d_ref_qualifier (di, ret);
43c6b8
+      
43c6b8
+      if (! d_check_char (di, 'E'))
43c6b8
+	ret = NULL;
43c6b8
+    }
43c6b8
+
43c6b8
+  if ((di->options & DMGL_NO_RECURSE_LIMIT) == 0)
43c6b8
+    di->recursion_level --;
43c6b8
   return ret;
43c6b8
 }
43c6b8
 
43c6b8
@@ -5898,6 +5912,7 @@ cplus_demangle_init_info (const char *ma
43c6b8
   di->expansion = 0;
43c6b8
   di->is_expression = 0;
43c6b8
   di->is_conversion = 0;
43c6b8
+  di->recursion_level = 0;
43c6b8
 }
43c6b8
 
43c6b8
 /* Internal implementation for the demangler.  If MANGLED is a g++ v3 ABI
43c6b8
@@ -5937,6 +5952,20 @@ d_demangle_callback (const char *mangled
43c6b8
 
43c6b8
   cplus_demangle_init_info (mangled, options, strlen (mangled), &di);
43c6b8
 
43c6b8
+  /* PR 87675 - Check for a mangled string that is so long
43c6b8
+     that we do not have enough stack space to demangle it.  */
43c6b8
+  if (((options & DMGL_NO_RECURSE_LIMIT) == 0)
43c6b8
+      /* This check is a bit arbitrary, since what we really want to do is to
43c6b8
+	 compare the sizes of the di.comps and di.subs arrays against the
43c6b8
+	 amount of stack space remaining.  But there is no portable way to do
43c6b8
+	 this, so instead we use the recursion limit as a guide to the maximum
43c6b8
+	 size of the arrays.  */
43c6b8
+      && (unsigned long) di.num_comps > DEMANGLE_RECURSION_LIMIT)
43c6b8
+    {
43c6b8
+      /* FIXME: We need a way to indicate that a stack limit has been reached.  */
43c6b8
+      return 0;
43c6b8
+    }
43c6b8
+
43c6b8
   {
43c6b8
 #ifdef CP_DYNAMIC_ARRAYS
43c6b8
     __extension__ struct demangle_component comps[di.num_comps];
43c6b8
Only in binutils-2.27/libiberty: cp-demangle.c.orig
43c6b8
diff -rup binutils.orig/libiberty/cp-demangle.h binutils-2.27/libiberty/cp-demangle.h
43c6b8
--- binutils.orig/libiberty/cp-demangle.h	2019-01-08 13:52:33.161713294 +0000
43c6b8
+++ binutils-2.27/libiberty/cp-demangle.h	2019-01-08 13:53:30.003255941 +0000
43c6b8
@@ -127,6 +127,9 @@ struct d_info
43c6b8
   /* Non-zero if we are parsing the type operand of a conversion
43c6b8
      operator, but not when in an expression.  */
43c6b8
   int is_conversion;
43c6b8
+  /* If DMGL_NO_RECURSE_LIMIT is not active then this is set to
43c6b8
+     the current recursion level.  */
43c6b8
+  unsigned int recursion_level;
43c6b8
 };
43c6b8
 
43c6b8
 /* To avoid running past the ending '\0', don't:
43c6b8
Only in binutils-2.27/libiberty: cp-demangle.h.orig
43c6b8
diff -rup binutils.orig/libiberty/cplus-dem.c binutils-2.27/libiberty/cplus-dem.c
43c6b8
--- binutils.orig/libiberty/cplus-dem.c	2019-01-08 13:52:33.161713294 +0000
43c6b8
+++ binutils-2.27/libiberty/cplus-dem.c	2019-01-08 14:10:00.057340821 +0000
43c6b8
@@ -137,6 +137,7 @@ struct work_stuff
43c6b8
   string* previous_argument; /* The last function argument demangled.  */
43c6b8
   int nrepeats;         /* The number of times to repeat the previous
43c6b8
 			   argument.  */
43c6b8
+  unsigned int recursion_level;
43c6b8
 };
43c6b8
 
43c6b8
 #define PRINT_ANSI_QUALIFIERS (work -> options & DMGL_ANSI)
43c6b8
@@ -1237,11 +1238,15 @@ squangle_mop_up (struct work_stuff *work
43c6b8
     {
43c6b8
       free ((char *) work -> btypevec);
43c6b8
       work->btypevec = NULL;
43c6b8
+      work->bsize = 0;
43c6b8
+      work->numb = 0;
43c6b8
     }
43c6b8
   if (work -> ktypevec != NULL)
43c6b8
     {
43c6b8
       free ((char *) work -> ktypevec);
43c6b8
       work->ktypevec = NULL;
43c6b8
+      work->ksize = 0;
43c6b8
+      work->numk = 0;
43c6b8
     }
43c6b8
 }
43c6b8
 
43c6b8
@@ -1275,8 +1280,15 @@ work_stuff_copy_to_from (struct work_stu
43c6b8
 
43c6b8
   for (i = 0; i < from->numk; i++)
43c6b8
     {
43c6b8
-      int len = strlen (from->ktypevec[i]) + 1;
43c6b8
+      int len;
43c6b8
 
43c6b8
+      if (from->ktypevec[i] == NULL)
43c6b8
+	{
43c6b8
+	  to->ktypevec[i] = NULL;
43c6b8
+	  continue;
43c6b8
+	}
43c6b8
+
43c6b8
+      len = strlen (from->ktypevec[i]) + 1;
43c6b8
       to->ktypevec[i] = XNEWVEC (char, len);
43c6b8
       memcpy (to->ktypevec[i], from->ktypevec[i], len);
43c6b8
     }
43c6b8
@@ -1286,8 +1298,15 @@ work_stuff_copy_to_from (struct work_stu
43c6b8
 
43c6b8
   for (i = 0; i < from->numb; i++)
43c6b8
     {
43c6b8
-      int len = strlen (from->btypevec[i]) + 1;
43c6b8
+      int len;
43c6b8
 
43c6b8
+      if (from->btypevec[i] == NULL)
43c6b8
+	{
43c6b8
+	  to->btypevec[i] = NULL;
43c6b8
+	  continue;
43c6b8
+	}
43c6b8
+
43c6b8
+      len = strlen (from->btypevec[i]) + 1;
43c6b8
       to->btypevec[i] = XNEWVEC (char , len);
43c6b8
       memcpy (to->btypevec[i], from->btypevec[i], len);
43c6b8
     }
43c6b8
@@ -1335,6 +1354,7 @@ delete_non_B_K_work_stuff (struct work_s
43c6b8
 
43c6b8
       free ((char*) work->tmpl_argvec);
43c6b8
       work->tmpl_argvec = NULL;
43c6b8
+      work->ntmpl_args = 0;
43c6b8
     }
43c6b8
   if (work->previous_argument)
43c6b8
     {
43c6b8
@@ -3347,6 +3367,20 @@ demangle_qualified (struct work_stuff *w
43c6b8
       success = 0;
43c6b8
     }
43c6b8
 
43c6b8
+  if ((work->options & DMGL_NO_RECURSE_LIMIT) == 0)
43c6b8
+    {
43c6b8
+      /* PR 87241: Catch malicious input that will try to trick this code into
43c6b8
+	 allocating a ridiculous amount of memory via the remember_Ktype()
43c6b8
+	 function.
43c6b8
+	 The choice of DEMANGLE_RECURSION_LIMIT is somewhat arbitrary.  Possibly
43c6b8
+	 a better solution would be to track how much memory remember_Ktype
43c6b8
+	 allocates and abort when some upper limit is reached.  */
43c6b8
+      if (qualifiers > DEMANGLE_RECURSION_LIMIT)
43c6b8
+	/* FIXME: We ought to have some way to tell the user that
43c6b8
+	   this limit has been reached.  */
43c6b8
+	success = 0;
43c6b8
+    }
43c6b8
+
43c6b8
   if (!success)
43c6b8
     return success;
43c6b8
 
43c6b8
@@ -4335,6 +4369,7 @@ remember_Btype (struct work_stuff *work,
43c6b8
 }
43c6b8
 
43c6b8
 /* Lose all the info related to B and K type codes. */
43c6b8
+
43c6b8
 static void
43c6b8
 forget_B_and_K_types (struct work_stuff *work)
43c6b8
 {
43c6b8
@@ -4360,6 +4395,7 @@ forget_B_and_K_types (struct work_stuff
43c6b8
 	}
43c6b8
     }
43c6b8
 }
43c6b8
+
43c6b8
 /* Forget the remembered types, but not the type vector itself.  */
43c6b8
 
43c6b8
 static void
43c6b8
@@ -4551,6 +4587,16 @@ demangle_nested_args (struct work_stuff
43c6b8
   int result;
43c6b8
   int saved_nrepeats;
43c6b8
 
43c6b8
+  if ((work->options & DMGL_NO_RECURSE_LIMIT) == 0)
43c6b8
+    {
43c6b8
+      if (work->recursion_level > DEMANGLE_RECURSION_LIMIT)
43c6b8
+	/* FIXME: There ought to be a way to report
43c6b8
+	   that the recursion limit has been reached.  */
43c6b8
+	return 0;
43c6b8
+
43c6b8
+      work->recursion_level ++;
43c6b8
+    }
43c6b8
+
43c6b8
   /* The G++ name-mangling algorithm does not remember types on nested
43c6b8
      argument lists, unless -fsquangling is used, and in that case the
43c6b8
      type vector updated by remember_type is not used.  So, we turn
43c6b8
@@ -4577,6 +4623,9 @@ demangle_nested_args (struct work_stuff
43c6b8
   --work->forgetting_types;
43c6b8
   work->nrepeats = saved_nrepeats;
43c6b8
 
43c6b8
+  if ((work->options & DMGL_NO_RECURSE_LIMIT) == 0)
43c6b8
+    --work->recursion_level;
43c6b8
+
43c6b8
   return result;
43c6b8
 }
43c6b8
 
43c6b8
Only in binutils-2.27/libiberty: cplus-dem.c.orig
43c6b8
Only in binutils-2.27/libiberty: cplus-dem.c.rej