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