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