|
| 1 | +From f10bec5ffa487ad3033ed5f38cfd0fc7d696deab Mon Sep 17 00:00:00 2001 |
| 2 | +From: Nick Clifton < [email protected]> |
| 3 | +Date: Mon, 31 Jan 2022 14:28:42 +0000 |
| 4 | +Subject: libiberty: Fix infinite recursion in rust demangler. |
| 5 | + |
| 6 | +libiberty/ |
| 7 | + PR demangler/98886 |
| 8 | + PR demangler/99935 |
| 9 | + * rust-demangle.c (struct rust_demangler): Add a recursion |
| 10 | + counter. |
| 11 | + (demangle_path): Increment/decrement the recursion counter upon |
| 12 | + entry and exit. Fail if the counter exceeds a fixed limit. |
| 13 | + (demangle_type): Likewise. |
| 14 | + (rust_demangle_callback): Initialise the recursion counter, |
| 15 | + disabling if requested by the option flags. |
| 16 | +--- |
| 17 | + libiberty/rust-demangle.c | 47 +++++++++++++++++++++++++++++++++++++++++------ |
| 18 | + 1 file changed, 41 insertions(+), 6 deletions(-) |
| 19 | + |
| 20 | +diff --git a/libiberty/rust-demangle.c b/libiberty/rust-demangle.c |
| 21 | +index 18c760491bdc..3b24d63892a9 100644 |
| 22 | +--- a/libiberty/rust-demangle.c |
| 23 | ++++ b/libiberty/rust-demangle.c |
| 24 | +@@ -74,6 +74,12 @@ struct rust_demangler |
| 25 | + /* Rust mangling version, with legacy mangling being -1. */ |
| 26 | + int version; |
| 27 | + |
| 28 | ++ /* Recursion depth. */ |
| 29 | ++ unsigned int recursion; |
| 30 | ++ /* Maximum number of times demangle_path may be called recursively. */ |
| 31 | ++#define RUST_MAX_RECURSION_COUNT 1024 |
| 32 | ++#define RUST_NO_RECURSION_LIMIT ((unsigned int) -1) |
| 33 | ++ |
| 34 | + uint64_t bound_lifetime_depth; |
| 35 | + }; |
| 36 | + |
| 37 | +@@ -671,6 +677,15 @@ demangle_path (struct rust_demangler *rdm, int in_value) |
| 38 | + if (rdm->errored) |
| 39 | + return; |
| 40 | + |
| 41 | ++ if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
| 42 | ++ { |
| 43 | ++ ++ rdm->recursion; |
| 44 | ++ if (rdm->recursion > RUST_MAX_RECURSION_COUNT) |
| 45 | ++ /* FIXME: There ought to be a way to report |
| 46 | ++ that the recursion limit has been reached. */ |
| 47 | ++ goto fail_return; |
| 48 | ++ } |
| 49 | ++ |
| 50 | + switch (tag = next (rdm)) |
| 51 | + { |
| 52 | + case 'C': |
| 53 | +@@ -688,10 +703,7 @@ demangle_path (struct rust_demangler *rdm, int in_value) |
| 54 | + case 'N': |
| 55 | + ns = next (rdm); |
| 56 | + if (!ISLOWER (ns) && !ISUPPER (ns)) |
| 57 | +- { |
| 58 | +- rdm->errored = 1; |
| 59 | +- return; |
| 60 | +- } |
| 61 | ++ goto fail_return; |
| 62 | + |
| 63 | + demangle_path (rdm, in_value); |
| 64 | + |
| 65 | +@@ -776,9 +788,15 @@ demangle_path (struct rust_demangler *rdm, int in_value) |
| 66 | + } |
| 67 | + break; |
| 68 | + default: |
| 69 | +- rdm->errored = 1; |
| 70 | +- return; |
| 71 | ++ goto fail_return; |
| 72 | + } |
| 73 | ++ goto pass_return; |
| 74 | ++ |
| 75 | ++ fail_return: |
| 76 | ++ rdm->errored = 1; |
| 77 | ++ pass_return: |
| 78 | ++ if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
| 79 | ++ -- rdm->recursion; |
| 80 | + } |
| 81 | + |
| 82 | + static void |
| 83 | +@@ -870,6 +888,19 @@ demangle_type (struct rust_demangler *rdm) |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | ++ if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
| 88 | ++ { |
| 89 | ++ ++ rdm->recursion; |
| 90 | ++ if (rdm->recursion > RUST_MAX_RECURSION_COUNT) |
| 91 | ++ /* FIXME: There ought to be a way to report |
| 92 | ++ that the recursion limit has been reached. */ |
| 93 | ++ { |
| 94 | ++ rdm->errored = 1; |
| 95 | ++ -- rdm->recursion; |
| 96 | ++ return; |
| 97 | ++ } |
| 98 | ++ } |
| 99 | ++ |
| 100 | + switch (tag) |
| 101 | + { |
| 102 | + case 'R': |
| 103 | +@@ -1030,6 +1061,9 @@ demangle_type (struct rust_demangler *rdm) |
| 104 | + rdm->next--; |
| 105 | + demangle_path (rdm, 0); |
| 106 | + } |
| 107 | ++ |
| 108 | ++ if (rdm->recursion != RUST_NO_RECURSION_LIMIT) |
| 109 | ++ -- rdm->recursion; |
| 110 | + } |
| 111 | + |
| 112 | + /* A trait in a trait object may have some "existential projections" |
| 113 | +@@ -1320,6 +1354,7 @@ rust_demangle_callback (const char *mangled, int options, |
| 114 | + rdm.skipping_printing = 0; |
| 115 | + rdm.verbose = (options & DMGL_VERBOSE) != 0; |
| 116 | + rdm.version = 0; |
| 117 | ++ rdm.recursion = (options & DMGL_NO_RECURSE_LIMIT) ? RUST_NO_RECURSION_LIMIT : 0; |
| 118 | + rdm.bound_lifetime_depth = 0; |
| 119 | + |
| 120 | + /* Rust symbols always start with _R (v0) or _ZN (legacy). */ |
| 121 | +-- |
| 122 | +cgit |
| 123 | + |
0 commit comments