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