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"
34 * aa_free_domain_entries - free entries in a domain table
35 * @domain: the domain table to free (MAYBE NULL)
37 void aa_free_domain_entries(struct aa_domain *domain)
44 for (i = 0; i < domain->size; i++)
45 kzfree(domain->table[i]);
46 kzfree(domain->table);
52 * may_change_ptraced_domain - check if can change profile on ptraced task
53 * @task: task we want to change profile of (NOT NULL)
54 * @to_profile: profile to change to (NOT NULL)
56 * Check if the task 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 task_struct *task,
62 struct aa_profile *to_profile)
64 struct task_struct *tracer;
65 struct aa_profile *tracerp = NULL;
69 tracer = ptrace_parent(task);
72 tracerp = aa_get_task_profile(tracer);
75 if (!tracer || unconfined(tracerp))
78 error = aa_may_ptrace(tracer, tracerp, to_profile, PTRACE_MODE_ATTACH);
82 aa_put_profile(tracerp);
88 * change_profile_perms - find permissions for change_profile
89 * @profile: the current profile (NOT NULL)
90 * @ns: the namespace being switched to (NOT NULL)
91 * @name: the name of the profile to change to (NOT NULL)
92 * @request: requested perms
93 * @start: state to start matching in
95 * Returns: permission set
97 static struct file_perms change_profile_perms(struct aa_profile *profile,
98 struct aa_namespace *ns,
99 const char *name, u32 request,
102 struct file_perms perms;
103 struct path_cond cond = { };
106 if (unconfined(profile)) {
107 perms.allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
108 perms.audit = perms.quiet = perms.kill = 0;
110 } else if (!profile->file.dfa) {
112 } else if ((ns == profile->ns)) {
113 /* try matching against rules with out namespace prepended */
114 aa_str_perms(profile->file.dfa, start, name, &cond, &perms);
115 if (COMBINED_PERM_MASK(perms) & request)
119 /* try matching with namespace name and then profile */
120 state = aa_dfa_match(profile->file.dfa, start, ns->base.name);
121 state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
122 aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
128 * __attach_match_ - find an attachment match
129 * @name - to match against (NOT NULL)
130 * @head - profile list to walk (NOT NULL)
132 * Do a linear search on the profiles in the list. There is a matching
133 * preference where an exact match is preferred over a name which uses
134 * expressions to match, and matching expressions with the greatest
135 * xmatch_len are preferred.
137 * Requires: @head not be shared or have appropriate locks held
139 * Returns: profile or NULL if no match found
141 static struct aa_profile *__attach_match(const char *name,
142 struct list_head *head)
145 struct aa_profile *profile, *candidate = NULL;
147 list_for_each_entry(profile, head, base.list) {
148 if (profile->flags & PFLAG_NULL)
150 if (profile->xmatch && profile->xmatch_len > len) {
151 unsigned int state = aa_dfa_match(profile->xmatch,
153 u32 perm = dfa_user_allow(profile->xmatch, state);
154 /* any accepting state means a valid match. */
155 if (perm & MAY_EXEC) {
157 len = profile->xmatch_len;
159 } else if (!strcmp(profile->base.name, name))
160 /* exact non-re match, no more searching required */
168 * find_attach - do attachment search for unconfined processes
169 * @ns: the current namespace (NOT NULL)
170 * @list: list to search (NOT NULL)
171 * @name: the executable name to match against (NOT NULL)
173 * Returns: profile or NULL if no match found
175 static struct aa_profile *find_attach(struct aa_namespace *ns,
176 struct list_head *list, const char *name)
178 struct aa_profile *profile;
180 read_lock(&ns->lock);
181 profile = aa_get_profile(__attach_match(name, list));
182 read_unlock(&ns->lock);
188 * separate_fqname - separate the namespace and profile names
189 * @fqname: the fqname name to split (NOT NULL)
190 * @ns_name: the namespace name if it exists (NOT NULL)
192 * This is the xtable equivalent routine of aa_split_fqname. It finds the
193 * split in an xtable fqname which contains an embedded \0 instead of a :
194 * if a namespace is specified. This is done so the xtable is constant and
195 * isn't re-split on every lookup.
197 * Either the profile or namespace name may be optional but if the namespace
198 * is specified the profile name termination must be present. This results
199 * in the following possible encodings:
201 * :ns_name\0profile_name\0
204 * NOTE: the xtable fqname is pre-validated at load time in unpack_trans_table
206 * Returns: profile name if it is specified else NULL
208 static const char *separate_fqname(const char *fqname, const char **ns_name)
212 if (fqname[0] == ':') {
213 /* In this case there is guaranteed to be two \0 terminators
214 * in the string. They are verified at load time by
215 * by unpack_trans_table
217 *ns_name = fqname + 1; /* skip : */
218 name = *ns_name + strlen(*ns_name) + 1;
229 static const char *next_name(int xtype, const char *name)
235 * x_table_lookup - lookup an x transition name via transition table
236 * @profile: current profile (NOT NULL)
237 * @xindex: index into x transition table
239 * Returns: refcounted profile, or NULL on failure (MAYBE NULL)
241 static struct aa_profile *x_table_lookup(struct aa_profile *profile, u32 xindex)
243 struct aa_profile *new_profile = NULL;
244 struct aa_namespace *ns = profile->ns;
245 u32 xtype = xindex & AA_X_TYPE_MASK;
246 int index = xindex & AA_X_INDEX_MASK;
249 /* index is guaranteed to be in range, validated at load time */
250 for (name = profile->file.trans.table[index]; !new_profile && name;
251 name = next_name(xtype, name)) {
252 struct aa_namespace *new_ns;
253 const char *xname = NULL;
256 if (xindex & AA_X_CHILD) {
257 /* release by caller */
258 new_profile = aa_find_child(profile, name);
260 } else if (*name == ':') {
261 /* switching namespace */
263 xname = name = separate_fqname(name, &ns_name);
265 /* no name so use profile name */
266 xname = profile->base.hname;
267 if (*ns_name == '@') {
268 /* TODO: variable support */
272 new_ns = aa_find_namespace(ns, ns_name);
275 } else if (*name == '@') {
276 /* TODO: variable support */
279 /* basic namespace lookup */
283 /* released by caller */
284 new_profile = aa_lookup_profile(new_ns ? new_ns : ns, xname);
285 aa_put_namespace(new_ns);
288 /* released by caller */
293 * x_to_profile - get target profile for a given xindex
294 * @profile: current profile (NOT NULL)
295 * @name: name to lookup (NOT NULL)
296 * @xindex: index into x transition table
298 * find profile for a transition index
300 * Returns: refcounted profile or NULL if not found available
302 static struct aa_profile *x_to_profile(struct aa_profile *profile,
303 const char *name, u32 xindex)
305 struct aa_profile *new_profile = NULL;
306 struct aa_namespace *ns = profile->ns;
307 u32 xtype = xindex & AA_X_TYPE_MASK;
311 /* fail exec unless ix || ux fallback - handled by caller */
314 if (xindex & AA_X_CHILD)
315 /* released by caller */
316 new_profile = find_attach(ns, &profile->base.profiles,
319 /* released by caller */
320 new_profile = find_attach(ns, &ns->base.profiles,
324 /* released by caller */
325 new_profile = x_table_lookup(profile, xindex);
329 /* released by caller */
334 * apparmor_bprm_set_creds - set the new creds on the bprm struct
335 * @bprm: binprm for the exec (NOT NULL)
337 * Returns: %0 or error on failure
339 int apparmor_bprm_set_creds(struct linux_binprm *bprm)
341 struct aa_task_cxt *cxt;
342 struct aa_profile *profile, *new_profile = NULL;
343 struct aa_namespace *ns;
346 struct file_perms perms = {};
347 struct path_cond cond = {
348 file_inode(bprm->file)->i_uid,
349 file_inode(bprm->file)->i_mode
351 const char *name = NULL, *target = NULL, *info = NULL;
352 int error = cap_bprm_set_creds(bprm);
356 if (bprm->cred_prepared)
359 cxt = cred_cxt(bprm->cred);
362 profile = aa_get_profile(aa_newest_version(cxt->profile));
364 * get the namespace from the replacement profile as replacement
365 * can change the namespace
368 state = profile->file.start;
370 /* buffer freed below, name is pointer into buffer */
371 error = aa_path_name(&bprm->file->f_path, profile->path_flags, &buffer,
375 (PFLAG_IX_ON_NAME_ERROR | PFLAG_UNCONFINED))
377 name = bprm->filename;
381 /* Test for onexec first as onexec directives override other
384 if (unconfined(profile)) {
385 /* unconfined task */
387 /* change_profile on exec already been granted */
388 new_profile = aa_get_profile(cxt->onexec);
390 new_profile = find_attach(ns, &ns->base.profiles, name);
394 * NOTE: Domain transitions from unconfined are allowed
395 * even when no_new_privs is set because this aways results
396 * in a further reduction of permissions.
401 /* find exec permissions for name */
402 state = aa_str_perms(profile->file.dfa, state, name, &cond, &perms);
404 struct file_perms cp;
405 info = "change_profile onexec";
406 if (!(perms.allow & AA_MAY_ONEXEC))
409 /* test if this exec can be paired with change_profile onexec.
410 * onexec permission is linked to exec with a standard pairing
411 * exec\0change_profile
413 state = aa_dfa_null_transition(profile->file.dfa, state);
414 cp = change_profile_perms(profile, cxt->onexec->ns,
415 cxt->onexec->base.name,
416 AA_MAY_ONEXEC, state);
418 if (!(cp.allow & AA_MAY_ONEXEC))
420 new_profile = aa_get_profile(aa_newest_version(cxt->onexec));
424 if (perms.allow & MAY_EXEC) {
425 /* exec permission determine how to transition */
426 new_profile = x_to_profile(profile, name, perms.xindex);
428 if (perms.xindex & AA_X_INHERIT) {
429 /* (p|c|n)ix - don't change profile but do
430 * use the newest version, which was picked
431 * up above when getting profile
433 info = "ix fallback";
434 new_profile = aa_get_profile(profile);
436 } else if (perms.xindex & AA_X_UNCONFINED) {
437 new_profile = aa_get_profile(ns->unconfined);
438 info = "ux fallback";
441 info = "profile not found";
442 /* remove MAY_EXEC to audit as failure */
443 perms.allow &= ~MAY_EXEC;
446 } else if (COMPLAIN_MODE(profile)) {
447 /* no exec permission - are we in learning mode */
448 new_profile = aa_new_null_profile(profile, 0);
451 info = "could not create null profile";
454 target = new_profile->base.hname;
456 perms.xindex |= AA_X_UNSAFE;
462 * Policy has specified a domain transition, if no_new_privs then
465 if (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) {
466 aa_put_profile(new_profile);
474 if (bprm->unsafe & LSM_UNSAFE_SHARE) {
475 /* FIXME: currently don't mediate shared state */
479 if (bprm->unsafe & (LSM_UNSAFE_PTRACE | LSM_UNSAFE_PTRACE_CAP)) {
480 error = may_change_ptraced_domain(current, new_profile);
482 aa_put_profile(new_profile);
487 /* Determine if secure exec is needed.
488 * Can be at this point for the following reasons:
489 * 1. unconfined switching to confined
490 * 2. confined switching to different confinement
491 * 3. confined switching to unconfined
493 * Cases 2 and 3 are marked as requiring secure exec
494 * (unless policy specified "unsafe exec")
496 * bprm->unsafe is used to cache the AA_X_UNSAFE permission
497 * to avoid having to recompute in secureexec
499 if (!(perms.xindex & AA_X_UNSAFE)) {
500 AA_DEBUG("scrubbing environment variables for %s profile=%s\n",
501 name, new_profile->base.hname);
502 bprm->unsafe |= AA_SECURE_X_NEEDED;
505 target = new_profile->base.hname;
506 /* when transitioning profiles clear unsafe personality bits */
507 bprm->per_clear |= PER_CLEAR_ON_SETID;
510 aa_put_profile(cxt->profile);
511 /* transfer new profile reference will be released when cxt is freed */
512 cxt->profile = new_profile;
514 /* clear out all temporary/transitional state from the context */
515 aa_clear_task_cxt_trans(cxt);
518 error = aa_audit_file(profile, &perms, GFP_KERNEL, OP_EXEC, MAY_EXEC,
519 name, target, cond.uid, info, error);
522 aa_put_profile(profile);
529 * apparmor_bprm_secureexec - determine if secureexec is needed
530 * @bprm: binprm for exec (NOT NULL)
532 * Returns: %1 if secureexec is needed else %0
534 int apparmor_bprm_secureexec(struct linux_binprm *bprm)
536 int ret = cap_bprm_secureexec(bprm);
538 /* the decision to use secure exec is computed in set_creds
539 * and stored in bprm->unsafe.
541 if (!ret && (bprm->unsafe & AA_SECURE_X_NEEDED))
548 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
549 * @bprm: binprm for the exec (NOT NULL)
551 void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
553 struct aa_profile *profile = __aa_current_profile();
554 struct aa_task_cxt *new_cxt = cred_cxt(bprm->cred);
556 /* bail out if unconfined or not changing profile */
557 if ((new_cxt->profile == profile) ||
558 (unconfined(new_cxt->profile)))
561 current->pdeath_signal = 0;
563 /* reset soft limits and set hard limits for the new profile */
564 __aa_transition_rlimits(profile, new_cxt->profile);
568 * apparmor_bprm_commited_cred - do cleanup after new creds committed
569 * @bprm: binprm for the exec (NOT NULL)
571 void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
573 /* TODO: cleanup signals - ipc mediation */
578 * Functions for self directed profile change
582 * new_compound_name - create an hname with @n2 appended to @n1
583 * @n1: base of hname (NOT NULL)
584 * @n2: name to append (NOT NULL)
586 * Returns: new name or NULL on error
588 static char *new_compound_name(const char *n1, const char *n2)
590 char *name = kmalloc(strlen(n1) + strlen(n2) + 3, GFP_KERNEL);
592 sprintf(name, "%s//%s", n1, n2);
597 * aa_change_hat - change hat to/from subprofile
598 * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
599 * @count: number of hat names in @hats
600 * @token: magic value to validate the hat change
601 * @permtest: true if this is just a permission test
603 * Change to the first profile specified in @hats that exists, and store
604 * the @hat_magic in the current task context. If the count == 0 and the
605 * @token matches that stored in the current task context, return to the
608 * Returns %0 on success, error otherwise.
610 int aa_change_hat(const char *hats[], int count, u64 token, bool permtest)
612 const struct cred *cred;
613 struct aa_task_cxt *cxt;
614 struct aa_profile *profile, *previous_profile, *hat = NULL;
617 struct file_perms perms = {};
618 const char *target = NULL, *info = NULL;
622 * Fail explicitly requested domain transitions if no_new_privs.
623 * There is no exception for unconfined as change_hat is not
626 if (current->no_new_privs)
630 cred = get_current_cred();
631 cxt = cred_cxt(cred);
632 profile = aa_cred_profile(cred);
633 previous_profile = cxt->previous;
635 if (unconfined(profile)) {
642 /* attempting to change into a new hat or switch to a sibling */
643 struct aa_profile *root;
644 root = PROFILE_IS_HAT(profile) ? profile->parent : profile;
646 /* find first matching hat */
647 for (i = 0; i < count && !hat; i++)
649 hat = aa_find_child(root, hats[i]);
651 if (!COMPLAIN_MODE(root) || permtest) {
652 if (list_empty(&root->base.profiles))
660 * In complain mode and failed to match any hats.
661 * Audit the failure is based off of the first hat
662 * supplied. This is done due how userspace
663 * interacts with change_hat.
665 * TODO: Add logging of all failed hats
669 name = new_compound_name(root->base.hname, hats[0]);
672 hat = aa_new_null_profile(profile, 1);
674 info = "failed null profile create";
679 target = hat->base.hname;
680 if (!PROFILE_IS_HAT(hat)) {
681 info = "target not hat";
687 error = may_change_ptraced_domain(current, 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, GFP_KERNEL,
717 OP_CHANGE_HAT, AA_MAY_CHANGEHAT, NULL,
718 target, GLOBAL_ROOT_UID, info, error);
729 * aa_change_profile - perform a one-way profile transition
730 * @ns_name: name of the profile namespace to change to (MAYBE NULL)
731 * @hname: name of profile to change to (MAYBE NULL)
732 * @onexec: whether this transition is to take place immediately or at exec
733 * @permtest: true if this is just a permission test
735 * Change to new profile @name. Unlike with hats, there is no way
736 * to change back. If @name isn't specified the current profile name is
738 * If @onexec then the transition is delayed until
741 * Returns %0 on success, error otherwise.
743 int aa_change_profile(const char *ns_name, const char *hname, bool onexec,
746 const struct cred *cred;
747 struct aa_profile *profile, *target = NULL;
748 struct aa_namespace *ns = NULL;
749 struct file_perms perms = {};
750 const char *name = NULL, *info = NULL;
754 if (!hname && !ns_name)
758 request = AA_MAY_ONEXEC;
759 op = OP_CHANGE_ONEXEC;
761 request = AA_MAY_CHANGE_PROFILE;
762 op = OP_CHANGE_PROFILE;
765 cred = get_current_cred();
766 profile = aa_cred_profile(cred);
769 * Fail explicitly requested domain transitions if no_new_privs
770 * and not unconfined.
771 * Domain transitions from unconfined are allowed even when
772 * no_new_privs is set because this aways results in a reduction
775 if (current->no_new_privs && !unconfined(profile)) {
782 ns = aa_find_namespace(profile->ns, ns_name);
784 /* we don't create new namespace in complain mode */
786 info = "namespace not found";
792 ns = aa_get_namespace(profile->ns);
794 /* if the name was not specified, use the name of the current profile */
796 if (unconfined(profile))
797 hname = ns->unconfined->base.hname;
799 hname = profile->base.hname;
802 perms = change_profile_perms(profile, ns, hname, request,
803 profile->file.start);
804 if (!(perms.allow & request)) {
810 target = aa_lookup_profile(ns, hname);
812 info = "profile not found";
814 if (permtest || !COMPLAIN_MODE(profile))
817 target = aa_new_null_profile(profile, 0);
819 info = "failed null profile create";
825 /* check if tracing task is allowed to trace target domain */
826 error = may_change_ptraced_domain(current, target);
828 info = "ptrace prevents transition";
836 error = aa_set_current_onexec(target);
838 error = aa_replace_current_profile(target);
842 error = aa_audit_file(profile, &perms, GFP_KERNEL, op, request,
843 name, hname, GLOBAL_ROOT_UID, info, error);
845 aa_put_namespace(ns);
846 aa_put_profile(target);