Skip to content

Commit b3725d4

Browse files
committed
[perl #123782] regcomp: check for overflow on /(?123)/
AFL (<http://lcamtuf.coredump.cx/afl>) found that the UV to I32 conversion can evade the necessary range checks on wraparound, leading to bad reads. Check for it, and force to I32_MAX, expecting that this will usually yield a "Reference to nonexistent group" error.
1 parent 0fa70a0 commit b3725d4

File tree

Image for: File tree

2 files changed

Image for: 2 files changed
+17
-2
lines changed

2 files changed

Image for: 2 files changed
+17
-2
lines changed

‎regcomp.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10118,12 +10118,14 @@ S_reg(pTHX_ RExC_state_t *pRExC_state, I32 paren, I32 *flagp,U32 depth)
1011810118
parse_recursion:
1011910119
{
1012010120
bool is_neg = FALSE;
10121+
UV unum;
1012110122
parse_start = RExC_parse - 1; /* MJD */
1012210123
if (*RExC_parse == '-') {
1012310124
RExC_parse++;
1012410125
is_neg = TRUE;
1012510126
}
10126-
num = grok_atou(RExC_parse, &endptr);
10127+
unum = grok_atou(RExC_parse, &endptr);
10128+
num = (unum > I32_MAX) ? I32_MAX : (I32)unum;
1012710129
if (endptr)
1012810130
RExC_parse = (char*)endptr;
1012910131
if (is_neg) {

‎t/re/pat.t

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ BEGIN {
2222
skip_all_without_unicode_tables();
2323
}
2424

25-
plan tests => 765; # Update this when adding/deleting tests.
25+
plan tests => 769; # Update this when adding/deleting tests.
2626

2727
run_tests() unless caller;
2828

@@ -1646,6 +1646,19 @@ EOP
16461646
"qr/${pat}x/ shows x in error even if it's a wide character");
16471647
}
16481648
}
1649+
1650+
{
1651+
# Expect one of these sizes to cause overflow and wrap to negative
1652+
for my $bits (32, 64) {
1653+
my $wrapneg = 2 ** ($bits - 2) * 3;
1654+
for my $sign ('', '-') {
1655+
my $pat = sprintf "qr/(?%s%u)/", $sign, $wrapneg;
1656+
eval $pat;
1657+
ok(1, "big backref $pat did not crash");
1658+
}
1659+
}
1660+
}
1661+
16491662
} # End of sub run_tests
16501663

16511664
1;

0 commit comments

Image for: 0 commit comments
Comments
 (0)