]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - security/apparmor/policy.c
apparmor: fix: permissions test to view and manage 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();
451dc704
JJ
624 bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) ||
625 in_egroup_p(make_kgid(user_ns, 0));
58acf9d9
JJ
626 bool response = false;
627
451dc704 628 if (root_in_user_ns &&
8ccc1769 629 (user_ns == &init_user_ns ||
590a154b 630 (unprivileged_userns_apparmor_policy != 0 &&
451dc704 631 user_ns->level == ns->level)))
8ccc1769
TH
632 response = true;
633 aa_put_ns(ns);
634
635 return response;
636}
637
94a9bc95 638bool policy_admin_capable(void)
8ccc1769
TH
639{
640 struct user_namespace *user_ns = current_user_ns();
451dc704 641 bool capable = ns_capable(user_ns, CAP_MAC_ADMIN);
8ccc1769 642
451dc704
JJ
643 AA_DEBUG("cap_mac_admin? %d\n", capable);
644 AA_DEBUG("policy locked? %d\n", aa_g_lock_policy);
58acf9d9 645
451dc704 646 return policy_view_capable() && capable && !aa_g_lock_policy;
58acf9d9
JJ
647}
648
c88d4c7b
JJ
649/**
650 * aa_may_manage_policy - can the current task manage policy
80594fc2 651 * @label: label to check if it can manage policy
c88d4c7b
JJ
652 * @op: the policy manipulation operation being done
653 *
80594fc2 654 * Returns: 0 if the task is allowed to manipulate policy else error
c88d4c7b 655 */
80594fc2 656int aa_may_manage_policy(struct aa_label *label, u32 mask)
c88d4c7b 657{
80594fc2
JJ
658 const char *op;
659
660 if (mask & AA_MAY_REMOVE_POLICY)
661 op = OP_PROF_RM;
662 else if (mask & AA_MAY_REPLACE_POLICY)
663 op = OP_PROF_REPL;
664 else
665 op = OP_PROF_LOAD;
666
c88d4c7b 667 /* check if loading policy is locked out */
80594fc2
JJ
668 if (aa_g_lock_policy)
669 return audit_policy(label, op, NULL, NULL, "policy_locked",
670 -EACCES);
c88d4c7b 671
80594fc2
JJ
672 if (!policy_admin_capable())
673 return audit_policy(label, op, NULL, NULL, "not policy admin",
674 -EACCES);
c88d4c7b 675
80594fc2
JJ
676 /* TODO: add fine grained mediation of policy loads */
677 return 0;
c88d4c7b
JJ
678}
679
dd51c848
JJ
680static struct aa_profile *__list_lookup_parent(struct list_head *lh,
681 struct aa_profile *profile)
682{
80594fc2 683 const char *base = basename(profile->base.hname);
dd51c848
JJ
684 long len = base - profile->base.hname;
685 struct aa_load_ent *ent;
686
687 /* parent won't have trailing // so remove from len */
688 if (len <= 2)
689 return NULL;
690 len -= 2;
691
692 list_for_each_entry(ent, lh, list) {
693 if (ent->new == profile)
694 continue;
695 if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
696 0 && ent->new->base.hname[len] == 0)
697 return ent->new;
698 }
699
700 return NULL;
701}
702
703/**
704 * __replace_profile - replace @old with @new on a list
705 * @old: profile to be replaced (NOT NULL)
706 * @new: profile to replace @old with (NOT NULL)
707 *
708 * Will duplicate and refcount elements that @new inherits from @old
709 * and will inherit @old children.
710 *
711 * refcount @new for list, put @old list refcount
712 *
713 * Requires: namespace list lock be held, or list not be shared
714 */
80594fc2 715static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
dd51c848
JJ
716{
717 struct aa_profile *child, *tmp;
718
719 if (!list_empty(&old->base.profiles)) {
720 LIST_HEAD(lh);
01e2b670 721 list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
dd51c848
JJ
722
723 list_for_each_entry_safe(child, tmp, &lh, base.list) {
724 struct aa_profile *p;
725
726 list_del_init(&child->base.list);
727 p = __find_child(&new->base.profiles, child->base.name);
728 if (p) {
729 /* @p replaces @child */
80594fc2 730 __replace_profile(child, p);
dd51c848
JJ
731 continue;
732 }
733
734 /* inherit @child and its children */
735 /* TODO: update hname of inherited children */
736 /* list refcount transferred to @new */
0d259f04 737 p = aa_deref_parent(child);
01e2b670
JJ
738 rcu_assign_pointer(child->parent, aa_get_profile(new));
739 list_add_rcu(&child->base.list, &new->base.profiles);
740 aa_put_profile(p);
dd51c848
JJ
741 }
742 }
743
01e2b670 744 if (!rcu_access_pointer(new->parent)) {
0d259f04 745 struct aa_profile *parent = aa_deref_parent(old);
01e2b670
JJ
746 rcu_assign_pointer(new->parent, aa_get_profile(parent));
747 }
80594fc2
JJ
748 aa_label_replace(&old->label, &new->label);
749 /* migrate dents must come after label replacement b/c update */
0d259f04 750 __aa_fs_profile_migrate_dents(old, new);
dd51c848
JJ
751
752 if (list_empty(&new->base.list)) {
753 /* new is not on a list already */
01e2b670 754 list_replace_rcu(&old->base.list, &new->base.list);
dd51c848
JJ
755 aa_get_profile(new);
756 aa_put_profile(old);
757 } else
758 __list_remove_profile(old);
759}
760
761/**
762 * __lookup_replace - lookup replacement information for a profile
763 * @ns - namespace the lookup occurs in
764 * @hname - name of profile to lookup
765 * @noreplace - true if not replacing an existing profile
766 * @p - Returns: profile to be replaced
767 * @info - Returns: info string on why lookup failed
768 *
769 * Returns: profile to replace (no ref) on success else ptr error
770 */
80594fc2 771static int __lookup_replace(struct aa_ns *ns, const char *hname,
dd51c848
JJ
772 bool noreplace, struct aa_profile **p,
773 const char **info)
774{
775 *p = aa_get_profile(__lookup_profile(&ns->base, hname));
776 if (*p) {
777 int error = replacement_allowed(*p, noreplace, info);
778 if (error) {
779 *info = "profile can not be replaced";
780 return error;
781 }
782 }
783
784 return 0;
785}
786
80594fc2
JJ
787static void share_name(struct aa_profile *old, struct aa_profile *new)
788{
789 aa_put_str(new->base.hname);
790 aa_get_str(old->base.hname);
791 new->base.hname = old->base.hname;
792 new->base.name = old->base.name;
793 new->label.hname = old->label.hname;
794}
795
796/* Update to newest version of parent after previous replacements
797 * Returns: unrefcount newest version of parent
798 */
799static struct aa_profile *update_to_newest_parent(struct aa_profile *new)
800{
801 struct aa_profile *parent, *newest;
802 parent = rcu_dereference_protected(new->parent,
803 mutex_is_locked(&new->ns->lock));
804 newest = aa_get_newest_profile(parent);
805
806 /* parent replaced in this atomic set? */
807 if (newest != parent) {
808 aa_put_profile(parent);
809 rcu_assign_pointer(new->parent, newest);
810 } else
811 aa_put_profile(newest);
812
813 return newest;
814}
815
c88d4c7b
JJ
816/**
817 * aa_replace_profiles - replace profile(s) on the profile list
80594fc2
JJ
818 * @label: label that is attempting to load/replace policy
819 * @mask: permission mask
c88d4c7b
JJ
820 * @udata: serialized data stream (NOT NULL)
821 * @size: size of the serialized data stream
c88d4c7b
JJ
822 *
823 * unpack and replace a profile on the profile list and uses of that profile
80594fc2 824 * by any aa_task_ctx. If the profile does not exist on the profile list
c88d4c7b
JJ
825 * it is added.
826 *
827 * Returns: size of data consumed else error code on failure.
828 */
80594fc2
JJ
829ssize_t aa_replace_profiles(struct aa_label *label, u32 mask, void *udata,
830 size_t size)
c88d4c7b 831{
bf15cf0c 832 const char *ns_name, *info = NULL;
80594fc2 833 struct aa_ns *ns = NULL;
dd51c848 834 struct aa_load_ent *ent, *tmp;
80594fc2
JJ
835 const char *op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD;
836 ssize_t count, error;
837
dd51c848 838 LIST_HEAD(lh);
c88d4c7b
JJ
839
840 /* released below */
dd51c848
JJ
841 error = aa_unpack(udata, size, &lh, &ns_name);
842 if (error)
843 goto out;
c88d4c7b 844
80594fc2
JJ
845 /* ensure that profiles are all for the same ns
846 * TODO: update locking to remove this constaint. All profiles in
847 * the load set must succeed as a set or the load will
848 * fail. Sort ent list and take ns locks in hierarchy order
849 */
850 count = 0;
851 list_for_each_entry(ent, &lh, list) {
852 if (ns_name) {
853 if (ent->ns_name &&
854 strcmp(ent->ns_name, ns_name) != 0) {
855 info = "policy load has mixed namespaces";
856 error = -EACCES;
857 goto fail;
858 }
859 } else if (ent->ns_name) {
860 if (count) {
861 info = "policy load has mixed namespaces";
862 error = -EACCES;
863 goto fail;
864 }
865 ns_name = ent->ns_name;
866 } else
867 count++;
c88d4c7b 868 }
80594fc2
JJ
869 if (ns_name) {
870 ns = aa_prepare_ns(labels_ns(label), ns_name);
871 if (!ns) {
872 info = "failed to prepare namespace";
873 error = -ENOMEM;
874 goto fail;
875 }
876 } else
877 ns = aa_get_ns(labels_ns(label));
c88d4c7b 878
01e2b670 879 mutex_lock(&ns->lock);
dd51c848
JJ
880 /* setup parent and ns info */
881 list_for_each_entry(ent, &lh, list) {
882 struct aa_policy *policy;
80594fc2
JJ
883
884 error = __lookup_replace(ns, ent->new->base.hname,
885 !(mask & AA_MAY_REPLACE_POLICY),
dd51c848
JJ
886 &ent->old, &info);
887 if (error)
888 goto fail_lock;
889
890 if (ent->new->rename) {
891 error = __lookup_replace(ns, ent->new->rename,
80594fc2
JJ
892 !(mask & AA_MAY_REPLACE_POLICY),
893 &ent->rename, &info);
dd51c848
JJ
894 if (error)
895 goto fail_lock;
c88d4c7b 896 }
c88d4c7b 897
dd51c848 898 /* released when @new is freed */
80594fc2 899 ent->new->ns = aa_get_ns(ns);
dd51c848
JJ
900
901 if (ent->old || ent->rename)
902 continue;
903
904 /* no ref on policy only use inside lock */
905 policy = __lookup_parent(ns, ent->new->base.hname);
906 if (!policy) {
907 struct aa_profile *p;
908 p = __list_lookup_parent(&lh, ent->new);
909 if (!p) {
910 error = -ENOENT;
911 info = "parent does not exist";
dd51c848
JJ
912 goto fail_lock;
913 }
01e2b670
JJ
914 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
915 } else if (policy != &ns->base) {
dd51c848 916 /* released on profile replacement or free_profile */
01e2b670
JJ
917 struct aa_profile *p = (struct aa_profile *) policy;
918 rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
919 }
dd51c848 920 }
c88d4c7b 921
0d259f04
JJ
922 /* create new fs entries for introspection if needed */
923 list_for_each_entry(ent, &lh, list) {
80594fc2 924 if (!ent->old) {
0d259f04
JJ
925 struct dentry *parent;
926 if (rcu_access_pointer(ent->new->parent)) {
927 struct aa_profile *p;
928 p = aa_deref_parent(ent->new);
929 parent = prof_child_dir(p);
930 } else
931 parent = ns_subprofs_dir(ent->new->ns);
932 error = __aa_fs_profile_mkdir(ent->new, parent);
933 }
934
935 if (error) {
80594fc2 936 info = "failed to create";
0d259f04
JJ
937 goto fail_lock;
938 }
939 }
940
941 /* Done with checks that may fail - do actual replacement */
dd51c848
JJ
942 list_for_each_entry_safe(ent, tmp, &lh, list) {
943 list_del_init(&ent->list);
944 op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
945
80594fc2 946 audit_policy(label, op, ns_name, ent->new->base.hname, NULL, error);
dd51c848
JJ
947
948 if (ent->old) {
80594fc2
JJ
949 share_name(ent->old, ent->new);
950 __replace_profile(ent->old, ent->new);
951 if (ent->rename)
952 __replace_profile(ent->rename, ent->new);
dd51c848 953 } else if (ent->rename) {
80594fc2
JJ
954 /* TODO: case not actually supported yet */
955 ;
0d259f04 956 } else {
80594fc2
JJ
957 struct list_head *lh;
958 if (rcu_access_pointer(ent->new->parent)) {
959 struct aa_profile *parent;
960 parent = update_to_newest_parent(ent->new);
961 lh = &parent->base.profiles;
962 } else
963 lh = &ns->base.profiles;
964 __add_profile(lh, ent->new);
0d259f04 965 }
dd51c848 966 aa_load_ent_free(ent);
c88d4c7b 967 }
80594fc2 968 __aa_labelset_update_subtree(ns);
01e2b670 969 mutex_unlock(&ns->lock);
c88d4c7b
JJ
970
971out:
80594fc2 972 aa_put_ns(ns);
dd51c848 973
c88d4c7b
JJ
974 if (error)
975 return error;
976 return size;
977
dd51c848 978fail_lock:
01e2b670 979 mutex_unlock(&ns->lock);
dd51c848 980
bf15cf0c
JJ
981 /* audit cause of failure */
982 op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
80594fc2
JJ
983fail:
984 audit_policy(label, op, ns_name, ent->new->base.hname, info, error);
bf15cf0c
JJ
985 /* audit status that rest of profiles in the atomic set failed too */
986 info = "valid profile in failed atomic policy load";
987 list_for_each_entry(tmp, &lh, list) {
988 if (tmp == ent) {
989 info = "unchecked profile in failed atomic policy load";
990 /* skip entry that caused failure */
991 continue;
992 }
993 op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
80594fc2 994 audit_policy(label, op, ns_name, tmp->new->base.hname, info, error);
bf15cf0c 995 }
dd51c848
JJ
996 list_for_each_entry_safe(ent, tmp, &lh, list) {
997 list_del_init(&ent->list);
998 aa_load_ent_free(ent);
999 }
1000
c88d4c7b
JJ
1001 goto out;
1002}
1003
1004/**
1005 * aa_remove_profiles - remove profile(s) from the system
80594fc2 1006 * @label: label attempting to remove policy
c88d4c7b
JJ
1007 * @fqname: name of the profile or namespace to remove (NOT NULL)
1008 * @size: size of the name
1009 *
1010 * Remove a profile or sub namespace from the current namespace, so that
1011 * they can not be found anymore and mark them as replaced by unconfined
1012 *
1013 * NOTE: removing confinement does not restore rlimits to preconfinemnet values
1014 *
1015 * Returns: size of data consume else error code if fails
1016 */
80594fc2 1017ssize_t aa_remove_profiles(struct aa_label *label, char *fqname, size_t size)
c88d4c7b 1018{
80594fc2 1019 struct aa_ns *root = NULL, *ns = NULL;
c88d4c7b
JJ
1020 struct aa_profile *profile = NULL;
1021 const char *name = fqname, *info = NULL;
80594fc2 1022 char *ns_name = NULL;
c88d4c7b
JJ
1023 ssize_t error = 0;
1024
1025 if (*fqname == 0) {
1026 info = "no profile specified";
1027 error = -ENOENT;
1028 goto fail;
1029 }
1030
80594fc2 1031 root = labels_ns(label);
c88d4c7b
JJ
1032
1033 if (fqname[0] == ':') {
c88d4c7b 1034 name = aa_split_fqname(fqname, &ns_name);
41d1b3e8 1035 /* released below */
80594fc2 1036 ns = aa_find_ns(root, ns_name);
41d1b3e8
JJ
1037 if (!ns) {
1038 info = "namespace does not exist";
1039 error = -ENOENT;
1040 goto fail;
c88d4c7b
JJ
1041 }
1042 } else
1043 /* released below */
80594fc2 1044 ns = aa_get_ns(root);
c88d4c7b 1045
c88d4c7b
JJ
1046 if (!name) {
1047 /* remove namespace - can only happen if fqname[0] == ':' */
01e2b670 1048 mutex_lock(&ns->parent->lock);
80594fc2 1049 __aa_remove_ns(ns);
01e2b670 1050 mutex_unlock(&ns->parent->lock);
c88d4c7b
JJ
1051 } else {
1052 /* remove profile */
01e2b670 1053 mutex_lock(&ns->lock);
c88d4c7b
JJ
1054 profile = aa_get_profile(__lookup_profile(&ns->base, name));
1055 if (!profile) {
1056 error = -ENOENT;
1057 info = "profile does not exist";
1058 goto fail_ns_lock;
1059 }
1060 name = profile->base.hname;
1061 __remove_profile(profile);
80594fc2 1062 __aa_labelset_update_subtree(ns);
01e2b670 1063 mutex_unlock(&ns->lock);
c88d4c7b 1064 }
c88d4c7b
JJ
1065
1066 /* don't fail removal if audit fails */
80594fc2 1067 (void) audit_policy(label, OP_PROF_RM, ns_name, name, info, error);
c88d4c7b
JJ
1068 aa_put_profile(profile);
1069 return size;
1070
1071fail_ns_lock:
01e2b670 1072 mutex_unlock(&ns->lock);
c88d4c7b
JJ
1073
1074fail:
80594fc2 1075 (void) audit_policy(label, OP_PROF_RM, ns_name, name, info, error);
c88d4c7b
JJ
1076 return error;
1077}