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