Skip to contentSkip to conversation

libselinux: add support for pcre2

PATCH In review Janis Danisevskis 5 participants 19 messages Sep 15, 2016 – Sep 16, 2016 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libselinux: add support for pcre2

From: Janis Danisevskis <[email protected]>

This patch moves all pcre1/2 dependencies into the new files regex.h and regex.c implementing the common denominator of features needed by libselinux. The compiler flag -DUSE_PCRE2 toggles between the used implementations.

As of this patch libselinux supports either pcre or pcre2 but not both at the same time. The persistently stored file contexts information differs. This means libselinux can only load file context files generated by sefcontext_compile build with the same pcre variant.

Also, for pcre2 the persistent format is architecture dependent. Stored precompiled regular expressions can only be used on the same architecture they were generated on. If pcre2 is used, sefcontext_compile now respects the "-r". This flag makes sefcontext_compile include the precompiled regular expressions in the output file. The default is to omit them, so that the output remains portable at the cost of having to recompile the regular expressions at load time, or rather on first use.

---
 libselinux/Makefile                   |   9 +
 libselinux/src/Makefile               |   6 +-
 libselinux/src/label_file.c           |  95 ++-----
 libselinux/src/label_file.h           |  64 ++---
 libselinux/src/regex.c                | 496 ++++++++++++++++++++++++++++++++++
 libselinux/src/regex.h                | 149 ++++++++++
 libselinux/utils/Makefile             |   7 +-
 libselinux/utils/sefcontext_compile.c |  72 ++---
 policycoreutils/restorecond/Makefile  |   9 +-
 9 files changed, 740 insertions(+), 167 deletions(-)
 create mode 100644 libselinux/src/regex.c
 create mode 100644 libselinux/src/regex.h
libselinux/Makefile +9 -0
index 6142b60..5a8d42c 100644
@@ -24,6 +24,15 @@ ifeq ($(DISABLE_SETRANS),y)
 endif
 export DISABLE_AVC DISABLE_SETRANS DISABLE_RPM DISABLE_BOOL EMFLAGS
 
+USE_PCRE2 ?= n
+ifeq ($(USE_PCRE2),y)
+	PCRE_CFLAGS := -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8
+	PCRE_LDFLAGS := -lpcre2-8
+else
+	PCRE_LDFLAGS := -lpcre
+endif
+export PCRE_CFLAGS PCRE_LDFLAGS
+
 all install relabel clean distclean indent:
 	@for subdir in $(SUBDIRS); do \
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
libselinux/src/Makefile +4 -2
index 37d01af..36e42b8 100644
@@ -74,7 +74,9 @@ CFLAGS ?= -O -Wall -W -Wundef -Wformat-y2k -Wformat-security -Winit-self -Wmissi
           -fipa-pure-const -Wno-suggest-attribute=pure -Wno-suggest-attribute=const \
           -Werror -Wno-aggregate-return -Wno-redundant-decls
 
-override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE $(EMFLAGS)
+PCRE_LDFLAGS ?= -lpcre
+
+override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE $(EMFLAGS) $(PCRE_CFLAGS)
 
 SWIG_CFLAGS += -Wno-error -Wno-unused-variable -Wno-unused-but-set-variable -Wno-unused-parameter \
 		-Wno-shadow -Wno-uninitialized -Wno-missing-prototypes -Wno-missing-declarations
@@ -113,7 +115,7 @@ $(LIBA): $(OBJS)
 	$(RANLIB) $@
 
 $(LIBSO): $(LOBJS)
-	$(CC) $(CFLAGS) -shared -o $@ $^ -lpcre -ldl $(LDFLAGS) -L$(LIBDIR) -Wl,-soname,$(LIBSO),-z,defs,-z,relro
+	$(CC) $(CFLAGS) -shared -o $@ $^ $(PCRE_LDFLAGS) -ldl $(LDFLAGS) -L$(LIBDIR) -Wl,-soname,$(LIBSO),-z,defs,-z,relro
 	ln -sf $@ $(TARGET) 
 
 $(LIBPC): $(LIBPC).in ../VERSION
libselinux/src/label_file.c +22 -73

189 lines not on this page · raw message

libselinux/src/label_file.h +21 -43

128 lines not on this page · raw message

libselinux/src/regex.c new file +496 -0

496 lines not on this page · raw message

libselinux/src/regex.h new file +149 -0

149 lines not on this page · raw message

libselinux/utils/Makefile +5 -2
index 8497cb4..1943bef 100644
@@ -24,12 +24,15 @@ CFLAGS ?= -O -Wall -W -Wundef -Wformat-y2k -Wformat-security -Winit-self -Wmissi
           -fasynchronous-unwind-tables -fdiagnostics-show-option -funit-at-a-time \
           -fipa-pure-const -Wno-suggest-attribute=pure -Wno-suggest-attribute=const \
           -Werror -Wno-aggregate-return -Wno-redundant-decls
-override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE $(EMFLAGS)
+override CFLAGS += -I../include -I$(INCLUDEDIR) -D_GNU_SOURCE $(EMFLAGS) $(PCRE_CFLAGS)
 LDLIBS += -L../src -lselinux -L$(LIBDIR)
+PCRE_LDFLAGS ?= -lpcre
 
 TARGETS=$(patsubst %.c,%,$(wildcard *.c))
 
-sefcontext_compile: LDLIBS += -lpcre ../src/libselinux.a -lsepol
+sefcontext_compile: LDLIBS += $(PCRE_LDFLAGS) ../src/libselinux.a -lsepol
+
+sefcontext_compile: sefcontext_compile.o ../src/regex.o
 
 selinux_restorecon: LDLIBS += -lsepol
 
libselinux/utils/sefcontext_compile.c +26 -46

155 lines not on this page · raw message

policycoreutils/restorecond/Makefile +8 -1
index f99e1e7..253246d 100644
@@ -17,7 +17,14 @@ DBUSLIB = -ldbus-glib-1 -ldbus-1
 CFLAGS ?= -g -Werror -Wall -W
 override CFLAGS += -I$(PREFIX)/include $(DBUSFLAGS) -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -I/usr/lib/glib-2.0/include
 
-LDLIBS += -lselinux $(DBUSLIB) -lglib-2.0 -L$(LIBDIR)
+USE_PCRE2 ?= n
+ifeq ($(USE_PCRE2),y)
+	PCRE_LDFLAGS := -lpcre2-8
+else
+	PCRE_LDFLAGS := -lpcre
+endif
+
+LDLIBS += -lselinux $(PCRE_LDFLAGS) $(DBUSLIB) -lglib-2.0 -L$(LIBDIR)
 
 all: restorecond
 
Signature · 1 line
2.8.0.rc3.226.g39d4020
#1 lore raw
Quoted 13 lines · Janis Danisevskis

From: Janis Danisevskis <[email protected]>

This patch moves all pcre1/2 dependencies into the new files regex.h and regex.c implementing the common denominator of features needed by libselinux. The compiler flag -DUSE_PCRE2 toggles between the used implementations.

As of this patch libselinux supports either pcre or pcre2 but not both at the same time. The persistently stored file contexts information differs. This means libselinux can only load file context files generated by sefcontext_compile build with the same pcre variant.

Quoted 8 lines

Also, for pcre2 the persistent format is architecture dependent. Stored precompiled regular expressions can only be used on the same architecture they were generated on. If pcre2 is used, sefcontext_compile now respects the "-r". This flag makes sefcontext_compile include the precompiled regular expressions in the output file. The default is to omit them, so that the output remains portable at the cost of having to recompile the regular expressions at load time, or rather on first use.

Is that really the default behavior you want?

Quoted 12 lines

---

libselinux/src/label_file.h +0 -1
index 6d1e890..3df7972 100644
@@ -453,12 +429,14 @@ static inline int process_line(struct selabel_handle *rec,
 	 */
 	data->nspec++;
 
-	if (rec->validating &&

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec, · line 439

Quoted 10 lines
libselinux/src/label_file.h +5 -2
-			    compile_regex(data, &spec_arr[nspec], &errbuf)) {
+	if (rec->validating
+			&& compile_regex(data, &spec_arr[nspec], &error_data)) {
+		regex_format_error(&error_data, regex_error_format_buffer,
+				sizeof(regex_error_format_buffer));
 		COMPAT_LOG(SELINUX_ERROR,
 			   "%s:  line %u has invalid regex %s:  %s\n",
 			   path, lineno, regex,
-			   (errbuf ? errbuf : "out of memory"));
+			   regex_error_format_buffer);

compile_regex() may fail on an out of memory condition before regex_error_format_buffer is initialized, which is why we previously passed errbuf ?: "out of memory" above. I suppose you could initialize regex_error_format_buffer with "out of memory" prior to calling compile_regex().

Quoted 5 lines
libselinux/src/regex.c new file +0 -0
new file mode 100644
index 0000000..1c4a84d

<snip>

Quoted 1 line

+int regex_writef(struct regex_data *regex, FILE *fp)

This needs to be updated with the new do_write_precompregex argument, and either use the argument or mark it unused to permit compilation for the USE_PCRE2=n.

#2 lore raw
Quoted 23 lines · Stephen Smalley
Quoted 13 lines · Janis Danisevskis

From: Janis Danisevskis <[email protected]>

This patch moves all pcre1/2 dependencies into the new files regex.h and regex.c implementing the common denominator of features needed by libselinux. The compiler flag -DUSE_PCRE2 toggles between the used implementations.

As of this patch libselinux supports either pcre or pcre2 but not both at the same time. The persistently stored file contexts information differs. This means libselinux can only load file context files generated by sefcontext_compile build with the same pcre variant.

Quoted 8 lines

Also, for pcre2 the persistent format is architecture dependent. Stored precompiled regular expressions can only be used on the same architecture they were generated on. If pcre2 is used, sefcontext_compile now respects the "-r". This flag makes sefcontext_compile include the precompiled regular expressions in the output file. The default is to omit them, so that the output remains portable at the cost of having to recompile the regular expressions at load time, or rather on first use.

Quoted 1 line

Is that really the default behavior you want?

I would make the default include the pre-compiled regex's, if there is an arch mismatch then ditch the pre-compiled ones and reload them. I would make it an option to NOT generate pre-compiled regexes. For the upstream consumers, who compile and deploy on the same system, this means they don't need to update to add -r. For android, we just need to update a makefile to include -r to omit the precompiled regex's.

In the case of Android, where we cross compile the file_contexts.bin, does using binary fc's even make sense now? The major reason is that the binary format loads faster by skipping the regex compilation, we can no longer skip that with PCRE2 and cross compilation. We should likely move Android back to text based if this is the case. I have speedups in the neighborhood of 30% for text file processing in the pipeline.

Quoted 47 lines
Quoted 13 lines

---

libselinux/src/label_file.h +0 -1
index 6d1e890..3df7972 100644
@@ -453,12 +429,14 @@ static inline int process_line(struct selabel_handle *rec,
       */
      data->nspec++;
 
-     if (rec->validating &&

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec, · line 439

Quoted 10 lines
libselinux/src/label_file.h +5 -2
-                         compile_regex(data, &spec_arr[nspec], &errbuf)) {
+     if (rec->validating
+                     && compile_regex(data, &spec_arr[nspec], &error_data)) {
+             regex_format_error(&error_data, regex_error_format_buffer,
+                             sizeof(regex_error_format_buffer));
              COMPAT_LOG(SELINUX_ERROR,
                         "%s:  line %u has invalid regex %s:  %s\n",
                         path, lineno, regex,
-                        (errbuf ? errbuf : "out of memory"));
+                        regex_error_format_buffer);

compile_regex() may fail on an out of memory condition before regex_error_format_buffer is initialized, which is why we previously passed errbuf ?: "out of memory" above. I suppose you could initialize regex_error_format_buffer with "out of memory" prior to calling compile_regex().

Quoted 5 lines
libselinux/src/regex.c new file +0 -0
new file mode 100644
index 0000000..1c4a84d

<snip>

Quoted 1 line

+int regex_writef(struct regex_data *regex, FILE *fp)

This needs to be updated with the new do_write_precompregex argument, and either use the argument or mark it unused to permit compilation for the USE_PCRE2=n.

_______________________________________________
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
#3 lore raw

First of all, I would like to thank you, Stephen and William, for your patience and support.

On Thu, Sep 15, 2016 at 8:34 PM William Roberts <[email protected]> wrote:

Quoted 23 lines · Stephen Smalley
Quoted 13 lines · Janis Danisevskis

From: Janis Danisevskis <[email protected]>

This patch moves all pcre1/2 dependencies into the new files regex.h and regex.c implementing the common denominator of features needed by libselinux. The compiler flag -DUSE_PCRE2 toggles between the used implementations.

As of this patch libselinux supports either pcre or pcre2 but not both at the same time. The persistently stored file contexts information differs. This means libselinux can only load file context files generated by sefcontext_compile build with the same pcre variant.

Quoted 8 lines

Also, for pcre2 the persistent format is architecture dependent. Stored precompiled regular expressions can only be used on the same architecture they were generated on. If pcre2 is used, sefcontext_compile now respects the "-r". This flag makes sefcontext_compile include the precompiled regular expressions in the output file. The default is to omit them, so that the output remains portable at the cost of having to recompile the regular expressions at load time, or rather on first use.

Quoted 1 line

Is that really the default behavior you want?

I would make the default include the pre-compiled regex's, if there is an arch mismatch then ditch the pre-compiled ones and reload them. I would make it an option to NOT generate pre-compiled regexes. For the upstream consumers, who compile and deploy on the same system, this means they don't need to update to add -r. For android, we just need to update a makefile to include -r to omit the precompiled regex's.

The arch is currently not encoded in the binary, so a mismatch cannot be detected. I would sleep better if we left the -r option as is for now, because it is kind of "use only if you know what you are doing". Also, this way it does not break the android build process.

In the case of Android, where we cross compile the file_contexts.bin, does using binary fc's even make sense now? The major reason is that the binary format loads faster by skipping the regex compilation, we can no longer skip that with PCRE2 and cross compilation. We should likely move Android back to text based if this is the case. I have speedups in the neighborhood of 30% for text file processing in the pipeline.

I realize that the binary PCRE2 variant is currently much slower than the PCRE stored regexes. But it still a little faster than the text file processing. If you can make text file processing 30% faster, than this may well be an option because serializing pcre2 regexes will probably never beat pcre, even if we did stunts like compiling for the correct architecture using qemu.
I'd be happy to help perform benchmarks and choose the best option.

Also I am exploring another option, which may sound a bit exotic. So I would like to have a running POC first before I come forward. Maybe nothing comes of it.

Quoted 9 lines

libselinux/src/label_file.h static inline int process_line(struct

Quoted 9 lines

---

libselinux/src/label_file.h +0 -0
index 6d1e890..3df7972 100644
@@ -453,12 +429,14 @@ static inline int process_line(struct

selabel_handle *rec,

Quoted 5 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec,

Quoted 5 lines
libselinux/src/label_file.h +0 -2
       */
      data->nspec++;
 
-     if (rec->validating &&
-                         compile_regex(data, &spec_arr[nspec],

&errbuf)) {

Quoted 2 lines

libselinux/src/label_file.h static inline int process_line(struct · line 433

Quoted 2 lines
libselinux/src/label_file.h +2 -0
+     if (rec->validating
+                     && compile_regex(data, &spec_arr[nspec],

&error_data)) {

Quoted 26 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec, · line 439

Quoted 7 lines
libselinux/src/label_file.h +3 -1
+             regex_format_error(&error_data, regex_error_format_buffer,
+                             sizeof(regex_error_format_buffer));
              COMPAT_LOG(SELINUX_ERROR,
                         "%s:  line %u has invalid regex %s:  %s\n",
                         path, lineno, regex,
-                        (errbuf ? errbuf : "out of memory"));
+                        regex_error_format_buffer);

compile_regex() may fail on an out of memory condition before regex_error_format_buffer is initialized, which is why we previously passed errbuf ?: "out of memory" above. I suppose you could initialize regex_error_format_buffer with "out of memory" prior to calling compile_regex().

Quoted 5 lines
libselinux/src/regex.c new file +0 -0
new file mode 100644
index 0000000..1c4a84d

<snip>

Quoted 1 line

+int regex_writef(struct regex_data *regex, FILE *fp)

This needs to be updated with the new do_write_precompregex argument, and either use the argument or mark it unused to permit compilation for the USE_PCRE2=n.

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
_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].

#4 lore raw
Quoted 42 lines · Janis Danisevskis

First of all, I would like to thank you, Stephen and William, for your patience and support.

Quoted 28 lines · William Roberts
Quoted 23 lines · Stephen Smalley
Quoted 13 lines · Janis Danisevskis

From: Janis Danisevskis <[email protected]>

This patch moves all pcre1/2 dependencies into the new files regex.h and regex.c implementing the common denominator of features needed by libselinux. The compiler flag -DUSE_PCRE2 toggles between the used implementations.

As of this patch libselinux supports either pcre or pcre2 but not both at the same time. The persistently stored file contexts information differs. This means libselinux can only load file context files generated by sefcontext_compile build with the same pcre variant.

Quoted 8 lines

Also, for pcre2 the persistent format is architecture dependent. Stored precompiled regular expressions can only be used on the same architecture they were generated on. If pcre2 is used, sefcontext_compile now respects the "-r". This flag makes sefcontext_compile include the precompiled regular expressions in the output file. The default is to omit them, so that the output remains portable at the cost of having to recompile the regular expressions at load time, or rather on first use.

Quoted 1 line

Is that really the default behavior you want?

Quoted 7 lines

I would make the default include the pre-compiled regex's, if there is an arch mismatch then ditch the pre-compiled ones and reload them. I would make it an option to NOT generate pre-compiled regexes. For the upstream consumers, who compile and deploy on the same system, this means they don't need to update to add -r. For android, we just need to update a makefile to include -r to omit the precompiled regex's.

Quoted 4 lines

The arch is currently not encoded in the binary, so a mismatch cannot be detected. I would sleep better if we left the -r option as is for now, because it is kind of "use only if you know what you are doing". Also, this way it does not break the android build process.

We probably would want to encode the architecture since this matters.This way we can detect a mismatch and compile them. This,prevents the
why isn't my device booting issue for cross compilations that don't have -r. The other thing is, this is only an issue on binary formated file_context files, this is the ideal reason to move back to text files, since their is no speedup gained if we have to compile them.

Quoted 12 lines
Quoted 10 lines

In the case of Android, where we cross compile the file_contexts.bin, does using binary fc's even make sense now? The major reason is that the binary format loads faster by skipping the regex compilation, we can no longer skip that with PCRE2 and cross compilation. We should likely move Android back to text based if this is the case. I have speedups in the neighborhood of 30% for text file processing in the pipeline.

Quoted 7 lines

I realize that the binary PCRE2 variant is currently much slower than the PCRE stored regexes. But it still a little faster than the text file processing. If you can make text file processing 30% faster, than this may well be an option because serializing pcre2 regexes will probably never beat pcre, even if we did stunts like compiling for the correct architecture using qemu.
I'd be happy to help perform benchmarks and choose the best option.

Once you dump the pre-compiled regex's from the binary format, your back to the text files performance. Also, with PCRE, the binary format was 3x the size on disk.

Quoted 72 lines

Also I am exploring another option, which may sound a bit exotic. So I would like to have a running POC first before I come forward. Maybe nothing comes of it.

Quoted 67 lines
Quoted 48 lines
Quoted 19 lines

---

libselinux/src/label_file.h +1 -2
index 6d1e890..3df7972 100644
@@ -453,12 +429,14 @@ static inline int process_line(struct
 selabel_handle *rec,
       */
      data->nspec++;
 
-     if (rec->validating &&
-                         compile_regex(data, &spec_arr[nspec],
 &errbuf)) {
+     if (rec->validating

libselinux/src/label_file.h static inline int process_line(struct · line 439

Quoted 10 lines
libselinux/src/label_file.h +4 -1
+                     && compile_regex(data, &spec_arr[nspec],
 &error_data)) {
+             regex_format_error(&error_data,
 regex_error_format_buffer,
+                             sizeof(regex_error_format_buffer));
              COMPAT_LOG(SELINUX_ERROR,
                         "%s:  line %u has invalid regex %s:  %s\n",
                         path, lineno, regex,
-                        (errbuf ? errbuf : "out of memory"));
+                        regex_error_format_buffer);

compile_regex() may fail on an out of memory condition before regex_error_format_buffer is initialized, which is why we previously passed errbuf ?: "out of memory" above. I suppose you could initialize regex_error_format_buffer with "out of memory" prior to calling compile_regex().

Quoted 5 lines
libselinux/src/regex.c new file +0 -0
new file mode 100644
index 0000000..1c4a84d

<snip>

Quoted 1 line

+int regex_writef(struct regex_data *regex, FILE *fp)

This needs to be updated with the new do_write_precompregex argument, and either use the argument or mark it unused to permit compilation for the USE_PCRE2=n.

Quoted 6 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
_______________________________________________
Selinux 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
#5 lore raw
Quoted 48 lines · William Roberts
Quoted 4 lines · Janis Danisevskis

First of all, I would like to thank you, Stephen and William, for your patience and support.

On Thu, Sep 15, 2016 at 8:34 PM William Roberts <

[email protected]>

Quoted 38 lines
Quoted 28 lines · wrote:
Quoted 23 lines · Stephen Smalley
Quoted 13 lines · Janis Danisevskis

From: Janis Danisevskis <[email protected]>

This patch moves all pcre1/2 dependencies into the new files regex.h and regex.c implementing the common denominator of features needed by libselinux. The compiler flag -DUSE_PCRE2 toggles between the used implementations.

As of this patch libselinux supports either pcre or pcre2 but not both at the same time. The persistently stored file contexts information differs. This means libselinux can only load file context files generated by sefcontext_compile build with the same pcre variant.

Quoted 8 lines

Also, for pcre2 the persistent format is architecture dependent. Stored precompiled regular expressions can only be used on the same architecture they were generated on. If pcre2 is used, sefcontext_compile now respects the "-r". This flag makes sefcontext_compile include the precompiled regular expressions in the output file. The default is to omit them, so that the output remains portable at the cost of having to recompile the regular expressions at load time, or rather on first use.

Quoted 1 line

Is that really the default behavior you want?

Quoted 7 lines

I would make the default include the pre-compiled regex's, if there is an arch mismatch then ditch the pre-compiled ones and reload them. I would make it an option to NOT generate pre-compiled regexes. For the upstream consumers, who compile and deploy on the same system, this means they don't need to update to add -r. For android, we just need to update a makefile to include -r to omit the precompiled regex's.

Quoted 3 lines

The arch is currently not encoded in the binary, so a mismatch cannot be detected. I would sleep better if we left the -r option as is for now, because it is kind of "use only if you know what you are doing". Also,

Quoted 9 lines

this

Quoted 1 line

way it does not break the android build process.

We probably would want to encode the architecture since this matters.This way
we can detect a mismatch and compile them. This,prevents the why isn't my device booting issue for cross compilations that don't have -r.

That's just the thing. Without -r the phone _will_ boot because the regexes are compiled on first use. With -r and an arch mismatch we have an undefined behavior, which is bad.

See, I don't currently know what part of the architecture is important. Will word size and endianess be enough, e.g., will regexes generated on x86_64el work on armv8el (apparently this is what I observed but it could change at the whim of the PCRE2 maintainers)? So what will be the encoding? If we encode the complete architecture then the typical case for android (build on x86_64 for armv7/armv8) will yield the same result as without the -r option, namely, we rebuild the regexes on the device. If we just encode word size and endianess it may work for a while... I just don't feel comfortable enough to make a decision at this point.

My limited understanding is that the automata built by PCRE2 use pointers to link states or other structures. These pointers are symbolized when serialized, but they keep their width, which is why the regexes are at least sensitive to the word size.

Just a wild Idea:
PCRE2 also supports JIT compiling. I did not use this here because I did not feel comfortable for libselinux to depend on the execmem permission. But this could be developed into pre cross compiling the regexes into native code, which can than be linked into a device specific library. But I guess this would require quite some cooperation with the PCRE2 developers.

Quoted 6 lines

The other thing is, this is only an issue on binary formated file_context files,
this is the ideal reason to move back to text files, since their is no speedup
gained if we have to compile them.

Right, or you could see it as an intermediate solution that does not require changes to the build system until we can properly cross compile the regexes.

Quoted 113 lines
Quoted 13 lines
Quoted 11 lines

In the case of Android, where we cross compile the file_contexts.bin, does using binary fc's even make sense now? The major reason is that the binary format loads faster by skipping the regex compilation, we can no longer skip that with PCRE2 and cross compilation. We should likely move Android back to text based if this is the case. I have speedups in the neighborhood of 30% for text file processing in the pipeline.

Quoted 3 lines

I realize that the binary PCRE2 variant is currently much slower than the PCRE stored regexes. But it still a little faster than the text file processing. If you can make text file processing 30% faster, than this

may

Quoted 1 line

well be an option because serializing pcre2 regexes will probably never

beat

Quoted 3 lines

pcre, even if we did stunts like compiling for the correct architecture using qemu.
I'd be happy to help perform benchmarks and choose the best option.

Once you dump the pre-compiled regex's from the binary format, your back to the text files performance. Also, with PCRE, the binary format was
3x the size on disk.

Quoted 2 lines

Also I am exploring another option, which may sound a bit exotic. So I

would

Quoted 1 line

like to have a running POC first before I come forward. Maybe nothing

comes

Quoted 9 lines

of it.

Quoted 7 lines
Quoted 7 lines

Review

Quoted 7 lines

---

(quoted diff) +0 -0

b/libselinux/src/label_file.h

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines

index 6d1e890..3df7972 100644

libselinux/src/label_file.h +4 -2
@@ -453,12 +429,14 @@ static inline int process_line(struct
 selabel_handle *rec,
       */
      data->nspec++;
 
-     if (rec->validating &&
-                         compile_regex(data, &spec_arr[nspec],
 &errbuf)) {
+     if (rec->validating
+                     && compile_regex(data, &spec_arr[nspec],
 &error_data)) {
+             regex_format_error(&error_data,
 regex_error_format_buffer,
+                             sizeof(regex_error_format_buffer));
              COMPAT_LOG(SELINUX_ERROR,
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

libselinux/src/label_file.h static inline int process_line(struct selabel_handle *rec, · line 439

Quoted 4 lines
libselinux/src/label_file.h +1 -1
                         "%s:  line %u has invalid regex %s:  %s\n",
                         path, lineno, regex,
-                        (errbuf ? errbuf : "out of memory"));
+                        regex_error_format_buffer);

compile_regex() may fail on an out of memory condition before regex_error_format_buffer is initialized, which is why we previously passed errbuf ?: "out of memory" above. I suppose you could

initialize

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

regex_error_format_buffer with "out of memory" prior to calling compile_regex().

Quoted 5 lines
libselinux/src/regex.c new file +0 -0
new file mode 100644
index 0000000..1c4a84d

<snip>

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 1 line

+int regex_writef(struct regex_data *regex, FILE *fp)

This needs to be updated with the new do_write_precompregex argument, and either use the argument or mark it unused to permit compilation

for

Quoted 14 lines
Quoted 14 lines
Quoted 8 lines

the USE_PCRE2=n.

_______________________________________________
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,

Quoted 7 lines
Quoted 7 lines

William C Roberts
_______________________________________________
Selinux 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

#6 lore raw

<snip>

Quoted 5 lines

That's just the thing. Without -r the phone _will_ boot because the regexes are compiled on first use. With -r and an arch mismatch we have an undefined behavior, which is bad.

That's just a limitation of the current design.

Quoted 10 lines

See, I don't currently know what part of the architecture is important. Will word size and endianess be enough, e.g., will regexes generated on x86_64el work on armv8el (apparently this is what I observed but it could change at the whim of the PCRE2 maintainers)? So what will be the encoding? If we encode the complete architecture then the typical case for android (build on x86_64 for armv7/armv8) will yield the same result as without the -r option, namely, we rebuild the regexes on the device. If we just encode word size and endianess it may work for a while... I just don't feel comfortable enough to make a decision at this point.

Then shelve it and use textual file contexts. The only purpose of the binary format is to
include the pre-compiled regex;s so if you cant use that feature, there's no point in
using binary format.

My thought would be that it has to be an exact match for architecture upfront, then
possibly investigate relaxing the requirement later. But, IIRC, if we get a 30% increase in text file load time, theirs really no point, for the binary format on Android.
Android fic files are smaller, and have much simpler regex's compared to our desktop
brethren.

Quoted 6 lines

My limited understanding is that the automata built by PCRE2 use pointers to link states or other structures. These pointers are symbolized when serialized, but they keep their width, which is why the regexes are at least sensitive to the word size.

Quoted 6 lines

Just a wild Idea:
PCRE2 also supports JIT compiling. I did not use this here because I did not feel comfortable for libselinux to depend on the execmem permission. But this could be developed into pre cross compiling the regexes into native code, which can than be linked into a device specific library. But I guess this would require quite some cooperation with the PCRE2 developers.

I was going to ask if the arch dependency was on JIT'd code or just something else
which you elaborated above with word size/endiness, etc.

Quoted 11 lines
Quoted 7 lines

The other thing is, this is only an issue on binary formated file_context files,
this is the ideal reason to move back to text files, since their is no speedup
gained if we have to compile them.

Right, or you could see it as an intermediate solution that does not require changes to the build system until we can properly cross compile the regexes.

If you add an option to sefcontext_compile it should be to remove them. not add it in.
This keeps it consistent with teh behavior for PCRE, it would be add to say, "make me
a binary fc, but don't actually embed the regex information", since that is currently not
the default behavior. Changing the Android make recipe is trivial, so adding -r shouldn't
for Android shouldn't be a show stopper.

<snip>

#7 lore raw

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Quoted 91 lines · William Roberts

<snip>

Quoted 3 lines

That's just the thing. Without -r the phone _will_ boot because the

regexes

Quoted 1 line

are compiled on first use. With -r and an arch mismatch we have an

undefined

Quoted 1 line

behavior, which is bad.

That's just a limitation of the current design.

Quoted 2 lines

See, I don't currently know what part of the architecture is important.

Will

Quoted 1 line

word size and endianess be enough, e.g., will regexes generated on

x86_64el

Quoted 1 line

work on armv8el (apparently this is what I observed but it could change

at

Quoted 2 lines

the whim of the PCRE2 maintainers)? So what will be the encoding? If we encode the complete architecture then the typical case for android

(build on

Quoted 1 line

x86_64 for armv7/armv8) will yield the same result as without the -r

option,

Quoted 3 lines

namely, we rebuild the regexes on the device. If we just encode word size and endianess it may work for a while... I just don't feel comfortable enough to make a decision at this point.

Then shelve it and use textual file contexts. The only purpose of the binary format is to
include the pre-compiled regex;s so if you cant use that feature, there's no point in
using binary format.

My thought would be that it has to be an exact match for architecture upfront, then
possibly investigate relaxing the requirement later. But, IIRC, if we get a 30%
increase in text file load time, theirs really no point, for the binary format on Android.
Android fic files are smaller, and have much simpler regex's compared to our desktop
brethren.

Quoted 2 lines

My limited understanding is that the automata built by PCRE2 use

pointers to

Quoted 2 lines

link states or other structures. These pointers are symbolized when serialized, but they keep their width, which is why the regexes are at

least

Quoted 4 lines

sensitive to the word size.

Just a wild Idea:
PCRE2 also supports JIT compiling. I did not use this here because I did

not

Quoted 3 lines

feel comfortable for libselinux to depend on the execmem permission. But this could be developed into pre cross compiling the regexes into native code, which can than be linked into a device specific library. But I

guess

Quoted 1 line

this would require quite some cooperation with the PCRE2 developers.

I was going to ask if the arch dependency was on JIT'd code or just something else
which you elaborated above with word size/endiness, etc.

Quoted 3 lines
Quoted 3 lines

The other thing is, this is only an issue on binary formated

file_context

Quoted 7 lines
Quoted 4 lines

files,
this is the ideal reason to move back to text files, since their is no speedup
gained if we have to compile them.

Right, or you could see it as an intermediate solution that does not

require

Quoted 1 line

changes to the build system until we can properly cross compile the

regexes.

If you add an option to sefcontext_compile it should be to remove them. not add it in.
This keeps it consistent with teh behavior for PCRE, it would be add to say, "make me
a binary fc, but don't actually embed the regex information", since that is currently not
the default behavior. Changing the Android make recipe is trivial, so adding -r shouldn't
for Android shouldn't be a show stopper.

<snip>

#8 lore raw
Quoted 2 lines · Janis Danisevskis

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Quoted 95 lines
Quoted 92 lines · William Roberts

<snip>

Quoted 7 lines

That's just the thing. Without -r the phone _will_ boot because the regexes
are compiled on first use. With -r and an arch mismatch we have an undefined
behavior, which is bad.

That's just a limitation of the current design.

Quoted 16 lines

See, I don't currently know what part of the architecture is important. Will
word size and endianess be enough, e.g., will regexes generated on x86_64el
work on armv8el (apparently this is what I observed but it could change at
the whim of the PCRE2 maintainers)? So what will be the encoding? If we encode the complete architecture then the typical case for android (build on
x86_64 for armv7/armv8) will yield the same result as without the -r option,
namely, we rebuild the regexes on the device. If we just encode word size
and endianess it may work for a while... I just don't feel comfortable enough to make a decision at this point.

Then shelve it and use textual file contexts. The only purpose of the binary format is to
include the pre-compiled regex;s so if you cant use that feature, there's no point in
using binary format.

My thought would be that it has to be an exact match for architecture upfront, then
possibly investigate relaxing the requirement later. But, IIRC, if we get a 30%
increase in text file load time, theirs really no point, for the binary format on Android.
Android fic files are smaller, and have much simpler regex's compared to our desktop
brethren.

Quoted 8 lines

My limited understanding is that the automata built by PCRE2 use pointers to
link states or other structures. These pointers are symbolized when serialized, but they keep their width, which is why the regexes are at least
sensitive to the word size.

Quoted 8 lines

Just a wild Idea:
PCRE2 also supports JIT compiling. I did not use this here because I did not
feel comfortable for libselinux to depend on the execmem permission. But this could be developed into pre cross compiling the regexes into native code, which can than be linked into a device specific library. But I guess
this would require quite some cooperation with the PCRE2 developers.

I was going to ask if the arch dependency was on JIT'd code or just something else
which you elaborated above with word size/endiness, etc.

Quoted 10 lines
Quoted 8 lines

The other thing is, this is only an issue on binary formated file_context
files,
this is the ideal reason to move back to text files, since their is no speedup
gained if we have to compile them.

Quoted 4 lines

Right, or you could see it as an intermediate solution that does not require
changes to the build system until we can properly cross compile the regexes.

If you add an option to sefcontext_compile it should be to remove them. not add it in.
This keeps it consistent with teh behavior for PCRE, it would be add to say, "make me
a binary fc, but don't actually embed the regex information", since that is currently not
the default behavior. Changing the Android make recipe is trivial, so adding -r shouldn't
for Android shouldn't be a show stopper.

<snip>

Signature · 3 lines
Respectfully,

William C Roberts
#9 lore raw
Quoted 9 lines · William Roberts
Quoted 2 lines · Janis Danisevskis

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Its the very latest thing in master yeah, but I do also agree with changing it.

I just wanted to add that from a distro perspective, compiling things by default makes more sense. In gentoo, the package post_install runs sefcontext_compile. Using the fcontext files happens a lot more than any updates to libselinux (and thus potential format changes) so I'm pretty sure most people would prefer to have the speedup.

Gentoo does it on the machine itself, I am not sure about redhat or debian but I wouldnt be surprised if they do it per-arch at the very least so cross-arch probably isnt an issue.

Also, I think we should add the arch to the version string stored. I would rather have false negatives than positives especially since we are not 100% sure exactly what part of the arch is important. We can always loosen it up later if that gets locked down.

Isnt the selinux policy in android stored in the rootfs or /system? isnt that all read-only? how would re-compiling on the device work? Does android put the fcontext.bin files on /data? that seems insecure.

-- Jason

Quoted 106 lines
Quoted 94 lines
Quoted 92 lines · William Roberts

<snip>

Quoted 7 lines

That's just the thing. Without -r the phone _will_ boot because the regexes
are compiled on first use. With -r and an arch mismatch we have an undefined
behavior, which is bad.

That's just a limitation of the current design.

Quoted 16 lines

See, I don't currently know what part of the architecture is important. Will
word size and endianess be enough, e.g., will regexes generated on x86_64el
work on armv8el (apparently this is what I observed but it could change at
the whim of the PCRE2 maintainers)? So what will be the encoding? If we encode the complete architecture then the typical case for android (build on
x86_64 for armv7/armv8) will yield the same result as without the -r option,
namely, we rebuild the regexes on the device. If we just encode word size
and endianess it may work for a while... I just don't feel comfortable enough to make a decision at this point.

Then shelve it and use textual file contexts. The only purpose of the binary format is to
include the pre-compiled regex;s so if you cant use that feature, there's no point in
using binary format.

My thought would be that it has to be an exact match for architecture upfront, then
possibly investigate relaxing the requirement later. But, IIRC, if we get a 30%
increase in text file load time, theirs really no point, for the binary format on Android.
Android fic files are smaller, and have much simpler regex's compared to our desktop
brethren.

Quoted 8 lines

My limited understanding is that the automata built by PCRE2 use pointers to
link states or other structures. These pointers are symbolized when serialized, but they keep their width, which is why the regexes are at least
sensitive to the word size.

Quoted 8 lines

Just a wild Idea:
PCRE2 also supports JIT compiling. I did not use this here because I did not
feel comfortable for libselinux to depend on the execmem permission. But this could be developed into pre cross compiling the regexes into native code, which can than be linked into a device specific library. But I guess
this would require quite some cooperation with the PCRE2 developers.

I was going to ask if the arch dependency was on JIT'd code or just something else
which you elaborated above with word size/endiness, etc.

Quoted 10 lines
Quoted 8 lines

The other thing is, this is only an issue on binary formated file_context
files,
this is the ideal reason to move back to text files, since their is no speedup
gained if we have to compile them.

Quoted 4 lines

Right, or you could see it as an intermediate solution that does not require
changes to the build system until we can properly cross compile the regexes.

If you add an option to sefcontext_compile it should be to remove them. not add it in.
This keeps it consistent with teh behavior for PCRE, it would be add to say, "make me
a binary fc, but don't actually embed the regex information", since that is currently not
the default behavior. Changing the Android make recipe is trivial, so adding -r shouldn't
for Android shouldn't be a show stopper.

<snip>

Signature · 8 lines
Respectfully,

William C Roberts
_______________________________________________
Seandroid-list mailing list
[email protected]
To unsubscribe, send email to [email protected].
To get help, send an email containing "help" to [email protected].
#10 lore raw
Quoted 5 lines · Jason Zaman
Quoted 4 lines · William Roberts
Quoted 2 lines · Janis Danisevskis

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Quoted 7 lines
Quoted 5 lines

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Its the very latest thing in master yeah, but I do also agree with changing it.

I'd prefer it changed myself. Another argument pro-change is that one doesn't know if its a PCRE2 or PCRE1 compatable version of the libraries, we should probably have an ability to intergoate that via API so we can print it out in help
dialogue, that this tool so we at least know what it can support.

The biggest thing that needs to get fixed with this, is that no matter if it contains the
pre-compiled regexs or not, it should always load and work on Android. In distros,
it will fall back to file_contexts, but we don't have this in Android. This ties into the arch
version information below. But if the arch differs, always recompile. The alternative to
this, is just go back to textual fc file son Android, since we won't be using any of the
features of binary fc's. Better yet, in the Android build, I would check to see if host arch
is the same as target arch, or let an OEM set a flag, to do the compilation. But I would
consider those stretch goals, and just revert android back to textual files.

Quoted 6 lines

I just wanted to add that from a distro perspective, compiling things by default makes more sense. In gentoo, the package post_install runs sefcontext_compile. Using the fcontext files happens a lot more than any updates to libselinux (and thus potential format changes) so I'm pretty sure most people would prefer to have the speedup.

For the distros it will make sense most of the time from what I can tell.

Quoted 9 lines

Gentoo does it on the machine itself, I am not sure about redhat or debian but I wouldnt be surprised if they do it per-arch at the very least so cross-arch probably isnt an issue.

Also, I think we should add the arch to the version string stored. I would rather have false negatives than positives especially since we are not 100% sure exactly what part of the arch is important. We can always loosen it up later if that gets locked down.

No don't append it onto the PCRE version string, just check the PCRE version string
and if its greater than X read out the arch field next. This should work like policy versioning
in libsepol.

Quoted 4 lines

Isnt the selinux policy in android stored in the rootfs or /system? isnt that all read-only? how would re-compiling on the device work? Does android put the fcontext.bin files on /data? that seems insecure.

This is the cross compile aspect, we build on the host and shove it into the device
rootfs at build time. We typically build on x86_64 and deploy on ARM. But this is NOT
always the case, just the most common. For instance, with Intel tablets it probably can
use the pre-compiled regexes.

Quoted 109 lines

-- Jason

Quoted 106 lines
Quoted 93 lines
Quoted 91 lines · William Roberts

<snip>

Quoted 7 lines

That's just the thing. Without -r the phone _will_ boot because the regexes
are compiled on first use. With -r and an arch mismatch we have an undefined
behavior, which is bad.

That's just a limitation of the current design.

Quoted 16 lines

See, I don't currently know what part of the architecture is important. Will
word size and endianess be enough, e.g., will regexes generated on x86_64el
work on armv8el (apparently this is what I observed but it could change at
the whim of the PCRE2 maintainers)? So what will be the encoding? If we encode the complete architecture then the typical case for android (build on
x86_64 for armv7/armv8) will yield the same result as without the -r option,
namely, we rebuild the regexes on the device. If we just encode word size
and endianess it may work for a while... I just don't feel comfortable enough to make a decision at this point.

Then shelve it and use textual file contexts. The only purpose of the binary format is to
include the pre-compiled regex;s so if you cant use that feature, there's no point in
using binary format.

My thought would be that it has to be an exact match for architecture upfront, then
possibly investigate relaxing the requirement later. But, IIRC, if we get a 30%
increase in text file load time, theirs really no point, for the binary format on Android.
Android fic files are smaller, and have much simpler regex's compared to our desktop
brethren.

Quoted 8 lines

My limited understanding is that the automata built by PCRE2 use pointers to
link states or other structures. These pointers are symbolized when serialized, but they keep their width, which is why the regexes are at least
sensitive to the word size.

Quoted 8 lines

Just a wild Idea:
PCRE2 also supports JIT compiling. I did not use this here because I did not
feel comfortable for libselinux to depend on the execmem permission. But this could be developed into pre cross compiling the regexes into native code, which can than be linked into a device specific library. But I guess
this would require quite some cooperation with the PCRE2 developers.

I was going to ask if the arch dependency was on JIT'd code or just something else
which you elaborated above with word size/endiness, etc.

Quoted 10 lines
Quoted 8 lines

The other thing is, this is only an issue on binary formated file_context
files,
this is the ideal reason to move back to text files, since their is no speedup
gained if we have to compile them.

Quoted 4 lines

Right, or you could see it as an intermediate solution that does not require
changes to the build system until we can properly cross compile the regexes.

If you add an option to sefcontext_compile it should be to remove them. not add it in.
This keeps it consistent with teh behavior for PCRE, it would be add to say, "make me
a binary fc, but don't actually embed the regex information", since that is currently not
the default behavior. Changing the Android make recipe is trivial, so adding -r shouldn't
for Android shouldn't be a show stopper.

Quoted 1 line
Quoted 1 line

<snip>

--
Respectfully,

William C Roberts
_______________________________________________
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
#11 lore raw
Quoted 20 lines · William Roberts
Quoted 5 lines · Jason Zaman
Quoted 4 lines · William Roberts
Quoted 2 lines · Janis Danisevskis

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Quoted 7 lines
Quoted 5 lines

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Its the very latest thing in master yeah, but I do also agree with changing it.

I'd prefer it changed myself. Another argument pro-change is that one doesn't know if its a PCRE2 or PCRE1 compatable version of the libraries, we should probably have an ability to intergoate that via API so we can print it out in help
dialogue, that this tool so we at least know what it can support.

Quoted 15 lines

The biggest thing that needs to get fixed with this, is that no matter if it contains the
pre-compiled regexs or not, it should always load and work on Android. In distros,
it will fall back to file_contexts, but we don't have this in Android. This ties into the arch
version information below. But if the arch differs, always recompile. The alternative to
this, is just go back to textual fc file son Android, since we won't be using any of the
features of binary fc's. Better yet, in the Android build, I would check to see if host arch
is the same as target arch, or let an OEM set a flag, to do the compilation. But I would
consider those stretch goals, and just revert android back to textual files.

My ramblings might not be clear here. If we just version it with arch and require
an exact match, we don't need -r, so that option goes away.

<snip>

#12 lore raw
Quoted 38 lines · William Roberts
Quoted 20 lines · William Roberts
Quoted 5 lines · Jason Zaman
Quoted 4 lines · William Roberts
Quoted 2 lines · Janis Danisevskis

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Quoted 7 lines
Quoted 5 lines

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Its the very latest thing in master yeah, but I do also agree with changing it.

I'd prefer it changed myself. Another argument pro-change is that one doesn't know if its a PCRE2 or PCRE1 compatable version of the libraries, we should probably have an ability to intergoate that via API so we can print it out in help
dialogue, that this tool so we at least know what it can support.

Quoted 15 lines

The biggest thing that needs to get fixed with this, is that no matter if it contains the
pre-compiled regexs or not, it should always load and work on Android. In distros,
it will fall back to file_contexts, but we don't have this in Android. This ties into the arch
version information below. But if the arch differs, always recompile. The alternative to
this, is just go back to textual fc file son Android, since we won't be using any of the
features of binary fc's. Better yet, in the Android build, I would check to see if host arch
is the same as target arch, or let an OEM set a flag, to do the compilation. But I would
consider those stretch goals, and just revert android back to textual files.

Quoted 3 lines

My ramblings might not be clear here. If we just version it with arch and require
an exact match, we don't need -r, so that option goes away.

This is what I was aiming for too, currently the pcre_version() is just strcmp'd so changing it to cat(pcre2_version(), arch_string()) should avoid any problems. It may turn out to be overzealous but it will always be safe. I dont think splitting pcre_version and arch into separate fields makes a huge difference if you prefer that. Currently the version numbers are not parsed tho, so checking arch only for pcre2 seems like a lot more work.

-- Jason

#13 lore raw
Quoted 5 lines · Jason Zaman
Quoted 3 lines · William Roberts
Quoted 1 line · William Roberts

On Fri, Sep 16, 2016 at 6:31 AM, Jason Zaman <[email protected]>

Quoted 2 lines · wrote:
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines · William Roberts
Quoted 1 line

On Fri, Sep 16, 2016 at 6:09 AM, Janis Danisevskis <

Quoted 1 line · [email protected]> wrote:
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

I don't mind. Then before sefcontext_compile -r gets widely

adapted we

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 7 lines
Quoted 1 line

should change the semantic quickly. I'll prepare a patch.

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Its the very latest thing in master yeah, but I do also agree with

changing it.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

I'd prefer it changed myself. Another argument pro-change is that one

doesn't

Quoted 1 line
Quoted 1 line
Quoted 1 line

know if its a PCRE2 or PCRE1 compatable version of the libraries, we

should

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

probably have an ability to intergoate that via API so we can print it out in help
dialogue, that this tool so we at least know what it can support.

Quoted 15 lines
Quoted 7 lines
Quoted 7 lines

The biggest thing that needs to get fixed with this, is that no matter if it contains the
pre-compiled regexs or not, it should always load and work on Android. In distros,
it will fall back to file_contexts, but we don't have this in Android. This ties into the arch
version information below. But if the arch differs, always recompile.

Quoted 8 lines
Quoted 8 lines

The alternative to
this, is just go back to textual fc file son Android, since we won't be using any of the
features of binary fc's. Better yet, in the Android build, I would check to see if host arch
is the same as target arch, or let an OEM set a flag, to do the compilation. But I would
consider those stretch goals, and just revert android back to textual

files.

Quoted 10 lines
Quoted 4 lines

My ramblings might not be clear here. If we just version it with arch and require
an exact match, we don't need -r, so that option goes away.

This is what I was aiming for too, currently the pcre_version() is just strcmp'd so changing it to cat(pcre2_version(), arch_string()) should avoid any problems. It may turn out to be overzealous but it will always be safe. I dont think splitting pcre_version and arch into separate fields makes a huge difference if you prefer that. Currently the version

It can make a difference if we ever need to parse any information for some reason in the future. We'd then have to split them back apart. This is just a bit more future proof IMO.

Quoted 4 lines

numbers are not parsed tho, so checking arch only for pcre2 seems like a lot more work.

-- Jason

#14 lore raw
In reply to Jason Zaman
Quoted 21 lines · Jason Zaman
Quoted 1 line · William Roberts

On Fri, Sep 16, 2016 at 6:09 AM, Janis Danisevskis <[email protected]>

Quoted 8 lines · wrote:
Quoted 2 lines

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Its the very latest thing in master yeah, but I do also agree with changing it.

I just wanted to add that from a distro perspective, compiling things by default makes more sense. In gentoo, the package post_install runs sefcontext_compile. Using the fcontext files happens a lot more than any updates to libselinux (and thus potential format changes) so I'm pretty sure most people would prefer to have the speedup.

Quoted 9 lines

Gentoo does it on the machine itself, I am not sure about redhat or debian but I wouldnt be surprised if they do it per-arch at the very least so cross-arch probably isnt an issue.

Also, I think we should add the arch to the version string stored. I would rather have false negatives than positives especially since we are not 100% sure exactly what part of the arch is important. We can always loosen it up later if that gets locked down.

Good, I will add an architecture string.

Quoted 1 line

From the PCRE2 documentation: [1] "The host on which the patterns are

reloaded must be running the same version of PCRE2, with the same code unit width, and must also have the same endianness, pointer width and PCRE2_SIZE type."
I will encode these three properties in the arch string.

[1] http://www.pcre.org/current/doc/html/pcre2serialize.html

Quoted 4 lines

Isnt the selinux policy in android stored in the rootfs or /system? isnt that all read-only? how would re-compiling on the device work? Does android put the fcontext.bin files on /data? that seems insecure.

On android file_context.bin is in / and read only. If the patterns are compiled on the device, they are not stored persistently. AFAIK this happens only once upon every boot.

Quoted 122 lines

-- Jason

Quoted 1 line
Quoted 1 line

On Fri, Sep 16, 2016 at 1:35 PM William Roberts <

[email protected]>

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines · wrote:

<snip>

Quoted 7 lines

That's just the thing. Without -r the phone _will_ boot because the regexes
are compiled on first use. With -r and an arch mismatch we have an undefined
behavior, which is bad.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

That's just a limitation of the current design.

Quoted 2 lines

See, I don't currently know what part of the architecture is

important.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

Will
word size and endianess be enough, e.g., will regexes generated on x86_64el
work on armv8el (apparently this is what I observed but it could

change

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

at
the whim of the PCRE2 maintainers)? So what will be the encoding?

If we

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

encode the complete architecture then the typical case for android (build on
x86_64 for armv7/armv8) will yield the same result as without the -r option,
namely, we rebuild the regexes on the device. If we just encode word size
and endianess it may work for a while... I just don't feel

comfortable

Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 1 line

enough to make a decision at this point.

Then shelve it and use textual file contexts. The only purpose of the binary format is to
include the pre-compiled regex;s so if you cant use that feature, there's no point in
using binary format.

My thought would be that it has to be an exact match for architecture upfront, then
possibly investigate relaxing the requirement later. But, IIRC, if we

get

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

a 30%
increase in text file load time, theirs really no point, for the binary format on Android.
Android fic files are smaller, and have much simpler regex's compared to our desktop
brethren.

Quoted 1 line
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

My limited understanding is that the automata built by PCRE2 use pointers to
link states or other structures. These pointers are symbolized when serialized, but they keep their width, which is why the regexes are

at

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

least
sensitive to the word size.

Just a wild Idea:
PCRE2 also supports JIT compiling. I did not use this here because

I did

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

not
feel comfortable for libselinux to depend on the execmem

permission. But

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

this could be developed into pre cross compiling the regexes into

native

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 3 lines

code, which can than be linked into a device specific library. But I guess
this would require quite some cooperation with the PCRE2 developers.

I was going to ask if the arch dependency was on JIT'd code or just something else
which you elaborated above with word size/endiness, etc.

Quoted 2 lines
Quoted 2 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

The other thing is, this is only an issue on binary formated file_context
files,
this is the ideal reason to move back to text files, since their

is no

Quoted 26 lines
Quoted 19 lines
Quoted 19 lines
Quoted 8 lines
Quoted 2 lines

speedup
gained if we have to compile them.

Right, or you could see it as an intermediate solution that does not require
changes to the build system until we can properly cross compile the regexes.

If you add an option to sefcontext_compile it should be to remove them. not add it in.
This keeps it consistent with teh behavior for PCRE, it would be add to say, "make me
a binary fc, but don't actually embed the regex information", since that is currently not
the default behavior. Changing the Android make recipe is trivial, so adding -r shouldn't
for Android shouldn't be a show stopper.

Quoted 1 line
Quoted 1 line

<snip>

--
Respectfully,

Quoted 6 lines

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

[email protected].

#15 lore raw
In reply to Jason Zaman
Quoted 19 lines · Jason Zaman
Quoted 9 lines · William Roberts
Quoted 2 lines · Janis Danisevskis

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Its the very latest thing in master yeah, but I do also agree with changing it.

I just wanted to add that from a distro perspective, compiling things by default makes more sense. In gentoo, the package post_install runs sefcontext_compile. Using the fcontext files happens a lot more than any updates to libselinux (and thus potential format changes) so I'm pretty sure most people would prefer to have the speedup.

Quoted 3 lines

Gentoo does it on the machine itself, I am not sure about redhat or debian but I wouldnt be surprised if they do it per-arch at the very least so cross-arch probably isnt an issue.

In Red Hat, SELinux policy is noarch, and they switched to precompiling both policy and file_contexts.bin at build time to minimize the cost at package install time. Otherwise, in small VMs, they had issues with running out of memory during semodule -B. So file_contexts.bin presently has to be arch-independent, or we need the arch properties detection logic and fallback. That said, none of this matters unless you build with USE_PCRE2=y, and no one outside of Android is doing that today.

Quoted 4 lines

Also, I think we should add the arch to the version string stored. I would rather have false negatives than positives especially since we are not 100% sure exactly what part of the arch is important. We can always loosen it up later if that gets locked down.

We don't want the arch string itself, because that would invalidate use of file_contexts.bin entirely on typical Android use cases (build on x86_64, install to ARM), but only the relevant properties. And for Android, that is fatal - there is no file_contexts text file on which to fallback anymore. They only ship file_contexts.bin.

#16 lore raw

I have started implementing an arch string patch. Unfortunately, i did not manage to finish it before I had to leave the office today. In essence I did this:
The regex_arch_string has three components: the pointer width determined by sizeof(void*), PCRE2_SIZE width determined by sizeof(), and endianess determined by __BYTE_ORDER__==__ORDER_BIG/LITTEL_ENDIAN__

For example, the resulting string for x86_64 and aarch64el should look like
this: "8-8-el".

I bumped the compiled context version number and added the string right after the version in the output.
Comments?

Quoted 47 lines · Stephen Smalley
Quoted 2 lines · Jason Zaman
Quoted 1 line · William Roberts

On Fri, Sep 16, 2016 at 6:09 AM, Janis Danisevskis <[email protected]>

Quoted 10 lines · wrote:
Quoted 8 lines
Quoted 2 lines

I don't mind. Then before sefcontext_compile -r gets widely adapted we should change the semantic quickly. I'll prepare a patch.

Did I miss something and this was merged? Iv'e been out recovering from a surgery so I haven't been
following this as well as I normally would have,

If its merged, just leave it.

Its the very latest thing in master yeah, but I do also agree with

changing it.

Quoted 10 lines

I just wanted to add that from a distro perspective, compiling things by default makes more sense. In gentoo, the package post_install runs sefcontext_compile. Using the fcontext files happens a lot more than any updates to libselinux (and thus potential format changes) so I'm pretty sure most people would prefer to have the speedup.

Gentoo does it on the machine itself, I am not sure about redhat or debian but I wouldnt be surprised if they do it per-arch at the very least so cross-arch probably isnt an issue.

In Red Hat, SELinux policy is noarch, and they switched to precompiling both policy and file_contexts.bin at build time to minimize the cost at package install time. Otherwise, in small VMs, they had issues with running out of memory during semodule -B. So file_contexts.bin presently has to be arch-independent, or we need the arch properties detection logic and fallback. That said, none of this matters unless you build with USE_PCRE2=y, and no one outside of Android is doing that today.

Quoted 4 lines

Also, I think we should add the arch to the version string stored. I would rather have false negatives than positives especially since we are not 100% sure exactly what part of the arch is important. We can always loosen it up later if that gets locked down.

We don't want the arch string itself, because that would invalidate use of file_contexts.bin entirely on typical Android use cases (build on x86_64, install to ARM), but only the relevant properties. And for Android, that is fatal - there is no file_contexts text file on which to fallback anymore. They only ship file_contexts.bin.

#17 lore raw
Quoted 8 lines · Janis Danisevskis

I have started implementing an arch string patch. Unfortunately, i did not manage to finish it before I had to leave the office today. In essence I did this:
The regex_arch_string has three components: the pointer width determined by sizeof(void*), PCRE2_SIZE width determined by sizeof(), and endianess determined by __BYTE_ORDER__==__ORDER_BIG/LITTEL_ENDIAN__

Quoted 6 lines

For example, the resulting string for x86_64 and aarch64el should look like
this: "8-8-el".

I bumped the compiled context version number and added the string right after the version in the output.
Comments?

What's the error handling when the versions do not match? Just ignore the compiled regexes in file_contexts.bin?

#18 lore raw
Quoted 17 lines · Stephen Smalley
Quoted 9 lines · Janis Danisevskis

I have started implementing an arch string patch. Unfortunately, i did not manage to finish it before I had to leave the office today. In essence I did this:
The regex_arch_string has three components: the pointer width determined by sizeof(void*), PCRE2_SIZE width determined by sizeof(), and endianess determined by __BYTE_ORDER__==__ORDER_BIG/LITTEL_ENDIAN__

For example, the resulting string for x86_64 and aarch64el should look

like

Quoted 5 lines

this: "8-8-el".

I bumped the compiled context version number and added the string right after the version in the output.
Comments?

Quoted 5 lines

What's the error handling when the versions do not match? Just ignore the compiled regexes in file_contexts.bin?

Yes, If the version and arch match attempt to load the compiled regexes

from the file. Otherwise, skip the corresponding regions of the file and leave the regex field NULL, so that they will be compiled on first use.