Skip to contentSkip to conversation

FUZZING.md,*/fuzz: document selinux fuzzing

PATCH Superseded Stephen Smalley 1 participant 1 message Aug 27 SELinux security module

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v3 1/1 FUZZING.md,*/fuzz: document selinux fuzzing

Document the use of OSS-Fuzz and CIFuzz by the selinux userspace project and provide some instructions and scaffolding for debugging fuzzing failures.

---
v3 adds support for building the libsepol fuzzers the same way, makes use of the pre-existing VERBOSE symbol to control verbose logging, migrates the Makefile rules to new fuzz/Makefiles, only recompiles the files that change when built for fuzzing, and builds private copies of those files so that normal builds never accidentally pick up the fuzzer variants.

 FUZZING.md                                    | 84 +++++++++++++++++++
 checkpolicy/Makefile                          |  3 +
 checkpolicy/fuzz/.gitignore                   |  1 +
 checkpolicy/fuzz/Makefile                     | 40 +++++++++
 checkpolicy/fuzz/checkpolicy-fuzzer.c         | 53 ++++++++++++
 libselinux/Makefile                           |  3 +
 libselinux/fuzz/.gitignore                    |  2 +
 libselinux/fuzz/Makefile                      | 32 +++++++
 .../fuzz/selabel_file_compiled-fuzzer.c       | 47 +++++++++++
 libselinux/fuzz/selabel_file_text-fuzzer.c    | 81 ++++++++++++++++++
 libselinux/src/.gitignore                     |  2 +
 libselinux/src/Makefile                       | 16 +++-
 libsepol/Makefile                             |  3 +
 libsepol/fuzz/.gitignore                      |  3 +
 libsepol/fuzz/Makefile                        | 25 ++++++
 libsepol/fuzz/binpolicy-fuzzer.c              | 39 +++++++++
 libsepol/fuzz/module-package-fuzzer.c         | 39 +++++++++
 libsepol/fuzz/secilc-fuzzer.c                 | 45 ++++++++++
 libsepol/src/Makefile                         |  7 ++
 19 files changed, 524 insertions(+), 1 deletion(-)
 create mode 100644 FUZZING.md
 create mode 100644 checkpolicy/fuzz/.gitignore
 create mode 100644 checkpolicy/fuzz/Makefile
 create mode 100644 libselinux/fuzz/.gitignore
 create mode 100644 libselinux/fuzz/Makefile
 create mode 100644 libsepol/fuzz/.gitignore
 create mode 100644 libsepol/fuzz/Makefile
FUZZING.md new file +84 -0
new file mode 100644
index 00000000..cf454fd1
@@ -0,0 +1,84 @@
+Fuzzing of the SELinux userspace
+================================
+
+OSS-Fuzz
+--------
+
+Portions of the SELinux userspace are regularly fuzzed by OSS-Fuzz based
+on the configuration at:
+https://github.com/google/oss-fuzz/blob/master/projects/selinux/project.yaml
+
+The fuzzers can be found under the fuzz subdirectories of various
+directories within the selinux repository, and can be built using the
+./scripts/oss-fuzz.sh script. OSS-Fuzz itself uses this script to
+build the fuzzers.
+
+See
+https://issues.oss-fuzz.com/issues?q=project:selinux
+for public OSS-Fuzz selinux issues. OSS-Fuzz automatically makes
+issues public once they are verified to be fixed or after a 90 day
+disclosure window. Issues are initially created private and
+sent via email to those listed in the configuration above.
+
+Not all issues will be fixed by the upstream SELinux project, e.g.
+small memory leaks on error paths for checkpolicy are not viewed as
+worth fixing since checkpolicy will exit shortly thereafter, and OSS
+Fuzz will create issues if it encounters a timeout during fuzzing even
+though the fuzzer would have completed if given more time.
+
+Items categorized as a vulnerability by OSS-Fuzz will not always be
+treated as security issues by the upstream SELinux project; in
+particular, issues that depend on attacker-controlled policy are not
+considered to be security issues and will just be handled as regular
+bugs. See
+[SECURITY.md](https://github.com/SELinuxProject/selinux/blob/main/SECURITY.md)
+for more information on the SELinux userspace security vulnerability
+handling process.
+
+CIFuzz
+------
+
+The selinux GitHub CI Actions include a CIFuzz workflow based on the
+configuration at:
+https://github.com/SELinuxProject/selinux/blob/main/.github/workflows/cifuzz.yml
+
+This action is invoked for pushes and pull requests targeting the main
+branch of the SELinuxProject/selinux repository to help detect
+regressions on new commits.
+
+The status of CIFuzz workflow runs can be viewed at:
+https://github.com/SELinuxProject/selinux/actions/workflows/cifuzz.yml
+as well as in the check status for each push or pull request and
+displayed in the banner at the top of the
+[README.md](https://github.com/SELinuxProject/selinux/blob/main/README.md)
+file.
+
+Debugging Fuzzing Failures
+--------------------------
+
+OSS-Fuzz provides a detailed report and, when possible, a reproducer
+testcase. If the cause cannot be determined from the report, the
+reproducer testcase can be downloaded locally, the fuzzer can be built
+using ./scripts/oss-fuzz.sh, and the fuzzer can be manually run on the
+reproducer testcase input.
+
+The fuzzers are built without debugging information and with verbose
+logging disabled when built using oss-fuzz.sh. You can recompile them
+with debugging, logging, and a trivial main() function as follows:
+
+    make -C libsepol fuzz
+    make -C checkpolicy fuzz
+    make -C libselinux fuzz
+
+These build into the fuzz/ directories and do not touch any object
+files or archives used by the normal build.
+
+In some cases, it may be helpful to run one of the selinux userspace
+programs on the fuzzer input directly, potentially under gdb or
+valgrind, to better understand the issue and whether it is applicable
+to any production tool. This can be done when the fuzzer input is
+accepted directly as input to one of the selinux userspace programs,
+such as a CIL policy file to secilc or a binary policy file to
+checkpolicy -b. In other cases you may need to split the fuzzer input
+into its parts and only feed one portion to a program like checkpolicy
+or selabel_lookup.
checkpolicy/Makefile +3 -0
index e59ad63a..b5a32e41 100644
@@ -58,6 +58,9 @@ test: checkpolicy checkmodule
 # helper target for fuzzing
 checkobjects: $(CHECKOBJS)
 
+fuzz: all
+	$(MAKE) -C fuzz
+
 install: all
 	-mkdir -p $(DESTDIR)$(BINDIR)
 	-mkdir -p $(DESTDIR)$(MANDIR)/man8
checkpolicy/fuzz/.gitignore new file +1 -0
new file mode 100644
index 00000000..f9ba626f
@@ -0,0 +1 @@
+checkpolicy-fuzzer
checkpolicy/fuzz/Makefile new file +40 -0
new file mode 100644
index 00000000..0c066831
@@ -0,0 +1,40 @@
+# Build the checkpolicy fuzzer as a standalone debug binary.
+# oss-fuzz.sh handles the sanitizer/libFuzzer build; this Makefile
+# only produces a plain binary suitable for gdb/valgrind on a single
+# reproducer input.
+
+CHECKPOLICY := ..
+LIBSEPOLA ?= ../../libsepol/src/libsepol.a
+CFLAGS ?= -g -O0 -Wall
+override CFLAGS += -I$(CHECKPOLICY) -I../../libsepol/include -D_GNU_SOURCE \
+	-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -DDEBUG -DVERBOSE=1
+
+# Parser objects - same set as $(CHECKOBJS) in ../Makefile, built
+# locally with the fuzzing defines so module_compiler_reset() and the
+# lexer's non-exit error paths are compiled in.
+CHECKOBJS = y.tab.o lex.yy.o queue.o module_compiler.o parse_util.o \
+	    policy_define.o
+
+vpath %.c $(CHECKPOLICY)
+
+all: checkpolicy-fuzzer
+
+checkpolicy-fuzzer: checkpolicy-fuzzer.o $(CHECKOBJS) $(LIBSEPOLA)
+	$(CC) $(LDFLAGS) -o $@ $^
+
+# Ensure the generated parser/lexer sources exist in the parent dir.
+$(CHECKPOLICY)/y.tab.c $(CHECKPOLICY)/y.tab.h $(CHECKPOLICY)/lex.yy.c:
+	$(MAKE) -C $(CHECKPOLICY) $(@F)
+
+y.tab.o: $(CHECKPOLICY)/y.tab.c $(CHECKPOLICY)/y.tab.h
+	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
+
+lex.yy.o: $(CHECKPOLICY)/lex.yy.c $(CHECKPOLICY)/y.tab.h
+	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
+
+clean:
+	rm -f checkpolicy-fuzzer *.o
+
+.PHONY: all clean
+
+
checkpolicy/fuzz/checkpolicy-fuzzer.c +53 -0

87 lines not on this page · raw message

libselinux/Makefile +3 -0
index aeede2b5..2ba414d1 100644
@@ -55,6 +55,9 @@ all install relabel clean distclean:
 		(cd $$subdir && $(MAKE) $@) || exit 1; \
 	done
 
+fuzz: all
+	$(MAKE) -C fuzz
+
 swigify: all
 	$(MAKE) -C src $@
 
libselinux/fuzz/.gitignore new file +2 -0
new file mode 100644
index 00000000..d3bf28ca
@@ -0,0 +1,2 @@
+selabel_file_text-fuzzer
+selabel_file_compiled-fuzzer
libselinux/fuzz/Makefile new file +32 -0
new file mode 100644
index 00000000..9d291947
@@ -0,0 +1,32 @@
+# Build the libselinux fuzzers as standalone debug binaries.
+# oss-fuzz.sh handles the sanitizer/libFuzzer build; this Makefile
+# only produces plain binaries suitable for gdb/valgrind on a single
+# reproducer input.
+TARGETS := selabel_file_text-fuzzer selabel_file_compiled-fuzzer
+
+LIBSELINUXA := ../src/libselinux.a
+
+PCRE_CFLAGS ?= -DUSE_PCRE2 -DPCRE2_CODE_UNIT_WIDTH=8
+PCRE_LDLIBS ?= -lpcre2-8
+
+CFLAGS ?= -g -O0 -Wall
+override CFLAGS += -I../include -I../src -D_GNU_SOURCE -DNO_ANDROID_BACKEND \
+	$(PCRE_CFLAGS) -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -DDEBUG -DVERBOSE=1
+
+all: $(TARGETS)
+
+# Only label_file.o changes behavior under
+# FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION. Compile a private copy
+# of it here and link the rest from the normally-built ../src/libselinux.a.
+label_file.o: ../src/label_file.c
+	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
+
+$(TARGETS): %: %.o label_file.o $(LIBSELINUXA)
+	$(CC) $(LDFLAGS) -o $@ $^ $(PCRE_LDLIBS)
+
+clean:
+	rm -f $(TARGETS) *.o
+
+.PHONY: all clean
+
+
libselinux/fuzz/selabel_file_compiled-fuzzer.c +47 -0

76 lines not on this page · raw message

libselinux/fuzz/selabel_file_text-fuzzer.c +81 -0

116 lines not on this page · raw message

libselinux/src/.gitignore +2 -0
index 5413ccf0..5b7258dd 100644
@@ -3,3 +3,5 @@ selinuxswig_python_wrap.c
 selinuxswig_ruby_wrap.c
 selinux.egg-info/
 dist/
+selabel_file_text-fuzzer
+selabel_file_compiled-fuzzer
libselinux/src/Makefile +15 -1
index 9ecdbd4d..2e1bbd20 100644
@@ -181,6 +181,20 @@ selinuxswig_python_exception.i: exception.sh ../include/selinux/selinux.h
 %.lo:  %.c policy.h
 	$(CC) $(CPPFLAGS) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
 
+selabel_file_text-fuzzer.o: ../fuzz/selabel_file_text-fuzzer.c
+	$(CC) $(CPPFLAGS) $(CFLAGS) -g -I. -c -o $@ $^
+
+selabel_file_text-fuzzer: CFLAGS = -g -I../include -D_GNU_SOURCE -DNO_ANDROID_BACKEND $(PCRE_CFLAGS) -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -DDEBUG
+selabel_file_text-fuzzer: selabel_file_text-fuzzer.o $(LIBA)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(PCRE_LDLIBS)
+
+selabel_file_compiled-fuzzer.o: ../fuzz/selabel_file_compiled-fuzzer.c
+	$(CC) $(CPPFLAGS) $(CFLAGS) -g -I. -c -o $@ $^
+
+selabel_file_compiled-fuzzer: CFLAGS = -g -I../include -D_GNU_SOURCE -DNO_ANDROID_BACKEND $(PCRE_CFLAGS) -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -DDEBUG
+selabel_file_compiled-fuzzer: selabel_file_compiled-fuzzer.o $(LIBA)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(PCRE_LDLIBS)
+
 $(SWIGRUBYCOUT): $(SWIGRUBYIF)
 	$(SWIGRUBY) $<
 
@@ -215,7 +229,7 @@ clean-rubywrap:
 	-rm -f $(SWIGRUBYLOBJ) $(SWIGRUBYSO)
 
 clean: clean-pywrap clean-rubywrap
-	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) *.o *.lo *~
+	-rm -f $(LIBPC) $(OBJS) $(LOBJS) $(LIBA) $(LIBSO) $(TARGET) selabel_file_text-fuzzer selabel_file_compiled-fuzzer *.o *.lo *~
 
 distclean: clean
 	rm -f $(GENERATED) $(SWIGFILES)
libsepol/Makefile +3 -0
index 223edcec..efa10d57 100644
@@ -6,6 +6,9 @@ all:
 	$(MAKE) -C src 
 	$(MAKE) -C utils
 
+fuzz: all
+	$(MAKE) -C fuzz
+
 install: 
 	$(MAKE) -C include install
 	$(MAKE) -C src install
libsepol/fuzz/.gitignore new file +3 -0
new file mode 100644
index 00000000..88310076
@@ -0,0 +1,3 @@
+binpolicy-fuzzer
+secilc-fuzzer
+module-package-fuzzer
libsepol/fuzz/Makefile new file +25 -0
new file mode 100644
index 00000000..0cffaf20
@@ -0,0 +1,25 @@
+# Build the libsepol fuzzers as standalone debug binaries.
+# oss-fuzz.sh handles the sanitizer/libFuzzer build; this Makefile
+# only produces plain binaries suitable for gdb/valgrind on a single
+# reproducer input.
+TARGETS := binpolicy-fuzzer secilc-fuzzer module-package-fuzzer
+
+# Nothing in libsepol itself is conditional on
+# FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION so the normally-built
+# ../src/libsepol.a can be used as-is.
+LIBSEPOLA := ../src/libsepol.a
+
+CFLAGS ?= -g -O0 -Wall
+override CFLAGS += -I../include -I../cil/include -D_GNU_SOURCE -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -DDEBUG -DVERBOSE=1
+
+all: $(TARGETS)
+
+$(TARGETS): %: %.o $(LIBSEPOLA)
+	$(CC) $(LDFLAGS) -o $@ $^
+
+clean:
+	rm -f $(TARGETS) *.o
+
+.PHONY: all clean
+
+
libsepol/fuzz/binpolicy-fuzzer.c +39 -0
index f2379175..d9c27856 100644
@@ -12,7 +12,9 @@ extern int policydb_validate(sepol_handle_t *handle, const policydb_t *p);
 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
 
 // set to 1 to enable more verbose libsepol logging
+#ifndef VERBOSE
 #define VERBOSE 0
+#endif
 
 static int write_binary_policy(policydb_t *p, FILE *outfp)
 {
@@ -117,3 +119,40 @@ exit:
 	/* Non-zero return values are reserved for future use. */
 	return 0;
 }
+
+#ifdef DEBUG
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+int main(int argc, char **argv)
+{
+	if (argc < 2) {
+		fprintf(stderr, "usage: %s fuzzer-input-file\n", argv[0]);
+		exit(1);
+	}
+
+	FILE *fp = fopen(argv[1], "rb");
+	if (!fp) {
+		perror(argv[1]);
+		exit(1);
+	}
+
+	struct stat sb;
+	int rc;
+
+	rc = fstat(fileno(fp), &sb);
+	if (rc < 0) {
+		perror("fstat");
+		exit(1);
+	}
+
+	void *address = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE,
+			     MAP_PRIVATE, fileno(fp), 0);
+	if (address == MAP_FAILED) {
+		perror("mmap");
+		exit(1);
+	}
+
+	return LLVMFuzzerTestOneInput(address, sb.st_size);
+}
+#endif
libsepol/fuzz/module-package-fuzzer.c +39 -0
index 2516970b..c097742c 100644
@@ -5,7 +5,9 @@
 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
 
 // set to 1 to enable more verbose libsepol logging
+#ifndef VERBOSE
 #define VERBOSE 0
+#endif
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 {
@@ -45,3 +47,40 @@ exit:
 	/* Non-zero return values are reserved for future use. */
 	return 0;
 }
+
+#ifdef DEBUG
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+int main(int argc, char **argv)
+{
+	if (argc < 2) {
+		fprintf(stderr, "usage: %s fuzzer-input-file\n", argv[0]);
+		exit(1);
+	}
+
+	FILE *fp = fopen(argv[1], "rb");
+	if (!fp) {
+		perror(argv[1]);
+		exit(1);
+	}
+
+	struct stat sb;
+	int rc;
+
+	rc = fstat(fileno(fp), &sb);
+	if (rc < 0) {
+		perror("fstat");
+		exit(1);
+	}
+
+	void *address = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE,
+			     MAP_PRIVATE, fileno(fp), 0);
+	if (address == MAP_FAILED) {
+		perror("mmap");
+		exit(1);
+	}
+
+	return LLVMFuzzerTestOneInput(address, sb.st_size);
+}
+#endif
libsepol/fuzz/secilc-fuzzer.c +45 -0

66 lines not on this page · raw message

libsepol/src/Makefile +7 -0
index c222a8fd..4a3dc384 100644
@@ -89,6 +89,13 @@ endif
 %.lo:  %.c
 	$(CC) $(CPPFLAGS) $(CFLAGS) -fPIC -DSHARED -c -o $@ $<
 
+binpolicy-fuzzer.o: ../fuzz/binpolicy-fuzzer.c
+	$(CC) $(CPPFLAGS) $(CFLAGS) -g -I. -c -o $@ $^
+
+binpolicy-fuzzer: CFLAGS = -g -I. -I../include -I$(CILDIR)/include -D_GNU_SOURCE -DDEBUG -DVERBOSE=1 -DHAVE_REALLOCARRAY
+binpolicy-fuzzer: binpolicy-fuzzer.o $(LIBA)
+	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^
+
 install: all
 	test -d $(DESTDIR)$(LIBDIR) || install -m 755 -d $(DESTDIR)$(LIBDIR)
 	install -m 644 $(LIBA) $(DESTDIR)$(LIBDIR)
Signature · 1 line
2.55.0