Skip to contentSkip to conversation

FUZZING.md,*/fuzz: document selinux fuzzing

PATCH In review Stephen Smalley 1 participant 2 messages Aug 28 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH v5 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.

---
v5 drops one more residual lingering cruft, gets rid of some trailing empty lines, and updates the FUZZING.md build instructions to note that one should always do a 'make clean all' first to clean any prior builds (especially if scripts/oss-fuzz.sh was run to get rid of the sanitizer artifacts) and build the base set of components leveraged by the make fuzz target.

 FUZZING.md                                    | 85 +++++++++++++++++++
 checkpolicy/Makefile                          |  3 +
 checkpolicy/fuzz/.gitignore                   |  1 +
 checkpolicy/fuzz/Makefile                     | 38 +++++++++
 checkpolicy/fuzz/checkpolicy-fuzzer.c         | 53 ++++++++++++
 libselinux/Makefile                           |  3 +
 libselinux/fuzz/.gitignore                    |  2 +
 libselinux/fuzz/Makefile                      | 31 +++++++
 .../fuzz/selabel_file_compiled-fuzzer.c       | 47 ++++++++++
 libselinux/fuzz/selabel_file_text-fuzzer.c    | 81 ++++++++++++++++++
 libsepol/Makefile                             |  3 +
 libsepol/fuzz/.gitignore                      |  3 +
 libsepol/fuzz/Makefile                        | 24 ++++++
 libsepol/fuzz/binpolicy-fuzzer.c              | 39 +++++++++
 libsepol/fuzz/module-package-fuzzer.c         | 39 +++++++++
 libsepol/fuzz/secilc-fuzzer.c                 | 45 ++++++++++
 16 files changed, 497 insertions(+)
 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 +85 -0
new file mode 100644
index 00000000..1f0ebbfb
@@ -0,0 +1,85 @@
+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 clean all
+    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 +38 -0
new file mode 100644
index 00000000..a7f619d0
@@ -0,0 +1,38 @@
+# 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 -DDEFINEMAIN -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 +31 -0
new file mode 100644
index 00000000..5410c487
@@ -0,0 +1,31 @@
+# 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 \
+	-DDEFINEMAIN -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

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 +24 -0
new file mode 100644
index 00000000..4c8820ae
@@ -0,0 +1,24 @@
+# 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 -DDEFINEMAIN -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..c75d0106 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 DEFINEMAIN
+#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..bce9155b 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 DEFINEMAIN
+#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

Signature · 1 line
2.55.0
author maintainer
#1 lore raw
Quoted 5 lines · Stephen Smalley

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

Quoted 8 lines

---
v5 drops one more residual lingering cruft, gets rid of some trailing empty lines, and updates the FUZZING.md build instructions to note that one should always do a 'make clean all' first to clean any prior builds (especially if scripts/oss-fuzz.sh was run to get rid of the sanitizer artifacts) and build the base set of components leveraged by the make fuzz target.

This is now merged but happy to take follow ups to further improve.