blob: dd63ac92d28f2f79cd86d919e62b3e46aae6a870 [file] [log] [blame]
John Johansenc88d4c72010-07-29 14:48:00 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor policy manipulation functions
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
13 *
14 *
15 * AppArmor policy is based around profiles, which contain the rules a
16 * task is confined by. Every task in the system has a profile attached
17 * to it determined either by matching "unconfined" tasks against the
18 * visible set of profiles or by following a profiles attachment rules.
19 *
20 * Each profile exists in a profile namespace which is a container of
21 * visible profiles. Each namespace contains a special "unconfined" profile,
22 * which doesn't enforce any confinement on a task beyond DAC.
23 *
24 * Namespace and profile names can be written together in either
25 * of two syntaxes.
26 * :namespace:profile - used by kernel interfaces for easy detection
27 * namespace://profile - used by policy
28 *
29 * Profile names can not start with : or @ or ^ and may not contain \0
30 *
31 * Reserved profile names
32 * unconfined - special automatically generated unconfined profile
33 * inherit - special name to indicate profile inheritance
34 * null-XXXX-YYYY - special automatically generated learning profiles
35 *
36 * Namespace names may not start with / or @ and may not contain \0 or :
37 * Reserved namespace names
38 * user-XXXX - user defined profiles
39 *
40 * a // in a profile or namespace name indicates a hierarchical name with the
41 * name before the // being the parent and the name after the child.
42 *
43 * Profile and namespace hierarchies serve two different but similar purposes.
44 * The namespace contains the set of visible profiles that are considered
45 * for attachment. The hierarchy of namespaces allows for virtualizing
46 * the namespace so that for example a chroot can have its own set of profiles
47 * which may define some local user namespaces.
48 * The profile hierarchy severs two distinct purposes,
49 * - it allows for sub profiles or hats, which allows an application to run
50 * subprograms under its own profile with different restriction than it
51 * self, and not have it use the system profile.
52 * eg. if a mail program starts an editor, the policy might make the
53 * restrictions tighter on the editor tighter than the mail program,
54 * and definitely different than general editor restrictions
55 * - it allows for binary hierarchy of profiles, so that execution history
56 * is preserved. This feature isn't exploited by AppArmor reference policy
57 * but is allowed. NOTE: this is currently suboptimal because profile
58 * aliasing is not currently implemented so that a profile for each
59 * level must be defined.
60 * eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started
61 * from /bin/bash
62 *
63 * A profile or namespace name that can contain one or more // separators
64 * is referred to as an hname (hierarchical).
65 * eg. /bin/bash//bin/ls
66 *
67 * An fqname is a name that may contain both namespace and profile hnames.
68 * eg. :ns:/bin/bash//bin/ls
69 *
70 * NOTES:
71 * - locking of profile lists is currently fairly coarse. All profile
72 * lists within a namespace use the namespace lock.
73 * FIXME: move profile lists to using rcu_lists
74 */
75
76#include <linux/slab.h>
77#include <linux/spinlock.h>
78#include <linux/string.h>
79
80#include "include/apparmor.h"
81#include "include/capability.h"
82#include "include/context.h"
83#include "include/file.h"
84#include "include/ipc.h"
85#include "include/match.h"
86#include "include/path.h"
87#include "include/policy.h"
John Johansencff281f2017-01-16 00:42:15 -080088#include "include/policy_ns.h"
John Johansenc88d4c72010-07-29 14:48:00 -070089#include "include/policy_unpack.h"
90#include "include/resource.h"
John Johansenc88d4c72010-07-29 14:48:00 -070091
92
John Johansen0d259f02013-07-10 21:13:43 -070093const char *const aa_profile_mode_names[] = {
John Johansenc88d4c72010-07-29 14:48:00 -070094 "enforce",
95 "complain",
96 "kill",
John Johansen03816502013-07-10 21:12:43 -070097 "unconfined",
John Johansenc88d4c72010-07-29 14:48:00 -070098};
99
John Johansenc88d4c72010-07-29 14:48:00 -0700100
John Johansencff281f2017-01-16 00:42:15 -0800101/* requires profile list write lock held */
John Johansen83995882017-01-16 00:42:19 -0800102void __aa_update_proxy(struct aa_profile *orig, struct aa_profile *new)
John Johansenc88d4c72010-07-29 14:48:00 -0700103{
John Johansencff281f2017-01-16 00:42:15 -0800104 struct aa_profile *tmp;
John Johansenc88d4c72010-07-29 14:48:00 -0700105
John Johansen83995882017-01-16 00:42:19 -0800106 tmp = rcu_dereference_protected(orig->proxy->profile,
John Johansencff281f2017-01-16 00:42:15 -0800107 mutex_is_locked(&orig->ns->lock));
John Johansen83995882017-01-16 00:42:19 -0800108 rcu_assign_pointer(orig->proxy->profile, aa_get_profile(new));
John Johansend97d51d2017-01-16 00:42:18 -0800109 orig->flags |= PFLAG_STALE;
John Johansencff281f2017-01-16 00:42:15 -0800110 aa_put_profile(tmp);
John Johansenc88d4c72010-07-29 14:48:00 -0700111}
112
113/**
114 * __list_add_profile - add a profile to a list
115 * @list: list to add it to (NOT NULL)
116 * @profile: the profile to add (NOT NULL)
117 *
118 * refcount @profile, should be put by __list_remove_profile
119 *
120 * Requires: namespace lock be held, or list not be shared
121 */
122static void __list_add_profile(struct list_head *list,
123 struct aa_profile *profile)
124{
John Johansen01e2b672013-07-10 21:06:43 -0700125 list_add_rcu(&profile->base.list, list);
John Johansenc88d4c72010-07-29 14:48:00 -0700126 /* get list reference */
127 aa_get_profile(profile);
128}
129
130/**
131 * __list_remove_profile - remove a profile from the list it is on
132 * @profile: the profile to remove (NOT NULL)
133 *
134 * remove a profile from the list, warning generally removal should
135 * be done with __replace_profile as most profile removals are
136 * replacements to the unconfined profile.
137 *
138 * put @profile list refcount
139 *
140 * Requires: namespace lock be held, or list not have been live
141 */
142static void __list_remove_profile(struct aa_profile *profile)
143{
John Johansen01e2b672013-07-10 21:06:43 -0700144 list_del_rcu(&profile->base.list);
145 aa_put_profile(profile);
John Johansenc88d4c72010-07-29 14:48:00 -0700146}
147
John Johansenc88d4c72010-07-29 14:48:00 -0700148/**
149 * __remove_profile - remove old profile, and children
150 * @profile: profile to be replaced (NOT NULL)
151 *
152 * Requires: namespace list lock be held, or list not be shared
153 */
154static void __remove_profile(struct aa_profile *profile)
155{
156 /* release any children lists first */
John Johansencff281f2017-01-16 00:42:15 -0800157 __aa_profile_list_release(&profile->base.profiles);
John Johansenc88d4c72010-07-29 14:48:00 -0700158 /* released by free_profile */
John Johansen83995882017-01-16 00:42:19 -0800159 __aa_update_proxy(profile, profile->ns->unconfined);
John Johansen0d259f02013-07-10 21:13:43 -0700160 __aa_fs_profile_rmdir(profile);
John Johansenc88d4c72010-07-29 14:48:00 -0700161 __list_remove_profile(profile);
162}
163
164/**
John Johansencff281f2017-01-16 00:42:15 -0800165 * __aa_profile_list_release - remove all profiles on the list and put refs
John Johansenc88d4c72010-07-29 14:48:00 -0700166 * @head: list of profiles (NOT NULL)
167 *
168 * Requires: namespace lock be held
169 */
John Johansencff281f2017-01-16 00:42:15 -0800170void __aa_profile_list_release(struct list_head *head)
John Johansenc88d4c72010-07-29 14:48:00 -0700171{
172 struct aa_profile *profile, *tmp;
173 list_for_each_entry_safe(profile, tmp, head, base.list)
174 __remove_profile(profile);
175}
176
John Johansen77b071b2013-07-10 21:07:43 -0700177
John Johansen83995882017-01-16 00:42:19 -0800178static void free_proxy(struct aa_proxy *p)
John Johansen77b071b2013-07-10 21:07:43 -0700179{
John Johansen83995882017-01-16 00:42:19 -0800180 if (p) {
John Johansen4cd4fc72013-09-29 08:39:22 -0700181 /* r->profile will not be updated any more as r is dead */
John Johansen83995882017-01-16 00:42:19 -0800182 aa_put_profile(rcu_dereference_protected(p->profile, true));
183 kzfree(p);
John Johansen77b071b2013-07-10 21:07:43 -0700184 }
185}
186
187
John Johansen83995882017-01-16 00:42:19 -0800188void aa_free_proxy_kref(struct kref *kref)
John Johansen77b071b2013-07-10 21:07:43 -0700189{
John Johansen83995882017-01-16 00:42:19 -0800190 struct aa_proxy *p = container_of(kref, struct aa_proxy, count);
191
192 free_proxy(p);
John Johansen77b071b2013-07-10 21:07:43 -0700193}
194
John Johansenc88d4c72010-07-29 14:48:00 -0700195/**
John Johansen8651e1d62013-07-10 21:11:43 -0700196 * aa_free_profile - free a profile
John Johansenc88d4c72010-07-29 14:48:00 -0700197 * @profile: the profile to free (MAYBE NULL)
198 *
199 * Free a profile, its hats and null_profile. All references to the profile,
200 * its hats and null_profile must have been put.
201 *
202 * If the profile was referenced from a task context, free_profile() will
203 * be called from an rcu callback routine, so we must not sleep here.
204 */
John Johansen8651e1d62013-07-10 21:11:43 -0700205void aa_free_profile(struct aa_profile *profile)
John Johansenc88d4c72010-07-29 14:48:00 -0700206{
207 AA_DEBUG("%s(%p)\n", __func__, profile);
208
209 if (!profile)
210 return;
211
John Johansenc88d4c72010-07-29 14:48:00 -0700212 /* free children profiles */
John Johansenfe6bb312017-01-16 00:42:14 -0800213 aa_policy_destroy(&profile->base);
John Johansen01e2b672013-07-10 21:06:43 -0700214 aa_put_profile(rcu_access_pointer(profile->parent));
John Johansenc88d4c72010-07-29 14:48:00 -0700215
John Johansen98849df2017-01-16 00:42:16 -0800216 aa_put_ns(profile->ns);
John Johansenc88d4c72010-07-29 14:48:00 -0700217 kzfree(profile->rename);
218
219 aa_free_file_rules(&profile->file);
220 aa_free_cap_rules(&profile->caps);
221 aa_free_rlimit_rules(&profile->rlimits);
222
John Johansen0d259f02013-07-10 21:13:43 -0700223 kzfree(profile->dirname);
John Johansenc88d4c72010-07-29 14:48:00 -0700224 aa_put_dfa(profile->xmatch);
John Johansenad5ff3d2012-02-16 07:07:53 -0800225 aa_put_dfa(profile->policy.dfa);
John Johansen83995882017-01-16 00:42:19 -0800226 aa_put_proxy(profile->proxy);
John Johansenc88d4c72010-07-29 14:48:00 -0700227
John Johansen5cb3e912013-10-14 11:44:34 -0700228 kzfree(profile->hash);
John Johansenc88d4c72010-07-29 14:48:00 -0700229 kzfree(profile);
230}
231
232/**
John Johansen01e2b672013-07-10 21:06:43 -0700233 * aa_free_profile_rcu - free aa_profile by rcu (called by aa_free_profile_kref)
234 * @head: rcu_head callback for freeing of a profile (NOT NULL)
235 */
236static void aa_free_profile_rcu(struct rcu_head *head)
237{
John Johansen742058b2013-07-10 21:10:43 -0700238 struct aa_profile *p = container_of(head, struct aa_profile, rcu);
239 if (p->flags & PFLAG_NS_COUNT)
John Johansen98849df2017-01-16 00:42:16 -0800240 aa_free_ns(p->ns);
John Johansen742058b2013-07-10 21:10:43 -0700241 else
John Johansen8651e1d62013-07-10 21:11:43 -0700242 aa_free_profile(p);
John Johansen01e2b672013-07-10 21:06:43 -0700243}
244
245/**
John Johansenc88d4c72010-07-29 14:48:00 -0700246 * aa_free_profile_kref - free aa_profile by kref (called by aa_put_profile)
247 * @kr: kref callback for freeing of a profile (NOT NULL)
248 */
249void aa_free_profile_kref(struct kref *kref)
250{
John Johansenfa2ac462013-07-10 21:08:43 -0700251 struct aa_profile *p = container_of(kref, struct aa_profile, count);
John Johansen742058b2013-07-10 21:10:43 -0700252 call_rcu(&p->rcu, aa_free_profile_rcu);
John Johansenc88d4c72010-07-29 14:48:00 -0700253}
254
John Johansen4da05cc2013-02-18 16:11:34 -0800255/**
256 * aa_alloc_profile - allocate, initialize and return a new profile
257 * @hname: name of the profile (NOT NULL)
John Johansen30b026a2017-01-16 00:42:35 -0800258 * @gfp: allocation type
John Johansen4da05cc2013-02-18 16:11:34 -0800259 *
260 * Returns: refcount profile or NULL on failure
261 */
John Johansen30b026a2017-01-16 00:42:35 -0800262struct aa_profile *aa_alloc_profile(const char *hname, gfp_t gfp)
John Johansen4da05cc2013-02-18 16:11:34 -0800263{
264 struct aa_profile *profile;
265
266 /* freed by free_profile - usually through aa_put_profile */
John Johansen30b026a2017-01-16 00:42:35 -0800267 profile = kzalloc(sizeof(*profile), gfp);
John Johansen4da05cc2013-02-18 16:11:34 -0800268 if (!profile)
269 return NULL;
270
John Johansen30b026a2017-01-16 00:42:35 -0800271 profile->proxy = kzalloc(sizeof(struct aa_proxy), gfp);
John Johansen83995882017-01-16 00:42:19 -0800272 if (!profile->proxy)
John Johansen77b071b2013-07-10 21:07:43 -0700273 goto fail;
John Johansen83995882017-01-16 00:42:19 -0800274 kref_init(&profile->proxy->count);
John Johansen77b071b2013-07-10 21:07:43 -0700275
John Johansen30b026a2017-01-16 00:42:35 -0800276 if (!aa_policy_init(&profile->base, NULL, hname, gfp))
John Johansen77b071b2013-07-10 21:07:43 -0700277 goto fail;
John Johansenfa2ac462013-07-10 21:08:43 -0700278 kref_init(&profile->count);
John Johansen4da05cc2013-02-18 16:11:34 -0800279
280 /* refcount released by caller */
281 return profile;
John Johansen77b071b2013-07-10 21:07:43 -0700282
283fail:
John Johansen83995882017-01-16 00:42:19 -0800284 kzfree(profile->proxy);
John Johansen77b071b2013-07-10 21:07:43 -0700285 kzfree(profile);
286
287 return NULL;
John Johansen4da05cc2013-02-18 16:11:34 -0800288}
289
290/**
John Johansen181f7c92017-01-16 00:42:36 -0800291 * aa_new_null_profile - create or find a null-X learning profile
John Johansen4da05cc2013-02-18 16:11:34 -0800292 * @parent: profile that caused this profile to be created (NOT NULL)
293 * @hat: true if the null- learning profile is a hat
John Johansen181f7c92017-01-16 00:42:36 -0800294 * @base: name to base the null profile off of
295 * @gfp: type of allocation
John Johansen4da05cc2013-02-18 16:11:34 -0800296 *
John Johansen181f7c92017-01-16 00:42:36 -0800297 * Find/Create a null- complain mode profile used in learning mode. The
298 * name of the profile is unique and follows the format of parent//null-XXX.
299 * where XXX is based on the @name or if that fails or is not supplied
300 * a unique number
John Johansen4da05cc2013-02-18 16:11:34 -0800301 *
302 * null profiles are added to the profile list but the list does not
303 * hold a count on them so that they are automatically released when
304 * not in use.
305 *
306 * Returns: new refcounted profile else NULL on failure
307 */
John Johansen181f7c92017-01-16 00:42:36 -0800308struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
309 const char *base, gfp_t gfp)
John Johansen4da05cc2013-02-18 16:11:34 -0800310{
John Johansen181f7c92017-01-16 00:42:36 -0800311 struct aa_profile *profile;
John Johansen4da05cc2013-02-18 16:11:34 -0800312 char *name;
John Johansen4da05cc2013-02-18 16:11:34 -0800313
John Johansen181f7c92017-01-16 00:42:36 -0800314 AA_BUG(!parent);
315
316 if (base) {
317 name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
318 gfp);
319 if (name) {
320 sprintf(name, "%s//null-%s", parent->base.hname, base);
321 goto name;
322 }
323 /* fall through to try shorter uniq */
324 }
325
326 name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
John Johansen4da05cc2013-02-18 16:11:34 -0800327 if (!name)
John Johansen181f7c92017-01-16 00:42:36 -0800328 return NULL;
329 sprintf(name, "%s//null-%x", parent->base.hname,
330 atomic_inc_return(&parent->ns->uniq_null));
John Johansen4da05cc2013-02-18 16:11:34 -0800331
John Johansen181f7c92017-01-16 00:42:36 -0800332name:
333 /* lookup to see if this is a dup creation */
334 profile = aa_find_child(parent, basename(name));
335 if (profile)
336 goto out;
337
338 profile = aa_alloc_profile(name, gfp);
John Johansen4da05cc2013-02-18 16:11:34 -0800339 if (!profile)
340 goto fail;
341
342 profile->mode = APPARMOR_COMPLAIN;
John Johansen181f7c92017-01-16 00:42:36 -0800343 profile->flags |= PFLAG_NULL;
John Johansen4da05cc2013-02-18 16:11:34 -0800344 if (hat)
345 profile->flags |= PFLAG_HAT;
John Johansen181f7c92017-01-16 00:42:36 -0800346 profile->path_flags = parent->path_flags;
John Johansen4da05cc2013-02-18 16:11:34 -0800347
348 /* released on free_profile */
John Johansen01e2b672013-07-10 21:06:43 -0700349 rcu_assign_pointer(profile->parent, aa_get_profile(parent));
John Johansen98849df2017-01-16 00:42:16 -0800350 profile->ns = aa_get_ns(parent->ns);
John Johansen4da05cc2013-02-18 16:11:34 -0800351
John Johansen01e2b672013-07-10 21:06:43 -0700352 mutex_lock(&profile->ns->lock);
John Johansen4da05cc2013-02-18 16:11:34 -0800353 __list_add_profile(&parent->base.profiles, profile);
John Johansen01e2b672013-07-10 21:06:43 -0700354 mutex_unlock(&profile->ns->lock);
John Johansen4da05cc2013-02-18 16:11:34 -0800355
356 /* refcount released by caller */
John Johansen181f7c92017-01-16 00:42:36 -0800357out:
358 kfree(name);
359
John Johansen4da05cc2013-02-18 16:11:34 -0800360 return profile;
361
362fail:
John Johansen181f7c92017-01-16 00:42:36 -0800363 kfree(name);
364 aa_free_profile(profile);
John Johansen4da05cc2013-02-18 16:11:34 -0800365 return NULL;
366}
367
John Johansenc88d4c72010-07-29 14:48:00 -0700368/* TODO: profile accounting - setup in remove */
369
370/**
371 * __find_child - find a profile on @head list with a name matching @name
372 * @head: list to search (NOT NULL)
373 * @name: name of profile (NOT NULL)
374 *
John Johansen01e2b672013-07-10 21:06:43 -0700375 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700376 *
377 * Returns: unrefcounted profile ptr, or NULL if not found
378 */
379static struct aa_profile *__find_child(struct list_head *head, const char *name)
380{
381 return (struct aa_profile *)__policy_find(head, name);
382}
383
384/**
385 * __strn_find_child - find a profile on @head list using substring of @name
386 * @head: list to search (NOT NULL)
387 * @name: name of profile (NOT NULL)
388 * @len: length of @name substring to match
389 *
John Johansen01e2b672013-07-10 21:06:43 -0700390 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700391 *
392 * Returns: unrefcounted profile ptr, or NULL if not found
393 */
394static struct aa_profile *__strn_find_child(struct list_head *head,
395 const char *name, int len)
396{
397 return (struct aa_profile *)__policy_strn_find(head, name, len);
398}
399
400/**
401 * aa_find_child - find a profile by @name in @parent
402 * @parent: profile to search (NOT NULL)
403 * @name: profile name to search for (NOT NULL)
404 *
405 * Returns: a refcounted profile or NULL if not found
406 */
407struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
408{
409 struct aa_profile *profile;
410
John Johansen01e2b672013-07-10 21:06:43 -0700411 rcu_read_lock();
John Johansende7c4cc2015-12-16 18:09:10 -0800412 do {
413 profile = __find_child(&parent->base.profiles, name);
414 } while (profile && !aa_get_profile_not0(profile));
John Johansen01e2b672013-07-10 21:06:43 -0700415 rcu_read_unlock();
John Johansenc88d4c72010-07-29 14:48:00 -0700416
417 /* refcount released by caller */
418 return profile;
419}
420
421/**
422 * __lookup_parent - lookup the parent of a profile of name @hname
423 * @ns: namespace to lookup profile in (NOT NULL)
424 * @hname: hierarchical profile name to find parent of (NOT NULL)
425 *
426 * Lookups up the parent of a fully qualified profile name, the profile
427 * that matches hname does not need to exist, in general this
428 * is used to load a new profile.
429 *
John Johansen01e2b672013-07-10 21:06:43 -0700430 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700431 *
432 * Returns: unrefcounted policy or NULL if not found
433 */
John Johansen98849df2017-01-16 00:42:16 -0800434static struct aa_policy *__lookup_parent(struct aa_ns *ns,
John Johansenc88d4c72010-07-29 14:48:00 -0700435 const char *hname)
436{
437 struct aa_policy *policy;
438 struct aa_profile *profile = NULL;
439 char *split;
440
441 policy = &ns->base;
442
443 for (split = strstr(hname, "//"); split;) {
444 profile = __strn_find_child(&policy->profiles, hname,
445 split - hname);
446 if (!profile)
447 return NULL;
448 policy = &profile->base;
449 hname = split + 2;
450 split = strstr(hname, "//");
451 }
452 if (!profile)
453 return &ns->base;
454 return &profile->base;
455}
456
457/**
John Johansen1741e9e2017-01-16 00:42:21 -0800458 * __lookupn_profile - lookup the profile matching @hname
John Johansenc88d4c72010-07-29 14:48:00 -0700459 * @base: base list to start looking up profile name from (NOT NULL)
460 * @hname: hierarchical profile name (NOT NULL)
John Johansen1741e9e2017-01-16 00:42:21 -0800461 * @n: length of @hname
John Johansenc88d4c72010-07-29 14:48:00 -0700462 *
John Johansen01e2b672013-07-10 21:06:43 -0700463 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700464 *
465 * Returns: unrefcounted profile pointer or NULL if not found
466 *
467 * Do a relative name lookup, recursing through profile tree.
468 */
John Johansen1741e9e2017-01-16 00:42:21 -0800469static struct aa_profile *__lookupn_profile(struct aa_policy *base,
470 const char *hname, size_t n)
John Johansenc88d4c72010-07-29 14:48:00 -0700471{
472 struct aa_profile *profile = NULL;
John Johansen1741e9e2017-01-16 00:42:21 -0800473 const char *split;
John Johansenc88d4c72010-07-29 14:48:00 -0700474
John Johansen1741e9e2017-01-16 00:42:21 -0800475 for (split = strnstr(hname, "//", n); split;
476 split = strnstr(hname, "//", n)) {
John Johansenc88d4c72010-07-29 14:48:00 -0700477 profile = __strn_find_child(&base->profiles, hname,
478 split - hname);
479 if (!profile)
480 return NULL;
481
482 base = &profile->base;
John Johansen1741e9e2017-01-16 00:42:21 -0800483 n -= split + 2 - hname;
John Johansenc88d4c72010-07-29 14:48:00 -0700484 hname = split + 2;
John Johansenc88d4c72010-07-29 14:48:00 -0700485 }
486
John Johansen1741e9e2017-01-16 00:42:21 -0800487 if (n)
488 return __strn_find_child(&base->profiles, hname, n);
489 return NULL;
490}
John Johansenc88d4c72010-07-29 14:48:00 -0700491
John Johansen1741e9e2017-01-16 00:42:21 -0800492static struct aa_profile *__lookup_profile(struct aa_policy *base,
493 const char *hname)
494{
495 return __lookupn_profile(base, hname, strlen(hname));
John Johansenc88d4c72010-07-29 14:48:00 -0700496}
497
498/**
499 * aa_lookup_profile - find a profile by its full or partial name
500 * @ns: the namespace to start from (NOT NULL)
501 * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL)
John Johansen1741e9e2017-01-16 00:42:21 -0800502 * @n: size of @hname
John Johansenc88d4c72010-07-29 14:48:00 -0700503 *
504 * Returns: refcounted profile or NULL if not found
505 */
John Johansen1741e9e2017-01-16 00:42:21 -0800506struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
507 size_t n)
John Johansenc88d4c72010-07-29 14:48:00 -0700508{
509 struct aa_profile *profile;
510
John Johansen01e2b672013-07-10 21:06:43 -0700511 rcu_read_lock();
512 do {
John Johansen1741e9e2017-01-16 00:42:21 -0800513 profile = __lookupn_profile(&ns->base, hname, n);
John Johansen01e2b672013-07-10 21:06:43 -0700514 } while (profile && !aa_get_profile_not0(profile));
515 rcu_read_unlock();
John Johansenc88d4c72010-07-29 14:48:00 -0700516
John Johansenbf832082012-05-16 11:00:05 -0700517 /* the unconfined profile is not in the regular profile list */
John Johansen1741e9e2017-01-16 00:42:21 -0800518 if (!profile && strncmp(hname, "unconfined", n) == 0)
John Johansenfa2ac462013-07-10 21:08:43 -0700519 profile = aa_get_newest_profile(ns->unconfined);
John Johansenbf832082012-05-16 11:00:05 -0700520
John Johansenc88d4c72010-07-29 14:48:00 -0700521 /* refcount released by caller */
522 return profile;
523}
524
John Johansen1741e9e2017-01-16 00:42:21 -0800525struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *hname)
526{
527 return aa_lookupn_profile(ns, hname, strlen(hname));
528}
John Johansen31617dd2017-01-16 00:42:24 -0800529
530struct aa_profile *aa_fqlookupn_profile(struct aa_profile *base,
531 const char *fqname, size_t n)
532{
533 struct aa_profile *profile;
534 struct aa_ns *ns;
535 const char *name, *ns_name;
536 size_t ns_len;
537
538 name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
539 if (ns_name) {
540 ns = aa_findn_ns(base->ns, ns_name, ns_len);
541 if (!ns)
542 return NULL;
543 } else
544 ns = aa_get_ns(base->ns);
545
546 if (name)
547 profile = aa_lookupn_profile(ns, name, n - (name - fqname));
548 else if (ns)
549 /* default profile for ns, currently unconfined */
550 profile = aa_get_newest_profile(ns->unconfined);
551 else
552 profile = NULL;
553 aa_put_ns(ns);
554
555 return profile;
556}
557
John Johansenc88d4c72010-07-29 14:48:00 -0700558/**
559 * replacement_allowed - test to see if replacement is allowed
560 * @profile: profile to test if it can be replaced (MAYBE NULL)
561 * @noreplace: true if replacement shouldn't be allowed but addition is okay
562 * @info: Returns - info about why replacement failed (NOT NULL)
563 *
564 * Returns: %0 if replacement allowed else error code
565 */
566static int replacement_allowed(struct aa_profile *profile, int noreplace,
567 const char **info)
568{
569 if (profile) {
570 if (profile->flags & PFLAG_IMMUTABLE) {
571 *info = "cannot replace immutible profile";
572 return -EPERM;
573 } else if (noreplace) {
574 *info = "profile already exists";
575 return -EEXIST;
576 }
577 }
578 return 0;
579}
580
581/**
John Johansenc88d4c72010-07-29 14:48:00 -0700582 * aa_audit_policy - Do auditing of policy changes
583 * @op: policy operation being performed
584 * @gfp: memory allocation flags
585 * @name: name of profile being manipulated (NOT NULL)
586 * @info: any extra information to be audited (MAYBE NULL)
587 * @error: error code
588 *
589 * Returns: the error to be returned after audit is done
590 */
591static int audit_policy(int op, gfp_t gfp, const char *name, const char *info,
592 int error)
593{
594 struct common_audit_data sa;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700595 struct apparmor_audit_data aad = {0,};
Eric Paris50c205f2012-04-04 15:01:43 -0400596 sa.type = LSM_AUDIT_DATA_NONE;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700597 sa.aad = &aad;
598 aad.op = op;
599 aad.name = name;
600 aad.info = info;
601 aad.error = error;
John Johansenc88d4c72010-07-29 14:48:00 -0700602
603 return aa_audit(AUDIT_APPARMOR_STATUS, __aa_current_profile(), gfp,
604 &sa, NULL);
605}
606
John Johansen58acf9d2016-06-22 18:01:08 -0700607bool policy_view_capable(void)
608{
609 struct user_namespace *user_ns = current_user_ns();
610 bool response = false;
611
612 if (ns_capable(user_ns, CAP_MAC_ADMIN))
613 response = true;
614
615 return response;
616}
617
618bool policy_admin_capable(void)
619{
620 return policy_view_capable() && !aa_g_lock_policy;
621}
622
John Johansenc88d4c72010-07-29 14:48:00 -0700623/**
624 * aa_may_manage_policy - can the current task manage policy
625 * @op: the policy manipulation operation being done
626 *
627 * Returns: true if the task is allowed to manipulate policy
628 */
629bool aa_may_manage_policy(int op)
630{
631 /* check if loading policy is locked out */
632 if (aa_g_lock_policy) {
633 audit_policy(op, GFP_KERNEL, NULL, "policy_locked", -EACCES);
634 return 0;
635 }
636
John Johansen58acf9d2016-06-22 18:01:08 -0700637 if (!policy_admin_capable()) {
John Johansenc88d4c72010-07-29 14:48:00 -0700638 audit_policy(op, GFP_KERNEL, NULL, "not policy admin", -EACCES);
639 return 0;
640 }
641
642 return 1;
643}
644
John Johansendd51c8482013-07-10 21:05:43 -0700645static struct aa_profile *__list_lookup_parent(struct list_head *lh,
646 struct aa_profile *profile)
647{
John Johansen6e474e32017-01-16 00:42:29 -0800648 const char *base = basename(profile->base.hname);
John Johansendd51c8482013-07-10 21:05:43 -0700649 long len = base - profile->base.hname;
650 struct aa_load_ent *ent;
651
652 /* parent won't have trailing // so remove from len */
653 if (len <= 2)
654 return NULL;
655 len -= 2;
656
657 list_for_each_entry(ent, lh, list) {
658 if (ent->new == profile)
659 continue;
660 if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
661 0 && ent->new->base.hname[len] == 0)
662 return ent->new;
663 }
664
665 return NULL;
666}
667
668/**
669 * __replace_profile - replace @old with @new on a list
670 * @old: profile to be replaced (NOT NULL)
671 * @new: profile to replace @old with (NOT NULL)
John Johansen83995882017-01-16 00:42:19 -0800672 * @share_proxy: transfer @old->proxy to @new
John Johansendd51c8482013-07-10 21:05:43 -0700673 *
674 * Will duplicate and refcount elements that @new inherits from @old
675 * and will inherit @old children.
676 *
677 * refcount @new for list, put @old list refcount
678 *
679 * Requires: namespace list lock be held, or list not be shared
680 */
John Johansen77b071b2013-07-10 21:07:43 -0700681static void __replace_profile(struct aa_profile *old, struct aa_profile *new,
John Johansen83995882017-01-16 00:42:19 -0800682 bool share_proxy)
John Johansendd51c8482013-07-10 21:05:43 -0700683{
684 struct aa_profile *child, *tmp;
685
686 if (!list_empty(&old->base.profiles)) {
687 LIST_HEAD(lh);
John Johansen01e2b672013-07-10 21:06:43 -0700688 list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
John Johansendd51c8482013-07-10 21:05:43 -0700689
690 list_for_each_entry_safe(child, tmp, &lh, base.list) {
691 struct aa_profile *p;
692
693 list_del_init(&child->base.list);
694 p = __find_child(&new->base.profiles, child->base.name);
695 if (p) {
696 /* @p replaces @child */
John Johansen83995882017-01-16 00:42:19 -0800697 __replace_profile(child, p, share_proxy);
John Johansendd51c8482013-07-10 21:05:43 -0700698 continue;
699 }
700
701 /* inherit @child and its children */
702 /* TODO: update hname of inherited children */
703 /* list refcount transferred to @new */
John Johansen0d259f02013-07-10 21:13:43 -0700704 p = aa_deref_parent(child);
John Johansen01e2b672013-07-10 21:06:43 -0700705 rcu_assign_pointer(child->parent, aa_get_profile(new));
706 list_add_rcu(&child->base.list, &new->base.profiles);
707 aa_put_profile(p);
John Johansendd51c8482013-07-10 21:05:43 -0700708 }
709 }
710
John Johansen01e2b672013-07-10 21:06:43 -0700711 if (!rcu_access_pointer(new->parent)) {
John Johansen0d259f02013-07-10 21:13:43 -0700712 struct aa_profile *parent = aa_deref_parent(old);
John Johansen01e2b672013-07-10 21:06:43 -0700713 rcu_assign_pointer(new->parent, aa_get_profile(parent));
714 }
John Johansen83995882017-01-16 00:42:19 -0800715 __aa_update_proxy(old, new);
716 if (share_proxy) {
717 aa_put_proxy(new->proxy);
718 new->proxy = aa_get_proxy(old->proxy);
719 } else if (!rcu_access_pointer(new->proxy->profile))
720 /* aafs interface uses proxy */
721 rcu_assign_pointer(new->proxy->profile,
John Johansen0d259f02013-07-10 21:13:43 -0700722 aa_get_profile(new));
723 __aa_fs_profile_migrate_dents(old, new);
John Johansendd51c8482013-07-10 21:05:43 -0700724
725 if (list_empty(&new->base.list)) {
726 /* new is not on a list already */
John Johansen01e2b672013-07-10 21:06:43 -0700727 list_replace_rcu(&old->base.list, &new->base.list);
John Johansendd51c8482013-07-10 21:05:43 -0700728 aa_get_profile(new);
729 aa_put_profile(old);
730 } else
731 __list_remove_profile(old);
732}
733
734/**
735 * __lookup_replace - lookup replacement information for a profile
736 * @ns - namespace the lookup occurs in
737 * @hname - name of profile to lookup
738 * @noreplace - true if not replacing an existing profile
739 * @p - Returns: profile to be replaced
740 * @info - Returns: info string on why lookup failed
741 *
742 * Returns: profile to replace (no ref) on success else ptr error
743 */
John Johansen98849df2017-01-16 00:42:16 -0800744static int __lookup_replace(struct aa_ns *ns, const char *hname,
John Johansendd51c8482013-07-10 21:05:43 -0700745 bool noreplace, struct aa_profile **p,
746 const char **info)
747{
748 *p = aa_get_profile(__lookup_profile(&ns->base, hname));
749 if (*p) {
750 int error = replacement_allowed(*p, noreplace, info);
751 if (error) {
752 *info = "profile can not be replaced";
753 return error;
754 }
755 }
756
757 return 0;
758}
759
John Johansenc88d4c72010-07-29 14:48:00 -0700760/**
761 * aa_replace_profiles - replace profile(s) on the profile list
John Johansen73688d12017-01-16 00:42:34 -0800762 * @view: namespace load is viewed from
John Johansenc88d4c72010-07-29 14:48:00 -0700763 * @udata: serialized data stream (NOT NULL)
764 * @size: size of the serialized data stream
765 * @noreplace: true if only doing addition, no replacement allowed
766 *
767 * unpack and replace a profile on the profile list and uses of that profile
768 * by any aa_task_cxt. If the profile does not exist on the profile list
769 * it is added.
770 *
771 * Returns: size of data consumed else error code on failure.
772 */
John Johansen73688d12017-01-16 00:42:34 -0800773ssize_t aa_replace_profiles(struct aa_ns *view, void *udata, size_t size,
774 bool noreplace)
John Johansenc88d4c72010-07-29 14:48:00 -0700775{
John Johansenbf15cf02016-04-16 14:16:50 -0700776 const char *ns_name, *info = NULL;
John Johansen98849df2017-01-16 00:42:16 -0800777 struct aa_ns *ns = NULL;
John Johansendd51c8482013-07-10 21:05:43 -0700778 struct aa_load_ent *ent, *tmp;
John Johansenc88d4c72010-07-29 14:48:00 -0700779 int op = OP_PROF_REPL;
780 ssize_t error;
John Johansendd51c8482013-07-10 21:05:43 -0700781 LIST_HEAD(lh);
John Johansenc88d4c72010-07-29 14:48:00 -0700782
783 /* released below */
John Johansendd51c8482013-07-10 21:05:43 -0700784 error = aa_unpack(udata, size, &lh, &ns_name);
785 if (error)
786 goto out;
John Johansenc88d4c72010-07-29 14:48:00 -0700787
788 /* released below */
John Johansen73688d12017-01-16 00:42:34 -0800789 ns = aa_prepare_ns(view, ns_name);
John Johansenc88d4c72010-07-29 14:48:00 -0700790 if (!ns) {
John Johansenbf15cf02016-04-16 14:16:50 -0700791 error = audit_policy(op, GFP_KERNEL, ns_name,
792 "failed to prepare namespace", -ENOMEM);
793 goto free;
John Johansenc88d4c72010-07-29 14:48:00 -0700794 }
795
John Johansen01e2b672013-07-10 21:06:43 -0700796 mutex_lock(&ns->lock);
John Johansendd51c8482013-07-10 21:05:43 -0700797 /* setup parent and ns info */
798 list_for_each_entry(ent, &lh, list) {
799 struct aa_policy *policy;
John Johansendd51c8482013-07-10 21:05:43 -0700800 error = __lookup_replace(ns, ent->new->base.hname, noreplace,
801 &ent->old, &info);
802 if (error)
803 goto fail_lock;
John Johansenc88d4c72010-07-29 14:48:00 -0700804
John Johansendd51c8482013-07-10 21:05:43 -0700805 if (ent->new->rename) {
806 error = __lookup_replace(ns, ent->new->rename,
807 noreplace, &ent->rename,
808 &info);
809 if (error)
810 goto fail_lock;
John Johansenc88d4c72010-07-29 14:48:00 -0700811 }
John Johansendd51c8482013-07-10 21:05:43 -0700812
813 /* released when @new is freed */
John Johansen98849df2017-01-16 00:42:16 -0800814 ent->new->ns = aa_get_ns(ns);
John Johansendd51c8482013-07-10 21:05:43 -0700815
816 if (ent->old || ent->rename)
817 continue;
818
819 /* no ref on policy only use inside lock */
820 policy = __lookup_parent(ns, ent->new->base.hname);
821 if (!policy) {
822 struct aa_profile *p;
823 p = __list_lookup_parent(&lh, ent->new);
824 if (!p) {
825 error = -ENOENT;
826 info = "parent does not exist";
John Johansendd51c8482013-07-10 21:05:43 -0700827 goto fail_lock;
828 }
John Johansen01e2b672013-07-10 21:06:43 -0700829 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
830 } else if (policy != &ns->base) {
John Johansendd51c8482013-07-10 21:05:43 -0700831 /* released on profile replacement or free_profile */
John Johansen01e2b672013-07-10 21:06:43 -0700832 struct aa_profile *p = (struct aa_profile *) policy;
833 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
834 }
John Johansenc88d4c72010-07-29 14:48:00 -0700835 }
836
John Johansen0d259f02013-07-10 21:13:43 -0700837 /* create new fs entries for introspection if needed */
838 list_for_each_entry(ent, &lh, list) {
839 if (ent->old) {
840 /* inherit old interface files */
841
842 /* if (ent->rename)
843 TODO: support rename */
844 /* } else if (ent->rename) {
845 TODO: support rename */
846 } else {
847 struct dentry *parent;
848 if (rcu_access_pointer(ent->new->parent)) {
849 struct aa_profile *p;
850 p = aa_deref_parent(ent->new);
851 parent = prof_child_dir(p);
852 } else
853 parent = ns_subprofs_dir(ent->new->ns);
854 error = __aa_fs_profile_mkdir(ent->new, parent);
855 }
856
857 if (error) {
858 info = "failed to create ";
859 goto fail_lock;
860 }
861 }
862
863 /* Done with checks that may fail - do actual replacement */
John Johansendd51c8482013-07-10 21:05:43 -0700864 list_for_each_entry_safe(ent, tmp, &lh, list) {
865 list_del_init(&ent->list);
866 op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
John Johansenc88d4c72010-07-29 14:48:00 -0700867
John Johansen7ee6da22016-04-16 14:19:38 -0700868 audit_policy(op, GFP_ATOMIC, ent->new->base.hname, NULL, error);
John Johansenc88d4c72010-07-29 14:48:00 -0700869
John Johansendd51c8482013-07-10 21:05:43 -0700870 if (ent->old) {
John Johansen77b071b2013-07-10 21:07:43 -0700871 __replace_profile(ent->old, ent->new, 1);
John Johansen0d259f02013-07-10 21:13:43 -0700872 if (ent->rename) {
John Johansen83995882017-01-16 00:42:19 -0800873 /* aafs interface uses proxy */
874 struct aa_proxy *r = ent->new->proxy;
John Johansen0d259f02013-07-10 21:13:43 -0700875 rcu_assign_pointer(r->profile,
876 aa_get_profile(ent->new));
John Johansen77b071b2013-07-10 21:07:43 -0700877 __replace_profile(ent->rename, ent->new, 0);
John Johansen0d259f02013-07-10 21:13:43 -0700878 }
John Johansendd51c8482013-07-10 21:05:43 -0700879 } else if (ent->rename) {
John Johansen83995882017-01-16 00:42:19 -0800880 /* aafs interface uses proxy */
881 rcu_assign_pointer(ent->new->proxy->profile,
John Johansen0d259f02013-07-10 21:13:43 -0700882 aa_get_profile(ent->new));
John Johansen77b071b2013-07-10 21:07:43 -0700883 __replace_profile(ent->rename, ent->new, 0);
John Johansendd51c8482013-07-10 21:05:43 -0700884 } else if (ent->new->parent) {
John Johansen01e2b672013-07-10 21:06:43 -0700885 struct aa_profile *parent, *newest;
John Johansen0d259f02013-07-10 21:13:43 -0700886 parent = aa_deref_parent(ent->new);
John Johansen77b071b2013-07-10 21:07:43 -0700887 newest = aa_get_newest_profile(parent);
John Johansen01e2b672013-07-10 21:06:43 -0700888
John Johansendd51c8482013-07-10 21:05:43 -0700889 /* parent replaced in this atomic set? */
John Johansen01e2b672013-07-10 21:06:43 -0700890 if (newest != parent) {
891 aa_get_profile(newest);
John Johansen01e2b672013-07-10 21:06:43 -0700892 rcu_assign_pointer(ent->new->parent, newest);
John Johansenf3518412016-04-16 13:59:02 -0700893 aa_put_profile(parent);
John Johansendcda6172016-04-11 16:55:10 -0700894 }
John Johansen83995882017-01-16 00:42:19 -0800895 /* aafs interface uses proxy */
896 rcu_assign_pointer(ent->new->proxy->profile,
John Johansen0d259f02013-07-10 21:13:43 -0700897 aa_get_profile(ent->new));
John Johansenec34fa22016-04-11 16:57:19 -0700898 __list_add_profile(&newest->base.profiles, ent->new);
John Johansendcda6172016-04-11 16:55:10 -0700899 aa_put_profile(newest);
John Johansen0d259f02013-07-10 21:13:43 -0700900 } else {
John Johansen83995882017-01-16 00:42:19 -0800901 /* aafs interface uses proxy */
902 rcu_assign_pointer(ent->new->proxy->profile,
John Johansen0d259f02013-07-10 21:13:43 -0700903 aa_get_profile(ent->new));
John Johansendd51c8482013-07-10 21:05:43 -0700904 __list_add_profile(&ns->base.profiles, ent->new);
John Johansen0d259f02013-07-10 21:13:43 -0700905 }
John Johansendd51c8482013-07-10 21:05:43 -0700906 aa_load_ent_free(ent);
John Johansenc88d4c72010-07-29 14:48:00 -0700907 }
John Johansen01e2b672013-07-10 21:06:43 -0700908 mutex_unlock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -0700909
910out:
John Johansen98849df2017-01-16 00:42:16 -0800911 aa_put_ns(ns);
John Johansendd51c8482013-07-10 21:05:43 -0700912
John Johansenc88d4c72010-07-29 14:48:00 -0700913 if (error)
914 return error;
915 return size;
916
John Johansendd51c8482013-07-10 21:05:43 -0700917fail_lock:
John Johansen01e2b672013-07-10 21:06:43 -0700918 mutex_unlock(&ns->lock);
John Johansendd51c8482013-07-10 21:05:43 -0700919
John Johansenbf15cf02016-04-16 14:16:50 -0700920 /* audit cause of failure */
921 op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
922 audit_policy(op, GFP_KERNEL, ent->new->base.hname, info, error);
923 /* audit status that rest of profiles in the atomic set failed too */
924 info = "valid profile in failed atomic policy load";
925 list_for_each_entry(tmp, &lh, list) {
926 if (tmp == ent) {
927 info = "unchecked profile in failed atomic policy load";
928 /* skip entry that caused failure */
929 continue;
930 }
931 op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
932 audit_policy(op, GFP_KERNEL, tmp->new->base.hname, info, error);
933 }
934free:
John Johansendd51c8482013-07-10 21:05:43 -0700935 list_for_each_entry_safe(ent, tmp, &lh, list) {
936 list_del_init(&ent->list);
937 aa_load_ent_free(ent);
938 }
939
John Johansenc88d4c72010-07-29 14:48:00 -0700940 goto out;
941}
942
943/**
944 * aa_remove_profiles - remove profile(s) from the system
945 * @fqname: name of the profile or namespace to remove (NOT NULL)
946 * @size: size of the name
947 *
948 * Remove a profile or sub namespace from the current namespace, so that
949 * they can not be found anymore and mark them as replaced by unconfined
950 *
951 * NOTE: removing confinement does not restore rlimits to preconfinemnet values
952 *
953 * Returns: size of data consume else error code if fails
954 */
955ssize_t aa_remove_profiles(char *fqname, size_t size)
956{
John Johansen98849df2017-01-16 00:42:16 -0800957 struct aa_ns *root, *ns = NULL;
John Johansenc88d4c72010-07-29 14:48:00 -0700958 struct aa_profile *profile = NULL;
959 const char *name = fqname, *info = NULL;
960 ssize_t error = 0;
961
962 if (*fqname == 0) {
963 info = "no profile specified";
964 error = -ENOENT;
965 goto fail;
966 }
967
968 root = aa_current_profile()->ns;
969
970 if (fqname[0] == ':') {
971 char *ns_name;
972 name = aa_split_fqname(fqname, &ns_name);
John Johansen41d1b3e2013-02-21 01:14:17 -0800973 /* released below */
John Johansen98849df2017-01-16 00:42:16 -0800974 ns = aa_find_ns(root, ns_name);
John Johansen41d1b3e2013-02-21 01:14:17 -0800975 if (!ns) {
976 info = "namespace does not exist";
977 error = -ENOENT;
978 goto fail;
John Johansenc88d4c72010-07-29 14:48:00 -0700979 }
980 } else
981 /* released below */
John Johansen98849df2017-01-16 00:42:16 -0800982 ns = aa_get_ns(root);
John Johansenc88d4c72010-07-29 14:48:00 -0700983
John Johansenc88d4c72010-07-29 14:48:00 -0700984 if (!name) {
985 /* remove namespace - can only happen if fqname[0] == ':' */
John Johansen01e2b672013-07-10 21:06:43 -0700986 mutex_lock(&ns->parent->lock);
John Johansen98849df2017-01-16 00:42:16 -0800987 __aa_remove_ns(ns);
John Johansen01e2b672013-07-10 21:06:43 -0700988 mutex_unlock(&ns->parent->lock);
John Johansenc88d4c72010-07-29 14:48:00 -0700989 } else {
990 /* remove profile */
John Johansen01e2b672013-07-10 21:06:43 -0700991 mutex_lock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -0700992 profile = aa_get_profile(__lookup_profile(&ns->base, name));
993 if (!profile) {
994 error = -ENOENT;
995 info = "profile does not exist";
996 goto fail_ns_lock;
997 }
998 name = profile->base.hname;
999 __remove_profile(profile);
John Johansen01e2b672013-07-10 21:06:43 -07001000 mutex_unlock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -07001001 }
John Johansenc88d4c72010-07-29 14:48:00 -07001002
1003 /* don't fail removal if audit fails */
1004 (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error);
John Johansen98849df2017-01-16 00:42:16 -08001005 aa_put_ns(ns);
John Johansenc88d4c72010-07-29 14:48:00 -07001006 aa_put_profile(profile);
1007 return size;
1008
1009fail_ns_lock:
John Johansen01e2b672013-07-10 21:06:43 -07001010 mutex_unlock(&ns->lock);
John Johansen98849df2017-01-16 00:42:16 -08001011 aa_put_ns(ns);
John Johansenc88d4c72010-07-29 14:48:00 -07001012
1013fail:
1014 (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error);
1015 return error;
1016}