2 * AppArmor security module
4 * This file contains AppArmor policy manipulation functions
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
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
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.
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.
24 * Namespace and profile names can be written together in either
26 * :namespace:profile - used by kernel interfaces for easy detection
27 * namespace://profile - used by policy
29 * Profile names can not start with : or @ or ^ and may not contain \0
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
36 * Namespace names may not start with / or @ and may not contain \0 or :
37 * Reserved namespace names
38 * user-XXXX - user defined profiles
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.
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
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
67 * An fqname is a name that may contain both namespace and profile hnames.
68 * eg. :ns:/bin/bash//bin/ls
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
76 #include <linux/slab.h>
77 #include <linux/spinlock.h>
78 #include <linux/string.h>
79 #include <linux/user_namespace.h>
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/label.h"
87 #include "include/match.h"
88 #include "include/path.h"
89 #include "include/policy.h"
90 #include "include/policy_unpack.h"
91 #include "include/resource.h"
93 int unprivileged_userns_apparmor_policy
= 1;
95 /* Note: mode names must be unique in the first character because of
96 * modechrs used to print modes on compound labels on some interfaces
98 const char *const aa_profile_mode_names
[] = {
107 * __add_profile - add a profiles to list and label tree
108 * @list: list to add it to (NOT NULL)
109 * @profile: the profile to add (NOT NULL)
111 * refcount @profile, should be put by __list_remove_profile
113 * Requires: namespace lock be held, or list not be shared
115 static void __add_profile(struct list_head
*list
, struct aa_profile
*profile
)
121 AA_BUG(!profile
->ns
);
122 AA_BUG(!mutex_is_locked(&profile
->ns
->lock
));
124 list_add_rcu(&profile
->base
.list
, list
);
125 /* get list reference */
126 aa_get_profile(profile
);
127 l
= aa_label_insert(&profile
->ns
->labels
, &profile
->label
);
128 AA_BUG(l
!= &profile
->label
);
133 * __list_remove_profile - remove a profile from the list it is on
134 * @profile: the profile to remove (NOT NULL)
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.
140 * put @profile list refcount
142 * Requires: namespace lock be held, or list not have been live
144 static void __list_remove_profile(struct aa_profile
*profile
)
147 AA_BUG(!profile
->ns
);
148 AA_BUG(!mutex_is_locked(&profile
->ns
->lock
));
150 list_del_rcu(&profile
->base
.list
);
151 aa_put_profile(profile
);
154 void __aa_profile_list_release(struct list_head
*head
);
157 * __remove_profile - remove old profile, and children
158 * @profile: profile to be replaced (NOT NULL)
160 * Requires: namespace list lock be held, or list not be shared
162 static void __remove_profile(struct aa_profile
*profile
)
165 AA_BUG(!profile
->ns
);
166 AA_BUG(!mutex_is_locked(&profile
->ns
->lock
));
168 /* release any children lists first */
169 __aa_profile_list_release(&profile
->base
.profiles
);
170 /* released by free_profile */
171 aa_label_remove(&profile
->label
);
172 __aa_fs_profile_rmdir(profile
);
173 __list_remove_profile(profile
);
177 * __aa_profile_list_release - remove all profiles on the list and put refs
178 * @head: list of profiles (NOT NULL)
180 * Requires: namespace lock be held
182 void __aa_profile_list_release(struct list_head
*head
)
184 struct aa_profile
*profile
, *tmp
;
185 list_for_each_entry_safe(profile
, tmp
, head
, base
.list
)
186 __remove_profile(profile
);
191 * aa_free_profile - free a profile
192 * @profile: the profile to free (MAYBE NULL)
194 * Free a profile, its hats and null_profile. All references to the profile,
195 * its hats and null_profile must have been put.
197 * If the profile was referenced from a task context, free_profile() will
198 * be called from an rcu callback routine, so we must not sleep here.
200 void aa_free_profile(struct aa_profile
*profile
)
202 AA_DEBUG("%s(%p)\n", __func__
, profile
);
207 /* free children profiles */
208 aa_policy_destroy(&profile
->base
);
209 aa_put_profile(rcu_access_pointer(profile
->parent
));
211 aa_put_ns(profile
->ns
);
212 kzfree(profile
->rename
);
214 aa_free_file_rules(&profile
->file
);
215 aa_free_cap_rules(&profile
->caps
);
216 aa_free_net_rules(&profile
->net
);
217 aa_free_rlimit_rules(&profile
->rlimits
);
219 kzfree(profile
->dirname
);
220 aa_put_dfa(profile
->xmatch
);
221 aa_put_dfa(profile
->policy
.dfa
);
223 kzfree(profile
->hash
);
228 * aa_alloc_profile - allocate, initialize and return a new profile
229 * @hname: name of the profile (NOT NULL)
230 * @gfp: allocation type
232 * Returns: refcount profile or NULL on failure
234 struct aa_profile
*aa_alloc_profile(const char *hname
, struct aa_proxy
*proxy
,
237 struct aa_profile
*profile
;
239 /* freed by free_profile - usually through aa_put_profile */
240 profile
= kzalloc(sizeof(*profile
) + sizeof (struct aa_profile
*) * 2,
245 if (!aa_policy_init(&profile
->base
, NULL
, hname
, gfp
))
247 if (!aa_label_init(&profile
->label
, 1))
250 /* update being set needed by fs interface */
252 proxy
= aa_alloc_proxy(&profile
->label
, gfp
);
257 profile
->label
.proxy
= proxy
;
259 profile
->label
.hname
= profile
->base
.hname
;
260 profile
->label
.flags
|= FLAG_PROFILE
;
261 profile
->label
.vec
[0] = profile
;
263 /* refcount released by caller */
267 aa_free_profile(profile
);
273 * aa_null_profile - create or find a null-X learning profile
274 * @parent: profile that caused this profile to be created (NOT NULL)
275 * @hat: true if the null- learning profile is a hat
276 * @base: name to base the null profile off of
277 * @gfp: type of allocation
279 * Find/Create a null- complain mode profile used in learning mode. The
280 * name of the profile is unique and follows the format of parent//null-XXX.
281 * where XXX is based on the @name or if that fails or is not supplied
284 * null profiles are added to the profile list but the list does not
285 * hold a count on them so that they are automatically released when
288 * Returns: new refcounted profile else NULL on failure
290 struct aa_profile
*aa_null_profile(struct aa_profile
*parent
, bool hat
,
291 const char *base
, gfp_t gfp
)
293 struct aa_profile
*profile
;
299 name
= kmalloc(strlen(parent
->base
.hname
) + 8 + strlen(base
),
302 sprintf(name
, "%s//null-%s", parent
->base
.hname
, base
);
305 /* fall through to try shorter uniq */
308 name
= kmalloc(strlen(parent
->base
.hname
) + 2 + 7 + 8, gfp
);
311 sprintf(name
, "%s//null-%x", parent
->base
.hname
,
312 atomic_inc_return(&parent
->ns
->uniq_null
));
315 /* lookup to see if this is a dup creation */
316 profile
= aa_find_child(parent
, basename(name
));
320 profile
= aa_alloc_profile(name
, NULL
, gfp
);
324 profile
->mode
= APPARMOR_COMPLAIN
;
325 profile
->label
.flags
|= FLAG_NULL
;
327 profile
->label
.flags
|= FLAG_HAT
;
329 /* released on free_profile */
330 rcu_assign_pointer(profile
->parent
, aa_get_profile(parent
));
331 profile
->ns
= aa_get_ns(parent
->ns
);
332 profile
->file
.dfa
= aa_get_dfa(nulldfa
);
333 profile
->policy
.dfa
= aa_get_dfa(nulldfa
);
335 mutex_lock(&profile
->ns
->lock
);
336 __add_profile(&parent
->base
.profiles
, profile
);
337 mutex_unlock(&profile
->ns
->lock
);
339 /* refcount released by caller */
345 aa_free_profile(profile
);
350 * aa_setup_default_label - create the initial default label
352 struct aa_label
*aa_setup_default_label(void)
354 struct aa_profile
*profile
= aa_alloc_profile("default", NULL
,
359 /* the default profile pretends to be unconfined until it is replaced */
360 profile
->label
.flags
|= FLAG_IX_ON_NAME_ERROR
| FLAG_UNCONFINED
;
361 profile
->mode
= APPARMOR_UNCONFINED
;
363 profile
->ns
= aa_get_ns(root_ns
);
365 __add_profile(&root_ns
->base
.profiles
, profile
);
367 return &profile
->label
;
370 /* TODO: profile accounting - setup in remove */
373 * __find_child - find a profile on @head list with a name matching @name
374 * @head: list to search (NOT NULL)
375 * @name: name of profile (NOT NULL)
377 * Requires: rcu_read_lock be held
379 * Returns: unrefcounted profile ptr, or NULL if not found
381 static struct aa_profile
*__find_child(struct list_head
*head
, const char *name
)
383 return (struct aa_profile
*)__policy_find(head
, name
);
387 * __strn_find_child - find a profile on @head list using substring of @name
388 * @head: list to search (NOT NULL)
389 * @name: name of profile (NOT NULL)
390 * @len: length of @name substring to match
392 * Requires: rcu_read_lock be held
394 * Returns: unrefcounted profile ptr, or NULL if not found
396 static struct aa_profile
*__strn_find_child(struct list_head
*head
,
397 const char *name
, int len
)
399 return (struct aa_profile
*)__policy_strn_find(head
, name
, len
);
403 * aa_find_child - find a profile by @name in @parent
404 * @parent: profile to search (NOT NULL)
405 * @name: profile name to search for (NOT NULL)
407 * Returns: a refcounted profile or NULL if not found
409 struct aa_profile
*aa_find_child(struct aa_profile
*parent
, const char *name
)
411 struct aa_profile
*profile
;
415 profile
= __find_child(&parent
->base
.profiles
, name
);
416 } while (profile
&& !aa_get_profile_not0(profile
));
419 /* refcount released by caller */
424 * __lookup_parent - lookup the parent of a profile of name @hname
425 * @ns: namespace to lookup profile in (NOT NULL)
426 * @hname: hierarchical profile name to find parent of (NOT NULL)
428 * Lookups up the parent of a fully qualified profile name, the profile
429 * that matches hname does not need to exist, in general this
430 * is used to load a new profile.
432 * Requires: rcu_read_lock be held
434 * Returns: unrefcounted policy or NULL if not found
436 static struct aa_policy
*__lookup_parent(struct aa_ns
*ns
, const char *hname
)
438 struct aa_policy
*policy
;
439 struct aa_profile
*profile
= NULL
;
444 for (split
= strstr(hname
, "//"); split
;) {
445 profile
= __strn_find_child(&policy
->profiles
, hname
,
449 policy
= &profile
->base
;
451 split
= strstr(hname
, "//");
455 return &profile
->base
;
459 * __lookupn_profile - lookup the profile matching @hname
460 * @base: base list to start looking up profile name from (NOT NULL)
461 * @hname: hierarchical profile name (NOT NULL)
462 * @n: length of @hname
464 * Requires: rcu_read_lock be held
466 * Returns: unrefcounted profile pointer or NULL if not found
468 * Do a relative name lookup, recursing through profile tree.
470 static struct aa_profile
*__lookupn_profile(struct aa_policy
*base
,
471 const char *hname
, size_t n
)
473 struct aa_profile
*profile
= NULL
;
476 for (split
= strnstr(hname
, "//", n
); split
;
477 split
= strnstr(hname
, "//", n
)) {
478 profile
= __strn_find_child(&base
->profiles
, hname
,
483 base
= &profile
->base
;
484 n
-= split
+ 2 - hname
;
489 return __strn_find_child(&base
->profiles
, hname
, n
);
493 static struct aa_profile
*__lookup_profile(struct aa_policy
*base
,
496 return __lookupn_profile(base
, hname
, strlen(hname
));
500 * aa_lookup_profile - find a profile by its full or partial name
501 * @ns: the namespace to start from (NOT NULL)
502 * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL)
505 * Returns: refcounted profile or NULL if not found
507 struct aa_profile
*aa_lookupn_profile(struct aa_ns
*ns
, const char *hname
,
510 struct aa_profile
*profile
;
514 profile
= __lookupn_profile(&ns
->base
, hname
, n
);
515 } while (profile
&& !aa_get_profile_not0(profile
));
518 /* the unconfined profile is not in the regular profile list */
519 if (!profile
&& strncmp(hname
, "unconfined", n
) == 0)
520 profile
= aa_get_newest_profile(ns
->unconfined
);
522 /* refcount released by caller */
526 struct aa_profile
*aa_lookup_profile(struct aa_ns
*ns
, const char *hname
)
528 return aa_lookupn_profile(ns
, hname
, strlen(hname
));
531 struct aa_profile
*aa_fqlookupn_profile(struct aa_label
*base
,
532 const char *fqname
, size_t n
)
534 struct aa_profile
*profile
;
536 const char *name
, *ns_name
;
539 name
= aa_splitn_fqname(fqname
, n
, &ns_name
, &ns_len
);
541 ns
= aa_findn_ns(labels_ns(base
), ns_name
, ns_len
);
545 ns
= aa_get_ns(labels_ns(base
));
548 profile
= aa_lookupn_profile(ns
, name
, n
- (name
- fqname
));
550 /* default profile for ns, currently unconfined */
551 profile
= aa_get_newest_profile(ns
->unconfined
);
560 * replacement_allowed - test to see if replacement is allowed
561 * @profile: profile to test if it can be replaced (MAYBE NULL)
562 * @noreplace: true if replacement shouldn't be allowed but addition is okay
563 * @info: Returns - info about why replacement failed (NOT NULL)
565 * Returns: %0 if replacement allowed else error code
567 static int replacement_allowed(struct aa_profile
*profile
, int noreplace
,
571 if (profile
->label
.flags
& FLAG_IMMUTIBLE
) {
572 *info
= "cannot replace immutible profile";
574 } else if (noreplace
) {
575 *info
= "profile already exists";
582 /* audit callback for net specific fields */
583 static void audit_cb(struct audit_buffer
*ab
, void *va
)
585 struct common_audit_data
*sa
= va
;
587 if (aad(sa
)->iface
.ns
) {
588 audit_log_format(ab
, " ns=");
589 audit_log_untrustedstring(ab
, aad(sa
)->iface
.ns
);
594 * audit_policy - Do auditing of policy changes
595 * @label: label to check if it can manage policy
596 * @op: policy operation being performed
597 * @profile: name of profile being manipulated (NOT NULL)
598 * @info: any extra information to be audited (MAYBE NULL)
601 * Returns: the error to be returned after audit is done
603 static int audit_policy(struct aa_label
*label
, const char *op
,
604 const char *ns_name
, const char *name
,
605 const char *info
, int error
)
607 DEFINE_AUDIT_DATA(sa
, LSM_AUDIT_DATA_NONE
, op
);
608 // aad(&sa)->op = op;
609 aad(&sa
)->iface
.ns
= ns_name
;
610 aad(&sa
)->name
= name
;
611 aad(&sa
)->info
= info
;
612 aad(&sa
)->error
= error
;
613 aad(&sa
)->label
= label
;
615 aa_audit_msg(AUDIT_APPARMOR_STATUS
, &sa
, audit_cb
);
620 bool policy_view_capable(void)
622 struct user_namespace
*user_ns
= current_user_ns();
623 struct aa_ns
*ns
= aa_get_current_ns();
624 bool response
= false;
626 if (ns_capable(user_ns
, CAP_MAC_ADMIN
) &&
627 (user_ns
== &init_user_ns
||
628 (unprivileged_userns_apparmor_policy
!= 0 &&
629 user_ns
->level
== 1 && ns
!= root_ns
)))
636 bool policy_admin_capable(void)
638 return policy_view_capable() && !aa_g_lock_policy
;
641 bool aa_may_open_profiles(void)
643 struct user_namespace
*user_ns
= current_user_ns();
644 struct aa_ns
*ns
= aa_get_current_ns();
645 bool root_in_user_ns
= uid_eq(current_euid(), make_kuid(user_ns
, 0)) ||
646 in_egroup_p(make_kgid(user_ns
, 0));
647 bool response
= false;
649 if (root_in_user_ns
&&
650 (user_ns
== &init_user_ns
||
651 (unprivileged_userns_apparmor_policy
!= 0 &&
652 user_ns
->level
== 1 && ns
!= root_ns
)))
660 * aa_may_manage_policy - can the current task manage policy
661 * @label: label to check if it can manage policy
662 * @op: the policy manipulation operation being done
664 * Returns: 0 if the task is allowed to manipulate policy else error
666 int aa_may_manage_policy(struct aa_label
*label
, u32 mask
)
670 if (mask
& AA_MAY_REMOVE_POLICY
)
672 else if (mask
& AA_MAY_REPLACE_POLICY
)
677 /* check if loading policy is locked out */
678 if (aa_g_lock_policy
)
679 return audit_policy(label
, op
, NULL
, NULL
, "policy_locked",
682 if (!policy_admin_capable())
683 return audit_policy(label
, op
, NULL
, NULL
, "not policy admin",
686 /* TODO: add fine grained mediation of policy loads */
690 static struct aa_profile
*__list_lookup_parent(struct list_head
*lh
,
691 struct aa_profile
*profile
)
693 const char *base
= basename(profile
->base
.hname
);
694 long len
= base
- profile
->base
.hname
;
695 struct aa_load_ent
*ent
;
697 /* parent won't have trailing // so remove from len */
702 list_for_each_entry(ent
, lh
, list
) {
703 if (ent
->new == profile
)
705 if (strncmp(ent
->new->base
.hname
, profile
->base
.hname
, len
) ==
706 0 && ent
->new->base
.hname
[len
] == 0)
714 * __replace_profile - replace @old with @new on a list
715 * @old: profile to be replaced (NOT NULL)
716 * @new: profile to replace @old with (NOT NULL)
718 * Will duplicate and refcount elements that @new inherits from @old
719 * and will inherit @old children.
721 * refcount @new for list, put @old list refcount
723 * Requires: namespace list lock be held, or list not be shared
725 static void __replace_profile(struct aa_profile
*old
, struct aa_profile
*new)
727 struct aa_profile
*child
, *tmp
;
729 if (!list_empty(&old
->base
.profiles
)) {
731 list_splice_init_rcu(&old
->base
.profiles
, &lh
, synchronize_rcu
);
733 list_for_each_entry_safe(child
, tmp
, &lh
, base
.list
) {
734 struct aa_profile
*p
;
736 list_del_init(&child
->base
.list
);
737 p
= __find_child(&new->base
.profiles
, child
->base
.name
);
739 /* @p replaces @child */
740 __replace_profile(child
, p
);
744 /* inherit @child and its children */
745 /* TODO: update hname of inherited children */
746 /* list refcount transferred to @new */
747 p
= aa_deref_parent(child
);
748 rcu_assign_pointer(child
->parent
, aa_get_profile(new));
749 list_add_rcu(&child
->base
.list
, &new->base
.profiles
);
754 if (!rcu_access_pointer(new->parent
)) {
755 struct aa_profile
*parent
= aa_deref_parent(old
);
756 rcu_assign_pointer(new->parent
, aa_get_profile(parent
));
758 aa_label_replace(&old
->label
, &new->label
);
759 /* migrate dents must come after label replacement b/c update */
760 __aa_fs_profile_migrate_dents(old
, new);
762 if (list_empty(&new->base
.list
)) {
763 /* new is not on a list already */
764 list_replace_rcu(&old
->base
.list
, &new->base
.list
);
768 __list_remove_profile(old
);
772 * __lookup_replace - lookup replacement information for a profile
773 * @ns - namespace the lookup occurs in
774 * @hname - name of profile to lookup
775 * @noreplace - true if not replacing an existing profile
776 * @p - Returns: profile to be replaced
777 * @info - Returns: info string on why lookup failed
779 * Returns: profile to replace (no ref) on success else ptr error
781 static int __lookup_replace(struct aa_ns
*ns
, const char *hname
,
782 bool noreplace
, struct aa_profile
**p
,
785 *p
= aa_get_profile(__lookup_profile(&ns
->base
, hname
));
787 int error
= replacement_allowed(*p
, noreplace
, info
);
789 *info
= "profile can not be replaced";
797 static void share_name(struct aa_profile
*old
, struct aa_profile
*new)
799 aa_put_str(new->base
.hname
);
800 aa_get_str(old
->base
.hname
);
801 new->base
.hname
= old
->base
.hname
;
802 new->base
.name
= old
->base
.name
;
803 new->label
.hname
= old
->label
.hname
;
806 /* Update to newest version of parent after previous replacements
807 * Returns: unrefcount newest version of parent
809 static struct aa_profile
*update_to_newest_parent(struct aa_profile
*new)
811 struct aa_profile
*parent
, *newest
;
812 parent
= rcu_dereference_protected(new->parent
,
813 mutex_is_locked(&new->ns
->lock
));
814 newest
= aa_get_newest_profile(parent
);
816 /* parent replaced in this atomic set? */
817 if (newest
!= parent
) {
818 aa_put_profile(parent
);
819 rcu_assign_pointer(new->parent
, newest
);
821 aa_put_profile(newest
);
827 * aa_replace_profiles - replace profile(s) on the profile list
828 * @label: label that is attempting to load/replace policy
829 * @mask: permission mask
830 * @udata: serialized data stream (NOT NULL)
831 * @size: size of the serialized data stream
833 * unpack and replace a profile on the profile list and uses of that profile
834 * by any aa_task_ctx. If the profile does not exist on the profile list
837 * Returns: size of data consumed else error code on failure.
839 ssize_t
aa_replace_profiles(struct aa_label
*label
, u32 mask
, void *udata
,
842 const char *ns_name
, *info
= NULL
;
843 struct aa_ns
*ns
= NULL
;
844 struct aa_load_ent
*ent
, *tmp
;
845 const char *op
= mask
& AA_MAY_REPLACE_POLICY
? OP_PROF_REPL
: OP_PROF_LOAD
;
846 ssize_t count
, error
;
851 error
= aa_unpack(udata
, size
, &lh
, &ns_name
);
855 /* ensure that profiles are all for the same ns
856 * TODO: update locking to remove this constaint. All profiles in
857 * the load set must succeed as a set or the load will
858 * fail. Sort ent list and take ns locks in hierarchy order
861 list_for_each_entry(ent
, &lh
, list
) {
864 strcmp(ent
->ns_name
, ns_name
) != 0) {
865 info
= "policy load has mixed namespaces";
869 } else if (ent
->ns_name
) {
871 info
= "policy load has mixed namespaces";
875 ns_name
= ent
->ns_name
;
880 ns
= aa_prepare_ns(labels_ns(label
), ns_name
);
882 info
= "failed to prepare namespace";
887 ns
= aa_get_ns(labels_ns(label
));
889 mutex_lock(&ns
->lock
);
890 /* setup parent and ns info */
891 list_for_each_entry(ent
, &lh
, list
) {
892 struct aa_policy
*policy
;
894 error
= __lookup_replace(ns
, ent
->new->base
.hname
,
895 !(mask
& AA_MAY_REPLACE_POLICY
),
900 if (ent
->new->rename
) {
901 error
= __lookup_replace(ns
, ent
->new->rename
,
902 !(mask
& AA_MAY_REPLACE_POLICY
),
903 &ent
->rename
, &info
);
908 /* released when @new is freed */
909 ent
->new->ns
= aa_get_ns(ns
);
911 if (ent
->old
|| ent
->rename
)
914 /* no ref on policy only use inside lock */
915 policy
= __lookup_parent(ns
, ent
->new->base
.hname
);
917 struct aa_profile
*p
;
918 p
= __list_lookup_parent(&lh
, ent
->new);
921 info
= "parent does not exist";
924 rcu_assign_pointer(ent
->new->parent
, aa_get_profile(p
));
925 } else if (policy
!= &ns
->base
) {
926 /* released on profile replacement or free_profile */
927 struct aa_profile
*p
= (struct aa_profile
*) policy
;
928 rcu_assign_pointer(ent
->new->parent
, aa_get_profile(p
));
932 /* create new fs entries for introspection if needed */
933 list_for_each_entry(ent
, &lh
, list
) {
935 struct dentry
*parent
;
936 if (rcu_access_pointer(ent
->new->parent
)) {
937 struct aa_profile
*p
;
938 p
= aa_deref_parent(ent
->new);
939 parent
= prof_child_dir(p
);
941 parent
= ns_subprofs_dir(ent
->new->ns
);
942 error
= __aa_fs_profile_mkdir(ent
->new, parent
);
946 info
= "failed to create";
951 /* Done with checks that may fail - do actual replacement */
952 list_for_each_entry_safe(ent
, tmp
, &lh
, list
) {
953 list_del_init(&ent
->list
);
954 op
= (!ent
->old
&& !ent
->rename
) ? OP_PROF_LOAD
: OP_PROF_REPL
;
956 audit_policy(label
, op
, ns_name
, ent
->new->base
.hname
, NULL
, error
);
959 share_name(ent
->old
, ent
->new);
960 __replace_profile(ent
->old
, ent
->new);
962 __replace_profile(ent
->rename
, ent
->new);
963 } else if (ent
->rename
) {
964 /* TODO: case not actually supported yet */
967 struct list_head
*lh
;
968 if (rcu_access_pointer(ent
->new->parent
)) {
969 struct aa_profile
*parent
;
970 parent
= update_to_newest_parent(ent
->new);
971 lh
= &parent
->base
.profiles
;
973 lh
= &ns
->base
.profiles
;
974 __add_profile(lh
, ent
->new);
976 aa_load_ent_free(ent
);
978 __aa_labelset_update_subtree(ns
);
979 mutex_unlock(&ns
->lock
);
989 mutex_unlock(&ns
->lock
);
991 /* audit cause of failure */
992 op
= (!ent
->old
) ? OP_PROF_LOAD
: OP_PROF_REPL
;
994 audit_policy(label
, op
, ns_name
, ent
->new->base
.hname
, info
, error
);
995 /* audit status that rest of profiles in the atomic set failed too */
996 info
= "valid profile in failed atomic policy load";
997 list_for_each_entry(tmp
, &lh
, list
) {
999 info
= "unchecked profile in failed atomic policy load";
1000 /* skip entry that caused failure */
1003 op
= (!ent
->old
) ? OP_PROF_LOAD
: OP_PROF_REPL
;
1004 audit_policy(label
, op
, ns_name
, tmp
->new->base
.hname
, info
, error
);
1006 list_for_each_entry_safe(ent
, tmp
, &lh
, list
) {
1007 list_del_init(&ent
->list
);
1008 aa_load_ent_free(ent
);
1015 * aa_remove_profiles - remove profile(s) from the system
1016 * @label: label attempting to remove policy
1017 * @fqname: name of the profile or namespace to remove (NOT NULL)
1018 * @size: size of the name
1020 * Remove a profile or sub namespace from the current namespace, so that
1021 * they can not be found anymore and mark them as replaced by unconfined
1023 * NOTE: removing confinement does not restore rlimits to preconfinemnet values
1025 * Returns: size of data consume else error code if fails
1027 ssize_t
aa_remove_profiles(struct aa_label
*label
, char *fqname
, size_t size
)
1029 struct aa_ns
*root
= NULL
, *ns
= NULL
;
1030 struct aa_profile
*profile
= NULL
;
1031 const char *name
= fqname
, *info
= NULL
;
1032 char *ns_name
= NULL
;
1036 info
= "no profile specified";
1041 root
= labels_ns(label
);
1043 if (fqname
[0] == ':') {
1044 name
= aa_split_fqname(fqname
, &ns_name
);
1045 /* released below */
1046 ns
= aa_find_ns(root
, ns_name
);
1048 info
= "namespace does not exist";
1053 /* released below */
1054 ns
= aa_get_ns(root
);
1057 /* remove namespace - can only happen if fqname[0] == ':' */
1058 mutex_lock(&ns
->parent
->lock
);
1060 mutex_unlock(&ns
->parent
->lock
);
1062 /* remove profile */
1063 mutex_lock(&ns
->lock
);
1064 profile
= aa_get_profile(__lookup_profile(&ns
->base
, name
));
1067 info
= "profile does not exist";
1070 name
= profile
->base
.hname
;
1071 __remove_profile(profile
);
1072 __aa_labelset_update_subtree(ns
);
1073 mutex_unlock(&ns
->lock
);
1076 /* don't fail removal if audit fails */
1077 (void) audit_policy(label
, OP_PROF_RM
, ns_name
, name
, info
, error
);
1078 aa_put_profile(profile
);
1082 mutex_unlock(&ns
->lock
);
1085 (void) audit_policy(label
, OP_PROF_RM
, ns_name
, name
, info
, error
);