blob: afcb4199c70319e18c679f0235a9b944da8ab669 [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 *
Paul Moore82c21bf2011-08-01 11:10:33 +000016 * Updated: Hewlett-Packard <paul@paul-moore.com>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070017 *
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>
Eric Paris6371dcd2010-07-29 23:02:34 -040053#include <linux/flex_array.h>
Stephen Rothwellf0d3d982010-10-20 16:08:00 +110054#include <linux/vmalloc.h>
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070055#include <net/netlabel.h>
Ingo Molnarbb003072006-03-22 00:09:14 -080056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "flask.h"
58#include "avc.h"
59#include "avc_ss.h"
60#include "security.h"
61#include "context.h"
62#include "policydb.h"
63#include "sidtab.h"
64#include "services.h"
65#include "conditional.h"
66#include "mls.h"
Venkat Yekkirala7420ed22006-08-04 23:17:57 -070067#include "objsec.h"
Paul Moorec60475b2007-02-28 15:14:23 -050068#include "netlabel.h"
Paul Moore3de4bab2006-11-17 17:38:54 -050069#include "xfrm.h"
Paul Moore02752762006-11-29 13:18:18 -050070#include "ebitmap.h"
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +020071#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jeff Vander Stoep2c434f62020-01-22 11:19:58 +010073int selinux_android_netlink_route;
Paul Moore3bb56b22008-01-29 08:38:19 -050074int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050075int selinux_policycap_openperm;
Chris PeBenito2be4d742013-05-03 09:05:39 -040076int selinux_policycap_alwaysnetwork;
Paul Moore3bb56b22008-01-29 08:38:19 -050077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static DEFINE_RWLOCK(policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static struct sidtab sidtab;
81struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040082int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84/*
85 * The largest sequence number that has been used when
86 * providing an access decision to the access vector cache.
87 * The sequence number only changes when a policy change
88 * occurs.
89 */
Eric Paris5d55a342008-04-18 17:38:33 -040090static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92/* Forward declaration. */
93static int context_struct_to_string(struct context *context, char **scontext,
94 u32 *scontext_len);
95
Stephen Smalley19439d02010-01-14 17:28:10 -050096static void context_struct_compute_av(struct context *scontext,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -040097 struct context *tcontext,
98 u16 tclass,
99 struct av_decision *avd,
100 struct extended_perms *xperms);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400101
102struct selinux_mapping {
103 u16 value; /* policy value */
104 unsigned num_perms;
105 u32 perms[sizeof(u32) * 8];
106};
107
108static struct selinux_mapping *current_mapping;
109static u16 current_mapping_size;
110
111static int selinux_set_mapping(struct policydb *pol,
112 struct security_class_mapping *map,
113 struct selinux_mapping **out_map_p,
114 u16 *out_map_size)
115{
116 struct selinux_mapping *out_map = NULL;
117 size_t size = sizeof(struct selinux_mapping);
118 u16 i, j;
119 unsigned k;
120 bool print_unknown_handle = false;
121
122 /* Find number of classes in the input mapping */
123 if (!map)
124 return -EINVAL;
125 i = 0;
126 while (map[i].name)
127 i++;
128
129 /* Allocate space for the class records, plus one for class zero */
130 out_map = kcalloc(++i, size, GFP_ATOMIC);
131 if (!out_map)
132 return -ENOMEM;
133
134 /* Store the raw class and permission values */
135 j = 0;
136 while (map[j].name) {
137 struct security_class_mapping *p_in = map + (j++);
138 struct selinux_mapping *p_out = out_map + j;
139
140 /* An empty class string skips ahead */
141 if (!strcmp(p_in->name, "")) {
142 p_out->num_perms = 0;
143 continue;
144 }
145
146 p_out->value = string_to_security_class(pol, p_in->name);
147 if (!p_out->value) {
148 printk(KERN_INFO
149 "SELinux: Class %s not defined in policy.\n",
150 p_in->name);
151 if (pol->reject_unknown)
152 goto err;
153 p_out->num_perms = 0;
154 print_unknown_handle = true;
155 continue;
156 }
157
158 k = 0;
Matthias Kaehlcke00972ac2017-03-16 15:26:52 -0700159 while (p_in->perms[k]) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400160 /* An empty permission string skips ahead */
161 if (!*p_in->perms[k]) {
162 k++;
163 continue;
164 }
165 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
166 p_in->perms[k]);
167 if (!p_out->perms[k]) {
168 printk(KERN_INFO
169 "SELinux: Permission %s in class %s not defined in policy.\n",
170 p_in->perms[k], p_in->name);
171 if (pol->reject_unknown)
172 goto err;
173 print_unknown_handle = true;
174 }
175
176 k++;
177 }
178 p_out->num_perms = k;
179 }
180
181 if (print_unknown_handle)
182 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
183 pol->allow_unknown ? "allowed" : "denied");
184
185 *out_map_p = out_map;
186 *out_map_size = i;
187 return 0;
188err:
189 kfree(out_map);
190 return -EINVAL;
191}
192
193/*
194 * Get real, policy values from mapped values
195 */
196
197static u16 unmap_class(u16 tclass)
198{
199 if (tclass < current_mapping_size)
200 return current_mapping[tclass].value;
201
202 return tclass;
203}
204
Harry Ciao6f5317e2011-03-02 13:32:33 +0800205/*
206 * Get kernel value for class from its policy value
207 */
208static u16 map_class(u16 pol_value)
209{
210 u16 i;
211
212 for (i = 1; i < current_mapping_size; i++) {
213 if (current_mapping[i].value == pol_value)
214 return i;
215 }
216
Stephen Smalley85cd6da2011-03-25 10:13:43 -0400217 return SECCLASS_NULL;
Harry Ciao6f5317e2011-03-02 13:32:33 +0800218}
219
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400220static void map_decision(u16 tclass, struct av_decision *avd,
221 int allow_unknown)
222{
223 if (tclass < current_mapping_size) {
224 unsigned i, n = current_mapping[tclass].num_perms;
225 u32 result;
226
227 for (i = 0, result = 0; i < n; i++) {
228 if (avd->allowed & current_mapping[tclass].perms[i])
229 result |= 1<<i;
230 if (allow_unknown && !current_mapping[tclass].perms[i])
231 result |= 1<<i;
232 }
233 avd->allowed = result;
234
235 for (i = 0, result = 0; i < n; i++)
236 if (avd->auditallow & current_mapping[tclass].perms[i])
237 result |= 1<<i;
238 avd->auditallow = result;
239
240 for (i = 0, result = 0; i < n; i++) {
241 if (avd->auditdeny & current_mapping[tclass].perms[i])
242 result |= 1<<i;
243 if (!allow_unknown && !current_mapping[tclass].perms[i])
244 result |= 1<<i;
245 }
Eric Paris0bce9522009-11-23 16:47:23 -0500246 /*
247 * In case the kernel has a bug and requests a permission
248 * between num_perms and the maximum permission number, we
249 * should audit that denial
250 */
251 for (; i < (sizeof(u32)*8); i++)
252 result |= 1<<i;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400253 avd->auditdeny = result;
254 }
255}
256
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100257int security_mls_enabled(void)
258{
259 return policydb.mls_enabled;
260}
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/*
263 * Return the boolean value of a constraint expression
264 * when it is applied to the specified source and target
265 * security contexts.
266 *
267 * xcontext is a special beast... It is used by the validatetrans rules
268 * only. For these rules, scontext is the context before the transition,
269 * tcontext is the context after the transition, and xcontext is the context
270 * of the process performing the transition. All other callers of
271 * constraint_expr_eval should pass in NULL for xcontext.
272 */
273static int constraint_expr_eval(struct context *scontext,
274 struct context *tcontext,
275 struct context *xcontext,
276 struct constraint_expr *cexpr)
277{
278 u32 val1, val2;
279 struct context *c;
280 struct role_datum *r1, *r2;
281 struct mls_level *l1, *l2;
282 struct constraint_expr *e;
283 int s[CEXPR_MAXDEPTH];
284 int sp = -1;
285
286 for (e = cexpr; e; e = e->next) {
287 switch (e->expr_type) {
288 case CEXPR_NOT:
289 BUG_ON(sp < 0);
290 s[sp] = !s[sp];
291 break;
292 case CEXPR_AND:
293 BUG_ON(sp < 1);
294 sp--;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800295 s[sp] &= s[sp + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 break;
297 case CEXPR_OR:
298 BUG_ON(sp < 1);
299 sp--;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800300 s[sp] |= s[sp + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 break;
302 case CEXPR_ATTR:
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800303 if (sp == (CEXPR_MAXDEPTH - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305 switch (e->attr) {
306 case CEXPR_USER:
307 val1 = scontext->user;
308 val2 = tcontext->user;
309 break;
310 case CEXPR_TYPE:
311 val1 = scontext->type;
312 val2 = tcontext->type;
313 break;
314 case CEXPR_ROLE:
315 val1 = scontext->role;
316 val2 = tcontext->role;
317 r1 = policydb.role_val_to_struct[val1 - 1];
318 r2 = policydb.role_val_to_struct[val2 - 1];
319 switch (e->op) {
320 case CEXPR_DOM:
321 s[++sp] = ebitmap_get_bit(&r1->dominates,
322 val2 - 1);
323 continue;
324 case CEXPR_DOMBY:
325 s[++sp] = ebitmap_get_bit(&r2->dominates,
326 val1 - 1);
327 continue;
328 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400329 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
330 val2 - 1) &&
331 !ebitmap_get_bit(&r2->dominates,
332 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 continue;
334 default:
335 break;
336 }
337 break;
338 case CEXPR_L1L2:
339 l1 = &(scontext->range.level[0]);
340 l2 = &(tcontext->range.level[0]);
341 goto mls_ops;
342 case CEXPR_L1H2:
343 l1 = &(scontext->range.level[0]);
344 l2 = &(tcontext->range.level[1]);
345 goto mls_ops;
346 case CEXPR_H1L2:
347 l1 = &(scontext->range.level[1]);
348 l2 = &(tcontext->range.level[0]);
349 goto mls_ops;
350 case CEXPR_H1H2:
351 l1 = &(scontext->range.level[1]);
352 l2 = &(tcontext->range.level[1]);
353 goto mls_ops;
354 case CEXPR_L1H1:
355 l1 = &(scontext->range.level[0]);
356 l2 = &(scontext->range.level[1]);
357 goto mls_ops;
358 case CEXPR_L2H2:
359 l1 = &(tcontext->range.level[0]);
360 l2 = &(tcontext->range.level[1]);
361 goto mls_ops;
362mls_ops:
363 switch (e->op) {
364 case CEXPR_EQ:
365 s[++sp] = mls_level_eq(l1, l2);
366 continue;
367 case CEXPR_NEQ:
368 s[++sp] = !mls_level_eq(l1, l2);
369 continue;
370 case CEXPR_DOM:
371 s[++sp] = mls_level_dom(l1, l2);
372 continue;
373 case CEXPR_DOMBY:
374 s[++sp] = mls_level_dom(l2, l1);
375 continue;
376 case CEXPR_INCOMP:
377 s[++sp] = mls_level_incomp(l2, l1);
378 continue;
379 default:
380 BUG();
381 return 0;
382 }
383 break;
384 default:
385 BUG();
386 return 0;
387 }
388
389 switch (e->op) {
390 case CEXPR_EQ:
391 s[++sp] = (val1 == val2);
392 break;
393 case CEXPR_NEQ:
394 s[++sp] = (val1 != val2);
395 break;
396 default:
397 BUG();
398 return 0;
399 }
400 break;
401 case CEXPR_NAMES:
402 if (sp == (CEXPR_MAXDEPTH-1))
403 return 0;
404 c = scontext;
405 if (e->attr & CEXPR_TARGET)
406 c = tcontext;
407 else if (e->attr & CEXPR_XTARGET) {
408 c = xcontext;
409 if (!c) {
410 BUG();
411 return 0;
412 }
413 }
414 if (e->attr & CEXPR_USER)
415 val1 = c->user;
416 else if (e->attr & CEXPR_ROLE)
417 val1 = c->role;
418 else if (e->attr & CEXPR_TYPE)
419 val1 = c->type;
420 else {
421 BUG();
422 return 0;
423 }
424
425 switch (e->op) {
426 case CEXPR_EQ:
427 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
428 break;
429 case CEXPR_NEQ:
430 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
431 break;
432 default:
433 BUG();
434 return 0;
435 }
436 break;
437 default:
438 BUG();
439 return 0;
440 }
441 }
442
443 BUG_ON(sp != 0);
444 return s[0];
445}
446
447/*
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900448 * security_dump_masked_av - dumps masked permissions during
449 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
450 */
451static int dump_masked_av_helper(void *k, void *d, void *args)
452{
453 struct perm_datum *pdatum = d;
454 char **permission_names = args;
455
456 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
457
458 permission_names[pdatum->value - 1] = (char *)k;
459
460 return 0;
461}
462
463static void security_dump_masked_av(struct context *scontext,
464 struct context *tcontext,
465 u16 tclass,
466 u32 permissions,
467 const char *reason)
468{
469 struct common_datum *common_dat;
470 struct class_datum *tclass_dat;
471 struct audit_buffer *ab;
472 char *tclass_name;
473 char *scontext_name = NULL;
474 char *tcontext_name = NULL;
475 char *permission_names[32];
James Morris2da5d312010-02-16 17:29:06 +1100476 int index;
477 u32 length;
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900478 bool need_comma = false;
479
480 if (!permissions)
481 return;
482
Eric Parisac76c052010-11-29 15:47:09 -0500483 tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1);
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900484 tclass_dat = policydb.class_val_to_struct[tclass - 1];
485 common_dat = tclass_dat->comdatum;
486
487 /* init permission_names */
488 if (common_dat &&
489 hashtab_map(common_dat->permissions.table,
490 dump_masked_av_helper, permission_names) < 0)
491 goto out;
492
493 if (hashtab_map(tclass_dat->permissions.table,
494 dump_masked_av_helper, permission_names) < 0)
495 goto out;
496
497 /* get scontext/tcontext in text form */
498 if (context_struct_to_string(scontext,
499 &scontext_name, &length) < 0)
500 goto out;
501
502 if (context_struct_to_string(tcontext,
503 &tcontext_name, &length) < 0)
504 goto out;
505
506 /* audit a message */
507 ab = audit_log_start(current->audit_context,
508 GFP_ATOMIC, AUDIT_SELINUX_ERR);
509 if (!ab)
510 goto out;
511
512 audit_log_format(ab, "op=security_compute_av reason=%s "
513 "scontext=%s tcontext=%s tclass=%s perms=",
514 reason, scontext_name, tcontext_name, tclass_name);
515
516 for (index = 0; index < 32; index++) {
517 u32 mask = (1 << index);
518
519 if ((mask & permissions) == 0)
520 continue;
521
522 audit_log_format(ab, "%s%s",
523 need_comma ? "," : "",
524 permission_names[index]
525 ? permission_names[index] : "????");
526 need_comma = true;
527 }
528 audit_log_end(ab);
529out:
530 /* release scontext/tcontext */
531 kfree(tcontext_name);
532 kfree(scontext_name);
533
534 return;
535}
536
537/*
KaiGai Koheid9250de2008-08-28 16:35:57 +0900538 * security_boundary_permission - drops violated permissions
539 * on boundary constraint.
540 */
541static void type_attribute_bounds_av(struct context *scontext,
542 struct context *tcontext,
543 u16 tclass,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900544 struct av_decision *avd)
545{
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900546 struct context lo_scontext;
Stephen Smalley7ea59202016-05-23 10:54:11 -0400547 struct context lo_tcontext, *tcontextp = tcontext;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900548 struct av_decision lo_avd;
Eric Paris23bdecb2010-11-29 15:47:09 -0500549 struct type_datum *source;
550 struct type_datum *target;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900551 u32 masked = 0;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900552
Eric Paris23bdecb2010-11-29 15:47:09 -0500553 source = flex_array_get_ptr(policydb.type_val_to_struct_array,
554 scontext->type - 1);
555 BUG_ON(!source);
556
Stephen Smalley7ea59202016-05-23 10:54:11 -0400557 if (!source->bounds)
558 return;
559
Eric Paris23bdecb2010-11-29 15:47:09 -0500560 target = flex_array_get_ptr(policydb.type_val_to_struct_array,
561 tcontext->type - 1);
562 BUG_ON(!target);
563
Stephen Smalley7ea59202016-05-23 10:54:11 -0400564 memset(&lo_avd, 0, sizeof(lo_avd));
KaiGai Koheid9250de2008-08-28 16:35:57 +0900565
Stephen Smalley7ea59202016-05-23 10:54:11 -0400566 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
567 lo_scontext.type = source->bounds;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900568
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900569 if (target->bounds) {
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900570 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
571 lo_tcontext.type = target->bounds;
Stephen Smalley7ea59202016-05-23 10:54:11 -0400572 tcontextp = &lo_tcontext;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900573 }
574
Stephen Smalley7ea59202016-05-23 10:54:11 -0400575 context_struct_compute_av(&lo_scontext,
576 tcontextp,
577 tclass,
578 &lo_avd,
579 NULL);
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900580
Stephen Smalley7ea59202016-05-23 10:54:11 -0400581 masked = ~lo_avd.allowed & avd->allowed;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900582
Stephen Smalley7ea59202016-05-23 10:54:11 -0400583 if (likely(!masked))
584 return; /* no masked permission */
KaiGai Koheid9250de2008-08-28 16:35:57 +0900585
Stephen Smalley7ea59202016-05-23 10:54:11 -0400586 /* mask violated permissions */
587 avd->allowed &= ~masked;
588
589 /* audit masked permissions */
590 security_dump_masked_av(scontext, tcontext,
591 tclass, masked, "bounds");
KaiGai Koheid9250de2008-08-28 16:35:57 +0900592}
593
594/*
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400595 * flag which drivers have permissions
596 * only looking for ioctl based extended permssions
597 */
598void services_compute_xperms_drivers(
599 struct extended_perms *xperms,
600 struct avtab_node *node)
601{
602 unsigned int i;
603
604 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
605 /* if one or more driver has all permissions allowed */
606 for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++)
607 xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i];
608 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
609 /* if allowing permissions within a driver */
610 security_xperm_set(xperms->drivers.p,
611 node->datum.u.xperms->driver);
612 }
613
614 /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
615 if (node->key.specified & AVTAB_XPERMS_ALLOWED)
616 xperms->len = 1;
617}
618
619/*
620 * Compute access vectors and extended permissions based on a context
621 * structure pair for the permissions in a particular class.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
Stephen Smalley19439d02010-01-14 17:28:10 -0500623static void context_struct_compute_av(struct context *scontext,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400624 struct context *tcontext,
625 u16 tclass,
626 struct av_decision *avd,
627 struct extended_perms *xperms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 struct constraint_node *constraint;
630 struct role_allow *ra;
631 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700632 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700634 struct ebitmap *sattr, *tattr;
635 struct ebitmap_node *snode, *tnode;
636 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 avd->allowed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 avd->auditallow = 0;
640 avd->auditdeny = 0xffffffff;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400641 if (xperms) {
642 memset(&xperms->drivers, 0, sizeof(xperms->drivers));
643 xperms->len = 0;
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400646 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
647 if (printk_ratelimit())
648 printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
Stephen Smalley19439d02010-01-14 17:28:10 -0500649 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400650 }
Eric Paris3f120702007-09-21 14:37:10 -0400651
652 tclass_datum = policydb.class_val_to_struct[tclass - 1];
653
654 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 * If a specific type enforcement rule was defined for
656 * this permission check, then use it.
657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 avkey.target_class = tclass;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400659 avkey.specified = AVTAB_AV | AVTAB_XPERMS;
Eric Paris6371dcd2010-07-29 23:02:34 -0400660 sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
661 BUG_ON(!sattr);
662 tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
663 BUG_ON(!tattr);
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900664 ebitmap_for_each_positive_bit(sattr, snode, i) {
665 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700666 avkey.source_type = i + 1;
667 avkey.target_type = j + 1;
668 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300669 node;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700670 node = avtab_search_node_next(node, avkey.specified)) {
671 if (node->key.specified == AVTAB_ALLOWED)
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400672 avd->allowed |= node->datum.u.data;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700673 else if (node->key.specified == AVTAB_AUDITALLOW)
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400674 avd->auditallow |= node->datum.u.data;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700675 else if (node->key.specified == AVTAB_AUDITDENY)
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400676 avd->auditdeny &= node->datum.u.data;
677 else if (xperms && (node->key.specified & AVTAB_XPERMS))
678 services_compute_xperms_drivers(xperms, node);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Stephen Smalley782ebb92005-09-03 15:55:16 -0700681 /* Check conditional av table for additional permissions */
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400682 cond_compute_av(&policydb.te_cond_avtab, &avkey,
683 avd, xperms);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700684
685 }
686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688 /*
689 * Remove any permissions prohibited by a constraint (this includes
690 * the MLS policy).
691 */
692 constraint = tclass_datum->constraints;
693 while (constraint) {
694 if ((constraint->permissions & (avd->allowed)) &&
695 !constraint_expr_eval(scontext, tcontext, NULL,
696 constraint->expr)) {
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900697 avd->allowed &= ~(constraint->permissions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699 constraint = constraint->next;
700 }
701
702 /*
703 * If checking process transition permission and the
704 * role is changing, then check the (current_role, new_role)
705 * pair.
706 */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400707 if (tclass == policydb.process_class &&
708 (avd->allowed & policydb.process_trans_perms) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 scontext->role != tcontext->role) {
710 for (ra = policydb.role_allow; ra; ra = ra->next) {
711 if (scontext->role == ra->role &&
712 tcontext->role == ra->new_role)
713 break;
714 }
715 if (!ra)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400716 avd->allowed &= ~policydb.process_trans_perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718
KaiGai Koheid9250de2008-08-28 16:35:57 +0900719 /*
720 * If the given source and target types have boundary
721 * constraint, lazy checks have to mask any violated
722 * permission and notice it to userspace via audit.
723 */
724 type_attribute_bounds_av(scontext, tcontext,
Stephen Smalley19439d02010-01-14 17:28:10 -0500725 tclass, avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
728static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400729 struct context *ncontext,
730 struct context *tcontext,
731 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 char *o = NULL, *n = NULL, *t = NULL;
734 u32 olen, nlen, tlen;
735
Eric Paris4b02b522010-11-23 11:40:08 -0500736 if (context_struct_to_string(ocontext, &o, &olen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -0500738 if (context_struct_to_string(ncontext, &n, &nlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -0500740 if (context_struct_to_string(tcontext, &t, &tlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100742 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Richard Guy Briggs4093a842014-09-18 20:47:48 -0400743 "op=security_validate_transition seresult=denied"
Eric Paris5d55a342008-04-18 17:38:33 -0400744 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
Eric Parisac76c052010-11-29 15:47:09 -0500745 o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746out:
747 kfree(o);
748 kfree(n);
749 kfree(t);
750
751 if (!selinux_enforcing)
752 return 0;
753 return -EPERM;
754}
755
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500756static int security_compute_validatetrans(u32 oldsid, u32 newsid, u32 tasksid,
757 u16 orig_tclass, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 struct context *ocontext;
760 struct context *ncontext;
761 struct context *tcontext;
762 struct class_datum *tclass_datum;
763 struct constraint_node *constraint;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400764 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 int rc = 0;
766
767 if (!ss_initialized)
768 return 0;
769
James Morris0804d112008-06-06 18:40:29 +1000770 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500772 if (!user)
773 tclass = unmap_class(orig_tclass);
774 else
775 tclass = orig_tclass;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!tclass || tclass > policydb.p_classes.nprim) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 rc = -EINVAL;
779 goto out;
780 }
781 tclass_datum = policydb.class_val_to_struct[tclass - 1];
782
783 ocontext = sidtab_search(&sidtab, oldsid);
784 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400785 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
786 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 rc = -EINVAL;
788 goto out;
789 }
790
791 ncontext = sidtab_search(&sidtab, newsid);
792 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400793 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
794 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 rc = -EINVAL;
796 goto out;
797 }
798
799 tcontext = sidtab_search(&sidtab, tasksid);
800 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400801 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
802 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 rc = -EINVAL;
804 goto out;
805 }
806
807 constraint = tclass_datum->validatetrans;
808 while (constraint) {
809 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400810 constraint->expr)) {
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500811 if (user)
812 rc = -EPERM;
813 else
814 rc = security_validtrans_handle_fail(ocontext,
815 ncontext,
816 tcontext,
817 tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto out;
819 }
820 constraint = constraint->next;
821 }
822
823out:
James Morris0804d112008-06-06 18:40:29 +1000824 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return rc;
826}
827
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500828int security_validate_transition_user(u32 oldsid, u32 newsid, u32 tasksid,
829 u16 tclass)
830{
831 return security_compute_validatetrans(oldsid, newsid, tasksid,
832 tclass, true);
833}
834
835int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
836 u16 orig_tclass)
837{
838 return security_compute_validatetrans(oldsid, newsid, tasksid,
839 orig_tclass, false);
840}
841
KaiGai Koheid9250de2008-08-28 16:35:57 +0900842/*
843 * security_bounded_transition - check whether the given
844 * transition is directed to bounded, or not.
845 * It returns 0, if @newsid is bounded by @oldsid.
846 * Otherwise, it returns error code.
847 *
848 * @oldsid : current security identifier
849 * @newsid : destinated security identifier
850 */
851int security_bounded_transition(u32 old_sid, u32 new_sid)
852{
853 struct context *old_context, *new_context;
854 struct type_datum *type;
855 int index;
Eric Paris4b02b522010-11-23 11:40:08 -0500856 int rc;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900857
Paul Moore5e6f51a2017-12-05 17:17:43 -0500858 if (!ss_initialized)
859 return 0;
860
KaiGai Koheid9250de2008-08-28 16:35:57 +0900861 read_lock(&policy_rwlock);
862
Eric Paris4b02b522010-11-23 11:40:08 -0500863 rc = -EINVAL;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900864 old_context = sidtab_search(&sidtab, old_sid);
865 if (!old_context) {
866 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
867 __func__, old_sid);
868 goto out;
869 }
870
Eric Paris4b02b522010-11-23 11:40:08 -0500871 rc = -EINVAL;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900872 new_context = sidtab_search(&sidtab, new_sid);
873 if (!new_context) {
874 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
875 __func__, new_sid);
876 goto out;
877 }
878
Eric Paris4b02b522010-11-23 11:40:08 -0500879 rc = 0;
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200880 /* type/domain unchanged */
Eric Paris4b02b522010-11-23 11:40:08 -0500881 if (old_context->type == new_context->type)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900882 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900883
884 index = new_context->type;
885 while (true) {
Eric Paris23bdecb2010-11-29 15:47:09 -0500886 type = flex_array_get_ptr(policydb.type_val_to_struct_array,
887 index - 1);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900888 BUG_ON(!type);
889
890 /* not bounded anymore */
Eric Paris4b02b522010-11-23 11:40:08 -0500891 rc = -EPERM;
892 if (!type->bounds)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900893 break;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900894
895 /* @newsid is bounded by @oldsid */
Eric Paris4b02b522010-11-23 11:40:08 -0500896 rc = 0;
897 if (type->bounds == old_context->type)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900898 break;
Eric Paris4b02b522010-11-23 11:40:08 -0500899
KaiGai Koheid9250de2008-08-28 16:35:57 +0900900 index = type->bounds;
901 }
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900902
903 if (rc) {
904 char *old_name = NULL;
905 char *new_name = NULL;
James Morris2da5d312010-02-16 17:29:06 +1100906 u32 length;
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900907
908 if (!context_struct_to_string(old_context,
909 &old_name, &length) &&
910 !context_struct_to_string(new_context,
911 &new_name, &length)) {
912 audit_log(current->audit_context,
913 GFP_ATOMIC, AUDIT_SELINUX_ERR,
914 "op=security_bounded_transition "
Richard Guy Briggs4093a842014-09-18 20:47:48 -0400915 "seresult=denied "
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900916 "oldcontext=%s newcontext=%s",
917 old_name, new_name);
918 }
919 kfree(new_name);
920 kfree(old_name);
921 }
KaiGai Koheid9250de2008-08-28 16:35:57 +0900922out:
923 read_unlock(&policy_rwlock);
924
925 return rc;
926}
927
Stephen Smalley19439d02010-01-14 17:28:10 -0500928static void avd_init(struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400929{
Stephen Smalley19439d02010-01-14 17:28:10 -0500930 avd->allowed = 0;
931 avd->auditallow = 0;
932 avd->auditdeny = 0xffffffff;
933 avd->seqno = latest_granting;
934 avd->flags = 0;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400935}
936
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400937void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
938 struct avtab_node *node)
939{
940 unsigned int i;
941
942 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
943 if (xpermd->driver != node->datum.u.xperms->driver)
944 return;
945 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
946 if (!security_xperm_test(node->datum.u.xperms->perms.p,
947 xpermd->driver))
948 return;
949 } else {
950 BUG();
951 }
952
953 if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
954 xpermd->used |= XPERMS_ALLOWED;
955 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
956 memset(xpermd->allowed->p, 0xff,
957 sizeof(xpermd->allowed->p));
958 }
959 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
960 for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
961 xpermd->allowed->p[i] |=
962 node->datum.u.xperms->perms.p[i];
963 }
964 } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
965 xpermd->used |= XPERMS_AUDITALLOW;
966 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
967 memset(xpermd->auditallow->p, 0xff,
968 sizeof(xpermd->auditallow->p));
969 }
970 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
971 for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
972 xpermd->auditallow->p[i] |=
973 node->datum.u.xperms->perms.p[i];
974 }
975 } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
976 xpermd->used |= XPERMS_DONTAUDIT;
977 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
978 memset(xpermd->dontaudit->p, 0xff,
979 sizeof(xpermd->dontaudit->p));
980 }
981 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
982 for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
983 xpermd->dontaudit->p[i] |=
984 node->datum.u.xperms->perms.p[i];
985 }
986 } else {
987 BUG();
988 }
989}
990
991void security_compute_xperms_decision(u32 ssid,
992 u32 tsid,
993 u16 orig_tclass,
994 u8 driver,
995 struct extended_perms_decision *xpermd)
996{
997 u16 tclass;
998 struct context *scontext, *tcontext;
999 struct avtab_key avkey;
1000 struct avtab_node *node;
1001 struct ebitmap *sattr, *tattr;
1002 struct ebitmap_node *snode, *tnode;
1003 unsigned int i, j;
1004
1005 xpermd->driver = driver;
1006 xpermd->used = 0;
1007 memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
1008 memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
1009 memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
1010
1011 read_lock(&policy_rwlock);
1012 if (!ss_initialized)
1013 goto allow;
1014
1015 scontext = sidtab_search(&sidtab, ssid);
1016 if (!scontext) {
1017 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1018 __func__, ssid);
1019 goto out;
1020 }
1021
1022 tcontext = sidtab_search(&sidtab, tsid);
1023 if (!tcontext) {
1024 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1025 __func__, tsid);
1026 goto out;
1027 }
1028
1029 tclass = unmap_class(orig_tclass);
1030 if (unlikely(orig_tclass && !tclass)) {
1031 if (policydb.allow_unknown)
1032 goto allow;
1033 goto out;
1034 }
1035
1036
1037 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
1038 pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
1039 goto out;
1040 }
1041
1042 avkey.target_class = tclass;
1043 avkey.specified = AVTAB_XPERMS;
1044 sattr = flex_array_get(policydb.type_attr_map_array,
1045 scontext->type - 1);
1046 BUG_ON(!sattr);
1047 tattr = flex_array_get(policydb.type_attr_map_array,
1048 tcontext->type - 1);
1049 BUG_ON(!tattr);
1050 ebitmap_for_each_positive_bit(sattr, snode, i) {
1051 ebitmap_for_each_positive_bit(tattr, tnode, j) {
1052 avkey.source_type = i + 1;
1053 avkey.target_type = j + 1;
1054 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
1055 node;
1056 node = avtab_search_node_next(node, avkey.specified))
1057 services_compute_xperms_decision(xpermd, node);
1058
1059 cond_compute_xperms(&policydb.te_cond_avtab,
1060 &avkey, xpermd);
1061 }
1062 }
1063out:
1064 read_unlock(&policy_rwlock);
1065 return;
1066allow:
1067 memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
1068 goto out;
1069}
Stephen Smalley19439d02010-01-14 17:28:10 -05001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071/**
1072 * security_compute_av - Compute access vector decisions.
1073 * @ssid: source security identifier
1074 * @tsid: target security identifier
1075 * @tclass: target security class
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * @avd: access vector decisions
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001077 * @xperms: extended permissions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 *
1079 * Compute a set of access vector decisions based on the
1080 * SID pair (@ssid, @tsid) for the permissions in @tclass.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Stephen Smalley19439d02010-01-14 17:28:10 -05001082void security_compute_av(u32 ssid,
1083 u32 tsid,
1084 u16 orig_tclass,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001085 struct av_decision *avd,
1086 struct extended_perms *xperms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001088 u16 tclass;
Stephen Smalley19439d02010-01-14 17:28:10 -05001089 struct context *scontext = NULL, *tcontext = NULL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001090
Stephen Smalleyb7f30082009-10-19 10:08:50 -04001091 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -05001092 avd_init(avd);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001093 xperms->len = 0;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001094 if (!ss_initialized)
1095 goto allow;
1096
Stephen Smalley19439d02010-01-14 17:28:10 -05001097 scontext = sidtab_search(&sidtab, ssid);
1098 if (!scontext) {
1099 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1100 __func__, ssid);
1101 goto out;
1102 }
1103
1104 /* permissive domain? */
1105 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
1106 avd->flags |= AVD_FLAGS_PERMISSIVE;
1107
1108 tcontext = sidtab_search(&sidtab, tsid);
1109 if (!tcontext) {
1110 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1111 __func__, tsid);
1112 goto out;
1113 }
1114
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001115 tclass = unmap_class(orig_tclass);
1116 if (unlikely(orig_tclass && !tclass)) {
1117 if (policydb.allow_unknown)
1118 goto allow;
Stephen Smalleyb7f30082009-10-19 10:08:50 -04001119 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001120 }
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001121 context_struct_compute_av(scontext, tcontext, tclass, avd, xperms);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001122 map_decision(orig_tclass, avd, policydb.allow_unknown);
Stephen Smalleyb7f30082009-10-19 10:08:50 -04001123out:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001124 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -05001125 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001126allow:
1127 avd->allowed = 0xffffffff;
Stephen Smalleyb7f30082009-10-19 10:08:50 -04001128 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001129}
1130
Stephen Smalley19439d02010-01-14 17:28:10 -05001131void security_compute_av_user(u32 ssid,
1132 u32 tsid,
1133 u16 tclass,
1134 struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001135{
Stephen Smalley19439d02010-01-14 17:28:10 -05001136 struct context *scontext = NULL, *tcontext = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
James Morris0804d112008-06-06 18:40:29 +10001138 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -05001139 avd_init(avd);
1140 if (!ss_initialized)
1141 goto allow;
1142
1143 scontext = sidtab_search(&sidtab, ssid);
1144 if (!scontext) {
1145 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1146 __func__, ssid);
1147 goto out;
1148 }
1149
1150 /* permissive domain? */
1151 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
1152 avd->flags |= AVD_FLAGS_PERMISSIVE;
1153
1154 tcontext = sidtab_search(&sidtab, tsid);
1155 if (!tcontext) {
1156 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1157 __func__, tsid);
1158 goto out;
1159 }
1160
1161 if (unlikely(!tclass)) {
1162 if (policydb.allow_unknown)
1163 goto allow;
1164 goto out;
1165 }
1166
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001167 context_struct_compute_av(scontext, tcontext, tclass, avd, NULL);
Stephen Smalley19439d02010-01-14 17:28:10 -05001168 out:
James Morris0804d112008-06-06 18:40:29 +10001169 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -05001170 return;
1171allow:
1172 avd->allowed = 0xffffffff;
1173 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176/*
1177 * Write the security context string representation of
1178 * the context structure `context' into a dynamically
1179 * allocated string of the correct size. Set `*scontext'
1180 * to point to this string and set `*scontext_len' to
1181 * the length of the string.
1182 */
1183static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
1184{
1185 char *scontextp;
1186
Eric Parisd5630b92010-10-13 16:24:48 -04001187 if (scontext)
1188 *scontext = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 *scontext_len = 0;
1190
Stephen Smalley12b29f32008-05-07 13:03:20 -04001191 if (context->len) {
1192 *scontext_len = context->len;
Eric Parisbb7081a2012-04-04 13:46:36 -04001193 if (scontext) {
1194 *scontext = kstrdup(context->str, GFP_ATOMIC);
1195 if (!(*scontext))
1196 return -ENOMEM;
1197 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04001198 return 0;
1199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 /* Compute the size of the context. */
Eric Parisac76c052010-11-29 15:47:09 -05001202 *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
1203 *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
1204 *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 *scontext_len += mls_compute_context_len(context);
1206
Eric Parisd5630b92010-10-13 16:24:48 -04001207 if (!scontext)
1208 return 0;
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 /* Allocate space for the context; caller must free this space. */
1211 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -04001212 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 *scontext = scontextp;
1215
1216 /*
1217 * Copy the user name, role name and type name into the context.
1218 */
Rasmus Villemoes9529c782015-10-21 17:44:27 -04001219 scontextp += sprintf(scontextp, "%s:%s:%s",
Eric Parisac76c052010-11-29 15:47:09 -05001220 sym_name(&policydb, SYM_USERS, context->user - 1),
1221 sym_name(&policydb, SYM_ROLES, context->role - 1),
1222 sym_name(&policydb, SYM_TYPES, context->type - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 mls_sid_to_context(context, &scontextp);
1225
1226 *scontextp = 0;
1227
1228 return 0;
1229}
1230
1231#include "initial_sid_to_string.h"
1232
James Carterf0ee2e42007-04-04 10:11:29 -04001233const char *security_get_initial_sid_context(u32 sid)
1234{
1235 if (unlikely(sid > SECINITSID_NUM))
1236 return NULL;
1237 return initial_sid_to_string[sid];
1238}
1239
Stephen Smalley12b29f32008-05-07 13:03:20 -04001240static int security_sid_to_context_core(u32 sid, char **scontext,
1241 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct context *context;
1244 int rc = 0;
1245
Eric Parisd5630b92010-10-13 16:24:48 -04001246 if (scontext)
1247 *scontext = NULL;
Stephen Smalley4f4acf32007-02-26 12:02:34 -05001248 *scontext_len = 0;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 if (!ss_initialized) {
1251 if (sid <= SECINITSID_NUM) {
1252 char *scontextp;
1253
1254 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Parisd5630b92010-10-13 16:24:48 -04001255 if (!scontext)
1256 goto out;
Rasmus Villemoesaa736c32015-10-21 17:44:26 -04001257 scontextp = kmemdup(initial_sid_to_string[sid],
1258 *scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -07001259 if (!scontextp) {
1260 rc = -ENOMEM;
1261 goto out;
1262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 *scontext = scontextp;
1264 goto out;
1265 }
Eric Paris744ba352008-04-17 11:52:44 -04001266 printk(KERN_ERR "SELinux: %s: called before initial "
1267 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 rc = -EINVAL;
1269 goto out;
1270 }
James Morris0804d112008-06-06 18:40:29 +10001271 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001272 if (force)
1273 context = sidtab_search_force(&sidtab, sid);
1274 else
1275 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -04001277 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1278 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 rc = -EINVAL;
1280 goto out_unlock;
1281 }
1282 rc = context_struct_to_string(context, scontext, scontext_len);
1283out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001284 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285out:
1286 return rc;
1287
1288}
1289
Stephen Smalley12b29f32008-05-07 13:03:20 -04001290/**
1291 * security_sid_to_context - Obtain a context for a given SID.
1292 * @sid: security identifier, SID
1293 * @scontext: security context
1294 * @scontext_len: length in bytes
1295 *
1296 * Write the string representation of the context associated with @sid
1297 * into a dynamically allocated string of the correct size. Set @scontext
1298 * to point to this string and set @scontext_len to the length of the string.
1299 */
1300int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Stephen Smalley12b29f32008-05-07 13:03:20 -04001302 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
1303}
1304
1305int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
1306{
1307 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
1308}
1309
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001310/*
1311 * Caveat: Mutates scontext.
1312 */
Stephen Smalley12b29f32008-05-07 13:03:20 -04001313static int string_to_context_struct(struct policydb *pol,
1314 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001315 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001316 u32 scontext_len,
1317 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001318 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001319{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 struct role_datum *role;
1321 struct type_datum *typdatum;
1322 struct user_datum *usrdatum;
1323 char *scontextp, *p, oldc;
1324 int rc = 0;
1325
Stephen Smalley12b29f32008-05-07 13:03:20 -04001326 context_init(ctx);
1327
Stephen Smalley12b29f32008-05-07 13:03:20 -04001328 /* Parse the security context. */
1329
1330 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001331 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001332
1333 /* Extract the user. */
1334 p = scontextp;
1335 while (*p && *p != ':')
1336 p++;
1337
1338 if (*p == 0)
1339 goto out;
1340
1341 *p++ = 0;
1342
1343 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1344 if (!usrdatum)
1345 goto out;
1346
1347 ctx->user = usrdatum->value;
1348
1349 /* Extract role. */
1350 scontextp = p;
1351 while (*p && *p != ':')
1352 p++;
1353
1354 if (*p == 0)
1355 goto out;
1356
1357 *p++ = 0;
1358
1359 role = hashtab_search(pol->p_roles.table, scontextp);
1360 if (!role)
1361 goto out;
1362 ctx->role = role->value;
1363
1364 /* Extract type. */
1365 scontextp = p;
1366 while (*p && *p != ':')
1367 p++;
1368 oldc = *p;
1369 *p++ = 0;
1370
1371 typdatum = hashtab_search(pol->p_types.table, scontextp);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001372 if (!typdatum || typdatum->attribute)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001373 goto out;
1374
1375 ctx->type = typdatum->value;
1376
1377 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
1378 if (rc)
1379 goto out;
1380
Eric Paris4b02b522010-11-23 11:40:08 -05001381 rc = -EINVAL;
1382 if ((p - scontext) < scontext_len)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001383 goto out;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001384
1385 /* Check the validity of the new context. */
Eric Paris4b02b522010-11-23 11:40:08 -05001386 if (!policydb_context_isvalid(pol, ctx))
Stephen Smalley12b29f32008-05-07 13:03:20 -04001387 goto out;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001388 rc = 0;
1389out:
Eric Paris8e531af2008-09-03 11:49:47 -04001390 if (rc)
1391 context_destroy(ctx);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001392 return rc;
1393}
1394
1395static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
1396 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1397 int force)
1398{
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001399 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001400 struct context context;
1401 int rc = 0;
1402
Stephen Smalley2172fa72014-01-30 11:26:59 -05001403 /* An empty security context is never valid. */
1404 if (!scontext_len)
1405 return -EINVAL;
1406
Paul Moorefe1cb582017-11-28 18:51:12 -05001407 /* Copy the string to allow changes and ensure a NUL terminator */
1408 scontext2 = kmemdup_nul(scontext, scontext_len, gfp_flags);
1409 if (!scontext2)
1410 return -ENOMEM;
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (!ss_initialized) {
1413 int i;
1414
1415 for (i = 1; i < SECINITSID_NUM; i++) {
Paul Moorefe1cb582017-11-28 18:51:12 -05001416 if (!strcmp(initial_sid_to_string[i], scontext2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 *sid = i;
Paul Moorefe1cb582017-11-28 18:51:12 -05001418 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420 }
1421 *sid = SECINITSID_KERNEL;
Paul Moorefe1cb582017-11-28 18:51:12 -05001422 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 *sid = SECSID_NULL;
1425
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001426 if (force) {
1427 /* Save another copy for storing in uninterpreted form */
Eric Paris4b02b522010-11-23 11:40:08 -05001428 rc = -ENOMEM;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001429 str = kstrdup(scontext2, gfp_flags);
Eric Paris4b02b522010-11-23 11:40:08 -05001430 if (!str)
1431 goto out;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001432 }
1433
James Morris0804d112008-06-06 18:40:29 +10001434 read_lock(&policy_rwlock);
Eric Paris4b02b522010-11-23 11:40:08 -05001435 rc = string_to_context_struct(&policydb, &sidtab, scontext2,
1436 scontext_len, &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001437 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001438 context.str = str;
Sachin Groverc738c802018-05-25 14:01:39 +05301439 context.len = strlen(str) + 1;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001440 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001441 } else if (rc)
Eric Paris4b02b522010-11-23 11:40:08 -05001442 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Eric Paris8e531af2008-09-03 11:49:47 -04001444 context_destroy(&context);
Eric Paris4b02b522010-11-23 11:40:08 -05001445out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001446 read_unlock(&policy_rwlock);
Eric Paris4b02b522010-11-23 11:40:08 -05001447out:
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001448 kfree(scontext2);
1449 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return rc;
1451}
1452
James Morrisf5c1d5b2005-07-28 01:07:37 -07001453/**
1454 * security_context_to_sid - Obtain a SID for a given security context.
1455 * @scontext: security context
1456 * @scontext_len: length in bytes
1457 * @sid: security identifier, SID
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001458 * @gfp: context for the allocation
James Morrisf5c1d5b2005-07-28 01:07:37 -07001459 *
1460 * Obtains a SID associated with the security context that
1461 * has the string representation specified by @scontext.
1462 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1463 * memory is available, or 0 on success.
1464 */
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001465int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid,
1466 gfp_t gfp)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001467{
1468 return security_context_to_sid_core(scontext, scontext_len,
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001469 sid, SECSID_NULL, gfp, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001470}
1471
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001472int security_context_str_to_sid(const char *scontext, u32 *sid, gfp_t gfp)
1473{
1474 return security_context_to_sid(scontext, strlen(scontext), sid, gfp);
1475}
1476
James Morrisf5c1d5b2005-07-28 01:07:37 -07001477/**
1478 * security_context_to_sid_default - Obtain a SID for a given security context,
1479 * falling back to specified default if needed.
1480 *
1481 * @scontext: security context
1482 * @scontext_len: length in bytes
1483 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -07001484 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -07001485 *
1486 * Obtains a SID associated with the security context that
1487 * has the string representation specified by @scontext.
1488 * The default SID is passed to the MLS layer to be used to allow
1489 * kernel labeling of the MLS field if the MLS field is not present
1490 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -04001491 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -07001492 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1493 * memory is available, or 0 on success.
1494 */
David Howells7bf570d2008-04-29 20:52:51 +01001495int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1496 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001497{
1498 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001499 sid, def_sid, gfp_flags, 1);
1500}
1501
1502int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1503 u32 *sid)
1504{
1505 return security_context_to_sid_core(scontext, scontext_len,
1506 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509static int compute_sid_handle_invalid_context(
1510 struct context *scontext,
1511 struct context *tcontext,
1512 u16 tclass,
1513 struct context *newcontext)
1514{
1515 char *s = NULL, *t = NULL, *n = NULL;
1516 u32 slen, tlen, nlen;
1517
Eric Paris4b02b522010-11-23 11:40:08 -05001518 if (context_struct_to_string(scontext, &s, &slen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -05001520 if (context_struct_to_string(tcontext, &t, &tlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -05001522 if (context_struct_to_string(newcontext, &n, &nlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001524 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Richard Guy Briggs4093a842014-09-18 20:47:48 -04001525 "op=security_compute_sid invalid_context=%s"
1526 " scontext=%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 " tcontext=%s"
1528 " tclass=%s",
Eric Parisac76c052010-11-29 15:47:09 -05001529 n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530out:
1531 kfree(s);
1532 kfree(t);
1533 kfree(n);
1534 if (!selinux_enforcing)
1535 return 0;
1536 return -EACCES;
1537}
1538
Eric Paris652bb9b2011-02-01 11:05:40 -05001539static void filename_compute_type(struct policydb *p, struct context *newcontext,
Eric Paris26679912011-04-28 15:11:20 -04001540 u32 stype, u32 ttype, u16 tclass,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001541 const char *objname)
Eric Paris652bb9b2011-02-01 11:05:40 -05001542{
Eric Paris2463c26d2011-04-28 15:11:21 -04001543 struct filename_trans ft;
1544 struct filename_trans_datum *otype;
Eric Paris03a4c012011-04-28 15:11:21 -04001545
1546 /*
1547 * Most filename trans rules are going to live in specific directories
1548 * like /dev or /var/run. This bitmap will quickly skip rule searches
1549 * if the ttype does not contain any rules.
1550 */
1551 if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
1552 return;
1553
Eric Paris2463c26d2011-04-28 15:11:21 -04001554 ft.stype = stype;
1555 ft.ttype = ttype;
1556 ft.tclass = tclass;
1557 ft.name = objname;
1558
1559 otype = hashtab_search(p->filename_trans, &ft);
1560 if (otype)
1561 newcontext->type = otype->otype;
Eric Paris652bb9b2011-02-01 11:05:40 -05001562}
1563
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564static int security_compute_sid(u32 ssid,
1565 u32 tsid,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001566 u16 orig_tclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 u32 specified,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001568 const char *objname,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001569 u32 *out_sid,
1570 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Eric Parisaa893262012-03-20 14:35:12 -04001572 struct class_datum *cladatum = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1574 struct role_trans *roletr = NULL;
1575 struct avtab_key avkey;
1576 struct avtab_datum *avdatum;
1577 struct avtab_node *node;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001578 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int rc = 0;
Harry Ciao6f5317e2011-03-02 13:32:33 +08001580 bool sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (!ss_initialized) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001583 switch (orig_tclass) {
1584 case SECCLASS_PROCESS: /* kernel value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 *out_sid = ssid;
1586 break;
1587 default:
1588 *out_sid = tsid;
1589 break;
1590 }
1591 goto out;
1592 }
1593
Venkat Yekkirala851f8a62006-07-30 03:03:18 -07001594 context_init(&newcontext);
1595
James Morris0804d112008-06-06 18:40:29 +10001596 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Harry Ciao6f5317e2011-03-02 13:32:33 +08001598 if (kern) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001599 tclass = unmap_class(orig_tclass);
Harry Ciao6f5317e2011-03-02 13:32:33 +08001600 sock = security_is_socket_class(orig_tclass);
1601 } else {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001602 tclass = orig_tclass;
Harry Ciao6f5317e2011-03-02 13:32:33 +08001603 sock = security_is_socket_class(map_class(tclass));
1604 }
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 scontext = sidtab_search(&sidtab, ssid);
1607 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001608 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1609 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 rc = -EINVAL;
1611 goto out_unlock;
1612 }
1613 tcontext = sidtab_search(&sidtab, tsid);
1614 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001615 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1616 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 rc = -EINVAL;
1618 goto out_unlock;
1619 }
1620
Eric Parisaa893262012-03-20 14:35:12 -04001621 if (tclass && tclass <= policydb.p_classes.nprim)
1622 cladatum = policydb.class_val_to_struct[tclass - 1];
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 /* Set the user identity. */
1625 switch (specified) {
1626 case AVTAB_TRANSITION:
1627 case AVTAB_CHANGE:
Eric Parisaa893262012-03-20 14:35:12 -04001628 if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
1629 newcontext.user = tcontext->user;
1630 } else {
1631 /* notice this gets both DEFAULT_SOURCE and unset */
1632 /* Use the process user identity. */
1633 newcontext.user = scontext->user;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 break;
1636 case AVTAB_MEMBER:
1637 /* Use the related object owner. */
1638 newcontext.user = tcontext->user;
1639 break;
1640 }
1641
Eric Parisaa893262012-03-20 14:35:12 -04001642 /* Set the role to default values. */
1643 if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 newcontext.role = scontext->role;
Eric Parisaa893262012-03-20 14:35:12 -04001645 } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
1646 newcontext.role = tcontext->role;
1647 } else {
1648 if ((tclass == policydb.process_class) || (sock == true))
1649 newcontext.role = scontext->role;
1650 else
1651 newcontext.role = OBJECT_R_VAL;
1652 }
1653
1654 /* Set the type to default values. */
Eric Pariseed77952012-03-20 14:35:12 -04001655 if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 newcontext.type = scontext->type;
Eric Pariseed77952012-03-20 14:35:12 -04001657 } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 newcontext.type = tcontext->type;
Eric Pariseed77952012-03-20 14:35:12 -04001659 } else {
1660 if ((tclass == policydb.process_class) || (sock == true)) {
1661 /* Use the type of process. */
1662 newcontext.type = scontext->type;
1663 } else {
1664 /* Use the type of the related object. */
1665 newcontext.type = tcontext->type;
1666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668
1669 /* Look for a type transition/member/change rule. */
1670 avkey.source_type = scontext->type;
1671 avkey.target_type = tcontext->type;
1672 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001673 avkey.specified = specified;
1674 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001677 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001678 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03001679 for (; node; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001680 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 avdatum = &node->datum;
1682 break;
1683 }
1684 }
1685 }
1686
Stephen Smalley782ebb92005-09-03 15:55:16 -07001687 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /* Use the type from the type transition/member/change rule. */
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001689 newcontext.type = avdatum->u.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691
Eric Paris47426002011-04-28 15:11:20 -04001692 /* if we have a objname this is a file trans check so check those rules */
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001693 if (objname)
Eric Paris652bb9b2011-02-01 11:05:40 -05001694 filename_compute_type(&policydb, &newcontext, scontext->type,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001695 tcontext->type, tclass, objname);
Eric Paris652bb9b2011-02-01 11:05:40 -05001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /* Check for class-specific changes. */
Harry Ciao63a312c2011-03-25 13:51:58 +08001698 if (specified & AVTAB_TRANSITION) {
1699 /* Look for a role transition rule. */
1700 for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
1701 if ((roletr->role == scontext->role) &&
1702 (roletr->type == tcontext->type) &&
1703 (roletr->tclass == tclass)) {
1704 /* Use the role transition rule. */
1705 newcontext.role = roletr->new_role;
1706 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
1711 /* Set the MLS attributes.
1712 This is done last because it may allocate memory. */
Harry Ciao6f5317e2011-03-02 13:32:33 +08001713 rc = mls_compute_sid(scontext, tcontext, tclass, specified,
1714 &newcontext, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 if (rc)
1716 goto out_unlock;
1717
1718 /* Check the validity of the context. */
1719 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1720 rc = compute_sid_handle_invalid_context(scontext,
1721 tcontext,
1722 tclass,
1723 &newcontext);
1724 if (rc)
1725 goto out_unlock;
1726 }
1727 /* Obtain the sid for the context. */
1728 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1729out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001730 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 context_destroy(&newcontext);
1732out:
1733 return rc;
1734}
1735
1736/**
1737 * security_transition_sid - Compute the SID for a new subject/object.
1738 * @ssid: source security identifier
1739 * @tsid: target security identifier
1740 * @tclass: target security class
1741 * @out_sid: security identifier for new subject/object
1742 *
1743 * Compute a SID to use for labeling a new subject or object in the
1744 * class @tclass based on a SID pair (@ssid, @tsid).
1745 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1746 * if insufficient memory is available, or %0 if the new SID was
1747 * computed successfully.
1748 */
Eric Paris652bb9b2011-02-01 11:05:40 -05001749int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
1750 const struct qstr *qstr, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001752 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001753 qstr ? qstr->name : NULL, out_sid, true);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001754}
1755
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001756int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
1757 const char *objname, u32 *out_sid)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001758{
1759 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001760 objname, out_sid, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
1763/**
1764 * security_member_sid - Compute the SID for member selection.
1765 * @ssid: source security identifier
1766 * @tsid: target security identifier
1767 * @tclass: target security class
1768 * @out_sid: security identifier for selected member
1769 *
1770 * Compute a SID to use when selecting a member of a polyinstantiated
1771 * object of class @tclass based on a SID pair (@ssid, @tsid).
1772 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1773 * if insufficient memory is available, or %0 if the SID was
1774 * computed successfully.
1775 */
1776int security_member_sid(u32 ssid,
1777 u32 tsid,
1778 u16 tclass,
1779 u32 *out_sid)
1780{
Eric Paris652bb9b2011-02-01 11:05:40 -05001781 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
1782 out_sid, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
1785/**
1786 * security_change_sid - Compute the SID for object relabeling.
1787 * @ssid: source security identifier
1788 * @tsid: target security identifier
1789 * @tclass: target security class
1790 * @out_sid: security identifier for selected member
1791 *
1792 * Compute a SID to use for relabeling an object of class @tclass
1793 * based on a SID pair (@ssid, @tsid).
1794 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1795 * if insufficient memory is available, or %0 if the SID was
1796 * computed successfully.
1797 */
1798int security_change_sid(u32 ssid,
1799 u32 tsid,
1800 u16 tclass,
1801 u32 *out_sid)
1802{
Eric Paris652bb9b2011-02-01 11:05:40 -05001803 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
1804 out_sid, false);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/* Clone the SID into the new SID table. */
1808static int clone_sid(u32 sid,
1809 struct context *context,
1810 void *arg)
1811{
1812 struct sidtab *s = arg;
1813
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001814 if (sid > SECINITSID_NUM)
1815 return sidtab_insert(s, sid, context);
1816 else
1817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818}
1819
1820static inline int convert_context_handle_invalid_context(struct context *context)
1821{
Eric Paris4b02b522010-11-23 11:40:08 -05001822 char *s;
1823 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Eric Paris4b02b522010-11-23 11:40:08 -05001825 if (selinux_enforcing)
1826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Eric Paris4b02b522010-11-23 11:40:08 -05001828 if (!context_struct_to_string(context, &s, &len)) {
1829 printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
1830 kfree(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
Eric Paris4b02b522010-11-23 11:40:08 -05001832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
1835struct convert_context_args {
1836 struct policydb *oldp;
1837 struct policydb *newp;
1838};
1839
1840/*
1841 * Convert the values in the security context
1842 * structure `c' from the values specified
1843 * in the policy `p->oldp' to the values specified
1844 * in the policy `p->newp'. Verify that the
1845 * context is valid under the new policy.
1846 */
1847static int convert_context(u32 key,
1848 struct context *c,
1849 void *p)
1850{
1851 struct convert_context_args *args;
1852 struct context oldc;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001853 struct ocontext *oc;
1854 struct mls_range *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 struct role_datum *role;
1856 struct type_datum *typdatum;
1857 struct user_datum *usrdatum;
1858 char *s;
1859 u32 len;
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001860 int rc = 0;
1861
1862 if (key <= SECINITSID_NUM)
1863 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 args = p;
1866
Stephen Smalley12b29f32008-05-07 13:03:20 -04001867 if (c->str) {
1868 struct context ctx;
Eric Paris4b02b522010-11-23 11:40:08 -05001869
1870 rc = -ENOMEM;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001871 s = kstrdup(c->str, GFP_KERNEL);
Eric Paris4b02b522010-11-23 11:40:08 -05001872 if (!s)
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001873 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -05001874
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001875 rc = string_to_context_struct(args->newp, NULL, s,
1876 c->len, &ctx, SECSID_NULL);
1877 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001878 if (!rc) {
Eric Paris4b02b522010-11-23 11:40:08 -05001879 printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
Stephen Smalley12b29f32008-05-07 13:03:20 -04001880 c->str);
1881 /* Replace string with mapped representation. */
1882 kfree(c->str);
1883 memcpy(c, &ctx, sizeof(*c));
1884 goto out;
1885 } else if (rc == -EINVAL) {
1886 /* Retain string representation for later mapping. */
1887 rc = 0;
1888 goto out;
1889 } else {
1890 /* Other error condition, e.g. ENOMEM. */
Eric Paris4b02b522010-11-23 11:40:08 -05001891 printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
Stephen Smalley12b29f32008-05-07 13:03:20 -04001892 c->str, -rc);
1893 goto out;
1894 }
1895 }
1896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 rc = context_cpy(&oldc, c);
1898 if (rc)
1899 goto out;
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* Convert the user. */
Eric Paris4b02b522010-11-23 11:40:08 -05001902 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Parisac76c052010-11-29 15:47:09 -05001904 sym_name(args->oldp, SYM_USERS, c->user - 1));
Eric Paris5d55a342008-04-18 17:38:33 -04001905 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 c->user = usrdatum->value;
1908
1909 /* Convert the role. */
Eric Paris4b02b522010-11-23 11:40:08 -05001910 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 role = hashtab_search(args->newp->p_roles.table,
Eric Parisac76c052010-11-29 15:47:09 -05001912 sym_name(args->oldp, SYM_ROLES, c->role - 1));
Eric Paris5d55a342008-04-18 17:38:33 -04001913 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 c->role = role->value;
1916
1917 /* Convert the type. */
Eric Paris4b02b522010-11-23 11:40:08 -05001918 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 typdatum = hashtab_search(args->newp->p_types.table,
Eric Parisac76c052010-11-29 15:47:09 -05001920 sym_name(args->oldp, SYM_TYPES, c->type - 1));
Eric Paris5d55a342008-04-18 17:38:33 -04001921 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 c->type = typdatum->value;
1924
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001925 /* Convert the MLS fields if dealing with MLS policies */
1926 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
1927 rc = mls_convert_context(args->oldp, args->newp, c);
1928 if (rc)
1929 goto bad;
1930 } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
1931 /*
1932 * Switching between MLS and non-MLS policy:
1933 * free any storage used by the MLS fields in the
1934 * context for all existing entries in the sidtab.
1935 */
1936 mls_context_destroy(c);
1937 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
1938 /*
1939 * Switching between non-MLS and MLS policy:
1940 * ensure that the MLS fields of the context for all
1941 * existing entries in the sidtab are filled in with a
1942 * suitable default value, likely taken from one of the
1943 * initial SIDs.
1944 */
1945 oc = args->newp->ocontexts[OCON_ISID];
1946 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
1947 oc = oc->next;
Eric Paris4b02b522010-11-23 11:40:08 -05001948 rc = -EINVAL;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001949 if (!oc) {
1950 printk(KERN_ERR "SELinux: unable to look up"
1951 " the initial SIDs list\n");
1952 goto bad;
1953 }
1954 range = &oc->context[0].range;
1955 rc = mls_range_set(c, range);
1956 if (rc)
1957 goto bad;
1958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 /* Check the validity of the new context. */
1961 if (!policydb_context_isvalid(args->newp, c)) {
1962 rc = convert_context_handle_invalid_context(&oldc);
1963 if (rc)
1964 goto bad;
1965 }
1966
1967 context_destroy(&oldc);
Eric Paris4b02b522010-11-23 11:40:08 -05001968
Stephen Smalley12b29f32008-05-07 13:03:20 -04001969 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970out:
1971 return rc;
1972bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001973 /* Map old representation to string and save it. */
Eric Paris4b02b522010-11-23 11:40:08 -05001974 rc = context_struct_to_string(&oldc, &s, &len);
1975 if (rc)
1976 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001978 context_destroy(c);
1979 c->str = s;
1980 c->len = len;
Eric Paris4b02b522010-11-23 11:40:08 -05001981 printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
Stephen Smalley12b29f32008-05-07 13:03:20 -04001982 c->str);
1983 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 goto out;
1985}
1986
Paul Moore3bb56b22008-01-29 08:38:19 -05001987static void security_load_policycaps(void)
1988{
1989 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
1990 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05001991 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
1992 POLICYDB_CAPABILITY_OPENPERM);
Chris PeBenito2be4d742013-05-03 09:05:39 -04001993 selinux_policycap_alwaysnetwork = ebitmap_get_bit(&policydb.policycaps,
1994 POLICYDB_CAPABILITY_ALWAYSNETWORK);
Jeff Vander Stoep2c434f62020-01-22 11:19:58 +01001995
1996 selinux_android_netlink_route = policydb.android_netlink_route;
1997 selinux_nlmsg_init();
Paul Moore3bb56b22008-01-29 08:38:19 -05001998}
1999
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002000static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002/**
2003 * security_load_policy - Load a security policy configuration.
2004 * @data: binary policy data
2005 * @len: length of data in bytes
2006 *
2007 * Load a new set of security policy configuration data,
2008 * validate it and convert the SID table as necessary.
2009 * This function will flush the access vector cache after
2010 * loading the new policy.
2011 */
2012int security_load_policy(void *data, size_t len)
2013{
Tim Gardnerb5495b42013-11-14 15:04:51 -07002014 struct policydb *oldpolicydb, *newpolicydb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 struct sidtab oldsidtab, newsidtab;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002016 struct selinux_mapping *oldmap, *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct convert_context_args args;
2018 u32 seqno;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002019 u16 map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int rc = 0;
2021 struct policy_file file = { data, len }, *fp = &file;
2022
Tim Gardnerb5495b42013-11-14 15:04:51 -07002023 oldpolicydb = kzalloc(2 * sizeof(*oldpolicydb), GFP_KERNEL);
2024 if (!oldpolicydb) {
2025 rc = -ENOMEM;
2026 goto out;
2027 }
2028 newpolicydb = oldpolicydb + 1;
2029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (!ss_initialized) {
2031 avtab_cache_init();
Eric Parisa2000052010-04-20 10:29:42 -04002032 rc = policydb_read(&policydb, fp);
2033 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 avtab_cache_destroy();
Tim Gardnerb5495b42013-11-14 15:04:51 -07002035 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Eric Parisa2000052010-04-20 10:29:42 -04002037
Eric Pariscee74f42010-10-13 17:50:25 -04002038 policydb.len = len;
Eric Parisa2000052010-04-20 10:29:42 -04002039 rc = selinux_set_mapping(&policydb, secclass_map,
2040 &current_mapping,
2041 &current_mapping_size);
2042 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 policydb_destroy(&policydb);
2044 avtab_cache_destroy();
Tim Gardnerb5495b42013-11-14 15:04:51 -07002045 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Eric Parisa2000052010-04-20 10:29:42 -04002047
2048 rc = policydb_load_isids(&policydb, &sidtab);
2049 if (rc) {
Chad Sellersb94c7e62006-11-06 12:38:18 -05002050 policydb_destroy(&policydb);
2051 avtab_cache_destroy();
Tim Gardnerb5495b42013-11-14 15:04:51 -07002052 goto out;
Chad Sellersb94c7e62006-11-06 12:38:18 -05002053 }
Eric Parisa2000052010-04-20 10:29:42 -04002054
Paul Moore3bb56b22008-01-29 08:38:19 -05002055 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07002057 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07002059 avc_ss_reset(seqno);
2060 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09002061 selinux_status_update_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002062 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002063 selinux_xfrm_notify_policyload();
Tim Gardnerb5495b42013-11-14 15:04:51 -07002064 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
2067#if 0
2068 sidtab_hash_eval(&sidtab, "sids");
2069#endif
2070
Tim Gardnerb5495b42013-11-14 15:04:51 -07002071 rc = policydb_read(newpolicydb, fp);
Eric Parisa2000052010-04-20 10:29:42 -04002072 if (rc)
Tim Gardnerb5495b42013-11-14 15:04:51 -07002073 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Tim Gardnerb5495b42013-11-14 15:04:51 -07002075 newpolicydb->len = len;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002076 /* If switching between different policy types, log MLS status */
Tim Gardnerb5495b42013-11-14 15:04:51 -07002077 if (policydb.mls_enabled && !newpolicydb->mls_enabled)
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002078 printk(KERN_INFO "SELinux: Disabling MLS support...\n");
Tim Gardnerb5495b42013-11-14 15:04:51 -07002079 else if (!policydb.mls_enabled && newpolicydb->mls_enabled)
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002080 printk(KERN_INFO "SELinux: Enabling MLS support...\n");
2081
Tim Gardnerb5495b42013-11-14 15:04:51 -07002082 rc = policydb_load_isids(newpolicydb, &newsidtab);
Guido Trentalancia42596ea2010-02-03 17:06:01 +01002083 if (rc) {
2084 printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
Tim Gardnerb5495b42013-11-14 15:04:51 -07002085 policydb_destroy(newpolicydb);
2086 goto out;
Stephen Smalley12b29f32008-05-07 13:03:20 -04002087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Tim Gardnerb5495b42013-11-14 15:04:51 -07002089 rc = selinux_set_mapping(newpolicydb, secclass_map, &map, &map_size);
Eric Parisa2000052010-04-20 10:29:42 -04002090 if (rc)
Chad Sellersb94c7e62006-11-06 12:38:18 -05002091 goto err;
Chad Sellersb94c7e62006-11-06 12:38:18 -05002092
Tim Gardnerb5495b42013-11-14 15:04:51 -07002093 rc = security_preserve_bools(newpolicydb);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002094 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002095 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002096 goto err;
2097 }
2098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /* Clone the SID table. */
2100 sidtab_shutdown(&sidtab);
Eric Parisa2000052010-04-20 10:29:42 -04002101
2102 rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
2103 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
Stephen Smalley12b29f32008-05-07 13:03:20 -04002106 /*
2107 * Convert the internal representations of contexts
2108 * in the new SID table.
2109 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 args.oldp = &policydb;
Tim Gardnerb5495b42013-11-14 15:04:51 -07002111 args.newp = newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04002112 rc = sidtab_map(&newsidtab, convert_context, &args);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002113 if (rc) {
2114 printk(KERN_ERR "SELinux: unable to convert the internal"
2115 " representation of contexts in the new SID"
2116 " table\n");
Stephen Smalley12b29f32008-05-07 13:03:20 -04002117 goto err;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 /* Save the old policydb and SID table to free later. */
Tim Gardnerb5495b42013-11-14 15:04:51 -07002121 memcpy(oldpolicydb, &policydb, sizeof(policydb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 sidtab_set(&oldsidtab, &sidtab);
2123
2124 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10002125 write_lock_irq(&policy_rwlock);
Tim Gardnerb5495b42013-11-14 15:04:51 -07002126 memcpy(&policydb, newpolicydb, sizeof(policydb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05002128 security_load_policycaps();
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002129 oldmap = current_mapping;
2130 current_mapping = map;
2131 current_mapping_size = map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 seqno = ++latest_granting;
James Morris0804d112008-06-06 18:40:29 +10002133 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 /* Free the old policydb and SID table. */
Tim Gardnerb5495b42013-11-14 15:04:51 -07002136 policydb_destroy(oldpolicydb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 sidtab_destroy(&oldsidtab);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002138 kfree(oldmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 avc_ss_reset(seqno);
2141 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09002142 selinux_status_update_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002143 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002144 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
Tim Gardnerb5495b42013-11-14 15:04:51 -07002146 rc = 0;
2147 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149err:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002150 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 sidtab_destroy(&newsidtab);
Tim Gardnerb5495b42013-11-14 15:04:51 -07002152 policydb_destroy(newpolicydb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Tim Gardnerb5495b42013-11-14 15:04:51 -07002154out:
2155 kfree(oldpolicydb);
2156 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157}
2158
Eric Pariscee74f42010-10-13 17:50:25 -04002159size_t security_policydb_len(void)
2160{
2161 size_t len;
2162
2163 read_lock(&policy_rwlock);
2164 len = policydb.len;
2165 read_unlock(&policy_rwlock);
2166
2167 return len;
2168}
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170/**
2171 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 * @protocol: protocol number
2173 * @port: port number
2174 * @out_sid: security identifier
2175 */
Paul Moore3e112172008-04-10 10:48:14 -04002176int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
2178 struct ocontext *c;
2179 int rc = 0;
2180
James Morris0804d112008-06-06 18:40:29 +10002181 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 c = policydb.ocontexts[OCON_PORT];
2184 while (c) {
2185 if (c->u.port.protocol == protocol &&
2186 c->u.port.low_port <= port &&
2187 c->u.port.high_port >= port)
2188 break;
2189 c = c->next;
2190 }
2191
2192 if (c) {
2193 if (!c->sid[0]) {
2194 rc = sidtab_context_to_sid(&sidtab,
2195 &c->context[0],
2196 &c->sid[0]);
2197 if (rc)
2198 goto out;
2199 }
2200 *out_sid = c->sid[0];
2201 } else {
2202 *out_sid = SECINITSID_PORT;
2203 }
2204
2205out:
James Morris0804d112008-06-06 18:40:29 +10002206 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return rc;
2208}
2209
2210/**
2211 * security_netif_sid - Obtain the SID for a network interface.
2212 * @name: interface name
2213 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05002215int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 int rc = 0;
2218 struct ocontext *c;
2219
James Morris0804d112008-06-06 18:40:29 +10002220 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 c = policydb.ocontexts[OCON_NETIF];
2223 while (c) {
2224 if (strcmp(name, c->u.name) == 0)
2225 break;
2226 c = c->next;
2227 }
2228
2229 if (c) {
2230 if (!c->sid[0] || !c->sid[1]) {
2231 rc = sidtab_context_to_sid(&sidtab,
2232 &c->context[0],
2233 &c->sid[0]);
2234 if (rc)
2235 goto out;
2236 rc = sidtab_context_to_sid(&sidtab,
2237 &c->context[1],
2238 &c->sid[1]);
2239 if (rc)
2240 goto out;
2241 }
2242 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05002243 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246out:
James Morris0804d112008-06-06 18:40:29 +10002247 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return rc;
2249}
2250
2251static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
2252{
2253 int i, fail = 0;
2254
Eric Paris5d55a342008-04-18 17:38:33 -04002255 for (i = 0; i < 4; i++)
2256 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 fail = 1;
2258 break;
2259 }
2260
2261 return !fail;
2262}
2263
2264/**
2265 * security_node_sid - Obtain the SID for a node (host).
2266 * @domain: communication domain aka address family
2267 * @addrp: address
2268 * @addrlen: address length in bytes
2269 * @out_sid: security identifier
2270 */
2271int security_node_sid(u16 domain,
2272 void *addrp,
2273 u32 addrlen,
2274 u32 *out_sid)
2275{
Eric Paris4b02b522010-11-23 11:40:08 -05002276 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 struct ocontext *c;
2278
James Morris0804d112008-06-06 18:40:29 +10002279 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 switch (domain) {
2282 case AF_INET: {
2283 u32 addr;
2284
Eric Paris4b02b522010-11-23 11:40:08 -05002285 rc = -EINVAL;
2286 if (addrlen != sizeof(u32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
2289 addr = *((u32 *)addrp);
2290
2291 c = policydb.ocontexts[OCON_NODE];
2292 while (c) {
2293 if (c->u.node.addr == (addr & c->u.node.mask))
2294 break;
2295 c = c->next;
2296 }
2297 break;
2298 }
2299
2300 case AF_INET6:
Eric Paris4b02b522010-11-23 11:40:08 -05002301 rc = -EINVAL;
2302 if (addrlen != sizeof(u64) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 c = policydb.ocontexts[OCON_NODE6];
2305 while (c) {
2306 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2307 c->u.node6.mask))
2308 break;
2309 c = c->next;
2310 }
2311 break;
2312
2313 default:
Eric Paris4b02b522010-11-23 11:40:08 -05002314 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 *out_sid = SECINITSID_NODE;
2316 goto out;
2317 }
2318
2319 if (c) {
2320 if (!c->sid[0]) {
2321 rc = sidtab_context_to_sid(&sidtab,
2322 &c->context[0],
2323 &c->sid[0]);
2324 if (rc)
2325 goto out;
2326 }
2327 *out_sid = c->sid[0];
2328 } else {
2329 *out_sid = SECINITSID_NODE;
2330 }
2331
Eric Paris4b02b522010-11-23 11:40:08 -05002332 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333out:
James Morris0804d112008-06-06 18:40:29 +10002334 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 return rc;
2336}
2337
2338#define SIDS_NEL 25
2339
2340/**
2341 * security_get_user_sids - Obtain reachable SIDs for a user.
2342 * @fromsid: starting SID
2343 * @username: username
2344 * @sids: array of reachable SIDs for user
2345 * @nel: number of elements in @sids
2346 *
2347 * Generate the set of SIDs for legal security contexts
2348 * for a given user that can be reached by @fromsid.
2349 * Set *@sids to point to a dynamically allocated
2350 * array containing the set of SIDs. Set *@nel to the
2351 * number of elements in the array.
2352 */
2353
2354int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04002355 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 u32 **sids,
2357 u32 *nel)
2358{
2359 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002360 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 u32 mynel = 0, maxnel = SIDS_NEL;
2362 struct user_datum *user;
2363 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002364 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 int rc = 0, i, j;
2366
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002367 *sids = NULL;
2368 *nel = 0;
2369
2370 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
James Morris0804d112008-06-06 18:40:29 +10002373 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Stephen Smalley12b29f32008-05-07 13:03:20 -04002375 context_init(&usercon);
2376
Eric Paris4b02b522010-11-23 11:40:08 -05002377 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 fromcon = sidtab_search(&sidtab, fromsid);
Eric Paris4b02b522010-11-23 11:40:08 -05002379 if (!fromcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Eric Paris4b02b522010-11-23 11:40:08 -05002382 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 user = hashtab_search(policydb.p_users.table, username);
Eric Paris4b02b522010-11-23 11:40:08 -05002384 if (!user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 goto out_unlock;
Eric Paris4b02b522010-11-23 11:40:08 -05002386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 usercon.user = user->value;
2388
Eric Paris4b02b522010-11-23 11:40:08 -05002389 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002390 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Eric Paris4b02b522010-11-23 11:40:08 -05002391 if (!mysids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002394 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 role = policydb.role_val_to_struct[i];
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08002396 usercon.role = i + 1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002397 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08002398 usercon.type = j + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 if (mls_setup_user_range(fromcon, user, &usercon))
2401 continue;
2402
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002404 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (mynel < maxnel) {
2407 mysids[mynel++] = sid;
2408 } else {
Eric Paris4b02b522010-11-23 11:40:08 -05002409 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08002411 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Eric Paris4b02b522010-11-23 11:40:08 -05002412 if (!mysids2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2415 kfree(mysids);
2416 mysids = mysids2;
2417 mysids[mynel++] = sid;
2418 }
2419 }
2420 }
Eric Paris4b02b522010-11-23 11:40:08 -05002421 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002423 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002424 if (rc || !mynel) {
2425 kfree(mysids);
2426 goto out;
2427 }
2428
Eric Paris4b02b522010-11-23 11:40:08 -05002429 rc = -ENOMEM;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002430 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2431 if (!mysids2) {
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002432 kfree(mysids);
2433 goto out;
2434 }
2435 for (i = 0, j = 0; i < mynel; i++) {
Linus Torvaldsf01e1af2011-05-24 13:48:51 -07002436 struct av_decision dummy_avd;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002437 rc = avc_has_perm_noaudit(fromsid, mysids[i],
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002438 SECCLASS_PROCESS, /* kernel value */
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002439 PROCESS__TRANSITION, AVC_STRICT,
Linus Torvaldsf01e1af2011-05-24 13:48:51 -07002440 &dummy_avd);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002441 if (!rc)
2442 mysids2[j++] = mysids[i];
2443 cond_resched();
2444 }
2445 rc = 0;
2446 kfree(mysids);
2447 *sids = mysids2;
2448 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449out:
2450 return rc;
2451}
2452
2453/**
Waiman Longf31e7992014-06-23 11:28:51 -04002454 * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 * @fstype: filesystem type
2456 * @path: path from root of mount
2457 * @sclass: file security class
2458 * @sid: SID for path
2459 *
2460 * Obtain a SID to use for a file in a filesystem that
2461 * cannot support xattr or use a fixed labeling behavior like
2462 * transition SIDs or task SIDs.
Waiman Longf31e7992014-06-23 11:28:51 -04002463 *
2464 * The caller must acquire the policy_rwlock before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 */
Waiman Longf31e7992014-06-23 11:28:51 -04002466static inline int __security_genfs_sid(const char *fstype,
2467 char *path,
2468 u16 orig_sclass,
2469 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
2471 int len;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002472 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 struct genfs *genfs;
2474 struct ocontext *c;
Eric Paris4b02b522010-11-23 11:40:08 -05002475 int rc, cmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05002477 while (path[0] == '/' && path[1] == '/')
2478 path++;
2479
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002480 sclass = unmap_class(orig_sclass);
Eric Paris4b02b522010-11-23 11:40:08 -05002481 *sid = SECINITSID_UNLABELED;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002482
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2484 cmp = strcmp(fstype, genfs->fstype);
2485 if (cmp <= 0)
2486 break;
2487 }
2488
Eric Paris4b02b522010-11-23 11:40:08 -05002489 rc = -ENOENT;
2490 if (!genfs || cmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 for (c = genfs->head; c; c = c->next) {
2494 len = strlen(c->u.name);
2495 if ((!c->v.sclass || sclass == c->v.sclass) &&
2496 (strncmp(c->u.name, path, len) == 0))
2497 break;
2498 }
2499
Eric Paris4b02b522010-11-23 11:40:08 -05002500 rc = -ENOENT;
2501 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
2504 if (!c->sid[0]) {
Eric Paris4b02b522010-11-23 11:40:08 -05002505 rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 if (rc)
2507 goto out;
2508 }
2509
2510 *sid = c->sid[0];
Eric Paris4b02b522010-11-23 11:40:08 -05002511 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return rc;
2514}
2515
2516/**
Waiman Longf31e7992014-06-23 11:28:51 -04002517 * security_genfs_sid - Obtain a SID for a file in a filesystem
2518 * @fstype: filesystem type
2519 * @path: path from root of mount
2520 * @sclass: file security class
2521 * @sid: SID for path
2522 *
2523 * Acquire policy_rwlock before calling __security_genfs_sid() and release
2524 * it afterward.
2525 */
2526int security_genfs_sid(const char *fstype,
2527 char *path,
2528 u16 orig_sclass,
2529 u32 *sid)
2530{
2531 int retval;
2532
2533 read_lock(&policy_rwlock);
2534 retval = __security_genfs_sid(fstype, path, orig_sclass, sid);
2535 read_unlock(&policy_rwlock);
2536 return retval;
2537}
2538
2539/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 * security_fs_use - Determine how to handle labeling for a filesystem.
Eric Parisa64c54c2012-08-24 15:59:07 -04002541 * @sb: superblock in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 */
Eric Parisa64c54c2012-08-24 15:59:07 -04002543int security_fs_use(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
2545 int rc = 0;
2546 struct ocontext *c;
Eric Parisa64c54c2012-08-24 15:59:07 -04002547 struct superblock_security_struct *sbsec = sb->s_security;
2548 const char *fstype = sb->s_type->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
James Morris0804d112008-06-06 18:40:29 +10002550 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Paul Moore4d546f82013-12-13 14:49:53 -05002552 c = policydb.ocontexts[OCON_FSUSE];
2553 while (c) {
2554 if (strcmp(fstype, c->u.name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 break;
Paul Moore4d546f82013-12-13 14:49:53 -05002556 c = c->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 }
2558
2559 if (c) {
Eric Parisa64c54c2012-08-24 15:59:07 -04002560 sbsec->behavior = c->v.behavior;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (!c->sid[0]) {
Eric Paris4b02b522010-11-23 11:40:08 -05002562 rc = sidtab_context_to_sid(&sidtab, &c->context[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 &c->sid[0]);
2564 if (rc)
2565 goto out;
2566 }
Eric Parisa64c54c2012-08-24 15:59:07 -04002567 sbsec->sid = c->sid[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 } else {
Waiman Longf31e7992014-06-23 11:28:51 -04002569 rc = __security_genfs_sid(fstype, "/", SECCLASS_DIR,
2570 &sbsec->sid);
James Morris089be432008-07-15 18:32:49 +10002571 if (rc) {
Eric Parisa64c54c2012-08-24 15:59:07 -04002572 sbsec->behavior = SECURITY_FS_USE_NONE;
James Morris089be432008-07-15 18:32:49 +10002573 rc = 0;
2574 } else {
Eric Parisa64c54c2012-08-24 15:59:07 -04002575 sbsec->behavior = SECURITY_FS_USE_GENFS;
James Morris089be432008-07-15 18:32:49 +10002576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578
2579out:
James Morris0804d112008-06-06 18:40:29 +10002580 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return rc;
2582}
2583
2584int security_get_bools(int *len, char ***names, int **values)
2585{
Eric Paris4b02b522010-11-23 11:40:08 -05002586 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587
James Morris0804d112008-06-06 18:40:29 +10002588 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 *names = NULL;
2590 *values = NULL;
2591
Eric Paris4b02b522010-11-23 11:40:08 -05002592 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 *len = policydb.p_bools.nprim;
Eric Paris4b02b522010-11-23 11:40:08 -05002594 if (!*len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Eric Paris4b02b522010-11-23 11:40:08 -05002597 rc = -ENOMEM;
2598 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if (!*names)
2600 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Eric Paris4b02b522010-11-23 11:40:08 -05002602 rc = -ENOMEM;
2603 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 if (!*values)
2605 goto err;
2606
2607 for (i = 0; i < *len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 (*values)[i] = policydb.bool_val_to_struct[i]->state;
Eric Paris4b02b522010-11-23 11:40:08 -05002609
2610 rc = -ENOMEM;
Rasmus Villemoes21b76f12015-10-21 17:44:26 -04002611 (*names)[i] = kstrdup(sym_name(&policydb, SYM_BOOLS, i), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (!(*names)[i])
2613 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
2615 rc = 0;
2616out:
James Morris0804d112008-06-06 18:40:29 +10002617 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 return rc;
2619err:
2620 if (*names) {
2621 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002622 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002624 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto out;
2626}
2627
2628
2629int security_set_bools(int len, int *values)
2630{
Eric Paris4b02b522010-11-23 11:40:08 -05002631 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 int lenp, seqno = 0;
2633 struct cond_node *cur;
2634
James Morris0804d112008-06-06 18:40:29 +10002635 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Eric Paris4b02b522010-11-23 11:40:08 -05002637 rc = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 lenp = policydb.p_bools.nprim;
Eric Paris4b02b522010-11-23 11:40:08 -05002639 if (len != lenp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002643 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2644 audit_log(current->audit_context, GFP_ATOMIC,
2645 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002646 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Eric Parisac76c052010-11-29 15:47:09 -05002647 sym_name(&policydb, SYM_BOOLS, i),
Steve Grubbaf601e42006-01-04 14:08:39 +00002648 !!values[i],
2649 policydb.bool_val_to_struct[i]->state,
Eric W. Biederman581abc02012-08-20 00:09:36 -07002650 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -05002651 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002652 }
Eric Paris5d55a342008-04-18 17:38:33 -04002653 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002655 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002659 for (cur = policydb.cond_list; cur; cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 rc = evaluate_cond_node(&policydb, cur);
2661 if (rc)
2662 goto out;
2663 }
2664
2665 seqno = ++latest_granting;
Eric Paris4b02b522010-11-23 11:40:08 -05002666 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667out:
James Morris0804d112008-06-06 18:40:29 +10002668 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 if (!rc) {
2670 avc_ss_reset(seqno);
2671 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09002672 selinux_status_update_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002673 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 return rc;
2676}
2677
Prarit Bhargava0fd71a62016-04-14 10:40:57 -04002678int security_get_bool_value(int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
Eric Paris4b02b522010-11-23 11:40:08 -05002680 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 int len;
2682
James Morris0804d112008-06-06 18:40:29 +10002683 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Eric Paris4b02b522010-11-23 11:40:08 -05002685 rc = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 len = policydb.p_bools.nprim;
Prarit Bhargava0fd71a62016-04-14 10:40:57 -04002687 if (index >= len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Prarit Bhargava0fd71a62016-04-14 10:40:57 -04002690 rc = policydb.bool_val_to_struct[index]->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691out:
James Morris0804d112008-06-06 18:40:29 +10002692 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return rc;
2694}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002695
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002696static int security_preserve_bools(struct policydb *p)
2697{
2698 int rc, nbools = 0, *bvalues = NULL, i;
2699 char **bnames = NULL;
2700 struct cond_bool_datum *booldatum;
2701 struct cond_node *cur;
2702
2703 rc = security_get_bools(&nbools, &bnames, &bvalues);
2704 if (rc)
2705 goto out;
2706 for (i = 0; i < nbools; i++) {
2707 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2708 if (booldatum)
2709 booldatum->state = bvalues[i];
2710 }
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002711 for (cur = p->cond_list; cur; cur = cur->next) {
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002712 rc = evaluate_cond_node(p, cur);
2713 if (rc)
2714 goto out;
2715 }
2716
2717out:
2718 if (bnames) {
2719 for (i = 0; i < nbools; i++)
2720 kfree(bnames[i]);
2721 }
2722 kfree(bnames);
2723 kfree(bvalues);
2724 return rc;
2725}
2726
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002727/*
2728 * security_sid_mls_copy() - computes a new sid based on the given
2729 * sid and the mls portion of mls_sid.
2730 */
2731int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2732{
2733 struct context *context1;
2734 struct context *context2;
2735 struct context newcon;
2736 char *s;
2737 u32 len;
Eric Paris4b02b522010-11-23 11:40:08 -05002738 int rc;
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002739
Eric Paris4b02b522010-11-23 11:40:08 -05002740 rc = 0;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002741 if (!ss_initialized || !policydb.mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002742 *new_sid = sid;
2743 goto out;
2744 }
2745
2746 context_init(&newcon);
2747
James Morris0804d112008-06-06 18:40:29 +10002748 read_lock(&policy_rwlock);
Eric Paris4b02b522010-11-23 11:40:08 -05002749
2750 rc = -EINVAL;
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002751 context1 = sidtab_search(&sidtab, sid);
2752 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002753 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2754 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002755 goto out_unlock;
2756 }
2757
Eric Paris4b02b522010-11-23 11:40:08 -05002758 rc = -EINVAL;
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002759 context2 = sidtab_search(&sidtab, mls_sid);
2760 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002761 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2762 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002763 goto out_unlock;
2764 }
2765
2766 newcon.user = context1->user;
2767 newcon.role = context1->role;
2768 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002769 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002770 if (rc)
2771 goto out_unlock;
2772
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002773 /* Check the validity of the new context. */
2774 if (!policydb_context_isvalid(&policydb, &newcon)) {
2775 rc = convert_context_handle_invalid_context(&newcon);
Eric Paris4b02b522010-11-23 11:40:08 -05002776 if (rc) {
2777 if (!context_struct_to_string(&newcon, &s, &len)) {
Richard Guy Briggs4093a842014-09-18 20:47:48 -04002778 audit_log(current->audit_context,
2779 GFP_ATOMIC, AUDIT_SELINUX_ERR,
2780 "op=security_sid_mls_copy "
2781 "invalid_context=%s", s);
Eric Paris4b02b522010-11-23 11:40:08 -05002782 kfree(s);
2783 }
2784 goto out_unlock;
2785 }
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002786 }
2787
2788 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002789out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002790 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002791 context_destroy(&newcon);
2792out:
2793 return rc;
2794}
2795
Paul Moore220deb92008-01-29 08:38:23 -05002796/**
2797 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2798 * @nlbl_sid: NetLabel SID
2799 * @nlbl_type: NetLabel labeling protocol type
2800 * @xfrm_sid: XFRM SID
2801 *
2802 * Description:
2803 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2804 * resolved into a single SID it is returned via @peer_sid and the function
2805 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2806 * returns a negative value. A table summarizing the behavior is below:
2807 *
2808 * | function return | @sid
2809 * ------------------------------+-----------------+-----------------
2810 * no peer labels | 0 | SECSID_NULL
2811 * single peer label | 0 | <peer_label>
2812 * multiple, consistent labels | 0 | <peer_label>
2813 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2814 *
2815 */
2816int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2817 u32 xfrm_sid,
2818 u32 *peer_sid)
2819{
2820 int rc;
2821 struct context *nlbl_ctx;
2822 struct context *xfrm_ctx;
2823
Eric Paris4b02b522010-11-23 11:40:08 -05002824 *peer_sid = SECSID_NULL;
2825
Paul Moore220deb92008-01-29 08:38:23 -05002826 /* handle the common (which also happens to be the set of easy) cases
2827 * right away, these two if statements catch everything involving a
2828 * single or absent peer SID/label */
2829 if (xfrm_sid == SECSID_NULL) {
2830 *peer_sid = nlbl_sid;
2831 return 0;
2832 }
2833 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2834 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2835 * is present */
2836 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2837 *peer_sid = xfrm_sid;
2838 return 0;
2839 }
2840
2841 /* we don't need to check ss_initialized here since the only way both
2842 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2843 * security server was initialized and ss_initialized was true */
Eric Paris4b02b522010-11-23 11:40:08 -05002844 if (!policydb.mls_enabled)
Paul Moore220deb92008-01-29 08:38:23 -05002845 return 0;
Paul Moore220deb92008-01-29 08:38:23 -05002846
James Morris0804d112008-06-06 18:40:29 +10002847 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002848
Eric Paris4b02b522010-11-23 11:40:08 -05002849 rc = -EINVAL;
Paul Moore220deb92008-01-29 08:38:23 -05002850 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2851 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002852 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2853 __func__, nlbl_sid);
Eric Paris4b02b522010-11-23 11:40:08 -05002854 goto out;
Paul Moore220deb92008-01-29 08:38:23 -05002855 }
Eric Paris4b02b522010-11-23 11:40:08 -05002856 rc = -EINVAL;
Paul Moore220deb92008-01-29 08:38:23 -05002857 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2858 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002859 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2860 __func__, xfrm_sid);
Eric Paris4b02b522010-11-23 11:40:08 -05002861 goto out;
Paul Moore220deb92008-01-29 08:38:23 -05002862 }
2863 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
Eric Paris4b02b522010-11-23 11:40:08 -05002864 if (rc)
2865 goto out;
Paul Moore220deb92008-01-29 08:38:23 -05002866
Eric Paris4b02b522010-11-23 11:40:08 -05002867 /* at present NetLabel SIDs/labels really only carry MLS
2868 * information so if the MLS portion of the NetLabel SID
2869 * matches the MLS portion of the labeled XFRM SID/label
2870 * then pass along the XFRM SID as it is the most
2871 * expressive */
2872 *peer_sid = xfrm_sid;
2873out:
James Morris0804d112008-06-06 18:40:29 +10002874 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002875 return rc;
2876}
2877
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002878static int get_classes_callback(void *k, void *d, void *args)
2879{
2880 struct class_datum *datum = d;
2881 char *name = k, **classes = args;
2882 int value = datum->value - 1;
2883
2884 classes[value] = kstrdup(name, GFP_ATOMIC);
2885 if (!classes[value])
2886 return -ENOMEM;
2887
2888 return 0;
2889}
2890
2891int security_get_classes(char ***classes, int *nclasses)
2892{
Eric Paris4b02b522010-11-23 11:40:08 -05002893 int rc;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002894
James Morris0804d112008-06-06 18:40:29 +10002895 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002896
Eric Paris4b02b522010-11-23 11:40:08 -05002897 rc = -ENOMEM;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002898 *nclasses = policydb.p_classes.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002899 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002900 if (!*classes)
2901 goto out;
2902
2903 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2904 *classes);
Eric Paris4b02b522010-11-23 11:40:08 -05002905 if (rc) {
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002906 int i;
2907 for (i = 0; i < *nclasses; i++)
2908 kfree((*classes)[i]);
2909 kfree(*classes);
2910 }
2911
2912out:
James Morris0804d112008-06-06 18:40:29 +10002913 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002914 return rc;
2915}
2916
2917static int get_permissions_callback(void *k, void *d, void *args)
2918{
2919 struct perm_datum *datum = d;
2920 char *name = k, **perms = args;
2921 int value = datum->value - 1;
2922
2923 perms[value] = kstrdup(name, GFP_ATOMIC);
2924 if (!perms[value])
2925 return -ENOMEM;
2926
2927 return 0;
2928}
2929
2930int security_get_permissions(char *class, char ***perms, int *nperms)
2931{
Eric Paris4b02b522010-11-23 11:40:08 -05002932 int rc, i;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002933 struct class_datum *match;
2934
James Morris0804d112008-06-06 18:40:29 +10002935 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002936
Eric Paris4b02b522010-11-23 11:40:08 -05002937 rc = -EINVAL;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002938 match = hashtab_search(policydb.p_classes.table, class);
2939 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002940 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002941 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002942 goto out;
2943 }
2944
Eric Paris4b02b522010-11-23 11:40:08 -05002945 rc = -ENOMEM;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002946 *nperms = match->permissions.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002947 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002948 if (!*perms)
2949 goto out;
2950
2951 if (match->comdatum) {
2952 rc = hashtab_map(match->comdatum->permissions.table,
2953 get_permissions_callback, *perms);
Eric Paris4b02b522010-11-23 11:40:08 -05002954 if (rc)
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002955 goto err;
2956 }
2957
2958 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2959 *perms);
Eric Paris4b02b522010-11-23 11:40:08 -05002960 if (rc)
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002961 goto err;
2962
2963out:
James Morris0804d112008-06-06 18:40:29 +10002964 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002965 return rc;
2966
2967err:
James Morris0804d112008-06-06 18:40:29 +10002968 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002969 for (i = 0; i < *nperms; i++)
2970 kfree((*perms)[i]);
2971 kfree(*perms);
2972 return rc;
2973}
2974
Eric Paris3f120702007-09-21 14:37:10 -04002975int security_get_reject_unknown(void)
2976{
2977 return policydb.reject_unknown;
2978}
2979
2980int security_get_allow_unknown(void)
2981{
2982 return policydb.allow_unknown;
2983}
2984
Paul Moore3bb56b22008-01-29 08:38:19 -05002985/**
Paul Moore3bb56b22008-01-29 08:38:19 -05002986 * security_policycap_supported - Check for a specific policy capability
2987 * @req_cap: capability
2988 *
2989 * Description:
2990 * This function queries the currently loaded policy to see if it supports the
2991 * capability specified by @req_cap. Returns true (1) if the capability is
2992 * supported, false (0) if it isn't supported.
2993 *
2994 */
2995int security_policycap_supported(unsigned int req_cap)
2996{
2997 int rc;
2998
James Morris0804d112008-06-06 18:40:29 +10002999 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05003000 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10003001 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05003002
3003 return rc;
3004}
3005
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003006struct selinux_audit_rule {
3007 u32 au_seqno;
3008 struct context au_ctxt;
3009};
3010
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003011void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003012{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003013 struct selinux_audit_rule *rule = vrule;
3014
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003015 if (rule) {
3016 context_destroy(&rule->au_ctxt);
3017 kfree(rule);
3018 }
3019}
3020
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003021int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003022{
3023 struct selinux_audit_rule *tmprule;
3024 struct role_datum *roledatum;
3025 struct type_datum *typedatum;
3026 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003027 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003028 int rc = 0;
3029
3030 *rule = NULL;
3031
3032 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07003033 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003034
3035 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003036 case AUDIT_SUBJ_USER:
3037 case AUDIT_SUBJ_ROLE:
3038 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003039 case AUDIT_OBJ_USER:
3040 case AUDIT_OBJ_ROLE:
3041 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003042 /* only 'equals' and 'not equals' fit user, role, and type */
Al Viro5af75d82008-12-16 05:59:26 -05003043 if (op != Audit_equal && op != Audit_not_equal)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003044 return -EINVAL;
3045 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003046 case AUDIT_SUBJ_SEN:
3047 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003048 case AUDIT_OBJ_LEV_LOW:
3049 case AUDIT_OBJ_LEV_HIGH:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003050 /* we do not allow a range, indicated by the presence of '-' */
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003051 if (strchr(rulestr, '-'))
3052 return -EINVAL;
3053 break;
3054 default:
3055 /* only the above fields are valid */
3056 return -EINVAL;
3057 }
3058
3059 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3060 if (!tmprule)
3061 return -ENOMEM;
3062
3063 context_init(&tmprule->au_ctxt);
3064
James Morris0804d112008-06-06 18:40:29 +10003065 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003066
3067 tmprule->au_seqno = latest_granting;
3068
3069 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003070 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003071 case AUDIT_OBJ_USER:
Eric Paris4b02b522010-11-23 11:40:08 -05003072 rc = -EINVAL;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003073 userdatum = hashtab_search(policydb.p_users.table, rulestr);
3074 if (!userdatum)
Eric Paris4b02b522010-11-23 11:40:08 -05003075 goto out;
3076 tmprule->au_ctxt.user = userdatum->value;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003077 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003078 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003079 case AUDIT_OBJ_ROLE:
Eric Paris4b02b522010-11-23 11:40:08 -05003080 rc = -EINVAL;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003081 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
3082 if (!roledatum)
Eric Paris4b02b522010-11-23 11:40:08 -05003083 goto out;
3084 tmprule->au_ctxt.role = roledatum->value;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003085 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003086 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003087 case AUDIT_OBJ_TYPE:
Eric Paris4b02b522010-11-23 11:40:08 -05003088 rc = -EINVAL;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003089 typedatum = hashtab_search(policydb.p_types.table, rulestr);
3090 if (!typedatum)
Eric Paris4b02b522010-11-23 11:40:08 -05003091 goto out;
3092 tmprule->au_ctxt.type = typedatum->value;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003093 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003094 case AUDIT_SUBJ_SEN:
3095 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003096 case AUDIT_OBJ_LEV_LOW:
3097 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003098 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
Eric Paris4b02b522010-11-23 11:40:08 -05003099 if (rc)
3100 goto out;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003101 break;
3102 }
Eric Paris4b02b522010-11-23 11:40:08 -05003103 rc = 0;
3104out:
James Morris0804d112008-06-06 18:40:29 +10003105 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003106
3107 if (rc) {
3108 selinux_audit_rule_free(tmprule);
3109 tmprule = NULL;
3110 }
3111
3112 *rule = tmprule;
3113
3114 return rc;
3115}
3116
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003117/* Check to see if the rule contains any selinux fields */
3118int selinux_audit_rule_known(struct audit_krule *rule)
3119{
3120 int i;
3121
3122 for (i = 0; i < rule->field_count; i++) {
3123 struct audit_field *f = &rule->fields[i];
3124 switch (f->type) {
3125 case AUDIT_SUBJ_USER:
3126 case AUDIT_SUBJ_ROLE:
3127 case AUDIT_SUBJ_TYPE:
3128 case AUDIT_SUBJ_SEN:
3129 case AUDIT_SUBJ_CLR:
3130 case AUDIT_OBJ_USER:
3131 case AUDIT_OBJ_ROLE:
3132 case AUDIT_OBJ_TYPE:
3133 case AUDIT_OBJ_LEV_LOW:
3134 case AUDIT_OBJ_LEV_HIGH:
3135 return 1;
3136 }
3137 }
3138
3139 return 0;
3140}
3141
3142int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04003143 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003144{
3145 struct context *ctxt;
3146 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003147 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003148 int match = 0;
3149
Richard Guy Briggs9ad42a72013-11-21 13:31:40 -05003150 if (unlikely(!rule)) {
3151 WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003152 return -ENOENT;
3153 }
3154
James Morris0804d112008-06-06 18:40:29 +10003155 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003156
3157 if (rule->au_seqno < latest_granting) {
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003158 match = -ESTALE;
3159 goto out;
3160 }
3161
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07003162 ctxt = sidtab_search(&sidtab, sid);
Richard Guy Briggs9ad42a72013-11-21 13:31:40 -05003163 if (unlikely(!ctxt)) {
3164 WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n",
Eric Paris5d55a342008-04-18 17:38:33 -04003165 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003166 match = -ENOENT;
3167 goto out;
3168 }
3169
3170 /* a field/op pair that is not caught here will simply fall through
3171 without a match */
3172 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003173 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003174 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003175 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05003176 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003177 match = (ctxt->user == rule->au_ctxt.user);
3178 break;
Al Viro5af75d82008-12-16 05:59:26 -05003179 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003180 match = (ctxt->user != rule->au_ctxt.user);
3181 break;
3182 }
3183 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003184 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003185 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003186 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05003187 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003188 match = (ctxt->role == rule->au_ctxt.role);
3189 break;
Al Viro5af75d82008-12-16 05:59:26 -05003190 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003191 match = (ctxt->role != rule->au_ctxt.role);
3192 break;
3193 }
3194 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003195 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003196 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003197 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05003198 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003199 match = (ctxt->type == rule->au_ctxt.type);
3200 break;
Al Viro5af75d82008-12-16 05:59:26 -05003201 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003202 match = (ctxt->type != rule->au_ctxt.type);
3203 break;
3204 }
3205 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003206 case AUDIT_SUBJ_SEN:
3207 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003208 case AUDIT_OBJ_LEV_LOW:
3209 case AUDIT_OBJ_LEV_HIGH:
3210 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04003211 field == AUDIT_OBJ_LEV_LOW) ?
3212 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003213 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05003214 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003215 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04003216 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003217 break;
Al Viro5af75d82008-12-16 05:59:26 -05003218 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003219 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04003220 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003221 break;
Al Viro5af75d82008-12-16 05:59:26 -05003222 case Audit_lt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003223 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04003224 level) &&
3225 !mls_level_eq(&rule->au_ctxt.range.level[0],
3226 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003227 break;
Al Viro5af75d82008-12-16 05:59:26 -05003228 case Audit_le:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003229 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04003230 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003231 break;
Al Viro5af75d82008-12-16 05:59:26 -05003232 case Audit_gt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003233 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04003234 &rule->au_ctxt.range.level[0]) &&
3235 !mls_level_eq(level,
3236 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003237 break;
Al Viro5af75d82008-12-16 05:59:26 -05003238 case Audit_ge:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003239 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04003240 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003241 break;
3242 }
3243 }
3244
3245out:
James Morris0804d112008-06-06 18:40:29 +10003246 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003247 return match;
3248}
3249
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003250static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003251
Wanlong Gao562c99f2012-03-07 22:17:14 +08003252static int aurule_avc_callback(u32 event)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003253{
3254 int err = 0;
3255
3256 if (event == AVC_CALLBACK_RESET && aurule_callback)
3257 err = aurule_callback();
3258 return err;
3259}
3260
3261static int __init aurule_init(void)
3262{
3263 int err;
3264
Wanlong Gao562c99f2012-03-07 22:17:14 +08003265 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003266 if (err)
3267 panic("avc_add_callback() failed, error %d\n", err);
3268
3269 return err;
3270}
3271__initcall(aurule_init);
3272
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003273#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003274/**
Paul Moore5778eab2007-02-28 15:14:22 -05003275 * security_netlbl_cache_add - Add an entry to the NetLabel cache
3276 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003277 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003278 *
3279 * Description:
3280 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05003281 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
3282 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003283 *
3284 */
Paul Moore5778eab2007-02-28 15:14:22 -05003285static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003286 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003287{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003288 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003289
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003290 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
3291 if (sid_cache == NULL)
3292 return;
Paul Moore5778eab2007-02-28 15:14:22 -05003293 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003294 if (secattr->cache == NULL) {
3295 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05003296 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02003297 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003298
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003299 *sid_cache = sid;
3300 secattr->cache->free = kfree;
3301 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05003302 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003303}
3304
3305/**
Paul Moore5778eab2007-02-28 15:14:22 -05003306 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003307 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003308 * @sid: the SELinux SID
3309 *
3310 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05003311 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003312 * SELinux SID. If the @secattr field does not contain a full SELinux
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003313 * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003314 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
3315 * allow the @secattr to be used by NetLabel to cache the secattr to SID
3316 * conversion for future lookups. Returns zero on success, negative values on
3317 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003318 *
3319 */
Paul Moore5778eab2007-02-28 15:14:22 -05003320int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05003321 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003322{
Eric Paris7ae9f232010-11-23 11:40:09 -05003323 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003324 struct context *ctx;
3325 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05003326
3327 if (!ss_initialized) {
3328 *sid = SECSID_NULL;
3329 return 0;
3330 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003331
James Morris0804d112008-06-06 18:40:29 +10003332 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003333
Eric Paris7ae9f232010-11-23 11:40:09 -05003334 if (secattr->flags & NETLBL_SECATTR_CACHE)
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003335 *sid = *(u32 *)secattr->cache->data;
Eric Paris7ae9f232010-11-23 11:40:09 -05003336 else if (secattr->flags & NETLBL_SECATTR_SECID)
Paul Moore16efd452008-01-29 08:37:59 -05003337 *sid = secattr->attr.secid;
Eric Paris7ae9f232010-11-23 11:40:09 -05003338 else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
3339 rc = -EIDRM;
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003340 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003341 if (ctx == NULL)
Eric Paris7ae9f232010-11-23 11:40:09 -05003342 goto out;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003343
Paul Moore81990fb2008-10-03 10:51:15 -04003344 context_init(&ctx_new);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003345 ctx_new.user = ctx->user;
3346 ctx_new.role = ctx->role;
3347 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05003348 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05003349 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Mooreda8026f2015-02-17 15:30:23 -05003350 rc = mls_import_netlbl_cat(&ctx_new, secattr);
Eric Paris7ae9f232010-11-23 11:40:09 -05003351 if (rc)
3352 goto out;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003353 }
Eric Paris7ae9f232010-11-23 11:40:09 -05003354 rc = -EIDRM;
3355 if (!mls_context_isvalid(&policydb, &ctx_new))
3356 goto out_free;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003357
3358 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
Eric Paris7ae9f232010-11-23 11:40:09 -05003359 if (rc)
3360 goto out_free;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003361
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003362 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05003363
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003364 ebitmap_destroy(&ctx_new.range.level[0].cat);
Eric Paris7ae9f232010-11-23 11:40:09 -05003365 } else
paul.moore@hp.com388b2402006-10-05 18:28:24 -04003366 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003367
Eric Paris7ae9f232010-11-23 11:40:09 -05003368 read_unlock(&policy_rwlock);
3369 return 0;
3370out_free:
3371 ebitmap_destroy(&ctx_new.range.level[0].cat);
3372out:
James Morris0804d112008-06-06 18:40:29 +10003373 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003374 return rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003375}
3376
3377/**
Paul Moore5778eab2007-02-28 15:14:22 -05003378 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3379 * @sid: the SELinux SID
3380 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003381 *
3382 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05003383 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3384 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003385 *
3386 */
Paul Moore5778eab2007-02-28 15:14:22 -05003387int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003388{
Paul Moore99d854d2008-10-10 10:16:30 -04003389 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003390 struct context *ctx;
3391
3392 if (!ss_initialized)
3393 return 0;
3394
James Morris0804d112008-06-06 18:40:29 +10003395 read_lock(&policy_rwlock);
Eric Paris4b02b522010-11-23 11:40:08 -05003396
3397 rc = -ENOENT;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003398 ctx = sidtab_search(&sidtab, sid);
Eric Paris4b02b522010-11-23 11:40:08 -05003399 if (ctx == NULL)
3400 goto out;
3401
3402 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -05003403 secattr->domain = kstrdup(sym_name(&policydb, SYM_TYPES, ctx->type - 1),
Paul Moore5778eab2007-02-28 15:14:22 -05003404 GFP_ATOMIC);
Eric Paris4b02b522010-11-23 11:40:08 -05003405 if (secattr->domain == NULL)
3406 goto out;
3407
Paul Moore8d758992008-10-10 10:16:33 -04003408 secattr->attr.secid = sid;
3409 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
Paul Moore5778eab2007-02-28 15:14:22 -05003410 mls_export_netlbl_lvl(ctx, secattr);
3411 rc = mls_export_netlbl_cat(ctx, secattr);
Eric Paris4b02b522010-11-23 11:40:08 -05003412out:
James Morris0804d112008-06-06 18:40:29 +10003413 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08003414 return rc;
3415}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003416#endif /* CONFIG_NETLABEL */
Eric Pariscee74f42010-10-13 17:50:25 -04003417
3418/**
3419 * security_read_policy - read the policy.
3420 * @data: binary policy data
3421 * @len: length of data in bytes
3422 *
3423 */
Eric Paris6b697322011-04-20 10:21:28 -04003424int security_read_policy(void **data, size_t *len)
Eric Pariscee74f42010-10-13 17:50:25 -04003425{
3426 int rc;
3427 struct policy_file fp;
3428
3429 if (!ss_initialized)
3430 return -EINVAL;
3431
3432 *len = security_policydb_len();
3433
Eric Paris845ca302010-10-13 17:50:31 -04003434 *data = vmalloc_user(*len);
Eric Pariscee74f42010-10-13 17:50:25 -04003435 if (!*data)
3436 return -ENOMEM;
3437
3438 fp.data = *data;
3439 fp.len = *len;
3440
3441 read_lock(&policy_rwlock);
3442 rc = policydb_write(&policydb, &fp);
3443 read_unlock(&policy_rwlock);
3444
3445 if (rc)
3446 return rc;
3447
3448 *len = (unsigned long)fp.data - (unsigned long)*data;
3449 return 0;
3450
3451}