diff options
author | Karl Williamson <khw@cpan.org> | 2020-04-21 17:30:42 -0600 |
---|---|---|
committer | Karl Williamson <khw@cpan.org> | 2020-04-29 13:10:11 -0600 |
commit | 07b2324000451f5ce36e176cb75d49997ca895bf (patch) | |
tree | 3450208b5e7d28828c554dedb66d03c0a2b79890 | |
parent | 28601d809e9116df25d4e9a091f21ecf73a1c87a (diff) | |
download | perl-07b2324000451f5ce36e176cb75d49997ca895bf.tar.gz |
regcomp.c: Avoid use after free
It turns out that the SV returned by re_intuit_string() may be freed by
future calls to re_intuit_start(). Thus, the caller doesn't get clear
title to the returned SV. (This wasn't documented until the
commit immediately prior to this one.)
Cope with this situation by making a mortalized copy. This commit also
changes to use the copy's PV directly, simplifying some 'if' statements.
re_intuit_string() is effectively in the API, as it is an element in the
regex engine structure, callable by anyone. It should not be returning
a tenuous SV. That returned scalar should not freed before the pattern
it is for is freed. It is too late in the development cycle to change
this, so this workaround is presented instead for 5.32.
This fixes #17734.
-rw-r--r-- | regcomp.c | 31 | ||||
-rw-r--r-- | t/re/pat_advanced.t | 9 |
2 files changed, 27 insertions, 13 deletions
@@ -25013,8 +25013,10 @@ S_handle_names_wildcard(pTHX_ const char * wname, /* wildcard name to match */ where we are now */ bool found_matches = FALSE; /* Did any name match so far? */ SV * empty; /* For matching zero length names */ - SV * must; /* What substring, if any, must be in a name - for the subpattern to match */ + SV * must_sv; /* Contains the substring, if any, that must be + in a name for the subpattern to match */ + char * must; /* The PV of 'must' */ + STRLEN must_len; /* And its length */ SV * syllable_name = NULL; /* For Hangul syllables */ const char hangul_prefix[] = "HANGUL SYLLABLE "; const STRLEN hangul_prefix_len = sizeof(hangul_prefix) - 1; @@ -25079,7 +25081,17 @@ S_handle_names_wildcard(pTHX_ const char * wname, /* wildcard name to match */ /* Compile the subpattern consisting of the name being looked for */ subpattern_re = compile_wildcard(wname, wname_len, FALSE /* /-i */ ); - must = re_intuit_string(subpattern_re); + + must_sv = re_intuit_string(subpattern_re); + if (must_sv) { + /* regexec.c can free the re_intuit_string() return. GH #17734 */ + must_sv = sv_2mortal(newSVsv(must_sv)); + must = SvPV(must_sv, must_len); + } + else { + must = ""; + must_len = 0; + } /* (Note: 'must' could contain a NUL. And yet we use strspn() below on it. * This works because the NUL causes the function to return early, thus @@ -25095,10 +25107,7 @@ S_handle_names_wildcard(pTHX_ const char * wname, /* wildcard name to match */ /* And match against the string of all names /gc. Don't even try if it * must match a character not found in any name. */ - if ( ! must - || SvCUR(must) == 0 - || strspn(SvPVX(must), "\n -0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()") - == SvCUR(must)) + if (strspn(must, "\n -0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ()") == must_len) { while (execute_wildcard(subpattern_re, cur_pos, @@ -25238,9 +25247,7 @@ S_handle_names_wildcard(pTHX_ const char * wname, /* wildcard name to match */ * one of the characters in that isn't in any Hangul syllable. */ if ( prog->minlen <= (SSize_t) syl_max_len && prog->maxlen > 0 - && ( ! must - || SvCUR(must) == 0 - || strspn(SvPVX(must), "\n ABCDEGHIJKLMNOPRSTUWY") == SvCUR(must))) + && (strspn(must, "\n ABCDEGHIJKLMNOPRSTUWY") == must_len)) { /* These constants, names, values, and algorithm are adapted from the * Unicode standard, version 5.1, section 3.12, and should never @@ -25335,9 +25342,7 @@ S_handle_names_wildcard(pTHX_ const char * wname, /* wildcard name to match */ * series */ if ( prog->minlen <= (SSize_t) SvCUR(algo_name) && prog->maxlen > 0 - && ( ! must - || SvCUR(must) == 0 - || strspn(SvPVX(must), legal) == SvCUR(must))) + && (strspn(must, legal) == must_len)) { for (j = low; j <= high; j++) { /* For each code point in the series */ diff --git a/t/re/pat_advanced.t b/t/re/pat_advanced.t index 41f344ac9e..21bdb8ca15 100644 --- a/t/re/pat_advanced.t +++ b/t/re/pat_advanced.t @@ -2553,6 +2553,15 @@ EOF {}, "Too large negative relative group number"); } + { # GH #17734, ASAN use after free + fresh_perl_like('no warnings "experimental::uniprop_wildcards"; + my $re = q<[[\p{name=/[Y-]+Z/}]]>; + eval { "\N{BYZANTINE MUSICAL SYMBOL PSILI}" + =~ /$re/ }; print $@ if $@; print "Done\n";', + qr/Done/, + {}, "GH #17734"); + } + # !!! NOTE that tests that aren't at all likely to crash perl should go # a ways above, above these last ones. There's a comment there that, like |