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