blob: 0ceee967434caeacc8f394dafe09e55b7fa9c2c3 [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"
88#include "include/policy_unpack.h"
89#include "include/resource.h"
John Johansenc88d4c72010-07-29 14:48:00 -070090
91
92/* root profile namespace */
93struct aa_namespace *root_ns;
94
Jan Engelhardt2d4cee72012-03-14 13:30:36 +010095const char *const profile_mode_names[] = {
John Johansenc88d4c72010-07-29 14:48:00 -070096 "enforce",
97 "complain",
98 "kill",
99};
100
101/**
102 * hname_tail - find the last component of an hname
103 * @name: hname to find the base profile name component of (NOT NULL)
104 *
105 * Returns: the tail (base profile name) name component of an hname
106 */
107static const char *hname_tail(const char *hname)
108{
109 char *split;
110 hname = strim((char *)hname);
111 for (split = strstr(hname, "//"); split; split = strstr(hname, "//"))
112 hname = split + 2;
113
114 return hname;
115}
116
117/**
118 * policy_init - initialize a policy structure
119 * @policy: policy to initialize (NOT NULL)
120 * @prefix: prefix name if any is required. (MAYBE NULL)
121 * @name: name of the policy, init will make a copy of it (NOT NULL)
122 *
123 * Note: this fn creates a copy of strings passed in
124 *
125 * Returns: true if policy init successful
126 */
127static bool policy_init(struct aa_policy *policy, const char *prefix,
128 const char *name)
129{
130 /* freed by policy_free */
131 if (prefix) {
132 policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3,
133 GFP_KERNEL);
134 if (policy->hname)
135 sprintf(policy->hname, "%s//%s", prefix, name);
136 } else
137 policy->hname = kstrdup(name, GFP_KERNEL);
138 if (!policy->hname)
139 return 0;
140 /* base.name is a substring of fqname */
141 policy->name = (char *)hname_tail(policy->hname);
142 INIT_LIST_HEAD(&policy->list);
143 INIT_LIST_HEAD(&policy->profiles);
John Johansenc88d4c72010-07-29 14:48:00 -0700144
145 return 1;
146}
147
148/**
149 * policy_destroy - free the elements referenced by @policy
150 * @policy: policy that is to have its elements freed (NOT NULL)
151 */
152static void policy_destroy(struct aa_policy *policy)
153{
154 /* still contains profiles -- invalid */
John Johansen01e2b672013-07-10 21:06:43 -0700155 if (on_list_rcu(&policy->profiles)) {
John Johansenc88d4c72010-07-29 14:48:00 -0700156 AA_ERROR("%s: internal error, "
157 "policy '%s' still contains profiles\n",
158 __func__, policy->name);
159 BUG();
160 }
John Johansen01e2b672013-07-10 21:06:43 -0700161 if (on_list_rcu(&policy->list)) {
John Johansenc88d4c72010-07-29 14:48:00 -0700162 AA_ERROR("%s: internal error, policy '%s' still on list\n",
163 __func__, policy->name);
164 BUG();
165 }
166
167 /* don't free name as its a subset of hname */
168 kzfree(policy->hname);
169}
170
171/**
172 * __policy_find - find a policy by @name on a policy list
173 * @head: list to search (NOT NULL)
174 * @name: name to search for (NOT NULL)
175 *
John Johansen01e2b672013-07-10 21:06:43 -0700176 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700177 *
178 * Returns: unrefcounted policy that match @name or NULL if not found
179 */
180static struct aa_policy *__policy_find(struct list_head *head, const char *name)
181{
182 struct aa_policy *policy;
183
John Johansen01e2b672013-07-10 21:06:43 -0700184 list_for_each_entry_rcu(policy, head, list) {
John Johansenc88d4c72010-07-29 14:48:00 -0700185 if (!strcmp(policy->name, name))
186 return policy;
187 }
188 return NULL;
189}
190
191/**
192 * __policy_strn_find - find a policy that's name matches @len chars of @str
193 * @head: list to search (NOT NULL)
194 * @str: string to search for (NOT NULL)
195 * @len: length of match required
196 *
John Johansen01e2b672013-07-10 21:06:43 -0700197 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700198 *
199 * Returns: unrefcounted policy that match @str or NULL if not found
200 *
201 * if @len == strlen(@strlen) then this is equiv to __policy_find
202 * other wise it allows searching for policy by a partial match of name
203 */
204static struct aa_policy *__policy_strn_find(struct list_head *head,
205 const char *str, int len)
206{
207 struct aa_policy *policy;
208
John Johansen01e2b672013-07-10 21:06:43 -0700209 list_for_each_entry_rcu(policy, head, list) {
John Johansenc88d4c72010-07-29 14:48:00 -0700210 if (aa_strneq(policy->name, str, len))
211 return policy;
212 }
213
214 return NULL;
215}
216
217/*
218 * Routines for AppArmor namespaces
219 */
220
221static const char *hidden_ns_name = "---";
222/**
223 * aa_ns_visible - test if @view is visible from @curr
224 * @curr: namespace to treat as the parent (NOT NULL)
225 * @view: namespace to test if visible from @curr (NOT NULL)
226 *
227 * Returns: true if @view is visible from @curr else false
228 */
229bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view)
230{
231 if (curr == view)
232 return true;
233
234 for ( ; view; view = view->parent) {
235 if (view->parent == curr)
236 return true;
237 }
238 return false;
239}
240
241/**
242 * aa_na_name - Find the ns name to display for @view from @curr
243 * @curr - current namespace (NOT NULL)
244 * @view - namespace attempting to view (NOT NULL)
245 *
246 * Returns: name of @view visible from @curr
247 */
248const char *aa_ns_name(struct aa_namespace *curr, struct aa_namespace *view)
249{
250 /* if view == curr then the namespace name isn't displayed */
251 if (curr == view)
252 return "";
253
254 if (aa_ns_visible(curr, view)) {
255 /* at this point if a ns is visible it is in a view ns
256 * thus the curr ns.hname is a prefix of its name.
257 * Only output the virtualized portion of the name
258 * Add + 2 to skip over // separating curr hname prefix
259 * from the visible tail of the views hname
260 */
261 return view->base.hname + strlen(curr->base.hname) + 2;
262 } else
263 return hidden_ns_name;
264}
265
266/**
267 * alloc_namespace - allocate, initialize and return a new namespace
268 * @prefix: parent namespace name (MAYBE NULL)
269 * @name: a preallocated name (NOT NULL)
270 *
271 * Returns: refcounted namespace or NULL on failure.
272 */
273static struct aa_namespace *alloc_namespace(const char *prefix,
274 const char *name)
275{
276 struct aa_namespace *ns;
277
278 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
279 AA_DEBUG("%s(%p)\n", __func__, ns);
280 if (!ns)
281 return NULL;
282 if (!policy_init(&ns->base, prefix, name))
283 goto fail_ns;
284
285 INIT_LIST_HEAD(&ns->sub_ns);
John Johansen01e2b672013-07-10 21:06:43 -0700286 mutex_init(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -0700287
288 /* released by free_namespace */
289 ns->unconfined = aa_alloc_profile("unconfined");
290 if (!ns->unconfined)
291 goto fail_unconfined;
292
John Johansenc88d4c72010-07-29 14:48:00 -0700293 ns->unconfined->flags = PFLAG_UNCONFINED | PFLAG_IX_ON_NAME_ERROR |
John Johansenfa2ac462013-07-10 21:08:43 -0700294 PFLAG_IMMUTABLE | PFLAG_NS_COUNT;
John Johansenc88d4c72010-07-29 14:48:00 -0700295
John Johansenfa2ac462013-07-10 21:08:43 -0700296 /* ns and ns->unconfined share ns->unconfined refcount */
297 ns->unconfined->ns = ns;
John Johansenc88d4c72010-07-29 14:48:00 -0700298
John Johansena4987852013-02-18 16:10:34 -0800299 atomic_set(&ns->uniq_null, 0);
300
John Johansenc88d4c72010-07-29 14:48:00 -0700301 return ns;
302
303fail_unconfined:
wzt.wzt@gmail.com246c3fb2010-11-10 11:31:55 +0800304 kzfree(ns->base.hname);
John Johansenc88d4c72010-07-29 14:48:00 -0700305fail_ns:
306 kzfree(ns);
307 return NULL;
308}
309
John Johansenfa2ac462013-07-10 21:08:43 -0700310static void free_profile(struct aa_profile *profile);
John Johansenc88d4c72010-07-29 14:48:00 -0700311/**
312 * free_namespace - free a profile namespace
313 * @ns: the namespace to free (MAYBE NULL)
314 *
315 * Requires: All references to the namespace must have been put, if the
316 * namespace was referenced by a profile confining a task,
317 */
318static void free_namespace(struct aa_namespace *ns)
319{
320 if (!ns)
321 return;
322
323 policy_destroy(&ns->base);
324 aa_put_namespace(ns->parent);
325
John Johansenfa2ac462013-07-10 21:08:43 -0700326 ns->unconfined->ns = NULL;
327 free_profile(ns->unconfined);
John Johansenc88d4c72010-07-29 14:48:00 -0700328 kzfree(ns);
329}
330
331/**
John Johansenfa2ac462013-07-10 21:08:43 -0700332 * aa_free_namespace_rcu - free aa_namespace by rcu
333 * @head: rcu_head callback for freeing of a profile (NOT NULL)
334 *
335 * rcu_head is to the unconfined profile associated with the namespace
336 */
337static void aa_free_namespace_rcu(struct rcu_head *head)
338{
339 struct aa_profile *p = container_of(head, struct aa_profile, base.rcu);
340 free_namespace(p->ns);
341}
342
343/**
John Johansenc88d4c72010-07-29 14:48:00 -0700344 * aa_free_namespace_kref - free aa_namespace by kref (see aa_put_namespace)
345 * @kr: kref callback for freeing of a namespace (NOT NULL)
John Johansenfa2ac462013-07-10 21:08:43 -0700346 *
347 * kref is to the unconfined profile associated with the namespace
John Johansenc88d4c72010-07-29 14:48:00 -0700348 */
349void aa_free_namespace_kref(struct kref *kref)
350{
John Johansenfa2ac462013-07-10 21:08:43 -0700351 struct aa_profile *p = container_of(kref, struct aa_profile, count);
352 call_rcu(&p->base.rcu, aa_free_namespace_rcu);
John Johansenc88d4c72010-07-29 14:48:00 -0700353}
354
355/**
356 * __aa_find_namespace - find a namespace on a list by @name
357 * @head: list to search for namespace on (NOT NULL)
358 * @name: name of namespace to look for (NOT NULL)
359 *
360 * Returns: unrefcounted namespace
361 *
John Johansen01e2b672013-07-10 21:06:43 -0700362 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700363 */
364static struct aa_namespace *__aa_find_namespace(struct list_head *head,
365 const char *name)
366{
367 return (struct aa_namespace *)__policy_find(head, name);
368}
369
370/**
371 * aa_find_namespace - look up a profile namespace on the namespace list
372 * @root: namespace to search in (NOT NULL)
373 * @name: name of namespace to find (NOT NULL)
374 *
375 * Returns: a refcounted namespace on the list, or NULL if no namespace
376 * called @name exists.
377 *
378 * refcount released by caller
379 */
380struct aa_namespace *aa_find_namespace(struct aa_namespace *root,
381 const char *name)
382{
383 struct aa_namespace *ns = NULL;
384
John Johansen01e2b672013-07-10 21:06:43 -0700385 rcu_read_lock();
John Johansenc88d4c72010-07-29 14:48:00 -0700386 ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name));
John Johansen01e2b672013-07-10 21:06:43 -0700387 rcu_read_unlock();
John Johansenc88d4c72010-07-29 14:48:00 -0700388
389 return ns;
390}
391
392/**
393 * aa_prepare_namespace - find an existing or create a new namespace of @name
394 * @name: the namespace to find or add (MAYBE NULL)
395 *
396 * Returns: refcounted namespace or NULL if failed to create one
397 */
398static struct aa_namespace *aa_prepare_namespace(const char *name)
399{
400 struct aa_namespace *ns, *root;
401
402 root = aa_current_profile()->ns;
403
John Johansen01e2b672013-07-10 21:06:43 -0700404 mutex_lock(&root->lock);
John Johansenc88d4c72010-07-29 14:48:00 -0700405
406 /* if name isn't specified the profile is loaded to the current ns */
407 if (!name) {
408 /* released by caller */
409 ns = aa_get_namespace(root);
410 goto out;
411 }
412
413 /* try and find the specified ns and if it doesn't exist create it */
414 /* released by caller */
415 ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name));
416 if (!ns) {
John Johansen01e2b672013-07-10 21:06:43 -0700417 ns = alloc_namespace(root->base.hname, name);
418 if (!ns)
419 goto out;
420 /* add parent ref */
421 ns->parent = aa_get_namespace(root);
422 list_add_rcu(&ns->base.list, &root->sub_ns);
423 /* add list ref */
424 aa_get_namespace(ns);
John Johansenc88d4c72010-07-29 14:48:00 -0700425 }
426out:
John Johansen01e2b672013-07-10 21:06:43 -0700427 mutex_unlock(&root->lock);
John Johansenc88d4c72010-07-29 14:48:00 -0700428
429 /* return ref */
430 return ns;
431}
432
433/**
434 * __list_add_profile - add a profile to a list
435 * @list: list to add it to (NOT NULL)
436 * @profile: the profile to add (NOT NULL)
437 *
438 * refcount @profile, should be put by __list_remove_profile
439 *
440 * Requires: namespace lock be held, or list not be shared
441 */
442static void __list_add_profile(struct list_head *list,
443 struct aa_profile *profile)
444{
John Johansen01e2b672013-07-10 21:06:43 -0700445 list_add_rcu(&profile->base.list, list);
John Johansenc88d4c72010-07-29 14:48:00 -0700446 /* get list reference */
447 aa_get_profile(profile);
448}
449
450/**
451 * __list_remove_profile - remove a profile from the list it is on
452 * @profile: the profile to remove (NOT NULL)
453 *
454 * remove a profile from the list, warning generally removal should
455 * be done with __replace_profile as most profile removals are
456 * replacements to the unconfined profile.
457 *
458 * put @profile list refcount
459 *
460 * Requires: namespace lock be held, or list not have been live
461 */
462static void __list_remove_profile(struct aa_profile *profile)
463{
John Johansen01e2b672013-07-10 21:06:43 -0700464 list_del_rcu(&profile->base.list);
465 aa_put_profile(profile);
John Johansenc88d4c72010-07-29 14:48:00 -0700466}
467
John Johansenc88d4c72010-07-29 14:48:00 -0700468static void __profile_list_release(struct list_head *head);
469
470/**
471 * __remove_profile - remove old profile, and children
472 * @profile: profile to be replaced (NOT NULL)
473 *
474 * Requires: namespace list lock be held, or list not be shared
475 */
476static void __remove_profile(struct aa_profile *profile)
477{
478 /* release any children lists first */
479 __profile_list_release(&profile->base.profiles);
480 /* released by free_profile */
John Johansen77b071b2013-07-10 21:07:43 -0700481 __aa_update_replacedby(profile, profile->ns->unconfined);
John Johansenc88d4c72010-07-29 14:48:00 -0700482 __list_remove_profile(profile);
483}
484
485/**
486 * __profile_list_release - remove all profiles on the list and put refs
487 * @head: list of profiles (NOT NULL)
488 *
489 * Requires: namespace lock be held
490 */
491static void __profile_list_release(struct list_head *head)
492{
493 struct aa_profile *profile, *tmp;
494 list_for_each_entry_safe(profile, tmp, head, base.list)
495 __remove_profile(profile);
496}
497
498static void __ns_list_release(struct list_head *head);
499
500/**
501 * destroy_namespace - remove everything contained by @ns
502 * @ns: namespace to have it contents removed (NOT NULL)
503 */
504static void destroy_namespace(struct aa_namespace *ns)
505{
506 if (!ns)
507 return;
508
John Johansen01e2b672013-07-10 21:06:43 -0700509 mutex_lock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -0700510 /* release all profiles in this namespace */
511 __profile_list_release(&ns->base.profiles);
512
513 /* release all sub namespaces */
514 __ns_list_release(&ns->sub_ns);
515
John Johansen01e2b672013-07-10 21:06:43 -0700516 if (ns->parent)
John Johansenfa2ac462013-07-10 21:08:43 -0700517 __aa_update_replacedby(ns->unconfined, ns->parent->unconfined);
John Johansen01e2b672013-07-10 21:06:43 -0700518 mutex_unlock(&ns->lock);
519}
520
John Johansenc88d4c72010-07-29 14:48:00 -0700521/**
522 * __remove_namespace - remove a namespace and all its children
523 * @ns: namespace to be removed (NOT NULL)
524 *
525 * Requires: ns->parent->lock be held and ns removed from parent.
526 */
527static void __remove_namespace(struct aa_namespace *ns)
528{
John Johansenc88d4c72010-07-29 14:48:00 -0700529 /* remove ns from namespace list */
John Johansen01e2b672013-07-10 21:06:43 -0700530 list_del_rcu(&ns->base.list);
John Johansenc88d4c72010-07-29 14:48:00 -0700531 destroy_namespace(ns);
John Johansenfa2ac462013-07-10 21:08:43 -0700532 aa_put_namespace(ns);
John Johansenc88d4c72010-07-29 14:48:00 -0700533}
534
535/**
536 * __ns_list_release - remove all profile namespaces on the list put refs
537 * @head: list of profile namespaces (NOT NULL)
538 *
539 * Requires: namespace lock be held
540 */
541static void __ns_list_release(struct list_head *head)
542{
543 struct aa_namespace *ns, *tmp;
544 list_for_each_entry_safe(ns, tmp, head, base.list)
545 __remove_namespace(ns);
546
547}
548
549/**
550 * aa_alloc_root_ns - allocate the root profile namespace
551 *
552 * Returns: %0 on success else error
553 *
554 */
555int __init aa_alloc_root_ns(void)
556{
557 /* released by aa_free_root_ns - used as list ref*/
558 root_ns = alloc_namespace(NULL, "root");
559 if (!root_ns)
560 return -ENOMEM;
561
562 return 0;
563}
564
565 /**
566 * aa_free_root_ns - free the root profile namespace
567 */
568void __init aa_free_root_ns(void)
569 {
570 struct aa_namespace *ns = root_ns;
571 root_ns = NULL;
572
573 destroy_namespace(ns);
574 aa_put_namespace(ns);
575}
576
John Johansen77b071b2013-07-10 21:07:43 -0700577
578static void free_replacedby(struct aa_replacedby *r)
579{
580 if (r) {
581 aa_put_profile(rcu_dereference(r->profile));
582 kzfree(r);
583 }
584}
585
586
587void aa_free_replacedby_kref(struct kref *kref)
588{
589 struct aa_replacedby *r = container_of(kref, struct aa_replacedby,
590 count);
591 free_replacedby(r);
592}
593
John Johansenc88d4c72010-07-29 14:48:00 -0700594/**
John Johansenc88d4c72010-07-29 14:48:00 -0700595 * free_profile - free a profile
596 * @profile: the profile to free (MAYBE NULL)
597 *
598 * Free a profile, its hats and null_profile. All references to the profile,
599 * its hats and null_profile must have been put.
600 *
601 * If the profile was referenced from a task context, free_profile() will
602 * be called from an rcu callback routine, so we must not sleep here.
603 */
604static void free_profile(struct aa_profile *profile)
605{
606 AA_DEBUG("%s(%p)\n", __func__, profile);
607
608 if (!profile)
609 return;
610
John Johansenc88d4c72010-07-29 14:48:00 -0700611 /* free children profiles */
612 policy_destroy(&profile->base);
John Johansen01e2b672013-07-10 21:06:43 -0700613 aa_put_profile(rcu_access_pointer(profile->parent));
John Johansenc88d4c72010-07-29 14:48:00 -0700614
615 aa_put_namespace(profile->ns);
616 kzfree(profile->rename);
617
618 aa_free_file_rules(&profile->file);
619 aa_free_cap_rules(&profile->caps);
620 aa_free_rlimit_rules(&profile->rlimits);
621
John Johansenc88d4c72010-07-29 14:48:00 -0700622 aa_put_dfa(profile->xmatch);
John Johansenad5ff3d2012-02-16 07:07:53 -0800623 aa_put_dfa(profile->policy.dfa);
John Johansen77b071b2013-07-10 21:07:43 -0700624 aa_put_replacedby(profile->replacedby);
John Johansenc88d4c72010-07-29 14:48:00 -0700625
626 kzfree(profile);
627}
628
629/**
John Johansen01e2b672013-07-10 21:06:43 -0700630 * aa_free_profile_rcu - free aa_profile by rcu (called by aa_free_profile_kref)
631 * @head: rcu_head callback for freeing of a profile (NOT NULL)
632 */
633static void aa_free_profile_rcu(struct rcu_head *head)
634{
635 struct aa_profile *p = container_of(head, struct aa_profile, base.rcu);
636 free_profile(p);
637}
638
639/**
John Johansenc88d4c72010-07-29 14:48:00 -0700640 * aa_free_profile_kref - free aa_profile by kref (called by aa_put_profile)
641 * @kr: kref callback for freeing of a profile (NOT NULL)
642 */
643void aa_free_profile_kref(struct kref *kref)
644{
John Johansenfa2ac462013-07-10 21:08:43 -0700645 struct aa_profile *p = container_of(kref, struct aa_profile, count);
John Johansen01e2b672013-07-10 21:06:43 -0700646 call_rcu(&p->base.rcu, aa_free_profile_rcu);
John Johansenc88d4c72010-07-29 14:48:00 -0700647}
648
John Johansen4da05cc2013-02-18 16:11:34 -0800649/**
650 * aa_alloc_profile - allocate, initialize and return a new profile
651 * @hname: name of the profile (NOT NULL)
652 *
653 * Returns: refcount profile or NULL on failure
654 */
655struct aa_profile *aa_alloc_profile(const char *hname)
656{
657 struct aa_profile *profile;
658
659 /* freed by free_profile - usually through aa_put_profile */
660 profile = kzalloc(sizeof(*profile), GFP_KERNEL);
661 if (!profile)
662 return NULL;
663
John Johansen77b071b2013-07-10 21:07:43 -0700664 profile->replacedby = kzalloc(sizeof(struct aa_replacedby), GFP_KERNEL);
665 if (!profile->replacedby)
666 goto fail;
667 kref_init(&profile->replacedby->count);
668
669 if (!policy_init(&profile->base, NULL, hname))
670 goto fail;
John Johansenfa2ac462013-07-10 21:08:43 -0700671 kref_init(&profile->count);
John Johansen4da05cc2013-02-18 16:11:34 -0800672
673 /* refcount released by caller */
674 return profile;
John Johansen77b071b2013-07-10 21:07:43 -0700675
676fail:
677 kzfree(profile->replacedby);
678 kzfree(profile);
679
680 return NULL;
John Johansen4da05cc2013-02-18 16:11:34 -0800681}
682
683/**
684 * aa_new_null_profile - create a new null-X learning profile
685 * @parent: profile that caused this profile to be created (NOT NULL)
686 * @hat: true if the null- learning profile is a hat
687 *
688 * Create a null- complain mode profile used in learning mode. The name of
689 * the profile is unique and follows the format of parent//null-<uniq>.
690 *
691 * null profiles are added to the profile list but the list does not
692 * hold a count on them so that they are automatically released when
693 * not in use.
694 *
695 * Returns: new refcounted profile else NULL on failure
696 */
697struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat)
698{
699 struct aa_profile *profile = NULL;
700 char *name;
701 int uniq = atomic_inc_return(&parent->ns->uniq_null);
702
703 /* freed below */
704 name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, GFP_KERNEL);
705 if (!name)
706 goto fail;
707 sprintf(name, "%s//null-%x", parent->base.hname, uniq);
708
709 profile = aa_alloc_profile(name);
710 kfree(name);
711 if (!profile)
712 goto fail;
713
714 profile->mode = APPARMOR_COMPLAIN;
715 profile->flags = PFLAG_NULL;
716 if (hat)
717 profile->flags |= PFLAG_HAT;
718
719 /* released on free_profile */
John Johansen01e2b672013-07-10 21:06:43 -0700720 rcu_assign_pointer(profile->parent, aa_get_profile(parent));
John Johansen4da05cc2013-02-18 16:11:34 -0800721 profile->ns = aa_get_namespace(parent->ns);
722
John Johansen01e2b672013-07-10 21:06:43 -0700723 mutex_lock(&profile->ns->lock);
John Johansen4da05cc2013-02-18 16:11:34 -0800724 __list_add_profile(&parent->base.profiles, profile);
John Johansen01e2b672013-07-10 21:06:43 -0700725 mutex_unlock(&profile->ns->lock);
John Johansen4da05cc2013-02-18 16:11:34 -0800726
727 /* refcount released by caller */
728 return profile;
729
730fail:
731 return NULL;
732}
733
John Johansenc88d4c72010-07-29 14:48:00 -0700734/* TODO: profile accounting - setup in remove */
735
736/**
737 * __find_child - find a profile on @head list with a name matching @name
738 * @head: list to search (NOT NULL)
739 * @name: name of profile (NOT NULL)
740 *
John Johansen01e2b672013-07-10 21:06:43 -0700741 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700742 *
743 * Returns: unrefcounted profile ptr, or NULL if not found
744 */
745static struct aa_profile *__find_child(struct list_head *head, const char *name)
746{
747 return (struct aa_profile *)__policy_find(head, name);
748}
749
750/**
751 * __strn_find_child - find a profile on @head list using substring of @name
752 * @head: list to search (NOT NULL)
753 * @name: name of profile (NOT NULL)
754 * @len: length of @name substring to match
755 *
John Johansen01e2b672013-07-10 21:06:43 -0700756 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700757 *
758 * Returns: unrefcounted profile ptr, or NULL if not found
759 */
760static struct aa_profile *__strn_find_child(struct list_head *head,
761 const char *name, int len)
762{
763 return (struct aa_profile *)__policy_strn_find(head, name, len);
764}
765
766/**
767 * aa_find_child - find a profile by @name in @parent
768 * @parent: profile to search (NOT NULL)
769 * @name: profile name to search for (NOT NULL)
770 *
771 * Returns: a refcounted profile or NULL if not found
772 */
773struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
774{
775 struct aa_profile *profile;
776
John Johansen01e2b672013-07-10 21:06:43 -0700777 rcu_read_lock();
John Johansenc88d4c72010-07-29 14:48:00 -0700778 profile = aa_get_profile(__find_child(&parent->base.profiles, name));
John Johansen01e2b672013-07-10 21:06:43 -0700779 rcu_read_unlock();
John Johansenc88d4c72010-07-29 14:48:00 -0700780
781 /* refcount released by caller */
782 return profile;
783}
784
785/**
786 * __lookup_parent - lookup the parent of a profile of name @hname
787 * @ns: namespace to lookup profile in (NOT NULL)
788 * @hname: hierarchical profile name to find parent of (NOT NULL)
789 *
790 * Lookups up the parent of a fully qualified profile name, the profile
791 * that matches hname does not need to exist, in general this
792 * is used to load a new profile.
793 *
John Johansen01e2b672013-07-10 21:06:43 -0700794 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700795 *
796 * Returns: unrefcounted policy or NULL if not found
797 */
798static struct aa_policy *__lookup_parent(struct aa_namespace *ns,
799 const char *hname)
800{
801 struct aa_policy *policy;
802 struct aa_profile *profile = NULL;
803 char *split;
804
805 policy = &ns->base;
806
807 for (split = strstr(hname, "//"); split;) {
808 profile = __strn_find_child(&policy->profiles, hname,
809 split - hname);
810 if (!profile)
811 return NULL;
812 policy = &profile->base;
813 hname = split + 2;
814 split = strstr(hname, "//");
815 }
816 if (!profile)
817 return &ns->base;
818 return &profile->base;
819}
820
821/**
822 * __lookup_profile - lookup the profile matching @hname
823 * @base: base list to start looking up profile name from (NOT NULL)
824 * @hname: hierarchical profile name (NOT NULL)
825 *
John Johansen01e2b672013-07-10 21:06:43 -0700826 * Requires: rcu_read_lock be held
John Johansenc88d4c72010-07-29 14:48:00 -0700827 *
828 * Returns: unrefcounted profile pointer or NULL if not found
829 *
830 * Do a relative name lookup, recursing through profile tree.
831 */
832static struct aa_profile *__lookup_profile(struct aa_policy *base,
833 const char *hname)
834{
835 struct aa_profile *profile = NULL;
836 char *split;
837
838 for (split = strstr(hname, "//"); split;) {
839 profile = __strn_find_child(&base->profiles, hname,
840 split - hname);
841 if (!profile)
842 return NULL;
843
844 base = &profile->base;
845 hname = split + 2;
846 split = strstr(hname, "//");
847 }
848
849 profile = __find_child(&base->profiles, hname);
850
851 return profile;
852}
853
854/**
855 * aa_lookup_profile - find a profile by its full or partial name
856 * @ns: the namespace to start from (NOT NULL)
857 * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL)
858 *
859 * Returns: refcounted profile or NULL if not found
860 */
861struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *hname)
862{
863 struct aa_profile *profile;
864
John Johansen01e2b672013-07-10 21:06:43 -0700865 rcu_read_lock();
866 do {
867 profile = __lookup_profile(&ns->base, hname);
868 } while (profile && !aa_get_profile_not0(profile));
869 rcu_read_unlock();
John Johansenc88d4c72010-07-29 14:48:00 -0700870
John Johansenbf832082012-05-16 11:00:05 -0700871 /* the unconfined profile is not in the regular profile list */
872 if (!profile && strcmp(hname, "unconfined") == 0)
John Johansenfa2ac462013-07-10 21:08:43 -0700873 profile = aa_get_newest_profile(ns->unconfined);
John Johansenbf832082012-05-16 11:00:05 -0700874
John Johansenc88d4c72010-07-29 14:48:00 -0700875 /* refcount released by caller */
876 return profile;
877}
878
879/**
880 * replacement_allowed - test to see if replacement is allowed
881 * @profile: profile to test if it can be replaced (MAYBE NULL)
882 * @noreplace: true if replacement shouldn't be allowed but addition is okay
883 * @info: Returns - info about why replacement failed (NOT NULL)
884 *
885 * Returns: %0 if replacement allowed else error code
886 */
887static int replacement_allowed(struct aa_profile *profile, int noreplace,
888 const char **info)
889{
890 if (profile) {
891 if (profile->flags & PFLAG_IMMUTABLE) {
892 *info = "cannot replace immutible profile";
893 return -EPERM;
894 } else if (noreplace) {
895 *info = "profile already exists";
896 return -EEXIST;
897 }
898 }
899 return 0;
900}
901
902/**
John Johansenc88d4c72010-07-29 14:48:00 -0700903 * aa_audit_policy - Do auditing of policy changes
904 * @op: policy operation being performed
905 * @gfp: memory allocation flags
906 * @name: name of profile being manipulated (NOT NULL)
907 * @info: any extra information to be audited (MAYBE NULL)
908 * @error: error code
909 *
910 * Returns: the error to be returned after audit is done
911 */
912static int audit_policy(int op, gfp_t gfp, const char *name, const char *info,
913 int error)
914{
915 struct common_audit_data sa;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700916 struct apparmor_audit_data aad = {0,};
Eric Paris50c205f2012-04-04 15:01:43 -0400917 sa.type = LSM_AUDIT_DATA_NONE;
Eric Paris3b3b0e42012-04-03 09:37:02 -0700918 sa.aad = &aad;
919 aad.op = op;
920 aad.name = name;
921 aad.info = info;
922 aad.error = error;
John Johansenc88d4c72010-07-29 14:48:00 -0700923
924 return aa_audit(AUDIT_APPARMOR_STATUS, __aa_current_profile(), gfp,
925 &sa, NULL);
926}
927
928/**
929 * aa_may_manage_policy - can the current task manage policy
930 * @op: the policy manipulation operation being done
931 *
932 * Returns: true if the task is allowed to manipulate policy
933 */
934bool aa_may_manage_policy(int op)
935{
936 /* check if loading policy is locked out */
937 if (aa_g_lock_policy) {
938 audit_policy(op, GFP_KERNEL, NULL, "policy_locked", -EACCES);
939 return 0;
940 }
941
942 if (!capable(CAP_MAC_ADMIN)) {
943 audit_policy(op, GFP_KERNEL, NULL, "not policy admin", -EACCES);
944 return 0;
945 }
946
947 return 1;
948}
949
John Johansendd51c8482013-07-10 21:05:43 -0700950static struct aa_profile *__list_lookup_parent(struct list_head *lh,
951 struct aa_profile *profile)
952{
953 const char *base = hname_tail(profile->base.hname);
954 long len = base - profile->base.hname;
955 struct aa_load_ent *ent;
956
957 /* parent won't have trailing // so remove from len */
958 if (len <= 2)
959 return NULL;
960 len -= 2;
961
962 list_for_each_entry(ent, lh, list) {
963 if (ent->new == profile)
964 continue;
965 if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
966 0 && ent->new->base.hname[len] == 0)
967 return ent->new;
968 }
969
970 return NULL;
971}
972
973/**
974 * __replace_profile - replace @old with @new on a list
975 * @old: profile to be replaced (NOT NULL)
976 * @new: profile to replace @old with (NOT NULL)
John Johansen77b071b2013-07-10 21:07:43 -0700977 * @share_replacedby: transfer @old->replacedby to @new
John Johansendd51c8482013-07-10 21:05:43 -0700978 *
979 * Will duplicate and refcount elements that @new inherits from @old
980 * and will inherit @old children.
981 *
982 * refcount @new for list, put @old list refcount
983 *
984 * Requires: namespace list lock be held, or list not be shared
985 */
John Johansen77b071b2013-07-10 21:07:43 -0700986static void __replace_profile(struct aa_profile *old, struct aa_profile *new,
987 bool share_replacedby)
John Johansendd51c8482013-07-10 21:05:43 -0700988{
989 struct aa_profile *child, *tmp;
990
991 if (!list_empty(&old->base.profiles)) {
992 LIST_HEAD(lh);
John Johansen01e2b672013-07-10 21:06:43 -0700993 list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
John Johansendd51c8482013-07-10 21:05:43 -0700994
995 list_for_each_entry_safe(child, tmp, &lh, base.list) {
996 struct aa_profile *p;
997
998 list_del_init(&child->base.list);
999 p = __find_child(&new->base.profiles, child->base.name);
1000 if (p) {
1001 /* @p replaces @child */
John Johansen77b071b2013-07-10 21:07:43 -07001002 __replace_profile(child, p, share_replacedby);
John Johansendd51c8482013-07-10 21:05:43 -07001003 continue;
1004 }
1005
1006 /* inherit @child and its children */
1007 /* TODO: update hname of inherited children */
1008 /* list refcount transferred to @new */
John Johansen01e2b672013-07-10 21:06:43 -07001009 p = rcu_dereference_protected(child->parent,
1010 mutex_is_locked(&child->ns->lock));
1011 rcu_assign_pointer(child->parent, aa_get_profile(new));
1012 list_add_rcu(&child->base.list, &new->base.profiles);
1013 aa_put_profile(p);
John Johansendd51c8482013-07-10 21:05:43 -07001014 }
1015 }
1016
John Johansen01e2b672013-07-10 21:06:43 -07001017 if (!rcu_access_pointer(new->parent)) {
1018 struct aa_profile *parent = rcu_dereference(old->parent);
1019 rcu_assign_pointer(new->parent, aa_get_profile(parent));
1020 }
John Johansen77b071b2013-07-10 21:07:43 -07001021 __aa_update_replacedby(old, new);
1022 if (share_replacedby) {
1023 aa_put_replacedby(new->replacedby);
1024 new->replacedby = aa_get_replacedby(old->replacedby);
1025 }
John Johansendd51c8482013-07-10 21:05:43 -07001026
1027 if (list_empty(&new->base.list)) {
1028 /* new is not on a list already */
John Johansen01e2b672013-07-10 21:06:43 -07001029 list_replace_rcu(&old->base.list, &new->base.list);
John Johansendd51c8482013-07-10 21:05:43 -07001030 aa_get_profile(new);
1031 aa_put_profile(old);
1032 } else
1033 __list_remove_profile(old);
1034}
1035
1036/**
1037 * __lookup_replace - lookup replacement information for a profile
1038 * @ns - namespace the lookup occurs in
1039 * @hname - name of profile to lookup
1040 * @noreplace - true if not replacing an existing profile
1041 * @p - Returns: profile to be replaced
1042 * @info - Returns: info string on why lookup failed
1043 *
1044 * Returns: profile to replace (no ref) on success else ptr error
1045 */
1046static int __lookup_replace(struct aa_namespace *ns, const char *hname,
1047 bool noreplace, struct aa_profile **p,
1048 const char **info)
1049{
1050 *p = aa_get_profile(__lookup_profile(&ns->base, hname));
1051 if (*p) {
1052 int error = replacement_allowed(*p, noreplace, info);
1053 if (error) {
1054 *info = "profile can not be replaced";
1055 return error;
1056 }
1057 }
1058
1059 return 0;
1060}
1061
John Johansenc88d4c72010-07-29 14:48:00 -07001062/**
1063 * aa_replace_profiles - replace profile(s) on the profile list
1064 * @udata: serialized data stream (NOT NULL)
1065 * @size: size of the serialized data stream
1066 * @noreplace: true if only doing addition, no replacement allowed
1067 *
1068 * unpack and replace a profile on the profile list and uses of that profile
1069 * by any aa_task_cxt. If the profile does not exist on the profile list
1070 * it is added.
1071 *
1072 * Returns: size of data consumed else error code on failure.
1073 */
1074ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace)
1075{
John Johansenc88d4c72010-07-29 14:48:00 -07001076 const char *ns_name, *name = NULL, *info = NULL;
John Johansendd51c8482013-07-10 21:05:43 -07001077 struct aa_namespace *ns = NULL;
1078 struct aa_load_ent *ent, *tmp;
John Johansenc88d4c72010-07-29 14:48:00 -07001079 int op = OP_PROF_REPL;
1080 ssize_t error;
John Johansendd51c8482013-07-10 21:05:43 -07001081 LIST_HEAD(lh);
John Johansenc88d4c72010-07-29 14:48:00 -07001082
1083 /* released below */
John Johansendd51c8482013-07-10 21:05:43 -07001084 error = aa_unpack(udata, size, &lh, &ns_name);
1085 if (error)
1086 goto out;
John Johansenc88d4c72010-07-29 14:48:00 -07001087
1088 /* released below */
1089 ns = aa_prepare_namespace(ns_name);
1090 if (!ns) {
1091 info = "failed to prepare namespace";
1092 error = -ENOMEM;
1093 name = ns_name;
1094 goto fail;
1095 }
1096
John Johansen01e2b672013-07-10 21:06:43 -07001097 mutex_lock(&ns->lock);
John Johansendd51c8482013-07-10 21:05:43 -07001098 /* setup parent and ns info */
1099 list_for_each_entry(ent, &lh, list) {
1100 struct aa_policy *policy;
John Johansenc88d4c72010-07-29 14:48:00 -07001101
John Johansendd51c8482013-07-10 21:05:43 -07001102 name = ent->new->base.hname;
1103 error = __lookup_replace(ns, ent->new->base.hname, noreplace,
1104 &ent->old, &info);
1105 if (error)
1106 goto fail_lock;
John Johansenc88d4c72010-07-29 14:48:00 -07001107
John Johansendd51c8482013-07-10 21:05:43 -07001108 if (ent->new->rename) {
1109 error = __lookup_replace(ns, ent->new->rename,
1110 noreplace, &ent->rename,
1111 &info);
1112 if (error)
1113 goto fail_lock;
John Johansenc88d4c72010-07-29 14:48:00 -07001114 }
John Johansendd51c8482013-07-10 21:05:43 -07001115
1116 /* released when @new is freed */
1117 ent->new->ns = aa_get_namespace(ns);
1118
1119 if (ent->old || ent->rename)
1120 continue;
1121
1122 /* no ref on policy only use inside lock */
1123 policy = __lookup_parent(ns, ent->new->base.hname);
1124 if (!policy) {
1125 struct aa_profile *p;
1126 p = __list_lookup_parent(&lh, ent->new);
1127 if (!p) {
1128 error = -ENOENT;
1129 info = "parent does not exist";
1130 name = ent->new->base.hname;
1131 goto fail_lock;
1132 }
John Johansen01e2b672013-07-10 21:06:43 -07001133 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1134 } else if (policy != &ns->base) {
John Johansendd51c8482013-07-10 21:05:43 -07001135 /* released on profile replacement or free_profile */
John Johansen01e2b672013-07-10 21:06:43 -07001136 struct aa_profile *p = (struct aa_profile *) policy;
1137 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1138 }
John Johansenc88d4c72010-07-29 14:48:00 -07001139 }
1140
John Johansendd51c8482013-07-10 21:05:43 -07001141 /* do actual replacement */
1142 list_for_each_entry_safe(ent, tmp, &lh, list) {
1143 list_del_init(&ent->list);
1144 op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
John Johansenc88d4c72010-07-29 14:48:00 -07001145
John Johansendd51c8482013-07-10 21:05:43 -07001146 audit_policy(op, GFP_ATOMIC, ent->new->base.name, NULL, error);
John Johansenc88d4c72010-07-29 14:48:00 -07001147
John Johansendd51c8482013-07-10 21:05:43 -07001148 if (ent->old) {
John Johansen77b071b2013-07-10 21:07:43 -07001149 __replace_profile(ent->old, ent->new, 1);
John Johansendd51c8482013-07-10 21:05:43 -07001150 if (ent->rename)
John Johansen77b071b2013-07-10 21:07:43 -07001151 __replace_profile(ent->rename, ent->new, 0);
John Johansendd51c8482013-07-10 21:05:43 -07001152 } else if (ent->rename) {
John Johansen77b071b2013-07-10 21:07:43 -07001153 __replace_profile(ent->rename, ent->new, 0);
John Johansendd51c8482013-07-10 21:05:43 -07001154 } else if (ent->new->parent) {
John Johansen01e2b672013-07-10 21:06:43 -07001155 struct aa_profile *parent, *newest;
1156 parent = rcu_dereference_protected(ent->new->parent,
1157 mutex_is_locked(&ns->lock));
John Johansen77b071b2013-07-10 21:07:43 -07001158 newest = aa_get_newest_profile(parent);
John Johansen01e2b672013-07-10 21:06:43 -07001159
John Johansendd51c8482013-07-10 21:05:43 -07001160 /* parent replaced in this atomic set? */
John Johansen01e2b672013-07-10 21:06:43 -07001161 if (newest != parent) {
1162 aa_get_profile(newest);
1163 aa_put_profile(parent);
1164 rcu_assign_pointer(ent->new->parent, newest);
John Johansen77b071b2013-07-10 21:07:43 -07001165 } else
1166 aa_put_profile(newest);
John Johansendd51c8482013-07-10 21:05:43 -07001167 __list_add_profile(&parent->base.profiles, ent->new);
1168 } else
1169 __list_add_profile(&ns->base.profiles, ent->new);
John Johansenc88d4c72010-07-29 14:48:00 -07001170
John Johansendd51c8482013-07-10 21:05:43 -07001171 aa_load_ent_free(ent);
John Johansenc88d4c72010-07-29 14:48:00 -07001172 }
John Johansen01e2b672013-07-10 21:06:43 -07001173 mutex_unlock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -07001174
1175out:
1176 aa_put_namespace(ns);
John Johansendd51c8482013-07-10 21:05:43 -07001177
John Johansenc88d4c72010-07-29 14:48:00 -07001178 if (error)
1179 return error;
1180 return size;
1181
John Johansendd51c8482013-07-10 21:05:43 -07001182fail_lock:
John Johansen01e2b672013-07-10 21:06:43 -07001183 mutex_unlock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -07001184fail:
1185 error = audit_policy(op, GFP_KERNEL, name, info, error);
John Johansendd51c8482013-07-10 21:05:43 -07001186
1187 list_for_each_entry_safe(ent, tmp, &lh, list) {
1188 list_del_init(&ent->list);
1189 aa_load_ent_free(ent);
1190 }
1191
John Johansenc88d4c72010-07-29 14:48:00 -07001192 goto out;
1193}
1194
1195/**
1196 * aa_remove_profiles - remove profile(s) from the system
1197 * @fqname: name of the profile or namespace to remove (NOT NULL)
1198 * @size: size of the name
1199 *
1200 * Remove a profile or sub namespace from the current namespace, so that
1201 * they can not be found anymore and mark them as replaced by unconfined
1202 *
1203 * NOTE: removing confinement does not restore rlimits to preconfinemnet values
1204 *
1205 * Returns: size of data consume else error code if fails
1206 */
1207ssize_t aa_remove_profiles(char *fqname, size_t size)
1208{
1209 struct aa_namespace *root, *ns = NULL;
1210 struct aa_profile *profile = NULL;
1211 const char *name = fqname, *info = NULL;
1212 ssize_t error = 0;
1213
1214 if (*fqname == 0) {
1215 info = "no profile specified";
1216 error = -ENOENT;
1217 goto fail;
1218 }
1219
1220 root = aa_current_profile()->ns;
1221
1222 if (fqname[0] == ':') {
1223 char *ns_name;
1224 name = aa_split_fqname(fqname, &ns_name);
John Johansen41d1b3e2013-02-21 01:14:17 -08001225 /* released below */
1226 ns = aa_find_namespace(root, ns_name);
1227 if (!ns) {
1228 info = "namespace does not exist";
1229 error = -ENOENT;
1230 goto fail;
John Johansenc88d4c72010-07-29 14:48:00 -07001231 }
1232 } else
1233 /* released below */
1234 ns = aa_get_namespace(root);
1235
John Johansenc88d4c72010-07-29 14:48:00 -07001236 if (!name) {
1237 /* remove namespace - can only happen if fqname[0] == ':' */
John Johansen01e2b672013-07-10 21:06:43 -07001238 mutex_lock(&ns->parent->lock);
John Johansenc88d4c72010-07-29 14:48:00 -07001239 __remove_namespace(ns);
John Johansen01e2b672013-07-10 21:06:43 -07001240 mutex_unlock(&ns->parent->lock);
John Johansenc88d4c72010-07-29 14:48:00 -07001241 } else {
1242 /* remove profile */
John Johansen01e2b672013-07-10 21:06:43 -07001243 mutex_lock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -07001244 profile = aa_get_profile(__lookup_profile(&ns->base, name));
1245 if (!profile) {
1246 error = -ENOENT;
1247 info = "profile does not exist";
1248 goto fail_ns_lock;
1249 }
1250 name = profile->base.hname;
1251 __remove_profile(profile);
John Johansen01e2b672013-07-10 21:06:43 -07001252 mutex_unlock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -07001253 }
John Johansenc88d4c72010-07-29 14:48:00 -07001254
1255 /* don't fail removal if audit fails */
1256 (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error);
1257 aa_put_namespace(ns);
1258 aa_put_profile(profile);
1259 return size;
1260
1261fail_ns_lock:
John Johansen01e2b672013-07-10 21:06:43 -07001262 mutex_unlock(&ns->lock);
John Johansenc88d4c72010-07-29 14:48:00 -07001263 aa_put_namespace(ns);
1264
1265fail:
1266 (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error);
1267 return error;
1268}