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" |
| 88 | #include "include/policy_unpack.h" |
| 89 | #include "include/resource.h" |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 90 | |
| 91 | |
| 92 | /* root profile namespace */ |
| 93 | struct aa_namespace *root_ns; |
| 94 | |
Jan Engelhardt | 2d4cee7 | 2012-03-14 13:30:36 +0100 | [diff] [blame] | 95 | const char *const profile_mode_names[] = { |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 96 | "enforce", |
| 97 | "complain", |
| 98 | "kill", |
| 99 | }; |
| 100 | |
| 101 | /** |
| 102 | * hname_tail - find the last component of an hname |
| 103 | * @name: hname to find the base profile name component of (NOT NULL) |
| 104 | * |
| 105 | * Returns: the tail (base profile name) name component of an hname |
| 106 | */ |
| 107 | static const char *hname_tail(const char *hname) |
| 108 | { |
| 109 | char *split; |
| 110 | hname = strim((char *)hname); |
| 111 | for (split = strstr(hname, "//"); split; split = strstr(hname, "//")) |
| 112 | hname = split + 2; |
| 113 | |
| 114 | return hname; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * policy_init - initialize a policy structure |
| 119 | * @policy: policy to initialize (NOT NULL) |
| 120 | * @prefix: prefix name if any is required. (MAYBE NULL) |
| 121 | * @name: name of the policy, init will make a copy of it (NOT NULL) |
| 122 | * |
| 123 | * Note: this fn creates a copy of strings passed in |
| 124 | * |
| 125 | * Returns: true if policy init successful |
| 126 | */ |
| 127 | static bool policy_init(struct aa_policy *policy, const char *prefix, |
| 128 | const char *name) |
| 129 | { |
| 130 | /* freed by policy_free */ |
| 131 | if (prefix) { |
| 132 | policy->hname = kmalloc(strlen(prefix) + strlen(name) + 3, |
| 133 | GFP_KERNEL); |
| 134 | if (policy->hname) |
| 135 | sprintf(policy->hname, "%s//%s", prefix, name); |
| 136 | } else |
| 137 | policy->hname = kstrdup(name, GFP_KERNEL); |
| 138 | if (!policy->hname) |
| 139 | return 0; |
| 140 | /* base.name is a substring of fqname */ |
| 141 | policy->name = (char *)hname_tail(policy->hname); |
| 142 | INIT_LIST_HEAD(&policy->list); |
| 143 | INIT_LIST_HEAD(&policy->profiles); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 144 | |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * policy_destroy - free the elements referenced by @policy |
| 150 | * @policy: policy that is to have its elements freed (NOT NULL) |
| 151 | */ |
| 152 | static void policy_destroy(struct aa_policy *policy) |
| 153 | { |
| 154 | /* still contains profiles -- invalid */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 155 | if (on_list_rcu(&policy->profiles)) { |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 156 | AA_ERROR("%s: internal error, " |
| 157 | "policy '%s' still contains profiles\n", |
| 158 | __func__, policy->name); |
| 159 | BUG(); |
| 160 | } |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 161 | if (on_list_rcu(&policy->list)) { |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 162 | AA_ERROR("%s: internal error, policy '%s' still on list\n", |
| 163 | __func__, policy->name); |
| 164 | BUG(); |
| 165 | } |
| 166 | |
| 167 | /* don't free name as its a subset of hname */ |
| 168 | kzfree(policy->hname); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * __policy_find - find a policy by @name on a policy list |
| 173 | * @head: list to search (NOT NULL) |
| 174 | * @name: name to search for (NOT NULL) |
| 175 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 176 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 177 | * |
| 178 | * Returns: unrefcounted policy that match @name or NULL if not found |
| 179 | */ |
| 180 | static struct aa_policy *__policy_find(struct list_head *head, const char *name) |
| 181 | { |
| 182 | struct aa_policy *policy; |
| 183 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 184 | list_for_each_entry_rcu(policy, head, list) { |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 185 | if (!strcmp(policy->name, name)) |
| 186 | return policy; |
| 187 | } |
| 188 | return NULL; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * __policy_strn_find - find a policy that's name matches @len chars of @str |
| 193 | * @head: list to search (NOT NULL) |
| 194 | * @str: string to search for (NOT NULL) |
| 195 | * @len: length of match required |
| 196 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 197 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 198 | * |
| 199 | * Returns: unrefcounted policy that match @str or NULL if not found |
| 200 | * |
| 201 | * if @len == strlen(@strlen) then this is equiv to __policy_find |
| 202 | * other wise it allows searching for policy by a partial match of name |
| 203 | */ |
| 204 | static struct aa_policy *__policy_strn_find(struct list_head *head, |
| 205 | const char *str, int len) |
| 206 | { |
| 207 | struct aa_policy *policy; |
| 208 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 209 | list_for_each_entry_rcu(policy, head, list) { |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 210 | if (aa_strneq(policy->name, str, len)) |
| 211 | return policy; |
| 212 | } |
| 213 | |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Routines for AppArmor namespaces |
| 219 | */ |
| 220 | |
| 221 | static const char *hidden_ns_name = "---"; |
| 222 | /** |
| 223 | * aa_ns_visible - test if @view is visible from @curr |
| 224 | * @curr: namespace to treat as the parent (NOT NULL) |
| 225 | * @view: namespace to test if visible from @curr (NOT NULL) |
| 226 | * |
| 227 | * Returns: true if @view is visible from @curr else false |
| 228 | */ |
| 229 | bool aa_ns_visible(struct aa_namespace *curr, struct aa_namespace *view) |
| 230 | { |
| 231 | if (curr == view) |
| 232 | return true; |
| 233 | |
| 234 | for ( ; view; view = view->parent) { |
| 235 | if (view->parent == curr) |
| 236 | return true; |
| 237 | } |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * aa_na_name - Find the ns name to display for @view from @curr |
| 243 | * @curr - current namespace (NOT NULL) |
| 244 | * @view - namespace attempting to view (NOT NULL) |
| 245 | * |
| 246 | * Returns: name of @view visible from @curr |
| 247 | */ |
| 248 | const char *aa_ns_name(struct aa_namespace *curr, struct aa_namespace *view) |
| 249 | { |
| 250 | /* if view == curr then the namespace name isn't displayed */ |
| 251 | if (curr == view) |
| 252 | return ""; |
| 253 | |
| 254 | if (aa_ns_visible(curr, view)) { |
| 255 | /* at this point if a ns is visible it is in a view ns |
| 256 | * thus the curr ns.hname is a prefix of its name. |
| 257 | * Only output the virtualized portion of the name |
| 258 | * Add + 2 to skip over // separating curr hname prefix |
| 259 | * from the visible tail of the views hname |
| 260 | */ |
| 261 | return view->base.hname + strlen(curr->base.hname) + 2; |
| 262 | } else |
| 263 | return hidden_ns_name; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * alloc_namespace - allocate, initialize and return a new namespace |
| 268 | * @prefix: parent namespace name (MAYBE NULL) |
| 269 | * @name: a preallocated name (NOT NULL) |
| 270 | * |
| 271 | * Returns: refcounted namespace or NULL on failure. |
| 272 | */ |
| 273 | static struct aa_namespace *alloc_namespace(const char *prefix, |
| 274 | const char *name) |
| 275 | { |
| 276 | struct aa_namespace *ns; |
| 277 | |
| 278 | ns = kzalloc(sizeof(*ns), GFP_KERNEL); |
| 279 | AA_DEBUG("%s(%p)\n", __func__, ns); |
| 280 | if (!ns) |
| 281 | return NULL; |
| 282 | if (!policy_init(&ns->base, prefix, name)) |
| 283 | goto fail_ns; |
| 284 | |
| 285 | INIT_LIST_HEAD(&ns->sub_ns); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 286 | mutex_init(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 287 | |
| 288 | /* released by free_namespace */ |
| 289 | ns->unconfined = aa_alloc_profile("unconfined"); |
| 290 | if (!ns->unconfined) |
| 291 | goto fail_unconfined; |
| 292 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 293 | ns->unconfined->flags = PFLAG_UNCONFINED | PFLAG_IX_ON_NAME_ERROR | |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 294 | PFLAG_IMMUTABLE | PFLAG_NS_COUNT; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 295 | |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 296 | /* ns and ns->unconfined share ns->unconfined refcount */ |
| 297 | ns->unconfined->ns = ns; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 298 | |
John Johansen | a498785 | 2013-02-18 16:10:34 -0800 | [diff] [blame] | 299 | atomic_set(&ns->uniq_null, 0); |
| 300 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 301 | return ns; |
| 302 | |
| 303 | fail_unconfined: |
wzt.wzt@gmail.com | 246c3fb1 | 2010-11-10 11:31:55 +0800 | [diff] [blame] | 304 | kzfree(ns->base.hname); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 305 | fail_ns: |
| 306 | kzfree(ns); |
| 307 | return NULL; |
| 308 | } |
| 309 | |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 310 | static void free_profile(struct aa_profile *profile); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 311 | /** |
| 312 | * free_namespace - free a profile namespace |
| 313 | * @ns: the namespace to free (MAYBE NULL) |
| 314 | * |
| 315 | * Requires: All references to the namespace must have been put, if the |
| 316 | * namespace was referenced by a profile confining a task, |
| 317 | */ |
| 318 | static void free_namespace(struct aa_namespace *ns) |
| 319 | { |
| 320 | if (!ns) |
| 321 | return; |
| 322 | |
| 323 | policy_destroy(&ns->base); |
| 324 | aa_put_namespace(ns->parent); |
| 325 | |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 326 | ns->unconfined->ns = NULL; |
| 327 | free_profile(ns->unconfined); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 328 | kzfree(ns); |
| 329 | } |
| 330 | |
| 331 | /** |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 332 | * aa_free_namespace_rcu - free aa_namespace by rcu |
| 333 | * @head: rcu_head callback for freeing of a profile (NOT NULL) |
| 334 | * |
| 335 | * rcu_head is to the unconfined profile associated with the namespace |
| 336 | */ |
| 337 | static void aa_free_namespace_rcu(struct rcu_head *head) |
| 338 | { |
| 339 | struct aa_profile *p = container_of(head, struct aa_profile, base.rcu); |
| 340 | free_namespace(p->ns); |
| 341 | } |
| 342 | |
| 343 | /** |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 344 | * aa_free_namespace_kref - free aa_namespace by kref (see aa_put_namespace) |
| 345 | * @kr: kref callback for freeing of a namespace (NOT NULL) |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 346 | * |
| 347 | * kref is to the unconfined profile associated with the namespace |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 348 | */ |
| 349 | void aa_free_namespace_kref(struct kref *kref) |
| 350 | { |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 351 | struct aa_profile *p = container_of(kref, struct aa_profile, count); |
| 352 | call_rcu(&p->base.rcu, aa_free_namespace_rcu); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | /** |
| 356 | * __aa_find_namespace - find a namespace on a list by @name |
| 357 | * @head: list to search for namespace on (NOT NULL) |
| 358 | * @name: name of namespace to look for (NOT NULL) |
| 359 | * |
| 360 | * Returns: unrefcounted namespace |
| 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 | static struct aa_namespace *__aa_find_namespace(struct list_head *head, |
| 365 | const char *name) |
| 366 | { |
| 367 | return (struct aa_namespace *)__policy_find(head, name); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * aa_find_namespace - look up a profile namespace on the namespace list |
| 372 | * @root: namespace to search in (NOT NULL) |
| 373 | * @name: name of namespace to find (NOT NULL) |
| 374 | * |
| 375 | * Returns: a refcounted namespace on the list, or NULL if no namespace |
| 376 | * called @name exists. |
| 377 | * |
| 378 | * refcount released by caller |
| 379 | */ |
| 380 | struct aa_namespace *aa_find_namespace(struct aa_namespace *root, |
| 381 | const char *name) |
| 382 | { |
| 383 | struct aa_namespace *ns = NULL; |
| 384 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 385 | rcu_read_lock(); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 386 | ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name)); |
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 | return ns; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * aa_prepare_namespace - find an existing or create a new namespace of @name |
| 394 | * @name: the namespace to find or add (MAYBE NULL) |
| 395 | * |
| 396 | * Returns: refcounted namespace or NULL if failed to create one |
| 397 | */ |
| 398 | static struct aa_namespace *aa_prepare_namespace(const char *name) |
| 399 | { |
| 400 | struct aa_namespace *ns, *root; |
| 401 | |
| 402 | root = aa_current_profile()->ns; |
| 403 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 404 | mutex_lock(&root->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 405 | |
| 406 | /* if name isn't specified the profile is loaded to the current ns */ |
| 407 | if (!name) { |
| 408 | /* released by caller */ |
| 409 | ns = aa_get_namespace(root); |
| 410 | goto out; |
| 411 | } |
| 412 | |
| 413 | /* try and find the specified ns and if it doesn't exist create it */ |
| 414 | /* released by caller */ |
| 415 | ns = aa_get_namespace(__aa_find_namespace(&root->sub_ns, name)); |
| 416 | if (!ns) { |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 417 | ns = alloc_namespace(root->base.hname, name); |
| 418 | if (!ns) |
| 419 | goto out; |
| 420 | /* add parent ref */ |
| 421 | ns->parent = aa_get_namespace(root); |
| 422 | list_add_rcu(&ns->base.list, &root->sub_ns); |
| 423 | /* add list ref */ |
| 424 | aa_get_namespace(ns); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 425 | } |
| 426 | out: |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 427 | mutex_unlock(&root->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 428 | |
| 429 | /* return ref */ |
| 430 | return ns; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * __list_add_profile - add a profile to a list |
| 435 | * @list: list to add it to (NOT NULL) |
| 436 | * @profile: the profile to add (NOT NULL) |
| 437 | * |
| 438 | * refcount @profile, should be put by __list_remove_profile |
| 439 | * |
| 440 | * Requires: namespace lock be held, or list not be shared |
| 441 | */ |
| 442 | static void __list_add_profile(struct list_head *list, |
| 443 | struct aa_profile *profile) |
| 444 | { |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 445 | list_add_rcu(&profile->base.list, list); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 446 | /* get list reference */ |
| 447 | aa_get_profile(profile); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * __list_remove_profile - remove a profile from the list it is on |
| 452 | * @profile: the profile to remove (NOT NULL) |
| 453 | * |
| 454 | * remove a profile from the list, warning generally removal should |
| 455 | * be done with __replace_profile as most profile removals are |
| 456 | * replacements to the unconfined profile. |
| 457 | * |
| 458 | * put @profile list refcount |
| 459 | * |
| 460 | * Requires: namespace lock be held, or list not have been live |
| 461 | */ |
| 462 | static void __list_remove_profile(struct aa_profile *profile) |
| 463 | { |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 464 | list_del_rcu(&profile->base.list); |
| 465 | aa_put_profile(profile); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 466 | } |
| 467 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 468 | static void __profile_list_release(struct list_head *head); |
| 469 | |
| 470 | /** |
| 471 | * __remove_profile - remove old profile, and children |
| 472 | * @profile: profile to be replaced (NOT NULL) |
| 473 | * |
| 474 | * Requires: namespace list lock be held, or list not be shared |
| 475 | */ |
| 476 | static void __remove_profile(struct aa_profile *profile) |
| 477 | { |
| 478 | /* release any children lists first */ |
| 479 | __profile_list_release(&profile->base.profiles); |
| 480 | /* released by free_profile */ |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 481 | __aa_update_replacedby(profile, profile->ns->unconfined); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 482 | __list_remove_profile(profile); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * __profile_list_release - remove all profiles on the list and put refs |
| 487 | * @head: list of profiles (NOT NULL) |
| 488 | * |
| 489 | * Requires: namespace lock be held |
| 490 | */ |
| 491 | static void __profile_list_release(struct list_head *head) |
| 492 | { |
| 493 | struct aa_profile *profile, *tmp; |
| 494 | list_for_each_entry_safe(profile, tmp, head, base.list) |
| 495 | __remove_profile(profile); |
| 496 | } |
| 497 | |
| 498 | static void __ns_list_release(struct list_head *head); |
| 499 | |
| 500 | /** |
| 501 | * destroy_namespace - remove everything contained by @ns |
| 502 | * @ns: namespace to have it contents removed (NOT NULL) |
| 503 | */ |
| 504 | static void destroy_namespace(struct aa_namespace *ns) |
| 505 | { |
| 506 | if (!ns) |
| 507 | return; |
| 508 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 509 | mutex_lock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 510 | /* release all profiles in this namespace */ |
| 511 | __profile_list_release(&ns->base.profiles); |
| 512 | |
| 513 | /* release all sub namespaces */ |
| 514 | __ns_list_release(&ns->sub_ns); |
| 515 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 516 | if (ns->parent) |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 517 | __aa_update_replacedby(ns->unconfined, ns->parent->unconfined); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 518 | mutex_unlock(&ns->lock); |
| 519 | } |
| 520 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 521 | /** |
| 522 | * __remove_namespace - remove a namespace and all its children |
| 523 | * @ns: namespace to be removed (NOT NULL) |
| 524 | * |
| 525 | * Requires: ns->parent->lock be held and ns removed from parent. |
| 526 | */ |
| 527 | static void __remove_namespace(struct aa_namespace *ns) |
| 528 | { |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 529 | /* remove ns from namespace list */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 530 | list_del_rcu(&ns->base.list); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 531 | destroy_namespace(ns); |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 532 | aa_put_namespace(ns); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /** |
| 536 | * __ns_list_release - remove all profile namespaces on the list put refs |
| 537 | * @head: list of profile namespaces (NOT NULL) |
| 538 | * |
| 539 | * Requires: namespace lock be held |
| 540 | */ |
| 541 | static void __ns_list_release(struct list_head *head) |
| 542 | { |
| 543 | struct aa_namespace *ns, *tmp; |
| 544 | list_for_each_entry_safe(ns, tmp, head, base.list) |
| 545 | __remove_namespace(ns); |
| 546 | |
| 547 | } |
| 548 | |
| 549 | /** |
| 550 | * aa_alloc_root_ns - allocate the root profile namespace |
| 551 | * |
| 552 | * Returns: %0 on success else error |
| 553 | * |
| 554 | */ |
| 555 | int __init aa_alloc_root_ns(void) |
| 556 | { |
| 557 | /* released by aa_free_root_ns - used as list ref*/ |
| 558 | root_ns = alloc_namespace(NULL, "root"); |
| 559 | if (!root_ns) |
| 560 | return -ENOMEM; |
| 561 | |
| 562 | return 0; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * aa_free_root_ns - free the root profile namespace |
| 567 | */ |
| 568 | void __init aa_free_root_ns(void) |
| 569 | { |
| 570 | struct aa_namespace *ns = root_ns; |
| 571 | root_ns = NULL; |
| 572 | |
| 573 | destroy_namespace(ns); |
| 574 | aa_put_namespace(ns); |
| 575 | } |
| 576 | |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 577 | |
| 578 | static void free_replacedby(struct aa_replacedby *r) |
| 579 | { |
| 580 | if (r) { |
| 581 | aa_put_profile(rcu_dereference(r->profile)); |
| 582 | kzfree(r); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | |
| 587 | void aa_free_replacedby_kref(struct kref *kref) |
| 588 | { |
| 589 | struct aa_replacedby *r = container_of(kref, struct aa_replacedby, |
| 590 | count); |
| 591 | free_replacedby(r); |
| 592 | } |
| 593 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 594 | /** |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 595 | * free_profile - free a profile |
| 596 | * @profile: the profile to free (MAYBE NULL) |
| 597 | * |
| 598 | * Free a profile, its hats and null_profile. All references to the profile, |
| 599 | * its hats and null_profile must have been put. |
| 600 | * |
| 601 | * If the profile was referenced from a task context, free_profile() will |
| 602 | * be called from an rcu callback routine, so we must not sleep here. |
| 603 | */ |
| 604 | static void free_profile(struct aa_profile *profile) |
| 605 | { |
| 606 | AA_DEBUG("%s(%p)\n", __func__, profile); |
| 607 | |
| 608 | if (!profile) |
| 609 | return; |
| 610 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 611 | /* free children profiles */ |
| 612 | policy_destroy(&profile->base); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 613 | aa_put_profile(rcu_access_pointer(profile->parent)); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 614 | |
| 615 | aa_put_namespace(profile->ns); |
| 616 | kzfree(profile->rename); |
| 617 | |
| 618 | aa_free_file_rules(&profile->file); |
| 619 | aa_free_cap_rules(&profile->caps); |
| 620 | aa_free_rlimit_rules(&profile->rlimits); |
| 621 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 622 | aa_put_dfa(profile->xmatch); |
John Johansen | ad5ff3d | 2012-02-16 07:07:53 -0800 | [diff] [blame] | 623 | aa_put_dfa(profile->policy.dfa); |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 624 | aa_put_replacedby(profile->replacedby); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 625 | |
| 626 | kzfree(profile); |
| 627 | } |
| 628 | |
| 629 | /** |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 630 | * aa_free_profile_rcu - free aa_profile by rcu (called by aa_free_profile_kref) |
| 631 | * @head: rcu_head callback for freeing of a profile (NOT NULL) |
| 632 | */ |
| 633 | static void aa_free_profile_rcu(struct rcu_head *head) |
| 634 | { |
| 635 | struct aa_profile *p = container_of(head, struct aa_profile, base.rcu); |
| 636 | free_profile(p); |
| 637 | } |
| 638 | |
| 639 | /** |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 640 | * aa_free_profile_kref - free aa_profile by kref (called by aa_put_profile) |
| 641 | * @kr: kref callback for freeing of a profile (NOT NULL) |
| 642 | */ |
| 643 | void aa_free_profile_kref(struct kref *kref) |
| 644 | { |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 645 | struct aa_profile *p = container_of(kref, struct aa_profile, count); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 646 | call_rcu(&p->base.rcu, aa_free_profile_rcu); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 647 | } |
| 648 | |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 649 | /** |
| 650 | * aa_alloc_profile - allocate, initialize and return a new profile |
| 651 | * @hname: name of the profile (NOT NULL) |
| 652 | * |
| 653 | * Returns: refcount profile or NULL on failure |
| 654 | */ |
| 655 | struct aa_profile *aa_alloc_profile(const char *hname) |
| 656 | { |
| 657 | struct aa_profile *profile; |
| 658 | |
| 659 | /* freed by free_profile - usually through aa_put_profile */ |
| 660 | profile = kzalloc(sizeof(*profile), GFP_KERNEL); |
| 661 | if (!profile) |
| 662 | return NULL; |
| 663 | |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 664 | profile->replacedby = kzalloc(sizeof(struct aa_replacedby), GFP_KERNEL); |
| 665 | if (!profile->replacedby) |
| 666 | goto fail; |
| 667 | kref_init(&profile->replacedby->count); |
| 668 | |
| 669 | if (!policy_init(&profile->base, NULL, hname)) |
| 670 | goto fail; |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 671 | kref_init(&profile->count); |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 672 | |
| 673 | /* refcount released by caller */ |
| 674 | return profile; |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 675 | |
| 676 | fail: |
| 677 | kzfree(profile->replacedby); |
| 678 | kzfree(profile); |
| 679 | |
| 680 | return NULL; |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /** |
| 684 | * aa_new_null_profile - create a new null-X learning profile |
| 685 | * @parent: profile that caused this profile to be created (NOT NULL) |
| 686 | * @hat: true if the null- learning profile is a hat |
| 687 | * |
| 688 | * Create a null- complain mode profile used in learning mode. The name of |
| 689 | * the profile is unique and follows the format of parent//null-<uniq>. |
| 690 | * |
| 691 | * null profiles are added to the profile list but the list does not |
| 692 | * hold a count on them so that they are automatically released when |
| 693 | * not in use. |
| 694 | * |
| 695 | * Returns: new refcounted profile else NULL on failure |
| 696 | */ |
| 697 | struct aa_profile *aa_new_null_profile(struct aa_profile *parent, int hat) |
| 698 | { |
| 699 | struct aa_profile *profile = NULL; |
| 700 | char *name; |
| 701 | int uniq = atomic_inc_return(&parent->ns->uniq_null); |
| 702 | |
| 703 | /* freed below */ |
| 704 | name = kmalloc(strlen(parent->base.hname) + 2 + 7 + 8, GFP_KERNEL); |
| 705 | if (!name) |
| 706 | goto fail; |
| 707 | sprintf(name, "%s//null-%x", parent->base.hname, uniq); |
| 708 | |
| 709 | profile = aa_alloc_profile(name); |
| 710 | kfree(name); |
| 711 | if (!profile) |
| 712 | goto fail; |
| 713 | |
| 714 | profile->mode = APPARMOR_COMPLAIN; |
| 715 | profile->flags = PFLAG_NULL; |
| 716 | if (hat) |
| 717 | profile->flags |= PFLAG_HAT; |
| 718 | |
| 719 | /* released on free_profile */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 720 | rcu_assign_pointer(profile->parent, aa_get_profile(parent)); |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 721 | profile->ns = aa_get_namespace(parent->ns); |
| 722 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 723 | mutex_lock(&profile->ns->lock); |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 724 | __list_add_profile(&parent->base.profiles, profile); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 725 | mutex_unlock(&profile->ns->lock); |
John Johansen | 4da05cc | 2013-02-18 16:11:34 -0800 | [diff] [blame] | 726 | |
| 727 | /* refcount released by caller */ |
| 728 | return profile; |
| 729 | |
| 730 | fail: |
| 731 | return NULL; |
| 732 | } |
| 733 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 734 | /* TODO: profile accounting - setup in remove */ |
| 735 | |
| 736 | /** |
| 737 | * __find_child - find a profile on @head list with a name matching @name |
| 738 | * @head: list to search (NOT NULL) |
| 739 | * @name: name of profile (NOT NULL) |
| 740 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 741 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 742 | * |
| 743 | * Returns: unrefcounted profile ptr, or NULL if not found |
| 744 | */ |
| 745 | static struct aa_profile *__find_child(struct list_head *head, const char *name) |
| 746 | { |
| 747 | return (struct aa_profile *)__policy_find(head, name); |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * __strn_find_child - find a profile on @head list using substring of @name |
| 752 | * @head: list to search (NOT NULL) |
| 753 | * @name: name of profile (NOT NULL) |
| 754 | * @len: length of @name substring to match |
| 755 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 756 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 757 | * |
| 758 | * Returns: unrefcounted profile ptr, or NULL if not found |
| 759 | */ |
| 760 | static struct aa_profile *__strn_find_child(struct list_head *head, |
| 761 | const char *name, int len) |
| 762 | { |
| 763 | return (struct aa_profile *)__policy_strn_find(head, name, len); |
| 764 | } |
| 765 | |
| 766 | /** |
| 767 | * aa_find_child - find a profile by @name in @parent |
| 768 | * @parent: profile to search (NOT NULL) |
| 769 | * @name: profile name to search for (NOT NULL) |
| 770 | * |
| 771 | * Returns: a refcounted profile or NULL if not found |
| 772 | */ |
| 773 | struct aa_profile *aa_find_child(struct aa_profile *parent, const char *name) |
| 774 | { |
| 775 | struct aa_profile *profile; |
| 776 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 777 | rcu_read_lock(); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 778 | profile = aa_get_profile(__find_child(&parent->base.profiles, name)); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 779 | rcu_read_unlock(); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 780 | |
| 781 | /* refcount released by caller */ |
| 782 | return profile; |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * __lookup_parent - lookup the parent of a profile of name @hname |
| 787 | * @ns: namespace to lookup profile in (NOT NULL) |
| 788 | * @hname: hierarchical profile name to find parent of (NOT NULL) |
| 789 | * |
| 790 | * Lookups up the parent of a fully qualified profile name, the profile |
| 791 | * that matches hname does not need to exist, in general this |
| 792 | * is used to load a new profile. |
| 793 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 794 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 795 | * |
| 796 | * Returns: unrefcounted policy or NULL if not found |
| 797 | */ |
| 798 | static struct aa_policy *__lookup_parent(struct aa_namespace *ns, |
| 799 | const char *hname) |
| 800 | { |
| 801 | struct aa_policy *policy; |
| 802 | struct aa_profile *profile = NULL; |
| 803 | char *split; |
| 804 | |
| 805 | policy = &ns->base; |
| 806 | |
| 807 | for (split = strstr(hname, "//"); split;) { |
| 808 | profile = __strn_find_child(&policy->profiles, hname, |
| 809 | split - hname); |
| 810 | if (!profile) |
| 811 | return NULL; |
| 812 | policy = &profile->base; |
| 813 | hname = split + 2; |
| 814 | split = strstr(hname, "//"); |
| 815 | } |
| 816 | if (!profile) |
| 817 | return &ns->base; |
| 818 | return &profile->base; |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * __lookup_profile - lookup the profile matching @hname |
| 823 | * @base: base list to start looking up profile name from (NOT NULL) |
| 824 | * @hname: hierarchical profile name (NOT NULL) |
| 825 | * |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 826 | * Requires: rcu_read_lock be held |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 827 | * |
| 828 | * Returns: unrefcounted profile pointer or NULL if not found |
| 829 | * |
| 830 | * Do a relative name lookup, recursing through profile tree. |
| 831 | */ |
| 832 | static struct aa_profile *__lookup_profile(struct aa_policy *base, |
| 833 | const char *hname) |
| 834 | { |
| 835 | struct aa_profile *profile = NULL; |
| 836 | char *split; |
| 837 | |
| 838 | for (split = strstr(hname, "//"); split;) { |
| 839 | profile = __strn_find_child(&base->profiles, hname, |
| 840 | split - hname); |
| 841 | if (!profile) |
| 842 | return NULL; |
| 843 | |
| 844 | base = &profile->base; |
| 845 | hname = split + 2; |
| 846 | split = strstr(hname, "//"); |
| 847 | } |
| 848 | |
| 849 | profile = __find_child(&base->profiles, hname); |
| 850 | |
| 851 | return profile; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * aa_lookup_profile - find a profile by its full or partial name |
| 856 | * @ns: the namespace to start from (NOT NULL) |
| 857 | * @hname: name to do lookup on. Does not contain namespace prefix (NOT NULL) |
| 858 | * |
| 859 | * Returns: refcounted profile or NULL if not found |
| 860 | */ |
| 861 | struct aa_profile *aa_lookup_profile(struct aa_namespace *ns, const char *hname) |
| 862 | { |
| 863 | struct aa_profile *profile; |
| 864 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 865 | rcu_read_lock(); |
| 866 | do { |
| 867 | profile = __lookup_profile(&ns->base, hname); |
| 868 | } while (profile && !aa_get_profile_not0(profile)); |
| 869 | rcu_read_unlock(); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 870 | |
John Johansen | bf83208 | 2012-05-16 11:00:05 -0700 | [diff] [blame] | 871 | /* the unconfined profile is not in the regular profile list */ |
| 872 | if (!profile && strcmp(hname, "unconfined") == 0) |
John Johansen | fa2ac46 | 2013-07-10 21:08:43 -0700 | [diff] [blame^] | 873 | profile = aa_get_newest_profile(ns->unconfined); |
John Johansen | bf83208 | 2012-05-16 11:00:05 -0700 | [diff] [blame] | 874 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 875 | /* refcount released by caller */ |
| 876 | return profile; |
| 877 | } |
| 878 | |
| 879 | /** |
| 880 | * replacement_allowed - test to see if replacement is allowed |
| 881 | * @profile: profile to test if it can be replaced (MAYBE NULL) |
| 882 | * @noreplace: true if replacement shouldn't be allowed but addition is okay |
| 883 | * @info: Returns - info about why replacement failed (NOT NULL) |
| 884 | * |
| 885 | * Returns: %0 if replacement allowed else error code |
| 886 | */ |
| 887 | static int replacement_allowed(struct aa_profile *profile, int noreplace, |
| 888 | const char **info) |
| 889 | { |
| 890 | if (profile) { |
| 891 | if (profile->flags & PFLAG_IMMUTABLE) { |
| 892 | *info = "cannot replace immutible profile"; |
| 893 | return -EPERM; |
| 894 | } else if (noreplace) { |
| 895 | *info = "profile already exists"; |
| 896 | return -EEXIST; |
| 897 | } |
| 898 | } |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | /** |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 903 | * aa_audit_policy - Do auditing of policy changes |
| 904 | * @op: policy operation being performed |
| 905 | * @gfp: memory allocation flags |
| 906 | * @name: name of profile being manipulated (NOT NULL) |
| 907 | * @info: any extra information to be audited (MAYBE NULL) |
| 908 | * @error: error code |
| 909 | * |
| 910 | * Returns: the error to be returned after audit is done |
| 911 | */ |
| 912 | static int audit_policy(int op, gfp_t gfp, const char *name, const char *info, |
| 913 | int error) |
| 914 | { |
| 915 | struct common_audit_data sa; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 916 | struct apparmor_audit_data aad = {0,}; |
Eric Paris | 50c205f | 2012-04-04 15:01:43 -0400 | [diff] [blame] | 917 | sa.type = LSM_AUDIT_DATA_NONE; |
Eric Paris | 3b3b0e4 | 2012-04-03 09:37:02 -0700 | [diff] [blame] | 918 | sa.aad = &aad; |
| 919 | aad.op = op; |
| 920 | aad.name = name; |
| 921 | aad.info = info; |
| 922 | aad.error = error; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 923 | |
| 924 | return aa_audit(AUDIT_APPARMOR_STATUS, __aa_current_profile(), gfp, |
| 925 | &sa, NULL); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * aa_may_manage_policy - can the current task manage policy |
| 930 | * @op: the policy manipulation operation being done |
| 931 | * |
| 932 | * Returns: true if the task is allowed to manipulate policy |
| 933 | */ |
| 934 | bool aa_may_manage_policy(int op) |
| 935 | { |
| 936 | /* check if loading policy is locked out */ |
| 937 | if (aa_g_lock_policy) { |
| 938 | audit_policy(op, GFP_KERNEL, NULL, "policy_locked", -EACCES); |
| 939 | return 0; |
| 940 | } |
| 941 | |
| 942 | if (!capable(CAP_MAC_ADMIN)) { |
| 943 | audit_policy(op, GFP_KERNEL, NULL, "not policy admin", -EACCES); |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | return 1; |
| 948 | } |
| 949 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 950 | static struct aa_profile *__list_lookup_parent(struct list_head *lh, |
| 951 | struct aa_profile *profile) |
| 952 | { |
| 953 | const char *base = hname_tail(profile->base.hname); |
| 954 | long len = base - profile->base.hname; |
| 955 | struct aa_load_ent *ent; |
| 956 | |
| 957 | /* parent won't have trailing // so remove from len */ |
| 958 | if (len <= 2) |
| 959 | return NULL; |
| 960 | len -= 2; |
| 961 | |
| 962 | list_for_each_entry(ent, lh, list) { |
| 963 | if (ent->new == profile) |
| 964 | continue; |
| 965 | if (strncmp(ent->new->base.hname, profile->base.hname, len) == |
| 966 | 0 && ent->new->base.hname[len] == 0) |
| 967 | return ent->new; |
| 968 | } |
| 969 | |
| 970 | return NULL; |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * __replace_profile - replace @old with @new on a list |
| 975 | * @old: profile to be replaced (NOT NULL) |
| 976 | * @new: profile to replace @old with (NOT NULL) |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 977 | * @share_replacedby: transfer @old->replacedby to @new |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 978 | * |
| 979 | * Will duplicate and refcount elements that @new inherits from @old |
| 980 | * and will inherit @old children. |
| 981 | * |
| 982 | * refcount @new for list, put @old list refcount |
| 983 | * |
| 984 | * Requires: namespace list lock be held, or list not be shared |
| 985 | */ |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 986 | static void __replace_profile(struct aa_profile *old, struct aa_profile *new, |
| 987 | bool share_replacedby) |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 988 | { |
| 989 | struct aa_profile *child, *tmp; |
| 990 | |
| 991 | if (!list_empty(&old->base.profiles)) { |
| 992 | LIST_HEAD(lh); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 993 | list_splice_init_rcu(&old->base.profiles, &lh, synchronize_rcu); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 994 | |
| 995 | list_for_each_entry_safe(child, tmp, &lh, base.list) { |
| 996 | struct aa_profile *p; |
| 997 | |
| 998 | list_del_init(&child->base.list); |
| 999 | p = __find_child(&new->base.profiles, child->base.name); |
| 1000 | if (p) { |
| 1001 | /* @p replaces @child */ |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 1002 | __replace_profile(child, p, share_replacedby); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1003 | continue; |
| 1004 | } |
| 1005 | |
| 1006 | /* inherit @child and its children */ |
| 1007 | /* TODO: update hname of inherited children */ |
| 1008 | /* list refcount transferred to @new */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1009 | p = rcu_dereference_protected(child->parent, |
| 1010 | mutex_is_locked(&child->ns->lock)); |
| 1011 | rcu_assign_pointer(child->parent, aa_get_profile(new)); |
| 1012 | list_add_rcu(&child->base.list, &new->base.profiles); |
| 1013 | aa_put_profile(p); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1014 | } |
| 1015 | } |
| 1016 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1017 | if (!rcu_access_pointer(new->parent)) { |
| 1018 | struct aa_profile *parent = rcu_dereference(old->parent); |
| 1019 | rcu_assign_pointer(new->parent, aa_get_profile(parent)); |
| 1020 | } |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 1021 | __aa_update_replacedby(old, new); |
| 1022 | if (share_replacedby) { |
| 1023 | aa_put_replacedby(new->replacedby); |
| 1024 | new->replacedby = aa_get_replacedby(old->replacedby); |
| 1025 | } |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1026 | |
| 1027 | if (list_empty(&new->base.list)) { |
| 1028 | /* new is not on a list already */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1029 | list_replace_rcu(&old->base.list, &new->base.list); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1030 | aa_get_profile(new); |
| 1031 | aa_put_profile(old); |
| 1032 | } else |
| 1033 | __list_remove_profile(old); |
| 1034 | } |
| 1035 | |
| 1036 | /** |
| 1037 | * __lookup_replace - lookup replacement information for a profile |
| 1038 | * @ns - namespace the lookup occurs in |
| 1039 | * @hname - name of profile to lookup |
| 1040 | * @noreplace - true if not replacing an existing profile |
| 1041 | * @p - Returns: profile to be replaced |
| 1042 | * @info - Returns: info string on why lookup failed |
| 1043 | * |
| 1044 | * Returns: profile to replace (no ref) on success else ptr error |
| 1045 | */ |
| 1046 | static int __lookup_replace(struct aa_namespace *ns, const char *hname, |
| 1047 | bool noreplace, struct aa_profile **p, |
| 1048 | const char **info) |
| 1049 | { |
| 1050 | *p = aa_get_profile(__lookup_profile(&ns->base, hname)); |
| 1051 | if (*p) { |
| 1052 | int error = replacement_allowed(*p, noreplace, info); |
| 1053 | if (error) { |
| 1054 | *info = "profile can not be replaced"; |
| 1055 | return error; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | return 0; |
| 1060 | } |
| 1061 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1062 | /** |
| 1063 | * aa_replace_profiles - replace profile(s) on the profile list |
| 1064 | * @udata: serialized data stream (NOT NULL) |
| 1065 | * @size: size of the serialized data stream |
| 1066 | * @noreplace: true if only doing addition, no replacement allowed |
| 1067 | * |
| 1068 | * unpack and replace a profile on the profile list and uses of that profile |
| 1069 | * by any aa_task_cxt. If the profile does not exist on the profile list |
| 1070 | * it is added. |
| 1071 | * |
| 1072 | * Returns: size of data consumed else error code on failure. |
| 1073 | */ |
| 1074 | ssize_t aa_replace_profiles(void *udata, size_t size, bool noreplace) |
| 1075 | { |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1076 | const char *ns_name, *name = NULL, *info = NULL; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1077 | struct aa_namespace *ns = NULL; |
| 1078 | struct aa_load_ent *ent, *tmp; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1079 | int op = OP_PROF_REPL; |
| 1080 | ssize_t error; |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1081 | LIST_HEAD(lh); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1082 | |
| 1083 | /* released below */ |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1084 | error = aa_unpack(udata, size, &lh, &ns_name); |
| 1085 | if (error) |
| 1086 | goto out; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1087 | |
| 1088 | /* released below */ |
| 1089 | ns = aa_prepare_namespace(ns_name); |
| 1090 | if (!ns) { |
| 1091 | info = "failed to prepare namespace"; |
| 1092 | error = -ENOMEM; |
| 1093 | name = ns_name; |
| 1094 | goto fail; |
| 1095 | } |
| 1096 | |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1097 | mutex_lock(&ns->lock); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1098 | /* setup parent and ns info */ |
| 1099 | list_for_each_entry(ent, &lh, list) { |
| 1100 | struct aa_policy *policy; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1101 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1102 | name = ent->new->base.hname; |
| 1103 | error = __lookup_replace(ns, ent->new->base.hname, noreplace, |
| 1104 | &ent->old, &info); |
| 1105 | if (error) |
| 1106 | goto fail_lock; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1107 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1108 | if (ent->new->rename) { |
| 1109 | error = __lookup_replace(ns, ent->new->rename, |
| 1110 | noreplace, &ent->rename, |
| 1111 | &info); |
| 1112 | if (error) |
| 1113 | goto fail_lock; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1114 | } |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1115 | |
| 1116 | /* released when @new is freed */ |
| 1117 | ent->new->ns = aa_get_namespace(ns); |
| 1118 | |
| 1119 | if (ent->old || ent->rename) |
| 1120 | continue; |
| 1121 | |
| 1122 | /* no ref on policy only use inside lock */ |
| 1123 | policy = __lookup_parent(ns, ent->new->base.hname); |
| 1124 | if (!policy) { |
| 1125 | struct aa_profile *p; |
| 1126 | p = __list_lookup_parent(&lh, ent->new); |
| 1127 | if (!p) { |
| 1128 | error = -ENOENT; |
| 1129 | info = "parent does not exist"; |
| 1130 | name = ent->new->base.hname; |
| 1131 | goto fail_lock; |
| 1132 | } |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1133 | rcu_assign_pointer(ent->new->parent, aa_get_profile(p)); |
| 1134 | } else if (policy != &ns->base) { |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1135 | /* released on profile replacement or free_profile */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1136 | struct aa_profile *p = (struct aa_profile *) policy; |
| 1137 | rcu_assign_pointer(ent->new->parent, aa_get_profile(p)); |
| 1138 | } |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1141 | /* do actual replacement */ |
| 1142 | list_for_each_entry_safe(ent, tmp, &lh, list) { |
| 1143 | list_del_init(&ent->list); |
| 1144 | op = (!ent->old && !ent->rename) ? OP_PROF_LOAD : OP_PROF_REPL; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1145 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1146 | audit_policy(op, GFP_ATOMIC, ent->new->base.name, NULL, error); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1147 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1148 | if (ent->old) { |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 1149 | __replace_profile(ent->old, ent->new, 1); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1150 | if (ent->rename) |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 1151 | __replace_profile(ent->rename, ent->new, 0); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1152 | } else if (ent->rename) { |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 1153 | __replace_profile(ent->rename, ent->new, 0); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1154 | } else if (ent->new->parent) { |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1155 | struct aa_profile *parent, *newest; |
| 1156 | parent = rcu_dereference_protected(ent->new->parent, |
| 1157 | mutex_is_locked(&ns->lock)); |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 1158 | newest = aa_get_newest_profile(parent); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1159 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1160 | /* parent replaced in this atomic set? */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1161 | if (newest != parent) { |
| 1162 | aa_get_profile(newest); |
| 1163 | aa_put_profile(parent); |
| 1164 | rcu_assign_pointer(ent->new->parent, newest); |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 1165 | } else |
| 1166 | aa_put_profile(newest); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1167 | __list_add_profile(&parent->base.profiles, ent->new); |
| 1168 | } else |
| 1169 | __list_add_profile(&ns->base.profiles, ent->new); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1170 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1171 | aa_load_ent_free(ent); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1172 | } |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1173 | mutex_unlock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1174 | |
| 1175 | out: |
| 1176 | aa_put_namespace(ns); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1177 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1178 | if (error) |
| 1179 | return error; |
| 1180 | return size; |
| 1181 | |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1182 | fail_lock: |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1183 | mutex_unlock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1184 | fail: |
| 1185 | error = audit_policy(op, GFP_KERNEL, name, info, error); |
John Johansen | dd51c848 | 2013-07-10 21:05:43 -0700 | [diff] [blame] | 1186 | |
| 1187 | list_for_each_entry_safe(ent, tmp, &lh, list) { |
| 1188 | list_del_init(&ent->list); |
| 1189 | aa_load_ent_free(ent); |
| 1190 | } |
| 1191 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1192 | goto out; |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * aa_remove_profiles - remove profile(s) from the system |
| 1197 | * @fqname: name of the profile or namespace to remove (NOT NULL) |
| 1198 | * @size: size of the name |
| 1199 | * |
| 1200 | * Remove a profile or sub namespace from the current namespace, so that |
| 1201 | * they can not be found anymore and mark them as replaced by unconfined |
| 1202 | * |
| 1203 | * NOTE: removing confinement does not restore rlimits to preconfinemnet values |
| 1204 | * |
| 1205 | * Returns: size of data consume else error code if fails |
| 1206 | */ |
| 1207 | ssize_t aa_remove_profiles(char *fqname, size_t size) |
| 1208 | { |
| 1209 | struct aa_namespace *root, *ns = NULL; |
| 1210 | struct aa_profile *profile = NULL; |
| 1211 | const char *name = fqname, *info = NULL; |
| 1212 | ssize_t error = 0; |
| 1213 | |
| 1214 | if (*fqname == 0) { |
| 1215 | info = "no profile specified"; |
| 1216 | error = -ENOENT; |
| 1217 | goto fail; |
| 1218 | } |
| 1219 | |
| 1220 | root = aa_current_profile()->ns; |
| 1221 | |
| 1222 | if (fqname[0] == ':') { |
| 1223 | char *ns_name; |
| 1224 | name = aa_split_fqname(fqname, &ns_name); |
John Johansen | 41d1b3e | 2013-02-21 01:14:17 -0800 | [diff] [blame] | 1225 | /* released below */ |
| 1226 | ns = aa_find_namespace(root, ns_name); |
| 1227 | if (!ns) { |
| 1228 | info = "namespace does not exist"; |
| 1229 | error = -ENOENT; |
| 1230 | goto fail; |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1231 | } |
| 1232 | } else |
| 1233 | /* released below */ |
| 1234 | ns = aa_get_namespace(root); |
| 1235 | |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1236 | if (!name) { |
| 1237 | /* remove namespace - can only happen if fqname[0] == ':' */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1238 | mutex_lock(&ns->parent->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1239 | __remove_namespace(ns); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1240 | mutex_unlock(&ns->parent->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1241 | } else { |
| 1242 | /* remove profile */ |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1243 | mutex_lock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1244 | profile = aa_get_profile(__lookup_profile(&ns->base, name)); |
| 1245 | if (!profile) { |
| 1246 | error = -ENOENT; |
| 1247 | info = "profile does not exist"; |
| 1248 | goto fail_ns_lock; |
| 1249 | } |
| 1250 | name = profile->base.hname; |
| 1251 | __remove_profile(profile); |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1252 | mutex_unlock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1253 | } |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1254 | |
| 1255 | /* don't fail removal if audit fails */ |
| 1256 | (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error); |
| 1257 | aa_put_namespace(ns); |
| 1258 | aa_put_profile(profile); |
| 1259 | return size; |
| 1260 | |
| 1261 | fail_ns_lock: |
John Johansen | 01e2b67 | 2013-07-10 21:06:43 -0700 | [diff] [blame] | 1262 | mutex_unlock(&ns->lock); |
John Johansen | c88d4c7 | 2010-07-29 14:48:00 -0700 | [diff] [blame] | 1263 | aa_put_namespace(ns); |
| 1264 | |
| 1265 | fail: |
| 1266 | (void) audit_policy(OP_PROF_RM, GFP_KERNEL, name, info, error); |
| 1267 | return error; |
| 1268 | } |