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