blob: 03140edf97a3d54229bca7f48b3fe62deb92bb49 [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];
356 ebitmap_for_each_bit(sattr, snode, i) {
357 if (!ebitmap_node_get_bit(snode, i))
358 continue;
359 ebitmap_for_each_bit(tattr, tnode, j) {
360 if (!ebitmap_node_get_bit(tnode, j))
361 continue;
362 avkey.source_type = i + 1;
363 avkey.target_type = j + 1;
364 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
365 node != NULL;
366 node = avtab_search_node_next(node, avkey.specified)) {
367 if (node->key.specified == AVTAB_ALLOWED)
368 avd->allowed |= node->datum.data;
369 else if (node->key.specified == AVTAB_AUDITALLOW)
370 avd->auditallow |= node->datum.data;
371 else if (node->key.specified == AVTAB_AUDITDENY)
372 avd->auditdeny &= node->datum.data;
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Stephen Smalley782ebb92005-09-03 15:55:16 -0700375 /* Check conditional av table for additional permissions */
376 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
377
378 }
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /*
382 * Remove any permissions prohibited by a constraint (this includes
383 * the MLS policy).
384 */
385 constraint = tclass_datum->constraints;
386 while (constraint) {
387 if ((constraint->permissions & (avd->allowed)) &&
388 !constraint_expr_eval(scontext, tcontext, NULL,
389 constraint->expr)) {
390 avd->allowed = (avd->allowed) & ~(constraint->permissions);
391 }
392 constraint = constraint->next;
393 }
394
395 /*
396 * If checking process transition permission and the
397 * role is changing, then check the (current_role, new_role)
398 * pair.
399 */
400 if (tclass == SECCLASS_PROCESS &&
401 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
402 scontext->role != tcontext->role) {
403 for (ra = policydb.role_allow; ra; ra = ra->next) {
404 if (scontext->role == ra->role &&
405 tcontext->role == ra->new_role)
406 break;
407 }
408 if (!ra)
409 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
410 PROCESS__DYNTRANSITION);
411 }
412
413 return 0;
Eric Paris3f120702007-09-21 14:37:10 -0400414
415inval_class:
416 printk(KERN_ERR "%s: unrecognized class %d\n", __FUNCTION__, tclass);
417 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420static int security_validtrans_handle_fail(struct context *ocontext,
421 struct context *ncontext,
422 struct context *tcontext,
423 u16 tclass)
424{
425 char *o = NULL, *n = NULL, *t = NULL;
426 u32 olen, nlen, tlen;
427
428 if (context_struct_to_string(ocontext, &o, &olen) < 0)
429 goto out;
430 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
431 goto out;
432 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
433 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100434 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 "security_validate_transition: denied for"
436 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
437 o, n, t, policydb.p_class_val_to_name[tclass-1]);
438out:
439 kfree(o);
440 kfree(n);
441 kfree(t);
442
443 if (!selinux_enforcing)
444 return 0;
445 return -EPERM;
446}
447
448int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
449 u16 tclass)
450{
451 struct context *ocontext;
452 struct context *ncontext;
453 struct context *tcontext;
454 struct class_datum *tclass_datum;
455 struct constraint_node *constraint;
456 int rc = 0;
457
458 if (!ss_initialized)
459 return 0;
460
461 POLICY_RDLOCK;
462
463 /*
464 * Remap extended Netlink classes for old policy versions.
465 * Do this here rather than socket_type_to_security_class()
466 * in case a newer policy version is loaded, allowing sockets
467 * to remain in the correct class.
468 */
469 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
470 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
471 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
472 tclass = SECCLASS_NETLINK_SOCKET;
473
474 if (!tclass || tclass > policydb.p_classes.nprim) {
475 printk(KERN_ERR "security_validate_transition: "
476 "unrecognized class %d\n", tclass);
477 rc = -EINVAL;
478 goto out;
479 }
480 tclass_datum = policydb.class_val_to_struct[tclass - 1];
481
482 ocontext = sidtab_search(&sidtab, oldsid);
483 if (!ocontext) {
484 printk(KERN_ERR "security_validate_transition: "
485 " unrecognized SID %d\n", oldsid);
486 rc = -EINVAL;
487 goto out;
488 }
489
490 ncontext = sidtab_search(&sidtab, newsid);
491 if (!ncontext) {
492 printk(KERN_ERR "security_validate_transition: "
493 " unrecognized SID %d\n", newsid);
494 rc = -EINVAL;
495 goto out;
496 }
497
498 tcontext = sidtab_search(&sidtab, tasksid);
499 if (!tcontext) {
500 printk(KERN_ERR "security_validate_transition: "
501 " unrecognized SID %d\n", tasksid);
502 rc = -EINVAL;
503 goto out;
504 }
505
506 constraint = tclass_datum->validatetrans;
507 while (constraint) {
508 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
509 constraint->expr)) {
510 rc = security_validtrans_handle_fail(ocontext, ncontext,
511 tcontext, tclass);
512 goto out;
513 }
514 constraint = constraint->next;
515 }
516
517out:
518 POLICY_RDUNLOCK;
519 return rc;
520}
521
522/**
523 * security_compute_av - Compute access vector decisions.
524 * @ssid: source security identifier
525 * @tsid: target security identifier
526 * @tclass: target security class
527 * @requested: requested permissions
528 * @avd: access vector decisions
529 *
530 * Compute a set of access vector decisions based on the
531 * SID pair (@ssid, @tsid) for the permissions in @tclass.
532 * Return -%EINVAL if any of the parameters are invalid or %0
533 * if the access vector decisions were computed successfully.
534 */
535int security_compute_av(u32 ssid,
536 u32 tsid,
537 u16 tclass,
538 u32 requested,
539 struct av_decision *avd)
540{
541 struct context *scontext = NULL, *tcontext = NULL;
542 int rc = 0;
543
544 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700545 avd->allowed = 0xffffffff;
546 avd->decided = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 avd->auditallow = 0;
548 avd->auditdeny = 0xffffffff;
549 avd->seqno = latest_granting;
550 return 0;
551 }
552
553 POLICY_RDLOCK;
554
555 scontext = sidtab_search(&sidtab, ssid);
556 if (!scontext) {
557 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
558 ssid);
559 rc = -EINVAL;
560 goto out;
561 }
562 tcontext = sidtab_search(&sidtab, tsid);
563 if (!tcontext) {
564 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
565 tsid);
566 rc = -EINVAL;
567 goto out;
568 }
569
570 rc = context_struct_compute_av(scontext, tcontext, tclass,
571 requested, avd);
572out:
573 POLICY_RDUNLOCK;
574 return rc;
575}
576
577/*
578 * Write the security context string representation of
579 * the context structure `context' into a dynamically
580 * allocated string of the correct size. Set `*scontext'
581 * to point to this string and set `*scontext_len' to
582 * the length of the string.
583 */
584static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
585{
586 char *scontextp;
587
588 *scontext = NULL;
589 *scontext_len = 0;
590
591 /* Compute the size of the context. */
592 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
593 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
594 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
595 *scontext_len += mls_compute_context_len(context);
596
597 /* Allocate space for the context; caller must free this space. */
598 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
599 if (!scontextp) {
600 return -ENOMEM;
601 }
602 *scontext = scontextp;
603
604 /*
605 * Copy the user name, role name and type name into the context.
606 */
607 sprintf(scontextp, "%s:%s:%s",
608 policydb.p_user_val_to_name[context->user - 1],
609 policydb.p_role_val_to_name[context->role - 1],
610 policydb.p_type_val_to_name[context->type - 1]);
611 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
612 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
613 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
614
615 mls_sid_to_context(context, &scontextp);
616
617 *scontextp = 0;
618
619 return 0;
620}
621
622#include "initial_sid_to_string.h"
623
James Carterf0ee2e42007-04-04 10:11:29 -0400624const char *security_get_initial_sid_context(u32 sid)
625{
626 if (unlikely(sid > SECINITSID_NUM))
627 return NULL;
628 return initial_sid_to_string[sid];
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/**
632 * security_sid_to_context - Obtain a context for a given SID.
633 * @sid: security identifier, SID
634 * @scontext: security context
635 * @scontext_len: length in bytes
636 *
637 * Write the string representation of the context associated with @sid
638 * into a dynamically allocated string of the correct size. Set @scontext
639 * to point to this string and set @scontext_len to the length of the string.
640 */
641int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
642{
643 struct context *context;
644 int rc = 0;
645
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500646 *scontext = NULL;
647 *scontext_len = 0;
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 if (!ss_initialized) {
650 if (sid <= SECINITSID_NUM) {
651 char *scontextp;
652
653 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
654 scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -0700655 if (!scontextp) {
656 rc = -ENOMEM;
657 goto out;
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 strcpy(scontextp, initial_sid_to_string[sid]);
660 *scontext = scontextp;
661 goto out;
662 }
663 printk(KERN_ERR "security_sid_to_context: called before initial "
664 "load_policy on unknown SID %d\n", sid);
665 rc = -EINVAL;
666 goto out;
667 }
668 POLICY_RDLOCK;
669 context = sidtab_search(&sidtab, sid);
670 if (!context) {
671 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
672 "%d\n", sid);
673 rc = -EINVAL;
674 goto out_unlock;
675 }
676 rc = context_struct_to_string(context, scontext, scontext_len);
677out_unlock:
678 POLICY_RDUNLOCK;
679out:
680 return rc;
681
682}
683
James Morrisf5c1d5b2005-07-28 01:07:37 -0700684static int security_context_to_sid_core(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 char *scontext2;
687 struct context context;
688 struct role_datum *role;
689 struct type_datum *typdatum;
690 struct user_datum *usrdatum;
691 char *scontextp, *p, oldc;
692 int rc = 0;
693
694 if (!ss_initialized) {
695 int i;
696
697 for (i = 1; i < SECINITSID_NUM; i++) {
698 if (!strcmp(initial_sid_to_string[i], scontext)) {
699 *sid = i;
700 goto out;
701 }
702 }
703 *sid = SECINITSID_KERNEL;
704 goto out;
705 }
706 *sid = SECSID_NULL;
707
708 /* Copy the string so that we can modify the copy as we parse it.
709 The string should already by null terminated, but we append a
710 null suffix to the copy to avoid problems with the existing
711 attr package, which doesn't view the null terminator as part
712 of the attribute value. */
713 scontext2 = kmalloc(scontext_len+1,GFP_KERNEL);
714 if (!scontext2) {
715 rc = -ENOMEM;
716 goto out;
717 }
718 memcpy(scontext2, scontext, scontext_len);
719 scontext2[scontext_len] = 0;
720
721 context_init(&context);
722 *sid = SECSID_NULL;
723
724 POLICY_RDLOCK;
725
726 /* Parse the security context. */
727
728 rc = -EINVAL;
729 scontextp = (char *) scontext2;
730
731 /* Extract the user. */
732 p = scontextp;
733 while (*p && *p != ':')
734 p++;
735
736 if (*p == 0)
737 goto out_unlock;
738
739 *p++ = 0;
740
741 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
742 if (!usrdatum)
743 goto out_unlock;
744
745 context.user = usrdatum->value;
746
747 /* Extract role. */
748 scontextp = p;
749 while (*p && *p != ':')
750 p++;
751
752 if (*p == 0)
753 goto out_unlock;
754
755 *p++ = 0;
756
757 role = hashtab_search(policydb.p_roles.table, scontextp);
758 if (!role)
759 goto out_unlock;
760 context.role = role->value;
761
762 /* Extract type. */
763 scontextp = p;
764 while (*p && *p != ':')
765 p++;
766 oldc = *p;
767 *p++ = 0;
768
769 typdatum = hashtab_search(policydb.p_types.table, scontextp);
770 if (!typdatum)
771 goto out_unlock;
772
773 context.type = typdatum->value;
774
James Morrisf5c1d5b2005-07-28 01:07:37 -0700775 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (rc)
777 goto out_unlock;
778
779 if ((p - scontext2) < scontext_len) {
780 rc = -EINVAL;
781 goto out_unlock;
782 }
783
784 /* Check the validity of the new context. */
785 if (!policydb_context_isvalid(&policydb, &context)) {
786 rc = -EINVAL;
787 goto out_unlock;
788 }
789 /* Obtain the new sid. */
790 rc = sidtab_context_to_sid(&sidtab, &context, sid);
791out_unlock:
792 POLICY_RDUNLOCK;
793 context_destroy(&context);
794 kfree(scontext2);
795out:
796 return rc;
797}
798
James Morrisf5c1d5b2005-07-28 01:07:37 -0700799/**
800 * security_context_to_sid - Obtain a SID for a given security context.
801 * @scontext: security context
802 * @scontext_len: length in bytes
803 * @sid: security identifier, SID
804 *
805 * Obtains a SID associated with the security context that
806 * has the string representation specified by @scontext.
807 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
808 * memory is available, or 0 on success.
809 */
810int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
811{
812 return security_context_to_sid_core(scontext, scontext_len,
813 sid, SECSID_NULL);
814}
815
816/**
817 * security_context_to_sid_default - Obtain a SID for a given security context,
818 * falling back to specified default if needed.
819 *
820 * @scontext: security context
821 * @scontext_len: length in bytes
822 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -0700823 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -0700824 *
825 * Obtains a SID associated with the security context that
826 * has the string representation specified by @scontext.
827 * The default SID is passed to the MLS layer to be used to allow
828 * kernel labeling of the MLS field if the MLS field is not present
829 * (for upgrading to MLS without full relabel).
830 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
831 * memory is available, or 0 on success.
832 */
833int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid, u32 def_sid)
834{
835 return security_context_to_sid_core(scontext, scontext_len,
836 sid, def_sid);
837}
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839static int compute_sid_handle_invalid_context(
840 struct context *scontext,
841 struct context *tcontext,
842 u16 tclass,
843 struct context *newcontext)
844{
845 char *s = NULL, *t = NULL, *n = NULL;
846 u32 slen, tlen, nlen;
847
848 if (context_struct_to_string(scontext, &s, &slen) < 0)
849 goto out;
850 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
851 goto out;
852 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
853 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100854 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 "security_compute_sid: invalid context %s"
856 " for scontext=%s"
857 " tcontext=%s"
858 " tclass=%s",
859 n, s, t, policydb.p_class_val_to_name[tclass-1]);
860out:
861 kfree(s);
862 kfree(t);
863 kfree(n);
864 if (!selinux_enforcing)
865 return 0;
866 return -EACCES;
867}
868
869static int security_compute_sid(u32 ssid,
870 u32 tsid,
871 u16 tclass,
872 u32 specified,
873 u32 *out_sid)
874{
875 struct context *scontext = NULL, *tcontext = NULL, newcontext;
876 struct role_trans *roletr = NULL;
877 struct avtab_key avkey;
878 struct avtab_datum *avdatum;
879 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 int rc = 0;
881
882 if (!ss_initialized) {
883 switch (tclass) {
884 case SECCLASS_PROCESS:
885 *out_sid = ssid;
886 break;
887 default:
888 *out_sid = tsid;
889 break;
890 }
891 goto out;
892 }
893
Venkat Yekkirala851f8a62006-07-30 03:03:18 -0700894 context_init(&newcontext);
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 POLICY_RDLOCK;
897
898 scontext = sidtab_search(&sidtab, ssid);
899 if (!scontext) {
900 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
901 ssid);
902 rc = -EINVAL;
903 goto out_unlock;
904 }
905 tcontext = sidtab_search(&sidtab, tsid);
906 if (!tcontext) {
907 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
908 tsid);
909 rc = -EINVAL;
910 goto out_unlock;
911 }
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 /* Set the user identity. */
914 switch (specified) {
915 case AVTAB_TRANSITION:
916 case AVTAB_CHANGE:
917 /* Use the process user identity. */
918 newcontext.user = scontext->user;
919 break;
920 case AVTAB_MEMBER:
921 /* Use the related object owner. */
922 newcontext.user = tcontext->user;
923 break;
924 }
925
926 /* Set the role and type to default values. */
927 switch (tclass) {
928 case SECCLASS_PROCESS:
929 /* Use the current role and type of process. */
930 newcontext.role = scontext->role;
931 newcontext.type = scontext->type;
932 break;
933 default:
934 /* Use the well-defined object role. */
935 newcontext.role = OBJECT_R_VAL;
936 /* Use the type of the related object. */
937 newcontext.type = tcontext->type;
938 }
939
940 /* Look for a type transition/member/change rule. */
941 avkey.source_type = scontext->type;
942 avkey.target_type = tcontext->type;
943 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700944 avkey.specified = specified;
945 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /* If no permanent rule, also check for enabled conditional rules */
948 if(!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700949 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700951 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 avdatum = &node->datum;
953 break;
954 }
955 }
956 }
957
Stephen Smalley782ebb92005-09-03 15:55:16 -0700958 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -0700960 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
963 /* Check for class-specific changes. */
964 switch (tclass) {
965 case SECCLASS_PROCESS:
966 if (specified & AVTAB_TRANSITION) {
967 /* Look for a role transition rule. */
968 for (roletr = policydb.role_tr; roletr;
969 roletr = roletr->next) {
970 if (roletr->role == scontext->role &&
971 roletr->type == tcontext->type) {
972 /* Use the role transition rule. */
973 newcontext.role = roletr->new_role;
974 break;
975 }
976 }
977 }
978 break;
979 default:
980 break;
981 }
982
983 /* Set the MLS attributes.
984 This is done last because it may allocate memory. */
985 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
986 if (rc)
987 goto out_unlock;
988
989 /* Check the validity of the context. */
990 if (!policydb_context_isvalid(&policydb, &newcontext)) {
991 rc = compute_sid_handle_invalid_context(scontext,
992 tcontext,
993 tclass,
994 &newcontext);
995 if (rc)
996 goto out_unlock;
997 }
998 /* Obtain the sid for the context. */
999 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1000out_unlock:
1001 POLICY_RDUNLOCK;
1002 context_destroy(&newcontext);
1003out:
1004 return rc;
1005}
1006
1007/**
1008 * security_transition_sid - Compute the SID for a new subject/object.
1009 * @ssid: source security identifier
1010 * @tsid: target security identifier
1011 * @tclass: target security class
1012 * @out_sid: security identifier for new subject/object
1013 *
1014 * Compute a SID to use for labeling a new subject or object in the
1015 * class @tclass based on a SID pair (@ssid, @tsid).
1016 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1017 * if insufficient memory is available, or %0 if the new SID was
1018 * computed successfully.
1019 */
1020int security_transition_sid(u32 ssid,
1021 u32 tsid,
1022 u16 tclass,
1023 u32 *out_sid)
1024{
1025 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1026}
1027
1028/**
1029 * security_member_sid - Compute the SID for member selection.
1030 * @ssid: source security identifier
1031 * @tsid: target security identifier
1032 * @tclass: target security class
1033 * @out_sid: security identifier for selected member
1034 *
1035 * Compute a SID to use when selecting a member of a polyinstantiated
1036 * object of class @tclass based on a SID pair (@ssid, @tsid).
1037 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1038 * if insufficient memory is available, or %0 if the SID was
1039 * computed successfully.
1040 */
1041int security_member_sid(u32 ssid,
1042 u32 tsid,
1043 u16 tclass,
1044 u32 *out_sid)
1045{
1046 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1047}
1048
1049/**
1050 * security_change_sid - Compute the SID for object relabeling.
1051 * @ssid: source security identifier
1052 * @tsid: target security identifier
1053 * @tclass: target security class
1054 * @out_sid: security identifier for selected member
1055 *
1056 * Compute a SID to use for relabeling an object of class @tclass
1057 * based on a SID pair (@ssid, @tsid).
1058 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1059 * if insufficient memory is available, or %0 if the SID was
1060 * computed successfully.
1061 */
1062int security_change_sid(u32 ssid,
1063 u32 tsid,
1064 u16 tclass,
1065 u32 *out_sid)
1066{
1067 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1068}
1069
Chad Sellersb94c7e62006-11-06 12:38:18 -05001070/*
1071 * Verify that each kernel class that is defined in the
1072 * policy is correct
1073 */
1074static int validate_classes(struct policydb *p)
1075{
1076 int i, j;
1077 struct class_datum *cladatum;
1078 struct perm_datum *perdatum;
1079 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1080 u16 class_val;
1081 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1082 const char *def_class, *def_perm, *pol_class;
1083 struct symtab *perms;
1084
Eric Paris3f120702007-09-21 14:37:10 -04001085 if (p->allow_unknown) {
1086 u32 num_classes = kdefs->cts_len;
1087 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1088 if (!p->undefined_perms)
1089 return -ENOMEM;
1090 }
1091
Chad Sellersb94c7e62006-11-06 12:38:18 -05001092 for (i = 1; i < kdefs->cts_len; i++) {
1093 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001094 if (!def_class)
1095 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001096 if (i > p->p_classes.nprim) {
1097 printk(KERN_INFO
1098 "security: class %s not defined in policy\n",
1099 def_class);
Eric Paris3f120702007-09-21 14:37:10 -04001100 if (p->reject_unknown)
1101 return -EINVAL;
1102 if (p->allow_unknown)
1103 p->undefined_perms[i-1] = ~0U;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001104 continue;
1105 }
1106 pol_class = p->p_class_val_to_name[i-1];
1107 if (strcmp(pol_class, def_class)) {
1108 printk(KERN_ERR
1109 "security: class %d is incorrect, found %s but should be %s\n",
1110 i, pol_class, def_class);
1111 return -EINVAL;
1112 }
1113 }
1114 for (i = 0; i < kdefs->av_pts_len; i++) {
1115 class_val = kdefs->av_perm_to_string[i].tclass;
1116 perm_val = kdefs->av_perm_to_string[i].value;
1117 def_perm = kdefs->av_perm_to_string[i].name;
1118 if (class_val > p->p_classes.nprim)
1119 continue;
1120 pol_class = p->p_class_val_to_name[class_val-1];
1121 cladatum = hashtab_search(p->p_classes.table, pol_class);
1122 BUG_ON(!cladatum);
1123 perms = &cladatum->permissions;
1124 nprim = 1 << (perms->nprim - 1);
1125 if (perm_val > nprim) {
1126 printk(KERN_INFO
1127 "security: permission %s in class %s not defined in policy\n",
1128 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001129 if (p->reject_unknown)
1130 return -EINVAL;
1131 if (p->allow_unknown)
1132 p->undefined_perms[class_val-1] |= perm_val;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001133 continue;
1134 }
1135 perdatum = hashtab_search(perms->table, def_perm);
1136 if (perdatum == NULL) {
1137 printk(KERN_ERR
Eric Paris3f120702007-09-21 14:37:10 -04001138 "security: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001139 def_perm, pol_class);
1140 return -EINVAL;
1141 }
1142 pol_val = 1 << (perdatum->value - 1);
1143 if (pol_val != perm_val) {
1144 printk(KERN_ERR
1145 "security: permission %s in class %s has incorrect value\n",
1146 def_perm, pol_class);
1147 return -EINVAL;
1148 }
1149 }
1150 for (i = 0; i < kdefs->av_inherit_len; i++) {
1151 class_val = kdefs->av_inherit[i].tclass;
1152 if (class_val > p->p_classes.nprim)
1153 continue;
1154 pol_class = p->p_class_val_to_name[class_val-1];
1155 cladatum = hashtab_search(p->p_classes.table, pol_class);
1156 BUG_ON(!cladatum);
1157 if (!cladatum->comdatum) {
1158 printk(KERN_ERR
1159 "security: class %s should have an inherits clause but does not\n",
1160 pol_class);
1161 return -EINVAL;
1162 }
1163 tmp = kdefs->av_inherit[i].common_base;
1164 common_pts_len = 0;
1165 while (!(tmp & 0x01)) {
1166 common_pts_len++;
1167 tmp >>= 1;
1168 }
1169 perms = &cladatum->comdatum->permissions;
1170 for (j = 0; j < common_pts_len; j++) {
1171 def_perm = kdefs->av_inherit[i].common_pts[j];
1172 if (j >= perms->nprim) {
1173 printk(KERN_INFO
1174 "security: permission %s in class %s not defined in policy\n",
1175 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001176 if (p->reject_unknown)
1177 return -EINVAL;
1178 if (p->allow_unknown)
1179 p->undefined_perms[class_val-1] |= (1 << j);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001180 continue;
1181 }
1182 perdatum = hashtab_search(perms->table, def_perm);
1183 if (perdatum == NULL) {
1184 printk(KERN_ERR
Eric Paris3f120702007-09-21 14:37:10 -04001185 "security: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001186 def_perm, pol_class);
1187 return -EINVAL;
1188 }
1189 if (perdatum->value != j + 1) {
1190 printk(KERN_ERR
1191 "security: permission %s in class %s has incorrect value\n",
1192 def_perm, pol_class);
1193 return -EINVAL;
1194 }
1195 }
1196 }
1197 return 0;
1198}
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200/* Clone the SID into the new SID table. */
1201static int clone_sid(u32 sid,
1202 struct context *context,
1203 void *arg)
1204{
1205 struct sidtab *s = arg;
1206
1207 return sidtab_insert(s, sid, context);
1208}
1209
1210static inline int convert_context_handle_invalid_context(struct context *context)
1211{
1212 int rc = 0;
1213
1214 if (selinux_enforcing) {
1215 rc = -EINVAL;
1216 } else {
1217 char *s;
1218 u32 len;
1219
1220 context_struct_to_string(context, &s, &len);
1221 printk(KERN_ERR "security: context %s is invalid\n", s);
1222 kfree(s);
1223 }
1224 return rc;
1225}
1226
1227struct convert_context_args {
1228 struct policydb *oldp;
1229 struct policydb *newp;
1230};
1231
1232/*
1233 * Convert the values in the security context
1234 * structure `c' from the values specified
1235 * in the policy `p->oldp' to the values specified
1236 * in the policy `p->newp'. Verify that the
1237 * context is valid under the new policy.
1238 */
1239static int convert_context(u32 key,
1240 struct context *c,
1241 void *p)
1242{
1243 struct convert_context_args *args;
1244 struct context oldc;
1245 struct role_datum *role;
1246 struct type_datum *typdatum;
1247 struct user_datum *usrdatum;
1248 char *s;
1249 u32 len;
1250 int rc;
1251
1252 args = p;
1253
1254 rc = context_cpy(&oldc, c);
1255 if (rc)
1256 goto out;
1257
1258 rc = -EINVAL;
1259
1260 /* Convert the user. */
1261 usrdatum = hashtab_search(args->newp->p_users.table,
1262 args->oldp->p_user_val_to_name[c->user - 1]);
1263 if (!usrdatum) {
1264 goto bad;
1265 }
1266 c->user = usrdatum->value;
1267
1268 /* Convert the role. */
1269 role = hashtab_search(args->newp->p_roles.table,
1270 args->oldp->p_role_val_to_name[c->role - 1]);
1271 if (!role) {
1272 goto bad;
1273 }
1274 c->role = role->value;
1275
1276 /* Convert the type. */
1277 typdatum = hashtab_search(args->newp->p_types.table,
1278 args->oldp->p_type_val_to_name[c->type - 1]);
1279 if (!typdatum) {
1280 goto bad;
1281 }
1282 c->type = typdatum->value;
1283
1284 rc = mls_convert_context(args->oldp, args->newp, c);
1285 if (rc)
1286 goto bad;
1287
1288 /* Check the validity of the new context. */
1289 if (!policydb_context_isvalid(args->newp, c)) {
1290 rc = convert_context_handle_invalid_context(&oldc);
1291 if (rc)
1292 goto bad;
1293 }
1294
1295 context_destroy(&oldc);
1296out:
1297 return rc;
1298bad:
1299 context_struct_to_string(&oldc, &s, &len);
1300 context_destroy(&oldc);
1301 printk(KERN_ERR "security: invalidating context %s\n", s);
1302 kfree(s);
1303 goto out;
1304}
1305
1306extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001307static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309/**
1310 * security_load_policy - Load a security policy configuration.
1311 * @data: binary policy data
1312 * @len: length of data in bytes
1313 *
1314 * Load a new set of security policy configuration data,
1315 * validate it and convert the SID table as necessary.
1316 * This function will flush the access vector cache after
1317 * loading the new policy.
1318 */
1319int security_load_policy(void *data, size_t len)
1320{
1321 struct policydb oldpolicydb, newpolicydb;
1322 struct sidtab oldsidtab, newsidtab;
1323 struct convert_context_args args;
1324 u32 seqno;
1325 int rc = 0;
1326 struct policy_file file = { data, len }, *fp = &file;
1327
1328 LOAD_LOCK;
1329
1330 if (!ss_initialized) {
1331 avtab_cache_init();
1332 if (policydb_read(&policydb, fp)) {
1333 LOAD_UNLOCK;
1334 avtab_cache_destroy();
1335 return -EINVAL;
1336 }
1337 if (policydb_load_isids(&policydb, &sidtab)) {
1338 LOAD_UNLOCK;
1339 policydb_destroy(&policydb);
1340 avtab_cache_destroy();
1341 return -EINVAL;
1342 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001343 /* Verify that the kernel defined classes are correct. */
1344 if (validate_classes(&policydb)) {
1345 printk(KERN_ERR
1346 "security: the definition of a class is incorrect\n");
1347 LOAD_UNLOCK;
1348 sidtab_destroy(&sidtab);
1349 policydb_destroy(&policydb);
1350 avtab_cache_destroy();
1351 return -EINVAL;
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 policydb_loaded_version = policydb.policyvers;
1354 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001355 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 LOAD_UNLOCK;
1357 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001358 avc_ss_reset(seqno);
1359 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001360 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001361 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return 0;
1363 }
1364
1365#if 0
1366 sidtab_hash_eval(&sidtab, "sids");
1367#endif
1368
1369 if (policydb_read(&newpolicydb, fp)) {
1370 LOAD_UNLOCK;
1371 return -EINVAL;
1372 }
1373
1374 sidtab_init(&newsidtab);
1375
Chad Sellersb94c7e62006-11-06 12:38:18 -05001376 /* Verify that the kernel defined classes are correct. */
1377 if (validate_classes(&newpolicydb)) {
1378 printk(KERN_ERR
1379 "security: the definition of a class is incorrect\n");
1380 rc = -EINVAL;
1381 goto err;
1382 }
1383
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001384 rc = security_preserve_bools(&newpolicydb);
1385 if (rc) {
1386 printk(KERN_ERR "security: unable to preserve booleans\n");
1387 goto err;
1388 }
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* Clone the SID table. */
1391 sidtab_shutdown(&sidtab);
1392 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1393 rc = -ENOMEM;
1394 goto err;
1395 }
1396
1397 /* Convert the internal representations of contexts
1398 in the new SID table and remove invalid SIDs. */
1399 args.oldp = &policydb;
1400 args.newp = &newpolicydb;
1401 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1402
1403 /* Save the old policydb and SID table to free later. */
1404 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1405 sidtab_set(&oldsidtab, &sidtab);
1406
1407 /* Install the new policydb and SID table. */
1408 POLICY_WRLOCK;
1409 memcpy(&policydb, &newpolicydb, sizeof policydb);
1410 sidtab_set(&sidtab, &newsidtab);
1411 seqno = ++latest_granting;
1412 policydb_loaded_version = policydb.policyvers;
1413 POLICY_WRUNLOCK;
1414 LOAD_UNLOCK;
1415
1416 /* Free the old policydb and SID table. */
1417 policydb_destroy(&oldpolicydb);
1418 sidtab_destroy(&oldsidtab);
1419
1420 avc_ss_reset(seqno);
1421 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001422 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001423 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 return 0;
1426
1427err:
1428 LOAD_UNLOCK;
1429 sidtab_destroy(&newsidtab);
1430 policydb_destroy(&newpolicydb);
1431 return rc;
1432
1433}
1434
1435/**
1436 * security_port_sid - Obtain the SID for a port.
1437 * @domain: communication domain aka address family
1438 * @type: socket type
1439 * @protocol: protocol number
1440 * @port: port number
1441 * @out_sid: security identifier
1442 */
1443int security_port_sid(u16 domain,
1444 u16 type,
1445 u8 protocol,
1446 u16 port,
1447 u32 *out_sid)
1448{
1449 struct ocontext *c;
1450 int rc = 0;
1451
1452 POLICY_RDLOCK;
1453
1454 c = policydb.ocontexts[OCON_PORT];
1455 while (c) {
1456 if (c->u.port.protocol == protocol &&
1457 c->u.port.low_port <= port &&
1458 c->u.port.high_port >= port)
1459 break;
1460 c = c->next;
1461 }
1462
1463 if (c) {
1464 if (!c->sid[0]) {
1465 rc = sidtab_context_to_sid(&sidtab,
1466 &c->context[0],
1467 &c->sid[0]);
1468 if (rc)
1469 goto out;
1470 }
1471 *out_sid = c->sid[0];
1472 } else {
1473 *out_sid = SECINITSID_PORT;
1474 }
1475
1476out:
1477 POLICY_RDUNLOCK;
1478 return rc;
1479}
1480
1481/**
1482 * security_netif_sid - Obtain the SID for a network interface.
1483 * @name: interface name
1484 * @if_sid: interface SID
1485 * @msg_sid: default SID for received packets
1486 */
1487int security_netif_sid(char *name,
1488 u32 *if_sid,
1489 u32 *msg_sid)
1490{
1491 int rc = 0;
1492 struct ocontext *c;
1493
1494 POLICY_RDLOCK;
1495
1496 c = policydb.ocontexts[OCON_NETIF];
1497 while (c) {
1498 if (strcmp(name, c->u.name) == 0)
1499 break;
1500 c = c->next;
1501 }
1502
1503 if (c) {
1504 if (!c->sid[0] || !c->sid[1]) {
1505 rc = sidtab_context_to_sid(&sidtab,
1506 &c->context[0],
1507 &c->sid[0]);
1508 if (rc)
1509 goto out;
1510 rc = sidtab_context_to_sid(&sidtab,
1511 &c->context[1],
1512 &c->sid[1]);
1513 if (rc)
1514 goto out;
1515 }
1516 *if_sid = c->sid[0];
1517 *msg_sid = c->sid[1];
1518 } else {
1519 *if_sid = SECINITSID_NETIF;
1520 *msg_sid = SECINITSID_NETMSG;
1521 }
1522
1523out:
1524 POLICY_RDUNLOCK;
1525 return rc;
1526}
1527
1528static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1529{
1530 int i, fail = 0;
1531
1532 for(i = 0; i < 4; i++)
1533 if(addr[i] != (input[i] & mask[i])) {
1534 fail = 1;
1535 break;
1536 }
1537
1538 return !fail;
1539}
1540
1541/**
1542 * security_node_sid - Obtain the SID for a node (host).
1543 * @domain: communication domain aka address family
1544 * @addrp: address
1545 * @addrlen: address length in bytes
1546 * @out_sid: security identifier
1547 */
1548int security_node_sid(u16 domain,
1549 void *addrp,
1550 u32 addrlen,
1551 u32 *out_sid)
1552{
1553 int rc = 0;
1554 struct ocontext *c;
1555
1556 POLICY_RDLOCK;
1557
1558 switch (domain) {
1559 case AF_INET: {
1560 u32 addr;
1561
1562 if (addrlen != sizeof(u32)) {
1563 rc = -EINVAL;
1564 goto out;
1565 }
1566
1567 addr = *((u32 *)addrp);
1568
1569 c = policydb.ocontexts[OCON_NODE];
1570 while (c) {
1571 if (c->u.node.addr == (addr & c->u.node.mask))
1572 break;
1573 c = c->next;
1574 }
1575 break;
1576 }
1577
1578 case AF_INET6:
1579 if (addrlen != sizeof(u64) * 2) {
1580 rc = -EINVAL;
1581 goto out;
1582 }
1583 c = policydb.ocontexts[OCON_NODE6];
1584 while (c) {
1585 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1586 c->u.node6.mask))
1587 break;
1588 c = c->next;
1589 }
1590 break;
1591
1592 default:
1593 *out_sid = SECINITSID_NODE;
1594 goto out;
1595 }
1596
1597 if (c) {
1598 if (!c->sid[0]) {
1599 rc = sidtab_context_to_sid(&sidtab,
1600 &c->context[0],
1601 &c->sid[0]);
1602 if (rc)
1603 goto out;
1604 }
1605 *out_sid = c->sid[0];
1606 } else {
1607 *out_sid = SECINITSID_NODE;
1608 }
1609
1610out:
1611 POLICY_RDUNLOCK;
1612 return rc;
1613}
1614
1615#define SIDS_NEL 25
1616
1617/**
1618 * security_get_user_sids - Obtain reachable SIDs for a user.
1619 * @fromsid: starting SID
1620 * @username: username
1621 * @sids: array of reachable SIDs for user
1622 * @nel: number of elements in @sids
1623 *
1624 * Generate the set of SIDs for legal security contexts
1625 * for a given user that can be reached by @fromsid.
1626 * Set *@sids to point to a dynamically allocated
1627 * array containing the set of SIDs. Set *@nel to the
1628 * number of elements in the array.
1629 */
1630
1631int security_get_user_sids(u32 fromsid,
1632 char *username,
1633 u32 **sids,
1634 u32 *nel)
1635{
1636 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001637 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 u32 mynel = 0, maxnel = SIDS_NEL;
1639 struct user_datum *user;
1640 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001641 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int rc = 0, i, j;
1643
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001644 *sids = NULL;
1645 *nel = 0;
1646
1647 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 POLICY_RDLOCK;
1651
1652 fromcon = sidtab_search(&sidtab, fromsid);
1653 if (!fromcon) {
1654 rc = -EINVAL;
1655 goto out_unlock;
1656 }
1657
1658 user = hashtab_search(policydb.p_users.table, username);
1659 if (!user) {
1660 rc = -EINVAL;
1661 goto out_unlock;
1662 }
1663 usercon.user = user->value;
1664
James Morris89d155e2005-10-30 14:59:21 -08001665 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 if (!mysids) {
1667 rc = -ENOMEM;
1668 goto out_unlock;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Stephen Smalley782ebb92005-09-03 15:55:16 -07001671 ebitmap_for_each_bit(&user->roles, rnode, i) {
1672 if (!ebitmap_node_get_bit(rnode, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 continue;
1674 role = policydb.role_val_to_struct[i];
1675 usercon.role = i+1;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001676 ebitmap_for_each_bit(&role->types, tnode, j) {
1677 if (!ebitmap_node_get_bit(tnode, j))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 continue;
1679 usercon.type = j+1;
1680
1681 if (mls_setup_user_range(fromcon, user, &usercon))
1682 continue;
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001685 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 if (mynel < maxnel) {
1688 mysids[mynel++] = sid;
1689 } else {
1690 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001691 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (!mysids2) {
1693 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 goto out_unlock;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1697 kfree(mysids);
1698 mysids = mysids2;
1699 mysids[mynel++] = sid;
1700 }
1701 }
1702 }
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704out_unlock:
1705 POLICY_RDUNLOCK;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001706 if (rc || !mynel) {
1707 kfree(mysids);
1708 goto out;
1709 }
1710
1711 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1712 if (!mysids2) {
1713 rc = -ENOMEM;
1714 kfree(mysids);
1715 goto out;
1716 }
1717 for (i = 0, j = 0; i < mynel; i++) {
1718 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1719 SECCLASS_PROCESS,
1720 PROCESS__TRANSITION, AVC_STRICT,
1721 NULL);
1722 if (!rc)
1723 mysids2[j++] = mysids[i];
1724 cond_resched();
1725 }
1726 rc = 0;
1727 kfree(mysids);
1728 *sids = mysids2;
1729 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730out:
1731 return rc;
1732}
1733
1734/**
1735 * security_genfs_sid - Obtain a SID for a file in a filesystem
1736 * @fstype: filesystem type
1737 * @path: path from root of mount
1738 * @sclass: file security class
1739 * @sid: SID for path
1740 *
1741 * Obtain a SID to use for a file in a filesystem that
1742 * cannot support xattr or use a fixed labeling behavior like
1743 * transition SIDs or task SIDs.
1744 */
1745int security_genfs_sid(const char *fstype,
1746 char *path,
1747 u16 sclass,
1748 u32 *sid)
1749{
1750 int len;
1751 struct genfs *genfs;
1752 struct ocontext *c;
1753 int rc = 0, cmp = 0;
1754
1755 POLICY_RDLOCK;
1756
1757 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1758 cmp = strcmp(fstype, genfs->fstype);
1759 if (cmp <= 0)
1760 break;
1761 }
1762
1763 if (!genfs || cmp) {
1764 *sid = SECINITSID_UNLABELED;
1765 rc = -ENOENT;
1766 goto out;
1767 }
1768
1769 for (c = genfs->head; c; c = c->next) {
1770 len = strlen(c->u.name);
1771 if ((!c->v.sclass || sclass == c->v.sclass) &&
1772 (strncmp(c->u.name, path, len) == 0))
1773 break;
1774 }
1775
1776 if (!c) {
1777 *sid = SECINITSID_UNLABELED;
1778 rc = -ENOENT;
1779 goto out;
1780 }
1781
1782 if (!c->sid[0]) {
1783 rc = sidtab_context_to_sid(&sidtab,
1784 &c->context[0],
1785 &c->sid[0]);
1786 if (rc)
1787 goto out;
1788 }
1789
1790 *sid = c->sid[0];
1791out:
1792 POLICY_RDUNLOCK;
1793 return rc;
1794}
1795
1796/**
1797 * security_fs_use - Determine how to handle labeling for a filesystem.
1798 * @fstype: filesystem type
1799 * @behavior: labeling behavior
1800 * @sid: SID for filesystem (superblock)
1801 */
1802int security_fs_use(
1803 const char *fstype,
1804 unsigned int *behavior,
1805 u32 *sid)
1806{
1807 int rc = 0;
1808 struct ocontext *c;
1809
1810 POLICY_RDLOCK;
1811
1812 c = policydb.ocontexts[OCON_FSUSE];
1813 while (c) {
1814 if (strcmp(fstype, c->u.name) == 0)
1815 break;
1816 c = c->next;
1817 }
1818
1819 if (c) {
1820 *behavior = c->v.behavior;
1821 if (!c->sid[0]) {
1822 rc = sidtab_context_to_sid(&sidtab,
1823 &c->context[0],
1824 &c->sid[0]);
1825 if (rc)
1826 goto out;
1827 }
1828 *sid = c->sid[0];
1829 } else {
1830 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1831 if (rc) {
1832 *behavior = SECURITY_FS_USE_NONE;
1833 rc = 0;
1834 } else {
1835 *behavior = SECURITY_FS_USE_GENFS;
1836 }
1837 }
1838
1839out:
1840 POLICY_RDUNLOCK;
1841 return rc;
1842}
1843
1844int security_get_bools(int *len, char ***names, int **values)
1845{
1846 int i, rc = -ENOMEM;
1847
1848 POLICY_RDLOCK;
1849 *names = NULL;
1850 *values = NULL;
1851
1852 *len = policydb.p_bools.nprim;
1853 if (!*len) {
1854 rc = 0;
1855 goto out;
1856 }
1857
Jesper Juhle0795cf2006-01-09 20:54:46 -08001858 *names = kcalloc(*len, sizeof(char*), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 if (!*names)
1860 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Jesper Juhle0795cf2006-01-09 20:54:46 -08001862 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 if (!*values)
1864 goto err;
1865
1866 for (i = 0; i < *len; i++) {
1867 size_t name_len;
1868 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1869 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Jesper Juhle0795cf2006-01-09 20:54:46 -08001870 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (!(*names)[i])
1872 goto err;
1873 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1874 (*names)[i][name_len - 1] = 0;
1875 }
1876 rc = 0;
1877out:
1878 POLICY_RDUNLOCK;
1879 return rc;
1880err:
1881 if (*names) {
1882 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001883 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001885 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 goto out;
1887}
1888
1889
1890int security_set_bools(int len, int *values)
1891{
1892 int i, rc = 0;
1893 int lenp, seqno = 0;
1894 struct cond_node *cur;
1895
1896 POLICY_WRLOCK;
1897
1898 lenp = policydb.p_bools.nprim;
1899 if (len != lenp) {
1900 rc = -EFAULT;
1901 goto out;
1902 }
1903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00001905 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1906 audit_log(current->audit_context, GFP_ATOMIC,
1907 AUDIT_MAC_CONFIG_CHANGE,
1908 "bool=%s val=%d old_val=%d auid=%u",
1909 policydb.p_bool_val_to_name[i],
1910 !!values[i],
1911 policydb.bool_val_to_struct[i]->state,
1912 audit_get_loginuid(current->audit_context));
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (values[i]) {
1915 policydb.bool_val_to_struct[i]->state = 1;
1916 } else {
1917 policydb.bool_val_to_struct[i]->state = 0;
1918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1922 rc = evaluate_cond_node(&policydb, cur);
1923 if (rc)
1924 goto out;
1925 }
1926
1927 seqno = ++latest_granting;
1928
1929out:
1930 POLICY_WRUNLOCK;
1931 if (!rc) {
1932 avc_ss_reset(seqno);
1933 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001934 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936 return rc;
1937}
1938
1939int security_get_bool_value(int bool)
1940{
1941 int rc = 0;
1942 int len;
1943
1944 POLICY_RDLOCK;
1945
1946 len = policydb.p_bools.nprim;
1947 if (bool >= len) {
1948 rc = -EFAULT;
1949 goto out;
1950 }
1951
1952 rc = policydb.bool_val_to_struct[bool]->state;
1953out:
1954 POLICY_RDUNLOCK;
1955 return rc;
1956}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06001957
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001958static int security_preserve_bools(struct policydb *p)
1959{
1960 int rc, nbools = 0, *bvalues = NULL, i;
1961 char **bnames = NULL;
1962 struct cond_bool_datum *booldatum;
1963 struct cond_node *cur;
1964
1965 rc = security_get_bools(&nbools, &bnames, &bvalues);
1966 if (rc)
1967 goto out;
1968 for (i = 0; i < nbools; i++) {
1969 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
1970 if (booldatum)
1971 booldatum->state = bvalues[i];
1972 }
1973 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
1974 rc = evaluate_cond_node(p, cur);
1975 if (rc)
1976 goto out;
1977 }
1978
1979out:
1980 if (bnames) {
1981 for (i = 0; i < nbools; i++)
1982 kfree(bnames[i]);
1983 }
1984 kfree(bnames);
1985 kfree(bvalues);
1986 return rc;
1987}
1988
Venkat Yekkirala08554d62006-07-24 23:27:16 -07001989/*
1990 * security_sid_mls_copy() - computes a new sid based on the given
1991 * sid and the mls portion of mls_sid.
1992 */
1993int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
1994{
1995 struct context *context1;
1996 struct context *context2;
1997 struct context newcon;
1998 char *s;
1999 u32 len;
2000 int rc = 0;
2001
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002002 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002003 *new_sid = sid;
2004 goto out;
2005 }
2006
2007 context_init(&newcon);
2008
2009 POLICY_RDLOCK;
2010 context1 = sidtab_search(&sidtab, sid);
2011 if (!context1) {
2012 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
2013 "%d\n", sid);
2014 rc = -EINVAL;
2015 goto out_unlock;
2016 }
2017
2018 context2 = sidtab_search(&sidtab, mls_sid);
2019 if (!context2) {
2020 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
2021 "%d\n", mls_sid);
2022 rc = -EINVAL;
2023 goto out_unlock;
2024 }
2025
2026 newcon.user = context1->user;
2027 newcon.role = context1->role;
2028 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002029 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002030 if (rc)
2031 goto out_unlock;
2032
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002033 /* Check the validity of the new context. */
2034 if (!policydb_context_isvalid(&policydb, &newcon)) {
2035 rc = convert_context_handle_invalid_context(&newcon);
2036 if (rc)
2037 goto bad;
2038 }
2039
2040 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2041 goto out_unlock;
2042
2043bad:
2044 if (!context_struct_to_string(&newcon, &s, &len)) {
2045 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2046 "security_sid_mls_copy: invalid context %s", s);
2047 kfree(s);
2048 }
2049
2050out_unlock:
2051 POLICY_RDUNLOCK;
2052 context_destroy(&newcon);
2053out:
2054 return rc;
2055}
2056
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002057static int get_classes_callback(void *k, void *d, void *args)
2058{
2059 struct class_datum *datum = d;
2060 char *name = k, **classes = args;
2061 int value = datum->value - 1;
2062
2063 classes[value] = kstrdup(name, GFP_ATOMIC);
2064 if (!classes[value])
2065 return -ENOMEM;
2066
2067 return 0;
2068}
2069
2070int security_get_classes(char ***classes, int *nclasses)
2071{
2072 int rc = -ENOMEM;
2073
2074 POLICY_RDLOCK;
2075
2076 *nclasses = policydb.p_classes.nprim;
2077 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2078 if (!*classes)
2079 goto out;
2080
2081 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2082 *classes);
2083 if (rc < 0) {
2084 int i;
2085 for (i = 0; i < *nclasses; i++)
2086 kfree((*classes)[i]);
2087 kfree(*classes);
2088 }
2089
2090out:
2091 POLICY_RDUNLOCK;
2092 return rc;
2093}
2094
2095static int get_permissions_callback(void *k, void *d, void *args)
2096{
2097 struct perm_datum *datum = d;
2098 char *name = k, **perms = args;
2099 int value = datum->value - 1;
2100
2101 perms[value] = kstrdup(name, GFP_ATOMIC);
2102 if (!perms[value])
2103 return -ENOMEM;
2104
2105 return 0;
2106}
2107
2108int security_get_permissions(char *class, char ***perms, int *nperms)
2109{
2110 int rc = -ENOMEM, i;
2111 struct class_datum *match;
2112
2113 POLICY_RDLOCK;
2114
2115 match = hashtab_search(policydb.p_classes.table, class);
2116 if (!match) {
2117 printk(KERN_ERR "%s: unrecognized class %s\n",
2118 __FUNCTION__, class);
2119 rc = -EINVAL;
2120 goto out;
2121 }
2122
2123 *nperms = match->permissions.nprim;
2124 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2125 if (!*perms)
2126 goto out;
2127
2128 if (match->comdatum) {
2129 rc = hashtab_map(match->comdatum->permissions.table,
2130 get_permissions_callback, *perms);
2131 if (rc < 0)
2132 goto err;
2133 }
2134
2135 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2136 *perms);
2137 if (rc < 0)
2138 goto err;
2139
2140out:
2141 POLICY_RDUNLOCK;
2142 return rc;
2143
2144err:
2145 POLICY_RDUNLOCK;
2146 for (i = 0; i < *nperms; i++)
2147 kfree((*perms)[i]);
2148 kfree(*perms);
2149 return rc;
2150}
2151
Eric Paris3f120702007-09-21 14:37:10 -04002152int security_get_reject_unknown(void)
2153{
2154 return policydb.reject_unknown;
2155}
2156
2157int security_get_allow_unknown(void)
2158{
2159 return policydb.allow_unknown;
2160}
2161
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002162struct selinux_audit_rule {
2163 u32 au_seqno;
2164 struct context au_ctxt;
2165};
2166
2167void selinux_audit_rule_free(struct selinux_audit_rule *rule)
2168{
2169 if (rule) {
2170 context_destroy(&rule->au_ctxt);
2171 kfree(rule);
2172 }
2173}
2174
2175int selinux_audit_rule_init(u32 field, u32 op, char *rulestr,
2176 struct selinux_audit_rule **rule)
2177{
2178 struct selinux_audit_rule *tmprule;
2179 struct role_datum *roledatum;
2180 struct type_datum *typedatum;
2181 struct user_datum *userdatum;
2182 int rc = 0;
2183
2184 *rule = NULL;
2185
2186 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002187 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002188
2189 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002190 case AUDIT_SUBJ_USER:
2191 case AUDIT_SUBJ_ROLE:
2192 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002193 case AUDIT_OBJ_USER:
2194 case AUDIT_OBJ_ROLE:
2195 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002196 /* only 'equals' and 'not equals' fit user, role, and type */
2197 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2198 return -EINVAL;
2199 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002200 case AUDIT_SUBJ_SEN:
2201 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002202 case AUDIT_OBJ_LEV_LOW:
2203 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002204 /* we do not allow a range, indicated by the presense of '-' */
2205 if (strchr(rulestr, '-'))
2206 return -EINVAL;
2207 break;
2208 default:
2209 /* only the above fields are valid */
2210 return -EINVAL;
2211 }
2212
2213 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2214 if (!tmprule)
2215 return -ENOMEM;
2216
2217 context_init(&tmprule->au_ctxt);
2218
2219 POLICY_RDLOCK;
2220
2221 tmprule->au_seqno = latest_granting;
2222
2223 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002224 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002225 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002226 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2227 if (!userdatum)
2228 rc = -EINVAL;
2229 else
2230 tmprule->au_ctxt.user = userdatum->value;
2231 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002232 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002233 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002234 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2235 if (!roledatum)
2236 rc = -EINVAL;
2237 else
2238 tmprule->au_ctxt.role = roledatum->value;
2239 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002240 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002241 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002242 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2243 if (!typedatum)
2244 rc = -EINVAL;
2245 else
2246 tmprule->au_ctxt.type = typedatum->value;
2247 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002248 case AUDIT_SUBJ_SEN:
2249 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002250 case AUDIT_OBJ_LEV_LOW:
2251 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002252 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2253 break;
2254 }
2255
2256 POLICY_RDUNLOCK;
2257
2258 if (rc) {
2259 selinux_audit_rule_free(tmprule);
2260 tmprule = NULL;
2261 }
2262
2263 *rule = tmprule;
2264
2265 return rc;
2266}
2267
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002268int selinux_audit_rule_match(u32 sid, u32 field, u32 op,
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002269 struct selinux_audit_rule *rule,
2270 struct audit_context *actx)
2271{
2272 struct context *ctxt;
2273 struct mls_level *level;
2274 int match = 0;
2275
2276 if (!rule) {
2277 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2278 "selinux_audit_rule_match: missing rule\n");
2279 return -ENOENT;
2280 }
2281
2282 POLICY_RDLOCK;
2283
2284 if (rule->au_seqno < latest_granting) {
2285 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2286 "selinux_audit_rule_match: stale rule\n");
2287 match = -ESTALE;
2288 goto out;
2289 }
2290
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002291 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002292 if (!ctxt) {
2293 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2294 "selinux_audit_rule_match: unrecognized SID %d\n",
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002295 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002296 match = -ENOENT;
2297 goto out;
2298 }
2299
2300 /* a field/op pair that is not caught here will simply fall through
2301 without a match */
2302 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002303 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002304 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002305 switch (op) {
2306 case AUDIT_EQUAL:
2307 match = (ctxt->user == rule->au_ctxt.user);
2308 break;
2309 case AUDIT_NOT_EQUAL:
2310 match = (ctxt->user != rule->au_ctxt.user);
2311 break;
2312 }
2313 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002314 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002315 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002316 switch (op) {
2317 case AUDIT_EQUAL:
2318 match = (ctxt->role == rule->au_ctxt.role);
2319 break;
2320 case AUDIT_NOT_EQUAL:
2321 match = (ctxt->role != rule->au_ctxt.role);
2322 break;
2323 }
2324 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002325 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002326 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002327 switch (op) {
2328 case AUDIT_EQUAL:
2329 match = (ctxt->type == rule->au_ctxt.type);
2330 break;
2331 case AUDIT_NOT_EQUAL:
2332 match = (ctxt->type != rule->au_ctxt.type);
2333 break;
2334 }
2335 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002336 case AUDIT_SUBJ_SEN:
2337 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002338 case AUDIT_OBJ_LEV_LOW:
2339 case AUDIT_OBJ_LEV_HIGH:
2340 level = ((field == AUDIT_SUBJ_SEN ||
2341 field == AUDIT_OBJ_LEV_LOW) ?
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002342 &ctxt->range.level[0] : &ctxt->range.level[1]);
2343 switch (op) {
2344 case AUDIT_EQUAL:
2345 match = mls_level_eq(&rule->au_ctxt.range.level[0],
2346 level);
2347 break;
2348 case AUDIT_NOT_EQUAL:
2349 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
2350 level);
2351 break;
2352 case AUDIT_LESS_THAN:
2353 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
2354 level) &&
2355 !mls_level_eq(&rule->au_ctxt.range.level[0],
2356 level));
2357 break;
2358 case AUDIT_LESS_THAN_OR_EQUAL:
2359 match = mls_level_dom(&rule->au_ctxt.range.level[0],
2360 level);
2361 break;
2362 case AUDIT_GREATER_THAN:
2363 match = (mls_level_dom(level,
2364 &rule->au_ctxt.range.level[0]) &&
2365 !mls_level_eq(level,
2366 &rule->au_ctxt.range.level[0]));
2367 break;
2368 case AUDIT_GREATER_THAN_OR_EQUAL:
2369 match = mls_level_dom(level,
2370 &rule->au_ctxt.range.level[0]);
2371 break;
2372 }
2373 }
2374
2375out:
2376 POLICY_RDUNLOCK;
2377 return match;
2378}
2379
2380static int (*aurule_callback)(void) = NULL;
2381
2382static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2383 u16 class, u32 perms, u32 *retained)
2384{
2385 int err = 0;
2386
2387 if (event == AVC_CALLBACK_RESET && aurule_callback)
2388 err = aurule_callback();
2389 return err;
2390}
2391
2392static int __init aurule_init(void)
2393{
2394 int err;
2395
2396 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
2397 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
2398 if (err)
2399 panic("avc_add_callback() failed, error %d\n", err);
2400
2401 return err;
2402}
2403__initcall(aurule_init);
2404
2405void selinux_audit_set_callback(int (*callback)(void))
2406{
2407 aurule_callback = callback;
2408}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002409
2410#ifdef CONFIG_NETLABEL
2411/*
Paul Moore5778eab2007-02-28 15:14:22 -05002412 * NetLabel cache structure
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002413 */
Paul Moore5778eab2007-02-28 15:14:22 -05002414#define NETLBL_CACHE(x) ((struct selinux_netlbl_cache *)(x))
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002415#define NETLBL_CACHE_T_NONE 0
2416#define NETLBL_CACHE_T_SID 1
2417#define NETLBL_CACHE_T_MLS 2
Paul Moore5778eab2007-02-28 15:14:22 -05002418struct selinux_netlbl_cache {
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002419 u32 type;
2420 union {
2421 u32 sid;
2422 struct mls_range mls_label;
2423 } data;
2424};
2425
2426/**
Paul Moore5778eab2007-02-28 15:14:22 -05002427 * security_netlbl_cache_free - Free the NetLabel cached data
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002428 * @data: the data to free
2429 *
2430 * Description:
2431 * This function is intended to be used as the free() callback inside the
2432 * netlbl_lsm_cache structure.
2433 *
2434 */
Paul Moore5778eab2007-02-28 15:14:22 -05002435static void security_netlbl_cache_free(const void *data)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002436{
Paul Moore5778eab2007-02-28 15:14:22 -05002437 struct selinux_netlbl_cache *cache;
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002438
2439 if (data == NULL)
2440 return;
2441
2442 cache = NETLBL_CACHE(data);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002443 switch (cache->type) {
2444 case NETLBL_CACHE_T_MLS:
2445 ebitmap_destroy(&cache->data.mls_label.level[0].cat);
2446 break;
2447 }
2448 kfree(data);
2449}
2450
2451/**
Paul Moore5778eab2007-02-28 15:14:22 -05002452 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2453 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002454 * @ctx: the SELinux context
2455 *
2456 * Description:
2457 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002458 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2459 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002460 *
2461 */
Paul Moore5778eab2007-02-28 15:14:22 -05002462static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
2463 struct context *ctx)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002464{
Paul Moore5778eab2007-02-28 15:14:22 -05002465 struct selinux_netlbl_cache *cache = NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002466
Paul Moore5778eab2007-02-28 15:14:22 -05002467 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
2468 if (secattr->cache == NULL)
2469 return;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002470
2471 cache = kzalloc(sizeof(*cache), GFP_ATOMIC);
2472 if (cache == NULL)
Paul Moore5778eab2007-02-28 15:14:22 -05002473 return;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002474
2475 cache->type = NETLBL_CACHE_T_MLS;
2476 if (ebitmap_cpy(&cache->data.mls_label.level[0].cat,
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002477 &ctx->range.level[0].cat) != 0) {
2478 kfree(cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002479 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002480 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002481 cache->data.mls_label.level[1].cat.highbit =
2482 cache->data.mls_label.level[0].cat.highbit;
2483 cache->data.mls_label.level[1].cat.node =
2484 cache->data.mls_label.level[0].cat.node;
2485 cache->data.mls_label.level[0].sens = ctx->range.level[0].sens;
2486 cache->data.mls_label.level[1].sens = ctx->range.level[0].sens;
2487
Paul Moore5778eab2007-02-28 15:14:22 -05002488 secattr->cache->free = security_netlbl_cache_free;
2489 secattr->cache->data = (void *)cache;
2490 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002491}
2492
2493/**
Paul Moore5778eab2007-02-28 15:14:22 -05002494 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002495 * @secattr: the NetLabel packet security attributes
2496 * @base_sid: the SELinux SID to use as a context for MLS only attributes
2497 * @sid: the SELinux SID
2498 *
2499 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002500 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002501 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5778eab2007-02-28 15:14:22 -05002502 * SID/context then use the context in @base_sid as the foundation. If
2503 * possibile the 'cache' field of @secattr is set and the CACHE flag is set;
2504 * this is to allow the @secattr to be used by NetLabel to cache the secattr to
2505 * SID conversion for future lookups. Returns zero on success, negative
2506 * values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002507 *
2508 */
Paul Moore5778eab2007-02-28 15:14:22 -05002509int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
2510 u32 base_sid,
2511 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002512{
2513 int rc = -EIDRM;
2514 struct context *ctx;
2515 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002516 struct selinux_netlbl_cache *cache;
2517
2518 if (!ss_initialized) {
2519 *sid = SECSID_NULL;
2520 return 0;
2521 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002522
2523 POLICY_RDLOCK;
2524
Paul Moore701a90b2006-11-17 17:38:46 -05002525 if (secattr->flags & NETLBL_SECATTR_CACHE) {
paul.moore@hp.comffb733c2006-10-04 11:46:31 -04002526 cache = NETLBL_CACHE(secattr->cache->data);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002527 switch (cache->type) {
2528 case NETLBL_CACHE_T_SID:
2529 *sid = cache->data.sid;
2530 rc = 0;
2531 break;
2532 case NETLBL_CACHE_T_MLS:
2533 ctx = sidtab_search(&sidtab, base_sid);
2534 if (ctx == NULL)
2535 goto netlbl_secattr_to_sid_return;
2536
2537 ctx_new.user = ctx->user;
2538 ctx_new.role = ctx->role;
2539 ctx_new.type = ctx->type;
2540 ctx_new.range.level[0].sens =
2541 cache->data.mls_label.level[0].sens;
2542 ctx_new.range.level[0].cat.highbit =
2543 cache->data.mls_label.level[0].cat.highbit;
2544 ctx_new.range.level[0].cat.node =
2545 cache->data.mls_label.level[0].cat.node;
2546 ctx_new.range.level[1].sens =
2547 cache->data.mls_label.level[1].sens;
2548 ctx_new.range.level[1].cat.highbit =
2549 cache->data.mls_label.level[1].cat.highbit;
2550 ctx_new.range.level[1].cat.node =
2551 cache->data.mls_label.level[1].cat.node;
2552
2553 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2554 break;
2555 default:
2556 goto netlbl_secattr_to_sid_return;
2557 }
Paul Moore701a90b2006-11-17 17:38:46 -05002558 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002559 ctx = sidtab_search(&sidtab, base_sid);
2560 if (ctx == NULL)
2561 goto netlbl_secattr_to_sid_return;
2562
2563 ctx_new.user = ctx->user;
2564 ctx_new.role = ctx->role;
2565 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002566 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002567 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002568 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
2569 secattr->mls_cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002570 goto netlbl_secattr_to_sid_return;
2571 ctx_new.range.level[1].cat.highbit =
2572 ctx_new.range.level[0].cat.highbit;
2573 ctx_new.range.level[1].cat.node =
2574 ctx_new.range.level[0].cat.node;
2575 } else {
2576 ebitmap_init(&ctx_new.range.level[0].cat);
2577 ebitmap_init(&ctx_new.range.level[1].cat);
2578 }
2579 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2580 goto netlbl_secattr_to_sid_return_cleanup;
2581
2582 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2583 if (rc != 0)
2584 goto netlbl_secattr_to_sid_return_cleanup;
2585
Paul Moore5778eab2007-02-28 15:14:22 -05002586 security_netlbl_cache_add(secattr, &ctx_new);
2587
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002588 ebitmap_destroy(&ctx_new.range.level[0].cat);
2589 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002590 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002591 rc = 0;
2592 }
2593
2594netlbl_secattr_to_sid_return:
2595 POLICY_RDUNLOCK;
2596 return rc;
2597netlbl_secattr_to_sid_return_cleanup:
2598 ebitmap_destroy(&ctx_new.range.level[0].cat);
2599 goto netlbl_secattr_to_sid_return;
2600}
2601
2602/**
Paul Moore5778eab2007-02-28 15:14:22 -05002603 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2604 * @sid: the SELinux SID
2605 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002606 *
2607 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002608 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2609 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002610 *
2611 */
Paul Moore5778eab2007-02-28 15:14:22 -05002612int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002613{
2614 int rc = -ENOENT;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002615 struct context *ctx;
2616
Paul Moore5778eab2007-02-28 15:14:22 -05002617 netlbl_secattr_init(secattr);
2618
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002619 if (!ss_initialized)
2620 return 0;
2621
2622 POLICY_RDLOCK;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002623 ctx = sidtab_search(&sidtab, sid);
2624 if (ctx == NULL)
Paul Moore5778eab2007-02-28 15:14:22 -05002625 goto netlbl_sid_to_secattr_failure;
2626 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2627 GFP_ATOMIC);
2628 secattr->flags |= NETLBL_SECATTR_DOMAIN;
2629 mls_export_netlbl_lvl(ctx, secattr);
2630 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002631 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05002632 goto netlbl_sid_to_secattr_failure;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002633 POLICY_RDUNLOCK;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002634
Paul Moore5778eab2007-02-28 15:14:22 -05002635 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05002636
Paul Moore5778eab2007-02-28 15:14:22 -05002637netlbl_sid_to_secattr_failure:
2638 POLICY_RDUNLOCK;
2639 netlbl_secattr_destroy(secattr);
Paul Mooref8687af2006-10-30 15:22:15 -08002640 return rc;
2641}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002642#endif /* CONFIG_NETLABEL */