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