apparmor: allow specifying the profile doing the management
[linux-2.6-block.git] / security / apparmor / policy.c
CommitLineData
c88d4c7b
JJ
1/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor policy manipulation functions
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
13 *
14 *
15 * AppArmor policy is based around profiles, which contain the rules a
16 * task is confined by. Every task in the system has a profile attached
17 * to it determined either by matching "unconfined" tasks against the
18 * visible set of profiles or by following a profiles attachment rules.
19 *
20 * Each profile exists in a profile namespace which is a container of
21 * visible profiles. Each namespace contains a special "unconfined" profile,
22 * which doesn't enforce any confinement on a task beyond DAC.
23 *
24 * Namespace and profile names can be written together in either
25 * of two syntaxes.
26 * :namespace:profile - used by kernel interfaces for easy detection
27 * namespace://profile - used by policy
28 *
29 * Profile names can not start with : or @ or ^ and may not contain \0
30 *
31 * Reserved profile names
32 * unconfined - special automatically generated unconfined profile
33 * inherit - special name to indicate profile inheritance
34 * null-XXXX-YYYY - special automatically generated learning profiles
35 *
36 * Namespace names may not start with / or @ and may not contain \0 or :
37 * Reserved namespace names
38 * user-XXXX - user defined profiles
39 *
40 * a // in a profile or namespace name indicates a hierarchical name with the
41 * name before the // being the parent and the name after the child.
42 *
43 * Profile and namespace hierarchies serve two different but similar purposes.
44 * The namespace contains the set of visible profiles that are considered
45 * for attachment. The hierarchy of namespaces allows for virtualizing
46 * the namespace so that for example a chroot can have its own set of profiles
47 * which may define some local user namespaces.
48 * The profile hierarchy severs two distinct purposes,
49 * - it allows for sub profiles or hats, which allows an application to run
50 * subprograms under its own profile with different restriction than it
51 * self, and not have it use the system profile.
52 * eg. if a mail program starts an editor, the policy might make the
53 * restrictions tighter on the editor tighter than the mail program,
54 * and definitely different than general editor restrictions
55 * - it allows for binary hierarchy of profiles, so that execution history
56 * is preserved. This feature isn't exploited by AppArmor reference policy
57 * but is allowed. NOTE: this is currently suboptimal because profile
58 * aliasing is not currently implemented so that a profile for each
59 * level must be defined.
60 * eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started
61 * from /bin/bash
62 *
63 * A profile or namespace name that can contain one or more // separators
64 * is referred to as an hname (hierarchical).
65 * eg. /bin/bash//bin/ls
66 *
67 * An fqname is a name that may contain both namespace and profile hnames.
68 * eg. :ns:/bin/bash//bin/ls
69 *
70 * NOTES:
71 * - locking of profile lists is currently fairly coarse. All profile
72 * lists within a namespace use the namespace lock.
73 * FIXME: move profile lists to using rcu_lists
74 */
75
76#include <linux/slab.h>
77#include <linux/spinlock.h>
78#include <linux/string.h>
79
80#include "include/apparmor.h"
81#include "include/capability.h"
82#include "include/context.h"
83#include "include/file.h"
84#include "include/ipc.h"
85#include "include/match.h"
86#include "include/path.h"
87#include "include/policy.h"
cff281f6 88#include "include/policy_ns.h"
c88d4c7b
JJ
89#include "include/policy_unpack.h"
90#include "include/resource.h"
c88d4c7b
JJ
91
92
0d259f04 93const char *const aa_profile_mode_names[] = {
c88d4c7b
JJ
94 "enforce",
95 "complain",
96 "kill",
03816507 97 "unconfined",
c88d4c7b
JJ
98};
99
c88d4c7b 100
cff281f6 101/* requires profile list write lock held */
8399588a 102void __aa_update_proxy(struct aa_profile *orig, struct aa_profile *new)
c88d4c7b 103{
cff281f6 104 struct aa_profile *tmp;
c88d4c7b 105
8399588a 106 tmp = rcu_dereference_protected(orig->proxy->profile,
cff281f6 107 mutex_is_locked(&orig->ns->lock));
8399588a 108 rcu_assign_pointer(orig->proxy->profile, aa_get_profile(new));
d97d51d2 109 orig->flags |= PFLAG_STALE;
cff281f6 110 aa_put_profile(tmp);
c88d4c7b
JJ
111}
112
113/**
114 * __list_add_profile - add a profile to a list
115 * @list: list to add it to (NOT NULL)
116 * @profile: the profile to add (NOT NULL)
117 *
118 * refcount @profile, should be put by __list_remove_profile
119 *
120 * Requires: namespace lock be held, or list not be shared
121 */
122static void __list_add_profile(struct list_head *list,
123 struct aa_profile *profile)
124{
01e2b670 125 list_add_rcu(&profile->base.list, list);
c88d4c7b
JJ
126 /* get list reference */
127 aa_get_profile(profile);
128}
129
130/**
131 * __list_remove_profile - remove a profile from the list it is on
132 * @profile: the profile to remove (NOT NULL)
133 *
134 * remove a profile from the list, warning generally removal should
135 * be done with __replace_profile as most profile removals are
136 * replacements to the unconfined profile.
137 *
138 * put @profile list refcount
139 *
140 * Requires: namespace lock be held, or list not have been live
141 */
142static void __list_remove_profile(struct aa_profile *profile)
143{
01e2b670
JJ
144 list_del_rcu(&profile->base.list);
145 aa_put_profile(profile);
c88d4c7b
JJ
146}
147
c88d4c7b
JJ
148/**
149 * __remove_profile - remove old profile, and children
150 * @profile: profile to be replaced (NOT NULL)
151 *
152 * Requires: namespace list lock be held, or list not be shared
153 */
154static void __remove_profile(struct aa_profile *profile)
155{
156 /* release any children lists first */
cff281f6 157 __aa_profile_list_release(&profile->base.profiles);
c88d4c7b 158 /* released by free_profile */
8399588a 159 __aa_update_proxy(profile, profile->ns->unconfined);
0d259f04 160 __aa_fs_profile_rmdir(profile);
c88d4c7b
JJ
161 __list_remove_profile(profile);
162}
163
164/**
cff281f6 165 * __aa_profile_list_release - remove all profiles on the list and put refs
c88d4c7b
JJ
166 * @head: list of profiles (NOT NULL)
167 *
168 * Requires: namespace lock be held
169 */
cff281f6 170void __aa_profile_list_release(struct list_head *head)
c88d4c7b
JJ
171{
172 struct aa_profile *profile, *tmp;
173 list_for_each_entry_safe(profile, tmp, head, base.list)
174 __remove_profile(profile);
175}
176
77b071b3 177
8399588a 178static void free_proxy(struct aa_proxy *p)
77b071b3 179{
8399588a 180 if (p) {
4cd4fc77 181 /* r->profile will not be updated any more as r is dead */
8399588a
JJ
182 aa_put_profile(rcu_dereference_protected(p->profile, true));
183 kzfree(p);
77b071b3
JJ
184 }
185}
186
187
8399588a 188void aa_free_proxy_kref(struct kref *kref)
77b071b3 189{
8399588a
JJ
190 struct aa_proxy *p = container_of(kref, struct aa_proxy, count);
191
192 free_proxy(p);
77b071b3
JJ
193}
194
c88d4c7b 195/**
8651e1d6 196 * aa_free_profile - free a profile
c88d4c7b
JJ
197 * @profile: the profile to free (MAYBE NULL)
198 *
199 * Free a profile, its hats and null_profile. All references to the profile,
200 * its hats and null_profile must have been put.
201 *
202 * If the profile was referenced from a task context, free_profile() will
203 * be called from an rcu callback routine, so we must not sleep here.
204 */
8651e1d6 205void aa_free_profile(struct aa_profile *profile)
c88d4c7b
JJ
206{
207 AA_DEBUG("%s(%p)\n", __func__, profile);
208
209 if (!profile)
210 return;
211
c88d4c7b 212 /* free children profiles */
fe6bb31f 213 aa_policy_destroy(&profile->base);
01e2b670 214 aa_put_profile(rcu_access_pointer(profile->parent));
c88d4c7b 215
98849dff 216 aa_put_ns(profile->ns);
c88d4c7b
JJ
217 kzfree(profile->rename);
218
219 aa_free_file_rules(&profile->file);
220 aa_free_cap_rules(&profile->caps);
221 aa_free_rlimit_rules(&profile->rlimits);
222
0d259f04 223 kzfree(profile->dirname);
c88d4c7b 224 aa_put_dfa(profile->xmatch);
ad5ff3db 225 aa_put_dfa(profile->policy.dfa);
8399588a 226 aa_put_proxy(profile->proxy);
c88d4c7b 227
5cb3e91e 228 kzfree(profile->hash);
c88d4c7b
JJ
229 kzfree(profile);
230}
231
01e2b670
JJ
232/**
233 * aa_free_profile_rcu - free aa_profile by rcu (called by aa_free_profile_kref)
234 * @head: rcu_head callback for freeing of a profile (NOT NULL)
235 */
236static void aa_free_profile_rcu(struct rcu_head *head)
237{
742058b0
JJ
238 struct aa_profile *p = container_of(head, struct aa_profile, rcu);
239 if (p->flags & PFLAG_NS_COUNT)
98849dff 240 aa_free_ns(p->ns);
742058b0 241 else
8651e1d6 242 aa_free_profile(p);
01e2b670
JJ
243}
244
c88d4c7b
JJ
245/**
246 * aa_free_profile_kref - free aa_profile by kref (called by aa_put_profile)
247 * @kr: kref callback for freeing of a profile (NOT NULL)
248 */
249void aa_free_profile_kref(struct kref *kref)
250{
fa2ac468 251 struct aa_profile *p = container_of(kref, struct aa_profile, count);
742058b0 252 call_rcu(&p->rcu, aa_free_profile_rcu);
c88d4c7b
JJ
253}
254
4da05cc0
JJ
255/**
256 * aa_alloc_profile - allocate, initialize and return a new profile
257 * @hname: name of the profile (NOT NULL)
30b026a8 258 * @gfp: allocation type
4da05cc0
JJ
259 *
260 * Returns: refcount profile or NULL on failure
261 */
30b026a8 262struct aa_profile *aa_alloc_profile(const char *hname, gfp_t gfp)
4da05cc0
JJ
263{
264 struct aa_profile *profile;
265
266 /* freed by free_profile - usually through aa_put_profile */
30b026a8 267 profile = kzalloc(sizeof(*profile), gfp);
4da05cc0
JJ
268 if (!profile)
269 return NULL;
270
30b026a8 271 profile->proxy = kzalloc(sizeof(struct aa_proxy), gfp);
8399588a 272 if (!profile->proxy)
77b071b3 273 goto fail;
8399588a 274 kref_init(&profile->proxy->count);
77b071b3 275
30b026a8 276 if (!aa_policy_init(&profile->base, NULL, hname, gfp))
77b071b3 277 goto fail;
fa2ac468 278 kref_init(&profile->count);
4da05cc0
JJ
279
280 /* refcount released by caller */
281 return profile;
77b071b3
JJ
282
283fail:
8399588a 284 kzfree(profile->proxy);
77b071b3
JJ
285 kzfree(profile);
286
287 return NULL;
4da05cc0
JJ
288}
289
290/**
181f7c97 291 * aa_new_null_profile - create or find a null-X learning profile
4da05cc0
JJ
292 * @parent: profile that caused this profile to be created (NOT NULL)
293 * @hat: true if the null- learning profile is a hat
181f7c97
JJ
294 * @base: name to base the null profile off of
295 * @gfp: type of allocation
4da05cc0 296 *
181f7c97
JJ
297 * Find/Create a null- complain mode profile used in learning mode. The
298 * name of the profile is unique and follows the format of parent//null-XXX.
299 * where XXX is based on the @name or if that fails or is not supplied
300 * a unique number
4da05cc0
JJ
301 *
302 * null profiles are added to the profile list but the list does not
303 * hold a count on them so that they are automatically released when
304 * not in use.
305 *
306 * Returns: new refcounted profile else NULL on failure
307 */
181f7c97
JJ
308struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
309 const char *base, gfp_t gfp)
4da05cc0 310{
181f7c97 311 struct aa_profile *profile;
4da05cc0 312 char *name;
4da05cc0 313
181f7c97
JJ
314 AA_BUG(!parent);
315
316 if (base) {
317 name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
318 gfp);
319 if (name) {
320 sprintf(name, "%s//null-%s", parent->base.hname, base);
321 goto name;
322 }
323 /* fall through to try shorter uniq */
324 }
325
326 name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
4da05cc0 327 if (!name)
181f7c97
JJ
328 return NULL;
329 sprintf(name, "%s//null-%x", parent->base.hname,
330 atomic_inc_return(&parent->ns->uniq_null));
4da05cc0 331
181f7c97
JJ
332name:
333 /* lookup to see if this is a dup creation */
334 profile = aa_find_child(parent, basename(name));
335 if (profile)
336 goto out;
337
338 profile = aa_alloc_profile(name, gfp);
4da05cc0
JJ
339 if (!profile)
340 goto fail;
341
342 profile->mode = APPARMOR_COMPLAIN;
181f7c97 343 profile->flags |= PFLAG_NULL;
4da05cc0
JJ
344 if (hat)
345 profile->flags |= PFLAG_HAT;
181f7c97 346 profile->path_flags = parent->path_flags;
4da05cc0
JJ
347
348 /* released on free_profile */
01e2b670 349 rcu_assign_pointer(profile->parent, aa_get_profile(parent));
98849dff 350 profile->ns = aa_get_ns(parent->ns);
11c236b8
JJ
351 profile->file.dfa = aa_get_dfa(nulldfa);
352 profile->policy.dfa = aa_get_dfa(nulldfa);
4da05cc0 353
01e2b670 354 mutex_lock(&profile->ns->lock);
4da05cc0 355 __list_add_profile(&parent->base.profiles, profile);
01e2b670 356 mutex_unlock(&profile->ns->lock);
4da05cc0
JJ
357
358 /* refcount released by caller */
181f7c97
JJ
359out:
360 kfree(name);
361
4da05cc0
JJ
362 return profile;
363
364fail:
181f7c97
JJ
365 kfree(name);
366 aa_free_profile(profile);
4da05cc0
JJ
367 return NULL;
368}
369
c88d4c7b
JJ
370/* TODO: profile accounting - setup in remove */
371
372/**
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)
376 *
01e2b670 377 * Requires: rcu_read_lock be held
c88d4c7b
JJ
378 *
379 * Returns: unrefcounted profile ptr, or NULL if not found
380 */
381static struct aa_profile *__find_child(struct list_head *head, const char *name)
382{
383 return (struct aa_profile *)__policy_find(head, name);
384}
385
386/**
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
391 *
01e2b670 392 * Requires: rcu_read_lock be held
c88d4c7b
JJ
393 *
394 * Returns: unrefcounted profile ptr, or NULL if not found
395 */
396static struct aa_profile *__strn_find_child(struct list_head *head,
397 const char *name, int len)
398{
399 return (struct aa_profile *)__policy_strn_find(head, name, len);
400}
401
402/**
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)
406 *
407 * Returns: a refcounted profile or NULL if not found
408 */
409struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
410{
411 struct aa_profile *profile;
412
01e2b670 413 rcu_read_lock();
de7c4cc9
JJ
414 do {
415 profile = __find_child(&parent->base.profiles, name);
416 } while (profile && !aa_get_profile_not0(profile));
01e2b670 417 rcu_read_unlock();
c88d4c7b
JJ
418
419 /* refcount released by caller */
420 return profile;
421}
422
423/**
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)
427 *
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.
431 *
01e2b670 432 * Requires: rcu_read_lock be held
c88d4c7b
JJ
433 *
434 * Returns: unrefcounted policy or NULL if not found
435 */
98849dff 436static struct aa_policy *__lookup_parent(struct aa_ns *ns,
c88d4c7b
JJ
437 const char *hname)
438{
439 struct aa_policy *policy;
440 struct aa_profile *profile = NULL;
441 char *split;
442
443 policy = &ns->base;
444
445 for (split = strstr(hname, "//"); split;) {
446 profile = __strn_find_child(&policy->profiles, hname,
447 split - hname);
448 if (!profile)
449 return NULL;
450 policy = &profile->base;
451 hname = split + 2;
452 split = strstr(hname, "//");
453 }
454 if (!profile)
455 return &ns->base;
456 return &profile->base;
457}
458
459/**
1741e9eb 460 * __lookupn_profile - lookup the profile matching @hname
c88d4c7b
JJ
461 * @base: base list to start looking up profile name from (NOT NULL)
462 * @hname: hierarchical profile name (NOT NULL)
1741e9eb 463 * @n: length of @hname
c88d4c7b 464 *
01e2b670 465 * Requires: rcu_read_lock be held
c88d4c7b
JJ
466 *
467 * Returns: unrefcounted profile pointer or NULL if not found
468 *
469 * Do a relative name lookup, recursing through profile tree.
470 */
1741e9eb
JJ
471static struct aa_profile *__lookupn_profile(struct aa_policy *base,
472 const char *hname, size_t n)
c88d4c7b
JJ
473{
474 struct aa_profile *profile = NULL;
1741e9eb 475 const char *split;
c88d4c7b 476
1741e9eb
JJ
477 for (split = strnstr(hname, "//", n); split;
478 split = strnstr(hname, "//", n)) {
c88d4c7b
JJ
479 profile = __strn_find_child(&base->profiles, hname,
480 split - hname);
481 if (!profile)
482 return NULL;
483
484 base = &profile->base;
1741e9eb 485 n -= split + 2 - hname;
c88d4c7b 486 hname = split + 2;
c88d4c7b
JJ
487 }
488
1741e9eb
JJ
489 if (n)
490 return __strn_find_child(&base->profiles, hname, n);
491 return NULL;
492}
c88d4c7b 493
1741e9eb
JJ
494static struct aa_profile *__lookup_profile(struct aa_policy *base,
495 const char *hname)
496{
497 return __lookupn_profile(base, hname, strlen(hname));
c88d4c7b
JJ
498}
499
500/**
501 * aa_lookup_profile - find a profile by its full or partial name
502 * @ns: the namespace to start from (NOT NULL)
503 * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL)
1741e9eb 504 * @n: size of @hname
c88d4c7b
JJ
505 *
506 * Returns: refcounted profile or NULL if not found
507 */
1741e9eb
JJ
508struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
509 size_t n)
c88d4c7b
JJ
510{
511 struct aa_profile *profile;
512
01e2b670
JJ
513 rcu_read_lock();
514 do {
1741e9eb 515 profile = __lookupn_profile(&ns->base, hname, n);
01e2b670
JJ
516 } while (profile && !aa_get_profile_not0(profile));
517 rcu_read_unlock();
c88d4c7b 518
bf83208e 519 /* the unconfined profile is not in the regular profile list */
1741e9eb 520 if (!profile && strncmp(hname, "unconfined", n) == 0)
fa2ac468 521 profile = aa_get_newest_profile(ns->unconfined);
bf83208e 522
c88d4c7b
JJ
523 /* refcount released by caller */
524 return profile;
525}
526
1741e9eb
JJ
527struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *hname)
528{
529 return aa_lookupn_profile(ns, hname, strlen(hname));
530}
31617ddf
JJ
531
532struct aa_profile *aa_fqlookupn_profile(struct aa_profile *base,
533 const char *fqname, size_t n)
534{
535 struct aa_profile *profile;
536 struct aa_ns *ns;
537 const char *name, *ns_name;
538 size_t ns_len;
539
540 name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
541 if (ns_name) {
542 ns = aa_findn_ns(base->ns, ns_name, ns_len);
543 if (!ns)
544 return NULL;
545 } else
546 ns = aa_get_ns(base->ns);
547
548 if (name)
549 profile = aa_lookupn_profile(ns, name, n - (name - fqname));
550 else if (ns)
551 /* default profile for ns, currently unconfined */
552 profile = aa_get_newest_profile(ns->unconfined);
553 else
554 profile = NULL;
555 aa_put_ns(ns);
556
557 return profile;
558}
559
c88d4c7b
JJ
560/**
561 * replacement_allowed - test to see if replacement is allowed
562 * @profile: profile to test if it can be replaced (MAYBE NULL)
563 * @noreplace: true if replacement shouldn't be allowed but addition is okay
564 * @info: Returns - info about why replacement failed (NOT NULL)
565 *
566 * Returns: %0 if replacement allowed else error code
567 */
568static int replacement_allowed(struct aa_profile *profile, int noreplace,
569 const char **info)
570{
571 if (profile) {
572 if (profile->flags & PFLAG_IMMUTABLE) {
573 *info = "cannot replace immutible profile";
574 return -EPERM;
575 } else if (noreplace) {
576 *info = "profile already exists";
577 return -EEXIST;
578 }
579 }
580 return 0;
581}
582
c88d4c7b
JJ
583/**
584 * aa_audit_policy - Do auditing of policy changes
a6f23300 585 * @profile: profile to check if it can manage policy
c88d4c7b
JJ
586 * @op: policy operation being performed
587 * @gfp: memory allocation flags
588 * @name: name of profile being manipulated (NOT NULL)
589 * @info: any extra information to be audited (MAYBE NULL)
590 * @error: error code
591 *
592 * Returns: the error to be returned after audit is done
593 */
a6f23300
JJ
594static int audit_policy(struct aa_profile *profile, int op, gfp_t gfp,
595 const char *name, const char *info, int error)
c88d4c7b
JJ
596{
597 struct common_audit_data sa;
3b3b0e4f 598 struct apparmor_audit_data aad = {0,};
50c205f5 599 sa.type = LSM_AUDIT_DATA_NONE;
3b3b0e4f
EP
600 sa.aad = &aad;
601 aad.op = op;
602 aad.name = name;
603 aad.info = info;
604 aad.error = error;
c88d4c7b 605
a6f23300 606 return aa_audit(AUDIT_APPARMOR_STATUS, profile, gfp,
c88d4c7b
JJ
607 &sa, NULL);
608}
609
58acf9d9
JJ
610bool policy_view_capable(void)
611{
612 struct user_namespace *user_ns = current_user_ns();
613 bool response = false;
614
615 if (ns_capable(user_ns, CAP_MAC_ADMIN))
616 response = true;
617
618 return response;
619}
620
621bool policy_admin_capable(void)
622{
623 return policy_view_capable() && !aa_g_lock_policy;
624}
625
c88d4c7b
JJ
626/**
627 * aa_may_manage_policy - can the current task manage policy
628 * @op: the policy manipulation operation being done
629 *
630 * Returns: true if the task is allowed to manipulate policy
631 */
632bool aa_may_manage_policy(int op)
633{
634 /* check if loading policy is locked out */
635 if (aa_g_lock_policy) {
a6f23300
JJ
636 audit_policy(__aa_current_profile(), op, GFP_KERNEL, NULL,
637 "policy_locked", -EACCES);
c88d4c7b
JJ
638 return 0;
639 }
640
58acf9d9 641 if (!policy_admin_capable()) {
a6f23300
JJ
642 audit_policy(__aa_current_profile(), op, GFP_KERNEL, NULL,
643 "not policy admin", -EACCES);
c88d4c7b
JJ
644 return 0;
645 }
646
647 return 1;
648}
649
dd51c848
JJ
650static struct aa_profile *__list_lookup_parent(struct list_head *lh,
651 struct aa_profile *profile)
652{
6e474e30 653 const char *base = basename(profile->base.hname);
dd51c848
JJ
654 long len = base - profile->base.hname;
655 struct aa_load_ent *ent;
656
657 /* parent won't have trailing // so remove from len */
658 if (len <= 2)
659 return NULL;
660 len -= 2;
661
662 list_for_each_entry(ent, lh, list) {
663 if (ent->new == profile)
664 continue;
665 if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
666 0 && ent->new->base.hname[len] == 0)
667 return ent->new;
668 }
669
670 return NULL;
671}
672
673/**
674 * __replace_profile - replace @old with @new on a list
675 * @old: profile to be replaced (NOT NULL)
676 * @new: profile to replace @old with (NOT NULL)
8399588a 677 * @share_proxy: transfer @old->proxy to @new
dd51c848
JJ
678 *
679 * Will duplicate and refcount elements that @new inherits from @old
680 * and will inherit @old children.
681 *
682 * refcount @new for list, put @old list refcount
683 *
684 * Requires: namespace list lock be held, or list not be shared
685 */
77b071b3 686static void __replace_profile(struct aa_profile *old, struct aa_profile *new,
8399588a 687 bool share_proxy)
dd51c848
JJ
688{
689 struct aa_profile *child, *tmp;
690
691 if (!list_empty(&old->base.profiles)) {
692 LIST_HEAD(lh);
01e2b670 693 list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
dd51c848
JJ
694
695 list_for_each_entry_safe(child, tmp, &lh, base.list) {
696 struct aa_profile *p;
697
698 list_del_init(&child->base.list);
699 p = __find_child(&new->base.profiles, child->base.name);
700 if (p) {
701 /* @p replaces @child */
8399588a 702 __replace_profile(child, p, share_proxy);
dd51c848
JJ
703 continue;
704 }
705
706 /* inherit @child and its children */
707 /* TODO: update hname of inherited children */
708 /* list refcount transferred to @new */
0d259f04 709 p = aa_deref_parent(child);
01e2b670
JJ
710 rcu_assign_pointer(child->parent, aa_get_profile(new));
711 list_add_rcu(&child->base.list, &new->base.profiles);
712 aa_put_profile(p);
dd51c848
JJ
713 }
714 }
715
01e2b670 716 if (!rcu_access_pointer(new->parent)) {
0d259f04 717 struct aa_profile *parent = aa_deref_parent(old);
01e2b670
JJ
718 rcu_assign_pointer(new->parent, aa_get_profile(parent));
719 }
8399588a
JJ
720 __aa_update_proxy(old, new);
721 if (share_proxy) {
722 aa_put_proxy(new->proxy);
723 new->proxy = aa_get_proxy(old->proxy);
724 } else if (!rcu_access_pointer(new->proxy->profile))
725 /* aafs interface uses proxy */
726 rcu_assign_pointer(new->proxy->profile,
0d259f04
JJ
727 aa_get_profile(new));
728 __aa_fs_profile_migrate_dents(old, new);
dd51c848
JJ
729
730 if (list_empty(&new->base.list)) {
731 /* new is not on a list already */
01e2b670 732 list_replace_rcu(&old->base.list, &new->base.list);
dd51c848
JJ
733 aa_get_profile(new);
734 aa_put_profile(old);
735 } else
736 __list_remove_profile(old);
737}
738
739/**
740 * __lookup_replace - lookup replacement information for a profile
741 * @ns - namespace the lookup occurs in
742 * @hname - name of profile to lookup
743 * @noreplace - true if not replacing an existing profile
744 * @p - Returns: profile to be replaced
745 * @info - Returns: info string on why lookup failed
746 *
747 * Returns: profile to replace (no ref) on success else ptr error
748 */
98849dff 749static int __lookup_replace(struct aa_ns *ns, const char *hname,
dd51c848
JJ
750 bool noreplace, struct aa_profile **p,
751 const char **info)
752{
753 *p = aa_get_profile(__lookup_profile(&ns->base, hname));
754 if (*p) {
755 int error = replacement_allowed(*p, noreplace, info);
756 if (error) {
757 *info = "profile can not be replaced";
758 return error;
759 }
760 }
761
762 return 0;
763}
764
c88d4c7b
JJ
765/**
766 * aa_replace_profiles - replace profile(s) on the profile list
73688d1e 767 * @view: namespace load is viewed from
a6f23300 768 * @profile: profile that is attempting to load/replace policy
c88d4c7b
JJ
769 * @udata: serialized data stream (NOT NULL)
770 * @size: size of the serialized data stream
771 * @noreplace: true if only doing addition, no replacement allowed
772 *
773 * unpack and replace a profile on the profile list and uses of that profile
774 * by any aa_task_cxt. If the profile does not exist on the profile list
775 * it is added.
776 *
777 * Returns: size of data consumed else error code on failure.
778 */
73688d1e
JJ
779ssize_t aa_replace_profiles(struct aa_ns *view, void *udata, size_t size,
780 bool noreplace)
c88d4c7b 781{
bf15cf0c 782 const char *ns_name, *info = NULL;
98849dff 783 struct aa_ns *ns = NULL;
dd51c848 784 struct aa_load_ent *ent, *tmp;
c88d4c7b
JJ
785 int op = OP_PROF_REPL;
786 ssize_t error;
dd51c848 787 LIST_HEAD(lh);
c88d4c7b
JJ
788
789 /* released below */
dd51c848
JJ
790 error = aa_unpack(udata, size, &lh, &ns_name);
791 if (error)
792 goto out;
c88d4c7b
JJ
793
794 /* released below */
73688d1e 795 ns = aa_prepare_ns(view, ns_name);
c88d4c7b 796 if (!ns) {
a6f23300
JJ
797 error = audit_policy(__aa_current_profile(), op, GFP_KERNEL,
798 ns_name,
bf15cf0c
JJ
799 "failed to prepare namespace", -ENOMEM);
800 goto free;
c88d4c7b
JJ
801 }
802
01e2b670 803 mutex_lock(&ns->lock);
dd51c848
JJ
804 /* setup parent and ns info */
805 list_for_each_entry(ent, &lh, list) {
806 struct aa_policy *policy;
dd51c848
JJ
807 error = __lookup_replace(ns, ent->new->base.hname, noreplace,
808 &ent->old, &info);
809 if (error)
810 goto fail_lock;
811
812 if (ent->new->rename) {
813 error = __lookup_replace(ns, ent->new->rename,
814 noreplace, &ent->rename,
815 &info);
816 if (error)
817 goto fail_lock;
c88d4c7b 818 }
c88d4c7b 819
dd51c848 820 /* released when @new is freed */
98849dff 821 ent->new->ns = aa_get_ns(ns);
dd51c848
JJ
822
823 if (ent->old || ent->rename)
824 continue;
825
826 /* no ref on policy only use inside lock */
827 policy = __lookup_parent(ns, ent->new->base.hname);
828 if (!policy) {
829 struct aa_profile *p;
830 p = __list_lookup_parent(&lh, ent->new);
831 if (!p) {
832 error = -ENOENT;
833 info = "parent does not exist";
dd51c848
JJ
834 goto fail_lock;
835 }
01e2b670
JJ
836 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
837 } else if (policy != &ns->base) {
dd51c848 838 /* released on profile replacement or free_profile */
01e2b670
JJ
839 struct aa_profile *p = (struct aa_profile *) policy;
840 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
841 }
dd51c848 842 }
c88d4c7b 843
0d259f04
JJ
844 /* create new fs entries for introspection if needed */
845 list_for_each_entry(ent, &lh, list) {
846 if (ent->old) {
847 /* inherit old interface files */
848
849 /* if (ent->rename)
850 TODO: support rename */
851 /* } else if (ent->rename) {
852 TODO: support rename */
853 } else {
854 struct dentry *parent;
855 if (rcu_access_pointer(ent->new->parent)) {
856 struct aa_profile *p;
857 p = aa_deref_parent(ent->new);
858 parent = prof_child_dir(p);
859 } else
860 parent = ns_subprofs_dir(ent->new->ns);
861 error = __aa_fs_profile_mkdir(ent->new, parent);
862 }
863
864 if (error) {
865 info = "failed to create ";
866 goto fail_lock;
867 }
868 }
869
870 /* Done with checks that may fail - do actual replacement */
dd51c848
JJ
871 list_for_each_entry_safe(ent, tmp, &lh, list) {
872 list_del_init(&ent->list);
873 op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
874
a6f23300
JJ
875 audit_policy(__aa_current_profile(), op, GFP_ATOMIC,
876 ent->new->base.hname, NULL, error);
dd51c848
JJ
877
878 if (ent->old) {
77b071b3 879 __replace_profile(ent->old, ent->new, 1);
0d259f04 880 if (ent->rename) {
8399588a
JJ
881 /* aafs interface uses proxy */
882 struct aa_proxy *r = ent->new->proxy;
0d259f04
JJ
883 rcu_assign_pointer(r->profile,
884 aa_get_profile(ent->new));
77b071b3 885 __replace_profile(ent->rename, ent->new, 0);
0d259f04 886 }
dd51c848 887 } else if (ent->rename) {
8399588a
JJ
888 /* aafs interface uses proxy */
889 rcu_assign_pointer(ent->new->proxy->profile,
0d259f04 890 aa_get_profile(ent->new));
77b071b3 891 __replace_profile(ent->rename, ent->new, 0);
dd51c848 892 } else if (ent->new->parent) {
01e2b670 893 struct aa_profile *parent, *newest;
0d259f04 894 parent = aa_deref_parent(ent->new);
77b071b3 895 newest = aa_get_newest_profile(parent);
01e2b670 896
dd51c848 897 /* parent replaced in this atomic set? */
01e2b670
JJ
898 if (newest != parent) {
899 aa_get_profile(newest);
01e2b670 900 rcu_assign_pointer(ent->new->parent, newest);
f351841f 901 aa_put_profile(parent);
dcda617a 902 }
8399588a
JJ
903 /* aafs interface uses proxy */
904 rcu_assign_pointer(ent->new->proxy->profile,
0d259f04 905 aa_get_profile(ent->new));
ec34fa24 906 __list_add_profile(&newest->base.profiles, ent->new);
dcda617a 907 aa_put_profile(newest);
0d259f04 908 } else {
8399588a
JJ
909 /* aafs interface uses proxy */
910 rcu_assign_pointer(ent->new->proxy->profile,
0d259f04 911 aa_get_profile(ent->new));
dd51c848 912 __list_add_profile(&ns->base.profiles, ent->new);
0d259f04 913 }
dd51c848 914 aa_load_ent_free(ent);
c88d4c7b 915 }
01e2b670 916 mutex_unlock(&ns->lock);
c88d4c7b
JJ
917
918out:
98849dff 919 aa_put_ns(ns);
dd51c848 920
c88d4c7b
JJ
921 if (error)
922 return error;
923 return size;
924
dd51c848 925fail_lock:
01e2b670 926 mutex_unlock(&ns->lock);
dd51c848 927
bf15cf0c
JJ
928 /* audit cause of failure */
929 op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
a6f23300
JJ
930 audit_policy(__aa_current_profile(), op, GFP_KERNEL,
931 ent->new->base.hname, info, error);
bf15cf0c
JJ
932 /* audit status that rest of profiles in the atomic set failed too */
933 info = "valid profile in failed atomic policy load";
934 list_for_each_entry(tmp, &lh, list) {
935 if (tmp == ent) {
936 info = "unchecked profile in failed atomic policy load";
937 /* skip entry that caused failure */
938 continue;
939 }
940 op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
a6f23300
JJ
941 audit_policy(__aa_current_profile(), op, GFP_KERNEL,
942 tmp->new->base.hname, info, error);
bf15cf0c
JJ
943 }
944free:
dd51c848
JJ
945 list_for_each_entry_safe(ent, tmp, &lh, list) {
946 list_del_init(&ent->list);
947 aa_load_ent_free(ent);
948 }
949
c88d4c7b
JJ
950 goto out;
951}
952
953/**
954 * aa_remove_profiles - remove profile(s) from the system
b79473f2 955 * @view: namespace the remove is being done from
c88d4c7b
JJ
956 * @fqname: name of the profile or namespace to remove (NOT NULL)
957 * @size: size of the name
958 *
959 * Remove a profile or sub namespace from the current namespace, so that
960 * they can not be found anymore and mark them as replaced by unconfined
961 *
962 * NOTE: removing confinement does not restore rlimits to preconfinemnet values
963 *
964 * Returns: size of data consume else error code if fails
965 */
b79473f2 966ssize_t aa_remove_profiles(struct aa_ns *view, char *fqname, size_t size)
c88d4c7b 967{
b79473f2 968 struct aa_ns *root = NULL, *ns = NULL;
c88d4c7b
JJ
969 struct aa_profile *profile = NULL;
970 const char *name = fqname, *info = NULL;
971 ssize_t error = 0;
972
973 if (*fqname == 0) {
974 info = "no profile specified";
975 error = -ENOENT;
976 goto fail;
977 }
978
b79473f2 979 root = view;
c88d4c7b
JJ
980
981 if (fqname[0] == ':') {
982 char *ns_name;
983 name = aa_split_fqname(fqname, &ns_name);
41d1b3e8 984 /* released below */
98849dff 985 ns = aa_find_ns(root, ns_name);
41d1b3e8
JJ
986 if (!ns) {
987 info = "namespace does not exist";
988 error = -ENOENT;
989 goto fail;
c88d4c7b
JJ
990 }
991 } else
992 /* released below */
98849dff 993 ns = aa_get_ns(root);
c88d4c7b 994
c88d4c7b
JJ
995 if (!name) {
996 /* remove namespace - can only happen if fqname[0] == ':' */
01e2b670 997 mutex_lock(&ns->parent->lock);
98849dff 998 __aa_remove_ns(ns);
01e2b670 999 mutex_unlock(&ns->parent->lock);
c88d4c7b
JJ
1000 } else {
1001 /* remove profile */
01e2b670 1002 mutex_lock(&ns->lock);
c88d4c7b
JJ
1003 profile = aa_get_profile(__lookup_profile(&ns->base, name));
1004 if (!profile) {
1005 error = -ENOENT;
1006 info = "profile does not exist";
1007 goto fail_ns_lock;
1008 }
1009 name = profile->base.hname;
1010 __remove_profile(profile);
01e2b670 1011 mutex_unlock(&ns->lock);
c88d4c7b 1012 }
c88d4c7b
JJ
1013
1014 /* don't fail removal if audit fails */
a6f23300
JJ
1015 (void) audit_policy(__aa_current_profile(), OP_PROF_RM, GFP_KERNEL,
1016 name, info, error);
98849dff 1017 aa_put_ns(ns);
c88d4c7b
JJ
1018 aa_put_profile(profile);
1019 return size;
1020
1021fail_ns_lock:
01e2b670 1022 mutex_unlock(&ns->lock);
98849dff 1023 aa_put_ns(ns);
c88d4c7b
JJ
1024
1025fail:
a6f23300
JJ
1026 (void) audit_policy(__aa_current_profile(), OP_PROF_RM, GFP_KERNEL,
1027 name, info, error);
c88d4c7b
JJ
1028 return error;
1029}