xref: /openbmc/linux/security/apparmor/policy.c (revision 690f33e1)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c88d4c7bSJohn Johansen /*
3c88d4c7bSJohn Johansen  * AppArmor security module
4c88d4c7bSJohn Johansen  *
5c88d4c7bSJohn Johansen  * This file contains AppArmor policy manipulation functions
6c88d4c7bSJohn Johansen  *
7c88d4c7bSJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
8c88d4c7bSJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
9c88d4c7bSJohn Johansen  *
10c88d4c7bSJohn Johansen  * AppArmor policy is based around profiles, which contain the rules a
11c88d4c7bSJohn Johansen  * task is confined by.  Every task in the system has a profile attached
12c88d4c7bSJohn Johansen  * to it determined either by matching "unconfined" tasks against the
13c88d4c7bSJohn Johansen  * visible set of profiles or by following a profiles attachment rules.
14c88d4c7bSJohn Johansen  *
15c88d4c7bSJohn Johansen  * Each profile exists in a profile namespace which is a container of
16c88d4c7bSJohn Johansen  * visible profiles.  Each namespace contains a special "unconfined" profile,
17c88d4c7bSJohn Johansen  * which doesn't enforce any confinement on a task beyond DAC.
18c88d4c7bSJohn Johansen  *
19c88d4c7bSJohn Johansen  * Namespace and profile names can be written together in either
20c88d4c7bSJohn Johansen  * of two syntaxes.
21c88d4c7bSJohn Johansen  *	:namespace:profile - used by kernel interfaces for easy detection
22c88d4c7bSJohn Johansen  *	namespace://profile - used by policy
23c88d4c7bSJohn Johansen  *
24c88d4c7bSJohn Johansen  * Profile names can not start with : or @ or ^ and may not contain \0
25c88d4c7bSJohn Johansen  *
26c88d4c7bSJohn Johansen  * Reserved profile names
27c88d4c7bSJohn Johansen  *	unconfined - special automatically generated unconfined profile
28c88d4c7bSJohn Johansen  *	inherit - special name to indicate profile inheritance
29c88d4c7bSJohn Johansen  *	null-XXXX-YYYY - special automatically generated learning profiles
30c88d4c7bSJohn Johansen  *
31c88d4c7bSJohn Johansen  * Namespace names may not start with / or @ and may not contain \0 or :
32c88d4c7bSJohn Johansen  * Reserved namespace names
33c88d4c7bSJohn Johansen  *	user-XXXX - user defined profiles
34c88d4c7bSJohn Johansen  *
35c88d4c7bSJohn Johansen  * a // in a profile or namespace name indicates a hierarchical name with the
36c88d4c7bSJohn Johansen  * name before the // being the parent and the name after the child.
37c88d4c7bSJohn Johansen  *
38c88d4c7bSJohn Johansen  * Profile and namespace hierarchies serve two different but similar purposes.
39c88d4c7bSJohn Johansen  * The namespace contains the set of visible profiles that are considered
40c88d4c7bSJohn Johansen  * for attachment.  The hierarchy of namespaces allows for virtualizing
41c88d4c7bSJohn Johansen  * the namespace so that for example a chroot can have its own set of profiles
42c88d4c7bSJohn Johansen  * which may define some local user namespaces.
43c88d4c7bSJohn Johansen  * The profile hierarchy severs two distinct purposes,
44c88d4c7bSJohn Johansen  * -  it allows for sub profiles or hats, which allows an application to run
45c88d4c7bSJohn Johansen  *    subprograms under its own profile with different restriction than it
46c88d4c7bSJohn Johansen  *    self, and not have it use the system profile.
47c88d4c7bSJohn Johansen  *    eg. if a mail program starts an editor, the policy might make the
48c88d4c7bSJohn Johansen  *        restrictions tighter on the editor tighter than the mail program,
49c88d4c7bSJohn Johansen  *        and definitely different than general editor restrictions
50c88d4c7bSJohn Johansen  * - it allows for binary hierarchy of profiles, so that execution history
51c88d4c7bSJohn Johansen  *   is preserved.  This feature isn't exploited by AppArmor reference policy
52c88d4c7bSJohn Johansen  *   but is allowed.  NOTE: this is currently suboptimal because profile
53c88d4c7bSJohn Johansen  *   aliasing is not currently implemented so that a profile for each
54c88d4c7bSJohn Johansen  *   level must be defined.
55c88d4c7bSJohn Johansen  *   eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started
56c88d4c7bSJohn Johansen  *       from /bin/bash
57c88d4c7bSJohn Johansen  *
58c88d4c7bSJohn Johansen  *   A profile or namespace name that can contain one or more // separators
59c88d4c7bSJohn Johansen  *   is referred to as an hname (hierarchical).
60c88d4c7bSJohn Johansen  *   eg.  /bin/bash//bin/ls
61c88d4c7bSJohn Johansen  *
62c88d4c7bSJohn Johansen  *   An fqname is a name that may contain both namespace and profile hnames.
63c88d4c7bSJohn Johansen  *   eg. :ns:/bin/bash//bin/ls
64c88d4c7bSJohn Johansen  *
65c88d4c7bSJohn Johansen  * NOTES:
66c88d4c7bSJohn Johansen  *   - locking of profile lists is currently fairly coarse.  All profile
67c88d4c7bSJohn Johansen  *     lists within a namespace use the namespace lock.
68c88d4c7bSJohn Johansen  * FIXME: move profile lists to using rcu_lists
69c88d4c7bSJohn Johansen  */
70c88d4c7bSJohn Johansen 
71c88d4c7bSJohn Johansen #include <linux/slab.h>
72c88d4c7bSJohn Johansen #include <linux/spinlock.h>
73c88d4c7bSJohn Johansen #include <linux/string.h>
745b825c3aSIngo Molnar #include <linux/cred.h>
75b2d09103SIngo Molnar #include <linux/rculist.h>
762bd8dbbfSJohn Johansen #include <linux/user_namespace.h>
77c88d4c7bSJohn Johansen 
78c88d4c7bSJohn Johansen #include "include/apparmor.h"
79c88d4c7bSJohn Johansen #include "include/capability.h"
80d8889d49SJohn Johansen #include "include/cred.h"
81c88d4c7bSJohn Johansen #include "include/file.h"
82c88d4c7bSJohn Johansen #include "include/ipc.h"
83c88d4c7bSJohn Johansen #include "include/match.h"
84c88d4c7bSJohn Johansen #include "include/path.h"
85c88d4c7bSJohn Johansen #include "include/policy.h"
86cff281f6SJohn Johansen #include "include/policy_ns.h"
87c88d4c7bSJohn Johansen #include "include/policy_unpack.h"
88c88d4c7bSJohn Johansen #include "include/resource.h"
89c88d4c7bSJohn Johansen 
902bd8dbbfSJohn Johansen int unprivileged_userns_apparmor_policy = 1;
91c88d4c7bSJohn Johansen 
920d259f04SJohn Johansen const char *const aa_profile_mode_names[] = {
93c88d4c7bSJohn Johansen 	"enforce",
94c88d4c7bSJohn Johansen 	"complain",
95c88d4c7bSJohn Johansen 	"kill",
9603816507SJohn Johansen 	"unconfined",
9722fac8a0SJohn Johansen 	"user",
98c88d4c7bSJohn Johansen };
99c88d4c7bSJohn Johansen 
100c88d4c7bSJohn Johansen 
101c88d4c7bSJohn Johansen /**
102637f688dSJohn Johansen  * __add_profile - add a profiles to list and label tree
103c88d4c7bSJohn Johansen  * @list: list to add it to  (NOT NULL)
104c88d4c7bSJohn Johansen  * @profile: the profile to add  (NOT NULL)
105c88d4c7bSJohn Johansen  *
106c88d4c7bSJohn Johansen  * refcount @profile, should be put by __list_remove_profile
107c88d4c7bSJohn Johansen  *
108c88d4c7bSJohn Johansen  * Requires: namespace lock be held, or list not be shared
109c88d4c7bSJohn Johansen  */
__add_profile(struct list_head * list,struct aa_profile * profile)110637f688dSJohn Johansen static void __add_profile(struct list_head *list, struct aa_profile *profile)
111c88d4c7bSJohn Johansen {
112637f688dSJohn Johansen 	struct aa_label *l;
113637f688dSJohn Johansen 
114637f688dSJohn Johansen 	AA_BUG(!list);
115637f688dSJohn Johansen 	AA_BUG(!profile);
116637f688dSJohn Johansen 	AA_BUG(!profile->ns);
117637f688dSJohn Johansen 	AA_BUG(!mutex_is_locked(&profile->ns->lock));
118637f688dSJohn Johansen 
11901e2b670SJohn Johansen 	list_add_rcu(&profile->base.list, list);
120c88d4c7bSJohn Johansen 	/* get list reference */
121c88d4c7bSJohn Johansen 	aa_get_profile(profile);
122637f688dSJohn Johansen 	l = aa_label_insert(&profile->ns->labels, &profile->label);
123637f688dSJohn Johansen 	AA_BUG(l != &profile->label);
124637f688dSJohn Johansen 	aa_put_label(l);
125c88d4c7bSJohn Johansen }
126c88d4c7bSJohn Johansen 
127c88d4c7bSJohn Johansen /**
128c88d4c7bSJohn Johansen  * __list_remove_profile - remove a profile from the list it is on
129c88d4c7bSJohn Johansen  * @profile: the profile to remove  (NOT NULL)
130c88d4c7bSJohn Johansen  *
131c88d4c7bSJohn Johansen  * remove a profile from the list, warning generally removal should
132c88d4c7bSJohn Johansen  * be done with __replace_profile as most profile removals are
133c88d4c7bSJohn Johansen  * replacements to the unconfined profile.
134c88d4c7bSJohn Johansen  *
135c88d4c7bSJohn Johansen  * put @profile list refcount
136c88d4c7bSJohn Johansen  *
137c88d4c7bSJohn Johansen  * Requires: namespace lock be held, or list not have been live
138c88d4c7bSJohn Johansen  */
__list_remove_profile(struct aa_profile * profile)139c88d4c7bSJohn Johansen static void __list_remove_profile(struct aa_profile *profile)
140c88d4c7bSJohn Johansen {
141637f688dSJohn Johansen 	AA_BUG(!profile);
142637f688dSJohn Johansen 	AA_BUG(!profile->ns);
143637f688dSJohn Johansen 	AA_BUG(!mutex_is_locked(&profile->ns->lock));
144637f688dSJohn Johansen 
14501e2b670SJohn Johansen 	list_del_rcu(&profile->base.list);
146c88d4c7bSJohn Johansen 	aa_put_profile(profile);
147c88d4c7bSJohn Johansen }
148c88d4c7bSJohn Johansen 
149c88d4c7bSJohn Johansen /**
150c88d4c7bSJohn Johansen  * __remove_profile - remove old profile, and children
151c88d4c7bSJohn Johansen  * @profile: profile to be replaced  (NOT NULL)
152c88d4c7bSJohn Johansen  *
153c88d4c7bSJohn Johansen  * Requires: namespace list lock be held, or list not be shared
154c88d4c7bSJohn Johansen  */
__remove_profile(struct aa_profile * profile)155c88d4c7bSJohn Johansen static void __remove_profile(struct aa_profile *profile)
156c88d4c7bSJohn Johansen {
157637f688dSJohn Johansen 	AA_BUG(!profile);
158637f688dSJohn Johansen 	AA_BUG(!profile->ns);
159637f688dSJohn Johansen 	AA_BUG(!mutex_is_locked(&profile->ns->lock));
160637f688dSJohn Johansen 
161c88d4c7bSJohn Johansen 	/* release any children lists first */
162cff281f6SJohn Johansen 	__aa_profile_list_release(&profile->base.profiles);
163c88d4c7bSJohn Johansen 	/* released by free_profile */
164637f688dSJohn Johansen 	aa_label_remove(&profile->label);
165c97204baSJohn Johansen 	__aafs_profile_rmdir(profile);
166c88d4c7bSJohn Johansen 	__list_remove_profile(profile);
167c88d4c7bSJohn Johansen }
168c88d4c7bSJohn Johansen 
169c88d4c7bSJohn Johansen /**
170cff281f6SJohn Johansen  * __aa_profile_list_release - remove all profiles on the list and put refs
171c88d4c7bSJohn Johansen  * @head: list of profiles  (NOT NULL)
172c88d4c7bSJohn Johansen  *
173c88d4c7bSJohn Johansen  * Requires: namespace lock be held
174c88d4c7bSJohn Johansen  */
__aa_profile_list_release(struct list_head * head)175cff281f6SJohn Johansen void __aa_profile_list_release(struct list_head *head)
176c88d4c7bSJohn Johansen {
177c88d4c7bSJohn Johansen 	struct aa_profile *profile, *tmp;
178c88d4c7bSJohn Johansen 	list_for_each_entry_safe(profile, tmp, head, base.list)
179c88d4c7bSJohn Johansen 		__remove_profile(profile);
180c88d4c7bSJohn Johansen }
181c88d4c7bSJohn Johansen 
182c88d4c7bSJohn Johansen /**
183e025be0fSWilliam Hua  * aa_free_data - free a data blob
184e025be0fSWilliam Hua  * @ptr: data to free
185e025be0fSWilliam Hua  * @arg: unused
186e025be0fSWilliam Hua  */
aa_free_data(void * ptr,void * arg)187e025be0fSWilliam Hua static void aa_free_data(void *ptr, void *arg)
188e025be0fSWilliam Hua {
189e025be0fSWilliam Hua 	struct aa_data *data = ptr;
190e025be0fSWilliam Hua 
191453431a5SWaiman Long 	kfree_sensitive(data->data);
192453431a5SWaiman Long 	kfree_sensitive(data->key);
193453431a5SWaiman Long 	kfree_sensitive(data);
194e025be0fSWilliam Hua }
195e025be0fSWilliam Hua 
free_attachment(struct aa_attachment * attach)196217af7e2SJohn Johansen static void free_attachment(struct aa_attachment *attach)
197217af7e2SJohn Johansen {
198217af7e2SJohn Johansen 	int i;
199217af7e2SJohn Johansen 
200217af7e2SJohn Johansen 	for (i = 0; i < attach->xattr_count; i++)
201217af7e2SJohn Johansen 		kfree_sensitive(attach->xattrs[i]);
202217af7e2SJohn Johansen 	kfree_sensitive(attach->xattrs);
203217af7e2SJohn Johansen 	aa_destroy_policydb(&attach->xmatch);
204217af7e2SJohn Johansen }
205217af7e2SJohn Johansen 
free_ruleset(struct aa_ruleset * rules)206217af7e2SJohn Johansen static void free_ruleset(struct aa_ruleset *rules)
207217af7e2SJohn Johansen {
208217af7e2SJohn Johansen 	int i;
209217af7e2SJohn Johansen 
210217af7e2SJohn Johansen 	aa_destroy_policydb(&rules->file);
211217af7e2SJohn Johansen 	aa_destroy_policydb(&rules->policy);
212217af7e2SJohn Johansen 	aa_free_cap_rules(&rules->caps);
213217af7e2SJohn Johansen 	aa_free_rlimit_rules(&rules->rlimits);
214217af7e2SJohn Johansen 
215217af7e2SJohn Johansen 	for (i = 0; i < rules->secmark_count; i++)
216217af7e2SJohn Johansen 		kfree_sensitive(rules->secmark[i].label);
217217af7e2SJohn Johansen 	kfree_sensitive(rules->secmark);
2187dd426e3SGaosheng Cui 	kfree_sensitive(rules);
219217af7e2SJohn Johansen }
220217af7e2SJohn Johansen 
aa_alloc_ruleset(gfp_t gfp)2211ad22fccSJohn Johansen struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp)
2221ad22fccSJohn Johansen {
2231ad22fccSJohn Johansen 	struct aa_ruleset *rules;
2241ad22fccSJohn Johansen 
2251ad22fccSJohn Johansen 	rules = kzalloc(sizeof(*rules), gfp);
2261ad22fccSJohn Johansen 	if (rules)
2271ad22fccSJohn Johansen 		INIT_LIST_HEAD(&rules->list);
2281ad22fccSJohn Johansen 
2291ad22fccSJohn Johansen 	return rules;
2301ad22fccSJohn Johansen }
2311ad22fccSJohn Johansen 
232e025be0fSWilliam Hua /**
2338651e1d6SJohn Johansen  * aa_free_profile - free a profile
234c88d4c7bSJohn Johansen  * @profile: the profile to free  (MAYBE NULL)
235c88d4c7bSJohn Johansen  *
236c88d4c7bSJohn Johansen  * Free a profile, its hats and null_profile. All references to the profile,
237c88d4c7bSJohn Johansen  * its hats and null_profile must have been put.
238c88d4c7bSJohn Johansen  *
239c88d4c7bSJohn Johansen  * If the profile was referenced from a task context, free_profile() will
240c88d4c7bSJohn Johansen  * be called from an rcu callback routine, so we must not sleep here.
241c88d4c7bSJohn Johansen  */
aa_free_profile(struct aa_profile * profile)2428651e1d6SJohn Johansen void aa_free_profile(struct aa_profile *profile)
243c88d4c7bSJohn Johansen {
2441ad22fccSJohn Johansen 	struct aa_ruleset *rule, *tmp;
245e025be0fSWilliam Hua 	struct rhashtable *rht;
246e025be0fSWilliam Hua 
247c88d4c7bSJohn Johansen 	AA_DEBUG("%s(%p)\n", __func__, profile);
248c88d4c7bSJohn Johansen 
249c88d4c7bSJohn Johansen 	if (!profile)
250c88d4c7bSJohn Johansen 		return;
251c88d4c7bSJohn Johansen 
252c88d4c7bSJohn Johansen 	/* free children profiles */
253fe6bb31fSJohn Johansen 	aa_policy_destroy(&profile->base);
25401e2b670SJohn Johansen 	aa_put_profile(rcu_access_pointer(profile->parent));
255c88d4c7bSJohn Johansen 
25698849dffSJohn Johansen 	aa_put_ns(profile->ns);
257453431a5SWaiman Long 	kfree_sensitive(profile->rename);
2588d0d83f5SGeorgia Garcia 	kfree_sensitive(profile->disconnected);
259c88d4c7bSJohn Johansen 
260217af7e2SJohn Johansen 	free_attachment(&profile->attach);
2611ad22fccSJohn Johansen 
2621ad22fccSJohn Johansen 	/*
2631ad22fccSJohn Johansen 	 * at this point there are no tasks that can have a reference
2641ad22fccSJohn Johansen 	 * to rules
2651ad22fccSJohn Johansen 	 */
2661ad22fccSJohn Johansen 	list_for_each_entry_safe(rule, tmp, &profile->rules, list) {
2671ad22fccSJohn Johansen 		list_del_init(&rule->list);
2681ad22fccSJohn Johansen 		free_ruleset(rule);
2691ad22fccSJohn Johansen 	}
270453431a5SWaiman Long 	kfree_sensitive(profile->dirname);
271217af7e2SJohn Johansen 
272e025be0fSWilliam Hua 	if (profile->data) {
273e025be0fSWilliam Hua 		rht = profile->data;
274e025be0fSWilliam Hua 		profile->data = NULL;
275e025be0fSWilliam Hua 		rhashtable_free_and_destroy(rht, aa_free_data, NULL);
276453431a5SWaiman Long 		kfree_sensitive(rht);
277e025be0fSWilliam Hua 	}
278e025be0fSWilliam Hua 
279453431a5SWaiman Long 	kfree_sensitive(profile->hash);
2805ac8c355SJohn Johansen 	aa_put_loaddata(profile->rawdata);
2813622ad25SJohn Johansen 	aa_label_destroy(&profile->label);
282637f688dSJohn Johansen 
283453431a5SWaiman Long 	kfree_sensitive(profile);
284c88d4c7bSJohn Johansen }
285c88d4c7bSJohn Johansen 
286c88d4c7bSJohn Johansen /**
2874da05cc0SJohn Johansen  * aa_alloc_profile - allocate, initialize and return a new profile
2884da05cc0SJohn Johansen  * @hname: name of the profile  (NOT NULL)
28975ae5a78SGaosheng Cui  * @proxy: proxy to use OR null if to allocate a new one
29030b026a8SJohn Johansen  * @gfp: allocation type
2914da05cc0SJohn Johansen  *
2924da05cc0SJohn Johansen  * Returns: refcount profile or NULL on failure
2934da05cc0SJohn Johansen  */
aa_alloc_profile(const char * hname,struct aa_proxy * proxy,gfp_t gfp)294637f688dSJohn Johansen struct aa_profile *aa_alloc_profile(const char *hname, struct aa_proxy *proxy,
295637f688dSJohn Johansen 				    gfp_t gfp)
2964da05cc0SJohn Johansen {
2974da05cc0SJohn Johansen 	struct aa_profile *profile;
2981ad22fccSJohn Johansen 	struct aa_ruleset *rules;
2994da05cc0SJohn Johansen 
3004da05cc0SJohn Johansen 	/* freed by free_profile - usually through aa_put_profile */
301f4a2d282SGustavo A. R. Silva 	profile = kzalloc(struct_size(profile, label.vec, 2), gfp);
3024da05cc0SJohn Johansen 	if (!profile)
3034da05cc0SJohn Johansen 		return NULL;
3044da05cc0SJohn Johansen 
30530b026a8SJohn Johansen 	if (!aa_policy_init(&profile->base, NULL, hname, gfp))
30677b071b3SJohn Johansen 		goto fail;
307c0929212SJohn Johansen 	if (!aa_label_init(&profile->label, 1, gfp))
308637f688dSJohn Johansen 		goto fail;
309637f688dSJohn Johansen 
3101ad22fccSJohn Johansen 	INIT_LIST_HEAD(&profile->rules);
3111ad22fccSJohn Johansen 
3121ad22fccSJohn Johansen 	/* allocate the first ruleset, but leave it empty */
3131ad22fccSJohn Johansen 	rules = aa_alloc_ruleset(gfp);
3141ad22fccSJohn Johansen 	if (!rules)
3151ad22fccSJohn Johansen 		goto fail;
3161ad22fccSJohn Johansen 	list_add(&rules->list, &profile->rules);
3171ad22fccSJohn Johansen 
318637f688dSJohn Johansen 	/* update being set needed by fs interface */
319637f688dSJohn Johansen 	if (!proxy) {
320637f688dSJohn Johansen 		proxy = aa_alloc_proxy(&profile->label, gfp);
321637f688dSJohn Johansen 		if (!proxy)
322637f688dSJohn Johansen 			goto fail;
323637f688dSJohn Johansen 	} else
324637f688dSJohn Johansen 		aa_get_proxy(proxy);
325637f688dSJohn Johansen 	profile->label.proxy = proxy;
326637f688dSJohn Johansen 
327637f688dSJohn Johansen 	profile->label.hname = profile->base.hname;
328637f688dSJohn Johansen 	profile->label.flags |= FLAG_PROFILE;
329637f688dSJohn Johansen 	profile->label.vec[0] = profile;
3304da05cc0SJohn Johansen 
3314da05cc0SJohn Johansen 	/* refcount released by caller */
3324da05cc0SJohn Johansen 	return profile;
33377b071b3SJohn Johansen 
33477b071b3SJohn Johansen fail:
335637f688dSJohn Johansen 	aa_free_profile(profile);
33677b071b3SJohn Johansen 
33777b071b3SJohn Johansen 	return NULL;
3384da05cc0SJohn Johansen }
3394da05cc0SJohn Johansen 
340c88d4c7bSJohn Johansen /* TODO: profile accounting - setup in remove */
341c88d4c7bSJohn Johansen 
342c88d4c7bSJohn Johansen /**
343c88d4c7bSJohn Johansen  * __strn_find_child - find a profile on @head list using substring of @name
344c88d4c7bSJohn Johansen  * @head: list to search  (NOT NULL)
345c88d4c7bSJohn Johansen  * @name: name of profile (NOT NULL)
346c88d4c7bSJohn Johansen  * @len: length of @name substring to match
347c88d4c7bSJohn Johansen  *
34801e2b670SJohn Johansen  * Requires: rcu_read_lock be held
349c88d4c7bSJohn Johansen  *
350c88d4c7bSJohn Johansen  * Returns: unrefcounted profile ptr, or NULL if not found
351c88d4c7bSJohn Johansen  */
__strn_find_child(struct list_head * head,const char * name,int len)352c88d4c7bSJohn Johansen static struct aa_profile *__strn_find_child(struct list_head *head,
353c88d4c7bSJohn Johansen 					    const char *name, int len)
354c88d4c7bSJohn Johansen {
355c88d4c7bSJohn Johansen 	return (struct aa_profile *)__policy_strn_find(head, name, len);
356c88d4c7bSJohn Johansen }
357c88d4c7bSJohn Johansen 
358c88d4c7bSJohn Johansen /**
359ae3b3165SJohn Johansen  * __find_child - find a profile on @head list with a name matching @name
360ae3b3165SJohn Johansen  * @head: list to search  (NOT NULL)
361ae3b3165SJohn Johansen  * @name: name of profile (NOT NULL)
362ae3b3165SJohn Johansen  *
363ae3b3165SJohn Johansen  * Requires: rcu_read_lock be held
364ae3b3165SJohn Johansen  *
365ae3b3165SJohn Johansen  * Returns: unrefcounted profile ptr, or NULL if not found
366ae3b3165SJohn Johansen  */
__find_child(struct list_head * head,const char * name)367ae3b3165SJohn Johansen static struct aa_profile *__find_child(struct list_head *head, const char *name)
368ae3b3165SJohn Johansen {
369ae3b3165SJohn Johansen 	return __strn_find_child(head, name, strlen(name));
370ae3b3165SJohn Johansen }
371ae3b3165SJohn Johansen 
372ae3b3165SJohn Johansen /**
373c88d4c7bSJohn Johansen  * aa_find_child - find a profile by @name in @parent
374c88d4c7bSJohn Johansen  * @parent: profile to search  (NOT NULL)
375c88d4c7bSJohn Johansen  * @name: profile name to search for  (NOT NULL)
376c88d4c7bSJohn Johansen  *
377c88d4c7bSJohn Johansen  * Returns: a refcounted profile or NULL if not found
378c88d4c7bSJohn Johansen  */
aa_find_child(struct aa_profile * parent,const char * name)379c88d4c7bSJohn Johansen struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
380c88d4c7bSJohn Johansen {
381c88d4c7bSJohn Johansen 	struct aa_profile *profile;
382c88d4c7bSJohn Johansen 
38301e2b670SJohn Johansen 	rcu_read_lock();
384de7c4cc9SJohn Johansen 	do {
385de7c4cc9SJohn Johansen 		profile = __find_child(&parent->base.profiles, name);
386de7c4cc9SJohn Johansen 	} while (profile && !aa_get_profile_not0(profile));
38701e2b670SJohn Johansen 	rcu_read_unlock();
388c88d4c7bSJohn Johansen 
389c88d4c7bSJohn Johansen 	/* refcount released by caller */
390c88d4c7bSJohn Johansen 	return profile;
391c88d4c7bSJohn Johansen }
392c88d4c7bSJohn Johansen 
393c88d4c7bSJohn Johansen /**
394c88d4c7bSJohn Johansen  * __lookup_parent - lookup the parent of a profile of name @hname
395c88d4c7bSJohn Johansen  * @ns: namespace to lookup profile in  (NOT NULL)
396c88d4c7bSJohn Johansen  * @hname: hierarchical profile name to find parent of  (NOT NULL)
397c88d4c7bSJohn Johansen  *
398c88d4c7bSJohn Johansen  * Lookups up the parent of a fully qualified profile name, the profile
399c88d4c7bSJohn Johansen  * that matches hname does not need to exist, in general this
400c88d4c7bSJohn Johansen  * is used to load a new profile.
401c88d4c7bSJohn Johansen  *
40201e2b670SJohn Johansen  * Requires: rcu_read_lock be held
403c88d4c7bSJohn Johansen  *
404c88d4c7bSJohn Johansen  * Returns: unrefcounted policy or NULL if not found
405c88d4c7bSJohn Johansen  */
__lookup_parent(struct aa_ns * ns,const char * hname)40698849dffSJohn Johansen static struct aa_policy *__lookup_parent(struct aa_ns *ns,
407c88d4c7bSJohn Johansen 					 const char *hname)
408c88d4c7bSJohn Johansen {
409c88d4c7bSJohn Johansen 	struct aa_policy *policy;
410c88d4c7bSJohn Johansen 	struct aa_profile *profile = NULL;
411c88d4c7bSJohn Johansen 	char *split;
412c88d4c7bSJohn Johansen 
413c88d4c7bSJohn Johansen 	policy = &ns->base;
414c88d4c7bSJohn Johansen 
415c88d4c7bSJohn Johansen 	for (split = strstr(hname, "//"); split;) {
416c88d4c7bSJohn Johansen 		profile = __strn_find_child(&policy->profiles, hname,
417c88d4c7bSJohn Johansen 					    split - hname);
418c88d4c7bSJohn Johansen 		if (!profile)
419c88d4c7bSJohn Johansen 			return NULL;
420c88d4c7bSJohn Johansen 		policy = &profile->base;
421c88d4c7bSJohn Johansen 		hname = split + 2;
422c88d4c7bSJohn Johansen 		split = strstr(hname, "//");
423c88d4c7bSJohn Johansen 	}
424c88d4c7bSJohn Johansen 	if (!profile)
425c88d4c7bSJohn Johansen 		return &ns->base;
426c88d4c7bSJohn Johansen 	return &profile->base;
427c88d4c7bSJohn Johansen }
428c88d4c7bSJohn Johansen 
429c88d4c7bSJohn Johansen /**
430665b1856SJohn Johansen  * __create_missing_ancestors - create place holders for missing ancestores
431665b1856SJohn Johansen  * @ns: namespace to lookup profile in (NOT NULL)
432665b1856SJohn Johansen  * @hname: hierarchical profile name to find parent of (NOT NULL)
433665b1856SJohn Johansen  * @gfp: type of allocation.
434665b1856SJohn Johansen  *
435665b1856SJohn Johansen  * Requires: ns mutex lock held
436665b1856SJohn Johansen  *
43776862af5SRandy Dunlap  * Return: unrefcounted parent policy on success or %NULL if error creating
438665b1856SJohn Johansen  *          place holder profiles.
439665b1856SJohn Johansen  */
__create_missing_ancestors(struct aa_ns * ns,const char * hname,gfp_t gfp)440665b1856SJohn Johansen static struct aa_policy *__create_missing_ancestors(struct aa_ns *ns,
441665b1856SJohn Johansen 						    const char *hname,
442665b1856SJohn Johansen 						    gfp_t gfp)
443665b1856SJohn Johansen {
444665b1856SJohn Johansen 	struct aa_policy *policy;
445665b1856SJohn Johansen 	struct aa_profile *parent, *profile = NULL;
446665b1856SJohn Johansen 	char *split;
447665b1856SJohn Johansen 
448665b1856SJohn Johansen 	AA_BUG(!ns);
449665b1856SJohn Johansen 	AA_BUG(!hname);
450665b1856SJohn Johansen 
451665b1856SJohn Johansen 	policy = &ns->base;
452665b1856SJohn Johansen 
453665b1856SJohn Johansen 	for (split = strstr(hname, "//"); split;) {
454665b1856SJohn Johansen 		parent = profile;
455665b1856SJohn Johansen 		profile = __strn_find_child(&policy->profiles, hname,
456665b1856SJohn Johansen 					    split - hname);
457665b1856SJohn Johansen 		if (!profile) {
458665b1856SJohn Johansen 			const char *name = kstrndup(hname, split - hname,
459665b1856SJohn Johansen 						    gfp);
460665b1856SJohn Johansen 			if (!name)
461665b1856SJohn Johansen 				return NULL;
462665b1856SJohn Johansen 			profile = aa_alloc_null(parent, name, gfp);
463665b1856SJohn Johansen 			kfree(name);
464665b1856SJohn Johansen 			if (!profile)
465665b1856SJohn Johansen 				return NULL;
466665b1856SJohn Johansen 			if (!parent)
467665b1856SJohn Johansen 				profile->ns = aa_get_ns(ns);
468665b1856SJohn Johansen 		}
469665b1856SJohn Johansen 		policy = &profile->base;
470665b1856SJohn Johansen 		hname = split + 2;
471665b1856SJohn Johansen 		split = strstr(hname, "//");
472665b1856SJohn Johansen 	}
473665b1856SJohn Johansen 	if (!profile)
474665b1856SJohn Johansen 		return &ns->base;
475665b1856SJohn Johansen 	return &profile->base;
476665b1856SJohn Johansen }
477665b1856SJohn Johansen 
478665b1856SJohn Johansen /**
4791741e9ebSJohn Johansen  * __lookupn_profile - lookup the profile matching @hname
480c88d4c7bSJohn Johansen  * @base: base list to start looking up profile name from  (NOT NULL)
481c88d4c7bSJohn Johansen  * @hname: hierarchical profile name  (NOT NULL)
4821741e9ebSJohn Johansen  * @n: length of @hname
483c88d4c7bSJohn Johansen  *
48401e2b670SJohn Johansen  * Requires: rcu_read_lock be held
485c88d4c7bSJohn Johansen  *
486c88d4c7bSJohn Johansen  * Returns: unrefcounted profile pointer or NULL if not found
487c88d4c7bSJohn Johansen  *
488c88d4c7bSJohn Johansen  * Do a relative name lookup, recursing through profile tree.
489c88d4c7bSJohn Johansen  */
__lookupn_profile(struct aa_policy * base,const char * hname,size_t n)4901741e9ebSJohn Johansen static struct aa_profile *__lookupn_profile(struct aa_policy *base,
4911741e9ebSJohn Johansen 					    const char *hname, size_t n)
492c88d4c7bSJohn Johansen {
493c88d4c7bSJohn Johansen 	struct aa_profile *profile = NULL;
4941741e9ebSJohn Johansen 	const char *split;
495c88d4c7bSJohn Johansen 
4961741e9ebSJohn Johansen 	for (split = strnstr(hname, "//", n); split;
4971741e9ebSJohn Johansen 	     split = strnstr(hname, "//", n)) {
498c88d4c7bSJohn Johansen 		profile = __strn_find_child(&base->profiles, hname,
499c88d4c7bSJohn Johansen 					    split - hname);
500c88d4c7bSJohn Johansen 		if (!profile)
501c88d4c7bSJohn Johansen 			return NULL;
502c88d4c7bSJohn Johansen 
503c88d4c7bSJohn Johansen 		base = &profile->base;
5041741e9ebSJohn Johansen 		n -= split + 2 - hname;
505c88d4c7bSJohn Johansen 		hname = split + 2;
506c88d4c7bSJohn Johansen 	}
507c88d4c7bSJohn Johansen 
5081741e9ebSJohn Johansen 	if (n)
5091741e9ebSJohn Johansen 		return __strn_find_child(&base->profiles, hname, n);
5101741e9ebSJohn Johansen 	return NULL;
5111741e9ebSJohn Johansen }
512c88d4c7bSJohn Johansen 
__lookup_profile(struct aa_policy * base,const char * hname)5131741e9ebSJohn Johansen static struct aa_profile *__lookup_profile(struct aa_policy *base,
5141741e9ebSJohn Johansen 					   const char *hname)
5151741e9ebSJohn Johansen {
5161741e9ebSJohn Johansen 	return __lookupn_profile(base, hname, strlen(hname));
517c88d4c7bSJohn Johansen }
518c88d4c7bSJohn Johansen 
519c88d4c7bSJohn Johansen /**
520240516dfSYang Li  * aa_lookupn_profile - find a profile by its full or partial name
521c88d4c7bSJohn Johansen  * @ns: the namespace to start from (NOT NULL)
522c88d4c7bSJohn Johansen  * @hname: name to do lookup on.  Does not contain namespace prefix (NOT NULL)
5231741e9ebSJohn Johansen  * @n: size of @hname
524c88d4c7bSJohn Johansen  *
525c88d4c7bSJohn Johansen  * Returns: refcounted profile or NULL if not found
526c88d4c7bSJohn Johansen  */
aa_lookupn_profile(struct aa_ns * ns,const char * hname,size_t n)5271741e9ebSJohn Johansen struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
5281741e9ebSJohn Johansen 				      size_t n)
529c88d4c7bSJohn Johansen {
530c88d4c7bSJohn Johansen 	struct aa_profile *profile;
531c88d4c7bSJohn Johansen 
53201e2b670SJohn Johansen 	rcu_read_lock();
53301e2b670SJohn Johansen 	do {
5341741e9ebSJohn Johansen 		profile = __lookupn_profile(&ns->base, hname, n);
53501e2b670SJohn Johansen 	} while (profile && !aa_get_profile_not0(profile));
53601e2b670SJohn Johansen 	rcu_read_unlock();
537c88d4c7bSJohn Johansen 
538bf83208eSJohn Johansen 	/* the unconfined profile is not in the regular profile list */
5391741e9ebSJohn Johansen 	if (!profile && strncmp(hname, "unconfined", n) == 0)
540fa2ac468SJohn Johansen 		profile = aa_get_newest_profile(ns->unconfined);
541bf83208eSJohn Johansen 
542c88d4c7bSJohn Johansen 	/* refcount released by caller */
543c88d4c7bSJohn Johansen 	return profile;
544c88d4c7bSJohn Johansen }
545c88d4c7bSJohn Johansen 
aa_lookup_profile(struct aa_ns * ns,const char * hname)5461741e9ebSJohn Johansen struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *hname)
5471741e9ebSJohn Johansen {
5481741e9ebSJohn Johansen 	return aa_lookupn_profile(ns, hname, strlen(hname));
5491741e9ebSJohn Johansen }
55031617ddfSJohn Johansen 
aa_fqlookupn_profile(struct aa_label * base,const char * fqname,size_t n)551637f688dSJohn Johansen struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
55231617ddfSJohn Johansen 					const char *fqname, size_t n)
55331617ddfSJohn Johansen {
55431617ddfSJohn Johansen 	struct aa_profile *profile;
55531617ddfSJohn Johansen 	struct aa_ns *ns;
55631617ddfSJohn Johansen 	const char *name, *ns_name;
55731617ddfSJohn Johansen 	size_t ns_len;
55831617ddfSJohn Johansen 
55931617ddfSJohn Johansen 	name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
56031617ddfSJohn Johansen 	if (ns_name) {
561637f688dSJohn Johansen 		ns = aa_lookupn_ns(labels_ns(base), ns_name, ns_len);
56231617ddfSJohn Johansen 		if (!ns)
56331617ddfSJohn Johansen 			return NULL;
56431617ddfSJohn Johansen 	} else
565637f688dSJohn Johansen 		ns = aa_get_ns(labels_ns(base));
56631617ddfSJohn Johansen 
56731617ddfSJohn Johansen 	if (name)
56831617ddfSJohn Johansen 		profile = aa_lookupn_profile(ns, name, n - (name - fqname));
56931617ddfSJohn Johansen 	else if (ns)
57031617ddfSJohn Johansen 		/* default profile for ns, currently unconfined */
57131617ddfSJohn Johansen 		profile = aa_get_newest_profile(ns->unconfined);
57231617ddfSJohn Johansen 	else
57331617ddfSJohn Johansen 		profile = NULL;
57431617ddfSJohn Johansen 	aa_put_ns(ns);
57531617ddfSJohn Johansen 
57631617ddfSJohn Johansen 	return profile;
57731617ddfSJohn Johansen }
57831617ddfSJohn Johansen 
57958f89ce5SJohn Johansen 
aa_alloc_null(struct aa_profile * parent,const char * name,gfp_t gfp)58058f89ce5SJohn Johansen struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name,
58158f89ce5SJohn Johansen 				 gfp_t gfp)
58258f89ce5SJohn Johansen {
58358f89ce5SJohn Johansen 	struct aa_profile *profile;
58458f89ce5SJohn Johansen 	struct aa_ruleset *rules;
58558f89ce5SJohn Johansen 
58658f89ce5SJohn Johansen 	profile = aa_alloc_profile(name, NULL, gfp);
58758f89ce5SJohn Johansen 	if (!profile)
58858f89ce5SJohn Johansen 		return NULL;
58958f89ce5SJohn Johansen 
59058f89ce5SJohn Johansen 	/* TODO: ideally we should inherit abi from parent */
59158f89ce5SJohn Johansen 	profile->label.flags |= FLAG_NULL;
59258f89ce5SJohn Johansen 	rules = list_first_entry(&profile->rules, typeof(*rules), list);
59358f89ce5SJohn Johansen 	rules->file.dfa = aa_get_dfa(nulldfa);
594ec6851aeSJohn Johansen 	rules->file.perms = kcalloc(2, sizeof(struct aa_perms), GFP_KERNEL);
595ec6851aeSJohn Johansen 	if (!rules->file.perms)
596ec6851aeSJohn Johansen 		goto fail;
597ec6851aeSJohn Johansen 	rules->file.size = 2;
59858f89ce5SJohn Johansen 	rules->policy.dfa = aa_get_dfa(nulldfa);
599ec6851aeSJohn Johansen 	rules->policy.perms = kcalloc(2, sizeof(struct aa_perms), GFP_KERNEL);
600ec6851aeSJohn Johansen 	if (!rules->policy.perms)
601ec6851aeSJohn Johansen 		goto fail;
602ec6851aeSJohn Johansen 	rules->policy.size = 2;
60358f89ce5SJohn Johansen 
60458f89ce5SJohn Johansen 	if (parent) {
60558f89ce5SJohn Johansen 		profile->path_flags = parent->path_flags;
60658f89ce5SJohn Johansen 
60758f89ce5SJohn Johansen 		/* released on free_profile */
60858f89ce5SJohn Johansen 		rcu_assign_pointer(profile->parent, aa_get_profile(parent));
60958f89ce5SJohn Johansen 		profile->ns = aa_get_ns(parent->ns);
61058f89ce5SJohn Johansen 	}
61158f89ce5SJohn Johansen 
61258f89ce5SJohn Johansen 	return profile;
613ec6851aeSJohn Johansen 
614ec6851aeSJohn Johansen fail:
615ec6851aeSJohn Johansen 	aa_free_profile(profile);
616ec6851aeSJohn Johansen 
617ec6851aeSJohn Johansen 	return NULL;
61858f89ce5SJohn Johansen }
61958f89ce5SJohn Johansen 
620c88d4c7bSJohn Johansen /**
62158f89ce5SJohn Johansen  * aa_new_learning_profile - create or find a null-X learning profile
622d07881d2SJohn Johansen  * @parent: profile that caused this profile to be created (NOT NULL)
623d07881d2SJohn Johansen  * @hat: true if the null- learning profile is a hat
624d07881d2SJohn Johansen  * @base: name to base the null profile off of
625d07881d2SJohn Johansen  * @gfp: type of allocation
626d07881d2SJohn Johansen  *
627d07881d2SJohn Johansen  * Find/Create a null- complain mode profile used in learning mode.  The
628d07881d2SJohn Johansen  * name of the profile is unique and follows the format of parent//null-XXX.
629d07881d2SJohn Johansen  * where XXX is based on the @name or if that fails or is not supplied
630d07881d2SJohn Johansen  * a unique number
631d07881d2SJohn Johansen  *
632d07881d2SJohn Johansen  * null profiles are added to the profile list but the list does not
633d07881d2SJohn Johansen  * hold a count on them so that they are automatically released when
634d07881d2SJohn Johansen  * not in use.
635d07881d2SJohn Johansen  *
636d07881d2SJohn Johansen  * Returns: new refcounted profile else NULL on failure
637d07881d2SJohn Johansen  */
aa_new_learning_profile(struct aa_profile * parent,bool hat,const char * base,gfp_t gfp)63858f89ce5SJohn Johansen struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
639d07881d2SJohn Johansen 					   const char *base, gfp_t gfp)
640d07881d2SJohn Johansen {
641290638a5SJohn Johansen 	struct aa_profile *p, *profile;
642290638a5SJohn Johansen 	const char *bname;
6434633307eSJohn Johansen 	char *name = NULL;
644d07881d2SJohn Johansen 
645d07881d2SJohn Johansen 	AA_BUG(!parent);
646d07881d2SJohn Johansen 
647d07881d2SJohn Johansen 	if (base) {
648d07881d2SJohn Johansen 		name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
649d07881d2SJohn Johansen 			       gfp);
650d07881d2SJohn Johansen 		if (name) {
651d07881d2SJohn Johansen 			sprintf(name, "%s//null-%s", parent->base.hname, base);
652d07881d2SJohn Johansen 			goto name;
653d07881d2SJohn Johansen 		}
654d07881d2SJohn Johansen 		/* fall through to try shorter uniq */
655d07881d2SJohn Johansen 	}
656d07881d2SJohn Johansen 
657d07881d2SJohn Johansen 	name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
658d07881d2SJohn Johansen 	if (!name)
659d07881d2SJohn Johansen 		return NULL;
660d07881d2SJohn Johansen 	sprintf(name, "%s//null-%x", parent->base.hname,
661d07881d2SJohn Johansen 		atomic_inc_return(&parent->ns->uniq_null));
662d07881d2SJohn Johansen 
663d07881d2SJohn Johansen name:
664d07881d2SJohn Johansen 	/* lookup to see if this is a dup creation */
665290638a5SJohn Johansen 	bname = basename(name);
666290638a5SJohn Johansen 	profile = aa_find_child(parent, bname);
667d07881d2SJohn Johansen 	if (profile)
668d07881d2SJohn Johansen 		goto out;
669d07881d2SJohn Johansen 
67058f89ce5SJohn Johansen 	profile = aa_alloc_null(parent, name, gfp);
671d07881d2SJohn Johansen 	if (!profile)
672d07881d2SJohn Johansen 		goto fail;
673d07881d2SJohn Johansen 	profile->mode = APPARMOR_COMPLAIN;
674d07881d2SJohn Johansen 	if (hat)
675d07881d2SJohn Johansen 		profile->label.flags |= FLAG_HAT;
676d07881d2SJohn Johansen 
677feb3c766SJohn Johansen 	mutex_lock_nested(&profile->ns->lock, profile->ns->level);
678290638a5SJohn Johansen 	p = __find_child(&parent->base.profiles, bname);
679290638a5SJohn Johansen 	if (p) {
680290638a5SJohn Johansen 		aa_free_profile(profile);
681290638a5SJohn Johansen 		profile = aa_get_profile(p);
682290638a5SJohn Johansen 	} else {
683d07881d2SJohn Johansen 		__add_profile(&parent->base.profiles, profile);
684290638a5SJohn Johansen 	}
685d07881d2SJohn Johansen 	mutex_unlock(&profile->ns->lock);
686d07881d2SJohn Johansen 
687d07881d2SJohn Johansen 	/* refcount released by caller */
688d07881d2SJohn Johansen out:
689d07881d2SJohn Johansen 	kfree(name);
690d07881d2SJohn Johansen 
691d07881d2SJohn Johansen 	return profile;
692d07881d2SJohn Johansen 
693d07881d2SJohn Johansen fail:
6944633307eSJohn Johansen 	kfree(name);
695d07881d2SJohn Johansen 	aa_free_profile(profile);
696d07881d2SJohn Johansen 	return NULL;
697d07881d2SJohn Johansen }
698d07881d2SJohn Johansen 
699d07881d2SJohn Johansen /**
700c88d4c7bSJohn Johansen  * replacement_allowed - test to see if replacement is allowed
701c88d4c7bSJohn Johansen  * @profile: profile to test if it can be replaced  (MAYBE NULL)
702c88d4c7bSJohn Johansen  * @noreplace: true if replacement shouldn't be allowed but addition is okay
703c88d4c7bSJohn Johansen  * @info: Returns - info about why replacement failed (NOT NULL)
704c88d4c7bSJohn Johansen  *
705c88d4c7bSJohn Johansen  * Returns: %0 if replacement allowed else error code
706c88d4c7bSJohn Johansen  */
replacement_allowed(struct aa_profile * profile,int noreplace,const char ** info)707c88d4c7bSJohn Johansen static int replacement_allowed(struct aa_profile *profile, int noreplace,
708c88d4c7bSJohn Johansen 			       const char **info)
709c88d4c7bSJohn Johansen {
710c88d4c7bSJohn Johansen 	if (profile) {
711637f688dSJohn Johansen 		if (profile->label.flags & FLAG_IMMUTIBLE) {
712058c4f34SColin Ian King 			*info = "cannot replace immutable profile";
713c88d4c7bSJohn Johansen 			return -EPERM;
714c88d4c7bSJohn Johansen 		} else if (noreplace) {
715c88d4c7bSJohn Johansen 			*info = "profile already exists";
716c88d4c7bSJohn Johansen 			return -EEXIST;
717c88d4c7bSJohn Johansen 		}
718c88d4c7bSJohn Johansen 	}
719c88d4c7bSJohn Johansen 	return 0;
720c88d4c7bSJohn Johansen }
721c88d4c7bSJohn Johansen 
722fc1c9fd1SJohn Johansen /* audit callback for net specific fields */
audit_cb(struct audit_buffer * ab,void * va)723fc1c9fd1SJohn Johansen static void audit_cb(struct audit_buffer *ab, void *va)
724fc1c9fd1SJohn Johansen {
725fc1c9fd1SJohn Johansen 	struct common_audit_data *sa = va;
726c57bc80fSJohn Johansen 	struct apparmor_audit_data *ad = aad(sa);
727fc1c9fd1SJohn Johansen 
728c57bc80fSJohn Johansen 	if (ad->iface.ns) {
729fc1c9fd1SJohn Johansen 		audit_log_format(ab, " ns=");
730c57bc80fSJohn Johansen 		audit_log_untrustedstring(ab, ad->iface.ns);
731fc1c9fd1SJohn Johansen 	}
732fc1c9fd1SJohn Johansen }
733fc1c9fd1SJohn Johansen 
734c88d4c7bSJohn Johansen /**
735637f688dSJohn Johansen  * audit_policy - Do auditing of policy changes
73630b3669dSJohn Johansen  * @subj_label: label to check if it can manage policy
737c88d4c7bSJohn Johansen  * @op: policy operation being performed
738637f688dSJohn Johansen  * @ns_name: name of namespace being manipulated
739c88d4c7bSJohn Johansen  * @name: name of profile being manipulated (NOT NULL)
740c88d4c7bSJohn Johansen  * @info: any extra information to be audited (MAYBE NULL)
741c88d4c7bSJohn Johansen  * @error: error code
742c88d4c7bSJohn Johansen  *
743c88d4c7bSJohn Johansen  * Returns: the error to be returned after audit is done
744c88d4c7bSJohn Johansen  */
audit_policy(struct aa_label * subj_label,const char * op,const char * ns_name,const char * name,const char * info,int error)74530b3669dSJohn Johansen static int audit_policy(struct aa_label *subj_label, const char *op,
746637f688dSJohn Johansen 			const char *ns_name, const char *name,
747fc1c9fd1SJohn Johansen 			const char *info, int error)
748c88d4c7bSJohn Johansen {
749c57bc80fSJohn Johansen 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, op);
750c88d4c7bSJohn Johansen 
751c57bc80fSJohn Johansen 	ad.iface.ns = ns_name;
752c57bc80fSJohn Johansen 	ad.name = name;
753c57bc80fSJohn Johansen 	ad.info = info;
754c57bc80fSJohn Johansen 	ad.error = error;
75530b3669dSJohn Johansen 	ad.subj_label = subj_label;
756ef88a7acSJohn Johansen 
757c57bc80fSJohn Johansen 	aa_audit_msg(AUDIT_APPARMOR_STATUS, &ad, audit_cb);
758637f688dSJohn Johansen 
759637f688dSJohn Johansen 	return error;
760c88d4c7bSJohn Johansen }
761c88d4c7bSJohn Johansen 
76231ec99e1SJohn Johansen /* don't call out to other LSMs in the stack for apparmor policy admin
76331ec99e1SJohn Johansen  * permissions
76431ec99e1SJohn Johansen  */
policy_ns_capable(const struct cred * subj_cred,struct aa_label * label,struct user_namespace * userns,int cap)765*690f33e1SJohn Johansen static int policy_ns_capable(const struct cred *subj_cred,
766*690f33e1SJohn Johansen 			     struct aa_label *label,
76731ec99e1SJohn Johansen 			     struct user_namespace *userns, int cap)
76831ec99e1SJohn Johansen {
76931ec99e1SJohn Johansen 	int err;
77031ec99e1SJohn Johansen 
77131ec99e1SJohn Johansen 	/* check for MAC_ADMIN cap in cred */
772*690f33e1SJohn Johansen 	err = cap_capable(subj_cred, userns, cap, CAP_OPT_NONE);
77331ec99e1SJohn Johansen 	if (!err)
774*690f33e1SJohn Johansen 		err = aa_capable(subj_cred, label, cap, CAP_OPT_NONE);
77531ec99e1SJohn Johansen 
77631ec99e1SJohn Johansen 	return err;
77731ec99e1SJohn Johansen }
77831ec99e1SJohn Johansen 
7792bd8dbbfSJohn Johansen /**
78092de220aSJohn Johansen  * aa_policy_view_capable - check if viewing policy in at @ns is allowed
781*690f33e1SJohn Johansen  * @subj_cred: cred of subject
78275ae5a78SGaosheng Cui  * @label: label that is trying to view policy in ns
78375ae5a78SGaosheng Cui  * @ns: namespace being viewed by @label (may be NULL if @label's ns)
78475ae5a78SGaosheng Cui  *
7852bd8dbbfSJohn Johansen  * Returns: true if viewing policy is allowed
7862bd8dbbfSJohn Johansen  *
7872bd8dbbfSJohn Johansen  * If @ns is NULL then the namespace being viewed is assumed to be the
7882bd8dbbfSJohn Johansen  * tasks current namespace.
7892bd8dbbfSJohn Johansen  */
aa_policy_view_capable(const struct cred * subj_cred,struct aa_label * label,struct aa_ns * ns)790*690f33e1SJohn Johansen bool aa_policy_view_capable(const struct cred *subj_cred,
791*690f33e1SJohn Johansen 			     struct aa_label *label, struct aa_ns *ns)
79258acf9d9SJohn Johansen {
793*690f33e1SJohn Johansen 	struct user_namespace *user_ns = subj_cred->user_ns;
79492de220aSJohn Johansen 	struct aa_ns *view_ns = labels_view(label);
7952bd8dbbfSJohn Johansen 	bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) ||
7962bd8dbbfSJohn Johansen 			       in_egroup_p(make_kgid(user_ns, 0));
79758acf9d9SJohn Johansen 	bool response = false;
7982bd8dbbfSJohn Johansen 	if (!ns)
7992bd8dbbfSJohn Johansen 		ns = view_ns;
80058acf9d9SJohn Johansen 
8012bd8dbbfSJohn Johansen 	if (root_in_user_ns && aa_ns_visible(view_ns, ns, true) &&
8022bd8dbbfSJohn Johansen 	    (user_ns == &init_user_ns ||
8032bd8dbbfSJohn Johansen 	     (unprivileged_userns_apparmor_policy != 0 &&
8042bd8dbbfSJohn Johansen 	      user_ns->level == view_ns->level)))
80558acf9d9SJohn Johansen 		response = true;
80658acf9d9SJohn Johansen 
80758acf9d9SJohn Johansen 	return response;
80858acf9d9SJohn Johansen }
80958acf9d9SJohn Johansen 
aa_policy_admin_capable(const struct cred * subj_cred,struct aa_label * label,struct aa_ns * ns)810*690f33e1SJohn Johansen bool aa_policy_admin_capable(const struct cred *subj_cred,
811*690f33e1SJohn Johansen 			     struct aa_label *label, struct aa_ns *ns)
81258acf9d9SJohn Johansen {
813*690f33e1SJohn Johansen 	struct user_namespace *user_ns = subj_cred->user_ns;
814*690f33e1SJohn Johansen 	bool capable = policy_ns_capable(subj_cred, label, user_ns,
815*690f33e1SJohn Johansen 					 CAP_MAC_ADMIN) == 0;
816fd2a8043SJohn Johansen 
817fd2a8043SJohn Johansen 	AA_DEBUG("cap_mac_admin? %d\n", capable);
818fd2a8043SJohn Johansen 	AA_DEBUG("policy locked? %d\n", aa_g_lock_policy);
819fd2a8043SJohn Johansen 
820*690f33e1SJohn Johansen 	return aa_policy_view_capable(subj_cred, label, ns) && capable &&
82192de220aSJohn Johansen 		!aa_g_lock_policy;
82292de220aSJohn Johansen }
82392de220aSJohn Johansen 
aa_current_policy_view_capable(struct aa_ns * ns)82492de220aSJohn Johansen bool aa_current_policy_view_capable(struct aa_ns *ns)
82592de220aSJohn Johansen {
82692de220aSJohn Johansen 	struct aa_label *label;
82792de220aSJohn Johansen 	bool res;
82892de220aSJohn Johansen 
82992de220aSJohn Johansen 	label = __begin_current_label_crit_section();
830*690f33e1SJohn Johansen 	res = aa_policy_view_capable(current_cred(), label, ns);
83192de220aSJohn Johansen 	__end_current_label_crit_section(label);
83292de220aSJohn Johansen 
83392de220aSJohn Johansen 	return res;
83492de220aSJohn Johansen }
83592de220aSJohn Johansen 
aa_current_policy_admin_capable(struct aa_ns * ns)83692de220aSJohn Johansen bool aa_current_policy_admin_capable(struct aa_ns *ns)
83792de220aSJohn Johansen {
83892de220aSJohn Johansen 	struct aa_label *label;
83992de220aSJohn Johansen 	bool res;
84092de220aSJohn Johansen 
84192de220aSJohn Johansen 	label = __begin_current_label_crit_section();
842*690f33e1SJohn Johansen 	res = aa_policy_admin_capable(current_cred(), label, ns);
84392de220aSJohn Johansen 	__end_current_label_crit_section(label);
84492de220aSJohn Johansen 
84592de220aSJohn Johansen 	return res;
84658acf9d9SJohn Johansen }
84758acf9d9SJohn Johansen 
848c88d4c7bSJohn Johansen /**
849c88d4c7bSJohn Johansen  * aa_may_manage_policy - can the current task manage policy
850*690f33e1SJohn Johansen  * @subj_cred; subjects cred
851637f688dSJohn Johansen  * @label: label to check if it can manage policy
85275ae5a78SGaosheng Cui  * @ns: namespace being managed by @label (may be NULL if @label's ns)
85376862af5SRandy Dunlap  * @mask: contains the policy manipulation operation being done
854c88d4c7bSJohn Johansen  *
855078c73c6SJohn Johansen  * Returns: 0 if the task is allowed to manipulate policy else error
856c88d4c7bSJohn Johansen  */
aa_may_manage_policy(const struct cred * subj_cred,struct aa_label * label,struct aa_ns * ns,u32 mask)857*690f33e1SJohn Johansen int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label,
858*690f33e1SJohn Johansen 			 struct aa_ns *ns, u32 mask)
859c88d4c7bSJohn Johansen {
86018e99f19SJohn Johansen 	const char *op;
86118e99f19SJohn Johansen 
86218e99f19SJohn Johansen 	if (mask & AA_MAY_REMOVE_POLICY)
86318e99f19SJohn Johansen 		op = OP_PROF_RM;
86418e99f19SJohn Johansen 	else if (mask & AA_MAY_REPLACE_POLICY)
86518e99f19SJohn Johansen 		op = OP_PROF_REPL;
86618e99f19SJohn Johansen 	else
86718e99f19SJohn Johansen 		op = OP_PROF_LOAD;
86818e99f19SJohn Johansen 
869c88d4c7bSJohn Johansen 	/* check if loading policy is locked out */
870078c73c6SJohn Johansen 	if (aa_g_lock_policy)
871637f688dSJohn Johansen 		return audit_policy(label, op, NULL, NULL, "policy_locked",
87218e99f19SJohn Johansen 				    -EACCES);
873c88d4c7bSJohn Johansen 
874*690f33e1SJohn Johansen 	if (!aa_policy_admin_capable(subj_cred, label, ns))
875637f688dSJohn Johansen 		return audit_policy(label, op, NULL, NULL, "not policy admin",
87618e99f19SJohn Johansen 				    -EACCES);
877c88d4c7bSJohn Johansen 
878078c73c6SJohn Johansen 	/* TODO: add fine grained mediation of policy loads */
879078c73c6SJohn Johansen 	return 0;
880c88d4c7bSJohn Johansen }
881c88d4c7bSJohn Johansen 
__list_lookup_parent(struct list_head * lh,struct aa_profile * profile)882dd51c848SJohn Johansen static struct aa_profile *__list_lookup_parent(struct list_head *lh,
883dd51c848SJohn Johansen 					       struct aa_profile *profile)
884dd51c848SJohn Johansen {
8856e474e30SJohn Johansen 	const char *base = basename(profile->base.hname);
886dd51c848SJohn Johansen 	long len = base - profile->base.hname;
887dd51c848SJohn Johansen 	struct aa_load_ent *ent;
888dd51c848SJohn Johansen 
889dd51c848SJohn Johansen 	/* parent won't have trailing // so remove from len */
890dd51c848SJohn Johansen 	if (len <= 2)
891dd51c848SJohn Johansen 		return NULL;
892dd51c848SJohn Johansen 	len -= 2;
893dd51c848SJohn Johansen 
894dd51c848SJohn Johansen 	list_for_each_entry(ent, lh, list) {
895dd51c848SJohn Johansen 		if (ent->new == profile)
896dd51c848SJohn Johansen 			continue;
897dd51c848SJohn Johansen 		if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
898dd51c848SJohn Johansen 		    0 && ent->new->base.hname[len] == 0)
899dd51c848SJohn Johansen 			return ent->new;
900dd51c848SJohn Johansen 	}
901dd51c848SJohn Johansen 
902dd51c848SJohn Johansen 	return NULL;
903dd51c848SJohn Johansen }
904dd51c848SJohn Johansen 
905dd51c848SJohn Johansen /**
906dd51c848SJohn Johansen  * __replace_profile - replace @old with @new on a list
907dd51c848SJohn Johansen  * @old: profile to be replaced  (NOT NULL)
908dd51c848SJohn Johansen  * @new: profile to replace @old with  (NOT NULL)
909dd51c848SJohn Johansen  *
910dd51c848SJohn Johansen  * Will duplicate and refcount elements that @new inherits from @old
911dd51c848SJohn Johansen  * and will inherit @old children.
912dd51c848SJohn Johansen  *
913dd51c848SJohn Johansen  * refcount @new for list, put @old list refcount
914dd51c848SJohn Johansen  *
915dd51c848SJohn Johansen  * Requires: namespace list lock be held, or list not be shared
916dd51c848SJohn Johansen  */
__replace_profile(struct aa_profile * old,struct aa_profile * new)917637f688dSJohn Johansen static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
918dd51c848SJohn Johansen {
919dd51c848SJohn Johansen 	struct aa_profile *child, *tmp;
920dd51c848SJohn Johansen 
921dd51c848SJohn Johansen 	if (!list_empty(&old->base.profiles)) {
922dd51c848SJohn Johansen 		LIST_HEAD(lh);
92301e2b670SJohn Johansen 		list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
924dd51c848SJohn Johansen 
925dd51c848SJohn Johansen 		list_for_each_entry_safe(child, tmp, &lh, base.list) {
926dd51c848SJohn Johansen 			struct aa_profile *p;
927dd51c848SJohn Johansen 
928dd51c848SJohn Johansen 			list_del_init(&child->base.list);
929dd51c848SJohn Johansen 			p = __find_child(&new->base.profiles, child->base.name);
930dd51c848SJohn Johansen 			if (p) {
931dd51c848SJohn Johansen 				/* @p replaces @child  */
932637f688dSJohn Johansen 				__replace_profile(child, p);
933dd51c848SJohn Johansen 				continue;
934dd51c848SJohn Johansen 			}
935dd51c848SJohn Johansen 
936dd51c848SJohn Johansen 			/* inherit @child and its children */
937dd51c848SJohn Johansen 			/* TODO: update hname of inherited children */
938dd51c848SJohn Johansen 			/* list refcount transferred to @new */
9390d259f04SJohn Johansen 			p = aa_deref_parent(child);
94001e2b670SJohn Johansen 			rcu_assign_pointer(child->parent, aa_get_profile(new));
94101e2b670SJohn Johansen 			list_add_rcu(&child->base.list, &new->base.profiles);
94201e2b670SJohn Johansen 			aa_put_profile(p);
943dd51c848SJohn Johansen 		}
944dd51c848SJohn Johansen 	}
945dd51c848SJohn Johansen 
94601e2b670SJohn Johansen 	if (!rcu_access_pointer(new->parent)) {
9470d259f04SJohn Johansen 		struct aa_profile *parent = aa_deref_parent(old);
94801e2b670SJohn Johansen 		rcu_assign_pointer(new->parent, aa_get_profile(parent));
94901e2b670SJohn Johansen 	}
950637f688dSJohn Johansen 	aa_label_replace(&old->label, &new->label);
951637f688dSJohn Johansen 	/* migrate dents must come after label replacement b/c update */
952c97204baSJohn Johansen 	__aafs_profile_migrate_dents(old, new);
953dd51c848SJohn Johansen 
954dd51c848SJohn Johansen 	if (list_empty(&new->base.list)) {
955dd51c848SJohn Johansen 		/* new is not on a list already */
95601e2b670SJohn Johansen 		list_replace_rcu(&old->base.list, &new->base.list);
957dd51c848SJohn Johansen 		aa_get_profile(new);
958dd51c848SJohn Johansen 		aa_put_profile(old);
959dd51c848SJohn Johansen 	} else
960dd51c848SJohn Johansen 		__list_remove_profile(old);
961dd51c848SJohn Johansen }
962dd51c848SJohn Johansen 
963dd51c848SJohn Johansen /**
964dd51c848SJohn Johansen  * __lookup_replace - lookup replacement information for a profile
96575ae5a78SGaosheng Cui  * @ns: namespace the lookup occurs in
96675ae5a78SGaosheng Cui  * @hname: name of profile to lookup
96775ae5a78SGaosheng Cui  * @noreplace: true if not replacing an existing profile
96875ae5a78SGaosheng Cui  * @p: Returns - profile to be replaced
96975ae5a78SGaosheng Cui  * @info: Returns - info string on why lookup failed
970dd51c848SJohn Johansen  *
971dd51c848SJohn Johansen  * Returns: profile to replace (no ref) on success else ptr error
972dd51c848SJohn Johansen  */
__lookup_replace(struct aa_ns * ns,const char * hname,bool noreplace,struct aa_profile ** p,const char ** info)97398849dffSJohn Johansen static int __lookup_replace(struct aa_ns *ns, const char *hname,
974dd51c848SJohn Johansen 			    bool noreplace, struct aa_profile **p,
975dd51c848SJohn Johansen 			    const char **info)
976dd51c848SJohn Johansen {
977dd51c848SJohn Johansen 	*p = aa_get_profile(__lookup_profile(&ns->base, hname));
978dd51c848SJohn Johansen 	if (*p) {
979dd51c848SJohn Johansen 		int error = replacement_allowed(*p, noreplace, info);
980dd51c848SJohn Johansen 		if (error) {
981dd51c848SJohn Johansen 			*info = "profile can not be replaced";
982dd51c848SJohn Johansen 			return error;
983dd51c848SJohn Johansen 		}
984dd51c848SJohn Johansen 	}
985dd51c848SJohn Johansen 
986dd51c848SJohn Johansen 	return 0;
987dd51c848SJohn Johansen }
988dd51c848SJohn Johansen 
share_name(struct aa_profile * old,struct aa_profile * new)989a1bd627bSJohn Johansen static void share_name(struct aa_profile *old, struct aa_profile *new)
990a1bd627bSJohn Johansen {
991a1bd627bSJohn Johansen 	aa_put_str(new->base.hname);
992a1bd627bSJohn Johansen 	aa_get_str(old->base.hname);
993a1bd627bSJohn Johansen 	new->base.hname = old->base.hname;
994a1bd627bSJohn Johansen 	new->base.name = old->base.name;
995637f688dSJohn Johansen 	new->label.hname = old->label.hname;
996a1bd627bSJohn Johansen }
997a1bd627bSJohn Johansen 
998435222bcSJohn Johansen /* Update to newest version of parent after previous replacements
999435222bcSJohn Johansen  * Returns: unrefcount newest version of parent
1000435222bcSJohn Johansen  */
update_to_newest_parent(struct aa_profile * new)1001435222bcSJohn Johansen static struct aa_profile *update_to_newest_parent(struct aa_profile *new)
1002435222bcSJohn Johansen {
1003435222bcSJohn Johansen 	struct aa_profile *parent, *newest;
1004435222bcSJohn Johansen 
1005435222bcSJohn Johansen 	parent = rcu_dereference_protected(new->parent,
1006435222bcSJohn Johansen 					   mutex_is_locked(&new->ns->lock));
1007435222bcSJohn Johansen 	newest = aa_get_newest_profile(parent);
1008435222bcSJohn Johansen 
1009435222bcSJohn Johansen 	/* parent replaced in this atomic set? */
1010435222bcSJohn Johansen 	if (newest != parent) {
1011435222bcSJohn Johansen 		aa_put_profile(parent);
1012435222bcSJohn Johansen 		rcu_assign_pointer(new->parent, newest);
1013435222bcSJohn Johansen 	} else
1014435222bcSJohn Johansen 		aa_put_profile(newest);
1015435222bcSJohn Johansen 
1016435222bcSJohn Johansen 	return newest;
1017435222bcSJohn Johansen }
1018435222bcSJohn Johansen 
1019c88d4c7bSJohn Johansen /**
1020c88d4c7bSJohn Johansen  * aa_replace_profiles - replace profile(s) on the profile list
102160285eb3SJohn Johansen  * @policy_ns: namespace load is occurring on
102212dd7171SJohn Johansen  * @label: label that is attempting to load/replace policy
102318e99f19SJohn Johansen  * @mask: permission mask
10245ac8c355SJohn Johansen  * @udata: serialized data stream  (NOT NULL)
1025c88d4c7bSJohn Johansen  *
1026c88d4c7bSJohn Johansen  * unpack and replace a profile on the profile list and uses of that profile
1027e1a03f62SJohn Johansen  * by any task creds via invalidating the old version of the profile, which
1028e1a03f62SJohn Johansen  * tasks will notice to update their own cred.  If the profile does not exist
1029e1a03f62SJohn Johansen  * on the profile list it is added.
1030c88d4c7bSJohn Johansen  *
1031c88d4c7bSJohn Johansen  * Returns: size of data consumed else error code on failure.
1032c88d4c7bSJohn Johansen  */
aa_replace_profiles(struct aa_ns * policy_ns,struct aa_label * label,u32 mask,struct aa_loaddata * udata)1033637f688dSJohn Johansen ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label,
103418e99f19SJohn Johansen 			    u32 mask, struct aa_loaddata *udata)
1035c88d4c7bSJohn Johansen {
1036145a0ef2SJohn Johansen 	const char *ns_name = NULL, *info = NULL;
103798849dffSJohn Johansen 	struct aa_ns *ns = NULL;
1038dd51c848SJohn Johansen 	struct aa_load_ent *ent, *tmp;
10395d5182caSJohn Johansen 	struct aa_loaddata *rawdata_ent;
104018e99f19SJohn Johansen 	const char *op;
104104dc715eSJohn Johansen 	ssize_t count, error;
1042dd51c848SJohn Johansen 	LIST_HEAD(lh);
1043c88d4c7bSJohn Johansen 
104418e99f19SJohn Johansen 	op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD;
10455d5182caSJohn Johansen 	aa_get_loaddata(udata);
1046c88d4c7bSJohn Johansen 	/* released below */
10475ac8c355SJohn Johansen 	error = aa_unpack(udata, &lh, &ns_name);
1048dd51c848SJohn Johansen 	if (error)
1049dd51c848SJohn Johansen 		goto out;
1050c88d4c7bSJohn Johansen 
105104dc715eSJohn Johansen 	/* ensure that profiles are all for the same ns
105204dc715eSJohn Johansen 	 * TODO: update locking to remove this constaint. All profiles in
105304dc715eSJohn Johansen 	 *       the load set must succeed as a set or the load will
105404dc715eSJohn Johansen 	 *       fail. Sort ent list and take ns locks in hierarchy order
105504dc715eSJohn Johansen 	 */
105604dc715eSJohn Johansen 	count = 0;
105704dc715eSJohn Johansen 	list_for_each_entry(ent, &lh, list) {
105804dc715eSJohn Johansen 		if (ns_name) {
105904dc715eSJohn Johansen 			if (ent->ns_name &&
106004dc715eSJohn Johansen 			    strcmp(ent->ns_name, ns_name) != 0) {
106104dc715eSJohn Johansen 				info = "policy load has mixed namespaces";
106204dc715eSJohn Johansen 				error = -EACCES;
106304dc715eSJohn Johansen 				goto fail;
1064c88d4c7bSJohn Johansen 			}
106504dc715eSJohn Johansen 		} else if (ent->ns_name) {
106604dc715eSJohn Johansen 			if (count) {
106704dc715eSJohn Johansen 				info = "policy load has mixed namespaces";
106804dc715eSJohn Johansen 				error = -EACCES;
106904dc715eSJohn Johansen 				goto fail;
107004dc715eSJohn Johansen 			}
107104dc715eSJohn Johansen 			ns_name = ent->ns_name;
107204dc715eSJohn Johansen 		} else
107304dc715eSJohn Johansen 			count++;
107404dc715eSJohn Johansen 	}
107504dc715eSJohn Johansen 	if (ns_name) {
1076637f688dSJohn Johansen 		ns = aa_prepare_ns(policy_ns ? policy_ns : labels_ns(label),
107760285eb3SJohn Johansen 				   ns_name);
107804dc715eSJohn Johansen 		if (IS_ERR(ns)) {
1079b9b144bcSJohn Johansen 			op = OP_PROF_LOAD;
108004dc715eSJohn Johansen 			info = "failed to prepare namespace";
108104dc715eSJohn Johansen 			error = PTR_ERR(ns);
108204dc715eSJohn Johansen 			ns = NULL;
1083b9b144bcSJohn Johansen 			ent = NULL;
108404dc715eSJohn Johansen 			goto fail;
108504dc715eSJohn Johansen 		}
108604dc715eSJohn Johansen 	} else
1087637f688dSJohn Johansen 		ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(label));
1088c88d4c7bSJohn Johansen 
1089feb3c766SJohn Johansen 	mutex_lock_nested(&ns->lock, ns->level);
10905d5182caSJohn Johansen 	/* check for duplicate rawdata blobs: space and file dedup */
1091d61c57fdSJohn Johansen 	if (!list_empty(&ns->rawdata_list)) {
10925d5182caSJohn Johansen 		list_for_each_entry(rawdata_ent, &ns->rawdata_list, list) {
10935d5182caSJohn Johansen 			if (aa_rawdata_eq(rawdata_ent, udata)) {
10945d5182caSJohn Johansen 				struct aa_loaddata *tmp;
10955d5182caSJohn Johansen 
10965d5182caSJohn Johansen 				tmp = __aa_get_loaddata(rawdata_ent);
10975d5182caSJohn Johansen 				/* check we didn't fail the race */
10985d5182caSJohn Johansen 				if (tmp) {
10995d5182caSJohn Johansen 					aa_put_loaddata(udata);
11005d5182caSJohn Johansen 					udata = tmp;
11015d5182caSJohn Johansen 					break;
11025d5182caSJohn Johansen 				}
11035d5182caSJohn Johansen 			}
11045d5182caSJohn Johansen 		}
1105d61c57fdSJohn Johansen 	}
1106dd51c848SJohn Johansen 	/* setup parent and ns info */
1107dd51c848SJohn Johansen 	list_for_each_entry(ent, &lh, list) {
1108dd51c848SJohn Johansen 		struct aa_policy *policy;
1109665b1856SJohn Johansen 		struct aa_profile *p;
11105d5182caSJohn Johansen 
1111d61c57fdSJohn Johansen 		if (aa_g_export_binary)
11125ac8c355SJohn Johansen 			ent->new->rawdata = aa_get_loaddata(udata);
111318e99f19SJohn Johansen 		error = __lookup_replace(ns, ent->new->base.hname,
111418e99f19SJohn Johansen 					 !(mask & AA_MAY_REPLACE_POLICY),
1115dd51c848SJohn Johansen 					 &ent->old, &info);
1116dd51c848SJohn Johansen 		if (error)
1117dd51c848SJohn Johansen 			goto fail_lock;
1118dd51c848SJohn Johansen 
1119dd51c848SJohn Johansen 		if (ent->new->rename) {
1120dd51c848SJohn Johansen 			error = __lookup_replace(ns, ent->new->rename,
112118e99f19SJohn Johansen 						!(mask & AA_MAY_REPLACE_POLICY),
112218e99f19SJohn Johansen 						&ent->rename, &info);
1123dd51c848SJohn Johansen 			if (error)
1124dd51c848SJohn Johansen 				goto fail_lock;
1125dd51c848SJohn Johansen 		}
1126dd51c848SJohn Johansen 
1127dd51c848SJohn Johansen 		/* released when @new is freed */
112898849dffSJohn Johansen 		ent->new->ns = aa_get_ns(ns);
1129dd51c848SJohn Johansen 
1130dd51c848SJohn Johansen 		if (ent->old || ent->rename)
1131dd51c848SJohn Johansen 			continue;
1132dd51c848SJohn Johansen 
1133c88d4c7bSJohn Johansen 		/* no ref on policy only use inside lock */
1134665b1856SJohn Johansen 		p = NULL;
1135dd51c848SJohn Johansen 		policy = __lookup_parent(ns, ent->new->base.hname);
1136c88d4c7bSJohn Johansen 		if (!policy) {
1137665b1856SJohn Johansen 			/* first check for parent in the load set */
1138dd51c848SJohn Johansen 			p = __list_lookup_parent(&lh, ent->new);
1139dd51c848SJohn Johansen 			if (!p) {
1140665b1856SJohn Johansen 				/*
1141665b1856SJohn Johansen 				 * fill in missing parent with null
1142665b1856SJohn Johansen 				 * profile that doesn't have
1143665b1856SJohn Johansen 				 * permissions. This allows for
1144665b1856SJohn Johansen 				 * individual profile loading where
1145665b1856SJohn Johansen 				 * the child is loaded before the
1146665b1856SJohn Johansen 				 * parent, and outside of the current
1147665b1856SJohn Johansen 				 * atomic set. This unfortunately can
1148665b1856SJohn Johansen 				 * happen with some userspaces.  The
1149665b1856SJohn Johansen 				 * null profile will be replaced once
1150665b1856SJohn Johansen 				 * the parent is loaded.
1151665b1856SJohn Johansen 				 */
1152665b1856SJohn Johansen 				policy = __create_missing_ancestors(ns,
1153665b1856SJohn Johansen 							ent->new->base.hname,
1154665b1856SJohn Johansen 							GFP_KERNEL);
1155665b1856SJohn Johansen 				if (!policy) {
1156dd51c848SJohn Johansen 					error = -ENOENT;
1157c88d4c7bSJohn Johansen 					info = "parent does not exist";
1158dd51c848SJohn Johansen 					goto fail_lock;
1159dd51c848SJohn Johansen 				}
116001e2b670SJohn Johansen 			}
1161c88d4c7bSJohn Johansen 		}
1162665b1856SJohn Johansen 		if (!p && policy != &ns->base)
1163665b1856SJohn Johansen 			/* released on profile replacement or free_profile */
1164665b1856SJohn Johansen 			p = (struct aa_profile *) policy;
1165665b1856SJohn Johansen 		rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1166665b1856SJohn Johansen 	}
1167c88d4c7bSJohn Johansen 
11680d259f04SJohn Johansen 	/* create new fs entries for introspection if needed */
1169d61c57fdSJohn Johansen 	if (!udata->dents[AAFS_LOADDATA_DIR] && aa_g_export_binary) {
11705d5182caSJohn Johansen 		error = __aa_fs_create_rawdata(ns, udata);
11715d5182caSJohn Johansen 		if (error) {
11725d5182caSJohn Johansen 			info = "failed to create raw_data dir and files";
11735d5182caSJohn Johansen 			ent = NULL;
11745d5182caSJohn Johansen 			goto fail_lock;
11755d5182caSJohn Johansen 		}
11765d5182caSJohn Johansen 	}
11770d259f04SJohn Johansen 	list_for_each_entry(ent, &lh, list) {
1178dca91402SJohn Johansen 		if (!ent->old) {
11790d259f04SJohn Johansen 			struct dentry *parent;
11800d259f04SJohn Johansen 			if (rcu_access_pointer(ent->new->parent)) {
11810d259f04SJohn Johansen 				struct aa_profile *p;
11820d259f04SJohn Johansen 				p = aa_deref_parent(ent->new);
11830d259f04SJohn Johansen 				parent = prof_child_dir(p);
11840d259f04SJohn Johansen 			} else
11850d259f04SJohn Johansen 				parent = ns_subprofs_dir(ent->new->ns);
1186c97204baSJohn Johansen 			error = __aafs_profile_mkdir(ent->new, parent);
11870d259f04SJohn Johansen 		}
11880d259f04SJohn Johansen 
11890d259f04SJohn Johansen 		if (error) {
11900d259f04SJohn Johansen 			info = "failed to create";
11910d259f04SJohn Johansen 			goto fail_lock;
11920d259f04SJohn Johansen 		}
11930d259f04SJohn Johansen 	}
11940d259f04SJohn Johansen 
11950d259f04SJohn Johansen 	/* Done with checks that may fail - do actual replacement */
11965d5182caSJohn Johansen 	__aa_bump_ns_revision(ns);
1197d61c57fdSJohn Johansen 	if (aa_g_export_binary)
11985d5182caSJohn Johansen 		__aa_loaddata_update(udata, ns->revision);
1199dd51c848SJohn Johansen 	list_for_each_entry_safe(ent, tmp, &lh, list) {
1200dd51c848SJohn Johansen 		list_del_init(&ent->list);
1201dd51c848SJohn Johansen 		op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
1202c88d4c7bSJohn Johansen 
1203d61c57fdSJohn Johansen 		if (ent->old && ent->old->rawdata == ent->new->rawdata &&
1204d61c57fdSJohn Johansen 		    ent->new->rawdata) {
12055d5182caSJohn Johansen 			/* dedup actual profile replacement */
1206637f688dSJohn Johansen 			audit_policy(label, op, ns_name, ent->new->base.hname,
12075d5182caSJohn Johansen 				     "same as current profile, skipping",
12085d5182caSJohn Johansen 				     error);
12093ddae987SJohn Johansen 			/* break refcount cycle with proxy. */
12103ddae987SJohn Johansen 			aa_put_proxy(ent->new->label.proxy);
12113ddae987SJohn Johansen 			ent->new->label.proxy = NULL;
12125d5182caSJohn Johansen 			goto skip;
12135d5182caSJohn Johansen 		}
12145d5182caSJohn Johansen 
12155d5182caSJohn Johansen 		/*
12165d5182caSJohn Johansen 		 * TODO: finer dedup based on profile range in data. Load set
12175d5182caSJohn Johansen 		 * can differ but profile may remain unchanged
12185d5182caSJohn Johansen 		 */
1219637f688dSJohn Johansen 		audit_policy(label, op, ns_name, ent->new->base.hname, NULL,
1220637f688dSJohn Johansen 			     error);
1221c88d4c7bSJohn Johansen 
1222dd51c848SJohn Johansen 		if (ent->old) {
1223a1bd627bSJohn Johansen 			share_name(ent->old, ent->new);
1224637f688dSJohn Johansen 			__replace_profile(ent->old, ent->new);
12250d259f04SJohn Johansen 		} else {
1226435222bcSJohn Johansen 			struct list_head *lh;
1227435222bcSJohn Johansen 
1228435222bcSJohn Johansen 			if (rcu_access_pointer(ent->new->parent)) {
1229435222bcSJohn Johansen 				struct aa_profile *parent;
1230435222bcSJohn Johansen 
1231435222bcSJohn Johansen 				parent = update_to_newest_parent(ent->new);
1232435222bcSJohn Johansen 				lh = &parent->base.profiles;
1233435222bcSJohn Johansen 			} else
1234435222bcSJohn Johansen 				lh = &ns->base.profiles;
1235637f688dSJohn Johansen 			__add_profile(lh, ent->new);
12360d259f04SJohn Johansen 		}
12375d5182caSJohn Johansen 	skip:
1238dd51c848SJohn Johansen 		aa_load_ent_free(ent);
1239c88d4c7bSJohn Johansen 	}
1240637f688dSJohn Johansen 	__aa_labelset_update_subtree(ns);
124101e2b670SJohn Johansen 	mutex_unlock(&ns->lock);
1242c88d4c7bSJohn Johansen 
1243c88d4c7bSJohn Johansen out:
124498849dffSJohn Johansen 	aa_put_ns(ns);
12455d5182caSJohn Johansen 	aa_put_loaddata(udata);
1246145a0ef2SJohn Johansen 	kfree(ns_name);
1247dd51c848SJohn Johansen 
1248c88d4c7bSJohn Johansen 	if (error)
1249c88d4c7bSJohn Johansen 		return error;
12505ac8c355SJohn Johansen 	return udata->size;
1251c88d4c7bSJohn Johansen 
1252dd51c848SJohn Johansen fail_lock:
125301e2b670SJohn Johansen 	mutex_unlock(&ns->lock);
1254dd51c848SJohn Johansen 
1255bf15cf0cSJohn Johansen 	/* audit cause of failure */
12565d5182caSJohn Johansen 	op = (ent && !ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
125704dc715eSJohn Johansen fail:
1258637f688dSJohn Johansen 	  audit_policy(label, op, ns_name, ent ? ent->new->base.hname : NULL,
125912dd7171SJohn Johansen 		       info, error);
1260bf15cf0cSJohn Johansen 	/* audit status that rest of profiles in the atomic set failed too */
1261bf15cf0cSJohn Johansen 	info = "valid profile in failed atomic policy load";
1262bf15cf0cSJohn Johansen 	list_for_each_entry(tmp, &lh, list) {
1263bf15cf0cSJohn Johansen 		if (tmp == ent) {
1264bf15cf0cSJohn Johansen 			info = "unchecked profile in failed atomic policy load";
1265bf15cf0cSJohn Johansen 			/* skip entry that caused failure */
1266bf15cf0cSJohn Johansen 			continue;
1267bf15cf0cSJohn Johansen 		}
1268b9b144bcSJohn Johansen 		op = (!tmp->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1269637f688dSJohn Johansen 		audit_policy(label, op, ns_name, tmp->new->base.hname, info,
1270637f688dSJohn Johansen 			     error);
1271bf15cf0cSJohn Johansen 	}
1272dd51c848SJohn Johansen 	list_for_each_entry_safe(ent, tmp, &lh, list) {
1273dd51c848SJohn Johansen 		list_del_init(&ent->list);
1274dd51c848SJohn Johansen 		aa_load_ent_free(ent);
1275dd51c848SJohn Johansen 	}
1276dd51c848SJohn Johansen 
1277c88d4c7bSJohn Johansen 	goto out;
1278c88d4c7bSJohn Johansen }
1279c88d4c7bSJohn Johansen 
1280c88d4c7bSJohn Johansen /**
1281c88d4c7bSJohn Johansen  * aa_remove_profiles - remove profile(s) from the system
128260285eb3SJohn Johansen  * @policy_ns: namespace the remove is being done from
1283637f688dSJohn Johansen  * @subj: label attempting to remove policy
1284c88d4c7bSJohn Johansen  * @fqname: name of the profile or namespace to remove  (NOT NULL)
1285c88d4c7bSJohn Johansen  * @size: size of the name
1286c88d4c7bSJohn Johansen  *
1287c88d4c7bSJohn Johansen  * Remove a profile or sub namespace from the current namespace, so that
1288c88d4c7bSJohn Johansen  * they can not be found anymore and mark them as replaced by unconfined
1289c88d4c7bSJohn Johansen  *
12903107e8cbSZygmunt Krynicki  * NOTE: removing confinement does not restore rlimits to preconfinement values
1291c88d4c7bSJohn Johansen  *
1292c88d4c7bSJohn Johansen  * Returns: size of data consume else error code if fails
1293c88d4c7bSJohn Johansen  */
aa_remove_profiles(struct aa_ns * policy_ns,struct aa_label * subj,char * fqname,size_t size)1294637f688dSJohn Johansen ssize_t aa_remove_profiles(struct aa_ns *policy_ns, struct aa_label *subj,
129512dd7171SJohn Johansen 			   char *fqname, size_t size)
1296c88d4c7bSJohn Johansen {
129760285eb3SJohn Johansen 	struct aa_ns *ns = NULL;
1298c88d4c7bSJohn Johansen 	struct aa_profile *profile = NULL;
1299c88d4c7bSJohn Johansen 	const char *name = fqname, *info = NULL;
13003664268fSJohn Johansen 	const char *ns_name = NULL;
1301c88d4c7bSJohn Johansen 	ssize_t error = 0;
1302c88d4c7bSJohn Johansen 
1303c88d4c7bSJohn Johansen 	if (*fqname == 0) {
1304c88d4c7bSJohn Johansen 		info = "no profile specified";
1305c88d4c7bSJohn Johansen 		error = -ENOENT;
1306c88d4c7bSJohn Johansen 		goto fail;
1307c88d4c7bSJohn Johansen 	}
1308c88d4c7bSJohn Johansen 
1309c88d4c7bSJohn Johansen 	if (fqname[0] == ':') {
13103664268fSJohn Johansen 		size_t ns_len;
13113664268fSJohn Johansen 
13123664268fSJohn Johansen 		name = aa_splitn_fqname(fqname, size, &ns_name, &ns_len);
1313c88d4c7bSJohn Johansen 		/* released below */
1314637f688dSJohn Johansen 		ns = aa_lookupn_ns(policy_ns ? policy_ns : labels_ns(subj),
1315637f688dSJohn Johansen 				   ns_name, ns_len);
1316c88d4c7bSJohn Johansen 		if (!ns) {
1317c88d4c7bSJohn Johansen 			info = "namespace does not exist";
1318c88d4c7bSJohn Johansen 			error = -ENOENT;
1319c88d4c7bSJohn Johansen 			goto fail;
1320c88d4c7bSJohn Johansen 		}
1321c88d4c7bSJohn Johansen 	} else
1322c88d4c7bSJohn Johansen 		/* released below */
1323637f688dSJohn Johansen 		ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(subj));
1324c88d4c7bSJohn Johansen 
1325c88d4c7bSJohn Johansen 	if (!name) {
1326c88d4c7bSJohn Johansen 		/* remove namespace - can only happen if fqname[0] == ':' */
13279c4557efSJohn Johansen 		mutex_lock_nested(&ns->parent->lock, ns->parent->level);
13285d5182caSJohn Johansen 		__aa_bump_ns_revision(ns);
13298c62ed27SJohn Johansen 		__aa_remove_ns(ns);
133001e2b670SJohn Johansen 		mutex_unlock(&ns->parent->lock);
1331c88d4c7bSJohn Johansen 	} else {
1332c88d4c7bSJohn Johansen 		/* remove profile */
1333feb3c766SJohn Johansen 		mutex_lock_nested(&ns->lock, ns->level);
1334c88d4c7bSJohn Johansen 		profile = aa_get_profile(__lookup_profile(&ns->base, name));
1335c88d4c7bSJohn Johansen 		if (!profile) {
1336c88d4c7bSJohn Johansen 			error = -ENOENT;
1337c88d4c7bSJohn Johansen 			info = "profile does not exist";
1338c88d4c7bSJohn Johansen 			goto fail_ns_lock;
1339c88d4c7bSJohn Johansen 		}
1340c88d4c7bSJohn Johansen 		name = profile->base.hname;
13418c62ed27SJohn Johansen 		__aa_bump_ns_revision(ns);
1342c88d4c7bSJohn Johansen 		__remove_profile(profile);
1343637f688dSJohn Johansen 		__aa_labelset_update_subtree(ns);
134401e2b670SJohn Johansen 		mutex_unlock(&ns->lock);
1345999b4f0aSJohn Johansen 	}
1346c88d4c7bSJohn Johansen 
1347c88d4c7bSJohn Johansen 	/* don't fail removal if audit fails */
1348ef88a7acSJohn Johansen 	(void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
134912dd7171SJohn Johansen 			    error);
135098849dffSJohn Johansen 	aa_put_ns(ns);
1351c88d4c7bSJohn Johansen 	aa_put_profile(profile);
1352c88d4c7bSJohn Johansen 	return size;
1353c88d4c7bSJohn Johansen 
1354c88d4c7bSJohn Johansen fail_ns_lock:
135501e2b670SJohn Johansen 	mutex_unlock(&ns->lock);
135698849dffSJohn Johansen 	aa_put_ns(ns);
1357c88d4c7bSJohn Johansen 
1358c88d4c7bSJohn Johansen fail:
1359ef88a7acSJohn Johansen 	(void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
136012dd7171SJohn Johansen 			    error);
1361c88d4c7bSJohn Johansen 	return error;
1362c88d4c7bSJohn Johansen }
1363