Skip to contentSkip to conversation

libsepol/fuzz: add POLICY_BASE and POLICY_MOD seed corpus entries

PATCH Superseded Stephen Smalley 1 participant 3 messages Sep 11 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/3 libsepol/fuzz: add POLICY_BASE and POLICY_MOD seed corpus entries

The shipped binpolicy-fuzzer seed is a POLICY_KERN policy, so link_modules() and sepol_module_policydb_to_cil() only get exercised once the fuzzer happens to mutate the magic and type fields into a valid module header. Ship a small base and module policy (compiled from libsepol/tests/policies/test-deps/) so those paths are covered from the first iteration.

---
 libsepol/fuzz/policy-base.bin | Bin 0 -> 10780 bytes
 libsepol/fuzz/policy-mod.bin  | Bin 0 -> 1014 bytes
 scripts/oss-fuzz.sh           |   3 ++-
 3 files changed, 2 insertions(+), 1 deletion(-)
 create mode 100644 libsepol/fuzz/policy-base.bin
 create mode 100644 libsepol/fuzz/policy-mod.bin
libsepol/fuzz/policy-base.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..19601d57564b1d756e74aeedc54135bb7e92471b
GIT binary patch
libsepol/fuzz/policy-mod.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..145156a7397e49f932c3595b4766096af9b0d1d6
GIT binary patch
scripts/oss-fuzz.sh +2 -1
index 08e06975..3e3532c9 100755
@@ -72,7 +72,8 @@ $CC $CFLAGS -c -o binpolicy-fuzzer.o libsepol/fuzz/binpolicy-fuzzer.c
 # shellcheck disable=SC2086
 $CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/binpolicy-fuzzer"
 
-zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin
+zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin \
+	libsepol/fuzz/policy-base.bin libsepol/fuzz/policy-mod.bin
 
 ## module package fuzzer ##
 
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/3 libsepol/fuzz: add Xen, v30 kernel and v18 module seed corpus entries

The binpolicy fuzzer had no seed for the Xen target platform, so ocontext_read_xen() was effectively unreachable (the fuzzer would have to mutate the full "SE Linux" target string into "XenFlask" unaided). Similarly the oldest supported kernel format (v30) and the module/base format (v18) have version-specific read paths that the existing v32/v24 seeds cannot reach without also mutating the coordinated sym_num/ocon_num header fields to match the compat table.

Add small seed policies for each of these and switch the seed-corpus zip to a glob so future additions do not need to touch the script.

Generated with:
checkpolicy -t Xen -c 30 checkpolicy/tests/policy_allonce_xen.conf checkpolicy -c 30 checkpolicy/tests/policy_minimal.conf checkpolicy -M -c 30 checkpolicy/tests/policy_minimal_mls.conf checkmodule -b -M -c 18 libsepol/fuzz/policy-base.bin checkmodule -b -M -c 18 libsepol/fuzz/policy-mod.bin

---
 libsepol/fuzz/policy-base-v18.bin     | Bin 0 -> 10772 bytes
 libsepol/fuzz/policy-kern-mls-v30.bin | Bin 0 -> 618 bytes
 libsepol/fuzz/policy-kern-v30.bin     | Bin 0 -> 518 bytes
 libsepol/fuzz/policy-mod-v18.bin      | Bin 0 -> 1034 bytes
 libsepol/fuzz/policy-xen.bin          | Bin 0 -> 2085 bytes
 scripts/oss-fuzz.sh                   |   3 +--
 6 files changed, 1 insertion(+), 2 deletions(-)
 create mode 100644 libsepol/fuzz/policy-base-v18.bin
 create mode 100644 libsepol/fuzz/policy-kern-mls-v30.bin
 create mode 100644 libsepol/fuzz/policy-kern-v30.bin
 create mode 100644 libsepol/fuzz/policy-mod-v18.bin
 create mode 100644 libsepol/fuzz/policy-xen.bin
libsepol/fuzz/policy-base-v18.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..4057e406a2768a660050daec0069c0d5557df175
GIT binary patch
libsepol/fuzz/policy-kern-mls-v30.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..22748058bb1f76701a5c0c7682e6a4c2a0be8654
GIT binary patch
libsepol/fuzz/policy-kern-v30.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..9bd8662127e38184a21ae3861b1590156ec368c6
GIT binary patch
libsepol/fuzz/policy-mod-v18.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..6906ec42c36ff35b5aa780960dfde36006f436f5
GIT binary patch
libsepol/fuzz/policy-xen.bin new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..6e7dec3adb1d4b4a7f79e748e3992f877a2f5204
GIT binary patch
scripts/oss-fuzz.sh +1 -2
index 3e3532c9..c8ab9ee1 100755
@@ -72,8 +72,7 @@ $CC $CFLAGS -c -o binpolicy-fuzzer.o libsepol/fuzz/binpolicy-fuzzer.c
 # shellcheck disable=SC2086
 $CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/binpolicy-fuzzer"
 
-zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin \
-	libsepol/fuzz/policy-base.bin libsepol/fuzz/policy-mod.bin
+zip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/*.bin
 
 ## module package fuzzer ##
 
Signature · 1 line
2.55.0
author maintainer
#2 lore raw

PATCH 3/3 libsepol/fuzz: link a trailing module into a base policy

In reply to Stephen Smalley

link_modules() is currently invoked with mods=NULL, so all of link.c's per-module remapping and copy paths (copy_module, *_copy_callback, copy_scope_index, ebitmap_convert) are unreachable from the fuzzer. Two of the recent link.c hardening fixes (d7a298a7) sit exactly in that dead subtree.

If a POLICY_BASE input carries a second concatenated policydb and it reads/validates as a matching-MLS POLICY_MOD, hand it to link_modules() as a single-element module array. Existing single-policy corpus entries have pf.len == 0 after the first read and behave exactly as before; a trailing blob that fails to parse as a module is discarded and the base is still linked and expanded on its own.

Ship a base+module concatenation (the test-deps pair from 34610ea2, which link together by design) as a seed so the new path is covered from the first iteration.

---
 libsepol/fuzz/binpolicy-fuzzer.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
libsepol/fuzz/binpolicy-fuzzer.c +24 -2
index c75d0106..977fcfb4 100644
@@ -28,7 +28,9 @@ static int write_binary_policy(policydb_t *p, FILE *outfp)
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 {
-	policydb_t policydb = {}, out = {};
+	policydb_t policydb = {}, out = {}, modpol = {};
+	policydb_t *mods[] = { &modpol };
+	int nmods = 0;
 	sidtab_t sidtab = {};
 	struct policy_file pf;
 	FILE *devnull = NULL;
@@ -80,7 +82,26 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 			abort();
 
 		if (policydb.policy_type == POLICY_BASE) {
-			if (link_modules(NULL, &policydb, NULL, 0, VERBOSE))
+			/*
+			 * If the input carries a second concatenated policydb after
+			 * the base and it is a POLICY_MOD, feed it to link_modules()
+			 * so that link.c's per-module remapping and copy paths are
+			 * exercised (with mods=NULL that whole subtree is dead).
+			 * Trailing junk that fails to read/validate is ignored.
+			 */
+			if (pf.len > 0 && policydb_init(&modpol) == 0) {
+				if (policydb_read(&modpol, &pf, VERBOSE) == 0 &&
+				    modpol.policy_type == POLICY_MOD &&
+				    modpol.mls == policydb.mls) {
+					nmods = 1;
+				} else {
+					policydb_destroy(&modpol);
+					memset(&modpol, 0, sizeof(modpol));
+				}
+			}
+
+			if (link_modules(NULL, &policydb, nmods ? mods : NULL,
+					 nmods, VERBOSE))
 				goto exit;
 
 			if (policydb_init(&out))
@@ -113,6 +134,7 @@ exit:
 		fclose(devnull);
 
 	policydb_destroy(&out);
+	policydb_destroy(&modpol);
 	policydb_destroy(&policydb);
 	sepol_sidtab_destroy(&sidtab);
 
Signature · 1 line
2.55.0