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/label.h" 27 #include "include/policy.h" 28 29 /* root profile namespace */ 30 struct aa_ns *root_ns; 31 const char *aa_hidden_ns_name = "---"; 32 33 /** 34 * aa_ns_visible - test if @view is visible from @curr 35 * @curr: namespace to treat as the parent (NOT NULL) 36 * @view: namespace to test if visible from @curr (NOT NULL) 37 * @subns: whether view of a subns is allowed 38 * 39 * Returns: true if @view is visible from @curr else false 40 */ 41 bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns) 42 { 43 if (curr == view) 44 return true; 45 46 if (!subns) 47 return false; 48 49 for ( ; view; view = view->parent) { 50 if (view->parent == curr) 51 return true; 52 } 53 54 return false; 55 } 56 57 /** 58 * aa_na_name - Find the ns name to display for @view from @curr 59 * @curr - current namespace (NOT NULL) 60 * @view - namespace attempting to view (NOT NULL) 61 * @subns - are subns visible 62 * 63 * Returns: name of @view visible from @curr 64 */ 65 const char *aa_ns_name(struct aa_ns *curr, struct aa_ns *view, bool subns) 66 { 67 /* if view == curr then the namespace name isn't displayed */ 68 if (curr == view) 69 return ""; 70 71 if (aa_ns_visible(curr, view, subns)) { 72 /* at this point if a ns is visible it is in a view ns 73 * thus the curr ns.hname is a prefix of its name. 74 * Only output the virtualized portion of the name 75 * Add + 2 to skip over // separating curr hname prefix 76 * from the visible tail of the views hname 77 */ 78 return view->base.hname + strlen(curr->base.hname) + 2; 79 } 80 81 return aa_hidden_ns_name; 82 } 83 84 /** 85 * alloc_ns - allocate, initialize and return a new namespace 86 * @prefix: parent namespace name (MAYBE NULL) 87 * @name: a preallocated name (NOT NULL) 88 * 89 * Returns: refcounted namespace or NULL on failure. 90 */ 91 static struct aa_ns *alloc_ns(const char *prefix, const char *name) 92 { 93 struct aa_ns *ns; 94 95 ns = kzalloc(sizeof(*ns), GFP_KERNEL); 96 AA_DEBUG("%s(%p)\n", __func__, ns); 97 if (!ns) 98 return NULL; 99 if (!aa_policy_init(&ns->base, prefix, name, GFP_KERNEL)) 100 goto fail_ns; 101 102 INIT_LIST_HEAD(&ns->sub_ns); 103 INIT_LIST_HEAD(&ns->rawdata_list); 104 mutex_init(&ns->lock); 105 init_waitqueue_head(&ns->wait); 106 107 /* released by aa_free_ns() */ 108 ns->unconfined = aa_alloc_profile("unconfined", NULL, GFP_KERNEL); 109 if (!ns->unconfined) 110 goto fail_unconfined; 111 112 ns->unconfined->label.flags |= FLAG_IX_ON_NAME_ERROR | 113 FLAG_IMMUTIBLE | FLAG_NS_COUNT | FLAG_UNCONFINED; 114 ns->unconfined->mode = APPARMOR_UNCONFINED; 115 116 /* ns and ns->unconfined share ns->unconfined refcount */ 117 ns->unconfined->ns = ns; 118 119 atomic_set(&ns->uniq_null, 0); 120 121 aa_labelset_init(&ns->labels); 122 123 return ns; 124 125 fail_unconfined: 126 kzfree(ns->base.hname); 127 fail_ns: 128 kzfree(ns); 129 return NULL; 130 } 131 132 /** 133 * aa_free_ns - free a profile namespace 134 * @ns: the namespace to free (MAYBE NULL) 135 * 136 * Requires: All references to the namespace must have been put, if the 137 * namespace was referenced by a profile confining a task, 138 */ 139 void aa_free_ns(struct aa_ns *ns) 140 { 141 if (!ns) 142 return; 143 144 aa_policy_destroy(&ns->base); 145 aa_labelset_destroy(&ns->labels); 146 aa_put_ns(ns->parent); 147 148 ns->unconfined->ns = NULL; 149 aa_free_profile(ns->unconfined); 150 kzfree(ns); 151 } 152 153 /** 154 * aa_findn_ns - look up a profile namespace on the namespace list 155 * @root: namespace to search in (NOT NULL) 156 * @name: name of namespace to find (NOT NULL) 157 * @n: length of @name 158 * 159 * Returns: a refcounted namespace on the list, or NULL if no namespace 160 * called @name exists. 161 * 162 * refcount released by caller 163 */ 164 struct aa_ns *aa_findn_ns(struct aa_ns *root, const char *name, size_t n) 165 { 166 struct aa_ns *ns = NULL; 167 168 rcu_read_lock(); 169 ns = aa_get_ns(__aa_findn_ns(&root->sub_ns, name, n)); 170 rcu_read_unlock(); 171 172 return ns; 173 } 174 175 /** 176 * aa_find_ns - look up a profile namespace on the namespace list 177 * @root: namespace to search in (NOT NULL) 178 * @name: name of namespace to find (NOT NULL) 179 * 180 * Returns: a refcounted namespace on the list, or NULL if no namespace 181 * called @name exists. 182 * 183 * refcount released by caller 184 */ 185 struct aa_ns *aa_find_ns(struct aa_ns *root, const char *name) 186 { 187 return aa_findn_ns(root, name, strlen(name)); 188 } 189 190 /** 191 * __aa_lookupn_ns - lookup the namespace matching @hname 192 * @base: base list to start looking up profile name from (NOT NULL) 193 * @hname: hierarchical ns name (NOT NULL) 194 * @n: length of @hname 195 * 196 * Requires: rcu_read_lock be held 197 * 198 * Returns: unrefcounted ns pointer or NULL if not found 199 * 200 * Do a relative name lookup, recursing through profile tree. 201 */ 202 struct aa_ns *__aa_lookupn_ns(struct aa_ns *view, const char *hname, size_t n) 203 { 204 struct aa_ns *ns = view; 205 const char *split; 206 207 for (split = strnstr(hname, "//", n); split; 208 split = strnstr(hname, "//", n)) { 209 ns = __aa_findn_ns(&ns->sub_ns, hname, split - hname); 210 if (!ns) 211 return NULL; 212 213 n -= split + 2 - hname; 214 hname = split + 2; 215 } 216 217 if (n) 218 return __aa_findn_ns(&ns->sub_ns, hname, n); 219 return NULL; 220 } 221 222 /** 223 * aa_lookupn_ns - look up a policy namespace relative to @view 224 * @view: namespace to search in (NOT NULL) 225 * @name: name of namespace to find (NOT NULL) 226 * @n: length of @name 227 * 228 * Returns: a refcounted namespace on the list, or NULL if no namespace 229 * called @name exists. 230 * 231 * refcount released by caller 232 */ 233 struct aa_ns *aa_lookupn_ns(struct aa_ns *view, const char *name, size_t n) 234 { 235 struct aa_ns *ns = NULL; 236 237 rcu_read_lock(); 238 ns = aa_get_ns(__aa_lookupn_ns(view, name, n)); 239 rcu_read_unlock(); 240 241 return ns; 242 } 243 244 static struct aa_ns *__aa_create_ns(struct aa_ns *parent, const char *name, 245 struct dentry *dir) 246 { 247 struct aa_ns *ns; 248 int error; 249 250 AA_BUG(!parent); 251 AA_BUG(!name); 252 AA_BUG(!mutex_is_locked(&parent->lock)); 253 254 ns = alloc_ns(parent->base.hname, name); 255 if (!ns) 256 return NULL; 257 mutex_lock(&ns->lock); 258 error = __aafs_ns_mkdir(ns, ns_subns_dir(parent), name, dir); 259 if (error) { 260 AA_ERROR("Failed to create interface for ns %s\n", 261 ns->base.name); 262 mutex_unlock(&ns->lock); 263 aa_free_ns(ns); 264 return ERR_PTR(error); 265 } 266 ns->parent = aa_get_ns(parent); 267 ns->level = parent->level + 1; 268 list_add_rcu(&ns->base.list, &parent->sub_ns); 269 /* add list ref */ 270 aa_get_ns(ns); 271 mutex_unlock(&ns->lock); 272 273 return ns; 274 } 275 276 /** 277 * aa_create_ns - create an ns, fail if it already exists 278 * @parent: the parent of the namespace being created 279 * @name: the name of the namespace 280 * @dir: if not null the dir to put the ns entries in 281 * 282 * Returns: the a refcounted ns that has been add or an ERR_PTR 283 */ 284 struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name, 285 struct dentry *dir) 286 { 287 struct aa_ns *ns; 288 289 AA_BUG(!mutex_is_locked(&parent->lock)); 290 291 /* try and find the specified ns */ 292 /* released by caller */ 293 ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name)); 294 if (!ns) 295 ns = __aa_create_ns(parent, name, dir); 296 else 297 ns = ERR_PTR(-EEXIST); 298 299 /* return ref */ 300 return ns; 301 } 302 303 /** 304 * aa_prepare_ns - find an existing or create a new namespace of @name 305 * @parent: ns to treat as parent 306 * @name: the namespace to find or add (NOT NULL) 307 * 308 * Returns: refcounted namespace or PTR_ERR if failed to create one 309 */ 310 struct aa_ns *aa_prepare_ns(struct aa_ns *parent, const char *name) 311 { 312 struct aa_ns *ns; 313 314 mutex_lock(&parent->lock); 315 /* try and find the specified ns and if it doesn't exist create it */ 316 /* released by caller */ 317 ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name)); 318 if (!ns) 319 ns = __aa_create_ns(parent, name, NULL); 320 mutex_unlock(&parent->lock); 321 322 /* return ref */ 323 return ns; 324 } 325 326 static void __ns_list_release(struct list_head *head); 327 328 /** 329 * destroy_ns - remove everything contained by @ns 330 * @ns: namespace to have it contents removed (NOT NULL) 331 */ 332 static void destroy_ns(struct aa_ns *ns) 333 { 334 if (!ns) 335 return; 336 337 mutex_lock(&ns->lock); 338 /* release all profiles in this namespace */ 339 __aa_profile_list_release(&ns->base.profiles); 340 341 /* release all sub namespaces */ 342 __ns_list_release(&ns->sub_ns); 343 344 if (ns->parent) { 345 unsigned long flags; 346 347 write_lock_irqsave(&ns->labels.lock, flags); 348 __aa_proxy_redirect(ns_unconfined(ns), 349 ns_unconfined(ns->parent)); 350 write_unlock_irqrestore(&ns->labels.lock, flags); 351 } 352 __aafs_ns_rmdir(ns); 353 mutex_unlock(&ns->lock); 354 } 355 356 /** 357 * __aa_remove_ns - remove a namespace and all its children 358 * @ns: namespace to be removed (NOT NULL) 359 * 360 * Requires: ns->parent->lock be held and ns removed from parent. 361 */ 362 void __aa_remove_ns(struct aa_ns *ns) 363 { 364 /* remove ns from namespace list */ 365 list_del_rcu(&ns->base.list); 366 destroy_ns(ns); 367 aa_put_ns(ns); 368 } 369 370 /** 371 * __ns_list_release - remove all profile namespaces on the list put refs 372 * @head: list of profile namespaces (NOT NULL) 373 * 374 * Requires: namespace lock be held 375 */ 376 static void __ns_list_release(struct list_head *head) 377 { 378 struct aa_ns *ns, *tmp; 379 380 list_for_each_entry_safe(ns, tmp, head, base.list) 381 __aa_remove_ns(ns); 382 383 } 384 385 /** 386 * aa_alloc_root_ns - allocate the root profile namespace 387 * 388 * Returns: %0 on success else error 389 * 390 */ 391 int __init aa_alloc_root_ns(void) 392 { 393 /* released by aa_free_root_ns - used as list ref*/ 394 root_ns = alloc_ns(NULL, "root"); 395 if (!root_ns) 396 return -ENOMEM; 397 398 return 0; 399 } 400 401 /** 402 * aa_free_root_ns - free the root profile namespace 403 */ 404 void __init aa_free_root_ns(void) 405 { 406 struct aa_ns *ns = root_ns; 407 408 root_ns = NULL; 409 410 destroy_ns(ns); 411 aa_put_ns(ns); 412 } 413