Skip to contentSkip to conversation

libselinux: fix incorrect regexp anchoring

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

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v4 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.

Move the anchoring into the regex backend. For PCRE2, set PCRE2_EXTRA_MATCH_LINE (as used by pcre2grep -x and GNU grep -Px):
pcre2 wraps the pattern in ^(?:...)$ after parsing it, so a top-level | is grouped without shifting user backreference numbers, an unmatched parenthesis is still rejected with an offset that points into the input, and mode changes like (?x)#... or an unterminated \Q inside the input cannot swallow the appended anchor. Set PCRE2_DOLLAR_ENDONLY so the injected $ matches only at the end of the
subject. PCRE2_EXTRA_MATCH_LINE composes with PCRE2_PARTIAL_SOFT and with JIT compilation, so selabel_partial_match() is unaffected. For the legacy PCRE1 back end, anchor with ^(?:...)$ around the input string.

compile_regex() no longer needs to build an anchored copy of the string.

---
v4 follows further suggestions from the issue reporter to use PCRE2_EXTRA_MATCH_LINE, thereby obviating the need for building an anchored copy of the regex string in the PCRE2 case. It therefore moves the anchoring into the regex backend where we can handle it differently for PCRE1 vs PCRE2. It also updates the selabel_file.5 man page accordingly.

 libselinux/man/man5/selabel_file.5 |  8 +++-
 libselinux/src/label_file.h        | 23 ++--------
 libselinux/src/regex.c             | 73 ++++++++++++++++++++++++++++--
 libselinux/src/regex.h             |  3 ++
 4 files changed, 83 insertions(+), 24 deletions(-)
libselinux/man/man5/selabel_file.5 +6 -2
index 679a3f0e..34a17351 100644
@@ -129,8 +129,12 @@ An entry that defines the path to be labeled.
 May contain either a fully qualified path,
 or a Perl compatible regular expression (PCRE),
 describing fully qualified path(s).
-The only PCRE flag in use is PCRE2_DOTALL,
-which causes a wildcard '.' to match anything, including a new line.
+The regular expression is anchored at both ends,
+so the whole path must match; a leading ^ or trailing $ is not required.
+The PCRE flags in use are PCRE2_DOTALL,
+which causes a wildcard '.' to match anything, including a new line,
+PCRE2_DOLLAR_ENDONLY, and PCRE2_MATCH_EXTRA_MATCH_LINE,
+which apply the anchoring.
 Strings representing paths are processed as bytes (as opposed to Unicode),
 meaning that non-ASCII characters are not matched by a single wildcard.
 .RE
libselinux/src/label_file.h +5 -18
index a42e2aef..a4f1d4b5 100644
@@ -403,7 +403,6 @@ static int compile_regex(struct regex_spec *spec, char *errbuf,
 			 size_t errbuf_size, bool jit)
 {
 	const char *reg_buf;
-	char *anchored_regex, *cp;
 	struct regex_error_data error_data;
 	size_t len;
 	int rc;
@@ -449,7 +448,6 @@ static int compile_regex(struct regex_spec *spec, char *errbuf,
 	}
 
 	reg_buf = spec->regex_str;
-	/* Anchor the regular expression. */
 	len = strlen(reg_buf);
 	/* Use a sufficient large upper bound for regular expression lengths
 	 * to limit the compilation time on malformed inputs. */
@@ -460,23 +458,12 @@ static int compile_regex(struct regex_spec *spec, char *errbuf,
 		errno = EINVAL;
 		return -1;
 	}
-	cp = anchored_regex = malloc(len + 3);
-	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++ = '$';
-	*cp = '\0';
-
-	/* Compile the regular expression. */
-	rc = regex_prepare_data(&spec->regex, anchored_regex, &error_data, jit);
-	free(anchored_regex);
+	/*
+	 * Compile the regular expression; anchoring is applied by the
+	 * regex back end.
+	 */
+	rc = regex_prepare_data(&spec->regex, reg_buf, &error_data, jit);
 	if (rc < 0) {
 		regex_format_error(&error_data, errbuf, errbuf_size);
 		__pthread_mutex_unlock(&spec->regex_lock);
libselinux/src/regex.c +69 -4

119 lines not on this page · raw message

libselinux/src/regex.h +3 -0
index 4cda7bfc..f5499fad 100644
@@ -71,6 +71,9 @@ void regex_data_free(struct regex_data *regex);
  * a pcre2_match_data structure of appropriate size to hold all possible
  * matches created by the pattern.
  *
+ * The pattern is anchored to match the whole subject string, so callers
+ * must not add their own ^/$ or \A/\z.
+ *
  * @arg regex If successful, the structure returned through *regex was allocated
  *            with regex_data_create and must be freed with regex_data_free.
  * @arg pattern_string The pattern string that is to be compiled.
Signature · 1 line
2.55.0