/src/selinux/libsepol/fuzz/secilc-fuzzer.c
Line | Count | Source |
1 | | #include <stdlib.h> |
2 | | #include <stdio.h> |
3 | | #include <stdint.h> |
4 | | #include <string.h> |
5 | | #include <getopt.h> |
6 | | #include <sys/stat.h> |
7 | | |
8 | | #include <sepol/cil/cil.h> |
9 | | #include <sepol/policydb.h> |
10 | | |
11 | 1.88M | static void log_handler(__attribute__((unused)) int lvl, __attribute__((unused)) const char *msg) { |
12 | | /* be quiet */ |
13 | 1.88M | } |
14 | | |
15 | 16.1k | int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
16 | 16.1k | enum cil_log_level log_level = CIL_ERR; |
17 | 16.1k | struct sepol_policy_file *pf = NULL; |
18 | 16.1k | FILE *dev_null = NULL; |
19 | 16.1k | int target = SEPOL_TARGET_SELINUX; |
20 | 16.1k | int disable_dontaudit = 0; |
21 | 16.1k | int multiple_decls = 0; |
22 | 16.1k | int disable_neverallow = 0; |
23 | 16.1k | int preserve_tunables = 0; |
24 | 16.1k | int policyvers = POLICYDB_VERSION_MAX; |
25 | 16.1k | int mls = -1; |
26 | 16.1k | int attrs_expand_generated = 0; |
27 | 16.1k | struct cil_db *db = NULL; |
28 | 16.1k | sepol_policydb_t *pdb = NULL; |
29 | | |
30 | 16.1k | cil_set_log_level(log_level); |
31 | 16.1k | cil_set_log_handler(log_handler); |
32 | | |
33 | 16.1k | cil_db_init(&db); |
34 | 16.1k | cil_set_disable_dontaudit(db, disable_dontaudit); |
35 | 16.1k | cil_set_multiple_decls(db, multiple_decls); |
36 | 16.1k | cil_set_disable_neverallow(db, disable_neverallow); |
37 | 16.1k | cil_set_preserve_tunables(db, preserve_tunables); |
38 | 16.1k | cil_set_mls(db, mls); |
39 | 16.1k | cil_set_target_platform(db, target); |
40 | 16.1k | cil_set_policy_version(db, policyvers); |
41 | 16.1k | cil_set_attrs_expand_generated(db, attrs_expand_generated); |
42 | | |
43 | 16.1k | if (cil_add_file(db, "fuzz", (const char *)data, size) != SEPOL_OK) |
44 | 493 | goto exit; |
45 | | |
46 | 15.6k | if (cil_compile(db) != SEPOL_OK) |
47 | 12.1k | goto exit; |
48 | | |
49 | 3.51k | if (cil_build_policydb(db, &pdb) != SEPOL_OK) |
50 | 993 | goto exit; |
51 | | |
52 | 2.51k | if (sepol_policydb_optimize(pdb) != SEPOL_OK) |
53 | 0 | goto exit; |
54 | | |
55 | 2.51k | dev_null = fopen("/dev/null", "w"); |
56 | 2.51k | if (dev_null == NULL) |
57 | 0 | goto exit; |
58 | | |
59 | 2.51k | if (sepol_policy_file_create(&pf) != 0) |
60 | 0 | goto exit; |
61 | | |
62 | 2.51k | sepol_policy_file_set_fp(pf, dev_null); |
63 | | |
64 | 2.51k | if (sepol_policydb_write(pdb, pf) != 0) |
65 | 0 | goto exit; |
66 | 16.1k | exit: |
67 | 16.1k | if (dev_null != NULL) |
68 | 2.51k | fclose(dev_null); |
69 | | |
70 | 16.1k | cil_db_destroy(&db); |
71 | 16.1k | sepol_policydb_free(pdb); |
72 | 16.1k | sepol_policy_file_free(pf); |
73 | 16.1k | return 0; |
74 | 2.51k | } |