1 /*
2 * Implementation of the security services.
3 *
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
5 * James Morris <jmorris@redhat.com>
6 *
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
8 *
9 * Support for enhanced MLS infrastructure.
10 * Support for context based audit filters.
11 *
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13 *
14 * Added conditional policy language extensions
15 *
16 * Updated: Hewlett-Packard <paul@paul-moore.com>
17 *
18 * Added support for NetLabel
19 * Added support for the policy capability bitmap
20 *
21 * Updated: Chad Sellers <csellers@tresys.com>
22 *
23 * Added validation of kernel classes and permissions
24 *
25 * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
26 *
27 * Added support for bounds domain and audit messaged on masked permissions
28 *
29 * Updated: Guido Trentalancia <guido@trentalancia.com>
30 *
31 * Added support for runtime switching of the policy type
32 *
33 * Copyright (C) 2008, 2009 NEC Corporation
34 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
35 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
36 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
37 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
38 * This program is free software; you can redistribute it and/or modify
39 * it under the terms of the GNU General Public License as published by
40 * the Free Software Foundation, version 2.
41 */
42 #include <linux/kernel.h>
43 #include <linux/slab.h>
44 #include <linux/string.h>
45 #include <linux/spinlock.h>
46 #include <linux/rcupdate.h>
47 #include <linux/errno.h>
48 #include <linux/in.h>
49 #include <linux/sched.h>
50 #include <linux/audit.h>
51 #include <linux/mutex.h>
52 #include <linux/selinux.h>
53 #include <linux/flex_array.h>
54 #include <linux/vmalloc.h>
55 #include <net/netlabel.h>
56
57 #include "flask.h"
58 #include "avc.h"
59 #include "avc_ss.h"
60 #include "security.h"
61 #include "context.h"
62 #include "policydb.h"
63 #include "sidtab.h"
64 #include "services.h"
65 #include "conditional.h"
66 #include "mls.h"
67 #include "objsec.h"
68 #include "netlabel.h"
69 #include "xfrm.h"
70 #include "ebitmap.h"
71 #include "audit.h"
72
73 int selinux_policycap_netpeer;
74 int selinux_policycap_openperm;
75
76 static DEFINE_RWLOCK(policy_rwlock);
77
78 static struct sidtab sidtab;
79 struct policydb policydb;
80 int ss_initialized;
81
82 /*
83 * The largest sequence number that has been used when
84 * providing an access decision to the access vector cache.
85 * The sequence number only changes when a policy change
86 * occurs.
87 */
88 static u32 latest_granting;
89
90 /* Forward declaration. */
91 static int context_struct_to_string(struct context *context, char **scontext,
92 u32 *scontext_len);
93
94 static void context_struct_compute_av(struct context *scontext,
95 struct context *tcontext,
96 u16 tclass,
97 struct av_decision *avd,
98 struct extended_perms *xperms);
99
100 struct selinux_mapping {
101 u16 value; /* policy value */
102 unsigned num_perms;
103 u32 perms[sizeof(u32) * 8];
104 };
105
106 static struct selinux_mapping *current_mapping;
107 static u16 current_mapping_size;
108
selinux_set_mapping(struct policydb * pol,struct security_class_mapping * map,struct selinux_mapping ** out_map_p,u16 * out_map_size)109 static int selinux_set_mapping(struct policydb *pol,
110 struct security_class_mapping *map,
111 struct selinux_mapping **out_map_p,
112 u16 *out_map_size)
113 {
114 struct selinux_mapping *out_map = NULL;
115 size_t size = sizeof(struct selinux_mapping);
116 u16 i, j;
117 unsigned k;
118 bool print_unknown_handle = false;
119
120 /* Find number of classes in the input mapping */
121 if (!map)
122 return -EINVAL;
123 i = 0;
124 while (map[i].name)
125 i++;
126
127 /* Allocate space for the class records, plus one for class zero */
128 out_map = kcalloc(++i, size, GFP_ATOMIC);
129 if (!out_map)
130 return -ENOMEM;
131
132 /* Store the raw class and permission values */
133 j = 0;
134 while (map[j].name) {
135 struct security_class_mapping *p_in = map + (j++);
136 struct selinux_mapping *p_out = out_map + j;
137
138 /* An empty class string skips ahead */
139 if (!strcmp(p_in->name, "")) {
140 p_out->num_perms = 0;
141 continue;
142 }
143
144 p_out->value = string_to_security_class(pol, p_in->name);
145 if (!p_out->value) {
146 printk(KERN_INFO
147 "SELinux: Class %s not defined in policy.\n",
148 p_in->name);
149 if (pol->reject_unknown)
150 goto err;
151 p_out->num_perms = 0;
152 print_unknown_handle = true;
153 continue;
154 }
155
156 k = 0;
157 while (p_in->perms && p_in->perms[k]) {
158 /* An empty permission string skips ahead */
159 if (!*p_in->perms[k]) {
160 k++;
161 continue;
162 }
163 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
164 p_in->perms[k]);
165 if (!p_out->perms[k]) {
166 printk(KERN_INFO
167 "SELinux: Permission %s in class %s not defined in policy.\n",
168 p_in->perms[k], p_in->name);
169 if (pol->reject_unknown)
170 goto err;
171 print_unknown_handle = true;
172 }
173
174 k++;
175 }
176 p_out->num_perms = k;
177 }
178
179 if (print_unknown_handle)
180 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
181 pol->allow_unknown ? "allowed" : "denied");
182
183 *out_map_p = out_map;
184 *out_map_size = i;
185 return 0;
186 err:
187 kfree(out_map);
188 return -EINVAL;
189 }
190
191 /*
192 * Get real, policy values from mapped values
193 */
194
unmap_class(u16 tclass)195 static u16 unmap_class(u16 tclass)
196 {
197 if (tclass < current_mapping_size)
198 return current_mapping[tclass].value;
199
200 return tclass;
201 }
202
203 /*
204 * Get kernel value for class from its policy value
205 */
map_class(u16 pol_value)206 static u16 map_class(u16 pol_value)
207 {
208 u16 i;
209
210 for (i = 1; i < current_mapping_size; i++) {
211 if (current_mapping[i].value == pol_value)
212 return i;
213 }
214
215 return SECCLASS_NULL;
216 }
217
map_decision(u16 tclass,struct av_decision * avd,int allow_unknown)218 static void map_decision(u16 tclass, struct av_decision *avd,
219 int allow_unknown)
220 {
221 if (tclass < current_mapping_size) {
222 unsigned i, n = current_mapping[tclass].num_perms;
223 u32 result;
224
225 for (i = 0, result = 0; i < n; i++) {
226 if (avd->allowed & current_mapping[tclass].perms[i])
227 result |= 1<<i;
228 if (allow_unknown && !current_mapping[tclass].perms[i])
229 result |= 1<<i;
230 }
231 avd->allowed = result;
232
233 for (i = 0, result = 0; i < n; i++)
234 if (avd->auditallow & current_mapping[tclass].perms[i])
235 result |= 1<<i;
236 avd->auditallow = result;
237
238 for (i = 0, result = 0; i < n; i++) {
239 if (avd->auditdeny & current_mapping[tclass].perms[i])
240 result |= 1<<i;
241 if (!allow_unknown && !current_mapping[tclass].perms[i])
242 result |= 1<<i;
243 }
244 /*
245 * In case the kernel has a bug and requests a permission
246 * between num_perms and the maximum permission number, we
247 * should audit that denial
248 */
249 for (; i < (sizeof(u32)*8); i++)
250 result |= 1<<i;
251 avd->auditdeny = result;
252 }
253 }
254
security_mls_enabled(void)255 int security_mls_enabled(void)
256 {
257 return policydb.mls_enabled;
258 }
259
260 /*
261 * Return the boolean value of a constraint expression
262 * when it is applied to the specified source and target
263 * security contexts.
264 *
265 * xcontext is a special beast... It is used by the validatetrans rules
266 * only. For these rules, scontext is the context before the transition,
267 * tcontext is the context after the transition, and xcontext is the context
268 * of the process performing the transition. All other callers of
269 * constraint_expr_eval should pass in NULL for xcontext.
270 */
constraint_expr_eval(struct context * scontext,struct context * tcontext,struct context * xcontext,struct constraint_expr * cexpr)271 static int constraint_expr_eval(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;
360 mls_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 * security_dump_masked_av - dumps masked permissions during
447 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
448 */
dump_masked_av_helper(void * k,void * d,void * args)449 static 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
security_dump_masked_av(struct context * scontext,struct context * tcontext,u16 tclass,u32 permissions,const char * reason)461 static void security_dump_masked_av(struct context *scontext,
462 struct context *tcontext,
463 u16 tclass,
464 u32 permissions,
465 const char *reason)
466 {
467 struct common_datum *common_dat;
468 struct class_datum *tclass_dat;
469 struct audit_buffer *ab;
470 char *tclass_name;
471 char *scontext_name = NULL;
472 char *tcontext_name = NULL;
473 char *permission_names[32];
474 int index;
475 u32 length;
476 bool need_comma = false;
477
478 if (!permissions)
479 return;
480
481 tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1);
482 tclass_dat = policydb.class_val_to_struct[tclass - 1];
483 common_dat = tclass_dat->comdatum;
484
485 /* init permission_names */
486 if (common_dat &&
487 hashtab_map(common_dat->permissions.table,
488 dump_masked_av_helper, permission_names) < 0)
489 goto out;
490
491 if (hashtab_map(tclass_dat->permissions.table,
492 dump_masked_av_helper, permission_names) < 0)
493 goto out;
494
495 /* get scontext/tcontext in text form */
496 if (context_struct_to_string(scontext,
497 &scontext_name, &length) < 0)
498 goto out;
499
500 if (context_struct_to_string(tcontext,
501 &tcontext_name, &length) < 0)
502 goto out;
503
504 /* audit a message */
505 ab = audit_log_start(current->audit_context,
506 GFP_ATOMIC, AUDIT_SELINUX_ERR);
507 if (!ab)
508 goto out;
509
510 audit_log_format(ab, "op=security_compute_av reason=%s "
511 "scontext=%s tcontext=%s tclass=%s perms=",
512 reason, scontext_name, tcontext_name, tclass_name);
513
514 for (index = 0; index < 32; index++) {
515 u32 mask = (1 << index);
516
517 if ((mask & permissions) == 0)
518 continue;
519
520 audit_log_format(ab, "%s%s",
521 need_comma ? "," : "",
522 permission_names[index]
523 ? permission_names[index] : "????");
524 need_comma = true;
525 }
526 audit_log_end(ab);
527 out:
528 /* release scontext/tcontext */
529 kfree(tcontext_name);
530 kfree(scontext_name);
531
532 return;
533 }
534
535 /*
536 * security_boundary_permission - drops violated permissions
537 * on boundary constraint.
538 */
type_attribute_bounds_av(struct context * scontext,struct context * tcontext,u16 tclass,struct av_decision * avd)539 static void type_attribute_bounds_av(struct context *scontext,
540 struct context *tcontext,
541 u16 tclass,
542 struct av_decision *avd)
543 {
544 struct context lo_scontext;
545 struct context lo_tcontext;
546 struct av_decision lo_avd;
547 struct type_datum *source;
548 struct type_datum *target;
549 u32 masked = 0;
550
551 source = flex_array_get_ptr(policydb.type_val_to_struct_array,
552 scontext->type - 1);
553 BUG_ON(!source);
554
555 target = flex_array_get_ptr(policydb.type_val_to_struct_array,
556 tcontext->type - 1);
557 BUG_ON(!target);
558
559 if (source->bounds) {
560 memset(&lo_avd, 0, sizeof(lo_avd));
561
562 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
563 lo_scontext.type = source->bounds;
564
565 context_struct_compute_av(&lo_scontext,
566 tcontext,
567 tclass,
568 &lo_avd,
569 NULL);
570 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
571 return; /* no masked permission */
572 masked = ~lo_avd.allowed & avd->allowed;
573 }
574
575 if (target->bounds) {
576 memset(&lo_avd, 0, sizeof(lo_avd));
577
578 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
579 lo_tcontext.type = target->bounds;
580
581 context_struct_compute_av(scontext,
582 &lo_tcontext,
583 tclass,
584 &lo_avd,
585 NULL);
586 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
587 return; /* no masked permission */
588 masked = ~lo_avd.allowed & avd->allowed;
589 }
590
591 if (source->bounds && target->bounds) {
592 memset(&lo_avd, 0, sizeof(lo_avd));
593 /*
594 * lo_scontext and lo_tcontext are already
595 * set up.
596 */
597
598 context_struct_compute_av(&lo_scontext,
599 &lo_tcontext,
600 tclass,
601 &lo_avd,
602 NULL);
603 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
604 return; /* no masked permission */
605 masked = ~lo_avd.allowed & avd->allowed;
606 }
607
608 if (masked) {
609 /* mask violated permissions */
610 avd->allowed &= ~masked;
611
612 /* audit masked permissions */
613 security_dump_masked_av(scontext, tcontext,
614 tclass, masked, "bounds");
615 }
616 }
617
618 /*
619 * flag which drivers have permissions
620 * only looking for ioctl based extended permssions
621 */
services_compute_xperms_drivers(struct extended_perms * xperms,struct avtab_node * node)622 void services_compute_xperms_drivers(
623 struct extended_perms *xperms,
624 struct avtab_node *node)
625 {
626 unsigned int i;
627
628 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
629 /* if one or more driver has all permissions allowed */
630 for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++)
631 xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i];
632 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
633 /* if allowing permissions within a driver */
634 security_xperm_set(xperms->drivers.p,
635 node->datum.u.xperms->driver);
636 }
637
638 /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
639 if (node->key.specified & AVTAB_XPERMS_ALLOWED)
640 xperms->len = 1;
641 }
642
643 /*
644 * Compute access vectors and extended permissions based on a context
645 * structure pair for the permissions in a particular class.
646 */
context_struct_compute_av(struct context * scontext,struct context * tcontext,u16 tclass,struct av_decision * avd,struct extended_perms * xperms)647 static void context_struct_compute_av(struct context *scontext,
648 struct context *tcontext,
649 u16 tclass,
650 struct av_decision *avd,
651 struct extended_perms *xperms)
652 {
653 struct constraint_node *constraint;
654 struct role_allow *ra;
655 struct avtab_key avkey;
656 struct avtab_node *node;
657 struct class_datum *tclass_datum;
658 struct ebitmap *sattr, *tattr;
659 struct ebitmap_node *snode, *tnode;
660 unsigned int i, j;
661
662 avd->allowed = 0;
663 avd->auditallow = 0;
664 avd->auditdeny = 0xffffffff;
665 if (xperms) {
666 memset(&xperms->drivers, 0, sizeof(xperms->drivers));
667 xperms->len = 0;
668 }
669
670 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
671 if (printk_ratelimit())
672 printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
673 return;
674 }
675
676 tclass_datum = policydb.class_val_to_struct[tclass - 1];
677
678 /*
679 * If a specific type enforcement rule was defined for
680 * this permission check, then use it.
681 */
682 avkey.target_class = tclass;
683 avkey.specified = AVTAB_AV | AVTAB_XPERMS;
684 sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
685 BUG_ON(!sattr);
686 tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
687 BUG_ON(!tattr);
688 ebitmap_for_each_positive_bit(sattr, snode, i) {
689 ebitmap_for_each_positive_bit(tattr, tnode, j) {
690 avkey.source_type = i + 1;
691 avkey.target_type = j + 1;
692 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
693 node;
694 node = avtab_search_node_next(node, avkey.specified)) {
695 if (node->key.specified == AVTAB_ALLOWED)
696 avd->allowed |= node->datum.u.data;
697 else if (node->key.specified == AVTAB_AUDITALLOW)
698 avd->auditallow |= node->datum.u.data;
699 else if (node->key.specified == AVTAB_AUDITDENY)
700 avd->auditdeny &= node->datum.u.data;
701 else if (xperms && (node->key.specified & AVTAB_XPERMS))
702 services_compute_xperms_drivers(xperms, node);
703 }
704
705 /* Check conditional av table for additional permissions */
706 cond_compute_av(&policydb.te_cond_avtab, &avkey,
707 avd, xperms);
708
709 }
710 }
711
712 /*
713 * Remove any permissions prohibited by a constraint (this includes
714 * the MLS policy).
715 */
716 constraint = tclass_datum->constraints;
717 while (constraint) {
718 if ((constraint->permissions & (avd->allowed)) &&
719 !constraint_expr_eval(scontext, tcontext, NULL,
720 constraint->expr)) {
721 avd->allowed &= ~(constraint->permissions);
722 }
723 constraint = constraint->next;
724 }
725
726 /*
727 * If checking process transition permission and the
728 * role is changing, then check the (current_role, new_role)
729 * pair.
730 */
731 if (tclass == policydb.process_class &&
732 (avd->allowed & policydb.process_trans_perms) &&
733 scontext->role != tcontext->role) {
734 for (ra = policydb.role_allow; ra; ra = ra->next) {
735 if (scontext->role == ra->role &&
736 tcontext->role == ra->new_role)
737 break;
738 }
739 if (!ra)
740 avd->allowed &= ~policydb.process_trans_perms;
741 }
742
743 /*
744 * If the given source and target types have boundary
745 * constraint, lazy checks have to mask any violated
746 * permission and notice it to userspace via audit.
747 */
748 type_attribute_bounds_av(scontext, tcontext,
749 tclass, avd);
750 }
751
security_validtrans_handle_fail(struct context * ocontext,struct context * ncontext,struct context * tcontext,u16 tclass)752 static int security_validtrans_handle_fail(struct context *ocontext,
753 struct context *ncontext,
754 struct context *tcontext,
755 u16 tclass)
756 {
757 char *o = NULL, *n = NULL, *t = NULL;
758 u32 olen, nlen, tlen;
759
760 if (context_struct_to_string(ocontext, &o, &olen))
761 goto out;
762 if (context_struct_to_string(ncontext, &n, &nlen))
763 goto out;
764 if (context_struct_to_string(tcontext, &t, &tlen))
765 goto out;
766 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
767 "security_validate_transition: denied for"
768 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
769 o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
770 out:
771 kfree(o);
772 kfree(n);
773 kfree(t);
774
775 if (!selinux_enforcing)
776 return 0;
777 return -EPERM;
778 }
779
security_validate_transition(u32 oldsid,u32 newsid,u32 tasksid,u16 orig_tclass)780 int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
781 u16 orig_tclass)
782 {
783 struct context *ocontext;
784 struct context *ncontext;
785 struct context *tcontext;
786 struct class_datum *tclass_datum;
787 struct constraint_node *constraint;
788 u16 tclass;
789 int rc = 0;
790
791 if (!ss_initialized)
792 return 0;
793
794 read_lock(&policy_rwlock);
795
796 tclass = unmap_class(orig_tclass);
797
798 if (!tclass || tclass > policydb.p_classes.nprim) {
799 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
800 __func__, tclass);
801 rc = -EINVAL;
802 goto out;
803 }
804 tclass_datum = policydb.class_val_to_struct[tclass - 1];
805
806 ocontext = sidtab_search(&sidtab, oldsid);
807 if (!ocontext) {
808 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
809 __func__, oldsid);
810 rc = -EINVAL;
811 goto out;
812 }
813
814 ncontext = sidtab_search(&sidtab, newsid);
815 if (!ncontext) {
816 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
817 __func__, newsid);
818 rc = -EINVAL;
819 goto out;
820 }
821
822 tcontext = sidtab_search(&sidtab, tasksid);
823 if (!tcontext) {
824 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
825 __func__, tasksid);
826 rc = -EINVAL;
827 goto out;
828 }
829
830 constraint = tclass_datum->validatetrans;
831 while (constraint) {
832 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
833 constraint->expr)) {
834 rc = security_validtrans_handle_fail(ocontext, ncontext,
835 tcontext, tclass);
836 goto out;
837 }
838 constraint = constraint->next;
839 }
840
841 out:
842 read_unlock(&policy_rwlock);
843 return rc;
844 }
845
846 /*
847 * security_bounded_transition - check whether the given
848 * transition is directed to bounded, or not.
849 * It returns 0, if @newsid is bounded by @oldsid.
850 * Otherwise, it returns error code.
851 *
852 * @oldsid : current security identifier
853 * @newsid : destinated security identifier
854 */
security_bounded_transition(u32 old_sid,u32 new_sid)855 int security_bounded_transition(u32 old_sid, u32 new_sid)
856 {
857 struct context *old_context, *new_context;
858 struct type_datum *type;
859 int index;
860 int rc;
861
862 read_lock(&policy_rwlock);
863
864 rc = -EINVAL;
865 old_context = sidtab_search(&sidtab, old_sid);
866 if (!old_context) {
867 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
868 __func__, old_sid);
869 goto out;
870 }
871
872 rc = -EINVAL;
873 new_context = sidtab_search(&sidtab, new_sid);
874 if (!new_context) {
875 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
876 __func__, new_sid);
877 goto out;
878 }
879
880 rc = 0;
881 /* type/domain unchanged */
882 if (old_context->type == new_context->type)
883 goto out;
884
885 index = new_context->type;
886 while (true) {
887 type = flex_array_get_ptr(policydb.type_val_to_struct_array,
888 index - 1);
889 BUG_ON(!type);
890
891 /* not bounded anymore */
892 rc = -EPERM;
893 if (!type->bounds)
894 break;
895
896 /* @newsid is bounded by @oldsid */
897 rc = 0;
898 if (type->bounds == old_context->type)
899 break;
900
901 index = type->bounds;
902 }
903
904 if (rc) {
905 char *old_name = NULL;
906 char *new_name = NULL;
907 u32 length;
908
909 if (!context_struct_to_string(old_context,
910 &old_name, &length) &&
911 !context_struct_to_string(new_context,
912 &new_name, &length)) {
913 audit_log(current->audit_context,
914 GFP_ATOMIC, AUDIT_SELINUX_ERR,
915 "op=security_bounded_transition "
916 "result=denied "
917 "oldcontext=%s newcontext=%s",
918 old_name, new_name);
919 }
920 kfree(new_name);
921 kfree(old_name);
922 }
923 out:
924 read_unlock(&policy_rwlock);
925
926 return rc;
927 }
928
avd_init(struct av_decision * avd)929 static void avd_init(struct av_decision *avd)
930 {
931 avd->allowed = 0;
932 avd->auditallow = 0;
933 avd->auditdeny = 0xffffffff;
934 avd->seqno = latest_granting;
935 avd->flags = 0;
936 }
937
services_compute_xperms_decision(struct extended_perms_decision * xpermd,struct avtab_node * node)938 void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
939 struct avtab_node *node)
940 {
941 unsigned int i;
942
943 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
944 if (xpermd->driver != node->datum.u.xperms->driver)
945 return;
946 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
947 if (!security_xperm_test(node->datum.u.xperms->perms.p,
948 xpermd->driver))
949 return;
950 } else {
951 BUG();
952 }
953
954 if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
955 xpermd->used |= XPERMS_ALLOWED;
956 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
957 memset(xpermd->allowed->p, 0xff,
958 sizeof(xpermd->allowed->p));
959 }
960 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
961 for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
962 xpermd->allowed->p[i] |=
963 node->datum.u.xperms->perms.p[i];
964 }
965 } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
966 xpermd->used |= XPERMS_AUDITALLOW;
967 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
968 memset(xpermd->auditallow->p, 0xff,
969 sizeof(xpermd->auditallow->p));
970 }
971 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
972 for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
973 xpermd->auditallow->p[i] |=
974 node->datum.u.xperms->perms.p[i];
975 }
976 } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
977 xpermd->used |= XPERMS_DONTAUDIT;
978 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
979 memset(xpermd->dontaudit->p, 0xff,
980 sizeof(xpermd->dontaudit->p));
981 }
982 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
983 for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
984 xpermd->dontaudit->p[i] |=
985 node->datum.u.xperms->perms.p[i];
986 }
987 } else {
988 BUG();
989 }
990 }
991
security_compute_xperms_decision(u32 ssid,u32 tsid,u16 orig_tclass,u8 driver,struct extended_perms_decision * xpermd)992 void security_compute_xperms_decision(u32 ssid,
993 u32 tsid,
994 u16 orig_tclass,
995 u8 driver,
996 struct extended_perms_decision *xpermd)
997 {
998 u16 tclass;
999 struct context *scontext, *tcontext;
1000 struct avtab_key avkey;
1001 struct avtab_node *node;
1002 struct ebitmap *sattr, *tattr;
1003 struct ebitmap_node *snode, *tnode;
1004 unsigned int i, j;
1005
1006 xpermd->driver = driver;
1007 xpermd->used = 0;
1008 memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
1009 memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
1010 memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
1011
1012 read_lock(&policy_rwlock);
1013 if (!ss_initialized)
1014 goto allow;
1015
1016 scontext = sidtab_search(&sidtab, ssid);
1017 if (!scontext) {
1018 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1019 __func__, ssid);
1020 goto out;
1021 }
1022
1023 tcontext = sidtab_search(&sidtab, tsid);
1024 if (!tcontext) {
1025 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1026 __func__, tsid);
1027 goto out;
1028 }
1029
1030 tclass = unmap_class(orig_tclass);
1031 if (unlikely(orig_tclass && !tclass)) {
1032 if (policydb.allow_unknown)
1033 goto allow;
1034 goto out;
1035 }
1036
1037
1038 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
1039 if (printk_ratelimit())
1040 printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
1041 goto out;
1042 }
1043
1044 avkey.target_class = tclass;
1045 avkey.specified = AVTAB_XPERMS;
1046 sattr = flex_array_get(policydb.type_attr_map_array,
1047 scontext->type - 1);
1048 BUG_ON(!sattr);
1049 tattr = flex_array_get(policydb.type_attr_map_array,
1050 tcontext->type - 1);
1051 BUG_ON(!tattr);
1052 ebitmap_for_each_positive_bit(sattr, snode, i) {
1053 ebitmap_for_each_positive_bit(tattr, tnode, j) {
1054 avkey.source_type = i + 1;
1055 avkey.target_type = j + 1;
1056 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
1057 node;
1058 node = avtab_search_node_next(node, avkey.specified))
1059 services_compute_xperms_decision(xpermd, node);
1060
1061 cond_compute_xperms(&policydb.te_cond_avtab,
1062 &avkey, xpermd);
1063 }
1064 }
1065 out:
1066 read_unlock(&policy_rwlock);
1067 return;
1068 allow:
1069 memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
1070 goto out;
1071 }
1072
1073 /**
1074 * security_compute_av - Compute access vector decisions.
1075 * @ssid: source security identifier
1076 * @tsid: target security identifier
1077 * @tclass: target security class
1078 * @avd: access vector decisions
1079 * @xperms: extended permissions
1080 *
1081 * Compute a set of access vector decisions based on the
1082 * SID pair (@ssid, @tsid) for the permissions in @tclass.
1083 */
security_compute_av(u32 ssid,u32 tsid,u16 orig_tclass,struct av_decision * avd,struct extended_perms * xperms)1084 void security_compute_av(u32 ssid,
1085 u32 tsid,
1086 u16 orig_tclass,
1087 struct av_decision *avd,
1088 struct extended_perms *xperms)
1089 {
1090 u16 tclass;
1091 struct context *scontext = NULL, *tcontext = NULL;
1092
1093 read_lock(&policy_rwlock);
1094 avd_init(avd);
1095 xperms->len = 0;
1096 if (!ss_initialized)
1097 goto allow;
1098
1099 scontext = sidtab_search(&sidtab, ssid);
1100 if (!scontext) {
1101 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1102 __func__, ssid);
1103 goto out;
1104 }
1105
1106 /* permissive domain? */
1107 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
1108 avd->flags |= AVD_FLAGS_PERMISSIVE;
1109
1110 tcontext = sidtab_search(&sidtab, tsid);
1111 if (!tcontext) {
1112 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1113 __func__, tsid);
1114 goto out;
1115 }
1116
1117 tclass = unmap_class(orig_tclass);
1118 if (unlikely(orig_tclass && !tclass)) {
1119 if (policydb.allow_unknown)
1120 goto allow;
1121 goto out;
1122 }
1123 context_struct_compute_av(scontext, tcontext, tclass, avd, xperms);
1124 map_decision(orig_tclass, avd, policydb.allow_unknown);
1125 out:
1126 read_unlock(&policy_rwlock);
1127 return;
1128 allow:
1129 avd->allowed = 0xffffffff;
1130 goto out;
1131 }
1132
security_compute_av_user(u32 ssid,u32 tsid,u16 tclass,struct av_decision * avd)1133 void security_compute_av_user(u32 ssid,
1134 u32 tsid,
1135 u16 tclass,
1136 struct av_decision *avd)
1137 {
1138 struct context *scontext = NULL, *tcontext = NULL;
1139
1140 read_lock(&policy_rwlock);
1141 avd_init(avd);
1142 if (!ss_initialized)
1143 goto allow;
1144
1145 scontext = sidtab_search(&sidtab, ssid);
1146 if (!scontext) {
1147 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1148 __func__, ssid);
1149 goto out;
1150 }
1151
1152 /* permissive domain? */
1153 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
1154 avd->flags |= AVD_FLAGS_PERMISSIVE;
1155
1156 tcontext = sidtab_search(&sidtab, tsid);
1157 if (!tcontext) {
1158 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1159 __func__, tsid);
1160 goto out;
1161 }
1162
1163 if (unlikely(!tclass)) {
1164 if (policydb.allow_unknown)
1165 goto allow;
1166 goto out;
1167 }
1168
1169 context_struct_compute_av(scontext, tcontext, tclass, avd, NULL);
1170 out:
1171 read_unlock(&policy_rwlock);
1172 return;
1173 allow:
1174 avd->allowed = 0xffffffff;
1175 goto out;
1176 }
1177
1178 /*
1179 * Write the security context string representation of
1180 * the context structure `context' into a dynamically
1181 * allocated string of the correct size. Set `*scontext'
1182 * to point to this string and set `*scontext_len' to
1183 * the length of the string.
1184 */
context_struct_to_string(struct context * context,char ** scontext,u32 * scontext_len)1185 static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
1186 {
1187 char *scontextp;
1188
1189 if (scontext)
1190 *scontext = NULL;
1191 *scontext_len = 0;
1192
1193 if (context->len) {
1194 *scontext_len = context->len;
1195 *scontext = kstrdup(context->str, GFP_ATOMIC);
1196 if (!(*scontext))
1197 return -ENOMEM;
1198 return 0;
1199 }
1200
1201 /* Compute the size of the context. */
1202 *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
1203 *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
1204 *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
1205 *scontext_len += mls_compute_context_len(context);
1206
1207 if (!scontext)
1208 return 0;
1209
1210 /* Allocate space for the context; caller must free this space. */
1211 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
1212 if (!scontextp)
1213 return -ENOMEM;
1214 *scontext = scontextp;
1215
1216 /*
1217 * Copy the user name, role name and type name into the context.
1218 */
1219 sprintf(scontextp, "%s:%s:%s",
1220 sym_name(&policydb, SYM_USERS, context->user - 1),
1221 sym_name(&policydb, SYM_ROLES, context->role - 1),
1222 sym_name(&policydb, SYM_TYPES, context->type - 1));
1223 scontextp += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) +
1224 1 + strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) +
1225 1 + strlen(sym_name(&policydb, SYM_TYPES, context->type - 1));
1226
1227 mls_sid_to_context(context, &scontextp);
1228
1229 *scontextp = 0;
1230
1231 return 0;
1232 }
1233
1234 #include "initial_sid_to_string.h"
1235
security_get_initial_sid_context(u32 sid)1236 const char *security_get_initial_sid_context(u32 sid)
1237 {
1238 if (unlikely(sid > SECINITSID_NUM))
1239 return NULL;
1240 return initial_sid_to_string[sid];
1241 }
1242
security_sid_to_context_core(u32 sid,char ** scontext,u32 * scontext_len,int force)1243 static int security_sid_to_context_core(u32 sid, char **scontext,
1244 u32 *scontext_len, int force)
1245 {
1246 struct context *context;
1247 int rc = 0;
1248
1249 if (scontext)
1250 *scontext = NULL;
1251 *scontext_len = 0;
1252
1253 if (!ss_initialized) {
1254 if (sid <= SECINITSID_NUM) {
1255 char *scontextp;
1256
1257 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
1258 if (!scontext)
1259 goto out;
1260 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
1261 if (!scontextp) {
1262 rc = -ENOMEM;
1263 goto out;
1264 }
1265 strcpy(scontextp, initial_sid_to_string[sid]);
1266 *scontext = scontextp;
1267 goto out;
1268 }
1269 printk(KERN_ERR "SELinux: %s: called before initial "
1270 "load_policy on unknown SID %d\n", __func__, sid);
1271 rc = -EINVAL;
1272 goto out;
1273 }
1274 read_lock(&policy_rwlock);
1275 if (force)
1276 context = sidtab_search_force(&sidtab, sid);
1277 else
1278 context = sidtab_search(&sidtab, sid);
1279 if (!context) {
1280 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1281 __func__, sid);
1282 rc = -EINVAL;
1283 goto out_unlock;
1284 }
1285 rc = context_struct_to_string(context, scontext, scontext_len);
1286 out_unlock:
1287 read_unlock(&policy_rwlock);
1288 out:
1289 return rc;
1290
1291 }
1292
1293 /**
1294 * security_sid_to_context - Obtain a context for a given SID.
1295 * @sid: security identifier, SID
1296 * @scontext: security context
1297 * @scontext_len: length in bytes
1298 *
1299 * Write the string representation of the context associated with @sid
1300 * into a dynamically allocated string of the correct size. Set @scontext
1301 * to point to this string and set @scontext_len to the length of the string.
1302 */
security_sid_to_context(u32 sid,char ** scontext,u32 * scontext_len)1303 int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
1304 {
1305 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
1306 }
1307
security_sid_to_context_force(u32 sid,char ** scontext,u32 * scontext_len)1308 int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
1309 {
1310 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
1311 }
1312
1313 /*
1314 * Caveat: Mutates scontext.
1315 */
string_to_context_struct(struct policydb * pol,struct sidtab * sidtabp,char * scontext,u32 scontext_len,struct context * ctx,u32 def_sid)1316 static int string_to_context_struct(struct policydb *pol,
1317 struct sidtab *sidtabp,
1318 char *scontext,
1319 u32 scontext_len,
1320 struct context *ctx,
1321 u32 def_sid)
1322 {
1323 struct role_datum *role;
1324 struct type_datum *typdatum;
1325 struct user_datum *usrdatum;
1326 char *scontextp, *p, oldc;
1327 int rc = 0;
1328
1329 context_init(ctx);
1330
1331 /* Parse the security context. */
1332
1333 rc = -EINVAL;
1334 scontextp = (char *) scontext;
1335
1336 /* Extract the user. */
1337 p = scontextp;
1338 while (*p && *p != ':')
1339 p++;
1340
1341 if (*p == 0)
1342 goto out;
1343
1344 *p++ = 0;
1345
1346 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1347 if (!usrdatum)
1348 goto out;
1349
1350 ctx->user = usrdatum->value;
1351
1352 /* Extract role. */
1353 scontextp = p;
1354 while (*p && *p != ':')
1355 p++;
1356
1357 if (*p == 0)
1358 goto out;
1359
1360 *p++ = 0;
1361
1362 role = hashtab_search(pol->p_roles.table, scontextp);
1363 if (!role)
1364 goto out;
1365 ctx->role = role->value;
1366
1367 /* Extract type. */
1368 scontextp = p;
1369 while (*p && *p != ':')
1370 p++;
1371 oldc = *p;
1372 *p++ = 0;
1373
1374 typdatum = hashtab_search(pol->p_types.table, scontextp);
1375 if (!typdatum || typdatum->attribute)
1376 goto out;
1377
1378 ctx->type = typdatum->value;
1379
1380 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
1381 if (rc)
1382 goto out;
1383
1384 rc = -EINVAL;
1385 if ((p - scontext) < scontext_len)
1386 goto out;
1387
1388 /* Check the validity of the new context. */
1389 if (!policydb_context_isvalid(pol, ctx))
1390 goto out;
1391 rc = 0;
1392 out:
1393 if (rc)
1394 context_destroy(ctx);
1395 return rc;
1396 }
1397
security_context_to_sid_core(const char * scontext,u32 scontext_len,u32 * sid,u32 def_sid,gfp_t gfp_flags,int force)1398 static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
1399 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1400 int force)
1401 {
1402 char *scontext2, *str = NULL;
1403 struct context context;
1404 int rc = 0;
1405
1406 /* An empty security context is never valid. */
1407 if (!scontext_len)
1408 return -EINVAL;
1409
1410 if (!ss_initialized) {
1411 int i;
1412
1413 for (i = 1; i < SECINITSID_NUM; i++) {
1414 if (!strcmp(initial_sid_to_string[i], scontext)) {
1415 *sid = i;
1416 return 0;
1417 }
1418 }
1419 *sid = SECINITSID_KERNEL;
1420 return 0;
1421 }
1422 *sid = SECSID_NULL;
1423
1424 /* Copy the string so that we can modify the copy as we parse it. */
1425 scontext2 = kmalloc(scontext_len + 1, gfp_flags);
1426 if (!scontext2)
1427 return -ENOMEM;
1428 memcpy(scontext2, scontext, scontext_len);
1429 scontext2[scontext_len] = 0;
1430
1431 if (force) {
1432 /* Save another copy for storing in uninterpreted form */
1433 rc = -ENOMEM;
1434 str = kstrdup(scontext2, gfp_flags);
1435 if (!str)
1436 goto out;
1437 }
1438
1439 read_lock(&policy_rwlock);
1440 rc = string_to_context_struct(&policydb, &sidtab, scontext2,
1441 scontext_len, &context, def_sid);
1442 if (rc == -EINVAL && force) {
1443 context.str = str;
1444 context.len = scontext_len;
1445 str = NULL;
1446 } else if (rc)
1447 goto out_unlock;
1448 rc = sidtab_context_to_sid(&sidtab, &context, sid);
1449 context_destroy(&context);
1450 out_unlock:
1451 read_unlock(&policy_rwlock);
1452 out:
1453 kfree(scontext2);
1454 kfree(str);
1455 return rc;
1456 }
1457
1458 /**
1459 * security_context_to_sid - Obtain a SID for a given security context.
1460 * @scontext: security context
1461 * @scontext_len: length in bytes
1462 * @sid: security identifier, SID
1463 *
1464 * Obtains a SID associated with the security context that
1465 * has the string representation specified by @scontext.
1466 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1467 * memory is available, or 0 on success.
1468 */
security_context_to_sid(const char * scontext,u32 scontext_len,u32 * sid)1469 int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
1470 {
1471 return security_context_to_sid_core(scontext, scontext_len,
1472 sid, SECSID_NULL, GFP_KERNEL, 0);
1473 }
1474
1475 /**
1476 * security_context_to_sid_default - Obtain a SID for a given security context,
1477 * falling back to specified default if needed.
1478 *
1479 * @scontext: security context
1480 * @scontext_len: length in bytes
1481 * @sid: security identifier, SID
1482 * @def_sid: default SID to assign on error
1483 *
1484 * Obtains a SID associated with the security context that
1485 * has the string representation specified by @scontext.
1486 * The default SID is passed to the MLS layer to be used to allow
1487 * kernel labeling of the MLS field if the MLS field is not present
1488 * (for upgrading to MLS without full relabel).
1489 * Implicitly forces adding of the context even if it cannot be mapped yet.
1490 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1491 * memory is available, or 0 on success.
1492 */
security_context_to_sid_default(const char * scontext,u32 scontext_len,u32 * sid,u32 def_sid,gfp_t gfp_flags)1493 int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1494 u32 *sid, u32 def_sid, gfp_t gfp_flags)
1495 {
1496 return security_context_to_sid_core(scontext, scontext_len,
1497 sid, def_sid, gfp_flags, 1);
1498 }
1499
security_context_to_sid_force(const char * scontext,u32 scontext_len,u32 * sid)1500 int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1501 u32 *sid)
1502 {
1503 return security_context_to_sid_core(scontext, scontext_len,
1504 sid, SECSID_NULL, GFP_KERNEL, 1);
1505 }
1506
compute_sid_handle_invalid_context(struct context * scontext,struct context * tcontext,u16 tclass,struct context * newcontext)1507 static int compute_sid_handle_invalid_context(
1508 struct context *scontext,
1509 struct context *tcontext,
1510 u16 tclass,
1511 struct context *newcontext)
1512 {
1513 char *s = NULL, *t = NULL, *n = NULL;
1514 u32 slen, tlen, nlen;
1515
1516 if (context_struct_to_string(scontext, &s, &slen))
1517 goto out;
1518 if (context_struct_to_string(tcontext, &t, &tlen))
1519 goto out;
1520 if (context_struct_to_string(newcontext, &n, &nlen))
1521 goto out;
1522 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1523 "security_compute_sid: invalid context %s"
1524 " for scontext=%s"
1525 " tcontext=%s"
1526 " tclass=%s",
1527 n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
1528 out:
1529 kfree(s);
1530 kfree(t);
1531 kfree(n);
1532 if (!selinux_enforcing)
1533 return 0;
1534 return -EACCES;
1535 }
1536
filename_compute_type(struct policydb * p,struct context * newcontext,u32 stype,u32 ttype,u16 tclass,const char * objname)1537 static void filename_compute_type(struct policydb *p, struct context *newcontext,
1538 u32 stype, u32 ttype, u16 tclass,
1539 const char *objname)
1540 {
1541 struct filename_trans ft;
1542 struct filename_trans_datum *otype;
1543
1544 /*
1545 * Most filename trans rules are going to live in specific directories
1546 * like /dev or /var/run. This bitmap will quickly skip rule searches
1547 * if the ttype does not contain any rules.
1548 */
1549 if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
1550 return;
1551
1552 ft.stype = stype;
1553 ft.ttype = ttype;
1554 ft.tclass = tclass;
1555 ft.name = objname;
1556
1557 otype = hashtab_search(p->filename_trans, &ft);
1558 if (otype)
1559 newcontext->type = otype->otype;
1560 }
1561
security_compute_sid(u32 ssid,u32 tsid,u16 orig_tclass,u32 specified,const char * objname,u32 * out_sid,bool kern)1562 static int security_compute_sid(u32 ssid,
1563 u32 tsid,
1564 u16 orig_tclass,
1565 u32 specified,
1566 const char *objname,
1567 u32 *out_sid,
1568 bool kern)
1569 {
1570 struct class_datum *cladatum = NULL;
1571 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1572 struct role_trans *roletr = NULL;
1573 struct avtab_key avkey;
1574 struct avtab_datum *avdatum;
1575 struct avtab_node *node;
1576 u16 tclass;
1577 int rc = 0;
1578 bool sock;
1579
1580 if (!ss_initialized) {
1581 switch (orig_tclass) {
1582 case SECCLASS_PROCESS: /* kernel value */
1583 *out_sid = ssid;
1584 break;
1585 default:
1586 *out_sid = tsid;
1587 break;
1588 }
1589 goto out;
1590 }
1591
1592 context_init(&newcontext);
1593
1594 read_lock(&policy_rwlock);
1595
1596 if (kern) {
1597 tclass = unmap_class(orig_tclass);
1598 sock = security_is_socket_class(orig_tclass);
1599 } else {
1600 tclass = orig_tclass;
1601 sock = security_is_socket_class(map_class(tclass));
1602 }
1603
1604 scontext = sidtab_search(&sidtab, ssid);
1605 if (!scontext) {
1606 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1607 __func__, ssid);
1608 rc = -EINVAL;
1609 goto out_unlock;
1610 }
1611 tcontext = sidtab_search(&sidtab, tsid);
1612 if (!tcontext) {
1613 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1614 __func__, tsid);
1615 rc = -EINVAL;
1616 goto out_unlock;
1617 }
1618
1619 if (tclass && tclass <= policydb.p_classes.nprim)
1620 cladatum = policydb.class_val_to_struct[tclass - 1];
1621
1622 /* Set the user identity. */
1623 switch (specified) {
1624 case AVTAB_TRANSITION:
1625 case AVTAB_CHANGE:
1626 if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
1627 newcontext.user = tcontext->user;
1628 } else {
1629 /* notice this gets both DEFAULT_SOURCE and unset */
1630 /* Use the process user identity. */
1631 newcontext.user = scontext->user;
1632 }
1633 break;
1634 case AVTAB_MEMBER:
1635 /* Use the related object owner. */
1636 newcontext.user = tcontext->user;
1637 break;
1638 }
1639
1640 /* Set the role to default values. */
1641 if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
1642 newcontext.role = scontext->role;
1643 } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
1644 newcontext.role = tcontext->role;
1645 } else {
1646 if ((tclass == policydb.process_class) || (sock == true))
1647 newcontext.role = scontext->role;
1648 else
1649 newcontext.role = OBJECT_R_VAL;
1650 }
1651
1652 /* Set the type to default values. */
1653 if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
1654 newcontext.type = scontext->type;
1655 } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
1656 newcontext.type = tcontext->type;
1657 } else {
1658 if ((tclass == policydb.process_class) || (sock == true)) {
1659 /* Use the type of process. */
1660 newcontext.type = scontext->type;
1661 } else {
1662 /* Use the type of the related object. */
1663 newcontext.type = tcontext->type;
1664 }
1665 }
1666
1667 /* Look for a type transition/member/change rule. */
1668 avkey.source_type = scontext->type;
1669 avkey.target_type = tcontext->type;
1670 avkey.target_class = tclass;
1671 avkey.specified = specified;
1672 avdatum = avtab_search(&policydb.te_avtab, &avkey);
1673
1674 /* If no permanent rule, also check for enabled conditional rules */
1675 if (!avdatum) {
1676 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
1677 for (; node; node = avtab_search_node_next(node, specified)) {
1678 if (node->key.specified & AVTAB_ENABLED) {
1679 avdatum = &node->datum;
1680 break;
1681 }
1682 }
1683 }
1684
1685 if (avdatum) {
1686 /* Use the type from the type transition/member/change rule. */
1687 newcontext.type = avdatum->u.data;
1688 }
1689
1690 /* if we have a objname this is a file trans check so check those rules */
1691 if (objname)
1692 filename_compute_type(&policydb, &newcontext, scontext->type,
1693 tcontext->type, tclass, objname);
1694
1695 /* Check for class-specific changes. */
1696 if (specified & AVTAB_TRANSITION) {
1697 /* Look for a role transition rule. */
1698 for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
1699 if ((roletr->role == scontext->role) &&
1700 (roletr->type == tcontext->type) &&
1701 (roletr->tclass == tclass)) {
1702 /* Use the role transition rule. */
1703 newcontext.role = roletr->new_role;
1704 break;
1705 }
1706 }
1707 }
1708
1709 /* Set the MLS attributes.
1710 This is done last because it may allocate memory. */
1711 rc = mls_compute_sid(scontext, tcontext, tclass, specified,
1712 &newcontext, sock);
1713 if (rc)
1714 goto out_unlock;
1715
1716 /* Check the validity of the context. */
1717 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1718 rc = compute_sid_handle_invalid_context(scontext,
1719 tcontext,
1720 tclass,
1721 &newcontext);
1722 if (rc)
1723 goto out_unlock;
1724 }
1725 /* Obtain the sid for the context. */
1726 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1727 out_unlock:
1728 read_unlock(&policy_rwlock);
1729 context_destroy(&newcontext);
1730 out:
1731 return rc;
1732 }
1733
1734 /**
1735 * security_transition_sid - Compute the SID for a new subject/object.
1736 * @ssid: source security identifier
1737 * @tsid: target security identifier
1738 * @tclass: target security class
1739 * @out_sid: security identifier for new subject/object
1740 *
1741 * Compute a SID to use for labeling a new subject or object in the
1742 * class @tclass based on a SID pair (@ssid, @tsid).
1743 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1744 * if insufficient memory is available, or %0 if the new SID was
1745 * computed successfully.
1746 */
security_transition_sid(u32 ssid,u32 tsid,u16 tclass,const struct qstr * qstr,u32 * out_sid)1747 int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
1748 const struct qstr *qstr, u32 *out_sid)
1749 {
1750 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1751 qstr ? qstr->name : NULL, out_sid, true);
1752 }
1753
security_transition_sid_user(u32 ssid,u32 tsid,u16 tclass,const char * objname,u32 * out_sid)1754 int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
1755 const char *objname, u32 *out_sid)
1756 {
1757 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1758 objname, out_sid, false);
1759 }
1760
1761 /**
1762 * security_member_sid - Compute the SID for member selection.
1763 * @ssid: source security identifier
1764 * @tsid: target security identifier
1765 * @tclass: target security class
1766 * @out_sid: security identifier for selected member
1767 *
1768 * Compute a SID to use when selecting a member of a polyinstantiated
1769 * object of class @tclass based on a SID pair (@ssid, @tsid).
1770 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1771 * if insufficient memory is available, or %0 if the SID was
1772 * computed successfully.
1773 */
security_member_sid(u32 ssid,u32 tsid,u16 tclass,u32 * out_sid)1774 int security_member_sid(u32 ssid,
1775 u32 tsid,
1776 u16 tclass,
1777 u32 *out_sid)
1778 {
1779 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
1780 out_sid, false);
1781 }
1782
1783 /**
1784 * security_change_sid - Compute the SID for object relabeling.
1785 * @ssid: source security identifier
1786 * @tsid: target security identifier
1787 * @tclass: target security class
1788 * @out_sid: security identifier for selected member
1789 *
1790 * Compute a SID to use for relabeling an object of class @tclass
1791 * based on a SID pair (@ssid, @tsid).
1792 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1793 * if insufficient memory is available, or %0 if the SID was
1794 * computed successfully.
1795 */
security_change_sid(u32 ssid,u32 tsid,u16 tclass,u32 * out_sid)1796 int security_change_sid(u32 ssid,
1797 u32 tsid,
1798 u16 tclass,
1799 u32 *out_sid)
1800 {
1801 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
1802 out_sid, false);
1803 }
1804
1805 /* Clone the SID into the new SID table. */
clone_sid(u32 sid,struct context * context,void * arg)1806 static int clone_sid(u32 sid,
1807 struct context *context,
1808 void *arg)
1809 {
1810 struct sidtab *s = arg;
1811
1812 if (sid > SECINITSID_NUM)
1813 return sidtab_insert(s, sid, context);
1814 else
1815 return 0;
1816 }
1817
convert_context_handle_invalid_context(struct context * context)1818 static inline int convert_context_handle_invalid_context(struct context *context)
1819 {
1820 char *s;
1821 u32 len;
1822
1823 if (selinux_enforcing)
1824 return -EINVAL;
1825
1826 if (!context_struct_to_string(context, &s, &len)) {
1827 printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
1828 kfree(s);
1829 }
1830 return 0;
1831 }
1832
1833 struct convert_context_args {
1834 struct policydb *oldp;
1835 struct policydb *newp;
1836 };
1837
1838 /*
1839 * Convert the values in the security context
1840 * structure `c' from the values specified
1841 * in the policy `p->oldp' to the values specified
1842 * in the policy `p->newp'. Verify that the
1843 * context is valid under the new policy.
1844 */
convert_context(u32 key,struct context * c,void * p)1845 static int convert_context(u32 key,
1846 struct context *c,
1847 void *p)
1848 {
1849 struct convert_context_args *args;
1850 struct context oldc;
1851 struct ocontext *oc;
1852 struct mls_range *range;
1853 struct role_datum *role;
1854 struct type_datum *typdatum;
1855 struct user_datum *usrdatum;
1856 char *s;
1857 u32 len;
1858 int rc = 0;
1859
1860 if (key <= SECINITSID_NUM)
1861 goto out;
1862
1863 args = p;
1864
1865 if (c->str) {
1866 struct context ctx;
1867
1868 rc = -ENOMEM;
1869 s = kstrdup(c->str, GFP_KERNEL);
1870 if (!s)
1871 goto out;
1872
1873 rc = string_to_context_struct(args->newp, NULL, s,
1874 c->len, &ctx, SECSID_NULL);
1875 kfree(s);
1876 if (!rc) {
1877 printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
1878 c->str);
1879 /* Replace string with mapped representation. */
1880 kfree(c->str);
1881 memcpy(c, &ctx, sizeof(*c));
1882 goto out;
1883 } else if (rc == -EINVAL) {
1884 /* Retain string representation for later mapping. */
1885 rc = 0;
1886 goto out;
1887 } else {
1888 /* Other error condition, e.g. ENOMEM. */
1889 printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
1890 c->str, -rc);
1891 goto out;
1892 }
1893 }
1894
1895 rc = context_cpy(&oldc, c);
1896 if (rc)
1897 goto out;
1898
1899 /* Convert the user. */
1900 rc = -EINVAL;
1901 usrdatum = hashtab_search(args->newp->p_users.table,
1902 sym_name(args->oldp, SYM_USERS, c->user - 1));
1903 if (!usrdatum)
1904 goto bad;
1905 c->user = usrdatum->value;
1906
1907 /* Convert the role. */
1908 rc = -EINVAL;
1909 role = hashtab_search(args->newp->p_roles.table,
1910 sym_name(args->oldp, SYM_ROLES, c->role - 1));
1911 if (!role)
1912 goto bad;
1913 c->role = role->value;
1914
1915 /* Convert the type. */
1916 rc = -EINVAL;
1917 typdatum = hashtab_search(args->newp->p_types.table,
1918 sym_name(args->oldp, SYM_TYPES, c->type - 1));
1919 if (!typdatum)
1920 goto bad;
1921 c->type = typdatum->value;
1922
1923 /* Convert the MLS fields if dealing with MLS policies */
1924 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
1925 rc = mls_convert_context(args->oldp, args->newp, c);
1926 if (rc)
1927 goto bad;
1928 } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
1929 /*
1930 * Switching between MLS and non-MLS policy:
1931 * free any storage used by the MLS fields in the
1932 * context for all existing entries in the sidtab.
1933 */
1934 mls_context_destroy(c);
1935 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
1936 /*
1937 * Switching between non-MLS and MLS policy:
1938 * ensure that the MLS fields of the context for all
1939 * existing entries in the sidtab are filled in with a
1940 * suitable default value, likely taken from one of the
1941 * initial SIDs.
1942 */
1943 oc = args->newp->ocontexts[OCON_ISID];
1944 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
1945 oc = oc->next;
1946 rc = -EINVAL;
1947 if (!oc) {
1948 printk(KERN_ERR "SELinux: unable to look up"
1949 " the initial SIDs list\n");
1950 goto bad;
1951 }
1952 range = &oc->context[0].range;
1953 rc = mls_range_set(c, range);
1954 if (rc)
1955 goto bad;
1956 }
1957
1958 /* Check the validity of the new context. */
1959 if (!policydb_context_isvalid(args->newp, c)) {
1960 rc = convert_context_handle_invalid_context(&oldc);
1961 if (rc)
1962 goto bad;
1963 }
1964
1965 context_destroy(&oldc);
1966
1967 rc = 0;
1968 out:
1969 return rc;
1970 bad:
1971 /* Map old representation to string and save it. */
1972 rc = context_struct_to_string(&oldc, &s, &len);
1973 if (rc)
1974 return rc;
1975 context_destroy(&oldc);
1976 context_destroy(c);
1977 c->str = s;
1978 c->len = len;
1979 printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
1980 c->str);
1981 rc = 0;
1982 goto out;
1983 }
1984
security_load_policycaps(void)1985 static void security_load_policycaps(void)
1986 {
1987 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1988 POLICYDB_CAPABILITY_NETPEER);
1989 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1990 POLICYDB_CAPABILITY_OPENPERM);
1991 }
1992
1993 static int security_preserve_bools(struct policydb *p);
1994
1995 /**
1996 * security_load_policy - Load a security policy configuration.
1997 * @data: binary policy data
1998 * @len: length of data in bytes
1999 *
2000 * Load a new set of security policy configuration data,
2001 * validate it and convert the SID table as necessary.
2002 * This function will flush the access vector cache after
2003 * loading the new policy.
2004 */
security_load_policy(void * data,size_t len)2005 int security_load_policy(void *data, size_t len)
2006 {
2007 struct policydb oldpolicydb, newpolicydb;
2008 struct sidtab oldsidtab, newsidtab;
2009 struct selinux_mapping *oldmap, *map = NULL;
2010 struct convert_context_args args;
2011 u32 seqno;
2012 u16 map_size;
2013 int rc = 0;
2014 struct policy_file file = { data, len }, *fp = &file;
2015
2016 if (!ss_initialized) {
2017 avtab_cache_init();
2018 rc = policydb_read(&policydb, fp);
2019 if (rc) {
2020 avtab_cache_destroy();
2021 return rc;
2022 }
2023
2024 policydb.len = len;
2025 rc = selinux_set_mapping(&policydb, secclass_map,
2026 ¤t_mapping,
2027 ¤t_mapping_size);
2028 if (rc) {
2029 policydb_destroy(&policydb);
2030 avtab_cache_destroy();
2031 return rc;
2032 }
2033
2034 rc = policydb_load_isids(&policydb, &sidtab);
2035 if (rc) {
2036 policydb_destroy(&policydb);
2037 avtab_cache_destroy();
2038 return rc;
2039 }
2040
2041 security_load_policycaps();
2042 ss_initialized = 1;
2043 seqno = ++latest_granting;
2044 selinux_complete_init();
2045 avc_ss_reset(seqno);
2046 selnl_notify_policyload(seqno);
2047 selinux_status_update_policyload(seqno);
2048 selinux_netlbl_cache_invalidate();
2049 selinux_xfrm_notify_policyload();
2050 return 0;
2051 }
2052
2053 #if 0
2054 sidtab_hash_eval(&sidtab, "sids");
2055 #endif
2056
2057 rc = policydb_read(&newpolicydb, fp);
2058 if (rc)
2059 return rc;
2060
2061 newpolicydb.len = len;
2062 /* If switching between different policy types, log MLS status */
2063 if (policydb.mls_enabled && !newpolicydb.mls_enabled)
2064 printk(KERN_INFO "SELinux: Disabling MLS support...\n");
2065 else if (!policydb.mls_enabled && newpolicydb.mls_enabled)
2066 printk(KERN_INFO "SELinux: Enabling MLS support...\n");
2067
2068 rc = policydb_load_isids(&newpolicydb, &newsidtab);
2069 if (rc) {
2070 printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
2071 policydb_destroy(&newpolicydb);
2072 return rc;
2073 }
2074
2075 rc = selinux_set_mapping(&newpolicydb, secclass_map, &map, &map_size);
2076 if (rc)
2077 goto err;
2078
2079 rc = security_preserve_bools(&newpolicydb);
2080 if (rc) {
2081 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
2082 goto err;
2083 }
2084
2085 /* Clone the SID table. */
2086 sidtab_shutdown(&sidtab);
2087
2088 rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
2089 if (rc)
2090 goto err;
2091
2092 /*
2093 * Convert the internal representations of contexts
2094 * in the new SID table.
2095 */
2096 args.oldp = &policydb;
2097 args.newp = &newpolicydb;
2098 rc = sidtab_map(&newsidtab, convert_context, &args);
2099 if (rc) {
2100 printk(KERN_ERR "SELinux: unable to convert the internal"
2101 " representation of contexts in the new SID"
2102 " table\n");
2103 goto err;
2104 }
2105
2106 /* Save the old policydb and SID table to free later. */
2107 memcpy(&oldpolicydb, &policydb, sizeof policydb);
2108 sidtab_set(&oldsidtab, &sidtab);
2109
2110 /* Install the new policydb and SID table. */
2111 write_lock_irq(&policy_rwlock);
2112 memcpy(&policydb, &newpolicydb, sizeof policydb);
2113 sidtab_set(&sidtab, &newsidtab);
2114 security_load_policycaps();
2115 oldmap = current_mapping;
2116 current_mapping = map;
2117 current_mapping_size = map_size;
2118 seqno = ++latest_granting;
2119 write_unlock_irq(&policy_rwlock);
2120
2121 /* Free the old policydb and SID table. */
2122 policydb_destroy(&oldpolicydb);
2123 sidtab_destroy(&oldsidtab);
2124 kfree(oldmap);
2125
2126 avc_ss_reset(seqno);
2127 selnl_notify_policyload(seqno);
2128 selinux_status_update_policyload(seqno);
2129 selinux_netlbl_cache_invalidate();
2130 selinux_xfrm_notify_policyload();
2131
2132 return 0;
2133
2134 err:
2135 kfree(map);
2136 sidtab_destroy(&newsidtab);
2137 policydb_destroy(&newpolicydb);
2138 return rc;
2139
2140 }
2141
security_policydb_len(void)2142 size_t security_policydb_len(void)
2143 {
2144 size_t len;
2145
2146 read_lock(&policy_rwlock);
2147 len = policydb.len;
2148 read_unlock(&policy_rwlock);
2149
2150 return len;
2151 }
2152
2153 /**
2154 * security_port_sid - Obtain the SID for a port.
2155 * @protocol: protocol number
2156 * @port: port number
2157 * @out_sid: security identifier
2158 */
security_port_sid(u8 protocol,u16 port,u32 * out_sid)2159 int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
2160 {
2161 struct ocontext *c;
2162 int rc = 0;
2163
2164 read_lock(&policy_rwlock);
2165
2166 c = policydb.ocontexts[OCON_PORT];
2167 while (c) {
2168 if (c->u.port.protocol == protocol &&
2169 c->u.port.low_port <= port &&
2170 c->u.port.high_port >= port)
2171 break;
2172 c = c->next;
2173 }
2174
2175 if (c) {
2176 if (!c->sid[0]) {
2177 rc = sidtab_context_to_sid(&sidtab,
2178 &c->context[0],
2179 &c->sid[0]);
2180 if (rc)
2181 goto out;
2182 }
2183 *out_sid = c->sid[0];
2184 } else {
2185 *out_sid = SECINITSID_PORT;
2186 }
2187
2188 out:
2189 read_unlock(&policy_rwlock);
2190 return rc;
2191 }
2192
2193 /**
2194 * security_netif_sid - Obtain the SID for a network interface.
2195 * @name: interface name
2196 * @if_sid: interface SID
2197 */
security_netif_sid(char * name,u32 * if_sid)2198 int security_netif_sid(char *name, u32 *if_sid)
2199 {
2200 int rc = 0;
2201 struct ocontext *c;
2202
2203 read_lock(&policy_rwlock);
2204
2205 c = policydb.ocontexts[OCON_NETIF];
2206 while (c) {
2207 if (strcmp(name, c->u.name) == 0)
2208 break;
2209 c = c->next;
2210 }
2211
2212 if (c) {
2213 if (!c->sid[0] || !c->sid[1]) {
2214 rc = sidtab_context_to_sid(&sidtab,
2215 &c->context[0],
2216 &c->sid[0]);
2217 if (rc)
2218 goto out;
2219 rc = sidtab_context_to_sid(&sidtab,
2220 &c->context[1],
2221 &c->sid[1]);
2222 if (rc)
2223 goto out;
2224 }
2225 *if_sid = c->sid[0];
2226 } else
2227 *if_sid = SECINITSID_NETIF;
2228
2229 out:
2230 read_unlock(&policy_rwlock);
2231 return rc;
2232 }
2233
match_ipv6_addrmask(u32 * input,u32 * addr,u32 * mask)2234 static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
2235 {
2236 int i, fail = 0;
2237
2238 for (i = 0; i < 4; i++)
2239 if (addr[i] != (input[i] & mask[i])) {
2240 fail = 1;
2241 break;
2242 }
2243
2244 return !fail;
2245 }
2246
2247 /**
2248 * security_node_sid - Obtain the SID for a node (host).
2249 * @domain: communication domain aka address family
2250 * @addrp: address
2251 * @addrlen: address length in bytes
2252 * @out_sid: security identifier
2253 */
security_node_sid(u16 domain,void * addrp,u32 addrlen,u32 * out_sid)2254 int security_node_sid(u16 domain,
2255 void *addrp,
2256 u32 addrlen,
2257 u32 *out_sid)
2258 {
2259 int rc;
2260 struct ocontext *c;
2261
2262 read_lock(&policy_rwlock);
2263
2264 switch (domain) {
2265 case AF_INET: {
2266 u32 addr;
2267
2268 rc = -EINVAL;
2269 if (addrlen != sizeof(u32))
2270 goto out;
2271
2272 addr = *((u32 *)addrp);
2273
2274 c = policydb.ocontexts[OCON_NODE];
2275 while (c) {
2276 if (c->u.node.addr == (addr & c->u.node.mask))
2277 break;
2278 c = c->next;
2279 }
2280 break;
2281 }
2282
2283 case AF_INET6:
2284 rc = -EINVAL;
2285 if (addrlen != sizeof(u64) * 2)
2286 goto out;
2287 c = policydb.ocontexts[OCON_NODE6];
2288 while (c) {
2289 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2290 c->u.node6.mask))
2291 break;
2292 c = c->next;
2293 }
2294 break;
2295
2296 default:
2297 rc = 0;
2298 *out_sid = SECINITSID_NODE;
2299 goto out;
2300 }
2301
2302 if (c) {
2303 if (!c->sid[0]) {
2304 rc = sidtab_context_to_sid(&sidtab,
2305 &c->context[0],
2306 &c->sid[0]);
2307 if (rc)
2308 goto out;
2309 }
2310 *out_sid = c->sid[0];
2311 } else {
2312 *out_sid = SECINITSID_NODE;
2313 }
2314
2315 rc = 0;
2316 out:
2317 read_unlock(&policy_rwlock);
2318 return rc;
2319 }
2320
2321 #define SIDS_NEL 25
2322
2323 /**
2324 * security_get_user_sids - Obtain reachable SIDs for a user.
2325 * @fromsid: starting SID
2326 * @username: username
2327 * @sids: array of reachable SIDs for user
2328 * @nel: number of elements in @sids
2329 *
2330 * Generate the set of SIDs for legal security contexts
2331 * for a given user that can be reached by @fromsid.
2332 * Set *@sids to point to a dynamically allocated
2333 * array containing the set of SIDs. Set *@nel to the
2334 * number of elements in the array.
2335 */
2336
security_get_user_sids(u32 fromsid,char * username,u32 ** sids,u32 * nel)2337 int security_get_user_sids(u32 fromsid,
2338 char *username,
2339 u32 **sids,
2340 u32 *nel)
2341 {
2342 struct context *fromcon, usercon;
2343 u32 *mysids = NULL, *mysids2, sid;
2344 u32 mynel = 0, maxnel = SIDS_NEL;
2345 struct user_datum *user;
2346 struct role_datum *role;
2347 struct ebitmap_node *rnode, *tnode;
2348 int rc = 0, i, j;
2349
2350 *sids = NULL;
2351 *nel = 0;
2352
2353 if (!ss_initialized)
2354 goto out;
2355
2356 read_lock(&policy_rwlock);
2357
2358 context_init(&usercon);
2359
2360 rc = -EINVAL;
2361 fromcon = sidtab_search(&sidtab, fromsid);
2362 if (!fromcon)
2363 goto out_unlock;
2364
2365 rc = -EINVAL;
2366 user = hashtab_search(policydb.p_users.table, username);
2367 if (!user)
2368 goto out_unlock;
2369
2370 usercon.user = user->value;
2371
2372 rc = -ENOMEM;
2373 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
2374 if (!mysids)
2375 goto out_unlock;
2376
2377 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
2378 role = policydb.role_val_to_struct[i];
2379 usercon.role = i + 1;
2380 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
2381 usercon.type = j + 1;
2382
2383 if (mls_setup_user_range(fromcon, user, &usercon))
2384 continue;
2385
2386 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
2387 if (rc)
2388 goto out_unlock;
2389 if (mynel < maxnel) {
2390 mysids[mynel++] = sid;
2391 } else {
2392 rc = -ENOMEM;
2393 maxnel += SIDS_NEL;
2394 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
2395 if (!mysids2)
2396 goto out_unlock;
2397 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2398 kfree(mysids);
2399 mysids = mysids2;
2400 mysids[mynel++] = sid;
2401 }
2402 }
2403 }
2404 rc = 0;
2405 out_unlock:
2406 read_unlock(&policy_rwlock);
2407 if (rc || !mynel) {
2408 kfree(mysids);
2409 goto out;
2410 }
2411
2412 rc = -ENOMEM;
2413 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2414 if (!mysids2) {
2415 kfree(mysids);
2416 goto out;
2417 }
2418 for (i = 0, j = 0; i < mynel; i++) {
2419 struct av_decision dummy_avd;
2420 rc = avc_has_perm_noaudit(fromsid, mysids[i],
2421 SECCLASS_PROCESS, /* kernel value */
2422 PROCESS__TRANSITION, AVC_STRICT,
2423 &dummy_avd);
2424 if (!rc)
2425 mysids2[j++] = mysids[i];
2426 cond_resched();
2427 }
2428 rc = 0;
2429 kfree(mysids);
2430 *sids = mysids2;
2431 *nel = j;
2432 out:
2433 return rc;
2434 }
2435
2436 /**
2437 * security_genfs_sid - Obtain a SID for a file in a filesystem
2438 * @fstype: filesystem type
2439 * @path: path from root of mount
2440 * @sclass: file security class
2441 * @sid: SID for path
2442 *
2443 * Obtain a SID to use for a file in a filesystem that
2444 * cannot support xattr or use a fixed labeling behavior like
2445 * transition SIDs or task SIDs.
2446 */
security_genfs_sid(const char * fstype,char * path,u16 orig_sclass,u32 * sid)2447 int security_genfs_sid(const char *fstype,
2448 char *path,
2449 u16 orig_sclass,
2450 u32 *sid)
2451 {
2452 int len;
2453 u16 sclass;
2454 struct genfs *genfs;
2455 struct ocontext *c;
2456 int rc, cmp = 0;
2457
2458 while (path[0] == '/' && path[1] == '/')
2459 path++;
2460
2461 read_lock(&policy_rwlock);
2462
2463 sclass = unmap_class(orig_sclass);
2464 *sid = SECINITSID_UNLABELED;
2465
2466 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2467 cmp = strcmp(fstype, genfs->fstype);
2468 if (cmp <= 0)
2469 break;
2470 }
2471
2472 rc = -ENOENT;
2473 if (!genfs || cmp)
2474 goto out;
2475
2476 for (c = genfs->head; c; c = c->next) {
2477 len = strlen(c->u.name);
2478 if ((!c->v.sclass || sclass == c->v.sclass) &&
2479 (strncmp(c->u.name, path, len) == 0))
2480 break;
2481 }
2482
2483 rc = -ENOENT;
2484 if (!c)
2485 goto out;
2486
2487 if (!c->sid[0]) {
2488 rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]);
2489 if (rc)
2490 goto out;
2491 }
2492
2493 *sid = c->sid[0];
2494 rc = 0;
2495 out:
2496 read_unlock(&policy_rwlock);
2497 return rc;
2498 }
2499
2500 /**
2501 * security_fs_use - Determine how to handle labeling for a filesystem.
2502 * @fstype: filesystem type
2503 * @behavior: labeling behavior
2504 * @sid: SID for filesystem (superblock)
2505 */
security_fs_use(const char * fstype,unsigned int * behavior,u32 * sid)2506 int security_fs_use(
2507 const char *fstype,
2508 unsigned int *behavior,
2509 u32 *sid)
2510 {
2511 int rc = 0;
2512 struct ocontext *c;
2513
2514 read_lock(&policy_rwlock);
2515
2516 c = policydb.ocontexts[OCON_FSUSE];
2517 while (c) {
2518 if (strcmp(fstype, c->u.name) == 0)
2519 break;
2520 c = c->next;
2521 }
2522
2523 if (c) {
2524 *behavior = c->v.behavior;
2525 if (!c->sid[0]) {
2526 rc = sidtab_context_to_sid(&sidtab, &c->context[0],
2527 &c->sid[0]);
2528 if (rc)
2529 goto out;
2530 }
2531 *sid = c->sid[0];
2532 } else {
2533 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2534 if (rc) {
2535 *behavior = SECURITY_FS_USE_NONE;
2536 rc = 0;
2537 } else {
2538 *behavior = SECURITY_FS_USE_GENFS;
2539 }
2540 }
2541
2542 out:
2543 read_unlock(&policy_rwlock);
2544 return rc;
2545 }
2546
security_get_bools(int * len,char *** names,int ** values)2547 int security_get_bools(int *len, char ***names, int **values)
2548 {
2549 int i, rc;
2550
2551 read_lock(&policy_rwlock);
2552 *names = NULL;
2553 *values = NULL;
2554
2555 rc = 0;
2556 *len = policydb.p_bools.nprim;
2557 if (!*len)
2558 goto out;
2559
2560 rc = -ENOMEM;
2561 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
2562 if (!*names)
2563 goto err;
2564
2565 rc = -ENOMEM;
2566 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
2567 if (!*values)
2568 goto err;
2569
2570 for (i = 0; i < *len; i++) {
2571 size_t name_len;
2572
2573 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2574 name_len = strlen(sym_name(&policydb, SYM_BOOLS, i)) + 1;
2575
2576 rc = -ENOMEM;
2577 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
2578 if (!(*names)[i])
2579 goto err;
2580
2581 strncpy((*names)[i], sym_name(&policydb, SYM_BOOLS, i), name_len);
2582 (*names)[i][name_len - 1] = 0;
2583 }
2584 rc = 0;
2585 out:
2586 read_unlock(&policy_rwlock);
2587 return rc;
2588 err:
2589 if (*names) {
2590 for (i = 0; i < *len; i++)
2591 kfree((*names)[i]);
2592 }
2593 kfree(*values);
2594 goto out;
2595 }
2596
2597
security_set_bools(int len,int * values)2598 int security_set_bools(int len, int *values)
2599 {
2600 int i, rc;
2601 int lenp, seqno = 0;
2602 struct cond_node *cur;
2603
2604 write_lock_irq(&policy_rwlock);
2605
2606 rc = -EFAULT;
2607 lenp = policydb.p_bools.nprim;
2608 if (len != lenp)
2609 goto out;
2610
2611 for (i = 0; i < len; i++) {
2612 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2613 audit_log(current->audit_context, GFP_ATOMIC,
2614 AUDIT_MAC_CONFIG_CHANGE,
2615 "bool=%s val=%d old_val=%d auid=%u ses=%u",
2616 sym_name(&policydb, SYM_BOOLS, i),
2617 !!values[i],
2618 policydb.bool_val_to_struct[i]->state,
2619 audit_get_loginuid(current),
2620 audit_get_sessionid(current));
2621 }
2622 if (values[i])
2623 policydb.bool_val_to_struct[i]->state = 1;
2624 else
2625 policydb.bool_val_to_struct[i]->state = 0;
2626 }
2627
2628 for (cur = policydb.cond_list; cur; cur = cur->next) {
2629 rc = evaluate_cond_node(&policydb, cur);
2630 if (rc)
2631 goto out;
2632 }
2633
2634 seqno = ++latest_granting;
2635 rc = 0;
2636 out:
2637 write_unlock_irq(&policy_rwlock);
2638 if (!rc) {
2639 avc_ss_reset(seqno);
2640 selnl_notify_policyload(seqno);
2641 selinux_status_update_policyload(seqno);
2642 selinux_xfrm_notify_policyload();
2643 }
2644 return rc;
2645 }
2646
security_get_bool_value(int bool)2647 int security_get_bool_value(int bool)
2648 {
2649 int rc;
2650 int len;
2651
2652 read_lock(&policy_rwlock);
2653
2654 rc = -EFAULT;
2655 len = policydb.p_bools.nprim;
2656 if (bool >= len)
2657 goto out;
2658
2659 rc = policydb.bool_val_to_struct[bool]->state;
2660 out:
2661 read_unlock(&policy_rwlock);
2662 return rc;
2663 }
2664
security_preserve_bools(struct policydb * p)2665 static int security_preserve_bools(struct policydb *p)
2666 {
2667 int rc, nbools = 0, *bvalues = NULL, i;
2668 char **bnames = NULL;
2669 struct cond_bool_datum *booldatum;
2670 struct cond_node *cur;
2671
2672 rc = security_get_bools(&nbools, &bnames, &bvalues);
2673 if (rc)
2674 goto out;
2675 for (i = 0; i < nbools; i++) {
2676 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2677 if (booldatum)
2678 booldatum->state = bvalues[i];
2679 }
2680 for (cur = p->cond_list; cur; cur = cur->next) {
2681 rc = evaluate_cond_node(p, cur);
2682 if (rc)
2683 goto out;
2684 }
2685
2686 out:
2687 if (bnames) {
2688 for (i = 0; i < nbools; i++)
2689 kfree(bnames[i]);
2690 }
2691 kfree(bnames);
2692 kfree(bvalues);
2693 return rc;
2694 }
2695
2696 /*
2697 * security_sid_mls_copy() - computes a new sid based on the given
2698 * sid and the mls portion of mls_sid.
2699 */
security_sid_mls_copy(u32 sid,u32 mls_sid,u32 * new_sid)2700 int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2701 {
2702 struct context *context1;
2703 struct context *context2;
2704 struct context newcon;
2705 char *s;
2706 u32 len;
2707 int rc;
2708
2709 rc = 0;
2710 if (!ss_initialized || !policydb.mls_enabled) {
2711 *new_sid = sid;
2712 goto out;
2713 }
2714
2715 context_init(&newcon);
2716
2717 read_lock(&policy_rwlock);
2718
2719 rc = -EINVAL;
2720 context1 = sidtab_search(&sidtab, sid);
2721 if (!context1) {
2722 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2723 __func__, sid);
2724 goto out_unlock;
2725 }
2726
2727 rc = -EINVAL;
2728 context2 = sidtab_search(&sidtab, mls_sid);
2729 if (!context2) {
2730 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2731 __func__, mls_sid);
2732 goto out_unlock;
2733 }
2734
2735 newcon.user = context1->user;
2736 newcon.role = context1->role;
2737 newcon.type = context1->type;
2738 rc = mls_context_cpy(&newcon, context2);
2739 if (rc)
2740 goto out_unlock;
2741
2742 /* Check the validity of the new context. */
2743 if (!policydb_context_isvalid(&policydb, &newcon)) {
2744 rc = convert_context_handle_invalid_context(&newcon);
2745 if (rc) {
2746 if (!context_struct_to_string(&newcon, &s, &len)) {
2747 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2748 "security_sid_mls_copy: invalid context %s", s);
2749 kfree(s);
2750 }
2751 goto out_unlock;
2752 }
2753 }
2754
2755 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2756 out_unlock:
2757 read_unlock(&policy_rwlock);
2758 context_destroy(&newcon);
2759 out:
2760 return rc;
2761 }
2762
2763 /**
2764 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2765 * @nlbl_sid: NetLabel SID
2766 * @nlbl_type: NetLabel labeling protocol type
2767 * @xfrm_sid: XFRM SID
2768 *
2769 * Description:
2770 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2771 * resolved into a single SID it is returned via @peer_sid and the function
2772 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2773 * returns a negative value. A table summarizing the behavior is below:
2774 *
2775 * | function return | @sid
2776 * ------------------------------+-----------------+-----------------
2777 * no peer labels | 0 | SECSID_NULL
2778 * single peer label | 0 | <peer_label>
2779 * multiple, consistent labels | 0 | <peer_label>
2780 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2781 *
2782 */
security_net_peersid_resolve(u32 nlbl_sid,u32 nlbl_type,u32 xfrm_sid,u32 * peer_sid)2783 int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2784 u32 xfrm_sid,
2785 u32 *peer_sid)
2786 {
2787 int rc;
2788 struct context *nlbl_ctx;
2789 struct context *xfrm_ctx;
2790
2791 *peer_sid = SECSID_NULL;
2792
2793 /* handle the common (which also happens to be the set of easy) cases
2794 * right away, these two if statements catch everything involving a
2795 * single or absent peer SID/label */
2796 if (xfrm_sid == SECSID_NULL) {
2797 *peer_sid = nlbl_sid;
2798 return 0;
2799 }
2800 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2801 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2802 * is present */
2803 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2804 *peer_sid = xfrm_sid;
2805 return 0;
2806 }
2807
2808 /* we don't need to check ss_initialized here since the only way both
2809 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2810 * security server was initialized and ss_initialized was true */
2811 if (!policydb.mls_enabled)
2812 return 0;
2813
2814 read_lock(&policy_rwlock);
2815
2816 rc = -EINVAL;
2817 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2818 if (!nlbl_ctx) {
2819 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2820 __func__, nlbl_sid);
2821 goto out;
2822 }
2823 rc = -EINVAL;
2824 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2825 if (!xfrm_ctx) {
2826 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2827 __func__, xfrm_sid);
2828 goto out;
2829 }
2830 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2831 if (rc)
2832 goto out;
2833
2834 /* at present NetLabel SIDs/labels really only carry MLS
2835 * information so if the MLS portion of the NetLabel SID
2836 * matches the MLS portion of the labeled XFRM SID/label
2837 * then pass along the XFRM SID as it is the most
2838 * expressive */
2839 *peer_sid = xfrm_sid;
2840 out:
2841 read_unlock(&policy_rwlock);
2842 return rc;
2843 }
2844
get_classes_callback(void * k,void * d,void * args)2845 static int get_classes_callback(void *k, void *d, void *args)
2846 {
2847 struct class_datum *datum = d;
2848 char *name = k, **classes = args;
2849 int value = datum->value - 1;
2850
2851 classes[value] = kstrdup(name, GFP_ATOMIC);
2852 if (!classes[value])
2853 return -ENOMEM;
2854
2855 return 0;
2856 }
2857
security_get_classes(char *** classes,int * nclasses)2858 int security_get_classes(char ***classes, int *nclasses)
2859 {
2860 int rc;
2861
2862 read_lock(&policy_rwlock);
2863
2864 rc = -ENOMEM;
2865 *nclasses = policydb.p_classes.nprim;
2866 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
2867 if (!*classes)
2868 goto out;
2869
2870 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2871 *classes);
2872 if (rc) {
2873 int i;
2874 for (i = 0; i < *nclasses; i++)
2875 kfree((*classes)[i]);
2876 kfree(*classes);
2877 }
2878
2879 out:
2880 read_unlock(&policy_rwlock);
2881 return rc;
2882 }
2883
get_permissions_callback(void * k,void * d,void * args)2884 static int get_permissions_callback(void *k, void *d, void *args)
2885 {
2886 struct perm_datum *datum = d;
2887 char *name = k, **perms = args;
2888 int value = datum->value - 1;
2889
2890 perms[value] = kstrdup(name, GFP_ATOMIC);
2891 if (!perms[value])
2892 return -ENOMEM;
2893
2894 return 0;
2895 }
2896
security_get_permissions(char * class,char *** perms,int * nperms)2897 int security_get_permissions(char *class, char ***perms, int *nperms)
2898 {
2899 int rc, i;
2900 struct class_datum *match;
2901
2902 read_lock(&policy_rwlock);
2903
2904 rc = -EINVAL;
2905 match = hashtab_search(policydb.p_classes.table, class);
2906 if (!match) {
2907 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
2908 __func__, class);
2909 goto out;
2910 }
2911
2912 rc = -ENOMEM;
2913 *nperms = match->permissions.nprim;
2914 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
2915 if (!*perms)
2916 goto out;
2917
2918 if (match->comdatum) {
2919 rc = hashtab_map(match->comdatum->permissions.table,
2920 get_permissions_callback, *perms);
2921 if (rc)
2922 goto err;
2923 }
2924
2925 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2926 *perms);
2927 if (rc)
2928 goto err;
2929
2930 out:
2931 read_unlock(&policy_rwlock);
2932 return rc;
2933
2934 err:
2935 read_unlock(&policy_rwlock);
2936 for (i = 0; i < *nperms; i++)
2937 kfree((*perms)[i]);
2938 kfree(*perms);
2939 return rc;
2940 }
2941
security_get_reject_unknown(void)2942 int security_get_reject_unknown(void)
2943 {
2944 return policydb.reject_unknown;
2945 }
2946
security_get_allow_unknown(void)2947 int security_get_allow_unknown(void)
2948 {
2949 return policydb.allow_unknown;
2950 }
2951
2952 /**
2953 * security_policycap_supported - Check for a specific policy capability
2954 * @req_cap: capability
2955 *
2956 * Description:
2957 * This function queries the currently loaded policy to see if it supports the
2958 * capability specified by @req_cap. Returns true (1) if the capability is
2959 * supported, false (0) if it isn't supported.
2960 *
2961 */
security_policycap_supported(unsigned int req_cap)2962 int security_policycap_supported(unsigned int req_cap)
2963 {
2964 int rc;
2965
2966 read_lock(&policy_rwlock);
2967 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2968 read_unlock(&policy_rwlock);
2969
2970 return rc;
2971 }
2972
2973 struct selinux_audit_rule {
2974 u32 au_seqno;
2975 struct context au_ctxt;
2976 };
2977
selinux_audit_rule_free(void * vrule)2978 void selinux_audit_rule_free(void *vrule)
2979 {
2980 struct selinux_audit_rule *rule = vrule;
2981
2982 if (rule) {
2983 context_destroy(&rule->au_ctxt);
2984 kfree(rule);
2985 }
2986 }
2987
selinux_audit_rule_init(u32 field,u32 op,char * rulestr,void ** vrule)2988 int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
2989 {
2990 struct selinux_audit_rule *tmprule;
2991 struct role_datum *roledatum;
2992 struct type_datum *typedatum;
2993 struct user_datum *userdatum;
2994 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
2995 int rc = 0;
2996
2997 *rule = NULL;
2998
2999 if (!ss_initialized)
3000 return -EOPNOTSUPP;
3001
3002 switch (field) {
3003 case AUDIT_SUBJ_USER:
3004 case AUDIT_SUBJ_ROLE:
3005 case AUDIT_SUBJ_TYPE:
3006 case AUDIT_OBJ_USER:
3007 case AUDIT_OBJ_ROLE:
3008 case AUDIT_OBJ_TYPE:
3009 /* only 'equals' and 'not equals' fit user, role, and type */
3010 if (op != Audit_equal && op != Audit_not_equal)
3011 return -EINVAL;
3012 break;
3013 case AUDIT_SUBJ_SEN:
3014 case AUDIT_SUBJ_CLR:
3015 case AUDIT_OBJ_LEV_LOW:
3016 case AUDIT_OBJ_LEV_HIGH:
3017 /* we do not allow a range, indicated by the presence of '-' */
3018 if (strchr(rulestr, '-'))
3019 return -EINVAL;
3020 break;
3021 default:
3022 /* only the above fields are valid */
3023 return -EINVAL;
3024 }
3025
3026 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3027 if (!tmprule)
3028 return -ENOMEM;
3029
3030 context_init(&tmprule->au_ctxt);
3031
3032 read_lock(&policy_rwlock);
3033
3034 tmprule->au_seqno = latest_granting;
3035
3036 switch (field) {
3037 case AUDIT_SUBJ_USER:
3038 case AUDIT_OBJ_USER:
3039 rc = -EINVAL;
3040 userdatum = hashtab_search(policydb.p_users.table, rulestr);
3041 if (!userdatum)
3042 goto out;
3043 tmprule->au_ctxt.user = userdatum->value;
3044 break;
3045 case AUDIT_SUBJ_ROLE:
3046 case AUDIT_OBJ_ROLE:
3047 rc = -EINVAL;
3048 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
3049 if (!roledatum)
3050 goto out;
3051 tmprule->au_ctxt.role = roledatum->value;
3052 break;
3053 case AUDIT_SUBJ_TYPE:
3054 case AUDIT_OBJ_TYPE:
3055 rc = -EINVAL;
3056 typedatum = hashtab_search(policydb.p_types.table, rulestr);
3057 if (!typedatum)
3058 goto out;
3059 tmprule->au_ctxt.type = typedatum->value;
3060 break;
3061 case AUDIT_SUBJ_SEN:
3062 case AUDIT_SUBJ_CLR:
3063 case AUDIT_OBJ_LEV_LOW:
3064 case AUDIT_OBJ_LEV_HIGH:
3065 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
3066 if (rc)
3067 goto out;
3068 break;
3069 }
3070 rc = 0;
3071 out:
3072 read_unlock(&policy_rwlock);
3073
3074 if (rc) {
3075 selinux_audit_rule_free(tmprule);
3076 tmprule = NULL;
3077 }
3078
3079 *rule = tmprule;
3080
3081 return rc;
3082 }
3083
3084 /* Check to see if the rule contains any selinux fields */
selinux_audit_rule_known(struct audit_krule * rule)3085 int selinux_audit_rule_known(struct audit_krule *rule)
3086 {
3087 int i;
3088
3089 for (i = 0; i < rule->field_count; i++) {
3090 struct audit_field *f = &rule->fields[i];
3091 switch (f->type) {
3092 case AUDIT_SUBJ_USER:
3093 case AUDIT_SUBJ_ROLE:
3094 case AUDIT_SUBJ_TYPE:
3095 case AUDIT_SUBJ_SEN:
3096 case AUDIT_SUBJ_CLR:
3097 case AUDIT_OBJ_USER:
3098 case AUDIT_OBJ_ROLE:
3099 case AUDIT_OBJ_TYPE:
3100 case AUDIT_OBJ_LEV_LOW:
3101 case AUDIT_OBJ_LEV_HIGH:
3102 return 1;
3103 }
3104 }
3105
3106 return 0;
3107 }
3108
selinux_audit_rule_match(u32 sid,u32 field,u32 op,void * vrule,struct audit_context * actx)3109 int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
3110 struct audit_context *actx)
3111 {
3112 struct context *ctxt;
3113 struct mls_level *level;
3114 struct selinux_audit_rule *rule = vrule;
3115 int match = 0;
3116
3117 if (!rule) {
3118 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
3119 "selinux_audit_rule_match: missing rule\n");
3120 return -ENOENT;
3121 }
3122
3123 read_lock(&policy_rwlock);
3124
3125 if (rule->au_seqno < latest_granting) {
3126 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
3127 "selinux_audit_rule_match: stale rule\n");
3128 match = -ESTALE;
3129 goto out;
3130 }
3131
3132 ctxt = sidtab_search(&sidtab, sid);
3133 if (!ctxt) {
3134 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
3135 "selinux_audit_rule_match: unrecognized SID %d\n",
3136 sid);
3137 match = -ENOENT;
3138 goto out;
3139 }
3140
3141 /* a field/op pair that is not caught here will simply fall through
3142 without a match */
3143 switch (field) {
3144 case AUDIT_SUBJ_USER:
3145 case AUDIT_OBJ_USER:
3146 switch (op) {
3147 case Audit_equal:
3148 match = (ctxt->user == rule->au_ctxt.user);
3149 break;
3150 case Audit_not_equal:
3151 match = (ctxt->user != rule->au_ctxt.user);
3152 break;
3153 }
3154 break;
3155 case AUDIT_SUBJ_ROLE:
3156 case AUDIT_OBJ_ROLE:
3157 switch (op) {
3158 case Audit_equal:
3159 match = (ctxt->role == rule->au_ctxt.role);
3160 break;
3161 case Audit_not_equal:
3162 match = (ctxt->role != rule->au_ctxt.role);
3163 break;
3164 }
3165 break;
3166 case AUDIT_SUBJ_TYPE:
3167 case AUDIT_OBJ_TYPE:
3168 switch (op) {
3169 case Audit_equal:
3170 match = (ctxt->type == rule->au_ctxt.type);
3171 break;
3172 case Audit_not_equal:
3173 match = (ctxt->type != rule->au_ctxt.type);
3174 break;
3175 }
3176 break;
3177 case AUDIT_SUBJ_SEN:
3178 case AUDIT_SUBJ_CLR:
3179 case AUDIT_OBJ_LEV_LOW:
3180 case AUDIT_OBJ_LEV_HIGH:
3181 level = ((field == AUDIT_SUBJ_SEN ||
3182 field == AUDIT_OBJ_LEV_LOW) ?
3183 &ctxt->range.level[0] : &ctxt->range.level[1]);
3184 switch (op) {
3185 case Audit_equal:
3186 match = mls_level_eq(&rule->au_ctxt.range.level[0],
3187 level);
3188 break;
3189 case Audit_not_equal:
3190 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
3191 level);
3192 break;
3193 case Audit_lt:
3194 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
3195 level) &&
3196 !mls_level_eq(&rule->au_ctxt.range.level[0],
3197 level));
3198 break;
3199 case Audit_le:
3200 match = mls_level_dom(&rule->au_ctxt.range.level[0],
3201 level);
3202 break;
3203 case Audit_gt:
3204 match = (mls_level_dom(level,
3205 &rule->au_ctxt.range.level[0]) &&
3206 !mls_level_eq(level,
3207 &rule->au_ctxt.range.level[0]));
3208 break;
3209 case Audit_ge:
3210 match = mls_level_dom(level,
3211 &rule->au_ctxt.range.level[0]);
3212 break;
3213 }
3214 }
3215
3216 out:
3217 read_unlock(&policy_rwlock);
3218 return match;
3219 }
3220
3221 static int (*aurule_callback)(void) = audit_update_lsm_rules;
3222
aurule_avc_callback(u32 event,u32 ssid,u32 tsid,u16 class,u32 perms,u32 * retained)3223 static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
3224 u16 class, u32 perms, u32 *retained)
3225 {
3226 int err = 0;
3227
3228 if (event == AVC_CALLBACK_RESET && aurule_callback)
3229 err = aurule_callback();
3230 return err;
3231 }
3232
aurule_init(void)3233 static int __init aurule_init(void)
3234 {
3235 int err;
3236
3237 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
3238 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
3239 if (err)
3240 panic("avc_add_callback() failed, error %d\n", err);
3241
3242 return err;
3243 }
3244 __initcall(aurule_init);
3245
3246 #ifdef CONFIG_NETLABEL
3247 /**
3248 * security_netlbl_cache_add - Add an entry to the NetLabel cache
3249 * @secattr: the NetLabel packet security attributes
3250 * @sid: the SELinux SID
3251 *
3252 * Description:
3253 * Attempt to cache the context in @ctx, which was derived from the packet in
3254 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
3255 * already been initialized.
3256 *
3257 */
security_netlbl_cache_add(struct netlbl_lsm_secattr * secattr,u32 sid)3258 static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
3259 u32 sid)
3260 {
3261 u32 *sid_cache;
3262
3263 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
3264 if (sid_cache == NULL)
3265 return;
3266 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
3267 if (secattr->cache == NULL) {
3268 kfree(sid_cache);
3269 return;
3270 }
3271
3272 *sid_cache = sid;
3273 secattr->cache->free = kfree;
3274 secattr->cache->data = sid_cache;
3275 secattr->flags |= NETLBL_SECATTR_CACHE;
3276 }
3277
3278 /**
3279 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
3280 * @secattr: the NetLabel packet security attributes
3281 * @sid: the SELinux SID
3282 *
3283 * Description:
3284 * Convert the given NetLabel security attributes in @secattr into a
3285 * SELinux SID. If the @secattr field does not contain a full SELinux
3286 * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
3287 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
3288 * allow the @secattr to be used by NetLabel to cache the secattr to SID
3289 * conversion for future lookups. Returns zero on success, negative values on
3290 * failure.
3291 *
3292 */
security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr * secattr,u32 * sid)3293 int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
3294 u32 *sid)
3295 {
3296 int rc;
3297 struct context *ctx;
3298 struct context ctx_new;
3299
3300 if (!ss_initialized) {
3301 *sid = SECSID_NULL;
3302 return 0;
3303 }
3304
3305 read_lock(&policy_rwlock);
3306
3307 if (secattr->flags & NETLBL_SECATTR_CACHE)
3308 *sid = *(u32 *)secattr->cache->data;
3309 else if (secattr->flags & NETLBL_SECATTR_SECID)
3310 *sid = secattr->attr.secid;
3311 else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
3312 rc = -EIDRM;
3313 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
3314 if (ctx == NULL)
3315 goto out;
3316
3317 context_init(&ctx_new);
3318 ctx_new.user = ctx->user;
3319 ctx_new.role = ctx->role;
3320 ctx_new.type = ctx->type;
3321 mls_import_netlbl_lvl(&ctx_new, secattr);
3322 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
3323 rc = ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
3324 secattr->attr.mls.cat);
3325 if (rc)
3326 goto out;
3327 memcpy(&ctx_new.range.level[1].cat,
3328 &ctx_new.range.level[0].cat,
3329 sizeof(ctx_new.range.level[0].cat));
3330 }
3331 rc = -EIDRM;
3332 if (!mls_context_isvalid(&policydb, &ctx_new))
3333 goto out_free;
3334
3335 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
3336 if (rc)
3337 goto out_free;
3338
3339 security_netlbl_cache_add(secattr, *sid);
3340
3341 ebitmap_destroy(&ctx_new.range.level[0].cat);
3342 } else
3343 *sid = SECSID_NULL;
3344
3345 read_unlock(&policy_rwlock);
3346 return 0;
3347 out_free:
3348 ebitmap_destroy(&ctx_new.range.level[0].cat);
3349 out:
3350 read_unlock(&policy_rwlock);
3351 return rc;
3352 }
3353
3354 /**
3355 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3356 * @sid: the SELinux SID
3357 * @secattr: the NetLabel packet security attributes
3358 *
3359 * Description:
3360 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3361 * Returns zero on success, negative values on failure.
3362 *
3363 */
security_netlbl_sid_to_secattr(u32 sid,struct netlbl_lsm_secattr * secattr)3364 int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
3365 {
3366 int rc;
3367 struct context *ctx;
3368
3369 if (!ss_initialized)
3370 return 0;
3371
3372 read_lock(&policy_rwlock);
3373
3374 rc = -ENOENT;
3375 ctx = sidtab_search(&sidtab, sid);
3376 if (ctx == NULL)
3377 goto out;
3378
3379 rc = -ENOMEM;
3380 secattr->domain = kstrdup(sym_name(&policydb, SYM_TYPES, ctx->type - 1),
3381 GFP_ATOMIC);
3382 if (secattr->domain == NULL)
3383 goto out;
3384
3385 secattr->attr.secid = sid;
3386 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
3387 mls_export_netlbl_lvl(ctx, secattr);
3388 rc = mls_export_netlbl_cat(ctx, secattr);
3389 out:
3390 read_unlock(&policy_rwlock);
3391 return rc;
3392 }
3393 #endif /* CONFIG_NETLABEL */
3394
3395 /**
3396 * security_read_policy - read the policy.
3397 * @data: binary policy data
3398 * @len: length of data in bytes
3399 *
3400 */
security_read_policy(void ** data,size_t * len)3401 int security_read_policy(void **data, size_t *len)
3402 {
3403 int rc;
3404 struct policy_file fp;
3405
3406 if (!ss_initialized)
3407 return -EINVAL;
3408
3409 *len = security_policydb_len();
3410
3411 *data = vmalloc_user(*len);
3412 if (!*data)
3413 return -ENOMEM;
3414
3415 fp.data = *data;
3416 fp.len = *len;
3417
3418 read_lock(&policy_rwlock);
3419 rc = policydb_write(&policydb, &fp);
3420 read_unlock(&policy_rwlock);
3421
3422 if (rc)
3423 return rc;
3424
3425 *len = (unsigned long)fp.data - (unsigned long)*data;
3426 return 0;
3427
3428 }
3429