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