xref: /openbmc/linux/security/apparmor/lsm.c (revision 6c920754)
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;
49ba808cb5SKees Cook 	DECLARE_FLEX_ARRAY(char, buffer);
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  */
apparmor_cred_free(struct cred * cred)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  */
apparmor_cred_alloc_blank(struct cred * cred,gfp_t gfp)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  */
apparmor_cred_prepare(struct cred * new,const struct cred * old,gfp_t gfp)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  */
apparmor_cred_transfer(struct cred * new,const struct cred * old)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 
apparmor_task_free(struct task_struct * task)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 
apparmor_task_alloc(struct task_struct * task,unsigned long clone_flags)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 
apparmor_ptrace_access_check(struct task_struct * child,unsigned int mode)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;
119690f33e1SJohn Johansen 	const struct cred *cred;
120b2d09ae4SJohn Johansen 	int error;
121b2d09ae4SJohn Johansen 
122690f33e1SJohn Johansen 	cred = get_task_cred(child);
123690f33e1SJohn Johansen 	tracee = cred_label(cred);	/* ref count on cred */
1241f8266ffSJann Horn 	tracer = __begin_current_label_crit_section();
125690f33e1SJohn Johansen 	error = aa_may_ptrace(current_cred(), tracer, cred, tracee,
126338d0be4SJohn Johansen 			(mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
127338d0be4SJohn Johansen 						  : AA_PTRACE_TRACE);
1281f8266ffSJann Horn 	__end_current_label_crit_section(tracer);
129690f33e1SJohn Johansen 	put_cred(cred);
130b2d09ae4SJohn Johansen 
131b2d09ae4SJohn Johansen 	return error;
132b5e95b48SJohn Johansen }
133b5e95b48SJohn Johansen 
apparmor_ptrace_traceme(struct task_struct * parent)134b5e95b48SJohn Johansen static int apparmor_ptrace_traceme(struct task_struct *parent)
135b5e95b48SJohn Johansen {
136b2d09ae4SJohn Johansen 	struct aa_label *tracer, *tracee;
137690f33e1SJohn Johansen 	const struct cred *cred;
138b2d09ae4SJohn Johansen 	int error;
139b2d09ae4SJohn Johansen 
140ca3fde52SJann Horn 	tracee = __begin_current_label_crit_section();
141690f33e1SJohn Johansen 	cred = get_task_cred(parent);
142690f33e1SJohn Johansen 	tracer = cred_label(cred);	/* ref count on cred */
143690f33e1SJohn Johansen 	error = aa_may_ptrace(cred, tracer, current_cred(), tracee,
144690f33e1SJohn Johansen 			      AA_PTRACE_TRACE);
145690f33e1SJohn Johansen 	put_cred(cred);
146ca3fde52SJann Horn 	__end_current_label_crit_section(tracee);
147b2d09ae4SJohn Johansen 
148b2d09ae4SJohn Johansen 	return error;
149b5e95b48SJohn Johansen }
150b5e95b48SJohn Johansen 
151b5e95b48SJohn Johansen /* Derived from security/commoncap.c:cap_capget */
apparmor_capget(const struct task_struct * target,kernel_cap_t * effective,kernel_cap_t * inheritable,kernel_cap_t * permitted)1526672efbbSKhadija Kamran static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
153b5e95b48SJohn Johansen 			   kernel_cap_t *inheritable, kernel_cap_t *permitted)
154b5e95b48SJohn Johansen {
155637f688dSJohn Johansen 	struct aa_label *label;
156b5e95b48SJohn Johansen 	const struct cred *cred;
157b5e95b48SJohn Johansen 
158b5e95b48SJohn Johansen 	rcu_read_lock();
159b5e95b48SJohn Johansen 	cred = __task_cred(target);
160637f688dSJohn Johansen 	label = aa_get_newest_cred_label(cred);
161c70c86c4SJohn Johansen 
162b1d9e6b0SCasey Schaufler 	/*
163b1d9e6b0SCasey Schaufler 	 * cap_capget is stacked ahead of this and will
164b1d9e6b0SCasey Schaufler 	 * initialize effective and permitted.
165b1d9e6b0SCasey Schaufler 	 */
166c70c86c4SJohn Johansen 	if (!unconfined(label)) {
167c70c86c4SJohn Johansen 		struct aa_profile *profile;
168c70c86c4SJohn Johansen 		struct label_it i;
169c70c86c4SJohn Johansen 
170c70c86c4SJohn Johansen 		label_for_each_confined(i, label, profile) {
1711ad22fccSJohn Johansen 			struct aa_ruleset *rules;
172c70c86c4SJohn Johansen 			if (COMPLAIN_MODE(profile))
173c70c86c4SJohn Johansen 				continue;
1741ad22fccSJohn Johansen 			rules = list_first_entry(&profile->rules,
1751ad22fccSJohn Johansen 						 typeof(*rules), list);
176c70c86c4SJohn Johansen 			*effective = cap_intersect(*effective,
1771ad22fccSJohn Johansen 						   rules->caps.allow);
178c70c86c4SJohn Johansen 			*permitted = cap_intersect(*permitted,
1791ad22fccSJohn Johansen 						   rules->caps.allow);
180c70c86c4SJohn Johansen 		}
181b5e95b48SJohn Johansen 	}
182b5e95b48SJohn Johansen 	rcu_read_unlock();
183637f688dSJohn Johansen 	aa_put_label(label);
184b5e95b48SJohn Johansen 
185b5e95b48SJohn Johansen 	return 0;
186b5e95b48SJohn Johansen }
187b5e95b48SJohn Johansen 
apparmor_capable(const struct cred * cred,struct user_namespace * ns,int cap,unsigned int opts)1886a9de491SEric Paris static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
189c1a85a00SMicah Morton 			    int cap, unsigned int opts)
190b5e95b48SJohn Johansen {
191637f688dSJohn Johansen 	struct aa_label *label;
192b1d9e6b0SCasey Schaufler 	int error = 0;
193b1d9e6b0SCasey Schaufler 
194637f688dSJohn Johansen 	label = aa_get_newest_cred_label(cred);
195637f688dSJohn Johansen 	if (!unconfined(label))
196690f33e1SJohn Johansen 		error = aa_capable(cred, label, cap, opts);
197637f688dSJohn Johansen 	aa_put_label(label);
198cf797c0eSJohn Johansen 
199b5e95b48SJohn Johansen 	return error;
200b5e95b48SJohn Johansen }
201b5e95b48SJohn Johansen 
202b5e95b48SJohn Johansen /**
203b5e95b48SJohn Johansen  * common_perm - basic common permission check wrapper fn for paths
204b5e95b48SJohn Johansen  * @op: operation being checked
205b5e95b48SJohn Johansen  * @path: path to check permission of  (NOT NULL)
206b5e95b48SJohn Johansen  * @mask: requested permissions mask
207b5e95b48SJohn Johansen  * @cond: conditional info for the permission request  (NOT NULL)
208b5e95b48SJohn Johansen  *
209b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
210b5e95b48SJohn Johansen  */
common_perm(const char * op,const struct path * path,u32 mask,struct path_cond * cond)21147f6e5ccSJohn Johansen static int common_perm(const char *op, const struct path *path, u32 mask,
212b5e95b48SJohn Johansen 		       struct path_cond *cond)
213b5e95b48SJohn Johansen {
214637f688dSJohn Johansen 	struct aa_label *label;
215b5e95b48SJohn Johansen 	int error = 0;
216b5e95b48SJohn Johansen 
217637f688dSJohn Johansen 	label = __begin_current_label_crit_section();
218637f688dSJohn Johansen 	if (!unconfined(label))
219690f33e1SJohn Johansen 		error = aa_path_perm(op, current_cred(), label, path, 0, mask,
220690f33e1SJohn Johansen 				     cond);
221637f688dSJohn Johansen 	__end_current_label_crit_section(label);
222b5e95b48SJohn Johansen 
223b5e95b48SJohn Johansen 	return error;
224b5e95b48SJohn Johansen }
225b5e95b48SJohn Johansen 
226b5e95b48SJohn Johansen /**
22731f75bfeSJohn Johansen  * common_perm_cond - common permission wrapper around inode cond
22831f75bfeSJohn Johansen  * @op: operation being checked
22931f75bfeSJohn Johansen  * @path: location to check (NOT NULL)
23031f75bfeSJohn Johansen  * @mask: requested permissions mask
23131f75bfeSJohn Johansen  *
23231f75bfeSJohn Johansen  * Returns: %0 else error code if error or permission denied
23331f75bfeSJohn Johansen  */
common_perm_cond(const char * op,const struct path * path,u32 mask)23431f75bfeSJohn Johansen static int common_perm_cond(const char *op, const struct path *path, u32 mask)
23531f75bfeSJohn Johansen {
236e67fe633SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
2375e26a01eSChristian Brauner 					    d_backing_inode(path->dentry));
2383cee6079SChristian Brauner 	struct path_cond cond = {
2395e26a01eSChristian Brauner 		vfsuid_into_kuid(vfsuid),
24031f75bfeSJohn Johansen 		d_backing_inode(path->dentry)->i_mode
24131f75bfeSJohn Johansen 	};
24231f75bfeSJohn Johansen 
24331f75bfeSJohn Johansen 	if (!path_mediated_fs(path->dentry))
24431f75bfeSJohn Johansen 		return 0;
24531f75bfeSJohn Johansen 
24631f75bfeSJohn Johansen 	return common_perm(op, path, mask, &cond);
24731f75bfeSJohn Johansen }
24831f75bfeSJohn Johansen 
24931f75bfeSJohn Johansen /**
250b5e95b48SJohn Johansen  * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
251b5e95b48SJohn Johansen  * @op: operation being checked
252b5e95b48SJohn Johansen  * @dir: directory of the dentry  (NOT NULL)
253b5e95b48SJohn Johansen  * @dentry: dentry to check  (NOT NULL)
254b5e95b48SJohn Johansen  * @mask: requested permissions mask
255b5e95b48SJohn Johansen  * @cond: conditional info for the permission request  (NOT NULL)
256b5e95b48SJohn Johansen  *
257b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
258b5e95b48SJohn Johansen  */
common_perm_dir_dentry(const char * op,const struct path * dir,struct dentry * dentry,u32 mask,struct path_cond * cond)25947f6e5ccSJohn Johansen static int common_perm_dir_dentry(const char *op, const struct path *dir,
260b5e95b48SJohn Johansen 				  struct dentry *dentry, u32 mask,
261b5e95b48SJohn Johansen 				  struct path_cond *cond)
262b5e95b48SJohn Johansen {
2638486adf0SKees Cook 	struct path path = { .mnt = dir->mnt, .dentry = dentry };
264b5e95b48SJohn Johansen 
265b5e95b48SJohn Johansen 	return common_perm(op, &path, mask, cond);
266b5e95b48SJohn Johansen }
267b5e95b48SJohn Johansen 
268b5e95b48SJohn Johansen /**
269b5e95b48SJohn Johansen  * common_perm_rm - common permission wrapper for operations doing rm
270b5e95b48SJohn Johansen  * @op: operation being checked
271b5e95b48SJohn Johansen  * @dir: directory that the dentry is in  (NOT NULL)
272b5e95b48SJohn Johansen  * @dentry: dentry being rm'd  (NOT NULL)
273b5e95b48SJohn Johansen  * @mask: requested permission mask
274b5e95b48SJohn Johansen  *
275b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
276b5e95b48SJohn Johansen  */
common_perm_rm(const char * op,const struct path * dir,struct dentry * dentry,u32 mask)27747f6e5ccSJohn Johansen static int common_perm_rm(const char *op, const struct path *dir,
278b5e95b48SJohn Johansen 			  struct dentry *dentry, u32 mask)
279b5e95b48SJohn Johansen {
280c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
281b5e95b48SJohn Johansen 	struct path_cond cond = { };
2825e26a01eSChristian Brauner 	vfsuid_t vfsuid;
283b5e95b48SJohn Johansen 
284efeee83aSJohn Johansen 	if (!inode || !path_mediated_fs(dentry))
285b5e95b48SJohn Johansen 		return 0;
286b5e95b48SJohn Johansen 
287e67fe633SChristian Brauner 	vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
2885e26a01eSChristian Brauner 	cond.uid = vfsuid_into_kuid(vfsuid);
289b5e95b48SJohn Johansen 	cond.mode = inode->i_mode;
290b5e95b48SJohn Johansen 
291b5e95b48SJohn Johansen 	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
292b5e95b48SJohn Johansen }
293b5e95b48SJohn Johansen 
294b5e95b48SJohn Johansen /**
295b5e95b48SJohn Johansen  * common_perm_create - common permission wrapper for operations doing create
296b5e95b48SJohn Johansen  * @op: operation being checked
297b5e95b48SJohn Johansen  * @dir: directory that dentry will be created in  (NOT NULL)
298b5e95b48SJohn Johansen  * @dentry: dentry to create   (NOT NULL)
299b5e95b48SJohn Johansen  * @mask: request permission mask
300b5e95b48SJohn Johansen  * @mode: created file mode
301b5e95b48SJohn Johansen  *
302b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
303b5e95b48SJohn Johansen  */
common_perm_create(const char * op,const struct path * dir,struct dentry * dentry,u32 mask,umode_t mode)30447f6e5ccSJohn Johansen static int common_perm_create(const char *op, const struct path *dir,
305d6b49f7aSAl Viro 			      struct dentry *dentry, u32 mask, umode_t mode)
306b5e95b48SJohn Johansen {
307b5e95b48SJohn Johansen 	struct path_cond cond = { current_fsuid(), mode };
308b5e95b48SJohn Johansen 
309efeee83aSJohn Johansen 	if (!path_mediated_fs(dir->dentry))
310b5e95b48SJohn Johansen 		return 0;
311b5e95b48SJohn Johansen 
312b5e95b48SJohn Johansen 	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
313b5e95b48SJohn Johansen }
314b5e95b48SJohn Johansen 
apparmor_path_unlink(const struct path * dir,struct dentry * dentry)315989f74e0SAl Viro static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
316b5e95b48SJohn Johansen {
317b5e95b48SJohn Johansen 	return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
318b5e95b48SJohn Johansen }
319b5e95b48SJohn Johansen 
apparmor_path_mkdir(const struct path * dir,struct dentry * dentry,umode_t mode)320d3607752SAl Viro static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
3214572befeSAl Viro 			       umode_t mode)
322b5e95b48SJohn Johansen {
323b5e95b48SJohn Johansen 	return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
324b5e95b48SJohn Johansen 				  S_IFDIR);
325b5e95b48SJohn Johansen }
326b5e95b48SJohn Johansen 
apparmor_path_rmdir(const struct path * dir,struct dentry * dentry)327989f74e0SAl Viro static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
328b5e95b48SJohn Johansen {
329b5e95b48SJohn Johansen 	return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
330b5e95b48SJohn Johansen }
331b5e95b48SJohn Johansen 
apparmor_path_mknod(const struct path * dir,struct dentry * dentry,umode_t mode,unsigned int dev)332d3607752SAl Viro static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
33304fc66e7SAl Viro 			       umode_t mode, unsigned int dev)
334b5e95b48SJohn Johansen {
335b5e95b48SJohn Johansen 	return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
336b5e95b48SJohn Johansen }
337b5e95b48SJohn Johansen 
apparmor_path_truncate(const struct path * path)33881f4c506SAl Viro static int apparmor_path_truncate(const struct path *path)
339b5e95b48SJohn Johansen {
340e53cfe6cSJohn Johansen 	return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
341b5e95b48SJohn Johansen }
342b5e95b48SJohn Johansen 
apparmor_file_truncate(struct file * file)3433350607dSGünther Noack static int apparmor_file_truncate(struct file *file)
3443350607dSGünther Noack {
3453350607dSGünther Noack 	return apparmor_path_truncate(&file->f_path);
3463350607dSGünther Noack }
3473350607dSGünther Noack 
apparmor_path_symlink(const struct path * dir,struct dentry * dentry,const char * old_name)348d3607752SAl Viro static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
349b5e95b48SJohn Johansen 				 const char *old_name)
350b5e95b48SJohn Johansen {
351b5e95b48SJohn Johansen 	return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
352b5e95b48SJohn Johansen 				  S_IFLNK);
353b5e95b48SJohn Johansen }
354b5e95b48SJohn Johansen 
apparmor_path_link(struct dentry * old_dentry,const struct path * new_dir,struct dentry * new_dentry)3553ccee46aSAl Viro static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
356b5e95b48SJohn Johansen 			      struct dentry *new_dentry)
357b5e95b48SJohn Johansen {
358637f688dSJohn Johansen 	struct aa_label *label;
359b5e95b48SJohn Johansen 	int error = 0;
360b5e95b48SJohn Johansen 
361efeee83aSJohn Johansen 	if (!path_mediated_fs(old_dentry))
362b5e95b48SJohn Johansen 		return 0;
363b5e95b48SJohn Johansen 
364637f688dSJohn Johansen 	label = begin_current_label_crit_section();
365637f688dSJohn Johansen 	if (!unconfined(label))
366690f33e1SJohn Johansen 		error = aa_path_link(current_cred(), label, old_dentry, new_dir,
367690f33e1SJohn Johansen 				     new_dentry);
368637f688dSJohn Johansen 	end_current_label_crit_section(label);
369cf797c0eSJohn Johansen 
370b5e95b48SJohn Johansen 	return error;
371b5e95b48SJohn Johansen }
372b5e95b48SJohn Johansen 
apparmor_path_rename(const struct path * old_dir,struct dentry * old_dentry,const struct path * new_dir,struct dentry * new_dentry,const unsigned int flags)3733ccee46aSAl Viro static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
374100f59d9SMickaël Salaün 				const struct path *new_dir, struct dentry *new_dentry,
375100f59d9SMickaël Salaün 				const unsigned int flags)
376b5e95b48SJohn Johansen {
377637f688dSJohn Johansen 	struct aa_label *label;
378b5e95b48SJohn Johansen 	int error = 0;
379b5e95b48SJohn Johansen 
380efeee83aSJohn Johansen 	if (!path_mediated_fs(old_dentry))
381b5e95b48SJohn Johansen 		return 0;
382100f59d9SMickaël Salaün 	if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
383100f59d9SMickaël Salaün 		return 0;
384b5e95b48SJohn Johansen 
385637f688dSJohn Johansen 	label = begin_current_label_crit_section();
386637f688dSJohn Johansen 	if (!unconfined(label)) {
387e67fe633SChristian Brauner 		struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
3885e26a01eSChristian Brauner 		vfsuid_t vfsuid;
3898486adf0SKees Cook 		struct path old_path = { .mnt = old_dir->mnt,
3908486adf0SKees Cook 					 .dentry = old_dentry };
3918486adf0SKees Cook 		struct path new_path = { .mnt = new_dir->mnt,
3928486adf0SKees Cook 					 .dentry = new_dentry };
3933cee6079SChristian Brauner 		struct path_cond cond = {
3945e26a01eSChristian Brauner 			.mode = d_backing_inode(old_dentry)->i_mode
395b5e95b48SJohn Johansen 		};
396e67fe633SChristian Brauner 		vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
3975e26a01eSChristian Brauner 		cond.uid = vfsuid_into_kuid(vfsuid);
398b5e95b48SJohn Johansen 
399100f59d9SMickaël Salaün 		if (flags & RENAME_EXCHANGE) {
400100f59d9SMickaël Salaün 			struct path_cond cond_exchange = {
4015e26a01eSChristian Brauner 				.mode = d_backing_inode(new_dentry)->i_mode,
402100f59d9SMickaël Salaün 			};
403e67fe633SChristian Brauner 			vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
4045e26a01eSChristian Brauner 			cond_exchange.uid = vfsuid_into_kuid(vfsuid);
405100f59d9SMickaël Salaün 
406690f33e1SJohn Johansen 			error = aa_path_perm(OP_RENAME_SRC, current_cred(),
407690f33e1SJohn Johansen 					     label, &new_path, 0,
408100f59d9SMickaël Salaün 					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
409100f59d9SMickaël Salaün 					     AA_MAY_SETATTR | AA_MAY_DELETE,
410100f59d9SMickaël Salaün 					     &cond_exchange);
411100f59d9SMickaël Salaün 			if (!error)
412690f33e1SJohn Johansen 				error = aa_path_perm(OP_RENAME_DEST, current_cred(),
413690f33e1SJohn Johansen 						     label, &old_path,
414100f59d9SMickaël Salaün 						     0, MAY_WRITE | AA_MAY_SETATTR |
415100f59d9SMickaël Salaün 						     AA_MAY_CREATE, &cond_exchange);
416100f59d9SMickaël Salaün 		}
417100f59d9SMickaël Salaün 
418100f59d9SMickaël Salaün 		if (!error)
419690f33e1SJohn Johansen 			error = aa_path_perm(OP_RENAME_SRC, current_cred(),
420690f33e1SJohn Johansen 					     label, &old_path, 0,
421e53cfe6cSJohn Johansen 					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
422e53cfe6cSJohn Johansen 					     AA_MAY_SETATTR | AA_MAY_DELETE,
423b5e95b48SJohn Johansen 					     &cond);
424b5e95b48SJohn Johansen 		if (!error)
425690f33e1SJohn Johansen 			error = aa_path_perm(OP_RENAME_DEST, current_cred(),
426690f33e1SJohn Johansen 					     label, &new_path,
427e53cfe6cSJohn Johansen 					     0, MAY_WRITE | AA_MAY_SETATTR |
428b5e95b48SJohn Johansen 					     AA_MAY_CREATE, &cond);
429b5e95b48SJohn Johansen 
430b5e95b48SJohn Johansen 	}
431637f688dSJohn Johansen 	end_current_label_crit_section(label);
432cf797c0eSJohn Johansen 
433b5e95b48SJohn Johansen 	return error;
434b5e95b48SJohn Johansen }
435b5e95b48SJohn Johansen 
apparmor_path_chmod(const struct path * path,umode_t mode)436be01f9f2SAl Viro static int apparmor_path_chmod(const struct path *path, umode_t mode)
437b5e95b48SJohn Johansen {
43831f75bfeSJohn Johansen 	return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
439b5e95b48SJohn Johansen }
440b5e95b48SJohn Johansen 
apparmor_path_chown(const struct path * path,kuid_t uid,kgid_t gid)4417fd25dacSAl Viro static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
442b5e95b48SJohn Johansen {
44331f75bfeSJohn Johansen 	return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
444b5e95b48SJohn Johansen }
445b5e95b48SJohn Johansen 
apparmor_inode_getattr(const struct path * path)4463f7036a0SAl Viro static int apparmor_inode_getattr(const struct path *path)
447b5e95b48SJohn Johansen {
448e53cfe6cSJohn Johansen 	return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
449b5e95b48SJohn Johansen }
450b5e95b48SJohn Johansen 
apparmor_file_open(struct file * file)45194817692SAl Viro static int apparmor_file_open(struct file *file)
452b5e95b48SJohn Johansen {
453637f688dSJohn Johansen 	struct aa_file_ctx *fctx = file_ctx(file);
454637f688dSJohn Johansen 	struct aa_label *label;
455b5e95b48SJohn Johansen 	int error = 0;
456b5e95b48SJohn Johansen 
457efeee83aSJohn Johansen 	if (!path_mediated_fs(file->f_path.dentry))
458b5e95b48SJohn Johansen 		return 0;
459b5e95b48SJohn Johansen 
460b5e95b48SJohn Johansen 	/* If in exec, permission is handled by bprm hooks.
461b5e95b48SJohn Johansen 	 * Cache permissions granted by the previous exec check, with
462b5e95b48SJohn Johansen 	 * implicit read and executable mmap which are required to
463b5e95b48SJohn Johansen 	 * actually execute the image.
464b5e95b48SJohn Johansen 	 */
465b5e95b48SJohn Johansen 	if (current->in_execve) {
46655a26ebfSJohn Johansen 		fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
467b5e95b48SJohn Johansen 		return 0;
468b5e95b48SJohn Johansen 	}
469b5e95b48SJohn Johansen 
47094817692SAl Viro 	label = aa_get_newest_cred_label(file->f_cred);
471637f688dSJohn Johansen 	if (!unconfined(label)) {
472e67fe633SChristian Brauner 		struct mnt_idmap *idmap = file_mnt_idmap(file);
473496ad9aaSAl Viro 		struct inode *inode = file_inode(file);
4745e26a01eSChristian Brauner 		vfsuid_t vfsuid;
4753cee6079SChristian Brauner 		struct path_cond cond = {
4765e26a01eSChristian Brauner 			.mode = inode->i_mode,
4773cee6079SChristian Brauner 		};
478e67fe633SChristian Brauner 		vfsuid = i_uid_into_vfsuid(idmap, inode);
4795e26a01eSChristian Brauner 		cond.uid = vfsuid_into_kuid(vfsuid);
480b5e95b48SJohn Johansen 
481690f33e1SJohn Johansen 		error = aa_path_perm(OP_OPEN, file->f_cred,
482690f33e1SJohn Johansen 				     label, &file->f_path, 0,
483b5e95b48SJohn Johansen 				     aa_map_file_to_perms(file), &cond);
484b5e95b48SJohn Johansen 		/* todo cache full allowed permissions set and state */
48555a26ebfSJohn Johansen 		fctx->allow = aa_map_file_to_perms(file);
486b5e95b48SJohn Johansen 	}
487637f688dSJohn Johansen 	aa_put_label(label);
488b5e95b48SJohn Johansen 
489b5e95b48SJohn Johansen 	return error;
490b5e95b48SJohn Johansen }
491b5e95b48SJohn Johansen 
apparmor_file_alloc_security(struct file * file)492b5e95b48SJohn Johansen static int apparmor_file_alloc_security(struct file *file)
493b5e95b48SJohn Johansen {
49433bf60caSCasey Schaufler 	struct aa_file_ctx *ctx = file_ctx(file);
495637f688dSJohn Johansen 	struct aa_label *label = begin_current_label_crit_section();
496b5e95b48SJohn Johansen 
49733bf60caSCasey Schaufler 	spin_lock_init(&ctx->lock);
49833bf60caSCasey Schaufler 	rcu_assign_pointer(ctx->label, aa_get_label(label));
49933bf60caSCasey Schaufler 	end_current_label_crit_section(label);
50033bf60caSCasey Schaufler 	return 0;
501b5e95b48SJohn Johansen }
502b5e95b48SJohn Johansen 
apparmor_file_free_security(struct file * file)503b5e95b48SJohn Johansen static void apparmor_file_free_security(struct file *file)
504b5e95b48SJohn Johansen {
50533bf60caSCasey Schaufler 	struct aa_file_ctx *ctx = file_ctx(file);
50633bf60caSCasey Schaufler 
50733bf60caSCasey Schaufler 	if (ctx)
50833bf60caSCasey Schaufler 		aa_put_label(rcu_access_pointer(ctx->label));
509b5e95b48SJohn Johansen }
510b5e95b48SJohn Johansen 
common_file_perm(const char * op,struct file * file,u32 mask,bool in_atomic)511341c1fdaSJohn Johansen static int common_file_perm(const char *op, struct file *file, u32 mask,
512341c1fdaSJohn Johansen 			    bool in_atomic)
513b5e95b48SJohn Johansen {
514190a9518SJohn Johansen 	struct aa_label *label;
515b5e95b48SJohn Johansen 	int error = 0;
516b5e95b48SJohn Johansen 
517192ca6b5SJohn Johansen 	/* don't reaudit files closed during inheritance */
518192ca6b5SJohn Johansen 	if (file->f_path.dentry == aa_null.dentry)
519192ca6b5SJohn Johansen 		return -EACCES;
520192ca6b5SJohn Johansen 
521637f688dSJohn Johansen 	label = __begin_current_label_crit_section();
522690f33e1SJohn Johansen 	error = aa_file_perm(op, current_cred(), label, file, mask, in_atomic);
523637f688dSJohn Johansen 	__end_current_label_crit_section(label);
524b5e95b48SJohn Johansen 
525b5e95b48SJohn Johansen 	return error;
526b5e95b48SJohn Johansen }
527b5e95b48SJohn Johansen 
apparmor_file_receive(struct file * file)528064dc947SJohn Johansen static int apparmor_file_receive(struct file *file)
529064dc947SJohn Johansen {
530341c1fdaSJohn Johansen 	return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
531341c1fdaSJohn Johansen 				false);
532064dc947SJohn Johansen }
533064dc947SJohn Johansen 
apparmor_file_permission(struct file * file,int mask)534b5e95b48SJohn Johansen static int apparmor_file_permission(struct file *file, int mask)
535b5e95b48SJohn Johansen {
536341c1fdaSJohn Johansen 	return common_file_perm(OP_FPERM, file, mask, false);
537b5e95b48SJohn Johansen }
538b5e95b48SJohn Johansen 
apparmor_file_lock(struct file * file,unsigned int cmd)539b5e95b48SJohn Johansen static int apparmor_file_lock(struct file *file, unsigned int cmd)
540b5e95b48SJohn Johansen {
541b5e95b48SJohn Johansen 	u32 mask = AA_MAY_LOCK;
542b5e95b48SJohn Johansen 
543b5e95b48SJohn Johansen 	if (cmd == F_WRLCK)
544b5e95b48SJohn Johansen 		mask |= MAY_WRITE;
545b5e95b48SJohn Johansen 
546341c1fdaSJohn Johansen 	return common_file_perm(OP_FLOCK, file, mask, false);
547b5e95b48SJohn Johansen }
548b5e95b48SJohn Johansen 
common_mmap(const char * op,struct file * file,unsigned long prot,unsigned long flags,bool in_atomic)54947f6e5ccSJohn Johansen static int common_mmap(const char *op, struct file *file, unsigned long prot,
550341c1fdaSJohn Johansen 		       unsigned long flags, bool in_atomic)
551b5e95b48SJohn Johansen {
552b5e95b48SJohn Johansen 	int mask = 0;
553b5e95b48SJohn Johansen 
554637f688dSJohn Johansen 	if (!file || !file_ctx(file))
555b5e95b48SJohn Johansen 		return 0;
556b5e95b48SJohn Johansen 
557b5e95b48SJohn Johansen 	if (prot & PROT_READ)
558b5e95b48SJohn Johansen 		mask |= MAY_READ;
559b5e95b48SJohn Johansen 	/*
560b5e95b48SJohn Johansen 	 * Private mappings don't require write perms since they don't
561b5e95b48SJohn Johansen 	 * write back to the files
562b5e95b48SJohn Johansen 	 */
563b5e95b48SJohn Johansen 	if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
564b5e95b48SJohn Johansen 		mask |= MAY_WRITE;
565b5e95b48SJohn Johansen 	if (prot & PROT_EXEC)
566b5e95b48SJohn Johansen 		mask |= AA_EXEC_MMAP;
567b5e95b48SJohn Johansen 
568341c1fdaSJohn Johansen 	return common_file_perm(op, file, mask, in_atomic);
569b5e95b48SJohn Johansen }
570b5e95b48SJohn Johansen 
apparmor_mmap_file(struct file * file,unsigned long reqprot,unsigned long prot,unsigned long flags)571e5467859SAl Viro static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
572e5467859SAl Viro 			      unsigned long prot, unsigned long flags)
573b5e95b48SJohn Johansen {
574341c1fdaSJohn Johansen 	return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
575b5e95b48SJohn Johansen }
576b5e95b48SJohn Johansen 
apparmor_file_mprotect(struct vm_area_struct * vma,unsigned long reqprot,unsigned long prot)577b5e95b48SJohn Johansen static int apparmor_file_mprotect(struct vm_area_struct *vma,
578b5e95b48SJohn Johansen 				  unsigned long reqprot, unsigned long prot)
579b5e95b48SJohn Johansen {
580b5e95b48SJohn Johansen 	return common_mmap(OP_FMPROT, vma->vm_file, prot,
581341c1fdaSJohn Johansen 			   !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
582341c1fdaSJohn Johansen 			   false);
583b5e95b48SJohn Johansen }
584b5e95b48SJohn Johansen 
apparmor_sb_mount(const char * dev_name,const struct path * path,const char * type,unsigned long flags,void * data)5852ea3ffb7SJohn Johansen static int apparmor_sb_mount(const char *dev_name, const struct path *path,
5862ea3ffb7SJohn Johansen 			     const char *type, unsigned long flags, void *data)
5872ea3ffb7SJohn Johansen {
5882ea3ffb7SJohn Johansen 	struct aa_label *label;
5892ea3ffb7SJohn Johansen 	int error = 0;
5902ea3ffb7SJohn Johansen 
5912ea3ffb7SJohn Johansen 	/* Discard magic */
5922ea3ffb7SJohn Johansen 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
5932ea3ffb7SJohn Johansen 		flags &= ~MS_MGC_MSK;
5942ea3ffb7SJohn Johansen 
5952ea3ffb7SJohn Johansen 	flags &= ~AA_MS_IGNORE_MASK;
5962ea3ffb7SJohn Johansen 
5972ea3ffb7SJohn Johansen 	label = __begin_current_label_crit_section();
5982ea3ffb7SJohn Johansen 	if (!unconfined(label)) {
5992ea3ffb7SJohn Johansen 		if (flags & MS_REMOUNT)
600690f33e1SJohn Johansen 			error = aa_remount(current_cred(), label, path, flags,
601690f33e1SJohn Johansen 					   data);
6022ea3ffb7SJohn Johansen 		else if (flags & MS_BIND)
603690f33e1SJohn Johansen 			error = aa_bind_mount(current_cred(), label, path,
604690f33e1SJohn Johansen 					      dev_name, flags);
6052ea3ffb7SJohn Johansen 		else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
6062ea3ffb7SJohn Johansen 				  MS_UNBINDABLE))
607690f33e1SJohn Johansen 			error = aa_mount_change_type(current_cred(), label,
608690f33e1SJohn Johansen 						     path, flags);
6092ea3ffb7SJohn Johansen 		else if (flags & MS_MOVE)
61096af4515SJohn Johansen 			error = aa_move_mount_old(current_cred(), label, path,
611690f33e1SJohn Johansen 						  dev_name);
6122ea3ffb7SJohn Johansen 		else
613690f33e1SJohn Johansen 			error = aa_new_mount(current_cred(), label, dev_name,
614690f33e1SJohn Johansen 					     path, type, flags, data);
6152ea3ffb7SJohn Johansen 	}
6162ea3ffb7SJohn Johansen 	__end_current_label_crit_section(label);
6172ea3ffb7SJohn Johansen 
6182ea3ffb7SJohn Johansen 	return error;
6192ea3ffb7SJohn Johansen }
6202ea3ffb7SJohn Johansen 
apparmor_move_mount(const struct path * from_path,const struct path * to_path)62196af4515SJohn Johansen static int apparmor_move_mount(const struct path *from_path,
62296af4515SJohn Johansen 			       const struct path *to_path)
62396af4515SJohn Johansen {
62496af4515SJohn Johansen 	struct aa_label *label;
62596af4515SJohn Johansen 	int error = 0;
62696af4515SJohn Johansen 
62796af4515SJohn Johansen 	label = __begin_current_label_crit_section();
62896af4515SJohn Johansen 	if (!unconfined(label))
62996af4515SJohn Johansen 		error = aa_move_mount(current_cred(), label, from_path,
63096af4515SJohn Johansen 				      to_path);
63196af4515SJohn Johansen 	__end_current_label_crit_section(label);
63296af4515SJohn Johansen 
63396af4515SJohn Johansen 	return error;
63496af4515SJohn Johansen }
63596af4515SJohn Johansen 
apparmor_sb_umount(struct vfsmount * mnt,int flags)6362ea3ffb7SJohn Johansen static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
6372ea3ffb7SJohn Johansen {
6382ea3ffb7SJohn Johansen 	struct aa_label *label;
6392ea3ffb7SJohn Johansen 	int error = 0;
6402ea3ffb7SJohn Johansen 
6412ea3ffb7SJohn Johansen 	label = __begin_current_label_crit_section();
6422ea3ffb7SJohn Johansen 	if (!unconfined(label))
643690f33e1SJohn Johansen 		error = aa_umount(current_cred(), label, mnt, flags);
6442ea3ffb7SJohn Johansen 	__end_current_label_crit_section(label);
6452ea3ffb7SJohn Johansen 
6462ea3ffb7SJohn Johansen 	return error;
6472ea3ffb7SJohn Johansen }
6482ea3ffb7SJohn Johansen 
apparmor_sb_pivotroot(const struct path * old_path,const struct path * new_path)6492ea3ffb7SJohn Johansen static int apparmor_sb_pivotroot(const struct path *old_path,
6502ea3ffb7SJohn Johansen 				 const struct path *new_path)
6512ea3ffb7SJohn Johansen {
6522ea3ffb7SJohn Johansen 	struct aa_label *label;
6532ea3ffb7SJohn Johansen 	int error = 0;
6542ea3ffb7SJohn Johansen 
6552ea3ffb7SJohn Johansen 	label = aa_get_current_label();
6562ea3ffb7SJohn Johansen 	if (!unconfined(label))
657690f33e1SJohn Johansen 		error = aa_pivotroot(current_cred(), label, old_path, new_path);
6582ea3ffb7SJohn Johansen 	aa_put_label(label);
6592ea3ffb7SJohn Johansen 
6602ea3ffb7SJohn Johansen 	return error;
6612ea3ffb7SJohn Johansen }
6622ea3ffb7SJohn Johansen 
apparmor_getprocattr(struct task_struct * task,const char * name,char ** value)663c8e477c6SAl Viro static int apparmor_getprocattr(struct task_struct *task, const char *name,
664b5e95b48SJohn Johansen 				char **value)
665b5e95b48SJohn Johansen {
666b5e95b48SJohn Johansen 	int error = -ENOENT;
667b5e95b48SJohn Johansen 	/* released below */
668b5e95b48SJohn Johansen 	const struct cred *cred = get_task_cred(task);
669de62de59SJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
670637f688dSJohn Johansen 	struct aa_label *label = NULL;
671b5e95b48SJohn Johansen 
672b5e95b48SJohn Johansen 	if (strcmp(name, "current") == 0)
673d9087c49SJohn Johansen 		label = aa_get_newest_label(cred_label(cred));
67455a26ebfSJohn Johansen 	else if (strcmp(name, "prev") == 0  && ctx->previous)
675637f688dSJohn Johansen 		label = aa_get_newest_label(ctx->previous);
67655a26ebfSJohn Johansen 	else if (strcmp(name, "exec") == 0 && ctx->onexec)
677637f688dSJohn Johansen 		label = aa_get_newest_label(ctx->onexec);
678b5e95b48SJohn Johansen 	else
679b5e95b48SJohn Johansen 		error = -EINVAL;
680b5e95b48SJohn Johansen 
681637f688dSJohn Johansen 	if (label)
68276a1d263SJohn Johansen 		error = aa_getprocattr(label, value);
68377b071b3SJohn Johansen 
684637f688dSJohn Johansen 	aa_put_label(label);
685b5e95b48SJohn Johansen 	put_cred(cred);
686b5e95b48SJohn Johansen 
687b5e95b48SJohn Johansen 	return error;
688b5e95b48SJohn Johansen }
689b5e95b48SJohn Johansen 
apparmor_setprocattr(const char * name,void * value,size_t size)690b21507e2SStephen Smalley static int apparmor_setprocattr(const char *name, void *value,
691b21507e2SStephen Smalley 				size_t size)
692b5e95b48SJohn Johansen {
693e89b8081SVegard Nossum 	char *command, *largs = NULL, *args = value;
694b5e95b48SJohn Johansen 	size_t arg_size;
695b5e95b48SJohn Johansen 	int error;
696c57bc80fSJohn Johansen 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
6978c4b785aSJohn Johansen 			  OP_SETPROCATTR);
698b5e95b48SJohn Johansen 
699b5e95b48SJohn Johansen 	if (size == 0)
700b5e95b48SJohn Johansen 		return -EINVAL;
701b5e95b48SJohn Johansen 
702e89b8081SVegard Nossum 	/* AppArmor requires that the buffer must be null terminated atm */
703e89b8081SVegard Nossum 	if (args[size - 1] != '\0') {
704e89b8081SVegard Nossum 		/* null terminate */
705e89b8081SVegard Nossum 		largs = args = kmalloc(size + 1, GFP_KERNEL);
706e89b8081SVegard Nossum 		if (!args)
707e89b8081SVegard Nossum 			return -ENOMEM;
708e89b8081SVegard Nossum 		memcpy(args, value, size);
709e89b8081SVegard Nossum 		args[size] = '\0';
710e89b8081SVegard Nossum 	}
711e89b8081SVegard Nossum 
712e89b8081SVegard Nossum 	error = -EINVAL;
713b5e95b48SJohn Johansen 	args = strim(args);
714b5e95b48SJohn Johansen 	command = strsep(&args, " ");
715b5e95b48SJohn Johansen 	if (!args)
716e89b8081SVegard Nossum 		goto out;
717b5e95b48SJohn Johansen 	args = skip_spaces(args);
718b5e95b48SJohn Johansen 	if (!*args)
719e89b8081SVegard Nossum 		goto out;
720b5e95b48SJohn Johansen 
721d4d03f74SJohn Johansen 	arg_size = size - (args - (largs ? largs : (char *) value));
722b5e95b48SJohn Johansen 	if (strcmp(name, "current") == 0) {
723b5e95b48SJohn Johansen 		if (strcmp(command, "changehat") == 0) {
724b5e95b48SJohn Johansen 			error = aa_setprocattr_changehat(args, arg_size,
725df8073c6SJohn Johansen 							 AA_CHANGE_NOFLAGS);
726b5e95b48SJohn Johansen 		} else if (strcmp(command, "permhat") == 0) {
727b5e95b48SJohn Johansen 			error = aa_setprocattr_changehat(args, arg_size,
728df8073c6SJohn Johansen 							 AA_CHANGE_TEST);
729b5e95b48SJohn Johansen 		} else if (strcmp(command, "changeprofile") == 0) {
730df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
731b5e95b48SJohn Johansen 		} else if (strcmp(command, "permprofile") == 0) {
732df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_TEST);
7336c5fc8f1SJohn Johansen 		} else if (strcmp(command, "stack") == 0) {
7346c5fc8f1SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_STACK);
7353eea57c2SJohn Johansen 		} else
7363eea57c2SJohn Johansen 			goto fail;
737b5e95b48SJohn Johansen 	} else if (strcmp(name, "exec") == 0) {
7383eea57c2SJohn Johansen 		if (strcmp(command, "exec") == 0)
739df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_ONEXEC);
7406c5fc8f1SJohn Johansen 		else if (strcmp(command, "stack") == 0)
7416c5fc8f1SJohn Johansen 			error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
7426c5fc8f1SJohn Johansen 							 AA_CHANGE_STACK));
7433eea57c2SJohn Johansen 		else
7443eea57c2SJohn Johansen 			goto fail;
7453eea57c2SJohn Johansen 	} else
746b5e95b48SJohn Johansen 		/* only support the "current" and "exec" process attributes */
747e89b8081SVegard Nossum 		goto fail;
7483eea57c2SJohn Johansen 
749b5e95b48SJohn Johansen 	if (!error)
750b5e95b48SJohn Johansen 		error = size;
751e89b8081SVegard Nossum out:
752e89b8081SVegard Nossum 	kfree(largs);
753b5e95b48SJohn Johansen 	return error;
7543eea57c2SJohn Johansen 
7553eea57c2SJohn Johansen fail:
75630b3669dSJohn Johansen 	ad.subj_label = begin_current_label_crit_section();
757c57bc80fSJohn Johansen 	ad.info = name;
758c57bc80fSJohn Johansen 	ad.error = error = -EINVAL;
759c57bc80fSJohn Johansen 	aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL);
76030b3669dSJohn Johansen 	end_current_label_crit_section(ad.subj_label);
761e89b8081SVegard Nossum 	goto out;
762b5e95b48SJohn Johansen }
763b5e95b48SJohn Johansen 
764fe864821SJohn Johansen /**
765fe864821SJohn Johansen  * apparmor_bprm_committing_creds - do task cleanup on committing new creds
766fe864821SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
767fe864821SJohn Johansen  */
apparmor_bprm_committing_creds(struct linux_binprm * bprm)768fe864821SJohn Johansen static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
769fe864821SJohn Johansen {
770637f688dSJohn Johansen 	struct aa_label *label = aa_current_raw_label();
771d9087c49SJohn Johansen 	struct aa_label *new_label = cred_label(bprm->cred);
772fe864821SJohn Johansen 
773fe864821SJohn Johansen 	/* bail out if unconfined or not changing profile */
774d9087c49SJohn Johansen 	if ((new_label->proxy == label->proxy) ||
775d9087c49SJohn Johansen 	    (unconfined(new_label)))
776fe864821SJohn Johansen 		return;
777fe864821SJohn Johansen 
778192ca6b5SJohn Johansen 	aa_inherit_files(bprm->cred, current->files);
779192ca6b5SJohn Johansen 
780fe864821SJohn Johansen 	current->pdeath_signal = 0;
781fe864821SJohn Johansen 
782637f688dSJohn Johansen 	/* reset soft limits and set hard limits for the new label */
783d9087c49SJohn Johansen 	__aa_transition_rlimits(label, new_label);
784fe864821SJohn Johansen }
785fe864821SJohn Johansen 
786fe864821SJohn Johansen /**
787391f1211SJiapeng Chong  * apparmor_bprm_committed_creds() - do cleanup after new creds committed
788fe864821SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
789fe864821SJohn Johansen  */
apparmor_bprm_committed_creds(struct linux_binprm * bprm)790fe864821SJohn Johansen static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
791fe864821SJohn Johansen {
7923b529a76SJohn Johansen 	/* clear out temporary/transitional state from the context */
793de62de59SJohn Johansen 	aa_clear_task_ctx_trans(task_ctx(current));
7943b529a76SJohn Johansen 
795fe864821SJohn Johansen 	return;
796fe864821SJohn Johansen }
797fe864821SJohn Johansen 
apparmor_current_getsecid_subj(u32 * secid)7986326948fSPaul Moore static void apparmor_current_getsecid_subj(u32 *secid)
7996326948fSPaul Moore {
8006326948fSPaul Moore 	struct aa_label *label = aa_get_current_label();
8016326948fSPaul Moore 	*secid = label->secid;
8026326948fSPaul Moore 	aa_put_label(label);
8036326948fSPaul Moore }
8046326948fSPaul Moore 
apparmor_task_getsecid_obj(struct task_struct * p,u32 * secid)8056326948fSPaul Moore static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
806a7ae3645SJohn Johansen {
807a7ae3645SJohn Johansen 	struct aa_label *label = aa_get_task_label(p);
808a7ae3645SJohn Johansen 	*secid = label->secid;
809a7ae3645SJohn Johansen 	aa_put_label(label);
810a7ae3645SJohn Johansen }
811a7ae3645SJohn Johansen 
apparmor_task_setrlimit(struct task_struct * task,unsigned int resource,struct rlimit * new_rlim)8127cb4dc9fSJiri Slaby static int apparmor_task_setrlimit(struct task_struct *task,
8137cb4dc9fSJiri Slaby 		unsigned int resource, struct rlimit *new_rlim)
814b5e95b48SJohn Johansen {
815637f688dSJohn Johansen 	struct aa_label *label = __begin_current_label_crit_section();
816b5e95b48SJohn Johansen 	int error = 0;
817b5e95b48SJohn Johansen 
818637f688dSJohn Johansen 	if (!unconfined(label))
819690f33e1SJohn Johansen 		error = aa_task_setrlimit(current_cred(), label, task,
820690f33e1SJohn Johansen 					  resource, new_rlim);
821637f688dSJohn Johansen 	__end_current_label_crit_section(label);
822b5e95b48SJohn Johansen 
823b5e95b48SJohn Johansen 	return error;
824b5e95b48SJohn Johansen }
825b5e95b48SJohn Johansen 
apparmor_task_kill(struct task_struct * target,struct kernel_siginfo * info,int sig,const struct cred * cred)826ae7795bcSEric W. Biederman static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
8276b4f3d01SStephen Smalley 			      int sig, const struct cred *cred)
828cd1dbf76SJohn Johansen {
829690f33e1SJohn Johansen 	const struct cred *tc;
830cd1dbf76SJohn Johansen 	struct aa_label *cl, *tl;
831cd1dbf76SJohn Johansen 	int error;
832cd1dbf76SJohn Johansen 
833690f33e1SJohn Johansen 	tc = get_task_cred(target);
834690f33e1SJohn Johansen 	tl = aa_get_newest_cred_label(tc);
8356b4f3d01SStephen Smalley 	if (cred) {
8366b4f3d01SStephen Smalley 		/*
837cd1dbf76SJohn Johansen 		 * Dealing with USB IO specific behavior
838cd1dbf76SJohn Johansen 		 */
8396b4f3d01SStephen Smalley 		cl = aa_get_newest_cred_label(cred);
840690f33e1SJohn Johansen 		error = aa_may_signal(cred, cl, tc, tl, sig);
8416b4f3d01SStephen Smalley 		aa_put_label(cl);
842690f33e1SJohn Johansen 	} else {
843cd1dbf76SJohn Johansen 		cl = __begin_current_label_crit_section();
844690f33e1SJohn Johansen 		error = aa_may_signal(current_cred(), cl, tc, tl, sig);
845cd1dbf76SJohn Johansen 		__end_current_label_crit_section(cl);
846690f33e1SJohn Johansen 	}
847690f33e1SJohn Johansen 	aa_put_label(tl);
848690f33e1SJohn Johansen 	put_cred(tc);
849cd1dbf76SJohn Johansen 
850cd1dbf76SJohn Johansen 	return error;
851cd1dbf76SJohn Johansen }
852cd1dbf76SJohn Johansen 
85356974a6fSJohn Johansen /**
85456974a6fSJohn Johansen  * apparmor_sk_alloc_security - allocate and attach the sk_security field
85556974a6fSJohn Johansen  */
apparmor_sk_alloc_security(struct sock * sk,int family,gfp_t flags)85656974a6fSJohn Johansen static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
85756974a6fSJohn Johansen {
85856974a6fSJohn Johansen 	struct aa_sk_ctx *ctx;
85956974a6fSJohn Johansen 
86056974a6fSJohn Johansen 	ctx = kzalloc(sizeof(*ctx), flags);
86156974a6fSJohn Johansen 	if (!ctx)
86256974a6fSJohn Johansen 		return -ENOMEM;
86356974a6fSJohn Johansen 
86456974a6fSJohn Johansen 	SK_CTX(sk) = ctx;
86556974a6fSJohn Johansen 
86656974a6fSJohn Johansen 	return 0;
86756974a6fSJohn Johansen }
86856974a6fSJohn Johansen 
86956974a6fSJohn Johansen /**
87056974a6fSJohn Johansen  * apparmor_sk_free_security - free the sk_security field
87156974a6fSJohn Johansen  */
apparmor_sk_free_security(struct sock * sk)87256974a6fSJohn Johansen static void apparmor_sk_free_security(struct sock *sk)
87356974a6fSJohn Johansen {
87456974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = SK_CTX(sk);
87556974a6fSJohn Johansen 
87656974a6fSJohn Johansen 	SK_CTX(sk) = NULL;
87756974a6fSJohn Johansen 	aa_put_label(ctx->label);
87856974a6fSJohn Johansen 	aa_put_label(ctx->peer);
87956974a6fSJohn Johansen 	kfree(ctx);
88056974a6fSJohn Johansen }
88156974a6fSJohn Johansen 
88256974a6fSJohn Johansen /**
8830fc6ab40SYang Li  * apparmor_sk_clone_security - clone the sk_security field
88456974a6fSJohn Johansen  */
apparmor_sk_clone_security(const struct sock * sk,struct sock * newsk)88556974a6fSJohn Johansen static void apparmor_sk_clone_security(const struct sock *sk,
88656974a6fSJohn Johansen 				       struct sock *newsk)
88756974a6fSJohn Johansen {
88856974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = SK_CTX(sk);
88956974a6fSJohn Johansen 	struct aa_sk_ctx *new = SK_CTX(newsk);
89056974a6fSJohn Johansen 
8913b646abcSMauricio Faria de Oliveira 	if (new->label)
8923b646abcSMauricio Faria de Oliveira 		aa_put_label(new->label);
89356974a6fSJohn Johansen 	new->label = aa_get_label(ctx->label);
8943b646abcSMauricio Faria de Oliveira 
8953b646abcSMauricio Faria de Oliveira 	if (new->peer)
8963b646abcSMauricio Faria de Oliveira 		aa_put_label(new->peer);
89756974a6fSJohn Johansen 	new->peer = aa_get_label(ctx->peer);
89856974a6fSJohn Johansen }
89956974a6fSJohn Johansen 
90056974a6fSJohn Johansen /**
90156974a6fSJohn Johansen  * apparmor_socket_create - check perms before creating a new socket
90256974a6fSJohn Johansen  */
apparmor_socket_create(int family,int type,int protocol,int kern)90356974a6fSJohn Johansen static int apparmor_socket_create(int family, int type, int protocol, int kern)
90456974a6fSJohn Johansen {
90556974a6fSJohn Johansen 	struct aa_label *label;
90656974a6fSJohn Johansen 	int error = 0;
90756974a6fSJohn Johansen 
90856974a6fSJohn Johansen 	AA_BUG(in_interrupt());
90956974a6fSJohn Johansen 
91056974a6fSJohn Johansen 	label = begin_current_label_crit_section();
91156974a6fSJohn Johansen 	if (!(kern || unconfined(label)))
91256974a6fSJohn Johansen 		error = af_select(family,
91356974a6fSJohn Johansen 				  create_perm(label, family, type, protocol),
914690f33e1SJohn Johansen 				  aa_af_perm(current_cred(), label,
915690f33e1SJohn Johansen 					     OP_CREATE, AA_MAY_CREATE,
91656974a6fSJohn Johansen 					     family, type, protocol));
91756974a6fSJohn Johansen 	end_current_label_crit_section(label);
91856974a6fSJohn Johansen 
91956974a6fSJohn Johansen 	return error;
92056974a6fSJohn Johansen }
92156974a6fSJohn Johansen 
92256974a6fSJohn Johansen /**
92356974a6fSJohn Johansen  * apparmor_socket_post_create - setup the per-socket security struct
92456974a6fSJohn Johansen  *
92556974a6fSJohn Johansen  * Note:
92656974a6fSJohn Johansen  * -   kernel sockets currently labeled unconfined but we may want to
92756974a6fSJohn Johansen  *     move to a special kernel label
92856974a6fSJohn Johansen  * -   socket may not have sk here if created with sock_create_lite or
92956974a6fSJohn Johansen  *     sock_alloc. These should be accept cases which will be handled in
93056974a6fSJohn Johansen  *     sock_graft.
93156974a6fSJohn Johansen  */
apparmor_socket_post_create(struct socket * sock,int family,int type,int protocol,int kern)93256974a6fSJohn Johansen static int apparmor_socket_post_create(struct socket *sock, int family,
93356974a6fSJohn Johansen 				       int type, int protocol, int kern)
93456974a6fSJohn Johansen {
93556974a6fSJohn Johansen 	struct aa_label *label;
93656974a6fSJohn Johansen 
93756974a6fSJohn Johansen 	if (kern) {
93895c0581fSJohn Johansen 		label = aa_get_label(kernel_t);
93956974a6fSJohn Johansen 	} else
94056974a6fSJohn Johansen 		label = aa_get_current_label();
94156974a6fSJohn Johansen 
94256974a6fSJohn Johansen 	if (sock->sk) {
94356974a6fSJohn Johansen 		struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
94456974a6fSJohn Johansen 
94556974a6fSJohn Johansen 		aa_put_label(ctx->label);
94656974a6fSJohn Johansen 		ctx->label = aa_get_label(label);
94756974a6fSJohn Johansen 	}
94856974a6fSJohn Johansen 	aa_put_label(label);
94956974a6fSJohn Johansen 
95056974a6fSJohn Johansen 	return 0;
95156974a6fSJohn Johansen }
95256974a6fSJohn Johansen 
95356974a6fSJohn Johansen /**
95456974a6fSJohn Johansen  * apparmor_socket_bind - check perms before bind addr to socket
95556974a6fSJohn Johansen  */
apparmor_socket_bind(struct socket * sock,struct sockaddr * address,int addrlen)95656974a6fSJohn Johansen static int apparmor_socket_bind(struct socket *sock,
95756974a6fSJohn Johansen 				struct sockaddr *address, int addrlen)
95856974a6fSJohn Johansen {
95956974a6fSJohn Johansen 	AA_BUG(!sock);
96056974a6fSJohn Johansen 	AA_BUG(!sock->sk);
96156974a6fSJohn Johansen 	AA_BUG(!address);
96256974a6fSJohn Johansen 	AA_BUG(in_interrupt());
96356974a6fSJohn Johansen 
96456974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
96556974a6fSJohn Johansen 			 bind_perm(sock, address, addrlen),
96656974a6fSJohn Johansen 			 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
96756974a6fSJohn Johansen }
96856974a6fSJohn Johansen 
96956974a6fSJohn Johansen /**
97056974a6fSJohn Johansen  * apparmor_socket_connect - check perms before connecting @sock to @address
97156974a6fSJohn Johansen  */
apparmor_socket_connect(struct socket * sock,struct sockaddr * address,int addrlen)97256974a6fSJohn Johansen static int apparmor_socket_connect(struct socket *sock,
97356974a6fSJohn Johansen 				   struct sockaddr *address, int addrlen)
97456974a6fSJohn Johansen {
97556974a6fSJohn Johansen 	AA_BUG(!sock);
97656974a6fSJohn Johansen 	AA_BUG(!sock->sk);
97756974a6fSJohn Johansen 	AA_BUG(!address);
97856974a6fSJohn Johansen 	AA_BUG(in_interrupt());
97956974a6fSJohn Johansen 
98056974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
98156974a6fSJohn Johansen 			 connect_perm(sock, address, addrlen),
98256974a6fSJohn Johansen 			 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
98356974a6fSJohn Johansen }
98456974a6fSJohn Johansen 
98556974a6fSJohn Johansen /**
9860fc6ab40SYang Li  * apparmor_socket_listen - check perms before allowing listen
98756974a6fSJohn Johansen  */
apparmor_socket_listen(struct socket * sock,int backlog)98856974a6fSJohn Johansen static int apparmor_socket_listen(struct socket *sock, int backlog)
98956974a6fSJohn Johansen {
99056974a6fSJohn Johansen 	AA_BUG(!sock);
99156974a6fSJohn Johansen 	AA_BUG(!sock->sk);
99256974a6fSJohn Johansen 	AA_BUG(in_interrupt());
99356974a6fSJohn Johansen 
99456974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
99556974a6fSJohn Johansen 			 listen_perm(sock, backlog),
99656974a6fSJohn Johansen 			 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
99756974a6fSJohn Johansen }
99856974a6fSJohn Johansen 
99956974a6fSJohn Johansen /**
100056974a6fSJohn Johansen  * apparmor_socket_accept - check perms before accepting a new connection.
100156974a6fSJohn Johansen  *
100256974a6fSJohn Johansen  * Note: while @newsock is created and has some information, the accept
100356974a6fSJohn Johansen  *       has not been done.
100456974a6fSJohn Johansen  */
apparmor_socket_accept(struct socket * sock,struct socket * newsock)100556974a6fSJohn Johansen static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
100656974a6fSJohn Johansen {
100756974a6fSJohn Johansen 	AA_BUG(!sock);
100856974a6fSJohn Johansen 	AA_BUG(!sock->sk);
100956974a6fSJohn Johansen 	AA_BUG(!newsock);
101056974a6fSJohn Johansen 	AA_BUG(in_interrupt());
101156974a6fSJohn Johansen 
101256974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
101356974a6fSJohn Johansen 			 accept_perm(sock, newsock),
101456974a6fSJohn Johansen 			 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
101556974a6fSJohn Johansen }
101656974a6fSJohn Johansen 
aa_sock_msg_perm(const char * op,u32 request,struct socket * sock,struct msghdr * msg,int size)101756974a6fSJohn Johansen static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
101856974a6fSJohn Johansen 			    struct msghdr *msg, int size)
101956974a6fSJohn Johansen {
102056974a6fSJohn Johansen 	AA_BUG(!sock);
102156974a6fSJohn Johansen 	AA_BUG(!sock->sk);
102256974a6fSJohn Johansen 	AA_BUG(!msg);
102356974a6fSJohn Johansen 	AA_BUG(in_interrupt());
102456974a6fSJohn Johansen 
102556974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
102656974a6fSJohn Johansen 			 msg_perm(op, request, sock, msg, size),
102756974a6fSJohn Johansen 			 aa_sk_perm(op, request, sock->sk));
102856974a6fSJohn Johansen }
102956974a6fSJohn Johansen 
103056974a6fSJohn Johansen /**
103156974a6fSJohn Johansen  * apparmor_socket_sendmsg - check perms before sending msg to another socket
103256974a6fSJohn Johansen  */
apparmor_socket_sendmsg(struct socket * sock,struct msghdr * msg,int size)103356974a6fSJohn Johansen static int apparmor_socket_sendmsg(struct socket *sock,
103456974a6fSJohn Johansen 				   struct msghdr *msg, int size)
103556974a6fSJohn Johansen {
103656974a6fSJohn Johansen 	return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
103756974a6fSJohn Johansen }
103856974a6fSJohn Johansen 
103956974a6fSJohn Johansen /**
104056974a6fSJohn Johansen  * apparmor_socket_recvmsg - check perms before receiving a message
104156974a6fSJohn Johansen  */
apparmor_socket_recvmsg(struct socket * sock,struct msghdr * msg,int size,int flags)104256974a6fSJohn Johansen static int apparmor_socket_recvmsg(struct socket *sock,
104356974a6fSJohn Johansen 				   struct msghdr *msg, int size, int flags)
104456974a6fSJohn Johansen {
104556974a6fSJohn Johansen 	return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
104656974a6fSJohn Johansen }
104756974a6fSJohn Johansen 
104856974a6fSJohn Johansen /* revaliation, get/set attr, shutdown */
aa_sock_perm(const char * op,u32 request,struct socket * sock)104956974a6fSJohn Johansen static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
105056974a6fSJohn Johansen {
105156974a6fSJohn Johansen 	AA_BUG(!sock);
105256974a6fSJohn Johansen 	AA_BUG(!sock->sk);
105356974a6fSJohn Johansen 	AA_BUG(in_interrupt());
105456974a6fSJohn Johansen 
105556974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
105656974a6fSJohn Johansen 			 sock_perm(op, request, sock),
105756974a6fSJohn Johansen 			 aa_sk_perm(op, request, sock->sk));
105856974a6fSJohn Johansen }
105956974a6fSJohn Johansen 
106056974a6fSJohn Johansen /**
106156974a6fSJohn Johansen  * apparmor_socket_getsockname - check perms before getting the local address
106256974a6fSJohn Johansen  */
apparmor_socket_getsockname(struct socket * sock)106356974a6fSJohn Johansen static int apparmor_socket_getsockname(struct socket *sock)
106456974a6fSJohn Johansen {
106556974a6fSJohn Johansen 	return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
106656974a6fSJohn Johansen }
106756974a6fSJohn Johansen 
106856974a6fSJohn Johansen /**
106956974a6fSJohn Johansen  * apparmor_socket_getpeername - check perms before getting remote address
107056974a6fSJohn Johansen  */
apparmor_socket_getpeername(struct socket * sock)107156974a6fSJohn Johansen static int apparmor_socket_getpeername(struct socket *sock)
107256974a6fSJohn Johansen {
107356974a6fSJohn Johansen 	return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
107456974a6fSJohn Johansen }
107556974a6fSJohn Johansen 
107656974a6fSJohn Johansen /* revaliation, get/set attr, opt */
aa_sock_opt_perm(const char * op,u32 request,struct socket * sock,int level,int optname)107756974a6fSJohn Johansen static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
107856974a6fSJohn Johansen 			    int level, int optname)
107956974a6fSJohn Johansen {
108056974a6fSJohn Johansen 	AA_BUG(!sock);
108156974a6fSJohn Johansen 	AA_BUG(!sock->sk);
108256974a6fSJohn Johansen 	AA_BUG(in_interrupt());
108356974a6fSJohn Johansen 
108456974a6fSJohn Johansen 	return af_select(sock->sk->sk_family,
108556974a6fSJohn Johansen 			 opt_perm(op, request, sock, level, optname),
108656974a6fSJohn Johansen 			 aa_sk_perm(op, request, sock->sk));
108756974a6fSJohn Johansen }
108856974a6fSJohn Johansen 
108956974a6fSJohn Johansen /**
10900fc6ab40SYang Li  * apparmor_socket_getsockopt - check perms before getting socket options
109156974a6fSJohn Johansen  */
apparmor_socket_getsockopt(struct socket * sock,int level,int optname)109256974a6fSJohn Johansen static int apparmor_socket_getsockopt(struct socket *sock, int level,
109356974a6fSJohn Johansen 				      int optname)
109456974a6fSJohn Johansen {
109556974a6fSJohn Johansen 	return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
109656974a6fSJohn Johansen 				level, optname);
109756974a6fSJohn Johansen }
109856974a6fSJohn Johansen 
109956974a6fSJohn Johansen /**
11000fc6ab40SYang Li  * apparmor_socket_setsockopt - check perms before setting socket options
110156974a6fSJohn Johansen  */
apparmor_socket_setsockopt(struct socket * sock,int level,int optname)110256974a6fSJohn Johansen static int apparmor_socket_setsockopt(struct socket *sock, int level,
110356974a6fSJohn Johansen 				      int optname)
110456974a6fSJohn Johansen {
110556974a6fSJohn Johansen 	return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
110656974a6fSJohn Johansen 				level, optname);
110756974a6fSJohn Johansen }
110856974a6fSJohn Johansen 
110956974a6fSJohn Johansen /**
111056974a6fSJohn Johansen  * apparmor_socket_shutdown - check perms before shutting down @sock conn
111156974a6fSJohn Johansen  */
apparmor_socket_shutdown(struct socket * sock,int how)111256974a6fSJohn Johansen static int apparmor_socket_shutdown(struct socket *sock, int how)
111356974a6fSJohn Johansen {
111456974a6fSJohn Johansen 	return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
111556974a6fSJohn Johansen }
111656974a6fSJohn Johansen 
1117e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK
111856974a6fSJohn Johansen /**
11190fc6ab40SYang Li  * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
112056974a6fSJohn Johansen  *
112156974a6fSJohn Johansen  * Note: can not sleep may be called with locks held
112256974a6fSJohn Johansen  *
112356974a6fSJohn Johansen  * dont want protocol specific in __skb_recv_datagram()
112456974a6fSJohn Johansen  * to deny an incoming connection  socket_sock_rcv_skb()
112556974a6fSJohn Johansen  */
apparmor_socket_sock_rcv_skb(struct sock * sk,struct sk_buff * skb)112656974a6fSJohn Johansen static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
112756974a6fSJohn Johansen {
1128ab9f2115SMatthew Garrett 	struct aa_sk_ctx *ctx = SK_CTX(sk);
1129ab9f2115SMatthew Garrett 
1130ab9f2115SMatthew Garrett 	if (!skb->secmark)
113156974a6fSJohn Johansen 		return 0;
1132ab9f2115SMatthew Garrett 
1133*6c920754SXiao Liang 	/*
1134*6c920754SXiao Liang 	 * If reach here before socket_post_create hook is called, in which
1135*6c920754SXiao Liang 	 * case label is null, drop the packet.
1136*6c920754SXiao Liang 	 */
1137*6c920754SXiao Liang 	if (!ctx->label)
1138*6c920754SXiao Liang 		return -EACCES;
1139*6c920754SXiao Liang 
1140ab9f2115SMatthew Garrett 	return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1141ab9f2115SMatthew Garrett 				      skb->secmark, sk);
114256974a6fSJohn Johansen }
1143e1af4779SArnd Bergmann #endif
114456974a6fSJohn Johansen 
114556974a6fSJohn Johansen 
sk_peer_label(struct sock * sk)114656974a6fSJohn Johansen static struct aa_label *sk_peer_label(struct sock *sk)
114756974a6fSJohn Johansen {
114856974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = SK_CTX(sk);
114956974a6fSJohn Johansen 
115056974a6fSJohn Johansen 	if (ctx->peer)
115156974a6fSJohn Johansen 		return ctx->peer;
115256974a6fSJohn Johansen 
115356974a6fSJohn Johansen 	return ERR_PTR(-ENOPROTOOPT);
115456974a6fSJohn Johansen }
115556974a6fSJohn Johansen 
115656974a6fSJohn Johansen /**
115756974a6fSJohn Johansen  * apparmor_socket_getpeersec_stream - get security context of peer
115856974a6fSJohn Johansen  *
115956974a6fSJohn Johansen  * Note: for tcp only valid if using ipsec or cipso on lan
116056974a6fSJohn Johansen  */
apparmor_socket_getpeersec_stream(struct socket * sock,sockptr_t optval,sockptr_t optlen,unsigned int len)116156974a6fSJohn Johansen static int apparmor_socket_getpeersec_stream(struct socket *sock,
1162b10b9c34SPaul Moore 					     sockptr_t optval, sockptr_t optlen,
116356974a6fSJohn Johansen 					     unsigned int len)
116456974a6fSJohn Johansen {
1165b10b9c34SPaul Moore 	char *name = NULL;
116656974a6fSJohn Johansen 	int slen, error = 0;
116756974a6fSJohn Johansen 	struct aa_label *label;
116856974a6fSJohn Johansen 	struct aa_label *peer;
116956974a6fSJohn Johansen 
117056974a6fSJohn Johansen 	label = begin_current_label_crit_section();
117156974a6fSJohn Johansen 	peer = sk_peer_label(sock->sk);
117256974a6fSJohn Johansen 	if (IS_ERR(peer)) {
117356974a6fSJohn Johansen 		error = PTR_ERR(peer);
117456974a6fSJohn Johansen 		goto done;
117556974a6fSJohn Johansen 	}
117656974a6fSJohn Johansen 	slen = aa_label_asxprint(&name, labels_ns(label), peer,
117756974a6fSJohn Johansen 				 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
117856974a6fSJohn Johansen 				 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
117956974a6fSJohn Johansen 	/* don't include terminating \0 in slen, it breaks some apps */
118056974a6fSJohn Johansen 	if (slen < 0) {
118156974a6fSJohn Johansen 		error = -ENOMEM;
1182b10b9c34SPaul Moore 		goto done;
1183b10b9c34SPaul Moore 	}
118456974a6fSJohn Johansen 	if (slen > len) {
118556974a6fSJohn Johansen 		error = -ERANGE;
1186b10b9c34SPaul Moore 		goto done_len;
118756974a6fSJohn Johansen 	}
118856974a6fSJohn Johansen 
1189b10b9c34SPaul Moore 	if (copy_to_sockptr(optval, name, slen))
1190b10b9c34SPaul Moore 		error = -EFAULT;
1191b10b9c34SPaul Moore done_len:
1192b10b9c34SPaul Moore 	if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
1193b10b9c34SPaul Moore 		error = -EFAULT;
119456974a6fSJohn Johansen done:
119556974a6fSJohn Johansen 	end_current_label_crit_section(label);
1196b10b9c34SPaul Moore 	kfree(name);
119756974a6fSJohn Johansen 	return error;
119856974a6fSJohn Johansen }
119956974a6fSJohn Johansen 
120056974a6fSJohn Johansen /**
120156974a6fSJohn Johansen  * apparmor_socket_getpeersec_dgram - get security label of packet
120256974a6fSJohn Johansen  * @sock: the peer socket
120356974a6fSJohn Johansen  * @skb: packet data
120456974a6fSJohn Johansen  * @secid: pointer to where to put the secid of the packet
120556974a6fSJohn Johansen  *
120656974a6fSJohn Johansen  * Sets the netlabel socket state on sk from parent
120756974a6fSJohn Johansen  */
apparmor_socket_getpeersec_dgram(struct socket * sock,struct sk_buff * skb,u32 * secid)120856974a6fSJohn Johansen static int apparmor_socket_getpeersec_dgram(struct socket *sock,
120956974a6fSJohn Johansen 					    struct sk_buff *skb, u32 *secid)
121056974a6fSJohn Johansen 
121156974a6fSJohn Johansen {
121256974a6fSJohn Johansen 	/* TODO: requires secid support */
121356974a6fSJohn Johansen 	return -ENOPROTOOPT;
121456974a6fSJohn Johansen }
121556974a6fSJohn Johansen 
121656974a6fSJohn Johansen /**
121756974a6fSJohn Johansen  * apparmor_sock_graft - Initialize newly created socket
121856974a6fSJohn Johansen  * @sk: child sock
121956974a6fSJohn Johansen  * @parent: parent socket
122056974a6fSJohn Johansen  *
122156974a6fSJohn Johansen  * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
122256974a6fSJohn Johansen  *       just set sk security information off of current creating process label
122356974a6fSJohn Johansen  *       Labeling of sk for accept case - probably should be sock based
122456974a6fSJohn Johansen  *       instead of task, because of the case where an implicitly labeled
122556974a6fSJohn Johansen  *       socket is shared by different tasks.
122656974a6fSJohn Johansen  */
apparmor_sock_graft(struct sock * sk,struct socket * parent)122756974a6fSJohn Johansen static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
122856974a6fSJohn Johansen {
122956974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = SK_CTX(sk);
123056974a6fSJohn Johansen 
123156974a6fSJohn Johansen 	if (!ctx->label)
123256974a6fSJohn Johansen 		ctx->label = aa_get_current_label();
123356974a6fSJohn Johansen }
123456974a6fSJohn Johansen 
1235e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK
apparmor_inet_conn_request(const struct sock * sk,struct sk_buff * skb,struct request_sock * req)123641dd9596SFlorian Westphal static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1237ab9f2115SMatthew Garrett 				      struct request_sock *req)
1238ab9f2115SMatthew Garrett {
1239ab9f2115SMatthew Garrett 	struct aa_sk_ctx *ctx = SK_CTX(sk);
1240ab9f2115SMatthew Garrett 
1241ab9f2115SMatthew Garrett 	if (!skb->secmark)
1242ab9f2115SMatthew Garrett 		return 0;
1243ab9f2115SMatthew Garrett 
1244ab9f2115SMatthew Garrett 	return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1245ab9f2115SMatthew Garrett 				      skb->secmark, sk);
1246ab9f2115SMatthew Garrett }
1247e1af4779SArnd Bergmann #endif
1248ab9f2115SMatthew Garrett 
1249bbd3662aSCasey Schaufler /*
125037923d43SXiu Jianfeng  * The cred blob is a pointer to, not an instance of, an aa_label.
1251bbd3662aSCasey Schaufler  */
1252f22f9aafSPaul Moore struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
125337923d43SXiu Jianfeng 	.lbs_cred = sizeof(struct aa_label *),
125433bf60caSCasey Schaufler 	.lbs_file = sizeof(struct aa_file_ctx),
1255f4ad8f2cSCasey Schaufler 	.lbs_task = sizeof(struct aa_task_ctx),
1256bbd3662aSCasey Schaufler };
1257bbd3662aSCasey Schaufler 
1258f22f9aafSPaul Moore static struct security_hook_list apparmor_hooks[] __ro_after_init = {
1259e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1260e20b043aSCasey Schaufler 	LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1261e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capget, apparmor_capget),
1262e20b043aSCasey Schaufler 	LSM_HOOK_INIT(capable, apparmor_capable),
1263b5e95b48SJohn Johansen 
126496af4515SJohn Johansen 	LSM_HOOK_INIT(move_mount, apparmor_move_mount),
12652ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
12662ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
12672ea3ffb7SJohn Johansen 	LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
12682ea3ffb7SJohn Johansen 
1269e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_link, apparmor_path_link),
1270e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1271e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1272e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1273e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1274e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1275e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1276e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1277e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1278e20b043aSCasey Schaufler 	LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1279e20b043aSCasey Schaufler 	LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1280b5e95b48SJohn Johansen 
1281e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_open, apparmor_file_open),
1282064dc947SJohn Johansen 	LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1283e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1284e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1285e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1286e20b043aSCasey Schaufler 	LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1287e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1288e20b043aSCasey Schaufler 	LSM_HOOK_INIT(file_lock, apparmor_file_lock),
12893350607dSGünther Noack 	LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
1290b5e95b48SJohn Johansen 
1291e20b043aSCasey Schaufler 	LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1292e20b043aSCasey Schaufler 	LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1293b5e95b48SJohn Johansen 
129456974a6fSJohn Johansen 	LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
129556974a6fSJohn Johansen 	LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
129656974a6fSJohn Johansen 	LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
129756974a6fSJohn Johansen 
129856974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_create, apparmor_socket_create),
129956974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
130056974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
130156974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
130256974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
130356974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
130456974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
130556974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
130656974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
130756974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
130856974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
130956974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
131056974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1311e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK
131256974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1313e1af4779SArnd Bergmann #endif
131456974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getpeersec_stream,
131556974a6fSJohn Johansen 		      apparmor_socket_getpeersec_stream),
131656974a6fSJohn Johansen 	LSM_HOOK_INIT(socket_getpeersec_dgram,
131756974a6fSJohn Johansen 		      apparmor_socket_getpeersec_dgram),
131856974a6fSJohn Johansen 	LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1319e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK
1320ab9f2115SMatthew Garrett 	LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1321e1af4779SArnd Bergmann #endif
132256974a6fSJohn Johansen 
1323e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1324e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1325e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1326e20b043aSCasey Schaufler 	LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1327b5e95b48SJohn Johansen 
1328b8bff599SEric W. Biederman 	LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1329e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1330e20b043aSCasey Schaufler 	LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1331b5e95b48SJohn Johansen 
13323b529a76SJohn Johansen 	LSM_HOOK_INIT(task_free, apparmor_task_free),
13333b529a76SJohn Johansen 	LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
13346326948fSPaul Moore 	LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj),
13356326948fSPaul Moore 	LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
1336e20b043aSCasey Schaufler 	LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1337cd1dbf76SJohn Johansen 	LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1338c0929212SJohn Johansen 
1339e79c26d0SMatthew Garrett #ifdef CONFIG_AUDIT
1340e79c26d0SMatthew Garrett 	LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1341e79c26d0SMatthew Garrett 	LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1342e79c26d0SMatthew Garrett 	LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1343e79c26d0SMatthew Garrett 	LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1344e79c26d0SMatthew Garrett #endif
1345e79c26d0SMatthew Garrett 
1346c0929212SJohn Johansen 	LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1347c0929212SJohn Johansen 	LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1348c0929212SJohn Johansen 	LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1349b5e95b48SJohn Johansen };
1350b5e95b48SJohn Johansen 
1351b5e95b48SJohn Johansen /*
1352b5e95b48SJohn Johansen  * AppArmor sysfs module parameters
1353b5e95b48SJohn Johansen  */
1354b5e95b48SJohn Johansen 
1355101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp);
1356101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1357b8aa09fdSRusty Russell #define param_check_aabool param_check_bool
13589c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aabool = {
13596a4c2643SJani Nikula 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1360101d6c82SStephen Rothwell 	.set = param_set_aabool,
1361101d6c82SStephen Rothwell 	.get = param_get_aabool
1362101d6c82SStephen Rothwell };
1363b5e95b48SJohn Johansen 
1364101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp);
1365101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1366b8aa09fdSRusty Russell #define param_check_aauint param_check_uint
13679c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aauint = {
1368101d6c82SStephen Rothwell 	.set = param_set_aauint,
1369101d6c82SStephen Rothwell 	.get = param_get_aauint
1370101d6c82SStephen Rothwell };
1371b5e95b48SJohn Johansen 
137263c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val,
137363c16c3aSChris Coulson 					const struct kernel_param *kp);
137463c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer,
137563c16c3aSChris Coulson 					const struct kernel_param *kp);
137663c16c3aSChris Coulson #define param_check_aacompressionlevel param_check_int
137763c16c3aSChris Coulson static const struct kernel_param_ops param_ops_aacompressionlevel = {
137863c16c3aSChris Coulson 	.set = param_set_aacompressionlevel,
137963c16c3aSChris Coulson 	.get = param_get_aacompressionlevel
138063c16c3aSChris Coulson };
138163c16c3aSChris Coulson 
1382101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1383101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1384b8aa09fdSRusty Russell #define param_check_aalockpolicy param_check_bool
13859c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aalockpolicy = {
13866a4c2643SJani Nikula 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1387101d6c82SStephen Rothwell 	.set = param_set_aalockpolicy,
1388101d6c82SStephen Rothwell 	.get = param_get_aalockpolicy
1389101d6c82SStephen Rothwell };
1390b5e95b48SJohn Johansen 
1391e4dca7b7SKees Cook static int param_set_audit(const char *val, const struct kernel_param *kp);
1392e4dca7b7SKees Cook static int param_get_audit(char *buffer, const struct kernel_param *kp);
1393b5e95b48SJohn Johansen 
1394e4dca7b7SKees Cook static int param_set_mode(const char *val, const struct kernel_param *kp);
1395e4dca7b7SKees Cook static int param_get_mode(char *buffer, const struct kernel_param *kp);
1396b5e95b48SJohn Johansen 
1397b5e95b48SJohn Johansen /* Flag values, also controllable via /sys/module/apparmor/parameters
1398b5e95b48SJohn Johansen  * We define special types as we want to do additional mediation.
1399b5e95b48SJohn Johansen  */
1400b5e95b48SJohn Johansen 
1401b5e95b48SJohn Johansen /* AppArmor global enforcement switch - complain, enforce, kill */
1402b5e95b48SJohn Johansen enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1403b5e95b48SJohn Johansen module_param_call(mode, param_set_mode, param_get_mode,
1404b5e95b48SJohn Johansen 		  &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1405b5e95b48SJohn Johansen 
14066059f71fSJohn Johansen /* whether policy verification hashing is enabled */
14077616ac70SArnd Bergmann bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
14083ccb76c5SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_HASH
14096059f71fSJohn Johansen module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
14107616ac70SArnd Bergmann #endif
14116059f71fSJohn Johansen 
1412d61c57fdSJohn Johansen /* whether policy exactly as loaded is retained for debug and checkpointing */
1413d61c57fdSJohn Johansen bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1414d61c57fdSJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1415d61c57fdSJohn Johansen module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1416d61c57fdSJohn Johansen #endif
1417d61c57fdSJohn Johansen 
141863c16c3aSChris Coulson /* policy loaddata compression level */
141970f24a9fSJohn Johansen int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
142063c16c3aSChris Coulson module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
142163c16c3aSChris Coulson 		   aacompressionlevel, 0400);
142263c16c3aSChris Coulson 
1423b5e95b48SJohn Johansen /* Debug mode */
1424eea7a05fSValentin Rothberg bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
1425b5e95b48SJohn Johansen module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1426b5e95b48SJohn Johansen 
1427b5e95b48SJohn Johansen /* Audit mode */
1428b5e95b48SJohn Johansen enum audit_mode aa_g_audit;
1429b5e95b48SJohn Johansen module_param_call(audit, param_set_audit, param_get_audit,
1430b5e95b48SJohn Johansen 		  &aa_g_audit, S_IRUSR | S_IWUSR);
1431b5e95b48SJohn Johansen 
1432b5e95b48SJohn Johansen /* Determines if audit header is included in audited messages.  This
1433b5e95b48SJohn Johansen  * provides more context if the audit daemon is not running
1434b5e95b48SJohn Johansen  */
1435954317feSThomas Meyer bool aa_g_audit_header = true;
1436b5e95b48SJohn Johansen module_param_named(audit_header, aa_g_audit_header, aabool,
1437b5e95b48SJohn Johansen 		   S_IRUSR | S_IWUSR);
1438b5e95b48SJohn Johansen 
1439b5e95b48SJohn Johansen /* lock out loading/removal of policy
1440b5e95b48SJohn Johansen  * TODO: add in at boot loading of policy, which is the only way to
1441b5e95b48SJohn Johansen  *       load policy, if lock_policy is set
1442b5e95b48SJohn Johansen  */
144390ab5ee9SRusty Russell bool aa_g_lock_policy;
1444b5e95b48SJohn Johansen module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1445b5e95b48SJohn Johansen 		   S_IRUSR | S_IWUSR);
1446b5e95b48SJohn Johansen 
1447b5e95b48SJohn Johansen /* Syscall logging mode */
144890ab5ee9SRusty Russell bool aa_g_logsyscall;
1449b5e95b48SJohn Johansen module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1450b5e95b48SJohn Johansen 
1451b5e95b48SJohn Johansen /* Maximum pathname length before accesses will start getting rejected */
1452b5e95b48SJohn Johansen unsigned int aa_g_path_max = 2 * PATH_MAX;
1453622f6e32SJohn Johansen module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1454b5e95b48SJohn Johansen 
1455b5e95b48SJohn Johansen /* Determines how paranoid loading of policy is and how much verification
1456b5e95b48SJohn Johansen  * on the loaded policy is done.
1457abbf8734SJohn Johansen  * DEPRECATED: read only as strict checking of load is always done now
1458abbf8734SJohn Johansen  * that none root users (user namespaces) can load policy.
1459b5e95b48SJohn Johansen  */
146079eb2711SLukas Bulwahn bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1461abbf8734SJohn Johansen module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1462b5e95b48SJohn Johansen 
1463e33c1b99SKees Cook static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1464e33c1b99SKees Cook static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1465e33c1b99SKees Cook #define param_check_aaintbool param_check_int
1466e33c1b99SKees Cook static const struct kernel_param_ops param_ops_aaintbool = {
1467e33c1b99SKees Cook 	.set = param_set_aaintbool,
1468e33c1b99SKees Cook 	.get = param_get_aaintbool
1469e33c1b99SKees Cook };
1470b5e95b48SJohn Johansen /* Boot time disable flag */
1471f22f9aafSPaul Moore static int apparmor_enabled __ro_after_init = 1;
1472e33c1b99SKees Cook module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1473b5e95b48SJohn Johansen 
apparmor_enabled_setup(char * str)1474b5e95b48SJohn Johansen static int __init apparmor_enabled_setup(char *str)
1475b5e95b48SJohn Johansen {
1476b5e95b48SJohn Johansen 	unsigned long enabled;
147729707b20SJingoo Han 	int error = kstrtoul(str, 0, &enabled);
1478b5e95b48SJohn Johansen 	if (!error)
1479b5e95b48SJohn Johansen 		apparmor_enabled = enabled ? 1 : 0;
1480b5e95b48SJohn Johansen 	return 1;
1481b5e95b48SJohn Johansen }
1482b5e95b48SJohn Johansen 
1483b5e95b48SJohn Johansen __setup("apparmor=", apparmor_enabled_setup);
1484b5e95b48SJohn Johansen 
1485b5e95b48SJohn Johansen /* set global flag turning off the ability to load policy */
param_set_aalockpolicy(const char * val,const struct kernel_param * kp)1486101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1487b5e95b48SJohn Johansen {
1488545de8feSJohn Johansen 	if (!apparmor_enabled)
1489545de8feSJohn Johansen 		return -EINVAL;
149092de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1491b5e95b48SJohn Johansen 		return -EPERM;
1492b5e95b48SJohn Johansen 	return param_set_bool(val, kp);
1493b5e95b48SJohn Johansen }
1494b5e95b48SJohn Johansen 
param_get_aalockpolicy(char * buffer,const struct kernel_param * kp)1495101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1496b5e95b48SJohn Johansen {
1497ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1498ca4bd5aeSJohn Johansen 		return -EINVAL;
149992de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1500545de8feSJohn Johansen 		return -EPERM;
1501b5e95b48SJohn Johansen 	return param_get_bool(buffer, kp);
1502b5e95b48SJohn Johansen }
1503b5e95b48SJohn Johansen 
param_set_aabool(const char * val,const struct kernel_param * kp)1504101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp)
1505b5e95b48SJohn Johansen {
1506ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1507ca4bd5aeSJohn Johansen 		return -EINVAL;
150892de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1509545de8feSJohn Johansen 		return -EPERM;
1510b5e95b48SJohn Johansen 	return param_set_bool(val, kp);
1511b5e95b48SJohn Johansen }
1512b5e95b48SJohn Johansen 
param_get_aabool(char * buffer,const struct kernel_param * kp)1513101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1514b5e95b48SJohn Johansen {
1515ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1516ca4bd5aeSJohn Johansen 		return -EINVAL;
151792de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1518545de8feSJohn Johansen 		return -EPERM;
1519b5e95b48SJohn Johansen 	return param_get_bool(buffer, kp);
1520b5e95b48SJohn Johansen }
1521b5e95b48SJohn Johansen 
param_set_aauint(const char * val,const struct kernel_param * kp)1522101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp)
1523b5e95b48SJohn Johansen {
152439d84824SJohn Johansen 	int error;
152539d84824SJohn Johansen 
1526ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1527ca4bd5aeSJohn Johansen 		return -EINVAL;
152839d84824SJohn Johansen 	/* file is ro but enforce 2nd line check */
152939d84824SJohn Johansen 	if (apparmor_initialized)
1530545de8feSJohn Johansen 		return -EPERM;
153139d84824SJohn Johansen 
153239d84824SJohn Johansen 	error = param_set_uint(val, kp);
1533df323337SSebastian Andrzej Siewior 	aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
153439d84824SJohn Johansen 	pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
153539d84824SJohn Johansen 
153639d84824SJohn Johansen 	return error;
1537b5e95b48SJohn Johansen }
1538b5e95b48SJohn Johansen 
param_get_aauint(char * buffer,const struct kernel_param * kp)1539101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1540b5e95b48SJohn Johansen {
1541ca4bd5aeSJohn Johansen 	if (!apparmor_enabled)
1542ca4bd5aeSJohn Johansen 		return -EINVAL;
154392de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1544545de8feSJohn Johansen 		return -EPERM;
1545b5e95b48SJohn Johansen 	return param_get_uint(buffer, kp);
1546b5e95b48SJohn Johansen }
1547b5e95b48SJohn Johansen 
1548e33c1b99SKees Cook /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
param_set_aaintbool(const char * val,const struct kernel_param * kp)1549e33c1b99SKees Cook static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1550e33c1b99SKees Cook {
1551e33c1b99SKees Cook 	struct kernel_param kp_local;
1552e33c1b99SKees Cook 	bool value;
1553e33c1b99SKees Cook 	int error;
1554e33c1b99SKees Cook 
1555e33c1b99SKees Cook 	if (apparmor_initialized)
1556e33c1b99SKees Cook 		return -EPERM;
1557e33c1b99SKees Cook 
1558e33c1b99SKees Cook 	/* Create local copy, with arg pointing to bool type. */
1559e33c1b99SKees Cook 	value = !!*((int *)kp->arg);
1560e33c1b99SKees Cook 	memcpy(&kp_local, kp, sizeof(kp_local));
1561e33c1b99SKees Cook 	kp_local.arg = &value;
1562e33c1b99SKees Cook 
1563e33c1b99SKees Cook 	error = param_set_bool(val, &kp_local);
1564e33c1b99SKees Cook 	if (!error)
1565e33c1b99SKees Cook 		*((int *)kp->arg) = *((bool *)kp_local.arg);
1566e33c1b99SKees Cook 	return error;
1567e33c1b99SKees Cook }
1568e33c1b99SKees Cook 
1569e33c1b99SKees Cook /*
1570e33c1b99SKees Cook  * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1571e33c1b99SKees Cook  * 1/0, this converts the "int that is actually bool" back to bool for
1572e33c1b99SKees Cook  * display in the /sys filesystem, while keeping it "int" for the LSM
1573e33c1b99SKees Cook  * infrastructure.
1574e33c1b99SKees Cook  */
param_get_aaintbool(char * buffer,const struct kernel_param * kp)1575e33c1b99SKees Cook static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1576e33c1b99SKees Cook {
1577e33c1b99SKees Cook 	struct kernel_param kp_local;
1578e33c1b99SKees Cook 	bool value;
1579e33c1b99SKees Cook 
1580e33c1b99SKees Cook 	/* Create local copy, with arg pointing to bool type. */
1581e33c1b99SKees Cook 	value = !!*((int *)kp->arg);
1582e33c1b99SKees Cook 	memcpy(&kp_local, kp, sizeof(kp_local));
1583e33c1b99SKees Cook 	kp_local.arg = &value;
1584e33c1b99SKees Cook 
1585e33c1b99SKees Cook 	return param_get_bool(buffer, &kp_local);
1586e33c1b99SKees Cook }
1587e33c1b99SKees Cook 
param_set_aacompressionlevel(const char * val,const struct kernel_param * kp)158863c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val,
158963c16c3aSChris Coulson 					const struct kernel_param *kp)
159063c16c3aSChris Coulson {
159163c16c3aSChris Coulson 	int error;
159263c16c3aSChris Coulson 
159363c16c3aSChris Coulson 	if (!apparmor_enabled)
159463c16c3aSChris Coulson 		return -EINVAL;
159563c16c3aSChris Coulson 	if (apparmor_initialized)
159663c16c3aSChris Coulson 		return -EPERM;
159763c16c3aSChris Coulson 
159863c16c3aSChris Coulson 	error = param_set_int(val, kp);
159963c16c3aSChris Coulson 
160063c16c3aSChris Coulson 	aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
160170f24a9fSJohn Johansen 					       AA_MIN_CLEVEL, AA_MAX_CLEVEL);
1602f4d6b94bSJon Tourville 	pr_info("AppArmor: policy rawdata compression level set to %d\n",
160363c16c3aSChris Coulson 		aa_g_rawdata_compression_level);
160463c16c3aSChris Coulson 
160563c16c3aSChris Coulson 	return error;
160663c16c3aSChris Coulson }
160763c16c3aSChris Coulson 
param_get_aacompressionlevel(char * buffer,const struct kernel_param * kp)160863c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer,
160963c16c3aSChris Coulson 					const struct kernel_param *kp)
161063c16c3aSChris Coulson {
161163c16c3aSChris Coulson 	if (!apparmor_enabled)
161263c16c3aSChris Coulson 		return -EINVAL;
161392de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
161463c16c3aSChris Coulson 		return -EPERM;
161563c16c3aSChris Coulson 	return param_get_int(buffer, kp);
161663c16c3aSChris Coulson }
161763c16c3aSChris Coulson 
param_get_audit(char * buffer,const struct kernel_param * kp)1618e4dca7b7SKees Cook static int param_get_audit(char *buffer, const struct kernel_param *kp)
1619b5e95b48SJohn Johansen {
1620b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1621b5e95b48SJohn Johansen 		return -EINVAL;
162292de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1623545de8feSJohn Johansen 		return -EPERM;
1624b5e95b48SJohn Johansen 	return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1625b5e95b48SJohn Johansen }
1626b5e95b48SJohn Johansen 
param_set_audit(const char * val,const struct kernel_param * kp)1627e4dca7b7SKees Cook static int param_set_audit(const char *val, const struct kernel_param *kp)
1628b5e95b48SJohn Johansen {
1629b5e95b48SJohn Johansen 	int i;
1630b5e95b48SJohn Johansen 
1631b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1632b5e95b48SJohn Johansen 		return -EINVAL;
1633b5e95b48SJohn Johansen 	if (!val)
1634b5e95b48SJohn Johansen 		return -EINVAL;
163592de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1636545de8feSJohn Johansen 		return -EPERM;
1637b5e95b48SJohn Johansen 
16385d8779a5SAndy Shevchenko 	i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
16395d8779a5SAndy Shevchenko 	if (i < 0)
16405d8779a5SAndy Shevchenko 		return -EINVAL;
16415d8779a5SAndy Shevchenko 
1642b5e95b48SJohn Johansen 	aa_g_audit = i;
1643b5e95b48SJohn Johansen 	return 0;
1644b5e95b48SJohn Johansen }
1645b5e95b48SJohn Johansen 
param_get_mode(char * buffer,const struct kernel_param * kp)1646e4dca7b7SKees Cook static int param_get_mode(char *buffer, const struct kernel_param *kp)
1647b5e95b48SJohn Johansen {
1648b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1649b5e95b48SJohn Johansen 		return -EINVAL;
165092de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1651545de8feSJohn Johansen 		return -EPERM;
1652b5e95b48SJohn Johansen 
16530d259f04SJohn Johansen 	return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1654b5e95b48SJohn Johansen }
1655b5e95b48SJohn Johansen 
param_set_mode(const char * val,const struct kernel_param * kp)1656e4dca7b7SKees Cook static int param_set_mode(const char *val, const struct kernel_param *kp)
1657b5e95b48SJohn Johansen {
1658b5e95b48SJohn Johansen 	int i;
1659b5e95b48SJohn Johansen 
1660b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1661b5e95b48SJohn Johansen 		return -EINVAL;
1662b5e95b48SJohn Johansen 	if (!val)
1663b5e95b48SJohn Johansen 		return -EINVAL;
166492de220aSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1665545de8feSJohn Johansen 		return -EPERM;
1666b5e95b48SJohn Johansen 
16675d8779a5SAndy Shevchenko 	i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
16685d8779a5SAndy Shevchenko 			 val);
16695d8779a5SAndy Shevchenko 	if (i < 0)
16705d8779a5SAndy Shevchenko 		return -EINVAL;
16715d8779a5SAndy Shevchenko 
1672b5e95b48SJohn Johansen 	aa_g_profile_mode = i;
1673b5e95b48SJohn Johansen 	return 0;
1674b5e95b48SJohn Johansen }
1675b5e95b48SJohn Johansen 
aa_get_buffer(bool in_atomic)1676341c1fdaSJohn Johansen char *aa_get_buffer(bool in_atomic)
1677df323337SSebastian Andrzej Siewior {
1678df323337SSebastian Andrzej Siewior 	union aa_buffer *aa_buf;
1679df323337SSebastian Andrzej Siewior 	bool try_again = true;
1680341c1fdaSJohn Johansen 	gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1681df323337SSebastian Andrzej Siewior 
1682df323337SSebastian Andrzej Siewior retry:
1683df323337SSebastian Andrzej Siewior 	spin_lock(&aa_buffers_lock);
1684341c1fdaSJohn Johansen 	if (buffer_count > reserve_count ||
1685341c1fdaSJohn Johansen 	    (in_atomic && !list_empty(&aa_global_buffers))) {
1686df323337SSebastian Andrzej Siewior 		aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1687df323337SSebastian Andrzej Siewior 					  list);
1688df323337SSebastian Andrzej Siewior 		list_del(&aa_buf->list);
1689341c1fdaSJohn Johansen 		buffer_count--;
1690df323337SSebastian Andrzej Siewior 		spin_unlock(&aa_buffers_lock);
1691ba808cb5SKees Cook 		return aa_buf->buffer;
1692df323337SSebastian Andrzej Siewior 	}
1693341c1fdaSJohn Johansen 	if (in_atomic) {
1694341c1fdaSJohn Johansen 		/*
1695341c1fdaSJohn Johansen 		 * out of reserve buffers and in atomic context so increase
1696341c1fdaSJohn Johansen 		 * how many buffers to keep in reserve
1697341c1fdaSJohn Johansen 		 */
1698341c1fdaSJohn Johansen 		reserve_count++;
1699341c1fdaSJohn Johansen 		flags = GFP_ATOMIC;
1700341c1fdaSJohn Johansen 	}
1701df323337SSebastian Andrzej Siewior 	spin_unlock(&aa_buffers_lock);
1702df323337SSebastian Andrzej Siewior 
1703341c1fdaSJohn Johansen 	if (!in_atomic)
1704341c1fdaSJohn Johansen 		might_sleep();
1705341c1fdaSJohn Johansen 	aa_buf = kmalloc(aa_g_path_max, flags);
1706df323337SSebastian Andrzej Siewior 	if (!aa_buf) {
1707df323337SSebastian Andrzej Siewior 		if (try_again) {
1708df323337SSebastian Andrzej Siewior 			try_again = false;
1709df323337SSebastian Andrzej Siewior 			goto retry;
1710df323337SSebastian Andrzej Siewior 		}
1711df323337SSebastian Andrzej Siewior 		pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
1712df323337SSebastian Andrzej Siewior 		return NULL;
1713df323337SSebastian Andrzej Siewior 	}
1714ba808cb5SKees Cook 	return aa_buf->buffer;
1715df323337SSebastian Andrzej Siewior }
1716df323337SSebastian Andrzej Siewior 
aa_put_buffer(char * buf)1717df323337SSebastian Andrzej Siewior void aa_put_buffer(char *buf)
1718df323337SSebastian Andrzej Siewior {
1719df323337SSebastian Andrzej Siewior 	union aa_buffer *aa_buf;
1720df323337SSebastian Andrzej Siewior 
1721df323337SSebastian Andrzej Siewior 	if (!buf)
1722df323337SSebastian Andrzej Siewior 		return;
1723df323337SSebastian Andrzej Siewior 	aa_buf = container_of(buf, union aa_buffer, buffer[0]);
1724df323337SSebastian Andrzej Siewior 
1725df323337SSebastian Andrzej Siewior 	spin_lock(&aa_buffers_lock);
1726df323337SSebastian Andrzej Siewior 	list_add(&aa_buf->list, &aa_global_buffers);
1727341c1fdaSJohn Johansen 	buffer_count++;
1728df323337SSebastian Andrzej Siewior 	spin_unlock(&aa_buffers_lock);
1729df323337SSebastian Andrzej Siewior }
1730df323337SSebastian Andrzej Siewior 
1731b5e95b48SJohn Johansen /*
1732b5e95b48SJohn Johansen  * AppArmor init functions
1733b5e95b48SJohn Johansen  */
1734b5e95b48SJohn Johansen 
1735b5e95b48SJohn Johansen /**
173655a26ebfSJohn Johansen  * set_init_ctx - set a task context and profile on the first task.
1737b5e95b48SJohn Johansen  *
1738b5e95b48SJohn Johansen  * TODO: allow setting an alternate profile than unconfined
1739b5e95b48SJohn Johansen  */
set_init_ctx(void)174055a26ebfSJohn Johansen static int __init set_init_ctx(void)
1741b5e95b48SJohn Johansen {
1742bf1d2ee7SBharath Vedartham 	struct cred *cred = (__force struct cred *)current->real_cred;
1743b5e95b48SJohn Johansen 
174469b5a44aSCasey Schaufler 	set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
1745b5e95b48SJohn Johansen 
1746b5e95b48SJohn Johansen 	return 0;
1747b5e95b48SJohn Johansen }
1748b5e95b48SJohn Johansen 
destroy_buffers(void)1749d4669f0bSJohn Johansen static void destroy_buffers(void)
1750d4669f0bSJohn Johansen {
1751df323337SSebastian Andrzej Siewior 	union aa_buffer *aa_buf;
1752d4669f0bSJohn Johansen 
1753df323337SSebastian Andrzej Siewior 	spin_lock(&aa_buffers_lock);
1754df323337SSebastian Andrzej Siewior 	while (!list_empty(&aa_global_buffers)) {
1755df323337SSebastian Andrzej Siewior 		aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1756df323337SSebastian Andrzej Siewior 					 list);
1757df323337SSebastian Andrzej Siewior 		list_del(&aa_buf->list);
1758df323337SSebastian Andrzej Siewior 		spin_unlock(&aa_buffers_lock);
1759df323337SSebastian Andrzej Siewior 		kfree(aa_buf);
1760df323337SSebastian Andrzej Siewior 		spin_lock(&aa_buffers_lock);
1761d4669f0bSJohn Johansen 	}
1762df323337SSebastian Andrzej Siewior 	spin_unlock(&aa_buffers_lock);
1763d4669f0bSJohn Johansen }
1764d4669f0bSJohn Johansen 
alloc_buffers(void)1765d4669f0bSJohn Johansen static int __init alloc_buffers(void)
1766d4669f0bSJohn Johansen {
1767df323337SSebastian Andrzej Siewior 	union aa_buffer *aa_buf;
1768df323337SSebastian Andrzej Siewior 	int i, num;
1769d4669f0bSJohn Johansen 
1770df323337SSebastian Andrzej Siewior 	/*
1771df323337SSebastian Andrzej Siewior 	 * A function may require two buffers at once. Usually the buffers are
1772df323337SSebastian Andrzej Siewior 	 * used for a short period of time and are shared. On UP kernel buffers
1773df323337SSebastian Andrzej Siewior 	 * two should be enough, with more CPUs it is possible that more
1774df323337SSebastian Andrzej Siewior 	 * buffers will be used simultaneously. The preallocated pool may grow.
1775df323337SSebastian Andrzej Siewior 	 * This preallocation has also the side-effect that AppArmor will be
1776df323337SSebastian Andrzej Siewior 	 * disabled early at boot if aa_g_path_max is extremly high.
1777df323337SSebastian Andrzej Siewior 	 */
1778df323337SSebastian Andrzej Siewior 	if (num_online_cpus() > 1)
1779341c1fdaSJohn Johansen 		num = 4 + RESERVE_COUNT;
1780d4669f0bSJohn Johansen 	else
1781341c1fdaSJohn Johansen 		num = 2 + RESERVE_COUNT;
1782df323337SSebastian Andrzej Siewior 
1783df323337SSebastian Andrzej Siewior 	for (i = 0; i < num; i++) {
1784df323337SSebastian Andrzej Siewior 
1785df323337SSebastian Andrzej Siewior 		aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
1786df323337SSebastian Andrzej Siewior 				 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1787df323337SSebastian Andrzej Siewior 		if (!aa_buf) {
1788d4669f0bSJohn Johansen 			destroy_buffers();
1789d4669f0bSJohn Johansen 			return -ENOMEM;
1790d4669f0bSJohn Johansen 		}
1791ba808cb5SKees Cook 		aa_put_buffer(aa_buf->buffer);
1792d4669f0bSJohn Johansen 	}
1793d4669f0bSJohn Johansen 	return 0;
1794d4669f0bSJohn Johansen }
1795d4669f0bSJohn Johansen 
1796e3ea1ca5STyler Hicks #ifdef CONFIG_SYSCTL
apparmor_dointvec(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1797e3ea1ca5STyler Hicks static int apparmor_dointvec(struct ctl_table *table, int write,
179832927393SChristoph Hellwig 			     void *buffer, size_t *lenp, loff_t *ppos)
1799e3ea1ca5STyler Hicks {
180092de220aSJohn Johansen 	if (!aa_current_policy_admin_capable(NULL))
1801e3ea1ca5STyler Hicks 		return -EPERM;
1802e3ea1ca5STyler Hicks 	if (!apparmor_enabled)
1803e3ea1ca5STyler Hicks 		return -EINVAL;
1804e3ea1ca5STyler Hicks 
1805e3ea1ca5STyler Hicks 	return proc_dointvec(table, write, buffer, lenp, ppos);
1806e3ea1ca5STyler Hicks }
1807e3ea1ca5STyler Hicks 
1808e3ea1ca5STyler Hicks static struct ctl_table apparmor_sysctl_table[] = {
1809e3ea1ca5STyler Hicks 	{
1810e3ea1ca5STyler Hicks 		.procname       = "unprivileged_userns_apparmor_policy",
1811e3ea1ca5STyler Hicks 		.data           = &unprivileged_userns_apparmor_policy,
1812e3ea1ca5STyler Hicks 		.maxlen         = sizeof(int),
1813e3ea1ca5STyler Hicks 		.mode           = 0600,
1814e3ea1ca5STyler Hicks 		.proc_handler   = apparmor_dointvec,
1815e3ea1ca5STyler Hicks 	},
1816524d8e14SJohn Johansen 	{
1817524d8e14SJohn Johansen 		.procname       = "apparmor_display_secid_mode",
1818524d8e14SJohn Johansen 		.data           = &apparmor_display_secid_mode,
1819524d8e14SJohn Johansen 		.maxlen         = sizeof(int),
1820524d8e14SJohn Johansen 		.mode           = 0600,
1821524d8e14SJohn Johansen 		.proc_handler   = apparmor_dointvec,
1822524d8e14SJohn Johansen 	},
1823524d8e14SJohn Johansen 
1824e3ea1ca5STyler Hicks 	{ }
1825e3ea1ca5STyler Hicks };
1826e3ea1ca5STyler Hicks 
apparmor_init_sysctl(void)1827e3ea1ca5STyler Hicks static int __init apparmor_init_sysctl(void)
1828e3ea1ca5STyler Hicks {
182996200952SLuis Chamberlain 	return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
1830e3ea1ca5STyler Hicks }
1831e3ea1ca5STyler Hicks #else
apparmor_init_sysctl(void)1832e3ea1ca5STyler Hicks static inline int apparmor_init_sysctl(void)
1833e3ea1ca5STyler Hicks {
1834e3ea1ca5STyler Hicks 	return 0;
1835e3ea1ca5STyler Hicks }
1836e3ea1ca5STyler Hicks #endif /* CONFIG_SYSCTL */
1837e3ea1ca5STyler Hicks 
1838e1af4779SArnd Bergmann #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
apparmor_ip_postroute(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)1839ab9f2115SMatthew Garrett static unsigned int apparmor_ip_postroute(void *priv,
1840ab9f2115SMatthew Garrett 					  struct sk_buff *skb,
1841ab9f2115SMatthew Garrett 					  const struct nf_hook_state *state)
1842ab9f2115SMatthew Garrett {
1843ab9f2115SMatthew Garrett 	struct aa_sk_ctx *ctx;
1844ab9f2115SMatthew Garrett 	struct sock *sk;
1845ab9f2115SMatthew Garrett 
1846ab9f2115SMatthew Garrett 	if (!skb->secmark)
1847ab9f2115SMatthew Garrett 		return NF_ACCEPT;
1848ab9f2115SMatthew Garrett 
1849ab9f2115SMatthew Garrett 	sk = skb_to_full_sk(skb);
1850ab9f2115SMatthew Garrett 	if (sk == NULL)
1851ab9f2115SMatthew Garrett 		return NF_ACCEPT;
1852ab9f2115SMatthew Garrett 
1853ab9f2115SMatthew Garrett 	ctx = SK_CTX(sk);
1854ab9f2115SMatthew Garrett 	if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
1855ab9f2115SMatthew Garrett 				    skb->secmark, sk))
1856ab9f2115SMatthew Garrett 		return NF_ACCEPT;
1857ab9f2115SMatthew Garrett 
1858ab9f2115SMatthew Garrett 	return NF_DROP_ERR(-ECONNREFUSED);
1859ab9f2115SMatthew Garrett 
1860ab9f2115SMatthew Garrett }
1861ab9f2115SMatthew Garrett 
1862ab9f2115SMatthew Garrett static const struct nf_hook_ops apparmor_nf_ops[] = {
1863ab9f2115SMatthew Garrett 	{
18647b721124SFlorian Westphal 		.hook =         apparmor_ip_postroute,
1865ab9f2115SMatthew Garrett 		.pf =           NFPROTO_IPV4,
1866ab9f2115SMatthew Garrett 		.hooknum =      NF_INET_POST_ROUTING,
1867ab9f2115SMatthew Garrett 		.priority =     NF_IP_PRI_SELINUX_FIRST,
1868ab9f2115SMatthew Garrett 	},
1869ab9f2115SMatthew Garrett #if IS_ENABLED(CONFIG_IPV6)
1870ab9f2115SMatthew Garrett 	{
18717b721124SFlorian Westphal 		.hook =         apparmor_ip_postroute,
1872ab9f2115SMatthew Garrett 		.pf =           NFPROTO_IPV6,
1873ab9f2115SMatthew Garrett 		.hooknum =      NF_INET_POST_ROUTING,
1874ab9f2115SMatthew Garrett 		.priority =     NF_IP6_PRI_SELINUX_FIRST,
1875ab9f2115SMatthew Garrett 	},
1876ab9f2115SMatthew Garrett #endif
1877ab9f2115SMatthew Garrett };
1878ab9f2115SMatthew Garrett 
apparmor_nf_register(struct net * net)1879ab9f2115SMatthew Garrett static int __net_init apparmor_nf_register(struct net *net)
1880ab9f2115SMatthew Garrett {
188184117994SMinghao Chi 	return nf_register_net_hooks(net, apparmor_nf_ops,
1882ab9f2115SMatthew Garrett 				    ARRAY_SIZE(apparmor_nf_ops));
1883ab9f2115SMatthew Garrett }
1884ab9f2115SMatthew Garrett 
apparmor_nf_unregister(struct net * net)1885ab9f2115SMatthew Garrett static void __net_exit apparmor_nf_unregister(struct net *net)
1886ab9f2115SMatthew Garrett {
1887ab9f2115SMatthew Garrett 	nf_unregister_net_hooks(net, apparmor_nf_ops,
1888ab9f2115SMatthew Garrett 				ARRAY_SIZE(apparmor_nf_ops));
1889ab9f2115SMatthew Garrett }
1890ab9f2115SMatthew Garrett 
1891ab9f2115SMatthew Garrett static struct pernet_operations apparmor_net_ops = {
1892ab9f2115SMatthew Garrett 	.init = apparmor_nf_register,
1893ab9f2115SMatthew Garrett 	.exit = apparmor_nf_unregister,
1894ab9f2115SMatthew Garrett };
1895ab9f2115SMatthew Garrett 
apparmor_nf_ip_init(void)1896ab9f2115SMatthew Garrett static int __init apparmor_nf_ip_init(void)
1897ab9f2115SMatthew Garrett {
1898ab9f2115SMatthew Garrett 	int err;
1899ab9f2115SMatthew Garrett 
1900ab9f2115SMatthew Garrett 	if (!apparmor_enabled)
1901ab9f2115SMatthew Garrett 		return 0;
1902ab9f2115SMatthew Garrett 
1903ab9f2115SMatthew Garrett 	err = register_pernet_subsys(&apparmor_net_ops);
1904ab9f2115SMatthew Garrett 	if (err)
1905ab9f2115SMatthew Garrett 		panic("Apparmor: register_pernet_subsys: error %d\n", err);
1906ab9f2115SMatthew Garrett 
1907ab9f2115SMatthew Garrett 	return 0;
1908ab9f2115SMatthew Garrett }
1909ab9f2115SMatthew Garrett __initcall(apparmor_nf_ip_init);
1910e1af4779SArnd Bergmann #endif
1911ab9f2115SMatthew Garrett 
apparmor_init(void)1912b5e95b48SJohn Johansen static int __init apparmor_init(void)
1913b5e95b48SJohn Johansen {
1914b5e95b48SJohn Johansen 	int error;
1915b5e95b48SJohn Johansen 
191611c236b8SJohn Johansen 	error = aa_setup_dfa_engine();
191711c236b8SJohn Johansen 	if (error) {
191811c236b8SJohn Johansen 		AA_ERROR("Unable to setup dfa engine\n");
191911c236b8SJohn Johansen 		goto alloc_out;
192011c236b8SJohn Johansen 	}
192111c236b8SJohn Johansen 
1922b5e95b48SJohn Johansen 	error = aa_alloc_root_ns();
1923b5e95b48SJohn Johansen 	if (error) {
1924b5e95b48SJohn Johansen 		AA_ERROR("Unable to allocate default profile namespace\n");
1925b5e95b48SJohn Johansen 		goto alloc_out;
1926b5e95b48SJohn Johansen 	}
1927b5e95b48SJohn Johansen 
1928e3ea1ca5STyler Hicks 	error = apparmor_init_sysctl();
1929e3ea1ca5STyler Hicks 	if (error) {
1930e3ea1ca5STyler Hicks 		AA_ERROR("Unable to register sysctls\n");
1931e3ea1ca5STyler Hicks 		goto alloc_out;
1932e3ea1ca5STyler Hicks 
1933e3ea1ca5STyler Hicks 	}
1934e3ea1ca5STyler Hicks 
1935d4669f0bSJohn Johansen 	error = alloc_buffers();
1936d4669f0bSJohn Johansen 	if (error) {
1937d4669f0bSJohn Johansen 		AA_ERROR("Unable to allocate work buffers\n");
1938df323337SSebastian Andrzej Siewior 		goto alloc_out;
1939d4669f0bSJohn Johansen 	}
1940d4669f0bSJohn Johansen 
194155a26ebfSJohn Johansen 	error = set_init_ctx();
1942b5e95b48SJohn Johansen 	if (error) {
1943b5e95b48SJohn Johansen 		AA_ERROR("Failed to set context on init task\n");
1944b1d9e6b0SCasey Schaufler 		aa_free_root_ns();
1945d4669f0bSJohn Johansen 		goto buffers_out;
1946b5e95b48SJohn Johansen 	}
1947d69dece5SCasey Schaufler 	security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1948d69dece5SCasey Schaufler 				"apparmor");
1949b5e95b48SJohn Johansen 
1950b5e95b48SJohn Johansen 	/* Report that AppArmor successfully initialized */
1951b5e95b48SJohn Johansen 	apparmor_initialized = 1;
1952b5e95b48SJohn Johansen 	if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1953b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized: complain mode enabled");
1954b5e95b48SJohn Johansen 	else if (aa_g_profile_mode == APPARMOR_KILL)
1955b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized: kill mode enabled");
1956b5e95b48SJohn Johansen 	else
1957b5e95b48SJohn Johansen 		aa_info_message("AppArmor initialized");
1958b5e95b48SJohn Johansen 
1959b5e95b48SJohn Johansen 	return error;
1960b5e95b48SJohn Johansen 
1961d4669f0bSJohn Johansen buffers_out:
1962d4669f0bSJohn Johansen 	destroy_buffers();
1963b5e95b48SJohn Johansen alloc_out:
1964b5e95b48SJohn Johansen 	aa_destroy_aafs();
196511c236b8SJohn Johansen 	aa_teardown_dfa_engine();
1966b5e95b48SJohn Johansen 
1967954317feSThomas Meyer 	apparmor_enabled = false;
1968b5e95b48SJohn Johansen 	return error;
1969b5e95b48SJohn Johansen }
1970b5e95b48SJohn Johansen 
19713d6e5f6dSKees Cook DEFINE_LSM(apparmor) = {
197207aed2f2SKees Cook 	.name = "apparmor",
197314bd99c8SKees Cook 	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
1974c5459b82SKees Cook 	.enabled = &apparmor_enabled,
1975bbd3662aSCasey Schaufler 	.blobs = &apparmor_blob_sizes,
19763d6e5f6dSKees Cook 	.init = apparmor_init,
19773d6e5f6dSKees Cook };
1978