78f1eb
From 0a320d753fe7fca03df259a4dfd8e641e51edaa8 Mon Sep 17 00:00:00 2001
78f1eb
From: Hugo van der Sanden <hv@crypt.org>
78f1eb
Date: Tue, 18 Feb 2020 13:51:16 +0000
78f1eb
Subject: [PATCH 1/2] study_chunk: extract rck_elide_nothing
78f1eb
MIME-Version: 1.0
78f1eb
Content-Type: text/plain; charset=UTF-8
78f1eb
Content-Transfer-Encoding: 8bit
78f1eb
78f1eb
(CVE-2020-10878)
78f1eb
78f1eb
(cherry picked from commit 93dee06613d4e1428fb10905ce1c3c96f53113dc)
78f1eb
Signed-off-by: Petr Písař <ppisar@redhat.com>
78f1eb
---
78f1eb
 embed.fnc |  1 +
78f1eb
 embed.h   |  1 +
78f1eb
 proto.h   |  3 +++
78f1eb
 regcomp.c | 70 ++++++++++++++++++++++++++++++++++---------------------
78f1eb
 4 files changed, 48 insertions(+), 27 deletions(-)
78f1eb
78f1eb
diff --git a/embed.fnc b/embed.fnc
78f1eb
index aedb4baef1..d7cd04d3fc 100644
78f1eb
--- a/embed.fnc
78f1eb
+++ b/embed.fnc
78f1eb
@@ -2481,6 +2481,7 @@ Es	|SSize_t|study_chunk	|NN RExC_state_t *pRExC_state \
78f1eb
                                 |I32 stopparen|U32 recursed_depth \
78f1eb
 				|NULLOK regnode_ssc *and_withp \
78f1eb
 				|U32 flags|U32 depth
78f1eb
+Es	|void	|rck_elide_nothing|NN regnode *node
78f1eb
 EsR	|SV *	|get_ANYOFM_contents|NN const regnode * n
78f1eb
 EsRn	|U32	|add_data	|NN RExC_state_t* const pRExC_state \
78f1eb
 				|NN const char* const s|const U32 n
78f1eb
diff --git a/embed.h b/embed.h
78f1eb
index 75c91f77f4..356a8b98d9 100644
78f1eb
--- a/embed.h
78f1eb
+++ b/embed.h
78f1eb
@@ -1208,6 +1208,7 @@
78f1eb
 #define parse_lparen_question_flags(a)	S_parse_lparen_question_flags(aTHX_ a)
78f1eb
 #define parse_uniprop_string(a,b,c,d,e,f,g,h,i)	Perl_parse_uniprop_string(aTHX_ a,b,c,d,e,f,g,h,i)
78f1eb
 #define populate_ANYOF_from_invlist(a,b)	S_populate_ANYOF_from_invlist(aTHX_ a,b)
78f1eb
+#define rck_elide_nothing(a)	S_rck_elide_nothing(aTHX_ a)
78f1eb
 #define reg(a,b,c,d)		S_reg(aTHX_ a,b,c,d)
78f1eb
 #define reg2Lanode(a,b,c,d)	S_reg2Lanode(aTHX_ a,b,c,d)
78f1eb
 #define reg_node(a,b)		S_reg_node(aTHX_ a,b)
78f1eb
diff --git a/proto.h b/proto.h
78f1eb
index 141ddbaee6..f316fe134e 100644
78f1eb
--- a/proto.h
78f1eb
+++ b/proto.h
78f1eb
@@ -5543,6 +5543,9 @@ PERL_CALLCONV SV *	Perl_parse_uniprop_string(pTHX_ const char * const name, cons
78f1eb
 STATIC void	S_populate_ANYOF_from_invlist(pTHX_ regnode *node, SV** invlist_ptr);
78f1eb
 #define PERL_ARGS_ASSERT_POPULATE_ANYOF_FROM_INVLIST	\
78f1eb
 	assert(node); assert(invlist_ptr)
78f1eb
+STATIC void	S_rck_elide_nothing(pTHX_ regnode *node);
78f1eb
+#define PERL_ARGS_ASSERT_RCK_ELIDE_NOTHING	\
78f1eb
+	assert(node)
78f1eb
 PERL_STATIC_NO_RET void	S_re_croak2(pTHX_ bool utf8, const char* pat1, const char* pat2, ...)
78f1eb
 			__attribute__noreturn__;
78f1eb
 #define PERL_ARGS_ASSERT_RE_CROAK2	\
78f1eb
diff --git a/regcomp.c b/regcomp.c
78f1eb
index 5f86be8086..4ba2980db6 100644
78f1eb
--- a/regcomp.c
78f1eb
+++ b/regcomp.c
78f1eb
@@ -4450,6 +4450,44 @@ S_unwind_scan_frames(pTHX_ const void *p)
78f1eb
     } while (f);
78f1eb
 }
78f1eb
 
78f1eb
+/* Follow the next-chain of the current node and optimize away
78f1eb
+   all the NOTHINGs from it.
78f1eb
+ */
78f1eb
+STATIC void
78f1eb
+S_rck_elide_nothing(pTHX_ regnode *node)
78f1eb
+{
78f1eb
+    dVAR;
78f1eb
+
78f1eb
+    PERL_ARGS_ASSERT_RCK_ELIDE_NOTHING;
78f1eb
+
78f1eb
+    if (OP(node) != CURLYX) {
78f1eb
+        const int max = (reg_off_by_arg[OP(node)]
78f1eb
+                        ? I32_MAX
78f1eb
+                          /* I32 may be smaller than U16 on CRAYs! */
78f1eb
+                        : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
78f1eb
+        int off = (reg_off_by_arg[OP(node)] ? ARG(node) : NEXT_OFF(node));
78f1eb
+        int noff;
78f1eb
+        regnode *n = node;
78f1eb
+
78f1eb
+        /* Skip NOTHING and LONGJMP. */
78f1eb
+        while (
78f1eb
+            (n = regnext(n))
78f1eb
+            && (
78f1eb
+                (PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
78f1eb
+                || ((OP(n) == LONGJMP) && (noff = ARG(n)))
78f1eb
+            )
78f1eb
+            && off + noff < max
78f1eb
+        ) {
78f1eb
+            off += noff;
78f1eb
+        }
78f1eb
+        if (reg_off_by_arg[OP(node)])
78f1eb
+            ARG(node) = off;
78f1eb
+        else
78f1eb
+            NEXT_OFF(node) = off;
78f1eb
+    }
78f1eb
+    return;
78f1eb
+}
78f1eb
+
78f1eb
 /* the return from this sub is the minimum length that could possibly match */
78f1eb
 STATIC SSize_t
78f1eb
 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp,
78f1eb
@@ -4550,28 +4588,10 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp,
78f1eb
          */
78f1eb
         JOIN_EXACT(scan,&min_subtract, &unfolded_multi_char, 0);
78f1eb
 
78f1eb
-	/* Follow the next-chain of the current node and optimize
78f1eb
-	   away all the NOTHINGs from it.  */
78f1eb
-	if (OP(scan) != CURLYX) {
78f1eb
-	    const int max = (reg_off_by_arg[OP(scan)]
78f1eb
-		       ? I32_MAX
78f1eb
-		       /* I32 may be smaller than U16 on CRAYs! */
78f1eb
-		       : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
78f1eb
-	    int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
78f1eb
-	    int noff;
78f1eb
-	    regnode *n = scan;
78f1eb
-
78f1eb
-	    /* Skip NOTHING and LONGJMP. */
78f1eb
-	    while ((n = regnext(n))
78f1eb
-		   && ((PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
78f1eb
-		       || ((OP(n) == LONGJMP) && (noff = ARG(n))))
78f1eb
-		   && off + noff < max)
78f1eb
-		off += noff;
78f1eb
-	    if (reg_off_by_arg[OP(scan)])
78f1eb
-		ARG(scan) = off;
78f1eb
-	    else
78f1eb
-		NEXT_OFF(scan) = off;
78f1eb
-	}
78f1eb
+        /* Follow the next-chain of the current node and optimize
78f1eb
+           away all the NOTHINGs from it.
78f1eb
+         */
78f1eb
+        rck_elide_nothing(scan);
78f1eb
 
78f1eb
 	/* The principal pseudo-switch.  Cannot be a switch, since we
78f1eb
 	   look into several different things.  */
78f1eb
@@ -5745,11 +5765,7 @@ Perl_re_printf( aTHX_  "LHS=%" UVuf " RHS=%" UVuf "\n",
78f1eb
 		if (data && (fl & SF_HAS_EVAL))
78f1eb
 		    data->flags |= SF_HAS_EVAL;
78f1eb
 	      optimize_curly_tail:
78f1eb
-		if (OP(oscan) != CURLYX) {
78f1eb
-		    while (PL_regkind[OP(next = regnext(oscan))] == NOTHING
78f1eb
-			   && NEXT_OFF(next))
78f1eb
-			NEXT_OFF(oscan) += NEXT_OFF(next);
78f1eb
-		}
78f1eb
+		rck_elide_nothing(oscan);
78f1eb
 		continue;
78f1eb
 
78f1eb
 	    default:
78f1eb
-- 
78f1eb
2.25.4
78f1eb