Skip to contentSkip to conversation

libselinux: prevent ReDoS in file context regex matching

PATCH Superseded Petr Matyas 3 participants 17 messages Jul 29 – Aug 24 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libselinux: prevent ReDoS in file context regex matching

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3) or pcre2_jit_match(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) to engage JIT for both complete and partial matching. regex_match() now calls pcre2_jit_match() and falls back to pcre2_match() only on PCRE2_ERROR_JIT_BADOPTION (JIT unavailable or not compiled for the requested mode).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to both pcre2_jit_match() and the fallback pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

---
 libselinux/src/regex.c | 40 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 37 insertions(+), 3 deletions(-)
libselinux/src/regex.c +37 -3
index 7a4c5ecc..dca2353c 100644
@@ -41,8 +41,17 @@ static pthread_once_t match_data_key_once = PTHREAD_ONCE_INIT;
 static int match_data_key_alloc_failed = 0;
 static int match_data_key_created = 0;
 static pthread_once_t once = PTHREAD_ONCE_INIT;
+static pcre2_match_context *match_context;
 static char arch_string_buffer[32];
 
+/*
+ * Limit the number of backtrack steps to prevent catastrophic backtracking
+ * (ReDoS) from crafted file context patterns or lookup keys. 10 million steps
+ * is generous for legitimate file context patterns while bounding worst-case
+ * matching time to milliseconds.
+ */
+#define REGEX_MATCH_LIMIT 10000000U
+
 static void regex_arch_string_init(void)
 {
 	char const *endianness;
@@ -94,6 +103,12 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
 		goto err;
 	}
 
+	/* JIT-compile for complete and partial matching to avoid catastrophic
+	 * backtracking in the PCRE2 interpreter. Failures are non-fatal:
+	 * regex_match() falls back to pcre2_match() on PCRE2_ERROR_JIT_BADOPTION. */
+	(void)pcre2_jit_compile((*regex)->regex,
+				PCRE2_JIT_COMPLETE | PCRE2_JIT_PARTIAL_SOFT);
+
 	return 0;
 
 err:
@@ -148,6 +163,10 @@ int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex,
 		if (rc != 1)
 			goto err;
 
+		(void)pcre2_jit_compile((*regex)->regex,
+					PCRE2_JIT_COMPLETE |
+						PCRE2_JIT_PARTIAL_SOFT);
+
 		*regex_compiled = true;
 	}
 
@@ -212,10 +231,18 @@ static void match_data_thread_free(void *ptr)
 
 static void match_data_key_init(void)
 {
+	pcre2_match_context *mctx;
+
 	if (__selinux_key_create(&match_data_key, match_data_thread_free) == 0)
 		match_data_key_created = 1;
 	else
 		match_data_key_alloc_failed = 1;
+
+	mctx = pcre2_match_context_create(NULL);
+	if (mctx) {
+		pcre2_set_match_limit(mctx, REGEX_MATCH_LIMIT);
+		match_context = mctx;
+	}
 }
 
 static void __attribute__((destructor)) match_data_key_destroy(void)
@@ -262,9 +289,15 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 			return REGEX_ERROR;
 	}
 
-	rc = pcre2_match(regex->regex, (PCRE2_SPTR)subject,
-			 PCRE2_ZERO_TERMINATED, 0,
-			 partial ? PCRE2_PARTIAL_SOFT : 0, match_data, NULL);
+	rc = pcre2_jit_match(regex->regex, (PCRE2_SPTR)subject,
+			     PCRE2_ZERO_TERMINATED, 0,
+			     partial ? PCRE2_PARTIAL_SOFT : 0, match_data,
+			     match_context);
+	if (rc == PCRE2_ERROR_JIT_BADOPTION)
+		rc = pcre2_match(regex->regex, (PCRE2_SPTR)subject,
+				 PCRE2_ZERO_TERMINATED, 0,
+				 partial ? PCRE2_PARTIAL_SOFT : 0, match_data,
+				 match_context);
 
 	if (slow)
 		pcre2_match_data_free(match_data);
@@ -275,6 +308,7 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 	case PCRE2_ERROR_PARTIAL:
 		return REGEX_MATCH_PARTIAL;
 	case PCRE2_ERROR_NOMATCH:
+	case PCRE2_ERROR_MATCHLIMIT:
 		return REGEX_NO_MATCH;
 	default:
 		return REGEX_ERROR;
Signature · 1 line
2.55.0
#1 lore raw

PATCH 2/2 libselinux: fix infinite loop and missing errno in partial <<none>> lookup

lookup_check_node() has a do-while loop that iterates over matching literal specs. When partial=true and a matching spec has <<none>> context the loop body used continue to skip to the next entry. In a do-while, continue jumps to the condition rather than the loop increment, so literal_idx was never advanced and the loop spun forever.

Fix by explicitly incrementing literal_idx before the continue so the loop moves to the next literal spec as intended.

Additionally, set errno = ENOENT before continuing so that callers which test errno after a NULL return (such as the selabel_file_text fuzzer) see a valid error code. Without this, lookup_all() returned NULL with errno still 0, violating the expected contract that a NULL result is always accompanied by a non-zero errno.

---
 libselinux/src/label_file.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
libselinux/src/label_file.c +5 -3
index 63066228..d716e0f1 100644
@@ -1936,10 +1936,12 @@ lookup_check_node(struct spec_node *node, const char *key, uint8_t file_kind,
 
 						if (strcmp(lspec->lr.ctx_raw,
 							   "<<none>>") == 0) {
-							if (partial)
+							errno = ENOENT;
+							if (partial) {
+								literal_idx++;
 								continue;
+							}
 
-							errno = ENOENT;
 							goto fail;
 						}
 
@@ -2025,10 +2027,10 @@ lookup_check_node(struct spec_node *node, const char *key, uint8_t file_kind,
 
 				if (strcmp(rspec->lr.ctx_raw, "<<none>>") ==
 				    0) {
+					errno = ENOENT;
 					if (partial)
 						continue;
 
-					errno = ENOENT;
 					goto fail;
 				}
 
Signature · 1 line
2.55.0
maintainer Acked-by on v1 2/2
#2 lore raw

Re: [PATCH 2/2] libselinux: fix infinite loop and missing errno in partial <<none>> lookup

Quoted 11 lines · Petr Matyas

lookup_check_node() has a do-while loop that iterates over matching literal specs. When partial=true and a matching spec has <<none>> context the loop body used continue to skip to the next entry. In a do-while, continue jumps to the condition rather than the loop increment, so literal_idx was never advanced and the loop spun forever.

Fix by explicitly incrementing literal_idx before the continue so the loop moves to the next literal spec as intended.

Quoted 7 lines

Additionally, set errno = ENOENT before continuing so that callers which test errno after a NULL return (such as the selabel_file_text fuzzer) see a valid error code. Without this, lookup_all() returned NULL with errno still 0, violating the expected contract that a NULL result is always accompanied by a non-zero errno.

maintainer
#3 lore raw

Re: [PATCH 2/2] libselinux: fix infinite loop and missing errno in partial <<none>> lookup

Quoted 19 lines · Stephen Smalley
Quoted 17 lines · Petr Matyas

lookup_check_node() has a do-while loop that iterates over matching literal specs. When partial=true and a matching spec has <<none>> context the loop body used continue to skip to the next entry. In a do-while, continue jumps to the condition rather than the loop increment, so literal_idx was never advanced and the loop spun forever.

Fix by explicitly incrementing literal_idx before the continue so the loop moves to the next literal spec as intended.

Additionally, set errno = ENOENT before continuing so that callers which test errno after a NULL return (such as the selabel_file_text fuzzer) see a valid error code. Without this, lookup_all() returned NULL with errno still 0, violating the expected contract that a NULL result is always accompanied by a non-zero errno.

Quoted 3 lines
Quoted 1 line

This patch is merged.

maintainer
#4 lore raw
In reply to Petr Matyas
Quoted 26 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3) or pcre2_jit_match(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) to engage JIT for both complete and partial matching. regex_match() now calls pcre2_jit_match() and falls back to pcre2_match() only on PCRE2_ERROR_JIT_BADOPTION (JIT unavailable or not compiled for the requested mode).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to both pcre2_jit_match() and the fallback pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Quoted 6 lines

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

Thanks for the report and patch. Given that file_contexts* is root-owned and the caller provides the paths
to lookup, this is only self-DoS. Will fix but don't think it warrants a security advisory; let us know if you
think differently. See below though for one comment on the code.

Quoted 7 lines

---

libselinux/src/regex.c +0 -2
@@ -262,9 +289,15 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
                        return REGEX_ERROR;
        }
 
-       rc = pcre2_match(regex->regex, (PCRE2_SPTR)subject,
-                        PCRE2_ZERO_TERMINATED, 0,

libselinux/src/regex.c int regex_match(struct regex_data *regex, char const *subject, int partial) · line 300

Quoted 10 lines
libselinux/src/regex.c +9 -1
-                        partial ? PCRE2_PARTIAL_SOFT : 0, match_data, NULL);
+       rc = pcre2_jit_match(regex->regex, (PCRE2_SPTR)subject,
+                            PCRE2_ZERO_TERMINATED, 0,
+                            partial ? PCRE2_PARTIAL_SOFT : 0, match_data,
+                            match_context);
+       if (rc == PCRE2_ERROR_JIT_BADOPTION)
+               rc = pcre2_match(regex->regex, (PCRE2_SPTR)subject,
+                                PCRE2_ZERO_TERMINATED, 0,
+                                partial ? PCRE2_PARTIAL_SOFT : 0, match_data,
+                                match_context);

You only fall back to pcre2_match() on PCRE2_ERROR_JIT_BADOPTION, so if we hit PCRE2_ERROR_JIT_STACKLIMIT
the lookup will fail rather than falling back to interpreted.

#5 lore raw

PATCH v2 1/1 libselinux: prevent ReDoS in file context regex matching

In reply to Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3) or pcre2_jit_match(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) to engage JIT for both complete and partial matching. regex_match() now calls pcre2_jit_match() and falls back to pcre2_match() only on PCRE2_ERROR_JIT_BADOPTION (JIT unavailable or not compiled for the requested mode).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to both pcre2_jit_match() and the fallback pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

libsepol/cil: Return SEPOL_ERR on failure to set bit

The functions cil_typepermissive_to_policydb() and cil_typeneveraudit_to_policydb() return rc when ebitmap_set_bit() fails, but the rc value at the point is SEPOL_OK.

---
 libselinux/src/regex.c | 40 +++++++++++++++++++++++++++++++++++++---
 1 file changed, 37 insertions(+), 3 deletions(-)
libselinux/src/regex.c +37 -3
index 7a4c5ecc..af4c4328 100644
@@ -41,8 +41,17 @@ static pthread_once_t match_data_key_once = PTHREAD_ONCE_INIT;
 static int match_data_key_alloc_failed = 0;
 static int match_data_key_created = 0;
 static pthread_once_t once = PTHREAD_ONCE_INIT;
+static pcre2_match_context *match_context;
 static char arch_string_buffer[32];
 
+/*
+ * Limit the number of backtrack steps to prevent catastrophic backtracking
+ * (ReDoS) from crafted file context patterns or lookup keys. 10 million steps
+ * is generous for legitimate file context patterns while bounding worst-case
+ * matching time to milliseconds.
+ */
+#define REGEX_MATCH_LIMIT 10000000U
+
 static void regex_arch_string_init(void)
 {
 	char const *endianness;
@@ -94,6 +103,12 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
 		goto err;
 	}
 
+	/* JIT-compile for complete and partial matching to avoid catastrophic
+	 * backtracking in the PCRE2 interpreter. Failures are non-fatal:
+	 * regex_match() falls back to pcre2_match() on PCRE2_ERROR_JIT_BADOPTION. */
+	(void)pcre2_jit_compile((*regex)->regex,
+				PCRE2_JIT_COMPLETE | PCRE2_JIT_PARTIAL_SOFT);
+
 	return 0;
 
 err:
@@ -148,6 +163,10 @@ int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex,
 		if (rc != 1)
 			goto err;
 
+		(void)pcre2_jit_compile((*regex)->regex,
+					PCRE2_JIT_COMPLETE |
+						PCRE2_JIT_PARTIAL_SOFT);
+
 		*regex_compiled = true;
 	}
 
@@ -212,10 +231,18 @@ static void match_data_thread_free(void *ptr)
 
 static void match_data_key_init(void)
 {
+	pcre2_match_context *mctx;
+
 	if (__selinux_key_create(&match_data_key, match_data_thread_free) == 0)
 		match_data_key_created = 1;
 	else
 		match_data_key_alloc_failed = 1;
+
+	mctx = pcre2_match_context_create(NULL);
+	if (mctx) {
+		pcre2_set_match_limit(mctx, REGEX_MATCH_LIMIT);
+		match_context = mctx;
+	}
 }
 
 static void __attribute__((destructor)) match_data_key_destroy(void)
@@ -262,9 +289,15 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 			return REGEX_ERROR;
 	}
 
-	rc = pcre2_match(regex->regex, (PCRE2_SPTR)subject,
-			 PCRE2_ZERO_TERMINATED, 0,
-			 partial ? PCRE2_PARTIAL_SOFT : 0, match_data, NULL);
+	rc = pcre2_jit_match(regex->regex, (PCRE2_SPTR)subject,
+			     PCRE2_ZERO_TERMINATED, 0,
+			     partial ? PCRE2_PARTIAL_SOFT : 0, match_data,
+			     match_context);
+	if (rc == PCRE2_ERROR_JIT_BADOPTION || rc == PCRE2_ERROR_JIT_STACKLIMIT)
+		rc = pcre2_match(regex->regex, (PCRE2_SPTR)subject,
+				 PCRE2_ZERO_TERMINATED, 0,
+				 partial ? PCRE2_PARTIAL_SOFT : 0, match_data,
+				 match_context);
 
 	if (slow)
 		pcre2_match_data_free(match_data);
@@ -275,6 +308,7 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 	case PCRE2_ERROR_PARTIAL:
 		return REGEX_MATCH_PARTIAL;
 	case PCRE2_ERROR_NOMATCH:
+	case PCRE2_ERROR_MATCHLIMIT:
 		return REGEX_NO_MATCH;
 	default:
 		return REGEX_ERROR;
Signature · 1 line
2.55.0
maintainer
#6 lore raw
Quoted 31 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3) or pcre2_jit_match(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) to engage JIT for both complete and partial matching. regex_match() now calls pcre2_jit_match() and falls back to pcre2_match() only on PCRE2_ERROR_JIT_BADOPTION (JIT unavailable or not compiled for the requested mode).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to both pcre2_jit_match() and the fallback pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

Quoted 7 lines

libsepol/cil: Return SEPOL_ERR on failure to set bit

The functions cil_typepermissive_to_policydb() and cil_typeneveraudit_to_policydb() return rc when ebitmap_set_bit() fails, but the rc value at the point is SEPOL_OK.

Something went wrong with your commit message - seemingly squashed/merged with another one.
Also with this patch applied, I get a selinux-testsuite failure, see below for just running the individual
failing test:

$ cd selinux-testsuite
$ sudo make -C policy load
$ sudo ./tests/file_contexts/test
ok 1
internal.c:assertContextsMatch:24 Lookup for /base/unkown from
test_lookup failed (No such file or directory)
not ok 2
#   Failed test at ./tests/file_contexts/test line 27.
ok 3
./tests/file_contexts/f1.fc: line 1 has invalid context
system_u:object_r:rootfs:s0
./tests/file_contexts/f1.fc: line 1 has invalid context
system_u:object_r:rootfs:s0
ok 4
./tests/file_contexts/f1.fc: line 1 has invalid context
system_u:object_r:rootfs:s0
./tests/file_contexts/f1.fc: line 1 has invalid context
system_u:object_r:rootfs:s0
ok 5
# Looks like you failed 1 test of 5.
#7 lore raw

PATCH v3 1/1 libselinux: prevent ReDoS in file context regex matching

In reply to Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) for both complete and partial matching modes. pcre2_match() uses the JIT automatically when available, which avoids the interpreter's susceptibility to catastrophic backtracking.
Note: pcre2_jit_match() is deliberately NOT used as it is known to produce incorrect results on some platforms (e.g. aarch64).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

---
 libselinux/src/regex.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)
libselinux/src/regex.c +34 -1
index 7a4c5ecc..3f7df4c7 100644
@@ -41,8 +41,17 @@ static pthread_once_t match_data_key_once = PTHREAD_ONCE_INIT;
 static int match_data_key_alloc_failed = 0;
 static int match_data_key_created = 0;
 static pthread_once_t once = PTHREAD_ONCE_INIT;
+static pcre2_match_context *match_context;
 static char arch_string_buffer[32];
 
+/*
+ * Limit the number of backtrack steps to prevent catastrophic backtracking
+ * (ReDoS) from crafted file context patterns or lookup keys. 10 million steps
+ * is generous for legitimate file context patterns while bounding worst-case
+ * matching time to milliseconds.
+ */
+#define REGEX_MATCH_LIMIT 10000000U
+
 static void regex_arch_string_init(void)
 {
 	char const *endianness;
@@ -94,6 +103,12 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
 		goto err;
 	}
 
+	/* JIT-compile for complete and partial matching. pcre2_match() uses the
+	 * JIT automatically when available, avoiding the interpreter's
+	 * susceptibility to catastrophic backtracking. Failures are non-fatal. */
+	(void)pcre2_jit_compile((*regex)->regex,
+				PCRE2_JIT_COMPLETE | PCRE2_JIT_PARTIAL_SOFT);
+
 	return 0;
 
 err:
@@ -148,6 +163,10 @@ int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex,
 		if (rc != 1)
 			goto err;
 
+		(void)pcre2_jit_compile((*regex)->regex,
+					PCRE2_JIT_COMPLETE |
+						PCRE2_JIT_PARTIAL_SOFT);
+
 		*regex_compiled = true;
 	}
 
@@ -212,10 +231,18 @@ static void match_data_thread_free(void *ptr)
 
 static void match_data_key_init(void)
 {
+	pcre2_match_context *mctx;
+
 	if (__selinux_key_create(&match_data_key, match_data_thread_free) == 0)
 		match_data_key_created = 1;
 	else
 		match_data_key_alloc_failed = 1;
+
+	mctx = pcre2_match_context_create(NULL);
+	if (mctx) {
+		pcre2_set_match_limit(mctx, REGEX_MATCH_LIMIT);
+		match_context = mctx;
+	}
 }
 
 static void __attribute__((destructor)) match_data_key_destroy(void)
@@ -262,9 +289,14 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 			return REGEX_ERROR;
 	}
 
+	/* Use pcre2_match() rather than pcre2_jit_match(): pcre2_match()
+	 * automatically uses the JIT when the code has been compiled with
+	 * pcre2_jit_compile(), while pcre2_jit_match() is known to produce
+	 * incorrect results on some platforms (e.g. aarch64). */
 	rc = pcre2_match(regex->regex, (PCRE2_SPTR)subject,
 			 PCRE2_ZERO_TERMINATED, 0,
-			 partial ? PCRE2_PARTIAL_SOFT : 0, match_data, NULL);
+			 partial ? PCRE2_PARTIAL_SOFT : 0, match_data,
+			 match_context);
 
 	if (slow)
 		pcre2_match_data_free(match_data);
@@ -275,6 +307,7 @@ int regex_match(struct regex_data *regex, char const *subject, int partial)
 	case PCRE2_ERROR_PARTIAL:
 		return REGEX_MATCH_PARTIAL;
 	case PCRE2_ERROR_NOMATCH:
+	case PCRE2_ERROR_MATCHLIMIT:
 		return REGEX_NO_MATCH;
 	default:
 		return REGEX_ERROR;
Signature · 1 line
2.55.0
maintainer Acked-by on v3 1/1
#8 lore raw
Quoted 26 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) for both complete and partial matching modes. pcre2_match() uses the JIT automatically when available, which avoids the interpreter's susceptibility to catastrophic backtracking.
Note: pcre2_jit_match() is deliberately NOT used as it is known to produce incorrect results on some platforms (e.g. aarch64).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Quoted 6 lines

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

maintainer
#9 lore raw
Quoted 28 lines · Stephen Smalley
Quoted 26 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) for both complete and partial matching modes. pcre2_match() uses the JIT automatically when available, which avoids the interpreter's susceptibility to catastrophic backtracking.
Note: pcre2_jit_match() is deliberately NOT used as it is known to produce incorrect results on some platforms (e.g. aarch64).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Quoted 8 lines
Quoted 6 lines

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

Merged.

maintainer
#10 lore raw
Quoted 36 lines · Stephen Smalley
Quoted 33 lines · Stephen Smalley
Quoted 31 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) for both complete and partial matching modes. pcre2_match() uses the JIT automatically when available, which avoids the interpreter's susceptibility to catastrophic backtracking.
Note: pcre2_jit_match() is deliberately NOT used as it is known to produce incorrect results on some platforms (e.g. aarch64).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Merged.

This commit produces a new oss-fuzz issue report,
https://issues.oss-fuzz.com/issues/541525193
but quoting the stack trace below since you may lack access to the report:

Uninitialized bytes in MemcmpInterceptorCommon at offset 0 inside
[0x70b00000008c, 8)
==248==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x56474054202c in ___interceptor_memcmp /src/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc:880:10
#1 0x793bcb92efb3 in libpcre2-8.so.0
#2 0x793bcb94ecf1 in libpcre2-8.so.0
#3 0x793bcb952edc in pcre2_jit_compile_8
#4 0x5647405c1509 in regex_prepare_data selinux/libselinux/src/regex.c:109:8
#5 0x5647405b4828 in compile_regex selinux/libselinux/src/label_file.h:476:7
#6 0x5647405a04b5 in insert_spec selinux/libselinux/src/label_file.h:657:8
#7 0x5647405a04b5 in process_line selinux/libselinux/src/label_file.h:898:9
#8 0x5647405a04b5 in process_text_file selinux/libselinux/src/label_file.c:226:8
#9 0x56474059baa1 in LLVMFuzzerTestOneInput
selinux/libselinux/fuzz/selabel_file_text-fuzzer.c:171:7
#10 0x56474048522d in fuzzer::Fuzzer::ExecuteCallback(unsigned char
const*, unsigned long)
/src/llvm-project/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:619:13
#11 0x56474046ffa2 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*,
unsigned long) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:329:6
#12 0x564740475e70 in fuzzer::FuzzerDriver(int*, char***, int
(*)(unsigned char const*, unsigned long))
/src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:865:9
#13 0x5647404a19a2 in main
/src/llvm-project/compiler-rt/lib/fuzzer/FuzzerMain.cpp:20:10
#14 0x793bcb580082 in __libc_start_main
/build/glibc-B3wQXB/glibc-2.31/csu/libc-start.c:308:16
#15 0x56474046908d in _start
Uninitialized value was created by a heap allocation
#0 0x56474053d7a2 in __interceptor_malloc
/src/llvm-project/compiler-rt/lib/msan/msan_interceptors.cpp:1047:3
#1 0x793bcb915f61 in pcre2_compile_8
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/mnt/scratch0/clusterfuzz/bot/builds/clusterfuzz-builds_selinux_f4e9262b6c04d654efaf1cf4240d4d2ed1e93020/revisions/selabel_file_text-fuzzer+0x12c02c) Exiting

When I asked an AI about possible causes, it said the following:
---<snip>---
The most likely cause is simply that this patch is the first time libselinux has ever called pcre2_jit_compile() — the commit message says as much ("the JIT was available but unused"). Before this patch, regex_match() always ran through PCRE2's plain interpreter. After it, pcre2_match() automatically switches to the JIT-compiled matcher whenever compilation succeeded, which is a completely different code path with its own memory-access patterns.

PCRE2's JIT is known to read some stack/register slots that sanitizers flag as uninitialized, even though the values are never actually used to affect output — this is a known, discussed behavior on the PCRE2 mailing list, where a user reproduced "Conditional jump or move depends on uninitialised value(s)" originating from a stack allocation, specifically when pcre2_jit_compile() was used, and removing the JIT compile call made the warning disappear [Oss-fuzz](https://issues.oss-fuzz.com/issues/474186379) . A PCRE2 maintainer's response in that thread characterized it as intentional optimization behavior in the JIT rather than a genuine bug. Under libFuzzer+MSan (which oss-fuzz uses), this shows up as "use-of-uninitialized-value" because MSan is stricter about propagating "poisoned" bytes than Valgrind is.

So the fuzzer isn't necessarily finding a new bug in libselinux's logic — it's newly exercising PCRE2's JIT matcher (previously dormant in this codebase) on crafted/fuzzed file-context patterns, and the JIT engine's normal register/stack handling trips MSan. This is consistent with the patch note that pcre2_jit_match() was avoided due to known correctness issues on some platforms — the JIT engine here has a track record of subtle divergence from the interpreter.

Practical next steps if you're triaging this upstream:
- Check whether the crash reproduces with pcre2_jit_compile() removed (isolate JIT vs. non-JIT).
- Check whether it reproduces under regular ASan/UBSan (a true bug) vs. only MSan (more likely a sanitizer-visibility artifact of JIT codegen).
- If MSan-only, this is likely worth reporting upstream to PCRE2 rather than something fixable in libselinux, since libselinux only added the pcre2_jit_compile() call and doesn't control the generated machine code.
---snip>---

Not sure anything needs to be done here but noting it in case anyone else has further insights.

#11 lore raw

Hi, thanks for getting me the output, I really don't have access there. Looking into the fix in pcre2 (well Claude is), probably easier than reporting an issue there.

Not sure if you/oss-fuzz are picking up pcre2 from a package (RHEL uses 10.44 which is 2 years old)
or building from source so it might be failing for a while, unless you want me to add an ignorelist entry to oss-fuzz,
which would then need to be removed after the fix is available to oss-fuzz tested system.
Assuming I can create a fix and not just end up reporting an issue after all.

Best regards
Petr Matyas

Quoted 131 lines · Stephen Smalley
Quoted 36 lines · Stephen Smalley
Quoted 33 lines · Stephen Smalley
Quoted 31 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) for both complete and partial matching modes. pcre2_match() uses the JIT automatically when available, which avoids the interpreter's susceptibility to catastrophic backtracking.
Note: pcre2_jit_match() is deliberately NOT used as it is known to produce incorrect results on some platforms (e.g. aarch64).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Merged.

This commit produces a new oss-fuzz issue report,
https://issues.oss-fuzz.com/issues/541525193
but quoting the stack trace below since you may lack access to the report:

Uninitialized bytes in MemcmpInterceptorCommon at offset 0 inside
[0x70b00000008c, 8)
==248==WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x56474054202c in ___interceptor_memcmp /src/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc:880:10
#1 0x793bcb92efb3 in libpcre2-8.so.0
#2 0x793bcb94ecf1 in libpcre2-8.so.0
#3 0x793bcb952edc in pcre2_jit_compile_8
#4 0x5647405c1509 in regex_prepare_data selinux/libselinux/src/regex.c:109:8
#5 0x5647405b4828 in compile_regex selinux/libselinux/src/label_file.h:476:7
#6 0x5647405a04b5 in insert_spec selinux/libselinux/src/label_file.h:657:8
#7 0x5647405a04b5 in process_line selinux/libselinux/src/label_file.h:898:9
#8 0x5647405a04b5 in process_text_file selinux/libselinux/src/label_file.c:226:8
#9 0x56474059baa1 in LLVMFuzzerTestOneInput
selinux/libselinux/fuzz/selabel_file_text-fuzzer.c:171:7
#10 0x56474048522d in fuzzer::Fuzzer::ExecuteCallback(unsigned char
const*, unsigned long)
/src/llvm-project/compiler-rt/lib/fuzzer/FuzzerLoop.cpp:619:13
#11 0x56474046ffa2 in fuzzer::RunOneTest(fuzzer::Fuzzer*, char const*,
unsigned long) /src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:329:6
#12 0x564740475e70 in fuzzer::FuzzerDriver(int*, char***, int
(*)(unsigned char const*, unsigned long))
/src/llvm-project/compiler-rt/lib/fuzzer/FuzzerDriver.cpp:865:9
#13 0x5647404a19a2 in main
/src/llvm-project/compiler-rt/lib/fuzzer/FuzzerMain.cpp:20:10
#14 0x793bcb580082 in __libc_start_main
/build/glibc-B3wQXB/glibc-2.31/csu/libc-start.c:308:16
#15 0x56474046908d in _start
Uninitialized value was created by a heap allocation
#0 0x56474053d7a2 in __interceptor_malloc
/src/llvm-project/compiler-rt/lib/msan/msan_interceptors.cpp:1047:3
#1 0x793bcb915f61 in pcre2_compile_8
SUMMARY: MemorySanitizer: use-of-uninitialized-value (/mnt/scratch0/clusterfuzz/bot/builds/clusterfuzz-builds_selinux_f4e9262b6c04d654efaf1cf4240d4d2ed1e93020/revisions/selabel_file_text-fuzzer+0x12c02c) Exiting

When I asked an AI about possible causes, it said the following:
---<snip>---
The most likely cause is simply that this patch is the first time libselinux has ever called pcre2_jit_compile() — the commit message says as much ("the JIT was available but unused"). Before this patch, regex_match() always ran through PCRE2's plain interpreter. After it, pcre2_match() automatically switches to the JIT-compiled matcher whenever compilation succeeded, which is a completely different code path with its own memory-access patterns.

PCRE2's JIT is known to read some stack/register slots that sanitizers flag as uninitialized, even though the values are never actually used to affect output — this is a known, discussed behavior on the PCRE2 mailing list, where a user reproduced "Conditional jump or move depends on uninitialised value(s)" originating from a stack allocation, specifically when pcre2_jit_compile() was used, and removing the JIT compile call made the warning disappear [Oss-fuzz](https://issues.oss-fuzz.com/issues/474186379) . A PCRE2 maintainer's response in that thread characterized it as intentional optimization behavior in the JIT rather than a genuine bug. Under libFuzzer+MSan (which oss-fuzz uses), this shows up as "use-of-uninitialized-value" because MSan is stricter about propagating "poisoned" bytes than Valgrind is.

So the fuzzer isn't necessarily finding a new bug in libselinux's logic — it's newly exercising PCRE2's JIT matcher (previously dormant in this codebase) on crafted/fuzzed file-context patterns, and the JIT engine's normal register/stack handling trips MSan. This is consistent with the patch note that pcre2_jit_match() was avoided due to known correctness issues on some platforms — the JIT engine here has a track record of subtle divergence from the interpreter.

Practical next steps if you're triaging this upstream:
- Check whether the crash reproduces with pcre2_jit_compile() removed (isolate JIT vs. non-JIT).
- Check whether it reproduces under regular ASan/UBSan (a true bug) vs. only MSan (more likely a sanitizer-visibility artifact of JIT codegen).
- If MSan-only, this is likely worth reporting upstream to PCRE2 rather than something fixable in libselinux, since libselinux only added the pcre2_jit_compile() call and doesn't control the generated machine code.
---snip>---

Not sure anything needs to be done here but noting it in case anyone else has further insights.

#12 lore raw
In reply to Petr Matyas
Quoted 4 lines · Petr Matyas

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

RHEL 10.3? Did you mean 10.2? I got RHEL 10 VM and there is no upgrade path to 10.3.

br,
KK

maintainer
#13 lore raw
In reply to Petr Matyas
Quoted 26 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3) or pcre2_jit_match(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) to engage JIT for both complete and partial matching. regex_match() now calls pcre2_jit_match() and falls back to pcre2_match() only on PCRE2_ERROR_JIT_BADOPTION (JIT unavailable or not compiled for the requested mode).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to both pcre2_jit_match() and the fallback pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Quoted 6 lines

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

It occurs to me belatedly that this change (which we already merged along with several follow-ups)
likely triggers SELinux execmem checks on any process that uses selabel_open(SELABEL_CTX_FILE,...)
due to the JIT compilation. Given that fact along with the fact that we are still seeing cases [1] where
oss-fuzz can trigger stack smashing on pathological regexes and that this was originally motivated by
concerns about such regexes and that the source of these regexes is always trusted (root-only config,
and in the case of Android integrity-verified, I am starting to wonder if this change is harmful rather than
beneficial. Open to others' thoughts. At the very least, I suspect Android might want to disable this.

[1] https://lore.kernel.org/selinux/CAEjxPJ6-b4KmkgSgO7hXf+8ZGgRtR_JkS1ZQU-ULVk0aex5zCw@mail.gmail.com/T/#t

maintainer
#14 lore raw
Quoted 33 lines · Stephen Smalley
Quoted 31 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3) or pcre2_jit_match(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) to engage JIT for both complete and partial matching. regex_match() now calls pcre2_jit_match() and falls back to pcre2_match() only on PCRE2_ERROR_JIT_BADOPTION (JIT unavailable or not compiled for the requested mode).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to both pcre2_jit_match() and the fallback pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

Quoted 7 lines
Quoted 1 line

It occurs to me belatedly that this change (which we already merged along with several follow-ups)
likely triggers SELinux execmem checks on any process that uses selabel_open(SELABEL_CTX_FILE,...)
due to the JIT compilation.

I experimentally confirmed that this does trigger execmem denials on Fedora if I setsebool deny_execmem=True. The denial is not fatal since it falls back to the interpreter but it will show up in the audit logs for any confined domain or if deny_execmem=True for all domains.

Quoted 12 lines

Given that fact along with the fact that
we are still seeing cases [1] where
oss-fuzz can trigger stack smashing on pathological regexes and that this was originally motivated by
concerns about such regexes and that the source of these regexes is always trusted (root-only config,
and in the case of Android integrity-verified, I am starting to wonder if this change is harmful rather than
beneficial. Open to others' thoughts. At the very least, I suspect Android might want to disable this.

[1] https://lore.kernel.org/selinux/CAEjxPJ6-b4KmkgSgO7hXf+8ZGgRtR_JkS1ZQU-ULVk0aex5zCw@mail.gmail.com/T/#t

maintainer
#15 lore raw
Quoted 44 lines · Stephen Smalley
Quoted 33 lines · Stephen Smalley
Quoted 31 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3) or pcre2_jit_match(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) to engage JIT for both complete and partial matching. regex_match() now calls pcre2_jit_match() and falls back to pcre2_match() only on PCRE2_ERROR_JIT_BADOPTION (JIT unavailable or not compiled for the requested mode).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to both pcre2_jit_match() and the fallback pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

Quoted 7 lines
Quoted 1 line

It occurs to me belatedly that this change (which we already merged along with several follow-ups)
likely triggers SELinux execmem checks on any process that uses selabel_open(SELABEL_CTX_FILE,...)
due to the JIT compilation.

Quoted 15 lines

I experimentally confirmed that this does trigger execmem denials on Fedora if I setsebool deny_execmem=True. The denial is not fatal since it falls back to the interpreter but it will show up in the audit logs for any confined domain or if deny_execmem=True for all domains.

Quoted 10 lines

Given that fact along with the fact that
we are still seeing cases [1] where
oss-fuzz can trigger stack smashing on pathological regexes and that this was originally motivated by
concerns about such regexes and that the source of these regexes is always trusted (root-only config,
and in the case of Android integrity-verified, I am starting to wonder if this change is harmful rather than
beneficial. Open to others' thoughts. At the very least, I suspect Android might want to disable this.

Barring objections or counter-proposals, I think we may want to revert the use of the PCRE2 JIT
to avoid users of libselinux selabel_open(SELABEL_CTX_FILE, ...) suddenly triggering execmem denials.
We can keep the other part of this change intact.

Quoted 2 lines
Quoted 2 lines

[1] https://lore.kernel.org/selinux/CAEjxPJ6-b4KmkgSgO7hXf+8ZGgRtR_JkS1ZQU-ULVk0aex5zCw@mail.gmail.com/T/#t

maintainer
#16 lore raw
Quoted 63 lines · Stephen Smalley
Quoted 44 lines · Stephen Smalley
Quoted 33 lines · Stephen Smalley
Quoted 31 lines · Petr Matyas

The PCRE2 interpreter is susceptible to catastrophic backtracking on crafted file context patterns or lookup keys. Since libselinux never called pcre2_jit_compile(3) or pcre2_jit_match(3), the JIT was available but unused, leaving the interpreter exposed on all platforms.

Fix with two complementary mitigations:

1. Call pcre2_jit_compile() after every pattern compilation (including mmap deserialization) to engage JIT for both complete and partial matching. regex_match() now calls pcre2_jit_match() and falls back to pcre2_match() only on PCRE2_ERROR_JIT_BADOPTION (JIT unavailable or not compiled for the requested mode).

2. Create a shared pcre2_match_context with a backtrack step limit of 10 000 000 and pass it to both pcre2_jit_match() and the fallback pcre2_match(). PCRE2_ERROR_MATCHLIMIT is treated as no-match to keep the lookup safe rather than failing noisily. This bounds worst-case matching time to milliseconds for any pattern/subject combination, regardless of JIT availability.

The vulnerability was reproduced and confirmed fixed on:
- RHIVOS 2.0 / aarch64
- RHEL 10.3 / x86_64
- Fedora 44 / x86_64

Reproducer (149 bytes, base64):
AwovMi0GLy8GCygKeAYGKwYLKAoKCgAAAPpXKgsoCi8vBgs8PG5vbmU+PgovMi0GLy8GCygKeAYG KwYLKAoKCiNXLy8wCwsoCgojV1dXClcqCygKLy8vV1cueHhXV1d4eHgveCsGCygKCgonVy9XV3cy eHgveAcGJAsoCi8tLwYvMi0GLy8GCzw8bm9uZT4+3q2+7y8=

Quoted 7 lines
Quoted 1 line

It occurs to me belatedly that this change (which we already merged along with several follow-ups)
likely triggers SELinux execmem checks on any process that uses selabel_open(SELABEL_CTX_FILE,...)
due to the JIT compilation.

Quoted 15 lines

I experimentally confirmed that this does trigger execmem denials on Fedora if I setsebool deny_execmem=True. The denial is not fatal since it falls back to the interpreter but it will show up in the audit logs for any confined domain or if deny_execmem=True for all domains.

Quoted 10 lines

Given that fact along with the fact that
we are still seeing cases [1] where
oss-fuzz can trigger stack smashing on pathological regexes and that this was originally motivated by
concerns about such regexes and that the source of these regexes is always trusted (root-only config,
and in the case of Android integrity-verified, I am starting to wonder if this change is harmful rather than
beneficial. Open to others' thoughts. At the very least, I suspect Android might want to disable this.

Quoted 5 lines

Barring objections or counter-proposals, I think we may want to revert the use of the PCRE2 JIT
to avoid users of libselinux selabel_open(SELABEL_CTX_FILE, ...) suddenly triggering execmem denials.
We can keep the other part of this change intact.

Rather than revert it entirely, I submitted a patch making it opt-in via:
https://lore.kernel.org/selinux/[email protected]/T/#u