selinux: remove userland security class and permission definitions
[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
596/**
597 * security_sid_to_context - Obtain a context for a given SID.
598 * @sid: security identifier, SID
599 * @scontext: security context
600 * @scontext_len: length in bytes
601 *
602 * Write the string representation of the context associated with @sid
603 * into a dynamically allocated string of the correct size. Set @scontext
604 * to point to this string and set @scontext_len to the length of the string.
605 */
606int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
607{
608 struct context *context;
609 int rc = 0;
610
4f4acf3a
SS
611 *scontext = NULL;
612 *scontext_len = 0;
613
1da177e4
LT
614 if (!ss_initialized) {
615 if (sid <= SECINITSID_NUM) {
616 char *scontextp;
617
618 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
619 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
0cccca06
SH
620 if (!scontextp) {
621 rc = -ENOMEM;
622 goto out;
623 }
1da177e4
LT
624 strcpy(scontextp, initial_sid_to_string[sid]);
625 *scontext = scontextp;
626 goto out;
627 }
628 printk(KERN_ERR "security_sid_to_context: called before initial "
629 "load_policy on unknown SID %d\n", sid);
630 rc = -EINVAL;
631 goto out;
632 }
633 POLICY_RDLOCK;
634 context = sidtab_search(&sidtab, sid);
635 if (!context) {
636 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
637 "%d\n", sid);
638 rc = -EINVAL;
639 goto out_unlock;
640 }
641 rc = context_struct_to_string(context, scontext, scontext_len);
642out_unlock:
643 POLICY_RDUNLOCK;
644out:
645 return rc;
646
647}
648
f5c1d5b2 649static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
1da177e4
LT
650{
651 char *scontext2;
652 struct context context;
653 struct role_datum *role;
654 struct type_datum *typdatum;
655 struct user_datum *usrdatum;
656 char *scontextp, *p, oldc;
657 int rc = 0;
658
659 if (!ss_initialized) {
660 int i;
661
662 for (i = 1; i < SECINITSID_NUM; i++) {
663 if (!strcmp(initial_sid_to_string[i], scontext)) {
664 *sid = i;
665 goto out;
666 }
667 }
668 *sid = SECINITSID_KERNEL;
669 goto out;
670 }
671 *sid = SECSID_NULL;
672
673 /* Copy the string so that we can modify the copy as we parse it.
674 The string should already by null terminated, but we append a
675 null suffix to the copy to avoid problems with the existing
676 attr package, which doesn't view the null terminator as part
677 of the attribute value. */
678 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
679 if (!scontext2) {
680 rc = -ENOMEM;
681 goto out;
682 }
683 memcpy(scontext2, scontext, scontext_len);
684 scontext2[scontext_len] = 0;
685
686 context_init(&context);
687 *sid = SECSID_NULL;
688
689 POLICY_RDLOCK;
690
691 /* Parse the security context. */
692
693 rc = -EINVAL;
694 scontextp = (char *) scontext2;
695
696 /* Extract the user. */
697 p = scontextp;
698 while (*p && *p != ':')
699 p++;
700
701 if (*p == 0)
702 goto out_unlock;
703
704 *p++ = 0;
705
706 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
707 if (!usrdatum)
708 goto out_unlock;
709
710 context.user = usrdatum->value;
711
712 /* Extract role. */
713 scontextp = p;
714 while (*p && *p != ':')
715 p++;
716
717 if (*p == 0)
718 goto out_unlock;
719
720 *p++ = 0;
721
722 role = hashtab_search(policydb.p_roles.table, scontextp);
723 if (!role)
724 goto out_unlock;
725 context.role = role->value;
726
727 /* Extract type. */
728 scontextp = p;
729 while (*p && *p != ':')
730 p++;
731 oldc = *p;
732 *p++ = 0;
733
734 typdatum = hashtab_search(policydb.p_types.table, scontextp);
735 if (!typdatum)
736 goto out_unlock;
737
738 context.type = typdatum->value;
739
f5c1d5b2 740 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
1da177e4
LT
741 if (rc)
742 goto out_unlock;
743
744 if ((p - scontext2) < scontext_len) {
745 rc = -EINVAL;
746 goto out_unlock;
747 }
748
749 /* Check the validity of the new context. */
750 if (!policydb_context_isvalid(&policydb, &context)) {
751 rc = -EINVAL;
752 goto out_unlock;
753 }
754 /* Obtain the new sid. */
755 rc = sidtab_context_to_sid(&sidtab, &context, sid);
756out_unlock:
757 POLICY_RDUNLOCK;
758 context_destroy(&context);
759 kfree(scontext2);
760out:
761 return rc;
762}
763
f5c1d5b2
JM
764/**
765 * security_context_to_sid - Obtain a SID for a given security context.
766 * @scontext: security context
767 * @scontext_len: length in bytes
768 * @sid: security identifier, SID
769 *
770 * Obtains a SID associated with the security context that
771 * has the string representation specified by @scontext.
772 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
773 * memory is available, or 0 on success.
774 */
775int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
776{
777 return security_context_to_sid_core(scontext, scontext_len,
778 sid, SECSID_NULL);
779}
780
781/**
782 * security_context_to_sid_default - Obtain a SID for a given security context,
783 * falling back to specified default if needed.
784 *
785 * @scontext: security context
786 * @scontext_len: length in bytes
787 * @sid: security identifier, SID
788 * @def_sid: default SID to assign on errror
789 *
790 * Obtains a SID associated with the security context that
791 * has the string representation specified by @scontext.
792 * The default SID is passed to the MLS layer to be used to allow
793 * kernel labeling of the MLS field if the MLS field is not present
794 * (for upgrading to MLS without full relabel).
795 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
796 * memory is available, or 0 on success.
797 */
798int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
799{
800 return security_context_to_sid_core(scontext, scontext_len,
801 sid, def_sid);
802}
803
1da177e4
LT
804static int compute_sid_handle_invalid_context(
805 struct context *scontext,
806 struct context *tcontext,
807 u16 tclass,
808 struct context *newcontext)
809{
810 char *s = NULL, *t = NULL, *n = NULL;
811 u32 slen, tlen, nlen;
812
813 if (context_struct_to_string(scontext, &s, &slen) < 0)
814 goto out;
815 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
816 goto out;
817 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
818 goto out;
9ad9ad38 819 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1da177e4
LT
820 "security_compute_sid: invalid context %s"
821 " for scontext=%s"
822 " tcontext=%s"
823 " tclass=%s",
824 n, s, t, policydb.p_class_val_to_name[tclass-1]);
825out:
826 kfree(s);
827 kfree(t);
828 kfree(n);
829 if (!selinux_enforcing)
830 return 0;
831 return -EACCES;
832}
833
834static int security_compute_sid(u32 ssid,
835 u32 tsid,
836 u16 tclass,
837 u32 specified,
838 u32 *out_sid)
839{
840 struct context *scontext = NULL, *tcontext = NULL, newcontext;
841 struct role_trans *roletr = NULL;
842 struct avtab_key avkey;
843 struct avtab_datum *avdatum;
844 struct avtab_node *node;
1da177e4
LT
845 int rc = 0;
846
847 if (!ss_initialized) {
848 switch (tclass) {
849 case SECCLASS_PROCESS:
850 *out_sid = ssid;
851 break;
852 default:
853 *out_sid = tsid;
854 break;
855 }
856 goto out;
857 }
858
851f8a69
VY
859 context_init(&newcontext);
860
1da177e4
LT
861 POLICY_RDLOCK;
862
863 scontext = sidtab_search(&sidtab, ssid);
864 if (!scontext) {
865 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
866 ssid);
867 rc = -EINVAL;
868 goto out_unlock;
869 }
870 tcontext = sidtab_search(&sidtab, tsid);
871 if (!tcontext) {
872 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
873 tsid);
874 rc = -EINVAL;
875 goto out_unlock;
876 }
877
1da177e4
LT
878 /* Set the user identity. */
879 switch (specified) {
880 case AVTAB_TRANSITION:
881 case AVTAB_CHANGE:
882 /* Use the process user identity. */
883 newcontext.user = scontext->user;
884 break;
885 case AVTAB_MEMBER:
886 /* Use the related object owner. */
887 newcontext.user = tcontext->user;
888 break;
889 }
890
891 /* Set the role and type to default values. */
892 switch (tclass) {
893 case SECCLASS_PROCESS:
894 /* Use the current role and type of process. */
895 newcontext.role = scontext->role;
896 newcontext.type = scontext->type;
897 break;
898 default:
899 /* Use the well-defined object role. */
900 newcontext.role = OBJECT_R_VAL;
901 /* Use the type of the related object. */
902 newcontext.type = tcontext->type;
903 }
904
905 /* Look for a type transition/member/change rule. */
906 avkey.source_type = scontext->type;
907 avkey.target_type = tcontext->type;
908 avkey.target_class = tclass;
782ebb99
SS
909 avkey.specified = specified;
910 avdatum = avtab_search(&policydb.te_avtab, &avkey);
1da177e4
LT
911
912 /* If no permanent rule, also check for enabled conditional rules */
913 if(!avdatum) {
782ebb99 914 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
1da177e4 915 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
782ebb99 916 if (node->key.specified & AVTAB_ENABLED) {
1da177e4
LT
917 avdatum = &node->datum;
918 break;
919 }
920 }
921 }
922
782ebb99 923 if (avdatum) {
1da177e4 924 /* Use the type from the type transition/member/change rule. */
782ebb99 925 newcontext.type = avdatum->data;
1da177e4
LT
926 }
927
928 /* Check for class-specific changes. */
929 switch (tclass) {
930 case SECCLASS_PROCESS:
931 if (specified & AVTAB_TRANSITION) {
932 /* Look for a role transition rule. */
933 for (roletr = policydb.role_tr; roletr;
934 roletr = roletr->next) {
935 if (roletr->role == scontext->role &&
936 roletr->type == tcontext->type) {
937 /* Use the role transition rule. */
938 newcontext.role = roletr->new_role;
939 break;
940 }
941 }
942 }
943 break;
944 default:
945 break;
946 }
947
948 /* Set the MLS attributes.
949 This is done last because it may allocate memory. */
950 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
951 if (rc)
952 goto out_unlock;
953
954 /* Check the validity of the context. */
955 if (!policydb_context_isvalid(&policydb, &newcontext)) {
956 rc = compute_sid_handle_invalid_context(scontext,
957 tcontext,
958 tclass,
959 &newcontext);
960 if (rc)
961 goto out_unlock;
962 }
963 /* Obtain the sid for the context. */
964 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
965out_unlock:
966 POLICY_RDUNLOCK;
967 context_destroy(&newcontext);
968out:
969 return rc;
970}
971
972/**
973 * security_transition_sid - Compute the SID for a new subject/object.
974 * @ssid: source security identifier
975 * @tsid: target security identifier
976 * @tclass: target security class
977 * @out_sid: security identifier for new subject/object
978 *
979 * Compute a SID to use for labeling a new subject or object in the
980 * class @tclass based on a SID pair (@ssid, @tsid).
981 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
982 * if insufficient memory is available, or %0 if the new SID was
983 * computed successfully.
984 */
985int security_transition_sid(u32 ssid,
986 u32 tsid,
987 u16 tclass,
988 u32 *out_sid)
989{
990 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
991}
992
993/**
994 * security_member_sid - Compute the SID for member selection.
995 * @ssid: source security identifier
996 * @tsid: target security identifier
997 * @tclass: target security class
998 * @out_sid: security identifier for selected member
999 *
1000 * Compute a SID to use when selecting a member of a polyinstantiated
1001 * object of class @tclass based on a SID pair (@ssid, @tsid).
1002 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1003 * if insufficient memory is available, or %0 if the SID was
1004 * computed successfully.
1005 */
1006int security_member_sid(u32 ssid,
1007 u32 tsid,
1008 u16 tclass,
1009 u32 *out_sid)
1010{
1011 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1012}
1013
1014/**
1015 * security_change_sid - Compute the SID for object relabeling.
1016 * @ssid: source security identifier
1017 * @tsid: target security identifier
1018 * @tclass: target security class
1019 * @out_sid: security identifier for selected member
1020 *
1021 * Compute a SID to use for relabeling an object of class @tclass
1022 * based on a SID pair (@ssid, @tsid).
1023 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1024 * if insufficient memory is available, or %0 if the SID was
1025 * computed successfully.
1026 */
1027int security_change_sid(u32 ssid,
1028 u32 tsid,
1029 u16 tclass,
1030 u32 *out_sid)
1031{
1032 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1033}
1034
b94c7e67
CS
1035/*
1036 * Verify that each kernel class that is defined in the
1037 * policy is correct
1038 */
1039static int validate_classes(struct policydb *p)
1040{
1041 int i, j;
1042 struct class_datum *cladatum;
1043 struct perm_datum *perdatum;
1044 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1045 u16 class_val;
1046 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1047 const char *def_class, *def_perm, *pol_class;
1048 struct symtab *perms;
1049
1050 for (i = 1; i < kdefs->cts_len; i++) {
1051 def_class = kdefs->class_to_string[i];
a764ae4b
SS
1052 if (!def_class)
1053 continue;
b94c7e67
CS
1054 if (i > p->p_classes.nprim) {
1055 printk(KERN_INFO
1056 "security: class %s not defined in policy\n",
1057 def_class);
1058 continue;
1059 }
1060 pol_class = p->p_class_val_to_name[i-1];
1061 if (strcmp(pol_class, def_class)) {
1062 printk(KERN_ERR
1063 "security: class %d is incorrect, found %s but should be %s\n",
1064 i, pol_class, def_class);
1065 return -EINVAL;
1066 }
1067 }
1068 for (i = 0; i < kdefs->av_pts_len; i++) {
1069 class_val = kdefs->av_perm_to_string[i].tclass;
1070 perm_val = kdefs->av_perm_to_string[i].value;
1071 def_perm = kdefs->av_perm_to_string[i].name;
1072 if (class_val > p->p_classes.nprim)
1073 continue;
1074 pol_class = p->p_class_val_to_name[class_val-1];
1075 cladatum = hashtab_search(p->p_classes.table, pol_class);
1076 BUG_ON(!cladatum);
1077 perms = &cladatum->permissions;
1078 nprim = 1 << (perms->nprim - 1);
1079 if (perm_val > nprim) {
1080 printk(KERN_INFO
1081 "security: permission %s in class %s not defined in policy\n",
1082 def_perm, pol_class);
1083 continue;
1084 }
1085 perdatum = hashtab_search(perms->table, def_perm);
1086 if (perdatum == NULL) {
1087 printk(KERN_ERR
1088 "security: permission %s in class %s not found in policy\n",
1089 def_perm, pol_class);
1090 return -EINVAL;
1091 }
1092 pol_val = 1 << (perdatum->value - 1);
1093 if (pol_val != perm_val) {
1094 printk(KERN_ERR
1095 "security: permission %s in class %s has incorrect value\n",
1096 def_perm, pol_class);
1097 return -EINVAL;
1098 }
1099 }
1100 for (i = 0; i < kdefs->av_inherit_len; i++) {
1101 class_val = kdefs->av_inherit[i].tclass;
1102 if (class_val > p->p_classes.nprim)
1103 continue;
1104 pol_class = p->p_class_val_to_name[class_val-1];
1105 cladatum = hashtab_search(p->p_classes.table, pol_class);
1106 BUG_ON(!cladatum);
1107 if (!cladatum->comdatum) {
1108 printk(KERN_ERR
1109 "security: class %s should have an inherits clause but does not\n",
1110 pol_class);
1111 return -EINVAL;
1112 }
1113 tmp = kdefs->av_inherit[i].common_base;
1114 common_pts_len = 0;
1115 while (!(tmp & 0x01)) {
1116 common_pts_len++;
1117 tmp >>= 1;
1118 }
1119 perms = &cladatum->comdatum->permissions;
1120 for (j = 0; j < common_pts_len; j++) {
1121 def_perm = kdefs->av_inherit[i].common_pts[j];
1122 if (j >= perms->nprim) {
1123 printk(KERN_INFO
1124 "security: permission %s in class %s not defined in policy\n",
1125 def_perm, pol_class);
1126 continue;
1127 }
1128 perdatum = hashtab_search(perms->table, def_perm);
1129 if (perdatum == NULL) {
1130 printk(KERN_ERR
1131 "security: permission %s in class %s not found in policy\n",
1132 def_perm, pol_class);
1133 return -EINVAL;
1134 }
1135 if (perdatum->value != j + 1) {
1136 printk(KERN_ERR
1137 "security: permission %s in class %s has incorrect value\n",
1138 def_perm, pol_class);
1139 return -EINVAL;
1140 }
1141 }
1142 }
1143 return 0;
1144}
1145
1da177e4
LT
1146/* Clone the SID into the new SID table. */
1147static int clone_sid(u32 sid,
1148 struct context *context,
1149 void *arg)
1150{
1151 struct sidtab *s = arg;
1152
1153 return sidtab_insert(s, sid, context);
1154}
1155
1156static inline int convert_context_handle_invalid_context(struct context *context)
1157{
1158 int rc = 0;
1159
1160 if (selinux_enforcing) {
1161 rc = -EINVAL;
1162 } else {
1163 char *s;
1164 u32 len;
1165
1166 context_struct_to_string(context, &s, &len);
1167 printk(KERN_ERR "security: context %s is invalid\n", s);
1168 kfree(s);
1169 }
1170 return rc;
1171}
1172
1173struct convert_context_args {
1174 struct policydb *oldp;
1175 struct policydb *newp;
1176};
1177
1178/*
1179 * Convert the values in the security context
1180 * structure `c' from the values specified
1181 * in the policy `p->oldp' to the values specified
1182 * in the policy `p->newp'. Verify that the
1183 * context is valid under the new policy.
1184 */
1185static int convert_context(u32 key,
1186 struct context *c,
1187 void *p)
1188{
1189 struct convert_context_args *args;
1190 struct context oldc;
1191 struct role_datum *role;
1192 struct type_datum *typdatum;
1193 struct user_datum *usrdatum;
1194 char *s;
1195 u32 len;
1196 int rc;
1197
1198 args = p;
1199
1200 rc = context_cpy(&oldc, c);
1201 if (rc)
1202 goto out;
1203
1204 rc = -EINVAL;
1205
1206 /* Convert the user. */
1207 usrdatum = hashtab_search(args->newp->p_users.table,
1208 args->oldp->p_user_val_to_name[c->user - 1]);
1209 if (!usrdatum) {
1210 goto bad;
1211 }
1212 c->user = usrdatum->value;
1213
1214 /* Convert the role. */
1215 role = hashtab_search(args->newp->p_roles.table,
1216 args->oldp->p_role_val_to_name[c->role - 1]);
1217 if (!role) {
1218 goto bad;
1219 }
1220 c->role = role->value;
1221
1222 /* Convert the type. */
1223 typdatum = hashtab_search(args->newp->p_types.table,
1224 args->oldp->p_type_val_to_name[c->type - 1]);
1225 if (!typdatum) {
1226 goto bad;
1227 }
1228 c->type = typdatum->value;
1229
1230 rc = mls_convert_context(args->oldp, args->newp, c);
1231 if (rc)
1232 goto bad;
1233
1234 /* Check the validity of the new context. */
1235 if (!policydb_context_isvalid(args->newp, c)) {
1236 rc = convert_context_handle_invalid_context(&oldc);
1237 if (rc)
1238 goto bad;
1239 }
1240
1241 context_destroy(&oldc);
1242out:
1243 return rc;
1244bad:
1245 context_struct_to_string(&oldc, &s, &len);
1246 context_destroy(&oldc);
1247 printk(KERN_ERR "security: invalidating context %s\n", s);
1248 kfree(s);
1249 goto out;
1250}
1251
1252extern void selinux_complete_init(void);
1253
1254/**
1255 * security_load_policy - Load a security policy configuration.
1256 * @data: binary policy data
1257 * @len: length of data in bytes
1258 *
1259 * Load a new set of security policy configuration data,
1260 * validate it and convert the SID table as necessary.
1261 * This function will flush the access vector cache after
1262 * loading the new policy.
1263 */
1264int security_load_policy(void *data, size_t len)
1265{
1266 struct policydb oldpolicydb, newpolicydb;
1267 struct sidtab oldsidtab, newsidtab;
1268 struct convert_context_args args;
1269 u32 seqno;
1270 int rc = 0;
1271 struct policy_file file = { data, len }, *fp = &file;
1272
1273 LOAD_LOCK;
1274
1275 if (!ss_initialized) {
1276 avtab_cache_init();
1277 if (policydb_read(&policydb, fp)) {
1278 LOAD_UNLOCK;
1279 avtab_cache_destroy();
1280 return -EINVAL;
1281 }
1282 if (policydb_load_isids(&policydb, &sidtab)) {
1283 LOAD_UNLOCK;
1284 policydb_destroy(&policydb);
1285 avtab_cache_destroy();
1286 return -EINVAL;
1287 }
b94c7e67
CS
1288 /* Verify that the kernel defined classes are correct. */
1289 if (validate_classes(&policydb)) {
1290 printk(KERN_ERR
1291 "security: the definition of a class is incorrect\n");
1292 LOAD_UNLOCK;
1293 sidtab_destroy(&sidtab);
1294 policydb_destroy(&policydb);
1295 avtab_cache_destroy();
1296 return -EINVAL;
1297 }
1da177e4
LT
1298 policydb_loaded_version = policydb.policyvers;
1299 ss_initialized = 1;
4c443d1b 1300 seqno = ++latest_granting;
1da177e4
LT
1301 LOAD_UNLOCK;
1302 selinux_complete_init();
4c443d1b
SS
1303 avc_ss_reset(seqno);
1304 selnl_notify_policyload(seqno);
7420ed23 1305 selinux_netlbl_cache_invalidate();
342a0cff 1306 selinux_xfrm_notify_policyload();
1da177e4
LT
1307 return 0;
1308 }
1309
1310#if 0
1311 sidtab_hash_eval(&sidtab, "sids");
1312#endif
1313
1314 if (policydb_read(&newpolicydb, fp)) {
1315 LOAD_UNLOCK;
1316 return -EINVAL;
1317 }
1318
1319 sidtab_init(&newsidtab);
1320
b94c7e67
CS
1321 /* Verify that the kernel defined classes are correct. */
1322 if (validate_classes(&newpolicydb)) {
1323 printk(KERN_ERR
1324 "security: the definition of a class is incorrect\n");
1325 rc = -EINVAL;
1326 goto err;
1327 }
1328
1da177e4
LT
1329 /* Clone the SID table. */
1330 sidtab_shutdown(&sidtab);
1331 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1332 rc = -ENOMEM;
1333 goto err;
1334 }
1335
1336 /* Convert the internal representations of contexts
1337 in the new SID table and remove invalid SIDs. */
1338 args.oldp = &policydb;
1339 args.newp = &newpolicydb;
1340 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1341
1342 /* Save the old policydb and SID table to free later. */
1343 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1344 sidtab_set(&oldsidtab, &sidtab);
1345
1346 /* Install the new policydb and SID table. */
1347 POLICY_WRLOCK;
1348 memcpy(&policydb, &newpolicydb, sizeof policydb);
1349 sidtab_set(&sidtab, &newsidtab);
1350 seqno = ++latest_granting;
1351 policydb_loaded_version = policydb.policyvers;
1352 POLICY_WRUNLOCK;
1353 LOAD_UNLOCK;
1354
1355 /* Free the old policydb and SID table. */
1356 policydb_destroy(&oldpolicydb);
1357 sidtab_destroy(&oldsidtab);
1358
1359 avc_ss_reset(seqno);
1360 selnl_notify_policyload(seqno);
7420ed23 1361 selinux_netlbl_cache_invalidate();
342a0cff 1362 selinux_xfrm_notify_policyload();
1da177e4
LT
1363
1364 return 0;
1365
1366err:
1367 LOAD_UNLOCK;
1368 sidtab_destroy(&newsidtab);
1369 policydb_destroy(&newpolicydb);
1370 return rc;
1371
1372}
1373
1374/**
1375 * security_port_sid - Obtain the SID for a port.
1376 * @domain: communication domain aka address family
1377 * @type: socket type
1378 * @protocol: protocol number
1379 * @port: port number
1380 * @out_sid: security identifier
1381 */
1382int security_port_sid(u16 domain,
1383 u16 type,
1384 u8 protocol,
1385 u16 port,
1386 u32 *out_sid)
1387{
1388 struct ocontext *c;
1389 int rc = 0;
1390
1391 POLICY_RDLOCK;
1392
1393 c = policydb.ocontexts[OCON_PORT];
1394 while (c) {
1395 if (c->u.port.protocol == protocol &&
1396 c->u.port.low_port <= port &&
1397 c->u.port.high_port >= port)
1398 break;
1399 c = c->next;
1400 }
1401
1402 if (c) {
1403 if (!c->sid[0]) {
1404 rc = sidtab_context_to_sid(&sidtab,
1405 &c->context[0],
1406 &c->sid[0]);
1407 if (rc)
1408 goto out;
1409 }
1410 *out_sid = c->sid[0];
1411 } else {
1412 *out_sid = SECINITSID_PORT;
1413 }
1414
1415out:
1416 POLICY_RDUNLOCK;
1417 return rc;
1418}
1419
1420/**
1421 * security_netif_sid - Obtain the SID for a network interface.
1422 * @name: interface name
1423 * @if_sid: interface SID
1424 * @msg_sid: default SID for received packets
1425 */
1426int security_netif_sid(char *name,
1427 u32 *if_sid,
1428 u32 *msg_sid)
1429{
1430 int rc = 0;
1431 struct ocontext *c;
1432
1433 POLICY_RDLOCK;
1434
1435 c = policydb.ocontexts[OCON_NETIF];
1436 while (c) {
1437 if (strcmp(name, c->u.name) == 0)
1438 break;
1439 c = c->next;
1440 }
1441
1442 if (c) {
1443 if (!c->sid[0] || !c->sid[1]) {
1444 rc = sidtab_context_to_sid(&sidtab,
1445 &c->context[0],
1446 &c->sid[0]);
1447 if (rc)
1448 goto out;
1449 rc = sidtab_context_to_sid(&sidtab,
1450 &c->context[1],
1451 &c->sid[1]);
1452 if (rc)
1453 goto out;
1454 }
1455 *if_sid = c->sid[0];
1456 *msg_sid = c->sid[1];
1457 } else {
1458 *if_sid = SECINITSID_NETIF;
1459 *msg_sid = SECINITSID_NETMSG;
1460 }
1461
1462out:
1463 POLICY_RDUNLOCK;
1464 return rc;
1465}
1466
1467static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1468{
1469 int i, fail = 0;
1470
1471 for(i = 0; i < 4; i++)
1472 if(addr[i] != (input[i] & mask[i])) {
1473 fail = 1;
1474 break;
1475 }
1476
1477 return !fail;
1478}
1479
1480/**
1481 * security_node_sid - Obtain the SID for a node (host).
1482 * @domain: communication domain aka address family
1483 * @addrp: address
1484 * @addrlen: address length in bytes
1485 * @out_sid: security identifier
1486 */
1487int security_node_sid(u16 domain,
1488 void *addrp,
1489 u32 addrlen,
1490 u32 *out_sid)
1491{
1492 int rc = 0;
1493 struct ocontext *c;
1494
1495 POLICY_RDLOCK;
1496
1497 switch (domain) {
1498 case AF_INET: {
1499 u32 addr;
1500
1501 if (addrlen != sizeof(u32)) {
1502 rc = -EINVAL;
1503 goto out;
1504 }
1505
1506 addr = *((u32 *)addrp);
1507
1508 c = policydb.ocontexts[OCON_NODE];
1509 while (c) {
1510 if (c->u.node.addr == (addr & c->u.node.mask))
1511 break;
1512 c = c->next;
1513 }
1514 break;
1515 }
1516
1517 case AF_INET6:
1518 if (addrlen != sizeof(u64) * 2) {
1519 rc = -EINVAL;
1520 goto out;
1521 }
1522 c = policydb.ocontexts[OCON_NODE6];
1523 while (c) {
1524 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1525 c->u.node6.mask))
1526 break;
1527 c = c->next;
1528 }
1529 break;
1530
1531 default:
1532 *out_sid = SECINITSID_NODE;
1533 goto out;
1534 }
1535
1536 if (c) {
1537 if (!c->sid[0]) {
1538 rc = sidtab_context_to_sid(&sidtab,
1539 &c->context[0],
1540 &c->sid[0]);
1541 if (rc)
1542 goto out;
1543 }
1544 *out_sid = c->sid[0];
1545 } else {
1546 *out_sid = SECINITSID_NODE;
1547 }
1548
1549out:
1550 POLICY_RDUNLOCK;
1551 return rc;
1552}
1553
1554#define SIDS_NEL 25
1555
1556/**
1557 * security_get_user_sids - Obtain reachable SIDs for a user.
1558 * @fromsid: starting SID
1559 * @username: username
1560 * @sids: array of reachable SIDs for user
1561 * @nel: number of elements in @sids
1562 *
1563 * Generate the set of SIDs for legal security contexts
1564 * for a given user that can be reached by @fromsid.
1565 * Set *@sids to point to a dynamically allocated
1566 * array containing the set of SIDs. Set *@nel to the
1567 * number of elements in the array.
1568 */
1569
1570int security_get_user_sids(u32 fromsid,
1571 char *username,
1572 u32 **sids,
1573 u32 *nel)
1574{
1575 struct context *fromcon, usercon;
1576 u32 *mysids, *mysids2, sid;
1577 u32 mynel = 0, maxnel = SIDS_NEL;
1578 struct user_datum *user;
1579 struct role_datum *role;
1580 struct av_decision avd;
782ebb99 1581 struct ebitmap_node *rnode, *tnode;
1da177e4
LT
1582 int rc = 0, i, j;
1583
1584 if (!ss_initialized) {
1585 *sids = NULL;
1586 *nel = 0;
1587 goto out;
1588 }
1589
1590 POLICY_RDLOCK;
1591
1592 fromcon = sidtab_search(&sidtab, fromsid);
1593 if (!fromcon) {
1594 rc = -EINVAL;
1595 goto out_unlock;
1596 }
1597
1598 user = hashtab_search(policydb.p_users.table, username);
1599 if (!user) {
1600 rc = -EINVAL;
1601 goto out_unlock;
1602 }
1603 usercon.user = user->value;
1604
89d155ef 1605 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
1da177e4
LT
1606 if (!mysids) {
1607 rc = -ENOMEM;
1608 goto out_unlock;
1609 }
1da177e4 1610
782ebb99
SS
1611 ebitmap_for_each_bit(&user->roles, rnode, i) {
1612 if (!ebitmap_node_get_bit(rnode, i))
1da177e4
LT
1613 continue;
1614 role = policydb.role_val_to_struct[i];
1615 usercon.role = i+1;
782ebb99
SS
1616 ebitmap_for_each_bit(&role->types, tnode, j) {
1617 if (!ebitmap_node_get_bit(tnode, j))
1da177e4
LT
1618 continue;
1619 usercon.type = j+1;
1620
1621 if (mls_setup_user_range(fromcon, user, &usercon))
1622 continue;
1623
1624 rc = context_struct_compute_av(fromcon, &usercon,
1625 SECCLASS_PROCESS,
1626 PROCESS__TRANSITION,
1627 &avd);
1628 if (rc || !(avd.allowed & PROCESS__TRANSITION))
1629 continue;
1630 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
1631 if (rc) {
1632 kfree(mysids);
1633 goto out_unlock;
1634 }
1635 if (mynel < maxnel) {
1636 mysids[mynel++] = sid;
1637 } else {
1638 maxnel += SIDS_NEL;
89d155ef 1639 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
1da177e4
LT
1640 if (!mysids2) {
1641 rc = -ENOMEM;
1642 kfree(mysids);
1643 goto out_unlock;
1644 }
1da177e4
LT
1645 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1646 kfree(mysids);
1647 mysids = mysids2;
1648 mysids[mynel++] = sid;
1649 }
1650 }
1651 }
1652
1653 *sids = mysids;
1654 *nel = mynel;
1655
1656out_unlock:
1657 POLICY_RDUNLOCK;
1658out:
1659 return rc;
1660}
1661
1662/**
1663 * security_genfs_sid - Obtain a SID for a file in a filesystem
1664 * @fstype: filesystem type
1665 * @path: path from root of mount
1666 * @sclass: file security class
1667 * @sid: SID for path
1668 *
1669 * Obtain a SID to use for a file in a filesystem that
1670 * cannot support xattr or use a fixed labeling behavior like
1671 * transition SIDs or task SIDs.
1672 */
1673int security_genfs_sid(const char *fstype,
1674 char *path,
1675 u16 sclass,
1676 u32 *sid)
1677{
1678 int len;
1679 struct genfs *genfs;
1680 struct ocontext *c;
1681 int rc = 0, cmp = 0;
1682
1683 POLICY_RDLOCK;
1684
1685 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1686 cmp = strcmp(fstype, genfs->fstype);
1687 if (cmp <= 0)
1688 break;
1689 }
1690
1691 if (!genfs || cmp) {
1692 *sid = SECINITSID_UNLABELED;
1693 rc = -ENOENT;
1694 goto out;
1695 }
1696
1697 for (c = genfs->head; c; c = c->next) {
1698 len = strlen(c->u.name);
1699 if ((!c->v.sclass || sclass == c->v.sclass) &&
1700 (strncmp(c->u.name, path, len) == 0))
1701 break;
1702 }
1703
1704 if (!c) {
1705 *sid = SECINITSID_UNLABELED;
1706 rc = -ENOENT;
1707 goto out;
1708 }
1709
1710 if (!c->sid[0]) {
1711 rc = sidtab_context_to_sid(&sidtab,
1712 &c->context[0],
1713 &c->sid[0]);
1714 if (rc)
1715 goto out;
1716 }
1717
1718 *sid = c->sid[0];
1719out:
1720 POLICY_RDUNLOCK;
1721 return rc;
1722}
1723
1724/**
1725 * security_fs_use - Determine how to handle labeling for a filesystem.
1726 * @fstype: filesystem type
1727 * @behavior: labeling behavior
1728 * @sid: SID for filesystem (superblock)
1729 */
1730int security_fs_use(
1731 const char *fstype,
1732 unsigned int *behavior,
1733 u32 *sid)
1734{
1735 int rc = 0;
1736 struct ocontext *c;
1737
1738 POLICY_RDLOCK;
1739
1740 c = policydb.ocontexts[OCON_FSUSE];
1741 while (c) {
1742 if (strcmp(fstype, c->u.name) == 0)
1743 break;
1744 c = c->next;
1745 }
1746
1747 if (c) {
1748 *behavior = c->v.behavior;
1749 if (!c->sid[0]) {
1750 rc = sidtab_context_to_sid(&sidtab,
1751 &c->context[0],
1752 &c->sid[0]);
1753 if (rc)
1754 goto out;
1755 }
1756 *sid = c->sid[0];
1757 } else {
1758 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1759 if (rc) {
1760 *behavior = SECURITY_FS_USE_NONE;
1761 rc = 0;
1762 } else {
1763 *behavior = SECURITY_FS_USE_GENFS;
1764 }
1765 }
1766
1767out:
1768 POLICY_RDUNLOCK;
1769 return rc;
1770}
1771
1772int security_get_bools(int *len, char ***names, int **values)
1773{
1774 int i, rc = -ENOMEM;
1775
1776 POLICY_RDLOCK;
1777 *names = NULL;
1778 *values = NULL;
1779
1780 *len = policydb.p_bools.nprim;
1781 if (!*len) {
1782 rc = 0;
1783 goto out;
1784 }
1785
e0795cf4 1786 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
1da177e4
LT
1787 if (!*names)
1788 goto err;
1da177e4 1789
e0795cf4 1790 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
1da177e4
LT
1791 if (!*values)
1792 goto err;
1793
1794 for (i = 0; i < *len; i++) {
1795 size_t name_len;
1796 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1797 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
e0795cf4 1798 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
1da177e4
LT
1799 if (!(*names)[i])
1800 goto err;
1801 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1802 (*names)[i][name_len - 1] = 0;
1803 }
1804 rc = 0;
1805out:
1806 POLICY_RDUNLOCK;
1807 return rc;
1808err:
1809 if (*names) {
1810 for (i = 0; i < *len; i++)
9a5f04bf 1811 kfree((*names)[i]);
1da177e4 1812 }
9a5f04bf 1813 kfree(*values);
1da177e4
LT
1814 goto out;
1815}
1816
1817
1818int security_set_bools(int len, int *values)
1819{
1820 int i, rc = 0;
1821 int lenp, seqno = 0;
1822 struct cond_node *cur;
1823
1824 POLICY_WRLOCK;
1825
1826 lenp = policydb.p_bools.nprim;
1827 if (len != lenp) {
1828 rc = -EFAULT;
1829 goto out;
1830 }
1831
1da177e4 1832 for (i = 0; i < len; i++) {
af601e46
SG
1833 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1834 audit_log(current->audit_context, GFP_ATOMIC,
1835 AUDIT_MAC_CONFIG_CHANGE,
1836 "bool=%s val=%d old_val=%d auid=%u",
1837 policydb.p_bool_val_to_name[i],
1838 !!values[i],
1839 policydb.bool_val_to_struct[i]->state,
1840 audit_get_loginuid(current->audit_context));
1841 }
1da177e4
LT
1842 if (values[i]) {
1843 policydb.bool_val_to_struct[i]->state = 1;
1844 } else {
1845 policydb.bool_val_to_struct[i]->state = 0;
1846 }
1da177e4 1847 }
1da177e4
LT
1848
1849 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1850 rc = evaluate_cond_node(&policydb, cur);
1851 if (rc)
1852 goto out;
1853 }
1854
1855 seqno = ++latest_granting;
1856
1857out:
1858 POLICY_WRUNLOCK;
1859 if (!rc) {
1860 avc_ss_reset(seqno);
1861 selnl_notify_policyload(seqno);
342a0cff 1862 selinux_xfrm_notify_policyload();
1da177e4
LT
1863 }
1864 return rc;
1865}
1866
1867int security_get_bool_value(int bool)
1868{
1869 int rc = 0;
1870 int len;
1871
1872 POLICY_RDLOCK;
1873
1874 len = policydb.p_bools.nprim;
1875 if (bool >= len) {
1876 rc = -EFAULT;
1877 goto out;
1878 }
1879
1880 rc = policydb.bool_val_to_struct[bool]->state;
1881out:
1882 POLICY_RDUNLOCK;
1883 return rc;
1884}
376bd9cb 1885
08554d6b
VY
1886/*
1887 * security_sid_mls_copy() - computes a new sid based on the given
1888 * sid and the mls portion of mls_sid.
1889 */
1890int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1891{
1892 struct context *context1;
1893 struct context *context2;
1894 struct context newcon;
1895 char *s;
1896 u32 len;
1897 int rc = 0;
1898
4eb327b5 1899 if (!ss_initialized || !selinux_mls_enabled) {
08554d6b
VY
1900 *new_sid = sid;
1901 goto out;
1902 }
1903
1904 context_init(&newcon);
1905
1906 POLICY_RDLOCK;
1907 context1 = sidtab_search(&sidtab, sid);
1908 if (!context1) {
1909 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1910 "%d\n", sid);
1911 rc = -EINVAL;
1912 goto out_unlock;
1913 }
1914
1915 context2 = sidtab_search(&sidtab, mls_sid);
1916 if (!context2) {
1917 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
1918 "%d\n", mls_sid);
1919 rc = -EINVAL;
1920 goto out_unlock;
1921 }
1922
1923 newcon.user = context1->user;
1924 newcon.role = context1->role;
1925 newcon.type = context1->type;
0efc61ea 1926 rc = mls_context_cpy(&newcon, context2);
08554d6b
VY
1927 if (rc)
1928 goto out_unlock;
1929
08554d6b
VY
1930 /* Check the validity of the new context. */
1931 if (!policydb_context_isvalid(&policydb, &newcon)) {
1932 rc = convert_context_handle_invalid_context(&newcon);
1933 if (rc)
1934 goto bad;
1935 }
1936
1937 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
1938 goto out_unlock;
1939
1940bad:
1941 if (!context_struct_to_string(&newcon, &s, &len)) {
1942 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
1943 "security_sid_mls_copy: invalid context %s", s);
1944 kfree(s);
1945 }
1946
1947out_unlock:
1948 POLICY_RDUNLOCK;
1949 context_destroy(&newcon);
1950out:
1951 return rc;
1952}
1953
376bd9cb
DG
1954struct selinux_audit_rule {
1955 u32 au_seqno;
1956 struct context au_ctxt;
1957};
1958
1959void selinux_audit_rule_free(struct selinux_audit_rule *rule)
1960{
1961 if (rule) {
1962 context_destroy(&rule->au_ctxt);
1963 kfree(rule);
1964 }
1965}
1966
1967int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
1968 struct selinux_audit_rule **rule)
1969{
1970 struct selinux_audit_rule *tmprule;
1971 struct role_datum *roledatum;
1972 struct type_datum *typedatum;
1973 struct user_datum *userdatum;
1974 int rc = 0;
1975
1976 *rule = NULL;
1977
1978 if (!ss_initialized)
1979 return -ENOTSUPP;
1980
1981 switch (field) {
3a6b9f85
DG
1982 case AUDIT_SUBJ_USER:
1983 case AUDIT_SUBJ_ROLE:
1984 case AUDIT_SUBJ_TYPE:
6e5a2d1d
DG
1985 case AUDIT_OBJ_USER:
1986 case AUDIT_OBJ_ROLE:
1987 case AUDIT_OBJ_TYPE:
376bd9cb
DG
1988 /* only 'equals' and 'not equals' fit user, role, and type */
1989 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
1990 return -EINVAL;
1991 break;
3a6b9f85
DG
1992 case AUDIT_SUBJ_SEN:
1993 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
1994 case AUDIT_OBJ_LEV_LOW:
1995 case AUDIT_OBJ_LEV_HIGH:
376bd9cb
DG
1996 /* we do not allow a range, indicated by the presense of '-' */
1997 if (strchr(rulestr, '-'))
1998 return -EINVAL;
1999 break;
2000 default:
2001 /* only the above fields are valid */
2002 return -EINVAL;
2003 }
2004
2005 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2006 if (!tmprule)
2007 return -ENOMEM;
2008
2009 context_init(&tmprule->au_ctxt);
2010
2011 POLICY_RDLOCK;
2012
2013 tmprule->au_seqno = latest_granting;
2014
2015 switch (field) {
3a6b9f85 2016 case AUDIT_SUBJ_USER:
6e5a2d1d 2017 case AUDIT_OBJ_USER:
376bd9cb
DG
2018 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2019 if (!userdatum)
2020 rc = -EINVAL;
2021 else
2022 tmprule->au_ctxt.user = userdatum->value;
2023 break;
3a6b9f85 2024 case AUDIT_SUBJ_ROLE:
6e5a2d1d 2025 case AUDIT_OBJ_ROLE:
376bd9cb
DG
2026 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2027 if (!roledatum)
2028 rc = -EINVAL;
2029 else
2030 tmprule->au_ctxt.role = roledatum->value;
2031 break;
3a6b9f85 2032 case AUDIT_SUBJ_TYPE:
6e5a2d1d 2033 case AUDIT_OBJ_TYPE:
376bd9cb
DG
2034 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2035 if (!typedatum)
2036 rc = -EINVAL;
2037 else
2038 tmprule->au_ctxt.type = typedatum->value;
2039 break;
3a6b9f85
DG
2040 case AUDIT_SUBJ_SEN:
2041 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
2042 case AUDIT_OBJ_LEV_LOW:
2043 case AUDIT_OBJ_LEV_HIGH:
376bd9cb
DG
2044 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2045 break;
2046 }
2047
2048 POLICY_RDUNLOCK;
2049
2050 if (rc) {
2051 selinux_audit_rule_free(tmprule);
2052 tmprule = NULL;
2053 }
2054
2055 *rule = tmprule;
2056
2057 return rc;
2058}
2059
9a2f44f0 2060int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
376bd9cb
DG
2061 struct selinux_audit_rule *rule,
2062 struct audit_context *actx)
2063{
2064 struct context *ctxt;
2065 struct mls_level *level;
2066 int match = 0;
2067
2068 if (!rule) {
2069 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2070 "selinux_audit_rule_match: missing rule\n");
2071 return -ENOENT;
2072 }
2073
2074 POLICY_RDLOCK;
2075
2076 if (rule->au_seqno < latest_granting) {
2077 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2078 "selinux_audit_rule_match: stale rule\n");
2079 match = -ESTALE;
2080 goto out;
2081 }
2082
9a2f44f0 2083 ctxt = sidtab_search(&sidtab, sid);
376bd9cb
DG
2084 if (!ctxt) {
2085 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2086 "selinux_audit_rule_match: unrecognized SID %d\n",
9a2f44f0 2087 sid);
376bd9cb
DG
2088 match = -ENOENT;
2089 goto out;
2090 }
2091
2092 /* a field/op pair that is not caught here will simply fall through
2093 without a match */
2094 switch (field) {
3a6b9f85 2095 case AUDIT_SUBJ_USER:
6e5a2d1d 2096 case AUDIT_OBJ_USER:
376bd9cb
DG
2097 switch (op) {
2098 case AUDIT_EQUAL:
2099 match = (ctxt->user == rule->au_ctxt.user);
2100 break;
2101 case AUDIT_NOT_EQUAL:
2102 match = (ctxt->user != rule->au_ctxt.user);
2103 break;
2104 }
2105 break;
3a6b9f85 2106 case AUDIT_SUBJ_ROLE:
6e5a2d1d 2107 case AUDIT_OBJ_ROLE:
376bd9cb
DG
2108 switch (op) {
2109 case AUDIT_EQUAL:
2110 match = (ctxt->role == rule->au_ctxt.role);
2111 break;
2112 case AUDIT_NOT_EQUAL:
2113 match = (ctxt->role != rule->au_ctxt.role);
2114 break;
2115 }
2116 break;
3a6b9f85 2117 case AUDIT_SUBJ_TYPE:
6e5a2d1d 2118 case AUDIT_OBJ_TYPE:
376bd9cb
DG
2119 switch (op) {
2120 case AUDIT_EQUAL:
2121 match = (ctxt->type == rule->au_ctxt.type);
2122 break;
2123 case AUDIT_NOT_EQUAL:
2124 match = (ctxt->type != rule->au_ctxt.type);
2125 break;
2126 }
2127 break;
3a6b9f85
DG
2128 case AUDIT_SUBJ_SEN:
2129 case AUDIT_SUBJ_CLR:
6e5a2d1d
DG
2130 case AUDIT_OBJ_LEV_LOW:
2131 case AUDIT_OBJ_LEV_HIGH:
2132 level = ((field == AUDIT_SUBJ_SEN ||
2133 field == AUDIT_OBJ_LEV_LOW) ?
376bd9cb
DG
2134 &ctxt->range.level[0] : &ctxt->range.level[1]);
2135 switch (op) {
2136 case AUDIT_EQUAL:
2137 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2138 level);
2139 break;
2140 case AUDIT_NOT_EQUAL:
2141 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2142 level);
2143 break;
2144 case AUDIT_LESS_THAN:
2145 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2146 level) &&
2147 !mls_level_eq(&rule->au_ctxt.range.level[0],
2148 level));
2149 break;
2150 case AUDIT_LESS_THAN_OR_EQUAL:
2151 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2152 level);
2153 break;
2154 case AUDIT_GREATER_THAN:
2155 match = (mls_level_dom(level,
2156 &rule->au_ctxt.range.level[0]) &&
2157 !mls_level_eq(level,
2158 &rule->au_ctxt.range.level[0]));
2159 break;
2160 case AUDIT_GREATER_THAN_OR_EQUAL:
2161 match = mls_level_dom(level,
2162 &rule->au_ctxt.range.level[0]);
2163 break;
2164 }
2165 }
2166
2167out:
2168 POLICY_RDUNLOCK;
2169 return match;
2170}
2171
2172static int (*aurule_callback)(void) = NULL;
2173
2174static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2175 u16 class, u32 perms, u32 *retained)
2176{
2177 int err = 0;
2178
2179 if (event == AVC_CALLBACK_RESET && aurule_callback)
2180 err = aurule_callback();
2181 return err;
2182}
2183
2184static int __init aurule_init(void)
2185{
2186 int err;
2187
2188 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2189 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2190 if (err)
2191 panic("avc_add_callback() failed, error %d\n", err);
2192
2193 return err;
2194}
2195__initcall(aurule_init);
2196
2197void selinux_audit_set_callback(int (*callback)(void))
2198{
2199 aurule_callback = callback;
2200}
7420ed23
VY
2201
2202#ifdef CONFIG_NETLABEL
2203/*
5778eabd 2204 * NetLabel cache structure
7420ed23 2205 */
5778eabd 2206#define NETLBL_CACHE(x) ((struct selinux_netlbl_cache *)(x))
7420ed23
VY
2207#define NETLBL_CACHE_T_NONE 0
2208#define NETLBL_CACHE_T_SID 1
2209#define NETLBL_CACHE_T_MLS 2
5778eabd 2210struct selinux_netlbl_cache {
7420ed23
VY
2211 u32 type;
2212 union {
2213 u32 sid;
2214 struct mls_range mls_label;
2215 } data;
2216};
2217
2218/**
5778eabd 2219 * security_netlbl_cache_free - Free the NetLabel cached data
7420ed23
VY
2220 * @data: the data to free
2221 *
2222 * Description:
2223 * This function is intended to be used as the free() callback inside the
2224 * netlbl_lsm_cache structure.
2225 *
2226 */
5778eabd 2227static void security_netlbl_cache_free(const void *data)
7420ed23 2228{
5778eabd 2229 struct selinux_netlbl_cache *cache;
ffb733c6 2230
2231 if (data == NULL)
2232 return;
2233
2234 cache = NETLBL_CACHE(data);
7420ed23
VY
2235 switch (cache->type) {
2236 case NETLBL_CACHE_T_MLS:
2237 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2238 break;
2239 }
2240 kfree(data);
2241}
2242
2243/**
5778eabd
PM
2244 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2245 * @secattr: the NetLabel packet security attributes
7420ed23
VY
2246 * @ctx: the SELinux context
2247 *
2248 * Description:
2249 * Attempt to cache the context in @ctx, which was derived from the packet in
5778eabd
PM
2250 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2251 * already been initialized.
7420ed23
VY
2252 *
2253 */
5778eabd
PM
2254static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2255 struct context *ctx)
7420ed23 2256{
5778eabd 2257 struct selinux_netlbl_cache *cache = NULL;
7420ed23 2258
5778eabd
PM
2259 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2260 if (secattr->cache == NULL)
2261 return;
7420ed23
VY
2262
2263 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2264 if (cache == NULL)
5778eabd 2265 return;
7420ed23
VY
2266
2267 cache->type = NETLBL_CACHE_T_MLS;
2268 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
2269 &ctx->range.level[0].cat) != 0)
5778eabd 2270 return;
7420ed23
VY
2271 cache->data.mls_label.level[1].cat.highbit =
2272 cache->data.mls_label.level[0].cat.highbit;
2273 cache->data.mls_label.level[1].cat.node =
2274 cache->data.mls_label.level[0].cat.node;
2275 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2276 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2277
5778eabd
PM
2278 secattr->cache->free = security_netlbl_cache_free;
2279 secattr->cache->data = (void *)cache;
2280 secattr->flags |= NETLBL_SECATTR_CACHE;
7420ed23
VY
2281}
2282
2283/**
5778eabd 2284 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
7420ed23
VY
2285 * @secattr: the NetLabel packet security attributes
2286 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2287 * @sid: the SELinux SID
2288 *
2289 * Description:
5778eabd 2290 * Convert the given NetLabel security attributes in @secattr into a
7420ed23 2291 * SELinux SID. If the @secattr field does not contain a full SELinux
5778eabd
PM
2292 * SID/context then use the context in @base_sid as the foundation. If
2293 * possibile the 'cache' field of @secattr is set and the CACHE flag is set;
2294 * this is to allow the @secattr to be used by NetLabel to cache the secattr to
2295 * SID conversion for future lookups. Returns zero on success, negative
2296 * values on failure.
7420ed23
VY
2297 *
2298 */
5778eabd
PM
2299int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2300 u32 base_sid,
2301 u32 *sid)
7420ed23
VY
2302{
2303 int rc = -EIDRM;
2304 struct context *ctx;
2305 struct context ctx_new;
5778eabd
PM
2306 struct selinux_netlbl_cache *cache;
2307
2308 if (!ss_initialized) {
2309 *sid = SECSID_NULL;
2310 return 0;
2311 }
7420ed23
VY
2312
2313 POLICY_RDLOCK;
2314
701a90ba 2315 if (secattr->flags & NETLBL_SECATTR_CACHE) {
ffb733c6 2316 cache = NETLBL_CACHE(secattr->cache->data);
7420ed23
VY
2317 switch (cache->type) {
2318 case NETLBL_CACHE_T_SID:
2319 *sid = cache->data.sid;
2320 rc = 0;
2321 break;
2322 case NETLBL_CACHE_T_MLS:
2323 ctx = sidtab_search(&sidtab, base_sid);
2324 if (ctx == NULL)
2325 goto netlbl_secattr_to_sid_return;
2326
2327 ctx_new.user = ctx->user;
2328 ctx_new.role = ctx->role;
2329 ctx_new.type = ctx->type;
2330 ctx_new.range.level[0].sens =
2331 cache->data.mls_label.level[0].sens;
2332 ctx_new.range.level[0].cat.highbit =
2333 cache->data.mls_label.level[0].cat.highbit;
2334 ctx_new.range.level[0].cat.node =
2335 cache->data.mls_label.level[0].cat.node;
2336 ctx_new.range.level[1].sens =
2337 cache->data.mls_label.level[1].sens;
2338 ctx_new.range.level[1].cat.highbit =
2339 cache->data.mls_label.level[1].cat.highbit;
2340 ctx_new.range.level[1].cat.node =
2341 cache->data.mls_label.level[1].cat.node;
2342
2343 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2344 break;
2345 default:
2346 goto netlbl_secattr_to_sid_return;
2347 }
701a90ba 2348 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
7420ed23
VY
2349 ctx = sidtab_search(&sidtab, base_sid);
2350 if (ctx == NULL)
2351 goto netlbl_secattr_to_sid_return;
2352
2353 ctx_new.user = ctx->user;
2354 ctx_new.role = ctx->role;
2355 ctx_new.type = ctx->type;
02752760 2356 mls_import_netlbl_lvl(&ctx_new, secattr);
701a90ba 2357 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
02752760
PM
2358 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2359 secattr->mls_cat) != 0)
7420ed23
VY
2360 goto netlbl_secattr_to_sid_return;
2361 ctx_new.range.level[1].cat.highbit =
2362 ctx_new.range.level[0].cat.highbit;
2363 ctx_new.range.level[1].cat.node =
2364 ctx_new.range.level[0].cat.node;
2365 } else {
2366 ebitmap_init(&ctx_new.range.level[0].cat);
2367 ebitmap_init(&ctx_new.range.level[1].cat);
2368 }
2369 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2370 goto netlbl_secattr_to_sid_return_cleanup;
2371
2372 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2373 if (rc != 0)
2374 goto netlbl_secattr_to_sid_return_cleanup;
2375
5778eabd
PM
2376 security_netlbl_cache_add(secattr, &ctx_new);
2377
7420ed23
VY
2378 ebitmap_destroy(&ctx_new.range.level[0].cat);
2379 } else {
388b2405 2380 *sid = SECSID_NULL;
7420ed23
VY
2381 rc = 0;
2382 }
2383
2384netlbl_secattr_to_sid_return:
2385 POLICY_RDUNLOCK;
2386 return rc;
2387netlbl_secattr_to_sid_return_cleanup:
2388 ebitmap_destroy(&ctx_new.range.level[0].cat);
2389 goto netlbl_secattr_to_sid_return;
2390}
2391
2392/**
5778eabd
PM
2393 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2394 * @sid: the SELinux SID
2395 * @secattr: the NetLabel packet security attributes
7420ed23
VY
2396 *
2397 * Description:
5778eabd
PM
2398 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2399 * Returns zero on success, negative values on failure.
7420ed23
VY
2400 *
2401 */
5778eabd 2402int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
7420ed23
VY
2403{
2404 int rc = -ENOENT;
7420ed23
VY
2405 struct context *ctx;
2406
5778eabd
PM
2407 netlbl_secattr_init(secattr);
2408
7420ed23
VY
2409 if (!ss_initialized)
2410 return 0;
2411
2412 POLICY_RDLOCK;
7420ed23
VY
2413 ctx = sidtab_search(&sidtab, sid);
2414 if (ctx == NULL)
5778eabd
PM
2415 goto netlbl_sid_to_secattr_failure;
2416 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2417 GFP_ATOMIC);
2418 secattr->flags |= NETLBL_SECATTR_DOMAIN;
2419 mls_export_netlbl_lvl(ctx, secattr);
2420 rc = mls_export_netlbl_cat(ctx, secattr);
bf0edf39 2421 if (rc != 0)
5778eabd 2422 goto netlbl_sid_to_secattr_failure;
7420ed23 2423 POLICY_RDUNLOCK;
99f59ed0 2424
5778eabd 2425 return 0;
f8687afe 2426
5778eabd
PM
2427netlbl_sid_to_secattr_failure:
2428 POLICY_RDUNLOCK;
2429 netlbl_secattr_destroy(secattr);
f8687afe
PM
2430 return rc;
2431}
7420ed23 2432#endif /* CONFIG_NETLABEL */
This page took 0.417961 seconds and 5 git commands to generate.