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 * may_change_ptraced_domain - check if can change profile on ptraced task
34 * @cred: cred of task changing domain
35 * @to_label: profile to change to (NOT NULL)
36 * @info: message if there is an error
38 * Check if current is ptraced and if so if the tracing task is allowed
39 * to trace the new domain
41 * Returns: %0 or error if change not allowed
43 static int may_change_ptraced_domain(const struct cred *to_cred,
44 struct aa_label *to_label,
47 struct task_struct *tracer;
48 struct aa_label *tracerl = NULL;
49 const struct cred *tracer_cred = NULL;
54 tracer = ptrace_parent(current);
57 tracerl = aa_get_task_label(tracer);
58 tracer_cred = get_task_cred(tracer);
61 if (!tracer || unconfined(tracerl))
64 error = aa_may_ptrace(tracer_cred, tracerl, to_cred, to_label,
69 aa_put_label(tracerl);
70 put_cred(tracer_cred);
73 *info = "ptrace prevents transition";
77 /**** TODO: dedup to aa_label_match - needs perm and dfa, merging
78 * specifically this is an exact copy of aa_label_match except
79 * aa_compute_perms is replaced with aa_compute_fperms
80 * and policy.dfa with file.dfa
82 /* match a profile and its associated ns component if needed
83 * Assumes visibility test has already been done.
84 * If a subns profile is not to be matched should be prescreened with
87 static inline aa_state_t match_component(struct aa_profile *profile,
88 struct aa_profile *tp,
89 bool stack, aa_state_t state)
91 struct aa_ruleset *rules = list_first_entry(&profile->rules,
92 typeof(*rules), list);
96 state = aa_dfa_match(rules->file.dfa, state, "&");
97 if (profile->ns == tp->ns)
98 return aa_dfa_match(rules->file.dfa, state, tp->base.hname);
100 /* try matching with namespace name and then profile */
101 ns_name = aa_ns_name(profile->ns, tp->ns, true);
102 state = aa_dfa_match_len(rules->file.dfa, state, ":", 1);
103 state = aa_dfa_match(rules->file.dfa, state, ns_name);
104 state = aa_dfa_match_len(rules->file.dfa, state, ":", 1);
105 return aa_dfa_match(rules->file.dfa, state, tp->base.hname);
109 * label_compound_match - find perms for full compound label
110 * @profile: profile to find perms for
111 * @label: label to check access permissions for
112 * @stack: whether this is a stacking request
113 * @state: state to start match in
114 * @subns: whether to do permission checks on components in a subns
115 * @request: permissions to request
116 * @perms: perms struct to set
118 * Returns: 0 on success else ERROR
120 * For the label A//&B//&C this does the perm match for A//&B//&C
121 * @perms should be preinitialized with allperms OR a previous permission
122 * check to be stacked.
124 static int label_compound_match(struct aa_profile *profile,
125 struct aa_label *label, bool stack,
126 aa_state_t state, bool subns, u32 request,
127 struct aa_perms *perms)
129 struct aa_ruleset *rules = list_first_entry(&profile->rules,
130 typeof(*rules), list);
131 struct aa_profile *tp;
133 struct path_cond cond = { };
135 /* find first subcomponent that is visible */
136 label_for_each(i, label, tp) {
137 if (!aa_ns_visible(profile->ns, tp->ns, subns))
139 state = match_component(profile, tp, stack, state);
145 /* no component visible */
150 label_for_each_cont(i, label, tp) {
151 if (!aa_ns_visible(profile->ns, tp->ns, subns))
153 state = aa_dfa_match(rules->file.dfa, state, "//&");
154 state = match_component(profile, tp, false, state);
158 *perms = *(aa_lookup_fperms(&(rules->file), state, &cond));
159 aa_apply_modes_to_perms(profile, perms);
160 if ((perms->allow & request) != request)
171 * label_components_match - find perms for all subcomponents of a label
172 * @profile: profile to find perms for
173 * @label: label to check access permissions for
174 * @stack: whether this is a stacking request
175 * @start: state to start match in
176 * @subns: whether to do permission checks on components in a subns
177 * @request: permissions to request
178 * @perms: an initialized perms struct to add accumulation to
180 * Returns: 0 on success else ERROR
182 * For the label A//&B//&C this does the perm match for each of A and B and C
183 * @perms should be preinitialized with allperms OR a previous permission
184 * check to be stacked.
186 static int label_components_match(struct aa_profile *profile,
187 struct aa_label *label, bool stack,
188 aa_state_t start, bool subns, u32 request,
189 struct aa_perms *perms)
191 struct aa_ruleset *rules = list_first_entry(&profile->rules,
192 typeof(*rules), list);
193 struct aa_profile *tp;
196 struct path_cond cond = { };
197 aa_state_t state = 0;
199 /* find first subcomponent to test */
200 label_for_each(i, label, tp) {
201 if (!aa_ns_visible(profile->ns, tp->ns, subns))
203 state = match_component(profile, tp, stack, start);
209 /* no subcomponents visible - no change in perms */
213 tmp = *(aa_lookup_fperms(&(rules->file), state, &cond));
214 aa_apply_modes_to_perms(profile, &tmp);
215 aa_perms_accum(perms, &tmp);
216 label_for_each_cont(i, label, tp) {
217 if (!aa_ns_visible(profile->ns, tp->ns, subns))
219 state = match_component(profile, tp, stack, start);
222 tmp = *(aa_lookup_fperms(&(rules->file), state, &cond));
223 aa_apply_modes_to_perms(profile, &tmp);
224 aa_perms_accum(perms, &tmp);
227 if ((perms->allow & request) != request)
238 * label_match - do a multi-component label match
239 * @profile: profile to match against (NOT NULL)
240 * @label: label to match (NOT NULL)
241 * @stack: whether this is a stacking request
242 * @state: state to start in
243 * @subns: whether to match subns components
244 * @request: permission request
245 * @perms: Returns computed perms (NOT NULL)
247 * Returns: the state the match finished in, may be the none matching state
249 static int label_match(struct aa_profile *profile, struct aa_label *label,
250 bool stack, aa_state_t state, bool subns, u32 request,
251 struct aa_perms *perms)
256 error = label_compound_match(profile, label, stack, state, subns,
262 return label_components_match(profile, label, stack, state, subns,
266 /******* end TODO: dedup *****/
269 * change_profile_perms - find permissions for change_profile
270 * @profile: the current profile (NOT NULL)
271 * @target: label to transition to (NOT NULL)
272 * @stack: whether this is a stacking request
273 * @request: requested perms
274 * @start: state to start matching in
277 * Returns: permission set
279 * currently only matches full label A//&B//&C or individual components A, B, C
280 * not arbitrary combinations. Eg. A//&B, C
282 static int change_profile_perms(struct aa_profile *profile,
283 struct aa_label *target, bool stack,
284 u32 request, aa_state_t start,
285 struct aa_perms *perms)
287 if (profile_unconfined(profile)) {
288 perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
289 perms->audit = perms->quiet = perms->kill = 0;
293 /* TODO: add profile in ns screening */
294 return label_match(profile, target, stack, start, true, request, perms);
298 * aa_xattrs_match - check whether a file matches the xattrs defined in profile
299 * @bprm: binprm struct for the process to validate
300 * @profile: profile to match against (NOT NULL)
301 * @state: state to start match in
303 * Returns: number of extended attributes that matched, or < 0 on error
305 static int aa_xattrs_match(const struct linux_binprm *bprm,
306 struct aa_profile *profile, aa_state_t state)
311 struct aa_attachment *attach = &profile->attach;
312 int size, value_size = 0, ret = attach->xattr_count;
314 if (!bprm || !attach->xattr_count)
318 /* transition from exec match to xattr set */
319 state = aa_dfa_outofband_transition(attach->xmatch.dfa, state);
320 d = bprm->file->f_path.dentry;
322 for (i = 0; i < attach->xattr_count; i++) {
323 size = vfs_getxattr_alloc(&nop_mnt_idmap, d, attach->xattrs[i],
324 &value, value_size, GFP_KERNEL);
329 * Check the xattr presence before value. This ensure
330 * that not present xattr can be distinguished from a 0
331 * length value or rule that matches any value
333 state = aa_dfa_null_transition(attach->xmatch.dfa,
335 /* Check xattr value */
336 state = aa_dfa_match_len(attach->xmatch.dfa, state,
338 index = ACCEPT_TABLE(attach->xmatch.dfa)[state];
339 perm = attach->xmatch.perms[index].allow;
340 if (!(perm & MAY_EXEC)) {
345 /* transition to next element */
346 state = aa_dfa_outofband_transition(attach->xmatch.dfa, state);
349 * No xattr match, so verify if transition to
350 * next element was valid. IFF so the xattr
357 /* don't count missing optional xattr as matched */
368 * find_attach - do attachment search for unconfined processes
369 * @bprm - binprm structure of transitioning task
370 * @ns: the current namespace (NOT NULL)
371 * @head - profile list to walk (NOT NULL)
372 * @name - to match against (NOT NULL)
373 * @info - info message if there was an error (NOT NULL)
375 * Do a linear search on the profiles in the list. There is a matching
376 * preference where an exact match is preferred over a name which uses
377 * expressions to match, and matching expressions with the greatest
378 * xmatch_len are preferred.
380 * Requires: @head not be shared or have appropriate locks held
382 * Returns: label or NULL if no match found
384 static struct aa_label *find_attach(const struct linux_binprm *bprm,
385 struct aa_ns *ns, struct list_head *head,
386 const char *name, const char **info)
388 int candidate_len = 0, candidate_xattrs = 0;
389 bool conflict = false;
390 struct aa_profile *profile, *candidate = NULL;
397 list_for_each_entry_rcu(profile, head, base.list) {
398 struct aa_attachment *attach = &profile->attach;
400 if (profile->label.flags & FLAG_NULL &&
401 &profile->label == ns_unconfined(profile->ns))
404 /* Find the "best" matching profile. Profiles must
405 * match the path and extended attributes (if any)
406 * associated with the file. A more specific path
407 * match will be preferred over a less specific one,
408 * and a match with more matching extended attributes
409 * will be preferred over one with fewer. If the best
410 * match has both the same level of path specificity
411 * and the same number of matching extended attributes
412 * as another profile, signal a conflict and refuse to
415 if (attach->xmatch.dfa) {
420 state = aa_dfa_leftmatch(attach->xmatch.dfa,
421 attach->xmatch.start[AA_CLASS_XMATCH],
423 index = ACCEPT_TABLE(attach->xmatch.dfa)[state];
424 perm = attach->xmatch.perms[index].allow;
425 /* any accepting state means a valid match. */
426 if (perm & MAY_EXEC) {
429 if (count < candidate_len)
432 if (bprm && attach->xattr_count) {
433 long rev = READ_ONCE(ns->revision);
435 if (!aa_get_profile_not0(profile))
438 ret = aa_xattrs_match(bprm, profile,
441 aa_put_profile(profile);
443 READ_ONCE(ns->revision))
447 * Fail matching if the xattrs don't
454 * TODO: allow for more flexible best match
456 * The new match isn't more specific
457 * than the current best match
459 if (count == candidate_len &&
460 ret <= candidate_xattrs) {
461 /* Match is equivalent, so conflict */
462 if (ret == candidate_xattrs)
467 /* Either the same length with more matching
468 * xattrs, or a longer match
471 candidate_len = max(count, attach->xmatch_len);
472 candidate_xattrs = ret;
475 } else if (!strcmp(profile->base.name, name)) {
477 * old exact non-re match, without conditionals such
478 * as xattrs. no more searching required
485 if (!candidate || conflict) {
487 *info = "conflicting profile attachments";
493 candidate = aa_get_newest_profile(candidate);
496 return &candidate->label;
499 static const char *next_name(int xtype, const char *name)
505 * x_table_lookup - lookup an x transition name via transition table
506 * @profile: current profile (NOT NULL)
507 * @xindex: index into x transition table
508 * @name: returns: name tested to find label (NOT NULL)
510 * Returns: refcounted label, or NULL on failure (MAYBE NULL)
512 struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
515 struct aa_ruleset *rules = list_first_entry(&profile->rules,
516 typeof(*rules), list);
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 = rules->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_ruleset *rules = list_first_entry(&profile->rules,
567 typeof(*rules), list);
568 struct aa_label *new = NULL;
569 struct aa_ns *ns = profile->ns;
570 u32 xtype = xindex & AA_X_TYPE_MASK;
571 const char *stack = NULL;
575 /* fail exec unless ix || ux fallback - handled by caller */
579 /* TODO: fix when perm mapping done at unload */
580 stack = rules->file.trans.table[xindex & AA_X_INDEX_MASK];
582 /* released by caller */
583 new = x_table_lookup(profile, xindex, lookupname);
587 fallthrough; /* to X_NAME */
589 if (xindex & AA_X_CHILD)
590 /* released by caller */
591 new = find_attach(bprm, ns, &profile->base.profiles,
594 /* released by caller */
595 new = find_attach(bprm, ns, &ns->base.profiles,
602 if (xindex & AA_X_INHERIT) {
603 /* (p|c|n)ix - don't change profile but do
604 * use the newest version
606 *info = "ix fallback";
607 /* no profile && no error */
608 new = aa_get_newest_label(&profile->label);
609 } else if (xindex & AA_X_UNCONFINED) {
610 new = aa_get_newest_label(ns_unconfined(profile->ns));
611 *info = "ux fallback";
616 /* base the stack on post domain transition */
617 struct aa_label *base = new;
619 new = aa_label_parse(base, stack, GFP_KERNEL, true, false);
625 /* released by caller */
629 static struct aa_label *profile_transition(const struct cred *subj_cred,
630 struct aa_profile *profile,
631 const struct linux_binprm *bprm,
632 char *buffer, struct path_cond *cond,
635 struct aa_ruleset *rules = list_first_entry(&profile->rules,
636 typeof(*rules), list);
637 struct aa_label *new = NULL;
638 const char *info = NULL, *name = NULL, *target = NULL;
639 aa_state_t state = rules->file.start[AA_CLASS_FILE];
640 struct aa_perms perms = {};
641 bool nonewprivs = false;
648 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
649 &name, &info, profile->disconnected);
651 if (profile_unconfined(profile) ||
652 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
653 AA_DEBUG("name lookup ix on error");
655 new = aa_get_newest_label(&profile->label);
657 name = bprm->filename;
661 if (profile_unconfined(profile)) {
662 new = find_attach(bprm, profile->ns,
663 &profile->ns->base.profiles, name, &info);
665 AA_DEBUG("unconfined attached to new label");
668 AA_DEBUG("unconfined exec no attachment");
669 return aa_get_newest_label(&profile->label);
672 /* find exec permissions for name */
673 state = aa_str_perms(&(rules->file), state, name, cond, &perms);
674 if (perms.allow & MAY_EXEC) {
675 /* exec permission determine how to transition */
676 new = x_to_label(profile, bprm, name, perms.xindex, &target,
678 if (new && new->proxy == profile->label.proxy && info) {
679 /* hack ix fallback - improve how this is detected */
683 info = "profile transition not found";
684 /* remove MAY_EXEC to audit as failure */
685 perms.allow &= ~MAY_EXEC;
687 } else if (COMPLAIN_MODE(profile)) {
688 /* no exec permission - learning mode */
689 struct aa_profile *new_profile = NULL;
691 new_profile = aa_new_learning_profile(profile, false, name,
695 info = "could not create null profile";
698 new = &new_profile->label;
700 perms.xindex |= AA_X_UNSAFE;
709 if (!(perms.xindex & AA_X_UNSAFE)) {
711 dbg_printk("apparmor: scrubbing environment variables"
712 " for %s profile=", name);
713 aa_label_printk(new, GFP_KERNEL);
720 aa_audit_file(subj_cred, profile, &perms, OP_EXEC, MAY_EXEC, name,
722 cond->uid, info, error);
723 if (!new || nonewprivs) {
725 return ERR_PTR(error);
731 static int profile_onexec(const struct cred *subj_cred,
732 struct aa_profile *profile, struct aa_label *onexec,
733 bool stack, const struct linux_binprm *bprm,
734 char *buffer, struct path_cond *cond,
737 struct aa_ruleset *rules = list_first_entry(&profile->rules,
738 typeof(*rules), list);
739 aa_state_t state = rules->file.start[AA_CLASS_FILE];
740 struct aa_perms perms = {};
741 const char *xname = NULL, *info = "change_profile onexec";
749 if (profile_unconfined(profile)) {
750 /* change_profile on exec already granted */
752 * NOTE: Domain transitions from unconfined are allowed
753 * even when no_new_privs is set because this aways results
754 * in a further reduction of permissions.
759 error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
760 &xname, &info, profile->disconnected);
762 if (profile_unconfined(profile) ||
763 (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
764 AA_DEBUG("name lookup ix on error");
767 xname = bprm->filename;
771 /* find exec permissions for name */
772 state = aa_str_perms(&(rules->file), state, xname, cond, &perms);
773 if (!(perms.allow & AA_MAY_ONEXEC)) {
774 info = "no change_onexec valid for executable";
777 /* test if this exec can be paired with change_profile onexec.
778 * onexec permission is linked to exec with a standard pairing
779 * exec\0change_profile
781 state = aa_dfa_null_transition(rules->file.dfa, state);
782 error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
785 perms.allow &= ~AA_MAY_ONEXEC;
789 if (!(perms.xindex & AA_X_UNSAFE)) {
791 dbg_printk("apparmor: scrubbing environment "
792 "variables for %s label=", xname);
793 aa_label_printk(onexec, GFP_KERNEL);
800 return aa_audit_file(subj_cred, profile, &perms, OP_EXEC,
801 AA_MAY_ONEXEC, xname,
802 NULL, onexec, cond->uid, info, error);
805 /* ensure none ns domain transitions are correctly applied with onexec */
807 static struct aa_label *handle_onexec(const struct cred *subj_cred,
808 struct aa_label *label,
809 struct aa_label *onexec, bool stack,
810 const struct linux_binprm *bprm,
811 char *buffer, struct path_cond *cond,
814 struct aa_profile *profile;
815 struct aa_label *new;
824 error = fn_for_each_in_ns(label, profile,
825 profile_onexec(subj_cred, profile, onexec, stack,
826 bprm, buffer, cond, unsafe));
828 return ERR_PTR(error);
829 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
830 aa_get_newest_label(onexec),
831 profile_transition(subj_cred, profile, bprm,
836 /* TODO: determine how much we want to loosen this */
837 error = fn_for_each_in_ns(label, profile,
838 profile_onexec(subj_cred, profile, onexec, stack, bprm,
839 buffer, cond, unsafe));
841 return ERR_PTR(error);
842 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
843 aa_label_merge(&profile->label, onexec,
845 profile_transition(subj_cred, profile, bprm,
853 /* TODO: get rid of GLOBAL_ROOT_UID */
854 error = fn_for_each_in_ns(label, profile,
855 aa_audit_file(subj_cred, profile, &nullperms,
857 AA_MAY_ONEXEC, bprm->filename, NULL,
858 onexec, GLOBAL_ROOT_UID,
859 "failed to build target label", -ENOMEM));
860 return ERR_PTR(error);
864 * apparmor_bprm_creds_for_exec - Update the new creds on the bprm struct
865 * @bprm: binprm for the exec (NOT NULL)
867 * Returns: %0 or error on failure
869 * TODO: once the other paths are done see if we can't refactor into a fn
871 int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm)
873 struct aa_task_ctx *ctx;
874 struct aa_label *label, *new = NULL;
875 const struct cred *subj_cred;
876 struct aa_profile *profile;
878 const char *info = NULL;
881 vfsuid_t vfsuid = i_uid_into_vfsuid(file_mnt_idmap(bprm->file),
882 file_inode(bprm->file));
883 struct path_cond cond = {
884 vfsuid_into_kuid(vfsuid),
885 file_inode(bprm->file)->i_mode
888 subj_cred = current_cred();
889 ctx = task_ctx(current);
890 AA_BUG(!cred_label(bprm->cred));
893 label = aa_get_newest_label(cred_label(bprm->cred));
896 * Detect no new privs being set, and store the label it
897 * occurred under. Ideally this would happen when nnp
898 * is set but there isn't a good way to do that yet.
900 * Testing for unconfined must be done before the subset test
902 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
904 ctx->nnp = aa_get_label(label);
906 /* buffer freed below, name is pointer into buffer */
907 buffer = aa_get_buffer(false);
913 /* Test for onexec first as onexec override other x transitions. */
915 new = handle_onexec(subj_cred, label, ctx->onexec, ctx->token,
916 bprm, buffer, &cond, &unsafe);
918 new = fn_label_build(label, profile, GFP_KERNEL,
919 profile_transition(subj_cred, profile, bprm,
925 error = PTR_ERR(new);
932 /* Policy has specified a domain transitions. If no_new_privs and
933 * confined ensure the transition is to confinement that is subset
934 * of the confinement when the task entered no new privs.
936 * NOTE: Domain transitions from unconfined and to stacked
937 * subsets are allowed even when no_new_privs is set because this
938 * aways results in a further reduction of permissions.
940 if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
941 !unconfined(label) &&
942 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
944 info = "no new privs";
948 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
949 /* FIXME: currently don't mediate shared state */
953 if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
954 /* TODO: test needs to be profile of label to new */
955 error = may_change_ptraced_domain(bprm->cred, new, &info);
962 dbg_printk("scrubbing environment variables for %s "
963 "label=", bprm->filename);
964 aa_label_printk(new, GFP_KERNEL);
967 bprm->secureexec = 1;
970 if (label->proxy != new->proxy) {
971 /* when transitioning clear unsafe personality bits */
973 dbg_printk("apparmor: clearing unsafe personality "
974 "bits. %s label=", bprm->filename);
975 aa_label_printk(new, GFP_KERNEL);
978 bprm->per_clear |= PER_CLEAR_ON_SETID;
980 aa_put_label(cred_label(bprm->cred));
981 /* transfer reference, released when cred is freed */
982 set_cred_label(bprm->cred, new);
986 aa_put_buffer(buffer);
991 error = fn_for_each(label, profile,
992 aa_audit_file(current_cred(), profile, &nullperms,
994 bprm->filename, NULL, new,
995 vfsuid_into_kuid(vfsuid), info, error));
1001 * Functions for self directed profile change
1005 /* helper fn for change_hat
1007 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
1009 static struct aa_label *build_change_hat(const struct cred *subj_cred,
1010 struct aa_profile *profile,
1011 const char *name, bool sibling)
1013 struct aa_profile *root, *hat = NULL;
1014 const char *info = NULL;
1017 if (sibling && PROFILE_IS_HAT(profile)) {
1018 root = aa_get_profile_rcu(&profile->parent);
1019 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
1020 root = aa_get_profile(profile);
1022 info = "conflicting target types";
1027 hat = aa_find_child(root, name);
1030 if (COMPLAIN_MODE(profile)) {
1031 hat = aa_new_learning_profile(profile, true, name,
1034 info = "failed null profile create";
1039 aa_put_profile(root);
1042 aa_audit_file(subj_cred, profile, &nullperms, OP_CHANGE_HAT,
1044 name, hat ? hat->base.hname : NULL,
1045 hat ? &hat->label : NULL, GLOBAL_ROOT_UID, info,
1047 if (!hat || (error && error != -ENOENT))
1048 return ERR_PTR(error);
1049 /* if hat && error - complain mode, already audited and we adjust for
1050 * complain mode allow by returning hat->label
1055 /* helper fn for changing into a hat
1057 * Returns: label for hat transition or ERR_PTR. Does not return NULL
1059 static struct aa_label *change_hat(const struct cred *subj_cred,
1060 struct aa_label *label, const char *hats[],
1061 int count, int flags)
1063 struct aa_profile *profile, *root, *hat = NULL;
1064 struct aa_label *new;
1066 bool sibling = false;
1067 const char *name, *info = NULL;
1074 if (PROFILE_IS_HAT(labels_profile(label)))
1077 /*find first matching hat */
1078 for (i = 0; i < count && !hat; i++) {
1080 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1081 if (sibling && PROFILE_IS_HAT(profile)) {
1082 root = aa_get_profile_rcu(&profile->parent);
1083 } else if (!sibling && !PROFILE_IS_HAT(profile)) {
1084 root = aa_get_profile(profile);
1085 } else { /* conflicting change type */
1086 info = "conflicting targets types";
1090 hat = aa_find_child(root, name);
1091 aa_put_profile(root);
1093 if (!COMPLAIN_MODE(profile))
1094 goto outer_continue;
1095 /* complain mode succeed as if hat */
1096 } else if (!PROFILE_IS_HAT(hat)) {
1097 info = "target not hat";
1099 aa_put_profile(hat);
1102 aa_put_profile(hat);
1104 /* found a hat for all profiles in ns */
1109 /* no hats that match, find appropriate error
1111 * In complain mode audit of the failure is based off of the first
1112 * hat supplied. This is done due how userspace interacts with
1116 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1117 if (!list_empty(&profile->base.profiles)) {
1118 info = "hat not found";
1123 info = "no hats defined";
1127 label_for_each_in_ns(it, labels_ns(label), label, profile) {
1129 * no target as it has failed to be found or built
1131 * change_hat uses probing and should not log failures
1132 * related to missing hats
1134 /* TODO: get rid of GLOBAL_ROOT_UID */
1135 if (count > 1 || COMPLAIN_MODE(profile)) {
1136 aa_audit_file(subj_cred, profile, &nullperms,
1138 AA_MAY_CHANGEHAT, name, NULL, NULL,
1139 GLOBAL_ROOT_UID, info, error);
1142 return ERR_PTR(error);
1145 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1146 build_change_hat(subj_cred, profile, name,
1148 aa_get_label(&profile->label));
1150 info = "label build failed";
1153 } /* else if (IS_ERR) build_change_hat has logged error so return new */
1159 * aa_change_hat - change hat to/from subprofile
1160 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1161 * @count: number of hat names in @hats
1162 * @token: magic value to validate the hat change
1163 * @flags: flags affecting behavior of the change
1165 * Returns %0 on success, error otherwise.
1167 * Change to the first profile specified in @hats that exists, and store
1168 * the @hat_magic in the current task context. If the count == 0 and the
1169 * @token matches that stored in the current task context, return to the
1170 * top level profile.
1172 * change_hat only applies to profiles in the current ns, and each profile
1173 * in the ns must make the same transition otherwise change_hat will fail.
1175 int aa_change_hat(const char *hats[], int count, u64 token, int flags)
1177 const struct cred *subj_cred;
1178 struct aa_task_ctx *ctx = task_ctx(current);
1179 struct aa_label *label, *previous, *new = NULL, *target = NULL;
1180 struct aa_profile *profile;
1181 struct aa_perms perms = {};
1182 const char *info = NULL;
1185 /* released below */
1186 subj_cred = get_current_cred();
1187 label = aa_get_newest_cred_label(subj_cred);
1188 previous = aa_get_newest_label(ctx->previous);
1191 * Detect no new privs being set, and store the label it
1192 * occurred under. Ideally this would happen when nnp
1193 * is set but there isn't a good way to do that yet.
1195 * Testing for unconfined must be done before the subset test
1197 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1198 ctx->nnp = aa_get_label(label);
1200 if (unconfined(label)) {
1201 info = "unconfined can not change_hat";
1207 new = change_hat(subj_cred, label, hats, count, flags);
1210 error = PTR_ERR(new);
1212 /* already audited */
1216 /* target cred is the same as current except new label */
1217 error = may_change_ptraced_domain(subj_cred, new, &info);
1222 * no new privs prevents domain transitions that would
1223 * reduce restrictions.
1225 if (task_no_new_privs(current) && !unconfined(label) &&
1226 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
1227 /* not an apparmor denial per se, so don't log it */
1228 AA_DEBUG("no_new_privs - change_hat denied");
1233 if (flags & AA_CHANGE_TEST)
1237 error = aa_set_current_hat(new, token);
1238 if (error == -EACCES)
1239 /* kill task in case of brute force attacks */
1241 } else if (previous && !(flags & AA_CHANGE_TEST)) {
1243 * no new privs prevents domain transitions that would
1244 * reduce restrictions.
1246 if (task_no_new_privs(current) && !unconfined(label) &&
1247 !aa_label_is_unconfined_subset(previous, ctx->nnp)) {
1248 /* not an apparmor denial per se, so don't log it */
1249 AA_DEBUG("no_new_privs - change_hat denied");
1254 /* Return to saved label. Kill task if restore fails
1255 * to avoid brute force attacks
1258 error = aa_restore_previous_label(token);
1260 if (error == -EACCES)
1264 } /* else ignore @flags && restores when there is no saved profile */
1268 aa_put_label(previous);
1269 aa_put_label(label);
1270 put_cred(subj_cred);
1275 info = "failed token match";
1276 perms.kill = AA_MAY_CHANGEHAT;
1279 fn_for_each_in_ns(label, profile,
1280 aa_audit_file(subj_cred, profile, &perms, OP_CHANGE_HAT,
1281 AA_MAY_CHANGEHAT, NULL, NULL, target,
1282 GLOBAL_ROOT_UID, info, error));
1288 static int change_profile_perms_wrapper(const char *op, const char *name,
1289 const struct cred *subj_cred,
1290 struct aa_profile *profile,
1291 struct aa_label *target, bool stack,
1292 u32 request, struct aa_perms *perms)
1294 struct aa_ruleset *rules = list_first_entry(&profile->rules,
1295 typeof(*rules), list);
1296 const char *info = NULL;
1300 error = change_profile_perms(profile, target, stack, request,
1301 rules->file.start[AA_CLASS_FILE],
1304 error = aa_audit_file(subj_cred, profile, perms, op, request,
1306 NULL, target, GLOBAL_ROOT_UID, info,
1313 * aa_change_profile - perform a one-way profile transition
1314 * @fqname: name of profile may include namespace (NOT NULL)
1315 * @flags: flags affecting change behavior
1317 * Change to new profile @name. Unlike with hats, there is no way
1318 * to change back. If @name isn't specified the current profile name is
1320 * If @onexec then the transition is delayed until
1323 * Returns %0 on success, error otherwise.
1325 int aa_change_profile(const char *fqname, int flags)
1327 struct aa_label *label, *new = NULL, *target = NULL;
1328 struct aa_profile *profile;
1329 struct aa_perms perms = {};
1330 const char *info = NULL;
1331 const char *auditname = fqname; /* retain leading & if stack */
1332 bool stack = flags & AA_CHANGE_STACK;
1333 struct aa_task_ctx *ctx = task_ctx(current);
1334 const struct cred *subj_cred = get_current_cred();
1339 label = aa_get_current_label();
1342 * Detect no new privs being set, and store the label it
1343 * occurred under. Ideally this would happen when nnp
1344 * is set but there isn't a good way to do that yet.
1346 * Testing for unconfined must be done before the subset test
1348 if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1349 ctx->nnp = aa_get_label(label);
1351 if (!fqname || !*fqname) {
1352 aa_put_label(label);
1353 AA_DEBUG("no profile name");
1357 if (flags & AA_CHANGE_ONEXEC) {
1358 request = AA_MAY_ONEXEC;
1360 op = OP_STACK_ONEXEC;
1362 op = OP_CHANGE_ONEXEC;
1364 request = AA_MAY_CHANGE_PROFILE;
1368 op = OP_CHANGE_PROFILE;
1371 if (*fqname == '&') {
1373 /* don't have label_parse() do stacking */
1376 target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1377 if (IS_ERR(target)) {
1378 struct aa_profile *tprofile;
1380 info = "label not found";
1381 error = PTR_ERR(target);
1384 * TODO: fixme using labels_profile is not right - do profile
1385 * per complain profile
1387 if ((flags & AA_CHANGE_TEST) ||
1388 !COMPLAIN_MODE(labels_profile(label)))
1390 /* released below */
1391 tprofile = aa_new_learning_profile(labels_profile(label), false,
1392 fqname, GFP_KERNEL);
1394 info = "failed null profile create";
1398 target = &tprofile->label;
1403 * self directed transitions only apply to current policy ns
1404 * TODO: currently requiring perms for stacking and straight change
1405 * stacking doesn't strictly need this. Determine how much
1406 * we want to loosen this restriction for stacking
1410 error = fn_for_each_in_ns(label, profile,
1411 change_profile_perms_wrapper(op, auditname,
1413 profile, target, stack,
1416 /* auditing done in change_profile_perms_wrapper */
1422 /* check if tracing task is allowed to trace target domain */
1423 error = may_change_ptraced_domain(subj_cred, target, &info);
1424 if (error && !fn_for_each_in_ns(label, profile,
1425 COMPLAIN_MODE(profile)))
1428 /* TODO: add permission check to allow this
1429 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
1430 * info = "not a single threaded task";
1435 if (flags & AA_CHANGE_TEST)
1438 /* stacking is always a subset, so only check the nonstack case */
1440 new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1441 aa_get_label(target),
1442 aa_get_label(&profile->label));
1444 * no new privs prevents domain transitions that would
1445 * reduce restrictions.
1447 if (task_no_new_privs(current) && !unconfined(label) &&
1448 !aa_label_is_unconfined_subset(new, ctx->nnp)) {
1449 /* not an apparmor denial per se, so don't log it */
1450 AA_DEBUG("no_new_privs - change_hat denied");
1456 if (!(flags & AA_CHANGE_ONEXEC)) {
1457 /* only transition profiles in the current ns */
1459 new = aa_label_merge(label, target, GFP_KERNEL);
1460 if (IS_ERR_OR_NULL(new)) {
1461 info = "failed to build target label";
1465 error = PTR_ERR(new);
1470 error = aa_replace_current_label(new);
1477 /* full transition will be built in exec path */
1478 error = aa_set_current_onexec(target, stack);
1482 error = fn_for_each_in_ns(label, profile,
1483 aa_audit_file(subj_cred,
1484 profile, &perms, op, request, auditname,
1485 NULL, new ? new : target,
1486 GLOBAL_ROOT_UID, info, error));
1490 aa_put_label(target);
1491 aa_put_label(label);
1492 put_cred(subj_cred);