Skip to contentSkip to conversation

libselinux: add support for pcre2

PATCH In review Janis Danisevskis 2 participants 2 messages Sep 15, 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           |  92 ++-----
 libselinux/src/label_file.h           |  72 ++---
 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, 745 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 +20 -72

178 lines not on this page · raw message

libselinux/src/label_file.h +28 -44

135 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 22 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.

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

Thanks, applied, with the attached fix on top to allow building.

Quoted 1 line

From a9162c813adaadbdb632d1a71d7c6ffc3e43b1b0 Mon Sep 17 00:00:00 2001

From: Stephen Smalley <[email protected]>
Date: Thu, 15 Sep 2016 13:43:24 -0400
Subject: [PATCH] libselinux: regex_writef: Mark unused argument with
__attribute__((unused)).

---
 libselinux/src/regex.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
libselinux/src/regex.c +2 -1
index 646351b..750088e 100644
@@ -312,7 +312,8 @@ static inline pcre_extra *get_pcre_extra(struct regex_data *regex)
 	}
 }
 
-int regex_writef(struct regex_data *regex, FILE *fp, int unused)
+int regex_writef(struct regex_data *regex, FILE *fp,
+		 int unused __attribute__((unused)))
 {
 	int rc;
 	size_t len;
Signature · 1 line
2.7.4