blob: 2dccba4851f8161772351f8ef05a9b38ac61bcab [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 Molnarbb0030792006-03-22 00:09:14 -080051#include <linux/mutex.h>
Adrian Bunk0e55a002008-03-31 01:54:02 +030052#include <linux/selinux.h>
Eric Paris6371dcd2010-07-29 23:02:34 -040053#include <linux/flex_array.h>
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 Molnarbb0030792006-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
Stephen Smalley4dc2fce2017-05-18 16:58:31 -040073/* Policy capability names */
74char *selinux_policycap_names[__POLICYDB_CAPABILITY_MAX] = {
75 "network_peer_controls",
76 "open_perms",
77 "extended_socket_class",
78 "always_check_network",
79 "cgroup_seclabel"
80};
81
Paul Moore3bb56b22008-01-29 08:38:19 -050082int selinux_policycap_netpeer;
Eric Parisb0c636b2008-02-28 12:58:40 -050083int selinux_policycap_openperm;
Stephen Smalleyda69a532017-01-09 10:07:30 -050084int selinux_policycap_extsockclass;
Chris PeBenito2be4d742013-05-03 09:05:39 -040085int selinux_policycap_alwaysnetwork;
Stephen Smalley2651225b2017-02-28 10:35:56 -050086int selinux_policycap_cgroupseclabel;
Paul Moore3bb56b22008-01-29 08:38:19 -050087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static DEFINE_RWLOCK(policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static struct sidtab sidtab;
91struct policydb policydb;
Eric Paris5d55a342008-04-18 17:38:33 -040092int ss_initialized;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94/*
95 * The largest sequence number that has been used when
96 * providing an access decision to the access vector cache.
97 * The sequence number only changes when a policy change
98 * occurs.
99 */
Eric Paris5d55a342008-04-18 17:38:33 -0400100static u32 latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/* Forward declaration. */
103static int context_struct_to_string(struct context *context, char **scontext,
104 u32 *scontext_len);
105
Stephen Smalley19439d02010-01-14 17:28:10 -0500106static void context_struct_compute_av(struct context *scontext,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400107 struct context *tcontext,
108 u16 tclass,
109 struct av_decision *avd,
110 struct extended_perms *xperms);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400111
112struct selinux_mapping {
113 u16 value; /* policy value */
114 unsigned num_perms;
115 u32 perms[sizeof(u32) * 8];
116};
117
118static struct selinux_mapping *current_mapping;
119static u16 current_mapping_size;
120
121static int selinux_set_mapping(struct policydb *pol,
122 struct security_class_mapping *map,
123 struct selinux_mapping **out_map_p,
124 u16 *out_map_size)
125{
126 struct selinux_mapping *out_map = NULL;
127 size_t size = sizeof(struct selinux_mapping);
128 u16 i, j;
129 unsigned k;
130 bool print_unknown_handle = false;
131
132 /* Find number of classes in the input mapping */
133 if (!map)
134 return -EINVAL;
135 i = 0;
136 while (map[i].name)
137 i++;
138
139 /* Allocate space for the class records, plus one for class zero */
140 out_map = kcalloc(++i, size, GFP_ATOMIC);
141 if (!out_map)
142 return -ENOMEM;
143
144 /* Store the raw class and permission values */
145 j = 0;
146 while (map[j].name) {
147 struct security_class_mapping *p_in = map + (j++);
148 struct selinux_mapping *p_out = out_map + j;
149
150 /* An empty class string skips ahead */
151 if (!strcmp(p_in->name, "")) {
152 p_out->num_perms = 0;
153 continue;
154 }
155
156 p_out->value = string_to_security_class(pol, p_in->name);
157 if (!p_out->value) {
158 printk(KERN_INFO
159 "SELinux: Class %s not defined in policy.\n",
160 p_in->name);
161 if (pol->reject_unknown)
162 goto err;
163 p_out->num_perms = 0;
164 print_unknown_handle = true;
165 continue;
166 }
167
168 k = 0;
Matthias Kaehlcke342e9152017-03-16 15:26:52 -0700169 while (p_in->perms[k]) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400170 /* An empty permission string skips ahead */
171 if (!*p_in->perms[k]) {
172 k++;
173 continue;
174 }
175 p_out->perms[k] = string_to_av_perm(pol, p_out->value,
176 p_in->perms[k]);
177 if (!p_out->perms[k]) {
178 printk(KERN_INFO
179 "SELinux: Permission %s in class %s not defined in policy.\n",
180 p_in->perms[k], p_in->name);
181 if (pol->reject_unknown)
182 goto err;
183 print_unknown_handle = true;
184 }
185
186 k++;
187 }
188 p_out->num_perms = k;
189 }
190
191 if (print_unknown_handle)
192 printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n",
193 pol->allow_unknown ? "allowed" : "denied");
194
195 *out_map_p = out_map;
196 *out_map_size = i;
197 return 0;
198err:
199 kfree(out_map);
200 return -EINVAL;
201}
202
203/*
204 * Get real, policy values from mapped values
205 */
206
207static u16 unmap_class(u16 tclass)
208{
209 if (tclass < current_mapping_size)
210 return current_mapping[tclass].value;
211
212 return tclass;
213}
214
Harry Ciao6f5317e2011-03-02 13:32:33 +0800215/*
216 * Get kernel value for class from its policy value
217 */
218static u16 map_class(u16 pol_value)
219{
220 u16 i;
221
222 for (i = 1; i < current_mapping_size; i++) {
223 if (current_mapping[i].value == pol_value)
224 return i;
225 }
226
Stephen Smalley85cd6da2011-03-25 10:13:43 -0400227 return SECCLASS_NULL;
Harry Ciao6f5317e2011-03-02 13:32:33 +0800228}
229
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400230static void map_decision(u16 tclass, struct av_decision *avd,
231 int allow_unknown)
232{
233 if (tclass < current_mapping_size) {
234 unsigned i, n = current_mapping[tclass].num_perms;
235 u32 result;
236
237 for (i = 0, result = 0; i < n; i++) {
238 if (avd->allowed & current_mapping[tclass].perms[i])
239 result |= 1<<i;
240 if (allow_unknown && !current_mapping[tclass].perms[i])
241 result |= 1<<i;
242 }
243 avd->allowed = result;
244
245 for (i = 0, result = 0; i < n; i++)
246 if (avd->auditallow & current_mapping[tclass].perms[i])
247 result |= 1<<i;
248 avd->auditallow = result;
249
250 for (i = 0, result = 0; i < n; i++) {
251 if (avd->auditdeny & current_mapping[tclass].perms[i])
252 result |= 1<<i;
253 if (!allow_unknown && !current_mapping[tclass].perms[i])
254 result |= 1<<i;
255 }
Eric Paris0bce9522009-11-23 16:47:23 -0500256 /*
257 * In case the kernel has a bug and requests a permission
258 * between num_perms and the maximum permission number, we
259 * should audit that denial
260 */
261 for (; i < (sizeof(u32)*8); i++)
262 result |= 1<<i;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400263 avd->auditdeny = result;
264 }
265}
266
Guido Trentalancia0719aaf2010-02-03 16:40:20 +0100267int security_mls_enabled(void)
268{
269 return policydb.mls_enabled;
270}
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
273 * Return the boolean value of a constraint expression
274 * when it is applied to the specified source and target
275 * security contexts.
276 *
277 * xcontext is a special beast... It is used by the validatetrans rules
278 * only. For these rules, scontext is the context before the transition,
279 * tcontext is the context after the transition, and xcontext is the context
280 * of the process performing the transition. All other callers of
281 * constraint_expr_eval should pass in NULL for xcontext.
282 */
283static int constraint_expr_eval(struct context *scontext,
284 struct context *tcontext,
285 struct context *xcontext,
286 struct constraint_expr *cexpr)
287{
288 u32 val1, val2;
289 struct context *c;
290 struct role_datum *r1, *r2;
291 struct mls_level *l1, *l2;
292 struct constraint_expr *e;
293 int s[CEXPR_MAXDEPTH];
294 int sp = -1;
295
296 for (e = cexpr; e; e = e->next) {
297 switch (e->expr_type) {
298 case CEXPR_NOT:
299 BUG_ON(sp < 0);
300 s[sp] = !s[sp];
301 break;
302 case CEXPR_AND:
303 BUG_ON(sp < 1);
304 sp--;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800305 s[sp] &= s[sp + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 break;
307 case CEXPR_OR:
308 BUG_ON(sp < 1);
309 sp--;
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800310 s[sp] |= s[sp + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 break;
312 case CEXPR_ATTR:
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +0800313 if (sp == (CEXPR_MAXDEPTH - 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315 switch (e->attr) {
316 case CEXPR_USER:
317 val1 = scontext->user;
318 val2 = tcontext->user;
319 break;
320 case CEXPR_TYPE:
321 val1 = scontext->type;
322 val2 = tcontext->type;
323 break;
324 case CEXPR_ROLE:
325 val1 = scontext->role;
326 val2 = tcontext->role;
327 r1 = policydb.role_val_to_struct[val1 - 1];
328 r2 = policydb.role_val_to_struct[val2 - 1];
329 switch (e->op) {
330 case CEXPR_DOM:
331 s[++sp] = ebitmap_get_bit(&r1->dominates,
332 val2 - 1);
333 continue;
334 case CEXPR_DOMBY:
335 s[++sp] = ebitmap_get_bit(&r2->dominates,
336 val1 - 1);
337 continue;
338 case CEXPR_INCOMP:
Eric Paris5d55a342008-04-18 17:38:33 -0400339 s[++sp] = (!ebitmap_get_bit(&r1->dominates,
340 val2 - 1) &&
341 !ebitmap_get_bit(&r2->dominates,
342 val1 - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 continue;
344 default:
345 break;
346 }
347 break;
348 case CEXPR_L1L2:
349 l1 = &(scontext->range.level[0]);
350 l2 = &(tcontext->range.level[0]);
351 goto mls_ops;
352 case CEXPR_L1H2:
353 l1 = &(scontext->range.level[0]);
354 l2 = &(tcontext->range.level[1]);
355 goto mls_ops;
356 case CEXPR_H1L2:
357 l1 = &(scontext->range.level[1]);
358 l2 = &(tcontext->range.level[0]);
359 goto mls_ops;
360 case CEXPR_H1H2:
361 l1 = &(scontext->range.level[1]);
362 l2 = &(tcontext->range.level[1]);
363 goto mls_ops;
364 case CEXPR_L1H1:
365 l1 = &(scontext->range.level[0]);
366 l2 = &(scontext->range.level[1]);
367 goto mls_ops;
368 case CEXPR_L2H2:
369 l1 = &(tcontext->range.level[0]);
370 l2 = &(tcontext->range.level[1]);
371 goto mls_ops;
372mls_ops:
373 switch (e->op) {
374 case CEXPR_EQ:
375 s[++sp] = mls_level_eq(l1, l2);
376 continue;
377 case CEXPR_NEQ:
378 s[++sp] = !mls_level_eq(l1, l2);
379 continue;
380 case CEXPR_DOM:
381 s[++sp] = mls_level_dom(l1, l2);
382 continue;
383 case CEXPR_DOMBY:
384 s[++sp] = mls_level_dom(l2, l1);
385 continue;
386 case CEXPR_INCOMP:
387 s[++sp] = mls_level_incomp(l2, l1);
388 continue;
389 default:
390 BUG();
391 return 0;
392 }
393 break;
394 default:
395 BUG();
396 return 0;
397 }
398
399 switch (e->op) {
400 case CEXPR_EQ:
401 s[++sp] = (val1 == val2);
402 break;
403 case CEXPR_NEQ:
404 s[++sp] = (val1 != val2);
405 break;
406 default:
407 BUG();
408 return 0;
409 }
410 break;
411 case CEXPR_NAMES:
412 if (sp == (CEXPR_MAXDEPTH-1))
413 return 0;
414 c = scontext;
415 if (e->attr & CEXPR_TARGET)
416 c = tcontext;
417 else if (e->attr & CEXPR_XTARGET) {
418 c = xcontext;
419 if (!c) {
420 BUG();
421 return 0;
422 }
423 }
424 if (e->attr & CEXPR_USER)
425 val1 = c->user;
426 else if (e->attr & CEXPR_ROLE)
427 val1 = c->role;
428 else if (e->attr & CEXPR_TYPE)
429 val1 = c->type;
430 else {
431 BUG();
432 return 0;
433 }
434
435 switch (e->op) {
436 case CEXPR_EQ:
437 s[++sp] = ebitmap_get_bit(&e->names, val1 - 1);
438 break;
439 case CEXPR_NEQ:
440 s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1);
441 break;
442 default:
443 BUG();
444 return 0;
445 }
446 break;
447 default:
448 BUG();
449 return 0;
450 }
451 }
452
453 BUG_ON(sp != 0);
454 return s[0];
455}
456
457/*
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900458 * security_dump_masked_av - dumps masked permissions during
459 * security_compute_av due to RBAC, MLS/Constraint and Type bounds.
460 */
461static int dump_masked_av_helper(void *k, void *d, void *args)
462{
463 struct perm_datum *pdatum = d;
464 char **permission_names = args;
465
466 BUG_ON(pdatum->value < 1 || pdatum->value > 32);
467
468 permission_names[pdatum->value - 1] = (char *)k;
469
470 return 0;
471}
472
473static void security_dump_masked_av(struct context *scontext,
474 struct context *tcontext,
475 u16 tclass,
476 u32 permissions,
477 const char *reason)
478{
479 struct common_datum *common_dat;
480 struct class_datum *tclass_dat;
481 struct audit_buffer *ab;
482 char *tclass_name;
483 char *scontext_name = NULL;
484 char *tcontext_name = NULL;
485 char *permission_names[32];
James Morris2da5d312010-02-16 17:29:06 +1100486 int index;
487 u32 length;
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900488 bool need_comma = false;
489
490 if (!permissions)
491 return;
492
Eric Parisac76c052010-11-29 15:47:09 -0500493 tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1);
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900494 tclass_dat = policydb.class_val_to_struct[tclass - 1];
495 common_dat = tclass_dat->comdatum;
496
497 /* init permission_names */
498 if (common_dat &&
499 hashtab_map(common_dat->permissions.table,
500 dump_masked_av_helper, permission_names) < 0)
501 goto out;
502
503 if (hashtab_map(tclass_dat->permissions.table,
504 dump_masked_av_helper, permission_names) < 0)
505 goto out;
506
507 /* get scontext/tcontext in text form */
508 if (context_struct_to_string(scontext,
509 &scontext_name, &length) < 0)
510 goto out;
511
512 if (context_struct_to_string(tcontext,
513 &tcontext_name, &length) < 0)
514 goto out;
515
516 /* audit a message */
517 ab = audit_log_start(current->audit_context,
518 GFP_ATOMIC, AUDIT_SELINUX_ERR);
519 if (!ab)
520 goto out;
521
522 audit_log_format(ab, "op=security_compute_av reason=%s "
523 "scontext=%s tcontext=%s tclass=%s perms=",
524 reason, scontext_name, tcontext_name, tclass_name);
525
526 for (index = 0; index < 32; index++) {
527 u32 mask = (1 << index);
528
529 if ((mask & permissions) == 0)
530 continue;
531
532 audit_log_format(ab, "%s%s",
533 need_comma ? "," : "",
534 permission_names[index]
535 ? permission_names[index] : "????");
536 need_comma = true;
537 }
538 audit_log_end(ab);
539out:
540 /* release scontext/tcontext */
541 kfree(tcontext_name);
542 kfree(scontext_name);
543
544 return;
545}
546
547/*
KaiGai Koheid9250de2008-08-28 16:35:57 +0900548 * security_boundary_permission - drops violated permissions
549 * on boundary constraint.
550 */
551static void type_attribute_bounds_av(struct context *scontext,
552 struct context *tcontext,
553 u16 tclass,
KaiGai Koheid9250de2008-08-28 16:35:57 +0900554 struct av_decision *avd)
555{
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900556 struct context lo_scontext;
Stephen Smalley7ea59202016-05-23 10:54:11 -0400557 struct context lo_tcontext, *tcontextp = tcontext;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900558 struct av_decision lo_avd;
Eric Paris23bdecb2010-11-29 15:47:09 -0500559 struct type_datum *source;
560 struct type_datum *target;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900561 u32 masked = 0;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900562
Eric Paris23bdecb2010-11-29 15:47:09 -0500563 source = flex_array_get_ptr(policydb.type_val_to_struct_array,
564 scontext->type - 1);
565 BUG_ON(!source);
566
Stephen Smalley7ea59202016-05-23 10:54:11 -0400567 if (!source->bounds)
568 return;
569
Eric Paris23bdecb2010-11-29 15:47:09 -0500570 target = flex_array_get_ptr(policydb.type_val_to_struct_array,
571 tcontext->type - 1);
572 BUG_ON(!target);
573
Stephen Smalley7ea59202016-05-23 10:54:11 -0400574 memset(&lo_avd, 0, sizeof(lo_avd));
KaiGai Koheid9250de2008-08-28 16:35:57 +0900575
Stephen Smalley7ea59202016-05-23 10:54:11 -0400576 memcpy(&lo_scontext, scontext, sizeof(lo_scontext));
577 lo_scontext.type = source->bounds;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900578
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900579 if (target->bounds) {
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900580 memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext));
581 lo_tcontext.type = target->bounds;
Stephen Smalley7ea59202016-05-23 10:54:11 -0400582 tcontextp = &lo_tcontext;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900583 }
584
Stephen Smalley7ea59202016-05-23 10:54:11 -0400585 context_struct_compute_av(&lo_scontext,
586 tcontextp,
587 tclass,
588 &lo_avd,
589 NULL);
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900590
Stephen Smalley7ea59202016-05-23 10:54:11 -0400591 masked = ~lo_avd.allowed & avd->allowed;
KaiGai Kohei2ae3ba32010-02-17 08:49:41 +0900592
Stephen Smalley7ea59202016-05-23 10:54:11 -0400593 if (likely(!masked))
594 return; /* no masked permission */
KaiGai Koheid9250de2008-08-28 16:35:57 +0900595
Stephen Smalley7ea59202016-05-23 10:54:11 -0400596 /* mask violated permissions */
597 avd->allowed &= ~masked;
598
599 /* audit masked permissions */
600 security_dump_masked_av(scontext, tcontext,
601 tclass, masked, "bounds");
KaiGai Koheid9250de2008-08-28 16:35:57 +0900602}
603
604/*
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400605 * flag which drivers have permissions
606 * only looking for ioctl based extended permssions
607 */
608void services_compute_xperms_drivers(
609 struct extended_perms *xperms,
610 struct avtab_node *node)
611{
612 unsigned int i;
613
614 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
615 /* if one or more driver has all permissions allowed */
616 for (i = 0; i < ARRAY_SIZE(xperms->drivers.p); i++)
617 xperms->drivers.p[i] |= node->datum.u.xperms->perms.p[i];
618 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
619 /* if allowing permissions within a driver */
620 security_xperm_set(xperms->drivers.p,
621 node->datum.u.xperms->driver);
622 }
623
624 /* If no ioctl commands are allowed, ignore auditallow and auditdeny */
625 if (node->key.specified & AVTAB_XPERMS_ALLOWED)
626 xperms->len = 1;
627}
628
629/*
630 * Compute access vectors and extended permissions based on a context
631 * structure pair for the permissions in a particular class.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Stephen Smalley19439d02010-01-14 17:28:10 -0500633static void context_struct_compute_av(struct context *scontext,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400634 struct context *tcontext,
635 u16 tclass,
636 struct av_decision *avd,
637 struct extended_perms *xperms)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
639 struct constraint_node *constraint;
640 struct role_allow *ra;
641 struct avtab_key avkey;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700642 struct avtab_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 struct class_datum *tclass_datum;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700644 struct ebitmap *sattr, *tattr;
645 struct ebitmap_node *snode, *tnode;
646 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 avd->allowed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 avd->auditallow = 0;
650 avd->auditdeny = 0xffffffff;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400651 if (xperms) {
652 memset(&xperms->drivers, 0, sizeof(xperms->drivers));
653 xperms->len = 0;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400656 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
657 if (printk_ratelimit())
658 printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass);
Stephen Smalley19439d02010-01-14 17:28:10 -0500659 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400660 }
Eric Paris3f120702007-09-21 14:37:10 -0400661
662 tclass_datum = policydb.class_val_to_struct[tclass - 1];
663
664 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * If a specific type enforcement rule was defined for
666 * this permission check, then use it.
667 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 avkey.target_class = tclass;
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400669 avkey.specified = AVTAB_AV | AVTAB_XPERMS;
Eric Paris6371dcd2010-07-29 23:02:34 -0400670 sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1);
671 BUG_ON(!sattr);
672 tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1);
673 BUG_ON(!tattr);
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +0900674 ebitmap_for_each_positive_bit(sattr, snode, i) {
675 ebitmap_for_each_positive_bit(tattr, tnode, j) {
Stephen Smalley782ebb92005-09-03 15:55:16 -0700676 avkey.source_type = i + 1;
677 avkey.target_type = j + 1;
678 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +0300679 node;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700680 node = avtab_search_node_next(node, avkey.specified)) {
681 if (node->key.specified == AVTAB_ALLOWED)
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400682 avd->allowed |= node->datum.u.data;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700683 else if (node->key.specified == AVTAB_AUDITALLOW)
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400684 avd->auditallow |= node->datum.u.data;
Stephen Smalley782ebb92005-09-03 15:55:16 -0700685 else if (node->key.specified == AVTAB_AUDITDENY)
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400686 avd->auditdeny &= node->datum.u.data;
687 else if (xperms && (node->key.specified & AVTAB_XPERMS))
688 services_compute_xperms_drivers(xperms, node);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Stephen Smalley782ebb92005-09-03 15:55:16 -0700691 /* Check conditional av table for additional permissions */
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400692 cond_compute_av(&policydb.te_cond_avtab, &avkey,
693 avd, xperms);
Stephen Smalley782ebb92005-09-03 15:55:16 -0700694
695 }
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 /*
699 * Remove any permissions prohibited by a constraint (this includes
700 * the MLS policy).
701 */
702 constraint = tclass_datum->constraints;
703 while (constraint) {
704 if ((constraint->permissions & (avd->allowed)) &&
705 !constraint_expr_eval(scontext, tcontext, NULL,
706 constraint->expr)) {
KaiGai Koheicaabbdc2009-06-18 17:30:07 +0900707 avd->allowed &= ~(constraint->permissions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709 constraint = constraint->next;
710 }
711
712 /*
713 * If checking process transition permission and the
714 * role is changing, then check the (current_role, new_role)
715 * pair.
716 */
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400717 if (tclass == policydb.process_class &&
718 (avd->allowed & policydb.process_trans_perms) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 scontext->role != tcontext->role) {
720 for (ra = policydb.role_allow; ra; ra = ra->next) {
721 if (scontext->role == ra->role &&
722 tcontext->role == ra->new_role)
723 break;
724 }
725 if (!ra)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400726 avd->allowed &= ~policydb.process_trans_perms;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
KaiGai Koheid9250de2008-08-28 16:35:57 +0900729 /*
730 * If the given source and target types have boundary
731 * constraint, lazy checks have to mask any violated
732 * permission and notice it to userspace via audit.
733 */
734 type_attribute_bounds_av(scontext, tcontext,
Stephen Smalley19439d02010-01-14 17:28:10 -0500735 tclass, avd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
738static int security_validtrans_handle_fail(struct context *ocontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400739 struct context *ncontext,
740 struct context *tcontext,
741 u16 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 char *o = NULL, *n = NULL, *t = NULL;
744 u32 olen, nlen, tlen;
745
Eric Paris4b02b522010-11-23 11:40:08 -0500746 if (context_struct_to_string(ocontext, &o, &olen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -0500748 if (context_struct_to_string(ncontext, &n, &nlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -0500750 if (context_struct_to_string(tcontext, &t, &tlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100752 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Richard Guy Briggs4093a842014-09-18 20:47:48 -0400753 "op=security_validate_transition seresult=denied"
Eric Paris5d55a342008-04-18 17:38:33 -0400754 " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s",
Eric Parisac76c052010-11-29 15:47:09 -0500755 o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756out:
757 kfree(o);
758 kfree(n);
759 kfree(t);
760
761 if (!selinux_enforcing)
762 return 0;
763 return -EPERM;
764}
765
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500766static int security_compute_validatetrans(u32 oldsid, u32 newsid, u32 tasksid,
767 u16 orig_tclass, bool user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 struct context *ocontext;
770 struct context *ncontext;
771 struct context *tcontext;
772 struct class_datum *tclass_datum;
773 struct constraint_node *constraint;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400774 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 int rc = 0;
776
777 if (!ss_initialized)
778 return 0;
779
James Morris0804d112008-06-06 18:40:29 +1000780 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500782 if (!user)
783 tclass = unmap_class(orig_tclass);
784 else
785 tclass = orig_tclass;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!tclass || tclass > policydb.p_classes.nprim) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 rc = -EINVAL;
789 goto out;
790 }
791 tclass_datum = policydb.class_val_to_struct[tclass - 1];
792
793 ocontext = sidtab_search(&sidtab, oldsid);
794 if (!ocontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400795 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
796 __func__, oldsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 rc = -EINVAL;
798 goto out;
799 }
800
801 ncontext = sidtab_search(&sidtab, newsid);
802 if (!ncontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400803 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
804 __func__, newsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 rc = -EINVAL;
806 goto out;
807 }
808
809 tcontext = sidtab_search(&sidtab, tasksid);
810 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -0400811 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
812 __func__, tasksid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 rc = -EINVAL;
814 goto out;
815 }
816
817 constraint = tclass_datum->validatetrans;
818 while (constraint) {
819 if (!constraint_expr_eval(ocontext, ncontext, tcontext,
Eric Paris5d55a342008-04-18 17:38:33 -0400820 constraint->expr)) {
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500821 if (user)
822 rc = -EPERM;
823 else
824 rc = security_validtrans_handle_fail(ocontext,
825 ncontext,
826 tcontext,
827 tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 goto out;
829 }
830 constraint = constraint->next;
831 }
832
833out:
James Morris0804d112008-06-06 18:40:29 +1000834 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return rc;
836}
837
Andrew Perepechkof9df6452015-12-24 11:09:41 -0500838int security_validate_transition_user(u32 oldsid, u32 newsid, u32 tasksid,
839 u16 tclass)
840{
841 return security_compute_validatetrans(oldsid, newsid, tasksid,
842 tclass, true);
843}
844
845int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,
846 u16 orig_tclass)
847{
848 return security_compute_validatetrans(oldsid, newsid, tasksid,
849 orig_tclass, false);
850}
851
KaiGai Koheid9250de2008-08-28 16:35:57 +0900852/*
853 * security_bounded_transition - check whether the given
854 * transition is directed to bounded, or not.
855 * It returns 0, if @newsid is bounded by @oldsid.
856 * Otherwise, it returns error code.
857 *
858 * @oldsid : current security identifier
859 * @newsid : destinated security identifier
860 */
861int security_bounded_transition(u32 old_sid, u32 new_sid)
862{
863 struct context *old_context, *new_context;
864 struct type_datum *type;
865 int index;
Eric Paris4b02b522010-11-23 11:40:08 -0500866 int rc;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900867
868 read_lock(&policy_rwlock);
869
Eric Paris4b02b522010-11-23 11:40:08 -0500870 rc = -EINVAL;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900871 old_context = sidtab_search(&sidtab, old_sid);
872 if (!old_context) {
873 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
874 __func__, old_sid);
875 goto out;
876 }
877
Eric Paris4b02b522010-11-23 11:40:08 -0500878 rc = -EINVAL;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900879 new_context = sidtab_search(&sidtab, new_sid);
880 if (!new_context) {
881 printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n",
882 __func__, new_sid);
883 goto out;
884 }
885
Eric Paris4b02b522010-11-23 11:40:08 -0500886 rc = 0;
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200887 /* type/domain unchanged */
Eric Paris4b02b522010-11-23 11:40:08 -0500888 if (old_context->type == new_context->type)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900889 goto out;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900890
891 index = new_context->type;
892 while (true) {
Eric Paris23bdecb2010-11-29 15:47:09 -0500893 type = flex_array_get_ptr(policydb.type_val_to_struct_array,
894 index - 1);
KaiGai Koheid9250de2008-08-28 16:35:57 +0900895 BUG_ON(!type);
896
897 /* not bounded anymore */
Eric Paris4b02b522010-11-23 11:40:08 -0500898 rc = -EPERM;
899 if (!type->bounds)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900900 break;
KaiGai Koheid9250de2008-08-28 16:35:57 +0900901
902 /* @newsid is bounded by @oldsid */
Eric Paris4b02b522010-11-23 11:40:08 -0500903 rc = 0;
904 if (type->bounds == old_context->type)
KaiGai Koheid9250de2008-08-28 16:35:57 +0900905 break;
Eric Paris4b02b522010-11-23 11:40:08 -0500906
KaiGai Koheid9250de2008-08-28 16:35:57 +0900907 index = type->bounds;
908 }
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900909
910 if (rc) {
911 char *old_name = NULL;
912 char *new_name = NULL;
James Morris2da5d312010-02-16 17:29:06 +1100913 u32 length;
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900914
915 if (!context_struct_to_string(old_context,
916 &old_name, &length) &&
917 !context_struct_to_string(new_context,
918 &new_name, &length)) {
919 audit_log(current->audit_context,
920 GFP_ATOMIC, AUDIT_SELINUX_ERR,
921 "op=security_bounded_transition "
Richard Guy Briggs4093a842014-09-18 20:47:48 -0400922 "seresult=denied "
KaiGai Kohei44c2d9b2009-06-18 17:26:13 +0900923 "oldcontext=%s newcontext=%s",
924 old_name, new_name);
925 }
926 kfree(new_name);
927 kfree(old_name);
928 }
KaiGai Koheid9250de2008-08-28 16:35:57 +0900929out:
930 read_unlock(&policy_rwlock);
931
932 return rc;
933}
934
Stephen Smalley19439d02010-01-14 17:28:10 -0500935static void avd_init(struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400936{
Stephen Smalley19439d02010-01-14 17:28:10 -0500937 avd->allowed = 0;
938 avd->auditallow = 0;
939 avd->auditdeny = 0xffffffff;
940 avd->seqno = latest_granting;
941 avd->flags = 0;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -0400942}
943
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -0400944void services_compute_xperms_decision(struct extended_perms_decision *xpermd,
945 struct avtab_node *node)
946{
947 unsigned int i;
948
949 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
950 if (xpermd->driver != node->datum.u.xperms->driver)
951 return;
952 } else if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
953 if (!security_xperm_test(node->datum.u.xperms->perms.p,
954 xpermd->driver))
955 return;
956 } else {
957 BUG();
958 }
959
960 if (node->key.specified == AVTAB_XPERMS_ALLOWED) {
961 xpermd->used |= XPERMS_ALLOWED;
962 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
963 memset(xpermd->allowed->p, 0xff,
964 sizeof(xpermd->allowed->p));
965 }
966 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
967 for (i = 0; i < ARRAY_SIZE(xpermd->allowed->p); i++)
968 xpermd->allowed->p[i] |=
969 node->datum.u.xperms->perms.p[i];
970 }
971 } else if (node->key.specified == AVTAB_XPERMS_AUDITALLOW) {
972 xpermd->used |= XPERMS_AUDITALLOW;
973 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
974 memset(xpermd->auditallow->p, 0xff,
975 sizeof(xpermd->auditallow->p));
976 }
977 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
978 for (i = 0; i < ARRAY_SIZE(xpermd->auditallow->p); i++)
979 xpermd->auditallow->p[i] |=
980 node->datum.u.xperms->perms.p[i];
981 }
982 } else if (node->key.specified == AVTAB_XPERMS_DONTAUDIT) {
983 xpermd->used |= XPERMS_DONTAUDIT;
984 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLDRIVER) {
985 memset(xpermd->dontaudit->p, 0xff,
986 sizeof(xpermd->dontaudit->p));
987 }
988 if (node->datum.u.xperms->specified == AVTAB_XPERMS_IOCTLFUNCTION) {
989 for (i = 0; i < ARRAY_SIZE(xpermd->dontaudit->p); i++)
990 xpermd->dontaudit->p[i] |=
991 node->datum.u.xperms->perms.p[i];
992 }
993 } else {
994 BUG();
995 }
996}
997
998void security_compute_xperms_decision(u32 ssid,
999 u32 tsid,
1000 u16 orig_tclass,
1001 u8 driver,
1002 struct extended_perms_decision *xpermd)
1003{
1004 u16 tclass;
1005 struct context *scontext, *tcontext;
1006 struct avtab_key avkey;
1007 struct avtab_node *node;
1008 struct ebitmap *sattr, *tattr;
1009 struct ebitmap_node *snode, *tnode;
1010 unsigned int i, j;
1011
1012 xpermd->driver = driver;
1013 xpermd->used = 0;
1014 memset(xpermd->allowed->p, 0, sizeof(xpermd->allowed->p));
1015 memset(xpermd->auditallow->p, 0, sizeof(xpermd->auditallow->p));
1016 memset(xpermd->dontaudit->p, 0, sizeof(xpermd->dontaudit->p));
1017
1018 read_lock(&policy_rwlock);
1019 if (!ss_initialized)
1020 goto allow;
1021
1022 scontext = sidtab_search(&sidtab, ssid);
1023 if (!scontext) {
1024 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1025 __func__, ssid);
1026 goto out;
1027 }
1028
1029 tcontext = sidtab_search(&sidtab, tsid);
1030 if (!tcontext) {
1031 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1032 __func__, tsid);
1033 goto out;
1034 }
1035
1036 tclass = unmap_class(orig_tclass);
1037 if (unlikely(orig_tclass && !tclass)) {
1038 if (policydb.allow_unknown)
1039 goto allow;
1040 goto out;
1041 }
1042
1043
1044 if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) {
1045 pr_warn_ratelimited("SELinux: Invalid class %hu\n", tclass);
1046 goto out;
1047 }
1048
1049 avkey.target_class = tclass;
1050 avkey.specified = AVTAB_XPERMS;
1051 sattr = flex_array_get(policydb.type_attr_map_array,
1052 scontext->type - 1);
1053 BUG_ON(!sattr);
1054 tattr = flex_array_get(policydb.type_attr_map_array,
1055 tcontext->type - 1);
1056 BUG_ON(!tattr);
1057 ebitmap_for_each_positive_bit(sattr, snode, i) {
1058 ebitmap_for_each_positive_bit(tattr, tnode, j) {
1059 avkey.source_type = i + 1;
1060 avkey.target_type = j + 1;
1061 for (node = avtab_search_node(&policydb.te_avtab, &avkey);
1062 node;
1063 node = avtab_search_node_next(node, avkey.specified))
1064 services_compute_xperms_decision(xpermd, node);
1065
1066 cond_compute_xperms(&policydb.te_cond_avtab,
1067 &avkey, xpermd);
1068 }
1069 }
1070out:
1071 read_unlock(&policy_rwlock);
1072 return;
1073allow:
1074 memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p));
1075 goto out;
1076}
Stephen Smalley19439d02010-01-14 17:28:10 -05001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/**
1079 * security_compute_av - Compute access vector decisions.
1080 * @ssid: source security identifier
1081 * @tsid: target security identifier
1082 * @tclass: target security class
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 * @avd: access vector decisions
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001084 * @xperms: extended permissions
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 *
1086 * Compute a set of access vector decisions based on the
1087 * SID pair (@ssid, @tsid) for the permissions in @tclass.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
Stephen Smalley19439d02010-01-14 17:28:10 -05001089void security_compute_av(u32 ssid,
1090 u32 tsid,
1091 u16 orig_tclass,
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001092 struct av_decision *avd,
1093 struct extended_perms *xperms)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001095 u16 tclass;
Stephen Smalley19439d02010-01-14 17:28:10 -05001096 struct context *scontext = NULL, *tcontext = NULL;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001097
Stephen Smalleyb7f30082009-10-19 10:08:50 -04001098 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -05001099 avd_init(avd);
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001100 xperms->len = 0;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001101 if (!ss_initialized)
1102 goto allow;
1103
Stephen Smalley19439d02010-01-14 17:28:10 -05001104 scontext = sidtab_search(&sidtab, ssid);
1105 if (!scontext) {
1106 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1107 __func__, ssid);
1108 goto out;
1109 }
1110
1111 /* permissive domain? */
1112 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
1113 avd->flags |= AVD_FLAGS_PERMISSIVE;
1114
1115 tcontext = sidtab_search(&sidtab, tsid);
1116 if (!tcontext) {
1117 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1118 __func__, tsid);
1119 goto out;
1120 }
1121
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001122 tclass = unmap_class(orig_tclass);
1123 if (unlikely(orig_tclass && !tclass)) {
1124 if (policydb.allow_unknown)
1125 goto allow;
Stephen Smalleyb7f30082009-10-19 10:08:50 -04001126 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001127 }
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001128 context_struct_compute_av(scontext, tcontext, tclass, avd, xperms);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001129 map_decision(orig_tclass, avd, policydb.allow_unknown);
Stephen Smalleyb7f30082009-10-19 10:08:50 -04001130out:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001131 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -05001132 return;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001133allow:
1134 avd->allowed = 0xffffffff;
Stephen Smalleyb7f30082009-10-19 10:08:50 -04001135 goto out;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001136}
1137
Stephen Smalley19439d02010-01-14 17:28:10 -05001138void security_compute_av_user(u32 ssid,
1139 u32 tsid,
1140 u16 tclass,
1141 struct av_decision *avd)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001142{
Stephen Smalley19439d02010-01-14 17:28:10 -05001143 struct context *scontext = NULL, *tcontext = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
James Morris0804d112008-06-06 18:40:29 +10001145 read_lock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -05001146 avd_init(avd);
1147 if (!ss_initialized)
1148 goto allow;
1149
1150 scontext = sidtab_search(&sidtab, ssid);
1151 if (!scontext) {
1152 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1153 __func__, ssid);
1154 goto out;
1155 }
1156
1157 /* permissive domain? */
1158 if (ebitmap_get_bit(&policydb.permissive_map, scontext->type))
1159 avd->flags |= AVD_FLAGS_PERMISSIVE;
1160
1161 tcontext = sidtab_search(&sidtab, tsid);
1162 if (!tcontext) {
1163 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1164 __func__, tsid);
1165 goto out;
1166 }
1167
1168 if (unlikely(!tclass)) {
1169 if (policydb.allow_unknown)
1170 goto allow;
1171 goto out;
1172 }
1173
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001174 context_struct_compute_av(scontext, tcontext, tclass, avd, NULL);
Stephen Smalley19439d02010-01-14 17:28:10 -05001175 out:
James Morris0804d112008-06-06 18:40:29 +10001176 read_unlock(&policy_rwlock);
Stephen Smalley19439d02010-01-14 17:28:10 -05001177 return;
1178allow:
1179 avd->allowed = 0xffffffff;
1180 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
1183/*
1184 * Write the security context string representation of
1185 * the context structure `context' into a dynamically
1186 * allocated string of the correct size. Set `*scontext'
1187 * to point to this string and set `*scontext_len' to
1188 * the length of the string.
1189 */
1190static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len)
1191{
1192 char *scontextp;
1193
Eric Parisd5630b92010-10-13 16:24:48 -04001194 if (scontext)
1195 *scontext = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 *scontext_len = 0;
1197
Stephen Smalley12b29f32008-05-07 13:03:20 -04001198 if (context->len) {
1199 *scontext_len = context->len;
Eric Parisbb7081a2012-04-04 13:46:36 -04001200 if (scontext) {
1201 *scontext = kstrdup(context->str, GFP_ATOMIC);
1202 if (!(*scontext))
1203 return -ENOMEM;
1204 }
Stephen Smalley12b29f32008-05-07 13:03:20 -04001205 return 0;
1206 }
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /* Compute the size of the context. */
Eric Parisac76c052010-11-29 15:47:09 -05001209 *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1;
1210 *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1;
1211 *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *scontext_len += mls_compute_context_len(context);
1213
Eric Parisd5630b92010-10-13 16:24:48 -04001214 if (!scontext)
1215 return 0;
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 /* Allocate space for the context; caller must free this space. */
1218 scontextp = kmalloc(*scontext_len, GFP_ATOMIC);
Eric Paris5d55a342008-04-18 17:38:33 -04001219 if (!scontextp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 *scontext = scontextp;
1222
1223 /*
1224 * Copy the user name, role name and type name into the context.
1225 */
Rasmus Villemoes9529c782015-10-21 17:44:27 -04001226 scontextp += sprintf(scontextp, "%s:%s:%s",
Eric Parisac76c052010-11-29 15:47:09 -05001227 sym_name(&policydb, SYM_USERS, context->user - 1),
1228 sym_name(&policydb, SYM_ROLES, context->role - 1),
1229 sym_name(&policydb, SYM_TYPES, context->type - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 mls_sid_to_context(context, &scontextp);
1232
1233 *scontextp = 0;
1234
1235 return 0;
1236}
1237
1238#include "initial_sid_to_string.h"
1239
James Carterf0ee2e42007-04-04 10:11:29 -04001240const char *security_get_initial_sid_context(u32 sid)
1241{
1242 if (unlikely(sid > SECINITSID_NUM))
1243 return NULL;
1244 return initial_sid_to_string[sid];
1245}
1246
Stephen Smalley12b29f32008-05-07 13:03:20 -04001247static int security_sid_to_context_core(u32 sid, char **scontext,
1248 u32 *scontext_len, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
1250 struct context *context;
1251 int rc = 0;
1252
Eric Parisd5630b92010-10-13 16:24:48 -04001253 if (scontext)
1254 *scontext = NULL;
Stephen Smalley4f4acf32007-02-26 12:02:34 -05001255 *scontext_len = 0;
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (!ss_initialized) {
1258 if (sid <= SECINITSID_NUM) {
1259 char *scontextp;
1260
1261 *scontext_len = strlen(initial_sid_to_string[sid]) + 1;
Eric Parisd5630b92010-10-13 16:24:48 -04001262 if (!scontext)
1263 goto out;
Rasmus Villemoesaa736c32015-10-21 17:44:26 -04001264 scontextp = kmemdup(initial_sid_to_string[sid],
1265 *scontext_len, GFP_ATOMIC);
Serge E. Hallyn0cccca062006-05-15 09:43:48 -07001266 if (!scontextp) {
1267 rc = -ENOMEM;
1268 goto out;
1269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 *scontext = scontextp;
1271 goto out;
1272 }
Eric Paris744ba352008-04-17 11:52:44 -04001273 printk(KERN_ERR "SELinux: %s: called before initial "
1274 "load_policy on unknown SID %d\n", __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 rc = -EINVAL;
1276 goto out;
1277 }
James Morris0804d112008-06-06 18:40:29 +10001278 read_lock(&policy_rwlock);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001279 if (force)
1280 context = sidtab_search_force(&sidtab, sid);
1281 else
1282 context = sidtab_search(&sidtab, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (!context) {
Eric Paris744ba352008-04-17 11:52:44 -04001284 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1285 __func__, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 rc = -EINVAL;
1287 goto out_unlock;
1288 }
1289 rc = context_struct_to_string(context, scontext, scontext_len);
1290out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001291 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292out:
1293 return rc;
1294
1295}
1296
Stephen Smalley12b29f32008-05-07 13:03:20 -04001297/**
1298 * security_sid_to_context - Obtain a context for a given SID.
1299 * @sid: security identifier, SID
1300 * @scontext: security context
1301 * @scontext_len: length in bytes
1302 *
1303 * Write the string representation of the context associated with @sid
1304 * into a dynamically allocated string of the correct size. Set @scontext
1305 * to point to this string and set @scontext_len to the length of the string.
1306 */
1307int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
Stephen Smalley12b29f32008-05-07 13:03:20 -04001309 return security_sid_to_context_core(sid, scontext, scontext_len, 0);
1310}
1311
1312int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len)
1313{
1314 return security_sid_to_context_core(sid, scontext, scontext_len, 1);
1315}
1316
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001317/*
1318 * Caveat: Mutates scontext.
1319 */
Stephen Smalley12b29f32008-05-07 13:03:20 -04001320static int string_to_context_struct(struct policydb *pol,
1321 struct sidtab *sidtabp,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001322 char *scontext,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001323 u32 scontext_len,
1324 struct context *ctx,
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001325 u32 def_sid)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001326{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 struct role_datum *role;
1328 struct type_datum *typdatum;
1329 struct user_datum *usrdatum;
1330 char *scontextp, *p, oldc;
1331 int rc = 0;
1332
Stephen Smalley12b29f32008-05-07 13:03:20 -04001333 context_init(ctx);
1334
Stephen Smalley12b29f32008-05-07 13:03:20 -04001335 /* Parse the security context. */
1336
1337 rc = -EINVAL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001338 scontextp = (char *) scontext;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001339
1340 /* Extract the user. */
1341 p = scontextp;
1342 while (*p && *p != ':')
1343 p++;
1344
1345 if (*p == 0)
1346 goto out;
1347
1348 *p++ = 0;
1349
1350 usrdatum = hashtab_search(pol->p_users.table, scontextp);
1351 if (!usrdatum)
1352 goto out;
1353
1354 ctx->user = usrdatum->value;
1355
1356 /* Extract role. */
1357 scontextp = p;
1358 while (*p && *p != ':')
1359 p++;
1360
1361 if (*p == 0)
1362 goto out;
1363
1364 *p++ = 0;
1365
1366 role = hashtab_search(pol->p_roles.table, scontextp);
1367 if (!role)
1368 goto out;
1369 ctx->role = role->value;
1370
1371 /* Extract type. */
1372 scontextp = p;
1373 while (*p && *p != ':')
1374 p++;
1375 oldc = *p;
1376 *p++ = 0;
1377
1378 typdatum = hashtab_search(pol->p_types.table, scontextp);
KaiGai Koheid9250de2008-08-28 16:35:57 +09001379 if (!typdatum || typdatum->attribute)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001380 goto out;
1381
1382 ctx->type = typdatum->value;
1383
1384 rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid);
1385 if (rc)
1386 goto out;
1387
Eric Paris4b02b522010-11-23 11:40:08 -05001388 rc = -EINVAL;
1389 if ((p - scontext) < scontext_len)
Stephen Smalley12b29f32008-05-07 13:03:20 -04001390 goto out;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001391
1392 /* Check the validity of the new context. */
Eric Paris4b02b522010-11-23 11:40:08 -05001393 if (!policydb_context_isvalid(pol, ctx))
Stephen Smalley12b29f32008-05-07 13:03:20 -04001394 goto out;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001395 rc = 0;
1396out:
Eric Paris8e531af2008-09-03 11:49:47 -04001397 if (rc)
1398 context_destroy(ctx);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001399 return rc;
1400}
1401
1402static int security_context_to_sid_core(const char *scontext, u32 scontext_len,
1403 u32 *sid, u32 def_sid, gfp_t gfp_flags,
1404 int force)
1405{
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001406 char *scontext2, *str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001407 struct context context;
1408 int rc = 0;
1409
Stephen Smalley2172fa72014-01-30 11:26:59 -05001410 /* An empty security context is never valid. */
1411 if (!scontext_len)
1412 return -EINVAL;
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (!ss_initialized) {
1415 int i;
1416
1417 for (i = 1; i < SECINITSID_NUM; i++) {
1418 if (!strcmp(initial_sid_to_string[i], scontext)) {
1419 *sid = i;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001420 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422 }
1423 *sid = SECINITSID_KERNEL;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001424 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
1426 *sid = SECSID_NULL;
1427
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001428 /* Copy the string so that we can modify the copy as we parse it. */
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08001429 scontext2 = kmalloc(scontext_len + 1, gfp_flags);
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001430 if (!scontext2)
1431 return -ENOMEM;
1432 memcpy(scontext2, scontext, scontext_len);
1433 scontext2[scontext_len] = 0;
1434
1435 if (force) {
1436 /* Save another copy for storing in uninterpreted form */
Eric Paris4b02b522010-11-23 11:40:08 -05001437 rc = -ENOMEM;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001438 str = kstrdup(scontext2, gfp_flags);
Eric Paris4b02b522010-11-23 11:40:08 -05001439 if (!str)
1440 goto out;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001441 }
1442
James Morris0804d112008-06-06 18:40:29 +10001443 read_lock(&policy_rwlock);
Eric Paris4b02b522010-11-23 11:40:08 -05001444 rc = string_to_context_struct(&policydb, &sidtab, scontext2,
1445 scontext_len, &context, def_sid);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001446 if (rc == -EINVAL && force) {
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001447 context.str = str;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001448 context.len = scontext_len;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001449 str = NULL;
Stephen Smalley12b29f32008-05-07 13:03:20 -04001450 } else if (rc)
Eric Paris4b02b522010-11-23 11:40:08 -05001451 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 rc = sidtab_context_to_sid(&sidtab, &context, sid);
Eric Paris8e531af2008-09-03 11:49:47 -04001453 context_destroy(&context);
Eric Paris4b02b522010-11-23 11:40:08 -05001454out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001455 read_unlock(&policy_rwlock);
Eric Paris4b02b522010-11-23 11:40:08 -05001456out:
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001457 kfree(scontext2);
1458 kfree(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 return rc;
1460}
1461
James Morrisf5c1d5b2005-07-28 01:07:37 -07001462/**
1463 * security_context_to_sid - Obtain a SID for a given security context.
1464 * @scontext: security context
1465 * @scontext_len: length in bytes
1466 * @sid: security identifier, SID
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001467 * @gfp: context for the allocation
James Morrisf5c1d5b2005-07-28 01:07:37 -07001468 *
1469 * Obtains a SID associated with the security context that
1470 * has the string representation specified by @scontext.
1471 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1472 * memory is available, or 0 on success.
1473 */
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001474int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid,
1475 gfp_t gfp)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001476{
1477 return security_context_to_sid_core(scontext, scontext_len,
Nikolay Aleksandrov52a4c642014-03-07 12:44:19 +01001478 sid, SECSID_NULL, gfp, 0);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001479}
1480
Rasmus Villemoes44be2f62015-10-21 17:44:25 -04001481int security_context_str_to_sid(const char *scontext, u32 *sid, gfp_t gfp)
1482{
1483 return security_context_to_sid(scontext, strlen(scontext), sid, gfp);
1484}
1485
James Morrisf5c1d5b2005-07-28 01:07:37 -07001486/**
1487 * security_context_to_sid_default - Obtain a SID for a given security context,
1488 * falling back to specified default if needed.
1489 *
1490 * @scontext: security context
1491 * @scontext_len: length in bytes
1492 * @sid: security identifier, SID
Gabriel Craciunescud133a962007-07-31 00:39:19 -07001493 * @def_sid: default SID to assign on error
James Morrisf5c1d5b2005-07-28 01:07:37 -07001494 *
1495 * Obtains a SID associated with the security context that
1496 * has the string representation specified by @scontext.
1497 * The default SID is passed to the MLS layer to be used to allow
1498 * kernel labeling of the MLS field if the MLS field is not present
1499 * (for upgrading to MLS without full relabel).
Stephen Smalley12b29f32008-05-07 13:03:20 -04001500 * Implicitly forces adding of the context even if it cannot be mapped yet.
James Morrisf5c1d5b2005-07-28 01:07:37 -07001501 * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient
1502 * memory is available, or 0 on success.
1503 */
David Howells7bf570d2008-04-29 20:52:51 +01001504int security_context_to_sid_default(const char *scontext, u32 scontext_len,
1505 u32 *sid, u32 def_sid, gfp_t gfp_flags)
James Morrisf5c1d5b2005-07-28 01:07:37 -07001506{
1507 return security_context_to_sid_core(scontext, scontext_len,
Stephen Smalley12b29f32008-05-07 13:03:20 -04001508 sid, def_sid, gfp_flags, 1);
1509}
1510
1511int security_context_to_sid_force(const char *scontext, u32 scontext_len,
1512 u32 *sid)
1513{
1514 return security_context_to_sid_core(scontext, scontext_len,
1515 sid, SECSID_NULL, GFP_KERNEL, 1);
James Morrisf5c1d5b2005-07-28 01:07:37 -07001516}
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518static int compute_sid_handle_invalid_context(
1519 struct context *scontext,
1520 struct context *tcontext,
1521 u16 tclass,
1522 struct context *newcontext)
1523{
1524 char *s = NULL, *t = NULL, *n = NULL;
1525 u32 slen, tlen, nlen;
1526
Eric Paris4b02b522010-11-23 11:40:08 -05001527 if (context_struct_to_string(scontext, &s, &slen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -05001529 if (context_struct_to_string(tcontext, &t, &tlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -05001531 if (context_struct_to_string(newcontext, &n, &nlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 goto out;
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001533 audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR,
Richard Guy Briggs4093a842014-09-18 20:47:48 -04001534 "op=security_compute_sid invalid_context=%s"
1535 " scontext=%s"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 " tcontext=%s"
1537 " tclass=%s",
Eric Parisac76c052010-11-29 15:47:09 -05001538 n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539out:
1540 kfree(s);
1541 kfree(t);
1542 kfree(n);
1543 if (!selinux_enforcing)
1544 return 0;
1545 return -EACCES;
1546}
1547
Eric Paris652bb9b2011-02-01 11:05:40 -05001548static void filename_compute_type(struct policydb *p, struct context *newcontext,
Eric Paris26679912011-04-28 15:11:20 -04001549 u32 stype, u32 ttype, u16 tclass,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001550 const char *objname)
Eric Paris652bb9b2011-02-01 11:05:40 -05001551{
Eric Paris2463c26d2011-04-28 15:11:21 -04001552 struct filename_trans ft;
1553 struct filename_trans_datum *otype;
Eric Paris03a4c012011-04-28 15:11:21 -04001554
1555 /*
1556 * Most filename trans rules are going to live in specific directories
1557 * like /dev or /var/run. This bitmap will quickly skip rule searches
1558 * if the ttype does not contain any rules.
1559 */
1560 if (!ebitmap_get_bit(&p->filename_trans_ttypes, ttype))
1561 return;
1562
Eric Paris2463c26d2011-04-28 15:11:21 -04001563 ft.stype = stype;
1564 ft.ttype = ttype;
1565 ft.tclass = tclass;
1566 ft.name = objname;
1567
1568 otype = hashtab_search(p->filename_trans, &ft);
1569 if (otype)
1570 newcontext->type = otype->otype;
Eric Paris652bb9b2011-02-01 11:05:40 -05001571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573static int security_compute_sid(u32 ssid,
1574 u32 tsid,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001575 u16 orig_tclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 u32 specified,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001577 const char *objname,
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001578 u32 *out_sid,
1579 bool kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Eric Parisaa893262012-03-20 14:35:12 -04001581 struct class_datum *cladatum = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 struct context *scontext = NULL, *tcontext = NULL, newcontext;
1583 struct role_trans *roletr = NULL;
1584 struct avtab_key avkey;
1585 struct avtab_datum *avdatum;
1586 struct avtab_node *node;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001587 u16 tclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int rc = 0;
Harry Ciao6f5317e2011-03-02 13:32:33 +08001589 bool sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 if (!ss_initialized) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001592 switch (orig_tclass) {
1593 case SECCLASS_PROCESS: /* kernel value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 *out_sid = ssid;
1595 break;
1596 default:
1597 *out_sid = tsid;
1598 break;
1599 }
1600 goto out;
1601 }
1602
Venkat Yekkirala851f8a62006-07-30 03:03:18 -07001603 context_init(&newcontext);
1604
James Morris0804d112008-06-06 18:40:29 +10001605 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Harry Ciao6f5317e2011-03-02 13:32:33 +08001607 if (kern) {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001608 tclass = unmap_class(orig_tclass);
Harry Ciao6f5317e2011-03-02 13:32:33 +08001609 sock = security_is_socket_class(orig_tclass);
1610 } else {
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001611 tclass = orig_tclass;
Harry Ciao6f5317e2011-03-02 13:32:33 +08001612 sock = security_is_socket_class(map_class(tclass));
1613 }
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 scontext = sidtab_search(&sidtab, ssid);
1616 if (!scontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001617 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1618 __func__, ssid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 rc = -EINVAL;
1620 goto out_unlock;
1621 }
1622 tcontext = sidtab_search(&sidtab, tsid);
1623 if (!tcontext) {
Eric Paris744ba352008-04-17 11:52:44 -04001624 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
1625 __func__, tsid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 rc = -EINVAL;
1627 goto out_unlock;
1628 }
1629
Eric Parisaa893262012-03-20 14:35:12 -04001630 if (tclass && tclass <= policydb.p_classes.nprim)
1631 cladatum = policydb.class_val_to_struct[tclass - 1];
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* Set the user identity. */
1634 switch (specified) {
1635 case AVTAB_TRANSITION:
1636 case AVTAB_CHANGE:
Eric Parisaa893262012-03-20 14:35:12 -04001637 if (cladatum && cladatum->default_user == DEFAULT_TARGET) {
1638 newcontext.user = tcontext->user;
1639 } else {
1640 /* notice this gets both DEFAULT_SOURCE and unset */
1641 /* Use the process user identity. */
1642 newcontext.user = scontext->user;
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 break;
1645 case AVTAB_MEMBER:
1646 /* Use the related object owner. */
1647 newcontext.user = tcontext->user;
1648 break;
1649 }
1650
Eric Parisaa893262012-03-20 14:35:12 -04001651 /* Set the role to default values. */
1652 if (cladatum && cladatum->default_role == DEFAULT_SOURCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 newcontext.role = scontext->role;
Eric Parisaa893262012-03-20 14:35:12 -04001654 } else if (cladatum && cladatum->default_role == DEFAULT_TARGET) {
1655 newcontext.role = tcontext->role;
1656 } else {
1657 if ((tclass == policydb.process_class) || (sock == true))
1658 newcontext.role = scontext->role;
1659 else
1660 newcontext.role = OBJECT_R_VAL;
1661 }
1662
1663 /* Set the type to default values. */
Eric Pariseed77952012-03-20 14:35:12 -04001664 if (cladatum && cladatum->default_type == DEFAULT_SOURCE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 newcontext.type = scontext->type;
Eric Pariseed77952012-03-20 14:35:12 -04001666 } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 newcontext.type = tcontext->type;
Eric Pariseed77952012-03-20 14:35:12 -04001668 } else {
1669 if ((tclass == policydb.process_class) || (sock == true)) {
1670 /* Use the type of process. */
1671 newcontext.type = scontext->type;
1672 } else {
1673 /* Use the type of the related object. */
1674 newcontext.type = tcontext->type;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
1677
1678 /* Look for a type transition/member/change rule. */
1679 avkey.source_type = scontext->type;
1680 avkey.target_type = tcontext->type;
1681 avkey.target_class = tclass;
Stephen Smalley782ebb92005-09-03 15:55:16 -07001682 avkey.specified = specified;
1683 avdatum = avtab_search(&policydb.te_avtab, &avkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 /* If no permanent rule, also check for enabled conditional rules */
Eric Paris5d55a342008-04-18 17:38:33 -04001686 if (!avdatum) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001687 node = avtab_search_node(&policydb.te_cond_avtab, &avkey);
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03001688 for (; node; node = avtab_search_node_next(node, specified)) {
Stephen Smalley782ebb92005-09-03 15:55:16 -07001689 if (node->key.specified & AVTAB_ENABLED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 avdatum = &node->datum;
1691 break;
1692 }
1693 }
1694 }
1695
Stephen Smalley782ebb92005-09-03 15:55:16 -07001696 if (avdatum) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /* Use the type from the type transition/member/change rule. */
Jeff Vander Stoepfa1aa142015-07-10 17:19:56 -04001698 newcontext.type = avdatum->u.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
Eric Paris47426002011-04-28 15:11:20 -04001701 /* if we have a objname this is a file trans check so check those rules */
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001702 if (objname)
Eric Paris652bb9b2011-02-01 11:05:40 -05001703 filename_compute_type(&policydb, &newcontext, scontext->type,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001704 tcontext->type, tclass, objname);
Eric Paris652bb9b2011-02-01 11:05:40 -05001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* Check for class-specific changes. */
Harry Ciao63a312c2011-03-25 13:51:58 +08001707 if (specified & AVTAB_TRANSITION) {
1708 /* Look for a role transition rule. */
1709 for (roletr = policydb.role_tr; roletr; roletr = roletr->next) {
1710 if ((roletr->role == scontext->role) &&
1711 (roletr->type == tcontext->type) &&
1712 (roletr->tclass == tclass)) {
1713 /* Use the role transition rule. */
1714 newcontext.role = roletr->new_role;
1715 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
1719
1720 /* Set the MLS attributes.
1721 This is done last because it may allocate memory. */
Harry Ciao6f5317e2011-03-02 13:32:33 +08001722 rc = mls_compute_sid(scontext, tcontext, tclass, specified,
1723 &newcontext, sock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (rc)
1725 goto out_unlock;
1726
1727 /* Check the validity of the context. */
1728 if (!policydb_context_isvalid(&policydb, &newcontext)) {
1729 rc = compute_sid_handle_invalid_context(scontext,
1730 tcontext,
1731 tclass,
1732 &newcontext);
1733 if (rc)
1734 goto out_unlock;
1735 }
1736 /* Obtain the sid for the context. */
1737 rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid);
1738out_unlock:
James Morris0804d112008-06-06 18:40:29 +10001739 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 context_destroy(&newcontext);
1741out:
1742 return rc;
1743}
1744
1745/**
1746 * security_transition_sid - Compute the SID for a new subject/object.
1747 * @ssid: source security identifier
1748 * @tsid: target security identifier
1749 * @tclass: target security class
1750 * @out_sid: security identifier for new subject/object
1751 *
1752 * Compute a SID to use for labeling a new subject or object in the
1753 * class @tclass based on a SID pair (@ssid, @tsid).
1754 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1755 * if insufficient memory is available, or %0 if the new SID was
1756 * computed successfully.
1757 */
Eric Paris652bb9b2011-02-01 11:05:40 -05001758int security_transition_sid(u32 ssid, u32 tsid, u16 tclass,
1759 const struct qstr *qstr, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001761 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001762 qstr ? qstr->name : NULL, out_sid, true);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001763}
1764
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001765int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass,
1766 const char *objname, u32 *out_sid)
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04001767{
1768 return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION,
Kohei Kaigaif50a3ec2011-04-01 15:39:26 +01001769 objname, out_sid, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
1772/**
1773 * security_member_sid - Compute the SID for member selection.
1774 * @ssid: source security identifier
1775 * @tsid: target security identifier
1776 * @tclass: target security class
1777 * @out_sid: security identifier for selected member
1778 *
1779 * Compute a SID to use when selecting a member of a polyinstantiated
1780 * object of class @tclass based on a SID pair (@ssid, @tsid).
1781 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1782 * if insufficient memory is available, or %0 if the SID was
1783 * computed successfully.
1784 */
1785int security_member_sid(u32 ssid,
1786 u32 tsid,
1787 u16 tclass,
1788 u32 *out_sid)
1789{
Eric Paris652bb9b2011-02-01 11:05:40 -05001790 return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL,
1791 out_sid, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792}
1793
1794/**
1795 * security_change_sid - Compute the SID for object relabeling.
1796 * @ssid: source security identifier
1797 * @tsid: target security identifier
1798 * @tclass: target security class
1799 * @out_sid: security identifier for selected member
1800 *
1801 * Compute a SID to use for relabeling an object of class @tclass
1802 * based on a SID pair (@ssid, @tsid).
1803 * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM
1804 * if insufficient memory is available, or %0 if the SID was
1805 * computed successfully.
1806 */
1807int security_change_sid(u32 ssid,
1808 u32 tsid,
1809 u16 tclass,
1810 u32 *out_sid)
1811{
Eric Paris652bb9b2011-02-01 11:05:40 -05001812 return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL,
1813 out_sid, false);
Chad Sellersb94c7e62006-11-06 12:38:18 -05001814}
1815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816/* Clone the SID into the new SID table. */
1817static int clone_sid(u32 sid,
1818 struct context *context,
1819 void *arg)
1820{
1821 struct sidtab *s = arg;
1822
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001823 if (sid > SECINITSID_NUM)
1824 return sidtab_insert(s, sid, context);
1825 else
1826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827}
1828
1829static inline int convert_context_handle_invalid_context(struct context *context)
1830{
Eric Paris4b02b522010-11-23 11:40:08 -05001831 char *s;
1832 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Eric Paris4b02b522010-11-23 11:40:08 -05001834 if (selinux_enforcing)
1835 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Eric Paris4b02b522010-11-23 11:40:08 -05001837 if (!context_struct_to_string(context, &s, &len)) {
1838 printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s);
1839 kfree(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Eric Paris4b02b522010-11-23 11:40:08 -05001841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844struct convert_context_args {
1845 struct policydb *oldp;
1846 struct policydb *newp;
1847};
1848
1849/*
1850 * Convert the values in the security context
1851 * structure `c' from the values specified
1852 * in the policy `p->oldp' to the values specified
1853 * in the policy `p->newp'. Verify that the
1854 * context is valid under the new policy.
1855 */
1856static int convert_context(u32 key,
1857 struct context *c,
1858 void *p)
1859{
1860 struct convert_context_args *args;
1861 struct context oldc;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001862 struct ocontext *oc;
1863 struct mls_range *range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct role_datum *role;
1865 struct type_datum *typdatum;
1866 struct user_datum *usrdatum;
1867 char *s;
1868 u32 len;
Guido Trentalancia42596ea2010-02-03 17:06:01 +01001869 int rc = 0;
1870
1871 if (key <= SECINITSID_NUM)
1872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 args = p;
1875
Stephen Smalley12b29f32008-05-07 13:03:20 -04001876 if (c->str) {
1877 struct context ctx;
Eric Paris4b02b522010-11-23 11:40:08 -05001878
1879 rc = -ENOMEM;
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001880 s = kstrdup(c->str, GFP_KERNEL);
Eric Paris4b02b522010-11-23 11:40:08 -05001881 if (!s)
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001882 goto out;
Eric Paris4b02b522010-11-23 11:40:08 -05001883
Stephen Smalley9a59daa2008-05-14 10:33:55 -04001884 rc = string_to_context_struct(args->newp, NULL, s,
1885 c->len, &ctx, SECSID_NULL);
1886 kfree(s);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001887 if (!rc) {
Eric Paris4b02b522010-11-23 11:40:08 -05001888 printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n",
Stephen Smalley12b29f32008-05-07 13:03:20 -04001889 c->str);
1890 /* Replace string with mapped representation. */
1891 kfree(c->str);
1892 memcpy(c, &ctx, sizeof(*c));
1893 goto out;
1894 } else if (rc == -EINVAL) {
1895 /* Retain string representation for later mapping. */
1896 rc = 0;
1897 goto out;
1898 } else {
1899 /* Other error condition, e.g. ENOMEM. */
Eric Paris4b02b522010-11-23 11:40:08 -05001900 printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n",
Stephen Smalley12b29f32008-05-07 13:03:20 -04001901 c->str, -rc);
1902 goto out;
1903 }
1904 }
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 rc = context_cpy(&oldc, c);
1907 if (rc)
1908 goto out;
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /* Convert the user. */
Eric Paris4b02b522010-11-23 11:40:08 -05001911 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 usrdatum = hashtab_search(args->newp->p_users.table,
Eric Parisac76c052010-11-29 15:47:09 -05001913 sym_name(args->oldp, SYM_USERS, c->user - 1));
Eric Paris5d55a342008-04-18 17:38:33 -04001914 if (!usrdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 c->user = usrdatum->value;
1917
1918 /* Convert the role. */
Eric Paris4b02b522010-11-23 11:40:08 -05001919 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 role = hashtab_search(args->newp->p_roles.table,
Eric Parisac76c052010-11-29 15:47:09 -05001921 sym_name(args->oldp, SYM_ROLES, c->role - 1));
Eric Paris5d55a342008-04-18 17:38:33 -04001922 if (!role)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 c->role = role->value;
1925
1926 /* Convert the type. */
Eric Paris4b02b522010-11-23 11:40:08 -05001927 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 typdatum = hashtab_search(args->newp->p_types.table,
Eric Parisac76c052010-11-29 15:47:09 -05001929 sym_name(args->oldp, SYM_TYPES, c->type - 1));
Eric Paris5d55a342008-04-18 17:38:33 -04001930 if (!typdatum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 c->type = typdatum->value;
1933
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001934 /* Convert the MLS fields if dealing with MLS policies */
1935 if (args->oldp->mls_enabled && args->newp->mls_enabled) {
1936 rc = mls_convert_context(args->oldp, args->newp, c);
1937 if (rc)
1938 goto bad;
1939 } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) {
1940 /*
1941 * Switching between MLS and non-MLS policy:
1942 * free any storage used by the MLS fields in the
1943 * context for all existing entries in the sidtab.
1944 */
1945 mls_context_destroy(c);
1946 } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) {
1947 /*
1948 * Switching between non-MLS and MLS policy:
1949 * ensure that the MLS fields of the context for all
1950 * existing entries in the sidtab are filled in with a
1951 * suitable default value, likely taken from one of the
1952 * initial SIDs.
1953 */
1954 oc = args->newp->ocontexts[OCON_ISID];
1955 while (oc && oc->sid[0] != SECINITSID_UNLABELED)
1956 oc = oc->next;
Eric Paris4b02b522010-11-23 11:40:08 -05001957 rc = -EINVAL;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01001958 if (!oc) {
1959 printk(KERN_ERR "SELinux: unable to look up"
1960 " the initial SIDs list\n");
1961 goto bad;
1962 }
1963 range = &oc->context[0].range;
1964 rc = mls_range_set(c, range);
1965 if (rc)
1966 goto bad;
1967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 /* Check the validity of the new context. */
1970 if (!policydb_context_isvalid(args->newp, c)) {
1971 rc = convert_context_handle_invalid_context(&oldc);
1972 if (rc)
1973 goto bad;
1974 }
1975
1976 context_destroy(&oldc);
Eric Paris4b02b522010-11-23 11:40:08 -05001977
Stephen Smalley12b29f32008-05-07 13:03:20 -04001978 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979out:
1980 return rc;
1981bad:
Stephen Smalley12b29f32008-05-07 13:03:20 -04001982 /* Map old representation to string and save it. */
Eric Paris4b02b522010-11-23 11:40:08 -05001983 rc = context_struct_to_string(&oldc, &s, &len);
1984 if (rc)
1985 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 context_destroy(&oldc);
Stephen Smalley12b29f32008-05-07 13:03:20 -04001987 context_destroy(c);
1988 c->str = s;
1989 c->len = len;
Eric Paris4b02b522010-11-23 11:40:08 -05001990 printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n",
Stephen Smalley12b29f32008-05-07 13:03:20 -04001991 c->str);
1992 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 goto out;
1994}
1995
Paul Moore3bb56b22008-01-29 08:38:19 -05001996static void security_load_policycaps(void)
1997{
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04001998 unsigned int i;
1999 struct ebitmap_node *node;
2000
Paul Moore3bb56b22008-01-29 08:38:19 -05002001 selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps,
2002 POLICYDB_CAPABILITY_NETPEER);
Eric Parisb0c636b2008-02-28 12:58:40 -05002003 selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps,
2004 POLICYDB_CAPABILITY_OPENPERM);
Stephen Smalleyda69a532017-01-09 10:07:30 -05002005 selinux_policycap_extsockclass = ebitmap_get_bit(&policydb.policycaps,
2006 POLICYDB_CAPABILITY_EXTSOCKCLASS);
Chris PeBenito2be4d742013-05-03 09:05:39 -04002007 selinux_policycap_alwaysnetwork = ebitmap_get_bit(&policydb.policycaps,
2008 POLICYDB_CAPABILITY_ALWAYSNETWORK);
Stephen Smalley2651225b2017-02-28 10:35:56 -05002009 selinux_policycap_cgroupseclabel =
2010 ebitmap_get_bit(&policydb.policycaps,
2011 POLICYDB_CAPABILITY_CGROUPSECLABEL);
Stephen Smalley4dc2fce2017-05-18 16:58:31 -04002012
2013 for (i = 0; i < ARRAY_SIZE(selinux_policycap_names); i++)
2014 pr_info("SELinux: policy capability %s=%d\n",
2015 selinux_policycap_names[i],
2016 ebitmap_get_bit(&policydb.policycaps, i));
2017
2018 ebitmap_for_each_positive_bit(&policydb.policycaps, node, i) {
2019 if (i >= ARRAY_SIZE(selinux_policycap_names))
2020 pr_info("SELinux: unknown policy capability %u\n",
2021 i);
2022 }
Paul Moore3bb56b22008-01-29 08:38:19 -05002023}
2024
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002025static int security_preserve_bools(struct policydb *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027/**
2028 * security_load_policy - Load a security policy configuration.
2029 * @data: binary policy data
2030 * @len: length of data in bytes
2031 *
2032 * Load a new set of security policy configuration data,
2033 * validate it and convert the SID table as necessary.
2034 * This function will flush the access vector cache after
2035 * loading the new policy.
2036 */
2037int security_load_policy(void *data, size_t len)
2038{
Tim Gardnerb5495b42013-11-14 15:04:51 -07002039 struct policydb *oldpolicydb, *newpolicydb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct sidtab oldsidtab, newsidtab;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002041 struct selinux_mapping *oldmap, *map = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct convert_context_args args;
2043 u32 seqno;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002044 u16 map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int rc = 0;
2046 struct policy_file file = { data, len }, *fp = &file;
2047
Tim Gardnerb5495b42013-11-14 15:04:51 -07002048 oldpolicydb = kzalloc(2 * sizeof(*oldpolicydb), GFP_KERNEL);
2049 if (!oldpolicydb) {
2050 rc = -ENOMEM;
2051 goto out;
2052 }
2053 newpolicydb = oldpolicydb + 1;
2054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (!ss_initialized) {
2056 avtab_cache_init();
Eric Parisa2000052010-04-20 10:29:42 -04002057 rc = policydb_read(&policydb, fp);
2058 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 avtab_cache_destroy();
Tim Gardnerb5495b42013-11-14 15:04:51 -07002060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Eric Parisa2000052010-04-20 10:29:42 -04002062
Eric Pariscee74f42010-10-13 17:50:25 -04002063 policydb.len = len;
Eric Parisa2000052010-04-20 10:29:42 -04002064 rc = selinux_set_mapping(&policydb, secclass_map,
2065 &current_mapping,
2066 &current_mapping_size);
2067 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 policydb_destroy(&policydb);
2069 avtab_cache_destroy();
Tim Gardnerb5495b42013-11-14 15:04:51 -07002070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
Eric Parisa2000052010-04-20 10:29:42 -04002072
2073 rc = policydb_load_isids(&policydb, &sidtab);
2074 if (rc) {
Chad Sellersb94c7e62006-11-06 12:38:18 -05002075 policydb_destroy(&policydb);
2076 avtab_cache_destroy();
Tim Gardnerb5495b42013-11-14 15:04:51 -07002077 goto out;
Chad Sellersb94c7e62006-11-06 12:38:18 -05002078 }
Eric Parisa2000052010-04-20 10:29:42 -04002079
Paul Moore3bb56b22008-01-29 08:38:19 -05002080 security_load_policycaps();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 ss_initialized = 1;
Stephen Smalley4c443d12005-05-16 21:53:52 -07002082 seqno = ++latest_granting;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 selinux_complete_init();
Stephen Smalley4c443d12005-05-16 21:53:52 -07002084 avc_ss_reset(seqno);
2085 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09002086 selinux_status_update_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002087 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002088 selinux_xfrm_notify_policyload();
Tim Gardnerb5495b42013-11-14 15:04:51 -07002089 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 }
2091
2092#if 0
2093 sidtab_hash_eval(&sidtab, "sids");
2094#endif
2095
Tim Gardnerb5495b42013-11-14 15:04:51 -07002096 rc = policydb_read(newpolicydb, fp);
Eric Parisa2000052010-04-20 10:29:42 -04002097 if (rc)
Tim Gardnerb5495b42013-11-14 15:04:51 -07002098 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Tim Gardnerb5495b42013-11-14 15:04:51 -07002100 newpolicydb->len = len;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002101 /* If switching between different policy types, log MLS status */
Tim Gardnerb5495b42013-11-14 15:04:51 -07002102 if (policydb.mls_enabled && !newpolicydb->mls_enabled)
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002103 printk(KERN_INFO "SELinux: Disabling MLS support...\n");
Tim Gardnerb5495b42013-11-14 15:04:51 -07002104 else if (!policydb.mls_enabled && newpolicydb->mls_enabled)
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002105 printk(KERN_INFO "SELinux: Enabling MLS support...\n");
2106
Tim Gardnerb5495b42013-11-14 15:04:51 -07002107 rc = policydb_load_isids(newpolicydb, &newsidtab);
Guido Trentalancia42596ea2010-02-03 17:06:01 +01002108 if (rc) {
2109 printk(KERN_ERR "SELinux: unable to load the initial SIDs\n");
Tim Gardnerb5495b42013-11-14 15:04:51 -07002110 policydb_destroy(newpolicydb);
2111 goto out;
Stephen Smalley12b29f32008-05-07 13:03:20 -04002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Tim Gardnerb5495b42013-11-14 15:04:51 -07002114 rc = selinux_set_mapping(newpolicydb, secclass_map, &map, &map_size);
Eric Parisa2000052010-04-20 10:29:42 -04002115 if (rc)
Chad Sellersb94c7e62006-11-06 12:38:18 -05002116 goto err;
Chad Sellersb94c7e62006-11-06 12:38:18 -05002117
Tim Gardnerb5495b42013-11-14 15:04:51 -07002118 rc = security_preserve_bools(newpolicydb);
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002119 if (rc) {
James Morris454d9722008-02-26 20:42:02 +11002120 printk(KERN_ERR "SELinux: unable to preserve booleans\n");
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002121 goto err;
2122 }
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /* Clone the SID table. */
2125 sidtab_shutdown(&sidtab);
Eric Parisa2000052010-04-20 10:29:42 -04002126
2127 rc = sidtab_map(&sidtab, clone_sid, &newsidtab);
2128 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
Stephen Smalley12b29f32008-05-07 13:03:20 -04002131 /*
2132 * Convert the internal representations of contexts
2133 * in the new SID table.
2134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 args.oldp = &policydb;
Tim Gardnerb5495b42013-11-14 15:04:51 -07002136 args.newp = newpolicydb;
Stephen Smalley12b29f32008-05-07 13:03:20 -04002137 rc = sidtab_map(&newsidtab, convert_context, &args);
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002138 if (rc) {
2139 printk(KERN_ERR "SELinux: unable to convert the internal"
2140 " representation of contexts in the new SID"
2141 " table\n");
Stephen Smalley12b29f32008-05-07 13:03:20 -04002142 goto err;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 /* Save the old policydb and SID table to free later. */
Tim Gardnerb5495b42013-11-14 15:04:51 -07002146 memcpy(oldpolicydb, &policydb, sizeof(policydb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 sidtab_set(&oldsidtab, &sidtab);
2148
2149 /* Install the new policydb and SID table. */
James Morris0804d112008-06-06 18:40:29 +10002150 write_lock_irq(&policy_rwlock);
Tim Gardnerb5495b42013-11-14 15:04:51 -07002151 memcpy(&policydb, newpolicydb, sizeof(policydb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 sidtab_set(&sidtab, &newsidtab);
Paul Moore3bb56b22008-01-29 08:38:19 -05002153 security_load_policycaps();
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002154 oldmap = current_mapping;
2155 current_mapping = map;
2156 current_mapping_size = map_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 seqno = ++latest_granting;
James Morris0804d112008-06-06 18:40:29 +10002158 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /* Free the old policydb and SID table. */
Tim Gardnerb5495b42013-11-14 15:04:51 -07002161 policydb_destroy(oldpolicydb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 sidtab_destroy(&oldsidtab);
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002163 kfree(oldmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 avc_ss_reset(seqno);
2166 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09002167 selinux_status_update_policyload(seqno);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07002168 selinux_netlbl_cache_invalidate();
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002169 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Tim Gardnerb5495b42013-11-14 15:04:51 -07002171 rc = 0;
2172 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174err:
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002175 kfree(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 sidtab_destroy(&newsidtab);
Tim Gardnerb5495b42013-11-14 15:04:51 -07002177 policydb_destroy(newpolicydb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Tim Gardnerb5495b42013-11-14 15:04:51 -07002179out:
2180 kfree(oldpolicydb);
2181 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Eric Pariscee74f42010-10-13 17:50:25 -04002184size_t security_policydb_len(void)
2185{
2186 size_t len;
2187
2188 read_lock(&policy_rwlock);
2189 len = policydb.len;
2190 read_unlock(&policy_rwlock);
2191
2192 return len;
2193}
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195/**
2196 * security_port_sid - Obtain the SID for a port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 * @protocol: protocol number
2198 * @port: port number
2199 * @out_sid: security identifier
2200 */
Paul Moore3e112172008-04-10 10:48:14 -04002201int security_port_sid(u8 protocol, u16 port, u32 *out_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
2203 struct ocontext *c;
2204 int rc = 0;
2205
James Morris0804d112008-06-06 18:40:29 +10002206 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 c = policydb.ocontexts[OCON_PORT];
2209 while (c) {
2210 if (c->u.port.protocol == protocol &&
2211 c->u.port.low_port <= port &&
2212 c->u.port.high_port >= port)
2213 break;
2214 c = c->next;
2215 }
2216
2217 if (c) {
2218 if (!c->sid[0]) {
2219 rc = sidtab_context_to_sid(&sidtab,
2220 &c->context[0],
2221 &c->sid[0]);
2222 if (rc)
2223 goto out;
2224 }
2225 *out_sid = c->sid[0];
2226 } else {
2227 *out_sid = SECINITSID_PORT;
2228 }
2229
2230out:
James Morris0804d112008-06-06 18:40:29 +10002231 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 return rc;
2233}
2234
2235/**
2236 * security_netif_sid - Obtain the SID for a network interface.
2237 * @name: interface name
2238 * @if_sid: interface SID
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 */
Paul Mooree8bfdb92008-01-29 08:38:08 -05002240int security_netif_sid(char *name, u32 *if_sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
2242 int rc = 0;
2243 struct ocontext *c;
2244
James Morris0804d112008-06-06 18:40:29 +10002245 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 c = policydb.ocontexts[OCON_NETIF];
2248 while (c) {
2249 if (strcmp(name, c->u.name) == 0)
2250 break;
2251 c = c->next;
2252 }
2253
2254 if (c) {
2255 if (!c->sid[0] || !c->sid[1]) {
2256 rc = sidtab_context_to_sid(&sidtab,
2257 &c->context[0],
2258 &c->sid[0]);
2259 if (rc)
2260 goto out;
2261 rc = sidtab_context_to_sid(&sidtab,
2262 &c->context[1],
2263 &c->sid[1]);
2264 if (rc)
2265 goto out;
2266 }
2267 *if_sid = c->sid[0];
Paul Mooree8bfdb92008-01-29 08:38:08 -05002268 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 *if_sid = SECINITSID_NETIF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271out:
James Morris0804d112008-06-06 18:40:29 +10002272 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return rc;
2274}
2275
2276static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask)
2277{
2278 int i, fail = 0;
2279
Eric Paris5d55a342008-04-18 17:38:33 -04002280 for (i = 0; i < 4; i++)
2281 if (addr[i] != (input[i] & mask[i])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 fail = 1;
2283 break;
2284 }
2285
2286 return !fail;
2287}
2288
2289/**
2290 * security_node_sid - Obtain the SID for a node (host).
2291 * @domain: communication domain aka address family
2292 * @addrp: address
2293 * @addrlen: address length in bytes
2294 * @out_sid: security identifier
2295 */
2296int security_node_sid(u16 domain,
2297 void *addrp,
2298 u32 addrlen,
2299 u32 *out_sid)
2300{
Eric Paris4b02b522010-11-23 11:40:08 -05002301 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct ocontext *c;
2303
James Morris0804d112008-06-06 18:40:29 +10002304 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 switch (domain) {
2307 case AF_INET: {
2308 u32 addr;
2309
Eric Paris4b02b522010-11-23 11:40:08 -05002310 rc = -EINVAL;
2311 if (addrlen != sizeof(u32))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 addr = *((u32 *)addrp);
2315
2316 c = policydb.ocontexts[OCON_NODE];
2317 while (c) {
2318 if (c->u.node.addr == (addr & c->u.node.mask))
2319 break;
2320 c = c->next;
2321 }
2322 break;
2323 }
2324
2325 case AF_INET6:
Eric Paris4b02b522010-11-23 11:40:08 -05002326 rc = -EINVAL;
2327 if (addrlen != sizeof(u64) * 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 c = policydb.ocontexts[OCON_NODE6];
2330 while (c) {
2331 if (match_ipv6_addrmask(addrp, c->u.node6.addr,
2332 c->u.node6.mask))
2333 break;
2334 c = c->next;
2335 }
2336 break;
2337
2338 default:
Eric Paris4b02b522010-11-23 11:40:08 -05002339 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 *out_sid = SECINITSID_NODE;
2341 goto out;
2342 }
2343
2344 if (c) {
2345 if (!c->sid[0]) {
2346 rc = sidtab_context_to_sid(&sidtab,
2347 &c->context[0],
2348 &c->sid[0]);
2349 if (rc)
2350 goto out;
2351 }
2352 *out_sid = c->sid[0];
2353 } else {
2354 *out_sid = SECINITSID_NODE;
2355 }
2356
Eric Paris4b02b522010-11-23 11:40:08 -05002357 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358out:
James Morris0804d112008-06-06 18:40:29 +10002359 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return rc;
2361}
2362
2363#define SIDS_NEL 25
2364
2365/**
2366 * security_get_user_sids - Obtain reachable SIDs for a user.
2367 * @fromsid: starting SID
2368 * @username: username
2369 * @sids: array of reachable SIDs for user
2370 * @nel: number of elements in @sids
2371 *
2372 * Generate the set of SIDs for legal security contexts
2373 * for a given user that can be reached by @fromsid.
2374 * Set *@sids to point to a dynamically allocated
2375 * array containing the set of SIDs. Set *@nel to the
2376 * number of elements in the array.
2377 */
2378
2379int security_get_user_sids(u32 fromsid,
Eric Paris5d55a342008-04-18 17:38:33 -04002380 char *username,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 u32 **sids,
2382 u32 *nel)
2383{
2384 struct context *fromcon, usercon;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002385 u32 *mysids = NULL, *mysids2, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 u32 mynel = 0, maxnel = SIDS_NEL;
2387 struct user_datum *user;
2388 struct role_datum *role;
Stephen Smalley782ebb92005-09-03 15:55:16 -07002389 struct ebitmap_node *rnode, *tnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 int rc = 0, i, j;
2391
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002392 *sids = NULL;
2393 *nel = 0;
2394
2395 if (!ss_initialized)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
James Morris0804d112008-06-06 18:40:29 +10002398 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
Stephen Smalley12b29f32008-05-07 13:03:20 -04002400 context_init(&usercon);
2401
Eric Paris4b02b522010-11-23 11:40:08 -05002402 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 fromcon = sidtab_search(&sidtab, fromsid);
Eric Paris4b02b522010-11-23 11:40:08 -05002404 if (!fromcon)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Eric Paris4b02b522010-11-23 11:40:08 -05002407 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 user = hashtab_search(policydb.p_users.table, username);
Eric Paris4b02b522010-11-23 11:40:08 -05002409 if (!user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto out_unlock;
Eric Paris4b02b522010-11-23 11:40:08 -05002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 usercon.user = user->value;
2413
Eric Paris4b02b522010-11-23 11:40:08 -05002414 rc = -ENOMEM;
James Morris89d155e2005-10-30 14:59:21 -08002415 mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC);
Eric Paris4b02b522010-11-23 11:40:08 -05002416 if (!mysids)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002419 ebitmap_for_each_positive_bit(&user->roles, rnode, i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 role = policydb.role_val_to_struct[i];
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08002421 usercon.role = i + 1;
KaiGai Kohei9fe79ad2007-09-29 02:20:55 +09002422 ebitmap_for_each_positive_bit(&role->types, tnode, j) {
wzt.wzt@gmail.comc1a73682010-04-09 19:30:29 +08002423 usercon.type = j + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 if (mls_setup_user_range(fromcon, user, &usercon))
2426 continue;
2427
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 rc = sidtab_context_to_sid(&sidtab, &usercon, &sid);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002429 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (mynel < maxnel) {
2432 mysids[mynel++] = sid;
2433 } else {
Eric Paris4b02b522010-11-23 11:40:08 -05002434 rc = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 maxnel += SIDS_NEL;
James Morris89d155e2005-10-30 14:59:21 -08002436 mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC);
Eric Paris4b02b522010-11-23 11:40:08 -05002437 if (!mysids2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto out_unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 memcpy(mysids2, mysids, mynel * sizeof(*mysids2));
2440 kfree(mysids);
2441 mysids = mysids2;
2442 mysids[mynel++] = sid;
2443 }
2444 }
2445 }
Eric Paris4b02b522010-11-23 11:40:08 -05002446 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002448 read_unlock(&policy_rwlock);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002449 if (rc || !mynel) {
2450 kfree(mysids);
2451 goto out;
2452 }
2453
Eric Paris4b02b522010-11-23 11:40:08 -05002454 rc = -ENOMEM;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002455 mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL);
2456 if (!mysids2) {
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002457 kfree(mysids);
2458 goto out;
2459 }
2460 for (i = 0, j = 0; i < mynel; i++) {
Linus Torvaldsf01e1af2011-05-24 13:48:51 -07002461 struct av_decision dummy_avd;
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002462 rc = avc_has_perm_noaudit(fromsid, mysids[i],
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002463 SECCLASS_PROCESS, /* kernel value */
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002464 PROCESS__TRANSITION, AVC_STRICT,
Linus Torvaldsf01e1af2011-05-24 13:48:51 -07002465 &dummy_avd);
Stephen Smalley2c3c05d2007-06-07 15:34:10 -04002466 if (!rc)
2467 mysids2[j++] = mysids[i];
2468 cond_resched();
2469 }
2470 rc = 0;
2471 kfree(mysids);
2472 *sids = mysids2;
2473 *nel = j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474out:
2475 return rc;
2476}
2477
2478/**
Waiman Longf31e7992014-06-23 11:28:51 -04002479 * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 * @fstype: filesystem type
2481 * @path: path from root of mount
2482 * @sclass: file security class
2483 * @sid: SID for path
2484 *
2485 * Obtain a SID to use for a file in a filesystem that
2486 * cannot support xattr or use a fixed labeling behavior like
2487 * transition SIDs or task SIDs.
Waiman Longf31e7992014-06-23 11:28:51 -04002488 *
2489 * The caller must acquire the policy_rwlock before calling this function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 */
Waiman Longf31e7992014-06-23 11:28:51 -04002491static inline int __security_genfs_sid(const char *fstype,
2492 char *path,
2493 u16 orig_sclass,
2494 u32 *sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
2496 int len;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002497 u16 sclass;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct genfs *genfs;
2499 struct ocontext *c;
Eric Paris4b02b522010-11-23 11:40:08 -05002500 int rc, cmp = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Stephen Smalleyb1aa5302008-01-25 13:03:42 -05002502 while (path[0] == '/' && path[1] == '/')
2503 path++;
2504
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002505 sclass = unmap_class(orig_sclass);
Eric Paris4b02b522010-11-23 11:40:08 -05002506 *sid = SECINITSID_UNLABELED;
Stephen Smalleyc6d3aaa2009-09-30 13:37:50 -04002507
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 for (genfs = policydb.genfs; genfs; genfs = genfs->next) {
2509 cmp = strcmp(fstype, genfs->fstype);
2510 if (cmp <= 0)
2511 break;
2512 }
2513
Eric Paris4b02b522010-11-23 11:40:08 -05002514 rc = -ENOENT;
2515 if (!genfs || cmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 for (c = genfs->head; c; c = c->next) {
2519 len = strlen(c->u.name);
2520 if ((!c->v.sclass || sclass == c->v.sclass) &&
2521 (strncmp(c->u.name, path, len) == 0))
2522 break;
2523 }
2524
Eric Paris4b02b522010-11-23 11:40:08 -05002525 rc = -ENOENT;
2526 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
2529 if (!c->sid[0]) {
Eric Paris4b02b522010-11-23 11:40:08 -05002530 rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (rc)
2532 goto out;
2533 }
2534
2535 *sid = c->sid[0];
Eric Paris4b02b522010-11-23 11:40:08 -05002536 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 return rc;
2539}
2540
2541/**
Waiman Longf31e7992014-06-23 11:28:51 -04002542 * security_genfs_sid - Obtain a SID for a file in a filesystem
2543 * @fstype: filesystem type
2544 * @path: path from root of mount
2545 * @sclass: file security class
2546 * @sid: SID for path
2547 *
2548 * Acquire policy_rwlock before calling __security_genfs_sid() and release
2549 * it afterward.
2550 */
2551int security_genfs_sid(const char *fstype,
2552 char *path,
2553 u16 orig_sclass,
2554 u32 *sid)
2555{
2556 int retval;
2557
2558 read_lock(&policy_rwlock);
2559 retval = __security_genfs_sid(fstype, path, orig_sclass, sid);
2560 read_unlock(&policy_rwlock);
2561 return retval;
2562}
2563
2564/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 * security_fs_use - Determine how to handle labeling for a filesystem.
Eric Parisa64c54c2012-08-24 15:59:07 -04002566 * @sb: superblock in question
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 */
Eric Parisa64c54c2012-08-24 15:59:07 -04002568int security_fs_use(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
2570 int rc = 0;
2571 struct ocontext *c;
Eric Parisa64c54c2012-08-24 15:59:07 -04002572 struct superblock_security_struct *sbsec = sb->s_security;
2573 const char *fstype = sb->s_type->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
James Morris0804d112008-06-06 18:40:29 +10002575 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Paul Moore4d546f82013-12-13 14:49:53 -05002577 c = policydb.ocontexts[OCON_FSUSE];
2578 while (c) {
2579 if (strcmp(fstype, c->u.name) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 break;
Paul Moore4d546f82013-12-13 14:49:53 -05002581 c = c->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 }
2583
2584 if (c) {
Eric Parisa64c54c2012-08-24 15:59:07 -04002585 sbsec->behavior = c->v.behavior;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (!c->sid[0]) {
Eric Paris4b02b522010-11-23 11:40:08 -05002587 rc = sidtab_context_to_sid(&sidtab, &c->context[0],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 &c->sid[0]);
2589 if (rc)
2590 goto out;
2591 }
Eric Parisa64c54c2012-08-24 15:59:07 -04002592 sbsec->sid = c->sid[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 } else {
Waiman Longf31e7992014-06-23 11:28:51 -04002594 rc = __security_genfs_sid(fstype, "/", SECCLASS_DIR,
2595 &sbsec->sid);
James Morris089be432008-07-15 18:32:49 +10002596 if (rc) {
Eric Parisa64c54c2012-08-24 15:59:07 -04002597 sbsec->behavior = SECURITY_FS_USE_NONE;
James Morris089be432008-07-15 18:32:49 +10002598 rc = 0;
2599 } else {
Eric Parisa64c54c2012-08-24 15:59:07 -04002600 sbsec->behavior = SECURITY_FS_USE_GENFS;
James Morris089be432008-07-15 18:32:49 +10002601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 }
2603
2604out:
James Morris0804d112008-06-06 18:40:29 +10002605 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return rc;
2607}
2608
2609int security_get_bools(int *len, char ***names, int **values)
2610{
Eric Paris4b02b522010-11-23 11:40:08 -05002611 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
James Morris0804d112008-06-06 18:40:29 +10002613 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 *names = NULL;
2615 *values = NULL;
2616
Eric Paris4b02b522010-11-23 11:40:08 -05002617 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 *len = policydb.p_bools.nprim;
Eric Paris4b02b522010-11-23 11:40:08 -05002619 if (!*len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Eric Paris4b02b522010-11-23 11:40:08 -05002622 rc = -ENOMEM;
2623 *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 if (!*names)
2625 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Eric Paris4b02b522010-11-23 11:40:08 -05002627 rc = -ENOMEM;
2628 *values = kcalloc(*len, sizeof(int), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (!*values)
2630 goto err;
2631
2632 for (i = 0; i < *len; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 (*values)[i] = policydb.bool_val_to_struct[i]->state;
Eric Paris4b02b522010-11-23 11:40:08 -05002634
2635 rc = -ENOMEM;
Rasmus Villemoes21b76f12015-10-21 17:44:26 -04002636 (*names)[i] = kstrdup(sym_name(&policydb, SYM_BOOLS, i), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (!(*names)[i])
2638 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
2640 rc = 0;
2641out:
James Morris0804d112008-06-06 18:40:29 +10002642 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 return rc;
2644err:
2645 if (*names) {
2646 for (i = 0; i < *len; i++)
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002647 kfree((*names)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
Jesper Juhl9a5f04b2005-06-25 14:58:51 -07002649 kfree(*values);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 goto out;
2651}
2652
2653
2654int security_set_bools(int len, int *values)
2655{
Eric Paris4b02b522010-11-23 11:40:08 -05002656 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 int lenp, seqno = 0;
2658 struct cond_node *cur;
2659
James Morris0804d112008-06-06 18:40:29 +10002660 write_lock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Eric Paris4b02b522010-11-23 11:40:08 -05002662 rc = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 lenp = policydb.p_bools.nprim;
Eric Paris4b02b522010-11-23 11:40:08 -05002664 if (len != lenp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 for (i = 0; i < len; i++) {
Steve Grubbaf601e42006-01-04 14:08:39 +00002668 if (!!values[i] != policydb.bool_val_to_struct[i]->state) {
2669 audit_log(current->audit_context, GFP_ATOMIC,
2670 AUDIT_MAC_CONFIG_CHANGE,
Eric Paris4746ec52008-01-08 10:06:53 -05002671 "bool=%s val=%d old_val=%d auid=%u ses=%u",
Eric Parisac76c052010-11-29 15:47:09 -05002672 sym_name(&policydb, SYM_BOOLS, i),
Steve Grubbaf601e42006-01-04 14:08:39 +00002673 !!values[i],
2674 policydb.bool_val_to_struct[i]->state,
Eric W. Biederman581abc02012-08-20 00:09:36 -07002675 from_kuid(&init_user_ns, audit_get_loginuid(current)),
Eric Paris4746ec52008-01-08 10:06:53 -05002676 audit_get_sessionid(current));
Steve Grubbaf601e42006-01-04 14:08:39 +00002677 }
Eric Paris5d55a342008-04-18 17:38:33 -04002678 if (values[i])
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 policydb.bool_val_to_struct[i]->state = 1;
Eric Paris5d55a342008-04-18 17:38:33 -04002680 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 policydb.bool_val_to_struct[i]->state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002684 for (cur = policydb.cond_list; cur; cur = cur->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 rc = evaluate_cond_node(&policydb, cur);
2686 if (rc)
2687 goto out;
2688 }
2689
2690 seqno = ++latest_granting;
Eric Paris4b02b522010-11-23 11:40:08 -05002691 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692out:
James Morris0804d112008-06-06 18:40:29 +10002693 write_unlock_irq(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (!rc) {
2695 avc_ss_reset(seqno);
2696 selnl_notify_policyload(seqno);
KaiGai Kohei11904162010-09-14 18:28:39 +09002697 selinux_status_update_policyload(seqno);
Venkat Yekkirala342a0cf2007-01-26 19:03:48 -08002698 selinux_xfrm_notify_policyload();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
2700 return rc;
2701}
2702
Prarit Bhargava0fd71a62016-04-14 10:40:57 -04002703int security_get_bool_value(int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Eric Paris4b02b522010-11-23 11:40:08 -05002705 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 int len;
2707
James Morris0804d112008-06-06 18:40:29 +10002708 read_lock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Eric Paris4b02b522010-11-23 11:40:08 -05002710 rc = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 len = policydb.p_bools.nprim;
Prarit Bhargava0fd71a62016-04-14 10:40:57 -04002712 if (index >= len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Prarit Bhargava0fd71a62016-04-14 10:40:57 -04002715 rc = policydb.bool_val_to_struct[index]->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716out:
James Morris0804d112008-06-06 18:40:29 +10002717 read_unlock(&policy_rwlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 return rc;
2719}
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06002720
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002721static int security_preserve_bools(struct policydb *p)
2722{
2723 int rc, nbools = 0, *bvalues = NULL, i;
2724 char **bnames = NULL;
2725 struct cond_bool_datum *booldatum;
2726 struct cond_node *cur;
2727
2728 rc = security_get_bools(&nbools, &bnames, &bvalues);
2729 if (rc)
2730 goto out;
2731 for (i = 0; i < nbools; i++) {
2732 booldatum = hashtab_search(p->p_bools.table, bnames[i]);
2733 if (booldatum)
2734 booldatum->state = bvalues[i];
2735 }
Vesa-Matti Karidbc74c62008-08-07 03:18:20 +03002736 for (cur = p->cond_list; cur; cur = cur->next) {
Stephen Smalleye900a7d2007-04-19 14:16:19 -04002737 rc = evaluate_cond_node(p, cur);
2738 if (rc)
2739 goto out;
2740 }
2741
2742out:
2743 if (bnames) {
2744 for (i = 0; i < nbools; i++)
2745 kfree(bnames[i]);
2746 }
2747 kfree(bnames);
2748 kfree(bvalues);
2749 return rc;
2750}
2751
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002752/*
2753 * security_sid_mls_copy() - computes a new sid based on the given
2754 * sid and the mls portion of mls_sid.
2755 */
2756int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid)
2757{
2758 struct context *context1;
2759 struct context *context2;
2760 struct context newcon;
2761 char *s;
2762 u32 len;
Eric Paris4b02b522010-11-23 11:40:08 -05002763 int rc;
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002764
Eric Paris4b02b522010-11-23 11:40:08 -05002765 rc = 0;
Guido Trentalancia0719aaf2010-02-03 16:40:20 +01002766 if (!ss_initialized || !policydb.mls_enabled) {
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002767 *new_sid = sid;
2768 goto out;
2769 }
2770
2771 context_init(&newcon);
2772
James Morris0804d112008-06-06 18:40:29 +10002773 read_lock(&policy_rwlock);
Eric Paris4b02b522010-11-23 11:40:08 -05002774
2775 rc = -EINVAL;
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002776 context1 = sidtab_search(&sidtab, sid);
2777 if (!context1) {
Eric Paris744ba352008-04-17 11:52:44 -04002778 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2779 __func__, sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002780 goto out_unlock;
2781 }
2782
Eric Paris4b02b522010-11-23 11:40:08 -05002783 rc = -EINVAL;
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002784 context2 = sidtab_search(&sidtab, mls_sid);
2785 if (!context2) {
Eric Paris744ba352008-04-17 11:52:44 -04002786 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2787 __func__, mls_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002788 goto out_unlock;
2789 }
2790
2791 newcon.user = context1->user;
2792 newcon.role = context1->role;
2793 newcon.type = context1->type;
Venkat Yekkirala0efc61e2006-12-12 13:02:41 -06002794 rc = mls_context_cpy(&newcon, context2);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002795 if (rc)
2796 goto out_unlock;
2797
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002798 /* Check the validity of the new context. */
2799 if (!policydb_context_isvalid(&policydb, &newcon)) {
2800 rc = convert_context_handle_invalid_context(&newcon);
Eric Paris4b02b522010-11-23 11:40:08 -05002801 if (rc) {
2802 if (!context_struct_to_string(&newcon, &s, &len)) {
Richard Guy Briggs4093a842014-09-18 20:47:48 -04002803 audit_log(current->audit_context,
2804 GFP_ATOMIC, AUDIT_SELINUX_ERR,
2805 "op=security_sid_mls_copy "
2806 "invalid_context=%s", s);
Eric Paris4b02b522010-11-23 11:40:08 -05002807 kfree(s);
2808 }
2809 goto out_unlock;
2810 }
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002811 }
2812
2813 rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002814out_unlock:
James Morris0804d112008-06-06 18:40:29 +10002815 read_unlock(&policy_rwlock);
Venkat Yekkirala08554d62006-07-24 23:27:16 -07002816 context_destroy(&newcon);
2817out:
2818 return rc;
2819}
2820
Paul Moore220deb92008-01-29 08:38:23 -05002821/**
2822 * security_net_peersid_resolve - Compare and resolve two network peer SIDs
2823 * @nlbl_sid: NetLabel SID
2824 * @nlbl_type: NetLabel labeling protocol type
2825 * @xfrm_sid: XFRM SID
2826 *
2827 * Description:
2828 * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be
2829 * resolved into a single SID it is returned via @peer_sid and the function
2830 * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function
2831 * returns a negative value. A table summarizing the behavior is below:
2832 *
2833 * | function return | @sid
2834 * ------------------------------+-----------------+-----------------
2835 * no peer labels | 0 | SECSID_NULL
2836 * single peer label | 0 | <peer_label>
2837 * multiple, consistent labels | 0 | <peer_label>
2838 * multiple, inconsistent labels | -<errno> | SECSID_NULL
2839 *
2840 */
2841int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type,
2842 u32 xfrm_sid,
2843 u32 *peer_sid)
2844{
2845 int rc;
2846 struct context *nlbl_ctx;
2847 struct context *xfrm_ctx;
2848
Eric Paris4b02b522010-11-23 11:40:08 -05002849 *peer_sid = SECSID_NULL;
2850
Paul Moore220deb92008-01-29 08:38:23 -05002851 /* handle the common (which also happens to be the set of easy) cases
2852 * right away, these two if statements catch everything involving a
2853 * single or absent peer SID/label */
2854 if (xfrm_sid == SECSID_NULL) {
2855 *peer_sid = nlbl_sid;
2856 return 0;
2857 }
2858 /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label
2859 * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label
2860 * is present */
2861 if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) {
2862 *peer_sid = xfrm_sid;
2863 return 0;
2864 }
2865
2866 /* we don't need to check ss_initialized here since the only way both
2867 * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the
2868 * security server was initialized and ss_initialized was true */
Eric Paris4b02b522010-11-23 11:40:08 -05002869 if (!policydb.mls_enabled)
Paul Moore220deb92008-01-29 08:38:23 -05002870 return 0;
Paul Moore220deb92008-01-29 08:38:23 -05002871
James Morris0804d112008-06-06 18:40:29 +10002872 read_lock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002873
Eric Paris4b02b522010-11-23 11:40:08 -05002874 rc = -EINVAL;
Paul Moore220deb92008-01-29 08:38:23 -05002875 nlbl_ctx = sidtab_search(&sidtab, nlbl_sid);
2876 if (!nlbl_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002877 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2878 __func__, nlbl_sid);
Eric Paris4b02b522010-11-23 11:40:08 -05002879 goto out;
Paul Moore220deb92008-01-29 08:38:23 -05002880 }
Eric Paris4b02b522010-11-23 11:40:08 -05002881 rc = -EINVAL;
Paul Moore220deb92008-01-29 08:38:23 -05002882 xfrm_ctx = sidtab_search(&sidtab, xfrm_sid);
2883 if (!xfrm_ctx) {
Eric Paris744ba352008-04-17 11:52:44 -04002884 printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n",
2885 __func__, xfrm_sid);
Eric Paris4b02b522010-11-23 11:40:08 -05002886 goto out;
Paul Moore220deb92008-01-29 08:38:23 -05002887 }
2888 rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES);
Eric Paris4b02b522010-11-23 11:40:08 -05002889 if (rc)
2890 goto out;
Paul Moore220deb92008-01-29 08:38:23 -05002891
Eric Paris4b02b522010-11-23 11:40:08 -05002892 /* at present NetLabel SIDs/labels really only carry MLS
2893 * information so if the MLS portion of the NetLabel SID
2894 * matches the MLS portion of the labeled XFRM SID/label
2895 * then pass along the XFRM SID as it is the most
2896 * expressive */
2897 *peer_sid = xfrm_sid;
2898out:
James Morris0804d112008-06-06 18:40:29 +10002899 read_unlock(&policy_rwlock);
Paul Moore220deb92008-01-29 08:38:23 -05002900 return rc;
2901}
2902
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002903static int get_classes_callback(void *k, void *d, void *args)
2904{
2905 struct class_datum *datum = d;
2906 char *name = k, **classes = args;
2907 int value = datum->value - 1;
2908
2909 classes[value] = kstrdup(name, GFP_ATOMIC);
2910 if (!classes[value])
2911 return -ENOMEM;
2912
2913 return 0;
2914}
2915
2916int security_get_classes(char ***classes, int *nclasses)
2917{
Eric Paris4b02b522010-11-23 11:40:08 -05002918 int rc;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002919
James Morris0804d112008-06-06 18:40:29 +10002920 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002921
Eric Paris4b02b522010-11-23 11:40:08 -05002922 rc = -ENOMEM;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002923 *nclasses = policydb.p_classes.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002924 *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002925 if (!*classes)
2926 goto out;
2927
2928 rc = hashtab_map(policydb.p_classes.table, get_classes_callback,
2929 *classes);
Eric Paris4b02b522010-11-23 11:40:08 -05002930 if (rc) {
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002931 int i;
2932 for (i = 0; i < *nclasses; i++)
2933 kfree((*classes)[i]);
2934 kfree(*classes);
2935 }
2936
2937out:
James Morris0804d112008-06-06 18:40:29 +10002938 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002939 return rc;
2940}
2941
2942static int get_permissions_callback(void *k, void *d, void *args)
2943{
2944 struct perm_datum *datum = d;
2945 char *name = k, **perms = args;
2946 int value = datum->value - 1;
2947
2948 perms[value] = kstrdup(name, GFP_ATOMIC);
2949 if (!perms[value])
2950 return -ENOMEM;
2951
2952 return 0;
2953}
2954
2955int security_get_permissions(char *class, char ***perms, int *nperms)
2956{
Eric Paris4b02b522010-11-23 11:40:08 -05002957 int rc, i;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002958 struct class_datum *match;
2959
James Morris0804d112008-06-06 18:40:29 +10002960 read_lock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002961
Eric Paris4b02b522010-11-23 11:40:08 -05002962 rc = -EINVAL;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002963 match = hashtab_search(policydb.p_classes.table, class);
2964 if (!match) {
Eric Paris744ba352008-04-17 11:52:44 -04002965 printk(KERN_ERR "SELinux: %s: unrecognized class %s\n",
Harvey Harrisondd6f9532008-03-06 10:03:59 +11002966 __func__, class);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002967 goto out;
2968 }
2969
Eric Paris4b02b522010-11-23 11:40:08 -05002970 rc = -ENOMEM;
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002971 *nperms = match->permissions.nprim;
Julia Lawall9f59f902009-12-06 10:16:51 +01002972 *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002973 if (!*perms)
2974 goto out;
2975
2976 if (match->comdatum) {
2977 rc = hashtab_map(match->comdatum->permissions.table,
2978 get_permissions_callback, *perms);
Eric Paris4b02b522010-11-23 11:40:08 -05002979 if (rc)
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002980 goto err;
2981 }
2982
2983 rc = hashtab_map(match->permissions.table, get_permissions_callback,
2984 *perms);
Eric Paris4b02b522010-11-23 11:40:08 -05002985 if (rc)
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002986 goto err;
2987
2988out:
James Morris0804d112008-06-06 18:40:29 +10002989 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002990 return rc;
2991
2992err:
James Morris0804d112008-06-06 18:40:29 +10002993 read_unlock(&policy_rwlock);
Christopher J. PeBenito55fcf092007-05-23 09:12:06 -04002994 for (i = 0; i < *nperms; i++)
2995 kfree((*perms)[i]);
2996 kfree(*perms);
2997 return rc;
2998}
2999
Eric Paris3f120702007-09-21 14:37:10 -04003000int security_get_reject_unknown(void)
3001{
3002 return policydb.reject_unknown;
3003}
3004
3005int security_get_allow_unknown(void)
3006{
3007 return policydb.allow_unknown;
3008}
3009
Paul Moore3bb56b22008-01-29 08:38:19 -05003010/**
Paul Moore3bb56b22008-01-29 08:38:19 -05003011 * security_policycap_supported - Check for a specific policy capability
3012 * @req_cap: capability
3013 *
3014 * Description:
3015 * This function queries the currently loaded policy to see if it supports the
3016 * capability specified by @req_cap. Returns true (1) if the capability is
3017 * supported, false (0) if it isn't supported.
3018 *
3019 */
3020int security_policycap_supported(unsigned int req_cap)
3021{
3022 int rc;
3023
James Morris0804d112008-06-06 18:40:29 +10003024 read_lock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05003025 rc = ebitmap_get_bit(&policydb.policycaps, req_cap);
James Morris0804d112008-06-06 18:40:29 +10003026 read_unlock(&policy_rwlock);
Paul Moore3bb56b22008-01-29 08:38:19 -05003027
3028 return rc;
3029}
3030
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003031struct selinux_audit_rule {
3032 u32 au_seqno;
3033 struct context au_ctxt;
3034};
3035
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003036void selinux_audit_rule_free(void *vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003037{
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003038 struct selinux_audit_rule *rule = vrule;
3039
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003040 if (rule) {
3041 context_destroy(&rule->au_ctxt);
3042 kfree(rule);
3043 }
3044}
3045
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003046int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003047{
3048 struct selinux_audit_rule *tmprule;
3049 struct role_datum *roledatum;
3050 struct type_datum *typedatum;
3051 struct user_datum *userdatum;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003052 struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003053 int rc = 0;
3054
3055 *rule = NULL;
3056
3057 if (!ss_initialized)
Steve G3ad40d62007-08-14 12:50:46 -07003058 return -EOPNOTSUPP;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003059
3060 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003061 case AUDIT_SUBJ_USER:
3062 case AUDIT_SUBJ_ROLE:
3063 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003064 case AUDIT_OBJ_USER:
3065 case AUDIT_OBJ_ROLE:
3066 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003067 /* only 'equals' and 'not equals' fit user, role, and type */
Al Viro5af75d82008-12-16 05:59:26 -05003068 if (op != Audit_equal && op != Audit_not_equal)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003069 return -EINVAL;
3070 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003071 case AUDIT_SUBJ_SEN:
3072 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003073 case AUDIT_OBJ_LEV_LOW:
3074 case AUDIT_OBJ_LEV_HIGH:
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003075 /* we do not allow a range, indicated by the presence of '-' */
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003076 if (strchr(rulestr, '-'))
3077 return -EINVAL;
3078 break;
3079 default:
3080 /* only the above fields are valid */
3081 return -EINVAL;
3082 }
3083
3084 tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL);
3085 if (!tmprule)
3086 return -ENOMEM;
3087
3088 context_init(&tmprule->au_ctxt);
3089
James Morris0804d112008-06-06 18:40:29 +10003090 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003091
3092 tmprule->au_seqno = latest_granting;
3093
3094 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003095 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003096 case AUDIT_OBJ_USER:
Eric Paris4b02b522010-11-23 11:40:08 -05003097 rc = -EINVAL;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003098 userdatum = hashtab_search(policydb.p_users.table, rulestr);
3099 if (!userdatum)
Eric Paris4b02b522010-11-23 11:40:08 -05003100 goto out;
3101 tmprule->au_ctxt.user = userdatum->value;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003102 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003103 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003104 case AUDIT_OBJ_ROLE:
Eric Paris4b02b522010-11-23 11:40:08 -05003105 rc = -EINVAL;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003106 roledatum = hashtab_search(policydb.p_roles.table, rulestr);
3107 if (!roledatum)
Eric Paris4b02b522010-11-23 11:40:08 -05003108 goto out;
3109 tmprule->au_ctxt.role = roledatum->value;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003110 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003111 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003112 case AUDIT_OBJ_TYPE:
Eric Paris4b02b522010-11-23 11:40:08 -05003113 rc = -EINVAL;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003114 typedatum = hashtab_search(policydb.p_types.table, rulestr);
3115 if (!typedatum)
Eric Paris4b02b522010-11-23 11:40:08 -05003116 goto out;
3117 tmprule->au_ctxt.type = typedatum->value;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003118 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003119 case AUDIT_SUBJ_SEN:
3120 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003121 case AUDIT_OBJ_LEV_LOW:
3122 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003123 rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC);
Eric Paris4b02b522010-11-23 11:40:08 -05003124 if (rc)
3125 goto out;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003126 break;
3127 }
Eric Paris4b02b522010-11-23 11:40:08 -05003128 rc = 0;
3129out:
James Morris0804d112008-06-06 18:40:29 +10003130 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003131
3132 if (rc) {
3133 selinux_audit_rule_free(tmprule);
3134 tmprule = NULL;
3135 }
3136
3137 *rule = tmprule;
3138
3139 return rc;
3140}
3141
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003142/* Check to see if the rule contains any selinux fields */
3143int selinux_audit_rule_known(struct audit_krule *rule)
3144{
3145 int i;
3146
3147 for (i = 0; i < rule->field_count; i++) {
3148 struct audit_field *f = &rule->fields[i];
3149 switch (f->type) {
3150 case AUDIT_SUBJ_USER:
3151 case AUDIT_SUBJ_ROLE:
3152 case AUDIT_SUBJ_TYPE:
3153 case AUDIT_SUBJ_SEN:
3154 case AUDIT_SUBJ_CLR:
3155 case AUDIT_OBJ_USER:
3156 case AUDIT_OBJ_ROLE:
3157 case AUDIT_OBJ_TYPE:
3158 case AUDIT_OBJ_LEV_LOW:
3159 case AUDIT_OBJ_LEV_HIGH:
3160 return 1;
3161 }
3162 }
3163
3164 return 0;
3165}
3166
3167int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule,
Eric Parisf5269712008-05-14 11:27:45 -04003168 struct audit_context *actx)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003169{
3170 struct context *ctxt;
3171 struct mls_level *level;
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003172 struct selinux_audit_rule *rule = vrule;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003173 int match = 0;
3174
Richard Guy Briggs9ad42a72013-11-21 13:31:40 -05003175 if (unlikely(!rule)) {
3176 WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n");
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003177 return -ENOENT;
3178 }
3179
James Morris0804d112008-06-06 18:40:29 +10003180 read_lock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003181
3182 if (rule->au_seqno < latest_granting) {
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003183 match = -ESTALE;
3184 goto out;
3185 }
3186
Stephen Smalley9a2f44f2006-09-25 23:31:58 -07003187 ctxt = sidtab_search(&sidtab, sid);
Richard Guy Briggs9ad42a72013-11-21 13:31:40 -05003188 if (unlikely(!ctxt)) {
3189 WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n",
Eric Paris5d55a342008-04-18 17:38:33 -04003190 sid);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003191 match = -ENOENT;
3192 goto out;
3193 }
3194
3195 /* a field/op pair that is not caught here will simply fall through
3196 without a match */
3197 switch (field) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003198 case AUDIT_SUBJ_USER:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003199 case AUDIT_OBJ_USER:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003200 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05003201 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003202 match = (ctxt->user == rule->au_ctxt.user);
3203 break;
Al Viro5af75d82008-12-16 05:59:26 -05003204 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003205 match = (ctxt->user != rule->au_ctxt.user);
3206 break;
3207 }
3208 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003209 case AUDIT_SUBJ_ROLE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003210 case AUDIT_OBJ_ROLE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003211 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05003212 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003213 match = (ctxt->role == rule->au_ctxt.role);
3214 break;
Al Viro5af75d82008-12-16 05:59:26 -05003215 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003216 match = (ctxt->role != rule->au_ctxt.role);
3217 break;
3218 }
3219 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003220 case AUDIT_SUBJ_TYPE:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003221 case AUDIT_OBJ_TYPE:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003222 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05003223 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003224 match = (ctxt->type == rule->au_ctxt.type);
3225 break;
Al Viro5af75d82008-12-16 05:59:26 -05003226 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003227 match = (ctxt->type != rule->au_ctxt.type);
3228 break;
3229 }
3230 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -05003231 case AUDIT_SUBJ_SEN:
3232 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -05003233 case AUDIT_OBJ_LEV_LOW:
3234 case AUDIT_OBJ_LEV_HIGH:
3235 level = ((field == AUDIT_SUBJ_SEN ||
Eric Paris5d55a342008-04-18 17:38:33 -04003236 field == AUDIT_OBJ_LEV_LOW) ?
3237 &ctxt->range.level[0] : &ctxt->range.level[1]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003238 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05003239 case Audit_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003240 match = mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04003241 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003242 break;
Al Viro5af75d82008-12-16 05:59:26 -05003243 case Audit_not_equal:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003244 match = !mls_level_eq(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04003245 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003246 break;
Al Viro5af75d82008-12-16 05:59:26 -05003247 case Audit_lt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003248 match = (mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04003249 level) &&
3250 !mls_level_eq(&rule->au_ctxt.range.level[0],
3251 level));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003252 break;
Al Viro5af75d82008-12-16 05:59:26 -05003253 case Audit_le:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003254 match = mls_level_dom(&rule->au_ctxt.range.level[0],
Eric Paris5d55a342008-04-18 17:38:33 -04003255 level);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003256 break;
Al Viro5af75d82008-12-16 05:59:26 -05003257 case Audit_gt:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003258 match = (mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04003259 &rule->au_ctxt.range.level[0]) &&
3260 !mls_level_eq(level,
3261 &rule->au_ctxt.range.level[0]));
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003262 break;
Al Viro5af75d82008-12-16 05:59:26 -05003263 case Audit_ge:
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003264 match = mls_level_dom(level,
Eric Paris5d55a342008-04-18 17:38:33 -04003265 &rule->au_ctxt.range.level[0]);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003266 break;
3267 }
3268 }
3269
3270out:
James Morris0804d112008-06-06 18:40:29 +10003271 read_unlock(&policy_rwlock);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003272 return match;
3273}
3274
Ahmed S. Darwish9d57a7f2008-03-01 22:03:14 +02003275static int (*aurule_callback)(void) = audit_update_lsm_rules;
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003276
Wanlong Gao562c99f2012-03-07 22:17:14 +08003277static int aurule_avc_callback(u32 event)
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003278{
3279 int err = 0;
3280
3281 if (event == AVC_CALLBACK_RESET && aurule_callback)
3282 err = aurule_callback();
3283 return err;
3284}
3285
3286static int __init aurule_init(void)
3287{
3288 int err;
3289
Wanlong Gao562c99f2012-03-07 22:17:14 +08003290 err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET);
Darrel Goeddel376bd9c2006-02-24 15:44:05 -06003291 if (err)
3292 panic("avc_add_callback() failed, error %d\n", err);
3293
3294 return err;
3295}
3296__initcall(aurule_init);
3297
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003298#ifdef CONFIG_NETLABEL
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003299/**
Paul Moore5778eab2007-02-28 15:14:22 -05003300 * security_netlbl_cache_add - Add an entry to the NetLabel cache
3301 * @secattr: the NetLabel packet security attributes
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003302 * @sid: the SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003303 *
3304 * Description:
3305 * Attempt to cache the context in @ctx, which was derived from the packet in
Paul Moore5778eab2007-02-28 15:14:22 -05003306 * @skb, in the NetLabel subsystem cache. This function assumes @secattr has
3307 * already been initialized.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003308 *
3309 */
Paul Moore5778eab2007-02-28 15:14:22 -05003310static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr,
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003311 u32 sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003312{
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003313 u32 *sid_cache;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003314
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003315 sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC);
3316 if (sid_cache == NULL)
3317 return;
Paul Moore5778eab2007-02-28 15:14:22 -05003318 secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC);
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003319 if (secattr->cache == NULL) {
3320 kfree(sid_cache);
Paul Moore5778eab2007-02-28 15:14:22 -05003321 return;
Jesper Juhl0ec8abd2007-07-21 00:12:44 +02003322 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003323
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003324 *sid_cache = sid;
3325 secattr->cache->free = kfree;
3326 secattr->cache->data = sid_cache;
Paul Moore5778eab2007-02-28 15:14:22 -05003327 secattr->flags |= NETLBL_SECATTR_CACHE;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003328}
3329
3330/**
Paul Moore5778eab2007-02-28 15:14:22 -05003331 * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003332 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003333 * @sid: the SELinux SID
3334 *
3335 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05003336 * Convert the given NetLabel security attributes in @secattr into a
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003337 * SELinux SID. If the @secattr field does not contain a full SELinux
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003338 * SID/context then use SECINITSID_NETMSG as the foundation. If possible the
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003339 * 'cache' field of @secattr is set and the CACHE flag is set; this is to
3340 * allow the @secattr to be used by NetLabel to cache the secattr to SID
3341 * conversion for future lookups. Returns zero on success, negative values on
3342 * failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003343 *
3344 */
Paul Moore5778eab2007-02-28 15:14:22 -05003345int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr,
Paul Moore5778eab2007-02-28 15:14:22 -05003346 u32 *sid)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003347{
Eric Paris7ae9f232010-11-23 11:40:09 -05003348 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003349 struct context *ctx;
3350 struct context ctx_new;
Paul Moore5778eab2007-02-28 15:14:22 -05003351
3352 if (!ss_initialized) {
3353 *sid = SECSID_NULL;
3354 return 0;
3355 }
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003356
James Morris0804d112008-06-06 18:40:29 +10003357 read_lock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003358
Eric Paris7ae9f232010-11-23 11:40:09 -05003359 if (secattr->flags & NETLBL_SECATTR_CACHE)
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003360 *sid = *(u32 *)secattr->cache->data;
Eric Paris7ae9f232010-11-23 11:40:09 -05003361 else if (secattr->flags & NETLBL_SECATTR_SECID)
Paul Moore16efd452008-01-29 08:37:59 -05003362 *sid = secattr->attr.secid;
Eric Paris7ae9f232010-11-23 11:40:09 -05003363 else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) {
3364 rc = -EIDRM;
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003365 ctx = sidtab_search(&sidtab, SECINITSID_NETMSG);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003366 if (ctx == NULL)
Eric Paris7ae9f232010-11-23 11:40:09 -05003367 goto out;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003368
Paul Moore81990fb2008-10-03 10:51:15 -04003369 context_init(&ctx_new);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003370 ctx_new.user = ctx->user;
3371 ctx_new.role = ctx->role;
3372 ctx_new.type = ctx->type;
Paul Moore02752762006-11-29 13:18:18 -05003373 mls_import_netlbl_lvl(&ctx_new, secattr);
Paul Moore701a90b2006-11-17 17:38:46 -05003374 if (secattr->flags & NETLBL_SECATTR_MLS_CAT) {
Paul Mooreda8026f2015-02-17 15:30:23 -05003375 rc = mls_import_netlbl_cat(&ctx_new, secattr);
Eric Paris7ae9f232010-11-23 11:40:09 -05003376 if (rc)
3377 goto out;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003378 }
Eric Paris7ae9f232010-11-23 11:40:09 -05003379 rc = -EIDRM;
3380 if (!mls_context_isvalid(&policydb, &ctx_new))
3381 goto out_free;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003382
3383 rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid);
Eric Paris7ae9f232010-11-23 11:40:09 -05003384 if (rc)
3385 goto out_free;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003386
Paul Moore5dbe1eb2008-01-29 08:44:18 -05003387 security_netlbl_cache_add(secattr, *sid);
Paul Moore5778eab2007-02-28 15:14:22 -05003388
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003389 ebitmap_destroy(&ctx_new.range.level[0].cat);
Eric Paris7ae9f232010-11-23 11:40:09 -05003390 } else
paul.moore@hp.com388b2402006-10-05 18:28:24 -04003391 *sid = SECSID_NULL;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003392
Eric Paris7ae9f232010-11-23 11:40:09 -05003393 read_unlock(&policy_rwlock);
3394 return 0;
3395out_free:
3396 ebitmap_destroy(&ctx_new.range.level[0].cat);
3397out:
James Morris0804d112008-06-06 18:40:29 +10003398 read_unlock(&policy_rwlock);
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003399 return rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003400}
3401
3402/**
Paul Moore5778eab2007-02-28 15:14:22 -05003403 * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr
3404 * @sid: the SELinux SID
3405 * @secattr: the NetLabel packet security attributes
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003406 *
3407 * Description:
Paul Moore5778eab2007-02-28 15:14:22 -05003408 * Convert the given SELinux SID in @sid into a NetLabel security attribute.
3409 * Returns zero on success, negative values on failure.
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003410 *
3411 */
Paul Moore5778eab2007-02-28 15:14:22 -05003412int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr)
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003413{
Paul Moore99d854d2008-10-10 10:16:30 -04003414 int rc;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003415 struct context *ctx;
3416
3417 if (!ss_initialized)
3418 return 0;
3419
James Morris0804d112008-06-06 18:40:29 +10003420 read_lock(&policy_rwlock);
Eric Paris4b02b522010-11-23 11:40:08 -05003421
3422 rc = -ENOENT;
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003423 ctx = sidtab_search(&sidtab, sid);
Eric Paris4b02b522010-11-23 11:40:08 -05003424 if (ctx == NULL)
3425 goto out;
3426
3427 rc = -ENOMEM;
Eric Parisac76c052010-11-29 15:47:09 -05003428 secattr->domain = kstrdup(sym_name(&policydb, SYM_TYPES, ctx->type - 1),
Paul Moore5778eab2007-02-28 15:14:22 -05003429 GFP_ATOMIC);
Eric Paris4b02b522010-11-23 11:40:08 -05003430 if (secattr->domain == NULL)
3431 goto out;
3432
Paul Moore8d758992008-10-10 10:16:33 -04003433 secattr->attr.secid = sid;
3434 secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID;
Paul Moore5778eab2007-02-28 15:14:22 -05003435 mls_export_netlbl_lvl(ctx, secattr);
3436 rc = mls_export_netlbl_cat(ctx, secattr);
Eric Paris4b02b522010-11-23 11:40:08 -05003437out:
James Morris0804d112008-06-06 18:40:29 +10003438 read_unlock(&policy_rwlock);
Paul Mooref8687af2006-10-30 15:22:15 -08003439 return rc;
3440}
Venkat Yekkirala7420ed22006-08-04 23:17:57 -07003441#endif /* CONFIG_NETLABEL */
Eric Pariscee74f42010-10-13 17:50:25 -04003442
3443/**
3444 * security_read_policy - read the policy.
3445 * @data: binary policy data
3446 * @len: length of data in bytes
3447 *
3448 */
Eric Paris6b697322011-04-20 10:21:28 -04003449int security_read_policy(void **data, size_t *len)
Eric Pariscee74f42010-10-13 17:50:25 -04003450{
3451 int rc;
3452 struct policy_file fp;
3453
3454 if (!ss_initialized)
3455 return -EINVAL;
3456
3457 *len = security_policydb_len();
3458
Eric Paris845ca302010-10-13 17:50:31 -04003459 *data = vmalloc_user(*len);
Eric Pariscee74f42010-10-13 17:50:25 -04003460 if (!*data)
3461 return -ENOMEM;
3462
3463 fp.data = *data;
3464 fp.len = *len;
3465
3466 read_lock(&policy_rwlock);
3467 rc = policydb_write(&policydb, &fp);
3468 read_unlock(&policy_rwlock);
3469
3470 if (rc)
3471 return rc;
3472
3473 *len = (unsigned long)fp.data - (unsigned long)*data;
3474 return 0;
3475
3476}