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