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