2 * AppArmor security module
4 * This file contains AppArmor policy attachment and domain transitions
6 * Copyright (C) 2002-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
15 #include <linux/errno.h>
16 #include <linux/fdtable.h>
17 #include <linux/file.h>
18 #include <linux/mount.h>
19 #include <linux/syscalls.h>
20 #include <linux/tracehook.h>
21 #include <linux/personality.h>
23 #include "include/audit.h"
24 #include "include/apparmorfs.h"
25 #include "include/context.h"
26 #include "include/domain.h"
27 #include "include/file.h"
28 #include "include/ipc.h"
29 #include "include/match.h"
30 #include "include/path.h"
31 #include "include/policy.h"
32 #include "include/policy_ns.h"
35 * aa_free_domain_entries - free entries in a domain table
36 * @domain: the domain table to free (MAYBE NULL)
38 void aa_free_domain_entries(struct aa_domain
*domain
)
45 for (i
= 0; i
< domain
->size
; i
++)
46 kzfree(domain
->table
[i
]);
47 kzfree(domain
->table
);
53 * may_change_ptraced_domain - check if can change profile on ptraced task
54 * @to_label: profile to change to (NOT NULL)
55 * @info: message if there is an error
57 * Check if current is ptraced and if so if the tracing task is allowed
58 * to trace the new domain
60 * Returns: %0 or error if change not allowed
62 static int may_change_ptraced_domain(struct aa_label
*to_label
,
65 struct task_struct
*tracer
;
66 struct aa_label
*tracerl
= NULL
;
70 tracer
= ptrace_parent(current
);
73 tracerl
= aa_get_task_label(tracer
);
76 if (!tracer
|| unconfined(tracerl
))
79 error
= aa_may_ptrace(tracerl
, to_label
, PTRACE_MODE_ATTACH
);
83 aa_put_label(tracerl
);
86 *info
= "ptrace prevents transition";
90 /**** TODO: dedup to aa_label_match - needs perm and dfa, merging
91 * specifically this is an exact copy of aa_label_match except
92 * aa_compute_perms is replaced with aa_compute_fperms
93 * and policy.dfa with file.dfa
95 /* match a profile and its associated ns component if needed
96 * Assumes visibility test has already been done.
97 * If a subns profile is not to be matched should be prescreened with
100 static inline unsigned int match_component(struct aa_profile
*profile
,
101 struct aa_profile
*tp
,
102 bool stack
, unsigned int state
)
107 state
= aa_dfa_match(profile
->file
.dfa
, state
, "&");
108 if (profile
->ns
== tp
->ns
)
109 return aa_dfa_match(profile
->file
.dfa
, state
, tp
->base
.hname
);
111 /* try matching with namespace name and then profile */
112 ns_name
= aa_ns_name(profile
->ns
, tp
->ns
, true);
113 state
= aa_dfa_match_len(profile
->file
.dfa
, state
, ":", 1);
114 state
= aa_dfa_match(profile
->file
.dfa
, state
, ns_name
);
115 state
= aa_dfa_match_len(profile
->file
.dfa
, state
, ":", 1);
116 return aa_dfa_match(profile
->file
.dfa
, state
, tp
->base
.hname
);
120 * label_compound_match - find perms for full compound label
121 * @profile: profile to find perms for
122 * @label: label to check access permissions for
123 * @stack: whether this is a stacking request
124 * @start: state to start match in
125 * @subns: whether to do permission checks on components in a subns
126 * @request: permissions to request
127 * @perms: perms struct to set
129 * Returns: 0 on success else ERROR
131 * For the label A//&B//&C this does the perm match for A//&B//&C
132 * @perms should be preinitialized with allperms OR a previous permission
133 * check to be stacked.
135 static int label_compound_match(struct aa_profile
*profile
,
136 struct aa_label
*label
, bool stack
,
137 unsigned int state
, bool subns
, u32 request
,
138 struct aa_perms
*perms
)
140 struct aa_profile
*tp
;
142 struct path_cond cond
= { };
144 /* find first subcomponent that is visible */
145 label_for_each(i
, label
, tp
) {
146 if (!aa_ns_visible(profile
->ns
, tp
->ns
, subns
))
148 state
= match_component(profile
, tp
, stack
, state
);
154 /* no component visible */
159 label_for_each_cont(i
, label
, tp
) {
160 if (!aa_ns_visible(profile
->ns
, tp
->ns
, subns
))
162 state
= aa_dfa_match(profile
->file
.dfa
, state
, "//&");
163 state
= match_component(profile
, tp
, false, state
);
167 *perms
= aa_compute_fperms(profile
->file
.dfa
, state
, &cond
);
168 aa_apply_modes_to_perms(profile
, perms
);
169 if ((perms
->allow
& request
) != request
)
180 * label_components_match - find perms for all subcomponents of a label
181 * @profile: profile to find perms for
182 * @label: label to check access permissions for
183 * @stack: whether this is a stacking request
184 * @start: state to start match in
185 * @subns: whether to do permission checks on components in a subns
186 * @request: permissions to request
187 * @perms: an initialized perms struct to add accumulation to
189 * Returns: 0 on success else ERROR
191 * For the label A//&B//&C this does the perm match for each of A and B and C
192 * @perms should be preinitialized with allperms OR a previous permission
193 * check to be stacked.
195 static int label_components_match(struct aa_profile
*profile
,
196 struct aa_label
*label
, bool stack
,
197 unsigned int start
, bool subns
, u32 request
,
198 struct aa_perms
*perms
)
200 struct aa_profile
*tp
;
203 struct path_cond cond
= { };
204 unsigned int state
= 0;
206 /* find first subcomponent to test */
207 label_for_each(i
, label
, tp
) {
208 if (!aa_ns_visible(profile
->ns
, tp
->ns
, subns
))
210 state
= match_component(profile
, tp
, stack
, start
);
216 /* no subcomponents visible - no change in perms */
220 tmp
= aa_compute_fperms(profile
->file
.dfa
, state
, &cond
);
221 aa_apply_modes_to_perms(profile
, &tmp
);
222 aa_perms_accum(perms
, &tmp
);
223 label_for_each_cont(i
, label
, tp
) {
224 if (!aa_ns_visible(profile
->ns
, tp
->ns
, subns
))
226 state
= match_component(profile
, tp
, stack
, start
);
229 tmp
= aa_compute_fperms(profile
->file
.dfa
, state
, &cond
);
230 aa_apply_modes_to_perms(profile
, &tmp
);
231 aa_perms_accum(perms
, &tmp
);
234 if ((perms
->allow
& request
) != request
)
245 * label_match - do a multi-component label match
246 * @profile: profile to match against (NOT NULL)
247 * @label: label to match (NOT NULL)
248 * @stack: whether this is a stacking request
249 * @state: state to start in
250 * @subns: whether to match subns components
251 * @request: permission request
252 * @perms: Returns computed perms (NOT NULL)
254 * Returns: the state the match finished in, may be the none matching state
256 static int label_match(struct aa_profile
*profile
, struct aa_label
*label
,
257 bool stack
, unsigned int state
, bool subns
, u32 request
,
258 struct aa_perms
*perms
)
263 error
= label_compound_match(profile
, label
, stack
, state
, subns
,
269 return label_components_match(profile
, label
, stack
, state
, subns
,
273 /******* end TODO: dedup *****/
276 * change_profile_perms - find permissions for change_profile
277 * @profile: the current profile (NOT NULL)
278 * @target: label to transition to (NOT NULL)
279 * @stack: whether this is a stacking request
280 * @request: requested perms
281 * @start: state to start matching in
284 * Returns: permission set
286 * currently only matches full label A//&B//&C or individual components A, B, C
287 * not arbitrary combinations. Eg. A//&B, C
289 static int change_profile_perms(struct aa_profile
*profile
,
290 struct aa_label
*target
, bool stack
,
291 u32 request
, unsigned int start
,
292 struct aa_perms
*perms
)
294 if (profile_unconfined(profile
)) {
295 perms
->allow
= AA_MAY_CHANGE_PROFILE
| AA_MAY_ONEXEC
;
296 perms
->audit
= perms
->quiet
= perms
->kill
= 0;
300 /* TODO: add profile in ns screening */
301 return label_match(profile
, target
, stack
, start
, true, request
, perms
);
305 * __attach_match_ - find an attachment match
306 * @name - to match against (NOT NULL)
307 * @head - profile list to walk (NOT NULL)
309 * Do a linear search on the profiles in the list. There is a matching
310 * preference where an exact match is preferred over a name which uses
311 * expressions to match, and matching expressions with the greatest
312 * xmatch_len are preferred.
314 * Requires: @head not be shared or have appropriate locks held
316 * Returns: profile or NULL if no match found
318 static struct aa_profile
*__attach_match(const char *name
,
319 struct list_head
*head
)
322 struct aa_profile
*profile
, *candidate
= NULL
;
324 list_for_each_entry_rcu(profile
, head
, base
.list
) {
325 if (profile
->label
.flags
& FLAG_NULL
)
327 if (profile
->xmatch
&& profile
->xmatch_len
> len
) {
328 unsigned int state
= aa_dfa_match(profile
->xmatch
,
330 u32 perm
= dfa_user_allow(profile
->xmatch
, state
);
331 /* any accepting state means a valid match. */
332 if (perm
& MAY_EXEC
) {
334 len
= profile
->xmatch_len
;
336 } else if (!strcmp(profile
->base
.name
, name
))
337 /* exact non-re match, no more searching required */
345 * find_attach - do attachment search for unconfined processes
346 * @ns: the current namespace (NOT NULL)
347 * @list: list to search (NOT NULL)
348 * @name: the executable name to match against (NOT NULL)
350 * Returns: label or NULL if no match found
352 static struct aa_label
*find_attach(struct aa_ns
*ns
, struct list_head
*list
,
355 struct aa_profile
*profile
;
358 profile
= aa_get_profile(__attach_match(name
, list
));
361 return profile
? &profile
->label
: NULL
;
364 static const char *next_name(int xtype
, const char *name
)
370 * x_table_lookup - lookup an x transition name via transition table
371 * @profile: current profile (NOT NULL)
372 * @xindex: index into x transition table
373 * @name: returns: name tested to find label (NOT NULL)
375 * Returns: refcounted label, or NULL on failure (MAYBE NULL)
377 static struct aa_label
*x_table_lookup(struct aa_profile
*profile
, u32 xindex
,
380 struct aa_label
*label
= NULL
;
381 u32 xtype
= xindex
& AA_X_TYPE_MASK
;
382 int index
= xindex
& AA_X_INDEX_MASK
;
386 /* index is guaranteed to be in range, validated at load time */
387 /* TODO: move lookup parsing to unpack time so this is a straight
388 * index into the resultant label
390 for (*name
= profile
->file
.trans
.table
[index
]; !label
&& *name
;
391 *name
= next_name(xtype
, *name
)) {
392 if (xindex
& AA_X_CHILD
) {
393 struct aa_profile
*new_profile
;
394 /* release by caller */
395 new_profile
= aa_find_child(profile
, *name
);
397 label
= &new_profile
->label
;
400 label
= aa_label_parse(&profile
->label
, *name
, GFP_ATOMIC
,
406 /* released by caller */
412 * x_to_label - get target label for a given xindex
413 * @profile: current profile (NOT NULL)
414 * @name: name to lookup (NOT NULL)
415 * @xindex: index into x transition table
416 * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
418 * find label for a transition index
420 * Returns: refcounted label or NULL if not found available
422 static struct aa_label
*x_to_label(struct aa_profile
*profile
,
423 const char *name
, u32 xindex
,
424 const char **lookupname
,
427 struct aa_label
*new = NULL
;
428 struct aa_ns
*ns
= profile
->ns
;
429 u32 xtype
= xindex
& AA_X_TYPE_MASK
;
430 const char *stack
= NULL
;
434 /* fail exec unless ix || ux fallback - handled by caller */
438 /* TODO: fix when perm mapping done at unload */
439 stack
= profile
->file
.trans
.table
[xindex
& AA_X_INDEX_MASK
];
441 /* released by caller */
442 new = x_table_lookup(profile
, xindex
, lookupname
);
446 /* fall through to X_NAME */
448 if (xindex
& AA_X_CHILD
)
449 /* released by caller */
450 new = find_attach(ns
, &profile
->base
.profiles
,
453 /* released by caller */
454 new = find_attach(ns
, &ns
->base
.profiles
,
461 if (xindex
& AA_X_INHERIT
) {
462 /* (p|c|n)ix - don't change profile but do
463 * use the newest version
465 *info
= "ix fallback";
466 /* no profile && no error */
467 new = aa_get_newest_label(&profile
->label
);
468 } else if (xindex
& AA_X_UNCONFINED
) {
469 new = aa_get_newest_label(ns_unconfined(profile
->ns
));
470 *info
= "ux fallback";
475 /* base the stack on post domain transition */
476 struct aa_label
*base
= new;
478 new = aa_label_parse(base
, stack
, GFP_ATOMIC
, true, false);
484 /* released by caller */
488 static struct aa_label
*profile_transition(struct aa_profile
*profile
,
489 const struct linux_binprm
*bprm
,
490 char *buffer
, struct path_cond
*cond
,
493 struct aa_label
*new = NULL
;
494 const char *info
= NULL
, *name
= NULL
, *target
= NULL
;
495 unsigned int state
= profile
->file
.start
;
496 struct aa_perms perms
= {};
497 bool nonewprivs
= false;
504 error
= aa_path_name(&bprm
->file
->f_path
, profile
->path_flags
, buffer
,
505 &name
, &info
, profile
->disconnected
);
507 if (profile_unconfined(profile
) ||
508 (profile
->label
.flags
& FLAG_IX_ON_NAME_ERROR
)) {
509 AA_DEBUG("name lookup ix on error");
511 new = aa_get_newest_label(&profile
->label
);
513 name
= bprm
->filename
;
517 if (profile_unconfined(profile
)) {
518 new = find_attach(profile
->ns
, &profile
->ns
->base
.profiles
,
521 AA_DEBUG("unconfined attached to new label");
524 AA_DEBUG("unconfined exec no attachment");
525 return aa_get_newest_label(&profile
->label
);
528 /* find exec permissions for name */
529 state
= aa_str_perms(profile
->file
.dfa
, state
, name
, cond
, &perms
);
530 if (perms
.allow
& MAY_EXEC
) {
531 /* exec permission determine how to transition */
532 new = x_to_label(profile
, name
, perms
.xindex
, &target
, &info
);
533 if (new && new->proxy
== profile
->label
.proxy
&& info
) {
534 /* hack ix fallback - improve how this is detected */
538 info
= "profile transition not found";
539 /* remove MAY_EXEC to audit as failure */
540 perms
.allow
&= ~MAY_EXEC
;
542 } else if (COMPLAIN_MODE(profile
)) {
543 /* no exec permission - learning mode */
544 struct aa_profile
*new_profile
= aa_new_null_profile(profile
,
549 info
= "could not create null profile";
552 new = &new_profile
->label
;
554 perms
.xindex
|= AA_X_UNSAFE
;
562 /* Policy has specified a domain transitions. if no_new_privs and
563 * confined and not transitioning to the current domain fail.
565 * NOTE: Domain transitions from unconfined and to stritly stacked
566 * subsets are allowed even when no_new_privs is set because this
567 * aways results in a further reduction of permissions.
569 if ((bprm
->unsafe
& LSM_UNSAFE_NO_NEW_PRIVS
) &&
570 !profile_unconfined(profile
) &&
571 !aa_label_is_subset(new, &profile
->label
)) {
573 info
= "no new privs";
575 perms
.allow
&= ~MAY_EXEC
;
579 if (!(perms
.xindex
& AA_X_UNSAFE
)) {
581 dbg_printk("apparmor: scrubbing environment variables"
582 " for %s profile=", name
);
583 aa_label_printk(new, GFP_ATOMIC
);
590 aa_audit_file(profile
, &perms
, OP_EXEC
, MAY_EXEC
, name
, target
, new,
591 cond
->uid
, info
, error
);
592 if (!new || nonewprivs
) {
594 return ERR_PTR(error
);
600 static int profile_onexec(struct aa_profile
*profile
, struct aa_label
*onexec
,
601 bool stack
, const struct linux_binprm
*bprm
,
602 char *buffer
, struct path_cond
*cond
,
605 unsigned int state
= profile
->file
.start
;
606 struct aa_perms perms
= {};
607 const char *xname
= NULL
, *info
= "change_profile onexec";
615 if (profile_unconfined(profile
)) {
616 /* change_profile on exec already granted */
618 * NOTE: Domain transitions from unconfined are allowed
619 * even when no_new_privs is set because this aways results
620 * in a further reduction of permissions.
625 error
= aa_path_name(&bprm
->file
->f_path
, profile
->path_flags
, buffer
,
626 &xname
, &info
, profile
->disconnected
);
628 if (profile_unconfined(profile
) ||
629 (profile
->label
.flags
& FLAG_IX_ON_NAME_ERROR
)) {
630 AA_DEBUG("name lookup ix on error");
633 xname
= bprm
->filename
;
637 /* find exec permissions for name */
638 state
= aa_str_perms(profile
->file
.dfa
, state
, xname
, cond
, &perms
);
639 if (!(perms
.allow
& AA_MAY_ONEXEC
)) {
640 info
= "no change_onexec valid for executable";
643 /* test if this exec can be paired with change_profile onexec.
644 * onexec permission is linked to exec with a standard pairing
645 * exec\0change_profile
647 state
= aa_dfa_null_transition(profile
->file
.dfa
, state
);
648 error
= change_profile_perms(profile
, onexec
, stack
, AA_MAY_ONEXEC
,
651 perms
.allow
&= ~AA_MAY_ONEXEC
;
654 /* Policy has specified a domain transitions. if no_new_privs and
655 * confined and not transitioning to the current domain fail.
657 * NOTE: Domain transitions from unconfined and to stritly stacked
658 * subsets are allowed even when no_new_privs is set because this
659 * aways results in a further reduction of permissions.
661 if ((bprm
->unsafe
& LSM_UNSAFE_NO_NEW_PRIVS
) &&
662 !profile_unconfined(profile
) &&
663 !aa_label_is_subset(onexec
, &profile
->label
)) {
665 info
= "no new privs";
666 perms
.allow
&= ~AA_MAY_ONEXEC
;
670 if (!(perms
.xindex
& AA_X_UNSAFE
)) {
672 dbg_printk("apparmor: scrubbing environment "
673 "variables for %s label=", xname
);
674 aa_label_printk(onexec
, GFP_ATOMIC
);
681 return aa_audit_file(profile
, &perms
, OP_EXEC
, AA_MAY_ONEXEC
, xname
,
682 NULL
, onexec
, cond
->uid
, info
, error
);
685 /* ensure none ns domain transitions are correctly applied with onexec */
687 static struct aa_label
*handle_onexec(struct aa_label
*label
,
688 struct aa_label
*onexec
, bool stack
,
689 const struct linux_binprm
*bprm
,
690 char *buffer
, struct path_cond
*cond
,
693 struct aa_profile
*profile
;
694 struct aa_label
*new;
703 error
= fn_for_each_in_ns(label
, profile
,
704 profile_onexec(profile
, onexec
, stack
,
705 bprm
, buffer
, cond
, unsafe
));
707 return ERR_PTR(error
);
708 new = fn_label_build_in_ns(label
, profile
, GFP_ATOMIC
,
709 aa_get_newest_label(onexec
),
710 profile_transition(profile
, bprm
, buffer
,
714 /* TODO: determine how much we want to losen this */
715 error
= fn_for_each_in_ns(label
, profile
,
716 profile_onexec(profile
, onexec
, stack
, bprm
,
717 buffer
, cond
, unsafe
));
719 return ERR_PTR(error
);
720 new = fn_label_build_in_ns(label
, profile
, GFP_ATOMIC
,
721 aa_label_merge(&profile
->label
, onexec
,
723 profile_transition(profile
, bprm
, buffer
,
730 /* TODO: get rid of GLOBAL_ROOT_UID */
731 error
= fn_for_each_in_ns(label
, profile
,
732 aa_audit_file(profile
, &nullperms
, OP_CHANGE_ONEXEC
,
733 AA_MAY_ONEXEC
, bprm
->filename
, NULL
,
734 onexec
, GLOBAL_ROOT_UID
,
735 "failed to build target label", -ENOMEM
));
736 return ERR_PTR(error
);
740 * apparmor_bprm_set_creds - set the new creds on the bprm struct
741 * @bprm: binprm for the exec (NOT NULL)
743 * Returns: %0 or error on failure
745 * TODO: once the other paths are done see if we can't refactor into a fn
747 int apparmor_bprm_set_creds(struct linux_binprm
*bprm
)
749 struct aa_task_ctx
*ctx
;
750 struct aa_label
*label
, *new = NULL
;
751 struct aa_profile
*profile
;
753 const char *info
= NULL
;
756 struct path_cond cond
= {
757 file_inode(bprm
->file
)->i_uid
,
758 file_inode(bprm
->file
)->i_mode
761 if (bprm
->cred_prepared
)
764 ctx
= cred_ctx(bprm
->cred
);
767 label
= aa_get_newest_label(ctx
->label
);
769 /* buffer freed below, name is pointer into buffer */
771 /* Test for onexec first as onexec override other x transitions. */
773 new = handle_onexec(label
, ctx
->onexec
, ctx
->token
,
774 bprm
, buffer
, &cond
, &unsafe
);
776 new = fn_label_build(label
, profile
, GFP_ATOMIC
,
777 profile_transition(profile
, bprm
, buffer
,
782 error
= PTR_ERR(new);
789 /* TODO: Add ns level no_new_privs subset test */
791 if (bprm
->unsafe
& LSM_UNSAFE_SHARE
) {
792 /* FIXME: currently don't mediate shared state */
796 if (bprm
->unsafe
& (LSM_UNSAFE_PTRACE
)) {
797 /* TODO: test needs to be profile of label to new */
798 error
= may_change_ptraced_domain(new, &info
);
805 dbg_printk("scrubbing environment variables for %s "
806 "label=", bprm
->filename
);
807 aa_label_printk(new, GFP_ATOMIC
);
810 bprm
->unsafe
|= AA_SECURE_X_NEEDED
;
813 if (label
->proxy
!= new->proxy
) {
814 /* when transitioning clear unsafe personality bits */
816 dbg_printk("apparmor: clearing unsafe personality "
817 "bits. %s label=", bprm
->filename
);
818 aa_label_printk(new, GFP_ATOMIC
);
821 bprm
->per_clear
|= PER_CLEAR_ON_SETID
;
823 aa_put_label(ctx
->label
);
824 /* transfer reference, released when ctx is freed */
828 /* clear out temporary/transitional state from the context */
829 aa_clear_task_ctx_trans(ctx
);
837 error
= fn_for_each(label
, profile
,
838 aa_audit_file(profile
, &nullperms
, OP_EXEC
, MAY_EXEC
,
839 bprm
->filename
, NULL
, new,
840 file_inode(bprm
->file
)->i_uid
, info
,
847 * apparmor_bprm_secureexec - determine if secureexec is needed
848 * @bprm: binprm for exec (NOT NULL)
850 * Returns: %1 if secureexec is needed else %0
852 int apparmor_bprm_secureexec(struct linux_binprm
*bprm
)
854 /* the decision to use secure exec is computed in set_creds
855 * and stored in bprm->unsafe.
857 if (bprm
->unsafe
& AA_SECURE_X_NEEDED
)
864 * Functions for self directed profile change
868 /* helper fn for change_hat
870 * Returns: label for hat transition OR ERR_PTR. Does NOT return NULL
872 static struct aa_label
*build_change_hat(struct aa_profile
*profile
,
873 const char *name
, bool sibling
)
875 struct aa_profile
*root
, *hat
= NULL
;
876 const char *info
= NULL
;
879 if (sibling
&& PROFILE_IS_HAT(profile
)) {
880 root
= aa_get_profile_rcu(&profile
->parent
);
881 } else if (!sibling
&& !PROFILE_IS_HAT(profile
)) {
882 root
= aa_get_profile(profile
);
884 info
= "conflicting target types";
889 hat
= aa_find_child(root
, name
);
892 if (COMPLAIN_MODE(profile
)) {
893 hat
= aa_new_null_profile(profile
, true, name
,
896 info
= "failed null profile create";
901 aa_put_profile(root
);
904 aa_audit_file(profile
, &nullperms
, OP_CHANGE_HAT
, AA_MAY_CHANGEHAT
,
905 name
, hat
? hat
->base
.hname
: NULL
,
906 hat
? &hat
->label
: NULL
, GLOBAL_ROOT_UID
, NULL
,
908 if (!hat
|| (error
&& error
!= -ENOENT
))
909 return ERR_PTR(error
);
910 /* if hat && error - complain mode, already audited and we adjust for
911 * complain mode allow by returning hat->label
916 /* helper fn for changing into a hat
918 * Returns: label for hat transition or ERR_PTR. Does not return NULL
920 static struct aa_label
*change_hat(struct aa_label
*label
, const char *hats
[],
921 int count
, int flags
)
923 struct aa_profile
*profile
, *root
, *hat
= NULL
;
924 struct aa_label
*new;
926 bool sibling
= false;
927 const char *name
, *info
= NULL
;
934 if (PROFILE_IS_HAT(labels_profile(label
)))
937 /*find first matching hat */
938 for (i
= 0; i
< count
&& !hat
; i
++) {
940 label_for_each_in_ns(it
, labels_ns(label
), label
, profile
) {
941 if (sibling
&& PROFILE_IS_HAT(profile
)) {
942 root
= aa_get_profile_rcu(&profile
->parent
);
943 } else if (!sibling
&& !PROFILE_IS_HAT(profile
)) {
944 root
= aa_get_profile(profile
);
945 } else { /* conflicting change type */
946 info
= "conflicting targets types";
950 hat
= aa_find_child(root
, name
);
951 aa_put_profile(root
);
953 if (!COMPLAIN_MODE(profile
))
955 /* complain mode succeed as if hat */
956 } else if (!PROFILE_IS_HAT(hat
)) {
957 info
= "target not hat";
964 /* found a hat for all profiles in ns */
969 /* no hats that match, find appropriate error
971 * In complain mode audit of the failure is based off of the first
972 * hat supplied. This is done due how userspace interacts with
976 label_for_each_in_ns(it
, labels_ns(label
), label
, profile
) {
977 if (!list_empty(&profile
->base
.profiles
)) {
978 info
= "hat not found";
983 info
= "no hats defined";
987 label_for_each_in_ns(it
, labels_ns(label
), label
, profile
) {
989 * no target as it has failed to be found or built
991 * change_hat uses probing and should not log failures
992 * related to missing hats
994 /* TODO: get rid of GLOBAL_ROOT_UID */
995 if (count
> 1 || COMPLAIN_MODE(profile
)) {
996 aa_audit_file(profile
, &nullperms
, OP_CHANGE_HAT
,
997 AA_MAY_CHANGEHAT
, name
, NULL
, NULL
,
998 GLOBAL_ROOT_UID
, info
, error
);
1001 return ERR_PTR(error
);
1004 new = fn_label_build_in_ns(label
, profile
, GFP_KERNEL
,
1005 build_change_hat(profile
, name
, sibling
),
1006 aa_get_label(&profile
->label
));
1008 info
= "label build failed";
1011 } /* else if (IS_ERR) build_change_hat has logged error so return new */
1017 * aa_change_hat - change hat to/from subprofile
1018 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1019 * @count: number of hat names in @hats
1020 * @token: magic value to validate the hat change
1021 * @flags: flags affecting behavior of the change
1023 * Returns %0 on success, error otherwise.
1025 * Change to the first profile specified in @hats that exists, and store
1026 * the @hat_magic in the current task context. If the count == 0 and the
1027 * @token matches that stored in the current task context, return to the
1028 * top level profile.
1030 * change_hat only applies to profiles in the current ns, and each profile
1031 * in the ns must make the same transition otherwise change_hat will fail.
1033 int aa_change_hat(const char *hats
[], int count
, u64 token
, int flags
)
1035 const struct cred
*cred
;
1036 struct aa_task_ctx
*ctx
;
1037 struct aa_label
*label
, *previous
, *new = NULL
, *target
= NULL
;
1038 struct aa_profile
*profile
;
1039 struct aa_perms perms
= {};
1040 const char *info
= NULL
;
1044 * Fail explicitly requested domain transitions if no_new_privs.
1045 * There is no exception for unconfined as change_hat is not
1048 if (task_no_new_privs(current
)) {
1049 /* not an apparmor denial per se, so don't log it */
1050 AA_DEBUG("no_new_privs - change_hat denied");
1054 /* released below */
1055 cred
= get_current_cred();
1056 ctx
= cred_ctx(cred
);
1057 label
= aa_get_newest_cred_label(cred
);
1058 previous
= aa_get_newest_label(ctx
->previous
);
1060 if (unconfined(label
)) {
1061 info
= "unconfined can not change_hat";
1067 new = change_hat(label
, hats
, count
, flags
);
1070 error
= PTR_ERR(new);
1072 /* already audited */
1076 error
= may_change_ptraced_domain(new, &info
);
1080 if (flags
& AA_CHANGE_TEST
)
1084 error
= aa_set_current_hat(new, token
);
1085 if (error
== -EACCES
)
1086 /* kill task in case of brute force attacks */
1088 } else if (previous
&& !(flags
& AA_CHANGE_TEST
)) {
1089 /* Return to saved label. Kill task if restore fails
1090 * to avoid brute force attacks
1093 error
= aa_restore_previous_label(token
);
1095 if (error
== -EACCES
)
1099 } /* else ignore @flags && restores when there is no saved profile */
1103 aa_put_label(previous
);
1104 aa_put_label(label
);
1110 info
= "failed token match";
1111 perms
.kill
= AA_MAY_CHANGEHAT
;
1114 fn_for_each_in_ns(label
, profile
,
1115 aa_audit_file(profile
, &perms
, OP_CHANGE_HAT
,
1116 AA_MAY_CHANGEHAT
, NULL
, NULL
, target
,
1117 GLOBAL_ROOT_UID
, info
, error
));
1123 static int change_profile_perms_wrapper(const char *op
, const char *name
,
1124 struct aa_profile
*profile
,
1125 struct aa_label
*target
, bool stack
,
1126 u32 request
, struct aa_perms
*perms
)
1128 const char *info
= NULL
;
1132 * Fail explicitly requested domain transitions when no_new_privs
1133 * and not unconfined OR the transition results in a stack on
1134 * the current label.
1135 * Stacking domain transitions and transitions from unconfined are
1136 * allowed even when no_new_privs is set because this aways results
1137 * in a reduction of permissions.
1139 if (task_no_new_privs(current
) && !stack
&&
1140 !profile_unconfined(profile
) &&
1141 !aa_label_is_subset(target
, &profile
->label
)) {
1142 info
= "no new privs";
1147 error
= change_profile_perms(profile
, target
, stack
, request
,
1148 profile
->file
.start
, perms
);
1150 error
= aa_audit_file(profile
, perms
, op
, request
, name
,
1151 NULL
, target
, GLOBAL_ROOT_UID
, info
,
1158 * aa_change_profile - perform a one-way profile transition
1159 * @fqname: name of profile may include namespace (NOT NULL)
1160 * @onexec: whether this transition is to take place immediately or at exec
1161 * @flags: flags affecting change behavior
1163 * Change to new profile @name. Unlike with hats, there is no way
1164 * to change back. If @name isn't specified the current profile name is
1166 * If @onexec then the transition is delayed until
1169 * Returns %0 on success, error otherwise.
1171 int aa_change_profile(const char *fqname
, int flags
)
1173 struct aa_label
*label
, *new = NULL
, *target
= NULL
;
1174 struct aa_profile
*profile
;
1175 struct aa_perms perms
= {};
1176 const char *info
= NULL
;
1177 const char *auditname
= fqname
; /* retain leading & if stack */
1178 bool stack
= flags
& AA_CHANGE_STACK
;
1183 if (!fqname
|| !*fqname
) {
1184 AA_DEBUG("no profile name");
1188 if (flags
& AA_CHANGE_ONEXEC
) {
1189 request
= AA_MAY_ONEXEC
;
1191 op
= OP_STACK_ONEXEC
;
1193 op
= OP_CHANGE_ONEXEC
;
1195 request
= AA_MAY_CHANGE_PROFILE
;
1199 op
= OP_CHANGE_PROFILE
;
1202 label
= aa_get_current_label();
1204 if (*fqname
== '&') {
1206 /* don't have label_parse() do stacking */
1209 target
= aa_label_parse(label
, fqname
, GFP_KERNEL
, true, false);
1210 if (IS_ERR(target
)) {
1211 struct aa_profile
*tprofile
;
1213 info
= "label not found";
1214 error
= PTR_ERR(target
);
1217 * TODO: fixme using labels_profile is not right - do profile
1218 * per complain profile
1220 if ((flags
& AA_CHANGE_TEST
) ||
1221 !COMPLAIN_MODE(labels_profile(label
)))
1223 /* released below */
1224 tprofile
= aa_new_null_profile(labels_profile(label
), false,
1225 fqname
, GFP_KERNEL
);
1227 info
= "failed null profile create";
1231 target
= &tprofile
->label
;
1236 * self directed transitions only apply to current policy ns
1237 * TODO: currently requiring perms for stacking and straight change
1238 * stacking doesn't strictly need this. Determine how much
1239 * we want to loosen this restriction for stacking
1243 error
= fn_for_each_in_ns(label
, profile
,
1244 change_profile_perms_wrapper(op
, auditname
,
1245 profile
, target
, stack
,
1248 /* auditing done in change_profile_perms_wrapper */
1254 /* check if tracing task is allowed to trace target domain */
1255 error
= may_change_ptraced_domain(target
, &info
);
1256 if (error
&& !fn_for_each_in_ns(label
, profile
,
1257 COMPLAIN_MODE(profile
)))
1260 /* TODO: add permission check to allow this
1261 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
1262 * info = "not a single threaded task";
1267 if (flags
& AA_CHANGE_TEST
)
1270 if (!(flags
& AA_CHANGE_ONEXEC
)) {
1271 /* only transition profiles in the current ns */
1273 new = aa_label_merge(label
, target
, GFP_KERNEL
);
1275 new = fn_label_build_in_ns(label
, profile
, GFP_KERNEL
,
1276 aa_get_label(target
),
1277 aa_get_label(&profile
->label
));
1278 if (IS_ERR_OR_NULL(new)) {
1279 info
= "failed to build target label";
1280 error
= PTR_ERR(new);
1285 error
= aa_replace_current_label(new);
1287 /* full transition will be built in exec path */
1288 error
= aa_set_current_onexec(target
, stack
);
1291 error
= fn_for_each_in_ns(label
, profile
,
1292 aa_audit_file(profile
, &perms
, op
, request
, auditname
,
1293 NULL
, new ? new : target
,
1294 GLOBAL_ROOT_UID
, info
, error
));
1298 aa_put_label(target
);
1299 aa_put_label(label
);