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