libselinux: label_file: make JIT compilation opt-in
Stephen Smalley
maintainerPATCH v2 1/1 libselinux: label_file: make JIT compilation opt-in
Commit 92af7f1b61db6e ("libselinux: prevent ReDoS in file context regex matching") introduced the use of pcre2_jit_compile() after every pattern compilation to leverage JIT for regex matching to help mitigate catastrophic backtracking on crafted file context regexes or lookup keys. This triggers SELinux execmem permission checks on every caller of selabel_open(SELABEL_CTX_FILE, ..). Permission denial is non-fatal in this case but allowing this permission may be undesirable for some callers and will produce audit log noise for others. Further, the JITed matching itself is subject to stack overflow on crafted regexes. Make the JIT compilation opt-in rather than automatic via a new SELABEL_OPT_JIT option for selabel_open() so that we do not impact existing callers while allowing callers to selectively opt in when they want this behavior and run in a context that can be allowed execmem without concern.
---
v2 fixes the wrong order of validating, jit parameters in one case, and also fixes the USE_PCRE2=n build.
libselinux/include/selinux/label.h | 4 +++- libselinux/man/man3/selabel_open.3 | 7 ++++++ libselinux/src/label.c | 13 +++++++++++ libselinux/src/label_file.c | 32 ++++++++++++++++----------- libselinux/src/label_file.h | 6 ++--- libselinux/src/label_internal.h | 2 ++ libselinux/src/regex.c | 16 +++++++++----- libselinux/src/regex.h | 6 +++-- libselinux/utils/sefcontext_compile.c | 1 + 9 files changed, 62 insertions(+), 25 deletions(-)
libselinux/include/selinux/label.h +3 -1
@@ -59,8 +59,10 @@ struct selabel_handle;#define SELABEL_OPT_SUBSET 4/* require a hash calculation on spec files */ #define SELABEL_OPT_DIGEST 5 +/* enable JIT compilation of regexes */ +#define SELABEL_OPT_JIT 6/* total number of options */ -#define SELABEL_NOPT 6 +#define SELABEL_NOPT 7/* * Label operations
libselinux/man/man3/selabel_open.3 +7 -0
@@ -73,6 +73,13 @@ A non-null value for this option enables the generation of an SHA1 digest of the spec files loaded as described in .BR selabel_digest (3) . +.TP +.B SELABEL_OPT_JIT +A non-null value for this option enables the JIT compilation of regexes +read from the spec files. This option is only currently supported with +the +.B SELABEL_CTX_FILE +backend. .SH "BACKENDS" .TP .B SELABEL_CTX_FILE
libselinux/src/label.c +13 -0
@@ -155,6 +155,18 @@ int selabel_validate(struct selabel_lookup_rec *contexts)return 0 ; } +/* Should we enable JIT compilation? */ +static inline int selabel_is_jit_set (const struct selinux_opt *opts,unsigned n) +{ +while (n) { + n--; +if (opts[n].type ==SELABEL_OPT_JIT ) +return !!opts[n].value; + } + +return 0 ; +} +/* Public API helpers */ static int selabel_fini (const struct selabel_handle *rec,struct selabel_lookup_rec *lr,bool translating)
@@ -261,6 +273,7 @@ struct selabel_handle *selabel_open(unsigned int backend, rec->backend = backend; rec->validating =selabel_is_validate_set (opts, nopts); + rec->jit =selabel_is_jit_set (opts, nopts); rec->digest =selabel_is_digest_set (opts, nopts);
libselinux/src/label_file.c +19 -13
@@ -633,8 +633,8 @@ static int load_mmap_literal_spec(struct mmap_area *mmap_area, bool validating, }static int load_mmap_regex_spec (struct mmap_area *mmap_area,bool validating, -bool do_load_precompregex,uint8_t inputno, -struct regex_spec *rspec, +bool jit,bool do_load_precompregex, +uint8_t inputno,struct regex_spec *rspec,const struct context_array *ctx_array) {uint32_t data_u32, ctx_id, lineno;
@@ -723,7 +723,7 @@ static int load_mmap_regex_spec(struct mmap_area *mmap_area, bool validating,* Read pcre regex related data */ rc =regex_load_mmap (mmap_area, &rspec->regex, do_load_precompregex, - &rspec->regex_compiled); + jit, &rspec->regex_compiled);if (rc <0 )return -1 ;
@@ -733,7 +733,8 @@ static int load_mmap_regex_spec(struct mmap_area *mmap_area, bool validating, }static int load_mmap_spec_node (struct mmap_area *mmap_area,const char *path, -bool validating,bool do_load_precompregex, +bool validating,bool jit, +bool do_load_precompregex,struct spec_node *node,const unsigned depth,uint8_t inputno,const struct context_array *ctx_array)
@@ -840,7 +841,7 @@ static int load_mmap_spec_node(struct mmap_area *mmap_area, const char *path, node->regex_specs_alloc = rspec_num;for (uint32_t i =0 ; i < rspec_num; i++) { - rc =load_mmap_regex_spec (mmap_area, validating, + rc =load_mmap_regex_spec (mmap_area, validating, jit, do_load_precompregex, inputno, &node->regex_specs[i], ctx_array);
@@ -876,7 +877,7 @@ static int load_mmap_spec_node(struct mmap_area *mmap_area, const char *path,for (uint32_t i =0 ; i < children_num; i++) { rc =load_mmap_spec_node (mmap_area, path, validating, - do_load_precompregex, + jit, do_load_precompregex, &node->children[i], depth +1 , inputno, ctx_array);if (rc)
@@ -1064,7 +1065,7 @@ end_arch_check:if (!root) goto err; - rc =load_mmap_spec_node (mmap_area, path, rec->validating, + rc =load_mmap_spec_node (mmap_area, path, rec->validating, rec->jit, reg_version_matches && reg_arch_matches, root,0 , inputno, &ctx_array);if (rc)
@@ -1538,6 +1539,7 @@ static int init(struct selabel_handle *rec, const struct selinux_opt *opts,case SELABEL_OPT_UNUSED :case SELABEL_OPT_VALIDATE :case SELABEL_OPT_DIGEST : +case SELABEL_OPT_JIT :break ;default : errno =EINVAL ;
@@ -1890,6 +1892,7 @@ fail:* @file_kind: The kind of the file to look up (translated from file type into LABEL_FILE_KIND_*). * @partial: Whether to partially match the given file path or completely. * @find_all: Whether to find all file context definitions or just the most specific. +* @jit: Whether to enable JIT compilation of regexes. * @buf: A pre-allocated buffer for a potential result to avoid allocating it on the heap or * NULL. Mutual exclusive with @find_all. *
@@ -1897,9 +1900,11 @@ fail:* its a linked list of all results. If @buf was specified it is returned on a match found. * NULL is returned in case of no match found. */ -static struct lookup_result * -lookup_check_node (struct spec_node *node,const char *key,uint8_t file_kind, -bool partial,bool find_all,struct lookup_result *buf) +static struct lookup_result *lookup_check_node (struct spec_node *node, +const char *key, +uint8_t file_kind,bool partial, +bool find_all,bool jit, +struct lookup_result *buf) {struct lookup_result *result =NULL ;struct lookup_result **next = &result;
@@ -2002,7 +2007,8 @@ lookup_check_node(struct spec_node *node, const char *key, uint8_t file_kind, file_kind != rspec->file_kind)continue ; -if (compile_regex (rspec, errbuf,sizeof (errbuf)) <0 ) { +if (compile_regex (rspec, errbuf,sizeof (errbuf), jit) < +0 ) { COMPAT_LOG( SELINUX_ERROR, "Failed to compile regular expression '%s': %s\n",
@@ -2274,8 +2280,8 @@ FUZZ_EXTERN struct lookup_result *lookup_all(struct selabel_handle *rec, node =lookup_find_deepest_node (data->root, key, &rest_key); - result = -lookup_check_node (node, key, file_kind, partial, find_all, buf); + result =lookup_check_node (node, key, file_kind, partial, find_all, + rec->jit, buf);/* For partial lookup if the key matches a child node, recursively add all specs */ if (partial && rest_key) {
libselinux/src/label_file.h +3 -3
@@ -398,7 +398,7 @@ static inline void sort_specs(struct saved_data *data) }static int compile_regex (struct regex_spec *spec,char *errbuf, -size_t errbuf_size) +size_t errbuf_size,bool jit) {const char *reg_buf;char *anchored_regex, *cp;
@@ -473,7 +473,7 @@ static int compile_regex(struct regex_spec *spec, char *errbuf, *cp ='\0' ;/* Compile the regular expression. */ - rc =regex_prepare_data (&spec->regex, anchored_regex, &error_data); + rc =regex_prepare_data (&spec->regex, anchored_regex, &error_data, jit);free (anchored_regex);if (rc <0 ) {regex_format_error (&error_data, errbuf, errbuf_size);
@@ -655,7 +655,7 @@ static int insert_spec(const struct selabel_handle *rec,char errbuf[256 ];if (compile_regex (&node->regex_specs[id], errbuf, -sizeof (errbuf))) { +sizeof (errbuf), rec->jit)) { COMPAT_LOG( SELINUX_ERROR, "%s: line %u has invalid regex %s: %s\n",
libselinux/src/label_internal.h +2 -0
@@ -11,6 +11,7 @@#include <stdlib.h> #include <stdarg.h> #include <stdio.h> +#include <stdbool.h> #include <selinux/selinux.h> #include <selinux/label.h> #include "sha1.h"
@@ -73,6 +74,7 @@ struct selabel_handle {/* arguments that were passed to selabel_open */ unsigned int backend;int validating; +bool jit;/* labeling operations */ struct selabel_lookup_rec *(*func_lookup)(struct selabel_handle *h,
libselinux/src/regex.c +10 -6
@@ -87,7 +87,7 @@ struct regex_data { };int regex_prepare_data (struct regex_data **regex,char const *pattern_string, -struct regex_error_data *errordata) +struct regex_error_data *errordata,bool jit) {memset (errordata,0 ,sizeof (struct regex_error_data ));
@@ -108,7 +108,8 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,* susceptibility to catastrophic backtracking. Partial matches fall * back to the interpreter, protected by the match limit. Failures * are non-fatal. */ - (void )pcre2_jit_compile ((*regex)->regex,PCRE2_JIT_COMPLETE ); +if (jit) + (void )pcre2_jit_compile ((*regex)->regex,PCRE2_JIT_COMPLETE );return 0 ;
@@ -130,7 +131,7 @@ char const *regex_version(void) }int regex_load_mmap (struct mmap_area *mmap_area,struct regex_data **regex, -int do_load_precompregex,bool *regex_compiled) +int do_load_precompregex,bool jit,bool *regex_compiled) {int rc;uint32_t data_u32, entry_len;
@@ -164,7 +165,9 @@ 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 ); +if (jit) + (void )pcre2_jit_compile ((*regex)->regex, +PCRE2_JIT_COMPLETE ); *regex_compiled = true; }
@@ -367,7 +370,8 @@ struct regex_data { };int regex_prepare_data (struct regex_data **regex,char const *pattern_string, -struct regex_error_data *errordata) +struct regex_error_data *errordata, +bool jit __attribute__((unused))) {memset (errordata,0 ,sizeof (struct regex_error_data ));
@@ -402,7 +406,7 @@ char const *regex_version(void)int regex_load_mmap (struct mmap_area *mmap_area,struct regex_data **regex,int do_load_precompregex __attribute__((unused)), -bool *regex_compiled) +bool jit __attribute((unused)),bool *regex_compiled) {int rc;uint32_t data_u32, entry_len;
libselinux/src/regex.h +4 -2
@@ -78,11 +78,12 @@ void regex_data_free(struct regex_data *regex);* to this function. This structure depends on the underlying * implementation. It can be passed to regex_format_error * to generate a human readable error message. +* @arg jit A boolean indicating whether JIT compilation should be performed. * @retval 0 on success * @retval -1 on error */ int regex_prepare_data (struct regex_data **regex,char const *pattern_string, -struct regex_error_data *errordata); +struct regex_error_data *errordata,bool jit);/** * This function loads a serialized precompiled pattern from a contiguous * data region given by map_area.
@@ -93,6 +94,7 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,* with regex_data_create and must be freed with regex_data_free. * @arg do_load_precompregex If non-zero precompiled patterns get loaded from * the mmap region (ignored by PCRE1 back-end). +* @arg jit A boolean indicating whether JIT compilation should be performed. * @arg regex_compiled Set to true if a precompiled pattern was loaded * into regex, otherwise set to false to indicate later * compilation must occur
@@ -101,7 +103,7 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,* @retval -1 on error */ int regex_load_mmap (struct mmap_area *map_area,struct regex_data **regex, -int do_load_precompregex,bool *regex_compiled); +int do_load_precompregex,bool jit,bool *regex_compiled);/** * This function stores a precompiled regular expression to a file. * In the case of PCRE, it just dumps the binary representation of the
libselinux/utils/sefcontext_compile.c +1 -0
@@ -5,6 +5,7 @@#include <stdio.h> #include <string.h> #include <unistd.h> +#include <limits.h> #include <selinux/selinux.h> #include <sepol/sepol.h>
Signature · 1 line
2.55.0