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 */ |
| 102 | void __aa_update_replacedby(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 | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame^] | 106 | tmp = rcu_dereference_protected(orig->replacedby->profile, |
| 107 | mutex_is_locked(&orig->ns->lock)); |
| 108 | rcu_assign_pointer(orig->replacedby->profile, aa_get_profile(new)); |
| 109 | orig->flags |= PFLAG_INVALID; |
| 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 | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 159 | __aa_update_replacedby(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 | |
| 178 | static void free_replacedby(struct aa_replacedby *r) |
| 179 | { |
| 180 | if (r) { |
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 */ |
| 182 | aa_put_profile(rcu_dereference_protected(r->profile, true)); |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 183 | kzfree(r); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | void aa_free_replacedby_kref(struct kref *kref) |
| 189 | { |
| 190 | struct aa_replacedby *r = container_of(kref, struct aa_replacedby, |
| 191 | count); |
| 192 | free_replacedby(r); |
| 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 | |
| 216 | aa_put_namespace(profile->ns); |
| 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 | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 226 | aa_put_replacedby(profile->replacedby); |
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 | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame^] | 240 | aa_free_namespace(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) |
| 258 | * |
| 259 | * Returns: refcount profile or NULL on failure |
| 260 | */ |
| 261 | struct aa_profile *aa_alloc_profile(const char *hname) |
| 262 | { |
| 263 | struct aa_profile *profile; |
| 264 | |
| 265 | /* freed by free_profile - usually through aa_put_profile */ |
| 266 | profile = kzalloc(sizeof(*profile), GFP_KERNEL); |
| 267 | if (!profile) |
| 268 | return NULL; |
| 269 | |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 270 | profile->replacedby = kzalloc(sizeof(struct aa_replacedby), GFP_KERNEL); |
| 271 | if (!profile->replacedby) |
| 272 | goto fail; |
| 273 | kref_init(&profile->replacedby->count); |
| 274 | |
John Johansen | fe6bb31 | 2017-01-16 00:42:14 -0800 | [diff] [blame] | 275 | if (!aa_policy_init(&profile->base, NULL, hname)) |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 276 | goto fail; |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame] | 277 | kref_init(&profile->count); |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 278 | |
| 279 | /* refcount released by caller */ |
| 280 | return profile; |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 281 | |
| 282 | fail: |
| 283 | kzfree(profile->replacedby); |
| 284 | kzfree(profile); |
| 285 | |
| 286 | return NULL; |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /** |
| 290 | * aa_new_null_profile - create a new null-X learning profile |
| 291 | * @parent: profile that caused this profile to be created (NOT NULL) |
| 292 | * @hat: true if the null- learning profile is a hat |
| 293 | * |
| 294 | * Create a null- complain mode profile used in learning mode. The name of |
| 295 | * the profile is unique and follows the format of parent//null-<uniq>. |
| 296 | * |
| 297 | * null profiles are added to the profile list but the list does not |
| 298 | * hold a count on them so that they are automatically released when |
| 299 | * not in use. |
| 300 | * |
| 301 | * Returns: new refcounted profile else NULL on failure |
| 302 | */ |
| 303 | struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat) |
| 304 | { |
| 305 | struct aa_profile *profile = NULL; |
| 306 | char *name; |
| 307 | int uniq = atomic_inc_return(&parent->ns->uniq_null); |
| 308 | |
| 309 | /* freed below */ |
| 310 | name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, GFP_KERNEL); |
| 311 | if (!name) |
| 312 | goto fail; |
| 313 | sprintf(name, "%s//null-%x", parent->base.hname, uniq); |
| 314 | |
| 315 | profile = aa_alloc_profile(name); |
| 316 | kfree(name); |
| 317 | if (!profile) |
| 318 | goto fail; |
| 319 | |
| 320 | profile->mode = APPARMOR_COMPLAIN; |
| 321 | profile->flags = PFLAG_NULL; |
| 322 | if (hat) |
| 323 | profile->flags |= PFLAG_HAT; |
| 324 | |
| 325 | /* released on free_profile */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 326 | rcu_assign_pointer(profile->parent, aa_get_profile(parent)); |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 327 | profile->ns = aa_get_namespace(parent->ns); |
| 328 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 329 | mutex_lock(&profile->ns->lock); |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 330 | __list_add_profile(&parent->base.profiles, profile); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 331 | mutex_unlock(&profile->ns->lock); |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 332 | |
| 333 | /* refcount released by caller */ |
| 334 | return profile; |
| 335 | |
| 336 | fail: |
| 337 | return NULL; |
| 338 | } |
| 339 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 340 | /* TODO: profile accounting - setup in remove */ |
| 341 | |
| 342 | /** |
| 343 | * __find_child - find a profile on @head list with a name matching @name |
| 344 | * @head: list to search (NOT NULL) |
| 345 | * @name: name of profile (NOT NULL) |
| 346 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 347 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 348 | * |
| 349 | * Returns: unrefcounted profile ptr, or NULL if not found |
| 350 | */ |
| 351 | static struct aa_profile *__find_child(struct list_head *head, const char *name) |
| 352 | { |
| 353 | return (struct aa_profile *)__policy_find(head, name); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * __strn_find_child - find a profile on @head list using substring of @name |
| 358 | * @head: list to search (NOT NULL) |
| 359 | * @name: name of profile (NOT NULL) |
| 360 | * @len: length of @name substring to match |
| 361 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 362 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 363 | * |
| 364 | * Returns: unrefcounted profile ptr, or NULL if not found |
| 365 | */ |
| 366 | static struct aa_profile *__strn_find_child(struct list_head *head, |
| 367 | const char *name, int len) |
| 368 | { |
| 369 | return (struct aa_profile *)__policy_strn_find(head, name, len); |
| 370 | } |
| 371 | |
| 372 | /** |
| 373 | * aa_find_child - find a profile by @name in @parent |
| 374 | * @parent: profile to search (NOT NULL) |
| 375 | * @name: profile name to search for (NOT NULL) |
| 376 | * |
| 377 | * Returns: a refcounted profile or NULL if not found |
| 378 | */ |
| 379 | struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name) |
| 380 | { |
| 381 | struct aa_profile *profile; |
| 382 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 383 | rcu_read_lock(); |
John Johansen | de7c4cc | 2015-12-16 18:09:10 -0800 | [diff] [blame] | 384 | do { |
| 385 | profile = __find_child(&parent->base.profiles, name); |
| 386 | } while (profile && !aa_get_profile_not0(profile)); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 387 | rcu_read_unlock(); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 388 | |
| 389 | /* refcount released by caller */ |
| 390 | return profile; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * __lookup_parent - lookup the parent of a profile of name @hname |
| 395 | * @ns: namespace to lookup profile in (NOT NULL) |
| 396 | * @hname: hierarchical profile name to find parent of (NOT NULL) |
| 397 | * |
| 398 | * Lookups up the parent of a fully qualified profile name, the profile |
| 399 | * that matches hname does not need to exist, in general this |
| 400 | * is used to load a new profile. |
| 401 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 402 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 403 | * |
| 404 | * Returns: unrefcounted policy or NULL if not found |
| 405 | */ |
| 406 | static struct aa_policy *__lookup_parent(struct aa_namespace *ns, |
| 407 | const char *hname) |
| 408 | { |
| 409 | struct aa_policy *policy; |
| 410 | struct aa_profile *profile = NULL; |
| 411 | char *split; |
| 412 | |
| 413 | policy = &ns->base; |
| 414 | |
| 415 | for (split = strstr(hname, "//"); split;) { |
| 416 | profile = __strn_find_child(&policy->profiles, hname, |
| 417 | split - hname); |
| 418 | if (!profile) |
| 419 | return NULL; |
| 420 | policy = &profile->base; |
| 421 | hname = split + 2; |
| 422 | split = strstr(hname, "//"); |
| 423 | } |
| 424 | if (!profile) |
| 425 | return &ns->base; |
| 426 | return &profile->base; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * __lookup_profile - lookup the profile matching @hname |
| 431 | * @base: base list to start looking up profile name from (NOT NULL) |
| 432 | * @hname: hierarchical profile name (NOT NULL) |
| 433 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 434 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 435 | * |
| 436 | * Returns: unrefcounted profile pointer or NULL if not found |
| 437 | * |
| 438 | * Do a relative name lookup, recursing through profile tree. |
| 439 | */ |
| 440 | static struct aa_profile *__lookup_profile(struct aa_policy *base, |
| 441 | const char *hname) |
| 442 | { |
| 443 | struct aa_profile *profile = NULL; |
| 444 | char *split; |
| 445 | |
| 446 | for (split = strstr(hname, "//"); split;) { |
| 447 | profile = __strn_find_child(&base->profiles, hname, |
| 448 | split - hname); |
| 449 | if (!profile) |
| 450 | return NULL; |
| 451 | |
| 452 | base = &profile->base; |
| 453 | hname = split + 2; |
| 454 | split = strstr(hname, "//"); |
| 455 | } |
| 456 | |
| 457 | profile = __find_child(&base->profiles, hname); |
| 458 | |
| 459 | return profile; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * aa_lookup_profile - find a profile by its full or partial name |
| 464 | * @ns: the namespace to start from (NOT NULL) |
| 465 | * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL) |
| 466 | * |
| 467 | * Returns: refcounted profile or NULL if not found |
| 468 | */ |
| 469 | struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *hname) |
| 470 | { |
| 471 | struct aa_profile *profile; |
| 472 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 473 | rcu_read_lock(); |
| 474 | do { |
| 475 | profile = __lookup_profile(&ns->base, hname); |
| 476 | } while (profile && !aa_get_profile_not0(profile)); |
| 477 | rcu_read_unlock(); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 478 | |
John Johansen | bf83208 | 2012-05-16 11:00:05 -0700 | [diff] [blame] | 479 | /* the unconfined profile is not in the regular profile list */ |
| 480 | if (!profile && strcmp(hname, "unconfined") == 0) |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame] | 481 | profile = aa_get_newest_profile(ns->unconfined); |
John Johansen | bf83208 | 2012-05-16 11:00:05 -0700 | [diff] [blame] | 482 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 483 | /* refcount released by caller */ |
| 484 | return profile; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * replacement_allowed - test to see if replacement is allowed |
| 489 | * @profile: profile to test if it can be replaced (MAYBE NULL) |
| 490 | * @noreplace: true if replacement shouldn't be allowed but addition is okay |
| 491 | * @info: Returns - info about why replacement failed (NOT NULL) |
| 492 | * |
| 493 | * Returns: %0 if replacement allowed else error code |
| 494 | */ |
| 495 | static int replacement_allowed(struct aa_profile *profile, int noreplace, |
| 496 | const char **info) |
| 497 | { |
| 498 | if (profile) { |
| 499 | if (profile->flags & PFLAG_IMMUTABLE) { |
| 500 | *info = "cannot replace immutible profile"; |
| 501 | return -EPERM; |
| 502 | } else if (noreplace) { |
| 503 | *info = "profile already exists"; |
| 504 | return -EEXIST; |
| 505 | } |
| 506 | } |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /** |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 511 | * aa_audit_policy - Do auditing of policy changes |
| 512 | * @op: policy operation being performed |
| 513 | * @gfp: memory allocation flags |
| 514 | * @name: name of profile being manipulated (NOT NULL) |
| 515 | * @info: any extra information to be audited (MAYBE NULL) |
| 516 | * @error: error code |
| 517 | * |
| 518 | * Returns: the error to be returned after audit is done |
| 519 | */ |
| 520 | static int audit_policy(int op, gfp_t gfp, const char *name, const char *info, |
| 521 | int error) |
| 522 | { |
| 523 | struct common_audit_data sa; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 524 | struct apparmor_audit_data aad = {0,}; |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 525 | sa.type = LSM_AUDIT_DATA_NONE; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 526 | sa.aad = &aad; |
| 527 | aad.op = op; |
| 528 | aad.name = name; |
| 529 | aad.info = info; |
| 530 | aad.error = error; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 531 | |
| 532 | return aa_audit(AUDIT_APPARMOR_STATUS, __aa_current_profile(), gfp, |
| 533 | &sa, NULL); |
| 534 | } |
| 535 | |
John Johansen | 58acf9d | 2016-06-22 18:01:08 -0700 | [diff] [blame] | 536 | bool policy_view_capable(void) |
| 537 | { |
| 538 | struct user_namespace *user_ns = current_user_ns(); |
| 539 | bool response = false; |
| 540 | |
| 541 | if (ns_capable(user_ns, CAP_MAC_ADMIN)) |
| 542 | response = true; |
| 543 | |
| 544 | return response; |
| 545 | } |
| 546 | |
| 547 | bool policy_admin_capable(void) |
| 548 | { |
| 549 | return policy_view_capable() && !aa_g_lock_policy; |
| 550 | } |
| 551 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 552 | /** |
| 553 | * aa_may_manage_policy - can the current task manage policy |
| 554 | * @op: the policy manipulation operation being done |
| 555 | * |
| 556 | * Returns: true if the task is allowed to manipulate policy |
| 557 | */ |
| 558 | bool aa_may_manage_policy(int op) |
| 559 | { |
| 560 | /* check if loading policy is locked out */ |
| 561 | if (aa_g_lock_policy) { |
| 562 | audit_policy(op, GFP_KERNEL, NULL, "policy_locked", -EACCES); |
| 563 | return 0; |
| 564 | } |
| 565 | |
John Johansen | 58acf9d | 2016-06-22 18:01:08 -0700 | [diff] [blame] | 566 | if (!policy_admin_capable()) { |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 567 | audit_policy(op, GFP_KERNEL, NULL, "not policy admin", -EACCES); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | return 1; |
| 572 | } |
| 573 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 574 | static struct aa_profile *__list_lookup_parent(struct list_head *lh, |
| 575 | struct aa_profile *profile) |
| 576 | { |
| 577 | const char *base = hname_tail(profile->base.hname); |
| 578 | long len = base - profile->base.hname; |
| 579 | struct aa_load_ent *ent; |
| 580 | |
| 581 | /* parent won't have trailing // so remove from len */ |
| 582 | if (len <= 2) |
| 583 | return NULL; |
| 584 | len -= 2; |
| 585 | |
| 586 | list_for_each_entry(ent, lh, list) { |
| 587 | if (ent->new == profile) |
| 588 | continue; |
| 589 | if (strncmp(ent->new->base.hname, profile->base.hname, len) == |
| 590 | 0 && ent->new->base.hname[len] == 0) |
| 591 | return ent->new; |
| 592 | } |
| 593 | |
| 594 | return NULL; |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * __replace_profile - replace @old with @new on a list |
| 599 | * @old: profile to be replaced (NOT NULL) |
| 600 | * @new: profile to replace @old with (NOT NULL) |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 601 | * @share_replacedby: transfer @old->replacedby to @new |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 602 | * |
| 603 | * Will duplicate and refcount elements that @new inherits from @old |
| 604 | * and will inherit @old children. |
| 605 | * |
| 606 | * refcount @new for list, put @old list refcount |
| 607 | * |
| 608 | * Requires: namespace list lock be held, or list not be shared |
| 609 | */ |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 610 | static void __replace_profile(struct aa_profile *old, struct aa_profile *new, |
| 611 | bool share_replacedby) |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 612 | { |
| 613 | struct aa_profile *child, *tmp; |
| 614 | |
| 615 | if (!list_empty(&old->base.profiles)) { |
| 616 | LIST_HEAD(lh); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 617 | list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 618 | |
| 619 | list_for_each_entry_safe(child, tmp, &lh, base.list) { |
| 620 | struct aa_profile *p; |
| 621 | |
| 622 | list_del_init(&child->base.list); |
| 623 | p = __find_child(&new->base.profiles, child->base.name); |
| 624 | if (p) { |
| 625 | /* @p replaces @child */ |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 626 | __replace_profile(child, p, share_replacedby); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 627 | continue; |
| 628 | } |
| 629 | |
| 630 | /* inherit @child and its children */ |
| 631 | /* TODO: update hname of inherited children */ |
| 632 | /* list refcount transferred to @new */ |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 633 | p = aa_deref_parent(child); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 634 | rcu_assign_pointer(child->parent, aa_get_profile(new)); |
| 635 | list_add_rcu(&child->base.list, &new->base.profiles); |
| 636 | aa_put_profile(p); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 640 | if (!rcu_access_pointer(new->parent)) { |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 641 | struct aa_profile *parent = aa_deref_parent(old); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 642 | rcu_assign_pointer(new->parent, aa_get_profile(parent)); |
| 643 | } |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 644 | __aa_update_replacedby(old, new); |
| 645 | if (share_replacedby) { |
| 646 | aa_put_replacedby(new->replacedby); |
| 647 | new->replacedby = aa_get_replacedby(old->replacedby); |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 648 | } else if (!rcu_access_pointer(new->replacedby->profile)) |
| 649 | /* aafs interface uses replacedby */ |
| 650 | rcu_assign_pointer(new->replacedby->profile, |
| 651 | aa_get_profile(new)); |
| 652 | __aa_fs_profile_migrate_dents(old, new); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 653 | |
| 654 | if (list_empty(&new->base.list)) { |
| 655 | /* new is not on a list already */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 656 | list_replace_rcu(&old->base.list, &new->base.list); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 657 | aa_get_profile(new); |
| 658 | aa_put_profile(old); |
| 659 | } else |
| 660 | __list_remove_profile(old); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * __lookup_replace - lookup replacement information for a profile |
| 665 | * @ns - namespace the lookup occurs in |
| 666 | * @hname - name of profile to lookup |
| 667 | * @noreplace - true if not replacing an existing profile |
| 668 | * @p - Returns: profile to be replaced |
| 669 | * @info - Returns: info string on why lookup failed |
| 670 | * |
| 671 | * Returns: profile to replace (no ref) on success else ptr error |
| 672 | */ |
| 673 | static int __lookup_replace(struct aa_namespace *ns, const char *hname, |
| 674 | bool noreplace, struct aa_profile **p, |
| 675 | const char **info) |
| 676 | { |
| 677 | *p = aa_get_profile(__lookup_profile(&ns->base, hname)); |
| 678 | if (*p) { |
| 679 | int error = replacement_allowed(*p, noreplace, info); |
| 680 | if (error) { |
| 681 | *info = "profile can not be replaced"; |
| 682 | return error; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 689 | /** |
| 690 | * aa_replace_profiles - replace profile(s) on the profile list |
| 691 | * @udata: serialized data stream (NOT NULL) |
| 692 | * @size: size of the serialized data stream |
| 693 | * @noreplace: true if only doing addition, no replacement allowed |
| 694 | * |
| 695 | * unpack and replace a profile on the profile list and uses of that profile |
| 696 | * by any aa_task_cxt. If the profile does not exist on the profile list |
| 697 | * it is added. |
| 698 | * |
| 699 | * Returns: size of data consumed else error code on failure. |
| 700 | */ |
| 701 | ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace) |
| 702 | { |
John Johansen | bf15cf0 | 2016-04-16 14:16:50 -0700 | [diff] [blame] | 703 | const char *ns_name, *info = NULL; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 704 | struct aa_namespace *ns = NULL; |
| 705 | struct aa_load_ent *ent, *tmp; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 706 | int op = OP_PROF_REPL; |
| 707 | ssize_t error; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 708 | LIST_HEAD(lh); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 709 | |
| 710 | /* released below */ |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 711 | error = aa_unpack(udata, size, &lh, &ns_name); |
| 712 | if (error) |
| 713 | goto out; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 714 | |
| 715 | /* released below */ |
| 716 | ns = aa_prepare_namespace(ns_name); |
| 717 | if (!ns) { |
John Johansen | bf15cf0 | 2016-04-16 14:16:50 -0700 | [diff] [blame] | 718 | error = audit_policy(op, GFP_KERNEL, ns_name, |
| 719 | "failed to prepare namespace", -ENOMEM); |
| 720 | goto free; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 721 | } |
| 722 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 723 | mutex_lock(&ns->lock); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 724 | /* setup parent and ns info */ |
| 725 | list_for_each_entry(ent, &lh, list) { |
| 726 | struct aa_policy *policy; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 727 | error = __lookup_replace(ns, ent->new->base.hname, noreplace, |
| 728 | &ent->old, &info); |
| 729 | if (error) |
| 730 | goto fail_lock; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 731 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 732 | if (ent->new->rename) { |
| 733 | error = __lookup_replace(ns, ent->new->rename, |
| 734 | noreplace, &ent->rename, |
| 735 | &info); |
| 736 | if (error) |
| 737 | goto fail_lock; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 738 | } |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 739 | |
| 740 | /* released when @new is freed */ |
| 741 | ent->new->ns = aa_get_namespace(ns); |
| 742 | |
| 743 | if (ent->old || ent->rename) |
| 744 | continue; |
| 745 | |
| 746 | /* no ref on policy only use inside lock */ |
| 747 | policy = __lookup_parent(ns, ent->new->base.hname); |
| 748 | if (!policy) { |
| 749 | struct aa_profile *p; |
| 750 | p = __list_lookup_parent(&lh, ent->new); |
| 751 | if (!p) { |
| 752 | error = -ENOENT; |
| 753 | info = "parent does not exist"; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 754 | goto fail_lock; |
| 755 | } |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 756 | rcu_assign_pointer(ent->new->parent, aa_get_profile(p)); |
| 757 | } else if (policy != &ns->base) { |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 758 | /* released on profile replacement or free_profile */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 759 | struct aa_profile *p = (struct aa_profile *) policy; |
| 760 | rcu_assign_pointer(ent->new->parent, aa_get_profile(p)); |
| 761 | } |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 762 | } |
| 763 | |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 764 | /* create new fs entries for introspection if needed */ |
| 765 | list_for_each_entry(ent, &lh, list) { |
| 766 | if (ent->old) { |
| 767 | /* inherit old interface files */ |
| 768 | |
| 769 | /* if (ent->rename) |
| 770 | TODO: support rename */ |
| 771 | /* } else if (ent->rename) { |
| 772 | TODO: support rename */ |
| 773 | } else { |
| 774 | struct dentry *parent; |
| 775 | if (rcu_access_pointer(ent->new->parent)) { |
| 776 | struct aa_profile *p; |
| 777 | p = aa_deref_parent(ent->new); |
| 778 | parent = prof_child_dir(p); |
| 779 | } else |
| 780 | parent = ns_subprofs_dir(ent->new->ns); |
| 781 | error = __aa_fs_profile_mkdir(ent->new, parent); |
| 782 | } |
| 783 | |
| 784 | if (error) { |
| 785 | info = "failed to create "; |
| 786 | goto fail_lock; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* Done with checks that may fail - do actual replacement */ |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 791 | list_for_each_entry_safe(ent, tmp, &lh, list) { |
| 792 | list_del_init(&ent->list); |
| 793 | op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 794 | |
John Johansen | 7ee6da2 | 2016-04-16 14:19:38 -0700 | [diff] [blame] | 795 | audit_policy(op, GFP_ATOMIC, ent->new->base.hname, NULL, error); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 796 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 797 | if (ent->old) { |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 798 | __replace_profile(ent->old, ent->new, 1); |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 799 | if (ent->rename) { |
| 800 | /* aafs interface uses replacedby */ |
| 801 | struct aa_replacedby *r = ent->new->replacedby; |
| 802 | rcu_assign_pointer(r->profile, |
| 803 | aa_get_profile(ent->new)); |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 804 | __replace_profile(ent->rename, ent->new, 0); |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 805 | } |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 806 | } else if (ent->rename) { |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 807 | /* aafs interface uses replacedby */ |
| 808 | rcu_assign_pointer(ent->new->replacedby->profile, |
| 809 | aa_get_profile(ent->new)); |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 810 | __replace_profile(ent->rename, ent->new, 0); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 811 | } else if (ent->new->parent) { |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 812 | struct aa_profile *parent, *newest; |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 813 | parent = aa_deref_parent(ent->new); |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 814 | newest = aa_get_newest_profile(parent); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 815 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 816 | /* parent replaced in this atomic set? */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 817 | if (newest != parent) { |
| 818 | aa_get_profile(newest); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 819 | rcu_assign_pointer(ent->new->parent, newest); |
John Johansen | f351841 | 2016-04-16 13:59:02 -0700 | [diff] [blame] | 820 | aa_put_profile(parent); |
John Johansen | dcda617 | 2016-04-11 16:55:10 -0700 | [diff] [blame] | 821 | } |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 822 | /* aafs interface uses replacedby */ |
| 823 | rcu_assign_pointer(ent->new->replacedby->profile, |
| 824 | aa_get_profile(ent->new)); |
John Johansen | ec34fa2 | 2016-04-11 16:57:19 -0700 | [diff] [blame] | 825 | __list_add_profile(&newest->base.profiles, ent->new); |
John Johansen | dcda617 | 2016-04-11 16:55:10 -0700 | [diff] [blame] | 826 | aa_put_profile(newest); |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 827 | } else { |
| 828 | /* aafs interface uses replacedby */ |
| 829 | rcu_assign_pointer(ent->new->replacedby->profile, |
| 830 | aa_get_profile(ent->new)); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 831 | __list_add_profile(&ns->base.profiles, ent->new); |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 832 | } |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 833 | aa_load_ent_free(ent); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 834 | } |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 835 | mutex_unlock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 836 | |
| 837 | out: |
| 838 | aa_put_namespace(ns); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 839 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 840 | if (error) |
| 841 | return error; |
| 842 | return size; |
| 843 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 844 | fail_lock: |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 845 | mutex_unlock(&ns->lock); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 846 | |
John Johansen | bf15cf0 | 2016-04-16 14:16:50 -0700 | [diff] [blame] | 847 | /* audit cause of failure */ |
| 848 | op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL; |
| 849 | audit_policy(op, GFP_KERNEL, ent->new->base.hname, info, error); |
| 850 | /* audit status that rest of profiles in the atomic set failed too */ |
| 851 | info = "valid profile in failed atomic policy load"; |
| 852 | list_for_each_entry(tmp, &lh, list) { |
| 853 | if (tmp == ent) { |
| 854 | info = "unchecked profile in failed atomic policy load"; |
| 855 | /* skip entry that caused failure */ |
| 856 | continue; |
| 857 | } |
| 858 | op = (!ent->old) ? OP_PROF_LOAD : OP_PROF_REPL; |
| 859 | audit_policy(op, GFP_KERNEL, tmp->new->base.hname, info, error); |
| 860 | } |
| 861 | free: |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 862 | list_for_each_entry_safe(ent, tmp, &lh, list) { |
| 863 | list_del_init(&ent->list); |
| 864 | aa_load_ent_free(ent); |
| 865 | } |
| 866 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 867 | goto out; |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * aa_remove_profiles - remove profile(s) from the system |
| 872 | * @fqname: name of the profile or namespace to remove (NOT NULL) |
| 873 | * @size: size of the name |
| 874 | * |
| 875 | * Remove a profile or sub namespace from the current namespace, so that |
| 876 | * they can not be found anymore and mark them as replaced by unconfined |
| 877 | * |
| 878 | * NOTE: removing confinement does not restore rlimits to preconfinemnet values |
| 879 | * |
| 880 | * Returns: size of data consume else error code if fails |
| 881 | */ |
| 882 | ssize_t aa_remove_profiles(char *fqname, size_t size) |
| 883 | { |
| 884 | struct aa_namespace *root, *ns = NULL; |
| 885 | struct aa_profile *profile = NULL; |
| 886 | const char *name = fqname, *info = NULL; |
| 887 | ssize_t error = 0; |
| 888 | |
| 889 | if (*fqname == 0) { |
| 890 | info = "no profile specified"; |
| 891 | error = -ENOENT; |
| 892 | goto fail; |
| 893 | } |
| 894 | |
| 895 | root = aa_current_profile()->ns; |
| 896 | |
| 897 | if (fqname[0] == ':') { |
| 898 | char *ns_name; |
| 899 | name = aa_split_fqname(fqname, &ns_name); |
John Johansen | 41d1b3e | 2013-02-21 01:14:17 -0800 | [diff] [blame] | 900 | /* released below */ |
| 901 | ns = aa_find_namespace(root, ns_name); |
| 902 | if (!ns) { |
| 903 | info = "namespace does not exist"; |
| 904 | error = -ENOENT; |
| 905 | goto fail; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 906 | } |
| 907 | } else |
| 908 | /* released below */ |
| 909 | ns = aa_get_namespace(root); |
| 910 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 911 | if (!name) { |
| 912 | /* remove namespace - can only happen if fqname[0] == ':' */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 913 | mutex_lock(&ns->parent->lock); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame^] | 914 | __aa_remove_namespace(ns); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 915 | mutex_unlock(&ns->parent->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 916 | } else { |
| 917 | /* remove profile */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 918 | mutex_lock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 919 | profile = aa_get_profile(__lookup_profile(&ns->base, name)); |
| 920 | if (!profile) { |
| 921 | error = -ENOENT; |
| 922 | info = "profile does not exist"; |
| 923 | goto fail_ns_lock; |
| 924 | } |
| 925 | name = profile->base.hname; |
| 926 | __remove_profile(profile); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 927 | mutex_unlock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 928 | } |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 929 | |
| 930 | /* don't fail removal if audit fails */ |
| 931 | (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error); |
| 932 | aa_put_namespace(ns); |
| 933 | aa_put_profile(profile); |
| 934 | return size; |
| 935 | |
| 936 | fail_ns_lock: |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 937 | mutex_unlock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 938 | aa_put_namespace(ns); |
| 939 | |
| 940 | fail: |
| 941 | (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error); |
| 942 | return error; |
| 943 | } |