Blame SOURCES/rsyslog-8.24.0-rhbz1427828-set-unset-not-checking-varName.patch

b65758
From e2767839bc23f1a2f70543efabfe0ca1be166ee9 Mon Sep 17 00:00:00 2001
b65758
From: Rainer Gerhards <rgerhards@adiscon.com>
b65758
Date: Tue, 24 Jan 2017 13:24:29 +0100
b65758
Subject: [PATCH] rainescript: set/unset statement do not check variable name
b65758
 validity
b65758
b65758
Only JSON-based variables can be use with set and unset. Unfortunately,
b65758
this restriction is not checked. If an invalid variable is given
b65758
(e.g. $invalid), this is not detected upon config processing on
b65758
startup. During execution phase, this can lead to a segfault, a
b65758
memory leak or other types of problems.
b65758
b65758
see also https://github.com/rsyslog/rsyslog/issues/1376
b65758
closes https://github.com/rsyslog/rsyslog/issues/1377
b65758
---
b65758
 grammar/rainerscript.c | 43 +++++++++++++++++++++++++++++++++++++++----
b65758
 1 file changed, 39 insertions(+), 4 deletions(-)
b65758
b65758
diff --git a/grammar/rainerscript.c b/grammar/rainerscript.c
b65758
index 0ebd6f1..2106ef9 100644
b65758
--- a/grammar/rainerscript.c
b65758
+++ b/grammar/rainerscript.c
b65758
@@ -3062,6 +3062,19 @@ cnfstmtNew(unsigned s_type)
b65758
 	return cnfstmt;
b65758
 }
b65758
 
b65758
+/* This function disables a cnfstmt by setting it to NOP. This is
b65758
+ * useful when we detect errors late in the parsing processing, where
b65758
+ * we need to return a valid cnfstmt. The optimizer later removes the
b65758
+ * NOPs, so all is well.
b65758
+ * NOTE: this call assumes that no dynamic data structures have been
b65758
+ * allocated. If so, these MUST be freed before calling cnfstmtDisable().
b65758
+ */
b65758
+static void
b65758
+cnfstmtDisable(struct cnfstmt *cnfstmt)
b65758
+{
b65758
+	cnfstmt->nodetype = S_NOP;
b65758
+}
b65758
+
b65758
 void cnfstmtDestructLst(struct cnfstmt *root);
b65758
 
b65758
 static void cnfIteratorDestruct(struct cnfitr *itr);
b65758
@@ -3166,11 +3179,22 @@ cnfIteratorDestruct(struct cnfitr *itr)
b65758
 struct cnfstmt *
b65758
 cnfstmtNewSet(char *var, struct cnfexpr *expr, int force_reset)
b65758
 {
b65758
+	propid_t propid;
b65758
 	struct cnfstmt* cnfstmt;
b65758
 	if((cnfstmt = cnfstmtNew(S_SET)) != NULL) {
b65758
-		cnfstmt->d.s_set.varname = (uchar*) var;
b65758
-		cnfstmt->d.s_set.expr = expr;
b65758
-		cnfstmt->d.s_set.force_reset = force_reset;
b65758
+		if(propNameToID((uchar *)var, &propid) == RS_RET_OK
b65758
+		   && (   propid == PROP_CEE
b65758
+		       || propid == PROP_LOCAL_VAR
b65758
+		       || propid == PROP_GLOBAL_VAR)
b65758
+		   ) {
b65758
+			cnfstmt->d.s_set.varname = (uchar*) var;
b65758
+			cnfstmt->d.s_set.expr = expr;
b65758
+			cnfstmt->d.s_set.force_reset = force_reset;
b65758
+		} else {
b65758
+			parser_errmsg("invalid variable '%s' in set statement.", var);
b65758
+			free(var);
b65758
+			cnfstmtDisable(cnfstmt);
b65758
+		}
b65758
 	}
b65758
 	return cnfstmt;
b65758
 }
b65758
@@ -3254,9 +3278,20 @@ cnfstmtNewReloadLookupTable(struct cnffparamlst *fparams)
b65758
 struct cnfstmt *
b65758
 cnfstmtNewUnset(char *var)
b65758
 {
b65758
+	propid_t propid;
b65758
 	struct cnfstmt* cnfstmt;
b65758
 	if((cnfstmt = cnfstmtNew(S_UNSET)) != NULL) {
b65758
-		cnfstmt->d.s_unset.varname = (uchar*) var;
b65758
+		if(propNameToID((uchar *)var, &propid) == RS_RET_OK
b65758
+		   && (   propid == PROP_CEE
b65758
+		       || propid == PROP_LOCAL_VAR
b65758
+		       || propid == PROP_GLOBAL_VAR)
b65758
+		   ) {
b65758
+			cnfstmt->d.s_unset.varname = (uchar*) var;
b65758
+		} else {
b65758
+			parser_errmsg("invalid variable '%s' in unset statement.", var);
b65758
+			free(var);
b65758
+			cnfstmtDisable(cnfstmt);
b65758
+		}
b65758
 	}
b65758
 	return cnfstmt;
b65758
 }