1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * AppArmor security module
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This file contains AppArmor policy manipulation functions
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 1998-2008 Novell/SUSE
8*4882a593Smuzhiyun * Copyright 2009-2010 Canonical Ltd.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * AppArmor policy is based around profiles, which contain the rules a
11*4882a593Smuzhiyun * task is confined by. Every task in the system has a profile attached
12*4882a593Smuzhiyun * to it determined either by matching "unconfined" tasks against the
13*4882a593Smuzhiyun * visible set of profiles or by following a profiles attachment rules.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * Each profile exists in a profile namespace which is a container of
16*4882a593Smuzhiyun * visible profiles. Each namespace contains a special "unconfined" profile,
17*4882a593Smuzhiyun * which doesn't enforce any confinement on a task beyond DAC.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * Namespace and profile names can be written together in either
20*4882a593Smuzhiyun * of two syntaxes.
21*4882a593Smuzhiyun * :namespace:profile - used by kernel interfaces for easy detection
22*4882a593Smuzhiyun * namespace://profile - used by policy
23*4882a593Smuzhiyun *
24*4882a593Smuzhiyun * Profile names can not start with : or @ or ^ and may not contain \0
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * Reserved profile names
27*4882a593Smuzhiyun * unconfined - special automatically generated unconfined profile
28*4882a593Smuzhiyun * inherit - special name to indicate profile inheritance
29*4882a593Smuzhiyun * null-XXXX-YYYY - special automatically generated learning profiles
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * Namespace names may not start with / or @ and may not contain \0 or :
32*4882a593Smuzhiyun * Reserved namespace names
33*4882a593Smuzhiyun * user-XXXX - user defined profiles
34*4882a593Smuzhiyun *
35*4882a593Smuzhiyun * a // in a profile or namespace name indicates a hierarchical name with the
36*4882a593Smuzhiyun * name before the // being the parent and the name after the child.
37*4882a593Smuzhiyun *
38*4882a593Smuzhiyun * Profile and namespace hierarchies serve two different but similar purposes.
39*4882a593Smuzhiyun * The namespace contains the set of visible profiles that are considered
40*4882a593Smuzhiyun * for attachment. The hierarchy of namespaces allows for virtualizing
41*4882a593Smuzhiyun * the namespace so that for example a chroot can have its own set of profiles
42*4882a593Smuzhiyun * which may define some local user namespaces.
43*4882a593Smuzhiyun * The profile hierarchy severs two distinct purposes,
44*4882a593Smuzhiyun * - it allows for sub profiles or hats, which allows an application to run
45*4882a593Smuzhiyun * subprograms under its own profile with different restriction than it
46*4882a593Smuzhiyun * self, and not have it use the system profile.
47*4882a593Smuzhiyun * eg. if a mail program starts an editor, the policy might make the
48*4882a593Smuzhiyun * restrictions tighter on the editor tighter than the mail program,
49*4882a593Smuzhiyun * and definitely different than general editor restrictions
50*4882a593Smuzhiyun * - it allows for binary hierarchy of profiles, so that execution history
51*4882a593Smuzhiyun * is preserved. This feature isn't exploited by AppArmor reference policy
52*4882a593Smuzhiyun * but is allowed. NOTE: this is currently suboptimal because profile
53*4882a593Smuzhiyun * aliasing is not currently implemented so that a profile for each
54*4882a593Smuzhiyun * level must be defined.
55*4882a593Smuzhiyun * eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started
56*4882a593Smuzhiyun * from /bin/bash
57*4882a593Smuzhiyun *
58*4882a593Smuzhiyun * A profile or namespace name that can contain one or more // separators
59*4882a593Smuzhiyun * is referred to as an hname (hierarchical).
60*4882a593Smuzhiyun * eg. /bin/bash//bin/ls
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * An fqname is a name that may contain both namespace and profile hnames.
63*4882a593Smuzhiyun * eg. :ns:/bin/bash//bin/ls
64*4882a593Smuzhiyun *
65*4882a593Smuzhiyun * NOTES:
66*4882a593Smuzhiyun * - locking of profile lists is currently fairly coarse. All profile
67*4882a593Smuzhiyun * lists within a namespace use the namespace lock.
68*4882a593Smuzhiyun * FIXME: move profile lists to using rcu_lists
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun #include <linux/slab.h>
72*4882a593Smuzhiyun #include <linux/spinlock.h>
73*4882a593Smuzhiyun #include <linux/string.h>
74*4882a593Smuzhiyun #include <linux/cred.h>
75*4882a593Smuzhiyun #include <linux/rculist.h>
76*4882a593Smuzhiyun #include <linux/user_namespace.h>
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #include "include/apparmor.h"
79*4882a593Smuzhiyun #include "include/capability.h"
80*4882a593Smuzhiyun #include "include/cred.h"
81*4882a593Smuzhiyun #include "include/file.h"
82*4882a593Smuzhiyun #include "include/ipc.h"
83*4882a593Smuzhiyun #include "include/match.h"
84*4882a593Smuzhiyun #include "include/path.h"
85*4882a593Smuzhiyun #include "include/policy.h"
86*4882a593Smuzhiyun #include "include/policy_ns.h"
87*4882a593Smuzhiyun #include "include/policy_unpack.h"
88*4882a593Smuzhiyun #include "include/resource.h"
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun int unprivileged_userns_apparmor_policy = 1;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun const char *const aa_profile_mode_names[] = {
93*4882a593Smuzhiyun "enforce",
94*4882a593Smuzhiyun "complain",
95*4882a593Smuzhiyun "kill",
96*4882a593Smuzhiyun "unconfined",
97*4882a593Smuzhiyun };
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /**
101*4882a593Smuzhiyun * __add_profile - add a profiles to list and label tree
102*4882a593Smuzhiyun * @list: list to add it to (NOT NULL)
103*4882a593Smuzhiyun * @profile: the profile to add (NOT NULL)
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * refcount @profile, should be put by __list_remove_profile
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Requires: namespace lock be held, or list not be shared
108*4882a593Smuzhiyun */
__add_profile(struct list_head * list,struct aa_profile * profile)109*4882a593Smuzhiyun static void __add_profile(struct list_head *list, struct aa_profile *profile)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun struct aa_label *l;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun AA_BUG(!list);
114*4882a593Smuzhiyun AA_BUG(!profile);
115*4882a593Smuzhiyun AA_BUG(!profile->ns);
116*4882a593Smuzhiyun AA_BUG(!mutex_is_locked(&profile->ns->lock));
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun list_add_rcu(&profile->base.list, list);
119*4882a593Smuzhiyun /* get list reference */
120*4882a593Smuzhiyun aa_get_profile(profile);
121*4882a593Smuzhiyun l = aa_label_insert(&profile->ns->labels, &profile->label);
122*4882a593Smuzhiyun AA_BUG(l != &profile->label);
123*4882a593Smuzhiyun aa_put_label(l);
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun /**
127*4882a593Smuzhiyun * __list_remove_profile - remove a profile from the list it is on
128*4882a593Smuzhiyun * @profile: the profile to remove (NOT NULL)
129*4882a593Smuzhiyun *
130*4882a593Smuzhiyun * remove a profile from the list, warning generally removal should
131*4882a593Smuzhiyun * be done with __replace_profile as most profile removals are
132*4882a593Smuzhiyun * replacements to the unconfined profile.
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * put @profile list refcount
135*4882a593Smuzhiyun *
136*4882a593Smuzhiyun * Requires: namespace lock be held, or list not have been live
137*4882a593Smuzhiyun */
__list_remove_profile(struct aa_profile * profile)138*4882a593Smuzhiyun static void __list_remove_profile(struct aa_profile *profile)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun AA_BUG(!profile);
141*4882a593Smuzhiyun AA_BUG(!profile->ns);
142*4882a593Smuzhiyun AA_BUG(!mutex_is_locked(&profile->ns->lock));
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun list_del_rcu(&profile->base.list);
145*4882a593Smuzhiyun aa_put_profile(profile);
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /**
149*4882a593Smuzhiyun * __remove_profile - remove old profile, and children
150*4882a593Smuzhiyun * @profile: profile to be replaced (NOT NULL)
151*4882a593Smuzhiyun *
152*4882a593Smuzhiyun * Requires: namespace list lock be held, or list not be shared
153*4882a593Smuzhiyun */
__remove_profile(struct aa_profile * profile)154*4882a593Smuzhiyun static void __remove_profile(struct aa_profile *profile)
155*4882a593Smuzhiyun {
156*4882a593Smuzhiyun AA_BUG(!profile);
157*4882a593Smuzhiyun AA_BUG(!profile->ns);
158*4882a593Smuzhiyun AA_BUG(!mutex_is_locked(&profile->ns->lock));
159*4882a593Smuzhiyun
160*4882a593Smuzhiyun /* release any children lists first */
161*4882a593Smuzhiyun __aa_profile_list_release(&profile->base.profiles);
162*4882a593Smuzhiyun /* released by free_profile */
163*4882a593Smuzhiyun aa_label_remove(&profile->label);
164*4882a593Smuzhiyun __aafs_profile_rmdir(profile);
165*4882a593Smuzhiyun __list_remove_profile(profile);
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /**
169*4882a593Smuzhiyun * __aa_profile_list_release - remove all profiles on the list and put refs
170*4882a593Smuzhiyun * @head: list of profiles (NOT NULL)
171*4882a593Smuzhiyun *
172*4882a593Smuzhiyun * Requires: namespace lock be held
173*4882a593Smuzhiyun */
__aa_profile_list_release(struct list_head * head)174*4882a593Smuzhiyun void __aa_profile_list_release(struct list_head *head)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun struct aa_profile *profile, *tmp;
177*4882a593Smuzhiyun list_for_each_entry_safe(profile, tmp, head, base.list)
178*4882a593Smuzhiyun __remove_profile(profile);
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun * aa_free_data - free a data blob
183*4882a593Smuzhiyun * @ptr: data to free
184*4882a593Smuzhiyun * @arg: unused
185*4882a593Smuzhiyun */
aa_free_data(void * ptr,void * arg)186*4882a593Smuzhiyun static void aa_free_data(void *ptr, void *arg)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun struct aa_data *data = ptr;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun kfree_sensitive(data->data);
191*4882a593Smuzhiyun kfree_sensitive(data->key);
192*4882a593Smuzhiyun kfree_sensitive(data);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /**
196*4882a593Smuzhiyun * aa_free_profile - free a profile
197*4882a593Smuzhiyun * @profile: the profile to free (MAYBE NULL)
198*4882a593Smuzhiyun *
199*4882a593Smuzhiyun * Free a profile, its hats and null_profile. All references to the profile,
200*4882a593Smuzhiyun * its hats and null_profile must have been put.
201*4882a593Smuzhiyun *
202*4882a593Smuzhiyun * If the profile was referenced from a task context, free_profile() will
203*4882a593Smuzhiyun * be called from an rcu callback routine, so we must not sleep here.
204*4882a593Smuzhiyun */
aa_free_profile(struct aa_profile * profile)205*4882a593Smuzhiyun void aa_free_profile(struct aa_profile *profile)
206*4882a593Smuzhiyun {
207*4882a593Smuzhiyun struct rhashtable *rht;
208*4882a593Smuzhiyun int i;
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun AA_DEBUG("%s(%p)\n", __func__, profile);
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun if (!profile)
213*4882a593Smuzhiyun return;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* free children profiles */
216*4882a593Smuzhiyun aa_policy_destroy(&profile->base);
217*4882a593Smuzhiyun aa_put_profile(rcu_access_pointer(profile->parent));
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun aa_put_ns(profile->ns);
220*4882a593Smuzhiyun kfree_sensitive(profile->rename);
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun aa_free_file_rules(&profile->file);
223*4882a593Smuzhiyun aa_free_cap_rules(&profile->caps);
224*4882a593Smuzhiyun aa_free_rlimit_rules(&profile->rlimits);
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun for (i = 0; i < profile->xattr_count; i++)
227*4882a593Smuzhiyun kfree_sensitive(profile->xattrs[i]);
228*4882a593Smuzhiyun kfree_sensitive(profile->xattrs);
229*4882a593Smuzhiyun for (i = 0; i < profile->secmark_count; i++)
230*4882a593Smuzhiyun kfree_sensitive(profile->secmark[i].label);
231*4882a593Smuzhiyun kfree_sensitive(profile->secmark);
232*4882a593Smuzhiyun kfree_sensitive(profile->dirname);
233*4882a593Smuzhiyun aa_put_dfa(profile->xmatch);
234*4882a593Smuzhiyun aa_put_dfa(profile->policy.dfa);
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun if (profile->data) {
237*4882a593Smuzhiyun rht = profile->data;
238*4882a593Smuzhiyun profile->data = NULL;
239*4882a593Smuzhiyun rhashtable_free_and_destroy(rht, aa_free_data, NULL);
240*4882a593Smuzhiyun kfree_sensitive(rht);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun kfree_sensitive(profile->hash);
244*4882a593Smuzhiyun aa_put_loaddata(profile->rawdata);
245*4882a593Smuzhiyun aa_label_destroy(&profile->label);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun kfree_sensitive(profile);
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /**
251*4882a593Smuzhiyun * aa_alloc_profile - allocate, initialize and return a new profile
252*4882a593Smuzhiyun * @hname: name of the profile (NOT NULL)
253*4882a593Smuzhiyun * @gfp: allocation type
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun * Returns: refcount profile or NULL on failure
256*4882a593Smuzhiyun */
aa_alloc_profile(const char * hname,struct aa_proxy * proxy,gfp_t gfp)257*4882a593Smuzhiyun struct aa_profile *aa_alloc_profile(const char *hname, struct aa_proxy *proxy,
258*4882a593Smuzhiyun gfp_t gfp)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun struct aa_profile *profile;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun /* freed by free_profile - usually through aa_put_profile */
263*4882a593Smuzhiyun profile = kzalloc(sizeof(*profile) + sizeof(struct aa_profile *) * 2,
264*4882a593Smuzhiyun gfp);
265*4882a593Smuzhiyun if (!profile)
266*4882a593Smuzhiyun return NULL;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun if (!aa_policy_init(&profile->base, NULL, hname, gfp))
269*4882a593Smuzhiyun goto fail;
270*4882a593Smuzhiyun if (!aa_label_init(&profile->label, 1, gfp))
271*4882a593Smuzhiyun goto fail;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /* update being set needed by fs interface */
274*4882a593Smuzhiyun if (!proxy) {
275*4882a593Smuzhiyun proxy = aa_alloc_proxy(&profile->label, gfp);
276*4882a593Smuzhiyun if (!proxy)
277*4882a593Smuzhiyun goto fail;
278*4882a593Smuzhiyun } else
279*4882a593Smuzhiyun aa_get_proxy(proxy);
280*4882a593Smuzhiyun profile->label.proxy = proxy;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun profile->label.hname = profile->base.hname;
283*4882a593Smuzhiyun profile->label.flags |= FLAG_PROFILE;
284*4882a593Smuzhiyun profile->label.vec[0] = profile;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun /* refcount released by caller */
287*4882a593Smuzhiyun return profile;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun fail:
290*4882a593Smuzhiyun aa_free_profile(profile);
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun return NULL;
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* TODO: profile accounting - setup in remove */
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun /**
298*4882a593Smuzhiyun * __strn_find_child - find a profile on @head list using substring of @name
299*4882a593Smuzhiyun * @head: list to search (NOT NULL)
300*4882a593Smuzhiyun * @name: name of profile (NOT NULL)
301*4882a593Smuzhiyun * @len: length of @name substring to match
302*4882a593Smuzhiyun *
303*4882a593Smuzhiyun * Requires: rcu_read_lock be held
304*4882a593Smuzhiyun *
305*4882a593Smuzhiyun * Returns: unrefcounted profile ptr, or NULL if not found
306*4882a593Smuzhiyun */
__strn_find_child(struct list_head * head,const char * name,int len)307*4882a593Smuzhiyun static struct aa_profile *__strn_find_child(struct list_head *head,
308*4882a593Smuzhiyun const char *name, int len)
309*4882a593Smuzhiyun {
310*4882a593Smuzhiyun return (struct aa_profile *)__policy_strn_find(head, name, len);
311*4882a593Smuzhiyun }
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun /**
314*4882a593Smuzhiyun * __find_child - find a profile on @head list with a name matching @name
315*4882a593Smuzhiyun * @head: list to search (NOT NULL)
316*4882a593Smuzhiyun * @name: name of profile (NOT NULL)
317*4882a593Smuzhiyun *
318*4882a593Smuzhiyun * Requires: rcu_read_lock be held
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * Returns: unrefcounted profile ptr, or NULL if not found
321*4882a593Smuzhiyun */
__find_child(struct list_head * head,const char * name)322*4882a593Smuzhiyun static struct aa_profile *__find_child(struct list_head *head, const char *name)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun return __strn_find_child(head, name, strlen(name));
325*4882a593Smuzhiyun }
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun /**
328*4882a593Smuzhiyun * aa_find_child - find a profile by @name in @parent
329*4882a593Smuzhiyun * @parent: profile to search (NOT NULL)
330*4882a593Smuzhiyun * @name: profile name to search for (NOT NULL)
331*4882a593Smuzhiyun *
332*4882a593Smuzhiyun * Returns: a refcounted profile or NULL if not found
333*4882a593Smuzhiyun */
aa_find_child(struct aa_profile * parent,const char * name)334*4882a593Smuzhiyun struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
335*4882a593Smuzhiyun {
336*4882a593Smuzhiyun struct aa_profile *profile;
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun rcu_read_lock();
339*4882a593Smuzhiyun do {
340*4882a593Smuzhiyun profile = __find_child(&parent->base.profiles, name);
341*4882a593Smuzhiyun } while (profile && !aa_get_profile_not0(profile));
342*4882a593Smuzhiyun rcu_read_unlock();
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun /* refcount released by caller */
345*4882a593Smuzhiyun return profile;
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /**
349*4882a593Smuzhiyun * __lookup_parent - lookup the parent of a profile of name @hname
350*4882a593Smuzhiyun * @ns: namespace to lookup profile in (NOT NULL)
351*4882a593Smuzhiyun * @hname: hierarchical profile name to find parent of (NOT NULL)
352*4882a593Smuzhiyun *
353*4882a593Smuzhiyun * Lookups up the parent of a fully qualified profile name, the profile
354*4882a593Smuzhiyun * that matches hname does not need to exist, in general this
355*4882a593Smuzhiyun * is used to load a new profile.
356*4882a593Smuzhiyun *
357*4882a593Smuzhiyun * Requires: rcu_read_lock be held
358*4882a593Smuzhiyun *
359*4882a593Smuzhiyun * Returns: unrefcounted policy or NULL if not found
360*4882a593Smuzhiyun */
__lookup_parent(struct aa_ns * ns,const char * hname)361*4882a593Smuzhiyun static struct aa_policy *__lookup_parent(struct aa_ns *ns,
362*4882a593Smuzhiyun const char *hname)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun struct aa_policy *policy;
365*4882a593Smuzhiyun struct aa_profile *profile = NULL;
366*4882a593Smuzhiyun char *split;
367*4882a593Smuzhiyun
368*4882a593Smuzhiyun policy = &ns->base;
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun for (split = strstr(hname, "//"); split;) {
371*4882a593Smuzhiyun profile = __strn_find_child(&policy->profiles, hname,
372*4882a593Smuzhiyun split - hname);
373*4882a593Smuzhiyun if (!profile)
374*4882a593Smuzhiyun return NULL;
375*4882a593Smuzhiyun policy = &profile->base;
376*4882a593Smuzhiyun hname = split + 2;
377*4882a593Smuzhiyun split = strstr(hname, "//");
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun if (!profile)
380*4882a593Smuzhiyun return &ns->base;
381*4882a593Smuzhiyun return &profile->base;
382*4882a593Smuzhiyun }
383*4882a593Smuzhiyun
384*4882a593Smuzhiyun /**
385*4882a593Smuzhiyun * __lookupn_profile - lookup the profile matching @hname
386*4882a593Smuzhiyun * @base: base list to start looking up profile name from (NOT NULL)
387*4882a593Smuzhiyun * @hname: hierarchical profile name (NOT NULL)
388*4882a593Smuzhiyun * @n: length of @hname
389*4882a593Smuzhiyun *
390*4882a593Smuzhiyun * Requires: rcu_read_lock be held
391*4882a593Smuzhiyun *
392*4882a593Smuzhiyun * Returns: unrefcounted profile pointer or NULL if not found
393*4882a593Smuzhiyun *
394*4882a593Smuzhiyun * Do a relative name lookup, recursing through profile tree.
395*4882a593Smuzhiyun */
__lookupn_profile(struct aa_policy * base,const char * hname,size_t n)396*4882a593Smuzhiyun static struct aa_profile *__lookupn_profile(struct aa_policy *base,
397*4882a593Smuzhiyun const char *hname, size_t n)
398*4882a593Smuzhiyun {
399*4882a593Smuzhiyun struct aa_profile *profile = NULL;
400*4882a593Smuzhiyun const char *split;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun for (split = strnstr(hname, "//", n); split;
403*4882a593Smuzhiyun split = strnstr(hname, "//", n)) {
404*4882a593Smuzhiyun profile = __strn_find_child(&base->profiles, hname,
405*4882a593Smuzhiyun split - hname);
406*4882a593Smuzhiyun if (!profile)
407*4882a593Smuzhiyun return NULL;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun base = &profile->base;
410*4882a593Smuzhiyun n -= split + 2 - hname;
411*4882a593Smuzhiyun hname = split + 2;
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun if (n)
415*4882a593Smuzhiyun return __strn_find_child(&base->profiles, hname, n);
416*4882a593Smuzhiyun return NULL;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
__lookup_profile(struct aa_policy * base,const char * hname)419*4882a593Smuzhiyun static struct aa_profile *__lookup_profile(struct aa_policy *base,
420*4882a593Smuzhiyun const char *hname)
421*4882a593Smuzhiyun {
422*4882a593Smuzhiyun return __lookupn_profile(base, hname, strlen(hname));
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun /**
426*4882a593Smuzhiyun * aa_lookup_profile - find a profile by its full or partial name
427*4882a593Smuzhiyun * @ns: the namespace to start from (NOT NULL)
428*4882a593Smuzhiyun * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL)
429*4882a593Smuzhiyun * @n: size of @hname
430*4882a593Smuzhiyun *
431*4882a593Smuzhiyun * Returns: refcounted profile or NULL if not found
432*4882a593Smuzhiyun */
aa_lookupn_profile(struct aa_ns * ns,const char * hname,size_t n)433*4882a593Smuzhiyun struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
434*4882a593Smuzhiyun size_t n)
435*4882a593Smuzhiyun {
436*4882a593Smuzhiyun struct aa_profile *profile;
437*4882a593Smuzhiyun
438*4882a593Smuzhiyun rcu_read_lock();
439*4882a593Smuzhiyun do {
440*4882a593Smuzhiyun profile = __lookupn_profile(&ns->base, hname, n);
441*4882a593Smuzhiyun } while (profile && !aa_get_profile_not0(profile));
442*4882a593Smuzhiyun rcu_read_unlock();
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun /* the unconfined profile is not in the regular profile list */
445*4882a593Smuzhiyun if (!profile && strncmp(hname, "unconfined", n) == 0)
446*4882a593Smuzhiyun profile = aa_get_newest_profile(ns->unconfined);
447*4882a593Smuzhiyun
448*4882a593Smuzhiyun /* refcount released by caller */
449*4882a593Smuzhiyun return profile;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun
aa_lookup_profile(struct aa_ns * ns,const char * hname)452*4882a593Smuzhiyun struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *hname)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun return aa_lookupn_profile(ns, hname, strlen(hname));
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
aa_fqlookupn_profile(struct aa_label * base,const char * fqname,size_t n)457*4882a593Smuzhiyun struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
458*4882a593Smuzhiyun const char *fqname, size_t n)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun struct aa_profile *profile;
461*4882a593Smuzhiyun struct aa_ns *ns;
462*4882a593Smuzhiyun const char *name, *ns_name;
463*4882a593Smuzhiyun size_t ns_len;
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
466*4882a593Smuzhiyun if (ns_name) {
467*4882a593Smuzhiyun ns = aa_lookupn_ns(labels_ns(base), ns_name, ns_len);
468*4882a593Smuzhiyun if (!ns)
469*4882a593Smuzhiyun return NULL;
470*4882a593Smuzhiyun } else
471*4882a593Smuzhiyun ns = aa_get_ns(labels_ns(base));
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun if (name)
474*4882a593Smuzhiyun profile = aa_lookupn_profile(ns, name, n - (name - fqname));
475*4882a593Smuzhiyun else if (ns)
476*4882a593Smuzhiyun /* default profile for ns, currently unconfined */
477*4882a593Smuzhiyun profile = aa_get_newest_profile(ns->unconfined);
478*4882a593Smuzhiyun else
479*4882a593Smuzhiyun profile = NULL;
480*4882a593Smuzhiyun aa_put_ns(ns);
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun return profile;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun /**
486*4882a593Smuzhiyun * aa_new_null_profile - create or find a null-X learning profile
487*4882a593Smuzhiyun * @parent: profile that caused this profile to be created (NOT NULL)
488*4882a593Smuzhiyun * @hat: true if the null- learning profile is a hat
489*4882a593Smuzhiyun * @base: name to base the null profile off of
490*4882a593Smuzhiyun * @gfp: type of allocation
491*4882a593Smuzhiyun *
492*4882a593Smuzhiyun * Find/Create a null- complain mode profile used in learning mode. The
493*4882a593Smuzhiyun * name of the profile is unique and follows the format of parent//null-XXX.
494*4882a593Smuzhiyun * where XXX is based on the @name or if that fails or is not supplied
495*4882a593Smuzhiyun * a unique number
496*4882a593Smuzhiyun *
497*4882a593Smuzhiyun * null profiles are added to the profile list but the list does not
498*4882a593Smuzhiyun * hold a count on them so that they are automatically released when
499*4882a593Smuzhiyun * not in use.
500*4882a593Smuzhiyun *
501*4882a593Smuzhiyun * Returns: new refcounted profile else NULL on failure
502*4882a593Smuzhiyun */
aa_new_null_profile(struct aa_profile * parent,bool hat,const char * base,gfp_t gfp)503*4882a593Smuzhiyun struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
504*4882a593Smuzhiyun const char *base, gfp_t gfp)
505*4882a593Smuzhiyun {
506*4882a593Smuzhiyun struct aa_profile *p, *profile;
507*4882a593Smuzhiyun const char *bname;
508*4882a593Smuzhiyun char *name = NULL;
509*4882a593Smuzhiyun
510*4882a593Smuzhiyun AA_BUG(!parent);
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun if (base) {
513*4882a593Smuzhiyun name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
514*4882a593Smuzhiyun gfp);
515*4882a593Smuzhiyun if (name) {
516*4882a593Smuzhiyun sprintf(name, "%s//null-%s", parent->base.hname, base);
517*4882a593Smuzhiyun goto name;
518*4882a593Smuzhiyun }
519*4882a593Smuzhiyun /* fall through to try shorter uniq */
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun
522*4882a593Smuzhiyun name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
523*4882a593Smuzhiyun if (!name)
524*4882a593Smuzhiyun return NULL;
525*4882a593Smuzhiyun sprintf(name, "%s//null-%x", parent->base.hname,
526*4882a593Smuzhiyun atomic_inc_return(&parent->ns->uniq_null));
527*4882a593Smuzhiyun
528*4882a593Smuzhiyun name:
529*4882a593Smuzhiyun /* lookup to see if this is a dup creation */
530*4882a593Smuzhiyun bname = basename(name);
531*4882a593Smuzhiyun profile = aa_find_child(parent, bname);
532*4882a593Smuzhiyun if (profile)
533*4882a593Smuzhiyun goto out;
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun profile = aa_alloc_profile(name, NULL, gfp);
536*4882a593Smuzhiyun if (!profile)
537*4882a593Smuzhiyun goto fail;
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun profile->mode = APPARMOR_COMPLAIN;
540*4882a593Smuzhiyun profile->label.flags |= FLAG_NULL;
541*4882a593Smuzhiyun if (hat)
542*4882a593Smuzhiyun profile->label.flags |= FLAG_HAT;
543*4882a593Smuzhiyun profile->path_flags = parent->path_flags;
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun /* released on free_profile */
546*4882a593Smuzhiyun rcu_assign_pointer(profile->parent, aa_get_profile(parent));
547*4882a593Smuzhiyun profile->ns = aa_get_ns(parent->ns);
548*4882a593Smuzhiyun profile->file.dfa = aa_get_dfa(nulldfa);
549*4882a593Smuzhiyun profile->policy.dfa = aa_get_dfa(nulldfa);
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun mutex_lock_nested(&profile->ns->lock, profile->ns->level);
552*4882a593Smuzhiyun p = __find_child(&parent->base.profiles, bname);
553*4882a593Smuzhiyun if (p) {
554*4882a593Smuzhiyun aa_free_profile(profile);
555*4882a593Smuzhiyun profile = aa_get_profile(p);
556*4882a593Smuzhiyun } else {
557*4882a593Smuzhiyun __add_profile(&parent->base.profiles, profile);
558*4882a593Smuzhiyun }
559*4882a593Smuzhiyun mutex_unlock(&profile->ns->lock);
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun /* refcount released by caller */
562*4882a593Smuzhiyun out:
563*4882a593Smuzhiyun kfree(name);
564*4882a593Smuzhiyun
565*4882a593Smuzhiyun return profile;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun fail:
568*4882a593Smuzhiyun kfree(name);
569*4882a593Smuzhiyun aa_free_profile(profile);
570*4882a593Smuzhiyun return NULL;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun
573*4882a593Smuzhiyun /**
574*4882a593Smuzhiyun * replacement_allowed - test to see if replacement is allowed
575*4882a593Smuzhiyun * @profile: profile to test if it can be replaced (MAYBE NULL)
576*4882a593Smuzhiyun * @noreplace: true if replacement shouldn't be allowed but addition is okay
577*4882a593Smuzhiyun * @info: Returns - info about why replacement failed (NOT NULL)
578*4882a593Smuzhiyun *
579*4882a593Smuzhiyun * Returns: %0 if replacement allowed else error code
580*4882a593Smuzhiyun */
replacement_allowed(struct aa_profile * profile,int noreplace,const char ** info)581*4882a593Smuzhiyun static int replacement_allowed(struct aa_profile *profile, int noreplace,
582*4882a593Smuzhiyun const char **info)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun if (profile) {
585*4882a593Smuzhiyun if (profile->label.flags & FLAG_IMMUTIBLE) {
586*4882a593Smuzhiyun *info = "cannot replace immutable profile";
587*4882a593Smuzhiyun return -EPERM;
588*4882a593Smuzhiyun } else if (noreplace) {
589*4882a593Smuzhiyun *info = "profile already exists";
590*4882a593Smuzhiyun return -EEXIST;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun }
593*4882a593Smuzhiyun return 0;
594*4882a593Smuzhiyun }
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun /* audit callback for net specific fields */
audit_cb(struct audit_buffer * ab,void * va)597*4882a593Smuzhiyun static void audit_cb(struct audit_buffer *ab, void *va)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun struct common_audit_data *sa = va;
600*4882a593Smuzhiyun
601*4882a593Smuzhiyun if (aad(sa)->iface.ns) {
602*4882a593Smuzhiyun audit_log_format(ab, " ns=");
603*4882a593Smuzhiyun audit_log_untrustedstring(ab, aad(sa)->iface.ns);
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun /**
608*4882a593Smuzhiyun * audit_policy - Do auditing of policy changes
609*4882a593Smuzhiyun * @label: label to check if it can manage policy
610*4882a593Smuzhiyun * @op: policy operation being performed
611*4882a593Smuzhiyun * @ns_name: name of namespace being manipulated
612*4882a593Smuzhiyun * @name: name of profile being manipulated (NOT NULL)
613*4882a593Smuzhiyun * @info: any extra information to be audited (MAYBE NULL)
614*4882a593Smuzhiyun * @error: error code
615*4882a593Smuzhiyun *
616*4882a593Smuzhiyun * Returns: the error to be returned after audit is done
617*4882a593Smuzhiyun */
audit_policy(struct aa_label * label,const char * op,const char * ns_name,const char * name,const char * info,int error)618*4882a593Smuzhiyun static int audit_policy(struct aa_label *label, const char *op,
619*4882a593Smuzhiyun const char *ns_name, const char *name,
620*4882a593Smuzhiyun const char *info, int error)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, op);
623*4882a593Smuzhiyun
624*4882a593Smuzhiyun aad(&sa)->iface.ns = ns_name;
625*4882a593Smuzhiyun aad(&sa)->name = name;
626*4882a593Smuzhiyun aad(&sa)->info = info;
627*4882a593Smuzhiyun aad(&sa)->error = error;
628*4882a593Smuzhiyun aad(&sa)->label = label;
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, audit_cb);
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun return error;
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun /**
636*4882a593Smuzhiyun * policy_view_capable - check if viewing policy in at @ns is allowed
637*4882a593Smuzhiyun * ns: namespace being viewed by current task (may be NULL)
638*4882a593Smuzhiyun * Returns: true if viewing policy is allowed
639*4882a593Smuzhiyun *
640*4882a593Smuzhiyun * If @ns is NULL then the namespace being viewed is assumed to be the
641*4882a593Smuzhiyun * tasks current namespace.
642*4882a593Smuzhiyun */
policy_view_capable(struct aa_ns * ns)643*4882a593Smuzhiyun bool policy_view_capable(struct aa_ns *ns)
644*4882a593Smuzhiyun {
645*4882a593Smuzhiyun struct user_namespace *user_ns = current_user_ns();
646*4882a593Smuzhiyun struct aa_ns *view_ns = aa_get_current_ns();
647*4882a593Smuzhiyun bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) ||
648*4882a593Smuzhiyun in_egroup_p(make_kgid(user_ns, 0));
649*4882a593Smuzhiyun bool response = false;
650*4882a593Smuzhiyun if (!ns)
651*4882a593Smuzhiyun ns = view_ns;
652*4882a593Smuzhiyun
653*4882a593Smuzhiyun if (root_in_user_ns && aa_ns_visible(view_ns, ns, true) &&
654*4882a593Smuzhiyun (user_ns == &init_user_ns ||
655*4882a593Smuzhiyun (unprivileged_userns_apparmor_policy != 0 &&
656*4882a593Smuzhiyun user_ns->level == view_ns->level)))
657*4882a593Smuzhiyun response = true;
658*4882a593Smuzhiyun aa_put_ns(view_ns);
659*4882a593Smuzhiyun
660*4882a593Smuzhiyun return response;
661*4882a593Smuzhiyun }
662*4882a593Smuzhiyun
policy_admin_capable(struct aa_ns * ns)663*4882a593Smuzhiyun bool policy_admin_capable(struct aa_ns *ns)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun struct user_namespace *user_ns = current_user_ns();
666*4882a593Smuzhiyun bool capable = ns_capable(user_ns, CAP_MAC_ADMIN);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun AA_DEBUG("cap_mac_admin? %d\n", capable);
669*4882a593Smuzhiyun AA_DEBUG("policy locked? %d\n", aa_g_lock_policy);
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun return policy_view_capable(ns) && capable && !aa_g_lock_policy;
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun /**
675*4882a593Smuzhiyun * aa_may_manage_policy - can the current task manage policy
676*4882a593Smuzhiyun * @label: label to check if it can manage policy
677*4882a593Smuzhiyun * @op: the policy manipulation operation being done
678*4882a593Smuzhiyun *
679*4882a593Smuzhiyun * Returns: 0 if the task is allowed to manipulate policy else error
680*4882a593Smuzhiyun */
aa_may_manage_policy(struct aa_label * label,struct aa_ns * ns,u32 mask)681*4882a593Smuzhiyun int aa_may_manage_policy(struct aa_label *label, struct aa_ns *ns, u32 mask)
682*4882a593Smuzhiyun {
683*4882a593Smuzhiyun const char *op;
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun if (mask & AA_MAY_REMOVE_POLICY)
686*4882a593Smuzhiyun op = OP_PROF_RM;
687*4882a593Smuzhiyun else if (mask & AA_MAY_REPLACE_POLICY)
688*4882a593Smuzhiyun op = OP_PROF_REPL;
689*4882a593Smuzhiyun else
690*4882a593Smuzhiyun op = OP_PROF_LOAD;
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun /* check if loading policy is locked out */
693*4882a593Smuzhiyun if (aa_g_lock_policy)
694*4882a593Smuzhiyun return audit_policy(label, op, NULL, NULL, "policy_locked",
695*4882a593Smuzhiyun -EACCES);
696*4882a593Smuzhiyun
697*4882a593Smuzhiyun if (!policy_admin_capable(ns))
698*4882a593Smuzhiyun return audit_policy(label, op, NULL, NULL, "not policy admin",
699*4882a593Smuzhiyun -EACCES);
700*4882a593Smuzhiyun
701*4882a593Smuzhiyun /* TODO: add fine grained mediation of policy loads */
702*4882a593Smuzhiyun return 0;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
__list_lookup_parent(struct list_head * lh,struct aa_profile * profile)705*4882a593Smuzhiyun static struct aa_profile *__list_lookup_parent(struct list_head *lh,
706*4882a593Smuzhiyun struct aa_profile *profile)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun const char *base = basename(profile->base.hname);
709*4882a593Smuzhiyun long len = base - profile->base.hname;
710*4882a593Smuzhiyun struct aa_load_ent *ent;
711*4882a593Smuzhiyun
712*4882a593Smuzhiyun /* parent won't have trailing // so remove from len */
713*4882a593Smuzhiyun if (len <= 2)
714*4882a593Smuzhiyun return NULL;
715*4882a593Smuzhiyun len -= 2;
716*4882a593Smuzhiyun
717*4882a593Smuzhiyun list_for_each_entry(ent, lh, list) {
718*4882a593Smuzhiyun if (ent->new == profile)
719*4882a593Smuzhiyun continue;
720*4882a593Smuzhiyun if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
721*4882a593Smuzhiyun 0 && ent->new->base.hname[len] == 0)
722*4882a593Smuzhiyun return ent->new;
723*4882a593Smuzhiyun }
724*4882a593Smuzhiyun
725*4882a593Smuzhiyun return NULL;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun /**
729*4882a593Smuzhiyun * __replace_profile - replace @old with @new on a list
730*4882a593Smuzhiyun * @old: profile to be replaced (NOT NULL)
731*4882a593Smuzhiyun * @new: profile to replace @old with (NOT NULL)
732*4882a593Smuzhiyun * @share_proxy: transfer @old->proxy to @new
733*4882a593Smuzhiyun *
734*4882a593Smuzhiyun * Will duplicate and refcount elements that @new inherits from @old
735*4882a593Smuzhiyun * and will inherit @old children.
736*4882a593Smuzhiyun *
737*4882a593Smuzhiyun * refcount @new for list, put @old list refcount
738*4882a593Smuzhiyun *
739*4882a593Smuzhiyun * Requires: namespace list lock be held, or list not be shared
740*4882a593Smuzhiyun */
__replace_profile(struct aa_profile * old,struct aa_profile * new)741*4882a593Smuzhiyun static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
742*4882a593Smuzhiyun {
743*4882a593Smuzhiyun struct aa_profile *child, *tmp;
744*4882a593Smuzhiyun
745*4882a593Smuzhiyun if (!list_empty(&old->base.profiles)) {
746*4882a593Smuzhiyun LIST_HEAD(lh);
747*4882a593Smuzhiyun list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun list_for_each_entry_safe(child, tmp, &lh, base.list) {
750*4882a593Smuzhiyun struct aa_profile *p;
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun list_del_init(&child->base.list);
753*4882a593Smuzhiyun p = __find_child(&new->base.profiles, child->base.name);
754*4882a593Smuzhiyun if (p) {
755*4882a593Smuzhiyun /* @p replaces @child */
756*4882a593Smuzhiyun __replace_profile(child, p);
757*4882a593Smuzhiyun continue;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun
760*4882a593Smuzhiyun /* inherit @child and its children */
761*4882a593Smuzhiyun /* TODO: update hname of inherited children */
762*4882a593Smuzhiyun /* list refcount transferred to @new */
763*4882a593Smuzhiyun p = aa_deref_parent(child);
764*4882a593Smuzhiyun rcu_assign_pointer(child->parent, aa_get_profile(new));
765*4882a593Smuzhiyun list_add_rcu(&child->base.list, &new->base.profiles);
766*4882a593Smuzhiyun aa_put_profile(p);
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun }
769*4882a593Smuzhiyun
770*4882a593Smuzhiyun if (!rcu_access_pointer(new->parent)) {
771*4882a593Smuzhiyun struct aa_profile *parent = aa_deref_parent(old);
772*4882a593Smuzhiyun rcu_assign_pointer(new->parent, aa_get_profile(parent));
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun aa_label_replace(&old->label, &new->label);
775*4882a593Smuzhiyun /* migrate dents must come after label replacement b/c update */
776*4882a593Smuzhiyun __aafs_profile_migrate_dents(old, new);
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun if (list_empty(&new->base.list)) {
779*4882a593Smuzhiyun /* new is not on a list already */
780*4882a593Smuzhiyun list_replace_rcu(&old->base.list, &new->base.list);
781*4882a593Smuzhiyun aa_get_profile(new);
782*4882a593Smuzhiyun aa_put_profile(old);
783*4882a593Smuzhiyun } else
784*4882a593Smuzhiyun __list_remove_profile(old);
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun /**
788*4882a593Smuzhiyun * __lookup_replace - lookup replacement information for a profile
789*4882a593Smuzhiyun * @ns - namespace the lookup occurs in
790*4882a593Smuzhiyun * @hname - name of profile to lookup
791*4882a593Smuzhiyun * @noreplace - true if not replacing an existing profile
792*4882a593Smuzhiyun * @p - Returns: profile to be replaced
793*4882a593Smuzhiyun * @info - Returns: info string on why lookup failed
794*4882a593Smuzhiyun *
795*4882a593Smuzhiyun * Returns: profile to replace (no ref) on success else ptr error
796*4882a593Smuzhiyun */
__lookup_replace(struct aa_ns * ns,const char * hname,bool noreplace,struct aa_profile ** p,const char ** info)797*4882a593Smuzhiyun static int __lookup_replace(struct aa_ns *ns, const char *hname,
798*4882a593Smuzhiyun bool noreplace, struct aa_profile **p,
799*4882a593Smuzhiyun const char **info)
800*4882a593Smuzhiyun {
801*4882a593Smuzhiyun *p = aa_get_profile(__lookup_profile(&ns->base, hname));
802*4882a593Smuzhiyun if (*p) {
803*4882a593Smuzhiyun int error = replacement_allowed(*p, noreplace, info);
804*4882a593Smuzhiyun if (error) {
805*4882a593Smuzhiyun *info = "profile can not be replaced";
806*4882a593Smuzhiyun return error;
807*4882a593Smuzhiyun }
808*4882a593Smuzhiyun }
809*4882a593Smuzhiyun
810*4882a593Smuzhiyun return 0;
811*4882a593Smuzhiyun }
812*4882a593Smuzhiyun
share_name(struct aa_profile * old,struct aa_profile * new)813*4882a593Smuzhiyun static void share_name(struct aa_profile *old, struct aa_profile *new)
814*4882a593Smuzhiyun {
815*4882a593Smuzhiyun aa_put_str(new->base.hname);
816*4882a593Smuzhiyun aa_get_str(old->base.hname);
817*4882a593Smuzhiyun new->base.hname = old->base.hname;
818*4882a593Smuzhiyun new->base.name = old->base.name;
819*4882a593Smuzhiyun new->label.hname = old->label.hname;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun /* Update to newest version of parent after previous replacements
823*4882a593Smuzhiyun * Returns: unrefcount newest version of parent
824*4882a593Smuzhiyun */
update_to_newest_parent(struct aa_profile * new)825*4882a593Smuzhiyun static struct aa_profile *update_to_newest_parent(struct aa_profile *new)
826*4882a593Smuzhiyun {
827*4882a593Smuzhiyun struct aa_profile *parent, *newest;
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun parent = rcu_dereference_protected(new->parent,
830*4882a593Smuzhiyun mutex_is_locked(&new->ns->lock));
831*4882a593Smuzhiyun newest = aa_get_newest_profile(parent);
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /* parent replaced in this atomic set? */
834*4882a593Smuzhiyun if (newest != parent) {
835*4882a593Smuzhiyun aa_put_profile(parent);
836*4882a593Smuzhiyun rcu_assign_pointer(new->parent, newest);
837*4882a593Smuzhiyun } else
838*4882a593Smuzhiyun aa_put_profile(newest);
839*4882a593Smuzhiyun
840*4882a593Smuzhiyun return newest;
841*4882a593Smuzhiyun }
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun /**
844*4882a593Smuzhiyun * aa_replace_profiles - replace profile(s) on the profile list
845*4882a593Smuzhiyun * @policy_ns: namespace load is occurring on
846*4882a593Smuzhiyun * @label: label that is attempting to load/replace policy
847*4882a593Smuzhiyun * @mask: permission mask
848*4882a593Smuzhiyun * @udata: serialized data stream (NOT NULL)
849*4882a593Smuzhiyun *
850*4882a593Smuzhiyun * unpack and replace a profile on the profile list and uses of that profile
851*4882a593Smuzhiyun * by any task creds via invalidating the old version of the profile, which
852*4882a593Smuzhiyun * tasks will notice to update their own cred. If the profile does not exist
853*4882a593Smuzhiyun * on the profile list it is added.
854*4882a593Smuzhiyun *
855*4882a593Smuzhiyun * Returns: size of data consumed else error code on failure.
856*4882a593Smuzhiyun */
aa_replace_profiles(struct aa_ns * policy_ns,struct aa_label * label,u32 mask,struct aa_loaddata * udata)857*4882a593Smuzhiyun ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label,
858*4882a593Smuzhiyun u32 mask, struct aa_loaddata *udata)
859*4882a593Smuzhiyun {
860*4882a593Smuzhiyun const char *ns_name = NULL, *info = NULL;
861*4882a593Smuzhiyun struct aa_ns *ns = NULL;
862*4882a593Smuzhiyun struct aa_load_ent *ent, *tmp;
863*4882a593Smuzhiyun struct aa_loaddata *rawdata_ent;
864*4882a593Smuzhiyun const char *op;
865*4882a593Smuzhiyun ssize_t count, error;
866*4882a593Smuzhiyun LIST_HEAD(lh);
867*4882a593Smuzhiyun
868*4882a593Smuzhiyun op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD;
869*4882a593Smuzhiyun aa_get_loaddata(udata);
870*4882a593Smuzhiyun /* released below */
871*4882a593Smuzhiyun error = aa_unpack(udata, &lh, &ns_name);
872*4882a593Smuzhiyun if (error)
873*4882a593Smuzhiyun goto out;
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun /* ensure that profiles are all for the same ns
876*4882a593Smuzhiyun * TODO: update locking to remove this constaint. All profiles in
877*4882a593Smuzhiyun * the load set must succeed as a set or the load will
878*4882a593Smuzhiyun * fail. Sort ent list and take ns locks in hierarchy order
879*4882a593Smuzhiyun */
880*4882a593Smuzhiyun count = 0;
881*4882a593Smuzhiyun list_for_each_entry(ent, &lh, list) {
882*4882a593Smuzhiyun if (ns_name) {
883*4882a593Smuzhiyun if (ent->ns_name &&
884*4882a593Smuzhiyun strcmp(ent->ns_name, ns_name) != 0) {
885*4882a593Smuzhiyun info = "policy load has mixed namespaces";
886*4882a593Smuzhiyun error = -EACCES;
887*4882a593Smuzhiyun goto fail;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun } else if (ent->ns_name) {
890*4882a593Smuzhiyun if (count) {
891*4882a593Smuzhiyun info = "policy load has mixed namespaces";
892*4882a593Smuzhiyun error = -EACCES;
893*4882a593Smuzhiyun goto fail;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun ns_name = ent->ns_name;
896*4882a593Smuzhiyun } else
897*4882a593Smuzhiyun count++;
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun if (ns_name) {
900*4882a593Smuzhiyun ns = aa_prepare_ns(policy_ns ? policy_ns : labels_ns(label),
901*4882a593Smuzhiyun ns_name);
902*4882a593Smuzhiyun if (IS_ERR(ns)) {
903*4882a593Smuzhiyun op = OP_PROF_LOAD;
904*4882a593Smuzhiyun info = "failed to prepare namespace";
905*4882a593Smuzhiyun error = PTR_ERR(ns);
906*4882a593Smuzhiyun ns = NULL;
907*4882a593Smuzhiyun ent = NULL;
908*4882a593Smuzhiyun goto fail;
909*4882a593Smuzhiyun }
910*4882a593Smuzhiyun } else
911*4882a593Smuzhiyun ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(label));
912*4882a593Smuzhiyun
913*4882a593Smuzhiyun mutex_lock_nested(&ns->lock, ns->level);
914*4882a593Smuzhiyun /* check for duplicate rawdata blobs: space and file dedup */
915*4882a593Smuzhiyun list_for_each_entry(rawdata_ent, &ns->rawdata_list, list) {
916*4882a593Smuzhiyun if (aa_rawdata_eq(rawdata_ent, udata)) {
917*4882a593Smuzhiyun struct aa_loaddata *tmp;
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun tmp = __aa_get_loaddata(rawdata_ent);
920*4882a593Smuzhiyun /* check we didn't fail the race */
921*4882a593Smuzhiyun if (tmp) {
922*4882a593Smuzhiyun aa_put_loaddata(udata);
923*4882a593Smuzhiyun udata = tmp;
924*4882a593Smuzhiyun break;
925*4882a593Smuzhiyun }
926*4882a593Smuzhiyun }
927*4882a593Smuzhiyun }
928*4882a593Smuzhiyun /* setup parent and ns info */
929*4882a593Smuzhiyun list_for_each_entry(ent, &lh, list) {
930*4882a593Smuzhiyun struct aa_policy *policy;
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun ent->new->rawdata = aa_get_loaddata(udata);
933*4882a593Smuzhiyun error = __lookup_replace(ns, ent->new->base.hname,
934*4882a593Smuzhiyun !(mask & AA_MAY_REPLACE_POLICY),
935*4882a593Smuzhiyun &ent->old, &info);
936*4882a593Smuzhiyun if (error)
937*4882a593Smuzhiyun goto fail_lock;
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun if (ent->new->rename) {
940*4882a593Smuzhiyun error = __lookup_replace(ns, ent->new->rename,
941*4882a593Smuzhiyun !(mask & AA_MAY_REPLACE_POLICY),
942*4882a593Smuzhiyun &ent->rename, &info);
943*4882a593Smuzhiyun if (error)
944*4882a593Smuzhiyun goto fail_lock;
945*4882a593Smuzhiyun }
946*4882a593Smuzhiyun
947*4882a593Smuzhiyun /* released when @new is freed */
948*4882a593Smuzhiyun ent->new->ns = aa_get_ns(ns);
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun if (ent->old || ent->rename)
951*4882a593Smuzhiyun continue;
952*4882a593Smuzhiyun
953*4882a593Smuzhiyun /* no ref on policy only use inside lock */
954*4882a593Smuzhiyun policy = __lookup_parent(ns, ent->new->base.hname);
955*4882a593Smuzhiyun if (!policy) {
956*4882a593Smuzhiyun struct aa_profile *p;
957*4882a593Smuzhiyun p = __list_lookup_parent(&lh, ent->new);
958*4882a593Smuzhiyun if (!p) {
959*4882a593Smuzhiyun error = -ENOENT;
960*4882a593Smuzhiyun info = "parent does not exist";
961*4882a593Smuzhiyun goto fail_lock;
962*4882a593Smuzhiyun }
963*4882a593Smuzhiyun rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
964*4882a593Smuzhiyun } else if (policy != &ns->base) {
965*4882a593Smuzhiyun /* released on profile replacement or free_profile */
966*4882a593Smuzhiyun struct aa_profile *p = (struct aa_profile *) policy;
967*4882a593Smuzhiyun rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun }
970*4882a593Smuzhiyun
971*4882a593Smuzhiyun /* create new fs entries for introspection if needed */
972*4882a593Smuzhiyun if (!udata->dents[AAFS_LOADDATA_DIR]) {
973*4882a593Smuzhiyun error = __aa_fs_create_rawdata(ns, udata);
974*4882a593Smuzhiyun if (error) {
975*4882a593Smuzhiyun info = "failed to create raw_data dir and files";
976*4882a593Smuzhiyun ent = NULL;
977*4882a593Smuzhiyun goto fail_lock;
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun list_for_each_entry(ent, &lh, list) {
981*4882a593Smuzhiyun if (!ent->old) {
982*4882a593Smuzhiyun struct dentry *parent;
983*4882a593Smuzhiyun if (rcu_access_pointer(ent->new->parent)) {
984*4882a593Smuzhiyun struct aa_profile *p;
985*4882a593Smuzhiyun p = aa_deref_parent(ent->new);
986*4882a593Smuzhiyun parent = prof_child_dir(p);
987*4882a593Smuzhiyun } else
988*4882a593Smuzhiyun parent = ns_subprofs_dir(ent->new->ns);
989*4882a593Smuzhiyun error = __aafs_profile_mkdir(ent->new, parent);
990*4882a593Smuzhiyun }
991*4882a593Smuzhiyun
992*4882a593Smuzhiyun if (error) {
993*4882a593Smuzhiyun info = "failed to create";
994*4882a593Smuzhiyun goto fail_lock;
995*4882a593Smuzhiyun }
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun /* Done with checks that may fail - do actual replacement */
999*4882a593Smuzhiyun __aa_bump_ns_revision(ns);
1000*4882a593Smuzhiyun __aa_loaddata_update(udata, ns->revision);
1001*4882a593Smuzhiyun list_for_each_entry_safe(ent, tmp, &lh, list) {
1002*4882a593Smuzhiyun list_del_init(&ent->list);
1003*4882a593Smuzhiyun op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun if (ent->old && ent->old->rawdata == ent->new->rawdata) {
1006*4882a593Smuzhiyun /* dedup actual profile replacement */
1007*4882a593Smuzhiyun audit_policy(label, op, ns_name, ent->new->base.hname,
1008*4882a593Smuzhiyun "same as current profile, skipping",
1009*4882a593Smuzhiyun error);
1010*4882a593Smuzhiyun /* break refcount cycle with proxy. */
1011*4882a593Smuzhiyun aa_put_proxy(ent->new->label.proxy);
1012*4882a593Smuzhiyun ent->new->label.proxy = NULL;
1013*4882a593Smuzhiyun goto skip;
1014*4882a593Smuzhiyun }
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun /*
1017*4882a593Smuzhiyun * TODO: finer dedup based on profile range in data. Load set
1018*4882a593Smuzhiyun * can differ but profile may remain unchanged
1019*4882a593Smuzhiyun */
1020*4882a593Smuzhiyun audit_policy(label, op, ns_name, ent->new->base.hname, NULL,
1021*4882a593Smuzhiyun error);
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun if (ent->old) {
1024*4882a593Smuzhiyun share_name(ent->old, ent->new);
1025*4882a593Smuzhiyun __replace_profile(ent->old, ent->new);
1026*4882a593Smuzhiyun } else {
1027*4882a593Smuzhiyun struct list_head *lh;
1028*4882a593Smuzhiyun
1029*4882a593Smuzhiyun if (rcu_access_pointer(ent->new->parent)) {
1030*4882a593Smuzhiyun struct aa_profile *parent;
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun parent = update_to_newest_parent(ent->new);
1033*4882a593Smuzhiyun lh = &parent->base.profiles;
1034*4882a593Smuzhiyun } else
1035*4882a593Smuzhiyun lh = &ns->base.profiles;
1036*4882a593Smuzhiyun __add_profile(lh, ent->new);
1037*4882a593Smuzhiyun }
1038*4882a593Smuzhiyun skip:
1039*4882a593Smuzhiyun aa_load_ent_free(ent);
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun __aa_labelset_update_subtree(ns);
1042*4882a593Smuzhiyun mutex_unlock(&ns->lock);
1043*4882a593Smuzhiyun
1044*4882a593Smuzhiyun out:
1045*4882a593Smuzhiyun aa_put_ns(ns);
1046*4882a593Smuzhiyun aa_put_loaddata(udata);
1047*4882a593Smuzhiyun kfree(ns_name);
1048*4882a593Smuzhiyun
1049*4882a593Smuzhiyun if (error)
1050*4882a593Smuzhiyun return error;
1051*4882a593Smuzhiyun return udata->size;
1052*4882a593Smuzhiyun
1053*4882a593Smuzhiyun fail_lock:
1054*4882a593Smuzhiyun mutex_unlock(&ns->lock);
1055*4882a593Smuzhiyun
1056*4882a593Smuzhiyun /* audit cause of failure */
1057*4882a593Smuzhiyun op = (ent && !ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1058*4882a593Smuzhiyun fail:
1059*4882a593Smuzhiyun audit_policy(label, op, ns_name, ent ? ent->new->base.hname : NULL,
1060*4882a593Smuzhiyun info, error);
1061*4882a593Smuzhiyun /* audit status that rest of profiles in the atomic set failed too */
1062*4882a593Smuzhiyun info = "valid profile in failed atomic policy load";
1063*4882a593Smuzhiyun list_for_each_entry(tmp, &lh, list) {
1064*4882a593Smuzhiyun if (tmp == ent) {
1065*4882a593Smuzhiyun info = "unchecked profile in failed atomic policy load";
1066*4882a593Smuzhiyun /* skip entry that caused failure */
1067*4882a593Smuzhiyun continue;
1068*4882a593Smuzhiyun }
1069*4882a593Smuzhiyun op = (!tmp->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1070*4882a593Smuzhiyun audit_policy(label, op, ns_name, tmp->new->base.hname, info,
1071*4882a593Smuzhiyun error);
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun list_for_each_entry_safe(ent, tmp, &lh, list) {
1074*4882a593Smuzhiyun list_del_init(&ent->list);
1075*4882a593Smuzhiyun aa_load_ent_free(ent);
1076*4882a593Smuzhiyun }
1077*4882a593Smuzhiyun
1078*4882a593Smuzhiyun goto out;
1079*4882a593Smuzhiyun }
1080*4882a593Smuzhiyun
1081*4882a593Smuzhiyun /**
1082*4882a593Smuzhiyun * aa_remove_profiles - remove profile(s) from the system
1083*4882a593Smuzhiyun * @policy_ns: namespace the remove is being done from
1084*4882a593Smuzhiyun * @subj: label attempting to remove policy
1085*4882a593Smuzhiyun * @fqname: name of the profile or namespace to remove (NOT NULL)
1086*4882a593Smuzhiyun * @size: size of the name
1087*4882a593Smuzhiyun *
1088*4882a593Smuzhiyun * Remove a profile or sub namespace from the current namespace, so that
1089*4882a593Smuzhiyun * they can not be found anymore and mark them as replaced by unconfined
1090*4882a593Smuzhiyun *
1091*4882a593Smuzhiyun * NOTE: removing confinement does not restore rlimits to preconfinement values
1092*4882a593Smuzhiyun *
1093*4882a593Smuzhiyun * Returns: size of data consume else error code if fails
1094*4882a593Smuzhiyun */
aa_remove_profiles(struct aa_ns * policy_ns,struct aa_label * subj,char * fqname,size_t size)1095*4882a593Smuzhiyun ssize_t aa_remove_profiles(struct aa_ns *policy_ns, struct aa_label *subj,
1096*4882a593Smuzhiyun char *fqname, size_t size)
1097*4882a593Smuzhiyun {
1098*4882a593Smuzhiyun struct aa_ns *ns = NULL;
1099*4882a593Smuzhiyun struct aa_profile *profile = NULL;
1100*4882a593Smuzhiyun const char *name = fqname, *info = NULL;
1101*4882a593Smuzhiyun const char *ns_name = NULL;
1102*4882a593Smuzhiyun ssize_t error = 0;
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun if (*fqname == 0) {
1105*4882a593Smuzhiyun info = "no profile specified";
1106*4882a593Smuzhiyun error = -ENOENT;
1107*4882a593Smuzhiyun goto fail;
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun
1110*4882a593Smuzhiyun if (fqname[0] == ':') {
1111*4882a593Smuzhiyun size_t ns_len;
1112*4882a593Smuzhiyun
1113*4882a593Smuzhiyun name = aa_splitn_fqname(fqname, size, &ns_name, &ns_len);
1114*4882a593Smuzhiyun /* released below */
1115*4882a593Smuzhiyun ns = aa_lookupn_ns(policy_ns ? policy_ns : labels_ns(subj),
1116*4882a593Smuzhiyun ns_name, ns_len);
1117*4882a593Smuzhiyun if (!ns) {
1118*4882a593Smuzhiyun info = "namespace does not exist";
1119*4882a593Smuzhiyun error = -ENOENT;
1120*4882a593Smuzhiyun goto fail;
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun } else
1123*4882a593Smuzhiyun /* released below */
1124*4882a593Smuzhiyun ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(subj));
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun if (!name) {
1127*4882a593Smuzhiyun /* remove namespace - can only happen if fqname[0] == ':' */
1128*4882a593Smuzhiyun mutex_lock_nested(&ns->parent->lock, ns->level);
1129*4882a593Smuzhiyun __aa_bump_ns_revision(ns);
1130*4882a593Smuzhiyun __aa_remove_ns(ns);
1131*4882a593Smuzhiyun mutex_unlock(&ns->parent->lock);
1132*4882a593Smuzhiyun } else {
1133*4882a593Smuzhiyun /* remove profile */
1134*4882a593Smuzhiyun mutex_lock_nested(&ns->lock, ns->level);
1135*4882a593Smuzhiyun profile = aa_get_profile(__lookup_profile(&ns->base, name));
1136*4882a593Smuzhiyun if (!profile) {
1137*4882a593Smuzhiyun error = -ENOENT;
1138*4882a593Smuzhiyun info = "profile does not exist";
1139*4882a593Smuzhiyun goto fail_ns_lock;
1140*4882a593Smuzhiyun }
1141*4882a593Smuzhiyun name = profile->base.hname;
1142*4882a593Smuzhiyun __aa_bump_ns_revision(ns);
1143*4882a593Smuzhiyun __remove_profile(profile);
1144*4882a593Smuzhiyun __aa_labelset_update_subtree(ns);
1145*4882a593Smuzhiyun mutex_unlock(&ns->lock);
1146*4882a593Smuzhiyun }
1147*4882a593Smuzhiyun
1148*4882a593Smuzhiyun /* don't fail removal if audit fails */
1149*4882a593Smuzhiyun (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1150*4882a593Smuzhiyun error);
1151*4882a593Smuzhiyun aa_put_ns(ns);
1152*4882a593Smuzhiyun aa_put_profile(profile);
1153*4882a593Smuzhiyun return size;
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun fail_ns_lock:
1156*4882a593Smuzhiyun mutex_unlock(&ns->lock);
1157*4882a593Smuzhiyun aa_put_ns(ns);
1158*4882a593Smuzhiyun
1159*4882a593Smuzhiyun fail:
1160*4882a593Smuzhiyun (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1161*4882a593Smuzhiyun error);
1162*4882a593Smuzhiyun return error;
1163*4882a593Smuzhiyun }
1164