selinux: change numbering of boolean directory inodes in selinuxfs
[deliverable/linux.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>
5 * James Morris <jmorris@redhat.com>
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 *
14 * Added conditional policy language extensions
15 *
7420ed23
VY
16 * Updated: Hewlett-Packard <paul.moore@hp.com>
17 *
18 * Added support for NetLabel
19 *
b94c7e67
CS
20 * Updated: Chad Sellers <csellers@tresys.com>
21 *
22 * Added validation of kernel classes and permissions
23 *
7420ed23 24 * Copyright (C) 2006 Hewlett-Packard Development Company, L.P.
376bd9cb 25 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
b94c7e67 26 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
1da177e4
LT
27 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, version 2.
31 */
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/string.h>
35#include <linux/spinlock.h>
9f2ad665 36#include <linux/rcupdate.h>
1da177e4
LT
37#include <linux/errno.h>
38#include <linux/in.h>
39#include <linux/sched.h>
40#include <linux/audit.h>
bb003079 41#include <linux/mutex.h>
7420ed23 42#include <net/netlabel.h>
bb003079 43
1da177e4
LT
44#include "flask.h"
45#include "avc.h"
46#include "avc_ss.h"
47#include "security.h"
48#include "context.h"
49#include "policydb.h"
50#include "sidtab.h"
51#include "services.h"
52#include "conditional.h"
53#include "mls.h"
7420ed23 54#include "objsec.h"
c60475bf 55#include "netlabel.h"
3de4bab5 56#include "xfrm.h"
02752760 57#include "ebitmap.h"
1da177e4
LT
58
59extern void selnl_notify_policyload(u32 seqno);
60unsigned int policydb_loaded_version;
61
b94c7e67
CS
62/*
63 * This is declared in avc.c
64 */
65extern const struct selinux_class_perm selinux_class_perm;
66
1da177e4
LT
67static DEFINE_RWLOCK(policy_rwlock);
68#define POLICY_RDLOCK read_lock(&policy_rwlock)
69#define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
70#define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
71#define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
72
bb003079
IM
73static DEFINE_MUTEX(load_mutex);
74#define LOAD_LOCK mutex_lock(&load_mutex)
75#define LOAD_UNLOCK mutex_unlock(&load_mutex)
1da177e4
LT
76
77static struct sidtab sidtab;
78struct policydb policydb;
79int ss_initialized = 0;
80
81/*
82 * The largest sequence number that has been used when
83 * providing an access decision to the access vector cache.
84 * The sequence number only changes when a policy change
85 * occurs.
86 */
87static u32 latest_granting = 0;
88
89/* Forward declaration. */
90static int context_struct_to_string(struct context *context, char **scontext,
91 u32 *scontext_len);
92
93/*
94 * Return the boolean value of a constraint expression
95 * when it is applied to the specified source and target
96 * security contexts.
97 *
98 * xcontext is a special beast... It is used by the validatetrans rules
99 * only. For these rules, scontext is the context before the transition,
100 * tcontext is the context after the transition, and xcontext is the context
101 * of the process performing the transition. All other callers of
102 * constraint_expr_eval should pass in NULL for xcontext.
103 */
104static int constraint_expr_eval(struct context *scontext,
105 struct context *tcontext,
106 struct context *xcontext,
107 struct constraint_expr *cexpr)
108{
109 u32 val1, val2;
110 struct context *c;
111 struct role_datum *r1, *r2;
112 struct mls_level *l1, *l2;
113 struct constraint_expr *e;
114 int s[CEXPR_MAXDEPTH];
115 int sp = -1;
116
117 for (e = cexpr; e; e = e->next) {
118 switch (e->expr_type) {
119 case CEXPR_NOT:
120 BUG_ON(sp < 0);
121 s[sp] = !s[sp];
122 break;
123 case CEXPR_AND:
124 BUG_ON(sp < 1);
125 sp--;
126 s[sp] &= s[sp+1];
127 break;
128 case CEXPR_OR:
129 BUG_ON(sp < 1);
130 sp--;
131 s[sp] |= s[sp+1];
132 break;
133 case CEXPR_ATTR:
134 if (sp == (CEXPR_MAXDEPTH-1))
135 return 0;
136 switch (e->attr) {
137 case CEXPR_USER:
138 val1 = scontext->user;
139 val2 = tcontext->user;
140 break;
141 case CEXPR_TYPE:
142 val1 = scontext->type;
143 val2 = tcontext->type;
144 break;
145 case CEXPR_ROLE:
146 val1 = scontext->role;
147 val2 = tcontext->role;
148 r1 = policydb.role_val_to_struct[val1 - 1];
149 r2 = policydb.role_val_to_struct[val2 - 1];
150 switch (e->op) {
151 case CEXPR_DOM:
152 s[++sp] = ebitmap_get_bit(&r1->dominates,
153 val2 - 1);
154 continue;
155 case CEXPR_DOMBY:
156 s[++sp] = ebitmap_get_bit(&r2->dominates,
157 val1 - 1);
158 continue;
159 case CEXPR_INCOMP:
160 s[++sp] = ( !ebitmap_get_bit(&r1->dominates,
161 val2 - 1) &&
162 !ebitmap_get_bit(&r2->dominates,
163 val1 - 1) );
164 continue;
165 default:
166 break;
167 }
168 break;
169 case CEXPR_L1L2:
170 l1 = &(scontext->range.level[0]);
171 l2 = &(tcontext->range.level[0]);
172 goto mls_ops;
173 case CEXPR_L1H2:
174 l1 = &(scontext->range.level[0]);
175 l2 = &(tcontext->range.level[1]);
176 goto mls_ops;
177 case CEXPR_H1L2:
178 l1 = &(scontext->range.level[1]);
179 l2 = &(tcontext->range.level[0]);
180 goto mls_ops;
181 case CEXPR_H1H2:
182 l1 = &(scontext->range.level[1]);
183 l2 = &(tcontext->range.level[1]);
184 goto mls_ops;
185 case CEXPR_L1H1:
186 l1 = &(scontext->range.level[0]);
187 l2 = &(scontext->range.level[1]);
188 goto mls_ops;
189 case CEXPR_L2H2:
190 l1 = &(tcontext->range.level[0]);
191 l2 = &(tcontext->range.level[1]);
192 goto mls_ops;
193mls_ops:
194 switch (e->op) {
195 case CEXPR_EQ:
196 s[++sp] = mls_level_eq(l1, l2);
197 continue;
198 case CEXPR_NEQ:
199 s[++sp] = !mls_level_eq(l1, l2);
200 continue;
201 case CEXPR_DOM:
202 s[++sp] = mls_level_dom(l1, l2);
203 continue;
204 case CEXPR_DOMBY:
205 s[++sp] = mls_level_dom(l2, l1);
206 continue;
207 case CEXPR_INCOMP:
208 s[++sp] = mls_level_incomp(l2, l1);
209 continue;
210 default:
211 BUG();
212 return 0;
213 }
214 break;
215 default:
216 BUG();
217 return 0;
218 }
219
220 switch (e->op) {
221 case CEXPR_EQ:
222 s[++sp] = (val1 == val2);
223 break;
224 case CEXPR_NEQ:
225 s[++sp] = (val1 != val2);
226 break;
227 default:
228 BUG();
229 return 0;
230 }
231 break;
232 case CEXPR_NAMES:
233 if (sp == (CEXPR_MAXDEPTH-1))
234 return 0;
235 c = scontext;
236 if (e->attr & CEXPR_TARGET)
237 c = tcontext;
238 else if (e->attr & CEXPR_XTARGET) {
239 c = xcontext;
240 if (!c) {
241 BUG();
242 return 0;
243 }
244 }
245 if (e->attr & CEXPR_USER)
246 val1 = c->user;
247 else if (e->attr & CEXPR_ROLE)
248 val1 = c->role;
249 else if (e->attr & CEXPR_TYPE)
250 val1 = c->type;
251 else {
252 BUG();
253 return 0;
254 }
255
256 switch (e->op) {
257 case CEXPR_EQ:
258 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
259 break;
260 case CEXPR_NEQ:
261 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
262 break;
263 default:
264 BUG();
265 return 0;
266 }
267 break;
268 default:
269 BUG();
270 return 0;
271 }
272 }
273
274 BUG_ON(sp != 0);
275 return s[0];
276}
277
278/*
279 * Compute access vectors based on a context structure pair for
280 * the permissions in a particular class.
281 */
282static int context_struct_compute_av(struct context *scontext,
283 struct context *tcontext,
284 u16 tclass,
285 u32 requested,
286 struct av_decision *avd)
287{
288 struct constraint_node *constraint;
289 struct role_allow *ra;
290 struct avtab_key avkey;
782ebb99 291 struct avtab_node *node;
1da177e4 292 struct class_datum *tclass_datum;
782ebb99
SS
293 struct ebitmap *sattr, *tattr;
294 struct ebitmap_node *snode, *tnode;
295 unsigned int i, j;
1da177e4
LT
296
297 /*
298 * Remap extended Netlink classes for old policy versions.
299 * Do this here rather than socket_type_to_security_class()
300 * in case a newer policy version is loaded, allowing sockets
301 * to remain in the correct class.
302 */
303 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
304 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
305 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
306 tclass = SECCLASS_NETLINK_SOCKET;
307
308 if (!tclass || tclass > policydb.p_classes.nprim) {
309 printk(KERN_ERR "security_compute_av: unrecognized class %d\n",
310 tclass);
311 return -EINVAL;
312 }
313 tclass_datum = policydb.class_val_to_struct[tclass - 1];
314
315 /*
316 * Initialize the access vectors to the default values.
317 */
318 avd->allowed = 0;
319 avd->decided = 0xffffffff;
320 avd->auditallow = 0;
321 avd->auditdeny = 0xffffffff;
322 avd->seqno = latest_granting;
323
324 /*
325 * If a specific type enforcement rule was defined for
326 * this permission check, then use it.
327 */
1da177e4 328 avkey.target_class = tclass;
782ebb99
SS
329 avkey.specified = AVTAB_AV;
330 sattr = &policydb.type_attr_map[scontext->type - 1];
331 tattr = &policydb.type_attr_map[tcontext->type - 1];
332 ebitmap_for_each_bit(sattr, snode, i) {
333 if (!ebitmap_node_get_bit(snode, i))
334 continue;
335 ebitmap_for_each_bit(tattr, tnode, j) {
336 if (!ebitmap_node_get_bit(tnode, j))
337 continue;
338 avkey.source_type = i + 1;
339 avkey.target_type = j + 1;
340 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
341 node != NULL;
342 node = avtab_search_node_next(node, avkey.specified)) {
343 if (node->key.specified == AVTAB_ALLOWED)
344 avd->allowed |= node->datum.data;
345 else if (node->key.specified == AVTAB_AUDITALLOW)
346 avd->auditallow |= node->datum.data;
347 else if (node->key.specified == AVTAB_AUDITDENY)
348 avd->auditdeny &= node->datum.data;
349 }
1da177e4 350
782ebb99
SS
351 /* Check conditional av table for additional permissions */
352 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
353
354 }
355 }
1da177e4
LT
356
357 /*
358 * Remove any permissions prohibited by a constraint (this includes
359 * the MLS policy).
360 */
361 constraint = tclass_datum->constraints;
362 while (constraint) {
363 if ((constraint->permissions & (avd->allowed)) &&
364 !constraint_expr_eval(scontext, tcontext, NULL,
365 constraint->expr)) {
366 avd->allowed = (avd->allowed) & ~(constraint->permissions);
367 }
368 constraint = constraint->next;
369 }
370
371 /*
372 * If checking process transition permission and the
373 * role is changing, then check the (current_role, new_role)
374 * pair.
375 */
376 if (tclass == SECCLASS_PROCESS &&
377 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
378 scontext->role != tcontext->role) {
379 for (ra = policydb.role_allow; ra; ra = ra->next) {
380 if (scontext->role == ra->role &&
381 tcontext->role == ra->new_role)
382 break;
383 }
384 if (!ra)
385 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
386 PROCESS__DYNTRANSITION);
387 }
388
389 return 0;
390}
391
392static int security_validtrans_handle_fail(struct context *ocontext,
393 struct context *ncontext,
394 struct context *tcontext,
395 u16 tclass)
396{
397 char *o = NULL, *n = NULL, *t = NULL;
398 u32 olen, nlen, tlen;
399
400 if (context_struct_to_string(ocontext, &o, &olen) < 0)
401 goto out;
402 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
403 goto out;
404 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
405 goto out;
9ad9ad38 406 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1da177e4
LT
407 "security_validate_transition: denied for"
408 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
409 o, n, t, policydb.p_class_val_to_name[tclass-1]);
410out:
411 kfree(o);
412 kfree(n);
413 kfree(t);
414
415 if (!selinux_enforcing)
416 return 0;
417 return -EPERM;
418}
419
420int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
421 u16 tclass)
422{
423 struct context *ocontext;
424 struct context *ncontext;
425 struct context *tcontext;
426 struct class_datum *tclass_datum;
427 struct constraint_node *constraint;
428 int rc = 0;
429
430 if (!ss_initialized)
431 return 0;
432
433 POLICY_RDLOCK;
434
435 /*
436 * Remap extended Netlink classes for old policy versions.
437 * Do this here rather than socket_type_to_security_class()
438 * in case a newer policy version is loaded, allowing sockets
439 * to remain in the correct class.
440 */
441 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
442 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
443 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
444 tclass = SECCLASS_NETLINK_SOCKET;
445
446 if (!tclass || tclass > policydb.p_classes.nprim) {
447 printk(KERN_ERR "security_validate_transition: "
448 "unrecognized class %d\n", tclass);
449 rc = -EINVAL;
450 goto out;
451 }
452 tclass_datum = policydb.class_val_to_struct[tclass - 1];
453
454 ocontext = sidtab_search(&sidtab, oldsid);
455 if (!ocontext) {
456 printk(KERN_ERR "security_validate_transition: "
457 " unrecognized SID %d\n", oldsid);
458 rc = -EINVAL;
459 goto out;
460 }
461
462 ncontext = sidtab_search(&sidtab, newsid);
463 if (!ncontext) {
464 printk(KERN_ERR "security_validate_transition: "
465 " unrecognized SID %d\n", newsid);
466 rc = -EINVAL;
467 goto out;
468 }
469
470 tcontext = sidtab_search(&sidtab, tasksid);
471 if (!tcontext) {
472 printk(KERN_ERR "security_validate_transition: "
473 " unrecognized SID %d\n", tasksid);
474 rc = -EINVAL;
475 goto out;
476 }
477
478 constraint = tclass_datum->validatetrans;
479 while (constraint) {
480 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
481 constraint->expr)) {
482 rc = security_validtrans_handle_fail(ocontext, ncontext,
483 tcontext, tclass);
484 goto out;
485 }
486 constraint = constraint->next;
487 }
488
489out:
490 POLICY_RDUNLOCK;
491 return rc;
492}
493
494/**
495 * security_compute_av - Compute access vector decisions.
496 * @ssid: source security identifier
497 * @tsid: target security identifier
498 * @tclass: target security class
499 * @requested: requested permissions
500 * @avd: access vector decisions
501 *
502 * Compute a set of access vector decisions based on the
503 * SID pair (@ssid, @tsid) for the permissions in @tclass.
504 * Return -%EINVAL if any of the parameters are invalid or %0
505 * if the access vector decisions were computed successfully.
506 */
507int security_compute_av(u32 ssid,
508 u32 tsid,
509 u16 tclass,
510 u32 requested,
511 struct av_decision *avd)
512{
513 struct context *scontext = NULL, *tcontext = NULL;
514 int rc = 0;
515
516 if (!ss_initialized) {
4c443d1b
SS
517 avd->allowed = 0xffffffff;
518 avd->decided = 0xffffffff;
1da177e4
LT
519 avd->auditallow = 0;
520 avd->auditdeny = 0xffffffff;
521 avd->seqno = latest_granting;
522 return 0;
523 }
524
525 POLICY_RDLOCK;
526
527 scontext = sidtab_search(&sidtab, ssid);
528 if (!scontext) {
529 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
530 ssid);
531 rc = -EINVAL;
532 goto out;
533 }
534 tcontext = sidtab_search(&sidtab, tsid);
535 if (!tcontext) {
536 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
537 tsid);
538 rc = -EINVAL;
539 goto out;
540 }
541
542 rc = context_struct_compute_av(scontext, tcontext, tclass,
543 requested, avd);
544out:
545 POLICY_RDUNLOCK;
546 return rc;
547}
548
549/*
550 * Write the security context string representation of
551 * the context structure `context' into a dynamically
552 * allocated string of the correct size. Set `*scontext'
553 * to point to this string and set `*scontext_len' to
554 * the length of the string.
555 */
556static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
557{
558 char *scontextp;
559
560 *scontext = NULL;
561 *scontext_len = 0;
562
563 /* Compute the size of the context. */
564 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
565 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
566 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
567 *scontext_len += mls_compute_context_len(context);
568
569 /* Allocate space for the context; caller must free this space. */
570 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
571 if (!scontextp) {
572 return -ENOMEM;
573 }
574 *scontext = scontextp;
575
576 /*
577 * Copy the user name, role name and type name into the context.
578 */
579 sprintf(scontextp, "%s:%s:%s",
580 policydb.p_user_val_to_name[context->user - 1],
581 policydb.p_role_val_to_name[context->role - 1],
582 policydb.p_type_val_to_name[context->type - 1]);
583 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
584 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
585 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
586
587 mls_sid_to_context(context, &scontextp);
588
589 *scontextp = 0;
590
591 return 0;
592}
593
594#include "initial_sid_to_string.h"
595
f0ee2e46
JC
596const char *security_get_initial_sid_context(u32 sid)
597{
598 if (unlikely(sid > SECINITSID_NUM))
599 return NULL;
600 return initial_sid_to_string[sid];
601}
602
1da177e4
LT
603/**
604 * security_sid_to_context - Obtain a context for a given SID.
605 * @sid: security identifier, SID
606 * @scontext: security context
607 * @scontext_len: length in bytes
608 *
609 * Write the string representation of the context associated with @sid
610 * into a dynamically allocated string of the correct size. Set @scontext
611 * to point to this string and set @scontext_len to the length of the string.
612 */
613int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
614{
615 struct context *context;
616 int rc = 0;
617
4f4acf3a
SS
618 *scontext = NULL;
619 *scontext_len = 0;
620
1da177e4
LT
621 if (!ss_initialized) {
622 if (sid <= SECINITSID_NUM) {
623 char *scontextp;
624
625 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
626 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
0cccca06
SH
627 if (!scontextp) {
628 rc = -ENOMEM;
629 goto out;
630 }
1da177e4
LT
631 strcpy(scontextp, initial_sid_to_string[sid]);
632 *scontext = scontextp;
633 goto out;
634 }
635 printk(KERN_ERR "security_sid_to_context: called before initial "
636 "load_policy on unknown SID %d\n", sid);
637 rc = -EINVAL;
638 goto out;
639 }
640 POLICY_RDLOCK;
641 context = sidtab_search(&sidtab, sid);
642 if (!context) {
643 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
644 "%d\n", sid);
645 rc = -EINVAL;
646 goto out_unlock;
647 }
648 rc = context_struct_to_string(context, scontext, scontext_len);
649out_unlock:
650 POLICY_RDUNLOCK;
651out:
652 return rc;
653
654}
655
f5c1d5b2 656static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
1da177e4
LT
657{
658 char *scontext2;
659 struct context context;
660 struct role_datum *role;
661 struct type_datum *typdatum;
662 struct user_datum *usrdatum;
663 char *scontextp, *p, oldc;
664 int rc = 0;
665
666 if (!ss_initialized) {
667 int i;
668
669 for (i = 1; i < SECINITSID_NUM; i++) {
670 if (!strcmp(initial_sid_to_string[i], scontext)) {
671 *sid = i;
672 goto out;
673 }
674 }
675 *sid = SECINITSID_KERNEL;
676 goto out;
677 }
678 *sid = SECSID_NULL;
679
680 /* Copy the string so that we can modify the copy as we parse it.
681 The string should already by null terminated, but we append a
682 null suffix to the copy to avoid problems with the existing
683 attr package, which doesn't view the null terminator as part
684 of the attribute value. */
685 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
686 if (!scontext2) {
687 rc = -ENOMEM;
688 goto out;
689 }
690 memcpy(scontext2, scontext, scontext_len);
691 scontext2[scontext_len] = 0;
692
693 context_init(&context);
694 *sid = SECSID_NULL;
695
696 POLICY_RDLOCK;
697
698 /* Parse the security context. */
699
700 rc = -EINVAL;
701 scontextp = (char *) scontext2;
702
703 /* Extract the user. */
704 p = scontextp;
705 while (*p && *p != ':')
706 p++;
707
708 if (*p == 0)
709 goto out_unlock;
710
711 *p++ = 0;
712
713 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
714 if (!usrdatum)
715 goto out_unlock;
716
717 context.user = usrdatum->value;
718
719 /* Extract role. */
720 scontextp = p;
721 while (*p && *p != ':')
722 p++;
723
724 if (*p == 0)
725 goto out_unlock;
726
727 *p++ = 0;
728
729 role = hashtab_search(policydb.p_roles.table, scontextp);
730 if (!role)
731 goto out_unlock;
732 context.role = role->value;
733
734 /* Extract type. */
735 scontextp = p;
736 while (*p && *p != ':')
737 p++;
738 oldc = *p;
739 *p++ = 0;
740
741 typdatum = hashtab_search(policydb.p_types.table, scontextp);
742 if (!typdatum)
743 goto out_unlock;
744
745 context.type = typdatum->value;
746
f5c1d5b2 747 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
1da177e4
LT
748 if (rc)
749 goto out_unlock;
750
751 if ((p - scontext2) < scontext_len) {
752 rc = -EINVAL;
753 goto out_unlock;
754 }
755
756 /* Check the validity of the new context. */
757 if (!policydb_context_isvalid(&policydb, &context)) {
758 rc = -EINVAL;
759 goto out_unlock;
760 }
761 /* Obtain the new sid. */
762 rc = sidtab_context_to_sid(&sidtab, &context, sid);
763out_unlock:
764 POLICY_RDUNLOCK;
765 context_destroy(&context);
766 kfree(scontext2);
767out:
768 return rc;
769}
770
f5c1d5b2
JM
771/**
772 * security_context_to_sid - Obtain a SID for a given security context.
773 * @scontext: security context
774 * @scontext_len: length in bytes
775 * @sid: security identifier, SID
776 *
777 * Obtains a SID associated with the security context that
778 * has the string representation specified by @scontext.
779 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
780 * memory is available, or 0 on success.
781 */
782int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
783{
784 return security_context_to_sid_core(scontext, scontext_len,
785 sid, SECSID_NULL);
786}
787
788/**
789 * security_context_to_sid_default - Obtain a SID for a given security context,
790 * falling back to specified default if needed.
791 *
792 * @scontext: security context
793 * @scontext_len: length in bytes
794 * @sid: security identifier, SID
795 * @def_sid: default SID to assign on errror
796 *
797 * Obtains a SID associated with the security context that
798 * has the string representation specified by @scontext.
799 * The default SID is passed to the MLS layer to be used to allow
800 * kernel labeling of the MLS field if the MLS field is not present
801 * (for upgrading to MLS without full relabel).
802 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
803 * memory is available, or 0 on success.
804 */
805int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
806{
807 return security_context_to_sid_core(scontext, scontext_len,
808 sid, def_sid);
809}
810
1da177e4
LT
811static int compute_sid_handle_invalid_context(
812 struct context *scontext,
813 struct context *tcontext,
814 u16 tclass,
815 struct context *newcontext)
816{
817 char *s = NULL, *t = NULL, *n = NULL;
818 u32 slen, tlen, nlen;
819
820 if (context_struct_to_string(scontext, &s, &slen) < 0)
821 goto out;
822 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
823 goto out;
824 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
825 goto out;
9ad9ad38 826 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1da177e4
LT
827 "security_compute_sid: invalid context %s"
828 " for scontext=%s"
829 " tcontext=%s"
830 " tclass=%s",
831 n, s, t, policydb.p_class_val_to_name[tclass-1]);
832out:
833 kfree(s);
834 kfree(t);
835 kfree(n);
836 if (!selinux_enforcing)
837 return 0;
838 return -EACCES;
839}
840
841static int security_compute_sid(u32 ssid,
842 u32 tsid,
843 u16 tclass,
844 u32 specified,
845 u32 *out_sid)
846{
847 struct context *scontext = NULL, *tcontext = NULL, newcontext;
848 struct role_trans *roletr = NULL;
849 struct avtab_key avkey;
850 struct avtab_datum *avdatum;
851 struct avtab_node *node;
1da177e4
LT
852 int rc = 0;
853
854 if (!ss_initialized) {
855 switch (tclass) {
856 case SECCLASS_PROCESS:
857 *out_sid = ssid;
858 break;
859 default:
860 *out_sid = tsid;
861 break;
862 }
863 goto out;
864 }
865
851f8a69
VY
866 context_init(&newcontext);
867
1da177e4
LT
868 POLICY_RDLOCK;
869
870 scontext = sidtab_search(&sidtab, ssid);
871 if (!scontext) {
872 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
873 ssid);
874 rc = -EINVAL;
875 goto out_unlock;
876 }
877 tcontext = sidtab_search(&sidtab, tsid);
878 if (!tcontext) {
879 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
880 tsid);
881 rc = -EINVAL;
882 goto out_unlock;
883 }
884
1da177e4
LT
885 /* Set the user identity. */
886 switch (specified) {
887 case AVTAB_TRANSITION:
888 case AVTAB_CHANGE:
889 /* Use the process user identity. */
890 newcontext.user = scontext->user;
891 break;
892 case AVTAB_MEMBER:
893 /* Use the related object owner. */
894 newcontext.user = tcontext->user;
895 break;
896 }
897
898 /* Set the role and type to default values. */
899 switch (tclass) {
900 case SECCLASS_PROCESS:
901 /* Use the current role and type of process. */
902 newcontext.role = scontext->role;
903 newcontext.type = scontext->type;
904 break;
905 default:
906 /* Use the well-defined object role. */
907 newcontext.role = OBJECT_R_VAL;
908 /* Use the type of the related object. */
909 newcontext.type = tcontext->type;
910 }
911
912 /* Look for a type transition/member/change rule. */
913 avkey.source_type = scontext->type;
914 avkey.target_type = tcontext->type;
915 avkey.target_class = tclass;
782ebb99
SS
916 avkey.specified = specified;
917 avdatum = avtab_search(&policydb.te_avtab, &avkey);
1da177e4
LT
918
919 /* If no permanent rule, also check for enabled conditional rules */
920 if(!avdatum) {
782ebb99 921 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
1da177e4 922 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
782ebb99 923 if (node->key.specified & AVTAB_ENABLED) {
1da177e4
LT
924 avdatum = &node->datum;
925 break;
926 }
927 }
928 }
929
782ebb99 930 if (avdatum) {
1da177e4 931 /* Use the type from the type transition/member/change rule. */
782ebb99 932 newcontext.type = avdatum->data;
1da177e4
LT
933 }
934
935 /* Check for class-specific changes. */
936 switch (tclass) {
937 case SECCLASS_PROCESS:
938 if (specified & AVTAB_TRANSITION) {
939 /* Look for a role transition rule. */
940 for (roletr = policydb.role_tr; roletr;
941 roletr = roletr->next) {
942 if (roletr->role == scontext->role &&
943 roletr->type == tcontext->type) {
944 /* Use the role transition rule. */
945 newcontext.role = roletr->new_role;
946 break;
947 }
948 }
949 }
950 break;
951 default:
952 break;
953 }
954
955 /* Set the MLS attributes.
956 This is done last because it may allocate memory. */
957 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
958 if (rc)
959 goto out_unlock;
960
961 /* Check the validity of the context. */
962 if (!policydb_context_isvalid(&policydb, &newcontext)) {
963 rc = compute_sid_handle_invalid_context(scontext,
964 tcontext,
965 tclass,
966 &newcontext);
967 if (rc)
968 goto out_unlock;
969 }
970 /* Obtain the sid for the context. */
971 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
972out_unlock:
973 POLICY_RDUNLOCK;
974 context_destroy(&newcontext);
975out:
976 return rc;
977}
978
979/**
980 * security_transition_sid - Compute the SID for a new subject/object.
981 * @ssid: source security identifier
982 * @tsid: target security identifier
983 * @tclass: target security class
984 * @out_sid: security identifier for new subject/object
985 *
986 * Compute a SID to use for labeling a new subject or object in the
987 * class @tclass based on a SID pair (@ssid, @tsid).
988 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
989 * if insufficient memory is available, or %0 if the new SID was
990 * computed successfully.
991 */
992int security_transition_sid(u32 ssid,
993 u32 tsid,
994 u16 tclass,
995 u32 *out_sid)
996{
997 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
998}
999
1000/**
1001 * security_member_sid - Compute the SID for member selection.
1002 * @ssid: source security identifier
1003 * @tsid: target security identifier
1004 * @tclass: target security class
1005 * @out_sid: security identifier for selected member
1006 *
1007 * Compute a SID to use when selecting a member of a polyinstantiated
1008 * object of class @tclass based on a SID pair (@ssid, @tsid).
1009 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1010 * if insufficient memory is available, or %0 if the SID was
1011 * computed successfully.
1012 */
1013int security_member_sid(u32 ssid,
1014 u32 tsid,
1015 u16 tclass,
1016 u32 *out_sid)
1017{
1018 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1019}
1020
1021/**
1022 * security_change_sid - Compute the SID for object relabeling.
1023 * @ssid: source security identifier
1024 * @tsid: target security identifier
1025 * @tclass: target security class
1026 * @out_sid: security identifier for selected member
1027 *
1028 * Compute a SID to use for relabeling an object of class @tclass
1029 * based on a SID pair (@ssid, @tsid).
1030 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1031 * if insufficient memory is available, or %0 if the SID was
1032 * computed successfully.
1033 */
1034int security_change_sid(u32 ssid,
1035 u32 tsid,
1036 u16 tclass,
1037 u32 *out_sid)
1038{
1039 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1040}
1041
b94c7e67
CS
1042/*
1043 * Verify that each kernel class that is defined in the
1044 * policy is correct
1045 */
1046static int validate_classes(struct policydb *p)
1047{
1048 int i, j;
1049 struct class_datum *cladatum;
1050 struct perm_datum *perdatum;
1051 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1052 u16 class_val;
1053 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1054 const char *def_class, *def_perm, *pol_class;
1055 struct symtab *perms;
1056
1057 for (i = 1; i < kdefs->cts_len; i++) {
1058 def_class = kdefs->class_to_string[i];
a764ae4b
SS
1059 if (!def_class)
1060 continue;
b94c7e67
CS
1061 if (i > p->p_classes.nprim) {
1062 printk(KERN_INFO
1063 "security: class %s not defined in policy\n",
1064 def_class);
1065 continue;
1066 }
1067 pol_class = p->p_class_val_to_name[i-1];
1068 if (strcmp(pol_class, def_class)) {
1069 printk(KERN_ERR
1070 "security: class %d is incorrect, found %s but should be %s\n",
1071 i, pol_class, def_class);
1072 return -EINVAL;
1073 }
1074 }
1075 for (i = 0; i < kdefs->av_pts_len; i++) {
1076 class_val = kdefs->av_perm_to_string[i].tclass;
1077 perm_val = kdefs->av_perm_to_string[i].value;
1078 def_perm = kdefs->av_perm_to_string[i].name;
1079 if (class_val > p->p_classes.nprim)
1080 continue;
1081 pol_class = p->p_class_val_to_name[class_val-1];
1082 cladatum = hashtab_search(p->p_classes.table, pol_class);
1083 BUG_ON(!cladatum);
1084 perms = &cladatum->permissions;
1085 nprim = 1 << (perms->nprim - 1);
1086 if (perm_val > nprim) {
1087 printk(KERN_INFO
1088 "security: permission %s in class %s not defined in policy\n",
1089 def_perm, pol_class);
1090 continue;
1091 }
1092 perdatum = hashtab_search(perms->table, def_perm);
1093 if (perdatum == NULL) {
1094 printk(KERN_ERR
1095 "security: permission %s in class %s not found in policy\n",
1096 def_perm, pol_class);
1097 return -EINVAL;
1098 }
1099 pol_val = 1 << (perdatum->value - 1);
1100 if (pol_val != perm_val) {
1101 printk(KERN_ERR
1102 "security: permission %s in class %s has incorrect value\n",
1103 def_perm, pol_class);
1104 return -EINVAL;
1105 }
1106 }
1107 for (i = 0; i < kdefs->av_inherit_len; i++) {
1108 class_val = kdefs->av_inherit[i].tclass;
1109 if (class_val > p->p_classes.nprim)
1110 continue;
1111 pol_class = p->p_class_val_to_name[class_val-1];
1112 cladatum = hashtab_search(p->p_classes.table, pol_class);
1113 BUG_ON(!cladatum);
1114 if (!cladatum->comdatum) {
1115 printk(KERN_ERR
1116 "security: class %s should have an inherits clause but does not\n",
1117 pol_class);
1118 return -EINVAL;
1119 }
1120 tmp = kdefs->av_inherit[i].common_base;
1121 common_pts_len = 0;
1122 while (!(tmp & 0x01)) {
1123 common_pts_len++;
1124 tmp >>= 1;
1125 }
1126 perms = &cladatum->comdatum->permissions;
1127 for (j = 0; j < common_pts_len; j++) {
1128 def_perm = kdefs->av_inherit[i].common_pts[j];
1129 if (j >= perms->nprim) {
1130 printk(KERN_INFO
1131 "security: permission %s in class %s not defined in policy\n",
1132 def_perm, pol_class);
1133 continue;
1134 }
1135 perdatum = hashtab_search(perms->table, def_perm);
1136 if (perdatum == NULL) {
1137 printk(KERN_ERR
1138 "security: permission %s in class %s not found in policy\n",
1139 def_perm, pol_class);
1140 return -EINVAL;
1141 }
1142 if (perdatum->value != j + 1) {
1143 printk(KERN_ERR
1144 "security: permission %s in class %s has incorrect value\n",
1145 def_perm, pol_class);
1146 return -EINVAL;
1147 }
1148 }
1149 }
1150 return 0;
1151}
1152
1da177e4
LT
1153/* Clone the SID into the new SID table. */
1154static int clone_sid(u32 sid,
1155 struct context *context,
1156 void *arg)
1157{
1158 struct sidtab *s = arg;
1159
1160 return sidtab_insert(s, sid, context);
1161}
1162
1163static inline int convert_context_handle_invalid_context(struct context *context)
1164{
1165 int rc = 0;
1166
1167 if (selinux_enforcing) {
1168 rc = -EINVAL;
1169 } else {
1170 char *s;
1171 u32 len;
1172
1173 context_struct_to_string(context, &s, &len);
1174 printk(KERN_ERR "security: context %s is invalid\n", s);
1175 kfree(s);
1176 }
1177 return rc;
1178}
1179
1180struct convert_context_args {
1181 struct policydb *oldp;
1182 struct policydb *newp;
1183};
1184
1185/*
1186 * Convert the values in the security context
1187 * structure `c' from the values specified
1188 * in the policy `p->oldp' to the values specified
1189 * in the policy `p->newp'. Verify that the
1190 * context is valid under the new policy.
1191 */
1192static int convert_context(u32 key,
1193 struct context *c,
1194 void *p)
1195{
1196 struct convert_context_args *args;
1197 struct context oldc;
1198 struct role_datum *role;
1199 struct type_datum *typdatum;
1200 struct user_datum *usrdatum;
1201 char *s;
1202 u32 len;
1203 int rc;
1204
1205 args = p;
1206
1207 rc = context_cpy(&oldc, c);
1208 if (rc)
1209 goto out;
1210
1211 rc = -EINVAL;
1212
1213 /* Convert the user. */
1214 usrdatum = hashtab_search(args->newp->p_users.table,
1215 args->oldp->p_user_val_to_name[c->user - 1]);
1216 if (!usrdatum) {
1217 goto bad;
1218 }
1219 c->user = usrdatum->value;
1220
1221 /* Convert the role. */
1222 role = hashtab_search(args->newp->p_roles.table,
1223 args->oldp->p_role_val_to_name[c->role - 1]);
1224 if (!role) {
1225 goto bad;
1226 }
1227 c->role = role->value;
1228
1229 /* Convert the type. */
1230 typdatum = hashtab_search(args->newp->p_types.table,
1231 args->oldp->p_type_val_to_name[c->type - 1]);
1232 if (!typdatum) {
1233 goto bad;
1234 }
1235 c->type = typdatum->value;
1236
1237 rc = mls_convert_context(args->oldp, args->newp, c);
1238 if (rc)
1239 goto bad;
1240
1241 /* Check the validity of the new context. */
1242 if (!policydb_context_isvalid(args->newp, c)) {
1243 rc = convert_context_handle_invalid_context(&oldc);
1244 if (rc)
1245 goto bad;
1246 }
1247
1248 context_destroy(&oldc);
1249out:
1250 return rc;
1251bad:
1252 context_struct_to_string(&oldc, &s, &len);
1253 context_destroy(&oldc);
1254 printk(KERN_ERR "security: invalidating context %s\n", s);
1255 kfree(s);
1256 goto out;
1257}
1258
1259extern void selinux_complete_init(void);
1260
1261/**
1262 * security_load_policy - Load a security policy configuration.
1263 * @data: binary policy data
1264 * @len: length of data in bytes
1265 *
1266 * Load a new set of security policy configuration data,
1267 * validate it and convert the SID table as necessary.
1268 * This function will flush the access vector cache after
1269 * loading the new policy.
1270 */
1271int security_load_policy(void *data, size_t len)
1272{
1273 struct policydb oldpolicydb, newpolicydb;
1274 struct sidtab oldsidtab, newsidtab;
1275 struct convert_context_args args;
1276 u32 seqno;
1277 int rc = 0;
1278 struct policy_file file = { data, len }, *fp = &file;
1279
1280 LOAD_LOCK;
1281
1282 if (!ss_initialized) {
1283 avtab_cache_init();
1284 if (policydb_read(&policydb, fp)) {
1285 LOAD_UNLOCK;
1286 avtab_cache_destroy();
1287 return -EINVAL;
1288 }
1289 if (policydb_load_isids(&policydb, &sidtab)) {
1290 LOAD_UNLOCK;
1291 policydb_destroy(&policydb);
1292 avtab_cache_destroy();
1293 return -EINVAL;
1294 }
b94c7e67
CS
1295 /* Verify that the kernel defined classes are correct. */
1296 if (validate_classes(&policydb)) {
1297 printk(KERN_ERR
1298 "security: the definition of a class is incorrect\n");
1299 LOAD_UNLOCK;
1300 sidtab_destroy(&sidtab);
1301 policydb_destroy(&policydb);
1302 avtab_cache_destroy();
1303 return -EINVAL;
1304 }
1da177e4
LT
1305 policydb_loaded_version = policydb.policyvers;
1306 ss_initialized = 1;
4c443d1b 1307 seqno = ++latest_granting;
1da177e4
LT
1308 LOAD_UNLOCK;
1309 selinux_complete_init();
4c443d1b
SS
1310 avc_ss_reset(seqno);
1311 selnl_notify_policyload(seqno);
7420ed23 1312 selinux_netlbl_cache_invalidate();
342a0cff 1313 selinux_xfrm_notify_policyload();
1da177e4
LT
1314 return 0;
1315 }
1316
1317#if 0
1318 sidtab_hash_eval(&sidtab, "sids");
1319#endif
1320
1321 if (policydb_read(&newpolicydb, fp)) {
1322 LOAD_UNLOCK;
1323 return -EINVAL;
1324 }
1325
1326 sidtab_init(&newsidtab);
1327
b94c7e67
CS
1328 /* Verify that the kernel defined classes are correct. */
1329 if (validate_classes(&newpolicydb)) {
1330 printk(KERN_ERR
1331 "security: the definition of a class is incorrect\n");
1332 rc = -EINVAL;
1333 goto err;
1334 }
1335
1da177e4
LT
1336 /* Clone the SID table. */
1337 sidtab_shutdown(&sidtab);
1338 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1339 rc = -ENOMEM;
1340 goto err;
1341 }
1342
1343 /* Convert the internal representations of contexts
1344 in the new SID table and remove invalid SIDs. */
1345 args.oldp = &policydb;
1346 args.newp = &newpolicydb;
1347 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1348
1349 /* Save the old policydb and SID table to free later. */
1350 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1351 sidtab_set(&oldsidtab, &sidtab);
1352
1353 /* Install the new policydb and SID table. */
1354 POLICY_WRLOCK;
1355 memcpy(&policydb, &newpolicydb, sizeof policydb);
1356 sidtab_set(&sidtab, &newsidtab);
1357 seqno = ++latest_granting;
1358 policydb_loaded_version = policydb.policyvers;
1359 POLICY_WRUNLOCK;
1360 LOAD_UNLOCK;
1361
1362 /* Free the old policydb and SID table. */
1363 policydb_destroy(&oldpolicydb);
1364 sidtab_destroy(&oldsidtab);
1365
1366 avc_ss_reset(seqno);
1367 selnl_notify_policyload(seqno);
7420ed23 1368 selinux_netlbl_cache_invalidate();
342a0cff 1369 selinux_xfrm_notify_policyload();
1da177e4
LT
1370
1371 return 0;
1372
1373err:
1374 LOAD_UNLOCK;
1375 sidtab_destroy(&newsidtab);
1376 policydb_destroy(&newpolicydb);
1377 return rc;
1378
1379}
1380
1381/**
1382 * security_port_sid - Obtain the SID for a port.
1383 * @domain: communication domain aka address family
1384 * @type: socket type
1385 * @protocol: protocol number
1386 * @port: port number
1387 * @out_sid: security identifier
1388 */
1389int security_port_sid(u16 domain,
1390 u16 type,
1391 u8 protocol,
1392 u16 port,
1393 u32 *out_sid)
1394{
1395 struct ocontext *c;
1396 int rc = 0;
1397
1398 POLICY_RDLOCK;
1399
1400 c = policydb.ocontexts[OCON_PORT];
1401 while (c) {
1402 if (c->u.port.protocol == protocol &&
1403 c->u.port.low_port <= port &&
1404 c->u.port.high_port >= port)
1405 break;
1406 c = c->next;
1407 }
1408
1409 if (c) {
1410 if (!c->sid[0]) {
1411 rc = sidtab_context_to_sid(&sidtab,
1412 &c->context[0],
1413 &c->sid[0]);
1414 if (rc)
1415 goto out;
1416 }
1417 *out_sid = c->sid[0];
1418 } else {
1419 *out_sid = SECINITSID_PORT;
1420 }
1421
1422out:
1423 POLICY_RDUNLOCK;
1424 return rc;
1425}
1426
1427/**
1428 * security_netif_sid - Obtain the SID for a network interface.
1429 * @name: interface name
1430 * @if_sid: interface SID
1431 * @msg_sid: default SID for received packets
1432 */
1433int security_netif_sid(char *name,
1434 u32 *if_sid,
1435 u32 *msg_sid)
1436{
1437 int rc = 0;
1438 struct ocontext *c;
1439
1440 POLICY_RDLOCK;
1441
1442 c = policydb.ocontexts[OCON_NETIF];
1443 while (c) {
1444 if (strcmp(name, c->u.name) == 0)
1445 break;
1446 c = c->next;
1447 }
1448
1449 if (c) {
1450 if (!c->sid[0] || !c->sid[1]) {
1451 rc = sidtab_context_to_sid(&sidtab,
1452 &c->context[0],
1453 &c->sid[0]);
1454 if (rc)
1455 goto out;
1456 rc = sidtab_context_to_sid(&sidtab,
1457 &c->context[1],
1458 &c->sid[1]);
1459 if (rc)
1460 goto out;
1461 }
1462 *if_sid = c->sid[0];
1463 *msg_sid = c->sid[1];
1464 } else {
1465 *if_sid = SECINITSID_NETIF;
1466 *msg_sid = SECINITSID_NETMSG;
1467 }
1468
1469out:
1470 POLICY_RDUNLOCK;
1471 return rc;
1472}
1473
1474static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1475{
1476 int i, fail = 0;
1477
1478 for(i = 0; i < 4; i++)
1479 if(addr[i] != (input[i] & mask[i])) {
1480 fail = 1;
1481 break;
1482 }
1483
1484 return !fail;
1485}
1486
1487/**
1488 * security_node_sid - Obtain the SID for a node (host).
1489 * @domain: communication domain aka address family
1490 * @addrp: address
1491 * @addrlen: address length in bytes
1492 * @out_sid: security identifier
1493 */
1494int security_node_sid(u16 domain,
1495 void *addrp,
1496 u32 addrlen,
1497 u32 *out_sid)
1498{
1499 int rc = 0;
1500 struct ocontext *c;
1501
1502 POLICY_RDLOCK;
1503
1504 switch (domain) {
1505 case AF_INET: {
1506 u32 addr;
1507
1508 if (addrlen != sizeof(u32)) {
1509 rc = -EINVAL;
1510 goto out;
1511 }
1512
1513 addr = *((u32 *)addrp);
1514
1515 c = policydb.ocontexts[OCON_NODE];
1516 while (c) {
1517 if (c->u.node.addr == (addr & c->u.node.mask))
1518 break;
1519 c = c->next;
1520 }
1521 break;
1522 }
1523
1524 case AF_INET6:
1525 if (addrlen != sizeof(u64) * 2) {
1526 rc = -EINVAL;
1527 goto out;
1528 }
1529 c = policydb.ocontexts[OCON_NODE6];
1530 while (c) {
1531 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1532 c->u.node6.mask))
1533 break;
1534 c = c->next;
1535 }
1536 break;
1537
1538 default:
1539 *out_sid = SECINITSID_NODE;
1540 goto out;
1541 }
1542
1543 if (c) {
1544 if (!c->sid[0]) {
1545 rc = sidtab_context_to_sid(&sidtab,
1546 &c->context[0],
1547 &c->sid[0]);
1548 if (rc)
1549 goto out;
1550 }
1551 *out_sid = c->sid[0];
1552 } else {
1553 *out_sid = SECINITSID_NODE;
1554 }
1555
1556out:
1557 POLICY_RDUNLOCK;
1558 return rc;
1559}
1560
1561#define SIDS_NEL 25
1562
1563/**
1564 * security_get_user_sids - Obtain reachable SIDs for a user.
1565 * @fromsid: starting SID
1566 * @username: username
1567 * @sids: array of reachable SIDs for user
1568 * @nel: number of elements in @sids
1569 *
1570 * Generate the set of SIDs for legal security contexts
1571 * for a given user that can be reached by @fromsid.
1572 * Set *@sids to point to a dynamically allocated
1573 * array containing the set of SIDs. Set *@nel to the
1574 * number of elements in the array.
1575 */
1576
1577int security_get_user_sids(u32 fromsid,
1578 char *username,
1579 u32 **sids,
1580 u32 *nel)
1581{
1582 struct context *fromcon, usercon;
1583 u32 *mysids, *mysids2, sid;
1584 u32 mynel = 0, maxnel = SIDS_NEL;
1585 struct user_datum *user;
1586 struct role_datum *role;
1587 struct av_decision avd;
782ebb99 1588 struct ebitmap_node *rnode, *tnode;
1da177e4
LT
1589 int rc = 0, i, j;
1590
1591 if (!ss_initialized) {
1592 *sids = NULL;
1593 *nel = 0;
1594 goto out;
1595 }
1596
1597 POLICY_RDLOCK;
1598
1599 fromcon = sidtab_search(&sidtab, fromsid);
1600 if (!fromcon) {
1601 rc = -EINVAL;
1602 goto out_unlock;
1603 }
1604
1605 user = hashtab_search(policydb.p_users.table, username);
1606 if (!user) {
1607 rc = -EINVAL;
1608 goto out_unlock;
1609 }
1610 usercon.user = user->value;
1611
89d155ef 1612 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1da177e4
LT
1613 if (!mysids) {
1614 rc = -ENOMEM;
1615 goto out_unlock;
1616 }
1da177e4 1617
782ebb99
SS
1618 ebitmap_for_each_bit(&user->roles, rnode, i) {
1619 if (!ebitmap_node_get_bit(rnode, i))
1da177e4
LT
1620 continue;
1621 role = policydb.role_val_to_struct[i];
1622 usercon.role = i+1;
782ebb99
SS
1623 ebitmap_for_each_bit(&role->types, tnode, j) {
1624 if (!ebitmap_node_get_bit(tnode, j))
1da177e4
LT
1625 continue;
1626 usercon.type = j+1;
1627
1628 if (mls_setup_user_range(fromcon, user, &usercon))
1629 continue;
1630
1631 rc = context_struct_compute_av(fromcon, &usercon,
1632 SECCLASS_PROCESS,
1633 PROCESS__TRANSITION,
1634 &avd);
1635 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1636 continue;
1637 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1638 if (rc) {
1639 kfree(mysids);
1640 goto out_unlock;
1641 }
1642 if (mynel < maxnel) {
1643 mysids[mynel++] = sid;
1644 } else {
1645 maxnel += SIDS_NEL;
89d155ef 1646 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1da177e4
LT
1647 if (!mysids2) {
1648 rc = -ENOMEM;
1649 kfree(mysids);
1650 goto out_unlock;
1651 }
1da177e4
LT
1652 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1653 kfree(mysids);
1654 mysids = mysids2;
1655 mysids[mynel++] = sid;
1656 }
1657 }
1658 }
1659
1660 *sids = mysids;
1661 *nel = mynel;
1662
1663out_unlock:
1664 POLICY_RDUNLOCK;
1665out:
1666 return rc;
1667}
1668
1669/**
1670 * security_genfs_sid - Obtain a SID for a file in a filesystem
1671 * @fstype: filesystem type
1672 * @path: path from root of mount
1673 * @sclass: file security class
1674 * @sid: SID for path
1675 *
1676 * Obtain a SID to use for a file in a filesystem that
1677 * cannot support xattr or use a fixed labeling behavior like
1678 * transition SIDs or task SIDs.
1679 */
1680int security_genfs_sid(const char *fstype,
1681 char *path,
1682 u16 sclass,
1683 u32 *sid)
1684{
1685 int len;
1686 struct genfs *genfs;
1687 struct ocontext *c;
1688 int rc = 0, cmp = 0;
1689
1690 POLICY_RDLOCK;
1691
1692 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1693 cmp = strcmp(fstype, genfs->fstype);
1694 if (cmp <= 0)
1695 break;
1696 }
1697
1698 if (!genfs || cmp) {
1699 *sid = SECINITSID_UNLABELED;
1700 rc = -ENOENT;
1701 goto out;
1702 }
1703
1704 for (c = genfs->head; c; c = c->next) {
1705 len = strlen(c->u.name);
1706 if ((!c->v.sclass || sclass == c->v.sclass) &&
1707 (strncmp(c->u.name, path, len) == 0))
1708 break;
1709 }
1710
1711 if (!c) {
1712 *sid = SECINITSID_UNLABELED;
1713 rc = -ENOENT;
1714 goto out;
1715 }
1716
1717 if (!c->sid[0]) {
1718 rc = sidtab_context_to_sid(&sidtab,
1719 &c->context[0],
1720 &c->sid[0]);
1721 if (rc)
1722 goto out;
1723 }
1724
1725 *sid = c->sid[0];
1726out:
1727 POLICY_RDUNLOCK;
1728 return rc;
1729}
1730
1731/**
1732 * security_fs_use - Determine how to handle labeling for a filesystem.
1733 * @fstype: filesystem type
1734 * @behavior: labeling behavior
1735 * @sid: SID for filesystem (superblock)
1736 */
1737int security_fs_use(
1738 const char *fstype,
1739 unsigned int *behavior,
1740 u32 *sid)
1741{
1742 int rc = 0;
1743 struct ocontext *c;
1744
1745 POLICY_RDLOCK;
1746
1747 c = policydb.ocontexts[OCON_FSUSE];
1748 while (c) {
1749 if (strcmp(fstype, c->u.name) == 0)
1750 break;
1751 c = c->next;
1752 }
1753
1754 if (c) {
1755 *behavior = c->v.behavior;
1756 if (!c->sid[0]) {
1757 rc = sidtab_context_to_sid(&sidtab,
1758 &c->context[0],
1759 &c->sid[0]);
1760 if (rc)
1761 goto out;
1762 }
1763 *sid = c->sid[0];
1764 } else {
1765 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1766 if (rc) {
1767 *behavior = SECURITY_FS_USE_NONE;
1768 rc = 0;
1769 } else {
1770 *behavior = SECURITY_FS_USE_GENFS;
1771 }
1772 }
1773
1774out:
1775 POLICY_RDUNLOCK;
1776 return rc;
1777}
1778
1779int security_get_bools(int *len, char ***names, int **values)
1780{
1781 int i, rc = -ENOMEM;
1782
1783 POLICY_RDLOCK;
1784 *names = NULL;
1785 *values = NULL;
1786
1787 *len = policydb.p_bools.nprim;
1788 if (!*len) {
1789 rc = 0;
1790 goto out;
1791 }
1792
e0795cf4 1793 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1da177e4
LT
1794 if (!*names)
1795 goto err;
1da177e4 1796
e0795cf4 1797 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1da177e4
LT
1798 if (!*values)
1799 goto err;
1800
1801 for (i = 0; i < *len; i++) {
1802 size_t name_len;
1803 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1804 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
e0795cf4 1805 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1da177e4
LT
1806 if (!(*names)[i])
1807 goto err;
1808 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1809 (*names)[i][name_len - 1] = 0;
1810 }
1811 rc = 0;
1812out:
1813 POLICY_RDUNLOCK;
1814 return rc;
1815err:
1816 if (*names) {
1817 for (i = 0; i < *len; i++)
9a5f04bf 1818 kfree((*names)[i]);
1da177e4 1819 }
9a5f04bf 1820 kfree(*values);
1da177e4
LT
1821 goto out;
1822}
1823
1824
1825int security_set_bools(int len, int *values)
1826{
1827 int i, rc = 0;
1828 int lenp, seqno = 0;
1829 struct cond_node *cur;
1830
1831 POLICY_WRLOCK;
1832
1833 lenp = policydb.p_bools.nprim;
1834 if (len != lenp) {
1835 rc = -EFAULT;
1836 goto out;
1837 }
1838
1da177e4 1839 for (i = 0; i < len; i++) {
af601e46
SG
1840 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1841 audit_log(current->audit_context, GFP_ATOMIC,
1842 AUDIT_MAC_CONFIG_CHANGE,
1843 "bool=%s val=%d old_val=%d auid=%u",
1844 policydb.p_bool_val_to_name[i],
1845 !!values[i],
1846 policydb.bool_val_to_struct[i]->state,
1847 audit_get_loginuid(current->audit_context));
1848 }
1da177e4
LT
1849 if (values[i]) {
1850 policydb.bool_val_to_struct[i]->state = 1;
1851 } else {
1852 policydb.bool_val_to_struct[i]->state = 0;
1853 }
1da177e4 1854 }
1da177e4
LT
1855
1856 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1857 rc = evaluate_cond_node(&policydb, cur);
1858 if (rc)
1859 goto out;
1860 }
1861
1862 seqno = ++latest_granting;
1863
1864out:
1865 POLICY_WRUNLOCK;
1866 if (!rc) {
1867 avc_ss_reset(seqno);
1868 selnl_notify_policyload(seqno);
342a0cff 1869 selinux_xfrm_notify_policyload();
1da177e4
LT
1870 }
1871 return rc;
1872}
1873
1874int security_get_bool_value(int bool)
1875{
1876 int rc = 0;
1877 int len;
1878
1879 POLICY_RDLOCK;
1880
1881 len = policydb.p_bools.nprim;
1882 if (bool >= len) {
1883 rc = -EFAULT;
1884 goto out;
1885 }
1886
1887 rc = policydb.bool_val_to_struct[bool]->state;
1888out:
1889 POLICY_RDUNLOCK;
1890 return rc;
1891}
376bd9cb 1892
08554d6b
VY
1893/*
1894 * security_sid_mls_copy() - computes a new sid based on the given
1895 * sid and the mls portion of mls_sid.
1896 */
1897int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1898{
1899 struct context *context1;
1900 struct context *context2;
1901 struct context newcon;
1902 char *s;
1903 u32 len;
1904 int rc = 0;
1905
4eb327b5 1906 if (!ss_initialized || !selinux_mls_enabled) {
08554d6b
VY
1907 *new_sid = sid;
1908 goto out;
1909 }
1910
1911 context_init(&newcon);
1912
1913 POLICY_RDLOCK;
1914 context1 = sidtab_search(&sidtab, sid);
1915 if (!context1) {
1916 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1917 "%d\n", sid);
1918 rc = -EINVAL;
1919 goto out_unlock;
1920 }
1921
1922 context2 = sidtab_search(&sidtab, mls_sid);
1923 if (!context2) {
1924 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1925 "%d\n", mls_sid);
1926 rc = -EINVAL;
1927 goto out_unlock;
1928 }
1929
1930 newcon.user = context1->user;
1931 newcon.role = context1->role;
1932 newcon.type = context1->type;
0efc61ea 1933 rc = mls_context_cpy(&newcon, context2);
08554d6b
VY
1934 if (rc)
1935 goto out_unlock;
1936
08554d6b
VY
1937 /* Check the validity of the new context. */
1938 if (!policydb_context_isvalid(&policydb, &newcon)) {
1939 rc = convert_context_handle_invalid_context(&newcon);
1940 if (rc)
1941 goto bad;
1942 }
1943
1944 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1945 goto out_unlock;
1946
1947bad:
1948 if (!context_struct_to_string(&newcon, &s, &len)) {
1949 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1950 "security_sid_mls_copy: invalid context %s", s);
1951 kfree(s);
1952 }
1953
1954out_unlock:
1955 POLICY_RDUNLOCK;
1956 context_destroy(&newcon);
1957out:
1958 return rc;
1959}
1960
376bd9cb
DG
1961struct selinux_audit_rule {
1962 u32 au_seqno;
1963 struct context au_ctxt;
1964};
1965
1966void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1967{
1968 if (rule) {
1969 context_destroy(&rule->au_ctxt);
1970 kfree(rule);
1971 }
1972}
1973
1974int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1975 struct selinux_audit_rule **rule)
1976{
1977 struct selinux_audit_rule *tmprule;
1978 struct role_datum *roledatum;
1979 struct type_datum *typedatum;
1980 struct user_datum *userdatum;
1981 int rc = 0;
1982
1983 *rule = NULL;
1984
1985 if (!ss_initialized)
1986 return -ENOTSUPP;
1987
1988 switch (field) {
3a6b9f85
DG
1989 case AUDIT_SUBJ_USER:
1990 case AUDIT_SUBJ_ROLE:
1991 case AUDIT_SUBJ_TYPE:
6e5a2d1d
DG
1992 case AUDIT_OBJ_USER:
1993 case AUDIT_OBJ_ROLE:
1994 case AUDIT_OBJ_TYPE:
376bd9cb
DG
1995 /* only 'equals' and 'not equals' fit user, role, and type */
1996 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1997 return -EINVAL;
1998 break;
3a6b9f85
DG
1999 case AUDIT_SUBJ_SEN:
2000 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
2001 case AUDIT_OBJ_LEV_LOW:
2002 case AUDIT_OBJ_LEV_HIGH:
376bd9cb
DG
2003 /* we do not allow a range, indicated by the presense of '-' */
2004 if (strchr(rulestr, '-'))
2005 return -EINVAL;
2006 break;
2007 default:
2008 /* only the above fields are valid */
2009 return -EINVAL;
2010 }
2011
2012 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2013 if (!tmprule)
2014 return -ENOMEM;
2015
2016 context_init(&tmprule->au_ctxt);
2017
2018 POLICY_RDLOCK;
2019
2020 tmprule->au_seqno = latest_granting;
2021
2022 switch (field) {
3a6b9f85 2023 case AUDIT_SUBJ_USER:
6e5a2d1d 2024 case AUDIT_OBJ_USER:
376bd9cb
DG
2025 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2026 if (!userdatum)
2027 rc = -EINVAL;
2028 else
2029 tmprule->au_ctxt.user = userdatum->value;
2030 break;
3a6b9f85 2031 case AUDIT_SUBJ_ROLE:
6e5a2d1d 2032 case AUDIT_OBJ_ROLE:
376bd9cb
DG
2033 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2034 if (!roledatum)
2035 rc = -EINVAL;
2036 else
2037 tmprule->au_ctxt.role = roledatum->value;
2038 break;
3a6b9f85 2039 case AUDIT_SUBJ_TYPE:
6e5a2d1d 2040 case AUDIT_OBJ_TYPE:
376bd9cb
DG
2041 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2042 if (!typedatum)
2043 rc = -EINVAL;
2044 else
2045 tmprule->au_ctxt.type = typedatum->value;
2046 break;
3a6b9f85
DG
2047 case AUDIT_SUBJ_SEN:
2048 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
2049 case AUDIT_OBJ_LEV_LOW:
2050 case AUDIT_OBJ_LEV_HIGH:
376bd9cb
DG
2051 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2052 break;
2053 }
2054
2055 POLICY_RDUNLOCK;
2056
2057 if (rc) {
2058 selinux_audit_rule_free(tmprule);
2059 tmprule = NULL;
2060 }
2061
2062 *rule = tmprule;
2063
2064 return rc;
2065}
2066
9a2f44f0 2067int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
376bd9cb
DG
2068 struct selinux_audit_rule *rule,
2069 struct audit_context *actx)
2070{
2071 struct context *ctxt;
2072 struct mls_level *level;
2073 int match = 0;
2074
2075 if (!rule) {
2076 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2077 "selinux_audit_rule_match: missing rule\n");
2078 return -ENOENT;
2079 }
2080
2081 POLICY_RDLOCK;
2082
2083 if (rule->au_seqno < latest_granting) {
2084 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2085 "selinux_audit_rule_match: stale rule\n");
2086 match = -ESTALE;
2087 goto out;
2088 }
2089
9a2f44f0 2090 ctxt = sidtab_search(&sidtab, sid);
376bd9cb
DG
2091 if (!ctxt) {
2092 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2093 "selinux_audit_rule_match: unrecognized SID %d\n",
9a2f44f0 2094 sid);
376bd9cb
DG
2095 match = -ENOENT;
2096 goto out;
2097 }
2098
2099 /* a field/op pair that is not caught here will simply fall through
2100 without a match */
2101 switch (field) {
3a6b9f85 2102 case AUDIT_SUBJ_USER:
6e5a2d1d 2103 case AUDIT_OBJ_USER:
376bd9cb
DG
2104 switch (op) {
2105 case AUDIT_EQUAL:
2106 match = (ctxt->user == rule->au_ctxt.user);
2107 break;
2108 case AUDIT_NOT_EQUAL:
2109 match = (ctxt->user != rule->au_ctxt.user);
2110 break;
2111 }
2112 break;
3a6b9f85 2113 case AUDIT_SUBJ_ROLE:
6e5a2d1d 2114 case AUDIT_OBJ_ROLE:
376bd9cb
DG
2115 switch (op) {
2116 case AUDIT_EQUAL:
2117 match = (ctxt->role == rule->au_ctxt.role);
2118 break;
2119 case AUDIT_NOT_EQUAL:
2120 match = (ctxt->role != rule->au_ctxt.role);
2121 break;
2122 }
2123 break;
3a6b9f85 2124 case AUDIT_SUBJ_TYPE:
6e5a2d1d 2125 case AUDIT_OBJ_TYPE:
376bd9cb
DG
2126 switch (op) {
2127 case AUDIT_EQUAL:
2128 match = (ctxt->type == rule->au_ctxt.type);
2129 break;
2130 case AUDIT_NOT_EQUAL:
2131 match = (ctxt->type != rule->au_ctxt.type);
2132 break;
2133 }
2134 break;
3a6b9f85
DG
2135 case AUDIT_SUBJ_SEN:
2136 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
2137 case AUDIT_OBJ_LEV_LOW:
2138 case AUDIT_OBJ_LEV_HIGH:
2139 level = ((field == AUDIT_SUBJ_SEN ||
2140 field == AUDIT_OBJ_LEV_LOW) ?
376bd9cb
DG
2141 &ctxt->range.level[0] : &ctxt->range.level[1]);
2142 switch (op) {
2143 case AUDIT_EQUAL:
2144 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2145 level);
2146 break;
2147 case AUDIT_NOT_EQUAL:
2148 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2149 level);
2150 break;
2151 case AUDIT_LESS_THAN:
2152 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2153 level) &&
2154 !mls_level_eq(&rule->au_ctxt.range.level[0],
2155 level));
2156 break;
2157 case AUDIT_LESS_THAN_OR_EQUAL:
2158 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2159 level);
2160 break;
2161 case AUDIT_GREATER_THAN:
2162 match = (mls_level_dom(level,
2163 &rule->au_ctxt.range.level[0]) &&
2164 !mls_level_eq(level,
2165 &rule->au_ctxt.range.level[0]));
2166 break;
2167 case AUDIT_GREATER_THAN_OR_EQUAL:
2168 match = mls_level_dom(level,
2169 &rule->au_ctxt.range.level[0]);
2170 break;
2171 }
2172 }
2173
2174out:
2175 POLICY_RDUNLOCK;
2176 return match;
2177}
2178
2179static int (*aurule_callback)(void) = NULL;
2180
2181static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2182 u16 class, u32 perms, u32 *retained)
2183{
2184 int err = 0;
2185
2186 if (event == AVC_CALLBACK_RESET && aurule_callback)
2187 err = aurule_callback();
2188 return err;
2189}
2190
2191static int __init aurule_init(void)
2192{
2193 int err;
2194
2195 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2196 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2197 if (err)
2198 panic("avc_add_callback() failed, error %d\n", err);
2199
2200 return err;
2201}
2202__initcall(aurule_init);
2203
2204void selinux_audit_set_callback(int (*callback)(void))
2205{
2206 aurule_callback = callback;
2207}
7420ed23
VY
2208
2209#ifdef CONFIG_NETLABEL
2210/*
5778eabd 2211 * NetLabel cache structure
7420ed23 2212 */
5778eabd 2213#define NETLBL_CACHE(x) ((struct selinux_netlbl_cache *)(x))
7420ed23
VY
2214#define NETLBL_CACHE_T_NONE 0
2215#define NETLBL_CACHE_T_SID 1
2216#define NETLBL_CACHE_T_MLS 2
5778eabd 2217struct selinux_netlbl_cache {
7420ed23
VY
2218 u32 type;
2219 union {
2220 u32 sid;
2221 struct mls_range mls_label;
2222 } data;
2223};
2224
2225/**
5778eabd 2226 * security_netlbl_cache_free - Free the NetLabel cached data
7420ed23
VY
2227 * @data: the data to free
2228 *
2229 * Description:
2230 * This function is intended to be used as the free() callback inside the
2231 * netlbl_lsm_cache structure.
2232 *
2233 */
5778eabd 2234static void security_netlbl_cache_free(const void *data)
7420ed23 2235{
5778eabd 2236 struct selinux_netlbl_cache *cache;
ffb733c6 2237
2238 if (data == NULL)
2239 return;
2240
2241 cache = NETLBL_CACHE(data);
7420ed23
VY
2242 switch (cache->type) {
2243 case NETLBL_CACHE_T_MLS:
2244 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2245 break;
2246 }
2247 kfree(data);
2248}
2249
2250/**
5778eabd
PM
2251 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2252 * @secattr: the NetLabel packet security attributes
7420ed23
VY
2253 * @ctx: the SELinux context
2254 *
2255 * Description:
2256 * Attempt to cache the context in @ctx, which was derived from the packet in
5778eabd
PM
2257 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2258 * already been initialized.
7420ed23
VY
2259 *
2260 */
5778eabd
PM
2261static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2262 struct context *ctx)
7420ed23 2263{
5778eabd 2264 struct selinux_netlbl_cache *cache = NULL;
7420ed23 2265
5778eabd
PM
2266 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2267 if (secattr->cache == NULL)
2268 return;
7420ed23
VY
2269
2270 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2271 if (cache == NULL)
5778eabd 2272 return;
7420ed23
VY
2273
2274 cache->type = NETLBL_CACHE_T_MLS;
2275 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2276 &ctx->range.level[0].cat) != 0)
5778eabd 2277 return;
7420ed23
VY
2278 cache->data.mls_label.level[1].cat.highbit =
2279 cache->data.mls_label.level[0].cat.highbit;
2280 cache->data.mls_label.level[1].cat.node =
2281 cache->data.mls_label.level[0].cat.node;
2282 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2283 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2284
5778eabd
PM
2285 secattr->cache->free = security_netlbl_cache_free;
2286 secattr->cache->data = (void *)cache;
2287 secattr->flags |= NETLBL_SECATTR_CACHE;
7420ed23
VY
2288}
2289
2290/**
5778eabd 2291 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
7420ed23
VY
2292 * @secattr: the NetLabel packet security attributes
2293 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2294 * @sid: the SELinux SID
2295 *
2296 * Description:
5778eabd 2297 * Convert the given NetLabel security attributes in @secattr into a
7420ed23 2298 * SELinux SID. If the @secattr field does not contain a full SELinux
5778eabd
PM
2299 * SID/context then use the context in @base_sid as the foundation. If
2300 * possibile the 'cache' field of @secattr is set and the CACHE flag is set;
2301 * this is to allow the @secattr to be used by NetLabel to cache the secattr to
2302 * SID conversion for future lookups. Returns zero on success, negative
2303 * values on failure.
7420ed23
VY
2304 *
2305 */
5778eabd
PM
2306int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2307 u32 base_sid,
2308 u32 *sid)
7420ed23
VY
2309{
2310 int rc = -EIDRM;
2311 struct context *ctx;
2312 struct context ctx_new;
5778eabd
PM
2313 struct selinux_netlbl_cache *cache;
2314
2315 if (!ss_initialized) {
2316 *sid = SECSID_NULL;
2317 return 0;
2318 }
7420ed23
VY
2319
2320 POLICY_RDLOCK;
2321
701a90ba 2322 if (secattr->flags & NETLBL_SECATTR_CACHE) {
ffb733c6 2323 cache = NETLBL_CACHE(secattr->cache->data);
7420ed23
VY
2324 switch (cache->type) {
2325 case NETLBL_CACHE_T_SID:
2326 *sid = cache->data.sid;
2327 rc = 0;
2328 break;
2329 case NETLBL_CACHE_T_MLS:
2330 ctx = sidtab_search(&sidtab, base_sid);
2331 if (ctx == NULL)
2332 goto netlbl_secattr_to_sid_return;
2333
2334 ctx_new.user = ctx->user;
2335 ctx_new.role = ctx->role;
2336 ctx_new.type = ctx->type;
2337 ctx_new.range.level[0].sens =
2338 cache->data.mls_label.level[0].sens;
2339 ctx_new.range.level[0].cat.highbit =
2340 cache->data.mls_label.level[0].cat.highbit;
2341 ctx_new.range.level[0].cat.node =
2342 cache->data.mls_label.level[0].cat.node;
2343 ctx_new.range.level[1].sens =
2344 cache->data.mls_label.level[1].sens;
2345 ctx_new.range.level[1].cat.highbit =
2346 cache->data.mls_label.level[1].cat.highbit;
2347 ctx_new.range.level[1].cat.node =
2348 cache->data.mls_label.level[1].cat.node;
2349
2350 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2351 break;
2352 default:
2353 goto netlbl_secattr_to_sid_return;
2354 }
701a90ba 2355 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
7420ed23
VY
2356 ctx = sidtab_search(&sidtab, base_sid);
2357 if (ctx == NULL)
2358 goto netlbl_secattr_to_sid_return;
2359
2360 ctx_new.user = ctx->user;
2361 ctx_new.role = ctx->role;
2362 ctx_new.type = ctx->type;
02752760 2363 mls_import_netlbl_lvl(&ctx_new, secattr);
701a90ba 2364 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
02752760
PM
2365 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2366 secattr->mls_cat) != 0)
7420ed23
VY
2367 goto netlbl_secattr_to_sid_return;
2368 ctx_new.range.level[1].cat.highbit =
2369 ctx_new.range.level[0].cat.highbit;
2370 ctx_new.range.level[1].cat.node =
2371 ctx_new.range.level[0].cat.node;
2372 } else {
2373 ebitmap_init(&ctx_new.range.level[0].cat);
2374 ebitmap_init(&ctx_new.range.level[1].cat);
2375 }
2376 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2377 goto netlbl_secattr_to_sid_return_cleanup;
2378
2379 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2380 if (rc != 0)
2381 goto netlbl_secattr_to_sid_return_cleanup;
2382
5778eabd
PM
2383 security_netlbl_cache_add(secattr, &ctx_new);
2384
7420ed23
VY
2385 ebitmap_destroy(&ctx_new.range.level[0].cat);
2386 } else {
388b2405 2387 *sid = SECSID_NULL;
7420ed23
VY
2388 rc = 0;
2389 }
2390
2391netlbl_secattr_to_sid_return:
2392 POLICY_RDUNLOCK;
2393 return rc;
2394netlbl_secattr_to_sid_return_cleanup:
2395 ebitmap_destroy(&ctx_new.range.level[0].cat);
2396 goto netlbl_secattr_to_sid_return;
2397}
2398
2399/**
5778eabd
PM
2400 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2401 * @sid: the SELinux SID
2402 * @secattr: the NetLabel packet security attributes
7420ed23
VY
2403 *
2404 * Description:
5778eabd
PM
2405 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2406 * Returns zero on success, negative values on failure.
7420ed23
VY
2407 *
2408 */
5778eabd 2409int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
7420ed23
VY
2410{
2411 int rc = -ENOENT;
7420ed23
VY
2412 struct context *ctx;
2413
5778eabd
PM
2414 netlbl_secattr_init(secattr);
2415
7420ed23
VY
2416 if (!ss_initialized)
2417 return 0;
2418
2419 POLICY_RDLOCK;
7420ed23
VY
2420 ctx = sidtab_search(&sidtab, sid);
2421 if (ctx == NULL)
5778eabd
PM
2422 goto netlbl_sid_to_secattr_failure;
2423 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2424 GFP_ATOMIC);
2425 secattr->flags |= NETLBL_SECATTR_DOMAIN;
2426 mls_export_netlbl_lvl(ctx, secattr);
2427 rc = mls_export_netlbl_cat(ctx, secattr);
bf0edf39 2428 if (rc != 0)
5778eabd 2429 goto netlbl_sid_to_secattr_failure;
7420ed23 2430 POLICY_RDUNLOCK;
99f59ed0 2431
5778eabd 2432 return 0;
f8687afe 2433
5778eabd
PM
2434netlbl_sid_to_secattr_failure:
2435 POLICY_RDUNLOCK;
2436 netlbl_secattr_destroy(secattr);
f8687afe
PM
2437 return rc;
2438}
7420ed23 2439#endif /* CONFIG_NETLABEL */
This page took 0.341924 seconds and 5 git commands to generate.