4c263d
From a3a7598c8ec6efb0eb9c0b786d80c4d2a3751b70 Mon Sep 17 00:00:00 2001
4c263d
From: Hugo van der Sanden <hv@crypt.org>
4c263d
Date: Tue, 18 Feb 2020 13:51:16 +0000
4c263d
Subject: [PATCH v528 2/3] study_chunk: extract rck_elide_nothing
4c263d
4c263d
(CVE-2020-10878)
4c263d
---
4c263d
 embed.fnc |  1 +
4c263d
 embed.h   |  1 +
4c263d
 proto.h   |  3 +++
4c263d
 regcomp.c | 70 ++++++++++++++++++++++++++++++++++---------------------
4c263d
 4 files changed, 48 insertions(+), 27 deletions(-)
4c263d
4c263d
diff --git a/embed.fnc b/embed.fnc
4c263d
index e762fe1eec..cf89277163 100644
4c263d
--- a/embed.fnc
4c263d
+++ b/embed.fnc
4c263d
@@ -2398,6 +2398,7 @@ Es	|SSize_t|study_chunk	|NN RExC_state_t *pRExC_state \
4c263d
                                 |I32 stopparen|U32 recursed_depth \
4c263d
 				|NULLOK regnode_ssc *and_withp \
4c263d
 				|U32 flags|U32 depth|bool was_mutate_ok
4c263d
+Es	|void	|rck_elide_nothing|NN regnode *node
4c263d
 EsRn	|U32	|add_data	|NN RExC_state_t* const pRExC_state \
4c263d
 				|NN const char* const s|const U32 n
4c263d
 rs	|void	|re_croak2	|bool utf8|NN const char* pat1|NN const char* pat2|...
4c263d
diff --git a/embed.h b/embed.h
4c263d
index a5416a1148..886551ce5c 100644
4c263d
--- a/embed.h
4c263d
+++ b/embed.h
4c263d
@@ -1046,6 +1046,7 @@
4c263d
 #define output_or_return_posix_warnings(a,b,c)	S_output_or_return_posix_warnings(aTHX_ a,b,c)
4c263d
 #define parse_lparen_question_flags(a)	S_parse_lparen_question_flags(aTHX_ a)
4c263d
 #define populate_ANYOF_from_invlist(a,b)	S_populate_ANYOF_from_invlist(aTHX_ a,b)
4c263d
+#define rck_elide_nothing(a)	S_rck_elide_nothing(aTHX_ a)
4c263d
 #define reg(a,b,c,d)		S_reg(aTHX_ a,b,c,d)
4c263d
 #define reg2Lanode(a,b,c,d)	S_reg2Lanode(aTHX_ a,b,c,d)
4c263d
 #define reg_node(a,b)		S_reg_node(aTHX_ a,b)
4c263d
diff --git a/proto.h b/proto.h
4c263d
index 66bb29b132..d3f8802c1d 100644
4c263d
--- a/proto.h
4c263d
+++ b/proto.h
4c263d
@@ -5150,6 +5150,9 @@ STATIC void	S_parse_lparen_question_flags(pTHX_ RExC_state_t *pRExC_state);
4c263d
 STATIC void	S_populate_ANYOF_from_invlist(pTHX_ regnode *node, SV** invlist_ptr);
4c263d
 #define PERL_ARGS_ASSERT_POPULATE_ANYOF_FROM_INVLIST	\
4c263d
 	assert(node); assert(invlist_ptr)
4c263d
+STATIC void	S_rck_elide_nothing(pTHX_ regnode *node);
4c263d
+#define PERL_ARGS_ASSERT_RCK_ELIDE_NOTHING	\
4c263d
+	assert(node)
4c263d
 PERL_STATIC_NO_RET void	S_re_croak2(pTHX_ bool utf8, const char* pat1, const char* pat2, ...)
4c263d
 			__attribute__noreturn__;
4c263d
 #define PERL_ARGS_ASSERT_RE_CROAK2	\
4c263d
diff --git a/regcomp.c b/regcomp.c
4c263d
index dd18add1db..0a9c6a8085 100644
4c263d
--- a/regcomp.c
4c263d
+++ b/regcomp.c
4c263d
@@ -4094,6 +4094,43 @@ S_unwind_scan_frames(pTHX_ const void *p)
4c263d
     } while (f);
4c263d
 }
4c263d
 
4c263d
+/* Follow the next-chain of the current node and optimize away
4c263d
+   all the NOTHINGs from it.
4c263d
+ */
4c263d
+STATIC void
4c263d
+S_rck_elide_nothing(pTHX_ regnode *node)
4c263d
+{
4c263d
+    dVAR;
4c263d
+
4c263d
+    PERL_ARGS_ASSERT_RCK_ELIDE_NOTHING;
4c263d
+
4c263d
+    if (OP(node) != CURLYX) {
4c263d
+        const int max = (reg_off_by_arg[OP(node)]
4c263d
+                        ? I32_MAX
4c263d
+                          /* I32 may be smaller than U16 on CRAYs! */
4c263d
+                        : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
4c263d
+        int off = (reg_off_by_arg[OP(node)] ? ARG(node) : NEXT_OFF(node));
4c263d
+        int noff;
4c263d
+        regnode *n = node;
4c263d
+
4c263d
+        /* Skip NOTHING and LONGJMP. */
4c263d
+        while (
4c263d
+            (n = regnext(n))
4c263d
+            && (
4c263d
+                (PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
4c263d
+                || ((OP(n) == LONGJMP) && (noff = ARG(n)))
4c263d
+            )
4c263d
+            && off + noff < max
4c263d
+        ) {
4c263d
+            off += noff;
4c263d
+        }
4c263d
+        if (reg_off_by_arg[OP(node)])
4c263d
+            ARG(node) = off;
4c263d
+        else
4c263d
+            NEXT_OFF(node) = off;
4c263d
+    }
4c263d
+    return;
4c263d
+}
4c263d
 
4c263d
 STATIC SSize_t
4c263d
 S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp,
4c263d
@@ -4197,28 +4234,10 @@ S_study_chunk(pTHX_ RExC_state_t *pRExC_state, regnode **scanp,
4c263d
         if (mutate_ok)
4c263d
             JOIN_EXACT(scan,&min_subtract, &unfolded_multi_char, 0);
4c263d
 
4c263d
-	/* Follow the next-chain of the current node and optimize
4c263d
-	   away all the NOTHINGs from it.  */
4c263d
-	if (OP(scan) != CURLYX) {
4c263d
-	    const int max = (reg_off_by_arg[OP(scan)]
4c263d
-		       ? I32_MAX
4c263d
-		       /* I32 may be smaller than U16 on CRAYs! */
4c263d
-		       : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
4c263d
-	    int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
4c263d
-	    int noff;
4c263d
-	    regnode *n = scan;
4c263d
-
4c263d
-	    /* Skip NOTHING and LONGJMP. */
4c263d
-	    while ((n = regnext(n))
4c263d
-		   && ((PL_regkind[OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
4c263d
-		       || ((OP(n) == LONGJMP) && (noff = ARG(n))))
4c263d
-		   && off + noff < max)
4c263d
-		off += noff;
4c263d
-	    if (reg_off_by_arg[OP(scan)])
4c263d
-		ARG(scan) = off;
4c263d
-	    else
4c263d
-		NEXT_OFF(scan) = off;
4c263d
-	}
4c263d
+        /* Follow the next-chain of the current node and optimize
4c263d
+           away all the NOTHINGs from it.
4c263d
+         */
4c263d
+        rck_elide_nothing(scan);
4c263d
 
4c263d
 	/* The principal pseudo-switch.  Cannot be a switch, since we
4c263d
 	   look into several different things.  */
4c263d
@@ -5348,11 +5367,7 @@ Perl_re_printf( aTHX_  "LHS=%" UVuf " RHS=%" UVuf "\n",
4c263d
 		if (data && (fl & SF_HAS_EVAL))
4c263d
 		    data->flags |= SF_HAS_EVAL;
4c263d
 	      optimize_curly_tail:
4c263d
-		if (OP(oscan) != CURLYX) {
4c263d
-		    while (PL_regkind[OP(next = regnext(oscan))] == NOTHING
4c263d
-			   && NEXT_OFF(next))
4c263d
-			NEXT_OFF(oscan) += NEXT_OFF(next);
4c263d
-		}
4c263d
+		rck_elide_nothing(oscan);
4c263d
 		continue;
4c263d
 
4c263d
 	    default:
4c263d
-- 
4c263d
2.20.1
4c263d