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