apparmor: convert policy lookup to use accept as an index
[linux-block.git] / security / apparmor / domain.c
CommitLineData
b886d83c 1// SPDX-License-Identifier: GPL-2.0-only
898127c3
JJ
2/*
3 * AppArmor security module
4 *
5 * This file contains AppArmor policy attachment and domain transitions
6 *
7 * Copyright (C) 2002-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
898127c3
JJ
9 */
10
11#include <linux/errno.h>
12#include <linux/fdtable.h>
3cee6079 13#include <linux/fs.h>
898127c3
JJ
14#include <linux/file.h>
15#include <linux/mount.h>
16#include <linux/syscalls.h>
898127c3 17#include <linux/personality.h>
8e51f908 18#include <linux/xattr.h>
3cee6079 19#include <linux/user_namespace.h>
898127c3
JJ
20
21#include "include/audit.h"
22#include "include/apparmorfs.h"
d8889d49 23#include "include/cred.h"
898127c3
JJ
24#include "include/domain.h"
25#include "include/file.h"
26#include "include/ipc.h"
27#include "include/match.h"
28#include "include/path.h"
29#include "include/policy.h"
cff281f6 30#include "include/policy_ns.h"
898127c3
JJ
31
32/**
33 * aa_free_domain_entries - free entries in a domain table
34 * @domain: the domain table to free (MAYBE NULL)
35 */
36void aa_free_domain_entries(struct aa_domain *domain)
37{
38 int i;
39 if (domain) {
40 if (!domain->table)
41 return;
42
43 for (i = 0; i < domain->size; i++)
453431a5
WL
44 kfree_sensitive(domain->table[i]);
45 kfree_sensitive(domain->table);
898127c3
JJ
46 domain->table = NULL;
47 }
48}
49
50/**
51 * may_change_ptraced_domain - check if can change profile on ptraced task
b2d09ae4
JJ
52 * @to_label: profile to change to (NOT NULL)
53 * @info: message if there is an error
898127c3 54 *
51775fe7 55 * Check if current is ptraced and if so if the tracing task is allowed
898127c3
JJ
56 * to trace the new domain
57 *
58 * Returns: %0 or error if change not allowed
59 */
b2d09ae4
JJ
60static int may_change_ptraced_domain(struct aa_label *to_label,
61 const char **info)
898127c3
JJ
62{
63 struct task_struct *tracer;
637f688d 64 struct aa_label *tracerl = NULL;
898127c3
JJ
65 int error = 0;
66
67 rcu_read_lock();
51775fe7 68 tracer = ptrace_parent(current);
3cfcc19e 69 if (tracer)
898127c3 70 /* released below */
637f688d 71 tracerl = aa_get_task_label(tracer);
898127c3
JJ
72
73 /* not ptraced */
637f688d 74 if (!tracer || unconfined(tracerl))
898127c3
JJ
75 goto out;
76
b2d09ae4 77 error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
898127c3
JJ
78
79out:
04fdc099 80 rcu_read_unlock();
637f688d 81 aa_put_label(tracerl);
898127c3 82
b2d09ae4
JJ
83 if (error)
84 *info = "ptrace prevents transition";
898127c3
JJ
85 return error;
86}
87
93c98a48
JJ
88/**** TODO: dedup to aa_label_match - needs perm and dfa, merging
89 * specifically this is an exact copy of aa_label_match except
90 * aa_compute_perms is replaced with aa_compute_fperms
91 * and policy.dfa with file.dfa
92 ****/
93/* match a profile and its associated ns component if needed
94 * Assumes visibility test has already been done.
95 * If a subns profile is not to be matched should be prescreened with
96 * visibility test.
97 */
98static inline unsigned int match_component(struct aa_profile *profile,
99 struct aa_profile *tp,
100 bool stack, unsigned int state)
101{
102 const char *ns_name;
103
104 if (stack)
105 state = aa_dfa_match(profile->file.dfa, state, "&");
106 if (profile->ns == tp->ns)
107 return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
108
109 /* try matching with namespace name and then profile */
110 ns_name = aa_ns_name(profile->ns, tp->ns, true);
111 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
112 state = aa_dfa_match(profile->file.dfa, state, ns_name);
113 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
114 return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
115}
116
117/**
118 * label_compound_match - find perms for full compound label
119 * @profile: profile to find perms for
120 * @label: label to check access permissions for
121 * @stack: whether this is a stacking request
bab1f77f 122 * @state: state to start match in
93c98a48
JJ
123 * @subns: whether to do permission checks on components in a subns
124 * @request: permissions to request
125 * @perms: perms struct to set
126 *
127 * Returns: 0 on success else ERROR
128 *
129 * For the label A//&B//&C this does the perm match for A//&B//&C
130 * @perms should be preinitialized with allperms OR a previous permission
131 * check to be stacked.
132 */
133static int label_compound_match(struct aa_profile *profile,
134 struct aa_label *label, bool stack,
135 unsigned int state, bool subns, u32 request,
136 struct aa_perms *perms)
137{
138 struct aa_profile *tp;
139 struct label_it i;
140 struct path_cond cond = { };
141
142 /* find first subcomponent that is visible */
143 label_for_each(i, label, tp) {
144 if (!aa_ns_visible(profile->ns, tp->ns, subns))
145 continue;
146 state = match_component(profile, tp, stack, state);
147 if (!state)
148 goto fail;
149 goto next;
150 }
151
152 /* no component visible */
153 *perms = allperms;
154 return 0;
155
156next:
157 label_for_each_cont(i, label, tp) {
158 if (!aa_ns_visible(profile->ns, tp->ns, subns))
159 continue;
160 state = aa_dfa_match(profile->file.dfa, state, "//&");
161 state = match_component(profile, tp, false, state);
162 if (!state)
163 goto fail;
164 }
408d53e9 165 *perms = *(aa_lookup_fperms(&(profile->file), state, &cond));
93c98a48
JJ
166 aa_apply_modes_to_perms(profile, perms);
167 if ((perms->allow & request) != request)
168 return -EACCES;
169
170 return 0;
171
172fail:
173 *perms = nullperms;
174 return -EACCES;
175}
176
177/**
178 * label_components_match - find perms for all subcomponents of a label
179 * @profile: profile to find perms for
180 * @label: label to check access permissions for
181 * @stack: whether this is a stacking request
182 * @start: state to start match in
183 * @subns: whether to do permission checks on components in a subns
184 * @request: permissions to request
185 * @perms: an initialized perms struct to add accumulation to
186 *
187 * Returns: 0 on success else ERROR
188 *
189 * For the label A//&B//&C this does the perm match for each of A and B and C
190 * @perms should be preinitialized with allperms OR a previous permission
191 * check to be stacked.
192 */
193static int label_components_match(struct aa_profile *profile,
194 struct aa_label *label, bool stack,
195 unsigned int start, bool subns, u32 request,
196 struct aa_perms *perms)
197{
198 struct aa_profile *tp;
199 struct label_it i;
200 struct aa_perms tmp;
201 struct path_cond cond = { };
202 unsigned int state = 0;
203
204 /* find first subcomponent to test */
205 label_for_each(i, label, tp) {
206 if (!aa_ns_visible(profile->ns, tp->ns, subns))
207 continue;
208 state = match_component(profile, tp, stack, start);
209 if (!state)
210 goto fail;
211 goto next;
212 }
213
214 /* no subcomponents visible - no change in perms */
215 return 0;
216
217next:
408d53e9 218 tmp = *(aa_lookup_fperms(&(profile->file), state, &cond));
93c98a48
JJ
219 aa_apply_modes_to_perms(profile, &tmp);
220 aa_perms_accum(perms, &tmp);
221 label_for_each_cont(i, label, tp) {
222 if (!aa_ns_visible(profile->ns, tp->ns, subns))
223 continue;
224 state = match_component(profile, tp, stack, start);
225 if (!state)
226 goto fail;
408d53e9 227 tmp = *(aa_lookup_fperms(&(profile->file), state, &cond));
93c98a48
JJ
228 aa_apply_modes_to_perms(profile, &tmp);
229 aa_perms_accum(perms, &tmp);
230 }
231
232 if ((perms->allow & request) != request)
233 return -EACCES;
234
235 return 0;
236
237fail:
238 *perms = nullperms;
239 return -EACCES;
240}
241
242/**
243 * label_match - do a multi-component label match
244 * @profile: profile to match against (NOT NULL)
245 * @label: label to match (NOT NULL)
246 * @stack: whether this is a stacking request
247 * @state: state to start in
248 * @subns: whether to match subns components
249 * @request: permission request
250 * @perms: Returns computed perms (NOT NULL)
251 *
252 * Returns: the state the match finished in, may be the none matching state
253 */
254static int label_match(struct aa_profile *profile, struct aa_label *label,
255 bool stack, unsigned int state, bool subns, u32 request,
256 struct aa_perms *perms)
257{
258 int error;
259
260 *perms = nullperms;
261 error = label_compound_match(profile, label, stack, state, subns,
262 request, perms);
263 if (!error)
264 return error;
265
266 *perms = allperms;
267 return label_components_match(profile, label, stack, state, subns,
268 request, perms);
269}
270
271/******* end TODO: dedup *****/
272
898127c3
JJ
273/**
274 * change_profile_perms - find permissions for change_profile
275 * @profile: the current profile (NOT NULL)
93c98a48
JJ
276 * @target: label to transition to (NOT NULL)
277 * @stack: whether this is a stacking request
898127c3
JJ
278 * @request: requested perms
279 * @start: state to start matching in
280 *
93c98a48 281 *
898127c3 282 * Returns: permission set
93c98a48
JJ
283 *
284 * currently only matches full label A//&B//&C or individual components A, B, C
285 * not arbitrary combinations. Eg. A//&B, C
898127c3 286 */
93c98a48
JJ
287static int change_profile_perms(struct aa_profile *profile,
288 struct aa_label *target, bool stack,
289 u32 request, unsigned int start,
290 struct aa_perms *perms)
291{
292 if (profile_unconfined(profile)) {
293 perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
294 perms->audit = perms->quiet = perms->kill = 0;
295 return 0;
296 }
297
298 /* TODO: add profile in ns screening */
299 return label_match(profile, target, stack, start, true, request, perms);
300}
301
8e51f908
MG
302/**
303 * aa_xattrs_match - check whether a file matches the xattrs defined in profile
304 * @bprm: binprm struct for the process to validate
305 * @profile: profile to match against (NOT NULL)
73f488cd 306 * @state: state to start match in
8e51f908
MG
307 *
308 * Returns: number of extended attributes that matched, or < 0 on error
309 */
310static int aa_xattrs_match(const struct linux_binprm *bprm,
73f488cd 311 struct aa_profile *profile, unsigned int state)
8e51f908
MG
312{
313 int i;
a61ecd32 314 ssize_t size;
8e51f908
MG
315 struct dentry *d;
316 char *value = NULL;
317 int value_size = 0, ret = profile->xattr_count;
318
319 if (!bprm || !profile->xattr_count)
320 return 0;
8c62ed27 321 might_sleep();
8e51f908 322
73f488cd 323 /* transition from exec match to xattr set */
048d4954 324 state = aa_dfa_outofband_transition(profile->xmatch.dfa, state);
8e51f908
MG
325 d = bprm->file->f_path.dentry;
326
327 for (i = 0; i < profile->xattr_count; i++) {
c7c7a1a1
TA
328 size = vfs_getxattr_alloc(&init_user_ns, d, profile->xattrs[i],
329 &value, value_size, GFP_KERNEL);
73f488cd 330 if (size >= 0) {
2d63dd43 331 u32 index, perm;
8e51f908 332
0df34a64
JJ
333 /*
334 * Check the xattr presence before value. This ensure
335 * that not present xattr can be distinguished from a 0
336 * length value or rule that matches any value
337 */
048d4954
JJ
338 state = aa_dfa_null_transition(profile->xmatch.dfa,
339 state);
0df34a64 340 /* Check xattr value */
048d4954
JJ
341 state = aa_dfa_match_len(profile->xmatch.dfa, state,
342 value, size);
2d63dd43
JJ
343 index = ACCEPT_TABLE(profile->xmatch.dfa)[state];
344 perm = profile->xmatch.perms[index].allow;
73f488cd 345 if (!(perm & MAY_EXEC)) {
8e51f908
MG
346 ret = -EINVAL;
347 goto out;
348 }
73f488cd
JJ
349 }
350 /* transition to next element */
048d4954 351 state = aa_dfa_outofband_transition(profile->xmatch.dfa, state);
73f488cd
JJ
352 if (size < 0) {
353 /*
354 * No xattr match, so verify if transition to
355 * next element was valid. IFF so the xattr
356 * was optional.
357 */
358 if (!state) {
8e51f908
MG
359 ret = -EINVAL;
360 goto out;
361 }
73f488cd
JJ
362 /* don't count missing optional xattr as matched */
363 ret--;
8e51f908
MG
364 }
365 }
366
367out:
368 kfree(value);
369 return ret;
370}
371
898127c3 372/**
8c62ed27 373 * find_attach - do attachment search for unconfined processes
8e51f908 374 * @bprm - binprm structure of transitioning task
8c62ed27 375 * @ns: the current namespace (NOT NULL)
898127c3 376 * @head - profile list to walk (NOT NULL)
8c62ed27 377 * @name - to match against (NOT NULL)
844b8292 378 * @info - info message if there was an error (NOT NULL)
898127c3
JJ
379 *
380 * Do a linear search on the profiles in the list. There is a matching
381 * preference where an exact match is preferred over a name which uses
382 * expressions to match, and matching expressions with the greatest
383 * xmatch_len are preferred.
384 *
385 * Requires: @head not be shared or have appropriate locks held
386 *
8c62ed27 387 * Returns: label or NULL if no match found
898127c3 388 */
8c62ed27
JJ
389static struct aa_label *find_attach(const struct linux_binprm *bprm,
390 struct aa_ns *ns, struct list_head *head,
391 const char *name, const char **info)
898127c3 392{
21f60661 393 int candidate_len = 0, candidate_xattrs = 0;
844b8292 394 bool conflict = false;
898127c3
JJ
395 struct aa_profile *profile, *candidate = NULL;
396
21f60661
JJ
397 AA_BUG(!name);
398 AA_BUG(!head);
399
8c62ed27
JJ
400 rcu_read_lock();
401restart:
01e2b670 402 list_for_each_entry_rcu(profile, head, base.list) {
06d426d1
JJ
403 if (profile->label.flags & FLAG_NULL &&
404 &profile->label == ns_unconfined(profile->ns))
898127c3 405 continue;
06d426d1 406
8e51f908
MG
407 /* Find the "best" matching profile. Profiles must
408 * match the path and extended attributes (if any)
409 * associated with the file. A more specific path
410 * match will be preferred over a less specific one,
411 * and a match with more matching extended attributes
412 * will be preferred over one with fewer. If the best
413 * match has both the same level of path specificity
414 * and the same number of matching extended attributes
415 * as another profile, signal a conflict and refuse to
416 * match.
417 */
048d4954 418 if (profile->xmatch.dfa) {
21f60661 419 unsigned int state, count;
2d63dd43 420 u32 index, perm;
8e51f908 421
048d4954
JJ
422 state = aa_dfa_leftmatch(profile->xmatch.dfa,
423 profile->xmatch.start[AA_CLASS_XMATCH],
424 name, &count);
2d63dd43
JJ
425 index = ACCEPT_TABLE(profile->xmatch.dfa)[state];
426 perm = profile->xmatch.perms[index].allow;
8e51f908
MG
427 /* any accepting state means a valid match. */
428 if (perm & MAY_EXEC) {
8c62ed27 429 int ret = 0;
21f60661
JJ
430
431 if (count < candidate_len)
432 continue;
8e51f908 433
8c62ed27
JJ
434 if (bprm && profile->xattr_count) {
435 long rev = READ_ONCE(ns->revision);
436
437 if (!aa_get_profile_not0(profile))
438 goto restart;
439 rcu_read_unlock();
440 ret = aa_xattrs_match(bprm, profile,
441 state);
442 rcu_read_lock();
443 aa_put_profile(profile);
444 if (rev !=
445 READ_ONCE(ns->revision))
446 /* policy changed */
447 goto restart;
448 /*
449 * Fail matching if the xattrs don't
450 * match
451 */
452 if (ret < 0)
453 continue;
454 }
73f488cd
JJ
455 /*
456 * TODO: allow for more flexible best match
457 *
458 * The new match isn't more specific
8e51f908
MG
459 * than the current best match
460 */
21f60661
JJ
461 if (count == candidate_len &&
462 ret <= candidate_xattrs) {
8e51f908 463 /* Match is equivalent, so conflict */
21f60661 464 if (ret == candidate_xattrs)
1a3881d3 465 conflict = true;
8e51f908 466 continue;
844b8292 467 }
8e51f908
MG
468
469 /* Either the same length with more matching
470 * xattrs, or a longer match
471 */
472 candidate = profile;
2504db20 473 candidate_len = max(count, profile->xmatch_len);
21f60661 474 candidate_xattrs = ret;
8e51f908 475 conflict = false;
898127c3 476 }
8c62ed27 477 } else if (!strcmp(profile->base.name, name)) {
73f488cd
JJ
478 /*
479 * old exact non-re match, without conditionals such
480 * as xattrs. no more searching required
481 */
8c62ed27
JJ
482 candidate = profile;
483 goto out;
484 }
898127c3
JJ
485 }
486
8c62ed27
JJ
487 if (!candidate || conflict) {
488 if (conflict)
489 *info = "conflicting profile attachments";
490 rcu_read_unlock();
844b8292
JJ
491 return NULL;
492 }
493
8c62ed27
JJ
494out:
495 candidate = aa_get_newest_profile(candidate);
01e2b670 496 rcu_read_unlock();
898127c3 497
8c62ed27 498 return &candidate->label;
898127c3
JJ
499}
500
501static const char *next_name(int xtype, const char *name)
502{
503 return NULL;
504}
505
506/**
507 * x_table_lookup - lookup an x transition name via transition table
508 * @profile: current profile (NOT NULL)
509 * @xindex: index into x transition table
93c98a48 510 * @name: returns: name tested to find label (NOT NULL)
898127c3 511 *
93c98a48 512 * Returns: refcounted label, or NULL on failure (MAYBE NULL)
898127c3 513 */
2ea3ffb7
JJ
514struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
515 const char **name)
898127c3 516{
93c98a48 517 struct aa_label *label = NULL;
898127c3
JJ
518 u32 xtype = xindex & AA_X_TYPE_MASK;
519 int index = xindex & AA_X_INDEX_MASK;
898127c3 520
93c98a48 521 AA_BUG(!name);
898127c3 522
93c98a48
JJ
523 /* index is guaranteed to be in range, validated at load time */
524 /* TODO: move lookup parsing to unpack time so this is a straight
525 * index into the resultant label
526 */
527 for (*name = profile->file.trans.table[index]; !label && *name;
528 *name = next_name(xtype, *name)) {
898127c3 529 if (xindex & AA_X_CHILD) {
93c98a48 530 struct aa_profile *new_profile;
898127c3 531 /* release by caller */
93c98a48
JJ
532 new_profile = aa_find_child(profile, *name);
533 if (new_profile)
534 label = &new_profile->label;
898127c3 535 continue;
898127c3 536 }
8ac2ca32 537 label = aa_label_parse(&profile->label, *name, GFP_KERNEL,
93c98a48
JJ
538 true, false);
539 if (IS_ERR(label))
540 label = NULL;
898127c3
JJ
541 }
542
543 /* released by caller */
93c98a48
JJ
544
545 return label;
898127c3
JJ
546}
547
548/**
93c98a48 549 * x_to_label - get target label for a given xindex
898127c3 550 * @profile: current profile (NOT NULL)
8e51f908 551 * @bprm: binprm structure of transitioning task
898127c3
JJ
552 * @name: name to lookup (NOT NULL)
553 * @xindex: index into x transition table
93c98a48 554 * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
898127c3 555 *
93c98a48 556 * find label for a transition index
898127c3 557 *
93c98a48 558 * Returns: refcounted label or NULL if not found available
898127c3 559 */
93c98a48 560static struct aa_label *x_to_label(struct aa_profile *profile,
8e51f908 561 const struct linux_binprm *bprm,
93c98a48
JJ
562 const char *name, u32 xindex,
563 const char **lookupname,
564 const char **info)
898127c3 565{
93c98a48 566 struct aa_label *new = NULL;
98849dff 567 struct aa_ns *ns = profile->ns;
898127c3 568 u32 xtype = xindex & AA_X_TYPE_MASK;
93c98a48 569 const char *stack = NULL;
898127c3
JJ
570
571 switch (xtype) {
572 case AA_X_NONE:
573 /* fail exec unless ix || ux fallback - handled by caller */
93c98a48
JJ
574 *lookupname = NULL;
575 break;
576 case AA_X_TABLE:
577 /* TODO: fix when perm mapping done at unload */
578 stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
579 if (*stack != '&') {
580 /* released by caller */
581 new = x_table_lookup(profile, xindex, lookupname);
582 stack = NULL;
583 break;
584 }
df561f66 585 fallthrough; /* to X_NAME */
898127c3
JJ
586 case AA_X_NAME:
587 if (xindex & AA_X_CHILD)
588 /* released by caller */
8e51f908 589 new = find_attach(bprm, ns, &profile->base.profiles,
844b8292 590 name, info);
898127c3
JJ
591 else
592 /* released by caller */
8e51f908 593 new = find_attach(bprm, ns, &ns->base.profiles,
844b8292 594 name, info);
93c98a48 595 *lookupname = name;
898127c3
JJ
596 break;
597 }
598
93c98a48
JJ
599 if (!new) {
600 if (xindex & AA_X_INHERIT) {
601 /* (p|c|n)ix - don't change profile but do
602 * use the newest version
603 */
604 *info = "ix fallback";
605 /* no profile && no error */
606 new = aa_get_newest_label(&profile->label);
607 } else if (xindex & AA_X_UNCONFINED) {
608 new = aa_get_newest_label(ns_unconfined(profile->ns));
609 *info = "ux fallback";
610 }
611 }
612
613 if (new && stack) {
614 /* base the stack on post domain transition */
615 struct aa_label *base = new;
616
8ac2ca32 617 new = aa_label_parse(base, stack, GFP_KERNEL, true, false);
93c98a48
JJ
618 if (IS_ERR(new))
619 new = NULL;
620 aa_put_label(base);
621 }
622
898127c3 623 /* released by caller */
93c98a48 624 return new;
898127c3
JJ
625}
626
93c98a48
JJ
627static struct aa_label *profile_transition(struct aa_profile *profile,
628 const struct linux_binprm *bprm,
629 char *buffer, struct path_cond *cond,
630 bool *secure_exec)
898127c3 631{
93c98a48
JJ
632 struct aa_label *new = NULL;
633 const char *info = NULL, *name = NULL, *target = NULL;
53bdc46f 634 unsigned int state = profile->file.start[AA_CLASS_FILE];
2d679f3c 635 struct aa_perms perms = {};
93c98a48 636 bool nonewprivs = false;
b1d9e6b0 637 int error = 0;
898127c3 638
93c98a48
JJ
639 AA_BUG(!profile);
640 AA_BUG(!bprm);
641 AA_BUG(!buffer);
898127c3 642
4227c333 643 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
72c8a768 644 &name, &info, profile->disconnected);
898127c3 645 if (error) {
637f688d 646 if (profile_unconfined(profile) ||
93c98a48
JJ
647 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
648 AA_DEBUG("name lookup ix on error");
898127c3 649 error = 0;
93c98a48
JJ
650 new = aa_get_newest_label(&profile->label);
651 }
898127c3
JJ
652 name = bprm->filename;
653 goto audit;
654 }
655
637f688d 656 if (profile_unconfined(profile)) {
8e51f908
MG
657 new = find_attach(bprm, profile->ns,
658 &profile->ns->base.profiles, name, &info);
93c98a48
JJ
659 if (new) {
660 AA_DEBUG("unconfined attached to new label");
661 return new;
662 }
663 AA_DEBUG("unconfined exec no attachment");
664 return aa_get_newest_label(&profile->label);
898127c3
JJ
665 }
666
667 /* find exec permissions for name */
408d53e9 668 state = aa_str_perms(&(profile->file), state, name, cond, &perms);
898127c3
JJ
669 if (perms.allow & MAY_EXEC) {
670 /* exec permission determine how to transition */
8e51f908
MG
671 new = x_to_label(profile, bprm, name, perms.xindex, &target,
672 &info);
93c98a48
JJ
673 if (new && new->proxy == profile->label.proxy && info) {
674 /* hack ix fallback - improve how this is detected */
675 goto audit;
676 } else if (!new) {
677 error = -EACCES;
678 info = "profile transition not found";
679 /* remove MAY_EXEC to audit as failure */
680 perms.allow &= ~MAY_EXEC;
898127c3
JJ
681 }
682 } else if (COMPLAIN_MODE(profile)) {
93c98a48 683 /* no exec permission - learning mode */
5d7c44ef 684 struct aa_profile *new_profile = NULL;
df323337
SAS
685
686 new_profile = aa_new_null_profile(profile, false, name,
687 GFP_KERNEL);
898127c3
JJ
688 if (!new_profile) {
689 error = -ENOMEM;
690 info = "could not create null profile";
93c98a48 691 } else {
898127c3 692 error = -EACCES;
93c98a48
JJ
693 new = &new_profile->label;
694 }
898127c3
JJ
695 perms.xindex |= AA_X_UNSAFE;
696 } else
697 /* fail exec */
698 error = -EACCES;
699
93c98a48
JJ
700 if (!new)
701 goto audit;
702
93c98a48
JJ
703
704 if (!(perms.xindex & AA_X_UNSAFE)) {
705 if (DEBUG_ON) {
706 dbg_printk("apparmor: scrubbing environment variables"
707 " for %s profile=", name);
8ac2ca32 708 aa_label_printk(new, GFP_KERNEL);
93c98a48
JJ
709 dbg_printk("\n");
710 }
711 *secure_exec = true;
c29bceb3
JJ
712 }
713
93c98a48
JJ
714audit:
715 aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
716 cond->uid, info, error);
717 if (!new || nonewprivs) {
718 aa_put_label(new);
719 return ERR_PTR(error);
720 }
721
722 return new;
723}
724
725static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec,
726 bool stack, const struct linux_binprm *bprm,
727 char *buffer, struct path_cond *cond,
728 bool *secure_exec)
729{
53bdc46f 730 unsigned int state = profile->file.start[AA_CLASS_FILE];
93c98a48
JJ
731 struct aa_perms perms = {};
732 const char *xname = NULL, *info = "change_profile onexec";
733 int error = -EACCES;
734
735 AA_BUG(!profile);
736 AA_BUG(!onexec);
737 AA_BUG(!bprm);
738 AA_BUG(!buffer);
739
740 if (profile_unconfined(profile)) {
741 /* change_profile on exec already granted */
742 /*
743 * NOTE: Domain transitions from unconfined are allowed
744 * even when no_new_privs is set because this aways results
745 * in a further reduction of permissions.
746 */
747 return 0;
748 }
749
750 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
751 &xname, &info, profile->disconnected);
752 if (error) {
753 if (profile_unconfined(profile) ||
754 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
755 AA_DEBUG("name lookup ix on error");
756 error = 0;
757 }
758 xname = bprm->filename;
898127c3 759 goto audit;
93c98a48
JJ
760 }
761
762 /* find exec permissions for name */
408d53e9 763 state = aa_str_perms(&(profile->file), state, xname, cond, &perms);
93c98a48
JJ
764 if (!(perms.allow & AA_MAY_ONEXEC)) {
765 info = "no change_onexec valid for executable";
766 goto audit;
767 }
768 /* test if this exec can be paired with change_profile onexec.
769 * onexec permission is linked to exec with a standard pairing
770 * exec\0change_profile
771 */
772 state = aa_dfa_null_transition(profile->file.dfa, state);
773 error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
774 state, &perms);
775 if (error) {
776 perms.allow &= ~AA_MAY_ONEXEC;
777 goto audit;
778 }
93c98a48
JJ
779
780 if (!(perms.xindex & AA_X_UNSAFE)) {
781 if (DEBUG_ON) {
782 dbg_printk("apparmor: scrubbing environment "
783 "variables for %s label=", xname);
8ac2ca32 784 aa_label_printk(onexec, GFP_KERNEL);
93c98a48
JJ
785 dbg_printk("\n");
786 }
787 *secure_exec = true;
788 }
789
790audit:
791 return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
792 NULL, onexec, cond->uid, info, error);
793}
794
795/* ensure none ns domain transitions are correctly applied with onexec */
796
797static struct aa_label *handle_onexec(struct aa_label *label,
798 struct aa_label *onexec, bool stack,
799 const struct linux_binprm *bprm,
800 char *buffer, struct path_cond *cond,
801 bool *unsafe)
802{
803 struct aa_profile *profile;
804 struct aa_label *new;
805 int error;
806
807 AA_BUG(!label);
808 AA_BUG(!onexec);
809 AA_BUG(!bprm);
810 AA_BUG(!buffer);
811
812 if (!stack) {
813 error = fn_for_each_in_ns(label, profile,
814 profile_onexec(profile, onexec, stack,
815 bprm, buffer, cond, unsafe));
816 if (error)
817 return ERR_PTR(error);
8ac2ca32 818 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
93c98a48
JJ
819 aa_get_newest_label(onexec),
820 profile_transition(profile, bprm, buffer,
821 cond, unsafe));
822
823 } else {
b2c2086c 824 /* TODO: determine how much we want to loosen this */
93c98a48
JJ
825 error = fn_for_each_in_ns(label, profile,
826 profile_onexec(profile, onexec, stack, bprm,
827 buffer, cond, unsafe));
828 if (error)
829 return ERR_PTR(error);
8ac2ca32 830 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
93c98a48 831 aa_label_merge(&profile->label, onexec,
8ac2ca32 832 GFP_KERNEL),
93c98a48
JJ
833 profile_transition(profile, bprm, buffer,
834 cond, unsafe));
835 }
836
837 if (new)
838 return new;
839
840 /* TODO: get rid of GLOBAL_ROOT_UID */
841 error = fn_for_each_in_ns(label, profile,
842 aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC,
843 AA_MAY_ONEXEC, bprm->filename, NULL,
844 onexec, GLOBAL_ROOT_UID,
845 "failed to build target label", -ENOMEM));
846 return ERR_PTR(error);
847}
848
849/**
b8bff599 850 * apparmor_bprm_creds_for_exec - Update the new creds on the bprm struct
93c98a48
JJ
851 * @bprm: binprm for the exec (NOT NULL)
852 *
853 * Returns: %0 or error on failure
854 *
855 * TODO: once the other paths are done see if we can't refactor into a fn
856 */
b8bff599 857int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm)
93c98a48 858{
f175221a 859 struct aa_task_ctx *ctx;
93c98a48
JJ
860 struct aa_label *label, *new = NULL;
861 struct aa_profile *profile;
862 char *buffer = NULL;
863 const char *info = NULL;
864 int error = 0;
865 bool unsafe = false;
3cee6079
CB
866 kuid_t i_uid = i_uid_into_mnt(file_mnt_user_ns(bprm->file),
867 file_inode(bprm->file));
93c98a48 868 struct path_cond cond = {
3cee6079 869 i_uid,
93c98a48
JJ
870 file_inode(bprm->file)->i_mode
871 };
872
de62de59 873 ctx = task_ctx(current);
d9087c49 874 AA_BUG(!cred_label(bprm->cred));
f175221a 875 AA_BUG(!ctx);
93c98a48 876
d9087c49 877 label = aa_get_newest_label(cred_label(bprm->cred));
93c98a48 878
9fcf78cc
JJ
879 /*
880 * Detect no new privs being set, and store the label it
881 * occurred under. Ideally this would happen when nnp
882 * is set but there isn't a good way to do that yet.
883 *
884 * Testing for unconfined must be done before the subset test
885 */
886 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
887 !ctx->nnp)
888 ctx->nnp = aa_get_label(label);
889
93c98a48 890 /* buffer freed below, name is pointer into buffer */
341c1fda 891 buffer = aa_get_buffer(false);
df323337
SAS
892 if (!buffer) {
893 error = -ENOMEM;
894 goto done;
895 }
896
93c98a48 897 /* Test for onexec first as onexec override other x transitions. */
f175221a
JJ
898 if (ctx->onexec)
899 new = handle_onexec(label, ctx->onexec, ctx->token,
93c98a48
JJ
900 bprm, buffer, &cond, &unsafe);
901 else
8ac2ca32 902 new = fn_label_build(label, profile, GFP_KERNEL,
93c98a48
JJ
903 profile_transition(profile, bprm, buffer,
904 &cond, &unsafe));
905
906 AA_BUG(!new);
907 if (IS_ERR(new)) {
908 error = PTR_ERR(new);
909 goto done;
910 } else if (!new) {
911 error = -ENOMEM;
912 goto done;
913 }
914
9fcf78cc
JJ
915 /* Policy has specified a domain transitions. If no_new_privs and
916 * confined ensure the transition is to confinement that is subset
917 * of the confinement when the task entered no new privs.
918 *
919 * NOTE: Domain transitions from unconfined and to stacked
920 * subsets are allowed even when no_new_privs is set because this
921 * aways results in a further reduction of permissions.
922 */
923 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
3ed4aaa9
JJ
924 !unconfined(label) &&
925 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
9fcf78cc
JJ
926 error = -EPERM;
927 info = "no new privs";
928 goto audit;
929 }
898127c3
JJ
930
931 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
932 /* FIXME: currently don't mediate shared state */
933 ;
934 }
935
93c98a48
JJ
936 if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
937 /* TODO: test needs to be profile of label to new */
938 error = may_change_ptraced_domain(new, &info);
f7da2de0 939 if (error)
898127c3 940 goto audit;
898127c3
JJ
941 }
942
93c98a48
JJ
943 if (unsafe) {
944 if (DEBUG_ON) {
945 dbg_printk("scrubbing environment variables for %s "
946 "label=", bprm->filename);
8ac2ca32 947 aa_label_printk(new, GFP_KERNEL);
93c98a48
JJ
948 dbg_printk("\n");
949 }
993b3ab0 950 bprm->secureexec = 1;
898127c3 951 }
898127c3 952
93c98a48
JJ
953 if (label->proxy != new->proxy) {
954 /* when transitioning clear unsafe personality bits */
955 if (DEBUG_ON) {
956 dbg_printk("apparmor: clearing unsafe personality "
957 "bits. %s label=", bprm->filename);
8ac2ca32 958 aa_label_printk(new, GFP_KERNEL);
93c98a48
JJ
959 dbg_printk("\n");
960 }
961 bprm->per_clear |= PER_CLEAR_ON_SETID;
962 }
d9087c49
JJ
963 aa_put_label(cred_label(bprm->cred));
964 /* transfer reference, released when cred is freed */
69b5a44a 965 set_cred_label(bprm->cred, new);
898127c3 966
93c98a48 967done:
637f688d 968 aa_put_label(label);
df323337 969 aa_put_buffer(buffer);
898127c3
JJ
970
971 return error;
93c98a48
JJ
972
973audit:
974 error = fn_for_each(label, profile,
975 aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
976 bprm->filename, NULL, new,
3cee6079 977 i_uid, info, error));
93c98a48
JJ
978 aa_put_label(new);
979 goto done;
898127c3
JJ
980}
981
898127c3
JJ
982/*
983 * Functions for self directed profile change
984 */
985
89dbf196
JJ
986
987/* helper fn for change_hat
898127c3 988 *
89dbf196 989 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
898127c3 990 */
89dbf196
JJ
991static struct aa_label *build_change_hat(struct aa_profile *profile,
992 const char *name, bool sibling)
898127c3 993{
89dbf196
JJ
994 struct aa_profile *root, *hat = NULL;
995 const char *info = NULL;
996 int error = 0;
997
998 if (sibling && PROFILE_IS_HAT(profile)) {
999 root = aa_get_profile_rcu(&profile->parent);
1000 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
1001 root = aa_get_profile(profile);
1002 } else {
1003 info = "conflicting target types";
1004 error = -EPERM;
1005 goto audit;
1006 }
1007
1008 hat = aa_find_child(root, name);
1009 if (!hat) {
1010 error = -ENOENT;
1011 if (COMPLAIN_MODE(profile)) {
1012 hat = aa_new_null_profile(profile, true, name,
1013 GFP_KERNEL);
1014 if (!hat) {
1015 info = "failed null profile create";
1016 error = -ENOMEM;
1017 }
1018 }
1019 }
1020 aa_put_profile(root);
1021
1022audit:
1023 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
1024 name, hat ? hat->base.hname : NULL,
24b87a16 1025 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, info,
89dbf196
JJ
1026 error);
1027 if (!hat || (error && error != -ENOENT))
1028 return ERR_PTR(error);
1029 /* if hat && error - complain mode, already audited and we adjust for
1030 * complain mode allow by returning hat->label
1031 */
1032 return &hat->label;
1033}
1034
1035/* helper fn for changing into a hat
1036 *
1037 * Returns: label for hat transition or ERR_PTR. Does not return NULL
1038 */
1039static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
1040 int count, int flags)
1041{
1042 struct aa_profile *profile, *root, *hat = NULL;
1043 struct aa_label *new;
1044 struct label_it it;
1045 bool sibling = false;
1046 const char *name, *info = NULL;
1047 int i, error;
1048
1049 AA_BUG(!label);
1050 AA_BUG(!hats);
1051 AA_BUG(count < 1);
1052
1053 if (PROFILE_IS_HAT(labels_profile(label)))
1054 sibling = true;
1055
1056 /*find first matching hat */
1057 for (i = 0; i < count && !hat; i++) {
1058 name = hats[i];
1059 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1060 if (sibling && PROFILE_IS_HAT(profile)) {
1061 root = aa_get_profile_rcu(&profile->parent);
1062 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
1063 root = aa_get_profile(profile);
1064 } else { /* conflicting change type */
1065 info = "conflicting targets types";
1066 error = -EPERM;
1067 goto fail;
1068 }
1069 hat = aa_find_child(root, name);
1070 aa_put_profile(root);
1071 if (!hat) {
1072 if (!COMPLAIN_MODE(profile))
1073 goto outer_continue;
1074 /* complain mode succeed as if hat */
1075 } else if (!PROFILE_IS_HAT(hat)) {
1076 info = "target not hat";
1077 error = -EPERM;
1078 aa_put_profile(hat);
1079 goto fail;
1080 }
1081 aa_put_profile(hat);
1082 }
1083 /* found a hat for all profiles in ns */
1084 goto build;
1085outer_continue:
1086 ;
1087 }
1088 /* no hats that match, find appropriate error
1089 *
1090 * In complain mode audit of the failure is based off of the first
1091 * hat supplied. This is done due how userspace interacts with
1092 * change_hat.
1093 */
1094 name = NULL;
1095 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1096 if (!list_empty(&profile->base.profiles)) {
1097 info = "hat not found";
1098 error = -ENOENT;
1099 goto fail;
1100 }
1101 }
1102 info = "no hats defined";
1103 error = -ECHILD;
1104
1105fail:
1106 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1107 /*
1108 * no target as it has failed to be found or built
1109 *
1110 * change_hat uses probing and should not log failures
1111 * related to missing hats
1112 */
1113 /* TODO: get rid of GLOBAL_ROOT_UID */
1114 if (count > 1 || COMPLAIN_MODE(profile)) {
1115 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
1116 AA_MAY_CHANGEHAT, name, NULL, NULL,
1117 GLOBAL_ROOT_UID, info, error);
1118 }
1119 }
1120 return ERR_PTR(error);
1121
1122build:
1123 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1124 build_change_hat(profile, name, sibling),
1125 aa_get_label(&profile->label));
1126 if (!new) {
1127 info = "label build failed";
1128 error = -ENOMEM;
1129 goto fail;
1130 } /* else if (IS_ERR) build_change_hat has logged error so return new */
1131
1132 return new;
898127c3
JJ
1133}
1134
1135/**
1136 * aa_change_hat - change hat to/from subprofile
1137 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1138 * @count: number of hat names in @hats
1139 * @token: magic value to validate the hat change
df8073c6 1140 * @flags: flags affecting behavior of the change
898127c3 1141 *
89dbf196
JJ
1142 * Returns %0 on success, error otherwise.
1143 *
898127c3
JJ
1144 * Change to the first profile specified in @hats that exists, and store
1145 * the @hat_magic in the current task context. If the count == 0 and the
1146 * @token matches that stored in the current task context, return to the
1147 * top level profile.
1148 *
89dbf196
JJ
1149 * change_hat only applies to profiles in the current ns, and each profile
1150 * in the ns must make the same transition otherwise change_hat will fail.
898127c3 1151 */
df8073c6 1152int aa_change_hat(const char *hats[], int count, u64 token, int flags)
898127c3
JJ
1153{
1154 const struct cred *cred;
9fcf78cc 1155 struct aa_task_ctx *ctx = task_ctx(current);
89dbf196
JJ
1156 struct aa_label *label, *previous, *new = NULL, *target = NULL;
1157 struct aa_profile *profile;
2d679f3c 1158 struct aa_perms perms = {};
89dbf196 1159 const char *info = NULL;
898127c3
JJ
1160 int error = 0;
1161
1162 /* released below */
1163 cred = get_current_cred();
637f688d 1164 label = aa_get_newest_cred_label(cred);
f175221a 1165 previous = aa_get_newest_label(ctx->previous);
898127c3 1166
9fcf78cc
JJ
1167 /*
1168 * Detect no new privs being set, and store the label it
1169 * occurred under. Ideally this would happen when nnp
1170 * is set but there isn't a good way to do that yet.
1171 *
1172 * Testing for unconfined must be done before the subset test
1173 */
1174 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1175 ctx->nnp = aa_get_label(label);
1176
637f688d 1177 if (unconfined(label)) {
89dbf196 1178 info = "unconfined can not change_hat";
898127c3 1179 error = -EPERM;
89dbf196 1180 goto fail;
898127c3
JJ
1181 }
1182
1183 if (count) {
89dbf196
JJ
1184 new = change_hat(label, hats, count, flags);
1185 AA_BUG(!new);
1186 if (IS_ERR(new)) {
1187 error = PTR_ERR(new);
1188 new = NULL;
1189 /* already audited */
1190 goto out;
898127c3
JJ
1191 }
1192
89dbf196
JJ
1193 error = may_change_ptraced_domain(new, &info);
1194 if (error)
1195 goto fail;
898127c3 1196
9fcf78cc
JJ
1197 /*
1198 * no new privs prevents domain transitions that would
1199 * reduce restrictions.
1200 */
1201 if (task_no_new_privs(current) && !unconfined(label) &&
3ed4aaa9 1202 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
9fcf78cc
JJ
1203 /* not an apparmor denial per se, so don't log it */
1204 AA_DEBUG("no_new_privs - change_hat denied");
1205 error = -EPERM;
1206 goto out;
1207 }
1208
89dbf196
JJ
1209 if (flags & AA_CHANGE_TEST)
1210 goto out;
1211
1212 target = new;
1213 error = aa_set_current_hat(new, token);
1214 if (error == -EACCES)
1215 /* kill task in case of brute force attacks */
1216 goto kill;
1217 } else if (previous && !(flags & AA_CHANGE_TEST)) {
9fcf78cc
JJ
1218 /*
1219 * no new privs prevents domain transitions that would
1220 * reduce restrictions.
1221 */
1222 if (task_no_new_privs(current) && !unconfined(label) &&
3ed4aaa9 1223 !aa_label_is_unconfined_subset(previous, ctx->nnp)) {
9fcf78cc
JJ
1224 /* not an apparmor denial per se, so don't log it */
1225 AA_DEBUG("no_new_privs - change_hat denied");
1226 error = -EPERM;
1227 goto out;
1228 }
1229
89dbf196 1230 /* Return to saved label. Kill task if restore fails
898127c3
JJ
1231 * to avoid brute force attacks
1232 */
89dbf196 1233 target = previous;
637f688d 1234 error = aa_restore_previous_label(token);
89dbf196
JJ
1235 if (error) {
1236 if (error == -EACCES)
1237 goto kill;
1238 goto fail;
1239 }
1240 } /* else ignore @flags && restores when there is no saved profile */
898127c3
JJ
1241
1242out:
89dbf196
JJ
1243 aa_put_label(new);
1244 aa_put_label(previous);
637f688d 1245 aa_put_label(label);
898127c3
JJ
1246 put_cred(cred);
1247
1248 return error;
89dbf196
JJ
1249
1250kill:
1251 info = "failed token match";
1252 perms.kill = AA_MAY_CHANGEHAT;
1253
1254fail:
1255 fn_for_each_in_ns(label, profile,
1256 aa_audit_file(profile, &perms, OP_CHANGE_HAT,
1257 AA_MAY_CHANGEHAT, NULL, NULL, target,
1258 GLOBAL_ROOT_UID, info, error));
1259
1260 goto out;
898127c3
JJ
1261}
1262
89dbf196 1263
e00b02bb
JJ
1264static int change_profile_perms_wrapper(const char *op, const char *name,
1265 struct aa_profile *profile,
1266 struct aa_label *target, bool stack,
1267 u32 request, struct aa_perms *perms)
1268{
1269 const char *info = NULL;
1270 int error = 0;
1271
e00b02bb
JJ
1272 if (!error)
1273 error = change_profile_perms(profile, target, stack, request,
53bdc46f
JJ
1274 profile->file.start[AA_CLASS_FILE],
1275 perms);
e00b02bb
JJ
1276 if (error)
1277 error = aa_audit_file(profile, perms, op, request, name,
1278 NULL, target, GLOBAL_ROOT_UID, info,
1279 error);
1280
1281 return error;
1282}
89dbf196 1283
898127c3
JJ
1284/**
1285 * aa_change_profile - perform a one-way profile transition
aa9a39ad 1286 * @fqname: name of profile may include namespace (NOT NULL)
df8073c6 1287 * @flags: flags affecting change behavior
898127c3
JJ
1288 *
1289 * Change to new profile @name. Unlike with hats, there is no way
1290 * to change back. If @name isn't specified the current profile name is
1291 * used.
1292 * If @onexec then the transition is delayed until
1293 * the next exec.
1294 *
1295 * Returns %0 on success, error otherwise.
1296 */
df8073c6 1297int aa_change_profile(const char *fqname, int flags)
898127c3 1298{
e00b02bb
JJ
1299 struct aa_label *label, *new = NULL, *target = NULL;
1300 struct aa_profile *profile;
2d679f3c 1301 struct aa_perms perms = {};
e00b02bb
JJ
1302 const char *info = NULL;
1303 const char *auditname = fqname; /* retain leading & if stack */
1304 bool stack = flags & AA_CHANGE_STACK;
9fcf78cc 1305 struct aa_task_ctx *ctx = task_ctx(current);
47f6e5cc 1306 int error = 0;
e00b02bb 1307 char *op;
898127c3
JJ
1308 u32 request;
1309
9fcf78cc
JJ
1310 label = aa_get_current_label();
1311
1312 /*
1313 * Detect no new privs being set, and store the label it
1314 * occurred under. Ideally this would happen when nnp
1315 * is set but there isn't a good way to do that yet.
1316 *
1317 * Testing for unconfined must be done before the subset test
1318 */
1319 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1320 ctx->nnp = aa_get_label(label);
1321
aa9a39ad 1322 if (!fqname || !*fqname) {
a0b845ff 1323 aa_put_label(label);
aa9a39ad 1324 AA_DEBUG("no profile name");
898127c3 1325 return -EINVAL;
aa9a39ad 1326 }
898127c3 1327
df8073c6 1328 if (flags & AA_CHANGE_ONEXEC) {
898127c3 1329 request = AA_MAY_ONEXEC;
e00b02bb
JJ
1330 if (stack)
1331 op = OP_STACK_ONEXEC;
1332 else
1333 op = OP_CHANGE_ONEXEC;
898127c3
JJ
1334 } else {
1335 request = AA_MAY_CHANGE_PROFILE;
e00b02bb
JJ
1336 if (stack)
1337 op = OP_STACK;
1338 else
1339 op = OP_CHANGE_PROFILE;
898127c3
JJ
1340 }
1341
e00b02bb
JJ
1342 if (*fqname == '&') {
1343 stack = true;
1344 /* don't have label_parse() do stacking */
1345 fqname++;
c29bceb3 1346 }
e00b02bb
JJ
1347 target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1348 if (IS_ERR(target)) {
1349 struct aa_profile *tprofile;
c29bceb3 1350
e00b02bb
JJ
1351 info = "label not found";
1352 error = PTR_ERR(target);
1353 target = NULL;
1354 /*
1355 * TODO: fixme using labels_profile is not right - do profile
1356 * per complain profile
1357 */
df8073c6 1358 if ((flags & AA_CHANGE_TEST) ||
e00b02bb 1359 !COMPLAIN_MODE(labels_profile(label)))
898127c3
JJ
1360 goto audit;
1361 /* released below */
e00b02bb
JJ
1362 tprofile = aa_new_null_profile(labels_profile(label), false,
1363 fqname, GFP_KERNEL);
1364 if (!tprofile) {
898127c3
JJ
1365 info = "failed null profile create";
1366 error = -ENOMEM;
1367 goto audit;
1368 }
e00b02bb
JJ
1369 target = &tprofile->label;
1370 goto check;
898127c3
JJ
1371 }
1372
e00b02bb
JJ
1373 /*
1374 * self directed transitions only apply to current policy ns
1375 * TODO: currently requiring perms for stacking and straight change
1376 * stacking doesn't strictly need this. Determine how much
1377 * we want to loosen this restriction for stacking
1378 *
1379 * if (!stack) {
1380 */
1381 error = fn_for_each_in_ns(label, profile,
1382 change_profile_perms_wrapper(op, auditname,
1383 profile, target, stack,
1384 request, &perms));
1385 if (error)
1386 /* auditing done in change_profile_perms_wrapper */
1387 goto out;
aa9a39ad 1388
e00b02bb
JJ
1389 /* } */
1390
1391check:
898127c3 1392 /* check if tracing task is allowed to trace target domain */
e00b02bb
JJ
1393 error = may_change_ptraced_domain(target, &info);
1394 if (error && !fn_for_each_in_ns(label, profile,
1395 COMPLAIN_MODE(profile)))
898127c3 1396 goto audit;
898127c3 1397
e00b02bb
JJ
1398 /* TODO: add permission check to allow this
1399 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
1400 * info = "not a single threaded task";
1401 * error = -EACCES;
1402 * goto audit;
1403 * }
1404 */
df8073c6 1405 if (flags & AA_CHANGE_TEST)
e00b02bb 1406 goto out;
898127c3 1407
9fcf78cc
JJ
1408 /* stacking is always a subset, so only check the nonstack case */
1409 if (!stack) {
1410 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1411 aa_get_label(target),
1412 aa_get_label(&profile->label));
1413 /*
1414 * no new privs prevents domain transitions that would
1415 * reduce restrictions.
1416 */
1417 if (task_no_new_privs(current) && !unconfined(label) &&
3ed4aaa9 1418 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
9fcf78cc
JJ
1419 /* not an apparmor denial per se, so don't log it */
1420 AA_DEBUG("no_new_privs - change_hat denied");
1421 error = -EPERM;
1422 goto out;
1423 }
1424 }
1425
e00b02bb
JJ
1426 if (!(flags & AA_CHANGE_ONEXEC)) {
1427 /* only transition profiles in the current ns */
1428 if (stack)
1429 new = aa_label_merge(label, target, GFP_KERNEL);
e00b02bb
JJ
1430 if (IS_ERR_OR_NULL(new)) {
1431 info = "failed to build target label";
d6d478ae
JJ
1432 if (!new)
1433 error = -ENOMEM;
1434 else
1435 error = PTR_ERR(new);
e00b02bb
JJ
1436 new = NULL;
1437 perms.allow = 0;
1438 goto audit;
1439 }
1440 error = aa_replace_current_label(new);
9fcf78cc
JJ
1441 } else {
1442 if (new) {
1443 aa_put_label(new);
1444 new = NULL;
1445 }
1446
e00b02bb
JJ
1447 /* full transition will be built in exec path */
1448 error = aa_set_current_onexec(target, stack);
9fcf78cc 1449 }
898127c3
JJ
1450
1451audit:
e00b02bb
JJ
1452 error = fn_for_each_in_ns(label, profile,
1453 aa_audit_file(profile, &perms, op, request, auditname,
1454 NULL, new ? new : target,
1455 GLOBAL_ROOT_UID, info, error));
898127c3 1456
e00b02bb
JJ
1457out:
1458 aa_put_label(new);
1459 aa_put_label(target);
637f688d 1460 aa_put_label(label);
898127c3
JJ
1461
1462 return error;
1463}