]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - security/apparmor/policy_ns.c
2 * AppArmor security module
4 * This file contains AppArmor policy manipulation functions
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2015 Canonical Ltd.
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
14 * AppArmor policy namespaces, allow for different sets of policies
15 * to be loaded for tasks within the namespace.
18 #include <linux/list.h>
19 #include <linux/mutex.h>
20 #include <linux/slab.h>
21 #include <linux/string.h>
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"
29 /* root profile namespace */
30 struct aa_ns
*root_ns
;
31 const char *aa_hidden_ns_name
= "---";
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
39 * Returns: true if @view is visible from @curr else false
41 bool aa_ns_visible(struct aa_ns
*curr
, struct aa_ns
*view
, bool subns
)
49 for ( ; view
; view
= view
->parent
) {
50 if (view
->parent
== curr
)
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
63 * Returns: name of @view visible from @curr
65 const char *aa_ns_name(struct aa_ns
*curr
, struct aa_ns
*view
, bool subns
)
67 /* if view == curr then the namespace name isn't displayed */
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
78 return view
->base
.hname
+ strlen(curr
->base
.hname
) + 2;
80 return aa_hidden_ns_name
;
84 * alloc_ns - allocate, initialize and return a new namespace
85 * @prefix: parent namespace name (MAYBE NULL)
86 * @name: a preallocated name (NOT NULL)
88 * Returns: refcounted namespace or NULL on failure.
90 static struct aa_ns
*alloc_ns(const char *prefix
, const char *name
)
94 ns
= kzalloc(sizeof(*ns
), GFP_KERNEL
);
95 AA_DEBUG("%s(%p)\n", __func__
, ns
);
98 if (!aa_policy_init(&ns
->base
, prefix
, name
, GFP_KERNEL
))
101 INIT_LIST_HEAD(&ns
->sub_ns
);
102 mutex_init(&ns
->lock
);
104 /* released by free_namespace */
105 ns
->unconfined
= aa_alloc_profile("unconfined", NULL
, GFP_KERNEL
);
107 goto fail_unconfined
;
109 ns
->unconfined
->label
.flags
|= FLAG_IX_ON_NAME_ERROR
|
110 FLAG_IMMUTIBLE
| FLAG_NS_COUNT
| FLAG_UNCONFINED
;
111 ns
->unconfined
->mode
= APPARMOR_UNCONFINED
;
113 /* ns and ns->unconfined share ns->unconfined refcount */
114 ns
->unconfined
->ns
= ns
;
116 atomic_set(&ns
->uniq_null
, 0);
118 aa_labelset_init(&ns
->labels
);
123 kzfree(ns
->base
.hname
);
130 * aa_free_ns - free a profile namespace
131 * @ns: the namespace to free (MAYBE NULL)
133 * Requires: All references to the namespace must have been put, if the
134 * namespace was referenced by a profile confining a task,
136 void aa_free_ns(struct aa_ns
*ns
)
141 aa_policy_destroy(&ns
->base
);
142 aa_labelset_destroy(&ns
->labels
);
143 aa_put_ns(ns
->parent
);
145 ns
->unconfined
->ns
= NULL
;
146 aa_free_profile(ns
->unconfined
);
151 * __aa_findn_ns - find a namespace on a list by @name
152 * @head: list to search for namespace on (NOT NULL)
153 * @name: name of namespace to look for (NOT NULL)
154 * @n: length of @name
155 * Returns: unrefcounted namespace
157 * Requires: rcu_read_lock be held
159 static struct aa_ns
*__aa_findn_ns(struct list_head
*head
, const char *name
,
162 return (struct aa_ns
*)__policy_strn_find(head
, name
, n
);
166 * aa_find_ns - look up a profile namespace on the namespace list
167 * @root: namespace to search in (NOT NULL)
168 * @name: name of namespace to find (NOT NULL)
169 * @n: length of @name
171 * Returns: a refcounted namespace on the list, or NULL if no namespace
172 * called @name exists.
174 * refcount released by caller
176 struct aa_ns
*aa_findn_ns(struct aa_ns
*root
, const char *name
, size_t n
)
178 struct aa_ns
*ns
= NULL
;
181 ns
= aa_get_ns(__aa_findn_ns(&root
->sub_ns
, name
, n
));
188 * aa_find_ns - look up a profile namespace on the namespace list
189 * @root: namespace to search in (NOT NULL)
190 * @name: name of namespace to find (NOT NULL)
192 * Returns: a refcounted namespace on the list, or NULL if no namespace
193 * called @name exists.
195 * refcount released by caller
197 struct aa_ns
*aa_find_ns(struct aa_ns
*root
, const char *name
)
199 return aa_findn_ns(root
, name
, strlen(name
));
203 * aa_prepare_ns - find an existing or create a new namespace of @name
204 * @root: ns to treat as root
205 * @name: the namespace to find or add (NOT NULL)
207 * Returns: refcounted namespace or NULL if failed to create one
209 struct aa_ns
*aa_prepare_ns(struct aa_ns
*root
, const char *name
)
213 mutex_lock(&root
->lock
);
214 /* try and find the specified ns and if it doesn't exist create it */
215 /* released by caller */
216 ns
= aa_get_ns(__aa_findn_ns(&root
->sub_ns
, name
, strlen(name
)));
218 ns
= alloc_ns(root
->base
.hname
, name
);
221 mutex_lock(&ns
->lock
);
222 if (__aa_fs_ns_mkdir(ns
, ns_subns_dir(root
), name
)) {
223 AA_ERROR("Failed to create interface for ns %s\n",
225 mutex_unlock(&ns
->lock
);
230 ns
->parent
= aa_get_ns(root
);
231 ns
->level
= root
->level
+ 1;
232 list_add_rcu(&ns
->base
.list
, &root
->sub_ns
);
235 mutex_unlock(&ns
->lock
);
238 mutex_unlock(&root
->lock
);
244 static void __ns_list_release(struct list_head
*head
);
247 * destroy_namespace - remove everything contained by @ns
248 * @ns: namespace to have it contents removed (NOT NULL)
250 static void destroy_ns(struct aa_ns
*ns
)
255 mutex_lock(&ns
->lock
);
256 /* release all profiles in this namespace */
257 __aa_profile_list_release(&ns
->base
.profiles
);
259 /* release all sub namespaces */
260 __ns_list_release(&ns
->sub_ns
);
264 write_lock_irqsave(&ns
->labels
.lock
, flags
);
265 __aa_proxy_redirect(ns_unconfined(ns
),
266 ns_unconfined(ns
->parent
));
267 write_unlock_irqrestore(&ns
->labels
.lock
, flags
);
269 __aa_fs_ns_rmdir(ns
);
270 mutex_unlock(&ns
->lock
);
274 * __aa_remove_ns - remove a namespace and all its children
275 * @ns: namespace to be removed (NOT NULL)
277 * Requires: ns->parent->lock be held and ns removed from parent.
279 void __aa_remove_ns(struct aa_ns
*ns
)
281 /* remove ns from namespace list */
282 list_del_rcu(&ns
->base
.list
);
288 * __ns_list_release - remove all profile namespaces on the list put refs
289 * @head: list of profile namespaces (NOT NULL)
291 * Requires: namespace lock be held
293 static void __ns_list_release(struct list_head
*head
)
295 struct aa_ns
*ns
, *tmp
;
296 list_for_each_entry_safe(ns
, tmp
, head
, base
.list
)
302 * aa_alloc_root_ns - allocate the root profile namespace
304 * Returns: %0 on success else error
307 int __init
aa_alloc_root_ns(void)
309 /* released by aa_free_root_ns - used as list ref*/
310 root_ns
= alloc_ns(NULL
, "root");
318 * aa_free_root_ns - free the root profile namespace
320 void __init
aa_free_root_ns(void)
322 struct aa_ns
*ns
= root_ns
;