forked from SELinuxProject/selinux
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
libsepol: add libfuzz based fuzzer for reading binary policies
Introduce a libfuzz[1] based fuzzer testing the parsing of a binary policy. Build the fuzzer in the oss-fuzz script. [1]: https://llvm.org/docs/LibFuzzer.html Signed-off-by: Christian Göttsche <[email protected]>
- Loading branch information
Showing
3 changed files
with
78 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <sepol/debug.h> | ||
#include <sepol/kernel_to_cil.h> | ||
#include <sepol/kernel_to_conf.h> | ||
#include <sepol/policydb/policydb.h> | ||
|
||
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); | ||
|
||
static int write_binary_policy(policydb_t *p, FILE *outfp) | ||
{ | ||
struct policy_file pf; | ||
|
||
policy_file_init(&pf); | ||
pf.type = PF_USE_STDIO; | ||
pf.fp = outfp; | ||
return policydb_write(p, &pf); | ||
} | ||
|
||
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) | ||
{ | ||
policydb_t policydb = {}; | ||
sidtab_t sidtab = {}; | ||
struct policy_file pf; | ||
FILE *devnull = NULL; | ||
|
||
sepol_debug(0); | ||
|
||
policy_file_init(&pf); | ||
pf.type = PF_USE_MEMORY; | ||
pf.data = (char *) data; | ||
pf.len = size; | ||
|
||
if (policydb_init(&policydb)) | ||
goto exit; | ||
|
||
if (policydb_read(&policydb, &pf, /*verbose=*/0)) | ||
goto exit; | ||
|
||
if (policydb_load_isids(&policydb, &sidtab)) | ||
goto exit; | ||
|
||
if (policydb.policy_type == POLICY_KERN) | ||
(void) policydb_optimize(&policydb); | ||
|
||
devnull = fopen("/dev/null", "w"); | ||
if (!devnull) | ||
goto exit; | ||
|
||
(void) write_binary_policy(&policydb, devnull); | ||
|
||
(void) sepol_kernel_policydb_to_conf(devnull, &policydb); | ||
|
||
(void) sepol_kernel_policydb_to_cil(devnull, &policydb); | ||
|
||
exit: | ||
if (devnull != NULL) | ||
fclose(devnull); | ||
|
||
policydb_destroy(&policydb); | ||
sepol_sidtab_destroy(&sidtab); | ||
|
||
/* Non-zero return values are reserved for future use. */ | ||
return 0; | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters