42 #include <linux/kernel.h>
43 #include <linux/slab.h>
44 #include <linux/string.h>
47 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #include <linux/audit.h>
88 static u32 latest_granting;
91 static int context_struct_to_string(
struct context *
context,
char **scontext,
94 static void context_struct_compute_av(
struct context *scontext,
106 static u16 current_mapping_size;
108 static int selinux_set_mapping(
struct policydb *
pol,
117 bool print_unknown_handle =
false;
133 while (map[j].name) {
146 "SELinux: Class %s not defined in policy.\n",
151 print_unknown_handle =
true;
158 if (!*p_in->
perms[k]) {
164 if (!p_out->
perms[k]) {
166 "SELinux: Permission %s in class %s not defined in policy.\n",
170 print_unknown_handle =
true;
178 if (print_unknown_handle)
179 printk(
KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
182 *out_map_p = out_map;
194 static u16 unmap_class(
u16 tclass)
196 if (tclass < current_mapping_size)
197 return current_mapping[tclass].
value;
205 static u16 map_class(
u16 pol_value)
209 for (i = 1; i < current_mapping_size; i++) {
210 if (current_mapping[i].
value == pol_value)
220 if (tclass < current_mapping_size) {
224 for (i = 0, result = 0; i <
n; i++) {
227 if (allow_unknown && !current_mapping[tclass].
perms[i])
232 for (i = 0, result = 0; i <
n; i++)
237 for (i = 0, result = 0; i <
n; i++) {
240 if (!allow_unknown && !current_mapping[tclass].
perms[i])
248 for (; i < (
sizeof(
u32)*8); i++)
270 static int constraint_expr_eval(
struct context *scontext,
283 for (e = cexpr;
e; e = e->
next) {
304 val1 = scontext->
user;
305 val2 = tcontext->
user;
308 val1 = scontext->
type;
309 val2 = tcontext->
type;
312 val1 = scontext->
role;
313 val2 = tcontext->
role;
336 l1 = &(scontext->
range.level[0]);
337 l2 = &(tcontext->
range.level[0]);
340 l1 = &(scontext->
range.level[0]);
341 l2 = &(tcontext->
range.level[1]);
344 l1 = &(scontext->
range.level[1]);
345 l2 = &(tcontext->
range.level[0]);
348 l1 = &(scontext->
range.level[1]);
349 l2 = &(tcontext->
range.level[1]);
352 l1 = &(scontext->
range.level[0]);
353 l2 = &(scontext->
range.level[1]);
356 l1 = &(tcontext->
range.level[0]);
357 l2 = &(tcontext->
range.level[1]);
362 s[++
sp] = mls_level_eq(l1, l2);
365 s[++
sp] = !mls_level_eq(l1, l2);
368 s[++
sp] = mls_level_dom(l1, l2);
371 s[++
sp] = mls_level_dom(l2, l1);
388 s[++
sp] = (val1 == val2);
391 s[++
sp] = (val1 != val2);
448 static int dump_masked_av_helper(
void *k,
void *
d,
void *args)
451 char **permission_names = args;
455 permission_names[pdatum->
value - 1] = (
char *)k;
460 static void security_dump_masked_av(
struct context *scontext,
470 char *scontext_name =
NULL;
471 char *tcontext_name =
NULL;
472 char *permission_names[32];
475 bool need_comma =
false;
487 dump_masked_av_helper, permission_names) < 0)
491 dump_masked_av_helper, permission_names) < 0)
495 if (context_struct_to_string(scontext,
496 &scontext_name, &length) < 0)
499 if (context_struct_to_string(tcontext,
500 &tcontext_name, &length) < 0)
510 "scontext=%s tcontext=%s tclass=%s perms=",
511 reason, scontext_name, tcontext_name, tclass_name);
513 for (index = 0; index < 32; index++) {
516 if ((mask & permissions) == 0)
520 need_comma ?
"," :
"",
521 permission_names[index]
522 ? permission_names[index] :
"????");
528 kfree(tcontext_name);
529 kfree(scontext_name);
538 static void type_attribute_bounds_av(
struct context *scontext,
559 memset(&lo_avd, 0,
sizeof(lo_avd));
561 memcpy(&lo_scontext, scontext,
sizeof(lo_scontext));
562 lo_scontext.type = source->
bounds;
564 context_struct_compute_av(&lo_scontext,
570 masked = ~lo_avd.allowed & avd->
allowed;
574 memset(&lo_avd, 0,
sizeof(lo_avd));
576 memcpy(&lo_tcontext, tcontext,
sizeof(lo_tcontext));
577 lo_tcontext.type = target->
bounds;
579 context_struct_compute_av(scontext,
585 masked = ~lo_avd.allowed & avd->
allowed;
589 memset(&lo_avd, 0,
sizeof(lo_avd));
595 context_struct_compute_av(&lo_scontext,
601 masked = ~lo_avd.allowed & avd->
allowed;
609 security_dump_masked_av(scontext, tcontext,
610 tclass, masked,
"bounds");
618 static void context_struct_compute_av(
struct context *scontext,
637 if (printk_ratelimit())
648 avkey.target_class = tclass;
656 avkey.source_type = i + 1;
657 avkey.target_type = j + 1;
682 !constraint_expr_eval(scontext, tcontext,
NULL,
686 constraint = constraint->
next;
711 type_attribute_bounds_av(scontext, tcontext,
721 u32 olen, nlen, tlen;
723 if (context_struct_to_string(ocontext, &o, &olen))
725 if (context_struct_to_string(ncontext, &n, &nlen))
727 if (context_struct_to_string(tcontext, &t, &tlen))
730 "security_validate_transition: denied for"
731 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
759 tclass = unmap_class(orig_tclass);
761 if (!tclass || tclass >
policydb.p_classes.nprim) {
795 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
797 rc = security_validtrans_handle_fail(ocontext, ncontext,
801 constraint = constraint->
next;
820 struct context *old_context, *new_context;
845 if (old_context->
type == new_context->
type)
848 index = new_context->
type;
868 char *old_name =
NULL;
869 char *new_name =
NULL;
872 if (!context_struct_to_string(old_context,
873 &old_name, &length) &&
874 !context_struct_to_string(new_context,
875 &new_name, &length)) {
878 "op=security_bounded_transition "
880 "oldcontext=%s newcontext=%s",
897 avd->
seqno = latest_granting;
943 tclass = unmap_class(orig_tclass);
944 if (
unlikely(orig_tclass && !tclass)) {
949 context_struct_compute_av(scontext, tcontext, tclass, avd);
995 context_struct_compute_av(scontext, tcontext, tclass, avd);
1011 static int context_struct_to_string(
struct context *
context,
char **scontext,
u32 *scontext_len)
1020 *scontext_len = context->
len;
1042 *scontext = scontextp;
1047 sprintf(scontextp,
"%s:%s:%s",
1066 if (
unlikely(sid > SECINITSID_NUM))
1068 return initial_sid_to_string[
sid];
1071 static int security_sid_to_context_core(
u32 sid,
char **scontext,
1082 if (sid <= SECINITSID_NUM) {
1085 *scontext_len =
strlen(initial_sid_to_string[sid]) + 1;
1093 strcpy(scontextp, initial_sid_to_string[sid]);
1094 *scontext = scontextp;
1098 "load_policy on unknown SID %d\n", __func__, sid);
1113 rc = context_struct_to_string(context, scontext, scontext_len);
1133 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
1138 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
1144 static int string_to_context_struct(
struct policydb *pol,
1148 struct context *
ctx,
1154 char *scontextp, *
p, oldc;
1162 scontextp = (
char *) scontext;
1166 while (*p && *p !=
':')
1182 while (*p && *p !=
':')
1197 while (*p && *p !=
':')
1213 if ((p - scontext) < scontext_len)
1222 context_destroy(ctx);
1226 static int security_context_to_sid_core(
const char *scontext,
u32 scontext_len,
1231 struct context context;
1237 for (i = 1; i < SECINITSID_NUM; i++) {
1238 if (!
strcmp(initial_sid_to_string[i], scontext)) {
1243 *sid = SECINITSID_KERNEL;
1249 scontext2 =
kmalloc(scontext_len + 1, gfp_flags);
1252 memcpy(scontext2, scontext, scontext_len);
1253 scontext2[scontext_len] = 0;
1258 str =
kstrdup(scontext2, gfp_flags);
1265 scontext_len, &context, def_sid);
1266 if (rc == -
EINVAL && force) {
1268 context.
len = scontext_len;
1273 context_destroy(&context);
1295 return security_context_to_sid_core(scontext, scontext_len,
1320 return security_context_to_sid_core(scontext, scontext_len,
1321 sid, def_sid, gfp_flags, 1);
1327 return security_context_to_sid_core(scontext, scontext_len,
1331 static int compute_sid_handle_invalid_context(
1332 struct context *scontext,
1333 struct context *tcontext,
1335 struct context *newcontext)
1338 u32 slen, tlen, nlen;
1340 if (context_struct_to_string(scontext, &s, &slen))
1342 if (context_struct_to_string(tcontext, &t, &tlen))
1344 if (context_struct_to_string(newcontext, &n, &nlen))
1347 "security_compute_sid: invalid context %s"
1361 static void filename_compute_type(
struct policydb *p,
struct context *newcontext,
1363 const char *objname)
1386 static int security_compute_sid(
u32 ssid,
1390 const char *objname,
1395 struct context *scontext =
NULL, *tcontext =
NULL, newcontext;
1405 switch (orig_tclass) {
1406 case SECCLASS_PROCESS:
1416 context_init(&newcontext);
1421 tclass = unmap_class(orig_tclass);
1422 sock = security_is_socket_class(orig_tclass);
1424 tclass = orig_tclass;
1425 sock = security_is_socket_class(map_class(tclass));
1443 if (tclass && tclass <=
policydb.p_classes.nprim)
1447 switch (specified) {
1451 newcontext.user = tcontext->user;
1455 newcontext.user = scontext->
user;
1460 newcontext.user = tcontext->user;
1466 newcontext.role = scontext->
role;
1468 newcontext.role = tcontext->role;
1471 newcontext.role = scontext->
role;
1478 newcontext.type = scontext->
type;
1480 newcontext.type = tcontext->type;
1484 newcontext.type = scontext->
type;
1487 newcontext.type = tcontext->type;
1492 avkey.source_type = scontext->
type;
1493 avkey.target_type = tcontext->type;
1494 avkey.target_class = tclass;
1495 avkey.specified = specified;
1503 avdatum = &node->
datum;
1511 newcontext.type = avdatum->
data;
1516 filename_compute_type(&
policydb, &newcontext, scontext->
type,
1517 tcontext->type, tclass, objname);
1523 if ((roletr->
role == scontext->
role) &&
1524 (roletr->
type == tcontext->type) &&
1525 (roletr->
tclass == tclass)) {
1527 newcontext.role = roletr->
new_role;
1542 rc = compute_sid_handle_invalid_context(scontext,
1553 context_destroy(&newcontext);
1574 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1575 qstr ? qstr->
name :
NULL, out_sid,
true);
1579 const char *objname,
u32 *out_sid)
1581 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1582 objname, out_sid,
false);
1630 static int clone_sid(
u32 sid,
1631 struct context *context,
1636 if (sid > SECINITSID_NUM)
1642 static inline int convert_context_handle_invalid_context(
struct context *context)
1650 if (!context_struct_to_string(context, &s, &len)) {
1674 struct context oldc;
1675 struct ocontext *oc;
1684 if (key <= SECINITSID_NUM)
1697 rc = string_to_context_struct(args->
newp,
NULL, s,
1705 memcpy(c, &ctx,
sizeof(*c));
1707 }
else if (rc == -
EINVAL) {
1719 rc = context_cpy(&oldc, c);
1748 if (args->
oldp->mls_enabled && args->
newp->mls_enabled) {
1752 }
else if (args->
oldp->mls_enabled && !args->
newp->mls_enabled) {
1758 mls_context_destroy(c);
1759 }
else if (!args->
oldp->mls_enabled && args->
newp->mls_enabled) {
1768 while (oc && oc->
sid[0] != SECINITSID_UNLABELED)
1773 " the initial SIDs list\n");
1776 range = &oc->
context[0].range;
1784 rc = convert_context_handle_invalid_context(&oldc);
1789 context_destroy(&oldc);
1796 rc = context_struct_to_string(&oldc, &s, &len);
1799 context_destroy(&oldc);
1809 static void security_load_policycaps(
void)
1817 static int security_preserve_bools(
struct policydb *p);
1831 struct policydb oldpolicydb, newpolicydb;
1832 struct sidtab oldsidtab, newsidtab;
1851 ¤t_mapping_size);
1865 security_load_policycaps();
1867 seqno = ++latest_granting;
1873 selinux_xfrm_notify_policyload();
1899 rc = selinux_set_mapping(&newpolicydb,
secclass_map, &map, &map_size);
1903 rc = security_preserve_bools(&newpolicydb);
1921 args.
newp = &newpolicydb;
1925 " representation of contexts in the new SID"
1938 security_load_policycaps();
1939 oldmap = current_mapping;
1940 current_mapping =
map;
1941 current_mapping_size = map_size;
1942 seqno = ++latest_granting;
1954 selinux_xfrm_notify_policyload();
1992 if (c->
u.
port.protocol == protocol &&
1993 c->
u.
port.low_port <= port &&
1994 c->
u.
port.high_port >= port)
2007 *out_sid = c->
sid[0];
2009 *out_sid = SECINITSID_PORT;
2037 if (!c->
sid[0] || !c->
sid[1]) {
2049 *if_sid = c->
sid[0];
2051 *if_sid = SECINITSID_NETIF;
2062 for (i = 0; i < 4; i++)
2063 if (addr[i] != (input[i] & mask[i])) {
2093 if (addrlen !=
sizeof(
u32))
2096 addr = *((
u32 *)addrp);
2100 if (c->
u.
node.addr == (addr & c->
u.
node.mask))
2109 if (addrlen !=
sizeof(
u64) * 2)
2113 if (match_ipv6_addrmask(addrp, c->
u.
node6.addr,
2122 *out_sid = SECINITSID_NODE;
2134 *out_sid = c->
sid[0];
2136 *out_sid = SECINITSID_NODE;
2166 struct context *fromcon, usercon;
2182 context_init(&usercon);
2197 mysids = kcalloc(maxnel,
sizeof(*mysids),
GFP_ATOMIC);
2203 usercon.
role = i + 1;
2205 usercon.
type = j + 1;
2213 if (mynel < maxnel) {
2214 mysids[mynel++] =
sid;
2218 mysids2 = kcalloc(maxnel,
sizeof(*mysids2),
GFP_ATOMIC);
2221 memcpy(mysids2, mysids, mynel *
sizeof(*mysids2));
2224 mysids[mynel++] =
sid;
2237 mysids2 = kcalloc(mynel,
sizeof(*mysids2),
GFP_KERNEL);
2242 for (i = 0, j = 0; i < mynel; i++) {
2249 mysids2[j++] = mysids[
i];
2282 while (path[0] ==
'/' && path[1] ==
'/')
2287 sclass = unmap_class(orig_sclass);
2288 *sid = SECINITSID_UNLABELED;
2300 for (c = genfs->
head; c; c = c->
next) {
2385 *names = kcalloc(*len,
sizeof(
char *),
GFP_ATOMIC);
2390 *values = kcalloc(*len,
sizeof(
int),
GFP_ATOMIC);
2394 for (i = 0; i < *len; i++) {
2406 (*names)[
i][name_len - 1] = 0;
2414 for (i = 0; i < *len; i++)
2425 int lenp,
seqno = 0;
2435 for (i = 0; i < len; i++) {
2439 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2444 audit_get_sessionid(
current));
2458 seqno = ++latest_granting;
2466 selinux_xfrm_notify_policyload();
2489 static int security_preserve_bools(
struct policydb *p)
2491 int rc, nbools = 0, *bvalues =
NULL,
i;
2492 char **bnames =
NULL;
2499 for (i = 0; i < nbools; i++) {
2502 booldatum->
state = bvalues[
i];
2512 for (i = 0; i < nbools; i++)
2526 struct context *context1;
2527 struct context *context2;
2528 struct context newcon;
2539 context_init(&newcon);
2562 rc = mls_context_cpy(&newcon, context2);
2568 rc = convert_context_handle_invalid_context(&newcon);
2570 if (!context_struct_to_string(&newcon, &s, &len)) {
2572 "security_sid_mls_copy: invalid context %s", s);
2582 context_destroy(&newcon);
2612 struct context *nlbl_ctx;
2613 struct context *xfrm_ctx;
2621 *peer_sid = nlbl_sid;
2628 *peer_sid = xfrm_sid;
2644 __func__, nlbl_sid);
2651 __func__, xfrm_sid);
2654 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -
EACCES);
2663 *peer_sid = xfrm_sid;
2669 static int get_classes_callback(
void *k,
void *
d,
void *args)
2672 char *name =
k, **classes = args;
2676 if (!classes[value])
2689 *nclasses =
policydb.p_classes.nprim;
2690 *classes = kcalloc(*nclasses,
sizeof(**classes),
GFP_ATOMIC);
2698 for (i = 0; i < *nclasses; i++)
2699 kfree((*classes)[i]);
2708 static int get_permissions_callback(
void *k,
void *d,
void *args)
2711 char *name =
k, **perms = args;
2712 int value = datum->
value - 1;
2738 *perms = kcalloc(*nperms,
sizeof(**perms),
GFP_ATOMIC);
2744 get_permissions_callback, *perms);
2760 for (i = 0; i < *nperms; i++)
2807 context_destroy(&rule->
au_ctxt);
2842 if (
strchr(rulestr,
'-'))
2854 context_init(&tmprule->
au_ctxt);
2858 tmprule->
au_seqno = latest_granting;
2936 struct context *ctxt;
2943 "selinux_audit_rule_match: missing rule\n");
2949 if (rule->
au_seqno < latest_granting) {
2951 "selinux_audit_rule_match: stale rule\n");
2959 "selinux_audit_rule_match: unrecognized SID %d\n",
3007 &ctxt->
range.level[0] : &ctxt->
range.level[1]);
3010 match = mls_level_eq(&rule->
au_ctxt.range.level[0],
3014 match = !mls_level_eq(&rule->
au_ctxt.range.level[0],
3018 match = (mls_level_dom(&rule->
au_ctxt.range.level[0],
3020 !mls_level_eq(&rule->
au_ctxt.range.level[0],
3024 match = mls_level_dom(&rule->
au_ctxt.range.level[0],
3028 match = (mls_level_dom(level,
3029 &rule->
au_ctxt.range.level[0]) &&
3030 !mls_level_eq(level,
3031 &rule->
au_ctxt.range.level[0]));
3034 match = mls_level_dom(level,
3035 &rule->
au_ctxt.range.level[0]);
3047 static int aurule_avc_callback(
u32 event)
3052 err = aurule_callback();
3056 static int __init aurule_init(
void)
3062 panic(
"avc_add_callback() failed, error %d\n", err);
3068 #ifdef CONFIG_NETLABEL
3086 if (sid_cache ==
NULL)
3096 secattr->
cache->data = sid_cache;
3119 struct context *
ctx;
3120 struct context ctx_new;
3130 *sid = *(
u32 *)secattr->
cache->data;
3132 *sid = secattr->
attr.secid;
3139 context_init(&ctx_new);
3140 ctx_new.user = ctx->
user;
3141 ctx_new.role = ctx->
role;
3142 ctx_new.type = ctx->
type;
3143 mls_import_netlbl_lvl(&ctx_new, secattr);
3145 rc = ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
3146 secattr->
attr.mls.cat);
3149 memcpy(&ctx_new.range.level[1].cat,
3150 &ctx_new.range.level[0].cat,
3151 sizeof(ctx_new.range.level[0].cat));
3161 security_netlbl_cache_add(secattr, *sid);
3189 struct context *
ctx;
3209 mls_export_netlbl_lvl(ctx, secattr);
3210 rc = mls_export_netlbl_cat(ctx, secattr);