]>
Commit | Line | Data |
---|---|---|
898127c3 JJ |
1 | /* |
2 | * AppArmor security module | |
3 | * | |
4 | * This file contains AppArmor policy attachment and domain transitions | |
5 | * | |
6 | * Copyright (C) 2002-2008 Novell/SUSE | |
7 | * Copyright 2009-2010 Canonical Ltd. | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License as | |
11 | * published by the Free Software Foundation, version 2 of the | |
12 | * License. | |
13 | */ | |
14 | ||
15 | #include <linux/errno.h> | |
16 | #include <linux/fdtable.h> | |
17 | #include <linux/file.h> | |
18 | #include <linux/mount.h> | |
19 | #include <linux/syscalls.h> | |
20 | #include <linux/tracehook.h> | |
21 | #include <linux/personality.h> | |
22 | ||
23 | #include "include/audit.h" | |
24 | #include "include/apparmorfs.h" | |
25 | #include "include/context.h" | |
26 | #include "include/domain.h" | |
27 | #include "include/file.h" | |
28 | #include "include/ipc.h" | |
29 | #include "include/match.h" | |
30 | #include "include/path.h" | |
31 | #include "include/policy.h" | |
32 | ||
33 | /** | |
34 | * aa_free_domain_entries - free entries in a domain table | |
35 | * @domain: the domain table to free (MAYBE NULL) | |
36 | */ | |
37 | void aa_free_domain_entries(struct aa_domain *domain) | |
38 | { | |
39 | int i; | |
40 | if (domain) { | |
41 | if (!domain->table) | |
42 | return; | |
43 | ||
44 | for (i = 0; i < domain->size; i++) | |
45 | kzfree(domain->table[i]); | |
46 | kzfree(domain->table); | |
47 | domain->table = NULL; | |
48 | } | |
49 | } | |
50 | ||
51 | /** | |
52 | * may_change_ptraced_domain - check if can change profile on ptraced task | |
80594fc2 JJ |
53 | * @to_label: profile to change to (NOT NULL) |
54 | * @info: message if there is an error | |
898127c3 | 55 | * |
51775fe7 | 56 | * Check if current is ptraced and if so if the tracing task is allowed |
898127c3 JJ |
57 | * to trace the new domain |
58 | * | |
59 | * Returns: %0 or error if change not allowed | |
60 | */ | |
80594fc2 JJ |
61 | static int may_change_ptraced_domain(struct aa_label *to_label, |
62 | const char **info) | |
898127c3 JJ |
63 | { |
64 | struct task_struct *tracer; | |
80594fc2 | 65 | struct aa_label *tracerl = NULL; |
898127c3 JJ |
66 | int error = 0; |
67 | ||
68 | rcu_read_lock(); | |
51775fe7 | 69 | tracer = ptrace_parent(current); |
3cfcc19e | 70 | if (tracer) |
898127c3 | 71 | /* released below */ |
80594fc2 | 72 | tracerl = aa_get_task_label(tracer); |
898127c3 JJ |
73 | |
74 | /* not ptraced */ | |
80594fc2 | 75 | if (!tracer || unconfined(tracerl)) |
898127c3 JJ |
76 | goto out; |
77 | ||
80594fc2 | 78 | error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH); |
898127c3 JJ |
79 | |
80 | out: | |
04fdc099 | 81 | rcu_read_unlock(); |
80594fc2 | 82 | aa_put_label(tracerl); |
898127c3 | 83 | |
80594fc2 JJ |
84 | if (error) |
85 | *info = "ptrace prevents transition"; | |
898127c3 JJ |
86 | return error; |
87 | } | |
88 | ||
80594fc2 JJ |
89 | /**** TODO: dedup to aa_label_match - needs perm and dfa, merging |
90 | * specifically this is an exact copy of aa_label_match except | |
91 | * aa_compute_perms is replaced with aa_compute_fperms | |
92 | * and policy.dfa with file.dfa | |
93 | ****/ | |
94 | /* match a profile and its associated ns component if needed | |
95 | * Assumes visibility test has already been done. | |
96 | * If a subns profile is not to be matched should be prescreened with | |
97 | * visibility test. | |
98 | */ | |
99 | /* match a profile and its associated ns component if needed | |
100 | * Assumes visibility test has already been done. | |
101 | * If a subns profile is not to be matched should be prescreened with | |
102 | * visibility test. | |
103 | */ | |
104 | static inline unsigned int match_component(struct aa_profile *profile, | |
105 | struct aa_profile *tp, | |
106 | bool stack, unsigned int state) | |
107 | { | |
108 | const char *ns_name; | |
109 | ||
110 | if (stack) | |
111 | state = aa_dfa_match(profile->file.dfa, state, "&"); | |
112 | if (profile->ns == tp->ns) | |
113 | return aa_dfa_match(profile->file.dfa, state, tp->base.hname); | |
114 | ||
115 | /* try matching with namespace name and then profile */ | |
116 | ns_name = aa_ns_name(profile->ns, tp->ns, true); | |
117 | state = aa_dfa_match_len(profile->file.dfa, state, ":", 1); | |
118 | state = aa_dfa_match(profile->file.dfa, state, ns_name); | |
119 | state = aa_dfa_match_len(profile->file.dfa, state, ":", 1); | |
120 | return aa_dfa_match(profile->file.dfa, state, tp->base.hname); | |
121 | } | |
122 | ||
123 | /** | |
124 | * label_component_match - find perms for full compound label | |
125 | * @profile: profile to find perms for | |
126 | * @label: label to check access permissions for | |
127 | * @stack: whether this is a stacking request | |
128 | * @start: state to start match in | |
129 | * @subns: whether to do permission checks on components in a subns | |
130 | * @request: permissions to request | |
131 | * @perms: perms struct to set | |
132 | * | |
133 | * Returns: 0 on success else ERROR | |
134 | * | |
135 | * For the label A//&B//&C this does the perm match for A//&B//&C | |
136 | * @perms should be preinitialized with allperms OR a previous permission | |
137 | * check to be stacked. | |
138 | */ | |
139 | static int label_compound_match(struct aa_profile *profile, | |
140 | struct aa_label *label, bool stack, | |
141 | unsigned int state, bool subns, u32 request, | |
142 | struct aa_perms *perms) | |
143 | { | |
144 | struct aa_profile *tp; | |
145 | struct label_it i; | |
146 | struct path_cond cond = { }; | |
147 | ||
148 | /* find first subcomponent that is visible */ | |
149 | label_for_each(i, label, tp) { | |
150 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) | |
151 | continue; | |
152 | state = match_component(profile, tp, stack, state); | |
153 | if (!state) | |
154 | goto fail; | |
155 | goto next; | |
156 | } | |
157 | ||
158 | /* no component visible */ | |
159 | *perms = allperms; | |
160 | return 0; | |
161 | ||
162 | next: | |
163 | label_for_each_cont(i, label, tp) { | |
164 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) | |
165 | continue; | |
166 | state = aa_dfa_match(profile->file.dfa, state, "//&"); | |
167 | state = match_component(profile, tp, false, state); | |
168 | if (!state) | |
169 | goto fail; | |
170 | } | |
171 | *perms = aa_compute_fperms(profile->file.dfa, state, &cond); | |
172 | aa_apply_modes_to_perms(profile, perms); | |
173 | if ((perms->allow & request) != request) | |
174 | return -EACCES; | |
175 | ||
176 | return 0; | |
177 | ||
178 | fail: | |
179 | *perms = nullperms; | |
180 | return -EACCES; | |
181 | } | |
182 | ||
183 | /** | |
184 | * label_component_match - find perms for all subcomponents of a label | |
185 | * @profile: profile to find perms for | |
186 | * @label: label to check access permissions for | |
187 | * @stack: whether this is a stacking request | |
188 | * @start: state to start match in | |
189 | * @subns: whether to do permission checks on components in a subns | |
190 | * @request: permissions to request | |
191 | * @perms: an initialized perms struct to add accumulation to | |
192 | * | |
193 | * Returns: 0 on success else ERROR | |
194 | * | |
195 | * For the label A//&B//&C this does the perm match for each of A and B and C | |
196 | * @perms should be preinitialized with allperms OR a previous permission | |
197 | * check to be stacked. | |
198 | */ | |
199 | static int label_components_match(struct aa_profile *profile, | |
200 | struct aa_label *label, bool stack, | |
201 | unsigned int start, bool subns, u32 request, | |
202 | struct aa_perms *perms) | |
203 | { | |
204 | struct aa_profile *tp; | |
205 | struct label_it i; | |
206 | struct aa_perms tmp; | |
207 | struct path_cond cond = { }; | |
208 | unsigned int state = 0; | |
209 | ||
210 | /* find first subcomponent to test */ | |
211 | label_for_each(i, label, tp) { | |
212 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) | |
213 | continue; | |
214 | state = match_component(profile, tp, stack, start); | |
215 | if (!state) | |
216 | goto fail; | |
217 | goto next; | |
218 | } | |
219 | ||
220 | /* no subcomponents visible - no change in perms */ | |
221 | return 0; | |
222 | ||
223 | next: | |
224 | tmp = aa_compute_fperms(profile->file.dfa, state, &cond); | |
225 | aa_apply_modes_to_perms(profile, &tmp); | |
226 | aa_perms_accum(perms, &tmp); | |
227 | label_for_each_cont(i, label, tp) { | |
228 | if (!aa_ns_visible(profile->ns, tp->ns, subns)) | |
229 | continue; | |
230 | state = match_component(profile, tp, stack, start); | |
231 | if (!state) | |
232 | goto fail; | |
233 | tmp = aa_compute_fperms(profile->file.dfa, state, &cond); | |
234 | aa_apply_modes_to_perms(profile, &tmp); | |
235 | aa_perms_accum(perms, &tmp); | |
236 | } | |
237 | ||
238 | if ((perms->allow & request) != request) | |
239 | return -EACCES; | |
240 | ||
241 | return 0; | |
242 | ||
243 | fail: | |
244 | *perms = nullperms; | |
245 | return -EACCES; | |
246 | } | |
247 | ||
248 | /** | |
249 | * aa_label_match - do a multi-component label match | |
250 | * @profile: profile to match against (NOT NULL) | |
251 | * @label: label to match (NOT NULL) | |
252 | * @stack: whether this is a stacking request | |
253 | * @state: state to start in | |
254 | * @subns: whether to match subns components | |
255 | * @request: permission request | |
256 | * @perms: Returns computed perms (NOT NULL) | |
257 | * | |
258 | * Returns: the state the match finished in, may be the none matching state | |
259 | */ | |
260 | static int label_match(struct aa_profile *profile, struct aa_label *label, | |
261 | bool stack, unsigned int state, bool subns, u32 request, | |
262 | struct aa_perms *perms) | |
263 | { | |
264 | int error; | |
265 | ||
266 | *perms = nullperms; | |
267 | error = label_compound_match(profile, label, stack, state, subns, | |
268 | request, perms); | |
269 | if (!error) | |
270 | return error; | |
271 | ||
272 | *perms = allperms; | |
273 | return label_components_match(profile, label, stack, state, subns, | |
274 | request, perms); | |
275 | } | |
276 | ||
277 | /******* end TODO: dedup *****/ | |
278 | ||
898127c3 JJ |
279 | /** |
280 | * change_profile_perms - find permissions for change_profile | |
281 | * @profile: the current profile (NOT NULL) | |
80594fc2 JJ |
282 | * @target: label to transition to (NOT NULL) |
283 | * @stack: whether this is a stacking request | |
898127c3 JJ |
284 | * @request: requested perms |
285 | * @start: state to start matching in | |
286 | * | |
80594fc2 | 287 | * |
898127c3 | 288 | * Returns: permission set |
80594fc2 JJ |
289 | * |
290 | * currently only matches full label A//&B//&C or individual components A, B, C | |
291 | * not arbitrary combinations. Eg. A//&B, C | |
898127c3 | 292 | */ |
80594fc2 JJ |
293 | static int change_profile_perms(struct aa_profile *profile, |
294 | struct aa_label *target, bool stack, | |
295 | u32 request, unsigned int start, | |
296 | struct aa_perms *perms) | |
898127c3 | 297 | { |
80594fc2 JJ |
298 | if (profile_unconfined(profile)) { |
299 | perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC; | |
300 | perms->audit = perms->quiet = perms->kill = 0; | |
301 | return 0; | |
898127c3 JJ |
302 | } |
303 | ||
80594fc2 JJ |
304 | /* TODO: add profile in ns screening */ |
305 | return label_match(profile, target, stack, start, true, request, perms); | |
898127c3 JJ |
306 | } |
307 | ||
308 | /** | |
309 | * __attach_match_ - find an attachment match | |
310 | * @name - to match against (NOT NULL) | |
311 | * @head - profile list to walk (NOT NULL) | |
312 | * | |
313 | * Do a linear search on the profiles in the list. There is a matching | |
314 | * preference where an exact match is preferred over a name which uses | |
315 | * expressions to match, and matching expressions with the greatest | |
316 | * xmatch_len are preferred. | |
317 | * | |
318 | * Requires: @head not be shared or have appropriate locks held | |
319 | * | |
320 | * Returns: profile or NULL if no match found | |
321 | */ | |
322 | static struct aa_profile *__attach_match(const char *name, | |
323 | struct list_head *head) | |
324 | { | |
325 | int len = 0; | |
326 | struct aa_profile *profile, *candidate = NULL; | |
327 | ||
01e2b670 | 328 | list_for_each_entry_rcu(profile, head, base.list) { |
80594fc2 | 329 | if (profile->label.flags & FLAG_NULL) |
898127c3 JJ |
330 | continue; |
331 | if (profile->xmatch && profile->xmatch_len > len) { | |
332 | unsigned int state = aa_dfa_match(profile->xmatch, | |
333 | DFA_START, name); | |
334 | u32 perm = dfa_user_allow(profile->xmatch, state); | |
335 | /* any accepting state means a valid match. */ | |
336 | if (perm & MAY_EXEC) { | |
337 | candidate = profile; | |
338 | len = profile->xmatch_len; | |
339 | } | |
340 | } else if (!strcmp(profile->base.name, name)) | |
341 | /* exact non-re match, no more searching required */ | |
342 | return profile; | |
343 | } | |
344 | ||
345 | return candidate; | |
346 | } | |
347 | ||
348 | /** | |
349 | * find_attach - do attachment search for unconfined processes | |
350 | * @ns: the current namespace (NOT NULL) | |
351 | * @list: list to search (NOT NULL) | |
352 | * @name: the executable name to match against (NOT NULL) | |
353 | * | |
80594fc2 | 354 | * Returns: label or NULL if no match found |
898127c3 | 355 | */ |
80594fc2 JJ |
356 | static struct aa_label *find_attach(struct aa_ns *ns, struct list_head *list, |
357 | const char *name) | |
898127c3 JJ |
358 | { |
359 | struct aa_profile *profile; | |
360 | ||
01e2b670 | 361 | rcu_read_lock(); |
898127c3 | 362 | profile = aa_get_profile(__attach_match(name, list)); |
01e2b670 | 363 | rcu_read_unlock(); |
898127c3 | 364 | |
80594fc2 | 365 | return profile ? &profile->label : NULL; |
898127c3 JJ |
366 | } |
367 | ||
368 | static const char *next_name(int xtype, const char *name) | |
369 | { | |
370 | return NULL; | |
371 | } | |
372 | ||
373 | /** | |
374 | * x_table_lookup - lookup an x transition name via transition table | |
375 | * @profile: current profile (NOT NULL) | |
376 | * @xindex: index into x transition table | |
80594fc2 | 377 | * @name: returns: name tested to find label (NOT NULL) |
898127c3 | 378 | * |
80594fc2 | 379 | * Returns: refcounted label, or NULL on failure (MAYBE NULL) |
898127c3 | 380 | */ |
80594fc2 JJ |
381 | struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex, |
382 | const char **name) | |
898127c3 | 383 | { |
80594fc2 | 384 | struct aa_label *label = NULL; |
898127c3 JJ |
385 | u32 xtype = xindex & AA_X_TYPE_MASK; |
386 | int index = xindex & AA_X_INDEX_MASK; | |
898127c3 | 387 | |
80594fc2 | 388 | AA_BUG(!name); |
898127c3 | 389 | |
80594fc2 JJ |
390 | /* index is guaranteed to be in range, validated at load time */ |
391 | /* TODO: move lookup parsing to unpack time so this is a straight | |
392 | * index into the resultant label | |
393 | */ | |
394 | for (*name = profile->file.trans.table[index]; !label && *name; | |
395 | *name = next_name(xtype, *name)) { | |
898127c3 | 396 | if (xindex & AA_X_CHILD) { |
80594fc2 | 397 | struct aa_profile *new_profile; |
898127c3 | 398 | /* release by caller */ |
80594fc2 JJ |
399 | new_profile = aa_find_child(profile, *name); |
400 | if (new_profile) | |
401 | label = &new_profile->label; | |
898127c3 | 402 | continue; |
898127c3 | 403 | } |
80594fc2 JJ |
404 | label = aa_label_parse(&profile->label, *name, GFP_ATOMIC, |
405 | true, false); | |
406 | if (IS_ERR(label)) | |
407 | label = NULL; | |
898127c3 JJ |
408 | } |
409 | ||
410 | /* released by caller */ | |
80594fc2 | 411 | return label; |
898127c3 JJ |
412 | } |
413 | ||
414 | /** | |
80594fc2 | 415 | * x_to_label - get target label for a given xindex |
898127c3 JJ |
416 | * @profile: current profile (NOT NULL) |
417 | * @name: name to lookup (NOT NULL) | |
418 | * @xindex: index into x transition table | |
80594fc2 | 419 | * @lookupname: returns: name used in lookup if one was specified (NOT NULL) |
898127c3 | 420 | * |
80594fc2 | 421 | * find label for a transition index |
898127c3 | 422 | * |
80594fc2 | 423 | * Returns: refcounted label or NULL if not found available |
898127c3 | 424 | */ |
80594fc2 JJ |
425 | static struct aa_label *x_to_label(struct aa_profile *profile, |
426 | const char *name, u32 xindex, | |
427 | const char **lookupname, | |
428 | const char **info) | |
898127c3 | 429 | { |
80594fc2 JJ |
430 | struct aa_label *new = NULL; |
431 | struct aa_ns *ns = profile->ns; | |
898127c3 | 432 | u32 xtype = xindex & AA_X_TYPE_MASK; |
80594fc2 | 433 | const char *stack = NULL; |
898127c3 JJ |
434 | |
435 | switch (xtype) { | |
436 | case AA_X_NONE: | |
437 | /* fail exec unless ix || ux fallback - handled by caller */ | |
80594fc2 JJ |
438 | *lookupname = NULL; |
439 | break; | |
440 | case AA_X_TABLE: | |
441 | /* TODO: fix when perm mapping done at unload */ | |
442 | stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK]; | |
443 | if (*stack != '&') { | |
444 | /* released by caller */ | |
445 | new = x_table_lookup(profile, xindex, lookupname); | |
446 | stack = NULL; | |
447 | break; | |
448 | } | |
449 | /* fall through to X_NAME */ | |
898127c3 JJ |
450 | case AA_X_NAME: |
451 | if (xindex & AA_X_CHILD) | |
452 | /* released by caller */ | |
80594fc2 JJ |
453 | new = find_attach(ns, &profile->base.profiles, |
454 | name); | |
898127c3 JJ |
455 | else |
456 | /* released by caller */ | |
80594fc2 JJ |
457 | new = find_attach(ns, &ns->base.profiles, |
458 | name); | |
459 | *lookupname = name; | |
898127c3 JJ |
460 | break; |
461 | } | |
462 | ||
80594fc2 JJ |
463 | if (!new) { |
464 | if (xindex & AA_X_INHERIT) { | |
465 | /* (p|c|n)ix - don't change profile but do | |
466 | * use the newest version | |
467 | */ | |
468 | *info = "ix fallback"; | |
469 | /* no profile && no error */ | |
470 | new = aa_get_newest_label(&profile->label); | |
471 | } else if (xindex & AA_X_UNCONFINED) { | |
472 | new = aa_get_newest_label(ns_unconfined(profile->ns)); | |
473 | *info = "ux fallback"; | |
474 | } | |
475 | } | |
476 | ||
477 | if (new && stack) { | |
478 | /* base the stack on post domain transition */ | |
479 | struct aa_label *base = new; | |
480 | new = aa_label_parse(base, stack, GFP_ATOMIC, true, false); | |
481 | if (IS_ERR(new)) | |
482 | new = NULL; | |
483 | aa_put_label(base); | |
484 | } | |
485 | ||
898127c3 | 486 | /* released by caller */ |
80594fc2 JJ |
487 | return new; |
488 | } | |
489 | ||
490 | static struct aa_label *profile_transition(struct aa_profile *profile, | |
491 | const char *name, | |
492 | struct path_cond *cond, | |
493 | bool *secure_exec) | |
494 | { | |
495 | struct aa_label *new = NULL; | |
496 | const char *info = NULL; | |
497 | unsigned int state = profile->file.start; | |
498 | struct aa_perms perms = {}; | |
499 | const char *target = NULL; | |
500 | int error = 0; | |
501 | ||
502 | if (profile_unconfined(profile)) { | |
503 | new = find_attach(profile->ns, &profile->ns->base.profiles, | |
504 | name); | |
505 | if (new) { | |
506 | AA_DEBUG("unconfined attached to new label"); | |
507 | ||
508 | return new; | |
509 | } | |
510 | AA_DEBUG("unconfined exec no attachment"); | |
511 | ||
512 | return aa_get_newest_label(&profile->label); | |
513 | } | |
514 | ||
515 | /* find exec permissions for name */ | |
516 | state = aa_str_perms(profile->file.dfa, state, name, cond, &perms); | |
517 | if (perms.allow & MAY_EXEC) { | |
518 | /* exec permission determine how to transition */ | |
519 | new = x_to_label(profile, name, perms.xindex, &target, &info); | |
ae3f556b | 520 | if (new && new->proxy == profile->label.proxy && info) { |
80594fc2 JJ |
521 | /* hack ix fallback - improve how this is detected */ |
522 | goto audit; | |
523 | } else if (!new) { | |
524 | error = -EACCES; | |
525 | info = "profile transition not found"; | |
526 | /* remove MAY_EXEC to audit as failure */ | |
527 | perms.allow &= ~MAY_EXEC; | |
528 | } | |
529 | } else if (COMPLAIN_MODE(profile)) { | |
530 | /* no exec permission - learning mode */ | |
531 | struct aa_profile *new_profile = aa_null_profile(profile, false, | |
532 | name, GFP_ATOMIC); | |
533 | if (!new_profile) { | |
534 | error = -ENOMEM; | |
535 | info = "could not create null profile"; | |
536 | } else { | |
537 | error = -EACCES; | |
538 | new = &new_profile->label; | |
539 | } | |
540 | perms.xindex |= AA_X_UNSAFE; | |
541 | } else | |
542 | /* fail exec */ | |
543 | error = -EACCES; | |
544 | ||
545 | if (!new) | |
546 | goto audit; | |
547 | ||
548 | if (!(perms.xindex & AA_X_UNSAFE)) { | |
549 | if (DEBUG_ON) { | |
550 | dbg_printk("apparmor: scrubbing environment variables " | |
551 | "for %s profile=", name); | |
552 | aa_label_printk(new, GFP_ATOMIC); | |
553 | dbg_printk("\n"); | |
554 | } | |
555 | *secure_exec = true; | |
556 | } | |
557 | ||
558 | audit: | |
559 | aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new, | |
560 | cond->uid, info, error); | |
561 | if (!new) | |
562 | return ERR_PTR(error); | |
563 | ||
564 | return new; | |
565 | } | |
566 | ||
567 | static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec, | |
568 | bool stack, const char *xname, struct path_cond *cond, | |
569 | bool *secure_exec) | |
570 | { | |
571 | unsigned int state = profile->file.start; | |
572 | struct aa_perms perms = {}; | |
573 | const char *info = "change_profile onexec"; | |
574 | int error = -EACCES; | |
575 | ||
576 | if (profile_unconfined(profile)) { | |
577 | /* change_profile on exec already granted */ | |
578 | /* | |
579 | * NOTE: Domain transitions from unconfined are allowed | |
580 | * even when no_new_privs is set because this aways results | |
581 | * in a further reduction of permissions. | |
582 | */ | |
583 | return 0; | |
584 | } | |
585 | ||
586 | /* find exec permissions for name */ | |
587 | state = aa_str_perms(profile->file.dfa, state, xname, cond, &perms); | |
588 | if (!(perms.allow & AA_MAY_ONEXEC)) { | |
589 | info = "no change_onexec valid for executable"; | |
590 | goto audit; | |
591 | } | |
592 | /* test if this exec can be paired with change_profile onexec. | |
593 | * onexec permission is linked to exec with a standard pairing | |
594 | * exec\0change_profile | |
595 | */ | |
596 | state = aa_dfa_null_transition(profile->file.dfa, state); | |
597 | error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC, | |
598 | state, &perms); | |
599 | if (error) | |
600 | goto audit; | |
601 | ||
602 | if (!(perms.xindex & AA_X_UNSAFE)) { | |
603 | if (DEBUG_ON) { | |
604 | dbg_printk("appaarmor: scrubbing environment " | |
605 | "variables for %s label=", xname); | |
606 | aa_label_printk(onexec, GFP_ATOMIC); | |
607 | dbg_printk("\n"); | |
608 | } | |
609 | *secure_exec = true; | |
610 | } | |
611 | ||
612 | audit: | |
613 | return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname, | |
614 | NULL, onexec, cond->uid, info, error); | |
615 | } | |
616 | ||
617 | /* ensure none ns domain transitions are correctly applied with onexec */ | |
618 | ||
619 | static struct aa_label *handle_onexec(struct aa_label *label, | |
620 | struct aa_label *onexec, bool stack, | |
621 | const char *xname, | |
622 | struct path_cond *cond, | |
623 | bool *unsafe) | |
624 | { | |
625 | struct aa_profile *profile; | |
626 | struct aa_label *new; | |
627 | int error; | |
628 | ||
629 | if (!stack) { | |
630 | error = fn_for_each_in_ns(label, profile, | |
631 | profile_onexec(profile, onexec, stack, | |
632 | xname, cond, unsafe)); | |
633 | if (error) | |
634 | return ERR_PTR(error); | |
635 | new = fn_label_build_in_ns(label, profile, GFP_ATOMIC, | |
636 | aa_get_newest_label(onexec), | |
637 | profile_transition(profile, xname, | |
638 | cond, unsafe)); | |
639 | } else { | |
640 | /* TODO: determine how much we want to losen this */ | |
641 | error = fn_for_each_in_ns(label, profile, | |
642 | profile_onexec(profile, onexec, stack, xname, | |
643 | cond, unsafe)); | |
644 | if (error) | |
645 | return ERR_PTR(error); | |
646 | new = fn_label_build_in_ns(label, profile, GFP_ATOMIC, | |
647 | aa_label_merge(label, onexec, | |
648 | GFP_ATOMIC), | |
649 | profile_transition(profile, xname, | |
650 | cond, unsafe)); | |
651 | } | |
652 | ||
653 | if (new) | |
654 | return new; | |
655 | ||
656 | error = fn_for_each_in_ns(label, profile, | |
657 | aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC, | |
658 | AA_MAY_ONEXEC, xname, NULL, onexec, | |
659 | GLOBAL_ROOT_UID, | |
660 | "failed to build target label", -ENOMEM)); | |
661 | return ERR_PTR(error); | |
898127c3 JJ |
662 | } |
663 | ||
664 | /** | |
665 | * apparmor_bprm_set_creds - set the new creds on the bprm struct | |
666 | * @bprm: binprm for the exec (NOT NULL) | |
667 | * | |
668 | * Returns: %0 or error on failure | |
80594fc2 JJ |
669 | * |
670 | * TODO: once the other paths are done see if we can't refactor into a fn | |
898127c3 JJ |
671 | */ |
672 | int apparmor_bprm_set_creds(struct linux_binprm *bprm) | |
673 | { | |
80594fc2 JJ |
674 | struct aa_task_ctx *ctx; |
675 | struct aa_label *label, *new = NULL; | |
676 | struct aa_profile *profile; | |
898127c3 | 677 | char *buffer = NULL; |
80594fc2 JJ |
678 | const char *xname = NULL; |
679 | const char *info = NULL; | |
680 | int error = 0; | |
681 | bool unsafe = false; | |
898127c3 | 682 | struct path_cond cond = { |
496ad9aa AV |
683 | file_inode(bprm->file)->i_uid, |
684 | file_inode(bprm->file)->i_mode | |
898127c3 | 685 | }; |
898127c3 JJ |
686 | |
687 | if (bprm->cred_prepared) | |
688 | return 0; | |
689 | ||
80594fc2 JJ |
690 | ctx = cred_ctx(bprm->cred); |
691 | AA_BUG(!ctx); | |
898127c3 | 692 | |
80594fc2 JJ |
693 | label = aa_get_newest_label(ctx->label); |
694 | profile = labels_profile(label); | |
898127c3 | 695 | |
80594fc2 JJ |
696 | /* buffer freed below, xname is pointer into buffer */ |
697 | get_buffers(buffer); | |
698 | error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer, | |
699 | &xname, &info, profile->disconnected); | |
898127c3 | 700 | if (error) { |
80594fc2 JJ |
701 | if (profile_unconfined(profile) || |
702 | (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) | |
898127c3 | 703 | error = 0; |
80594fc2 | 704 | xname = bprm->filename; |
898127c3 JJ |
705 | goto audit; |
706 | } | |
707 | ||
80594fc2 JJ |
708 | /* Test for onexec first as onexec override other x transitions. */ |
709 | if (ctx->onexec) | |
710 | new = handle_onexec(label, ctx->onexec, ctx->token, xname, | |
711 | &cond, &unsafe); | |
712 | else | |
713 | new = fn_label_build(label, profile, GFP_ATOMIC, | |
714 | profile_transition(profile, xname, &cond, | |
715 | &unsafe)); | |
716 | ||
717 | AA_BUG(!new); | |
718 | if (IS_ERR(new)) { | |
719 | error = PTR_ERR(new); | |
720 | goto done; | |
721 | } else if (!new) { | |
722 | error = -ENOMEM; | |
723 | goto done; | |
898127c3 JJ |
724 | } |
725 | ||
80594fc2 JJ |
726 | /* Policy has specified a domain transitions. if no_new_privs and |
727 | * confined and not transitioning to the current domain fail. | |
728 | * | |
729 | * NOTE: Domain transitions from unconfined and to stritly stacked | |
730 | * subsets are allowed even when no_new_privs is set because this | |
731 | * aways results in a further reduction of permissions. | |
c29bceb3 | 732 | */ |
80594fc2 JJ |
733 | if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS && |
734 | !unconfined(label) && !aa_label_is_subset(new, label)) { | |
c29bceb3 | 735 | error = -EPERM; |
80594fc2 | 736 | info = "no new privs"; |
898127c3 | 737 | goto audit; |
80594fc2 | 738 | } |
898127c3 JJ |
739 | |
740 | if (bprm->unsafe & LSM_UNSAFE_SHARE) { | |
741 | /* FIXME: currently don't mediate shared state */ | |
742 | ; | |
743 | } | |
744 | ||
745 | if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) { | |
80594fc2 JJ |
746 | /* TODO: test needs to be profile of label to new */ |
747 | error = may_change_ptraced_domain(new, &info); | |
f7da2de0 | 748 | if (error) |
898127c3 | 749 | goto audit; |
898127c3 JJ |
750 | } |
751 | ||
80594fc2 JJ |
752 | if (unsafe) { |
753 | if (DEBUG_ON) { | |
754 | dbg_printk("scrubbing environment variables for %s " | |
755 | "label=", xname); | |
756 | aa_label_printk(new, GFP_ATOMIC); | |
757 | dbg_printk("\n"); | |
758 | } | |
898127c3 JJ |
759 | bprm->unsafe |= AA_SECURE_X_NEEDED; |
760 | } | |
898127c3 | 761 | |
ae3f556b | 762 | if (label->proxy != new->proxy) { |
80594fc2 JJ |
763 | /* when transitioning clear unsafe personality bits */ |
764 | if (DEBUG_ON) { | |
765 | dbg_printk("apparmor: clearing unsafe personality " | |
766 | "bits. %s label=", xname); | |
767 | aa_label_printk(new, GFP_ATOMIC); | |
768 | dbg_printk("\n"); | |
769 | } | |
770 | bprm->per_clear |= PER_CLEAR_ON_SETID; | |
771 | } | |
772 | aa_put_label(ctx->label); | |
773 | /* transfer reference, released when ctx is freed */ | |
774 | ctx->label = new; | |
898127c3 | 775 | |
80594fc2 JJ |
776 | done: |
777 | /* clear out temporary/transitional state from the context */ | |
778 | aa_clear_task_ctx_trans(ctx); | |
898127c3 | 779 | |
80594fc2 JJ |
780 | aa_put_label(label); |
781 | put_buffers(buffer); | |
898127c3 JJ |
782 | |
783 | return error; | |
80594fc2 JJ |
784 | |
785 | audit: | |
786 | error = fn_for_each(label, profile, | |
787 | aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC, | |
788 | xname, NULL, new, | |
789 | file_inode(bprm->file)->i_uid, info, | |
790 | error)); | |
791 | aa_put_label(new); | |
792 | goto done; | |
898127c3 JJ |
793 | } |
794 | ||
795 | /** | |
796 | * apparmor_bprm_secureexec - determine if secureexec is needed | |
797 | * @bprm: binprm for exec (NOT NULL) | |
798 | * | |
799 | * Returns: %1 if secureexec is needed else %0 | |
800 | */ | |
801 | int apparmor_bprm_secureexec(struct linux_binprm *bprm) | |
802 | { | |
898127c3 JJ |
803 | /* the decision to use secure exec is computed in set_creds |
804 | * and stored in bprm->unsafe. | |
805 | */ | |
b1d9e6b0 CS |
806 | if (bprm->unsafe & AA_SECURE_X_NEEDED) |
807 | return 1; | |
898127c3 | 808 | |
b1d9e6b0 | 809 | return 0; |
898127c3 JJ |
810 | } |
811 | ||
80594fc2 JJ |
812 | /* |
813 | * Functions for self directed profile change | |
898127c3 | 814 | */ |
898127c3 | 815 | |
898127c3 | 816 | |
80594fc2 JJ |
817 | /* helper fn for change_hat |
818 | * | |
819 | * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL | |
898127c3 | 820 | */ |
80594fc2 JJ |
821 | static struct aa_label *build_change_hat(struct aa_profile *profile, |
822 | const char *name, bool sibling) | |
898127c3 | 823 | { |
80594fc2 JJ |
824 | struct aa_profile *root, *hat = NULL; |
825 | const char *info = NULL; | |
826 | int error = 0; | |
898127c3 | 827 | |
80594fc2 JJ |
828 | if (sibling && PROFILE_IS_HAT(profile)) { |
829 | root = aa_get_profile_rcu(&profile->parent); | |
830 | } else if (!sibling && !PROFILE_IS_HAT(profile)) { | |
831 | root = aa_get_profile(profile); | |
832 | } else { | |
833 | info = "conflicting target types"; | |
834 | error = -EPERM; | |
835 | goto audit; | |
836 | } | |
898127c3 | 837 | |
80594fc2 JJ |
838 | hat = aa_find_child(root, name); |
839 | if (!hat) { | |
840 | error = -ENOENT; | |
841 | if (COMPLAIN_MODE(profile)) { | |
842 | hat = aa_null_profile(profile, true, name, GFP_KERNEL); | |
843 | if (!hat) { | |
844 | info = "failed null profile create"; | |
845 | error = -ENOMEM; | |
846 | } | |
847 | } | |
848 | } | |
849 | aa_put_profile(root); | |
850 | ||
851 | audit: | |
852 | aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT, | |
853 | name, hat ? hat->base.hname : NULL, hat ? &hat->label : NULL, GLOBAL_ROOT_UID, | |
854 | NULL, error); | |
855 | if (!hat || (error && error != -ENOENT)) | |
856 | return ERR_PTR(error); | |
857 | /* if hat && error - complain mode, already audited and we adjust for | |
858 | * complain mode allow by returning hat->label | |
859 | */ | |
860 | return &hat->label; | |
861 | } | |
862 | ||
863 | /* helper fn for changing into a hat | |
898127c3 | 864 | * |
80594fc2 | 865 | * Returns: label for hat transition or ERR_PTR. Does not return NULL |
898127c3 | 866 | */ |
80594fc2 JJ |
867 | static struct aa_label *change_hat(struct aa_label *label, const char *hats[], |
868 | int count, bool permtest) | |
898127c3 | 869 | { |
80594fc2 JJ |
870 | struct aa_profile *profile, *root, *hat = NULL; |
871 | struct aa_label *new; | |
872 | struct label_it it; | |
873 | bool sibling = false; | |
874 | const char *name, *info = NULL; | |
875 | int i, error; | |
876 | ||
877 | AA_BUG(!label); | |
878 | AA_BUG(!hats); | |
879 | AA_BUG(count < 1); | |
880 | ||
881 | if (PROFILE_IS_HAT(labels_profile(label))) | |
882 | sibling = true; | |
883 | ||
884 | /*find first matching hat */ | |
885 | for (i = 0; i < count && !hat; i++) { | |
886 | name = hats[i]; | |
887 | label_for_each_in_ns(it, labels_ns(label), label, profile) { | |
888 | if (sibling && PROFILE_IS_HAT(profile)) { | |
889 | root = aa_get_profile_rcu(&profile->parent); | |
890 | } else if (!sibling && !PROFILE_IS_HAT(profile)) { | |
891 | root = aa_get_profile(profile); | |
892 | } else { /* conflicting change type */ | |
893 | info = "conflicting targets types"; | |
894 | error = -EPERM; | |
895 | goto fail; | |
896 | } | |
897 | hat = aa_find_child(root, name); | |
898 | aa_put_profile(root); | |
899 | if (!hat) { | |
900 | if (!COMPLAIN_MODE(profile)) | |
901 | goto outer_continue; | |
902 | /* complain mode succeed as if hat */ | |
903 | } else if (!PROFILE_IS_HAT(hat)) { | |
904 | info = "target not hat"; | |
905 | error = -EPERM; | |
906 | aa_put_profile(hat); | |
907 | goto fail; | |
908 | } | |
909 | aa_put_profile(hat); | |
910 | } | |
911 | /* found a hat for all profiles in ns */ | |
912 | goto build; | |
913 | outer_continue: ; | |
914 | } | |
915 | /* no hats that match, find appropriate error | |
916 | * | |
917 | * In complain mode audit of the failure is based off of the first | |
918 | * hat supplied. This is done due how userspace interacts with | |
919 | * change_hat. | |
920 | */ | |
921 | name = NULL; | |
922 | label_for_each_in_ns(it, labels_ns(label), label, profile) { | |
923 | if (!list_empty(&profile->base.profiles)) { | |
924 | info = "hat not found"; | |
925 | error = -ENOENT; | |
926 | goto fail; | |
927 | } | |
928 | } | |
929 | info = "no hats defined"; | |
930 | error = -ECHILD; | |
931 | ||
932 | fail: | |
933 | fn_for_each_in_ns(label, profile, | |
934 | /* no target as it has failed to be found or built */ | |
935 | /* TODO: get rid of GLOBAL_ROOT_UID */ | |
936 | aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, | |
937 | AA_MAY_CHANGEHAT, name, NULL, NULL, | |
938 | GLOBAL_ROOT_UID, info, error)); | |
939 | return (ERR_PTR(error)); | |
940 | ||
941 | build: | |
942 | new = fn_label_build_in_ns(label, profile, GFP_KERNEL, | |
943 | build_change_hat(profile, name, sibling), | |
944 | aa_get_label(&profile->label)); | |
945 | if (!new) { | |
946 | info = "label build failed"; | |
947 | error = -ENOMEM; | |
948 | goto fail; | |
949 | } /* else if (IS_ERR) build_change_hat has logged error so return new */ | |
950 | ||
951 | return new; | |
898127c3 JJ |
952 | } |
953 | ||
954 | /** | |
955 | * aa_change_hat - change hat to/from subprofile | |
956 | * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0) | |
957 | * @count: number of hat names in @hats | |
958 | * @token: magic value to validate the hat change | |
959 | * @permtest: true if this is just a permission test | |
960 | * | |
80594fc2 JJ |
961 | * Returns %0 on success, error otherwise. |
962 | * | |
898127c3 JJ |
963 | * Change to the first profile specified in @hats that exists, and store |
964 | * the @hat_magic in the current task context. If the count == 0 and the | |
965 | * @token matches that stored in the current task context, return to the | |
966 | * top level profile. | |
967 | * | |
80594fc2 JJ |
968 | * change_hat only applies to profiles in the current ns, and each profile |
969 | * in the ns must make the same transition otherwise change_hat will fail. | |
898127c3 JJ |
970 | */ |
971 | int aa_change_hat(const char *hats[], int count, u64 token, bool permtest) | |
972 | { | |
973 | const struct cred *cred; | |
80594fc2 JJ |
974 | struct aa_task_ctx *ctx; |
975 | struct aa_label *label, *previous, *new = NULL, *target = NULL; | |
976 | struct aa_profile *profile; | |
977 | struct aa_perms perms = {}; | |
978 | const char *info = NULL; | |
898127c3 JJ |
979 | int error = 0; |
980 | ||
c29bceb3 JJ |
981 | /* |
982 | * Fail explicitly requested domain transitions if no_new_privs. | |
983 | * There is no exception for unconfined as change_hat is not | |
984 | * available. | |
985 | */ | |
80594fc2 JJ |
986 | if (task_no_new_privs(current)) { |
987 | /* not an apparmor denial per se, so don't log it */ | |
988 | AA_DEBUG("no_new_privs - chanage_hat denied"); | |
c29bceb3 | 989 | return -EPERM; |
80594fc2 | 990 | } |
c29bceb3 | 991 | |
898127c3 JJ |
992 | /* released below */ |
993 | cred = get_current_cred(); | |
80594fc2 JJ |
994 | ctx = cred_ctx(cred); |
995 | label = aa_get_newest_cred_label(cred); | |
996 | previous = aa_get_newest_label(ctx->previous); | |
898127c3 | 997 | |
80594fc2 JJ |
998 | if (unconfined(label)) { |
999 | info = "unconfined can not change_hat"; | |
898127c3 | 1000 | error = -EPERM; |
80594fc2 | 1001 | goto fail; |
898127c3 JJ |
1002 | } |
1003 | ||
1004 | if (count) { | |
80594fc2 JJ |
1005 | new = change_hat(label, hats, count, permtest); |
1006 | AA_BUG(!new); | |
1007 | if (IS_ERR(new)) { | |
1008 | error = PTR_ERR(new); | |
1009 | new = NULL; | |
1010 | /* already audited */ | |
1011 | goto out; | |
898127c3 JJ |
1012 | } |
1013 | ||
80594fc2 JJ |
1014 | error = may_change_ptraced_domain(new, &info); |
1015 | if (error) | |
1016 | goto fail; | |
1017 | ||
1018 | if (permtest) | |
1019 | goto out; | |
1020 | ||
1021 | target = new; | |
1022 | error = aa_set_current_hat(new, token); | |
1023 | if (error == -EACCES) | |
1024 | /* kill task in case of brute force attacks */ | |
1025 | goto kill; | |
1026 | } else if (previous && !permtest) { | |
1027 | /* Return to saved label. Kill task if restore fails | |
1028 | * to avoid brute force attacks | |
1029 | */ | |
1030 | target = previous; | |
1031 | error = aa_restore_previous_label(token); | |
898127c3 | 1032 | if (error) { |
898127c3 | 1033 | if (error == -EACCES) |
80594fc2 JJ |
1034 | goto kill; |
1035 | goto fail; | |
898127c3 | 1036 | } |
80594fc2 | 1037 | } /* else ignore permtest && restores when there is no saved profile */ |
898127c3 JJ |
1038 | |
1039 | out: | |
80594fc2 JJ |
1040 | aa_put_label(new); |
1041 | aa_put_label(previous); | |
1042 | aa_put_label(label); | |
898127c3 JJ |
1043 | put_cred(cred); |
1044 | ||
1045 | return error; | |
80594fc2 JJ |
1046 | |
1047 | kill: | |
1048 | info = "failed token match"; | |
1049 | perms.kill = AA_MAY_CHANGEHAT; | |
1050 | ||
1051 | fail: | |
1052 | fn_for_each_in_ns(label, profile, | |
1053 | aa_audit_file(profile, &perms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT, | |
1054 | NULL, NULL, target, GLOBAL_ROOT_UID, info, error)); | |
1055 | ||
1056 | goto out; | |
1057 | } | |
1058 | ||
1059 | ||
1060 | static int change_profile_perms_wrapper(const char *op, const char *name, | |
1061 | struct aa_profile *profile, | |
1062 | struct aa_label *target, bool stack, | |
1063 | u32 request, struct aa_perms *perms) | |
1064 | { | |
1065 | int error = change_profile_perms(profile, target, | |
1066 | stack, request, | |
1067 | profile->file.start, perms); | |
1068 | if (error) | |
1069 | error = aa_audit_file(profile, perms, op, request, name, | |
1070 | NULL, target, GLOBAL_ROOT_UID, NULL, | |
1071 | error); | |
1072 | ||
1073 | return error; | |
898127c3 JJ |
1074 | } |
1075 | ||
1076 | /** | |
1077 | * aa_change_profile - perform a one-way profile transition | |
80594fc2 | 1078 | * @fqname: name of profile may include namespace (NOT NULL) |
898127c3 JJ |
1079 | * @onexec: whether this transition is to take place immediately or at exec |
1080 | * @permtest: true if this is just a permission test | |
80594fc2 | 1081 | * @stack: true if this call is to stack on top of current domain |
898127c3 JJ |
1082 | * Change to new profile @name. Unlike with hats, there is no way |
1083 | * to change back. If @name isn't specified the current profile name is | |
1084 | * used. | |
1085 | * If @onexec then the transition is delayed until | |
1086 | * the next exec. | |
1087 | * | |
1088 | * Returns %0 on success, error otherwise. | |
1089 | */ | |
80594fc2 JJ |
1090 | int aa_change_profile(const char *fqname, bool onexec, |
1091 | bool permtest, bool stack) | |
898127c3 | 1092 | { |
80594fc2 JJ |
1093 | struct aa_label *label, *new = NULL, *target = NULL; |
1094 | struct aa_profile *profile; | |
1095 | struct aa_perms perms = {}; | |
1096 | const char *info = NULL; | |
1097 | const char *auditname = fqname; /* retain leading & if stack */ | |
1098 | int error = 0; | |
1099 | char *op; | |
898127c3 JJ |
1100 | u32 request; |
1101 | ||
80594fc2 JJ |
1102 | if (!fqname || !*fqname) { |
1103 | AA_DEBUG("no profile name"); | |
898127c3 | 1104 | return -EINVAL; |
80594fc2 | 1105 | } |
898127c3 JJ |
1106 | |
1107 | if (onexec) { | |
1108 | request = AA_MAY_ONEXEC; | |
80594fc2 JJ |
1109 | if (stack) |
1110 | op = OP_STACK_ONEXEC; | |
1111 | else | |
1112 | op = OP_CHANGE_ONEXEC; | |
898127c3 JJ |
1113 | } else { |
1114 | request = AA_MAY_CHANGE_PROFILE; | |
80594fc2 JJ |
1115 | if (stack) |
1116 | op = OP_STACK; | |
1117 | else | |
1118 | op = OP_CHANGE_PROFILE; | |
898127c3 JJ |
1119 | } |
1120 | ||
80594fc2 | 1121 | label = aa_get_current_label(); |
898127c3 | 1122 | |
80594fc2 JJ |
1123 | if (*fqname == '&') { |
1124 | stack = true; | |
1125 | /* don't have label_parse() do stacking */ | |
1126 | fqname++; | |
c29bceb3 | 1127 | } |
80594fc2 JJ |
1128 | target = aa_label_parse(label, fqname, GFP_KERNEL, true, false); |
1129 | if (IS_ERR(target)) { | |
1130 | struct aa_profile *tprofile; | |
1131 | ||
1132 | info = "label not found"; | |
1133 | error = PTR_ERR(target); | |
1134 | target = NULL; | |
1135 | /* TODO: fixme using labels_profile is not right - do profile | |
1136 | per complain profile ??? */ | |
1137 | if (permtest || !COMPLAIN_MODE(labels_profile(label))) | |
1138 | goto audit; | |
898127c3 | 1139 | /* released below */ |
80594fc2 JJ |
1140 | tprofile = aa_null_profile(labels_profile(label), false, fqname, GFP_KERNEL); |
1141 | if (!tprofile) { | |
1142 | info = "failed null profile create"; | |
1143 | error = -ENOMEM; | |
898127c3 JJ |
1144 | goto audit; |
1145 | } | |
80594fc2 JJ |
1146 | target = &tprofile->label; |
1147 | goto check; | |
898127c3 JJ |
1148 | } |
1149 | ||
80594fc2 JJ |
1150 | /* |
1151 | * Fail explicitly requested domain transitions when no_new_privs | |
1152 | * and not unconfined OR the transition results in a stack on | |
1153 | * the current label. | |
1154 | * Stacking domain transitions and transitions from unconfined are | |
1155 | * allowed even when no_new_privs is set because this aways results | |
1156 | * in a reduction of permissions. | |
1157 | */ | |
1158 | if (task_no_new_privs(current) && !stack && !unconfined(label) && | |
1159 | !aa_label_is_subset(target, label)) { | |
1160 | info = "no new privs"; | |
1161 | error = -EPERM; | |
898127c3 JJ |
1162 | goto audit; |
1163 | } | |
1164 | ||
80594fc2 JJ |
1165 | /* self directed transitions only apply to current policy ns */ |
1166 | /* TODO: currently requiring perms for stacking and straight change | |
1167 | * stacking doesn't strictly need this. Determine how much | |
1168 | * we want to loosen this restriction for stacking | |
1169 | */ | |
1170 | /* if (!stack) { */ | |
1171 | error = fn_for_each_in_ns(label, profile, | |
1172 | change_profile_perms_wrapper(op, auditname, | |
1173 | profile, target, stack, | |
1174 | request, &perms)); | |
1175 | if (error) | |
1176 | /* auditing done in change_profile_perms_wrapper */ | |
1177 | goto out; | |
1178 | ||
1179 | /* } */ | |
898127c3 | 1180 | |
80594fc2 | 1181 | check: |
898127c3 | 1182 | /* check if tracing task is allowed to trace target domain */ |
80594fc2 JJ |
1183 | error = may_change_ptraced_domain(target, &info); |
1184 | if (error && !fn_for_each_in_ns(label, profile, | |
1185 | COMPLAIN_MODE(profile))) | |
898127c3 | 1186 | goto audit; |
898127c3 | 1187 | |
80594fc2 JJ |
1188 | /* TODO: add permission check to allow this |
1189 | if (onexec && !current_is_single_threaded()) { | |
1190 | info = "not a single threaded task"; | |
1191 | error = -EACCES; | |
898127c3 | 1192 | goto audit; |
80594fc2 JJ |
1193 | } |
1194 | */ | |
1195 | if (permtest) | |
1196 | goto out; | |
898127c3 | 1197 | |
80594fc2 JJ |
1198 | if (!onexec) { |
1199 | /* only transition profiles in the current ns */ | |
1200 | if (stack) | |
1201 | new = aa_label_merge(label, target, GFP_KERNEL); | |
1202 | else | |
1203 | new = fn_label_build_in_ns(label, profile, GFP_KERNEL, | |
1204 | aa_get_label(target), | |
1205 | aa_get_label(&profile->label)); | |
1206 | if (IS_ERR_OR_NULL(new)) { | |
1207 | info = "failed to build target label"; | |
1208 | error = PTR_ERR(new); | |
1209 | new = NULL; | |
1210 | perms.allow = 0; | |
1211 | goto audit; | |
1212 | } | |
1213 | error = aa_replace_current_label(new); | |
1214 | } else | |
1215 | /* full transition will be built in exec path */ | |
1216 | error = aa_set_current_onexec(target, stack); | |
898127c3 JJ |
1217 | |
1218 | audit: | |
80594fc2 JJ |
1219 | error = fn_for_each_in_ns(label, profile, |
1220 | aa_audit_file(profile, &perms, op, request, auditname, | |
1221 | NULL, new ? new : target, | |
1222 | GLOBAL_ROOT_UID, info, error)); | |
898127c3 | 1223 | |
80594fc2 JJ |
1224 | out: |
1225 | aa_put_label(new); | |
1226 | aa_put_label(target); | |
1227 | aa_put_label(label); | |
898127c3 JJ |
1228 | |
1229 | return error; | |
1230 | } |