1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor policy attachment and domain transitions
7 * Copyright (C) 2002-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
11 #include <linux/errno.h>
12 #include <linux/fdtable.h>
14 #include <linux/file.h>
15 #include <linux/mount.h>
16 #include <linux/syscalls.h>
17 #include <linux/personality.h>
18 #include <linux/xattr.h>
19 #include <linux/user_namespace.h>
21 #include "include/audit.h"
22 #include "include/apparmorfs.h"
23 #include "include/cred.h"
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"
30 #include "include/policy_ns.h"
33 * aa_free_domain_entries - free entries in a domain table
34 * @domain: the domain table to free (MAYBE NULL)
36 void aa_free_domain_entries(struct aa_domain *domain)
43 for (i = 0; i < domain->size; i++)
44 kfree_sensitive(domain->table[i]);
45 kfree_sensitive(domain->table);
51 * may_change_ptraced_domain - check if can change profile on ptraced task
52 * @to_label: profile to change to (NOT NULL)
53 * @info: message if there is an error
55 * Check if current is ptraced and if so if the tracing task is allowed
56 * to trace the new domain
58 * Returns: %0 or error if change not allowed
60 static int may_change_ptraced_domain(struct aa_label *to_label,
63 struct task_struct *tracer;
64 struct aa_label *tracerl = NULL;
68 tracer = ptrace_parent(current);
71 tracerl = aa_get_task_label(tracer);
74 if (!tracer || unconfined(tracerl))
77 error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
81 aa_put_label(tracerl);
84 *info = "ptrace prevents transition";
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
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
98 static inline unsigned int match_component(struct aa_profile *profile,
99 struct aa_profile *tp,
100 bool stack, unsigned int state)
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);
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);
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
122 * @state: state to start match in
123 * @subns: whether to do permission checks on components in a subns
124 * @request: permissions to request
125 * @perms: perms struct to set
127 * Returns: 0 on success else ERROR
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.
133 static 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)
138 struct aa_profile *tp;
140 struct path_cond cond = { };
142 /* find first subcomponent that is visible */
143 label_for_each(i, label, tp) {
144 if (!aa_ns_visible(profile->ns, tp->ns, subns))
146 state = match_component(profile, tp, stack, state);
152 /* no component visible */
157 label_for_each_cont(i, label, tp) {
158 if (!aa_ns_visible(profile->ns, tp->ns, subns))
160 state = aa_dfa_match(profile->file.dfa, state, "//&");
161 state = match_component(profile, tp, false, state);
165 *perms = *(aa_lookup_fperms(&(profile->file), state, &cond));
166 aa_apply_modes_to_perms(profile, perms);
167 if ((perms->allow & request) != request)
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
187 * Returns: 0 on success else ERROR
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.
193 static 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)
198 struct aa_profile *tp;
201 struct path_cond cond = { };
202 unsigned int state = 0;
204 /* find first subcomponent to test */
205 label_for_each(i, label, tp) {
206 if (!aa_ns_visible(profile->ns, tp->ns, subns))
208 state = match_component(profile, tp, stack, start);
214 /* no subcomponents visible - no change in perms */
218 tmp = *(aa_lookup_fperms(&(profile->file), state, &cond));
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))
224 state = match_component(profile, tp, stack, start);
227 tmp = *(aa_lookup_fperms(&(profile->file), state, &cond));
228 aa_apply_modes_to_perms(profile, &tmp);
229 aa_perms_accum(perms, &tmp);
232 if ((perms->allow & request) != request)
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)
252 * Returns: the state the match finished in, may be the none matching state
254 static 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)
261 error = label_compound_match(profile, label, stack, state, subns,
267 return label_components_match(profile, label, stack, state, subns,
271 /******* end TODO: dedup *****/
274 * change_profile_perms - find permissions for change_profile
275 * @profile: the current profile (NOT NULL)
276 * @target: label to transition to (NOT NULL)
277 * @stack: whether this is a stacking request
278 * @request: requested perms
279 * @start: state to start matching in
282 * Returns: permission set
284 * currently only matches full label A//&B//&C or individual components A, B, C
285 * not arbitrary combinations. Eg. A//&B, C
287 static 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)
292 if (profile_unconfined(profile)) {
293 perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
294 perms->audit = perms->quiet = perms->kill = 0;
298 /* TODO: add profile in ns screening */
299 return label_match(profile, target, stack, start, true, request, perms);
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)
306 * @state: state to start match in
308 * Returns: number of extended attributes that matched, or < 0 on error
310 static int aa_xattrs_match(const struct linux_binprm *bprm,
311 struct aa_profile *profile, unsigned int state)
317 int value_size = 0, ret = profile->xattr_count;
319 if (!bprm || !profile->xattr_count)
323 /* transition from exec match to xattr set */
324 state = aa_dfa_outofband_transition(profile->xmatch.dfa, state);
325 d = bprm->file->f_path.dentry;
327 for (i = 0; i < profile->xattr_count; i++) {
328 size = vfs_getxattr_alloc(&init_user_ns, d, profile->xattrs[i],
329 &value, value_size, GFP_KERNEL);
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
338 state = aa_dfa_null_transition(profile->xmatch.dfa,
340 /* Check xattr value */
341 state = aa_dfa_match_len(profile->xmatch.dfa, state,
343 index = ACCEPT_TABLE(profile->xmatch.dfa)[state];
344 perm = profile->xmatch.perms[index].allow;
345 if (!(perm & MAY_EXEC)) {
350 /* transition to next element */
351 state = aa_dfa_outofband_transition(profile->xmatch.dfa, state);
354 * No xattr match, so verify if transition to
355 * next element was valid. IFF so the xattr
362 /* don't count missing optional xattr as matched */
373 * find_attach - do attachment search for unconfined processes
374 * @bprm - binprm structure of transitioning task
375 * @ns: the current namespace (NOT NULL)
376 * @head - profile list to walk (NOT NULL)
377 * @name - to match against (NOT NULL)
378 * @info - info message if there was an error (NOT NULL)
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.
385 * Requires: @head not be shared or have appropriate locks held
387 * Returns: label or NULL if no match found
389 static 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)
393 int candidate_len = 0, candidate_xattrs = 0;
394 bool conflict = false;
395 struct aa_profile *profile, *candidate = NULL;
402 list_for_each_entry_rcu(profile, head, base.list) {
403 if (profile->label.flags & FLAG_NULL &&
404 &profile->label == ns_unconfined(profile->ns))
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
418 if (profile->xmatch.dfa) {
419 unsigned int state, count;
422 state = aa_dfa_leftmatch(profile->xmatch.dfa,
423 profile->xmatch.start[AA_CLASS_XMATCH],
425 index = ACCEPT_TABLE(profile->xmatch.dfa)[state];
426 perm = profile->xmatch.perms[index].allow;
427 /* any accepting state means a valid match. */
428 if (perm & MAY_EXEC) {
431 if (count < candidate_len)
434 if (bprm && profile->xattr_count) {
435 long rev = READ_ONCE(ns->revision);
437 if (!aa_get_profile_not0(profile))
440 ret = aa_xattrs_match(bprm, profile,
443 aa_put_profile(profile);
445 READ_ONCE(ns->revision))
449 * Fail matching if the xattrs don't
456 * TODO: allow for more flexible best match
458 * The new match isn't more specific
459 * than the current best match
461 if (count == candidate_len &&
462 ret <= candidate_xattrs) {
463 /* Match is equivalent, so conflict */
464 if (ret == candidate_xattrs)
469 /* Either the same length with more matching
470 * xattrs, or a longer match
473 candidate_len = max(count, profile->xmatch_len);
474 candidate_xattrs = ret;
477 } else if (!strcmp(profile->base.name, name)) {
479 * old exact non-re match, without conditionals such
480 * as xattrs. no more searching required
487 if (!candidate || conflict) {
489 *info = "conflicting profile attachments";
495 candidate = aa_get_newest_profile(candidate);
498 return &candidate->label;
501 static const char *next_name(int xtype, const char *name)
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
510 * @name: returns: name tested to find label (NOT NULL)
512 * Returns: refcounted label, or NULL on failure (MAYBE NULL)
514 struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
517 struct aa_label *label = NULL;
518 u32 xtype = xindex & AA_X_TYPE_MASK;
519 int index = xindex & AA_X_INDEX_MASK;
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
527 for (*name = profile->file.trans.table[index]; !label && *name;
528 *name = next_name(xtype, *name)) {
529 if (xindex & AA_X_CHILD) {
530 struct aa_profile *new_profile;
531 /* release by caller */
532 new_profile = aa_find_child(profile, *name);
534 label = &new_profile->label;
537 label = aa_label_parse(&profile->label, *name, GFP_KERNEL,
543 /* released by caller */
549 * x_to_label - get target label for a given xindex
550 * @profile: current profile (NOT NULL)
551 * @bprm: binprm structure of transitioning task
552 * @name: name to lookup (NOT NULL)
553 * @xindex: index into x transition table
554 * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
556 * find label for a transition index
558 * Returns: refcounted label or NULL if not found available
560 static struct aa_label *x_to_label(struct aa_profile *profile,
561 const struct linux_binprm *bprm,
562 const char *name, u32 xindex,
563 const char **lookupname,
566 struct aa_label *new = NULL;
567 struct aa_ns *ns = profile->ns;
568 u32 xtype = xindex & AA_X_TYPE_MASK;
569 const char *stack = NULL;
573 /* fail exec unless ix || ux fallback - handled by caller */
577 /* TODO: fix when perm mapping done at unload */
578 stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
580 /* released by caller */
581 new = x_table_lookup(profile, xindex, lookupname);
585 fallthrough; /* to X_NAME */
587 if (xindex & AA_X_CHILD)
588 /* released by caller */
589 new = find_attach(bprm, ns, &profile->base.profiles,
592 /* released by caller */
593 new = find_attach(bprm, ns, &ns->base.profiles,
600 if (xindex & AA_X_INHERIT) {
601 /* (p|c|n)ix - don't change profile but do
602 * use the newest version
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";
614 /* base the stack on post domain transition */
615 struct aa_label *base = new;
617 new = aa_label_parse(base, stack, GFP_KERNEL, true, false);
623 /* released by caller */
627 static struct aa_label *profile_transition(struct aa_profile *profile,
628 const struct linux_binprm *bprm,
629 char *buffer, struct path_cond *cond,
632 struct aa_label *new = NULL;
633 const char *info = NULL, *name = NULL, *target = NULL;
634 unsigned int state = profile->file.start[AA_CLASS_FILE];
635 struct aa_perms perms = {};
636 bool nonewprivs = false;
643 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
644 &name, &info, profile->disconnected);
646 if (profile_unconfined(profile) ||
647 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
648 AA_DEBUG("name lookup ix on error");
650 new = aa_get_newest_label(&profile->label);
652 name = bprm->filename;
656 if (profile_unconfined(profile)) {
657 new = find_attach(bprm, profile->ns,
658 &profile->ns->base.profiles, name, &info);
660 AA_DEBUG("unconfined attached to new label");
663 AA_DEBUG("unconfined exec no attachment");
664 return aa_get_newest_label(&profile->label);
667 /* find exec permissions for name */
668 state = aa_str_perms(&(profile->file), state, name, cond, &perms);
669 if (perms.allow & MAY_EXEC) {
670 /* exec permission determine how to transition */
671 new = x_to_label(profile, bprm, name, perms.xindex, &target,
673 if (new && new->proxy == profile->label.proxy && info) {
674 /* hack ix fallback - improve how this is detected */
678 info = "profile transition not found";
679 /* remove MAY_EXEC to audit as failure */
680 perms.allow &= ~MAY_EXEC;
682 } else if (COMPLAIN_MODE(profile)) {
683 /* no exec permission - learning mode */
684 struct aa_profile *new_profile = NULL;
686 new_profile = aa_new_null_profile(profile, false, name,
690 info = "could not create null profile";
693 new = &new_profile->label;
695 perms.xindex |= AA_X_UNSAFE;
704 if (!(perms.xindex & AA_X_UNSAFE)) {
706 dbg_printk("apparmor: scrubbing environment variables"
707 " for %s profile=", name);
708 aa_label_printk(new, GFP_KERNEL);
715 aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
716 cond->uid, info, error);
717 if (!new || nonewprivs) {
719 return ERR_PTR(error);
725 static 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,
730 unsigned int state = profile->file.start[AA_CLASS_FILE];
731 struct aa_perms perms = {};
732 const char *xname = NULL, *info = "change_profile onexec";
740 if (profile_unconfined(profile)) {
741 /* change_profile on exec already granted */
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.
750 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
751 &xname, &info, profile->disconnected);
753 if (profile_unconfined(profile) ||
754 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
755 AA_DEBUG("name lookup ix on error");
758 xname = bprm->filename;
762 /* find exec permissions for name */
763 state = aa_str_perms(&(profile->file), state, xname, cond, &perms);
764 if (!(perms.allow & AA_MAY_ONEXEC)) {
765 info = "no change_onexec valid for executable";
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
772 state = aa_dfa_null_transition(profile->file.dfa, state);
773 error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
776 perms.allow &= ~AA_MAY_ONEXEC;
780 if (!(perms.xindex & AA_X_UNSAFE)) {
782 dbg_printk("apparmor: scrubbing environment "
783 "variables for %s label=", xname);
784 aa_label_printk(onexec, GFP_KERNEL);
791 return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
792 NULL, onexec, cond->uid, info, error);
795 /* ensure none ns domain transitions are correctly applied with onexec */
797 static 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,
803 struct aa_profile *profile;
804 struct aa_label *new;
813 error = fn_for_each_in_ns(label, profile,
814 profile_onexec(profile, onexec, stack,
815 bprm, buffer, cond, unsafe));
817 return ERR_PTR(error);
818 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
819 aa_get_newest_label(onexec),
820 profile_transition(profile, bprm, buffer,
824 /* TODO: determine how much we want to loosen this */
825 error = fn_for_each_in_ns(label, profile,
826 profile_onexec(profile, onexec, stack, bprm,
827 buffer, cond, unsafe));
829 return ERR_PTR(error);
830 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
831 aa_label_merge(&profile->label, onexec,
833 profile_transition(profile, bprm, buffer,
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);
850 * apparmor_bprm_creds_for_exec - Update the new creds on the bprm struct
851 * @bprm: binprm for the exec (NOT NULL)
853 * Returns: %0 or error on failure
855 * TODO: once the other paths are done see if we can't refactor into a fn
857 int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm)
859 struct aa_task_ctx *ctx;
860 struct aa_label *label, *new = NULL;
861 struct aa_profile *profile;
863 const char *info = NULL;
866 kuid_t i_uid = i_uid_into_mnt(file_mnt_user_ns(bprm->file),
867 file_inode(bprm->file));
868 struct path_cond cond = {
870 file_inode(bprm->file)->i_mode
873 ctx = task_ctx(current);
874 AA_BUG(!cred_label(bprm->cred));
877 label = aa_get_newest_label(cred_label(bprm->cred));
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.
884 * Testing for unconfined must be done before the subset test
886 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
888 ctx->nnp = aa_get_label(label);
890 /* buffer freed below, name is pointer into buffer */
891 buffer = aa_get_buffer(false);
897 /* Test for onexec first as onexec override other x transitions. */
899 new = handle_onexec(label, ctx->onexec, ctx->token,
900 bprm, buffer, &cond, &unsafe);
902 new = fn_label_build(label, profile, GFP_KERNEL,
903 profile_transition(profile, bprm, buffer,
908 error = PTR_ERR(new);
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.
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.
923 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
924 !unconfined(label) &&
925 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
927 info = "no new privs";
931 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
932 /* FIXME: currently don't mediate shared state */
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);
945 dbg_printk("scrubbing environment variables for %s "
946 "label=", bprm->filename);
947 aa_label_printk(new, GFP_KERNEL);
950 bprm->secureexec = 1;
953 if (label->proxy != new->proxy) {
954 /* when transitioning clear unsafe personality bits */
956 dbg_printk("apparmor: clearing unsafe personality "
957 "bits. %s label=", bprm->filename);
958 aa_label_printk(new, GFP_KERNEL);
961 bprm->per_clear |= PER_CLEAR_ON_SETID;
963 aa_put_label(cred_label(bprm->cred));
964 /* transfer reference, released when cred is freed */
965 set_cred_label(bprm->cred, new);
969 aa_put_buffer(buffer);
974 error = fn_for_each(label, profile,
975 aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
976 bprm->filename, NULL, new,
977 i_uid, info, error));
983 * Functions for self directed profile change
987 /* helper fn for change_hat
989 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
991 static struct aa_label *build_change_hat(struct aa_profile *profile,
992 const char *name, bool sibling)
994 struct aa_profile *root, *hat = NULL;
995 const char *info = NULL;
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);
1003 info = "conflicting target types";
1008 hat = aa_find_child(root, name);
1011 if (COMPLAIN_MODE(profile)) {
1012 hat = aa_new_null_profile(profile, true, name,
1015 info = "failed null profile create";
1020 aa_put_profile(root);
1023 aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
1024 name, hat ? hat->base.hname : NULL,
1025 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, info,
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
1035 /* helper fn for changing into a hat
1037 * Returns: label for hat transition or ERR_PTR. Does not return NULL
1039 static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
1040 int count, int flags)
1042 struct aa_profile *profile, *root, *hat = NULL;
1043 struct aa_label *new;
1045 bool sibling = false;
1046 const char *name, *info = NULL;
1053 if (PROFILE_IS_HAT(labels_profile(label)))
1056 /*find first matching hat */
1057 for (i = 0; i < count && !hat; 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";
1069 hat = aa_find_child(root, name);
1070 aa_put_profile(root);
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";
1078 aa_put_profile(hat);
1081 aa_put_profile(hat);
1083 /* found a hat for all profiles in ns */
1088 /* no hats that match, find appropriate error
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
1095 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1096 if (!list_empty(&profile->base.profiles)) {
1097 info = "hat not found";
1102 info = "no hats defined";
1106 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1108 * no target as it has failed to be found or built
1110 * change_hat uses probing and should not log failures
1111 * related to missing hats
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);
1120 return ERR_PTR(error);
1123 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1124 build_change_hat(profile, name, sibling),
1125 aa_get_label(&profile->label));
1127 info = "label build failed";
1130 } /* else if (IS_ERR) build_change_hat has logged error so return new */
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
1140 * @flags: flags affecting behavior of the change
1142 * Returns %0 on success, error otherwise.
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.
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.
1152 int aa_change_hat(const char *hats[], int count, u64 token, int flags)
1154 const struct cred *cred;
1155 struct aa_task_ctx *ctx = task_ctx(current);
1156 struct aa_label *label, *previous, *new = NULL, *target = NULL;
1157 struct aa_profile *profile;
1158 struct aa_perms perms = {};
1159 const char *info = NULL;
1162 /* released below */
1163 cred = get_current_cred();
1164 label = aa_get_newest_cred_label(cred);
1165 previous = aa_get_newest_label(ctx->previous);
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.
1172 * Testing for unconfined must be done before the subset test
1174 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1175 ctx->nnp = aa_get_label(label);
1177 if (unconfined(label)) {
1178 info = "unconfined can not change_hat";
1184 new = change_hat(label, hats, count, flags);
1187 error = PTR_ERR(new);
1189 /* already audited */
1193 error = may_change_ptraced_domain(new, &info);
1198 * no new privs prevents domain transitions that would
1199 * reduce restrictions.
1201 if (task_no_new_privs(current) && !unconfined(label) &&
1202 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
1203 /* not an apparmor denial per se, so don't log it */
1204 AA_DEBUG("no_new_privs - change_hat denied");
1209 if (flags & AA_CHANGE_TEST)
1213 error = aa_set_current_hat(new, token);
1214 if (error == -EACCES)
1215 /* kill task in case of brute force attacks */
1217 } else if (previous && !(flags & AA_CHANGE_TEST)) {
1219 * no new privs prevents domain transitions that would
1220 * reduce restrictions.
1222 if (task_no_new_privs(current) && !unconfined(label) &&
1223 !aa_label_is_unconfined_subset(previous, ctx->nnp)) {
1224 /* not an apparmor denial per se, so don't log it */
1225 AA_DEBUG("no_new_privs - change_hat denied");
1230 /* Return to saved label. Kill task if restore fails
1231 * to avoid brute force attacks
1234 error = aa_restore_previous_label(token);
1236 if (error == -EACCES)
1240 } /* else ignore @flags && restores when there is no saved profile */
1244 aa_put_label(previous);
1245 aa_put_label(label);
1251 info = "failed token match";
1252 perms.kill = AA_MAY_CHANGEHAT;
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));
1264 static 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)
1269 const char *info = NULL;
1273 error = change_profile_perms(profile, target, stack, request,
1274 profile->file.start[AA_CLASS_FILE],
1277 error = aa_audit_file(profile, perms, op, request, name,
1278 NULL, target, GLOBAL_ROOT_UID, info,
1285 * aa_change_profile - perform a one-way profile transition
1286 * @fqname: name of profile may include namespace (NOT NULL)
1287 * @flags: flags affecting change behavior
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
1292 * If @onexec then the transition is delayed until
1295 * Returns %0 on success, error otherwise.
1297 int aa_change_profile(const char *fqname, int flags)
1299 struct aa_label *label, *new = NULL, *target = NULL;
1300 struct aa_profile *profile;
1301 struct aa_perms perms = {};
1302 const char *info = NULL;
1303 const char *auditname = fqname; /* retain leading & if stack */
1304 bool stack = flags & AA_CHANGE_STACK;
1305 struct aa_task_ctx *ctx = task_ctx(current);
1310 label = aa_get_current_label();
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.
1317 * Testing for unconfined must be done before the subset test
1319 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1320 ctx->nnp = aa_get_label(label);
1322 if (!fqname || !*fqname) {
1323 aa_put_label(label);
1324 AA_DEBUG("no profile name");
1328 if (flags & AA_CHANGE_ONEXEC) {
1329 request = AA_MAY_ONEXEC;
1331 op = OP_STACK_ONEXEC;
1333 op = OP_CHANGE_ONEXEC;
1335 request = AA_MAY_CHANGE_PROFILE;
1339 op = OP_CHANGE_PROFILE;
1342 if (*fqname == '&') {
1344 /* don't have label_parse() do stacking */
1347 target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1348 if (IS_ERR(target)) {
1349 struct aa_profile *tprofile;
1351 info = "label not found";
1352 error = PTR_ERR(target);
1355 * TODO: fixme using labels_profile is not right - do profile
1356 * per complain profile
1358 if ((flags & AA_CHANGE_TEST) ||
1359 !COMPLAIN_MODE(labels_profile(label)))
1361 /* released below */
1362 tprofile = aa_new_null_profile(labels_profile(label), false,
1363 fqname, GFP_KERNEL);
1365 info = "failed null profile create";
1369 target = &tprofile->label;
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
1381 error = fn_for_each_in_ns(label, profile,
1382 change_profile_perms_wrapper(op, auditname,
1383 profile, target, stack,
1386 /* auditing done in change_profile_perms_wrapper */
1392 /* check if tracing task is allowed to trace target domain */
1393 error = may_change_ptraced_domain(target, &info);
1394 if (error && !fn_for_each_in_ns(label, profile,
1395 COMPLAIN_MODE(profile)))
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";
1405 if (flags & AA_CHANGE_TEST)
1408 /* stacking is always a subset, so only check the nonstack case */
1410 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1411 aa_get_label(target),
1412 aa_get_label(&profile->label));
1414 * no new privs prevents domain transitions that would
1415 * reduce restrictions.
1417 if (task_no_new_privs(current) && !unconfined(label) &&
1418 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
1419 /* not an apparmor denial per se, so don't log it */
1420 AA_DEBUG("no_new_privs - change_hat denied");
1426 if (!(flags & AA_CHANGE_ONEXEC)) {
1427 /* only transition profiles in the current ns */
1429 new = aa_label_merge(label, target, GFP_KERNEL);
1430 if (IS_ERR_OR_NULL(new)) {
1431 info = "failed to build target label";
1435 error = PTR_ERR(new);
1440 error = aa_replace_current_label(new);
1447 /* full transition will be built in exec path */
1448 error = aa_set_current_onexec(target, stack);
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));
1459 aa_put_label(target);
1460 aa_put_label(label);