blob: 494ff527c174e81f84daac44a3c31aec275a4a8f [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 *
Guido Trentalancia0719aaf2010-02-03 16:40:20 +010029 * Updated: Guido Trentalancia <guido@trentalancia.com>
30 *
31 * Added support for runtime switching of the policy type
32 *
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +090033 * Copyright (C) 2008, 2009 NEC Corporation
Paul Moore3bb56b22008-01-29 08:38:19 -050034 * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P.
Darrel Goeddel376bd9c2006-02-24 15:44:05 -060035 * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc.
Chad Sellersb94c7e62006-11-06 12:38:18 -050036 * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com>
38 * This program is free software; you can redistribute it and/or modify
Eric Paris5d55a342008-04-18 17:38:33 -040039 * it under the terms of the GNU General Public License as published by
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * the Free Software Foundation, version 2.
41 */
42#include <linux/kernel.h>
43#include <linux/slab.h>
44#include <linux/string.h>
45#include <linux/spinlock.h>
Paul Moore9f2ad662006-11-17 17:38:53 -050046#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/errno.h>
48#include <linux/in.h>
49#include <linux/sched.h>
50#include <linux/audit.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080051#include <linux/mutex.h>
Adrian Bunk0e55a002008-03-31 01:54:02 +030052#include <linux/selinux.h>
Eric Paris6371dcd2010-07-29 23:02:34 -040053#include <linux/flex_array.h>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070054#include <net/netlabel.h>
Ingo Molnarbb0030792006-03-22 00:09:14 -080055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "flask.h"
57#include "avc.h"
58#include "avc_ss.h"
59#include "security.h"
60#include "context.h"
61#include "policydb.h"
62#include "sidtab.h"
63#include "services.h"
64#include "conditional.h"
65#include "mls.h"
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070066#include "objsec.h"
Paul Moorec60475b2007-02-28 15:14:23 -050067#include "netlabel.h"
Paul Moore3de4bab2006-11-17 17:38:54 -050068#include "xfrm.h"
Paul Moore02752762006-11-29 13:18:18 -050069#include "ebitmap.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020070#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72extern void selnl_notify_policyload(u32 seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Paul Moore3bb56b22008-01-29 08:38:19 -050074int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050075int selinux_policycap_openperm;
Paul Moore3bb56b22008-01-29 08:38:19 -050076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static DEFINE_RWLOCK(policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static struct sidtab sidtab;
80struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040081int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * The largest sequence number that has been used when
85 * providing an access decision to the access vector cache.
86 * The sequence number only changes when a policy change
87 * occurs.
88 */
Eric Paris5d55a342008-04-18 17:38:33 -040089static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/* Forward declaration. */
92static int context_struct_to_string(struct context *context, char **scontext,
93 u32 *scontext_len);
94
Stephen Smalley19439d02010-01-14 17:28:10 -050095static void context_struct_compute_av(struct context *scontext,
96 struct context *tcontext,
97 u16 tclass,
98 struct av_decision *avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -040099
100struct selinux_mapping {
101 u16 value; /* policy value */
102 unsigned num_perms;
103 u32 perms[sizeof(u32) * 8];
104};
105
106static struct selinux_mapping *current_mapping;
107static u16 current_mapping_size;
108
109static int selinux_set_mapping(struct policydb *pol,
110 struct security_class_mapping *map,
111 struct selinux_mapping **out_map_p,
112 u16 *out_map_size)
113{
114 struct selinux_mapping *out_map = NULL;
115 size_t size = sizeof(struct selinux_mapping);
116 u16 i, j;
117 unsigned k;
118 bool print_unknown_handle = false;
119
120 /* Find number of classes in the input mapping */
121 if (!map)
122 return -EINVAL;
123 i = 0;
124 while (map[i].name)
125 i++;
126
127 /* Allocate space for the class records, plus one for class zero */
128 out_map = kcalloc(++i, size, GFP_ATOMIC);
129 if (!out_map)
130 return -ENOMEM;
131
132 /* Store the raw class and permission values */
133 j = 0;
134 while (map[j].name) {
135 struct security_class_mapping *p_in = map + (j++);
136 struct selinux_mapping *p_out = out_map + j;
137
138 /* An empty class string skips ahead */
139 if (!strcmp(p_in->name, "")) {
140 p_out->num_perms = 0;
141 continue;
142 }
143
144 p_out->value = string_to_security_class(pol, p_in->name);
145 if (!p_out->value) {
146 printk(KERN_INFO
147 "SELinux: Class %s not defined in policy.\n",
148 p_in->name);
149 if (pol->reject_unknown)
150 goto err;
151 p_out->num_perms = 0;
152 print_unknown_handle = true;
153 continue;
154 }
155
156 k = 0;
157 while (p_in->perms && p_in->perms[k]) {
158 /* An empty permission string skips ahead */
159 if (!*p_in->perms[k]) {
160 k++;
161 continue;
162 }
163 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
164 p_in->perms[k]);
165 if (!p_out->perms[k]) {
166 printk(KERN_INFO
167 "SELinux: Permission %s in class %s not defined in policy.\n",
168 p_in->perms[k], p_in->name);
169 if (pol->reject_unknown)
170 goto err;
171 print_unknown_handle = true;
172 }
173
174 k++;
175 }
176 p_out->num_perms = k;
177 }
178
179 if (print_unknown_handle)
180 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
181 pol->allow_unknown ? "allowed" : "denied");
182
183 *out_map_p = out_map;
184 *out_map_size = i;
185 return 0;
186err:
187 kfree(out_map);
188 return -EINVAL;
189}
190
191/*
192 * Get real, policy values from mapped values
193 */
194
195static u16 unmap_class(u16 tclass)
196{
197 if (tclass < current_mapping_size)
198 return current_mapping[tclass].value;
199
200 return tclass;
201}
202
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400203static void map_decision(u16 tclass, struct av_decision *avd,
204 int allow_unknown)
205{
206 if (tclass < current_mapping_size) {
207 unsigned i, n = current_mapping[tclass].num_perms;
208 u32 result;
209
210 for (i = 0, result = 0; i < n; i++) {
211 if (avd->allowed & current_mapping[tclass].perms[i])
212 result |= 1<<i;
213 if (allow_unknown && !current_mapping[tclass].perms[i])
214 result |= 1<<i;
215 }
216 avd->allowed = result;
217
218 for (i = 0, result = 0; i < n; i++)
219 if (avd->auditallow & current_mapping[tclass].perms[i])
220 result |= 1<<i;
221 avd->auditallow = result;
222
223 for (i = 0, result = 0; i < n; i++) {
224 if (avd->auditdeny & current_mapping[tclass].perms[i])
225 result |= 1<<i;
226 if (!allow_unknown && !current_mapping[tclass].perms[i])
227 result |= 1<<i;
228 }
Eric Paris0bce9522009-11-23 16:47:23 -0500229 /*
230 * In case the kernel has a bug and requests a permission
231 * between num_perms and the maximum permission number, we
232 * should audit that denial
233 */
234 for (; i < (sizeof(u32)*8); i++)
235 result |= 1<<i;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400236 avd->auditdeny = result;
237 }
238}
239
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100240int security_mls_enabled(void)
241{
242 return policydb.mls_enabled;
243}
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245/*
246 * Return the boolean value of a constraint expression
247 * when it is applied to the specified source and target
248 * security contexts.
249 *
250 * xcontext is a special beast... It is used by the validatetrans rules
251 * only. For these rules, scontext is the context before the transition,
252 * tcontext is the context after the transition, and xcontext is the context
253 * of the process performing the transition. All other callers of
254 * constraint_expr_eval should pass in NULL for xcontext.
255 */
256static int constraint_expr_eval(struct context *scontext,
257 struct context *tcontext,
258 struct context *xcontext,
259 struct constraint_expr *cexpr)
260{
261 u32 val1, val2;
262 struct context *c;
263 struct role_datum *r1, *r2;
264 struct mls_level *l1, *l2;
265 struct constraint_expr *e;
266 int s[CEXPR_MAXDEPTH];
267 int sp = -1;
268
269 for (e = cexpr; e; e = e->next) {
270 switch (e->expr_type) {
271 case CEXPR_NOT:
272 BUG_ON(sp < 0);
273 s[sp] = !s[sp];
274 break;
275 case CEXPR_AND:
276 BUG_ON(sp < 1);
277 sp--;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800278 s[sp] &= s[sp + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 break;
280 case CEXPR_OR:
281 BUG_ON(sp < 1);
282 sp--;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800283 s[sp] |= s[sp + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 case CEXPR_ATTR:
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800286 if (sp == (CEXPR_MAXDEPTH - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return 0;
288 switch (e->attr) {
289 case CEXPR_USER:
290 val1 = scontext->user;
291 val2 = tcontext->user;
292 break;
293 case CEXPR_TYPE:
294 val1 = scontext->type;
295 val2 = tcontext->type;
296 break;
297 case CEXPR_ROLE:
298 val1 = scontext->role;
299 val2 = tcontext->role;
300 r1 = policydb.role_val_to_struct[val1 - 1];
301 r2 = policydb.role_val_to_struct[val2 - 1];
302 switch (e->op) {
303 case CEXPR_DOM:
304 s[++sp] = ebitmap_get_bit(&r1->dominates,
305 val2 - 1);
306 continue;
307 case CEXPR_DOMBY:
308 s[++sp] = ebitmap_get_bit(&r2->dominates,
309 val1 - 1);
310 continue;
311 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400312 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
313 val2 - 1) &&
314 !ebitmap_get_bit(&r2->dominates,
315 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 continue;
317 default:
318 break;
319 }
320 break;
321 case CEXPR_L1L2:
322 l1 = &(scontext->range.level[0]);
323 l2 = &(tcontext->range.level[0]);
324 goto mls_ops;
325 case CEXPR_L1H2:
326 l1 = &(scontext->range.level[0]);
327 l2 = &(tcontext->range.level[1]);
328 goto mls_ops;
329 case CEXPR_H1L2:
330 l1 = &(scontext->range.level[1]);
331 l2 = &(tcontext->range.level[0]);
332 goto mls_ops;
333 case CEXPR_H1H2:
334 l1 = &(scontext->range.level[1]);
335 l2 = &(tcontext->range.level[1]);
336 goto mls_ops;
337 case CEXPR_L1H1:
338 l1 = &(scontext->range.level[0]);
339 l2 = &(scontext->range.level[1]);
340 goto mls_ops;
341 case CEXPR_L2H2:
342 l1 = &(tcontext->range.level[0]);
343 l2 = &(tcontext->range.level[1]);
344 goto mls_ops;
345mls_ops:
346 switch (e->op) {
347 case CEXPR_EQ:
348 s[++sp] = mls_level_eq(l1, l2);
349 continue;
350 case CEXPR_NEQ:
351 s[++sp] = !mls_level_eq(l1, l2);
352 continue;
353 case CEXPR_DOM:
354 s[++sp] = mls_level_dom(l1, l2);
355 continue;
356 case CEXPR_DOMBY:
357 s[++sp] = mls_level_dom(l2, l1);
358 continue;
359 case CEXPR_INCOMP:
360 s[++sp] = mls_level_incomp(l2, l1);
361 continue;
362 default:
363 BUG();
364 return 0;
365 }
366 break;
367 default:
368 BUG();
369 return 0;
370 }
371
372 switch (e->op) {
373 case CEXPR_EQ:
374 s[++sp] = (val1 == val2);
375 break;
376 case CEXPR_NEQ:
377 s[++sp] = (val1 != val2);
378 break;
379 default:
380 BUG();
381 return 0;
382 }
383 break;
384 case CEXPR_NAMES:
385 if (sp == (CEXPR_MAXDEPTH-1))
386 return 0;
387 c = scontext;
388 if (e->attr & CEXPR_TARGET)
389 c = tcontext;
390 else if (e->attr & CEXPR_XTARGET) {
391 c = xcontext;
392 if (!c) {
393 BUG();
394 return 0;
395 }
396 }
397 if (e->attr & CEXPR_USER)
398 val1 = c->user;
399 else if (e->attr & CEXPR_ROLE)
400 val1 = c->role;
401 else if (e->attr & CEXPR_TYPE)
402 val1 = c->type;
403 else {
404 BUG();
405 return 0;
406 }
407
408 switch (e->op) {
409 case CEXPR_EQ:
410 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
411 break;
412 case CEXPR_NEQ:
413 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
414 break;
415 default:
416 BUG();
417 return 0;
418 }
419 break;
420 default:
421 BUG();
422 return 0;
423 }
424 }
425
426 BUG_ON(sp != 0);
427 return s[0];
428}
429
430/*
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900431 * security_dump_masked_av - dumps masked permissions during
432 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
433 */
434static int dump_masked_av_helper(void *k, void *d, void *args)
435{
436 struct perm_datum *pdatum = d;
437 char **permission_names = args;
438
439 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
440
441 permission_names[pdatum->value - 1] = (char *)k;
442
443 return 0;
444}
445
446static void security_dump_masked_av(struct context *scontext,
447 struct context *tcontext,
448 u16 tclass,
449 u32 permissions,
450 const char *reason)
451{
452 struct common_datum *common_dat;
453 struct class_datum *tclass_dat;
454 struct audit_buffer *ab;
455 char *tclass_name;
456 char *scontext_name = NULL;
457 char *tcontext_name = NULL;
458 char *permission_names[32];
James Morris2da5d312010-02-16 17:29:06 +1100459 int index;
460 u32 length;
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900461 bool need_comma = false;
462
463 if (!permissions)
464 return;
465
466 tclass_name = policydb.p_class_val_to_name[tclass - 1];
467 tclass_dat = policydb.class_val_to_struct[tclass - 1];
468 common_dat = tclass_dat->comdatum;
469
470 /* init permission_names */
471 if (common_dat &&
472 hashtab_map(common_dat->permissions.table,
473 dump_masked_av_helper, permission_names) < 0)
474 goto out;
475
476 if (hashtab_map(tclass_dat->permissions.table,
477 dump_masked_av_helper, permission_names) < 0)
478 goto out;
479
480 /* get scontext/tcontext in text form */
481 if (context_struct_to_string(scontext,
482 &scontext_name, &length) < 0)
483 goto out;
484
485 if (context_struct_to_string(tcontext,
486 &tcontext_name, &length) < 0)
487 goto out;
488
489 /* audit a message */
490 ab = audit_log_start(current->audit_context,
491 GFP_ATOMIC, AUDIT_SELINUX_ERR);
492 if (!ab)
493 goto out;
494
495 audit_log_format(ab, "op=security_compute_av reason=%s "
496 "scontext=%s tcontext=%s tclass=%s perms=",
497 reason, scontext_name, tcontext_name, tclass_name);
498
499 for (index = 0; index < 32; index++) {
500 u32 mask = (1 << index);
501
502 if ((mask & permissions) == 0)
503 continue;
504
505 audit_log_format(ab, "%s%s",
506 need_comma ? "," : "",
507 permission_names[index]
508 ? permission_names[index] : "????");
509 need_comma = true;
510 }
511 audit_log_end(ab);
512out:
513 /* release scontext/tcontext */
514 kfree(tcontext_name);
515 kfree(scontext_name);
516
517 return;
518}
519
520/*
KaiGai Koheid9250de2008-08-28 16:35:57 +0900521 * security_boundary_permission - drops violated permissions
522 * on boundary constraint.
523 */
524static void type_attribute_bounds_av(struct context *scontext,
525 struct context *tcontext,
526 u16 tclass,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900527 struct av_decision *avd)
528{
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900529 struct context lo_scontext;
530 struct context lo_tcontext;
531 struct av_decision lo_avd;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900532 struct type_datum *source
533 = policydb.type_val_to_struct[scontext->type - 1];
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900534 struct type_datum *target
535 = policydb.type_val_to_struct[tcontext->type - 1];
536 u32 masked = 0;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900537
538 if (source->bounds) {
539 memset(&lo_avd, 0, sizeof(lo_avd));
540
541 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
542 lo_scontext.type = source->bounds;
543
544 context_struct_compute_av(&lo_scontext,
545 tcontext,
546 tclass,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900547 &lo_avd);
548 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
549 return; /* no masked permission */
550 masked = ~lo_avd.allowed & avd->allowed;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900551 }
KaiGai Koheid9250de2008-08-28 16:35:57 +0900552
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900553 if (target->bounds) {
554 memset(&lo_avd, 0, sizeof(lo_avd));
555
556 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
557 lo_tcontext.type = target->bounds;
558
559 context_struct_compute_av(scontext,
560 &lo_tcontext,
561 tclass,
562 &lo_avd);
563 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
564 return; /* no masked permission */
565 masked = ~lo_avd.allowed & avd->allowed;
566 }
567
568 if (source->bounds && target->bounds) {
569 memset(&lo_avd, 0, sizeof(lo_avd));
570 /*
571 * lo_scontext and lo_tcontext are already
572 * set up.
573 */
574
575 context_struct_compute_av(&lo_scontext,
576 &lo_tcontext,
577 tclass,
578 &lo_avd);
579 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
580 return; /* no masked permission */
581 masked = ~lo_avd.allowed & avd->allowed;
582 }
583
584 if (masked) {
KaiGai Koheid9250de2008-08-28 16:35:57 +0900585 /* mask violated permissions */
586 avd->allowed &= ~masked;
587
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900588 /* audit masked permissions */
589 security_dump_masked_av(scontext, tcontext,
590 tclass, masked, "bounds");
KaiGai Koheid9250de2008-08-28 16:35:57 +0900591 }
592}
593
594/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Compute access vectors based on a context structure pair for
596 * the permissions in a particular class.
597 */
Stephen Smalley19439d02010-01-14 17:28:10 -0500598static void context_struct_compute_av(struct context *scontext,
599 struct context *tcontext,
600 u16 tclass,
601 struct av_decision *avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct constraint_node *constraint;
604 struct role_allow *ra;
605 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700606 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700608 struct ebitmap *sattr, *tattr;
609 struct ebitmap_node *snode, *tnode;
610 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 avd->allowed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 avd->auditallow = 0;
614 avd->auditdeny = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400616 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
617 if (printk_ratelimit())
618 printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
Stephen Smalley19439d02010-01-14 17:28:10 -0500619 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400620 }
Eric Paris3f120702007-09-21 14:37:10 -0400621
622 tclass_datum = policydb.class_val_to_struct[tclass - 1];
623
624 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 * If a specific type enforcement rule was defined for
626 * this permission check, then use it.
627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700629 avkey.specified = AVTAB_AV;
Eric Paris6371dcd2010-07-29 23:02:34 -0400630 sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
631 BUG_ON(!sattr);
632 tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
633 BUG_ON(!tattr);
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900634 ebitmap_for_each_positive_bit(sattr, snode, i) {
635 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700636 avkey.source_type = i + 1;
637 avkey.target_type = j + 1;
638 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300639 node;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700640 node = avtab_search_node_next(node, avkey.specified)) {
641 if (node->key.specified == AVTAB_ALLOWED)
642 avd->allowed |= node->datum.data;
643 else if (node->key.specified == AVTAB_AUDITALLOW)
644 avd->auditallow |= node->datum.data;
645 else if (node->key.specified == AVTAB_AUDITDENY)
646 avd->auditdeny &= node->datum.data;
647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Stephen Smalley782ebb92005-09-03 15:55:16 -0700649 /* Check conditional av table for additional permissions */
650 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
651
652 }
653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /*
656 * Remove any permissions prohibited by a constraint (this includes
657 * the MLS policy).
658 */
659 constraint = tclass_datum->constraints;
660 while (constraint) {
661 if ((constraint->permissions & (avd->allowed)) &&
662 !constraint_expr_eval(scontext, tcontext, NULL,
663 constraint->expr)) {
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900664 avd->allowed &= ~(constraint->permissions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666 constraint = constraint->next;
667 }
668
669 /*
670 * If checking process transition permission and the
671 * role is changing, then check the (current_role, new_role)
672 * pair.
673 */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400674 if (tclass == policydb.process_class &&
675 (avd->allowed & policydb.process_trans_perms) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 scontext->role != tcontext->role) {
677 for (ra = policydb.role_allow; ra; ra = ra->next) {
678 if (scontext->role == ra->role &&
679 tcontext->role == ra->new_role)
680 break;
681 }
682 if (!ra)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400683 avd->allowed &= ~policydb.process_trans_perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
KaiGai Koheid9250de2008-08-28 16:35:57 +0900686 /*
687 * If the given source and target types have boundary
688 * constraint, lazy checks have to mask any violated
689 * permission and notice it to userspace via audit.
690 */
691 type_attribute_bounds_av(scontext, tcontext,
Stephen Smalley19439d02010-01-14 17:28:10 -0500692 tclass, avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400696 struct context *ncontext,
697 struct context *tcontext,
698 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 char *o = NULL, *n = NULL, *t = NULL;
701 u32 olen, nlen, tlen;
702
703 if (context_struct_to_string(ocontext, &o, &olen) < 0)
704 goto out;
705 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
706 goto out;
707 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
708 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100709 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400710 "security_validate_transition: denied for"
711 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
712 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713out:
714 kfree(o);
715 kfree(n);
716 kfree(t);
717
718 if (!selinux_enforcing)
719 return 0;
720 return -EPERM;
721}
722
723int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400724 u16 orig_tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 struct context *ocontext;
727 struct context *ncontext;
728 struct context *tcontext;
729 struct class_datum *tclass_datum;
730 struct constraint_node *constraint;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400731 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int rc = 0;
733
734 if (!ss_initialized)
735 return 0;
736
James Morris0804d112008-06-06 18:40:29 +1000737 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400739 tclass = unmap_class(orig_tclass);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400742 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
743 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 rc = -EINVAL;
745 goto out;
746 }
747 tclass_datum = policydb.class_val_to_struct[tclass - 1];
748
749 ocontext = sidtab_search(&sidtab, oldsid);
750 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400751 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
752 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 rc = -EINVAL;
754 goto out;
755 }
756
757 ncontext = sidtab_search(&sidtab, newsid);
758 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400759 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
760 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 rc = -EINVAL;
762 goto out;
763 }
764
765 tcontext = sidtab_search(&sidtab, tasksid);
766 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400767 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
768 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 rc = -EINVAL;
770 goto out;
771 }
772
773 constraint = tclass_datum->validatetrans;
774 while (constraint) {
775 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400776 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400778 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 goto out;
780 }
781 constraint = constraint->next;
782 }
783
784out:
James Morris0804d112008-06-06 18:40:29 +1000785 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 return rc;
787}
788
KaiGai Koheid9250de2008-08-28 16:35:57 +0900789/*
790 * security_bounded_transition - check whether the given
791 * transition is directed to bounded, or not.
792 * It returns 0, if @newsid is bounded by @oldsid.
793 * Otherwise, it returns error code.
794 *
795 * @oldsid : current security identifier
796 * @newsid : destinated security identifier
797 */
798int security_bounded_transition(u32 old_sid, u32 new_sid)
799{
800 struct context *old_context, *new_context;
801 struct type_datum *type;
802 int index;
803 int rc = -EINVAL;
804
805 read_lock(&policy_rwlock);
806
807 old_context = sidtab_search(&sidtab, old_sid);
808 if (!old_context) {
809 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
810 __func__, old_sid);
811 goto out;
812 }
813
814 new_context = sidtab_search(&sidtab, new_sid);
815 if (!new_context) {
816 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
817 __func__, new_sid);
818 goto out;
819 }
820
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200821 /* type/domain unchanged */
KaiGai Koheid9250de2008-08-28 16:35:57 +0900822 if (old_context->type == new_context->type) {
823 rc = 0;
824 goto out;
825 }
826
827 index = new_context->type;
828 while (true) {
829 type = policydb.type_val_to_struct[index - 1];
830 BUG_ON(!type);
831
832 /* not bounded anymore */
833 if (!type->bounds) {
834 rc = -EPERM;
835 break;
836 }
837
838 /* @newsid is bounded by @oldsid */
839 if (type->bounds == old_context->type) {
840 rc = 0;
841 break;
842 }
843 index = type->bounds;
844 }
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900845
846 if (rc) {
847 char *old_name = NULL;
848 char *new_name = NULL;
James Morris2da5d312010-02-16 17:29:06 +1100849 u32 length;
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900850
851 if (!context_struct_to_string(old_context,
852 &old_name, &length) &&
853 !context_struct_to_string(new_context,
854 &new_name, &length)) {
855 audit_log(current->audit_context,
856 GFP_ATOMIC, AUDIT_SELINUX_ERR,
857 "op=security_bounded_transition "
858 "result=denied "
859 "oldcontext=%s newcontext=%s",
860 old_name, new_name);
861 }
862 kfree(new_name);
863 kfree(old_name);
864 }
KaiGai Koheid9250de2008-08-28 16:35:57 +0900865out:
866 read_unlock(&policy_rwlock);
867
868 return rc;
869}
870
Stephen Smalley19439d02010-01-14 17:28:10 -0500871static void avd_init(struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400872{
Stephen Smalley19439d02010-01-14 17:28:10 -0500873 avd->allowed = 0;
874 avd->auditallow = 0;
875 avd->auditdeny = 0xffffffff;
876 avd->seqno = latest_granting;
877 avd->flags = 0;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400878}
879
Stephen Smalley19439d02010-01-14 17:28:10 -0500880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881/**
882 * security_compute_av - Compute access vector decisions.
883 * @ssid: source security identifier
884 * @tsid: target security identifier
885 * @tclass: target security class
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * @avd: access vector decisions
887 *
888 * Compute a set of access vector decisions based on the
889 * SID pair (@ssid, @tsid) for the permissions in @tclass.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 */
Stephen Smalley19439d02010-01-14 17:28:10 -0500891void security_compute_av(u32 ssid,
892 u32 tsid,
893 u16 orig_tclass,
894 struct av_decision *avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400896 u16 tclass;
Stephen Smalley19439d02010-01-14 17:28:10 -0500897 struct context *scontext = NULL, *tcontext = NULL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400898
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400899 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500900 avd_init(avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400901 if (!ss_initialized)
902 goto allow;
903
Stephen Smalley19439d02010-01-14 17:28:10 -0500904 scontext = sidtab_search(&sidtab, ssid);
905 if (!scontext) {
906 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
907 __func__, ssid);
908 goto out;
909 }
910
911 /* permissive domain? */
912 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
913 avd->flags |= AVD_FLAGS_PERMISSIVE;
914
915 tcontext = sidtab_search(&sidtab, tsid);
916 if (!tcontext) {
917 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
918 __func__, tsid);
919 goto out;
920 }
921
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400922 tclass = unmap_class(orig_tclass);
923 if (unlikely(orig_tclass && !tclass)) {
924 if (policydb.allow_unknown)
925 goto allow;
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400926 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400927 }
Stephen Smalley19439d02010-01-14 17:28:10 -0500928 context_struct_compute_av(scontext, tcontext, tclass, avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400929 map_decision(orig_tclass, avd, policydb.allow_unknown);
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400930out:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400931 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500932 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400933allow:
934 avd->allowed = 0xffffffff;
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400935 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400936}
937
Stephen Smalley19439d02010-01-14 17:28:10 -0500938void security_compute_av_user(u32 ssid,
939 u32 tsid,
940 u16 tclass,
941 struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400942{
Stephen Smalley19439d02010-01-14 17:28:10 -0500943 struct context *scontext = NULL, *tcontext = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
James Morris0804d112008-06-06 18:40:29 +1000945 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500946 avd_init(avd);
947 if (!ss_initialized)
948 goto allow;
949
950 scontext = sidtab_search(&sidtab, ssid);
951 if (!scontext) {
952 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
953 __func__, ssid);
954 goto out;
955 }
956
957 /* permissive domain? */
958 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
959 avd->flags |= AVD_FLAGS_PERMISSIVE;
960
961 tcontext = sidtab_search(&sidtab, tsid);
962 if (!tcontext) {
963 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
964 __func__, tsid);
965 goto out;
966 }
967
968 if (unlikely(!tclass)) {
969 if (policydb.allow_unknown)
970 goto allow;
971 goto out;
972 }
973
974 context_struct_compute_av(scontext, tcontext, tclass, avd);
975 out:
James Morris0804d112008-06-06 18:40:29 +1000976 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500977 return;
978allow:
979 avd->allowed = 0xffffffff;
980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
983/*
984 * Write the security context string representation of
985 * the context structure `context' into a dynamically
986 * allocated string of the correct size. Set `*scontext'
987 * to point to this string and set `*scontext_len' to
988 * the length of the string.
989 */
990static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
991{
992 char *scontextp;
993
994 *scontext = NULL;
995 *scontext_len = 0;
996
Stephen Smalley12b29f32008-05-07 13:03:20 -0400997 if (context->len) {
998 *scontext_len = context->len;
999 *scontext = kstrdup(context->str, GFP_ATOMIC);
1000 if (!(*scontext))
1001 return -ENOMEM;
1002 return 0;
1003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /* Compute the size of the context. */
1006 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
1007 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
1008 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
1009 *scontext_len += mls_compute_context_len(context);
1010
1011 /* Allocate space for the context; caller must free this space. */
1012 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -04001013 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 *scontext = scontextp;
1016
1017 /*
1018 * Copy the user name, role name and type name into the context.
1019 */
1020 sprintf(scontextp, "%s:%s:%s",
1021 policydb.p_user_val_to_name[context->user - 1],
1022 policydb.p_role_val_to_name[context->role - 1],
1023 policydb.p_type_val_to_name[context->type - 1]);
1024 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -04001025 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
1026 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 mls_sid_to_context(context, &scontextp);
1029
1030 *scontextp = 0;
1031
1032 return 0;
1033}
1034
1035#include "initial_sid_to_string.h"
1036
James Carterf0ee2e42007-04-04 10:11:29 -04001037const char *security_get_initial_sid_context(u32 sid)
1038{
1039 if (unlikely(sid > SECINITSID_NUM))
1040 return NULL;
1041 return initial_sid_to_string[sid];
1042}
1043
Stephen Smalley12b29f32008-05-07 13:03:20 -04001044static int security_sid_to_context_core(u32 sid, char **scontext,
1045 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct context *context;
1048 int rc = 0;
1049
Stephen Smalley4f4acf32007-02-26 12:02:34 -05001050 *scontext = NULL;
1051 *scontext_len = 0;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (!ss_initialized) {
1054 if (sid <= SECINITSID_NUM) {
1055 char *scontextp;
1056
1057 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001058 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -07001059 if (!scontextp) {
1060 rc = -ENOMEM;
1061 goto out;
1062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 strcpy(scontextp, initial_sid_to_string[sid]);
1064 *scontext = scontextp;
1065 goto out;
1066 }
Eric Paris744ba352008-04-17 11:52:44 -04001067 printk(KERN_ERR "SELinux: %s: called before initial "
1068 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 rc = -EINVAL;
1070 goto out;
1071 }
James Morris0804d112008-06-06 18:40:29 +10001072 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001073 if (force)
1074 context = sidtab_search_force(&sidtab, sid);
1075 else
1076 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -04001078 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1079 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 rc = -EINVAL;
1081 goto out_unlock;
1082 }
1083 rc = context_struct_to_string(context, scontext, scontext_len);
1084out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001085 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086out:
1087 return rc;
1088
1089}
1090
Stephen Smalley12b29f32008-05-07 13:03:20 -04001091/**
1092 * security_sid_to_context - Obtain a context for a given SID.
1093 * @sid: security identifier, SID
1094 * @scontext: security context
1095 * @scontext_len: length in bytes
1096 *
1097 * Write the string representation of the context associated with @sid
1098 * into a dynamically allocated string of the correct size. Set @scontext
1099 * to point to this string and set @scontext_len to the length of the string.
1100 */
1101int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Stephen Smalley12b29f32008-05-07 13:03:20 -04001103 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
1104}
1105
1106int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
1107{
1108 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
1109}
1110
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001111/*
1112 * Caveat: Mutates scontext.
1113 */
Stephen Smalley12b29f32008-05-07 13:03:20 -04001114static int string_to_context_struct(struct policydb *pol,
1115 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001116 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001117 u32 scontext_len,
1118 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001119 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 struct role_datum *role;
1122 struct type_datum *typdatum;
1123 struct user_datum *usrdatum;
1124 char *scontextp, *p, oldc;
1125 int rc = 0;
1126
Stephen Smalley12b29f32008-05-07 13:03:20 -04001127 context_init(ctx);
1128
Stephen Smalley12b29f32008-05-07 13:03:20 -04001129 /* Parse the security context. */
1130
1131 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001132 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001133
1134 /* Extract the user. */
1135 p = scontextp;
1136 while (*p && *p != ':')
1137 p++;
1138
1139 if (*p == 0)
1140 goto out;
1141
1142 *p++ = 0;
1143
1144 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1145 if (!usrdatum)
1146 goto out;
1147
1148 ctx->user = usrdatum->value;
1149
1150 /* Extract role. */
1151 scontextp = p;
1152 while (*p && *p != ':')
1153 p++;
1154
1155 if (*p == 0)
1156 goto out;
1157
1158 *p++ = 0;
1159
1160 role = hashtab_search(pol->p_roles.table, scontextp);
1161 if (!role)
1162 goto out;
1163 ctx->role = role->value;
1164
1165 /* Extract type. */
1166 scontextp = p;
1167 while (*p && *p != ':')
1168 p++;
1169 oldc = *p;
1170 *p++ = 0;
1171
1172 typdatum = hashtab_search(pol->p_types.table, scontextp);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001173 if (!typdatum || typdatum->attribute)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001174 goto out;
1175
1176 ctx->type = typdatum->value;
1177
1178 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
1179 if (rc)
1180 goto out;
1181
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001182 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001183 rc = -EINVAL;
1184 goto out;
1185 }
1186
1187 /* Check the validity of the new context. */
1188 if (!policydb_context_isvalid(pol, ctx)) {
1189 rc = -EINVAL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001190 goto out;
1191 }
1192 rc = 0;
1193out:
Eric Paris8e531af2008-09-03 11:49:47 -04001194 if (rc)
1195 context_destroy(ctx);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001196 return rc;
1197}
1198
1199static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
1200 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1201 int force)
1202{
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001203 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001204 struct context context;
1205 int rc = 0;
1206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 if (!ss_initialized) {
1208 int i;
1209
1210 for (i = 1; i < SECINITSID_NUM; i++) {
1211 if (!strcmp(initial_sid_to_string[i], scontext)) {
1212 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 }
1216 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 *sid = SECSID_NULL;
1220
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001221 /* Copy the string so that we can modify the copy as we parse it. */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001222 scontext2 = kmalloc(scontext_len + 1, gfp_flags);
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001223 if (!scontext2)
1224 return -ENOMEM;
1225 memcpy(scontext2, scontext, scontext_len);
1226 scontext2[scontext_len] = 0;
1227
1228 if (force) {
1229 /* Save another copy for storing in uninterpreted form */
1230 str = kstrdup(scontext2, gfp_flags);
1231 if (!str) {
1232 kfree(scontext2);
1233 return -ENOMEM;
1234 }
1235 }
1236
James Morris0804d112008-06-06 18:40:29 +10001237 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001238 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001239 scontext2, scontext_len,
1240 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001241 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001242 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001243 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001244 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001245 } else if (rc)
1246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Eric Paris8e531af2008-09-03 11:49:47 -04001248 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249out:
James Morris0804d112008-06-06 18:40:29 +10001250 read_unlock(&policy_rwlock);
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001251 kfree(scontext2);
1252 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return rc;
1254}
1255
James Morrisf5c1d5b2005-07-28 01:07:37 -07001256/**
1257 * security_context_to_sid - Obtain a SID for a given security context.
1258 * @scontext: security context
1259 * @scontext_len: length in bytes
1260 * @sid: security identifier, SID
1261 *
1262 * Obtains a SID associated with the security context that
1263 * has the string representation specified by @scontext.
1264 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1265 * memory is available, or 0 on success.
1266 */
David Howells8f0cfa52008-04-29 00:59:41 -07001267int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001268{
1269 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001270 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001271}
1272
1273/**
1274 * security_context_to_sid_default - Obtain a SID for a given security context,
1275 * falling back to specified default if needed.
1276 *
1277 * @scontext: security context
1278 * @scontext_len: length in bytes
1279 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -07001280 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -07001281 *
1282 * Obtains a SID associated with the security context that
1283 * has the string representation specified by @scontext.
1284 * The default SID is passed to the MLS layer to be used to allow
1285 * kernel labeling of the MLS field if the MLS field is not present
1286 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -04001287 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -07001288 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1289 * memory is available, or 0 on success.
1290 */
David Howells7bf570d2008-04-29 20:52:51 +01001291int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1292 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001293{
1294 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001295 sid, def_sid, gfp_flags, 1);
1296}
1297
1298int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1299 u32 *sid)
1300{
1301 return security_context_to_sid_core(scontext, scontext_len,
1302 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001303}
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305static int compute_sid_handle_invalid_context(
1306 struct context *scontext,
1307 struct context *tcontext,
1308 u16 tclass,
1309 struct context *newcontext)
1310{
1311 char *s = NULL, *t = NULL, *n = NULL;
1312 u32 slen, tlen, nlen;
1313
1314 if (context_struct_to_string(scontext, &s, &slen) < 0)
1315 goto out;
1316 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
1317 goto out;
1318 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
1319 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001320 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 "security_compute_sid: invalid context %s"
1322 " for scontext=%s"
1323 " tcontext=%s"
1324 " tclass=%s",
1325 n, s, t, policydb.p_class_val_to_name[tclass-1]);
1326out:
1327 kfree(s);
1328 kfree(t);
1329 kfree(n);
1330 if (!selinux_enforcing)
1331 return 0;
1332 return -EACCES;
1333}
1334
1335static int security_compute_sid(u32 ssid,
1336 u32 tsid,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001337 u16 orig_tclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 u32 specified,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001339 u32 *out_sid,
1340 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1343 struct role_trans *roletr = NULL;
1344 struct avtab_key avkey;
1345 struct avtab_datum *avdatum;
1346 struct avtab_node *node;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001347 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 int rc = 0;
1349
1350 if (!ss_initialized) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001351 switch (orig_tclass) {
1352 case SECCLASS_PROCESS: /* kernel value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *out_sid = ssid;
1354 break;
1355 default:
1356 *out_sid = tsid;
1357 break;
1358 }
1359 goto out;
1360 }
1361
Venkat Yekkirala851f8a62006-07-30 03:03:18 -07001362 context_init(&newcontext);
1363
James Morris0804d112008-06-06 18:40:29 +10001364 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001366 if (kern)
1367 tclass = unmap_class(orig_tclass);
1368 else
1369 tclass = orig_tclass;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 scontext = sidtab_search(&sidtab, ssid);
1372 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001373 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1374 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 rc = -EINVAL;
1376 goto out_unlock;
1377 }
1378 tcontext = sidtab_search(&sidtab, tsid);
1379 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001380 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1381 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 rc = -EINVAL;
1383 goto out_unlock;
1384 }
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 /* Set the user identity. */
1387 switch (specified) {
1388 case AVTAB_TRANSITION:
1389 case AVTAB_CHANGE:
1390 /* Use the process user identity. */
1391 newcontext.user = scontext->user;
1392 break;
1393 case AVTAB_MEMBER:
1394 /* Use the related object owner. */
1395 newcontext.user = tcontext->user;
1396 break;
1397 }
1398
1399 /* Set the role and type to default values. */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001400 if (tclass == policydb.process_class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /* Use the current role and type of process. */
1402 newcontext.role = scontext->role;
1403 newcontext.type = scontext->type;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001404 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 /* Use the well-defined object role. */
1406 newcontext.role = OBJECT_R_VAL;
1407 /* Use the type of the related object. */
1408 newcontext.type = tcontext->type;
1409 }
1410
1411 /* Look for a type transition/member/change rule. */
1412 avkey.source_type = scontext->type;
1413 avkey.target_type = tcontext->type;
1414 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001415 avkey.specified = specified;
1416 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001419 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001420 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03001421 for (; node; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001422 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 avdatum = &node->datum;
1424 break;
1425 }
1426 }
1427 }
1428
Stephen Smalley782ebb92005-09-03 15:55:16 -07001429 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001431 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433
1434 /* Check for class-specific changes. */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001435 if (tclass == policydb.process_class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (specified & AVTAB_TRANSITION) {
1437 /* Look for a role transition rule. */
1438 for (roletr = policydb.role_tr; roletr;
1439 roletr = roletr->next) {
1440 if (roletr->role == scontext->role &&
1441 roletr->type == tcontext->type) {
1442 /* Use the role transition rule. */
1443 newcontext.role = roletr->new_role;
1444 break;
1445 }
1446 }
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449
1450 /* Set the MLS attributes.
1451 This is done last because it may allocate memory. */
1452 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1453 if (rc)
1454 goto out_unlock;
1455
1456 /* Check the validity of the context. */
1457 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1458 rc = compute_sid_handle_invalid_context(scontext,
1459 tcontext,
1460 tclass,
1461 &newcontext);
1462 if (rc)
1463 goto out_unlock;
1464 }
1465 /* Obtain the sid for the context. */
1466 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1467out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001468 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 context_destroy(&newcontext);
1470out:
1471 return rc;
1472}
1473
1474/**
1475 * security_transition_sid - Compute the SID for a new subject/object.
1476 * @ssid: source security identifier
1477 * @tsid: target security identifier
1478 * @tclass: target security class
1479 * @out_sid: security identifier for new subject/object
1480 *
1481 * Compute a SID to use for labeling a new subject or object in the
1482 * class @tclass based on a SID pair (@ssid, @tsid).
1483 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1484 * if insufficient memory is available, or %0 if the new SID was
1485 * computed successfully.
1486 */
1487int security_transition_sid(u32 ssid,
1488 u32 tsid,
1489 u16 tclass,
1490 u32 *out_sid)
1491{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001492 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1493 out_sid, true);
1494}
1495
1496int security_transition_sid_user(u32 ssid,
1497 u32 tsid,
1498 u16 tclass,
1499 u32 *out_sid)
1500{
1501 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1502 out_sid, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505/**
1506 * security_member_sid - Compute the SID for member selection.
1507 * @ssid: source security identifier
1508 * @tsid: target security identifier
1509 * @tclass: target security class
1510 * @out_sid: security identifier for selected member
1511 *
1512 * Compute a SID to use when selecting a member of a polyinstantiated
1513 * object of class @tclass based on a SID pair (@ssid, @tsid).
1514 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1515 * if insufficient memory is available, or %0 if the SID was
1516 * computed successfully.
1517 */
1518int security_member_sid(u32 ssid,
1519 u32 tsid,
1520 u16 tclass,
1521 u32 *out_sid)
1522{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001523 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid,
1524 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/**
1528 * security_change_sid - Compute the SID for object relabeling.
1529 * @ssid: source security identifier
1530 * @tsid: target security identifier
1531 * @tclass: target security class
1532 * @out_sid: security identifier for selected member
1533 *
1534 * Compute a SID to use for relabeling an object of class @tclass
1535 * based on a SID pair (@ssid, @tsid).
1536 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1537 * if insufficient memory is available, or %0 if the SID was
1538 * computed successfully.
1539 */
1540int security_change_sid(u32 ssid,
1541 u32 tsid,
1542 u16 tclass,
1543 u32 *out_sid)
1544{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001545 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid,
1546 false);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001547}
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/* Clone the SID into the new SID table. */
1550static int clone_sid(u32 sid,
1551 struct context *context,
1552 void *arg)
1553{
1554 struct sidtab *s = arg;
1555
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001556 if (sid > SECINITSID_NUM)
1557 return sidtab_insert(s, sid, context);
1558 else
1559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562static inline int convert_context_handle_invalid_context(struct context *context)
1563{
1564 int rc = 0;
1565
1566 if (selinux_enforcing) {
1567 rc = -EINVAL;
1568 } else {
1569 char *s;
1570 u32 len;
1571
Stephen Smalley12b29f32008-05-07 13:03:20 -04001572 if (!context_struct_to_string(context, &s, &len)) {
1573 printk(KERN_WARNING
1574 "SELinux: Context %s would be invalid if enforcing\n",
1575 s);
1576 kfree(s);
1577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 }
1579 return rc;
1580}
1581
1582struct convert_context_args {
1583 struct policydb *oldp;
1584 struct policydb *newp;
1585};
1586
1587/*
1588 * Convert the values in the security context
1589 * structure `c' from the values specified
1590 * in the policy `p->oldp' to the values specified
1591 * in the policy `p->newp'. Verify that the
1592 * context is valid under the new policy.
1593 */
1594static int convert_context(u32 key,
1595 struct context *c,
1596 void *p)
1597{
1598 struct convert_context_args *args;
1599 struct context oldc;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001600 struct ocontext *oc;
1601 struct mls_range *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 struct role_datum *role;
1603 struct type_datum *typdatum;
1604 struct user_datum *usrdatum;
1605 char *s;
1606 u32 len;
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001607 int rc = 0;
1608
1609 if (key <= SECINITSID_NUM)
1610 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 args = p;
1613
Stephen Smalley12b29f32008-05-07 13:03:20 -04001614 if (c->str) {
1615 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001616 s = kstrdup(c->str, GFP_KERNEL);
1617 if (!s) {
1618 rc = -ENOMEM;
1619 goto out;
1620 }
1621 rc = string_to_context_struct(args->newp, NULL, s,
1622 c->len, &ctx, SECSID_NULL);
1623 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001624 if (!rc) {
1625 printk(KERN_INFO
1626 "SELinux: Context %s became valid (mapped).\n",
1627 c->str);
1628 /* Replace string with mapped representation. */
1629 kfree(c->str);
1630 memcpy(c, &ctx, sizeof(*c));
1631 goto out;
1632 } else if (rc == -EINVAL) {
1633 /* Retain string representation for later mapping. */
1634 rc = 0;
1635 goto out;
1636 } else {
1637 /* Other error condition, e.g. ENOMEM. */
1638 printk(KERN_ERR
1639 "SELinux: Unable to map context %s, rc = %d.\n",
1640 c->str, -rc);
1641 goto out;
1642 }
1643 }
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 rc = context_cpy(&oldc, c);
1646 if (rc)
1647 goto out;
1648
1649 rc = -EINVAL;
1650
1651 /* Convert the user. */
1652 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001653 args->oldp->p_user_val_to_name[c->user - 1]);
1654 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 c->user = usrdatum->value;
1657
1658 /* Convert the role. */
1659 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001660 args->oldp->p_role_val_to_name[c->role - 1]);
1661 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 c->role = role->value;
1664
1665 /* Convert the type. */
1666 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001667 args->oldp->p_type_val_to_name[c->type - 1]);
1668 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 c->type = typdatum->value;
1671
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001672 /* Convert the MLS fields if dealing with MLS policies */
1673 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
1674 rc = mls_convert_context(args->oldp, args->newp, c);
1675 if (rc)
1676 goto bad;
1677 } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
1678 /*
1679 * Switching between MLS and non-MLS policy:
1680 * free any storage used by the MLS fields in the
1681 * context for all existing entries in the sidtab.
1682 */
1683 mls_context_destroy(c);
1684 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
1685 /*
1686 * Switching between non-MLS and MLS policy:
1687 * ensure that the MLS fields of the context for all
1688 * existing entries in the sidtab are filled in with a
1689 * suitable default value, likely taken from one of the
1690 * initial SIDs.
1691 */
1692 oc = args->newp->ocontexts[OCON_ISID];
1693 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
1694 oc = oc->next;
1695 if (!oc) {
1696 printk(KERN_ERR "SELinux: unable to look up"
1697 " the initial SIDs list\n");
1698 goto bad;
1699 }
1700 range = &oc->context[0].range;
1701 rc = mls_range_set(c, range);
1702 if (rc)
1703 goto bad;
1704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 /* Check the validity of the new context. */
1707 if (!policydb_context_isvalid(args->newp, c)) {
1708 rc = convert_context_handle_invalid_context(&oldc);
1709 if (rc)
1710 goto bad;
1711 }
1712
1713 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001714 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715out:
1716 return rc;
1717bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001718 /* Map old representation to string and save it. */
1719 if (context_struct_to_string(&oldc, &s, &len))
1720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001722 context_destroy(c);
1723 c->str = s;
1724 c->len = len;
1725 printk(KERN_INFO
1726 "SELinux: Context %s became invalid (unmapped).\n",
1727 c->str);
1728 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto out;
1730}
1731
Paul Moore3bb56b22008-01-29 08:38:19 -05001732static void security_load_policycaps(void)
1733{
1734 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1735 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001736 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1737 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001738}
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001741static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
1743/**
1744 * security_load_policy - Load a security policy configuration.
1745 * @data: binary policy data
1746 * @len: length of data in bytes
1747 *
1748 * Load a new set of security policy configuration data,
1749 * validate it and convert the SID table as necessary.
1750 * This function will flush the access vector cache after
1751 * loading the new policy.
1752 */
1753int security_load_policy(void *data, size_t len)
1754{
1755 struct policydb oldpolicydb, newpolicydb;
1756 struct sidtab oldsidtab, newsidtab;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001757 struct selinux_mapping *oldmap, *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 struct convert_context_args args;
1759 u32 seqno;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001760 u16 map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 int rc = 0;
1762 struct policy_file file = { data, len }, *fp = &file;
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (!ss_initialized) {
1765 avtab_cache_init();
Eric Parisa2000052010-04-20 10:29:42 -04001766 rc = policydb_read(&policydb, fp);
1767 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 avtab_cache_destroy();
Eric Parisa2000052010-04-20 10:29:42 -04001769 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Eric Parisa2000052010-04-20 10:29:42 -04001771
1772 rc = selinux_set_mapping(&policydb, secclass_map,
1773 &current_mapping,
1774 &current_mapping_size);
1775 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 policydb_destroy(&policydb);
1777 avtab_cache_destroy();
Eric Parisa2000052010-04-20 10:29:42 -04001778 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
Eric Parisa2000052010-04-20 10:29:42 -04001780
1781 rc = policydb_load_isids(&policydb, &sidtab);
1782 if (rc) {
Chad Sellersb94c7e62006-11-06 12:38:18 -05001783 policydb_destroy(&policydb);
1784 avtab_cache_destroy();
Eric Parisa2000052010-04-20 10:29:42 -04001785 return rc;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001786 }
Eric Parisa2000052010-04-20 10:29:42 -04001787
Paul Moore3bb56b22008-01-29 08:38:19 -05001788 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001790 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001792 avc_ss_reset(seqno);
1793 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09001794 selinux_status_update_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001795 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001796 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return 0;
1798 }
1799
1800#if 0
1801 sidtab_hash_eval(&sidtab, "sids");
1802#endif
1803
Eric Parisa2000052010-04-20 10:29:42 -04001804 rc = policydb_read(&newpolicydb, fp);
1805 if (rc)
1806 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001808 /* If switching between different policy types, log MLS status */
1809 if (policydb.mls_enabled && !newpolicydb.mls_enabled)
1810 printk(KERN_INFO "SELinux: Disabling MLS support...\n");
1811 else if (!policydb.mls_enabled && newpolicydb.mls_enabled)
1812 printk(KERN_INFO "SELinux: Enabling MLS support...\n");
1813
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001814 rc = policydb_load_isids(&newpolicydb, &newsidtab);
1815 if (rc) {
1816 printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
Stephen Smalley12b29f32008-05-07 13:03:20 -04001817 policydb_destroy(&newpolicydb);
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001818 return rc;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Eric Parisa2000052010-04-20 10:29:42 -04001821 rc = selinux_set_mapping(&newpolicydb, secclass_map, &map, &map_size);
1822 if (rc)
Chad Sellersb94c7e62006-11-06 12:38:18 -05001823 goto err;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001824
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001825 rc = security_preserve_bools(&newpolicydb);
1826 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001827 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001828 goto err;
1829 }
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /* Clone the SID table. */
1832 sidtab_shutdown(&sidtab);
Eric Parisa2000052010-04-20 10:29:42 -04001833
1834 rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
1835 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Stephen Smalley12b29f32008-05-07 13:03:20 -04001838 /*
1839 * Convert the internal representations of contexts
1840 * in the new SID table.
1841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 args.oldp = &policydb;
1843 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001844 rc = sidtab_map(&newsidtab, convert_context, &args);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001845 if (rc) {
1846 printk(KERN_ERR "SELinux: unable to convert the internal"
1847 " representation of contexts in the new SID"
1848 " table\n");
Stephen Smalley12b29f32008-05-07 13:03:20 -04001849 goto err;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 /* Save the old policydb and SID table to free later. */
1853 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1854 sidtab_set(&oldsidtab, &sidtab);
1855
1856 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10001857 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 memcpy(&policydb, &newpolicydb, sizeof policydb);
1859 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001860 security_load_policycaps();
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001861 oldmap = current_mapping;
1862 current_mapping = map;
1863 current_mapping_size = map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 seqno = ++latest_granting;
James Morris0804d112008-06-06 18:40:29 +10001865 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 /* Free the old policydb and SID table. */
1868 policydb_destroy(&oldpolicydb);
1869 sidtab_destroy(&oldsidtab);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001870 kfree(oldmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 avc_ss_reset(seqno);
1873 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09001874 selinux_status_update_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001875 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001876 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
1878 return 0;
1879
1880err:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001881 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 sidtab_destroy(&newsidtab);
1883 policydb_destroy(&newpolicydb);
1884 return rc;
1885
1886}
1887
1888/**
1889 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 * @protocol: protocol number
1891 * @port: port number
1892 * @out_sid: security identifier
1893 */
Paul Moore3e112172008-04-10 10:48:14 -04001894int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 struct ocontext *c;
1897 int rc = 0;
1898
James Morris0804d112008-06-06 18:40:29 +10001899 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
1901 c = policydb.ocontexts[OCON_PORT];
1902 while (c) {
1903 if (c->u.port.protocol == protocol &&
1904 c->u.port.low_port <= port &&
1905 c->u.port.high_port >= port)
1906 break;
1907 c = c->next;
1908 }
1909
1910 if (c) {
1911 if (!c->sid[0]) {
1912 rc = sidtab_context_to_sid(&sidtab,
1913 &c->context[0],
1914 &c->sid[0]);
1915 if (rc)
1916 goto out;
1917 }
1918 *out_sid = c->sid[0];
1919 } else {
1920 *out_sid = SECINITSID_PORT;
1921 }
1922
1923out:
James Morris0804d112008-06-06 18:40:29 +10001924 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return rc;
1926}
1927
1928/**
1929 * security_netif_sid - Obtain the SID for a network interface.
1930 * @name: interface name
1931 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001933int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 int rc = 0;
1936 struct ocontext *c;
1937
James Morris0804d112008-06-06 18:40:29 +10001938 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 c = policydb.ocontexts[OCON_NETIF];
1941 while (c) {
1942 if (strcmp(name, c->u.name) == 0)
1943 break;
1944 c = c->next;
1945 }
1946
1947 if (c) {
1948 if (!c->sid[0] || !c->sid[1]) {
1949 rc = sidtab_context_to_sid(&sidtab,
1950 &c->context[0],
1951 &c->sid[0]);
1952 if (rc)
1953 goto out;
1954 rc = sidtab_context_to_sid(&sidtab,
1955 &c->context[1],
1956 &c->sid[1]);
1957 if (rc)
1958 goto out;
1959 }
1960 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001961 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964out:
James Morris0804d112008-06-06 18:40:29 +10001965 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return rc;
1967}
1968
1969static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1970{
1971 int i, fail = 0;
1972
Eric Paris5d55a342008-04-18 17:38:33 -04001973 for (i = 0; i < 4; i++)
1974 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 fail = 1;
1976 break;
1977 }
1978
1979 return !fail;
1980}
1981
1982/**
1983 * security_node_sid - Obtain the SID for a node (host).
1984 * @domain: communication domain aka address family
1985 * @addrp: address
1986 * @addrlen: address length in bytes
1987 * @out_sid: security identifier
1988 */
1989int security_node_sid(u16 domain,
1990 void *addrp,
1991 u32 addrlen,
1992 u32 *out_sid)
1993{
1994 int rc = 0;
1995 struct ocontext *c;
1996
James Morris0804d112008-06-06 18:40:29 +10001997 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
1999 switch (domain) {
2000 case AF_INET: {
2001 u32 addr;
2002
2003 if (addrlen != sizeof(u32)) {
2004 rc = -EINVAL;
2005 goto out;
2006 }
2007
2008 addr = *((u32 *)addrp);
2009
2010 c = policydb.ocontexts[OCON_NODE];
2011 while (c) {
2012 if (c->u.node.addr == (addr & c->u.node.mask))
2013 break;
2014 c = c->next;
2015 }
2016 break;
2017 }
2018
2019 case AF_INET6:
2020 if (addrlen != sizeof(u64) * 2) {
2021 rc = -EINVAL;
2022 goto out;
2023 }
2024 c = policydb.ocontexts[OCON_NODE6];
2025 while (c) {
2026 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2027 c->u.node6.mask))
2028 break;
2029 c = c->next;
2030 }
2031 break;
2032
2033 default:
2034 *out_sid = SECINITSID_NODE;
2035 goto out;
2036 }
2037
2038 if (c) {
2039 if (!c->sid[0]) {
2040 rc = sidtab_context_to_sid(&sidtab,
2041 &c->context[0],
2042 &c->sid[0]);
2043 if (rc)
2044 goto out;
2045 }
2046 *out_sid = c->sid[0];
2047 } else {
2048 *out_sid = SECINITSID_NODE;
2049 }
2050
2051out:
James Morris0804d112008-06-06 18:40:29 +10002052 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return rc;
2054}
2055
2056#define SIDS_NEL 25
2057
2058/**
2059 * security_get_user_sids - Obtain reachable SIDs for a user.
2060 * @fromsid: starting SID
2061 * @username: username
2062 * @sids: array of reachable SIDs for user
2063 * @nel: number of elements in @sids
2064 *
2065 * Generate the set of SIDs for legal security contexts
2066 * for a given user that can be reached by @fromsid.
2067 * Set *@sids to point to a dynamically allocated
2068 * array containing the set of SIDs. Set *@nel to the
2069 * number of elements in the array.
2070 */
2071
2072int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04002073 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 u32 **sids,
2075 u32 *nel)
2076{
2077 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002078 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 u32 mynel = 0, maxnel = SIDS_NEL;
2080 struct user_datum *user;
2081 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002082 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 int rc = 0, i, j;
2084
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002085 *sids = NULL;
2086 *nel = 0;
2087
2088 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
James Morris0804d112008-06-06 18:40:29 +10002091 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Stephen Smalley12b29f32008-05-07 13:03:20 -04002093 context_init(&usercon);
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 fromcon = sidtab_search(&sidtab, fromsid);
2096 if (!fromcon) {
2097 rc = -EINVAL;
2098 goto out_unlock;
2099 }
2100
2101 user = hashtab_search(policydb.p_users.table, username);
2102 if (!user) {
2103 rc = -EINVAL;
2104 goto out_unlock;
2105 }
2106 usercon.user = user->value;
2107
James Morris89d155e2005-10-30 14:59:21 -08002108 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (!mysids) {
2110 rc = -ENOMEM;
2111 goto out_unlock;
2112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002114 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 role = policydb.role_val_to_struct[i];
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08002116 usercon.role = i + 1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002117 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08002118 usercon.type = j + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 if (mls_setup_user_range(fromcon, user, &usercon))
2121 continue;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002124 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (mynel < maxnel) {
2127 mysids[mynel++] = sid;
2128 } else {
2129 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08002130 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (!mysids2) {
2132 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 goto out_unlock;
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2136 kfree(mysids);
2137 mysids = mysids2;
2138 mysids[mynel++] = sid;
2139 }
2140 }
2141 }
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002144 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002145 if (rc || !mynel) {
2146 kfree(mysids);
2147 goto out;
2148 }
2149
2150 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2151 if (!mysids2) {
2152 rc = -ENOMEM;
2153 kfree(mysids);
2154 goto out;
2155 }
2156 for (i = 0, j = 0; i < mynel; i++) {
2157 rc = avc_has_perm_noaudit(fromsid, mysids[i],
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002158 SECCLASS_PROCESS, /* kernel value */
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002159 PROCESS__TRANSITION, AVC_STRICT,
2160 NULL);
2161 if (!rc)
2162 mysids2[j++] = mysids[i];
2163 cond_resched();
2164 }
2165 rc = 0;
2166 kfree(mysids);
2167 *sids = mysids2;
2168 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169out:
2170 return rc;
2171}
2172
2173/**
2174 * security_genfs_sid - Obtain a SID for a file in a filesystem
2175 * @fstype: filesystem type
2176 * @path: path from root of mount
2177 * @sclass: file security class
2178 * @sid: SID for path
2179 *
2180 * Obtain a SID to use for a file in a filesystem that
2181 * cannot support xattr or use a fixed labeling behavior like
2182 * transition SIDs or task SIDs.
2183 */
2184int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04002185 char *path,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002186 u16 orig_sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 u32 *sid)
2188{
2189 int len;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002190 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 struct genfs *genfs;
2192 struct ocontext *c;
2193 int rc = 0, cmp = 0;
2194
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05002195 while (path[0] == '/' && path[1] == '/')
2196 path++;
2197
James Morris0804d112008-06-06 18:40:29 +10002198 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002200 sclass = unmap_class(orig_sclass);
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2203 cmp = strcmp(fstype, genfs->fstype);
2204 if (cmp <= 0)
2205 break;
2206 }
2207
2208 if (!genfs || cmp) {
2209 *sid = SECINITSID_UNLABELED;
2210 rc = -ENOENT;
2211 goto out;
2212 }
2213
2214 for (c = genfs->head; c; c = c->next) {
2215 len = strlen(c->u.name);
2216 if ((!c->v.sclass || sclass == c->v.sclass) &&
2217 (strncmp(c->u.name, path, len) == 0))
2218 break;
2219 }
2220
2221 if (!c) {
2222 *sid = SECINITSID_UNLABELED;
2223 rc = -ENOENT;
2224 goto out;
2225 }
2226
2227 if (!c->sid[0]) {
2228 rc = sidtab_context_to_sid(&sidtab,
2229 &c->context[0],
2230 &c->sid[0]);
2231 if (rc)
2232 goto out;
2233 }
2234
2235 *sid = c->sid[0];
2236out:
James Morris0804d112008-06-06 18:40:29 +10002237 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return rc;
2239}
2240
2241/**
2242 * security_fs_use - Determine how to handle labeling for a filesystem.
2243 * @fstype: filesystem type
2244 * @behavior: labeling behavior
2245 * @sid: SID for filesystem (superblock)
2246 */
2247int security_fs_use(
2248 const char *fstype,
2249 unsigned int *behavior,
James Morris089be432008-07-15 18:32:49 +10002250 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 int rc = 0;
2253 struct ocontext *c;
2254
James Morris0804d112008-06-06 18:40:29 +10002255 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
2257 c = policydb.ocontexts[OCON_FSUSE];
2258 while (c) {
2259 if (strcmp(fstype, c->u.name) == 0)
2260 break;
2261 c = c->next;
2262 }
2263
2264 if (c) {
2265 *behavior = c->v.behavior;
2266 if (!c->sid[0]) {
2267 rc = sidtab_context_to_sid(&sidtab,
2268 &c->context[0],
2269 &c->sid[0]);
2270 if (rc)
2271 goto out;
2272 }
2273 *sid = c->sid[0];
2274 } else {
James Morris089be432008-07-15 18:32:49 +10002275 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2276 if (rc) {
2277 *behavior = SECURITY_FS_USE_NONE;
2278 rc = 0;
2279 } else {
2280 *behavior = SECURITY_FS_USE_GENFS;
2281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
2283
2284out:
James Morris0804d112008-06-06 18:40:29 +10002285 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return rc;
2287}
2288
2289int security_get_bools(int *len, char ***names, int **values)
2290{
2291 int i, rc = -ENOMEM;
2292
James Morris0804d112008-06-06 18:40:29 +10002293 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 *names = NULL;
2295 *values = NULL;
2296
2297 *len = policydb.p_bools.nprim;
2298 if (!*len) {
2299 rc = 0;
2300 goto out;
2301 }
2302
Eric Paris5d55a342008-04-18 17:38:33 -04002303 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (!*names)
2305 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Jesper Juhle0795cf2006-01-09 20:54:46 -08002307 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 if (!*values)
2309 goto err;
2310
2311 for (i = 0; i < *len; i++) {
2312 size_t name_len;
2313 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2314 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002315 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (!(*names)[i])
2317 goto err;
2318 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2319 (*names)[i][name_len - 1] = 0;
2320 }
2321 rc = 0;
2322out:
James Morris0804d112008-06-06 18:40:29 +10002323 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return rc;
2325err:
2326 if (*names) {
2327 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002328 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002330 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 goto out;
2332}
2333
2334
2335int security_set_bools(int len, int *values)
2336{
2337 int i, rc = 0;
2338 int lenp, seqno = 0;
2339 struct cond_node *cur;
2340
James Morris0804d112008-06-06 18:40:29 +10002341 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 lenp = policydb.p_bools.nprim;
2344 if (len != lenp) {
2345 rc = -EFAULT;
2346 goto out;
2347 }
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002350 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2351 audit_log(current->audit_context, GFP_ATOMIC,
2352 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002353 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002354 policydb.p_bool_val_to_name[i],
2355 !!values[i],
2356 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002357 audit_get_loginuid(current),
2358 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002359 }
Eric Paris5d55a342008-04-18 17:38:33 -04002360 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002362 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002366 for (cur = policydb.cond_list; cur; cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 rc = evaluate_cond_node(&policydb, cur);
2368 if (rc)
2369 goto out;
2370 }
2371
2372 seqno = ++latest_granting;
2373
2374out:
James Morris0804d112008-06-06 18:40:29 +10002375 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if (!rc) {
2377 avc_ss_reset(seqno);
2378 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09002379 selinux_status_update_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002380 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
2382 return rc;
2383}
2384
2385int security_get_bool_value(int bool)
2386{
2387 int rc = 0;
2388 int len;
2389
James Morris0804d112008-06-06 18:40:29 +10002390 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 len = policydb.p_bools.nprim;
2393 if (bool >= len) {
2394 rc = -EFAULT;
2395 goto out;
2396 }
2397
2398 rc = policydb.bool_val_to_struct[bool]->state;
2399out:
James Morris0804d112008-06-06 18:40:29 +10002400 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 return rc;
2402}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002403
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002404static int security_preserve_bools(struct policydb *p)
2405{
2406 int rc, nbools = 0, *bvalues = NULL, i;
2407 char **bnames = NULL;
2408 struct cond_bool_datum *booldatum;
2409 struct cond_node *cur;
2410
2411 rc = security_get_bools(&nbools, &bnames, &bvalues);
2412 if (rc)
2413 goto out;
2414 for (i = 0; i < nbools; i++) {
2415 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2416 if (booldatum)
2417 booldatum->state = bvalues[i];
2418 }
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002419 for (cur = p->cond_list; cur; cur = cur->next) {
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002420 rc = evaluate_cond_node(p, cur);
2421 if (rc)
2422 goto out;
2423 }
2424
2425out:
2426 if (bnames) {
2427 for (i = 0; i < nbools; i++)
2428 kfree(bnames[i]);
2429 }
2430 kfree(bnames);
2431 kfree(bvalues);
2432 return rc;
2433}
2434
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002435/*
2436 * security_sid_mls_copy() - computes a new sid based on the given
2437 * sid and the mls portion of mls_sid.
2438 */
2439int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2440{
2441 struct context *context1;
2442 struct context *context2;
2443 struct context newcon;
2444 char *s;
2445 u32 len;
2446 int rc = 0;
2447
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002448 if (!ss_initialized || !policydb.mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002449 *new_sid = sid;
2450 goto out;
2451 }
2452
2453 context_init(&newcon);
2454
James Morris0804d112008-06-06 18:40:29 +10002455 read_lock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002456 context1 = sidtab_search(&sidtab, sid);
2457 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002458 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2459 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002460 rc = -EINVAL;
2461 goto out_unlock;
2462 }
2463
2464 context2 = sidtab_search(&sidtab, mls_sid);
2465 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002466 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2467 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002468 rc = -EINVAL;
2469 goto out_unlock;
2470 }
2471
2472 newcon.user = context1->user;
2473 newcon.role = context1->role;
2474 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002475 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002476 if (rc)
2477 goto out_unlock;
2478
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002479 /* Check the validity of the new context. */
2480 if (!policydb_context_isvalid(&policydb, &newcon)) {
2481 rc = convert_context_handle_invalid_context(&newcon);
2482 if (rc)
2483 goto bad;
2484 }
2485
2486 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2487 goto out_unlock;
2488
2489bad:
2490 if (!context_struct_to_string(&newcon, &s, &len)) {
2491 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2492 "security_sid_mls_copy: invalid context %s", s);
2493 kfree(s);
2494 }
2495
2496out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002497 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002498 context_destroy(&newcon);
2499out:
2500 return rc;
2501}
2502
Paul Moore220deb92008-01-29 08:38:23 -05002503/**
2504 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2505 * @nlbl_sid: NetLabel SID
2506 * @nlbl_type: NetLabel labeling protocol type
2507 * @xfrm_sid: XFRM SID
2508 *
2509 * Description:
2510 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2511 * resolved into a single SID it is returned via @peer_sid and the function
2512 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2513 * returns a negative value. A table summarizing the behavior is below:
2514 *
2515 * | function return | @sid
2516 * ------------------------------+-----------------+-----------------
2517 * no peer labels | 0 | SECSID_NULL
2518 * single peer label | 0 | <peer_label>
2519 * multiple, consistent labels | 0 | <peer_label>
2520 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2521 *
2522 */
2523int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2524 u32 xfrm_sid,
2525 u32 *peer_sid)
2526{
2527 int rc;
2528 struct context *nlbl_ctx;
2529 struct context *xfrm_ctx;
2530
2531 /* handle the common (which also happens to be the set of easy) cases
2532 * right away, these two if statements catch everything involving a
2533 * single or absent peer SID/label */
2534 if (xfrm_sid == SECSID_NULL) {
2535 *peer_sid = nlbl_sid;
2536 return 0;
2537 }
2538 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2539 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2540 * is present */
2541 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2542 *peer_sid = xfrm_sid;
2543 return 0;
2544 }
2545
2546 /* we don't need to check ss_initialized here since the only way both
2547 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2548 * security server was initialized and ss_initialized was true */
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002549 if (!policydb.mls_enabled) {
Paul Moore220deb92008-01-29 08:38:23 -05002550 *peer_sid = SECSID_NULL;
2551 return 0;
2552 }
2553
James Morris0804d112008-06-06 18:40:29 +10002554 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002555
2556 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2557 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002558 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2559 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002560 rc = -EINVAL;
2561 goto out_slowpath;
2562 }
2563 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2564 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002565 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2566 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002567 rc = -EINVAL;
2568 goto out_slowpath;
2569 }
2570 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2571
2572out_slowpath:
James Morris0804d112008-06-06 18:40:29 +10002573 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002574 if (rc == 0)
2575 /* at present NetLabel SIDs/labels really only carry MLS
2576 * information so if the MLS portion of the NetLabel SID
2577 * matches the MLS portion of the labeled XFRM SID/label
2578 * then pass along the XFRM SID as it is the most
2579 * expressive */
2580 *peer_sid = xfrm_sid;
2581 else
2582 *peer_sid = SECSID_NULL;
2583 return rc;
2584}
2585
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002586static int get_classes_callback(void *k, void *d, void *args)
2587{
2588 struct class_datum *datum = d;
2589 char *name = k, **classes = args;
2590 int value = datum->value - 1;
2591
2592 classes[value] = kstrdup(name, GFP_ATOMIC);
2593 if (!classes[value])
2594 return -ENOMEM;
2595
2596 return 0;
2597}
2598
2599int security_get_classes(char ***classes, int *nclasses)
2600{
2601 int rc = -ENOMEM;
2602
James Morris0804d112008-06-06 18:40:29 +10002603 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002604
2605 *nclasses = policydb.p_classes.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002606 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002607 if (!*classes)
2608 goto out;
2609
2610 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2611 *classes);
2612 if (rc < 0) {
2613 int i;
2614 for (i = 0; i < *nclasses; i++)
2615 kfree((*classes)[i]);
2616 kfree(*classes);
2617 }
2618
2619out:
James Morris0804d112008-06-06 18:40:29 +10002620 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002621 return rc;
2622}
2623
2624static int get_permissions_callback(void *k, void *d, void *args)
2625{
2626 struct perm_datum *datum = d;
2627 char *name = k, **perms = args;
2628 int value = datum->value - 1;
2629
2630 perms[value] = kstrdup(name, GFP_ATOMIC);
2631 if (!perms[value])
2632 return -ENOMEM;
2633
2634 return 0;
2635}
2636
2637int security_get_permissions(char *class, char ***perms, int *nperms)
2638{
2639 int rc = -ENOMEM, i;
2640 struct class_datum *match;
2641
James Morris0804d112008-06-06 18:40:29 +10002642 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002643
2644 match = hashtab_search(policydb.p_classes.table, class);
2645 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002646 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002647 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002648 rc = -EINVAL;
2649 goto out;
2650 }
2651
2652 *nperms = match->permissions.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002653 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002654 if (!*perms)
2655 goto out;
2656
2657 if (match->comdatum) {
2658 rc = hashtab_map(match->comdatum->permissions.table,
2659 get_permissions_callback, *perms);
2660 if (rc < 0)
2661 goto err;
2662 }
2663
2664 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2665 *perms);
2666 if (rc < 0)
2667 goto err;
2668
2669out:
James Morris0804d112008-06-06 18:40:29 +10002670 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002671 return rc;
2672
2673err:
James Morris0804d112008-06-06 18:40:29 +10002674 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002675 for (i = 0; i < *nperms; i++)
2676 kfree((*perms)[i]);
2677 kfree(*perms);
2678 return rc;
2679}
2680
Eric Paris3f120702007-09-21 14:37:10 -04002681int security_get_reject_unknown(void)
2682{
2683 return policydb.reject_unknown;
2684}
2685
2686int security_get_allow_unknown(void)
2687{
2688 return policydb.allow_unknown;
2689}
2690
Paul Moore3bb56b22008-01-29 08:38:19 -05002691/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002692 * security_policycap_supported - Check for a specific policy capability
2693 * @req_cap: capability
2694 *
2695 * Description:
2696 * This function queries the currently loaded policy to see if it supports the
2697 * capability specified by @req_cap. Returns true (1) if the capability is
2698 * supported, false (0) if it isn't supported.
2699 *
2700 */
2701int security_policycap_supported(unsigned int req_cap)
2702{
2703 int rc;
2704
James Morris0804d112008-06-06 18:40:29 +10002705 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002706 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10002707 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002708
2709 return rc;
2710}
2711
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002712struct selinux_audit_rule {
2713 u32 au_seqno;
2714 struct context au_ctxt;
2715};
2716
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002717void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002718{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002719 struct selinux_audit_rule *rule = vrule;
2720
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002721 if (rule) {
2722 context_destroy(&rule->au_ctxt);
2723 kfree(rule);
2724 }
2725}
2726
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002727int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002728{
2729 struct selinux_audit_rule *tmprule;
2730 struct role_datum *roledatum;
2731 struct type_datum *typedatum;
2732 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002733 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002734 int rc = 0;
2735
2736 *rule = NULL;
2737
2738 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002739 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002740
2741 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002742 case AUDIT_SUBJ_USER:
2743 case AUDIT_SUBJ_ROLE:
2744 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002745 case AUDIT_OBJ_USER:
2746 case AUDIT_OBJ_ROLE:
2747 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002748 /* only 'equals' and 'not equals' fit user, role, and type */
Al Viro5af75d82008-12-16 05:59:26 -05002749 if (op != Audit_equal && op != Audit_not_equal)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002750 return -EINVAL;
2751 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002752 case AUDIT_SUBJ_SEN:
2753 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002754 case AUDIT_OBJ_LEV_LOW:
2755 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002756 /* we do not allow a range, indicated by the presense of '-' */
2757 if (strchr(rulestr, '-'))
2758 return -EINVAL;
2759 break;
2760 default:
2761 /* only the above fields are valid */
2762 return -EINVAL;
2763 }
2764
2765 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2766 if (!tmprule)
2767 return -ENOMEM;
2768
2769 context_init(&tmprule->au_ctxt);
2770
James Morris0804d112008-06-06 18:40:29 +10002771 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002772
2773 tmprule->au_seqno = latest_granting;
2774
2775 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002776 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002777 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002778 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2779 if (!userdatum)
2780 rc = -EINVAL;
2781 else
2782 tmprule->au_ctxt.user = userdatum->value;
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 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2787 if (!roledatum)
2788 rc = -EINVAL;
2789 else
2790 tmprule->au_ctxt.role = roledatum->value;
2791 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002792 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002793 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002794 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2795 if (!typedatum)
2796 rc = -EINVAL;
2797 else
2798 tmprule->au_ctxt.type = typedatum->value;
2799 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002800 case AUDIT_SUBJ_SEN:
2801 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002802 case AUDIT_OBJ_LEV_LOW:
2803 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002804 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2805 break;
2806 }
2807
James Morris0804d112008-06-06 18:40:29 +10002808 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002809
2810 if (rc) {
2811 selinux_audit_rule_free(tmprule);
2812 tmprule = NULL;
2813 }
2814
2815 *rule = tmprule;
2816
2817 return rc;
2818}
2819
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002820/* Check to see if the rule contains any selinux fields */
2821int selinux_audit_rule_known(struct audit_krule *rule)
2822{
2823 int i;
2824
2825 for (i = 0; i < rule->field_count; i++) {
2826 struct audit_field *f = &rule->fields[i];
2827 switch (f->type) {
2828 case AUDIT_SUBJ_USER:
2829 case AUDIT_SUBJ_ROLE:
2830 case AUDIT_SUBJ_TYPE:
2831 case AUDIT_SUBJ_SEN:
2832 case AUDIT_SUBJ_CLR:
2833 case AUDIT_OBJ_USER:
2834 case AUDIT_OBJ_ROLE:
2835 case AUDIT_OBJ_TYPE:
2836 case AUDIT_OBJ_LEV_LOW:
2837 case AUDIT_OBJ_LEV_HIGH:
2838 return 1;
2839 }
2840 }
2841
2842 return 0;
2843}
2844
2845int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04002846 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002847{
2848 struct context *ctxt;
2849 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002850 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002851 int match = 0;
2852
2853 if (!rule) {
2854 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002855 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002856 return -ENOENT;
2857 }
2858
James Morris0804d112008-06-06 18:40:29 +10002859 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002860
2861 if (rule->au_seqno < latest_granting) {
2862 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002863 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002864 match = -ESTALE;
2865 goto out;
2866 }
2867
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002868 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002869 if (!ctxt) {
2870 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002871 "selinux_audit_rule_match: unrecognized SID %d\n",
2872 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002873 match = -ENOENT;
2874 goto out;
2875 }
2876
2877 /* a field/op pair that is not caught here will simply fall through
2878 without a match */
2879 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002880 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002881 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002882 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002883 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002884 match = (ctxt->user == rule->au_ctxt.user);
2885 break;
Al Viro5af75d82008-12-16 05:59:26 -05002886 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002887 match = (ctxt->user != rule->au_ctxt.user);
2888 break;
2889 }
2890 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002891 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002892 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002893 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002894 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002895 match = (ctxt->role == rule->au_ctxt.role);
2896 break;
Al Viro5af75d82008-12-16 05:59:26 -05002897 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002898 match = (ctxt->role != rule->au_ctxt.role);
2899 break;
2900 }
2901 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002902 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002903 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002904 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002905 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002906 match = (ctxt->type == rule->au_ctxt.type);
2907 break;
Al Viro5af75d82008-12-16 05:59:26 -05002908 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002909 match = (ctxt->type != rule->au_ctxt.type);
2910 break;
2911 }
2912 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002913 case AUDIT_SUBJ_SEN:
2914 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002915 case AUDIT_OBJ_LEV_LOW:
2916 case AUDIT_OBJ_LEV_HIGH:
2917 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002918 field == AUDIT_OBJ_LEV_LOW) ?
2919 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002920 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002921 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002922 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002923 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002924 break;
Al Viro5af75d82008-12-16 05:59:26 -05002925 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002926 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002927 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002928 break;
Al Viro5af75d82008-12-16 05:59:26 -05002929 case Audit_lt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002930 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002931 level) &&
2932 !mls_level_eq(&rule->au_ctxt.range.level[0],
2933 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002934 break;
Al Viro5af75d82008-12-16 05:59:26 -05002935 case Audit_le:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002936 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002937 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002938 break;
Al Viro5af75d82008-12-16 05:59:26 -05002939 case Audit_gt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002940 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002941 &rule->au_ctxt.range.level[0]) &&
2942 !mls_level_eq(level,
2943 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002944 break;
Al Viro5af75d82008-12-16 05:59:26 -05002945 case Audit_ge:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002946 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002947 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002948 break;
2949 }
2950 }
2951
2952out:
James Morris0804d112008-06-06 18:40:29 +10002953 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002954 return match;
2955}
2956
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002957static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002958
2959static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
Eric Parisf5269712008-05-14 11:27:45 -04002960 u16 class, u32 perms, u32 *retained)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002961{
2962 int err = 0;
2963
2964 if (event == AVC_CALLBACK_RESET && aurule_callback)
2965 err = aurule_callback();
2966 return err;
2967}
2968
2969static int __init aurule_init(void)
2970{
2971 int err;
2972
2973 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002974 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002975 if (err)
2976 panic("avc_add_callback() failed, error %d\n", err);
2977
2978 return err;
2979}
2980__initcall(aurule_init);
2981
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002982#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002983/**
Paul Moore5778eab2007-02-28 15:14:22 -05002984 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2985 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002986 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002987 *
2988 * Description:
2989 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002990 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2991 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002992 *
2993 */
Paul Moore5778eab2007-02-28 15:14:22 -05002994static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002995 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002996{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002997 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002998
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002999 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
3000 if (sid_cache == NULL)
3001 return;
Paul Moore5778eab2007-02-28 15:14:22 -05003002 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003003 if (secattr->cache == NULL) {
3004 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05003005 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02003006 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003007
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003008 *sid_cache = sid;
3009 secattr->cache->free = kfree;
3010 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05003011 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003012}
3013
3014/**
Paul Moore5778eab2007-02-28 15:14:22 -05003015 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003016 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003017 * @sid: the SELinux SID
3018 *
3019 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05003020 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003021 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003022 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
3023 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
3024 * allow the @secattr to be used by NetLabel to cache the secattr to SID
3025 * conversion for future lookups. Returns zero on success, negative values on
3026 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003027 *
3028 */
Paul Moore5778eab2007-02-28 15:14:22 -05003029int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05003030 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003031{
3032 int rc = -EIDRM;
3033 struct context *ctx;
3034 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05003035
3036 if (!ss_initialized) {
3037 *sid = SECSID_NULL;
3038 return 0;
3039 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003040
James Morris0804d112008-06-06 18:40:29 +10003041 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003042
Paul Moore701a90b2006-11-17 17:38:46 -05003043 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003044 *sid = *(u32 *)secattr->cache->data;
3045 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05003046 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
3047 *sid = secattr->attr.secid;
3048 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05003049 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003050 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003051 if (ctx == NULL)
3052 goto netlbl_secattr_to_sid_return;
3053
Paul Moore81990fb2008-10-03 10:51:15 -04003054 context_init(&ctx_new);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003055 ctx_new.user = ctx->user;
3056 ctx_new.role = ctx->role;
3057 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05003058 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05003059 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05003060 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05003061 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003062 goto netlbl_secattr_to_sid_return;
Paul Moore81990fb2008-10-03 10:51:15 -04003063 memcpy(&ctx_new.range.level[1].cat,
3064 &ctx_new.range.level[0].cat,
3065 sizeof(ctx_new.range.level[0].cat));
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003066 }
3067 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
3068 goto netlbl_secattr_to_sid_return_cleanup;
3069
3070 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
3071 if (rc != 0)
3072 goto netlbl_secattr_to_sid_return_cleanup;
3073
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003074 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05003075
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003076 ebitmap_destroy(&ctx_new.range.level[0].cat);
3077 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04003078 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003079 rc = 0;
3080 }
3081
3082netlbl_secattr_to_sid_return:
James Morris0804d112008-06-06 18:40:29 +10003083 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003084 return rc;
3085netlbl_secattr_to_sid_return_cleanup:
3086 ebitmap_destroy(&ctx_new.range.level[0].cat);
3087 goto netlbl_secattr_to_sid_return;
3088}
3089
3090/**
Paul Moore5778eab2007-02-28 15:14:22 -05003091 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3092 * @sid: the SELinux SID
3093 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003094 *
3095 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05003096 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3097 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003098 *
3099 */
Paul Moore5778eab2007-02-28 15:14:22 -05003100int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003101{
Paul Moore99d854d2008-10-10 10:16:30 -04003102 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003103 struct context *ctx;
3104
3105 if (!ss_initialized)
3106 return 0;
3107
James Morris0804d112008-06-06 18:40:29 +10003108 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003109 ctx = sidtab_search(&sidtab, sid);
Paul Moore99d854d2008-10-10 10:16:30 -04003110 if (ctx == NULL) {
3111 rc = -ENOENT;
Paul Moore5778eab2007-02-28 15:14:22 -05003112 goto netlbl_sid_to_secattr_failure;
Paul Moore99d854d2008-10-10 10:16:30 -04003113 }
Paul Moore5778eab2007-02-28 15:14:22 -05003114 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
3115 GFP_ATOMIC);
Paul Moore99d854d2008-10-10 10:16:30 -04003116 if (secattr->domain == NULL) {
3117 rc = -ENOMEM;
3118 goto netlbl_sid_to_secattr_failure;
3119 }
Paul Moore8d758992008-10-10 10:16:33 -04003120 secattr->attr.secid = sid;
3121 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
Paul Moore5778eab2007-02-28 15:14:22 -05003122 mls_export_netlbl_lvl(ctx, secattr);
3123 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04003124 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05003125 goto netlbl_sid_to_secattr_failure;
James Morris0804d112008-06-06 18:40:29 +10003126 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003127
Paul Moore5778eab2007-02-28 15:14:22 -05003128 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05003129
Paul Moore5778eab2007-02-28 15:14:22 -05003130netlbl_sid_to_secattr_failure:
James Morris0804d112008-06-06 18:40:29 +10003131 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08003132 return rc;
3133}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003134#endif /* CONFIG_NETLABEL */