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 | * |
7420ed23 VY |
16 | * Updated: Hewlett-Packard <paul.moore@hp.com> |
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 LT |
72 | |
73 | extern void selnl_notify_policyload(u32 seqno); | |
1da177e4 | 74 | |
3bb56b25 | 75 | int selinux_policycap_netpeer; |
b0c636b9 | 76 | int selinux_policycap_openperm; |
3bb56b25 | 77 | |
1da177e4 | 78 | static DEFINE_RWLOCK(policy_rwlock); |
1da177e4 LT |
79 | |
80 | static struct sidtab sidtab; | |
81 | struct policydb policydb; | |
5d55a345 | 82 | int ss_initialized; |
1da177e4 LT |
83 | |
84 | /* | |
85 | * The largest sequence number that has been used when | |
86 | * providing an access decision to the access vector cache. | |
87 | * The sequence number only changes when a policy change | |
88 | * occurs. | |
89 | */ | |
5d55a345 | 90 | static u32 latest_granting; |
1da177e4 LT |
91 | |
92 | /* Forward declaration. */ | |
93 | static int context_struct_to_string(struct context *context, char **scontext, | |
94 | u32 *scontext_len); | |
95 | ||
19439d05 SS |
96 | static void context_struct_compute_av(struct context *scontext, |
97 | struct context *tcontext, | |
98 | u16 tclass, | |
99 | struct av_decision *avd); | |
c6d3aaa4 SS |
100 | |
101 | struct selinux_mapping { | |
102 | u16 value; /* policy value */ | |
103 | unsigned num_perms; | |
104 | u32 perms[sizeof(u32) * 8]; | |
105 | }; | |
106 | ||
107 | static struct selinux_mapping *current_mapping; | |
108 | static u16 current_mapping_size; | |
109 | ||
110 | static int selinux_set_mapping(struct policydb *pol, | |
111 | struct security_class_mapping *map, | |
112 | struct selinux_mapping **out_map_p, | |
113 | u16 *out_map_size) | |
114 | { | |
115 | struct selinux_mapping *out_map = NULL; | |
116 | size_t size = sizeof(struct selinux_mapping); | |
117 | u16 i, j; | |
118 | unsigned k; | |
119 | bool print_unknown_handle = false; | |
120 | ||
121 | /* Find number of classes in the input mapping */ | |
122 | if (!map) | |
123 | return -EINVAL; | |
124 | i = 0; | |
125 | while (map[i].name) | |
126 | i++; | |
127 | ||
128 | /* Allocate space for the class records, plus one for class zero */ | |
129 | out_map = kcalloc(++i, size, GFP_ATOMIC); | |
130 | if (!out_map) | |
131 | return -ENOMEM; | |
132 | ||
133 | /* Store the raw class and permission values */ | |
134 | j = 0; | |
135 | while (map[j].name) { | |
136 | struct security_class_mapping *p_in = map + (j++); | |
137 | struct selinux_mapping *p_out = out_map + j; | |
138 | ||
139 | /* An empty class string skips ahead */ | |
140 | if (!strcmp(p_in->name, "")) { | |
141 | p_out->num_perms = 0; | |
142 | continue; | |
143 | } | |
144 | ||
145 | p_out->value = string_to_security_class(pol, p_in->name); | |
146 | if (!p_out->value) { | |
147 | printk(KERN_INFO | |
148 | "SELinux: Class %s not defined in policy.\n", | |
149 | p_in->name); | |
150 | if (pol->reject_unknown) | |
151 | goto err; | |
152 | p_out->num_perms = 0; | |
153 | print_unknown_handle = true; | |
154 | continue; | |
155 | } | |
156 | ||
157 | k = 0; | |
158 | while (p_in->perms && p_in->perms[k]) { | |
159 | /* An empty permission string skips ahead */ | |
160 | if (!*p_in->perms[k]) { | |
161 | k++; | |
162 | continue; | |
163 | } | |
164 | p_out->perms[k] = string_to_av_perm(pol, p_out->value, | |
165 | p_in->perms[k]); | |
166 | if (!p_out->perms[k]) { | |
167 | printk(KERN_INFO | |
168 | "SELinux: Permission %s in class %s not defined in policy.\n", | |
169 | p_in->perms[k], p_in->name); | |
170 | if (pol->reject_unknown) | |
171 | goto err; | |
172 | print_unknown_handle = true; | |
173 | } | |
174 | ||
175 | k++; | |
176 | } | |
177 | p_out->num_perms = k; | |
178 | } | |
179 | ||
180 | if (print_unknown_handle) | |
181 | printk(KERN_INFO "SELinux: the above unknown classes and permissions will be %s\n", | |
182 | pol->allow_unknown ? "allowed" : "denied"); | |
183 | ||
184 | *out_map_p = out_map; | |
185 | *out_map_size = i; | |
186 | return 0; | |
187 | err: | |
188 | kfree(out_map); | |
189 | return -EINVAL; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Get real, policy values from mapped values | |
194 | */ | |
195 | ||
196 | static u16 unmap_class(u16 tclass) | |
197 | { | |
198 | if (tclass < current_mapping_size) | |
199 | return current_mapping[tclass].value; | |
200 | ||
201 | return tclass; | |
202 | } | |
203 | ||
c6d3aaa4 SS |
204 | static void map_decision(u16 tclass, struct av_decision *avd, |
205 | int allow_unknown) | |
206 | { | |
207 | if (tclass < current_mapping_size) { | |
208 | unsigned i, n = current_mapping[tclass].num_perms; | |
209 | u32 result; | |
210 | ||
211 | for (i = 0, result = 0; i < n; i++) { | |
212 | if (avd->allowed & current_mapping[tclass].perms[i]) | |
213 | result |= 1<<i; | |
214 | if (allow_unknown && !current_mapping[tclass].perms[i]) | |
215 | result |= 1<<i; | |
216 | } | |
217 | avd->allowed = result; | |
218 | ||
219 | for (i = 0, result = 0; i < n; i++) | |
220 | if (avd->auditallow & current_mapping[tclass].perms[i]) | |
221 | result |= 1<<i; | |
222 | avd->auditallow = result; | |
223 | ||
224 | for (i = 0, result = 0; i < n; i++) { | |
225 | if (avd->auditdeny & current_mapping[tclass].perms[i]) | |
226 | result |= 1<<i; | |
227 | if (!allow_unknown && !current_mapping[tclass].perms[i]) | |
228 | result |= 1<<i; | |
229 | } | |
0bce9527 EP |
230 | /* |
231 | * In case the kernel has a bug and requests a permission | |
232 | * between num_perms and the maximum permission number, we | |
233 | * should audit that denial | |
234 | */ | |
235 | for (; i < (sizeof(u32)*8); i++) | |
236 | result |= 1<<i; | |
c6d3aaa4 SS |
237 | avd->auditdeny = result; |
238 | } | |
239 | } | |
240 | ||
0719aaf5 GT |
241 | int security_mls_enabled(void) |
242 | { | |
243 | return policydb.mls_enabled; | |
244 | } | |
c6d3aaa4 | 245 | |
1da177e4 LT |
246 | /* |
247 | * Return the boolean value of a constraint expression | |
248 | * when it is applied to the specified source and target | |
249 | * security contexts. | |
250 | * | |
251 | * xcontext is a special beast... It is used by the validatetrans rules | |
252 | * only. For these rules, scontext is the context before the transition, | |
253 | * tcontext is the context after the transition, and xcontext is the context | |
254 | * of the process performing the transition. All other callers of | |
255 | * constraint_expr_eval should pass in NULL for xcontext. | |
256 | */ | |
257 | static int constraint_expr_eval(struct context *scontext, | |
258 | struct context *tcontext, | |
259 | struct context *xcontext, | |
260 | struct constraint_expr *cexpr) | |
261 | { | |
262 | u32 val1, val2; | |
263 | struct context *c; | |
264 | struct role_datum *r1, *r2; | |
265 | struct mls_level *l1, *l2; | |
266 | struct constraint_expr *e; | |
267 | int s[CEXPR_MAXDEPTH]; | |
268 | int sp = -1; | |
269 | ||
270 | for (e = cexpr; e; e = e->next) { | |
271 | switch (e->expr_type) { | |
272 | case CEXPR_NOT: | |
273 | BUG_ON(sp < 0); | |
274 | s[sp] = !s[sp]; | |
275 | break; | |
276 | case CEXPR_AND: | |
277 | BUG_ON(sp < 1); | |
278 | sp--; | |
c1a7368a | 279 | s[sp] &= s[sp + 1]; |
1da177e4 LT |
280 | break; |
281 | case CEXPR_OR: | |
282 | BUG_ON(sp < 1); | |
283 | sp--; | |
c1a7368a | 284 | s[sp] |= s[sp + 1]; |
1da177e4 LT |
285 | break; |
286 | case CEXPR_ATTR: | |
c1a7368a | 287 | if (sp == (CEXPR_MAXDEPTH - 1)) |
1da177e4 LT |
288 | return 0; |
289 | switch (e->attr) { | |
290 | case CEXPR_USER: | |
291 | val1 = scontext->user; | |
292 | val2 = tcontext->user; | |
293 | break; | |
294 | case CEXPR_TYPE: | |
295 | val1 = scontext->type; | |
296 | val2 = tcontext->type; | |
297 | break; | |
298 | case CEXPR_ROLE: | |
299 | val1 = scontext->role; | |
300 | val2 = tcontext->role; | |
301 | r1 = policydb.role_val_to_struct[val1 - 1]; | |
302 | r2 = policydb.role_val_to_struct[val2 - 1]; | |
303 | switch (e->op) { | |
304 | case CEXPR_DOM: | |
305 | s[++sp] = ebitmap_get_bit(&r1->dominates, | |
306 | val2 - 1); | |
307 | continue; | |
308 | case CEXPR_DOMBY: | |
309 | s[++sp] = ebitmap_get_bit(&r2->dominates, | |
310 | val1 - 1); | |
311 | continue; | |
312 | case CEXPR_INCOMP: | |
5d55a345 EP |
313 | s[++sp] = (!ebitmap_get_bit(&r1->dominates, |
314 | val2 - 1) && | |
315 | !ebitmap_get_bit(&r2->dominates, | |
316 | val1 - 1)); | |
1da177e4 LT |
317 | continue; |
318 | default: | |
319 | break; | |
320 | } | |
321 | break; | |
322 | case CEXPR_L1L2: | |
323 | l1 = &(scontext->range.level[0]); | |
324 | l2 = &(tcontext->range.level[0]); | |
325 | goto mls_ops; | |
326 | case CEXPR_L1H2: | |
327 | l1 = &(scontext->range.level[0]); | |
328 | l2 = &(tcontext->range.level[1]); | |
329 | goto mls_ops; | |
330 | case CEXPR_H1L2: | |
331 | l1 = &(scontext->range.level[1]); | |
332 | l2 = &(tcontext->range.level[0]); | |
333 | goto mls_ops; | |
334 | case CEXPR_H1H2: | |
335 | l1 = &(scontext->range.level[1]); | |
336 | l2 = &(tcontext->range.level[1]); | |
337 | goto mls_ops; | |
338 | case CEXPR_L1H1: | |
339 | l1 = &(scontext->range.level[0]); | |
340 | l2 = &(scontext->range.level[1]); | |
341 | goto mls_ops; | |
342 | case CEXPR_L2H2: | |
343 | l1 = &(tcontext->range.level[0]); | |
344 | l2 = &(tcontext->range.level[1]); | |
345 | goto mls_ops; | |
346 | mls_ops: | |
347 | switch (e->op) { | |
348 | case CEXPR_EQ: | |
349 | s[++sp] = mls_level_eq(l1, l2); | |
350 | continue; | |
351 | case CEXPR_NEQ: | |
352 | s[++sp] = !mls_level_eq(l1, l2); | |
353 | continue; | |
354 | case CEXPR_DOM: | |
355 | s[++sp] = mls_level_dom(l1, l2); | |
356 | continue; | |
357 | case CEXPR_DOMBY: | |
358 | s[++sp] = mls_level_dom(l2, l1); | |
359 | continue; | |
360 | case CEXPR_INCOMP: | |
361 | s[++sp] = mls_level_incomp(l2, l1); | |
362 | continue; | |
363 | default: | |
364 | BUG(); | |
365 | return 0; | |
366 | } | |
367 | break; | |
368 | default: | |
369 | BUG(); | |
370 | return 0; | |
371 | } | |
372 | ||
373 | switch (e->op) { | |
374 | case CEXPR_EQ: | |
375 | s[++sp] = (val1 == val2); | |
376 | break; | |
377 | case CEXPR_NEQ: | |
378 | s[++sp] = (val1 != val2); | |
379 | break; | |
380 | default: | |
381 | BUG(); | |
382 | return 0; | |
383 | } | |
384 | break; | |
385 | case CEXPR_NAMES: | |
386 | if (sp == (CEXPR_MAXDEPTH-1)) | |
387 | return 0; | |
388 | c = scontext; | |
389 | if (e->attr & CEXPR_TARGET) | |
390 | c = tcontext; | |
391 | else if (e->attr & CEXPR_XTARGET) { | |
392 | c = xcontext; | |
393 | if (!c) { | |
394 | BUG(); | |
395 | return 0; | |
396 | } | |
397 | } | |
398 | if (e->attr & CEXPR_USER) | |
399 | val1 = c->user; | |
400 | else if (e->attr & CEXPR_ROLE) | |
401 | val1 = c->role; | |
402 | else if (e->attr & CEXPR_TYPE) | |
403 | val1 = c->type; | |
404 | else { | |
405 | BUG(); | |
406 | return 0; | |
407 | } | |
408 | ||
409 | switch (e->op) { | |
410 | case CEXPR_EQ: | |
411 | s[++sp] = ebitmap_get_bit(&e->names, val1 - 1); | |
412 | break; | |
413 | case CEXPR_NEQ: | |
414 | s[++sp] = !ebitmap_get_bit(&e->names, val1 - 1); | |
415 | break; | |
416 | default: | |
417 | BUG(); | |
418 | return 0; | |
419 | } | |
420 | break; | |
421 | default: | |
422 | BUG(); | |
423 | return 0; | |
424 | } | |
425 | } | |
426 | ||
427 | BUG_ON(sp != 0); | |
428 | return s[0]; | |
429 | } | |
430 | ||
44c2d9bd KK |
431 | /* |
432 | * security_dump_masked_av - dumps masked permissions during | |
433 | * security_compute_av due to RBAC, MLS/Constraint and Type bounds. | |
434 | */ | |
435 | static int dump_masked_av_helper(void *k, void *d, void *args) | |
436 | { | |
437 | struct perm_datum *pdatum = d; | |
438 | char **permission_names = args; | |
439 | ||
440 | BUG_ON(pdatum->value < 1 || pdatum->value > 32); | |
441 | ||
442 | permission_names[pdatum->value - 1] = (char *)k; | |
443 | ||
444 | return 0; | |
445 | } | |
446 | ||
447 | static void security_dump_masked_av(struct context *scontext, | |
448 | struct context *tcontext, | |
449 | u16 tclass, | |
450 | u32 permissions, | |
451 | const char *reason) | |
452 | { | |
453 | struct common_datum *common_dat; | |
454 | struct class_datum *tclass_dat; | |
455 | struct audit_buffer *ab; | |
456 | char *tclass_name; | |
457 | char *scontext_name = NULL; | |
458 | char *tcontext_name = NULL; | |
459 | char *permission_names[32]; | |
2da5d31b JM |
460 | int index; |
461 | u32 length; | |
44c2d9bd KK |
462 | bool need_comma = false; |
463 | ||
464 | if (!permissions) | |
465 | return; | |
466 | ||
ac76c05b | 467 | tclass_name = sym_name(&policydb, SYM_CLASSES, tclass - 1); |
44c2d9bd KK |
468 | tclass_dat = policydb.class_val_to_struct[tclass - 1]; |
469 | common_dat = tclass_dat->comdatum; | |
470 | ||
471 | /* init permission_names */ | |
472 | if (common_dat && | |
473 | hashtab_map(common_dat->permissions.table, | |
474 | dump_masked_av_helper, permission_names) < 0) | |
475 | goto out; | |
476 | ||
477 | if (hashtab_map(tclass_dat->permissions.table, | |
478 | dump_masked_av_helper, permission_names) < 0) | |
479 | goto out; | |
480 | ||
481 | /* get scontext/tcontext in text form */ | |
482 | if (context_struct_to_string(scontext, | |
483 | &scontext_name, &length) < 0) | |
484 | goto out; | |
485 | ||
486 | if (context_struct_to_string(tcontext, | |
487 | &tcontext_name, &length) < 0) | |
488 | goto out; | |
489 | ||
490 | /* audit a message */ | |
491 | ab = audit_log_start(current->audit_context, | |
492 | GFP_ATOMIC, AUDIT_SELINUX_ERR); | |
493 | if (!ab) | |
494 | goto out; | |
495 | ||
496 | audit_log_format(ab, "op=security_compute_av reason=%s " | |
497 | "scontext=%s tcontext=%s tclass=%s perms=", | |
498 | reason, scontext_name, tcontext_name, tclass_name); | |
499 | ||
500 | for (index = 0; index < 32; index++) { | |
501 | u32 mask = (1 << index); | |
502 | ||
503 | if ((mask & permissions) == 0) | |
504 | continue; | |
505 | ||
506 | audit_log_format(ab, "%s%s", | |
507 | need_comma ? "," : "", | |
508 | permission_names[index] | |
509 | ? permission_names[index] : "????"); | |
510 | need_comma = true; | |
511 | } | |
512 | audit_log_end(ab); | |
513 | out: | |
514 | /* release scontext/tcontext */ | |
515 | kfree(tcontext_name); | |
516 | kfree(scontext_name); | |
517 | ||
518 | return; | |
519 | } | |
520 | ||
d9250dea KK |
521 | /* |
522 | * security_boundary_permission - drops violated permissions | |
523 | * on boundary constraint. | |
524 | */ | |
525 | static void type_attribute_bounds_av(struct context *scontext, | |
526 | struct context *tcontext, | |
527 | u16 tclass, | |
d9250dea KK |
528 | struct av_decision *avd) |
529 | { | |
2ae3ba39 KK |
530 | struct context lo_scontext; |
531 | struct context lo_tcontext; | |
532 | struct av_decision lo_avd; | |
23bdecb0 EP |
533 | struct type_datum *source; |
534 | struct type_datum *target; | |
2ae3ba39 | 535 | u32 masked = 0; |
d9250dea | 536 | |
23bdecb0 EP |
537 | source = flex_array_get_ptr(policydb.type_val_to_struct_array, |
538 | scontext->type - 1); | |
539 | BUG_ON(!source); | |
540 | ||
541 | target = flex_array_get_ptr(policydb.type_val_to_struct_array, | |
542 | tcontext->type - 1); | |
543 | BUG_ON(!target); | |
544 | ||
d9250dea KK |
545 | if (source->bounds) { |
546 | memset(&lo_avd, 0, sizeof(lo_avd)); | |
547 | ||
548 | memcpy(&lo_scontext, scontext, sizeof(lo_scontext)); | |
549 | lo_scontext.type = source->bounds; | |
550 | ||
551 | context_struct_compute_av(&lo_scontext, | |
552 | tcontext, | |
553 | tclass, | |
d9250dea KK |
554 | &lo_avd); |
555 | if ((lo_avd.allowed & avd->allowed) == avd->allowed) | |
556 | return; /* no masked permission */ | |
557 | masked = ~lo_avd.allowed & avd->allowed; | |
2ae3ba39 KK |
558 | } |
559 | ||
560 | if (target->bounds) { | |
561 | memset(&lo_avd, 0, sizeof(lo_avd)); | |
562 | ||
563 | memcpy(&lo_tcontext, tcontext, sizeof(lo_tcontext)); | |
564 | lo_tcontext.type = target->bounds; | |
565 | ||
566 | context_struct_compute_av(scontext, | |
567 | &lo_tcontext, | |
568 | tclass, | |
569 | &lo_avd); | |
570 | if ((lo_avd.allowed & avd->allowed) == avd->allowed) | |
571 | return; /* no masked permission */ | |
572 | masked = ~lo_avd.allowed & avd->allowed; | |
573 | } | |
574 | ||
575 | if (source->bounds && target->bounds) { | |
576 | memset(&lo_avd, 0, sizeof(lo_avd)); | |
577 | /* | |
578 | * lo_scontext and lo_tcontext are already | |
579 | * set up. | |
580 | */ | |
581 | ||
582 | context_struct_compute_av(&lo_scontext, | |
583 | &lo_tcontext, | |
584 | tclass, | |
585 | &lo_avd); | |
586 | if ((lo_avd.allowed & avd->allowed) == avd->allowed) | |
587 | return; /* no masked permission */ | |
588 | masked = ~lo_avd.allowed & avd->allowed; | |
589 | } | |
d9250dea | 590 | |
2ae3ba39 | 591 | if (masked) { |
d9250dea KK |
592 | /* mask violated permissions */ |
593 | avd->allowed &= ~masked; | |
594 | ||
44c2d9bd KK |
595 | /* audit masked permissions */ |
596 | security_dump_masked_av(scontext, tcontext, | |
597 | tclass, masked, "bounds"); | |
d9250dea KK |
598 | } |
599 | } | |
600 | ||
1da177e4 LT |
601 | /* |
602 | * Compute access vectors based on a context structure pair for | |
603 | * the permissions in a particular class. | |
604 | */ | |
19439d05 SS |
605 | static void context_struct_compute_av(struct context *scontext, |
606 | struct context *tcontext, | |
607 | u16 tclass, | |
608 | struct av_decision *avd) | |
1da177e4 LT |
609 | { |
610 | struct constraint_node *constraint; | |
611 | struct role_allow *ra; | |
612 | struct avtab_key avkey; | |
782ebb99 | 613 | struct avtab_node *node; |
1da177e4 | 614 | struct class_datum *tclass_datum; |
782ebb99 SS |
615 | struct ebitmap *sattr, *tattr; |
616 | struct ebitmap_node *snode, *tnode; | |
617 | unsigned int i, j; | |
1da177e4 | 618 | |
1da177e4 | 619 | avd->allowed = 0; |
1da177e4 LT |
620 | avd->auditallow = 0; |
621 | avd->auditdeny = 0xffffffff; | |
1da177e4 | 622 | |
c6d3aaa4 SS |
623 | if (unlikely(!tclass || tclass > policydb.p_classes.nprim)) { |
624 | if (printk_ratelimit()) | |
625 | printk(KERN_WARNING "SELinux: Invalid class %hu\n", tclass); | |
19439d05 | 626 | return; |
c6d3aaa4 | 627 | } |
3f12070e EP |
628 | |
629 | tclass_datum = policydb.class_val_to_struct[tclass - 1]; | |
630 | ||
1da177e4 LT |
631 | /* |
632 | * If a specific type enforcement rule was defined for | |
633 | * this permission check, then use it. | |
634 | */ | |
1da177e4 | 635 | avkey.target_class = tclass; |
782ebb99 | 636 | avkey.specified = AVTAB_AV; |
6371dcd3 EP |
637 | sattr = flex_array_get(policydb.type_attr_map_array, scontext->type - 1); |
638 | BUG_ON(!sattr); | |
639 | tattr = flex_array_get(policydb.type_attr_map_array, tcontext->type - 1); | |
640 | BUG_ON(!tattr); | |
9fe79ad1 KK |
641 | ebitmap_for_each_positive_bit(sattr, snode, i) { |
642 | ebitmap_for_each_positive_bit(tattr, tnode, j) { | |
782ebb99 SS |
643 | avkey.source_type = i + 1; |
644 | avkey.target_type = j + 1; | |
645 | for (node = avtab_search_node(&policydb.te_avtab, &avkey); | |
dbc74c65 | 646 | node; |
782ebb99 SS |
647 | node = avtab_search_node_next(node, avkey.specified)) { |
648 | if (node->key.specified == AVTAB_ALLOWED) | |
649 | avd->allowed |= node->datum.data; | |
650 | else if (node->key.specified == AVTAB_AUDITALLOW) | |
651 | avd->auditallow |= node->datum.data; | |
652 | else if (node->key.specified == AVTAB_AUDITDENY) | |
653 | avd->auditdeny &= node->datum.data; | |
654 | } | |
1da177e4 | 655 | |
782ebb99 SS |
656 | /* Check conditional av table for additional permissions */ |
657 | cond_compute_av(&policydb.te_cond_avtab, &avkey, avd); | |
658 | ||
659 | } | |
660 | } | |
1da177e4 LT |
661 | |
662 | /* | |
663 | * Remove any permissions prohibited by a constraint (this includes | |
664 | * the MLS policy). | |
665 | */ | |
666 | constraint = tclass_datum->constraints; | |
667 | while (constraint) { | |
668 | if ((constraint->permissions & (avd->allowed)) && | |
669 | !constraint_expr_eval(scontext, tcontext, NULL, | |
670 | constraint->expr)) { | |
caabbdc0 | 671 | avd->allowed &= ~(constraint->permissions); |
1da177e4 LT |
672 | } |
673 | constraint = constraint->next; | |
674 | } | |
675 | ||
676 | /* | |
677 | * If checking process transition permission and the | |
678 | * role is changing, then check the (current_role, new_role) | |
679 | * pair. | |
680 | */ | |
c6d3aaa4 SS |
681 | if (tclass == policydb.process_class && |
682 | (avd->allowed & policydb.process_trans_perms) && | |
1da177e4 LT |
683 | scontext->role != tcontext->role) { |
684 | for (ra = policydb.role_allow; ra; ra = ra->next) { | |
685 | if (scontext->role == ra->role && | |
686 | tcontext->role == ra->new_role) | |
687 | break; | |
688 | } | |
689 | if (!ra) | |
c6d3aaa4 | 690 | avd->allowed &= ~policydb.process_trans_perms; |
1da177e4 LT |
691 | } |
692 | ||
d9250dea KK |
693 | /* |
694 | * If the given source and target types have boundary | |
695 | * constraint, lazy checks have to mask any violated | |
696 | * permission and notice it to userspace via audit. | |
697 | */ | |
698 | type_attribute_bounds_av(scontext, tcontext, | |
19439d05 | 699 | tclass, avd); |
1da177e4 LT |
700 | } |
701 | ||
702 | static int security_validtrans_handle_fail(struct context *ocontext, | |
5d55a345 EP |
703 | struct context *ncontext, |
704 | struct context *tcontext, | |
705 | u16 tclass) | |
1da177e4 LT |
706 | { |
707 | char *o = NULL, *n = NULL, *t = NULL; | |
708 | u32 olen, nlen, tlen; | |
709 | ||
4b02b524 | 710 | if (context_struct_to_string(ocontext, &o, &olen)) |
1da177e4 | 711 | goto out; |
4b02b524 | 712 | if (context_struct_to_string(ncontext, &n, &nlen)) |
1da177e4 | 713 | goto out; |
4b02b524 | 714 | if (context_struct_to_string(tcontext, &t, &tlen)) |
1da177e4 | 715 | goto out; |
9ad9ad38 | 716 | audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
5d55a345 EP |
717 | "security_validate_transition: denied for" |
718 | " oldcontext=%s newcontext=%s taskcontext=%s tclass=%s", | |
ac76c05b | 719 | o, n, t, sym_name(&policydb, SYM_CLASSES, tclass-1)); |
1da177e4 LT |
720 | out: |
721 | kfree(o); | |
722 | kfree(n); | |
723 | kfree(t); | |
724 | ||
725 | if (!selinux_enforcing) | |
726 | return 0; | |
727 | return -EPERM; | |
728 | } | |
729 | ||
730 | int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid, | |
c6d3aaa4 | 731 | u16 orig_tclass) |
1da177e4 LT |
732 | { |
733 | struct context *ocontext; | |
734 | struct context *ncontext; | |
735 | struct context *tcontext; | |
736 | struct class_datum *tclass_datum; | |
737 | struct constraint_node *constraint; | |
c6d3aaa4 | 738 | u16 tclass; |
1da177e4 LT |
739 | int rc = 0; |
740 | ||
741 | if (!ss_initialized) | |
742 | return 0; | |
743 | ||
0804d113 | 744 | read_lock(&policy_rwlock); |
1da177e4 | 745 | |
c6d3aaa4 SS |
746 | tclass = unmap_class(orig_tclass); |
747 | ||
1da177e4 | 748 | if (!tclass || tclass > policydb.p_classes.nprim) { |
744ba35e EP |
749 | printk(KERN_ERR "SELinux: %s: unrecognized class %d\n", |
750 | __func__, tclass); | |
1da177e4 LT |
751 | rc = -EINVAL; |
752 | goto out; | |
753 | } | |
754 | tclass_datum = policydb.class_val_to_struct[tclass - 1]; | |
755 | ||
756 | ocontext = sidtab_search(&sidtab, oldsid); | |
757 | if (!ocontext) { | |
744ba35e EP |
758 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
759 | __func__, oldsid); | |
1da177e4 LT |
760 | rc = -EINVAL; |
761 | goto out; | |
762 | } | |
763 | ||
764 | ncontext = sidtab_search(&sidtab, newsid); | |
765 | if (!ncontext) { | |
744ba35e EP |
766 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
767 | __func__, newsid); | |
1da177e4 LT |
768 | rc = -EINVAL; |
769 | goto out; | |
770 | } | |
771 | ||
772 | tcontext = sidtab_search(&sidtab, tasksid); | |
773 | if (!tcontext) { | |
744ba35e EP |
774 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
775 | __func__, tasksid); | |
1da177e4 LT |
776 | rc = -EINVAL; |
777 | goto out; | |
778 | } | |
779 | ||
780 | constraint = tclass_datum->validatetrans; | |
781 | while (constraint) { | |
782 | if (!constraint_expr_eval(ocontext, ncontext, tcontext, | |
5d55a345 | 783 | constraint->expr)) { |
1da177e4 | 784 | rc = security_validtrans_handle_fail(ocontext, ncontext, |
5d55a345 | 785 | tcontext, tclass); |
1da177e4 LT |
786 | goto out; |
787 | } | |
788 | constraint = constraint->next; | |
789 | } | |
790 | ||
791 | out: | |
0804d113 | 792 | read_unlock(&policy_rwlock); |
1da177e4 LT |
793 | return rc; |
794 | } | |
795 | ||
d9250dea KK |
796 | /* |
797 | * security_bounded_transition - check whether the given | |
798 | * transition is directed to bounded, or not. | |
799 | * It returns 0, if @newsid is bounded by @oldsid. | |
800 | * Otherwise, it returns error code. | |
801 | * | |
802 | * @oldsid : current security identifier | |
803 | * @newsid : destinated security identifier | |
804 | */ | |
805 | int security_bounded_transition(u32 old_sid, u32 new_sid) | |
806 | { | |
807 | struct context *old_context, *new_context; | |
808 | struct type_datum *type; | |
809 | int index; | |
4b02b524 | 810 | int rc; |
d9250dea KK |
811 | |
812 | read_lock(&policy_rwlock); | |
813 | ||
4b02b524 | 814 | rc = -EINVAL; |
d9250dea KK |
815 | old_context = sidtab_search(&sidtab, old_sid); |
816 | if (!old_context) { | |
817 | printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n", | |
818 | __func__, old_sid); | |
819 | goto out; | |
820 | } | |
821 | ||
4b02b524 | 822 | rc = -EINVAL; |
d9250dea KK |
823 | new_context = sidtab_search(&sidtab, new_sid); |
824 | if (!new_context) { | |
825 | printk(KERN_ERR "SELinux: %s: unrecognized SID %u\n", | |
826 | __func__, new_sid); | |
827 | goto out; | |
828 | } | |
829 | ||
4b02b524 | 830 | rc = 0; |
af901ca1 | 831 | /* type/domain unchanged */ |
4b02b524 | 832 | if (old_context->type == new_context->type) |
d9250dea | 833 | goto out; |
d9250dea KK |
834 | |
835 | index = new_context->type; | |
836 | while (true) { | |
23bdecb0 EP |
837 | type = flex_array_get_ptr(policydb.type_val_to_struct_array, |
838 | index - 1); | |
d9250dea KK |
839 | BUG_ON(!type); |
840 | ||
841 | /* not bounded anymore */ | |
4b02b524 EP |
842 | rc = -EPERM; |
843 | if (!type->bounds) | |
d9250dea | 844 | break; |
d9250dea KK |
845 | |
846 | /* @newsid is bounded by @oldsid */ | |
4b02b524 EP |
847 | rc = 0; |
848 | if (type->bounds == old_context->type) | |
d9250dea | 849 | break; |
4b02b524 | 850 | |
d9250dea KK |
851 | index = type->bounds; |
852 | } | |
44c2d9bd KK |
853 | |
854 | if (rc) { | |
855 | char *old_name = NULL; | |
856 | char *new_name = NULL; | |
2da5d31b | 857 | u32 length; |
44c2d9bd KK |
858 | |
859 | if (!context_struct_to_string(old_context, | |
860 | &old_name, &length) && | |
861 | !context_struct_to_string(new_context, | |
862 | &new_name, &length)) { | |
863 | audit_log(current->audit_context, | |
864 | GFP_ATOMIC, AUDIT_SELINUX_ERR, | |
865 | "op=security_bounded_transition " | |
866 | "result=denied " | |
867 | "oldcontext=%s newcontext=%s", | |
868 | old_name, new_name); | |
869 | } | |
870 | kfree(new_name); | |
871 | kfree(old_name); | |
872 | } | |
d9250dea KK |
873 | out: |
874 | read_unlock(&policy_rwlock); | |
875 | ||
876 | return rc; | |
877 | } | |
878 | ||
19439d05 | 879 | static void avd_init(struct av_decision *avd) |
c6d3aaa4 | 880 | { |
19439d05 SS |
881 | avd->allowed = 0; |
882 | avd->auditallow = 0; | |
883 | avd->auditdeny = 0xffffffff; | |
884 | avd->seqno = latest_granting; | |
885 | avd->flags = 0; | |
c6d3aaa4 SS |
886 | } |
887 | ||
19439d05 | 888 | |
1da177e4 LT |
889 | /** |
890 | * security_compute_av - Compute access vector decisions. | |
891 | * @ssid: source security identifier | |
892 | * @tsid: target security identifier | |
893 | * @tclass: target security class | |
1da177e4 LT |
894 | * @avd: access vector decisions |
895 | * | |
896 | * Compute a set of access vector decisions based on the | |
897 | * SID pair (@ssid, @tsid) for the permissions in @tclass. | |
1da177e4 | 898 | */ |
19439d05 SS |
899 | void security_compute_av(u32 ssid, |
900 | u32 tsid, | |
901 | u16 orig_tclass, | |
902 | struct av_decision *avd) | |
1da177e4 | 903 | { |
c6d3aaa4 | 904 | u16 tclass; |
19439d05 | 905 | struct context *scontext = NULL, *tcontext = NULL; |
c6d3aaa4 | 906 | |
b7f3008a | 907 | read_lock(&policy_rwlock); |
19439d05 | 908 | avd_init(avd); |
c6d3aaa4 SS |
909 | if (!ss_initialized) |
910 | goto allow; | |
911 | ||
19439d05 SS |
912 | scontext = sidtab_search(&sidtab, ssid); |
913 | if (!scontext) { | |
914 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", | |
915 | __func__, ssid); | |
916 | goto out; | |
917 | } | |
918 | ||
919 | /* permissive domain? */ | |
920 | if (ebitmap_get_bit(&policydb.permissive_map, scontext->type)) | |
921 | avd->flags |= AVD_FLAGS_PERMISSIVE; | |
922 | ||
923 | tcontext = sidtab_search(&sidtab, tsid); | |
924 | if (!tcontext) { | |
925 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", | |
926 | __func__, tsid); | |
927 | goto out; | |
928 | } | |
929 | ||
c6d3aaa4 SS |
930 | tclass = unmap_class(orig_tclass); |
931 | if (unlikely(orig_tclass && !tclass)) { | |
932 | if (policydb.allow_unknown) | |
933 | goto allow; | |
b7f3008a | 934 | goto out; |
c6d3aaa4 | 935 | } |
19439d05 | 936 | context_struct_compute_av(scontext, tcontext, tclass, avd); |
c6d3aaa4 | 937 | map_decision(orig_tclass, avd, policydb.allow_unknown); |
b7f3008a | 938 | out: |
c6d3aaa4 | 939 | read_unlock(&policy_rwlock); |
19439d05 | 940 | return; |
c6d3aaa4 SS |
941 | allow: |
942 | avd->allowed = 0xffffffff; | |
b7f3008a | 943 | goto out; |
c6d3aaa4 SS |
944 | } |
945 | ||
19439d05 SS |
946 | void security_compute_av_user(u32 ssid, |
947 | u32 tsid, | |
948 | u16 tclass, | |
949 | struct av_decision *avd) | |
c6d3aaa4 | 950 | { |
19439d05 | 951 | struct context *scontext = NULL, *tcontext = NULL; |
1da177e4 | 952 | |
19439d05 SS |
953 | read_lock(&policy_rwlock); |
954 | avd_init(avd); | |
955 | if (!ss_initialized) | |
956 | goto allow; | |
957 | ||
958 | scontext = sidtab_search(&sidtab, ssid); | |
959 | if (!scontext) { | |
960 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", | |
961 | __func__, ssid); | |
962 | goto out; | |
1da177e4 LT |
963 | } |
964 | ||
19439d05 SS |
965 | /* permissive domain? */ |
966 | if (ebitmap_get_bit(&policydb.permissive_map, scontext->type)) | |
967 | avd->flags |= AVD_FLAGS_PERMISSIVE; | |
968 | ||
969 | tcontext = sidtab_search(&sidtab, tsid); | |
970 | if (!tcontext) { | |
971 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", | |
972 | __func__, tsid); | |
973 | goto out; | |
974 | } | |
975 | ||
976 | if (unlikely(!tclass)) { | |
977 | if (policydb.allow_unknown) | |
978 | goto allow; | |
979 | goto out; | |
980 | } | |
981 | ||
982 | context_struct_compute_av(scontext, tcontext, tclass, avd); | |
983 | out: | |
0804d113 | 984 | read_unlock(&policy_rwlock); |
19439d05 SS |
985 | return; |
986 | allow: | |
987 | avd->allowed = 0xffffffff; | |
988 | goto out; | |
1da177e4 LT |
989 | } |
990 | ||
991 | /* | |
992 | * Write the security context string representation of | |
993 | * the context structure `context' into a dynamically | |
994 | * allocated string of the correct size. Set `*scontext' | |
995 | * to point to this string and set `*scontext_len' to | |
996 | * the length of the string. | |
997 | */ | |
998 | static int context_struct_to_string(struct context *context, char **scontext, u32 *scontext_len) | |
999 | { | |
1000 | char *scontextp; | |
1001 | ||
d5630b9d EP |
1002 | if (scontext) |
1003 | *scontext = NULL; | |
1da177e4 LT |
1004 | *scontext_len = 0; |
1005 | ||
12b29f34 SS |
1006 | if (context->len) { |
1007 | *scontext_len = context->len; | |
1008 | *scontext = kstrdup(context->str, GFP_ATOMIC); | |
1009 | if (!(*scontext)) | |
1010 | return -ENOMEM; | |
1011 | return 0; | |
1012 | } | |
1013 | ||
1da177e4 | 1014 | /* Compute the size of the context. */ |
ac76c05b EP |
1015 | *scontext_len += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + 1; |
1016 | *scontext_len += strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + 1; | |
1017 | *scontext_len += strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)) + 1; | |
1da177e4 LT |
1018 | *scontext_len += mls_compute_context_len(context); |
1019 | ||
d5630b9d EP |
1020 | if (!scontext) |
1021 | return 0; | |
1022 | ||
1da177e4 LT |
1023 | /* Allocate space for the context; caller must free this space. */ |
1024 | scontextp = kmalloc(*scontext_len, GFP_ATOMIC); | |
5d55a345 | 1025 | if (!scontextp) |
1da177e4 | 1026 | return -ENOMEM; |
1da177e4 LT |
1027 | *scontext = scontextp; |
1028 | ||
1029 | /* | |
1030 | * Copy the user name, role name and type name into the context. | |
1031 | */ | |
1032 | sprintf(scontextp, "%s:%s:%s", | |
ac76c05b EP |
1033 | sym_name(&policydb, SYM_USERS, context->user - 1), |
1034 | sym_name(&policydb, SYM_ROLES, context->role - 1), | |
1035 | sym_name(&policydb, SYM_TYPES, context->type - 1)); | |
1036 | scontextp += strlen(sym_name(&policydb, SYM_USERS, context->user - 1)) + | |
1037 | 1 + strlen(sym_name(&policydb, SYM_ROLES, context->role - 1)) + | |
1038 | 1 + strlen(sym_name(&policydb, SYM_TYPES, context->type - 1)); | |
1da177e4 LT |
1039 | |
1040 | mls_sid_to_context(context, &scontextp); | |
1041 | ||
1042 | *scontextp = 0; | |
1043 | ||
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | #include "initial_sid_to_string.h" | |
1048 | ||
f0ee2e46 JC |
1049 | const char *security_get_initial_sid_context(u32 sid) |
1050 | { | |
1051 | if (unlikely(sid > SECINITSID_NUM)) | |
1052 | return NULL; | |
1053 | return initial_sid_to_string[sid]; | |
1054 | } | |
1055 | ||
12b29f34 SS |
1056 | static int security_sid_to_context_core(u32 sid, char **scontext, |
1057 | u32 *scontext_len, int force) | |
1da177e4 LT |
1058 | { |
1059 | struct context *context; | |
1060 | int rc = 0; | |
1061 | ||
d5630b9d EP |
1062 | if (scontext) |
1063 | *scontext = NULL; | |
4f4acf3a SS |
1064 | *scontext_len = 0; |
1065 | ||
1da177e4 LT |
1066 | if (!ss_initialized) { |
1067 | if (sid <= SECINITSID_NUM) { | |
1068 | char *scontextp; | |
1069 | ||
1070 | *scontext_len = strlen(initial_sid_to_string[sid]) + 1; | |
d5630b9d EP |
1071 | if (!scontext) |
1072 | goto out; | |
5d55a345 | 1073 | scontextp = kmalloc(*scontext_len, GFP_ATOMIC); |
0cccca06 SH |
1074 | if (!scontextp) { |
1075 | rc = -ENOMEM; | |
1076 | goto out; | |
1077 | } | |
1da177e4 LT |
1078 | strcpy(scontextp, initial_sid_to_string[sid]); |
1079 | *scontext = scontextp; | |
1080 | goto out; | |
1081 | } | |
744ba35e EP |
1082 | printk(KERN_ERR "SELinux: %s: called before initial " |
1083 | "load_policy on unknown SID %d\n", __func__, sid); | |
1da177e4 LT |
1084 | rc = -EINVAL; |
1085 | goto out; | |
1086 | } | |
0804d113 | 1087 | read_lock(&policy_rwlock); |
12b29f34 SS |
1088 | if (force) |
1089 | context = sidtab_search_force(&sidtab, sid); | |
1090 | else | |
1091 | context = sidtab_search(&sidtab, sid); | |
1da177e4 | 1092 | if (!context) { |
744ba35e EP |
1093 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
1094 | __func__, sid); | |
1da177e4 LT |
1095 | rc = -EINVAL; |
1096 | goto out_unlock; | |
1097 | } | |
1098 | rc = context_struct_to_string(context, scontext, scontext_len); | |
1099 | out_unlock: | |
0804d113 | 1100 | read_unlock(&policy_rwlock); |
1da177e4 LT |
1101 | out: |
1102 | return rc; | |
1103 | ||
1104 | } | |
1105 | ||
12b29f34 SS |
1106 | /** |
1107 | * security_sid_to_context - Obtain a context for a given SID. | |
1108 | * @sid: security identifier, SID | |
1109 | * @scontext: security context | |
1110 | * @scontext_len: length in bytes | |
1111 | * | |
1112 | * Write the string representation of the context associated with @sid | |
1113 | * into a dynamically allocated string of the correct size. Set @scontext | |
1114 | * to point to this string and set @scontext_len to the length of the string. | |
1115 | */ | |
1116 | int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len) | |
1da177e4 | 1117 | { |
12b29f34 SS |
1118 | return security_sid_to_context_core(sid, scontext, scontext_len, 0); |
1119 | } | |
1120 | ||
1121 | int security_sid_to_context_force(u32 sid, char **scontext, u32 *scontext_len) | |
1122 | { | |
1123 | return security_sid_to_context_core(sid, scontext, scontext_len, 1); | |
1124 | } | |
1125 | ||
9a59daa0 SS |
1126 | /* |
1127 | * Caveat: Mutates scontext. | |
1128 | */ | |
12b29f34 SS |
1129 | static int string_to_context_struct(struct policydb *pol, |
1130 | struct sidtab *sidtabp, | |
9a59daa0 | 1131 | char *scontext, |
12b29f34 SS |
1132 | u32 scontext_len, |
1133 | struct context *ctx, | |
9a59daa0 | 1134 | u32 def_sid) |
12b29f34 | 1135 | { |
1da177e4 LT |
1136 | struct role_datum *role; |
1137 | struct type_datum *typdatum; | |
1138 | struct user_datum *usrdatum; | |
1139 | char *scontextp, *p, oldc; | |
1140 | int rc = 0; | |
1141 | ||
12b29f34 | 1142 | context_init(ctx); |
1da177e4 | 1143 | |
1da177e4 LT |
1144 | /* Parse the security context. */ |
1145 | ||
1146 | rc = -EINVAL; | |
9a59daa0 | 1147 | scontextp = (char *) scontext; |
1da177e4 LT |
1148 | |
1149 | /* Extract the user. */ | |
1150 | p = scontextp; | |
1151 | while (*p && *p != ':') | |
1152 | p++; | |
1153 | ||
1154 | if (*p == 0) | |
12b29f34 | 1155 | goto out; |
1da177e4 LT |
1156 | |
1157 | *p++ = 0; | |
1158 | ||
12b29f34 | 1159 | usrdatum = hashtab_search(pol->p_users.table, scontextp); |
1da177e4 | 1160 | if (!usrdatum) |
12b29f34 | 1161 | goto out; |
1da177e4 | 1162 | |
12b29f34 | 1163 | ctx->user = usrdatum->value; |
1da177e4 LT |
1164 | |
1165 | /* Extract role. */ | |
1166 | scontextp = p; | |
1167 | while (*p && *p != ':') | |
1168 | p++; | |
1169 | ||
1170 | if (*p == 0) | |
12b29f34 | 1171 | goto out; |
1da177e4 LT |
1172 | |
1173 | *p++ = 0; | |
1174 | ||
12b29f34 | 1175 | role = hashtab_search(pol->p_roles.table, scontextp); |
1da177e4 | 1176 | if (!role) |
12b29f34 SS |
1177 | goto out; |
1178 | ctx->role = role->value; | |
1da177e4 LT |
1179 | |
1180 | /* Extract type. */ | |
1181 | scontextp = p; | |
1182 | while (*p && *p != ':') | |
1183 | p++; | |
1184 | oldc = *p; | |
1185 | *p++ = 0; | |
1186 | ||
12b29f34 | 1187 | typdatum = hashtab_search(pol->p_types.table, scontextp); |
d9250dea | 1188 | if (!typdatum || typdatum->attribute) |
12b29f34 | 1189 | goto out; |
1da177e4 | 1190 | |
12b29f34 | 1191 | ctx->type = typdatum->value; |
1da177e4 | 1192 | |
12b29f34 | 1193 | rc = mls_context_to_sid(pol, oldc, &p, ctx, sidtabp, def_sid); |
1da177e4 | 1194 | if (rc) |
12b29f34 | 1195 | goto out; |
1da177e4 | 1196 | |
4b02b524 EP |
1197 | rc = -EINVAL; |
1198 | if ((p - scontext) < scontext_len) | |
12b29f34 | 1199 | goto out; |
1da177e4 LT |
1200 | |
1201 | /* Check the validity of the new context. */ | |
4b02b524 | 1202 | if (!policydb_context_isvalid(pol, ctx)) |
12b29f34 | 1203 | goto out; |
12b29f34 SS |
1204 | rc = 0; |
1205 | out: | |
8e531af9 EP |
1206 | if (rc) |
1207 | context_destroy(ctx); | |
12b29f34 SS |
1208 | return rc; |
1209 | } | |
1210 | ||
1211 | static int security_context_to_sid_core(const char *scontext, u32 scontext_len, | |
1212 | u32 *sid, u32 def_sid, gfp_t gfp_flags, | |
1213 | int force) | |
1214 | { | |
9a59daa0 | 1215 | char *scontext2, *str = NULL; |
12b29f34 SS |
1216 | struct context context; |
1217 | int rc = 0; | |
1218 | ||
1219 | if (!ss_initialized) { | |
1220 | int i; | |
1221 | ||
1222 | for (i = 1; i < SECINITSID_NUM; i++) { | |
1223 | if (!strcmp(initial_sid_to_string[i], scontext)) { | |
1224 | *sid = i; | |
9a59daa0 | 1225 | return 0; |
12b29f34 SS |
1226 | } |
1227 | } | |
1228 | *sid = SECINITSID_KERNEL; | |
9a59daa0 | 1229 | return 0; |
12b29f34 SS |
1230 | } |
1231 | *sid = SECSID_NULL; | |
1232 | ||
9a59daa0 | 1233 | /* Copy the string so that we can modify the copy as we parse it. */ |
c1a7368a | 1234 | scontext2 = kmalloc(scontext_len + 1, gfp_flags); |
9a59daa0 SS |
1235 | if (!scontext2) |
1236 | return -ENOMEM; | |
1237 | memcpy(scontext2, scontext, scontext_len); | |
1238 | scontext2[scontext_len] = 0; | |
1239 | ||
1240 | if (force) { | |
1241 | /* Save another copy for storing in uninterpreted form */ | |
4b02b524 | 1242 | rc = -ENOMEM; |
9a59daa0 | 1243 | str = kstrdup(scontext2, gfp_flags); |
4b02b524 EP |
1244 | if (!str) |
1245 | goto out; | |
9a59daa0 SS |
1246 | } |
1247 | ||
0804d113 | 1248 | read_lock(&policy_rwlock); |
4b02b524 EP |
1249 | rc = string_to_context_struct(&policydb, &sidtab, scontext2, |
1250 | scontext_len, &context, def_sid); | |
12b29f34 | 1251 | if (rc == -EINVAL && force) { |
9a59daa0 | 1252 | context.str = str; |
12b29f34 | 1253 | context.len = scontext_len; |
9a59daa0 | 1254 | str = NULL; |
12b29f34 | 1255 | } else if (rc) |
4b02b524 | 1256 | goto out_unlock; |
12b29f34 | 1257 | rc = sidtab_context_to_sid(&sidtab, &context, sid); |
8e531af9 | 1258 | context_destroy(&context); |
4b02b524 | 1259 | out_unlock: |
0804d113 | 1260 | read_unlock(&policy_rwlock); |
4b02b524 | 1261 | out: |
9a59daa0 SS |
1262 | kfree(scontext2); |
1263 | kfree(str); | |
1da177e4 LT |
1264 | return rc; |
1265 | } | |
1266 | ||
f5c1d5b2 JM |
1267 | /** |
1268 | * security_context_to_sid - Obtain a SID for a given security context. | |
1269 | * @scontext: security context | |
1270 | * @scontext_len: length in bytes | |
1271 | * @sid: security identifier, SID | |
1272 | * | |
1273 | * Obtains a SID associated with the security context that | |
1274 | * has the string representation specified by @scontext. | |
1275 | * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient | |
1276 | * memory is available, or 0 on success. | |
1277 | */ | |
8f0cfa52 | 1278 | int security_context_to_sid(const char *scontext, u32 scontext_len, u32 *sid) |
f5c1d5b2 JM |
1279 | { |
1280 | return security_context_to_sid_core(scontext, scontext_len, | |
12b29f34 | 1281 | sid, SECSID_NULL, GFP_KERNEL, 0); |
f5c1d5b2 JM |
1282 | } |
1283 | ||
1284 | /** | |
1285 | * security_context_to_sid_default - Obtain a SID for a given security context, | |
1286 | * falling back to specified default if needed. | |
1287 | * | |
1288 | * @scontext: security context | |
1289 | * @scontext_len: length in bytes | |
1290 | * @sid: security identifier, SID | |
d133a960 | 1291 | * @def_sid: default SID to assign on error |
f5c1d5b2 JM |
1292 | * |
1293 | * Obtains a SID associated with the security context that | |
1294 | * has the string representation specified by @scontext. | |
1295 | * The default SID is passed to the MLS layer to be used to allow | |
1296 | * kernel labeling of the MLS field if the MLS field is not present | |
1297 | * (for upgrading to MLS without full relabel). | |
12b29f34 | 1298 | * Implicitly forces adding of the context even if it cannot be mapped yet. |
f5c1d5b2 JM |
1299 | * Returns -%EINVAL if the context is invalid, -%ENOMEM if insufficient |
1300 | * memory is available, or 0 on success. | |
1301 | */ | |
7bf570dc DH |
1302 | int security_context_to_sid_default(const char *scontext, u32 scontext_len, |
1303 | u32 *sid, u32 def_sid, gfp_t gfp_flags) | |
f5c1d5b2 JM |
1304 | { |
1305 | return security_context_to_sid_core(scontext, scontext_len, | |
12b29f34 SS |
1306 | sid, def_sid, gfp_flags, 1); |
1307 | } | |
1308 | ||
1309 | int security_context_to_sid_force(const char *scontext, u32 scontext_len, | |
1310 | u32 *sid) | |
1311 | { | |
1312 | return security_context_to_sid_core(scontext, scontext_len, | |
1313 | sid, SECSID_NULL, GFP_KERNEL, 1); | |
f5c1d5b2 JM |
1314 | } |
1315 | ||
1da177e4 LT |
1316 | static int compute_sid_handle_invalid_context( |
1317 | struct context *scontext, | |
1318 | struct context *tcontext, | |
1319 | u16 tclass, | |
1320 | struct context *newcontext) | |
1321 | { | |
1322 | char *s = NULL, *t = NULL, *n = NULL; | |
1323 | u32 slen, tlen, nlen; | |
1324 | ||
4b02b524 | 1325 | if (context_struct_to_string(scontext, &s, &slen)) |
1da177e4 | 1326 | goto out; |
4b02b524 | 1327 | if (context_struct_to_string(tcontext, &t, &tlen)) |
1da177e4 | 1328 | goto out; |
4b02b524 | 1329 | if (context_struct_to_string(newcontext, &n, &nlen)) |
1da177e4 | 1330 | goto out; |
9ad9ad38 | 1331 | audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, |
1da177e4 LT |
1332 | "security_compute_sid: invalid context %s" |
1333 | " for scontext=%s" | |
1334 | " tcontext=%s" | |
1335 | " tclass=%s", | |
ac76c05b | 1336 | n, s, t, sym_name(&policydb, SYM_CLASSES, tclass-1)); |
1da177e4 LT |
1337 | out: |
1338 | kfree(s); | |
1339 | kfree(t); | |
1340 | kfree(n); | |
1341 | if (!selinux_enforcing) | |
1342 | return 0; | |
1343 | return -EACCES; | |
1344 | } | |
1345 | ||
1346 | static int security_compute_sid(u32 ssid, | |
1347 | u32 tsid, | |
c6d3aaa4 | 1348 | u16 orig_tclass, |
1da177e4 | 1349 | u32 specified, |
c6d3aaa4 SS |
1350 | u32 *out_sid, |
1351 | bool kern) | |
1da177e4 LT |
1352 | { |
1353 | struct context *scontext = NULL, *tcontext = NULL, newcontext; | |
1354 | struct role_trans *roletr = NULL; | |
1355 | struct avtab_key avkey; | |
1356 | struct avtab_datum *avdatum; | |
1357 | struct avtab_node *node; | |
c6d3aaa4 | 1358 | u16 tclass; |
1da177e4 LT |
1359 | int rc = 0; |
1360 | ||
1361 | if (!ss_initialized) { | |
c6d3aaa4 SS |
1362 | switch (orig_tclass) { |
1363 | case SECCLASS_PROCESS: /* kernel value */ | |
1da177e4 LT |
1364 | *out_sid = ssid; |
1365 | break; | |
1366 | default: | |
1367 | *out_sid = tsid; | |
1368 | break; | |
1369 | } | |
1370 | goto out; | |
1371 | } | |
1372 | ||
851f8a69 VY |
1373 | context_init(&newcontext); |
1374 | ||
0804d113 | 1375 | read_lock(&policy_rwlock); |
1da177e4 | 1376 | |
c6d3aaa4 SS |
1377 | if (kern) |
1378 | tclass = unmap_class(orig_tclass); | |
1379 | else | |
1380 | tclass = orig_tclass; | |
1381 | ||
1da177e4 LT |
1382 | scontext = sidtab_search(&sidtab, ssid); |
1383 | if (!scontext) { | |
744ba35e EP |
1384 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
1385 | __func__, ssid); | |
1da177e4 LT |
1386 | rc = -EINVAL; |
1387 | goto out_unlock; | |
1388 | } | |
1389 | tcontext = sidtab_search(&sidtab, tsid); | |
1390 | if (!tcontext) { | |
744ba35e EP |
1391 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
1392 | __func__, tsid); | |
1da177e4 LT |
1393 | rc = -EINVAL; |
1394 | goto out_unlock; | |
1395 | } | |
1396 | ||
1da177e4 LT |
1397 | /* Set the user identity. */ |
1398 | switch (specified) { | |
1399 | case AVTAB_TRANSITION: | |
1400 | case AVTAB_CHANGE: | |
1401 | /* Use the process user identity. */ | |
1402 | newcontext.user = scontext->user; | |
1403 | break; | |
1404 | case AVTAB_MEMBER: | |
1405 | /* Use the related object owner. */ | |
1406 | newcontext.user = tcontext->user; | |
1407 | break; | |
1408 | } | |
1409 | ||
1410 | /* Set the role and type to default values. */ | |
c6d3aaa4 | 1411 | if (tclass == policydb.process_class) { |
1da177e4 LT |
1412 | /* Use the current role and type of process. */ |
1413 | newcontext.role = scontext->role; | |
1414 | newcontext.type = scontext->type; | |
c6d3aaa4 | 1415 | } else { |
1da177e4 LT |
1416 | /* Use the well-defined object role. */ |
1417 | newcontext.role = OBJECT_R_VAL; | |
1418 | /* Use the type of the related object. */ | |
1419 | newcontext.type = tcontext->type; | |
1420 | } | |
1421 | ||
1422 | /* Look for a type transition/member/change rule. */ | |
1423 | avkey.source_type = scontext->type; | |
1424 | avkey.target_type = tcontext->type; | |
1425 | avkey.target_class = tclass; | |
782ebb99 SS |
1426 | avkey.specified = specified; |
1427 | avdatum = avtab_search(&policydb.te_avtab, &avkey); | |
1da177e4 LT |
1428 | |
1429 | /* If no permanent rule, also check for enabled conditional rules */ | |
5d55a345 | 1430 | if (!avdatum) { |
782ebb99 | 1431 | node = avtab_search_node(&policydb.te_cond_avtab, &avkey); |
dbc74c65 | 1432 | for (; node; node = avtab_search_node_next(node, specified)) { |
782ebb99 | 1433 | if (node->key.specified & AVTAB_ENABLED) { |
1da177e4 LT |
1434 | avdatum = &node->datum; |
1435 | break; | |
1436 | } | |
1437 | } | |
1438 | } | |
1439 | ||
782ebb99 | 1440 | if (avdatum) { |
1da177e4 | 1441 | /* Use the type from the type transition/member/change rule. */ |
782ebb99 | 1442 | newcontext.type = avdatum->data; |
1da177e4 LT |
1443 | } |
1444 | ||
1445 | /* Check for class-specific changes. */ | |
c6d3aaa4 | 1446 | if (tclass == policydb.process_class) { |
1da177e4 LT |
1447 | if (specified & AVTAB_TRANSITION) { |
1448 | /* Look for a role transition rule. */ | |
1449 | for (roletr = policydb.role_tr; roletr; | |
1450 | roletr = roletr->next) { | |
1451 | if (roletr->role == scontext->role && | |
1452 | roletr->type == tcontext->type) { | |
1453 | /* Use the role transition rule. */ | |
1454 | newcontext.role = roletr->new_role; | |
1455 | break; | |
1456 | } | |
1457 | } | |
1458 | } | |
1da177e4 LT |
1459 | } |
1460 | ||
1461 | /* Set the MLS attributes. | |
1462 | This is done last because it may allocate memory. */ | |
1463 | rc = mls_compute_sid(scontext, tcontext, tclass, specified, &newcontext); | |
1464 | if (rc) | |
1465 | goto out_unlock; | |
1466 | ||
1467 | /* Check the validity of the context. */ | |
1468 | if (!policydb_context_isvalid(&policydb, &newcontext)) { | |
1469 | rc = compute_sid_handle_invalid_context(scontext, | |
1470 | tcontext, | |
1471 | tclass, | |
1472 | &newcontext); | |
1473 | if (rc) | |
1474 | goto out_unlock; | |
1475 | } | |
1476 | /* Obtain the sid for the context. */ | |
1477 | rc = sidtab_context_to_sid(&sidtab, &newcontext, out_sid); | |
1478 | out_unlock: | |
0804d113 | 1479 | read_unlock(&policy_rwlock); |
1da177e4 LT |
1480 | context_destroy(&newcontext); |
1481 | out: | |
1482 | return rc; | |
1483 | } | |
1484 | ||
1485 | /** | |
1486 | * security_transition_sid - Compute the SID for a new subject/object. | |
1487 | * @ssid: source security identifier | |
1488 | * @tsid: target security identifier | |
1489 | * @tclass: target security class | |
1490 | * @out_sid: security identifier for new subject/object | |
1491 | * | |
1492 | * Compute a SID to use for labeling a new subject or object in the | |
1493 | * class @tclass based on a SID pair (@ssid, @tsid). | |
1494 | * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM | |
1495 | * if insufficient memory is available, or %0 if the new SID was | |
1496 | * computed successfully. | |
1497 | */ | |
1498 | int security_transition_sid(u32 ssid, | |
1499 | u32 tsid, | |
1500 | u16 tclass, | |
1501 | u32 *out_sid) | |
1502 | { | |
c6d3aaa4 SS |
1503 | return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, |
1504 | out_sid, true); | |
1505 | } | |
1506 | ||
1507 | int security_transition_sid_user(u32 ssid, | |
1508 | u32 tsid, | |
1509 | u16 tclass, | |
1510 | u32 *out_sid) | |
1511 | { | |
1512 | return security_compute_sid(ssid, tsid, tclass, AVTAB_TRANSITION, | |
1513 | out_sid, false); | |
1da177e4 LT |
1514 | } |
1515 | ||
1516 | /** | |
1517 | * security_member_sid - Compute the SID for member selection. | |
1518 | * @ssid: source security identifier | |
1519 | * @tsid: target security identifier | |
1520 | * @tclass: target security class | |
1521 | * @out_sid: security identifier for selected member | |
1522 | * | |
1523 | * Compute a SID to use when selecting a member of a polyinstantiated | |
1524 | * object of class @tclass based on a SID pair (@ssid, @tsid). | |
1525 | * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM | |
1526 | * if insufficient memory is available, or %0 if the SID was | |
1527 | * computed successfully. | |
1528 | */ | |
1529 | int security_member_sid(u32 ssid, | |
1530 | u32 tsid, | |
1531 | u16 tclass, | |
1532 | u32 *out_sid) | |
1533 | { | |
c6d3aaa4 SS |
1534 | return security_compute_sid(ssid, tsid, tclass, AVTAB_MEMBER, out_sid, |
1535 | false); | |
1da177e4 LT |
1536 | } |
1537 | ||
1538 | /** | |
1539 | * security_change_sid - Compute the SID for object relabeling. | |
1540 | * @ssid: source security identifier | |
1541 | * @tsid: target security identifier | |
1542 | * @tclass: target security class | |
1543 | * @out_sid: security identifier for selected member | |
1544 | * | |
1545 | * Compute a SID to use for relabeling an object of class @tclass | |
1546 | * based on a SID pair (@ssid, @tsid). | |
1547 | * Return -%EINVAL if any of the parameters are invalid, -%ENOMEM | |
1548 | * if insufficient memory is available, or %0 if the SID was | |
1549 | * computed successfully. | |
1550 | */ | |
1551 | int security_change_sid(u32 ssid, | |
1552 | u32 tsid, | |
1553 | u16 tclass, | |
1554 | u32 *out_sid) | |
1555 | { | |
c6d3aaa4 SS |
1556 | return security_compute_sid(ssid, tsid, tclass, AVTAB_CHANGE, out_sid, |
1557 | false); | |
b94c7e67 CS |
1558 | } |
1559 | ||
1da177e4 LT |
1560 | /* Clone the SID into the new SID table. */ |
1561 | static int clone_sid(u32 sid, | |
1562 | struct context *context, | |
1563 | void *arg) | |
1564 | { | |
1565 | struct sidtab *s = arg; | |
1566 | ||
42596eaf GT |
1567 | if (sid > SECINITSID_NUM) |
1568 | return sidtab_insert(s, sid, context); | |
1569 | else | |
1570 | return 0; | |
1da177e4 LT |
1571 | } |
1572 | ||
1573 | static inline int convert_context_handle_invalid_context(struct context *context) | |
1574 | { | |
4b02b524 EP |
1575 | char *s; |
1576 | u32 len; | |
1da177e4 | 1577 | |
4b02b524 EP |
1578 | if (selinux_enforcing) |
1579 | return -EINVAL; | |
1580 | ||
1581 | if (!context_struct_to_string(context, &s, &len)) { | |
1582 | printk(KERN_WARNING "SELinux: Context %s would be invalid if enforcing\n", s); | |
1583 | kfree(s); | |
1da177e4 | 1584 | } |
4b02b524 | 1585 | return 0; |
1da177e4 LT |
1586 | } |
1587 | ||
1588 | struct convert_context_args { | |
1589 | struct policydb *oldp; | |
1590 | struct policydb *newp; | |
1591 | }; | |
1592 | ||
1593 | /* | |
1594 | * Convert the values in the security context | |
1595 | * structure `c' from the values specified | |
1596 | * in the policy `p->oldp' to the values specified | |
1597 | * in the policy `p->newp'. Verify that the | |
1598 | * context is valid under the new policy. | |
1599 | */ | |
1600 | static int convert_context(u32 key, | |
1601 | struct context *c, | |
1602 | void *p) | |
1603 | { | |
1604 | struct convert_context_args *args; | |
1605 | struct context oldc; | |
0719aaf5 GT |
1606 | struct ocontext *oc; |
1607 | struct mls_range *range; | |
1da177e4 LT |
1608 | struct role_datum *role; |
1609 | struct type_datum *typdatum; | |
1610 | struct user_datum *usrdatum; | |
1611 | char *s; | |
1612 | u32 len; | |
42596eaf GT |
1613 | int rc = 0; |
1614 | ||
1615 | if (key <= SECINITSID_NUM) | |
1616 | goto out; | |
1da177e4 LT |
1617 | |
1618 | args = p; | |
1619 | ||
12b29f34 SS |
1620 | if (c->str) { |
1621 | struct context ctx; | |
4b02b524 EP |
1622 | |
1623 | rc = -ENOMEM; | |
9a59daa0 | 1624 | s = kstrdup(c->str, GFP_KERNEL); |
4b02b524 | 1625 | if (!s) |
9a59daa0 | 1626 | goto out; |
4b02b524 | 1627 | |
9a59daa0 SS |
1628 | rc = string_to_context_struct(args->newp, NULL, s, |
1629 | c->len, &ctx, SECSID_NULL); | |
1630 | kfree(s); | |
12b29f34 | 1631 | if (!rc) { |
4b02b524 | 1632 | printk(KERN_INFO "SELinux: Context %s became valid (mapped).\n", |
12b29f34 SS |
1633 | c->str); |
1634 | /* Replace string with mapped representation. */ | |
1635 | kfree(c->str); | |
1636 | memcpy(c, &ctx, sizeof(*c)); | |
1637 | goto out; | |
1638 | } else if (rc == -EINVAL) { | |
1639 | /* Retain string representation for later mapping. */ | |
1640 | rc = 0; | |
1641 | goto out; | |
1642 | } else { | |
1643 | /* Other error condition, e.g. ENOMEM. */ | |
4b02b524 | 1644 | printk(KERN_ERR "SELinux: Unable to map context %s, rc = %d.\n", |
12b29f34 SS |
1645 | c->str, -rc); |
1646 | goto out; | |
1647 | } | |
1648 | } | |
1649 | ||
1da177e4 LT |
1650 | rc = context_cpy(&oldc, c); |
1651 | if (rc) | |
1652 | goto out; | |
1653 | ||
1da177e4 | 1654 | /* Convert the user. */ |
4b02b524 | 1655 | rc = -EINVAL; |
1da177e4 | 1656 | usrdatum = hashtab_search(args->newp->p_users.table, |
ac76c05b | 1657 | sym_name(args->oldp, SYM_USERS, c->user - 1)); |
5d55a345 | 1658 | if (!usrdatum) |
1da177e4 | 1659 | goto bad; |
1da177e4 LT |
1660 | c->user = usrdatum->value; |
1661 | ||
1662 | /* Convert the role. */ | |
4b02b524 | 1663 | rc = -EINVAL; |
1da177e4 | 1664 | role = hashtab_search(args->newp->p_roles.table, |
ac76c05b | 1665 | sym_name(args->oldp, SYM_ROLES, c->role - 1)); |
5d55a345 | 1666 | if (!role) |
1da177e4 | 1667 | goto bad; |
1da177e4 LT |
1668 | c->role = role->value; |
1669 | ||
1670 | /* Convert the type. */ | |
4b02b524 | 1671 | rc = -EINVAL; |
1da177e4 | 1672 | typdatum = hashtab_search(args->newp->p_types.table, |
ac76c05b | 1673 | sym_name(args->oldp, SYM_TYPES, c->type - 1)); |
5d55a345 | 1674 | if (!typdatum) |
1da177e4 | 1675 | goto bad; |
1da177e4 LT |
1676 | c->type = typdatum->value; |
1677 | ||
0719aaf5 GT |
1678 | /* Convert the MLS fields if dealing with MLS policies */ |
1679 | if (args->oldp->mls_enabled && args->newp->mls_enabled) { | |
1680 | rc = mls_convert_context(args->oldp, args->newp, c); | |
1681 | if (rc) | |
1682 | goto bad; | |
1683 | } else if (args->oldp->mls_enabled && !args->newp->mls_enabled) { | |
1684 | /* | |
1685 | * Switching between MLS and non-MLS policy: | |
1686 | * free any storage used by the MLS fields in the | |
1687 | * context for all existing entries in the sidtab. | |
1688 | */ | |
1689 | mls_context_destroy(c); | |
1690 | } else if (!args->oldp->mls_enabled && args->newp->mls_enabled) { | |
1691 | /* | |
1692 | * Switching between non-MLS and MLS policy: | |
1693 | * ensure that the MLS fields of the context for all | |
1694 | * existing entries in the sidtab are filled in with a | |
1695 | * suitable default value, likely taken from one of the | |
1696 | * initial SIDs. | |
1697 | */ | |
1698 | oc = args->newp->ocontexts[OCON_ISID]; | |
1699 | while (oc && oc->sid[0] != SECINITSID_UNLABELED) | |
1700 | oc = oc->next; | |
4b02b524 | 1701 | rc = -EINVAL; |
0719aaf5 GT |
1702 | if (!oc) { |
1703 | printk(KERN_ERR "SELinux: unable to look up" | |
1704 | " the initial SIDs list\n"); | |
1705 | goto bad; | |
1706 | } | |
1707 | range = &oc->context[0].range; | |
1708 | rc = mls_range_set(c, range); | |
1709 | if (rc) | |
1710 | goto bad; | |
1711 | } | |
1da177e4 LT |
1712 | |
1713 | /* Check the validity of the new context. */ | |
1714 | if (!policydb_context_isvalid(args->newp, c)) { | |
1715 | rc = convert_context_handle_invalid_context(&oldc); | |
1716 | if (rc) | |
1717 | goto bad; | |
1718 | } | |
1719 | ||
1720 | context_destroy(&oldc); | |
4b02b524 | 1721 | |
12b29f34 | 1722 | rc = 0; |
1da177e4 LT |
1723 | out: |
1724 | return rc; | |
1725 | bad: | |
12b29f34 | 1726 | /* Map old representation to string and save it. */ |
4b02b524 EP |
1727 | rc = context_struct_to_string(&oldc, &s, &len); |
1728 | if (rc) | |
1729 | return rc; | |
1da177e4 | 1730 | context_destroy(&oldc); |
12b29f34 SS |
1731 | context_destroy(c); |
1732 | c->str = s; | |
1733 | c->len = len; | |
4b02b524 | 1734 | printk(KERN_INFO "SELinux: Context %s became invalid (unmapped).\n", |
12b29f34 SS |
1735 | c->str); |
1736 | rc = 0; | |
1da177e4 LT |
1737 | goto out; |
1738 | } | |
1739 | ||
3bb56b25 PM |
1740 | static void security_load_policycaps(void) |
1741 | { | |
1742 | selinux_policycap_netpeer = ebitmap_get_bit(&policydb.policycaps, | |
1743 | POLICYDB_CAPABILITY_NETPEER); | |
b0c636b9 EP |
1744 | selinux_policycap_openperm = ebitmap_get_bit(&policydb.policycaps, |
1745 | POLICYDB_CAPABILITY_OPENPERM); | |
3bb56b25 PM |
1746 | } |
1747 | ||
1da177e4 | 1748 | extern void selinux_complete_init(void); |
e900a7d9 | 1749 | static int security_preserve_bools(struct policydb *p); |
1da177e4 LT |
1750 | |
1751 | /** | |
1752 | * security_load_policy - Load a security policy configuration. | |
1753 | * @data: binary policy data | |
1754 | * @len: length of data in bytes | |
1755 | * | |
1756 | * Load a new set of security policy configuration data, | |
1757 | * validate it and convert the SID table as necessary. | |
1758 | * This function will flush the access vector cache after | |
1759 | * loading the new policy. | |
1760 | */ | |
1761 | int security_load_policy(void *data, size_t len) | |
1762 | { | |
1763 | struct policydb oldpolicydb, newpolicydb; | |
1764 | struct sidtab oldsidtab, newsidtab; | |
c6d3aaa4 | 1765 | struct selinux_mapping *oldmap, *map = NULL; |
1da177e4 LT |
1766 | struct convert_context_args args; |
1767 | u32 seqno; | |
c6d3aaa4 | 1768 | u16 map_size; |
1da177e4 LT |
1769 | int rc = 0; |
1770 | struct policy_file file = { data, len }, *fp = &file; | |
1771 | ||
1da177e4 LT |
1772 | if (!ss_initialized) { |
1773 | avtab_cache_init(); | |
a2000050 EP |
1774 | rc = policydb_read(&policydb, fp); |
1775 | if (rc) { | |
1da177e4 | 1776 | avtab_cache_destroy(); |
a2000050 | 1777 | return rc; |
1da177e4 | 1778 | } |
a2000050 | 1779 | |
cee74f47 | 1780 | policydb.len = len; |
a2000050 EP |
1781 | rc = selinux_set_mapping(&policydb, secclass_map, |
1782 | ¤t_mapping, | |
1783 | ¤t_mapping_size); | |
1784 | if (rc) { | |
1da177e4 LT |
1785 | policydb_destroy(&policydb); |
1786 | avtab_cache_destroy(); | |
a2000050 | 1787 | return rc; |
1da177e4 | 1788 | } |
a2000050 EP |
1789 | |
1790 | rc = policydb_load_isids(&policydb, &sidtab); | |
1791 | if (rc) { | |
b94c7e67 CS |
1792 | policydb_destroy(&policydb); |
1793 | avtab_cache_destroy(); | |
a2000050 | 1794 | return rc; |
b94c7e67 | 1795 | } |
a2000050 | 1796 | |
3bb56b25 | 1797 | security_load_policycaps(); |
1da177e4 | 1798 | ss_initialized = 1; |
4c443d1b | 1799 | seqno = ++latest_granting; |
1da177e4 | 1800 | selinux_complete_init(); |
4c443d1b SS |
1801 | avc_ss_reset(seqno); |
1802 | selnl_notify_policyload(seqno); | |
11904167 | 1803 | selinux_status_update_policyload(seqno); |
7420ed23 | 1804 | selinux_netlbl_cache_invalidate(); |
342a0cff | 1805 | selinux_xfrm_notify_policyload(); |
1da177e4 LT |
1806 | return 0; |
1807 | } | |
1808 | ||
1809 | #if 0 | |
1810 | sidtab_hash_eval(&sidtab, "sids"); | |
1811 | #endif | |
1812 | ||
a2000050 EP |
1813 | rc = policydb_read(&newpolicydb, fp); |
1814 | if (rc) | |
1815 | return rc; | |
1da177e4 | 1816 | |
cee74f47 | 1817 | newpolicydb.len = len; |
0719aaf5 GT |
1818 | /* If switching between different policy types, log MLS status */ |
1819 | if (policydb.mls_enabled && !newpolicydb.mls_enabled) | |
1820 | printk(KERN_INFO "SELinux: Disabling MLS support...\n"); | |
1821 | else if (!policydb.mls_enabled && newpolicydb.mls_enabled) | |
1822 | printk(KERN_INFO "SELinux: Enabling MLS support...\n"); | |
1823 | ||
42596eaf GT |
1824 | rc = policydb_load_isids(&newpolicydb, &newsidtab); |
1825 | if (rc) { | |
1826 | printk(KERN_ERR "SELinux: unable to load the initial SIDs\n"); | |
12b29f34 | 1827 | policydb_destroy(&newpolicydb); |
42596eaf | 1828 | return rc; |
12b29f34 | 1829 | } |
1da177e4 | 1830 | |
a2000050 EP |
1831 | rc = selinux_set_mapping(&newpolicydb, secclass_map, &map, &map_size); |
1832 | if (rc) | |
b94c7e67 | 1833 | goto err; |
b94c7e67 | 1834 | |
e900a7d9 SS |
1835 | rc = security_preserve_bools(&newpolicydb); |
1836 | if (rc) { | |
454d972c | 1837 | printk(KERN_ERR "SELinux: unable to preserve booleans\n"); |
e900a7d9 SS |
1838 | goto err; |
1839 | } | |
1840 | ||
1da177e4 LT |
1841 | /* Clone the SID table. */ |
1842 | sidtab_shutdown(&sidtab); | |
a2000050 EP |
1843 | |
1844 | rc = sidtab_map(&sidtab, clone_sid, &newsidtab); | |
1845 | if (rc) | |
1da177e4 | 1846 | goto err; |
1da177e4 | 1847 | |
12b29f34 SS |
1848 | /* |
1849 | * Convert the internal representations of contexts | |
1850 | * in the new SID table. | |
1851 | */ | |
1da177e4 LT |
1852 | args.oldp = &policydb; |
1853 | args.newp = &newpolicydb; | |
12b29f34 | 1854 | rc = sidtab_map(&newsidtab, convert_context, &args); |
0719aaf5 GT |
1855 | if (rc) { |
1856 | printk(KERN_ERR "SELinux: unable to convert the internal" | |
1857 | " representation of contexts in the new SID" | |
1858 | " table\n"); | |
12b29f34 | 1859 | goto err; |
0719aaf5 | 1860 | } |
1da177e4 LT |
1861 | |
1862 | /* Save the old policydb and SID table to free later. */ | |
1863 | memcpy(&oldpolicydb, &policydb, sizeof policydb); | |
1864 | sidtab_set(&oldsidtab, &sidtab); | |
1865 | ||
1866 | /* Install the new policydb and SID table. */ | |
0804d113 | 1867 | write_lock_irq(&policy_rwlock); |
1da177e4 LT |
1868 | memcpy(&policydb, &newpolicydb, sizeof policydb); |
1869 | sidtab_set(&sidtab, &newsidtab); | |
3bb56b25 | 1870 | security_load_policycaps(); |
c6d3aaa4 SS |
1871 | oldmap = current_mapping; |
1872 | current_mapping = map; | |
1873 | current_mapping_size = map_size; | |
1da177e4 | 1874 | seqno = ++latest_granting; |
0804d113 | 1875 | write_unlock_irq(&policy_rwlock); |
1da177e4 LT |
1876 | |
1877 | /* Free the old policydb and SID table. */ | |
1878 | policydb_destroy(&oldpolicydb); | |
1879 | sidtab_destroy(&oldsidtab); | |
c6d3aaa4 | 1880 | kfree(oldmap); |
1da177e4 LT |
1881 | |
1882 | avc_ss_reset(seqno); | |
1883 | selnl_notify_policyload(seqno); | |
11904167 | 1884 | selinux_status_update_policyload(seqno); |
7420ed23 | 1885 | selinux_netlbl_cache_invalidate(); |
342a0cff | 1886 | selinux_xfrm_notify_policyload(); |
1da177e4 LT |
1887 | |
1888 | return 0; | |
1889 | ||
1890 | err: | |
c6d3aaa4 | 1891 | kfree(map); |
1da177e4 LT |
1892 | sidtab_destroy(&newsidtab); |
1893 | policydb_destroy(&newpolicydb); | |
1894 | return rc; | |
1895 | ||
1896 | } | |
1897 | ||
cee74f47 EP |
1898 | size_t security_policydb_len(void) |
1899 | { | |
1900 | size_t len; | |
1901 | ||
1902 | read_lock(&policy_rwlock); | |
1903 | len = policydb.len; | |
1904 | read_unlock(&policy_rwlock); | |
1905 | ||
1906 | return len; | |
1907 | } | |
1908 | ||
1da177e4 LT |
1909 | /** |
1910 | * security_port_sid - Obtain the SID for a port. | |
1da177e4 LT |
1911 | * @protocol: protocol number |
1912 | * @port: port number | |
1913 | * @out_sid: security identifier | |
1914 | */ | |
3e112172 | 1915 | int security_port_sid(u8 protocol, u16 port, u32 *out_sid) |
1da177e4 LT |
1916 | { |
1917 | struct ocontext *c; | |
1918 | int rc = 0; | |
1919 | ||
0804d113 | 1920 | read_lock(&policy_rwlock); |
1da177e4 LT |
1921 | |
1922 | c = policydb.ocontexts[OCON_PORT]; | |
1923 | while (c) { | |
1924 | if (c->u.port.protocol == protocol && | |
1925 | c->u.port.low_port <= port && | |
1926 | c->u.port.high_port >= port) | |
1927 | break; | |
1928 | c = c->next; | |
1929 | } | |
1930 | ||
1931 | if (c) { | |
1932 | if (!c->sid[0]) { | |
1933 | rc = sidtab_context_to_sid(&sidtab, | |
1934 | &c->context[0], | |
1935 | &c->sid[0]); | |
1936 | if (rc) | |
1937 | goto out; | |
1938 | } | |
1939 | *out_sid = c->sid[0]; | |
1940 | } else { | |
1941 | *out_sid = SECINITSID_PORT; | |
1942 | } | |
1943 | ||
1944 | out: | |
0804d113 | 1945 | read_unlock(&policy_rwlock); |
1da177e4 LT |
1946 | return rc; |
1947 | } | |
1948 | ||
1949 | /** | |
1950 | * security_netif_sid - Obtain the SID for a network interface. | |
1951 | * @name: interface name | |
1952 | * @if_sid: interface SID | |
1da177e4 | 1953 | */ |
e8bfdb9d | 1954 | int security_netif_sid(char *name, u32 *if_sid) |
1da177e4 LT |
1955 | { |
1956 | int rc = 0; | |
1957 | struct ocontext *c; | |
1958 | ||
0804d113 | 1959 | read_lock(&policy_rwlock); |
1da177e4 LT |
1960 | |
1961 | c = policydb.ocontexts[OCON_NETIF]; | |
1962 | while (c) { | |
1963 | if (strcmp(name, c->u.name) == 0) | |
1964 | break; | |
1965 | c = c->next; | |
1966 | } | |
1967 | ||
1968 | if (c) { | |
1969 | if (!c->sid[0] || !c->sid[1]) { | |
1970 | rc = sidtab_context_to_sid(&sidtab, | |
1971 | &c->context[0], | |
1972 | &c->sid[0]); | |
1973 | if (rc) | |
1974 | goto out; | |
1975 | rc = sidtab_context_to_sid(&sidtab, | |
1976 | &c->context[1], | |
1977 | &c->sid[1]); | |
1978 | if (rc) | |
1979 | goto out; | |
1980 | } | |
1981 | *if_sid = c->sid[0]; | |
e8bfdb9d | 1982 | } else |
1da177e4 | 1983 | *if_sid = SECINITSID_NETIF; |
1da177e4 LT |
1984 | |
1985 | out: | |
0804d113 | 1986 | read_unlock(&policy_rwlock); |
1da177e4 LT |
1987 | return rc; |
1988 | } | |
1989 | ||
1990 | static int match_ipv6_addrmask(u32 *input, u32 *addr, u32 *mask) | |
1991 | { | |
1992 | int i, fail = 0; | |
1993 | ||
5d55a345 EP |
1994 | for (i = 0; i < 4; i++) |
1995 | if (addr[i] != (input[i] & mask[i])) { | |
1da177e4 LT |
1996 | fail = 1; |
1997 | break; | |
1998 | } | |
1999 | ||
2000 | return !fail; | |
2001 | } | |
2002 | ||
2003 | /** | |
2004 | * security_node_sid - Obtain the SID for a node (host). | |
2005 | * @domain: communication domain aka address family | |
2006 | * @addrp: address | |
2007 | * @addrlen: address length in bytes | |
2008 | * @out_sid: security identifier | |
2009 | */ | |
2010 | int security_node_sid(u16 domain, | |
2011 | void *addrp, | |
2012 | u32 addrlen, | |
2013 | u32 *out_sid) | |
2014 | { | |
4b02b524 | 2015 | int rc; |
1da177e4 LT |
2016 | struct ocontext *c; |
2017 | ||
0804d113 | 2018 | read_lock(&policy_rwlock); |
1da177e4 LT |
2019 | |
2020 | switch (domain) { | |
2021 | case AF_INET: { | |
2022 | u32 addr; | |
2023 | ||
4b02b524 EP |
2024 | rc = -EINVAL; |
2025 | if (addrlen != sizeof(u32)) | |
1da177e4 | 2026 | goto out; |
1da177e4 LT |
2027 | |
2028 | addr = *((u32 *)addrp); | |
2029 | ||
2030 | c = policydb.ocontexts[OCON_NODE]; | |
2031 | while (c) { | |
2032 | if (c->u.node.addr == (addr & c->u.node.mask)) | |
2033 | break; | |
2034 | c = c->next; | |
2035 | } | |
2036 | break; | |
2037 | } | |
2038 | ||
2039 | case AF_INET6: | |
4b02b524 EP |
2040 | rc = -EINVAL; |
2041 | if (addrlen != sizeof(u64) * 2) | |
1da177e4 | 2042 | goto out; |
1da177e4 LT |
2043 | c = policydb.ocontexts[OCON_NODE6]; |
2044 | while (c) { | |
2045 | if (match_ipv6_addrmask(addrp, c->u.node6.addr, | |
2046 | c->u.node6.mask)) | |
2047 | break; | |
2048 | c = c->next; | |
2049 | } | |
2050 | break; | |
2051 | ||
2052 | default: | |
4b02b524 | 2053 | rc = 0; |
1da177e4 LT |
2054 | *out_sid = SECINITSID_NODE; |
2055 | goto out; | |
2056 | } | |
2057 | ||
2058 | if (c) { | |
2059 | if (!c->sid[0]) { | |
2060 | rc = sidtab_context_to_sid(&sidtab, | |
2061 | &c->context[0], | |
2062 | &c->sid[0]); | |
2063 | if (rc) | |
2064 | goto out; | |
2065 | } | |
2066 | *out_sid = c->sid[0]; | |
2067 | } else { | |
2068 | *out_sid = SECINITSID_NODE; | |
2069 | } | |
2070 | ||
4b02b524 | 2071 | rc = 0; |
1da177e4 | 2072 | out: |
0804d113 | 2073 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2074 | return rc; |
2075 | } | |
2076 | ||
2077 | #define SIDS_NEL 25 | |
2078 | ||
2079 | /** | |
2080 | * security_get_user_sids - Obtain reachable SIDs for a user. | |
2081 | * @fromsid: starting SID | |
2082 | * @username: username | |
2083 | * @sids: array of reachable SIDs for user | |
2084 | * @nel: number of elements in @sids | |
2085 | * | |
2086 | * Generate the set of SIDs for legal security contexts | |
2087 | * for a given user that can be reached by @fromsid. | |
2088 | * Set *@sids to point to a dynamically allocated | |
2089 | * array containing the set of SIDs. Set *@nel to the | |
2090 | * number of elements in the array. | |
2091 | */ | |
2092 | ||
2093 | int security_get_user_sids(u32 fromsid, | |
5d55a345 | 2094 | char *username, |
1da177e4 LT |
2095 | u32 **sids, |
2096 | u32 *nel) | |
2097 | { | |
2098 | struct context *fromcon, usercon; | |
2c3c05db | 2099 | u32 *mysids = NULL, *mysids2, sid; |
1da177e4 LT |
2100 | u32 mynel = 0, maxnel = SIDS_NEL; |
2101 | struct user_datum *user; | |
2102 | struct role_datum *role; | |
782ebb99 | 2103 | struct ebitmap_node *rnode, *tnode; |
1da177e4 LT |
2104 | int rc = 0, i, j; |
2105 | ||
2c3c05db SS |
2106 | *sids = NULL; |
2107 | *nel = 0; | |
2108 | ||
2109 | if (!ss_initialized) | |
1da177e4 | 2110 | goto out; |
1da177e4 | 2111 | |
0804d113 | 2112 | read_lock(&policy_rwlock); |
1da177e4 | 2113 | |
12b29f34 SS |
2114 | context_init(&usercon); |
2115 | ||
4b02b524 | 2116 | rc = -EINVAL; |
1da177e4 | 2117 | fromcon = sidtab_search(&sidtab, fromsid); |
4b02b524 | 2118 | if (!fromcon) |
1da177e4 | 2119 | goto out_unlock; |
1da177e4 | 2120 | |
4b02b524 | 2121 | rc = -EINVAL; |
1da177e4 | 2122 | user = hashtab_search(policydb.p_users.table, username); |
4b02b524 | 2123 | if (!user) |
1da177e4 | 2124 | goto out_unlock; |
4b02b524 | 2125 | |
1da177e4 LT |
2126 | usercon.user = user->value; |
2127 | ||
4b02b524 | 2128 | rc = -ENOMEM; |
89d155ef | 2129 | mysids = kcalloc(maxnel, sizeof(*mysids), GFP_ATOMIC); |
4b02b524 | 2130 | if (!mysids) |
1da177e4 | 2131 | goto out_unlock; |
1da177e4 | 2132 | |
9fe79ad1 | 2133 | ebitmap_for_each_positive_bit(&user->roles, rnode, i) { |
1da177e4 | 2134 | role = policydb.role_val_to_struct[i]; |
c1a7368a | 2135 | usercon.role = i + 1; |
9fe79ad1 | 2136 | ebitmap_for_each_positive_bit(&role->types, tnode, j) { |
c1a7368a | 2137 | usercon.type = j + 1; |
1da177e4 LT |
2138 | |
2139 | if (mls_setup_user_range(fromcon, user, &usercon)) | |
2140 | continue; | |
2141 | ||
1da177e4 | 2142 | rc = sidtab_context_to_sid(&sidtab, &usercon, &sid); |
2c3c05db | 2143 | if (rc) |
1da177e4 | 2144 | goto out_unlock; |
1da177e4 LT |
2145 | if (mynel < maxnel) { |
2146 | mysids[mynel++] = sid; | |
2147 | } else { | |
4b02b524 | 2148 | rc = -ENOMEM; |
1da177e4 | 2149 | maxnel += SIDS_NEL; |
89d155ef | 2150 | mysids2 = kcalloc(maxnel, sizeof(*mysids2), GFP_ATOMIC); |
4b02b524 | 2151 | if (!mysids2) |
1da177e4 | 2152 | goto out_unlock; |
1da177e4 LT |
2153 | memcpy(mysids2, mysids, mynel * sizeof(*mysids2)); |
2154 | kfree(mysids); | |
2155 | mysids = mysids2; | |
2156 | mysids[mynel++] = sid; | |
2157 | } | |
2158 | } | |
2159 | } | |
4b02b524 | 2160 | rc = 0; |
1da177e4 | 2161 | out_unlock: |
0804d113 | 2162 | read_unlock(&policy_rwlock); |
2c3c05db SS |
2163 | if (rc || !mynel) { |
2164 | kfree(mysids); | |
2165 | goto out; | |
2166 | } | |
2167 | ||
4b02b524 | 2168 | rc = -ENOMEM; |
2c3c05db SS |
2169 | mysids2 = kcalloc(mynel, sizeof(*mysids2), GFP_KERNEL); |
2170 | if (!mysids2) { | |
2c3c05db SS |
2171 | kfree(mysids); |
2172 | goto out; | |
2173 | } | |
2174 | for (i = 0, j = 0; i < mynel; i++) { | |
2175 | rc = avc_has_perm_noaudit(fromsid, mysids[i], | |
c6d3aaa4 | 2176 | SECCLASS_PROCESS, /* kernel value */ |
2c3c05db SS |
2177 | PROCESS__TRANSITION, AVC_STRICT, |
2178 | NULL); | |
2179 | if (!rc) | |
2180 | mysids2[j++] = mysids[i]; | |
2181 | cond_resched(); | |
2182 | } | |
2183 | rc = 0; | |
2184 | kfree(mysids); | |
2185 | *sids = mysids2; | |
2186 | *nel = j; | |
1da177e4 LT |
2187 | out: |
2188 | return rc; | |
2189 | } | |
2190 | ||
2191 | /** | |
2192 | * security_genfs_sid - Obtain a SID for a file in a filesystem | |
2193 | * @fstype: filesystem type | |
2194 | * @path: path from root of mount | |
2195 | * @sclass: file security class | |
2196 | * @sid: SID for path | |
2197 | * | |
2198 | * Obtain a SID to use for a file in a filesystem that | |
2199 | * cannot support xattr or use a fixed labeling behavior like | |
2200 | * transition SIDs or task SIDs. | |
2201 | */ | |
2202 | int security_genfs_sid(const char *fstype, | |
5d55a345 | 2203 | char *path, |
c6d3aaa4 | 2204 | u16 orig_sclass, |
1da177e4 LT |
2205 | u32 *sid) |
2206 | { | |
2207 | int len; | |
c6d3aaa4 | 2208 | u16 sclass; |
1da177e4 LT |
2209 | struct genfs *genfs; |
2210 | struct ocontext *c; | |
4b02b524 | 2211 | int rc, cmp = 0; |
1da177e4 | 2212 | |
b1aa5301 SS |
2213 | while (path[0] == '/' && path[1] == '/') |
2214 | path++; | |
2215 | ||
0804d113 | 2216 | read_lock(&policy_rwlock); |
1da177e4 | 2217 | |
c6d3aaa4 | 2218 | sclass = unmap_class(orig_sclass); |
4b02b524 | 2219 | *sid = SECINITSID_UNLABELED; |
c6d3aaa4 | 2220 | |
1da177e4 LT |
2221 | for (genfs = policydb.genfs; genfs; genfs = genfs->next) { |
2222 | cmp = strcmp(fstype, genfs->fstype); | |
2223 | if (cmp <= 0) | |
2224 | break; | |
2225 | } | |
2226 | ||
4b02b524 EP |
2227 | rc = -ENOENT; |
2228 | if (!genfs || cmp) | |
1da177e4 | 2229 | goto out; |
1da177e4 LT |
2230 | |
2231 | for (c = genfs->head; c; c = c->next) { | |
2232 | len = strlen(c->u.name); | |
2233 | if ((!c->v.sclass || sclass == c->v.sclass) && | |
2234 | (strncmp(c->u.name, path, len) == 0)) | |
2235 | break; | |
2236 | } | |
2237 | ||
4b02b524 EP |
2238 | rc = -ENOENT; |
2239 | if (!c) | |
1da177e4 | 2240 | goto out; |
1da177e4 LT |
2241 | |
2242 | if (!c->sid[0]) { | |
4b02b524 | 2243 | rc = sidtab_context_to_sid(&sidtab, &c->context[0], &c->sid[0]); |
1da177e4 LT |
2244 | if (rc) |
2245 | goto out; | |
2246 | } | |
2247 | ||
2248 | *sid = c->sid[0]; | |
4b02b524 | 2249 | rc = 0; |
1da177e4 | 2250 | out: |
0804d113 | 2251 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2252 | return rc; |
2253 | } | |
2254 | ||
2255 | /** | |
2256 | * security_fs_use - Determine how to handle labeling for a filesystem. | |
2257 | * @fstype: filesystem type | |
2258 | * @behavior: labeling behavior | |
2259 | * @sid: SID for filesystem (superblock) | |
2260 | */ | |
2261 | int security_fs_use( | |
2262 | const char *fstype, | |
2263 | unsigned int *behavior, | |
089be43e | 2264 | u32 *sid) |
1da177e4 LT |
2265 | { |
2266 | int rc = 0; | |
2267 | struct ocontext *c; | |
2268 | ||
0804d113 | 2269 | read_lock(&policy_rwlock); |
1da177e4 LT |
2270 | |
2271 | c = policydb.ocontexts[OCON_FSUSE]; | |
2272 | while (c) { | |
2273 | if (strcmp(fstype, c->u.name) == 0) | |
2274 | break; | |
2275 | c = c->next; | |
2276 | } | |
2277 | ||
2278 | if (c) { | |
2279 | *behavior = c->v.behavior; | |
2280 | if (!c->sid[0]) { | |
4b02b524 | 2281 | rc = sidtab_context_to_sid(&sidtab, &c->context[0], |
1da177e4 LT |
2282 | &c->sid[0]); |
2283 | if (rc) | |
2284 | goto out; | |
2285 | } | |
2286 | *sid = c->sid[0]; | |
2287 | } else { | |
089be43e JM |
2288 | rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid); |
2289 | if (rc) { | |
2290 | *behavior = SECURITY_FS_USE_NONE; | |
2291 | rc = 0; | |
2292 | } else { | |
2293 | *behavior = SECURITY_FS_USE_GENFS; | |
2294 | } | |
1da177e4 LT |
2295 | } |
2296 | ||
2297 | out: | |
0804d113 | 2298 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2299 | return rc; |
2300 | } | |
2301 | ||
2302 | int security_get_bools(int *len, char ***names, int **values) | |
2303 | { | |
4b02b524 | 2304 | int i, rc; |
1da177e4 | 2305 | |
0804d113 | 2306 | read_lock(&policy_rwlock); |
1da177e4 LT |
2307 | *names = NULL; |
2308 | *values = NULL; | |
2309 | ||
4b02b524 | 2310 | rc = 0; |
1da177e4 | 2311 | *len = policydb.p_bools.nprim; |
4b02b524 | 2312 | if (!*len) |
1da177e4 | 2313 | goto out; |
1da177e4 | 2314 | |
4b02b524 EP |
2315 | rc = -ENOMEM; |
2316 | *names = kcalloc(*len, sizeof(char *), GFP_ATOMIC); | |
1da177e4 LT |
2317 | if (!*names) |
2318 | goto err; | |
1da177e4 | 2319 | |
4b02b524 EP |
2320 | rc = -ENOMEM; |
2321 | *values = kcalloc(*len, sizeof(int), GFP_ATOMIC); | |
1da177e4 LT |
2322 | if (!*values) |
2323 | goto err; | |
2324 | ||
2325 | for (i = 0; i < *len; i++) { | |
2326 | size_t name_len; | |
4b02b524 | 2327 | |
1da177e4 | 2328 | (*values)[i] = policydb.bool_val_to_struct[i]->state; |
ac76c05b | 2329 | name_len = strlen(sym_name(&policydb, SYM_BOOLS, i)) + 1; |
4b02b524 EP |
2330 | |
2331 | rc = -ENOMEM; | |
2332 | (*names)[i] = kmalloc(sizeof(char) * name_len, GFP_ATOMIC); | |
1da177e4 LT |
2333 | if (!(*names)[i]) |
2334 | goto err; | |
4b02b524 | 2335 | |
ac76c05b | 2336 | strncpy((*names)[i], sym_name(&policydb, SYM_BOOLS, i), name_len); |
1da177e4 LT |
2337 | (*names)[i][name_len - 1] = 0; |
2338 | } | |
2339 | rc = 0; | |
2340 | out: | |
0804d113 | 2341 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2342 | return rc; |
2343 | err: | |
2344 | if (*names) { | |
2345 | for (i = 0; i < *len; i++) | |
9a5f04bf | 2346 | kfree((*names)[i]); |
1da177e4 | 2347 | } |
9a5f04bf | 2348 | kfree(*values); |
1da177e4 LT |
2349 | goto out; |
2350 | } | |
2351 | ||
2352 | ||
2353 | int security_set_bools(int len, int *values) | |
2354 | { | |
4b02b524 | 2355 | int i, rc; |
1da177e4 LT |
2356 | int lenp, seqno = 0; |
2357 | struct cond_node *cur; | |
2358 | ||
0804d113 | 2359 | write_lock_irq(&policy_rwlock); |
1da177e4 | 2360 | |
4b02b524 | 2361 | rc = -EFAULT; |
1da177e4 | 2362 | lenp = policydb.p_bools.nprim; |
4b02b524 | 2363 | if (len != lenp) |
1da177e4 | 2364 | goto out; |
1da177e4 | 2365 | |
1da177e4 | 2366 | for (i = 0; i < len; i++) { |
af601e46 SG |
2367 | if (!!values[i] != policydb.bool_val_to_struct[i]->state) { |
2368 | audit_log(current->audit_context, GFP_ATOMIC, | |
2369 | AUDIT_MAC_CONFIG_CHANGE, | |
4746ec5b | 2370 | "bool=%s val=%d old_val=%d auid=%u ses=%u", |
ac76c05b | 2371 | sym_name(&policydb, SYM_BOOLS, i), |
af601e46 SG |
2372 | !!values[i], |
2373 | policydb.bool_val_to_struct[i]->state, | |
4746ec5b EP |
2374 | audit_get_loginuid(current), |
2375 | audit_get_sessionid(current)); | |
af601e46 | 2376 | } |
5d55a345 | 2377 | if (values[i]) |
1da177e4 | 2378 | policydb.bool_val_to_struct[i]->state = 1; |
5d55a345 | 2379 | else |
1da177e4 | 2380 | policydb.bool_val_to_struct[i]->state = 0; |
1da177e4 | 2381 | } |
1da177e4 | 2382 | |
dbc74c65 | 2383 | for (cur = policydb.cond_list; cur; cur = cur->next) { |
1da177e4 LT |
2384 | rc = evaluate_cond_node(&policydb, cur); |
2385 | if (rc) | |
2386 | goto out; | |
2387 | } | |
2388 | ||
2389 | seqno = ++latest_granting; | |
4b02b524 | 2390 | rc = 0; |
1da177e4 | 2391 | out: |
0804d113 | 2392 | write_unlock_irq(&policy_rwlock); |
1da177e4 LT |
2393 | if (!rc) { |
2394 | avc_ss_reset(seqno); | |
2395 | selnl_notify_policyload(seqno); | |
11904167 | 2396 | selinux_status_update_policyload(seqno); |
342a0cff | 2397 | selinux_xfrm_notify_policyload(); |
1da177e4 LT |
2398 | } |
2399 | return rc; | |
2400 | } | |
2401 | ||
2402 | int security_get_bool_value(int bool) | |
2403 | { | |
4b02b524 | 2404 | int rc; |
1da177e4 LT |
2405 | int len; |
2406 | ||
0804d113 | 2407 | read_lock(&policy_rwlock); |
1da177e4 | 2408 | |
4b02b524 | 2409 | rc = -EFAULT; |
1da177e4 | 2410 | len = policydb.p_bools.nprim; |
4b02b524 | 2411 | if (bool >= len) |
1da177e4 | 2412 | goto out; |
1da177e4 LT |
2413 | |
2414 | rc = policydb.bool_val_to_struct[bool]->state; | |
2415 | out: | |
0804d113 | 2416 | read_unlock(&policy_rwlock); |
1da177e4 LT |
2417 | return rc; |
2418 | } | |
376bd9cb | 2419 | |
e900a7d9 SS |
2420 | static int security_preserve_bools(struct policydb *p) |
2421 | { | |
2422 | int rc, nbools = 0, *bvalues = NULL, i; | |
2423 | char **bnames = NULL; | |
2424 | struct cond_bool_datum *booldatum; | |
2425 | struct cond_node *cur; | |
2426 | ||
2427 | rc = security_get_bools(&nbools, &bnames, &bvalues); | |
2428 | if (rc) | |
2429 | goto out; | |
2430 | for (i = 0; i < nbools; i++) { | |
2431 | booldatum = hashtab_search(p->p_bools.table, bnames[i]); | |
2432 | if (booldatum) | |
2433 | booldatum->state = bvalues[i]; | |
2434 | } | |
dbc74c65 | 2435 | for (cur = p->cond_list; cur; cur = cur->next) { |
e900a7d9 SS |
2436 | rc = evaluate_cond_node(p, cur); |
2437 | if (rc) | |
2438 | goto out; | |
2439 | } | |
2440 | ||
2441 | out: | |
2442 | if (bnames) { | |
2443 | for (i = 0; i < nbools; i++) | |
2444 | kfree(bnames[i]); | |
2445 | } | |
2446 | kfree(bnames); | |
2447 | kfree(bvalues); | |
2448 | return rc; | |
2449 | } | |
2450 | ||
08554d6b VY |
2451 | /* |
2452 | * security_sid_mls_copy() - computes a new sid based on the given | |
2453 | * sid and the mls portion of mls_sid. | |
2454 | */ | |
2455 | int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid) | |
2456 | { | |
2457 | struct context *context1; | |
2458 | struct context *context2; | |
2459 | struct context newcon; | |
2460 | char *s; | |
2461 | u32 len; | |
4b02b524 | 2462 | int rc; |
08554d6b | 2463 | |
4b02b524 | 2464 | rc = 0; |
0719aaf5 | 2465 | if (!ss_initialized || !policydb.mls_enabled) { |
08554d6b VY |
2466 | *new_sid = sid; |
2467 | goto out; | |
2468 | } | |
2469 | ||
2470 | context_init(&newcon); | |
2471 | ||
0804d113 | 2472 | read_lock(&policy_rwlock); |
4b02b524 EP |
2473 | |
2474 | rc = -EINVAL; | |
08554d6b VY |
2475 | context1 = sidtab_search(&sidtab, sid); |
2476 | if (!context1) { | |
744ba35e EP |
2477 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
2478 | __func__, sid); | |
08554d6b VY |
2479 | goto out_unlock; |
2480 | } | |
2481 | ||
4b02b524 | 2482 | rc = -EINVAL; |
08554d6b VY |
2483 | context2 = sidtab_search(&sidtab, mls_sid); |
2484 | if (!context2) { | |
744ba35e EP |
2485 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
2486 | __func__, mls_sid); | |
08554d6b VY |
2487 | goto out_unlock; |
2488 | } | |
2489 | ||
2490 | newcon.user = context1->user; | |
2491 | newcon.role = context1->role; | |
2492 | newcon.type = context1->type; | |
0efc61ea | 2493 | rc = mls_context_cpy(&newcon, context2); |
08554d6b VY |
2494 | if (rc) |
2495 | goto out_unlock; | |
2496 | ||
08554d6b VY |
2497 | /* Check the validity of the new context. */ |
2498 | if (!policydb_context_isvalid(&policydb, &newcon)) { | |
2499 | rc = convert_context_handle_invalid_context(&newcon); | |
4b02b524 EP |
2500 | if (rc) { |
2501 | if (!context_struct_to_string(&newcon, &s, &len)) { | |
2502 | audit_log(current->audit_context, GFP_ATOMIC, AUDIT_SELINUX_ERR, | |
2503 | "security_sid_mls_copy: invalid context %s", s); | |
2504 | kfree(s); | |
2505 | } | |
2506 | goto out_unlock; | |
2507 | } | |
08554d6b VY |
2508 | } |
2509 | ||
2510 | rc = sidtab_context_to_sid(&sidtab, &newcon, new_sid); | |
08554d6b | 2511 | out_unlock: |
0804d113 | 2512 | read_unlock(&policy_rwlock); |
08554d6b VY |
2513 | context_destroy(&newcon); |
2514 | out: | |
2515 | return rc; | |
2516 | } | |
2517 | ||
220deb96 PM |
2518 | /** |
2519 | * security_net_peersid_resolve - Compare and resolve two network peer SIDs | |
2520 | * @nlbl_sid: NetLabel SID | |
2521 | * @nlbl_type: NetLabel labeling protocol type | |
2522 | * @xfrm_sid: XFRM SID | |
2523 | * | |
2524 | * Description: | |
2525 | * Compare the @nlbl_sid and @xfrm_sid values and if the two SIDs can be | |
2526 | * resolved into a single SID it is returned via @peer_sid and the function | |
2527 | * returns zero. Otherwise @peer_sid is set to SECSID_NULL and the function | |
2528 | * returns a negative value. A table summarizing the behavior is below: | |
2529 | * | |
2530 | * | function return | @sid | |
2531 | * ------------------------------+-----------------+----------------- | |
2532 | * no peer labels | 0 | SECSID_NULL | |
2533 | * single peer label | 0 | <peer_label> | |
2534 | * multiple, consistent labels | 0 | <peer_label> | |
2535 | * multiple, inconsistent labels | -<errno> | SECSID_NULL | |
2536 | * | |
2537 | */ | |
2538 | int security_net_peersid_resolve(u32 nlbl_sid, u32 nlbl_type, | |
2539 | u32 xfrm_sid, | |
2540 | u32 *peer_sid) | |
2541 | { | |
2542 | int rc; | |
2543 | struct context *nlbl_ctx; | |
2544 | struct context *xfrm_ctx; | |
2545 | ||
4b02b524 EP |
2546 | *peer_sid = SECSID_NULL; |
2547 | ||
220deb96 PM |
2548 | /* handle the common (which also happens to be the set of easy) cases |
2549 | * right away, these two if statements catch everything involving a | |
2550 | * single or absent peer SID/label */ | |
2551 | if (xfrm_sid == SECSID_NULL) { | |
2552 | *peer_sid = nlbl_sid; | |
2553 | return 0; | |
2554 | } | |
2555 | /* NOTE: an nlbl_type == NETLBL_NLTYPE_UNLABELED is a "fallback" label | |
2556 | * and is treated as if nlbl_sid == SECSID_NULL when a XFRM SID/label | |
2557 | * is present */ | |
2558 | if (nlbl_sid == SECSID_NULL || nlbl_type == NETLBL_NLTYPE_UNLABELED) { | |
2559 | *peer_sid = xfrm_sid; | |
2560 | return 0; | |
2561 | } | |
2562 | ||
2563 | /* we don't need to check ss_initialized here since the only way both | |
2564 | * nlbl_sid and xfrm_sid are not equal to SECSID_NULL would be if the | |
2565 | * security server was initialized and ss_initialized was true */ | |
4b02b524 | 2566 | if (!policydb.mls_enabled) |
220deb96 | 2567 | return 0; |
220deb96 | 2568 | |
0804d113 | 2569 | read_lock(&policy_rwlock); |
220deb96 | 2570 | |
4b02b524 | 2571 | rc = -EINVAL; |
220deb96 PM |
2572 | nlbl_ctx = sidtab_search(&sidtab, nlbl_sid); |
2573 | if (!nlbl_ctx) { | |
744ba35e EP |
2574 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
2575 | __func__, nlbl_sid); | |
4b02b524 | 2576 | goto out; |
220deb96 | 2577 | } |
4b02b524 | 2578 | rc = -EINVAL; |
220deb96 PM |
2579 | xfrm_ctx = sidtab_search(&sidtab, xfrm_sid); |
2580 | if (!xfrm_ctx) { | |
744ba35e EP |
2581 | printk(KERN_ERR "SELinux: %s: unrecognized SID %d\n", |
2582 | __func__, xfrm_sid); | |
4b02b524 | 2583 | goto out; |
220deb96 PM |
2584 | } |
2585 | rc = (mls_context_cmp(nlbl_ctx, xfrm_ctx) ? 0 : -EACCES); | |
4b02b524 EP |
2586 | if (rc) |
2587 | goto out; | |
220deb96 | 2588 | |
4b02b524 EP |
2589 | /* at present NetLabel SIDs/labels really only carry MLS |
2590 | * information so if the MLS portion of the NetLabel SID | |
2591 | * matches the MLS portion of the labeled XFRM SID/label | |
2592 | * then pass along the XFRM SID as it is the most | |
2593 | * expressive */ | |
2594 | *peer_sid = xfrm_sid; | |
2595 | out: | |
0804d113 | 2596 | read_unlock(&policy_rwlock); |
220deb96 PM |
2597 | return rc; |
2598 | } | |
2599 | ||
55fcf09b CP |
2600 | static int get_classes_callback(void *k, void *d, void *args) |
2601 | { | |
2602 | struct class_datum *datum = d; | |
2603 | char *name = k, **classes = args; | |
2604 | int value = datum->value - 1; | |
2605 | ||
2606 | classes[value] = kstrdup(name, GFP_ATOMIC); | |
2607 | if (!classes[value]) | |
2608 | return -ENOMEM; | |
2609 | ||
2610 | return 0; | |
2611 | } | |
2612 | ||
2613 | int security_get_classes(char ***classes, int *nclasses) | |
2614 | { | |
4b02b524 | 2615 | int rc; |
55fcf09b | 2616 | |
0804d113 | 2617 | read_lock(&policy_rwlock); |
55fcf09b | 2618 | |
4b02b524 | 2619 | rc = -ENOMEM; |
55fcf09b | 2620 | *nclasses = policydb.p_classes.nprim; |
9f59f90b | 2621 | *classes = kcalloc(*nclasses, sizeof(**classes), GFP_ATOMIC); |
55fcf09b CP |
2622 | if (!*classes) |
2623 | goto out; | |
2624 | ||
2625 | rc = hashtab_map(policydb.p_classes.table, get_classes_callback, | |
2626 | *classes); | |
4b02b524 | 2627 | if (rc) { |
55fcf09b CP |
2628 | int i; |
2629 | for (i = 0; i < *nclasses; i++) | |
2630 | kfree((*classes)[i]); | |
2631 | kfree(*classes); | |
2632 | } | |
2633 | ||
2634 | out: | |
0804d113 | 2635 | read_unlock(&policy_rwlock); |
55fcf09b CP |
2636 | return rc; |
2637 | } | |
2638 | ||
2639 | static int get_permissions_callback(void *k, void *d, void *args) | |
2640 | { | |
2641 | struct perm_datum *datum = d; | |
2642 | char *name = k, **perms = args; | |
2643 | int value = datum->value - 1; | |
2644 | ||
2645 | perms[value] = kstrdup(name, GFP_ATOMIC); | |
2646 | if (!perms[value]) | |
2647 | return -ENOMEM; | |
2648 | ||
2649 | return 0; | |
2650 | } | |
2651 | ||
2652 | int security_get_permissions(char *class, char ***perms, int *nperms) | |
2653 | { | |
4b02b524 | 2654 | int rc, i; |
55fcf09b CP |
2655 | struct class_datum *match; |
2656 | ||
0804d113 | 2657 | read_lock(&policy_rwlock); |
55fcf09b | 2658 | |
4b02b524 | 2659 | rc = -EINVAL; |
55fcf09b CP |
2660 | match = hashtab_search(policydb.p_classes.table, class); |
2661 | if (!match) { | |
744ba35e | 2662 | printk(KERN_ERR "SELinux: %s: unrecognized class %s\n", |
dd6f953a | 2663 | __func__, class); |
55fcf09b CP |
2664 | goto out; |
2665 | } | |
2666 | ||
4b02b524 | 2667 | rc = -ENOMEM; |
55fcf09b | 2668 | *nperms = match->permissions.nprim; |
9f59f90b | 2669 | *perms = kcalloc(*nperms, sizeof(**perms), GFP_ATOMIC); |
55fcf09b CP |
2670 | if (!*perms) |
2671 | goto out; | |
2672 | ||
2673 | if (match->comdatum) { | |
2674 | rc = hashtab_map(match->comdatum->permissions.table, | |
2675 | get_permissions_callback, *perms); | |
4b02b524 | 2676 | if (rc) |
55fcf09b CP |
2677 | goto err; |
2678 | } | |
2679 | ||
2680 | rc = hashtab_map(match->permissions.table, get_permissions_callback, | |
2681 | *perms); | |
4b02b524 | 2682 | if (rc) |
55fcf09b CP |
2683 | goto err; |
2684 | ||
2685 | out: | |
0804d113 | 2686 | read_unlock(&policy_rwlock); |
55fcf09b CP |
2687 | return rc; |
2688 | ||
2689 | err: | |
0804d113 | 2690 | read_unlock(&policy_rwlock); |
55fcf09b CP |
2691 | for (i = 0; i < *nperms; i++) |
2692 | kfree((*perms)[i]); | |
2693 | kfree(*perms); | |
2694 | return rc; | |
2695 | } | |
2696 | ||
3f12070e EP |
2697 | int security_get_reject_unknown(void) |
2698 | { | |
2699 | return policydb.reject_unknown; | |
2700 | } | |
2701 | ||
2702 | int security_get_allow_unknown(void) | |
2703 | { | |
2704 | return policydb.allow_unknown; | |
2705 | } | |
2706 | ||
3bb56b25 PM |
2707 | /** |
2708 | * security_policycap_supported - Check for a specific policy capability | |
2709 | * @req_cap: capability | |
2710 | * | |
2711 | * Description: | |
2712 | * This function queries the currently loaded policy to see if it supports the | |
2713 | * capability specified by @req_cap. Returns true (1) if the capability is | |
2714 | * supported, false (0) if it isn't supported. | |
2715 | * | |
2716 | */ | |
2717 | int security_policycap_supported(unsigned int req_cap) | |
2718 | { | |
2719 | int rc; | |
2720 | ||
0804d113 | 2721 | read_lock(&policy_rwlock); |
3bb56b25 | 2722 | rc = ebitmap_get_bit(&policydb.policycaps, req_cap); |
0804d113 | 2723 | read_unlock(&policy_rwlock); |
3bb56b25 PM |
2724 | |
2725 | return rc; | |
2726 | } | |
2727 | ||
376bd9cb DG |
2728 | struct selinux_audit_rule { |
2729 | u32 au_seqno; | |
2730 | struct context au_ctxt; | |
2731 | }; | |
2732 | ||
9d57a7f9 | 2733 | void selinux_audit_rule_free(void *vrule) |
376bd9cb | 2734 | { |
9d57a7f9 AD |
2735 | struct selinux_audit_rule *rule = vrule; |
2736 | ||
376bd9cb DG |
2737 | if (rule) { |
2738 | context_destroy(&rule->au_ctxt); | |
2739 | kfree(rule); | |
2740 | } | |
2741 | } | |
2742 | ||
9d57a7f9 | 2743 | int selinux_audit_rule_init(u32 field, u32 op, char *rulestr, void **vrule) |
376bd9cb DG |
2744 | { |
2745 | struct selinux_audit_rule *tmprule; | |
2746 | struct role_datum *roledatum; | |
2747 | struct type_datum *typedatum; | |
2748 | struct user_datum *userdatum; | |
9d57a7f9 | 2749 | struct selinux_audit_rule **rule = (struct selinux_audit_rule **)vrule; |
376bd9cb DG |
2750 | int rc = 0; |
2751 | ||
2752 | *rule = NULL; | |
2753 | ||
2754 | if (!ss_initialized) | |
3ad40d64 | 2755 | return -EOPNOTSUPP; |
376bd9cb DG |
2756 | |
2757 | switch (field) { | |
3a6b9f85 DG |
2758 | case AUDIT_SUBJ_USER: |
2759 | case AUDIT_SUBJ_ROLE: | |
2760 | case AUDIT_SUBJ_TYPE: | |
6e5a2d1d DG |
2761 | case AUDIT_OBJ_USER: |
2762 | case AUDIT_OBJ_ROLE: | |
2763 | case AUDIT_OBJ_TYPE: | |
376bd9cb | 2764 | /* only 'equals' and 'not equals' fit user, role, and type */ |
5af75d8d | 2765 | if (op != Audit_equal && op != Audit_not_equal) |
376bd9cb DG |
2766 | return -EINVAL; |
2767 | break; | |
3a6b9f85 DG |
2768 | case AUDIT_SUBJ_SEN: |
2769 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
2770 | case AUDIT_OBJ_LEV_LOW: |
2771 | case AUDIT_OBJ_LEV_HIGH: | |
376bd9cb DG |
2772 | /* we do not allow a range, indicated by the presense of '-' */ |
2773 | if (strchr(rulestr, '-')) | |
2774 | return -EINVAL; | |
2775 | break; | |
2776 | default: | |
2777 | /* only the above fields are valid */ | |
2778 | return -EINVAL; | |
2779 | } | |
2780 | ||
2781 | tmprule = kzalloc(sizeof(struct selinux_audit_rule), GFP_KERNEL); | |
2782 | if (!tmprule) | |
2783 | return -ENOMEM; | |
2784 | ||
2785 | context_init(&tmprule->au_ctxt); | |
2786 | ||
0804d113 | 2787 | read_lock(&policy_rwlock); |
376bd9cb DG |
2788 | |
2789 | tmprule->au_seqno = latest_granting; | |
2790 | ||
2791 | switch (field) { | |
3a6b9f85 | 2792 | case AUDIT_SUBJ_USER: |
6e5a2d1d | 2793 | case AUDIT_OBJ_USER: |
4b02b524 | 2794 | rc = -EINVAL; |
376bd9cb DG |
2795 | userdatum = hashtab_search(policydb.p_users.table, rulestr); |
2796 | if (!userdatum) | |
4b02b524 EP |
2797 | goto out; |
2798 | tmprule->au_ctxt.user = userdatum->value; | |
376bd9cb | 2799 | break; |
3a6b9f85 | 2800 | case AUDIT_SUBJ_ROLE: |
6e5a2d1d | 2801 | case AUDIT_OBJ_ROLE: |
4b02b524 | 2802 | rc = -EINVAL; |
376bd9cb DG |
2803 | roledatum = hashtab_search(policydb.p_roles.table, rulestr); |
2804 | if (!roledatum) | |
4b02b524 EP |
2805 | goto out; |
2806 | tmprule->au_ctxt.role = roledatum->value; | |
376bd9cb | 2807 | break; |
3a6b9f85 | 2808 | case AUDIT_SUBJ_TYPE: |
6e5a2d1d | 2809 | case AUDIT_OBJ_TYPE: |
4b02b524 | 2810 | rc = -EINVAL; |
376bd9cb DG |
2811 | typedatum = hashtab_search(policydb.p_types.table, rulestr); |
2812 | if (!typedatum) | |
4b02b524 EP |
2813 | goto out; |
2814 | tmprule->au_ctxt.type = typedatum->value; | |
376bd9cb | 2815 | break; |
3a6b9f85 DG |
2816 | case AUDIT_SUBJ_SEN: |
2817 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
2818 | case AUDIT_OBJ_LEV_LOW: |
2819 | case AUDIT_OBJ_LEV_HIGH: | |
376bd9cb | 2820 | rc = mls_from_string(rulestr, &tmprule->au_ctxt, GFP_ATOMIC); |
4b02b524 EP |
2821 | if (rc) |
2822 | goto out; | |
376bd9cb DG |
2823 | break; |
2824 | } | |
4b02b524 EP |
2825 | rc = 0; |
2826 | out: | |
0804d113 | 2827 | read_unlock(&policy_rwlock); |
376bd9cb DG |
2828 | |
2829 | if (rc) { | |
2830 | selinux_audit_rule_free(tmprule); | |
2831 | tmprule = NULL; | |
2832 | } | |
2833 | ||
2834 | *rule = tmprule; | |
2835 | ||
2836 | return rc; | |
2837 | } | |
2838 | ||
9d57a7f9 AD |
2839 | /* Check to see if the rule contains any selinux fields */ |
2840 | int selinux_audit_rule_known(struct audit_krule *rule) | |
2841 | { | |
2842 | int i; | |
2843 | ||
2844 | for (i = 0; i < rule->field_count; i++) { | |
2845 | struct audit_field *f = &rule->fields[i]; | |
2846 | switch (f->type) { | |
2847 | case AUDIT_SUBJ_USER: | |
2848 | case AUDIT_SUBJ_ROLE: | |
2849 | case AUDIT_SUBJ_TYPE: | |
2850 | case AUDIT_SUBJ_SEN: | |
2851 | case AUDIT_SUBJ_CLR: | |
2852 | case AUDIT_OBJ_USER: | |
2853 | case AUDIT_OBJ_ROLE: | |
2854 | case AUDIT_OBJ_TYPE: | |
2855 | case AUDIT_OBJ_LEV_LOW: | |
2856 | case AUDIT_OBJ_LEV_HIGH: | |
2857 | return 1; | |
2858 | } | |
2859 | } | |
2860 | ||
2861 | return 0; | |
2862 | } | |
2863 | ||
2864 | int selinux_audit_rule_match(u32 sid, u32 field, u32 op, void *vrule, | |
f5269710 | 2865 | struct audit_context *actx) |
376bd9cb DG |
2866 | { |
2867 | struct context *ctxt; | |
2868 | struct mls_level *level; | |
9d57a7f9 | 2869 | struct selinux_audit_rule *rule = vrule; |
376bd9cb DG |
2870 | int match = 0; |
2871 | ||
2872 | if (!rule) { | |
2873 | audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, | |
5d55a345 | 2874 | "selinux_audit_rule_match: missing rule\n"); |
376bd9cb DG |
2875 | return -ENOENT; |
2876 | } | |
2877 | ||
0804d113 | 2878 | read_lock(&policy_rwlock); |
376bd9cb DG |
2879 | |
2880 | if (rule->au_seqno < latest_granting) { | |
2881 | audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, | |
5d55a345 | 2882 | "selinux_audit_rule_match: stale rule\n"); |
376bd9cb DG |
2883 | match = -ESTALE; |
2884 | goto out; | |
2885 | } | |
2886 | ||
9a2f44f0 | 2887 | ctxt = sidtab_search(&sidtab, sid); |
376bd9cb DG |
2888 | if (!ctxt) { |
2889 | audit_log(actx, GFP_ATOMIC, AUDIT_SELINUX_ERR, | |
5d55a345 EP |
2890 | "selinux_audit_rule_match: unrecognized SID %d\n", |
2891 | sid); | |
376bd9cb DG |
2892 | match = -ENOENT; |
2893 | goto out; | |
2894 | } | |
2895 | ||
2896 | /* a field/op pair that is not caught here will simply fall through | |
2897 | without a match */ | |
2898 | switch (field) { | |
3a6b9f85 | 2899 | case AUDIT_SUBJ_USER: |
6e5a2d1d | 2900 | case AUDIT_OBJ_USER: |
376bd9cb | 2901 | switch (op) { |
5af75d8d | 2902 | case Audit_equal: |
376bd9cb DG |
2903 | match = (ctxt->user == rule->au_ctxt.user); |
2904 | break; | |
5af75d8d | 2905 | case Audit_not_equal: |
376bd9cb DG |
2906 | match = (ctxt->user != rule->au_ctxt.user); |
2907 | break; | |
2908 | } | |
2909 | break; | |
3a6b9f85 | 2910 | case AUDIT_SUBJ_ROLE: |
6e5a2d1d | 2911 | case AUDIT_OBJ_ROLE: |
376bd9cb | 2912 | switch (op) { |
5af75d8d | 2913 | case Audit_equal: |
376bd9cb DG |
2914 | match = (ctxt->role == rule->au_ctxt.role); |
2915 | break; | |
5af75d8d | 2916 | case Audit_not_equal: |
376bd9cb DG |
2917 | match = (ctxt->role != rule->au_ctxt.role); |
2918 | break; | |
2919 | } | |
2920 | break; | |
3a6b9f85 | 2921 | case AUDIT_SUBJ_TYPE: |
6e5a2d1d | 2922 | case AUDIT_OBJ_TYPE: |
376bd9cb | 2923 | switch (op) { |
5af75d8d | 2924 | case Audit_equal: |
376bd9cb DG |
2925 | match = (ctxt->type == rule->au_ctxt.type); |
2926 | break; | |
5af75d8d | 2927 | case Audit_not_equal: |
376bd9cb DG |
2928 | match = (ctxt->type != rule->au_ctxt.type); |
2929 | break; | |
2930 | } | |
2931 | break; | |
3a6b9f85 DG |
2932 | case AUDIT_SUBJ_SEN: |
2933 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
2934 | case AUDIT_OBJ_LEV_LOW: |
2935 | case AUDIT_OBJ_LEV_HIGH: | |
2936 | level = ((field == AUDIT_SUBJ_SEN || | |
5d55a345 EP |
2937 | field == AUDIT_OBJ_LEV_LOW) ? |
2938 | &ctxt->range.level[0] : &ctxt->range.level[1]); | |
376bd9cb | 2939 | switch (op) { |
5af75d8d | 2940 | case Audit_equal: |
376bd9cb | 2941 | match = mls_level_eq(&rule->au_ctxt.range.level[0], |
5d55a345 | 2942 | level); |
376bd9cb | 2943 | break; |
5af75d8d | 2944 | case Audit_not_equal: |
376bd9cb | 2945 | match = !mls_level_eq(&rule->au_ctxt.range.level[0], |
5d55a345 | 2946 | level); |
376bd9cb | 2947 | break; |
5af75d8d | 2948 | case Audit_lt: |
376bd9cb | 2949 | match = (mls_level_dom(&rule->au_ctxt.range.level[0], |
5d55a345 EP |
2950 | level) && |
2951 | !mls_level_eq(&rule->au_ctxt.range.level[0], | |
2952 | level)); | |
376bd9cb | 2953 | break; |
5af75d8d | 2954 | case Audit_le: |
376bd9cb | 2955 | match = mls_level_dom(&rule->au_ctxt.range.level[0], |
5d55a345 | 2956 | level); |
376bd9cb | 2957 | break; |
5af75d8d | 2958 | case Audit_gt: |
376bd9cb | 2959 | match = (mls_level_dom(level, |
5d55a345 EP |
2960 | &rule->au_ctxt.range.level[0]) && |
2961 | !mls_level_eq(level, | |
2962 | &rule->au_ctxt.range.level[0])); | |
376bd9cb | 2963 | break; |
5af75d8d | 2964 | case Audit_ge: |
376bd9cb | 2965 | match = mls_level_dom(level, |
5d55a345 | 2966 | &rule->au_ctxt.range.level[0]); |
376bd9cb DG |
2967 | break; |
2968 | } | |
2969 | } | |
2970 | ||
2971 | out: | |
0804d113 | 2972 | read_unlock(&policy_rwlock); |
376bd9cb DG |
2973 | return match; |
2974 | } | |
2975 | ||
9d57a7f9 | 2976 | static int (*aurule_callback)(void) = audit_update_lsm_rules; |
376bd9cb DG |
2977 | |
2978 | static int aurule_avc_callback(u32 event, u32 ssid, u32 tsid, | |
f5269710 | 2979 | u16 class, u32 perms, u32 *retained) |
376bd9cb DG |
2980 | { |
2981 | int err = 0; | |
2982 | ||
2983 | if (event == AVC_CALLBACK_RESET && aurule_callback) | |
2984 | err = aurule_callback(); | |
2985 | return err; | |
2986 | } | |
2987 | ||
2988 | static int __init aurule_init(void) | |
2989 | { | |
2990 | int err; | |
2991 | ||
2992 | err = avc_add_callback(aurule_avc_callback, AVC_CALLBACK_RESET, | |
5d55a345 | 2993 | SECSID_NULL, SECSID_NULL, SECCLASS_NULL, 0); |
376bd9cb DG |
2994 | if (err) |
2995 | panic("avc_add_callback() failed, error %d\n", err); | |
2996 | ||
2997 | return err; | |
2998 | } | |
2999 | __initcall(aurule_init); | |
3000 | ||
7420ed23 | 3001 | #ifdef CONFIG_NETLABEL |
7420ed23 | 3002 | /** |
5778eabd PM |
3003 | * security_netlbl_cache_add - Add an entry to the NetLabel cache |
3004 | * @secattr: the NetLabel packet security attributes | |
5dbe1eb0 | 3005 | * @sid: the SELinux SID |
7420ed23 VY |
3006 | * |
3007 | * Description: | |
3008 | * Attempt to cache the context in @ctx, which was derived from the packet in | |
5778eabd PM |
3009 | * @skb, in the NetLabel subsystem cache. This function assumes @secattr has |
3010 | * already been initialized. | |
7420ed23 VY |
3011 | * |
3012 | */ | |
5778eabd | 3013 | static void security_netlbl_cache_add(struct netlbl_lsm_secattr *secattr, |
5dbe1eb0 | 3014 | u32 sid) |
7420ed23 | 3015 | { |
5dbe1eb0 | 3016 | u32 *sid_cache; |
7420ed23 | 3017 | |
5dbe1eb0 PM |
3018 | sid_cache = kmalloc(sizeof(*sid_cache), GFP_ATOMIC); |
3019 | if (sid_cache == NULL) | |
5778eabd | 3020 | return; |
5dbe1eb0 PM |
3021 | secattr->cache = netlbl_secattr_cache_alloc(GFP_ATOMIC); |
3022 | if (secattr->cache == NULL) { | |
3023 | kfree(sid_cache); | |
5778eabd | 3024 | return; |
0ec8abd7 | 3025 | } |
7420ed23 | 3026 | |
5dbe1eb0 PM |
3027 | *sid_cache = sid; |
3028 | secattr->cache->free = kfree; | |
3029 | secattr->cache->data = sid_cache; | |
5778eabd | 3030 | secattr->flags |= NETLBL_SECATTR_CACHE; |
7420ed23 VY |
3031 | } |
3032 | ||
3033 | /** | |
5778eabd | 3034 | * security_netlbl_secattr_to_sid - Convert a NetLabel secattr to a SELinux SID |
7420ed23 | 3035 | * @secattr: the NetLabel packet security attributes |
7420ed23 VY |
3036 | * @sid: the SELinux SID |
3037 | * | |
3038 | * Description: | |
5778eabd | 3039 | * Convert the given NetLabel security attributes in @secattr into a |
7420ed23 | 3040 | * SELinux SID. If the @secattr field does not contain a full SELinux |
5dbe1eb0 PM |
3041 | * SID/context then use SECINITSID_NETMSG as the foundation. If possibile the |
3042 | * 'cache' field of @secattr is set and the CACHE flag is set; this is to | |
3043 | * allow the @secattr to be used by NetLabel to cache the secattr to SID | |
3044 | * conversion for future lookups. Returns zero on success, negative values on | |
3045 | * failure. | |
7420ed23 VY |
3046 | * |
3047 | */ | |
5778eabd | 3048 | int security_netlbl_secattr_to_sid(struct netlbl_lsm_secattr *secattr, |
5778eabd | 3049 | u32 *sid) |
7420ed23 | 3050 | { |
7ae9f23c | 3051 | int rc; |
7420ed23 VY |
3052 | struct context *ctx; |
3053 | struct context ctx_new; | |
5778eabd PM |
3054 | |
3055 | if (!ss_initialized) { | |
3056 | *sid = SECSID_NULL; | |
3057 | return 0; | |
3058 | } | |
7420ed23 | 3059 | |
0804d113 | 3060 | read_lock(&policy_rwlock); |
7420ed23 | 3061 | |
7ae9f23c | 3062 | if (secattr->flags & NETLBL_SECATTR_CACHE) |
5dbe1eb0 | 3063 | *sid = *(u32 *)secattr->cache->data; |
7ae9f23c | 3064 | else if (secattr->flags & NETLBL_SECATTR_SECID) |
16efd454 | 3065 | *sid = secattr->attr.secid; |
7ae9f23c EP |
3066 | else if (secattr->flags & NETLBL_SECATTR_MLS_LVL) { |
3067 | rc = -EIDRM; | |
5dbe1eb0 | 3068 | ctx = sidtab_search(&sidtab, SECINITSID_NETMSG); |
7420ed23 | 3069 | if (ctx == NULL) |
7ae9f23c | 3070 | goto out; |
7420ed23 | 3071 | |
81990fbd | 3072 | context_init(&ctx_new); |
7420ed23 VY |
3073 | ctx_new.user = ctx->user; |
3074 | ctx_new.role = ctx->role; | |
3075 | ctx_new.type = ctx->type; | |
02752760 | 3076 | mls_import_netlbl_lvl(&ctx_new, secattr); |
701a90ba | 3077 | if (secattr->flags & NETLBL_SECATTR_MLS_CAT) { |
7ae9f23c EP |
3078 | rc = ebitmap_netlbl_import(&ctx_new.range.level[0].cat, |
3079 | secattr->attr.mls.cat); | |
3080 | if (rc) | |
3081 | goto out; | |
81990fbd PM |
3082 | memcpy(&ctx_new.range.level[1].cat, |
3083 | &ctx_new.range.level[0].cat, | |
3084 | sizeof(ctx_new.range.level[0].cat)); | |
7420ed23 | 3085 | } |
7ae9f23c EP |
3086 | rc = -EIDRM; |
3087 | if (!mls_context_isvalid(&policydb, &ctx_new)) | |
3088 | goto out_free; | |
7420ed23 VY |
3089 | |
3090 | rc = sidtab_context_to_sid(&sidtab, &ctx_new, sid); | |
7ae9f23c EP |
3091 | if (rc) |
3092 | goto out_free; | |
7420ed23 | 3093 | |
5dbe1eb0 | 3094 | security_netlbl_cache_add(secattr, *sid); |
5778eabd | 3095 | |
7420ed23 | 3096 | ebitmap_destroy(&ctx_new.range.level[0].cat); |
7ae9f23c | 3097 | } else |
388b2405 | 3098 | *sid = SECSID_NULL; |
7420ed23 | 3099 | |
0804d113 | 3100 | read_unlock(&policy_rwlock); |
7ae9f23c EP |
3101 | return 0; |
3102 | out_free: | |
7420ed23 | 3103 | ebitmap_destroy(&ctx_new.range.level[0].cat); |
7ae9f23c EP |
3104 | out: |
3105 | read_unlock(&policy_rwlock); | |
3106 | return rc; | |
7420ed23 VY |
3107 | } |
3108 | ||
3109 | /** | |
5778eabd PM |
3110 | * security_netlbl_sid_to_secattr - Convert a SELinux SID to a NetLabel secattr |
3111 | * @sid: the SELinux SID | |
3112 | * @secattr: the NetLabel packet security attributes | |
7420ed23 VY |
3113 | * |
3114 | * Description: | |
5778eabd PM |
3115 | * Convert the given SELinux SID in @sid into a NetLabel security attribute. |
3116 | * Returns zero on success, negative values on failure. | |
7420ed23 VY |
3117 | * |
3118 | */ | |
5778eabd | 3119 | int security_netlbl_sid_to_secattr(u32 sid, struct netlbl_lsm_secattr *secattr) |
7420ed23 | 3120 | { |
99d854d2 | 3121 | int rc; |
7420ed23 VY |
3122 | struct context *ctx; |
3123 | ||
3124 | if (!ss_initialized) | |
3125 | return 0; | |
3126 | ||
0804d113 | 3127 | read_lock(&policy_rwlock); |
4b02b524 EP |
3128 | |
3129 | rc = -ENOENT; | |
7420ed23 | 3130 | ctx = sidtab_search(&sidtab, sid); |
4b02b524 EP |
3131 | if (ctx == NULL) |
3132 | goto out; | |
3133 | ||
3134 | rc = -ENOMEM; | |
ac76c05b | 3135 | secattr->domain = kstrdup(sym_name(&policydb, SYM_TYPES, ctx->type - 1), |
5778eabd | 3136 | GFP_ATOMIC); |
4b02b524 EP |
3137 | if (secattr->domain == NULL) |
3138 | goto out; | |
3139 | ||
8d75899d PM |
3140 | secattr->attr.secid = sid; |
3141 | secattr->flags |= NETLBL_SECATTR_DOMAIN_CPY | NETLBL_SECATTR_SECID; | |
5778eabd PM |
3142 | mls_export_netlbl_lvl(ctx, secattr); |
3143 | rc = mls_export_netlbl_cat(ctx, secattr); | |
4b02b524 | 3144 | out: |
0804d113 | 3145 | read_unlock(&policy_rwlock); |
f8687afe PM |
3146 | return rc; |
3147 | } | |
7420ed23 | 3148 | #endif /* CONFIG_NETLABEL */ |
cee74f47 EP |
3149 | |
3150 | /** | |
3151 | * security_read_policy - read the policy. | |
3152 | * @data: binary policy data | |
3153 | * @len: length of data in bytes | |
3154 | * | |
3155 | */ | |
3156 | int security_read_policy(void **data, ssize_t *len) | |
3157 | { | |
3158 | int rc; | |
3159 | struct policy_file fp; | |
3160 | ||
3161 | if (!ss_initialized) | |
3162 | return -EINVAL; | |
3163 | ||
3164 | *len = security_policydb_len(); | |
3165 | ||
845ca30f | 3166 | *data = vmalloc_user(*len); |
cee74f47 EP |
3167 | if (!*data) |
3168 | return -ENOMEM; | |
3169 | ||
3170 | fp.data = *data; | |
3171 | fp.len = *len; | |
3172 | ||
3173 | read_lock(&policy_rwlock); | |
3174 | rc = policydb_write(&policydb, &fp); | |
3175 | read_unlock(&policy_rwlock); | |
3176 | ||
3177 | if (rc) | |
3178 | return rc; | |
3179 | ||
3180 | *len = (unsigned long)fp.data - (unsigned long)*data; | |
3181 | return 0; | |
3182 | ||
3183 | } |