libselinux: fix incorrect regexp anchoring
Stephen Smalley
maintainerPATCH v6 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 as \A(?:...\E)\z so a top-level | stays inside the anchors, a trailing newline in the subject is not matched, and a pattern ending inside \Q does not swallow the closing group and anchor. PCRE1 has no equivalent of PCRE2_EXTRA_MATCH_LINE, so an (?x)#... comment at the end of the pattern will still consume the suffix; that case is left unhandled on this legacy path.
compile_regex() no longer needs to build an anchored copy of the string.
---
v6 fixes the missing initialization of cp in the pcre1 code.
libselinux/man/man5/selabel_file.5 | 8 ++- libselinux/src/label_file.h | 23 ++------- libselinux/src/regex.c | 78 ++++++++++++++++++++++++++++-- libselinux/src/regex.h | 3 ++ 4 files changed, 88 insertions(+), 24 deletions(-)
libselinux/man/man5/selabel_file.5 +6 -2
@@ -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
@@ -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 +74 -4
124 lines not on this page · raw message
libselinux/src/regex.h +3 -0
@@ -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