blob: 4a2bf212057b6eacf3d7a8e4a5dfb10c54e755e5 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Paul Moore3bb56b22008-01-29 08:38:19 -050069int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050070int selinux_policycap_openperm;
Paul Moore3bb56b22008-01-29 08:38:19 -050071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static DEFINE_RWLOCK(policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static struct sidtab sidtab;
75struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040076int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78/*
79 * The largest sequence number that has been used when
80 * providing an access decision to the access vector cache.
81 * The sequence number only changes when a policy change
82 * occurs.
83 */
Eric Paris5d55a342008-04-18 17:38:33 -040084static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86/* Forward declaration. */
87static int context_struct_to_string(struct context *context, char **scontext,
88 u32 *scontext_len);
89
Stephen Smalley19439d02010-01-14 17:28:10 -050090static void context_struct_compute_av(struct context *scontext,
91 struct context *tcontext,
92 u16 tclass,
93 struct av_decision *avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -040094
95struct selinux_mapping {
96 u16 value; /* policy value */
97 unsigned num_perms;
98 u32 perms[sizeof(u32) * 8];
99};
100
101static struct selinux_mapping *current_mapping;
102static u16 current_mapping_size;
103
104static int selinux_set_mapping(struct policydb *pol,
105 struct security_class_mapping *map,
106 struct selinux_mapping **out_map_p,
107 u16 *out_map_size)
108{
109 struct selinux_mapping *out_map = NULL;
110 size_t size = sizeof(struct selinux_mapping);
111 u16 i, j;
112 unsigned k;
113 bool print_unknown_handle = false;
114
115 /* Find number of classes in the input mapping */
116 if (!map)
117 return -EINVAL;
118 i = 0;
119 while (map[i].name)
120 i++;
121
122 /* Allocate space for the class records, plus one for class zero */
123 out_map = kcalloc(++i, size, GFP_ATOMIC);
124 if (!out_map)
125 return -ENOMEM;
126
127 /* Store the raw class and permission values */
128 j = 0;
129 while (map[j].name) {
130 struct security_class_mapping *p_in = map + (j++);
131 struct selinux_mapping *p_out = out_map + j;
132
133 /* An empty class string skips ahead */
134 if (!strcmp(p_in->name, "")) {
135 p_out->num_perms = 0;
136 continue;
137 }
138
139 p_out->value = string_to_security_class(pol, p_in->name);
140 if (!p_out->value) {
141 printk(KERN_INFO
142 "SELinux: Class %s not defined in policy.\n",
143 p_in->name);
144 if (pol->reject_unknown)
145 goto err;
146 p_out->num_perms = 0;
147 print_unknown_handle = true;
148 continue;
149 }
150
151 k = 0;
152 while (p_in->perms && p_in->perms[k]) {
153 /* An empty permission string skips ahead */
154 if (!*p_in->perms[k]) {
155 k++;
156 continue;
157 }
158 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
159 p_in->perms[k]);
160 if (!p_out->perms[k]) {
161 printk(KERN_INFO
162 "SELinux: Permission %s in class %s not defined in policy.\n",
163 p_in->perms[k], p_in->name);
164 if (pol->reject_unknown)
165 goto err;
166 print_unknown_handle = true;
167 }
168
169 k++;
170 }
171 p_out->num_perms = k;
172 }
173
174 if (print_unknown_handle)
175 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
176 pol->allow_unknown ? "allowed" : "denied");
177
178 *out_map_p = out_map;
179 *out_map_size = i;
180 return 0;
181err:
182 kfree(out_map);
183 return -EINVAL;
184}
185
186/*
187 * Get real, policy values from mapped values
188 */
189
190static u16 unmap_class(u16 tclass)
191{
192 if (tclass < current_mapping_size)
193 return current_mapping[tclass].value;
194
195 return tclass;
196}
197
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400198static void map_decision(u16 tclass, struct av_decision *avd,
199 int allow_unknown)
200{
201 if (tclass < current_mapping_size) {
202 unsigned i, n = current_mapping[tclass].num_perms;
203 u32 result;
204
205 for (i = 0, result = 0; i < n; i++) {
206 if (avd->allowed & current_mapping[tclass].perms[i])
207 result |= 1<<i;
208 if (allow_unknown && !current_mapping[tclass].perms[i])
209 result |= 1<<i;
210 }
211 avd->allowed = result;
212
213 for (i = 0, result = 0; i < n; i++)
214 if (avd->auditallow & current_mapping[tclass].perms[i])
215 result |= 1<<i;
216 avd->auditallow = result;
217
218 for (i = 0, result = 0; i < n; i++) {
219 if (avd->auditdeny & current_mapping[tclass].perms[i])
220 result |= 1<<i;
221 if (!allow_unknown && !current_mapping[tclass].perms[i])
222 result |= 1<<i;
223 }
Eric Paris0bce9522009-11-23 16:47:23 -0500224 /*
225 * In case the kernel has a bug and requests a permission
226 * between num_perms and the maximum permission number, we
227 * should audit that denial
228 */
229 for (; i < (sizeof(u32)*8); i++)
230 result |= 1<<i;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400231 avd->auditdeny = result;
232 }
233}
234
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
237 * Return the boolean value of a constraint expression
238 * when it is applied to the specified source and target
239 * security contexts.
240 *
241 * xcontext is a special beast... It is used by the validatetrans rules
242 * only. For these rules, scontext is the context before the transition,
243 * tcontext is the context after the transition, and xcontext is the context
244 * of the process performing the transition. All other callers of
245 * constraint_expr_eval should pass in NULL for xcontext.
246 */
247static int constraint_expr_eval(struct context *scontext,
248 struct context *tcontext,
249 struct context *xcontext,
250 struct constraint_expr *cexpr)
251{
252 u32 val1, val2;
253 struct context *c;
254 struct role_datum *r1, *r2;
255 struct mls_level *l1, *l2;
256 struct constraint_expr *e;
257 int s[CEXPR_MAXDEPTH];
258 int sp = -1;
259
260 for (e = cexpr; e; e = e->next) {
261 switch (e->expr_type) {
262 case CEXPR_NOT:
263 BUG_ON(sp < 0);
264 s[sp] = !s[sp];
265 break;
266 case CEXPR_AND:
267 BUG_ON(sp < 1);
268 sp--;
269 s[sp] &= s[sp+1];
270 break;
271 case CEXPR_OR:
272 BUG_ON(sp < 1);
273 sp--;
274 s[sp] |= s[sp+1];
275 break;
276 case CEXPR_ATTR:
277 if (sp == (CEXPR_MAXDEPTH-1))
278 return 0;
279 switch (e->attr) {
280 case CEXPR_USER:
281 val1 = scontext->user;
282 val2 = tcontext->user;
283 break;
284 case CEXPR_TYPE:
285 val1 = scontext->type;
286 val2 = tcontext->type;
287 break;
288 case CEXPR_ROLE:
289 val1 = scontext->role;
290 val2 = tcontext->role;
291 r1 = policydb.role_val_to_struct[val1 - 1];
292 r2 = policydb.role_val_to_struct[val2 - 1];
293 switch (e->op) {
294 case CEXPR_DOM:
295 s[++sp] = ebitmap_get_bit(&r1->dominates,
296 val2 - 1);
297 continue;
298 case CEXPR_DOMBY:
299 s[++sp] = ebitmap_get_bit(&r2->dominates,
300 val1 - 1);
301 continue;
302 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400303 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
304 val2 - 1) &&
305 !ebitmap_get_bit(&r2->dominates,
306 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 continue;
308 default:
309 break;
310 }
311 break;
312 case CEXPR_L1L2:
313 l1 = &(scontext->range.level[0]);
314 l2 = &(tcontext->range.level[0]);
315 goto mls_ops;
316 case CEXPR_L1H2:
317 l1 = &(scontext->range.level[0]);
318 l2 = &(tcontext->range.level[1]);
319 goto mls_ops;
320 case CEXPR_H1L2:
321 l1 = &(scontext->range.level[1]);
322 l2 = &(tcontext->range.level[0]);
323 goto mls_ops;
324 case CEXPR_H1H2:
325 l1 = &(scontext->range.level[1]);
326 l2 = &(tcontext->range.level[1]);
327 goto mls_ops;
328 case CEXPR_L1H1:
329 l1 = &(scontext->range.level[0]);
330 l2 = &(scontext->range.level[1]);
331 goto mls_ops;
332 case CEXPR_L2H2:
333 l1 = &(tcontext->range.level[0]);
334 l2 = &(tcontext->range.level[1]);
335 goto mls_ops;
336mls_ops:
337 switch (e->op) {
338 case CEXPR_EQ:
339 s[++sp] = mls_level_eq(l1, l2);
340 continue;
341 case CEXPR_NEQ:
342 s[++sp] = !mls_level_eq(l1, l2);
343 continue;
344 case CEXPR_DOM:
345 s[++sp] = mls_level_dom(l1, l2);
346 continue;
347 case CEXPR_DOMBY:
348 s[++sp] = mls_level_dom(l2, l1);
349 continue;
350 case CEXPR_INCOMP:
351 s[++sp] = mls_level_incomp(l2, l1);
352 continue;
353 default:
354 BUG();
355 return 0;
356 }
357 break;
358 default:
359 BUG();
360 return 0;
361 }
362
363 switch (e->op) {
364 case CEXPR_EQ:
365 s[++sp] = (val1 == val2);
366 break;
367 case CEXPR_NEQ:
368 s[++sp] = (val1 != val2);
369 break;
370 default:
371 BUG();
372 return 0;
373 }
374 break;
375 case CEXPR_NAMES:
376 if (sp == (CEXPR_MAXDEPTH-1))
377 return 0;
378 c = scontext;
379 if (e->attr & CEXPR_TARGET)
380 c = tcontext;
381 else if (e->attr & CEXPR_XTARGET) {
382 c = xcontext;
383 if (!c) {
384 BUG();
385 return 0;
386 }
387 }
388 if (e->attr & CEXPR_USER)
389 val1 = c->user;
390 else if (e->attr & CEXPR_ROLE)
391 val1 = c->role;
392 else if (e->attr & CEXPR_TYPE)
393 val1 = c->type;
394 else {
395 BUG();
396 return 0;
397 }
398
399 switch (e->op) {
400 case CEXPR_EQ:
401 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
402 break;
403 case CEXPR_NEQ:
404 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
405 break;
406 default:
407 BUG();
408 return 0;
409 }
410 break;
411 default:
412 BUG();
413 return 0;
414 }
415 }
416
417 BUG_ON(sp != 0);
418 return s[0];
419}
420
421/*
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900422 * security_dump_masked_av - dumps masked permissions during
423 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
424 */
425static int dump_masked_av_helper(void *k, void *d, void *args)
426{
427 struct perm_datum *pdatum = d;
428 char **permission_names = args;
429
430 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
431
432 permission_names[pdatum->value - 1] = (char *)k;
433
434 return 0;
435}
436
437static void security_dump_masked_av(struct context *scontext,
438 struct context *tcontext,
439 u16 tclass,
440 u32 permissions,
441 const char *reason)
442{
443 struct common_datum *common_dat;
444 struct class_datum *tclass_dat;
445 struct audit_buffer *ab;
446 char *tclass_name;
447 char *scontext_name = NULL;
448 char *tcontext_name = NULL;
449 char *permission_names[32];
450 int index, length;
451 bool need_comma = false;
452
453 if (!permissions)
454 return;
455
456 tclass_name = policydb.p_class_val_to_name[tclass - 1];
457 tclass_dat = policydb.class_val_to_struct[tclass - 1];
458 common_dat = tclass_dat->comdatum;
459
460 /* init permission_names */
461 if (common_dat &&
462 hashtab_map(common_dat->permissions.table,
463 dump_masked_av_helper, permission_names) < 0)
464 goto out;
465
466 if (hashtab_map(tclass_dat->permissions.table,
467 dump_masked_av_helper, permission_names) < 0)
468 goto out;
469
470 /* get scontext/tcontext in text form */
471 if (context_struct_to_string(scontext,
472 &scontext_name, &length) < 0)
473 goto out;
474
475 if (context_struct_to_string(tcontext,
476 &tcontext_name, &length) < 0)
477 goto out;
478
479 /* audit a message */
480 ab = audit_log_start(current->audit_context,
481 GFP_ATOMIC, AUDIT_SELINUX_ERR);
482 if (!ab)
483 goto out;
484
485 audit_log_format(ab, "op=security_compute_av reason=%s "
486 "scontext=%s tcontext=%s tclass=%s perms=",
487 reason, scontext_name, tcontext_name, tclass_name);
488
489 for (index = 0; index < 32; index++) {
490 u32 mask = (1 << index);
491
492 if ((mask & permissions) == 0)
493 continue;
494
495 audit_log_format(ab, "%s%s",
496 need_comma ? "," : "",
497 permission_names[index]
498 ? permission_names[index] : "????");
499 need_comma = true;
500 }
501 audit_log_end(ab);
502out:
503 /* release scontext/tcontext */
504 kfree(tcontext_name);
505 kfree(scontext_name);
506
507 return;
508}
509
510/*
KaiGai Koheid9250de2008-08-28 16:35:57 +0900511 * security_boundary_permission - drops violated permissions
512 * on boundary constraint.
513 */
514static void type_attribute_bounds_av(struct context *scontext,
515 struct context *tcontext,
516 u16 tclass,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900517 struct av_decision *avd)
518{
KaiGai Koheid9250de2008-08-28 16:35:57 +0900519 struct type_datum *source
520 = policydb.type_val_to_struct[scontext->type - 1];
KaiGai Koheid9250de2008-08-28 16:35:57 +0900521
522 if (source->bounds) {
KaiGai Kohei7d52a152010-01-21 15:00:15 +0900523 struct context lo_scontext;
524 struct av_decision lo_avd;
525 u32 masked;
526
KaiGai Koheid9250de2008-08-28 16:35:57 +0900527 memset(&lo_avd, 0, sizeof(lo_avd));
528
529 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
530 lo_scontext.type = source->bounds;
531
532 context_struct_compute_av(&lo_scontext,
533 tcontext,
534 tclass,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900535 &lo_avd);
536 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
537 return; /* no masked permission */
538 masked = ~lo_avd.allowed & avd->allowed;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900539
KaiGai Koheid9250de2008-08-28 16:35:57 +0900540 /* mask violated permissions */
541 avd->allowed &= ~masked;
542
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900543 /* audit masked permissions */
544 security_dump_masked_av(scontext, tcontext,
545 tclass, masked, "bounds");
KaiGai Koheid9250de2008-08-28 16:35:57 +0900546 }
547}
548
549/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 * Compute access vectors based on a context structure pair for
551 * the permissions in a particular class.
552 */
Stephen Smalley19439d02010-01-14 17:28:10 -0500553static void context_struct_compute_av(struct context *scontext,
554 struct context *tcontext,
555 u16 tclass,
556 struct av_decision *avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct constraint_node *constraint;
559 struct role_allow *ra;
560 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700561 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700563 struct ebitmap *sattr, *tattr;
564 struct ebitmap_node *snode, *tnode;
565 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 avd->allowed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 avd->auditallow = 0;
569 avd->auditdeny = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400571 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
572 if (printk_ratelimit())
573 printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
Stephen Smalley19439d02010-01-14 17:28:10 -0500574 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400575 }
Eric Paris3f120702007-09-21 14:37:10 -0400576
577 tclass_datum = policydb.class_val_to_struct[tclass - 1];
578
579 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * If a specific type enforcement rule was defined for
581 * this permission check, then use it.
582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700584 avkey.specified = AVTAB_AV;
585 sattr = &policydb.type_attr_map[scontext->type - 1];
586 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900587 ebitmap_for_each_positive_bit(sattr, snode, i) {
588 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700589 avkey.source_type = i + 1;
590 avkey.target_type = j + 1;
591 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300592 node;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700593 node = avtab_search_node_next(node, avkey.specified)) {
594 if (node->key.specified == AVTAB_ALLOWED)
595 avd->allowed |= node->datum.data;
596 else if (node->key.specified == AVTAB_AUDITALLOW)
597 avd->auditallow |= node->datum.data;
598 else if (node->key.specified == AVTAB_AUDITDENY)
599 avd->auditdeny &= node->datum.data;
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Stephen Smalley782ebb92005-09-03 15:55:16 -0700602 /* Check conditional av table for additional permissions */
603 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
604
605 }
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /*
609 * Remove any permissions prohibited by a constraint (this includes
610 * the MLS policy).
611 */
612 constraint = tclass_datum->constraints;
613 while (constraint) {
614 if ((constraint->permissions & (avd->allowed)) &&
615 !constraint_expr_eval(scontext, tcontext, NULL,
616 constraint->expr)) {
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900617 avd->allowed &= ~(constraint->permissions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 constraint = constraint->next;
620 }
621
622 /*
623 * If checking process transition permission and the
624 * role is changing, then check the (current_role, new_role)
625 * pair.
626 */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400627 if (tclass == policydb.process_class &&
628 (avd->allowed & policydb.process_trans_perms) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 scontext->role != tcontext->role) {
630 for (ra = policydb.role_allow; ra; ra = ra->next) {
631 if (scontext->role == ra->role &&
632 tcontext->role == ra->new_role)
633 break;
634 }
635 if (!ra)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400636 avd->allowed &= ~policydb.process_trans_perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
KaiGai Koheid9250de2008-08-28 16:35:57 +0900639 /*
640 * If the given source and target types have boundary
641 * constraint, lazy checks have to mask any violated
642 * permission and notice it to userspace via audit.
643 */
644 type_attribute_bounds_av(scontext, tcontext,
Stephen Smalley19439d02010-01-14 17:28:10 -0500645 tclass, avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400649 struct context *ncontext,
650 struct context *tcontext,
651 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 char *o = NULL, *n = NULL, *t = NULL;
654 u32 olen, nlen, tlen;
655
656 if (context_struct_to_string(ocontext, &o, &olen) < 0)
657 goto out;
658 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
659 goto out;
660 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
661 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100662 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400663 "security_validate_transition: denied for"
664 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
665 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666out:
667 kfree(o);
668 kfree(n);
669 kfree(t);
670
671 if (!selinux_enforcing)
672 return 0;
673 return -EPERM;
674}
675
676int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400677 u16 orig_tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct context *ocontext;
680 struct context *ncontext;
681 struct context *tcontext;
682 struct class_datum *tclass_datum;
683 struct constraint_node *constraint;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400684 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 int rc = 0;
686
687 if (!ss_initialized)
688 return 0;
689
James Morris0804d112008-06-06 18:40:29 +1000690 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400692 tclass = unmap_class(orig_tclass);
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400695 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
696 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 rc = -EINVAL;
698 goto out;
699 }
700 tclass_datum = policydb.class_val_to_struct[tclass - 1];
701
702 ocontext = sidtab_search(&sidtab, oldsid);
703 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400704 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
705 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 rc = -EINVAL;
707 goto out;
708 }
709
710 ncontext = sidtab_search(&sidtab, newsid);
711 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400712 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
713 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 rc = -EINVAL;
715 goto out;
716 }
717
718 tcontext = sidtab_search(&sidtab, tasksid);
719 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400720 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
721 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 rc = -EINVAL;
723 goto out;
724 }
725
726 constraint = tclass_datum->validatetrans;
727 while (constraint) {
728 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400729 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400731 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 goto out;
733 }
734 constraint = constraint->next;
735 }
736
737out:
James Morris0804d112008-06-06 18:40:29 +1000738 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return rc;
740}
741
KaiGai Koheid9250de2008-08-28 16:35:57 +0900742/*
743 * security_bounded_transition - check whether the given
744 * transition is directed to bounded, or not.
745 * It returns 0, if @newsid is bounded by @oldsid.
746 * Otherwise, it returns error code.
747 *
748 * @oldsid : current security identifier
749 * @newsid : destinated security identifier
750 */
751int security_bounded_transition(u32 old_sid, u32 new_sid)
752{
753 struct context *old_context, *new_context;
754 struct type_datum *type;
755 int index;
756 int rc = -EINVAL;
757
758 read_lock(&policy_rwlock);
759
760 old_context = sidtab_search(&sidtab, old_sid);
761 if (!old_context) {
762 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
763 __func__, old_sid);
764 goto out;
765 }
766
767 new_context = sidtab_search(&sidtab, new_sid);
768 if (!new_context) {
769 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
770 __func__, new_sid);
771 goto out;
772 }
773
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200774 /* type/domain unchanged */
KaiGai Koheid9250de2008-08-28 16:35:57 +0900775 if (old_context->type == new_context->type) {
776 rc = 0;
777 goto out;
778 }
779
780 index = new_context->type;
781 while (true) {
782 type = policydb.type_val_to_struct[index - 1];
783 BUG_ON(!type);
784
785 /* not bounded anymore */
786 if (!type->bounds) {
787 rc = -EPERM;
788 break;
789 }
790
791 /* @newsid is bounded by @oldsid */
792 if (type->bounds == old_context->type) {
793 rc = 0;
794 break;
795 }
796 index = type->bounds;
797 }
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900798
799 if (rc) {
800 char *old_name = NULL;
801 char *new_name = NULL;
802 int length;
803
804 if (!context_struct_to_string(old_context,
805 &old_name, &length) &&
806 !context_struct_to_string(new_context,
807 &new_name, &length)) {
808 audit_log(current->audit_context,
809 GFP_ATOMIC, AUDIT_SELINUX_ERR,
810 "op=security_bounded_transition "
811 "result=denied "
812 "oldcontext=%s newcontext=%s",
813 old_name, new_name);
814 }
815 kfree(new_name);
816 kfree(old_name);
817 }
KaiGai Koheid9250de2008-08-28 16:35:57 +0900818out:
819 read_unlock(&policy_rwlock);
820
821 return rc;
822}
823
Stephen Smalley19439d02010-01-14 17:28:10 -0500824static void avd_init(struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400825{
Stephen Smalley19439d02010-01-14 17:28:10 -0500826 avd->allowed = 0;
827 avd->auditallow = 0;
828 avd->auditdeny = 0xffffffff;
829 avd->seqno = latest_granting;
830 avd->flags = 0;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400831}
832
Stephen Smalley19439d02010-01-14 17:28:10 -0500833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/**
835 * security_compute_av - Compute access vector decisions.
836 * @ssid: source security identifier
837 * @tsid: target security identifier
838 * @tclass: target security class
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 * @avd: access vector decisions
840 *
841 * Compute a set of access vector decisions based on the
842 * SID pair (@ssid, @tsid) for the permissions in @tclass.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
Stephen Smalley19439d02010-01-14 17:28:10 -0500844void security_compute_av(u32 ssid,
845 u32 tsid,
846 u16 orig_tclass,
847 struct av_decision *avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400849 u16 tclass;
Stephen Smalley19439d02010-01-14 17:28:10 -0500850 struct context *scontext = NULL, *tcontext = NULL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400851
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400852 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500853 avd_init(avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400854 if (!ss_initialized)
855 goto allow;
856
Stephen Smalley19439d02010-01-14 17:28:10 -0500857 scontext = sidtab_search(&sidtab, ssid);
858 if (!scontext) {
859 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
860 __func__, ssid);
861 goto out;
862 }
863
864 /* permissive domain? */
865 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
866 avd->flags |= AVD_FLAGS_PERMISSIVE;
867
868 tcontext = sidtab_search(&sidtab, tsid);
869 if (!tcontext) {
870 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
871 __func__, tsid);
872 goto out;
873 }
874
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400875 tclass = unmap_class(orig_tclass);
876 if (unlikely(orig_tclass && !tclass)) {
877 if (policydb.allow_unknown)
878 goto allow;
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400879 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400880 }
Stephen Smalley19439d02010-01-14 17:28:10 -0500881 context_struct_compute_av(scontext, tcontext, tclass, avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400882 map_decision(orig_tclass, avd, policydb.allow_unknown);
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400883out:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400884 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500885 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400886allow:
887 avd->allowed = 0xffffffff;
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400888 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400889}
890
Stephen Smalley19439d02010-01-14 17:28:10 -0500891void security_compute_av_user(u32 ssid,
892 u32 tsid,
893 u16 tclass,
894 struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400895{
Stephen Smalley19439d02010-01-14 17:28:10 -0500896 struct context *scontext = NULL, *tcontext = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
James Morris0804d112008-06-06 18:40:29 +1000898 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500899 avd_init(avd);
900 if (!ss_initialized)
901 goto allow;
902
903 scontext = sidtab_search(&sidtab, ssid);
904 if (!scontext) {
905 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
906 __func__, ssid);
907 goto out;
908 }
909
910 /* permissive domain? */
911 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
912 avd->flags |= AVD_FLAGS_PERMISSIVE;
913
914 tcontext = sidtab_search(&sidtab, tsid);
915 if (!tcontext) {
916 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
917 __func__, tsid);
918 goto out;
919 }
920
921 if (unlikely(!tclass)) {
922 if (policydb.allow_unknown)
923 goto allow;
924 goto out;
925 }
926
927 context_struct_compute_av(scontext, tcontext, tclass, avd);
928 out:
James Morris0804d112008-06-06 18:40:29 +1000929 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500930 return;
931allow:
932 avd->allowed = 0xffffffff;
933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/*
937 * Write the security context string representation of
938 * the context structure `context' into a dynamically
939 * allocated string of the correct size. Set `*scontext'
940 * to point to this string and set `*scontext_len' to
941 * the length of the string.
942 */
943static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
944{
945 char *scontextp;
946
947 *scontext = NULL;
948 *scontext_len = 0;
949
Stephen Smalley12b29f32008-05-07 13:03:20 -0400950 if (context->len) {
951 *scontext_len = context->len;
952 *scontext = kstrdup(context->str, GFP_ATOMIC);
953 if (!(*scontext))
954 return -ENOMEM;
955 return 0;
956 }
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* Compute the size of the context. */
959 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
960 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
961 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
962 *scontext_len += mls_compute_context_len(context);
963
964 /* Allocate space for the context; caller must free this space. */
965 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400966 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 *scontext = scontextp;
969
970 /*
971 * Copy the user name, role name and type name into the context.
972 */
973 sprintf(scontextp, "%s:%s:%s",
974 policydb.p_user_val_to_name[context->user - 1],
975 policydb.p_role_val_to_name[context->role - 1],
976 policydb.p_type_val_to_name[context->type - 1]);
977 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400978 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
979 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 mls_sid_to_context(context, &scontextp);
982
983 *scontextp = 0;
984
985 return 0;
986}
987
988#include "initial_sid_to_string.h"
989
James Carterf0ee2e42007-04-04 10:11:29 -0400990const char *security_get_initial_sid_context(u32 sid)
991{
992 if (unlikely(sid > SECINITSID_NUM))
993 return NULL;
994 return initial_sid_to_string[sid];
995}
996
Stephen Smalley12b29f32008-05-07 13:03:20 -0400997static int security_sid_to_context_core(u32 sid, char **scontext,
998 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 struct context *context;
1001 int rc = 0;
1002
Stephen Smalley4f4acf32007-02-26 12:02:34 -05001003 *scontext = NULL;
1004 *scontext_len = 0;
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!ss_initialized) {
1007 if (sid <= SECINITSID_NUM) {
1008 char *scontextp;
1009
1010 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001011 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca02006-05-15 09:43:48 -07001012 if (!scontextp) {
1013 rc = -ENOMEM;
1014 goto out;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 strcpy(scontextp, initial_sid_to_string[sid]);
1017 *scontext = scontextp;
1018 goto out;
1019 }
Eric Paris744ba352008-04-17 11:52:44 -04001020 printk(KERN_ERR "SELinux: %s: called before initial "
1021 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 rc = -EINVAL;
1023 goto out;
1024 }
James Morris0804d112008-06-06 18:40:29 +10001025 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001026 if (force)
1027 context = sidtab_search_force(&sidtab, sid);
1028 else
1029 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -04001031 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1032 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 rc = -EINVAL;
1034 goto out_unlock;
1035 }
1036 rc = context_struct_to_string(context, scontext, scontext_len);
1037out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001038 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039out:
1040 return rc;
1041
1042}
1043
Stephen Smalley12b29f32008-05-07 13:03:20 -04001044/**
1045 * security_sid_to_context - Obtain a context for a given SID.
1046 * @sid: security identifier, SID
1047 * @scontext: security context
1048 * @scontext_len: length in bytes
1049 *
1050 * Write the string representation of the context associated with @sid
1051 * into a dynamically allocated string of the correct size. Set @scontext
1052 * to point to this string and set @scontext_len to the length of the string.
1053 */
1054int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Stephen Smalley12b29f32008-05-07 13:03:20 -04001056 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
1057}
1058
1059int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
1060{
1061 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
1062}
1063
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001064/*
1065 * Caveat: Mutates scontext.
1066 */
Stephen Smalley12b29f32008-05-07 13:03:20 -04001067static int string_to_context_struct(struct policydb *pol,
1068 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001069 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001070 u32 scontext_len,
1071 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001072 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001073{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct role_datum *role;
1075 struct type_datum *typdatum;
1076 struct user_datum *usrdatum;
1077 char *scontextp, *p, oldc;
1078 int rc = 0;
1079
Stephen Smalley12b29f32008-05-07 13:03:20 -04001080 context_init(ctx);
1081
Stephen Smalley12b29f32008-05-07 13:03:20 -04001082 /* Parse the security context. */
1083
1084 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001085 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001086
1087 /* Extract the user. */
1088 p = scontextp;
1089 while (*p && *p != ':')
1090 p++;
1091
1092 if (*p == 0)
1093 goto out;
1094
1095 *p++ = 0;
1096
1097 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1098 if (!usrdatum)
1099 goto out;
1100
1101 ctx->user = usrdatum->value;
1102
1103 /* Extract role. */
1104 scontextp = p;
1105 while (*p && *p != ':')
1106 p++;
1107
1108 if (*p == 0)
1109 goto out;
1110
1111 *p++ = 0;
1112
1113 role = hashtab_search(pol->p_roles.table, scontextp);
1114 if (!role)
1115 goto out;
1116 ctx->role = role->value;
1117
1118 /* Extract type. */
1119 scontextp = p;
1120 while (*p && *p != ':')
1121 p++;
1122 oldc = *p;
1123 *p++ = 0;
1124
1125 typdatum = hashtab_search(pol->p_types.table, scontextp);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001126 if (!typdatum || typdatum->attribute)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001127 goto out;
1128
1129 ctx->type = typdatum->value;
1130
1131 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
1132 if (rc)
1133 goto out;
1134
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001135 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001136 rc = -EINVAL;
1137 goto out;
1138 }
1139
1140 /* Check the validity of the new context. */
1141 if (!policydb_context_isvalid(pol, ctx)) {
1142 rc = -EINVAL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001143 goto out;
1144 }
1145 rc = 0;
1146out:
Eric Paris8e531af2008-09-03 11:49:47 -04001147 if (rc)
1148 context_destroy(ctx);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001149 return rc;
1150}
1151
1152static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
1153 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1154 int force)
1155{
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001156 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001157 struct context context;
1158 int rc = 0;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!ss_initialized) {
1161 int i;
1162
1163 for (i = 1; i < SECINITSID_NUM; i++) {
1164 if (!strcmp(initial_sid_to_string[i], scontext)) {
1165 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168 }
1169 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172 *sid = SECSID_NULL;
1173
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001174 /* Copy the string so that we can modify the copy as we parse it. */
1175 scontext2 = kmalloc(scontext_len+1, gfp_flags);
1176 if (!scontext2)
1177 return -ENOMEM;
1178 memcpy(scontext2, scontext, scontext_len);
1179 scontext2[scontext_len] = 0;
1180
1181 if (force) {
1182 /* Save another copy for storing in uninterpreted form */
1183 str = kstrdup(scontext2, gfp_flags);
1184 if (!str) {
1185 kfree(scontext2);
1186 return -ENOMEM;
1187 }
1188 }
1189
James Morris0804d112008-06-06 18:40:29 +10001190 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001191 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001192 scontext2, scontext_len,
1193 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001194 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001195 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001196 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001197 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001198 } else if (rc)
1199 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Eric Paris8e531af2008-09-03 11:49:47 -04001201 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202out:
James Morris0804d112008-06-06 18:40:29 +10001203 read_unlock(&policy_rwlock);
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001204 kfree(scontext2);
1205 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return rc;
1207}
1208
James Morrisf5c1d5b2005-07-28 01:07:37 -07001209/**
1210 * security_context_to_sid - Obtain a SID for a given security context.
1211 * @scontext: security context
1212 * @scontext_len: length in bytes
1213 * @sid: security identifier, SID
1214 *
1215 * Obtains a SID associated with the security context that
1216 * has the string representation specified by @scontext.
1217 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1218 * memory is available, or 0 on success.
1219 */
David Howells8f0cfa52008-04-29 00:59:41 -07001220int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001221{
1222 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001223 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001224}
1225
1226/**
1227 * security_context_to_sid_default - Obtain a SID for a given security context,
1228 * falling back to specified default if needed.
1229 *
1230 * @scontext: security context
1231 * @scontext_len: length in bytes
1232 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -07001233 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -07001234 *
1235 * Obtains a SID associated with the security context that
1236 * has the string representation specified by @scontext.
1237 * The default SID is passed to the MLS layer to be used to allow
1238 * kernel labeling of the MLS field if the MLS field is not present
1239 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -04001240 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -07001241 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1242 * memory is available, or 0 on success.
1243 */
David Howells7bf570d2008-04-29 20:52:51 +01001244int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1245 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001246{
1247 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001248 sid, def_sid, gfp_flags, 1);
1249}
1250
1251int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1252 u32 *sid)
1253{
1254 return security_context_to_sid_core(scontext, scontext_len,
1255 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001256}
1257
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258static int compute_sid_handle_invalid_context(
1259 struct context *scontext,
1260 struct context *tcontext,
1261 u16 tclass,
1262 struct context *newcontext)
1263{
1264 char *s = NULL, *t = NULL, *n = NULL;
1265 u32 slen, tlen, nlen;
1266
1267 if (context_struct_to_string(scontext, &s, &slen) < 0)
1268 goto out;
1269 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
1270 goto out;
1271 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
1272 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001273 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 "security_compute_sid: invalid context %s"
1275 " for scontext=%s"
1276 " tcontext=%s"
1277 " tclass=%s",
1278 n, s, t, policydb.p_class_val_to_name[tclass-1]);
1279out:
1280 kfree(s);
1281 kfree(t);
1282 kfree(n);
1283 if (!selinux_enforcing)
1284 return 0;
1285 return -EACCES;
1286}
1287
1288static int security_compute_sid(u32 ssid,
1289 u32 tsid,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001290 u16 orig_tclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 u32 specified,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001292 u32 *out_sid,
1293 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
1295 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1296 struct role_trans *roletr = NULL;
1297 struct avtab_key avkey;
1298 struct avtab_datum *avdatum;
1299 struct avtab_node *node;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001300 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 int rc = 0;
1302
1303 if (!ss_initialized) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001304 switch (orig_tclass) {
1305 case SECCLASS_PROCESS: /* kernel value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 *out_sid = ssid;
1307 break;
1308 default:
1309 *out_sid = tsid;
1310 break;
1311 }
1312 goto out;
1313 }
1314
Venkat Yekkirala851f8a62006-07-30 03:03:18 -07001315 context_init(&newcontext);
1316
James Morris0804d112008-06-06 18:40:29 +10001317 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001319 if (kern)
1320 tclass = unmap_class(orig_tclass);
1321 else
1322 tclass = orig_tclass;
1323
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 scontext = sidtab_search(&sidtab, ssid);
1325 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001326 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1327 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 rc = -EINVAL;
1329 goto out_unlock;
1330 }
1331 tcontext = sidtab_search(&sidtab, tsid);
1332 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001333 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1334 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 rc = -EINVAL;
1336 goto out_unlock;
1337 }
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 /* Set the user identity. */
1340 switch (specified) {
1341 case AVTAB_TRANSITION:
1342 case AVTAB_CHANGE:
1343 /* Use the process user identity. */
1344 newcontext.user = scontext->user;
1345 break;
1346 case AVTAB_MEMBER:
1347 /* Use the related object owner. */
1348 newcontext.user = tcontext->user;
1349 break;
1350 }
1351
1352 /* Set the role and type to default values. */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001353 if (tclass == policydb.process_class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 /* Use the current role and type of process. */
1355 newcontext.role = scontext->role;
1356 newcontext.type = scontext->type;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001357 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* Use the well-defined object role. */
1359 newcontext.role = OBJECT_R_VAL;
1360 /* Use the type of the related object. */
1361 newcontext.type = tcontext->type;
1362 }
1363
1364 /* Look for a type transition/member/change rule. */
1365 avkey.source_type = scontext->type;
1366 avkey.target_type = tcontext->type;
1367 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001368 avkey.specified = specified;
1369 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001372 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001373 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03001374 for (; node; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001375 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 avdatum = &node->datum;
1377 break;
1378 }
1379 }
1380 }
1381
Stephen Smalley782ebb92005-09-03 15:55:16 -07001382 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001384 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 }
1386
1387 /* Check for class-specific changes. */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001388 if (tclass == policydb.process_class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 if (specified & AVTAB_TRANSITION) {
1390 /* Look for a role transition rule. */
1391 for (roletr = policydb.role_tr; roletr;
1392 roletr = roletr->next) {
1393 if (roletr->role == scontext->role &&
1394 roletr->type == tcontext->type) {
1395 /* Use the role transition rule. */
1396 newcontext.role = roletr->new_role;
1397 break;
1398 }
1399 }
1400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
1403 /* Set the MLS attributes.
1404 This is done last because it may allocate memory. */
1405 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1406 if (rc)
1407 goto out_unlock;
1408
1409 /* Check the validity of the context. */
1410 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1411 rc = compute_sid_handle_invalid_context(scontext,
1412 tcontext,
1413 tclass,
1414 &newcontext);
1415 if (rc)
1416 goto out_unlock;
1417 }
1418 /* Obtain the sid for the context. */
1419 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1420out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001421 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 context_destroy(&newcontext);
1423out:
1424 return rc;
1425}
1426
1427/**
1428 * security_transition_sid - Compute the SID for a new subject/object.
1429 * @ssid: source security identifier
1430 * @tsid: target security identifier
1431 * @tclass: target security class
1432 * @out_sid: security identifier for new subject/object
1433 *
1434 * Compute a SID to use for labeling a new subject or object in the
1435 * class @tclass based on a SID pair (@ssid, @tsid).
1436 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1437 * if insufficient memory is available, or %0 if the new SID was
1438 * computed successfully.
1439 */
1440int security_transition_sid(u32 ssid,
1441 u32 tsid,
1442 u16 tclass,
1443 u32 *out_sid)
1444{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001445 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1446 out_sid, true);
1447}
1448
1449int security_transition_sid_user(u32 ssid,
1450 u32 tsid,
1451 u16 tclass,
1452 u32 *out_sid)
1453{
1454 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1455 out_sid, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456}
1457
1458/**
1459 * security_member_sid - Compute the SID for member selection.
1460 * @ssid: source security identifier
1461 * @tsid: target security identifier
1462 * @tclass: target security class
1463 * @out_sid: security identifier for selected member
1464 *
1465 * Compute a SID to use when selecting a member of a polyinstantiated
1466 * object of class @tclass based on a SID pair (@ssid, @tsid).
1467 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1468 * if insufficient memory is available, or %0 if the SID was
1469 * computed successfully.
1470 */
1471int security_member_sid(u32 ssid,
1472 u32 tsid,
1473 u16 tclass,
1474 u32 *out_sid)
1475{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001476 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid,
1477 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480/**
1481 * security_change_sid - Compute the SID for object relabeling.
1482 * @ssid: source security identifier
1483 * @tsid: target security identifier
1484 * @tclass: target security class
1485 * @out_sid: security identifier for selected member
1486 *
1487 * Compute a SID to use for relabeling an object of class @tclass
1488 * based on a SID pair (@ssid, @tsid).
1489 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1490 * if insufficient memory is available, or %0 if the SID was
1491 * computed successfully.
1492 */
1493int security_change_sid(u32 ssid,
1494 u32 tsid,
1495 u16 tclass,
1496 u32 *out_sid)
1497{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001498 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid,
1499 false);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001500}
1501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502/* Clone the SID into the new SID table. */
1503static int clone_sid(u32 sid,
1504 struct context *context,
1505 void *arg)
1506{
1507 struct sidtab *s = arg;
1508
1509 return sidtab_insert(s, sid, context);
1510}
1511
1512static inline int convert_context_handle_invalid_context(struct context *context)
1513{
1514 int rc = 0;
1515
1516 if (selinux_enforcing) {
1517 rc = -EINVAL;
1518 } else {
1519 char *s;
1520 u32 len;
1521
Stephen Smalley12b29f32008-05-07 13:03:20 -04001522 if (!context_struct_to_string(context, &s, &len)) {
1523 printk(KERN_WARNING
1524 "SELinux: Context %s would be invalid if enforcing\n",
1525 s);
1526 kfree(s);
1527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529 return rc;
1530}
1531
1532struct convert_context_args {
1533 struct policydb *oldp;
1534 struct policydb *newp;
1535};
1536
1537/*
1538 * Convert the values in the security context
1539 * structure `c' from the values specified
1540 * in the policy `p->oldp' to the values specified
1541 * in the policy `p->newp'. Verify that the
1542 * context is valid under the new policy.
1543 */
1544static int convert_context(u32 key,
1545 struct context *c,
1546 void *p)
1547{
1548 struct convert_context_args *args;
1549 struct context oldc;
1550 struct role_datum *role;
1551 struct type_datum *typdatum;
1552 struct user_datum *usrdatum;
1553 char *s;
1554 u32 len;
1555 int rc;
1556
1557 args = p;
1558
Stephen Smalley12b29f32008-05-07 13:03:20 -04001559 if (c->str) {
1560 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001561 s = kstrdup(c->str, GFP_KERNEL);
1562 if (!s) {
1563 rc = -ENOMEM;
1564 goto out;
1565 }
1566 rc = string_to_context_struct(args->newp, NULL, s,
1567 c->len, &ctx, SECSID_NULL);
1568 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001569 if (!rc) {
1570 printk(KERN_INFO
1571 "SELinux: Context %s became valid (mapped).\n",
1572 c->str);
1573 /* Replace string with mapped representation. */
1574 kfree(c->str);
1575 memcpy(c, &ctx, sizeof(*c));
1576 goto out;
1577 } else if (rc == -EINVAL) {
1578 /* Retain string representation for later mapping. */
1579 rc = 0;
1580 goto out;
1581 } else {
1582 /* Other error condition, e.g. ENOMEM. */
1583 printk(KERN_ERR
1584 "SELinux: Unable to map context %s, rc = %d.\n",
1585 c->str, -rc);
1586 goto out;
1587 }
1588 }
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 rc = context_cpy(&oldc, c);
1591 if (rc)
1592 goto out;
1593
1594 rc = -EINVAL;
1595
1596 /* Convert the user. */
1597 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001598 args->oldp->p_user_val_to_name[c->user - 1]);
1599 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 c->user = usrdatum->value;
1602
1603 /* Convert the role. */
1604 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001605 args->oldp->p_role_val_to_name[c->role - 1]);
1606 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 c->role = role->value;
1609
1610 /* Convert the type. */
1611 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001612 args->oldp->p_type_val_to_name[c->type - 1]);
1613 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 c->type = typdatum->value;
1616
1617 rc = mls_convert_context(args->oldp, args->newp, c);
1618 if (rc)
1619 goto bad;
1620
1621 /* Check the validity of the new context. */
1622 if (!policydb_context_isvalid(args->newp, c)) {
1623 rc = convert_context_handle_invalid_context(&oldc);
1624 if (rc)
1625 goto bad;
1626 }
1627
1628 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001629 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630out:
1631 return rc;
1632bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001633 /* Map old representation to string and save it. */
1634 if (context_struct_to_string(&oldc, &s, &len))
1635 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001637 context_destroy(c);
1638 c->str = s;
1639 c->len = len;
1640 printk(KERN_INFO
1641 "SELinux: Context %s became invalid (unmapped).\n",
1642 c->str);
1643 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 goto out;
1645}
1646
Paul Moore3bb56b22008-01-29 08:38:19 -05001647static void security_load_policycaps(void)
1648{
1649 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1650 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001651 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1652 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001653}
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001656static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658/**
1659 * security_load_policy - Load a security policy configuration.
1660 * @data: binary policy data
1661 * @len: length of data in bytes
1662 *
1663 * Load a new set of security policy configuration data,
1664 * validate it and convert the SID table as necessary.
1665 * This function will flush the access vector cache after
1666 * loading the new policy.
1667 */
1668int security_load_policy(void *data, size_t len)
1669{
1670 struct policydb oldpolicydb, newpolicydb;
1671 struct sidtab oldsidtab, newsidtab;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001672 struct selinux_mapping *oldmap, *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 struct convert_context_args args;
1674 u32 seqno;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001675 u16 map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 int rc = 0;
1677 struct policy_file file = { data, len }, *fp = &file;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (!ss_initialized) {
1680 avtab_cache_init();
1681 if (policydb_read(&policydb, fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 avtab_cache_destroy();
1683 return -EINVAL;
1684 }
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001685 if (selinux_set_mapping(&policydb, secclass_map,
1686 &current_mapping,
1687 &current_mapping_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 policydb_destroy(&policydb);
1689 avtab_cache_destroy();
1690 return -EINVAL;
1691 }
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001692 if (policydb_load_isids(&policydb, &sidtab)) {
Chad Sellersb94c7e62006-11-06 12:38:18 -05001693 policydb_destroy(&policydb);
1694 avtab_cache_destroy();
1695 return -EINVAL;
1696 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001697 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001699 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001701 avc_ss_reset(seqno);
1702 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001703 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001704 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return 0;
1706 }
1707
1708#if 0
1709 sidtab_hash_eval(&sidtab, "sids");
1710#endif
1711
Eric Paris89abd0a2008-06-09 15:58:04 -04001712 if (policydb_read(&newpolicydb, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Stephen Smalley12b29f32008-05-07 13:03:20 -04001715 if (sidtab_init(&newsidtab)) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001716 policydb_destroy(&newpolicydb);
1717 return -ENOMEM;
1718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001720 if (selinux_set_mapping(&newpolicydb, secclass_map,
1721 &map, &map_size))
Chad Sellersb94c7e62006-11-06 12:38:18 -05001722 goto err;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001723
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001724 rc = security_preserve_bools(&newpolicydb);
1725 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001726 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001727 goto err;
1728 }
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /* Clone the SID table. */
1731 sidtab_shutdown(&sidtab);
1732 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1733 rc = -ENOMEM;
1734 goto err;
1735 }
1736
Stephen Smalley12b29f32008-05-07 13:03:20 -04001737 /*
1738 * Convert the internal representations of contexts
1739 * in the new SID table.
1740 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 args.oldp = &policydb;
1742 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001743 rc = sidtab_map(&newsidtab, convert_context, &args);
1744 if (rc)
1745 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* Save the old policydb and SID table to free later. */
1748 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1749 sidtab_set(&oldsidtab, &sidtab);
1750
1751 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10001752 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 memcpy(&policydb, &newpolicydb, sizeof policydb);
1754 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001755 security_load_policycaps();
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001756 oldmap = current_mapping;
1757 current_mapping = map;
1758 current_mapping_size = map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 seqno = ++latest_granting;
James Morris0804d112008-06-06 18:40:29 +10001760 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /* Free the old policydb and SID table. */
1763 policydb_destroy(&oldpolicydb);
1764 sidtab_destroy(&oldsidtab);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001765 kfree(oldmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
1767 avc_ss_reset(seqno);
1768 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001769 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001770 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 return 0;
1773
1774err:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001775 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 sidtab_destroy(&newsidtab);
1777 policydb_destroy(&newpolicydb);
1778 return rc;
1779
1780}
1781
1782/**
1783 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 * @protocol: protocol number
1785 * @port: port number
1786 * @out_sid: security identifier
1787 */
Paul Moore3e112172008-04-10 10:48:14 -04001788int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 struct ocontext *c;
1791 int rc = 0;
1792
James Morris0804d112008-06-06 18:40:29 +10001793 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 c = policydb.ocontexts[OCON_PORT];
1796 while (c) {
1797 if (c->u.port.protocol == protocol &&
1798 c->u.port.low_port <= port &&
1799 c->u.port.high_port >= port)
1800 break;
1801 c = c->next;
1802 }
1803
1804 if (c) {
1805 if (!c->sid[0]) {
1806 rc = sidtab_context_to_sid(&sidtab,
1807 &c->context[0],
1808 &c->sid[0]);
1809 if (rc)
1810 goto out;
1811 }
1812 *out_sid = c->sid[0];
1813 } else {
1814 *out_sid = SECINITSID_PORT;
1815 }
1816
1817out:
James Morris0804d112008-06-06 18:40:29 +10001818 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return rc;
1820}
1821
1822/**
1823 * security_netif_sid - Obtain the SID for a network interface.
1824 * @name: interface name
1825 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001827int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 int rc = 0;
1830 struct ocontext *c;
1831
James Morris0804d112008-06-06 18:40:29 +10001832 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
1834 c = policydb.ocontexts[OCON_NETIF];
1835 while (c) {
1836 if (strcmp(name, c->u.name) == 0)
1837 break;
1838 c = c->next;
1839 }
1840
1841 if (c) {
1842 if (!c->sid[0] || !c->sid[1]) {
1843 rc = sidtab_context_to_sid(&sidtab,
1844 &c->context[0],
1845 &c->sid[0]);
1846 if (rc)
1847 goto out;
1848 rc = sidtab_context_to_sid(&sidtab,
1849 &c->context[1],
1850 &c->sid[1]);
1851 if (rc)
1852 goto out;
1853 }
1854 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001855 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857
1858out:
James Morris0804d112008-06-06 18:40:29 +10001859 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 return rc;
1861}
1862
1863static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1864{
1865 int i, fail = 0;
1866
Eric Paris5d55a342008-04-18 17:38:33 -04001867 for (i = 0; i < 4; i++)
1868 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 fail = 1;
1870 break;
1871 }
1872
1873 return !fail;
1874}
1875
1876/**
1877 * security_node_sid - Obtain the SID for a node (host).
1878 * @domain: communication domain aka address family
1879 * @addrp: address
1880 * @addrlen: address length in bytes
1881 * @out_sid: security identifier
1882 */
1883int security_node_sid(u16 domain,
1884 void *addrp,
1885 u32 addrlen,
1886 u32 *out_sid)
1887{
1888 int rc = 0;
1889 struct ocontext *c;
1890
James Morris0804d112008-06-06 18:40:29 +10001891 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 switch (domain) {
1894 case AF_INET: {
1895 u32 addr;
1896
1897 if (addrlen != sizeof(u32)) {
1898 rc = -EINVAL;
1899 goto out;
1900 }
1901
1902 addr = *((u32 *)addrp);
1903
1904 c = policydb.ocontexts[OCON_NODE];
1905 while (c) {
1906 if (c->u.node.addr == (addr & c->u.node.mask))
1907 break;
1908 c = c->next;
1909 }
1910 break;
1911 }
1912
1913 case AF_INET6:
1914 if (addrlen != sizeof(u64) * 2) {
1915 rc = -EINVAL;
1916 goto out;
1917 }
1918 c = policydb.ocontexts[OCON_NODE6];
1919 while (c) {
1920 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1921 c->u.node6.mask))
1922 break;
1923 c = c->next;
1924 }
1925 break;
1926
1927 default:
1928 *out_sid = SECINITSID_NODE;
1929 goto out;
1930 }
1931
1932 if (c) {
1933 if (!c->sid[0]) {
1934 rc = sidtab_context_to_sid(&sidtab,
1935 &c->context[0],
1936 &c->sid[0]);
1937 if (rc)
1938 goto out;
1939 }
1940 *out_sid = c->sid[0];
1941 } else {
1942 *out_sid = SECINITSID_NODE;
1943 }
1944
1945out:
James Morris0804d112008-06-06 18:40:29 +10001946 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 return rc;
1948}
1949
1950#define SIDS_NEL 25
1951
1952/**
1953 * security_get_user_sids - Obtain reachable SIDs for a user.
1954 * @fromsid: starting SID
1955 * @username: username
1956 * @sids: array of reachable SIDs for user
1957 * @nel: number of elements in @sids
1958 *
1959 * Generate the set of SIDs for legal security contexts
1960 * for a given user that can be reached by @fromsid.
1961 * Set *@sids to point to a dynamically allocated
1962 * array containing the set of SIDs. Set *@nel to the
1963 * number of elements in the array.
1964 */
1965
1966int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04001967 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 u32 **sids,
1969 u32 *nel)
1970{
1971 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001972 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 u32 mynel = 0, maxnel = SIDS_NEL;
1974 struct user_datum *user;
1975 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001976 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int rc = 0, i, j;
1978
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04001979 *sids = NULL;
1980 *nel = 0;
1981
1982 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
James Morris0804d112008-06-06 18:40:29 +10001985 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Stephen Smalley12b29f32008-05-07 13:03:20 -04001987 context_init(&usercon);
1988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 fromcon = sidtab_search(&sidtab, fromsid);
1990 if (!fromcon) {
1991 rc = -EINVAL;
1992 goto out_unlock;
1993 }
1994
1995 user = hashtab_search(policydb.p_users.table, username);
1996 if (!user) {
1997 rc = -EINVAL;
1998 goto out_unlock;
1999 }
2000 usercon.user = user->value;
2001
James Morris89d155e2005-10-30 14:59:21 -08002002 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (!mysids) {
2004 rc = -ENOMEM;
2005 goto out_unlock;
2006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002008 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 role = policydb.role_val_to_struct[i];
2010 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002011 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 usercon.type = j+1;
2013
2014 if (mls_setup_user_range(fromcon, user, &usercon))
2015 continue;
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002018 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (mynel < maxnel) {
2021 mysids[mynel++] = sid;
2022 } else {
2023 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08002024 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (!mysids2) {
2026 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto out_unlock;
2028 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2030 kfree(mysids);
2031 mysids = mysids2;
2032 mysids[mynel++] = sid;
2033 }
2034 }
2035 }
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002038 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002039 if (rc || !mynel) {
2040 kfree(mysids);
2041 goto out;
2042 }
2043
2044 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2045 if (!mysids2) {
2046 rc = -ENOMEM;
2047 kfree(mysids);
2048 goto out;
2049 }
2050 for (i = 0, j = 0; i < mynel; i++) {
2051 rc = avc_has_perm_noaudit(fromsid, mysids[i],
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002052 SECCLASS_PROCESS, /* kernel value */
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002053 PROCESS__TRANSITION, AVC_STRICT,
2054 NULL);
2055 if (!rc)
2056 mysids2[j++] = mysids[i];
2057 cond_resched();
2058 }
2059 rc = 0;
2060 kfree(mysids);
2061 *sids = mysids2;
2062 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063out:
2064 return rc;
2065}
2066
2067/**
2068 * security_genfs_sid - Obtain a SID for a file in a filesystem
2069 * @fstype: filesystem type
2070 * @path: path from root of mount
2071 * @sclass: file security class
2072 * @sid: SID for path
2073 *
2074 * Obtain a SID to use for a file in a filesystem that
2075 * cannot support xattr or use a fixed labeling behavior like
2076 * transition SIDs or task SIDs.
2077 */
2078int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04002079 char *path,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002080 u16 orig_sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 u32 *sid)
2082{
2083 int len;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002084 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct genfs *genfs;
2086 struct ocontext *c;
2087 int rc = 0, cmp = 0;
2088
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05002089 while (path[0] == '/' && path[1] == '/')
2090 path++;
2091
James Morris0804d112008-06-06 18:40:29 +10002092 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002094 sclass = unmap_class(orig_sclass);
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2097 cmp = strcmp(fstype, genfs->fstype);
2098 if (cmp <= 0)
2099 break;
2100 }
2101
2102 if (!genfs || cmp) {
2103 *sid = SECINITSID_UNLABELED;
2104 rc = -ENOENT;
2105 goto out;
2106 }
2107
2108 for (c = genfs->head; c; c = c->next) {
2109 len = strlen(c->u.name);
2110 if ((!c->v.sclass || sclass == c->v.sclass) &&
2111 (strncmp(c->u.name, path, len) == 0))
2112 break;
2113 }
2114
2115 if (!c) {
2116 *sid = SECINITSID_UNLABELED;
2117 rc = -ENOENT;
2118 goto out;
2119 }
2120
2121 if (!c->sid[0]) {
2122 rc = sidtab_context_to_sid(&sidtab,
2123 &c->context[0],
2124 &c->sid[0]);
2125 if (rc)
2126 goto out;
2127 }
2128
2129 *sid = c->sid[0];
2130out:
James Morris0804d112008-06-06 18:40:29 +10002131 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return rc;
2133}
2134
2135/**
2136 * security_fs_use - Determine how to handle labeling for a filesystem.
2137 * @fstype: filesystem type
2138 * @behavior: labeling behavior
2139 * @sid: SID for filesystem (superblock)
2140 */
2141int security_fs_use(
2142 const char *fstype,
2143 unsigned int *behavior,
James Morris089be432008-07-15 18:32:49 +10002144 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
2146 int rc = 0;
2147 struct ocontext *c;
2148
James Morris0804d112008-06-06 18:40:29 +10002149 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151 c = policydb.ocontexts[OCON_FSUSE];
2152 while (c) {
2153 if (strcmp(fstype, c->u.name) == 0)
2154 break;
2155 c = c->next;
2156 }
2157
2158 if (c) {
2159 *behavior = c->v.behavior;
2160 if (!c->sid[0]) {
2161 rc = sidtab_context_to_sid(&sidtab,
2162 &c->context[0],
2163 &c->sid[0]);
2164 if (rc)
2165 goto out;
2166 }
2167 *sid = c->sid[0];
2168 } else {
James Morris089be432008-07-15 18:32:49 +10002169 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2170 if (rc) {
2171 *behavior = SECURITY_FS_USE_NONE;
2172 rc = 0;
2173 } else {
2174 *behavior = SECURITY_FS_USE_GENFS;
2175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
2177
2178out:
James Morris0804d112008-06-06 18:40:29 +10002179 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return rc;
2181}
2182
2183int security_get_bools(int *len, char ***names, int **values)
2184{
2185 int i, rc = -ENOMEM;
2186
James Morris0804d112008-06-06 18:40:29 +10002187 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 *names = NULL;
2189 *values = NULL;
2190
2191 *len = policydb.p_bools.nprim;
2192 if (!*len) {
2193 rc = 0;
2194 goto out;
2195 }
2196
Eric Paris5d55a342008-04-18 17:38:33 -04002197 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (!*names)
2199 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Jesper Juhle0795cf2006-01-09 20:54:46 -08002201 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (!*values)
2203 goto err;
2204
2205 for (i = 0; i < *len; i++) {
2206 size_t name_len;
2207 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2208 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002209 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (!(*names)[i])
2211 goto err;
2212 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2213 (*names)[i][name_len - 1] = 0;
2214 }
2215 rc = 0;
2216out:
James Morris0804d112008-06-06 18:40:29 +10002217 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return rc;
2219err:
2220 if (*names) {
2221 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002222 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002224 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 goto out;
2226}
2227
2228
2229int security_set_bools(int len, int *values)
2230{
2231 int i, rc = 0;
2232 int lenp, seqno = 0;
2233 struct cond_node *cur;
2234
James Morris0804d112008-06-06 18:40:29 +10002235 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 lenp = policydb.p_bools.nprim;
2238 if (len != lenp) {
2239 rc = -EFAULT;
2240 goto out;
2241 }
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002244 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2245 audit_log(current->audit_context, GFP_ATOMIC,
2246 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002247 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002248 policydb.p_bool_val_to_name[i],
2249 !!values[i],
2250 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002251 audit_get_loginuid(current),
2252 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002253 }
Eric Paris5d55a342008-04-18 17:38:33 -04002254 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002256 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002260 for (cur = policydb.cond_list; cur; cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 rc = evaluate_cond_node(&policydb, cur);
2262 if (rc)
2263 goto out;
2264 }
2265
2266 seqno = ++latest_granting;
2267
2268out:
James Morris0804d112008-06-06 18:40:29 +10002269 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (!rc) {
2271 avc_ss_reset(seqno);
2272 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002273 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 return rc;
2276}
2277
2278int security_get_bool_value(int bool)
2279{
2280 int rc = 0;
2281 int len;
2282
James Morris0804d112008-06-06 18:40:29 +10002283 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 len = policydb.p_bools.nprim;
2286 if (bool >= len) {
2287 rc = -EFAULT;
2288 goto out;
2289 }
2290
2291 rc = policydb.bool_val_to_struct[bool]->state;
2292out:
James Morris0804d112008-06-06 18:40:29 +10002293 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return rc;
2295}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002296
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002297static int security_preserve_bools(struct policydb *p)
2298{
2299 int rc, nbools = 0, *bvalues = NULL, i;
2300 char **bnames = NULL;
2301 struct cond_bool_datum *booldatum;
2302 struct cond_node *cur;
2303
2304 rc = security_get_bools(&nbools, &bnames, &bvalues);
2305 if (rc)
2306 goto out;
2307 for (i = 0; i < nbools; i++) {
2308 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2309 if (booldatum)
2310 booldatum->state = bvalues[i];
2311 }
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002312 for (cur = p->cond_list; cur; cur = cur->next) {
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002313 rc = evaluate_cond_node(p, cur);
2314 if (rc)
2315 goto out;
2316 }
2317
2318out:
2319 if (bnames) {
2320 for (i = 0; i < nbools; i++)
2321 kfree(bnames[i]);
2322 }
2323 kfree(bnames);
2324 kfree(bvalues);
2325 return rc;
2326}
2327
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002328/*
2329 * security_sid_mls_copy() - computes a new sid based on the given
2330 * sid and the mls portion of mls_sid.
2331 */
2332int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2333{
2334 struct context *context1;
2335 struct context *context2;
2336 struct context newcon;
2337 char *s;
2338 u32 len;
2339 int rc = 0;
2340
Venkat Yekkirala4eb327b2006-09-19 10:24:19 -07002341 if (!ss_initialized || !selinux_mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002342 *new_sid = sid;
2343 goto out;
2344 }
2345
2346 context_init(&newcon);
2347
James Morris0804d112008-06-06 18:40:29 +10002348 read_lock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002349 context1 = sidtab_search(&sidtab, sid);
2350 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002351 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2352 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002353 rc = -EINVAL;
2354 goto out_unlock;
2355 }
2356
2357 context2 = sidtab_search(&sidtab, mls_sid);
2358 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002359 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2360 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002361 rc = -EINVAL;
2362 goto out_unlock;
2363 }
2364
2365 newcon.user = context1->user;
2366 newcon.role = context1->role;
2367 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002368 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002369 if (rc)
2370 goto out_unlock;
2371
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002372 /* Check the validity of the new context. */
2373 if (!policydb_context_isvalid(&policydb, &newcon)) {
2374 rc = convert_context_handle_invalid_context(&newcon);
2375 if (rc)
2376 goto bad;
2377 }
2378
2379 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2380 goto out_unlock;
2381
2382bad:
2383 if (!context_struct_to_string(&newcon, &s, &len)) {
2384 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2385 "security_sid_mls_copy: invalid context %s", s);
2386 kfree(s);
2387 }
2388
2389out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002390 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002391 context_destroy(&newcon);
2392out:
2393 return rc;
2394}
2395
Paul Moore220deb92008-01-29 08:38:23 -05002396/**
2397 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2398 * @nlbl_sid: NetLabel SID
2399 * @nlbl_type: NetLabel labeling protocol type
2400 * @xfrm_sid: XFRM SID
2401 *
2402 * Description:
2403 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2404 * resolved into a single SID it is returned via @peer_sid and the function
2405 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2406 * returns a negative value. A table summarizing the behavior is below:
2407 *
2408 * | function return | @sid
2409 * ------------------------------+-----------------+-----------------
2410 * no peer labels | 0 | SECSID_NULL
2411 * single peer label | 0 | <peer_label>
2412 * multiple, consistent labels | 0 | <peer_label>
2413 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2414 *
2415 */
2416int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2417 u32 xfrm_sid,
2418 u32 *peer_sid)
2419{
2420 int rc;
2421 struct context *nlbl_ctx;
2422 struct context *xfrm_ctx;
2423
2424 /* handle the common (which also happens to be the set of easy) cases
2425 * right away, these two if statements catch everything involving a
2426 * single or absent peer SID/label */
2427 if (xfrm_sid == SECSID_NULL) {
2428 *peer_sid = nlbl_sid;
2429 return 0;
2430 }
2431 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2432 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2433 * is present */
2434 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2435 *peer_sid = xfrm_sid;
2436 return 0;
2437 }
2438
2439 /* we don't need to check ss_initialized here since the only way both
2440 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2441 * security server was initialized and ss_initialized was true */
2442 if (!selinux_mls_enabled) {
2443 *peer_sid = SECSID_NULL;
2444 return 0;
2445 }
2446
James Morris0804d112008-06-06 18:40:29 +10002447 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002448
2449 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2450 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002451 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2452 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002453 rc = -EINVAL;
2454 goto out_slowpath;
2455 }
2456 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2457 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002458 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2459 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002460 rc = -EINVAL;
2461 goto out_slowpath;
2462 }
2463 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2464
2465out_slowpath:
James Morris0804d112008-06-06 18:40:29 +10002466 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002467 if (rc == 0)
2468 /* at present NetLabel SIDs/labels really only carry MLS
2469 * information so if the MLS portion of the NetLabel SID
2470 * matches the MLS portion of the labeled XFRM SID/label
2471 * then pass along the XFRM SID as it is the most
2472 * expressive */
2473 *peer_sid = xfrm_sid;
2474 else
2475 *peer_sid = SECSID_NULL;
2476 return rc;
2477}
2478
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002479static int get_classes_callback(void *k, void *d, void *args)
2480{
2481 struct class_datum *datum = d;
2482 char *name = k, **classes = args;
2483 int value = datum->value - 1;
2484
2485 classes[value] = kstrdup(name, GFP_ATOMIC);
2486 if (!classes[value])
2487 return -ENOMEM;
2488
2489 return 0;
2490}
2491
2492int security_get_classes(char ***classes, int *nclasses)
2493{
2494 int rc = -ENOMEM;
2495
James Morris0804d112008-06-06 18:40:29 +10002496 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002497
2498 *nclasses = policydb.p_classes.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002499 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002500 if (!*classes)
2501 goto out;
2502
2503 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2504 *classes);
2505 if (rc < 0) {
2506 int i;
2507 for (i = 0; i < *nclasses; i++)
2508 kfree((*classes)[i]);
2509 kfree(*classes);
2510 }
2511
2512out:
James Morris0804d112008-06-06 18:40:29 +10002513 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002514 return rc;
2515}
2516
2517static int get_permissions_callback(void *k, void *d, void *args)
2518{
2519 struct perm_datum *datum = d;
2520 char *name = k, **perms = args;
2521 int value = datum->value - 1;
2522
2523 perms[value] = kstrdup(name, GFP_ATOMIC);
2524 if (!perms[value])
2525 return -ENOMEM;
2526
2527 return 0;
2528}
2529
2530int security_get_permissions(char *class, char ***perms, int *nperms)
2531{
2532 int rc = -ENOMEM, i;
2533 struct class_datum *match;
2534
James Morris0804d112008-06-06 18:40:29 +10002535 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002536
2537 match = hashtab_search(policydb.p_classes.table, class);
2538 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002539 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002540 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002541 rc = -EINVAL;
2542 goto out;
2543 }
2544
2545 *nperms = match->permissions.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002546 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002547 if (!*perms)
2548 goto out;
2549
2550 if (match->comdatum) {
2551 rc = hashtab_map(match->comdatum->permissions.table,
2552 get_permissions_callback, *perms);
2553 if (rc < 0)
2554 goto err;
2555 }
2556
2557 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2558 *perms);
2559 if (rc < 0)
2560 goto err;
2561
2562out:
James Morris0804d112008-06-06 18:40:29 +10002563 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002564 return rc;
2565
2566err:
James Morris0804d112008-06-06 18:40:29 +10002567 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002568 for (i = 0; i < *nperms; i++)
2569 kfree((*perms)[i]);
2570 kfree(*perms);
2571 return rc;
2572}
2573
Eric Paris3f120702007-09-21 14:37:10 -04002574int security_get_reject_unknown(void)
2575{
2576 return policydb.reject_unknown;
2577}
2578
2579int security_get_allow_unknown(void)
2580{
2581 return policydb.allow_unknown;
2582}
2583
Paul Moore3bb56b22008-01-29 08:38:19 -05002584/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002585 * security_policycap_supported - Check for a specific policy capability
2586 * @req_cap: capability
2587 *
2588 * Description:
2589 * This function queries the currently loaded policy to see if it supports the
2590 * capability specified by @req_cap. Returns true (1) if the capability is
2591 * supported, false (0) if it isn't supported.
2592 *
2593 */
2594int security_policycap_supported(unsigned int req_cap)
2595{
2596 int rc;
2597
James Morris0804d112008-06-06 18:40:29 +10002598 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002599 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10002600 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002601
2602 return rc;
2603}
2604
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002605struct selinux_audit_rule {
2606 u32 au_seqno;
2607 struct context au_ctxt;
2608};
2609
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002610void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002611{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002612 struct selinux_audit_rule *rule = vrule;
2613
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002614 if (rule) {
2615 context_destroy(&rule->au_ctxt);
2616 kfree(rule);
2617 }
2618}
2619
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002620int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002621{
2622 struct selinux_audit_rule *tmprule;
2623 struct role_datum *roledatum;
2624 struct type_datum *typedatum;
2625 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002626 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002627 int rc = 0;
2628
2629 *rule = NULL;
2630
2631 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002632 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002633
2634 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002635 case AUDIT_SUBJ_USER:
2636 case AUDIT_SUBJ_ROLE:
2637 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002638 case AUDIT_OBJ_USER:
2639 case AUDIT_OBJ_ROLE:
2640 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002641 /* only 'equals' and 'not equals' fit user, role, and type */
Al Viro5af75d82008-12-16 05:59:26 -05002642 if (op != Audit_equal && op != Audit_not_equal)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002643 return -EINVAL;
2644 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002645 case AUDIT_SUBJ_SEN:
2646 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002647 case AUDIT_OBJ_LEV_LOW:
2648 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002649 /* we do not allow a range, indicated by the presense of '-' */
2650 if (strchr(rulestr, '-'))
2651 return -EINVAL;
2652 break;
2653 default:
2654 /* only the above fields are valid */
2655 return -EINVAL;
2656 }
2657
2658 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2659 if (!tmprule)
2660 return -ENOMEM;
2661
2662 context_init(&tmprule->au_ctxt);
2663
James Morris0804d112008-06-06 18:40:29 +10002664 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002665
2666 tmprule->au_seqno = latest_granting;
2667
2668 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002669 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002670 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002671 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2672 if (!userdatum)
2673 rc = -EINVAL;
2674 else
2675 tmprule->au_ctxt.user = userdatum->value;
2676 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002677 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002678 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002679 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2680 if (!roledatum)
2681 rc = -EINVAL;
2682 else
2683 tmprule->au_ctxt.role = roledatum->value;
2684 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002685 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002686 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002687 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2688 if (!typedatum)
2689 rc = -EINVAL;
2690 else
2691 tmprule->au_ctxt.type = typedatum->value;
2692 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002693 case AUDIT_SUBJ_SEN:
2694 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002695 case AUDIT_OBJ_LEV_LOW:
2696 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002697 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2698 break;
2699 }
2700
James Morris0804d112008-06-06 18:40:29 +10002701 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002702
2703 if (rc) {
2704 selinux_audit_rule_free(tmprule);
2705 tmprule = NULL;
2706 }
2707
2708 *rule = tmprule;
2709
2710 return rc;
2711}
2712
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002713/* Check to see if the rule contains any selinux fields */
2714int selinux_audit_rule_known(struct audit_krule *rule)
2715{
2716 int i;
2717
2718 for (i = 0; i < rule->field_count; i++) {
2719 struct audit_field *f = &rule->fields[i];
2720 switch (f->type) {
2721 case AUDIT_SUBJ_USER:
2722 case AUDIT_SUBJ_ROLE:
2723 case AUDIT_SUBJ_TYPE:
2724 case AUDIT_SUBJ_SEN:
2725 case AUDIT_SUBJ_CLR:
2726 case AUDIT_OBJ_USER:
2727 case AUDIT_OBJ_ROLE:
2728 case AUDIT_OBJ_TYPE:
2729 case AUDIT_OBJ_LEV_LOW:
2730 case AUDIT_OBJ_LEV_HIGH:
2731 return 1;
2732 }
2733 }
2734
2735 return 0;
2736}
2737
2738int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04002739 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002740{
2741 struct context *ctxt;
2742 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002743 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002744 int match = 0;
2745
2746 if (!rule) {
2747 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002748 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002749 return -ENOENT;
2750 }
2751
James Morris0804d112008-06-06 18:40:29 +10002752 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002753
2754 if (rule->au_seqno < latest_granting) {
2755 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002756 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002757 match = -ESTALE;
2758 goto out;
2759 }
2760
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002761 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002762 if (!ctxt) {
2763 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002764 "selinux_audit_rule_match: unrecognized SID %d\n",
2765 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002766 match = -ENOENT;
2767 goto out;
2768 }
2769
2770 /* a field/op pair that is not caught here will simply fall through
2771 without a match */
2772 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002773 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002774 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002775 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002776 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002777 match = (ctxt->user == rule->au_ctxt.user);
2778 break;
Al Viro5af75d82008-12-16 05:59:26 -05002779 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002780 match = (ctxt->user != rule->au_ctxt.user);
2781 break;
2782 }
2783 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002784 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002785 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002786 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002787 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002788 match = (ctxt->role == rule->au_ctxt.role);
2789 break;
Al Viro5af75d82008-12-16 05:59:26 -05002790 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002791 match = (ctxt->role != rule->au_ctxt.role);
2792 break;
2793 }
2794 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002795 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002796 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002797 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002798 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002799 match = (ctxt->type == rule->au_ctxt.type);
2800 break;
Al Viro5af75d82008-12-16 05:59:26 -05002801 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002802 match = (ctxt->type != rule->au_ctxt.type);
2803 break;
2804 }
2805 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002806 case AUDIT_SUBJ_SEN:
2807 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002808 case AUDIT_OBJ_LEV_LOW:
2809 case AUDIT_OBJ_LEV_HIGH:
2810 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002811 field == AUDIT_OBJ_LEV_LOW) ?
2812 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002813 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002814 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002815 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002816 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002817 break;
Al Viro5af75d82008-12-16 05:59:26 -05002818 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002819 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002820 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002821 break;
Al Viro5af75d82008-12-16 05:59:26 -05002822 case Audit_lt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002823 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002824 level) &&
2825 !mls_level_eq(&rule->au_ctxt.range.level[0],
2826 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002827 break;
Al Viro5af75d82008-12-16 05:59:26 -05002828 case Audit_le:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002829 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002830 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002831 break;
Al Viro5af75d82008-12-16 05:59:26 -05002832 case Audit_gt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002833 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002834 &rule->au_ctxt.range.level[0]) &&
2835 !mls_level_eq(level,
2836 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002837 break;
Al Viro5af75d82008-12-16 05:59:26 -05002838 case Audit_ge:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002839 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002840 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002841 break;
2842 }
2843 }
2844
2845out:
James Morris0804d112008-06-06 18:40:29 +10002846 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002847 return match;
2848}
2849
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002850static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002851
2852static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
Eric Parisf5269712008-05-14 11:27:45 -04002853 u16 class, u32 perms, u32 *retained)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002854{
2855 int err = 0;
2856
2857 if (event == AVC_CALLBACK_RESET && aurule_callback)
2858 err = aurule_callback();
2859 return err;
2860}
2861
2862static int __init aurule_init(void)
2863{
2864 int err;
2865
2866 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002867 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002868 if (err)
2869 panic("avc_add_callback() failed, error %d\n", err);
2870
2871 return err;
2872}
2873__initcall(aurule_init);
2874
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002875#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002876/**
Paul Moore5778eab2007-02-28 15:14:22 -05002877 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2878 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002879 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002880 *
2881 * Description:
2882 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002883 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2884 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002885 *
2886 */
Paul Moore5778eab2007-02-28 15:14:22 -05002887static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002888 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002889{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002890 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002891
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002892 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2893 if (sid_cache == NULL)
2894 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002895 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002896 if (secattr->cache == NULL) {
2897 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002898 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002899 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002900
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002901 *sid_cache = sid;
2902 secattr->cache->free = kfree;
2903 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002904 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002905}
2906
2907/**
Paul Moore5778eab2007-02-28 15:14:22 -05002908 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002909 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002910 * @sid: the SELinux SID
2911 *
2912 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002913 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002914 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002915 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2916 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2917 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2918 * conversion for future lookups. Returns zero on success, negative values on
2919 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002920 *
2921 */
Paul Moore5778eab2007-02-28 15:14:22 -05002922int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002923 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002924{
2925 int rc = -EIDRM;
2926 struct context *ctx;
2927 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002928
2929 if (!ss_initialized) {
2930 *sid = SECSID_NULL;
2931 return 0;
2932 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002933
James Morris0804d112008-06-06 18:40:29 +10002934 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002935
Paul Moore701a90b2006-11-17 17:38:46 -05002936 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002937 *sid = *(u32 *)secattr->cache->data;
2938 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002939 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2940 *sid = secattr->attr.secid;
2941 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05002942 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002943 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002944 if (ctx == NULL)
2945 goto netlbl_secattr_to_sid_return;
2946
Paul Moore81990fb2008-10-03 10:51:15 -04002947 context_init(&ctx_new);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002948 ctx_new.user = ctx->user;
2949 ctx_new.role = ctx->role;
2950 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05002951 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05002952 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05002953 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05002954 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002955 goto netlbl_secattr_to_sid_return;
Paul Moore81990fb2008-10-03 10:51:15 -04002956 memcpy(&ctx_new.range.level[1].cat,
2957 &ctx_new.range.level[0].cat,
2958 sizeof(ctx_new.range.level[0].cat));
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002959 }
2960 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
2961 goto netlbl_secattr_to_sid_return_cleanup;
2962
2963 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
2964 if (rc != 0)
2965 goto netlbl_secattr_to_sid_return_cleanup;
2966
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002967 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05002968
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002969 ebitmap_destroy(&ctx_new.range.level[0].cat);
2970 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04002971 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002972 rc = 0;
2973 }
2974
2975netlbl_secattr_to_sid_return:
James Morris0804d112008-06-06 18:40:29 +10002976 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002977 return rc;
2978netlbl_secattr_to_sid_return_cleanup:
2979 ebitmap_destroy(&ctx_new.range.level[0].cat);
2980 goto netlbl_secattr_to_sid_return;
2981}
2982
2983/**
Paul Moore5778eab2007-02-28 15:14:22 -05002984 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
2985 * @sid: the SELinux SID
2986 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002987 *
2988 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002989 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
2990 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002991 *
2992 */
Paul Moore5778eab2007-02-28 15:14:22 -05002993int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002994{
Paul Moore99d854d2008-10-10 10:16:30 -04002995 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002996 struct context *ctx;
2997
2998 if (!ss_initialized)
2999 return 0;
3000
James Morris0804d112008-06-06 18:40:29 +10003001 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003002 ctx = sidtab_search(&sidtab, sid);
Paul Moore99d854d2008-10-10 10:16:30 -04003003 if (ctx == NULL) {
3004 rc = -ENOENT;
Paul Moore5778eab2007-02-28 15:14:22 -05003005 goto netlbl_sid_to_secattr_failure;
Paul Moore99d854d2008-10-10 10:16:30 -04003006 }
Paul Moore5778eab2007-02-28 15:14:22 -05003007 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
3008 GFP_ATOMIC);
Paul Moore99d854d2008-10-10 10:16:30 -04003009 if (secattr->domain == NULL) {
3010 rc = -ENOMEM;
3011 goto netlbl_sid_to_secattr_failure;
3012 }
Paul Moore8d758992008-10-10 10:16:33 -04003013 secattr->attr.secid = sid;
3014 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
Paul Moore5778eab2007-02-28 15:14:22 -05003015 mls_export_netlbl_lvl(ctx, secattr);
3016 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04003017 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05003018 goto netlbl_sid_to_secattr_failure;
James Morris0804d112008-06-06 18:40:29 +10003019 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003020
Paul Moore5778eab2007-02-28 15:14:22 -05003021 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05003022
Paul Moore5778eab2007-02-28 15:14:22 -05003023netlbl_sid_to_secattr_failure:
James Morris0804d112008-06-06 18:40:29 +10003024 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08003025 return rc;
3026}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003027#endif /* CONFIG_NETLABEL */