]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Implementation of the security services. | |
3 | * | |
4 | * Authors : Stephen Smalley, <sds@epoch.ncsc.mil> | |
5d55a345 | 5 | * James Morris <jmorris@redhat.com> |
1da177e4 LT |
6 | * |
7 | * Updated: Trusted Computer Solutions, Inc. <dgoeddel@trustedcs.com> | |
8 | * | |
9 | * Support for enhanced MLS infrastructure. | |
376bd9cb | 10 | * Support for context based audit filters. |
1da177e4 LT |
11 | * |
12 | * Updated: Frank Mayer <mayerf@tresys.com> and Karl MacMillan <kmacmillan@tresys.com> | |
13 | * | |
5d55a345 | 14 | * Added conditional policy language extensions |
1da177e4 | 15 | * |
82c21bfa | 16 | * Updated: Hewlett-Packard <paul@paul-moore.com> |
7420ed23 VY |
17 | * |
18 | * Added support for NetLabel | |
3bb56b25 | 19 | * Added support for the policy capability bitmap |
7420ed23 | 20 | * |
b94c7e67 CS |
21 | * Updated: Chad Sellers <csellers@tresys.com> |
22 | * | |
23 | * Added validation of kernel classes and permissions | |
24 | * | |
44c2d9bd KK |
25 | * Updated: KaiGai Kohei <kaigai@ak.jp.nec.com> |
26 | * | |
27 | * Added support for bounds domain and audit messaged on masked permissions | |
28 | * | |
0719aaf5 GT |
29 | * Updated: Guido Trentalancia <guido@trentalancia.com> |
30 | * | |
31 | * Added support for runtime switching of the policy type | |
32 | * | |
44c2d9bd | 33 | * Copyright (C) 2008, 2009 NEC Corporation |
3bb56b25 | 34 | * Copyright (C) 2006, 2007 Hewlett-Packard Development Company, L.P. |
376bd9cb | 35 | * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc. |
b94c7e67 | 36 | * Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC |
1da177e4 LT |
37 | * Copyright (C) 2003 Red Hat, Inc., James Morris <jmorris@redhat.com> |
38 | * This program is free software; you can redistribute it and/or modify | |
5d55a345 | 39 | * it under the terms of the GNU General Public License as published by |
1da177e4 LT |
40 | * 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> | |
9f2ad665 | 46 | #include <linux/rcupdate.h> |
1da177e4 LT |
47 | #include <linux/errno.h> |
48 | #include <linux/in.h> | |
49 | #include <linux/sched.h> | |
50 | #include <linux/audit.h> | |
bb003079 | 51 | #include <linux/mutex.h> |
0e55a004 | 52 | #include <linux/selinux.h> |
6371dcd3 | 53 | #include <linux/flex_array.h> |
f0d3d989 | 54 | #include <linux/vmalloc.h> |
7420ed23 | 55 | #include <net/netlabel.h> |
bb003079 | 56 | |
1da177e4 LT |
57 | #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" | |
7420ed23 | 67 | #include "objsec.h" |
c60475bf | 68 | #include "netlabel.h" |
3de4bab5 | 69 | #include "xfrm.h" |
02752760 | 70 | #include "ebitmap.h" |
9d57a7f9 | 71 | #include "audit.h" |
1da177e4 | 72 | |
4dc2fce3 SS |
73 | /* Policy capability names */ |
74 | char *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 | ||
3bb56b25 | 82 | int selinux_policycap_netpeer; |
b0c636b9 | 83 | int selinux_policycap_openperm; |
da69a530 | 84 | int selinux_policycap_extsockclass; |
2be4d74f | 85 | int selinux_policycap_alwaysnetwork; |
2651225b | 86 | int selinux_policycap_cgroupseclabel; |
3bb56b25 | 87 | |
1da177e4 | 88 | static DEFINE_RWLOCK(policy_rwlock); |
1da177e4 LT |
89 | |
90 | static struct sidtab sidtab; | |
91 | struct policydb policydb; | |
5d55a345 | 92 | int ss_initialized; |
1da177e4 LT |
93 | |
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 | */ | |
5d55a345 | 100 | static u32 latest_granting; |
1da177e4 LT |
101 | |
102 | /* Forward declaration. */ | |
103 | static int context_struct_to_string(struct context *context, char **scontext, | |
104 | u32 *scontext_len); | |
105 | ||
19439d05 | 106 | static void context_struct_compute_av(struct context *scontext, |
fa1aa143 JVS |
107 | struct context *tcontext, |
108 | u16 tclass, | |
109 | struct av_decision *avd, | |
110 | struct extended_perms *xperms); | |
c6d3aaa4 SS |
111 | |
112 | struct selinux_mapping { | |
113 | u16 value; /* policy value */ | |
114 | unsigned num_perms; | |
115 | u32 perms[sizeof(u32) * 8]; | |
116 | }; | |
117 | ||
118 | static struct selinux_mapping *current_mapping; | |
119 | static u16 current_mapping_size; | |
120 | ||
121 | static 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; | |
342e9157 | 169 | while (p_in->perms[k]) { |
c6d3aaa4 SS |
170 | /* 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; | |
198 | err: | |
199 | kfree(out_map); | |
200 | return -EINVAL; | |
201 | } | |
202 | ||
203 | /* | |
204 | * Get real, policy values from mapped values | |
205 | */ | |
206 | ||
207 | static u16 unmap_class(u16 tclass) | |
208 | { | |
209 | if (tclass < current_mapping_size) | |
210 | return current_mapping[tclass].value; | |
211 | ||
212 | return tclass; | |
213 | } | |
214 | ||
6f5317e7 HC |
215 | /* |
216 | * Get kernel value for class from its policy value | |
217 | */ | |
218 | static 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 | ||
85cd6da5 | 227 | return SECCLASS_NULL; |
6f5317e7 HC |
228 | } |
229 | ||
c6d3aaa4 SS |
230 | static 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 | } | |
0bce9527 EP |
256 | /* |
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; | |
c6d3aaa4 SS |
263 | avd->auditdeny = result; |
264 | } | |
265 | } | |
266 | ||
0719aaf5 GT |
267 | int security_mls_enabled(void) |
268 | { | |
269 | return policydb.mls_enabled; | |
270 | } | |
c6d3aaa4 | 271 | |
1da177e4 LT |
272 | /* |
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 | */ | |
283 | static 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--; | |
c1a7368a | 305 | s[sp] &= s[sp + 1]; |
1da177e4 LT |
306 | break; |
307 | case CEXPR_OR: | |
308 | BUG_ON(sp < 1); | |
309 | sp--; | |
c1a7368a | 310 | s[sp] |= s[sp + 1]; |
1da177e4 LT |
311 | break; |
312 | case CEXPR_ATTR: | |
c1a7368a | 313 | if (sp == (CEXPR_MAXDEPTH - 1)) |
1da177e4 LT |
314 | 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: | |
5d55a345 EP |
339 | s[++sp] = (!ebitmap_get_bit(&r1->dominates, |
340 | val2 - 1) && | |
341 | !ebitmap_get_bit(&r2->dominates, | |
342 | val1 - 1)); | |
1da177e4 LT |
343 | 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; | |
372 | mls_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 | ||
44c2d9bd KK |
457 | /* |
458 | * security_dump_masked_av - dumps masked permissions during | |
459 | * security_compute_av due to RBAC, MLS/Constraint and Type bounds. | |
460 | */ | |
461 | static 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 | ||
473 | static 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]; | |
2da5d31b JM |
486 | int index; |
487 | u32 length; | |
44c2d9bd KK |
488 | bool need_comma = false; |
489 | ||
490 | if (!permissions) | |
491 | return; | |
492 | ||
ac76c05b | 493 | tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1); |
44c2d9bd KK |
494 | 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); | |
539 | out: | |
540 | /* release scontext/tcontext */ | |
541 | kfree(tcontext_name); | |
542 | kfree(scontext_name); | |
543 | ||
544 | return; | |
545 | } | |
546 | ||
d9250dea KK |
547 | /* |
548 | * security_boundary_permission - drops violated permissions | |
549 | * on boundary constraint. | |
550 | */ | |
551 | static void type_attribute_bounds_av(struct context *scontext, | |
552 | struct context *tcontext, | |
553 | u16 tclass, | |
d9250dea KK |
554 | struct av_decision *avd) |
555 | { | |
2ae3ba39 | 556 | struct context lo_scontext; |
7ea59202 | 557 | struct context lo_tcontext, *tcontextp = tcontext; |
2ae3ba39 | 558 | struct av_decision lo_avd; |
23bdecb0 EP |
559 | struct type_datum *source; |
560 | struct type_datum *target; | |
2ae3ba39 | 561 | u32 masked = 0; |
d9250dea | 562 | |
23bdecb0 EP |
563 | source = flex_array_get_ptr(policydb.type_val_to_struct_array, |
564 | scontext->type - 1); | |
565 | BUG_ON(!source); | |
566 | ||
7ea59202 SS |
567 | if (!source->bounds) |
568 | return; | |
569 | ||
23bdecb0 EP |
570 | target = flex_array_get_ptr(policydb.type_val_to_struct_array, |
571 | tcontext->type - 1); | |
572 | BUG_ON(!target); | |
573 | ||
7ea59202 | 574 | memset(&lo_avd, 0, sizeof(lo_avd)); |
d9250dea | 575 | |
7ea59202 SS |
576 | memcpy(&lo_scontext, scontext, sizeof(lo_scontext)); |
577 | lo_scontext.type = source->bounds; | |
2ae3ba39 KK |
578 | |
579 | if (target->bounds) { | |
2ae3ba39 KK |
580 | memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext)); |
581 | lo_tcontext.type = target->bounds; | |
7ea59202 | 582 | tcontextp = &lo_tcontext; |
2ae3ba39 KK |
583 | } |
584 | ||
7ea59202 SS |
585 | context_struct_compute_av(&lo_scontext, |
586 | tcontextp, | |
587 | tclass, | |
588 | &lo_avd, | |
589 | NULL); | |
2ae3ba39 | 590 | |
7ea59202 | 591 | masked = ~lo_avd.allowed & avd->allowed; |
d9250dea | 592 | |
7ea59202 SS |
593 | if (likely(!masked)) |
594 | return; /* no masked permission */ | |
d9250dea | 595 | |
7ea59202 SS |
596 | /* mask violated permissions */ |
597 | avd->allowed &= ~masked; | |
598 | ||
599 | /* audit masked permissions */ | |
600 | security_dump_masked_av(scontext, tcontext, | |
601 | tclass, masked, "bounds"); | |
d9250dea KK |
602 | } |
603 | ||
1da177e4 | 604 | /* |
fa1aa143 JVS |
605 | * flag which drivers have permissions |
606 | * only looking for ioctl based extended permssions | |
607 | */ | |
608 | void 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. | |
1da177e4 | 632 | */ |
19439d05 | 633 | static void context_struct_compute_av(struct context *scontext, |
fa1aa143 JVS |
634 | struct context *tcontext, |
635 | u16 tclass, | |
636 | struct av_decision *avd, | |
637 | struct extended_perms *xperms) | |
1da177e4 LT |
638 | { |
639 | struct constraint_node *constraint; | |
640 | struct role_allow *ra; | |
641 | struct avtab_key avkey; | |
782ebb99 | 642 | struct avtab_node *node; |
1da177e4 | 643 | struct class_datum *tclass_datum; |
782ebb99 SS |
644 | struct ebitmap *sattr, *tattr; |
645 | struct ebitmap_node *snode, *tnode; | |
646 | unsigned int i, j; | |
1da177e4 | 647 | |
1da177e4 | 648 | avd->allowed = 0; |
1da177e4 LT |
649 | avd->auditallow = 0; |
650 | avd->auditdeny = 0xffffffff; | |
fa1aa143 JVS |
651 | if (xperms) { |
652 | memset(&xperms->drivers, 0, sizeof(xperms->drivers)); | |
653 | xperms->len = 0; | |
654 | } | |
1da177e4 | 655 | |
c6d3aaa4 SS |
656 | if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) { |
657 | if (printk_ratelimit()) | |
658 | printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass); | |
19439d05 | 659 | return; |
c6d3aaa4 | 660 | } |
3f12070e EP |
661 | |
662 | tclass_datum = policydb.class_val_to_struct[tclass - 1]; | |
663 | ||
1da177e4 LT |
664 | /* |
665 | * If a specific type enforcement rule was defined for | |
666 | * this permission check, then use it. | |
667 | */ | |
1da177e4 | 668 | avkey.target_class = tclass; |
fa1aa143 | 669 | avkey.specified = AVTAB_AV | AVTAB_XPERMS; |
6371dcd3 EP |
670 | 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); | |
9fe79ad1 KK |
674 | ebitmap_for_each_positive_bit(sattr, snode, i) { |
675 | ebitmap_for_each_positive_bit(tattr, tnode, j) { | |
782ebb99 SS |
676 | avkey.source_type = i + 1; |
677 | avkey.target_type = j + 1; | |
678 | for (node = avtab_search_node(&policydb.te_avtab, &avkey); | |
dbc74c65 | 679 | node; |
782ebb99 SS |
680 | node = avtab_search_node_next(node, avkey.specified)) { |
681 | if (node->key.specified == AVTAB_ALLOWED) | |
fa1aa143 | 682 | avd->allowed |= node->datum.u.data; |
782ebb99 | 683 | else if (node->key.specified == AVTAB_AUDITALLOW) |
fa1aa143 | 684 | avd->auditallow |= node->datum.u.data; |
782ebb99 | 685 | else if (node->key.specified == AVTAB_AUDITDENY) |
fa1aa143 JVS |
686 | avd->auditdeny &= node->datum.u.data; |
687 | else if (xperms && (node->key.specified & AVTAB_XPERMS)) | |
688 | services_compute_xperms_drivers(xperms, node); | |
782ebb99 | 689 | } |
1da177e4 | 690 | |
782ebb99 | 691 | /* Check conditional av table for additional permissions */ |
fa1aa143 JVS |
692 | cond_compute_av(&policydb.te_cond_avtab, &avkey, |
693 | avd, xperms); | |
782ebb99 SS |
694 | |
695 | } | |
696 | } | |
1da177e4 LT |
697 | |
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)) { | |
caabbdc0 | 707 | avd->allowed &= ~(constraint->permissions); |
1da177e4 LT |
708 | } |
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 | */ | |
c6d3aaa4 SS |
717 | if (tclass == policydb.process_class && |
718 | (avd->allowed & policydb.process_trans_perms) && | |
1da177e4 LT |
719 | 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) | |
c6d3aaa4 | 726 | avd->allowed &= ~policydb.process_trans_perms; |
1da177e4 LT |
727 | } |
728 | ||
d9250dea KK |
729 | /* |
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, | |
19439d05 | 735 | tclass, avd); |
1da177e4 LT |
736 | } |
737 | ||
738 | static int security_validtrans_handle_fail(struct context *ocontext, | |
5d55a345 EP |
739 | struct context *ncontext, |
740 | struct context *tcontext, | |
741 | u16 tclass) | |
1da177e4 LT |
742 | { |
743 | char *o = NULL, *n = NULL, *t = NULL; | |
744 | u32 olen, nlen, tlen; | |
745 | ||
4b02b524 | 746 | if (context_struct_to_string(ocontext, &o, &olen)) |
1da177e4 | 747 | goto out; |
4b02b524 | 748 | if (context_struct_to_string(ncontext, &n, &nlen)) |
1da177e4 | 749 | goto out; |
4b02b524 | 750 | if (context_struct_to_string(tcontext, &t, &tlen)) |
1da177e4 | 751 | goto out; |
9ad9ad38 | 752 | audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
4093a844 | 753 | "op=security_validate_transition seresult=denied" |
5d55a345 | 754 | " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s", |
ac76c05b | 755 | o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1)); |
1da177e4 LT |
756 | out: |
757 | kfree(o); | |
758 | kfree(n); | |
759 | kfree(t); | |
760 | ||
761 | if (!selinux_enforcing) | |
762 | return 0; | |
763 | return -EPERM; | |
764 | } | |
765 | ||
f9df6458 AP |
766 | static int security_compute_validatetrans(u32 oldsid, u32 newsid, u32 tasksid, |
767 | u16 orig_tclass, bool user) | |
1da177e4 LT |
768 | { |
769 | struct context *ocontext; | |
770 | struct context *ncontext; | |
771 | struct context *tcontext; | |
772 | struct class_datum *tclass_datum; | |
773 | struct constraint_node *constraint; | |
c6d3aaa4 | 774 | u16 tclass; |
1da177e4 LT |
775 | int rc = 0; |
776 | ||
777 | if (!ss_initialized) | |
778 | return 0; | |
779 | ||
0804d113 | 780 | read_lock(&policy_rwlock); |
1da177e4 | 781 | |
f9df6458 AP |
782 | if (!user) |
783 | tclass = unmap_class(orig_tclass); | |
784 | else | |
785 | tclass = orig_tclass; | |
c6d3aaa4 | 786 | |
1da177e4 | 787 | if (!tclass || tclass > policydb.p_classes.nprim) { |
1da177e4 LT |
788 | 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) { | |
744ba35e EP |
795 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
796 | __func__, oldsid); | |
1da177e4 LT |
797 | rc = -EINVAL; |
798 | goto out; | |
799 | } | |
800 | ||
801 | ncontext = sidtab_search(&sidtab, newsid); | |
802 | if (!ncontext) { | |
744ba35e EP |
803 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
804 | __func__, newsid); | |
1da177e4 LT |
805 | rc = -EINVAL; |
806 | goto out; | |
807 | } | |
808 | ||
809 | tcontext = sidtab_search(&sidtab, tasksid); | |
810 | if (!tcontext) { | |
744ba35e EP |
811 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
812 | __func__, tasksid); | |
1da177e4 LT |
813 | rc = -EINVAL; |
814 | goto out; | |
815 | } | |
816 | ||
817 | constraint = tclass_datum->validatetrans; | |
818 | while (constraint) { | |
819 | if (!constraint_expr_eval(ocontext, ncontext, tcontext, | |
5d55a345 | 820 | constraint->expr)) { |
f9df6458 AP |
821 | if (user) |
822 | rc = -EPERM; | |
823 | else | |
824 | rc = security_validtrans_handle_fail(ocontext, | |
825 | ncontext, | |
826 | tcontext, | |
827 | tclass); | |
1da177e4 LT |
828 | goto out; |
829 | } | |
830 | constraint = constraint->next; | |
831 | } | |
832 | ||
833 | out: | |
0804d113 | 834 | read_unlock(&policy_rwlock); |
1da177e4 LT |
835 | return rc; |
836 | } | |
837 | ||
f9df6458 AP |
838 | int 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 | ||
845 | int 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 | ||
d9250dea KK |
852 | /* |
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 | */ | |
861 | int 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; | |
4b02b524 | 866 | int rc; |
d9250dea KK |
867 | |
868 | read_lock(&policy_rwlock); | |
869 | ||
4b02b524 | 870 | rc = -EINVAL; |
d9250dea KK |
871 | 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 | ||
4b02b524 | 878 | rc = -EINVAL; |
d9250dea KK |
879 | 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 | ||
4b02b524 | 886 | rc = 0; |
af901ca1 | 887 | /* type/domain unchanged */ |
4b02b524 | 888 | if (old_context->type == new_context->type) |
d9250dea | 889 | goto out; |
d9250dea KK |
890 | |
891 | index = new_context->type; | |
892 | while (true) { | |
23bdecb0 EP |
893 | type = flex_array_get_ptr(policydb.type_val_to_struct_array, |
894 | index - 1); | |
d9250dea KK |
895 | BUG_ON(!type); |
896 | ||
897 | /* not bounded anymore */ | |
4b02b524 EP |
898 | rc = -EPERM; |
899 | if (!type->bounds) | |
d9250dea | 900 | break; |
d9250dea KK |
901 | |
902 | /* @newsid is bounded by @oldsid */ | |
4b02b524 EP |
903 | rc = 0; |
904 | if (type->bounds == old_context->type) | |
d9250dea | 905 | break; |
4b02b524 | 906 | |
d9250dea KK |
907 | index = type->bounds; |
908 | } | |
44c2d9bd KK |
909 | |
910 | if (rc) { | |
911 | char *old_name = NULL; | |
912 | char *new_name = NULL; | |
2da5d31b | 913 | u32 length; |
44c2d9bd KK |
914 | |
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 " | |
4093a844 | 922 | "seresult=denied " |
44c2d9bd KK |
923 | "oldcontext=%s newcontext=%s", |
924 | old_name, new_name); | |
925 | } | |
926 | kfree(new_name); | |
927 | kfree(old_name); | |
928 | } | |
d9250dea KK |
929 | out: |
930 | read_unlock(&policy_rwlock); | |
931 | ||
932 | return rc; | |
933 | } | |
934 | ||
19439d05 | 935 | static void avd_init(struct av_decision *avd) |
c6d3aaa4 | 936 | { |
19439d05 SS |
937 | avd->allowed = 0; |
938 | avd->auditallow = 0; | |
939 | avd->auditdeny = 0xffffffff; | |
940 | avd->seqno = latest_granting; | |
941 | avd->flags = 0; | |
c6d3aaa4 SS |
942 | } |
943 | ||
fa1aa143 JVS |
944 | void 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 | ||
998 | void 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 | } | |
1070 | out: | |
1071 | read_unlock(&policy_rwlock); | |
1072 | return; | |
1073 | allow: | |
1074 | memset(xpermd->allowed->p, 0xff, sizeof(xpermd->allowed->p)); | |
1075 | goto out; | |
1076 | } | |
19439d05 | 1077 | |
1da177e4 LT |
1078 | /** |
1079 | * security_compute_av - Compute access vector decisions. | |
1080 | * @ssid: source security identifier | |
1081 | * @tsid: target security identifier | |
1082 | * @tclass: target security class | |
1da177e4 | 1083 | * @avd: access vector decisions |
fa1aa143 | 1084 | * @xperms: extended permissions |
1da177e4 LT |
1085 | * |
1086 | * Compute a set of access vector decisions based on the | |
1087 | * SID pair (@ssid, @tsid) for the permissions in @tclass. | |
1da177e4 | 1088 | */ |
19439d05 SS |
1089 | void security_compute_av(u32 ssid, |
1090 | u32 tsid, | |
1091 | u16 orig_tclass, | |
fa1aa143 JVS |
1092 | struct av_decision *avd, |
1093 | struct extended_perms *xperms) | |
1da177e4 | 1094 | { |
c6d3aaa4 | 1095 | u16 tclass; |
19439d05 | 1096 | struct context *scontext = NULL, *tcontext = NULL; |
c6d3aaa4 | 1097 | |
b7f3008a | 1098 | read_lock(&policy_rwlock); |
19439d05 | 1099 | avd_init(avd); |
fa1aa143 | 1100 | xperms->len = 0; |
c6d3aaa4 SS |
1101 | if (!ss_initialized) |
1102 | goto allow; | |
1103 | ||
19439d05 SS |
1104 | 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 | ||
c6d3aaa4 SS |
1122 | tclass = unmap_class(orig_tclass); |
1123 | if (unlikely(orig_tclass && !tclass)) { | |
1124 | if (policydb.allow_unknown) | |
1125 | goto allow; | |
b7f3008a | 1126 | goto out; |
c6d3aaa4 | 1127 | } |
fa1aa143 | 1128 | context_struct_compute_av(scontext, tcontext, tclass, avd, xperms); |
c6d3aaa4 | 1129 | map_decision(orig_tclass, avd, policydb.allow_unknown); |
b7f3008a | 1130 | out: |
c6d3aaa4 | 1131 | read_unlock(&policy_rwlock); |
19439d05 | 1132 | return; |
c6d3aaa4 SS |
1133 | allow: |
1134 | avd->allowed = 0xffffffff; | |
b7f3008a | 1135 | goto out; |
c6d3aaa4 SS |
1136 | } |
1137 | ||
19439d05 SS |
1138 | void security_compute_av_user(u32 ssid, |
1139 | u32 tsid, | |
1140 | u16 tclass, | |
1141 | struct av_decision *avd) | |
c6d3aaa4 | 1142 | { |
19439d05 | 1143 | struct context *scontext = NULL, *tcontext = NULL; |
1da177e4 | 1144 | |
19439d05 SS |
1145 | read_lock(&policy_rwlock); |
1146 | 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; | |
1da177e4 LT |
1155 | } |
1156 | ||
19439d05 SS |
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 | ||
fa1aa143 | 1174 | context_struct_compute_av(scontext, tcontext, tclass, avd, NULL); |
19439d05 | 1175 | out: |
0804d113 | 1176 | read_unlock(&policy_rwlock); |
19439d05 SS |
1177 | return; |
1178 | allow: | |
1179 | avd->allowed = 0xffffffff; | |
1180 | goto out; | |
1da177e4 LT |
1181 | } |
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 | */ | |
1190 | static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len) | |
1191 | { | |
1192 | char *scontextp; | |
1193 | ||
d5630b9d EP |
1194 | if (scontext) |
1195 | *scontext = NULL; | |
1da177e4 LT |
1196 | *scontext_len = 0; |
1197 | ||
12b29f34 SS |
1198 | if (context->len) { |
1199 | *scontext_len = context->len; | |
bb7081ab EP |
1200 | if (scontext) { |
1201 | *scontext = kstrdup(context->str, GFP_ATOMIC); | |
1202 | if (!(*scontext)) | |
1203 | return -ENOMEM; | |
1204 | } | |
12b29f34 SS |
1205 | return 0; |
1206 | } | |
1207 | ||
1da177e4 | 1208 | /* Compute the size of the context. */ |
ac76c05b EP |
1209 | *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; | |
1da177e4 LT |
1212 | *scontext_len += mls_compute_context_len(context); |
1213 | ||
d5630b9d EP |
1214 | if (!scontext) |
1215 | return 0; | |
1216 | ||
1da177e4 LT |
1217 | /* Allocate space for the context; caller must free this space. */ |
1218 | scontextp = kmalloc(*scontext_len, GFP_ATOMIC); | |
5d55a345 | 1219 | if (!scontextp) |
1da177e4 | 1220 | return -ENOMEM; |
1da177e4 LT |
1221 | *scontext = scontextp; |
1222 | ||
1223 | /* | |
1224 | * Copy the user name, role name and type name into the context. | |
1225 | */ | |
9529c788 | 1226 | scontextp += sprintf(scontextp, "%s:%s:%s", |
ac76c05b EP |
1227 | 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)); | |
1da177e4 LT |
1230 | |
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 | ||
f0ee2e46 JC |
1240 | const 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 | ||
12b29f34 SS |
1247 | static int security_sid_to_context_core(u32 sid, char **scontext, |
1248 | u32 *scontext_len, int force) | |
1da177e4 LT |
1249 | { |
1250 | struct context *context; | |
1251 | int rc = 0; | |
1252 | ||
d5630b9d EP |
1253 | if (scontext) |
1254 | *scontext = NULL; | |
4f4acf3a SS |
1255 | *scontext_len = 0; |
1256 | ||
1da177e4 LT |
1257 | if (!ss_initialized) { |
1258 | if (sid <= SECINITSID_NUM) { | |
1259 | char *scontextp; | |
1260 | ||
1261 | *scontext_len = strlen(initial_sid_to_string[sid]) + 1; | |
d5630b9d EP |
1262 | if (!scontext) |
1263 | goto out; | |
aa736c36 RV |
1264 | scontextp = kmemdup(initial_sid_to_string[sid], |
1265 | *scontext_len, GFP_ATOMIC); | |
0cccca06 SH |
1266 | if (!scontextp) { |
1267 | rc = -ENOMEM; | |
1268 | goto out; | |
1269 | } | |
1da177e4 LT |
1270 | *scontext = scontextp; |
1271 | goto out; | |
1272 | } | |
744ba35e EP |
1273 | printk(KERN_ERR "SELinux: %s: called before initial " |
1274 | "load_policy on unknown SID %d\n", __func__, sid); | |
1da177e4 LT |
1275 | rc = -EINVAL; |
1276 | goto out; | |
1277 | } | |
0804d113 | 1278 | read_lock(&policy_rwlock); |
12b29f34 SS |
1279 | if (force) |
1280 | context = sidtab_search_force(&sidtab, sid); | |
1281 | else | |
1282 | context = sidtab_search(&sidtab, sid); | |
1da177e4 | 1283 | if (!context) { |
744ba35e EP |
1284 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
1285 | __func__, sid); | |
1da177e4 LT |
1286 | rc = -EINVAL; |
1287 | goto out_unlock; | |
1288 | } | |
1289 | rc = context_struct_to_string(context, scontext, scontext_len); | |
1290 | out_unlock: | |
0804d113 | 1291 | read_unlock(&policy_rwlock); |
1da177e4 LT |
1292 | out: |
1293 | return rc; | |
1294 | ||
1295 | } | |
1296 | ||
12b29f34 SS |
1297 | /** |
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 | */ | |
1307 | int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len) | |
1da177e4 | 1308 | { |
12b29f34 SS |
1309 | return security_sid_to_context_core(sid, scontext, scontext_len, 0); |
1310 | } | |
1311 | ||
1312 | int 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 | ||
9a59daa0 SS |
1317 | /* |
1318 | * Caveat: Mutates scontext. | |
1319 | */ | |
12b29f34 SS |
1320 | static int string_to_context_struct(struct policydb *pol, |
1321 | struct sidtab *sidtabp, | |
9a59daa0 | 1322 | char *scontext, |
12b29f34 SS |
1323 | u32 scontext_len, |
1324 | struct context *ctx, | |
9a59daa0 | 1325 | u32 def_sid) |
12b29f34 | 1326 | { |
1da177e4 LT |
1327 | struct role_datum *role; |
1328 | struct type_datum *typdatum; | |
1329 | struct user_datum *usrdatum; | |
1330 | char *scontextp, *p, oldc; | |
1331 | int rc = 0; | |
1332 | ||
12b29f34 | 1333 | context_init(ctx); |
1da177e4 | 1334 | |
1da177e4 LT |
1335 | /* Parse the security context. */ |
1336 | ||
1337 | rc = -EINVAL; | |
9a59daa0 | 1338 | scontextp = (char *) scontext; |
1da177e4 LT |
1339 | |
1340 | /* Extract the user. */ | |
1341 | p = scontextp; | |
1342 | while (*p && *p != ':') | |
1343 | p++; | |
1344 | ||
1345 | if (*p == 0) | |
12b29f34 | 1346 | goto out; |
1da177e4 LT |
1347 | |
1348 | *p++ = 0; | |
1349 | ||
12b29f34 | 1350 | usrdatum = hashtab_search(pol->p_users.table, scontextp); |
1da177e4 | 1351 | if (!usrdatum) |
12b29f34 | 1352 | goto out; |
1da177e4 | 1353 | |
12b29f34 | 1354 | ctx->user = usrdatum->value; |
1da177e4 LT |
1355 | |
1356 | /* Extract role. */ | |
1357 | scontextp = p; | |
1358 | while (*p && *p != ':') | |
1359 | p++; | |
1360 | ||
1361 | if (*p == 0) | |
12b29f34 | 1362 | goto out; |
1da177e4 LT |
1363 | |
1364 | *p++ = 0; | |
1365 | ||
12b29f34 | 1366 | role = hashtab_search(pol->p_roles.table, scontextp); |
1da177e4 | 1367 | if (!role) |
12b29f34 SS |
1368 | goto out; |
1369 | ctx->role = role->value; | |
1da177e4 LT |
1370 | |
1371 | /* Extract type. */ | |
1372 | scontextp = p; | |
1373 | while (*p && *p != ':') | |
1374 | p++; | |
1375 | oldc = *p; | |
1376 | *p++ = 0; | |
1377 | ||
12b29f34 | 1378 | typdatum = hashtab_search(pol->p_types.table, scontextp); |
d9250dea | 1379 | if (!typdatum || typdatum->attribute) |
12b29f34 | 1380 | goto out; |
1da177e4 | 1381 | |
12b29f34 | 1382 | ctx->type = typdatum->value; |
1da177e4 | 1383 | |
12b29f34 | 1384 | rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid); |
1da177e4 | 1385 | if (rc) |
12b29f34 | 1386 | goto out; |
1da177e4 | 1387 | |
4b02b524 EP |
1388 | rc = -EINVAL; |
1389 | if ((p - scontext) < scontext_len) | |
12b29f34 | 1390 | goto out; |
1da177e4 LT |
1391 | |
1392 | /* Check the validity of the new context. */ | |
4b02b524 | 1393 | if (!policydb_context_isvalid(pol, ctx)) |
12b29f34 | 1394 | goto out; |
12b29f34 SS |
1395 | rc = 0; |
1396 | out: | |
8e531af9 EP |
1397 | if (rc) |
1398 | context_destroy(ctx); | |
12b29f34 SS |
1399 | return rc; |
1400 | } | |
1401 | ||
1402 | static 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 | { | |
9a59daa0 | 1406 | char *scontext2, *str = NULL; |
12b29f34 SS |
1407 | struct context context; |
1408 | int rc = 0; | |
1409 | ||
2172fa70 SS |
1410 | /* An empty security context is never valid. */ |
1411 | if (!scontext_len) | |
1412 | return -EINVAL; | |
1413 | ||
12b29f34 SS |
1414 | 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; | |
9a59daa0 | 1420 | return 0; |
12b29f34 SS |
1421 | } |
1422 | } | |
1423 | *sid = SECINITSID_KERNEL; | |
9a59daa0 | 1424 | return 0; |
12b29f34 SS |
1425 | } |
1426 | *sid = SECSID_NULL; | |
1427 | ||
9a59daa0 | 1428 | /* Copy the string so that we can modify the copy as we parse it. */ |
c1a7368a | 1429 | scontext2 = kmalloc(scontext_len + 1, gfp_flags); |
9a59daa0 SS |
1430 | 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 */ | |
4b02b524 | 1437 | rc = -ENOMEM; |
9a59daa0 | 1438 | str = kstrdup(scontext2, gfp_flags); |
4b02b524 EP |
1439 | if (!str) |
1440 | goto out; | |
9a59daa0 SS |
1441 | } |
1442 | ||
0804d113 | 1443 | read_lock(&policy_rwlock); |
4b02b524 EP |
1444 | rc = string_to_context_struct(&policydb, &sidtab, scontext2, |
1445 | scontext_len, &context, def_sid); | |
12b29f34 | 1446 | if (rc == -EINVAL && force) { |
9a59daa0 | 1447 | context.str = str; |
12b29f34 | 1448 | context.len = scontext_len; |
9a59daa0 | 1449 | str = NULL; |
12b29f34 | 1450 | } else if (rc) |
4b02b524 | 1451 | goto out_unlock; |
12b29f34 | 1452 | rc = sidtab_context_to_sid(&sidtab, &context, sid); |
8e531af9 | 1453 | context_destroy(&context); |
4b02b524 | 1454 | out_unlock: |
0804d113 | 1455 | read_unlock(&policy_rwlock); |
4b02b524 | 1456 | out: |
9a59daa0 SS |
1457 | kfree(scontext2); |
1458 | kfree(str); | |
1da177e4 LT |
1459 | return rc; |
1460 | } | |
1461 | ||
f5c1d5b2 JM |
1462 | /** |
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 | |
52a4c640 | 1467 | * @gfp: context for the allocation |
f5c1d5b2 JM |
1468 | * |
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 | */ | |
52a4c640 NA |
1474 | int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid, |
1475 | gfp_t gfp) | |
f5c1d5b2 JM |
1476 | { |
1477 | return security_context_to_sid_core(scontext, scontext_len, | |
52a4c640 | 1478 | sid, SECSID_NULL, gfp, 0); |
44be2f65 RV |
1479 | } |
1480 | ||
1481 | int 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); | |
f5c1d5b2 JM |
1484 | } |
1485 | ||
1486 | /** | |
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 | |
d133a960 | 1493 | * @def_sid: default SID to assign on error |
f5c1d5b2 JM |
1494 | * |
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). | |
12b29f34 | 1500 | * Implicitly forces adding of the context even if it cannot be mapped yet. |
f5c1d5b2 JM |
1501 | * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient |
1502 | * memory is available, or 0 on success. | |
1503 | */ | |
7bf570dc DH |
1504 | int security_context_to_sid_default(const char *scontext, u32 scontext_len, |
1505 | u32 *sid, u32 def_sid, gfp_t gfp_flags) | |
f5c1d5b2 JM |
1506 | { |
1507 | return security_context_to_sid_core(scontext, scontext_len, | |
12b29f34 SS |
1508 | sid, def_sid, gfp_flags, 1); |
1509 | } | |
1510 | ||
1511 | int 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); | |
f5c1d5b2 JM |
1516 | } |
1517 | ||
1da177e4 LT |
1518 | static 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 | ||
4b02b524 | 1527 | if (context_struct_to_string(scontext, &s, &slen)) |
1da177e4 | 1528 | goto out; |
4b02b524 | 1529 | if (context_struct_to_string(tcontext, &t, &tlen)) |
1da177e4 | 1530 | goto out; |
4b02b524 | 1531 | if (context_struct_to_string(newcontext, &n, &nlen)) |
1da177e4 | 1532 | goto out; |
9ad9ad38 | 1533 | audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
4093a844 RGB |
1534 | "op=security_compute_sid invalid_context=%s" |
1535 | " scontext=%s" | |
1da177e4 LT |
1536 | " tcontext=%s" |
1537 | " tclass=%s", | |
ac76c05b | 1538 | n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1)); |
1da177e4 LT |
1539 | out: |
1540 | kfree(s); | |
1541 | kfree(t); | |
1542 | kfree(n); | |
1543 | if (!selinux_enforcing) | |
1544 | return 0; | |
1545 | return -EACCES; | |
1546 | } | |
1547 | ||
652bb9b0 | 1548 | static void filename_compute_type(struct policydb *p, struct context *newcontext, |
2667991f | 1549 | u32 stype, u32 ttype, u16 tclass, |
f50a3ec9 | 1550 | const char *objname) |
652bb9b0 | 1551 | { |
2463c26d EP |
1552 | struct filename_trans ft; |
1553 | struct filename_trans_datum *otype; | |
03a4c018 EP |
1554 | |
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 | ||
2463c26d EP |
1563 | 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; | |
652bb9b0 EP |
1571 | } |
1572 | ||
1da177e4 LT |
1573 | static int security_compute_sid(u32 ssid, |
1574 | u32 tsid, | |
c6d3aaa4 | 1575 | u16 orig_tclass, |
1da177e4 | 1576 | u32 specified, |
f50a3ec9 | 1577 | const char *objname, |
c6d3aaa4 SS |
1578 | u32 *out_sid, |
1579 | bool kern) | |
1da177e4 | 1580 | { |
aa893269 | 1581 | struct class_datum *cladatum = NULL; |
1da177e4 LT |
1582 | 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; | |
c6d3aaa4 | 1587 | u16 tclass; |
1da177e4 | 1588 | int rc = 0; |
6f5317e7 | 1589 | bool sock; |
1da177e4 LT |
1590 | |
1591 | if (!ss_initialized) { | |
c6d3aaa4 SS |
1592 | switch (orig_tclass) { |
1593 | case SECCLASS_PROCESS: /* kernel value */ | |
1da177e4 LT |
1594 | *out_sid = ssid; |
1595 | break; | |
1596 | default: | |
1597 | *out_sid = tsid; | |
1598 | break; | |
1599 | } | |
1600 | goto out; | |
1601 | } | |
1602 | ||
851f8a69 VY |
1603 | context_init(&newcontext); |
1604 | ||
0804d113 | 1605 | read_lock(&policy_rwlock); |
1da177e4 | 1606 | |
6f5317e7 | 1607 | if (kern) { |
c6d3aaa4 | 1608 | tclass = unmap_class(orig_tclass); |
6f5317e7 HC |
1609 | sock = security_is_socket_class(orig_tclass); |
1610 | } else { | |
c6d3aaa4 | 1611 | tclass = orig_tclass; |
6f5317e7 HC |
1612 | sock = security_is_socket_class(map_class(tclass)); |
1613 | } | |
c6d3aaa4 | 1614 | |
1da177e4 LT |
1615 | scontext = sidtab_search(&sidtab, ssid); |
1616 | if (!scontext) { | |
744ba35e EP |
1617 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
1618 | __func__, ssid); | |
1da177e4 LT |
1619 | rc = -EINVAL; |
1620 | goto out_unlock; | |
1621 | } | |
1622 | tcontext = sidtab_search(&sidtab, tsid); | |
1623 | if (!tcontext) { | |
744ba35e EP |
1624 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
1625 | __func__, tsid); | |
1da177e4 LT |
1626 | rc = -EINVAL; |
1627 | goto out_unlock; | |
1628 | } | |
1629 | ||
aa893269 EP |
1630 | if (tclass && tclass <= policydb.p_classes.nprim) |
1631 | cladatum = policydb.class_val_to_struct[tclass - 1]; | |
1632 | ||
1da177e4 LT |
1633 | /* Set the user identity. */ |
1634 | switch (specified) { | |
1635 | case AVTAB_TRANSITION: | |
1636 | case AVTAB_CHANGE: | |
aa893269 EP |
1637 | 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 | } | |
1da177e4 LT |
1644 | break; |
1645 | case AVTAB_MEMBER: | |
1646 | /* Use the related object owner. */ | |
1647 | newcontext.user = tcontext->user; | |
1648 | break; | |
1649 | } | |
1650 | ||
aa893269 EP |
1651 | /* Set the role to default values. */ |
1652 | if (cladatum && cladatum->default_role == DEFAULT_SOURCE) { | |
1da177e4 | 1653 | newcontext.role = scontext->role; |
aa893269 EP |
1654 | } 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. */ | |
eed7795d | 1664 | if (cladatum && cladatum->default_type == DEFAULT_SOURCE) { |
1da177e4 | 1665 | newcontext.type = scontext->type; |
eed7795d | 1666 | } else if (cladatum && cladatum->default_type == DEFAULT_TARGET) { |
1da177e4 | 1667 | newcontext.type = tcontext->type; |
eed7795d EP |
1668 | } 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 | } | |
1da177e4 LT |
1676 | } |
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; | |
782ebb99 SS |
1682 | avkey.specified = specified; |
1683 | avdatum = avtab_search(&policydb.te_avtab, &avkey); | |
1da177e4 LT |
1684 | |
1685 | /* If no permanent rule, also check for enabled conditional rules */ | |
5d55a345 | 1686 | if (!avdatum) { |
782ebb99 | 1687 | node = avtab_search_node(&policydb.te_cond_avtab, &avkey); |
dbc74c65 | 1688 | for (; node; node = avtab_search_node_next(node, specified)) { |
782ebb99 | 1689 | if (node->key.specified & AVTAB_ENABLED) { |
1da177e4 LT |
1690 | avdatum = &node->datum; |
1691 | break; | |
1692 | } | |
1693 | } | |
1694 | } | |
1695 | ||
782ebb99 | 1696 | if (avdatum) { |
1da177e4 | 1697 | /* Use the type from the type transition/member/change rule. */ |
fa1aa143 | 1698 | newcontext.type = avdatum->u.data; |
1da177e4 LT |
1699 | } |
1700 | ||
4742600c | 1701 | /* if we have a objname this is a file trans check so check those rules */ |
f50a3ec9 | 1702 | if (objname) |
652bb9b0 | 1703 | filename_compute_type(&policydb, &newcontext, scontext->type, |
f50a3ec9 | 1704 | tcontext->type, tclass, objname); |
652bb9b0 | 1705 | |
1da177e4 | 1706 | /* Check for class-specific changes. */ |
63a312ca HC |
1707 | 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; | |
1da177e4 LT |
1716 | } |
1717 | } | |
1da177e4 LT |
1718 | } |
1719 | ||
1720 | /* Set the MLS attributes. | |
1721 | This is done last because it may allocate memory. */ | |
6f5317e7 HC |
1722 | rc = mls_compute_sid(scontext, tcontext, tclass, specified, |
1723 | &newcontext, sock); | |
1da177e4 LT |
1724 | 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); | |
1738 | out_unlock: | |
0804d113 | 1739 | read_unlock(&policy_rwlock); |
1da177e4 LT |
1740 | context_destroy(&newcontext); |
1741 | out: | |
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 | */ | |
652bb9b0 EP |
1758 | int security_transition_sid(u32 ssid, u32 tsid, u16 tclass, |
1759 | const struct qstr *qstr, u32 *out_sid) | |
1da177e4 | 1760 | { |
c6d3aaa4 | 1761 | return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, |
f50a3ec9 | 1762 | qstr ? qstr->name : NULL, out_sid, true); |
c6d3aaa4 SS |
1763 | } |
1764 | ||
f50a3ec9 KK |
1765 | int security_transition_sid_user(u32 ssid, u32 tsid, u16 tclass, |
1766 | const char *objname, u32 *out_sid) | |
c6d3aaa4 SS |
1767 | { |
1768 | return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, | |
f50a3ec9 | 1769 | objname, out_sid, false); |
1da177e4 LT |
1770 | } |
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 | */ | |
1785 | int security_member_sid(u32 ssid, | |
1786 | u32 tsid, | |
1787 | u16 tclass, | |
1788 | u32 *out_sid) | |
1789 | { | |
652bb9b0 EP |
1790 | return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, NULL, |
1791 | out_sid, false); | |
1da177e4 LT |
1792 | } |
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 | */ | |
1807 | int security_change_sid(u32 ssid, | |
1808 | u32 tsid, | |
1809 | u16 tclass, | |
1810 | u32 *out_sid) | |
1811 | { | |
652bb9b0 EP |
1812 | return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, NULL, |
1813 | out_sid, false); | |
b94c7e67 CS |
1814 | } |
1815 | ||
1da177e4 LT |
1816 | /* Clone the SID into the new SID table. */ |
1817 | static int clone_sid(u32 sid, | |
1818 | struct context *context, | |
1819 | void *arg) | |
1820 | { | |
1821 | struct sidtab *s = arg; | |
1822 | ||
42596eaf GT |
1823 | if (sid > SECINITSID_NUM) |
1824 | return sidtab_insert(s, sid, context); | |
1825 | else | |
1826 | return 0; | |
1da177e4 LT |
1827 | } |
1828 | ||
1829 | static inline int convert_context_handle_invalid_context(struct context *context) | |
1830 | { | |
4b02b524 EP |
1831 | char *s; |
1832 | u32 len; | |
1da177e4 | 1833 | |
4b02b524 EP |
1834 | if (selinux_enforcing) |
1835 | return -EINVAL; | |
1836 | ||
1837 | 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); | |
1da177e4 | 1840 | } |
4b02b524 | 1841 | return 0; |
1da177e4 LT |
1842 | } |
1843 | ||
1844 | struct 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 | */ | |
1856 | static int convert_context(u32 key, | |
1857 | struct context *c, | |
1858 | void *p) | |
1859 | { | |
1860 | struct convert_context_args *args; | |
1861 | struct context oldc; | |
0719aaf5 GT |
1862 | struct ocontext *oc; |
1863 | struct mls_range *range; | |
1da177e4 LT |
1864 | struct role_datum *role; |
1865 | struct type_datum *typdatum; | |
1866 | struct user_datum *usrdatum; | |
1867 | char *s; | |
1868 | u32 len; | |
42596eaf GT |
1869 | int rc = 0; |
1870 | ||
1871 | if (key <= SECINITSID_NUM) | |
1872 | goto out; | |
1da177e4 LT |
1873 | |
1874 | args = p; | |
1875 | ||
12b29f34 SS |
1876 | if (c->str) { |
1877 | struct context ctx; | |
4b02b524 EP |
1878 | |
1879 | rc = -ENOMEM; | |
9a59daa0 | 1880 | s = kstrdup(c->str, GFP_KERNEL); |
4b02b524 | 1881 | if (!s) |
9a59daa0 | 1882 | goto out; |
4b02b524 | 1883 | |
9a59daa0 SS |
1884 | rc = string_to_context_struct(args->newp, NULL, s, |
1885 | c->len, &ctx, SECSID_NULL); | |
1886 | kfree(s); | |
12b29f34 | 1887 | if (!rc) { |
4b02b524 | 1888 | printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n", |
12b29f34 SS |
1889 | 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. */ | |
4b02b524 | 1900 | printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n", |
12b29f34 SS |
1901 | c->str, -rc); |
1902 | goto out; | |
1903 | } | |
1904 | } | |
1905 | ||
1da177e4 LT |
1906 | rc = context_cpy(&oldc, c); |
1907 | if (rc) | |
1908 | goto out; | |
1909 | ||
1da177e4 | 1910 | /* Convert the user. */ |
4b02b524 | 1911 | rc = -EINVAL; |
1da177e4 | 1912 | usrdatum = hashtab_search(args->newp->p_users.table, |
ac76c05b | 1913 | sym_name(args->oldp, SYM_USERS, c->user - 1)); |
5d55a345 | 1914 | if (!usrdatum) |
1da177e4 | 1915 | goto bad; |
1da177e4 LT |
1916 | c->user = usrdatum->value; |
1917 | ||
1918 | /* Convert the role. */ | |
4b02b524 | 1919 | rc = -EINVAL; |
1da177e4 | 1920 | role = hashtab_search(args->newp->p_roles.table, |
ac76c05b | 1921 | sym_name(args->oldp, SYM_ROLES, c->role - 1)); |
5d55a345 | 1922 | if (!role) |
1da177e4 | 1923 | goto bad; |
1da177e4 LT |
1924 | c->role = role->value; |
1925 | ||
1926 | /* Convert the type. */ | |
4b02b524 | 1927 | rc = -EINVAL; |
1da177e4 | 1928 | typdatum = hashtab_search(args->newp->p_types.table, |
ac76c05b | 1929 | sym_name(args->oldp, SYM_TYPES, c->type - 1)); |
5d55a345 | 1930 | if (!typdatum) |
1da177e4 | 1931 | goto bad; |
1da177e4 LT |
1932 | c->type = typdatum->value; |
1933 | ||
0719aaf5 GT |
1934 | /* 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; | |
4b02b524 | 1957 | rc = -EINVAL; |
0719aaf5 GT |
1958 | 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 | } | |
1da177e4 LT |
1968 | |
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); | |
4b02b524 | 1977 | |
12b29f34 | 1978 | rc = 0; |
1da177e4 LT |
1979 | out: |
1980 | return rc; | |
1981 | bad: | |
12b29f34 | 1982 | /* Map old representation to string and save it. */ |
4b02b524 EP |
1983 | rc = context_struct_to_string(&oldc, &s, &len); |
1984 | if (rc) | |
1985 | return rc; | |
1da177e4 | 1986 | context_destroy(&oldc); |
12b29f34 SS |
1987 | context_destroy(c); |
1988 | c->str = s; | |
1989 | c->len = len; | |
4b02b524 | 1990 | printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n", |
12b29f34 SS |
1991 | c->str); |
1992 | rc = 0; | |
1da177e4 LT |
1993 | goto out; |
1994 | } | |
1995 | ||
3bb56b25 PM |
1996 | static void security_load_policycaps(void) |
1997 | { | |
4dc2fce3 SS |
1998 | unsigned int i; |
1999 | struct ebitmap_node *node; | |
2000 | ||
3bb56b25 PM |
2001 | selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps, |
2002 | POLICYDB_CAPABILITY_NETPEER); | |
b0c636b9 EP |
2003 | selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps, |
2004 | POLICYDB_CAPABILITY_OPENPERM); | |
da69a530 SS |
2005 | selinux_policycap_extsockclass = ebitmap_get_bit(&policydb.policycaps, |
2006 | POLICYDB_CAPABILITY_EXTSOCKCLASS); | |
2be4d74f CP |
2007 | selinux_policycap_alwaysnetwork = ebitmap_get_bit(&policydb.policycaps, |
2008 | POLICYDB_CAPABILITY_ALWAYSNETWORK); | |
2651225b SS |
2009 | selinux_policycap_cgroupseclabel = |
2010 | ebitmap_get_bit(&policydb.policycaps, | |
2011 | POLICYDB_CAPABILITY_CGROUPSECLABEL); | |
4dc2fce3 SS |
2012 | |
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 | } | |
3bb56b25 PM |
2023 | } |
2024 | ||
e900a7d9 | 2025 | static int security_preserve_bools(struct policydb *p); |
1da177e4 LT |
2026 | |
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 | */ | |
2037 | int security_load_policy(void *data, size_t len) | |
2038 | { | |
b5495b42 | 2039 | struct policydb *oldpolicydb, *newpolicydb; |
1da177e4 | 2040 | struct sidtab oldsidtab, newsidtab; |
c6d3aaa4 | 2041 | struct selinux_mapping *oldmap, *map = NULL; |
1da177e4 LT |
2042 | struct convert_context_args args; |
2043 | u32 seqno; | |
c6d3aaa4 | 2044 | u16 map_size; |
1da177e4 LT |
2045 | int rc = 0; |
2046 | struct policy_file file = { data, len }, *fp = &file; | |
2047 | ||
b5495b42 TG |
2048 | oldpolicydb = kzalloc(2 * sizeof(*oldpolicydb), GFP_KERNEL); |
2049 | if (!oldpolicydb) { | |
2050 | rc = -ENOMEM; | |
2051 | goto out; | |
2052 | } | |
2053 | newpolicydb = oldpolicydb + 1; | |
2054 | ||
1da177e4 LT |
2055 | if (!ss_initialized) { |
2056 | avtab_cache_init(); | |
a2000050 EP |
2057 | rc = policydb_read(&policydb, fp); |
2058 | if (rc) { | |
1da177e4 | 2059 | avtab_cache_destroy(); |
b5495b42 | 2060 | goto out; |
1da177e4 | 2061 | } |
a2000050 | 2062 | |
cee74f47 | 2063 | policydb.len = len; |
a2000050 EP |
2064 | rc = selinux_set_mapping(&policydb, secclass_map, |
2065 | ¤t_mapping, | |
2066 | ¤t_mapping_size); | |
2067 | if (rc) { | |
1da177e4 LT |
2068 | policydb_destroy(&policydb); |
2069 | avtab_cache_destroy(); | |
b5495b42 | 2070 | goto out; |
1da177e4 | 2071 | } |
a2000050 EP |
2072 | |
2073 | rc = policydb_load_isids(&policydb, &sidtab); | |
2074 | if (rc) { | |
b94c7e67 CS |
2075 | policydb_destroy(&policydb); |
2076 | avtab_cache_destroy(); | |
b5495b42 | 2077 | goto out; |
b94c7e67 | 2078 | } |
a2000050 | 2079 | |
3bb56b25 | 2080 | security_load_policycaps(); |
1da177e4 | 2081 | ss_initialized = 1; |
4c443d1b | 2082 | seqno = ++latest_granting; |
1da177e4 | 2083 | selinux_complete_init(); |
4c443d1b SS |
2084 | avc_ss_reset(seqno); |
2085 | selnl_notify_policyload(seqno); | |
11904167 | 2086 | selinux_status_update_policyload(seqno); |
7420ed23 | 2087 | selinux_netlbl_cache_invalidate(); |
342a0cff | 2088 | selinux_xfrm_notify_policyload(); |
b5495b42 | 2089 | goto out; |
1da177e4 LT |
2090 | } |
2091 | ||
2092 | #if 0 | |
2093 | sidtab_hash_eval(&sidtab, "sids"); | |
2094 | #endif | |
2095 | ||
b5495b42 | 2096 | rc = policydb_read(newpolicydb, fp); |
a2000050 | 2097 | if (rc) |
b5495b42 | 2098 | goto out; |
1da177e4 | 2099 | |
b5495b42 | 2100 | newpolicydb->len = len; |
0719aaf5 | 2101 | /* If switching between different policy types, log MLS status */ |
b5495b42 | 2102 | if (policydb.mls_enabled && !newpolicydb->mls_enabled) |
0719aaf5 | 2103 | printk(KERN_INFO "SELinux: Disabling MLS support...\n"); |
b5495b42 | 2104 | else if (!policydb.mls_enabled && newpolicydb->mls_enabled) |
0719aaf5 GT |
2105 | printk(KERN_INFO "SELinux: Enabling MLS support...\n"); |
2106 | ||
b5495b42 | 2107 | rc = policydb_load_isids(newpolicydb, &newsidtab); |
42596eaf GT |
2108 | if (rc) { |
2109 | printk(KERN_ERR "SELinux: unable to load the initial SIDs\n"); | |
b5495b42 TG |
2110 | policydb_destroy(newpolicydb); |
2111 | goto out; | |
12b29f34 | 2112 | } |
1da177e4 | 2113 | |
b5495b42 | 2114 | rc = selinux_set_mapping(newpolicydb, secclass_map, &map, &map_size); |
a2000050 | 2115 | if (rc) |
b94c7e67 | 2116 | goto err; |
b94c7e67 | 2117 | |
b5495b42 | 2118 | rc = security_preserve_bools(newpolicydb); |
e900a7d9 | 2119 | if (rc) { |
454d972c | 2120 | printk(KERN_ERR "SELinux: unable to preserve booleans\n"); |
e900a7d9 SS |
2121 | goto err; |
2122 | } | |
2123 | ||
1da177e4 LT |
2124 | /* Clone the SID table. */ |
2125 | sidtab_shutdown(&sidtab); | |
a2000050 EP |
2126 | |
2127 | rc = sidtab_map(&sidtab, clone_sid, &newsidtab); | |
2128 | if (rc) | |
1da177e4 | 2129 | goto err; |
1da177e4 | 2130 | |
12b29f34 SS |
2131 | /* |
2132 | * Convert the internal representations of contexts | |
2133 | * in the new SID table. | |
2134 | */ | |
1da177e4 | 2135 | args.oldp = &policydb; |
b5495b42 | 2136 | args.newp = newpolicydb; |
12b29f34 | 2137 | rc = sidtab_map(&newsidtab, convert_context, &args); |
0719aaf5 GT |
2138 | if (rc) { |
2139 | printk(KERN_ERR "SELinux: unable to convert the internal" | |
2140 | " representation of contexts in the new SID" | |
2141 | " table\n"); | |
12b29f34 | 2142 | goto err; |
0719aaf5 | 2143 | } |
1da177e4 LT |
2144 | |
2145 | /* Save the old policydb and SID table to free later. */ | |
b5495b42 | 2146 | memcpy(oldpolicydb, &policydb, sizeof(policydb)); |
1da177e4 LT |
2147 | sidtab_set(&oldsidtab, &sidtab); |
2148 | ||
2149 | /* Install the new policydb and SID table. */ | |
0804d113 | 2150 | write_lock_irq(&policy_rwlock); |
b5495b42 | 2151 | memcpy(&policydb, newpolicydb, sizeof(policydb)); |
1da177e4 | 2152 | sidtab_set(&sidtab, &newsidtab); |
3bb56b25 | 2153 | security_load_policycaps(); |
c6d3aaa4 SS |
2154 | oldmap = current_mapping; |
2155 | current_mapping = map; | |
2156 | current_mapping_size = map_size; | |
1da177e4 | 2157 | seqno = ++latest_granting; |
0804d113 | 2158 | write_unlock_irq(&policy_rwlock); |
1da177e4 LT |
2159 | |
2160 | /* Free the old policydb and SID table. */ | |
b5495b42 | 2161 | policydb_destroy(oldpolicydb); |
1da177e4 | 2162 | sidtab_destroy(&oldsidtab); |
c6d3aaa4 | 2163 | kfree(oldmap); |
1da177e4 LT |
2164 | |
2165 | avc_ss_reset(seqno); | |
2166 | selnl_notify_policyload(seqno); | |
11904167 | 2167 | selinux_status_update_policyload(seqno); |
7420ed23 | 2168 | selinux_netlbl_cache_invalidate(); |
342a0cff | 2169 | selinux_xfrm_notify_policyload(); |
1da177e4 | 2170 | |
b5495b42 TG |
2171 | rc = 0; |
2172 | goto out; | |
1da177e4 LT |
2173 | |
2174 | err: | |
c6d3aaa4 | 2175 | kfree(map); |
1da177e4 | 2176 | sidtab_destroy(&newsidtab); |
b5495b42 | 2177 | policydb_destroy(newpolicydb); |
1da177e4 | 2178 | |
b5495b42 TG |
2179 | out: |
2180 | kfree(oldpolicydb); | |
2181 | return rc; | |
1da177e4 LT |
2182 | } |
2183 | ||
cee74f47 EP |
2184 | size_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 | ||
1da177e4 LT |
2195 | /** |
2196 | * security_port_sid - Obtain the SID for a port. | |
1da177e4 LT |
2197 | * @protocol: protocol number |
2198 | * @port: port number | |
2199 | * @out_sid: security identifier | |
2200 | */ | |
3e112172 | 2201 | int security_port_sid(u8 protocol, u16 port, u32 *out_sid) |
1da177e4 LT |
2202 | { |
2203 | struct ocontext *c; | |
2204 | int rc = 0; | |
2205 | ||
0804d113 | 2206 | read_lock(&policy_rwlock); |
1da177e4 LT |
2207 | |
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 | ||
2230 | out: | |
0804d113 | 2231 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2232 | return rc; |
2233 | } | |
2234 | ||
cfc4d882 DJ |
2235 | /** |
2236 | * security_pkey_sid - Obtain the SID for a pkey. | |
2237 | * @subnet_prefix: Subnet Prefix | |
2238 | * @pkey_num: pkey number | |
2239 | * @out_sid: security identifier | |
2240 | */ | |
2241 | int security_ib_pkey_sid(u64 subnet_prefix, u16 pkey_num, u32 *out_sid) | |
2242 | { | |
2243 | struct ocontext *c; | |
2244 | int rc = 0; | |
2245 | ||
2246 | read_lock(&policy_rwlock); | |
2247 | ||
2248 | c = policydb.ocontexts[OCON_IBPKEY]; | |
2249 | while (c) { | |
2250 | if (c->u.ibpkey.low_pkey <= pkey_num && | |
2251 | c->u.ibpkey.high_pkey >= pkey_num && | |
2252 | c->u.ibpkey.subnet_prefix == subnet_prefix) | |
2253 | break; | |
2254 | ||
2255 | c = c->next; | |
2256 | } | |
2257 | ||
2258 | if (c) { | |
2259 | if (!c->sid[0]) { | |
2260 | rc = sidtab_context_to_sid(&sidtab, | |
2261 | &c->context[0], | |
2262 | &c->sid[0]); | |
2263 | if (rc) | |
2264 | goto out; | |
2265 | } | |
2266 | *out_sid = c->sid[0]; | |
2267 | } else | |
2268 | *out_sid = SECINITSID_UNLABELED; | |
2269 | ||
2270 | out: | |
2271 | read_unlock(&policy_rwlock); | |
2272 | return rc; | |
2273 | } | |
2274 | ||
1da177e4 LT |
2275 | /** |
2276 | * security_netif_sid - Obtain the SID for a network interface. | |
2277 | * @name: interface name | |
2278 | * @if_sid: interface SID | |
1da177e4 | 2279 | */ |
e8bfdb9d | 2280 | int security_netif_sid(char *name, u32 *if_sid) |
1da177e4 LT |
2281 | { |
2282 | int rc = 0; | |
2283 | struct ocontext *c; | |
2284 | ||
0804d113 | 2285 | read_lock(&policy_rwlock); |
1da177e4 LT |
2286 | |
2287 | c = policydb.ocontexts[OCON_NETIF]; | |
2288 | while (c) { | |
2289 | if (strcmp(name, c->u.name) == 0) | |
2290 | break; | |
2291 | c = c->next; | |
2292 | } | |
2293 | ||
2294 | if (c) { | |
2295 | if (!c->sid[0] || !c->sid[1]) { | |
2296 | rc = sidtab_context_to_sid(&sidtab, | |
2297 | &c->context[0], | |
2298 | &c->sid[0]); | |
2299 | if (rc) | |
2300 | goto out; | |
2301 | rc = sidtab_context_to_sid(&sidtab, | |
2302 | &c->context[1], | |
2303 | &c->sid[1]); | |
2304 | if (rc) | |
2305 | goto out; | |
2306 | } | |
2307 | *if_sid = c->sid[0]; | |
e8bfdb9d | 2308 | } else |
1da177e4 | 2309 | *if_sid = SECINITSID_NETIF; |
1da177e4 LT |
2310 | |
2311 | out: | |
0804d113 | 2312 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2313 | return rc; |
2314 | } | |
2315 | ||
2316 | static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask) | |
2317 | { | |
2318 | int i, fail = 0; | |
2319 | ||
5d55a345 EP |
2320 | for (i = 0; i < 4; i++) |
2321 | if (addr[i] != (input[i] & mask[i])) { | |
1da177e4 LT |
2322 | fail = 1; |
2323 | break; | |
2324 | } | |
2325 | ||
2326 | return !fail; | |
2327 | } | |
2328 | ||
2329 | /** | |
2330 | * security_node_sid - Obtain the SID for a node (host). | |
2331 | * @domain: communication domain aka address family | |
2332 | * @addrp: address | |
2333 | * @addrlen: address length in bytes | |
2334 | * @out_sid: security identifier | |
2335 | */ | |
2336 | int security_node_sid(u16 domain, | |
2337 | void *addrp, | |
2338 | u32 addrlen, | |
2339 | u32 *out_sid) | |
2340 | { | |
4b02b524 | 2341 | int rc; |
1da177e4 LT |
2342 | struct ocontext *c; |
2343 | ||
0804d113 | 2344 | read_lock(&policy_rwlock); |
1da177e4 LT |
2345 | |
2346 | switch (domain) { | |
2347 | case AF_INET: { | |
2348 | u32 addr; | |
2349 | ||
4b02b524 EP |
2350 | rc = -EINVAL; |
2351 | if (addrlen != sizeof(u32)) | |
1da177e4 | 2352 | goto out; |
1da177e4 LT |
2353 | |
2354 | addr = *((u32 *)addrp); | |
2355 | ||
2356 | c = policydb.ocontexts[OCON_NODE]; | |
2357 | while (c) { | |
2358 | if (c->u.node.addr == (addr & c->u.node.mask)) | |
2359 | break; | |
2360 | c = c->next; | |
2361 | } | |
2362 | break; | |
2363 | } | |
2364 | ||
2365 | case AF_INET6: | |
4b02b524 EP |
2366 | rc = -EINVAL; |
2367 | if (addrlen != sizeof(u64) * 2) | |
1da177e4 | 2368 | goto out; |
1da177e4 LT |
2369 | c = policydb.ocontexts[OCON_NODE6]; |
2370 | while (c) { | |
2371 | if (match_ipv6_addrmask(addrp, c->u.node6.addr, | |
2372 | c->u.node6.mask)) | |
2373 | break; | |
2374 | c = c->next; | |
2375 | } | |
2376 | break; | |
2377 | ||
2378 | default: | |
4b02b524 | 2379 | rc = 0; |
1da177e4 LT |
2380 | *out_sid = SECINITSID_NODE; |
2381 | goto out; | |
2382 | } | |
2383 | ||
2384 | if (c) { | |
2385 | if (!c->sid[0]) { | |
2386 | rc = sidtab_context_to_sid(&sidtab, | |
2387 | &c->context[0], | |
2388 | &c->sid[0]); | |
2389 | if (rc) | |
2390 | goto out; | |
2391 | } | |
2392 | *out_sid = c->sid[0]; | |
2393 | } else { | |
2394 | *out_sid = SECINITSID_NODE; | |
2395 | } | |
2396 | ||
4b02b524 | 2397 | rc = 0; |
1da177e4 | 2398 | out: |
0804d113 | 2399 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2400 | return rc; |
2401 | } | |
2402 | ||
2403 | #define SIDS_NEL 25 | |
2404 | ||
2405 | /** | |
2406 | * security_get_user_sids - Obtain reachable SIDs for a user. | |
2407 | * @fromsid: starting SID | |
2408 | * @username: username | |
2409 | * @sids: array of reachable SIDs for user | |
2410 | * @nel: number of elements in @sids | |
2411 | * | |
2412 | * Generate the set of SIDs for legal security contexts | |
2413 | * for a given user that can be reached by @fromsid. | |
2414 | * Set *@sids to point to a dynamically allocated | |
2415 | * array containing the set of SIDs. Set *@nel to the | |
2416 | * number of elements in the array. | |
2417 | */ | |
2418 | ||
2419 | int security_get_user_sids(u32 fromsid, | |
5d55a345 | 2420 | char *username, |
1da177e4 LT |
2421 | u32 **sids, |
2422 | u32 *nel) | |
2423 | { | |
2424 | struct context *fromcon, usercon; | |
2c3c05db | 2425 | u32 *mysids = NULL, *mysids2, sid; |
1da177e4 LT |
2426 | u32 mynel = 0, maxnel = SIDS_NEL; |
2427 | struct user_datum *user; | |
2428 | struct role_datum *role; | |
782ebb99 | 2429 | struct ebitmap_node *rnode, *tnode; |
1da177e4 LT |
2430 | int rc = 0, i, j; |
2431 | ||
2c3c05db SS |
2432 | *sids = NULL; |
2433 | *nel = 0; | |
2434 | ||
2435 | if (!ss_initialized) | |
1da177e4 | 2436 | goto out; |
1da177e4 | 2437 | |
0804d113 | 2438 | read_lock(&policy_rwlock); |
1da177e4 | 2439 | |
12b29f34 SS |
2440 | context_init(&usercon); |
2441 | ||
4b02b524 | 2442 | rc = -EINVAL; |
1da177e4 | 2443 | fromcon = sidtab_search(&sidtab, fromsid); |
4b02b524 | 2444 | if (!fromcon) |
1da177e4 | 2445 | goto out_unlock; |
1da177e4 | 2446 | |
4b02b524 | 2447 | rc = -EINVAL; |
1da177e4 | 2448 | user = hashtab_search(policydb.p_users.table, username); |
4b02b524 | 2449 | if (!user) |
1da177e4 | 2450 | goto out_unlock; |
4b02b524 | 2451 | |
1da177e4 LT |
2452 | usercon.user = user->value; |
2453 | ||
4b02b524 | 2454 | rc = -ENOMEM; |
89d155ef | 2455 | mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC); |
4b02b524 | 2456 | if (!mysids) |
1da177e4 | 2457 | goto out_unlock; |
1da177e4 | 2458 | |
9fe79ad1 | 2459 | ebitmap_for_each_positive_bit(&user->roles, rnode, i) { |
1da177e4 | 2460 | role = policydb.role_val_to_struct[i]; |
c1a7368a | 2461 | usercon.role = i + 1; |
9fe79ad1 | 2462 | ebitmap_for_each_positive_bit(&role->types, tnode, j) { |
c1a7368a | 2463 | usercon.type = j + 1; |
1da177e4 LT |
2464 | |
2465 | if (mls_setup_user_range(fromcon, user, &usercon)) | |
2466 | continue; | |
2467 | ||
1da177e4 | 2468 | rc = sidtab_context_to_sid(&sidtab, &usercon, &sid); |
2c3c05db | 2469 | if (rc) |
1da177e4 | 2470 | goto out_unlock; |
1da177e4 LT |
2471 | if (mynel < maxnel) { |
2472 | mysids[mynel++] = sid; | |
2473 | } else { | |
4b02b524 | 2474 | rc = -ENOMEM; |
1da177e4 | 2475 | maxnel += SIDS_NEL; |
89d155ef | 2476 | mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC); |
4b02b524 | 2477 | if (!mysids2) |
1da177e4 | 2478 | goto out_unlock; |
1da177e4 LT |
2479 | memcpy(mysids2, mysids, mynel * sizeof(*mysids2)); |
2480 | kfree(mysids); | |
2481 | mysids = mysids2; | |
2482 | mysids[mynel++] = sid; | |
2483 | } | |
2484 | } | |
2485 | } | |
4b02b524 | 2486 | rc = 0; |
1da177e4 | 2487 | out_unlock: |
0804d113 | 2488 | read_unlock(&policy_rwlock); |
2c3c05db SS |
2489 | if (rc || !mynel) { |
2490 | kfree(mysids); | |
2491 | goto out; | |
2492 | } | |
2493 | ||
4b02b524 | 2494 | rc = -ENOMEM; |
2c3c05db SS |
2495 | mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL); |
2496 | if (!mysids2) { | |
2c3c05db SS |
2497 | kfree(mysids); |
2498 | goto out; | |
2499 | } | |
2500 | for (i = 0, j = 0; i < mynel; i++) { | |
f01e1af4 | 2501 | struct av_decision dummy_avd; |
2c3c05db | 2502 | rc = avc_has_perm_noaudit(fromsid, mysids[i], |
c6d3aaa4 | 2503 | SECCLASS_PROCESS, /* kernel value */ |
2c3c05db | 2504 | PROCESS__TRANSITION, AVC_STRICT, |
f01e1af4 | 2505 | &dummy_avd); |
2c3c05db SS |
2506 | if (!rc) |
2507 | mysids2[j++] = mysids[i]; | |
2508 | cond_resched(); | |
2509 | } | |
2510 | rc = 0; | |
2511 | kfree(mysids); | |
2512 | *sids = mysids2; | |
2513 | *nel = j; | |
1da177e4 LT |
2514 | out: |
2515 | return rc; | |
2516 | } | |
2517 | ||
2518 | /** | |
f31e7994 | 2519 | * __security_genfs_sid - Helper to obtain a SID for a file in a filesystem |
1da177e4 LT |
2520 | * @fstype: filesystem type |
2521 | * @path: path from root of mount | |
2522 | * @sclass: file security class | |
2523 | * @sid: SID for path | |
2524 | * | |
2525 | * Obtain a SID to use for a file in a filesystem that | |
2526 | * cannot support xattr or use a fixed labeling behavior like | |
2527 | * transition SIDs or task SIDs. | |
f31e7994 WL |
2528 | * |
2529 | * The caller must acquire the policy_rwlock before calling this function. | |
1da177e4 | 2530 | */ |
f31e7994 WL |
2531 | static inline int __security_genfs_sid(const char *fstype, |
2532 | char *path, | |
2533 | u16 orig_sclass, | |
2534 | u32 *sid) | |
1da177e4 LT |
2535 | { |
2536 | int len; | |
c6d3aaa4 | 2537 | u16 sclass; |
1da177e4 LT |
2538 | struct genfs *genfs; |
2539 | struct ocontext *c; | |
4b02b524 | 2540 | int rc, cmp = 0; |
1da177e4 | 2541 | |
b1aa5301 SS |
2542 | while (path[0] == '/' && path[1] == '/') |
2543 | path++; | |
2544 | ||
c6d3aaa4 | 2545 | sclass = unmap_class(orig_sclass); |
4b02b524 | 2546 | *sid = SECINITSID_UNLABELED; |
c6d3aaa4 | 2547 | |
1da177e4 LT |
2548 | for (genfs = policydb.genfs; genfs; genfs = genfs->next) { |
2549 | cmp = strcmp(fstype, genfs->fstype); | |
2550 | if (cmp <= 0) | |
2551 | break; | |
2552 | } | |
2553 | ||
4b02b524 EP |
2554 | rc = -ENOENT; |
2555 | if (!genfs || cmp) | |
1da177e4 | 2556 | goto out; |
1da177e4 LT |
2557 | |
2558 | for (c = genfs->head; c; c = c->next) { | |
2559 | len = strlen(c->u.name); | |
2560 | if ((!c->v.sclass || sclass == c->v.sclass) && | |
2561 | (strncmp(c->u.name, path, len) == 0)) | |
2562 | break; | |
2563 | } | |
2564 | ||
4b02b524 EP |
2565 | rc = -ENOENT; |
2566 | if (!c) | |
1da177e4 | 2567 | goto out; |
1da177e4 LT |
2568 | |
2569 | if (!c->sid[0]) { | |
4b02b524 | 2570 | rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]); |
1da177e4 LT |
2571 | if (rc) |
2572 | goto out; | |
2573 | } | |
2574 | ||
2575 | *sid = c->sid[0]; | |
4b02b524 | 2576 | rc = 0; |
1da177e4 | 2577 | out: |
1da177e4 LT |
2578 | return rc; |
2579 | } | |
2580 | ||
f31e7994 WL |
2581 | /** |
2582 | * security_genfs_sid - Obtain a SID for a file in a filesystem | |
2583 | * @fstype: filesystem type | |
2584 | * @path: path from root of mount | |
2585 | * @sclass: file security class | |
2586 | * @sid: SID for path | |
2587 | * | |
2588 | * Acquire policy_rwlock before calling __security_genfs_sid() and release | |
2589 | * it afterward. | |
2590 | */ | |
2591 | int security_genfs_sid(const char *fstype, | |
2592 | char *path, | |
2593 | u16 orig_sclass, | |
2594 | u32 *sid) | |
2595 | { | |
2596 | int retval; | |
2597 | ||
2598 | read_lock(&policy_rwlock); | |
2599 | retval = __security_genfs_sid(fstype, path, orig_sclass, sid); | |
2600 | read_unlock(&policy_rwlock); | |
2601 | return retval; | |
2602 | } | |
2603 | ||
1da177e4 LT |
2604 | /** |
2605 | * security_fs_use - Determine how to handle labeling for a filesystem. | |
a64c54cf | 2606 | * @sb: superblock in question |
1da177e4 | 2607 | */ |
a64c54cf | 2608 | int security_fs_use(struct super_block *sb) |
1da177e4 LT |
2609 | { |
2610 | int rc = 0; | |
2611 | struct ocontext *c; | |
a64c54cf EP |
2612 | struct superblock_security_struct *sbsec = sb->s_security; |
2613 | const char *fstype = sb->s_type->name; | |
1da177e4 | 2614 | |
0804d113 | 2615 | read_lock(&policy_rwlock); |
1da177e4 | 2616 | |
4d546f81 PM |
2617 | c = policydb.ocontexts[OCON_FSUSE]; |
2618 | while (c) { | |
2619 | if (strcmp(fstype, c->u.name) == 0) | |
1da177e4 | 2620 | break; |
4d546f81 | 2621 | c = c->next; |
1da177e4 LT |
2622 | } |
2623 | ||
2624 | if (c) { | |
a64c54cf | 2625 | sbsec->behavior = c->v.behavior; |
1da177e4 | 2626 | if (!c->sid[0]) { |
4b02b524 | 2627 | rc = sidtab_context_to_sid(&sidtab, &c->context[0], |
1da177e4 LT |
2628 | &c->sid[0]); |
2629 | if (rc) | |
2630 | goto out; | |
2631 | } | |
a64c54cf | 2632 | sbsec->sid = c->sid[0]; |
1da177e4 | 2633 | } else { |
f31e7994 WL |
2634 | rc = __security_genfs_sid(fstype, "/", SECCLASS_DIR, |
2635 | &sbsec->sid); | |
089be43e | 2636 | if (rc) { |
a64c54cf | 2637 | sbsec->behavior = SECURITY_FS_USE_NONE; |
089be43e JM |
2638 | rc = 0; |
2639 | } else { | |
a64c54cf | 2640 | sbsec->behavior = SECURITY_FS_USE_GENFS; |
089be43e | 2641 | } |
1da177e4 LT |
2642 | } |
2643 | ||
2644 | out: | |
0804d113 | 2645 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2646 | return rc; |
2647 | } | |
2648 | ||
2649 | int security_get_bools(int *len, char ***names, int **values) | |
2650 | { | |
4b02b524 | 2651 | int i, rc; |
1da177e4 | 2652 | |
0804d113 | 2653 | read_lock(&policy_rwlock); |
1da177e4 LT |
2654 | *names = NULL; |
2655 | *values = NULL; | |
2656 | ||
4b02b524 | 2657 | rc = 0; |
1da177e4 | 2658 | *len = policydb.p_bools.nprim; |
4b02b524 | 2659 | if (!*len) |
1da177e4 | 2660 | goto out; |
1da177e4 | 2661 | |
4b02b524 EP |
2662 | rc = -ENOMEM; |
2663 | *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC); | |
1da177e4 LT |
2664 | if (!*names) |
2665 | goto err; | |
1da177e4 | 2666 | |
4b02b524 EP |
2667 | rc = -ENOMEM; |
2668 | *values = kcalloc(*len, sizeof(int), GFP_ATOMIC); | |
1da177e4 LT |
2669 | if (!*values) |
2670 | goto err; | |
2671 | ||
2672 | for (i = 0; i < *len; i++) { | |
1da177e4 | 2673 | (*values)[i] = policydb.bool_val_to_struct[i]->state; |
4b02b524 EP |
2674 | |
2675 | rc = -ENOMEM; | |
21b76f19 | 2676 | (*names)[i] = kstrdup(sym_name(&policydb, SYM_BOOLS, i), GFP_ATOMIC); |
1da177e4 LT |
2677 | if (!(*names)[i]) |
2678 | goto err; | |
1da177e4 LT |
2679 | } |
2680 | rc = 0; | |
2681 | out: | |
0804d113 | 2682 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2683 | return rc; |
2684 | err: | |
2685 | if (*names) { | |
2686 | for (i = 0; i < *len; i++) | |
9a5f04bf | 2687 | kfree((*names)[i]); |
1da177e4 | 2688 | } |
9a5f04bf | 2689 | kfree(*values); |
1da177e4 LT |
2690 | goto out; |
2691 | } | |
2692 | ||
2693 | ||
2694 | int security_set_bools(int len, int *values) | |
2695 | { | |
4b02b524 | 2696 | int i, rc; |
1da177e4 LT |
2697 | int lenp, seqno = 0; |
2698 | struct cond_node *cur; | |
2699 | ||
0804d113 | 2700 | write_lock_irq(&policy_rwlock); |
1da177e4 | 2701 | |
4b02b524 | 2702 | rc = -EFAULT; |
1da177e4 | 2703 | lenp = policydb.p_bools.nprim; |
4b02b524 | 2704 | if (len != lenp) |
1da177e4 | 2705 | goto out; |
1da177e4 | 2706 | |
1da177e4 | 2707 | for (i = 0; i < len; i++) { |
af601e46 SG |
2708 | if (!!values[i] != policydb.bool_val_to_struct[i]->state) { |
2709 | audit_log(current->audit_context, GFP_ATOMIC, | |
2710 | AUDIT_MAC_CONFIG_CHANGE, | |
4746ec5b | 2711 | "bool=%s val=%d old_val=%d auid=%u ses=%u", |
ac76c05b | 2712 | sym_name(&policydb, SYM_BOOLS, i), |
af601e46 SG |
2713 | !!values[i], |
2714 | policydb.bool_val_to_struct[i]->state, | |
581abc09 | 2715 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
4746ec5b | 2716 | audit_get_sessionid(current)); |
af601e46 | 2717 | } |
5d55a345 | 2718 | if (values[i]) |
1da177e4 | 2719 | policydb.bool_val_to_struct[i]->state = 1; |
5d55a345 | 2720 | else |
1da177e4 | 2721 | policydb.bool_val_to_struct[i]->state = 0; |
1da177e4 | 2722 | } |
1da177e4 | 2723 | |
dbc74c65 | 2724 | for (cur = policydb.cond_list; cur; cur = cur->next) { |
1da177e4 LT |
2725 | rc = evaluate_cond_node(&policydb, cur); |
2726 | if (rc) | |
2727 | goto out; | |
2728 | } | |
2729 | ||
2730 | seqno = ++latest_granting; | |
4b02b524 | 2731 | rc = 0; |
1da177e4 | 2732 | out: |
0804d113 | 2733 | write_unlock_irq(&policy_rwlock); |
1da177e4 LT |
2734 | if (!rc) { |
2735 | avc_ss_reset(seqno); | |
2736 | selnl_notify_policyload(seqno); | |
11904167 | 2737 | selinux_status_update_policyload(seqno); |
342a0cff | 2738 | selinux_xfrm_notify_policyload(); |
1da177e4 LT |
2739 | } |
2740 | return rc; | |
2741 | } | |
2742 | ||
0fd71a62 | 2743 | int security_get_bool_value(int index) |
1da177e4 | 2744 | { |
4b02b524 | 2745 | int rc; |
1da177e4 LT |
2746 | int len; |
2747 | ||
0804d113 | 2748 | read_lock(&policy_rwlock); |
1da177e4 | 2749 | |
4b02b524 | 2750 | rc = -EFAULT; |
1da177e4 | 2751 | len = policydb.p_bools.nprim; |
0fd71a62 | 2752 | if (index >= len) |
1da177e4 | 2753 | goto out; |
1da177e4 | 2754 | |
0fd71a62 | 2755 | rc = policydb.bool_val_to_struct[index]->state; |
1da177e4 | 2756 | out: |
0804d113 | 2757 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2758 | return rc; |
2759 | } | |
376bd9cb | 2760 | |
e900a7d9 SS |
2761 | static int security_preserve_bools(struct policydb *p) |
2762 | { | |
2763 | int rc, nbools = 0, *bvalues = NULL, i; | |
2764 | char **bnames = NULL; | |
2765 | struct cond_bool_datum *booldatum; | |
2766 | struct cond_node *cur; | |
2767 | ||
2768 | rc = security_get_bools(&nbools, &bnames, &bvalues); | |
2769 | if (rc) | |
2770 | goto out; | |
2771 | for (i = 0; i < nbools; i++) { | |
2772 | booldatum = hashtab_search(p->p_bools.table, bnames[i]); | |
2773 | if (booldatum) | |
2774 | booldatum->state = bvalues[i]; | |
2775 | } | |
dbc74c65 | 2776 | for (cur = p->cond_list; cur; cur = cur->next) { |
e900a7d9 SS |
2777 | rc = evaluate_cond_node(p, cur); |
2778 | if (rc) | |
2779 | goto out; | |
2780 | } | |
2781 | ||
2782 | out: | |
2783 | if (bnames) { | |
2784 | for (i = 0; i < nbools; i++) | |
2785 | kfree(bnames[i]); | |
2786 | } | |
2787 | kfree(bnames); | |
2788 | kfree(bvalues); | |
2789 | return rc; | |
2790 | } | |
2791 | ||
08554d6b VY |
2792 | /* |
2793 | * security_sid_mls_copy() - computes a new sid based on the given | |
2794 | * sid and the mls portion of mls_sid. | |
2795 | */ | |
2796 | int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid) | |
2797 | { | |
2798 | struct context *context1; | |
2799 | struct context *context2; | |
2800 | struct context newcon; | |
2801 | char *s; | |
2802 | u32 len; | |
4b02b524 | 2803 | int rc; |
08554d6b | 2804 | |
4b02b524 | 2805 | rc = 0; |
0719aaf5 | 2806 | if (!ss_initialized || !policydb.mls_enabled) { |
08554d6b VY |
2807 | *new_sid = sid; |
2808 | goto out; | |
2809 | } | |
2810 | ||
2811 | context_init(&newcon); | |
2812 | ||
0804d113 | 2813 | read_lock(&policy_rwlock); |
4b02b524 EP |
2814 | |
2815 | rc = -EINVAL; | |
08554d6b VY |
2816 | context1 = sidtab_search(&sidtab, sid); |
2817 | if (!context1) { | |
744ba35e EP |
2818 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
2819 | __func__, sid); | |
08554d6b VY |
2820 | goto out_unlock; |
2821 | } | |
2822 | ||
4b02b524 | 2823 | rc = -EINVAL; |
08554d6b VY |
2824 | context2 = sidtab_search(&sidtab, mls_sid); |
2825 | if (!context2) { | |
744ba35e EP |
2826 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
2827 | __func__, mls_sid); | |
08554d6b VY |
2828 | goto out_unlock; |
2829 | } | |
2830 | ||
2831 | newcon.user = context1->user; | |
2832 | newcon.role = context1->role; | |
2833 | newcon.type = context1->type; | |
0efc61ea | 2834 | rc = mls_context_cpy(&newcon, context2); |
08554d6b VY |
2835 | if (rc) |
2836 | goto out_unlock; | |
2837 | ||
08554d6b VY |
2838 | /* Check the validity of the new context. */ |
2839 | if (!policydb_context_isvalid(&policydb, &newcon)) { | |
2840 | rc = convert_context_handle_invalid_context(&newcon); | |
4b02b524 EP |
2841 | if (rc) { |
2842 | if (!context_struct_to_string(&newcon, &s, &len)) { | |
4093a844 RGB |
2843 | audit_log(current->audit_context, |
2844 | GFP_ATOMIC, AUDIT_SELINUX_ERR, | |
2845 | "op=security_sid_mls_copy " | |
2846 | "invalid_context=%s", s); | |
4b02b524 EP |
2847 | kfree(s); |
2848 | } | |
2849 | goto out_unlock; | |
2850 | } | |
08554d6b VY |
2851 | } |
2852 | ||
2853 | rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid); | |
08554d6b | 2854 | out_unlock: |
0804d113 | 2855 | read_unlock(&policy_rwlock); |
08554d6b VY |
2856 | context_destroy(&newcon); |
2857 | out: | |
2858 | return rc; | |
2859 | } | |
2860 | ||
220deb96 PM |
2861 | /** |
2862 | * security_net_peersid_resolve - Compare and resolve two network peer SIDs | |
2863 | * @nlbl_sid: NetLabel SID | |
2864 | * @nlbl_type: NetLabel labeling protocol type | |
2865 | * @xfrm_sid: XFRM SID | |
2866 | * | |
2867 | * Description: | |
2868 | * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be | |
2869 | * resolved into a single SID it is returned via @peer_sid and the function | |
2870 | * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function | |
2871 | * returns a negative value. A table summarizing the behavior is below: | |
2872 | * | |
2873 | * | function return | @sid | |
2874 | * ------------------------------+-----------------+----------------- | |
2875 | * no peer labels | 0 | SECSID_NULL | |
2876 | * single peer label | 0 | <peer_label> | |
2877 | * multiple, consistent labels | 0 | <peer_label> | |
2878 | * multiple, inconsistent labels | -<errno> | SECSID_NULL | |
2879 | * | |
2880 | */ | |
2881 | int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type, | |
2882 | u32 xfrm_sid, | |
2883 | u32 *peer_sid) | |
2884 | { | |
2885 | int rc; | |
2886 | struct context *nlbl_ctx; | |
2887 | struct context *xfrm_ctx; | |
2888 | ||
4b02b524 EP |
2889 | *peer_sid = SECSID_NULL; |
2890 | ||
220deb96 PM |
2891 | /* handle the common (which also happens to be the set of easy) cases |
2892 | * right away, these two if statements catch everything involving a | |
2893 | * single or absent peer SID/label */ | |
2894 | if (xfrm_sid == SECSID_NULL) { | |
2895 | *peer_sid = nlbl_sid; | |
2896 | return 0; | |
2897 | } | |
2898 | /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label | |
2899 | * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label | |
2900 | * is present */ | |
2901 | if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) { | |
2902 | *peer_sid = xfrm_sid; | |
2903 | return 0; | |
2904 | } | |
2905 | ||
2906 | /* we don't need to check ss_initialized here since the only way both | |
2907 | * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the | |
2908 | * security server was initialized and ss_initialized was true */ | |
4b02b524 | 2909 | if (!policydb.mls_enabled) |
220deb96 | 2910 | return 0; |
220deb96 | 2911 | |
0804d113 | 2912 | read_lock(&policy_rwlock); |
220deb96 | 2913 | |
4b02b524 | 2914 | rc = -EINVAL; |
220deb96 PM |
2915 | nlbl_ctx = sidtab_search(&sidtab, nlbl_sid); |
2916 | if (!nlbl_ctx) { | |
744ba35e EP |
2917 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
2918 | __func__, nlbl_sid); | |
4b02b524 | 2919 | goto out; |
220deb96 | 2920 | } |
4b02b524 | 2921 | rc = -EINVAL; |
220deb96 PM |
2922 | xfrm_ctx = sidtab_search(&sidtab, xfrm_sid); |
2923 | if (!xfrm_ctx) { | |
744ba35e EP |
2924 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
2925 | __func__, xfrm_sid); | |
4b02b524 | 2926 | goto out; |
220deb96 PM |
2927 | } |
2928 | rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES); | |
4b02b524 EP |
2929 | if (rc) |
2930 | goto out; | |
220deb96 | 2931 | |
4b02b524 EP |
2932 | /* at present NetLabel SIDs/labels really only carry MLS |
2933 | * information so if the MLS portion of the NetLabel SID | |
2934 | * matches the MLS portion of the labeled XFRM SID/label | |
2935 | * then pass along the XFRM SID as it is the most | |
2936 | * expressive */ | |
2937 | *peer_sid = xfrm_sid; | |
2938 | out: | |
0804d113 | 2939 | read_unlock(&policy_rwlock); |
220deb96 PM |
2940 | return rc; |
2941 | } | |
2942 | ||
55fcf09b CP |
2943 | static int get_classes_callback(void *k, void *d, void *args) |
2944 | { | |
2945 | struct class_datum *datum = d; | |
2946 | char *name = k, **classes = args; | |
2947 | int value = datum->value - 1; | |
2948 | ||
2949 | classes[value] = kstrdup(name, GFP_ATOMIC); | |
2950 | if (!classes[value]) | |
2951 | return -ENOMEM; | |
2952 | ||
2953 | return 0; | |
2954 | } | |
2955 | ||
2956 | int security_get_classes(char ***classes, int *nclasses) | |
2957 | { | |
4b02b524 | 2958 | int rc; |
55fcf09b | 2959 | |
0804d113 | 2960 | read_lock(&policy_rwlock); |
55fcf09b | 2961 | |
4b02b524 | 2962 | rc = -ENOMEM; |
55fcf09b | 2963 | *nclasses = policydb.p_classes.nprim; |
9f59f90b | 2964 | *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC); |
55fcf09b CP |
2965 | if (!*classes) |
2966 | goto out; | |
2967 | ||
2968 | rc = hashtab_map(policydb.p_classes.table, get_classes_callback, | |
2969 | *classes); | |
4b02b524 | 2970 | if (rc) { |
55fcf09b CP |
2971 | int i; |
2972 | for (i = 0; i < *nclasses; i++) | |
2973 | kfree((*classes)[i]); | |
2974 | kfree(*classes); | |
2975 | } | |
2976 | ||
2977 | out: | |
0804d113 | 2978 | read_unlock(&policy_rwlock); |
55fcf09b CP |
2979 | return rc; |
2980 | } | |
2981 | ||
2982 | static int get_permissions_callback(void *k, void *d, void *args) | |
2983 | { | |
2984 | struct perm_datum *datum = d; | |
2985 | char *name = k, **perms = args; | |
2986 | int value = datum->value - 1; | |
2987 | ||
2988 | perms[value] = kstrdup(name, GFP_ATOMIC); | |
2989 | if (!perms[value]) | |
2990 | return -ENOMEM; | |
2991 | ||
2992 | return 0; | |
2993 | } | |
2994 | ||
2995 | int security_get_permissions(char *class, char ***perms, int *nperms) | |
2996 | { | |
4b02b524 | 2997 | int rc, i; |
55fcf09b CP |
2998 | struct class_datum *match; |
2999 | ||
0804d113 | 3000 | read_lock(&policy_rwlock); |
55fcf09b | 3001 | |
4b02b524 | 3002 | rc = -EINVAL; |
55fcf09b CP |
3003 | match = hashtab_search(policydb.p_classes.table, class); |
3004 | if (!match) { | |
744ba35e | 3005 | printk(KERN_ERR "SELinux: %s: unrecognized class %s\n", |
dd6f953a | 3006 | __func__, class); |
55fcf09b CP |
3007 | goto out; |
3008 | } | |
3009 | ||
4b02b524 | 3010 | rc = -ENOMEM; |
55fcf09b | 3011 | *nperms = match->permissions.nprim; |
9f59f90b | 3012 | *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC); |
55fcf09b CP |
3013 | if (!*perms) |
3014 | goto out; | |
3015 | ||
3016 | if (match->comdatum) { | |
3017 | rc = hashtab_map(match->comdatum->permissions.table, | |
3018 | get_permissions_callback, *perms); | |
4b02b524 | 3019 | if (rc) |
55fcf09b CP |
3020 | goto err; |
3021 | } | |
3022 | ||
3023 | rc = hashtab_map(match->permissions.table, get_permissions_callback, | |
3024 | *perms); | |
4b02b524 | 3025 | if (rc) |
55fcf09b CP |
3026 | goto err; |
3027 | ||
3028 | out: | |
0804d113 | 3029 | read_unlock(&policy_rwlock); |
55fcf09b CP |
3030 | return rc; |
3031 | ||
3032 | err: | |
0804d113 | 3033 | read_unlock(&policy_rwlock); |
55fcf09b CP |
3034 | for (i = 0; i < *nperms; i++) |
3035 | kfree((*perms)[i]); | |
3036 | kfree(*perms); | |
3037 | return rc; | |
3038 | } | |
3039 | ||
3f12070e EP |
3040 | int security_get_reject_unknown(void) |
3041 | { | |
3042 | return policydb.reject_unknown; | |
3043 | } | |
3044 | ||
3045 | int security_get_allow_unknown(void) | |
3046 | { | |
3047 | return policydb.allow_unknown; | |
3048 | } | |
3049 | ||
3bb56b25 PM |
3050 | /** |
3051 | * security_policycap_supported - Check for a specific policy capability | |
3052 | * @req_cap: capability | |
3053 | * | |
3054 | * Description: | |
3055 | * This function queries the currently loaded policy to see if it supports the | |
3056 | * capability specified by @req_cap. Returns true (1) if the capability is | |
3057 | * supported, false (0) if it isn't supported. | |
3058 | * | |
3059 | */ | |
3060 | int security_policycap_supported(unsigned int req_cap) | |
3061 | { | |
3062 | int rc; | |
3063 | ||
0804d113 | 3064 | read_lock(&policy_rwlock); |
3bb56b25 | 3065 | rc = ebitmap_get_bit(&policydb.policycaps, req_cap); |
0804d113 | 3066 | read_unlock(&policy_rwlock); |
3bb56b25 PM |
3067 | |
3068 | return rc; | |
3069 | } | |
3070 | ||
376bd9cb DG |
3071 | struct selinux_audit_rule { |
3072 | u32 au_seqno; | |
3073 | struct context au_ctxt; | |
3074 | }; | |
3075 | ||
9d57a7f9 | 3076 | void selinux_audit_rule_free(void *vrule) |
376bd9cb | 3077 | { |
9d57a7f9 AD |
3078 | struct selinux_audit_rule *rule = vrule; |
3079 | ||
376bd9cb DG |
3080 | if (rule) { |
3081 | context_destroy(&rule->au_ctxt); | |
3082 | kfree(rule); | |
3083 | } | |
3084 | } | |
3085 | ||
9d57a7f9 | 3086 | int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) |
376bd9cb DG |
3087 | { |
3088 | struct selinux_audit_rule *tmprule; | |
3089 | struct role_datum *roledatum; | |
3090 | struct type_datum *typedatum; | |
3091 | struct user_datum *userdatum; | |
9d57a7f9 | 3092 | struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule; |
376bd9cb DG |
3093 | int rc = 0; |
3094 | ||
3095 | *rule = NULL; | |
3096 | ||
3097 | if (!ss_initialized) | |
3ad40d64 | 3098 | return -EOPNOTSUPP; |
376bd9cb DG |
3099 | |
3100 | switch (field) { | |
3a6b9f85 DG |
3101 | case AUDIT_SUBJ_USER: |
3102 | case AUDIT_SUBJ_ROLE: | |
3103 | case AUDIT_SUBJ_TYPE: | |
6e5a2d1d DG |
3104 | case AUDIT_OBJ_USER: |
3105 | case AUDIT_OBJ_ROLE: | |
3106 | case AUDIT_OBJ_TYPE: | |
376bd9cb | 3107 | /* only 'equals' and 'not equals' fit user, role, and type */ |
5af75d8d | 3108 | if (op != Audit_equal && op != Audit_not_equal) |
376bd9cb DG |
3109 | return -EINVAL; |
3110 | break; | |
3a6b9f85 DG |
3111 | case AUDIT_SUBJ_SEN: |
3112 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
3113 | case AUDIT_OBJ_LEV_LOW: |
3114 | case AUDIT_OBJ_LEV_HIGH: | |
25985edc | 3115 | /* we do not allow a range, indicated by the presence of '-' */ |
376bd9cb DG |
3116 | if (strchr(rulestr, '-')) |
3117 | return -EINVAL; | |
3118 | break; | |
3119 | default: | |
3120 | /* only the above fields are valid */ | |
3121 | return -EINVAL; | |
3122 | } | |
3123 | ||
3124 | tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL); | |
3125 | if (!tmprule) | |
3126 | return -ENOMEM; | |
3127 | ||
3128 | context_init(&tmprule->au_ctxt); | |
3129 | ||
0804d113 | 3130 | read_lock(&policy_rwlock); |
376bd9cb DG |
3131 | |
3132 | tmprule->au_seqno = latest_granting; | |
3133 | ||
3134 | switch (field) { | |
3a6b9f85 | 3135 | case AUDIT_SUBJ_USER: |
6e5a2d1d | 3136 | case AUDIT_OBJ_USER: |
4b02b524 | 3137 | rc = -EINVAL; |
376bd9cb DG |
3138 | userdatum = hashtab_search(policydb.p_users.table, rulestr); |
3139 | if (!userdatum) | |
4b02b524 EP |
3140 | goto out; |
3141 | tmprule->au_ctxt.user = userdatum->value; | |
376bd9cb | 3142 | break; |
3a6b9f85 | 3143 | case AUDIT_SUBJ_ROLE: |
6e5a2d1d | 3144 | case AUDIT_OBJ_ROLE: |
4b02b524 | 3145 | rc = -EINVAL; |
376bd9cb DG |
3146 | roledatum = hashtab_search(policydb.p_roles.table, rulestr); |
3147 | if (!roledatum) | |
4b02b524 EP |
3148 | goto out; |
3149 | tmprule->au_ctxt.role = roledatum->value; | |
376bd9cb | 3150 | break; |
3a6b9f85 | 3151 | case AUDIT_SUBJ_TYPE: |
6e5a2d1d | 3152 | case AUDIT_OBJ_TYPE: |
4b02b524 | 3153 | rc = -EINVAL; |
376bd9cb DG |
3154 | typedatum = hashtab_search(policydb.p_types.table, rulestr); |
3155 | if (!typedatum) | |
4b02b524 EP |
3156 | goto out; |
3157 | tmprule->au_ctxt.type = typedatum->value; | |
376bd9cb | 3158 | break; |
3a6b9f85 DG |
3159 | case AUDIT_SUBJ_SEN: |
3160 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
3161 | case AUDIT_OBJ_LEV_LOW: |
3162 | case AUDIT_OBJ_LEV_HIGH: | |
376bd9cb | 3163 | rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC); |
4b02b524 EP |
3164 | if (rc) |
3165 | goto out; | |
376bd9cb DG |
3166 | break; |
3167 | } | |
4b02b524 EP |
3168 | rc = 0; |
3169 | out: | |
0804d113 | 3170 | read_unlock(&policy_rwlock); |
376bd9cb DG |
3171 | |
3172 | if (rc) { | |
3173 | selinux_audit_rule_free(tmprule); | |
3174 | tmprule = NULL; | |
3175 | } | |
3176 | ||
3177 | *rule = tmprule; | |
3178 | ||
3179 | return rc; | |
3180 | } | |
3181 | ||
9d57a7f9 AD |
3182 | /* Check to see if the rule contains any selinux fields */ |
3183 | int selinux_audit_rule_known(struct audit_krule *rule) | |
3184 | { | |
3185 | int i; | |
3186 | ||
3187 | for (i = 0; i < rule->field_count; i++) { | |
3188 | struct audit_field *f = &rule->fields[i]; | |
3189 | switch (f->type) { | |
3190 | case AUDIT_SUBJ_USER: | |
3191 | case AUDIT_SUBJ_ROLE: | |
3192 | case AUDIT_SUBJ_TYPE: | |
3193 | case AUDIT_SUBJ_SEN: | |
3194 | case AUDIT_SUBJ_CLR: | |
3195 | case AUDIT_OBJ_USER: | |
3196 | case AUDIT_OBJ_ROLE: | |
3197 | case AUDIT_OBJ_TYPE: | |
3198 | case AUDIT_OBJ_LEV_LOW: | |
3199 | case AUDIT_OBJ_LEV_HIGH: | |
3200 | return 1; | |
3201 | } | |
3202 | } | |
3203 | ||
3204 | return 0; | |
3205 | } | |
3206 | ||
3207 | int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule, | |
f5269710 | 3208 | struct audit_context *actx) |
376bd9cb DG |
3209 | { |
3210 | struct context *ctxt; | |
3211 | struct mls_level *level; | |
9d57a7f9 | 3212 | struct selinux_audit_rule *rule = vrule; |
376bd9cb DG |
3213 | int match = 0; |
3214 | ||
9ad42a79 RGB |
3215 | if (unlikely(!rule)) { |
3216 | WARN_ONCE(1, "selinux_audit_rule_match: missing rule\n"); | |
376bd9cb DG |
3217 | return -ENOENT; |
3218 | } | |
3219 | ||
0804d113 | 3220 | read_lock(&policy_rwlock); |
376bd9cb DG |
3221 | |
3222 | if (rule->au_seqno < latest_granting) { | |
376bd9cb DG |
3223 | match = -ESTALE; |
3224 | goto out; | |
3225 | } | |
3226 | ||
9a2f44f0 | 3227 | ctxt = sidtab_search(&sidtab, sid); |
9ad42a79 RGB |
3228 | if (unlikely(!ctxt)) { |
3229 | WARN_ONCE(1, "selinux_audit_rule_match: unrecognized SID %d\n", | |
5d55a345 | 3230 | sid); |
376bd9cb DG |
3231 | match = -ENOENT; |
3232 | goto out; | |
3233 | } | |
3234 | ||
3235 | /* a field/op pair that is not caught here will simply fall through | |
3236 | without a match */ | |
3237 | switch (field) { | |
3a6b9f85 | 3238 | case AUDIT_SUBJ_USER: |
6e5a2d1d | 3239 | case AUDIT_OBJ_USER: |
376bd9cb | 3240 | switch (op) { |
5af75d8d | 3241 | case Audit_equal: |
376bd9cb DG |
3242 | match = (ctxt->user == rule->au_ctxt.user); |
3243 | break; | |
5af75d8d | 3244 | case Audit_not_equal: |
376bd9cb DG |
3245 | match = (ctxt->user != rule->au_ctxt.user); |
3246 | break; | |
3247 | } | |
3248 | break; | |
3a6b9f85 | 3249 | case AUDIT_SUBJ_ROLE: |
6e5a2d1d | 3250 | case AUDIT_OBJ_ROLE: |
376bd9cb | 3251 | switch (op) { |
5af75d8d | 3252 | case Audit_equal: |
376bd9cb DG |
3253 | match = (ctxt->role == rule->au_ctxt.role); |
3254 | break; | |
5af75d8d | 3255 | case Audit_not_equal: |
376bd9cb DG |
3256 | match = (ctxt->role != rule->au_ctxt.role); |
3257 | break; | |
3258 | } | |
3259 | break; | |
3a6b9f85 | 3260 | case AUDIT_SUBJ_TYPE: |
6e5a2d1d | 3261 | case AUDIT_OBJ_TYPE: |
376bd9cb | 3262 | switch (op) { |
5af75d8d | 3263 | case Audit_equal: |
376bd9cb DG |
3264 | match = (ctxt->type == rule->au_ctxt.type); |
3265 | break; | |
5af75d8d | 3266 | case Audit_not_equal: |
376bd9cb DG |
3267 | match = (ctxt->type != rule->au_ctxt.type); |
3268 | break; | |
3269 | } | |
3270 | break; | |
3a6b9f85 DG |
3271 | case AUDIT_SUBJ_SEN: |
3272 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
3273 | case AUDIT_OBJ_LEV_LOW: |
3274 | case AUDIT_OBJ_LEV_HIGH: | |
3275 | level = ((field == AUDIT_SUBJ_SEN || | |
5d55a345 EP |
3276 | field == AUDIT_OBJ_LEV_LOW) ? |
3277 | &ctxt->range.level[0] : &ctxt->range.level[1]); | |
376bd9cb | 3278 | switch (op) { |
5af75d8d | 3279 | case Audit_equal: |
376bd9cb | 3280 | match = mls_level_eq(&rule->au_ctxt.range.level[0], |
5d55a345 | 3281 | level); |
376bd9cb | 3282 | break; |
5af75d8d | 3283 | case Audit_not_equal: |
376bd9cb | 3284 | match = !mls_level_eq(&rule->au_ctxt.range.level[0], |
5d55a345 | 3285 | level); |
376bd9cb | 3286 | break; |
5af75d8d | 3287 | case Audit_lt: |
376bd9cb | 3288 | match = (mls_level_dom(&rule->au_ctxt.range.level[0], |
5d55a345 EP |
3289 | level) && |
3290 | !mls_level_eq(&rule->au_ctxt.range.level[0], | |
3291 | level)); | |
376bd9cb | 3292 | break; |
5af75d8d | 3293 | case Audit_le: |
376bd9cb | 3294 | match = mls_level_dom(&rule->au_ctxt.range.level[0], |
5d55a345 | 3295 | level); |
376bd9cb | 3296 | break; |
5af75d8d | 3297 | case Audit_gt: |
376bd9cb | 3298 | match = (mls_level_dom(level, |
5d55a345 EP |
3299 | &rule->au_ctxt.range.level[0]) && |
3300 | !mls_level_eq(level, | |
3301 | &rule->au_ctxt.range.level[0])); | |
376bd9cb | 3302 | break; |
5af75d8d | 3303 | case Audit_ge: |
376bd9cb | 3304 | match = mls_level_dom(level, |
5d55a345 | 3305 | &rule->au_ctxt.range.level[0]); |
376bd9cb DG |
3306 | break; |
3307 | } | |
3308 | } | |
3309 | ||
3310 | out: | |
0804d113 | 3311 | read_unlock(&policy_rwlock); |
376bd9cb DG |
3312 | return match; |
3313 | } | |
3314 | ||
9d57a7f9 | 3315 | static int (*aurule_callback)(void) = audit_update_lsm_rules; |
376bd9cb | 3316 | |
562c99f2 | 3317 | static int aurule_avc_callback(u32 event) |
376bd9cb DG |
3318 | { |
3319 | int err = 0; | |
3320 | ||
3321 | if (event == AVC_CALLBACK_RESET && aurule_callback) | |
3322 | err = aurule_callback(); | |
3323 | return err; | |
3324 | } | |
3325 | ||
3326 | static int __init aurule_init(void) | |
3327 | { | |
3328 | int err; | |
3329 | ||
562c99f2 | 3330 | err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET); |
376bd9cb DG |
3331 | if (err) |
3332 | panic("avc_add_callback() failed, error %d\n", err); | |
3333 | ||
3334 | return err; | |
3335 | } | |
3336 | __initcall(aurule_init); | |
3337 | ||
7420ed23 | 3338 | #ifdef CONFIG_NETLABEL |
7420ed23 | 3339 | /** |
5778eabd PM |
3340 | * security_netlbl_cache_add - Add an entry to the NetLabel cache |
3341 | * @secattr: the NetLabel packet security attributes | |
5dbe1eb0 | 3342 | * @sid: the SELinux SID |
7420ed23 VY |
3343 | * |
3344 | * Description: | |
3345 | * Attempt to cache the context in @ctx, which was derived from the packet in | |
5778eabd PM |
3346 | * @skb, in the NetLabel subsystem cache. This function assumes @secattr has |
3347 | * already been initialized. | |
7420ed23 VY |
3348 | * |
3349 | */ | |
5778eabd | 3350 | static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr, |
5dbe1eb0 | 3351 | u32 sid) |
7420ed23 | 3352 | { |
5dbe1eb0 | 3353 | u32 *sid_cache; |
7420ed23 | 3354 | |
5dbe1eb0 PM |
3355 | sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC); |
3356 | if (sid_cache == NULL) | |
5778eabd | 3357 | return; |
5dbe1eb0 PM |
3358 | secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC); |
3359 | if (secattr->cache == NULL) { | |
3360 | kfree(sid_cache); | |
5778eabd | 3361 | return; |
0ec8abd7 | 3362 | } |
7420ed23 | 3363 | |
5dbe1eb0 PM |
3364 | *sid_cache = sid; |
3365 | secattr->cache->free = kfree; | |
3366 | secattr->cache->data = sid_cache; | |
5778eabd | 3367 | secattr->flags |= NETLBL_SECATTR_CACHE; |
7420ed23 VY |
3368 | } |
3369 | ||
3370 | /** | |
5778eabd | 3371 | * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID |
7420ed23 | 3372 | * @secattr: the NetLabel packet security attributes |
7420ed23 VY |
3373 | * @sid: the SELinux SID |
3374 | * | |
3375 | * Description: | |
5778eabd | 3376 | * Convert the given NetLabel security attributes in @secattr into a |
7420ed23 | 3377 | * SELinux SID. If the @secattr field does not contain a full SELinux |
25985edc | 3378 | * SID/context then use SECINITSID_NETMSG as the foundation. If possible the |
5dbe1eb0 PM |
3379 | * 'cache' field of @secattr is set and the CACHE flag is set; this is to |
3380 | * allow the @secattr to be used by NetLabel to cache the secattr to SID | |
3381 | * conversion for future lookups. Returns zero on success, negative values on | |
3382 | * failure. | |
7420ed23 VY |
3383 | * |
3384 | */ | |
5778eabd | 3385 | int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr, |
5778eabd | 3386 | u32 *sid) |
7420ed23 | 3387 | { |
7ae9f23c | 3388 | int rc; |
7420ed23 VY |
3389 | struct context *ctx; |
3390 | struct context ctx_new; | |
5778eabd PM |
3391 | |
3392 | if (!ss_initialized) { | |
3393 | *sid = SECSID_NULL; | |
3394 | return 0; | |
3395 | } | |
7420ed23 | 3396 | |
0804d113 | 3397 | read_lock(&policy_rwlock); |
7420ed23 | 3398 | |
7ae9f23c | 3399 | if (secattr->flags & NETLBL_SECATTR_CACHE) |
5dbe1eb0 | 3400 | *sid = *(u32 *)secattr->cache->data; |
7ae9f23c | 3401 | else if (secattr->flags & NETLBL_SECATTR_SECID) |
16efd454 | 3402 | *sid = secattr->attr.secid; |
7ae9f23c EP |
3403 | else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) { |
3404 | rc = -EIDRM; | |
5dbe1eb0 | 3405 | ctx = sidtab_search(&sidtab, SECINITSID_NETMSG); |
7420ed23 | 3406 | if (ctx == NULL) |
7ae9f23c | 3407 | goto out; |
7420ed23 | 3408 | |
81990fbd | 3409 | context_init(&ctx_new); |
7420ed23 VY |
3410 | ctx_new.user = ctx->user; |
3411 | ctx_new.role = ctx->role; | |
3412 | ctx_new.type = ctx->type; | |
02752760 | 3413 | mls_import_netlbl_lvl(&ctx_new, secattr); |
701a90ba | 3414 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
da8026fa | 3415 | rc = mls_import_netlbl_cat(&ctx_new, secattr); |
7ae9f23c EP |
3416 | if (rc) |
3417 | goto out; | |
7420ed23 | 3418 | } |
7ae9f23c EP |
3419 | rc = -EIDRM; |
3420 | if (!mls_context_isvalid(&policydb, &ctx_new)) | |
3421 | goto out_free; | |
7420ed23 VY |
3422 | |
3423 | rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid); | |
7ae9f23c EP |
3424 | if (rc) |
3425 | goto out_free; | |
7420ed23 | 3426 | |
5dbe1eb0 | 3427 | security_netlbl_cache_add(secattr, *sid); |
5778eabd | 3428 | |
7420ed23 | 3429 | ebitmap_destroy(&ctx_new.range.level[0].cat); |
7ae9f23c | 3430 | } else |
388b2405 | 3431 | *sid = SECSID_NULL; |
7420ed23 | 3432 | |
0804d113 | 3433 | read_unlock(&policy_rwlock); |
7ae9f23c EP |
3434 | return 0; |
3435 | out_free: | |
7420ed23 | 3436 | ebitmap_destroy(&ctx_new.range.level[0].cat); |
7ae9f23c EP |
3437 | out: |
3438 | read_unlock(&policy_rwlock); | |
3439 | return rc; | |
7420ed23 VY |
3440 | } |
3441 | ||
3442 | /** | |
5778eabd PM |
3443 | * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr |
3444 | * @sid: the SELinux SID | |
3445 | * @secattr: the NetLabel packet security attributes | |
7420ed23 VY |
3446 | * |
3447 | * Description: | |
5778eabd PM |
3448 | * Convert the given SELinux SID in @sid into a NetLabel security attribute. |
3449 | * Returns zero on success, negative values on failure. | |
7420ed23 VY |
3450 | * |
3451 | */ | |
5778eabd | 3452 | int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr) |
7420ed23 | 3453 | { |
99d854d2 | 3454 | int rc; |
7420ed23 VY |
3455 | struct context *ctx; |
3456 | ||
3457 | if (!ss_initialized) | |
3458 | return 0; | |
3459 | ||
0804d113 | 3460 | read_lock(&policy_rwlock); |
4b02b524 EP |
3461 | |
3462 | rc = -ENOENT; | |
7420ed23 | 3463 | ctx = sidtab_search(&sidtab, sid); |
4b02b524 EP |
3464 | if (ctx == NULL) |
3465 | goto out; | |
3466 | ||
3467 | rc = -ENOMEM; | |
ac76c05b | 3468 | secattr->domain = kstrdup(sym_name(&policydb, SYM_TYPES, ctx->type - 1), |
5778eabd | 3469 | GFP_ATOMIC); |
4b02b524 EP |
3470 | if (secattr->domain == NULL) |
3471 | goto out; | |
3472 | ||
8d75899d PM |
3473 | secattr->attr.secid = sid; |
3474 | secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID; | |
5778eabd PM |
3475 | mls_export_netlbl_lvl(ctx, secattr); |
3476 | rc = mls_export_netlbl_cat(ctx, secattr); | |
4b02b524 | 3477 | out: |
0804d113 | 3478 | read_unlock(&policy_rwlock); |
f8687afe PM |
3479 | return rc; |
3480 | } | |
7420ed23 | 3481 | #endif /* CONFIG_NETLABEL */ |
cee74f47 EP |
3482 | |
3483 | /** | |
3484 | * security_read_policy - read the policy. | |
3485 | * @data: binary policy data | |
3486 | * @len: length of data in bytes | |
3487 | * | |
3488 | */ | |
6b697323 | 3489 | int security_read_policy(void **data, size_t *len) |
cee74f47 EP |
3490 | { |
3491 | int rc; | |
3492 | struct policy_file fp; | |
3493 | ||
3494 | if (!ss_initialized) | |
3495 | return -EINVAL; | |
3496 | ||
3497 | *len = security_policydb_len(); | |
3498 | ||
845ca30f | 3499 | *data = vmalloc_user(*len); |
cee74f47 EP |
3500 | if (!*data) |
3501 | return -ENOMEM; | |
3502 | ||
3503 | fp.data = *data; | |
3504 | fp.len = *len; | |
3505 | ||
3506 | read_lock(&policy_rwlock); | |
3507 | rc = policydb_write(&policydb, &fp); | |
3508 | read_unlock(&policy_rwlock); | |
3509 | ||
3510 | if (rc) | |
3511 | return rc; | |
3512 | ||
3513 | *len = (unsigned long)fp.data - (unsigned long)*data; | |
3514 | return 0; | |
3515 | ||
3516 | } |