John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [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-2017 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 | * AppArmor policy namespaces, allow for different sets of policies |
| 15 | * to be loaded for tasks within the namespace. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/mutex.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/string.h> |
| 22 | |
| 23 | #include "include/apparmor.h" |
| 24 | #include "include/context.h" |
| 25 | #include "include/policy_ns.h" |
| 26 | #include "include/policy.h" |
| 27 | |
| 28 | /* root profile namespace */ |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 29 | struct aa_ns *root_ns; |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 30 | const char *aa_hidden_ns_name = "---"; |
| 31 | |
| 32 | /** |
| 33 | * aa_ns_visible - test if @view is visible from @curr |
| 34 | * @curr: namespace to treat as the parent (NOT NULL) |
| 35 | * @view: namespace to test if visible from @curr (NOT NULL) |
John Johansen | 92b6d8e | 2017-01-16 00:42:25 -0800 | [diff] [blame] | 36 | * @subns: whether view of a subns is allowed |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 37 | * |
| 38 | * Returns: true if @view is visible from @curr else false |
| 39 | */ |
John Johansen | 92b6d8e | 2017-01-16 00:42:25 -0800 | [diff] [blame] | 40 | bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 41 | { |
| 42 | if (curr == view) |
| 43 | return true; |
| 44 | |
John Johansen | 92b6d8e | 2017-01-16 00:42:25 -0800 | [diff] [blame] | 45 | if (!subns) |
| 46 | return false; |
| 47 | |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 48 | for ( ; view; view = view->parent) { |
| 49 | if (view->parent == curr) |
| 50 | return true; |
| 51 | } |
John Johansen | 92b6d8e | 2017-01-16 00:42:25 -0800 | [diff] [blame] | 52 | |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * aa_na_name - Find the ns name to display for @view from @curr |
| 58 | * @curr - current namespace (NOT NULL) |
| 59 | * @view - namespace attempting to view (NOT NULL) |
John Johansen | 92b6d8e | 2017-01-16 00:42:25 -0800 | [diff] [blame] | 60 | * @subns - are subns visible |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 61 | * |
| 62 | * Returns: name of @view visible from @curr |
| 63 | */ |
John Johansen | 92b6d8e | 2017-01-16 00:42:25 -0800 | [diff] [blame] | 64 | const char *aa_ns_name(struct aa_ns *curr, struct aa_ns *view, bool subns) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 65 | { |
| 66 | /* if view == curr then the namespace name isn't displayed */ |
| 67 | if (curr == view) |
| 68 | return ""; |
| 69 | |
John Johansen | 92b6d8e | 2017-01-16 00:42:25 -0800 | [diff] [blame] | 70 | if (aa_ns_visible(curr, view, subns)) { |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 71 | /* at this point if a ns is visible it is in a view ns |
| 72 | * thus the curr ns.hname is a prefix of its name. |
| 73 | * Only output the virtualized portion of the name |
| 74 | * Add + 2 to skip over // separating curr hname prefix |
| 75 | * from the visible tail of the views hname |
| 76 | */ |
| 77 | return view->base.hname + strlen(curr->base.hname) + 2; |
| 78 | } |
| 79 | |
| 80 | return aa_hidden_ns_name; |
| 81 | } |
| 82 | |
| 83 | /** |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 84 | * alloc_ns - allocate, initialize and return a new namespace |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 85 | * @prefix: parent namespace name (MAYBE NULL) |
| 86 | * @name: a preallocated name (NOT NULL) |
| 87 | * |
| 88 | * Returns: refcounted namespace or NULL on failure. |
| 89 | */ |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 90 | static struct aa_ns *alloc_ns(const char *prefix, const char *name) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 91 | { |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 92 | struct aa_ns *ns; |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 93 | |
| 94 | ns = kzalloc(sizeof(*ns), GFP_KERNEL); |
| 95 | AA_DEBUG("%s(%p)\n", __func__, ns); |
| 96 | if (!ns) |
| 97 | return NULL; |
John Johansen | d102d89 | 2017-01-16 00:42:31 -0800 | [diff] [blame] | 98 | if (!aa_policy_init(&ns->base, prefix, name, GFP_KERNEL)) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 99 | goto fail_ns; |
| 100 | |
| 101 | INIT_LIST_HEAD(&ns->sub_ns); |
| 102 | mutex_init(&ns->lock); |
| 103 | |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 104 | /* released by aa_free_ns() */ |
John Johansen | 30b026a | 2017-01-16 00:42:35 -0800 | [diff] [blame] | 105 | ns->unconfined = aa_alloc_profile("unconfined", GFP_KERNEL); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 106 | if (!ns->unconfined) |
| 107 | goto fail_unconfined; |
| 108 | |
| 109 | ns->unconfined->flags = PFLAG_IX_ON_NAME_ERROR | |
| 110 | PFLAG_IMMUTABLE | PFLAG_NS_COUNT; |
| 111 | ns->unconfined->mode = APPARMOR_UNCONFINED; |
| 112 | |
| 113 | /* ns and ns->unconfined share ns->unconfined refcount */ |
| 114 | ns->unconfined->ns = ns; |
| 115 | |
| 116 | atomic_set(&ns->uniq_null, 0); |
| 117 | |
| 118 | return ns; |
| 119 | |
| 120 | fail_unconfined: |
| 121 | kzfree(ns->base.hname); |
| 122 | fail_ns: |
| 123 | kzfree(ns); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | /** |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 128 | * aa_free_ns - free a profile namespace |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 129 | * @ns: the namespace to free (MAYBE NULL) |
| 130 | * |
| 131 | * Requires: All references to the namespace must have been put, if the |
| 132 | * namespace was referenced by a profile confining a task, |
| 133 | */ |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 134 | void aa_free_ns(struct aa_ns *ns) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 135 | { |
| 136 | if (!ns) |
| 137 | return; |
| 138 | |
| 139 | aa_policy_destroy(&ns->base); |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 140 | aa_put_ns(ns->parent); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 141 | |
| 142 | ns->unconfined->ns = NULL; |
| 143 | aa_free_profile(ns->unconfined); |
| 144 | kzfree(ns); |
| 145 | } |
| 146 | |
| 147 | /** |
John Johansen | 9a2d40c | 2017-01-16 00:42:22 -0800 | [diff] [blame] | 148 | * aa_findn_ns - look up a profile namespace on the namespace list |
| 149 | * @root: namespace to search in (NOT NULL) |
| 150 | * @name: name of namespace to find (NOT NULL) |
| 151 | * @n: length of @name |
| 152 | * |
| 153 | * Returns: a refcounted namespace on the list, or NULL if no namespace |
| 154 | * called @name exists. |
| 155 | * |
| 156 | * refcount released by caller |
| 157 | */ |
| 158 | struct aa_ns *aa_findn_ns(struct aa_ns *root, const char *name, size_t n) |
| 159 | { |
| 160 | struct aa_ns *ns = NULL; |
| 161 | |
| 162 | rcu_read_lock(); |
| 163 | ns = aa_get_ns(__aa_findn_ns(&root->sub_ns, name, n)); |
| 164 | rcu_read_unlock(); |
| 165 | |
| 166 | return ns; |
| 167 | } |
| 168 | |
| 169 | /** |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 170 | * aa_find_ns - look up a profile namespace on the namespace list |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 171 | * @root: namespace to search in (NOT NULL) |
| 172 | * @name: name of namespace to find (NOT NULL) |
| 173 | * |
| 174 | * Returns: a refcounted namespace on the list, or NULL if no namespace |
| 175 | * called @name exists. |
| 176 | * |
| 177 | * refcount released by caller |
| 178 | */ |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 179 | struct aa_ns *aa_find_ns(struct aa_ns *root, const char *name) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 180 | { |
John Johansen | 9a2d40c | 2017-01-16 00:42:22 -0800 | [diff] [blame] | 181 | return aa_findn_ns(root, name, strlen(name)); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 182 | } |
| 183 | |
John Johansen | 73688d1 | 2017-01-16 00:42:34 -0800 | [diff] [blame] | 184 | static struct aa_ns *__aa_create_ns(struct aa_ns *parent, const char *name, |
| 185 | struct dentry *dir) |
| 186 | { |
| 187 | struct aa_ns *ns; |
| 188 | int error; |
| 189 | |
| 190 | AA_BUG(!parent); |
| 191 | AA_BUG(!name); |
| 192 | AA_BUG(!mutex_is_locked(&parent->lock)); |
| 193 | |
| 194 | ns = alloc_ns(parent->base.hname, name); |
| 195 | if (!ns) |
| 196 | return NULL; |
| 197 | mutex_lock(&ns->lock); |
| 198 | error = __aa_fs_ns_mkdir(ns, ns_subns_dir(parent), name); |
| 199 | if (error) { |
| 200 | AA_ERROR("Failed to create interface for ns %s\n", |
| 201 | ns->base.name); |
| 202 | mutex_unlock(&ns->lock); |
| 203 | aa_free_ns(ns); |
| 204 | return ERR_PTR(error); |
| 205 | } |
| 206 | ns->parent = aa_get_ns(parent); |
John Johansen | ee2351e | 2017-01-16 00:42:46 -0800 | [diff] [blame] | 207 | ns->level = parent->level + 1; |
John Johansen | 73688d1 | 2017-01-16 00:42:34 -0800 | [diff] [blame] | 208 | list_add_rcu(&ns->base.list, &parent->sub_ns); |
| 209 | /* add list ref */ |
| 210 | aa_get_ns(ns); |
| 211 | mutex_unlock(&ns->lock); |
| 212 | |
| 213 | return ns; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * aa_create_ns - create an ns, fail if it already exists |
| 218 | * @parent: the parent of the namespace being created |
| 219 | * @name: the name of the namespace |
| 220 | * @dir: if not null the dir to put the ns entries in |
| 221 | * |
| 222 | * Returns: the a refcounted ns that has been add or an ERR_PTR |
| 223 | */ |
| 224 | struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name, |
| 225 | struct dentry *dir) |
| 226 | { |
| 227 | struct aa_ns *ns; |
| 228 | |
| 229 | AA_BUG(!mutex_is_locked(&parent->lock)); |
| 230 | |
| 231 | /* try and find the specified ns */ |
| 232 | /* released by caller */ |
| 233 | ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name)); |
| 234 | if (!ns) |
| 235 | ns = __aa_create_ns(parent, name, dir); |
| 236 | else |
| 237 | ns = ERR_PTR(-EEXIST); |
| 238 | |
| 239 | /* return ref */ |
| 240 | return ns; |
| 241 | } |
| 242 | |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 243 | /** |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 244 | * aa_prepare_ns - find an existing or create a new namespace of @name |
John Johansen | 73688d1 | 2017-01-16 00:42:34 -0800 | [diff] [blame] | 245 | * @parent: ns to treat as parent |
| 246 | * @name: the namespace to find or add (NOT NULL) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 247 | * |
John Johansen | 73688d1 | 2017-01-16 00:42:34 -0800 | [diff] [blame] | 248 | * Returns: refcounted namespace or PTR_ERR if failed to create one |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 249 | */ |
John Johansen | 73688d1 | 2017-01-16 00:42:34 -0800 | [diff] [blame] | 250 | struct aa_ns *aa_prepare_ns(struct aa_ns *parent, const char *name) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 251 | { |
John Johansen | 73688d1 | 2017-01-16 00:42:34 -0800 | [diff] [blame] | 252 | struct aa_ns *ns; |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 253 | |
John Johansen | 73688d1 | 2017-01-16 00:42:34 -0800 | [diff] [blame] | 254 | mutex_lock(&parent->lock); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 255 | /* try and find the specified ns and if it doesn't exist create it */ |
| 256 | /* released by caller */ |
John Johansen | 73688d1 | 2017-01-16 00:42:34 -0800 | [diff] [blame] | 257 | ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name)); |
| 258 | if (!ns) |
| 259 | ns = __aa_create_ns(parent, name, NULL); |
| 260 | mutex_unlock(&parent->lock); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 261 | |
| 262 | /* return ref */ |
| 263 | return ns; |
| 264 | } |
| 265 | |
| 266 | static void __ns_list_release(struct list_head *head); |
| 267 | |
| 268 | /** |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 269 | * destroy_ns - remove everything contained by @ns |
John Johansen | 31617dd | 2017-01-16 00:42:24 -0800 | [diff] [blame] | 270 | * @ns: namespace to have it contents removed (NOT NULL) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 271 | */ |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 272 | static void destroy_ns(struct aa_ns *ns) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 273 | { |
| 274 | if (!ns) |
| 275 | return; |
| 276 | |
| 277 | mutex_lock(&ns->lock); |
| 278 | /* release all profiles in this namespace */ |
| 279 | __aa_profile_list_release(&ns->base.profiles); |
| 280 | |
| 281 | /* release all sub namespaces */ |
| 282 | __ns_list_release(&ns->sub_ns); |
| 283 | |
| 284 | if (ns->parent) |
John Johansen | 8399588 | 2017-01-16 00:42:19 -0800 | [diff] [blame] | 285 | __aa_update_proxy(ns->unconfined, ns->parent->unconfined); |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 286 | __aa_fs_ns_rmdir(ns); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 287 | mutex_unlock(&ns->lock); |
| 288 | } |
| 289 | |
| 290 | /** |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 291 | * __aa_remove_ns - remove a namespace and all its children |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 292 | * @ns: namespace to be removed (NOT NULL) |
| 293 | * |
| 294 | * Requires: ns->parent->lock be held and ns removed from parent. |
| 295 | */ |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 296 | void __aa_remove_ns(struct aa_ns *ns) |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 297 | { |
| 298 | /* remove ns from namespace list */ |
| 299 | list_del_rcu(&ns->base.list); |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 300 | destroy_ns(ns); |
| 301 | aa_put_ns(ns); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
| 305 | * __ns_list_release - remove all profile namespaces on the list put refs |
| 306 | * @head: list of profile namespaces (NOT NULL) |
| 307 | * |
| 308 | * Requires: namespace lock be held |
| 309 | */ |
| 310 | static void __ns_list_release(struct list_head *head) |
| 311 | { |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 312 | struct aa_ns *ns, *tmp; |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 313 | |
| 314 | list_for_each_entry_safe(ns, tmp, head, base.list) |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 315 | __aa_remove_ns(ns); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 316 | |
| 317 | } |
| 318 | |
| 319 | /** |
John Johansen | 31617dd | 2017-01-16 00:42:24 -0800 | [diff] [blame] | 320 | * aa_alloc_root_ns - allocate the root profile namespace |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 321 | * |
| 322 | * Returns: %0 on success else error |
| 323 | * |
| 324 | */ |
| 325 | int __init aa_alloc_root_ns(void) |
| 326 | { |
| 327 | /* released by aa_free_root_ns - used as list ref*/ |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 328 | root_ns = alloc_ns(NULL, "root"); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 329 | if (!root_ns) |
| 330 | return -ENOMEM; |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * aa_free_root_ns - free the root profile namespace |
| 337 | */ |
| 338 | void __init aa_free_root_ns(void) |
| 339 | { |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 340 | struct aa_ns *ns = root_ns; |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 341 | |
| 342 | root_ns = NULL; |
| 343 | |
John Johansen | 98849df | 2017-01-16 00:42:16 -0800 | [diff] [blame] | 344 | destroy_ns(ns); |
| 345 | aa_put_ns(ns); |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 346 | } |