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