xref: /linux/security/apparmor/policy.c (revision 3d28e2397af7a89ac3de33c686ed404cda59b5d5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * AppArmor security module
4  *
5  * This file contains AppArmor policy manipulation functions
6  *
7  * Copyright (C) 1998-2008 Novell/SUSE
8  * Copyright 2009-2010 Canonical Ltd.
9  *
10  * AppArmor policy is based around profiles, which contain the rules a
11  * task is confined by.  Every task in the system has a profile attached
12  * to it determined either by matching "unconfined" tasks against the
13  * visible set of profiles or by following a profiles attachment rules.
14  *
15  * Each profile exists in a profile namespace which is a container of
16  * visible profiles.  Each namespace contains a special "unconfined" profile,
17  * which doesn't enforce any confinement on a task beyond DAC.
18  *
19  * Namespace and profile names can be written together in either
20  * of two syntaxes.
21  *	:namespace:profile - used by kernel interfaces for easy detection
22  *	namespace://profile - used by policy
23  *
24  * Profile names can not start with : or @ or ^ and may not contain \0
25  *
26  * Reserved profile names
27  *	unconfined - special automatically generated unconfined profile
28  *	inherit - special name to indicate profile inheritance
29  *	null-XXXX-YYYY - special automatically generated learning profiles
30  *
31  * Namespace names may not start with / or @ and may not contain \0 or :
32  * Reserved namespace names
33  *	user-XXXX - user defined profiles
34  *
35  * a // in a profile or namespace name indicates a hierarchical name with the
36  * name before the // being the parent and the name after the child.
37  *
38  * Profile and namespace hierarchies serve two different but similar purposes.
39  * The namespace contains the set of visible profiles that are considered
40  * for attachment.  The hierarchy of namespaces allows for virtualizing
41  * the namespace so that for example a chroot can have its own set of profiles
42  * which may define some local user namespaces.
43  * The profile hierarchy severs two distinct purposes,
44  * -  it allows for sub profiles or hats, which allows an application to run
45  *    subprograms under its own profile with different restriction than it
46  *    self, and not have it use the system profile.
47  *    eg. if a mail program starts an editor, the policy might make the
48  *        restrictions tighter on the editor tighter than the mail program,
49  *        and definitely different than general editor restrictions
50  * - it allows for binary hierarchy of profiles, so that execution history
51  *   is preserved.  This feature isn't exploited by AppArmor reference policy
52  *   but is allowed.  NOTE: this is currently suboptimal because profile
53  *   aliasing is not currently implemented so that a profile for each
54  *   level must be defined.
55  *   eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started
56  *       from /bin/bash
57  *
58  *   A profile or namespace name that can contain one or more // separators
59  *   is referred to as an hname (hierarchical).
60  *   eg.  /bin/bash//bin/ls
61  *
62  *   An fqname is a name that may contain both namespace and profile hnames.
63  *   eg. :ns:/bin/bash//bin/ls
64  *
65  * NOTES:
66  *   - locking of profile lists is currently fairly coarse.  All profile
67  *     lists within a namespace use the namespace lock.
68  * FIXME: move profile lists to using rcu_lists
69  */
70 
71 #include <linux/slab.h>
72 #include <linux/spinlock.h>
73 #include <linux/string.h>
74 #include <linux/cred.h>
75 #include <linux/rculist.h>
76 #include <linux/user_namespace.h>
77 
78 #include "include/apparmor.h"
79 #include "include/capability.h"
80 #include "include/cred.h"
81 #include "include/file.h"
82 #include "include/ipc.h"
83 #include "include/match.h"
84 #include "include/path.h"
85 #include "include/policy.h"
86 #include "include/policy_ns.h"
87 #include "include/policy_unpack.h"
88 #include "include/resource.h"
89 
90 int unprivileged_userns_apparmor_policy = 1;
91 int aa_unprivileged_unconfined_restricted;
92 
93 const char *const aa_profile_mode_names[] = {
94 	"enforce",
95 	"complain",
96 	"kill",
97 	"unconfined",
98 	"user",
99 };
100 
101 void aa_destroy_tags(struct aa_tags_struct *tags)
102 {
103 	kfree_sensitive(tags->hdrs.table);
104 	kfree_sensitive(tags->sets.table);
105 	aa_destroy_str_table(&tags->strs);
106 	memset(tags, 0, sizeof(*tags));
107 }
108 
109 static void aa_free_pdb(struct aa_policydb *pdb)
110 {
111 	if (pdb) {
112 		aa_put_dfa(pdb->dfa);
113 		kvfree(pdb->perms);
114 		aa_destroy_str_table(&pdb->trans);
115 		aa_destroy_tags(&pdb->tags);
116 		kfree(pdb);
117 	}
118 }
119 
120 /**
121  * aa_pdb_free_kref - free aa_policydb by kref (called by aa_put_pdb)
122  * @kref: kref callback for freeing of a dfa  (NOT NULL)
123  */
124 void aa_pdb_free_kref(struct kref *kref)
125 {
126 	struct aa_policydb *pdb = container_of(kref, struct aa_policydb, count);
127 
128 	aa_free_pdb(pdb);
129 }
130 
131 
132 struct aa_policydb *aa_alloc_pdb(gfp_t gfp)
133 {
134 	struct aa_policydb *pdb = kzalloc(sizeof(struct aa_policydb), gfp);
135 
136 	if (!pdb)
137 		return NULL;
138 
139 	kref_init(&pdb->count);
140 
141 	return pdb;
142 }
143 
144 
145 /**
146  * __add_profile - add a profiles to list and label tree
147  * @list: list to add it to  (NOT NULL)
148  * @profile: the profile to add  (NOT NULL)
149  *
150  * refcount @profile, should be put by __list_remove_profile
151  *
152  * Requires: namespace lock be held, or list not be shared
153  */
154 static void __add_profile(struct list_head *list, struct aa_profile *profile)
155 {
156 	struct aa_label *l;
157 
158 	AA_BUG(!list);
159 	AA_BUG(!profile);
160 	AA_BUG(!profile->ns);
161 	AA_BUG(!mutex_is_locked(&profile->ns->lock));
162 
163 	list_add_rcu(&profile->base.list, list);
164 	/* get list reference */
165 	aa_get_profile(profile);
166 	l = aa_label_insert(&profile->ns->labels, &profile->label);
167 	AA_BUG(l != &profile->label);
168 	aa_put_label(l);
169 }
170 
171 /**
172  * __list_remove_profile - remove a profile from the list it is on
173  * @profile: the profile to remove  (NOT NULL)
174  *
175  * remove a profile from the list, warning generally removal should
176  * be done with __replace_profile as most profile removals are
177  * replacements to the unconfined profile.
178  *
179  * put @profile list refcount
180  *
181  * Requires: namespace lock be held, or list not have been live
182  */
183 static void __list_remove_profile(struct aa_profile *profile)
184 {
185 	AA_BUG(!profile);
186 	AA_BUG(!profile->ns);
187 	AA_BUG(!mutex_is_locked(&profile->ns->lock));
188 
189 	list_del_rcu(&profile->base.list);
190 	aa_put_profile(profile);
191 }
192 
193 /**
194  * __remove_profile - remove old profile, and children
195  * @profile: profile to be replaced  (NOT NULL)
196  *
197  * Requires: namespace list lock be held, or list not be shared
198  */
199 static void __remove_profile(struct aa_profile *profile)
200 {
201 	AA_BUG(!profile);
202 	AA_BUG(!profile->ns);
203 	AA_BUG(!mutex_is_locked(&profile->ns->lock));
204 
205 	/* release any children lists first */
206 	__aa_profile_list_release(&profile->base.profiles);
207 	/* released by free_profile */
208 	aa_label_remove(&profile->label);
209 	__aafs_profile_rmdir(profile);
210 	__list_remove_profile(profile);
211 }
212 
213 /**
214  * __aa_profile_list_release - remove all profiles on the list and put refs
215  * @head: list of profiles  (NOT NULL)
216  *
217  * Requires: namespace lock be held
218  */
219 void __aa_profile_list_release(struct list_head *head)
220 {
221 	struct aa_profile *profile, *tmp;
222 	list_for_each_entry_safe(profile, tmp, head, base.list)
223 		__remove_profile(profile);
224 }
225 
226 /**
227  * aa_free_data - free a data blob
228  * @ptr: data to free
229  * @arg: unused
230  */
231 static void aa_free_data(void *ptr, void *arg)
232 {
233 	struct aa_data *data = ptr;
234 
235 	kvfree_sensitive(data->data, data->size);
236 	kfree_sensitive(data->key);
237 	kfree_sensitive(data);
238 }
239 
240 static void free_attachment(struct aa_attachment *attach)
241 {
242 	int i;
243 
244 	for (i = 0; i < attach->xattr_count; i++)
245 		kfree_sensitive(attach->xattrs[i]);
246 	kfree_sensitive(attach->xattrs);
247 	aa_put_pdb(attach->xmatch);
248 }
249 
250 static void free_ruleset(struct aa_ruleset *rules)
251 {
252 	int i;
253 
254 	if (!rules)
255 	  return;
256 
257 	aa_put_pdb(rules->file);
258 	aa_put_pdb(rules->policy);
259 	aa_free_cap_rules(&rules->caps);
260 	aa_free_rlimit_rules(&rules->rlimits);
261 
262 	for (i = 0; i < rules->secmark_count; i++)
263 		kfree_sensitive(rules->secmark[i].label);
264 	kfree_sensitive(rules->secmark);
265 	kfree_sensitive(rules);
266 }
267 
268 struct aa_ruleset *aa_alloc_ruleset(gfp_t gfp)
269 {
270 	struct aa_ruleset *rules;
271 
272 	rules = kzalloc(sizeof(*rules), gfp);
273 
274 	return rules;
275 }
276 
277 /**
278  * aa_free_profile - free a profile
279  * @profile: the profile to free  (MAYBE NULL)
280  *
281  * Free a profile, its hats and null_profile. All references to the profile,
282  * its hats and null_profile must have been put.
283  *
284  * If the profile was referenced from a task context, free_profile() will
285  * be called from an rcu callback routine, so we must not sleep here.
286  */
287 void aa_free_profile(struct aa_profile *profile)
288 {
289 	struct rhashtable *rht;
290 
291 	AA_DEBUG(DEBUG_POLICY, "%s(%p)\n", __func__, profile);
292 
293 	if (!profile)
294 		return;
295 
296 	/* free children profiles */
297 	aa_policy_destroy(&profile->base);
298 	aa_put_profile(rcu_access_pointer(profile->parent));
299 
300 	aa_put_ns(profile->ns);
301 	kfree_sensitive(profile->rename);
302 	kfree_sensitive(profile->disconnected);
303 
304 	free_attachment(&profile->attach);
305 
306 	/*
307 	 * at this point there are no tasks that can have a reference
308 	 * to rules
309 	 */
310 	for (int i = 0; i < profile->n_rules; i++)
311 		free_ruleset(profile->label.rules[i]);
312 
313 	kfree_sensitive(profile->dirname);
314 
315 	if (profile->data) {
316 		rht = profile->data;
317 		profile->data = NULL;
318 		rhashtable_free_and_destroy(rht, aa_free_data, NULL);
319 		kfree_sensitive(rht);
320 	}
321 
322 	kfree_sensitive(profile->hash);
323 	aa_put_loaddata(profile->rawdata);
324 	aa_label_destroy(&profile->label);
325 
326 	kfree_sensitive(profile);
327 }
328 
329 /**
330  * aa_alloc_profile - allocate, initialize and return a new profile
331  * @hname: name of the profile  (NOT NULL)
332  * @proxy: proxy to use OR null if to allocate a new one
333  * @gfp: allocation type
334  *
335  * Returns: refcount profile or NULL on failure
336  */
337 struct aa_profile *aa_alloc_profile(const char *hname, struct aa_proxy *proxy,
338 				    gfp_t gfp)
339 {
340 	struct aa_profile *profile;
341 
342 	/* freed by free_profile - usually through aa_put_profile
343 	 * this adds space for a single ruleset in the rules section of the
344 	 * label
345 	 */
346 	profile = kzalloc(struct_size(profile, label.rules, 1), gfp);
347 	if (!profile)
348 		return NULL;
349 
350 	if (!aa_policy_init(&profile->base, NULL, hname, gfp))
351 		goto fail;
352 	if (!aa_label_init(&profile->label, 1, gfp))
353 		goto fail;
354 
355 	/* allocate the first ruleset, but leave it empty */
356 	profile->label.rules[0] = aa_alloc_ruleset(gfp);
357 	if (!profile->label.rules[0])
358 		goto fail;
359 	profile->n_rules = 1;
360 
361 	/* update being set needed by fs interface */
362 	if (!proxy) {
363 		proxy = aa_alloc_proxy(&profile->label, gfp);
364 		if (!proxy)
365 			goto fail;
366 	} else
367 		aa_get_proxy(proxy);
368 	profile->label.proxy = proxy;
369 
370 	profile->label.hname = profile->base.hname;
371 	profile->label.flags |= FLAG_PROFILE;
372 	profile->label.vec[0] = profile;
373 
374 	profile->signal = SIGKILL;
375 	/* refcount released by caller */
376 	return profile;
377 
378 fail:
379 	aa_free_profile(profile);
380 
381 	return NULL;
382 }
383 
384 static inline bool ANY_RULE_MEDIATES(struct aa_profile *profile,
385 				     unsigned char class)
386 {
387 	int i;
388 
389 	for (i = 0; i < profile->n_rules; i++) {
390 		if (RULE_MEDIATES(profile->label.rules[i], class))
391 			return true;
392 	}
393 	return false;
394 }
395 
396 /* set of rules that are mediated by unconfined */
397 static int unconfined_mediates[] = { AA_CLASS_NS, AA_CLASS_IO_URING, 0 };
398 
399 /* must be called after profile rulesets and start information is setup */
400 void aa_compute_profile_mediates(struct aa_profile *profile)
401 {
402 	int c;
403 
404 	if (profile_unconfined(profile)) {
405 		int *pos;
406 
407 		for (pos = unconfined_mediates; *pos; pos++) {
408 			if (ANY_RULE_MEDIATES(profile, *pos))
409 				profile->label.mediates |= ((u64) 1) << AA_CLASS_NS;
410 		}
411 		return;
412 	}
413 	for (c = 0; c <= AA_CLASS_LAST; c++) {
414 		if (ANY_RULE_MEDIATES(profile, c))
415 			profile->label.mediates |= ((u64) 1) << c;
416 	}
417 }
418 
419 /* TODO: profile accounting - setup in remove */
420 
421 /**
422  * __strn_find_child - find a profile on @head list using substring of @name
423  * @head: list to search  (NOT NULL)
424  * @name: name of profile (NOT NULL)
425  * @len: length of @name substring to match
426  *
427  * Requires: rcu_read_lock be held
428  *
429  * Returns: unrefcounted profile ptr, or NULL if not found
430  */
431 static struct aa_profile *__strn_find_child(struct list_head *head,
432 					    const char *name, int len)
433 {
434 	return (struct aa_profile *)__policy_strn_find(head, name, len);
435 }
436 
437 /**
438  * __find_child - find a profile on @head list with a name matching @name
439  * @head: list to search  (NOT NULL)
440  * @name: name of profile (NOT NULL)
441  *
442  * Requires: rcu_read_lock be held
443  *
444  * Returns: unrefcounted profile ptr, or NULL if not found
445  */
446 static struct aa_profile *__find_child(struct list_head *head, const char *name)
447 {
448 	return __strn_find_child(head, name, strlen(name));
449 }
450 
451 /**
452  * aa_find_child - find a profile by @name in @parent
453  * @parent: profile to search  (NOT NULL)
454  * @name: profile name to search for  (NOT NULL)
455  *
456  * Returns: a refcounted profile or NULL if not found
457  */
458 struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
459 {
460 	struct aa_profile *profile;
461 
462 	rcu_read_lock();
463 	do {
464 		profile = __find_child(&parent->base.profiles, name);
465 	} while (profile && !aa_get_profile_not0(profile));
466 	rcu_read_unlock();
467 
468 	/* refcount released by caller */
469 	return profile;
470 }
471 
472 /**
473  * __lookup_parent - lookup the parent of a profile of name @hname
474  * @ns: namespace to lookup profile in  (NOT NULL)
475  * @hname: hierarchical profile name to find parent of  (NOT NULL)
476  *
477  * Lookups up the parent of a fully qualified profile name, the profile
478  * that matches hname does not need to exist, in general this
479  * is used to load a new profile.
480  *
481  * Requires: rcu_read_lock be held
482  *
483  * Returns: unrefcounted policy or NULL if not found
484  */
485 static struct aa_policy *__lookup_parent(struct aa_ns *ns,
486 					 const char *hname)
487 {
488 	struct aa_policy *policy;
489 	struct aa_profile *profile = NULL;
490 	char *split;
491 
492 	policy = &ns->base;
493 
494 	for (split = strstr(hname, "//"); split;) {
495 		profile = __strn_find_child(&policy->profiles, hname,
496 					    split - hname);
497 		if (!profile)
498 			return NULL;
499 		policy = &profile->base;
500 		hname = split + 2;
501 		split = strstr(hname, "//");
502 	}
503 	if (!profile)
504 		return &ns->base;
505 	return &profile->base;
506 }
507 
508 /**
509  * __create_missing_ancestors - create place holders for missing ancestors
510  * @ns: namespace to lookup profile in (NOT NULL)
511  * @hname: hierarchical profile name to find parent of (NOT NULL)
512  * @gfp: type of allocation.
513  *
514  * Requires: ns mutex lock held
515  *
516  * Return: unrefcounted parent policy on success or %NULL if error creating
517  *          place holder profiles.
518  */
519 static struct aa_policy *__create_missing_ancestors(struct aa_ns *ns,
520 						    const char *hname,
521 						    gfp_t gfp)
522 {
523 	struct aa_policy *policy;
524 	struct aa_profile *parent, *profile = NULL;
525 	char *split;
526 
527 	AA_BUG(!ns);
528 	AA_BUG(!hname);
529 
530 	policy = &ns->base;
531 
532 	for (split = strstr(hname, "//"); split;) {
533 		parent = profile;
534 		profile = __strn_find_child(&policy->profiles, hname,
535 					    split - hname);
536 		if (!profile) {
537 			const char *name = kstrndup(hname, split - hname,
538 						    gfp);
539 			if (!name)
540 				return NULL;
541 			profile = aa_alloc_null(parent, name, gfp);
542 			kfree(name);
543 			if (!profile)
544 				return NULL;
545 			if (!parent)
546 				profile->ns = aa_get_ns(ns);
547 		}
548 		policy = &profile->base;
549 		hname = split + 2;
550 		split = strstr(hname, "//");
551 	}
552 	if (!profile)
553 		return &ns->base;
554 	return &profile->base;
555 }
556 
557 /**
558  * __lookupn_profile - lookup the profile matching @hname
559  * @base: base list to start looking up profile name from  (NOT NULL)
560  * @hname: hierarchical profile name  (NOT NULL)
561  * @n: length of @hname
562  *
563  * Requires: rcu_read_lock be held
564  *
565  * Returns: unrefcounted profile pointer or NULL if not found
566  *
567  * Do a relative name lookup, recursing through profile tree.
568  */
569 static struct aa_profile *__lookupn_profile(struct aa_policy *base,
570 					    const char *hname, size_t n)
571 {
572 	struct aa_profile *profile = NULL;
573 	const char *split;
574 
575 	for (split = strnstr(hname, "//", n); split;
576 	     split = strnstr(hname, "//", n)) {
577 		profile = __strn_find_child(&base->profiles, hname,
578 					    split - hname);
579 		if (!profile)
580 			return NULL;
581 
582 		base = &profile->base;
583 		n -= split + 2 - hname;
584 		hname = split + 2;
585 	}
586 
587 	if (n)
588 		return __strn_find_child(&base->profiles, hname, n);
589 	return NULL;
590 }
591 
592 static struct aa_profile *__lookup_profile(struct aa_policy *base,
593 					   const char *hname)
594 {
595 	return __lookupn_profile(base, hname, strlen(hname));
596 }
597 
598 /**
599  * aa_lookupn_profile - find a profile by its full or partial name
600  * @ns: the namespace to start from (NOT NULL)
601  * @hname: name to do lookup on.  Does not contain namespace prefix (NOT NULL)
602  * @n: size of @hname
603  *
604  * Returns: refcounted profile or NULL if not found
605  */
606 struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
607 				      size_t n)
608 {
609 	struct aa_profile *profile;
610 
611 	rcu_read_lock();
612 	do {
613 		profile = __lookupn_profile(&ns->base, hname, n);
614 	} while (profile && !aa_get_profile_not0(profile));
615 	rcu_read_unlock();
616 
617 	/* the unconfined profile is not in the regular profile list */
618 	if (!profile && strncmp(hname, "unconfined", n) == 0)
619 		profile = aa_get_newest_profile(ns->unconfined);
620 
621 	/* refcount released by caller */
622 	return profile;
623 }
624 
625 struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
626 					const char *fqname, size_t n)
627 {
628 	struct aa_profile *profile;
629 	struct aa_ns *ns;
630 	const char *name, *ns_name;
631 	size_t ns_len;
632 
633 	name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
634 	if (ns_name) {
635 		ns = aa_lookupn_ns(labels_ns(base), ns_name, ns_len);
636 		if (!ns)
637 			return NULL;
638 	} else
639 		ns = aa_get_ns(labels_ns(base));
640 
641 	if (name)
642 		profile = aa_lookupn_profile(ns, name, n - (name - fqname));
643 	else if (ns)
644 		/* default profile for ns, currently unconfined */
645 		profile = aa_get_newest_profile(ns->unconfined);
646 	else
647 		profile = NULL;
648 	aa_put_ns(ns);
649 
650 	return profile;
651 }
652 
653 
654 struct aa_profile *aa_alloc_null(struct aa_profile *parent, const char *name,
655 				 gfp_t gfp)
656 {
657 	struct aa_profile *profile;
658 	struct aa_ruleset *rules;
659 
660 	profile = aa_alloc_profile(name, NULL, gfp);
661 	if (!profile)
662 		return NULL;
663 
664 	/* TODO: ideally we should inherit abi from parent */
665 	profile->label.flags |= FLAG_NULL;
666 	profile->attach.xmatch = aa_get_pdb(nullpdb);
667 	rules = profile->label.rules[0];
668 	rules->file = aa_get_pdb(nullpdb);
669 	rules->policy = aa_get_pdb(nullpdb);
670 	aa_compute_profile_mediates(profile);
671 
672 	if (parent) {
673 		profile->path_flags = parent->path_flags;
674 		/* override/inherit what is mediated from parent */
675 		profile->label.mediates = parent->label.mediates;
676 		/* released on free_profile */
677 		rcu_assign_pointer(profile->parent, aa_get_profile(parent));
678 		profile->ns = aa_get_ns(parent->ns);
679 	}
680 
681 	return profile;
682 }
683 
684 /**
685  * aa_new_learning_profile - create or find a null-X learning profile
686  * @parent: profile that caused this profile to be created (NOT NULL)
687  * @hat: true if the null- learning profile is a hat
688  * @base: name to base the null profile off of
689  * @gfp: type of allocation
690  *
691  * Find/Create a null- complain mode profile used in learning mode.  The
692  * name of the profile is unique and follows the format of parent//null-XXX.
693  * where XXX is based on the @name or if that fails or is not supplied
694  * a unique number
695  *
696  * null profiles are added to the profile list but the list does not
697  * hold a count on them so that they are automatically released when
698  * not in use.
699  *
700  * Returns: new refcounted profile else NULL on failure
701  */
702 struct aa_profile *aa_new_learning_profile(struct aa_profile *parent, bool hat,
703 					   const char *base, gfp_t gfp)
704 {
705 	struct aa_profile *p, *profile;
706 	const char *bname;
707 	char *name = NULL;
708 	size_t name_sz;
709 
710 	AA_BUG(!parent);
711 
712 	if (base) {
713 		name_sz = strlen(parent->base.hname) + 8 + strlen(base);
714 		name = kmalloc(name_sz, gfp);
715 		if (name) {
716 			snprintf(name, name_sz, "%s//null-%s",
717 				 parent->base.hname, base);
718 			goto name;
719 		}
720 		/* fall through to try shorter uniq */
721 	}
722 
723 	name_sz = strlen(parent->base.hname) + 2 + 7 + 8;
724 	name = kmalloc(name_sz, gfp);
725 	if (!name)
726 		return NULL;
727 	snprintf(name, name_sz, "%s//null-%x", parent->base.hname,
728 		 atomic_inc_return(&parent->ns->uniq_null));
729 
730 name:
731 	/* lookup to see if this is a dup creation */
732 	bname = basename(name);
733 	profile = aa_find_child(parent, bname);
734 	if (profile)
735 		goto out;
736 
737 	profile = aa_alloc_null(parent, name, gfp);
738 	if (!profile)
739 		goto fail;
740 	profile->mode = APPARMOR_COMPLAIN;
741 	if (hat)
742 		profile->label.flags |= FLAG_HAT;
743 
744 	mutex_lock_nested(&profile->ns->lock, profile->ns->level);
745 	p = __find_child(&parent->base.profiles, bname);
746 	if (p) {
747 		aa_free_profile(profile);
748 		profile = aa_get_profile(p);
749 	} else {
750 		__add_profile(&parent->base.profiles, profile);
751 	}
752 	mutex_unlock(&profile->ns->lock);
753 
754 	/* refcount released by caller */
755 out:
756 	kfree(name);
757 
758 	return profile;
759 
760 fail:
761 	kfree(name);
762 	aa_free_profile(profile);
763 	return NULL;
764 }
765 
766 /**
767  * replacement_allowed - test to see if replacement is allowed
768  * @profile: profile to test if it can be replaced  (MAYBE NULL)
769  * @noreplace: true if replacement shouldn't be allowed but addition is okay
770  * @info: Returns - info about why replacement failed (NOT NULL)
771  *
772  * Returns: %0 if replacement allowed else error code
773  */
774 static int replacement_allowed(struct aa_profile *profile, int noreplace,
775 			       const char **info)
776 {
777 	if (profile) {
778 		if (profile->label.flags & FLAG_IMMUTIBLE) {
779 			*info = "cannot replace immutable profile";
780 			return -EPERM;
781 		} else if (noreplace) {
782 			*info = "profile already exists";
783 			return -EEXIST;
784 		}
785 	}
786 	return 0;
787 }
788 
789 /* audit callback for net specific fields */
790 static void audit_cb(struct audit_buffer *ab, void *va)
791 {
792 	struct common_audit_data *sa = va;
793 	struct apparmor_audit_data *ad = aad(sa);
794 
795 	if (ad->iface.ns) {
796 		audit_log_format(ab, " ns=");
797 		audit_log_untrustedstring(ab, ad->iface.ns);
798 	}
799 }
800 
801 /**
802  * audit_policy - Do auditing of policy changes
803  * @subj_label: label to check if it can manage policy
804  * @op: policy operation being performed
805  * @ns_name: name of namespace being manipulated
806  * @name: name of profile being manipulated (NOT NULL)
807  * @info: any extra information to be audited (MAYBE NULL)
808  * @error: error code
809  *
810  * Returns: the error to be returned after audit is done
811  */
812 static int audit_policy(struct aa_label *subj_label, const char *op,
813 			const char *ns_name, const char *name,
814 			const char *info, int error)
815 {
816 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, op);
817 
818 	ad.iface.ns = ns_name;
819 	ad.name = name;
820 	ad.info = info;
821 	ad.error = error;
822 	ad.subj_label = subj_label;
823 
824 	aa_audit_msg(AUDIT_APPARMOR_STATUS, &ad, audit_cb);
825 
826 	return error;
827 }
828 
829 /* don't call out to other LSMs in the stack for apparmor policy admin
830  * permissions
831  */
832 static int policy_ns_capable(const struct cred *subj_cred,
833 			     struct aa_label *label,
834 			     struct user_namespace *userns, int cap)
835 {
836 	int err;
837 
838 	/* check for MAC_ADMIN cap in cred */
839 	err = cap_capable(subj_cred, userns, cap, CAP_OPT_NONE);
840 	if (!err)
841 		err = aa_capable(subj_cred, label, cap, CAP_OPT_NONE);
842 
843 	return err;
844 }
845 
846 /**
847  * aa_policy_view_capable - check if viewing policy in at @ns is allowed
848  * @subj_cred: cred of subject
849  * @label: label that is trying to view policy in ns
850  * @ns: namespace being viewed by @label (may be NULL if @label's ns)
851  *
852  * Returns: true if viewing policy is allowed
853  *
854  * If @ns is NULL then the namespace being viewed is assumed to be the
855  * tasks current namespace.
856  */
857 bool aa_policy_view_capable(const struct cred *subj_cred,
858 			     struct aa_label *label, struct aa_ns *ns)
859 {
860 	struct user_namespace *user_ns = subj_cred->user_ns;
861 	struct aa_ns *view_ns = labels_view(label);
862 	bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) ||
863 			       in_egroup_p(make_kgid(user_ns, 0));
864 	bool response = false;
865 	if (!ns)
866 		ns = view_ns;
867 
868 	if (root_in_user_ns && aa_ns_visible(view_ns, ns, true) &&
869 	    (user_ns == &init_user_ns ||
870 	     (unprivileged_userns_apparmor_policy != 0 &&
871 	      user_ns->level == view_ns->level)))
872 		response = true;
873 
874 	return response;
875 }
876 
877 bool aa_policy_admin_capable(const struct cred *subj_cred,
878 			     struct aa_label *label, struct aa_ns *ns)
879 {
880 	struct user_namespace *user_ns = subj_cred->user_ns;
881 	bool capable = policy_ns_capable(subj_cred, label, user_ns,
882 					 CAP_MAC_ADMIN) == 0;
883 
884 	AA_DEBUG(DEBUG_POLICY, "cap_mac_admin? %d\n", capable);
885 	AA_DEBUG(DEBUG_POLICY, "policy locked? %d\n", aa_g_lock_policy);
886 
887 	return aa_policy_view_capable(subj_cred, label, ns) && capable &&
888 		!aa_g_lock_policy;
889 }
890 
891 bool aa_current_policy_view_capable(struct aa_ns *ns)
892 {
893 	struct aa_label *label;
894 	bool needput, res;
895 
896 	label = __begin_current_label_crit_section(&needput);
897 	res = aa_policy_view_capable(current_cred(), label, ns);
898 	__end_current_label_crit_section(label, needput);
899 
900 	return res;
901 }
902 
903 bool aa_current_policy_admin_capable(struct aa_ns *ns)
904 {
905 	struct aa_label *label;
906 	bool needput, res;
907 
908 	label = __begin_current_label_crit_section(&needput);
909 	res = aa_policy_admin_capable(current_cred(), label, ns);
910 	__end_current_label_crit_section(label, needput);
911 
912 	return res;
913 }
914 
915 /**
916  * aa_may_manage_policy - can the current task manage policy
917  * @subj_cred: subjects cred
918  * @label: label to check if it can manage policy
919  * @ns: namespace being managed by @label (may be NULL if @label's ns)
920  * @mask: contains the policy manipulation operation being done
921  *
922  * Returns: 0 if the task is allowed to manipulate policy else error
923  */
924 int aa_may_manage_policy(const struct cred *subj_cred, struct aa_label *label,
925 			 struct aa_ns *ns, u32 mask)
926 {
927 	const char *op;
928 
929 	if (mask & AA_MAY_REMOVE_POLICY)
930 		op = OP_PROF_RM;
931 	else if (mask & AA_MAY_REPLACE_POLICY)
932 		op = OP_PROF_REPL;
933 	else
934 		op = OP_PROF_LOAD;
935 
936 	/* check if loading policy is locked out */
937 	if (aa_g_lock_policy)
938 		return audit_policy(label, op, NULL, NULL, "policy_locked",
939 				    -EACCES);
940 
941 	if (!aa_policy_admin_capable(subj_cred, label, ns))
942 		return audit_policy(label, op, NULL, NULL, "not policy admin",
943 				    -EACCES);
944 
945 	/* TODO: add fine grained mediation of policy loads */
946 	return 0;
947 }
948 
949 static struct aa_profile *__list_lookup_parent(struct list_head *lh,
950 					       struct aa_profile *profile)
951 {
952 	const char *base = basename(profile->base.hname);
953 	long len = base - profile->base.hname;
954 	struct aa_load_ent *ent;
955 
956 	/* parent won't have trailing // so remove from len */
957 	if (len <= 2)
958 		return NULL;
959 	len -= 2;
960 
961 	list_for_each_entry(ent, lh, list) {
962 		if (ent->new == profile)
963 			continue;
964 		if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
965 		    0 && ent->new->base.hname[len] == 0)
966 			return ent->new;
967 	}
968 
969 	return NULL;
970 }
971 
972 /**
973  * __replace_profile - replace @old with @new on a list
974  * @old: profile to be replaced  (NOT NULL)
975  * @new: profile to replace @old with  (NOT NULL)
976  *
977  * Will duplicate and refcount elements that @new inherits from @old
978  * and will inherit @old children.
979  *
980  * refcount @new for list, put @old list refcount
981  *
982  * Requires: namespace list lock be held, or list not be shared
983  */
984 static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
985 {
986 	struct aa_profile *child, *tmp;
987 
988 	if (!list_empty(&old->base.profiles)) {
989 		LIST_HEAD(lh);
990 		list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
991 
992 		list_for_each_entry_safe(child, tmp, &lh, base.list) {
993 			struct aa_profile *p;
994 
995 			list_del_init(&child->base.list);
996 			p = __find_child(&new->base.profiles, child->base.name);
997 			if (p) {
998 				/* @p replaces @child  */
999 				__replace_profile(child, p);
1000 				continue;
1001 			}
1002 
1003 			/* inherit @child and its children */
1004 			/* TODO: update hname of inherited children */
1005 			/* list refcount transferred to @new */
1006 			p = aa_deref_parent(child);
1007 			rcu_assign_pointer(child->parent, aa_get_profile(new));
1008 			list_add_rcu(&child->base.list, &new->base.profiles);
1009 			aa_put_profile(p);
1010 		}
1011 	}
1012 
1013 	if (!rcu_access_pointer(new->parent)) {
1014 		struct aa_profile *parent = aa_deref_parent(old);
1015 		rcu_assign_pointer(new->parent, aa_get_profile(parent));
1016 	}
1017 	aa_label_replace(&old->label, &new->label);
1018 	/* migrate dents must come after label replacement b/c update */
1019 	__aafs_profile_migrate_dents(old, new);
1020 
1021 	if (list_empty(&new->base.list)) {
1022 		/* new is not on a list already */
1023 		list_replace_rcu(&old->base.list, &new->base.list);
1024 		aa_get_profile(new);
1025 		aa_put_profile(old);
1026 	} else
1027 		__list_remove_profile(old);
1028 }
1029 
1030 /**
1031  * __lookup_replace - lookup replacement information for a profile
1032  * @ns: namespace the lookup occurs in
1033  * @hname: name of profile to lookup
1034  * @noreplace: true if not replacing an existing profile
1035  * @p: Returns - profile to be replaced
1036  * @info: Returns - info string on why lookup failed
1037  *
1038  * Returns: profile to replace (no ref) on success else ptr error
1039  */
1040 static int __lookup_replace(struct aa_ns *ns, const char *hname,
1041 			    bool noreplace, struct aa_profile **p,
1042 			    const char **info)
1043 {
1044 	*p = aa_get_profile(__lookup_profile(&ns->base, hname));
1045 	if (*p) {
1046 		int error = replacement_allowed(*p, noreplace, info);
1047 		if (error) {
1048 			*info = "profile can not be replaced";
1049 			return error;
1050 		}
1051 	}
1052 
1053 	return 0;
1054 }
1055 
1056 static void share_name(struct aa_profile *old, struct aa_profile *new)
1057 {
1058 	aa_put_str(new->base.hname);
1059 	aa_get_str(old->base.hname);
1060 	new->base.hname = old->base.hname;
1061 	new->base.name = old->base.name;
1062 	new->label.hname = old->label.hname;
1063 }
1064 
1065 /* Update to newest version of parent after previous replacements
1066  * Returns: unrefcount newest version of parent
1067  */
1068 static struct aa_profile *update_to_newest_parent(struct aa_profile *new)
1069 {
1070 	struct aa_profile *parent, *newest;
1071 
1072 	parent = rcu_dereference_protected(new->parent,
1073 					   mutex_is_locked(&new->ns->lock));
1074 	newest = aa_get_newest_profile(parent);
1075 
1076 	/* parent replaced in this atomic set? */
1077 	if (newest != parent) {
1078 		aa_put_profile(parent);
1079 		rcu_assign_pointer(new->parent, newest);
1080 	} else
1081 		aa_put_profile(newest);
1082 
1083 	return newest;
1084 }
1085 
1086 /**
1087  * aa_replace_profiles - replace profile(s) on the profile list
1088  * @policy_ns: namespace load is occurring on
1089  * @label: label that is attempting to load/replace policy
1090  * @mask: permission mask
1091  * @udata: serialized data stream  (NOT NULL)
1092  *
1093  * unpack and replace a profile on the profile list and uses of that profile
1094  * by any task creds via invalidating the old version of the profile, which
1095  * tasks will notice to update their own cred.  If the profile does not exist
1096  * on the profile list it is added.
1097  *
1098  * Returns: size of data consumed else error code on failure.
1099  */
1100 ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label,
1101 			    u32 mask, struct aa_loaddata *udata)
1102 {
1103 	const char *ns_name = NULL, *info = NULL;
1104 	struct aa_ns *ns = NULL;
1105 	struct aa_load_ent *ent, *tmp;
1106 	struct aa_loaddata *rawdata_ent;
1107 	const char *op;
1108 	ssize_t count, error;
1109 	LIST_HEAD(lh);
1110 
1111 	op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD;
1112 	aa_get_loaddata(udata);
1113 	/* released below */
1114 	error = aa_unpack(udata, &lh, &ns_name);
1115 	if (error)
1116 		goto out;
1117 
1118 	/* ensure that profiles are all for the same ns
1119 	 * TODO: update locking to remove this constraint. All profiles in
1120 	 *       the load set must succeed as a set or the load will
1121 	 *       fail. Sort ent list and take ns locks in hierarchy order
1122 	 */
1123 	count = 0;
1124 	list_for_each_entry(ent, &lh, list) {
1125 		if (ns_name) {
1126 			if (ent->ns_name &&
1127 			    strcmp(ent->ns_name, ns_name) != 0) {
1128 				info = "policy load has mixed namespaces";
1129 				error = -EACCES;
1130 				goto fail;
1131 			}
1132 		} else if (ent->ns_name) {
1133 			if (count) {
1134 				info = "policy load has mixed namespaces";
1135 				error = -EACCES;
1136 				goto fail;
1137 			}
1138 			ns_name = ent->ns_name;
1139 		} else
1140 			count++;
1141 	}
1142 	if (ns_name) {
1143 		ns = aa_prepare_ns(policy_ns ? policy_ns : labels_ns(label),
1144 				   ns_name);
1145 		if (IS_ERR(ns)) {
1146 			op = OP_PROF_LOAD;
1147 			info = "failed to prepare namespace";
1148 			error = PTR_ERR(ns);
1149 			ns = NULL;
1150 			ent = NULL;
1151 			goto fail;
1152 		}
1153 	} else
1154 		ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(label));
1155 
1156 	mutex_lock_nested(&ns->lock, ns->level);
1157 	/* check for duplicate rawdata blobs: space and file dedup */
1158 	if (!list_empty(&ns->rawdata_list)) {
1159 		list_for_each_entry(rawdata_ent, &ns->rawdata_list, list) {
1160 			if (aa_rawdata_eq(rawdata_ent, udata)) {
1161 				struct aa_loaddata *tmp;
1162 
1163 				tmp = __aa_get_loaddata(rawdata_ent);
1164 				/* check we didn't fail the race */
1165 				if (tmp) {
1166 					aa_put_loaddata(udata);
1167 					udata = tmp;
1168 					break;
1169 				}
1170 			}
1171 		}
1172 	}
1173 	/* setup parent and ns info */
1174 	list_for_each_entry(ent, &lh, list) {
1175 		struct aa_policy *policy;
1176 		struct aa_profile *p;
1177 
1178 		if (aa_g_export_binary)
1179 			ent->new->rawdata = aa_get_loaddata(udata);
1180 		error = __lookup_replace(ns, ent->new->base.hname,
1181 					 !(mask & AA_MAY_REPLACE_POLICY),
1182 					 &ent->old, &info);
1183 		if (error)
1184 			goto fail_lock;
1185 
1186 		if (ent->new->rename) {
1187 			error = __lookup_replace(ns, ent->new->rename,
1188 						!(mask & AA_MAY_REPLACE_POLICY),
1189 						&ent->rename, &info);
1190 			if (error)
1191 				goto fail_lock;
1192 		}
1193 
1194 		/* released when @new is freed */
1195 		ent->new->ns = aa_get_ns(ns);
1196 
1197 		if (ent->old || ent->rename)
1198 			continue;
1199 
1200 		/* no ref on policy only use inside lock */
1201 		p = NULL;
1202 		policy = __lookup_parent(ns, ent->new->base.hname);
1203 		if (!policy) {
1204 			/* first check for parent in the load set */
1205 			p = __list_lookup_parent(&lh, ent->new);
1206 			if (!p) {
1207 				/*
1208 				 * fill in missing parent with null
1209 				 * profile that doesn't have
1210 				 * permissions. This allows for
1211 				 * individual profile loading where
1212 				 * the child is loaded before the
1213 				 * parent, and outside of the current
1214 				 * atomic set. This unfortunately can
1215 				 * happen with some userspaces.  The
1216 				 * null profile will be replaced once
1217 				 * the parent is loaded.
1218 				 */
1219 				policy = __create_missing_ancestors(ns,
1220 							ent->new->base.hname,
1221 							GFP_KERNEL);
1222 				if (!policy) {
1223 					error = -ENOENT;
1224 					info = "parent does not exist";
1225 					goto fail_lock;
1226 				}
1227 			}
1228 		}
1229 		if (!p && policy != &ns->base)
1230 			/* released on profile replacement or free_profile */
1231 			p = (struct aa_profile *) policy;
1232 		rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1233 	}
1234 
1235 	/* create new fs entries for introspection if needed */
1236 	if (!udata->dents[AAFS_LOADDATA_DIR] && aa_g_export_binary) {
1237 		error = __aa_fs_create_rawdata(ns, udata);
1238 		if (error) {
1239 			info = "failed to create raw_data dir and files";
1240 			ent = NULL;
1241 			goto fail_lock;
1242 		}
1243 	}
1244 	list_for_each_entry(ent, &lh, list) {
1245 		if (!ent->old) {
1246 			struct dentry *parent;
1247 			if (rcu_access_pointer(ent->new->parent)) {
1248 				struct aa_profile *p;
1249 				p = aa_deref_parent(ent->new);
1250 				parent = prof_child_dir(p);
1251 			} else
1252 				parent = ns_subprofs_dir(ent->new->ns);
1253 			error = __aafs_profile_mkdir(ent->new, parent);
1254 		}
1255 
1256 		if (error) {
1257 			info = "failed to create";
1258 			goto fail_lock;
1259 		}
1260 	}
1261 
1262 	/* Done with checks that may fail - do actual replacement */
1263 	__aa_bump_ns_revision(ns);
1264 	if (aa_g_export_binary)
1265 		__aa_loaddata_update(udata, ns->revision);
1266 	list_for_each_entry_safe(ent, tmp, &lh, list) {
1267 		list_del_init(&ent->list);
1268 		op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
1269 
1270 		if (ent->old && ent->old->rawdata == ent->new->rawdata &&
1271 		    ent->new->rawdata) {
1272 			/* dedup actual profile replacement */
1273 			audit_policy(label, op, ns_name, ent->new->base.hname,
1274 				     "same as current profile, skipping",
1275 				     error);
1276 			/* break refcount cycle with proxy. */
1277 			aa_put_proxy(ent->new->label.proxy);
1278 			ent->new->label.proxy = NULL;
1279 			goto skip;
1280 		}
1281 
1282 		/*
1283 		 * TODO: finer dedup based on profile range in data. Load set
1284 		 * can differ but profile may remain unchanged
1285 		 */
1286 		audit_policy(label, op, ns_name, ent->new->base.hname, NULL,
1287 			     error);
1288 
1289 		if (ent->old) {
1290 			share_name(ent->old, ent->new);
1291 			__replace_profile(ent->old, ent->new);
1292 		} else {
1293 			struct list_head *lh;
1294 
1295 			if (rcu_access_pointer(ent->new->parent)) {
1296 				struct aa_profile *parent;
1297 
1298 				parent = update_to_newest_parent(ent->new);
1299 				lh = &parent->base.profiles;
1300 			} else
1301 				lh = &ns->base.profiles;
1302 			__add_profile(lh, ent->new);
1303 		}
1304 	skip:
1305 		aa_load_ent_free(ent);
1306 	}
1307 	__aa_labelset_update_subtree(ns);
1308 	mutex_unlock(&ns->lock);
1309 
1310 out:
1311 	aa_put_ns(ns);
1312 	aa_put_loaddata(udata);
1313 	kfree(ns_name);
1314 
1315 	if (error)
1316 		return error;
1317 	return udata->size;
1318 
1319 fail_lock:
1320 	mutex_unlock(&ns->lock);
1321 
1322 	/* audit cause of failure */
1323 	op = (ent && !ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1324 fail:
1325 	  audit_policy(label, op, ns_name, ent ? ent->new->base.hname : NULL,
1326 		       info, error);
1327 	/* audit status that rest of profiles in the atomic set failed too */
1328 	info = "valid profile in failed atomic policy load";
1329 	list_for_each_entry(tmp, &lh, list) {
1330 		if (tmp == ent) {
1331 			info = "unchecked profile in failed atomic policy load";
1332 			/* skip entry that caused failure */
1333 			continue;
1334 		}
1335 		op = (!tmp->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1336 		audit_policy(label, op, ns_name, tmp->new->base.hname, info,
1337 			     error);
1338 	}
1339 	list_for_each_entry_safe(ent, tmp, &lh, list) {
1340 		list_del_init(&ent->list);
1341 		aa_load_ent_free(ent);
1342 	}
1343 
1344 	goto out;
1345 }
1346 
1347 /**
1348  * aa_remove_profiles - remove profile(s) from the system
1349  * @policy_ns: namespace the remove is being done from
1350  * @subj: label attempting to remove policy
1351  * @fqname: name of the profile or namespace to remove  (NOT NULL)
1352  * @size: size of the name
1353  *
1354  * Remove a profile or sub namespace from the current namespace, so that
1355  * they can not be found anymore and mark them as replaced by unconfined
1356  *
1357  * NOTE: removing confinement does not restore rlimits to preconfinement values
1358  *
1359  * Returns: size of data consume else error code if fails
1360  */
1361 ssize_t aa_remove_profiles(struct aa_ns *policy_ns, struct aa_label *subj,
1362 			   char *fqname, size_t size)
1363 {
1364 	struct aa_ns *ns = NULL;
1365 	struct aa_profile *profile = NULL;
1366 	const char *name = fqname, *info = NULL;
1367 	const char *ns_name = NULL;
1368 	ssize_t error = 0;
1369 
1370 	if (*fqname == 0) {
1371 		info = "no profile specified";
1372 		error = -ENOENT;
1373 		goto fail;
1374 	}
1375 
1376 	if (fqname[0] == ':') {
1377 		size_t ns_len;
1378 
1379 		name = aa_splitn_fqname(fqname, size, &ns_name, &ns_len);
1380 		/* released below */
1381 		ns = aa_lookupn_ns(policy_ns ? policy_ns : labels_ns(subj),
1382 				   ns_name, ns_len);
1383 		if (!ns) {
1384 			info = "namespace does not exist";
1385 			error = -ENOENT;
1386 			goto fail;
1387 		}
1388 	} else
1389 		/* released below */
1390 		ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(subj));
1391 
1392 	if (!name) {
1393 		/* remove namespace - can only happen if fqname[0] == ':' */
1394 		mutex_lock_nested(&ns->parent->lock, ns->parent->level);
1395 		__aa_bump_ns_revision(ns);
1396 		__aa_remove_ns(ns);
1397 		mutex_unlock(&ns->parent->lock);
1398 	} else {
1399 		/* remove profile */
1400 		mutex_lock_nested(&ns->lock, ns->level);
1401 		profile = aa_get_profile(__lookup_profile(&ns->base, name));
1402 		if (!profile) {
1403 			error = -ENOENT;
1404 			info = "profile does not exist";
1405 			goto fail_ns_lock;
1406 		}
1407 		name = profile->base.hname;
1408 		__aa_bump_ns_revision(ns);
1409 		__remove_profile(profile);
1410 		__aa_labelset_update_subtree(ns);
1411 		mutex_unlock(&ns->lock);
1412 	}
1413 
1414 	/* don't fail removal if audit fails */
1415 	(void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1416 			    error);
1417 	aa_put_ns(ns);
1418 	aa_put_profile(profile);
1419 	return size;
1420 
1421 fail_ns_lock:
1422 	mutex_unlock(&ns->lock);
1423 	aa_put_ns(ns);
1424 
1425 fail:
1426 	(void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1427 			    error);
1428 	return error;
1429 }
1430