From 2866e54337643e1a6e2cd05333d0fbb2d1dd29d4 Mon Sep 17 00:00:00 2001 From: Nicholas Wilson Date: Thu, 27 Aug 2026 16:52:16 +0100 Subject: [PATCH] Backport of pcre2-10.48-Fix-invalid-UTF-backwards-scans.patch Cherry-pick of 4889caf31a4c5a6b3c051f0031bf2dbd78f2c287 Fix invalid UTF backwards-scan reads; see GHSA-9qww-pwc4-77qq for details --- src/pcre2_extuni.c | 10 +++++----- src/pcre2_match.c | 10 +++++----- testdata/testinput10 | 10 ++++++++++ testdata/testinput12 | 10 ++++++++++ testdata/testoutput10 | 12 ++++++++++++ testdata/testoutput12-16 | 12 ++++++++++++ testdata/testoutput12-32 | 12 ++++++++++++ 7 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/pcre2_extuni.c b/src/pcre2_extuni.c index b23946b0..b0e73350 100644 --- a/src/pcre2_extuni.c +++ b/src/pcre2_extuni.c @@ -57,12 +57,12 @@ support, because some compilers do not like functionless source files. */ #ifndef SUPPORT_UNICODE PCRE2_SPTR -PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject, +PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR check_subject, PCRE2_SPTR end_subject, BOOL utf, int *xcount) { (void)c; (void)eptr; -(void)start_subject; +(void)check_subject; (void)end_subject; (void)utf; (void)xcount; @@ -79,7 +79,7 @@ return NULL; Arguments: c the first character eptr pointer to next character - start_subject pointer to start of subject + check_subject pointer to start of validated subject end_subject pointer to end of subject utf TRUE if in UTF mode xcount pointer to count of additional characters, @@ -89,7 +89,7 @@ Returns: pointer after the end of the sequence */ PCRE2_SPTR -PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR start_subject, +PRIV(extuni)(uint32_t c, PCRE2_SPTR eptr, PCRE2_SPTR check_subject, PCRE2_SPTR end_subject, BOOL utf, int *xcount) { int lgb = UCD_GRAPHBREAK(c); @@ -113,7 +113,7 @@ while (eptr < end_subject) /* bptr is pointing to the left-hand character */ - while (bptr > start_subject) + while (bptr > check_subject) { bptr--; if (utf) diff --git a/src/pcre2_match.c b/src/pcre2_match.c index cae13a7d..0db58304 100644 --- a/src/pcre2_match.c +++ b/src/pcre2_match.c @@ -2627,7 +2627,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode, else { GETCHARINCTEST(fc, Feptr); - Feptr = PRIV(extuni)(fc, Feptr, mb->start_subject, mb->end_subject, utf, + Feptr = PRIV(extuni)(fc, Feptr, mb->check_subject, mb->end_subject, utf, NULL); } CHECK_PARTIAL(); @@ -2988,7 +2988,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode, else { GETCHARINCTEST(fc, Feptr); - Feptr = PRIV(extuni)(fc, Feptr, mb->start_subject, + Feptr = PRIV(extuni)(fc, Feptr, mb->check_subject, mb->end_subject, utf, NULL); } CHECK_PARTIAL(); @@ -3819,7 +3819,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode, else { GETCHARINCTEST(fc, Feptr); - Feptr = PRIV(extuni)(fc, Feptr, mb->start_subject, mb->end_subject, + Feptr = PRIV(extuni)(fc, Feptr, mb->check_subject, mb->end_subject, utf, NULL); } CHECK_PARTIAL(); @@ -4413,7 +4413,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode, else { GETCHARINCTEST(fc, Feptr); - Feptr = PRIV(extuni)(fc, Feptr, mb->start_subject, mb->end_subject, + Feptr = PRIV(extuni)(fc, Feptr, mb->check_subject, mb->end_subject, utf, NULL); } CHECK_PARTIAL(); @@ -5845,7 +5845,7 @@ fprintf(stderr, "++ %2ld op=%3d %s\n", Fecode - mb->start_code, *Fecode, { for (i = 0; i < Lmax; i++) { - if (Feptr == mb->start_subject) + if (Feptr <= mb->check_subject) { if (i < Lmin) RRETURN(MATCH_NOMATCH); Lmax = i; diff --git a/testdata/testinput10 b/testdata/testinput10 index e901d51d..b3db4240 100644 --- a/testdata/testinput10 +++ b/testdata/testinput10 @@ -576,6 +576,16 @@ AAA\x80BXYZ AAA\x80BBXYZ +# Backward scans must not enter the invalid prefix before check_subject. + +/(?<=a{1,2})X/utf,match_invalid_utf +\= Expect no match + \x80X + +/\X\X/utf,match_invalid_utf +\= Expect no match + \x80\x{1f1e6}\x{1f1e7} + # ------------------------------------- /(*UTF)(?=\x{123})/I diff --git a/testdata/testinput12 b/testdata/testinput12 index 85550c3b..0b182da3 100644 --- a/testdata/testinput12 +++ b/testdata/testinput12 @@ -468,6 +468,16 @@ /A\z/utf,match_invalid_utf A\x{df00}\n +# Backward scans must not enter the invalid prefix before check_subject. + +/(?<=a{1,2})X/utf,match_invalid_utf +\= Expect no match + \x{dc00}X + +/\X\X/utf,match_invalid_utf +\= Expect no match + \x{dc00}\x{1f1e6}\x{1f1e7} + # ---------------------------------------------------- /(*UTF)(?=\x{123})/I diff --git a/testdata/testoutput10 b/testdata/testoutput10 index 8145891f..37fff507 100644 --- a/testdata/testoutput10 +++ b/testdata/testoutput10 @@ -1761,6 +1761,18 @@ No match AAA\x80BBXYZ No match +# Backward scans must not enter the invalid prefix before check_subject. + +/(?<=a{1,2})X/utf,match_invalid_utf +\= Expect no match + \x80X +No match + +/\X\X/utf,match_invalid_utf +\= Expect no match + \x80\x{1f1e6}\x{1f1e7} +No match + # ------------------------------------- /(*UTF)(?=\x{123})/I diff --git a/testdata/testoutput12-16 b/testdata/testoutput12-16 index 616d6930..c1444a36 100644 --- a/testdata/testoutput12-16 +++ b/testdata/testoutput12-16 @@ -1612,6 +1612,18 @@ No match A\x{df00}\n No match +# Backward scans must not enter the invalid prefix before check_subject. + +/(?<=a{1,2})X/utf,match_invalid_utf +\= Expect no match + \x{dc00}X +No match + +/\X\X/utf,match_invalid_utf +\= Expect no match + \x{dc00}\x{1f1e6}\x{1f1e7} +No match + # ---------------------------------------------------- /(*UTF)(?=\x{123})/I diff --git a/testdata/testoutput12-32 b/testdata/testoutput12-32 index 3c9586e4..c26b44b1 100644 --- a/testdata/testoutput12-32 +++ b/testdata/testoutput12-32 @@ -1610,6 +1610,18 @@ No match A\x{df00}\n No match +# Backward scans must not enter the invalid prefix before check_subject. + +/(?<=a{1,2})X/utf,match_invalid_utf +\= Expect no match + \x{dc00}X +No match + +/\X\X/utf,match_invalid_utf +\= Expect no match + \x{dc00}\x{1f1e6}\x{1f1e7} +No match + # ---------------------------------------------------- /(*UTF)(?=\x{123})/I -- 2.56.0