Coverage Report

Created: 2026-09-01 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
#ifndef VERBOSE
12
#define VERBOSE 0
13
#endif
14
15
#if !VERBOSE
16
static void log_handler(__attribute__((unused)) int lvl,
17
      __attribute__((unused)) const char *msg)
18
0
{
19
  /* be quiet */
20
0
}
21
#endif
22
23
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
24
{
25
  enum cil_log_level log_level = CIL_ERR;
26
  struct sepol_policy_file *pf = NULL;
27
  FILE *dev_null = NULL;
28
  int target = SEPOL_TARGET_SELINUX;
29
  int disable_dontaudit = 0;
30
  int multiple_decls = 0;
31
  int disable_neverallow = 0;
32
  int preserve_tunables = 0;
33
  int policyvers = POLICYDB_VERSION_MAX;
34
  int mls = -1;
35
  int attrs_expand_generated = 0;
36
  struct cil_db *db = NULL;
37
  sepol_policydb_t *pdb = NULL;
38
39
  cil_set_log_level(log_level);
40
#if !VERBOSE
41
  cil_set_log_handler(log_handler);
42
#endif
43
44
  cil_db_init(&db);
45
  cil_set_disable_dontaudit(db, disable_dontaudit);
46
  cil_set_multiple_decls(db, multiple_decls);
47
  cil_set_disable_neverallow(db, disable_neverallow);
48
  cil_set_preserve_tunables(db, preserve_tunables);
49
  cil_set_mls(db, mls);
50
  cil_set_target_platform(db, target);
51
  cil_set_policy_version(db, policyvers);
52
  cil_set_attrs_expand_generated(db, attrs_expand_generated);
53
54
  if (cil_add_file(db, "fuzz", (const char *)data, size) != SEPOL_OK)
55
    goto exit;
56
57
  if (cil_compile(db) != SEPOL_OK)
58
    goto exit;
59
60
  if (cil_build_policydb(db, &pdb) != SEPOL_OK)
61
    goto exit;
62
63
  if (sepol_policydb_optimize(pdb) != SEPOL_OK)
64
    goto exit;
65
66
  dev_null = fopen("/dev/null", "w");
67
  if (dev_null == NULL)
68
    goto exit;
69
70
  if (sepol_policy_file_create(&pf) != 0)
71
    goto exit;
72
73
  sepol_policy_file_set_fp(pf, dev_null);
74
75
  if (sepol_policydb_write(pdb, pf) != 0)
76
    goto exit;
77
exit:
78
  if (dev_null != NULL)
79
    fclose(dev_null);
80
81
  cil_db_destroy(&db);
82
  sepol_policydb_free(pdb);
83
  sepol_policy_file_free(pf);
84
  return 0;
85
}
86
87
#ifdef DEFINEMAIN
88
#include <sys/mman.h>
89
#include <sys/stat.h>
90
91
int main(int argc, char **argv)
92
{
93
  if (argc < 2) {
94
    fprintf(stderr, "usage: %s fuzzer-input-file\n", argv[0]);
95
    exit(1);
96
  }
97
98
  FILE *fp = fopen(argv[1], "rb");
99
  if (!fp) {
100
    perror(argv[1]);
101
    exit(1);
102
  }
103
104
  struct stat sb;
105
  int rc;
106
107
  rc = fstat(fileno(fp), &sb);
108
  if (rc < 0) {
109
    perror("fstat");
110
    exit(1);
111
  }
112
113
  void *address = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE,
114
           MAP_PRIVATE, fileno(fp), 0);
115
  if (address == MAP_FAILED) {
116
    perror("mmap");
117
    exit(1);
118
  }
119
120
  return LLVMFuzzerTestOneInput(address, sb.st_size);
121
}
122
#endif