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