xref: /OK3568_Linux_fs/kernel/security/apparmor/domain.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * AppArmor security module
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This file contains AppArmor policy attachment and domain transitions
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 2002-2008 Novell/SUSE
8*4882a593Smuzhiyun  * Copyright 2009-2010 Canonical Ltd.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/errno.h>
12*4882a593Smuzhiyun #include <linux/fdtable.h>
13*4882a593Smuzhiyun #include <linux/file.h>
14*4882a593Smuzhiyun #include <linux/mount.h>
15*4882a593Smuzhiyun #include <linux/syscalls.h>
16*4882a593Smuzhiyun #include <linux/tracehook.h>
17*4882a593Smuzhiyun #include <linux/personality.h>
18*4882a593Smuzhiyun #include <linux/xattr.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "include/audit.h"
21*4882a593Smuzhiyun #include "include/apparmorfs.h"
22*4882a593Smuzhiyun #include "include/cred.h"
23*4882a593Smuzhiyun #include "include/domain.h"
24*4882a593Smuzhiyun #include "include/file.h"
25*4882a593Smuzhiyun #include "include/ipc.h"
26*4882a593Smuzhiyun #include "include/match.h"
27*4882a593Smuzhiyun #include "include/path.h"
28*4882a593Smuzhiyun #include "include/policy.h"
29*4882a593Smuzhiyun #include "include/policy_ns.h"
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun /**
32*4882a593Smuzhiyun  * aa_free_domain_entries - free entries in a domain table
33*4882a593Smuzhiyun  * @domain: the domain table to free  (MAYBE NULL)
34*4882a593Smuzhiyun  */
aa_free_domain_entries(struct aa_domain * domain)35*4882a593Smuzhiyun void aa_free_domain_entries(struct aa_domain *domain)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	int i;
38*4882a593Smuzhiyun 	if (domain) {
39*4882a593Smuzhiyun 		if (!domain->table)
40*4882a593Smuzhiyun 			return;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 		for (i = 0; i < domain->size; i++)
43*4882a593Smuzhiyun 			kfree_sensitive(domain->table[i]);
44*4882a593Smuzhiyun 		kfree_sensitive(domain->table);
45*4882a593Smuzhiyun 		domain->table = NULL;
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun /**
50*4882a593Smuzhiyun  * may_change_ptraced_domain - check if can change profile on ptraced task
51*4882a593Smuzhiyun  * @to_label: profile to change to  (NOT NULL)
52*4882a593Smuzhiyun  * @info: message if there is an error
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * Check if current is ptraced and if so if the tracing task is allowed
55*4882a593Smuzhiyun  * to trace the new domain
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * Returns: %0 or error if change not allowed
58*4882a593Smuzhiyun  */
may_change_ptraced_domain(struct aa_label * to_label,const char ** info)59*4882a593Smuzhiyun static int may_change_ptraced_domain(struct aa_label *to_label,
60*4882a593Smuzhiyun 				     const char **info)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	struct task_struct *tracer;
63*4882a593Smuzhiyun 	struct aa_label *tracerl = NULL;
64*4882a593Smuzhiyun 	int error = 0;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	rcu_read_lock();
67*4882a593Smuzhiyun 	tracer = ptrace_parent(current);
68*4882a593Smuzhiyun 	if (tracer)
69*4882a593Smuzhiyun 		/* released below */
70*4882a593Smuzhiyun 		tracerl = aa_get_task_label(tracer);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	/* not ptraced */
73*4882a593Smuzhiyun 	if (!tracer || unconfined(tracerl))
74*4882a593Smuzhiyun 		goto out;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	error = aa_may_ptrace(tracerl, to_label, PTRACE_MODE_ATTACH);
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun out:
79*4882a593Smuzhiyun 	rcu_read_unlock();
80*4882a593Smuzhiyun 	aa_put_label(tracerl);
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	if (error)
83*4882a593Smuzhiyun 		*info = "ptrace prevents transition";
84*4882a593Smuzhiyun 	return error;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun /**** TODO: dedup to aa_label_match - needs perm and dfa, merging
88*4882a593Smuzhiyun  * specifically this is an exact copy of aa_label_match except
89*4882a593Smuzhiyun  * aa_compute_perms is replaced with aa_compute_fperms
90*4882a593Smuzhiyun  * and policy.dfa with file.dfa
91*4882a593Smuzhiyun  ****/
92*4882a593Smuzhiyun /* match a profile and its associated ns component if needed
93*4882a593Smuzhiyun  * Assumes visibility test has already been done.
94*4882a593Smuzhiyun  * If a subns profile is not to be matched should be prescreened with
95*4882a593Smuzhiyun  * visibility test.
96*4882a593Smuzhiyun  */
match_component(struct aa_profile * profile,struct aa_profile * tp,bool stack,unsigned int state)97*4882a593Smuzhiyun static inline unsigned int match_component(struct aa_profile *profile,
98*4882a593Smuzhiyun 					   struct aa_profile *tp,
99*4882a593Smuzhiyun 					   bool stack, unsigned int state)
100*4882a593Smuzhiyun {
101*4882a593Smuzhiyun 	const char *ns_name;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	if (stack)
104*4882a593Smuzhiyun 		state = aa_dfa_match(profile->file.dfa, state, "&");
105*4882a593Smuzhiyun 	if (profile->ns == tp->ns)
106*4882a593Smuzhiyun 		return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	/* try matching with namespace name and then profile */
109*4882a593Smuzhiyun 	ns_name = aa_ns_name(profile->ns, tp->ns, true);
110*4882a593Smuzhiyun 	state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
111*4882a593Smuzhiyun 	state = aa_dfa_match(profile->file.dfa, state, ns_name);
112*4882a593Smuzhiyun 	state = aa_dfa_match_len(profile->file.dfa, state, ":", 1);
113*4882a593Smuzhiyun 	return aa_dfa_match(profile->file.dfa, state, tp->base.hname);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun /**
117*4882a593Smuzhiyun  * label_compound_match - find perms for full compound label
118*4882a593Smuzhiyun  * @profile: profile to find perms for
119*4882a593Smuzhiyun  * @label: label to check access permissions for
120*4882a593Smuzhiyun  * @stack: whether this is a stacking request
121*4882a593Smuzhiyun  * @start: state to start match in
122*4882a593Smuzhiyun  * @subns: whether to do permission checks on components in a subns
123*4882a593Smuzhiyun  * @request: permissions to request
124*4882a593Smuzhiyun  * @perms: perms struct to set
125*4882a593Smuzhiyun  *
126*4882a593Smuzhiyun  * Returns: 0 on success else ERROR
127*4882a593Smuzhiyun  *
128*4882a593Smuzhiyun  * For the label A//&B//&C this does the perm match for A//&B//&C
129*4882a593Smuzhiyun  * @perms should be preinitialized with allperms OR a previous permission
130*4882a593Smuzhiyun  *        check to be stacked.
131*4882a593Smuzhiyun  */
label_compound_match(struct aa_profile * profile,struct aa_label * label,bool stack,unsigned int state,bool subns,u32 request,struct aa_perms * perms)132*4882a593Smuzhiyun static int label_compound_match(struct aa_profile *profile,
133*4882a593Smuzhiyun 				struct aa_label *label, bool stack,
134*4882a593Smuzhiyun 				unsigned int state, bool subns, u32 request,
135*4882a593Smuzhiyun 				struct aa_perms *perms)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	struct aa_profile *tp;
138*4882a593Smuzhiyun 	struct label_it i;
139*4882a593Smuzhiyun 	struct path_cond cond = { };
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	/* find first subcomponent that is visible */
142*4882a593Smuzhiyun 	label_for_each(i, label, tp) {
143*4882a593Smuzhiyun 		if (!aa_ns_visible(profile->ns, tp->ns, subns))
144*4882a593Smuzhiyun 			continue;
145*4882a593Smuzhiyun 		state = match_component(profile, tp, stack, state);
146*4882a593Smuzhiyun 		if (!state)
147*4882a593Smuzhiyun 			goto fail;
148*4882a593Smuzhiyun 		goto next;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	/* no component visible */
152*4882a593Smuzhiyun 	*perms = allperms;
153*4882a593Smuzhiyun 	return 0;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun next:
156*4882a593Smuzhiyun 	label_for_each_cont(i, label, tp) {
157*4882a593Smuzhiyun 		if (!aa_ns_visible(profile->ns, tp->ns, subns))
158*4882a593Smuzhiyun 			continue;
159*4882a593Smuzhiyun 		state = aa_dfa_match(profile->file.dfa, state, "//&");
160*4882a593Smuzhiyun 		state = match_component(profile, tp, false, state);
161*4882a593Smuzhiyun 		if (!state)
162*4882a593Smuzhiyun 			goto fail;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 	*perms = aa_compute_fperms(profile->file.dfa, state, &cond);
165*4882a593Smuzhiyun 	aa_apply_modes_to_perms(profile, perms);
166*4882a593Smuzhiyun 	if ((perms->allow & request) != request)
167*4882a593Smuzhiyun 		return -EACCES;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return 0;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun fail:
172*4882a593Smuzhiyun 	*perms = nullperms;
173*4882a593Smuzhiyun 	return -EACCES;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun /**
177*4882a593Smuzhiyun  * label_components_match - find perms for all subcomponents of a label
178*4882a593Smuzhiyun  * @profile: profile to find perms for
179*4882a593Smuzhiyun  * @label: label to check access permissions for
180*4882a593Smuzhiyun  * @stack: whether this is a stacking request
181*4882a593Smuzhiyun  * @start: state to start match in
182*4882a593Smuzhiyun  * @subns: whether to do permission checks on components in a subns
183*4882a593Smuzhiyun  * @request: permissions to request
184*4882a593Smuzhiyun  * @perms: an initialized perms struct to add accumulation to
185*4882a593Smuzhiyun  *
186*4882a593Smuzhiyun  * Returns: 0 on success else ERROR
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  * For the label A//&B//&C this does the perm match for each of A and B and C
189*4882a593Smuzhiyun  * @perms should be preinitialized with allperms OR a previous permission
190*4882a593Smuzhiyun  *        check to be stacked.
191*4882a593Smuzhiyun  */
label_components_match(struct aa_profile * profile,struct aa_label * label,bool stack,unsigned int start,bool subns,u32 request,struct aa_perms * perms)192*4882a593Smuzhiyun static int label_components_match(struct aa_profile *profile,
193*4882a593Smuzhiyun 				  struct aa_label *label, bool stack,
194*4882a593Smuzhiyun 				  unsigned int start, bool subns, u32 request,
195*4882a593Smuzhiyun 				  struct aa_perms *perms)
196*4882a593Smuzhiyun {
197*4882a593Smuzhiyun 	struct aa_profile *tp;
198*4882a593Smuzhiyun 	struct label_it i;
199*4882a593Smuzhiyun 	struct aa_perms tmp;
200*4882a593Smuzhiyun 	struct path_cond cond = { };
201*4882a593Smuzhiyun 	unsigned int state = 0;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 	/* find first subcomponent to test */
204*4882a593Smuzhiyun 	label_for_each(i, label, tp) {
205*4882a593Smuzhiyun 		if (!aa_ns_visible(profile->ns, tp->ns, subns))
206*4882a593Smuzhiyun 			continue;
207*4882a593Smuzhiyun 		state = match_component(profile, tp, stack, start);
208*4882a593Smuzhiyun 		if (!state)
209*4882a593Smuzhiyun 			goto fail;
210*4882a593Smuzhiyun 		goto next;
211*4882a593Smuzhiyun 	}
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* no subcomponents visible - no change in perms */
214*4882a593Smuzhiyun 	return 0;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun next:
217*4882a593Smuzhiyun 	tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
218*4882a593Smuzhiyun 	aa_apply_modes_to_perms(profile, &tmp);
219*4882a593Smuzhiyun 	aa_perms_accum(perms, &tmp);
220*4882a593Smuzhiyun 	label_for_each_cont(i, label, tp) {
221*4882a593Smuzhiyun 		if (!aa_ns_visible(profile->ns, tp->ns, subns))
222*4882a593Smuzhiyun 			continue;
223*4882a593Smuzhiyun 		state = match_component(profile, tp, stack, start);
224*4882a593Smuzhiyun 		if (!state)
225*4882a593Smuzhiyun 			goto fail;
226*4882a593Smuzhiyun 		tmp = aa_compute_fperms(profile->file.dfa, state, &cond);
227*4882a593Smuzhiyun 		aa_apply_modes_to_perms(profile, &tmp);
228*4882a593Smuzhiyun 		aa_perms_accum(perms, &tmp);
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	if ((perms->allow & request) != request)
232*4882a593Smuzhiyun 		return -EACCES;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	return 0;
235*4882a593Smuzhiyun 
236*4882a593Smuzhiyun fail:
237*4882a593Smuzhiyun 	*perms = nullperms;
238*4882a593Smuzhiyun 	return -EACCES;
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun /**
242*4882a593Smuzhiyun  * label_match - do a multi-component label match
243*4882a593Smuzhiyun  * @profile: profile to match against (NOT NULL)
244*4882a593Smuzhiyun  * @label: label to match (NOT NULL)
245*4882a593Smuzhiyun  * @stack: whether this is a stacking request
246*4882a593Smuzhiyun  * @state: state to start in
247*4882a593Smuzhiyun  * @subns: whether to match subns components
248*4882a593Smuzhiyun  * @request: permission request
249*4882a593Smuzhiyun  * @perms: Returns computed perms (NOT NULL)
250*4882a593Smuzhiyun  *
251*4882a593Smuzhiyun  * Returns: the state the match finished in, may be the none matching state
252*4882a593Smuzhiyun  */
label_match(struct aa_profile * profile,struct aa_label * label,bool stack,unsigned int state,bool subns,u32 request,struct aa_perms * perms)253*4882a593Smuzhiyun static int label_match(struct aa_profile *profile, struct aa_label *label,
254*4882a593Smuzhiyun 		       bool stack, unsigned int state, bool subns, u32 request,
255*4882a593Smuzhiyun 		       struct aa_perms *perms)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun 	int error;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	*perms = nullperms;
260*4882a593Smuzhiyun 	error = label_compound_match(profile, label, stack, state, subns,
261*4882a593Smuzhiyun 				     request, perms);
262*4882a593Smuzhiyun 	if (!error)
263*4882a593Smuzhiyun 		return error;
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	*perms = allperms;
266*4882a593Smuzhiyun 	return label_components_match(profile, label, stack, state, subns,
267*4882a593Smuzhiyun 				      request, perms);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun /******* end TODO: dedup *****/
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun /**
273*4882a593Smuzhiyun  * change_profile_perms - find permissions for change_profile
274*4882a593Smuzhiyun  * @profile: the current profile  (NOT NULL)
275*4882a593Smuzhiyun  * @target: label to transition to (NOT NULL)
276*4882a593Smuzhiyun  * @stack: whether this is a stacking request
277*4882a593Smuzhiyun  * @request: requested perms
278*4882a593Smuzhiyun  * @start: state to start matching in
279*4882a593Smuzhiyun  *
280*4882a593Smuzhiyun  *
281*4882a593Smuzhiyun  * Returns: permission set
282*4882a593Smuzhiyun  *
283*4882a593Smuzhiyun  * currently only matches full label A//&B//&C or individual components A, B, C
284*4882a593Smuzhiyun  * not arbitrary combinations. Eg. A//&B, C
285*4882a593Smuzhiyun  */
change_profile_perms(struct aa_profile * profile,struct aa_label * target,bool stack,u32 request,unsigned int start,struct aa_perms * perms)286*4882a593Smuzhiyun static int change_profile_perms(struct aa_profile *profile,
287*4882a593Smuzhiyun 				struct aa_label *target, bool stack,
288*4882a593Smuzhiyun 				u32 request, unsigned int start,
289*4882a593Smuzhiyun 				struct aa_perms *perms)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	if (profile_unconfined(profile)) {
292*4882a593Smuzhiyun 		perms->allow = AA_MAY_CHANGE_PROFILE | AA_MAY_ONEXEC;
293*4882a593Smuzhiyun 		perms->audit = perms->quiet = perms->kill = 0;
294*4882a593Smuzhiyun 		return 0;
295*4882a593Smuzhiyun 	}
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	/* TODO: add profile in ns screening */
298*4882a593Smuzhiyun 	return label_match(profile, target, stack, start, true, request, perms);
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun /**
302*4882a593Smuzhiyun  * aa_xattrs_match - check whether a file matches the xattrs defined in profile
303*4882a593Smuzhiyun  * @bprm: binprm struct for the process to validate
304*4882a593Smuzhiyun  * @profile: profile to match against (NOT NULL)
305*4882a593Smuzhiyun  * @state: state to start match in
306*4882a593Smuzhiyun  *
307*4882a593Smuzhiyun  * Returns: number of extended attributes that matched, or < 0 on error
308*4882a593Smuzhiyun  */
aa_xattrs_match(const struct linux_binprm * bprm,struct aa_profile * profile,unsigned int state)309*4882a593Smuzhiyun static int aa_xattrs_match(const struct linux_binprm *bprm,
310*4882a593Smuzhiyun 			   struct aa_profile *profile, unsigned int state)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun 	int i;
313*4882a593Smuzhiyun 	ssize_t size;
314*4882a593Smuzhiyun 	struct dentry *d;
315*4882a593Smuzhiyun 	char *value = NULL;
316*4882a593Smuzhiyun 	int value_size = 0, ret = profile->xattr_count;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	if (!bprm || !profile->xattr_count)
319*4882a593Smuzhiyun 		return 0;
320*4882a593Smuzhiyun 	might_sleep();
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	/* transition from exec match to xattr set */
323*4882a593Smuzhiyun 	state = aa_dfa_outofband_transition(profile->xmatch, state);
324*4882a593Smuzhiyun 	d = bprm->file->f_path.dentry;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	for (i = 0; i < profile->xattr_count; i++) {
327*4882a593Smuzhiyun 		size = vfs_getxattr_alloc(d, profile->xattrs[i], &value,
328*4882a593Smuzhiyun 					  value_size, GFP_KERNEL);
329*4882a593Smuzhiyun 		if (size >= 0) {
330*4882a593Smuzhiyun 			u32 perm;
331*4882a593Smuzhiyun 
332*4882a593Smuzhiyun 			/*
333*4882a593Smuzhiyun 			 * Check the xattr presence before value. This ensure
334*4882a593Smuzhiyun 			 * that not present xattr can be distinguished from a 0
335*4882a593Smuzhiyun 			 * length value or rule that matches any value
336*4882a593Smuzhiyun 			 */
337*4882a593Smuzhiyun 			state = aa_dfa_null_transition(profile->xmatch, state);
338*4882a593Smuzhiyun 			/* Check xattr value */
339*4882a593Smuzhiyun 			state = aa_dfa_match_len(profile->xmatch, state, value,
340*4882a593Smuzhiyun 						 size);
341*4882a593Smuzhiyun 			perm = dfa_user_allow(profile->xmatch, state);
342*4882a593Smuzhiyun 			if (!(perm & MAY_EXEC)) {
343*4882a593Smuzhiyun 				ret = -EINVAL;
344*4882a593Smuzhiyun 				goto out;
345*4882a593Smuzhiyun 			}
346*4882a593Smuzhiyun 		}
347*4882a593Smuzhiyun 		/* transition to next element */
348*4882a593Smuzhiyun 		state = aa_dfa_outofband_transition(profile->xmatch, state);
349*4882a593Smuzhiyun 		if (size < 0) {
350*4882a593Smuzhiyun 			/*
351*4882a593Smuzhiyun 			 * No xattr match, so verify if transition to
352*4882a593Smuzhiyun 			 * next element was valid. IFF so the xattr
353*4882a593Smuzhiyun 			 * was optional.
354*4882a593Smuzhiyun 			 */
355*4882a593Smuzhiyun 			if (!state) {
356*4882a593Smuzhiyun 				ret = -EINVAL;
357*4882a593Smuzhiyun 				goto out;
358*4882a593Smuzhiyun 			}
359*4882a593Smuzhiyun 			/* don't count missing optional xattr as matched */
360*4882a593Smuzhiyun 			ret--;
361*4882a593Smuzhiyun 		}
362*4882a593Smuzhiyun 	}
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun out:
365*4882a593Smuzhiyun 	kfree(value);
366*4882a593Smuzhiyun 	return ret;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun /**
370*4882a593Smuzhiyun  * find_attach - do attachment search for unconfined processes
371*4882a593Smuzhiyun  * @bprm - binprm structure of transitioning task
372*4882a593Smuzhiyun  * @ns: the current namespace  (NOT NULL)
373*4882a593Smuzhiyun  * @head - profile list to walk  (NOT NULL)
374*4882a593Smuzhiyun  * @name - to match against  (NOT NULL)
375*4882a593Smuzhiyun  * @info - info message if there was an error (NOT NULL)
376*4882a593Smuzhiyun  *
377*4882a593Smuzhiyun  * Do a linear search on the profiles in the list.  There is a matching
378*4882a593Smuzhiyun  * preference where an exact match is preferred over a name which uses
379*4882a593Smuzhiyun  * expressions to match, and matching expressions with the greatest
380*4882a593Smuzhiyun  * xmatch_len are preferred.
381*4882a593Smuzhiyun  *
382*4882a593Smuzhiyun  * Requires: @head not be shared or have appropriate locks held
383*4882a593Smuzhiyun  *
384*4882a593Smuzhiyun  * Returns: label or NULL if no match found
385*4882a593Smuzhiyun  */
find_attach(const struct linux_binprm * bprm,struct aa_ns * ns,struct list_head * head,const char * name,const char ** info)386*4882a593Smuzhiyun static struct aa_label *find_attach(const struct linux_binprm *bprm,
387*4882a593Smuzhiyun 				    struct aa_ns *ns, struct list_head *head,
388*4882a593Smuzhiyun 				    const char *name, const char **info)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	int candidate_len = 0, candidate_xattrs = 0;
391*4882a593Smuzhiyun 	bool conflict = false;
392*4882a593Smuzhiyun 	struct aa_profile *profile, *candidate = NULL;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	AA_BUG(!name);
395*4882a593Smuzhiyun 	AA_BUG(!head);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	rcu_read_lock();
398*4882a593Smuzhiyun restart:
399*4882a593Smuzhiyun 	list_for_each_entry_rcu(profile, head, base.list) {
400*4882a593Smuzhiyun 		if (profile->label.flags & FLAG_NULL &&
401*4882a593Smuzhiyun 		    &profile->label == ns_unconfined(profile->ns))
402*4882a593Smuzhiyun 			continue;
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 		/* Find the "best" matching profile. Profiles must
405*4882a593Smuzhiyun 		 * match the path and extended attributes (if any)
406*4882a593Smuzhiyun 		 * associated with the file. A more specific path
407*4882a593Smuzhiyun 		 * match will be preferred over a less specific one,
408*4882a593Smuzhiyun 		 * and a match with more matching extended attributes
409*4882a593Smuzhiyun 		 * will be preferred over one with fewer. If the best
410*4882a593Smuzhiyun 		 * match has both the same level of path specificity
411*4882a593Smuzhiyun 		 * and the same number of matching extended attributes
412*4882a593Smuzhiyun 		 * as another profile, signal a conflict and refuse to
413*4882a593Smuzhiyun 		 * match.
414*4882a593Smuzhiyun 		 */
415*4882a593Smuzhiyun 		if (profile->xmatch) {
416*4882a593Smuzhiyun 			unsigned int state, count;
417*4882a593Smuzhiyun 			u32 perm;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 			state = aa_dfa_leftmatch(profile->xmatch, DFA_START,
420*4882a593Smuzhiyun 						 name, &count);
421*4882a593Smuzhiyun 			perm = dfa_user_allow(profile->xmatch, state);
422*4882a593Smuzhiyun 			/* any accepting state means a valid match. */
423*4882a593Smuzhiyun 			if (perm & MAY_EXEC) {
424*4882a593Smuzhiyun 				int ret = 0;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 				if (count < candidate_len)
427*4882a593Smuzhiyun 					continue;
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun 				if (bprm && profile->xattr_count) {
430*4882a593Smuzhiyun 					long rev = READ_ONCE(ns->revision);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 					if (!aa_get_profile_not0(profile))
433*4882a593Smuzhiyun 						goto restart;
434*4882a593Smuzhiyun 					rcu_read_unlock();
435*4882a593Smuzhiyun 					ret = aa_xattrs_match(bprm, profile,
436*4882a593Smuzhiyun 							      state);
437*4882a593Smuzhiyun 					rcu_read_lock();
438*4882a593Smuzhiyun 					aa_put_profile(profile);
439*4882a593Smuzhiyun 					if (rev !=
440*4882a593Smuzhiyun 					    READ_ONCE(ns->revision))
441*4882a593Smuzhiyun 						/* policy changed */
442*4882a593Smuzhiyun 						goto restart;
443*4882a593Smuzhiyun 					/*
444*4882a593Smuzhiyun 					 * Fail matching if the xattrs don't
445*4882a593Smuzhiyun 					 * match
446*4882a593Smuzhiyun 					 */
447*4882a593Smuzhiyun 					if (ret < 0)
448*4882a593Smuzhiyun 						continue;
449*4882a593Smuzhiyun 				}
450*4882a593Smuzhiyun 				/*
451*4882a593Smuzhiyun 				 * TODO: allow for more flexible best match
452*4882a593Smuzhiyun 				 *
453*4882a593Smuzhiyun 				 * The new match isn't more specific
454*4882a593Smuzhiyun 				 * than the current best match
455*4882a593Smuzhiyun 				 */
456*4882a593Smuzhiyun 				if (count == candidate_len &&
457*4882a593Smuzhiyun 				    ret <= candidate_xattrs) {
458*4882a593Smuzhiyun 					/* Match is equivalent, so conflict */
459*4882a593Smuzhiyun 					if (ret == candidate_xattrs)
460*4882a593Smuzhiyun 						conflict = true;
461*4882a593Smuzhiyun 					continue;
462*4882a593Smuzhiyun 				}
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 				/* Either the same length with more matching
465*4882a593Smuzhiyun 				 * xattrs, or a longer match
466*4882a593Smuzhiyun 				 */
467*4882a593Smuzhiyun 				candidate = profile;
468*4882a593Smuzhiyun 				candidate_len = max(count, profile->xmatch_len);
469*4882a593Smuzhiyun 				candidate_xattrs = ret;
470*4882a593Smuzhiyun 				conflict = false;
471*4882a593Smuzhiyun 			}
472*4882a593Smuzhiyun 		} else if (!strcmp(profile->base.name, name)) {
473*4882a593Smuzhiyun 			/*
474*4882a593Smuzhiyun 			 * old exact non-re match, without conditionals such
475*4882a593Smuzhiyun 			 * as xattrs. no more searching required
476*4882a593Smuzhiyun 			 */
477*4882a593Smuzhiyun 			candidate = profile;
478*4882a593Smuzhiyun 			goto out;
479*4882a593Smuzhiyun 		}
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	if (!candidate || conflict) {
483*4882a593Smuzhiyun 		if (conflict)
484*4882a593Smuzhiyun 			*info = "conflicting profile attachments";
485*4882a593Smuzhiyun 		rcu_read_unlock();
486*4882a593Smuzhiyun 		return NULL;
487*4882a593Smuzhiyun 	}
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun out:
490*4882a593Smuzhiyun 	candidate = aa_get_newest_profile(candidate);
491*4882a593Smuzhiyun 	rcu_read_unlock();
492*4882a593Smuzhiyun 
493*4882a593Smuzhiyun 	return &candidate->label;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun 
next_name(int xtype,const char * name)496*4882a593Smuzhiyun static const char *next_name(int xtype, const char *name)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun 	return NULL;
499*4882a593Smuzhiyun }
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun /**
502*4882a593Smuzhiyun  * x_table_lookup - lookup an x transition name via transition table
503*4882a593Smuzhiyun  * @profile: current profile (NOT NULL)
504*4882a593Smuzhiyun  * @xindex: index into x transition table
505*4882a593Smuzhiyun  * @name: returns: name tested to find label (NOT NULL)
506*4882a593Smuzhiyun  *
507*4882a593Smuzhiyun  * Returns: refcounted label, or NULL on failure (MAYBE NULL)
508*4882a593Smuzhiyun  */
x_table_lookup(struct aa_profile * profile,u32 xindex,const char ** name)509*4882a593Smuzhiyun struct aa_label *x_table_lookup(struct aa_profile *profile, u32 xindex,
510*4882a593Smuzhiyun 				const char **name)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun 	struct aa_label *label = NULL;
513*4882a593Smuzhiyun 	u32 xtype = xindex & AA_X_TYPE_MASK;
514*4882a593Smuzhiyun 	int index = xindex & AA_X_INDEX_MASK;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	AA_BUG(!name);
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	/* index is guaranteed to be in range, validated at load time */
519*4882a593Smuzhiyun 	/* TODO: move lookup parsing to unpack time so this is a straight
520*4882a593Smuzhiyun 	 *       index into the resultant label
521*4882a593Smuzhiyun 	 */
522*4882a593Smuzhiyun 	for (*name = profile->file.trans.table[index]; !label && *name;
523*4882a593Smuzhiyun 	     *name = next_name(xtype, *name)) {
524*4882a593Smuzhiyun 		if (xindex & AA_X_CHILD) {
525*4882a593Smuzhiyun 			struct aa_profile *new_profile;
526*4882a593Smuzhiyun 			/* release by caller */
527*4882a593Smuzhiyun 			new_profile = aa_find_child(profile, *name);
528*4882a593Smuzhiyun 			if (new_profile)
529*4882a593Smuzhiyun 				label = &new_profile->label;
530*4882a593Smuzhiyun 			continue;
531*4882a593Smuzhiyun 		}
532*4882a593Smuzhiyun 		label = aa_label_parse(&profile->label, *name, GFP_KERNEL,
533*4882a593Smuzhiyun 				       true, false);
534*4882a593Smuzhiyun 		if (IS_ERR(label))
535*4882a593Smuzhiyun 			label = NULL;
536*4882a593Smuzhiyun 	}
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	/* released by caller */
539*4882a593Smuzhiyun 
540*4882a593Smuzhiyun 	return label;
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /**
544*4882a593Smuzhiyun  * x_to_label - get target label for a given xindex
545*4882a593Smuzhiyun  * @profile: current profile  (NOT NULL)
546*4882a593Smuzhiyun  * @bprm: binprm structure of transitioning task
547*4882a593Smuzhiyun  * @name: name to lookup (NOT NULL)
548*4882a593Smuzhiyun  * @xindex: index into x transition table
549*4882a593Smuzhiyun  * @lookupname: returns: name used in lookup if one was specified (NOT NULL)
550*4882a593Smuzhiyun  *
551*4882a593Smuzhiyun  * find label for a transition index
552*4882a593Smuzhiyun  *
553*4882a593Smuzhiyun  * Returns: refcounted label or NULL if not found available
554*4882a593Smuzhiyun  */
x_to_label(struct aa_profile * profile,const struct linux_binprm * bprm,const char * name,u32 xindex,const char ** lookupname,const char ** info)555*4882a593Smuzhiyun static struct aa_label *x_to_label(struct aa_profile *profile,
556*4882a593Smuzhiyun 				   const struct linux_binprm *bprm,
557*4882a593Smuzhiyun 				   const char *name, u32 xindex,
558*4882a593Smuzhiyun 				   const char **lookupname,
559*4882a593Smuzhiyun 				   const char **info)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	struct aa_label *new = NULL;
562*4882a593Smuzhiyun 	struct aa_ns *ns = profile->ns;
563*4882a593Smuzhiyun 	u32 xtype = xindex & AA_X_TYPE_MASK;
564*4882a593Smuzhiyun 	const char *stack = NULL;
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	switch (xtype) {
567*4882a593Smuzhiyun 	case AA_X_NONE:
568*4882a593Smuzhiyun 		/* fail exec unless ix || ux fallback - handled by caller */
569*4882a593Smuzhiyun 		*lookupname = NULL;
570*4882a593Smuzhiyun 		break;
571*4882a593Smuzhiyun 	case AA_X_TABLE:
572*4882a593Smuzhiyun 		/* TODO: fix when perm mapping done at unload */
573*4882a593Smuzhiyun 		stack = profile->file.trans.table[xindex & AA_X_INDEX_MASK];
574*4882a593Smuzhiyun 		if (*stack != '&') {
575*4882a593Smuzhiyun 			/* released by caller */
576*4882a593Smuzhiyun 			new = x_table_lookup(profile, xindex, lookupname);
577*4882a593Smuzhiyun 			stack = NULL;
578*4882a593Smuzhiyun 			break;
579*4882a593Smuzhiyun 		}
580*4882a593Smuzhiyun 		fallthrough;	/* to X_NAME */
581*4882a593Smuzhiyun 	case AA_X_NAME:
582*4882a593Smuzhiyun 		if (xindex & AA_X_CHILD)
583*4882a593Smuzhiyun 			/* released by caller */
584*4882a593Smuzhiyun 			new = find_attach(bprm, ns, &profile->base.profiles,
585*4882a593Smuzhiyun 					  name, info);
586*4882a593Smuzhiyun 		else
587*4882a593Smuzhiyun 			/* released by caller */
588*4882a593Smuzhiyun 			new = find_attach(bprm, ns, &ns->base.profiles,
589*4882a593Smuzhiyun 					  name, info);
590*4882a593Smuzhiyun 		*lookupname = name;
591*4882a593Smuzhiyun 		break;
592*4882a593Smuzhiyun 	}
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 	if (!new) {
595*4882a593Smuzhiyun 		if (xindex & AA_X_INHERIT) {
596*4882a593Smuzhiyun 			/* (p|c|n)ix - don't change profile but do
597*4882a593Smuzhiyun 			 * use the newest version
598*4882a593Smuzhiyun 			 */
599*4882a593Smuzhiyun 			*info = "ix fallback";
600*4882a593Smuzhiyun 			/* no profile && no error */
601*4882a593Smuzhiyun 			new = aa_get_newest_label(&profile->label);
602*4882a593Smuzhiyun 		} else if (xindex & AA_X_UNCONFINED) {
603*4882a593Smuzhiyun 			new = aa_get_newest_label(ns_unconfined(profile->ns));
604*4882a593Smuzhiyun 			*info = "ux fallback";
605*4882a593Smuzhiyun 		}
606*4882a593Smuzhiyun 	}
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 	if (new && stack) {
609*4882a593Smuzhiyun 		/* base the stack on post domain transition */
610*4882a593Smuzhiyun 		struct aa_label *base = new;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 		new = aa_label_parse(base, stack, GFP_KERNEL, true, false);
613*4882a593Smuzhiyun 		if (IS_ERR(new))
614*4882a593Smuzhiyun 			new = NULL;
615*4882a593Smuzhiyun 		aa_put_label(base);
616*4882a593Smuzhiyun 	}
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun 	/* released by caller */
619*4882a593Smuzhiyun 	return new;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun 
profile_transition(struct aa_profile * profile,const struct linux_binprm * bprm,char * buffer,struct path_cond * cond,bool * secure_exec)622*4882a593Smuzhiyun static struct aa_label *profile_transition(struct aa_profile *profile,
623*4882a593Smuzhiyun 					   const struct linux_binprm *bprm,
624*4882a593Smuzhiyun 					   char *buffer, struct path_cond *cond,
625*4882a593Smuzhiyun 					   bool *secure_exec)
626*4882a593Smuzhiyun {
627*4882a593Smuzhiyun 	struct aa_label *new = NULL;
628*4882a593Smuzhiyun 	const char *info = NULL, *name = NULL, *target = NULL;
629*4882a593Smuzhiyun 	unsigned int state = profile->file.start;
630*4882a593Smuzhiyun 	struct aa_perms perms = {};
631*4882a593Smuzhiyun 	bool nonewprivs = false;
632*4882a593Smuzhiyun 	int error = 0;
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	AA_BUG(!profile);
635*4882a593Smuzhiyun 	AA_BUG(!bprm);
636*4882a593Smuzhiyun 	AA_BUG(!buffer);
637*4882a593Smuzhiyun 
638*4882a593Smuzhiyun 	error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
639*4882a593Smuzhiyun 			     &name, &info, profile->disconnected);
640*4882a593Smuzhiyun 	if (error) {
641*4882a593Smuzhiyun 		if (profile_unconfined(profile) ||
642*4882a593Smuzhiyun 		    (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
643*4882a593Smuzhiyun 			AA_DEBUG("name lookup ix on error");
644*4882a593Smuzhiyun 			error = 0;
645*4882a593Smuzhiyun 			new = aa_get_newest_label(&profile->label);
646*4882a593Smuzhiyun 		}
647*4882a593Smuzhiyun 		name = bprm->filename;
648*4882a593Smuzhiyun 		goto audit;
649*4882a593Smuzhiyun 	}
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	if (profile_unconfined(profile)) {
652*4882a593Smuzhiyun 		new = find_attach(bprm, profile->ns,
653*4882a593Smuzhiyun 				  &profile->ns->base.profiles, name, &info);
654*4882a593Smuzhiyun 		if (new) {
655*4882a593Smuzhiyun 			AA_DEBUG("unconfined attached to new label");
656*4882a593Smuzhiyun 			return new;
657*4882a593Smuzhiyun 		}
658*4882a593Smuzhiyun 		AA_DEBUG("unconfined exec no attachment");
659*4882a593Smuzhiyun 		return aa_get_newest_label(&profile->label);
660*4882a593Smuzhiyun 	}
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	/* find exec permissions for name */
663*4882a593Smuzhiyun 	state = aa_str_perms(profile->file.dfa, state, name, cond, &perms);
664*4882a593Smuzhiyun 	if (perms.allow & MAY_EXEC) {
665*4882a593Smuzhiyun 		/* exec permission determine how to transition */
666*4882a593Smuzhiyun 		new = x_to_label(profile, bprm, name, perms.xindex, &target,
667*4882a593Smuzhiyun 				 &info);
668*4882a593Smuzhiyun 		if (new && new->proxy == profile->label.proxy && info) {
669*4882a593Smuzhiyun 			/* hack ix fallback - improve how this is detected */
670*4882a593Smuzhiyun 			goto audit;
671*4882a593Smuzhiyun 		} else if (!new) {
672*4882a593Smuzhiyun 			error = -EACCES;
673*4882a593Smuzhiyun 			info = "profile transition not found";
674*4882a593Smuzhiyun 			/* remove MAY_EXEC to audit as failure */
675*4882a593Smuzhiyun 			perms.allow &= ~MAY_EXEC;
676*4882a593Smuzhiyun 		}
677*4882a593Smuzhiyun 	} else if (COMPLAIN_MODE(profile)) {
678*4882a593Smuzhiyun 		/* no exec permission - learning mode */
679*4882a593Smuzhiyun 		struct aa_profile *new_profile = NULL;
680*4882a593Smuzhiyun 
681*4882a593Smuzhiyun 		new_profile = aa_new_null_profile(profile, false, name,
682*4882a593Smuzhiyun 						  GFP_KERNEL);
683*4882a593Smuzhiyun 		if (!new_profile) {
684*4882a593Smuzhiyun 			error = -ENOMEM;
685*4882a593Smuzhiyun 			info = "could not create null profile";
686*4882a593Smuzhiyun 		} else {
687*4882a593Smuzhiyun 			error = -EACCES;
688*4882a593Smuzhiyun 			new = &new_profile->label;
689*4882a593Smuzhiyun 		}
690*4882a593Smuzhiyun 		perms.xindex |= AA_X_UNSAFE;
691*4882a593Smuzhiyun 	} else
692*4882a593Smuzhiyun 		/* fail exec */
693*4882a593Smuzhiyun 		error = -EACCES;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (!new)
696*4882a593Smuzhiyun 		goto audit;
697*4882a593Smuzhiyun 
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	if (!(perms.xindex & AA_X_UNSAFE)) {
700*4882a593Smuzhiyun 		if (DEBUG_ON) {
701*4882a593Smuzhiyun 			dbg_printk("apparmor: scrubbing environment variables"
702*4882a593Smuzhiyun 				   " for %s profile=", name);
703*4882a593Smuzhiyun 			aa_label_printk(new, GFP_KERNEL);
704*4882a593Smuzhiyun 			dbg_printk("\n");
705*4882a593Smuzhiyun 		}
706*4882a593Smuzhiyun 		*secure_exec = true;
707*4882a593Smuzhiyun 	}
708*4882a593Smuzhiyun 
709*4882a593Smuzhiyun audit:
710*4882a593Smuzhiyun 	aa_audit_file(profile, &perms, OP_EXEC, MAY_EXEC, name, target, new,
711*4882a593Smuzhiyun 		      cond->uid, info, error);
712*4882a593Smuzhiyun 	if (!new || nonewprivs) {
713*4882a593Smuzhiyun 		aa_put_label(new);
714*4882a593Smuzhiyun 		return ERR_PTR(error);
715*4882a593Smuzhiyun 	}
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun 	return new;
718*4882a593Smuzhiyun }
719*4882a593Smuzhiyun 
profile_onexec(struct aa_profile * profile,struct aa_label * onexec,bool stack,const struct linux_binprm * bprm,char * buffer,struct path_cond * cond,bool * secure_exec)720*4882a593Smuzhiyun static int profile_onexec(struct aa_profile *profile, struct aa_label *onexec,
721*4882a593Smuzhiyun 			  bool stack, const struct linux_binprm *bprm,
722*4882a593Smuzhiyun 			  char *buffer, struct path_cond *cond,
723*4882a593Smuzhiyun 			  bool *secure_exec)
724*4882a593Smuzhiyun {
725*4882a593Smuzhiyun 	unsigned int state = profile->file.start;
726*4882a593Smuzhiyun 	struct aa_perms perms = {};
727*4882a593Smuzhiyun 	const char *xname = NULL, *info = "change_profile onexec";
728*4882a593Smuzhiyun 	int error = -EACCES;
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	AA_BUG(!profile);
731*4882a593Smuzhiyun 	AA_BUG(!onexec);
732*4882a593Smuzhiyun 	AA_BUG(!bprm);
733*4882a593Smuzhiyun 	AA_BUG(!buffer);
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 	if (profile_unconfined(profile)) {
736*4882a593Smuzhiyun 		/* change_profile on exec already granted */
737*4882a593Smuzhiyun 		/*
738*4882a593Smuzhiyun 		 * NOTE: Domain transitions from unconfined are allowed
739*4882a593Smuzhiyun 		 * even when no_new_privs is set because this aways results
740*4882a593Smuzhiyun 		 * in a further reduction of permissions.
741*4882a593Smuzhiyun 		 */
742*4882a593Smuzhiyun 		return 0;
743*4882a593Smuzhiyun 	}
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 	error = aa_path_name(&bprm->file->f_path, profile->path_flags, buffer,
746*4882a593Smuzhiyun 			     &xname, &info, profile->disconnected);
747*4882a593Smuzhiyun 	if (error) {
748*4882a593Smuzhiyun 		if (profile_unconfined(profile) ||
749*4882a593Smuzhiyun 		    (profile->label.flags & FLAG_IX_ON_NAME_ERROR)) {
750*4882a593Smuzhiyun 			AA_DEBUG("name lookup ix on error");
751*4882a593Smuzhiyun 			error = 0;
752*4882a593Smuzhiyun 		}
753*4882a593Smuzhiyun 		xname = bprm->filename;
754*4882a593Smuzhiyun 		goto audit;
755*4882a593Smuzhiyun 	}
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	/* find exec permissions for name */
758*4882a593Smuzhiyun 	state = aa_str_perms(profile->file.dfa, state, xname, cond, &perms);
759*4882a593Smuzhiyun 	if (!(perms.allow & AA_MAY_ONEXEC)) {
760*4882a593Smuzhiyun 		info = "no change_onexec valid for executable";
761*4882a593Smuzhiyun 		goto audit;
762*4882a593Smuzhiyun 	}
763*4882a593Smuzhiyun 	/* test if this exec can be paired with change_profile onexec.
764*4882a593Smuzhiyun 	 * onexec permission is linked to exec with a standard pairing
765*4882a593Smuzhiyun 	 * exec\0change_profile
766*4882a593Smuzhiyun 	 */
767*4882a593Smuzhiyun 	state = aa_dfa_null_transition(profile->file.dfa, state);
768*4882a593Smuzhiyun 	error = change_profile_perms(profile, onexec, stack, AA_MAY_ONEXEC,
769*4882a593Smuzhiyun 				     state, &perms);
770*4882a593Smuzhiyun 	if (error) {
771*4882a593Smuzhiyun 		perms.allow &= ~AA_MAY_ONEXEC;
772*4882a593Smuzhiyun 		goto audit;
773*4882a593Smuzhiyun 	}
774*4882a593Smuzhiyun 
775*4882a593Smuzhiyun 	if (!(perms.xindex & AA_X_UNSAFE)) {
776*4882a593Smuzhiyun 		if (DEBUG_ON) {
777*4882a593Smuzhiyun 			dbg_printk("apparmor: scrubbing environment "
778*4882a593Smuzhiyun 				   "variables for %s label=", xname);
779*4882a593Smuzhiyun 			aa_label_printk(onexec, GFP_KERNEL);
780*4882a593Smuzhiyun 			dbg_printk("\n");
781*4882a593Smuzhiyun 		}
782*4882a593Smuzhiyun 		*secure_exec = true;
783*4882a593Smuzhiyun 	}
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun audit:
786*4882a593Smuzhiyun 	return aa_audit_file(profile, &perms, OP_EXEC, AA_MAY_ONEXEC, xname,
787*4882a593Smuzhiyun 			     NULL, onexec, cond->uid, info, error);
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun /* ensure none ns domain transitions are correctly applied with onexec */
791*4882a593Smuzhiyun 
handle_onexec(struct aa_label * label,struct aa_label * onexec,bool stack,const struct linux_binprm * bprm,char * buffer,struct path_cond * cond,bool * unsafe)792*4882a593Smuzhiyun static struct aa_label *handle_onexec(struct aa_label *label,
793*4882a593Smuzhiyun 				      struct aa_label *onexec, bool stack,
794*4882a593Smuzhiyun 				      const struct linux_binprm *bprm,
795*4882a593Smuzhiyun 				      char *buffer, struct path_cond *cond,
796*4882a593Smuzhiyun 				      bool *unsafe)
797*4882a593Smuzhiyun {
798*4882a593Smuzhiyun 	struct aa_profile *profile;
799*4882a593Smuzhiyun 	struct aa_label *new;
800*4882a593Smuzhiyun 	int error;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	AA_BUG(!label);
803*4882a593Smuzhiyun 	AA_BUG(!onexec);
804*4882a593Smuzhiyun 	AA_BUG(!bprm);
805*4882a593Smuzhiyun 	AA_BUG(!buffer);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	if (!stack) {
808*4882a593Smuzhiyun 		error = fn_for_each_in_ns(label, profile,
809*4882a593Smuzhiyun 				profile_onexec(profile, onexec, stack,
810*4882a593Smuzhiyun 					       bprm, buffer, cond, unsafe));
811*4882a593Smuzhiyun 		if (error)
812*4882a593Smuzhiyun 			return ERR_PTR(error);
813*4882a593Smuzhiyun 		new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
814*4882a593Smuzhiyun 				aa_get_newest_label(onexec),
815*4882a593Smuzhiyun 				profile_transition(profile, bprm, buffer,
816*4882a593Smuzhiyun 						   cond, unsafe));
817*4882a593Smuzhiyun 
818*4882a593Smuzhiyun 	} else {
819*4882a593Smuzhiyun 		/* TODO: determine how much we want to loosen this */
820*4882a593Smuzhiyun 		error = fn_for_each_in_ns(label, profile,
821*4882a593Smuzhiyun 				profile_onexec(profile, onexec, stack, bprm,
822*4882a593Smuzhiyun 					       buffer, cond, unsafe));
823*4882a593Smuzhiyun 		if (error)
824*4882a593Smuzhiyun 			return ERR_PTR(error);
825*4882a593Smuzhiyun 		new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
826*4882a593Smuzhiyun 				aa_label_merge(&profile->label, onexec,
827*4882a593Smuzhiyun 					       GFP_KERNEL),
828*4882a593Smuzhiyun 				profile_transition(profile, bprm, buffer,
829*4882a593Smuzhiyun 						   cond, unsafe));
830*4882a593Smuzhiyun 	}
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	if (new)
833*4882a593Smuzhiyun 		return new;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 	/* TODO: get rid of GLOBAL_ROOT_UID */
836*4882a593Smuzhiyun 	error = fn_for_each_in_ns(label, profile,
837*4882a593Smuzhiyun 			aa_audit_file(profile, &nullperms, OP_CHANGE_ONEXEC,
838*4882a593Smuzhiyun 				      AA_MAY_ONEXEC, bprm->filename, NULL,
839*4882a593Smuzhiyun 				      onexec, GLOBAL_ROOT_UID,
840*4882a593Smuzhiyun 				      "failed to build target label", -ENOMEM));
841*4882a593Smuzhiyun 	return ERR_PTR(error);
842*4882a593Smuzhiyun }
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun /**
845*4882a593Smuzhiyun  * apparmor_bprm_creds_for_exec - Update the new creds on the bprm struct
846*4882a593Smuzhiyun  * @bprm: binprm for the exec  (NOT NULL)
847*4882a593Smuzhiyun  *
848*4882a593Smuzhiyun  * Returns: %0 or error on failure
849*4882a593Smuzhiyun  *
850*4882a593Smuzhiyun  * TODO: once the other paths are done see if we can't refactor into a fn
851*4882a593Smuzhiyun  */
apparmor_bprm_creds_for_exec(struct linux_binprm * bprm)852*4882a593Smuzhiyun int apparmor_bprm_creds_for_exec(struct linux_binprm *bprm)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	struct aa_task_ctx *ctx;
855*4882a593Smuzhiyun 	struct aa_label *label, *new = NULL;
856*4882a593Smuzhiyun 	struct aa_profile *profile;
857*4882a593Smuzhiyun 	char *buffer = NULL;
858*4882a593Smuzhiyun 	const char *info = NULL;
859*4882a593Smuzhiyun 	int error = 0;
860*4882a593Smuzhiyun 	bool unsafe = false;
861*4882a593Smuzhiyun 	struct path_cond cond = {
862*4882a593Smuzhiyun 		file_inode(bprm->file)->i_uid,
863*4882a593Smuzhiyun 		file_inode(bprm->file)->i_mode
864*4882a593Smuzhiyun 	};
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	ctx = task_ctx(current);
867*4882a593Smuzhiyun 	AA_BUG(!cred_label(bprm->cred));
868*4882a593Smuzhiyun 	AA_BUG(!ctx);
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	label = aa_get_newest_label(cred_label(bprm->cred));
871*4882a593Smuzhiyun 
872*4882a593Smuzhiyun 	/*
873*4882a593Smuzhiyun 	 * Detect no new privs being set, and store the label it
874*4882a593Smuzhiyun 	 * occurred under. Ideally this would happen when nnp
875*4882a593Smuzhiyun 	 * is set but there isn't a good way to do that yet.
876*4882a593Smuzhiyun 	 *
877*4882a593Smuzhiyun 	 * Testing for unconfined must be done before the subset test
878*4882a593Smuzhiyun 	 */
879*4882a593Smuzhiyun 	if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) && !unconfined(label) &&
880*4882a593Smuzhiyun 	    !ctx->nnp)
881*4882a593Smuzhiyun 		ctx->nnp = aa_get_label(label);
882*4882a593Smuzhiyun 
883*4882a593Smuzhiyun 	/* buffer freed below, name is pointer into buffer */
884*4882a593Smuzhiyun 	buffer = aa_get_buffer(false);
885*4882a593Smuzhiyun 	if (!buffer) {
886*4882a593Smuzhiyun 		error = -ENOMEM;
887*4882a593Smuzhiyun 		goto done;
888*4882a593Smuzhiyun 	}
889*4882a593Smuzhiyun 
890*4882a593Smuzhiyun 	/* Test for onexec first as onexec override other x transitions. */
891*4882a593Smuzhiyun 	if (ctx->onexec)
892*4882a593Smuzhiyun 		new = handle_onexec(label, ctx->onexec, ctx->token,
893*4882a593Smuzhiyun 				    bprm, buffer, &cond, &unsafe);
894*4882a593Smuzhiyun 	else
895*4882a593Smuzhiyun 		new = fn_label_build(label, profile, GFP_KERNEL,
896*4882a593Smuzhiyun 				profile_transition(profile, bprm, buffer,
897*4882a593Smuzhiyun 						   &cond, &unsafe));
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun 	AA_BUG(!new);
900*4882a593Smuzhiyun 	if (IS_ERR(new)) {
901*4882a593Smuzhiyun 		error = PTR_ERR(new);
902*4882a593Smuzhiyun 		goto done;
903*4882a593Smuzhiyun 	} else if (!new) {
904*4882a593Smuzhiyun 		error = -ENOMEM;
905*4882a593Smuzhiyun 		goto done;
906*4882a593Smuzhiyun 	}
907*4882a593Smuzhiyun 
908*4882a593Smuzhiyun 	/* Policy has specified a domain transitions. If no_new_privs and
909*4882a593Smuzhiyun 	 * confined ensure the transition is to confinement that is subset
910*4882a593Smuzhiyun 	 * of the confinement when the task entered no new privs.
911*4882a593Smuzhiyun 	 *
912*4882a593Smuzhiyun 	 * NOTE: Domain transitions from unconfined and to stacked
913*4882a593Smuzhiyun 	 * subsets are allowed even when no_new_privs is set because this
914*4882a593Smuzhiyun 	 * aways results in a further reduction of permissions.
915*4882a593Smuzhiyun 	 */
916*4882a593Smuzhiyun 	if ((bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS) &&
917*4882a593Smuzhiyun 	    !unconfined(label) &&
918*4882a593Smuzhiyun 	    !aa_label_is_unconfined_subset(new, ctx->nnp)) {
919*4882a593Smuzhiyun 		error = -EPERM;
920*4882a593Smuzhiyun 		info = "no new privs";
921*4882a593Smuzhiyun 		goto audit;
922*4882a593Smuzhiyun 	}
923*4882a593Smuzhiyun 
924*4882a593Smuzhiyun 	if (bprm->unsafe & LSM_UNSAFE_SHARE) {
925*4882a593Smuzhiyun 		/* FIXME: currently don't mediate shared state */
926*4882a593Smuzhiyun 		;
927*4882a593Smuzhiyun 	}
928*4882a593Smuzhiyun 
929*4882a593Smuzhiyun 	if (bprm->unsafe & (LSM_UNSAFE_PTRACE)) {
930*4882a593Smuzhiyun 		/* TODO: test needs to be profile of label to new */
931*4882a593Smuzhiyun 		error = may_change_ptraced_domain(new, &info);
932*4882a593Smuzhiyun 		if (error)
933*4882a593Smuzhiyun 			goto audit;
934*4882a593Smuzhiyun 	}
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	if (unsafe) {
937*4882a593Smuzhiyun 		if (DEBUG_ON) {
938*4882a593Smuzhiyun 			dbg_printk("scrubbing environment variables for %s "
939*4882a593Smuzhiyun 				   "label=", bprm->filename);
940*4882a593Smuzhiyun 			aa_label_printk(new, GFP_KERNEL);
941*4882a593Smuzhiyun 			dbg_printk("\n");
942*4882a593Smuzhiyun 		}
943*4882a593Smuzhiyun 		bprm->secureexec = 1;
944*4882a593Smuzhiyun 	}
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	if (label->proxy != new->proxy) {
947*4882a593Smuzhiyun 		/* when transitioning clear unsafe personality bits */
948*4882a593Smuzhiyun 		if (DEBUG_ON) {
949*4882a593Smuzhiyun 			dbg_printk("apparmor: clearing unsafe personality "
950*4882a593Smuzhiyun 				   "bits. %s label=", bprm->filename);
951*4882a593Smuzhiyun 			aa_label_printk(new, GFP_KERNEL);
952*4882a593Smuzhiyun 			dbg_printk("\n");
953*4882a593Smuzhiyun 		}
954*4882a593Smuzhiyun 		bprm->per_clear |= PER_CLEAR_ON_SETID;
955*4882a593Smuzhiyun 	}
956*4882a593Smuzhiyun 	aa_put_label(cred_label(bprm->cred));
957*4882a593Smuzhiyun 	/* transfer reference, released when cred is freed */
958*4882a593Smuzhiyun 	set_cred_label(bprm->cred, new);
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun done:
961*4882a593Smuzhiyun 	aa_put_label(label);
962*4882a593Smuzhiyun 	aa_put_buffer(buffer);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	return error;
965*4882a593Smuzhiyun 
966*4882a593Smuzhiyun audit:
967*4882a593Smuzhiyun 	error = fn_for_each(label, profile,
968*4882a593Smuzhiyun 			aa_audit_file(profile, &nullperms, OP_EXEC, MAY_EXEC,
969*4882a593Smuzhiyun 				      bprm->filename, NULL, new,
970*4882a593Smuzhiyun 				      file_inode(bprm->file)->i_uid, info,
971*4882a593Smuzhiyun 				      error));
972*4882a593Smuzhiyun 	aa_put_label(new);
973*4882a593Smuzhiyun 	goto done;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun /*
977*4882a593Smuzhiyun  * Functions for self directed profile change
978*4882a593Smuzhiyun  */
979*4882a593Smuzhiyun 
980*4882a593Smuzhiyun 
981*4882a593Smuzhiyun /* helper fn for change_hat
982*4882a593Smuzhiyun  *
983*4882a593Smuzhiyun  * Returns: label for hat transition OR ERR_PTR.  Does NOT return NULL
984*4882a593Smuzhiyun  */
build_change_hat(struct aa_profile * profile,const char * name,bool sibling)985*4882a593Smuzhiyun static struct aa_label *build_change_hat(struct aa_profile *profile,
986*4882a593Smuzhiyun 					 const char *name, bool sibling)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun 	struct aa_profile *root, *hat = NULL;
989*4882a593Smuzhiyun 	const char *info = NULL;
990*4882a593Smuzhiyun 	int error = 0;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	if (sibling && PROFILE_IS_HAT(profile)) {
993*4882a593Smuzhiyun 		root = aa_get_profile_rcu(&profile->parent);
994*4882a593Smuzhiyun 	} else if (!sibling && !PROFILE_IS_HAT(profile)) {
995*4882a593Smuzhiyun 		root = aa_get_profile(profile);
996*4882a593Smuzhiyun 	} else {
997*4882a593Smuzhiyun 		info = "conflicting target types";
998*4882a593Smuzhiyun 		error = -EPERM;
999*4882a593Smuzhiyun 		goto audit;
1000*4882a593Smuzhiyun 	}
1001*4882a593Smuzhiyun 
1002*4882a593Smuzhiyun 	hat = aa_find_child(root, name);
1003*4882a593Smuzhiyun 	if (!hat) {
1004*4882a593Smuzhiyun 		error = -ENOENT;
1005*4882a593Smuzhiyun 		if (COMPLAIN_MODE(profile)) {
1006*4882a593Smuzhiyun 			hat = aa_new_null_profile(profile, true, name,
1007*4882a593Smuzhiyun 						  GFP_KERNEL);
1008*4882a593Smuzhiyun 			if (!hat) {
1009*4882a593Smuzhiyun 				info = "failed null profile create";
1010*4882a593Smuzhiyun 				error = -ENOMEM;
1011*4882a593Smuzhiyun 			}
1012*4882a593Smuzhiyun 		}
1013*4882a593Smuzhiyun 	}
1014*4882a593Smuzhiyun 	aa_put_profile(root);
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun audit:
1017*4882a593Smuzhiyun 	aa_audit_file(profile, &nullperms, OP_CHANGE_HAT, AA_MAY_CHANGEHAT,
1018*4882a593Smuzhiyun 		      name, hat ? hat->base.hname : NULL,
1019*4882a593Smuzhiyun 		      hat ? &hat->label : NULL, GLOBAL_ROOT_UID, info,
1020*4882a593Smuzhiyun 		      error);
1021*4882a593Smuzhiyun 	if (!hat || (error && error != -ENOENT))
1022*4882a593Smuzhiyun 		return ERR_PTR(error);
1023*4882a593Smuzhiyun 	/* if hat && error - complain mode, already audited and we adjust for
1024*4882a593Smuzhiyun 	 * complain mode allow by returning hat->label
1025*4882a593Smuzhiyun 	 */
1026*4882a593Smuzhiyun 	return &hat->label;
1027*4882a593Smuzhiyun }
1028*4882a593Smuzhiyun 
1029*4882a593Smuzhiyun /* helper fn for changing into a hat
1030*4882a593Smuzhiyun  *
1031*4882a593Smuzhiyun  * Returns: label for hat transition or ERR_PTR. Does not return NULL
1032*4882a593Smuzhiyun  */
change_hat(struct aa_label * label,const char * hats[],int count,int flags)1033*4882a593Smuzhiyun static struct aa_label *change_hat(struct aa_label *label, const char *hats[],
1034*4882a593Smuzhiyun 				   int count, int flags)
1035*4882a593Smuzhiyun {
1036*4882a593Smuzhiyun 	struct aa_profile *profile, *root, *hat = NULL;
1037*4882a593Smuzhiyun 	struct aa_label *new;
1038*4882a593Smuzhiyun 	struct label_it it;
1039*4882a593Smuzhiyun 	bool sibling = false;
1040*4882a593Smuzhiyun 	const char *name, *info = NULL;
1041*4882a593Smuzhiyun 	int i, error;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	AA_BUG(!label);
1044*4882a593Smuzhiyun 	AA_BUG(!hats);
1045*4882a593Smuzhiyun 	AA_BUG(count < 1);
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	if (PROFILE_IS_HAT(labels_profile(label)))
1048*4882a593Smuzhiyun 		sibling = true;
1049*4882a593Smuzhiyun 
1050*4882a593Smuzhiyun 	/*find first matching hat */
1051*4882a593Smuzhiyun 	for (i = 0; i < count && !hat; i++) {
1052*4882a593Smuzhiyun 		name = hats[i];
1053*4882a593Smuzhiyun 		label_for_each_in_ns(it, labels_ns(label), label, profile) {
1054*4882a593Smuzhiyun 			if (sibling && PROFILE_IS_HAT(profile)) {
1055*4882a593Smuzhiyun 				root = aa_get_profile_rcu(&profile->parent);
1056*4882a593Smuzhiyun 			} else if (!sibling && !PROFILE_IS_HAT(profile)) {
1057*4882a593Smuzhiyun 				root = aa_get_profile(profile);
1058*4882a593Smuzhiyun 			} else {	/* conflicting change type */
1059*4882a593Smuzhiyun 				info = "conflicting targets types";
1060*4882a593Smuzhiyun 				error = -EPERM;
1061*4882a593Smuzhiyun 				goto fail;
1062*4882a593Smuzhiyun 			}
1063*4882a593Smuzhiyun 			hat = aa_find_child(root, name);
1064*4882a593Smuzhiyun 			aa_put_profile(root);
1065*4882a593Smuzhiyun 			if (!hat) {
1066*4882a593Smuzhiyun 				if (!COMPLAIN_MODE(profile))
1067*4882a593Smuzhiyun 					goto outer_continue;
1068*4882a593Smuzhiyun 				/* complain mode succeed as if hat */
1069*4882a593Smuzhiyun 			} else if (!PROFILE_IS_HAT(hat)) {
1070*4882a593Smuzhiyun 				info = "target not hat";
1071*4882a593Smuzhiyun 				error = -EPERM;
1072*4882a593Smuzhiyun 				aa_put_profile(hat);
1073*4882a593Smuzhiyun 				goto fail;
1074*4882a593Smuzhiyun 			}
1075*4882a593Smuzhiyun 			aa_put_profile(hat);
1076*4882a593Smuzhiyun 		}
1077*4882a593Smuzhiyun 		/* found a hat for all profiles in ns */
1078*4882a593Smuzhiyun 		goto build;
1079*4882a593Smuzhiyun outer_continue:
1080*4882a593Smuzhiyun 	;
1081*4882a593Smuzhiyun 	}
1082*4882a593Smuzhiyun 	/* no hats that match, find appropriate error
1083*4882a593Smuzhiyun 	 *
1084*4882a593Smuzhiyun 	 * In complain mode audit of the failure is based off of the first
1085*4882a593Smuzhiyun 	 * hat supplied.  This is done due how userspace interacts with
1086*4882a593Smuzhiyun 	 * change_hat.
1087*4882a593Smuzhiyun 	 */
1088*4882a593Smuzhiyun 	name = NULL;
1089*4882a593Smuzhiyun 	label_for_each_in_ns(it, labels_ns(label), label, profile) {
1090*4882a593Smuzhiyun 		if (!list_empty(&profile->base.profiles)) {
1091*4882a593Smuzhiyun 			info = "hat not found";
1092*4882a593Smuzhiyun 			error = -ENOENT;
1093*4882a593Smuzhiyun 			goto fail;
1094*4882a593Smuzhiyun 		}
1095*4882a593Smuzhiyun 	}
1096*4882a593Smuzhiyun 	info = "no hats defined";
1097*4882a593Smuzhiyun 	error = -ECHILD;
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun fail:
1100*4882a593Smuzhiyun 	label_for_each_in_ns(it, labels_ns(label), label, profile) {
1101*4882a593Smuzhiyun 		/*
1102*4882a593Smuzhiyun 		 * no target as it has failed to be found or built
1103*4882a593Smuzhiyun 		 *
1104*4882a593Smuzhiyun 		 * change_hat uses probing and should not log failures
1105*4882a593Smuzhiyun 		 * related to missing hats
1106*4882a593Smuzhiyun 		 */
1107*4882a593Smuzhiyun 		/* TODO: get rid of GLOBAL_ROOT_UID */
1108*4882a593Smuzhiyun 		if (count > 1 || COMPLAIN_MODE(profile)) {
1109*4882a593Smuzhiyun 			aa_audit_file(profile, &nullperms, OP_CHANGE_HAT,
1110*4882a593Smuzhiyun 				      AA_MAY_CHANGEHAT, name, NULL, NULL,
1111*4882a593Smuzhiyun 				      GLOBAL_ROOT_UID, info, error);
1112*4882a593Smuzhiyun 		}
1113*4882a593Smuzhiyun 	}
1114*4882a593Smuzhiyun 	return ERR_PTR(error);
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun build:
1117*4882a593Smuzhiyun 	new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1118*4882a593Smuzhiyun 				   build_change_hat(profile, name, sibling),
1119*4882a593Smuzhiyun 				   aa_get_label(&profile->label));
1120*4882a593Smuzhiyun 	if (!new) {
1121*4882a593Smuzhiyun 		info = "label build failed";
1122*4882a593Smuzhiyun 		error = -ENOMEM;
1123*4882a593Smuzhiyun 		goto fail;
1124*4882a593Smuzhiyun 	} /* else if (IS_ERR) build_change_hat has logged error so return new */
1125*4882a593Smuzhiyun 
1126*4882a593Smuzhiyun 	return new;
1127*4882a593Smuzhiyun }
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun /**
1130*4882a593Smuzhiyun  * aa_change_hat - change hat to/from subprofile
1131*4882a593Smuzhiyun  * @hats: vector of hat names to try changing into (MAYBE NULL if @count == 0)
1132*4882a593Smuzhiyun  * @count: number of hat names in @hats
1133*4882a593Smuzhiyun  * @token: magic value to validate the hat change
1134*4882a593Smuzhiyun  * @flags: flags affecting behavior of the change
1135*4882a593Smuzhiyun  *
1136*4882a593Smuzhiyun  * Returns %0 on success, error otherwise.
1137*4882a593Smuzhiyun  *
1138*4882a593Smuzhiyun  * Change to the first profile specified in @hats that exists, and store
1139*4882a593Smuzhiyun  * the @hat_magic in the current task context.  If the count == 0 and the
1140*4882a593Smuzhiyun  * @token matches that stored in the current task context, return to the
1141*4882a593Smuzhiyun  * top level profile.
1142*4882a593Smuzhiyun  *
1143*4882a593Smuzhiyun  * change_hat only applies to profiles in the current ns, and each profile
1144*4882a593Smuzhiyun  * in the ns must make the same transition otherwise change_hat will fail.
1145*4882a593Smuzhiyun  */
aa_change_hat(const char * hats[],int count,u64 token,int flags)1146*4882a593Smuzhiyun int aa_change_hat(const char *hats[], int count, u64 token, int flags)
1147*4882a593Smuzhiyun {
1148*4882a593Smuzhiyun 	const struct cred *cred;
1149*4882a593Smuzhiyun 	struct aa_task_ctx *ctx = task_ctx(current);
1150*4882a593Smuzhiyun 	struct aa_label *label, *previous, *new = NULL, *target = NULL;
1151*4882a593Smuzhiyun 	struct aa_profile *profile;
1152*4882a593Smuzhiyun 	struct aa_perms perms = {};
1153*4882a593Smuzhiyun 	const char *info = NULL;
1154*4882a593Smuzhiyun 	int error = 0;
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	/* released below */
1157*4882a593Smuzhiyun 	cred = get_current_cred();
1158*4882a593Smuzhiyun 	label = aa_get_newest_cred_label(cred);
1159*4882a593Smuzhiyun 	previous = aa_get_newest_label(ctx->previous);
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	/*
1162*4882a593Smuzhiyun 	 * Detect no new privs being set, and store the label it
1163*4882a593Smuzhiyun 	 * occurred under. Ideally this would happen when nnp
1164*4882a593Smuzhiyun 	 * is set but there isn't a good way to do that yet.
1165*4882a593Smuzhiyun 	 *
1166*4882a593Smuzhiyun 	 * Testing for unconfined must be done before the subset test
1167*4882a593Smuzhiyun 	 */
1168*4882a593Smuzhiyun 	if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1169*4882a593Smuzhiyun 		ctx->nnp = aa_get_label(label);
1170*4882a593Smuzhiyun 
1171*4882a593Smuzhiyun 	if (unconfined(label)) {
1172*4882a593Smuzhiyun 		info = "unconfined can not change_hat";
1173*4882a593Smuzhiyun 		error = -EPERM;
1174*4882a593Smuzhiyun 		goto fail;
1175*4882a593Smuzhiyun 	}
1176*4882a593Smuzhiyun 
1177*4882a593Smuzhiyun 	if (count) {
1178*4882a593Smuzhiyun 		new = change_hat(label, hats, count, flags);
1179*4882a593Smuzhiyun 		AA_BUG(!new);
1180*4882a593Smuzhiyun 		if (IS_ERR(new)) {
1181*4882a593Smuzhiyun 			error = PTR_ERR(new);
1182*4882a593Smuzhiyun 			new = NULL;
1183*4882a593Smuzhiyun 			/* already audited */
1184*4882a593Smuzhiyun 			goto out;
1185*4882a593Smuzhiyun 		}
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun 		error = may_change_ptraced_domain(new, &info);
1188*4882a593Smuzhiyun 		if (error)
1189*4882a593Smuzhiyun 			goto fail;
1190*4882a593Smuzhiyun 
1191*4882a593Smuzhiyun 		/*
1192*4882a593Smuzhiyun 		 * no new privs prevents domain transitions that would
1193*4882a593Smuzhiyun 		 * reduce restrictions.
1194*4882a593Smuzhiyun 		 */
1195*4882a593Smuzhiyun 		if (task_no_new_privs(current) && !unconfined(label) &&
1196*4882a593Smuzhiyun 		    !aa_label_is_unconfined_subset(new, ctx->nnp)) {
1197*4882a593Smuzhiyun 			/* not an apparmor denial per se, so don't log it */
1198*4882a593Smuzhiyun 			AA_DEBUG("no_new_privs - change_hat denied");
1199*4882a593Smuzhiyun 			error = -EPERM;
1200*4882a593Smuzhiyun 			goto out;
1201*4882a593Smuzhiyun 		}
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 		if (flags & AA_CHANGE_TEST)
1204*4882a593Smuzhiyun 			goto out;
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun 		target = new;
1207*4882a593Smuzhiyun 		error = aa_set_current_hat(new, token);
1208*4882a593Smuzhiyun 		if (error == -EACCES)
1209*4882a593Smuzhiyun 			/* kill task in case of brute force attacks */
1210*4882a593Smuzhiyun 			goto kill;
1211*4882a593Smuzhiyun 	} else if (previous && !(flags & AA_CHANGE_TEST)) {
1212*4882a593Smuzhiyun 		/*
1213*4882a593Smuzhiyun 		 * no new privs prevents domain transitions that would
1214*4882a593Smuzhiyun 		 * reduce restrictions.
1215*4882a593Smuzhiyun 		 */
1216*4882a593Smuzhiyun 		if (task_no_new_privs(current) && !unconfined(label) &&
1217*4882a593Smuzhiyun 		    !aa_label_is_unconfined_subset(previous, ctx->nnp)) {
1218*4882a593Smuzhiyun 			/* not an apparmor denial per se, so don't log it */
1219*4882a593Smuzhiyun 			AA_DEBUG("no_new_privs - change_hat denied");
1220*4882a593Smuzhiyun 			error = -EPERM;
1221*4882a593Smuzhiyun 			goto out;
1222*4882a593Smuzhiyun 		}
1223*4882a593Smuzhiyun 
1224*4882a593Smuzhiyun 		/* Return to saved label.  Kill task if restore fails
1225*4882a593Smuzhiyun 		 * to avoid brute force attacks
1226*4882a593Smuzhiyun 		 */
1227*4882a593Smuzhiyun 		target = previous;
1228*4882a593Smuzhiyun 		error = aa_restore_previous_label(token);
1229*4882a593Smuzhiyun 		if (error) {
1230*4882a593Smuzhiyun 			if (error == -EACCES)
1231*4882a593Smuzhiyun 				goto kill;
1232*4882a593Smuzhiyun 			goto fail;
1233*4882a593Smuzhiyun 		}
1234*4882a593Smuzhiyun 	} /* else ignore @flags && restores when there is no saved profile */
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun out:
1237*4882a593Smuzhiyun 	aa_put_label(new);
1238*4882a593Smuzhiyun 	aa_put_label(previous);
1239*4882a593Smuzhiyun 	aa_put_label(label);
1240*4882a593Smuzhiyun 	put_cred(cred);
1241*4882a593Smuzhiyun 
1242*4882a593Smuzhiyun 	return error;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun kill:
1245*4882a593Smuzhiyun 	info = "failed token match";
1246*4882a593Smuzhiyun 	perms.kill = AA_MAY_CHANGEHAT;
1247*4882a593Smuzhiyun 
1248*4882a593Smuzhiyun fail:
1249*4882a593Smuzhiyun 	fn_for_each_in_ns(label, profile,
1250*4882a593Smuzhiyun 		aa_audit_file(profile, &perms, OP_CHANGE_HAT,
1251*4882a593Smuzhiyun 			      AA_MAY_CHANGEHAT, NULL, NULL, target,
1252*4882a593Smuzhiyun 			      GLOBAL_ROOT_UID, info, error));
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 	goto out;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun 
1257*4882a593Smuzhiyun 
change_profile_perms_wrapper(const char * op,const char * name,struct aa_profile * profile,struct aa_label * target,bool stack,u32 request,struct aa_perms * perms)1258*4882a593Smuzhiyun static int change_profile_perms_wrapper(const char *op, const char *name,
1259*4882a593Smuzhiyun 					struct aa_profile *profile,
1260*4882a593Smuzhiyun 					struct aa_label *target, bool stack,
1261*4882a593Smuzhiyun 					u32 request, struct aa_perms *perms)
1262*4882a593Smuzhiyun {
1263*4882a593Smuzhiyun 	const char *info = NULL;
1264*4882a593Smuzhiyun 	int error = 0;
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	if (!error)
1267*4882a593Smuzhiyun 		error = change_profile_perms(profile, target, stack, request,
1268*4882a593Smuzhiyun 					     profile->file.start, perms);
1269*4882a593Smuzhiyun 	if (error)
1270*4882a593Smuzhiyun 		error = aa_audit_file(profile, perms, op, request, name,
1271*4882a593Smuzhiyun 				      NULL, target, GLOBAL_ROOT_UID, info,
1272*4882a593Smuzhiyun 				      error);
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	return error;
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun 
1277*4882a593Smuzhiyun /**
1278*4882a593Smuzhiyun  * aa_change_profile - perform a one-way profile transition
1279*4882a593Smuzhiyun  * @fqname: name of profile may include namespace (NOT NULL)
1280*4882a593Smuzhiyun  * @onexec: whether this transition is to take place immediately or at exec
1281*4882a593Smuzhiyun  * @flags: flags affecting change behavior
1282*4882a593Smuzhiyun  *
1283*4882a593Smuzhiyun  * Change to new profile @name.  Unlike with hats, there is no way
1284*4882a593Smuzhiyun  * to change back.  If @name isn't specified the current profile name is
1285*4882a593Smuzhiyun  * used.
1286*4882a593Smuzhiyun  * If @onexec then the transition is delayed until
1287*4882a593Smuzhiyun  * the next exec.
1288*4882a593Smuzhiyun  *
1289*4882a593Smuzhiyun  * Returns %0 on success, error otherwise.
1290*4882a593Smuzhiyun  */
aa_change_profile(const char * fqname,int flags)1291*4882a593Smuzhiyun int aa_change_profile(const char *fqname, int flags)
1292*4882a593Smuzhiyun {
1293*4882a593Smuzhiyun 	struct aa_label *label, *new = NULL, *target = NULL;
1294*4882a593Smuzhiyun 	struct aa_profile *profile;
1295*4882a593Smuzhiyun 	struct aa_perms perms = {};
1296*4882a593Smuzhiyun 	const char *info = NULL;
1297*4882a593Smuzhiyun 	const char *auditname = fqname;		/* retain leading & if stack */
1298*4882a593Smuzhiyun 	bool stack = flags & AA_CHANGE_STACK;
1299*4882a593Smuzhiyun 	struct aa_task_ctx *ctx = task_ctx(current);
1300*4882a593Smuzhiyun 	int error = 0;
1301*4882a593Smuzhiyun 	char *op;
1302*4882a593Smuzhiyun 	u32 request;
1303*4882a593Smuzhiyun 
1304*4882a593Smuzhiyun 	label = aa_get_current_label();
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	/*
1307*4882a593Smuzhiyun 	 * Detect no new privs being set, and store the label it
1308*4882a593Smuzhiyun 	 * occurred under. Ideally this would happen when nnp
1309*4882a593Smuzhiyun 	 * is set but there isn't a good way to do that yet.
1310*4882a593Smuzhiyun 	 *
1311*4882a593Smuzhiyun 	 * Testing for unconfined must be done before the subset test
1312*4882a593Smuzhiyun 	 */
1313*4882a593Smuzhiyun 	if (task_no_new_privs(current) && !unconfined(label) && !ctx->nnp)
1314*4882a593Smuzhiyun 		ctx->nnp = aa_get_label(label);
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 	if (!fqname || !*fqname) {
1317*4882a593Smuzhiyun 		aa_put_label(label);
1318*4882a593Smuzhiyun 		AA_DEBUG("no profile name");
1319*4882a593Smuzhiyun 		return -EINVAL;
1320*4882a593Smuzhiyun 	}
1321*4882a593Smuzhiyun 
1322*4882a593Smuzhiyun 	if (flags & AA_CHANGE_ONEXEC) {
1323*4882a593Smuzhiyun 		request = AA_MAY_ONEXEC;
1324*4882a593Smuzhiyun 		if (stack)
1325*4882a593Smuzhiyun 			op = OP_STACK_ONEXEC;
1326*4882a593Smuzhiyun 		else
1327*4882a593Smuzhiyun 			op = OP_CHANGE_ONEXEC;
1328*4882a593Smuzhiyun 	} else {
1329*4882a593Smuzhiyun 		request = AA_MAY_CHANGE_PROFILE;
1330*4882a593Smuzhiyun 		if (stack)
1331*4882a593Smuzhiyun 			op = OP_STACK;
1332*4882a593Smuzhiyun 		else
1333*4882a593Smuzhiyun 			op = OP_CHANGE_PROFILE;
1334*4882a593Smuzhiyun 	}
1335*4882a593Smuzhiyun 
1336*4882a593Smuzhiyun 	if (*fqname == '&') {
1337*4882a593Smuzhiyun 		stack = true;
1338*4882a593Smuzhiyun 		/* don't have label_parse() do stacking */
1339*4882a593Smuzhiyun 		fqname++;
1340*4882a593Smuzhiyun 	}
1341*4882a593Smuzhiyun 	target = aa_label_parse(label, fqname, GFP_KERNEL, true, false);
1342*4882a593Smuzhiyun 	if (IS_ERR(target)) {
1343*4882a593Smuzhiyun 		struct aa_profile *tprofile;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 		info = "label not found";
1346*4882a593Smuzhiyun 		error = PTR_ERR(target);
1347*4882a593Smuzhiyun 		target = NULL;
1348*4882a593Smuzhiyun 		/*
1349*4882a593Smuzhiyun 		 * TODO: fixme using labels_profile is not right - do profile
1350*4882a593Smuzhiyun 		 * per complain profile
1351*4882a593Smuzhiyun 		 */
1352*4882a593Smuzhiyun 		if ((flags & AA_CHANGE_TEST) ||
1353*4882a593Smuzhiyun 		    !COMPLAIN_MODE(labels_profile(label)))
1354*4882a593Smuzhiyun 			goto audit;
1355*4882a593Smuzhiyun 		/* released below */
1356*4882a593Smuzhiyun 		tprofile = aa_new_null_profile(labels_profile(label), false,
1357*4882a593Smuzhiyun 					       fqname, GFP_KERNEL);
1358*4882a593Smuzhiyun 		if (!tprofile) {
1359*4882a593Smuzhiyun 			info = "failed null profile create";
1360*4882a593Smuzhiyun 			error = -ENOMEM;
1361*4882a593Smuzhiyun 			goto audit;
1362*4882a593Smuzhiyun 		}
1363*4882a593Smuzhiyun 		target = &tprofile->label;
1364*4882a593Smuzhiyun 		goto check;
1365*4882a593Smuzhiyun 	}
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	/*
1368*4882a593Smuzhiyun 	 * self directed transitions only apply to current policy ns
1369*4882a593Smuzhiyun 	 * TODO: currently requiring perms for stacking and straight change
1370*4882a593Smuzhiyun 	 *       stacking doesn't strictly need this. Determine how much
1371*4882a593Smuzhiyun 	 *       we want to loosen this restriction for stacking
1372*4882a593Smuzhiyun 	 *
1373*4882a593Smuzhiyun 	 * if (!stack) {
1374*4882a593Smuzhiyun 	 */
1375*4882a593Smuzhiyun 	error = fn_for_each_in_ns(label, profile,
1376*4882a593Smuzhiyun 			change_profile_perms_wrapper(op, auditname,
1377*4882a593Smuzhiyun 						     profile, target, stack,
1378*4882a593Smuzhiyun 						     request, &perms));
1379*4882a593Smuzhiyun 	if (error)
1380*4882a593Smuzhiyun 		/* auditing done in change_profile_perms_wrapper */
1381*4882a593Smuzhiyun 		goto out;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	/* } */
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun check:
1386*4882a593Smuzhiyun 	/* check if tracing task is allowed to trace target domain */
1387*4882a593Smuzhiyun 	error = may_change_ptraced_domain(target, &info);
1388*4882a593Smuzhiyun 	if (error && !fn_for_each_in_ns(label, profile,
1389*4882a593Smuzhiyun 					COMPLAIN_MODE(profile)))
1390*4882a593Smuzhiyun 		goto audit;
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun 	/* TODO: add permission check to allow this
1393*4882a593Smuzhiyun 	 * if ((flags & AA_CHANGE_ONEXEC) && !current_is_single_threaded()) {
1394*4882a593Smuzhiyun 	 *      info = "not a single threaded task";
1395*4882a593Smuzhiyun 	 *      error = -EACCES;
1396*4882a593Smuzhiyun 	 *      goto audit;
1397*4882a593Smuzhiyun 	 * }
1398*4882a593Smuzhiyun 	 */
1399*4882a593Smuzhiyun 	if (flags & AA_CHANGE_TEST)
1400*4882a593Smuzhiyun 		goto out;
1401*4882a593Smuzhiyun 
1402*4882a593Smuzhiyun 	/* stacking is always a subset, so only check the nonstack case */
1403*4882a593Smuzhiyun 	if (!stack) {
1404*4882a593Smuzhiyun 		new = fn_label_build_in_ns(label, profile, GFP_KERNEL,
1405*4882a593Smuzhiyun 					   aa_get_label(target),
1406*4882a593Smuzhiyun 					   aa_get_label(&profile->label));
1407*4882a593Smuzhiyun 		/*
1408*4882a593Smuzhiyun 		 * no new privs prevents domain transitions that would
1409*4882a593Smuzhiyun 		 * reduce restrictions.
1410*4882a593Smuzhiyun 		 */
1411*4882a593Smuzhiyun 		if (task_no_new_privs(current) && !unconfined(label) &&
1412*4882a593Smuzhiyun 		    !aa_label_is_unconfined_subset(new, ctx->nnp)) {
1413*4882a593Smuzhiyun 			/* not an apparmor denial per se, so don't log it */
1414*4882a593Smuzhiyun 			AA_DEBUG("no_new_privs - change_hat denied");
1415*4882a593Smuzhiyun 			error = -EPERM;
1416*4882a593Smuzhiyun 			goto out;
1417*4882a593Smuzhiyun 		}
1418*4882a593Smuzhiyun 	}
1419*4882a593Smuzhiyun 
1420*4882a593Smuzhiyun 	if (!(flags & AA_CHANGE_ONEXEC)) {
1421*4882a593Smuzhiyun 		/* only transition profiles in the current ns */
1422*4882a593Smuzhiyun 		if (stack)
1423*4882a593Smuzhiyun 			new = aa_label_merge(label, target, GFP_KERNEL);
1424*4882a593Smuzhiyun 		if (IS_ERR_OR_NULL(new)) {
1425*4882a593Smuzhiyun 			info = "failed to build target label";
1426*4882a593Smuzhiyun 			if (!new)
1427*4882a593Smuzhiyun 				error = -ENOMEM;
1428*4882a593Smuzhiyun 			else
1429*4882a593Smuzhiyun 				error = PTR_ERR(new);
1430*4882a593Smuzhiyun 			new = NULL;
1431*4882a593Smuzhiyun 			perms.allow = 0;
1432*4882a593Smuzhiyun 			goto audit;
1433*4882a593Smuzhiyun 		}
1434*4882a593Smuzhiyun 		error = aa_replace_current_label(new);
1435*4882a593Smuzhiyun 	} else {
1436*4882a593Smuzhiyun 		if (new) {
1437*4882a593Smuzhiyun 			aa_put_label(new);
1438*4882a593Smuzhiyun 			new = NULL;
1439*4882a593Smuzhiyun 		}
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 		/* full transition will be built in exec path */
1442*4882a593Smuzhiyun 		error = aa_set_current_onexec(target, stack);
1443*4882a593Smuzhiyun 	}
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun audit:
1446*4882a593Smuzhiyun 	error = fn_for_each_in_ns(label, profile,
1447*4882a593Smuzhiyun 			aa_audit_file(profile, &perms, op, request, auditname,
1448*4882a593Smuzhiyun 				      NULL, new ? new : target,
1449*4882a593Smuzhiyun 				      GLOBAL_ROOT_UID, info, error));
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun out:
1452*4882a593Smuzhiyun 	aa_put_label(new);
1453*4882a593Smuzhiyun 	aa_put_label(target);
1454*4882a593Smuzhiyun 	aa_put_label(label);
1455*4882a593Smuzhiyun 
1456*4882a593Smuzhiyun 	return error;
1457*4882a593Smuzhiyun }
1458