xref: /linux/security/apparmor/domain.c (revision 9fcf78cca198600b27c44b4e50f00f8af3927f17)
1898127c3SJohn Johansen /*
2898127c3SJohn Johansen  * AppArmor security module
3898127c3SJohn Johansen  *
4898127c3SJohn Johansen  * This file contains AppArmor policy attachment and domain transitions
5898127c3SJohn Johansen  *
6898127c3SJohn Johansen  * Copyright (C) 2002-2008 Novell/SUSE
7898127c3SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
8898127c3SJohn Johansen  *
9898127c3SJohn Johansen  * This program is free software; you can redistribute it and/or
10898127c3SJohn Johansen  * modify it under the terms of the GNU General Public License as
11898127c3SJohn Johansen  * published by the Free Software Foundation, version 2 of the
12898127c3SJohn Johansen  * License.
13898127c3SJohn Johansen  */
14898127c3SJohn Johansen 
15898127c3SJohn Johansen #include <linux/errno.h>
16898127c3SJohn Johansen #include <linux/fdtable.h>
17898127c3SJohn Johansen #include <linux/file.h>
18898127c3SJohn Johansen #include <linux/mount.h>
19898127c3SJohn Johansen #include <linux/syscalls.h>
20898127c3SJohn Johansen #include <linux/tracehook.h>
21898127c3SJohn Johansen #include <linux/personality.h>
22898127c3SJohn Johansen 
23898127c3SJohn Johansen #include "include/audit.h"
24898127c3SJohn Johansen #include "include/apparmorfs.h"
25d8889d49SJohn Johansen #include "include/cred.h"
26898127c3SJohn Johansen #include "include/domain.h"
27898127c3SJohn Johansen #include "include/file.h"
28898127c3SJohn Johansen #include "include/ipc.h"
29898127c3SJohn Johansen #include "include/match.h"
30898127c3SJohn Johansen #include "include/path.h"
31898127c3SJohn Johansen #include "include/policy.h"
32cff281f6SJohn Johansen #include "include/policy_ns.h"
33898127c3SJohn Johansen 
34898127c3SJohn Johansen /**
35898127c3SJohn Johansen  * aa_free_domain_entries - free entries in a domain table
36898127c3SJohn Johansen  * @domain: the domain table to free  (MAYBE NULL)
37898127c3SJohn Johansen  */
38898127c3SJohn Johansen void aa_free_domain_entries(struct aa_domain *domain)
39898127c3SJohn Johansen {
40898127c3SJohn Johansen 	int i;
41898127c3SJohn Johansen 	if (domain) {
42898127c3SJohn Johansen 		if (!domain->table)
43898127c3SJohn Johansen 			return;
44898127c3SJohn Johansen 
45898127c3SJohn Johansen 		for (i = 0; i < domain->size; i++)
46898127c3SJohn Johansen 			kzfree(domain->table[i]);
47898127c3SJohn Johansen 		kzfree(domain->table);
48898127c3SJohn Johansen 		domain->table = NULL;
49898127c3SJohn Johansen 	}
50898127c3SJohn Johansen }
51898127c3SJohn Johansen 
52898127c3SJohn Johansen /**
53898127c3SJohn Johansen  * may_change_ptraced_domain - check if can change profile on ptraced task
54b2d09ae4SJohn Johansen  * @to_label: profile to change to  (NOT NULL)
55b2d09ae4SJohn Johansen  * @info: message if there is an error
56898127c3SJohn Johansen  *
5751775fe7SOleg Nesterov  * Check if current is ptraced and if so if the tracing task is allowed
58898127c3SJohn Johansen  * to trace the new domain
59898127c3SJohn Johansen  *
60898127c3SJohn Johansen  * Returns: %0 or error if change not allowed
61898127c3SJohn Johansen  */
62b2d09ae4SJohn Johansen static int may_change_ptraced_domain(struct aa_label *to_label,
63b2d09ae4SJohn Johansen 				     const char **info)
64898127c3SJohn Johansen {
65898127c3SJohn Johansen 	struct task_struct *tracer;
66637f688dSJohn Johansen 	struct aa_label *tracerl = NULL;
67898127c3SJohn Johansen 	int error = 0;
68898127c3SJohn Johansen 
69898127c3SJohn Johansen 	rcu_read_lock();
7051775fe7SOleg Nesterov 	tracer = ptrace_parent(current);
713cfcc19eSJohn Johansen 	if (tracer)
72898127c3SJohn Johansen 		/* released below */
73637f688dSJohn Johansen 		tracerl = aa_get_task_label(tracer);
74898127c3SJohn Johansen 
75898127c3SJohn Johansen 	/* not ptraced */
76637f688dSJohn Johansen 	if (!tracer || unconfined(tracerl))
77898127c3SJohn Johansen 		goto out;
78898127c3SJohn Johansen 
79b2d09ae4SJohn Johansen 	error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
80898127c3SJohn Johansen 
81898127c3SJohn Johansen out:
8204fdc099SJohn Johansen 	rcu_read_unlock();
83637f688dSJohn Johansen 	aa_put_label(tracerl);
84898127c3SJohn Johansen 
85b2d09ae4SJohn Johansen 	if (error)
86b2d09ae4SJohn Johansen 		*info = "ptrace prevents transition";
87898127c3SJohn Johansen 	return error;
88898127c3SJohn Johansen }
89898127c3SJohn Johansen 
9093c98a48SJohn Johansen /**** TODO: dedup to aa_label_match - needs perm and dfa, merging
9193c98a48SJohn Johansen  * specifically this is an exact copy of aa_label_match except
9293c98a48SJohn Johansen  * aa_compute_perms is replaced with aa_compute_fperms
9393c98a48SJohn Johansen  * and policy.dfa with file.dfa
9493c98a48SJohn Johansen  ****/
9593c98a48SJohn Johansen /* match a profile and its associated ns component if needed
9693c98a48SJohn Johansen  * Assumes visibility test has already been done.
9793c98a48SJohn Johansen  * If a subns profile is not to be matched should be prescreened with
9893c98a48SJohn Johansen  * visibility test.
9993c98a48SJohn Johansen  */
10093c98a48SJohn Johansen static inline unsigned int match_component(struct aa_profile *profile,
10193c98a48SJohn Johansen 					   struct aa_profile *tp,
10293c98a48SJohn Johansen 					   bool stack, unsigned int state)
10393c98a48SJohn Johansen {
10493c98a48SJohn Johansen 	const char *ns_name;
10593c98a48SJohn Johansen 
10693c98a48SJohn Johansen 	if (stack)
10793c98a48SJohn Johansen 		state = aa_dfa_match(profile->file.dfa, state, "&");
10893c98a48SJohn Johansen 	if (profile->ns == tp->ns)
10993c98a48SJohn Johansen 		return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
11093c98a48SJohn Johansen 
11193c98a48SJohn Johansen 	/* try matching with namespace name and then profile */
11293c98a48SJohn Johansen 	ns_name = aa_ns_name(profile->ns, tp->ns, true);
11393c98a48SJohn Johansen 	state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
11493c98a48SJohn Johansen 	state = aa_dfa_match(profile->file.dfa, state, ns_name);
11593c98a48SJohn Johansen 	state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
11693c98a48SJohn Johansen 	return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
11793c98a48SJohn Johansen }
11893c98a48SJohn Johansen 
11993c98a48SJohn Johansen /**
12093c98a48SJohn Johansen  * label_compound_match - find perms for full compound label
12193c98a48SJohn Johansen  * @profile: profile to find perms for
12293c98a48SJohn Johansen  * @label: label to check access permissions for
12393c98a48SJohn Johansen  * @stack: whether this is a stacking request
12493c98a48SJohn Johansen  * @start: state to start match in
12593c98a48SJohn Johansen  * @subns: whether to do permission checks on components in a subns
12693c98a48SJohn Johansen  * @request: permissions to request
12793c98a48SJohn Johansen  * @perms: perms struct to set
12893c98a48SJohn Johansen  *
12993c98a48SJohn Johansen  * Returns: 0 on success else ERROR
13093c98a48SJohn Johansen  *
13193c98a48SJohn Johansen  * For the label A//&B//&C this does the perm match for A//&B//&C
13293c98a48SJohn Johansen  * @perms should be preinitialized with allperms OR a previous permission
13393c98a48SJohn Johansen  *        check to be stacked.
13493c98a48SJohn Johansen  */
13593c98a48SJohn Johansen static int label_compound_match(struct aa_profile *profile,
13693c98a48SJohn Johansen 				struct aa_label *label, bool stack,
13793c98a48SJohn Johansen 				unsigned int state, bool subns, u32 request,
13893c98a48SJohn Johansen 				struct aa_perms *perms)
13993c98a48SJohn Johansen {
14093c98a48SJohn Johansen 	struct aa_profile *tp;
14193c98a48SJohn Johansen 	struct label_it i;
14293c98a48SJohn Johansen 	struct path_cond cond = { };
14393c98a48SJohn Johansen 
14493c98a48SJohn Johansen 	/* find first subcomponent that is visible */
14593c98a48SJohn Johansen 	label_for_each(i, label, tp) {
14693c98a48SJohn Johansen 		if (!aa_ns_visible(profile->ns, tp->ns, subns))
14793c98a48SJohn Johansen 			continue;
14893c98a48SJohn Johansen 		state = match_component(profile, tp, stack, state);
14993c98a48SJohn Johansen 		if (!state)
15093c98a48SJohn Johansen 			goto fail;
15193c98a48SJohn Johansen 		goto next;
15293c98a48SJohn Johansen 	}
15393c98a48SJohn Johansen 
15493c98a48SJohn Johansen 	/* no component visible */
15593c98a48SJohn Johansen 	*perms = allperms;
15693c98a48SJohn Johansen 	return 0;
15793c98a48SJohn Johansen 
15893c98a48SJohn Johansen next:
15993c98a48SJohn Johansen 	label_for_each_cont(i, label, tp) {
16093c98a48SJohn Johansen 		if (!aa_ns_visible(profile->ns, tp->ns, subns))
16193c98a48SJohn Johansen 			continue;
16293c98a48SJohn Johansen 		state = aa_dfa_match(profile->file.dfa, state, "//&");
16393c98a48SJohn Johansen 		state = match_component(profile, tp, false, state);
16493c98a48SJohn Johansen 		if (!state)
16593c98a48SJohn Johansen 			goto fail;
16693c98a48SJohn Johansen 	}
16793c98a48SJohn Johansen 	*perms = aa_compute_fperms(profile->file.dfa, state, &cond);
16893c98a48SJohn Johansen 	aa_apply_modes_to_perms(profile, perms);
16993c98a48SJohn Johansen 	if ((perms->allow & request) != request)
17093c98a48SJohn Johansen 		return -EACCES;
17193c98a48SJohn Johansen 
17293c98a48SJohn Johansen 	return 0;
17393c98a48SJohn Johansen 
17493c98a48SJohn Johansen fail:
17593c98a48SJohn Johansen 	*perms = nullperms;
17693c98a48SJohn Johansen 	return -EACCES;
17793c98a48SJohn Johansen }
17893c98a48SJohn Johansen 
17993c98a48SJohn Johansen /**
18093c98a48SJohn Johansen  * label_components_match - find perms for all subcomponents of a label
18193c98a48SJohn Johansen  * @profile: profile to find perms for
18293c98a48SJohn Johansen  * @label: label to check access permissions for
18393c98a48SJohn Johansen  * @stack: whether this is a stacking request
18493c98a48SJohn Johansen  * @start: state to start match in
18593c98a48SJohn Johansen  * @subns: whether to do permission checks on components in a subns
18693c98a48SJohn Johansen  * @request: permissions to request
18793c98a48SJohn Johansen  * @perms: an initialized perms struct to add accumulation to
18893c98a48SJohn Johansen  *
18993c98a48SJohn Johansen  * Returns: 0 on success else ERROR
19093c98a48SJohn Johansen  *
19193c98a48SJohn Johansen  * For the label A//&B//&C this does the perm match for each of A and B and C
19293c98a48SJohn Johansen  * @perms should be preinitialized with allperms OR a previous permission
19393c98a48SJohn Johansen  *        check to be stacked.
19493c98a48SJohn Johansen  */
19593c98a48SJohn Johansen static int label_components_match(struct aa_profile *profile,
19693c98a48SJohn Johansen 				  struct aa_label *label, bool stack,
19793c98a48SJohn Johansen 				  unsigned int start, bool subns, u32 request,
19893c98a48SJohn Johansen 				  struct aa_perms *perms)
19993c98a48SJohn Johansen {
20093c98a48SJohn Johansen 	struct aa_profile *tp;
20193c98a48SJohn Johansen 	struct label_it i;
20293c98a48SJohn Johansen 	struct aa_perms tmp;
20393c98a48SJohn Johansen 	struct path_cond cond = { };
20493c98a48SJohn Johansen 	unsigned int state = 0;
20593c98a48SJohn Johansen 
20693c98a48SJohn Johansen 	/* find first subcomponent to test */
20793c98a48SJohn Johansen 	label_for_each(i, label, tp) {
20893c98a48SJohn Johansen 		if (!aa_ns_visible(profile->ns, tp->ns, subns))
20993c98a48SJohn Johansen 			continue;
21093c98a48SJohn Johansen 		state = match_component(profile, tp, stack, start);
21193c98a48SJohn Johansen 		if (!state)
21293c98a48SJohn Johansen 			goto fail;
21393c98a48SJohn Johansen 		goto next;
21493c98a48SJohn Johansen 	}
21593c98a48SJohn Johansen 
21693c98a48SJohn Johansen 	/* no subcomponents visible - no change in perms */
21793c98a48SJohn Johansen 	return 0;
21893c98a48SJohn Johansen 
21993c98a48SJohn Johansen next:
22093c98a48SJohn Johansen 	tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
22193c98a48SJohn Johansen 	aa_apply_modes_to_perms(profile, &tmp);
22293c98a48SJohn Johansen 	aa_perms_accum(perms, &tmp);
22393c98a48SJohn Johansen 	label_for_each_cont(i, label, tp) {
22493c98a48SJohn Johansen 		if (!aa_ns_visible(profile->ns, tp->ns, subns))
22593c98a48SJohn Johansen 			continue;
22693c98a48SJohn Johansen 		state = match_component(profile, tp, stack, start);
22793c98a48SJohn Johansen 		if (!state)
22893c98a48SJohn Johansen 			goto fail;
22993c98a48SJohn Johansen 		tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
23093c98a48SJohn Johansen 		aa_apply_modes_to_perms(profile, &tmp);
23193c98a48SJohn Johansen 		aa_perms_accum(perms, &tmp);
23293c98a48SJohn Johansen 	}
23393c98a48SJohn Johansen 
23493c98a48SJohn Johansen 	if ((perms->allow & request) != request)
23593c98a48SJohn Johansen 		return -EACCES;
23693c98a48SJohn Johansen 
23793c98a48SJohn Johansen 	return 0;
23893c98a48SJohn Johansen 
23993c98a48SJohn Johansen fail:
24093c98a48SJohn Johansen 	*perms = nullperms;
24193c98a48SJohn Johansen 	return -EACCES;
24293c98a48SJohn Johansen }
24393c98a48SJohn Johansen 
24493c98a48SJohn Johansen /**
24593c98a48SJohn Johansen  * label_match - do a multi-component label match
24693c98a48SJohn Johansen  * @profile: profile to match against (NOT NULL)
24793c98a48SJohn Johansen  * @label: label to match (NOT NULL)
24893c98a48SJohn Johansen  * @stack: whether this is a stacking request
24993c98a48SJohn Johansen  * @state: state to start in
25093c98a48SJohn Johansen  * @subns: whether to match subns components
25193c98a48SJohn Johansen  * @request: permission request
25293c98a48SJohn Johansen  * @perms: Returns computed perms (NOT NULL)
25393c98a48SJohn Johansen  *
25493c98a48SJohn Johansen  * Returns: the state the match finished in, may be the none matching state
25593c98a48SJohn Johansen  */
25693c98a48SJohn Johansen static int label_match(struct aa_profile *profile, struct aa_label *label,
25793c98a48SJohn Johansen 		       bool stack, unsigned int state, bool subns, u32 request,
25893c98a48SJohn Johansen 		       struct aa_perms *perms)
25993c98a48SJohn Johansen {
26093c98a48SJohn Johansen 	int error;
26193c98a48SJohn Johansen 
26293c98a48SJohn Johansen 	*perms = nullperms;
26393c98a48SJohn Johansen 	error = label_compound_match(profile, label, stack, state, subns,
26493c98a48SJohn Johansen 				     request, perms);
26593c98a48SJohn Johansen 	if (!error)
26693c98a48SJohn Johansen 		return error;
26793c98a48SJohn Johansen 
26893c98a48SJohn Johansen 	*perms = allperms;
26993c98a48SJohn Johansen 	return label_components_match(profile, label, stack, state, subns,
27093c98a48SJohn Johansen 				      request, perms);
27193c98a48SJohn Johansen }
27293c98a48SJohn Johansen 
27393c98a48SJohn Johansen /******* end TODO: dedup *****/
27493c98a48SJohn Johansen 
275898127c3SJohn Johansen /**
276898127c3SJohn Johansen  * change_profile_perms - find permissions for change_profile
277898127c3SJohn Johansen  * @profile: the current profile  (NOT NULL)
27893c98a48SJohn Johansen  * @target: label to transition to (NOT NULL)
27993c98a48SJohn Johansen  * @stack: whether this is a stacking request
280898127c3SJohn Johansen  * @request: requested perms
281898127c3SJohn Johansen  * @start: state to start matching in
282898127c3SJohn Johansen  *
28393c98a48SJohn Johansen  *
284898127c3SJohn Johansen  * Returns: permission set
28593c98a48SJohn Johansen  *
28693c98a48SJohn Johansen  * currently only matches full label A//&B//&C or individual components A, B, C
28793c98a48SJohn Johansen  * not arbitrary combinations. Eg. A//&B, C
288898127c3SJohn Johansen  */
28993c98a48SJohn Johansen static int change_profile_perms(struct aa_profile *profile,
29093c98a48SJohn Johansen 				struct aa_label *target, bool stack,
29193c98a48SJohn Johansen 				u32 request, unsigned int start,
29293c98a48SJohn Johansen 				struct aa_perms *perms)
29393c98a48SJohn Johansen {
29493c98a48SJohn Johansen 	if (profile_unconfined(profile)) {
29593c98a48SJohn Johansen 		perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
29693c98a48SJohn Johansen 		perms->audit = perms->quiet = perms->kill = 0;
29793c98a48SJohn Johansen 		return 0;
29893c98a48SJohn Johansen 	}
29993c98a48SJohn Johansen 
30093c98a48SJohn Johansen 	/* TODO: add profile in ns screening */
30193c98a48SJohn Johansen 	return label_match(profile, target, stack, start, true, request, perms);
30293c98a48SJohn Johansen }
30393c98a48SJohn Johansen 
304898127c3SJohn Johansen /**
305898127c3SJohn Johansen  * __attach_match_ - find an attachment match
306898127c3SJohn Johansen  * @name - to match against  (NOT NULL)
307898127c3SJohn Johansen  * @head - profile list to walk  (NOT NULL)
308844b8292SJohn Johansen  * @info - info message if there was an error (NOT NULL)
309898127c3SJohn Johansen  *
310898127c3SJohn Johansen  * Do a linear search on the profiles in the list.  There is a matching
311898127c3SJohn Johansen  * preference where an exact match is preferred over a name which uses
312898127c3SJohn Johansen  * expressions to match, and matching expressions with the greatest
313898127c3SJohn Johansen  * xmatch_len are preferred.
314898127c3SJohn Johansen  *
315898127c3SJohn Johansen  * Requires: @head not be shared or have appropriate locks held
316898127c3SJohn Johansen  *
317898127c3SJohn Johansen  * Returns: profile or NULL if no match found
318898127c3SJohn Johansen  */
319898127c3SJohn Johansen static struct aa_profile *__attach_match(const char *name,
320844b8292SJohn Johansen 					 struct list_head *head,
321844b8292SJohn Johansen 					 const char **info)
322898127c3SJohn Johansen {
323898127c3SJohn Johansen 	int len = 0;
324844b8292SJohn Johansen 	bool conflict = false;
325898127c3SJohn Johansen 	struct aa_profile *profile, *candidate = NULL;
326898127c3SJohn Johansen 
32701e2b670SJohn Johansen 	list_for_each_entry_rcu(profile, head, base.list) {
32806d426d1SJohn Johansen 		if (profile->label.flags & FLAG_NULL &&
32906d426d1SJohn Johansen 		    &profile->label == ns_unconfined(profile->ns))
330898127c3SJohn Johansen 			continue;
33106d426d1SJohn Johansen 
332844b8292SJohn Johansen 		if (profile->xmatch) {
3331a3881d3SMatthew Garrett 			if (profile->xmatch_len >= len) {
334844b8292SJohn Johansen 				unsigned int state;
335844b8292SJohn Johansen 				u32 perm;
336844b8292SJohn Johansen 
337844b8292SJohn Johansen 				state = aa_dfa_match(profile->xmatch,
338898127c3SJohn Johansen 						     DFA_START, name);
339844b8292SJohn Johansen 				perm = dfa_user_allow(profile->xmatch, state);
340898127c3SJohn Johansen 				/* any accepting state means a valid match. */
341898127c3SJohn Johansen 				if (perm & MAY_EXEC) {
3421a3881d3SMatthew Garrett 					if (profile->xmatch_len == len) {
3431a3881d3SMatthew Garrett 						conflict = true;
3441a3881d3SMatthew Garrett 						continue;
3451a3881d3SMatthew Garrett 					}
346898127c3SJohn Johansen 					candidate = profile;
347898127c3SJohn Johansen 					len = profile->xmatch_len;
348844b8292SJohn Johansen 					conflict = false;
349844b8292SJohn Johansen 				}
350898127c3SJohn Johansen 			}
351898127c3SJohn Johansen 		} else if (!strcmp(profile->base.name, name))
352898127c3SJohn Johansen 			/* exact non-re match, no more searching required */
353898127c3SJohn Johansen 			return profile;
354898127c3SJohn Johansen 	}
355898127c3SJohn Johansen 
356844b8292SJohn Johansen 	if (conflict) {
357844b8292SJohn Johansen 		*info = "conflicting profile attachments";
358844b8292SJohn Johansen 		return NULL;
359844b8292SJohn Johansen 	}
360844b8292SJohn Johansen 
361898127c3SJohn Johansen 	return candidate;
362898127c3SJohn Johansen }
363898127c3SJohn Johansen 
364898127c3SJohn Johansen /**
365898127c3SJohn Johansen  * find_attach - do attachment search for unconfined processes
366898127c3SJohn Johansen  * @ns: the current namespace  (NOT NULL)
367898127c3SJohn Johansen  * @list: list to search  (NOT NULL)
368898127c3SJohn Johansen  * @name: the executable name to match against  (NOT NULL)
369844b8292SJohn Johansen  * @info: info message if there was an error
370898127c3SJohn Johansen  *
37193c98a48SJohn Johansen  * Returns: label or NULL if no match found
372898127c3SJohn Johansen  */
37393c98a48SJohn Johansen static struct aa_label *find_attach(struct aa_ns *ns, struct list_head *list,
374844b8292SJohn Johansen 				    const char *name, const char **info)
375898127c3SJohn Johansen {
376898127c3SJohn Johansen 	struct aa_profile *profile;
377898127c3SJohn Johansen 
37801e2b670SJohn Johansen 	rcu_read_lock();
379844b8292SJohn Johansen 	profile = aa_get_profile(__attach_match(name, list, info));
38001e2b670SJohn Johansen 	rcu_read_unlock();
381898127c3SJohn Johansen 
38293c98a48SJohn Johansen 	return profile ? &profile->label : NULL;
383898127c3SJohn Johansen }
384898127c3SJohn Johansen 
385898127c3SJohn Johansen static const char *next_name(int xtype, const char *name)
386898127c3SJohn Johansen {
387898127c3SJohn Johansen 	return NULL;
388898127c3SJohn Johansen }
389898127c3SJohn Johansen 
390898127c3SJohn Johansen /**
391898127c3SJohn Johansen  * x_table_lookup - lookup an x transition name via transition table
392898127c3SJohn Johansen  * @profile: current profile (NOT NULL)
393898127c3SJohn Johansen  * @xindex: index into x transition table
39493c98a48SJohn Johansen  * @name: returns: name tested to find label (NOT NULL)
395898127c3SJohn Johansen  *
39693c98a48SJohn Johansen  * Returns: refcounted label, or NULL on failure (MAYBE NULL)
397898127c3SJohn Johansen  */
3982ea3ffb7SJohn Johansen struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
39993c98a48SJohn Johansen 				const char **name)
400898127c3SJohn Johansen {
40193c98a48SJohn Johansen 	struct aa_label *label = NULL;
402898127c3SJohn Johansen 	u32 xtype = xindex & AA_X_TYPE_MASK;
403898127c3SJohn Johansen 	int index = xindex & AA_X_INDEX_MASK;
40493c98a48SJohn Johansen 
40593c98a48SJohn Johansen 	AA_BUG(!name);
406898127c3SJohn Johansen 
407898127c3SJohn Johansen 	/* index is guaranteed to be in range, validated at load time */
40893c98a48SJohn Johansen 	/* TODO: move lookup parsing to unpack time so this is a straight
40993c98a48SJohn Johansen 	 *       index into the resultant label
41093c98a48SJohn Johansen 	 */
41193c98a48SJohn Johansen 	for (*name = profile->file.trans.table[index]; !label && *name;
41293c98a48SJohn Johansen 	     *name = next_name(xtype, *name)) {
413898127c3SJohn Johansen 		if (xindex & AA_X_CHILD) {
41493c98a48SJohn Johansen 			struct aa_profile *new_profile;
415898127c3SJohn Johansen 			/* release by caller */
41693c98a48SJohn Johansen 			new_profile = aa_find_child(profile, *name);
41793c98a48SJohn Johansen 			if (new_profile)
41893c98a48SJohn Johansen 				label = &new_profile->label;
419898127c3SJohn Johansen 			continue;
420898127c3SJohn Johansen 		}
42193c98a48SJohn Johansen 		label = aa_label_parse(&profile->label, *name, GFP_ATOMIC,
42293c98a48SJohn Johansen 				       true, false);
42393c98a48SJohn Johansen 		if (IS_ERR(label))
42493c98a48SJohn Johansen 			label = NULL;
425898127c3SJohn Johansen 	}
426898127c3SJohn Johansen 
427898127c3SJohn Johansen 	/* released by caller */
428898127c3SJohn Johansen 
42993c98a48SJohn Johansen 	return label;
430898127c3SJohn Johansen }
431898127c3SJohn Johansen 
432898127c3SJohn Johansen /**
43393c98a48SJohn Johansen  * x_to_label - get target label for a given xindex
434898127c3SJohn Johansen  * @profile: current profile  (NOT NULL)
435898127c3SJohn Johansen  * @name: name to lookup (NOT NULL)
436898127c3SJohn Johansen  * @xindex: index into x transition table
43793c98a48SJohn Johansen  * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
438898127c3SJohn Johansen  *
43993c98a48SJohn Johansen  * find label for a transition index
440898127c3SJohn Johansen  *
44193c98a48SJohn Johansen  * Returns: refcounted label or NULL if not found available
442898127c3SJohn Johansen  */
44393c98a48SJohn Johansen static struct aa_label *x_to_label(struct aa_profile *profile,
44493c98a48SJohn Johansen 				   const char *name, u32 xindex,
44593c98a48SJohn Johansen 				   const char **lookupname,
44693c98a48SJohn Johansen 				   const char **info)
447898127c3SJohn Johansen {
44893c98a48SJohn Johansen 	struct aa_label *new = NULL;
44998849dffSJohn Johansen 	struct aa_ns *ns = profile->ns;
450898127c3SJohn Johansen 	u32 xtype = xindex & AA_X_TYPE_MASK;
45193c98a48SJohn Johansen 	const char *stack = NULL;
452898127c3SJohn Johansen 
453898127c3SJohn Johansen 	switch (xtype) {
454898127c3SJohn Johansen 	case AA_X_NONE:
455898127c3SJohn Johansen 		/* fail exec unless ix || ux fallback - handled by caller */
45693c98a48SJohn Johansen 		*lookupname = NULL;
45793c98a48SJohn Johansen 		break;
45893c98a48SJohn Johansen 	case AA_X_TABLE:
45993c98a48SJohn Johansen 		/* TODO: fix when perm mapping done at unload */
46093c98a48SJohn Johansen 		stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
46193c98a48SJohn Johansen 		if (*stack != '&') {
46293c98a48SJohn Johansen 			/* released by caller */
46393c98a48SJohn Johansen 			new = x_table_lookup(profile, xindex, lookupname);
46493c98a48SJohn Johansen 			stack = NULL;
46593c98a48SJohn Johansen 			break;
46693c98a48SJohn Johansen 		}
46793c98a48SJohn Johansen 		/* fall through to X_NAME */
468898127c3SJohn Johansen 	case AA_X_NAME:
469898127c3SJohn Johansen 		if (xindex & AA_X_CHILD)
470898127c3SJohn Johansen 			/* released by caller */
47193c98a48SJohn Johansen 			new = find_attach(ns, &profile->base.profiles,
472844b8292SJohn Johansen 					  name, info);
473898127c3SJohn Johansen 		else
474898127c3SJohn Johansen 			/* released by caller */
47593c98a48SJohn Johansen 			new = find_attach(ns, &ns->base.profiles,
476844b8292SJohn Johansen 					  name, info);
47793c98a48SJohn Johansen 		*lookupname = name;
478898127c3SJohn Johansen 		break;
479898127c3SJohn Johansen 	}
480898127c3SJohn Johansen 
48193c98a48SJohn Johansen 	if (!new) {
48293c98a48SJohn Johansen 		if (xindex & AA_X_INHERIT) {
48393c98a48SJohn Johansen 			/* (p|c|n)ix - don't change profile but do
48493c98a48SJohn Johansen 			 * use the newest version
48593c98a48SJohn Johansen 			 */
48693c98a48SJohn Johansen 			*info = "ix fallback";
48793c98a48SJohn Johansen 			/* no profile && no error */
48893c98a48SJohn Johansen 			new = aa_get_newest_label(&profile->label);
48993c98a48SJohn Johansen 		} else if (xindex & AA_X_UNCONFINED) {
49093c98a48SJohn Johansen 			new = aa_get_newest_label(ns_unconfined(profile->ns));
49193c98a48SJohn Johansen 			*info = "ux fallback";
49293c98a48SJohn Johansen 		}
49393c98a48SJohn Johansen 	}
49493c98a48SJohn Johansen 
49593c98a48SJohn Johansen 	if (new && stack) {
49693c98a48SJohn Johansen 		/* base the stack on post domain transition */
49793c98a48SJohn Johansen 		struct aa_label *base = new;
49893c98a48SJohn Johansen 
49993c98a48SJohn Johansen 		new = aa_label_parse(base, stack, GFP_ATOMIC, true, false);
50093c98a48SJohn Johansen 		if (IS_ERR(new))
50193c98a48SJohn Johansen 			new = NULL;
50293c98a48SJohn Johansen 		aa_put_label(base);
50393c98a48SJohn Johansen 	}
50493c98a48SJohn Johansen 
505898127c3SJohn Johansen 	/* released by caller */
50693c98a48SJohn Johansen 	return new;
50793c98a48SJohn Johansen }
50893c98a48SJohn Johansen 
50993c98a48SJohn Johansen static struct aa_label *profile_transition(struct aa_profile *profile,
51093c98a48SJohn Johansen 					   const struct linux_binprm *bprm,
51193c98a48SJohn Johansen 					   char *buffer, struct path_cond *cond,
51293c98a48SJohn Johansen 					   bool *secure_exec)
51393c98a48SJohn Johansen {
51493c98a48SJohn Johansen 	struct aa_label *new = NULL;
51593c98a48SJohn Johansen 	const char *info = NULL, *name = NULL, *target = NULL;
51693c98a48SJohn Johansen 	unsigned int state = profile->file.start;
51793c98a48SJohn Johansen 	struct aa_perms perms = {};
51893c98a48SJohn Johansen 	bool nonewprivs = false;
51993c98a48SJohn Johansen 	int error = 0;
52093c98a48SJohn Johansen 
52193c98a48SJohn Johansen 	AA_BUG(!profile);
52293c98a48SJohn Johansen 	AA_BUG(!bprm);
52393c98a48SJohn Johansen 	AA_BUG(!buffer);
52493c98a48SJohn Johansen 
52593c98a48SJohn Johansen 	error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
52693c98a48SJohn Johansen 			     &name, &info, profile->disconnected);
52793c98a48SJohn Johansen 	if (error) {
52893c98a48SJohn Johansen 		if (profile_unconfined(profile) ||
52993c98a48SJohn Johansen 		    (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
53093c98a48SJohn Johansen 			AA_DEBUG("name lookup ix on error");
53193c98a48SJohn Johansen 			error = 0;
53293c98a48SJohn Johansen 			new = aa_get_newest_label(&profile->label);
53393c98a48SJohn Johansen 		}
53493c98a48SJohn Johansen 		name = bprm->filename;
53593c98a48SJohn Johansen 		goto audit;
53693c98a48SJohn Johansen 	}
53793c98a48SJohn Johansen 
53893c98a48SJohn Johansen 	if (profile_unconfined(profile)) {
53993c98a48SJohn Johansen 		new = find_attach(profile->ns, &profile->ns->base.profiles,
540844b8292SJohn Johansen 				  name, &info);
54193c98a48SJohn Johansen 		if (new) {
54293c98a48SJohn Johansen 			AA_DEBUG("unconfined attached to new label");
54393c98a48SJohn Johansen 			return new;
54493c98a48SJohn Johansen 		}
54593c98a48SJohn Johansen 		AA_DEBUG("unconfined exec no attachment");
54693c98a48SJohn Johansen 		return aa_get_newest_label(&profile->label);
54793c98a48SJohn Johansen 	}
54893c98a48SJohn Johansen 
54993c98a48SJohn Johansen 	/* find exec permissions for name */
55093c98a48SJohn Johansen 	state = aa_str_perms(profile->file.dfa, state, name, cond, &perms);
55193c98a48SJohn Johansen 	if (perms.allow & MAY_EXEC) {
55293c98a48SJohn Johansen 		/* exec permission determine how to transition */
55393c98a48SJohn Johansen 		new = x_to_label(profile, name, perms.xindex, &target, &info);
55493c98a48SJohn Johansen 		if (new && new->proxy == profile->label.proxy && info) {
55593c98a48SJohn Johansen 			/* hack ix fallback - improve how this is detected */
55693c98a48SJohn Johansen 			goto audit;
55793c98a48SJohn Johansen 		} else if (!new) {
55893c98a48SJohn Johansen 			error = -EACCES;
55993c98a48SJohn Johansen 			info = "profile transition not found";
56093c98a48SJohn Johansen 			/* remove MAY_EXEC to audit as failure */
56193c98a48SJohn Johansen 			perms.allow &= ~MAY_EXEC;
56293c98a48SJohn Johansen 		}
56393c98a48SJohn Johansen 	} else if (COMPLAIN_MODE(profile)) {
56493c98a48SJohn Johansen 		/* no exec permission - learning mode */
5655d7c44efSJohn Johansen 		struct aa_profile *new_profile = NULL;
5665d7c44efSJohn Johansen 		char *n = kstrdup(name, GFP_ATOMIC);
5675d7c44efSJohn Johansen 
5685d7c44efSJohn Johansen 		if (n) {
5695d7c44efSJohn Johansen 			/* name is ptr into buffer */
5705d7c44efSJohn Johansen 			long pos = name - buffer;
5715d7c44efSJohn Johansen 			/* break per cpu buffer hold */
5725d7c44efSJohn Johansen 			put_buffers(buffer);
5735d7c44efSJohn Johansen 			new_profile = aa_new_null_profile(profile, false, n,
5745d7c44efSJohn Johansen 							  GFP_KERNEL);
5755d7c44efSJohn Johansen 			get_buffers(buffer);
5765d7c44efSJohn Johansen 			name = buffer + pos;
5775d7c44efSJohn Johansen 			strcpy((char *)name, n);
5785d7c44efSJohn Johansen 			kfree(n);
5795d7c44efSJohn Johansen 		}
58093c98a48SJohn Johansen 		if (!new_profile) {
58193c98a48SJohn Johansen 			error = -ENOMEM;
58293c98a48SJohn Johansen 			info = "could not create null profile";
58393c98a48SJohn Johansen 		} else {
58493c98a48SJohn Johansen 			error = -EACCES;
58593c98a48SJohn Johansen 			new = &new_profile->label;
58693c98a48SJohn Johansen 		}
58793c98a48SJohn Johansen 		perms.xindex |= AA_X_UNSAFE;
58893c98a48SJohn Johansen 	} else
58993c98a48SJohn Johansen 		/* fail exec */
59093c98a48SJohn Johansen 		error = -EACCES;
59193c98a48SJohn Johansen 
59293c98a48SJohn Johansen 	if (!new)
59393c98a48SJohn Johansen 		goto audit;
59493c98a48SJohn Johansen 
59593c98a48SJohn Johansen 
59693c98a48SJohn Johansen 	if (!(perms.xindex & AA_X_UNSAFE)) {
59793c98a48SJohn Johansen 		if (DEBUG_ON) {
59893c98a48SJohn Johansen 			dbg_printk("apparmor: scrubbing environment variables"
59993c98a48SJohn Johansen 				   " for %s profile=", name);
60093c98a48SJohn Johansen 			aa_label_printk(new, GFP_ATOMIC);
60193c98a48SJohn Johansen 			dbg_printk("\n");
60293c98a48SJohn Johansen 		}
60393c98a48SJohn Johansen 		*secure_exec = true;
60493c98a48SJohn Johansen 	}
60593c98a48SJohn Johansen 
60693c98a48SJohn Johansen audit:
60793c98a48SJohn Johansen 	aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
60893c98a48SJohn Johansen 		      cond->uid, info, error);
60993c98a48SJohn Johansen 	if (!new || nonewprivs) {
61093c98a48SJohn Johansen 		aa_put_label(new);
61193c98a48SJohn Johansen 		return ERR_PTR(error);
61293c98a48SJohn Johansen 	}
61393c98a48SJohn Johansen 
61493c98a48SJohn Johansen 	return new;
61593c98a48SJohn Johansen }
61693c98a48SJohn Johansen 
61793c98a48SJohn Johansen static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec,
61893c98a48SJohn Johansen 			  bool stack, const struct linux_binprm *bprm,
61993c98a48SJohn Johansen 			  char *buffer, struct path_cond *cond,
62093c98a48SJohn Johansen 			  bool *secure_exec)
62193c98a48SJohn Johansen {
62293c98a48SJohn Johansen 	unsigned int state = profile->file.start;
62393c98a48SJohn Johansen 	struct aa_perms perms = {};
62493c98a48SJohn Johansen 	const char *xname = NULL, *info = "change_profile onexec";
62593c98a48SJohn Johansen 	int error = -EACCES;
62693c98a48SJohn Johansen 
62793c98a48SJohn Johansen 	AA_BUG(!profile);
62893c98a48SJohn Johansen 	AA_BUG(!onexec);
62993c98a48SJohn Johansen 	AA_BUG(!bprm);
63093c98a48SJohn Johansen 	AA_BUG(!buffer);
63193c98a48SJohn Johansen 
63293c98a48SJohn Johansen 	if (profile_unconfined(profile)) {
63393c98a48SJohn Johansen 		/* change_profile on exec already granted */
63493c98a48SJohn Johansen 		/*
63593c98a48SJohn Johansen 		 * NOTE: Domain transitions from unconfined are allowed
63693c98a48SJohn Johansen 		 * even when no_new_privs is set because this aways results
63793c98a48SJohn Johansen 		 * in a further reduction of permissions.
63893c98a48SJohn Johansen 		 */
63993c98a48SJohn Johansen 		return 0;
64093c98a48SJohn Johansen 	}
64193c98a48SJohn Johansen 
64293c98a48SJohn Johansen 	error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
64393c98a48SJohn Johansen 			     &xname, &info, profile->disconnected);
64493c98a48SJohn Johansen 	if (error) {
64593c98a48SJohn Johansen 		if (profile_unconfined(profile) ||
64693c98a48SJohn Johansen 		    (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
64793c98a48SJohn Johansen 			AA_DEBUG("name lookup ix on error");
64893c98a48SJohn Johansen 			error = 0;
64993c98a48SJohn Johansen 		}
65093c98a48SJohn Johansen 		xname = bprm->filename;
65193c98a48SJohn Johansen 		goto audit;
65293c98a48SJohn Johansen 	}
65393c98a48SJohn Johansen 
65493c98a48SJohn Johansen 	/* find exec permissions for name */
65593c98a48SJohn Johansen 	state = aa_str_perms(profile->file.dfa, state, xname, cond, &perms);
65693c98a48SJohn Johansen 	if (!(perms.allow & AA_MAY_ONEXEC)) {
65793c98a48SJohn Johansen 		info = "no change_onexec valid for executable";
65893c98a48SJohn Johansen 		goto audit;
65993c98a48SJohn Johansen 	}
66093c98a48SJohn Johansen 	/* test if this exec can be paired with change_profile onexec.
66193c98a48SJohn Johansen 	 * onexec permission is linked to exec with a standard pairing
66293c98a48SJohn Johansen 	 * exec\0change_profile
66393c98a48SJohn Johansen 	 */
66493c98a48SJohn Johansen 	state = aa_dfa_null_transition(profile->file.dfa, state);
66593c98a48SJohn Johansen 	error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
66693c98a48SJohn Johansen 				     state, &perms);
66793c98a48SJohn Johansen 	if (error) {
66893c98a48SJohn Johansen 		perms.allow &= ~AA_MAY_ONEXEC;
66993c98a48SJohn Johansen 		goto audit;
67093c98a48SJohn Johansen 	}
67193c98a48SJohn Johansen 
67293c98a48SJohn Johansen 	if (!(perms.xindex & AA_X_UNSAFE)) {
67393c98a48SJohn Johansen 		if (DEBUG_ON) {
67493c98a48SJohn Johansen 			dbg_printk("apparmor: scrubbing environment "
67593c98a48SJohn Johansen 				   "variables for %s label=", xname);
67693c98a48SJohn Johansen 			aa_label_printk(onexec, GFP_ATOMIC);
67793c98a48SJohn Johansen 			dbg_printk("\n");
67893c98a48SJohn Johansen 		}
67993c98a48SJohn Johansen 		*secure_exec = true;
68093c98a48SJohn Johansen 	}
68193c98a48SJohn Johansen 
68293c98a48SJohn Johansen audit:
68393c98a48SJohn Johansen 	return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
68493c98a48SJohn Johansen 			     NULL, onexec, cond->uid, info, error);
68593c98a48SJohn Johansen }
68693c98a48SJohn Johansen 
68793c98a48SJohn Johansen /* ensure none ns domain transitions are correctly applied with onexec */
68893c98a48SJohn Johansen 
68993c98a48SJohn Johansen static struct aa_label *handle_onexec(struct aa_label *label,
69093c98a48SJohn Johansen 				      struct aa_label *onexec, bool stack,
69193c98a48SJohn Johansen 				      const struct linux_binprm *bprm,
69293c98a48SJohn Johansen 				      char *buffer, struct path_cond *cond,
69393c98a48SJohn Johansen 				      bool *unsafe)
69493c98a48SJohn Johansen {
69593c98a48SJohn Johansen 	struct aa_profile *profile;
69693c98a48SJohn Johansen 	struct aa_label *new;
69793c98a48SJohn Johansen 	int error;
69893c98a48SJohn Johansen 
69993c98a48SJohn Johansen 	AA_BUG(!label);
70093c98a48SJohn Johansen 	AA_BUG(!onexec);
70193c98a48SJohn Johansen 	AA_BUG(!bprm);
70293c98a48SJohn Johansen 	AA_BUG(!buffer);
70393c98a48SJohn Johansen 
70493c98a48SJohn Johansen 	if (!stack) {
70593c98a48SJohn Johansen 		error = fn_for_each_in_ns(label, profile,
70693c98a48SJohn Johansen 				profile_onexec(profile, onexec, stack,
70793c98a48SJohn Johansen 					       bprm, buffer, cond, unsafe));
70893c98a48SJohn Johansen 		if (error)
70993c98a48SJohn Johansen 			return ERR_PTR(error);
71093c98a48SJohn Johansen 		new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
71193c98a48SJohn Johansen 				aa_get_newest_label(onexec),
71293c98a48SJohn Johansen 				profile_transition(profile, bprm, buffer,
71393c98a48SJohn Johansen 						   cond, unsafe));
71493c98a48SJohn Johansen 
71593c98a48SJohn Johansen 	} else {
71693c98a48SJohn Johansen 		/* TODO: determine how much we want to losen this */
71793c98a48SJohn Johansen 		error = fn_for_each_in_ns(label, profile,
71893c98a48SJohn Johansen 				profile_onexec(profile, onexec, stack, bprm,
71993c98a48SJohn Johansen 					       buffer, cond, unsafe));
72093c98a48SJohn Johansen 		if (error)
72193c98a48SJohn Johansen 			return ERR_PTR(error);
72293c98a48SJohn Johansen 		new = fn_label_build_in_ns(label, profile, GFP_ATOMIC,
72393c98a48SJohn Johansen 				aa_label_merge(&profile->label, onexec,
72493c98a48SJohn Johansen 					       GFP_ATOMIC),
72593c98a48SJohn Johansen 				profile_transition(profile, bprm, buffer,
72693c98a48SJohn Johansen 						   cond, unsafe));
72793c98a48SJohn Johansen 	}
72893c98a48SJohn Johansen 
72993c98a48SJohn Johansen 	if (new)
73093c98a48SJohn Johansen 		return new;
73193c98a48SJohn Johansen 
73293c98a48SJohn Johansen 	/* TODO: get rid of GLOBAL_ROOT_UID */
73393c98a48SJohn Johansen 	error = fn_for_each_in_ns(label, profile,
73493c98a48SJohn Johansen 			aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC,
73593c98a48SJohn Johansen 				      AA_MAY_ONEXEC, bprm->filename, NULL,
73693c98a48SJohn Johansen 				      onexec, GLOBAL_ROOT_UID,
73793c98a48SJohn Johansen 				      "failed to build target label", -ENOMEM));
73893c98a48SJohn Johansen 	return ERR_PTR(error);
739898127c3SJohn Johansen }
740898127c3SJohn Johansen 
741898127c3SJohn Johansen /**
742898127c3SJohn Johansen  * apparmor_bprm_set_creds - set the new creds on the bprm struct
743898127c3SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
744898127c3SJohn Johansen  *
745898127c3SJohn Johansen  * Returns: %0 or error on failure
74693c98a48SJohn Johansen  *
74793c98a48SJohn Johansen  * TODO: once the other paths are done see if we can't refactor into a fn
748898127c3SJohn Johansen  */
749898127c3SJohn Johansen int apparmor_bprm_set_creds(struct linux_binprm *bprm)
750898127c3SJohn Johansen {
751f175221aSJohn Johansen 	struct aa_task_ctx *ctx;
75293c98a48SJohn Johansen 	struct aa_label *label, *new = NULL;
75393c98a48SJohn Johansen 	struct aa_profile *profile;
754898127c3SJohn Johansen 	char *buffer = NULL;
75593c98a48SJohn Johansen 	const char *info = NULL;
75693c98a48SJohn Johansen 	int error = 0;
75793c98a48SJohn Johansen 	bool unsafe = false;
758898127c3SJohn Johansen 	struct path_cond cond = {
759496ad9aaSAl Viro 		file_inode(bprm->file)->i_uid,
760496ad9aaSAl Viro 		file_inode(bprm->file)->i_mode
761898127c3SJohn Johansen 	};
762898127c3SJohn Johansen 
763ddb4a144SKees Cook 	if (bprm->called_set_creds)
764898127c3SJohn Johansen 		return 0;
765898127c3SJohn Johansen 
766de62de59SJohn Johansen 	ctx = task_ctx(current);
767d9087c49SJohn Johansen 	AA_BUG(!cred_label(bprm->cred));
768f175221aSJohn Johansen 	AA_BUG(!ctx);
769898127c3SJohn Johansen 
770d9087c49SJohn Johansen 	label = aa_get_newest_label(cred_label(bprm->cred));
7714227c333SJohn Johansen 
772*9fcf78ccSJohn Johansen 	/*
773*9fcf78ccSJohn Johansen 	 * Detect no new privs being set, and store the label it
774*9fcf78ccSJohn Johansen 	 * occurred under. Ideally this would happen when nnp
775*9fcf78ccSJohn Johansen 	 * is set but there isn't a good way to do that yet.
776*9fcf78ccSJohn Johansen 	 *
777*9fcf78ccSJohn Johansen 	 * Testing for unconfined must be done before the subset test
778*9fcf78ccSJohn Johansen 	 */
779*9fcf78ccSJohn Johansen 	if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
780*9fcf78ccSJohn Johansen 	    !ctx->nnp)
781*9fcf78ccSJohn Johansen 		ctx->nnp = aa_get_label(label);
782*9fcf78ccSJohn Johansen 
7834227c333SJohn Johansen 	/* buffer freed below, name is pointer into buffer */
7844227c333SJohn Johansen 	get_buffers(buffer);
78593c98a48SJohn Johansen 	/* Test for onexec first as onexec override other x transitions. */
786f175221aSJohn Johansen 	if (ctx->onexec)
787f175221aSJohn Johansen 		new = handle_onexec(label, ctx->onexec, ctx->token,
78893c98a48SJohn Johansen 				    bprm, buffer, &cond, &unsafe);
789898127c3SJohn Johansen 	else
79093c98a48SJohn Johansen 		new = fn_label_build(label, profile, GFP_ATOMIC,
79193c98a48SJohn Johansen 				profile_transition(profile, bprm, buffer,
79293c98a48SJohn Johansen 						   &cond, &unsafe));
793898127c3SJohn Johansen 
79493c98a48SJohn Johansen 	AA_BUG(!new);
79593c98a48SJohn Johansen 	if (IS_ERR(new)) {
79693c98a48SJohn Johansen 		error = PTR_ERR(new);
79793c98a48SJohn Johansen 		goto done;
79893c98a48SJohn Johansen 	} else if (!new) {
799898127c3SJohn Johansen 		error = -ENOMEM;
80093c98a48SJohn Johansen 		goto done;
801c29bceb3SJohn Johansen 	}
802c29bceb3SJohn Johansen 
803*9fcf78ccSJohn Johansen 	/* Policy has specified a domain transitions. If no_new_privs and
804*9fcf78ccSJohn Johansen 	 * confined ensure the transition is to confinement that is subset
805*9fcf78ccSJohn Johansen 	 * of the confinement when the task entered no new privs.
806*9fcf78ccSJohn Johansen 	 *
807*9fcf78ccSJohn Johansen 	 * NOTE: Domain transitions from unconfined and to stacked
808*9fcf78ccSJohn Johansen 	 * subsets are allowed even when no_new_privs is set because this
809*9fcf78ccSJohn Johansen 	 * aways results in a further reduction of permissions.
810*9fcf78ccSJohn Johansen 	 */
811*9fcf78ccSJohn Johansen 	if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
812*9fcf78ccSJohn Johansen 	    !unconfined(label) && !aa_label_is_subset(new, ctx->nnp)) {
813*9fcf78ccSJohn Johansen 		error = -EPERM;
814*9fcf78ccSJohn Johansen 		info = "no new privs";
815*9fcf78ccSJohn Johansen 		goto audit;
816*9fcf78ccSJohn Johansen 	}
817898127c3SJohn Johansen 
818898127c3SJohn Johansen 	if (bprm->unsafe & LSM_UNSAFE_SHARE) {
819898127c3SJohn Johansen 		/* FIXME: currently don't mediate shared state */
820898127c3SJohn Johansen 		;
821898127c3SJohn Johansen 	}
822898127c3SJohn Johansen 
82393c98a48SJohn Johansen 	if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
82493c98a48SJohn Johansen 		/* TODO: test needs to be profile of label to new */
82593c98a48SJohn Johansen 		error = may_change_ptraced_domain(new, &info);
826f7da2de0SJohn Johansen 		if (error)
827898127c3SJohn Johansen 			goto audit;
828898127c3SJohn Johansen 	}
829898127c3SJohn Johansen 
83093c98a48SJohn Johansen 	if (unsafe) {
83193c98a48SJohn Johansen 		if (DEBUG_ON) {
83293c98a48SJohn Johansen 			dbg_printk("scrubbing environment variables for %s "
83393c98a48SJohn Johansen 				   "label=", bprm->filename);
83493c98a48SJohn Johansen 			aa_label_printk(new, GFP_ATOMIC);
83593c98a48SJohn Johansen 			dbg_printk("\n");
83693c98a48SJohn Johansen 		}
837993b3ab0SKees Cook 		bprm->secureexec = 1;
838898127c3SJohn Johansen 	}
83993c98a48SJohn Johansen 
84093c98a48SJohn Johansen 	if (label->proxy != new->proxy) {
84193c98a48SJohn Johansen 		/* when transitioning clear unsafe personality bits */
84293c98a48SJohn Johansen 		if (DEBUG_ON) {
84393c98a48SJohn Johansen 			dbg_printk("apparmor: clearing unsafe personality "
84493c98a48SJohn Johansen 				   "bits. %s label=", bprm->filename);
84593c98a48SJohn Johansen 			aa_label_printk(new, GFP_ATOMIC);
84693c98a48SJohn Johansen 			dbg_printk("\n");
84793c98a48SJohn Johansen 		}
848898127c3SJohn Johansen 		bprm->per_clear |= PER_CLEAR_ON_SETID;
84993c98a48SJohn Johansen 	}
850d9087c49SJohn Johansen 	aa_put_label(cred_label(bprm->cred));
851d9087c49SJohn Johansen 	/* transfer reference, released when cred is freed */
852d9087c49SJohn Johansen 	cred_label(bprm->cred) = new;
853898127c3SJohn Johansen 
85493c98a48SJohn Johansen done:
855637f688dSJohn Johansen 	aa_put_label(label);
8564227c333SJohn Johansen 	put_buffers(buffer);
857898127c3SJohn Johansen 
858898127c3SJohn Johansen 	return error;
85993c98a48SJohn Johansen 
86093c98a48SJohn Johansen audit:
86193c98a48SJohn Johansen 	error = fn_for_each(label, profile,
86293c98a48SJohn Johansen 			aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
86393c98a48SJohn Johansen 				      bprm->filename, NULL, new,
86493c98a48SJohn Johansen 				      file_inode(bprm->file)->i_uid, info,
86593c98a48SJohn Johansen 				      error));
86693c98a48SJohn Johansen 	aa_put_label(new);
86793c98a48SJohn Johansen 	goto done;
868898127c3SJohn Johansen }
869898127c3SJohn Johansen 
870898127c3SJohn Johansen /*
871898127c3SJohn Johansen  * Functions for self directed profile change
872898127c3SJohn Johansen  */
873898127c3SJohn Johansen 
87489dbf196SJohn Johansen 
87589dbf196SJohn Johansen /* helper fn for change_hat
876898127c3SJohn Johansen  *
87789dbf196SJohn Johansen  * Returns: label for hat transition OR ERR_PTR.  Does NOT return NULL
878898127c3SJohn Johansen  */
87989dbf196SJohn Johansen static struct aa_label *build_change_hat(struct aa_profile *profile,
88089dbf196SJohn Johansen 					 const char *name, bool sibling)
881898127c3SJohn Johansen {
88289dbf196SJohn Johansen 	struct aa_profile *root, *hat = NULL;
88389dbf196SJohn Johansen 	const char *info = NULL;
88489dbf196SJohn Johansen 	int error = 0;
88589dbf196SJohn Johansen 
88689dbf196SJohn Johansen 	if (sibling && PROFILE_IS_HAT(profile)) {
88789dbf196SJohn Johansen 		root = aa_get_profile_rcu(&profile->parent);
88889dbf196SJohn Johansen 	} else if (!sibling && !PROFILE_IS_HAT(profile)) {
88989dbf196SJohn Johansen 		root = aa_get_profile(profile);
89089dbf196SJohn Johansen 	} else {
89189dbf196SJohn Johansen 		info = "conflicting target types";
89289dbf196SJohn Johansen 		error = -EPERM;
89389dbf196SJohn Johansen 		goto audit;
89489dbf196SJohn Johansen 	}
89589dbf196SJohn Johansen 
89689dbf196SJohn Johansen 	hat = aa_find_child(root, name);
89789dbf196SJohn Johansen 	if (!hat) {
89889dbf196SJohn Johansen 		error = -ENOENT;
89989dbf196SJohn Johansen 		if (COMPLAIN_MODE(profile)) {
90089dbf196SJohn Johansen 			hat = aa_new_null_profile(profile, true, name,
90189dbf196SJohn Johansen 						  GFP_KERNEL);
90289dbf196SJohn Johansen 			if (!hat) {
90389dbf196SJohn Johansen 				info = "failed null profile create";
90489dbf196SJohn Johansen 				error = -ENOMEM;
90589dbf196SJohn Johansen 			}
90689dbf196SJohn Johansen 		}
90789dbf196SJohn Johansen 	}
90889dbf196SJohn Johansen 	aa_put_profile(root);
90989dbf196SJohn Johansen 
91089dbf196SJohn Johansen audit:
91189dbf196SJohn Johansen 	aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
91289dbf196SJohn Johansen 		      name, hat ? hat->base.hname : NULL,
91389dbf196SJohn Johansen 		      hat ? &hat->label : NULL, GLOBAL_ROOT_UID, NULL,
91489dbf196SJohn Johansen 		      error);
91589dbf196SJohn Johansen 	if (!hat || (error && error != -ENOENT))
91689dbf196SJohn Johansen 		return ERR_PTR(error);
91789dbf196SJohn Johansen 	/* if hat && error - complain mode, already audited and we adjust for
91889dbf196SJohn Johansen 	 * complain mode allow by returning hat->label
91989dbf196SJohn Johansen 	 */
92089dbf196SJohn Johansen 	return &hat->label;
92189dbf196SJohn Johansen }
92289dbf196SJohn Johansen 
92389dbf196SJohn Johansen /* helper fn for changing into a hat
92489dbf196SJohn Johansen  *
92589dbf196SJohn Johansen  * Returns: label for hat transition or ERR_PTR. Does not return NULL
92689dbf196SJohn Johansen  */
92789dbf196SJohn Johansen static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
92889dbf196SJohn Johansen 				   int count, int flags)
92989dbf196SJohn Johansen {
93089dbf196SJohn Johansen 	struct aa_profile *profile, *root, *hat = NULL;
93189dbf196SJohn Johansen 	struct aa_label *new;
93289dbf196SJohn Johansen 	struct label_it it;
93389dbf196SJohn Johansen 	bool sibling = false;
93489dbf196SJohn Johansen 	const char *name, *info = NULL;
93589dbf196SJohn Johansen 	int i, error;
93689dbf196SJohn Johansen 
93789dbf196SJohn Johansen 	AA_BUG(!label);
93889dbf196SJohn Johansen 	AA_BUG(!hats);
93989dbf196SJohn Johansen 	AA_BUG(count < 1);
94089dbf196SJohn Johansen 
94189dbf196SJohn Johansen 	if (PROFILE_IS_HAT(labels_profile(label)))
94289dbf196SJohn Johansen 		sibling = true;
94389dbf196SJohn Johansen 
94489dbf196SJohn Johansen 	/*find first matching hat */
94589dbf196SJohn Johansen 	for (i = 0; i < count && !hat; i++) {
94689dbf196SJohn Johansen 		name = hats[i];
94789dbf196SJohn Johansen 		label_for_each_in_ns(it, labels_ns(label), label, profile) {
94889dbf196SJohn Johansen 			if (sibling && PROFILE_IS_HAT(profile)) {
94989dbf196SJohn Johansen 				root = aa_get_profile_rcu(&profile->parent);
95089dbf196SJohn Johansen 			} else if (!sibling && !PROFILE_IS_HAT(profile)) {
95189dbf196SJohn Johansen 				root = aa_get_profile(profile);
95289dbf196SJohn Johansen 			} else {	/* conflicting change type */
95389dbf196SJohn Johansen 				info = "conflicting targets types";
95489dbf196SJohn Johansen 				error = -EPERM;
95589dbf196SJohn Johansen 				goto fail;
95689dbf196SJohn Johansen 			}
95789dbf196SJohn Johansen 			hat = aa_find_child(root, name);
95889dbf196SJohn Johansen 			aa_put_profile(root);
95989dbf196SJohn Johansen 			if (!hat) {
96089dbf196SJohn Johansen 				if (!COMPLAIN_MODE(profile))
96189dbf196SJohn Johansen 					goto outer_continue;
96289dbf196SJohn Johansen 				/* complain mode succeed as if hat */
96389dbf196SJohn Johansen 			} else if (!PROFILE_IS_HAT(hat)) {
96489dbf196SJohn Johansen 				info = "target not hat";
96589dbf196SJohn Johansen 				error = -EPERM;
96689dbf196SJohn Johansen 				aa_put_profile(hat);
96789dbf196SJohn Johansen 				goto fail;
96889dbf196SJohn Johansen 			}
96989dbf196SJohn Johansen 			aa_put_profile(hat);
97089dbf196SJohn Johansen 		}
97189dbf196SJohn Johansen 		/* found a hat for all profiles in ns */
97289dbf196SJohn Johansen 		goto build;
97389dbf196SJohn Johansen outer_continue:
97489dbf196SJohn Johansen 	;
97589dbf196SJohn Johansen 	}
97689dbf196SJohn Johansen 	/* no hats that match, find appropriate error
97789dbf196SJohn Johansen 	 *
97889dbf196SJohn Johansen 	 * In complain mode audit of the failure is based off of the first
97989dbf196SJohn Johansen 	 * hat supplied.  This is done due how userspace interacts with
98089dbf196SJohn Johansen 	 * change_hat.
98189dbf196SJohn Johansen 	 */
98289dbf196SJohn Johansen 	name = NULL;
98389dbf196SJohn Johansen 	label_for_each_in_ns(it, labels_ns(label), label, profile) {
98489dbf196SJohn Johansen 		if (!list_empty(&profile->base.profiles)) {
98589dbf196SJohn Johansen 			info = "hat not found";
98689dbf196SJohn Johansen 			error = -ENOENT;
98789dbf196SJohn Johansen 			goto fail;
98889dbf196SJohn Johansen 		}
98989dbf196SJohn Johansen 	}
99089dbf196SJohn Johansen 	info = "no hats defined";
99189dbf196SJohn Johansen 	error = -ECHILD;
99289dbf196SJohn Johansen 
99389dbf196SJohn Johansen fail:
99489dbf196SJohn Johansen 	label_for_each_in_ns(it, labels_ns(label), label, profile) {
99589dbf196SJohn Johansen 		/*
99689dbf196SJohn Johansen 		 * no target as it has failed to be found or built
99789dbf196SJohn Johansen 		 *
99889dbf196SJohn Johansen 		 * change_hat uses probing and should not log failures
99989dbf196SJohn Johansen 		 * related to missing hats
100089dbf196SJohn Johansen 		 */
100189dbf196SJohn Johansen 		/* TODO: get rid of GLOBAL_ROOT_UID */
100289dbf196SJohn Johansen 		if (count > 1 || COMPLAIN_MODE(profile)) {
100389dbf196SJohn Johansen 			aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
100489dbf196SJohn Johansen 				      AA_MAY_CHANGEHAT, name, NULL, NULL,
100589dbf196SJohn Johansen 				      GLOBAL_ROOT_UID, info, error);
100689dbf196SJohn Johansen 		}
100789dbf196SJohn Johansen 	}
100889dbf196SJohn Johansen 	return ERR_PTR(error);
100989dbf196SJohn Johansen 
101089dbf196SJohn Johansen build:
101189dbf196SJohn Johansen 	new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
101289dbf196SJohn Johansen 				   build_change_hat(profile, name, sibling),
101389dbf196SJohn Johansen 				   aa_get_label(&profile->label));
101489dbf196SJohn Johansen 	if (!new) {
101589dbf196SJohn Johansen 		info = "label build failed";
101689dbf196SJohn Johansen 		error = -ENOMEM;
101789dbf196SJohn Johansen 		goto fail;
101889dbf196SJohn Johansen 	} /* else if (IS_ERR) build_change_hat has logged error so return new */
101989dbf196SJohn Johansen 
102089dbf196SJohn Johansen 	return new;
1021898127c3SJohn Johansen }
1022898127c3SJohn Johansen 
1023898127c3SJohn Johansen /**
1024898127c3SJohn Johansen  * aa_change_hat - change hat to/from subprofile
1025898127c3SJohn Johansen  * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1026898127c3SJohn Johansen  * @count: number of hat names in @hats
1027898127c3SJohn Johansen  * @token: magic value to validate the hat change
1028df8073c6SJohn Johansen  * @flags: flags affecting behavior of the change
1029898127c3SJohn Johansen  *
103089dbf196SJohn Johansen  * Returns %0 on success, error otherwise.
103189dbf196SJohn Johansen  *
1032898127c3SJohn Johansen  * Change to the first profile specified in @hats that exists, and store
1033898127c3SJohn Johansen  * the @hat_magic in the current task context.  If the count == 0 and the
1034898127c3SJohn Johansen  * @token matches that stored in the current task context, return to the
1035898127c3SJohn Johansen  * top level profile.
1036898127c3SJohn Johansen  *
103789dbf196SJohn Johansen  * change_hat only applies to profiles in the current ns, and each profile
103889dbf196SJohn Johansen  * in the ns must make the same transition otherwise change_hat will fail.
1039898127c3SJohn Johansen  */
1040df8073c6SJohn Johansen int aa_change_hat(const char *hats[], int count, u64 token, int flags)
1041898127c3SJohn Johansen {
1042898127c3SJohn Johansen 	const struct cred *cred;
1043*9fcf78ccSJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
104489dbf196SJohn Johansen 	struct aa_label *label, *previous, *new = NULL, *target = NULL;
104589dbf196SJohn Johansen 	struct aa_profile *profile;
10462d679f3cSJohn Johansen 	struct aa_perms perms = {};
104789dbf196SJohn Johansen 	const char *info = NULL;
1048898127c3SJohn Johansen 	int error = 0;
1049898127c3SJohn Johansen 
1050898127c3SJohn Johansen 	/* released below */
1051898127c3SJohn Johansen 	cred = get_current_cred();
1052637f688dSJohn Johansen 	label = aa_get_newest_cred_label(cred);
1053f175221aSJohn Johansen 	previous = aa_get_newest_label(ctx->previous);
1054898127c3SJohn Johansen 
1055*9fcf78ccSJohn Johansen 	/*
1056*9fcf78ccSJohn Johansen 	 * Detect no new privs being set, and store the label it
1057*9fcf78ccSJohn Johansen 	 * occurred under. Ideally this would happen when nnp
1058*9fcf78ccSJohn Johansen 	 * is set but there isn't a good way to do that yet.
1059*9fcf78ccSJohn Johansen 	 *
1060*9fcf78ccSJohn Johansen 	 * Testing for unconfined must be done before the subset test
1061*9fcf78ccSJohn Johansen 	 */
1062*9fcf78ccSJohn Johansen 	if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1063*9fcf78ccSJohn Johansen 		ctx->nnp = aa_get_label(label);
1064*9fcf78ccSJohn Johansen 
1065637f688dSJohn Johansen 	if (unconfined(label)) {
106689dbf196SJohn Johansen 		info = "unconfined can not change_hat";
1067898127c3SJohn Johansen 		error = -EPERM;
106889dbf196SJohn Johansen 		goto fail;
1069898127c3SJohn Johansen 	}
1070898127c3SJohn Johansen 
1071898127c3SJohn Johansen 	if (count) {
107289dbf196SJohn Johansen 		new = change_hat(label, hats, count, flags);
107389dbf196SJohn Johansen 		AA_BUG(!new);
107489dbf196SJohn Johansen 		if (IS_ERR(new)) {
107589dbf196SJohn Johansen 			error = PTR_ERR(new);
107689dbf196SJohn Johansen 			new = NULL;
107789dbf196SJohn Johansen 			/* already audited */
1078898127c3SJohn Johansen 			goto out;
1079898127c3SJohn Johansen 		}
1080898127c3SJohn Johansen 
108189dbf196SJohn Johansen 		error = may_change_ptraced_domain(new, &info);
108289dbf196SJohn Johansen 		if (error)
108389dbf196SJohn Johansen 			goto fail;
1084898127c3SJohn Johansen 
1085*9fcf78ccSJohn Johansen 		/*
1086*9fcf78ccSJohn Johansen 		 * no new privs prevents domain transitions that would
1087*9fcf78ccSJohn Johansen 		 * reduce restrictions.
1088*9fcf78ccSJohn Johansen 		 */
1089*9fcf78ccSJohn Johansen 		if (task_no_new_privs(current) && !unconfined(label) &&
1090*9fcf78ccSJohn Johansen 		    !aa_label_is_subset(new, ctx->nnp)) {
1091*9fcf78ccSJohn Johansen 			/* not an apparmor denial per se, so don't log it */
1092*9fcf78ccSJohn Johansen 			AA_DEBUG("no_new_privs - change_hat denied");
1093*9fcf78ccSJohn Johansen 			error = -EPERM;
1094*9fcf78ccSJohn Johansen 			goto out;
1095*9fcf78ccSJohn Johansen 		}
1096*9fcf78ccSJohn Johansen 
109789dbf196SJohn Johansen 		if (flags & AA_CHANGE_TEST)
109889dbf196SJohn Johansen 			goto out;
1099898127c3SJohn Johansen 
110089dbf196SJohn Johansen 		target = new;
110189dbf196SJohn Johansen 		error = aa_set_current_hat(new, token);
1102898127c3SJohn Johansen 		if (error == -EACCES)
1103898127c3SJohn Johansen 			/* kill task in case of brute force attacks */
110489dbf196SJohn Johansen 			goto kill;
110589dbf196SJohn Johansen 	} else if (previous && !(flags & AA_CHANGE_TEST)) {
1106*9fcf78ccSJohn Johansen 		/*
1107*9fcf78ccSJohn Johansen 		 * no new privs prevents domain transitions that would
1108*9fcf78ccSJohn Johansen 		 * reduce restrictions.
1109*9fcf78ccSJohn Johansen 		 */
1110*9fcf78ccSJohn Johansen 		if (task_no_new_privs(current) && !unconfined(label) &&
1111*9fcf78ccSJohn Johansen 		    !aa_label_is_subset(previous, ctx->nnp)) {
1112*9fcf78ccSJohn Johansen 			/* not an apparmor denial per se, so don't log it */
1113*9fcf78ccSJohn Johansen 			AA_DEBUG("no_new_privs - change_hat denied");
1114*9fcf78ccSJohn Johansen 			error = -EPERM;
1115*9fcf78ccSJohn Johansen 			goto out;
1116*9fcf78ccSJohn Johansen 		}
1117*9fcf78ccSJohn Johansen 
111889dbf196SJohn Johansen 		/* Return to saved label.  Kill task if restore fails
1119898127c3SJohn Johansen 		 * to avoid brute force attacks
1120898127c3SJohn Johansen 		 */
112189dbf196SJohn Johansen 		target = previous;
1122637f688dSJohn Johansen 		error = aa_restore_previous_label(token);
112389dbf196SJohn Johansen 		if (error) {
112489dbf196SJohn Johansen 			if (error == -EACCES)
112589dbf196SJohn Johansen 				goto kill;
112689dbf196SJohn Johansen 			goto fail;
112789dbf196SJohn Johansen 		}
112889dbf196SJohn Johansen 	} /* else ignore @flags && restores when there is no saved profile */
1129898127c3SJohn Johansen 
1130898127c3SJohn Johansen out:
113189dbf196SJohn Johansen 	aa_put_label(new);
113289dbf196SJohn Johansen 	aa_put_label(previous);
1133637f688dSJohn Johansen 	aa_put_label(label);
1134898127c3SJohn Johansen 	put_cred(cred);
1135898127c3SJohn Johansen 
1136898127c3SJohn Johansen 	return error;
113789dbf196SJohn Johansen 
113889dbf196SJohn Johansen kill:
113989dbf196SJohn Johansen 	info = "failed token match";
114089dbf196SJohn Johansen 	perms.kill = AA_MAY_CHANGEHAT;
114189dbf196SJohn Johansen 
114289dbf196SJohn Johansen fail:
114389dbf196SJohn Johansen 	fn_for_each_in_ns(label, profile,
114489dbf196SJohn Johansen 		aa_audit_file(profile, &perms, OP_CHANGE_HAT,
114589dbf196SJohn Johansen 			      AA_MAY_CHANGEHAT, NULL, NULL, target,
114689dbf196SJohn Johansen 			      GLOBAL_ROOT_UID, info, error));
114789dbf196SJohn Johansen 
114889dbf196SJohn Johansen 	goto out;
1149898127c3SJohn Johansen }
1150898127c3SJohn Johansen 
115189dbf196SJohn Johansen 
1152e00b02bbSJohn Johansen static int change_profile_perms_wrapper(const char *op, const char *name,
1153e00b02bbSJohn Johansen 					struct aa_profile *profile,
1154e00b02bbSJohn Johansen 					struct aa_label *target, bool stack,
1155e00b02bbSJohn Johansen 					u32 request, struct aa_perms *perms)
1156e00b02bbSJohn Johansen {
1157e00b02bbSJohn Johansen 	const char *info = NULL;
1158e00b02bbSJohn Johansen 	int error = 0;
1159e00b02bbSJohn Johansen 
1160e00b02bbSJohn Johansen 	if (!error)
1161e00b02bbSJohn Johansen 		error = change_profile_perms(profile, target, stack, request,
1162e00b02bbSJohn Johansen 					     profile->file.start, perms);
1163e00b02bbSJohn Johansen 	if (error)
1164e00b02bbSJohn Johansen 		error = aa_audit_file(profile, perms, op, request, name,
1165e00b02bbSJohn Johansen 				      NULL, target, GLOBAL_ROOT_UID, info,
1166e00b02bbSJohn Johansen 				      error);
1167e00b02bbSJohn Johansen 
1168e00b02bbSJohn Johansen 	return error;
1169e00b02bbSJohn Johansen }
117089dbf196SJohn Johansen 
1171898127c3SJohn Johansen /**
1172898127c3SJohn Johansen  * aa_change_profile - perform a one-way profile transition
1173aa9a39adSJohn Johansen  * @fqname: name of profile may include namespace (NOT NULL)
1174898127c3SJohn Johansen  * @onexec: whether this transition is to take place immediately or at exec
1175df8073c6SJohn Johansen  * @flags: flags affecting change behavior
1176898127c3SJohn Johansen  *
1177898127c3SJohn Johansen  * Change to new profile @name.  Unlike with hats, there is no way
1178898127c3SJohn Johansen  * to change back.  If @name isn't specified the current profile name is
1179898127c3SJohn Johansen  * used.
1180898127c3SJohn Johansen  * If @onexec then the transition is delayed until
1181898127c3SJohn Johansen  * the next exec.
1182898127c3SJohn Johansen  *
1183898127c3SJohn Johansen  * Returns %0 on success, error otherwise.
1184898127c3SJohn Johansen  */
1185df8073c6SJohn Johansen int aa_change_profile(const char *fqname, int flags)
1186898127c3SJohn Johansen {
1187e00b02bbSJohn Johansen 	struct aa_label *label, *new = NULL, *target = NULL;
1188e00b02bbSJohn Johansen 	struct aa_profile *profile;
11892d679f3cSJohn Johansen 	struct aa_perms perms = {};
1190e00b02bbSJohn Johansen 	const char *info = NULL;
1191e00b02bbSJohn Johansen 	const char *auditname = fqname;		/* retain leading & if stack */
1192e00b02bbSJohn Johansen 	bool stack = flags & AA_CHANGE_STACK;
1193*9fcf78ccSJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
119447f6e5ccSJohn Johansen 	int error = 0;
1195e00b02bbSJohn Johansen 	char *op;
1196898127c3SJohn Johansen 	u32 request;
1197898127c3SJohn Johansen 
1198*9fcf78ccSJohn Johansen 	label = aa_get_current_label();
1199*9fcf78ccSJohn Johansen 
1200*9fcf78ccSJohn Johansen 	/*
1201*9fcf78ccSJohn Johansen 	 * Detect no new privs being set, and store the label it
1202*9fcf78ccSJohn Johansen 	 * occurred under. Ideally this would happen when nnp
1203*9fcf78ccSJohn Johansen 	 * is set but there isn't a good way to do that yet.
1204*9fcf78ccSJohn Johansen 	 *
1205*9fcf78ccSJohn Johansen 	 * Testing for unconfined must be done before the subset test
1206*9fcf78ccSJohn Johansen 	 */
1207*9fcf78ccSJohn Johansen 	if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1208*9fcf78ccSJohn Johansen 		ctx->nnp = aa_get_label(label);
1209*9fcf78ccSJohn Johansen 
1210aa9a39adSJohn Johansen 	if (!fqname || !*fqname) {
1211aa9a39adSJohn Johansen 		AA_DEBUG("no profile name");
1212898127c3SJohn Johansen 		return -EINVAL;
1213aa9a39adSJohn Johansen 	}
1214898127c3SJohn Johansen 
1215df8073c6SJohn Johansen 	if (flags & AA_CHANGE_ONEXEC) {
1216898127c3SJohn Johansen 		request = AA_MAY_ONEXEC;
1217e00b02bbSJohn Johansen 		if (stack)
1218e00b02bbSJohn Johansen 			op = OP_STACK_ONEXEC;
1219e00b02bbSJohn Johansen 		else
1220898127c3SJohn Johansen 			op = OP_CHANGE_ONEXEC;
1221898127c3SJohn Johansen 	} else {
1222898127c3SJohn Johansen 		request = AA_MAY_CHANGE_PROFILE;
1223e00b02bbSJohn Johansen 		if (stack)
1224e00b02bbSJohn Johansen 			op = OP_STACK;
1225e00b02bbSJohn Johansen 		else
1226898127c3SJohn Johansen 			op = OP_CHANGE_PROFILE;
1227898127c3SJohn Johansen 	}
1228898127c3SJohn Johansen 
1229e00b02bbSJohn Johansen 	label = aa_get_current_label();
1230898127c3SJohn Johansen 
1231e00b02bbSJohn Johansen 	if (*fqname == '&') {
1232e00b02bbSJohn Johansen 		stack = true;
1233e00b02bbSJohn Johansen 		/* don't have label_parse() do stacking */
1234e00b02bbSJohn Johansen 		fqname++;
1235c29bceb3SJohn Johansen 	}
1236e00b02bbSJohn Johansen 	target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1237e00b02bbSJohn Johansen 	if (IS_ERR(target)) {
1238e00b02bbSJohn Johansen 		struct aa_profile *tprofile;
1239c29bceb3SJohn Johansen 
1240e00b02bbSJohn Johansen 		info = "label not found";
1241e00b02bbSJohn Johansen 		error = PTR_ERR(target);
1242e00b02bbSJohn Johansen 		target = NULL;
1243e00b02bbSJohn Johansen 		/*
1244e00b02bbSJohn Johansen 		 * TODO: fixme using labels_profile is not right - do profile
1245e00b02bbSJohn Johansen 		 * per complain profile
1246e00b02bbSJohn Johansen 		 */
1247df8073c6SJohn Johansen 		if ((flags & AA_CHANGE_TEST) ||
1248e00b02bbSJohn Johansen 		    !COMPLAIN_MODE(labels_profile(label)))
1249898127c3SJohn Johansen 			goto audit;
1250898127c3SJohn Johansen 		/* released below */
1251e00b02bbSJohn Johansen 		tprofile = aa_new_null_profile(labels_profile(label), false,
1252e00b02bbSJohn Johansen 					       fqname, GFP_KERNEL);
1253e00b02bbSJohn Johansen 		if (!tprofile) {
1254898127c3SJohn Johansen 			info = "failed null profile create";
1255898127c3SJohn Johansen 			error = -ENOMEM;
1256898127c3SJohn Johansen 			goto audit;
1257898127c3SJohn Johansen 		}
1258e00b02bbSJohn Johansen 		target = &tprofile->label;
1259e00b02bbSJohn Johansen 		goto check;
1260898127c3SJohn Johansen 	}
1261898127c3SJohn Johansen 
1262e00b02bbSJohn Johansen 	/*
1263e00b02bbSJohn Johansen 	 * self directed transitions only apply to current policy ns
1264e00b02bbSJohn Johansen 	 * TODO: currently requiring perms for stacking and straight change
1265e00b02bbSJohn Johansen 	 *       stacking doesn't strictly need this. Determine how much
1266e00b02bbSJohn Johansen 	 *       we want to loosen this restriction for stacking
1267e00b02bbSJohn Johansen 	 *
1268e00b02bbSJohn Johansen 	 * if (!stack) {
1269e00b02bbSJohn Johansen 	 */
1270e00b02bbSJohn Johansen 	error = fn_for_each_in_ns(label, profile,
1271e00b02bbSJohn Johansen 			change_profile_perms_wrapper(op, auditname,
1272e00b02bbSJohn Johansen 						     profile, target, stack,
1273e00b02bbSJohn Johansen 						     request, &perms));
1274e00b02bbSJohn Johansen 	if (error)
1275e00b02bbSJohn Johansen 		/* auditing done in change_profile_perms_wrapper */
1276e00b02bbSJohn Johansen 		goto out;
1277aa9a39adSJohn Johansen 
1278e00b02bbSJohn Johansen 	/* } */
1279e00b02bbSJohn Johansen 
1280e00b02bbSJohn Johansen check:
1281898127c3SJohn Johansen 	/* check if tracing task is allowed to trace target domain */
1282e00b02bbSJohn Johansen 	error = may_change_ptraced_domain(target, &info);
1283e00b02bbSJohn Johansen 	if (error && !fn_for_each_in_ns(label, profile,
1284e00b02bbSJohn Johansen 					COMPLAIN_MODE(profile)))
1285e00b02bbSJohn Johansen 		goto audit;
1286e00b02bbSJohn Johansen 
1287e00b02bbSJohn Johansen 	/* TODO: add permission check to allow this
1288e00b02bbSJohn Johansen 	 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
1289e00b02bbSJohn Johansen 	 *      info = "not a single threaded task";
1290e00b02bbSJohn Johansen 	 *      error = -EACCES;
1291e00b02bbSJohn Johansen 	 *      goto audit;
1292e00b02bbSJohn Johansen 	 * }
1293e00b02bbSJohn Johansen 	 */
1294e00b02bbSJohn Johansen 	if (flags & AA_CHANGE_TEST)
1295e00b02bbSJohn Johansen 		goto out;
1296e00b02bbSJohn Johansen 
1297*9fcf78ccSJohn Johansen 	/* stacking is always a subset, so only check the nonstack case */
1298*9fcf78ccSJohn Johansen 	if (!stack) {
1299*9fcf78ccSJohn Johansen 		new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1300*9fcf78ccSJohn Johansen 					   aa_get_label(target),
1301*9fcf78ccSJohn Johansen 					   aa_get_label(&profile->label));
1302*9fcf78ccSJohn Johansen 		/*
1303*9fcf78ccSJohn Johansen 		 * no new privs prevents domain transitions that would
1304*9fcf78ccSJohn Johansen 		 * reduce restrictions.
1305*9fcf78ccSJohn Johansen 		 */
1306*9fcf78ccSJohn Johansen 		if (task_no_new_privs(current) && !unconfined(label) &&
1307*9fcf78ccSJohn Johansen 		    !aa_label_is_subset(new, ctx->nnp)) {
1308*9fcf78ccSJohn Johansen 			/* not an apparmor denial per se, so don't log it */
1309*9fcf78ccSJohn Johansen 			AA_DEBUG("no_new_privs - change_hat denied");
1310*9fcf78ccSJohn Johansen 			error = -EPERM;
1311*9fcf78ccSJohn Johansen 			goto out;
1312*9fcf78ccSJohn Johansen 		}
1313*9fcf78ccSJohn Johansen 	}
1314*9fcf78ccSJohn Johansen 
1315e00b02bbSJohn Johansen 	if (!(flags & AA_CHANGE_ONEXEC)) {
1316e00b02bbSJohn Johansen 		/* only transition profiles in the current ns */
1317e00b02bbSJohn Johansen 		if (stack)
1318e00b02bbSJohn Johansen 			new = aa_label_merge(label, target, GFP_KERNEL);
1319e00b02bbSJohn Johansen 		if (IS_ERR_OR_NULL(new)) {
1320e00b02bbSJohn Johansen 			info = "failed to build target label";
1321e00b02bbSJohn Johansen 			error = PTR_ERR(new);
1322e00b02bbSJohn Johansen 			new = NULL;
1323e00b02bbSJohn Johansen 			perms.allow = 0;
1324898127c3SJohn Johansen 			goto audit;
1325898127c3SJohn Johansen 		}
1326e00b02bbSJohn Johansen 		error = aa_replace_current_label(new);
1327*9fcf78ccSJohn Johansen 	} else {
1328*9fcf78ccSJohn Johansen 		if (new) {
1329*9fcf78ccSJohn Johansen 			aa_put_label(new);
1330*9fcf78ccSJohn Johansen 			new = NULL;
1331*9fcf78ccSJohn Johansen 		}
1332*9fcf78ccSJohn Johansen 
1333e00b02bbSJohn Johansen 		/* full transition will be built in exec path */
1334e00b02bbSJohn Johansen 		error = aa_set_current_onexec(target, stack);
1335*9fcf78ccSJohn Johansen 	}
1336898127c3SJohn Johansen 
1337898127c3SJohn Johansen audit:
1338e00b02bbSJohn Johansen 	error = fn_for_each_in_ns(label, profile,
1339e00b02bbSJohn Johansen 			aa_audit_file(profile, &perms, op, request, auditname,
1340e00b02bbSJohn Johansen 				      NULL, new ? new : target,
1341e00b02bbSJohn Johansen 				      GLOBAL_ROOT_UID, info, error));
1342898127c3SJohn Johansen 
1343e00b02bbSJohn Johansen out:
1344e00b02bbSJohn Johansen 	aa_put_label(new);
1345e00b02bbSJohn Johansen 	aa_put_label(target);
1346637f688dSJohn Johansen 	aa_put_label(label);
1347898127c3SJohn Johansen 
1348898127c3SJohn Johansen 	return error;
1349898127c3SJohn Johansen }
1350