blob: 0e5c3a422a8ece0adf4235db9487b5136978d86f [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 Molnarbb003072006-03-22 00:09:14 -080051#include <linux/mutex.h>
Adrian Bunk0e55a002008-03-31 01:54:02 +030052#include <linux/selinux.h>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070053#include <net/netlabel.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include "flask.h"
56#include "avc.h"
57#include "avc_ss.h"
58#include "security.h"
59#include "context.h"
60#include "policydb.h"
61#include "sidtab.h"
62#include "services.h"
63#include "conditional.h"
64#include "mls.h"
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070065#include "objsec.h"
Paul Moorec60475b2007-02-28 15:14:23 -050066#include "netlabel.h"
Paul Moore3de4bab2006-11-17 17:38:54 -050067#include "xfrm.h"
Paul Moore02752762006-11-29 13:18:18 -050068#include "ebitmap.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020069#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71extern void selnl_notify_policyload(u32 seqno);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Paul Moore3bb56b22008-01-29 08:38:19 -050073int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050074int selinux_policycap_openperm;
Paul Moore3bb56b22008-01-29 08:38:19 -050075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static DEFINE_RWLOCK(policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static struct sidtab sidtab;
79struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040080int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * The largest sequence number that has been used when
84 * providing an access decision to the access vector cache.
85 * The sequence number only changes when a policy change
86 * occurs.
87 */
Eric Paris5d55a342008-04-18 17:38:33 -040088static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90/* Forward declaration. */
91static int context_struct_to_string(struct context *context, char **scontext,
92 u32 *scontext_len);
93
Stephen Smalley19439d02010-01-14 17:28:10 -050094static void context_struct_compute_av(struct context *scontext,
95 struct context *tcontext,
96 u16 tclass,
97 struct av_decision *avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -040098
99struct selinux_mapping {
100 u16 value; /* policy value */
101 unsigned num_perms;
102 u32 perms[sizeof(u32) * 8];
103};
104
105static struct selinux_mapping *current_mapping;
106static u16 current_mapping_size;
107
108static int selinux_set_mapping(struct policydb *pol,
109 struct security_class_mapping *map,
110 struct selinux_mapping **out_map_p,
111 u16 *out_map_size)
112{
113 struct selinux_mapping *out_map = NULL;
114 size_t size = sizeof(struct selinux_mapping);
115 u16 i, j;
116 unsigned k;
117 bool print_unknown_handle = false;
118
119 /* Find number of classes in the input mapping */
120 if (!map)
121 return -EINVAL;
122 i = 0;
123 while (map[i].name)
124 i++;
125
126 /* Allocate space for the class records, plus one for class zero */
127 out_map = kcalloc(++i, size, GFP_ATOMIC);
128 if (!out_map)
129 return -ENOMEM;
130
131 /* Store the raw class and permission values */
132 j = 0;
133 while (map[j].name) {
134 struct security_class_mapping *p_in = map + (j++);
135 struct selinux_mapping *p_out = out_map + j;
136
137 /* An empty class string skips ahead */
138 if (!strcmp(p_in->name, "")) {
139 p_out->num_perms = 0;
140 continue;
141 }
142
143 p_out->value = string_to_security_class(pol, p_in->name);
144 if (!p_out->value) {
145 printk(KERN_INFO
146 "SELinux: Class %s not defined in policy.\n",
147 p_in->name);
148 if (pol->reject_unknown)
149 goto err;
150 p_out->num_perms = 0;
151 print_unknown_handle = true;
152 continue;
153 }
154
155 k = 0;
156 while (p_in->perms && p_in->perms[k]) {
157 /* An empty permission string skips ahead */
158 if (!*p_in->perms[k]) {
159 k++;
160 continue;
161 }
162 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
163 p_in->perms[k]);
164 if (!p_out->perms[k]) {
165 printk(KERN_INFO
166 "SELinux: Permission %s in class %s not defined in policy.\n",
167 p_in->perms[k], p_in->name);
168 if (pol->reject_unknown)
169 goto err;
170 print_unknown_handle = true;
171 }
172
173 k++;
174 }
175 p_out->num_perms = k;
176 }
177
178 if (print_unknown_handle)
179 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
180 pol->allow_unknown ? "allowed" : "denied");
181
182 *out_map_p = out_map;
183 *out_map_size = i;
184 return 0;
185err:
186 kfree(out_map);
187 return -EINVAL;
188}
189
190/*
191 * Get real, policy values from mapped values
192 */
193
194static u16 unmap_class(u16 tclass)
195{
196 if (tclass < current_mapping_size)
197 return current_mapping[tclass].value;
198
199 return tclass;
200}
201
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400202static void map_decision(u16 tclass, struct av_decision *avd,
203 int allow_unknown)
204{
205 if (tclass < current_mapping_size) {
206 unsigned i, n = current_mapping[tclass].num_perms;
207 u32 result;
208
209 for (i = 0, result = 0; i < n; i++) {
210 if (avd->allowed & current_mapping[tclass].perms[i])
211 result |= 1<<i;
212 if (allow_unknown && !current_mapping[tclass].perms[i])
213 result |= 1<<i;
214 }
215 avd->allowed = result;
216
217 for (i = 0, result = 0; i < n; i++)
218 if (avd->auditallow & current_mapping[tclass].perms[i])
219 result |= 1<<i;
220 avd->auditallow = result;
221
222 for (i = 0, result = 0; i < n; i++) {
223 if (avd->auditdeny & current_mapping[tclass].perms[i])
224 result |= 1<<i;
225 if (!allow_unknown && !current_mapping[tclass].perms[i])
226 result |= 1<<i;
227 }
Eric Paris0bce9522009-11-23 16:47:23 -0500228 /*
229 * In case the kernel has a bug and requests a permission
230 * between num_perms and the maximum permission number, we
231 * should audit that denial
232 */
233 for (; i < (sizeof(u32)*8); i++)
234 result |= 1<<i;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400235 avd->auditdeny = result;
236 }
237}
238
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100239int security_mls_enabled(void)
240{
241 return policydb.mls_enabled;
242}
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244/*
245 * Return the boolean value of a constraint expression
246 * when it is applied to the specified source and target
247 * security contexts.
248 *
249 * xcontext is a special beast... It is used by the validatetrans rules
250 * only. For these rules, scontext is the context before the transition,
251 * tcontext is the context after the transition, and xcontext is the context
252 * of the process performing the transition. All other callers of
253 * constraint_expr_eval should pass in NULL for xcontext.
254 */
255static int constraint_expr_eval(struct context *scontext,
256 struct context *tcontext,
257 struct context *xcontext,
258 struct constraint_expr *cexpr)
259{
260 u32 val1, val2;
261 struct context *c;
262 struct role_datum *r1, *r2;
263 struct mls_level *l1, *l2;
264 struct constraint_expr *e;
265 int s[CEXPR_MAXDEPTH];
266 int sp = -1;
267
268 for (e = cexpr; e; e = e->next) {
269 switch (e->expr_type) {
270 case CEXPR_NOT:
271 BUG_ON(sp < 0);
272 s[sp] = !s[sp];
273 break;
274 case CEXPR_AND:
275 BUG_ON(sp < 1);
276 sp--;
277 s[sp] &= s[sp+1];
278 break;
279 case CEXPR_OR:
280 BUG_ON(sp < 1);
281 sp--;
282 s[sp] |= s[sp+1];
283 break;
284 case CEXPR_ATTR:
285 if (sp == (CEXPR_MAXDEPTH-1))
286 return 0;
287 switch (e->attr) {
288 case CEXPR_USER:
289 val1 = scontext->user;
290 val2 = tcontext->user;
291 break;
292 case CEXPR_TYPE:
293 val1 = scontext->type;
294 val2 = tcontext->type;
295 break;
296 case CEXPR_ROLE:
297 val1 = scontext->role;
298 val2 = tcontext->role;
299 r1 = policydb.role_val_to_struct[val1 - 1];
300 r2 = policydb.role_val_to_struct[val2 - 1];
301 switch (e->op) {
302 case CEXPR_DOM:
303 s[++sp] = ebitmap_get_bit(&r1->dominates,
304 val2 - 1);
305 continue;
306 case CEXPR_DOMBY:
307 s[++sp] = ebitmap_get_bit(&r2->dominates,
308 val1 - 1);
309 continue;
310 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400311 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
312 val2 - 1) &&
313 !ebitmap_get_bit(&r2->dominates,
314 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 continue;
316 default:
317 break;
318 }
319 break;
320 case CEXPR_L1L2:
321 l1 = &(scontext->range.level[0]);
322 l2 = &(tcontext->range.level[0]);
323 goto mls_ops;
324 case CEXPR_L1H2:
325 l1 = &(scontext->range.level[0]);
326 l2 = &(tcontext->range.level[1]);
327 goto mls_ops;
328 case CEXPR_H1L2:
329 l1 = &(scontext->range.level[1]);
330 l2 = &(tcontext->range.level[0]);
331 goto mls_ops;
332 case CEXPR_H1H2:
333 l1 = &(scontext->range.level[1]);
334 l2 = &(tcontext->range.level[1]);
335 goto mls_ops;
336 case CEXPR_L1H1:
337 l1 = &(scontext->range.level[0]);
338 l2 = &(scontext->range.level[1]);
339 goto mls_ops;
340 case CEXPR_L2H2:
341 l1 = &(tcontext->range.level[0]);
342 l2 = &(tcontext->range.level[1]);
343 goto mls_ops;
344mls_ops:
345 switch (e->op) {
346 case CEXPR_EQ:
347 s[++sp] = mls_level_eq(l1, l2);
348 continue;
349 case CEXPR_NEQ:
350 s[++sp] = !mls_level_eq(l1, l2);
351 continue;
352 case CEXPR_DOM:
353 s[++sp] = mls_level_dom(l1, l2);
354 continue;
355 case CEXPR_DOMBY:
356 s[++sp] = mls_level_dom(l2, l1);
357 continue;
358 case CEXPR_INCOMP:
359 s[++sp] = mls_level_incomp(l2, l1);
360 continue;
361 default:
362 BUG();
363 return 0;
364 }
365 break;
366 default:
367 BUG();
368 return 0;
369 }
370
371 switch (e->op) {
372 case CEXPR_EQ:
373 s[++sp] = (val1 == val2);
374 break;
375 case CEXPR_NEQ:
376 s[++sp] = (val1 != val2);
377 break;
378 default:
379 BUG();
380 return 0;
381 }
382 break;
383 case CEXPR_NAMES:
384 if (sp == (CEXPR_MAXDEPTH-1))
385 return 0;
386 c = scontext;
387 if (e->attr & CEXPR_TARGET)
388 c = tcontext;
389 else if (e->attr & CEXPR_XTARGET) {
390 c = xcontext;
391 if (!c) {
392 BUG();
393 return 0;
394 }
395 }
396 if (e->attr & CEXPR_USER)
397 val1 = c->user;
398 else if (e->attr & CEXPR_ROLE)
399 val1 = c->role;
400 else if (e->attr & CEXPR_TYPE)
401 val1 = c->type;
402 else {
403 BUG();
404 return 0;
405 }
406
407 switch (e->op) {
408 case CEXPR_EQ:
409 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
410 break;
411 case CEXPR_NEQ:
412 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
413 break;
414 default:
415 BUG();
416 return 0;
417 }
418 break;
419 default:
420 BUG();
421 return 0;
422 }
423 }
424
425 BUG_ON(sp != 0);
426 return s[0];
427}
428
429/*
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900430 * security_dump_masked_av - dumps masked permissions during
431 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
432 */
433static int dump_masked_av_helper(void *k, void *d, void *args)
434{
435 struct perm_datum *pdatum = d;
436 char **permission_names = args;
437
438 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
439
440 permission_names[pdatum->value - 1] = (char *)k;
441
442 return 0;
443}
444
445static void security_dump_masked_av(struct context *scontext,
446 struct context *tcontext,
447 u16 tclass,
448 u32 permissions,
449 const char *reason)
450{
451 struct common_datum *common_dat;
452 struct class_datum *tclass_dat;
453 struct audit_buffer *ab;
454 char *tclass_name;
455 char *scontext_name = NULL;
456 char *tcontext_name = NULL;
457 char *permission_names[32];
James Morris2da5d312010-02-16 17:29:06 +1100458 int index;
459 u32 length;
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900460 bool need_comma = false;
461
462 if (!permissions)
463 return;
464
465 tclass_name = policydb.p_class_val_to_name[tclass - 1];
466 tclass_dat = policydb.class_val_to_struct[tclass - 1];
467 common_dat = tclass_dat->comdatum;
468
469 /* init permission_names */
470 if (common_dat &&
471 hashtab_map(common_dat->permissions.table,
472 dump_masked_av_helper, permission_names) < 0)
473 goto out;
474
475 if (hashtab_map(tclass_dat->permissions.table,
476 dump_masked_av_helper, permission_names) < 0)
477 goto out;
478
479 /* get scontext/tcontext in text form */
480 if (context_struct_to_string(scontext,
481 &scontext_name, &length) < 0)
482 goto out;
483
484 if (context_struct_to_string(tcontext,
485 &tcontext_name, &length) < 0)
486 goto out;
487
488 /* audit a message */
489 ab = audit_log_start(current->audit_context,
490 GFP_ATOMIC, AUDIT_SELINUX_ERR);
491 if (!ab)
492 goto out;
493
494 audit_log_format(ab, "op=security_compute_av reason=%s "
495 "scontext=%s tcontext=%s tclass=%s perms=",
496 reason, scontext_name, tcontext_name, tclass_name);
497
498 for (index = 0; index < 32; index++) {
499 u32 mask = (1 << index);
500
501 if ((mask & permissions) == 0)
502 continue;
503
504 audit_log_format(ab, "%s%s",
505 need_comma ? "," : "",
506 permission_names[index]
507 ? permission_names[index] : "????");
508 need_comma = true;
509 }
510 audit_log_end(ab);
511out:
512 /* release scontext/tcontext */
513 kfree(tcontext_name);
514 kfree(scontext_name);
515
516 return;
517}
518
519/*
KaiGai Koheid9250de2008-08-28 16:35:57 +0900520 * security_boundary_permission - drops violated permissions
521 * on boundary constraint.
522 */
523static void type_attribute_bounds_av(struct context *scontext,
524 struct context *tcontext,
525 u16 tclass,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900526 struct av_decision *avd)
527{
KaiGai Koheid9250de2008-08-28 16:35:57 +0900528 struct type_datum *source
529 = policydb.type_val_to_struct[scontext->type - 1];
KaiGai Koheid9250de2008-08-28 16:35:57 +0900530
531 if (source->bounds) {
KaiGai Kohei7d52a152010-01-21 15:00:15 +0900532 struct context lo_scontext;
533 struct av_decision lo_avd;
534 u32 masked;
535
KaiGai Koheid9250de2008-08-28 16:35:57 +0900536 memset(&lo_avd, 0, sizeof(lo_avd));
537
538 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
539 lo_scontext.type = source->bounds;
540
541 context_struct_compute_av(&lo_scontext,
542 tcontext,
543 tclass,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900544 &lo_avd);
545 if ((lo_avd.allowed & avd->allowed) == avd->allowed)
546 return; /* no masked permission */
547 masked = ~lo_avd.allowed & avd->allowed;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900548
KaiGai Koheid9250de2008-08-28 16:35:57 +0900549 /* mask violated permissions */
550 avd->allowed &= ~masked;
551
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900552 /* audit masked permissions */
553 security_dump_masked_av(scontext, tcontext,
554 tclass, masked, "bounds");
KaiGai Koheid9250de2008-08-28 16:35:57 +0900555 }
556}
557
558/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 * Compute access vectors based on a context structure pair for
560 * the permissions in a particular class.
561 */
Stephen Smalley19439d02010-01-14 17:28:10 -0500562static void context_struct_compute_av(struct context *scontext,
563 struct context *tcontext,
564 u16 tclass,
565 struct av_decision *avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct constraint_node *constraint;
568 struct role_allow *ra;
569 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700570 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700572 struct ebitmap *sattr, *tattr;
573 struct ebitmap_node *snode, *tnode;
574 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 avd->allowed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 avd->auditallow = 0;
578 avd->auditdeny = 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400580 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
581 if (printk_ratelimit())
582 printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
Stephen Smalley19439d02010-01-14 17:28:10 -0500583 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400584 }
Eric Paris3f120702007-09-21 14:37:10 -0400585
586 tclass_datum = policydb.class_val_to_struct[tclass - 1];
587
588 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 * If a specific type enforcement rule was defined for
590 * this permission check, then use it.
591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700593 avkey.specified = AVTAB_AV;
594 sattr = &policydb.type_attr_map[scontext->type - 1];
595 tattr = &policydb.type_attr_map[tcontext->type - 1];
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900596 ebitmap_for_each_positive_bit(sattr, snode, i) {
597 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700598 avkey.source_type = i + 1;
599 avkey.target_type = j + 1;
600 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300601 node;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700602 node = avtab_search_node_next(node, avkey.specified)) {
603 if (node->key.specified == AVTAB_ALLOWED)
604 avd->allowed |= node->datum.data;
605 else if (node->key.specified == AVTAB_AUDITALLOW)
606 avd->auditallow |= node->datum.data;
607 else if (node->key.specified == AVTAB_AUDITDENY)
608 avd->auditdeny &= node->datum.data;
609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Stephen Smalley782ebb92005-09-03 15:55:16 -0700611 /* Check conditional av table for additional permissions */
612 cond_compute_av(&policydb.te_cond_avtab, &avkey, avd);
613
614 }
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 /*
618 * Remove any permissions prohibited by a constraint (this includes
619 * the MLS policy).
620 */
621 constraint = tclass_datum->constraints;
622 while (constraint) {
623 if ((constraint->permissions & (avd->allowed)) &&
624 !constraint_expr_eval(scontext, tcontext, NULL,
625 constraint->expr)) {
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900626 avd->allowed &= ~(constraint->permissions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628 constraint = constraint->next;
629 }
630
631 /*
632 * If checking process transition permission and the
633 * role is changing, then check the (current_role, new_role)
634 * pair.
635 */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400636 if (tclass == policydb.process_class &&
637 (avd->allowed & policydb.process_trans_perms) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 scontext->role != tcontext->role) {
639 for (ra = policydb.role_allow; ra; ra = ra->next) {
640 if (scontext->role == ra->role &&
641 tcontext->role == ra->new_role)
642 break;
643 }
644 if (!ra)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400645 avd->allowed &= ~policydb.process_trans_perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
KaiGai Koheid9250de2008-08-28 16:35:57 +0900648 /*
649 * If the given source and target types have boundary
650 * constraint, lazy checks have to mask any violated
651 * permission and notice it to userspace via audit.
652 */
653 type_attribute_bounds_av(scontext, tcontext,
Stephen Smalley19439d02010-01-14 17:28:10 -0500654 tclass, avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400658 struct context *ncontext,
659 struct context *tcontext,
660 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
662 char *o = NULL, *n = NULL, *t = NULL;
663 u32 olen, nlen, tlen;
664
665 if (context_struct_to_string(ocontext, &o, &olen) < 0)
666 goto out;
667 if (context_struct_to_string(ncontext, &n, &nlen) < 0)
668 goto out;
669 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
670 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100671 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -0400672 "security_validate_transition: denied for"
673 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
674 o, n, t, policydb.p_class_val_to_name[tclass-1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675out:
676 kfree(o);
677 kfree(n);
678 kfree(t);
679
680 if (!selinux_enforcing)
681 return 0;
682 return -EPERM;
683}
684
685int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400686 u16 orig_tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
688 struct context *ocontext;
689 struct context *ncontext;
690 struct context *tcontext;
691 struct class_datum *tclass_datum;
692 struct constraint_node *constraint;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400693 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 int rc = 0;
695
696 if (!ss_initialized)
697 return 0;
698
James Morris0804d112008-06-06 18:40:29 +1000699 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400701 tclass = unmap_class(orig_tclass);
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (!tclass || tclass > policydb.p_classes.nprim) {
Eric Paris744ba352008-04-17 11:52:44 -0400704 printk(KERN_ERR "SELinux: %s: unrecognized class %d\n",
705 __func__, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 rc = -EINVAL;
707 goto out;
708 }
709 tclass_datum = policydb.class_val_to_struct[tclass - 1];
710
711 ocontext = sidtab_search(&sidtab, oldsid);
712 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400713 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
714 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 rc = -EINVAL;
716 goto out;
717 }
718
719 ncontext = sidtab_search(&sidtab, newsid);
720 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400721 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
722 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 rc = -EINVAL;
724 goto out;
725 }
726
727 tcontext = sidtab_search(&sidtab, tasksid);
728 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400729 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
730 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 rc = -EINVAL;
732 goto out;
733 }
734
735 constraint = tclass_datum->validatetrans;
736 while (constraint) {
737 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400738 constraint->expr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 rc = security_validtrans_handle_fail(ocontext, ncontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400740 tcontext, tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 goto out;
742 }
743 constraint = constraint->next;
744 }
745
746out:
James Morris0804d112008-06-06 18:40:29 +1000747 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return rc;
749}
750
KaiGai Koheid9250de2008-08-28 16:35:57 +0900751/*
752 * security_bounded_transition - check whether the given
753 * transition is directed to bounded, or not.
754 * It returns 0, if @newsid is bounded by @oldsid.
755 * Otherwise, it returns error code.
756 *
757 * @oldsid : current security identifier
758 * @newsid : destinated security identifier
759 */
760int security_bounded_transition(u32 old_sid, u32 new_sid)
761{
762 struct context *old_context, *new_context;
763 struct type_datum *type;
764 int index;
765 int rc = -EINVAL;
766
767 read_lock(&policy_rwlock);
768
769 old_context = sidtab_search(&sidtab, old_sid);
770 if (!old_context) {
771 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
772 __func__, old_sid);
773 goto out;
774 }
775
776 new_context = sidtab_search(&sidtab, new_sid);
777 if (!new_context) {
778 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
779 __func__, new_sid);
780 goto out;
781 }
782
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200783 /* type/domain unchanged */
KaiGai Koheid9250de2008-08-28 16:35:57 +0900784 if (old_context->type == new_context->type) {
785 rc = 0;
786 goto out;
787 }
788
789 index = new_context->type;
790 while (true) {
791 type = policydb.type_val_to_struct[index - 1];
792 BUG_ON(!type);
793
794 /* not bounded anymore */
795 if (!type->bounds) {
796 rc = -EPERM;
797 break;
798 }
799
800 /* @newsid is bounded by @oldsid */
801 if (type->bounds == old_context->type) {
802 rc = 0;
803 break;
804 }
805 index = type->bounds;
806 }
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900807
808 if (rc) {
809 char *old_name = NULL;
810 char *new_name = NULL;
James Morris2da5d312010-02-16 17:29:06 +1100811 u32 length;
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900812
813 if (!context_struct_to_string(old_context,
814 &old_name, &length) &&
815 !context_struct_to_string(new_context,
816 &new_name, &length)) {
817 audit_log(current->audit_context,
818 GFP_ATOMIC, AUDIT_SELINUX_ERR,
819 "op=security_bounded_transition "
820 "result=denied "
821 "oldcontext=%s newcontext=%s",
822 old_name, new_name);
823 }
824 kfree(new_name);
825 kfree(old_name);
826 }
KaiGai Koheid9250de2008-08-28 16:35:57 +0900827out:
828 read_unlock(&policy_rwlock);
829
830 return rc;
831}
832
Stephen Smalley19439d02010-01-14 17:28:10 -0500833static void avd_init(struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400834{
Stephen Smalley19439d02010-01-14 17:28:10 -0500835 avd->allowed = 0;
836 avd->auditallow = 0;
837 avd->auditdeny = 0xffffffff;
838 avd->seqno = latest_granting;
839 avd->flags = 0;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400840}
841
Stephen Smalley19439d02010-01-14 17:28:10 -0500842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/**
844 * security_compute_av - Compute access vector decisions.
845 * @ssid: source security identifier
846 * @tsid: target security identifier
847 * @tclass: target security class
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 * @avd: access vector decisions
849 *
850 * Compute a set of access vector decisions based on the
851 * SID pair (@ssid, @tsid) for the permissions in @tclass.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
Stephen Smalley19439d02010-01-14 17:28:10 -0500853void security_compute_av(u32 ssid,
854 u32 tsid,
855 u16 orig_tclass,
856 struct av_decision *avd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400858 u16 tclass;
Stephen Smalley19439d02010-01-14 17:28:10 -0500859 struct context *scontext = NULL, *tcontext = NULL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400860
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400861 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500862 avd_init(avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400863 if (!ss_initialized)
864 goto allow;
865
Stephen Smalley19439d02010-01-14 17:28:10 -0500866 scontext = sidtab_search(&sidtab, ssid);
867 if (!scontext) {
868 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
869 __func__, ssid);
870 goto out;
871 }
872
873 /* permissive domain? */
874 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
875 avd->flags |= AVD_FLAGS_PERMISSIVE;
876
877 tcontext = sidtab_search(&sidtab, tsid);
878 if (!tcontext) {
879 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
880 __func__, tsid);
881 goto out;
882 }
883
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400884 tclass = unmap_class(orig_tclass);
885 if (unlikely(orig_tclass && !tclass)) {
886 if (policydb.allow_unknown)
887 goto allow;
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400888 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400889 }
Stephen Smalley19439d02010-01-14 17:28:10 -0500890 context_struct_compute_av(scontext, tcontext, tclass, avd);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400891 map_decision(orig_tclass, avd, policydb.allow_unknown);
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400892out:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400893 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500894 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400895allow:
896 avd->allowed = 0xffffffff;
Stephen Smalleyb7f30082009-10-19 10:08:50 -0400897 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400898}
899
Stephen Smalley19439d02010-01-14 17:28:10 -0500900void security_compute_av_user(u32 ssid,
901 u32 tsid,
902 u16 tclass,
903 struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400904{
Stephen Smalley19439d02010-01-14 17:28:10 -0500905 struct context *scontext = NULL, *tcontext = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
James Morris0804d112008-06-06 18:40:29 +1000907 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500908 avd_init(avd);
909 if (!ss_initialized)
910 goto allow;
911
912 scontext = sidtab_search(&sidtab, ssid);
913 if (!scontext) {
914 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
915 __func__, ssid);
916 goto out;
917 }
918
919 /* permissive domain? */
920 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
921 avd->flags |= AVD_FLAGS_PERMISSIVE;
922
923 tcontext = sidtab_search(&sidtab, tsid);
924 if (!tcontext) {
925 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
926 __func__, tsid);
927 goto out;
928 }
929
930 if (unlikely(!tclass)) {
931 if (policydb.allow_unknown)
932 goto allow;
933 goto out;
934 }
935
936 context_struct_compute_av(scontext, tcontext, tclass, avd);
937 out:
James Morris0804d112008-06-06 18:40:29 +1000938 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -0500939 return;
940allow:
941 avd->allowed = 0xffffffff;
942 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
945/*
946 * Write the security context string representation of
947 * the context structure `context' into a dynamically
948 * allocated string of the correct size. Set `*scontext'
949 * to point to this string and set `*scontext_len' to
950 * the length of the string.
951 */
952static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
953{
954 char *scontextp;
955
956 *scontext = NULL;
957 *scontext_len = 0;
958
Stephen Smalley12b29f32008-05-07 13:03:20 -0400959 if (context->len) {
960 *scontext_len = context->len;
961 *scontext = kstrdup(context->str, GFP_ATOMIC);
962 if (!(*scontext))
963 return -ENOMEM;
964 return 0;
965 }
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /* Compute the size of the context. */
968 *scontext_len += strlen(policydb.p_user_val_to_name[context->user - 1]) + 1;
969 *scontext_len += strlen(policydb.p_role_val_to_name[context->role - 1]) + 1;
970 *scontext_len += strlen(policydb.p_type_val_to_name[context->type - 1]) + 1;
971 *scontext_len += mls_compute_context_len(context);
972
973 /* Allocate space for the context; caller must free this space. */
974 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -0400975 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 *scontext = scontextp;
978
979 /*
980 * Copy the user name, role name and type name into the context.
981 */
982 sprintf(scontextp, "%s:%s:%s",
983 policydb.p_user_val_to_name[context->user - 1],
984 policydb.p_role_val_to_name[context->role - 1],
985 policydb.p_type_val_to_name[context->type - 1]);
986 scontextp += strlen(policydb.p_user_val_to_name[context->user - 1]) +
Eric Paris5d55a342008-04-18 17:38:33 -0400987 1 + strlen(policydb.p_role_val_to_name[context->role - 1]) +
988 1 + strlen(policydb.p_type_val_to_name[context->type - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 mls_sid_to_context(context, &scontextp);
991
992 *scontextp = 0;
993
994 return 0;
995}
996
997#include "initial_sid_to_string.h"
998
James Carterf0ee2e42007-04-04 10:11:29 -0400999const char *security_get_initial_sid_context(u32 sid)
1000{
1001 if (unlikely(sid > SECINITSID_NUM))
1002 return NULL;
1003 return initial_sid_to_string[sid];
1004}
1005
Stephen Smalley12b29f32008-05-07 13:03:20 -04001006static int security_sid_to_context_core(u32 sid, char **scontext,
1007 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
1009 struct context *context;
1010 int rc = 0;
1011
Stephen Smalley4f4acf32007-02-26 12:02:34 -05001012 *scontext = NULL;
1013 *scontext_len = 0;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!ss_initialized) {
1016 if (sid <= SECINITSID_NUM) {
1017 char *scontextp;
1018
1019 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04001020 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -07001021 if (!scontextp) {
1022 rc = -ENOMEM;
1023 goto out;
1024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 strcpy(scontextp, initial_sid_to_string[sid]);
1026 *scontext = scontextp;
1027 goto out;
1028 }
Eric Paris744ba352008-04-17 11:52:44 -04001029 printk(KERN_ERR "SELinux: %s: called before initial "
1030 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 rc = -EINVAL;
1032 goto out;
1033 }
James Morris0804d112008-06-06 18:40:29 +10001034 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001035 if (force)
1036 context = sidtab_search_force(&sidtab, sid);
1037 else
1038 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -04001040 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1041 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 rc = -EINVAL;
1043 goto out_unlock;
1044 }
1045 rc = context_struct_to_string(context, scontext, scontext_len);
1046out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001047 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048out:
1049 return rc;
1050
1051}
1052
Stephen Smalley12b29f32008-05-07 13:03:20 -04001053/**
1054 * security_sid_to_context - Obtain a context for a given SID.
1055 * @sid: security identifier, SID
1056 * @scontext: security context
1057 * @scontext_len: length in bytes
1058 *
1059 * Write the string representation of the context associated with @sid
1060 * into a dynamically allocated string of the correct size. Set @scontext
1061 * to point to this string and set @scontext_len to the length of the string.
1062 */
1063int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Stephen Smalley12b29f32008-05-07 13:03:20 -04001065 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
1066}
1067
1068int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
1069{
1070 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
1071}
1072
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001073/*
1074 * Caveat: Mutates scontext.
1075 */
Stephen Smalley12b29f32008-05-07 13:03:20 -04001076static int string_to_context_struct(struct policydb *pol,
1077 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001078 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001079 u32 scontext_len,
1080 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001081 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct role_datum *role;
1084 struct type_datum *typdatum;
1085 struct user_datum *usrdatum;
1086 char *scontextp, *p, oldc;
1087 int rc = 0;
1088
Stephen Smalley12b29f32008-05-07 13:03:20 -04001089 context_init(ctx);
1090
Stephen Smalley12b29f32008-05-07 13:03:20 -04001091 /* Parse the security context. */
1092
1093 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001094 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001095
1096 /* Extract the user. */
1097 p = scontextp;
1098 while (*p && *p != ':')
1099 p++;
1100
1101 if (*p == 0)
1102 goto out;
1103
1104 *p++ = 0;
1105
1106 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1107 if (!usrdatum)
1108 goto out;
1109
1110 ctx->user = usrdatum->value;
1111
1112 /* Extract role. */
1113 scontextp = p;
1114 while (*p && *p != ':')
1115 p++;
1116
1117 if (*p == 0)
1118 goto out;
1119
1120 *p++ = 0;
1121
1122 role = hashtab_search(pol->p_roles.table, scontextp);
1123 if (!role)
1124 goto out;
1125 ctx->role = role->value;
1126
1127 /* Extract type. */
1128 scontextp = p;
1129 while (*p && *p != ':')
1130 p++;
1131 oldc = *p;
1132 *p++ = 0;
1133
1134 typdatum = hashtab_search(pol->p_types.table, scontextp);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001135 if (!typdatum || typdatum->attribute)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001136 goto out;
1137
1138 ctx->type = typdatum->value;
1139
1140 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
1141 if (rc)
1142 goto out;
1143
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001144 if ((p - scontext) < scontext_len) {
Stephen Smalley12b29f32008-05-07 13:03:20 -04001145 rc = -EINVAL;
1146 goto out;
1147 }
1148
1149 /* Check the validity of the new context. */
1150 if (!policydb_context_isvalid(pol, ctx)) {
1151 rc = -EINVAL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001152 goto out;
1153 }
1154 rc = 0;
1155out:
Eric Paris8e531af2008-09-03 11:49:47 -04001156 if (rc)
1157 context_destroy(ctx);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001158 return rc;
1159}
1160
1161static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
1162 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1163 int force)
1164{
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001165 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001166 struct context context;
1167 int rc = 0;
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (!ss_initialized) {
1170 int i;
1171
1172 for (i = 1; i < SECINITSID_NUM; i++) {
1173 if (!strcmp(initial_sid_to_string[i], scontext)) {
1174 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001175 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177 }
1178 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181 *sid = SECSID_NULL;
1182
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001183 /* Copy the string so that we can modify the copy as we parse it. */
1184 scontext2 = kmalloc(scontext_len+1, gfp_flags);
1185 if (!scontext2)
1186 return -ENOMEM;
1187 memcpy(scontext2, scontext, scontext_len);
1188 scontext2[scontext_len] = 0;
1189
1190 if (force) {
1191 /* Save another copy for storing in uninterpreted form */
1192 str = kstrdup(scontext2, gfp_flags);
1193 if (!str) {
1194 kfree(scontext2);
1195 return -ENOMEM;
1196 }
1197 }
1198
James Morris0804d112008-06-06 18:40:29 +10001199 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001200 rc = string_to_context_struct(&policydb, &sidtab,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001201 scontext2, scontext_len,
1202 &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001203 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001204 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001205 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001206 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001207 } else if (rc)
1208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Eric Paris8e531af2008-09-03 11:49:47 -04001210 context_destroy(&context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211out:
James Morris0804d112008-06-06 18:40:29 +10001212 read_unlock(&policy_rwlock);
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001213 kfree(scontext2);
1214 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return rc;
1216}
1217
James Morrisf5c1d5b2005-07-28 01:07:37 -07001218/**
1219 * security_context_to_sid - Obtain a SID for a given security context.
1220 * @scontext: security context
1221 * @scontext_len: length in bytes
1222 * @sid: security identifier, SID
1223 *
1224 * Obtains a SID associated with the security context that
1225 * has the string representation specified by @scontext.
1226 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1227 * memory is available, or 0 on success.
1228 */
David Howells8f0cfa52008-04-29 00:59:41 -07001229int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001230{
1231 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001232 sid, SECSID_NULL, GFP_KERNEL, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001233}
1234
1235/**
1236 * security_context_to_sid_default - Obtain a SID for a given security context,
1237 * falling back to specified default if needed.
1238 *
1239 * @scontext: security context
1240 * @scontext_len: length in bytes
1241 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -07001242 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -07001243 *
1244 * Obtains a SID associated with the security context that
1245 * has the string representation specified by @scontext.
1246 * The default SID is passed to the MLS layer to be used to allow
1247 * kernel labeling of the MLS field if the MLS field is not present
1248 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -04001249 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -07001250 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1251 * memory is available, or 0 on success.
1252 */
David Howells7bf570d2008-04-29 20:52:51 +01001253int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1254 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001255{
1256 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001257 sid, def_sid, gfp_flags, 1);
1258}
1259
1260int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1261 u32 *sid)
1262{
1263 return security_context_to_sid_core(scontext, scontext_len,
1264 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001265}
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267static int compute_sid_handle_invalid_context(
1268 struct context *scontext,
1269 struct context *tcontext,
1270 u16 tclass,
1271 struct context *newcontext)
1272{
1273 char *s = NULL, *t = NULL, *n = NULL;
1274 u32 slen, tlen, nlen;
1275
1276 if (context_struct_to_string(scontext, &s, &slen) < 0)
1277 goto out;
1278 if (context_struct_to_string(tcontext, &t, &tlen) < 0)
1279 goto out;
1280 if (context_struct_to_string(newcontext, &n, &nlen) < 0)
1281 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001282 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 "security_compute_sid: invalid context %s"
1284 " for scontext=%s"
1285 " tcontext=%s"
1286 " tclass=%s",
1287 n, s, t, policydb.p_class_val_to_name[tclass-1]);
1288out:
1289 kfree(s);
1290 kfree(t);
1291 kfree(n);
1292 if (!selinux_enforcing)
1293 return 0;
1294 return -EACCES;
1295}
1296
1297static int security_compute_sid(u32 ssid,
1298 u32 tsid,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001299 u16 orig_tclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 u32 specified,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001301 u32 *out_sid,
1302 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
1304 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1305 struct role_trans *roletr = NULL;
1306 struct avtab_key avkey;
1307 struct avtab_datum *avdatum;
1308 struct avtab_node *node;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001309 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 int rc = 0;
1311
1312 if (!ss_initialized) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001313 switch (orig_tclass) {
1314 case SECCLASS_PROCESS: /* kernel value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 *out_sid = ssid;
1316 break;
1317 default:
1318 *out_sid = tsid;
1319 break;
1320 }
1321 goto out;
1322 }
1323
Venkat Yekkirala851f8a62006-07-30 03:03:18 -07001324 context_init(&newcontext);
1325
James Morris0804d112008-06-06 18:40:29 +10001326 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001328 if (kern)
1329 tclass = unmap_class(orig_tclass);
1330 else
1331 tclass = orig_tclass;
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 scontext = sidtab_search(&sidtab, ssid);
1334 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001335 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1336 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 rc = -EINVAL;
1338 goto out_unlock;
1339 }
1340 tcontext = sidtab_search(&sidtab, tsid);
1341 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001342 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1343 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 rc = -EINVAL;
1345 goto out_unlock;
1346 }
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 /* Set the user identity. */
1349 switch (specified) {
1350 case AVTAB_TRANSITION:
1351 case AVTAB_CHANGE:
1352 /* Use the process user identity. */
1353 newcontext.user = scontext->user;
1354 break;
1355 case AVTAB_MEMBER:
1356 /* Use the related object owner. */
1357 newcontext.user = tcontext->user;
1358 break;
1359 }
1360
1361 /* Set the role and type to default values. */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001362 if (tclass == policydb.process_class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 /* Use the current role and type of process. */
1364 newcontext.role = scontext->role;
1365 newcontext.type = scontext->type;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001366 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* Use the well-defined object role. */
1368 newcontext.role = OBJECT_R_VAL;
1369 /* Use the type of the related object. */
1370 newcontext.type = tcontext->type;
1371 }
1372
1373 /* Look for a type transition/member/change rule. */
1374 avkey.source_type = scontext->type;
1375 avkey.target_type = tcontext->type;
1376 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001377 avkey.specified = specified;
1378 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001381 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001382 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03001383 for (; node; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001384 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 avdatum = &node->datum;
1386 break;
1387 }
1388 }
1389 }
1390
Stephen Smalley782ebb92005-09-03 15:55:16 -07001391 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 /* Use the type from the type transition/member/change rule. */
Stephen Smalley782ebb92005-09-03 15:55:16 -07001393 newcontext.type = avdatum->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
1396 /* Check for class-specific changes. */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001397 if (tclass == policydb.process_class) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 if (specified & AVTAB_TRANSITION) {
1399 /* Look for a role transition rule. */
1400 for (roletr = policydb.role_tr; roletr;
1401 roletr = roletr->next) {
1402 if (roletr->role == scontext->role &&
1403 roletr->type == tcontext->type) {
1404 /* Use the role transition rule. */
1405 newcontext.role = roletr->new_role;
1406 break;
1407 }
1408 }
1409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
1412 /* Set the MLS attributes.
1413 This is done last because it may allocate memory. */
1414 rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext);
1415 if (rc)
1416 goto out_unlock;
1417
1418 /* Check the validity of the context. */
1419 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1420 rc = compute_sid_handle_invalid_context(scontext,
1421 tcontext,
1422 tclass,
1423 &newcontext);
1424 if (rc)
1425 goto out_unlock;
1426 }
1427 /* Obtain the sid for the context. */
1428 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1429out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001430 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 context_destroy(&newcontext);
1432out:
1433 return rc;
1434}
1435
1436/**
1437 * security_transition_sid - Compute the SID for a new subject/object.
1438 * @ssid: source security identifier
1439 * @tsid: target security identifier
1440 * @tclass: target security class
1441 * @out_sid: security identifier for new subject/object
1442 *
1443 * Compute a SID to use for labeling a new subject or object in the
1444 * class @tclass based on a SID pair (@ssid, @tsid).
1445 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1446 * if insufficient memory is available, or %0 if the new SID was
1447 * computed successfully.
1448 */
1449int security_transition_sid(u32 ssid,
1450 u32 tsid,
1451 u16 tclass,
1452 u32 *out_sid)
1453{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001454 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1455 out_sid, true);
1456}
1457
1458int security_transition_sid_user(u32 ssid,
1459 u32 tsid,
1460 u16 tclass,
1461 u32 *out_sid)
1462{
1463 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
1464 out_sid, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467/**
1468 * security_member_sid - Compute the SID for member selection.
1469 * @ssid: source security identifier
1470 * @tsid: target security identifier
1471 * @tclass: target security class
1472 * @out_sid: security identifier for selected member
1473 *
1474 * Compute a SID to use when selecting a member of a polyinstantiated
1475 * object of class @tclass based on a SID pair (@ssid, @tsid).
1476 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1477 * if insufficient memory is available, or %0 if the SID was
1478 * computed successfully.
1479 */
1480int security_member_sid(u32 ssid,
1481 u32 tsid,
1482 u16 tclass,
1483 u32 *out_sid)
1484{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001485 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid,
1486 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
1489/**
1490 * security_change_sid - Compute the SID for object relabeling.
1491 * @ssid: source security identifier
1492 * @tsid: target security identifier
1493 * @tclass: target security class
1494 * @out_sid: security identifier for selected member
1495 *
1496 * Compute a SID to use for relabeling an object of class @tclass
1497 * based on a SID pair (@ssid, @tsid).
1498 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1499 * if insufficient memory is available, or %0 if the SID was
1500 * computed successfully.
1501 */
1502int security_change_sid(u32 ssid,
1503 u32 tsid,
1504 u16 tclass,
1505 u32 *out_sid)
1506{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001507 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid,
1508 false);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001509}
1510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511/* Clone the SID into the new SID table. */
1512static int clone_sid(u32 sid,
1513 struct context *context,
1514 void *arg)
1515{
1516 struct sidtab *s = arg;
1517
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001518 if (sid > SECINITSID_NUM)
1519 return sidtab_insert(s, sid, context);
1520 else
1521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524static inline int convert_context_handle_invalid_context(struct context *context)
1525{
1526 int rc = 0;
1527
1528 if (selinux_enforcing) {
1529 rc = -EINVAL;
1530 } else {
1531 char *s;
1532 u32 len;
1533
Stephen Smalley12b29f32008-05-07 13:03:20 -04001534 if (!context_struct_to_string(context, &s, &len)) {
1535 printk(KERN_WARNING
1536 "SELinux: Context %s would be invalid if enforcing\n",
1537 s);
1538 kfree(s);
1539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
1541 return rc;
1542}
1543
1544struct convert_context_args {
1545 struct policydb *oldp;
1546 struct policydb *newp;
1547};
1548
1549/*
1550 * Convert the values in the security context
1551 * structure `c' from the values specified
1552 * in the policy `p->oldp' to the values specified
1553 * in the policy `p->newp'. Verify that the
1554 * context is valid under the new policy.
1555 */
1556static int convert_context(u32 key,
1557 struct context *c,
1558 void *p)
1559{
1560 struct convert_context_args *args;
1561 struct context oldc;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001562 struct ocontext *oc;
1563 struct mls_range *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct role_datum *role;
1565 struct type_datum *typdatum;
1566 struct user_datum *usrdatum;
1567 char *s;
1568 u32 len;
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001569 int rc = 0;
1570
1571 if (key <= SECINITSID_NUM)
1572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574 args = p;
1575
Stephen Smalley12b29f32008-05-07 13:03:20 -04001576 if (c->str) {
1577 struct context ctx;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001578 s = kstrdup(c->str, GFP_KERNEL);
1579 if (!s) {
1580 rc = -ENOMEM;
1581 goto out;
1582 }
1583 rc = string_to_context_struct(args->newp, NULL, s,
1584 c->len, &ctx, SECSID_NULL);
1585 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001586 if (!rc) {
1587 printk(KERN_INFO
1588 "SELinux: Context %s became valid (mapped).\n",
1589 c->str);
1590 /* Replace string with mapped representation. */
1591 kfree(c->str);
1592 memcpy(c, &ctx, sizeof(*c));
1593 goto out;
1594 } else if (rc == -EINVAL) {
1595 /* Retain string representation for later mapping. */
1596 rc = 0;
1597 goto out;
1598 } else {
1599 /* Other error condition, e.g. ENOMEM. */
1600 printk(KERN_ERR
1601 "SELinux: Unable to map context %s, rc = %d.\n",
1602 c->str, -rc);
1603 goto out;
1604 }
1605 }
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 rc = context_cpy(&oldc, c);
1608 if (rc)
1609 goto out;
1610
1611 rc = -EINVAL;
1612
1613 /* Convert the user. */
1614 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001615 args->oldp->p_user_val_to_name[c->user - 1]);
1616 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 c->user = usrdatum->value;
1619
1620 /* Convert the role. */
1621 role = hashtab_search(args->newp->p_roles.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001622 args->oldp->p_role_val_to_name[c->role - 1]);
1623 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 c->role = role->value;
1626
1627 /* Convert the type. */
1628 typdatum = hashtab_search(args->newp->p_types.table,
Eric Paris5d55a342008-04-18 17:38:33 -04001629 args->oldp->p_type_val_to_name[c->type - 1]);
1630 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 c->type = typdatum->value;
1633
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001634 /* Convert the MLS fields if dealing with MLS policies */
1635 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
1636 rc = mls_convert_context(args->oldp, args->newp, c);
1637 if (rc)
1638 goto bad;
1639 } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
1640 /*
1641 * Switching between MLS and non-MLS policy:
1642 * free any storage used by the MLS fields in the
1643 * context for all existing entries in the sidtab.
1644 */
1645 mls_context_destroy(c);
1646 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
1647 /*
1648 * Switching between non-MLS and MLS policy:
1649 * ensure that the MLS fields of the context for all
1650 * existing entries in the sidtab are filled in with a
1651 * suitable default value, likely taken from one of the
1652 * initial SIDs.
1653 */
1654 oc = args->newp->ocontexts[OCON_ISID];
1655 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
1656 oc = oc->next;
1657 if (!oc) {
1658 printk(KERN_ERR "SELinux: unable to look up"
1659 " the initial SIDs list\n");
1660 goto bad;
1661 }
1662 range = &oc->context[0].range;
1663 rc = mls_range_set(c, range);
1664 if (rc)
1665 goto bad;
1666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
1668 /* Check the validity of the new context. */
1669 if (!policydb_context_isvalid(args->newp, c)) {
1670 rc = convert_context_handle_invalid_context(&oldc);
1671 if (rc)
1672 goto bad;
1673 }
1674
1675 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001676 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677out:
1678 return rc;
1679bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001680 /* Map old representation to string and save it. */
1681 if (context_struct_to_string(&oldc, &s, &len))
1682 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001684 context_destroy(c);
1685 c->str = s;
1686 c->len = len;
1687 printk(KERN_INFO
1688 "SELinux: Context %s became invalid (unmapped).\n",
1689 c->str);
1690 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 goto out;
1692}
1693
Paul Moore3bb56b22008-01-29 08:38:19 -05001694static void security_load_policycaps(void)
1695{
1696 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1697 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001698 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1699 POLICYDB_CAPABILITY_OPENPERM);
Paul Moore3bb56b22008-01-29 08:38:19 -05001700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702extern void selinux_complete_init(void);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001703static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705/**
1706 * security_load_policy - Load a security policy configuration.
1707 * @data: binary policy data
1708 * @len: length of data in bytes
1709 *
1710 * Load a new set of security policy configuration data,
1711 * validate it and convert the SID table as necessary.
1712 * This function will flush the access vector cache after
1713 * loading the new policy.
1714 */
1715int security_load_policy(void *data, size_t len)
1716{
1717 struct policydb oldpolicydb, newpolicydb;
1718 struct sidtab oldsidtab, newsidtab;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001719 struct selinux_mapping *oldmap, *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 struct convert_context_args args;
1721 u32 seqno;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001722 u16 map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int rc = 0;
1724 struct policy_file file = { data, len }, *fp = &file;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (!ss_initialized) {
1727 avtab_cache_init();
1728 if (policydb_read(&policydb, fp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 avtab_cache_destroy();
1730 return -EINVAL;
1731 }
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001732 if (selinux_set_mapping(&policydb, secclass_map,
1733 &current_mapping,
1734 &current_mapping_size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 policydb_destroy(&policydb);
1736 avtab_cache_destroy();
1737 return -EINVAL;
1738 }
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001739 if (policydb_load_isids(&policydb, &sidtab)) {
Chad Sellersb94c7e62006-11-06 12:38:18 -05001740 policydb_destroy(&policydb);
1741 avtab_cache_destroy();
1742 return -EINVAL;
1743 }
Paul Moore3bb56b22008-01-29 08:38:19 -05001744 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07001746 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07001748 avc_ss_reset(seqno);
1749 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001750 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001751 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 return 0;
1753 }
1754
1755#if 0
1756 sidtab_hash_eval(&sidtab, "sids");
1757#endif
1758
Eric Paris89abd0a2008-06-09 15:58:04 -04001759 if (policydb_read(&newpolicydb, fp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001762 /* If switching between different policy types, log MLS status */
1763 if (policydb.mls_enabled && !newpolicydb.mls_enabled)
1764 printk(KERN_INFO "SELinux: Disabling MLS support...\n");
1765 else if (!policydb.mls_enabled && newpolicydb.mls_enabled)
1766 printk(KERN_INFO "SELinux: Enabling MLS support...\n");
1767
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001768 rc = policydb_load_isids(&newpolicydb, &newsidtab);
1769 if (rc) {
1770 printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
Stephen Smalley12b29f32008-05-07 13:03:20 -04001771 policydb_destroy(&newpolicydb);
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001772 return rc;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001775 if (selinux_set_mapping(&newpolicydb, secclass_map,
1776 &map, &map_size))
Chad Sellersb94c7e62006-11-06 12:38:18 -05001777 goto err;
Chad Sellersb94c7e62006-11-06 12:38:18 -05001778
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001779 rc = security_preserve_bools(&newpolicydb);
1780 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11001781 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04001782 goto err;
1783 }
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 /* Clone the SID table. */
1786 sidtab_shutdown(&sidtab);
1787 if (sidtab_map(&sidtab, clone_sid, &newsidtab)) {
1788 rc = -ENOMEM;
1789 goto err;
1790 }
1791
Stephen Smalley12b29f32008-05-07 13:03:20 -04001792 /*
1793 * Convert the internal representations of contexts
1794 * in the new SID table.
1795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 args.oldp = &policydb;
1797 args.newp = &newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001798 rc = sidtab_map(&newsidtab, convert_context, &args);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001799 if (rc) {
1800 printk(KERN_ERR "SELinux: unable to convert the internal"
1801 " representation of contexts in the new SID"
1802 " table\n");
Stephen Smalley12b29f32008-05-07 13:03:20 -04001803 goto err;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 /* Save the old policydb and SID table to free later. */
1807 memcpy(&oldpolicydb, &policydb, sizeof policydb);
1808 sidtab_set(&oldsidtab, &sidtab);
1809
1810 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10001811 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 memcpy(&policydb, &newpolicydb, sizeof policydb);
1813 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05001814 security_load_policycaps();
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001815 oldmap = current_mapping;
1816 current_mapping = map;
1817 current_mapping_size = map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 seqno = ++latest_granting;
James Morris0804d112008-06-06 18:40:29 +10001819 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 /* Free the old policydb and SID table. */
1822 policydb_destroy(&oldpolicydb);
1823 sidtab_destroy(&oldsidtab);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001824 kfree(oldmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 avc_ss_reset(seqno);
1827 selnl_notify_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07001828 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08001829 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 return 0;
1832
1833err:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001834 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 sidtab_destroy(&newsidtab);
1836 policydb_destroy(&newpolicydb);
1837 return rc;
1838
1839}
1840
1841/**
1842 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 * @protocol: protocol number
1844 * @port: port number
1845 * @out_sid: security identifier
1846 */
Paul Moore3e112172008-04-10 10:48:14 -04001847int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
1849 struct ocontext *c;
1850 int rc = 0;
1851
James Morris0804d112008-06-06 18:40:29 +10001852 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 c = policydb.ocontexts[OCON_PORT];
1855 while (c) {
1856 if (c->u.port.protocol == protocol &&
1857 c->u.port.low_port <= port &&
1858 c->u.port.high_port >= port)
1859 break;
1860 c = c->next;
1861 }
1862
1863 if (c) {
1864 if (!c->sid[0]) {
1865 rc = sidtab_context_to_sid(&sidtab,
1866 &c->context[0],
1867 &c->sid[0]);
1868 if (rc)
1869 goto out;
1870 }
1871 *out_sid = c->sid[0];
1872 } else {
1873 *out_sid = SECINITSID_PORT;
1874 }
1875
1876out:
James Morris0804d112008-06-06 18:40:29 +10001877 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return rc;
1879}
1880
1881/**
1882 * security_netif_sid - Obtain the SID for a network interface.
1883 * @name: interface name
1884 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05001886int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 int rc = 0;
1889 struct ocontext *c;
1890
James Morris0804d112008-06-06 18:40:29 +10001891 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 c = policydb.ocontexts[OCON_NETIF];
1894 while (c) {
1895 if (strcmp(name, c->u.name) == 0)
1896 break;
1897 c = c->next;
1898 }
1899
1900 if (c) {
1901 if (!c->sid[0] || !c->sid[1]) {
1902 rc = sidtab_context_to_sid(&sidtab,
1903 &c->context[0],
1904 &c->sid[0]);
1905 if (rc)
1906 goto out;
1907 rc = sidtab_context_to_sid(&sidtab,
1908 &c->context[1],
1909 &c->sid[1]);
1910 if (rc)
1911 goto out;
1912 }
1913 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05001914 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917out:
James Morris0804d112008-06-06 18:40:29 +10001918 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return rc;
1920}
1921
1922static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
1923{
1924 int i, fail = 0;
1925
Eric Paris5d55a342008-04-18 17:38:33 -04001926 for (i = 0; i < 4; i++)
1927 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 fail = 1;
1929 break;
1930 }
1931
1932 return !fail;
1933}
1934
1935/**
1936 * security_node_sid - Obtain the SID for a node (host).
1937 * @domain: communication domain aka address family
1938 * @addrp: address
1939 * @addrlen: address length in bytes
1940 * @out_sid: security identifier
1941 */
1942int security_node_sid(u16 domain,
1943 void *addrp,
1944 u32 addrlen,
1945 u32 *out_sid)
1946{
1947 int rc = 0;
1948 struct ocontext *c;
1949
James Morris0804d112008-06-06 18:40:29 +10001950 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 switch (domain) {
1953 case AF_INET: {
1954 u32 addr;
1955
1956 if (addrlen != sizeof(u32)) {
1957 rc = -EINVAL;
1958 goto out;
1959 }
1960
1961 addr = *((u32 *)addrp);
1962
1963 c = policydb.ocontexts[OCON_NODE];
1964 while (c) {
1965 if (c->u.node.addr == (addr & c->u.node.mask))
1966 break;
1967 c = c->next;
1968 }
1969 break;
1970 }
1971
1972 case AF_INET6:
1973 if (addrlen != sizeof(u64) * 2) {
1974 rc = -EINVAL;
1975 goto out;
1976 }
1977 c = policydb.ocontexts[OCON_NODE6];
1978 while (c) {
1979 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
1980 c->u.node6.mask))
1981 break;
1982 c = c->next;
1983 }
1984 break;
1985
1986 default:
1987 *out_sid = SECINITSID_NODE;
1988 goto out;
1989 }
1990
1991 if (c) {
1992 if (!c->sid[0]) {
1993 rc = sidtab_context_to_sid(&sidtab,
1994 &c->context[0],
1995 &c->sid[0]);
1996 if (rc)
1997 goto out;
1998 }
1999 *out_sid = c->sid[0];
2000 } else {
2001 *out_sid = SECINITSID_NODE;
2002 }
2003
2004out:
James Morris0804d112008-06-06 18:40:29 +10002005 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return rc;
2007}
2008
2009#define SIDS_NEL 25
2010
2011/**
2012 * security_get_user_sids - Obtain reachable SIDs for a user.
2013 * @fromsid: starting SID
2014 * @username: username
2015 * @sids: array of reachable SIDs for user
2016 * @nel: number of elements in @sids
2017 *
2018 * Generate the set of SIDs for legal security contexts
2019 * for a given user that can be reached by @fromsid.
2020 * Set *@sids to point to a dynamically allocated
2021 * array containing the set of SIDs. Set *@nel to the
2022 * number of elements in the array.
2023 */
2024
2025int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04002026 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 u32 **sids,
2028 u32 *nel)
2029{
2030 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002031 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 u32 mynel = 0, maxnel = SIDS_NEL;
2033 struct user_datum *user;
2034 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002035 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 int rc = 0, i, j;
2037
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002038 *sids = NULL;
2039 *nel = 0;
2040
2041 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
James Morris0804d112008-06-06 18:40:29 +10002044 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Stephen Smalley12b29f32008-05-07 13:03:20 -04002046 context_init(&usercon);
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 fromcon = sidtab_search(&sidtab, fromsid);
2049 if (!fromcon) {
2050 rc = -EINVAL;
2051 goto out_unlock;
2052 }
2053
2054 user = hashtab_search(policydb.p_users.table, username);
2055 if (!user) {
2056 rc = -EINVAL;
2057 goto out_unlock;
2058 }
2059 usercon.user = user->value;
2060
James Morris89d155e2005-10-30 14:59:21 -08002061 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (!mysids) {
2063 rc = -ENOMEM;
2064 goto out_unlock;
2065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002067 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 role = policydb.role_val_to_struct[i];
2069 usercon.role = i+1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002070 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 usercon.type = j+1;
2072
2073 if (mls_setup_user_range(fromcon, user, &usercon))
2074 continue;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002077 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (mynel < maxnel) {
2080 mysids[mynel++] = sid;
2081 } else {
2082 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08002083 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (!mysids2) {
2085 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 goto out_unlock;
2087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2089 kfree(mysids);
2090 mysids = mysids2;
2091 mysids[mynel++] = sid;
2092 }
2093 }
2094 }
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002097 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002098 if (rc || !mynel) {
2099 kfree(mysids);
2100 goto out;
2101 }
2102
2103 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2104 if (!mysids2) {
2105 rc = -ENOMEM;
2106 kfree(mysids);
2107 goto out;
2108 }
2109 for (i = 0, j = 0; i < mynel; i++) {
2110 rc = avc_has_perm_noaudit(fromsid, mysids[i],
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002111 SECCLASS_PROCESS, /* kernel value */
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002112 PROCESS__TRANSITION, AVC_STRICT,
2113 NULL);
2114 if (!rc)
2115 mysids2[j++] = mysids[i];
2116 cond_resched();
2117 }
2118 rc = 0;
2119 kfree(mysids);
2120 *sids = mysids2;
2121 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122out:
2123 return rc;
2124}
2125
2126/**
2127 * security_genfs_sid - Obtain a SID for a file in a filesystem
2128 * @fstype: filesystem type
2129 * @path: path from root of mount
2130 * @sclass: file security class
2131 * @sid: SID for path
2132 *
2133 * Obtain a SID to use for a file in a filesystem that
2134 * cannot support xattr or use a fixed labeling behavior like
2135 * transition SIDs or task SIDs.
2136 */
2137int security_genfs_sid(const char *fstype,
Eric Paris5d55a342008-04-18 17:38:33 -04002138 char *path,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002139 u16 orig_sclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 u32 *sid)
2141{
2142 int len;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002143 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 struct genfs *genfs;
2145 struct ocontext *c;
2146 int rc = 0, cmp = 0;
2147
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05002148 while (path[0] == '/' && path[1] == '/')
2149 path++;
2150
James Morris0804d112008-06-06 18:40:29 +10002151 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002153 sclass = unmap_class(orig_sclass);
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2156 cmp = strcmp(fstype, genfs->fstype);
2157 if (cmp <= 0)
2158 break;
2159 }
2160
2161 if (!genfs || cmp) {
2162 *sid = SECINITSID_UNLABELED;
2163 rc = -ENOENT;
2164 goto out;
2165 }
2166
2167 for (c = genfs->head; c; c = c->next) {
2168 len = strlen(c->u.name);
2169 if ((!c->v.sclass || sclass == c->v.sclass) &&
2170 (strncmp(c->u.name, path, len) == 0))
2171 break;
2172 }
2173
2174 if (!c) {
2175 *sid = SECINITSID_UNLABELED;
2176 rc = -ENOENT;
2177 goto out;
2178 }
2179
2180 if (!c->sid[0]) {
2181 rc = sidtab_context_to_sid(&sidtab,
2182 &c->context[0],
2183 &c->sid[0]);
2184 if (rc)
2185 goto out;
2186 }
2187
2188 *sid = c->sid[0];
2189out:
James Morris0804d112008-06-06 18:40:29 +10002190 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 return rc;
2192}
2193
2194/**
2195 * security_fs_use - Determine how to handle labeling for a filesystem.
2196 * @fstype: filesystem type
2197 * @behavior: labeling behavior
2198 * @sid: SID for filesystem (superblock)
2199 */
2200int security_fs_use(
2201 const char *fstype,
2202 unsigned int *behavior,
James Morris089be432008-07-15 18:32:49 +10002203 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 int rc = 0;
2206 struct ocontext *c;
2207
James Morris0804d112008-06-06 18:40:29 +10002208 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 c = policydb.ocontexts[OCON_FSUSE];
2211 while (c) {
2212 if (strcmp(fstype, c->u.name) == 0)
2213 break;
2214 c = c->next;
2215 }
2216
2217 if (c) {
2218 *behavior = c->v.behavior;
2219 if (!c->sid[0]) {
2220 rc = sidtab_context_to_sid(&sidtab,
2221 &c->context[0],
2222 &c->sid[0]);
2223 if (rc)
2224 goto out;
2225 }
2226 *sid = c->sid[0];
2227 } else {
James Morris089be432008-07-15 18:32:49 +10002228 rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
2229 if (rc) {
2230 *behavior = SECURITY_FS_USE_NONE;
2231 rc = 0;
2232 } else {
2233 *behavior = SECURITY_FS_USE_GENFS;
2234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
2236
2237out:
James Morris0804d112008-06-06 18:40:29 +10002238 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return rc;
2240}
2241
2242int security_get_bools(int *len, char ***names, int **values)
2243{
2244 int i, rc = -ENOMEM;
2245
James Morris0804d112008-06-06 18:40:29 +10002246 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 *names = NULL;
2248 *values = NULL;
2249
2250 *len = policydb.p_bools.nprim;
2251 if (!*len) {
2252 rc = 0;
2253 goto out;
2254 }
2255
Eric Paris5d55a342008-04-18 17:38:33 -04002256 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (!*names)
2258 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
Jesper Juhle0795cf2006-01-09 20:54:46 -08002260 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 if (!*values)
2262 goto err;
2263
2264 for (i = 0; i < *len; i++) {
2265 size_t name_len;
2266 (*values)[i] = policydb.bool_val_to_struct[i]->state;
2267 name_len = strlen(policydb.p_bool_val_to_name[i]) + 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002268 (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (!(*names)[i])
2270 goto err;
2271 strncpy((*names)[i], policydb.p_bool_val_to_name[i], name_len);
2272 (*names)[i][name_len - 1] = 0;
2273 }
2274 rc = 0;
2275out:
James Morris0804d112008-06-06 18:40:29 +10002276 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 return rc;
2278err:
2279 if (*names) {
2280 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002281 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002283 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto out;
2285}
2286
2287
2288int security_set_bools(int len, int *values)
2289{
2290 int i, rc = 0;
2291 int lenp, seqno = 0;
2292 struct cond_node *cur;
2293
James Morris0804d112008-06-06 18:40:29 +10002294 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 lenp = policydb.p_bools.nprim;
2297 if (len != lenp) {
2298 rc = -EFAULT;
2299 goto out;
2300 }
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002303 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2304 audit_log(current->audit_context, GFP_ATOMIC,
2305 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002306 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Steve Grubbaf601e42006-01-04 14:08:39 +00002307 policydb.p_bool_val_to_name[i],
2308 !!values[i],
2309 policydb.bool_val_to_struct[i]->state,
Eric Paris4746ec52008-01-08 10:06:53 -05002310 audit_get_loginuid(current),
2311 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002312 }
Eric Paris5d55a342008-04-18 17:38:33 -04002313 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002315 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002319 for (cur = policydb.cond_list; cur; cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 rc = evaluate_cond_node(&policydb, cur);
2321 if (rc)
2322 goto out;
2323 }
2324
2325 seqno = ++latest_granting;
2326
2327out:
James Morris0804d112008-06-06 18:40:29 +10002328 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (!rc) {
2330 avc_ss_reset(seqno);
2331 selnl_notify_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002332 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
2334 return rc;
2335}
2336
2337int security_get_bool_value(int bool)
2338{
2339 int rc = 0;
2340 int len;
2341
James Morris0804d112008-06-06 18:40:29 +10002342 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
2344 len = policydb.p_bools.nprim;
2345 if (bool >= len) {
2346 rc = -EFAULT;
2347 goto out;
2348 }
2349
2350 rc = policydb.bool_val_to_struct[bool]->state;
2351out:
James Morris0804d112008-06-06 18:40:29 +10002352 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return rc;
2354}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002355
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002356static int security_preserve_bools(struct policydb *p)
2357{
2358 int rc, nbools = 0, *bvalues = NULL, i;
2359 char **bnames = NULL;
2360 struct cond_bool_datum *booldatum;
2361 struct cond_node *cur;
2362
2363 rc = security_get_bools(&nbools, &bnames, &bvalues);
2364 if (rc)
2365 goto out;
2366 for (i = 0; i < nbools; i++) {
2367 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2368 if (booldatum)
2369 booldatum->state = bvalues[i];
2370 }
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002371 for (cur = p->cond_list; cur; cur = cur->next) {
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002372 rc = evaluate_cond_node(p, cur);
2373 if (rc)
2374 goto out;
2375 }
2376
2377out:
2378 if (bnames) {
2379 for (i = 0; i < nbools; i++)
2380 kfree(bnames[i]);
2381 }
2382 kfree(bnames);
2383 kfree(bvalues);
2384 return rc;
2385}
2386
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002387/*
2388 * security_sid_mls_copy() - computes a new sid based on the given
2389 * sid and the mls portion of mls_sid.
2390 */
2391int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2392{
2393 struct context *context1;
2394 struct context *context2;
2395 struct context newcon;
2396 char *s;
2397 u32 len;
2398 int rc = 0;
2399
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002400 if (!ss_initialized || !policydb.mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002401 *new_sid = sid;
2402 goto out;
2403 }
2404
2405 context_init(&newcon);
2406
James Morris0804d112008-06-06 18:40:29 +10002407 read_lock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002408 context1 = sidtab_search(&sidtab, sid);
2409 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002410 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2411 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002412 rc = -EINVAL;
2413 goto out_unlock;
2414 }
2415
2416 context2 = sidtab_search(&sidtab, mls_sid);
2417 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002418 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2419 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002420 rc = -EINVAL;
2421 goto out_unlock;
2422 }
2423
2424 newcon.user = context1->user;
2425 newcon.role = context1->role;
2426 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002427 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002428 if (rc)
2429 goto out_unlock;
2430
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002431 /* Check the validity of the new context. */
2432 if (!policydb_context_isvalid(&policydb, &newcon)) {
2433 rc = convert_context_handle_invalid_context(&newcon);
2434 if (rc)
2435 goto bad;
2436 }
2437
2438 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
2439 goto out_unlock;
2440
2441bad:
2442 if (!context_struct_to_string(&newcon, &s, &len)) {
2443 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
2444 "security_sid_mls_copy: invalid context %s", s);
2445 kfree(s);
2446 }
2447
2448out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002449 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002450 context_destroy(&newcon);
2451out:
2452 return rc;
2453}
2454
Paul Moore220deb92008-01-29 08:38:23 -05002455/**
2456 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2457 * @nlbl_sid: NetLabel SID
2458 * @nlbl_type: NetLabel labeling protocol type
2459 * @xfrm_sid: XFRM SID
2460 *
2461 * Description:
2462 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2463 * resolved into a single SID it is returned via @peer_sid and the function
2464 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2465 * returns a negative value. A table summarizing the behavior is below:
2466 *
2467 * | function return | @sid
2468 * ------------------------------+-----------------+-----------------
2469 * no peer labels | 0 | SECSID_NULL
2470 * single peer label | 0 | <peer_label>
2471 * multiple, consistent labels | 0 | <peer_label>
2472 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2473 *
2474 */
2475int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2476 u32 xfrm_sid,
2477 u32 *peer_sid)
2478{
2479 int rc;
2480 struct context *nlbl_ctx;
2481 struct context *xfrm_ctx;
2482
2483 /* handle the common (which also happens to be the set of easy) cases
2484 * right away, these two if statements catch everything involving a
2485 * single or absent peer SID/label */
2486 if (xfrm_sid == SECSID_NULL) {
2487 *peer_sid = nlbl_sid;
2488 return 0;
2489 }
2490 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2491 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2492 * is present */
2493 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2494 *peer_sid = xfrm_sid;
2495 return 0;
2496 }
2497
2498 /* we don't need to check ss_initialized here since the only way both
2499 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2500 * security server was initialized and ss_initialized was true */
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002501 if (!policydb.mls_enabled) {
Paul Moore220deb92008-01-29 08:38:23 -05002502 *peer_sid = SECSID_NULL;
2503 return 0;
2504 }
2505
James Morris0804d112008-06-06 18:40:29 +10002506 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002507
2508 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2509 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002510 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2511 __func__, nlbl_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002512 rc = -EINVAL;
2513 goto out_slowpath;
2514 }
2515 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2516 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002517 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2518 __func__, xfrm_sid);
Paul Moore220deb92008-01-29 08:38:23 -05002519 rc = -EINVAL;
2520 goto out_slowpath;
2521 }
2522 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
2523
2524out_slowpath:
James Morris0804d112008-06-06 18:40:29 +10002525 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002526 if (rc == 0)
2527 /* at present NetLabel SIDs/labels really only carry MLS
2528 * information so if the MLS portion of the NetLabel SID
2529 * matches the MLS portion of the labeled XFRM SID/label
2530 * then pass along the XFRM SID as it is the most
2531 * expressive */
2532 *peer_sid = xfrm_sid;
2533 else
2534 *peer_sid = SECSID_NULL;
2535 return rc;
2536}
2537
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002538static int get_classes_callback(void *k, void *d, void *args)
2539{
2540 struct class_datum *datum = d;
2541 char *name = k, **classes = args;
2542 int value = datum->value - 1;
2543
2544 classes[value] = kstrdup(name, GFP_ATOMIC);
2545 if (!classes[value])
2546 return -ENOMEM;
2547
2548 return 0;
2549}
2550
2551int security_get_classes(char ***classes, int *nclasses)
2552{
2553 int rc = -ENOMEM;
2554
James Morris0804d112008-06-06 18:40:29 +10002555 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002556
2557 *nclasses = policydb.p_classes.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002558 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002559 if (!*classes)
2560 goto out;
2561
2562 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2563 *classes);
2564 if (rc < 0) {
2565 int i;
2566 for (i = 0; i < *nclasses; i++)
2567 kfree((*classes)[i]);
2568 kfree(*classes);
2569 }
2570
2571out:
James Morris0804d112008-06-06 18:40:29 +10002572 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002573 return rc;
2574}
2575
2576static int get_permissions_callback(void *k, void *d, void *args)
2577{
2578 struct perm_datum *datum = d;
2579 char *name = k, **perms = args;
2580 int value = datum->value - 1;
2581
2582 perms[value] = kstrdup(name, GFP_ATOMIC);
2583 if (!perms[value])
2584 return -ENOMEM;
2585
2586 return 0;
2587}
2588
2589int security_get_permissions(char *class, char ***perms, int *nperms)
2590{
2591 int rc = -ENOMEM, i;
2592 struct class_datum *match;
2593
James Morris0804d112008-06-06 18:40:29 +10002594 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002595
2596 match = hashtab_search(policydb.p_classes.table, class);
2597 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002598 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002599 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002600 rc = -EINVAL;
2601 goto out;
2602 }
2603
2604 *nperms = match->permissions.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002605 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002606 if (!*perms)
2607 goto out;
2608
2609 if (match->comdatum) {
2610 rc = hashtab_map(match->comdatum->permissions.table,
2611 get_permissions_callback, *perms);
2612 if (rc < 0)
2613 goto err;
2614 }
2615
2616 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2617 *perms);
2618 if (rc < 0)
2619 goto err;
2620
2621out:
James Morris0804d112008-06-06 18:40:29 +10002622 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002623 return rc;
2624
2625err:
James Morris0804d112008-06-06 18:40:29 +10002626 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002627 for (i = 0; i < *nperms; i++)
2628 kfree((*perms)[i]);
2629 kfree(*perms);
2630 return rc;
2631}
2632
Eric Paris3f120702007-09-21 14:37:10 -04002633int security_get_reject_unknown(void)
2634{
2635 return policydb.reject_unknown;
2636}
2637
2638int security_get_allow_unknown(void)
2639{
2640 return policydb.allow_unknown;
2641}
2642
Paul Moore3bb56b22008-01-29 08:38:19 -05002643/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002644 * security_policycap_supported - Check for a specific policy capability
2645 * @req_cap: capability
2646 *
2647 * Description:
2648 * This function queries the currently loaded policy to see if it supports the
2649 * capability specified by @req_cap. Returns true (1) if the capability is
2650 * supported, false (0) if it isn't supported.
2651 *
2652 */
2653int security_policycap_supported(unsigned int req_cap)
2654{
2655 int rc;
2656
James Morris0804d112008-06-06 18:40:29 +10002657 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002658 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10002659 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05002660
2661 return rc;
2662}
2663
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002664struct selinux_audit_rule {
2665 u32 au_seqno;
2666 struct context au_ctxt;
2667};
2668
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002669void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002670{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002671 struct selinux_audit_rule *rule = vrule;
2672
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002673 if (rule) {
2674 context_destroy(&rule->au_ctxt);
2675 kfree(rule);
2676 }
2677}
2678
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002679int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002680{
2681 struct selinux_audit_rule *tmprule;
2682 struct role_datum *roledatum;
2683 struct type_datum *typedatum;
2684 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002685 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002686 int rc = 0;
2687
2688 *rule = NULL;
2689
2690 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07002691 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002692
2693 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002694 case AUDIT_SUBJ_USER:
2695 case AUDIT_SUBJ_ROLE:
2696 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002697 case AUDIT_OBJ_USER:
2698 case AUDIT_OBJ_ROLE:
2699 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002700 /* only 'equals' and 'not equals' fit user, role, and type */
Al Viro5af75d82008-12-16 05:59:26 -05002701 if (op != Audit_equal && op != Audit_not_equal)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002702 return -EINVAL;
2703 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002704 case AUDIT_SUBJ_SEN:
2705 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002706 case AUDIT_OBJ_LEV_LOW:
2707 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002708 /* we do not allow a range, indicated by the presense of '-' */
2709 if (strchr(rulestr, '-'))
2710 return -EINVAL;
2711 break;
2712 default:
2713 /* only the above fields are valid */
2714 return -EINVAL;
2715 }
2716
2717 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
2718 if (!tmprule)
2719 return -ENOMEM;
2720
2721 context_init(&tmprule->au_ctxt);
2722
James Morris0804d112008-06-06 18:40:29 +10002723 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002724
2725 tmprule->au_seqno = latest_granting;
2726
2727 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002728 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002729 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002730 userdatum = hashtab_search(policydb.p_users.table, rulestr);
2731 if (!userdatum)
2732 rc = -EINVAL;
2733 else
2734 tmprule->au_ctxt.user = userdatum->value;
2735 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002736 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002737 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002738 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
2739 if (!roledatum)
2740 rc = -EINVAL;
2741 else
2742 tmprule->au_ctxt.role = roledatum->value;
2743 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002744 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002745 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002746 typedatum = hashtab_search(policydb.p_types.table, rulestr);
2747 if (!typedatum)
2748 rc = -EINVAL;
2749 else
2750 tmprule->au_ctxt.type = typedatum->value;
2751 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002752 case AUDIT_SUBJ_SEN:
2753 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002754 case AUDIT_OBJ_LEV_LOW:
2755 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002756 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
2757 break;
2758 }
2759
James Morris0804d112008-06-06 18:40:29 +10002760 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002761
2762 if (rc) {
2763 selinux_audit_rule_free(tmprule);
2764 tmprule = NULL;
2765 }
2766
2767 *rule = tmprule;
2768
2769 return rc;
2770}
2771
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002772/* Check to see if the rule contains any selinux fields */
2773int selinux_audit_rule_known(struct audit_krule *rule)
2774{
2775 int i;
2776
2777 for (i = 0; i < rule->field_count; i++) {
2778 struct audit_field *f = &rule->fields[i];
2779 switch (f->type) {
2780 case AUDIT_SUBJ_USER:
2781 case AUDIT_SUBJ_ROLE:
2782 case AUDIT_SUBJ_TYPE:
2783 case AUDIT_SUBJ_SEN:
2784 case AUDIT_SUBJ_CLR:
2785 case AUDIT_OBJ_USER:
2786 case AUDIT_OBJ_ROLE:
2787 case AUDIT_OBJ_TYPE:
2788 case AUDIT_OBJ_LEV_LOW:
2789 case AUDIT_OBJ_LEV_HIGH:
2790 return 1;
2791 }
2792 }
2793
2794 return 0;
2795}
2796
2797int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04002798 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002799{
2800 struct context *ctxt;
2801 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002802 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002803 int match = 0;
2804
2805 if (!rule) {
2806 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002807 "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002808 return -ENOENT;
2809 }
2810
James Morris0804d112008-06-06 18:40:29 +10002811 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002812
2813 if (rule->au_seqno < latest_granting) {
2814 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002815 "selinux_audit_rule_match: stale rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002816 match = -ESTALE;
2817 goto out;
2818 }
2819
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07002820 ctxt = sidtab_search(&sidtab, sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002821 if (!ctxt) {
2822 audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Eric Paris5d55a342008-04-18 17:38:33 -04002823 "selinux_audit_rule_match: unrecognized SID %d\n",
2824 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002825 match = -ENOENT;
2826 goto out;
2827 }
2828
2829 /* a field/op pair that is not caught here will simply fall through
2830 without a match */
2831 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002832 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002833 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002834 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002835 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002836 match = (ctxt->user == rule->au_ctxt.user);
2837 break;
Al Viro5af75d82008-12-16 05:59:26 -05002838 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002839 match = (ctxt->user != rule->au_ctxt.user);
2840 break;
2841 }
2842 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002843 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002844 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002845 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002846 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002847 match = (ctxt->role == rule->au_ctxt.role);
2848 break;
Al Viro5af75d82008-12-16 05:59:26 -05002849 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002850 match = (ctxt->role != rule->au_ctxt.role);
2851 break;
2852 }
2853 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002854 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002855 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002856 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002857 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002858 match = (ctxt->type == rule->au_ctxt.type);
2859 break;
Al Viro5af75d82008-12-16 05:59:26 -05002860 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002861 match = (ctxt->type != rule->au_ctxt.type);
2862 break;
2863 }
2864 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05002865 case AUDIT_SUBJ_SEN:
2866 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05002867 case AUDIT_OBJ_LEV_LOW:
2868 case AUDIT_OBJ_LEV_HIGH:
2869 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04002870 field == AUDIT_OBJ_LEV_LOW) ?
2871 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002872 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05002873 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002874 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002875 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002876 break;
Al Viro5af75d82008-12-16 05:59:26 -05002877 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002878 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002879 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002880 break;
Al Viro5af75d82008-12-16 05:59:26 -05002881 case Audit_lt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002882 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002883 level) &&
2884 !mls_level_eq(&rule->au_ctxt.range.level[0],
2885 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002886 break;
Al Viro5af75d82008-12-16 05:59:26 -05002887 case Audit_le:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002888 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04002889 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002890 break;
Al Viro5af75d82008-12-16 05:59:26 -05002891 case Audit_gt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002892 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002893 &rule->au_ctxt.range.level[0]) &&
2894 !mls_level_eq(level,
2895 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002896 break;
Al Viro5af75d82008-12-16 05:59:26 -05002897 case Audit_ge:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002898 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04002899 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002900 break;
2901 }
2902 }
2903
2904out:
James Morris0804d112008-06-06 18:40:29 +10002905 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002906 return match;
2907}
2908
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02002909static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002910
2911static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid,
Eric Parisf5269712008-05-14 11:27:45 -04002912 u16 class, u32 perms, u32 *retained)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002913{
2914 int err = 0;
2915
2916 if (event == AVC_CALLBACK_RESET && aurule_callback)
2917 err = aurule_callback();
2918 return err;
2919}
2920
2921static int __init aurule_init(void)
2922{
2923 int err;
2924
2925 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET,
Eric Paris5d55a342008-04-18 17:38:33 -04002926 SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002927 if (err)
2928 panic("avc_add_callback() failed, error %d\n", err);
2929
2930 return err;
2931}
2932__initcall(aurule_init);
2933
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002934#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002935/**
Paul Moore5778eab2007-02-28 15:14:22 -05002936 * security_netlbl_cache_add - Add an entry to the NetLabel cache
2937 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002938 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002939 *
2940 * Description:
2941 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05002942 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
2943 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002944 *
2945 */
Paul Moore5778eab2007-02-28 15:14:22 -05002946static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002947 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002948{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002949 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002950
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002951 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
2952 if (sid_cache == NULL)
2953 return;
Paul Moore5778eab2007-02-28 15:14:22 -05002954 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002955 if (secattr->cache == NULL) {
2956 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05002957 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02002958 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002959
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002960 *sid_cache = sid;
2961 secattr->cache->free = kfree;
2962 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05002963 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002964}
2965
2966/**
Paul Moore5778eab2007-02-28 15:14:22 -05002967 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002968 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002969 * @sid: the SELinux SID
2970 *
2971 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05002972 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002973 * SELinux SID. If the @secattr field does not contain a full SELinux
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002974 * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the
2975 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
2976 * allow the @secattr to be used by NetLabel to cache the secattr to SID
2977 * conversion for future lookups. Returns zero on success, negative values on
2978 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002979 *
2980 */
Paul Moore5778eab2007-02-28 15:14:22 -05002981int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05002982 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002983{
2984 int rc = -EIDRM;
2985 struct context *ctx;
2986 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05002987
2988 if (!ss_initialized) {
2989 *sid = SECSID_NULL;
2990 return 0;
2991 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002992
James Morris0804d112008-06-06 18:40:29 +10002993 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002994
Paul Moore701a90b2006-11-17 17:38:46 -05002995 if (secattr->flags & NETLBL_SECATTR_CACHE) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05002996 *sid = *(u32 *)secattr->cache->data;
2997 rc = 0;
Paul Moore16efd452008-01-29 08:37:59 -05002998 } else if (secattr->flags & NETLBL_SECATTR_SECID) {
2999 *sid = secattr->attr.secid;
3000 rc = 0;
Paul Moore701a90b2006-11-17 17:38:46 -05003001 } else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003002 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003003 if (ctx == NULL)
3004 goto netlbl_secattr_to_sid_return;
3005
Paul Moore81990fb2008-10-03 10:51:15 -04003006 context_init(&ctx_new);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003007 ctx_new.user = ctx->user;
3008 ctx_new.role = ctx->role;
3009 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05003010 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05003011 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Moore02752762006-11-29 13:18:18 -05003012 if (ebitmap_netlbl_import(&ctx_new.range.level[0].cat,
Paul Moore16efd452008-01-29 08:37:59 -05003013 secattr->attr.mls.cat) != 0)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003014 goto netlbl_secattr_to_sid_return;
Paul Moore81990fb2008-10-03 10:51:15 -04003015 memcpy(&ctx_new.range.level[1].cat,
3016 &ctx_new.range.level[0].cat,
3017 sizeof(ctx_new.range.level[0].cat));
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003018 }
3019 if (mls_context_isvalid(&policydb, &ctx_new) != 1)
3020 goto netlbl_secattr_to_sid_return_cleanup;
3021
3022 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
3023 if (rc != 0)
3024 goto netlbl_secattr_to_sid_return_cleanup;
3025
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003026 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05003027
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003028 ebitmap_destroy(&ctx_new.range.level[0].cat);
3029 } else {
paul.moore@hp.com388b2402006-10-05 18:28:24 -04003030 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003031 rc = 0;
3032 }
3033
3034netlbl_secattr_to_sid_return:
James Morris0804d112008-06-06 18:40:29 +10003035 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003036 return rc;
3037netlbl_secattr_to_sid_return_cleanup:
3038 ebitmap_destroy(&ctx_new.range.level[0].cat);
3039 goto netlbl_secattr_to_sid_return;
3040}
3041
3042/**
Paul Moore5778eab2007-02-28 15:14:22 -05003043 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3044 * @sid: the SELinux SID
3045 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003046 *
3047 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05003048 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3049 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003050 *
3051 */
Paul Moore5778eab2007-02-28 15:14:22 -05003052int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003053{
Paul Moore99d854d2008-10-10 10:16:30 -04003054 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003055 struct context *ctx;
3056
3057 if (!ss_initialized)
3058 return 0;
3059
James Morris0804d112008-06-06 18:40:29 +10003060 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003061 ctx = sidtab_search(&sidtab, sid);
Paul Moore99d854d2008-10-10 10:16:30 -04003062 if (ctx == NULL) {
3063 rc = -ENOENT;
Paul Moore5778eab2007-02-28 15:14:22 -05003064 goto netlbl_sid_to_secattr_failure;
Paul Moore99d854d2008-10-10 10:16:30 -04003065 }
Paul Moore5778eab2007-02-28 15:14:22 -05003066 secattr->domain = kstrdup(policydb.p_type_val_to_name[ctx->type - 1],
3067 GFP_ATOMIC);
Paul Moore99d854d2008-10-10 10:16:30 -04003068 if (secattr->domain == NULL) {
3069 rc = -ENOMEM;
3070 goto netlbl_sid_to_secattr_failure;
3071 }
Paul Moore8d758992008-10-10 10:16:33 -04003072 secattr->attr.secid = sid;
3073 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
Paul Moore5778eab2007-02-28 15:14:22 -05003074 mls_export_netlbl_lvl(ctx, secattr);
3075 rc = mls_export_netlbl_cat(ctx, secattr);
Paul Moorebf0edf32006-10-11 19:10:48 -04003076 if (rc != 0)
Paul Moore5778eab2007-02-28 15:14:22 -05003077 goto netlbl_sid_to_secattr_failure;
James Morris0804d112008-06-06 18:40:29 +10003078 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003079
Paul Moore5778eab2007-02-28 15:14:22 -05003080 return 0;
Paul Moore9f2ad662006-11-17 17:38:53 -05003081
Paul Moore5778eab2007-02-28 15:14:22 -05003082netlbl_sid_to_secattr_failure:
James Morris0804d112008-06-06 18:40:29 +10003083 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08003084 return rc;
3085}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003086#endif /* CONFIG_NETLABEL */