blob: 25cac5a2aa8eac5a027753e15fd1696478e299f3 [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 Molnarbb003072006-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 Molnarbb003072006-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 Molnarbb003072006-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:
Eric Paris744ba352008-04-17 11:52:44 -0400418 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n", __func__,
419 tclass);
Eric Paris3f120702007-09-21 14:37:10 -0400420 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Eric Paris64dbf072008-03-31 12:17:33 +1100423/*
424 * Given a sid find if the type has the permissive flag set
425 */
426int security_permissive_sid(u32 sid)
427{
428 struct context *context;
429 u32 type;
430 int rc;
431
432 POLICY_RDLOCK;
433
434 context = sidtab_search(&sidtab, sid);
435 BUG_ON(!context);
436
437 type = context->type;
438 /*
439 * we are intentionally using type here, not type-1, the 0th bit may
440 * someday indicate that we are globally setting permissive in policy.
441 */
442 rc = ebitmap_get_bit(&policydb.permissive_map, type);
443
444 POLICY_RDUNLOCK;
445 return rc;
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400449 struct context *ncontext,
450 struct context *tcontext,
451 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 char *o = NULL, *n = NULL, *t = NULL;
454 u32 olen, nlen, tlen;
455
456 if (context_struct_to_string(ocontext, &o, &olen) < 0)
457 goto out;
458 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
459 goto out;
460 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
461 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100462 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400463 "security_validate_transition: denied for"
464 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
465 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466out:
467 kfree(o);
468 kfree(n);
469 kfree(t);
470
471 if (!selinux_enforcing)
472 return 0;
473 return -EPERM;
474}
475
476int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Eric Paris5d55a342008-04-18 17:38:33 -0400477 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct context *ocontext;
480 struct context *ncontext;
481 struct context *tcontext;
482 struct class_datum *tclass_datum;
483 struct constraint_node *constraint;
484 int rc = 0;
485
486 if (!ss_initialized)
487 return 0;
488
489 POLICY_RDLOCK;
490
491 /*
492 * Remap extended Netlink classes for old policy versions.
493 * Do this here rather than socket_type_to_security_class()
494 * in case a newer policy version is loaded, allowing sockets
495 * to remain in the correct class.
496 */
497 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
498 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
499 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
500 tclass = SECCLASS_NETLINK_SOCKET;
501
502 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400503 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
504 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 rc = -EINVAL;
506 goto out;
507 }
508 tclass_datum = policydb.class_val_to_struct[tclass - 1];
509
510 ocontext = sidtab_search(&sidtab, oldsid);
511 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400512 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
513 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 rc = -EINVAL;
515 goto out;
516 }
517
518 ncontext = sidtab_search(&sidtab, newsid);
519 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400520 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
521 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rc = -EINVAL;
523 goto out;
524 }
525
526 tcontext = sidtab_search(&sidtab, tasksid);
527 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400528 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
529 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 rc = -EINVAL;
531 goto out;
532 }
533
534 constraint = tclass_datum->validatetrans;
535 while (constraint) {
536 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400537 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400539 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 goto out;
541 }
542 constraint = constraint->next;
543 }
544
545out:
546 POLICY_RDUNLOCK;
547 return rc;
548}
549
550/**
551 * security_compute_av - Compute access vector decisions.
552 * @ssid: source security identifier
553 * @tsid: target security identifier
554 * @tclass: target security class
555 * @requested: requested permissions
556 * @avd: access vector decisions
557 *
558 * Compute a set of access vector decisions based on the
559 * SID pair (@ssid, @tsid) for the permissions in @tclass.
560 * Return -%EINVAL if any of the parameters are invalid or %0
561 * if the access vector decisions were computed successfully.
562 */
563int security_compute_av(u32 ssid,
564 u32 tsid,
565 u16 tclass,
566 u32 requested,
567 struct av_decision *avd)
568{
569 struct context *scontext = NULL, *tcontext = NULL;
570 int rc = 0;
571
572 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700573 avd->allowed = 0xffffffff;
574 avd->decided = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 avd->auditallow = 0;
576 avd->auditdeny = 0xffffffff;
577 avd->seqno = latest_granting;
578 return 0;
579 }
580
581 POLICY_RDLOCK;
582
583 scontext = sidtab_search(&sidtab, ssid);
584 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400585 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
586 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 rc = -EINVAL;
588 goto out;
589 }
590 tcontext = sidtab_search(&sidtab, tsid);
591 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400592 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
593 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 rc = -EINVAL;
595 goto out;
596 }
597
598 rc = context_struct_compute_av(scontext, tcontext, tclass,
599 requested, avd);
600out:
601 POLICY_RDUNLOCK;
602 return rc;
603}
604
605/*
606 * Write the security context string representation of
607 * the context structure `context' into a dynamically
608 * allocated string of the correct size. Set `*scontext'
609 * to point to this string and set `*scontext_len' to
610 * the length of the string.
611 */
612static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
613{
614 char *scontextp;
615
616 *scontext = NULL;
617 *scontext_len = 0;
618
619 /* Compute the size of the context. */
620 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
621 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
622 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
623 *scontext_len += mls_compute_context_len(context);
624
625 /* Allocate space for the context; caller must free this space. */
626 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400627 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *scontext = scontextp;
630
631 /*
632 * Copy the user name, role name and type name into the context.
633 */
634 sprintf(scontextp, "%s:%s:%s",
635 policydb.p_user_val_to_name[context->user - 1],
636 policydb.p_role_val_to_name[context->role - 1],
637 policydb.p_type_val_to_name[context->type - 1]);
638 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400639 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
640 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 mls_sid_to_context(context, &scontextp);
643
644 *scontextp = 0;
645
646 return 0;
647}
648
649#include "initial_sid_to_string.h"
650
James Carterf0ee2e42007-04-04 10:11:29 -0400651const char *security_get_initial_sid_context(u32 sid)
652{
653 if (unlikely(sid > SECINITSID_NUM))
654 return NULL;
655 return initial_sid_to_string[sid];
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658/**
659 * security_sid_to_context - Obtain a context for a given SID.
660 * @sid: security identifier, SID
661 * @scontext: security context
662 * @scontext_len: length in bytes
663 *
664 * Write the string representation of the context associated with @sid
665 * into a dynamically allocated string of the correct size. Set @scontext
666 * to point to this string and set @scontext_len to the length of the string.
667 */
668int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
669{
670 struct context *context;
671 int rc = 0;
672
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500673 *scontext = NULL;
674 *scontext_len = 0;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!ss_initialized) {
677 if (sid <= SECINITSID_NUM) {
678 char *scontextp;
679
680 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -0400681 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca02006-05-15 09:43:48 -0700682 if (!scontextp) {
683 rc = -ENOMEM;
684 goto out;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 strcpy(scontextp, initial_sid_to_string[sid]);
687 *scontext = scontextp;
688 goto out;
689 }
Eric Paris744ba352008-04-17 11:52:44 -0400690 printk(KERN_ERR "SELinux: %s: called before initial "
691 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 rc = -EINVAL;
693 goto out;
694 }
695 POLICY_RDLOCK;
696 context = sidtab_search(&sidtab, sid);
697 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -0400698 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
699 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 rc = -EINVAL;
701 goto out_unlock;
702 }
703 rc = context_struct_to_string(context, scontext, scontext_len);
704out_unlock:
705 POLICY_RDUNLOCK;
706out:
707 return rc;
708
709}
710
David Howells8f0cfa52008-04-29 00:59:41 -0700711static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
Stephen Smalley869ab512008-04-04 08:46:05 -0400712 u32 *sid, u32 def_sid, gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 char *scontext2;
715 struct context context;
716 struct role_datum *role;
717 struct type_datum *typdatum;
718 struct user_datum *usrdatum;
719 char *scontextp, *p, oldc;
720 int rc = 0;
721
722 if (!ss_initialized) {
723 int i;
724
725 for (i = 1; i < SECINITSID_NUM; i++) {
726 if (!strcmp(initial_sid_to_string[i], scontext)) {
727 *sid = i;
728 goto out;
729 }
730 }
731 *sid = SECINITSID_KERNEL;
732 goto out;
733 }
734 *sid = SECSID_NULL;
735
736 /* Copy the string so that we can modify the copy as we parse it.
737 The string should already by null terminated, but we append a
738 null suffix to the copy to avoid problems with the existing
739 attr package, which doesn't view the null terminator as part
740 of the attribute value. */
Stephen Smalley869ab512008-04-04 08:46:05 -0400741 scontext2 = kmalloc(scontext_len+1, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!scontext2) {
743 rc = -ENOMEM;
744 goto out;
745 }
746 memcpy(scontext2, scontext, scontext_len);
747 scontext2[scontext_len] = 0;
748
749 context_init(&context);
750 *sid = SECSID_NULL;
751
752 POLICY_RDLOCK;
753
754 /* Parse the security context. */
755
756 rc = -EINVAL;
757 scontextp = (char *) scontext2;
758
759 /* Extract the user. */
760 p = scontextp;
761 while (*p && *p != ':')
762 p++;
763
764 if (*p == 0)
765 goto out_unlock;
766
767 *p++ = 0;
768
769 usrdatum = hashtab_search(policydb.p_users.table, scontextp);
770 if (!usrdatum)
771 goto out_unlock;
772
773 context.user = usrdatum->value;
774
775 /* Extract role. */
776 scontextp = p;
777 while (*p && *p != ':')
778 p++;
779
780 if (*p == 0)
781 goto out_unlock;
782
783 *p++ = 0;
784
785 role = hashtab_search(policydb.p_roles.table, scontextp);
786 if (!role)
787 goto out_unlock;
788 context.role = role->value;
789
790 /* Extract type. */
791 scontextp = p;
792 while (*p && *p != ':')
793 p++;
794 oldc = *p;
795 *p++ = 0;
796
797 typdatum = hashtab_search(policydb.p_types.table, scontextp);
798 if (!typdatum)
799 goto out_unlock;
800
801 context.type = typdatum->value;
802
James Morrisf5c1d5b2005-07-28 01:07:37 -0700803 rc = mls_context_to_sid(oldc, &p, &context, &sidtab, def_sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (rc)
805 goto out_unlock;
806
807 if ((p - scontext2) < scontext_len) {
808 rc = -EINVAL;
809 goto out_unlock;
810 }
811
812 /* Check the validity of the new context. */
813 if (!policydb_context_isvalid(&policydb, &context)) {
814 rc = -EINVAL;
815 goto out_unlock;
816 }
817 /* Obtain the new sid. */
818 rc = sidtab_context_to_sid(&sidtab, &context, sid);
819out_unlock:
820 POLICY_RDUNLOCK;
821 context_destroy(&context);
822 kfree(scontext2);
823out:
824 return rc;
825}
826
James Morrisf5c1d5b2005-07-28 01:07:37 -0700827/**
828 * security_context_to_sid - Obtain a SID for a given security context.
829 * @scontext: security context
830 * @scontext_len: length in bytes
831 * @sid: security identifier, SID
832 *
833 * Obtains a SID associated with the security context that
834 * has the string representation specified by @scontext.
835 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
836 * memory is available, or 0 on success.
837 */
David Howells8f0cfa52008-04-29 00:59:41 -0700838int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700839{
840 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley869ab512008-04-04 08:46:05 -0400841 sid, SECSID_NULL, GFP_KERNEL);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700842}
843
844/**
845 * security_context_to_sid_default - Obtain a SID for a given security context,
846 * falling back to specified default if needed.
847 *
848 * @scontext: security context
849 * @scontext_len: length in bytes
850 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -0700851 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -0700852 *
853 * Obtains a SID associated with the security context that
854 * has the string representation specified by @scontext.
855 * The default SID is passed to the MLS layer to be used to allow
856 * kernel labeling of the MLS field if the MLS field is not present
857 * (for upgrading to MLS without full relabel).
858 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
859 * memory is available, or 0 on success.
860 */
Stephen Smalley869ab512008-04-04 08:46:05 -0400861int security_context_to_sid_default(char *scontext, u32 scontext_len, u32 *sid,
862 u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -0700863{
864 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley869ab512008-04-04 08:46:05 -0400865 sid, def_sid, gfp_flags);
James Morrisf5c1d5b2005-07-28 01:07:37 -0700866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868static int compute_sid_handle_invalid_context(
869 struct context *scontext,
870 struct context *tcontext,
871 u16 tclass,
872 struct context *newcontext)
873{
874 char *s = NULL, *t = NULL, *n = NULL;
875 u32 slen, tlen, nlen;
876
877 if (context_struct_to_string(scontext, &s, &slen) < 0)
878 goto out;
879 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
880 goto out;
881 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
882 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100883 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 "security_compute_sid: invalid context %s"
885 " for scontext=%s"
886 " tcontext=%s"
887 " tclass=%s",
888 n, s, t, policydb.p_class_val_to_name[tclass-1]);
889out:
890 kfree(s);
891 kfree(t);
892 kfree(n);
893 if (!selinux_enforcing)
894 return 0;
895 return -EACCES;
896}
897
898static int security_compute_sid(u32 ssid,
899 u32 tsid,
900 u16 tclass,
901 u32 specified,
902 u32 *out_sid)
903{
904 struct context *scontext = NULL, *tcontext = NULL, newcontext;
905 struct role_trans *roletr = NULL;
906 struct avtab_key avkey;
907 struct avtab_datum *avdatum;
908 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int rc = 0;
910
911 if (!ss_initialized) {
912 switch (tclass) {
913 case SECCLASS_PROCESS:
914 *out_sid = ssid;
915 break;
916 default:
917 *out_sid = tsid;
918 break;
919 }
920 goto out;
921 }
922
Venkat Yekkirala851f8a62006-07-30 03:03:18 -0700923 context_init(&newcontext);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 POLICY_RDLOCK;
926
927 scontext = sidtab_search(&sidtab, ssid);
928 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400929 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
930 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 rc = -EINVAL;
932 goto out_unlock;
933 }
934 tcontext = sidtab_search(&sidtab, tsid);
935 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400936 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
937 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 rc = -EINVAL;
939 goto out_unlock;
940 }
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 /* Set the user identity. */
943 switch (specified) {
944 case AVTAB_TRANSITION:
945 case AVTAB_CHANGE:
946 /* Use the process user identity. */
947 newcontext.user = scontext->user;
948 break;
949 case AVTAB_MEMBER:
950 /* Use the related object owner. */
951 newcontext.user = tcontext->user;
952 break;
953 }
954
955 /* Set the role and type to default values. */
956 switch (tclass) {
957 case SECCLASS_PROCESS:
958 /* Use the current role and type of process. */
959 newcontext.role = scontext->role;
960 newcontext.type = scontext->type;
961 break;
962 default:
963 /* Use the well-defined object role. */
964 newcontext.role = OBJECT_R_VAL;
965 /* Use the type of the related object. */
966 newcontext.type = tcontext->type;
967 }
968
969 /* Look for a type transition/member/change rule. */
970 avkey.source_type = scontext->type;
971 avkey.target_type = tcontext->type;
972 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700973 avkey.specified = specified;
974 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -0400977 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700978 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 for (; node != NULL; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700980 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 avdatum = &node->datum;
982 break;
983 }
984 }
985 }
986
Stephen Smalley782ebb92005-09-03 15:55:16 -0700987 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -0700989 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
992 /* Check for class-specific changes. */
993 switch (tclass) {
994 case SECCLASS_PROCESS:
995 if (specified & AVTAB_TRANSITION) {
996 /* Look for a role transition rule. */
997 for (roletr = policydb.role_tr; roletr;
998 roletr = roletr->next) {
999 if (roletr->role == scontext->role &&
1000 roletr->type == tcontext->type) {
1001 /* Use the role transition rule. */
1002 newcontext.role = roletr->new_role;
1003 break;
1004 }
1005 }
1006 }
1007 break;
1008 default:
1009 break;
1010 }
1011
1012 /* Set the MLS attributes.
1013 This is done last because it may allocate memory. */
1014 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1015 if (rc)
1016 goto out_unlock;
1017
1018 /* Check the validity of the context. */
1019 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1020 rc = compute_sid_handle_invalid_context(scontext,
1021 tcontext,
1022 tclass,
1023 &newcontext);
1024 if (rc)
1025 goto out_unlock;
1026 }
1027 /* Obtain the sid for the context. */
1028 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1029out_unlock:
1030 POLICY_RDUNLOCK;
1031 context_destroy(&newcontext);
1032out:
1033 return rc;
1034}
1035
1036/**
1037 * security_transition_sid - Compute the SID for a new subject/object.
1038 * @ssid: source security identifier
1039 * @tsid: target security identifier
1040 * @tclass: target security class
1041 * @out_sid: security identifier for new subject/object
1042 *
1043 * Compute a SID to use for labeling a new subject or object in the
1044 * class @tclass based on a SID pair (@ssid, @tsid).
1045 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1046 * if insufficient memory is available, or %0 if the new SID was
1047 * computed successfully.
1048 */
1049int security_transition_sid(u32 ssid,
1050 u32 tsid,
1051 u16 tclass,
1052 u32 *out_sid)
1053{
1054 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1055}
1056
1057/**
1058 * security_member_sid - Compute the SID for member selection.
1059 * @ssid: source security identifier
1060 * @tsid: target security identifier
1061 * @tclass: target security class
1062 * @out_sid: security identifier for selected member
1063 *
1064 * Compute a SID to use when selecting a member of a polyinstantiated
1065 * object of class @tclass based on a SID pair (@ssid, @tsid).
1066 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1067 * if insufficient memory is available, or %0 if the SID was
1068 * computed successfully.
1069 */
1070int security_member_sid(u32 ssid,
1071 u32 tsid,
1072 u16 tclass,
1073 u32 *out_sid)
1074{
1075 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1076}
1077
1078/**
1079 * security_change_sid - Compute the SID for object relabeling.
1080 * @ssid: source security identifier
1081 * @tsid: target security identifier
1082 * @tclass: target security class
1083 * @out_sid: security identifier for selected member
1084 *
1085 * Compute a SID to use for relabeling an object of class @tclass
1086 * based on a SID pair (@ssid, @tsid).
1087 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1088 * if insufficient memory is available, or %0 if the SID was
1089 * computed successfully.
1090 */
1091int security_change_sid(u32 ssid,
1092 u32 tsid,
1093 u16 tclass,
1094 u32 *out_sid)
1095{
1096 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1097}
1098
Chad Sellersb94c7e62006-11-06 12:38:18 -05001099/*
1100 * Verify that each kernel class that is defined in the
1101 * policy is correct
1102 */
1103static int validate_classes(struct policydb *p)
1104{
1105 int i, j;
1106 struct class_datum *cladatum;
1107 struct perm_datum *perdatum;
1108 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1109 u16 class_val;
1110 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1111 const char *def_class, *def_perm, *pol_class;
1112 struct symtab *perms;
1113
Eric Paris3f120702007-09-21 14:37:10 -04001114 if (p->allow_unknown) {
1115 u32 num_classes = kdefs->cts_len;
1116 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1117 if (!p->undefined_perms)
1118 return -ENOMEM;
1119 }
1120
Chad Sellersb94c7e62006-11-06 12:38:18 -05001121 for (i = 1; i < kdefs->cts_len; i++) {
1122 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001123 if (!def_class)
1124 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001125 if (i > p->p_classes.nprim) {
1126 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001127 "SELinux: class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001128 def_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[i-1] = ~0U;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001133 continue;
1134 }
1135 pol_class = p->p_class_val_to_name[i-1];
1136 if (strcmp(pol_class, def_class)) {
1137 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001138 "SELinux: class %d is incorrect, found %s but should be %s\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001139 i, pol_class, def_class);
1140 return -EINVAL;
1141 }
1142 }
1143 for (i = 0; i < kdefs->av_pts_len; i++) {
1144 class_val = kdefs->av_perm_to_string[i].tclass;
1145 perm_val = kdefs->av_perm_to_string[i].value;
1146 def_perm = kdefs->av_perm_to_string[i].name;
1147 if (class_val > p->p_classes.nprim)
1148 continue;
1149 pol_class = p->p_class_val_to_name[class_val-1];
1150 cladatum = hashtab_search(p->p_classes.table, pol_class);
1151 BUG_ON(!cladatum);
1152 perms = &cladatum->permissions;
1153 nprim = 1 << (perms->nprim - 1);
1154 if (perm_val > nprim) {
1155 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001156 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001157 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001158 if (p->reject_unknown)
1159 return -EINVAL;
1160 if (p->allow_unknown)
1161 p->undefined_perms[class_val-1] |= perm_val;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001162 continue;
1163 }
1164 perdatum = hashtab_search(perms->table, def_perm);
1165 if (perdatum == NULL) {
1166 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001167 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001168 def_perm, pol_class);
1169 return -EINVAL;
1170 }
1171 pol_val = 1 << (perdatum->value - 1);
1172 if (pol_val != perm_val) {
1173 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001174 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001175 def_perm, pol_class);
1176 return -EINVAL;
1177 }
1178 }
1179 for (i = 0; i < kdefs->av_inherit_len; i++) {
1180 class_val = kdefs->av_inherit[i].tclass;
1181 if (class_val > p->p_classes.nprim)
1182 continue;
1183 pol_class = p->p_class_val_to_name[class_val-1];
1184 cladatum = hashtab_search(p->p_classes.table, pol_class);
1185 BUG_ON(!cladatum);
1186 if (!cladatum->comdatum) {
1187 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001188 "SELinux: class %s should have an inherits clause but does not\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001189 pol_class);
1190 return -EINVAL;
1191 }
1192 tmp = kdefs->av_inherit[i].common_base;
1193 common_pts_len = 0;
1194 while (!(tmp & 0x01)) {
1195 common_pts_len++;
1196 tmp >>= 1;
1197 }
1198 perms = &cladatum->comdatum->permissions;
1199 for (j = 0; j < common_pts_len; j++) {
1200 def_perm = kdefs->av_inherit[i].common_pts[j];
1201 if (j >= perms->nprim) {
1202 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001203 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001204 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001205 if (p->reject_unknown)
1206 return -EINVAL;
1207 if (p->allow_unknown)
1208 p->undefined_perms[class_val-1] |= (1 << j);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001209 continue;
1210 }
1211 perdatum = hashtab_search(perms->table, def_perm);
1212 if (perdatum == NULL) {
1213 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001214 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001215 def_perm, pol_class);
1216 return -EINVAL;
1217 }
1218 if (perdatum->value != j + 1) {
1219 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001220 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001221 def_perm, pol_class);
1222 return -EINVAL;
1223 }
1224 }
1225 }
1226 return 0;
1227}
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/* Clone the SID into the new SID table. */
1230static int clone_sid(u32 sid,
1231 struct context *context,
1232 void *arg)
1233{
1234 struct sidtab *s = arg;
1235
1236 return sidtab_insert(s, sid, context);
1237}
1238
1239static inline int convert_context_handle_invalid_context(struct context *context)
1240{
1241 int rc = 0;
1242
1243 if (selinux_enforcing) {
1244 rc = -EINVAL;
1245 } else {
1246 char *s;
1247 u32 len;
1248
1249 context_struct_to_string(context, &s, &len);
James Morris454d9722008-02-26 20:42:02 +11001250 printk(KERN_ERR "SELinux: context %s is invalid\n", s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 kfree(s);
1252 }
1253 return rc;
1254}
1255
1256struct convert_context_args {
1257 struct policydb *oldp;
1258 struct policydb *newp;
1259};
1260
1261/*
1262 * Convert the values in the security context
1263 * structure `c' from the values specified
1264 * in the policy `p->oldp' to the values specified
1265 * in the policy `p->newp'. Verify that the
1266 * context is valid under the new policy.
1267 */
1268static int convert_context(u32 key,
1269 struct context *c,
1270 void *p)
1271{
1272 struct convert_context_args *args;
1273 struct context oldc;
1274 struct role_datum *role;
1275 struct type_datum *typdatum;
1276 struct user_datum *usrdatum;
1277 char *s;
1278 u32 len;
1279 int rc;
1280
1281 args = p;
1282
1283 rc = context_cpy(&oldc, c);
1284 if (rc)
1285 goto out;
1286
1287 rc = -EINVAL;
1288
1289 /* Convert the user. */
1290 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001291 args->oldp->p_user_val_to_name[c->user - 1]);
1292 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 c->user = usrdatum->value;
1295
1296 /* Convert the role. */
1297 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001298 args->oldp->p_role_val_to_name[c->role - 1]);
1299 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 c->role = role->value;
1302
1303 /* Convert the type. */
1304 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001305 args->oldp->p_type_val_to_name[c->type - 1]);
1306 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 c->type = typdatum->value;
1309
1310 rc = mls_convert_context(args->oldp, args->newp, c);
1311 if (rc)
1312 goto bad;
1313
1314 /* Check the validity of the new context. */
1315 if (!policydb_context_isvalid(args->newp, c)) {
1316 rc = convert_context_handle_invalid_context(&oldc);
1317 if (rc)
1318 goto bad;
1319 }
1320
1321 context_destroy(&oldc);
1322out:
1323 return rc;
1324bad:
1325 context_struct_to_string(&oldc, &s, &len);
1326 context_destroy(&oldc);
James Morris454d9722008-02-26 20:42:02 +11001327 printk(KERN_ERR "SELinux: invalidating context %s\n", s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 kfree(s);
1329 goto out;
1330}
1331
Paul Moore3bb56b22008-01-29 08:38:19 -05001332static void security_load_policycaps(void)
1333{
1334 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1335 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001336 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1337 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001341static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
1343/**
1344 * security_load_policy - Load a security policy configuration.
1345 * @data: binary policy data
1346 * @len: length of data in bytes
1347 *
1348 * Load a new set of security policy configuration data,
1349 * validate it and convert the SID table as necessary.
1350 * This function will flush the access vector cache after
1351 * loading the new policy.
1352 */
1353int security_load_policy(void *data, size_t len)
1354{
1355 struct policydb oldpolicydb, newpolicydb;
1356 struct sidtab oldsidtab, newsidtab;
1357 struct convert_context_args args;
1358 u32 seqno;
1359 int rc = 0;
1360 struct policy_file file = { data, len }, *fp = &file;
1361
1362 LOAD_LOCK;
1363
1364 if (!ss_initialized) {
1365 avtab_cache_init();
1366 if (policydb_read(&policydb, fp)) {
1367 LOAD_UNLOCK;
1368 avtab_cache_destroy();
1369 return -EINVAL;
1370 }
1371 if (policydb_load_isids(&policydb, &sidtab)) {
1372 LOAD_UNLOCK;
1373 policydb_destroy(&policydb);
1374 avtab_cache_destroy();
1375 return -EINVAL;
1376 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001377 /* Verify that the kernel defined classes are correct. */
1378 if (validate_classes(&policydb)) {
1379 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001380 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001381 LOAD_UNLOCK;
1382 sidtab_destroy(&sidtab);
1383 policydb_destroy(&policydb);
1384 avtab_cache_destroy();
1385 return -EINVAL;
1386 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001387 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 policydb_loaded_version = policydb.policyvers;
1389 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001390 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 LOAD_UNLOCK;
1392 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001393 avc_ss_reset(seqno);
1394 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001395 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001396 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return 0;
1398 }
1399
1400#if 0
1401 sidtab_hash_eval(&sidtab, "sids");
1402#endif
1403
1404 if (policydb_read(&newpolicydb, fp)) {
1405 LOAD_UNLOCK;
1406 return -EINVAL;
1407 }
1408
1409 sidtab_init(&newsidtab);
1410
Chad Sellersb94c7e62006-11-06 12:38:18 -05001411 /* Verify that the kernel defined classes are correct. */
1412 if (validate_classes(&newpolicydb)) {
1413 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001414 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001415 rc = -EINVAL;
1416 goto err;
1417 }
1418
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001419 rc = security_preserve_bools(&newpolicydb);
1420 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001421 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001422 goto err;
1423 }
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 /* Clone the SID table. */
1426 sidtab_shutdown(&sidtab);
1427 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1428 rc = -ENOMEM;
1429 goto err;
1430 }
1431
1432 /* Convert the internal representations of contexts
1433 in the new SID table and remove invalid SIDs. */
1434 args.oldp = &policydb;
1435 args.newp = &newpolicydb;
1436 sidtab_map_remove_on_error(&newsidtab, convert_context, &args);
1437
1438 /* Save the old policydb and SID table to free later. */
1439 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1440 sidtab_set(&oldsidtab, &sidtab);
1441
1442 /* Install the new policydb and SID table. */
1443 POLICY_WRLOCK;
1444 memcpy(&policydb, &newpolicydb, sizeof policydb);
1445 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001446 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 seqno = ++latest_granting;
1448 policydb_loaded_version = policydb.policyvers;
1449 POLICY_WRUNLOCK;
1450 LOAD_UNLOCK;
1451
1452 /* Free the old policydb and SID table. */
1453 policydb_destroy(&oldpolicydb);
1454 sidtab_destroy(&oldsidtab);
1455
1456 avc_ss_reset(seqno);
1457 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001458 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001459 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 return 0;
1462
1463err:
1464 LOAD_UNLOCK;
1465 sidtab_destroy(&newsidtab);
1466 policydb_destroy(&newpolicydb);
1467 return rc;
1468
1469}
1470
1471/**
1472 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * @protocol: protocol number
1474 * @port: port number
1475 * @out_sid: security identifier
1476 */
Paul Moore3e112172008-04-10 10:48:14 -04001477int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct ocontext *c;
1480 int rc = 0;
1481
1482 POLICY_RDLOCK;
1483
1484 c = policydb.ocontexts[OCON_PORT];
1485 while (c) {
1486 if (c->u.port.protocol == protocol &&
1487 c->u.port.low_port <= port &&
1488 c->u.port.high_port >= port)
1489 break;
1490 c = c->next;
1491 }
1492
1493 if (c) {
1494 if (!c->sid[0]) {
1495 rc = sidtab_context_to_sid(&sidtab,
1496 &c->context[0],
1497 &c->sid[0]);
1498 if (rc)
1499 goto out;
1500 }
1501 *out_sid = c->sid[0];
1502 } else {
1503 *out_sid = SECINITSID_PORT;
1504 }
1505
1506out:
1507 POLICY_RDUNLOCK;
1508 return rc;
1509}
1510
1511/**
1512 * security_netif_sid - Obtain the SID for a network interface.
1513 * @name: interface name
1514 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001516int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517{
1518 int rc = 0;
1519 struct ocontext *c;
1520
1521 POLICY_RDLOCK;
1522
1523 c = policydb.ocontexts[OCON_NETIF];
1524 while (c) {
1525 if (strcmp(name, c->u.name) == 0)
1526 break;
1527 c = c->next;
1528 }
1529
1530 if (c) {
1531 if (!c->sid[0] || !c->sid[1]) {
1532 rc = sidtab_context_to_sid(&sidtab,
1533 &c->context[0],
1534 &c->sid[0]);
1535 if (rc)
1536 goto out;
1537 rc = sidtab_context_to_sid(&sidtab,
1538 &c->context[1],
1539 &c->sid[1]);
1540 if (rc)
1541 goto out;
1542 }
1543 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001544 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547out:
1548 POLICY_RDUNLOCK;
1549 return rc;
1550}
1551
1552static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1553{
1554 int i, fail = 0;
1555
Eric Paris5d55a342008-04-18 17:38:33 -04001556 for (i = 0; i < 4; i++)
1557 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 fail = 1;
1559 break;
1560 }
1561
1562 return !fail;
1563}
1564
1565/**
1566 * security_node_sid - Obtain the SID for a node (host).
1567 * @domain: communication domain aka address family
1568 * @addrp: address
1569 * @addrlen: address length in bytes
1570 * @out_sid: security identifier
1571 */
1572int security_node_sid(u16 domain,
1573 void *addrp,
1574 u32 addrlen,
1575 u32 *out_sid)
1576{
1577 int rc = 0;
1578 struct ocontext *c;
1579
1580 POLICY_RDLOCK;
1581
1582 switch (domain) {
1583 case AF_INET: {
1584 u32 addr;
1585
1586 if (addrlen != sizeof(u32)) {
1587 rc = -EINVAL;
1588 goto out;
1589 }
1590
1591 addr = *((u32 *)addrp);
1592
1593 c = policydb.ocontexts[OCON_NODE];
1594 while (c) {
1595 if (c->u.node.addr == (addr & c->u.node.mask))
1596 break;
1597 c = c->next;
1598 }
1599 break;
1600 }
1601
1602 case AF_INET6:
1603 if (addrlen != sizeof(u64) * 2) {
1604 rc = -EINVAL;
1605 goto out;
1606 }
1607 c = policydb.ocontexts[OCON_NODE6];
1608 while (c) {
1609 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1610 c->u.node6.mask))
1611 break;
1612 c = c->next;
1613 }
1614 break;
1615
1616 default:
1617 *out_sid = SECINITSID_NODE;
1618 goto out;
1619 }
1620
1621 if (c) {
1622 if (!c->sid[0]) {
1623 rc = sidtab_context_to_sid(&sidtab,
1624 &c->context[0],
1625 &c->sid[0]);
1626 if (rc)
1627 goto out;
1628 }
1629 *out_sid = c->sid[0];
1630 } else {
1631 *out_sid = SECINITSID_NODE;
1632 }
1633
1634out:
1635 POLICY_RDUNLOCK;
1636 return rc;
1637}
1638
1639#define SIDS_NEL 25
1640
1641/**
1642 * security_get_user_sids - Obtain reachable SIDs for a user.
1643 * @fromsid: starting SID
1644 * @username: username
1645 * @sids: array of reachable SIDs for user
1646 * @nel: number of elements in @sids
1647 *
1648 * Generate the set of SIDs for legal security contexts
1649 * for a given user that can be reached by @fromsid.
1650 * Set *@sids to point to a dynamically allocated
1651 * array containing the set of SIDs. Set *@nel to the
1652 * number of elements in the array.
1653 */
1654
1655int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04001656 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 u32 **sids,
1658 u32 *nel)
1659{
1660 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001661 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 u32 mynel = 0, maxnel = SIDS_NEL;
1663 struct user_datum *user;
1664 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001665 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 int rc = 0, i, j;
1667
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001668 *sids = NULL;
1669 *nel = 0;
1670
1671 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 POLICY_RDLOCK;
1675
1676 fromcon = sidtab_search(&sidtab, fromsid);
1677 if (!fromcon) {
1678 rc = -EINVAL;
1679 goto out_unlock;
1680 }
1681
1682 user = hashtab_search(policydb.p_users.table, username);
1683 if (!user) {
1684 rc = -EINVAL;
1685 goto out_unlock;
1686 }
1687 usercon.user = user->value;
1688
James Morris89d155e2005-10-30 14:59:21 -08001689 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (!mysids) {
1691 rc = -ENOMEM;
1692 goto out_unlock;
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001695 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 role = policydb.role_val_to_struct[i];
1697 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001698 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 usercon.type = j+1;
1700
1701 if (mls_setup_user_range(fromcon, user, &usercon))
1702 continue;
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001705 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (mynel < maxnel) {
1708 mysids[mynel++] = sid;
1709 } else {
1710 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001711 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (!mysids2) {
1713 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 goto out_unlock;
1715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1717 kfree(mysids);
1718 mysids = mysids2;
1719 mysids[mynel++] = sid;
1720 }
1721 }
1722 }
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724out_unlock:
1725 POLICY_RDUNLOCK;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001726 if (rc || !mynel) {
1727 kfree(mysids);
1728 goto out;
1729 }
1730
1731 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1732 if (!mysids2) {
1733 rc = -ENOMEM;
1734 kfree(mysids);
1735 goto out;
1736 }
1737 for (i = 0, j = 0; i < mynel; i++) {
1738 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1739 SECCLASS_PROCESS,
1740 PROCESS__TRANSITION, AVC_STRICT,
1741 NULL);
1742 if (!rc)
1743 mysids2[j++] = mysids[i];
1744 cond_resched();
1745 }
1746 rc = 0;
1747 kfree(mysids);
1748 *sids = mysids2;
1749 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750out:
1751 return rc;
1752}
1753
1754/**
1755 * security_genfs_sid - Obtain a SID for a file in a filesystem
1756 * @fstype: filesystem type
1757 * @path: path from root of mount
1758 * @sclass: file security class
1759 * @sid: SID for path
1760 *
1761 * Obtain a SID to use for a file in a filesystem that
1762 * cannot support xattr or use a fixed labeling behavior like
1763 * transition SIDs or task SIDs.
1764 */
1765int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04001766 char *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 u16 sclass,
1768 u32 *sid)
1769{
1770 int len;
1771 struct genfs *genfs;
1772 struct ocontext *c;
1773 int rc = 0, cmp = 0;
1774
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05001775 while (path[0] == '/' && path[1] == '/')
1776 path++;
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 POLICY_RDLOCK;
1779
1780 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
1781 cmp = strcmp(fstype, genfs->fstype);
1782 if (cmp <= 0)
1783 break;
1784 }
1785
1786 if (!genfs || cmp) {
1787 *sid = SECINITSID_UNLABELED;
1788 rc = -ENOENT;
1789 goto out;
1790 }
1791
1792 for (c = genfs->head; c; c = c->next) {
1793 len = strlen(c->u.name);
1794 if ((!c->v.sclass || sclass == c->v.sclass) &&
1795 (strncmp(c->u.name, path, len) == 0))
1796 break;
1797 }
1798
1799 if (!c) {
1800 *sid = SECINITSID_UNLABELED;
1801 rc = -ENOENT;
1802 goto out;
1803 }
1804
1805 if (!c->sid[0]) {
1806 rc = sidtab_context_to_sid(&sidtab,
1807 &c->context[0],
1808 &c->sid[0]);
1809 if (rc)
1810 goto out;
1811 }
1812
1813 *sid = c->sid[0];
1814out:
1815 POLICY_RDUNLOCK;
1816 return rc;
1817}
1818
1819/**
1820 * security_fs_use - Determine how to handle labeling for a filesystem.
1821 * @fstype: filesystem type
1822 * @behavior: labeling behavior
1823 * @sid: SID for filesystem (superblock)
1824 */
1825int security_fs_use(
1826 const char *fstype,
1827 unsigned int *behavior,
1828 u32 *sid)
1829{
1830 int rc = 0;
1831 struct ocontext *c;
1832
1833 POLICY_RDLOCK;
1834
1835 c = policydb.ocontexts[OCON_FSUSE];
1836 while (c) {
1837 if (strcmp(fstype, c->u.name) == 0)
1838 break;
1839 c = c->next;
1840 }
1841
1842 if (c) {
1843 *behavior = c->v.behavior;
1844 if (!c->sid[0]) {
1845 rc = sidtab_context_to_sid(&sidtab,
1846 &c->context[0],
1847 &c->sid[0]);
1848 if (rc)
1849 goto out;
1850 }
1851 *sid = c->sid[0];
1852 } else {
1853 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
1854 if (rc) {
1855 *behavior = SECURITY_FS_USE_NONE;
1856 rc = 0;
1857 } else {
1858 *behavior = SECURITY_FS_USE_GENFS;
1859 }
1860 }
1861
1862out:
1863 POLICY_RDUNLOCK;
1864 return rc;
1865}
1866
1867int security_get_bools(int *len, char ***names, int **values)
1868{
1869 int i, rc = -ENOMEM;
1870
1871 POLICY_RDLOCK;
1872 *names = NULL;
1873 *values = NULL;
1874
1875 *len = policydb.p_bools.nprim;
1876 if (!*len) {
1877 rc = 0;
1878 goto out;
1879 }
1880
Eric Paris5d55a342008-04-18 17:38:33 -04001881 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (!*names)
1883 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Jesper Juhle0795cf2006-01-09 20:54:46 -08001885 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (!*values)
1887 goto err;
1888
1889 for (i = 0; i < *len; i++) {
1890 size_t name_len;
1891 (*values)[i] = policydb.bool_val_to_struct[i]->state;
1892 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001893 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (!(*names)[i])
1895 goto err;
1896 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
1897 (*names)[i][name_len - 1] = 0;
1898 }
1899 rc = 0;
1900out:
1901 POLICY_RDUNLOCK;
1902 return rc;
1903err:
1904 if (*names) {
1905 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001906 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07001908 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 goto out;
1910}
1911
1912
1913int security_set_bools(int len, int *values)
1914{
1915 int i, rc = 0;
1916 int lenp, seqno = 0;
1917 struct cond_node *cur;
1918
1919 POLICY_WRLOCK;
1920
1921 lenp = policydb.p_bools.nprim;
1922 if (len != lenp) {
1923 rc = -EFAULT;
1924 goto out;
1925 }
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00001928 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
1929 audit_log(current->audit_context, GFP_ATOMIC,
1930 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05001931 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00001932 policydb.p_bool_val_to_name[i],
1933 !!values[i],
1934 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05001935 audit_get_loginuid(current),
1936 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00001937 }
Eric Paris5d55a342008-04-18 17:38:33 -04001938 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001940 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 for (cur = policydb.cond_list; cur != NULL; cur = cur->next) {
1945 rc = evaluate_cond_node(&policydb, cur);
1946 if (rc)
1947 goto out;
1948 }
1949
1950 seqno = ++latest_granting;
1951
1952out:
1953 POLICY_WRUNLOCK;
1954 if (!rc) {
1955 avc_ss_reset(seqno);
1956 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001957 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 return rc;
1960}
1961
1962int security_get_bool_value(int bool)
1963{
1964 int rc = 0;
1965 int len;
1966
1967 POLICY_RDLOCK;
1968
1969 len = policydb.p_bools.nprim;
1970 if (bool >= len) {
1971 rc = -EFAULT;
1972 goto out;
1973 }
1974
1975 rc = policydb.bool_val_to_struct[bool]->state;
1976out:
1977 POLICY_RDUNLOCK;
1978 return rc;
1979}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06001980
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001981static int security_preserve_bools(struct policydb *p)
1982{
1983 int rc, nbools = 0, *bvalues = NULL, i;
1984 char **bnames = NULL;
1985 struct cond_bool_datum *booldatum;
1986 struct cond_node *cur;
1987
1988 rc = security_get_bools(&nbools, &bnames, &bvalues);
1989 if (rc)
1990 goto out;
1991 for (i = 0; i < nbools; i++) {
1992 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
1993 if (booldatum)
1994 booldatum->state = bvalues[i];
1995 }
1996 for (cur = p->cond_list; cur != NULL; cur = cur->next) {
1997 rc = evaluate_cond_node(p, cur);
1998 if (rc)
1999 goto out;
2000 }
2001
2002out:
2003 if (bnames) {
2004 for (i = 0; i < nbools; i++)
2005 kfree(bnames[i]);
2006 }
2007 kfree(bnames);
2008 kfree(bvalues);
2009 return rc;
2010}
2011
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002012/*
2013 * security_sid_mls_copy() - computes a new sid based on the given
2014 * sid and the mls portion of mls_sid.
2015 */
2016int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2017{
2018 struct context *context1;
2019 struct context *context2;
2020 struct context newcon;
2021 char *s;
2022 u32 len;
2023 int rc = 0;
2024
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002025 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002026 *new_sid = sid;
2027 goto out;
2028 }
2029
2030 context_init(&newcon);
2031
2032 POLICY_RDLOCK;
2033 context1 = sidtab_search(&sidtab, sid);
2034 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002035 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2036 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002037 rc = -EINVAL;
2038 goto out_unlock;
2039 }
2040
2041 context2 = sidtab_search(&sidtab, mls_sid);
2042 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002043 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2044 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002045 rc = -EINVAL;
2046 goto out_unlock;
2047 }
2048
2049 newcon.user = context1->user;
2050 newcon.role = context1->role;
2051 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002052 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002053 if (rc)
2054 goto out_unlock;
2055
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002056 /* Check the validity of the new context. */
2057 if (!policydb_context_isvalid(&policydb, &newcon)) {
2058 rc = convert_context_handle_invalid_context(&newcon);
2059 if (rc)
2060 goto bad;
2061 }
2062
2063 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2064 goto out_unlock;
2065
2066bad:
2067 if (!context_struct_to_string(&newcon, &s, &len)) {
2068 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2069 "security_sid_mls_copy: invalid context %s", s);
2070 kfree(s);
2071 }
2072
2073out_unlock:
2074 POLICY_RDUNLOCK;
2075 context_destroy(&newcon);
2076out:
2077 return rc;
2078}
2079
Paul Moore220deb92008-01-29 08:38:23 -05002080/**
2081 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2082 * @nlbl_sid: NetLabel SID
2083 * @nlbl_type: NetLabel labeling protocol type
2084 * @xfrm_sid: XFRM SID
2085 *
2086 * Description:
2087 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2088 * resolved into a single SID it is returned via @peer_sid and the function
2089 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2090 * returns a negative value. A table summarizing the behavior is below:
2091 *
2092 * | function return | @sid
2093 * ------------------------------+-----------------+-----------------
2094 * no peer labels | 0 | SECSID_NULL
2095 * single peer label | 0 | <peer_label>
2096 * multiple, consistent labels | 0 | <peer_label>
2097 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2098 *
2099 */
2100int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2101 u32 xfrm_sid,
2102 u32 *peer_sid)
2103{
2104 int rc;
2105 struct context *nlbl_ctx;
2106 struct context *xfrm_ctx;
2107
2108 /* handle the common (which also happens to be the set of easy) cases
2109 * right away, these two if statements catch everything involving a
2110 * single or absent peer SID/label */
2111 if (xfrm_sid == SECSID_NULL) {
2112 *peer_sid = nlbl_sid;
2113 return 0;
2114 }
2115 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2116 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2117 * is present */
2118 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2119 *peer_sid = xfrm_sid;
2120 return 0;
2121 }
2122
2123 /* we don't need to check ss_initialized here since the only way both
2124 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2125 * security server was initialized and ss_initialized was true */
2126 if (!selinux_mls_enabled) {
2127 *peer_sid = SECSID_NULL;
2128 return 0;
2129 }
2130
2131 POLICY_RDLOCK;
2132
2133 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2134 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002135 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2136 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002137 rc = -EINVAL;
2138 goto out_slowpath;
2139 }
2140 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2141 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002142 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2143 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002144 rc = -EINVAL;
2145 goto out_slowpath;
2146 }
2147 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2148
2149out_slowpath:
2150 POLICY_RDUNLOCK;
2151 if (rc == 0)
2152 /* at present NetLabel SIDs/labels really only carry MLS
2153 * information so if the MLS portion of the NetLabel SID
2154 * matches the MLS portion of the labeled XFRM SID/label
2155 * then pass along the XFRM SID as it is the most
2156 * expressive */
2157 *peer_sid = xfrm_sid;
2158 else
2159 *peer_sid = SECSID_NULL;
2160 return rc;
2161}
2162
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002163static int get_classes_callback(void *k, void *d, void *args)
2164{
2165 struct class_datum *datum = d;
2166 char *name = k, **classes = args;
2167 int value = datum->value - 1;
2168
2169 classes[value] = kstrdup(name, GFP_ATOMIC);
2170 if (!classes[value])
2171 return -ENOMEM;
2172
2173 return 0;
2174}
2175
2176int security_get_classes(char ***classes, int *nclasses)
2177{
2178 int rc = -ENOMEM;
2179
2180 POLICY_RDLOCK;
2181
2182 *nclasses = policydb.p_classes.nprim;
2183 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2184 if (!*classes)
2185 goto out;
2186
2187 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2188 *classes);
2189 if (rc < 0) {
2190 int i;
2191 for (i = 0; i < *nclasses; i++)
2192 kfree((*classes)[i]);
2193 kfree(*classes);
2194 }
2195
2196out:
2197 POLICY_RDUNLOCK;
2198 return rc;
2199}
2200
2201static int get_permissions_callback(void *k, void *d, void *args)
2202{
2203 struct perm_datum *datum = d;
2204 char *name = k, **perms = args;
2205 int value = datum->value - 1;
2206
2207 perms[value] = kstrdup(name, GFP_ATOMIC);
2208 if (!perms[value])
2209 return -ENOMEM;
2210
2211 return 0;
2212}
2213
2214int security_get_permissions(char *class, char ***perms, int *nperms)
2215{
2216 int rc = -ENOMEM, i;
2217 struct class_datum *match;
2218
2219 POLICY_RDLOCK;
2220
2221 match = hashtab_search(policydb.p_classes.table, class);
2222 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002223 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002224 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002225 rc = -EINVAL;
2226 goto out;
2227 }
2228
2229 *nperms = match->permissions.nprim;
2230 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2231 if (!*perms)
2232 goto out;
2233
2234 if (match->comdatum) {
2235 rc = hashtab_map(match->comdatum->permissions.table,
2236 get_permissions_callback, *perms);
2237 if (rc < 0)
2238 goto err;
2239 }
2240
2241 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2242 *perms);
2243 if (rc < 0)
2244 goto err;
2245
2246out:
2247 POLICY_RDUNLOCK;
2248 return rc;
2249
2250err:
2251 POLICY_RDUNLOCK;
2252 for (i = 0; i < *nperms; i++)
2253 kfree((*perms)[i]);
2254 kfree(*perms);
2255 return rc;
2256}
2257
Eric Paris3f120702007-09-21 14:37:10 -04002258int security_get_reject_unknown(void)
2259{
2260 return policydb.reject_unknown;
2261}
2262
2263int security_get_allow_unknown(void)
2264{
2265 return policydb.allow_unknown;
2266}
2267
Paul Moore3bb56b22008-01-29 08:38:19 -05002268/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002269 * security_policycap_supported - Check for a specific policy capability
2270 * @req_cap: capability
2271 *
2272 * Description:
2273 * This function queries the currently loaded policy to see if it supports the
2274 * capability specified by @req_cap. Returns true (1) if the capability is
2275 * supported, false (0) if it isn't supported.
2276 *
2277 */
2278int security_policycap_supported(unsigned int req_cap)
2279{
2280 int rc;
2281
2282 POLICY_RDLOCK;
2283 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
2284 POLICY_RDUNLOCK;
2285
2286 return rc;
2287}
2288
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002289struct selinux_audit_rule {
2290 u32 au_seqno;
2291 struct context au_ctxt;
2292};
2293
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002294void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002295{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002296 struct selinux_audit_rule *rule = vrule;
2297
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002298 if (rule) {
2299 context_destroy(&rule->au_ctxt);
2300 kfree(rule);
2301 }
2302}
2303
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002304int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002305{
2306 struct selinux_audit_rule *tmprule;
2307 struct role_datum *roledatum;
2308 struct type_datum *typedatum;
2309 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002310 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002311 int rc = 0;
2312
2313 *rule = NULL;
2314
2315 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002316 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002317
2318 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002319 case AUDIT_SUBJ_USER:
2320 case AUDIT_SUBJ_ROLE:
2321 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002322 case AUDIT_OBJ_USER:
2323 case AUDIT_OBJ_ROLE:
2324 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002325 /* only 'equals' and 'not equals' fit user, role, and type */
2326 if (op != AUDIT_EQUAL && op != AUDIT_NOT_EQUAL)
2327 return -EINVAL;
2328 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002329 case AUDIT_SUBJ_SEN:
2330 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002331 case AUDIT_OBJ_LEV_LOW:
2332 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002333 /* we do not allow a range, indicated by the presense of '-' */
2334 if (strchr(rulestr, '-'))
2335 return -EINVAL;
2336 break;
2337 default:
2338 /* only the above fields are valid */
2339 return -EINVAL;
2340 }
2341
2342 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2343 if (!tmprule)
2344 return -ENOMEM;
2345
2346 context_init(&tmprule->au_ctxt);
2347
2348 POLICY_RDLOCK;
2349
2350 tmprule->au_seqno = latest_granting;
2351
2352 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002353 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002354 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002355 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2356 if (!userdatum)
2357 rc = -EINVAL;
2358 else
2359 tmprule->au_ctxt.user = userdatum->value;
2360 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002361 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002362 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002363 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2364 if (!roledatum)
2365 rc = -EINVAL;
2366 else
2367 tmprule->au_ctxt.role = roledatum->value;
2368 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002369 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002370 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002371 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2372 if (!typedatum)
2373 rc = -EINVAL;
2374 else
2375 tmprule->au_ctxt.type = typedatum->value;
2376 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002377 case AUDIT_SUBJ_SEN:
2378 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002379 case AUDIT_OBJ_LEV_LOW:
2380 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002381 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2382 break;
2383 }
2384
2385 POLICY_RDUNLOCK;
2386
2387 if (rc) {
2388 selinux_audit_rule_free(tmprule);
2389 tmprule = NULL;
2390 }
2391
2392 *rule = tmprule;
2393
2394 return rc;
2395}
2396
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002397/* Check to see if the rule contains any selinux fields */
2398int selinux_audit_rule_known(struct audit_krule *rule)
2399{
2400 int i;
2401
2402 for (i = 0; i < rule->field_count; i++) {
2403 struct audit_field *f = &rule->fields[i];
2404 switch (f->type) {
2405 case AUDIT_SUBJ_USER:
2406 case AUDIT_SUBJ_ROLE:
2407 case AUDIT_SUBJ_TYPE:
2408 case AUDIT_SUBJ_SEN:
2409 case AUDIT_SUBJ_CLR:
2410 case AUDIT_OBJ_USER:
2411 case AUDIT_OBJ_ROLE:
2412 case AUDIT_OBJ_TYPE:
2413 case AUDIT_OBJ_LEV_LOW:
2414 case AUDIT_OBJ_LEV_HIGH:
2415 return 1;
2416 }
2417 }
2418
2419 return 0;
2420}
2421
2422int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002423 struct audit_context *actx)
2424{
2425 struct context *ctxt;
2426 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002427 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002428 int match = 0;
2429
2430 if (!rule) {
2431 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002432 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002433 return -ENOENT;
2434 }
2435
2436 POLICY_RDLOCK;
2437
2438 if (rule->au_seqno < latest_granting) {
2439 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002440 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002441 match = -ESTALE;
2442 goto out;
2443 }
2444
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002445 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002446 if (!ctxt) {
2447 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002448 "selinux_audit_rule_match: unrecognized SID %d\n",
2449 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002450 match = -ENOENT;
2451 goto out;
2452 }
2453
2454 /* a field/op pair that is not caught here will simply fall through
2455 without a match */
2456 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002457 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002458 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002459 switch (op) {
2460 case AUDIT_EQUAL:
2461 match = (ctxt->user == rule->au_ctxt.user);
2462 break;
2463 case AUDIT_NOT_EQUAL:
2464 match = (ctxt->user != rule->au_ctxt.user);
2465 break;
2466 }
2467 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002468 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002469 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002470 switch (op) {
2471 case AUDIT_EQUAL:
2472 match = (ctxt->role == rule->au_ctxt.role);
2473 break;
2474 case AUDIT_NOT_EQUAL:
2475 match = (ctxt->role != rule->au_ctxt.role);
2476 break;
2477 }
2478 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002479 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002480 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002481 switch (op) {
2482 case AUDIT_EQUAL:
2483 match = (ctxt->type == rule->au_ctxt.type);
2484 break;
2485 case AUDIT_NOT_EQUAL:
2486 match = (ctxt->type != rule->au_ctxt.type);
2487 break;
2488 }
2489 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002490 case AUDIT_SUBJ_SEN:
2491 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002492 case AUDIT_OBJ_LEV_LOW:
2493 case AUDIT_OBJ_LEV_HIGH:
2494 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002495 field == AUDIT_OBJ_LEV_LOW) ?
2496 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002497 switch (op) {
2498 case AUDIT_EQUAL:
2499 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002500 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002501 break;
2502 case AUDIT_NOT_EQUAL:
2503 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002504 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002505 break;
2506 case AUDIT_LESS_THAN:
2507 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002508 level) &&
2509 !mls_level_eq(&rule->au_ctxt.range.level[0],
2510 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002511 break;
2512 case AUDIT_LESS_THAN_OR_EQUAL:
2513 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002514 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002515 break;
2516 case AUDIT_GREATER_THAN:
2517 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002518 &rule->au_ctxt.range.level[0]) &&
2519 !mls_level_eq(level,
2520 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002521 break;
2522 case AUDIT_GREATER_THAN_OR_EQUAL:
2523 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002524 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002525 break;
2526 }
2527 }
2528
2529out:
2530 POLICY_RDUNLOCK;
2531 return match;
2532}
2533
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002534static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002535
2536static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
2537 u16 class, u32 perms, u32 *retained)
2538{
2539 int err = 0;
2540
2541 if (event == AVC_CALLBACK_RESET && aurule_callback)
2542 err = aurule_callback();
2543 return err;
2544}
2545
2546static int __init aurule_init(void)
2547{
2548 int err;
2549
2550 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002551 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002552 if (err)
2553 panic("avc_add_callback() failed, error %d\n", err);
2554
2555 return err;
2556}
2557__initcall(aurule_init);
2558
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002559#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002560/**
Paul Moore5778eab2007-02-28 15:14:22 -05002561 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2562 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002563 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002564 *
2565 * Description:
2566 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002567 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2568 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002569 *
2570 */
Paul Moore5778eab2007-02-28 15:14:22 -05002571static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002572 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002573{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002574 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002575
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002576 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2577 if (sid_cache == NULL)
2578 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002579 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002580 if (secattr->cache == NULL) {
2581 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002582 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002583 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002584
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002585 *sid_cache = sid;
2586 secattr->cache->free = kfree;
2587 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002588 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002589}
2590
2591/**
Paul Moore5778eab2007-02-28 15:14:22 -05002592 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002593 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002594 * @sid: the SELinux SID
2595 *
2596 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002597 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002598 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002599 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2600 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2601 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2602 * conversion for future lookups. Returns zero on success, negative values on
2603 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002604 *
2605 */
Paul Moore5778eab2007-02-28 15:14:22 -05002606int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002607 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002608{
2609 int rc = -EIDRM;
2610 struct context *ctx;
2611 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002612
2613 if (!ss_initialized) {
2614 *sid = SECSID_NULL;
2615 return 0;
2616 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002617
2618 POLICY_RDLOCK;
2619
Paul Moore701a90b2006-11-17 17:38:46 -05002620 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002621 *sid = *(u32 *)secattr->cache->data;
2622 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002623 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2624 *sid = secattr->attr.secid;
2625 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05002626 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002627 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002628 if (ctx == NULL)
2629 goto netlbl_secattr_to_sid_return;
2630
2631 ctx_new.user = ctx->user;
2632 ctx_new.role = ctx->role;
2633 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002634 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002635 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002636 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05002637 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002638 goto netlbl_secattr_to_sid_return;
2639 ctx_new.range.level[1].cat.highbit =
2640 ctx_new.range.level[0].cat.highbit;
2641 ctx_new.range.level[1].cat.node =
2642 ctx_new.range.level[0].cat.node;
2643 } else {
2644 ebitmap_init(&ctx_new.range.level[0].cat);
2645 ebitmap_init(&ctx_new.range.level[1].cat);
2646 }
2647 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2648 goto netlbl_secattr_to_sid_return_cleanup;
2649
2650 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2651 if (rc != 0)
2652 goto netlbl_secattr_to_sid_return_cleanup;
2653
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002654 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05002655
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002656 ebitmap_destroy(&ctx_new.range.level[0].cat);
2657 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002658 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002659 rc = 0;
2660 }
2661
2662netlbl_secattr_to_sid_return:
2663 POLICY_RDUNLOCK;
2664 return rc;
2665netlbl_secattr_to_sid_return_cleanup:
2666 ebitmap_destroy(&ctx_new.range.level[0].cat);
2667 goto netlbl_secattr_to_sid_return;
2668}
2669
2670/**
Paul Moore5778eab2007-02-28 15:14:22 -05002671 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2672 * @sid: the SELinux SID
2673 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002674 *
2675 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002676 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2677 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002678 *
2679 */
Paul Moore5778eab2007-02-28 15:14:22 -05002680int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002681{
2682 int rc = -ENOENT;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002683 struct context *ctx;
2684
2685 if (!ss_initialized)
2686 return 0;
2687
2688 POLICY_RDLOCK;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002689 ctx = sidtab_search(&sidtab, sid);
2690 if (ctx == NULL)
Paul Moore5778eab2007-02-28 15:14:22 -05002691 goto netlbl_sid_to_secattr_failure;
2692 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2693 GFP_ATOMIC);
Paul Moore00447872008-04-12 19:06:42 -07002694 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY;
Paul Moore5778eab2007-02-28 15:14:22 -05002695 mls_export_netlbl_lvl(ctx, secattr);
2696 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002697 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05002698 goto netlbl_sid_to_secattr_failure;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002699 POLICY_RDUNLOCK;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002700
Paul Moore5778eab2007-02-28 15:14:22 -05002701 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05002702
Paul Moore5778eab2007-02-28 15:14:22 -05002703netlbl_sid_to_secattr_failure:
2704 POLICY_RDUNLOCK;
Paul Mooref8687af2006-10-30 15:22:15 -08002705 return rc;
2706}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002707#endif /* CONFIG_NETLABEL */