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-11-08 11:43:11 +00:00
|
|
|
#include "logging.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
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
static int get_attr(const 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
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
return ebitmap_get_bit(&policydb->attr_type_map[attr->s.value - 1], value - 1) != 0;
|
2015-10-25 23:11:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
static int get_attr_id(const 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;
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
static int set_attr(const 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
|
|
|
}
|
|
|
|
|
2018-11-29 10:42:08 +00:00
|
|
|
static void check_avtab_node(avtab_ptr_t node) {
|
|
|
|
int redundant = 0;
|
|
|
|
if (node->key.specified == AVTAB_AUDITDENY)
|
|
|
|
redundant = node->datum.data == ~0U;
|
|
|
|
else if (node->key.specified & AVTAB_XPERMS)
|
|
|
|
redundant = node->datum.xperms == NULL;
|
|
|
|
else
|
|
|
|
redundant = node->datum.data == 0U;
|
|
|
|
if (redundant)
|
|
|
|
avtab_remove_node(&policydb->te_avtab, node);
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
static avtab_ptr_t get_avtab_node(avtab_key_t *key, avtab_extended_perms_t *xperms) {
|
|
|
|
avtab_ptr_t node;
|
|
|
|
avtab_datum_t avdatum;
|
|
|
|
int match = 0;
|
|
|
|
|
|
|
|
/* AVTAB_XPERMS entries are not necessarily unique */
|
|
|
|
if (key->specified & AVTAB_XPERMS) {
|
|
|
|
node = avtab_search_node(&policydb->te_avtab, key);
|
|
|
|
while (node) {
|
|
|
|
if ((node->datum.xperms->specified == xperms->specified) &&
|
|
|
|
(node->datum.xperms->driver == xperms->driver)) {
|
|
|
|
match = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = avtab_search_node_next(node, key->specified);
|
|
|
|
}
|
|
|
|
if (!match)
|
|
|
|
node = NULL;
|
|
|
|
} else {
|
|
|
|
node = avtab_search_node(&policydb->te_avtab, key);
|
|
|
|
}
|
2017-01-31 16:51:45 +00:00
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
if (!node) {
|
|
|
|
memset(&avdatum, 0, sizeof avdatum);
|
|
|
|
/*
|
|
|
|
* AUDITDENY, aka DONTAUDIT, are &= assigned, versus |= for
|
|
|
|
* others. Initialize the data accordingly.
|
|
|
|
*/
|
|
|
|
avdatum.data = key->specified == AVTAB_AUDITDENY ? ~0U : 0U;
|
|
|
|
/* this is used to get the node - insertion is actually unique */
|
|
|
|
node = avtab_insert_nonunique(&policydb->te_avtab, key, &avdatum);
|
2017-04-19 18:15:10 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int add_avrule(avtab_key_t *key, int p, int not) {
|
2018-11-29 10:42:08 +00:00
|
|
|
avtab_ptr_t node = get_avtab_node(key, NULL);
|
2018-11-29 11:42:04 +00:00
|
|
|
// Support DONTAUDIT (AUDITDENY is inverted)
|
|
|
|
if (AVTAB_AUDITDENY == node->key.specified == !not) {
|
2017-04-19 18:15:10 +00:00
|
|
|
if (p < 0)
|
2018-11-29 10:42:08 +00:00
|
|
|
node->datum.data = 0U;
|
2017-04-19 18:15:10 +00:00
|
|
|
else
|
2018-11-29 10:42:08 +00:00
|
|
|
node->datum.data &= ~(1U << (p - 1));
|
2017-04-19 18:15:10 +00:00
|
|
|
} else {
|
|
|
|
if (p < 0)
|
2018-11-29 10:42:08 +00:00
|
|
|
node->datum.data = ~0U;
|
2017-04-19 18:15:10 +00:00
|
|
|
else
|
2018-11-29 10:42:08 +00:00
|
|
|
node->datum.data |= 1U << (p - 1);
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
2018-11-29 10:42:08 +00:00
|
|
|
check_avtab_node(node);
|
2017-01-31 16:51:45 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
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) {
|
|
|
|
avtab_key_t key;
|
2017-01-31 16:51:45 +00:00
|
|
|
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;
|
2018-11-29 08:46:29 +00:00
|
|
|
ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
|
2017-01-31 16:51:45 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-29 08:46:29 +00:00
|
|
|
key.source_type = src->s.value;
|
|
|
|
key.target_type = tgt->s.value;
|
|
|
|
key.target_class = cls->s.value;
|
|
|
|
key.specified = effect;
|
|
|
|
return add_avrule(&key, perm ? perm->s.value : -1, not);
|
2017-04-19 20:04:09 +00:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ioctl_driver(x) (x>>8 & 0xFF)
|
|
|
|
#define ioctl_func(x) (x & 0xFF)
|
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
static int add_avxrule(avtab_key_t *key, uint16_t low, uint16_t high, int not) {
|
|
|
|
avtab_datum_t *datum;
|
|
|
|
avtab_extended_perms_t xperms;
|
2017-04-19 20:04:09 +00:00
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
memset(&xperms, 0, sizeof(xperms));
|
|
|
|
if (ioctl_driver(low) != ioctl_driver(high)) {
|
|
|
|
xperms.specified = AVTAB_XPERMS_IOCTLDRIVER;
|
|
|
|
xperms.driver = 0;
|
|
|
|
} else {
|
|
|
|
xperms.specified = AVTAB_XPERMS_IOCTLFUNCTION;
|
|
|
|
xperms.driver = ioctl_driver(low);
|
2017-04-19 20:04:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
if (xperms.specified == AVTAB_XPERMS_IOCTLDRIVER) {
|
|
|
|
for (int i = ioctl_driver(low); i <= ioctl_driver(high); ++i) {
|
2017-04-19 20:04:09 +00:00
|
|
|
if (not)
|
2018-11-29 08:46:29 +00:00
|
|
|
xperm_clear(i, xperms.perms);
|
2017-04-19 20:04:09 +00:00
|
|
|
else
|
2018-11-29 08:46:29 +00:00
|
|
|
xperm_set(i, xperms.perms);
|
2017-04-19 20:04:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-29 08:46:29 +00:00
|
|
|
for (int i = ioctl_func(low); i <= ioctl_func(high); ++i) {
|
2017-04-19 20:04:09 +00:00
|
|
|
if (not)
|
2018-11-29 08:46:29 +00:00
|
|
|
xperm_clear(i, xperms.perms);
|
2017-04-19 20:04:09 +00:00
|
|
|
else
|
2018-11-29 08:46:29 +00:00
|
|
|
xperm_set(i, xperms.perms);
|
2017-04-19 20:04:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
datum = &get_avtab_node(key, &xperms)->datum;
|
|
|
|
|
|
|
|
if (datum->xperms == NULL)
|
|
|
|
datum->xperms = xmalloc(sizeof(xperms));
|
2017-04-19 20:04:09 +00:00
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
memcpy(datum->xperms, &xperms, sizeof(xperms));
|
2017-04-19 20:04:09 +00:00
|
|
|
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) {
|
2018-11-29 08:46:29 +00:00
|
|
|
avtab_key_t key;
|
2017-04-19 20:04:09 +00:00
|
|
|
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;
|
2018-11-29 08:46:29 +00:00
|
|
|
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
|
2017-04-19 20:04:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-29 08:46:29 +00:00
|
|
|
key.source_type = src->s.value;
|
|
|
|
key.target_type = tgt->s.value;
|
|
|
|
key.target_class = cls->s.value;
|
|
|
|
key.specified = effect;
|
|
|
|
return add_avxrule(&key, low, high, 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-02-04 09:30:34 +00:00
|
|
|
struct policy_file pf;
|
2017-01-31 16:51:45 +00:00
|
|
|
void *map;
|
2018-11-08 11:43:11 +00:00
|
|
|
size_t size;
|
2017-01-31 16:51:45 +00:00
|
|
|
int ret;
|
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
if (policydb)
|
|
|
|
destroy_policydb();
|
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
policydb = xcalloc(sizeof(*policydb), 1);
|
2017-04-15 18:29:42 +00:00
|
|
|
|
2018-11-08 11:43:11 +00:00
|
|
|
mmap_ro(filename, &map, &size);
|
2017-01-31 16:51:45 +00:00
|
|
|
|
2017-02-04 09:30:34 +00:00
|
|
|
policy_file_init(&pf);
|
|
|
|
pf.type = PF_USE_MEMORY;
|
|
|
|
pf.data = map;
|
2018-11-08 11:43:11 +00:00
|
|
|
pf.len = size;
|
2017-04-15 18:29:42 +00:00
|
|
|
if (policydb_init(policydb)) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGE("policydb_init: Out of memory!\n");
|
2017-01-31 16:51:45 +00:00
|
|
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGE("error(s) encountered while parsing configuration\n");
|
2017-01-31 16:51:45 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-11-08 11:43:11 +00:00
|
|
|
munmap(map, size);
|
2017-04-15 18:29:42 +00:00
|
|
|
|
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;
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGD("cil_add[%s]\n", SPLIT_PLAT_CIL);
|
2018-07-20 21:12:22 +00:00
|
|
|
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;
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGD("cil_add[%s]\n", path);
|
2018-07-20 21:12:22 +00:00
|
|
|
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;
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGD("cil_add[%s]\n", path);
|
2018-07-20 21:12:22 +00:00
|
|
|
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) {
|
2018-11-29 10:42:08 +00:00
|
|
|
LOGE("Fail to dump policy image!\n");
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGE("Can't open '%s': %s\n", filename, strerror(errno));
|
2017-02-04 09:30:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2018-11-08 11:43:11 +00:00
|
|
|
ret = xwrite(fd, data, len);
|
2017-02-04 09:30:34 +00:00
|
|
|
close(fd);
|
2018-11-08 11:43:11 +00:00
|
|
|
if (ret < 0)
|
2017-02-04 09:30:34 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-15 18:29:42 +00:00
|
|
|
void destroy_policydb() {
|
|
|
|
policydb_destroy(policydb);
|
|
|
|
free(policydb);
|
|
|
|
policydb = NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
int create_domain(const 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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("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
|
|
|
}
|
|
|
|
|
2018-11-29 11:28:37 +00:00
|
|
|
void for_each_avtab_node(void (*callback)(avtab_ptr_t)) {
|
|
|
|
avtab_ptr_t cur, next;
|
|
|
|
for (int i = 0; i < policydb->te_avtab.nslot; ++i) {
|
|
|
|
for (cur = policydb->te_avtab.htable[i]; cur; cur = next) {
|
|
|
|
// cur could be removed after callback
|
|
|
|
next = cur->next;
|
|
|
|
callback(cur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
int set_domain_state(const char *s, int state) {
|
2017-02-03 20:24:22 +00:00
|
|
|
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)) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("Could not set bit in permissive map\n");
|
2017-02-03 22:36:15 +00:00
|
|
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("type %s does not exist\n", s);
|
|
|
|
return 1;
|
2017-02-03 22:36:15 +00:00
|
|
|
}
|
2017-04-15 18:29:42 +00:00
|
|
|
if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("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
|
|
|
|
2018-11-29 08:46:29 +00:00
|
|
|
int sepol_nametrans(const char *s, const char *t, const char *c, const char *d, const char *o) {
|
2017-02-03 22:36:15 +00:00
|
|
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("class %s does not exist\n", c);
|
2015-11-01 16:38:32 +00:00
|
|
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("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;
|
2018-11-29 08:46:29 +00:00
|
|
|
trans_key.name = (char *) o;
|
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) {
|
2018-11-29 08:46:29 +00:00
|
|
|
trans_datum = xcalloc(sizeof(*trans_datum), 1);
|
2017-04-19 18:15:10 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
int add_typeattribute(const char *domainS, const 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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("source type %s does not exist\n", domainS);
|
2015-10-25 17:10:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
int add_rule(const char *s, const char *t, const char *c, const char *p, int effect, int n) {
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("source type %s does not exist\n", s);
|
2016-09-12 22:34:13 +00:00
|
|
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("target type %s does not exist\n", t);
|
2016-09-12 22:34:13 +00:00
|
|
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("class %s does not exist\n", c);
|
2016-09-12 22:34:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
if (c == NULL) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("No class is specified, cannot add perm [%s] \n", p);
|
2016-09-12 22:34:13 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2017-08-27 18:13:36 +00:00
|
|
|
|
2018-11-08 11:43:11 +00:00
|
|
|
perm = hashtab_search(cls->permissions.table, p);
|
|
|
|
if (perm == NULL && cls->comdatum != NULL) {
|
|
|
|
perm = hashtab_search(cls->comdatum->permissions.table, p);
|
|
|
|
}
|
|
|
|
if (perm == NULL) {
|
|
|
|
LOGW("perm %s does not exist in class %s\n", p, c);
|
|
|
|
return 1;
|
2016-09-12 22:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-08 09:20:16 +00:00
|
|
|
return add_rule_auto(src, tgt, cls, perm, effect, n);
|
2016-09-12 22:34:13 +00:00
|
|
|
}
|
2017-04-19 20:04:09 +00:00
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
int add_xperm_rule(const char *s, const char *t, const char *c, const char *range, int effect,
|
|
|
|
int n) {
|
2017-04-19 20:04:09 +00:00
|
|
|
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) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("source type %s does not exist\n", s);
|
2017-04-19 20:04:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t) {
|
|
|
|
tgt = hashtab_search(policydb->p_types.table, t);
|
|
|
|
if (tgt == NULL) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("target type %s does not exist\n", t);
|
2017-04-19 20:04:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c) {
|
|
|
|
cls = hashtab_search(policydb->p_classes.table, c);
|
|
|
|
if (cls == NULL) {
|
2018-11-08 11:43:11 +00:00
|
|
|
LOGW("class %s does not exist\n", c);
|
2017-04-19 20:04:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-08 09:20:16 +00:00
|
|
|
return add_xperm_rule_auto(src, tgt, cls, low, high, effect, n);
|
2017-04-19 20:04:09 +00:00
|
|
|
}
|
2018-11-29 08:46:29 +00:00
|
|
|
|
|
|
|
int add_type_rule(const char *s, const char *t, const char *c, const char *d, int effect) {
|
|
|
|
type_datum_t *src, *tgt, *def;
|
|
|
|
class_datum_t *cls;
|
|
|
|
|
|
|
|
src = hashtab_search(policydb->p_types.table, s);
|
|
|
|
if (src == NULL) {
|
|
|
|
LOGW("source type %s does not exist\n", s);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
tgt = hashtab_search(policydb->p_types.table, t);
|
|
|
|
if (tgt == NULL) {
|
|
|
|
LOGW("target type %s does not exist\n", t);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
cls = hashtab_search(policydb->p_classes.table, c);
|
|
|
|
if (cls == NULL) {
|
|
|
|
LOGW("class %s does not exist\n", c);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
def = hashtab_search(policydb->p_types.table, d);
|
|
|
|
if (def == NULL) {
|
|
|
|
LOGW("default type %s does not exist\n", d);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-11-29 10:42:08 +00:00
|
|
|
avtab_key_t key;
|
2018-11-29 08:46:29 +00:00
|
|
|
key.source_type = src->s.value;
|
|
|
|
key.target_type = tgt->s.value;
|
|
|
|
key.target_class = cls->s.value;
|
|
|
|
key.specified = effect;
|
|
|
|
|
2018-11-29 10:42:08 +00:00
|
|
|
avtab_ptr_t node = get_avtab_node(&key, NULL);
|
|
|
|
node->datum.data = def->s.value;
|
2018-11-29 08:46:29 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|