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