policy_ns.c 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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"
#include "include/context.h"
#include "include/policy_ns.h"
#include "include/policy.h"

/* root profile namespace */
29
struct aa_ns *root_ns;
30 31 32 33 34 35
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)
36
 * @subns: whether view of a subns is allowed
37 38 39
 *
 * Returns: true if @view is visible from @curr else false
 */
40
bool aa_ns_visible(struct aa_ns *curr, struct aa_ns *view, bool subns)
41 42 43 44
{
	if (curr == view)
		return true;

45 46 47
	if (!subns)
		return false;

48 49 50 51
	for ( ; view; view = view->parent) {
		if (view->parent == curr)
			return true;
	}
52

53 54 55 56 57 58 59
	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)
60
 * @subns - are subns visible
61 62 63
 *
 * Returns: name of @view visible from @curr
 */
64
const char *aa_ns_name(struct aa_ns *curr, struct aa_ns *view, bool subns)
65 66 67 68 69
{
	/* if view == curr then the namespace name isn't displayed */
	if (curr == view)
		return "";

70
	if (aa_ns_visible(curr, view, subns)) {
71 72 73 74 75 76 77 78 79 80 81 82 83
		/* 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;
}

/**
84
 * alloc_ns - allocate, initialize and return a new namespace
85 86 87 88 89
 * @prefix: parent namespace name (MAYBE NULL)
 * @name: a preallocated name  (NOT NULL)
 *
 * Returns: refcounted namespace or NULL on failure.
 */
90
static struct aa_ns *alloc_ns(const char *prefix, const char *name)
91
{
92
	struct aa_ns *ns;
93 94 95 96 97

	ns = kzalloc(sizeof(*ns), GFP_KERNEL);
	AA_DEBUG("%s(%p)\n", __func__, ns);
	if (!ns)
		return NULL;
98
	if (!aa_policy_init(&ns->base, prefix, name, GFP_KERNEL))
99 100 101
		goto fail_ns;

	INIT_LIST_HEAD(&ns->sub_ns);
102
	INIT_LIST_HEAD(&ns->rawdata_list);
103 104
	mutex_init(&ns->lock);

105
	/* released by aa_free_ns() */
106
	ns->unconfined = aa_alloc_profile("unconfined", GFP_KERNEL);
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	if (!ns->unconfined)
		goto fail_unconfined;

	ns->unconfined->flags = PFLAG_IX_ON_NAME_ERROR |
		PFLAG_IMMUTABLE | PFLAG_NS_COUNT;
	ns->unconfined->mode = APPARMOR_UNCONFINED;

	/* ns and ns->unconfined share ns->unconfined refcount */
	ns->unconfined->ns = ns;

	atomic_set(&ns->uniq_null, 0);

	return ns;

fail_unconfined:
	kzfree(ns->base.hname);
fail_ns:
	kzfree(ns);
	return NULL;
}

/**
129
 * aa_free_ns - free a profile namespace
130 131 132 133 134
 * @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,
 */
135
void aa_free_ns(struct aa_ns *ns)
136 137 138 139 140
{
	if (!ns)
		return;

	aa_policy_destroy(&ns->base);
141
	aa_put_ns(ns->parent);
142 143 144 145 146 147 148

	ns->unconfined->ns = NULL;
	aa_free_profile(ns->unconfined);
	kzfree(ns);
}

/**
149
 * aa_findn_ns  -  look up a profile namespace on the namespace list
150 151
 * @root: namespace to search in  (NOT NULL)
 * @name: name of namespace to find  (NOT NULL)
152
 * @n: length of @name
153 154 155 156 157 158
 *
 * Returns: a refcounted namespace on the list, or NULL if no namespace
 *          called @name exists.
 *
 * refcount released by caller
 */
159
struct aa_ns *aa_findn_ns(struct aa_ns *root, const char *name, size_t n)
160
{
161
	struct aa_ns *ns = NULL;
162 163

	rcu_read_lock();
164
	ns = aa_get_ns(__aa_findn_ns(&root->sub_ns, name, n));
165 166 167 168 169
	rcu_read_unlock();

	return ns;
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/**
 * 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));
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
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)
		return NULL;
	mutex_lock(&ns->lock);
	error = __aa_fs_ns_mkdir(ns, ns_subns_dir(parent), name);
	if (error) {
		AA_ERROR("Failed to create interface for ns %s\n",
			 ns->base.name);
		mutex_unlock(&ns->lock);
		aa_free_ns(ns);
		return ERR_PTR(error);
	}
	ns->parent = aa_get_ns(parent);
208
	ns->level = parent->level + 1;
209 210 211 212 213 214 215 216
	list_add_rcu(&ns->base.list, &parent->sub_ns);
	/* add list ref */
	aa_get_ns(ns);
	mutex_unlock(&ns->lock);

	return ns;
}

217
/**
218 219 220 221
 * 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
222
 *
223
 * Returns: the a refcounted ns that has been add or an ERR_PTR
224
 */
225 226
struct aa_ns *__aa_find_or_create_ns(struct aa_ns *parent, const char *name,
				     struct dentry *dir)
227
{
228
	struct aa_ns *ns;
229

230
	AA_BUG(!mutex_is_locked(&parent->lock));
231

232 233 234 235 236 237 238
	/* 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);
239

240 241 242
	/* return ref */
	return ns;
}
243

244 245 246 247 248 249 250 251 252 253 254 255
/**
 * 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;

	mutex_lock(&parent->lock);
256 257
	/* try and find the specified ns and if it doesn't exist create it */
	/* released by caller */
258 259 260 261
	ns = aa_get_ns(__aa_find_ns(&parent->sub_ns, name));
	if (!ns)
		ns = __aa_create_ns(parent, name, NULL);
	mutex_unlock(&parent->lock);
262 263 264 265 266 267 268 269

	/* return ref */
	return ns;
}

static void __ns_list_release(struct list_head *head);

/**
270
 * destroy_ns - remove everything contained by @ns
271
 * @ns: namespace to have it contents removed  (NOT NULL)
272
 */
273
static void destroy_ns(struct aa_ns *ns)
274 275 276 277 278 279 280 281 282 283 284 285
{
	if (!ns)
		return;

	mutex_lock(&ns->lock);
	/* release all profiles in this namespace */
	__aa_profile_list_release(&ns->base.profiles);

	/* release all sub namespaces */
	__ns_list_release(&ns->sub_ns);

	if (ns->parent)
286
		__aa_update_proxy(ns->unconfined, ns->parent->unconfined);
287
	__aa_fs_ns_rmdir(ns);
288 289 290 291
	mutex_unlock(&ns->lock);
}

/**
292
 * __aa_remove_ns - remove a namespace and all its children
293 294 295 296
 * @ns: namespace to be removed  (NOT NULL)
 *
 * Requires: ns->parent->lock be held and ns removed from parent.
 */
297
void __aa_remove_ns(struct aa_ns *ns)
298 299 300
{
	/* remove ns from namespace list */
	list_del_rcu(&ns->base.list);
301 302
	destroy_ns(ns);
	aa_put_ns(ns);
303 304 305 306 307 308 309 310 311 312
}

/**
 * __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)
{
313
	struct aa_ns *ns, *tmp;
314 315

	list_for_each_entry_safe(ns, tmp, head, base.list)
316
		__aa_remove_ns(ns);
317 318 319 320

}

/**
321
 * aa_alloc_root_ns - allocate the root profile namespace
322 323 324 325 326 327 328
 *
 * Returns: %0 on success else error
 *
 */
int __init aa_alloc_root_ns(void)
{
	/* released by aa_free_root_ns - used as list ref*/
329
	root_ns = alloc_ns(NULL, "root");
330 331 332 333 334 335 336 337 338 339 340
	if (!root_ns)
		return -ENOMEM;

	return 0;
}

 /**
  * aa_free_root_ns - free the root profile namespace
  */
void __init aa_free_root_ns(void)
{
341
	 struct aa_ns *ns = root_ns;
342 343 344

	 root_ns = NULL;

345 346
	 destroy_ns(ns);
	 aa_put_ns(ns);
347
}