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