blob: fc3dfca475d66fece3a78edf99581628592082ce [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>
Eric Paris5d55a342008-04-18 17:38:33 -04005 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 *
Eric Paris5d55a342008-04-18 17:38:33 -040014 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070016 * Updated: Hewlett-Packard <paul.moore@hp.com>
17 *
18 * Added support for NetLabel
Paul Moore3bb56b22008-01-29 08:38:19 -050019 * Added support for the policy capability bitmap
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070020 *
Chad Sellersb94c7e62006-11-06 12:38:18 -050021 * Updated: Chad Sellers <csellers@tresys.com>
22 *
23 * Added validation of kernel classes and permissions
24 *
Paul Moore3bb56b22008-01-29 08:38:19 -050025 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
Darrel Goeddel376bd9c2006-02-24 15:44:05 -060026 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
Chad Sellersb94c7e62006-11-06 12:38:18 -050027 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
29 * This program is free software; you can redistribute it and/or modify
Eric Paris5d55a342008-04-18 17:38:33 -040030 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * the Free Software Foundation, version 2.
32 */
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/string.h>
36#include <linux/spinlock.h>
Paul Moore9f2ad662006-11-17 17:38:53 -050037#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/errno.h>
39#include <linux/in.h>
40#include <linux/sched.h>
41#include <linux/audit.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080042#include <linux/mutex.h>
Adrian Bunk0e55a002008-03-31 01:54:02 +030043#include <linux/selinux.h>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070044#include <net/netlabel.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "flask.h"
47#include "avc.h"
48#include "avc_ss.h"
49#include "security.h"
50#include "context.h"
51#include "policydb.h"
52#include "sidtab.h"
53#include "services.h"
54#include "conditional.h"
55#include "mls.h"
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070056#include "objsec.h"
Paul Moorec60475b2007-02-28 15:14:23 -050057#include "netlabel.h"
Paul Moore3de4bab2006-11-17 17:38:54 -050058#include "xfrm.h"
Paul Moore02752762006-11-29 13:18:18 -050059#include "ebitmap.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020060#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62extern void selnl_notify_policyload(u32 seqno);
63unsigned int policydb_loaded_version;
64
Paul Moore3bb56b22008-01-29 08:38:19 -050065int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050066int selinux_policycap_openperm;
Paul Moore3bb56b22008-01-29 08:38:19 -050067
Chad Sellersb94c7e62006-11-06 12:38:18 -050068/*
69 * This is declared in avc.c
70 */
71extern const struct selinux_class_perm selinux_class_perm;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static DEFINE_RWLOCK(policy_rwlock);
74#define POLICY_RDLOCK read_lock(&policy_rwlock)
75#define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
76#define POLICY_RDUNLOCK read_unlock(&policy_rwlock)
77#define POLICY_WRUNLOCK write_unlock_irq(&policy_rwlock)
78
Ingo Molnarbb0030792006-03-22 00:09:14 -080079static DEFINE_MUTEX(load_mutex);
80#define LOAD_LOCK mutex_lock(&load_mutex)
81#define LOAD_UNLOCK mutex_unlock(&load_mutex)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83static struct sidtab sidtab;
84struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040085int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/*
88 * The largest sequence number that has been used when
89 * providing an access decision to the access vector cache.
90 * The sequence number only changes when a policy change
91 * occurs.
92 */
Eric Paris5d55a342008-04-18 17:38:33 -040093static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* Forward declaration. */
96static int context_struct_to_string(struct context *context, char **scontext,
97 u32 *scontext_len);
98
99/*
100 * Return the boolean value of a constraint expression
101 * when it is applied to the specified source and target
102 * security contexts.
103 *
104 * xcontext is a special beast... It is used by the validatetrans rules
105 * only. For these rules, scontext is the context before the transition,
106 * tcontext is the context after the transition, and xcontext is the context
107 * of the process performing the transition. All other callers of
108 * constraint_expr_eval should pass in NULL for xcontext.
109 */
110static int constraint_expr_eval(struct context *scontext,
111 struct context *tcontext,
112 struct context *xcontext,
113 struct constraint_expr *cexpr)
114{
115 u32 val1, val2;
116 struct context *c;
117 struct role_datum *r1, *r2;
118 struct mls_level *l1, *l2;
119 struct constraint_expr *e;
120 int s[CEXPR_MAXDEPTH];
121 int sp = -1;
122
123 for (e = cexpr; e; e = e->next) {
124 switch (e->expr_type) {
125 case CEXPR_NOT:
126 BUG_ON(sp < 0);
127 s[sp] = !s[sp];
128 break;
129 case CEXPR_AND:
130 BUG_ON(sp < 1);
131 sp--;
132 s[sp] &= s[sp+1];
133 break;
134 case CEXPR_OR:
135 BUG_ON(sp < 1);
136 sp--;
137 s[sp] |= s[sp+1];
138 break;
139 case CEXPR_ATTR:
140 if (sp == (CEXPR_MAXDEPTH-1))
141 return 0;
142 switch (e->attr) {
143 case CEXPR_USER:
144 val1 = scontext->user;
145 val2 = tcontext->user;
146 break;
147 case CEXPR_TYPE:
148 val1 = scontext->type;
149 val2 = tcontext->type;
150 break;
151 case CEXPR_ROLE:
152 val1 = scontext->role;
153 val2 = tcontext->role;
154 r1 = policydb.role_val_to_struct[val1 - 1];
155 r2 = policydb.role_val_to_struct[val2 - 1];
156 switch (e->op) {
157 case CEXPR_DOM:
158 s[++sp] = ebitmap_get_bit(&r1->dominates,
159 val2 - 1);
160 continue;
161 case CEXPR_DOMBY:
162 s[++sp] = ebitmap_get_bit(&r2->dominates,
163 val1 - 1);
164 continue;
165 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400166 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
167 val2 - 1) &&
168 !ebitmap_get_bit(&r2->dominates,
169 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 continue;
171 default:
172 break;
173 }
174 break;
175 case CEXPR_L1L2:
176 l1 = &(scontext->range.level[0]);
177 l2 = &(tcontext->range.level[0]);
178 goto mls_ops;
179 case CEXPR_L1H2:
180 l1 = &(scontext->range.level[0]);
181 l2 = &(tcontext->range.level[1]);
182 goto mls_ops;
183 case CEXPR_H1L2:
184 l1 = &(scontext->range.level[1]);
185 l2 = &(tcontext->range.level[0]);
186 goto mls_ops;
187 case CEXPR_H1H2:
188 l1 = &(scontext->range.level[1]);
189 l2 = &(tcontext->range.level[1]);
190 goto mls_ops;
191 case CEXPR_L1H1:
192 l1 = &(scontext->range.level[0]);
193 l2 = &(scontext->range.level[1]);
194 goto mls_ops;
195 case CEXPR_L2H2:
196 l1 = &(tcontext->range.level[0]);
197 l2 = &(tcontext->range.level[1]);
198 goto mls_ops;
199mls_ops:
200 switch (e->op) {
201 case CEXPR_EQ:
202 s[++sp] = mls_level_eq(l1, l2);
203 continue;
204 case CEXPR_NEQ:
205 s[++sp] = !mls_level_eq(l1, l2);
206 continue;
207 case CEXPR_DOM:
208 s[++sp] = mls_level_dom(l1, l2);
209 continue;
210 case CEXPR_DOMBY:
211 s[++sp] = mls_level_dom(l2, l1);
212 continue;
213 case CEXPR_INCOMP:
214 s[++sp] = mls_level_incomp(l2, l1);
215 continue;
216 default:
217 BUG();
218 return 0;
219 }
220 break;
221 default:
222 BUG();
223 return 0;
224 }
225
226 switch (e->op) {
227 case CEXPR_EQ:
228 s[++sp] = (val1 == val2);
229 break;
230 case CEXPR_NEQ:
231 s[++sp] = (val1 != val2);
232 break;
233 default:
234 BUG();
235 return 0;
236 }
237 break;
238 case CEXPR_NAMES:
239 if (sp == (CEXPR_MAXDEPTH-1))
240 return 0;
241 c = scontext;
242 if (e->attr & CEXPR_TARGET)
243 c = tcontext;
244 else if (e->attr & CEXPR_XTARGET) {
245 c = xcontext;
246 if (!c) {
247 BUG();
248 return 0;
249 }
250 }
251 if (e->attr & CEXPR_USER)
252 val1 = c->user;
253 else if (e->attr & CEXPR_ROLE)
254 val1 = c->role;
255 else if (e->attr & CEXPR_TYPE)
256 val1 = c->type;
257 else {
258 BUG();
259 return 0;
260 }
261
262 switch (e->op) {
263 case CEXPR_EQ:
264 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
265 break;
266 case CEXPR_NEQ:
267 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
268 break;
269 default:
270 BUG();
271 return 0;
272 }
273 break;
274 default:
275 BUG();
276 return 0;
277 }
278 }
279
280 BUG_ON(sp != 0);
281 return s[0];
282}
283
284/*
285 * Compute access vectors based on a context structure pair for
286 * the permissions in a particular class.
287 */
288static int context_struct_compute_av(struct context *scontext,
289 struct context *tcontext,
290 u16 tclass,
291 u32 requested,
292 struct av_decision *avd)
293{
294 struct constraint_node *constraint;
295 struct role_allow *ra;
296 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700297 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700299 struct ebitmap *sattr, *tattr;
300 struct ebitmap_node *snode, *tnode;
Eric Paris3f120702007-09-21 14:37:10 -0400301 const struct selinux_class_perm *kdefs = &selinux_class_perm;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700302 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /*
305 * Remap extended Netlink classes for old policy versions.
306 * Do this here rather than socket_type_to_security_class()
307 * in case a newer policy version is loaded, allowing sockets
308 * to remain in the correct class.
309 */
310 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
311 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
312 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
313 tclass = SECCLASS_NETLINK_SOCKET;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * Initialize the access vectors to the default values.
317 */
318 avd->allowed = 0;
319 avd->decided = 0xffffffff;
320 avd->auditallow = 0;
321 avd->auditdeny = 0xffffffff;
322 avd->seqno = latest_granting;
323
324 /*
Eric Paris3f120702007-09-21 14:37:10 -0400325 * Check for all the invalid cases.
326 * - tclass 0
327 * - tclass > policy and > kernel
328 * - tclass > policy but is a userspace class
329 * - tclass > policy but we do not allow unknowns
330 */
331 if (unlikely(!tclass))
332 goto inval_class;
333 if (unlikely(tclass > policydb.p_classes.nprim))
334 if (tclass > kdefs->cts_len ||
335 !kdefs->class_to_string[tclass - 1] ||
336 !policydb.allow_unknown)
337 goto inval_class;
338
339 /*
340 * Kernel class and we allow unknown so pad the allow decision
341 * the pad will be all 1 for unknown classes.
342 */
343 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
344 avd->allowed = policydb.undefined_perms[tclass - 1];
345
346 /*
347 * Not in policy. Since decision is completed (all 1 or all 0) return.
348 */
349 if (unlikely(tclass > policydb.p_classes.nprim))
350 return 0;
351
352 tclass_datum = policydb.class_val_to_struct[tclass - 1];
353
354 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 * If a specific type enforcement rule was defined for
356 * this permission check, then use it.
357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700359 avkey.specified = AVTAB_AV;
360 sattr = &policydb.type_attr_map[scontext->type - 1];
361 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900362 ebitmap_for_each_positive_bit(sattr, snode, i) {
363 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700364 avkey.source_type = i + 1;
365 avkey.target_type = j + 1;
366 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
367 node != NULL;
368 node = avtab_search_node_next(node, avkey.specified)) {
369 if (node->key.specified == AVTAB_ALLOWED)
370 avd->allowed |= node->datum.data;
371 else if (node->key.specified == AVTAB_AUDITALLOW)
372 avd->auditallow |= node->datum.data;
373 else if (node->key.specified == AVTAB_AUDITDENY)
374 avd->auditdeny &= node->datum.data;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Stephen Smalley782ebb92005-09-03 15:55:16 -0700377 /* Check conditional av table for additional permissions */
378 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
379
380 }
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /*
384 * Remove any permissions prohibited by a constraint (this includes
385 * the MLS policy).
386 */
387 constraint = tclass_datum->constraints;
388 while (constraint) {
389 if ((constraint->permissions & (avd->allowed)) &&
390 !constraint_expr_eval(scontext, tcontext, NULL,
391 constraint->expr)) {
392 avd->allowed = (avd->allowed) & ~(constraint->permissions);
393 }
394 constraint = constraint->next;
395 }
396
397 /*
398 * If checking process transition permission and the
399 * role is changing, then check the (current_role, new_role)
400 * pair.
401 */
402 if (tclass == SECCLASS_PROCESS &&
403 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
404 scontext->role != tcontext->role) {
405 for (ra = policydb.role_allow; ra; ra = ra->next) {
406 if (scontext->role == ra->role &&
407 tcontext->role == ra->new_role)
408 break;
409 }
410 if (!ra)
411 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
Eric Paris5d55a342008-04-18 17:38:33 -0400412 PROCESS__DYNTRANSITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414
415 return 0;
Eric Paris3f120702007-09-21 14:37:10 -0400416
417inval_class:
Harvey Harrisondd6f9532008-03-06 10:03:59 +1100418 printk(KERN_ERR "%s: unrecognized class %d\n", __func__, tclass);
Eric Paris3f120702007-09-21 14:37:10 -0400419 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Eric Paris64dbf072008-03-31 12:17:33 +1100422/*
423 * Given a sid find if the type has the permissive flag set
424 */
425int security_permissive_sid(u32 sid)
426{
427 struct context *context;
428 u32 type;
429 int rc;
430
431 POLICY_RDLOCK;
432
433 context = sidtab_search(&sidtab, sid);
434 BUG_ON(!context);
435
436 type = context->type;
437 /*
438 * we are intentionally using type here, not type-1, the 0th bit may
439 * someday indicate that we are globally setting permissive in policy.
440 */
441 rc = ebitmap_get_bit(&policydb.permissive_map, type);
442
443 POLICY_RDUNLOCK;
444 return rc;
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400448 struct context *ncontext,
449 struct context *tcontext,
450 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 char *o = NULL, *n = NULL, *t = NULL;
453 u32 olen, nlen, tlen;
454
455 if (context_struct_to_string(ocontext, &o, &olen) < 0)
456 goto out;
457 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
458 goto out;
459 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
460 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100461 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400462 "security_validate_transition: denied for"
463 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
464 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465out:
466 kfree(o);
467 kfree(n);
468 kfree(t);
469
470 if (!selinux_enforcing)
471 return 0;
472 return -EPERM;
473}
474
475int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Eric Paris5d55a342008-04-18 17:38:33 -0400476 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 struct context *ocontext;
479 struct context *ncontext;
480 struct context *tcontext;
481 struct class_datum *tclass_datum;
482 struct constraint_node *constraint;
483 int rc = 0;
484
485 if (!ss_initialized)
486 return 0;
487
488 POLICY_RDLOCK;
489
490 /*
491 * Remap extended Netlink classes for old policy versions.
492 * Do this here rather than socket_type_to_security_class()
493 * in case a newer policy version is loaded, allowing sockets
494 * to remain in the correct class.
495 */
496 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
497 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
498 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
499 tclass = SECCLASS_NETLINK_SOCKET;
500
501 if (!tclass || tclass > policydb.p_classes.nprim) {
502 printk(KERN_ERR "security_validate_transition: "
503 "unrecognized class %d\n", tclass);
504 rc = -EINVAL;
505 goto out;
506 }
507 tclass_datum = policydb.class_val_to_struct[tclass - 1];
508
509 ocontext = sidtab_search(&sidtab, oldsid);
510 if (!ocontext) {
511 printk(KERN_ERR "security_validate_transition: "
512 " unrecognized SID %d\n", oldsid);
513 rc = -EINVAL;
514 goto out;
515 }
516
517 ncontext = sidtab_search(&sidtab, newsid);
518 if (!ncontext) {
519 printk(KERN_ERR "security_validate_transition: "
520 " unrecognized SID %d\n", newsid);
521 rc = -EINVAL;
522 goto out;
523 }
524
525 tcontext = sidtab_search(&sidtab, tasksid);
526 if (!tcontext) {
527 printk(KERN_ERR "security_validate_transition: "
528 " unrecognized SID %d\n", tasksid);
529 rc = -EINVAL;
530 goto out;
531 }
532
533 constraint = tclass_datum->validatetrans;
534 while (constraint) {
535 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400536 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400538 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto out;
540 }
541 constraint = constraint->next;
542 }
543
544out:
545 POLICY_RDUNLOCK;
546 return rc;
547}
548
549/**
550 * security_compute_av - Compute access vector decisions.
551 * @ssid: source security identifier
552 * @tsid: target security identifier
553 * @tclass: target security class
554 * @requested: requested permissions
555 * @avd: access vector decisions
556 *
557 * Compute a set of access vector decisions based on the
558 * SID pair (@ssid, @tsid) for the permissions in @tclass.
559 * Return -%EINVAL if any of the parameters are invalid or %0
560 * if the access vector decisions were computed successfully.
561 */
562int security_compute_av(u32 ssid,
563 u32 tsid,
564 u16 tclass,
565 u32 requested,
566 struct av_decision *avd)
567{
568 struct context *scontext = NULL, *tcontext = NULL;
569 int rc = 0;
570
571 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700572 avd->allowed = 0xffffffff;
573 avd->decided = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 avd->auditallow = 0;
575 avd->auditdeny = 0xffffffff;
576 avd->seqno = latest_granting;
577 return 0;
578 }
579
580 POLICY_RDLOCK;
581
582 scontext = sidtab_search(&sidtab, ssid);
583 if (!scontext) {
584 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
585 ssid);
586 rc = -EINVAL;
587 goto out;
588 }
589 tcontext = sidtab_search(&sidtab, tsid);
590 if (!tcontext) {
591 printk(KERN_ERR "security_compute_av: unrecognized SID %d\n",
592 tsid);
593 rc = -EINVAL;
594 goto out;
595 }
596
597 rc = context_struct_compute_av(scontext, tcontext, tclass,
598 requested, avd);
599out:
600 POLICY_RDUNLOCK;
601 return rc;
602}
603
604/*
605 * Write the security context string representation of
606 * the context structure `context' into a dynamically
607 * allocated string of the correct size. Set `*scontext'
608 * to point to this string and set `*scontext_len' to
609 * the length of the string.
610 */
611static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
612{
613 char *scontextp;
614
615 *scontext = NULL;
616 *scontext_len = 0;
617
618 /* Compute the size of the context. */
619 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
620 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
621 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
622 *scontext_len += mls_compute_context_len(context);
623
624 /* Allocate space for the context; caller must free this space. */
625 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400626 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 *scontext = scontextp;
629
630 /*
631 * Copy the user name, role name and type name into the context.
632 */
633 sprintf(scontextp, "%s:%s:%s",
634 policydb.p_user_val_to_name[context->user - 1],
635 policydb.p_role_val_to_name[context->role - 1],
636 policydb.p_type_val_to_name[context->type - 1]);
637 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400638 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
639 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 mls_sid_to_context(context, &scontextp);
642
643 *scontextp = 0;
644
645 return 0;
646}
647
648#include "initial_sid_to_string.h"
649
James Carterf0ee2e42007-04-04 10:11:29 -0400650const char *security_get_initial_sid_context(u32 sid)
651{
652 if (unlikely(sid > SECINITSID_NUM))
653 return NULL;
654 return initial_sid_to_string[sid];
655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/**
658 * security_sid_to_context - Obtain a context for a given SID.
659 * @sid: security identifier, SID
660 * @scontext: security context
661 * @scontext_len: length in bytes
662 *
663 * Write the string representation of the context associated with @sid
664 * into a dynamically allocated string of the correct size. Set @scontext
665 * to point to this string and set @scontext_len to the length of the string.
666 */
667int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
668{
669 struct context *context;
670 int rc = 0;
671
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500672 *scontext = NULL;
673 *scontext_len = 0;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (!ss_initialized) {
676 if (sid <= SECINITSID_NUM) {
677 char *scontextp;
678
679 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -0400680 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -0700681 if (!scontextp) {
682 rc = -ENOMEM;
683 goto out;
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 strcpy(scontextp, initial_sid_to_string[sid]);
686 *scontext = scontextp;
687 goto out;
688 }
689 printk(KERN_ERR "security_sid_to_context: called before initial "
690 "load_policy on unknown SID %d\n", sid);
691 rc = -EINVAL;
692 goto out;
693 }
694 POLICY_RDLOCK;
695 context = sidtab_search(&sidtab, sid);
696 if (!context) {
697 printk(KERN_ERR "security_sid_to_context: unrecognized SID "
698 "%d\n", sid);
699 rc = -EINVAL;
700 goto out_unlock;
701 }
702 rc = context_struct_to_string(context, scontext, scontext_len);
703out_unlock:
704 POLICY_RDUNLOCK;
705out:
706 return rc;
707
708}
709
Stephen Smalley869ab512008-04-04 08:46:05 -0400710static int security_context_to_sid_core(char *scontext, u32 scontext_len,
711 u32 *sid, u32 def_sid, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 char *scontext2;
714 struct context context;
715 struct role_datum *role;
716 struct type_datum *typdatum;
717 struct user_datum *usrdatum;
718 char *scontextp, *p, oldc;
719 int rc = 0;
720
721 if (!ss_initialized) {
722 int i;
723
724 for (i = 1; i < SECINITSID_NUM; i++) {
725 if (!strcmp(initial_sid_to_string[i], scontext)) {
726 *sid = i;
727 goto out;
728 }
729 }
730 *sid = SECINITSID_KERNEL;
731 goto out;
732 }
733 *sid = SECSID_NULL;
734
735 /* Copy the string so that we can modify the copy as we parse it.
736 The string should already by null terminated, but we append a
737 null suffix to the copy to avoid problems with the existing
738 attr package, which doesn't view the null terminator as part
739 of the attribute value. */
Stephen Smalley869ab512008-04-04 08:46:05 -0400740 scontext2 = kmalloc(scontext_len+1, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!scontext2) {
742 rc = -ENOMEM;
743 goto out;
744 }
745 memcpy(scontext2, scontext, scontext_len);
746 scontext2[scontext_len] = 0;
747
748 context_init(&context);
749 *sid = SECSID_NULL;
750
751 POLICY_RDLOCK;
752
753 /* Parse the security context. */
754
755 rc = -EINVAL;
756 scontextp = (char *) scontext2;
757
758 /* Extract the user. */
759 p = scontextp;
760 while (*p && *p != ':')
761 p++;
762
763 if (*p == 0)
764 goto out_unlock;
765
766 *p++ = 0;
767
768 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
769 if (!usrdatum)
770 goto out_unlock;
771
772 context.user = usrdatum->value;
773
774 /* Extract role. */
775 scontextp = p;
776 while (*p && *p != ':')
777 p++;
778
779 if (*p == 0)
780 goto out_unlock;
781
782 *p++ = 0;
783
784 role = hashtab_search(policydb.p_roles.table, scontextp);
785 if (!role)
786 goto out_unlock;
787 context.role = role->value;
788
789 /* Extract type. */
790 scontextp = p;
791 while (*p && *p != ':')
792 p++;
793 oldc = *p;
794 *p++ = 0;
795
796 typdatum = hashtab_search(policydb.p_types.table, scontextp);
797 if (!typdatum)
798 goto out_unlock;
799
800 context.type = typdatum->value;
801
James Morrisf5c1d5b2005-07-28 01:07:37 -0700802 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (rc)
804 goto out_unlock;
805
806 if ((p - scontext2) < scontext_len) {
807 rc = -EINVAL;
808 goto out_unlock;
809 }
810
811 /* Check the validity of the new context. */
812 if (!policydb_context_isvalid(&policydb, &context)) {
813 rc = -EINVAL;
814 goto out_unlock;
815 }
816 /* Obtain the new sid. */
817 rc = sidtab_context_to_sid(&sidtab, &context, sid);
818out_unlock:
819 POLICY_RDUNLOCK;
820 context_destroy(&context);
821 kfree(scontext2);
822out:
823 return rc;
824}
825
James Morrisf5c1d5b2005-07-28 01:07:37 -0700826/**
827 * security_context_to_sid - Obtain a SID for a given security context.
828 * @scontext: security context
829 * @scontext_len: length in bytes
830 * @sid: security identifier, SID
831 *
832 * Obtains a SID associated with the security context that
833 * has the string representation specified by @scontext.
834 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
835 * memory is available, or 0 on success.
836 */
837int security_context_to_sid(char *scontext, u32 scontext_len, u32 *sid)
838{
839 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley869ab512008-04-04 08:46:05 -0400840 sid, SECSID_NULL, GFP_KERNEL);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700841}
842
843/**
844 * security_context_to_sid_default - Obtain a SID for a given security context,
845 * falling back to specified default if needed.
846 *
847 * @scontext: security context
848 * @scontext_len: length in bytes
849 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -0700850 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -0700851 *
852 * Obtains a SID associated with the security context that
853 * has the string representation specified by @scontext.
854 * The default SID is passed to the MLS layer to be used to allow
855 * kernel labeling of the MLS field if the MLS field is not present
856 * (for upgrading to MLS without full relabel).
857 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
858 * memory is available, or 0 on success.
859 */
Stephen Smalley869ab512008-04-04 08:46:05 -0400860int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid,
861 u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700862{
863 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley869ab512008-04-04 08:46:05 -0400864 sid, def_sid, gfp_flags);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867static int compute_sid_handle_invalid_context(
868 struct context *scontext,
869 struct context *tcontext,
870 u16 tclass,
871 struct context *newcontext)
872{
873 char *s = NULL, *t = NULL, *n = NULL;
874 u32 slen, tlen, nlen;
875
876 if (context_struct_to_string(scontext, &s, &slen) < 0)
877 goto out;
878 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
879 goto out;
880 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
881 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100882 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 "security_compute_sid: invalid context %s"
884 " for scontext=%s"
885 " tcontext=%s"
886 " tclass=%s",
887 n, s, t, policydb.p_class_val_to_name[tclass-1]);
888out:
889 kfree(s);
890 kfree(t);
891 kfree(n);
892 if (!selinux_enforcing)
893 return 0;
894 return -EACCES;
895}
896
897static int security_compute_sid(u32 ssid,
898 u32 tsid,
899 u16 tclass,
900 u32 specified,
901 u32 *out_sid)
902{
903 struct context *scontext = NULL, *tcontext = NULL, newcontext;
904 struct role_trans *roletr = NULL;
905 struct avtab_key avkey;
906 struct avtab_datum *avdatum;
907 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 int rc = 0;
909
910 if (!ss_initialized) {
911 switch (tclass) {
912 case SECCLASS_PROCESS:
913 *out_sid = ssid;
914 break;
915 default:
916 *out_sid = tsid;
917 break;
918 }
919 goto out;
920 }
921
Venkat Yekkirala851f8a62006-07-30 03:03:18 -0700922 context_init(&newcontext);
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 POLICY_RDLOCK;
925
926 scontext = sidtab_search(&sidtab, ssid);
927 if (!scontext) {
928 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
929 ssid);
930 rc = -EINVAL;
931 goto out_unlock;
932 }
933 tcontext = sidtab_search(&sidtab, tsid);
934 if (!tcontext) {
935 printk(KERN_ERR "security_compute_sid: unrecognized SID %d\n",
936 tsid);
937 rc = -EINVAL;
938 goto out_unlock;
939 }
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 /* Set the user identity. */
942 switch (specified) {
943 case AVTAB_TRANSITION:
944 case AVTAB_CHANGE:
945 /* Use the process user identity. */
946 newcontext.user = scontext->user;
947 break;
948 case AVTAB_MEMBER:
949 /* Use the related object owner. */
950 newcontext.user = tcontext->user;
951 break;
952 }
953
954 /* Set the role and type to default values. */
955 switch (tclass) {
956 case SECCLASS_PROCESS:
957 /* Use the current role and type of process. */
958 newcontext.role = scontext->role;
959 newcontext.type = scontext->type;
960 break;
961 default:
962 /* Use the well-defined object role. */
963 newcontext.role = OBJECT_R_VAL;
964 /* Use the type of the related object. */
965 newcontext.type = tcontext->type;
966 }
967
968 /* Look for a type transition/member/change rule. */
969 avkey.source_type = scontext->type;
970 avkey.target_type = tcontext->type;
971 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700972 avkey.specified = specified;
973 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -0400976 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700977 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700979 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 avdatum = &node->datum;
981 break;
982 }
983 }
984 }
985
Stephen Smalley782ebb92005-09-03 15:55:16 -0700986 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -0700988 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 }
990
991 /* Check for class-specific changes. */
992 switch (tclass) {
993 case SECCLASS_PROCESS:
994 if (specified & AVTAB_TRANSITION) {
995 /* Look for a role transition rule. */
996 for (roletr = policydb.role_tr; roletr;
997 roletr = roletr->next) {
998 if (roletr->role == scontext->role &&
999 roletr->type == tcontext->type) {
1000 /* Use the role transition rule. */
1001 newcontext.role = roletr->new_role;
1002 break;
1003 }
1004 }
1005 }
1006 break;
1007 default:
1008 break;
1009 }
1010
1011 /* Set the MLS attributes.
1012 This is done last because it may allocate memory. */
1013 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1014 if (rc)
1015 goto out_unlock;
1016
1017 /* Check the validity of the context. */
1018 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1019 rc = compute_sid_handle_invalid_context(scontext,
1020 tcontext,
1021 tclass,
1022 &newcontext);
1023 if (rc)
1024 goto out_unlock;
1025 }
1026 /* Obtain the sid for the context. */
1027 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1028out_unlock:
1029 POLICY_RDUNLOCK;
1030 context_destroy(&newcontext);
1031out:
1032 return rc;
1033}
1034
1035/**
1036 * security_transition_sid - Compute the SID for a new subject/object.
1037 * @ssid: source security identifier
1038 * @tsid: target security identifier
1039 * @tclass: target security class
1040 * @out_sid: security identifier for new subject/object
1041 *
1042 * Compute a SID to use for labeling a new subject or object in the
1043 * class @tclass based on a SID pair (@ssid, @tsid).
1044 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1045 * if insufficient memory is available, or %0 if the new SID was
1046 * computed successfully.
1047 */
1048int security_transition_sid(u32 ssid,
1049 u32 tsid,
1050 u16 tclass,
1051 u32 *out_sid)
1052{
1053 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1054}
1055
1056/**
1057 * security_member_sid - Compute the SID for member selection.
1058 * @ssid: source security identifier
1059 * @tsid: target security identifier
1060 * @tclass: target security class
1061 * @out_sid: security identifier for selected member
1062 *
1063 * Compute a SID to use when selecting a member of a polyinstantiated
1064 * object of class @tclass based on a SID pair (@ssid, @tsid).
1065 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1066 * if insufficient memory is available, or %0 if the SID was
1067 * computed successfully.
1068 */
1069int security_member_sid(u32 ssid,
1070 u32 tsid,
1071 u16 tclass,
1072 u32 *out_sid)
1073{
1074 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1075}
1076
1077/**
1078 * security_change_sid - Compute the SID for object relabeling.
1079 * @ssid: source security identifier
1080 * @tsid: target security identifier
1081 * @tclass: target security class
1082 * @out_sid: security identifier for selected member
1083 *
1084 * Compute a SID to use for relabeling an object of class @tclass
1085 * based on a SID pair (@ssid, @tsid).
1086 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1087 * if insufficient memory is available, or %0 if the SID was
1088 * computed successfully.
1089 */
1090int security_change_sid(u32 ssid,
1091 u32 tsid,
1092 u16 tclass,
1093 u32 *out_sid)
1094{
1095 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1096}
1097
Chad Sellersb94c7e62006-11-06 12:38:18 -05001098/*
1099 * Verify that each kernel class that is defined in the
1100 * policy is correct
1101 */
1102static int validate_classes(struct policydb *p)
1103{
1104 int i, j;
1105 struct class_datum *cladatum;
1106 struct perm_datum *perdatum;
1107 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1108 u16 class_val;
1109 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1110 const char *def_class, *def_perm, *pol_class;
1111 struct symtab *perms;
1112
Eric Paris3f120702007-09-21 14:37:10 -04001113 if (p->allow_unknown) {
1114 u32 num_classes = kdefs->cts_len;
1115 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1116 if (!p->undefined_perms)
1117 return -ENOMEM;
1118 }
1119
Chad Sellersb94c7e62006-11-06 12:38:18 -05001120 for (i = 1; i < kdefs->cts_len; i++) {
1121 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001122 if (!def_class)
1123 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001124 if (i > p->p_classes.nprim) {
1125 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001126 "SELinux: class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001127 def_class);
Eric Paris3f120702007-09-21 14:37:10 -04001128 if (p->reject_unknown)
1129 return -EINVAL;
1130 if (p->allow_unknown)
1131 p->undefined_perms[i-1] = ~0U;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001132 continue;
1133 }
1134 pol_class = p->p_class_val_to_name[i-1];
1135 if (strcmp(pol_class, def_class)) {
1136 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001137 "SELinux: class %d is incorrect, found %s but should be %s\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001138 i, pol_class, def_class);
1139 return -EINVAL;
1140 }
1141 }
1142 for (i = 0; i < kdefs->av_pts_len; i++) {
1143 class_val = kdefs->av_perm_to_string[i].tclass;
1144 perm_val = kdefs->av_perm_to_string[i].value;
1145 def_perm = kdefs->av_perm_to_string[i].name;
1146 if (class_val > p->p_classes.nprim)
1147 continue;
1148 pol_class = p->p_class_val_to_name[class_val-1];
1149 cladatum = hashtab_search(p->p_classes.table, pol_class);
1150 BUG_ON(!cladatum);
1151 perms = &cladatum->permissions;
1152 nprim = 1 << (perms->nprim - 1);
1153 if (perm_val > nprim) {
1154 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001155 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001156 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001157 if (p->reject_unknown)
1158 return -EINVAL;
1159 if (p->allow_unknown)
1160 p->undefined_perms[class_val-1] |= perm_val;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001161 continue;
1162 }
1163 perdatum = hashtab_search(perms->table, def_perm);
1164 if (perdatum == NULL) {
1165 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001166 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001167 def_perm, pol_class);
1168 return -EINVAL;
1169 }
1170 pol_val = 1 << (perdatum->value - 1);
1171 if (pol_val != perm_val) {
1172 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001173 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001174 def_perm, pol_class);
1175 return -EINVAL;
1176 }
1177 }
1178 for (i = 0; i < kdefs->av_inherit_len; i++) {
1179 class_val = kdefs->av_inherit[i].tclass;
1180 if (class_val > p->p_classes.nprim)
1181 continue;
1182 pol_class = p->p_class_val_to_name[class_val-1];
1183 cladatum = hashtab_search(p->p_classes.table, pol_class);
1184 BUG_ON(!cladatum);
1185 if (!cladatum->comdatum) {
1186 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001187 "SELinux: class %s should have an inherits clause but does not\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001188 pol_class);
1189 return -EINVAL;
1190 }
1191 tmp = kdefs->av_inherit[i].common_base;
1192 common_pts_len = 0;
1193 while (!(tmp & 0x01)) {
1194 common_pts_len++;
1195 tmp >>= 1;
1196 }
1197 perms = &cladatum->comdatum->permissions;
1198 for (j = 0; j < common_pts_len; j++) {
1199 def_perm = kdefs->av_inherit[i].common_pts[j];
1200 if (j >= perms->nprim) {
1201 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001202 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001203 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001204 if (p->reject_unknown)
1205 return -EINVAL;
1206 if (p->allow_unknown)
1207 p->undefined_perms[class_val-1] |= (1 << j);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001208 continue;
1209 }
1210 perdatum = hashtab_search(perms->table, def_perm);
1211 if (perdatum == NULL) {
1212 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001213 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001214 def_perm, pol_class);
1215 return -EINVAL;
1216 }
1217 if (perdatum->value != j + 1) {
1218 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001219 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001220 def_perm, pol_class);
1221 return -EINVAL;
1222 }
1223 }
1224 }
1225 return 0;
1226}
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228/* Clone the SID into the new SID table. */
1229static int clone_sid(u32 sid,
1230 struct context *context,
1231 void *arg)
1232{
1233 struct sidtab *s = arg;
1234
1235 return sidtab_insert(s, sid, context);
1236}
1237
1238static inline int convert_context_handle_invalid_context(struct context *context)
1239{
1240 int rc = 0;
1241
1242 if (selinux_enforcing) {
1243 rc = -EINVAL;
1244 } else {
1245 char *s;
1246 u32 len;
1247
1248 context_struct_to_string(context, &s, &len);
James Morris454d9722008-02-26 20:42:02 +11001249 printk(KERN_ERR "SELinux: context %s is invalid\n", s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 kfree(s);
1251 }
1252 return rc;
1253}
1254
1255struct convert_context_args {
1256 struct policydb *oldp;
1257 struct policydb *newp;
1258};
1259
1260/*
1261 * Convert the values in the security context
1262 * structure `c' from the values specified
1263 * in the policy `p->oldp' to the values specified
1264 * in the policy `p->newp'. Verify that the
1265 * context is valid under the new policy.
1266 */
1267static int convert_context(u32 key,
1268 struct context *c,
1269 void *p)
1270{
1271 struct convert_context_args *args;
1272 struct context oldc;
1273 struct role_datum *role;
1274 struct type_datum *typdatum;
1275 struct user_datum *usrdatum;
1276 char *s;
1277 u32 len;
1278 int rc;
1279
1280 args = p;
1281
1282 rc = context_cpy(&oldc, c);
1283 if (rc)
1284 goto out;
1285
1286 rc = -EINVAL;
1287
1288 /* Convert the user. */
1289 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001290 args->oldp->p_user_val_to_name[c->user - 1]);
1291 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 c->user = usrdatum->value;
1294
1295 /* Convert the role. */
1296 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001297 args->oldp->p_role_val_to_name[c->role - 1]);
1298 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 c->role = role->value;
1301
1302 /* Convert the type. */
1303 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001304 args->oldp->p_type_val_to_name[c->type - 1]);
1305 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 c->type = typdatum->value;
1308
1309 rc = mls_convert_context(args->oldp, args->newp, c);
1310 if (rc)
1311 goto bad;
1312
1313 /* Check the validity of the new context. */
1314 if (!policydb_context_isvalid(args->newp, c)) {
1315 rc = convert_context_handle_invalid_context(&oldc);
1316 if (rc)
1317 goto bad;
1318 }
1319
1320 context_destroy(&oldc);
1321out:
1322 return rc;
1323bad:
1324 context_struct_to_string(&oldc, &s, &len);
1325 context_destroy(&oldc);
James Morris454d9722008-02-26 20:42:02 +11001326 printk(KERN_ERR "SELinux: invalidating context %s\n", s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 kfree(s);
1328 goto out;
1329}
1330
Paul Moore3bb56b22008-01-29 08:38:19 -05001331static void security_load_policycaps(void)
1332{
1333 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1334 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001335 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1336 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001340static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342/**
1343 * security_load_policy - Load a security policy configuration.
1344 * @data: binary policy data
1345 * @len: length of data in bytes
1346 *
1347 * Load a new set of security policy configuration data,
1348 * validate it and convert the SID table as necessary.
1349 * This function will flush the access vector cache after
1350 * loading the new policy.
1351 */
1352int security_load_policy(void *data, size_t len)
1353{
1354 struct policydb oldpolicydb, newpolicydb;
1355 struct sidtab oldsidtab, newsidtab;
1356 struct convert_context_args args;
1357 u32 seqno;
1358 int rc = 0;
1359 struct policy_file file = { data, len }, *fp = &file;
1360
1361 LOAD_LOCK;
1362
1363 if (!ss_initialized) {
1364 avtab_cache_init();
1365 if (policydb_read(&policydb, fp)) {
1366 LOAD_UNLOCK;
1367 avtab_cache_destroy();
1368 return -EINVAL;
1369 }
1370 if (policydb_load_isids(&policydb, &sidtab)) {
1371 LOAD_UNLOCK;
1372 policydb_destroy(&policydb);
1373 avtab_cache_destroy();
1374 return -EINVAL;
1375 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001376 /* Verify that the kernel defined classes are correct. */
1377 if (validate_classes(&policydb)) {
1378 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001379 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001380 LOAD_UNLOCK;
1381 sidtab_destroy(&sidtab);
1382 policydb_destroy(&policydb);
1383 avtab_cache_destroy();
1384 return -EINVAL;
1385 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001386 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 policydb_loaded_version = policydb.policyvers;
1388 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001389 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 LOAD_UNLOCK;
1391 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001392 avc_ss_reset(seqno);
1393 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001394 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001395 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 return 0;
1397 }
1398
1399#if 0
1400 sidtab_hash_eval(&sidtab, "sids");
1401#endif
1402
1403 if (policydb_read(&newpolicydb, fp)) {
1404 LOAD_UNLOCK;
1405 return -EINVAL;
1406 }
1407
1408 sidtab_init(&newsidtab);
1409
Chad Sellersb94c7e62006-11-06 12:38:18 -05001410 /* Verify that the kernel defined classes are correct. */
1411 if (validate_classes(&newpolicydb)) {
1412 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001413 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001414 rc = -EINVAL;
1415 goto err;
1416 }
1417
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001418 rc = security_preserve_bools(&newpolicydb);
1419 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001420 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001421 goto err;
1422 }
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 /* Clone the SID table. */
1425 sidtab_shutdown(&sidtab);
1426 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1427 rc = -ENOMEM;
1428 goto err;
1429 }
1430
1431 /* Convert the internal representations of contexts
1432 in the new SID table and remove invalid SIDs. */
1433 args.oldp = &policydb;
1434 args.newp = &newpolicydb;
1435 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1436
1437 /* Save the old policydb and SID table to free later. */
1438 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1439 sidtab_set(&oldsidtab, &sidtab);
1440
1441 /* Install the new policydb and SID table. */
1442 POLICY_WRLOCK;
1443 memcpy(&policydb, &newpolicydb, sizeof policydb);
1444 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001445 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 seqno = ++latest_granting;
1447 policydb_loaded_version = policydb.policyvers;
1448 POLICY_WRUNLOCK;
1449 LOAD_UNLOCK;
1450
1451 /* Free the old policydb and SID table. */
1452 policydb_destroy(&oldpolicydb);
1453 sidtab_destroy(&oldsidtab);
1454
1455 avc_ss_reset(seqno);
1456 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001457 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001458 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460 return 0;
1461
1462err:
1463 LOAD_UNLOCK;
1464 sidtab_destroy(&newsidtab);
1465 policydb_destroy(&newpolicydb);
1466 return rc;
1467
1468}
1469
1470/**
1471 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 * @protocol: protocol number
1473 * @port: port number
1474 * @out_sid: security identifier
1475 */
Paul Moore3e112172008-04-10 10:48:14 -04001476int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct ocontext *c;
1479 int rc = 0;
1480
1481 POLICY_RDLOCK;
1482
1483 c = policydb.ocontexts[OCON_PORT];
1484 while (c) {
1485 if (c->u.port.protocol == protocol &&
1486 c->u.port.low_port <= port &&
1487 c->u.port.high_port >= port)
1488 break;
1489 c = c->next;
1490 }
1491
1492 if (c) {
1493 if (!c->sid[0]) {
1494 rc = sidtab_context_to_sid(&sidtab,
1495 &c->context[0],
1496 &c->sid[0]);
1497 if (rc)
1498 goto out;
1499 }
1500 *out_sid = c->sid[0];
1501 } else {
1502 *out_sid = SECINITSID_PORT;
1503 }
1504
1505out:
1506 POLICY_RDUNLOCK;
1507 return rc;
1508}
1509
1510/**
1511 * security_netif_sid - Obtain the SID for a network interface.
1512 * @name: interface name
1513 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001515int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
1517 int rc = 0;
1518 struct ocontext *c;
1519
1520 POLICY_RDLOCK;
1521
1522 c = policydb.ocontexts[OCON_NETIF];
1523 while (c) {
1524 if (strcmp(name, c->u.name) == 0)
1525 break;
1526 c = c->next;
1527 }
1528
1529 if (c) {
1530 if (!c->sid[0] || !c->sid[1]) {
1531 rc = sidtab_context_to_sid(&sidtab,
1532 &c->context[0],
1533 &c->sid[0]);
1534 if (rc)
1535 goto out;
1536 rc = sidtab_context_to_sid(&sidtab,
1537 &c->context[1],
1538 &c->sid[1]);
1539 if (rc)
1540 goto out;
1541 }
1542 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001543 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546out:
1547 POLICY_RDUNLOCK;
1548 return rc;
1549}
1550
1551static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1552{
1553 int i, fail = 0;
1554
Eric Paris5d55a342008-04-18 17:38:33 -04001555 for (i = 0; i < 4; i++)
1556 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 fail = 1;
1558 break;
1559 }
1560
1561 return !fail;
1562}
1563
1564/**
1565 * security_node_sid - Obtain the SID for a node (host).
1566 * @domain: communication domain aka address family
1567 * @addrp: address
1568 * @addrlen: address length in bytes
1569 * @out_sid: security identifier
1570 */
1571int security_node_sid(u16 domain,
1572 void *addrp,
1573 u32 addrlen,
1574 u32 *out_sid)
1575{
1576 int rc = 0;
1577 struct ocontext *c;
1578
1579 POLICY_RDLOCK;
1580
1581 switch (domain) {
1582 case AF_INET: {
1583 u32 addr;
1584
1585 if (addrlen != sizeof(u32)) {
1586 rc = -EINVAL;
1587 goto out;
1588 }
1589
1590 addr = *((u32 *)addrp);
1591
1592 c = policydb.ocontexts[OCON_NODE];
1593 while (c) {
1594 if (c->u.node.addr == (addr & c->u.node.mask))
1595 break;
1596 c = c->next;
1597 }
1598 break;
1599 }
1600
1601 case AF_INET6:
1602 if (addrlen != sizeof(u64) * 2) {
1603 rc = -EINVAL;
1604 goto out;
1605 }
1606 c = policydb.ocontexts[OCON_NODE6];
1607 while (c) {
1608 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1609 c->u.node6.mask))
1610 break;
1611 c = c->next;
1612 }
1613 break;
1614
1615 default:
1616 *out_sid = SECINITSID_NODE;
1617 goto out;
1618 }
1619
1620 if (c) {
1621 if (!c->sid[0]) {
1622 rc = sidtab_context_to_sid(&sidtab,
1623 &c->context[0],
1624 &c->sid[0]);
1625 if (rc)
1626 goto out;
1627 }
1628 *out_sid = c->sid[0];
1629 } else {
1630 *out_sid = SECINITSID_NODE;
1631 }
1632
1633out:
1634 POLICY_RDUNLOCK;
1635 return rc;
1636}
1637
1638#define SIDS_NEL 25
1639
1640/**
1641 * security_get_user_sids - Obtain reachable SIDs for a user.
1642 * @fromsid: starting SID
1643 * @username: username
1644 * @sids: array of reachable SIDs for user
1645 * @nel: number of elements in @sids
1646 *
1647 * Generate the set of SIDs for legal security contexts
1648 * for a given user that can be reached by @fromsid.
1649 * Set *@sids to point to a dynamically allocated
1650 * array containing the set of SIDs. Set *@nel to the
1651 * number of elements in the array.
1652 */
1653
1654int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04001655 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 u32 **sids,
1657 u32 *nel)
1658{
1659 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001660 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 u32 mynel = 0, maxnel = SIDS_NEL;
1662 struct user_datum *user;
1663 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001664 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int rc = 0, i, j;
1666
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001667 *sids = NULL;
1668 *nel = 0;
1669
1670 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 POLICY_RDLOCK;
1674
1675 fromcon = sidtab_search(&sidtab, fromsid);
1676 if (!fromcon) {
1677 rc = -EINVAL;
1678 goto out_unlock;
1679 }
1680
1681 user = hashtab_search(policydb.p_users.table, username);
1682 if (!user) {
1683 rc = -EINVAL;
1684 goto out_unlock;
1685 }
1686 usercon.user = user->value;
1687
James Morris89d155e2005-10-30 14:59:21 -08001688 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 if (!mysids) {
1690 rc = -ENOMEM;
1691 goto out_unlock;
1692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001694 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 role = policydb.role_val_to_struct[i];
1696 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001697 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 usercon.type = j+1;
1699
1700 if (mls_setup_user_range(fromcon, user, &usercon))
1701 continue;
1702
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001704 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (mynel < maxnel) {
1707 mysids[mynel++] = sid;
1708 } else {
1709 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001710 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 if (!mysids2) {
1712 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 goto out_unlock;
1714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1716 kfree(mysids);
1717 mysids = mysids2;
1718 mysids[mynel++] = sid;
1719 }
1720 }
1721 }
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723out_unlock:
1724 POLICY_RDUNLOCK;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001725 if (rc || !mynel) {
1726 kfree(mysids);
1727 goto out;
1728 }
1729
1730 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1731 if (!mysids2) {
1732 rc = -ENOMEM;
1733 kfree(mysids);
1734 goto out;
1735 }
1736 for (i = 0, j = 0; i < mynel; i++) {
1737 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1738 SECCLASS_PROCESS,
1739 PROCESS__TRANSITION, AVC_STRICT,
1740 NULL);
1741 if (!rc)
1742 mysids2[j++] = mysids[i];
1743 cond_resched();
1744 }
1745 rc = 0;
1746 kfree(mysids);
1747 *sids = mysids2;
1748 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749out:
1750 return rc;
1751}
1752
1753/**
1754 * security_genfs_sid - Obtain a SID for a file in a filesystem
1755 * @fstype: filesystem type
1756 * @path: path from root of mount
1757 * @sclass: file security class
1758 * @sid: SID for path
1759 *
1760 * Obtain a SID to use for a file in a filesystem that
1761 * cannot support xattr or use a fixed labeling behavior like
1762 * transition SIDs or task SIDs.
1763 */
1764int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04001765 char *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 u16 sclass,
1767 u32 *sid)
1768{
1769 int len;
1770 struct genfs *genfs;
1771 struct ocontext *c;
1772 int rc = 0, cmp = 0;
1773
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05001774 while (path[0] == '/' && path[1] == '/')
1775 path++;
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 POLICY_RDLOCK;
1778
1779 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1780 cmp = strcmp(fstype, genfs->fstype);
1781 if (cmp <= 0)
1782 break;
1783 }
1784
1785 if (!genfs || cmp) {
1786 *sid = SECINITSID_UNLABELED;
1787 rc = -ENOENT;
1788 goto out;
1789 }
1790
1791 for (c = genfs->head; c; c = c->next) {
1792 len = strlen(c->u.name);
1793 if ((!c->v.sclass || sclass == c->v.sclass) &&
1794 (strncmp(c->u.name, path, len) == 0))
1795 break;
1796 }
1797
1798 if (!c) {
1799 *sid = SECINITSID_UNLABELED;
1800 rc = -ENOENT;
1801 goto out;
1802 }
1803
1804 if (!c->sid[0]) {
1805 rc = sidtab_context_to_sid(&sidtab,
1806 &c->context[0],
1807 &c->sid[0]);
1808 if (rc)
1809 goto out;
1810 }
1811
1812 *sid = c->sid[0];
1813out:
1814 POLICY_RDUNLOCK;
1815 return rc;
1816}
1817
1818/**
1819 * security_fs_use - Determine how to handle labeling for a filesystem.
1820 * @fstype: filesystem type
1821 * @behavior: labeling behavior
1822 * @sid: SID for filesystem (superblock)
1823 */
1824int security_fs_use(
1825 const char *fstype,
1826 unsigned int *behavior,
1827 u32 *sid)
1828{
1829 int rc = 0;
1830 struct ocontext *c;
1831
1832 POLICY_RDLOCK;
1833
1834 c = policydb.ocontexts[OCON_FSUSE];
1835 while (c) {
1836 if (strcmp(fstype, c->u.name) == 0)
1837 break;
1838 c = c->next;
1839 }
1840
1841 if (c) {
1842 *behavior = c->v.behavior;
1843 if (!c->sid[0]) {
1844 rc = sidtab_context_to_sid(&sidtab,
1845 &c->context[0],
1846 &c->sid[0]);
1847 if (rc)
1848 goto out;
1849 }
1850 *sid = c->sid[0];
1851 } else {
1852 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1853 if (rc) {
1854 *behavior = SECURITY_FS_USE_NONE;
1855 rc = 0;
1856 } else {
1857 *behavior = SECURITY_FS_USE_GENFS;
1858 }
1859 }
1860
1861out:
1862 POLICY_RDUNLOCK;
1863 return rc;
1864}
1865
1866int security_get_bools(int *len, char ***names, int **values)
1867{
1868 int i, rc = -ENOMEM;
1869
1870 POLICY_RDLOCK;
1871 *names = NULL;
1872 *values = NULL;
1873
1874 *len = policydb.p_bools.nprim;
1875 if (!*len) {
1876 rc = 0;
1877 goto out;
1878 }
1879
Eric Paris5d55a342008-04-18 17:38:33 -04001880 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (!*names)
1882 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Jesper Juhle0795cf2006-01-09 20:54:46 -08001884 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (!*values)
1886 goto err;
1887
1888 for (i = 0; i < *len; i++) {
1889 size_t name_len;
1890 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1891 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001892 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (!(*names)[i])
1894 goto err;
1895 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1896 (*names)[i][name_len - 1] = 0;
1897 }
1898 rc = 0;
1899out:
1900 POLICY_RDUNLOCK;
1901 return rc;
1902err:
1903 if (*names) {
1904 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001905 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001907 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto out;
1909}
1910
1911
1912int security_set_bools(int len, int *values)
1913{
1914 int i, rc = 0;
1915 int lenp, seqno = 0;
1916 struct cond_node *cur;
1917
1918 POLICY_WRLOCK;
1919
1920 lenp = policydb.p_bools.nprim;
1921 if (len != lenp) {
1922 rc = -EFAULT;
1923 goto out;
1924 }
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00001927 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1928 audit_log(current->audit_context, GFP_ATOMIC,
1929 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05001930 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00001931 policydb.p_bool_val_to_name[i],
1932 !!values[i],
1933 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05001934 audit_get_loginuid(current),
1935 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00001936 }
Eric Paris5d55a342008-04-18 17:38:33 -04001937 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001939 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
1943 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1944 rc = evaluate_cond_node(&policydb, cur);
1945 if (rc)
1946 goto out;
1947 }
1948
1949 seqno = ++latest_granting;
1950
1951out:
1952 POLICY_WRUNLOCK;
1953 if (!rc) {
1954 avc_ss_reset(seqno);
1955 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001956 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958 return rc;
1959}
1960
1961int security_get_bool_value(int bool)
1962{
1963 int rc = 0;
1964 int len;
1965
1966 POLICY_RDLOCK;
1967
1968 len = policydb.p_bools.nprim;
1969 if (bool >= len) {
1970 rc = -EFAULT;
1971 goto out;
1972 }
1973
1974 rc = policydb.bool_val_to_struct[bool]->state;
1975out:
1976 POLICY_RDUNLOCK;
1977 return rc;
1978}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06001979
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001980static int security_preserve_bools(struct policydb *p)
1981{
1982 int rc, nbools = 0, *bvalues = NULL, i;
1983 char **bnames = NULL;
1984 struct cond_bool_datum *booldatum;
1985 struct cond_node *cur;
1986
1987 rc = security_get_bools(&nbools, &bnames, &bvalues);
1988 if (rc)
1989 goto out;
1990 for (i = 0; i < nbools; i++) {
1991 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
1992 if (booldatum)
1993 booldatum->state = bvalues[i];
1994 }
1995 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
1996 rc = evaluate_cond_node(p, cur);
1997 if (rc)
1998 goto out;
1999 }
2000
2001out:
2002 if (bnames) {
2003 for (i = 0; i < nbools; i++)
2004 kfree(bnames[i]);
2005 }
2006 kfree(bnames);
2007 kfree(bvalues);
2008 return rc;
2009}
2010
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002011/*
2012 * security_sid_mls_copy() - computes a new sid based on the given
2013 * sid and the mls portion of mls_sid.
2014 */
2015int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2016{
2017 struct context *context1;
2018 struct context *context2;
2019 struct context newcon;
2020 char *s;
2021 u32 len;
2022 int rc = 0;
2023
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002024 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002025 *new_sid = sid;
2026 goto out;
2027 }
2028
2029 context_init(&newcon);
2030
2031 POLICY_RDLOCK;
2032 context1 = sidtab_search(&sidtab, sid);
2033 if (!context1) {
2034 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
2035 "%d\n", sid);
2036 rc = -EINVAL;
2037 goto out_unlock;
2038 }
2039
2040 context2 = sidtab_search(&sidtab, mls_sid);
2041 if (!context2) {
2042 printk(KERN_ERR "security_sid_mls_copy: unrecognized SID "
2043 "%d\n", mls_sid);
2044 rc = -EINVAL;
2045 goto out_unlock;
2046 }
2047
2048 newcon.user = context1->user;
2049 newcon.role = context1->role;
2050 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002051 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002052 if (rc)
2053 goto out_unlock;
2054
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002055 /* Check the validity of the new context. */
2056 if (!policydb_context_isvalid(&policydb, &newcon)) {
2057 rc = convert_context_handle_invalid_context(&newcon);
2058 if (rc)
2059 goto bad;
2060 }
2061
2062 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2063 goto out_unlock;
2064
2065bad:
2066 if (!context_struct_to_string(&newcon, &s, &len)) {
2067 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2068 "security_sid_mls_copy: invalid context %s", s);
2069 kfree(s);
2070 }
2071
2072out_unlock:
2073 POLICY_RDUNLOCK;
2074 context_destroy(&newcon);
2075out:
2076 return rc;
2077}
2078
Paul Moore220deb92008-01-29 08:38:23 -05002079/**
2080 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2081 * @nlbl_sid: NetLabel SID
2082 * @nlbl_type: NetLabel labeling protocol type
2083 * @xfrm_sid: XFRM SID
2084 *
2085 * Description:
2086 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2087 * resolved into a single SID it is returned via @peer_sid and the function
2088 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2089 * returns a negative value. A table summarizing the behavior is below:
2090 *
2091 * | function return | @sid
2092 * ------------------------------+-----------------+-----------------
2093 * no peer labels | 0 | SECSID_NULL
2094 * single peer label | 0 | <peer_label>
2095 * multiple, consistent labels | 0 | <peer_label>
2096 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2097 *
2098 */
2099int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2100 u32 xfrm_sid,
2101 u32 *peer_sid)
2102{
2103 int rc;
2104 struct context *nlbl_ctx;
2105 struct context *xfrm_ctx;
2106
2107 /* handle the common (which also happens to be the set of easy) cases
2108 * right away, these two if statements catch everything involving a
2109 * single or absent peer SID/label */
2110 if (xfrm_sid == SECSID_NULL) {
2111 *peer_sid = nlbl_sid;
2112 return 0;
2113 }
2114 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2115 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2116 * is present */
2117 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2118 *peer_sid = xfrm_sid;
2119 return 0;
2120 }
2121
2122 /* we don't need to check ss_initialized here since the only way both
2123 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2124 * security server was initialized and ss_initialized was true */
2125 if (!selinux_mls_enabled) {
2126 *peer_sid = SECSID_NULL;
2127 return 0;
2128 }
2129
2130 POLICY_RDLOCK;
2131
2132 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2133 if (!nlbl_ctx) {
2134 printk(KERN_ERR
2135 "security_sid_mls_cmp: unrecognized SID %d\n",
2136 nlbl_sid);
2137 rc = -EINVAL;
2138 goto out_slowpath;
2139 }
2140 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2141 if (!xfrm_ctx) {
2142 printk(KERN_ERR
2143 "security_sid_mls_cmp: unrecognized SID %d\n",
2144 xfrm_sid);
2145 rc = -EINVAL;
2146 goto out_slowpath;
2147 }
2148 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2149
2150out_slowpath:
2151 POLICY_RDUNLOCK;
2152 if (rc == 0)
2153 /* at present NetLabel SIDs/labels really only carry MLS
2154 * information so if the MLS portion of the NetLabel SID
2155 * matches the MLS portion of the labeled XFRM SID/label
2156 * then pass along the XFRM SID as it is the most
2157 * expressive */
2158 *peer_sid = xfrm_sid;
2159 else
2160 *peer_sid = SECSID_NULL;
2161 return rc;
2162}
2163
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002164static int get_classes_callback(void *k, void *d, void *args)
2165{
2166 struct class_datum *datum = d;
2167 char *name = k, **classes = args;
2168 int value = datum->value - 1;
2169
2170 classes[value] = kstrdup(name, GFP_ATOMIC);
2171 if (!classes[value])
2172 return -ENOMEM;
2173
2174 return 0;
2175}
2176
2177int security_get_classes(char ***classes, int *nclasses)
2178{
2179 int rc = -ENOMEM;
2180
2181 POLICY_RDLOCK;
2182
2183 *nclasses = policydb.p_classes.nprim;
2184 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2185 if (!*classes)
2186 goto out;
2187
2188 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2189 *classes);
2190 if (rc < 0) {
2191 int i;
2192 for (i = 0; i < *nclasses; i++)
2193 kfree((*classes)[i]);
2194 kfree(*classes);
2195 }
2196
2197out:
2198 POLICY_RDUNLOCK;
2199 return rc;
2200}
2201
2202static int get_permissions_callback(void *k, void *d, void *args)
2203{
2204 struct perm_datum *datum = d;
2205 char *name = k, **perms = args;
2206 int value = datum->value - 1;
2207
2208 perms[value] = kstrdup(name, GFP_ATOMIC);
2209 if (!perms[value])
2210 return -ENOMEM;
2211
2212 return 0;
2213}
2214
2215int security_get_permissions(char *class, char ***perms, int *nperms)
2216{
2217 int rc = -ENOMEM, i;
2218 struct class_datum *match;
2219
2220 POLICY_RDLOCK;
2221
2222 match = hashtab_search(policydb.p_classes.table, class);
2223 if (!match) {
2224 printk(KERN_ERR "%s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002225 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002226 rc = -EINVAL;
2227 goto out;
2228 }
2229
2230 *nperms = match->permissions.nprim;
2231 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2232 if (!*perms)
2233 goto out;
2234
2235 if (match->comdatum) {
2236 rc = hashtab_map(match->comdatum->permissions.table,
2237 get_permissions_callback, *perms);
2238 if (rc < 0)
2239 goto err;
2240 }
2241
2242 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2243 *perms);
2244 if (rc < 0)
2245 goto err;
2246
2247out:
2248 POLICY_RDUNLOCK;
2249 return rc;
2250
2251err:
2252 POLICY_RDUNLOCK;
2253 for (i = 0; i < *nperms; i++)
2254 kfree((*perms)[i]);
2255 kfree(*perms);
2256 return rc;
2257}
2258
Eric Paris3f120702007-09-21 14:37:10 -04002259int security_get_reject_unknown(void)
2260{
2261 return policydb.reject_unknown;
2262}
2263
2264int security_get_allow_unknown(void)
2265{
2266 return policydb.allow_unknown;
2267}
2268
Paul Moore3bb56b22008-01-29 08:38:19 -05002269/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002270 * security_policycap_supported - Check for a specific policy capability
2271 * @req_cap: capability
2272 *
2273 * Description:
2274 * This function queries the currently loaded policy to see if it supports the
2275 * capability specified by @req_cap. Returns true (1) if the capability is
2276 * supported, false (0) if it isn't supported.
2277 *
2278 */
2279int security_policycap_supported(unsigned int req_cap)
2280{
2281 int rc;
2282
2283 POLICY_RDLOCK;
2284 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2285 POLICY_RDUNLOCK;
2286
2287 return rc;
2288}
2289
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002290struct selinux_audit_rule {
2291 u32 au_seqno;
2292 struct context au_ctxt;
2293};
2294
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002295void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002296{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002297 struct selinux_audit_rule *rule = vrule;
2298
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002299 if (rule) {
2300 context_destroy(&rule->au_ctxt);
2301 kfree(rule);
2302 }
2303}
2304
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002305int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002306{
2307 struct selinux_audit_rule *tmprule;
2308 struct role_datum *roledatum;
2309 struct type_datum *typedatum;
2310 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002311 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002312 int rc = 0;
2313
2314 *rule = NULL;
2315
2316 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002317 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002318
2319 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002320 case AUDIT_SUBJ_USER:
2321 case AUDIT_SUBJ_ROLE:
2322 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002323 case AUDIT_OBJ_USER:
2324 case AUDIT_OBJ_ROLE:
2325 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002326 /* only 'equals' and 'not equals' fit user, role, and type */
2327 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2328 return -EINVAL;
2329 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002330 case AUDIT_SUBJ_SEN:
2331 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002332 case AUDIT_OBJ_LEV_LOW:
2333 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002334 /* we do not allow a range, indicated by the presense of '-' */
2335 if (strchr(rulestr, '-'))
2336 return -EINVAL;
2337 break;
2338 default:
2339 /* only the above fields are valid */
2340 return -EINVAL;
2341 }
2342
2343 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2344 if (!tmprule)
2345 return -ENOMEM;
2346
2347 context_init(&tmprule->au_ctxt);
2348
2349 POLICY_RDLOCK;
2350
2351 tmprule->au_seqno = latest_granting;
2352
2353 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002354 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002355 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002356 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2357 if (!userdatum)
2358 rc = -EINVAL;
2359 else
2360 tmprule->au_ctxt.user = userdatum->value;
2361 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002362 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002363 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002364 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2365 if (!roledatum)
2366 rc = -EINVAL;
2367 else
2368 tmprule->au_ctxt.role = roledatum->value;
2369 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002370 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002371 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002372 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2373 if (!typedatum)
2374 rc = -EINVAL;
2375 else
2376 tmprule->au_ctxt.type = typedatum->value;
2377 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002378 case AUDIT_SUBJ_SEN:
2379 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002380 case AUDIT_OBJ_LEV_LOW:
2381 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002382 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2383 break;
2384 }
2385
2386 POLICY_RDUNLOCK;
2387
2388 if (rc) {
2389 selinux_audit_rule_free(tmprule);
2390 tmprule = NULL;
2391 }
2392
2393 *rule = tmprule;
2394
2395 return rc;
2396}
2397
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002398/* Check to see if the rule contains any selinux fields */
2399int selinux_audit_rule_known(struct audit_krule *rule)
2400{
2401 int i;
2402
2403 for (i = 0; i < rule->field_count; i++) {
2404 struct audit_field *f = &rule->fields[i];
2405 switch (f->type) {
2406 case AUDIT_SUBJ_USER:
2407 case AUDIT_SUBJ_ROLE:
2408 case AUDIT_SUBJ_TYPE:
2409 case AUDIT_SUBJ_SEN:
2410 case AUDIT_SUBJ_CLR:
2411 case AUDIT_OBJ_USER:
2412 case AUDIT_OBJ_ROLE:
2413 case AUDIT_OBJ_TYPE:
2414 case AUDIT_OBJ_LEV_LOW:
2415 case AUDIT_OBJ_LEV_HIGH:
2416 return 1;
2417 }
2418 }
2419
2420 return 0;
2421}
2422
2423int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002424 struct audit_context *actx)
2425{
2426 struct context *ctxt;
2427 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002428 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002429 int match = 0;
2430
2431 if (!rule) {
2432 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002433 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002434 return -ENOENT;
2435 }
2436
2437 POLICY_RDLOCK;
2438
2439 if (rule->au_seqno < latest_granting) {
2440 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002441 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002442 match = -ESTALE;
2443 goto out;
2444 }
2445
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002446 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002447 if (!ctxt) {
2448 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002449 "selinux_audit_rule_match: unrecognized SID %d\n",
2450 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002451 match = -ENOENT;
2452 goto out;
2453 }
2454
2455 /* a field/op pair that is not caught here will simply fall through
2456 without a match */
2457 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002458 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002459 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002460 switch (op) {
2461 case AUDIT_EQUAL:
2462 match = (ctxt->user == rule->au_ctxt.user);
2463 break;
2464 case AUDIT_NOT_EQUAL:
2465 match = (ctxt->user != rule->au_ctxt.user);
2466 break;
2467 }
2468 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002469 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002470 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002471 switch (op) {
2472 case AUDIT_EQUAL:
2473 match = (ctxt->role == rule->au_ctxt.role);
2474 break;
2475 case AUDIT_NOT_EQUAL:
2476 match = (ctxt->role != rule->au_ctxt.role);
2477 break;
2478 }
2479 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002480 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002481 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002482 switch (op) {
2483 case AUDIT_EQUAL:
2484 match = (ctxt->type == rule->au_ctxt.type);
2485 break;
2486 case AUDIT_NOT_EQUAL:
2487 match = (ctxt->type != rule->au_ctxt.type);
2488 break;
2489 }
2490 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002491 case AUDIT_SUBJ_SEN:
2492 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002493 case AUDIT_OBJ_LEV_LOW:
2494 case AUDIT_OBJ_LEV_HIGH:
2495 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002496 field == AUDIT_OBJ_LEV_LOW) ?
2497 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002498 switch (op) {
2499 case AUDIT_EQUAL:
2500 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002501 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002502 break;
2503 case AUDIT_NOT_EQUAL:
2504 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002505 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002506 break;
2507 case AUDIT_LESS_THAN:
2508 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002509 level) &&
2510 !mls_level_eq(&rule->au_ctxt.range.level[0],
2511 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002512 break;
2513 case AUDIT_LESS_THAN_OR_EQUAL:
2514 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002515 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002516 break;
2517 case AUDIT_GREATER_THAN:
2518 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002519 &rule->au_ctxt.range.level[0]) &&
2520 !mls_level_eq(level,
2521 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002522 break;
2523 case AUDIT_GREATER_THAN_OR_EQUAL:
2524 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002525 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002526 break;
2527 }
2528 }
2529
2530out:
2531 POLICY_RDUNLOCK;
2532 return match;
2533}
2534
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002535static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002536
2537static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2538 u16 class, u32 perms, u32 *retained)
2539{
2540 int err = 0;
2541
2542 if (event == AVC_CALLBACK_RESET && aurule_callback)
2543 err = aurule_callback();
2544 return err;
2545}
2546
2547static int __init aurule_init(void)
2548{
2549 int err;
2550
2551 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002552 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002553 if (err)
2554 panic("avc_add_callback() failed, error %d\n", err);
2555
2556 return err;
2557}
2558__initcall(aurule_init);
2559
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002560#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002561/**
Paul Moore5778eab2007-02-28 15:14:22 -05002562 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2563 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002564 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002565 *
2566 * Description:
2567 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002568 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2569 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002570 *
2571 */
Paul Moore5778eab2007-02-28 15:14:22 -05002572static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002573 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002574{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002575 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002576
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002577 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2578 if (sid_cache == NULL)
2579 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002580 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002581 if (secattr->cache == NULL) {
2582 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002583 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002584 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002585
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002586 *sid_cache = sid;
2587 secattr->cache->free = kfree;
2588 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002589 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002590}
2591
2592/**
Paul Moore5778eab2007-02-28 15:14:22 -05002593 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002594 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002595 * @sid: the SELinux SID
2596 *
2597 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002598 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002599 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002600 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2601 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2602 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2603 * conversion for future lookups. Returns zero on success, negative values on
2604 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002605 *
2606 */
Paul Moore5778eab2007-02-28 15:14:22 -05002607int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002608 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002609{
2610 int rc = -EIDRM;
2611 struct context *ctx;
2612 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002613
2614 if (!ss_initialized) {
2615 *sid = SECSID_NULL;
2616 return 0;
2617 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002618
2619 POLICY_RDLOCK;
2620
Paul Moore701a90b2006-11-17 17:38:46 -05002621 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002622 *sid = *(u32 *)secattr->cache->data;
2623 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002624 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2625 *sid = secattr->attr.secid;
2626 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05002627 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002628 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002629 if (ctx == NULL)
2630 goto netlbl_secattr_to_sid_return;
2631
2632 ctx_new.user = ctx->user;
2633 ctx_new.role = ctx->role;
2634 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002635 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002636 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002637 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05002638 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002639 goto netlbl_secattr_to_sid_return;
2640 ctx_new.range.level[1].cat.highbit =
2641 ctx_new.range.level[0].cat.highbit;
2642 ctx_new.range.level[1].cat.node =
2643 ctx_new.range.level[0].cat.node;
2644 } else {
2645 ebitmap_init(&ctx_new.range.level[0].cat);
2646 ebitmap_init(&ctx_new.range.level[1].cat);
2647 }
2648 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2649 goto netlbl_secattr_to_sid_return_cleanup;
2650
2651 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2652 if (rc != 0)
2653 goto netlbl_secattr_to_sid_return_cleanup;
2654
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002655 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05002656
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002657 ebitmap_destroy(&ctx_new.range.level[0].cat);
2658 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002659 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002660 rc = 0;
2661 }
2662
2663netlbl_secattr_to_sid_return:
2664 POLICY_RDUNLOCK;
2665 return rc;
2666netlbl_secattr_to_sid_return_cleanup:
2667 ebitmap_destroy(&ctx_new.range.level[0].cat);
2668 goto netlbl_secattr_to_sid_return;
2669}
2670
2671/**
Paul Moore5778eab2007-02-28 15:14:22 -05002672 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2673 * @sid: the SELinux SID
2674 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002675 *
2676 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002677 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2678 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002679 *
2680 */
Paul Moore5778eab2007-02-28 15:14:22 -05002681int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002682{
2683 int rc = -ENOENT;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002684 struct context *ctx;
2685
2686 if (!ss_initialized)
2687 return 0;
2688
2689 POLICY_RDLOCK;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002690 ctx = sidtab_search(&sidtab, sid);
2691 if (ctx == NULL)
Paul Moore5778eab2007-02-28 15:14:22 -05002692 goto netlbl_sid_to_secattr_failure;
2693 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2694 GFP_ATOMIC);
Paul Moore00447872008-04-12 19:06:42 -07002695 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
Paul Moore5778eab2007-02-28 15:14:22 -05002696 mls_export_netlbl_lvl(ctx, secattr);
2697 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002698 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05002699 goto netlbl_sid_to_secattr_failure;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002700 POLICY_RDUNLOCK;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002701
Paul Moore5778eab2007-02-28 15:14:22 -05002702 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05002703
Paul Moore5778eab2007-02-28 15:14:22 -05002704netlbl_sid_to_secattr_failure:
2705 POLICY_RDUNLOCK;
Paul Mooref8687af2006-10-30 15:22:15 -08002706 return rc;
2707}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002708#endif /* CONFIG_NETLABEL */