Coverage Report

Created: 2025-08-29 06:31

/src/selinux/libsepol/fuzz/binpolicy-fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
#include <sepol/debug.h>
2
#include <sepol/kernel_to_cil.h>
3
#include <sepol/kernel_to_conf.h>
4
#include <sepol/policydb/expand.h>
5
#include <sepol/policydb/hierarchy.h>
6
#include <sepol/policydb/link.h>
7
#include <sepol/policydb/policydb.h>
8
9
extern int policydb_validate(sepol_handle_t *handle, const policydb_t *p);
10
11
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
12
13
14
// set to 1 to enable more verbose libsepol logging
15
9.60k
#define VERBOSE 0
16
17
18
static int write_binary_policy(policydb_t *p, FILE *outfp)
19
653
{
20
653
  struct policy_file pf;
21
22
653
  policy_file_init(&pf);
23
653
  pf.type = PF_USE_STDIO;
24
653
  pf.fp = outfp;
25
653
  return policydb_write(p, &pf);
26
653
}
27
28
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
29
4.71k
{
30
4.71k
  policydb_t policydb = {}, out = {};
31
4.71k
  sidtab_t sidtab = {};
32
4.71k
  struct policy_file pf;
33
4.71k
  FILE *devnull = NULL;
34
35
4.71k
  sepol_debug(VERBOSE);
36
37
4.71k
  policy_file_init(&pf);
38
4.71k
  pf.type = PF_USE_MEMORY;
39
4.71k
  pf.data = (char *) data;
40
4.71k
  pf.len = size;
41
42
4.71k
  if (policydb_init(&policydb))
43
0
    goto exit;
44
45
4.71k
  if (policydb_read(&policydb, &pf, VERBOSE))
46
4.13k
    goto exit;
47
48
578
  if (policydb_load_isids(&policydb, &sidtab))
49
1
    goto exit;
50
51
577
  if (policydb.policy_type == POLICY_KERN) {
52
424
    (void) policydb_optimize(&policydb);
53
54
424
    if (policydb_validate(NULL, &policydb) == -1)
55
0
      abort();
56
424
  }
57
58
577
  if (policydb.global->branch_list)
59
556
    (void) check_assertions(NULL, &policydb, policydb.global->branch_list->avrules);
60
61
577
  (void) hierarchy_check_constraints(NULL, &policydb);
62
63
577
  devnull = fopen("/dev/null", "we");
64
577
  if (!devnull)
65
0
    goto exit;
66
67
577
  if (write_binary_policy(&policydb, devnull))
68
0
    abort();
69
70
577
  if (policydb.policy_type == POLICY_KERN) {
71
424
    if (sepol_kernel_policydb_to_conf(devnull, &policydb))
72
0
      abort();
73
74
424
    if (sepol_kernel_policydb_to_cil(devnull, &policydb))
75
0
      abort();
76
77
424
  } else if (policydb.policy_type == POLICY_BASE) {
78
98
    if (link_modules(NULL, &policydb, NULL, 0, VERBOSE))
79
20
      goto exit;
80
81
78
    if (policydb_init(&out))
82
0
      goto exit;
83
84
78
    if (expand_module(NULL, &policydb, &out, VERBOSE, /*check_assertions=*/0))
85
2
      goto exit;
86
87
76
    (void) check_assertions(NULL, &out, out.global->branch_list->avrules);
88
76
    (void) hierarchy_check_constraints(NULL, &out);
89
90
76
    if (write_binary_policy(&out, devnull))
91
0
      abort();
92
93
76
    if (sepol_kernel_policydb_to_conf(devnull, &out))
94
0
      abort();
95
96
76
    if (sepol_kernel_policydb_to_cil(devnull, &out))
97
0
      abort();
98
99
76
  }
100
101
4.71k
exit:
102
4.71k
  if (devnull != NULL)
103
577
    fclose(devnull);
104
105
4.71k
  policydb_destroy(&out);
106
4.71k
  policydb_destroy(&policydb);
107
4.71k
  sepol_sidtab_destroy(&sidtab);
108
109
  /* Non-zero return values are reserved for future use. */
110
4.71k
  return 0;
111
577
}