Skip to contentSkip to conversation

libselinux: Add architecture string to file_context.bin

PATCH In review Janis Danisevskis 4 participants 9 messages Sep 26, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libselinux: Add architecture string to file_context.bin

Serialized precompiled regular expressins are architecture dependent when using PCRE2. This patch
- bumps the SELINUX_COMPILED_FCONTEXT version to 5 and
- adds a field to the output indicating the architecture compatibility.

libselinux can cope with an architecture mismatch by ignoring the precompiled data in the input file and recompiling the regular expressions at runtime. It can also load older versions of file_contexts.bin if they where built with sefcontext_compile using the exact same version of the pcre1/2 as selinux.

---
 libselinux/src/label_file.c           | 44 +++++++++++++++++++++++++++++-
 libselinux/src/label_file.h           |  4 ++-
 libselinux/src/regex.c                | 50 ++++++++++++++++++++++++++++++++---
 libselinux/src/regex.h                | 15 ++++++++++-
 libselinux/utils/sefcontext_compile.c | 13 +++++++++
 5 files changed, 120 insertions(+), 6 deletions(-)
libselinux/src/label_file.c +43 -1
index 7156825..9abc1d6 100644
@@ -126,6 +126,8 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
 	uint32_t i, magic, version;
 	uint32_t entry_len, stem_map_len, regex_array_len;
 	const char *reg_version;
+	const char *reg_arch;
+	char reg_arch_matches = 1;
 
 	mmap_area = malloc(sizeof(*mmap_area));
 	if (!mmap_area) {
@@ -159,6 +161,10 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
 	if (!reg_version)
 		return -1;
 
+	reg_arch = regex_arch_string();
+	if (!reg_arch)
+		return -1;
+
 	if (version >= SELINUX_COMPILED_FCONTEXT_PCRE_VERS) {
 
 		len = strlen(reg_version);
@@ -188,7 +194,43 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
 			return -1;
 		}
 		free(str_buf);
+
+		if (version >= SELINUX_COMPILED_FCONTEXT_REGEX_ARCH) {
+			len = strlen(reg_arch);
+
+			rc = next_entry(&entry_len, mmap_area,
+					sizeof(uint32_t));
+			if (rc < 0)
+				return -1;
+
+			/* Check arch string lengths */
+			if (len != entry_len) {
+				/*
+				 * Skip the entry and conclude that we have
+				 * a mismatch, which is not fatal.
+				 */
+				next_entry(NULL, mmap_area, entry_len);
+				reg_arch_matches = 0;
+				goto end_arch_check;
+			}
+
+			/* Check if arch string mismatch */
+			str_buf = malloc(entry_len + 1);
+			if (!str_buf)
+				return -1;
+
+			rc = next_entry(str_buf, mmap_area, entry_len);
+			if (rc < 0) {
+				free(str_buf);
+				return -1;
+			}
+
+			str_buf[entry_len] = '\0';
+			reg_arch_matches = strcmp(str_buf, reg_arch) == 0;
+			free(str_buf);
+		}
 	}
+end_arch_check:
 
 	/* allocate the stems_data array */
 	rc = next_entry(&stem_map_len, mmap_area, sizeof(uint32_t));
@@ -349,7 +391,7 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
 			spec->prefix_len = prefix_len;
 		}
 
-		rc = regex_load_mmap(mmap_area, &spec->regex);
+		rc = regex_load_mmap(mmap_area, &spec->regex, reg_arch_matches);
 		if (rc < 0)
 			goto out;
 
libselinux/src/label_file.h +3 -1
index 88f4294..00c0a5c 100644
@@ -24,8 +24,10 @@
 #define SELINUX_COMPILED_FCONTEXT_PCRE_VERS	2
 #define SELINUX_COMPILED_FCONTEXT_MODE		3
 #define SELINUX_COMPILED_FCONTEXT_PREFIX_LEN	4
+#define SELINUX_COMPILED_FCONTEXT_REGEX_ARCH	5
 
-#define SELINUX_COMPILED_FCONTEXT_MAX_VERS	SELINUX_COMPILED_FCONTEXT_PREFIX_LEN
+#define SELINUX_COMPILED_FCONTEXT_MAX_VERS \
+	SELINUX_COMPILED_FCONTEXT_REGEX_ARCH
 
 /* A file security context specification. */
 struct spec {
libselinux/src/regex.c +47 -3

80 lines not on this page · raw message

libselinux/src/regex.h +14 -1
index 810b3c5..186c5ec 100644
@@ -34,6 +34,16 @@ struct regex_error_data {
 struct mmap_area;
 
 /**
+ * regex_arch_string return a string that represents the pointer width, the
+ * width of what the backend considers a size type, and the endianness of the
+ * system that this library was build for. (e.g. for x86_64: "8-8-el").
+ * This is required when loading stored regular espressions. PCRE2 regular
+ * expressions are not portable across architectures that do not have a
+ * matching arch-string.
+ */
+char const *regex_arch_string(void) hidden;
+
+/**
  * regex_verison returns the version string of the underlying regular
  * regular expressions library. In the case of PCRE it just returns the
  * result of pcre_version(). In the case of PCRE2, the very first time this
@@ -86,12 +96,15 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
  *               representation of the precompiled pattern.
  * @arg regex If successful, the structure returned through *regex was allocated
  *            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).
  *
  * @retval 0 on success
  * @retval -1 on error
  */
 int regex_load_mmap(struct mmap_area *map_area,
-		    struct regex_data **regex) hidden;
+		    struct regex_data **regex,
+		    int do_load_precompregex) hidden;
 /**
  * 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 +13 -0
index 70853e7..d91db9a 100644
@@ -103,6 +103,7 @@ static int write_binary_file(struct saved_data *data, int fd,
 	uint32_t i;
 	int rc;
 	const char *reg_version;
+	const char *reg_arch;
 
 	bin_file = fdopen(fd, "w");
 	if (!bin_file) {
@@ -133,6 +134,18 @@ static int write_binary_file(struct saved_data *data, int fd,
 	if (len != section_len)
 		goto err;
 
+	/* write regex arch string */
+	reg_arch = regex_arch_string();
+	if (!reg_arch)
+		goto err;
+	section_len = strlen(reg_arch);
+	len = fwrite(&section_len, sizeof(uint32_t), 1, bin_file);
+	if (len != 1)
+		goto err;
+	len = fwrite(reg_arch, sizeof(char), section_len, bin_file);
+	if (len != section_len)
+		goto err;
+
 	/* write the number of stems coming */
 	section_len = data->num_stems;
 	len = fwrite(&section_len, sizeof(uint32_t), 1, bin_file);
Signature · 1 line
2.8.0.rc3.226.g39d4020
#1 lore raw

PATCH 2/3 libselinux: sefcontext_compile: Add "-i" flag

Adds the "-i" flag, which prints the version and architecture identifier of the regular expression back end.

---
 libselinux/utils/sefcontext_compile.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
libselinux/utils/sefcontext_compile.c +10 -1
index d91db9a..8c48d32 100644
@@ -280,6 +280,11 @@ static void usage(const char *progname)
 	    "         (PCRE2 only. Compiled PCRE2 regular expressions are\n\t"
 	    "         not portable across architectures. When linked against\n\t"
 	    "         PCRE this flag is ignored)\n\t"
+	    "-i       Print regular expression info end exit. That is, back\n\t"
+	    "         end version and architecture identifier.\n\t"
+	    "         Arch identifier format (PCRE2):\n\t"
+	    "         <pointer width>-<size type width>-<endianness>, e.g.,\n\t"
+	    "         \"8-8-el\" for x86_64.\n\t"
 	    "fc_file  The text based file contexts file to be processed.\n",
 	    progname);
 		exit(EXIT_FAILURE);
@@ -301,7 +306,7 @@ int main(int argc, char *argv[])
 	if (argc < 2)
 		usage(argv[0]);
 
-	while ((opt = getopt(argc, argv, "o:p:r")) > 0) {
+	while ((opt = getopt(argc, argv, "io:p:r")) > 0) {
 		switch (opt) {
 		case 'o':
 			out_file = optarg;
@@ -312,6 +317,10 @@ int main(int argc, char *argv[])
 		case 'r':
 			do_write_precompregex = 1;
 			break;
+		case 'i':
+			printf("%s (%s)\n", regex_version(),
+					regex_arch_string());
+			return 0;
 		default:
 			usage(argv[0]);
 		}
Signature · 1 line
2.8.0.rc3.226.g39d4020
#2 lore raw

PATCH 3/3 libselinux: sefcontext_compile invert semantics of "-r" flag

In reply to Janis Danisevskis

The "-r" flag of sefcontext_compile now causes it to omit the precompiled regular expressions from the output.

---
 libselinux/utils/sefcontext_compile.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
libselinux/utils/sefcontext_compile.c +7 -5
index 8c48d32..b2746c7 100644
@@ -276,10 +276,12 @@ static void usage(const char *progname)
 	    "         will be fc_file with the .bin suffix appended.\n\t"
 	    "-p       Optional binary policy file that will be used to\n\t"
 	    "         validate contexts defined in the fc_file.\n\t"
-	    "-r       Include precompiled regular expressions in the output.\n\t"
+	    "-r       Omit precompiled regular expressions from the output.\n\t"
 	    "         (PCRE2 only. Compiled PCRE2 regular expressions are\n\t"
-	    "         not portable across architectures. When linked against\n\t"
-	    "         PCRE this flag is ignored)\n\t"
+	    "         not portable across architectures. Use this flag\n\t"
+	    "         if you know that you build for an incompatible\n\t"
+	    "         architecture to save space. When linked against\n\t"
+	    "         PCRE1 this flag is ignored.)\n\t"
 	    "-i       Print regular expression info end exit. That is, back\n\t"
 	    "         end version and architecture identifier.\n\t"
 	    "         Arch identifier format (PCRE2):\n\t"
@@ -294,7 +296,7 @@ int main(int argc, char *argv[])
 {
 	const char *path = NULL;
 	const char *out_file = NULL;
-	int do_write_precompregex = 0;
+	int do_write_precompregex = 1;
 	char stack_path[PATH_MAX + 1];
 	char *tmp = NULL;
 	int fd, rc, opt;
@@ -315,7 +317,7 @@ int main(int argc, char *argv[])
 			policy_file = optarg;
 			break;
 		case 'r':
-			do_write_precompregex = 1;
+			do_write_precompregex = 0;
 			break;
 		case 'i':
 			printf("%s (%s)\n", regex_version(),
Signature · 1 line
2.8.0.rc3.226.g39d4020
#3 lore raw

Re: [PATCH 3/3] libselinux: sefcontext_compile invert semantics of "-r" flag

Quoted 2 lines · Janis Danisevskis

The "-r" flag of sefcontext_compile now causes it to omit the precompiled regular expressions from the output.

The code itself looks ok, aside from William's suggestion. Experimenting with this a bit, I noticed the following difference in sizes among the various options:

383165 file_contexts (text)
1507941 file_contexts.bin (binary with pcre1 regexes) 8304105 file_contexts.bin (binary with pcre2 regexes) 540540 file_contexts.bin (binary omitting pcre2 regexes, via -r)

The increase in file_contexts.bin size from pcre1 to pcre2 (unless using -r) is quite substantial. Wondering how that affects the cost/benefit tradeoff...

Quoted 45 lines
---
 libselinux/utils/sefcontext_compile.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
libselinux/utils/sefcontext_compile.c +7 -5
index 8c48d32..b2746c7 100644
@@ -276,10 +276,12 @@ static void usage(const char *progname)
 	    "         will be fc_file with the .bin suffix appended.\n\t"
 	    "-p       Optional binary policy file that will be used to\n\t"
 	    "         validate contexts defined in the fc_file.\n\t"
-	    "-r       Include precompiled regular expressions in the output.\n\t"
+	    "-r       Omit precompiled regular expressions from the output.\n\t"
 	    "         (PCRE2 only. Compiled PCRE2 regular expressions are\n\t"
-	    "         not portable across architectures. When linked against\n\t"
-	    "         PCRE this flag is ignored)\n\t"
+	    "         not portable across architectures. Use this flag\n\t"
+	    "         if you know that you build for an incompatible\n\t"
+	    "         architecture to save space. When linked against\n\t"
+	    "         PCRE1 this flag is ignored.)\n\t"
 	    "-i       Print regular expression info end exit. That is, back\n\t"
 	    "         end version and architecture identifier.\n\t"
 	    "         Arch identifier format (PCRE2):\n\t"
@@ -294,7 +296,7 @@ int main(int argc, char *argv[])
 {
 	const char *path = NULL;
 	const char *out_file = NULL;
-	int do_write_precompregex = 0;
+	int do_write_precompregex = 1;
 	char stack_path[PATH_MAX + 1];
 	char *tmp = NULL;
 	int fd, rc, opt;
@@ -315,7 +317,7 @@ int main(int argc, char *argv[])
 			policy_file = optarg;
 			break;
 		case 'r':
-			do_write_precompregex = 1;
+			do_write_precompregex = 0;
 			break;
 		case 'i':
 			printf("%s (%s)\n", regex_version(),
#4 lore raw

Re: [PATCH 3/3] libselinux: sefcontext_compile invert semantics of "-r" flag

Quoted 8 lines · Stephen Smalley
Quoted 2 lines · Janis Danisevskis

The "-r" flag of sefcontext_compile now causes it to omit the precompiled regular expressions from the output.

The code itself looks ok, aside from William's suggestion. Experimenting with this a bit, I noticed the following difference in sizes among the various options:

Quoted 8 lines

383165 file_contexts (text)
1507941 file_contexts.bin (binary with pcre1 regexes) 8304105 file_contexts.bin (binary with pcre2 regexes) 540540 file_contexts.bin (binary omitting pcre2 regexes, via -r)

The increase in file_contexts.bin size from pcre1 to pcre2 (unless using -r) is quite substantial. Wondering how that affects the cost/benefit tradeoff...

I think a lot of it might be with how complex your regex's are. Android is pretty simple, which shows, as textual and binary load times are pretty close. I think judicious use of -r and knowing what works best for your arch/build/fc entries, at the moment, is likely required.

<snip>

#5 lore raw

Re: [PATCH 3/3] libselinux: sefcontext_compile invert semantics of "-r" flag

In reply to Stephen Smalley
Quoted 4 lines · Stephen Smalley
Quoted 2 lines · Janis Danisevskis

The "-r" flag of sefcontext_compile now causes it to omit the precompiled regular expressions from the output.

Quoted 8 lines

The code itself looks ok, aside from William's suggestion. Experimenting with this a bit, I noticed the following difference in sizes among the various options:

383165 file_contexts (text)
1507941 file_contexts.bin (binary with pcre1 regexes) 8304105 file_contexts.bin (binary with pcre2 regexes) 540540 file_contexts.bin (binary omitting pcre2 regexes, via -r)

What's the size of the textual intermediate file?

Quoted 51 lines

The increase in file_contexts.bin size from pcre1 to pcre2 (unless using -r) is quite substantial. Wondering how that affects the cost/benefit tradeoff...

Quoted 45 lines
---
 libselinux/utils/sefcontext_compile.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
libselinux/utils/sefcontext_compile.c +7 -5
index 8c48d32..b2746c7 100644
@@ -276,10 +276,12 @@ static void usage(const char *progname)
          "         will be fc_file with the .bin suffix appended.\n\t"
          "-p       Optional binary policy file that will be used to\n\t"
          "         validate contexts defined in the fc_file.\n\t"
-         "-r       Include precompiled regular expressions in the output.\n\t"
+         "-r       Omit precompiled regular expressions from the output.\n\t"
          "         (PCRE2 only. Compiled PCRE2 regular expressions are\n\t"
-         "         not portable across architectures. When linked against\n\t"
-         "         PCRE this flag is ignored)\n\t"
+         "         not portable across architectures. Use this flag\n\t"
+         "         if you know that you build for an incompatible\n\t"
+         "         architecture to save space. When linked against\n\t"
+         "         PCRE1 this flag is ignored.)\n\t"
          "-i       Print regular expression info end exit. That is, back\n\t"
          "         end version and architecture identifier.\n\t"
          "         Arch identifier format (PCRE2):\n\t"
@@ -294,7 +296,7 @@ int main(int argc, char *argv[])
 {
      const char *path = NULL;
      const char *out_file = NULL;
-     int do_write_precompregex = 0;
+     int do_write_precompregex = 1;
      char stack_path[PATH_MAX + 1];
      char *tmp = NULL;
      int fd, rc, opt;
@@ -315,7 +317,7 @@ int main(int argc, char *argv[])
                      policy_file = optarg;
                      break;
              case 'r':
-                     do_write_precompregex = 1;
+                     do_write_precompregex = 0;
                      break;
              case 'i':
                      printf("%s (%s)\n", regex_version(),
Signature · 3 lines
Respectfully,

William C Roberts
#6 lore raw

Re: [PATCH 3/3] libselinux: sefcontext_compile invert semantics of "-r" flag

Quoted 9 lines · William Roberts
Quoted 8 lines · Stephen Smalley
Quoted 2 lines · Janis Danisevskis

The "-r" flag of sefcontext_compile now causes it to omit the precompiled regular expressions from the output.

The code itself looks ok, aside from William's suggestion. Experimenting with this a bit, I noticed the following difference in sizes among the various options:

Quoted 6 lines
Quoted 4 lines

383165 file_contexts (text)
1507941 file_contexts.bin (binary with pcre1 regexes) 8304105 file_contexts.bin (binary with pcre2 regexes) 540540 file_contexts.bin (binary omitting pcre2 regexes, via -r)

What's the size of the textual intermediate file?

This was just taking the /etc/selinux/targeted/contexts/files/file_contexts file on Fedora and running it through sefcontext_compile built with and without USE_PCRE2=y and in the PCRE2 case, running it with and without -r. So the text size above is exactly what was fed into sefcontext_compile, no intermediates.

Quoted 55 lines
Quoted 52 lines

The increase in file_contexts.bin size from pcre1 to pcre2 (unless using -r) is quite substantial. Wondering how that affects the cost/benefit tradeoff...

Quoted 45 lines
---
 libselinux/utils/sefcontext_compile.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
libselinux/utils/sefcontext_compile.c +7 -5
index 8c48d32..b2746c7 100644
@@ -276,10 +276,12 @@ static void usage(const char *progname)
          "         will be fc_file with the .bin suffix appended.\n\t"
          "-p       Optional binary policy file that will be used to\n\t"
          "         validate contexts defined in the fc_file.\n\t"
-         "-r       Include precompiled regular expressions in the output.\n\t"
+         "-r       Omit precompiled regular expressions from the output.\n\t"
          "         (PCRE2 only. Compiled PCRE2 regular expressions are\n\t"
-         "         not portable across architectures. When linked against\n\t"
-         "         PCRE this flag is ignored)\n\t"
+         "         not portable across architectures. Use this flag\n\t"
+         "         if you know that you build for an incompatible\n\t"
+         "         architecture to save space. When linked against\n\t"
+         "         PCRE1 this flag is ignored.)\n\t"
          "-i       Print regular expression info end exit. That is, back\n\t"
          "         end version and architecture identifier.\n\t"
          "         Arch identifier format (PCRE2):\n\t"
@@ -294,7 +296,7 @@ int main(int argc, char *argv[])
 {
      const char *path = NULL;
      const char *out_file = NULL;
-     int do_write_precompregex = 0;
+     int do_write_precompregex = 1;
      char stack_path[PATH_MAX + 1];
      char *tmp = NULL;
      int fd, rc, opt;
@@ -315,7 +317,7 @@ int main(int argc, char *argv[])
                      policy_file = optarg;
                      break;
              case 'r':
-                     do_write_precompregex = 1;
+                     do_write_precompregex = 0;
                      break;
              case 'i':
                      printf("%s (%s)\n", regex_version(),
#7 lore raw
In reply to Janis Danisevskis
Quoted 27 lines · Janis Danisevskis

Serialized precompiled regular expressins are architecture dependent when using PCRE2. This patch
- bumps the SELINUX_COMPILED_FCONTEXT version to 5 and
- adds a field to the output indicating the architecture compatibility.

libselinux can cope with an architecture mismatch by ignoring the precompiled data in the input file and recompiling the regular expressions at runtime. It can also load older versions of file_contexts.bin if they where built with sefcontext_compile using the exact same version of the pcre1/2 as selinux.

---
 libselinux/src/label_file.c           | 44 +++++++++++++++++++++++++++++-
 libselinux/src/label_file.h           |  4 ++-
 libselinux/src/regex.c                | 50 ++++++++++++++++++++++++++++++++---
 libselinux/src/regex.h                | 15 ++++++++++-
 libselinux/utils/sefcontext_compile.c | 13 +++++++++
 5 files changed, 120 insertions(+), 6 deletions(-)
libselinux/src/label_file.c +0 -0
index 7156825..9abc1d6 100644

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec, · line 130

Quoted 5 lines
libselinux/src/label_file.c +2 -0
@@ -126,6 +126,8 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
        uint32_t i, magic, version;
        uint32_t entry_len, stem_map_len, regex_array_len;
        const char *reg_version;
+       const char *reg_arch;
+       char reg_arch_matches = 1;

It appears all the patchs would reset this to 0 properly, but is their any reason you
chose the default to be that it matches? I would assume it would be safer to mark
this as its not a match.

Quoted 249 lines
libselinux/src/label_file.c +41 -1
        mmap_area = malloc(sizeof(*mmap_area));
        if (!mmap_area) {
@@ -159,6 +161,10 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
        if (!reg_version)
                return -1;
 
+       reg_arch = regex_arch_string();
+       if (!reg_arch)
+               return -1;
+
        if (version >= SELINUX_COMPILED_FCONTEXT_PCRE_VERS) {
 
                len = strlen(reg_version);
@@ -188,7 +194,43 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
                        return -1;
                }
                free(str_buf);
+
+               if (version >= SELINUX_COMPILED_FCONTEXT_REGEX_ARCH) {
+                       len = strlen(reg_arch);
+
+                       rc = next_entry(&entry_len, mmap_area,
+                                       sizeof(uint32_t));
+                       if (rc < 0)
+                               return -1;
+
+                       /* Check arch string lengths */
+                       if (len != entry_len) {
+                               /*
+                                * Skip the entry and conclude that we have
+                                * a mismatch, which is not fatal.
+                                */
+                               next_entry(NULL, mmap_area, entry_len);
+                               reg_arch_matches = 0;
+                               goto end_arch_check;
+                       }
+
+                       /* Check if arch string mismatch */
+                       str_buf = malloc(entry_len + 1);
+                       if (!str_buf)
+                               return -1;
+
+                       rc = next_entry(str_buf, mmap_area, entry_len);
+                       if (rc < 0) {
+                               free(str_buf);
+                               return -1;
+                       }
+
+                       str_buf[entry_len] = '\0';
+                       reg_arch_matches = strcmp(str_buf, reg_arch) == 0;
+                       free(str_buf);
+               }
        }
+end_arch_check:
 
        /* allocate the stems_data array */
        rc = next_entry(&stem_map_len, mmap_area, sizeof(uint32_t));
@@ -349,7 +391,7 @@ static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,
                        spec->prefix_len = prefix_len;
                }
 
-               rc = regex_load_mmap(mmap_area, &spec->regex);
+               rc = regex_load_mmap(mmap_area, &spec->regex, reg_arch_matches);
                if (rc < 0)
                        goto out;
 
libselinux/src/label_file.h +3 -1
index 88f4294..00c0a5c 100644
@@ -24,8 +24,10 @@
 #define SELINUX_COMPILED_FCONTEXT_PCRE_VERS    2
 #define SELINUX_COMPILED_FCONTEXT_MODE         3
 #define SELINUX_COMPILED_FCONTEXT_PREFIX_LEN   4
+#define SELINUX_COMPILED_FCONTEXT_REGEX_ARCH   5
 
-#define SELINUX_COMPILED_FCONTEXT_MAX_VERS     SELINUX_COMPILED_FCONTEXT_PREFIX_LEN
+#define SELINUX_COMPILED_FCONTEXT_MAX_VERS \
+       SELINUX_COMPILED_FCONTEXT_REGEX_ARCH
 
 /* A file security context specification. */
 struct spec {
libselinux/src/regex.c +47 -3
index 750088e..a3b427b 100644
@@ -7,6 +7,44 @@
 #include "label_file.h"
 
 #ifdef USE_PCRE2
+#define REGEX_ARCH_SIZE_T PCRE2_SIZE
+#else
+#define REGEX_ARCH_SIZE_T size_t
+#endif
+
+#ifndef __BYTE_ORDER__
+#error __BYTE_ORDER__ not defined. Unable to determine endianness.
+#endif
+
+#ifdef USE_PCRE2
+char const *regex_arch_string(void)
+{
+       static char arch_string_buffer[32];
+       static char const *arch_string = "";
+       char const *endianness = NULL;
+       int rc;
+
+       if (arch_string[0] == '\0') {
+               if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
+                       endianness = "el";
+               else if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+                       endianness = "eb";
+
+               if (!endianness)
+                       return NULL;
+
+               rc = snprintf(arch_string_buffer, sizeof(arch_string_buffer),
+                               "%zu-%zu-%s", sizeof(void *),
+                               sizeof(REGEX_ARCH_SIZE_T),
+                               endianness);
+               if (rc < 0)
+                       abort();
+
+               arch_string = &arch_string_buffer[0];
+       }
+       return arch_string;
+}
+
 struct regex_data {
        pcre2_code *regex; /* compiled regular expression */
        /*
@@ -56,7 +94,8 @@ char const *regex_version(void)
        return version_buf;
 }
 
-int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex)
+int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex,
+                   int do_load_precompregex)
 {
        int rc;
        uint32_t entry_len;
@@ -65,7 +104,7 @@ int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex)
        if (rc < 0)
                return -1;
 
-       if (entry_len) {
+       if (entry_len && do_load_precompregex) {
                /*
                 * this should yield exactly one because we store one pattern at
                 * a time
@@ -195,6 +234,10 @@ int regex_cmp(struct regex_data *regex1, struct regex_data *regex2)
 }
 
 #else // !USE_PCRE2
+char const *regex_arch_string(void)
+{
+       return "N/A";
+}
 
 /* Prior to version 8.20, libpcre did not have pcre_free_study() */
 #if (PCRE_MAJOR < 8 || (PCRE_MAJOR == 8 && PCRE_MINOR < 20))
@@ -247,7 +290,8 @@ char const *regex_version(void)
        return pcre_version();
 }
 
-int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex)
+int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex,
+                   int unused __attribute__((unused)))
 {
        int rc;
        uint32_t entry_len;
libselinux/src/regex.h +14 -1
index 810b3c5..186c5ec 100644
@@ -34,6 +34,16 @@ struct regex_error_data {
 struct mmap_area;
 
 /**
+ * regex_arch_string return a string that represents the pointer width, the
+ * width of what the backend considers a size type, and the endianness of the
+ * system that this library was build for. (e.g. for x86_64: "8-8-el").
+ * This is required when loading stored regular espressions. PCRE2 regular
+ * expressions are not portable across architectures that do not have a
+ * matching arch-string.
+ */
+char const *regex_arch_string(void) hidden;
+
+/**
  * regex_verison returns the version string of the underlying regular
  * regular expressions library. In the case of PCRE it just returns the
  * result of pcre_version(). In the case of PCRE2, the very first time this
@@ -86,12 +96,15 @@ int regex_prepare_data(struct regex_data **regex, char const *pattern_string,
  *               representation of the precompiled pattern.
  * @arg regex If successful, the structure returned through *regex was allocated
  *            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).
  *
  * @retval 0 on success
  * @retval -1 on error
  */
 int regex_load_mmap(struct mmap_area *map_area,
-                   struct regex_data **regex) hidden;
+                   struct regex_data **regex,
+                   int do_load_precompregex) hidden;
 /**
  * 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 +13 -1
index 70853e7..d91db9a 100644
@@ -103,6 +103,7 @@ static int write_binary_file(struct saved_data *data, int fd,
        uint32_t i;
        int rc;
        const char *reg_version;
+       const char *reg_arch;
 
        bin_file = fdopen(fd, "w");
        if (!bin_file) {
@@ -133,6 +134,18 @@ static int write_binary_file(struct saved_data *data, int fd,
        if (len != section_len)
                goto err;
 
+       /* write regex arch string */
+       reg_arch = regex_arch_string();
+       if (!reg_arch)
+               goto err;
+       section_len = strlen(reg_arch);
+       len = fwrite(&section_len, sizeof(uint32_t), 1, bin_file);
+       if (len != 1)
+               goto err;
+       len = fwrite(reg_arch, sizeof(char), section_len, bin_file);
+       if (len != section_len)
+               goto err;
+
        /* write the number of stems coming */
        section_len = data->num_stems;
        len = fwrite(&section_len, sizeof(uint32_t), 1, bin_file);
--

2.8.0.rc3.226.g39d4020

_______________________________________________
Seandroid-list mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].

Signature · 3 lines
Respectfully,

William C Roberts
#8 lore raw

My line of thought was that all pre version 5 .bin files should load the regexes from the file. However, given that the PCRE back end ignores the flag and that only .bin files of version < 5 that where build with sefcompile_context that was linked against PCRE2 would be affected, I would agree that reg_arch_matches = 0 would be the saner default setting. I'll modify the patch accordingly.

Thanks

Quoted 328 lines · On Mon, 26 Sep 2016, 17:29 William Roberts, <[email protected]> wrote:
Quoted 13 lines · Janis Danisevskis

Serialized precompiled regular expressins are architecture dependent when using PCRE2. This patch
- bumps the SELINUX_COMPILED_FCONTEXT version to 5 and
- adds a field to the output indicating the architecture compatibility.

libselinux can cope with an architecture mismatch by ignoring the precompiled data in the input file and recompiling the regular expressions at runtime. It can also load older versions of file_contexts.bin if they where built with sefcontext_compile using the exact same version of the pcre1/2 as selinux.

Quoted 3 lines
---
 libselinux/src/label_file.c           | 44

+++++++++++++++++++++++++++++-

Quoted 2 lines
 libselinux/src/label_file.h           |  4 ++-
 libselinux/src/regex.c                | 50

++++++++++++++++++++++++++++++++---

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 9 lines
 libselinux/src/regex.h                | 15 ++++++++++-
 libselinux/utils/sefcontext_compile.c | 13 +++++++++
 5 files changed, 120 insertions(+), 6 deletions(-)
libselinux/src/label_file.c +0 -0
index 7156825..9abc1d6 100644
@@ -126,6 +126,8 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec, · line 130

Quoted 5 lines
libselinux/src/label_file.c +2 -0
        uint32_t i, magic, version;
        uint32_t entry_len, stem_map_len, regex_array_len;
        const char *reg_version;
+       const char *reg_arch;
+       char reg_arch_matches = 1;

It appears all the patchs would reset this to 0 properly, but is their any reason you
chose the default to be that it matches? I would assume it would be safer to mark
this as its not a match.

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 4 lines
libselinux/src/label_file.c +0 -0
        mmap_area = malloc(sizeof(*mmap_area));
        if (!mmap_area) {
@@ -159,6 +161,10 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 11 lines
libselinux/src/label_file.c +4 -0
        if (!reg_version)
                return -1;
 
+       reg_arch = regex_arch_string();
+       if (!reg_arch)
+               return -1;
+
        if (version >= SELINUX_COMPILED_FCONTEXT_PCRE_VERS) {
 
                len = strlen(reg_version);
@@ -188,7 +194,43 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

Quoted 6 lines
libselinux/src/label_file.c +3 -0
                        return -1;
                }
                free(str_buf);
+
+               if (version >= SELINUX_COMPILED_FCONTEXT_REGEX_ARCH) {
+                       len = strlen(reg_arch);

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.c +10 -0
+
+                       rc = next_entry(&entry_len, mmap_area,
+                                       sizeof(uint32_t));
+                       if (rc < 0)
+                               return -1;
+
+                       /* Check arch string lengths */
+                       if (len != entry_len) {
+                               /*
+                                * Skip the entry and conclude that we

have

Quoted 10 lines
libselinux/src/label_file.c +10 -0
+                                * a mismatch, which is not fatal.
+                                */
+                               next_entry(NULL, mmap_area, entry_len);
+                               reg_arch_matches = 0;
+                               goto end_arch_check;
+                       }
+
+                       /* Check if arch string mismatch */
+                       str_buf = malloc(entry_len + 1);
+                       if (!str_buf)

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 10 lines
libselinux/src/label_file.c +10 -0
+                               return -1;
+
+                       rc = next_entry(str_buf, mmap_area, entry_len);
+                       if (rc < 0) {
+                               free(str_buf);
+                               return -1;
+                       }
+
+                       str_buf[entry_len] = '\0';
+                       reg_arch_matches = strcmp(str_buf, reg_arch) ==

0;

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct

Quoted 8 lines
libselinux/src/label_file.c +3 -0
+                       free(str_buf);
+               }
        }
+end_arch_check:
 
        /* allocate the stems_data array */
        rc = next_entry(&stem_map_len, mmap_area, sizeof(uint32_t));
@@ -349,7 +391,7 @@ static int load_mmap(FILE *fp, size_t len, struct

selabel_handle *rec,

libselinux/src/label_file.c static int load_mmap(FILE *fp, size_t len, struct selabel_handle *rec,

Quoted 5 lines
libselinux/src/label_file.c +1 -1
                        spec->prefix_len = prefix_len;
                }
 
-               rc = regex_load_mmap(mmap_area, &spec->regex);
+               rc = regex_load_mmap(mmap_area, &spec->regex,

reg_arch_matches);

Quoted 8 lines
libselinux/src/label_file.c +0 -0
                if (rc < 0)
                        goto out;
 
libselinux/src/label_file.h +0 -0
index 88f4294..00c0a5c 100644

libselinux/src/label_file.h line 28

Quoted 6 lines
libselinux/src/label_file.h +1 -1
@@ -24,8 +24,10 @@
 #define SELINUX_COMPILED_FCONTEXT_PCRE_VERS    2
 #define SELINUX_COMPILED_FCONTEXT_MODE         3
 #define SELINUX_COMPILED_FCONTEXT_PREFIX_LEN   4
+#define SELINUX_COMPILED_FCONTEXT_REGEX_ARCH   5
 
-#define SELINUX_COMPILED_FCONTEXT_MAX_VERS

SELINUX_COMPILED_FCONTEXT_PREFIX_LEN

Quoted 30 lines
libselinux/src/label_file.h +2 -0
+#define SELINUX_COMPILED_FCONTEXT_MAX_VERS \
+       SELINUX_COMPILED_FCONTEXT_REGEX_ARCH
 
 /* A file security context specification. */
 struct spec {
libselinux/src/regex.c +17 -0
index 750088e..a3b427b 100644
@@ -7,6 +7,44 @@
 #include "label_file.h"
 
 #ifdef USE_PCRE2
+#define REGEX_ARCH_SIZE_T PCRE2_SIZE
+#else
+#define REGEX_ARCH_SIZE_T size_t
+#endif
+
+#ifndef __BYTE_ORDER__
+#error __BYTE_ORDER__ not defined. Unable to determine endianness.
+#endif
+
+#ifdef USE_PCRE2
+char const *regex_arch_string(void)
+{
+       static char arch_string_buffer[32];
+       static char const *arch_string = "";
+       char const *endianness = NULL;
+       int rc;
+

libselinux/src/regex.c line 36

Quoted 10 lines
libselinux/src/regex.c +10 -0
+       if (arch_string[0] == '\0') {
+               if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
+                       endianness = "el";
+               else if (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
+                       endianness = "eb";
+
+               if (!endianness)
+                       return NULL;
+
+               rc = snprintf(arch_string_buffer,

sizeof(arch_string_buffer),

Quoted 15 lines
libselinux/src/regex.c +11 -0
+                               "%zu-%zu-%s", sizeof(void *),
+                               sizeof(REGEX_ARCH_SIZE_T),
+                               endianness);
+               if (rc < 0)
+                       abort();
+
+               arch_string = &arch_string_buffer[0];
+       }
+       return arch_string;
+}
+
 struct regex_data {
        pcre2_code *regex; /* compiled regular expression */
        /*

libselinux/src/regex.c char const *regex_version(void) · line 59

Quoted 4 lines
libselinux/src/regex.c +0 -1
@@ -56,7 +94,8 @@ char const *regex_version(void)
        return version_buf;
 }
 
-int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data

**regex)

libselinux/src/regex.c char const *regex_version(void) · line 97

Quoted 1 line
libselinux/src/regex.c +1 -0
+int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data

**regex,

libselinux/src/regex.c int regex_load_mmap(struct mmap_area *mmap_area,

Quoted 5 lines
libselinux/src/regex.c +1 -0
+                   int do_load_precompregex)
 {
        int rc;
        uint32_t entry_len;
@@ -65,7 +104,7 @@ int regex_load_mmap(struct mmap_area *mmap_area,

struct regex_data **regex)

libselinux/src/regex.c int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data **regex)

Quoted 7 lines
libselinux/src/regex.c +1 -1
        if (rc < 0)
                return -1;
 
-       if (entry_len) {
+       if (entry_len && do_load_precompregex) {
                /*
                 * this should yield exactly one because we store one

pattern at

libselinux/src/regex.c int regex_cmp(struct regex_data *regex1, struct

Quoted 2 lines
libselinux/src/regex.c +0 -0
                 * a time
@@ -195,6 +234,10 @@ int regex_cmp(struct regex_data *regex1, struct

regex_data *regex2)

Quoted 11 lines
libselinux/src/regex.c +4 -0
 }
 
 #else // !USE_PCRE2
+char const *regex_arch_string(void)
+{
+       return "N/A";
+}
 
 /* Prior to version 8.20, libpcre did not have pcre_free_study() */
 #if (PCRE_MAJOR < 8 || (PCRE_MAJOR == 8 && PCRE_MINOR < 20))

libselinux/src/regex.c char const *regex_version(void) · line 250

Quoted 4 lines
libselinux/src/regex.c +0 -1
@@ -247,7 +290,8 @@ char const *regex_version(void)
        return pcre_version();
 }
 
-int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data

**regex)

libselinux/src/regex.c char const *regex_version(void) · line 293

Quoted 1 line
libselinux/src/regex.c +1 -0
+int regex_load_mmap(struct mmap_area *mmap_area, struct regex_data

**regex,

libselinux/src/regex.h struct regex_error_data { · line 37

Quoted 13 lines
libselinux/src/regex.c +1 -0
+                   int unused __attribute__((unused)))
 {
        int rc;
        uint32_t entry_len;
libselinux/src/regex.h +1 -0
index 810b3c5..186c5ec 100644
@@ -34,6 +34,16 @@ struct regex_error_data {
 struct mmap_area;
 
 /**
+ * regex_arch_string return a string that represents the pointer width,

the

libselinux/src/regex.h struct regex_error_data { · line 38

Quoted 1 line
libselinux/src/regex.h +1 -0
+ * width of what the backend considers a size type, and the endianness

of the

libselinux/src/regex.h struct regex_error_data { · line 40

Quoted 2 lines
libselinux/src/regex.h +2 -0
+ * system that this library was build for. (e.g. for x86_64: "8-8-el").
+ * This is required when loading stored regular espressions. PCRE2

regular

libselinux/src/regex.h struct regex_error_data {

Quoted 9 lines
libselinux/src/regex.h +6 -0
+ * expressions are not portable across architectures that do not have a
+ * matching arch-string.
+ */
+char const *regex_arch_string(void) hidden;
+
+/**
  * regex_verison returns the version string of the underlying regular
  * regular expressions library. In the case of PCRE it just returns the
  * result of pcre_version(). In the case of PCRE2, the very first time

this

libselinux/src/regex.h int regex_prepare_data(struct regex_data **regex,

Quoted 1 line
libselinux/src/regex.h +0 -0
@@ -86,12 +96,15 @@ int regex_prepare_data(struct regex_data **regex,

char const *pattern_string,

libselinux/src/regex.h int regex_prepare_data(struct regex_data **regex, · line 97

Quoted 2 lines
libselinux/src/regex.h +0 -0
  *               representation of the precompiled pattern.
  * @arg regex If successful, the structure returned through *regex was

allocated

libselinux/src/regex.h int regex_prepare_data(struct regex_data **regex, · line 98

Quoted 1 line
libselinux/src/regex.h +0 -0
  *            with regex_data_create and must be freed with

regex_data_free.

libselinux/src/regex.h int regex_prepare_data(struct regex_data **regex, · line 99

Quoted 1 line
libselinux/src/regex.h +1 -0
+ * @arg do_load_precompregex If non-zero precompiled patterns get

loaded from

Review

Quoted 13 lines
libselinux/src/regex.h +3 -1
+ *                          the mmap region (ignored by PCRE1 back-end).
  *
  * @retval 0 on success
  * @retval -1 on error
  */
 int regex_load_mmap(struct mmap_area *map_area,
-                   struct regex_data **regex) hidden;
+                   struct regex_data **regex,
+                   int do_load_precompregex) hidden;
 /**
  * This function stores a precompiled regular expression to a file.
  * In the case of PCRE, it just dumps the binary representation of the
(quoted diff) +0 -0

b/libselinux/utils/sefcontext_compile.c

libselinux/utils/sefcontext_compile.c static int write_binary_file(struct saved_data

Quoted 4 lines

index 70853e7..d91db9a 100644

libselinux/utils/sefcontext_compile.c +0 -0
@@ -103,6 +103,7 @@ static int write_binary_file(struct saved_data

*data, int fd,

libselinux/utils/sefcontext_compile.c static int write_binary_file(struct saved_data

Quoted 8 lines
libselinux/utils/sefcontext_compile.c +1 -0
        uint32_t i;
        int rc;
        const char *reg_version;
+       const char *reg_arch;
 
        bin_file = fdopen(fd, "w");
        if (!bin_file) {
@@ -133,6 +134,18 @@ static int write_binary_file(struct saved_data

*data, int fd,

Quoted 21 lines
libselinux/utils/sefcontext_compile.c +12 -1
        if (len != section_len)
                goto err;
 
+       /* write regex arch string */
+       reg_arch = regex_arch_string();
+       if (!reg_arch)
+               goto err;
+       section_len = strlen(reg_arch);
+       len = fwrite(&section_len, sizeof(uint32_t), 1, bin_file);
+       if (len != 1)
+               goto err;
+       len = fwrite(reg_arch, sizeof(char), section_len, bin_file);
+       if (len != section_len)
+               goto err;
+
        /* write the number of stems coming */
        section_len = data->num_stems;
        len = fwrite(&section_len, sizeof(uint32_t), 1, bin_file);
--

2.8.0.rc3.226.g39d4020

Quoted 5 lines

_______________________________________________
Seandroid-list mailing list [email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to

[email protected].

--
Respectfully,

William C Roberts