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_profile: profile to change to (NOT NULL)
56 * Check if current is ptraced and if so if the tracing task is allowed
57 * to trace the new domain
59 * Returns: %0 or error if change not allowed
61 static int may_change_ptraced_domain(struct aa_profile *to_profile)
63 struct task_struct *tracer;
64 struct aa_profile *tracerp = NULL;
68 tracer = ptrace_parent(current);
71 tracerp = aa_get_task_profile(tracer);
74 if (!tracer || unconfined(tracerp))
77 error = aa_may_ptrace(tracerp, to_profile, PTRACE_MODE_ATTACH);
81 aa_put_profile(tracerp);
87 * change_profile_perms - find permissions for change_profile
88 * @profile: the current profile (NOT NULL)
89 * @ns: the namespace being switched to (NOT NULL)
90 * @name: the name of the profile to change to (NOT NULL)
91 * @request: requested perms
92 * @start: state to start matching in
94 * Returns: permission set
96 static struct file_perms change_profile_perms(struct aa_profile *profile,
98 const char *name, u32 request,
101 struct file_perms perms;
102 struct path_cond cond = { };
105 if (unconfined(profile)) {
106 perms.allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
107 perms.audit = perms.quiet = perms.kill = 0;
109 } else if (!profile->file.dfa) {
111 } else if ((ns == profile->ns)) {
112 /* try matching against rules with out namespace prepended */
113 aa_str_perms(profile->file.dfa, start, name, &cond, &perms);
114 if (COMBINED_PERM_MASK(perms) & request)
118 /* try matching with namespace name and then profile */
119 state = aa_dfa_match(profile->file.dfa, start, ns->base.name);
120 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
121 aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
127 * __attach_match_ - find an attachment match
128 * @name - to match against (NOT NULL)
129 * @head - profile list to walk (NOT NULL)
131 * Do a linear search on the profiles in the list. There is a matching
132 * preference where an exact match is preferred over a name which uses
133 * expressions to match, and matching expressions with the greatest
134 * xmatch_len are preferred.
136 * Requires: @head not be shared or have appropriate locks held
138 * Returns: profile or NULL if no match found
140 static struct aa_profile *__attach_match(const char *name,
141 struct list_head *head)
144 struct aa_profile *profile, *candidate = NULL;
146 list_for_each_entry_rcu(profile, head, base.list) {
147 if (profile->flags & PFLAG_NULL)
149 if (profile->xmatch && profile->xmatch_len > len) {
150 unsigned int state = aa_dfa_match(profile->xmatch,
152 u32 perm = dfa_user_allow(profile->xmatch, state);
153 /* any accepting state means a valid match. */
154 if (perm & MAY_EXEC) {
156 len = profile->xmatch_len;
158 } else if (!strcmp(profile->base.name, name))
159 /* exact non-re match, no more searching required */
167 * find_attach - do attachment search for unconfined processes
168 * @ns: the current namespace (NOT NULL)
169 * @list: list to search (NOT NULL)
170 * @name: the executable name to match against (NOT NULL)
172 * Returns: profile or NULL if no match found
174 static struct aa_profile *find_attach(struct aa_ns *ns,
175 struct list_head *list, const char *name)
177 struct aa_profile *profile;
180 profile = aa_get_profile(__attach_match(name, list));
187 * separate_fqname - separate the namespace and profile names
188 * @fqname: the fqname name to split (NOT NULL)
189 * @ns_name: the namespace name if it exists (NOT NULL)
191 * This is the xtable equivalent routine of aa_split_fqname. It finds the
192 * split in an xtable fqname which contains an embedded \0 instead of a :
193 * if a namespace is specified. This is done so the xtable is constant and
194 * isn't re-split on every lookup.
196 * Either the profile or namespace name may be optional but if the namespace
197 * is specified the profile name termination must be present. This results
198 * in the following possible encodings:
200 * :ns_name\0profile_name\0
203 * NOTE: the xtable fqname is pre-validated at load time in unpack_trans_table
205 * Returns: profile name if it is specified else NULL
207 static const char *separate_fqname(const char *fqname, const char **ns_name)
211 if (fqname[0] == ':') {
212 /* In this case there is guaranteed to be two \0 terminators
213 * in the string. They are verified at load time by
214 * by unpack_trans_table
216 *ns_name = fqname + 1; /* skip : */
217 name = *ns_name + strlen(*ns_name) + 1;
228 static const char *next_name(int xtype, const char *name)
234 * x_table_lookup - lookup an x transition name via transition table
235 * @profile: current profile (NOT NULL)
236 * @xindex: index into x transition table
238 * Returns: refcounted profile, or NULL on failure (MAYBE NULL)
240 static struct aa_profile *x_table_lookup(struct aa_profile *profile, u32 xindex)
242 struct aa_profile *new_profile = NULL;
243 struct aa_ns *ns = profile->ns;
244 u32 xtype = xindex & AA_X_TYPE_MASK;
245 int index = xindex & AA_X_INDEX_MASK;
248 /* index is guaranteed to be in range, validated at load time */
249 for (name = profile->file.trans.table[index]; !new_profile && name;
250 name = next_name(xtype, name)) {
251 struct aa_ns *new_ns;
252 const char *xname = NULL;
255 if (xindex & AA_X_CHILD) {
256 /* release by caller */
257 new_profile = aa_find_child(profile, name);
259 } else if (*name == ':') {
260 /* switching namespace */
262 xname = name = separate_fqname(name, &ns_name);
264 /* no name so use profile name */
265 xname = profile->base.hname;
266 if (*ns_name == '@') {
267 /* TODO: variable support */
271 new_ns = aa_find_ns(ns, ns_name);
274 } else if (*name == '@') {
275 /* TODO: variable support */
278 /* basic namespace lookup */
282 /* released by caller */
283 new_profile = aa_lookup_profile(new_ns ? new_ns : ns, xname);
287 /* released by caller */
292 * x_to_profile - get target profile for a given xindex
293 * @profile: current profile (NOT NULL)
294 * @name: name to lookup (NOT NULL)
295 * @xindex: index into x transition table
297 * find profile for a transition index
299 * Returns: refcounted profile or NULL if not found available
301 static struct aa_profile *x_to_profile(struct aa_profile *profile,
302 const char *name, u32 xindex)
304 struct aa_profile *new_profile = NULL;
305 struct aa_ns *ns = profile->ns;
306 u32 xtype = xindex & AA_X_TYPE_MASK;
310 /* fail exec unless ix || ux fallback - handled by caller */
313 if (xindex & AA_X_CHILD)
314 /* released by caller */
315 new_profile = find_attach(ns, &profile->base.profiles,
318 /* released by caller */
319 new_profile = find_attach(ns, &ns->base.profiles,
323 /* released by caller */
324 new_profile = x_table_lookup(profile, xindex);
328 /* released by caller */
333 * apparmor_bprm_set_creds - set the new creds on the bprm struct
334 * @bprm: binprm for the exec (NOT NULL)
336 * Returns: %0 or error on failure
338 int apparmor_bprm_set_creds(struct linux_binprm *bprm)
340 struct aa_task_ctx *ctx;
341 struct aa_profile *profile, *new_profile = NULL;
345 struct file_perms perms = {};
346 struct path_cond cond = {
347 file_inode(bprm->file)->i_uid,
348 file_inode(bprm->file)->i_mode
350 const char *name = NULL, *info = NULL;
353 if (bprm->cred_prepared)
356 ctx = cred_ctx(bprm->cred);
359 profile = aa_get_newest_profile(ctx->profile);
361 * get the namespace from the replacement profile as replacement
362 * can change the namespace
365 state = profile->file.start;
367 /* buffer freed below, name is pointer into buffer */
368 error = aa_path_name(&bprm->file->f_path, profile->path_flags, &buffer,
371 if (unconfined(profile) ||
372 (profile->flags & PFLAG_IX_ON_NAME_ERROR))
374 name = bprm->filename;
378 /* Test for onexec first as onexec directives override other
381 if (unconfined(profile)) {
382 /* unconfined task */
384 /* change_profile on exec already been granted */
385 new_profile = aa_get_profile(ctx->onexec);
387 new_profile = find_attach(ns, &ns->base.profiles, name);
391 * NOTE: Domain transitions from unconfined are allowed
392 * even when no_new_privs is set because this aways results
393 * in a further reduction of permissions.
398 /* find exec permissions for name */
399 state = aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
401 struct file_perms cp;
402 info = "change_profile onexec";
403 new_profile = aa_get_newest_profile(ctx->onexec);
404 if (!(perms.allow & AA_MAY_ONEXEC))
407 /* test if this exec can be paired with change_profile onexec.
408 * onexec permission is linked to exec with a standard pairing
409 * exec\0change_profile
411 state = aa_dfa_null_transition(profile->file.dfa, state);
412 cp = change_profile_perms(profile, ctx->onexec->ns,
413 ctx->onexec->base.name,
414 AA_MAY_ONEXEC, state);
416 if (!(cp.allow & AA_MAY_ONEXEC))
421 if (perms.allow & MAY_EXEC) {
422 /* exec permission determine how to transition */
423 new_profile = x_to_profile(profile, name, perms.xindex);
425 if (perms.xindex & AA_X_INHERIT) {
426 /* (p|c|n)ix - don't change profile but do
427 * use the newest version, which was picked
428 * up above when getting profile
430 info = "ix fallback";
431 new_profile = aa_get_profile(profile);
433 } else if (perms.xindex & AA_X_UNCONFINED) {
434 new_profile = aa_get_newest_profile(ns->unconfined);
435 info = "ux fallback";
438 info = "profile not found";
439 /* remove MAY_EXEC to audit as failure */
440 perms.allow &= ~MAY_EXEC;
443 } else if (COMPLAIN_MODE(profile)) {
444 /* no exec permission - are we in learning mode */
445 new_profile = aa_new_null_profile(profile, false, name,
449 info = "could not create null profile";
452 perms.xindex |= AA_X_UNSAFE;
458 * Policy has specified a domain transition, if no_new_privs then
461 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) {
469 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
470 /* FIXME: currently don't mediate shared state */
474 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
475 error = may_change_ptraced_domain(new_profile);
480 /* Determine if secure exec is needed.
481 * Can be at this point for the following reasons:
482 * 1. unconfined switching to confined
483 * 2. confined switching to different confinement
484 * 3. confined switching to unconfined
486 * Cases 2 and 3 are marked as requiring secure exec
487 * (unless policy specified "unsafe exec")
489 * bprm->unsafe is used to cache the AA_X_UNSAFE permission
490 * to avoid having to recompute in secureexec
492 if (!(perms.xindex & AA_X_UNSAFE)) {
493 AA_DEBUG("scrubbing environment variables for %s profile=%s\n",
494 name, new_profile->base.hname);
495 bprm->unsafe |= AA_SECURE_X_NEEDED;
498 /* when transitioning profiles clear unsafe personality bits */
499 bprm->per_clear |= PER_CLEAR_ON_SETID;
502 aa_put_profile(ctx->profile);
503 /* transfer new profile reference will be released when ctx is freed */
504 ctx->profile = new_profile;
507 /* clear out all temporary/transitional state from the context */
508 aa_clear_task_ctx_trans(ctx);
511 error = aa_audit_file(profile, &perms, GFP_KERNEL, OP_EXEC, MAY_EXEC,
513 new_profile ? new_profile->base.hname : NULL,
514 cond.uid, info, error);
517 aa_put_profile(new_profile);
518 aa_put_profile(profile);
525 * apparmor_bprm_secureexec - determine if secureexec is needed
526 * @bprm: binprm for exec (NOT NULL)
528 * Returns: %1 if secureexec is needed else %0
530 int apparmor_bprm_secureexec(struct linux_binprm *bprm)
532 /* the decision to use secure exec is computed in set_creds
533 * and stored in bprm->unsafe.
535 if (bprm->unsafe & AA_SECURE_X_NEEDED)
542 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
543 * @bprm: binprm for the exec (NOT NULL)
545 void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
547 struct aa_profile *profile = __aa_current_profile();
548 struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred);
550 /* bail out if unconfined or not changing profile */
551 if ((new_ctx->profile == profile) ||
552 (unconfined(new_ctx->profile)))
555 current->pdeath_signal = 0;
557 /* reset soft limits and set hard limits for the new profile */
558 __aa_transition_rlimits(profile, new_ctx->profile);
562 * apparmor_bprm_commited_cred - do cleanup after new creds committed
563 * @bprm: binprm for the exec (NOT NULL)
565 void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
567 /* TODO: cleanup signals - ipc mediation */
572 * Functions for self directed profile change
576 * new_compound_name - create an hname with @n2 appended to @n1
577 * @n1: base of hname (NOT NULL)
578 * @n2: name to append (NOT NULL)
580 * Returns: new name or NULL on error
582 static char *new_compound_name(const char *n1, const char *n2)
584 char *name = kmalloc(strlen(n1) + strlen(n2) + 3, GFP_KERNEL);
586 sprintf(name, "%s//%s", n1, n2);
591 * aa_change_hat - change hat to/from subprofile
592 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
593 * @count: number of hat names in @hats
594 * @token: magic value to validate the hat change
595 * @permtest: true if this is just a permission test
597 * Change to the first profile specified in @hats that exists, and store
598 * the @hat_magic in the current task context. If the count == 0 and the
599 * @token matches that stored in the current task context, return to the
602 * Returns %0 on success, error otherwise.
604 int aa_change_hat(const char *hats[], int count, u64 token, bool permtest)
606 const struct cred *cred;
607 struct aa_task_ctx *ctx;
608 struct aa_profile *profile, *previous_profile, *hat = NULL;
611 struct file_perms perms = {};
612 const char *target = NULL, *info = NULL;
616 * Fail explicitly requested domain transitions if no_new_privs.
617 * There is no exception for unconfined as change_hat is not
620 if (task_no_new_privs(current))
624 cred = get_current_cred();
625 ctx = cred_ctx(cred);
626 profile = aa_get_newest_profile(aa_cred_profile(cred));
627 previous_profile = aa_get_newest_profile(ctx->previous);
629 if (unconfined(profile)) {
636 /* attempting to change into a new hat or switch to a sibling */
637 struct aa_profile *root;
638 if (PROFILE_IS_HAT(profile))
639 root = aa_get_profile_rcu(&profile->parent);
641 root = aa_get_profile(profile);
643 /* find first matching hat */
644 for (i = 0; i < count && !hat; i++)
646 hat = aa_find_child(root, hats[i]);
648 if (!COMPLAIN_MODE(root) || permtest) {
649 if (list_empty(&root->base.profiles))
653 aa_put_profile(root);
658 * In complain mode and failed to match any hats.
659 * Audit the failure is based off of the first hat
660 * supplied. This is done due how userspace
661 * interacts with change_hat.
663 * TODO: Add logging of all failed hats
667 name = new_compound_name(root->base.hname, hats[0]);
668 aa_put_profile(root);
671 hat = aa_new_null_profile(profile, true, hats[0],
674 info = "failed null profile create";
679 aa_put_profile(root);
680 target = hat->base.hname;
681 if (!PROFILE_IS_HAT(hat)) {
682 info = "target not hat";
688 error = may_change_ptraced_domain(hat);
696 error = aa_set_current_hat(hat, token);
697 if (error == -EACCES)
698 /* kill task in case of brute force attacks */
699 perms.kill = AA_MAY_CHANGEHAT;
700 else if (name && !error)
701 /* reset error for learning of new hats */
704 } else if (previous_profile) {
705 /* Return to saved profile. Kill task if restore fails
706 * to avoid brute force attacks
708 target = previous_profile->base.hname;
709 error = aa_restore_previous_profile(token);
710 perms.kill = AA_MAY_CHANGEHAT;
712 /* ignore restores when there is no saved profile */
717 error = aa_audit_file(profile, &perms, GFP_KERNEL,
718 OP_CHANGE_HAT, AA_MAY_CHANGEHAT, NULL,
719 target, GLOBAL_ROOT_UID, info, error);
724 aa_put_profile(profile);
725 aa_put_profile(previous_profile);
732 * aa_change_profile - perform a one-way profile transition
733 * @ns_name: name of the profile namespace to change to (MAYBE NULL)
734 * @hname: name of profile to change to (MAYBE NULL)
735 * @onexec: whether this transition is to take place immediately or at exec
736 * @permtest: true if this is just a permission test
738 * Change to new profile @name. Unlike with hats, there is no way
739 * to change back. If @name isn't specified the current profile name is
741 * If @onexec then the transition is delayed until
744 * Returns %0 on success, error otherwise.
746 int aa_change_profile(const char *ns_name, const char *hname, bool onexec,
749 const struct cred *cred;
750 struct aa_profile *profile, *target = NULL;
751 struct aa_ns *ns = NULL;
752 struct file_perms perms = {};
753 const char *name = NULL, *info = NULL;
757 if (!hname && !ns_name)
761 request = AA_MAY_ONEXEC;
762 op = OP_CHANGE_ONEXEC;
764 request = AA_MAY_CHANGE_PROFILE;
765 op = OP_CHANGE_PROFILE;
768 cred = get_current_cred();
769 profile = aa_cred_profile(cred);
772 * Fail explicitly requested domain transitions if no_new_privs
773 * and not unconfined.
774 * Domain transitions from unconfined are allowed even when
775 * no_new_privs is set because this aways results in a reduction
778 if (task_no_new_privs(current) && !unconfined(profile)) {
785 ns = aa_find_ns(profile->ns, ns_name);
787 /* we don't create new namespace in complain mode */
789 info = "namespace not found";
795 ns = aa_get_ns(profile->ns);
797 /* if the name was not specified, use the name of the current profile */
799 if (unconfined(profile))
800 hname = ns->unconfined->base.hname;
802 hname = profile->base.hname;
805 perms = change_profile_perms(profile, ns, hname, request,
806 profile->file.start);
807 if (!(perms.allow & request)) {
813 target = aa_lookup_profile(ns, hname);
815 info = "profile not found";
817 if (permtest || !COMPLAIN_MODE(profile))
820 target = aa_new_null_profile(profile, false, hname, GFP_KERNEL);
822 info = "failed null profile create";
828 /* check if tracing task is allowed to trace target domain */
829 error = may_change_ptraced_domain(target);
831 info = "ptrace prevents transition";
839 error = aa_set_current_onexec(target);
841 error = aa_replace_current_profile(target);
845 error = aa_audit_file(profile, &perms, GFP_KERNEL, op, request,
846 name, hname, GLOBAL_ROOT_UID, info, error);
849 aa_put_profile(target);