Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * AppArmor security module
0004  *
0005  * This file contains AppArmor policy manipulation functions
0006  *
0007  * Copyright (C) 1998-2008 Novell/SUSE
0008  * Copyright 2009-2010 Canonical Ltd.
0009  *
0010  * AppArmor policy is based around profiles, which contain the rules a
0011  * task is confined by.  Every task in the system has a profile attached
0012  * to it determined either by matching "unconfined" tasks against the
0013  * visible set of profiles or by following a profiles attachment rules.
0014  *
0015  * Each profile exists in a profile namespace which is a container of
0016  * visible profiles.  Each namespace contains a special "unconfined" profile,
0017  * which doesn't enforce any confinement on a task beyond DAC.
0018  *
0019  * Namespace and profile names can be written together in either
0020  * of two syntaxes.
0021  *  :namespace:profile - used by kernel interfaces for easy detection
0022  *  namespace://profile - used by policy
0023  *
0024  * Profile names can not start with : or @ or ^ and may not contain \0
0025  *
0026  * Reserved profile names
0027  *  unconfined - special automatically generated unconfined profile
0028  *  inherit - special name to indicate profile inheritance
0029  *  null-XXXX-YYYY - special automatically generated learning profiles
0030  *
0031  * Namespace names may not start with / or @ and may not contain \0 or :
0032  * Reserved namespace names
0033  *  user-XXXX - user defined profiles
0034  *
0035  * a // in a profile or namespace name indicates a hierarchical name with the
0036  * name before the // being the parent and the name after the child.
0037  *
0038  * Profile and namespace hierarchies serve two different but similar purposes.
0039  * The namespace contains the set of visible profiles that are considered
0040  * for attachment.  The hierarchy of namespaces allows for virtualizing
0041  * the namespace so that for example a chroot can have its own set of profiles
0042  * which may define some local user namespaces.
0043  * The profile hierarchy severs two distinct purposes,
0044  * -  it allows for sub profiles or hats, which allows an application to run
0045  *    subprograms under its own profile with different restriction than it
0046  *    self, and not have it use the system profile.
0047  *    eg. if a mail program starts an editor, the policy might make the
0048  *        restrictions tighter on the editor tighter than the mail program,
0049  *        and definitely different than general editor restrictions
0050  * - it allows for binary hierarchy of profiles, so that execution history
0051  *   is preserved.  This feature isn't exploited by AppArmor reference policy
0052  *   but is allowed.  NOTE: this is currently suboptimal because profile
0053  *   aliasing is not currently implemented so that a profile for each
0054  *   level must be defined.
0055  *   eg. /bin/bash///bin/ls as a name would indicate /bin/ls was started
0056  *       from /bin/bash
0057  *
0058  *   A profile or namespace name that can contain one or more // separators
0059  *   is referred to as an hname (hierarchical).
0060  *   eg.  /bin/bash//bin/ls
0061  *
0062  *   An fqname is a name that may contain both namespace and profile hnames.
0063  *   eg. :ns:/bin/bash//bin/ls
0064  *
0065  * NOTES:
0066  *   - locking of profile lists is currently fairly coarse.  All profile
0067  *     lists within a namespace use the namespace lock.
0068  * FIXME: move profile lists to using rcu_lists
0069  */
0070 
0071 #include <linux/slab.h>
0072 #include <linux/spinlock.h>
0073 #include <linux/string.h>
0074 #include <linux/cred.h>
0075 #include <linux/rculist.h>
0076 #include <linux/user_namespace.h>
0077 
0078 #include "include/apparmor.h"
0079 #include "include/capability.h"
0080 #include "include/cred.h"
0081 #include "include/file.h"
0082 #include "include/ipc.h"
0083 #include "include/match.h"
0084 #include "include/path.h"
0085 #include "include/policy.h"
0086 #include "include/policy_ns.h"
0087 #include "include/policy_unpack.h"
0088 #include "include/resource.h"
0089 
0090 int unprivileged_userns_apparmor_policy = 1;
0091 
0092 const char *const aa_profile_mode_names[] = {
0093     "enforce",
0094     "complain",
0095     "kill",
0096     "unconfined",
0097 };
0098 
0099 
0100 /**
0101  * __add_profile - add a profiles to list and label tree
0102  * @list: list to add it to  (NOT NULL)
0103  * @profile: the profile to add  (NOT NULL)
0104  *
0105  * refcount @profile, should be put by __list_remove_profile
0106  *
0107  * Requires: namespace lock be held, or list not be shared
0108  */
0109 static void __add_profile(struct list_head *list, struct aa_profile *profile)
0110 {
0111     struct aa_label *l;
0112 
0113     AA_BUG(!list);
0114     AA_BUG(!profile);
0115     AA_BUG(!profile->ns);
0116     AA_BUG(!mutex_is_locked(&profile->ns->lock));
0117 
0118     list_add_rcu(&profile->base.list, list);
0119     /* get list reference */
0120     aa_get_profile(profile);
0121     l = aa_label_insert(&profile->ns->labels, &profile->label);
0122     AA_BUG(l != &profile->label);
0123     aa_put_label(l);
0124 }
0125 
0126 /**
0127  * __list_remove_profile - remove a profile from the list it is on
0128  * @profile: the profile to remove  (NOT NULL)
0129  *
0130  * remove a profile from the list, warning generally removal should
0131  * be done with __replace_profile as most profile removals are
0132  * replacements to the unconfined profile.
0133  *
0134  * put @profile list refcount
0135  *
0136  * Requires: namespace lock be held, or list not have been live
0137  */
0138 static void __list_remove_profile(struct aa_profile *profile)
0139 {
0140     AA_BUG(!profile);
0141     AA_BUG(!profile->ns);
0142     AA_BUG(!mutex_is_locked(&profile->ns->lock));
0143 
0144     list_del_rcu(&profile->base.list);
0145     aa_put_profile(profile);
0146 }
0147 
0148 /**
0149  * __remove_profile - remove old profile, and children
0150  * @profile: profile to be replaced  (NOT NULL)
0151  *
0152  * Requires: namespace list lock be held, or list not be shared
0153  */
0154 static void __remove_profile(struct aa_profile *profile)
0155 {
0156     AA_BUG(!profile);
0157     AA_BUG(!profile->ns);
0158     AA_BUG(!mutex_is_locked(&profile->ns->lock));
0159 
0160     /* release any children lists first */
0161     __aa_profile_list_release(&profile->base.profiles);
0162     /* released by free_profile */
0163     aa_label_remove(&profile->label);
0164     __aafs_profile_rmdir(profile);
0165     __list_remove_profile(profile);
0166 }
0167 
0168 /**
0169  * __aa_profile_list_release - remove all profiles on the list and put refs
0170  * @head: list of profiles  (NOT NULL)
0171  *
0172  * Requires: namespace lock be held
0173  */
0174 void __aa_profile_list_release(struct list_head *head)
0175 {
0176     struct aa_profile *profile, *tmp;
0177     list_for_each_entry_safe(profile, tmp, head, base.list)
0178         __remove_profile(profile);
0179 }
0180 
0181 /**
0182  * aa_free_data - free a data blob
0183  * @ptr: data to free
0184  * @arg: unused
0185  */
0186 static void aa_free_data(void *ptr, void *arg)
0187 {
0188     struct aa_data *data = ptr;
0189 
0190     kfree_sensitive(data->data);
0191     kfree_sensitive(data->key);
0192     kfree_sensitive(data);
0193 }
0194 
0195 /**
0196  * aa_free_profile - free a profile
0197  * @profile: the profile to free  (MAYBE NULL)
0198  *
0199  * Free a profile, its hats and null_profile. All references to the profile,
0200  * its hats and null_profile must have been put.
0201  *
0202  * If the profile was referenced from a task context, free_profile() will
0203  * be called from an rcu callback routine, so we must not sleep here.
0204  */
0205 void aa_free_profile(struct aa_profile *profile)
0206 {
0207     struct rhashtable *rht;
0208     int i;
0209 
0210     AA_DEBUG("%s(%p)\n", __func__, profile);
0211 
0212     if (!profile)
0213         return;
0214 
0215     /* free children profiles */
0216     aa_policy_destroy(&profile->base);
0217     aa_put_profile(rcu_access_pointer(profile->parent));
0218 
0219     aa_put_ns(profile->ns);
0220     kfree_sensitive(profile->rename);
0221 
0222     aa_free_file_rules(&profile->file);
0223     aa_free_cap_rules(&profile->caps);
0224     aa_free_rlimit_rules(&profile->rlimits);
0225 
0226     for (i = 0; i < profile->xattr_count; i++)
0227         kfree_sensitive(profile->xattrs[i]);
0228     kfree_sensitive(profile->xattrs);
0229     for (i = 0; i < profile->secmark_count; i++)
0230         kfree_sensitive(profile->secmark[i].label);
0231     kfree_sensitive(profile->secmark);
0232     kfree_sensitive(profile->dirname);
0233     aa_put_dfa(profile->xmatch);
0234     aa_put_dfa(profile->policy.dfa);
0235 
0236     if (profile->data) {
0237         rht = profile->data;
0238         profile->data = NULL;
0239         rhashtable_free_and_destroy(rht, aa_free_data, NULL);
0240         kfree_sensitive(rht);
0241     }
0242 
0243     kfree_sensitive(profile->hash);
0244     aa_put_loaddata(profile->rawdata);
0245     aa_label_destroy(&profile->label);
0246 
0247     kfree_sensitive(profile);
0248 }
0249 
0250 /**
0251  * aa_alloc_profile - allocate, initialize and return a new profile
0252  * @hname: name of the profile  (NOT NULL)
0253  * @gfp: allocation type
0254  *
0255  * Returns: refcount profile or NULL on failure
0256  */
0257 struct aa_profile *aa_alloc_profile(const char *hname, struct aa_proxy *proxy,
0258                     gfp_t gfp)
0259 {
0260     struct aa_profile *profile;
0261 
0262     /* freed by free_profile - usually through aa_put_profile */
0263     profile = kzalloc(struct_size(profile, label.vec, 2), gfp);
0264     if (!profile)
0265         return NULL;
0266 
0267     if (!aa_policy_init(&profile->base, NULL, hname, gfp))
0268         goto fail;
0269     if (!aa_label_init(&profile->label, 1, gfp))
0270         goto fail;
0271 
0272     /* update being set needed by fs interface */
0273     if (!proxy) {
0274         proxy = aa_alloc_proxy(&profile->label, gfp);
0275         if (!proxy)
0276             goto fail;
0277     } else
0278         aa_get_proxy(proxy);
0279     profile->label.proxy = proxy;
0280 
0281     profile->label.hname = profile->base.hname;
0282     profile->label.flags |= FLAG_PROFILE;
0283     profile->label.vec[0] = profile;
0284 
0285     /* refcount released by caller */
0286     return profile;
0287 
0288 fail:
0289     aa_free_profile(profile);
0290 
0291     return NULL;
0292 }
0293 
0294 /* TODO: profile accounting - setup in remove */
0295 
0296 /**
0297  * __strn_find_child - find a profile on @head list using substring of @name
0298  * @head: list to search  (NOT NULL)
0299  * @name: name of profile (NOT NULL)
0300  * @len: length of @name substring to match
0301  *
0302  * Requires: rcu_read_lock be held
0303  *
0304  * Returns: unrefcounted profile ptr, or NULL if not found
0305  */
0306 static struct aa_profile *__strn_find_child(struct list_head *head,
0307                         const char *name, int len)
0308 {
0309     return (struct aa_profile *)__policy_strn_find(head, name, len);
0310 }
0311 
0312 /**
0313  * __find_child - find a profile on @head list with a name matching @name
0314  * @head: list to search  (NOT NULL)
0315  * @name: name of profile (NOT NULL)
0316  *
0317  * Requires: rcu_read_lock be held
0318  *
0319  * Returns: unrefcounted profile ptr, or NULL if not found
0320  */
0321 static struct aa_profile *__find_child(struct list_head *head, const char *name)
0322 {
0323     return __strn_find_child(head, name, strlen(name));
0324 }
0325 
0326 /**
0327  * aa_find_child - find a profile by @name in @parent
0328  * @parent: profile to search  (NOT NULL)
0329  * @name: profile name to search for  (NOT NULL)
0330  *
0331  * Returns: a refcounted profile or NULL if not found
0332  */
0333 struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name)
0334 {
0335     struct aa_profile *profile;
0336 
0337     rcu_read_lock();
0338     do {
0339         profile = __find_child(&parent->base.profiles, name);
0340     } while (profile && !aa_get_profile_not0(profile));
0341     rcu_read_unlock();
0342 
0343     /* refcount released by caller */
0344     return profile;
0345 }
0346 
0347 /**
0348  * __lookup_parent - lookup the parent of a profile of name @hname
0349  * @ns: namespace to lookup profile in  (NOT NULL)
0350  * @hname: hierarchical profile name to find parent of  (NOT NULL)
0351  *
0352  * Lookups up the parent of a fully qualified profile name, the profile
0353  * that matches hname does not need to exist, in general this
0354  * is used to load a new profile.
0355  *
0356  * Requires: rcu_read_lock be held
0357  *
0358  * Returns: unrefcounted policy or NULL if not found
0359  */
0360 static struct aa_policy *__lookup_parent(struct aa_ns *ns,
0361                      const char *hname)
0362 {
0363     struct aa_policy *policy;
0364     struct aa_profile *profile = NULL;
0365     char *split;
0366 
0367     policy = &ns->base;
0368 
0369     for (split = strstr(hname, "//"); split;) {
0370         profile = __strn_find_child(&policy->profiles, hname,
0371                         split - hname);
0372         if (!profile)
0373             return NULL;
0374         policy = &profile->base;
0375         hname = split + 2;
0376         split = strstr(hname, "//");
0377     }
0378     if (!profile)
0379         return &ns->base;
0380     return &profile->base;
0381 }
0382 
0383 /**
0384  * __lookupn_profile - lookup the profile matching @hname
0385  * @base: base list to start looking up profile name from  (NOT NULL)
0386  * @hname: hierarchical profile name  (NOT NULL)
0387  * @n: length of @hname
0388  *
0389  * Requires: rcu_read_lock be held
0390  *
0391  * Returns: unrefcounted profile pointer or NULL if not found
0392  *
0393  * Do a relative name lookup, recursing through profile tree.
0394  */
0395 static struct aa_profile *__lookupn_profile(struct aa_policy *base,
0396                         const char *hname, size_t n)
0397 {
0398     struct aa_profile *profile = NULL;
0399     const char *split;
0400 
0401     for (split = strnstr(hname, "//", n); split;
0402          split = strnstr(hname, "//", n)) {
0403         profile = __strn_find_child(&base->profiles, hname,
0404                         split - hname);
0405         if (!profile)
0406             return NULL;
0407 
0408         base = &profile->base;
0409         n -= split + 2 - hname;
0410         hname = split + 2;
0411     }
0412 
0413     if (n)
0414         return __strn_find_child(&base->profiles, hname, n);
0415     return NULL;
0416 }
0417 
0418 static struct aa_profile *__lookup_profile(struct aa_policy *base,
0419                        const char *hname)
0420 {
0421     return __lookupn_profile(base, hname, strlen(hname));
0422 }
0423 
0424 /**
0425  * aa_lookupn_profile - find a profile by its full or partial name
0426  * @ns: the namespace to start from (NOT NULL)
0427  * @hname: name to do lookup on.  Does not contain namespace prefix (NOT NULL)
0428  * @n: size of @hname
0429  *
0430  * Returns: refcounted profile or NULL if not found
0431  */
0432 struct aa_profile *aa_lookupn_profile(struct aa_ns *ns, const char *hname,
0433                       size_t n)
0434 {
0435     struct aa_profile *profile;
0436 
0437     rcu_read_lock();
0438     do {
0439         profile = __lookupn_profile(&ns->base, hname, n);
0440     } while (profile && !aa_get_profile_not0(profile));
0441     rcu_read_unlock();
0442 
0443     /* the unconfined profile is not in the regular profile list */
0444     if (!profile && strncmp(hname, "unconfined", n) == 0)
0445         profile = aa_get_newest_profile(ns->unconfined);
0446 
0447     /* refcount released by caller */
0448     return profile;
0449 }
0450 
0451 struct aa_profile *aa_lookup_profile(struct aa_ns *ns, const char *hname)
0452 {
0453     return aa_lookupn_profile(ns, hname, strlen(hname));
0454 }
0455 
0456 struct aa_profile *aa_fqlookupn_profile(struct aa_label *base,
0457                     const char *fqname, size_t n)
0458 {
0459     struct aa_profile *profile;
0460     struct aa_ns *ns;
0461     const char *name, *ns_name;
0462     size_t ns_len;
0463 
0464     name = aa_splitn_fqname(fqname, n, &ns_name, &ns_len);
0465     if (ns_name) {
0466         ns = aa_lookupn_ns(labels_ns(base), ns_name, ns_len);
0467         if (!ns)
0468             return NULL;
0469     } else
0470         ns = aa_get_ns(labels_ns(base));
0471 
0472     if (name)
0473         profile = aa_lookupn_profile(ns, name, n - (name - fqname));
0474     else if (ns)
0475         /* default profile for ns, currently unconfined */
0476         profile = aa_get_newest_profile(ns->unconfined);
0477     else
0478         profile = NULL;
0479     aa_put_ns(ns);
0480 
0481     return profile;
0482 }
0483 
0484 /**
0485  * aa_new_null_profile - create or find a null-X learning profile
0486  * @parent: profile that caused this profile to be created (NOT NULL)
0487  * @hat: true if the null- learning profile is a hat
0488  * @base: name to base the null profile off of
0489  * @gfp: type of allocation
0490  *
0491  * Find/Create a null- complain mode profile used in learning mode.  The
0492  * name of the profile is unique and follows the format of parent//null-XXX.
0493  * where XXX is based on the @name or if that fails or is not supplied
0494  * a unique number
0495  *
0496  * null profiles are added to the profile list but the list does not
0497  * hold a count on them so that they are automatically released when
0498  * not in use.
0499  *
0500  * Returns: new refcounted profile else NULL on failure
0501  */
0502 struct aa_profile *aa_new_null_profile(struct aa_profile *parent, bool hat,
0503                        const char *base, gfp_t gfp)
0504 {
0505     struct aa_profile *p, *profile;
0506     const char *bname;
0507     char *name = NULL;
0508 
0509     AA_BUG(!parent);
0510 
0511     if (base) {
0512         name = kmalloc(strlen(parent->base.hname) + 8 + strlen(base),
0513                    gfp);
0514         if (name) {
0515             sprintf(name, "%s//null-%s", parent->base.hname, base);
0516             goto name;
0517         }
0518         /* fall through to try shorter uniq */
0519     }
0520 
0521     name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, gfp);
0522     if (!name)
0523         return NULL;
0524     sprintf(name, "%s//null-%x", parent->base.hname,
0525         atomic_inc_return(&parent->ns->uniq_null));
0526 
0527 name:
0528     /* lookup to see if this is a dup creation */
0529     bname = basename(name);
0530     profile = aa_find_child(parent, bname);
0531     if (profile)
0532         goto out;
0533 
0534     profile = aa_alloc_profile(name, NULL, gfp);
0535     if (!profile)
0536         goto fail;
0537 
0538     profile->mode = APPARMOR_COMPLAIN;
0539     profile->label.flags |= FLAG_NULL;
0540     if (hat)
0541         profile->label.flags |= FLAG_HAT;
0542     profile->path_flags = parent->path_flags;
0543 
0544     /* released on free_profile */
0545     rcu_assign_pointer(profile->parent, aa_get_profile(parent));
0546     profile->ns = aa_get_ns(parent->ns);
0547     profile->file.dfa = aa_get_dfa(nulldfa);
0548     profile->policy.dfa = aa_get_dfa(nulldfa);
0549 
0550     mutex_lock_nested(&profile->ns->lock, profile->ns->level);
0551     p = __find_child(&parent->base.profiles, bname);
0552     if (p) {
0553         aa_free_profile(profile);
0554         profile = aa_get_profile(p);
0555     } else {
0556         __add_profile(&parent->base.profiles, profile);
0557     }
0558     mutex_unlock(&profile->ns->lock);
0559 
0560     /* refcount released by caller */
0561 out:
0562     kfree(name);
0563 
0564     return profile;
0565 
0566 fail:
0567     kfree(name);
0568     aa_free_profile(profile);
0569     return NULL;
0570 }
0571 
0572 /**
0573  * replacement_allowed - test to see if replacement is allowed
0574  * @profile: profile to test if it can be replaced  (MAYBE NULL)
0575  * @noreplace: true if replacement shouldn't be allowed but addition is okay
0576  * @info: Returns - info about why replacement failed (NOT NULL)
0577  *
0578  * Returns: %0 if replacement allowed else error code
0579  */
0580 static int replacement_allowed(struct aa_profile *profile, int noreplace,
0581                    const char **info)
0582 {
0583     if (profile) {
0584         if (profile->label.flags & FLAG_IMMUTIBLE) {
0585             *info = "cannot replace immutable profile";
0586             return -EPERM;
0587         } else if (noreplace) {
0588             *info = "profile already exists";
0589             return -EEXIST;
0590         }
0591     }
0592     return 0;
0593 }
0594 
0595 /* audit callback for net specific fields */
0596 static void audit_cb(struct audit_buffer *ab, void *va)
0597 {
0598     struct common_audit_data *sa = va;
0599 
0600     if (aad(sa)->iface.ns) {
0601         audit_log_format(ab, " ns=");
0602         audit_log_untrustedstring(ab, aad(sa)->iface.ns);
0603     }
0604 }
0605 
0606 /**
0607  * audit_policy - Do auditing of policy changes
0608  * @label: label to check if it can manage policy
0609  * @op: policy operation being performed
0610  * @ns_name: name of namespace being manipulated
0611  * @name: name of profile being manipulated (NOT NULL)
0612  * @info: any extra information to be audited (MAYBE NULL)
0613  * @error: error code
0614  *
0615  * Returns: the error to be returned after audit is done
0616  */
0617 static int audit_policy(struct aa_label *label, const char *op,
0618             const char *ns_name, const char *name,
0619             const char *info, int error)
0620 {
0621     DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, op);
0622 
0623     aad(&sa)->iface.ns = ns_name;
0624     aad(&sa)->name = name;
0625     aad(&sa)->info = info;
0626     aad(&sa)->error = error;
0627     aad(&sa)->label = label;
0628 
0629     aa_audit_msg(AUDIT_APPARMOR_STATUS, &sa, audit_cb);
0630 
0631     return error;
0632 }
0633 
0634 /* don't call out to other LSMs in the stack for apparmor policy admin
0635  * permissions
0636  */
0637 static int policy_ns_capable(struct aa_label *label,
0638                  struct user_namespace *userns, int cap)
0639 {
0640     int err;
0641 
0642     /* check for MAC_ADMIN cap in cred */
0643     err = cap_capable(current_cred(), userns, cap, CAP_OPT_NONE);
0644     if (!err)
0645         err = aa_capable(label, cap, CAP_OPT_NONE);
0646 
0647     return err;
0648 }
0649 
0650 /**
0651  * aa_policy_view_capable - check if viewing policy in at @ns is allowed
0652  * label: label that is trying to view policy in ns
0653  * ns: namespace being viewed by @label (may be NULL if @label's ns)
0654  * Returns: true if viewing policy is allowed
0655  *
0656  * If @ns is NULL then the namespace being viewed is assumed to be the
0657  * tasks current namespace.
0658  */
0659 bool aa_policy_view_capable(struct aa_label *label, struct aa_ns *ns)
0660 {
0661     struct user_namespace *user_ns = current_user_ns();
0662     struct aa_ns *view_ns = labels_view(label);
0663     bool root_in_user_ns = uid_eq(current_euid(), make_kuid(user_ns, 0)) ||
0664                    in_egroup_p(make_kgid(user_ns, 0));
0665     bool response = false;
0666     if (!ns)
0667         ns = view_ns;
0668 
0669     if (root_in_user_ns && aa_ns_visible(view_ns, ns, true) &&
0670         (user_ns == &init_user_ns ||
0671          (unprivileged_userns_apparmor_policy != 0 &&
0672           user_ns->level == view_ns->level)))
0673         response = true;
0674 
0675     return response;
0676 }
0677 
0678 bool aa_policy_admin_capable(struct aa_label *label, struct aa_ns *ns)
0679 {
0680     struct user_namespace *user_ns = current_user_ns();
0681     bool capable = policy_ns_capable(label, user_ns, CAP_MAC_ADMIN) == 0;
0682 
0683     AA_DEBUG("cap_mac_admin? %d\n", capable);
0684     AA_DEBUG("policy locked? %d\n", aa_g_lock_policy);
0685 
0686     return aa_policy_view_capable(label, ns) && capable &&
0687         !aa_g_lock_policy;
0688 }
0689 
0690 bool aa_current_policy_view_capable(struct aa_ns *ns)
0691 {
0692     struct aa_label *label;
0693     bool res;
0694 
0695     label = __begin_current_label_crit_section();
0696     res = aa_policy_view_capable(label, ns);
0697     __end_current_label_crit_section(label);
0698 
0699     return res;
0700 }
0701 
0702 bool aa_current_policy_admin_capable(struct aa_ns *ns)
0703 {
0704     struct aa_label *label;
0705     bool res;
0706 
0707     label = __begin_current_label_crit_section();
0708     res = aa_policy_admin_capable(label, ns);
0709     __end_current_label_crit_section(label);
0710 
0711     return res;
0712 }
0713 
0714 /**
0715  * aa_may_manage_policy - can the current task manage policy
0716  * @label: label to check if it can manage policy
0717  * @op: the policy manipulation operation being done
0718  *
0719  * Returns: 0 if the task is allowed to manipulate policy else error
0720  */
0721 int aa_may_manage_policy(struct aa_label *label, struct aa_ns *ns, u32 mask)
0722 {
0723     const char *op;
0724 
0725     if (mask & AA_MAY_REMOVE_POLICY)
0726         op = OP_PROF_RM;
0727     else if (mask & AA_MAY_REPLACE_POLICY)
0728         op = OP_PROF_REPL;
0729     else
0730         op = OP_PROF_LOAD;
0731 
0732     /* check if loading policy is locked out */
0733     if (aa_g_lock_policy)
0734         return audit_policy(label, op, NULL, NULL, "policy_locked",
0735                     -EACCES);
0736 
0737     if (!aa_policy_admin_capable(label, ns))
0738         return audit_policy(label, op, NULL, NULL, "not policy admin",
0739                     -EACCES);
0740 
0741     /* TODO: add fine grained mediation of policy loads */
0742     return 0;
0743 }
0744 
0745 static struct aa_profile *__list_lookup_parent(struct list_head *lh,
0746                            struct aa_profile *profile)
0747 {
0748     const char *base = basename(profile->base.hname);
0749     long len = base - profile->base.hname;
0750     struct aa_load_ent *ent;
0751 
0752     /* parent won't have trailing // so remove from len */
0753     if (len <= 2)
0754         return NULL;
0755     len -= 2;
0756 
0757     list_for_each_entry(ent, lh, list) {
0758         if (ent->new == profile)
0759             continue;
0760         if (strncmp(ent->new->base.hname, profile->base.hname, len) ==
0761             0 && ent->new->base.hname[len] == 0)
0762             return ent->new;
0763     }
0764 
0765     return NULL;
0766 }
0767 
0768 /**
0769  * __replace_profile - replace @old with @new on a list
0770  * @old: profile to be replaced  (NOT NULL)
0771  * @new: profile to replace @old with  (NOT NULL)
0772  * @share_proxy: transfer @old->proxy to @new
0773  *
0774  * Will duplicate and refcount elements that @new inherits from @old
0775  * and will inherit @old children.
0776  *
0777  * refcount @new for list, put @old list refcount
0778  *
0779  * Requires: namespace list lock be held, or list not be shared
0780  */
0781 static void __replace_profile(struct aa_profile *old, struct aa_profile *new)
0782 {
0783     struct aa_profile *child, *tmp;
0784 
0785     if (!list_empty(&old->base.profiles)) {
0786         LIST_HEAD(lh);
0787         list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu);
0788 
0789         list_for_each_entry_safe(child, tmp, &lh, base.list) {
0790             struct aa_profile *p;
0791 
0792             list_del_init(&child->base.list);
0793             p = __find_child(&new->base.profiles, child->base.name);
0794             if (p) {
0795                 /* @p replaces @child  */
0796                 __replace_profile(child, p);
0797                 continue;
0798             }
0799 
0800             /* inherit @child and its children */
0801             /* TODO: update hname of inherited children */
0802             /* list refcount transferred to @new */
0803             p = aa_deref_parent(child);
0804             rcu_assign_pointer(child->parent, aa_get_profile(new));
0805             list_add_rcu(&child->base.list, &new->base.profiles);
0806             aa_put_profile(p);
0807         }
0808     }
0809 
0810     if (!rcu_access_pointer(new->parent)) {
0811         struct aa_profile *parent = aa_deref_parent(old);
0812         rcu_assign_pointer(new->parent, aa_get_profile(parent));
0813     }
0814     aa_label_replace(&old->label, &new->label);
0815     /* migrate dents must come after label replacement b/c update */
0816     __aafs_profile_migrate_dents(old, new);
0817 
0818     if (list_empty(&new->base.list)) {
0819         /* new is not on a list already */
0820         list_replace_rcu(&old->base.list, &new->base.list);
0821         aa_get_profile(new);
0822         aa_put_profile(old);
0823     } else
0824         __list_remove_profile(old);
0825 }
0826 
0827 /**
0828  * __lookup_replace - lookup replacement information for a profile
0829  * @ns - namespace the lookup occurs in
0830  * @hname - name of profile to lookup
0831  * @noreplace - true if not replacing an existing profile
0832  * @p - Returns: profile to be replaced
0833  * @info - Returns: info string on why lookup failed
0834  *
0835  * Returns: profile to replace (no ref) on success else ptr error
0836  */
0837 static int __lookup_replace(struct aa_ns *ns, const char *hname,
0838                 bool noreplace, struct aa_profile **p,
0839                 const char **info)
0840 {
0841     *p = aa_get_profile(__lookup_profile(&ns->base, hname));
0842     if (*p) {
0843         int error = replacement_allowed(*p, noreplace, info);
0844         if (error) {
0845             *info = "profile can not be replaced";
0846             return error;
0847         }
0848     }
0849 
0850     return 0;
0851 }
0852 
0853 static void share_name(struct aa_profile *old, struct aa_profile *new)
0854 {
0855     aa_put_str(new->base.hname);
0856     aa_get_str(old->base.hname);
0857     new->base.hname = old->base.hname;
0858     new->base.name = old->base.name;
0859     new->label.hname = old->label.hname;
0860 }
0861 
0862 /* Update to newest version of parent after previous replacements
0863  * Returns: unrefcount newest version of parent
0864  */
0865 static struct aa_profile *update_to_newest_parent(struct aa_profile *new)
0866 {
0867     struct aa_profile *parent, *newest;
0868 
0869     parent = rcu_dereference_protected(new->parent,
0870                        mutex_is_locked(&new->ns->lock));
0871     newest = aa_get_newest_profile(parent);
0872 
0873     /* parent replaced in this atomic set? */
0874     if (newest != parent) {
0875         aa_put_profile(parent);
0876         rcu_assign_pointer(new->parent, newest);
0877     } else
0878         aa_put_profile(newest);
0879 
0880     return newest;
0881 }
0882 
0883 /**
0884  * aa_replace_profiles - replace profile(s) on the profile list
0885  * @policy_ns: namespace load is occurring on
0886  * @label: label that is attempting to load/replace policy
0887  * @mask: permission mask
0888  * @udata: serialized data stream  (NOT NULL)
0889  *
0890  * unpack and replace a profile on the profile list and uses of that profile
0891  * by any task creds via invalidating the old version of the profile, which
0892  * tasks will notice to update their own cred.  If the profile does not exist
0893  * on the profile list it is added.
0894  *
0895  * Returns: size of data consumed else error code on failure.
0896  */
0897 ssize_t aa_replace_profiles(struct aa_ns *policy_ns, struct aa_label *label,
0898                 u32 mask, struct aa_loaddata *udata)
0899 {
0900     const char *ns_name = NULL, *info = NULL;
0901     struct aa_ns *ns = NULL;
0902     struct aa_load_ent *ent, *tmp;
0903     struct aa_loaddata *rawdata_ent;
0904     const char *op;
0905     ssize_t count, error;
0906     LIST_HEAD(lh);
0907 
0908     op = mask & AA_MAY_REPLACE_POLICY ? OP_PROF_REPL : OP_PROF_LOAD;
0909     aa_get_loaddata(udata);
0910     /* released below */
0911     error = aa_unpack(udata, &lh, &ns_name);
0912     if (error)
0913         goto out;
0914 
0915     /* ensure that profiles are all for the same ns
0916      * TODO: update locking to remove this constaint. All profiles in
0917      *       the load set must succeed as a set or the load will
0918      *       fail. Sort ent list and take ns locks in hierarchy order
0919      */
0920     count = 0;
0921     list_for_each_entry(ent, &lh, list) {
0922         if (ns_name) {
0923             if (ent->ns_name &&
0924                 strcmp(ent->ns_name, ns_name) != 0) {
0925                 info = "policy load has mixed namespaces";
0926                 error = -EACCES;
0927                 goto fail;
0928             }
0929         } else if (ent->ns_name) {
0930             if (count) {
0931                 info = "policy load has mixed namespaces";
0932                 error = -EACCES;
0933                 goto fail;
0934             }
0935             ns_name = ent->ns_name;
0936         } else
0937             count++;
0938     }
0939     if (ns_name) {
0940         ns = aa_prepare_ns(policy_ns ? policy_ns : labels_ns(label),
0941                    ns_name);
0942         if (IS_ERR(ns)) {
0943             op = OP_PROF_LOAD;
0944             info = "failed to prepare namespace";
0945             error = PTR_ERR(ns);
0946             ns = NULL;
0947             ent = NULL;
0948             goto fail;
0949         }
0950     } else
0951         ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(label));
0952 
0953     mutex_lock_nested(&ns->lock, ns->level);
0954     /* check for duplicate rawdata blobs: space and file dedup */
0955     if (!list_empty(&ns->rawdata_list)) {
0956         list_for_each_entry(rawdata_ent, &ns->rawdata_list, list) {
0957             if (aa_rawdata_eq(rawdata_ent, udata)) {
0958                 struct aa_loaddata *tmp;
0959 
0960                 tmp = __aa_get_loaddata(rawdata_ent);
0961                 /* check we didn't fail the race */
0962                 if (tmp) {
0963                     aa_put_loaddata(udata);
0964                     udata = tmp;
0965                     break;
0966                 }
0967             }
0968         }
0969     }
0970     /* setup parent and ns info */
0971     list_for_each_entry(ent, &lh, list) {
0972         struct aa_policy *policy;
0973 
0974         if (aa_g_export_binary)
0975             ent->new->rawdata = aa_get_loaddata(udata);
0976         error = __lookup_replace(ns, ent->new->base.hname,
0977                      !(mask & AA_MAY_REPLACE_POLICY),
0978                      &ent->old, &info);
0979         if (error)
0980             goto fail_lock;
0981 
0982         if (ent->new->rename) {
0983             error = __lookup_replace(ns, ent->new->rename,
0984                         !(mask & AA_MAY_REPLACE_POLICY),
0985                         &ent->rename, &info);
0986             if (error)
0987                 goto fail_lock;
0988         }
0989 
0990         /* released when @new is freed */
0991         ent->new->ns = aa_get_ns(ns);
0992 
0993         if (ent->old || ent->rename)
0994             continue;
0995 
0996         /* no ref on policy only use inside lock */
0997         policy = __lookup_parent(ns, ent->new->base.hname);
0998         if (!policy) {
0999             struct aa_profile *p;
1000             p = __list_lookup_parent(&lh, ent->new);
1001             if (!p) {
1002                 error = -ENOENT;
1003                 info = "parent does not exist";
1004                 goto fail_lock;
1005             }
1006             rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1007         } else if (policy != &ns->base) {
1008             /* released on profile replacement or free_profile */
1009             struct aa_profile *p = (struct aa_profile *) policy;
1010             rcu_assign_pointer(ent->new->parent, aa_get_profile(p));
1011         }
1012     }
1013 
1014     /* create new fs entries for introspection if needed */
1015     if (!udata->dents[AAFS_LOADDATA_DIR] && aa_g_export_binary) {
1016         error = __aa_fs_create_rawdata(ns, udata);
1017         if (error) {
1018             info = "failed to create raw_data dir and files";
1019             ent = NULL;
1020             goto fail_lock;
1021         }
1022     }
1023     list_for_each_entry(ent, &lh, list) {
1024         if (!ent->old) {
1025             struct dentry *parent;
1026             if (rcu_access_pointer(ent->new->parent)) {
1027                 struct aa_profile *p;
1028                 p = aa_deref_parent(ent->new);
1029                 parent = prof_child_dir(p);
1030             } else
1031                 parent = ns_subprofs_dir(ent->new->ns);
1032             error = __aafs_profile_mkdir(ent->new, parent);
1033         }
1034 
1035         if (error) {
1036             info = "failed to create";
1037             goto fail_lock;
1038         }
1039     }
1040 
1041     /* Done with checks that may fail - do actual replacement */
1042     __aa_bump_ns_revision(ns);
1043     if (aa_g_export_binary)
1044         __aa_loaddata_update(udata, ns->revision);
1045     list_for_each_entry_safe(ent, tmp, &lh, list) {
1046         list_del_init(&ent->list);
1047         op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL;
1048 
1049         if (ent->old && ent->old->rawdata == ent->new->rawdata &&
1050             ent->new->rawdata) {
1051             /* dedup actual profile replacement */
1052             audit_policy(label, op, ns_name, ent->new->base.hname,
1053                      "same as current profile, skipping",
1054                      error);
1055             /* break refcount cycle with proxy. */
1056             aa_put_proxy(ent->new->label.proxy);
1057             ent->new->label.proxy = NULL;
1058             goto skip;
1059         }
1060 
1061         /*
1062          * TODO: finer dedup based on profile range in data. Load set
1063          * can differ but profile may remain unchanged
1064          */
1065         audit_policy(label, op, ns_name, ent->new->base.hname, NULL,
1066                  error);
1067 
1068         if (ent->old) {
1069             share_name(ent->old, ent->new);
1070             __replace_profile(ent->old, ent->new);
1071         } else {
1072             struct list_head *lh;
1073 
1074             if (rcu_access_pointer(ent->new->parent)) {
1075                 struct aa_profile *parent;
1076 
1077                 parent = update_to_newest_parent(ent->new);
1078                 lh = &parent->base.profiles;
1079             } else
1080                 lh = &ns->base.profiles;
1081             __add_profile(lh, ent->new);
1082         }
1083     skip:
1084         aa_load_ent_free(ent);
1085     }
1086     __aa_labelset_update_subtree(ns);
1087     mutex_unlock(&ns->lock);
1088 
1089 out:
1090     aa_put_ns(ns);
1091     aa_put_loaddata(udata);
1092     kfree(ns_name);
1093 
1094     if (error)
1095         return error;
1096     return udata->size;
1097 
1098 fail_lock:
1099     mutex_unlock(&ns->lock);
1100 
1101     /* audit cause of failure */
1102     op = (ent && !ent->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1103 fail:
1104       audit_policy(label, op, ns_name, ent ? ent->new->base.hname : NULL,
1105                info, error);
1106     /* audit status that rest of profiles in the atomic set failed too */
1107     info = "valid profile in failed atomic policy load";
1108     list_for_each_entry(tmp, &lh, list) {
1109         if (tmp == ent) {
1110             info = "unchecked profile in failed atomic policy load";
1111             /* skip entry that caused failure */
1112             continue;
1113         }
1114         op = (!tmp->old) ? OP_PROF_LOAD : OP_PROF_REPL;
1115         audit_policy(label, op, ns_name, tmp->new->base.hname, info,
1116                  error);
1117     }
1118     list_for_each_entry_safe(ent, tmp, &lh, list) {
1119         list_del_init(&ent->list);
1120         aa_load_ent_free(ent);
1121     }
1122 
1123     goto out;
1124 }
1125 
1126 /**
1127  * aa_remove_profiles - remove profile(s) from the system
1128  * @policy_ns: namespace the remove is being done from
1129  * @subj: label attempting to remove policy
1130  * @fqname: name of the profile or namespace to remove  (NOT NULL)
1131  * @size: size of the name
1132  *
1133  * Remove a profile or sub namespace from the current namespace, so that
1134  * they can not be found anymore and mark them as replaced by unconfined
1135  *
1136  * NOTE: removing confinement does not restore rlimits to preconfinement values
1137  *
1138  * Returns: size of data consume else error code if fails
1139  */
1140 ssize_t aa_remove_profiles(struct aa_ns *policy_ns, struct aa_label *subj,
1141                char *fqname, size_t size)
1142 {
1143     struct aa_ns *ns = NULL;
1144     struct aa_profile *profile = NULL;
1145     const char *name = fqname, *info = NULL;
1146     const char *ns_name = NULL;
1147     ssize_t error = 0;
1148 
1149     if (*fqname == 0) {
1150         info = "no profile specified";
1151         error = -ENOENT;
1152         goto fail;
1153     }
1154 
1155     if (fqname[0] == ':') {
1156         size_t ns_len;
1157 
1158         name = aa_splitn_fqname(fqname, size, &ns_name, &ns_len);
1159         /* released below */
1160         ns = aa_lookupn_ns(policy_ns ? policy_ns : labels_ns(subj),
1161                    ns_name, ns_len);
1162         if (!ns) {
1163             info = "namespace does not exist";
1164             error = -ENOENT;
1165             goto fail;
1166         }
1167     } else
1168         /* released below */
1169         ns = aa_get_ns(policy_ns ? policy_ns : labels_ns(subj));
1170 
1171     if (!name) {
1172         /* remove namespace - can only happen if fqname[0] == ':' */
1173         mutex_lock_nested(&ns->parent->lock, ns->level);
1174         __aa_bump_ns_revision(ns);
1175         __aa_remove_ns(ns);
1176         mutex_unlock(&ns->parent->lock);
1177     } else {
1178         /* remove profile */
1179         mutex_lock_nested(&ns->lock, ns->level);
1180         profile = aa_get_profile(__lookup_profile(&ns->base, name));
1181         if (!profile) {
1182             error = -ENOENT;
1183             info = "profile does not exist";
1184             goto fail_ns_lock;
1185         }
1186         name = profile->base.hname;
1187         __aa_bump_ns_revision(ns);
1188         __remove_profile(profile);
1189         __aa_labelset_update_subtree(ns);
1190         mutex_unlock(&ns->lock);
1191     }
1192 
1193     /* don't fail removal if audit fails */
1194     (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1195                 error);
1196     aa_put_ns(ns);
1197     aa_put_profile(profile);
1198     return size;
1199 
1200 fail_ns_lock:
1201     mutex_unlock(&ns->lock);
1202     aa_put_ns(ns);
1203 
1204 fail:
1205     (void) audit_policy(subj, OP_PROF_RM, ns_name, name, info,
1206                 error);
1207     return error;
1208 }