xref: /openbmc/linux/security/apparmor/lsm.c (revision e67fe633)
1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b5e95b48SJohn Johansen /*
3b5e95b48SJohn Johansen  * AppArmor security module
4b5e95b48SJohn Johansen  *
5b5e95b48SJohn Johansen  * This file contains AppArmor LSM hooks.
6b5e95b48SJohn Johansen  *
7b5e95b48SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
8b5e95b48SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
9b5e95b48SJohn Johansen  */
10b5e95b48SJohn Johansen 
113c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h>
12b5e95b48SJohn Johansen #include <linux/moduleparam.h>
13b5e95b48SJohn Johansen #include <linux/mm.h>
14b5e95b48SJohn Johansen #include <linux/mman.h>
15b5e95b48SJohn Johansen #include <linux/mount.h>
16b5e95b48SJohn Johansen #include <linux/namei.h>
17b5e95b48SJohn Johansen #include <linux/ptrace.h>
18b5e95b48SJohn Johansen #include <linux/ctype.h>
19b5e95b48SJohn Johansen #include <linux/sysctl.h>
20b5e95b48SJohn Johansen #include <linux/audit.h>
213486740aSSerge E. Hallyn #include <linux/user_namespace.h>
22ab9f2115SMatthew Garrett #include <linux/netfilter_ipv4.h>
23ab9f2115SMatthew Garrett #include <linux/netfilter_ipv6.h>
24f4d6b94bSJon Tourville #include <linux/zstd.h>
25b5e95b48SJohn Johansen #include <net/sock.h>
26e262e32dSDavid Howells #include <uapi/linux/mount.h>
27b5e95b48SJohn Johansen 
28b5e95b48SJohn Johansen #include "include/apparmor.h"
29b5e95b48SJohn Johansen #include "include/apparmorfs.h"
30b5e95b48SJohn Johansen #include "include/audit.h"
31b5e95b48SJohn Johansen #include "include/capability.h"
32d8889d49SJohn Johansen #include "include/cred.h"
33b5e95b48SJohn Johansen #include "include/file.h"
34b5e95b48SJohn Johansen #include "include/ipc.h"
3556974a6fSJohn Johansen #include "include/net.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"
42c0929212SJohn Johansen #include "include/secid.h"
43b5e95b48SJohn Johansen 
44b5e95b48SJohn Johansen /* Flag indicating whether initialization completed */
45545de8feSJohn Johansen int apparmor_initialized;
46b5e95b48SJohn Johansen 
47df323337SSebastian Andrzej Siewior union aa_buffer {
48df323337SSebastian Andrzej Siewior 	struct list_head list;
49df323337SSebastian Andrzej Siewior 	char buffer[1];
50df323337SSebastian Andrzej Siewior };
51d4669f0bSJohn Johansen 
52341c1fdaSJohn Johansen #define RESERVE_COUNT 2
53341c1fdaSJohn Johansen static int reserve_count = RESERVE_COUNT;
54341c1fdaSJohn Johansen static int buffer_count;
55341c1fdaSJohn Johansen 
56df323337SSebastian Andrzej Siewior static LIST_HEAD(aa_global_buffers);
57df323337SSebastian Andrzej Siewior static DEFINE_SPINLOCK(aa_buffers_lock);
58d4669f0bSJohn Johansen 
59b5e95b48SJohn Johansen /*
60b5e95b48SJohn Johansen  * LSM hook functions
61b5e95b48SJohn Johansen  */
62b5e95b48SJohn Johansen 
63b5e95b48SJohn Johansen /*
64d9087c49SJohn Johansen  * put the associated labels
65b5e95b48SJohn Johansen  */
66b5e95b48SJohn Johansen static void apparmor_cred_free(struct cred *cred)
67b5e95b48SJohn Johansen {
68d9087c49SJohn Johansen 	aa_put_label(cred_label(cred));
6969b5a44aSCasey Schaufler 	set_cred_label(cred, NULL);
70b5e95b48SJohn Johansen }
71b5e95b48SJohn Johansen 
72b5e95b48SJohn Johansen /*
73b5e95b48SJohn Johansen  * allocate the apparmor part of blank credentials
74b5e95b48SJohn Johansen  */
75b5e95b48SJohn Johansen static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
76b5e95b48SJohn Johansen {
7769b5a44aSCasey Schaufler 	set_cred_label(cred, NULL);
78b5e95b48SJohn Johansen 	return 0;
79b5e95b48SJohn Johansen }
80b5e95b48SJohn Johansen 
81b5e95b48SJohn Johansen /*
82d9087c49SJohn Johansen  * prepare new cred label for modification by prepare_cred block
83b5e95b48SJohn Johansen  */
84b5e95b48SJohn Johansen static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
85b5e95b48SJohn Johansen 				 gfp_t gfp)
86b5e95b48SJohn Johansen {
8769b5a44aSCasey Schaufler 	set_cred_label(new, aa_get_newest_label(cred_label(old)));
88b5e95b48SJohn Johansen 	return 0;
89b5e95b48SJohn Johansen }
90b5e95b48SJohn Johansen 
91b5e95b48SJohn Johansen /*
92b5e95b48SJohn Johansen  * transfer the apparmor data to a blank set of creds
93b5e95b48SJohn Johansen  */
94b5e95b48SJohn Johansen static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
95b5e95b48SJohn Johansen {
9669b5a44aSCasey Schaufler 	set_cred_label(new, aa_get_newest_label(cred_label(old)));
97b5e95b48SJohn Johansen }
98b5e95b48SJohn Johansen 
993b529a76SJohn Johansen static void apparmor_task_free(struct task_struct *task)
1003b529a76SJohn Johansen {
1013b529a76SJohn Johansen 
1023b529a76SJohn Johansen 	aa_free_task_ctx(task_ctx(task));
1033b529a76SJohn Johansen }
1043b529a76SJohn Johansen 
1053b529a76SJohn Johansen static int apparmor_task_alloc(struct task_struct *task,
1063b529a76SJohn Johansen 			       unsigned long clone_flags)
1073b529a76SJohn Johansen {
108f4ad8f2cSCasey Schaufler 	struct aa_task_ctx *new = task_ctx(task);
1093b529a76SJohn Johansen 
110de62de59SJohn Johansen 	aa_dup_task_ctx(new, task_ctx(current));
1113b529a76SJohn Johansen 
1123b529a76SJohn Johansen 	return 0;
113b5e95b48SJohn Johansen }
114b5e95b48SJohn Johansen 
115b5e95b48SJohn Johansen static int apparmor_ptrace_access_check(struct task_struct *child,
116b5e95b48SJohn Johansen 					unsigned int mode)
117b5e95b48SJohn Johansen {
118b2d09ae4SJohn Johansen 	struct aa_label *tracer, *tracee;
119b2d09ae4SJohn Johansen 	int error;
120b2d09ae4SJohn Johansen 
1211f8266ffSJann Horn 	tracer = __begin_current_label_crit_section();
122b2d09ae4SJohn Johansen 	tracee = aa_get_task_label(child);
123b2d09ae4SJohn Johansen 	error = aa_may_ptrace(tracer, tracee,
124338d0be4SJohn Johansen 			(mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
125338d0be4SJohn Johansen 						  : AA_PTRACE_TRACE);
126b2d09ae4SJohn Johansen 	aa_put_label(tracee);
1271f8266ffSJann Horn 	__end_current_label_crit_section(tracer);
128b2d09ae4SJohn Johansen 
129b2d09ae4SJohn Johansen 	return error;
130b5e95b48SJohn Johansen }
131b5e95b48SJohn Johansen 
132b5e95b48SJohn Johansen static int apparmor_ptrace_traceme(struct task_struct *parent)
133b5e95b48SJohn Johansen {
134b2d09ae4SJohn Johansen 	struct aa_label *tracer, *tracee;
135b2d09ae4SJohn Johansen 	int error;
136b2d09ae4SJohn Johansen 
137ca3fde52SJann Horn 	tracee = __begin_current_label_crit_section();
138b2d09ae4SJohn Johansen 	tracer = aa_get_task_label(parent);
139b2d09ae4SJohn Johansen 	error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
140b2d09ae4SJohn Johansen 	aa_put_label(tracer);
141ca3fde52SJann Horn 	__end_current_label_crit_section(tracee);
142b2d09ae4SJohn Johansen 
143b2d09ae4SJohn Johansen 	return error;
144b5e95b48SJohn Johansen }
145b5e95b48SJohn Johansen 
146b5e95b48SJohn Johansen /* Derived from security/commoncap.c:cap_capget */
147b5e95b48SJohn Johansen static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
148b5e95b48SJohn Johansen 			   kernel_cap_t *inheritable, kernel_cap_t *permitted)
149b5e95b48SJohn Johansen {
150637f688dSJohn Johansen 	struct aa_label *label;
151b5e95b48SJohn Johansen 	const struct cred *cred;
152b5e95b48SJohn Johansen 
153b5e95b48SJohn Johansen 	rcu_read_lock();
154b5e95b48SJohn Johansen 	cred = __task_cred(target);
155637f688dSJohn Johansen 	label = aa_get_newest_cred_label(cred);
156c70c86c4SJohn Johansen 
157b1d9e6b0SCasey Schaufler 	/*
158b1d9e6b0SCasey Schaufler 	 * cap_capget is stacked ahead of this and will
159b1d9e6b0SCasey Schaufler 	 * initialize effective and permitted.
160b1d9e6b0SCasey Schaufler 	 */
161c70c86c4SJohn Johansen 	if (!unconfined(label)) {
162c70c86c4SJohn Johansen 		struct aa_profile *profile;
163c70c86c4SJohn Johansen 		struct label_it i;
164c70c86c4SJohn Johansen 
165c70c86c4SJohn Johansen 		label_for_each_confined(i, label, profile) {
1661ad22fccSJohn Johansen 			struct aa_ruleset *rules;
167c70c86c4SJohn Johansen 			if (COMPLAIN_MODE(profile))
168c70c86c4SJohn Johansen 				continue;
1691ad22fccSJohn Johansen 			rules = list_first_entry(&profile->rules,
1701ad22fccSJohn Johansen 						 typeof(*rules), list);
171c70c86c4SJohn Johansen 			*effective = cap_intersect(*effective,
1721ad22fccSJohn Johansen 						   rules->caps.allow);
173c70c86c4SJohn Johansen 			*permitted = cap_intersect(*permitted,
1741ad22fccSJohn Johansen 						   rules->caps.allow);
175c70c86c4SJohn Johansen 		}
176b5e95b48SJohn Johansen 	}
177b5e95b48SJohn Johansen 	rcu_read_unlock();
178637f688dSJohn Johansen 	aa_put_label(label);
179b5e95b48SJohn Johansen 
180b5e95b48SJohn Johansen 	return 0;
181b5e95b48SJohn Johansen }
182b5e95b48SJohn Johansen 
1836a9de491SEric Paris static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
184c1a85a00SMicah Morton 			    int cap, unsigned int opts)
185b5e95b48SJohn Johansen {
186637f688dSJohn Johansen 	struct aa_label *label;
187b1d9e6b0SCasey Schaufler 	int error = 0;
188b1d9e6b0SCasey Schaufler 
189637f688dSJohn Johansen 	label = aa_get_newest_cred_label(cred);
190637f688dSJohn Johansen 	if (!unconfined(label))
191c1a85a00SMicah Morton 		error = aa_capable(label, cap, opts);
192637f688dSJohn Johansen 	aa_put_label(label);
193cf797c0eSJohn Johansen 
194b5e95b48SJohn Johansen 	return error;
195b5e95b48SJohn Johansen }
196b5e95b48SJohn Johansen 
197b5e95b48SJohn Johansen /**
198b5e95b48SJohn Johansen  * common_perm - basic common permission check wrapper fn for paths
199b5e95b48SJohn Johansen  * @op: operation being checked
200b5e95b48SJohn Johansen  * @path: path to check permission of  (NOT NULL)
201b5e95b48SJohn Johansen  * @mask: requested permissions mask
202b5e95b48SJohn Johansen  * @cond: conditional info for the permission request  (NOT NULL)
203b5e95b48SJohn Johansen  *
204b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
205b5e95b48SJohn Johansen  */
20647f6e5ccSJohn Johansen static int common_perm(const char *op, const struct path *path, u32 mask,
207b5e95b48SJohn Johansen 		       struct path_cond *cond)
208b5e95b48SJohn Johansen {
209637f688dSJohn Johansen 	struct aa_label *label;
210b5e95b48SJohn Johansen 	int error = 0;
211b5e95b48SJohn Johansen 
212637f688dSJohn Johansen 	label = __begin_current_label_crit_section();
213637f688dSJohn Johansen 	if (!unconfined(label))
214aebd873eSJohn Johansen 		error = aa_path_perm(op, label, path, 0, mask, cond);
215637f688dSJohn Johansen 	__end_current_label_crit_section(label);
216b5e95b48SJohn Johansen 
217b5e95b48SJohn Johansen 	return error;
218b5e95b48SJohn Johansen }
219b5e95b48SJohn Johansen 
220b5e95b48SJohn Johansen /**
22131f75bfeSJohn Johansen  * common_perm_cond - common permission wrapper around inode cond
22231f75bfeSJohn Johansen  * @op: operation being checked
22331f75bfeSJohn Johansen  * @path: location to check (NOT NULL)
22431f75bfeSJohn Johansen  * @mask: requested permissions mask
22531f75bfeSJohn Johansen  *
22631f75bfeSJohn Johansen  * Returns: %0 else error code if error or permission denied
22731f75bfeSJohn Johansen  */
22831f75bfeSJohn Johansen static int common_perm_cond(const char *op, const struct path *path, u32 mask)
22931f75bfeSJohn Johansen {
230*e67fe633SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
2315e26a01eSChristian Brauner 					    d_backing_inode(path->dentry));
2323cee6079SChristian Brauner 	struct path_cond cond = {
2335e26a01eSChristian Brauner 		vfsuid_into_kuid(vfsuid),
23431f75bfeSJohn Johansen 		d_backing_inode(path->dentry)->i_mode
23531f75bfeSJohn Johansen 	};
23631f75bfeSJohn Johansen 
23731f75bfeSJohn Johansen 	if (!path_mediated_fs(path->dentry))
23831f75bfeSJohn Johansen 		return 0;
23931f75bfeSJohn Johansen 
24031f75bfeSJohn Johansen 	return common_perm(op, path, mask, &cond);
24131f75bfeSJohn Johansen }
24231f75bfeSJohn Johansen 
24331f75bfeSJohn Johansen /**
244b5e95b48SJohn Johansen  * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
245b5e95b48SJohn Johansen  * @op: operation being checked
246b5e95b48SJohn Johansen  * @dir: directory of the dentry  (NOT NULL)
247b5e95b48SJohn Johansen  * @dentry: dentry to check  (NOT NULL)
248b5e95b48SJohn Johansen  * @mask: requested permissions mask
249b5e95b48SJohn Johansen  * @cond: conditional info for the permission request  (NOT NULL)
250b5e95b48SJohn Johansen  *
251b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
252b5e95b48SJohn Johansen  */
25347f6e5ccSJohn Johansen static int common_perm_dir_dentry(const char *op, const struct path *dir,
254b5e95b48SJohn Johansen 				  struct dentry *dentry, u32 mask,
255b5e95b48SJohn Johansen 				  struct path_cond *cond)
256b5e95b48SJohn Johansen {
2578486adf0SKees Cook 	struct path path = { .mnt = dir->mnt, .dentry = dentry };
258b5e95b48SJohn Johansen 
259b5e95b48SJohn Johansen 	return common_perm(op, &path, mask, cond);
260b5e95b48SJohn Johansen }
261b5e95b48SJohn Johansen 
262b5e95b48SJohn Johansen /**
263b5e95b48SJohn Johansen  * common_perm_rm - common permission wrapper for operations doing rm
264b5e95b48SJohn Johansen  * @op: operation being checked
265b5e95b48SJohn Johansen  * @dir: directory that the dentry is in  (NOT NULL)
266b5e95b48SJohn Johansen  * @dentry: dentry being rm'd  (NOT NULL)
267b5e95b48SJohn Johansen  * @mask: requested permission mask
268b5e95b48SJohn Johansen  *
269b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
270b5e95b48SJohn Johansen  */
27147f6e5ccSJohn Johansen static int common_perm_rm(const char *op, const struct path *dir,
272b5e95b48SJohn Johansen 			  struct dentry *dentry, u32 mask)
273b5e95b48SJohn Johansen {
274c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
275b5e95b48SJohn Johansen 	struct path_cond cond = { };
2765e26a01eSChristian Brauner 	vfsuid_t vfsuid;
277b5e95b48SJohn Johansen 
278efeee83aSJohn Johansen 	if (!inode || !path_mediated_fs(dentry))
279b5e95b48SJohn Johansen 		return 0;
280b5e95b48SJohn Johansen 
281*e67fe633SChristian Brauner 	vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
2825e26a01eSChristian Brauner 	cond.uid = vfsuid_into_kuid(vfsuid);
283b5e95b48SJohn Johansen 	cond.mode = inode->i_mode;
284b5e95b48SJohn Johansen 
285b5e95b48SJohn Johansen 	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
286b5e95b48SJohn Johansen }
287b5e95b48SJohn Johansen 
288b5e95b48SJohn Johansen /**
289b5e95b48SJohn Johansen  * common_perm_create - common permission wrapper for operations doing create
290b5e95b48SJohn Johansen  * @op: operation being checked
291b5e95b48SJohn Johansen  * @dir: directory that dentry will be created in  (NOT NULL)
292b5e95b48SJohn Johansen  * @dentry: dentry to create   (NOT NULL)
293b5e95b48SJohn Johansen  * @mask: request permission mask
294b5e95b48SJohn Johansen  * @mode: created file mode
295b5e95b48SJohn Johansen  *
296b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
297b5e95b48SJohn Johansen  */
29847f6e5ccSJohn Johansen static int common_perm_create(const char *op, const struct path *dir,
299d6b49f7aSAl Viro 			      struct dentry *dentry, u32 mask, umode_t mode)
300b5e95b48SJohn Johansen {
301b5e95b48SJohn Johansen 	struct path_cond cond = { current_fsuid(), mode };
302b5e95b48SJohn Johansen 
303efeee83aSJohn Johansen 	if (!path_mediated_fs(dir->dentry))
304b5e95b48SJohn Johansen 		return 0;
305b5e95b48SJohn Johansen 
306b5e95b48SJohn Johansen 	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
307b5e95b48SJohn Johansen }
308b5e95b48SJohn Johansen 
309989f74e0SAl Viro static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
310b5e95b48SJohn Johansen {
311b5e95b48SJohn Johansen 	return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
312b5e95b48SJohn Johansen }
313b5e95b48SJohn Johansen 
314d3607752SAl Viro static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
3154572befeSAl Viro 			       umode_t mode)
316b5e95b48SJohn Johansen {
317b5e95b48SJohn Johansen 	return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
318b5e95b48SJohn Johansen 				  S_IFDIR);
319b5e95b48SJohn Johansen }
320b5e95b48SJohn Johansen 
321989f74e0SAl Viro static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
322b5e95b48SJohn Johansen {
323b5e95b48SJohn Johansen 	return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
324b5e95b48SJohn Johansen }
325b5e95b48SJohn Johansen 
326d3607752SAl Viro static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
32704fc66e7SAl Viro 			       umode_t mode, unsigned int dev)
328b5e95b48SJohn Johansen {
329b5e95b48SJohn Johansen 	return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
330b5e95b48SJohn Johansen }
331b5e95b48SJohn Johansen 
33281f4c506SAl Viro static int apparmor_path_truncate(const struct path *path)
333b5e95b48SJohn Johansen {
334e53cfe6cSJohn Johansen 	return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
335b5e95b48SJohn Johansen }
336b5e95b48SJohn Johansen 
3373350607dSGünther Noack static int apparmor_file_truncate(struct file *file)
3383350607dSGünther Noack {
3393350607dSGünther Noack 	return apparmor_path_truncate(&file->f_path);
3403350607dSGünther Noack }
3413350607dSGünther Noack 
342d3607752SAl Viro static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
343b5e95b48SJohn Johansen 				 const char *old_name)
344b5e95b48SJohn Johansen {
345b5e95b48SJohn Johansen 	return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
346b5e95b48SJohn Johansen 				  S_IFLNK);
347b5e95b48SJohn Johansen }
348b5e95b48SJohn Johansen 
3493ccee46aSAl Viro static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
350b5e95b48SJohn Johansen 			      struct dentry *new_dentry)
351b5e95b48SJohn Johansen {
352637f688dSJohn Johansen 	struct aa_label *label;
353b5e95b48SJohn Johansen 	int error = 0;
354b5e95b48SJohn Johansen 
355efeee83aSJohn Johansen 	if (!path_mediated_fs(old_dentry))
356b5e95b48SJohn Johansen 		return 0;
357b5e95b48SJohn Johansen 
358637f688dSJohn Johansen 	label = begin_current_label_crit_section();
359637f688dSJohn Johansen 	if (!unconfined(label))
3608014370fSJohn Johansen 		error = aa_path_link(label, old_dentry, new_dir, new_dentry);
361637f688dSJohn Johansen 	end_current_label_crit_section(label);
362cf797c0eSJohn Johansen 
363b5e95b48SJohn Johansen 	return error;
364b5e95b48SJohn Johansen }
365b5e95b48SJohn Johansen 
3663ccee46aSAl Viro static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
367100f59d9SMickaël Salaün 				const struct path *new_dir, struct dentry *new_dentry,
368100f59d9SMickaël Salaün 				const unsigned int flags)
369b5e95b48SJohn Johansen {
370637f688dSJohn Johansen 	struct aa_label *label;
371b5e95b48SJohn Johansen 	int error = 0;
372b5e95b48SJohn Johansen 
373efeee83aSJohn Johansen 	if (!path_mediated_fs(old_dentry))
374b5e95b48SJohn Johansen 		return 0;
375100f59d9SMickaël Salaün 	if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
376100f59d9SMickaël Salaün 		return 0;
377b5e95b48SJohn Johansen 
378637f688dSJohn Johansen 	label = begin_current_label_crit_section();
379637f688dSJohn Johansen 	if (!unconfined(label)) {
380*e67fe633SChristian Brauner 		struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
3815e26a01eSChristian Brauner 		vfsuid_t vfsuid;
3828486adf0SKees Cook 		struct path old_path = { .mnt = old_dir->mnt,
3838486adf0SKees Cook 					 .dentry = old_dentry };
3848486adf0SKees Cook 		struct path new_path = { .mnt = new_dir->mnt,
3858486adf0SKees Cook 					 .dentry = new_dentry };
3863cee6079SChristian Brauner 		struct path_cond cond = {
3875e26a01eSChristian Brauner 			.mode = d_backing_inode(old_dentry)->i_mode
388b5e95b48SJohn Johansen 		};
389*e67fe633SChristian Brauner 		vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
3905e26a01eSChristian Brauner 		cond.uid = vfsuid_into_kuid(vfsuid);
391b5e95b48SJohn Johansen 
392100f59d9SMickaël Salaün 		if (flags & RENAME_EXCHANGE) {
393100f59d9SMickaël Salaün 			struct path_cond cond_exchange = {
3945e26a01eSChristian Brauner 				.mode = d_backing_inode(new_dentry)->i_mode,
395100f59d9SMickaël Salaün 			};
396*e67fe633SChristian Brauner 			vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
3975e26a01eSChristian Brauner 			cond_exchange.uid = vfsuid_into_kuid(vfsuid);
398100f59d9SMickaël Salaün 
399100f59d9SMickaël Salaün 			error = aa_path_perm(OP_RENAME_SRC, label, &new_path, 0,
400100f59d9SMickaël Salaün 					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
401100f59d9SMickaël Salaün 					     AA_MAY_SETATTR | AA_MAY_DELETE,
402100f59d9SMickaël Salaün 					     &cond_exchange);
403100f59d9SMickaël Salaün 			if (!error)
404100f59d9SMickaël Salaün 				error = aa_path_perm(OP_RENAME_DEST, label, &old_path,
405100f59d9SMickaël Salaün 						     0, MAY_WRITE | AA_MAY_SETATTR |
406100f59d9SMickaël Salaün 						     AA_MAY_CREATE, &cond_exchange);
407100f59d9SMickaël Salaün 		}
408100f59d9SMickaël Salaün 
409100f59d9SMickaël Salaün 		if (!error)
410aebd873eSJohn Johansen 			error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
411e53cfe6cSJohn Johansen 					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
412e53cfe6cSJohn Johansen 					     AA_MAY_SETATTR | AA_MAY_DELETE,
413b5e95b48SJohn Johansen 					     &cond);
414b5e95b48SJohn Johansen 		if (!error)
415aebd873eSJohn Johansen 			error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
416e53cfe6cSJohn Johansen 					     0, MAY_WRITE | AA_MAY_SETATTR |
417b5e95b48SJohn Johansen 					     AA_MAY_CREATE, &cond);
418b5e95b48SJohn Johansen 
419b5e95b48SJohn Johansen 	}
420637f688dSJohn Johansen 	end_current_label_crit_section(label);
421cf797c0eSJohn Johansen 
422b5e95b48SJohn Johansen 	return error;
423b5e95b48SJohn Johansen }
424b5e95b48SJohn Johansen 
425be01f9f2SAl Viro static int apparmor_path_chmod(const struct path *path, umode_t mode)
426b5e95b48SJohn Johansen {
42731f75bfeSJohn Johansen 	return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
428b5e95b48SJohn Johansen }
429b5e95b48SJohn Johansen 
4307fd25dacSAl Viro static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
431b5e95b48SJohn Johansen {
43231f75bfeSJohn Johansen 	return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
433b5e95b48SJohn Johansen }
434b5e95b48SJohn Johansen 
4353f7036a0SAl Viro static int apparmor_inode_getattr(const struct path *path)
436b5e95b48SJohn Johansen {
437e53cfe6cSJohn Johansen 	return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
438b5e95b48SJohn Johansen }
439b5e95b48SJohn Johansen 
44094817692SAl Viro static int apparmor_file_open(struct file *file)
441b5e95b48SJohn Johansen {
442637f688dSJohn Johansen 	struct aa_file_ctx *fctx = file_ctx(file);
443637f688dSJohn Johansen 	struct aa_label *label;
444b5e95b48SJohn Johansen 	int error = 0;
445b5e95b48SJohn Johansen 
446efeee83aSJohn Johansen 	if (!path_mediated_fs(file->f_path.dentry))
447b5e95b48SJohn Johansen 		return 0;
448b5e95b48SJohn Johansen 
449b5e95b48SJohn Johansen 	/* If in exec, permission is handled by bprm hooks.
450b5e95b48SJohn Johansen 	 * Cache permissions granted by the previous exec check, with
451b5e95b48SJohn Johansen 	 * implicit read and executable mmap which are required to
452b5e95b48SJohn Johansen 	 * actually execute the image.
453b5e95b48SJohn Johansen 	 */
454b5e95b48SJohn Johansen 	if (current->in_execve) {
45555a26ebfSJohn Johansen 		fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
456b5e95b48SJohn Johansen 		return 0;
457b5e95b48SJohn Johansen 	}
458b5e95b48SJohn Johansen 
45994817692SAl Viro 	label = aa_get_newest_cred_label(file->f_cred);
460637f688dSJohn Johansen 	if (!unconfined(label)) {
461*e67fe633SChristian Brauner 		struct mnt_idmap *idmap = file_mnt_idmap(file);
462496ad9aaSAl Viro 		struct inode *inode = file_inode(file);
4635e26a01eSChristian Brauner 		vfsuid_t vfsuid;
4643cee6079SChristian Brauner 		struct path_cond cond = {
4655e26a01eSChristian Brauner 			.mode = inode->i_mode,
4663cee6079SChristian Brauner 		};
467*e67fe633SChristian Brauner 		vfsuid = i_uid_into_vfsuid(idmap, inode);
4685e26a01eSChristian Brauner 		cond.uid = vfsuid_into_kuid(vfsuid);
469b5e95b48SJohn Johansen 
470aebd873eSJohn Johansen 		error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
471b5e95b48SJohn Johansen 				     aa_map_file_to_perms(file), &cond);
472b5e95b48SJohn Johansen 		/* todo cache full allowed permissions set and state */
47355a26ebfSJohn Johansen 		fctx->allow = aa_map_file_to_perms(file);
474b5e95b48SJohn Johansen 	}
475637f688dSJohn Johansen 	aa_put_label(label);
476b5e95b48SJohn Johansen 
477b5e95b48SJohn Johansen 	return error;
478b5e95b48SJohn Johansen }
479b5e95b48SJohn Johansen 
480b5e95b48SJohn Johansen static int apparmor_file_alloc_security(struct file *file)
481b5e95b48SJohn Johansen {
48233bf60caSCasey Schaufler 	struct aa_file_ctx *ctx = file_ctx(file);
483637f688dSJohn Johansen 	struct aa_label *label = begin_current_label_crit_section();
484b5e95b48SJohn Johansen 
48533bf60caSCasey Schaufler 	spin_lock_init(&ctx->lock);
48633bf60caSCasey Schaufler 	rcu_assign_pointer(ctx->label, aa_get_label(label));
48733bf60caSCasey Schaufler 	end_current_label_crit_section(label);
48833bf60caSCasey Schaufler 	return 0;
489b5e95b48SJohn Johansen }
490b5e95b48SJohn Johansen 
491b5e95b48SJohn Johansen static void apparmor_file_free_security(struct file *file)
492b5e95b48SJohn Johansen {
49333bf60caSCasey Schaufler 	struct aa_file_ctx *ctx = file_ctx(file);
49433bf60caSCasey Schaufler 
49533bf60caSCasey Schaufler 	if (ctx)
49633bf60caSCasey Schaufler 		aa_put_label(rcu_access_pointer(ctx->label));
497b5e95b48SJohn Johansen }
498b5e95b48SJohn Johansen 
499341c1fdaSJohn Johansen static int common_file_perm(const char *op, struct file *file, u32 mask,
500341c1fdaSJohn Johansen 			    bool in_atomic)
501b5e95b48SJohn Johansen {
502190a9518SJohn Johansen 	struct aa_label *label;
503b5e95b48SJohn Johansen 	int error = 0;
504b5e95b48SJohn Johansen 
505192ca6b5SJohn Johansen 	/* don't reaudit files closed during inheritance */
506192ca6b5SJohn Johansen 	if (file->f_path.dentry == aa_null.dentry)
507192ca6b5SJohn Johansen 		return -EACCES;
508192ca6b5SJohn Johansen 
509637f688dSJohn Johansen 	label = __begin_current_label_crit_section();
510341c1fdaSJohn Johansen 	error = aa_file_perm(op, label, file, mask, in_atomic);
511637f688dSJohn Johansen 	__end_current_label_crit_section(label);
512b5e95b48SJohn Johansen 
513b5e95b48SJohn Johansen 	return error;
514b5e95b48SJohn Johansen }
515b5e95b48SJohn Johansen 
516064dc947SJohn Johansen static int apparmor_file_receive(struct file *file)
517064dc947SJohn Johansen {
518341c1fdaSJohn Johansen 	return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
519341c1fdaSJohn Johansen 				false);
520064dc947SJohn Johansen }
521064dc947SJohn Johansen 
522b5e95b48SJohn Johansen static int apparmor_file_permission(struct file *file, int mask)
523b5e95b48SJohn Johansen {
524341c1fdaSJohn Johansen 	return common_file_perm(OP_FPERM, file, mask, false);
525b5e95b48SJohn Johansen }
526b5e95b48SJohn Johansen 
527b5e95b48SJohn Johansen static int apparmor_file_lock(struct file *file, unsigned int cmd)
528b5e95b48SJohn Johansen {
529b5e95b48SJohn Johansen 	u32 mask = AA_MAY_LOCK;
530b5e95b48SJohn Johansen 
531b5e95b48SJohn Johansen 	if (cmd == F_WRLCK)
532b5e95b48SJohn Johansen 		mask |= MAY_WRITE;
533b5e95b48SJohn Johansen 
534341c1fdaSJohn Johansen 	return common_file_perm(OP_FLOCK, file, mask, false);
535b5e95b48SJohn Johansen }
536b5e95b48SJohn Johansen 
53747f6e5ccSJohn Johansen static int common_mmap(const char *op, struct file *file, unsigned long prot,
538341c1fdaSJohn Johansen 		       unsigned long flags, bool in_atomic)
539b5e95b48SJohn Johansen {
540b5e95b48SJohn Johansen 	int mask = 0;
541b5e95b48SJohn Johansen 
542637f688dSJohn Johansen 	if (!file || !file_ctx(file))
543b5e95b48SJohn Johansen 		return 0;
544b5e95b48SJohn Johansen 
545b5e95b48SJohn Johansen 	if (prot & PROT_READ)
546b5e95b48SJohn Johansen 		mask |= MAY_READ;
547b5e95b48SJohn Johansen 	/*
548b5e95b48SJohn Johansen 	 * Private mappings don't require write perms since they don't
549b5e95b48SJohn Johansen 	 * write back to the files
550b5e95b48SJohn Johansen 	 */
551b5e95b48SJohn Johansen 	if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
552b5e95b48SJohn Johansen 		mask |= MAY_WRITE;
553b5e95b48SJohn Johansen 	if (prot & PROT_EXEC)
554b5e95b48SJohn Johansen 		mask |= AA_EXEC_MMAP;
555b5e95b48SJohn Johansen 
556341c1fdaSJohn Johansen 	return common_file_perm(op, file, mask, in_atomic);
557b5e95b48SJohn Johansen }
558b5e95b48SJohn Johansen 
559e5467859SAl Viro static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
560e5467859SAl Viro 			      unsigned long prot, unsigned long flags)
561b5e95b48SJohn Johansen {
562341c1fdaSJohn Johansen 	return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
563b5e95b48SJohn Johansen }
564b5e95b48SJohn Johansen 
565b5e95b48SJohn Johansen static int apparmor_file_mprotect(struct vm_area_struct *vma,
566b5e95b48SJohn Johansen 				  unsigned long reqprot, unsigned long prot)
567b5e95b48SJohn Johansen {
568b5e95b48SJohn Johansen 	return common_mmap(OP_FMPROT, vma->vm_file, prot,
569341c1fdaSJohn Johansen 			   !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
570341c1fdaSJohn Johansen 			   false);
571b5e95b48SJohn Johansen }
572b5e95b48SJohn Johansen 
5732ea3ffb7SJohn Johansen static int apparmor_sb_mount(const char *dev_name, const struct path *path,
5742ea3ffb7SJohn Johansen 			     const char *type, unsigned long flags, void *data)
5752ea3ffb7SJohn Johansen {
5762ea3ffb7SJohn Johansen 	struct aa_label *label;
5772ea3ffb7SJohn Johansen 	int error = 0;
5782ea3ffb7SJohn Johansen 
5792ea3ffb7SJohn Johansen 	/* Discard magic */
5802ea3ffb7SJohn Johansen 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
5812ea3ffb7SJohn Johansen 		flags &= ~MS_MGC_MSK;
5822ea3ffb7SJohn Johansen 
5832ea3ffb7SJohn Johansen 	flags &= ~AA_MS_IGNORE_MASK;
5842ea3ffb7SJohn Johansen 
5852ea3ffb7SJohn Johansen 	label = __begin_current_label_crit_section();
5862ea3ffb7SJohn Johansen 	if (!unconfined(label)) {
5872ea3ffb7SJohn Johansen 		if (flags & MS_REMOUNT)
5882ea3ffb7SJohn Johansen 			error = aa_remount(label, path, flags, data);
5892ea3ffb7SJohn Johansen 		else if (flags & MS_BIND)
5902ea3ffb7SJohn Johansen 			error = aa_bind_mount(label, path, dev_name, flags);
5912ea3ffb7SJohn Johansen 		else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
5922ea3ffb7SJohn Johansen 				  MS_UNBINDABLE))
5932ea3ffb7SJohn Johansen 			error = aa_mount_change_type(label, path, flags);
5942ea3ffb7SJohn Johansen 		else if (flags & MS_MOVE)
5952ea3ffb7SJohn Johansen 			error = aa_move_mount(label, path, dev_name);
5962ea3ffb7SJohn Johansen 		else
5972ea3ffb7SJohn Johansen 			error = aa_new_mount(label, dev_name, path, type,
5982ea3ffb7SJohn Johansen 					     flags, data);
5992ea3ffb7SJohn Johansen 	}
6002ea3ffb7SJohn Johansen 	__end_current_label_crit_section(label);
6012ea3ffb7SJohn Johansen 
6022ea3ffb7SJohn Johansen 	return error;
6032ea3ffb7SJohn Johansen }
6042ea3ffb7SJohn Johansen 
6052ea3ffb7SJohn Johansen static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
6062ea3ffb7SJohn Johansen {
6072ea3ffb7SJohn Johansen 	struct aa_label *label;
6082ea3ffb7SJohn Johansen 	int error = 0;
6092ea3ffb7SJohn Johansen 
6102ea3ffb7SJohn Johansen 	label = __begin_current_label_crit_section();
6112ea3ffb7SJohn Johansen 	if (!unconfined(label))
6122ea3ffb7SJohn Johansen 		error = aa_umount(label, mnt, flags);
6132ea3ffb7SJohn Johansen 	__end_current_label_crit_section(label);
6142ea3ffb7SJohn Johansen 
6152ea3ffb7SJohn Johansen 	return error;
6162ea3ffb7SJohn Johansen }
6172ea3ffb7SJohn Johansen 
6182ea3ffb7SJohn Johansen static int apparmor_sb_pivotroot(const struct path *old_path,
6192ea3ffb7SJohn Johansen 				 const struct path *new_path)
6202ea3ffb7SJohn Johansen {
6212ea3ffb7SJohn Johansen 	struct aa_label *label;
6222ea3ffb7SJohn Johansen 	int error = 0;
6232ea3ffb7SJohn Johansen 
6242ea3ffb7SJohn Johansen 	label = aa_get_current_label();
6252ea3ffb7SJohn Johansen 	if (!unconfined(label))
6262ea3ffb7SJohn Johansen 		error = aa_pivotroot(label, old_path, new_path);
6272ea3ffb7SJohn Johansen 	aa_put_label(label);
6282ea3ffb7SJohn Johansen 
6292ea3ffb7SJohn Johansen 	return error;
6302ea3ffb7SJohn Johansen }
6312ea3ffb7SJohn Johansen 
632c8e477c6SAl Viro static int apparmor_getprocattr(struct task_struct *task, const char *name,
633b5e95b48SJohn Johansen 				char **value)
634b5e95b48SJohn Johansen {
635b5e95b48SJohn Johansen 	int error = -ENOENT;
636b5e95b48SJohn Johansen 	/* released below */
637b5e95b48SJohn Johansen 	const struct cred *cred = get_task_cred(task);
638de62de59SJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
639637f688dSJohn Johansen 	struct aa_label *label = NULL;
640b5e95b48SJohn Johansen 
641b5e95b48SJohn Johansen 	if (strcmp(name, "current") == 0)
642d9087c49SJohn Johansen 		label = aa_get_newest_label(cred_label(cred));
64355a26ebfSJohn Johansen 	else if (strcmp(name, "prev") == 0  && ctx->previous)
644637f688dSJohn Johansen 		label = aa_get_newest_label(ctx->previous);
64555a26ebfSJohn Johansen 	else if (strcmp(name, "exec") == 0 && ctx->onexec)
646637f688dSJohn Johansen 		label = aa_get_newest_label(ctx->onexec);
647b5e95b48SJohn Johansen 	else
648b5e95b48SJohn Johansen 		error = -EINVAL;
649b5e95b48SJohn Johansen 
650637f688dSJohn Johansen 	if (label)
65176a1d263SJohn Johansen 		error = aa_getprocattr(label, value);
65277b071b3SJohn Johansen 
653637f688dSJohn Johansen 	aa_put_label(label);
654b5e95b48SJohn Johansen 	put_cred(cred);
655b5e95b48SJohn Johansen 
656b5e95b48SJohn Johansen 	return error;
657b5e95b48SJohn Johansen }
658b5e95b48SJohn Johansen 
659b21507e2SStephen Smalley static int apparmor_setprocattr(const char *name, void *value,
660b21507e2SStephen Smalley 				size_t size)
661b5e95b48SJohn Johansen {
662e89b8081SVegard Nossum 	char *command, *largs = NULL, *args = value;
663b5e95b48SJohn Johansen 	size_t arg_size;
664b5e95b48SJohn Johansen 	int error;
6658c4b785aSJohn Johansen 	DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
6668c4b785aSJohn Johansen 			  OP_SETPROCATTR);
667b5e95b48SJohn Johansen 
668b5e95b48SJohn Johansen 	if (size == 0)
669b5e95b48SJohn Johansen 		return -EINVAL;
670b5e95b48SJohn Johansen 
671e89b8081SVegard Nossum 	/* AppArmor requires that the buffer must be null terminated atm */
672e89b8081SVegard Nossum 	if (args[size - 1] != '\0') {
673e89b8081SVegard Nossum 		/* null terminate */
674e89b8081SVegard Nossum 		largs = args = kmalloc(size + 1, GFP_KERNEL);
675e89b8081SVegard Nossum 		if (!args)
676e89b8081SVegard Nossum 			return -ENOMEM;
677e89b8081SVegard Nossum 		memcpy(args, value, size);
678e89b8081SVegard Nossum 		args[size] = '\0';
679e89b8081SVegard Nossum 	}
680e89b8081SVegard Nossum 
681e89b8081SVegard Nossum 	error = -EINVAL;
682b5e95b48SJohn Johansen 	args = strim(args);
683b5e95b48SJohn Johansen 	command = strsep(&args, " ");
684b5e95b48SJohn Johansen 	if (!args)
685e89b8081SVegard Nossum 		goto out;
686b5e95b48SJohn Johansen 	args = skip_spaces(args);
687b5e95b48SJohn Johansen 	if (!*args)
688e89b8081SVegard Nossum 		goto out;
689b5e95b48SJohn Johansen 
690d4d03f74SJohn Johansen 	arg_size = size - (args - (largs ? largs : (char *) value));
691b5e95b48SJohn Johansen 	if (strcmp(name, "current") == 0) {
692b5e95b48SJohn Johansen 		if (strcmp(command, "changehat") == 0) {
693b5e95b48SJohn Johansen 			error = aa_setprocattr_changehat(args, arg_size,
694df8073c6SJohn Johansen 							 AA_CHANGE_NOFLAGS);
695b5e95b48SJohn Johansen 		} else if (strcmp(command, "permhat") == 0) {
696b5e95b48SJohn Johansen 			error = aa_setprocattr_changehat(args, arg_size,
697df8073c6SJohn Johansen 							 AA_CHANGE_TEST);
698b5e95b48SJohn Johansen 		} else if (strcmp(command, "changeprofile") == 0) {
699df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
700b5e95b48SJohn Johansen 		} else if (strcmp(command, "permprofile") == 0) {
701df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_TEST);
7026c5fc8f1SJohn Johansen 		} else if (strcmp(command, "stack") == 0) {
7036c5fc8f1SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_STACK);
7043eea57c2SJohn Johansen 		} else
7053eea57c2SJohn Johansen 			goto fail;
706b5e95b48SJohn Johansen 	} else if (strcmp(name, "exec") == 0) {
7073eea57c2SJohn Johansen 		if (strcmp(command, "exec") == 0)
708df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_ONEXEC);
7096c5fc8f1SJohn Johansen 		else if (strcmp(command, "stack") == 0)
7106c5fc8f1SJohn Johansen 			error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
7116c5fc8f1SJohn Johansen 							 AA_CHANGE_STACK));
7123eea57c2SJohn Johansen 		else
7133eea57c2SJohn Johansen 			goto fail;
7143eea57c2SJohn Johansen 	} else
715b5e95b48SJohn Johansen 		/* only support the "current" and "exec" process attributes */
716e89b8081SVegard Nossum 		goto fail;
7173eea57c2SJohn Johansen 
718b5e95b48SJohn Johansen 	if (!error)
719b5e95b48SJohn Johansen 		error = size;
720e89b8081SVegard Nossum out:
721e89b8081SVegard Nossum 	kfree(largs);
722b5e95b48SJohn Johansen 	return error;
7233eea57c2SJohn Johansen 
7243eea57c2SJohn Johansen fail:
725637f688dSJohn Johansen 	aad(&sa)->label = begin_current_label_crit_section();
726ef88a7acSJohn Johansen 	aad(&sa)->info = name;
727ef88a7acSJohn Johansen 	aad(&sa)->error = error = -EINVAL;
7283eea57c2SJohn Johansen 	aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
729637f688dSJohn Johansen 	end_current_label_crit_section(aad(&sa)->label);
730e89b8081SVegard Nossum 	goto out;
731b5e95b48SJohn Johansen }
732b5e95b48SJohn Johansen 
733fe864821SJohn Johansen /**
734fe864821SJohn Johansen  * apparmor_bprm_committing_creds - do task cleanup on committing new creds
735fe864821SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
736fe864821SJohn Johansen  */
737fe864821SJohn Johansen static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
738fe864821SJohn Johansen {
739637f688dSJohn Johansen 	struct aa_label *label = aa_current_raw_label();
740d9087c49SJohn Johansen 	struct aa_label *new_label = cred_label(bprm->cred);
741fe864821SJohn Johansen 
742fe864821SJohn Johansen 	/* bail out if unconfined or not changing profile */
743d9087c49SJohn Johansen 	if ((new_label->proxy == label->proxy) ||
744d9087c49SJohn Johansen 	    (unconfined(new_label)))
745fe864821SJohn Johansen 		return;
746fe864821SJohn Johansen 
747192ca6b5SJohn Johansen 	aa_inherit_files(bprm->cred, current->files);
748192ca6b5SJohn Johansen 
749fe864821SJohn Johansen 	current->pdeath_signal = 0;
750fe864821SJohn Johansen 
751637f688dSJohn Johansen 	/* reset soft limits and set hard limits for the new label */
752d9087c49SJohn Johansen 	__aa_transition_rlimits(label, new_label);
753fe864821SJohn Johansen }
754fe864821SJohn Johansen 
755fe864821SJohn Johansen /**
756391f1211SJiapeng Chong  * apparmor_bprm_committed_creds() - do cleanup after new creds committed
757fe864821SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
758fe864821SJohn Johansen  */
759fe864821SJohn Johansen static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
760fe864821SJohn Johansen {
7613b529a76SJohn Johansen 	/* clear out temporary/transitional state from the context */
762de62de59SJohn Johansen 	aa_clear_task_ctx_trans(task_ctx(current));
7633b529a76SJohn Johansen 
764fe864821SJohn Johansen 	return;
765fe864821SJohn Johansen }
766fe864821SJohn Johansen 
7676326948fSPaul Moore static void apparmor_current_getsecid_subj(u32 *secid)
7686326948fSPaul Moore {
7696326948fSPaul Moore 	struct aa_label *label = aa_get_current_label();
7706326948fSPaul Moore 	*secid = label->secid;
7716326948fSPaul Moore 	aa_put_label(label);
7726326948fSPaul Moore }
7736326948fSPaul Moore 
7746326948fSPaul Moore static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
775a7ae3645SJohn Johansen {
776a7ae3645SJohn Johansen 	struct aa_label *label = aa_get_task_label(p);
777a7ae3645SJohn Johansen 	*secid = label->secid;
778a7ae3645SJohn Johansen 	aa_put_label(label);
779a7ae3645SJohn Johansen }
780a7ae3645SJohn Johansen 
7817cb4dc9fSJiri Slaby static int apparmor_task_setrlimit(struct task_struct *task,
7827cb4dc9fSJiri Slaby 		unsigned int resource, struct rlimit *new_rlim)
783b5e95b48SJohn Johansen {
784637f688dSJohn Johansen 	struct aa_label *label = __begin_current_label_crit_section();
785b5e95b48SJohn Johansen 	int error = 0;
786b5e95b48SJohn Johansen 
787637f688dSJohn Johansen 	if (!unconfined(label))
78886b92cb7SJohn Johansen 		error = aa_task_setrlimit(label, task, resource, new_rlim);
789637f688dSJohn Johansen 	__end_current_label_crit_section(label);
790b5e95b48SJohn Johansen 
791b5e95b48SJohn Johansen 	return error;
792b5e95b48SJohn Johansen }
793b5e95b48SJohn Johansen 
794ae7795bcSEric W. Biederman static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
7956b4f3d01SStephen Smalley 			      int sig, const struct cred *cred)
796cd1dbf76SJohn Johansen {
797cd1dbf76SJohn Johansen 	struct aa_label *cl, *tl;
798cd1dbf76SJohn Johansen 	int error;
799cd1dbf76SJohn Johansen 
8006b4f3d01SStephen Smalley 	if (cred) {
8016b4f3d01SStephen Smalley 		/*
802cd1dbf76SJohn Johansen 		 * Dealing with USB IO specific behavior
803cd1dbf76SJohn Johansen 		 */
8046b4f3d01SStephen Smalley 		cl = aa_get_newest_cred_label(cred);
8056b4f3d01SStephen Smalley 		tl = aa_get_task_label(target);
8066b4f3d01SStephen Smalley 		error = aa_may_signal(cl, tl, sig);
8076b4f3d01SStephen Smalley 		aa_put_label(cl);
8086b4f3d01SStephen Smalley 		aa_put_label(tl);
8096b4f3d01SStephen Smalley 		return error;
8106b4f3d01SStephen Smalley 	}
8116b4f3d01SStephen Smalley 
812cd1dbf76SJohn Johansen 	cl = __begin_current_label_crit_section();
813cd1dbf76SJohn Johansen 	tl = aa_get_task_label(target);
814cd1dbf76SJohn Johansen 	error = aa_may_signal(cl, tl, sig);
815cd1dbf76SJohn Johansen 	aa_put_label(tl);
816cd1dbf76SJohn Johansen 	__end_current_label_crit_section(cl);
817cd1dbf76SJohn Johansen 
818cd1dbf76SJohn Johansen 	return error;
819cd1dbf76SJohn Johansen }
820cd1dbf76SJohn Johansen 
82156974a6fSJohn Johansen /**
82256974a6fSJohn Johansen  * apparmor_sk_alloc_security - allocate and attach the sk_security field
82356974a6fSJohn Johansen  */
82456974a6fSJohn Johansen static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
82556974a6fSJohn Johansen {
82656974a6fSJohn Johansen 	struct aa_sk_ctx *ctx;
82756974a6fSJohn Johansen 
82856974a6fSJohn Johansen 	ctx = kzalloc(sizeof(*ctx), flags);
82956974a6fSJohn Johansen 	if (!ctx)
83056974a6fSJohn Johansen 		return -ENOMEM;
83156974a6fSJohn Johansen 
83256974a6fSJohn Johansen 	SK_CTX(sk) = ctx;
83356974a6fSJohn Johansen 
83456974a6fSJohn Johansen 	return 0;
83556974a6fSJohn Johansen }
83656974a6fSJohn Johansen 
83756974a6fSJohn Johansen /**
83856974a6fSJohn Johansen  * apparmor_sk_free_security - free the sk_security field
83956974a6fSJohn Johansen  */
84056974a6fSJohn Johansen static void apparmor_sk_free_security(struct sock *sk)
84156974a6fSJohn Johansen {
84256974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = SK_CTX(sk);
84356974a6fSJohn Johansen 
84456974a6fSJohn Johansen 	SK_CTX(sk) = NULL;
84556974a6fSJohn Johansen 	aa_put_label(ctx->label);
84656974a6fSJohn Johansen 	aa_put_label(ctx->peer);
84756974a6fSJohn Johansen 	kfree(ctx);
84856974a6fSJohn Johansen }
84956974a6fSJohn Johansen 
85056974a6fSJohn Johansen /**
8510fc6ab40SYang Li  * apparmor_sk_clone_security - clone the sk_security field
85256974a6fSJohn Johansen  */
85356974a6fSJohn Johansen static void apparmor_sk_clone_security(const struct sock *sk,
85456974a6fSJohn Johansen 				       struct sock *newsk)
85556974a6fSJohn Johansen {
85656974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = SK_CTX(sk);
85756974a6fSJohn Johansen 	struct aa_sk_ctx *new = SK_CTX(newsk);
85856974a6fSJohn Johansen 
8593b646abcSMauricio Faria de Oliveira 	if (new->label)
8603b646abcSMauricio Faria de Oliveira 		aa_put_label(new->label);
86156974a6fSJohn Johansen 	new->label = aa_get_label(ctx->label);
8623b646abcSMauricio Faria de Oliveira 
8633b646abcSMauricio Faria de Oliveira 	if (new->peer)
8643b646abcSMauricio Faria de Oliveira 		aa_put_label(new->peer);
86556974a6fSJohn Johansen 	new->peer = aa_get_label(ctx->peer);
86656974a6fSJohn Johansen }
86756974a6fSJohn Johansen 
86856974a6fSJohn Johansen /**
86956974a6fSJohn Johansen  * apparmor_socket_create - check perms before creating a new socket
87056974a6fSJohn Johansen  */
87156974a6fSJohn Johansen static int apparmor_socket_create(int family, int type, int protocol, int kern)
87256974a6fSJohn Johansen {
87356974a6fSJohn Johansen 	struct aa_label *label;
87456974a6fSJohn Johansen 	int error = 0;
87556974a6fSJohn Johansen 
87656974a6fSJohn Johansen 	AA_BUG(in_interrupt());
87756974a6fSJohn Johansen 
87856974a6fSJohn Johansen 	label = begin_current_label_crit_section();
87956974a6fSJohn Johansen 	if (!(kern || unconfined(label)))
88056974a6fSJohn Johansen 		error = af_select(family,
88156974a6fSJohn Johansen 				  create_perm(label, family, type, protocol),
88256974a6fSJohn Johansen 				  aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
88356974a6fSJohn Johansen 					     family, type, protocol));
88456974a6fSJohn Johansen 	end_current_label_crit_section(label);
88556974a6fSJohn Johansen 
88656974a6fSJohn Johansen 	return error;
88756974a6fSJohn Johansen }
88856974a6fSJohn Johansen 
88956974a6fSJohn Johansen /**
89056974a6fSJohn Johansen  * apparmor_socket_post_create - setup the per-socket security struct
89156974a6fSJohn Johansen  *
89256974a6fSJohn Johansen  * Note:
89356974a6fSJohn Johansen  * -   kernel sockets currently labeled unconfined but we may want to
89456974a6fSJohn Johansen  *     move to a special kernel label
89556974a6fSJohn Johansen  * -   socket may not have sk here if created with sock_create_lite or
89656974a6fSJohn Johansen  *     sock_alloc. These should be accept cases which will be handled in
89756974a6fSJohn Johansen  *     sock_graft.
89856974a6fSJohn Johansen  */
89956974a6fSJohn Johansen static int apparmor_socket_post_create(struct socket *sock, int family,
90056974a6fSJohn Johansen 				       int type, int protocol, int kern)
90156974a6fSJohn Johansen {
90256974a6fSJohn Johansen 	struct aa_label *label;
90356974a6fSJohn Johansen 
90456974a6fSJohn Johansen 	if (kern) {
90595c0581fSJohn Johansen 		label = aa_get_label(kernel_t);
90656974a6fSJohn Johansen 	} else
90756974a6fSJohn Johansen 		label = aa_get_current_label();
90856974a6fSJohn Johansen 
90956974a6fSJohn Johansen 	if (sock->sk) {
91056974a6fSJohn Johansen 		struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
91156974a6fSJohn Johansen 
91256974a6fSJohn Johansen 		aa_put_label(ctx->label);
91356974a6fSJohn Johansen 		ctx->label = aa_get_label(label);
91456974a6fSJohn Johansen 	}
91556974a6fSJohn Johansen 	aa_put_label(label);
91656974a6fSJohn Johansen 
91756974a6fSJohn Johansen 	return 0;
91856974a6fSJohn Johansen }
91956974a6fSJohn Johansen 
92056974a6fSJohn Johansen /**
92156974a6fSJohn Johansen  * apparmor_socket_bind - check perms before bind addr to socket
92256974a6fSJohn Johansen  */
92356974a6fSJohn Johansen static int apparmor_socket_bind(struct socket *sock,
92456974a6fSJohn Johansen 				struct sockaddr *address, int addrlen)
92556974a6fSJohn Johansen {
92656974a6fSJohn Johansen 	AA_BUG(!sock);
92756974a6fSJohn Johansen 	AA_BUG(!sock->sk);
92856974a6fSJohn Johansen 	AA_BUG(!address);
92956974a6fSJohn Johansen 	AA_BUG(in_interrupt());
93056974a6fSJohn Johansen 
93156974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
93256974a6fSJohn Johansen 			 bind_perm(sock, address, addrlen),
93356974a6fSJohn Johansen 			 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
93456974a6fSJohn Johansen }
93556974a6fSJohn Johansen 
93656974a6fSJohn Johansen /**
93756974a6fSJohn Johansen  * apparmor_socket_connect - check perms before connecting @sock to @address
93856974a6fSJohn Johansen  */
93956974a6fSJohn Johansen static int apparmor_socket_connect(struct socket *sock,
94056974a6fSJohn Johansen 				   struct sockaddr *address, int addrlen)
94156974a6fSJohn Johansen {
94256974a6fSJohn Johansen 	AA_BUG(!sock);
94356974a6fSJohn Johansen 	AA_BUG(!sock->sk);
94456974a6fSJohn Johansen 	AA_BUG(!address);
94556974a6fSJohn Johansen 	AA_BUG(in_interrupt());
94656974a6fSJohn Johansen 
94756974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
94856974a6fSJohn Johansen 			 connect_perm(sock, address, addrlen),
94956974a6fSJohn Johansen 			 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
95056974a6fSJohn Johansen }
95156974a6fSJohn Johansen 
95256974a6fSJohn Johansen /**
9530fc6ab40SYang Li  * apparmor_socket_listen - check perms before allowing listen
95456974a6fSJohn Johansen  */
95556974a6fSJohn Johansen static int apparmor_socket_listen(struct socket *sock, int backlog)
95656974a6fSJohn Johansen {
95756974a6fSJohn Johansen 	AA_BUG(!sock);
95856974a6fSJohn Johansen 	AA_BUG(!sock->sk);
95956974a6fSJohn Johansen 	AA_BUG(in_interrupt());
96056974a6fSJohn Johansen 
96156974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
96256974a6fSJohn Johansen 			 listen_perm(sock, backlog),
96356974a6fSJohn Johansen 			 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
96456974a6fSJohn Johansen }
96556974a6fSJohn Johansen 
96656974a6fSJohn Johansen /**
96756974a6fSJohn Johansen  * apparmor_socket_accept - check perms before accepting a new connection.
96856974a6fSJohn Johansen  *
96956974a6fSJohn Johansen  * Note: while @newsock is created and has some information, the accept
97056974a6fSJohn Johansen  *       has not been done.
97156974a6fSJohn Johansen  */
97256974a6fSJohn Johansen static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
97356974a6fSJohn Johansen {
97456974a6fSJohn Johansen 	AA_BUG(!sock);
97556974a6fSJohn Johansen 	AA_BUG(!sock->sk);
97656974a6fSJohn Johansen 	AA_BUG(!newsock);
97756974a6fSJohn Johansen 	AA_BUG(in_interrupt());
97856974a6fSJohn Johansen 
97956974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
98056974a6fSJohn Johansen 			 accept_perm(sock, newsock),
98156974a6fSJohn Johansen 			 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
98256974a6fSJohn Johansen }
98356974a6fSJohn Johansen 
98456974a6fSJohn Johansen static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
98556974a6fSJohn Johansen 			    struct msghdr *msg, int size)
98656974a6fSJohn Johansen {
98756974a6fSJohn Johansen 	AA_BUG(!sock);
98856974a6fSJohn Johansen 	AA_BUG(!sock->sk);
98956974a6fSJohn Johansen 	AA_BUG(!msg);
99056974a6fSJohn Johansen 	AA_BUG(in_interrupt());
99156974a6fSJohn Johansen 
99256974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
99356974a6fSJohn Johansen 			 msg_perm(op, request, sock, msg, size),
99456974a6fSJohn Johansen 			 aa_sk_perm(op, request, sock->sk));
99556974a6fSJohn Johansen }
99656974a6fSJohn Johansen 
99756974a6fSJohn Johansen /**
99856974a6fSJohn Johansen  * apparmor_socket_sendmsg - check perms before sending msg to another socket
99956974a6fSJohn Johansen  */
100056974a6fSJohn Johansen static int apparmor_socket_sendmsg(struct socket *sock,
100156974a6fSJohn Johansen 				   struct msghdr *msg, int size)
100256974a6fSJohn Johansen {
100356974a6fSJohn Johansen 	return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
100456974a6fSJohn Johansen }
100556974a6fSJohn Johansen 
100656974a6fSJohn Johansen /**
100756974a6fSJohn Johansen  * apparmor_socket_recvmsg - check perms before receiving a message
100856974a6fSJohn Johansen  */
100956974a6fSJohn Johansen static int apparmor_socket_recvmsg(struct socket *sock,
101056974a6fSJohn Johansen 				   struct msghdr *msg, int size, int flags)
101156974a6fSJohn Johansen {
101256974a6fSJohn Johansen 	return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
101356974a6fSJohn Johansen }
101456974a6fSJohn Johansen 
101556974a6fSJohn Johansen /* revaliation, get/set attr, shutdown */
101656974a6fSJohn Johansen static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
101756974a6fSJohn Johansen {
101856974a6fSJohn Johansen 	AA_BUG(!sock);
101956974a6fSJohn Johansen 	AA_BUG(!sock->sk);
102056974a6fSJohn Johansen 	AA_BUG(in_interrupt());
102156974a6fSJohn Johansen 
102256974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
102356974a6fSJohn Johansen 			 sock_perm(op, request, sock),
102456974a6fSJohn Johansen 			 aa_sk_perm(op, request, sock->sk));
102556974a6fSJohn Johansen }
102656974a6fSJohn Johansen 
102756974a6fSJohn Johansen /**
102856974a6fSJohn Johansen  * apparmor_socket_getsockname - check perms before getting the local address
102956974a6fSJohn Johansen  */
103056974a6fSJohn Johansen static int apparmor_socket_getsockname(struct socket *sock)
103156974a6fSJohn Johansen {
103256974a6fSJohn Johansen 	return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
103356974a6fSJohn Johansen }
103456974a6fSJohn Johansen 
103556974a6fSJohn Johansen /**
103656974a6fSJohn Johansen  * apparmor_socket_getpeername - check perms before getting remote address
103756974a6fSJohn Johansen  */
103856974a6fSJohn Johansen static int apparmor_socket_getpeername(struct socket *sock)
103956974a6fSJohn Johansen {
104056974a6fSJohn Johansen 	return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
104156974a6fSJohn Johansen }
104256974a6fSJohn Johansen 
104356974a6fSJohn Johansen /* revaliation, get/set attr, opt */
104456974a6fSJohn Johansen static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
104556974a6fSJohn Johansen 			    int level, int optname)
104656974a6fSJohn Johansen {
104756974a6fSJohn Johansen 	AA_BUG(!sock);
104856974a6fSJohn Johansen 	AA_BUG(!sock->sk);
104956974a6fSJohn Johansen 	AA_BUG(in_interrupt());
105056974a6fSJohn Johansen 
105156974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
105256974a6fSJohn Johansen 			 opt_perm(op, request, sock, level, optname),
105356974a6fSJohn Johansen 			 aa_sk_perm(op, request, sock->sk));
105456974a6fSJohn Johansen }
105556974a6fSJohn Johansen 
105656974a6fSJohn Johansen /**
10570fc6ab40SYang Li  * apparmor_socket_getsockopt - check perms before getting socket options
105856974a6fSJohn Johansen  */
105956974a6fSJohn Johansen static int apparmor_socket_getsockopt(struct socket *sock, int level,
106056974a6fSJohn Johansen 				      int optname)
106156974a6fSJohn Johansen {
106256974a6fSJohn Johansen 	return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
106356974a6fSJohn Johansen 				level, optname);
106456974a6fSJohn Johansen }
106556974a6fSJohn Johansen 
106656974a6fSJohn Johansen /**
10670fc6ab40SYang Li  * apparmor_socket_setsockopt - check perms before setting socket options
106856974a6fSJohn Johansen  */
106956974a6fSJohn Johansen static int apparmor_socket_setsockopt(struct socket *sock, int level,
107056974a6fSJohn Johansen 				      int optname)
107156974a6fSJohn Johansen {
107256974a6fSJohn Johansen 	return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
107356974a6fSJohn Johansen 				level, optname);
107456974a6fSJohn Johansen }
107556974a6fSJohn Johansen 
107656974a6fSJohn Johansen /**
107756974a6fSJohn Johansen  * apparmor_socket_shutdown - check perms before shutting down @sock conn
107856974a6fSJohn Johansen  */
107956974a6fSJohn Johansen static int apparmor_socket_shutdown(struct socket *sock, int how)
108056974a6fSJohn Johansen {
108156974a6fSJohn Johansen 	return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
108256974a6fSJohn Johansen }
108356974a6fSJohn Johansen 
1084e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK
108556974a6fSJohn Johansen /**
10860fc6ab40SYang Li  * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
108756974a6fSJohn Johansen  *
108856974a6fSJohn Johansen  * Note: can not sleep may be called with locks held
108956974a6fSJohn Johansen  *
109056974a6fSJohn Johansen  * dont want protocol specific in __skb_recv_datagram()
109156974a6fSJohn Johansen  * to deny an incoming connection  socket_sock_rcv_skb()
109256974a6fSJohn Johansen  */
109356974a6fSJohn Johansen static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
109456974a6fSJohn Johansen {
1095ab9f2115SMatthew Garrett 	struct aa_sk_ctx *ctx = SK_CTX(sk);
1096ab9f2115SMatthew Garrett 
1097ab9f2115SMatthew Garrett 	if (!skb->secmark)
109856974a6fSJohn Johansen 		return 0;
1099ab9f2115SMatthew Garrett 
1100ab9f2115SMatthew Garrett 	return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1101ab9f2115SMatthew Garrett 				      skb->secmark, sk);
110256974a6fSJohn Johansen }
1103e1af4779SArnd Bergmann #endif
110456974a6fSJohn Johansen 
110556974a6fSJohn Johansen 
110656974a6fSJohn Johansen static struct aa_label *sk_peer_label(struct sock *sk)
110756974a6fSJohn Johansen {
110856974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = SK_CTX(sk);
110956974a6fSJohn Johansen 
111056974a6fSJohn Johansen 	if (ctx->peer)
111156974a6fSJohn Johansen 		return ctx->peer;
111256974a6fSJohn Johansen 
111356974a6fSJohn Johansen 	return ERR_PTR(-ENOPROTOOPT);
111456974a6fSJohn Johansen }
111556974a6fSJohn Johansen 
111656974a6fSJohn Johansen /**
111756974a6fSJohn Johansen  * apparmor_socket_getpeersec_stream - get security context of peer
111856974a6fSJohn Johansen  *
111956974a6fSJohn Johansen  * Note: for tcp only valid if using ipsec or cipso on lan
112056974a6fSJohn Johansen  */
112156974a6fSJohn Johansen static int apparmor_socket_getpeersec_stream(struct socket *sock,
1122b10b9c34SPaul Moore 					     sockptr_t optval, sockptr_t optlen,
112356974a6fSJohn Johansen 					     unsigned int len)
112456974a6fSJohn Johansen {
1125b10b9c34SPaul Moore 	char *name = NULL;
112656974a6fSJohn Johansen 	int slen, error = 0;
112756974a6fSJohn Johansen 	struct aa_label *label;
112856974a6fSJohn Johansen 	struct aa_label *peer;
112956974a6fSJohn Johansen 
113056974a6fSJohn Johansen 	label = begin_current_label_crit_section();
113156974a6fSJohn Johansen 	peer = sk_peer_label(sock->sk);
113256974a6fSJohn Johansen 	if (IS_ERR(peer)) {
113356974a6fSJohn Johansen 		error = PTR_ERR(peer);
113456974a6fSJohn Johansen 		goto done;
113556974a6fSJohn Johansen 	}
113656974a6fSJohn Johansen 	slen = aa_label_asxprint(&name, labels_ns(label), peer,
113756974a6fSJohn Johansen 				 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
113856974a6fSJohn Johansen 				 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
113956974a6fSJohn Johansen 	/* don't include terminating \0 in slen, it breaks some apps */
114056974a6fSJohn Johansen 	if (slen < 0) {
114156974a6fSJohn Johansen 		error = -ENOMEM;
1142b10b9c34SPaul Moore 		goto done;
1143b10b9c34SPaul Moore 	}
114456974a6fSJohn Johansen 	if (slen > len) {
114556974a6fSJohn Johansen 		error = -ERANGE;
1146b10b9c34SPaul Moore 		goto done_len;
114756974a6fSJohn Johansen 	}
114856974a6fSJohn Johansen 
1149b10b9c34SPaul Moore 	if (copy_to_sockptr(optval, name, slen))
1150b10b9c34SPaul Moore 		error = -EFAULT;
1151b10b9c34SPaul Moore done_len:
1152b10b9c34SPaul Moore 	if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
1153b10b9c34SPaul Moore 		error = -EFAULT;
115456974a6fSJohn Johansen done:
115556974a6fSJohn Johansen 	end_current_label_crit_section(label);
1156b10b9c34SPaul Moore 	kfree(name);
115756974a6fSJohn Johansen 	return error;
115856974a6fSJohn Johansen }
115956974a6fSJohn Johansen 
116056974a6fSJohn Johansen /**
116156974a6fSJohn Johansen  * apparmor_socket_getpeersec_dgram - get security label of packet
116256974a6fSJohn Johansen  * @sock: the peer socket
116356974a6fSJohn Johansen  * @skb: packet data
116456974a6fSJohn Johansen  * @secid: pointer to where to put the secid of the packet
116556974a6fSJohn Johansen  *
116656974a6fSJohn Johansen  * Sets the netlabel socket state on sk from parent
116756974a6fSJohn Johansen  */
116856974a6fSJohn Johansen static int apparmor_socket_getpeersec_dgram(struct socket *sock,
116956974a6fSJohn Johansen 					    struct sk_buff *skb, u32 *secid)
117056974a6fSJohn Johansen 
117156974a6fSJohn Johansen {
117256974a6fSJohn Johansen 	/* TODO: requires secid support */
117356974a6fSJohn Johansen 	return -ENOPROTOOPT;
117456974a6fSJohn Johansen }
117556974a6fSJohn Johansen 
117656974a6fSJohn Johansen /**
117756974a6fSJohn Johansen  * apparmor_sock_graft - Initialize newly created socket
117856974a6fSJohn Johansen  * @sk: child sock
117956974a6fSJohn Johansen  * @parent: parent socket
118056974a6fSJohn Johansen  *
118156974a6fSJohn Johansen  * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
118256974a6fSJohn Johansen  *       just set sk security information off of current creating process label
118356974a6fSJohn Johansen  *       Labeling of sk for accept case - probably should be sock based
118456974a6fSJohn Johansen  *       instead of task, because of the case where an implicitly labeled
118556974a6fSJohn Johansen  *       socket is shared by different tasks.
118656974a6fSJohn Johansen  */
118756974a6fSJohn Johansen static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
118856974a6fSJohn Johansen {
118956974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = SK_CTX(sk);
119056974a6fSJohn Johansen 
119156974a6fSJohn Johansen 	if (!ctx->label)
119256974a6fSJohn Johansen 		ctx->label = aa_get_current_label();
119356974a6fSJohn Johansen }
119456974a6fSJohn Johansen 
1195e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK
119641dd9596SFlorian Westphal static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1197ab9f2115SMatthew Garrett 				      struct request_sock *req)
1198ab9f2115SMatthew Garrett {
1199ab9f2115SMatthew Garrett 	struct aa_sk_ctx *ctx = SK_CTX(sk);
1200ab9f2115SMatthew Garrett 
1201ab9f2115SMatthew Garrett 	if (!skb->secmark)
1202ab9f2115SMatthew Garrett 		return 0;
1203ab9f2115SMatthew Garrett 
1204ab9f2115SMatthew Garrett 	return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1205ab9f2115SMatthew Garrett 				      skb->secmark, sk);
1206ab9f2115SMatthew Garrett }
1207e1af4779SArnd Bergmann #endif
1208ab9f2115SMatthew Garrett 
1209bbd3662aSCasey Schaufler /*
121037923d43SXiu Jianfeng  * The cred blob is a pointer to, not an instance of, an aa_label.
1211bbd3662aSCasey Schaufler  */
1212bbd3662aSCasey Schaufler struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = {
121337923d43SXiu Jianfeng 	.lbs_cred = sizeof(struct aa_label *),
121433bf60caSCasey Schaufler 	.lbs_file = sizeof(struct aa_file_ctx),
1215f4ad8f2cSCasey Schaufler 	.lbs_task = sizeof(struct aa_task_ctx),
1216bbd3662aSCasey Schaufler };
1217bbd3662aSCasey Schaufler 
1218ca97d939SJames Morris static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
1219e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1220e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1221e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, apparmor_capget),
1222e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, apparmor_capable),
1223b5e95b48SJohn Johansen 
12242ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
12252ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
12262ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
12272ea3ffb7SJohn Johansen 
1228e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_link, apparmor_path_link),
1229e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1230e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1231e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1232e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1233e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1234e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1235e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1236e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1237e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1238e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1239b5e95b48SJohn Johansen 
1240e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, apparmor_file_open),
1241064dc947SJohn Johansen 	LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1242e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1243e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1244e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1245e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1246e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1247e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, apparmor_file_lock),
12483350607dSGünther Noack 	LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
1249b5e95b48SJohn Johansen 
1250e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1251e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1252b5e95b48SJohn Johansen 
125356974a6fSJohn Johansen 	LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
125456974a6fSJohn Johansen 	LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
125556974a6fSJohn Johansen 	LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
125656974a6fSJohn Johansen 
125756974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_create, apparmor_socket_create),
125856974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
125956974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
126056974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
126156974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
126256974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
126356974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
126456974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
126556974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
126656974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
126756974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
126856974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
126956974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1270e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK
127156974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1272e1af4779SArnd Bergmann #endif
127356974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getpeersec_stream,
127456974a6fSJohn Johansen 		      apparmor_socket_getpeersec_stream),
127556974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getpeersec_dgram,
127656974a6fSJohn Johansen 		      apparmor_socket_getpeersec_dgram),
127756974a6fSJohn Johansen 	LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1278e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK
1279ab9f2115SMatthew Garrett 	LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1280e1af4779SArnd Bergmann #endif
128156974a6fSJohn Johansen 
1282e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1283e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1284e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1285e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1286b5e95b48SJohn Johansen 
1287b8bff599SEric W. Biederman 	LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1288e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1289e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1290b5e95b48SJohn Johansen 
12913b529a76SJohn Johansen 	LSM_HOOK_INIT(task_free, apparmor_task_free),
12923b529a76SJohn Johansen 	LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
12936326948fSPaul Moore 	LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj),
12946326948fSPaul Moore 	LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
1295e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1296cd1dbf76SJohn Johansen 	LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1297c0929212SJohn Johansen 
1298e79c26d0SMatthew Garrett #ifdef CONFIG_AUDIT
1299e79c26d0SMatthew Garrett 	LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1300e79c26d0SMatthew Garrett 	LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1301e79c26d0SMatthew Garrett 	LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1302e79c26d0SMatthew Garrett 	LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1303e79c26d0SMatthew Garrett #endif
1304e79c26d0SMatthew Garrett 
1305c0929212SJohn Johansen 	LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1306c0929212SJohn Johansen 	LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1307c0929212SJohn Johansen 	LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1308b5e95b48SJohn Johansen };
1309b5e95b48SJohn Johansen 
1310b5e95b48SJohn Johansen /*
1311b5e95b48SJohn Johansen  * AppArmor sysfs module parameters
1312b5e95b48SJohn Johansen  */
1313b5e95b48SJohn Johansen 
1314101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp);
1315101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1316b8aa09fdSRusty Russell #define param_check_aabool param_check_bool
13179c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aabool = {
13186a4c2643SJani Nikula 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1319101d6c82SStephen Rothwell 	.set = param_set_aabool,
1320101d6c82SStephen Rothwell 	.get = param_get_aabool
1321101d6c82SStephen Rothwell };
1322b5e95b48SJohn Johansen 
1323101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp);
1324101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1325b8aa09fdSRusty Russell #define param_check_aauint param_check_uint
13269c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aauint = {
1327101d6c82SStephen Rothwell 	.set = param_set_aauint,
1328101d6c82SStephen Rothwell 	.get = param_get_aauint
1329101d6c82SStephen Rothwell };
1330b5e95b48SJohn Johansen 
133163c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val,
133263c16c3aSChris Coulson 					const struct kernel_param *kp);
133363c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer,
133463c16c3aSChris Coulson 					const struct kernel_param *kp);
133563c16c3aSChris Coulson #define param_check_aacompressionlevel param_check_int
133663c16c3aSChris Coulson static const struct kernel_param_ops param_ops_aacompressionlevel = {
133763c16c3aSChris Coulson 	.set = param_set_aacompressionlevel,
133863c16c3aSChris Coulson 	.get = param_get_aacompressionlevel
133963c16c3aSChris Coulson };
134063c16c3aSChris Coulson 
1341101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1342101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1343b8aa09fdSRusty Russell #define param_check_aalockpolicy param_check_bool
13449c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aalockpolicy = {
13456a4c2643SJani Nikula 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1346101d6c82SStephen Rothwell 	.set = param_set_aalockpolicy,
1347101d6c82SStephen Rothwell 	.get = param_get_aalockpolicy
1348101d6c82SStephen Rothwell };
1349b5e95b48SJohn Johansen 
1350e4dca7b7SKees Cook static int param_set_audit(const char *val, const struct kernel_param *kp);
1351e4dca7b7SKees Cook static int param_get_audit(char *buffer, const struct kernel_param *kp);
1352b5e95b48SJohn Johansen 
1353e4dca7b7SKees Cook static int param_set_mode(const char *val, const struct kernel_param *kp);
1354e4dca7b7SKees Cook static int param_get_mode(char *buffer, const struct kernel_param *kp);
1355b5e95b48SJohn Johansen 
1356b5e95b48SJohn Johansen /* Flag values, also controllable via /sys/module/apparmor/parameters
1357b5e95b48SJohn Johansen  * We define special types as we want to do additional mediation.
1358b5e95b48SJohn Johansen  */
1359b5e95b48SJohn Johansen 
1360b5e95b48SJohn Johansen /* AppArmor global enforcement switch - complain, enforce, kill */
1361b5e95b48SJohn Johansen enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1362b5e95b48SJohn Johansen module_param_call(mode, param_set_mode, param_get_mode,
1363b5e95b48SJohn Johansen 		  &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1364b5e95b48SJohn Johansen 
13656059f71fSJohn Johansen /* whether policy verification hashing is enabled */
13667616ac70SArnd Bergmann bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
13673ccb76c5SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_HASH
13686059f71fSJohn Johansen module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
13697616ac70SArnd Bergmann #endif
13706059f71fSJohn Johansen 
1371d61c57fdSJohn Johansen /* whether policy exactly as loaded is retained for debug and checkpointing */
1372d61c57fdSJohn Johansen bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1373d61c57fdSJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1374d61c57fdSJohn Johansen module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1375d61c57fdSJohn Johansen #endif
1376d61c57fdSJohn Johansen 
137763c16c3aSChris Coulson /* policy loaddata compression level */
137870f24a9fSJohn Johansen int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
137963c16c3aSChris Coulson module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
138063c16c3aSChris Coulson 		   aacompressionlevel, 0400);
138163c16c3aSChris Coulson 
1382b5e95b48SJohn Johansen /* Debug mode */
1383eea7a05fSValentin Rothberg bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
1384b5e95b48SJohn Johansen module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1385b5e95b48SJohn Johansen 
1386b5e95b48SJohn Johansen /* Audit mode */
1387b5e95b48SJohn Johansen enum audit_mode aa_g_audit;
1388b5e95b48SJohn Johansen module_param_call(audit, param_set_audit, param_get_audit,
1389b5e95b48SJohn Johansen 		  &aa_g_audit, S_IRUSR | S_IWUSR);
1390b5e95b48SJohn Johansen 
1391b5e95b48SJohn Johansen /* Determines if audit header is included in audited messages.  This
1392b5e95b48SJohn Johansen  * provides more context if the audit daemon is not running
1393b5e95b48SJohn Johansen  */
1394954317feSThomas Meyer bool aa_g_audit_header = true;
1395b5e95b48SJohn Johansen module_param_named(audit_header, aa_g_audit_header, aabool,
1396b5e95b48SJohn Johansen 		   S_IRUSR | S_IWUSR);
1397b5e95b48SJohn Johansen 
1398b5e95b48SJohn Johansen /* lock out loading/removal of policy
1399b5e95b48SJohn Johansen  * TODO: add in at boot loading of policy, which is the only way to
1400b5e95b48SJohn Johansen  *       load policy, if lock_policy is set
1401b5e95b48SJohn Johansen  */
140290ab5ee9SRusty Russell bool aa_g_lock_policy;
1403b5e95b48SJohn Johansen module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1404b5e95b48SJohn Johansen 		   S_IRUSR | S_IWUSR);
1405b5e95b48SJohn Johansen 
1406b5e95b48SJohn Johansen /* Syscall logging mode */
140790ab5ee9SRusty Russell bool aa_g_logsyscall;
1408b5e95b48SJohn Johansen module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1409b5e95b48SJohn Johansen 
1410b5e95b48SJohn Johansen /* Maximum pathname length before accesses will start getting rejected */
1411b5e95b48SJohn Johansen unsigned int aa_g_path_max = 2 * PATH_MAX;
1412622f6e32SJohn Johansen module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1413b5e95b48SJohn Johansen 
1414b5e95b48SJohn Johansen /* Determines how paranoid loading of policy is and how much verification
1415b5e95b48SJohn Johansen  * on the loaded policy is done.
1416abbf8734SJohn Johansen  * DEPRECATED: read only as strict checking of load is always done now
1417abbf8734SJohn Johansen  * that none root users (user namespaces) can load policy.
1418b5e95b48SJohn Johansen  */
141979eb2711SLukas Bulwahn bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1420abbf8734SJohn Johansen module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1421b5e95b48SJohn Johansen 
1422e33c1b99SKees Cook static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1423e33c1b99SKees Cook static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1424e33c1b99SKees Cook #define param_check_aaintbool param_check_int
1425e33c1b99SKees Cook static const struct kernel_param_ops param_ops_aaintbool = {
1426e33c1b99SKees Cook 	.set = param_set_aaintbool,
1427e33c1b99SKees Cook 	.get = param_get_aaintbool
1428e33c1b99SKees Cook };
1429b5e95b48SJohn Johansen /* Boot time disable flag */
14300102fb83SKees Cook static int apparmor_enabled __lsm_ro_after_init = 1;
1431e33c1b99SKees Cook module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1432b5e95b48SJohn Johansen 
1433b5e95b48SJohn Johansen static int __init apparmor_enabled_setup(char *str)
1434b5e95b48SJohn Johansen {
1435b5e95b48SJohn Johansen 	unsigned long enabled;
143629707b20SJingoo Han 	int error = kstrtoul(str, 0, &enabled);
1437b5e95b48SJohn Johansen 	if (!error)
1438b5e95b48SJohn Johansen 		apparmor_enabled = enabled ? 1 : 0;
1439b5e95b48SJohn Johansen 	return 1;
1440b5e95b48SJohn Johansen }
1441b5e95b48SJohn Johansen 
1442b5e95b48SJohn Johansen __setup("apparmor=", apparmor_enabled_setup);
1443b5e95b48SJohn Johansen 
1444b5e95b48SJohn Johansen /* set global flag turning off the ability to load policy */
1445101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1446b5e95b48SJohn Johansen {
1447545de8feSJohn Johansen 	if (!apparmor_enabled)
1448545de8feSJohn Johansen 		return -EINVAL;
144992de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1450b5e95b48SJohn Johansen 		return -EPERM;
1451b5e95b48SJohn Johansen 	return param_set_bool(val, kp);
1452b5e95b48SJohn Johansen }
1453b5e95b48SJohn Johansen 
1454101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1455b5e95b48SJohn Johansen {
1456ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1457ca4bd5aeSJohn Johansen 		return -EINVAL;
145892de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1459545de8feSJohn Johansen 		return -EPERM;
1460b5e95b48SJohn Johansen 	return param_get_bool(buffer, kp);
1461b5e95b48SJohn Johansen }
1462b5e95b48SJohn Johansen 
1463101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp)
1464b5e95b48SJohn Johansen {
1465ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1466ca4bd5aeSJohn Johansen 		return -EINVAL;
146792de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1468545de8feSJohn Johansen 		return -EPERM;
1469b5e95b48SJohn Johansen 	return param_set_bool(val, kp);
1470b5e95b48SJohn Johansen }
1471b5e95b48SJohn Johansen 
1472101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1473b5e95b48SJohn Johansen {
1474ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1475ca4bd5aeSJohn Johansen 		return -EINVAL;
147692de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1477545de8feSJohn Johansen 		return -EPERM;
1478b5e95b48SJohn Johansen 	return param_get_bool(buffer, kp);
1479b5e95b48SJohn Johansen }
1480b5e95b48SJohn Johansen 
1481101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp)
1482b5e95b48SJohn Johansen {
148339d84824SJohn Johansen 	int error;
148439d84824SJohn Johansen 
1485ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1486ca4bd5aeSJohn Johansen 		return -EINVAL;
148739d84824SJohn Johansen 	/* file is ro but enforce 2nd line check */
148839d84824SJohn Johansen 	if (apparmor_initialized)
1489545de8feSJohn Johansen 		return -EPERM;
149039d84824SJohn Johansen 
149139d84824SJohn Johansen 	error = param_set_uint(val, kp);
1492df323337SSebastian Andrzej Siewior 	aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
149339d84824SJohn Johansen 	pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
149439d84824SJohn Johansen 
149539d84824SJohn Johansen 	return error;
1496b5e95b48SJohn Johansen }
1497b5e95b48SJohn Johansen 
1498101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1499b5e95b48SJohn Johansen {
1500ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1501ca4bd5aeSJohn Johansen 		return -EINVAL;
150292de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1503545de8feSJohn Johansen 		return -EPERM;
1504b5e95b48SJohn Johansen 	return param_get_uint(buffer, kp);
1505b5e95b48SJohn Johansen }
1506b5e95b48SJohn Johansen 
1507e33c1b99SKees Cook /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
1508e33c1b99SKees Cook static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1509e33c1b99SKees Cook {
1510e33c1b99SKees Cook 	struct kernel_param kp_local;
1511e33c1b99SKees Cook 	bool value;
1512e33c1b99SKees Cook 	int error;
1513e33c1b99SKees Cook 
1514e33c1b99SKees Cook 	if (apparmor_initialized)
1515e33c1b99SKees Cook 		return -EPERM;
1516e33c1b99SKees Cook 
1517e33c1b99SKees Cook 	/* Create local copy, with arg pointing to bool type. */
1518e33c1b99SKees Cook 	value = !!*((int *)kp->arg);
1519e33c1b99SKees Cook 	memcpy(&kp_local, kp, sizeof(kp_local));
1520e33c1b99SKees Cook 	kp_local.arg = &value;
1521e33c1b99SKees Cook 
1522e33c1b99SKees Cook 	error = param_set_bool(val, &kp_local);
1523e33c1b99SKees Cook 	if (!error)
1524e33c1b99SKees Cook 		*((int *)kp->arg) = *((bool *)kp_local.arg);
1525e33c1b99SKees Cook 	return error;
1526e33c1b99SKees Cook }
1527e33c1b99SKees Cook 
1528e33c1b99SKees Cook /*
1529e33c1b99SKees Cook  * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1530e33c1b99SKees Cook  * 1/0, this converts the "int that is actually bool" back to bool for
1531e33c1b99SKees Cook  * display in the /sys filesystem, while keeping it "int" for the LSM
1532e33c1b99SKees Cook  * infrastructure.
1533e33c1b99SKees Cook  */
1534e33c1b99SKees Cook static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1535e33c1b99SKees Cook {
1536e33c1b99SKees Cook 	struct kernel_param kp_local;
1537e33c1b99SKees Cook 	bool value;
1538e33c1b99SKees Cook 
1539e33c1b99SKees Cook 	/* Create local copy, with arg pointing to bool type. */
1540e33c1b99SKees Cook 	value = !!*((int *)kp->arg);
1541e33c1b99SKees Cook 	memcpy(&kp_local, kp, sizeof(kp_local));
1542e33c1b99SKees Cook 	kp_local.arg = &value;
1543e33c1b99SKees Cook 
1544e33c1b99SKees Cook 	return param_get_bool(buffer, &kp_local);
1545e33c1b99SKees Cook }
1546e33c1b99SKees Cook 
154763c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val,
154863c16c3aSChris Coulson 					const struct kernel_param *kp)
154963c16c3aSChris Coulson {
155063c16c3aSChris Coulson 	int error;
155163c16c3aSChris Coulson 
155263c16c3aSChris Coulson 	if (!apparmor_enabled)
155363c16c3aSChris Coulson 		return -EINVAL;
155463c16c3aSChris Coulson 	if (apparmor_initialized)
155563c16c3aSChris Coulson 		return -EPERM;
155663c16c3aSChris Coulson 
155763c16c3aSChris Coulson 	error = param_set_int(val, kp);
155863c16c3aSChris Coulson 
155963c16c3aSChris Coulson 	aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
156070f24a9fSJohn Johansen 					       AA_MIN_CLEVEL, AA_MAX_CLEVEL);
1561f4d6b94bSJon Tourville 	pr_info("AppArmor: policy rawdata compression level set to %d\n",
156263c16c3aSChris Coulson 		aa_g_rawdata_compression_level);
156363c16c3aSChris Coulson 
156463c16c3aSChris Coulson 	return error;
156563c16c3aSChris Coulson }
156663c16c3aSChris Coulson 
156763c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer,
156863c16c3aSChris Coulson 					const struct kernel_param *kp)
156963c16c3aSChris Coulson {
157063c16c3aSChris Coulson 	if (!apparmor_enabled)
157163c16c3aSChris Coulson 		return -EINVAL;
157292de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
157363c16c3aSChris Coulson 		return -EPERM;
157463c16c3aSChris Coulson 	return param_get_int(buffer, kp);
157563c16c3aSChris Coulson }
157663c16c3aSChris Coulson 
1577e4dca7b7SKees Cook static int param_get_audit(char *buffer, const struct kernel_param *kp)
1578b5e95b48SJohn Johansen {
1579b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1580b5e95b48SJohn Johansen 		return -EINVAL;
158192de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1582545de8feSJohn Johansen 		return -EPERM;
1583b5e95b48SJohn Johansen 	return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1584b5e95b48SJohn Johansen }
1585b5e95b48SJohn Johansen 
1586e4dca7b7SKees Cook static int param_set_audit(const char *val, const struct kernel_param *kp)
1587b5e95b48SJohn Johansen {
1588b5e95b48SJohn Johansen 	int i;
1589b5e95b48SJohn Johansen 
1590b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1591b5e95b48SJohn Johansen 		return -EINVAL;
1592b5e95b48SJohn Johansen 	if (!val)
1593b5e95b48SJohn Johansen 		return -EINVAL;
159492de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1595545de8feSJohn Johansen 		return -EPERM;
1596b5e95b48SJohn Johansen 
15975d8779a5SAndy Shevchenko 	i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
15985d8779a5SAndy Shevchenko 	if (i < 0)
15995d8779a5SAndy Shevchenko 		return -EINVAL;
16005d8779a5SAndy Shevchenko 
1601b5e95b48SJohn Johansen 	aa_g_audit = i;
1602b5e95b48SJohn Johansen 	return 0;
1603b5e95b48SJohn Johansen }
1604b5e95b48SJohn Johansen 
1605e4dca7b7SKees Cook static int param_get_mode(char *buffer, const struct kernel_param *kp)
1606b5e95b48SJohn Johansen {
1607b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1608b5e95b48SJohn Johansen 		return -EINVAL;
160992de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1610545de8feSJohn Johansen 		return -EPERM;
1611b5e95b48SJohn Johansen 
16120d259f04SJohn Johansen 	return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1613b5e95b48SJohn Johansen }
1614b5e95b48SJohn Johansen 
1615e4dca7b7SKees Cook static int param_set_mode(const char *val, const struct kernel_param *kp)
1616b5e95b48SJohn Johansen {
1617b5e95b48SJohn Johansen 	int i;
1618b5e95b48SJohn Johansen 
1619b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1620b5e95b48SJohn Johansen 		return -EINVAL;
1621b5e95b48SJohn Johansen 	if (!val)
1622b5e95b48SJohn Johansen 		return -EINVAL;
162392de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1624545de8feSJohn Johansen 		return -EPERM;
1625b5e95b48SJohn Johansen 
16265d8779a5SAndy Shevchenko 	i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
16275d8779a5SAndy Shevchenko 			 val);
16285d8779a5SAndy Shevchenko 	if (i < 0)
16295d8779a5SAndy Shevchenko 		return -EINVAL;
16305d8779a5SAndy Shevchenko 
1631b5e95b48SJohn Johansen 	aa_g_profile_mode = i;
1632b5e95b48SJohn Johansen 	return 0;
1633b5e95b48SJohn Johansen }
1634b5e95b48SJohn Johansen 
1635341c1fdaSJohn Johansen char *aa_get_buffer(bool in_atomic)
1636df323337SSebastian Andrzej Siewior {
1637df323337SSebastian Andrzej Siewior 	union aa_buffer *aa_buf;
1638df323337SSebastian Andrzej Siewior 	bool try_again = true;
1639341c1fdaSJohn Johansen 	gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1640df323337SSebastian Andrzej Siewior 
1641df323337SSebastian Andrzej Siewior retry:
1642df323337SSebastian Andrzej Siewior 	spin_lock(&aa_buffers_lock);
1643341c1fdaSJohn Johansen 	if (buffer_count > reserve_count ||
1644341c1fdaSJohn Johansen 	    (in_atomic && !list_empty(&aa_global_buffers))) {
1645df323337SSebastian Andrzej Siewior 		aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1646df323337SSebastian Andrzej Siewior 					  list);
1647df323337SSebastian Andrzej Siewior 		list_del(&aa_buf->list);
1648341c1fdaSJohn Johansen 		buffer_count--;
1649df323337SSebastian Andrzej Siewior 		spin_unlock(&aa_buffers_lock);
1650df323337SSebastian Andrzej Siewior 		return &aa_buf->buffer[0];
1651df323337SSebastian Andrzej Siewior 	}
1652341c1fdaSJohn Johansen 	if (in_atomic) {
1653341c1fdaSJohn Johansen 		/*
1654341c1fdaSJohn Johansen 		 * out of reserve buffers and in atomic context so increase
1655341c1fdaSJohn Johansen 		 * how many buffers to keep in reserve
1656341c1fdaSJohn Johansen 		 */
1657341c1fdaSJohn Johansen 		reserve_count++;
1658341c1fdaSJohn Johansen 		flags = GFP_ATOMIC;
1659341c1fdaSJohn Johansen 	}
1660df323337SSebastian Andrzej Siewior 	spin_unlock(&aa_buffers_lock);
1661df323337SSebastian Andrzej Siewior 
1662341c1fdaSJohn Johansen 	if (!in_atomic)
1663341c1fdaSJohn Johansen 		might_sleep();
1664341c1fdaSJohn Johansen 	aa_buf = kmalloc(aa_g_path_max, flags);
1665df323337SSebastian Andrzej Siewior 	if (!aa_buf) {
1666df323337SSebastian Andrzej Siewior 		if (try_again) {
1667df323337SSebastian Andrzej Siewior 			try_again = false;
1668df323337SSebastian Andrzej Siewior 			goto retry;
1669df323337SSebastian Andrzej Siewior 		}
1670df323337SSebastian Andrzej Siewior 		pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
1671df323337SSebastian Andrzej Siewior 		return NULL;
1672df323337SSebastian Andrzej Siewior 	}
1673df323337SSebastian Andrzej Siewior 	return &aa_buf->buffer[0];
1674df323337SSebastian Andrzej Siewior }
1675df323337SSebastian Andrzej Siewior 
1676df323337SSebastian Andrzej Siewior void aa_put_buffer(char *buf)
1677df323337SSebastian Andrzej Siewior {
1678df323337SSebastian Andrzej Siewior 	union aa_buffer *aa_buf;
1679df323337SSebastian Andrzej Siewior 
1680df323337SSebastian Andrzej Siewior 	if (!buf)
1681df323337SSebastian Andrzej Siewior 		return;
1682df323337SSebastian Andrzej Siewior 	aa_buf = container_of(buf, union aa_buffer, buffer[0]);
1683df323337SSebastian Andrzej Siewior 
1684df323337SSebastian Andrzej Siewior 	spin_lock(&aa_buffers_lock);
1685df323337SSebastian Andrzej Siewior 	list_add(&aa_buf->list, &aa_global_buffers);
1686341c1fdaSJohn Johansen 	buffer_count++;
1687df323337SSebastian Andrzej Siewior 	spin_unlock(&aa_buffers_lock);
1688df323337SSebastian Andrzej Siewior }
1689df323337SSebastian Andrzej Siewior 
1690b5e95b48SJohn Johansen /*
1691b5e95b48SJohn Johansen  * AppArmor init functions
1692b5e95b48SJohn Johansen  */
1693b5e95b48SJohn Johansen 
1694b5e95b48SJohn Johansen /**
169555a26ebfSJohn Johansen  * set_init_ctx - set a task context and profile on the first task.
1696b5e95b48SJohn Johansen  *
1697b5e95b48SJohn Johansen  * TODO: allow setting an alternate profile than unconfined
1698b5e95b48SJohn Johansen  */
169955a26ebfSJohn Johansen static int __init set_init_ctx(void)
1700b5e95b48SJohn Johansen {
1701bf1d2ee7SBharath Vedartham 	struct cred *cred = (__force struct cred *)current->real_cred;
1702b5e95b48SJohn Johansen 
170369b5a44aSCasey Schaufler 	set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
1704b5e95b48SJohn Johansen 
1705b5e95b48SJohn Johansen 	return 0;
1706b5e95b48SJohn Johansen }
1707b5e95b48SJohn Johansen 
1708d4669f0bSJohn Johansen static void destroy_buffers(void)
1709d4669f0bSJohn Johansen {
1710df323337SSebastian Andrzej Siewior 	union aa_buffer *aa_buf;
1711d4669f0bSJohn Johansen 
1712df323337SSebastian Andrzej Siewior 	spin_lock(&aa_buffers_lock);
1713df323337SSebastian Andrzej Siewior 	while (!list_empty(&aa_global_buffers)) {
1714df323337SSebastian Andrzej Siewior 		aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1715df323337SSebastian Andrzej Siewior 					 list);
1716df323337SSebastian Andrzej Siewior 		list_del(&aa_buf->list);
1717df323337SSebastian Andrzej Siewior 		spin_unlock(&aa_buffers_lock);
1718df323337SSebastian Andrzej Siewior 		kfree(aa_buf);
1719df323337SSebastian Andrzej Siewior 		spin_lock(&aa_buffers_lock);
1720d4669f0bSJohn Johansen 	}
1721df323337SSebastian Andrzej Siewior 	spin_unlock(&aa_buffers_lock);
1722d4669f0bSJohn Johansen }
1723d4669f0bSJohn Johansen 
1724d4669f0bSJohn Johansen static int __init alloc_buffers(void)
1725d4669f0bSJohn Johansen {
1726df323337SSebastian Andrzej Siewior 	union aa_buffer *aa_buf;
1727df323337SSebastian Andrzej Siewior 	int i, num;
1728d4669f0bSJohn Johansen 
1729df323337SSebastian Andrzej Siewior 	/*
1730df323337SSebastian Andrzej Siewior 	 * A function may require two buffers at once. Usually the buffers are
1731df323337SSebastian Andrzej Siewior 	 * used for a short period of time and are shared. On UP kernel buffers
1732df323337SSebastian Andrzej Siewior 	 * two should be enough, with more CPUs it is possible that more
1733df323337SSebastian Andrzej Siewior 	 * buffers will be used simultaneously. The preallocated pool may grow.
1734df323337SSebastian Andrzej Siewior 	 * This preallocation has also the side-effect that AppArmor will be
1735df323337SSebastian Andrzej Siewior 	 * disabled early at boot if aa_g_path_max is extremly high.
1736df323337SSebastian Andrzej Siewior 	 */
1737df323337SSebastian Andrzej Siewior 	if (num_online_cpus() > 1)
1738341c1fdaSJohn Johansen 		num = 4 + RESERVE_COUNT;
1739d4669f0bSJohn Johansen 	else
1740341c1fdaSJohn Johansen 		num = 2 + RESERVE_COUNT;
1741df323337SSebastian Andrzej Siewior 
1742df323337SSebastian Andrzej Siewior 	for (i = 0; i < num; i++) {
1743df323337SSebastian Andrzej Siewior 
1744df323337SSebastian Andrzej Siewior 		aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
1745df323337SSebastian Andrzej Siewior 				 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1746df323337SSebastian Andrzej Siewior 		if (!aa_buf) {
1747d4669f0bSJohn Johansen 			destroy_buffers();
1748d4669f0bSJohn Johansen 			return -ENOMEM;
1749d4669f0bSJohn Johansen 		}
1750df323337SSebastian Andrzej Siewior 		aa_put_buffer(&aa_buf->buffer[0]);
1751d4669f0bSJohn Johansen 	}
1752d4669f0bSJohn Johansen 	return 0;
1753d4669f0bSJohn Johansen }
1754d4669f0bSJohn Johansen 
1755e3ea1ca5STyler Hicks #ifdef CONFIG_SYSCTL
1756e3ea1ca5STyler Hicks static int apparmor_dointvec(struct ctl_table *table, int write,
175732927393SChristoph Hellwig 			     void *buffer, size_t *lenp, loff_t *ppos)
1758e3ea1ca5STyler Hicks {
175992de220aSJohn Johansen 	if (!aa_current_policy_admin_capable(NULL))
1760e3ea1ca5STyler Hicks 		return -EPERM;
1761e3ea1ca5STyler Hicks 	if (!apparmor_enabled)
1762e3ea1ca5STyler Hicks 		return -EINVAL;
1763e3ea1ca5STyler Hicks 
1764e3ea1ca5STyler Hicks 	return proc_dointvec(table, write, buffer, lenp, ppos);
1765e3ea1ca5STyler Hicks }
1766e3ea1ca5STyler Hicks 
1767e3ea1ca5STyler Hicks static struct ctl_path apparmor_sysctl_path[] = {
1768e3ea1ca5STyler Hicks 	{ .procname = "kernel", },
1769e3ea1ca5STyler Hicks 	{ }
1770e3ea1ca5STyler Hicks };
1771e3ea1ca5STyler Hicks 
1772e3ea1ca5STyler Hicks static struct ctl_table apparmor_sysctl_table[] = {
1773e3ea1ca5STyler Hicks 	{
1774e3ea1ca5STyler Hicks 		.procname       = "unprivileged_userns_apparmor_policy",
1775e3ea1ca5STyler Hicks 		.data           = &unprivileged_userns_apparmor_policy,
1776e3ea1ca5STyler Hicks 		.maxlen         = sizeof(int),
1777e3ea1ca5STyler Hicks 		.mode           = 0600,
1778e3ea1ca5STyler Hicks 		.proc_handler   = apparmor_dointvec,
1779e3ea1ca5STyler Hicks 	},
1780524d8e14SJohn Johansen 	{
1781524d8e14SJohn Johansen 		.procname       = "apparmor_display_secid_mode",
1782524d8e14SJohn Johansen 		.data           = &apparmor_display_secid_mode,
1783524d8e14SJohn Johansen 		.maxlen         = sizeof(int),
1784524d8e14SJohn Johansen 		.mode           = 0600,
1785524d8e14SJohn Johansen 		.proc_handler   = apparmor_dointvec,
1786524d8e14SJohn Johansen 	},
1787524d8e14SJohn Johansen 
1788e3ea1ca5STyler Hicks 	{ }
1789e3ea1ca5STyler Hicks };
1790e3ea1ca5STyler Hicks 
1791e3ea1ca5STyler Hicks static int __init apparmor_init_sysctl(void)
1792e3ea1ca5STyler Hicks {
1793e3ea1ca5STyler Hicks 	return register_sysctl_paths(apparmor_sysctl_path,
1794e3ea1ca5STyler Hicks 				     apparmor_sysctl_table) ? 0 : -ENOMEM;
1795e3ea1ca5STyler Hicks }
1796e3ea1ca5STyler Hicks #else
1797e3ea1ca5STyler Hicks static inline int apparmor_init_sysctl(void)
1798e3ea1ca5STyler Hicks {
1799e3ea1ca5STyler Hicks 	return 0;
1800e3ea1ca5STyler Hicks }
1801e3ea1ca5STyler Hicks #endif /* CONFIG_SYSCTL */
1802e3ea1ca5STyler Hicks 
1803e1af4779SArnd Bergmann #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
1804ab9f2115SMatthew Garrett static unsigned int apparmor_ip_postroute(void *priv,
1805ab9f2115SMatthew Garrett 					  struct sk_buff *skb,
1806ab9f2115SMatthew Garrett 					  const struct nf_hook_state *state)
1807ab9f2115SMatthew Garrett {
1808ab9f2115SMatthew Garrett 	struct aa_sk_ctx *ctx;
1809ab9f2115SMatthew Garrett 	struct sock *sk;
1810ab9f2115SMatthew Garrett 
1811ab9f2115SMatthew Garrett 	if (!skb->secmark)
1812ab9f2115SMatthew Garrett 		return NF_ACCEPT;
1813ab9f2115SMatthew Garrett 
1814ab9f2115SMatthew Garrett 	sk = skb_to_full_sk(skb);
1815ab9f2115SMatthew Garrett 	if (sk == NULL)
1816ab9f2115SMatthew Garrett 		return NF_ACCEPT;
1817ab9f2115SMatthew Garrett 
1818ab9f2115SMatthew Garrett 	ctx = SK_CTX(sk);
1819ab9f2115SMatthew Garrett 	if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
1820ab9f2115SMatthew Garrett 				    skb->secmark, sk))
1821ab9f2115SMatthew Garrett 		return NF_ACCEPT;
1822ab9f2115SMatthew Garrett 
1823ab9f2115SMatthew Garrett 	return NF_DROP_ERR(-ECONNREFUSED);
1824ab9f2115SMatthew Garrett 
1825ab9f2115SMatthew Garrett }
1826ab9f2115SMatthew Garrett 
1827ab9f2115SMatthew Garrett static const struct nf_hook_ops apparmor_nf_ops[] = {
1828ab9f2115SMatthew Garrett 	{
18297b721124SFlorian Westphal 		.hook =         apparmor_ip_postroute,
1830ab9f2115SMatthew Garrett 		.pf =           NFPROTO_IPV4,
1831ab9f2115SMatthew Garrett 		.hooknum =      NF_INET_POST_ROUTING,
1832ab9f2115SMatthew Garrett 		.priority =     NF_IP_PRI_SELINUX_FIRST,
1833ab9f2115SMatthew Garrett 	},
1834ab9f2115SMatthew Garrett #if IS_ENABLED(CONFIG_IPV6)
1835ab9f2115SMatthew Garrett 	{
18367b721124SFlorian Westphal 		.hook =         apparmor_ip_postroute,
1837ab9f2115SMatthew Garrett 		.pf =           NFPROTO_IPV6,
1838ab9f2115SMatthew Garrett 		.hooknum =      NF_INET_POST_ROUTING,
1839ab9f2115SMatthew Garrett 		.priority =     NF_IP6_PRI_SELINUX_FIRST,
1840ab9f2115SMatthew Garrett 	},
1841ab9f2115SMatthew Garrett #endif
1842ab9f2115SMatthew Garrett };
1843ab9f2115SMatthew Garrett 
1844ab9f2115SMatthew Garrett static int __net_init apparmor_nf_register(struct net *net)
1845ab9f2115SMatthew Garrett {
184684117994SMinghao Chi 	return nf_register_net_hooks(net, apparmor_nf_ops,
1847ab9f2115SMatthew Garrett 				    ARRAY_SIZE(apparmor_nf_ops));
1848ab9f2115SMatthew Garrett }
1849ab9f2115SMatthew Garrett 
1850ab9f2115SMatthew Garrett static void __net_exit apparmor_nf_unregister(struct net *net)
1851ab9f2115SMatthew Garrett {
1852ab9f2115SMatthew Garrett 	nf_unregister_net_hooks(net, apparmor_nf_ops,
1853ab9f2115SMatthew Garrett 				ARRAY_SIZE(apparmor_nf_ops));
1854ab9f2115SMatthew Garrett }
1855ab9f2115SMatthew Garrett 
1856ab9f2115SMatthew Garrett static struct pernet_operations apparmor_net_ops = {
1857ab9f2115SMatthew Garrett 	.init = apparmor_nf_register,
1858ab9f2115SMatthew Garrett 	.exit = apparmor_nf_unregister,
1859ab9f2115SMatthew Garrett };
1860ab9f2115SMatthew Garrett 
1861ab9f2115SMatthew Garrett static int __init apparmor_nf_ip_init(void)
1862ab9f2115SMatthew Garrett {
1863ab9f2115SMatthew Garrett 	int err;
1864ab9f2115SMatthew Garrett 
1865ab9f2115SMatthew Garrett 	if (!apparmor_enabled)
1866ab9f2115SMatthew Garrett 		return 0;
1867ab9f2115SMatthew Garrett 
1868ab9f2115SMatthew Garrett 	err = register_pernet_subsys(&apparmor_net_ops);
1869ab9f2115SMatthew Garrett 	if (err)
1870ab9f2115SMatthew Garrett 		panic("Apparmor: register_pernet_subsys: error %d\n", err);
1871ab9f2115SMatthew Garrett 
1872ab9f2115SMatthew Garrett 	return 0;
1873ab9f2115SMatthew Garrett }
1874ab9f2115SMatthew Garrett __initcall(apparmor_nf_ip_init);
1875e1af4779SArnd Bergmann #endif
1876ab9f2115SMatthew Garrett 
1877b5e95b48SJohn Johansen static int __init apparmor_init(void)
1878b5e95b48SJohn Johansen {
1879b5e95b48SJohn Johansen 	int error;
1880b5e95b48SJohn Johansen 
188111c236b8SJohn Johansen 	error = aa_setup_dfa_engine();
188211c236b8SJohn Johansen 	if (error) {
188311c236b8SJohn Johansen 		AA_ERROR("Unable to setup dfa engine\n");
188411c236b8SJohn Johansen 		goto alloc_out;
188511c236b8SJohn Johansen 	}
188611c236b8SJohn Johansen 
1887b5e95b48SJohn Johansen 	error = aa_alloc_root_ns();
1888b5e95b48SJohn Johansen 	if (error) {
1889b5e95b48SJohn Johansen 		AA_ERROR("Unable to allocate default profile namespace\n");
1890b5e95b48SJohn Johansen 		goto alloc_out;
1891b5e95b48SJohn Johansen 	}
1892b5e95b48SJohn Johansen 
1893e3ea1ca5STyler Hicks 	error = apparmor_init_sysctl();
1894e3ea1ca5STyler Hicks 	if (error) {
1895e3ea1ca5STyler Hicks 		AA_ERROR("Unable to register sysctls\n");
1896e3ea1ca5STyler Hicks 		goto alloc_out;
1897e3ea1ca5STyler Hicks 
1898e3ea1ca5STyler Hicks 	}
1899e3ea1ca5STyler Hicks 
1900d4669f0bSJohn Johansen 	error = alloc_buffers();
1901d4669f0bSJohn Johansen 	if (error) {
1902d4669f0bSJohn Johansen 		AA_ERROR("Unable to allocate work buffers\n");
1903df323337SSebastian Andrzej Siewior 		goto alloc_out;
1904d4669f0bSJohn Johansen 	}
1905d4669f0bSJohn Johansen 
190655a26ebfSJohn Johansen 	error = set_init_ctx();
1907b5e95b48SJohn Johansen 	if (error) {
1908b5e95b48SJohn Johansen 		AA_ERROR("Failed to set context on init task\n");
1909b1d9e6b0SCasey Schaufler 		aa_free_root_ns();
1910d4669f0bSJohn Johansen 		goto buffers_out;
1911b5e95b48SJohn Johansen 	}
1912d69dece5SCasey Schaufler 	security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1913d69dece5SCasey Schaufler 				"apparmor");
1914b5e95b48SJohn Johansen 
1915b5e95b48SJohn Johansen 	/* Report that AppArmor successfully initialized */
1916b5e95b48SJohn Johansen 	apparmor_initialized = 1;
1917b5e95b48SJohn Johansen 	if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1918b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized: complain mode enabled");
1919b5e95b48SJohn Johansen 	else if (aa_g_profile_mode == APPARMOR_KILL)
1920b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized: kill mode enabled");
1921b5e95b48SJohn Johansen 	else
1922b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized");
1923b5e95b48SJohn Johansen 
1924b5e95b48SJohn Johansen 	return error;
1925b5e95b48SJohn Johansen 
1926d4669f0bSJohn Johansen buffers_out:
1927d4669f0bSJohn Johansen 	destroy_buffers();
1928b5e95b48SJohn Johansen alloc_out:
1929b5e95b48SJohn Johansen 	aa_destroy_aafs();
193011c236b8SJohn Johansen 	aa_teardown_dfa_engine();
1931b5e95b48SJohn Johansen 
1932954317feSThomas Meyer 	apparmor_enabled = false;
1933b5e95b48SJohn Johansen 	return error;
1934b5e95b48SJohn Johansen }
1935b5e95b48SJohn Johansen 
19363d6e5f6dSKees Cook DEFINE_LSM(apparmor) = {
193707aed2f2SKees Cook 	.name = "apparmor",
193814bd99c8SKees Cook 	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
1939c5459b82SKees Cook 	.enabled = &apparmor_enabled,
1940bbd3662aSCasey Schaufler 	.blobs = &apparmor_blob_sizes,
19413d6e5f6dSKees Cook 	.init = apparmor_init,
19423d6e5f6dSKees Cook };
1943