Skip to contentSkip to conversation

libselinux: fix incorrect regexp anchoring

PATCH Superseded Stephen Smalley 1 participant 2 messages Sep 15 SELinux security module

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v3 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(?:...\E)\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. The \E before the closing ) is a no-op unless the input ends inside an open \Q, in which case it closes the quoting so the added ) and \z are not swallowed. 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 parenthesis" would now compile as a different pattern. Compile the un-anchored input once first and report any error from that; this also gives error offsets that point into what the user wrote rather than into the wrapped string. On a validating open of a refpolicy-sized file_contexts (~5800 entries) the extra compile costs ~3-4ms.

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().

---
v3 drops the custom regex parenthesis matching function and just performs a regex_prepare_data() on the original regex string before anchoring to provide full validation and accurate error reporting, and adds a \E before the closing ) to prevents a \Q in the input from swallowing the suffix.

 libselinux/src/label_file.h | 34 ++++++++++++++++++++++++++++------
 libselinux/src/regex.c      |  6 ++++--
 2 files changed, 32 insertions(+), 8 deletions(-)
libselinux/src/label_file.h +28 -6
index a42e2aef..01f53238 100644
@@ -405,6 +405,7 @@ static int compile_regex(struct regex_spec *spec, char *errbuf,
 	const char *reg_buf;
 	char *anchored_regex, *cp;
 	struct regex_error_data error_data;
+	struct regex_data *tmp_regex;
 	size_t len;
 	int rc;
 	bool regex_compiled;
@@ -460,19 +461,40 @@ static int compile_regex(struct regex_spec *spec, char *errbuf,
 		errno = EINVAL;
 		return -1;
 	}
-	cp = anchored_regex = malloc(len + 3);
+
+	/*
+	 * Compile the input as-is once, so an entry that is invalid on its
+	 * own (e.g. unmatched ')') is rejected here rather than being
+	 * silently made valid by the (?:...) added below. Report the error
+	 * against the unanchored string so the offset points into what the
+	 * user wrote.
+	 */
+	rc = regex_prepare_data(&tmp_regex, reg_buf, &error_data, false);
+	if (rc < 0) {
+		regex_format_error(&error_data, errbuf, errbuf_size);
+		__pthread_mutex_unlock(&spec->regex_lock);
+		errno = EINVAL;
+		return -1;
+	}
+	regex_data_free(tmp_regex);
+
+#define REPREFIX "\\A(?:"
+#define RESUFFIX "\\E)\\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
author maintainer
#1 lore raw
Quoted 30 lines · Stephen Smalley

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(?:...\E)\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. The \E before the closing ) is a no-op unless the input ends inside an open \Q, in which case it closes the quoting so the added ) and \z are not swallowed. 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 parenthesis" would now compile as a different pattern. Compile the un-anchored input once first and report any error from that; this also gives error offsets that point into what the user wrote rather than into the wrapped string. On a validating open of a refpolicy-sized file_contexts (~5800 entries) the extra compile costs ~3-4ms.

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().

Quoted 8 lines

---
v3 drops the custom regex parenthesis matching function and just performs a regex_prepare_data() on the original regex string before anchoring to provide full validation and accurate error reporting, and adds a \E before the closing ) to prevents a \Q in the input from swallowing the suffix.

The reporter suggested using pcre2_set_compile_extra_options(PCRE2_EXTRA_MATCH_LINE) as used by GNU grep -Px and pcre2grep -x instead so I will follow up with a v4 after further research and experimentation.