Skip to content

Commit faa1fc3

Browse files
Abseil Teamcopybara-github
authored andcommitted
Avoid DoS with unnecessarily long numbers in RustSymbolParser::ParseBase62Number()
PiperOrigin-RevId: 948350914 Change-Id: I1d53894f02e6c4e87c5f2af4853a59131f66ead2
1 parent e6a287b commit faa1fc3

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

absl/debugging/internal/demangle_rust.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,15 @@ class RustSymbolParser {
653653
// A nonempty digit sequence denotes its base-62 value plus 1.
654654
int encoded_number = 0;
655655
bool overflowed = false;
656-
while (IsAlpha(Peek()) || IsDigit(Peek())) {
656+
for (int scanned = 0; IsAlpha(Peek()) || IsDigit(Peek()); ++scanned) {
657+
// Cap the scan length: a u64 fits in 11 base-62 digits, and int overflows
658+
// after ~5, so anything beyond ~16 digits is already failing to parse.
659+
if (scanned >= 16) {
660+
// Reject pathologically long runs so a backref cannot re-scan
661+
// arbitrarily long stretches of input per iteration.
662+
return false;
663+
}
664+
657665
const char c = Take();
658666
if (encoded_number >= std::numeric_limits<int>::max()/62) {
659667
// If we are close to overflowing an int, keep parsing but stop updating

absl/debugging/internal/demangle_rust_test.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ TEST(DemangleRust, ClosureNumberOverflowingInt) {
176176
"crate_name::func_name::{closure#?}");
177177
}
178178

179+
TEST(DemangleRust, Base62NumberScanLimit) {
180+
// Up to 16 base-62 digits is permitted (though int overflow yields "?").
181+
EXPECT_DEMANGLING(
182+
"_RNCNvCs09azAZ_10crate_name9func_names0123456789abcdef_0Cs123_12client_"
183+
"crate",
184+
"crate_name::func_name::{closure#?}");
185+
186+
// Beyond 16 base-62 digits is rejected to prevent excessive re-scanning.
187+
EXPECT_DEMANGLING_FAILS(
188+
"_RNCNvCs09azAZ_10crate_name9func_names0123456789abcdef0_0Cs123_12client_"
189+
"crate");
190+
EXPECT_DEMANGLING_FAILS("_RB0123456789abcdef0_");
191+
}
192+
179193
TEST(DemangleRust, UnexpectedlyNamedClosure) {
180194
EXPECT_DEMANGLING(
181195
"_RNCNvCs123_10crate_name9func_name12closure_nameCs456_12client_crate",

0 commit comments

Comments
 (0)