2021-12-09 16:48:56 +00:00
|
|
|
#include <sepol/debug.h>
|
|
|
|
#include <sepol/kernel_to_cil.h>
|
|
|
|
#include <sepol/kernel_to_conf.h>
|
2023-11-28 18:23:33 +00:00
|
|
|
#include <sepol/policydb/expand.h>
|
|
|
|
#include <sepol/policydb/hierarchy.h>
|
|
|
|
#include <sepol/policydb/link.h>
|
2021-12-09 16:48:56 +00:00
|
|
|
#include <sepol/policydb/policydb.h>
|
|
|
|
|
2023-07-06 14:02:33 +00:00
|
|
|
extern int policydb_validate(sepol_handle_t *handle, const policydb_t *p);
|
|
|
|
|
2021-12-09 16:48:56 +00:00
|
|
|
extern int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
|
|
|
|
|
2023-11-28 18:23:33 +00:00
|
|
|
|
|
|
|
// set to 1 to enable more verbose libsepol logging
|
|
|
|
#define VERBOSE 0
|
|
|
|
|
|
|
|
|
2021-12-09 16:48:56 +00:00
|
|
|
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)
|
|
|
|
{
|
2023-11-28 18:23:33 +00:00
|
|
|
policydb_t policydb = {}, out = {};
|
2021-12-09 16:48:56 +00:00
|
|
|
sidtab_t sidtab = {};
|
|
|
|
struct policy_file pf;
|
|
|
|
FILE *devnull = NULL;
|
|
|
|
|
2023-11-28 18:23:33 +00:00
|
|
|
sepol_debug(VERBOSE);
|
2021-12-09 16:48:56 +00:00
|
|
|
|
|
|
|
policy_file_init(&pf);
|
|
|
|
pf.type = PF_USE_MEMORY;
|
|
|
|
pf.data = (char *) data;
|
|
|
|
pf.len = size;
|
|
|
|
|
|
|
|
if (policydb_init(&policydb))
|
|
|
|
goto exit;
|
|
|
|
|
2023-11-28 18:23:33 +00:00
|
|
|
if (policydb_read(&policydb, &pf, VERBOSE))
|
2021-12-09 16:48:56 +00:00
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (policydb_load_isids(&policydb, &sidtab))
|
|
|
|
goto exit;
|
|
|
|
|
2023-07-06 14:02:33 +00:00
|
|
|
if (policydb.policy_type == POLICY_KERN) {
|
2021-12-09 16:48:56 +00:00
|
|
|
(void) policydb_optimize(&policydb);
|
|
|
|
|
2023-07-06 14:02:33 +00:00
|
|
|
if (policydb_validate(NULL, &policydb) == -1)
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2023-11-28 18:23:33 +00:00
|
|
|
if (policydb.global->branch_list)
|
|
|
|
(void) check_assertions(NULL, &policydb, policydb.global->branch_list->avrules);
|
|
|
|
|
|
|
|
(void) hierarchy_check_constraints(NULL, &policydb);
|
2023-07-06 14:02:33 +00:00
|
|
|
|
|
|
|
devnull = fopen("/dev/null", "we");
|
2021-12-09 16:48:56 +00:00
|
|
|
if (!devnull)
|
|
|
|
goto exit;
|
|
|
|
|
2023-07-06 14:02:33 +00:00
|
|
|
if (write_binary_policy(&policydb, devnull))
|
|
|
|
abort();
|
2021-12-09 16:48:56 +00:00
|
|
|
|
2023-11-28 18:23:33 +00:00
|
|
|
if (policydb.policy_type == POLICY_KERN) {
|
|
|
|
if (sepol_kernel_policydb_to_conf(devnull, &policydb))
|
|
|
|
abort();
|
2021-12-09 16:48:56 +00:00
|
|
|
|
2023-11-28 18:23:33 +00:00
|
|
|
if (sepol_kernel_policydb_to_cil(devnull, &policydb))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
} else if (policydb.policy_type == POLICY_BASE) {
|
|
|
|
if (link_modules(NULL, &policydb, NULL, 0, VERBOSE))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (policydb_init(&out))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
if (expand_module(NULL, &policydb, &out, VERBOSE, /*check_assertions=*/0))
|
|
|
|
goto exit;
|
|
|
|
|
|
|
|
(void) check_assertions(NULL, &out, out.global->branch_list->avrules);
|
|
|
|
(void) hierarchy_check_constraints(NULL, &out);
|
|
|
|
|
|
|
|
if (write_binary_policy(&out, devnull))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
if (sepol_kernel_policydb_to_conf(devnull, &out))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
if (sepol_kernel_policydb_to_cil(devnull, &out))
|
|
|
|
abort();
|
|
|
|
|
|
|
|
}
|
2021-12-09 16:48:56 +00:00
|
|
|
|
|
|
|
exit:
|
|
|
|
if (devnull != NULL)
|
|
|
|
fclose(devnull);
|
|
|
|
|
2023-11-28 18:23:33 +00:00
|
|
|
policydb_destroy(&out);
|
2021-12-09 16:48:56 +00:00
|
|
|
policydb_destroy(&policydb);
|
|
|
|
sepol_sidtab_destroy(&sidtab);
|
|
|
|
|
|
|
|
/* Non-zero return values are reserved for future use. */
|
|
|
|
return 0;
|
|
|
|
}
|