28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/string.h>
32 #include <linux/errno.h>
33 #include <linux/audit.h>
45 static const char *symtab_name[
SYM_NUM] = {
57 static unsigned int symtab_sizes[
SYM_NUM] = {
153 for (i = 0; i <
ARRAY_SIZE(policydb_compat); i++) {
154 if (policydb_compat[i].version == version) {
155 info = &policydb_compat[
i];
165 static int roles_init(
struct policydb *
p)
177 role->
value = ++p->p_roles.nprim;
197 static u32 filenametr_hash(
struct hashtab *
h,
const void *
k)
201 unsigned int byte_num;
207 while ((focus = ft->
name[byte_num++]))
208 hash = partial_name_hash(focus, hash);
209 return hash & (h->
size - 1);
212 static int filenametr_cmp(
struct hashtab *h,
const void *
k1,
const void *k2)
234 static u32 rangetr_hash(
struct hashtab *h,
const void *k)
241 static int rangetr_cmp(
struct hashtab *h,
const void *k1,
const void *k2)
262 static int policydb_init(
struct policydb *p)
268 for (i = 0; i <
SYM_NUM; i++) {
317 static int common_index(
void *key,
void *datum,
void *datap)
325 if (!comdatum->
value || comdatum->
value > p->p_commons.nprim)
335 static int class_index(
void *key,
void *datum,
void *datap)
343 if (!cladatum->
value || cladatum->
value > p->p_classes.nprim)
353 static int role_index(
void *key,
void *datum,
void *datap)
362 || role->
value > p->p_roles.nprim
363 || role->
bounds > p->p_roles.nprim)
374 static int type_index(
void *key,
void *datum,
void *datap)
385 || typdatum->
value > p->p_types.nprim
386 || typdatum->
bounds > p->p_types.nprim)
402 static int user_index(
void *key,
void *datum,
void *datap)
411 || usrdatum->
value > p->p_users.nprim
412 || usrdatum->
bounds > p->p_users.nprim)
423 static int sens_index(
void *key,
void *datum,
void *datap)
433 if (!levdatum->
level->sens ||
434 levdatum->
level->sens > p->p_levels.nprim)
445 static int cat_index(
void *key,
void *datum,
void *datap)
455 if (!catdatum->
value || catdatum->
value > p->p_cats.nprim)
466 static int (*index_f[SYM_NUM]) (
void *
key,
void *datum,
void *datap) =
479 static void hash_eval(
struct hashtab *h,
const char *hash_name)
485 "longest chain length %d\n", hash_name, h->
nel,
486 info.slots_used, h->
size, info.max_chain_len);
489 static void symtab_hash_eval(
struct symtab *
s)
494 hash_eval(s[i].
table, symtab_name[i]);
498 static inline void hash_eval(
struct hashtab *h,
char *hash_name)
509 static int policydb_index(
struct policydb *p)
514 p->p_users.nprim, p->p_roles.nprim, p->p_types.nprim, p->p_bools.nprim);
516 printk(
", %d sens, %d cats", p->p_levels.nprim,
521 p->p_classes.nprim, p->
te_avtab.nel);
525 symtab_hash_eval(p->
symtab);
566 for (i = 0; i <
SYM_NUM; i++) {
595 static int perm_destroy(
void *key,
void *datum,
void *p)
602 static int common_destroy(
void *key,
void *datum,
void *p)
616 static int cls_destroy(
void *key,
void *datum,
void *p)
629 e = constraint->
expr;
637 constraint = constraint->
next;
643 e = constraint->
expr;
651 constraint = constraint->
next;
661 static int role_destroy(
void *key,
void *datum,
void *p)
675 static int type_destroy(
void *key,
void *datum,
void *p)
682 static int user_destroy(
void *key,
void *datum,
void *p)
698 static int sens_destroy(
void *key,
void *datum,
void *p)
712 static int cat_destroy(
void *key,
void *datum,
void *p)
719 static int (*destroy_f[SYM_NUM]) (
void *
key,
void *datum,
void *datap) =
731 static int filenametr_destroy(
void *key,
void *datum,
void *p)
741 static int range_tr_destroy(
void *key,
void *datum,
void *p)
752 static void ocontext_destroy(
struct ocontext *
c,
int i)
757 context_destroy(&c->
context[0]);
758 context_destroy(&c->
context[1]);
776 for (i = 0; i <
SYM_NUM; i++) {
782 for (i = 0; i <
SYM_NUM; i++) {
801 ocontext_destroy(ctmp, i);
845 for (i = 0; i < p->p_types.nprim; i++) {
879 for (c = head;
c; c = c->
next) {
901 if (!
class ||
class > p->p_classes.nprim)
908 if (!role || role > p->p_roles.nprim)
915 if (!type || type > p->p_types.nprim)
929 if (!c->
role || c->
role > p->p_roles.nprim)
932 if (!c->
user || c->
user > p->p_users.nprim)
935 if (!c->
type || c->
type > p->p_types.nprim)
969 static int mls_read_range_helper(
struct mls_range *
r,
void *
fp)
975 rc = next_entry(buf, fp,
sizeof(
u32));
986 rc = next_entry(buf, fp,
sizeof(
u32) * items);
1006 printk(
KERN_ERR "SELinux: mls: error reading high categories\n");
1028 static int context_read_and_validate(
struct context *c,
1035 rc = next_entry(buf, fp,
sizeof buf);
1044 rc = mls_read_range_helper(&c->
range, fp);
1046 printk(
KERN_ERR "SELinux: error reading MLS range of context\n");
1077 perdatum = kzalloc(
sizeof(*perdatum),
GFP_KERNEL);
1081 rc = next_entry(buf, fp,
sizeof buf);
1093 rc = next_entry(key, fp, len);
1104 perm_destroy(key, perdatum,
NULL);
1108 static int common_read(
struct policydb *p,
struct hashtab *h,
void *fp)
1117 comdatum = kzalloc(
sizeof(*comdatum),
GFP_KERNEL);
1121 rc = next_entry(buf, fp,
sizeof buf);
1139 rc = next_entry(key, fp, len);
1144 for (i = 0; i < nel; i++) {
1145 rc = perm_read(p, comdatum->
permissions.table, fp);
1155 common_destroy(key, comdatum,
NULL);
1159 static int read_cons_helper(
struct constraint_node **nodep,
int ncons,
1160 int allowxtarget,
void *fp)
1169 for (i = 0; i < ncons; i++) {
1179 rc = next_entry(buf, fp, (
sizeof(
u32) * 2));
1186 for (j = 0; j < nexpr; j++) {
1196 rc = next_entry(buf, fp, (
sizeof(
u32) * 3));
1242 static int class_read(
struct policydb *p,
struct hashtab *h,
void *fp)
1247 u32 len, len2, ncons, nel;
1251 cladatum = kzalloc(
sizeof(*cladatum),
GFP_KERNEL);
1255 rc = next_entry(buf, fp,
sizeof(
u32)*6);
1276 rc = next_entry(key, fp, len);
1286 rc = next_entry(cladatum->
comkey, fp, len2);
1289 cladatum->
comkey[len2] =
'\0';
1298 for (i = 0; i < nel; i++) {
1299 rc = perm_read(p, cladatum->
permissions.table, fp);
1304 rc = read_cons_helper(&cladatum->
constraints, ncons, 0, fp);
1310 rc = next_entry(buf, fp,
sizeof(
u32));
1314 rc = read_cons_helper(&cladatum->
validatetrans, ncons, 1, fp);
1320 rc = next_entry(buf, fp,
sizeof(
u32) * 3);
1330 rc = next_entry(buf, fp,
sizeof(
u32) * 1);
1342 cls_destroy(key, cladatum,
NULL);
1350 int rc, to_read = 2;
1362 rc = next_entry(buf, fp,
sizeof(buf[0]) * to_read);
1376 rc = next_entry(key, fp, len);
1405 role_destroy(key, role,
NULL);
1413 int rc, to_read = 3;
1418 typdatum = kzalloc(
sizeof(*typdatum),
GFP_KERNEL);
1425 rc = next_entry(buf, fp,
sizeof(buf[0]) * to_read);
1448 rc = next_entry(key, fp, len);
1458 type_destroy(key, typdatum,
NULL);
1467 static int mls_read_level(
struct mls_level *
lp,
void *fp)
1472 memset(lp, 0,
sizeof(*lp));
1474 rc = next_entry(buf, fp,
sizeof buf);
1483 printk(
KERN_ERR "SELinux: mls: error reading level categories\n");
1493 int rc, to_read = 2;
1498 usrdatum = kzalloc(
sizeof(*usrdatum),
GFP_KERNEL);
1505 rc = next_entry(buf, fp,
sizeof(buf[0]) * to_read);
1518 rc = next_entry(key, fp, len);
1528 rc = mls_read_range_helper(&usrdatum->
range, fp);
1531 rc = mls_read_level(&usrdatum->
dfltlevel, fp);
1554 levdatum = kzalloc(
sizeof(*levdatum),
GFP_ATOMIC);
1558 rc = next_entry(buf, fp,
sizeof buf);
1569 rc = next_entry(key, fp, len);
1576 if (!levdatum->
level)
1579 rc = mls_read_level(levdatum->
level, fp);
1588 sens_destroy(key, levdatum,
NULL);
1601 catdatum = kzalloc(
sizeof(*catdatum),
GFP_ATOMIC);
1605 rc = next_entry(buf, fp,
sizeof buf);
1617 rc = next_entry(key, fp, len);
1627 cat_destroy(key, catdatum,
NULL);
1643 static int user_bounds_sanity_check(
void *key,
void *datum,
void *datap)
1649 upper = user = datum;
1656 "too deep or looped boundary",
1667 "SELinux: boundary violated policy: "
1668 "user=%s role=%s bounds=%s\n",
1680 static int role_bounds_sanity_check(
void *key,
void *datum,
void *datap)
1686 upper = role = datum;
1693 "too deep or looped bounds\n",
1704 "SELinux: boundary violated policy: "
1705 "role=%s type=%s bounds=%s\n",
1717 static int type_bounds_sanity_check(
void *key,
void *datum,
void *datap)
1727 "too deep or looped boundary\n",
1738 "bounded by attribute %s",
1748 static int policydb_bounds_sanity_check(
struct policydb *p)
1756 user_bounds_sanity_check, p);
1761 role_bounds_sanity_check, p);
1766 type_bounds_sanity_check, p);
1781 return cladatum->
value;
1790 if (!tclass || tclass > p->p_classes.nprim)
1804 return 1
U << (perdatum->
value-1);
1807 static int range_read(
struct policydb *p,
void *fp)
1818 rc = next_entry(buf, fp,
sizeof(
u32));
1823 for (i = 0; i < nel; i++) {
1829 rc = next_entry(buf, fp, (
sizeof(
u32) * 2));
1836 rc = next_entry(buf, fp,
sizeof(
u32));
1854 rc = mls_read_range_helper(r, fp);
1879 static int filename_trans_read(
struct policydb *p,
void *fp)
1891 rc = next_entry(buf, fp,
sizeof(
u32));
1896 for (i = 0; i < nel; i++) {
1912 rc = next_entry(buf, fp,
sizeof(
u32));
1925 rc = next_entry(name, fp, len);
1930 rc = next_entry(buf, fp,
sizeof(
u32) * 4);
1956 static int genfs_read(
struct policydb *p,
void *fp)
1959 u32 nel, nel2, len, len2;
1964 struct genfs *newgenfs =
NULL;
1966 rc = next_entry(buf, fp,
sizeof(
u32));
1971 for (i = 0; i < nel; i++) {
1972 rc = next_entry(buf, fp,
sizeof(
u32));
1978 newgenfs = kzalloc(
sizeof(*newgenfs),
GFP_KERNEL);
1987 rc = next_entry(newgenfs->
fstype, fp, len);
1991 newgenfs->
fstype[len] = 0;
1993 for (genfs_p =
NULL, genfs = p->
genfs; genfs;
1994 genfs_p = genfs, genfs = genfs->
next) {
2004 newgenfs->
next = genfs;
2006 genfs_p->
next = newgenfs;
2008 p->
genfs = newgenfs;
2012 rc = next_entry(buf, fp,
sizeof(
u32));
2017 for (j = 0; j < nel2; j++) {
2018 rc = next_entry(buf, fp,
sizeof(
u32));
2033 rc = next_entry(newc->
u.
name, fp, len);
2036 newc->
u.
name[len] = 0;
2038 rc = next_entry(buf, fp,
sizeof(
u32));
2043 rc = context_read_and_validate(&newc->
context[0], p, fp);
2048 l = c, c = c->
next) {
2090 for (i = 0; i < info->
ocon_num; i++) {
2091 rc = next_entry(buf, fp,
sizeof(
u32));
2097 for (j = 0; j < nel; j++) {
2110 rc = next_entry(buf, fp,
sizeof(
u32));
2115 rc = context_read_and_validate(&c->
context[0], p, fp);
2121 rc = next_entry(buf, fp,
sizeof(
u32));
2131 rc = next_entry(c->
u.
name, fp, len);
2136 rc = context_read_and_validate(&c->
context[0], p, fp);
2139 rc = context_read_and_validate(&c->
context[1], p, fp);
2144 rc = next_entry(buf, fp,
sizeof(
u32)*3);
2150 rc = context_read_and_validate(&c->
context[0], p, fp);
2155 rc = next_entry(nodebuf, fp,
sizeof(
u32) * 2);
2158 c->
u.
node.addr = nodebuf[0];
2159 c->
u.
node.mask = nodebuf[1];
2160 rc = context_read_and_validate(&c->
context[0], p, fp);
2165 rc = next_entry(buf, fp,
sizeof(
u32)*2);
2180 rc = next_entry(c->
u.
name, fp, len);
2184 rc = context_read_and_validate(&c->
context[0], p, fp);
2191 rc = next_entry(nodebuf, fp,
sizeof(
u32) * 8);
2194 for (k = 0; k < 4; k++)
2195 c->
u.
node6.addr[k] = nodebuf[k];
2196 for (k = 0; k < 4; k++)
2197 c->
u.
node6.mask[k] = nodebuf[k+4];
2198 rc = context_read_and_validate(&c->
context[0], p, fp);
2221 u32 len, nprim, nel;
2226 rc = policydb_init(p);
2231 rc = next_entry(buf, fp,
sizeof(
u32) * 2);
2238 "not match expected magic number 0x%x\n",
2247 "match expected length %Zu\n",
2254 if (!policydb_str) {
2255 printk(
KERN_ERR "SELinux: unable to allocate memory for policydb "
2256 "string of length %d\n", len);
2260 rc = next_entry(policydb_str, fp, len);
2262 printk(
KERN_ERR "SELinux: truncated policydb string identifier\n");
2263 kfree(policydb_str);
2268 policydb_str[len] =
'\0';
2272 kfree(policydb_str);
2276 kfree(policydb_str);
2277 policydb_str =
NULL;
2280 rc = next_entry(buf, fp,
sizeof(
u32)*4);
2289 "my version range %d-%d\n",
2300 "(MLS) not backwards compatible\n",
2321 info = policydb_lookup_compat(p->
policyvers);
2338 for (i = 0; i < info->
sym_num; i++) {
2339 rc = next_entry(buf, fp,
sizeof(
u32)*2);
2344 for (j = 0; j < nel; j++) {
2345 rc = read_f[
i](
p, p->symtab[
i].table,
fp);
2368 rc = next_entry(buf, fp,
sizeof(
u32));
2373 for (i = 0; i < nel; i++) {
2382 rc = next_entry(buf, fp,
sizeof(
u32)*3);
2391 rc = next_entry(buf, fp,
sizeof(
u32));
2406 rc = next_entry(buf, fp,
sizeof(
u32));
2411 for (i = 0; i < nel; i++) {
2420 rc = next_entry(buf, fp,
sizeof(
u32)*2);
2433 rc = filename_trans_read(p, fp);
2437 rc = policydb_index(p);
2447 rc = ocontext_read(p, info, fp);
2451 rc = genfs_read(p, fp);
2455 rc = range_read(p, fp);
2472 for (i = 0; i < p->p_types.nprim; i++) {
2488 rc = policydb_bounds_sanity_check(p);
2504 static int mls_write_level(
struct mls_level *l,
void *fp)
2510 rc = put_entry(buf,
sizeof(
u32), 1, fp);
2525 static int mls_write_range_helper(
struct mls_range *r,
void *fp)
2531 eq = mls_level_eq(&r->
level[1], &r->
level[0]);
2542 BUG_ON(items > (
sizeof(buf)/
sizeof(buf[0])));
2544 rc = put_entry(buf,
sizeof(
u32), items, fp);
2560 static int sens_write(
void *vkey,
void *datum,
void *
ptr)
2573 rc = put_entry(buf,
sizeof(
u32), 2, fp);
2577 rc = put_entry(key, 1, len, fp);
2581 rc = mls_write_level(levdatum->
level, fp);
2588 static int cat_write(
void *vkey,
void *datum,
void *ptr)
2602 rc = put_entry(buf,
sizeof(
u32), 3, fp);
2606 rc = put_entry(key, 1, len, fp);
2613 static int role_trans_write(
struct policydb *p,
void *fp)
2622 for (tr = r; tr; tr = tr->
next)
2625 rc = put_entry(buf,
sizeof(
u32), 1, fp);
2628 for (tr = r; tr; tr = tr->
next) {
2632 rc = put_entry(buf,
sizeof(
u32), 3, fp);
2637 rc = put_entry(buf,
sizeof(
u32), 1, fp);
2646 static int role_allow_write(
struct role_allow *r,
void *fp)
2654 for (ra = r;
ra; ra = ra->
next)
2657 rc = put_entry(buf,
sizeof(
u32), 1, fp);
2660 for (ra = r;
ra; ra = ra->
next) {
2663 rc = put_entry(buf,
sizeof(
u32), 2, fp);
2684 rc = put_entry(buf,
sizeof(
u32), 3, fp);
2688 rc = mls_write_range_helper(&c->
range, fp);
2701 static int perm_write(
void *vkey,
void *datum,
void *fp)
2712 rc = put_entry(buf,
sizeof(
u32), 2, fp);
2716 rc = put_entry(key, 1, len, fp);
2723 static int common_write(
void *vkey,
void *datum,
void *ptr)
2738 rc = put_entry(buf,
sizeof(
u32), 4, fp);
2742 rc = put_entry(key, 1, len, fp);
2762 for (c = node;
c; c = c->
next) {
2768 rc = put_entry(buf,
sizeof(
u32), 2, fp);
2771 for (e = c->
expr; e; e = e->
next) {
2775 rc = put_entry(buf,
sizeof(
u32), 3, fp);
2794 static int class_write(
void *vkey,
void *datum,
void *ptr)
2826 rc = put_entry(buf,
sizeof(
u32), 6, fp);
2830 rc = put_entry(key, 1, len, fp);
2835 rc = put_entry(cladatum->
comkey, 1, len2, fp);
2844 rc = write_cons_helper(p, cladatum->
constraints, fp);
2854 rc = put_entry(buf,
sizeof(
u32), 1, fp);
2867 rc = put_entry(buf,
sizeof(
uint32_t), 3, fp);
2874 rc = put_entry(buf,
sizeof(
uint32_t), 1, fp);
2882 static int role_write(
void *vkey,
void *datum,
void *ptr)
2900 BUG_ON(items > (
sizeof(buf)/
sizeof(buf[0])));
2902 rc = put_entry(buf,
sizeof(
u32), items, fp);
2906 rc = put_entry(key, 1, len, fp);
2921 static int type_write(
void *vkey,
void *datum,
void *ptr)
2950 BUG_ON(items > (
sizeof(buf) /
sizeof(buf[0])));
2951 rc = put_entry(buf,
sizeof(
u32), items, fp);
2955 rc = put_entry(key, 1, len, fp);
2962 static int user_write(
void *vkey,
void *datum,
void *ptr)
2979 BUG_ON(items > (
sizeof(buf) /
sizeof(buf[0])));
2980 rc = put_entry(buf,
sizeof(
u32), items, fp);
2984 rc = put_entry(key, 1, len, fp);
2992 rc = mls_write_range_helper(&usrdatum->
range, fp);
2996 rc = mls_write_level(&usrdatum->
dfltlevel, fp);
3003 static int (*write_f[SYM_NUM]) (
void *
key,
void *datum,
3019 unsigned int i,
j,
rc;
3024 for (i = 0; i < info->
ocon_num; i++) {
3029 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3036 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3039 rc = context_write(p, &c->
context[0], fp);
3047 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3050 rc = put_entry(c->
u.
name, 1, len, fp);
3053 rc = context_write(p, &c->
context[0], fp);
3056 rc = context_write(p, &c->
context[1], fp);
3064 rc = put_entry(buf,
sizeof(
u32), 3, fp);
3067 rc = context_write(p, &c->
context[0], fp);
3072 nodebuf[0] = c->
u.
node.addr;
3073 nodebuf[1] = c->
u.
node.mask;
3074 rc = put_entry(nodebuf,
sizeof(
u32), 2, fp);
3077 rc = context_write(p, &c->
context[0], fp);
3085 rc = put_entry(buf,
sizeof(
u32), 2, fp);
3088 rc = put_entry(c->
u.
name, 1, len, fp);
3091 rc = context_write(p, &c->
context[0], fp);
3096 for (j = 0; j < 4; j++)
3097 nodebuf[j] = c->
u.
node6.addr[j];
3098 for (j = 0; j < 4; j++)
3099 nodebuf[j + 4] = c->
u.
node6.mask[j];
3100 rc = put_entry(nodebuf,
sizeof(
u32), 8, fp);
3103 rc = context_write(p, &c->
context[0], fp);
3113 static int genfs_write(
struct policydb *p,
void *fp)
3115 struct genfs *genfs;
3122 for (genfs = p->
genfs; genfs; genfs = genfs->
next)
3125 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3128 for (genfs = p->
genfs; genfs; genfs = genfs->
next) {
3131 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3134 rc = put_entry(genfs->
fstype, 1, len, fp);
3138 for (c = genfs->
head; c; c = c->
next)
3141 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3144 for (c = genfs->
head; c; c = c->
next) {
3147 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3150 rc = put_entry(c->
u.
name, 1, len, fp);
3154 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3157 rc = context_write(p, &c->
context[0], fp);
3165 static int hashtab_cnt(
void *key,
void *
data,
void *ptr)
3173 static int range_write_helper(
void *key,
void *
data,
void *ptr)
3185 rc = put_entry(buf,
sizeof(
u32), 2, fp);
3190 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3194 rc = mls_write_range_helper(r, fp);
3201 static int range_write(
struct policydb *p,
void *fp)
3218 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3230 static int filename_write_helper(
void *key,
void *data,
void *ptr)
3241 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3245 rc = put_entry(ft->
name,
sizeof(
char), len, fp);
3252 buf[3] = otype->
otype;
3254 rc = put_entry(buf,
sizeof(
u32), 4, fp);
3261 static int filename_trans_write(
struct policydb *p,
void *fp)
3276 rc = put_entry(buf,
sizeof(
u32), 1, fp);
3294 unsigned int i, num_syms;
3309 " Because it is less than version %d\n", p->
policyvers,
3327 rc = put_entry(buf,
sizeof(
u32), 2, fp);
3335 info = policydb_lookup_compat(p->
policyvers);
3337 printk(
KERN_ERR "SELinux: compatibility lookup failed for policy "
3347 rc = put_entry(buf,
sizeof(
u32), 4, fp);
3364 for (i = 0; i < num_syms; i++) {
3373 rc = put_entry(buf,
sizeof(
u32), 2, fp);
3389 rc = role_trans_write(p, fp);
3397 rc = filename_trans_write(p, fp);
3401 rc = ocontext_write(p, info, fp);
3405 rc = genfs_write(p, fp);
3409 rc = range_write(p, fp);
3413 for (i = 0; i < p->p_types.nprim; i++) {