libselinux: Fix file labels for regexes with metachars
PATCH v2 1/1 libselinux: Fix file labels for regexes with metachars
File labels assigned using the lookup_best_match() function do not assign the best match if its regex contains metacharacters.
For non-exact regex matches, lookup_best_match() finds the closest match by tracking the length of the matching prefix. Prefix match is tracked via the prefix_len variable. This was previously calculated and set in the spec_hasMetaChars() function. Commit 3cb6078 removed the prefix_len calculation, this commit restores it.
v2 changes:label_file.c - set prefix_len in load_mmap. Needed for binary file_contexts files.
--- libselinux/src/label_file.c | 2 ++ libselinux/src/label_file.h | 2 ++ 2 files changed, 4 insertions(+)
libselinux/src/label_file.c +2 -0
@@ -337,6 +337,8 @@ static int load_mmap(struct selabel_handle *rec, const char *path, goto err; spec->hasMetaChars = meta_chars; +if (spec->hasMetaChars) + spec->prefix_len =strlen (spec->regex_str);/* Process regex and study_data entries */ rc =next_entry (&entry_len, mmap_area,sizeof (uint32_t ));
libselinux/src/label_file.h +2 -0
@@ -148,6 +148,7 @@ static inline void spec_hasMetaChars(struct spec *spec) end = c + len; spec->hasMetaChars =0 ; + spec->prefix_len = len;/* Look at each character in the RE specification string for a * meta character. Return when any meta character reached. */
@@ -164,6 +165,7 @@ static inline void spec_hasMetaChars(struct spec *spec)case '(' :case '{' : spec->hasMetaChars =1 ; + spec->prefix_len = c - spec->regex_str;return ;case '\\' :/* skip the next character */ c++;
Signature · 1 line
2.4.3.573.g4eafbef
Quoted 24 lines · Jeff Vander Stoep
File labels assigned using the
lookup_best_match()function do not assign the best match if its regex contains metacharacters.For non-exact regex matches,
lookup_best_match()finds the closest match by tracking the length of the matching prefix. Prefix match is tracked via theprefix_lenvariable. This was previously calculated and set in thespec_hasMetaChars()function. Commit 3cb6078 removed theprefix_lencalculation, this commit restores it.v2 changes:
label_file.c- setprefix_leninload_mmap. Needed for binaryfile_contextsfiles.Signed-off-by Jeff Vander Stoep <[email protected]>--- libselinux/src/label_file.c | 2 ++ libselinux/src/label_file.h | 2 ++ 2 files changed, 4 insertions(+)libselinux/src/label_file.c +0
-0
libselinux/src/label_file.c static int load_mmap(struct selabel_handle *rec, const char *path, · line 341
Quoted 5 lines
libselinux/src/label_file.c +2
-0@@ -337,6 +337,8 @@ static int load_mmap(struct selabel_handle *rec, const char *path, goto err; spec->hasMetaChars = meta_chars; +if (spec->hasMetaChars) + spec->prefix_len =strlen (spec->regex_str);
This doesn't seem right, as the prefix_len should be the length of the fixed prefix prior to any meta characters. We could either store the prefix len as in Richard's patch or recompute it here (e.g. call spec_hasMetaChars).
Quoted 24 lines
libselinux/src/label_file.c +0
-0/* Process regex and study_data entries */ rc =next_entry (&entry_len, mmap_area,sizeof (uint32_t ));libselinux/src/label_file.h +2
-0@@ -148,6 +148,7 @@ static inline void spec_hasMetaChars(struct spec *spec) end = c + len; spec->hasMetaChars =0 ; + spec->prefix_len = len;/* Look at each character in the RE specification string for a * meta character. Return when any meta character reached. */ @@ -164,6 +165,7 @@ static inline void spec_hasMetaChars(struct spec *spec)case '(' :case '{' : spec->hasMetaChars =1 ; + spec->prefix_len = c - spec->regex_str;return ;case '\\' :/* skip the next character */ c++;