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, OP_EXEC, MAY_EXEC, name,
512 new_profile ? new_profile->base.hname : NULL,
513 cond.uid, info, error);
516 aa_put_profile(new_profile);
517 aa_put_profile(profile);
524 * apparmor_bprm_secureexec - determine if secureexec is needed
525 * @bprm: binprm for exec (NOT NULL)
527 * Returns: %1 if secureexec is needed else %0
529 int apparmor_bprm_secureexec(struct linux_binprm *bprm)
531 /* the decision to use secure exec is computed in set_creds
532 * and stored in bprm->unsafe.
534 if (bprm->unsafe & AA_SECURE_X_NEEDED)
541 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
542 * @bprm: binprm for the exec (NOT NULL)
544 void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
546 struct aa_profile *profile = __aa_current_profile();
547 struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred);
549 /* bail out if unconfined or not changing profile */
550 if ((new_ctx->profile == profile) ||
551 (unconfined(new_ctx->profile)))
554 current->pdeath_signal = 0;
556 /* reset soft limits and set hard limits for the new profile */
557 __aa_transition_rlimits(profile, new_ctx->profile);
561 * apparmor_bprm_commited_cred - do cleanup after new creds committed
562 * @bprm: binprm for the exec (NOT NULL)
564 void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
566 /* TODO: cleanup signals - ipc mediation */
571 * Functions for self directed profile change
575 * new_compound_name - create an hname with @n2 appended to @n1
576 * @n1: base of hname (NOT NULL)
577 * @n2: name to append (NOT NULL)
579 * Returns: new name or NULL on error
581 static char *new_compound_name(const char *n1, const char *n2)
583 char *name = kmalloc(strlen(n1) + strlen(n2) + 3, GFP_KERNEL);
585 sprintf(name, "%s//%s", n1, n2);
590 * aa_change_hat - change hat to/from subprofile
591 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
592 * @count: number of hat names in @hats
593 * @token: magic value to validate the hat change
594 * @permtest: true if this is just a permission test
596 * Change to the first profile specified in @hats that exists, and store
597 * the @hat_magic in the current task context. If the count == 0 and the
598 * @token matches that stored in the current task context, return to the
601 * Returns %0 on success, error otherwise.
603 int aa_change_hat(const char *hats[], int count, u64 token, bool permtest)
605 const struct cred *cred;
606 struct aa_task_ctx *ctx;
607 struct aa_profile *profile, *previous_profile, *hat = NULL;
610 struct file_perms perms = {};
611 const char *target = NULL, *info = NULL;
615 * Fail explicitly requested domain transitions if no_new_privs.
616 * There is no exception for unconfined as change_hat is not
619 if (task_no_new_privs(current))
623 cred = get_current_cred();
624 ctx = cred_ctx(cred);
625 profile = aa_get_newest_profile(aa_cred_profile(cred));
626 previous_profile = aa_get_newest_profile(ctx->previous);
628 if (unconfined(profile)) {
635 /* attempting to change into a new hat or switch to a sibling */
636 struct aa_profile *root;
637 if (PROFILE_IS_HAT(profile))
638 root = aa_get_profile_rcu(&profile->parent);
640 root = aa_get_profile(profile);
642 /* find first matching hat */
643 for (i = 0; i < count && !hat; i++)
645 hat = aa_find_child(root, hats[i]);
647 if (!COMPLAIN_MODE(root) || permtest) {
648 if (list_empty(&root->base.profiles))
652 aa_put_profile(root);
657 * In complain mode and failed to match any hats.
658 * Audit the failure is based off of the first hat
659 * supplied. This is done due how userspace
660 * interacts with change_hat.
662 * TODO: Add logging of all failed hats
666 name = new_compound_name(root->base.hname, hats[0]);
667 aa_put_profile(root);
670 hat = aa_new_null_profile(profile, true, hats[0],
673 info = "failed null profile create";
678 aa_put_profile(root);
679 target = hat->base.hname;
680 if (!PROFILE_IS_HAT(hat)) {
681 info = "target not hat";
687 error = may_change_ptraced_domain(hat);
695 error = aa_set_current_hat(hat, token);
696 if (error == -EACCES)
697 /* kill task in case of brute force attacks */
698 perms.kill = AA_MAY_CHANGEHAT;
699 else if (name && !error)
700 /* reset error for learning of new hats */
703 } else if (previous_profile) {
704 /* Return to saved profile. Kill task if restore fails
705 * to avoid brute force attacks
707 target = previous_profile->base.hname;
708 error = aa_restore_previous_profile(token);
709 perms.kill = AA_MAY_CHANGEHAT;
711 /* ignore restores when there is no saved profile */
716 error = aa_audit_file(profile, &perms, OP_CHANGE_HAT,
717 AA_MAY_CHANGEHAT, NULL, target,
718 GLOBAL_ROOT_UID, info, error);
723 aa_put_profile(profile);
724 aa_put_profile(previous_profile);
731 * aa_change_profile - perform a one-way profile transition
732 * @fqname: name of profile may include namespace (NOT NULL)
733 * @onexec: whether this transition is to take place immediately or at exec
734 * @permtest: true if this is just a permission test
736 * Change to new profile @name. Unlike with hats, there is no way
737 * to change back. If @name isn't specified the current profile name is
739 * If @onexec then the transition is delayed until
742 * Returns %0 on success, error otherwise.
744 int aa_change_profile(const char *fqname, bool onexec,
745 bool permtest, bool stack)
747 const struct cred *cred;
748 struct aa_profile *profile, *target = NULL;
749 struct file_perms perms = {};
750 const char *info = NULL, *op;
754 if (!fqname || !*fqname) {
755 AA_DEBUG("no profile name");
760 request = AA_MAY_ONEXEC;
761 op = OP_CHANGE_ONEXEC;
763 request = AA_MAY_CHANGE_PROFILE;
764 op = OP_CHANGE_PROFILE;
767 cred = get_current_cred();
768 profile = aa_cred_profile(cred);
771 * Fail explicitly requested domain transitions if no_new_privs
772 * and not unconfined.
773 * Domain transitions from unconfined are allowed even when
774 * no_new_privs is set because this aways results in a reduction
777 if (task_no_new_privs(current) && !unconfined(profile)) {
782 target = aa_fqlookupn_profile(profile, fqname, strlen(fqname));
784 info = "profile not found";
786 if (permtest || !COMPLAIN_MODE(profile))
789 target = aa_new_null_profile(profile, false, fqname,
792 info = "failed null profile create";
798 perms = change_profile_perms(profile, target->ns, target->base.hname,
799 request, profile->file.start);
800 if (!(perms.allow & request)) {
805 /* check if tracing task is allowed to trace target domain */
806 error = may_change_ptraced_domain(target);
808 info = "ptrace prevents transition";
816 error = aa_set_current_onexec(target);
818 error = aa_replace_current_profile(target);
822 error = aa_audit_file(profile, &perms, op, request, NULL,
823 fqname, GLOBAL_ROOT_UID, info, error);
825 aa_put_profile(target);