blob: cad5765ca1cbb10a48523f9a51f48d75e1aee039 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static struct sidtab sidtab;
76struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040077int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79/*
80 * The largest sequence number that has been used when
81 * providing an access decision to the access vector cache.
82 * The sequence number only changes when a policy change
83 * occurs.
84 */
Eric Paris5d55a342008-04-18 17:38:33 -040085static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87/* Forward declaration. */
88static int context_struct_to_string(struct context *context, char **scontext,
89 u32 *scontext_len);
90
KaiGai Koheid9250de2008-08-28 16:35:57 +090091static int context_struct_compute_av(struct context *scontext,
92 struct context *tcontext,
93 u16 tclass,
94 u32 requested,
95 struct av_decision *avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/*
97 * Return the boolean value of a constraint expression
98 * when it is applied to the specified source and target
99 * security contexts.
100 *
101 * xcontext is a special beast... It is used by the validatetrans rules
102 * only. For these rules, scontext is the context before the transition,
103 * tcontext is the context after the transition, and xcontext is the context
104 * of the process performing the transition. All other callers of
105 * constraint_expr_eval should pass in NULL for xcontext.
106 */
107static int constraint_expr_eval(struct context *scontext,
108 struct context *tcontext,
109 struct context *xcontext,
110 struct constraint_expr *cexpr)
111{
112 u32 val1, val2;
113 struct context *c;
114 struct role_datum *r1, *r2;
115 struct mls_level *l1, *l2;
116 struct constraint_expr *e;
117 int s[CEXPR_MAXDEPTH];
118 int sp = -1;
119
120 for (e = cexpr; e; e = e->next) {
121 switch (e->expr_type) {
122 case CEXPR_NOT:
123 BUG_ON(sp < 0);
124 s[sp] = !s[sp];
125 break;
126 case CEXPR_AND:
127 BUG_ON(sp < 1);
128 sp--;
129 s[sp] &= s[sp+1];
130 break;
131 case CEXPR_OR:
132 BUG_ON(sp < 1);
133 sp--;
134 s[sp] |= s[sp+1];
135 break;
136 case CEXPR_ATTR:
137 if (sp == (CEXPR_MAXDEPTH-1))
138 return 0;
139 switch (e->attr) {
140 case CEXPR_USER:
141 val1 = scontext->user;
142 val2 = tcontext->user;
143 break;
144 case CEXPR_TYPE:
145 val1 = scontext->type;
146 val2 = tcontext->type;
147 break;
148 case CEXPR_ROLE:
149 val1 = scontext->role;
150 val2 = tcontext->role;
151 r1 = policydb.role_val_to_struct[val1 - 1];
152 r2 = policydb.role_val_to_struct[val2 - 1];
153 switch (e->op) {
154 case CEXPR_DOM:
155 s[++sp] = ebitmap_get_bit(&r1->dominates,
156 val2 - 1);
157 continue;
158 case CEXPR_DOMBY:
159 s[++sp] = ebitmap_get_bit(&r2->dominates,
160 val1 - 1);
161 continue;
162 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400163 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
164 val2 - 1) &&
165 !ebitmap_get_bit(&r2->dominates,
166 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 continue;
168 default:
169 break;
170 }
171 break;
172 case CEXPR_L1L2:
173 l1 = &(scontext->range.level[0]);
174 l2 = &(tcontext->range.level[0]);
175 goto mls_ops;
176 case CEXPR_L1H2:
177 l1 = &(scontext->range.level[0]);
178 l2 = &(tcontext->range.level[1]);
179 goto mls_ops;
180 case CEXPR_H1L2:
181 l1 = &(scontext->range.level[1]);
182 l2 = &(tcontext->range.level[0]);
183 goto mls_ops;
184 case CEXPR_H1H2:
185 l1 = &(scontext->range.level[1]);
186 l2 = &(tcontext->range.level[1]);
187 goto mls_ops;
188 case CEXPR_L1H1:
189 l1 = &(scontext->range.level[0]);
190 l2 = &(scontext->range.level[1]);
191 goto mls_ops;
192 case CEXPR_L2H2:
193 l1 = &(tcontext->range.level[0]);
194 l2 = &(tcontext->range.level[1]);
195 goto mls_ops;
196mls_ops:
197 switch (e->op) {
198 case CEXPR_EQ:
199 s[++sp] = mls_level_eq(l1, l2);
200 continue;
201 case CEXPR_NEQ:
202 s[++sp] = !mls_level_eq(l1, l2);
203 continue;
204 case CEXPR_DOM:
205 s[++sp] = mls_level_dom(l1, l2);
206 continue;
207 case CEXPR_DOMBY:
208 s[++sp] = mls_level_dom(l2, l1);
209 continue;
210 case CEXPR_INCOMP:
211 s[++sp] = mls_level_incomp(l2, l1);
212 continue;
213 default:
214 BUG();
215 return 0;
216 }
217 break;
218 default:
219 BUG();
220 return 0;
221 }
222
223 switch (e->op) {
224 case CEXPR_EQ:
225 s[++sp] = (val1 == val2);
226 break;
227 case CEXPR_NEQ:
228 s[++sp] = (val1 != val2);
229 break;
230 default:
231 BUG();
232 return 0;
233 }
234 break;
235 case CEXPR_NAMES:
236 if (sp == (CEXPR_MAXDEPTH-1))
237 return 0;
238 c = scontext;
239 if (e->attr & CEXPR_TARGET)
240 c = tcontext;
241 else if (e->attr & CEXPR_XTARGET) {
242 c = xcontext;
243 if (!c) {
244 BUG();
245 return 0;
246 }
247 }
248 if (e->attr & CEXPR_USER)
249 val1 = c->user;
250 else if (e->attr & CEXPR_ROLE)
251 val1 = c->role;
252 else if (e->attr & CEXPR_TYPE)
253 val1 = c->type;
254 else {
255 BUG();
256 return 0;
257 }
258
259 switch (e->op) {
260 case CEXPR_EQ:
261 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
262 break;
263 case CEXPR_NEQ:
264 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
265 break;
266 default:
267 BUG();
268 return 0;
269 }
270 break;
271 default:
272 BUG();
273 return 0;
274 }
275 }
276
277 BUG_ON(sp != 0);
278 return s[0];
279}
280
281/*
KaiGai Koheid9250de2008-08-28 16:35:57 +0900282 * security_boundary_permission - drops violated permissions
283 * on boundary constraint.
284 */
285static void type_attribute_bounds_av(struct context *scontext,
286 struct context *tcontext,
287 u16 tclass,
288 u32 requested,
289 struct av_decision *avd)
290{
291 struct context lo_scontext;
292 struct context lo_tcontext;
293 struct av_decision lo_avd;
294 struct type_datum *source
295 = policydb.type_val_to_struct[scontext->type - 1];
296 struct type_datum *target
297 = policydb.type_val_to_struct[tcontext->type - 1];
298 u32 masked = 0;
299
300 if (source->bounds) {
301 memset(&lo_avd, 0, sizeof(lo_avd));
302
303 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
304 lo_scontext.type = source->bounds;
305
306 context_struct_compute_av(&lo_scontext,
307 tcontext,
308 tclass,
309 requested,
310 &lo_avd);
311 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
312 return; /* no masked permission */
313 masked = ~lo_avd.allowed & avd->allowed;
314 }
315
316 if (target->bounds) {
317 memset(&lo_avd, 0, sizeof(lo_avd));
318
319 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
320 lo_tcontext.type = target->bounds;
321
322 context_struct_compute_av(scontext,
323 &lo_tcontext,
324 tclass,
325 requested,
326 &lo_avd);
327 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
328 return; /* no masked permission */
329 masked = ~lo_avd.allowed & avd->allowed;
330 }
331
332 if (source->bounds && target->bounds) {
333 memset(&lo_avd, 0, sizeof(lo_avd));
334 /*
335 * lo_scontext and lo_tcontext are already
336 * set up.
337 */
338
339 context_struct_compute_av(&lo_scontext,
340 &lo_tcontext,
341 tclass,
342 requested,
343 &lo_avd);
344 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
345 return; /* no masked permission */
346 masked = ~lo_avd.allowed & avd->allowed;
347 }
348
349 if (masked) {
350 struct audit_buffer *ab;
351 char *stype_name
352 = policydb.p_type_val_to_name[source->value - 1];
353 char *ttype_name
354 = policydb.p_type_val_to_name[target->value - 1];
355 char *tclass_name
356 = policydb.p_class_val_to_name[tclass - 1];
357
358 /* mask violated permissions */
359 avd->allowed &= ~masked;
360
361 /* notice to userspace via audit message */
362 ab = audit_log_start(current->audit_context,
363 GFP_ATOMIC, AUDIT_SELINUX_ERR);
364 if (!ab)
365 return;
366
367 audit_log_format(ab, "av boundary violation: "
368 "source=%s target=%s tclass=%s",
369 stype_name, ttype_name, tclass_name);
370 avc_dump_av(ab, tclass, masked);
371 audit_log_end(ab);
372 }
373}
374
375/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 * Compute access vectors based on a context structure pair for
377 * the permissions in a particular class.
378 */
379static int context_struct_compute_av(struct context *scontext,
380 struct context *tcontext,
381 u16 tclass,
382 u32 requested,
383 struct av_decision *avd)
384{
385 struct constraint_node *constraint;
386 struct role_allow *ra;
387 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700388 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700390 struct ebitmap *sattr, *tattr;
391 struct ebitmap_node *snode, *tnode;
Eric Paris3f120702007-09-21 14:37:10 -0400392 const struct selinux_class_perm *kdefs = &selinux_class_perm;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700393 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 /*
396 * Remap extended Netlink classes for old policy versions.
397 * Do this here rather than socket_type_to_security_class()
398 * in case a newer policy version is loaded, allowing sockets
399 * to remain in the correct class.
400 */
401 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
402 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
403 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
404 tclass = SECCLASS_NETLINK_SOCKET;
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
407 * Initialize the access vectors to the default values.
408 */
409 avd->allowed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 avd->auditallow = 0;
411 avd->auditdeny = 0xffffffff;
412 avd->seqno = latest_granting;
413
414 /*
Eric Paris3f120702007-09-21 14:37:10 -0400415 * Check for all the invalid cases.
416 * - tclass 0
417 * - tclass > policy and > kernel
418 * - tclass > policy but is a userspace class
419 * - tclass > policy but we do not allow unknowns
420 */
421 if (unlikely(!tclass))
422 goto inval_class;
423 if (unlikely(tclass > policydb.p_classes.nprim))
424 if (tclass > kdefs->cts_len ||
Eric Pariscea78dc2008-06-09 15:43:12 -0400425 !kdefs->class_to_string[tclass] ||
Eric Paris3f120702007-09-21 14:37:10 -0400426 !policydb.allow_unknown)
427 goto inval_class;
428
429 /*
430 * Kernel class and we allow unknown so pad the allow decision
431 * the pad will be all 1 for unknown classes.
432 */
433 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
434 avd->allowed = policydb.undefined_perms[tclass - 1];
435
436 /*
437 * Not in policy. Since decision is completed (all 1 or all 0) return.
438 */
439 if (unlikely(tclass > policydb.p_classes.nprim))
440 return 0;
441
442 tclass_datum = policydb.class_val_to_struct[tclass - 1];
443
444 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 * If a specific type enforcement rule was defined for
446 * this permission check, then use it.
447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700449 avkey.specified = AVTAB_AV;
450 sattr = &policydb.type_attr_map[scontext->type - 1];
451 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900452 ebitmap_for_each_positive_bit(sattr, snode, i) {
453 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700454 avkey.source_type = i + 1;
455 avkey.target_type = j + 1;
456 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300457 node;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700458 node = avtab_search_node_next(node, avkey.specified)) {
459 if (node->key.specified == AVTAB_ALLOWED)
460 avd->allowed |= node->datum.data;
461 else if (node->key.specified == AVTAB_AUDITALLOW)
462 avd->auditallow |= node->datum.data;
463 else if (node->key.specified == AVTAB_AUDITDENY)
464 avd->auditdeny &= node->datum.data;
465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Stephen Smalley782ebb92005-09-03 15:55:16 -0700467 /* Check conditional av table for additional permissions */
468 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
469
470 }
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * Remove any permissions prohibited by a constraint (this includes
475 * the MLS policy).
476 */
477 constraint = tclass_datum->constraints;
478 while (constraint) {
479 if ((constraint->permissions & (avd->allowed)) &&
480 !constraint_expr_eval(scontext, tcontext, NULL,
481 constraint->expr)) {
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900482 avd->allowed &= ~(constraint->permissions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
484 constraint = constraint->next;
485 }
486
487 /*
488 * If checking process transition permission and the
489 * role is changing, then check the (current_role, new_role)
490 * pair.
491 */
492 if (tclass == SECCLASS_PROCESS &&
493 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
494 scontext->role != tcontext->role) {
495 for (ra = policydb.role_allow; ra; ra = ra->next) {
496 if (scontext->role == ra->role &&
497 tcontext->role == ra->new_role)
498 break;
499 }
500 if (!ra)
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900501 avd->allowed &= ~(PROCESS__TRANSITION |
502 PROCESS__DYNTRANSITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
KaiGai Koheid9250de2008-08-28 16:35:57 +0900505 /*
506 * If the given source and target types have boundary
507 * constraint, lazy checks have to mask any violated
508 * permission and notice it to userspace via audit.
509 */
510 type_attribute_bounds_av(scontext, tcontext,
511 tclass, requested, avd);
512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return 0;
Eric Paris3f120702007-09-21 14:37:10 -0400514
515inval_class:
Stephen Smalley22df4ad2008-06-09 16:03:56 -0400516 if (!tclass || tclass > kdefs->cts_len ||
517 !kdefs->class_to_string[tclass]) {
518 if (printk_ratelimit())
519 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
520 __func__, tclass);
521 return -EINVAL;
522 }
523
524 /*
525 * Known to the kernel, but not to the policy.
526 * Handle as a denial (allowed is 0).
527 */
528 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Eric Paris64dbf072008-03-31 12:17:33 +1100531/*
532 * Given a sid find if the type has the permissive flag set
533 */
534int security_permissive_sid(u32 sid)
535{
536 struct context *context;
537 u32 type;
538 int rc;
539
James Morris0804d112008-06-06 18:40:29 +1000540 read_lock(&policy_rwlock);
Eric Paris64dbf072008-03-31 12:17:33 +1100541
542 context = sidtab_search(&sidtab, sid);
543 BUG_ON(!context);
544
545 type = context->type;
546 /*
547 * we are intentionally using type here, not type-1, the 0th bit may
548 * someday indicate that we are globally setting permissive in policy.
549 */
550 rc = ebitmap_get_bit(&policydb.permissive_map, type);
551
James Morris0804d112008-06-06 18:40:29 +1000552 read_unlock(&policy_rwlock);
Eric Paris64dbf072008-03-31 12:17:33 +1100553 return rc;
554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400557 struct context *ncontext,
558 struct context *tcontext,
559 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 char *o = NULL, *n = NULL, *t = NULL;
562 u32 olen, nlen, tlen;
563
564 if (context_struct_to_string(ocontext, &o, &olen) < 0)
565 goto out;
566 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
567 goto out;
568 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
569 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100570 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400571 "security_validate_transition: denied for"
572 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
573 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574out:
575 kfree(o);
576 kfree(n);
577 kfree(t);
578
579 if (!selinux_enforcing)
580 return 0;
581 return -EPERM;
582}
583
584int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Eric Paris5d55a342008-04-18 17:38:33 -0400585 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 struct context *ocontext;
588 struct context *ncontext;
589 struct context *tcontext;
590 struct class_datum *tclass_datum;
591 struct constraint_node *constraint;
592 int rc = 0;
593
594 if (!ss_initialized)
595 return 0;
596
James Morris0804d112008-06-06 18:40:29 +1000597 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /*
600 * Remap extended Netlink classes for old policy versions.
601 * Do this here rather than socket_type_to_security_class()
602 * in case a newer policy version is loaded, allowing sockets
603 * to remain in the correct class.
604 */
605 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
606 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
607 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
608 tclass = SECCLASS_NETLINK_SOCKET;
609
610 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400611 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
612 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 rc = -EINVAL;
614 goto out;
615 }
616 tclass_datum = policydb.class_val_to_struct[tclass - 1];
617
618 ocontext = sidtab_search(&sidtab, oldsid);
619 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400620 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
621 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 rc = -EINVAL;
623 goto out;
624 }
625
626 ncontext = sidtab_search(&sidtab, newsid);
627 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400628 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
629 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 rc = -EINVAL;
631 goto out;
632 }
633
634 tcontext = sidtab_search(&sidtab, tasksid);
635 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400636 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
637 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 rc = -EINVAL;
639 goto out;
640 }
641
642 constraint = tclass_datum->validatetrans;
643 while (constraint) {
644 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400645 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400647 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 goto out;
649 }
650 constraint = constraint->next;
651 }
652
653out:
James Morris0804d112008-06-06 18:40:29 +1000654 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 return rc;
656}
657
KaiGai Koheid9250de2008-08-28 16:35:57 +0900658/*
659 * security_bounded_transition - check whether the given
660 * transition is directed to bounded, or not.
661 * It returns 0, if @newsid is bounded by @oldsid.
662 * Otherwise, it returns error code.
663 *
664 * @oldsid : current security identifier
665 * @newsid : destinated security identifier
666 */
667int security_bounded_transition(u32 old_sid, u32 new_sid)
668{
669 struct context *old_context, *new_context;
670 struct type_datum *type;
671 int index;
672 int rc = -EINVAL;
673
674 read_lock(&policy_rwlock);
675
676 old_context = sidtab_search(&sidtab, old_sid);
677 if (!old_context) {
678 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
679 __func__, old_sid);
680 goto out;
681 }
682
683 new_context = sidtab_search(&sidtab, new_sid);
684 if (!new_context) {
685 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
686 __func__, new_sid);
687 goto out;
688 }
689
690 /* type/domain unchaned */
691 if (old_context->type == new_context->type) {
692 rc = 0;
693 goto out;
694 }
695
696 index = new_context->type;
697 while (true) {
698 type = policydb.type_val_to_struct[index - 1];
699 BUG_ON(!type);
700
701 /* not bounded anymore */
702 if (!type->bounds) {
703 rc = -EPERM;
704 break;
705 }
706
707 /* @newsid is bounded by @oldsid */
708 if (type->bounds == old_context->type) {
709 rc = 0;
710 break;
711 }
712 index = type->bounds;
713 }
714out:
715 read_unlock(&policy_rwlock);
716
717 return rc;
718}
719
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/**
722 * security_compute_av - Compute access vector decisions.
723 * @ssid: source security identifier
724 * @tsid: target security identifier
725 * @tclass: target security class
726 * @requested: requested permissions
727 * @avd: access vector decisions
728 *
729 * Compute a set of access vector decisions based on the
730 * SID pair (@ssid, @tsid) for the permissions in @tclass.
731 * Return -%EINVAL if any of the parameters are invalid or %0
732 * if the access vector decisions were computed successfully.
733 */
734int security_compute_av(u32 ssid,
735 u32 tsid,
736 u16 tclass,
737 u32 requested,
738 struct av_decision *avd)
739{
740 struct context *scontext = NULL, *tcontext = NULL;
741 int rc = 0;
742
743 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700744 avd->allowed = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 avd->auditallow = 0;
746 avd->auditdeny = 0xffffffff;
747 avd->seqno = latest_granting;
748 return 0;
749 }
750
James Morris0804d112008-06-06 18:40:29 +1000751 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 scontext = sidtab_search(&sidtab, ssid);
754 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400755 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
756 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 rc = -EINVAL;
758 goto out;
759 }
760 tcontext = sidtab_search(&sidtab, tsid);
761 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400762 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
763 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 rc = -EINVAL;
765 goto out;
766 }
767
768 rc = context_struct_compute_av(scontext, tcontext, tclass,
769 requested, avd);
770out:
James Morris0804d112008-06-06 18:40:29 +1000771 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return rc;
773}
774
775/*
776 * Write the security context string representation of
777 * the context structure `context' into a dynamically
778 * allocated string of the correct size. Set `*scontext'
779 * to point to this string and set `*scontext_len' to
780 * the length of the string.
781 */
782static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
783{
784 char *scontextp;
785
786 *scontext = NULL;
787 *scontext_len = 0;
788
Stephen Smalley12b29f32008-05-07 13:03:20 -0400789 if (context->len) {
790 *scontext_len = context->len;
791 *scontext = kstrdup(context->str, GFP_ATOMIC);
792 if (!(*scontext))
793 return -ENOMEM;
794 return 0;
795 }
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* Compute the size of the context. */
798 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
799 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
800 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
801 *scontext_len += mls_compute_context_len(context);
802
803 /* Allocate space for the context; caller must free this space. */
804 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400805 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *scontext = scontextp;
808
809 /*
810 * Copy the user name, role name and type name into the context.
811 */
812 sprintf(scontextp, "%s:%s:%s",
813 policydb.p_user_val_to_name[context->user - 1],
814 policydb.p_role_val_to_name[context->role - 1],
815 policydb.p_type_val_to_name[context->type - 1]);
816 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400817 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
818 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
820 mls_sid_to_context(context, &scontextp);
821
822 *scontextp = 0;
823
824 return 0;
825}
826
827#include "initial_sid_to_string.h"
828
James Carterf0ee2e42007-04-04 10:11:29 -0400829const char *security_get_initial_sid_context(u32 sid)
830{
831 if (unlikely(sid > SECINITSID_NUM))
832 return NULL;
833 return initial_sid_to_string[sid];
834}
835
Stephen Smalley12b29f32008-05-07 13:03:20 -0400836static int security_sid_to_context_core(u32 sid, char **scontext,
837 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct context *context;
840 int rc = 0;
841
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500842 *scontext = NULL;
843 *scontext_len = 0;
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (!ss_initialized) {
846 if (sid <= SECINITSID_NUM) {
847 char *scontextp;
848
849 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -0400850 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -0700851 if (!scontextp) {
852 rc = -ENOMEM;
853 goto out;
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 strcpy(scontextp, initial_sid_to_string[sid]);
856 *scontext = scontextp;
857 goto out;
858 }
Eric Paris744ba352008-04-17 11:52:44 -0400859 printk(KERN_ERR "SELinux: %s: called before initial "
860 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 rc = -EINVAL;
862 goto out;
863 }
James Morris0804d112008-06-06 18:40:29 +1000864 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400865 if (force)
866 context = sidtab_search_force(&sidtab, sid);
867 else
868 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -0400870 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
871 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 rc = -EINVAL;
873 goto out_unlock;
874 }
875 rc = context_struct_to_string(context, scontext, scontext_len);
876out_unlock:
James Morris0804d112008-06-06 18:40:29 +1000877 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878out:
879 return rc;
880
881}
882
Stephen Smalley12b29f32008-05-07 13:03:20 -0400883/**
884 * security_sid_to_context - Obtain a context for a given SID.
885 * @sid: security identifier, SID
886 * @scontext: security context
887 * @scontext_len: length in bytes
888 *
889 * Write the string representation of the context associated with @sid
890 * into a dynamically allocated string of the correct size. Set @scontext
891 * to point to this string and set @scontext_len to the length of the string.
892 */
893int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Stephen Smalley12b29f32008-05-07 13:03:20 -0400895 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
896}
897
898int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
899{
900 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
901}
902
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400903/*
904 * Caveat: Mutates scontext.
905 */
Stephen Smalley12b29f32008-05-07 13:03:20 -0400906static int string_to_context_struct(struct policydb *pol,
907 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400908 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400909 u32 scontext_len,
910 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400911 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -0400912{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 struct role_datum *role;
914 struct type_datum *typdatum;
915 struct user_datum *usrdatum;
916 char *scontextp, *p, oldc;
917 int rc = 0;
918
Stephen Smalley12b29f32008-05-07 13:03:20 -0400919 context_init(ctx);
920
Stephen Smalley12b29f32008-05-07 13:03:20 -0400921 /* Parse the security context. */
922
923 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400924 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400925
926 /* Extract the user. */
927 p = scontextp;
928 while (*p && *p != ':')
929 p++;
930
931 if (*p == 0)
932 goto out;
933
934 *p++ = 0;
935
936 usrdatum = hashtab_search(pol->p_users.table, scontextp);
937 if (!usrdatum)
938 goto out;
939
940 ctx->user = usrdatum->value;
941
942 /* Extract role. */
943 scontextp = p;
944 while (*p && *p != ':')
945 p++;
946
947 if (*p == 0)
948 goto out;
949
950 *p++ = 0;
951
952 role = hashtab_search(pol->p_roles.table, scontextp);
953 if (!role)
954 goto out;
955 ctx->role = role->value;
956
957 /* Extract type. */
958 scontextp = p;
959 while (*p && *p != ':')
960 p++;
961 oldc = *p;
962 *p++ = 0;
963
964 typdatum = hashtab_search(pol->p_types.table, scontextp);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900965 if (!typdatum || typdatum->attribute)
Stephen Smalley12b29f32008-05-07 13:03:20 -0400966 goto out;
967
968 ctx->type = typdatum->value;
969
970 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
971 if (rc)
972 goto out;
973
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400974 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -0400975 rc = -EINVAL;
976 goto out;
977 }
978
979 /* Check the validity of the new context. */
980 if (!policydb_context_isvalid(pol, ctx)) {
981 rc = -EINVAL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400982 goto out;
983 }
984 rc = 0;
985out:
Eric Paris8e531af2008-09-03 11:49:47 -0400986 if (rc)
987 context_destroy(ctx);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400988 return rc;
989}
990
991static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
992 u32 *sid, u32 def_sid, gfp_t gfp_flags,
993 int force)
994{
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400995 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400996 struct context context;
997 int rc = 0;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (!ss_initialized) {
1000 int i;
1001
1002 for (i = 1; i < SECINITSID_NUM; i++) {
1003 if (!strcmp(initial_sid_to_string[i], scontext)) {
1004 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007 }
1008 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001009 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 }
1011 *sid = SECSID_NULL;
1012
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001013 /* Copy the string so that we can modify the copy as we parse it. */
1014 scontext2 = kmalloc(scontext_len+1, gfp_flags);
1015 if (!scontext2)
1016 return -ENOMEM;
1017 memcpy(scontext2, scontext, scontext_len);
1018 scontext2[scontext_len] = 0;
1019
1020 if (force) {
1021 /* Save another copy for storing in uninterpreted form */
1022 str = kstrdup(scontext2, gfp_flags);
1023 if (!str) {
1024 kfree(scontext2);
1025 return -ENOMEM;
1026 }
1027 }
1028
James Morris0804d112008-06-06 18:40:29 +10001029 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001030 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001031 scontext2, scontext_len,
1032 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001033 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001034 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001035 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001036 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001037 } else if (rc)
1038 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Eric Paris8e531af2008-09-03 11:49:47 -04001040 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041out:
James Morris0804d112008-06-06 18:40:29 +10001042 read_unlock(&policy_rwlock);
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001043 kfree(scontext2);
1044 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return rc;
1046}
1047
James Morrisf5c1d5b2005-07-28 01:07:37 -07001048/**
1049 * security_context_to_sid - Obtain a SID for a given security context.
1050 * @scontext: security context
1051 * @scontext_len: length in bytes
1052 * @sid: security identifier, SID
1053 *
1054 * Obtains a SID associated with the security context that
1055 * has the string representation specified by @scontext.
1056 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1057 * memory is available, or 0 on success.
1058 */
David Howells8f0cfa52008-04-29 00:59:41 -07001059int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001060{
1061 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001062 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001063}
1064
1065/**
1066 * security_context_to_sid_default - Obtain a SID for a given security context,
1067 * falling back to specified default if needed.
1068 *
1069 * @scontext: security context
1070 * @scontext_len: length in bytes
1071 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -07001072 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -07001073 *
1074 * Obtains a SID associated with the security context that
1075 * has the string representation specified by @scontext.
1076 * The default SID is passed to the MLS layer to be used to allow
1077 * kernel labeling of the MLS field if the MLS field is not present
1078 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -04001079 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -07001080 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1081 * memory is available, or 0 on success.
1082 */
David Howells7bf570d2008-04-29 20:52:51 +01001083int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1084 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001085{
1086 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001087 sid, def_sid, gfp_flags, 1);
1088}
1089
1090int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1091 u32 *sid)
1092{
1093 return security_context_to_sid_core(scontext, scontext_len,
1094 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001095}
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097static int compute_sid_handle_invalid_context(
1098 struct context *scontext,
1099 struct context *tcontext,
1100 u16 tclass,
1101 struct context *newcontext)
1102{
1103 char *s = NULL, *t = NULL, *n = NULL;
1104 u32 slen, tlen, nlen;
1105
1106 if (context_struct_to_string(scontext, &s, &slen) < 0)
1107 goto out;
1108 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
1109 goto out;
1110 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
1111 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001112 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 "security_compute_sid: invalid context %s"
1114 " for scontext=%s"
1115 " tcontext=%s"
1116 " tclass=%s",
1117 n, s, t, policydb.p_class_val_to_name[tclass-1]);
1118out:
1119 kfree(s);
1120 kfree(t);
1121 kfree(n);
1122 if (!selinux_enforcing)
1123 return 0;
1124 return -EACCES;
1125}
1126
1127static int security_compute_sid(u32 ssid,
1128 u32 tsid,
1129 u16 tclass,
1130 u32 specified,
1131 u32 *out_sid)
1132{
1133 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1134 struct role_trans *roletr = NULL;
1135 struct avtab_key avkey;
1136 struct avtab_datum *avdatum;
1137 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 int rc = 0;
1139
1140 if (!ss_initialized) {
1141 switch (tclass) {
1142 case SECCLASS_PROCESS:
1143 *out_sid = ssid;
1144 break;
1145 default:
1146 *out_sid = tsid;
1147 break;
1148 }
1149 goto out;
1150 }
1151
Venkat Yekkirala851f8a62006-07-30 03:03:18 -07001152 context_init(&newcontext);
1153
James Morris0804d112008-06-06 18:40:29 +10001154 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 scontext = sidtab_search(&sidtab, ssid);
1157 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001158 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1159 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 rc = -EINVAL;
1161 goto out_unlock;
1162 }
1163 tcontext = sidtab_search(&sidtab, tsid);
1164 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001165 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1166 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 rc = -EINVAL;
1168 goto out_unlock;
1169 }
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /* Set the user identity. */
1172 switch (specified) {
1173 case AVTAB_TRANSITION:
1174 case AVTAB_CHANGE:
1175 /* Use the process user identity. */
1176 newcontext.user = scontext->user;
1177 break;
1178 case AVTAB_MEMBER:
1179 /* Use the related object owner. */
1180 newcontext.user = tcontext->user;
1181 break;
1182 }
1183
1184 /* Set the role and type to default values. */
1185 switch (tclass) {
1186 case SECCLASS_PROCESS:
1187 /* Use the current role and type of process. */
1188 newcontext.role = scontext->role;
1189 newcontext.type = scontext->type;
1190 break;
1191 default:
1192 /* Use the well-defined object role. */
1193 newcontext.role = OBJECT_R_VAL;
1194 /* Use the type of the related object. */
1195 newcontext.type = tcontext->type;
1196 }
1197
1198 /* Look for a type transition/member/change rule. */
1199 avkey.source_type = scontext->type;
1200 avkey.target_type = tcontext->type;
1201 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001202 avkey.specified = specified;
1203 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001206 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001207 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03001208 for (; node; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001209 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 avdatum = &node->datum;
1211 break;
1212 }
1213 }
1214 }
1215
Stephen Smalley782ebb92005-09-03 15:55:16 -07001216 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001218 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
1221 /* Check for class-specific changes. */
1222 switch (tclass) {
1223 case SECCLASS_PROCESS:
1224 if (specified & AVTAB_TRANSITION) {
1225 /* Look for a role transition rule. */
1226 for (roletr = policydb.role_tr; roletr;
1227 roletr = roletr->next) {
1228 if (roletr->role == scontext->role &&
1229 roletr->type == tcontext->type) {
1230 /* Use the role transition rule. */
1231 newcontext.role = roletr->new_role;
1232 break;
1233 }
1234 }
1235 }
1236 break;
1237 default:
1238 break;
1239 }
1240
1241 /* Set the MLS attributes.
1242 This is done last because it may allocate memory. */
1243 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1244 if (rc)
1245 goto out_unlock;
1246
1247 /* Check the validity of the context. */
1248 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1249 rc = compute_sid_handle_invalid_context(scontext,
1250 tcontext,
1251 tclass,
1252 &newcontext);
1253 if (rc)
1254 goto out_unlock;
1255 }
1256 /* Obtain the sid for the context. */
1257 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1258out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001259 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 context_destroy(&newcontext);
1261out:
1262 return rc;
1263}
1264
1265/**
1266 * security_transition_sid - Compute the SID for a new subject/object.
1267 * @ssid: source security identifier
1268 * @tsid: target security identifier
1269 * @tclass: target security class
1270 * @out_sid: security identifier for new subject/object
1271 *
1272 * Compute a SID to use for labeling a new subject or object in the
1273 * class @tclass based on a SID pair (@ssid, @tsid).
1274 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1275 * if insufficient memory is available, or %0 if the new SID was
1276 * computed successfully.
1277 */
1278int security_transition_sid(u32 ssid,
1279 u32 tsid,
1280 u16 tclass,
1281 u32 *out_sid)
1282{
1283 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1284}
1285
1286/**
1287 * security_member_sid - Compute the SID for member selection.
1288 * @ssid: source security identifier
1289 * @tsid: target security identifier
1290 * @tclass: target security class
1291 * @out_sid: security identifier for selected member
1292 *
1293 * Compute a SID to use when selecting a member of a polyinstantiated
1294 * object of class @tclass based on a SID pair (@ssid, @tsid).
1295 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1296 * if insufficient memory is available, or %0 if the SID was
1297 * computed successfully.
1298 */
1299int security_member_sid(u32 ssid,
1300 u32 tsid,
1301 u16 tclass,
1302 u32 *out_sid)
1303{
1304 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1305}
1306
1307/**
1308 * security_change_sid - Compute the SID for object relabeling.
1309 * @ssid: source security identifier
1310 * @tsid: target security identifier
1311 * @tclass: target security class
1312 * @out_sid: security identifier for selected member
1313 *
1314 * Compute a SID to use for relabeling an object of class @tclass
1315 * based on a SID pair (@ssid, @tsid).
1316 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1317 * if insufficient memory is available, or %0 if the SID was
1318 * computed successfully.
1319 */
1320int security_change_sid(u32 ssid,
1321 u32 tsid,
1322 u16 tclass,
1323 u32 *out_sid)
1324{
1325 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1326}
1327
Chad Sellersb94c7e62006-11-06 12:38:18 -05001328/*
1329 * Verify that each kernel class that is defined in the
1330 * policy is correct
1331 */
1332static int validate_classes(struct policydb *p)
1333{
1334 int i, j;
1335 struct class_datum *cladatum;
1336 struct perm_datum *perdatum;
1337 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1338 u16 class_val;
1339 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1340 const char *def_class, *def_perm, *pol_class;
1341 struct symtab *perms;
Eric Paris6cbe2702008-06-09 16:51:37 -04001342 bool print_unknown_handle = 0;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001343
Eric Paris3f120702007-09-21 14:37:10 -04001344 if (p->allow_unknown) {
1345 u32 num_classes = kdefs->cts_len;
1346 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1347 if (!p->undefined_perms)
1348 return -ENOMEM;
1349 }
1350
Chad Sellersb94c7e62006-11-06 12:38:18 -05001351 for (i = 1; i < kdefs->cts_len; i++) {
1352 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001353 if (!def_class)
1354 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001355 if (i > p->p_classes.nprim) {
1356 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001357 "SELinux: class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001358 def_class);
Eric Paris3f120702007-09-21 14:37:10 -04001359 if (p->reject_unknown)
1360 return -EINVAL;
1361 if (p->allow_unknown)
1362 p->undefined_perms[i-1] = ~0U;
Eric Paris6cbe2702008-06-09 16:51:37 -04001363 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001364 continue;
1365 }
1366 pol_class = p->p_class_val_to_name[i-1];
1367 if (strcmp(pol_class, def_class)) {
1368 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001369 "SELinux: class %d is incorrect, found %s but should be %s\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001370 i, pol_class, def_class);
1371 return -EINVAL;
1372 }
1373 }
1374 for (i = 0; i < kdefs->av_pts_len; i++) {
1375 class_val = kdefs->av_perm_to_string[i].tclass;
1376 perm_val = kdefs->av_perm_to_string[i].value;
1377 def_perm = kdefs->av_perm_to_string[i].name;
1378 if (class_val > p->p_classes.nprim)
1379 continue;
1380 pol_class = p->p_class_val_to_name[class_val-1];
1381 cladatum = hashtab_search(p->p_classes.table, pol_class);
1382 BUG_ON(!cladatum);
1383 perms = &cladatum->permissions;
1384 nprim = 1 << (perms->nprim - 1);
1385 if (perm_val > nprim) {
1386 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001387 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001388 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001389 if (p->reject_unknown)
1390 return -EINVAL;
1391 if (p->allow_unknown)
1392 p->undefined_perms[class_val-1] |= perm_val;
Eric Paris6cbe2702008-06-09 16:51:37 -04001393 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001394 continue;
1395 }
1396 perdatum = hashtab_search(perms->table, def_perm);
1397 if (perdatum == NULL) {
1398 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001399 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001400 def_perm, pol_class);
1401 return -EINVAL;
1402 }
1403 pol_val = 1 << (perdatum->value - 1);
1404 if (pol_val != perm_val) {
1405 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001406 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001407 def_perm, pol_class);
1408 return -EINVAL;
1409 }
1410 }
1411 for (i = 0; i < kdefs->av_inherit_len; i++) {
1412 class_val = kdefs->av_inherit[i].tclass;
1413 if (class_val > p->p_classes.nprim)
1414 continue;
1415 pol_class = p->p_class_val_to_name[class_val-1];
1416 cladatum = hashtab_search(p->p_classes.table, pol_class);
1417 BUG_ON(!cladatum);
1418 if (!cladatum->comdatum) {
1419 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001420 "SELinux: class %s should have an inherits clause but does not\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001421 pol_class);
1422 return -EINVAL;
1423 }
1424 tmp = kdefs->av_inherit[i].common_base;
1425 common_pts_len = 0;
1426 while (!(tmp & 0x01)) {
1427 common_pts_len++;
1428 tmp >>= 1;
1429 }
1430 perms = &cladatum->comdatum->permissions;
1431 for (j = 0; j < common_pts_len; j++) {
1432 def_perm = kdefs->av_inherit[i].common_pts[j];
1433 if (j >= perms->nprim) {
1434 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001435 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001436 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001437 if (p->reject_unknown)
1438 return -EINVAL;
1439 if (p->allow_unknown)
1440 p->undefined_perms[class_val-1] |= (1 << j);
Eric Paris6cbe2702008-06-09 16:51:37 -04001441 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001442 continue;
1443 }
1444 perdatum = hashtab_search(perms->table, def_perm);
1445 if (perdatum == NULL) {
1446 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001447 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001448 def_perm, pol_class);
1449 return -EINVAL;
1450 }
1451 if (perdatum->value != j + 1) {
1452 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001453 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001454 def_perm, pol_class);
1455 return -EINVAL;
1456 }
1457 }
1458 }
Eric Paris6cbe2702008-06-09 16:51:37 -04001459 if (print_unknown_handle)
1460 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
1461 (security_get_allow_unknown() ? "allowed" : "denied"));
Chad Sellersb94c7e62006-11-06 12:38:18 -05001462 return 0;
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/* Clone the SID into the new SID table. */
1466static int clone_sid(u32 sid,
1467 struct context *context,
1468 void *arg)
1469{
1470 struct sidtab *s = arg;
1471
1472 return sidtab_insert(s, sid, context);
1473}
1474
1475static inline int convert_context_handle_invalid_context(struct context *context)
1476{
1477 int rc = 0;
1478
1479 if (selinux_enforcing) {
1480 rc = -EINVAL;
1481 } else {
1482 char *s;
1483 u32 len;
1484
Stephen Smalley12b29f32008-05-07 13:03:20 -04001485 if (!context_struct_to_string(context, &s, &len)) {
1486 printk(KERN_WARNING
1487 "SELinux: Context %s would be invalid if enforcing\n",
1488 s);
1489 kfree(s);
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 }
1492 return rc;
1493}
1494
1495struct convert_context_args {
1496 struct policydb *oldp;
1497 struct policydb *newp;
1498};
1499
1500/*
1501 * Convert the values in the security context
1502 * structure `c' from the values specified
1503 * in the policy `p->oldp' to the values specified
1504 * in the policy `p->newp'. Verify that the
1505 * context is valid under the new policy.
1506 */
1507static int convert_context(u32 key,
1508 struct context *c,
1509 void *p)
1510{
1511 struct convert_context_args *args;
1512 struct context oldc;
1513 struct role_datum *role;
1514 struct type_datum *typdatum;
1515 struct user_datum *usrdatum;
1516 char *s;
1517 u32 len;
1518 int rc;
1519
1520 args = p;
1521
Stephen Smalley12b29f32008-05-07 13:03:20 -04001522 if (c->str) {
1523 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001524 s = kstrdup(c->str, GFP_KERNEL);
1525 if (!s) {
1526 rc = -ENOMEM;
1527 goto out;
1528 }
1529 rc = string_to_context_struct(args->newp, NULL, s,
1530 c->len, &ctx, SECSID_NULL);
1531 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001532 if (!rc) {
1533 printk(KERN_INFO
1534 "SELinux: Context %s became valid (mapped).\n",
1535 c->str);
1536 /* Replace string with mapped representation. */
1537 kfree(c->str);
1538 memcpy(c, &ctx, sizeof(*c));
1539 goto out;
1540 } else if (rc == -EINVAL) {
1541 /* Retain string representation for later mapping. */
1542 rc = 0;
1543 goto out;
1544 } else {
1545 /* Other error condition, e.g. ENOMEM. */
1546 printk(KERN_ERR
1547 "SELinux: Unable to map context %s, rc = %d.\n",
1548 c->str, -rc);
1549 goto out;
1550 }
1551 }
1552
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 rc = context_cpy(&oldc, c);
1554 if (rc)
1555 goto out;
1556
1557 rc = -EINVAL;
1558
1559 /* Convert the user. */
1560 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001561 args->oldp->p_user_val_to_name[c->user - 1]);
1562 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 c->user = usrdatum->value;
1565
1566 /* Convert the role. */
1567 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001568 args->oldp->p_role_val_to_name[c->role - 1]);
1569 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 c->role = role->value;
1572
1573 /* Convert the type. */
1574 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001575 args->oldp->p_type_val_to_name[c->type - 1]);
1576 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 c->type = typdatum->value;
1579
1580 rc = mls_convert_context(args->oldp, args->newp, c);
1581 if (rc)
1582 goto bad;
1583
1584 /* Check the validity of the new context. */
1585 if (!policydb_context_isvalid(args->newp, c)) {
1586 rc = convert_context_handle_invalid_context(&oldc);
1587 if (rc)
1588 goto bad;
1589 }
1590
1591 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001592 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593out:
1594 return rc;
1595bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001596 /* Map old representation to string and save it. */
1597 if (context_struct_to_string(&oldc, &s, &len))
1598 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001600 context_destroy(c);
1601 c->str = s;
1602 c->len = len;
1603 printk(KERN_INFO
1604 "SELinux: Context %s became invalid (unmapped).\n",
1605 c->str);
1606 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 goto out;
1608}
1609
Paul Moore3bb56b22008-01-29 08:38:19 -05001610static void security_load_policycaps(void)
1611{
1612 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1613 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001614 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1615 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001616}
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001619static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621/**
1622 * security_load_policy - Load a security policy configuration.
1623 * @data: binary policy data
1624 * @len: length of data in bytes
1625 *
1626 * Load a new set of security policy configuration data,
1627 * validate it and convert the SID table as necessary.
1628 * This function will flush the access vector cache after
1629 * loading the new policy.
1630 */
1631int security_load_policy(void *data, size_t len)
1632{
1633 struct policydb oldpolicydb, newpolicydb;
1634 struct sidtab oldsidtab, newsidtab;
1635 struct convert_context_args args;
1636 u32 seqno;
1637 int rc = 0;
1638 struct policy_file file = { data, len }, *fp = &file;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (!ss_initialized) {
1641 avtab_cache_init();
1642 if (policydb_read(&policydb, fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 avtab_cache_destroy();
1644 return -EINVAL;
1645 }
1646 if (policydb_load_isids(&policydb, &sidtab)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 policydb_destroy(&policydb);
1648 avtab_cache_destroy();
1649 return -EINVAL;
1650 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001651 /* Verify that the kernel defined classes are correct. */
1652 if (validate_classes(&policydb)) {
1653 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001654 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001655 sidtab_destroy(&sidtab);
1656 policydb_destroy(&policydb);
1657 avtab_cache_destroy();
1658 return -EINVAL;
1659 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001660 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 policydb_loaded_version = policydb.policyvers;
1662 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001663 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001665 avc_ss_reset(seqno);
1666 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001667 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001668 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return 0;
1670 }
1671
1672#if 0
1673 sidtab_hash_eval(&sidtab, "sids");
1674#endif
1675
Eric Paris89abd0a2008-06-09 15:58:04 -04001676 if (policydb_read(&newpolicydb, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Stephen Smalley12b29f32008-05-07 13:03:20 -04001679 if (sidtab_init(&newsidtab)) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001680 policydb_destroy(&newpolicydb);
1681 return -ENOMEM;
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Chad Sellersb94c7e62006-11-06 12:38:18 -05001684 /* Verify that the kernel defined classes are correct. */
1685 if (validate_classes(&newpolicydb)) {
1686 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001687 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001688 rc = -EINVAL;
1689 goto err;
1690 }
1691
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001692 rc = security_preserve_bools(&newpolicydb);
1693 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001694 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001695 goto err;
1696 }
1697
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 /* Clone the SID table. */
1699 sidtab_shutdown(&sidtab);
1700 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1701 rc = -ENOMEM;
1702 goto err;
1703 }
1704
Stephen Smalley12b29f32008-05-07 13:03:20 -04001705 /*
1706 * Convert the internal representations of contexts
1707 * in the new SID table.
1708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 args.oldp = &policydb;
1710 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001711 rc = sidtab_map(&newsidtab, convert_context, &args);
1712 if (rc)
1713 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 /* Save the old policydb and SID table to free later. */
1716 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1717 sidtab_set(&oldsidtab, &sidtab);
1718
1719 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10001720 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 memcpy(&policydb, &newpolicydb, sizeof policydb);
1722 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001723 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 seqno = ++latest_granting;
1725 policydb_loaded_version = policydb.policyvers;
James Morris0804d112008-06-06 18:40:29 +10001726 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 /* Free the old policydb and SID table. */
1729 policydb_destroy(&oldpolicydb);
1730 sidtab_destroy(&oldsidtab);
1731
1732 avc_ss_reset(seqno);
1733 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001734 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001735 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 return 0;
1738
1739err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 sidtab_destroy(&newsidtab);
1741 policydb_destroy(&newpolicydb);
1742 return rc;
1743
1744}
1745
1746/**
1747 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * @protocol: protocol number
1749 * @port: port number
1750 * @out_sid: security identifier
1751 */
Paul Moore3e112172008-04-10 10:48:14 -04001752int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753{
1754 struct ocontext *c;
1755 int rc = 0;
1756
James Morris0804d112008-06-06 18:40:29 +10001757 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 c = policydb.ocontexts[OCON_PORT];
1760 while (c) {
1761 if (c->u.port.protocol == protocol &&
1762 c->u.port.low_port <= port &&
1763 c->u.port.high_port >= port)
1764 break;
1765 c = c->next;
1766 }
1767
1768 if (c) {
1769 if (!c->sid[0]) {
1770 rc = sidtab_context_to_sid(&sidtab,
1771 &c->context[0],
1772 &c->sid[0]);
1773 if (rc)
1774 goto out;
1775 }
1776 *out_sid = c->sid[0];
1777 } else {
1778 *out_sid = SECINITSID_PORT;
1779 }
1780
1781out:
James Morris0804d112008-06-06 18:40:29 +10001782 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return rc;
1784}
1785
1786/**
1787 * security_netif_sid - Obtain the SID for a network interface.
1788 * @name: interface name
1789 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001791int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 int rc = 0;
1794 struct ocontext *c;
1795
James Morris0804d112008-06-06 18:40:29 +10001796 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 c = policydb.ocontexts[OCON_NETIF];
1799 while (c) {
1800 if (strcmp(name, c->u.name) == 0)
1801 break;
1802 c = c->next;
1803 }
1804
1805 if (c) {
1806 if (!c->sid[0] || !c->sid[1]) {
1807 rc = sidtab_context_to_sid(&sidtab,
1808 &c->context[0],
1809 &c->sid[0]);
1810 if (rc)
1811 goto out;
1812 rc = sidtab_context_to_sid(&sidtab,
1813 &c->context[1],
1814 &c->sid[1]);
1815 if (rc)
1816 goto out;
1817 }
1818 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001819 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
1822out:
James Morris0804d112008-06-06 18:40:29 +10001823 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 return rc;
1825}
1826
1827static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1828{
1829 int i, fail = 0;
1830
Eric Paris5d55a342008-04-18 17:38:33 -04001831 for (i = 0; i < 4; i++)
1832 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 fail = 1;
1834 break;
1835 }
1836
1837 return !fail;
1838}
1839
1840/**
1841 * security_node_sid - Obtain the SID for a node (host).
1842 * @domain: communication domain aka address family
1843 * @addrp: address
1844 * @addrlen: address length in bytes
1845 * @out_sid: security identifier
1846 */
1847int security_node_sid(u16 domain,
1848 void *addrp,
1849 u32 addrlen,
1850 u32 *out_sid)
1851{
1852 int rc = 0;
1853 struct ocontext *c;
1854
James Morris0804d112008-06-06 18:40:29 +10001855 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 switch (domain) {
1858 case AF_INET: {
1859 u32 addr;
1860
1861 if (addrlen != sizeof(u32)) {
1862 rc = -EINVAL;
1863 goto out;
1864 }
1865
1866 addr = *((u32 *)addrp);
1867
1868 c = policydb.ocontexts[OCON_NODE];
1869 while (c) {
1870 if (c->u.node.addr == (addr & c->u.node.mask))
1871 break;
1872 c = c->next;
1873 }
1874 break;
1875 }
1876
1877 case AF_INET6:
1878 if (addrlen != sizeof(u64) * 2) {
1879 rc = -EINVAL;
1880 goto out;
1881 }
1882 c = policydb.ocontexts[OCON_NODE6];
1883 while (c) {
1884 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1885 c->u.node6.mask))
1886 break;
1887 c = c->next;
1888 }
1889 break;
1890
1891 default:
1892 *out_sid = SECINITSID_NODE;
1893 goto out;
1894 }
1895
1896 if (c) {
1897 if (!c->sid[0]) {
1898 rc = sidtab_context_to_sid(&sidtab,
1899 &c->context[0],
1900 &c->sid[0]);
1901 if (rc)
1902 goto out;
1903 }
1904 *out_sid = c->sid[0];
1905 } else {
1906 *out_sid = SECINITSID_NODE;
1907 }
1908
1909out:
James Morris0804d112008-06-06 18:40:29 +10001910 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 return rc;
1912}
1913
1914#define SIDS_NEL 25
1915
1916/**
1917 * security_get_user_sids - Obtain reachable SIDs for a user.
1918 * @fromsid: starting SID
1919 * @username: username
1920 * @sids: array of reachable SIDs for user
1921 * @nel: number of elements in @sids
1922 *
1923 * Generate the set of SIDs for legal security contexts
1924 * for a given user that can be reached by @fromsid.
1925 * Set *@sids to point to a dynamically allocated
1926 * array containing the set of SIDs. Set *@nel to the
1927 * number of elements in the array.
1928 */
1929
1930int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04001931 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 u32 **sids,
1933 u32 *nel)
1934{
1935 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001936 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 u32 mynel = 0, maxnel = SIDS_NEL;
1938 struct user_datum *user;
1939 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001940 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 int rc = 0, i, j;
1942
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001943 *sids = NULL;
1944 *nel = 0;
1945
1946 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
James Morris0804d112008-06-06 18:40:29 +10001949 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Stephen Smalley12b29f32008-05-07 13:03:20 -04001951 context_init(&usercon);
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 fromcon = sidtab_search(&sidtab, fromsid);
1954 if (!fromcon) {
1955 rc = -EINVAL;
1956 goto out_unlock;
1957 }
1958
1959 user = hashtab_search(policydb.p_users.table, username);
1960 if (!user) {
1961 rc = -EINVAL;
1962 goto out_unlock;
1963 }
1964 usercon.user = user->value;
1965
James Morris89d155e2005-10-30 14:59:21 -08001966 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!mysids) {
1968 rc = -ENOMEM;
1969 goto out_unlock;
1970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001972 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 role = policydb.role_val_to_struct[i];
1974 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001975 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 usercon.type = j+1;
1977
1978 if (mls_setup_user_range(fromcon, user, &usercon))
1979 continue;
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001982 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (mynel < maxnel) {
1985 mysids[mynel++] = sid;
1986 } else {
1987 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001988 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (!mysids2) {
1990 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 goto out_unlock;
1992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1994 kfree(mysids);
1995 mysids = mysids2;
1996 mysids[mynel++] = sid;
1997 }
1998 }
1999 }
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002002 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002003 if (rc || !mynel) {
2004 kfree(mysids);
2005 goto out;
2006 }
2007
2008 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2009 if (!mysids2) {
2010 rc = -ENOMEM;
2011 kfree(mysids);
2012 goto out;
2013 }
2014 for (i = 0, j = 0; i < mynel; i++) {
2015 rc = avc_has_perm_noaudit(fromsid, mysids[i],
2016 SECCLASS_PROCESS,
2017 PROCESS__TRANSITION, AVC_STRICT,
2018 NULL);
2019 if (!rc)
2020 mysids2[j++] = mysids[i];
2021 cond_resched();
2022 }
2023 rc = 0;
2024 kfree(mysids);
2025 *sids = mysids2;
2026 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027out:
2028 return rc;
2029}
2030
2031/**
2032 * security_genfs_sid - Obtain a SID for a file in a filesystem
2033 * @fstype: filesystem type
2034 * @path: path from root of mount
2035 * @sclass: file security class
2036 * @sid: SID for path
2037 *
2038 * Obtain a SID to use for a file in a filesystem that
2039 * cannot support xattr or use a fixed labeling behavior like
2040 * transition SIDs or task SIDs.
2041 */
2042int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04002043 char *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 u16 sclass,
2045 u32 *sid)
2046{
2047 int len;
2048 struct genfs *genfs;
2049 struct ocontext *c;
2050 int rc = 0, cmp = 0;
2051
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05002052 while (path[0] == '/' && path[1] == '/')
2053 path++;
2054
James Morris0804d112008-06-06 18:40:29 +10002055 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
2057 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2058 cmp = strcmp(fstype, genfs->fstype);
2059 if (cmp <= 0)
2060 break;
2061 }
2062
2063 if (!genfs || cmp) {
2064 *sid = SECINITSID_UNLABELED;
2065 rc = -ENOENT;
2066 goto out;
2067 }
2068
2069 for (c = genfs->head; c; c = c->next) {
2070 len = strlen(c->u.name);
2071 if ((!c->v.sclass || sclass == c->v.sclass) &&
2072 (strncmp(c->u.name, path, len) == 0))
2073 break;
2074 }
2075
2076 if (!c) {
2077 *sid = SECINITSID_UNLABELED;
2078 rc = -ENOENT;
2079 goto out;
2080 }
2081
2082 if (!c->sid[0]) {
2083 rc = sidtab_context_to_sid(&sidtab,
2084 &c->context[0],
2085 &c->sid[0]);
2086 if (rc)
2087 goto out;
2088 }
2089
2090 *sid = c->sid[0];
2091out:
James Morris0804d112008-06-06 18:40:29 +10002092 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return rc;
2094}
2095
2096/**
2097 * security_fs_use - Determine how to handle labeling for a filesystem.
2098 * @fstype: filesystem type
2099 * @behavior: labeling behavior
2100 * @sid: SID for filesystem (superblock)
2101 */
2102int security_fs_use(
2103 const char *fstype,
2104 unsigned int *behavior,
James Morris089be432008-07-15 18:32:49 +10002105 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106{
2107 int rc = 0;
2108 struct ocontext *c;
2109
James Morris0804d112008-06-06 18:40:29 +10002110 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 c = policydb.ocontexts[OCON_FSUSE];
2113 while (c) {
2114 if (strcmp(fstype, c->u.name) == 0)
2115 break;
2116 c = c->next;
2117 }
2118
2119 if (c) {
2120 *behavior = c->v.behavior;
2121 if (!c->sid[0]) {
2122 rc = sidtab_context_to_sid(&sidtab,
2123 &c->context[0],
2124 &c->sid[0]);
2125 if (rc)
2126 goto out;
2127 }
2128 *sid = c->sid[0];
2129 } else {
James Morris089be432008-07-15 18:32:49 +10002130 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2131 if (rc) {
2132 *behavior = SECURITY_FS_USE_NONE;
2133 rc = 0;
2134 } else {
2135 *behavior = SECURITY_FS_USE_GENFS;
2136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138
2139out:
James Morris0804d112008-06-06 18:40:29 +10002140 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return rc;
2142}
2143
2144int security_get_bools(int *len, char ***names, int **values)
2145{
2146 int i, rc = -ENOMEM;
2147
James Morris0804d112008-06-06 18:40:29 +10002148 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 *names = NULL;
2150 *values = NULL;
2151
2152 *len = policydb.p_bools.nprim;
2153 if (!*len) {
2154 rc = 0;
2155 goto out;
2156 }
2157
Eric Paris5d55a342008-04-18 17:38:33 -04002158 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (!*names)
2160 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Jesper Juhle0795cf2006-01-09 20:54:46 -08002162 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (!*values)
2164 goto err;
2165
2166 for (i = 0; i < *len; i++) {
2167 size_t name_len;
2168 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2169 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002170 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (!(*names)[i])
2172 goto err;
2173 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2174 (*names)[i][name_len - 1] = 0;
2175 }
2176 rc = 0;
2177out:
James Morris0804d112008-06-06 18:40:29 +10002178 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return rc;
2180err:
2181 if (*names) {
2182 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002183 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002185 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 goto out;
2187}
2188
2189
2190int security_set_bools(int len, int *values)
2191{
2192 int i, rc = 0;
2193 int lenp, seqno = 0;
2194 struct cond_node *cur;
2195
James Morris0804d112008-06-06 18:40:29 +10002196 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 lenp = policydb.p_bools.nprim;
2199 if (len != lenp) {
2200 rc = -EFAULT;
2201 goto out;
2202 }
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002205 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2206 audit_log(current->audit_context, GFP_ATOMIC,
2207 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002208 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002209 policydb.p_bool_val_to_name[i],
2210 !!values[i],
2211 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002212 audit_get_loginuid(current),
2213 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002214 }
Eric Paris5d55a342008-04-18 17:38:33 -04002215 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002217 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002221 for (cur = policydb.cond_list; cur; cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 rc = evaluate_cond_node(&policydb, cur);
2223 if (rc)
2224 goto out;
2225 }
2226
2227 seqno = ++latest_granting;
2228
2229out:
James Morris0804d112008-06-06 18:40:29 +10002230 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (!rc) {
2232 avc_ss_reset(seqno);
2233 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002234 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236 return rc;
2237}
2238
2239int security_get_bool_value(int bool)
2240{
2241 int rc = 0;
2242 int len;
2243
James Morris0804d112008-06-06 18:40:29 +10002244 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246 len = policydb.p_bools.nprim;
2247 if (bool >= len) {
2248 rc = -EFAULT;
2249 goto out;
2250 }
2251
2252 rc = policydb.bool_val_to_struct[bool]->state;
2253out:
James Morris0804d112008-06-06 18:40:29 +10002254 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return rc;
2256}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002257
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002258static int security_preserve_bools(struct policydb *p)
2259{
2260 int rc, nbools = 0, *bvalues = NULL, i;
2261 char **bnames = NULL;
2262 struct cond_bool_datum *booldatum;
2263 struct cond_node *cur;
2264
2265 rc = security_get_bools(&nbools, &bnames, &bvalues);
2266 if (rc)
2267 goto out;
2268 for (i = 0; i < nbools; i++) {
2269 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2270 if (booldatum)
2271 booldatum->state = bvalues[i];
2272 }
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002273 for (cur = p->cond_list; cur; cur = cur->next) {
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002274 rc = evaluate_cond_node(p, cur);
2275 if (rc)
2276 goto out;
2277 }
2278
2279out:
2280 if (bnames) {
2281 for (i = 0; i < nbools; i++)
2282 kfree(bnames[i]);
2283 }
2284 kfree(bnames);
2285 kfree(bvalues);
2286 return rc;
2287}
2288
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002289/*
2290 * security_sid_mls_copy() - computes a new sid based on the given
2291 * sid and the mls portion of mls_sid.
2292 */
2293int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2294{
2295 struct context *context1;
2296 struct context *context2;
2297 struct context newcon;
2298 char *s;
2299 u32 len;
2300 int rc = 0;
2301
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002302 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002303 *new_sid = sid;
2304 goto out;
2305 }
2306
2307 context_init(&newcon);
2308
James Morris0804d112008-06-06 18:40:29 +10002309 read_lock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002310 context1 = sidtab_search(&sidtab, sid);
2311 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002312 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2313 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002314 rc = -EINVAL;
2315 goto out_unlock;
2316 }
2317
2318 context2 = sidtab_search(&sidtab, mls_sid);
2319 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002320 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2321 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002322 rc = -EINVAL;
2323 goto out_unlock;
2324 }
2325
2326 newcon.user = context1->user;
2327 newcon.role = context1->role;
2328 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002329 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002330 if (rc)
2331 goto out_unlock;
2332
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002333 /* Check the validity of the new context. */
2334 if (!policydb_context_isvalid(&policydb, &newcon)) {
2335 rc = convert_context_handle_invalid_context(&newcon);
2336 if (rc)
2337 goto bad;
2338 }
2339
2340 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2341 goto out_unlock;
2342
2343bad:
2344 if (!context_struct_to_string(&newcon, &s, &len)) {
2345 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2346 "security_sid_mls_copy: invalid context %s", s);
2347 kfree(s);
2348 }
2349
2350out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002351 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002352 context_destroy(&newcon);
2353out:
2354 return rc;
2355}
2356
Paul Moore220deb92008-01-29 08:38:23 -05002357/**
2358 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2359 * @nlbl_sid: NetLabel SID
2360 * @nlbl_type: NetLabel labeling protocol type
2361 * @xfrm_sid: XFRM SID
2362 *
2363 * Description:
2364 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2365 * resolved into a single SID it is returned via @peer_sid and the function
2366 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2367 * returns a negative value. A table summarizing the behavior is below:
2368 *
2369 * | function return | @sid
2370 * ------------------------------+-----------------+-----------------
2371 * no peer labels | 0 | SECSID_NULL
2372 * single peer label | 0 | <peer_label>
2373 * multiple, consistent labels | 0 | <peer_label>
2374 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2375 *
2376 */
2377int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2378 u32 xfrm_sid,
2379 u32 *peer_sid)
2380{
2381 int rc;
2382 struct context *nlbl_ctx;
2383 struct context *xfrm_ctx;
2384
2385 /* handle the common (which also happens to be the set of easy) cases
2386 * right away, these two if statements catch everything involving a
2387 * single or absent peer SID/label */
2388 if (xfrm_sid == SECSID_NULL) {
2389 *peer_sid = nlbl_sid;
2390 return 0;
2391 }
2392 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2393 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2394 * is present */
2395 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2396 *peer_sid = xfrm_sid;
2397 return 0;
2398 }
2399
2400 /* we don't need to check ss_initialized here since the only way both
2401 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2402 * security server was initialized and ss_initialized was true */
2403 if (!selinux_mls_enabled) {
2404 *peer_sid = SECSID_NULL;
2405 return 0;
2406 }
2407
James Morris0804d112008-06-06 18:40:29 +10002408 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002409
2410 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2411 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002412 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2413 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002414 rc = -EINVAL;
2415 goto out_slowpath;
2416 }
2417 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2418 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002419 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2420 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002421 rc = -EINVAL;
2422 goto out_slowpath;
2423 }
2424 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2425
2426out_slowpath:
James Morris0804d112008-06-06 18:40:29 +10002427 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002428 if (rc == 0)
2429 /* at present NetLabel SIDs/labels really only carry MLS
2430 * information so if the MLS portion of the NetLabel SID
2431 * matches the MLS portion of the labeled XFRM SID/label
2432 * then pass along the XFRM SID as it is the most
2433 * expressive */
2434 *peer_sid = xfrm_sid;
2435 else
2436 *peer_sid = SECSID_NULL;
2437 return rc;
2438}
2439
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002440static int get_classes_callback(void *k, void *d, void *args)
2441{
2442 struct class_datum *datum = d;
2443 char *name = k, **classes = args;
2444 int value = datum->value - 1;
2445
2446 classes[value] = kstrdup(name, GFP_ATOMIC);
2447 if (!classes[value])
2448 return -ENOMEM;
2449
2450 return 0;
2451}
2452
2453int security_get_classes(char ***classes, int *nclasses)
2454{
2455 int rc = -ENOMEM;
2456
James Morris0804d112008-06-06 18:40:29 +10002457 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002458
2459 *nclasses = policydb.p_classes.nprim;
2460 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2461 if (!*classes)
2462 goto out;
2463
2464 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2465 *classes);
2466 if (rc < 0) {
2467 int i;
2468 for (i = 0; i < *nclasses; i++)
2469 kfree((*classes)[i]);
2470 kfree(*classes);
2471 }
2472
2473out:
James Morris0804d112008-06-06 18:40:29 +10002474 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002475 return rc;
2476}
2477
2478static int get_permissions_callback(void *k, void *d, void *args)
2479{
2480 struct perm_datum *datum = d;
2481 char *name = k, **perms = args;
2482 int value = datum->value - 1;
2483
2484 perms[value] = kstrdup(name, GFP_ATOMIC);
2485 if (!perms[value])
2486 return -ENOMEM;
2487
2488 return 0;
2489}
2490
2491int security_get_permissions(char *class, char ***perms, int *nperms)
2492{
2493 int rc = -ENOMEM, i;
2494 struct class_datum *match;
2495
James Morris0804d112008-06-06 18:40:29 +10002496 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002497
2498 match = hashtab_search(policydb.p_classes.table, class);
2499 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002500 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002501 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002502 rc = -EINVAL;
2503 goto out;
2504 }
2505
2506 *nperms = match->permissions.nprim;
2507 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2508 if (!*perms)
2509 goto out;
2510
2511 if (match->comdatum) {
2512 rc = hashtab_map(match->comdatum->permissions.table,
2513 get_permissions_callback, *perms);
2514 if (rc < 0)
2515 goto err;
2516 }
2517
2518 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2519 *perms);
2520 if (rc < 0)
2521 goto err;
2522
2523out:
James Morris0804d112008-06-06 18:40:29 +10002524 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002525 return rc;
2526
2527err:
James Morris0804d112008-06-06 18:40:29 +10002528 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002529 for (i = 0; i < *nperms; i++)
2530 kfree((*perms)[i]);
2531 kfree(*perms);
2532 return rc;
2533}
2534
Eric Paris3f120702007-09-21 14:37:10 -04002535int security_get_reject_unknown(void)
2536{
2537 return policydb.reject_unknown;
2538}
2539
2540int security_get_allow_unknown(void)
2541{
2542 return policydb.allow_unknown;
2543}
2544
Paul Moore3bb56b22008-01-29 08:38:19 -05002545/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002546 * security_policycap_supported - Check for a specific policy capability
2547 * @req_cap: capability
2548 *
2549 * Description:
2550 * This function queries the currently loaded policy to see if it supports the
2551 * capability specified by @req_cap. Returns true (1) if the capability is
2552 * supported, false (0) if it isn't supported.
2553 *
2554 */
2555int security_policycap_supported(unsigned int req_cap)
2556{
2557 int rc;
2558
James Morris0804d112008-06-06 18:40:29 +10002559 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002560 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10002561 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002562
2563 return rc;
2564}
2565
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002566struct selinux_audit_rule {
2567 u32 au_seqno;
2568 struct context au_ctxt;
2569};
2570
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002571void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002572{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002573 struct selinux_audit_rule *rule = vrule;
2574
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002575 if (rule) {
2576 context_destroy(&rule->au_ctxt);
2577 kfree(rule);
2578 }
2579}
2580
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002581int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002582{
2583 struct selinux_audit_rule *tmprule;
2584 struct role_datum *roledatum;
2585 struct type_datum *typedatum;
2586 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002587 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002588 int rc = 0;
2589
2590 *rule = NULL;
2591
2592 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002593 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002594
2595 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002596 case AUDIT_SUBJ_USER:
2597 case AUDIT_SUBJ_ROLE:
2598 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002599 case AUDIT_OBJ_USER:
2600 case AUDIT_OBJ_ROLE:
2601 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002602 /* only 'equals' and 'not equals' fit user, role, and type */
Al Viro5af75d82008-12-16 05:59:26 -05002603 if (op != Audit_equal && op != Audit_not_equal)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002604 return -EINVAL;
2605 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002606 case AUDIT_SUBJ_SEN:
2607 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002608 case AUDIT_OBJ_LEV_LOW:
2609 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002610 /* we do not allow a range, indicated by the presense of '-' */
2611 if (strchr(rulestr, '-'))
2612 return -EINVAL;
2613 break;
2614 default:
2615 /* only the above fields are valid */
2616 return -EINVAL;
2617 }
2618
2619 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2620 if (!tmprule)
2621 return -ENOMEM;
2622
2623 context_init(&tmprule->au_ctxt);
2624
James Morris0804d112008-06-06 18:40:29 +10002625 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002626
2627 tmprule->au_seqno = latest_granting;
2628
2629 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002630 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002631 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002632 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2633 if (!userdatum)
2634 rc = -EINVAL;
2635 else
2636 tmprule->au_ctxt.user = userdatum->value;
2637 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002638 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002639 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002640 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2641 if (!roledatum)
2642 rc = -EINVAL;
2643 else
2644 tmprule->au_ctxt.role = roledatum->value;
2645 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002646 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002647 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002648 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2649 if (!typedatum)
2650 rc = -EINVAL;
2651 else
2652 tmprule->au_ctxt.type = typedatum->value;
2653 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002654 case AUDIT_SUBJ_SEN:
2655 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002656 case AUDIT_OBJ_LEV_LOW:
2657 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002658 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2659 break;
2660 }
2661
James Morris0804d112008-06-06 18:40:29 +10002662 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002663
2664 if (rc) {
2665 selinux_audit_rule_free(tmprule);
2666 tmprule = NULL;
2667 }
2668
2669 *rule = tmprule;
2670
2671 return rc;
2672}
2673
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002674/* Check to see if the rule contains any selinux fields */
2675int selinux_audit_rule_known(struct audit_krule *rule)
2676{
2677 int i;
2678
2679 for (i = 0; i < rule->field_count; i++) {
2680 struct audit_field *f = &rule->fields[i];
2681 switch (f->type) {
2682 case AUDIT_SUBJ_USER:
2683 case AUDIT_SUBJ_ROLE:
2684 case AUDIT_SUBJ_TYPE:
2685 case AUDIT_SUBJ_SEN:
2686 case AUDIT_SUBJ_CLR:
2687 case AUDIT_OBJ_USER:
2688 case AUDIT_OBJ_ROLE:
2689 case AUDIT_OBJ_TYPE:
2690 case AUDIT_OBJ_LEV_LOW:
2691 case AUDIT_OBJ_LEV_HIGH:
2692 return 1;
2693 }
2694 }
2695
2696 return 0;
2697}
2698
2699int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04002700 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002701{
2702 struct context *ctxt;
2703 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002704 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002705 int match = 0;
2706
2707 if (!rule) {
2708 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002709 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002710 return -ENOENT;
2711 }
2712
James Morris0804d112008-06-06 18:40:29 +10002713 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002714
2715 if (rule->au_seqno < latest_granting) {
2716 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002717 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002718 match = -ESTALE;
2719 goto out;
2720 }
2721
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002722 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002723 if (!ctxt) {
2724 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002725 "selinux_audit_rule_match: unrecognized SID %d\n",
2726 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002727 match = -ENOENT;
2728 goto out;
2729 }
2730
2731 /* a field/op pair that is not caught here will simply fall through
2732 without a match */
2733 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002734 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002735 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002736 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002737 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002738 match = (ctxt->user == rule->au_ctxt.user);
2739 break;
Al Viro5af75d82008-12-16 05:59:26 -05002740 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002741 match = (ctxt->user != rule->au_ctxt.user);
2742 break;
2743 }
2744 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002745 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002746 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002747 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002748 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002749 match = (ctxt->role == rule->au_ctxt.role);
2750 break;
Al Viro5af75d82008-12-16 05:59:26 -05002751 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002752 match = (ctxt->role != rule->au_ctxt.role);
2753 break;
2754 }
2755 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002756 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002757 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002758 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002759 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002760 match = (ctxt->type == rule->au_ctxt.type);
2761 break;
Al Viro5af75d82008-12-16 05:59:26 -05002762 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002763 match = (ctxt->type != rule->au_ctxt.type);
2764 break;
2765 }
2766 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002767 case AUDIT_SUBJ_SEN:
2768 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002769 case AUDIT_OBJ_LEV_LOW:
2770 case AUDIT_OBJ_LEV_HIGH:
2771 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002772 field == AUDIT_OBJ_LEV_LOW) ?
2773 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002774 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002775 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002776 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002777 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002778 break;
Al Viro5af75d82008-12-16 05:59:26 -05002779 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002780 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002781 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002782 break;
Al Viro5af75d82008-12-16 05:59:26 -05002783 case Audit_lt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002784 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002785 level) &&
2786 !mls_level_eq(&rule->au_ctxt.range.level[0],
2787 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002788 break;
Al Viro5af75d82008-12-16 05:59:26 -05002789 case Audit_le:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002790 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002791 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002792 break;
Al Viro5af75d82008-12-16 05:59:26 -05002793 case Audit_gt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002794 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002795 &rule->au_ctxt.range.level[0]) &&
2796 !mls_level_eq(level,
2797 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002798 break;
Al Viro5af75d82008-12-16 05:59:26 -05002799 case Audit_ge:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002800 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002801 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002802 break;
2803 }
2804 }
2805
2806out:
James Morris0804d112008-06-06 18:40:29 +10002807 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002808 return match;
2809}
2810
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002811static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002812
2813static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
Eric Parisf5269712008-05-14 11:27:45 -04002814 u16 class, u32 perms, u32 *retained)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002815{
2816 int err = 0;
2817
2818 if (event == AVC_CALLBACK_RESET && aurule_callback)
2819 err = aurule_callback();
2820 return err;
2821}
2822
2823static int __init aurule_init(void)
2824{
2825 int err;
2826
2827 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002828 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002829 if (err)
2830 panic("avc_add_callback() failed, error %d\n", err);
2831
2832 return err;
2833}
2834__initcall(aurule_init);
2835
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002836#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002837/**
Paul Moore5778eab2007-02-28 15:14:22 -05002838 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2839 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002840 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002841 *
2842 * Description:
2843 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002844 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2845 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002846 *
2847 */
Paul Moore5778eab2007-02-28 15:14:22 -05002848static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002849 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002850{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002851 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002852
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002853 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2854 if (sid_cache == NULL)
2855 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002856 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002857 if (secattr->cache == NULL) {
2858 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002859 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002860 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002861
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002862 *sid_cache = sid;
2863 secattr->cache->free = kfree;
2864 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002865 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002866}
2867
2868/**
Paul Moore5778eab2007-02-28 15:14:22 -05002869 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002870 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002871 * @sid: the SELinux SID
2872 *
2873 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002874 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002875 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002876 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2877 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2878 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2879 * conversion for future lookups. Returns zero on success, negative values on
2880 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002881 *
2882 */
Paul Moore5778eab2007-02-28 15:14:22 -05002883int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002884 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002885{
2886 int rc = -EIDRM;
2887 struct context *ctx;
2888 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002889
2890 if (!ss_initialized) {
2891 *sid = SECSID_NULL;
2892 return 0;
2893 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002894
James Morris0804d112008-06-06 18:40:29 +10002895 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002896
Paul Moore701a90b2006-11-17 17:38:46 -05002897 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002898 *sid = *(u32 *)secattr->cache->data;
2899 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002900 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2901 *sid = secattr->attr.secid;
2902 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05002903 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002904 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002905 if (ctx == NULL)
2906 goto netlbl_secattr_to_sid_return;
2907
Paul Moore81990fb2008-10-03 10:51:15 -04002908 context_init(&ctx_new);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002909 ctx_new.user = ctx->user;
2910 ctx_new.role = ctx->role;
2911 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002912 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002913 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002914 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05002915 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002916 goto netlbl_secattr_to_sid_return;
Paul Moore81990fb2008-10-03 10:51:15 -04002917 memcpy(&ctx_new.range.level[1].cat,
2918 &ctx_new.range.level[0].cat,
2919 sizeof(ctx_new.range.level[0].cat));
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002920 }
2921 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2922 goto netlbl_secattr_to_sid_return_cleanup;
2923
2924 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2925 if (rc != 0)
2926 goto netlbl_secattr_to_sid_return_cleanup;
2927
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002928 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05002929
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002930 ebitmap_destroy(&ctx_new.range.level[0].cat);
2931 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002932 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002933 rc = 0;
2934 }
2935
2936netlbl_secattr_to_sid_return:
James Morris0804d112008-06-06 18:40:29 +10002937 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002938 return rc;
2939netlbl_secattr_to_sid_return_cleanup:
2940 ebitmap_destroy(&ctx_new.range.level[0].cat);
2941 goto netlbl_secattr_to_sid_return;
2942}
2943
2944/**
Paul Moore5778eab2007-02-28 15:14:22 -05002945 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2946 * @sid: the SELinux SID
2947 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002948 *
2949 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002950 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2951 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002952 *
2953 */
Paul Moore5778eab2007-02-28 15:14:22 -05002954int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002955{
Paul Moore99d854d2008-10-10 10:16:30 -04002956 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002957 struct context *ctx;
2958
2959 if (!ss_initialized)
2960 return 0;
2961
James Morris0804d112008-06-06 18:40:29 +10002962 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002963 ctx = sidtab_search(&sidtab, sid);
Paul Moore99d854d2008-10-10 10:16:30 -04002964 if (ctx == NULL) {
2965 rc = -ENOENT;
Paul Moore5778eab2007-02-28 15:14:22 -05002966 goto netlbl_sid_to_secattr_failure;
Paul Moore99d854d2008-10-10 10:16:30 -04002967 }
Paul Moore5778eab2007-02-28 15:14:22 -05002968 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2969 GFP_ATOMIC);
Paul Moore99d854d2008-10-10 10:16:30 -04002970 if (secattr->domain == NULL) {
2971 rc = -ENOMEM;
2972 goto netlbl_sid_to_secattr_failure;
2973 }
Paul Moore8d758992008-10-10 10:16:33 -04002974 secattr->attr.secid = sid;
2975 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
Paul Moore5778eab2007-02-28 15:14:22 -05002976 mls_export_netlbl_lvl(ctx, secattr);
2977 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002978 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05002979 goto netlbl_sid_to_secattr_failure;
James Morris0804d112008-06-06 18:40:29 +10002980 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002981
Paul Moore5778eab2007-02-28 15:14:22 -05002982 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05002983
Paul Moore5778eab2007-02-28 15:14:22 -05002984netlbl_sid_to_secattr_failure:
James Morris0804d112008-06-06 18:40:29 +10002985 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08002986 return rc;
2987}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002988#endif /* CONFIG_NETLABEL */