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