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