• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <sepol/debug.h>
2 #include <sepol/kernel_to_cil.h>
3 #include <sepol/kernel_to_conf.h>
4 #include <sepol/policydb/policydb.h>
5 
6 extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
7 
write_binary_policy(policydb_t * p,FILE * outfp)8 static int write_binary_policy(policydb_t *p, FILE *outfp)
9 {
10 	struct policy_file pf;
11 
12 	policy_file_init(&pf);
13 	pf.type = PF_USE_STDIO;
14 	pf.fp = outfp;
15 	return policydb_write(p, &pf);
16 }
17 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)18 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
19 {
20 	policydb_t policydb = {};
21 	sidtab_t sidtab = {};
22 	struct policy_file pf;
23 	FILE *devnull = NULL;
24 
25 	sepol_debug(0);
26 
27 	policy_file_init(&pf);
28 	pf.type = PF_USE_MEMORY;
29 	pf.data = (char *) data;
30 	pf.len = size;
31 
32 	if (policydb_init(&policydb))
33 		goto exit;
34 
35 	if (policydb_read(&policydb, &pf, /*verbose=*/0))
36 		goto exit;
37 
38 	if (policydb_load_isids(&policydb, &sidtab))
39 		goto exit;
40 
41 	if (policydb.policy_type == POLICY_KERN)
42 		(void) policydb_optimize(&policydb);
43 
44 	devnull = fopen("/dev/null", "w");
45 	if (!devnull)
46 		goto exit;
47 
48 	(void) write_binary_policy(&policydb, devnull);
49 
50 	(void) sepol_kernel_policydb_to_conf(devnull, &policydb);
51 
52 	(void) sepol_kernel_policydb_to_cil(devnull, &policydb);
53 
54 exit:
55 	if (devnull != NULL)
56 		fclose(devnull);
57 
58 	policydb_destroy(&policydb);
59 	sepol_sidtab_destroy(&sidtab);
60 
61 	/* Non-zero return values are reserved for future use. */
62 	return 0;
63 }
64