Skip to contentSkip to conversation

libselinux: fix incorrect regexp anchoring

PATCH Superseded Stephen Smalley 1 participant 1 message Sep 14 SELinux security module

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v2 1/1 libselinux: fix incorrect regexp anchoring

As identified in #544, libselinux anchors the pathname regexes by prepending a ^ and appending a $, which will produce the wrong result for a regexp like /foo|/bar. #544 also notes that we should be using PCRE2_DOLLAR_ENDONLY/PCRE_DOLLAR_ENDONLY when compiling the regexp to avoid matching a pathname with a trailing newline.

Wrap the input in \A(?:...)\z instead: the non-capturing group keeps a top-level | inside the anchors without shifting user backreference numbers, and \z (unlike $) never matches before a trailing newline. PCRE2_DOLLAR_ENDONLY is set as well so that any $ inside the input pattern is tightened the same way.

Because the added (?: and ) can pair with an unmatched ) or ( in the input, an entry that used to fail pcre2_compile() with "unmatched closing parentheis" would now compile as a different pattern. Reject an unmatched ) before wrapping so that malformed input keeps failing; an unmatched ( still fails inside pcre2_compile() on the wrapped string.

PCRE2_ANCHORED|PCRE2_ENDANCHORED at compile time would avoid the wrapping entirely, but a pattern compiled with PCRE2_ENDANCHORED returns PCRE2_ERROR_BADOPTION for PCRE2_PARTIAL_SOFT and passing PCRE2_ENDANCHORED at match time bypasses JIT, so neither is usable here given selabel_partial_match().

---
v2 rejects an unmatched ')' before wrapping so an entry that failed pcre2_compile() before does not silently start compiling as a different pattern, explains why PCRE2_ANCHORED/ENDANCHORED cannot be used, and #undef's REPREFIX/RESUFFIX after use.

 libselinux/src/label_file.h | 68 +++++++++++++++++++++++++++++++++----
 libselinux/src/regex.c      |  6 ++--
 2 files changed, 66 insertions(+), 8 deletions(-)
libselinux/src/label_file.h +62 -6
index a42e2aef..8e1f1932 100644
@@ -399,6 +399,50 @@ static inline void sort_specs(struct saved_data *data)
 	sort_spec_node(data->root, NULL);
 }
 
+static bool regex_no_unmatched_close_paren(const char *regex)
+{
+	const char *p;
+	int depth = 0;
+
+	for (p = regex; *p; p++) {
+		switch (*p) {
+		case '\\':
+			if (p[1])
+				p++;
+			break;
+		case '[':
+			p++;
+			if (*p == '^')
+				p++;
+			if (*p == ']')
+				p++;
+			for (; *p && *p != ']'; p++) {
+				if (*p == '\\' && p[1])
+					p++;
+				else if (p[0] == '[' && p[1] == ':') {
+					const char *q = strstr(p + 2, ":]");
+
+					if (q)
+						p = q + 1;
+				}
+			}
+			if (!*p)
+				return true;
+			break;
+		case '(':
+			depth++;
+			break;
+		case ')':
+			if (depth == 0)
+				return false;
+			depth--;
+			break;
+		}
+	}
+
+	return true;
+}
+
 static int compile_regex(struct regex_spec *spec, char *errbuf,
 			 size_t errbuf_size, bool jit)
 {
@@ -460,19 +504,31 @@ static int compile_regex(struct regex_spec *spec, char *errbuf,
 		errno = EINVAL;
 		return -1;
 	}
-	cp = anchored_regex = malloc(len + 3);
+
+	if (!regex_no_unmatched_close_paren(reg_buf)) {
+		__pthread_mutex_unlock(&spec->regex_lock);
+		snprintf(errbuf, errbuf_size, "unmatched closing parenthesis");
+		errno = EINVAL;
+		return -1;
+	}
+
+#define REPREFIX "\\A(?:"
+#define RESUFFIX ")\\z"
+	cp = anchored_regex =
+		malloc(len + strlen(REPREFIX) + strlen(RESUFFIX) + 1);
 	if (!anchored_regex) {
 		__pthread_mutex_unlock(&spec->regex_lock);
 		snprintf(errbuf, errbuf_size, "out of memory");
 		return -1;
 	}
 
-	/* Create ^...$ regexp.  */
-	*cp++ = '^';
-	memcpy(cp, reg_buf, len);
-	cp += len;
-	*cp++ = '$';
+	/* Create anchored regexp.  */
+	cp = mempcpy(cp, REPREFIX, strlen(REPREFIX));
+	cp = mempcpy(cp, reg_buf, len);
+	cp = mempcpy(cp, RESUFFIX, strlen(RESUFFIX));
 	*cp = '\0';
+#undef REPREFIX
+#undef RESUFFIX
 
 	/* Compile the regular expression. */
 	rc = regex_prepare_data(&spec->regex, anchored_regex, &error_data, jit);
libselinux/src/regex.c +4 -2
index d6b5bf02..a76f29ce 100644
@@ -96,7 +96,8 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
 		return -1;
 
 	(*regex)->regex = pcre2_compile((PCRE2_SPTR)pattern_string,
-					PCRE2_ZERO_TERMINATED, PCRE2_DOTALL,
+					PCRE2_ZERO_TERMINATED,
+					PCRE2_DOTALL | PCRE2_DOLLAR_ENDONLY,
 					&errordata->error_code,
 					&errordata->error_offset, NULL);
 	if (!(*regex)->regex) {
@@ -379,7 +380,8 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
 	if (!(*regex))
 		return -1;
 
-	(*regex)->regex = pcre_compile(pattern_string, PCRE_DOTALL,
+	(*regex)->regex = pcre_compile(pattern_string,
+				       PCRE_DOTALL | PCRE_DOLLAR_ENDONLY,
 				       &errordata->error_buffer,
 				       &errordata->error_offset, NULL);
 	if (!(*regex)->regex)
Signature · 1 line
2.55.0