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