1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/kernel.h>
41#include <linux/slab.h>
42#include <linux/string.h>
43#include <linux/spinlock.h>
44#include <linux/rcupdate.h>
45#include <linux/errno.h>
46#include <linux/in.h>
47#include <linux/sched.h>
48#include <linux/audit.h>
49#include <linux/vmalloc.h>
50#include <linux/lsm_hooks.h>
51#include <net/netlabel.h>
52
53#include "flask.h"
54#include "avc.h"
55#include "avc_ss.h"
56#include "security.h"
57#include "context.h"
58#include "policydb.h"
59#include "sidtab.h"
60#include "services.h"
61#include "conditional.h"
62#include "mls.h"
63#include "objsec.h"
64#include "netlabel.h"
65#include "xfrm.h"
66#include "ebitmap.h"
67#include "audit.h"
68#include "policycap_names.h"
69#include "ima.h"
70
71struct convert_context_args {
72 struct selinux_state *state;
73 struct policydb *oldp;
74 struct policydb *newp;
75};
76
77struct selinux_policy_convert_data {
78 struct convert_context_args args;
79 struct sidtab_convert_params sidtab_params;
80};
81
82
83static int context_struct_to_string(struct policydb *policydb,
84 struct context *context,
85 char **scontext,
86 u32 *scontext_len);
87
88static int sidtab_entry_to_string(struct policydb *policydb,
89 struct sidtab *sidtab,
90 struct sidtab_entry *entry,
91 char **scontext,
92 u32 *scontext_len);
93
94static void context_struct_compute_av(struct policydb *policydb,
95 struct context *scontext,
96 struct context *tcontext,
97 u16 tclass,
98 struct av_decision *avd,
99 struct extended_perms *xperms);
100
101static int selinux_set_mapping(struct policydb *pol,
102 struct security_class_mapping *map,
103 struct selinux_map *out_map)
104{
105 u16 i, j;
106 unsigned k;
107 bool print_unknown_handle = false;
108
109
110 if (!map)
111 return -EINVAL;
112 i = 0;
113 while (map[i].name)
114 i++;
115
116
117 out_map->mapping = kcalloc(++i, sizeof(*out_map->mapping), GFP_ATOMIC);
118 if (!out_map->mapping)
119 return -ENOMEM;
120
121
122 j = 0;
123 while (map[j].name) {
124 struct security_class_mapping *p_in = map + (j++);
125 struct selinux_mapping *p_out = out_map->mapping + j;
126
127
128 if (!strcmp(p_in->name, "")) {
129 p_out->num_perms = 0;
130 continue;
131 }
132
133 p_out->value = string_to_security_class(pol, p_in->name);
134 if (!p_out->value) {
135 pr_info("SELinux: Class %s not defined in policy.\n",
136 p_in->name);
137 if (pol->reject_unknown)
138 goto err;
139 p_out->num_perms = 0;
140 print_unknown_handle = true;
141 continue;
142 }
143
144 k = 0;
145 while (p_in->perms[k]) {
146
147 if (!*p_in->perms[k]) {
148 k++;
149 continue;
150 }
151 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
152 p_in->perms[k]);
153 if (!p_out->perms[k]) {
154 pr_info("SELinux: Permission %s in class %s not defined in policy.\n",
155 p_in->perms[k], p_in->name);
156 if (pol->reject_unknown)
157 goto err;
158 print_unknown_handle = true;
159 }
160
161 k++;
162 }
163 p_out->num_perms = k;
164 }
165
166 if (print_unknown_handle)
167 pr_info("SELinux: the above unknown classes and permissions will be %s\n",
168 pol->allow_unknown ? "allowed" : "denied");
169
170 out_map->size = i;
171 return 0;
172err:
173 kfree(out_map->mapping);
174 out_map->mapping = NULL;
175 return -EINVAL;
176}
177
178
179
180
181
182static u16 unmap_class(struct selinux_map *map, u16 tclass)
183{
184 if (tclass < map->size)
185 return map->mapping[tclass].value;
186
187 return tclass;
188}
189
190
191
192
193static u16 map_class(struct selinux_map *map, u16 pol_value)
194{
195 u16 i;
196
197 for (i = 1; i < map->size; i++) {
198 if (map->mapping[i].value == pol_value)
199 return i;
200 }
201
202 return SECCLASS_NULL;
203}
204
205static void map_decision(struct selinux_map *map,
206 u16 tclass, struct av_decision *avd,
207 int allow_unknown)
208{
209 if (tclass < map->size) {
210 struct selinux_mapping *mapping = &map->mapping[tclass];
211 unsigned int i, n = mapping->num_perms;
212 u32 result;
213
214 for (i = 0, result = 0; i < n; i++) {
215 if (avd->allowed & mapping->perms[i])
216 result |= 1<<i;
217 if (allow_unknown && !mapping->perms[i])
218 result |= 1<<i;
219 }
220 avd->allowed = result;
221
222 for (i = 0, result = 0; i < n; i++)
223 if (avd->auditallow & mapping->perms[i])
224 result |= 1<<i;
225 avd->auditallow = result;
226
227 for (i = 0, result = 0; i < n; i++) {
228 if (avd->auditdeny & mapping->perms[i])
229 result |= 1<<i;
230 if (!allow_unknown && !mapping->perms[i])
231 result |= 1<<i;
232 }
233
234
235
236
237
238 for (; i < (sizeof(u32)*8); i++)
239 result |= 1<<i;
240 avd->auditdeny = result;
241 }
242}
243
244int security_mls_enabled(struct selinux_state *state)
245{
246 int mls_enabled;
247 struct selinux_policy *policy;
248
249 if (!selinux_initialized(state))
250 return 0;
251
252 rcu_read_lock();
253 policy = rcu_dereference(state->policy);
254 mls_enabled = policy->policydb.mls_enabled;
255 rcu_read_unlock();
256 return mls_enabled;
257}
258
259
260
261
262
263
264
265
266
267
268
269
270static int constraint_expr_eval(struct policydb *policydb,
271 struct context *scontext,
272 struct context *tcontext,
273 struct context *xcontext,
274 struct constraint_expr *cexpr)
275{
276 u32 val1, val2;
277 struct context *c;
278 struct role_datum *r1, *r2;
279 struct mls_level *l1, *l2;
280 struct constraint_expr *e;
281 int s[CEXPR_MAXDEPTH];
282 int sp = -1;
283
284 for (e = cexpr; e; e = e->next) {
285 switch (e->expr_type) {
286 case CEXPR_NOT:
287 BUG_ON(sp < 0);
288 s[sp] = !s[sp];
289 break;
290 case CEXPR_AND:
291 BUG_ON(sp < 1);
292 sp--;
293 s[sp] &= s[sp + 1];
294 break;
295 case CEXPR_OR:
296 BUG_ON(sp < 1);
297 sp--;
298 s[sp] |= s[sp + 1];
299 break;
300 case CEXPR_ATTR:
301 if (sp == (CEXPR_MAXDEPTH - 1))
302 return 0;
303 switch (e->attr) {
304 case CEXPR_USER:
305 val1 = scontext->user;
306 val2 = tcontext->user;
307 break;
308 case CEXPR_TYPE:
309 val1 = scontext->type;
310 val2 = tcontext->type;
311 break;
312 case CEXPR_ROLE:
313 val1 = scontext->role;
314 val2 = tcontext->role;
315 r1 = policydb->role_val_to_struct[val1 - 1];
316 r2 = policydb->role_val_to_struct[val2 - 1];
317 switch (e->op) {
318 case CEXPR_DOM:
319 s[++sp] = ebitmap_get_bit(&r1->dominates,
320 val2 - 1);
321 continue;
322 case CEXPR_DOMBY:
323 s[++sp] = ebitmap_get_bit(&r2->dominates,
324 val1 - 1);
325 continue;
326 case CEXPR_INCOMP:
327 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
328 val2 - 1) &&
329 !ebitmap_get_bit(&r2->dominates,
330 val1 - 1));
331 continue;
332 default:
333 break;
334 }
335 break;
336 case CEXPR_L1L2:
337 l1 = &(scontext->range.level[0]);
338 l2 = &(tcontext->range.level[0]);
339 goto mls_ops;
340 case CEXPR_L1H2:
341 l1 = &(scontext->range.level[0]);
342 l2 = &(tcontext->range.level[1]);
343 goto mls_ops;
344 case CEXPR_H1L2:
345 l1 = &(scontext->range.level[1]);
346 l2 = &(tcontext->range.level[0]);
347 goto mls_ops;
348 case CEXPR_H1H2:
349 l1 = &(scontext->range.level[1]);
350 l2 = &(tcontext->range.level[1]);
351 goto mls_ops;
352 case CEXPR_L1H1:
353 l1 = &(scontext->range.level[0]);
354 l2 = &(scontext->range.level[1]);
355 goto mls_ops;
356 case CEXPR_L2H2:
357 l1 = &(tcontext->range.level[0]);
358 l2 = &(tcontext->range.level[1]);
359 goto mls_ops;
360mls_ops:
361 switch (e->op) {
362 case CEXPR_EQ:
363 s[++sp] = mls_level_eq(l1, l2);
364 continue;
365 case CEXPR_NEQ:
366 s[++sp] = !mls_level_eq(l1, l2);
367 continue;
368 case CEXPR_DOM:
369 s[++sp] = mls_level_dom(l1, l2);
370 continue;
371 case CEXPR_DOMBY:
372 s[++sp] = mls_level_dom(l2, l1);
373 continue;
374 case CEXPR_INCOMP:
375 s[++sp] = mls_level_incomp(l2, l1);
376 continue;
377 default:
378 BUG();
379 return 0;
380 }
381 break;
382 default:
383 BUG();
384 return 0;
385 }
386
387 switch (e->op) {
388 case CEXPR_EQ:
389 s[++sp] = (val1 == val2);
390 break;
391 case CEXPR_NEQ:
392 s[++sp] = (val1 != val2);
393 break;
394 default:
395 BUG();
396 return 0;
397 }
398 break;
399 case CEXPR_NAMES:
400 if (sp == (CEXPR_MAXDEPTH-1))
401 return 0;
402 c = scontext;
403 if (e->attr & CEXPR_TARGET)
404 c = tcontext;
405 else if (e->attr & CEXPR_XTARGET) {
406 c = xcontext;
407 if (!c) {
408 BUG();
409 return 0;
410 }
411 }
412 if (e->attr & CEXPR_USER)
413 val1 = c->user;
414 else if (e->attr & CEXPR_ROLE)
415 val1 = c->role;
416 else if (e->attr & CEXPR_TYPE)
417 val1 = c->type;
418 else {
419 BUG();
420 return 0;
421 }
422
423 switch (e->op) {
424 case CEXPR_EQ:
425 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
426 break;
427 case CEXPR_NEQ:
428 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
429 break;
430 default:
431 BUG();
432 return 0;
433 }
434 break;
435 default:
436 BUG();
437 return 0;
438 }
439 }
440
441 BUG_ON(sp != 0);
442 return s[0];
443}
444
445
446
447
448
449static int dump_masked_av_helper(void *k, void *d, void *args)
450{
451 struct perm_datum *pdatum = d;
452 char **permission_names = args;
453
454 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
455
456 permission_names[pdatum->value - 1] = (char *)k;
457
458 return 0;
459}
460
461static void security_dump_masked_av(struct policydb *policydb,
462 struct context *scontext,
463 struct context *tcontext,
464 u16 tclass,
465 u32 permissions,
466 const char *reason)
467{
468 struct common_datum *common_dat;
469 struct class_datum *tclass_dat;
470 struct audit_buffer *ab;
471 char *tclass_name;
472 char *scontext_name = NULL;
473 char *tcontext_name = NULL;
474 char *permission_names[32];
475 int index;
476 u32 length;
477 bool need_comma = false;
478
479 if (!permissions)
480 return;
481
482 tclass_name = sym_name(policydb, SYM_CLASSES, tclass - 1);
483 tclass_dat = policydb->class_val_to_struct[tclass - 1];
484 common_dat = tclass_dat->comdatum;
485
486
487 if (common_dat &&
488 hashtab_map(&common_dat->permissions.table,
489 dump_masked_av_helper, permission_names) < 0)
490 goto out;
491
492 if (hashtab_map(&tclass_dat->permissions.table,
493 dump_masked_av_helper, permission_names) < 0)
494 goto out;
495
496
497 if (context_struct_to_string(policydb, scontext,
498 &scontext_name, &length) < 0)
499 goto out;
500
501 if (context_struct_to_string(policydb, tcontext,
502 &tcontext_name, &length) < 0)
503 goto out;
504
505
506 ab = audit_log_start(audit_context(),
507 GFP_ATOMIC, AUDIT_SELINUX_ERR);
508 if (!ab)
509 goto out;
510
511 audit_log_format(ab, "op=security_compute_av reason=%s "
512 "scontext=%s tcontext=%s tclass=%s perms=",
513 reason, scontext_name, tcontext_name, tclass_name);
514
515 for (index = 0; index < 32; index++) {
516 u32 mask = (1 << index);
517
518 if ((mask & permissions) == 0)
519 continue;
520
521 audit_log_format(ab, "%s%s",
522 need_comma ? "," : "",
523 permission_names[index]
524 ? permission_names[index] : "????");
525 need_comma = true;
526 }
527 audit_log_end(ab);
528out:
529
530 kfree(tcontext_name);
531 kfree(scontext_name);
532
533 return;
534}
535
536
537
538
539
540static void type_attribute_bounds_av(struct policydb *policydb,
541 struct context *scontext,
542 struct context *tcontext,
543 u16 tclass,
544 struct av_decision *avd)
545{
546 struct context lo_scontext;
547 struct context lo_tcontext, *tcontextp = tcontext;
548 struct av_decision lo_avd;
549 struct type_datum *source;
550 struct type_datum *target;
551 u32 masked = 0;
552
553 source = policydb->type_val_to_struct[scontext->type - 1];
554 BUG_ON(!source);
555
556 if (!source->bounds)
557 return;
558
559 target = policydb->type_val_to_struct[tcontext->type - 1];
560 BUG_ON(!target);
561
562 memset(&lo_avd, 0, sizeof(lo_avd));
563
564 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
565 lo_scontext.type = source->bounds;
566
567 if (target->bounds) {
568 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
569 lo_tcontext.type = target->bounds;
570 tcontextp = &lo_tcontext;
571 }
572
573 context_struct_compute_av(policydb, &lo_scontext,
574 tcontextp,
575 tclass,
576 &lo_avd,
577 NULL);
578
579 masked = ~lo_avd.allowed & avd->allowed;
580
581 if (likely(!masked))
582 return;
583
584
585 avd->allowed &= ~masked;
586
587
588 security_dump_masked_av(policydb, scontext, tcontext,
589 tclass, masked, "bounds");
590}
591
592
593
594
595
596void services_compute_xperms_drivers(
597 struct extended_perms *xperms,
598 struct avtab_node *node)
599{
600 unsigned int i;
601
602 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
603
604 for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++)
605 xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i];
606 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
607
608 security_xperm_set(xperms->drivers.p,
609 node->datum.u.xperms->driver);
610 }
611
612 xperms->len = 1;
613}
614
615
616
617
618
619static void context_struct_compute_av(struct policydb *policydb,
620 struct context *scontext,
621 struct context *tcontext,
622 u16 tclass,
623 struct av_decision *avd,
624 struct extended_perms *xperms)
625{
626 struct constraint_node *constraint;
627 struct role_allow *ra;
628 struct avtab_key avkey;
629 struct avtab_node *node;
630 struct class_datum *tclass_datum;
631 struct ebitmap *sattr, *tattr;
632 struct ebitmap_node *snode, *tnode;
633 unsigned int i, j;
634
635 avd->allowed = 0;
636 avd->auditallow = 0;
637 avd->auditdeny = 0xffffffff;
638 if (xperms) {
639 memset(&xperms->drivers, 0, sizeof(xperms->drivers));
640 xperms->len = 0;
641 }
642
643 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
644 if (printk_ratelimit())
645 pr_warn("SELinux: Invalid class %hu\n", tclass);
646 return;
647 }
648
649 tclass_datum = policydb->class_val_to_struct[tclass - 1];
650
651
652
653
654
655 avkey.target_class = tclass;
656 avkey.specified = AVTAB_AV | AVTAB_XPERMS;
657 sattr = &policydb->type_attr_map_array[scontext->type - 1];
658 tattr = &policydb->type_attr_map_array[tcontext->type - 1];
659 ebitmap_for_each_positive_bit(sattr, snode, i) {
660 ebitmap_for_each_positive_bit(tattr, tnode, j) {
661 avkey.source_type = i + 1;
662 avkey.target_type = j + 1;
663 for (node = avtab_search_node(&policydb->te_avtab,
664 &avkey);
665 node;
666 node = avtab_search_node_next(node, avkey.specified)) {
667 if (node->key.specified == AVTAB_ALLOWED)
668 avd->allowed |= node->datum.u.data;
669 else if (node->key.specified == AVTAB_AUDITALLOW)
670 avd->auditallow |= node->datum.u.data;
671 else if (node->key.specified == AVTAB_AUDITDENY)
672 avd->auditdeny &= node->datum.u.data;
673 else if (xperms && (node->key.specified & AVTAB_XPERMS))
674 services_compute_xperms_drivers(xperms, node);
675 }
676
677
678 cond_compute_av(&policydb->te_cond_avtab, &avkey,
679 avd, xperms);
680
681 }
682 }
683
684
685
686
687
688 constraint = tclass_datum->constraints;
689 while (constraint) {
690 if ((constraint->permissions & (avd->allowed)) &&
691 !constraint_expr_eval(policydb, scontext, tcontext, NULL,
692 constraint->expr)) {
693 avd->allowed &= ~(constraint->permissions);
694 }
695 constraint = constraint->next;
696 }
697
698
699
700
701
702
703 if (tclass == policydb->process_class &&
704 (avd->allowed & policydb->process_trans_perms) &&
705 scontext->role != tcontext->role) {
706 for (ra = policydb->role_allow; ra; ra = ra->next) {
707 if (scontext->role == ra->role &&
708 tcontext->role == ra->new_role)
709 break;
710 }
711 if (!ra)
712 avd->allowed &= ~policydb->process_trans_perms;
713 }
714
715
716
717
718
719
720 type_attribute_bounds_av(policydb, scontext, tcontext,
721 tclass, avd);
722}
723
724static int security_validtrans_handle_fail(struct selinux_state *state,
725 struct selinux_policy *policy,
726 struct sidtab_entry *oentry,
727 struct sidtab_entry *nentry,
728 struct sidtab_entry *tentry,
729 u16 tclass)
730{
731 struct policydb *p = &policy->policydb;
732 struct sidtab *sidtab = policy->sidtab;
733 char *o = NULL, *n = NULL, *t = NULL;
734 u32 olen, nlen, tlen;
735
736 if (sidtab_entry_to_string(p, sidtab, oentry, &o, &olen))
737 goto out;
738 if (sidtab_entry_to_string(p, sidtab, nentry, &n, &nlen))
739 goto out;
740 if (sidtab_entry_to_string(p, sidtab, tentry, &t, &tlen))
741 goto out;
742 audit_log(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR,
743 "op=security_validate_transition seresult=denied"
744 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
745 o, n, t, sym_name(p, SYM_CLASSES, tclass-1));
746out:
747 kfree(o);
748 kfree(n);
749 kfree(t);
750
751 if (!enforcing_enabled(state))
752 return 0;
753 return -EPERM;
754}
755
756static int security_compute_validatetrans(struct selinux_state *state,
757 u32 oldsid, u32 newsid, u32 tasksid,
758 u16 orig_tclass, bool user)
759{
760 struct selinux_policy *policy;
761 struct policydb *policydb;
762 struct sidtab *sidtab;
763 struct sidtab_entry *oentry;
764 struct sidtab_entry *nentry;
765 struct sidtab_entry *tentry;
766 struct class_datum *tclass_datum;
767 struct constraint_node *constraint;
768 u16 tclass;
769 int rc = 0;
770
771
772 if (!selinux_initialized(state))
773 return 0;
774
775 rcu_read_lock();
776
777 policy = rcu_dereference(state->policy);
778 policydb = &policy->policydb;
779 sidtab = policy->sidtab;
780
781 if (!user)
782 tclass = unmap_class(&policy->map, orig_tclass);
783 else
784 tclass = orig_tclass;
785
786 if (!tclass || tclass > policydb->p_classes.nprim) {
787 rc = -EINVAL;
788 goto out;
789 }
790 tclass_datum = policydb->class_val_to_struct[tclass - 1];
791
792 oentry = sidtab_search_entry(sidtab, oldsid);
793 if (!oentry) {
794 pr_err("SELinux: %s: unrecognized SID %d\n",
795 __func__, oldsid);
796 rc = -EINVAL;
797 goto out;
798 }
799
800 nentry = sidtab_search_entry(sidtab, newsid);
801 if (!nentry) {
802 pr_err("SELinux: %s: unrecognized SID %d\n",
803 __func__, newsid);
804 rc = -EINVAL;
805 goto out;
806 }
807
808 tentry = sidtab_search_entry(sidtab, tasksid);
809 if (!tentry) {
810 pr_err("SELinux: %s: unrecognized SID %d\n",
811 __func__, tasksid);
812 rc = -EINVAL;
813 goto out;
814 }
815
816 constraint = tclass_datum->validatetrans;
817 while (constraint) {
818 if (!constraint_expr_eval(policydb, &oentry->context,
819 &nentry->context, &tentry->context,
820 constraint->expr)) {
821 if (user)
822 rc = -EPERM;
823 else
824 rc = security_validtrans_handle_fail(state,
825 policy,
826 oentry,
827 nentry,
828 tentry,
829 tclass);
830 goto out;
831 }
832 constraint = constraint->next;
833 }
834
835out:
836 rcu_read_unlock();
837 return rc;
838}
839
840int security_validate_transition_user(struct selinux_state *state,
841 u32 oldsid, u32 newsid, u32 tasksid,
842 u16 tclass)
843{
844 return security_compute_validatetrans(state, oldsid, newsid, tasksid,
845 tclass, true);
846}
847
848int security_validate_transition(struct selinux_state *state,
849 u32 oldsid, u32 newsid, u32 tasksid,
850 u16 orig_tclass)
851{
852 return security_compute_validatetrans(state, oldsid, newsid, tasksid,
853 orig_tclass, false);
854}
855
856
857
858
859
860
861
862
863
864
865
866int security_bounded_transition(struct selinux_state *state,
867 u32 old_sid, u32 new_sid)
868{
869 struct selinux_policy *policy;
870 struct policydb *policydb;
871 struct sidtab *sidtab;
872 struct sidtab_entry *old_entry, *new_entry;
873 struct type_datum *type;
874 int index;
875 int rc;
876
877 if (!selinux_initialized(state))
878 return 0;
879
880 rcu_read_lock();
881 policy = rcu_dereference(state->policy);
882 policydb = &policy->policydb;
883 sidtab = policy->sidtab;
884
885 rc = -EINVAL;
886 old_entry = sidtab_search_entry(sidtab, old_sid);
887 if (!old_entry) {
888 pr_err("SELinux: %s: unrecognized SID %u\n",
889 __func__, old_sid);
890 goto out;
891 }
892
893 rc = -EINVAL;
894 new_entry = sidtab_search_entry(sidtab, new_sid);
895 if (!new_entry) {
896 pr_err("SELinux: %s: unrecognized SID %u\n",
897 __func__, new_sid);
898 goto out;
899 }
900
901 rc = 0;
902
903 if (old_entry->context.type == new_entry->context.type)
904 goto out;
905
906 index = new_entry->context.type;
907 while (true) {
908 type = policydb->type_val_to_struct[index - 1];
909 BUG_ON(!type);
910
911
912 rc = -EPERM;
913 if (!type->bounds)
914 break;
915
916
917 rc = 0;
918 if (type->bounds == old_entry->context.type)
919 break;
920
921 index = type->bounds;
922 }
923
924 if (rc) {
925 char *old_name = NULL;
926 char *new_name = NULL;
927 u32 length;
928
929 if (!sidtab_entry_to_string(policydb, sidtab, old_entry,
930 &old_name, &length) &&
931 !sidtab_entry_to_string(policydb, sidtab, new_entry,
932 &new_name, &length)) {
933 audit_log(audit_context(),
934 GFP_ATOMIC, AUDIT_SELINUX_ERR,
935 "op=security_bounded_transition "
936 "seresult=denied "
937 "oldcontext=%s newcontext=%s",
938 old_name, new_name);
939 }
940 kfree(new_name);
941 kfree(old_name);
942 }
943out:
944 rcu_read_unlock();
945
946 return rc;
947}
948
949static void avd_init(struct selinux_policy *policy, struct av_decision *avd)
950{
951 avd->allowed = 0;
952 avd->auditallow = 0;
953 avd->auditdeny = 0xffffffff;
954 if (policy)
955 avd->seqno = policy->latest_granting;
956 else
957 avd->seqno = 0;
958 avd->flags = 0;
959}
960
961void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
962 struct avtab_node *node)
963{
964 unsigned int i;
965
966 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
967 if (xpermd->driver != node->datum.u.xperms->driver)
968 return;
969 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
970 if (!security_xperm_test(node->datum.u.xperms->perms.p,
971 xpermd->driver))
972 return;
973 } else {
974 BUG();
975 }
976
977 if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
978 xpermd->used |= XPERMS_ALLOWED;
979 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
980 memset(xpermd->allowed->p, 0xff,
981 sizeof(xpermd->allowed->p));
982 }
983 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
984 for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
985 xpermd->allowed->p[i] |=
986 node->datum.u.xperms->perms.p[i];
987 }
988 } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
989 xpermd->used |= XPERMS_AUDITALLOW;
990 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
991 memset(xpermd->auditallow->p, 0xff,
992 sizeof(xpermd->auditallow->p));
993 }
994 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
995 for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
996 xpermd->auditallow->p[i] |=
997 node->datum.u.xperms->perms.p[i];
998 }
999 } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
1000 xpermd->used |= XPERMS_DONTAUDIT;
1001 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
1002 memset(xpermd->dontaudit->p, 0xff,
1003 sizeof(xpermd->dontaudit->p));
1004 }
1005 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
1006 for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
1007 xpermd->dontaudit->p[i] |=
1008 node->datum.u.xperms->perms.p[i];
1009 }
1010 } else {
1011 BUG();
1012 }
1013}
1014
1015void security_compute_xperms_decision(struct selinux_state *state,
1016 u32 ssid,
1017 u32 tsid,
1018 u16 orig_tclass,
1019 u8 driver,
1020 struct extended_perms_decision *xpermd)
1021{
1022 struct selinux_policy *policy;
1023 struct policydb *policydb;
1024 struct sidtab *sidtab;
1025 u16 tclass;
1026 struct context *scontext, *tcontext;
1027 struct avtab_key avkey;
1028 struct avtab_node *node;
1029 struct ebitmap *sattr, *tattr;
1030 struct ebitmap_node *snode, *tnode;
1031 unsigned int i, j;
1032
1033 xpermd->driver = driver;
1034 xpermd->used = 0;
1035 memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
1036 memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
1037 memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
1038
1039 rcu_read_lock();
1040 if (!selinux_initialized(state))
1041 goto allow;
1042
1043 policy = rcu_dereference(state->policy);
1044 policydb = &policy->policydb;
1045 sidtab = policy->sidtab;
1046
1047 scontext = sidtab_search(sidtab, ssid);
1048 if (!scontext) {
1049 pr_err("SELinux: %s: unrecognized SID %d\n",
1050 __func__, ssid);
1051 goto out;
1052 }
1053
1054 tcontext = sidtab_search(sidtab, tsid);
1055 if (!tcontext) {
1056 pr_err("SELinux: %s: unrecognized SID %d\n",
1057 __func__, tsid);
1058 goto out;
1059 }
1060
1061 tclass = unmap_class(&policy->map, orig_tclass);
1062 if (unlikely(orig_tclass && !tclass)) {
1063 if (policydb->allow_unknown)
1064 goto allow;
1065 goto out;
1066 }
1067
1068
1069 if (unlikely(!tclass || tclass > policydb->p_classes.nprim)) {
1070 pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
1071 goto out;
1072 }
1073
1074 avkey.target_class = tclass;
1075 avkey.specified = AVTAB_XPERMS;
1076 sattr = &policydb->type_attr_map_array[scontext->type - 1];
1077 tattr = &policydb->type_attr_map_array[tcontext->type - 1];
1078 ebitmap_for_each_positive_bit(sattr, snode, i) {
1079 ebitmap_for_each_positive_bit(tattr, tnode, j) {
1080 avkey.source_type = i + 1;
1081 avkey.target_type = j + 1;
1082 for (node = avtab_search_node(&policydb->te_avtab,
1083 &avkey);
1084 node;
1085 node = avtab_search_node_next(node, avkey.specified))
1086 services_compute_xperms_decision(xpermd, node);
1087
1088 cond_compute_xperms(&policydb->te_cond_avtab,
1089 &avkey, xpermd);
1090 }
1091 }
1092out:
1093 rcu_read_unlock();
1094 return;
1095allow:
1096 memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
1097 goto out;
1098}
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112void security_compute_av(struct selinux_state *state,
1113 u32 ssid,
1114 u32 tsid,
1115 u16 orig_tclass,
1116 struct av_decision *avd,
1117 struct extended_perms *xperms)
1118{
1119 struct selinux_policy *policy;
1120 struct policydb *policydb;
1121 struct sidtab *sidtab;
1122 u16 tclass;
1123 struct context *scontext = NULL, *tcontext = NULL;
1124
1125 rcu_read_lock();
1126 policy = rcu_dereference(state->policy);
1127 avd_init(policy, avd);
1128 xperms->len = 0;
1129 if (!selinux_initialized(state))
1130 goto allow;
1131
1132 policydb = &policy->policydb;
1133 sidtab = policy->sidtab;
1134
1135 scontext = sidtab_search(sidtab, ssid);
1136 if (!scontext) {
1137 pr_err("SELinux: %s: unrecognized SID %d\n",
1138 __func__, ssid);
1139 goto out;
1140 }
1141
1142
1143 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type))
1144 avd->flags |= AVD_FLAGS_PERMISSIVE;
1145
1146 tcontext = sidtab_search(sidtab, tsid);
1147 if (!tcontext) {
1148 pr_err("SELinux: %s: unrecognized SID %d\n",
1149 __func__, tsid);
1150 goto out;
1151 }
1152
1153 tclass = unmap_class(&policy->map, orig_tclass);
1154 if (unlikely(orig_tclass && !tclass)) {
1155 if (policydb->allow_unknown)
1156 goto allow;
1157 goto out;
1158 }
1159 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd,
1160 xperms);
1161 map_decision(&policy->map, orig_tclass, avd,
1162 policydb->allow_unknown);
1163out:
1164 rcu_read_unlock();
1165 return;
1166allow:
1167 avd->allowed = 0xffffffff;
1168 goto out;
1169}
1170
1171void security_compute_av_user(struct selinux_state *state,
1172 u32 ssid,
1173 u32 tsid,
1174 u16 tclass,
1175 struct av_decision *avd)
1176{
1177 struct selinux_policy *policy;
1178 struct policydb *policydb;
1179 struct sidtab *sidtab;
1180 struct context *scontext = NULL, *tcontext = NULL;
1181
1182 rcu_read_lock();
1183 policy = rcu_dereference(state->policy);
1184 avd_init(policy, avd);
1185 if (!selinux_initialized(state))
1186 goto allow;
1187
1188 policydb = &policy->policydb;
1189 sidtab = policy->sidtab;
1190
1191 scontext = sidtab_search(sidtab, ssid);
1192 if (!scontext) {
1193 pr_err("SELinux: %s: unrecognized SID %d\n",
1194 __func__, ssid);
1195 goto out;
1196 }
1197
1198
1199 if (ebitmap_get_bit(&policydb->permissive_map, scontext->type))
1200 avd->flags |= AVD_FLAGS_PERMISSIVE;
1201
1202 tcontext = sidtab_search(sidtab, tsid);
1203 if (!tcontext) {
1204 pr_err("SELinux: %s: unrecognized SID %d\n",
1205 __func__, tsid);
1206 goto out;
1207 }
1208
1209 if (unlikely(!tclass)) {
1210 if (policydb->allow_unknown)
1211 goto allow;
1212 goto out;
1213 }
1214
1215 context_struct_compute_av(policydb, scontext, tcontext, tclass, avd,
1216 NULL);
1217 out:
1218 rcu_read_unlock();
1219 return;
1220allow:
1221 avd->allowed = 0xffffffff;
1222 goto out;
1223}
1224
1225
1226
1227
1228
1229
1230
1231
1232static int context_struct_to_string(struct policydb *p,
1233 struct context *context,
1234 char **scontext, u32 *scontext_len)
1235{
1236 char *scontextp;
1237
1238 if (scontext)
1239 *scontext = NULL;
1240 *scontext_len = 0;
1241
1242 if (context->len) {
1243 *scontext_len = context->len;
1244 if (scontext) {
1245 *scontext = kstrdup(context->str, GFP_ATOMIC);
1246 if (!(*scontext))
1247 return -ENOMEM;
1248 }
1249 return 0;
1250 }
1251
1252
1253 *scontext_len += strlen(sym_name(p, SYM_USERS, context->user - 1)) + 1;
1254 *scontext_len += strlen(sym_name(p, SYM_ROLES, context->role - 1)) + 1;
1255 *scontext_len += strlen(sym_name(p, SYM_TYPES, context->type - 1)) + 1;
1256 *scontext_len += mls_compute_context_len(p, context);
1257
1258 if (!scontext)
1259 return 0;
1260
1261
1262 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
1263 if (!scontextp)
1264 return -ENOMEM;
1265 *scontext = scontextp;
1266
1267
1268
1269
1270 scontextp += sprintf(scontextp, "%s:%s:%s",
1271 sym_name(p, SYM_USERS, context->user - 1),
1272 sym_name(p, SYM_ROLES, context->role - 1),
1273 sym_name(p, SYM_TYPES, context->type - 1));
1274
1275 mls_sid_to_context(p, context, &scontextp);
1276
1277 *scontextp = 0;
1278
1279 return 0;
1280}
1281
1282static int sidtab_entry_to_string(struct policydb *p,
1283 struct sidtab *sidtab,
1284 struct sidtab_entry *entry,
1285 char **scontext, u32 *scontext_len)
1286{
1287 int rc = sidtab_sid2str_get(sidtab, entry, scontext, scontext_len);
1288
1289 if (rc != -ENOENT)
1290 return rc;
1291
1292 rc = context_struct_to_string(p, &entry->context, scontext,
1293 scontext_len);
1294 if (!rc && scontext)
1295 sidtab_sid2str_put(sidtab, entry, *scontext, *scontext_len);
1296 return rc;
1297}
1298
1299#include "initial_sid_to_string.h"
1300
1301int security_sidtab_hash_stats(struct selinux_state *state, char *page)
1302{
1303 struct selinux_policy *policy;
1304 int rc;
1305
1306 if (!selinux_initialized(state)) {
1307 pr_err("SELinux: %s: called before initial load_policy\n",
1308 __func__);
1309 return -EINVAL;
1310 }
1311
1312 rcu_read_lock();
1313 policy = rcu_dereference(state->policy);
1314 rc = sidtab_hash_stats(policy->sidtab, page);
1315 rcu_read_unlock();
1316
1317 return rc;
1318}
1319
1320const char *security_get_initial_sid_context(u32 sid)
1321{
1322 if (unlikely(sid > SECINITSID_NUM))
1323 return NULL;
1324 return initial_sid_to_string[sid];
1325}
1326
1327static int security_sid_to_context_core(struct selinux_state *state,
1328 u32 sid, char **scontext,
1329 u32 *scontext_len, int force,
1330 int only_invalid)
1331{
1332 struct selinux_policy *policy;
1333 struct policydb *policydb;
1334 struct sidtab *sidtab;
1335 struct sidtab_entry *entry;
1336 int rc = 0;
1337
1338 if (scontext)
1339 *scontext = NULL;
1340 *scontext_len = 0;
1341
1342 if (!selinux_initialized(state)) {
1343 if (sid <= SECINITSID_NUM) {
1344 char *scontextp;
1345 const char *s = initial_sid_to_string[sid];
1346
1347 if (!s)
1348 return -EINVAL;
1349 *scontext_len = strlen(s) + 1;
1350 if (!scontext)
1351 return 0;
1352 scontextp = kmemdup(s, *scontext_len, GFP_ATOMIC);
1353 if (!scontextp)
1354 return -ENOMEM;
1355 *scontext = scontextp;
1356 return 0;
1357 }
1358 pr_err("SELinux: %s: called before initial "
1359 "load_policy on unknown SID %d\n", __func__, sid);
1360 return -EINVAL;
1361 }
1362 rcu_read_lock();
1363 policy = rcu_dereference(state->policy);
1364 policydb = &policy->policydb;
1365 sidtab = policy->sidtab;
1366
1367 if (force)
1368 entry = sidtab_search_entry_force(sidtab, sid);
1369 else
1370 entry = sidtab_search_entry(sidtab, sid);
1371 if (!entry) {
1372 pr_err("SELinux: %s: unrecognized SID %d\n",
1373 __func__, sid);
1374 rc = -EINVAL;
1375 goto out_unlock;
1376 }
1377 if (only_invalid && !entry->context.len)
1378 goto out_unlock;
1379
1380 rc = sidtab_entry_to_string(policydb, sidtab, entry, scontext,
1381 scontext_len);
1382
1383out_unlock:
1384 rcu_read_unlock();
1385 return rc;
1386
1387}
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400int security_sid_to_context(struct selinux_state *state,
1401 u32 sid, char **scontext, u32 *scontext_len)
1402{
1403 return security_sid_to_context_core(state, sid, scontext,
1404 scontext_len, 0, 0);
1405}
1406
1407int security_sid_to_context_force(struct selinux_state *state, u32 sid,
1408 char **scontext, u32 *scontext_len)
1409{
1410 return security_sid_to_context_core(state, sid, scontext,
1411 scontext_len, 1, 0);
1412}
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428int security_sid_to_context_inval(struct selinux_state *state, u32 sid,
1429 char **scontext, u32 *scontext_len)
1430{
1431 return security_sid_to_context_core(state, sid, scontext,
1432 scontext_len, 1, 1);
1433}
1434
1435
1436
1437
1438static int string_to_context_struct(struct policydb *pol,
1439 struct sidtab *sidtabp,
1440 char *scontext,
1441 struct context *ctx,
1442 u32 def_sid)
1443{
1444 struct role_datum *role;
1445 struct type_datum *typdatum;
1446 struct user_datum *usrdatum;
1447 char *scontextp, *p, oldc;
1448 int rc = 0;
1449
1450 context_init(ctx);
1451
1452
1453
1454 rc = -EINVAL;
1455 scontextp = (char *) scontext;
1456
1457
1458 p = scontextp;
1459 while (*p && *p != ':')
1460 p++;
1461
1462 if (*p == 0)
1463 goto out;
1464
1465 *p++ = 0;
1466
1467 usrdatum = symtab_search(&pol->p_users, scontextp);
1468 if (!usrdatum)
1469 goto out;
1470
1471 ctx->user = usrdatum->value;
1472
1473
1474 scontextp = p;
1475 while (*p && *p != ':')
1476 p++;
1477
1478 if (*p == 0)
1479 goto out;
1480
1481 *p++ = 0;
1482
1483 role = symtab_search(&pol->p_roles, scontextp);
1484 if (!role)
1485 goto out;
1486 ctx->role = role->value;
1487
1488
1489 scontextp = p;
1490 while (*p && *p != ':')
1491 p++;
1492 oldc = *p;
1493 *p++ = 0;
1494
1495 typdatum = symtab_search(&pol->p_types, scontextp);
1496 if (!typdatum || typdatum->attribute)
1497 goto out;
1498
1499 ctx->type = typdatum->value;
1500
1501 rc = mls_context_to_sid(pol, oldc, p, ctx, sidtabp, def_sid);
1502 if (rc)
1503 goto out;
1504
1505
1506 rc = -EINVAL;
1507 if (!policydb_context_isvalid(pol, ctx))
1508 goto out;
1509 rc = 0;
1510out:
1511 if (rc)
1512 context_destroy(ctx);
1513 return rc;
1514}
1515
1516static int security_context_to_sid_core(struct selinux_state *state,
1517 const char *scontext, u32 scontext_len,
1518 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1519 int force)
1520{
1521 struct selinux_policy *policy;
1522 struct policydb *policydb;
1523 struct sidtab *sidtab;
1524 char *scontext2, *str = NULL;
1525 struct context context;
1526 int rc = 0;
1527
1528
1529 if (!scontext_len)
1530 return -EINVAL;
1531
1532
1533 scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags);
1534 if (!scontext2)
1535 return -ENOMEM;
1536
1537 if (!selinux_initialized(state)) {
1538 int i;
1539
1540 for (i = 1; i < SECINITSID_NUM; i++) {
1541 const char *s = initial_sid_to_string[i];
1542
1543 if (s && !strcmp(s, scontext2)) {
1544 *sid = i;
1545 goto out;
1546 }
1547 }
1548 *sid = SECINITSID_KERNEL;
1549 goto out;
1550 }
1551 *sid = SECSID_NULL;
1552
1553 if (force) {
1554
1555 rc = -ENOMEM;
1556 str = kstrdup(scontext2, gfp_flags);
1557 if (!str)
1558 goto out;
1559 }
1560retry:
1561 rcu_read_lock();
1562 policy = rcu_dereference(state->policy);
1563 policydb = &policy->policydb;
1564 sidtab = policy->sidtab;
1565 rc = string_to_context_struct(policydb, sidtab, scontext2,
1566 &context, def_sid);
1567 if (rc == -EINVAL && force) {
1568 context.str = str;
1569 context.len = strlen(str) + 1;
1570 str = NULL;
1571 } else if (rc)
1572 goto out_unlock;
1573 rc = sidtab_context_to_sid(sidtab, &context, sid);
1574 if (rc == -ESTALE) {
1575 rcu_read_unlock();
1576 if (context.str) {
1577 str = context.str;
1578 context.str = NULL;
1579 }
1580 context_destroy(&context);
1581 goto retry;
1582 }
1583 context_destroy(&context);
1584out_unlock:
1585 rcu_read_unlock();
1586out:
1587 kfree(scontext2);
1588 kfree(str);
1589 return rc;
1590}
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605int security_context_to_sid(struct selinux_state *state,
1606 const char *scontext, u32 scontext_len, u32 *sid,
1607 gfp_t gfp)
1608{
1609 return security_context_to_sid_core(state, scontext, scontext_len,
1610 sid, SECSID_NULL, gfp, 0);
1611}
1612
1613int security_context_str_to_sid(struct selinux_state *state,
1614 const char *scontext, u32 *sid, gfp_t gfp)
1615{
1616 return security_context_to_sid(state, scontext, strlen(scontext),
1617 sid, gfp);
1618}
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639int security_context_to_sid_default(struct selinux_state *state,
1640 const char *scontext, u32 scontext_len,
1641 u32 *sid, u32 def_sid, gfp_t gfp_flags)
1642{
1643 return security_context_to_sid_core(state, scontext, scontext_len,
1644 sid, def_sid, gfp_flags, 1);
1645}
1646
1647int security_context_to_sid_force(struct selinux_state *state,
1648 const char *scontext, u32 scontext_len,
1649 u32 *sid)
1650{
1651 return security_context_to_sid_core(state, scontext, scontext_len,
1652 sid, SECSID_NULL, GFP_KERNEL, 1);
1653}
1654
1655static int compute_sid_handle_invalid_context(
1656 struct selinux_state *state,
1657 struct selinux_policy *policy,
1658 struct sidtab_entry *sentry,
1659 struct sidtab_entry *tentry,
1660 u16 tclass,
1661 struct context *newcontext)
1662{
1663 struct policydb *policydb = &policy->policydb;
1664 struct sidtab *sidtab = policy->sidtab;
1665 char *s = NULL, *t = NULL, *n = NULL;
1666 u32 slen, tlen, nlen;
1667 struct audit_buffer *ab;
1668
1669 if (sidtab_entry_to_string(policydb, sidtab, sentry, &s, &slen))
1670 goto out;
1671 if (sidtab_entry_to_string(policydb, sidtab, tentry, &t, &tlen))
1672 goto out;
1673 if (context_struct_to_string(policydb, newcontext, &n, &nlen))
1674 goto out;
1675 ab = audit_log_start(audit_context(), GFP_ATOMIC, AUDIT_SELINUX_ERR);
1676 if (!ab)
1677 goto out;
1678 audit_log_format(ab,
1679 "op=security_compute_sid invalid_context=");
1680
1681 audit_log_n_untrustedstring(ab, n, nlen - 1);
1682 audit_log_format(ab, " scontext=%s tcontext=%s tclass=%s",
1683 s, t, sym_name(policydb, SYM_CLASSES, tclass-1));
1684 audit_log_end(ab);
1685out:
1686 kfree(s);
1687 kfree(t);
1688 kfree(n);
1689 if (!enforcing_enabled(state))
1690 return 0;
1691 return -EACCES;
1692}
1693
1694static void filename_compute_type(struct policydb *policydb,
1695 struct context *newcontext,
1696 u32 stype, u32 ttype, u16 tclass,
1697 const char *objname)
1698{
1699 struct filename_trans_key ft;
1700 struct filename_trans_datum *datum;
1701
1702
1703
1704
1705
1706
1707 if (!ebitmap_get_bit(&policydb->filename_trans_ttypes, ttype))
1708 return;
1709
1710 ft.ttype = ttype;
1711 ft.tclass = tclass;
1712 ft.name = objname;
1713
1714 datum = policydb_filenametr_search(policydb, &ft);
1715 while (datum) {
1716 if (ebitmap_get_bit(&datum->stypes, stype - 1)) {
1717 newcontext->type = datum->otype;
1718 return;
1719 }
1720 datum = datum->next;
1721 }
1722}
1723
1724static int security_compute_sid(struct selinux_state *state,
1725 u32 ssid,
1726 u32 tsid,
1727 u16 orig_tclass,
1728 u32 specified,
1729 const char *objname,
1730 u32 *out_sid,
1731 bool kern)
1732{
1733 struct selinux_policy *policy;
1734 struct policydb *policydb;
1735 struct sidtab *sidtab;
1736 struct class_datum *cladatum;
1737 struct context *scontext, *tcontext, newcontext;
1738 struct sidtab_entry *sentry, *tentry;
1739 struct avtab_key avkey;
1740 struct avtab_datum *avdatum;
1741 struct avtab_node *node;
1742 u16 tclass;
1743 int rc = 0;
1744 bool sock;
1745
1746 if (!selinux_initialized(state)) {
1747 switch (orig_tclass) {
1748 case SECCLASS_PROCESS:
1749 *out_sid = ssid;
1750 break;
1751 default:
1752 *out_sid = tsid;
1753 break;
1754 }
1755 goto out;
1756 }
1757
1758retry:
1759 cladatum = NULL;
1760 context_init(&newcontext);
1761
1762 rcu_read_lock();
1763
1764 policy = rcu_dereference(state->policy);
1765
1766 if (kern) {
1767 tclass = unmap_class(&policy->map, orig_tclass);
1768 sock = security_is_socket_class(orig_tclass);
1769 } else {
1770 tclass = orig_tclass;
1771 sock = security_is_socket_class(map_class(&policy->map,
1772 tclass));
1773 }
1774
1775 policydb = &policy->policydb;
1776 sidtab = policy->sidtab;
1777
1778 sentry = sidtab_search_entry(sidtab, ssid);
1779 if (!sentry) {
1780 pr_err("SELinux: %s: unrecognized SID %d\n",
1781 __func__, ssid);
1782 rc = -EINVAL;
1783 goto out_unlock;
1784 }
1785 tentry = sidtab_search_entry(sidtab, tsid);
1786 if (!tentry) {
1787 pr_err("SELinux: %s: unrecognized SID %d\n",
1788 __func__, tsid);
1789 rc = -EINVAL;
1790 goto out_unlock;
1791 }
1792
1793 scontext = &sentry->context;
1794 tcontext = &tentry->context;
1795
1796 if (tclass && tclass <= policydb->p_classes.nprim)
1797 cladatum = policydb->class_val_to_struct[tclass - 1];
1798
1799
1800 switch (specified) {
1801 case AVTAB_TRANSITION:
1802 case AVTAB_CHANGE:
1803 if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
1804 newcontext.user = tcontext->user;
1805 } else {
1806
1807
1808 newcontext.user = scontext->user;
1809 }
1810 break;
1811 case AVTAB_MEMBER:
1812
1813 newcontext.user = tcontext->user;
1814 break;
1815 }
1816
1817
1818 if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
1819 newcontext.role = scontext->role;
1820 } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
1821 newcontext.role = tcontext->role;
1822 } else {
1823 if ((tclass == policydb->process_class) || sock)
1824 newcontext.role = scontext->role;
1825 else
1826 newcontext.role = OBJECT_R_VAL;
1827 }
1828
1829
1830 if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
1831 newcontext.type = scontext->type;
1832 } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
1833 newcontext.type = tcontext->type;
1834 } else {
1835 if ((tclass == policydb->process_class) || sock) {
1836
1837 newcontext.type = scontext->type;
1838 } else {
1839
1840 newcontext.type = tcontext->type;
1841 }
1842 }
1843
1844
1845 avkey.source_type = scontext->type;
1846 avkey.target_type = tcontext->type;
1847 avkey.target_class = tclass;
1848 avkey.specified = specified;
1849 avdatum = avtab_search(&policydb->te_avtab, &avkey);
1850
1851
1852 if (!avdatum) {
1853 node = avtab_search_node(&policydb->te_cond_avtab, &avkey);
1854 for (; node; node = avtab_search_node_next(node, specified)) {
1855 if (node->key.specified & AVTAB_ENABLED) {
1856 avdatum = &node->datum;
1857 break;
1858 }
1859 }
1860 }
1861
1862 if (avdatum) {
1863
1864 newcontext.type = avdatum->u.data;
1865 }
1866
1867
1868 if (objname)
1869 filename_compute_type(policydb, &newcontext, scontext->type,
1870 tcontext->type, tclass, objname);
1871
1872
1873 if (specified & AVTAB_TRANSITION) {
1874
1875 struct role_trans_datum *rtd;
1876 struct role_trans_key rtk = {
1877 .role = scontext->role,
1878 .type = tcontext->type,
1879 .tclass = tclass,
1880 };
1881
1882 rtd = policydb_roletr_search(policydb, &rtk);
1883 if (rtd)
1884 newcontext.role = rtd->new_role;
1885 }
1886
1887
1888
1889 rc = mls_compute_sid(policydb, scontext, tcontext, tclass, specified,
1890 &newcontext, sock);
1891 if (rc)
1892 goto out_unlock;
1893
1894
1895 if (!policydb_context_isvalid(policydb, &newcontext)) {
1896 rc = compute_sid_handle_invalid_context(state, policy, sentry,
1897 tentry, tclass,
1898 &newcontext);
1899 if (rc)
1900 goto out_unlock;
1901 }
1902
1903 rc = sidtab_context_to_sid(sidtab, &newcontext, out_sid);
1904 if (rc == -ESTALE) {
1905 rcu_read_unlock();
1906 context_destroy(&newcontext);
1907 goto retry;
1908 }
1909out_unlock:
1910 rcu_read_unlock();
1911 context_destroy(&newcontext);
1912out:
1913 return rc;
1914}
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930int security_transition_sid(struct selinux_state *state,
1931 u32 ssid, u32 tsid, u16 tclass,
1932 const struct qstr *qstr, u32 *out_sid)
1933{
1934 return security_compute_sid(state, ssid, tsid, tclass,
1935 AVTAB_TRANSITION,
1936 qstr ? qstr->name : NULL, out_sid, true);
1937}
1938
1939int security_transition_sid_user(struct selinux_state *state,
1940 u32 ssid, u32 tsid, u16 tclass,
1941 const char *objname, u32 *out_sid)
1942{
1943 return security_compute_sid(state, ssid, tsid, tclass,
1944 AVTAB_TRANSITION,
1945 objname, out_sid, false);
1946}
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961int security_member_sid(struct selinux_state *state,
1962 u32 ssid,
1963 u32 tsid,
1964 u16 tclass,
1965 u32 *out_sid)
1966{
1967 return security_compute_sid(state, ssid, tsid, tclass,
1968 AVTAB_MEMBER, NULL,
1969 out_sid, false);
1970}
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986int security_change_sid(struct selinux_state *state,
1987 u32 ssid,
1988 u32 tsid,
1989 u16 tclass,
1990 u32 *out_sid)
1991{
1992 return security_compute_sid(state,
1993 ssid, tsid, tclass, AVTAB_CHANGE, NULL,
1994 out_sid, false);
1995}
1996
1997static inline int convert_context_handle_invalid_context(
1998 struct selinux_state *state,
1999 struct policydb *policydb,
2000 struct context *context)
2001{
2002 char *s;
2003 u32 len;
2004
2005 if (enforcing_enabled(state))
2006 return -EINVAL;
2007
2008 if (!context_struct_to_string(policydb, context, &s, &len)) {
2009 pr_warn("SELinux: Context %s would be invalid if enforcing\n",
2010 s);
2011 kfree(s);
2012 }
2013 return 0;
2014}
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024static int convert_context(struct context *oldc, struct context *newc, void *p)
2025{
2026 struct convert_context_args *args;
2027 struct ocontext *oc;
2028 struct role_datum *role;
2029 struct type_datum *typdatum;
2030 struct user_datum *usrdatum;
2031 char *s;
2032 u32 len;
2033 int rc;
2034
2035 args = p;
2036
2037 if (oldc->str) {
2038 s = kstrdup(oldc->str, GFP_KERNEL);
2039 if (!s)
2040 return -ENOMEM;
2041
2042 rc = string_to_context_struct(args->newp, NULL, s,
2043 newc, SECSID_NULL);
2044 if (rc == -EINVAL) {
2045
2046
2047
2048
2049
2050
2051
2052 memcpy(s, oldc->str, oldc->len);
2053 context_init(newc);
2054 newc->str = s;
2055 newc->len = oldc->len;
2056 return 0;
2057 }
2058 kfree(s);
2059 if (rc) {
2060
2061 pr_err("SELinux: Unable to map context %s, rc = %d.\n",
2062 oldc->str, -rc);
2063 return rc;
2064 }
2065 pr_info("SELinux: Context %s became valid (mapped).\n",
2066 oldc->str);
2067 return 0;
2068 }
2069
2070 context_init(newc);
2071
2072
2073 usrdatum = symtab_search(&args->newp->p_users,
2074 sym_name(args->oldp,
2075 SYM_USERS, oldc->user - 1));
2076 if (!usrdatum)
2077 goto bad;
2078 newc->user = usrdatum->value;
2079
2080
2081 role = symtab_search(&args->newp->p_roles,
2082 sym_name(args->oldp, SYM_ROLES, oldc->role - 1));
2083 if (!role)
2084 goto bad;
2085 newc->role = role->value;
2086
2087
2088 typdatum = symtab_search(&args->newp->p_types,
2089 sym_name(args->oldp,
2090 SYM_TYPES, oldc->type - 1));
2091 if (!typdatum)
2092 goto bad;
2093 newc->type = typdatum->value;
2094
2095
2096 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
2097 rc = mls_convert_context(args->oldp, args->newp, oldc, newc);
2098 if (rc)
2099 goto bad;
2100 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
2101
2102
2103
2104
2105
2106
2107
2108 oc = args->newp->ocontexts[OCON_ISID];
2109 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
2110 oc = oc->next;
2111 if (!oc) {
2112 pr_err("SELinux: unable to look up"
2113 " the initial SIDs list\n");
2114 goto bad;
2115 }
2116 rc = mls_range_set(newc, &oc->context[0].range);
2117 if (rc)
2118 goto bad;
2119 }
2120
2121
2122 if (!policydb_context_isvalid(args->newp, newc)) {
2123 rc = convert_context_handle_invalid_context(args->state,
2124 args->oldp,
2125 oldc);
2126 if (rc)
2127 goto bad;
2128 }
2129
2130 return 0;
2131bad:
2132
2133 rc = context_struct_to_string(args->oldp, oldc, &s, &len);
2134 if (rc)
2135 return rc;
2136 context_destroy(newc);
2137 newc->str = s;
2138 newc->len = len;
2139 pr_info("SELinux: Context %s became invalid (unmapped).\n",
2140 newc->str);
2141 return 0;
2142}
2143
2144static void security_load_policycaps(struct selinux_state *state,
2145 struct selinux_policy *policy)
2146{
2147 struct policydb *p;
2148 unsigned int i;
2149 struct ebitmap_node *node;
2150
2151 p = &policy->policydb;
2152
2153 for (i = 0; i < ARRAY_SIZE(state->policycap); i++)
2154 WRITE_ONCE(state->policycap[i],
2155 ebitmap_get_bit(&p->policycaps, i));
2156
2157 for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
2158 pr_info("SELinux: policy capability %s=%d\n",
2159 selinux_policycap_names[i],
2160 ebitmap_get_bit(&p->policycaps, i));
2161
2162 ebitmap_for_each_positive_bit(&p->policycaps, node, i) {
2163 if (i >= ARRAY_SIZE(selinux_policycap_names))
2164 pr_info("SELinux: unknown policy capability %u\n",
2165 i);
2166 }
2167}
2168
2169static int security_preserve_bools(struct selinux_policy *oldpolicy,
2170 struct selinux_policy *newpolicy);
2171
2172static void selinux_policy_free(struct selinux_policy *policy)
2173{
2174 if (!policy)
2175 return;
2176
2177 sidtab_destroy(policy->sidtab);
2178 kfree(policy->map.mapping);
2179 policydb_destroy(&policy->policydb);
2180 kfree(policy->sidtab);
2181 kfree(policy);
2182}
2183
2184static void selinux_policy_cond_free(struct selinux_policy *policy)
2185{
2186 cond_policydb_destroy_dup(&policy->policydb);
2187 kfree(policy);
2188}
2189
2190void selinux_policy_cancel(struct selinux_state *state,
2191 struct selinux_load_state *load_state)
2192{
2193 struct selinux_policy *oldpolicy;
2194
2195 oldpolicy = rcu_dereference_protected(state->policy,
2196 lockdep_is_held(&state->policy_mutex));
2197
2198 sidtab_cancel_convert(oldpolicy->sidtab);
2199 selinux_policy_free(load_state->policy);
2200 kfree(load_state->convert_data);
2201}
2202
2203static void selinux_notify_policy_change(struct selinux_state *state,
2204 u32 seqno)
2205{
2206
2207 avc_ss_reset(state->avc, seqno);
2208 selnl_notify_policyload(seqno);
2209 selinux_status_update_policyload(state, seqno);
2210 selinux_netlbl_cache_invalidate();
2211 selinux_xfrm_notify_policyload();
2212 selinux_ima_measure_state_locked(state);
2213}
2214
2215void selinux_policy_commit(struct selinux_state *state,
2216 struct selinux_load_state *load_state)
2217{
2218 struct selinux_policy *oldpolicy, *newpolicy = load_state->policy;
2219 unsigned long flags;
2220 u32 seqno;
2221
2222 oldpolicy = rcu_dereference_protected(state->policy,
2223 lockdep_is_held(&state->policy_mutex));
2224
2225
2226 if (oldpolicy) {
2227 if (oldpolicy->policydb.mls_enabled && !newpolicy->policydb.mls_enabled)
2228 pr_info("SELinux: Disabling MLS support...\n");
2229 else if (!oldpolicy->policydb.mls_enabled && newpolicy->policydb.mls_enabled)
2230 pr_info("SELinux: Enabling MLS support...\n");
2231 }
2232
2233
2234 if (oldpolicy)
2235 newpolicy->latest_granting = oldpolicy->latest_granting + 1;
2236 else
2237 newpolicy->latest_granting = 1;
2238 seqno = newpolicy->latest_granting;
2239
2240
2241 if (oldpolicy) {
2242 sidtab_freeze_begin(oldpolicy->sidtab, &flags);
2243 rcu_assign_pointer(state->policy, newpolicy);
2244 sidtab_freeze_end(oldpolicy->sidtab, &flags);
2245 } else {
2246 rcu_assign_pointer(state->policy, newpolicy);
2247 }
2248
2249
2250 security_load_policycaps(state, newpolicy);
2251
2252 if (!selinux_initialized(state)) {
2253
2254
2255
2256
2257
2258 selinux_mark_initialized(state);
2259 selinux_complete_init();
2260 }
2261
2262
2263 synchronize_rcu();
2264 selinux_policy_free(oldpolicy);
2265 kfree(load_state->convert_data);
2266
2267
2268 selinux_notify_policy_change(state, seqno);
2269}
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282int security_load_policy(struct selinux_state *state, void *data, size_t len,
2283 struct selinux_load_state *load_state)
2284{
2285 struct selinux_policy *newpolicy, *oldpolicy;
2286 struct selinux_policy_convert_data *convert_data;
2287 int rc = 0;
2288 struct policy_file file = { data, len }, *fp = &file;
2289
2290 newpolicy = kzalloc(sizeof(*newpolicy), GFP_KERNEL);
2291 if (!newpolicy)
2292 return -ENOMEM;
2293
2294 newpolicy->sidtab = kzalloc(sizeof(*newpolicy->sidtab), GFP_KERNEL);
2295 if (!newpolicy->sidtab) {
2296 rc = -ENOMEM;
2297 goto err_policy;
2298 }
2299
2300 rc = policydb_read(&newpolicy->policydb, fp);
2301 if (rc)
2302 goto err_sidtab;
2303
2304 newpolicy->policydb.len = len;
2305 rc = selinux_set_mapping(&newpolicy->policydb, secclass_map,
2306 &newpolicy->map);
2307 if (rc)
2308 goto err_policydb;
2309
2310 rc = policydb_load_isids(&newpolicy->policydb, newpolicy->sidtab);
2311 if (rc) {
2312 pr_err("SELinux: unable to load the initial SIDs\n");
2313 goto err_mapping;
2314 }
2315
2316 if (!selinux_initialized(state)) {
2317
2318 load_state->policy = newpolicy;
2319 load_state->convert_data = NULL;
2320 return 0;
2321 }
2322
2323 oldpolicy = rcu_dereference_protected(state->policy,
2324 lockdep_is_held(&state->policy_mutex));
2325
2326
2327 rc = security_preserve_bools(oldpolicy, newpolicy);
2328 if (rc) {
2329 pr_err("SELinux: unable to preserve booleans\n");
2330 goto err_free_isids;
2331 }
2332
2333 convert_data = kmalloc(sizeof(*convert_data), GFP_KERNEL);
2334 if (!convert_data) {
2335 rc = -ENOMEM;
2336 goto err_free_isids;
2337 }
2338
2339
2340
2341
2342
2343 convert_data->args.state = state;
2344 convert_data->args.oldp = &oldpolicy->policydb;
2345 convert_data->args.newp = &newpolicy->policydb;
2346
2347 convert_data->sidtab_params.func = convert_context;
2348 convert_data->sidtab_params.args = &convert_data->args;
2349 convert_data->sidtab_params.target = newpolicy->sidtab;
2350
2351 rc = sidtab_convert(oldpolicy->sidtab, &convert_data->sidtab_params);
2352 if (rc) {
2353 pr_err("SELinux: unable to convert the internal"
2354 " representation of contexts in the new SID"
2355 " table\n");
2356 goto err_free_convert_data;
2357 }
2358
2359 load_state->policy = newpolicy;
2360 load_state->convert_data = convert_data;
2361 return 0;
2362
2363err_free_convert_data:
2364 kfree(convert_data);
2365err_free_isids:
2366 sidtab_destroy(newpolicy->sidtab);
2367err_mapping:
2368 kfree(newpolicy->map.mapping);
2369err_policydb:
2370 policydb_destroy(&newpolicy->policydb);
2371err_sidtab:
2372 kfree(newpolicy->sidtab);
2373err_policy:
2374 kfree(newpolicy);
2375
2376 return rc;
2377}
2378
2379
2380
2381
2382
2383
2384
2385
2386int security_port_sid(struct selinux_state *state,
2387 u8 protocol, u16 port, u32 *out_sid)
2388{
2389 struct selinux_policy *policy;
2390 struct policydb *policydb;
2391 struct sidtab *sidtab;
2392 struct ocontext *c;
2393 int rc;
2394
2395 if (!selinux_initialized(state)) {
2396 *out_sid = SECINITSID_PORT;
2397 return 0;
2398 }
2399
2400retry:
2401 rc = 0;
2402 rcu_read_lock();
2403 policy = rcu_dereference(state->policy);
2404 policydb = &policy->policydb;
2405 sidtab = policy->sidtab;
2406
2407 c = policydb->ocontexts[OCON_PORT];
2408 while (c) {
2409 if (c->u.port.protocol == protocol &&
2410 c->u.port.low_port <= port &&
2411 c->u.port.high_port >= port)
2412 break;
2413 c = c->next;
2414 }
2415
2416 if (c) {
2417 if (!c->sid[0]) {
2418 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2419 &c->sid[0]);
2420 if (rc == -ESTALE) {
2421 rcu_read_unlock();
2422 goto retry;
2423 }
2424 if (rc)
2425 goto out;
2426 }
2427 *out_sid = c->sid[0];
2428 } else {
2429 *out_sid = SECINITSID_PORT;
2430 }
2431
2432out:
2433 rcu_read_unlock();
2434 return rc;
2435}
2436
2437
2438
2439
2440
2441
2442
2443
2444int security_ib_pkey_sid(struct selinux_state *state,
2445 u64 subnet_prefix, u16 pkey_num, u32 *out_sid)
2446{
2447 struct selinux_policy *policy;
2448 struct policydb *policydb;
2449 struct sidtab *sidtab;
2450 struct ocontext *c;
2451 int rc;
2452
2453 if (!selinux_initialized(state)) {
2454 *out_sid = SECINITSID_UNLABELED;
2455 return 0;
2456 }
2457
2458retry:
2459 rc = 0;
2460 rcu_read_lock();
2461 policy = rcu_dereference(state->policy);
2462 policydb = &policy->policydb;
2463 sidtab = policy->sidtab;
2464
2465 c = policydb->ocontexts[OCON_IBPKEY];
2466 while (c) {
2467 if (c->u.ibpkey.low_pkey <= pkey_num &&
2468 c->u.ibpkey.high_pkey >= pkey_num &&
2469 c->u.ibpkey.subnet_prefix == subnet_prefix)
2470 break;
2471
2472 c = c->next;
2473 }
2474
2475 if (c) {
2476 if (!c->sid[0]) {
2477 rc = sidtab_context_to_sid(sidtab,
2478 &c->context[0],
2479 &c->sid[0]);
2480 if (rc == -ESTALE) {
2481 rcu_read_unlock();
2482 goto retry;
2483 }
2484 if (rc)
2485 goto out;
2486 }
2487 *out_sid = c->sid[0];
2488 } else
2489 *out_sid = SECINITSID_UNLABELED;
2490
2491out:
2492 rcu_read_unlock();
2493 return rc;
2494}
2495
2496
2497
2498
2499
2500
2501
2502
2503int security_ib_endport_sid(struct selinux_state *state,
2504 const char *dev_name, u8 port_num, u32 *out_sid)
2505{
2506 struct selinux_policy *policy;
2507 struct policydb *policydb;
2508 struct sidtab *sidtab;
2509 struct ocontext *c;
2510 int rc;
2511
2512 if (!selinux_initialized(state)) {
2513 *out_sid = SECINITSID_UNLABELED;
2514 return 0;
2515 }
2516
2517retry:
2518 rc = 0;
2519 rcu_read_lock();
2520 policy = rcu_dereference(state->policy);
2521 policydb = &policy->policydb;
2522 sidtab = policy->sidtab;
2523
2524 c = policydb->ocontexts[OCON_IBENDPORT];
2525 while (c) {
2526 if (c->u.ibendport.port == port_num &&
2527 !strncmp(c->u.ibendport.dev_name,
2528 dev_name,
2529 IB_DEVICE_NAME_MAX))
2530 break;
2531
2532 c = c->next;
2533 }
2534
2535 if (c) {
2536 if (!c->sid[0]) {
2537 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2538 &c->sid[0]);
2539 if (rc == -ESTALE) {
2540 rcu_read_unlock();
2541 goto retry;
2542 }
2543 if (rc)
2544 goto out;
2545 }
2546 *out_sid = c->sid[0];
2547 } else
2548 *out_sid = SECINITSID_UNLABELED;
2549
2550out:
2551 rcu_read_unlock();
2552 return rc;
2553}
2554
2555
2556
2557
2558
2559
2560
2561int security_netif_sid(struct selinux_state *state,
2562 char *name, u32 *if_sid)
2563{
2564 struct selinux_policy *policy;
2565 struct policydb *policydb;
2566 struct sidtab *sidtab;
2567 int rc;
2568 struct ocontext *c;
2569
2570 if (!selinux_initialized(state)) {
2571 *if_sid = SECINITSID_NETIF;
2572 return 0;
2573 }
2574
2575retry:
2576 rc = 0;
2577 rcu_read_lock();
2578 policy = rcu_dereference(state->policy);
2579 policydb = &policy->policydb;
2580 sidtab = policy->sidtab;
2581
2582 c = policydb->ocontexts[OCON_NETIF];
2583 while (c) {
2584 if (strcmp(name, c->u.name) == 0)
2585 break;
2586 c = c->next;
2587 }
2588
2589 if (c) {
2590 if (!c->sid[0] || !c->sid[1]) {
2591 rc = sidtab_context_to_sid(sidtab, &c->context[0],
2592 &c->sid[0]);
2593 if (rc == -ESTALE) {
2594 rcu_read_unlock();
2595 goto retry;
2596 }
2597 if (rc)
2598 goto out;
2599 rc = sidtab_context_to_sid(sidtab, &c->context[1],
2600 &c->sid[1]);
2601 if (rc == -ESTALE) {
2602 rcu_read_unlock();
2603 goto retry;
2604 }
2605 if (rc)
2606 goto out;
2607 }
2608 *if_sid = c->sid[0];
2609 } else
2610 *if_sid = SECINITSID_NETIF;
2611
2612out:
2613 rcu_read_unlock();
2614 return rc;
2615}
2616
2617static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
2618{
2619 int i, fail = 0;
2620
2621 for (i = 0; i < 4; i++)
2622 if (addr[i] != (input[i] & mask[i])) {
2623 fail = 1;
2624 break;
2625 }
2626
2627 return !fail;
2628}
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638int security_node_sid(struct selinux_state *state,
2639 u16 domain,
2640 void *addrp,
2641 u32 addrlen,
2642 u32 *out_sid)
2643{
2644 struct selinux_policy *policy;
2645 struct policydb *policydb;
2646 struct sidtab *sidtab;
2647 int rc;
2648 struct ocontext *c;
2649
2650 if (!selinux_initialized(state)) {
2651 *out_sid = SECINITSID_NODE;
2652 return 0;
2653 }
2654
2655retry:
2656 rcu_read_lock();
2657 policy = rcu_dereference(state->policy);
2658 policydb = &policy->policydb;
2659 sidtab = policy->sidtab;
2660
2661 switch (domain) {
2662 case AF_INET: {
2663 u32 addr;
2664
2665 rc = -EINVAL;
2666 if (addrlen != sizeof(u32))
2667 goto out;
2668
2669 addr = *((u32 *)addrp);
2670
2671 c = policydb->ocontexts[OCON_NODE];
2672 while (c) {
2673 if (c->u.node.addr == (addr & c->u.node.mask))
2674 break;
2675 c = c->next;
2676 }
2677 break;
2678 }
2679
2680 case AF_INET6:
2681 rc = -EINVAL;
2682 if (addrlen != sizeof(u64) * 2)
2683 goto out;
2684 c = policydb->ocontexts[OCON_NODE6];
2685 while (c) {
2686 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2687 c->u.node6.mask))
2688 break;
2689 c = c->next;
2690 }
2691 break;
2692
2693 default:
2694 rc = 0;
2695 *out_sid = SECINITSID_NODE;
2696 goto out;
2697 }
2698
2699 if (c) {
2700 if (!c->sid[0]) {
2701 rc = sidtab_context_to_sid(sidtab,
2702 &c->context[0],
2703 &c->sid[0]);
2704 if (rc == -ESTALE) {
2705 rcu_read_unlock();
2706 goto retry;
2707 }
2708 if (rc)
2709 goto out;
2710 }
2711 *out_sid = c->sid[0];
2712 } else {
2713 *out_sid = SECINITSID_NODE;
2714 }
2715
2716 rc = 0;
2717out:
2718 rcu_read_unlock();
2719 return rc;
2720}
2721
2722#define SIDS_NEL 25
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739int security_get_user_sids(struct selinux_state *state,
2740 u32 fromsid,
2741 char *username,
2742 u32 **sids,
2743 u32 *nel)
2744{
2745 struct selinux_policy *policy;
2746 struct policydb *policydb;
2747 struct sidtab *sidtab;
2748 struct context *fromcon, usercon;
2749 u32 *mysids = NULL, *mysids2, sid;
2750 u32 i, j, mynel, maxnel = SIDS_NEL;
2751 struct user_datum *user;
2752 struct role_datum *role;
2753 struct ebitmap_node *rnode, *tnode;
2754 int rc;
2755
2756 *sids = NULL;
2757 *nel = 0;
2758
2759 if (!selinux_initialized(state))
2760 return 0;
2761
2762 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_KERNEL);
2763 if (!mysids)
2764 return -ENOMEM;
2765
2766retry:
2767 mynel = 0;
2768 rcu_read_lock();
2769 policy = rcu_dereference(state->policy);
2770 policydb = &policy->policydb;
2771 sidtab = policy->sidtab;
2772
2773 context_init(&usercon);
2774
2775 rc = -EINVAL;
2776 fromcon = sidtab_search(sidtab, fromsid);
2777 if (!fromcon)
2778 goto out_unlock;
2779
2780 rc = -EINVAL;
2781 user = symtab_search(&policydb->p_users, username);
2782 if (!user)
2783 goto out_unlock;
2784
2785 usercon.user = user->value;
2786
2787 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
2788 role = policydb->role_val_to_struct[i];
2789 usercon.role = i + 1;
2790 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
2791 usercon.type = j + 1;
2792
2793 if (mls_setup_user_range(policydb, fromcon, user,
2794 &usercon))
2795 continue;
2796
2797 rc = sidtab_context_to_sid(sidtab, &usercon, &sid);
2798 if (rc == -ESTALE) {
2799 rcu_read_unlock();
2800 goto retry;
2801 }
2802 if (rc)
2803 goto out_unlock;
2804 if (mynel < maxnel) {
2805 mysids[mynel++] = sid;
2806 } else {
2807 rc = -ENOMEM;
2808 maxnel += SIDS_NEL;
2809 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
2810 if (!mysids2)
2811 goto out_unlock;
2812 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2813 kfree(mysids);
2814 mysids = mysids2;
2815 mysids[mynel++] = sid;
2816 }
2817 }
2818 }
2819 rc = 0;
2820out_unlock:
2821 rcu_read_unlock();
2822 if (rc || !mynel) {
2823 kfree(mysids);
2824 return rc;
2825 }
2826
2827 rc = -ENOMEM;
2828 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2829 if (!mysids2) {
2830 kfree(mysids);
2831 return rc;
2832 }
2833 for (i = 0, j = 0; i < mynel; i++) {
2834 struct av_decision dummy_avd;
2835 rc = avc_has_perm_noaudit(state,
2836 fromsid, mysids[i],
2837 SECCLASS_PROCESS,
2838 PROCESS__TRANSITION, AVC_STRICT,
2839 &dummy_avd);
2840 if (!rc)
2841 mysids2[j++] = mysids[i];
2842 cond_resched();
2843 }
2844 kfree(mysids);
2845 *sids = mysids2;
2846 *nel = j;
2847 return 0;
2848}
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864static inline int __security_genfs_sid(struct selinux_policy *policy,
2865 const char *fstype,
2866 char *path,
2867 u16 orig_sclass,
2868 u32 *sid)
2869{
2870 struct policydb *policydb = &policy->policydb;
2871 struct sidtab *sidtab = policy->sidtab;
2872 int len;
2873 u16 sclass;
2874 struct genfs *genfs;
2875 struct ocontext *c;
2876 int rc, cmp = 0;
2877
2878 while (path[0] == '/' && path[1] == '/')
2879 path++;
2880
2881 sclass = unmap_class(&policy->map, orig_sclass);
2882 *sid = SECINITSID_UNLABELED;
2883
2884 for (genfs = policydb->genfs; genfs; genfs = genfs->next) {
2885 cmp = strcmp(fstype, genfs->fstype);
2886 if (cmp <= 0)
2887 break;
2888 }
2889
2890 rc = -ENOENT;
2891 if (!genfs || cmp)
2892 goto out;
2893
2894 for (c = genfs->head; c; c = c->next) {
2895 len = strlen(c->u.name);
2896 if ((!c->v.sclass || sclass == c->v.sclass) &&
2897 (strncmp(c->u.name, path, len) == 0))
2898 break;
2899 }
2900
2901 rc = -ENOENT;
2902 if (!c)
2903 goto out;
2904
2905 if (!c->sid[0]) {
2906 rc = sidtab_context_to_sid(sidtab, &c->context[0], &c->sid[0]);
2907 if (rc)
2908 goto out;
2909 }
2910
2911 *sid = c->sid[0];
2912 rc = 0;
2913out:
2914 return rc;
2915}
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928int security_genfs_sid(struct selinux_state *state,
2929 const char *fstype,
2930 char *path,
2931 u16 orig_sclass,
2932 u32 *sid)
2933{
2934 struct selinux_policy *policy;
2935 int retval;
2936
2937 if (!selinux_initialized(state)) {
2938 *sid = SECINITSID_UNLABELED;
2939 return 0;
2940 }
2941
2942 do {
2943 rcu_read_lock();
2944 policy = rcu_dereference(state->policy);
2945 retval = __security_genfs_sid(policy, fstype, path,
2946 orig_sclass, sid);
2947 rcu_read_unlock();
2948 } while (retval == -ESTALE);
2949 return retval;
2950}
2951
2952int selinux_policy_genfs_sid(struct selinux_policy *policy,
2953 const char *fstype,
2954 char *path,
2955 u16 orig_sclass,
2956 u32 *sid)
2957{
2958
2959 return __security_genfs_sid(policy, fstype, path, orig_sclass, sid);
2960}
2961
2962
2963
2964
2965
2966
2967int security_fs_use(struct selinux_state *state, struct super_block *sb)
2968{
2969 struct selinux_policy *policy;
2970 struct policydb *policydb;
2971 struct sidtab *sidtab;
2972 int rc;
2973 struct ocontext *c;
2974 struct superblock_security_struct *sbsec = selinux_superblock(sb);
2975 const char *fstype = sb->s_type->name;
2976
2977 if (!selinux_initialized(state)) {
2978 sbsec->behavior = SECURITY_FS_USE_NONE;
2979 sbsec->sid = SECINITSID_UNLABELED;
2980 return 0;
2981 }
2982
2983retry:
2984 rc = 0;
2985 rcu_read_lock();
2986 policy = rcu_dereference(state->policy);
2987 policydb = &policy->policydb;
2988 sidtab = policy->sidtab;
2989
2990 c = policydb->ocontexts[OCON_FSUSE];
2991 while (c) {
2992 if (strcmp(fstype, c->u.name) == 0)
2993 break;
2994 c = c->next;
2995 }
2996
2997 if (c) {
2998 sbsec->behavior = c->v.behavior;
2999 if (!c->sid[0]) {
3000 rc = sidtab_context_to_sid(sidtab, &c->context[0],
3001 &c->sid[0]);
3002 if (rc == -ESTALE) {
3003 rcu_read_unlock();
3004 goto retry;
3005 }
3006 if (rc)
3007 goto out;
3008 }
3009 sbsec->sid = c->sid[0];
3010 } else {
3011 rc = __security_genfs_sid(policy, fstype, "/",
3012 SECCLASS_DIR, &sbsec->sid);
3013 if (rc == -ESTALE) {
3014 rcu_read_unlock();
3015 goto retry;
3016 }
3017 if (rc) {
3018 sbsec->behavior = SECURITY_FS_USE_NONE;
3019 rc = 0;
3020 } else {
3021 sbsec->behavior = SECURITY_FS_USE_GENFS;
3022 }
3023 }
3024
3025out:
3026 rcu_read_unlock();
3027 return rc;
3028}
3029
3030int security_get_bools(struct selinux_policy *policy,
3031 u32 *len, char ***names, int **values)
3032{
3033 struct policydb *policydb;
3034 u32 i;
3035 int rc;
3036
3037 policydb = &policy->policydb;
3038
3039 *names = NULL;
3040 *values = NULL;
3041
3042 rc = 0;
3043 *len = policydb->p_bools.nprim;
3044 if (!*len)
3045 goto out;
3046
3047 rc = -ENOMEM;
3048 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
3049 if (!*names)
3050 goto err;
3051
3052 rc = -ENOMEM;
3053 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
3054 if (!*values)
3055 goto err;
3056
3057 for (i = 0; i < *len; i++) {
3058 (*values)[i] = policydb->bool_val_to_struct[i]->state;
3059
3060 rc = -ENOMEM;
3061 (*names)[i] = kstrdup(sym_name(policydb, SYM_BOOLS, i),
3062 GFP_ATOMIC);
3063 if (!(*names)[i])
3064 goto err;
3065 }
3066 rc = 0;
3067out:
3068 return rc;
3069err:
3070 if (*names) {
3071 for (i = 0; i < *len; i++)
3072 kfree((*names)[i]);
3073 kfree(*names);
3074 }
3075 kfree(*values);
3076 *len = 0;
3077 *names = NULL;
3078 *values = NULL;
3079 goto out;
3080}
3081
3082
3083int security_set_bools(struct selinux_state *state, u32 len, int *values)
3084{
3085 struct selinux_policy *newpolicy, *oldpolicy;
3086 int rc;
3087 u32 i, seqno = 0;
3088
3089 if (!selinux_initialized(state))
3090 return -EINVAL;
3091
3092 oldpolicy = rcu_dereference_protected(state->policy,
3093 lockdep_is_held(&state->policy_mutex));
3094
3095
3096 if (WARN_ON(len != oldpolicy->policydb.p_bools.nprim))
3097 return -EINVAL;
3098
3099 newpolicy = kmemdup(oldpolicy, sizeof(*newpolicy), GFP_KERNEL);
3100 if (!newpolicy)
3101 return -ENOMEM;
3102
3103
3104
3105
3106
3107 rc = cond_policydb_dup(&newpolicy->policydb, &oldpolicy->policydb);
3108 if (rc) {
3109 kfree(newpolicy);
3110 return -ENOMEM;
3111 }
3112
3113
3114 for (i = 0; i < len; i++) {
3115 int new_state = !!values[i];
3116 int old_state = newpolicy->policydb.bool_val_to_struct[i]->state;
3117
3118 if (new_state != old_state) {
3119 audit_log(audit_context(), GFP_ATOMIC,
3120 AUDIT_MAC_CONFIG_CHANGE,
3121 "bool=%s val=%d old_val=%d auid=%u ses=%u",
3122 sym_name(&newpolicy->policydb, SYM_BOOLS, i),
3123 new_state,
3124 old_state,
3125 from_kuid(&init_user_ns, audit_get_loginuid(current)),
3126 audit_get_sessionid(current));
3127 newpolicy->policydb.bool_val_to_struct[i]->state = new_state;
3128 }
3129 }
3130
3131
3132 evaluate_cond_nodes(&newpolicy->policydb);
3133
3134
3135 newpolicy->latest_granting = oldpolicy->latest_granting + 1;
3136 seqno = newpolicy->latest_granting;
3137
3138
3139 rcu_assign_pointer(state->policy, newpolicy);
3140
3141
3142
3143
3144
3145
3146 synchronize_rcu();
3147 selinux_policy_cond_free(oldpolicy);
3148
3149
3150 selinux_notify_policy_change(state, seqno);
3151 return 0;
3152}
3153
3154int security_get_bool_value(struct selinux_state *state,
3155 u32 index)
3156{
3157 struct selinux_policy *policy;
3158 struct policydb *policydb;
3159 int rc;
3160 u32 len;
3161
3162 if (!selinux_initialized(state))
3163 return 0;
3164
3165 rcu_read_lock();
3166 policy = rcu_dereference(state->policy);
3167 policydb = &policy->policydb;
3168
3169 rc = -EFAULT;
3170 len = policydb->p_bools.nprim;
3171 if (index >= len)
3172 goto out;
3173
3174 rc = policydb->bool_val_to_struct[index]->state;
3175out:
3176 rcu_read_unlock();
3177 return rc;
3178}
3179
3180static int security_preserve_bools(struct selinux_policy *oldpolicy,
3181 struct selinux_policy *newpolicy)
3182{
3183 int rc, *bvalues = NULL;
3184 char **bnames = NULL;
3185 struct cond_bool_datum *booldatum;
3186 u32 i, nbools = 0;
3187
3188 rc = security_get_bools(oldpolicy, &nbools, &bnames, &bvalues);
3189 if (rc)
3190 goto out;
3191 for (i = 0; i < nbools; i++) {
3192 booldatum = symtab_search(&newpolicy->policydb.p_bools,
3193 bnames[i]);
3194 if (booldatum)
3195 booldatum->state = bvalues[i];
3196 }
3197 evaluate_cond_nodes(&newpolicy->policydb);
3198
3199out:
3200 if (bnames) {
3201 for (i = 0; i < nbools; i++)
3202 kfree(bnames[i]);
3203 }
3204 kfree(bnames);
3205 kfree(bvalues);
3206 return rc;
3207}
3208
3209
3210
3211
3212
3213int security_sid_mls_copy(struct selinux_state *state,
3214 u32 sid, u32 mls_sid, u32 *new_sid)
3215{
3216 struct selinux_policy *policy;
3217 struct policydb *policydb;
3218 struct sidtab *sidtab;
3219 struct context *context1;
3220 struct context *context2;
3221 struct context newcon;
3222 char *s;
3223 u32 len;
3224 int rc;
3225
3226 if (!selinux_initialized(state)) {
3227 *new_sid = sid;
3228 return 0;
3229 }
3230
3231retry:
3232 rc = 0;
3233 context_init(&newcon);
3234
3235 rcu_read_lock();
3236 policy = rcu_dereference(state->policy);
3237 policydb = &policy->policydb;
3238 sidtab = policy->sidtab;
3239
3240 if (!policydb->mls_enabled) {
3241 *new_sid = sid;
3242 goto out_unlock;
3243 }
3244
3245 rc = -EINVAL;
3246 context1 = sidtab_search(sidtab, sid);
3247 if (!context1) {
3248 pr_err("SELinux: %s: unrecognized SID %d\n",
3249 __func__, sid);
3250 goto out_unlock;
3251 }
3252
3253 rc = -EINVAL;
3254 context2 = sidtab_search(sidtab, mls_sid);
3255 if (!context2) {
3256 pr_err("SELinux: %s: unrecognized SID %d\n",
3257 __func__, mls_sid);
3258 goto out_unlock;
3259 }
3260
3261 newcon.user = context1->user;
3262 newcon.role = context1->role;
3263 newcon.type = context1->type;
3264 rc = mls_context_cpy(&newcon, context2);
3265 if (rc)
3266 goto out_unlock;
3267
3268
3269 if (!policydb_context_isvalid(policydb, &newcon)) {
3270 rc = convert_context_handle_invalid_context(state, policydb,
3271 &newcon);
3272 if (rc) {
3273 if (!context_struct_to_string(policydb, &newcon, &s,
3274 &len)) {
3275 struct audit_buffer *ab;
3276
3277 ab = audit_log_start(audit_context(),
3278 GFP_ATOMIC,
3279 AUDIT_SELINUX_ERR);
3280 audit_log_format(ab,
3281 "op=security_sid_mls_copy invalid_context=");
3282
3283 audit_log_n_untrustedstring(ab, s, len - 1);
3284 audit_log_end(ab);
3285 kfree(s);
3286 }
3287 goto out_unlock;
3288 }
3289 }
3290 rc = sidtab_context_to_sid(sidtab, &newcon, new_sid);
3291 if (rc == -ESTALE) {
3292 rcu_read_unlock();
3293 context_destroy(&newcon);
3294 goto retry;
3295 }
3296out_unlock:
3297 rcu_read_unlock();
3298 context_destroy(&newcon);
3299 return rc;
3300}
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323int security_net_peersid_resolve(struct selinux_state *state,
3324 u32 nlbl_sid, u32 nlbl_type,
3325 u32 xfrm_sid,
3326 u32 *peer_sid)
3327{
3328 struct selinux_policy *policy;
3329 struct policydb *policydb;
3330 struct sidtab *sidtab;
3331 int rc;
3332 struct context *nlbl_ctx;
3333 struct context *xfrm_ctx;
3334
3335 *peer_sid = SECSID_NULL;
3336
3337
3338
3339
3340 if (xfrm_sid == SECSID_NULL) {
3341 *peer_sid = nlbl_sid;
3342 return 0;
3343 }
3344
3345
3346
3347 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
3348 *peer_sid = xfrm_sid;
3349 return 0;
3350 }
3351
3352 if (!selinux_initialized(state))
3353 return 0;
3354
3355 rcu_read_lock();
3356 policy = rcu_dereference(state->policy);
3357 policydb = &policy->policydb;
3358 sidtab = policy->sidtab;
3359
3360
3361
3362
3363
3364
3365 if (!policydb->mls_enabled) {
3366 rc = 0;
3367 goto out;
3368 }
3369
3370 rc = -EINVAL;
3371 nlbl_ctx = sidtab_search(sidtab, nlbl_sid);
3372 if (!nlbl_ctx) {
3373 pr_err("SELinux: %s: unrecognized SID %d\n",
3374 __func__, nlbl_sid);
3375 goto out;
3376 }
3377 rc = -EINVAL;
3378 xfrm_ctx = sidtab_search(sidtab, xfrm_sid);
3379 if (!xfrm_ctx) {
3380 pr_err("SELinux: %s: unrecognized SID %d\n",
3381 __func__, xfrm_sid);
3382 goto out;
3383 }
3384 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
3385 if (rc)
3386 goto out;
3387
3388
3389
3390
3391
3392
3393 *peer_sid = xfrm_sid;
3394out:
3395 rcu_read_unlock();
3396 return rc;
3397}
3398
3399static int get_classes_callback(void *k, void *d, void *args)
3400{
3401 struct class_datum *datum = d;
3402 char *name = k, **classes = args;
3403 int value = datum->value - 1;
3404
3405 classes[value] = kstrdup(name, GFP_ATOMIC);
3406 if (!classes[value])
3407 return -ENOMEM;
3408
3409 return 0;
3410}
3411
3412int security_get_classes(struct selinux_policy *policy,
3413 char ***classes, int *nclasses)
3414{
3415 struct policydb *policydb;
3416 int rc;
3417
3418 policydb = &policy->policydb;
3419
3420 rc = -ENOMEM;
3421 *nclasses = policydb->p_classes.nprim;
3422 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
3423 if (!*classes)
3424 goto out;
3425
3426 rc = hashtab_map(&policydb->p_classes.table, get_classes_callback,
3427 *classes);
3428 if (rc) {
3429 int i;
3430 for (i = 0; i < *nclasses; i++)
3431 kfree((*classes)[i]);
3432 kfree(*classes);
3433 }
3434
3435out:
3436 return rc;
3437}
3438
3439static int get_permissions_callback(void *k, void *d, void *args)
3440{
3441 struct perm_datum *datum = d;
3442 char *name = k, **perms = args;
3443 int value = datum->value - 1;
3444
3445 perms[value] = kstrdup(name, GFP_ATOMIC);
3446 if (!perms[value])
3447 return -ENOMEM;
3448
3449 return 0;
3450}
3451
3452int security_get_permissions(struct selinux_policy *policy,
3453 char *class, char ***perms, int *nperms)
3454{
3455 struct policydb *policydb;
3456 int rc, i;
3457 struct class_datum *match;
3458
3459 policydb = &policy->policydb;
3460
3461 rc = -EINVAL;
3462 match = symtab_search(&policydb->p_classes, class);
3463 if (!match) {
3464 pr_err("SELinux: %s: unrecognized class %s\n",
3465 __func__, class);
3466 goto out;
3467 }
3468
3469 rc = -ENOMEM;
3470 *nperms = match->permissions.nprim;
3471 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
3472 if (!*perms)
3473 goto out;
3474
3475 if (match->comdatum) {
3476 rc = hashtab_map(&match->comdatum->permissions.table,
3477 get_permissions_callback, *perms);
3478 if (rc)
3479 goto err;
3480 }
3481
3482 rc = hashtab_map(&match->permissions.table, get_permissions_callback,
3483 *perms);
3484 if (rc)
3485 goto err;
3486
3487out:
3488 return rc;
3489
3490err:
3491 for (i = 0; i < *nperms; i++)
3492 kfree((*perms)[i]);
3493 kfree(*perms);
3494 return rc;
3495}
3496
3497int security_get_reject_unknown(struct selinux_state *state)
3498{
3499 struct selinux_policy *policy;
3500 int value;
3501
3502 if (!selinux_initialized(state))
3503 return 0;
3504
3505 rcu_read_lock();
3506 policy = rcu_dereference(state->policy);
3507 value = policy->policydb.reject_unknown;
3508 rcu_read_unlock();
3509 return value;
3510}
3511
3512int security_get_allow_unknown(struct selinux_state *state)
3513{
3514 struct selinux_policy *policy;
3515 int value;
3516
3517 if (!selinux_initialized(state))
3518 return 0;
3519
3520 rcu_read_lock();
3521 policy = rcu_dereference(state->policy);
3522 value = policy->policydb.allow_unknown;
3523 rcu_read_unlock();
3524 return value;
3525}
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538int security_policycap_supported(struct selinux_state *state,
3539 unsigned int req_cap)
3540{
3541 struct selinux_policy *policy;
3542 int rc;
3543
3544 if (!selinux_initialized(state))
3545 return 0;
3546
3547 rcu_read_lock();
3548 policy = rcu_dereference(state->policy);
3549 rc = ebitmap_get_bit(&policy->policydb.policycaps, req_cap);
3550 rcu_read_unlock();
3551
3552 return rc;
3553}
3554
3555struct selinux_audit_rule {
3556 u32 au_seqno;
3557 struct context au_ctxt;
3558};
3559
3560void selinux_audit_rule_free(void *vrule)
3561{
3562 struct selinux_audit_rule *rule = vrule;
3563
3564 if (rule) {
3565 context_destroy(&rule->au_ctxt);
3566 kfree(rule);
3567 }
3568}
3569
3570int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
3571{
3572 struct selinux_state *state = &selinux_state;
3573 struct selinux_policy *policy;
3574 struct policydb *policydb;
3575 struct selinux_audit_rule *tmprule;
3576 struct role_datum *roledatum;
3577 struct type_datum *typedatum;
3578 struct user_datum *userdatum;
3579 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
3580 int rc = 0;
3581
3582 *rule = NULL;
3583
3584 if (!selinux_initialized(state))
3585 return -EOPNOTSUPP;
3586
3587 switch (field) {
3588 case AUDIT_SUBJ_USER:
3589 case AUDIT_SUBJ_ROLE:
3590 case AUDIT_SUBJ_TYPE:
3591 case AUDIT_OBJ_USER:
3592 case AUDIT_OBJ_ROLE:
3593 case AUDIT_OBJ_TYPE:
3594
3595 if (op != Audit_equal && op != Audit_not_equal)
3596 return -EINVAL;
3597 break;
3598 case AUDIT_SUBJ_SEN:
3599 case AUDIT_SUBJ_CLR:
3600 case AUDIT_OBJ_LEV_LOW:
3601 case AUDIT_OBJ_LEV_HIGH:
3602
3603 if (strchr(rulestr, '-'))
3604 return -EINVAL;
3605 break;
3606 default:
3607
3608 return -EINVAL;
3609 }
3610
3611 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3612 if (!tmprule)
3613 return -ENOMEM;
3614
3615 context_init(&tmprule->au_ctxt);
3616
3617 rcu_read_lock();
3618 policy = rcu_dereference(state->policy);
3619 policydb = &policy->policydb;
3620
3621 tmprule->au_seqno = policy->latest_granting;
3622
3623 switch (field) {
3624 case AUDIT_SUBJ_USER:
3625 case AUDIT_OBJ_USER:
3626 rc = -EINVAL;
3627 userdatum = symtab_search(&policydb->p_users, rulestr);
3628 if (!userdatum)
3629 goto out;
3630 tmprule->au_ctxt.user = userdatum->value;
3631 break;
3632 case AUDIT_SUBJ_ROLE:
3633 case AUDIT_OBJ_ROLE:
3634 rc = -EINVAL;
3635 roledatum = symtab_search(&policydb->p_roles, rulestr);
3636 if (!roledatum)
3637 goto out;
3638 tmprule->au_ctxt.role = roledatum->value;
3639 break;
3640 case AUDIT_SUBJ_TYPE:
3641 case AUDIT_OBJ_TYPE:
3642 rc = -EINVAL;
3643 typedatum = symtab_search(&policydb->p_types, rulestr);
3644 if (!typedatum)
3645 goto out;
3646 tmprule->au_ctxt.type = typedatum->value;
3647 break;
3648 case AUDIT_SUBJ_SEN:
3649 case AUDIT_SUBJ_CLR:
3650 case AUDIT_OBJ_LEV_LOW:
3651 case AUDIT_OBJ_LEV_HIGH:
3652 rc = mls_from_string(policydb, rulestr, &tmprule->au_ctxt,
3653 GFP_ATOMIC);
3654 if (rc)
3655 goto out;
3656 break;
3657 }
3658 rc = 0;
3659out:
3660 rcu_read_unlock();
3661
3662 if (rc) {
3663 selinux_audit_rule_free(tmprule);
3664 tmprule = NULL;
3665 }
3666
3667 *rule = tmprule;
3668
3669 return rc;
3670}
3671
3672
3673int selinux_audit_rule_known(struct audit_krule *rule)
3674{
3675 int i;
3676
3677 for (i = 0; i < rule->field_count; i++) {
3678 struct audit_field *f = &rule->fields[i];
3679 switch (f->type) {
3680 case AUDIT_SUBJ_USER:
3681 case AUDIT_SUBJ_ROLE:
3682 case AUDIT_SUBJ_TYPE:
3683 case AUDIT_SUBJ_SEN:
3684 case AUDIT_SUBJ_CLR:
3685 case AUDIT_OBJ_USER:
3686 case AUDIT_OBJ_ROLE:
3687 case AUDIT_OBJ_TYPE:
3688 case AUDIT_OBJ_LEV_LOW:
3689 case AUDIT_OBJ_LEV_HIGH:
3690 return 1;
3691 }
3692 }
3693
3694 return 0;
3695}
3696
3697int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule)
3698{
3699 struct selinux_state *state = &selinux_state;
3700 struct selinux_policy *policy;
3701 struct context *ctxt;
3702 struct mls_level *level;
3703 struct selinux_audit_rule *rule = vrule;
3704 int match = 0;
3705
3706 if (unlikely(!rule)) {
3707 WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n");
3708 return -ENOENT;
3709 }
3710
3711 if (!selinux_initialized(state))
3712 return 0;
3713
3714 rcu_read_lock();
3715
3716 policy = rcu_dereference(state->policy);
3717
3718 if (rule->au_seqno < policy->latest_granting) {
3719 match = -ESTALE;
3720 goto out;
3721 }
3722
3723 ctxt = sidtab_search(policy->sidtab, sid);
3724 if (unlikely(!ctxt)) {
3725 WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n",
3726 sid);
3727 match = -ENOENT;
3728 goto out;
3729 }
3730
3731
3732
3733 switch (field) {
3734 case AUDIT_SUBJ_USER:
3735 case AUDIT_OBJ_USER:
3736 switch (op) {
3737 case Audit_equal:
3738 match = (ctxt->user == rule->au_ctxt.user);
3739 break;
3740 case Audit_not_equal:
3741 match = (ctxt->user != rule->au_ctxt.user);
3742 break;
3743 }
3744 break;
3745 case AUDIT_SUBJ_ROLE:
3746 case AUDIT_OBJ_ROLE:
3747 switch (op) {
3748 case Audit_equal:
3749 match = (ctxt->role == rule->au_ctxt.role);
3750 break;
3751 case Audit_not_equal:
3752 match = (ctxt->role != rule->au_ctxt.role);
3753 break;
3754 }
3755 break;
3756 case AUDIT_SUBJ_TYPE:
3757 case AUDIT_OBJ_TYPE:
3758 switch (op) {
3759 case Audit_equal:
3760 match = (ctxt->type == rule->au_ctxt.type);
3761 break;
3762 case Audit_not_equal:
3763 match = (ctxt->type != rule->au_ctxt.type);
3764 break;
3765 }
3766 break;
3767 case AUDIT_SUBJ_SEN:
3768 case AUDIT_SUBJ_CLR:
3769 case AUDIT_OBJ_LEV_LOW:
3770 case AUDIT_OBJ_LEV_HIGH:
3771 level = ((field == AUDIT_SUBJ_SEN ||
3772 field == AUDIT_OBJ_LEV_LOW) ?
3773 &ctxt->range.level[0] : &ctxt->range.level[1]);
3774 switch (op) {
3775 case Audit_equal:
3776 match = mls_level_eq(&rule->au_ctxt.range.level[0],
3777 level);
3778 break;
3779 case Audit_not_equal:
3780 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
3781 level);
3782 break;
3783 case Audit_lt:
3784 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
3785 level) &&
3786 !mls_level_eq(&rule->au_ctxt.range.level[0],
3787 level));
3788 break;
3789 case Audit_le:
3790 match = mls_level_dom(&rule->au_ctxt.range.level[0],
3791 level);
3792 break;
3793 case Audit_gt:
3794 match = (mls_level_dom(level,
3795 &rule->au_ctxt.range.level[0]) &&
3796 !mls_level_eq(level,
3797 &rule->au_ctxt.range.level[0]));
3798 break;
3799 case Audit_ge:
3800 match = mls_level_dom(level,
3801 &rule->au_ctxt.range.level[0]);
3802 break;
3803 }
3804 }
3805
3806out:
3807 rcu_read_unlock();
3808 return match;
3809}
3810
3811static int aurule_avc_callback(u32 event)
3812{
3813 if (event == AVC_CALLBACK_RESET)
3814 return audit_update_lsm_rules();
3815 return 0;
3816}
3817
3818static int __init aurule_init(void)
3819{
3820 int err;
3821
3822 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET);
3823 if (err)
3824 panic("avc_add_callback() failed, error %d\n", err);
3825
3826 return err;
3827}
3828__initcall(aurule_init);
3829
3830#ifdef CONFIG_NETLABEL
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
3843 u32 sid)
3844{
3845 u32 *sid_cache;
3846
3847 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
3848 if (sid_cache == NULL)
3849 return;
3850 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
3851 if (secattr->cache == NULL) {
3852 kfree(sid_cache);
3853 return;
3854 }
3855
3856 *sid_cache = sid;
3857 secattr->cache->free = kfree;
3858 secattr->cache->data = sid_cache;
3859 secattr->flags |= NETLBL_SECATTR_CACHE;
3860}
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878int security_netlbl_secattr_to_sid(struct selinux_state *state,
3879 struct netlbl_lsm_secattr *secattr,
3880 u32 *sid)
3881{
3882 struct selinux_policy *policy;
3883 struct policydb *policydb;
3884 struct sidtab *sidtab;
3885 int rc;
3886 struct context *ctx;
3887 struct context ctx_new;
3888
3889 if (!selinux_initialized(state)) {
3890 *sid = SECSID_NULL;
3891 return 0;
3892 }
3893
3894retry:
3895 rc = 0;
3896 rcu_read_lock();
3897 policy = rcu_dereference(state->policy);
3898 policydb = &policy->policydb;
3899 sidtab = policy->sidtab;
3900
3901 if (secattr->flags & NETLBL_SECATTR_CACHE)
3902 *sid = *(u32 *)secattr->cache->data;
3903 else if (secattr->flags & NETLBL_SECATTR_SECID)
3904 *sid = secattr->attr.secid;
3905 else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
3906 rc = -EIDRM;
3907 ctx = sidtab_search(sidtab, SECINITSID_NETMSG);
3908 if (ctx == NULL)
3909 goto out;
3910
3911 context_init(&ctx_new);
3912 ctx_new.user = ctx->user;
3913 ctx_new.role = ctx->role;
3914 ctx_new.type = ctx->type;
3915 mls_import_netlbl_lvl(policydb, &ctx_new, secattr);
3916 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
3917 rc = mls_import_netlbl_cat(policydb, &ctx_new, secattr);
3918 if (rc)
3919 goto out;
3920 }
3921 rc = -EIDRM;
3922 if (!mls_context_isvalid(policydb, &ctx_new)) {
3923 ebitmap_destroy(&ctx_new.range.level[0].cat);
3924 goto out;
3925 }
3926
3927 rc = sidtab_context_to_sid(sidtab, &ctx_new, sid);
3928 ebitmap_destroy(&ctx_new.range.level[0].cat);
3929 if (rc == -ESTALE) {
3930 rcu_read_unlock();
3931 goto retry;
3932 }
3933 if (rc)
3934 goto out;
3935
3936 security_netlbl_cache_add(secattr, *sid);
3937 } else
3938 *sid = SECSID_NULL;
3939
3940out:
3941 rcu_read_unlock();
3942 return rc;
3943}
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956int security_netlbl_sid_to_secattr(struct selinux_state *state,
3957 u32 sid, struct netlbl_lsm_secattr *secattr)
3958{
3959 struct selinux_policy *policy;
3960 struct policydb *policydb;
3961 int rc;
3962 struct context *ctx;
3963
3964 if (!selinux_initialized(state))
3965 return 0;
3966
3967 rcu_read_lock();
3968 policy = rcu_dereference(state->policy);
3969 policydb = &policy->policydb;
3970
3971 rc = -ENOENT;
3972 ctx = sidtab_search(policy->sidtab, sid);
3973 if (ctx == NULL)
3974 goto out;
3975
3976 rc = -ENOMEM;
3977 secattr->domain = kstrdup(sym_name(policydb, SYM_TYPES, ctx->type - 1),
3978 GFP_ATOMIC);
3979 if (secattr->domain == NULL)
3980 goto out;
3981
3982 secattr->attr.secid = sid;
3983 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
3984 mls_export_netlbl_lvl(policydb, ctx, secattr);
3985 rc = mls_export_netlbl_cat(policydb, ctx, secattr);
3986out:
3987 rcu_read_unlock();
3988 return rc;
3989}
3990#endif
3991
3992
3993
3994
3995
3996
3997
3998
3999static int __security_read_policy(struct selinux_policy *policy,
4000 void *data, size_t *len)
4001{
4002 int rc;
4003 struct policy_file fp;
4004
4005 fp.data = data;
4006 fp.len = *len;
4007
4008 rc = policydb_write(&policy->policydb, &fp);
4009 if (rc)
4010 return rc;
4011
4012 *len = (unsigned long)fp.data - (unsigned long)data;
4013 return 0;
4014}
4015
4016
4017
4018
4019
4020
4021
4022
4023int security_read_policy(struct selinux_state *state,
4024 void **data, size_t *len)
4025{
4026 struct selinux_policy *policy;
4027
4028 policy = rcu_dereference_protected(
4029 state->policy, lockdep_is_held(&state->policy_mutex));
4030 if (!policy)
4031 return -EINVAL;
4032
4033 *len = policy->policydb.len;
4034 *data = vmalloc_user(*len);
4035 if (!*data)
4036 return -ENOMEM;
4037
4038 return __security_read_policy(policy, *data, len);
4039}
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053int security_read_state_kernel(struct selinux_state *state,
4054 void **data, size_t *len)
4055{
4056 struct selinux_policy *policy;
4057
4058 policy = rcu_dereference_protected(
4059 state->policy, lockdep_is_held(&state->policy_mutex));
4060 if (!policy)
4061 return -EINVAL;
4062
4063 *len = policy->policydb.len;
4064 *data = vmalloc(*len);
4065 if (!*data)
4066 return -ENOMEM;
4067
4068 return __security_read_policy(policy, *data, len);
4069}
4070