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