]> git.karo-electronics.de Git - karo-tx-linux.git/blob - security/apparmor/policy.c
apparmor: add fn to lookup profiles by fqname
[karo-tx-linux.git] / security / apparmor / policy.c
1 /*
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_ns.h"
89 #include "include/policy_unpack.h"
90 #include "include/resource.h"
91
92
93 const char *const aa_profile_mode_names[] = {
94         "enforce",
95         "complain",
96         "kill",
97         "unconfined",
98 };
99
100
101 /* requires profile list write lock held */
102 void __aa_update_proxy(struct aa_profile *orig, struct aa_profile *new)
103 {
104         struct aa_profile *tmp;
105
106         tmp = rcu_dereference_protected(orig->proxy->profile,
107                                         mutex_is_locked(&orig->ns->lock));
108         rcu_assign_pointer(orig->proxy->profile, aa_get_profile(new));
109         orig->flags |= PFLAG_STALE;
110         aa_put_profile(tmp);
111 }
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  */
122 static void __list_add_profile(struct list_head *list,
123                                struct aa_profile *profile)
124 {
125         list_add_rcu(&profile->base.list, list);
126         /* 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  */
142 static void __list_remove_profile(struct aa_profile *profile)
143 {
144         list_del_rcu(&profile->base.list);
145         aa_put_profile(profile);
146 }
147
148 /**
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  */
154 static void __remove_profile(struct aa_profile *profile)
155 {
156         /* release any children lists first */
157         __aa_profile_list_release(&profile->base.profiles);
158         /* released by free_profile */
159         __aa_update_proxy(profile, profile->ns->unconfined);
160         __aa_fs_profile_rmdir(profile);
161         __list_remove_profile(profile);
162 }
163
164 /**
165  * __aa_profile_list_release - remove all profiles on the list and put refs
166  * @head: list of profiles  (NOT NULL)
167  *
168  * Requires: namespace lock be held
169  */
170 void __aa_profile_list_release(struct list_head *head)
171 {
172         struct aa_profile *profile, *tmp;
173         list_for_each_entry_safe(profile, tmp, head, base.list)
174                 __remove_profile(profile);
175 }
176
177
178 static void free_proxy(struct aa_proxy *p)
179 {
180         if (p) {
181                 /* r->profile will not be updated any more as r is dead */
182                 aa_put_profile(rcu_dereference_protected(p->profile, true));
183                 kzfree(p);
184         }
185 }
186
187
188 void aa_free_proxy_kref(struct kref *kref)
189 {
190         struct aa_proxy *p = container_of(kref, struct aa_proxy, count);
191
192         free_proxy(p);
193 }
194
195 /**
196  * aa_free_profile - free a profile
197  * @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  */
205 void aa_free_profile(struct aa_profile *profile)
206 {
207         AA_DEBUG("%s(%p)\n", __func__, profile);
208
209         if (!profile)
210                 return;
211
212         /* free children profiles */
213         aa_policy_destroy(&profile->base);
214         aa_put_profile(rcu_access_pointer(profile->parent));
215
216         aa_put_ns(profile->ns);
217         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
223         kzfree(profile->dirname);
224         aa_put_dfa(profile->xmatch);
225         aa_put_dfa(profile->policy.dfa);
226         aa_put_proxy(profile->proxy);
227
228         kzfree(profile->hash);
229         kzfree(profile);
230 }
231
232 /**
233  * 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  */
236 static void aa_free_profile_rcu(struct rcu_head *head)
237 {
238         struct aa_profile *p = container_of(head, struct aa_profile, rcu);
239         if (p->flags & PFLAG_NS_COUNT)
240                 aa_free_ns(p->ns);
241         else
242                 aa_free_profile(p);
243 }
244
245 /**
246  * 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  */
249 void aa_free_profile_kref(struct kref *kref)
250 {
251         struct aa_profile *p = container_of(kref, struct aa_profile, count);
252         call_rcu(&p->rcu, aa_free_profile_rcu);
253 }
254
255 /**
256  * aa_alloc_profile - allocate, initialize and return a new profile
257  * @hname: name of the profile  (NOT NULL)
258  *
259  * Returns: refcount profile or NULL on failure
260  */
261 struct aa_profile *aa_alloc_profile(const char *hname)
262 {
263         struct aa_profile *profile;
264
265         /* freed by free_profile - usually through aa_put_profile */
266         profile = kzalloc(sizeof(*profile), GFP_KERNEL);
267         if (!profile)
268                 return NULL;
269
270         profile->proxy = kzalloc(sizeof(struct aa_proxy), GFP_KERNEL);
271         if (!profile->proxy)
272                 goto fail;
273         kref_init(&profile->proxy->count);
274
275         if (!aa_policy_init(&profile->base, NULL, hname))
276                 goto fail;
277         kref_init(&profile->count);
278
279         /* refcount released by caller */
280         return profile;
281
282 fail:
283         kzfree(profile->proxy);
284         kzfree(profile);
285
286         return NULL;
287 }
288
289 /**
290  * aa_new_null_profile - create a new null-X learning profile
291  * @parent: profile that caused this profile to be created (NOT NULL)
292  * @hat: true if the null- learning profile is a hat
293  *
294  * Create a null- complain mode profile used in learning mode.  The name of
295  * the profile is unique and follows the format of parent//null-<uniq>.
296  *
297  * null profiles are added to the profile list but the list does not
298  * hold a count on them so that they are automatically released when
299  * not in use.
300  *
301  * Returns: new refcounted profile else NULL on failure
302  */
303 struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat)
304 {
305         struct aa_profile *profile = NULL;
306         char *name;
307         int uniq = atomic_inc_return(&parent->ns->uniq_null);
308
309         /* freed below */
310         name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, GFP_KERNEL);
311         if (!name)
312                 goto fail;
313         sprintf(name, "%s//null-%x", parent->base.hname, uniq);
314
315         profile = aa_alloc_profile(name);
316         kfree(name);
317         if (!profile)
318                 goto fail;
319
320         profile->mode = APPARMOR_COMPLAIN;
321         profile->flags = PFLAG_NULL;
322         if (hat)
323                 profile->flags |= PFLAG_HAT;
324
325         /* released on free_profile */
326         rcu_assign_pointer(profile->parent, aa_get_profile(parent));
327         profile->ns = aa_get_ns(parent->ns);
328
329         mutex_lock(&profile->ns->lock);
330         __list_add_profile(&parent->base.profiles, profile);
331         mutex_unlock(&profile->ns->lock);
332
333         /* refcount released by caller */
334         return profile;
335
336 fail:
337         return NULL;
338 }
339
340 /* TODO: profile accounting - setup in remove */
341
342 /**
343  * __find_child - find a profile on @head list with a name matching @name
344  * @head: list to search  (NOT NULL)
345  * @name: name of profile (NOT NULL)
346  *
347  * Requires: rcu_read_lock be held
348  *
349  * Returns: unrefcounted profile ptr, or NULL if not found
350  */
351 static struct aa_profile *__find_child(struct list_head *head, const char *name)
352 {
353         return (struct aa_profile *)__policy_find(head, name);
354 }
355
356 /**
357  * __strn_find_child - find a profile on @head list using substring of @name
358  * @head: list to search  (NOT NULL)
359  * @name: name of profile (NOT NULL)
360  * @len: length of @name substring to match
361  *
362  * Requires: rcu_read_lock be held
363  *
364  * Returns: unrefcounted profile ptr, or NULL if not found
365  */
366 static struct aa_profile *__strn_find_child(struct list_head *head,
367                                             const char *name, int len)
368 {
369         return (struct aa_profile *)__policy_strn_find(head, name, len);
370 }
371
372 /**
373  * aa_find_child - find a profile by @name in @parent
374  * @parent: profile to search  (NOT NULL)
375  * @name: profile name to search for  (NOT NULL)
376  *
377  * Returns: a refcounted profile or NULL if not found
378  */
379 struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
380 {
381         struct aa_profile *profile;
382
383         rcu_read_lock();
384         do {
385                 profile = __find_child(&parent->base.profiles, name);
386         } while (profile && !aa_get_profile_not0(profile));
387         rcu_read_unlock();
388
389         /* refcount released by caller */
390         return profile;
391 }
392
393 /**
394  * __lookup_parent - lookup the parent of a profile of name @hname
395  * @ns: namespace to lookup profile in  (NOT NULL)
396  * @hname: hierarchical profile name to find parent of  (NOT NULL)
397  *
398  * Lookups up the parent of a fully qualified profile name, the profile
399  * that matches hname does not need to exist, in general this
400  * is used to load a new profile.
401  *
402  * Requires: rcu_read_lock be held
403  *
404  * Returns: unrefcounted policy or NULL if not found
405  */
406 static struct aa_policy *__lookup_parent(struct aa_ns *ns,
407                                          const char *hname)
408 {
409         struct aa_policy *policy;
410         struct aa_profile *profile = NULL;
411         char *split;
412
413         policy = &ns->base;
414
415         for (split = strstr(hname, "//"); split;) {
416                 profile = __strn_find_child(&policy->profiles, hname,
417                                             split - hname);
418                 if (!profile)
419                         return NULL;
420                 policy = &profile->base;
421                 hname = split + 2;
422                 split = strstr(hname, "//");
423         }
424         if (!profile)
425                 return &ns->base;
426         return &profile->base;
427 }
428
429 /**
430  * __lookupn_profile - lookup the profile matching @hname
431  * @base: base list to start looking up profile name from  (NOT NULL)
432  * @hname: hierarchical profile name  (NOT NULL)
433  * @n: length of @hname
434  *
435  * Requires: rcu_read_lock be held
436  *
437  * Returns: unrefcounted profile pointer or NULL if not found
438  *
439  * Do a relative name lookup, recursing through profile tree.
440  */
441 static struct aa_profile *__lookupn_profile(struct aa_policy *base,
442                                             const char *hname, size_t n)
443 {
444         struct aa_profile *profile = NULL;
445         const char *split;
446
447         for (split = strnstr(hname, "//", n); split;
448              split = strnstr(hname, "//", n)) {
449                 profile = __strn_find_child(&base->profiles, hname,
450                                             split - hname);
451                 if (!profile)
452                         return NULL;
453
454                 base = &profile->base;
455                 n -= split + 2 - hname;
456                 hname = split + 2;
457         }
458
459         if (n)
460                 return __strn_find_child(&base->profiles, hname, n);
461         return NULL;
462 }
463
464 static struct aa_profile *__lookup_profile(struct aa_policy *base,
465                                            const char *hname)
466 {
467         return __lookupn_profile(base, hname, strlen(hname));
468 }
469
470 /**
471  * aa_lookup_profile - find a profile by its full or partial name
472  * @ns: the namespace to start from (NOT NULL)
473  * @hname: name to do lookup on.  Does not contain namespace prefix (NOT NULL)
474  * @n: size of @hname
475  *
476  * Returns: refcounted profile or NULL if not found
477  */
478 struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
479                                       size_t n)
480 {
481         struct aa_profile *profile;
482
483         rcu_read_lock();
484         do {
485                 profile = __lookupn_profile(&ns->base, hname, n);
486         } while (profile && !aa_get_profile_not0(profile));
487         rcu_read_unlock();
488
489         /* the unconfined profile is not in the regular profile list */
490         if (!profile && strncmp(hname, "unconfined", n) == 0)
491                 profile = aa_get_newest_profile(ns->unconfined);
492
493         /* refcount released by caller */
494         return profile;
495 }
496
497 struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *hname)
498 {
499         return aa_lookupn_profile(ns, hname, strlen(hname));
500 }
501
502 struct aa_profile *aa_fqlookupn_profile(struct aa_profile *base,
503                                         const char *fqname, size_t n)
504 {
505         struct aa_profile *profile;
506         struct aa_ns *ns;
507         const char *name, *ns_name;
508         size_t ns_len;
509
510         name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
511         if (ns_name) {
512                 ns = aa_findn_ns(base->ns, ns_name, ns_len);
513                 if (!ns)
514                         return NULL;
515         } else
516                 ns = aa_get_ns(base->ns);
517
518         if (name)
519                 profile = aa_lookupn_profile(ns, name, n - (name - fqname));
520         else if (ns)
521                 /* default profile for ns, currently unconfined */
522                 profile = aa_get_newest_profile(ns->unconfined);
523         else
524                 profile = NULL;
525         aa_put_ns(ns);
526
527         return profile;
528 }
529
530 /**
531  * replacement_allowed - test to see if replacement is allowed
532  * @profile: profile to test if it can be replaced  (MAYBE NULL)
533  * @noreplace: true if replacement shouldn't be allowed but addition is okay
534  * @info: Returns - info about why replacement failed (NOT NULL)
535  *
536  * Returns: %0 if replacement allowed else error code
537  */
538 static int replacement_allowed(struct aa_profile *profile, int noreplace,
539                                const char **info)
540 {
541         if (profile) {
542                 if (profile->flags & PFLAG_IMMUTABLE) {
543                         *info = "cannot replace immutible profile";
544                         return -EPERM;
545                 } else if (noreplace) {
546                         *info = "profile already exists";
547                         return -EEXIST;
548                 }
549         }
550         return 0;
551 }
552
553 /**
554  * aa_audit_policy - Do auditing of policy changes
555  * @op: policy operation being performed
556  * @gfp: memory allocation flags
557  * @name: name of profile being manipulated (NOT NULL)
558  * @info: any extra information to be audited (MAYBE NULL)
559  * @error: error code
560  *
561  * Returns: the error to be returned after audit is done
562  */
563 static int audit_policy(int op, gfp_t gfp, const char *name, const char *info,
564                         int error)
565 {
566         struct common_audit_data sa;
567         struct apparmor_audit_data aad = {0,};
568         sa.type = LSM_AUDIT_DATA_NONE;
569         sa.aad = &aad;
570         aad.op = op;
571         aad.name = name;
572         aad.info = info;
573         aad.error = error;
574
575         return aa_audit(AUDIT_APPARMOR_STATUS, __aa_current_profile(), gfp,
576                         &sa, NULL);
577 }
578
579 bool policy_view_capable(void)
580 {
581         struct user_namespace *user_ns = current_user_ns();
582         bool response = false;
583
584         if (ns_capable(user_ns, CAP_MAC_ADMIN))
585                 response = true;
586
587         return response;
588 }
589
590 bool policy_admin_capable(void)
591 {
592         return policy_view_capable() && !aa_g_lock_policy;
593 }
594
595 /**
596  * aa_may_manage_policy - can the current task manage policy
597  * @op: the policy manipulation operation being done
598  *
599  * Returns: true if the task is allowed to manipulate policy
600  */
601 bool aa_may_manage_policy(int op)
602 {
603         /* check if loading policy is locked out */
604         if (aa_g_lock_policy) {
605                 audit_policy(op, GFP_KERNEL, NULL, "policy_locked", -EACCES);
606                 return 0;
607         }
608
609         if (!policy_admin_capable()) {
610                 audit_policy(op, GFP_KERNEL, NULL, "not policy admin", -EACCES);
611                 return 0;
612         }
613
614         return 1;
615 }
616
617 static struct aa_profile *__list_lookup_parent(struct list_head *lh,
618                                                struct aa_profile *profile)
619 {
620         const char *base = hname_tail(profile->base.hname);
621         long len = base - profile->base.hname;
622         struct aa_load_ent *ent;
623
624         /* parent won't have trailing // so remove from len */
625         if (len <= 2)
626                 return NULL;
627         len -= 2;
628
629         list_for_each_entry(ent, lh, list) {
630                 if (ent->new == profile)
631                         continue;
632                 if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
633                     0 && ent->new->base.hname[len] == 0)
634                         return ent->new;
635         }
636
637         return NULL;
638 }
639
640 /**
641  * __replace_profile - replace @old with @new on a list
642  * @old: profile to be replaced  (NOT NULL)
643  * @new: profile to replace @old with  (NOT NULL)
644  * @share_proxy: transfer @old->proxy to @new
645  *
646  * Will duplicate and refcount elements that @new inherits from @old
647  * and will inherit @old children.
648  *
649  * refcount @new for list, put @old list refcount
650  *
651  * Requires: namespace list lock be held, or list not be shared
652  */
653 static void __replace_profile(struct aa_profile *old, struct aa_profile *new,
654                               bool share_proxy)
655 {
656         struct aa_profile *child, *tmp;
657
658         if (!list_empty(&old->base.profiles)) {
659                 LIST_HEAD(lh);
660                 list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
661
662                 list_for_each_entry_safe(child, tmp, &lh, base.list) {
663                         struct aa_profile *p;
664
665                         list_del_init(&child->base.list);
666                         p = __find_child(&new->base.profiles, child->base.name);
667                         if (p) {
668                                 /* @p replaces @child  */
669                                 __replace_profile(child, p, share_proxy);
670                                 continue;
671                         }
672
673                         /* inherit @child and its children */
674                         /* TODO: update hname of inherited children */
675                         /* list refcount transferred to @new */
676                         p = aa_deref_parent(child);
677                         rcu_assign_pointer(child->parent, aa_get_profile(new));
678                         list_add_rcu(&child->base.list, &new->base.profiles);
679                         aa_put_profile(p);
680                 }
681         }
682
683         if (!rcu_access_pointer(new->parent)) {
684                 struct aa_profile *parent = aa_deref_parent(old);
685                 rcu_assign_pointer(new->parent, aa_get_profile(parent));
686         }
687         __aa_update_proxy(old, new);
688         if (share_proxy) {
689                 aa_put_proxy(new->proxy);
690                 new->proxy = aa_get_proxy(old->proxy);
691         } else if (!rcu_access_pointer(new->proxy->profile))
692                 /* aafs interface uses proxy */
693                 rcu_assign_pointer(new->proxy->profile,
694                                    aa_get_profile(new));
695         __aa_fs_profile_migrate_dents(old, new);
696
697         if (list_empty(&new->base.list)) {
698                 /* new is not on a list already */
699                 list_replace_rcu(&old->base.list, &new->base.list);
700                 aa_get_profile(new);
701                 aa_put_profile(old);
702         } else
703                 __list_remove_profile(old);
704 }
705
706 /**
707  * __lookup_replace - lookup replacement information for a profile
708  * @ns - namespace the lookup occurs in
709  * @hname - name of profile to lookup
710  * @noreplace - true if not replacing an existing profile
711  * @p - Returns: profile to be replaced
712  * @info - Returns: info string on why lookup failed
713  *
714  * Returns: profile to replace (no ref) on success else ptr error
715  */
716 static int __lookup_replace(struct aa_ns *ns, const char *hname,
717                             bool noreplace, struct aa_profile **p,
718                             const char **info)
719 {
720         *p = aa_get_profile(__lookup_profile(&ns->base, hname));
721         if (*p) {
722                 int error = replacement_allowed(*p, noreplace, info);
723                 if (error) {
724                         *info = "profile can not be replaced";
725                         return error;
726                 }
727         }
728
729         return 0;
730 }
731
732 /**
733  * aa_replace_profiles - replace profile(s) on the profile list
734  * @udata: serialized data stream  (NOT NULL)
735  * @size: size of the serialized data stream
736  * @noreplace: true if only doing addition, no replacement allowed
737  *
738  * unpack and replace a profile on the profile list and uses of that profile
739  * by any aa_task_cxt.  If the profile does not exist on the profile list
740  * it is added.
741  *
742  * Returns: size of data consumed else error code on failure.
743  */
744 ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace)
745 {
746         const char *ns_name, *info = NULL;
747         struct aa_ns *ns = NULL;
748         struct aa_load_ent *ent, *tmp;
749         int op = OP_PROF_REPL;
750         ssize_t error;
751         LIST_HEAD(lh);
752
753         /* released below */
754         error = aa_unpack(udata, size, &lh, &ns_name);
755         if (error)
756                 goto out;
757
758         /* released below */
759         ns = aa_prepare_ns(ns_name);
760         if (!ns) {
761                 error = audit_policy(op, GFP_KERNEL, ns_name,
762                                      "failed to prepare namespace", -ENOMEM);
763                 goto free;
764         }
765
766         mutex_lock(&ns->lock);
767         /* setup parent and ns info */
768         list_for_each_entry(ent, &lh, list) {
769                 struct aa_policy *policy;
770                 error = __lookup_replace(ns, ent->new->base.hname, noreplace,
771                                          &ent->old, &info);
772                 if (error)
773                         goto fail_lock;
774
775                 if (ent->new->rename) {
776                         error = __lookup_replace(ns, ent->new->rename,
777                                                  noreplace, &ent->rename,
778                                                  &info);
779                         if (error)
780                                 goto fail_lock;
781                 }
782
783                 /* released when @new is freed */
784                 ent->new->ns = aa_get_ns(ns);
785
786                 if (ent->old || ent->rename)
787                         continue;
788
789                 /* no ref on policy only use inside lock */
790                 policy = __lookup_parent(ns, ent->new->base.hname);
791                 if (!policy) {
792                         struct aa_profile *p;
793                         p = __list_lookup_parent(&lh, ent->new);
794                         if (!p) {
795                                 error = -ENOENT;
796                                 info = "parent does not exist";
797                                 goto fail_lock;
798                         }
799                         rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
800                 } else if (policy != &ns->base) {
801                         /* released on profile replacement or free_profile */
802                         struct aa_profile *p = (struct aa_profile *) policy;
803                         rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
804                 }
805         }
806
807         /* create new fs entries for introspection if needed */
808         list_for_each_entry(ent, &lh, list) {
809                 if (ent->old) {
810                         /* inherit old interface files */
811
812                         /* if (ent->rename)
813                                 TODO: support rename */
814                 /* } else if (ent->rename) {
815                         TODO: support rename */
816                 } else {
817                         struct dentry *parent;
818                         if (rcu_access_pointer(ent->new->parent)) {
819                                 struct aa_profile *p;
820                                 p = aa_deref_parent(ent->new);
821                                 parent = prof_child_dir(p);
822                         } else
823                                 parent = ns_subprofs_dir(ent->new->ns);
824                         error = __aa_fs_profile_mkdir(ent->new, parent);
825                 }
826
827                 if (error) {
828                         info = "failed to create ";
829                         goto fail_lock;
830                 }
831         }
832
833         /* Done with checks that may fail - do actual replacement */
834         list_for_each_entry_safe(ent, tmp, &lh, list) {
835                 list_del_init(&ent->list);
836                 op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
837
838                 audit_policy(op, GFP_ATOMIC, ent->new->base.hname, NULL, error);
839
840                 if (ent->old) {
841                         __replace_profile(ent->old, ent->new, 1);
842                         if (ent->rename) {
843                                 /* aafs interface uses proxy */
844                                 struct aa_proxy *r = ent->new->proxy;
845                                 rcu_assign_pointer(r->profile,
846                                                    aa_get_profile(ent->new));
847                                 __replace_profile(ent->rename, ent->new, 0);
848                         }
849                 } else if (ent->rename) {
850                         /* aafs interface uses proxy */
851                         rcu_assign_pointer(ent->new->proxy->profile,
852                                            aa_get_profile(ent->new));
853                         __replace_profile(ent->rename, ent->new, 0);
854                 } else if (ent->new->parent) {
855                         struct aa_profile *parent, *newest;
856                         parent = aa_deref_parent(ent->new);
857                         newest = aa_get_newest_profile(parent);
858
859                         /* parent replaced in this atomic set? */
860                         if (newest != parent) {
861                                 aa_get_profile(newest);
862                                 rcu_assign_pointer(ent->new->parent, newest);
863                                 aa_put_profile(parent);
864                         }
865                         /* aafs interface uses proxy */
866                         rcu_assign_pointer(ent->new->proxy->profile,
867                                            aa_get_profile(ent->new));
868                         __list_add_profile(&newest->base.profiles, ent->new);
869                         aa_put_profile(newest);
870                 } else {
871                         /* aafs interface uses proxy */
872                         rcu_assign_pointer(ent->new->proxy->profile,
873                                            aa_get_profile(ent->new));
874                         __list_add_profile(&ns->base.profiles, ent->new);
875                 }
876                 aa_load_ent_free(ent);
877         }
878         mutex_unlock(&ns->lock);
879
880 out:
881         aa_put_ns(ns);
882
883         if (error)
884                 return error;
885         return size;
886
887 fail_lock:
888         mutex_unlock(&ns->lock);
889
890         /* audit cause of failure */
891         op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
892         audit_policy(op, GFP_KERNEL, ent->new->base.hname, info, error);
893         /* audit status that rest of profiles in the atomic set failed too */
894         info = "valid profile in failed atomic policy load";
895         list_for_each_entry(tmp, &lh, list) {
896                 if (tmp == ent) {
897                         info = "unchecked profile in failed atomic policy load";
898                         /* skip entry that caused failure */
899                         continue;
900                 }
901                 op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
902                 audit_policy(op, GFP_KERNEL, tmp->new->base.hname, info, error);
903         }
904 free:
905         list_for_each_entry_safe(ent, tmp, &lh, list) {
906                 list_del_init(&ent->list);
907                 aa_load_ent_free(ent);
908         }
909
910         goto out;
911 }
912
913 /**
914  * aa_remove_profiles - remove profile(s) from the system
915  * @fqname: name of the profile or namespace to remove  (NOT NULL)
916  * @size: size of the name
917  *
918  * Remove a profile or sub namespace from the current namespace, so that
919  * they can not be found anymore and mark them as replaced by unconfined
920  *
921  * NOTE: removing confinement does not restore rlimits to preconfinemnet values
922  *
923  * Returns: size of data consume else error code if fails
924  */
925 ssize_t aa_remove_profiles(char *fqname, size_t size)
926 {
927         struct aa_ns *root, *ns = NULL;
928         struct aa_profile *profile = NULL;
929         const char *name = fqname, *info = NULL;
930         ssize_t error = 0;
931
932         if (*fqname == 0) {
933                 info = "no profile specified";
934                 error = -ENOENT;
935                 goto fail;
936         }
937
938         root = aa_current_profile()->ns;
939
940         if (fqname[0] == ':') {
941                 char *ns_name;
942                 name = aa_split_fqname(fqname, &ns_name);
943                 /* released below */
944                 ns = aa_find_ns(root, ns_name);
945                 if (!ns) {
946                         info = "namespace does not exist";
947                         error = -ENOENT;
948                         goto fail;
949                 }
950         } else
951                 /* released below */
952                 ns = aa_get_ns(root);
953
954         if (!name) {
955                 /* remove namespace - can only happen if fqname[0] == ':' */
956                 mutex_lock(&ns->parent->lock);
957                 __aa_remove_ns(ns);
958                 mutex_unlock(&ns->parent->lock);
959         } else {
960                 /* remove profile */
961                 mutex_lock(&ns->lock);
962                 profile = aa_get_profile(__lookup_profile(&ns->base, name));
963                 if (!profile) {
964                         error = -ENOENT;
965                         info = "profile does not exist";
966                         goto fail_ns_lock;
967                 }
968                 name = profile->base.hname;
969                 __remove_profile(profile);
970                 mutex_unlock(&ns->lock);
971         }
972
973         /* don't fail removal if audit fails */
974         (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error);
975         aa_put_ns(ns);
976         aa_put_profile(profile);
977         return size;
978
979 fail_ns_lock:
980         mutex_unlock(&ns->lock);
981         aa_put_ns(ns);
982
983 fail:
984         (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error);
985         return error;
986 }