Blame SOURCES/binutils-CVE-2021-3530.patch

8d443f
--- binutils.orig/libiberty/rust-demangle.c	2021-05-07 17:13:43.762229783 +0100
8d443f
+++ binutils-2.36.1/libiberty/rust-demangle.c	2021-05-07 17:14:39.805820593 +0100
8d443f
@@ -74,6 +74,12 @@ struct rust_demangler
8d443f
   /* Rust mangling version, with legacy mangling being -1. */
8d443f
   int version;
8d443f
 
8d443f
+  /* Recursion depth.  */
8d443f
+  uint recursion;
8d443f
+  /* Maximum number of times demangle_path may be called recursively.  */
8d443f
+#define RUST_MAX_RECURSION_COUNT  1024
8d443f
+#define RUST_NO_RECURSION_LIMIT   ((uint) -1)
8d443f
+
8d443f
   uint64_t bound_lifetime_depth;
8d443f
 };
8d443f
 
8d443f
@@ -671,6 +677,15 @@ demangle_path (struct rust_demangler *rd
8d443f
   if (rdm->errored)
8d443f
     return;
8d443f
 
8d443f
+  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
8d443f
+    {
8d443f
+      ++ rdm->recursion;
8d443f
+      if (rdm->recursion > RUST_MAX_RECURSION_COUNT)
8d443f
+	/* FIXME: There ought to be a way to report
8d443f
+	   that the recursion limit has been reached.  */
8d443f
+	goto fail_return;
8d443f
+    }
8d443f
+
8d443f
   switch (tag = next (rdm))
8d443f
     {
8d443f
     case 'C':
8d443f
@@ -688,10 +703,7 @@ demangle_path (struct rust_demangler *rd
8d443f
     case 'N':
8d443f
       ns = next (rdm);
8d443f
       if (!ISLOWER (ns) && !ISUPPER (ns))
8d443f
-        {
8d443f
-          rdm->errored = 1;
8d443f
-          return;
8d443f
-        }
8d443f
+	goto fail_return;
8d443f
 
8d443f
       demangle_path (rdm, in_value);
8d443f
 
8d443f
@@ -776,9 +788,15 @@ demangle_path (struct rust_demangler *rd
8d443f
         }
8d443f
       break;
8d443f
     default:
8d443f
-      rdm->errored = 1;
8d443f
-      return;
8d443f
+      goto fail_return;
8d443f
     }
8d443f
+  goto pass_return;
8d443f
+
8d443f
+ fail_return:
8d443f
+  rdm->errored = 1;
8d443f
+ pass_return:
8d443f
+  if (rdm->recursion != RUST_NO_RECURSION_LIMIT)
8d443f
+    -- rdm->recursion;
8d443f
 }
8d443f
 
8d443f
 static void
8d443f
@@ -1317,6 +1335,7 @@ rust_demangle_callback (const char *mang
8d443f
   rdm.skipping_printing = 0;
8d443f
   rdm.verbose = (options & DMGL_VERBOSE) != 0;
8d443f
   rdm.version = 0;
8d443f
+  rdm.recursion = (options & DMGL_NO_RECURSE_LIMIT) ? RUST_NO_RECURSION_LIMIT : 0;
8d443f
   rdm.bound_lifetime_depth = 0;
8d443f
 
8d443f
   /* Rust symbols always start with _R (v0) or _ZN (legacy). */