BUG/MEDIUM: cfgcond: limit recursion level in the condition expression parser

Oss-fuzz reports in issue 36328 that we can recurse too far by passing
extremely deep expressions to the ".if" parser. I thought we were still
limited to the 1024 chars per line, that would be highly sufficient, but
we don't have any limit now :-/

Let's just pass a maximum recursion counter to the recursive parsers.
It's decremented for each call and the expression fails if it reaches
zero. On the most complex paths it can add 3 levels per parenthesis,
so with a limit of 1024, that's roughly 343 nested sub-expressions that
are supported in the worst case. That's more than sufficient, for just
a few kB of RAM.

No backport is needed.
diff --git a/src/cfgcond.c b/src/cfgcond.c
index c1259c0..50ba3df 100644
--- a/src/cfgcond.c
+++ b/src/cfgcond.c
@@ -68,9 +68,11 @@
  * untouched on failure. On success, the caller must free <term> using
  * cfg_free_cond_term(). An error will be set in <err> on error, and only
  * in this case. In this case the first bad character will be reported in
- * <errptr>.
+ * <errptr>. <maxdepth> corresponds to the maximum recursion depth permitted,
+ * it is decremented on each recursive call and the parsing will fail one
+ * reaching <= 0.
  */
-int cfg_parse_cond_term(const char **text, struct cfg_cond_term **term, char **err, const char **errptr)
+int cfg_parse_cond_term(const char **text, struct cfg_cond_term **term, char **err, const char **errptr, int maxdepth)
 {
 	struct cfg_cond_term *t;
 	const char *in = *text;
@@ -86,6 +88,10 @@
 	if (!*in) /* empty term does not parse */
 		return 0;
 
+	*term = NULL;
+	if (maxdepth <= 0)
+		goto fail0;
+
 	t = *term = calloc(1, sizeof(**term));
 	if (!t) {
 		memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text);
@@ -117,7 +123,7 @@
 		t->args = NULL;
 
 		do { in++; } while (*in == ' ' || *in == '\t');
-		ret = cfg_parse_cond_expr(&in, &t->expr, err, errptr);
+		ret = cfg_parse_cond_expr(&in, &t->expr, err, errptr, maxdepth - 1);
 		if (ret == -1)
 			goto fail2;
 		if (ret == 0)
@@ -275,9 +281,11 @@
  * on failure. On success, the caller will have to free all lower-level
  * allocated structs using cfg_free_cond_expr(). An error will be set in
  * <err> on error, and only in this case. In this case the first bad
- * character will be reported in <errptr>.
+ * character will be reported in <errptr>. <maxdepth> corresponds to the
+ * maximum recursion depth permitted, it is decremented on each recursive
+ * call and the parsing will fail one reaching <= 0.
  */
-int cfg_parse_cond_and(const char **text, struct cfg_cond_and **expr, char **err, const char **errptr)
+int cfg_parse_cond_and(const char **text, struct cfg_cond_and **expr, char **err, const char **errptr, int maxdepth)
 {
 	struct cfg_cond_and *e;
 	const char *in = *text;
@@ -286,13 +294,21 @@
 	if (!*in) /* empty expr does not parse */
 		return 0;
 
+	*expr = NULL;
+	if (maxdepth <= 0) {
+		memprintf(err, "unparsable conditional sub-expression '%s'", in);
+		if (errptr)
+			*errptr = in;
+		goto done;
+	}
+
 	e = *expr = calloc(1, sizeof(**expr));
 	if (!e) {
 		memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text);
 		goto done;
 	}
 
-	ret = cfg_parse_cond_term(&in, &e->left, err, errptr);
+	ret = cfg_parse_cond_term(&in, &e->left, err, errptr, maxdepth - 1);
 	if (ret == -1) // parse error, error already reported
 		goto done;
 
@@ -320,7 +336,7 @@
 	while (*in == ' ' || *in == '\t')
 		in++;
 
-	ret = cfg_parse_cond_and(&in, &e->right, err, errptr);
+	ret = cfg_parse_cond_and(&in, &e->right, err, errptr, maxdepth - 1);
 	if (ret > 0)
 		*text = in;
  done:
@@ -338,9 +354,11 @@
  * on failure. On success, the caller will have to free all lower-level
  * allocated structs using cfg_free_cond_expr(). An error will be set in
  * <err> on error, and only in this case. In this case the first bad
- * character will be reported in <errptr>.
+ * character will be reported in <errptr>. <maxdepth> corresponds to the
+ * maximum recursion depth permitted, it is decremented on each recursive call
+ * and the parsing will fail one reaching <= 0.
  */
-int cfg_parse_cond_expr(const char **text, struct cfg_cond_expr **expr, char **err, const char **errptr)
+int cfg_parse_cond_expr(const char **text, struct cfg_cond_expr **expr, char **err, const char **errptr, int maxdepth)
 {
 	struct cfg_cond_expr *e;
 	const char *in = *text;
@@ -349,13 +367,21 @@
 	if (!*in) /* empty expr does not parse */
 		return 0;
 
+	*expr = NULL;
+	if (maxdepth <= 0) {
+		memprintf(err, "unparsable conditional expression '%s'", in);
+		if (errptr)
+			*errptr = in;
+		goto done;
+	}
+
 	e = *expr = calloc(1, sizeof(**expr));
 	if (!e) {
 		memprintf(err, "memory allocation error while parsing conditional expression '%s'", *text);
 		goto done;
 	}
 
-	ret = cfg_parse_cond_and(&in, &e->left, err, errptr);
+	ret = cfg_parse_cond_and(&in, &e->left, err, errptr, maxdepth - 1);
 	if (ret == -1) // parse error, error already reported
 		goto done;
 
@@ -383,7 +409,7 @@
 	while (*in == ' ' || *in == '\t')
 		in++;
 
-	ret = cfg_parse_cond_expr(&in, &e->right, err, errptr);
+	ret = cfg_parse_cond_expr(&in, &e->right, err, errptr, maxdepth - 1);
 	if (ret > 0)
 		*text = in;
  done:
@@ -440,7 +466,7 @@
 	if (!*text) /* note: empty = false */
 		return 0;
 
-	ret = cfg_parse_cond_expr(&text, &expr, err, errptr);
+	ret = cfg_parse_cond_expr(&text, &expr, err, errptr, MAX_CFG_RECURSION);
 	if (ret != 0) {
 		if (ret == -1) // parse error, error already reported
 			goto done;