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