xref: /openbmc/linux/security/apparmor/lsm.c (revision 954317fe)
1b5e95b48SJohn Johansen /*
2b5e95b48SJohn Johansen  * AppArmor security module
3b5e95b48SJohn Johansen  *
4b5e95b48SJohn Johansen  * This file contains AppArmor LSM hooks.
5b5e95b48SJohn Johansen  *
6b5e95b48SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
7b5e95b48SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
8b5e95b48SJohn Johansen  *
9b5e95b48SJohn Johansen  * This program is free software; you can redistribute it and/or
10b5e95b48SJohn Johansen  * modify it under the terms of the GNU General Public License as
11b5e95b48SJohn Johansen  * published by the Free Software Foundation, version 2 of the
12b5e95b48SJohn Johansen  * License.
13b5e95b48SJohn Johansen  */
14b5e95b48SJohn Johansen 
153c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
16b5e95b48SJohn Johansen #include <linux/moduleparam.h>
17b5e95b48SJohn Johansen #include <linux/mm.h>
18b5e95b48SJohn Johansen #include <linux/mman.h>
19b5e95b48SJohn Johansen #include <linux/mount.h>
20b5e95b48SJohn Johansen #include <linux/namei.h>
21b5e95b48SJohn Johansen #include <linux/ptrace.h>
22b5e95b48SJohn Johansen #include <linux/ctype.h>
23b5e95b48SJohn Johansen #include <linux/sysctl.h>
24b5e95b48SJohn Johansen #include <linux/audit.h>
253486740aSSerge E. Hallyn #include <linux/user_namespace.h>
26e025be0fSWilliam Hua #include <linux/kmemleak.h>
27b5e95b48SJohn Johansen #include <net/sock.h>
28b5e95b48SJohn Johansen 
29b5e95b48SJohn Johansen #include "include/apparmor.h"
30b5e95b48SJohn Johansen #include "include/apparmorfs.h"
31b5e95b48SJohn Johansen #include "include/audit.h"
32b5e95b48SJohn Johansen #include "include/capability.h"
33b5e95b48SJohn Johansen #include "include/context.h"
34b5e95b48SJohn Johansen #include "include/file.h"
35b5e95b48SJohn Johansen #include "include/ipc.h"
36b5e95b48SJohn Johansen #include "include/path.h"
37637f688dSJohn Johansen #include "include/label.h"
38b5e95b48SJohn Johansen #include "include/policy.h"
39cff281f6SJohn Johansen #include "include/policy_ns.h"
40b5e95b48SJohn Johansen #include "include/procattr.h"
412ea3ffb7SJohn Johansen #include "include/mount.h"
42b5e95b48SJohn Johansen 
43b5e95b48SJohn Johansen /* Flag indicating whether initialization completed */
44545de8feSJohn Johansen int apparmor_initialized;
45b5e95b48SJohn Johansen 
46d4669f0bSJohn Johansen DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
47d4669f0bSJohn Johansen 
48d4669f0bSJohn Johansen 
49b5e95b48SJohn Johansen /*
50b5e95b48SJohn Johansen  * LSM hook functions
51b5e95b48SJohn Johansen  */
52b5e95b48SJohn Johansen 
53b5e95b48SJohn Johansen /*
54637f688dSJohn Johansen  * free the associated aa_task_ctx and put its labels
55b5e95b48SJohn Johansen  */
56b5e95b48SJohn Johansen static void apparmor_cred_free(struct cred *cred)
57b5e95b48SJohn Johansen {
5855a26ebfSJohn Johansen 	aa_free_task_context(cred_ctx(cred));
5955a26ebfSJohn Johansen 	cred_ctx(cred) = NULL;
60b5e95b48SJohn Johansen }
61b5e95b48SJohn Johansen 
62b5e95b48SJohn Johansen /*
63b5e95b48SJohn Johansen  * allocate the apparmor part of blank credentials
64b5e95b48SJohn Johansen  */
65b5e95b48SJohn Johansen static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
66b5e95b48SJohn Johansen {
67b5e95b48SJohn Johansen 	/* freed by apparmor_cred_free */
6855a26ebfSJohn Johansen 	struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
6955a26ebfSJohn Johansen 
7055a26ebfSJohn Johansen 	if (!ctx)
71b5e95b48SJohn Johansen 		return -ENOMEM;
72b5e95b48SJohn Johansen 
7355a26ebfSJohn Johansen 	cred_ctx(cred) = ctx;
74b5e95b48SJohn Johansen 	return 0;
75b5e95b48SJohn Johansen }
76b5e95b48SJohn Johansen 
77b5e95b48SJohn Johansen /*
7855a26ebfSJohn Johansen  * prepare new aa_task_ctx for modification by prepare_cred block
79b5e95b48SJohn Johansen  */
80b5e95b48SJohn Johansen static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
81b5e95b48SJohn Johansen 				 gfp_t gfp)
82b5e95b48SJohn Johansen {
83b5e95b48SJohn Johansen 	/* freed by apparmor_cred_free */
8455a26ebfSJohn Johansen 	struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
8555a26ebfSJohn Johansen 
8655a26ebfSJohn Johansen 	if (!ctx)
87b5e95b48SJohn Johansen 		return -ENOMEM;
88b5e95b48SJohn Johansen 
8955a26ebfSJohn Johansen 	aa_dup_task_context(ctx, cred_ctx(old));
9055a26ebfSJohn Johansen 	cred_ctx(new) = ctx;
91b5e95b48SJohn Johansen 	return 0;
92b5e95b48SJohn Johansen }
93b5e95b48SJohn Johansen 
94b5e95b48SJohn Johansen /*
95b5e95b48SJohn Johansen  * transfer the apparmor data to a blank set of creds
96b5e95b48SJohn Johansen  */
97b5e95b48SJohn Johansen static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
98b5e95b48SJohn Johansen {
9955a26ebfSJohn Johansen 	const struct aa_task_ctx *old_ctx = cred_ctx(old);
10055a26ebfSJohn Johansen 	struct aa_task_ctx *new_ctx = cred_ctx(new);
101b5e95b48SJohn Johansen 
10255a26ebfSJohn Johansen 	aa_dup_task_context(new_ctx, old_ctx);
103b5e95b48SJohn Johansen }
104b5e95b48SJohn Johansen 
105b5e95b48SJohn Johansen static int apparmor_ptrace_access_check(struct task_struct *child,
106b5e95b48SJohn Johansen 					unsigned int mode)
107b5e95b48SJohn Johansen {
108b2d09ae4SJohn Johansen 	struct aa_label *tracer, *tracee;
109b2d09ae4SJohn Johansen 	int error;
110b2d09ae4SJohn Johansen 
111b2d09ae4SJohn Johansen 	tracer = begin_current_label_crit_section();
112b2d09ae4SJohn Johansen 	tracee = aa_get_task_label(child);
113b2d09ae4SJohn Johansen 	error = aa_may_ptrace(tracer, tracee,
114b2d09ae4SJohn Johansen 		  mode == PTRACE_MODE_READ ? AA_PTRACE_READ : AA_PTRACE_TRACE);
115b2d09ae4SJohn Johansen 	aa_put_label(tracee);
116b2d09ae4SJohn Johansen 	end_current_label_crit_section(tracer);
117b2d09ae4SJohn Johansen 
118b2d09ae4SJohn Johansen 	return error;
119b5e95b48SJohn Johansen }
120b5e95b48SJohn Johansen 
121b5e95b48SJohn Johansen static int apparmor_ptrace_traceme(struct task_struct *parent)
122b5e95b48SJohn Johansen {
123b2d09ae4SJohn Johansen 	struct aa_label *tracer, *tracee;
124b2d09ae4SJohn Johansen 	int error;
125b2d09ae4SJohn Johansen 
126b2d09ae4SJohn Johansen 	tracee = begin_current_label_crit_section();
127b2d09ae4SJohn Johansen 	tracer = aa_get_task_label(parent);
128b2d09ae4SJohn Johansen 	error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
129b2d09ae4SJohn Johansen 	aa_put_label(tracer);
130b2d09ae4SJohn Johansen 	end_current_label_crit_section(tracee);
131b2d09ae4SJohn Johansen 
132b2d09ae4SJohn Johansen 	return error;
133b5e95b48SJohn Johansen }
134b5e95b48SJohn Johansen 
135b5e95b48SJohn Johansen /* Derived from security/commoncap.c:cap_capget */
136b5e95b48SJohn Johansen static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
137b5e95b48SJohn Johansen 			   kernel_cap_t *inheritable, kernel_cap_t *permitted)
138b5e95b48SJohn Johansen {
139637f688dSJohn Johansen 	struct aa_label *label;
140b5e95b48SJohn Johansen 	const struct cred *cred;
141b5e95b48SJohn Johansen 
142b5e95b48SJohn Johansen 	rcu_read_lock();
143b5e95b48SJohn Johansen 	cred = __task_cred(target);
144637f688dSJohn Johansen 	label = aa_get_newest_cred_label(cred);
145c70c86c4SJohn Johansen 
146b1d9e6b0SCasey Schaufler 	/*
147b1d9e6b0SCasey Schaufler 	 * cap_capget is stacked ahead of this and will
148b1d9e6b0SCasey Schaufler 	 * initialize effective and permitted.
149b1d9e6b0SCasey Schaufler 	 */
150c70c86c4SJohn Johansen 	if (!unconfined(label)) {
151c70c86c4SJohn Johansen 		struct aa_profile *profile;
152c70c86c4SJohn Johansen 		struct label_it i;
153c70c86c4SJohn Johansen 
154c70c86c4SJohn Johansen 		label_for_each_confined(i, label, profile) {
155c70c86c4SJohn Johansen 			if (COMPLAIN_MODE(profile))
156c70c86c4SJohn Johansen 				continue;
157c70c86c4SJohn Johansen 			*effective = cap_intersect(*effective,
158c70c86c4SJohn Johansen 						   profile->caps.allow);
159c70c86c4SJohn Johansen 			*permitted = cap_intersect(*permitted,
160c70c86c4SJohn Johansen 						   profile->caps.allow);
161c70c86c4SJohn Johansen 		}
162b5e95b48SJohn Johansen 	}
163b5e95b48SJohn Johansen 	rcu_read_unlock();
164637f688dSJohn Johansen 	aa_put_label(label);
165b5e95b48SJohn Johansen 
166b5e95b48SJohn Johansen 	return 0;
167b5e95b48SJohn Johansen }
168b5e95b48SJohn Johansen 
1696a9de491SEric Paris static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
1706a9de491SEric Paris 			    int cap, int audit)
171b5e95b48SJohn Johansen {
172637f688dSJohn Johansen 	struct aa_label *label;
173b1d9e6b0SCasey Schaufler 	int error = 0;
174b1d9e6b0SCasey Schaufler 
175637f688dSJohn Johansen 	label = aa_get_newest_cred_label(cred);
176637f688dSJohn Johansen 	if (!unconfined(label))
177c70c86c4SJohn Johansen 		error = aa_capable(label, cap, audit);
178637f688dSJohn Johansen 	aa_put_label(label);
179cf797c0eSJohn Johansen 
180b5e95b48SJohn Johansen 	return error;
181b5e95b48SJohn Johansen }
182b5e95b48SJohn Johansen 
183b5e95b48SJohn Johansen /**
184b5e95b48SJohn Johansen  * common_perm - basic common permission check wrapper fn for paths
185b5e95b48SJohn Johansen  * @op: operation being checked
186b5e95b48SJohn Johansen  * @path: path to check permission of  (NOT NULL)
187b5e95b48SJohn Johansen  * @mask: requested permissions mask
188b5e95b48SJohn Johansen  * @cond: conditional info for the permission request  (NOT NULL)
189b5e95b48SJohn Johansen  *
190b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
191b5e95b48SJohn Johansen  */
19247f6e5ccSJohn Johansen static int common_perm(const char *op, const struct path *path, u32 mask,
193b5e95b48SJohn Johansen 		       struct path_cond *cond)
194b5e95b48SJohn Johansen {
195637f688dSJohn Johansen 	struct aa_label *label;
196b5e95b48SJohn Johansen 	int error = 0;
197b5e95b48SJohn Johansen 
198637f688dSJohn Johansen 	label = __begin_current_label_crit_section();
199637f688dSJohn Johansen 	if (!unconfined(label))
200aebd873eSJohn Johansen 		error = aa_path_perm(op, label, path, 0, mask, cond);
201637f688dSJohn Johansen 	__end_current_label_crit_section(label);
202b5e95b48SJohn Johansen 
203b5e95b48SJohn Johansen 	return error;
204b5e95b48SJohn Johansen }
205b5e95b48SJohn Johansen 
206b5e95b48SJohn Johansen /**
20731f75bfeSJohn Johansen  * common_perm_cond - common permission wrapper around inode cond
20831f75bfeSJohn Johansen  * @op: operation being checked
20931f75bfeSJohn Johansen  * @path: location to check (NOT NULL)
21031f75bfeSJohn Johansen  * @mask: requested permissions mask
21131f75bfeSJohn Johansen  *
21231f75bfeSJohn Johansen  * Returns: %0 else error code if error or permission denied
21331f75bfeSJohn Johansen  */
21431f75bfeSJohn Johansen static int common_perm_cond(const char *op, const struct path *path, u32 mask)
21531f75bfeSJohn Johansen {
21631f75bfeSJohn Johansen 	struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
21731f75bfeSJohn Johansen 				  d_backing_inode(path->dentry)->i_mode
21831f75bfeSJohn Johansen 	};
21931f75bfeSJohn Johansen 
22031f75bfeSJohn Johansen 	if (!path_mediated_fs(path->dentry))
22131f75bfeSJohn Johansen 		return 0;
22231f75bfeSJohn Johansen 
22331f75bfeSJohn Johansen 	return common_perm(op, path, mask, &cond);
22431f75bfeSJohn Johansen }
22531f75bfeSJohn Johansen 
22631f75bfeSJohn Johansen /**
227b5e95b48SJohn Johansen  * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
228b5e95b48SJohn Johansen  * @op: operation being checked
229b5e95b48SJohn Johansen  * @dir: directory of the dentry  (NOT NULL)
230b5e95b48SJohn Johansen  * @dentry: dentry to check  (NOT NULL)
231b5e95b48SJohn Johansen  * @mask: requested permissions mask
232b5e95b48SJohn Johansen  * @cond: conditional info for the permission request  (NOT NULL)
233b5e95b48SJohn Johansen  *
234b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
235b5e95b48SJohn Johansen  */
23647f6e5ccSJohn Johansen static int common_perm_dir_dentry(const char *op, const struct path *dir,
237b5e95b48SJohn Johansen 				  struct dentry *dentry, u32 mask,
238b5e95b48SJohn Johansen 				  struct path_cond *cond)
239b5e95b48SJohn Johansen {
2408486adf0SKees Cook 	struct path path = { .mnt = dir->mnt, .dentry = dentry };
241b5e95b48SJohn Johansen 
242b5e95b48SJohn Johansen 	return common_perm(op, &path, mask, cond);
243b5e95b48SJohn Johansen }
244b5e95b48SJohn Johansen 
245b5e95b48SJohn Johansen /**
246b5e95b48SJohn Johansen  * common_perm_rm - common permission wrapper for operations doing rm
247b5e95b48SJohn Johansen  * @op: operation being checked
248b5e95b48SJohn Johansen  * @dir: directory that the dentry is in  (NOT NULL)
249b5e95b48SJohn Johansen  * @dentry: dentry being rm'd  (NOT NULL)
250b5e95b48SJohn Johansen  * @mask: requested permission mask
251b5e95b48SJohn Johansen  *
252b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
253b5e95b48SJohn Johansen  */
25447f6e5ccSJohn Johansen static int common_perm_rm(const char *op, const struct path *dir,
255b5e95b48SJohn Johansen 			  struct dentry *dentry, u32 mask)
256b5e95b48SJohn Johansen {
257c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
258b5e95b48SJohn Johansen 	struct path_cond cond = { };
259b5e95b48SJohn Johansen 
260efeee83aSJohn Johansen 	if (!inode || !path_mediated_fs(dentry))
261b5e95b48SJohn Johansen 		return 0;
262b5e95b48SJohn Johansen 
263b5e95b48SJohn Johansen 	cond.uid = inode->i_uid;
264b5e95b48SJohn Johansen 	cond.mode = inode->i_mode;
265b5e95b48SJohn Johansen 
266b5e95b48SJohn Johansen 	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
267b5e95b48SJohn Johansen }
268b5e95b48SJohn Johansen 
269b5e95b48SJohn Johansen /**
270b5e95b48SJohn Johansen  * common_perm_create - common permission wrapper for operations doing create
271b5e95b48SJohn Johansen  * @op: operation being checked
272b5e95b48SJohn Johansen  * @dir: directory that dentry will be created in  (NOT NULL)
273b5e95b48SJohn Johansen  * @dentry: dentry to create   (NOT NULL)
274b5e95b48SJohn Johansen  * @mask: request permission mask
275b5e95b48SJohn Johansen  * @mode: created file mode
276b5e95b48SJohn Johansen  *
277b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
278b5e95b48SJohn Johansen  */
27947f6e5ccSJohn Johansen static int common_perm_create(const char *op, const struct path *dir,
280d6b49f7aSAl Viro 			      struct dentry *dentry, u32 mask, umode_t mode)
281b5e95b48SJohn Johansen {
282b5e95b48SJohn Johansen 	struct path_cond cond = { current_fsuid(), mode };
283b5e95b48SJohn Johansen 
284efeee83aSJohn Johansen 	if (!path_mediated_fs(dir->dentry))
285b5e95b48SJohn Johansen 		return 0;
286b5e95b48SJohn Johansen 
287b5e95b48SJohn Johansen 	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
288b5e95b48SJohn Johansen }
289b5e95b48SJohn Johansen 
290989f74e0SAl Viro static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
291b5e95b48SJohn Johansen {
292b5e95b48SJohn Johansen 	return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
293b5e95b48SJohn Johansen }
294b5e95b48SJohn Johansen 
295d3607752SAl Viro static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
2964572befeSAl Viro 			       umode_t mode)
297b5e95b48SJohn Johansen {
298b5e95b48SJohn Johansen 	return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
299b5e95b48SJohn Johansen 				  S_IFDIR);
300b5e95b48SJohn Johansen }
301b5e95b48SJohn Johansen 
302989f74e0SAl Viro static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
303b5e95b48SJohn Johansen {
304b5e95b48SJohn Johansen 	return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
305b5e95b48SJohn Johansen }
306b5e95b48SJohn Johansen 
307d3607752SAl Viro static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
30804fc66e7SAl Viro 			       umode_t mode, unsigned int dev)
309b5e95b48SJohn Johansen {
310b5e95b48SJohn Johansen 	return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
311b5e95b48SJohn Johansen }
312b5e95b48SJohn Johansen 
31381f4c506SAl Viro static int apparmor_path_truncate(const struct path *path)
314b5e95b48SJohn Johansen {
315e53cfe6cSJohn Johansen 	return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
316b5e95b48SJohn Johansen }
317b5e95b48SJohn Johansen 
318d3607752SAl Viro static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
319b5e95b48SJohn Johansen 				 const char *old_name)
320b5e95b48SJohn Johansen {
321b5e95b48SJohn Johansen 	return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
322b5e95b48SJohn Johansen 				  S_IFLNK);
323b5e95b48SJohn Johansen }
324b5e95b48SJohn Johansen 
3253ccee46aSAl Viro static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
326b5e95b48SJohn Johansen 			      struct dentry *new_dentry)
327b5e95b48SJohn Johansen {
328637f688dSJohn Johansen 	struct aa_label *label;
329b5e95b48SJohn Johansen 	int error = 0;
330b5e95b48SJohn Johansen 
331efeee83aSJohn Johansen 	if (!path_mediated_fs(old_dentry))
332b5e95b48SJohn Johansen 		return 0;
333b5e95b48SJohn Johansen 
334637f688dSJohn Johansen 	label = begin_current_label_crit_section();
335637f688dSJohn Johansen 	if (!unconfined(label))
3368014370fSJohn Johansen 		error = aa_path_link(label, old_dentry, new_dir, new_dentry);
337637f688dSJohn Johansen 	end_current_label_crit_section(label);
338cf797c0eSJohn Johansen 
339b5e95b48SJohn Johansen 	return error;
340b5e95b48SJohn Johansen }
341b5e95b48SJohn Johansen 
3423ccee46aSAl Viro static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
3433ccee46aSAl Viro 				const struct path *new_dir, struct dentry *new_dentry)
344b5e95b48SJohn Johansen {
345637f688dSJohn Johansen 	struct aa_label *label;
346b5e95b48SJohn Johansen 	int error = 0;
347b5e95b48SJohn Johansen 
348efeee83aSJohn Johansen 	if (!path_mediated_fs(old_dentry))
349b5e95b48SJohn Johansen 		return 0;
350b5e95b48SJohn Johansen 
351637f688dSJohn Johansen 	label = begin_current_label_crit_section();
352637f688dSJohn Johansen 	if (!unconfined(label)) {
3538486adf0SKees Cook 		struct path old_path = { .mnt = old_dir->mnt,
3548486adf0SKees Cook 					 .dentry = old_dentry };
3558486adf0SKees Cook 		struct path new_path = { .mnt = new_dir->mnt,
3568486adf0SKees Cook 					 .dentry = new_dentry };
357c6f493d6SDavid Howells 		struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
358c6f493d6SDavid Howells 					  d_backing_inode(old_dentry)->i_mode
359b5e95b48SJohn Johansen 		};
360b5e95b48SJohn Johansen 
361aebd873eSJohn Johansen 		error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
362e53cfe6cSJohn Johansen 				     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
363e53cfe6cSJohn Johansen 				     AA_MAY_SETATTR | AA_MAY_DELETE,
364b5e95b48SJohn Johansen 				     &cond);
365b5e95b48SJohn Johansen 		if (!error)
366aebd873eSJohn Johansen 			error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
367e53cfe6cSJohn Johansen 					     0, MAY_WRITE | AA_MAY_SETATTR |
368b5e95b48SJohn Johansen 					     AA_MAY_CREATE, &cond);
369b5e95b48SJohn Johansen 
370b5e95b48SJohn Johansen 	}
371637f688dSJohn Johansen 	end_current_label_crit_section(label);
372cf797c0eSJohn Johansen 
373b5e95b48SJohn Johansen 	return error;
374b5e95b48SJohn Johansen }
375b5e95b48SJohn Johansen 
376be01f9f2SAl Viro static int apparmor_path_chmod(const struct path *path, umode_t mode)
377b5e95b48SJohn Johansen {
37831f75bfeSJohn Johansen 	return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
379b5e95b48SJohn Johansen }
380b5e95b48SJohn Johansen 
3817fd25dacSAl Viro static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
382b5e95b48SJohn Johansen {
38331f75bfeSJohn Johansen 	return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
384b5e95b48SJohn Johansen }
385b5e95b48SJohn Johansen 
3863f7036a0SAl Viro static int apparmor_inode_getattr(const struct path *path)
387b5e95b48SJohn Johansen {
388e53cfe6cSJohn Johansen 	return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
389b5e95b48SJohn Johansen }
390b5e95b48SJohn Johansen 
39183d49856SEric Paris static int apparmor_file_open(struct file *file, const struct cred *cred)
392b5e95b48SJohn Johansen {
393637f688dSJohn Johansen 	struct aa_file_ctx *fctx = file_ctx(file);
394637f688dSJohn Johansen 	struct aa_label *label;
395b5e95b48SJohn Johansen 	int error = 0;
396b5e95b48SJohn Johansen 
397efeee83aSJohn Johansen 	if (!path_mediated_fs(file->f_path.dentry))
398b5e95b48SJohn Johansen 		return 0;
399b5e95b48SJohn Johansen 
400b5e95b48SJohn Johansen 	/* If in exec, permission is handled by bprm hooks.
401b5e95b48SJohn Johansen 	 * Cache permissions granted by the previous exec check, with
402b5e95b48SJohn Johansen 	 * implicit read and executable mmap which are required to
403b5e95b48SJohn Johansen 	 * actually execute the image.
404b5e95b48SJohn Johansen 	 */
405b5e95b48SJohn Johansen 	if (current->in_execve) {
40655a26ebfSJohn Johansen 		fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
407b5e95b48SJohn Johansen 		return 0;
408b5e95b48SJohn Johansen 	}
409b5e95b48SJohn Johansen 
410637f688dSJohn Johansen 	label = aa_get_newest_cred_label(cred);
411637f688dSJohn Johansen 	if (!unconfined(label)) {
412496ad9aaSAl Viro 		struct inode *inode = file_inode(file);
413b5e95b48SJohn Johansen 		struct path_cond cond = { inode->i_uid, inode->i_mode };
414b5e95b48SJohn Johansen 
415aebd873eSJohn Johansen 		error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
416b5e95b48SJohn Johansen 				     aa_map_file_to_perms(file), &cond);
417b5e95b48SJohn Johansen 		/* todo cache full allowed permissions set and state */
41855a26ebfSJohn Johansen 		fctx->allow = aa_map_file_to_perms(file);
419b5e95b48SJohn Johansen 	}
420637f688dSJohn Johansen 	aa_put_label(label);
421b5e95b48SJohn Johansen 
422b5e95b48SJohn Johansen 	return error;
423b5e95b48SJohn Johansen }
424b5e95b48SJohn Johansen 
425b5e95b48SJohn Johansen static int apparmor_file_alloc_security(struct file *file)
426b5e95b48SJohn Johansen {
427cf797c0eSJohn Johansen 	int error = 0;
428cf797c0eSJohn Johansen 
429b5e95b48SJohn Johansen 	/* freed by apparmor_file_free_security */
430637f688dSJohn Johansen 	struct aa_label *label = begin_current_label_crit_section();
431190a9518SJohn Johansen 	file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
4322835a13bSJohn Johansen 	if (!file_ctx(file))
4332835a13bSJohn Johansen 		error = -ENOMEM;
434637f688dSJohn Johansen 	end_current_label_crit_section(label);
435b5e95b48SJohn Johansen 
436cf797c0eSJohn Johansen 	return error;
437b5e95b48SJohn Johansen }
438b5e95b48SJohn Johansen 
439b5e95b48SJohn Johansen static void apparmor_file_free_security(struct file *file)
440b5e95b48SJohn Johansen {
4412835a13bSJohn Johansen 	aa_free_file_ctx(file_ctx(file));
442b5e95b48SJohn Johansen }
443b5e95b48SJohn Johansen 
44447f6e5ccSJohn Johansen static int common_file_perm(const char *op, struct file *file, u32 mask)
445b5e95b48SJohn Johansen {
446190a9518SJohn Johansen 	struct aa_label *label;
447b5e95b48SJohn Johansen 	int error = 0;
448b5e95b48SJohn Johansen 
449192ca6b5SJohn Johansen 	/* don't reaudit files closed during inheritance */
450192ca6b5SJohn Johansen 	if (file->f_path.dentry == aa_null.dentry)
451192ca6b5SJohn Johansen 		return -EACCES;
452192ca6b5SJohn Johansen 
453637f688dSJohn Johansen 	label = __begin_current_label_crit_section();
454190a9518SJohn Johansen 	error = aa_file_perm(op, label, file, mask);
455637f688dSJohn Johansen 	__end_current_label_crit_section(label);
456b5e95b48SJohn Johansen 
457b5e95b48SJohn Johansen 	return error;
458b5e95b48SJohn Johansen }
459b5e95b48SJohn Johansen 
460064dc947SJohn Johansen static int apparmor_file_receive(struct file *file)
461064dc947SJohn Johansen {
462064dc947SJohn Johansen 	return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
463064dc947SJohn Johansen }
464064dc947SJohn Johansen 
465b5e95b48SJohn Johansen static int apparmor_file_permission(struct file *file, int mask)
466b5e95b48SJohn Johansen {
467b5e95b48SJohn Johansen 	return common_file_perm(OP_FPERM, file, mask);
468b5e95b48SJohn Johansen }
469b5e95b48SJohn Johansen 
470b5e95b48SJohn Johansen static int apparmor_file_lock(struct file *file, unsigned int cmd)
471b5e95b48SJohn Johansen {
472b5e95b48SJohn Johansen 	u32 mask = AA_MAY_LOCK;
473b5e95b48SJohn Johansen 
474b5e95b48SJohn Johansen 	if (cmd == F_WRLCK)
475b5e95b48SJohn Johansen 		mask |= MAY_WRITE;
476b5e95b48SJohn Johansen 
477b5e95b48SJohn Johansen 	return common_file_perm(OP_FLOCK, file, mask);
478b5e95b48SJohn Johansen }
479b5e95b48SJohn Johansen 
48047f6e5ccSJohn Johansen static int common_mmap(const char *op, struct file *file, unsigned long prot,
481b5e95b48SJohn Johansen 		       unsigned long flags)
482b5e95b48SJohn Johansen {
483b5e95b48SJohn Johansen 	int mask = 0;
484b5e95b48SJohn Johansen 
485637f688dSJohn Johansen 	if (!file || !file_ctx(file))
486b5e95b48SJohn Johansen 		return 0;
487b5e95b48SJohn Johansen 
488b5e95b48SJohn Johansen 	if (prot & PROT_READ)
489b5e95b48SJohn Johansen 		mask |= MAY_READ;
490b5e95b48SJohn Johansen 	/*
491b5e95b48SJohn Johansen 	 * Private mappings don't require write perms since they don't
492b5e95b48SJohn Johansen 	 * write back to the files
493b5e95b48SJohn Johansen 	 */
494b5e95b48SJohn Johansen 	if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
495b5e95b48SJohn Johansen 		mask |= MAY_WRITE;
496b5e95b48SJohn Johansen 	if (prot & PROT_EXEC)
497b5e95b48SJohn Johansen 		mask |= AA_EXEC_MMAP;
498b5e95b48SJohn Johansen 
499b5e95b48SJohn Johansen 	return common_file_perm(op, file, mask);
500b5e95b48SJohn Johansen }
501b5e95b48SJohn Johansen 
502e5467859SAl Viro static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
503e5467859SAl Viro 			      unsigned long prot, unsigned long flags)
504b5e95b48SJohn Johansen {
505b5e95b48SJohn Johansen 	return common_mmap(OP_FMMAP, file, prot, flags);
506b5e95b48SJohn Johansen }
507b5e95b48SJohn Johansen 
508b5e95b48SJohn Johansen static int apparmor_file_mprotect(struct vm_area_struct *vma,
509b5e95b48SJohn Johansen 				  unsigned long reqprot, unsigned long prot)
510b5e95b48SJohn Johansen {
511b5e95b48SJohn Johansen 	return common_mmap(OP_FMPROT, vma->vm_file, prot,
512b5e95b48SJohn Johansen 			   !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
513b5e95b48SJohn Johansen }
514b5e95b48SJohn Johansen 
5152ea3ffb7SJohn Johansen static int apparmor_sb_mount(const char *dev_name, const struct path *path,
5162ea3ffb7SJohn Johansen 			     const char *type, unsigned long flags, void *data)
5172ea3ffb7SJohn Johansen {
5182ea3ffb7SJohn Johansen 	struct aa_label *label;
5192ea3ffb7SJohn Johansen 	int error = 0;
5202ea3ffb7SJohn Johansen 
5212ea3ffb7SJohn Johansen 	/* Discard magic */
5222ea3ffb7SJohn Johansen 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
5232ea3ffb7SJohn Johansen 		flags &= ~MS_MGC_MSK;
5242ea3ffb7SJohn Johansen 
5252ea3ffb7SJohn Johansen 	flags &= ~AA_MS_IGNORE_MASK;
5262ea3ffb7SJohn Johansen 
5272ea3ffb7SJohn Johansen 	label = __begin_current_label_crit_section();
5282ea3ffb7SJohn Johansen 	if (!unconfined(label)) {
5292ea3ffb7SJohn Johansen 		if (flags & MS_REMOUNT)
5302ea3ffb7SJohn Johansen 			error = aa_remount(label, path, flags, data);
5312ea3ffb7SJohn Johansen 		else if (flags & MS_BIND)
5322ea3ffb7SJohn Johansen 			error = aa_bind_mount(label, path, dev_name, flags);
5332ea3ffb7SJohn Johansen 		else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
5342ea3ffb7SJohn Johansen 				  MS_UNBINDABLE))
5352ea3ffb7SJohn Johansen 			error = aa_mount_change_type(label, path, flags);
5362ea3ffb7SJohn Johansen 		else if (flags & MS_MOVE)
5372ea3ffb7SJohn Johansen 			error = aa_move_mount(label, path, dev_name);
5382ea3ffb7SJohn Johansen 		else
5392ea3ffb7SJohn Johansen 			error = aa_new_mount(label, dev_name, path, type,
5402ea3ffb7SJohn Johansen 					     flags, data);
5412ea3ffb7SJohn Johansen 	}
5422ea3ffb7SJohn Johansen 	__end_current_label_crit_section(label);
5432ea3ffb7SJohn Johansen 
5442ea3ffb7SJohn Johansen 	return error;
5452ea3ffb7SJohn Johansen }
5462ea3ffb7SJohn Johansen 
5472ea3ffb7SJohn Johansen static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
5482ea3ffb7SJohn Johansen {
5492ea3ffb7SJohn Johansen 	struct aa_label *label;
5502ea3ffb7SJohn Johansen 	int error = 0;
5512ea3ffb7SJohn Johansen 
5522ea3ffb7SJohn Johansen 	label = __begin_current_label_crit_section();
5532ea3ffb7SJohn Johansen 	if (!unconfined(label))
5542ea3ffb7SJohn Johansen 		error = aa_umount(label, mnt, flags);
5552ea3ffb7SJohn Johansen 	__end_current_label_crit_section(label);
5562ea3ffb7SJohn Johansen 
5572ea3ffb7SJohn Johansen 	return error;
5582ea3ffb7SJohn Johansen }
5592ea3ffb7SJohn Johansen 
5602ea3ffb7SJohn Johansen static int apparmor_sb_pivotroot(const struct path *old_path,
5612ea3ffb7SJohn Johansen 				 const struct path *new_path)
5622ea3ffb7SJohn Johansen {
5632ea3ffb7SJohn Johansen 	struct aa_label *label;
5642ea3ffb7SJohn Johansen 	int error = 0;
5652ea3ffb7SJohn Johansen 
5662ea3ffb7SJohn Johansen 	label = aa_get_current_label();
5672ea3ffb7SJohn Johansen 	if (!unconfined(label))
5682ea3ffb7SJohn Johansen 		error = aa_pivotroot(label, old_path, new_path);
5692ea3ffb7SJohn Johansen 	aa_put_label(label);
5702ea3ffb7SJohn Johansen 
5712ea3ffb7SJohn Johansen 	return error;
5722ea3ffb7SJohn Johansen }
5732ea3ffb7SJohn Johansen 
574b5e95b48SJohn Johansen static int apparmor_getprocattr(struct task_struct *task, char *name,
575b5e95b48SJohn Johansen 				char **value)
576b5e95b48SJohn Johansen {
577b5e95b48SJohn Johansen 	int error = -ENOENT;
578b5e95b48SJohn Johansen 	/* released below */
579b5e95b48SJohn Johansen 	const struct cred *cred = get_task_cred(task);
58055a26ebfSJohn Johansen 	struct aa_task_ctx *ctx = cred_ctx(cred);
581637f688dSJohn Johansen 	struct aa_label *label = NULL;
582b5e95b48SJohn Johansen 
583b5e95b48SJohn Johansen 	if (strcmp(name, "current") == 0)
584637f688dSJohn Johansen 		label = aa_get_newest_label(ctx->label);
58555a26ebfSJohn Johansen 	else if (strcmp(name, "prev") == 0  && ctx->previous)
586637f688dSJohn Johansen 		label = aa_get_newest_label(ctx->previous);
58755a26ebfSJohn Johansen 	else if (strcmp(name, "exec") == 0 && ctx->onexec)
588637f688dSJohn Johansen 		label = aa_get_newest_label(ctx->onexec);
589b5e95b48SJohn Johansen 	else
590b5e95b48SJohn Johansen 		error = -EINVAL;
591b5e95b48SJohn Johansen 
592637f688dSJohn Johansen 	if (label)
59376a1d263SJohn Johansen 		error = aa_getprocattr(label, value);
59477b071b3SJohn Johansen 
595637f688dSJohn Johansen 	aa_put_label(label);
596b5e95b48SJohn Johansen 	put_cred(cred);
597b5e95b48SJohn Johansen 
598b5e95b48SJohn Johansen 	return error;
599b5e95b48SJohn Johansen }
600b5e95b48SJohn Johansen 
601b21507e2SStephen Smalley static int apparmor_setprocattr(const char *name, void *value,
602b21507e2SStephen Smalley 				size_t size)
603b5e95b48SJohn Johansen {
604e89b8081SVegard Nossum 	char *command, *largs = NULL, *args = value;
605b5e95b48SJohn Johansen 	size_t arg_size;
606b5e95b48SJohn Johansen 	int error;
607ef88a7acSJohn Johansen 	DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
608b5e95b48SJohn Johansen 
609b5e95b48SJohn Johansen 	if (size == 0)
610b5e95b48SJohn Johansen 		return -EINVAL;
611b5e95b48SJohn Johansen 
612e89b8081SVegard Nossum 	/* AppArmor requires that the buffer must be null terminated atm */
613e89b8081SVegard Nossum 	if (args[size - 1] != '\0') {
614e89b8081SVegard Nossum 		/* null terminate */
615e89b8081SVegard Nossum 		largs = args = kmalloc(size + 1, GFP_KERNEL);
616e89b8081SVegard Nossum 		if (!args)
617e89b8081SVegard Nossum 			return -ENOMEM;
618e89b8081SVegard Nossum 		memcpy(args, value, size);
619e89b8081SVegard Nossum 		args[size] = '\0';
620e89b8081SVegard Nossum 	}
621e89b8081SVegard Nossum 
622e89b8081SVegard Nossum 	error = -EINVAL;
623b5e95b48SJohn Johansen 	args = strim(args);
624b5e95b48SJohn Johansen 	command = strsep(&args, " ");
625b5e95b48SJohn Johansen 	if (!args)
626e89b8081SVegard Nossum 		goto out;
627b5e95b48SJohn Johansen 	args = skip_spaces(args);
628b5e95b48SJohn Johansen 	if (!*args)
629e89b8081SVegard Nossum 		goto out;
630b5e95b48SJohn Johansen 
631d4d03f74SJohn Johansen 	arg_size = size - (args - (largs ? largs : (char *) value));
632b5e95b48SJohn Johansen 	if (strcmp(name, "current") == 0) {
633b5e95b48SJohn Johansen 		if (strcmp(command, "changehat") == 0) {
634b5e95b48SJohn Johansen 			error = aa_setprocattr_changehat(args, arg_size,
635df8073c6SJohn Johansen 							 AA_CHANGE_NOFLAGS);
636b5e95b48SJohn Johansen 		} else if (strcmp(command, "permhat") == 0) {
637b5e95b48SJohn Johansen 			error = aa_setprocattr_changehat(args, arg_size,
638df8073c6SJohn Johansen 							 AA_CHANGE_TEST);
639b5e95b48SJohn Johansen 		} else if (strcmp(command, "changeprofile") == 0) {
640df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
641b5e95b48SJohn Johansen 		} else if (strcmp(command, "permprofile") == 0) {
642df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_TEST);
6436c5fc8f1SJohn Johansen 		} else if (strcmp(command, "stack") == 0) {
6446c5fc8f1SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_STACK);
6453eea57c2SJohn Johansen 		} else
6463eea57c2SJohn Johansen 			goto fail;
647b5e95b48SJohn Johansen 	} else if (strcmp(name, "exec") == 0) {
6483eea57c2SJohn Johansen 		if (strcmp(command, "exec") == 0)
649df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_ONEXEC);
6506c5fc8f1SJohn Johansen 		else if (strcmp(command, "stack") == 0)
6516c5fc8f1SJohn Johansen 			error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
6526c5fc8f1SJohn Johansen 							 AA_CHANGE_STACK));
6533eea57c2SJohn Johansen 		else
6543eea57c2SJohn Johansen 			goto fail;
6553eea57c2SJohn Johansen 	} else
656b5e95b48SJohn Johansen 		/* only support the "current" and "exec" process attributes */
657e89b8081SVegard Nossum 		goto fail;
6583eea57c2SJohn Johansen 
659b5e95b48SJohn Johansen 	if (!error)
660b5e95b48SJohn Johansen 		error = size;
661e89b8081SVegard Nossum out:
662e89b8081SVegard Nossum 	kfree(largs);
663b5e95b48SJohn Johansen 	return error;
6643eea57c2SJohn Johansen 
6653eea57c2SJohn Johansen fail:
666637f688dSJohn Johansen 	aad(&sa)->label = begin_current_label_crit_section();
667ef88a7acSJohn Johansen 	aad(&sa)->info = name;
668ef88a7acSJohn Johansen 	aad(&sa)->error = error = -EINVAL;
6693eea57c2SJohn Johansen 	aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
670637f688dSJohn Johansen 	end_current_label_crit_section(aad(&sa)->label);
671e89b8081SVegard Nossum 	goto out;
672b5e95b48SJohn Johansen }
673b5e95b48SJohn Johansen 
674fe864821SJohn Johansen /**
675fe864821SJohn Johansen  * apparmor_bprm_committing_creds - do task cleanup on committing new creds
676fe864821SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
677fe864821SJohn Johansen  */
678fe864821SJohn Johansen static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
679fe864821SJohn Johansen {
680637f688dSJohn Johansen 	struct aa_label *label = aa_current_raw_label();
681fe864821SJohn Johansen 	struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred);
682fe864821SJohn Johansen 
683fe864821SJohn Johansen 	/* bail out if unconfined or not changing profile */
684637f688dSJohn Johansen 	if ((new_ctx->label->proxy == label->proxy) ||
685637f688dSJohn Johansen 	    (unconfined(new_ctx->label)))
686fe864821SJohn Johansen 		return;
687fe864821SJohn Johansen 
688192ca6b5SJohn Johansen 	aa_inherit_files(bprm->cred, current->files);
689192ca6b5SJohn Johansen 
690fe864821SJohn Johansen 	current->pdeath_signal = 0;
691fe864821SJohn Johansen 
692637f688dSJohn Johansen 	/* reset soft limits and set hard limits for the new label */
69386b92cb7SJohn Johansen 	__aa_transition_rlimits(label, new_ctx->label);
694fe864821SJohn Johansen }
695fe864821SJohn Johansen 
696fe864821SJohn Johansen /**
697fe864821SJohn Johansen  * apparmor_bprm_committed_cred - do cleanup after new creds committed
698fe864821SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
699fe864821SJohn Johansen  */
700fe864821SJohn Johansen static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
701fe864821SJohn Johansen {
702fe864821SJohn Johansen 	/* TODO: cleanup signals - ipc mediation */
703fe864821SJohn Johansen 	return;
704fe864821SJohn Johansen }
705fe864821SJohn Johansen 
7067cb4dc9fSJiri Slaby static int apparmor_task_setrlimit(struct task_struct *task,
7077cb4dc9fSJiri Slaby 		unsigned int resource, struct rlimit *new_rlim)
708b5e95b48SJohn Johansen {
709637f688dSJohn Johansen 	struct aa_label *label = __begin_current_label_crit_section();
710b5e95b48SJohn Johansen 	int error = 0;
711b5e95b48SJohn Johansen 
712637f688dSJohn Johansen 	if (!unconfined(label))
71386b92cb7SJohn Johansen 		error = aa_task_setrlimit(label, task, resource, new_rlim);
714637f688dSJohn Johansen 	__end_current_label_crit_section(label);
715b5e95b48SJohn Johansen 
716b5e95b48SJohn Johansen 	return error;
717b5e95b48SJohn Johansen }
718b5e95b48SJohn Johansen 
719cd1dbf76SJohn Johansen static int apparmor_task_kill(struct task_struct *target, struct siginfo *info,
720cd1dbf76SJohn Johansen 			      int sig, u32 secid)
721cd1dbf76SJohn Johansen {
722cd1dbf76SJohn Johansen 	struct aa_label *cl, *tl;
723cd1dbf76SJohn Johansen 	int error;
724cd1dbf76SJohn Johansen 
725cd1dbf76SJohn Johansen 	if (secid)
726cd1dbf76SJohn Johansen 		/* TODO: after secid to label mapping is done.
727cd1dbf76SJohn Johansen 		 *  Dealing with USB IO specific behavior
728cd1dbf76SJohn Johansen 		 */
729cd1dbf76SJohn Johansen 		return 0;
730cd1dbf76SJohn Johansen 	cl = __begin_current_label_crit_section();
731cd1dbf76SJohn Johansen 	tl = aa_get_task_label(target);
732cd1dbf76SJohn Johansen 	error = aa_may_signal(cl, tl, sig);
733cd1dbf76SJohn Johansen 	aa_put_label(tl);
734cd1dbf76SJohn Johansen 	__end_current_label_crit_section(cl);
735cd1dbf76SJohn Johansen 
736cd1dbf76SJohn Johansen 	return error;
737cd1dbf76SJohn Johansen }
738cd1dbf76SJohn Johansen 
739ca97d939SJames Morris static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
740e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
741e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
742e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, apparmor_capget),
743e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, apparmor_capable),
744b5e95b48SJohn Johansen 
7452ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
7462ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
7472ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
7482ea3ffb7SJohn Johansen 
749e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_link, apparmor_path_link),
750e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
751e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
752e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
753e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
754e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
755e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_rename, apparmor_path_rename),
756e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
757e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chown, apparmor_path_chown),
758e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
759e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
760b5e95b48SJohn Johansen 
761e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, apparmor_file_open),
762064dc947SJohn Johansen 	LSM_HOOK_INIT(file_receive, apparmor_file_receive),
763e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, apparmor_file_permission),
764e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
765e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
766e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
767e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
768e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, apparmor_file_lock),
769b5e95b48SJohn Johansen 
770e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
771e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
772b5e95b48SJohn Johansen 
773e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
774e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_free, apparmor_cred_free),
775e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
776e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
777b5e95b48SJohn Johansen 
778e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
779e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
780e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
781b5e95b48SJohn Johansen 
782e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
783cd1dbf76SJohn Johansen 	LSM_HOOK_INIT(task_kill, apparmor_task_kill),
784b5e95b48SJohn Johansen };
785b5e95b48SJohn Johansen 
786b5e95b48SJohn Johansen /*
787b5e95b48SJohn Johansen  * AppArmor sysfs module parameters
788b5e95b48SJohn Johansen  */
789b5e95b48SJohn Johansen 
790101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp);
791101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp);
792b8aa09fdSRusty Russell #define param_check_aabool param_check_bool
7939c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aabool = {
7946a4c2643SJani Nikula 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
795101d6c82SStephen Rothwell 	.set = param_set_aabool,
796101d6c82SStephen Rothwell 	.get = param_get_aabool
797101d6c82SStephen Rothwell };
798b5e95b48SJohn Johansen 
799101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp);
800101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp);
801b8aa09fdSRusty Russell #define param_check_aauint param_check_uint
8029c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aauint = {
803101d6c82SStephen Rothwell 	.set = param_set_aauint,
804101d6c82SStephen Rothwell 	.get = param_get_aauint
805101d6c82SStephen Rothwell };
806b5e95b48SJohn Johansen 
807101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
808101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
809b8aa09fdSRusty Russell #define param_check_aalockpolicy param_check_bool
8109c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aalockpolicy = {
8116a4c2643SJani Nikula 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
812101d6c82SStephen Rothwell 	.set = param_set_aalockpolicy,
813101d6c82SStephen Rothwell 	.get = param_get_aalockpolicy
814101d6c82SStephen Rothwell };
815b5e95b48SJohn Johansen 
816b5e95b48SJohn Johansen static int param_set_audit(const char *val, struct kernel_param *kp);
817b5e95b48SJohn Johansen static int param_get_audit(char *buffer, struct kernel_param *kp);
818b5e95b48SJohn Johansen 
819b5e95b48SJohn Johansen static int param_set_mode(const char *val, struct kernel_param *kp);
820b5e95b48SJohn Johansen static int param_get_mode(char *buffer, struct kernel_param *kp);
821b5e95b48SJohn Johansen 
822b5e95b48SJohn Johansen /* Flag values, also controllable via /sys/module/apparmor/parameters
823b5e95b48SJohn Johansen  * We define special types as we want to do additional mediation.
824b5e95b48SJohn Johansen  */
825b5e95b48SJohn Johansen 
826b5e95b48SJohn Johansen /* AppArmor global enforcement switch - complain, enforce, kill */
827b5e95b48SJohn Johansen enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
828b5e95b48SJohn Johansen module_param_call(mode, param_set_mode, param_get_mode,
829b5e95b48SJohn Johansen 		  &aa_g_profile_mode, S_IRUSR | S_IWUSR);
830b5e95b48SJohn Johansen 
8316059f71fSJohn Johansen /* whether policy verification hashing is enabled */
8327616ac70SArnd Bergmann bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
8333ccb76c5SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_HASH
8346059f71fSJohn Johansen module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
8357616ac70SArnd Bergmann #endif
8366059f71fSJohn Johansen 
837b5e95b48SJohn Johansen /* Debug mode */
838eea7a05fSValentin Rothberg bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
839b5e95b48SJohn Johansen module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
840b5e95b48SJohn Johansen 
841b5e95b48SJohn Johansen /* Audit mode */
842b5e95b48SJohn Johansen enum audit_mode aa_g_audit;
843b5e95b48SJohn Johansen module_param_call(audit, param_set_audit, param_get_audit,
844b5e95b48SJohn Johansen 		  &aa_g_audit, S_IRUSR | S_IWUSR);
845b5e95b48SJohn Johansen 
846b5e95b48SJohn Johansen /* Determines if audit header is included in audited messages.  This
847b5e95b48SJohn Johansen  * provides more context if the audit daemon is not running
848b5e95b48SJohn Johansen  */
849*954317feSThomas Meyer bool aa_g_audit_header = true;
850b5e95b48SJohn Johansen module_param_named(audit_header, aa_g_audit_header, aabool,
851b5e95b48SJohn Johansen 		   S_IRUSR | S_IWUSR);
852b5e95b48SJohn Johansen 
853b5e95b48SJohn Johansen /* lock out loading/removal of policy
854b5e95b48SJohn Johansen  * TODO: add in at boot loading of policy, which is the only way to
855b5e95b48SJohn Johansen  *       load policy, if lock_policy is set
856b5e95b48SJohn Johansen  */
85790ab5ee9SRusty Russell bool aa_g_lock_policy;
858b5e95b48SJohn Johansen module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
859b5e95b48SJohn Johansen 		   S_IRUSR | S_IWUSR);
860b5e95b48SJohn Johansen 
861b5e95b48SJohn Johansen /* Syscall logging mode */
86290ab5ee9SRusty Russell bool aa_g_logsyscall;
863b5e95b48SJohn Johansen module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
864b5e95b48SJohn Johansen 
865b5e95b48SJohn Johansen /* Maximum pathname length before accesses will start getting rejected */
866b5e95b48SJohn Johansen unsigned int aa_g_path_max = 2 * PATH_MAX;
867622f6e32SJohn Johansen module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
868b5e95b48SJohn Johansen 
869b5e95b48SJohn Johansen /* Determines how paranoid loading of policy is and how much verification
870b5e95b48SJohn Johansen  * on the loaded policy is done.
871abbf8734SJohn Johansen  * DEPRECATED: read only as strict checking of load is always done now
872abbf8734SJohn Johansen  * that none root users (user namespaces) can load policy.
873b5e95b48SJohn Johansen  */
874*954317feSThomas Meyer bool aa_g_paranoid_load = true;
875abbf8734SJohn Johansen module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
876b5e95b48SJohn Johansen 
877b5e95b48SJohn Johansen /* Boot time disable flag */
87890ab5ee9SRusty Russell static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
879c611616cSJohn Johansen module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
880b5e95b48SJohn Johansen 
881b5e95b48SJohn Johansen static int __init apparmor_enabled_setup(char *str)
882b5e95b48SJohn Johansen {
883b5e95b48SJohn Johansen 	unsigned long enabled;
88429707b20SJingoo Han 	int error = kstrtoul(str, 0, &enabled);
885b5e95b48SJohn Johansen 	if (!error)
886b5e95b48SJohn Johansen 		apparmor_enabled = enabled ? 1 : 0;
887b5e95b48SJohn Johansen 	return 1;
888b5e95b48SJohn Johansen }
889b5e95b48SJohn Johansen 
890b5e95b48SJohn Johansen __setup("apparmor=", apparmor_enabled_setup);
891b5e95b48SJohn Johansen 
892b5e95b48SJohn Johansen /* set global flag turning off the ability to load policy */
893101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
894b5e95b48SJohn Johansen {
895545de8feSJohn Johansen 	if (!apparmor_enabled)
896545de8feSJohn Johansen 		return -EINVAL;
897545de8feSJohn Johansen 	if (apparmor_initialized && !policy_admin_capable(NULL))
898b5e95b48SJohn Johansen 		return -EPERM;
899b5e95b48SJohn Johansen 	return param_set_bool(val, kp);
900b5e95b48SJohn Johansen }
901b5e95b48SJohn Johansen 
902101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
903b5e95b48SJohn Johansen {
904ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
905ca4bd5aeSJohn Johansen 		return -EINVAL;
906545de8feSJohn Johansen 	if (apparmor_initialized && !policy_view_capable(NULL))
907545de8feSJohn Johansen 		return -EPERM;
908b5e95b48SJohn Johansen 	return param_get_bool(buffer, kp);
909b5e95b48SJohn Johansen }
910b5e95b48SJohn Johansen 
911101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp)
912b5e95b48SJohn Johansen {
913ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
914ca4bd5aeSJohn Johansen 		return -EINVAL;
915545de8feSJohn Johansen 	if (apparmor_initialized && !policy_admin_capable(NULL))
916545de8feSJohn Johansen 		return -EPERM;
917b5e95b48SJohn Johansen 	return param_set_bool(val, kp);
918b5e95b48SJohn Johansen }
919b5e95b48SJohn Johansen 
920101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp)
921b5e95b48SJohn Johansen {
922ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
923ca4bd5aeSJohn Johansen 		return -EINVAL;
924545de8feSJohn Johansen 	if (apparmor_initialized && !policy_view_capable(NULL))
925545de8feSJohn Johansen 		return -EPERM;
926b5e95b48SJohn Johansen 	return param_get_bool(buffer, kp);
927b5e95b48SJohn Johansen }
928b5e95b48SJohn Johansen 
929101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp)
930b5e95b48SJohn Johansen {
93139d84824SJohn Johansen 	int error;
93239d84824SJohn Johansen 
933ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
934ca4bd5aeSJohn Johansen 		return -EINVAL;
93539d84824SJohn Johansen 	/* file is ro but enforce 2nd line check */
93639d84824SJohn Johansen 	if (apparmor_initialized)
937545de8feSJohn Johansen 		return -EPERM;
93839d84824SJohn Johansen 
93939d84824SJohn Johansen 	error = param_set_uint(val, kp);
94039d84824SJohn Johansen 	pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
94139d84824SJohn Johansen 
94239d84824SJohn Johansen 	return error;
943b5e95b48SJohn Johansen }
944b5e95b48SJohn Johansen 
945101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp)
946b5e95b48SJohn Johansen {
947ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
948ca4bd5aeSJohn Johansen 		return -EINVAL;
949545de8feSJohn Johansen 	if (apparmor_initialized && !policy_view_capable(NULL))
950545de8feSJohn Johansen 		return -EPERM;
951b5e95b48SJohn Johansen 	return param_get_uint(buffer, kp);
952b5e95b48SJohn Johansen }
953b5e95b48SJohn Johansen 
954b5e95b48SJohn Johansen static int param_get_audit(char *buffer, struct kernel_param *kp)
955b5e95b48SJohn Johansen {
956b5e95b48SJohn Johansen 	if (!apparmor_enabled)
957b5e95b48SJohn Johansen 		return -EINVAL;
958545de8feSJohn Johansen 	if (apparmor_initialized && !policy_view_capable(NULL))
959545de8feSJohn Johansen 		return -EPERM;
960b5e95b48SJohn Johansen 	return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
961b5e95b48SJohn Johansen }
962b5e95b48SJohn Johansen 
963b5e95b48SJohn Johansen static int param_set_audit(const char *val, struct kernel_param *kp)
964b5e95b48SJohn Johansen {
965b5e95b48SJohn Johansen 	int i;
966b5e95b48SJohn Johansen 
967b5e95b48SJohn Johansen 	if (!apparmor_enabled)
968b5e95b48SJohn Johansen 		return -EINVAL;
969b5e95b48SJohn Johansen 	if (!val)
970b5e95b48SJohn Johansen 		return -EINVAL;
971545de8feSJohn Johansen 	if (apparmor_initialized && !policy_admin_capable(NULL))
972545de8feSJohn Johansen 		return -EPERM;
973b5e95b48SJohn Johansen 
974b5e95b48SJohn Johansen 	for (i = 0; i < AUDIT_MAX_INDEX; i++) {
975b5e95b48SJohn Johansen 		if (strcmp(val, audit_mode_names[i]) == 0) {
976b5e95b48SJohn Johansen 			aa_g_audit = i;
977b5e95b48SJohn Johansen 			return 0;
978b5e95b48SJohn Johansen 		}
979b5e95b48SJohn Johansen 	}
980b5e95b48SJohn Johansen 
981b5e95b48SJohn Johansen 	return -EINVAL;
982b5e95b48SJohn Johansen }
983b5e95b48SJohn Johansen 
984b5e95b48SJohn Johansen static int param_get_mode(char *buffer, struct kernel_param *kp)
985b5e95b48SJohn Johansen {
986b5e95b48SJohn Johansen 	if (!apparmor_enabled)
987b5e95b48SJohn Johansen 		return -EINVAL;
988545de8feSJohn Johansen 	if (apparmor_initialized && !policy_view_capable(NULL))
989545de8feSJohn Johansen 		return -EPERM;
990b5e95b48SJohn Johansen 
9910d259f04SJohn Johansen 	return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
992b5e95b48SJohn Johansen }
993b5e95b48SJohn Johansen 
994b5e95b48SJohn Johansen static int param_set_mode(const char *val, struct kernel_param *kp)
995b5e95b48SJohn Johansen {
996b5e95b48SJohn Johansen 	int i;
997b5e95b48SJohn Johansen 
998b5e95b48SJohn Johansen 	if (!apparmor_enabled)
999b5e95b48SJohn Johansen 		return -EINVAL;
1000b5e95b48SJohn Johansen 	if (!val)
1001b5e95b48SJohn Johansen 		return -EINVAL;
1002545de8feSJohn Johansen 	if (apparmor_initialized && !policy_admin_capable(NULL))
1003545de8feSJohn Johansen 		return -EPERM;
1004b5e95b48SJohn Johansen 
10050d259f04SJohn Johansen 	for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
10060d259f04SJohn Johansen 		if (strcmp(val, aa_profile_mode_names[i]) == 0) {
1007b5e95b48SJohn Johansen 			aa_g_profile_mode = i;
1008b5e95b48SJohn Johansen 			return 0;
1009b5e95b48SJohn Johansen 		}
1010b5e95b48SJohn Johansen 	}
1011b5e95b48SJohn Johansen 
1012b5e95b48SJohn Johansen 	return -EINVAL;
1013b5e95b48SJohn Johansen }
1014b5e95b48SJohn Johansen 
1015b5e95b48SJohn Johansen /*
1016b5e95b48SJohn Johansen  * AppArmor init functions
1017b5e95b48SJohn Johansen  */
1018b5e95b48SJohn Johansen 
1019b5e95b48SJohn Johansen /**
102055a26ebfSJohn Johansen  * set_init_ctx - set a task context and profile on the first task.
1021b5e95b48SJohn Johansen  *
1022b5e95b48SJohn Johansen  * TODO: allow setting an alternate profile than unconfined
1023b5e95b48SJohn Johansen  */
102455a26ebfSJohn Johansen static int __init set_init_ctx(void)
1025b5e95b48SJohn Johansen {
1026b5e95b48SJohn Johansen 	struct cred *cred = (struct cred *)current->real_cred;
102755a26ebfSJohn Johansen 	struct aa_task_ctx *ctx;
1028b5e95b48SJohn Johansen 
102955a26ebfSJohn Johansen 	ctx = aa_alloc_task_context(GFP_KERNEL);
103055a26ebfSJohn Johansen 	if (!ctx)
1031b5e95b48SJohn Johansen 		return -ENOMEM;
1032b5e95b48SJohn Johansen 
1033637f688dSJohn Johansen 	ctx->label = aa_get_label(ns_unconfined(root_ns));
103455a26ebfSJohn Johansen 	cred_ctx(cred) = ctx;
1035b5e95b48SJohn Johansen 
1036b5e95b48SJohn Johansen 	return 0;
1037b5e95b48SJohn Johansen }
1038b5e95b48SJohn Johansen 
1039d4669f0bSJohn Johansen static void destroy_buffers(void)
1040d4669f0bSJohn Johansen {
1041d4669f0bSJohn Johansen 	u32 i, j;
1042d4669f0bSJohn Johansen 
1043d4669f0bSJohn Johansen 	for_each_possible_cpu(i) {
1044d4669f0bSJohn Johansen 		for_each_cpu_buffer(j) {
1045d4669f0bSJohn Johansen 			kfree(per_cpu(aa_buffers, i).buf[j]);
1046d4669f0bSJohn Johansen 			per_cpu(aa_buffers, i).buf[j] = NULL;
1047d4669f0bSJohn Johansen 		}
1048d4669f0bSJohn Johansen 	}
1049d4669f0bSJohn Johansen }
1050d4669f0bSJohn Johansen 
1051d4669f0bSJohn Johansen static int __init alloc_buffers(void)
1052d4669f0bSJohn Johansen {
1053d4669f0bSJohn Johansen 	u32 i, j;
1054d4669f0bSJohn Johansen 
1055d4669f0bSJohn Johansen 	for_each_possible_cpu(i) {
1056d4669f0bSJohn Johansen 		for_each_cpu_buffer(j) {
1057d4669f0bSJohn Johansen 			char *buffer;
1058d4669f0bSJohn Johansen 
1059d4669f0bSJohn Johansen 			if (cpu_to_node(i) > num_online_nodes())
1060d4669f0bSJohn Johansen 				/* fallback to kmalloc for offline nodes */
1061d4669f0bSJohn Johansen 				buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1062d4669f0bSJohn Johansen 			else
1063d4669f0bSJohn Johansen 				buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1064d4669f0bSJohn Johansen 						      cpu_to_node(i));
1065d4669f0bSJohn Johansen 			if (!buffer) {
1066d4669f0bSJohn Johansen 				destroy_buffers();
1067d4669f0bSJohn Johansen 				return -ENOMEM;
1068d4669f0bSJohn Johansen 			}
1069d4669f0bSJohn Johansen 			per_cpu(aa_buffers, i).buf[j] = buffer;
1070d4669f0bSJohn Johansen 		}
1071d4669f0bSJohn Johansen 	}
1072d4669f0bSJohn Johansen 
1073d4669f0bSJohn Johansen 	return 0;
1074d4669f0bSJohn Johansen }
1075d4669f0bSJohn Johansen 
1076e3ea1ca5STyler Hicks #ifdef CONFIG_SYSCTL
1077e3ea1ca5STyler Hicks static int apparmor_dointvec(struct ctl_table *table, int write,
1078e3ea1ca5STyler Hicks 			     void __user *buffer, size_t *lenp, loff_t *ppos)
1079e3ea1ca5STyler Hicks {
1080e3ea1ca5STyler Hicks 	if (!policy_admin_capable(NULL))
1081e3ea1ca5STyler Hicks 		return -EPERM;
1082e3ea1ca5STyler Hicks 	if (!apparmor_enabled)
1083e3ea1ca5STyler Hicks 		return -EINVAL;
1084e3ea1ca5STyler Hicks 
1085e3ea1ca5STyler Hicks 	return proc_dointvec(table, write, buffer, lenp, ppos);
1086e3ea1ca5STyler Hicks }
1087e3ea1ca5STyler Hicks 
1088e3ea1ca5STyler Hicks static struct ctl_path apparmor_sysctl_path[] = {
1089e3ea1ca5STyler Hicks 	{ .procname = "kernel", },
1090e3ea1ca5STyler Hicks 	{ }
1091e3ea1ca5STyler Hicks };
1092e3ea1ca5STyler Hicks 
1093e3ea1ca5STyler Hicks static struct ctl_table apparmor_sysctl_table[] = {
1094e3ea1ca5STyler Hicks 	{
1095e3ea1ca5STyler Hicks 		.procname       = "unprivileged_userns_apparmor_policy",
1096e3ea1ca5STyler Hicks 		.data           = &unprivileged_userns_apparmor_policy,
1097e3ea1ca5STyler Hicks 		.maxlen         = sizeof(int),
1098e3ea1ca5STyler Hicks 		.mode           = 0600,
1099e3ea1ca5STyler Hicks 		.proc_handler   = apparmor_dointvec,
1100e3ea1ca5STyler Hicks 	},
1101e3ea1ca5STyler Hicks 	{ }
1102e3ea1ca5STyler Hicks };
1103e3ea1ca5STyler Hicks 
1104e3ea1ca5STyler Hicks static int __init apparmor_init_sysctl(void)
1105e3ea1ca5STyler Hicks {
1106e3ea1ca5STyler Hicks 	return register_sysctl_paths(apparmor_sysctl_path,
1107e3ea1ca5STyler Hicks 				     apparmor_sysctl_table) ? 0 : -ENOMEM;
1108e3ea1ca5STyler Hicks }
1109e3ea1ca5STyler Hicks #else
1110e3ea1ca5STyler Hicks static inline int apparmor_init_sysctl(void)
1111e3ea1ca5STyler Hicks {
1112e3ea1ca5STyler Hicks 	return 0;
1113e3ea1ca5STyler Hicks }
1114e3ea1ca5STyler Hicks #endif /* CONFIG_SYSCTL */
1115e3ea1ca5STyler Hicks 
1116b5e95b48SJohn Johansen static int __init apparmor_init(void)
1117b5e95b48SJohn Johansen {
1118b5e95b48SJohn Johansen 	int error;
1119b5e95b48SJohn Johansen 
1120b1d9e6b0SCasey Schaufler 	if (!apparmor_enabled || !security_module_enable("apparmor")) {
1121b5e95b48SJohn Johansen 		aa_info_message("AppArmor disabled by boot time parameter");
1122*954317feSThomas Meyer 		apparmor_enabled = false;
1123b5e95b48SJohn Johansen 		return 0;
1124b5e95b48SJohn Johansen 	}
1125b5e95b48SJohn Johansen 
112611c236b8SJohn Johansen 	error = aa_setup_dfa_engine();
112711c236b8SJohn Johansen 	if (error) {
112811c236b8SJohn Johansen 		AA_ERROR("Unable to setup dfa engine\n");
112911c236b8SJohn Johansen 		goto alloc_out;
113011c236b8SJohn Johansen 	}
113111c236b8SJohn Johansen 
1132b5e95b48SJohn Johansen 	error = aa_alloc_root_ns();
1133b5e95b48SJohn Johansen 	if (error) {
1134b5e95b48SJohn Johansen 		AA_ERROR("Unable to allocate default profile namespace\n");
1135b5e95b48SJohn Johansen 		goto alloc_out;
1136b5e95b48SJohn Johansen 	}
1137b5e95b48SJohn Johansen 
1138e3ea1ca5STyler Hicks 	error = apparmor_init_sysctl();
1139e3ea1ca5STyler Hicks 	if (error) {
1140e3ea1ca5STyler Hicks 		AA_ERROR("Unable to register sysctls\n");
1141e3ea1ca5STyler Hicks 		goto alloc_out;
1142e3ea1ca5STyler Hicks 
1143e3ea1ca5STyler Hicks 	}
1144e3ea1ca5STyler Hicks 
1145d4669f0bSJohn Johansen 	error = alloc_buffers();
1146d4669f0bSJohn Johansen 	if (error) {
1147d4669f0bSJohn Johansen 		AA_ERROR("Unable to allocate work buffers\n");
1148d4669f0bSJohn Johansen 		goto buffers_out;
1149d4669f0bSJohn Johansen 	}
1150d4669f0bSJohn Johansen 
115155a26ebfSJohn Johansen 	error = set_init_ctx();
1152b5e95b48SJohn Johansen 	if (error) {
1153b5e95b48SJohn Johansen 		AA_ERROR("Failed to set context on init task\n");
1154b1d9e6b0SCasey Schaufler 		aa_free_root_ns();
1155d4669f0bSJohn Johansen 		goto buffers_out;
1156b5e95b48SJohn Johansen 	}
1157d69dece5SCasey Schaufler 	security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1158d69dece5SCasey Schaufler 				"apparmor");
1159b5e95b48SJohn Johansen 
1160b5e95b48SJohn Johansen 	/* Report that AppArmor successfully initialized */
1161b5e95b48SJohn Johansen 	apparmor_initialized = 1;
1162b5e95b48SJohn Johansen 	if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1163b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized: complain mode enabled");
1164b5e95b48SJohn Johansen 	else if (aa_g_profile_mode == APPARMOR_KILL)
1165b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized: kill mode enabled");
1166b5e95b48SJohn Johansen 	else
1167b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized");
1168b5e95b48SJohn Johansen 
1169b5e95b48SJohn Johansen 	return error;
1170b5e95b48SJohn Johansen 
1171d4669f0bSJohn Johansen buffers_out:
1172d4669f0bSJohn Johansen 	destroy_buffers();
1173d4669f0bSJohn Johansen 
1174b5e95b48SJohn Johansen alloc_out:
1175b5e95b48SJohn Johansen 	aa_destroy_aafs();
117611c236b8SJohn Johansen 	aa_teardown_dfa_engine();
1177b5e95b48SJohn Johansen 
1178*954317feSThomas Meyer 	apparmor_enabled = false;
1179b5e95b48SJohn Johansen 	return error;
1180b5e95b48SJohn Johansen }
1181b5e95b48SJohn Johansen 
1182b5e95b48SJohn Johansen security_initcall(apparmor_init);
1183