blob: 1f02cfe1d974d9a13d8a6c9cff4d9c7a52983c18 [file] [log] [blame]
Thomas Gleixnerb886d83c2019-06-01 10:08:55 +02001// SPDX-License-Identifier: GPL-2.0-only
John Johansencff281f2017-01-16 00:42:15 -08002/*
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 *
John Johansencff281f2017-01-16 00:42:15 -080010 * 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"
John Johansend8889d42017-10-11 01:04:48 -070020#include "include/cred.h"
John Johansencff281f2017-01-16 00:42:15 -080021#include "include/policy_ns.h"
John Johansen637f6882017-06-09 08:14:28 -070022#include "include/label.h"
John Johansencff281f2017-01-16 00:42:15 -080023#include "include/policy.h"
24
John Johansen95c0581f2022-05-24 02:38:12 -070025/* kernel label */
26struct aa_label *kernel_t;
27
John Johansencff281f2017-01-16 00:42:15 -080028/* root profile namespace */
John Johansen98849df2017-01-16 00:42:16 -080029struct aa_ns *root_ns;
John Johansencff281f2017-01-16 00:42:15 -080030const 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 Johansen92b6d8e2017-01-16 00:42:25 -080036 * @subns: whether view of a subns is allowed
John Johansencff281f2017-01-16 00:42:15 -080037 *
38 * Returns: true if @view is visible from @curr else false
39 */
John Johansen92b6d8e2017-01-16 00:42:25 -080040bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns)
John Johansencff281f2017-01-16 00:42:15 -080041{
42 if (curr == view)
43 return true;
44
John Johansen92b6d8e2017-01-16 00:42:25 -080045 if (!subns)
46 return false;
47
John Johansencff281f2017-01-16 00:42:15 -080048 for ( ; view; view = view->parent) {
49 if (view->parent == curr)
50 return true;
51 }
John Johansen92b6d8e2017-01-16 00:42:25 -080052
John Johansencff281f2017-01-16 00:42:15 -080053 return false;
54}
55
56/**
Yang Lif3789732022-07-18 14:30:22 +080057 * 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
John Johansencff281f2017-01-16 00:42:15 -080061 *
62 * Returns: name of @view visible from @curr
63 */
John Johansen92b6d8e2017-01-16 00:42:25 -080064const char *aa_ns_name(struct aa_ns *curr, struct aa_ns *view, bool subns)
John Johansencff281f2017-01-16 00:42:15 -080065{
66 /* if view == curr then the namespace name isn't displayed */
67 if (curr == view)
68 return "";
69
John Johansen92b6d8e2017-01-16 00:42:25 -080070 if (aa_ns_visible(curr, view, subns)) {
John Johansencff281f2017-01-16 00:42:15 -080071 /* 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
Souptick Joarder (HPE)a3f215e2022-07-19 07:42:18 +053083static struct aa_profile *alloc_unconfined(const char *name)
John Johansen95c0581f2022-05-24 02:38:12 -070084{
85 struct aa_profile *profile;
86
John Johansen58f89ce2022-10-03 02:48:24 -070087 profile = aa_alloc_null(NULL, name, GFP_KERNEL);
John Johansen95c0581f2022-05-24 02:38:12 -070088 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;
John Johansen95c0581f2022-05-24 02:38:12 -070094
95 return profile;
96}
97
John Johansencff281f2017-01-16 00:42:15 -080098/**
John Johansen98849df2017-01-16 00:42:16 -080099 * alloc_ns - allocate, initialize and return a new namespace
John Johansencff281f2017-01-16 00:42:15 -0800100 * @prefix: parent namespace name (MAYBE NULL)
101 * @name: a preallocated name (NOT NULL)
102 *
103 * Returns: refcounted namespace or NULL on failure.
104 */
John Johansen98849df2017-01-16 00:42:16 -0800105static struct aa_ns *alloc_ns(const char *prefix, const char *name)
John Johansencff281f2017-01-16 00:42:15 -0800106{
John Johansen98849df2017-01-16 00:42:16 -0800107 struct aa_ns *ns;
John Johansencff281f2017-01-16 00:42:15 -0800108
109 ns = kzalloc(sizeof(*ns), GFP_KERNEL);
110 AA_DEBUG("%s(%p)\n", __func__, ns);
111 if (!ns)
112 return NULL;
John Johansend102d892017-01-16 00:42:31 -0800113 if (!aa_policy_init(&ns->base, prefix, name, GFP_KERNEL))
John Johansencff281f2017-01-16 00:42:15 -0800114 goto fail_ns;
115
116 INIT_LIST_HEAD(&ns->sub_ns);
John Johansen5d5182ca2017-05-09 00:08:41 -0700117 INIT_LIST_HEAD(&ns->rawdata_list);
John Johansencff281f2017-01-16 00:42:15 -0800118 mutex_init(&ns->lock);
John Johansend9bf2c22017-05-26 16:27:58 -0700119 init_waitqueue_head(&ns->wait);
John Johansencff281f2017-01-16 00:42:15 -0800120
John Johansen98849df2017-01-16 00:42:16 -0800121 /* released by aa_free_ns() */
John Johansen95c0581f2022-05-24 02:38:12 -0700122 ns->unconfined = alloc_unconfined("unconfined");
John Johansencff281f2017-01-16 00:42:15 -0800123 if (!ns->unconfined)
124 goto fail_unconfined;
John Johansencff281f2017-01-16 00:42:15 -0800125 /* ns and ns->unconfined share ns->unconfined refcount */
126 ns->unconfined->ns = ns;
127
128 atomic_set(&ns->uniq_null, 0);
129
John Johansen637f6882017-06-09 08:14:28 -0700130 aa_labelset_init(&ns->labels);
131
John Johansencff281f2017-01-16 00:42:15 -0800132 return ns;
133
134fail_unconfined:
Xiu Jianfenge9e6fa42022-10-28 20:33:20 +0800135 aa_policy_destroy(&ns->base);
John Johansencff281f2017-01-16 00:42:15 -0800136fail_ns:
Waiman Long453431a2020-08-06 23:18:13 -0700137 kfree_sensitive(ns);
John Johansencff281f2017-01-16 00:42:15 -0800138 return NULL;
139}
140
141/**
John Johansen98849df2017-01-16 00:42:16 -0800142 * aa_free_ns - free a profile namespace
John Johansencff281f2017-01-16 00:42:15 -0800143 * @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 */
John Johansen98849df2017-01-16 00:42:16 -0800148void aa_free_ns(struct aa_ns *ns)
John Johansencff281f2017-01-16 00:42:15 -0800149{
150 if (!ns)
151 return;
152
153 aa_policy_destroy(&ns->base);
John Johansen637f6882017-06-09 08:14:28 -0700154 aa_labelset_destroy(&ns->labels);
John Johansen98849df2017-01-16 00:42:16 -0800155 aa_put_ns(ns->parent);
John Johansencff281f2017-01-16 00:42:15 -0800156
157 ns->unconfined->ns = NULL;
158 aa_free_profile(ns->unconfined);
Waiman Long453431a2020-08-06 23:18:13 -0700159 kfree_sensitive(ns);
John Johansencff281f2017-01-16 00:42:15 -0800160}
161
162/**
John Johansen36642682017-06-02 17:44:27 -0700163 * __aa_lookupn_ns - lookup the namespace matching @hname
Yang Lif3789732022-07-18 14:30:22 +0800164 * @view: namespace to search in (NOT NULL)
John Johansen36642682017-06-02 17:44:27 -0700165 * @hname: hierarchical ns name (NOT NULL)
166 * @n: length of @hname
167 *
168 * Requires: rcu_read_lock be held
169 *
170 * Returns: unrefcounted ns pointer or NULL if not found
171 *
172 * Do a relative name lookup, recursing through profile tree.
173 */
174struct aa_ns *__aa_lookupn_ns(struct aa_ns *view, const char *hname, size_t n)
175{
176 struct aa_ns *ns = view;
177 const char *split;
178
179 for (split = strnstr(hname, "//", n); split;
180 split = strnstr(hname, "//", n)) {
181 ns = __aa_findn_ns(&ns->sub_ns, hname, split - hname);
182 if (!ns)
183 return NULL;
184
185 n -= split + 2 - hname;
186 hname = split + 2;
187 }
188
189 if (n)
190 return __aa_findn_ns(&ns->sub_ns, hname, n);
191 return NULL;
192}
193
194/**
195 * aa_lookupn_ns - look up a policy namespace relative to @view
196 * @view: namespace to search in (NOT NULL)
197 * @name: name of namespace to find (NOT NULL)
198 * @n: length of @name
199 *
200 * Returns: a refcounted namespace on the list, or NULL if no namespace
201 * called @name exists.
202 *
203 * refcount released by caller
204 */
205struct aa_ns *aa_lookupn_ns(struct aa_ns *view, const char *name, size_t n)
206{
207 struct aa_ns *ns = NULL;
208
209 rcu_read_lock();
210 ns = aa_get_ns(__aa_lookupn_ns(view, name, n));
211 rcu_read_unlock();
212
213 return ns;
214}
215
John Johansen73688d12017-01-16 00:42:34 -0800216static struct aa_ns *__aa_create_ns(struct aa_ns *parent, const char *name,
217 struct dentry *dir)
218{
219 struct aa_ns *ns;
220 int error;
221
222 AA_BUG(!parent);
223 AA_BUG(!name);
224 AA_BUG(!mutex_is_locked(&parent->lock));
225
226 ns = alloc_ns(parent->base.hname, name);
227 if (!ns)
Dan Carpenter0a6b2922018-08-02 11:38:23 +0300228 return ERR_PTR(-ENOMEM);
John Johansenfeb3c762017-11-20 23:24:09 -0800229 ns->level = parent->level + 1;
230 mutex_lock_nested(&ns->lock, ns->level);
John Johansen98407f02017-05-25 06:31:46 -0700231 error = __aafs_ns_mkdir(ns, ns_subns_dir(parent), name, dir);
John Johansen73688d12017-01-16 00:42:34 -0800232 if (error) {
233 AA_ERROR("Failed to create interface for ns %s\n",
234 ns->base.name);
235 mutex_unlock(&ns->lock);
236 aa_free_ns(ns);
237 return ERR_PTR(error);
238 }
239 ns->parent = aa_get_ns(parent);
240 list_add_rcu(&ns->base.list, &parent->sub_ns);
241 /* add list ref */
242 aa_get_ns(ns);
243 mutex_unlock(&ns->lock);
244
245 return ns;
246}
247
248/**
Yang Lif3789732022-07-18 14:30:22 +0800249 * __aa_find_or_create_ns - create an ns, fail if it already exists
John Johansen73688d12017-01-16 00:42:34 -0800250 * @parent: the parent of the namespace being created
251 * @name: the name of the namespace
252 * @dir: if not null the dir to put the ns entries in
253 *
254 * Returns: the a refcounted ns that has been add or an ERR_PTR
255 */
256struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name,
257 struct dentry *dir)
258{
259 struct aa_ns *ns;
260
261 AA_BUG(!mutex_is_locked(&parent->lock));
262
263 /* try and find the specified ns */
264 /* released by caller */
265 ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
266 if (!ns)
267 ns = __aa_create_ns(parent, name, dir);
268 else
269 ns = ERR_PTR(-EEXIST);
270
271 /* return ref */
272 return ns;
273}
274
John Johansencff281f2017-01-16 00:42:15 -0800275/**
John Johansen98849df2017-01-16 00:42:16 -0800276 * aa_prepare_ns - find an existing or create a new namespace of @name
John Johansen73688d12017-01-16 00:42:34 -0800277 * @parent: ns to treat as parent
278 * @name: the namespace to find or add (NOT NULL)
John Johansencff281f2017-01-16 00:42:15 -0800279 *
John Johansen73688d12017-01-16 00:42:34 -0800280 * Returns: refcounted namespace or PTR_ERR if failed to create one
John Johansencff281f2017-01-16 00:42:15 -0800281 */
John Johansen73688d12017-01-16 00:42:34 -0800282struct aa_ns *aa_prepare_ns(struct aa_ns *parent, const char *name)
John Johansencff281f2017-01-16 00:42:15 -0800283{
John Johansen73688d12017-01-16 00:42:34 -0800284 struct aa_ns *ns;
John Johansencff281f2017-01-16 00:42:15 -0800285
John Johansenfeb3c762017-11-20 23:24:09 -0800286 mutex_lock_nested(&parent->lock, parent->level);
John Johansencff281f2017-01-16 00:42:15 -0800287 /* try and find the specified ns and if it doesn't exist create it */
288 /* released by caller */
John Johansen73688d12017-01-16 00:42:34 -0800289 ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
290 if (!ns)
291 ns = __aa_create_ns(parent, name, NULL);
292 mutex_unlock(&parent->lock);
John Johansencff281f2017-01-16 00:42:15 -0800293
294 /* return ref */
295 return ns;
296}
297
298static void __ns_list_release(struct list_head *head);
299
300/**
John Johansen98849df2017-01-16 00:42:16 -0800301 * destroy_ns - remove everything contained by @ns
John Johansen31617dd2017-01-16 00:42:24 -0800302 * @ns: namespace to have it contents removed (NOT NULL)
John Johansencff281f2017-01-16 00:42:15 -0800303 */
John Johansen98849df2017-01-16 00:42:16 -0800304static void destroy_ns(struct aa_ns *ns)
John Johansencff281f2017-01-16 00:42:15 -0800305{
306 if (!ns)
307 return;
308
John Johansenfeb3c762017-11-20 23:24:09 -0800309 mutex_lock_nested(&ns->lock, ns->level);
John Johansencff281f2017-01-16 00:42:15 -0800310 /* release all profiles in this namespace */
311 __aa_profile_list_release(&ns->base.profiles);
312
313 /* release all sub namespaces */
314 __ns_list_release(&ns->sub_ns);
315
John Johansen637f6882017-06-09 08:14:28 -0700316 if (ns->parent) {
317 unsigned long flags;
318
319 write_lock_irqsave(&ns->labels.lock, flags);
320 __aa_proxy_redirect(ns_unconfined(ns),
321 ns_unconfined(ns->parent));
322 write_unlock_irqrestore(&ns->labels.lock, flags);
323 }
John Johansenc97204b2017-05-25 06:23:42 -0700324 __aafs_ns_rmdir(ns);
John Johansencff281f2017-01-16 00:42:15 -0800325 mutex_unlock(&ns->lock);
326}
327
328/**
John Johansen98849df2017-01-16 00:42:16 -0800329 * __aa_remove_ns - remove a namespace and all its children
John Johansencff281f2017-01-16 00:42:15 -0800330 * @ns: namespace to be removed (NOT NULL)
331 *
332 * Requires: ns->parent->lock be held and ns removed from parent.
333 */
John Johansen98849df2017-01-16 00:42:16 -0800334void __aa_remove_ns(struct aa_ns *ns)
John Johansencff281f2017-01-16 00:42:15 -0800335{
336 /* remove ns from namespace list */
337 list_del_rcu(&ns->base.list);
John Johansen98849df2017-01-16 00:42:16 -0800338 destroy_ns(ns);
339 aa_put_ns(ns);
John Johansencff281f2017-01-16 00:42:15 -0800340}
341
342/**
343 * __ns_list_release - remove all profile namespaces on the list put refs
344 * @head: list of profile namespaces (NOT NULL)
345 *
346 * Requires: namespace lock be held
347 */
348static void __ns_list_release(struct list_head *head)
349{
John Johansen98849df2017-01-16 00:42:16 -0800350 struct aa_ns *ns, *tmp;
John Johansencff281f2017-01-16 00:42:15 -0800351
352 list_for_each_entry_safe(ns, tmp, head, base.list)
John Johansen98849df2017-01-16 00:42:16 -0800353 __aa_remove_ns(ns);
John Johansencff281f2017-01-16 00:42:15 -0800354
355}
356
357/**
John Johansen31617dd2017-01-16 00:42:24 -0800358 * aa_alloc_root_ns - allocate the root profile namespace
John Johansencff281f2017-01-16 00:42:15 -0800359 *
360 * Returns: %0 on success else error
361 *
362 */
363int __init aa_alloc_root_ns(void)
364{
John Johansen95c0581f2022-05-24 02:38:12 -0700365 struct aa_profile *kernel_p;
366
John Johansencff281f2017-01-16 00:42:15 -0800367 /* released by aa_free_root_ns - used as list ref*/
John Johansen98849df2017-01-16 00:42:16 -0800368 root_ns = alloc_ns(NULL, "root");
John Johansencff281f2017-01-16 00:42:15 -0800369 if (!root_ns)
370 return -ENOMEM;
371
John Johansen95c0581f2022-05-24 02:38:12 -0700372 kernel_p = alloc_unconfined("kernel_t");
373 if (!kernel_p) {
374 destroy_ns(root_ns);
375 aa_free_ns(root_ns);
376 return -ENOMEM;
377 }
378 kernel_t = &kernel_p->label;
379 root_ns->unconfined->ns = aa_get_ns(root_ns);
380
John Johansencff281f2017-01-16 00:42:15 -0800381 return 0;
382}
383
384 /**
385 * aa_free_root_ns - free the root profile namespace
386 */
387void __init aa_free_root_ns(void)
388{
John Johansen98849df2017-01-16 00:42:16 -0800389 struct aa_ns *ns = root_ns;
John Johansencff281f2017-01-16 00:42:15 -0800390
391 root_ns = NULL;
392
John Johansen95c0581f2022-05-24 02:38:12 -0700393 aa_label_free(kernel_t);
John Johansen98849df2017-01-16 00:42:16 -0800394 destroy_ns(ns);
395 aa_put_ns(ns);
John Johansencff281f2017-01-16 00:42:15 -0800396}