blob: a90cab207d9aea5bca459832f61cbbd2e54dde57 [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 *
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +090025 * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com>
26 *
27 * Added support for bounds domain and audit messaged on masked permissions
28 *
29 * Copyright (C) 2008, 2009 NEC Corporation
Paul Moore3bb56b22008-01-29 08:38:19 -050030 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
Darrel Goeddel376bd9c2006-02-24 15:44:05 -060031 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
Chad Sellersb94c7e62006-11-06 12:38:18 -050032 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
34 * This program is free software; you can redistribute it and/or modify
Eric Paris5d55a342008-04-18 17:38:33 -040035 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * the Free Software Foundation, version 2.
37 */
38#include <linux/kernel.h>
39#include <linux/slab.h>
40#include <linux/string.h>
41#include <linux/spinlock.h>
Paul Moore9f2ad662006-11-17 17:38:53 -050042#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/errno.h>
44#include <linux/in.h>
45#include <linux/sched.h>
46#include <linux/audit.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080047#include <linux/mutex.h>
Adrian Bunk0e55a002008-03-31 01:54:02 +030048#include <linux/selinux.h>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070049#include <net/netlabel.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "flask.h"
52#include "avc.h"
53#include "avc_ss.h"
54#include "security.h"
55#include "context.h"
56#include "policydb.h"
57#include "sidtab.h"
58#include "services.h"
59#include "conditional.h"
60#include "mls.h"
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070061#include "objsec.h"
Paul Moorec60475b2007-02-28 15:14:23 -050062#include "netlabel.h"
Paul Moore3de4bab2006-11-17 17:38:54 -050063#include "xfrm.h"
Paul Moore02752762006-11-29 13:18:18 -050064#include "ebitmap.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020065#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67extern void selnl_notify_policyload(u32 seqno);
68unsigned int policydb_loaded_version;
69
Paul Moore3bb56b22008-01-29 08:38:19 -050070int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050071int selinux_policycap_openperm;
Paul Moore3bb56b22008-01-29 08:38:19 -050072
Chad Sellersb94c7e62006-11-06 12:38:18 -050073/*
74 * This is declared in avc.c
75 */
76extern const struct selinux_class_perm selinux_class_perm;
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static DEFINE_RWLOCK(policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static struct sidtab sidtab;
81struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040082int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * The largest sequence number that has been used when
86 * providing an access decision to the access vector cache.
87 * The sequence number only changes when a policy change
88 * occurs.
89 */
Eric Paris5d55a342008-04-18 17:38:33 -040090static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* Forward declaration. */
93static int context_struct_to_string(struct context *context, char **scontext,
94 u32 *scontext_len);
95
KaiGai Koheid9250de2008-08-28 16:35:57 +090096static int context_struct_compute_av(struct context *scontext,
97 struct context *tcontext,
98 u16 tclass,
99 u32 requested,
100 struct av_decision *avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101/*
102 * Return the boolean value of a constraint expression
103 * when it is applied to the specified source and target
104 * security contexts.
105 *
106 * xcontext is a special beast... It is used by the validatetrans rules
107 * only. For these rules, scontext is the context before the transition,
108 * tcontext is the context after the transition, and xcontext is the context
109 * of the process performing the transition. All other callers of
110 * constraint_expr_eval should pass in NULL for xcontext.
111 */
112static int constraint_expr_eval(struct context *scontext,
113 struct context *tcontext,
114 struct context *xcontext,
115 struct constraint_expr *cexpr)
116{
117 u32 val1, val2;
118 struct context *c;
119 struct role_datum *r1, *r2;
120 struct mls_level *l1, *l2;
121 struct constraint_expr *e;
122 int s[CEXPR_MAXDEPTH];
123 int sp = -1;
124
125 for (e = cexpr; e; e = e->next) {
126 switch (e->expr_type) {
127 case CEXPR_NOT:
128 BUG_ON(sp < 0);
129 s[sp] = !s[sp];
130 break;
131 case CEXPR_AND:
132 BUG_ON(sp < 1);
133 sp--;
134 s[sp] &= s[sp+1];
135 break;
136 case CEXPR_OR:
137 BUG_ON(sp < 1);
138 sp--;
139 s[sp] |= s[sp+1];
140 break;
141 case CEXPR_ATTR:
142 if (sp == (CEXPR_MAXDEPTH-1))
143 return 0;
144 switch (e->attr) {
145 case CEXPR_USER:
146 val1 = scontext->user;
147 val2 = tcontext->user;
148 break;
149 case CEXPR_TYPE:
150 val1 = scontext->type;
151 val2 = tcontext->type;
152 break;
153 case CEXPR_ROLE:
154 val1 = scontext->role;
155 val2 = tcontext->role;
156 r1 = policydb.role_val_to_struct[val1 - 1];
157 r2 = policydb.role_val_to_struct[val2 - 1];
158 switch (e->op) {
159 case CEXPR_DOM:
160 s[++sp] = ebitmap_get_bit(&r1->dominates,
161 val2 - 1);
162 continue;
163 case CEXPR_DOMBY:
164 s[++sp] = ebitmap_get_bit(&r2->dominates,
165 val1 - 1);
166 continue;
167 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400168 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
169 val2 - 1) &&
170 !ebitmap_get_bit(&r2->dominates,
171 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 continue;
173 default:
174 break;
175 }
176 break;
177 case CEXPR_L1L2:
178 l1 = &(scontext->range.level[0]);
179 l2 = &(tcontext->range.level[0]);
180 goto mls_ops;
181 case CEXPR_L1H2:
182 l1 = &(scontext->range.level[0]);
183 l2 = &(tcontext->range.level[1]);
184 goto mls_ops;
185 case CEXPR_H1L2:
186 l1 = &(scontext->range.level[1]);
187 l2 = &(tcontext->range.level[0]);
188 goto mls_ops;
189 case CEXPR_H1H2:
190 l1 = &(scontext->range.level[1]);
191 l2 = &(tcontext->range.level[1]);
192 goto mls_ops;
193 case CEXPR_L1H1:
194 l1 = &(scontext->range.level[0]);
195 l2 = &(scontext->range.level[1]);
196 goto mls_ops;
197 case CEXPR_L2H2:
198 l1 = &(tcontext->range.level[0]);
199 l2 = &(tcontext->range.level[1]);
200 goto mls_ops;
201mls_ops:
202 switch (e->op) {
203 case CEXPR_EQ:
204 s[++sp] = mls_level_eq(l1, l2);
205 continue;
206 case CEXPR_NEQ:
207 s[++sp] = !mls_level_eq(l1, l2);
208 continue;
209 case CEXPR_DOM:
210 s[++sp] = mls_level_dom(l1, l2);
211 continue;
212 case CEXPR_DOMBY:
213 s[++sp] = mls_level_dom(l2, l1);
214 continue;
215 case CEXPR_INCOMP:
216 s[++sp] = mls_level_incomp(l2, l1);
217 continue;
218 default:
219 BUG();
220 return 0;
221 }
222 break;
223 default:
224 BUG();
225 return 0;
226 }
227
228 switch (e->op) {
229 case CEXPR_EQ:
230 s[++sp] = (val1 == val2);
231 break;
232 case CEXPR_NEQ:
233 s[++sp] = (val1 != val2);
234 break;
235 default:
236 BUG();
237 return 0;
238 }
239 break;
240 case CEXPR_NAMES:
241 if (sp == (CEXPR_MAXDEPTH-1))
242 return 0;
243 c = scontext;
244 if (e->attr & CEXPR_TARGET)
245 c = tcontext;
246 else if (e->attr & CEXPR_XTARGET) {
247 c = xcontext;
248 if (!c) {
249 BUG();
250 return 0;
251 }
252 }
253 if (e->attr & CEXPR_USER)
254 val1 = c->user;
255 else if (e->attr & CEXPR_ROLE)
256 val1 = c->role;
257 else if (e->attr & CEXPR_TYPE)
258 val1 = c->type;
259 else {
260 BUG();
261 return 0;
262 }
263
264 switch (e->op) {
265 case CEXPR_EQ:
266 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
267 break;
268 case CEXPR_NEQ:
269 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
270 break;
271 default:
272 BUG();
273 return 0;
274 }
275 break;
276 default:
277 BUG();
278 return 0;
279 }
280 }
281
282 BUG_ON(sp != 0);
283 return s[0];
284}
285
286/*
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900287 * security_dump_masked_av - dumps masked permissions during
288 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
289 */
290static int dump_masked_av_helper(void *k, void *d, void *args)
291{
292 struct perm_datum *pdatum = d;
293 char **permission_names = args;
294
295 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
296
297 permission_names[pdatum->value - 1] = (char *)k;
298
299 return 0;
300}
301
302static void security_dump_masked_av(struct context *scontext,
303 struct context *tcontext,
304 u16 tclass,
305 u32 permissions,
306 const char *reason)
307{
308 struct common_datum *common_dat;
309 struct class_datum *tclass_dat;
310 struct audit_buffer *ab;
311 char *tclass_name;
312 char *scontext_name = NULL;
313 char *tcontext_name = NULL;
314 char *permission_names[32];
315 int index, length;
316 bool need_comma = false;
317
318 if (!permissions)
319 return;
320
321 tclass_name = policydb.p_class_val_to_name[tclass - 1];
322 tclass_dat = policydb.class_val_to_struct[tclass - 1];
323 common_dat = tclass_dat->comdatum;
324
325 /* init permission_names */
326 if (common_dat &&
327 hashtab_map(common_dat->permissions.table,
328 dump_masked_av_helper, permission_names) < 0)
329 goto out;
330
331 if (hashtab_map(tclass_dat->permissions.table,
332 dump_masked_av_helper, permission_names) < 0)
333 goto out;
334
335 /* get scontext/tcontext in text form */
336 if (context_struct_to_string(scontext,
337 &scontext_name, &length) < 0)
338 goto out;
339
340 if (context_struct_to_string(tcontext,
341 &tcontext_name, &length) < 0)
342 goto out;
343
344 /* audit a message */
345 ab = audit_log_start(current->audit_context,
346 GFP_ATOMIC, AUDIT_SELINUX_ERR);
347 if (!ab)
348 goto out;
349
350 audit_log_format(ab, "op=security_compute_av reason=%s "
351 "scontext=%s tcontext=%s tclass=%s perms=",
352 reason, scontext_name, tcontext_name, tclass_name);
353
354 for (index = 0; index < 32; index++) {
355 u32 mask = (1 << index);
356
357 if ((mask & permissions) == 0)
358 continue;
359
360 audit_log_format(ab, "%s%s",
361 need_comma ? "," : "",
362 permission_names[index]
363 ? permission_names[index] : "????");
364 need_comma = true;
365 }
366 audit_log_end(ab);
367out:
368 /* release scontext/tcontext */
369 kfree(tcontext_name);
370 kfree(scontext_name);
371
372 return;
373}
374
375/*
KaiGai Koheid9250de2008-08-28 16:35:57 +0900376 * security_boundary_permission - drops violated permissions
377 * on boundary constraint.
378 */
379static void type_attribute_bounds_av(struct context *scontext,
380 struct context *tcontext,
381 u16 tclass,
382 u32 requested,
383 struct av_decision *avd)
384{
385 struct context lo_scontext;
386 struct context lo_tcontext;
387 struct av_decision lo_avd;
388 struct type_datum *source
389 = policydb.type_val_to_struct[scontext->type - 1];
390 struct type_datum *target
391 = policydb.type_val_to_struct[tcontext->type - 1];
392 u32 masked = 0;
393
394 if (source->bounds) {
395 memset(&lo_avd, 0, sizeof(lo_avd));
396
397 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
398 lo_scontext.type = source->bounds;
399
400 context_struct_compute_av(&lo_scontext,
401 tcontext,
402 tclass,
403 requested,
404 &lo_avd);
405 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
406 return; /* no masked permission */
407 masked = ~lo_avd.allowed & avd->allowed;
408 }
409
410 if (target->bounds) {
411 memset(&lo_avd, 0, sizeof(lo_avd));
412
413 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
414 lo_tcontext.type = target->bounds;
415
416 context_struct_compute_av(scontext,
417 &lo_tcontext,
418 tclass,
419 requested,
420 &lo_avd);
421 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
422 return; /* no masked permission */
423 masked = ~lo_avd.allowed & avd->allowed;
424 }
425
426 if (source->bounds && target->bounds) {
427 memset(&lo_avd, 0, sizeof(lo_avd));
428 /*
429 * lo_scontext and lo_tcontext are already
430 * set up.
431 */
432
433 context_struct_compute_av(&lo_scontext,
434 &lo_tcontext,
435 tclass,
436 requested,
437 &lo_avd);
438 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
439 return; /* no masked permission */
440 masked = ~lo_avd.allowed & avd->allowed;
441 }
442
443 if (masked) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900444 /* mask violated permissions */
445 avd->allowed &= ~masked;
446
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900447 /* audit masked permissions */
448 security_dump_masked_av(scontext, tcontext,
449 tclass, masked, "bounds");
KaiGai Koheid9250de2008-08-28 16:35:57 +0900450 }
451}
452
453/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 * Compute access vectors based on a context structure pair for
455 * the permissions in a particular class.
456 */
457static int context_struct_compute_av(struct context *scontext,
458 struct context *tcontext,
459 u16 tclass,
460 u32 requested,
461 struct av_decision *avd)
462{
463 struct constraint_node *constraint;
464 struct role_allow *ra;
465 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700466 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700468 struct ebitmap *sattr, *tattr;
469 struct ebitmap_node *snode, *tnode;
Eric Paris3f120702007-09-21 14:37:10 -0400470 const struct selinux_class_perm *kdefs = &selinux_class_perm;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700471 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 /*
474 * Remap extended Netlink classes for old policy versions.
475 * Do this here rather than socket_type_to_security_class()
476 * in case a newer policy version is loaded, allowing sockets
477 * to remain in the correct class.
478 */
479 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
480 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
481 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
482 tclass = SECCLASS_NETLINK_SOCKET;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /*
485 * Initialize the access vectors to the default values.
486 */
487 avd->allowed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 avd->auditallow = 0;
489 avd->auditdeny = 0xffffffff;
490 avd->seqno = latest_granting;
491
492 /*
Eric Paris3f120702007-09-21 14:37:10 -0400493 * Check for all the invalid cases.
494 * - tclass 0
495 * - tclass > policy and > kernel
496 * - tclass > policy but is a userspace class
497 * - tclass > policy but we do not allow unknowns
498 */
499 if (unlikely(!tclass))
500 goto inval_class;
501 if (unlikely(tclass > policydb.p_classes.nprim))
502 if (tclass > kdefs->cts_len ||
Eric Pariscea78dc2008-06-09 15:43:12 -0400503 !kdefs->class_to_string[tclass] ||
Eric Paris3f120702007-09-21 14:37:10 -0400504 !policydb.allow_unknown)
505 goto inval_class;
506
507 /*
508 * Kernel class and we allow unknown so pad the allow decision
509 * the pad will be all 1 for unknown classes.
510 */
511 if (tclass <= kdefs->cts_len && policydb.allow_unknown)
512 avd->allowed = policydb.undefined_perms[tclass - 1];
513
514 /*
515 * Not in policy. Since decision is completed (all 1 or all 0) return.
516 */
517 if (unlikely(tclass > policydb.p_classes.nprim))
518 return 0;
519
520 tclass_datum = policydb.class_val_to_struct[tclass - 1];
521
522 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * If a specific type enforcement rule was defined for
524 * this permission check, then use it.
525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700527 avkey.specified = AVTAB_AV;
528 sattr = &policydb.type_attr_map[scontext->type - 1];
529 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900530 ebitmap_for_each_positive_bit(sattr, snode, i) {
531 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700532 avkey.source_type = i + 1;
533 avkey.target_type = j + 1;
534 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300535 node;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700536 node = avtab_search_node_next(node, avkey.specified)) {
537 if (node->key.specified == AVTAB_ALLOWED)
538 avd->allowed |= node->datum.data;
539 else if (node->key.specified == AVTAB_AUDITALLOW)
540 avd->auditallow |= node->datum.data;
541 else if (node->key.specified == AVTAB_AUDITDENY)
542 avd->auditdeny &= node->datum.data;
543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Stephen Smalley782ebb92005-09-03 15:55:16 -0700545 /* Check conditional av table for additional permissions */
546 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
547
548 }
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 /*
552 * Remove any permissions prohibited by a constraint (this includes
553 * the MLS policy).
554 */
555 constraint = tclass_datum->constraints;
556 while (constraint) {
557 if ((constraint->permissions & (avd->allowed)) &&
558 !constraint_expr_eval(scontext, tcontext, NULL,
559 constraint->expr)) {
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900560 avd->allowed &= ~(constraint->permissions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562 constraint = constraint->next;
563 }
564
565 /*
566 * If checking process transition permission and the
567 * role is changing, then check the (current_role, new_role)
568 * pair.
569 */
570 if (tclass == SECCLASS_PROCESS &&
571 (avd->allowed & (PROCESS__TRANSITION | PROCESS__DYNTRANSITION)) &&
572 scontext->role != tcontext->role) {
573 for (ra = policydb.role_allow; ra; ra = ra->next) {
574 if (scontext->role == ra->role &&
575 tcontext->role == ra->new_role)
576 break;
577 }
578 if (!ra)
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900579 avd->allowed &= ~(PROCESS__TRANSITION |
580 PROCESS__DYNTRANSITION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
KaiGai Koheid9250de2008-08-28 16:35:57 +0900583 /*
584 * If the given source and target types have boundary
585 * constraint, lazy checks have to mask any violated
586 * permission and notice it to userspace via audit.
587 */
588 type_attribute_bounds_av(scontext, tcontext,
589 tclass, requested, avd);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 return 0;
Eric Paris3f120702007-09-21 14:37:10 -0400592
593inval_class:
Stephen Smalley22df4ad2008-06-09 16:03:56 -0400594 if (!tclass || tclass > kdefs->cts_len ||
595 !kdefs->class_to_string[tclass]) {
596 if (printk_ratelimit())
597 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
598 __func__, tclass);
599 return -EINVAL;
600 }
601
602 /*
603 * Known to the kernel, but not to the policy.
604 * Handle as a denial (allowed is 0).
605 */
606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Eric Paris64dbf072008-03-31 12:17:33 +1100609/*
610 * Given a sid find if the type has the permissive flag set
611 */
612int security_permissive_sid(u32 sid)
613{
614 struct context *context;
615 u32 type;
616 int rc;
617
James Morris0804d112008-06-06 18:40:29 +1000618 read_lock(&policy_rwlock);
Eric Paris64dbf072008-03-31 12:17:33 +1100619
620 context = sidtab_search(&sidtab, sid);
621 BUG_ON(!context);
622
623 type = context->type;
624 /*
625 * we are intentionally using type here, not type-1, the 0th bit may
626 * someday indicate that we are globally setting permissive in policy.
627 */
628 rc = ebitmap_get_bit(&policydb.permissive_map, type);
629
James Morris0804d112008-06-06 18:40:29 +1000630 read_unlock(&policy_rwlock);
Eric Paris64dbf072008-03-31 12:17:33 +1100631 return rc;
632}
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400635 struct context *ncontext,
636 struct context *tcontext,
637 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 char *o = NULL, *n = NULL, *t = NULL;
640 u32 olen, nlen, tlen;
641
642 if (context_struct_to_string(ocontext, &o, &olen) < 0)
643 goto out;
644 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
645 goto out;
646 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
647 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100648 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400649 "security_validate_transition: denied for"
650 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
651 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652out:
653 kfree(o);
654 kfree(n);
655 kfree(t);
656
657 if (!selinux_enforcing)
658 return 0;
659 return -EPERM;
660}
661
662int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Eric Paris5d55a342008-04-18 17:38:33 -0400663 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 struct context *ocontext;
666 struct context *ncontext;
667 struct context *tcontext;
668 struct class_datum *tclass_datum;
669 struct constraint_node *constraint;
670 int rc = 0;
671
672 if (!ss_initialized)
673 return 0;
674
James Morris0804d112008-06-06 18:40:29 +1000675 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 /*
678 * Remap extended Netlink classes for old policy versions.
679 * Do this here rather than socket_type_to_security_class()
680 * in case a newer policy version is loaded, allowing sockets
681 * to remain in the correct class.
682 */
683 if (policydb_loaded_version < POLICYDB_VERSION_NLCLASS)
684 if (tclass >= SECCLASS_NETLINK_ROUTE_SOCKET &&
685 tclass <= SECCLASS_NETLINK_DNRT_SOCKET)
686 tclass = SECCLASS_NETLINK_SOCKET;
687
688 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400689 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
690 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 rc = -EINVAL;
692 goto out;
693 }
694 tclass_datum = policydb.class_val_to_struct[tclass - 1];
695
696 ocontext = sidtab_search(&sidtab, oldsid);
697 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400698 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
699 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 rc = -EINVAL;
701 goto out;
702 }
703
704 ncontext = sidtab_search(&sidtab, newsid);
705 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400706 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
707 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 rc = -EINVAL;
709 goto out;
710 }
711
712 tcontext = sidtab_search(&sidtab, tasksid);
713 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400714 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
715 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 rc = -EINVAL;
717 goto out;
718 }
719
720 constraint = tclass_datum->validatetrans;
721 while (constraint) {
722 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400723 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400725 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto out;
727 }
728 constraint = constraint->next;
729 }
730
731out:
James Morris0804d112008-06-06 18:40:29 +1000732 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return rc;
734}
735
KaiGai Koheid9250de2008-08-28 16:35:57 +0900736/*
737 * security_bounded_transition - check whether the given
738 * transition is directed to bounded, or not.
739 * It returns 0, if @newsid is bounded by @oldsid.
740 * Otherwise, it returns error code.
741 *
742 * @oldsid : current security identifier
743 * @newsid : destinated security identifier
744 */
745int security_bounded_transition(u32 old_sid, u32 new_sid)
746{
747 struct context *old_context, *new_context;
748 struct type_datum *type;
749 int index;
750 int rc = -EINVAL;
751
752 read_lock(&policy_rwlock);
753
754 old_context = sidtab_search(&sidtab, old_sid);
755 if (!old_context) {
756 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
757 __func__, old_sid);
758 goto out;
759 }
760
761 new_context = sidtab_search(&sidtab, new_sid);
762 if (!new_context) {
763 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
764 __func__, new_sid);
765 goto out;
766 }
767
768 /* type/domain unchaned */
769 if (old_context->type == new_context->type) {
770 rc = 0;
771 goto out;
772 }
773
774 index = new_context->type;
775 while (true) {
776 type = policydb.type_val_to_struct[index - 1];
777 BUG_ON(!type);
778
779 /* not bounded anymore */
780 if (!type->bounds) {
781 rc = -EPERM;
782 break;
783 }
784
785 /* @newsid is bounded by @oldsid */
786 if (type->bounds == old_context->type) {
787 rc = 0;
788 break;
789 }
790 index = type->bounds;
791 }
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900792
793 if (rc) {
794 char *old_name = NULL;
795 char *new_name = NULL;
796 int length;
797
798 if (!context_struct_to_string(old_context,
799 &old_name, &length) &&
800 !context_struct_to_string(new_context,
801 &new_name, &length)) {
802 audit_log(current->audit_context,
803 GFP_ATOMIC, AUDIT_SELINUX_ERR,
804 "op=security_bounded_transition "
805 "result=denied "
806 "oldcontext=%s newcontext=%s",
807 old_name, new_name);
808 }
809 kfree(new_name);
810 kfree(old_name);
811 }
KaiGai Koheid9250de2008-08-28 16:35:57 +0900812out:
813 read_unlock(&policy_rwlock);
814
815 return rc;
816}
817
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819/**
820 * security_compute_av - Compute access vector decisions.
821 * @ssid: source security identifier
822 * @tsid: target security identifier
823 * @tclass: target security class
824 * @requested: requested permissions
825 * @avd: access vector decisions
826 *
827 * Compute a set of access vector decisions based on the
828 * SID pair (@ssid, @tsid) for the permissions in @tclass.
829 * Return -%EINVAL if any of the parameters are invalid or %0
830 * if the access vector decisions were computed successfully.
831 */
832int security_compute_av(u32 ssid,
833 u32 tsid,
834 u16 tclass,
835 u32 requested,
836 struct av_decision *avd)
837{
838 struct context *scontext = NULL, *tcontext = NULL;
839 int rc = 0;
840
841 if (!ss_initialized) {
Stephen Smalley4c443d12005-05-16 21:53:52 -0700842 avd->allowed = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 avd->auditallow = 0;
844 avd->auditdeny = 0xffffffff;
845 avd->seqno = latest_granting;
846 return 0;
847 }
848
James Morris0804d112008-06-06 18:40:29 +1000849 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 scontext = sidtab_search(&sidtab, ssid);
852 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400853 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
854 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 rc = -EINVAL;
856 goto out;
857 }
858 tcontext = sidtab_search(&sidtab, tsid);
859 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400860 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
861 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 rc = -EINVAL;
863 goto out;
864 }
865
866 rc = context_struct_compute_av(scontext, tcontext, tclass,
867 requested, avd);
868out:
James Morris0804d112008-06-06 18:40:29 +1000869 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return rc;
871}
872
873/*
874 * Write the security context string representation of
875 * the context structure `context' into a dynamically
876 * allocated string of the correct size. Set `*scontext'
877 * to point to this string and set `*scontext_len' to
878 * the length of the string.
879 */
880static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
881{
882 char *scontextp;
883
884 *scontext = NULL;
885 *scontext_len = 0;
886
Stephen Smalley12b29f32008-05-07 13:03:20 -0400887 if (context->len) {
888 *scontext_len = context->len;
889 *scontext = kstrdup(context->str, GFP_ATOMIC);
890 if (!(*scontext))
891 return -ENOMEM;
892 return 0;
893 }
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* Compute the size of the context. */
896 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
897 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
898 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
899 *scontext_len += mls_compute_context_len(context);
900
901 /* Allocate space for the context; caller must free this space. */
902 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400903 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 *scontext = scontextp;
906
907 /*
908 * Copy the user name, role name and type name into the context.
909 */
910 sprintf(scontextp, "%s:%s:%s",
911 policydb.p_user_val_to_name[context->user - 1],
912 policydb.p_role_val_to_name[context->role - 1],
913 policydb.p_type_val_to_name[context->type - 1]);
914 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400915 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
916 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 mls_sid_to_context(context, &scontextp);
919
920 *scontextp = 0;
921
922 return 0;
923}
924
925#include "initial_sid_to_string.h"
926
James Carterf0ee2e42007-04-04 10:11:29 -0400927const char *security_get_initial_sid_context(u32 sid)
928{
929 if (unlikely(sid > SECINITSID_NUM))
930 return NULL;
931 return initial_sid_to_string[sid];
932}
933
Stephen Smalley12b29f32008-05-07 13:03:20 -0400934static int security_sid_to_context_core(u32 sid, char **scontext,
935 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936{
937 struct context *context;
938 int rc = 0;
939
Stephen Smalley4f4acf32007-02-26 12:02:34 -0500940 *scontext = NULL;
941 *scontext_len = 0;
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!ss_initialized) {
944 if (sid <= SECINITSID_NUM) {
945 char *scontextp;
946
947 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -0400948 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -0700949 if (!scontextp) {
950 rc = -ENOMEM;
951 goto out;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 strcpy(scontextp, initial_sid_to_string[sid]);
954 *scontext = scontextp;
955 goto out;
956 }
Eric Paris744ba352008-04-17 11:52:44 -0400957 printk(KERN_ERR "SELinux: %s: called before initial "
958 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 rc = -EINVAL;
960 goto out;
961 }
James Morris0804d112008-06-06 18:40:29 +1000962 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -0400963 if (force)
964 context = sidtab_search_force(&sidtab, sid);
965 else
966 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -0400968 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
969 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 rc = -EINVAL;
971 goto out_unlock;
972 }
973 rc = context_struct_to_string(context, scontext, scontext_len);
974out_unlock:
James Morris0804d112008-06-06 18:40:29 +1000975 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976out:
977 return rc;
978
979}
980
Stephen Smalley12b29f32008-05-07 13:03:20 -0400981/**
982 * security_sid_to_context - Obtain a context for a given SID.
983 * @sid: security identifier, SID
984 * @scontext: security context
985 * @scontext_len: length in bytes
986 *
987 * Write the string representation of the context associated with @sid
988 * into a dynamically allocated string of the correct size. Set @scontext
989 * to point to this string and set @scontext_len to the length of the string.
990 */
991int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Stephen Smalley12b29f32008-05-07 13:03:20 -0400993 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
994}
995
996int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
997{
998 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
999}
1000
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001001/*
1002 * Caveat: Mutates scontext.
1003 */
Stephen Smalley12b29f32008-05-07 13:03:20 -04001004static int string_to_context_struct(struct policydb *pol,
1005 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001006 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001007 u32 scontext_len,
1008 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001009 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001010{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 struct role_datum *role;
1012 struct type_datum *typdatum;
1013 struct user_datum *usrdatum;
1014 char *scontextp, *p, oldc;
1015 int rc = 0;
1016
Stephen Smalley12b29f32008-05-07 13:03:20 -04001017 context_init(ctx);
1018
Stephen Smalley12b29f32008-05-07 13:03:20 -04001019 /* Parse the security context. */
1020
1021 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001022 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001023
1024 /* Extract the user. */
1025 p = scontextp;
1026 while (*p && *p != ':')
1027 p++;
1028
1029 if (*p == 0)
1030 goto out;
1031
1032 *p++ = 0;
1033
1034 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1035 if (!usrdatum)
1036 goto out;
1037
1038 ctx->user = usrdatum->value;
1039
1040 /* Extract role. */
1041 scontextp = p;
1042 while (*p && *p != ':')
1043 p++;
1044
1045 if (*p == 0)
1046 goto out;
1047
1048 *p++ = 0;
1049
1050 role = hashtab_search(pol->p_roles.table, scontextp);
1051 if (!role)
1052 goto out;
1053 ctx->role = role->value;
1054
1055 /* Extract type. */
1056 scontextp = p;
1057 while (*p && *p != ':')
1058 p++;
1059 oldc = *p;
1060 *p++ = 0;
1061
1062 typdatum = hashtab_search(pol->p_types.table, scontextp);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001063 if (!typdatum || typdatum->attribute)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001064 goto out;
1065
1066 ctx->type = typdatum->value;
1067
1068 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
1069 if (rc)
1070 goto out;
1071
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001072 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001073 rc = -EINVAL;
1074 goto out;
1075 }
1076
1077 /* Check the validity of the new context. */
1078 if (!policydb_context_isvalid(pol, ctx)) {
1079 rc = -EINVAL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001080 goto out;
1081 }
1082 rc = 0;
1083out:
Eric Paris8e531af2008-09-03 11:49:47 -04001084 if (rc)
1085 context_destroy(ctx);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001086 return rc;
1087}
1088
1089static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
1090 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1091 int force)
1092{
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001093 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001094 struct context context;
1095 int rc = 0;
1096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (!ss_initialized) {
1098 int i;
1099
1100 for (i = 1; i < SECINITSID_NUM; i++) {
1101 if (!strcmp(initial_sid_to_string[i], scontext)) {
1102 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
1105 }
1106 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
1109 *sid = SECSID_NULL;
1110
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001111 /* Copy the string so that we can modify the copy as we parse it. */
1112 scontext2 = kmalloc(scontext_len+1, gfp_flags);
1113 if (!scontext2)
1114 return -ENOMEM;
1115 memcpy(scontext2, scontext, scontext_len);
1116 scontext2[scontext_len] = 0;
1117
1118 if (force) {
1119 /* Save another copy for storing in uninterpreted form */
1120 str = kstrdup(scontext2, gfp_flags);
1121 if (!str) {
1122 kfree(scontext2);
1123 return -ENOMEM;
1124 }
1125 }
1126
James Morris0804d112008-06-06 18:40:29 +10001127 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001128 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001129 scontext2, scontext_len,
1130 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001131 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001132 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001133 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001134 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001135 } else if (rc)
1136 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Eric Paris8e531af2008-09-03 11:49:47 -04001138 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139out:
James Morris0804d112008-06-06 18:40:29 +10001140 read_unlock(&policy_rwlock);
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001141 kfree(scontext2);
1142 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 return rc;
1144}
1145
James Morrisf5c1d5b2005-07-28 01:07:37 -07001146/**
1147 * security_context_to_sid - Obtain a SID for a given security context.
1148 * @scontext: security context
1149 * @scontext_len: length in bytes
1150 * @sid: security identifier, SID
1151 *
1152 * Obtains a SID associated with the security context that
1153 * has the string representation specified by @scontext.
1154 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1155 * memory is available, or 0 on success.
1156 */
David Howells8f0cfa52008-04-29 00:59:41 -07001157int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001158{
1159 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001160 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001161}
1162
1163/**
1164 * security_context_to_sid_default - Obtain a SID for a given security context,
1165 * falling back to specified default if needed.
1166 *
1167 * @scontext: security context
1168 * @scontext_len: length in bytes
1169 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -07001170 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -07001171 *
1172 * Obtains a SID associated with the security context that
1173 * has the string representation specified by @scontext.
1174 * The default SID is passed to the MLS layer to be used to allow
1175 * kernel labeling of the MLS field if the MLS field is not present
1176 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -04001177 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -07001178 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1179 * memory is available, or 0 on success.
1180 */
David Howells7bf570d2008-04-29 20:52:51 +01001181int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1182 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001183{
1184 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001185 sid, def_sid, gfp_flags, 1);
1186}
1187
1188int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1189 u32 *sid)
1190{
1191 return security_context_to_sid_core(scontext, scontext_len,
1192 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195static int compute_sid_handle_invalid_context(
1196 struct context *scontext,
1197 struct context *tcontext,
1198 u16 tclass,
1199 struct context *newcontext)
1200{
1201 char *s = NULL, *t = NULL, *n = NULL;
1202 u32 slen, tlen, nlen;
1203
1204 if (context_struct_to_string(scontext, &s, &slen) < 0)
1205 goto out;
1206 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
1207 goto out;
1208 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
1209 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001210 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 "security_compute_sid: invalid context %s"
1212 " for scontext=%s"
1213 " tcontext=%s"
1214 " tclass=%s",
1215 n, s, t, policydb.p_class_val_to_name[tclass-1]);
1216out:
1217 kfree(s);
1218 kfree(t);
1219 kfree(n);
1220 if (!selinux_enforcing)
1221 return 0;
1222 return -EACCES;
1223}
1224
1225static int security_compute_sid(u32 ssid,
1226 u32 tsid,
1227 u16 tclass,
1228 u32 specified,
1229 u32 *out_sid)
1230{
1231 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1232 struct role_trans *roletr = NULL;
1233 struct avtab_key avkey;
1234 struct avtab_datum *avdatum;
1235 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 int rc = 0;
1237
1238 if (!ss_initialized) {
1239 switch (tclass) {
1240 case SECCLASS_PROCESS:
1241 *out_sid = ssid;
1242 break;
1243 default:
1244 *out_sid = tsid;
1245 break;
1246 }
1247 goto out;
1248 }
1249
Venkat Yekkirala851f8a62006-07-30 03:03:18 -07001250 context_init(&newcontext);
1251
James Morris0804d112008-06-06 18:40:29 +10001252 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 scontext = sidtab_search(&sidtab, ssid);
1255 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001256 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1257 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 rc = -EINVAL;
1259 goto out_unlock;
1260 }
1261 tcontext = sidtab_search(&sidtab, tsid);
1262 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001263 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1264 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 rc = -EINVAL;
1266 goto out_unlock;
1267 }
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 /* Set the user identity. */
1270 switch (specified) {
1271 case AVTAB_TRANSITION:
1272 case AVTAB_CHANGE:
1273 /* Use the process user identity. */
1274 newcontext.user = scontext->user;
1275 break;
1276 case AVTAB_MEMBER:
1277 /* Use the related object owner. */
1278 newcontext.user = tcontext->user;
1279 break;
1280 }
1281
1282 /* Set the role and type to default values. */
1283 switch (tclass) {
1284 case SECCLASS_PROCESS:
1285 /* Use the current role and type of process. */
1286 newcontext.role = scontext->role;
1287 newcontext.type = scontext->type;
1288 break;
1289 default:
1290 /* Use the well-defined object role. */
1291 newcontext.role = OBJECT_R_VAL;
1292 /* Use the type of the related object. */
1293 newcontext.type = tcontext->type;
1294 }
1295
1296 /* Look for a type transition/member/change rule. */
1297 avkey.source_type = scontext->type;
1298 avkey.target_type = tcontext->type;
1299 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001300 avkey.specified = specified;
1301 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001304 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001305 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03001306 for (; node; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001307 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 avdatum = &node->datum;
1309 break;
1310 }
1311 }
1312 }
1313
Stephen Smalley782ebb92005-09-03 15:55:16 -07001314 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001316 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
1319 /* Check for class-specific changes. */
1320 switch (tclass) {
1321 case SECCLASS_PROCESS:
1322 if (specified & AVTAB_TRANSITION) {
1323 /* Look for a role transition rule. */
1324 for (roletr = policydb.role_tr; roletr;
1325 roletr = roletr->next) {
1326 if (roletr->role == scontext->role &&
1327 roletr->type == tcontext->type) {
1328 /* Use the role transition rule. */
1329 newcontext.role = roletr->new_role;
1330 break;
1331 }
1332 }
1333 }
1334 break;
1335 default:
1336 break;
1337 }
1338
1339 /* Set the MLS attributes.
1340 This is done last because it may allocate memory. */
1341 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1342 if (rc)
1343 goto out_unlock;
1344
1345 /* Check the validity of the context. */
1346 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1347 rc = compute_sid_handle_invalid_context(scontext,
1348 tcontext,
1349 tclass,
1350 &newcontext);
1351 if (rc)
1352 goto out_unlock;
1353 }
1354 /* Obtain the sid for the context. */
1355 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1356out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001357 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 context_destroy(&newcontext);
1359out:
1360 return rc;
1361}
1362
1363/**
1364 * security_transition_sid - Compute the SID for a new subject/object.
1365 * @ssid: source security identifier
1366 * @tsid: target security identifier
1367 * @tclass: target security class
1368 * @out_sid: security identifier for new subject/object
1369 *
1370 * Compute a SID to use for labeling a new subject or object in the
1371 * class @tclass based on a SID pair (@ssid, @tsid).
1372 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1373 * if insufficient memory is available, or %0 if the new SID was
1374 * computed successfully.
1375 */
1376int security_transition_sid(u32 ssid,
1377 u32 tsid,
1378 u16 tclass,
1379 u32 *out_sid)
1380{
1381 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, out_sid);
1382}
1383
1384/**
1385 * security_member_sid - Compute the SID for member selection.
1386 * @ssid: source security identifier
1387 * @tsid: target security identifier
1388 * @tclass: target security class
1389 * @out_sid: security identifier for selected member
1390 *
1391 * Compute a SID to use when selecting a member of a polyinstantiated
1392 * object of class @tclass based on a SID pair (@ssid, @tsid).
1393 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1394 * if insufficient memory is available, or %0 if the SID was
1395 * computed successfully.
1396 */
1397int security_member_sid(u32 ssid,
1398 u32 tsid,
1399 u16 tclass,
1400 u32 *out_sid)
1401{
1402 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid);
1403}
1404
1405/**
1406 * security_change_sid - Compute the SID for object relabeling.
1407 * @ssid: source security identifier
1408 * @tsid: target security identifier
1409 * @tclass: target security class
1410 * @out_sid: security identifier for selected member
1411 *
1412 * Compute a SID to use for relabeling an object of class @tclass
1413 * based on a SID pair (@ssid, @tsid).
1414 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1415 * if insufficient memory is available, or %0 if the SID was
1416 * computed successfully.
1417 */
1418int security_change_sid(u32 ssid,
1419 u32 tsid,
1420 u16 tclass,
1421 u32 *out_sid)
1422{
1423 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid);
1424}
1425
Chad Sellersb94c7e62006-11-06 12:38:18 -05001426/*
1427 * Verify that each kernel class that is defined in the
1428 * policy is correct
1429 */
1430static int validate_classes(struct policydb *p)
1431{
1432 int i, j;
1433 struct class_datum *cladatum;
1434 struct perm_datum *perdatum;
1435 u32 nprim, tmp, common_pts_len, perm_val, pol_val;
1436 u16 class_val;
1437 const struct selinux_class_perm *kdefs = &selinux_class_perm;
1438 const char *def_class, *def_perm, *pol_class;
1439 struct symtab *perms;
Eric Paris6cbe2702008-06-09 16:51:37 -04001440 bool print_unknown_handle = 0;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001441
Eric Paris3f120702007-09-21 14:37:10 -04001442 if (p->allow_unknown) {
1443 u32 num_classes = kdefs->cts_len;
1444 p->undefined_perms = kcalloc(num_classes, sizeof(u32), GFP_KERNEL);
1445 if (!p->undefined_perms)
1446 return -ENOMEM;
1447 }
1448
Chad Sellersb94c7e62006-11-06 12:38:18 -05001449 for (i = 1; i < kdefs->cts_len; i++) {
1450 def_class = kdefs->class_to_string[i];
Stephen Smalleya764ae42007-03-26 13:36:26 -04001451 if (!def_class)
1452 continue;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001453 if (i > p->p_classes.nprim) {
1454 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001455 "SELinux: class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001456 def_class);
Eric Paris3f120702007-09-21 14:37:10 -04001457 if (p->reject_unknown)
1458 return -EINVAL;
1459 if (p->allow_unknown)
1460 p->undefined_perms[i-1] = ~0U;
Eric Paris6cbe2702008-06-09 16:51:37 -04001461 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001462 continue;
1463 }
1464 pol_class = p->p_class_val_to_name[i-1];
1465 if (strcmp(pol_class, def_class)) {
1466 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001467 "SELinux: class %d is incorrect, found %s but should be %s\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001468 i, pol_class, def_class);
1469 return -EINVAL;
1470 }
1471 }
1472 for (i = 0; i < kdefs->av_pts_len; i++) {
1473 class_val = kdefs->av_perm_to_string[i].tclass;
1474 perm_val = kdefs->av_perm_to_string[i].value;
1475 def_perm = kdefs->av_perm_to_string[i].name;
1476 if (class_val > p->p_classes.nprim)
1477 continue;
1478 pol_class = p->p_class_val_to_name[class_val-1];
1479 cladatum = hashtab_search(p->p_classes.table, pol_class);
1480 BUG_ON(!cladatum);
1481 perms = &cladatum->permissions;
1482 nprim = 1 << (perms->nprim - 1);
1483 if (perm_val > nprim) {
1484 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001485 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001486 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001487 if (p->reject_unknown)
1488 return -EINVAL;
1489 if (p->allow_unknown)
1490 p->undefined_perms[class_val-1] |= perm_val;
Eric Paris6cbe2702008-06-09 16:51:37 -04001491 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001492 continue;
1493 }
1494 perdatum = hashtab_search(perms->table, def_perm);
1495 if (perdatum == NULL) {
1496 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001497 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001498 def_perm, pol_class);
1499 return -EINVAL;
1500 }
1501 pol_val = 1 << (perdatum->value - 1);
1502 if (pol_val != perm_val) {
1503 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001504 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001505 def_perm, pol_class);
1506 return -EINVAL;
1507 }
1508 }
1509 for (i = 0; i < kdefs->av_inherit_len; i++) {
1510 class_val = kdefs->av_inherit[i].tclass;
1511 if (class_val > p->p_classes.nprim)
1512 continue;
1513 pol_class = p->p_class_val_to_name[class_val-1];
1514 cladatum = hashtab_search(p->p_classes.table, pol_class);
1515 BUG_ON(!cladatum);
1516 if (!cladatum->comdatum) {
1517 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001518 "SELinux: class %s should have an inherits clause but does not\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001519 pol_class);
1520 return -EINVAL;
1521 }
1522 tmp = kdefs->av_inherit[i].common_base;
1523 common_pts_len = 0;
1524 while (!(tmp & 0x01)) {
1525 common_pts_len++;
1526 tmp >>= 1;
1527 }
1528 perms = &cladatum->comdatum->permissions;
1529 for (j = 0; j < common_pts_len; j++) {
1530 def_perm = kdefs->av_inherit[i].common_pts[j];
1531 if (j >= perms->nprim) {
1532 printk(KERN_INFO
James Morris454d9722008-02-26 20:42:02 +11001533 "SELinux: permission %s in class %s not defined in policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001534 def_perm, pol_class);
Eric Paris3f120702007-09-21 14:37:10 -04001535 if (p->reject_unknown)
1536 return -EINVAL;
1537 if (p->allow_unknown)
1538 p->undefined_perms[class_val-1] |= (1 << j);
Eric Paris6cbe2702008-06-09 16:51:37 -04001539 print_unknown_handle = 1;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001540 continue;
1541 }
1542 perdatum = hashtab_search(perms->table, def_perm);
1543 if (perdatum == NULL) {
1544 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001545 "SELinux: permission %s in class %s not found in policy, bad policy\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001546 def_perm, pol_class);
1547 return -EINVAL;
1548 }
1549 if (perdatum->value != j + 1) {
1550 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001551 "SELinux: permission %s in class %s has incorrect value\n",
Chad Sellersb94c7e62006-11-06 12:38:18 -05001552 def_perm, pol_class);
1553 return -EINVAL;
1554 }
1555 }
1556 }
Eric Paris6cbe2702008-06-09 16:51:37 -04001557 if (print_unknown_handle)
1558 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
1559 (security_get_allow_unknown() ? "allowed" : "denied"));
Chad Sellersb94c7e62006-11-06 12:38:18 -05001560 return 0;
1561}
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563/* Clone the SID into the new SID table. */
1564static int clone_sid(u32 sid,
1565 struct context *context,
1566 void *arg)
1567{
1568 struct sidtab *s = arg;
1569
1570 return sidtab_insert(s, sid, context);
1571}
1572
1573static inline int convert_context_handle_invalid_context(struct context *context)
1574{
1575 int rc = 0;
1576
1577 if (selinux_enforcing) {
1578 rc = -EINVAL;
1579 } else {
1580 char *s;
1581 u32 len;
1582
Stephen Smalley12b29f32008-05-07 13:03:20 -04001583 if (!context_struct_to_string(context, &s, &len)) {
1584 printk(KERN_WARNING
1585 "SELinux: Context %s would be invalid if enforcing\n",
1586 s);
1587 kfree(s);
1588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 }
1590 return rc;
1591}
1592
1593struct convert_context_args {
1594 struct policydb *oldp;
1595 struct policydb *newp;
1596};
1597
1598/*
1599 * Convert the values in the security context
1600 * structure `c' from the values specified
1601 * in the policy `p->oldp' to the values specified
1602 * in the policy `p->newp'. Verify that the
1603 * context is valid under the new policy.
1604 */
1605static int convert_context(u32 key,
1606 struct context *c,
1607 void *p)
1608{
1609 struct convert_context_args *args;
1610 struct context oldc;
1611 struct role_datum *role;
1612 struct type_datum *typdatum;
1613 struct user_datum *usrdatum;
1614 char *s;
1615 u32 len;
1616 int rc;
1617
1618 args = p;
1619
Stephen Smalley12b29f32008-05-07 13:03:20 -04001620 if (c->str) {
1621 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001622 s = kstrdup(c->str, GFP_KERNEL);
1623 if (!s) {
1624 rc = -ENOMEM;
1625 goto out;
1626 }
1627 rc = string_to_context_struct(args->newp, NULL, s,
1628 c->len, &ctx, SECSID_NULL);
1629 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001630 if (!rc) {
1631 printk(KERN_INFO
1632 "SELinux: Context %s became valid (mapped).\n",
1633 c->str);
1634 /* Replace string with mapped representation. */
1635 kfree(c->str);
1636 memcpy(c, &ctx, sizeof(*c));
1637 goto out;
1638 } else if (rc == -EINVAL) {
1639 /* Retain string representation for later mapping. */
1640 rc = 0;
1641 goto out;
1642 } else {
1643 /* Other error condition, e.g. ENOMEM. */
1644 printk(KERN_ERR
1645 "SELinux: Unable to map context %s, rc = %d.\n",
1646 c->str, -rc);
1647 goto out;
1648 }
1649 }
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 rc = context_cpy(&oldc, c);
1652 if (rc)
1653 goto out;
1654
1655 rc = -EINVAL;
1656
1657 /* Convert the user. */
1658 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001659 args->oldp->p_user_val_to_name[c->user - 1]);
1660 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 c->user = usrdatum->value;
1663
1664 /* Convert the role. */
1665 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001666 args->oldp->p_role_val_to_name[c->role - 1]);
1667 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 c->role = role->value;
1670
1671 /* Convert the type. */
1672 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001673 args->oldp->p_type_val_to_name[c->type - 1]);
1674 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 c->type = typdatum->value;
1677
1678 rc = mls_convert_context(args->oldp, args->newp, c);
1679 if (rc)
1680 goto bad;
1681
1682 /* Check the validity of the new context. */
1683 if (!policydb_context_isvalid(args->newp, c)) {
1684 rc = convert_context_handle_invalid_context(&oldc);
1685 if (rc)
1686 goto bad;
1687 }
1688
1689 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001690 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691out:
1692 return rc;
1693bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001694 /* Map old representation to string and save it. */
1695 if (context_struct_to_string(&oldc, &s, &len))
1696 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001698 context_destroy(c);
1699 c->str = s;
1700 c->len = len;
1701 printk(KERN_INFO
1702 "SELinux: Context %s became invalid (unmapped).\n",
1703 c->str);
1704 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto out;
1706}
1707
Paul Moore3bb56b22008-01-29 08:38:19 -05001708static void security_load_policycaps(void)
1709{
1710 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1711 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001712 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1713 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001717static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719/**
1720 * security_load_policy - Load a security policy configuration.
1721 * @data: binary policy data
1722 * @len: length of data in bytes
1723 *
1724 * Load a new set of security policy configuration data,
1725 * validate it and convert the SID table as necessary.
1726 * This function will flush the access vector cache after
1727 * loading the new policy.
1728 */
1729int security_load_policy(void *data, size_t len)
1730{
1731 struct policydb oldpolicydb, newpolicydb;
1732 struct sidtab oldsidtab, newsidtab;
1733 struct convert_context_args args;
1734 u32 seqno;
1735 int rc = 0;
1736 struct policy_file file = { data, len }, *fp = &file;
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (!ss_initialized) {
1739 avtab_cache_init();
1740 if (policydb_read(&policydb, fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 avtab_cache_destroy();
1742 return -EINVAL;
1743 }
1744 if (policydb_load_isids(&policydb, &sidtab)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 policydb_destroy(&policydb);
1746 avtab_cache_destroy();
1747 return -EINVAL;
1748 }
Chad Sellersb94c7e62006-11-06 12:38:18 -05001749 /* Verify that the kernel defined classes are correct. */
1750 if (validate_classes(&policydb)) {
1751 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001752 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001753 sidtab_destroy(&sidtab);
1754 policydb_destroy(&policydb);
1755 avtab_cache_destroy();
1756 return -EINVAL;
1757 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001758 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 policydb_loaded_version = policydb.policyvers;
1760 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001761 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001763 avc_ss_reset(seqno);
1764 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001765 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001766 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768 }
1769
1770#if 0
1771 sidtab_hash_eval(&sidtab, "sids");
1772#endif
1773
Eric Paris89abd0a2008-06-09 15:58:04 -04001774 if (policydb_read(&newpolicydb, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Stephen Smalley12b29f32008-05-07 13:03:20 -04001777 if (sidtab_init(&newsidtab)) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001778 policydb_destroy(&newpolicydb);
1779 return -ENOMEM;
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Chad Sellersb94c7e62006-11-06 12:38:18 -05001782 /* Verify that the kernel defined classes are correct. */
1783 if (validate_classes(&newpolicydb)) {
1784 printk(KERN_ERR
James Morris454d9722008-02-26 20:42:02 +11001785 "SELinux: the definition of a class is incorrect\n");
Chad Sellersb94c7e62006-11-06 12:38:18 -05001786 rc = -EINVAL;
1787 goto err;
1788 }
1789
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001790 rc = security_preserve_bools(&newpolicydb);
1791 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001792 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001793 goto err;
1794 }
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 /* Clone the SID table. */
1797 sidtab_shutdown(&sidtab);
1798 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1799 rc = -ENOMEM;
1800 goto err;
1801 }
1802
Stephen Smalley12b29f32008-05-07 13:03:20 -04001803 /*
1804 * Convert the internal representations of contexts
1805 * in the new SID table.
1806 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 args.oldp = &policydb;
1808 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001809 rc = sidtab_map(&newsidtab, convert_context, &args);
1810 if (rc)
1811 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
1813 /* Save the old policydb and SID table to free later. */
1814 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1815 sidtab_set(&oldsidtab, &sidtab);
1816
1817 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10001818 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 memcpy(&policydb, &newpolicydb, sizeof policydb);
1820 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001821 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 seqno = ++latest_granting;
1823 policydb_loaded_version = policydb.policyvers;
James Morris0804d112008-06-06 18:40:29 +10001824 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* Free the old policydb and SID table. */
1827 policydb_destroy(&oldpolicydb);
1828 sidtab_destroy(&oldsidtab);
1829
1830 avc_ss_reset(seqno);
1831 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001832 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001833 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 return 0;
1836
1837err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 sidtab_destroy(&newsidtab);
1839 policydb_destroy(&newpolicydb);
1840 return rc;
1841
1842}
1843
1844/**
1845 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 * @protocol: protocol number
1847 * @port: port number
1848 * @out_sid: security identifier
1849 */
Paul Moore3e112172008-04-10 10:48:14 -04001850int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 struct ocontext *c;
1853 int rc = 0;
1854
James Morris0804d112008-06-06 18:40:29 +10001855 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 c = policydb.ocontexts[OCON_PORT];
1858 while (c) {
1859 if (c->u.port.protocol == protocol &&
1860 c->u.port.low_port <= port &&
1861 c->u.port.high_port >= port)
1862 break;
1863 c = c->next;
1864 }
1865
1866 if (c) {
1867 if (!c->sid[0]) {
1868 rc = sidtab_context_to_sid(&sidtab,
1869 &c->context[0],
1870 &c->sid[0]);
1871 if (rc)
1872 goto out;
1873 }
1874 *out_sid = c->sid[0];
1875 } else {
1876 *out_sid = SECINITSID_PORT;
1877 }
1878
1879out:
James Morris0804d112008-06-06 18:40:29 +10001880 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return rc;
1882}
1883
1884/**
1885 * security_netif_sid - Obtain the SID for a network interface.
1886 * @name: interface name
1887 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001889int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
1891 int rc = 0;
1892 struct ocontext *c;
1893
James Morris0804d112008-06-06 18:40:29 +10001894 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 c = policydb.ocontexts[OCON_NETIF];
1897 while (c) {
1898 if (strcmp(name, c->u.name) == 0)
1899 break;
1900 c = c->next;
1901 }
1902
1903 if (c) {
1904 if (!c->sid[0] || !c->sid[1]) {
1905 rc = sidtab_context_to_sid(&sidtab,
1906 &c->context[0],
1907 &c->sid[0]);
1908 if (rc)
1909 goto out;
1910 rc = sidtab_context_to_sid(&sidtab,
1911 &c->context[1],
1912 &c->sid[1]);
1913 if (rc)
1914 goto out;
1915 }
1916 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001917 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920out:
James Morris0804d112008-06-06 18:40:29 +10001921 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return rc;
1923}
1924
1925static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1926{
1927 int i, fail = 0;
1928
Eric Paris5d55a342008-04-18 17:38:33 -04001929 for (i = 0; i < 4; i++)
1930 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 fail = 1;
1932 break;
1933 }
1934
1935 return !fail;
1936}
1937
1938/**
1939 * security_node_sid - Obtain the SID for a node (host).
1940 * @domain: communication domain aka address family
1941 * @addrp: address
1942 * @addrlen: address length in bytes
1943 * @out_sid: security identifier
1944 */
1945int security_node_sid(u16 domain,
1946 void *addrp,
1947 u32 addrlen,
1948 u32 *out_sid)
1949{
1950 int rc = 0;
1951 struct ocontext *c;
1952
James Morris0804d112008-06-06 18:40:29 +10001953 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
1955 switch (domain) {
1956 case AF_INET: {
1957 u32 addr;
1958
1959 if (addrlen != sizeof(u32)) {
1960 rc = -EINVAL;
1961 goto out;
1962 }
1963
1964 addr = *((u32 *)addrp);
1965
1966 c = policydb.ocontexts[OCON_NODE];
1967 while (c) {
1968 if (c->u.node.addr == (addr & c->u.node.mask))
1969 break;
1970 c = c->next;
1971 }
1972 break;
1973 }
1974
1975 case AF_INET6:
1976 if (addrlen != sizeof(u64) * 2) {
1977 rc = -EINVAL;
1978 goto out;
1979 }
1980 c = policydb.ocontexts[OCON_NODE6];
1981 while (c) {
1982 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1983 c->u.node6.mask))
1984 break;
1985 c = c->next;
1986 }
1987 break;
1988
1989 default:
1990 *out_sid = SECINITSID_NODE;
1991 goto out;
1992 }
1993
1994 if (c) {
1995 if (!c->sid[0]) {
1996 rc = sidtab_context_to_sid(&sidtab,
1997 &c->context[0],
1998 &c->sid[0]);
1999 if (rc)
2000 goto out;
2001 }
2002 *out_sid = c->sid[0];
2003 } else {
2004 *out_sid = SECINITSID_NODE;
2005 }
2006
2007out:
James Morris0804d112008-06-06 18:40:29 +10002008 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return rc;
2010}
2011
2012#define SIDS_NEL 25
2013
2014/**
2015 * security_get_user_sids - Obtain reachable SIDs for a user.
2016 * @fromsid: starting SID
2017 * @username: username
2018 * @sids: array of reachable SIDs for user
2019 * @nel: number of elements in @sids
2020 *
2021 * Generate the set of SIDs for legal security contexts
2022 * for a given user that can be reached by @fromsid.
2023 * Set *@sids to point to a dynamically allocated
2024 * array containing the set of SIDs. Set *@nel to the
2025 * number of elements in the array.
2026 */
2027
2028int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04002029 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 u32 **sids,
2031 u32 *nel)
2032{
2033 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002034 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 u32 mynel = 0, maxnel = SIDS_NEL;
2036 struct user_datum *user;
2037 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002038 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 int rc = 0, i, j;
2040
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002041 *sids = NULL;
2042 *nel = 0;
2043
2044 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
James Morris0804d112008-06-06 18:40:29 +10002047 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
Stephen Smalley12b29f32008-05-07 13:03:20 -04002049 context_init(&usercon);
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 fromcon = sidtab_search(&sidtab, fromsid);
2052 if (!fromcon) {
2053 rc = -EINVAL;
2054 goto out_unlock;
2055 }
2056
2057 user = hashtab_search(policydb.p_users.table, username);
2058 if (!user) {
2059 rc = -EINVAL;
2060 goto out_unlock;
2061 }
2062 usercon.user = user->value;
2063
James Morris89d155e2005-10-30 14:59:21 -08002064 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (!mysids) {
2066 rc = -ENOMEM;
2067 goto out_unlock;
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002070 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 role = policydb.role_val_to_struct[i];
2072 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002073 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 usercon.type = j+1;
2075
2076 if (mls_setup_user_range(fromcon, user, &usercon))
2077 continue;
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002080 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (mynel < maxnel) {
2083 mysids[mynel++] = sid;
2084 } else {
2085 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08002086 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (!mysids2) {
2088 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 goto out_unlock;
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2092 kfree(mysids);
2093 mysids = mysids2;
2094 mysids[mynel++] = sid;
2095 }
2096 }
2097 }
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002100 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002101 if (rc || !mynel) {
2102 kfree(mysids);
2103 goto out;
2104 }
2105
2106 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2107 if (!mysids2) {
2108 rc = -ENOMEM;
2109 kfree(mysids);
2110 goto out;
2111 }
2112 for (i = 0, j = 0; i < mynel; i++) {
2113 rc = avc_has_perm_noaudit(fromsid, mysids[i],
2114 SECCLASS_PROCESS,
2115 PROCESS__TRANSITION, AVC_STRICT,
2116 NULL);
2117 if (!rc)
2118 mysids2[j++] = mysids[i];
2119 cond_resched();
2120 }
2121 rc = 0;
2122 kfree(mysids);
2123 *sids = mysids2;
2124 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125out:
2126 return rc;
2127}
2128
2129/**
2130 * security_genfs_sid - Obtain a SID for a file in a filesystem
2131 * @fstype: filesystem type
2132 * @path: path from root of mount
2133 * @sclass: file security class
2134 * @sid: SID for path
2135 *
2136 * Obtain a SID to use for a file in a filesystem that
2137 * cannot support xattr or use a fixed labeling behavior like
2138 * transition SIDs or task SIDs.
2139 */
2140int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04002141 char *path,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 u16 sclass,
2143 u32 *sid)
2144{
2145 int len;
2146 struct genfs *genfs;
2147 struct ocontext *c;
2148 int rc = 0, cmp = 0;
2149
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05002150 while (path[0] == '/' && path[1] == '/')
2151 path++;
2152
James Morris0804d112008-06-06 18:40:29 +10002153 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2156 cmp = strcmp(fstype, genfs->fstype);
2157 if (cmp <= 0)
2158 break;
2159 }
2160
2161 if (!genfs || cmp) {
2162 *sid = SECINITSID_UNLABELED;
2163 rc = -ENOENT;
2164 goto out;
2165 }
2166
2167 for (c = genfs->head; c; c = c->next) {
2168 len = strlen(c->u.name);
2169 if ((!c->v.sclass || sclass == c->v.sclass) &&
2170 (strncmp(c->u.name, path, len) == 0))
2171 break;
2172 }
2173
2174 if (!c) {
2175 *sid = SECINITSID_UNLABELED;
2176 rc = -ENOENT;
2177 goto out;
2178 }
2179
2180 if (!c->sid[0]) {
2181 rc = sidtab_context_to_sid(&sidtab,
2182 &c->context[0],
2183 &c->sid[0]);
2184 if (rc)
2185 goto out;
2186 }
2187
2188 *sid = c->sid[0];
2189out:
James Morris0804d112008-06-06 18:40:29 +10002190 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return rc;
2192}
2193
2194/**
2195 * security_fs_use - Determine how to handle labeling for a filesystem.
2196 * @fstype: filesystem type
2197 * @behavior: labeling behavior
2198 * @sid: SID for filesystem (superblock)
2199 */
2200int security_fs_use(
2201 const char *fstype,
2202 unsigned int *behavior,
James Morris089be432008-07-15 18:32:49 +10002203 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 int rc = 0;
2206 struct ocontext *c;
2207
James Morris0804d112008-06-06 18:40:29 +10002208 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 c = policydb.ocontexts[OCON_FSUSE];
2211 while (c) {
2212 if (strcmp(fstype, c->u.name) == 0)
2213 break;
2214 c = c->next;
2215 }
2216
2217 if (c) {
2218 *behavior = c->v.behavior;
2219 if (!c->sid[0]) {
2220 rc = sidtab_context_to_sid(&sidtab,
2221 &c->context[0],
2222 &c->sid[0]);
2223 if (rc)
2224 goto out;
2225 }
2226 *sid = c->sid[0];
2227 } else {
James Morris089be432008-07-15 18:32:49 +10002228 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2229 if (rc) {
2230 *behavior = SECURITY_FS_USE_NONE;
2231 rc = 0;
2232 } else {
2233 *behavior = SECURITY_FS_USE_GENFS;
2234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236
2237out:
James Morris0804d112008-06-06 18:40:29 +10002238 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return rc;
2240}
2241
2242int security_get_bools(int *len, char ***names, int **values)
2243{
2244 int i, rc = -ENOMEM;
2245
James Morris0804d112008-06-06 18:40:29 +10002246 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 *names = NULL;
2248 *values = NULL;
2249
2250 *len = policydb.p_bools.nprim;
2251 if (!*len) {
2252 rc = 0;
2253 goto out;
2254 }
2255
Eric Paris5d55a342008-04-18 17:38:33 -04002256 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (!*names)
2258 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Jesper Juhle0795cf2006-01-09 20:54:46 -08002260 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (!*values)
2262 goto err;
2263
2264 for (i = 0; i < *len; i++) {
2265 size_t name_len;
2266 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2267 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002268 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (!(*names)[i])
2270 goto err;
2271 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2272 (*names)[i][name_len - 1] = 0;
2273 }
2274 rc = 0;
2275out:
James Morris0804d112008-06-06 18:40:29 +10002276 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return rc;
2278err:
2279 if (*names) {
2280 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002281 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002283 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto out;
2285}
2286
2287
2288int security_set_bools(int len, int *values)
2289{
2290 int i, rc = 0;
2291 int lenp, seqno = 0;
2292 struct cond_node *cur;
2293
James Morris0804d112008-06-06 18:40:29 +10002294 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 lenp = policydb.p_bools.nprim;
2297 if (len != lenp) {
2298 rc = -EFAULT;
2299 goto out;
2300 }
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002303 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2304 audit_log(current->audit_context, GFP_ATOMIC,
2305 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002306 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002307 policydb.p_bool_val_to_name[i],
2308 !!values[i],
2309 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002310 audit_get_loginuid(current),
2311 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002312 }
Eric Paris5d55a342008-04-18 17:38:33 -04002313 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002315 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002319 for (cur = policydb.cond_list; cur; cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 rc = evaluate_cond_node(&policydb, cur);
2321 if (rc)
2322 goto out;
2323 }
2324
2325 seqno = ++latest_granting;
2326
2327out:
James Morris0804d112008-06-06 18:40:29 +10002328 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (!rc) {
2330 avc_ss_reset(seqno);
2331 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002332 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334 return rc;
2335}
2336
2337int security_get_bool_value(int bool)
2338{
2339 int rc = 0;
2340 int len;
2341
James Morris0804d112008-06-06 18:40:29 +10002342 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 len = policydb.p_bools.nprim;
2345 if (bool >= len) {
2346 rc = -EFAULT;
2347 goto out;
2348 }
2349
2350 rc = policydb.bool_val_to_struct[bool]->state;
2351out:
James Morris0804d112008-06-06 18:40:29 +10002352 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return rc;
2354}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002355
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002356static int security_preserve_bools(struct policydb *p)
2357{
2358 int rc, nbools = 0, *bvalues = NULL, i;
2359 char **bnames = NULL;
2360 struct cond_bool_datum *booldatum;
2361 struct cond_node *cur;
2362
2363 rc = security_get_bools(&nbools, &bnames, &bvalues);
2364 if (rc)
2365 goto out;
2366 for (i = 0; i < nbools; i++) {
2367 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2368 if (booldatum)
2369 booldatum->state = bvalues[i];
2370 }
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002371 for (cur = p->cond_list; cur; cur = cur->next) {
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002372 rc = evaluate_cond_node(p, cur);
2373 if (rc)
2374 goto out;
2375 }
2376
2377out:
2378 if (bnames) {
2379 for (i = 0; i < nbools; i++)
2380 kfree(bnames[i]);
2381 }
2382 kfree(bnames);
2383 kfree(bvalues);
2384 return rc;
2385}
2386
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002387/*
2388 * security_sid_mls_copy() - computes a new sid based on the given
2389 * sid and the mls portion of mls_sid.
2390 */
2391int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2392{
2393 struct context *context1;
2394 struct context *context2;
2395 struct context newcon;
2396 char *s;
2397 u32 len;
2398 int rc = 0;
2399
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002400 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002401 *new_sid = sid;
2402 goto out;
2403 }
2404
2405 context_init(&newcon);
2406
James Morris0804d112008-06-06 18:40:29 +10002407 read_lock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002408 context1 = sidtab_search(&sidtab, sid);
2409 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002410 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2411 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002412 rc = -EINVAL;
2413 goto out_unlock;
2414 }
2415
2416 context2 = sidtab_search(&sidtab, mls_sid);
2417 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002418 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2419 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002420 rc = -EINVAL;
2421 goto out_unlock;
2422 }
2423
2424 newcon.user = context1->user;
2425 newcon.role = context1->role;
2426 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002427 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002428 if (rc)
2429 goto out_unlock;
2430
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002431 /* Check the validity of the new context. */
2432 if (!policydb_context_isvalid(&policydb, &newcon)) {
2433 rc = convert_context_handle_invalid_context(&newcon);
2434 if (rc)
2435 goto bad;
2436 }
2437
2438 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2439 goto out_unlock;
2440
2441bad:
2442 if (!context_struct_to_string(&newcon, &s, &len)) {
2443 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2444 "security_sid_mls_copy: invalid context %s", s);
2445 kfree(s);
2446 }
2447
2448out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002449 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002450 context_destroy(&newcon);
2451out:
2452 return rc;
2453}
2454
Paul Moore220deb92008-01-29 08:38:23 -05002455/**
2456 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2457 * @nlbl_sid: NetLabel SID
2458 * @nlbl_type: NetLabel labeling protocol type
2459 * @xfrm_sid: XFRM SID
2460 *
2461 * Description:
2462 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2463 * resolved into a single SID it is returned via @peer_sid and the function
2464 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2465 * returns a negative value. A table summarizing the behavior is below:
2466 *
2467 * | function return | @sid
2468 * ------------------------------+-----------------+-----------------
2469 * no peer labels | 0 | SECSID_NULL
2470 * single peer label | 0 | <peer_label>
2471 * multiple, consistent labels | 0 | <peer_label>
2472 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2473 *
2474 */
2475int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2476 u32 xfrm_sid,
2477 u32 *peer_sid)
2478{
2479 int rc;
2480 struct context *nlbl_ctx;
2481 struct context *xfrm_ctx;
2482
2483 /* handle the common (which also happens to be the set of easy) cases
2484 * right away, these two if statements catch everything involving a
2485 * single or absent peer SID/label */
2486 if (xfrm_sid == SECSID_NULL) {
2487 *peer_sid = nlbl_sid;
2488 return 0;
2489 }
2490 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2491 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2492 * is present */
2493 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2494 *peer_sid = xfrm_sid;
2495 return 0;
2496 }
2497
2498 /* we don't need to check ss_initialized here since the only way both
2499 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2500 * security server was initialized and ss_initialized was true */
2501 if (!selinux_mls_enabled) {
2502 *peer_sid = SECSID_NULL;
2503 return 0;
2504 }
2505
James Morris0804d112008-06-06 18:40:29 +10002506 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002507
2508 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2509 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002510 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2511 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002512 rc = -EINVAL;
2513 goto out_slowpath;
2514 }
2515 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2516 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002517 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2518 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002519 rc = -EINVAL;
2520 goto out_slowpath;
2521 }
2522 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2523
2524out_slowpath:
James Morris0804d112008-06-06 18:40:29 +10002525 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002526 if (rc == 0)
2527 /* at present NetLabel SIDs/labels really only carry MLS
2528 * information so if the MLS portion of the NetLabel SID
2529 * matches the MLS portion of the labeled XFRM SID/label
2530 * then pass along the XFRM SID as it is the most
2531 * expressive */
2532 *peer_sid = xfrm_sid;
2533 else
2534 *peer_sid = SECSID_NULL;
2535 return rc;
2536}
2537
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002538static int get_classes_callback(void *k, void *d, void *args)
2539{
2540 struct class_datum *datum = d;
2541 char *name = k, **classes = args;
2542 int value = datum->value - 1;
2543
2544 classes[value] = kstrdup(name, GFP_ATOMIC);
2545 if (!classes[value])
2546 return -ENOMEM;
2547
2548 return 0;
2549}
2550
2551int security_get_classes(char ***classes, int *nclasses)
2552{
2553 int rc = -ENOMEM;
2554
James Morris0804d112008-06-06 18:40:29 +10002555 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002556
2557 *nclasses = policydb.p_classes.nprim;
2558 *classes = kcalloc(*nclasses, sizeof(*classes), GFP_ATOMIC);
2559 if (!*classes)
2560 goto out;
2561
2562 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2563 *classes);
2564 if (rc < 0) {
2565 int i;
2566 for (i = 0; i < *nclasses; i++)
2567 kfree((*classes)[i]);
2568 kfree(*classes);
2569 }
2570
2571out:
James Morris0804d112008-06-06 18:40:29 +10002572 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002573 return rc;
2574}
2575
2576static int get_permissions_callback(void *k, void *d, void *args)
2577{
2578 struct perm_datum *datum = d;
2579 char *name = k, **perms = args;
2580 int value = datum->value - 1;
2581
2582 perms[value] = kstrdup(name, GFP_ATOMIC);
2583 if (!perms[value])
2584 return -ENOMEM;
2585
2586 return 0;
2587}
2588
2589int security_get_permissions(char *class, char ***perms, int *nperms)
2590{
2591 int rc = -ENOMEM, i;
2592 struct class_datum *match;
2593
James Morris0804d112008-06-06 18:40:29 +10002594 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002595
2596 match = hashtab_search(policydb.p_classes.table, class);
2597 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002598 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002599 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002600 rc = -EINVAL;
2601 goto out;
2602 }
2603
2604 *nperms = match->permissions.nprim;
2605 *perms = kcalloc(*nperms, sizeof(*perms), GFP_ATOMIC);
2606 if (!*perms)
2607 goto out;
2608
2609 if (match->comdatum) {
2610 rc = hashtab_map(match->comdatum->permissions.table,
2611 get_permissions_callback, *perms);
2612 if (rc < 0)
2613 goto err;
2614 }
2615
2616 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2617 *perms);
2618 if (rc < 0)
2619 goto err;
2620
2621out:
James Morris0804d112008-06-06 18:40:29 +10002622 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002623 return rc;
2624
2625err:
James Morris0804d112008-06-06 18:40:29 +10002626 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002627 for (i = 0; i < *nperms; i++)
2628 kfree((*perms)[i]);
2629 kfree(*perms);
2630 return rc;
2631}
2632
Eric Paris3f120702007-09-21 14:37:10 -04002633int security_get_reject_unknown(void)
2634{
2635 return policydb.reject_unknown;
2636}
2637
2638int security_get_allow_unknown(void)
2639{
2640 return policydb.allow_unknown;
2641}
2642
Paul Moore3bb56b22008-01-29 08:38:19 -05002643/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002644 * security_policycap_supported - Check for a specific policy capability
2645 * @req_cap: capability
2646 *
2647 * Description:
2648 * This function queries the currently loaded policy to see if it supports the
2649 * capability specified by @req_cap. Returns true (1) if the capability is
2650 * supported, false (0) if it isn't supported.
2651 *
2652 */
2653int security_policycap_supported(unsigned int req_cap)
2654{
2655 int rc;
2656
James Morris0804d112008-06-06 18:40:29 +10002657 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002658 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10002659 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002660
2661 return rc;
2662}
2663
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002664struct selinux_audit_rule {
2665 u32 au_seqno;
2666 struct context au_ctxt;
2667};
2668
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002669void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002670{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002671 struct selinux_audit_rule *rule = vrule;
2672
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002673 if (rule) {
2674 context_destroy(&rule->au_ctxt);
2675 kfree(rule);
2676 }
2677}
2678
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002679int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002680{
2681 struct selinux_audit_rule *tmprule;
2682 struct role_datum *roledatum;
2683 struct type_datum *typedatum;
2684 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002685 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002686 int rc = 0;
2687
2688 *rule = NULL;
2689
2690 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002691 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002692
2693 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002694 case AUDIT_SUBJ_USER:
2695 case AUDIT_SUBJ_ROLE:
2696 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002697 case AUDIT_OBJ_USER:
2698 case AUDIT_OBJ_ROLE:
2699 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002700 /* only 'equals' and 'not equals' fit user, role, and type */
Al Viro5af75d82008-12-16 05:59:26 -05002701 if (op != Audit_equal && op != Audit_not_equal)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002702 return -EINVAL;
2703 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002704 case AUDIT_SUBJ_SEN:
2705 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002706 case AUDIT_OBJ_LEV_LOW:
2707 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002708 /* we do not allow a range, indicated by the presense of '-' */
2709 if (strchr(rulestr, '-'))
2710 return -EINVAL;
2711 break;
2712 default:
2713 /* only the above fields are valid */
2714 return -EINVAL;
2715 }
2716
2717 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2718 if (!tmprule)
2719 return -ENOMEM;
2720
2721 context_init(&tmprule->au_ctxt);
2722
James Morris0804d112008-06-06 18:40:29 +10002723 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002724
2725 tmprule->au_seqno = latest_granting;
2726
2727 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002728 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002729 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002730 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2731 if (!userdatum)
2732 rc = -EINVAL;
2733 else
2734 tmprule->au_ctxt.user = userdatum->value;
2735 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002736 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002737 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002738 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2739 if (!roledatum)
2740 rc = -EINVAL;
2741 else
2742 tmprule->au_ctxt.role = roledatum->value;
2743 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002744 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002745 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002746 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2747 if (!typedatum)
2748 rc = -EINVAL;
2749 else
2750 tmprule->au_ctxt.type = typedatum->value;
2751 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002752 case AUDIT_SUBJ_SEN:
2753 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002754 case AUDIT_OBJ_LEV_LOW:
2755 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002756 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2757 break;
2758 }
2759
James Morris0804d112008-06-06 18:40:29 +10002760 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002761
2762 if (rc) {
2763 selinux_audit_rule_free(tmprule);
2764 tmprule = NULL;
2765 }
2766
2767 *rule = tmprule;
2768
2769 return rc;
2770}
2771
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002772/* Check to see if the rule contains any selinux fields */
2773int selinux_audit_rule_known(struct audit_krule *rule)
2774{
2775 int i;
2776
2777 for (i = 0; i < rule->field_count; i++) {
2778 struct audit_field *f = &rule->fields[i];
2779 switch (f->type) {
2780 case AUDIT_SUBJ_USER:
2781 case AUDIT_SUBJ_ROLE:
2782 case AUDIT_SUBJ_TYPE:
2783 case AUDIT_SUBJ_SEN:
2784 case AUDIT_SUBJ_CLR:
2785 case AUDIT_OBJ_USER:
2786 case AUDIT_OBJ_ROLE:
2787 case AUDIT_OBJ_TYPE:
2788 case AUDIT_OBJ_LEV_LOW:
2789 case AUDIT_OBJ_LEV_HIGH:
2790 return 1;
2791 }
2792 }
2793
2794 return 0;
2795}
2796
2797int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04002798 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002799{
2800 struct context *ctxt;
2801 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002802 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002803 int match = 0;
2804
2805 if (!rule) {
2806 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002807 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002808 return -ENOENT;
2809 }
2810
James Morris0804d112008-06-06 18:40:29 +10002811 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002812
2813 if (rule->au_seqno < latest_granting) {
2814 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002815 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002816 match = -ESTALE;
2817 goto out;
2818 }
2819
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002820 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002821 if (!ctxt) {
2822 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002823 "selinux_audit_rule_match: unrecognized SID %d\n",
2824 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002825 match = -ENOENT;
2826 goto out;
2827 }
2828
2829 /* a field/op pair that is not caught here will simply fall through
2830 without a match */
2831 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002832 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002833 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002834 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002835 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002836 match = (ctxt->user == rule->au_ctxt.user);
2837 break;
Al Viro5af75d82008-12-16 05:59:26 -05002838 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002839 match = (ctxt->user != rule->au_ctxt.user);
2840 break;
2841 }
2842 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002843 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002844 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002845 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002846 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002847 match = (ctxt->role == rule->au_ctxt.role);
2848 break;
Al Viro5af75d82008-12-16 05:59:26 -05002849 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002850 match = (ctxt->role != rule->au_ctxt.role);
2851 break;
2852 }
2853 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002854 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002855 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002856 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002857 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002858 match = (ctxt->type == rule->au_ctxt.type);
2859 break;
Al Viro5af75d82008-12-16 05:59:26 -05002860 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002861 match = (ctxt->type != rule->au_ctxt.type);
2862 break;
2863 }
2864 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002865 case AUDIT_SUBJ_SEN:
2866 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002867 case AUDIT_OBJ_LEV_LOW:
2868 case AUDIT_OBJ_LEV_HIGH:
2869 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002870 field == AUDIT_OBJ_LEV_LOW) ?
2871 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002872 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002873 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002874 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002875 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002876 break;
Al Viro5af75d82008-12-16 05:59:26 -05002877 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002878 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002879 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002880 break;
Al Viro5af75d82008-12-16 05:59:26 -05002881 case Audit_lt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002882 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002883 level) &&
2884 !mls_level_eq(&rule->au_ctxt.range.level[0],
2885 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002886 break;
Al Viro5af75d82008-12-16 05:59:26 -05002887 case Audit_le:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002888 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002889 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002890 break;
Al Viro5af75d82008-12-16 05:59:26 -05002891 case Audit_gt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002892 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002893 &rule->au_ctxt.range.level[0]) &&
2894 !mls_level_eq(level,
2895 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002896 break;
Al Viro5af75d82008-12-16 05:59:26 -05002897 case Audit_ge:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002898 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002899 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002900 break;
2901 }
2902 }
2903
2904out:
James Morris0804d112008-06-06 18:40:29 +10002905 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002906 return match;
2907}
2908
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002909static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002910
2911static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
Eric Parisf5269712008-05-14 11:27:45 -04002912 u16 class, u32 perms, u32 *retained)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002913{
2914 int err = 0;
2915
2916 if (event == AVC_CALLBACK_RESET && aurule_callback)
2917 err = aurule_callback();
2918 return err;
2919}
2920
2921static int __init aurule_init(void)
2922{
2923 int err;
2924
2925 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002926 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002927 if (err)
2928 panic("avc_add_callback() failed, error %d\n", err);
2929
2930 return err;
2931}
2932__initcall(aurule_init);
2933
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002934#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002935/**
Paul Moore5778eab2007-02-28 15:14:22 -05002936 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2937 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002938 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002939 *
2940 * Description:
2941 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002942 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2943 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002944 *
2945 */
Paul Moore5778eab2007-02-28 15:14:22 -05002946static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002947 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002948{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002949 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002950
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002951 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2952 if (sid_cache == NULL)
2953 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002954 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002955 if (secattr->cache == NULL) {
2956 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002957 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002958 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002959
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002960 *sid_cache = sid;
2961 secattr->cache->free = kfree;
2962 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002963 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002964}
2965
2966/**
Paul Moore5778eab2007-02-28 15:14:22 -05002967 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002968 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002969 * @sid: the SELinux SID
2970 *
2971 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002972 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002973 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002974 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2975 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2976 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2977 * conversion for future lookups. Returns zero on success, negative values on
2978 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002979 *
2980 */
Paul Moore5778eab2007-02-28 15:14:22 -05002981int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002982 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002983{
2984 int rc = -EIDRM;
2985 struct context *ctx;
2986 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002987
2988 if (!ss_initialized) {
2989 *sid = SECSID_NULL;
2990 return 0;
2991 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002992
James Morris0804d112008-06-06 18:40:29 +10002993 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002994
Paul Moore701a90b2006-11-17 17:38:46 -05002995 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002996 *sid = *(u32 *)secattr->cache->data;
2997 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002998 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2999 *sid = secattr->attr.secid;
3000 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05003001 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003002 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003003 if (ctx == NULL)
3004 goto netlbl_secattr_to_sid_return;
3005
Paul Moore81990fb2008-10-03 10:51:15 -04003006 context_init(&ctx_new);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003007 ctx_new.user = ctx->user;
3008 ctx_new.role = ctx->role;
3009 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05003010 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05003011 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05003012 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05003013 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003014 goto netlbl_secattr_to_sid_return;
Paul Moore81990fb2008-10-03 10:51:15 -04003015 memcpy(&ctx_new.range.level[1].cat,
3016 &ctx_new.range.level[0].cat,
3017 sizeof(ctx_new.range.level[0].cat));
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003018 }
3019 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
3020 goto netlbl_secattr_to_sid_return_cleanup;
3021
3022 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
3023 if (rc != 0)
3024 goto netlbl_secattr_to_sid_return_cleanup;
3025
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003026 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05003027
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003028 ebitmap_destroy(&ctx_new.range.level[0].cat);
3029 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04003030 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003031 rc = 0;
3032 }
3033
3034netlbl_secattr_to_sid_return:
James Morris0804d112008-06-06 18:40:29 +10003035 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003036 return rc;
3037netlbl_secattr_to_sid_return_cleanup:
3038 ebitmap_destroy(&ctx_new.range.level[0].cat);
3039 goto netlbl_secattr_to_sid_return;
3040}
3041
3042/**
Paul Moore5778eab2007-02-28 15:14:22 -05003043 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3044 * @sid: the SELinux SID
3045 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003046 *
3047 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05003048 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3049 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003050 *
3051 */
Paul Moore5778eab2007-02-28 15:14:22 -05003052int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003053{
Paul Moore99d854d2008-10-10 10:16:30 -04003054 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003055 struct context *ctx;
3056
3057 if (!ss_initialized)
3058 return 0;
3059
James Morris0804d112008-06-06 18:40:29 +10003060 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003061 ctx = sidtab_search(&sidtab, sid);
Paul Moore99d854d2008-10-10 10:16:30 -04003062 if (ctx == NULL) {
3063 rc = -ENOENT;
Paul Moore5778eab2007-02-28 15:14:22 -05003064 goto netlbl_sid_to_secattr_failure;
Paul Moore99d854d2008-10-10 10:16:30 -04003065 }
Paul Moore5778eab2007-02-28 15:14:22 -05003066 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
3067 GFP_ATOMIC);
Paul Moore99d854d2008-10-10 10:16:30 -04003068 if (secattr->domain == NULL) {
3069 rc = -ENOMEM;
3070 goto netlbl_sid_to_secattr_failure;
3071 }
Paul Moore8d758992008-10-10 10:16:33 -04003072 secattr->attr.secid = sid;
3073 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
Paul Moore5778eab2007-02-28 15:14:22 -05003074 mls_export_netlbl_lvl(ctx, secattr);
3075 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04003076 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05003077 goto netlbl_sid_to_secattr_failure;
James Morris0804d112008-06-06 18:40:29 +10003078 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003079
Paul Moore5778eab2007-02-28 15:14:22 -05003080 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05003081
Paul Moore5778eab2007-02-28 15:14:22 -05003082netlbl_sid_to_secattr_failure:
James Morris0804d112008-06-06 18:40:29 +10003083 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08003084 return rc;
3085}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003086#endif /* CONFIG_NETLABEL */