2018-07-20 21:12:22 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <cil/cil.h>
|
|
|
|
#include <sepol/debug.h>
|
|
|
|
#include <sepol/policydb/policydb.h>
|
|
|
|
#include <sepol/policydb/expand.h>
|
|
|
|
#include <sepol/policydb/link.h>
|
|
|
|
#include <sepol/policydb/services.h>
|
|
|
|
#include <sepol/policydb/avrule_block.h>
|
|
|
|
#include <sepol/policydb/conditional.h>
|
|
|
|
#include <sepol/policydb/constraint.h>
|
|
|
|
|
|
|
|
#include "utils.h"
|
2017-03-18 08:52:38 +00:00
|
|
|
#include "magiskpolicy.h"
|
2017-04-15 11:26:29 +00:00
|
|
|
#include "sepolicy.h"
|
2018-07-20 21:12:22 +00:00
|
|
|
#include "vector.h"
|
2013-06-28 01:42:09 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
policydb_t *policydb = NULL;
|
2018-07-20 21:12:22 +00:00
|
|
|
extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p);
|
2017-04-15 18:29:42 +00:00
|
|
|
|
2017-01-31 16:51:45 +00:00
|
|
|
static void *cmalloc(size_t s) {
|
2017-04-19 18:15:10 +00:00
|
|
|
void *t = calloc(s, 1);
|
2013-06-28 01:42:09 +00:00
|
|
|
if (t == NULL) {
|
|
|
|
fprintf(stderr, "Out of memory\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2017-01-31 16:51:45 +00:00
|
|
|
static int get_attr(char *type, int value) {
|
2017-04-15 18:29:42 +00:00
|
|
|
type_datum_t *attr = hashtab_search(policydb->p_types.table, type);
|
2015-10-25 23:11:37 +00:00
|
|
|
if (!attr)
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-10-25 23:11:37 +00:00
|
|
|
|
|
|
|
if (attr->flavor != TYPE_ATTRIB)
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-10-25 23:11:37 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
return !! ebitmap_get_bit(&policydb->attr_type_map[attr->s.value-1], value-1);
|
2015-10-25 23:11:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 16:51:45 +00:00
|
|
|
static int get_attr_id(char *type) {
|
2017-04-15 18:29:42 +00:00
|
|
|
type_datum_t *attr = hashtab_search(policydb->p_types.table, type);
|
2015-11-01 16:39:06 +00:00
|
|
|
if (!attr)
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-11-01 16:39:06 +00:00
|
|
|
|
|
|
|
if (attr->flavor != TYPE_ATTRIB)
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-11-01 16:39:06 +00:00
|
|
|
|
|
|
|
return attr->s.value;
|
|
|
|
}
|
|
|
|
|
2017-01-31 16:51:45 +00:00
|
|
|
static int set_attr(char *type, int value) {
|
2017-04-15 18:29:42 +00:00
|
|
|
type_datum_t *attr = hashtab_search(policydb->p_types.table, type);
|
2015-06-12 10:03:58 +00:00
|
|
|
if (!attr)
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-06-12 10:03:58 +00:00
|
|
|
|
|
|
|
if (attr->flavor != TYPE_ATTRIB)
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-06-12 10:03:58 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
if(ebitmap_set_bit(&policydb->type_attr_map[value-1], attr->s.value-1, 1))
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2017-04-15 18:29:42 +00:00
|
|
|
if(ebitmap_set_bit(&policydb->attr_type_map[attr->s.value-1], value-1, 1))
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-10-25 17:10:06 +00:00
|
|
|
|
|
|
|
return 0;
|
2015-06-12 10:03:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 20:04:09 +00:00
|
|
|
static int __add_rule(int s, int t, int c, int p, int effect, int not) {
|
2017-01-31 16:51:45 +00:00
|
|
|
avtab_key_t key;
|
2017-04-19 18:15:10 +00:00
|
|
|
avtab_datum_t *av;
|
|
|
|
int new_rule = 0;
|
2017-01-31 16:51:45 +00:00
|
|
|
|
|
|
|
key.source_type = s;
|
|
|
|
key.target_type = t;
|
|
|
|
key.target_class = c;
|
|
|
|
key.specified = effect;
|
|
|
|
|
2017-04-19 18:15:10 +00:00
|
|
|
av = avtab_search(&policydb->te_avtab, &key);
|
2017-01-31 16:51:45 +00:00
|
|
|
if (av == NULL) {
|
|
|
|
av = cmalloc(sizeof(*av));
|
2017-04-19 18:15:10 +00:00
|
|
|
new_rule = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(not) {
|
|
|
|
if (p < 0)
|
|
|
|
av->data = 0U;
|
|
|
|
else
|
|
|
|
av->data &= ~(1U << (p - 1));
|
|
|
|
} else {
|
|
|
|
if (p < 0)
|
|
|
|
av->data = ~0U;
|
|
|
|
else
|
|
|
|
av->data |= 1U << (p - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_rule) {
|
|
|
|
if (avtab_insert(&policydb->te_avtab, &key, av)) {
|
2017-01-31 16:51:45 +00:00
|
|
|
fprintf(stderr, "Error inserting into avtab\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-19 18:15:10 +00:00
|
|
|
free(av);
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum_t *cls, perm_datum_t *perm, int effect, int not) {
|
|
|
|
hashtab_ptr_t cur;
|
2017-02-03 20:24:22 +00:00
|
|
|
int ret = 0;
|
2017-01-31 16:51:45 +00:00
|
|
|
|
|
|
|
if (src == NULL) {
|
2017-04-15 18:29:42 +00:00
|
|
|
hashtab_for_each(policydb->p_types.table, &cur) {
|
2017-02-03 20:24:22 +00:00
|
|
|
src = cur->datum;
|
2017-04-19 18:15:10 +00:00
|
|
|
ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
|
|
|
} else if (tgt == NULL) {
|
2017-04-15 18:29:42 +00:00
|
|
|
hashtab_for_each(policydb->p_types.table, &cur) {
|
2017-02-03 20:24:22 +00:00
|
|
|
tgt = cur->datum;
|
2017-04-19 18:15:10 +00:00
|
|
|
ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
|
|
|
} else if (cls == NULL) {
|
2017-04-15 18:29:42 +00:00
|
|
|
hashtab_for_each(policydb->p_classes.table, &cur) {
|
2017-02-03 20:24:22 +00:00
|
|
|
cls = cur->datum;
|
2017-04-19 20:04:09 +00:00
|
|
|
ret |= __add_rule(src->s.value, tgt->s.value, cls->s.value, -1, effect, not);
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-04-19 20:04:09 +00:00
|
|
|
return __add_rule(src->s.value, tgt->s.value, cls->s.value, perm ? perm->s.value : -1, effect, not);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ioctl_driver(x) (x>>8 & 0xFF)
|
|
|
|
#define ioctl_func(x) (x & 0xFF)
|
|
|
|
|
|
|
|
static int __add_xperm_rule(int s, int t, int c, uint16_t low, uint16_t high, int effect, int not) {
|
|
|
|
avtab_key_t key;
|
|
|
|
avtab_datum_t *av;
|
|
|
|
int new_rule = 0;
|
|
|
|
|
|
|
|
key.source_type = s;
|
|
|
|
key.target_type = t;
|
|
|
|
key.target_class = c;
|
|
|
|
key.specified = effect;
|
|
|
|
|
|
|
|
av = avtab_search(&policydb->te_avtab, &key);
|
|
|
|
if (av == NULL) {
|
|
|
|
av = cmalloc(sizeof(*av));
|
|
|
|
av->xperms = cmalloc(sizeof(avtab_extended_perms_t));
|
|
|
|
new_rule = 1;
|
|
|
|
if (ioctl_driver(low) != ioctl_driver(high)) {
|
|
|
|
av->xperms->specified = AVTAB_XPERMS_IOCTLDRIVER;
|
|
|
|
av->xperms->driver = 0;
|
|
|
|
} else {
|
|
|
|
av->xperms->specified = AVTAB_XPERMS_IOCTLFUNCTION;
|
|
|
|
av->xperms->driver = ioctl_driver(low);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (av->xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
|
|
|
|
for (unsigned i = ioctl_driver(low); i <= ioctl_driver(high); ++i) {
|
|
|
|
if (not)
|
|
|
|
xperm_clear(i, av->xperms->perms);
|
|
|
|
else
|
|
|
|
xperm_set(i, av->xperms->perms);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (unsigned i = ioctl_func(low); i <= ioctl_func(high); ++i) {
|
|
|
|
if (not)
|
|
|
|
xperm_clear(i, av->xperms->perms);
|
|
|
|
else
|
|
|
|
xperm_set(i, av->xperms->perms);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_rule) {
|
|
|
|
if (avtab_insert(&policydb->te_avtab, &key, av)) {
|
|
|
|
fprintf(stderr, "Error inserting into avtab\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
free(av);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_xperm_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum_t *cls,
|
|
|
|
uint16_t low, uint16_t high, int effect, int not) {
|
|
|
|
hashtab_ptr_t cur;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (src == NULL) {
|
|
|
|
hashtab_for_each(policydb->p_types.table, &cur) {
|
|
|
|
src = cur->datum;
|
|
|
|
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
|
|
|
|
}
|
|
|
|
} else if (tgt == NULL) {
|
|
|
|
hashtab_for_each(policydb->p_types.table, &cur) {
|
|
|
|
tgt = cur->datum;
|
|
|
|
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
|
|
|
|
}
|
|
|
|
} else if (cls == NULL) {
|
|
|
|
hashtab_for_each(policydb->p_classes.table, &cur) {
|
|
|
|
cls = cur->datum;
|
|
|
|
ret |= __add_xperm_rule(src->s.value, tgt->s.value, cls->s.value, low, high, effect, not);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return __add_xperm_rule(src->s.value, tgt->s.value, cls->s.value, low, high, effect, not);
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
2017-02-03 20:24:22 +00:00
|
|
|
return ret;
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
int load_policydb(const char *filename) {
|
2017-01-31 16:51:45 +00:00
|
|
|
int fd;
|
|
|
|
struct stat sb;
|
2017-02-04 09:30:34 +00:00
|
|
|
struct policy_file pf;
|
2017-01-31 16:51:45 +00:00
|
|
|
void *map;
|
|
|
|
int ret;
|
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
if (policydb)
|
|
|
|
destroy_policydb();
|
|
|
|
|
|
|
|
policydb = cmalloc(sizeof(*policydb));
|
|
|
|
|
2017-01-31 16:51:45 +00:00
|
|
|
fd = open(filename, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Can't open '%s': %s\n",
|
|
|
|
filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (fstat(fd, &sb) < 0) {
|
|
|
|
fprintf(stderr, "Can't stat '%s': %s\n",
|
|
|
|
filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
map = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
|
|
|
|
fd, 0);
|
|
|
|
if (map == MAP_FAILED) {
|
|
|
|
fprintf(stderr, "Can't mmap '%s': %s\n",
|
|
|
|
filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-04 09:30:34 +00:00
|
|
|
policy_file_init(&pf);
|
|
|
|
pf.type = PF_USE_MEMORY;
|
|
|
|
pf.data = map;
|
|
|
|
pf.len = sb.st_size;
|
2017-04-15 18:29:42 +00:00
|
|
|
if (policydb_init(policydb)) {
|
2017-01-31 16:51:45 +00:00
|
|
|
fprintf(stderr, "policydb_init: Out of memory!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
ret = policydb_read(policydb, &pf, 0);
|
2017-01-31 16:51:45 +00:00
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "error(s) encountered while parsing configuration\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-15 20:11:14 +00:00
|
|
|
munmap(map, sb.st_size);
|
2017-04-15 18:29:42 +00:00
|
|
|
close(fd);
|
|
|
|
|
2017-01-31 16:51:45 +00:00
|
|
|
return 0;
|
2018-07-20 21:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int compile_split_cil() {
|
|
|
|
DIR *dir;
|
|
|
|
struct dirent *entry;
|
|
|
|
char path[128];
|
|
|
|
|
|
|
|
struct cil_db *db = NULL;
|
|
|
|
sepol_policydb_t *pdb = NULL;
|
|
|
|
void *addr;
|
|
|
|
size_t size;
|
|
|
|
|
|
|
|
cil_db_init(&db);
|
|
|
|
cil_set_mls(db, 1);
|
|
|
|
cil_set_multiple_decls(db, 1);
|
|
|
|
cil_set_disable_neverallow(db, 1);
|
|
|
|
cil_set_target_platform(db, SEPOL_TARGET_SELINUX);
|
|
|
|
cil_set_policy_version(db, POLICYDB_VERSION_XPERMS_IOCTL);
|
|
|
|
cil_set_attrs_expand_generated(db, 0);
|
|
|
|
|
|
|
|
// plat
|
|
|
|
mmap_ro(SPLIT_PLAT_CIL, &addr, &size);
|
|
|
|
if (cil_add_file(db, SPLIT_PLAT_CIL, addr, size))
|
|
|
|
return 1;
|
|
|
|
fprintf(stderr, "cil_add[%s]\n", SPLIT_PLAT_CIL);
|
|
|
|
munmap(addr, size);
|
|
|
|
|
|
|
|
// mapping
|
|
|
|
char plat[10];
|
|
|
|
int fd = open(SPLIT_NONPLAT_VER, O_RDONLY | O_CLOEXEC);
|
|
|
|
plat[read(fd, plat, sizeof(plat)) - 1] = '\0';
|
|
|
|
sprintf(path, SPLIT_PLAT_MAPPING, plat);
|
|
|
|
mmap_ro(path, &addr, &size);
|
|
|
|
if (cil_add_file(db, path, addr, size))
|
|
|
|
return 1;
|
|
|
|
fprintf(stderr, "cil_add[%s]\n", path);
|
|
|
|
munmap(addr, size);
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
// nonplat
|
|
|
|
dir = opendir(NONPLAT_POLICY_DIR);
|
|
|
|
while ((entry = readdir(dir))) {
|
|
|
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
|
|
|
continue;
|
|
|
|
if (strend(entry->d_name, ".cil") == 0) {
|
|
|
|
sprintf(path, NONPLAT_POLICY_DIR "%s", entry->d_name);
|
|
|
|
mmap_ro(path, &addr, &size);
|
|
|
|
if (cil_add_file(db, path, addr, size))
|
|
|
|
return 1;
|
|
|
|
fprintf(stderr, "cil_add[%s]\n", path);
|
|
|
|
munmap(addr, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
if (cil_compile(db))
|
|
|
|
return 1;
|
|
|
|
if (cil_build_policydb(db, &pdb))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
cil_db_destroy(&db);
|
|
|
|
policydb = &pdb->p;
|
|
|
|
return 0;
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
int dump_policydb(const char *filename) {
|
2017-02-04 09:30:34 +00:00
|
|
|
int fd, ret;
|
|
|
|
void *data = NULL;
|
|
|
|
size_t len;
|
2017-04-15 18:29:42 +00:00
|
|
|
policydb_to_image(NULL, policydb, &data, &len);
|
2017-02-04 09:30:34 +00:00
|
|
|
if (data == NULL) {
|
2017-04-15 18:29:42 +00:00
|
|
|
fprintf(stderr, "Fail to dump policy image!");
|
2017-02-04 09:30:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-11-28 22:09:35 +00:00
|
|
|
fd = creat(filename, 0644);
|
2017-02-04 09:30:34 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Can't open '%s': %s\n",
|
|
|
|
filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
ret = write(fd, data, len);
|
|
|
|
close(fd);
|
|
|
|
if (ret < 0) {
|
|
|
|
fprintf(stderr, "Could not write policy to %s\n",
|
|
|
|
filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
void destroy_policydb() {
|
|
|
|
policydb_destroy(policydb);
|
|
|
|
free(policydb);
|
|
|
|
policydb = NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:36:15 +00:00
|
|
|
int create_domain(char *d) {
|
2017-04-15 18:29:42 +00:00
|
|
|
symtab_datum_t *src = hashtab_search(policydb->p_types.table, d);
|
2017-04-19 18:15:10 +00:00
|
|
|
if(src) {
|
|
|
|
fprintf(stderr, "Domain %s already exists\n", d);
|
2017-08-27 18:13:36 +00:00
|
|
|
return 0;
|
2017-04-19 18:15:10 +00:00
|
|
|
}
|
2015-06-12 10:03:58 +00:00
|
|
|
|
2017-04-19 18:15:10 +00:00
|
|
|
type_datum_t *typedatum = (type_datum_t *) malloc(sizeof(type_datum_t));
|
|
|
|
type_datum_init(typedatum);
|
|
|
|
typedatum->primary = 1;
|
|
|
|
typedatum->flavor = TYPE_TYPE;
|
2015-06-12 10:03:58 +00:00
|
|
|
|
|
|
|
uint32_t value = 0;
|
2017-07-13 02:12:54 +00:00
|
|
|
symtab_insert(policydb, SYM_TYPES, strdup(d), typedatum, SCOPE_DECL, 1, &value);
|
2017-04-19 18:15:10 +00:00
|
|
|
typedatum->s.value = value;
|
2015-06-12 10:03:58 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
if (ebitmap_set_bit(&policydb->global->branch_list->declared.scope[SYM_TYPES], value - 1, 1)) {
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-06-12 10:03:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 18:15:10 +00:00
|
|
|
policydb->type_attr_map = realloc(policydb->type_attr_map, sizeof(ebitmap_t) * policydb->p_types.nprim);
|
|
|
|
policydb->attr_type_map = realloc(policydb->attr_type_map, sizeof(ebitmap_t) * policydb->p_types.nprim);
|
2017-04-15 18:29:42 +00:00
|
|
|
ebitmap_init(&policydb->type_attr_map[value-1]);
|
|
|
|
ebitmap_init(&policydb->attr_type_map[value-1]);
|
|
|
|
ebitmap_set_bit(&policydb->type_attr_map[value-1], value-1, 1);
|
2015-06-12 10:03:58 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
src = hashtab_search(policydb->p_types.table, d);
|
2015-06-12 10:03:58 +00:00
|
|
|
if(!src)
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-06-12 10:03:58 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
if(policydb_index_decls(NULL, policydb))
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-06-12 10:03:58 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
if(policydb_index_classes(policydb))
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-06-12 10:03:58 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
if(policydb_index_others(NULL, policydb, 0))
|
2017-02-03 22:36:15 +00:00
|
|
|
return 1;
|
2015-06-12 10:03:58 +00:00
|
|
|
|
2017-07-13 02:12:54 +00:00
|
|
|
//Add the domain to all roles
|
|
|
|
for(unsigned i=0; i<policydb->p_roles.nprim; ++i) {
|
|
|
|
//Not sure all those three calls are needed
|
|
|
|
ebitmap_set_bit(&policydb->role_val_to_struct[i]->types.negset, value-1, 0);
|
|
|
|
ebitmap_set_bit(&policydb->role_val_to_struct[i]->types.types, value-1, 1);
|
|
|
|
type_set_expand(&policydb->role_val_to_struct[i]->types, &policydb->role_val_to_struct[i]->cache, policydb, 0);
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:36:15 +00:00
|
|
|
return set_attr("domain", value);
|
2015-06-12 10:03:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-03 20:24:22 +00:00
|
|
|
int set_domain_state(char* s, int state) {
|
|
|
|
type_datum_t *type;
|
2017-02-03 22:36:15 +00:00
|
|
|
hashtab_ptr_t cur;
|
|
|
|
if (s == NULL) {
|
2017-04-15 18:29:42 +00:00
|
|
|
hashtab_for_each(policydb->p_types.table, &cur) {
|
2017-02-03 22:36:15 +00:00
|
|
|
type = cur->datum;
|
2017-04-15 18:29:42 +00:00
|
|
|
if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) {
|
2017-02-03 22:36:15 +00:00
|
|
|
fprintf(stderr, "Could not set bit in permissive map\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-04-15 18:29:42 +00:00
|
|
|
type = hashtab_search(policydb->p_types.table, s);
|
2017-02-03 22:36:15 +00:00
|
|
|
if (type == NULL) {
|
|
|
|
fprintf(stderr, "type %s does not exist\n", s);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) {
|
2017-02-03 22:36:15 +00:00
|
|
|
fprintf(stderr, "Could not set bit in permissive map\n");
|
2013-06-28 01:42:09 +00:00
|
|
|
return 1;
|
2017-02-03 22:36:15 +00:00
|
|
|
}
|
2013-06-28 01:42:09 +00:00
|
|
|
}
|
2017-08-27 18:13:36 +00:00
|
|
|
|
2017-02-03 20:24:22 +00:00
|
|
|
return 0;
|
2013-06-28 01:42:09 +00:00
|
|
|
}
|
2015-11-03 09:52:03 +00:00
|
|
|
|
2017-02-03 22:36:15 +00:00
|
|
|
int add_transition(char *s, char *t, char *c, char *d) {
|
|
|
|
type_datum_t *src, *tgt, *def;
|
2015-10-25 15:20:42 +00:00
|
|
|
class_datum_t *cls;
|
|
|
|
|
|
|
|
avtab_key_t key;
|
2017-04-19 18:15:10 +00:00
|
|
|
avtab_datum_t *av;
|
|
|
|
int new_rule = 0;
|
2015-10-25 15:20:42 +00:00
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
src = hashtab_search(policydb->p_types.table, s);
|
2015-10-25 15:20:42 +00:00
|
|
|
if (src == NULL) {
|
2017-02-03 22:36:15 +00:00
|
|
|
fprintf(stderr, "source type %s does not exist\n", s);
|
2015-10-25 15:20:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
tgt = hashtab_search(policydb->p_types.table, t);
|
2015-10-25 15:20:42 +00:00
|
|
|
if (tgt == NULL) {
|
2017-02-03 22:36:15 +00:00
|
|
|
fprintf(stderr, "target type %s does not exist\n", t);
|
2015-10-25 15:20:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
cls = hashtab_search(policydb->p_classes.table, c);
|
2015-10-25 15:20:42 +00:00
|
|
|
if (cls == NULL) {
|
|
|
|
fprintf(stderr, "class %s does not exist\n", c);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
def = hashtab_search(policydb->p_types.table, d);
|
2017-02-03 22:36:15 +00:00
|
|
|
if (def == NULL) {
|
|
|
|
fprintf(stderr, "default type %s does not exist\n", d);
|
2015-10-25 15:20:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
key.source_type = src->s.value;
|
2017-02-03 22:36:15 +00:00
|
|
|
key.target_type = tgt->s.value;
|
2015-10-25 15:20:42 +00:00
|
|
|
key.target_class = cls->s.value;
|
|
|
|
key.specified = AVTAB_TRANSITION;
|
2017-04-15 18:29:42 +00:00
|
|
|
av = avtab_search(&policydb->te_avtab, &key);
|
2015-10-25 15:20:42 +00:00
|
|
|
if (av == NULL) {
|
2015-11-01 16:32:00 +00:00
|
|
|
av = cmalloc(sizeof(*av));
|
2017-04-19 18:15:10 +00:00
|
|
|
new_rule = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
av->data = def->s.value;
|
|
|
|
|
|
|
|
if (new_rule) {
|
|
|
|
if (avtab_insert(&policydb->te_avtab, &key, av)) {
|
2015-10-25 15:20:42 +00:00
|
|
|
fprintf(stderr, "Error inserting into avtab\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-19 18:15:10 +00:00
|
|
|
free(av);
|
2015-10-25 15:20:42 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-28 01:42:09 +00:00
|
|
|
|
2017-02-03 22:36:15 +00:00
|
|
|
int add_file_transition(char *s, char *t, char *c, char *d, char* filename) {
|
|
|
|
type_datum_t *src, *tgt, *def;
|
2015-11-01 16:38:32 +00:00
|
|
|
class_datum_t *cls;
|
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
src = hashtab_search(policydb->p_types.table, s);
|
2015-11-01 16:38:32 +00:00
|
|
|
if (src == NULL) {
|
2017-02-03 22:36:15 +00:00
|
|
|
fprintf(stderr, "source type %s does not exist\n", s);
|
2015-11-01 16:38:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
tgt = hashtab_search(policydb->p_types.table, t);
|
2015-11-01 16:38:32 +00:00
|
|
|
if (tgt == NULL) {
|
2017-02-03 22:36:15 +00:00
|
|
|
fprintf(stderr, "target type %s does not exist\n", t);
|
2015-11-01 16:38:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
cls = hashtab_search(policydb->p_classes.table, c);
|
2015-11-01 16:38:32 +00:00
|
|
|
if (cls == NULL) {
|
|
|
|
fprintf(stderr, "class %s does not exist\n", c);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
def = hashtab_search(policydb->p_types.table, d);
|
2017-02-03 22:36:15 +00:00
|
|
|
if (def == NULL) {
|
|
|
|
fprintf(stderr, "default type %s does not exist\n", d);
|
2015-11-01 16:38:32 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-04-19 18:15:10 +00:00
|
|
|
filename_trans_t trans_key;
|
|
|
|
trans_key.stype = src->s.value;
|
|
|
|
trans_key.ttype = tgt->s.value;
|
|
|
|
trans_key.tclass = cls->s.value;
|
|
|
|
trans_key.name = filename;
|
2017-04-05 01:13:09 +00:00
|
|
|
|
2017-04-19 18:15:10 +00:00
|
|
|
filename_trans_datum_t *trans_datum;
|
|
|
|
trans_datum = hashtab_search(policydb->p_types.table, (hashtab_key_t) &trans_key);
|
2017-04-05 01:13:09 +00:00
|
|
|
|
2017-04-19 18:15:10 +00:00
|
|
|
if (trans_datum == NULL) {
|
|
|
|
trans_datum = cmalloc(sizeof(*trans_datum));
|
|
|
|
hashtab_insert(policydb->filename_trans, (hashtab_key_t) &trans_key, trans_datum);
|
|
|
|
}
|
2015-11-01 16:38:32 +00:00
|
|
|
|
2017-04-19 18:15:10 +00:00
|
|
|
// Overwrite existing
|
|
|
|
trans_datum->otype = def->s.value;
|
2015-11-01 16:38:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:36:15 +00:00
|
|
|
int add_typeattribute(char *domainS, char *attr) {
|
2015-10-25 17:10:06 +00:00
|
|
|
type_datum_t *domain;
|
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
domain = hashtab_search(policydb->p_types.table, domainS);
|
2015-10-25 17:10:06 +00:00
|
|
|
if (domain == NULL) {
|
|
|
|
fprintf(stderr, "source type %s does not exist\n", domainS);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-02-03 22:36:15 +00:00
|
|
|
set_attr(attr, domain->s.value);
|
2015-11-01 16:39:06 +00:00
|
|
|
|
2017-02-03 22:36:15 +00:00
|
|
|
int typeId = get_attr_id(attr);
|
2015-11-01 16:39:06 +00:00
|
|
|
//Now let's update all constraints!
|
|
|
|
//(kernel doesn't support (yet?) type_names rules)
|
2017-04-15 18:29:42 +00:00
|
|
|
for(int i=0; i<policydb->p_classes.nprim; ++i) {
|
|
|
|
class_datum_t *cl = policydb->class_val_to_struct[i];
|
2015-11-01 16:39:06 +00:00
|
|
|
for(constraint_node_t *n = cl->constraints; n ; n=n->next) {
|
|
|
|
for(constraint_expr_t *e = n->expr; e; e=e->next) {
|
|
|
|
if(e->expr_type == CEXPR_NAMES) {
|
|
|
|
if(ebitmap_get_bit(&e->type_names->types, typeId-1)) {
|
|
|
|
ebitmap_set_bit(&e->names, domain->s.value-1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-25 23:11:37 +00:00
|
|
|
return 0;
|
2015-10-25 17:10:06 +00:00
|
|
|
}
|
|
|
|
|
2017-01-31 16:51:45 +00:00
|
|
|
int add_rule(char *s, char *t, char *c, char *p, int effect, int not) {
|
2016-09-12 22:34:13 +00:00
|
|
|
type_datum_t *src = NULL, *tgt = NULL;
|
|
|
|
class_datum_t *cls = NULL;
|
|
|
|
perm_datum_t *perm = NULL;
|
|
|
|
|
|
|
|
if (s) {
|
2017-04-15 18:29:42 +00:00
|
|
|
src = hashtab_search(policydb->p_types.table, s);
|
2016-09-12 22:34:13 +00:00
|
|
|
if (src == NULL) {
|
|
|
|
fprintf(stderr, "source type %s does not exist\n", s);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t) {
|
2017-04-15 18:29:42 +00:00
|
|
|
tgt = hashtab_search(policydb->p_types.table, t);
|
2016-09-12 22:34:13 +00:00
|
|
|
if (tgt == NULL) {
|
|
|
|
fprintf(stderr, "target type %s does not exist\n", t);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c) {
|
2017-04-15 18:29:42 +00:00
|
|
|
cls = hashtab_search(policydb->p_classes.table, c);
|
2016-09-12 22:34:13 +00:00
|
|
|
if (cls == NULL) {
|
|
|
|
fprintf(stderr, "class %s does not exist\n", c);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
if (c == NULL) {
|
|
|
|
fprintf(stderr, "No class is specified, cannot add perm [%s] \n", p);
|
|
|
|
return 1;
|
|
|
|
}
|
2017-08-27 18:13:36 +00:00
|
|
|
|
2016-09-12 22:34:13 +00:00
|
|
|
if (cls != NULL) {
|
|
|
|
perm = hashtab_search(cls->permissions.table, p);
|
|
|
|
if (perm == NULL && cls->comdatum != NULL) {
|
|
|
|
perm = hashtab_search(cls->comdatum->permissions.table, p);
|
|
|
|
}
|
|
|
|
if (perm == NULL) {
|
|
|
|
fprintf(stderr, "perm %s does not exist in class %s\n", p, c);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-31 16:51:45 +00:00
|
|
|
return add_rule_auto(src, tgt, cls, perm, effect, not);
|
2016-09-12 22:34:13 +00:00
|
|
|
}
|
2017-04-19 20:04:09 +00:00
|
|
|
|
|
|
|
int add_xperm_rule(char *s, char *t, char *c, char *range, int effect, int not) {
|
|
|
|
type_datum_t *src = NULL, *tgt = NULL;
|
|
|
|
class_datum_t *cls = NULL;
|
|
|
|
|
|
|
|
if (s) {
|
|
|
|
src = hashtab_search(policydb->p_types.table, s);
|
|
|
|
if (src == NULL) {
|
|
|
|
fprintf(stderr, "source type %s does not exist\n", s);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t) {
|
|
|
|
tgt = hashtab_search(policydb->p_types.table, t);
|
|
|
|
if (tgt == NULL) {
|
|
|
|
fprintf(stderr, "target type %s does not exist\n", t);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c) {
|
|
|
|
cls = hashtab_search(policydb->p_classes.table, c);
|
|
|
|
if (cls == NULL) {
|
|
|
|
fprintf(stderr, "class %s does not exist\n", c);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t low, high;
|
|
|
|
|
|
|
|
if (range) {
|
|
|
|
if (strchr(range, '-')){
|
|
|
|
sscanf(range, "%hx-%hx", &low, &high);
|
|
|
|
} else {
|
|
|
|
sscanf(range, "%hx", &low);
|
|
|
|
high = low;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
low = 0;
|
|
|
|
high = 0xFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
return add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
|
|
|
|
}
|