libsepol/fuzz: add POLICY_BASE and POLICY_MOD seed corpus entries
Stephen Smalley
maintainerPATCH 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
libsepol/fuzz/policy-mod.bin new file binary +0 -0
scripts/oss-fuzz.sh +2 -1
@@ -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
Stephen Smalley
maintainerPATCH 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
libsepol/fuzz/policy-kern-mls-v30.bin new file binary +0 -0
libsepol/fuzz/policy-kern-v30.bin new file binary +0 -0
libsepol/fuzz/policy-mod-v18.bin new file binary +0 -0
libsepol/fuzz/policy-xen.bin new file binary +0 -0
scripts/oss-fuzz.sh +1 -2
@@ -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
Stephen Smalley
maintainerPATCH 3/3 libsepol/fuzz: link a trailing module into a base policy
In reply to Stephen Smalleylink_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
@@ -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