blob: 500e6f78e1159e1d568355ce74a113b4b49859c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implementation of the security services.
3 *
4 * Authors : Stephen Smalley, <sds@epoch.ncsc.mil>
Eric Paris5d55a342008-04-18 17:38:33 -04005 * James Morris <jmorris@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com>
8 *
9 * Support for enhanced MLS infrastructure.
Darrel Goeddel376bd9c2006-02-24 15:44:05 -060010 * Support for context based audit filters.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com>
13 *
Eric Paris5d55a342008-04-18 17:38:33 -040014 * Added conditional policy language extensions
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070016 * Updated: Hewlett-Packard <paul.moore@hp.com>
17 *
18 * Added support for NetLabel
Paul Moore3bb56b22008-01-29 08:38:19 -050019 * Added support for the policy capability bitmap
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070020 *
Chad Sellersb94c7e62006-11-06 12:38:18 -050021 * Updated: Chad Sellers <csellers@tresys.com>
22 *
23 * Added validation of kernel classes and permissions
24 *
Paul Moore3bb56b22008-01-29 08:38:19 -050025 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
Darrel Goeddel376bd9c2006-02-24 15:44:05 -060026 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
Chad Sellersb94c7e62006-11-06 12:38:18 -050027 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
29 * This program is free software; you can redistribute it and/or modify
Eric Paris5d55a342008-04-18 17:38:33 -040030 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * the Free Software Foundation, version 2.
32 */
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/string.h>
36#include <linux/spinlock.h>
Paul Moore9f2ad662006-11-17 17:38:53 -050037#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/errno.h>
39#include <linux/in.h>
40#include <linux/sched.h>
41#include <linux/audit.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080042#include <linux/mutex.h>
Adrian Bunk0e55a002008-03-31 01:54:02 +030043#include <linux/selinux.h>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070044#include <net/netlabel.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "flask.h"
47#include "avc.h"
48#include "avc_ss.h"
49#include "security.h"
50#include "context.h"
51#include "policydb.h"
52#include "sidtab.h"
53#include "services.h"
54#include "conditional.h"
55#include "mls.h"
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070056#include "objsec.h"
Paul Moorec60475b2007-02-28 15:14:23 -050057#include "netlabel.h"
Paul Moore3de4bab2006-11-17 17:38:54 -050058#include "xfrm.h"
Paul Moore02752762006-11-29 13:18:18 -050059#include "ebitmap.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020060#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62extern void selnl_notify_policyload(u32 seqno);
63unsigned int policydb_loaded_version;
64
Paul Moore3bb56b22008-01-29 08:38:19 -050065int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050066int selinux_policycap_openperm;
Paul Moore3bb56b22008-01-29 08:38:19 -050067
Chad Sellersb94c7e62006-11-06 12:38:18 -050068/*
69 * This is declared in avc.c
70 */
71extern const struct selinux_class_perm selinux_class_perm;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static DEFINE_RWLOCK(policy_rwlock);
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;
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900413 avd->flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /*
Eric Paris3f120702007-09-21 14:37:10 -0400416 * Check for all the invalid cases.
417 * - tclass 0
418 * - tclass > policy and > kernel
419 * - tclass > policy but is a userspace class
420 * - tclass > policy but we do not allow unknowns
421 */
422 if (unlikely(!tclass))
423 goto inval_class;
424 if (unlikely(tclass > policydb.p_classes.nprim))
425 if (tclass > kdefs->cts_len ||
Eric Pariscea78dc2008-06-09 15:43:12 -0400426 !kdefs->class_to_string[tclass] ||
Eric Paris3f120702007-09-21 14:37:10 -0400427 !policydb.allow_unknown)
428 goto inval_class;
429
430 /*
431 * Kernel class and we allow unknown so pad the allow decision
432 * the pad will be all 1 for unknown classes.
433 */
434 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
435 avd->allowed = policydb.undefined_perms[tclass - 1];
436
437 /*
438 * Not in policy. Since decision is completed (all 1 or all 0) return.
439 */
440 if (unlikely(tclass > policydb.p_classes.nprim))
441 return 0;
442
443 tclass_datum = policydb.class_val_to_struct[tclass - 1];
444
445 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 * If a specific type enforcement rule was defined for
447 * this permission check, then use it.
448 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700450 avkey.specified = AVTAB_AV;
451 sattr = &policydb.type_attr_map[scontext->type - 1];
452 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900453 ebitmap_for_each_positive_bit(sattr, snode, i) {
454 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700455 avkey.source_type = i + 1;
456 avkey.target_type = j + 1;
457 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300458 node;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700459 node = avtab_search_node_next(node, avkey.specified)) {
460 if (node->key.specified == AVTAB_ALLOWED)
461 avd->allowed |= node->datum.data;
462 else if (node->key.specified == AVTAB_AUDITALLOW)
463 avd->auditallow |= node->datum.data;
464 else if (node->key.specified == AVTAB_AUDITDENY)
465 avd->auditdeny &= node->datum.data;
466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Stephen Smalley782ebb92005-09-03 15:55:16 -0700468 /* Check conditional av table for additional permissions */
469 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
470
471 }
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 /*
475 * Remove any permissions prohibited by a constraint (this includes
476 * the MLS policy).
477 */
478 constraint = tclass_datum->constraints;
479 while (constraint) {
480 if ((constraint->permissions & (avd->allowed)) &&
481 !constraint_expr_eval(scontext, tcontext, NULL,
482 constraint->expr)) {
483 avd->allowed = (avd->allowed) & ~(constraint->permissions);
484 }
485 constraint = constraint->next;
486 }
487
488 /*
489 * If checking process transition permission and the
490 * role is changing, then check the (current_role, new_role)
491 * pair.
492 */
493 if (tclass == SECCLASS_PROCESS &&
494 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
495 scontext->role != tcontext->role) {
496 for (ra = policydb.role_allow; ra; ra = ra->next) {
497 if (scontext->role == ra->role &&
498 tcontext->role == ra->new_role)
499 break;
500 }
501 if (!ra)
502 avd->allowed = (avd->allowed) & ~(PROCESS__TRANSITION |
Eric Paris5d55a342008-04-18 17:38:33 -0400503 PROCESS__DYNTRANSITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
KaiGai Koheid9250de2008-08-28 16:35:57 +0900506 /*
507 * If the given source and target types have boundary
508 * constraint, lazy checks have to mask any violated
509 * permission and notice it to userspace via audit.
510 */
511 type_attribute_bounds_av(scontext, tcontext,
512 tclass, requested, avd);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
Eric Paris3f120702007-09-21 14:37:10 -0400515
516inval_class:
Stephen Smalley22df4ad2008-06-09 16:03:56 -0400517 if (!tclass || tclass > kdefs->cts_len ||
518 !kdefs->class_to_string[tclass]) {
519 if (printk_ratelimit())
520 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
521 __func__, tclass);
522 return -EINVAL;
523 }
524
525 /*
526 * Known to the kernel, but not to the policy.
527 * Handle as a denial (allowed is 0).
528 */
529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
532static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400533 struct context *ncontext,
534 struct context *tcontext,
535 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 char *o = NULL, *n = NULL, *t = NULL;
538 u32 olen, nlen, tlen;
539
540 if (context_struct_to_string(ocontext, &o, &olen) < 0)
541 goto out;
542 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
543 goto out;
544 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
545 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100546 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400547 "security_validate_transition: denied for"
548 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
549 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550out:
551 kfree(o);
552 kfree(n);
553 kfree(t);
554
555 if (!selinux_enforcing)
556 return 0;
557 return -EPERM;
558}
559
560int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Eric Paris5d55a342008-04-18 17:38:33 -0400561 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
563 struct context *ocontext;
564 struct context *ncontext;
565 struct context *tcontext;
566 struct class_datum *tclass_datum;
567 struct constraint_node *constraint;
568 int rc = 0;
569
570 if (!ss_initialized)
571 return 0;
572
James Morris0804d112008-06-06 18:40:29 +1000573 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /*
576 * Remap extended Netlink classes for old policy versions.
577 * Do this here rather than socket_type_to_security_class()
578 * in case a newer policy version is loaded, allowing sockets
579 * to remain in the correct class.
580 */
581 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
582 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
583 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
584 tclass = SECCLASS_NETLINK_SOCKET;
585
586 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400587 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
588 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 rc = -EINVAL;
590 goto out;
591 }
592 tclass_datum = policydb.class_val_to_struct[tclass - 1];
593
594 ocontext = sidtab_search(&sidtab, oldsid);
595 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400596 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
597 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 rc = -EINVAL;
599 goto out;
600 }
601
602 ncontext = sidtab_search(&sidtab, newsid);
603 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400604 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
605 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 rc = -EINVAL;
607 goto out;
608 }
609
610 tcontext = sidtab_search(&sidtab, tasksid);
611 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400612 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
613 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 rc = -EINVAL;
615 goto out;
616 }
617
618 constraint = tclass_datum->validatetrans;
619 while (constraint) {
620 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400621 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400623 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto out;
625 }
626 constraint = constraint->next;
627 }
628
629out:
James Morris0804d112008-06-06 18:40:29 +1000630 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return rc;
632}
633
KaiGai Koheid9250de2008-08-28 16:35:57 +0900634/*
635 * security_bounded_transition - check whether the given
636 * transition is directed to bounded, or not.
637 * It returns 0, if @newsid is bounded by @oldsid.
638 * Otherwise, it returns error code.
639 *
640 * @oldsid : current security identifier
641 * @newsid : destinated security identifier
642 */
643int security_bounded_transition(u32 old_sid, u32 new_sid)
644{
645 struct context *old_context, *new_context;
646 struct type_datum *type;
647 int index;
648 int rc = -EINVAL;
649
650 read_lock(&policy_rwlock);
651
652 old_context = sidtab_search(&sidtab, old_sid);
653 if (!old_context) {
654 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
655 __func__, old_sid);
656 goto out;
657 }
658
659 new_context = sidtab_search(&sidtab, new_sid);
660 if (!new_context) {
661 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
662 __func__, new_sid);
663 goto out;
664 }
665
666 /* type/domain unchaned */
667 if (old_context->type == new_context->type) {
668 rc = 0;
669 goto out;
670 }
671
672 index = new_context->type;
673 while (true) {
674 type = policydb.type_val_to_struct[index - 1];
675 BUG_ON(!type);
676
677 /* not bounded anymore */
678 if (!type->bounds) {
679 rc = -EPERM;
680 break;
681 }
682
683 /* @newsid is bounded by @oldsid */
684 if (type->bounds == old_context->type) {
685 rc = 0;
686 break;
687 }
688 index = type->bounds;
689 }
690out:
691 read_unlock(&policy_rwlock);
692
693 return rc;
694}
695
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/**
698 * security_compute_av - Compute access vector decisions.
699 * @ssid: source security identifier
700 * @tsid: target security identifier
701 * @tclass: target security class
702 * @requested: requested permissions
703 * @avd: access vector decisions
704 *
705 * Compute a set of access vector decisions based on the
706 * SID pair (@ssid, @tsid) for the permissions in @tclass.
707 * Return -%EINVAL if any of the parameters are invalid or %0
708 * if the access vector decisions were computed successfully.
709 */
710int security_compute_av(u32 ssid,
711 u32 tsid,
712 u16 tclass,
713 u32 requested,
714 struct av_decision *avd)
715{
716 struct context *scontext = NULL, *tcontext = NULL;
717 int rc = 0;
718
719 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700720 avd->allowed = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 avd->auditallow = 0;
722 avd->auditdeny = 0xffffffff;
723 avd->seqno = latest_granting;
724 return 0;
725 }
726
James Morris0804d112008-06-06 18:40:29 +1000727 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 scontext = sidtab_search(&sidtab, ssid);
730 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400731 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
732 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 rc = -EINVAL;
734 goto out;
735 }
736 tcontext = sidtab_search(&sidtab, tsid);
737 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400738 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
739 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 rc = -EINVAL;
741 goto out;
742 }
743
744 rc = context_struct_compute_av(scontext, tcontext, tclass,
745 requested, avd);
KaiGai Kohei8a6f83a2009-04-01 10:07:57 +0900746
747 /* permissive domain? */
748 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
749 avd->flags |= AVD_FLAGS_PERMISSIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750out:
James Morris0804d112008-06-06 18:40:29 +1000751 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return rc;
753}
754
755/*
756 * Write the security context string representation of
757 * the context structure `context' into a dynamically
758 * allocated string of the correct size. Set `*scontext'
759 * to point to this string and set `*scontext_len' to
760 * the length of the string.
761 */
762static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
763{
764 char *scontextp;
765
766 *scontext = NULL;
767 *scontext_len = 0;
768
Stephen Smalley12b29f32008-05-07 13:03:20 -0400769 if (context->len) {
770 *scontext_len = context->len;
771 *scontext = kstrdup(context->str, GFP_ATOMIC);
772 if (!(*scontext))
773 return -ENOMEM;
774 return 0;
775 }
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 /* Compute the size of the context. */
778 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
779 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
780 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
781 *scontext_len += mls_compute_context_len(context);
782
783 /* Allocate space for the context; caller must free this space. */
784 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400785 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 *scontext = scontextp;
788
789 /*
790 * Copy the user name, role name and type name into the context.
791 */
792 sprintf(scontextp, "%s:%s:%s",
793 policydb.p_user_val_to_name[context->user - 1],
794 policydb.p_role_val_to_name[context->role - 1],
795 policydb.p_type_val_to_name[context->type - 1]);
796 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400797 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
798 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 mls_sid_to_context(context, &scontextp);
801
802 *scontextp = 0;
803
804 return 0;
805}
806
807#include "initial_sid_to_string.h"
808
James Carterf0ee2e42007-04-04 10:11:29 -0400809const char *security_get_initial_sid_context(u32 sid)
810{
811 if (unlikely(sid > SECINITSID_NUM))
812 return NULL;
813 return initial_sid_to_string[sid];
814}
815
Stephen Smalley12b29f32008-05-07 13:03:20 -0400816static int security_sid_to_context_core(u32 sid, char **scontext,
817 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
819 struct context *context;
820 int rc = 0;
821
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500822 *scontext = NULL;
823 *scontext_len = 0;
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (!ss_initialized) {
826 if (sid <= SECINITSID_NUM) {
827 char *scontextp;
828
829 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -0400830 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -0700831 if (!scontextp) {
832 rc = -ENOMEM;
833 goto out;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 strcpy(scontextp, initial_sid_to_string[sid]);
836 *scontext = scontextp;
837 goto out;
838 }
Eric Paris744ba352008-04-17 11:52:44 -0400839 printk(KERN_ERR "SELinux: %s: called before initial "
840 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 rc = -EINVAL;
842 goto out;
843 }
James Morris0804d112008-06-06 18:40:29 +1000844 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400845 if (force)
846 context = sidtab_search_force(&sidtab, sid);
847 else
848 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -0400850 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
851 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 rc = -EINVAL;
853 goto out_unlock;
854 }
855 rc = context_struct_to_string(context, scontext, scontext_len);
856out_unlock:
James Morris0804d112008-06-06 18:40:29 +1000857 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858out:
859 return rc;
860
861}
862
Stephen Smalley12b29f32008-05-07 13:03:20 -0400863/**
864 * security_sid_to_context - Obtain a context for a given SID.
865 * @sid: security identifier, SID
866 * @scontext: security context
867 * @scontext_len: length in bytes
868 *
869 * Write the string representation of the context associated with @sid
870 * into a dynamically allocated string of the correct size. Set @scontext
871 * to point to this string and set @scontext_len to the length of the string.
872 */
873int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Stephen Smalley12b29f32008-05-07 13:03:20 -0400875 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
876}
877
878int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
879{
880 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
881}
882
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400883/*
884 * Caveat: Mutates scontext.
885 */
Stephen Smalley12b29f32008-05-07 13:03:20 -0400886static int string_to_context_struct(struct policydb *pol,
887 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400888 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -0400889 u32 scontext_len,
890 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400891 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -0400892{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct role_datum *role;
894 struct type_datum *typdatum;
895 struct user_datum *usrdatum;
896 char *scontextp, *p, oldc;
897 int rc = 0;
898
Stephen Smalley12b29f32008-05-07 13:03:20 -0400899 context_init(ctx);
900
Stephen Smalley12b29f32008-05-07 13:03:20 -0400901 /* Parse the security context. */
902
903 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400904 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400905
906 /* Extract the user. */
907 p = scontextp;
908 while (*p && *p != ':')
909 p++;
910
911 if (*p == 0)
912 goto out;
913
914 *p++ = 0;
915
916 usrdatum = hashtab_search(pol->p_users.table, scontextp);
917 if (!usrdatum)
918 goto out;
919
920 ctx->user = usrdatum->value;
921
922 /* Extract role. */
923 scontextp = p;
924 while (*p && *p != ':')
925 p++;
926
927 if (*p == 0)
928 goto out;
929
930 *p++ = 0;
931
932 role = hashtab_search(pol->p_roles.table, scontextp);
933 if (!role)
934 goto out;
935 ctx->role = role->value;
936
937 /* Extract type. */
938 scontextp = p;
939 while (*p && *p != ':')
940 p++;
941 oldc = *p;
942 *p++ = 0;
943
944 typdatum = hashtab_search(pol->p_types.table, scontextp);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900945 if (!typdatum || typdatum->attribute)
Stephen Smalley12b29f32008-05-07 13:03:20 -0400946 goto out;
947
948 ctx->type = typdatum->value;
949
950 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
951 if (rc)
952 goto out;
953
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400954 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -0400955 rc = -EINVAL;
956 goto out;
957 }
958
959 /* Check the validity of the new context. */
960 if (!policydb_context_isvalid(pol, ctx)) {
961 rc = -EINVAL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400962 goto out;
963 }
964 rc = 0;
965out:
Eric Paris8e531af2008-09-03 11:49:47 -0400966 if (rc)
967 context_destroy(ctx);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400968 return rc;
969}
970
971static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
972 u32 *sid, u32 def_sid, gfp_t gfp_flags,
973 int force)
974{
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400975 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -0400976 struct context context;
977 int rc = 0;
978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!ss_initialized) {
980 int i;
981
982 for (i = 1; i < SECINITSID_NUM; i++) {
983 if (!strcmp(initial_sid_to_string[i], scontext)) {
984 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 }
988 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 *sid = SECSID_NULL;
992
Stephen Smalley9a59daa2008-05-14 10:33:55 -0400993 /* Copy the string so that we can modify the copy as we parse it. */
994 scontext2 = kmalloc(scontext_len+1, gfp_flags);
995 if (!scontext2)
996 return -ENOMEM;
997 memcpy(scontext2, scontext, scontext_len);
998 scontext2[scontext_len] = 0;
999
1000 if (force) {
1001 /* Save another copy for storing in uninterpreted form */
1002 str = kstrdup(scontext2, gfp_flags);
1003 if (!str) {
1004 kfree(scontext2);
1005 return -ENOMEM;
1006 }
1007 }
1008
James Morris0804d112008-06-06 18:40:29 +10001009 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001010 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001011 scontext2, scontext_len,
1012 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001013 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001014 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001015 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001016 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001017 } else if (rc)
1018 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Eric Paris8e531af2008-09-03 11:49:47 -04001020 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021out:
James Morris0804d112008-06-06 18:40:29 +10001022 read_unlock(&policy_rwlock);
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001023 kfree(scontext2);
1024 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 return rc;
1026}
1027
James Morrisf5c1d5b2005-07-28 01:07:37 -07001028/**
1029 * security_context_to_sid - Obtain a SID for a given security context.
1030 * @scontext: security context
1031 * @scontext_len: length in bytes
1032 * @sid: security identifier, SID
1033 *
1034 * Obtains a SID associated with the security context that
1035 * has the string representation specified by @scontext.
1036 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1037 * memory is available, or 0 on success.
1038 */
David Howells8f0cfa52008-04-29 00:59:41 -07001039int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001040{
1041 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001042 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001043}
1044
1045/**
1046 * security_context_to_sid_default - Obtain a SID for a given security context,
1047 * falling back to specified default if needed.
1048 *
1049 * @scontext: security context
1050 * @scontext_len: length in bytes
1051 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -07001052 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -07001053 *
1054 * Obtains a SID associated with the security context that
1055 * has the string representation specified by @scontext.
1056 * The default SID is passed to the MLS layer to be used to allow
1057 * kernel labeling of the MLS field if the MLS field is not present
1058 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -04001059 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -07001060 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1061 * memory is available, or 0 on success.
1062 */
David Howells7bf570d2008-04-29 20:52:51 +01001063int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1064 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001065{
1066 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001067 sid, def_sid, gfp_flags, 1);
1068}
1069
1070int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1071 u32 *sid)
1072{
1073 return security_context_to_sid_core(scontext, scontext_len,
1074 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001075}
1076
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077static int compute_sid_handle_invalid_context(
1078 struct context *scontext,
1079 struct context *tcontext,
1080 u16 tclass,
1081 struct context *newcontext)
1082{
1083 char *s = NULL, *t = NULL, *n = NULL;
1084 u32 slen, tlen, nlen;
1085
1086 if (context_struct_to_string(scontext, &s, &slen) < 0)
1087 goto out;
1088 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
1089 goto out;
1090 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
1091 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001092 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 "security_compute_sid: invalid context %s"
1094 " for scontext=%s"
1095 " tcontext=%s"
1096 " tclass=%s",
1097 n, s, t, policydb.p_class_val_to_name[tclass-1]);
1098out:
1099 kfree(s);
1100 kfree(t);
1101 kfree(n);
1102 if (!selinux_enforcing)
1103 return 0;
1104 return -EACCES;
1105}
1106
1107static int security_compute_sid(u32 ssid,
1108 u32 tsid,
1109 u16 tclass,
1110 u32 specified,
1111 u32 *out_sid)
1112{
1113 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1114 struct role_trans *roletr = NULL;
1115 struct avtab_key avkey;
1116 struct avtab_datum *avdatum;
1117 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int rc = 0;
1119
1120 if (!ss_initialized) {
1121 switch (tclass) {
1122 case SECCLASS_PROCESS:
1123 *out_sid = ssid;
1124 break;
1125 default:
1126 *out_sid = tsid;
1127 break;
1128 }
1129 goto out;
1130 }
1131
Venkat Yekkirala851f8a62006-07-30 03:03:18 -07001132 context_init(&newcontext);
1133
James Morris0804d112008-06-06 18:40:29 +10001134 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 scontext = sidtab_search(&sidtab, ssid);
1137 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001138 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1139 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 rc = -EINVAL;
1141 goto out_unlock;
1142 }
1143 tcontext = sidtab_search(&sidtab, tsid);
1144 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001145 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1146 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 rc = -EINVAL;
1148 goto out_unlock;
1149 }
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 /* Set the user identity. */
1152 switch (specified) {
1153 case AVTAB_TRANSITION:
1154 case AVTAB_CHANGE:
1155 /* Use the process user identity. */
1156 newcontext.user = scontext->user;
1157 break;
1158 case AVTAB_MEMBER:
1159 /* Use the related object owner. */
1160 newcontext.user = tcontext->user;
1161 break;
1162 }
1163
1164 /* Set the role and type to default values. */
1165 switch (tclass) {
1166 case SECCLASS_PROCESS:
1167 /* Use the current role and type of process. */
1168 newcontext.role = scontext->role;
1169 newcontext.type = scontext->type;
1170 break;
1171 default:
1172 /* Use the well-defined object role. */
1173 newcontext.role = OBJECT_R_VAL;
1174 /* Use the type of the related object. */
1175 newcontext.type = tcontext->type;
1176 }
1177
1178 /* Look for a type transition/member/change rule. */
1179 avkey.source_type = scontext->type;
1180 avkey.target_type = tcontext->type;
1181 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001182 avkey.specified = specified;
1183 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001186 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001187 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03001188 for (; node; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001189 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 avdatum = &node->datum;
1191 break;
1192 }
1193 }
1194 }
1195
Stephen Smalley782ebb92005-09-03 15:55:16 -07001196 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001198 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200
1201 /* Check for class-specific changes. */
1202 switch (tclass) {
1203 case SECCLASS_PROCESS:
1204 if (specified & AVTAB_TRANSITION) {
1205 /* Look for a role transition rule. */
1206 for (roletr = policydb.role_tr; roletr;
1207 roletr = roletr->next) {
1208 if (roletr->role == scontext->role &&
1209 roletr->type == tcontext->type) {
1210 /* Use the role transition rule. */
1211 newcontext.role = roletr->new_role;
1212 break;
1213 }
1214 }
1215 }
1216 break;
1217 default:
1218 break;
1219 }
1220
1221 /* Set the MLS attributes.
1222 This is done last because it may allocate memory. */
1223 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1224 if (rc)
1225 goto out_unlock;
1226
1227 /* Check the validity of the context. */
1228 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1229 rc = compute_sid_handle_invalid_context(scontext,
1230 tcontext,
1231 tclass,
1232 &newcontext);
1233 if (rc)
1234 goto out_unlock;
1235 }
1236 /* Obtain the sid for the context. */
1237 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1238out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001239 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 context_destroy(&newcontext);
1241out:
1242 return rc;
1243}
1244
1245/**
1246 * security_transition_sid - Compute the SID for a new subject/object.
1247 * @ssid: source security identifier
1248 * @tsid: target security identifier
1249 * @tclass: target security class
1250 * @out_sid: security identifier for new subject/object
1251 *
1252 * Compute a SID to use for labeling a new subject or object in the
1253 * class @tclass based on a SID pair (@ssid, @tsid).
1254 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1255 * if insufficient memory is available, or %0 if the new SID was
1256 * computed successfully.
1257 */
1258int security_transition_sid(u32 ssid,
1259 u32 tsid,
1260 u16 tclass,
1261 u32 *out_sid)
1262{
1263 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1264}
1265
1266/**
1267 * security_member_sid - Compute the SID for member selection.
1268 * @ssid: source security identifier
1269 * @tsid: target security identifier
1270 * @tclass: target security class
1271 * @out_sid: security identifier for selected member
1272 *
1273 * Compute a SID to use when selecting a member of a polyinstantiated
1274 * object of class @tclass based on a SID pair (@ssid, @tsid).
1275 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1276 * if insufficient memory is available, or %0 if the SID was
1277 * computed successfully.
1278 */
1279int security_member_sid(u32 ssid,
1280 u32 tsid,
1281 u16 tclass,
1282 u32 *out_sid)
1283{
1284 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1285}
1286
1287/**
1288 * security_change_sid - Compute the SID for object relabeling.
1289 * @ssid: source security identifier
1290 * @tsid: target security identifier
1291 * @tclass: target security class
1292 * @out_sid: security identifier for selected member
1293 *
1294 * Compute a SID to use for relabeling an object of class @tclass
1295 * based on a SID pair (@ssid, @tsid).
1296 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1297 * if insufficient memory is available, or %0 if the SID was
1298 * computed successfully.
1299 */
1300int security_change_sid(u32 ssid,
1301 u32 tsid,
1302 u16 tclass,
1303 u32 *out_sid)
1304{
1305 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1306}
1307
Chad Sellersb94c7e62006-11-06 12:38:18 -05001308/*
1309 * Verify that each kernel class that is defined in the
1310 * policy is correct
1311 */
1312static int validate_classes(struct policydb *p)
1313{
1314 int i, j;
1315 struct class_datum *cladatum;
1316 struct perm_datum *perdatum;
1317 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1318 u16 class_val;
1319 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1320 const char *def_class, *def_perm, *pol_class;
1321 struct symtab *perms;
Eric Paris6cbe2702008-06-09 16:51:37 -04001322 bool print_unknown_handle = 0;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001323
Eric Paris3f120702007-09-21 14:37:10 -04001324 if (p->allow_unknown) {
1325 u32 num_classes = kdefs->cts_len;
1326 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1327 if (!p->undefined_perms)
1328 return -ENOMEM;
1329 }
1330
Chad Sellersb94c7e62006-11-06 12:38:18 -05001331 for (i = 1; i < kdefs->cts_len; i++) {
1332 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001333 if (!def_class)
1334 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001335 if (i > p->p_classes.nprim) {
1336 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001337 "SELinux: class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001338 def_class);
Eric Paris3f120702007-09-21 14:37:10 -04001339 if (p->reject_unknown)
1340 return -EINVAL;
1341 if (p->allow_unknown)
1342 p->undefined_perms[i-1] = ~0U;
Eric Paris6cbe2702008-06-09 16:51:37 -04001343 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001344 continue;
1345 }
1346 pol_class = p->p_class_val_to_name[i-1];
1347 if (strcmp(pol_class, def_class)) {
1348 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001349 "SELinux: class %d is incorrect, found %s but should be %s\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001350 i, pol_class, def_class);
1351 return -EINVAL;
1352 }
1353 }
1354 for (i = 0; i < kdefs->av_pts_len; i++) {
1355 class_val = kdefs->av_perm_to_string[i].tclass;
1356 perm_val = kdefs->av_perm_to_string[i].value;
1357 def_perm = kdefs->av_perm_to_string[i].name;
1358 if (class_val > p->p_classes.nprim)
1359 continue;
1360 pol_class = p->p_class_val_to_name[class_val-1];
1361 cladatum = hashtab_search(p->p_classes.table, pol_class);
1362 BUG_ON(!cladatum);
1363 perms = &cladatum->permissions;
1364 nprim = 1 << (perms->nprim - 1);
1365 if (perm_val > nprim) {
1366 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001367 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001368 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001369 if (p->reject_unknown)
1370 return -EINVAL;
1371 if (p->allow_unknown)
1372 p->undefined_perms[class_val-1] |= perm_val;
Eric Paris6cbe2702008-06-09 16:51:37 -04001373 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001374 continue;
1375 }
1376 perdatum = hashtab_search(perms->table, def_perm);
1377 if (perdatum == NULL) {
1378 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001379 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001380 def_perm, pol_class);
1381 return -EINVAL;
1382 }
1383 pol_val = 1 << (perdatum->value - 1);
1384 if (pol_val != perm_val) {
1385 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001386 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001387 def_perm, pol_class);
1388 return -EINVAL;
1389 }
1390 }
1391 for (i = 0; i < kdefs->av_inherit_len; i++) {
1392 class_val = kdefs->av_inherit[i].tclass;
1393 if (class_val > p->p_classes.nprim)
1394 continue;
1395 pol_class = p->p_class_val_to_name[class_val-1];
1396 cladatum = hashtab_search(p->p_classes.table, pol_class);
1397 BUG_ON(!cladatum);
1398 if (!cladatum->comdatum) {
1399 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001400 "SELinux: class %s should have an inherits clause but does not\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001401 pol_class);
1402 return -EINVAL;
1403 }
1404 tmp = kdefs->av_inherit[i].common_base;
1405 common_pts_len = 0;
1406 while (!(tmp & 0x01)) {
1407 common_pts_len++;
1408 tmp >>= 1;
1409 }
1410 perms = &cladatum->comdatum->permissions;
1411 for (j = 0; j < common_pts_len; j++) {
1412 def_perm = kdefs->av_inherit[i].common_pts[j];
1413 if (j >= perms->nprim) {
1414 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001415 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001416 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001417 if (p->reject_unknown)
1418 return -EINVAL;
1419 if (p->allow_unknown)
1420 p->undefined_perms[class_val-1] |= (1 << j);
Eric Paris6cbe2702008-06-09 16:51:37 -04001421 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001422 continue;
1423 }
1424 perdatum = hashtab_search(perms->table, def_perm);
1425 if (perdatum == NULL) {
1426 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001427 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001428 def_perm, pol_class);
1429 return -EINVAL;
1430 }
1431 if (perdatum->value != j + 1) {
1432 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001433 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001434 def_perm, pol_class);
1435 return -EINVAL;
1436 }
1437 }
1438 }
Eric Paris6cbe2702008-06-09 16:51:37 -04001439 if (print_unknown_handle)
1440 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
1441 (security_get_allow_unknown() ? "allowed" : "denied"));
Chad Sellersb94c7e62006-11-06 12:38:18 -05001442 return 0;
1443}
1444
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445/* Clone the SID into the new SID table. */
1446static int clone_sid(u32 sid,
1447 struct context *context,
1448 void *arg)
1449{
1450 struct sidtab *s = arg;
1451
1452 return sidtab_insert(s, sid, context);
1453}
1454
1455static inline int convert_context_handle_invalid_context(struct context *context)
1456{
1457 int rc = 0;
1458
1459 if (selinux_enforcing) {
1460 rc = -EINVAL;
1461 } else {
1462 char *s;
1463 u32 len;
1464
Stephen Smalley12b29f32008-05-07 13:03:20 -04001465 if (!context_struct_to_string(context, &s, &len)) {
1466 printk(KERN_WARNING
1467 "SELinux: Context %s would be invalid if enforcing\n",
1468 s);
1469 kfree(s);
1470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 return rc;
1473}
1474
1475struct convert_context_args {
1476 struct policydb *oldp;
1477 struct policydb *newp;
1478};
1479
1480/*
1481 * Convert the values in the security context
1482 * structure `c' from the values specified
1483 * in the policy `p->oldp' to the values specified
1484 * in the policy `p->newp'. Verify that the
1485 * context is valid under the new policy.
1486 */
1487static int convert_context(u32 key,
1488 struct context *c,
1489 void *p)
1490{
1491 struct convert_context_args *args;
1492 struct context oldc;
1493 struct role_datum *role;
1494 struct type_datum *typdatum;
1495 struct user_datum *usrdatum;
1496 char *s;
1497 u32 len;
1498 int rc;
1499
1500 args = p;
1501
Stephen Smalley12b29f32008-05-07 13:03:20 -04001502 if (c->str) {
1503 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001504 s = kstrdup(c->str, GFP_KERNEL);
1505 if (!s) {
1506 rc = -ENOMEM;
1507 goto out;
1508 }
1509 rc = string_to_context_struct(args->newp, NULL, s,
1510 c->len, &ctx, SECSID_NULL);
1511 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001512 if (!rc) {
1513 printk(KERN_INFO
1514 "SELinux: Context %s became valid (mapped).\n",
1515 c->str);
1516 /* Replace string with mapped representation. */
1517 kfree(c->str);
1518 memcpy(c, &ctx, sizeof(*c));
1519 goto out;
1520 } else if (rc == -EINVAL) {
1521 /* Retain string representation for later mapping. */
1522 rc = 0;
1523 goto out;
1524 } else {
1525 /* Other error condition, e.g. ENOMEM. */
1526 printk(KERN_ERR
1527 "SELinux: Unable to map context %s, rc = %d.\n",
1528 c->str, -rc);
1529 goto out;
1530 }
1531 }
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 rc = context_cpy(&oldc, c);
1534 if (rc)
1535 goto out;
1536
1537 rc = -EINVAL;
1538
1539 /* Convert the user. */
1540 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001541 args->oldp->p_user_val_to_name[c->user - 1]);
1542 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 c->user = usrdatum->value;
1545
1546 /* Convert the role. */
1547 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001548 args->oldp->p_role_val_to_name[c->role - 1]);
1549 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 c->role = role->value;
1552
1553 /* Convert the type. */
1554 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001555 args->oldp->p_type_val_to_name[c->type - 1]);
1556 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 c->type = typdatum->value;
1559
1560 rc = mls_convert_context(args->oldp, args->newp, c);
1561 if (rc)
1562 goto bad;
1563
1564 /* Check the validity of the new context. */
1565 if (!policydb_context_isvalid(args->newp, c)) {
1566 rc = convert_context_handle_invalid_context(&oldc);
1567 if (rc)
1568 goto bad;
1569 }
1570
1571 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001572 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573out:
1574 return rc;
1575bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001576 /* Map old representation to string and save it. */
1577 if (context_struct_to_string(&oldc, &s, &len))
1578 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001580 context_destroy(c);
1581 c->str = s;
1582 c->len = len;
1583 printk(KERN_INFO
1584 "SELinux: Context %s became invalid (unmapped).\n",
1585 c->str);
1586 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 goto out;
1588}
1589
Paul Moore3bb56b22008-01-29 08:38:19 -05001590static void security_load_policycaps(void)
1591{
1592 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1593 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001594 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1595 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001596}
1597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001599static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601/**
1602 * security_load_policy - Load a security policy configuration.
1603 * @data: binary policy data
1604 * @len: length of data in bytes
1605 *
1606 * Load a new set of security policy configuration data,
1607 * validate it and convert the SID table as necessary.
1608 * This function will flush the access vector cache after
1609 * loading the new policy.
1610 */
1611int security_load_policy(void *data, size_t len)
1612{
1613 struct policydb oldpolicydb, newpolicydb;
1614 struct sidtab oldsidtab, newsidtab;
1615 struct convert_context_args args;
1616 u32 seqno;
1617 int rc = 0;
1618 struct policy_file file = { data, len }, *fp = &file;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if (!ss_initialized) {
1621 avtab_cache_init();
1622 if (policydb_read(&policydb, fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 avtab_cache_destroy();
1624 return -EINVAL;
1625 }
1626 if (policydb_load_isids(&policydb, &sidtab)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 policydb_destroy(&policydb);
1628 avtab_cache_destroy();
1629 return -EINVAL;
1630 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001631 /* Verify that the kernel defined classes are correct. */
1632 if (validate_classes(&policydb)) {
1633 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001634 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001635 sidtab_destroy(&sidtab);
1636 policydb_destroy(&policydb);
1637 avtab_cache_destroy();
1638 return -EINVAL;
1639 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001640 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 policydb_loaded_version = policydb.policyvers;
1642 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001643 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001645 avc_ss_reset(seqno);
1646 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001647 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001648 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return 0;
1650 }
1651
1652#if 0
1653 sidtab_hash_eval(&sidtab, "sids");
1654#endif
1655
Eric Paris89abd0a2008-06-09 15:58:04 -04001656 if (policydb_read(&newpolicydb, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Stephen Smalley12b29f32008-05-07 13:03:20 -04001659 if (sidtab_init(&newsidtab)) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001660 policydb_destroy(&newpolicydb);
1661 return -ENOMEM;
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Chad Sellersb94c7e62006-11-06 12:38:18 -05001664 /* Verify that the kernel defined classes are correct. */
1665 if (validate_classes(&newpolicydb)) {
1666 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001667 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001668 rc = -EINVAL;
1669 goto err;
1670 }
1671
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001672 rc = security_preserve_bools(&newpolicydb);
1673 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001674 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001675 goto err;
1676 }
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /* Clone the SID table. */
1679 sidtab_shutdown(&sidtab);
1680 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1681 rc = -ENOMEM;
1682 goto err;
1683 }
1684
Stephen Smalley12b29f32008-05-07 13:03:20 -04001685 /*
1686 * Convert the internal representations of contexts
1687 * in the new SID table.
1688 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 args.oldp = &policydb;
1690 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001691 rc = sidtab_map(&newsidtab, convert_context, &args);
1692 if (rc)
1693 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 /* Save the old policydb and SID table to free later. */
1696 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1697 sidtab_set(&oldsidtab, &sidtab);
1698
1699 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10001700 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 memcpy(&policydb, &newpolicydb, sizeof policydb);
1702 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001703 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 seqno = ++latest_granting;
1705 policydb_loaded_version = policydb.policyvers;
James Morris0804d112008-06-06 18:40:29 +10001706 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707
1708 /* Free the old policydb and SID table. */
1709 policydb_destroy(&oldpolicydb);
1710 sidtab_destroy(&oldsidtab);
1711
1712 avc_ss_reset(seqno);
1713 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001714 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001715 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 return 0;
1718
1719err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 sidtab_destroy(&newsidtab);
1721 policydb_destroy(&newpolicydb);
1722 return rc;
1723
1724}
1725
1726/**
1727 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 * @protocol: protocol number
1729 * @port: port number
1730 * @out_sid: security identifier
1731 */
Paul Moore3e112172008-04-10 10:48:14 -04001732int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
1734 struct ocontext *c;
1735 int rc = 0;
1736
James Morris0804d112008-06-06 18:40:29 +10001737 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 c = policydb.ocontexts[OCON_PORT];
1740 while (c) {
1741 if (c->u.port.protocol == protocol &&
1742 c->u.port.low_port <= port &&
1743 c->u.port.high_port >= port)
1744 break;
1745 c = c->next;
1746 }
1747
1748 if (c) {
1749 if (!c->sid[0]) {
1750 rc = sidtab_context_to_sid(&sidtab,
1751 &c->context[0],
1752 &c->sid[0]);
1753 if (rc)
1754 goto out;
1755 }
1756 *out_sid = c->sid[0];
1757 } else {
1758 *out_sid = SECINITSID_PORT;
1759 }
1760
1761out:
James Morris0804d112008-06-06 18:40:29 +10001762 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return rc;
1764}
1765
1766/**
1767 * security_netif_sid - Obtain the SID for a network interface.
1768 * @name: interface name
1769 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001771int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 int rc = 0;
1774 struct ocontext *c;
1775
James Morris0804d112008-06-06 18:40:29 +10001776 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 c = policydb.ocontexts[OCON_NETIF];
1779 while (c) {
1780 if (strcmp(name, c->u.name) == 0)
1781 break;
1782 c = c->next;
1783 }
1784
1785 if (c) {
1786 if (!c->sid[0] || !c->sid[1]) {
1787 rc = sidtab_context_to_sid(&sidtab,
1788 &c->context[0],
1789 &c->sid[0]);
1790 if (rc)
1791 goto out;
1792 rc = sidtab_context_to_sid(&sidtab,
1793 &c->context[1],
1794 &c->sid[1]);
1795 if (rc)
1796 goto out;
1797 }
1798 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001799 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802out:
James Morris0804d112008-06-06 18:40:29 +10001803 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return rc;
1805}
1806
1807static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1808{
1809 int i, fail = 0;
1810
Eric Paris5d55a342008-04-18 17:38:33 -04001811 for (i = 0; i < 4; i++)
1812 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 fail = 1;
1814 break;
1815 }
1816
1817 return !fail;
1818}
1819
1820/**
1821 * security_node_sid - Obtain the SID for a node (host).
1822 * @domain: communication domain aka address family
1823 * @addrp: address
1824 * @addrlen: address length in bytes
1825 * @out_sid: security identifier
1826 */
1827int security_node_sid(u16 domain,
1828 void *addrp,
1829 u32 addrlen,
1830 u32 *out_sid)
1831{
1832 int rc = 0;
1833 struct ocontext *c;
1834
James Morris0804d112008-06-06 18:40:29 +10001835 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 switch (domain) {
1838 case AF_INET: {
1839 u32 addr;
1840
1841 if (addrlen != sizeof(u32)) {
1842 rc = -EINVAL;
1843 goto out;
1844 }
1845
1846 addr = *((u32 *)addrp);
1847
1848 c = policydb.ocontexts[OCON_NODE];
1849 while (c) {
1850 if (c->u.node.addr == (addr & c->u.node.mask))
1851 break;
1852 c = c->next;
1853 }
1854 break;
1855 }
1856
1857 case AF_INET6:
1858 if (addrlen != sizeof(u64) * 2) {
1859 rc = -EINVAL;
1860 goto out;
1861 }
1862 c = policydb.ocontexts[OCON_NODE6];
1863 while (c) {
1864 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1865 c->u.node6.mask))
1866 break;
1867 c = c->next;
1868 }
1869 break;
1870
1871 default:
1872 *out_sid = SECINITSID_NODE;
1873 goto out;
1874 }
1875
1876 if (c) {
1877 if (!c->sid[0]) {
1878 rc = sidtab_context_to_sid(&sidtab,
1879 &c->context[0],
1880 &c->sid[0]);
1881 if (rc)
1882 goto out;
1883 }
1884 *out_sid = c->sid[0];
1885 } else {
1886 *out_sid = SECINITSID_NODE;
1887 }
1888
1889out:
James Morris0804d112008-06-06 18:40:29 +10001890 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 return rc;
1892}
1893
1894#define SIDS_NEL 25
1895
1896/**
1897 * security_get_user_sids - Obtain reachable SIDs for a user.
1898 * @fromsid: starting SID
1899 * @username: username
1900 * @sids: array of reachable SIDs for user
1901 * @nel: number of elements in @sids
1902 *
1903 * Generate the set of SIDs for legal security contexts
1904 * for a given user that can be reached by @fromsid.
1905 * Set *@sids to point to a dynamically allocated
1906 * array containing the set of SIDs. Set *@nel to the
1907 * number of elements in the array.
1908 */
1909
1910int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04001911 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 u32 **sids,
1913 u32 *nel)
1914{
1915 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001916 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 u32 mynel = 0, maxnel = SIDS_NEL;
1918 struct user_datum *user;
1919 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001920 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 int rc = 0, i, j;
1922
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001923 *sids = NULL;
1924 *nel = 0;
1925
1926 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
James Morris0804d112008-06-06 18:40:29 +10001929 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Stephen Smalley12b29f32008-05-07 13:03:20 -04001931 context_init(&usercon);
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 fromcon = sidtab_search(&sidtab, fromsid);
1934 if (!fromcon) {
1935 rc = -EINVAL;
1936 goto out_unlock;
1937 }
1938
1939 user = hashtab_search(policydb.p_users.table, username);
1940 if (!user) {
1941 rc = -EINVAL;
1942 goto out_unlock;
1943 }
1944 usercon.user = user->value;
1945
James Morris89d155e2005-10-30 14:59:21 -08001946 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (!mysids) {
1948 rc = -ENOMEM;
1949 goto out_unlock;
1950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001952 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 role = policydb.role_val_to_struct[i];
1954 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09001955 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 usercon.type = j+1;
1957
1958 if (mls_setup_user_range(fromcon, user, &usercon))
1959 continue;
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001962 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (mynel < maxnel) {
1965 mysids[mynel++] = sid;
1966 } else {
1967 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08001968 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (!mysids2) {
1970 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto out_unlock;
1972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
1974 kfree(mysids);
1975 mysids = mysids2;
1976 mysids[mynel++] = sid;
1977 }
1978 }
1979 }
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001982 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001983 if (rc || !mynel) {
1984 kfree(mysids);
1985 goto out;
1986 }
1987
1988 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
1989 if (!mysids2) {
1990 rc = -ENOMEM;
1991 kfree(mysids);
1992 goto out;
1993 }
1994 for (i = 0, j = 0; i < mynel; i++) {
1995 rc = avc_has_perm_noaudit(fromsid, mysids[i],
1996 SECCLASS_PROCESS,
1997 PROCESS__TRANSITION, AVC_STRICT,
1998 NULL);
1999 if (!rc)
2000 mysids2[j++] = mysids[i];
2001 cond_resched();
2002 }
2003 rc = 0;
2004 kfree(mysids);
2005 *sids = mysids2;
2006 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007out:
2008 return rc;
2009}
2010
2011/**
2012 * security_genfs_sid - Obtain a SID for a file in a filesystem
2013 * @fstype: filesystem type
2014 * @path: path from root of mount
2015 * @sclass: file security class
2016 * @sid: SID for path
2017 *
2018 * Obtain a SID to use for a file in a filesystem that
2019 * cannot support xattr or use a fixed labeling behavior like
2020 * transition SIDs or task SIDs.
2021 */
2022int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04002023 char *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 u16 sclass,
2025 u32 *sid)
2026{
2027 int len;
2028 struct genfs *genfs;
2029 struct ocontext *c;
2030 int rc = 0, cmp = 0;
2031
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05002032 while (path[0] == '/' && path[1] == '/')
2033 path++;
2034
James Morris0804d112008-06-06 18:40:29 +10002035 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2038 cmp = strcmp(fstype, genfs->fstype);
2039 if (cmp <= 0)
2040 break;
2041 }
2042
2043 if (!genfs || cmp) {
2044 *sid = SECINITSID_UNLABELED;
2045 rc = -ENOENT;
2046 goto out;
2047 }
2048
2049 for (c = genfs->head; c; c = c->next) {
2050 len = strlen(c->u.name);
2051 if ((!c->v.sclass || sclass == c->v.sclass) &&
2052 (strncmp(c->u.name, path, len) == 0))
2053 break;
2054 }
2055
2056 if (!c) {
2057 *sid = SECINITSID_UNLABELED;
2058 rc = -ENOENT;
2059 goto out;
2060 }
2061
2062 if (!c->sid[0]) {
2063 rc = sidtab_context_to_sid(&sidtab,
2064 &c->context[0],
2065 &c->sid[0]);
2066 if (rc)
2067 goto out;
2068 }
2069
2070 *sid = c->sid[0];
2071out:
James Morris0804d112008-06-06 18:40:29 +10002072 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 return rc;
2074}
2075
2076/**
2077 * security_fs_use - Determine how to handle labeling for a filesystem.
2078 * @fstype: filesystem type
2079 * @behavior: labeling behavior
2080 * @sid: SID for filesystem (superblock)
2081 */
2082int security_fs_use(
2083 const char *fstype,
2084 unsigned int *behavior,
James Morris089be432008-07-15 18:32:49 +10002085 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
2087 int rc = 0;
2088 struct ocontext *c;
2089
James Morris0804d112008-06-06 18:40:29 +10002090 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 c = policydb.ocontexts[OCON_FSUSE];
2093 while (c) {
2094 if (strcmp(fstype, c->u.name) == 0)
2095 break;
2096 c = c->next;
2097 }
2098
2099 if (c) {
2100 *behavior = c->v.behavior;
2101 if (!c->sid[0]) {
2102 rc = sidtab_context_to_sid(&sidtab,
2103 &c->context[0],
2104 &c->sid[0]);
2105 if (rc)
2106 goto out;
2107 }
2108 *sid = c->sid[0];
2109 } else {
James Morris089be432008-07-15 18:32:49 +10002110 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2111 if (rc) {
2112 *behavior = SECURITY_FS_USE_NONE;
2113 rc = 0;
2114 } else {
2115 *behavior = SECURITY_FS_USE_GENFS;
2116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
2118
2119out:
James Morris0804d112008-06-06 18:40:29 +10002120 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 return rc;
2122}
2123
2124int security_get_bools(int *len, char ***names, int **values)
2125{
2126 int i, rc = -ENOMEM;
2127
James Morris0804d112008-06-06 18:40:29 +10002128 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 *names = NULL;
2130 *values = NULL;
2131
2132 *len = policydb.p_bools.nprim;
2133 if (!*len) {
2134 rc = 0;
2135 goto out;
2136 }
2137
Eric Paris5d55a342008-04-18 17:38:33 -04002138 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (!*names)
2140 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
Jesper Juhle0795cf2006-01-09 20:54:46 -08002142 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (!*values)
2144 goto err;
2145
2146 for (i = 0; i < *len; i++) {
2147 size_t name_len;
2148 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2149 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002150 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (!(*names)[i])
2152 goto err;
2153 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2154 (*names)[i][name_len - 1] = 0;
2155 }
2156 rc = 0;
2157out:
James Morris0804d112008-06-06 18:40:29 +10002158 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return rc;
2160err:
2161 if (*names) {
2162 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002163 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002165 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto out;
2167}
2168
2169
2170int security_set_bools(int len, int *values)
2171{
2172 int i, rc = 0;
2173 int lenp, seqno = 0;
2174 struct cond_node *cur;
2175
James Morris0804d112008-06-06 18:40:29 +10002176 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 lenp = policydb.p_bools.nprim;
2179 if (len != lenp) {
2180 rc = -EFAULT;
2181 goto out;
2182 }
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002185 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2186 audit_log(current->audit_context, GFP_ATOMIC,
2187 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002188 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002189 policydb.p_bool_val_to_name[i],
2190 !!values[i],
2191 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002192 audit_get_loginuid(current),
2193 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002194 }
Eric Paris5d55a342008-04-18 17:38:33 -04002195 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002197 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002201 for (cur = policydb.cond_list; cur; cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 rc = evaluate_cond_node(&policydb, cur);
2203 if (rc)
2204 goto out;
2205 }
2206
2207 seqno = ++latest_granting;
2208
2209out:
James Morris0804d112008-06-06 18:40:29 +10002210 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if (!rc) {
2212 avc_ss_reset(seqno);
2213 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002214 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 }
2216 return rc;
2217}
2218
2219int security_get_bool_value(int bool)
2220{
2221 int rc = 0;
2222 int len;
2223
James Morris0804d112008-06-06 18:40:29 +10002224 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 len = policydb.p_bools.nprim;
2227 if (bool >= len) {
2228 rc = -EFAULT;
2229 goto out;
2230 }
2231
2232 rc = policydb.bool_val_to_struct[bool]->state;
2233out:
James Morris0804d112008-06-06 18:40:29 +10002234 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 return rc;
2236}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002237
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002238static int security_preserve_bools(struct policydb *p)
2239{
2240 int rc, nbools = 0, *bvalues = NULL, i;
2241 char **bnames = NULL;
2242 struct cond_bool_datum *booldatum;
2243 struct cond_node *cur;
2244
2245 rc = security_get_bools(&nbools, &bnames, &bvalues);
2246 if (rc)
2247 goto out;
2248 for (i = 0; i < nbools; i++) {
2249 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2250 if (booldatum)
2251 booldatum->state = bvalues[i];
2252 }
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002253 for (cur = p->cond_list; cur; cur = cur->next) {
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002254 rc = evaluate_cond_node(p, cur);
2255 if (rc)
2256 goto out;
2257 }
2258
2259out:
2260 if (bnames) {
2261 for (i = 0; i < nbools; i++)
2262 kfree(bnames[i]);
2263 }
2264 kfree(bnames);
2265 kfree(bvalues);
2266 return rc;
2267}
2268
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002269/*
2270 * security_sid_mls_copy() - computes a new sid based on the given
2271 * sid and the mls portion of mls_sid.
2272 */
2273int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2274{
2275 struct context *context1;
2276 struct context *context2;
2277 struct context newcon;
2278 char *s;
2279 u32 len;
2280 int rc = 0;
2281
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002282 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002283 *new_sid = sid;
2284 goto out;
2285 }
2286
2287 context_init(&newcon);
2288
James Morris0804d112008-06-06 18:40:29 +10002289 read_lock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002290 context1 = sidtab_search(&sidtab, sid);
2291 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002292 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2293 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002294 rc = -EINVAL;
2295 goto out_unlock;
2296 }
2297
2298 context2 = sidtab_search(&sidtab, mls_sid);
2299 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002300 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2301 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002302 rc = -EINVAL;
2303 goto out_unlock;
2304 }
2305
2306 newcon.user = context1->user;
2307 newcon.role = context1->role;
2308 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002309 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002310 if (rc)
2311 goto out_unlock;
2312
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002313 /* Check the validity of the new context. */
2314 if (!policydb_context_isvalid(&policydb, &newcon)) {
2315 rc = convert_context_handle_invalid_context(&newcon);
2316 if (rc)
2317 goto bad;
2318 }
2319
2320 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2321 goto out_unlock;
2322
2323bad:
2324 if (!context_struct_to_string(&newcon, &s, &len)) {
2325 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2326 "security_sid_mls_copy: invalid context %s", s);
2327 kfree(s);
2328 }
2329
2330out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002331 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002332 context_destroy(&newcon);
2333out:
2334 return rc;
2335}
2336
Paul Moore220deb92008-01-29 08:38:23 -05002337/**
2338 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2339 * @nlbl_sid: NetLabel SID
2340 * @nlbl_type: NetLabel labeling protocol type
2341 * @xfrm_sid: XFRM SID
2342 *
2343 * Description:
2344 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2345 * resolved into a single SID it is returned via @peer_sid and the function
2346 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2347 * returns a negative value. A table summarizing the behavior is below:
2348 *
2349 * | function return | @sid
2350 * ------------------------------+-----------------+-----------------
2351 * no peer labels | 0 | SECSID_NULL
2352 * single peer label | 0 | <peer_label>
2353 * multiple, consistent labels | 0 | <peer_label>
2354 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2355 *
2356 */
2357int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2358 u32 xfrm_sid,
2359 u32 *peer_sid)
2360{
2361 int rc;
2362 struct context *nlbl_ctx;
2363 struct context *xfrm_ctx;
2364
2365 /* handle the common (which also happens to be the set of easy) cases
2366 * right away, these two if statements catch everything involving a
2367 * single or absent peer SID/label */
2368 if (xfrm_sid == SECSID_NULL) {
2369 *peer_sid = nlbl_sid;
2370 return 0;
2371 }
2372 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2373 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2374 * is present */
2375 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2376 *peer_sid = xfrm_sid;
2377 return 0;
2378 }
2379
2380 /* we don't need to check ss_initialized here since the only way both
2381 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2382 * security server was initialized and ss_initialized was true */
2383 if (!selinux_mls_enabled) {
2384 *peer_sid = SECSID_NULL;
2385 return 0;
2386 }
2387
James Morris0804d112008-06-06 18:40:29 +10002388 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002389
2390 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2391 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002392 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2393 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002394 rc = -EINVAL;
2395 goto out_slowpath;
2396 }
2397 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2398 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002399 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2400 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002401 rc = -EINVAL;
2402 goto out_slowpath;
2403 }
2404 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2405
2406out_slowpath:
James Morris0804d112008-06-06 18:40:29 +10002407 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002408 if (rc == 0)
2409 /* at present NetLabel SIDs/labels really only carry MLS
2410 * information so if the MLS portion of the NetLabel SID
2411 * matches the MLS portion of the labeled XFRM SID/label
2412 * then pass along the XFRM SID as it is the most
2413 * expressive */
2414 *peer_sid = xfrm_sid;
2415 else
2416 *peer_sid = SECSID_NULL;
2417 return rc;
2418}
2419
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002420static int get_classes_callback(void *k, void *d, void *args)
2421{
2422 struct class_datum *datum = d;
2423 char *name = k, **classes = args;
2424 int value = datum->value - 1;
2425
2426 classes[value] = kstrdup(name, GFP_ATOMIC);
2427 if (!classes[value])
2428 return -ENOMEM;
2429
2430 return 0;
2431}
2432
2433int security_get_classes(char ***classes, int *nclasses)
2434{
2435 int rc = -ENOMEM;
2436
James Morris0804d112008-06-06 18:40:29 +10002437 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002438
2439 *nclasses = policydb.p_classes.nprim;
2440 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2441 if (!*classes)
2442 goto out;
2443
2444 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2445 *classes);
2446 if (rc < 0) {
2447 int i;
2448 for (i = 0; i < *nclasses; i++)
2449 kfree((*classes)[i]);
2450 kfree(*classes);
2451 }
2452
2453out:
James Morris0804d112008-06-06 18:40:29 +10002454 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002455 return rc;
2456}
2457
2458static int get_permissions_callback(void *k, void *d, void *args)
2459{
2460 struct perm_datum *datum = d;
2461 char *name = k, **perms = args;
2462 int value = datum->value - 1;
2463
2464 perms[value] = kstrdup(name, GFP_ATOMIC);
2465 if (!perms[value])
2466 return -ENOMEM;
2467
2468 return 0;
2469}
2470
2471int security_get_permissions(char *class, char ***perms, int *nperms)
2472{
2473 int rc = -ENOMEM, i;
2474 struct class_datum *match;
2475
James Morris0804d112008-06-06 18:40:29 +10002476 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002477
2478 match = hashtab_search(policydb.p_classes.table, class);
2479 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002480 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002481 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002482 rc = -EINVAL;
2483 goto out;
2484 }
2485
2486 *nperms = match->permissions.nprim;
2487 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2488 if (!*perms)
2489 goto out;
2490
2491 if (match->comdatum) {
2492 rc = hashtab_map(match->comdatum->permissions.table,
2493 get_permissions_callback, *perms);
2494 if (rc < 0)
2495 goto err;
2496 }
2497
2498 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2499 *perms);
2500 if (rc < 0)
2501 goto err;
2502
2503out:
James Morris0804d112008-06-06 18:40:29 +10002504 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002505 return rc;
2506
2507err:
James Morris0804d112008-06-06 18:40:29 +10002508 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002509 for (i = 0; i < *nperms; i++)
2510 kfree((*perms)[i]);
2511 kfree(*perms);
2512 return rc;
2513}
2514
Eric Paris3f120702007-09-21 14:37:10 -04002515int security_get_reject_unknown(void)
2516{
2517 return policydb.reject_unknown;
2518}
2519
2520int security_get_allow_unknown(void)
2521{
2522 return policydb.allow_unknown;
2523}
2524
Paul Moore3bb56b22008-01-29 08:38:19 -05002525/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002526 * security_policycap_supported - Check for a specific policy capability
2527 * @req_cap: capability
2528 *
2529 * Description:
2530 * This function queries the currently loaded policy to see if it supports the
2531 * capability specified by @req_cap. Returns true (1) if the capability is
2532 * supported, false (0) if it isn't supported.
2533 *
2534 */
2535int security_policycap_supported(unsigned int req_cap)
2536{
2537 int rc;
2538
James Morris0804d112008-06-06 18:40:29 +10002539 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002540 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10002541 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002542
2543 return rc;
2544}
2545
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002546struct selinux_audit_rule {
2547 u32 au_seqno;
2548 struct context au_ctxt;
2549};
2550
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002551void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002552{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002553 struct selinux_audit_rule *rule = vrule;
2554
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002555 if (rule) {
2556 context_destroy(&rule->au_ctxt);
2557 kfree(rule);
2558 }
2559}
2560
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002561int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002562{
2563 struct selinux_audit_rule *tmprule;
2564 struct role_datum *roledatum;
2565 struct type_datum *typedatum;
2566 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002567 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002568 int rc = 0;
2569
2570 *rule = NULL;
2571
2572 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002573 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002574
2575 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002576 case AUDIT_SUBJ_USER:
2577 case AUDIT_SUBJ_ROLE:
2578 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002579 case AUDIT_OBJ_USER:
2580 case AUDIT_OBJ_ROLE:
2581 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002582 /* only 'equals' and 'not equals' fit user, role, and type */
Al Viro5af75d82008-12-16 05:59:26 -05002583 if (op != Audit_equal && op != Audit_not_equal)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002584 return -EINVAL;
2585 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002586 case AUDIT_SUBJ_SEN:
2587 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002588 case AUDIT_OBJ_LEV_LOW:
2589 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002590 /* we do not allow a range, indicated by the presense of '-' */
2591 if (strchr(rulestr, '-'))
2592 return -EINVAL;
2593 break;
2594 default:
2595 /* only the above fields are valid */
2596 return -EINVAL;
2597 }
2598
2599 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2600 if (!tmprule)
2601 return -ENOMEM;
2602
2603 context_init(&tmprule->au_ctxt);
2604
James Morris0804d112008-06-06 18:40:29 +10002605 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002606
2607 tmprule->au_seqno = latest_granting;
2608
2609 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002610 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002611 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002612 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2613 if (!userdatum)
2614 rc = -EINVAL;
2615 else
2616 tmprule->au_ctxt.user = userdatum->value;
2617 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002618 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002619 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002620 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2621 if (!roledatum)
2622 rc = -EINVAL;
2623 else
2624 tmprule->au_ctxt.role = roledatum->value;
2625 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002626 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002627 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002628 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2629 if (!typedatum)
2630 rc = -EINVAL;
2631 else
2632 tmprule->au_ctxt.type = typedatum->value;
2633 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002634 case AUDIT_SUBJ_SEN:
2635 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002636 case AUDIT_OBJ_LEV_LOW:
2637 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002638 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2639 break;
2640 }
2641
James Morris0804d112008-06-06 18:40:29 +10002642 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002643
2644 if (rc) {
2645 selinux_audit_rule_free(tmprule);
2646 tmprule = NULL;
2647 }
2648
2649 *rule = tmprule;
2650
2651 return rc;
2652}
2653
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002654/* Check to see if the rule contains any selinux fields */
2655int selinux_audit_rule_known(struct audit_krule *rule)
2656{
2657 int i;
2658
2659 for (i = 0; i < rule->field_count; i++) {
2660 struct audit_field *f = &rule->fields[i];
2661 switch (f->type) {
2662 case AUDIT_SUBJ_USER:
2663 case AUDIT_SUBJ_ROLE:
2664 case AUDIT_SUBJ_TYPE:
2665 case AUDIT_SUBJ_SEN:
2666 case AUDIT_SUBJ_CLR:
2667 case AUDIT_OBJ_USER:
2668 case AUDIT_OBJ_ROLE:
2669 case AUDIT_OBJ_TYPE:
2670 case AUDIT_OBJ_LEV_LOW:
2671 case AUDIT_OBJ_LEV_HIGH:
2672 return 1;
2673 }
2674 }
2675
2676 return 0;
2677}
2678
2679int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04002680 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002681{
2682 struct context *ctxt;
2683 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002684 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002685 int match = 0;
2686
2687 if (!rule) {
2688 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002689 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002690 return -ENOENT;
2691 }
2692
James Morris0804d112008-06-06 18:40:29 +10002693 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002694
2695 if (rule->au_seqno < latest_granting) {
2696 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002697 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002698 match = -ESTALE;
2699 goto out;
2700 }
2701
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002702 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002703 if (!ctxt) {
2704 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002705 "selinux_audit_rule_match: unrecognized SID %d\n",
2706 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002707 match = -ENOENT;
2708 goto out;
2709 }
2710
2711 /* a field/op pair that is not caught here will simply fall through
2712 without a match */
2713 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002714 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002715 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002716 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002717 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002718 match = (ctxt->user == rule->au_ctxt.user);
2719 break;
Al Viro5af75d82008-12-16 05:59:26 -05002720 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002721 match = (ctxt->user != rule->au_ctxt.user);
2722 break;
2723 }
2724 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002725 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002726 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002727 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002728 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002729 match = (ctxt->role == rule->au_ctxt.role);
2730 break;
Al Viro5af75d82008-12-16 05:59:26 -05002731 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002732 match = (ctxt->role != rule->au_ctxt.role);
2733 break;
2734 }
2735 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002736 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002737 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002738 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002739 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002740 match = (ctxt->type == rule->au_ctxt.type);
2741 break;
Al Viro5af75d82008-12-16 05:59:26 -05002742 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002743 match = (ctxt->type != rule->au_ctxt.type);
2744 break;
2745 }
2746 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002747 case AUDIT_SUBJ_SEN:
2748 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002749 case AUDIT_OBJ_LEV_LOW:
2750 case AUDIT_OBJ_LEV_HIGH:
2751 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002752 field == AUDIT_OBJ_LEV_LOW) ?
2753 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002754 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002755 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002756 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002757 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002758 break;
Al Viro5af75d82008-12-16 05:59:26 -05002759 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002760 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002761 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002762 break;
Al Viro5af75d82008-12-16 05:59:26 -05002763 case Audit_lt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002764 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002765 level) &&
2766 !mls_level_eq(&rule->au_ctxt.range.level[0],
2767 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002768 break;
Al Viro5af75d82008-12-16 05:59:26 -05002769 case Audit_le:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002770 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002771 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002772 break;
Al Viro5af75d82008-12-16 05:59:26 -05002773 case Audit_gt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002774 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002775 &rule->au_ctxt.range.level[0]) &&
2776 !mls_level_eq(level,
2777 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002778 break;
Al Viro5af75d82008-12-16 05:59:26 -05002779 case Audit_ge:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002780 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002781 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002782 break;
2783 }
2784 }
2785
2786out:
James Morris0804d112008-06-06 18:40:29 +10002787 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002788 return match;
2789}
2790
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002791static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002792
2793static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
Eric Parisf5269712008-05-14 11:27:45 -04002794 u16 class, u32 perms, u32 *retained)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002795{
2796 int err = 0;
2797
2798 if (event == AVC_CALLBACK_RESET && aurule_callback)
2799 err = aurule_callback();
2800 return err;
2801}
2802
2803static int __init aurule_init(void)
2804{
2805 int err;
2806
2807 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002808 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002809 if (err)
2810 panic("avc_add_callback() failed, error %d\n", err);
2811
2812 return err;
2813}
2814__initcall(aurule_init);
2815
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002816#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002817/**
Paul Moore5778eab2007-02-28 15:14:22 -05002818 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2819 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002820 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002821 *
2822 * Description:
2823 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002824 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2825 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002826 *
2827 */
Paul Moore5778eab2007-02-28 15:14:22 -05002828static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002829 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002830{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002831 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002832
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002833 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2834 if (sid_cache == NULL)
2835 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002836 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002837 if (secattr->cache == NULL) {
2838 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002839 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002840 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002841
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002842 *sid_cache = sid;
2843 secattr->cache->free = kfree;
2844 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002845 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002846}
2847
2848/**
Paul Moore5778eab2007-02-28 15:14:22 -05002849 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002850 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002851 * @sid: the SELinux SID
2852 *
2853 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002854 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002855 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002856 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2857 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2858 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2859 * conversion for future lookups. Returns zero on success, negative values on
2860 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002861 *
2862 */
Paul Moore5778eab2007-02-28 15:14:22 -05002863int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002864 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002865{
2866 int rc = -EIDRM;
2867 struct context *ctx;
2868 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002869
2870 if (!ss_initialized) {
2871 *sid = SECSID_NULL;
2872 return 0;
2873 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002874
James Morris0804d112008-06-06 18:40:29 +10002875 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002876
Paul Moore701a90b2006-11-17 17:38:46 -05002877 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002878 *sid = *(u32 *)secattr->cache->data;
2879 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002880 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2881 *sid = secattr->attr.secid;
2882 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05002883 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002884 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002885 if (ctx == NULL)
2886 goto netlbl_secattr_to_sid_return;
2887
Paul Moore81990fb2008-10-03 10:51:15 -04002888 context_init(&ctx_new);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002889 ctx_new.user = ctx->user;
2890 ctx_new.role = ctx->role;
2891 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002892 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002893 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002894 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05002895 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002896 goto netlbl_secattr_to_sid_return;
Paul Moore81990fb2008-10-03 10:51:15 -04002897 memcpy(&ctx_new.range.level[1].cat,
2898 &ctx_new.range.level[0].cat,
2899 sizeof(ctx_new.range.level[0].cat));
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002900 }
2901 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2902 goto netlbl_secattr_to_sid_return_cleanup;
2903
2904 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2905 if (rc != 0)
2906 goto netlbl_secattr_to_sid_return_cleanup;
2907
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002908 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05002909
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002910 ebitmap_destroy(&ctx_new.range.level[0].cat);
2911 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002912 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002913 rc = 0;
2914 }
2915
2916netlbl_secattr_to_sid_return:
James Morris0804d112008-06-06 18:40:29 +10002917 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002918 return rc;
2919netlbl_secattr_to_sid_return_cleanup:
2920 ebitmap_destroy(&ctx_new.range.level[0].cat);
2921 goto netlbl_secattr_to_sid_return;
2922}
2923
2924/**
Paul Moore5778eab2007-02-28 15:14:22 -05002925 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2926 * @sid: the SELinux SID
2927 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002928 *
2929 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002930 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2931 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002932 *
2933 */
Paul Moore5778eab2007-02-28 15:14:22 -05002934int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002935{
Paul Moore99d854d2008-10-10 10:16:30 -04002936 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002937 struct context *ctx;
2938
2939 if (!ss_initialized)
2940 return 0;
2941
James Morris0804d112008-06-06 18:40:29 +10002942 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002943 ctx = sidtab_search(&sidtab, sid);
Paul Moore99d854d2008-10-10 10:16:30 -04002944 if (ctx == NULL) {
2945 rc = -ENOENT;
Paul Moore5778eab2007-02-28 15:14:22 -05002946 goto netlbl_sid_to_secattr_failure;
Paul Moore99d854d2008-10-10 10:16:30 -04002947 }
Paul Moore5778eab2007-02-28 15:14:22 -05002948 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
2949 GFP_ATOMIC);
Paul Moore99d854d2008-10-10 10:16:30 -04002950 if (secattr->domain == NULL) {
2951 rc = -ENOMEM;
2952 goto netlbl_sid_to_secattr_failure;
2953 }
Paul Moore8d758992008-10-10 10:16:33 -04002954 secattr->attr.secid = sid;
2955 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
Paul Moore5778eab2007-02-28 15:14:22 -05002956 mls_export_netlbl_lvl(ctx, secattr);
2957 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04002958 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05002959 goto netlbl_sid_to_secattr_failure;
James Morris0804d112008-06-06 18:40:29 +10002960 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002961
Paul Moore5778eab2007-02-28 15:14:22 -05002962 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05002963
Paul Moore5778eab2007-02-28 15:14:22 -05002964netlbl_sid_to_secattr_failure:
James Morris0804d112008-06-06 18:40:29 +10002965 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08002966 return rc;
2967}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002968#endif /* CONFIG_NETLABEL */