Skip to contentSkip to conversation

libsepol: add module package fuzzer

PATCH Superseded Petr Matyas 2 participants 5 messages Jul 20 – Jul 24 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: add module package fuzzer

Add a libFuzzer target for the binary module package format (.mod/.pp) read by sepol_module_package_read(). On a valid package, also exercises sepol_module_package_to_cil() to cover the CIL conversion path for module policies.

The seed corpus contains a minimal module package produced by checkmodule + semodule_package to give the fuzzer a structurally correct starting point.

Wire the new fuzzer into scripts/oss-fuzz.sh alongside the existing libsepol and checkpolicy fuzzers.

---
 libsepol/fuzz/min_mod.pp              | Bin 0 -> 859 bytes
 libsepol/fuzz/module-package-fuzzer.c |  44 ++++++++++++++++++++++++++
 scripts/oss-fuzz.sh                   |  11 +++++++
 3 files changed, 55 insertions(+)
 create mode 100644 libsepol/fuzz/min_mod.pp
 create mode 100644 libsepol/fuzz/module-package-fuzzer.c
libsepol/fuzz/min_mod.pp new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..264bd4406bf94b1c0044d46d0ca82fcdc4716f88
GIT binary patch
libsepol/fuzz/module-package-fuzzer.c new file +44 -0

44 lines not on this page · raw message

scripts/oss-fuzz.sh +11 -0
index 9376b6e4..08e06975 100755
@@ -74,6 +74,17 @@ $CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol
 
 zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin
 
+## module package fuzzer ##
+
+# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by
+# the compiler/linker so they shouldn't be quoted
+# shellcheck disable=SC2086
+$CC $CFLAGS -c -o module-package-fuzzer.o libsepol/fuzz/module-package-fuzzer.c
+# shellcheck disable=SC2086
+$CXX $CXXFLAGS $LIB_FUZZING_ENGINE module-package-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/module-package-fuzzer"
+
+zip -j "$OUT/module-package-fuzzer_seed_corpus.zip" libsepol/fuzz/min_mod.pp
+
 ## checkpolicy fuzzer ##
 
 make -C checkpolicy clean
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 52 lines · Petr Matyas

Add a libFuzzer target for the binary module package format (.mod/.pp) read by sepol_module_package_read(). On a valid package, also exercises sepol_module_package_to_cil() to cover the CIL conversion path for module policies.

The seed corpus contains a minimal module package produced by checkmodule + semodule_package to give the fuzzer a structurally correct starting point.

Wire the new fuzzer into scripts/oss-fuzz.sh alongside the existing libsepol and checkpolicy fuzzers.

---
 libsepol/fuzz/min_mod.pp              | Bin 0 -> 859 bytes
 libsepol/fuzz/module-package-fuzzer.c |  44 ++++++++++++++++++++++++++
 scripts/oss-fuzz.sh                   |  11 +++++++
 3 files changed, 55 insertions(+)
 create mode 100644 libsepol/fuzz/min_mod.pp
 create mode 100644 libsepol/fuzz/module-package-fuzzer.c
libsepol/fuzz/module-package-fuzzer.c new file +24 -0
new file mode 100644
index 00000000..18d645bb
@@ -0,0 +1,44 @@
+#include <sepol/debug.h>
+#include <sepol/module.h>
+#include <sepol/module_to_cil.h>
+
+extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
+
+// set to 1 to enable more verbose libsepol logging
+#define VERBOSE 0
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+       sepol_module_package_t *mod = NULL;
+       struct sepol_policy_file *spf = NULL;
+       FILE *devnull = NULL;
+
+       sepol_debug(VERBOSE);
+
+       if (sepol_policy_file_create(&spf))
+               goto exit;
+
+       sepol_policy_file_set_mem(spf, (char *)data, size);
+
+       if (sepol_module_package_create(&mod))
+               goto exit;

libsepol/fuzz/module-package-fuzzer.c line 34

Quoted 10 lines
libsepol/fuzz/module-package-fuzzer.c new file +10 -0
+
+       if (sepol_module_package_read(mod, spf, VERBOSE))
+               goto exit;
+
+       devnull = fopen("/dev/null", "we");
+       if (!devnull)
+               goto exit;
+
+       if (sepol_module_package_to_cil(devnull, mod))
+               abort();

Do we want it to abort if there was garbage in the .fc file or other *_contexts files?
sepol_module_package_read() only parses the binary module and extracts the raw contents of the contexts files but doesn't try to parse them, so the error won't show up
until we try to convert to CIL.

Quoted 10 lines
libsepol/fuzz/module-package-fuzzer.c new file +10 -0
+
+exit:
+       if (devnull)
+               fclose(devnull);
+       sepol_module_package_free(mod);
+       sepol_policy_file_free(spf);
+
+       /* Non-zero return values are reserved for future use. */
+       return 0;
+}
#2 lore raw

PATCH v2 1/1 libsepol: add module package fuzzer

In reply to Petr Matyas

Add a libFuzzer target for the binary module package format (.mod/.pp) read by sepol_module_package_read(). On a valid package, also exercises sepol_module_package_to_cil() to cover the CIL conversion path for module policies.

The seed corpus contains a minimal module package produced by checkmodule + semodule_package to give the fuzzer a structurally correct starting point.

Wire the new fuzzer into scripts/oss-fuzz.sh alongside the existing libsepol and checkpolicy fuzzers.

---
 libsepol/fuzz/min_mod.pp              | Bin 0 -> 859 bytes
 libsepol/fuzz/module-package-fuzzer.c |  47 ++++++++++++++++++++++++++
 scripts/oss-fuzz.sh                   |  11 ++++++
 3 files changed, 58 insertions(+)
 create mode 100644 libsepol/fuzz/min_mod.pp
 create mode 100644 libsepol/fuzz/module-package-fuzzer.c
libsepol/fuzz/min_mod.pp new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..264bd4406bf94b1c0044d46d0ca82fcdc4716f88
GIT binary patch
libsepol/fuzz/module-package-fuzzer.c new file +47 -0

47 lines not on this page · raw message

scripts/oss-fuzz.sh +11 -0
index 9376b6e4..08e06975 100755
@@ -74,6 +74,17 @@ $CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol
 
 zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin
 
+## module package fuzzer ##
+
+# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by
+# the compiler/linker so they shouldn't be quoted
+# shellcheck disable=SC2086
+$CC $CFLAGS -c -o module-package-fuzzer.o libsepol/fuzz/module-package-fuzzer.c
+# shellcheck disable=SC2086
+$CXX $CXXFLAGS $LIB_FUZZING_ENGINE module-package-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/module-package-fuzzer"
+
+zip -j "$OUT/module-package-fuzzer_seed_corpus.zip" libsepol/fuzz/min_mod.pp
+
 ## checkpolicy fuzzer ##
 
 make -C checkpolicy clean
Signature · 1 line
2.55.0
maintainer Acked-by on v2 1/1
#3 lore raw
Quoted 6 lines · Petr Matyas

Add a libFuzzer target for the binary module package format (.mod/.pp) read by sepol_module_package_read(). On a valid package, also exercises sepol_module_package_to_cil() to cover the CIL conversion path for module policies.

Quoted 8 lines

The seed corpus contains a minimal module package produced by checkmodule + semodule_package to give the fuzzer a structurally correct starting point.

Wire the new fuzzer into scripts/oss-fuzz.sh alongside the existing libsepol and checkpolicy fuzzers.

Quoted 101 lines
---
 libsepol/fuzz/min_mod.pp              | Bin 0 -> 859 bytes
 libsepol/fuzz/module-package-fuzzer.c |  47 ++++++++++++++++++++++++++
 scripts/oss-fuzz.sh                   |  11 ++++++
 3 files changed, 58 insertions(+)
 create mode 100644 libsepol/fuzz/min_mod.pp
 create mode 100644 libsepol/fuzz/module-package-fuzzer.c
libsepol/fuzz/min_mod.pp new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..264bd4406bf94b1c0044d46d0ca82fcdc4716f88
GIT binary patch
libsepol/fuzz/module-package-fuzzer.c new file +47 -0
new file mode 100644
index 00000000..2516970b
@@ -0,0 +1,47 @@
+#include <sepol/debug.h>
+#include <sepol/module.h>
+#include <sepol/module_to_cil.h>
+
+extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
+
+// set to 1 to enable more verbose libsepol logging
+#define VERBOSE 0
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+       sepol_module_package_t *mod = NULL;
+       struct sepol_policy_file *spf = NULL;
+       FILE *devnull = NULL;
+
+       sepol_debug(VERBOSE);
+
+       if (sepol_policy_file_create(&spf))
+               goto exit;
+
+       sepol_policy_file_set_mem(spf, (char *)data, size);
+
+       if (sepol_module_package_create(&mod))
+               goto exit;
+
+       if (sepol_module_package_read(mod, spf, VERBOSE))
+               goto exit;
+
+       devnull = fopen("/dev/null", "we");
+       if (!devnull)
+               goto exit;
+
+       /* sepol_module_package_read() stores contexts sections as raw blobs
+        * without validating their text syntax; sepol_module_package_to_cil()
+        * is the first place that parses them, so a graceful error return here
+        * is expected behaviour for malformed input, not a bug. */
+       (void)sepol_module_package_to_cil(devnull, mod);
+
+exit:
+       if (devnull)
+               fclose(devnull);
+       sepol_module_package_free(mod);
+       sepol_policy_file_free(spf);
+
+       /* Non-zero return values are reserved for future use. */
+       return 0;
+}
scripts/oss-fuzz.sh +11 -1
index 9376b6e4..08e06975 100755
@@ -74,6 +74,17 @@ $CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol
 
 zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin
 
+## module package fuzzer ##
+
+# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by
+# the compiler/linker so they shouldn't be quoted
+# shellcheck disable=SC2086
+$CC $CFLAGS -c -o module-package-fuzzer.o libsepol/fuzz/module-package-fuzzer.c
+# shellcheck disable=SC2086
+$CXX $CXXFLAGS $LIB_FUZZING_ENGINE module-package-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/module-package-fuzzer"
+
+zip -j "$OUT/module-package-fuzzer_seed_corpus.zip" libsepol/fuzz/min_mod.pp
+
 ## checkpolicy fuzzer ##
 
 make -C checkpolicy clean
--

2.55.0

maintainer
#4 lore raw
Quoted 12 lines · Stephen Smalley
Quoted 10 lines · Petr Matyas

Add a libFuzzer target for the binary module package format (.mod/.pp) read by sepol_module_package_read(). On a valid package, also exercises sepol_module_package_to_cil() to cover the CIL conversion path for module policies.

The seed corpus contains a minimal module package produced by checkmodule + semodule_package to give the fuzzer a structurally correct starting point.

Quoted 6 lines
Quoted 4 lines

Wire the new fuzzer into scripts/oss-fuzz.sh alongside the existing libsepol and checkpolicy fuzzers.

Merged. Would also suggest having it call sepol_module_package_info() to exercise that code path.

Quoted 102 lines
Quoted 102 lines
---
 libsepol/fuzz/min_mod.pp              | Bin 0 -> 859 bytes
 libsepol/fuzz/module-package-fuzzer.c |  47 ++++++++++++++++++++++++++
 scripts/oss-fuzz.sh                   |  11 ++++++
 3 files changed, 58 insertions(+)
 create mode 100644 libsepol/fuzz/min_mod.pp
 create mode 100644 libsepol/fuzz/module-package-fuzzer.c
libsepol/fuzz/min_mod.pp new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..264bd4406bf94b1c0044d46d0ca82fcdc4716f88
GIT binary patch
libsepol/fuzz/module-package-fuzzer.c new file +47 -0
new file mode 100644
index 00000000..2516970b
@@ -0,0 +1,47 @@
+#include <sepol/debug.h>
+#include <sepol/module.h>
+#include <sepol/module_to_cil.h>
+
+extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
+
+// set to 1 to enable more verbose libsepol logging
+#define VERBOSE 0
+
+int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
+{
+       sepol_module_package_t *mod = NULL;
+       struct sepol_policy_file *spf = NULL;
+       FILE *devnull = NULL;
+
+       sepol_debug(VERBOSE);
+
+       if (sepol_policy_file_create(&spf))
+               goto exit;
+
+       sepol_policy_file_set_mem(spf, (char *)data, size);
+
+       if (sepol_module_package_create(&mod))
+               goto exit;
+
+       if (sepol_module_package_read(mod, spf, VERBOSE))
+               goto exit;
+
+       devnull = fopen("/dev/null", "we");
+       if (!devnull)
+               goto exit;
+
+       /* sepol_module_package_read() stores contexts sections as raw blobs
+        * without validating their text syntax; sepol_module_package_to_cil()
+        * is the first place that parses them, so a graceful error return here
+        * is expected behaviour for malformed input, not a bug. */
+       (void)sepol_module_package_to_cil(devnull, mod);
+
+exit:
+       if (devnull)
+               fclose(devnull);
+       sepol_module_package_free(mod);
+       sepol_policy_file_free(spf);
+
+       /* Non-zero return values are reserved for future use. */
+       return 0;
+}
scripts/oss-fuzz.sh +11 -1
index 9376b6e4..08e06975 100755
@@ -74,6 +74,17 @@ $CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol
 
 zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin
 
+## module package fuzzer ##
+
+# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by
+# the compiler/linker so they shouldn't be quoted
+# shellcheck disable=SC2086
+$CC $CFLAGS -c -o module-package-fuzzer.o libsepol/fuzz/module-package-fuzzer.c
+# shellcheck disable=SC2086
+$CXX $CXXFLAGS $LIB_FUZZING_ENGINE module-package-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/module-package-fuzzer"
+
+zip -j "$OUT/module-package-fuzzer_seed_corpus.zip" libsepol/fuzz/min_mod.pp
+
 ## checkpolicy fuzzer ##
 
 make -C checkpolicy clean
--

2.55.0