xref: /linux/security/apparmor/lsm.c (revision 9f2347842b526cbc2655068591fb0166362d2999)
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>
27f3b8788cSCasey Schaufler #include <uapi/linux/lsm.h>
28b5e95b48SJohn Johansen 
29b5e95b48SJohn Johansen #include "include/af_unix.h"
30b5e95b48SJohn Johansen #include "include/apparmor.h"
31b5e95b48SJohn Johansen #include "include/apparmorfs.h"
32b5e95b48SJohn Johansen #include "include/audit.h"
33d8889d49SJohn Johansen #include "include/capability.h"
34b5e95b48SJohn Johansen #include "include/cred.h"
35b5e95b48SJohn Johansen #include "include/file.h"
3656974a6fSJohn Johansen #include "include/ipc.h"
37b5e95b48SJohn Johansen #include "include/net.h"
38637f688dSJohn Johansen #include "include/path.h"
39b5e95b48SJohn Johansen #include "include/label.h"
40cff281f6SJohn Johansen #include "include/policy.h"
41b5e95b48SJohn Johansen #include "include/policy_ns.h"
422ea3ffb7SJohn Johansen #include "include/procattr.h"
43c0929212SJohn Johansen #include "include/mount.h"
44b5e95b48SJohn Johansen #include "include/secid.h"
45b5e95b48SJohn Johansen 
46545de8feSJohn Johansen /* Flag indicating whether initialization completed */
47b5e95b48SJohn Johansen int apparmor_initialized;
48df323337SSebastian Andrzej Siewior 
49df323337SSebastian Andrzej Siewior union aa_buffer {
50ba808cb5SKees Cook 	struct list_head list;
51df323337SSebastian Andrzej Siewior 	DECLARE_FLEX_ARRAY(char, buffer);
52d4669f0bSJohn Johansen };
53ea9bae12SJohn Johansen 
54ea9bae12SJohn Johansen struct aa_local_cache {
55ea9bae12SJohn Johansen 	unsigned int hold;
56ea9bae12SJohn Johansen 	unsigned int count;
57ea9bae12SJohn Johansen 	struct list_head head;
58ea9bae12SJohn Johansen };
59341c1fdaSJohn Johansen 
60341c1fdaSJohn Johansen #define RESERVE_COUNT 2
61341c1fdaSJohn Johansen static int reserve_count = RESERVE_COUNT;
62341c1fdaSJohn Johansen static int buffer_count;
63df323337SSebastian Andrzej Siewior 
64df323337SSebastian Andrzej Siewior static LIST_HEAD(aa_global_buffers);
65ea9bae12SJohn Johansen static DEFINE_SPINLOCK(aa_buffers_lock);
66d4669f0bSJohn Johansen static DEFINE_PER_CPU(struct aa_local_cache, aa_local_buffers);
67b5e95b48SJohn Johansen 
68b5e95b48SJohn Johansen /*
69b5e95b48SJohn Johansen  * LSM hook functions
70b5e95b48SJohn Johansen  */
71b5e95b48SJohn Johansen 
72d9087c49SJohn Johansen /*
73b5e95b48SJohn Johansen  * put the associated labels
74b5e95b48SJohn Johansen  */
apparmor_cred_free(struct cred * cred)75b5e95b48SJohn Johansen static void apparmor_cred_free(struct cred *cred)
76d9087c49SJohn Johansen {
7769b5a44aSCasey Schaufler 	aa_put_label(cred_label(cred));
78b5e95b48SJohn Johansen 	set_cred_label(cred, NULL);
79b5e95b48SJohn Johansen }
80b5e95b48SJohn Johansen 
81b5e95b48SJohn Johansen /*
82b5e95b48SJohn Johansen  * allocate the apparmor part of blank credentials
83b5e95b48SJohn Johansen  */
apparmor_cred_alloc_blank(struct cred * cred,gfp_t gfp)84b5e95b48SJohn Johansen static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
8569b5a44aSCasey Schaufler {
86b5e95b48SJohn Johansen 	set_cred_label(cred, NULL);
87b5e95b48SJohn Johansen 	return 0;
88b5e95b48SJohn Johansen }
89b5e95b48SJohn Johansen 
90d9087c49SJohn Johansen /*
91b5e95b48SJohn Johansen  * prepare new cred label for modification by prepare_cred block
92b5e95b48SJohn Johansen  */
apparmor_cred_prepare(struct cred * new,const struct cred * old,gfp_t gfp)93b5e95b48SJohn Johansen static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
94b5e95b48SJohn Johansen 				 gfp_t gfp)
9569b5a44aSCasey Schaufler {
96b5e95b48SJohn Johansen 	set_cred_label(new, aa_get_newest_label(cred_label(old)));
97b5e95b48SJohn Johansen 	return 0;
98b5e95b48SJohn Johansen }
99b5e95b48SJohn Johansen 
100b5e95b48SJohn Johansen /*
101b5e95b48SJohn Johansen  * transfer the apparmor data to a blank set of creds
102b5e95b48SJohn Johansen  */
apparmor_cred_transfer(struct cred * new,const struct cred * old)103b5e95b48SJohn Johansen static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
10469b5a44aSCasey Schaufler {
105b5e95b48SJohn Johansen 	set_cred_label(new, aa_get_newest_label(cred_label(old)));
106b5e95b48SJohn Johansen }
1073b529a76SJohn Johansen 
apparmor_task_free(struct task_struct * task)1083b529a76SJohn Johansen static void apparmor_task_free(struct task_struct *task)
1093b529a76SJohn Johansen {
1103b529a76SJohn Johansen 
1113b529a76SJohn Johansen 	aa_free_task_ctx(task_ctx(task));
1123b529a76SJohn Johansen }
1133b529a76SJohn Johansen 
apparmor_task_alloc(struct task_struct * task,u64 clone_flags)1143b529a76SJohn Johansen static int apparmor_task_alloc(struct task_struct *task,
1153b529a76SJohn Johansen 			       u64 clone_flags)
116f4ad8f2cSCasey Schaufler {
1173b529a76SJohn Johansen 	struct aa_task_ctx *new = task_ctx(task);
118de62de59SJohn Johansen 
1193b529a76SJohn Johansen 	aa_dup_task_ctx(new, task_ctx(current));
1203b529a76SJohn Johansen 
121b5e95b48SJohn Johansen 	return 0;
122b5e95b48SJohn Johansen }
123b5e95b48SJohn Johansen 
apparmor_ptrace_access_check(struct task_struct * child,unsigned int mode)124b5e95b48SJohn Johansen static int apparmor_ptrace_access_check(struct task_struct *child,
125b5e95b48SJohn Johansen 					unsigned int mode)
126b2d09ae4SJohn Johansen {
12790c436a6SJohn Johansen 	struct aa_label *tracer, *tracee;
128b2d09ae4SJohn Johansen 	const struct cred *cred;
129b2d09ae4SJohn Johansen 	int error;
13090c436a6SJohn Johansen 	bool needput;
13190c436a6SJohn Johansen 
1321f8266ffSJann Horn 	cred = get_task_cred(child);
13390c436a6SJohn Johansen 	tracee = cred_label(cred);	/* ref count on cred */
134338d0be4SJohn Johansen 	tracer = __begin_current_label_crit_section(&needput);
135338d0be4SJohn Johansen 	error = aa_may_ptrace(current_cred(), tracer, cred, tracee,
1361f8266ffSJann Horn 			(mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
13790c436a6SJohn Johansen 						  : AA_PTRACE_TRACE);
138b2d09ae4SJohn Johansen 	__end_current_label_crit_section(tracer, needput);
139b2d09ae4SJohn Johansen 	put_cred(cred);
140b5e95b48SJohn Johansen 
141b5e95b48SJohn Johansen 	return error;
142b5e95b48SJohn Johansen }
143b5e95b48SJohn Johansen 
apparmor_ptrace_traceme(struct task_struct * parent)144b2d09ae4SJohn Johansen static int apparmor_ptrace_traceme(struct task_struct *parent)
14590c436a6SJohn Johansen {
146b2d09ae4SJohn Johansen 	struct aa_label *tracer, *tracee;
147b2d09ae4SJohn Johansen 	const struct cred *cred;
148ca3fde52SJann Horn 	int error;
14990c436a6SJohn Johansen 	bool needput;
15090c436a6SJohn Johansen 
15190c436a6SJohn Johansen 	tracee = __begin_current_label_crit_section(&needput);
15290c436a6SJohn Johansen 	cred = get_task_cred(parent);
15390c436a6SJohn Johansen 	tracer = cred_label(cred);	/* ref count on cred */
154ca3fde52SJann Horn 	error = aa_may_ptrace(cred, tracer, current_cred(), tracee,
155b2d09ae4SJohn Johansen 			      AA_PTRACE_TRACE);
156b2d09ae4SJohn Johansen 	put_cred(cred);
157b5e95b48SJohn Johansen 	__end_current_label_crit_section(tracee, needput);
158b5e95b48SJohn Johansen 
159b5e95b48SJohn Johansen 	return error;
1606672efbbSKhadija Kamran }
161b5e95b48SJohn Johansen 
162b5e95b48SJohn 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)163637f688dSJohn Johansen static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
164b5e95b48SJohn Johansen 			   kernel_cap_t *inheritable, kernel_cap_t *permitted)
165b5e95b48SJohn Johansen {
166b5e95b48SJohn Johansen 	struct aa_label *label;
167b5e95b48SJohn Johansen 	const struct cred *cred;
168637f688dSJohn Johansen 
169c70c86c4SJohn Johansen 	rcu_read_lock();
170b1d9e6b0SCasey Schaufler 	cred = __task_cred(target);
171b1d9e6b0SCasey Schaufler 	label = aa_get_newest_cred_label(cred);
172b1d9e6b0SCasey Schaufler 
173b1d9e6b0SCasey Schaufler 	/*
174c70c86c4SJohn Johansen 	 * cap_capget is stacked ahead of this and will
175c70c86c4SJohn Johansen 	 * initialize effective and permitted.
176c70c86c4SJohn Johansen 	 */
177c70c86c4SJohn Johansen 	if (!unconfined(label)) {
178c70c86c4SJohn Johansen 		struct aa_profile *profile;
1791ad22fccSJohn Johansen 		struct label_it i;
180c70c86c4SJohn Johansen 
181c70c86c4SJohn Johansen 		label_for_each_confined(i, label, profile) {
1821ad22fccSJohn Johansen 			kernel_cap_t allowed;
1831ad22fccSJohn Johansen 
184c70c86c4SJohn Johansen 			allowed = aa_profile_capget(profile);
1851ad22fccSJohn Johansen 			*effective = cap_intersect(*effective, allowed);
186c70c86c4SJohn Johansen 			*permitted = cap_intersect(*permitted, allowed);
1871ad22fccSJohn Johansen 		}
188c70c86c4SJohn Johansen 	}
189b5e95b48SJohn Johansen 	rcu_read_unlock();
190b5e95b48SJohn Johansen 	aa_put_label(label);
191637f688dSJohn Johansen 
192b5e95b48SJohn Johansen 	return 0;
193b5e95b48SJohn Johansen }
194b5e95b48SJohn Johansen 
apparmor_capable(const struct cred * cred,struct user_namespace * ns,int cap,unsigned int opts)195b5e95b48SJohn Johansen static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
1966a9de491SEric Paris 			    int cap, unsigned int opts)
197c1a85a00SMicah Morton {
198b5e95b48SJohn Johansen 	struct aa_label *label;
199637f688dSJohn Johansen 	int error = 0;
200b1d9e6b0SCasey Schaufler 
201b1d9e6b0SCasey Schaufler 	label = aa_get_newest_cred_label(cred);
202637f688dSJohn Johansen 	if (!unconfined(label))
203637f688dSJohn Johansen 		error = aa_capable(cred, label, cap, opts);
20490c436a6SJohn Johansen 	aa_put_label(label);
205637f688dSJohn Johansen 
206cf797c0eSJohn Johansen 	return error;
207b5e95b48SJohn Johansen }
208b5e95b48SJohn Johansen 
209b5e95b48SJohn Johansen /**
210b5e95b48SJohn Johansen  * common_perm - basic common permission check wrapper fn for paths
211b5e95b48SJohn Johansen  * @op: operation being checked
212b5e95b48SJohn Johansen  * @path: path to check permission of  (NOT NULL)
213b5e95b48SJohn Johansen  * @mask: requested permissions mask
214b5e95b48SJohn Johansen  * @cond: conditional info for the permission request  (NOT NULL)
215b5e95b48SJohn Johansen  *
216b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
217b5e95b48SJohn Johansen  */
common_perm(const char * op,const struct path * path,u32 mask,struct path_cond * cond)218b5e95b48SJohn Johansen static int common_perm(const char *op, const struct path *path, u32 mask,
21947f6e5ccSJohn Johansen 		       struct path_cond *cond)
220b5e95b48SJohn Johansen {
221b5e95b48SJohn Johansen 	struct aa_label *label;
222637f688dSJohn Johansen 	int error = 0;
223b5e95b48SJohn Johansen 	bool needput;
224b5e95b48SJohn Johansen 
225637f688dSJohn Johansen 	label = __begin_current_label_crit_section(&needput);
226637f688dSJohn Johansen 	if (!unconfined(label))
22790c436a6SJohn Johansen 		error = aa_path_perm(op, current_cred(), label, path, 0, mask,
22890c436a6SJohn Johansen 				     cond);
229637f688dSJohn Johansen 	__end_current_label_crit_section(label, needput);
230b5e95b48SJohn Johansen 
231b5e95b48SJohn Johansen 	return error;
232b5e95b48SJohn Johansen }
233b5e95b48SJohn Johansen 
234b5e95b48SJohn Johansen /**
23531f75bfeSJohn Johansen  * common_perm_cond - common permission wrapper around inode cond
23631f75bfeSJohn Johansen  * @op: operation being checked
23731f75bfeSJohn Johansen  * @path: location to check (NOT NULL)
23831f75bfeSJohn Johansen  * @mask: requested permissions mask
23931f75bfeSJohn Johansen  *
24031f75bfeSJohn Johansen  * Returns: %0 else error code if error or permission denied
24131f75bfeSJohn Johansen  */
common_perm_cond(const char * op,const struct path * path,u32 mask)24231f75bfeSJohn Johansen static int common_perm_cond(const char *op, const struct path *path, u32 mask)
24331f75bfeSJohn Johansen {
244e67fe633SChristian Brauner 	vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
2455e26a01eSChristian Brauner 					    d_backing_inode(path->dentry));
2463cee6079SChristian Brauner 	struct path_cond cond = {
2475e26a01eSChristian Brauner 		vfsuid_into_kuid(vfsuid),
24831f75bfeSJohn Johansen 		d_backing_inode(path->dentry)->i_mode
24931f75bfeSJohn Johansen 	};
25031f75bfeSJohn Johansen 
25131f75bfeSJohn Johansen 	if (!path_mediated_fs(path->dentry))
25231f75bfeSJohn Johansen 		return 0;
25331f75bfeSJohn Johansen 
25431f75bfeSJohn Johansen 	return common_perm(op, path, mask, &cond);
25531f75bfeSJohn Johansen }
25631f75bfeSJohn Johansen 
25731f75bfeSJohn Johansen /**
258b5e95b48SJohn Johansen  * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
259b5e95b48SJohn Johansen  * @op: operation being checked
260b5e95b48SJohn Johansen  * @dir: directory of the dentry  (NOT NULL)
261b5e95b48SJohn Johansen  * @dentry: dentry to check  (NOT NULL)
262b5e95b48SJohn Johansen  * @mask: requested permissions mask
263b5e95b48SJohn Johansen  * @cond: conditional info for the permission request  (NOT NULL)
264b5e95b48SJohn Johansen  *
265b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
266b5e95b48SJohn Johansen  */
common_perm_dir_dentry(const char * op,const struct path * dir,struct dentry * dentry,u32 mask,struct path_cond * cond)26747f6e5ccSJohn Johansen static int common_perm_dir_dentry(const char *op, const struct path *dir,
268b5e95b48SJohn Johansen 				  struct dentry *dentry, u32 mask,
269b5e95b48SJohn Johansen 				  struct path_cond *cond)
270b5e95b48SJohn Johansen {
2718486adf0SKees Cook 	struct path path = { .mnt = dir->mnt, .dentry = dentry };
272b5e95b48SJohn Johansen 
273b5e95b48SJohn Johansen 	return common_perm(op, &path, mask, cond);
274b5e95b48SJohn Johansen }
275b5e95b48SJohn Johansen 
276b5e95b48SJohn Johansen /**
277b5e95b48SJohn Johansen  * common_perm_rm - common permission wrapper for operations doing rm
278b5e95b48SJohn Johansen  * @op: operation being checked
279b5e95b48SJohn Johansen  * @dir: directory that the dentry is in  (NOT NULL)
280b5e95b48SJohn Johansen  * @dentry: dentry being rm'd  (NOT NULL)
281b5e95b48SJohn Johansen  * @mask: requested permission mask
282b5e95b48SJohn Johansen  *
283b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
284b5e95b48SJohn Johansen  */
common_perm_rm(const char * op,const struct path * dir,struct dentry * dentry,u32 mask)28547f6e5ccSJohn Johansen static int common_perm_rm(const char *op, const struct path *dir,
286b5e95b48SJohn Johansen 			  struct dentry *dentry, u32 mask)
287b5e95b48SJohn Johansen {
288c6f493d6SDavid Howells 	struct inode *inode = d_backing_inode(dentry);
289b5e95b48SJohn Johansen 	struct path_cond cond = { };
2905e26a01eSChristian Brauner 	vfsuid_t vfsuid;
291b5e95b48SJohn Johansen 
292efeee83aSJohn Johansen 	if (!inode || !path_mediated_fs(dentry))
293b5e95b48SJohn Johansen 		return 0;
294b5e95b48SJohn Johansen 
295e67fe633SChristian Brauner 	vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
2965e26a01eSChristian Brauner 	cond.uid = vfsuid_into_kuid(vfsuid);
297b5e95b48SJohn Johansen 	cond.mode = inode->i_mode;
298b5e95b48SJohn Johansen 
299b5e95b48SJohn Johansen 	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
300b5e95b48SJohn Johansen }
301b5e95b48SJohn Johansen 
302b5e95b48SJohn Johansen /**
303b5e95b48SJohn Johansen  * common_perm_create - common permission wrapper for operations doing create
304b5e95b48SJohn Johansen  * @op: operation being checked
305b5e95b48SJohn Johansen  * @dir: directory that dentry will be created in  (NOT NULL)
306b5e95b48SJohn Johansen  * @dentry: dentry to create   (NOT NULL)
307b5e95b48SJohn Johansen  * @mask: request permission mask
308b5e95b48SJohn Johansen  * @mode: created file mode
309b5e95b48SJohn Johansen  *
310b5e95b48SJohn Johansen  * Returns: %0 else error code if error or permission denied
311b5e95b48SJohn Johansen  */
common_perm_create(const char * op,const struct path * dir,struct dentry * dentry,u32 mask,umode_t mode)31247f6e5ccSJohn Johansen static int common_perm_create(const char *op, const struct path *dir,
313d6b49f7aSAl Viro 			      struct dentry *dentry, u32 mask, umode_t mode)
314b5e95b48SJohn Johansen {
315b5e95b48SJohn Johansen 	struct path_cond cond = { current_fsuid(), mode };
316b5e95b48SJohn Johansen 
317efeee83aSJohn Johansen 	if (!path_mediated_fs(dir->dentry))
318b5e95b48SJohn Johansen 		return 0;
319b5e95b48SJohn Johansen 
320b5e95b48SJohn Johansen 	return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
321b5e95b48SJohn Johansen }
322b5e95b48SJohn Johansen 
apparmor_path_unlink(const struct path * dir,struct dentry * dentry)323989f74e0SAl Viro static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
324b5e95b48SJohn Johansen {
325b5e95b48SJohn Johansen 	return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
326b5e95b48SJohn Johansen }
327b5e95b48SJohn Johansen 
apparmor_path_mkdir(const struct path * dir,struct dentry * dentry,umode_t mode)328d3607752SAl Viro static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
3294572befeSAl Viro 			       umode_t mode)
330b5e95b48SJohn Johansen {
331b5e95b48SJohn Johansen 	return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
332b5e95b48SJohn Johansen 				  S_IFDIR);
333b5e95b48SJohn Johansen }
334b5e95b48SJohn Johansen 
apparmor_path_rmdir(const struct path * dir,struct dentry * dentry)335989f74e0SAl Viro static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
336b5e95b48SJohn Johansen {
337b5e95b48SJohn Johansen 	return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
338b5e95b48SJohn Johansen }
339b5e95b48SJohn Johansen 
apparmor_path_mknod(const struct path * dir,struct dentry * dentry,umode_t mode,unsigned int dev)340d3607752SAl Viro static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
34104fc66e7SAl Viro 			       umode_t mode, unsigned int dev)
342b5e95b48SJohn Johansen {
343b5e95b48SJohn Johansen 	return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
344b5e95b48SJohn Johansen }
345b5e95b48SJohn Johansen 
apparmor_path_truncate(const struct path * path)34681f4c506SAl Viro static int apparmor_path_truncate(const struct path *path)
347b5e95b48SJohn Johansen {
348e53cfe6cSJohn Johansen 	return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
349b5e95b48SJohn Johansen }
350b5e95b48SJohn Johansen 
apparmor_file_truncate(struct file * file)3513350607dSGünther Noack static int apparmor_file_truncate(struct file *file)
3523350607dSGünther Noack {
3533350607dSGünther Noack 	return apparmor_path_truncate(&file->f_path);
3543350607dSGünther Noack }
3553350607dSGünther Noack 
apparmor_path_symlink(const struct path * dir,struct dentry * dentry,const char * old_name)356d3607752SAl Viro static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
357b5e95b48SJohn Johansen 				 const char *old_name)
358b5e95b48SJohn Johansen {
359b5e95b48SJohn Johansen 	return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
360b5e95b48SJohn Johansen 				  S_IFLNK);
361b5e95b48SJohn Johansen }
362b5e95b48SJohn Johansen 
apparmor_path_link(struct dentry * old_dentry,const struct path * new_dir,struct dentry * new_dentry)3633ccee46aSAl Viro static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
364b5e95b48SJohn Johansen 			      struct dentry *new_dentry)
365b5e95b48SJohn Johansen {
366637f688dSJohn Johansen 	struct aa_label *label;
367b5e95b48SJohn Johansen 	int error = 0;
368b5e95b48SJohn Johansen 
369efeee83aSJohn Johansen 	if (!path_mediated_fs(old_dentry))
370b5e95b48SJohn Johansen 		return 0;
371b5e95b48SJohn Johansen 
372637f688dSJohn Johansen 	label = begin_current_label_crit_section();
373637f688dSJohn Johansen 	if (!unconfined(label))
37490c436a6SJohn Johansen 		error = aa_path_link(current_cred(), label, old_dentry, new_dir,
37590c436a6SJohn Johansen 				     new_dentry);
376637f688dSJohn Johansen 	end_current_label_crit_section(label);
377cf797c0eSJohn Johansen 
378b5e95b48SJohn Johansen 	return error;
379b5e95b48SJohn Johansen }
380b5e95b48SJohn 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)3813ccee46aSAl Viro static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
382100f59d9SMickaël Salaün 				const struct path *new_dir, struct dentry *new_dentry,
383100f59d9SMickaël Salaün 				const unsigned int flags)
384b5e95b48SJohn Johansen {
385637f688dSJohn Johansen 	struct aa_label *label;
386b5e95b48SJohn Johansen 	int error = 0;
387b5e95b48SJohn Johansen 
388efeee83aSJohn Johansen 	if (!path_mediated_fs(old_dentry))
389b5e95b48SJohn Johansen 		return 0;
390100f59d9SMickaël Salaün 	if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
391100f59d9SMickaël Salaün 		return 0;
392b5e95b48SJohn Johansen 
393637f688dSJohn Johansen 	label = begin_current_label_crit_section();
394637f688dSJohn Johansen 	if (!unconfined(label)) {
395e67fe633SChristian Brauner 		struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
3965e26a01eSChristian Brauner 		vfsuid_t vfsuid;
3978486adf0SKees Cook 		struct path old_path = { .mnt = old_dir->mnt,
3988486adf0SKees Cook 					 .dentry = old_dentry };
3998486adf0SKees Cook 		struct path new_path = { .mnt = new_dir->mnt,
4008486adf0SKees Cook 					 .dentry = new_dentry };
4013cee6079SChristian Brauner 		struct path_cond cond = {
4025e26a01eSChristian Brauner 			.mode = d_backing_inode(old_dentry)->i_mode
403b5e95b48SJohn Johansen 		};
404e67fe633SChristian Brauner 		vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
4055e26a01eSChristian Brauner 		cond.uid = vfsuid_into_kuid(vfsuid);
406b5e95b48SJohn Johansen 
407100f59d9SMickaël Salaün 		if (flags & RENAME_EXCHANGE) {
408100f59d9SMickaël Salaün 			struct path_cond cond_exchange = {
4095e26a01eSChristian Brauner 				.mode = d_backing_inode(new_dentry)->i_mode,
410100f59d9SMickaël Salaün 			};
411e67fe633SChristian Brauner 			vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
4125e26a01eSChristian Brauner 			cond_exchange.uid = vfsuid_into_kuid(vfsuid);
413100f59d9SMickaël Salaün 
41490c436a6SJohn Johansen 			error = aa_path_perm(OP_RENAME_SRC, current_cred(),
41590c436a6SJohn Johansen 					     label, &new_path, 0,
416100f59d9SMickaël Salaün 					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
417100f59d9SMickaël Salaün 					     AA_MAY_SETATTR | AA_MAY_DELETE,
418100f59d9SMickaël Salaün 					     &cond_exchange);
419100f59d9SMickaël Salaün 			if (!error)
42090c436a6SJohn Johansen 				error = aa_path_perm(OP_RENAME_DEST, current_cred(),
42190c436a6SJohn Johansen 						     label, &old_path,
422100f59d9SMickaël Salaün 						     0, MAY_WRITE | AA_MAY_SETATTR |
423100f59d9SMickaël Salaün 						     AA_MAY_CREATE, &cond_exchange);
424100f59d9SMickaël Salaün 		}
425100f59d9SMickaël Salaün 
426100f59d9SMickaël Salaün 		if (!error)
42790c436a6SJohn Johansen 			error = aa_path_perm(OP_RENAME_SRC, current_cred(),
42890c436a6SJohn Johansen 					     label, &old_path, 0,
429e53cfe6cSJohn Johansen 					     MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
430e53cfe6cSJohn Johansen 					     AA_MAY_SETATTR | AA_MAY_DELETE,
431b5e95b48SJohn Johansen 					     &cond);
432b5e95b48SJohn Johansen 		if (!error)
43390c436a6SJohn Johansen 			error = aa_path_perm(OP_RENAME_DEST, current_cred(),
43490c436a6SJohn Johansen 					     label, &new_path,
435e53cfe6cSJohn Johansen 					     0, MAY_WRITE | AA_MAY_SETATTR |
436b5e95b48SJohn Johansen 					     AA_MAY_CREATE, &cond);
437b5e95b48SJohn Johansen 
438b5e95b48SJohn Johansen 	}
439637f688dSJohn Johansen 	end_current_label_crit_section(label);
440cf797c0eSJohn Johansen 
441b5e95b48SJohn Johansen 	return error;
442b5e95b48SJohn Johansen }
443b5e95b48SJohn Johansen 
apparmor_path_chmod(const struct path * path,umode_t mode)444be01f9f2SAl Viro static int apparmor_path_chmod(const struct path *path, umode_t mode)
445b5e95b48SJohn Johansen {
44631f75bfeSJohn Johansen 	return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
447b5e95b48SJohn Johansen }
448b5e95b48SJohn Johansen 
apparmor_path_chown(const struct path * path,kuid_t uid,kgid_t gid)4497fd25dacSAl Viro static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
450b5e95b48SJohn Johansen {
45131f75bfeSJohn Johansen 	return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
452b5e95b48SJohn Johansen }
453b5e95b48SJohn Johansen 
apparmor_inode_getattr(const struct path * path)4543f7036a0SAl Viro static int apparmor_inode_getattr(const struct path *path)
455b5e95b48SJohn Johansen {
456e53cfe6cSJohn Johansen 	return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
457b5e95b48SJohn Johansen }
458b5e95b48SJohn Johansen 
apparmor_file_open(struct file * file)45994817692SAl Viro static int apparmor_file_open(struct file *file)
460b5e95b48SJohn Johansen {
461637f688dSJohn Johansen 	struct aa_file_ctx *fctx = file_ctx(file);
462637f688dSJohn Johansen 	struct aa_label *label;
463b5e95b48SJohn Johansen 	int error = 0;
464b5e95b48SJohn Johansen 	bool needput;
465efeee83aSJohn Johansen 
466b5e95b48SJohn Johansen 	if (!path_mediated_fs(file->f_path.dentry))
467b5e95b48SJohn Johansen 		return 0;
468b5e95b48SJohn Johansen 
469b5e95b48SJohn Johansen 	/* If in exec, permission is handled by bprm hooks.
470b5e95b48SJohn Johansen 	 * Cache permissions granted by the previous exec check, with
471b5e95b48SJohn Johansen 	 * implicit read and executable mmap which are required to
4724759ff71SKees Cook 	 * actually execute the image.
4734759ff71SKees Cook 	 *
474b5e95b48SJohn Johansen 	 * Illogically, FMODE_EXEC is in f_flags, not f_mode.
4754759ff71SKees Cook 	 */
47655a26ebfSJohn Johansen 	if (file->f_flags & __FMODE_EXEC) {
477b5e95b48SJohn Johansen 		fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
478b5e95b48SJohn Johansen 		return 0;
479b5e95b48SJohn Johansen 	}
48094817692SAl Viro 
481637f688dSJohn Johansen 	label = aa_get_newest_cred_label_condref(file->f_cred, &needput);
482e67fe633SChristian Brauner 	if (!unconfined(label)) {
483496ad9aaSAl Viro 		struct mnt_idmap *idmap = file_mnt_idmap(file);
4845e26a01eSChristian Brauner 		struct inode *inode = file_inode(file);
4853cee6079SChristian Brauner 		vfsuid_t vfsuid;
4865e26a01eSChristian Brauner 		struct path_cond cond = {
4873cee6079SChristian Brauner 			.mode = inode->i_mode,
488e67fe633SChristian Brauner 		};
4895e26a01eSChristian Brauner 		vfsuid = i_uid_into_vfsuid(idmap, inode);
490b5e95b48SJohn Johansen 		cond.uid = vfsuid_into_kuid(vfsuid);
49190c436a6SJohn Johansen 
49290c436a6SJohn Johansen 		error = aa_path_perm(OP_OPEN, file->f_cred,
493b5e95b48SJohn Johansen 				     label, &file->f_path, 0,
494b5e95b48SJohn Johansen 				     aa_map_file_to_perms(file), &cond);
49555a26ebfSJohn Johansen 		/* todo cache full allowed permissions set and state */
496b5e95b48SJohn Johansen 		fctx->allow = aa_map_file_to_perms(file);
497637f688dSJohn Johansen 	}
498b5e95b48SJohn Johansen 	aa_put_label_condref(label, needput);
499b5e95b48SJohn Johansen 
500b5e95b48SJohn Johansen 	return error;
501b5e95b48SJohn Johansen }
502b5e95b48SJohn Johansen 
apparmor_file_alloc_security(struct file * file)503b5e95b48SJohn Johansen static int apparmor_file_alloc_security(struct file *file)
50433bf60caSCasey Schaufler {
505637f688dSJohn Johansen 	struct aa_file_ctx *ctx = file_ctx(file);
506b5e95b48SJohn Johansen 	struct aa_label *label = begin_current_label_crit_section();
50733bf60caSCasey Schaufler 
50833bf60caSCasey Schaufler 	spin_lock_init(&ctx->lock);
50933bf60caSCasey Schaufler 	rcu_assign_pointer(ctx->label, aa_get_label(label));
51033bf60caSCasey Schaufler 	end_current_label_crit_section(label);
511b5e95b48SJohn Johansen 	return 0;
512b5e95b48SJohn Johansen }
513b5e95b48SJohn Johansen 
apparmor_file_free_security(struct file * file)514b5e95b48SJohn Johansen static void apparmor_file_free_security(struct file *file)
51533bf60caSCasey Schaufler {
51633bf60caSCasey Schaufler 	struct aa_file_ctx *ctx = file_ctx(file);
51733bf60caSCasey Schaufler 
51833bf60caSCasey Schaufler 	if (ctx)
519b5e95b48SJohn Johansen 		aa_put_label(rcu_access_pointer(ctx->label));
520b5e95b48SJohn Johansen }
521341c1fdaSJohn Johansen 
common_file_perm(const char * op,struct file * file,u32 mask,bool in_atomic)522341c1fdaSJohn Johansen static int common_file_perm(const char *op, struct file *file, u32 mask,
523b5e95b48SJohn Johansen 			    bool in_atomic)
524190a9518SJohn Johansen {
525b5e95b48SJohn Johansen 	struct aa_label *label;
526b5e95b48SJohn Johansen 	int error = 0;
527192ca6b5SJohn Johansen 	bool needput;
528192ca6b5SJohn Johansen 
529192ca6b5SJohn Johansen 	/* don't reaudit files closed during inheritance */
530192ca6b5SJohn Johansen 	if (unlikely(file->f_path.dentry == aa_null.dentry))
531637f688dSJohn Johansen 		return -EACCES;
53290c436a6SJohn Johansen 
533637f688dSJohn Johansen 	label = __begin_current_label_crit_section(&needput);
534b5e95b48SJohn Johansen 	error = aa_file_perm(op, current_cred(), label, file, mask, in_atomic);
535b5e95b48SJohn Johansen 	__end_current_label_crit_section(label, needput);
536b5e95b48SJohn Johansen 
537b5e95b48SJohn Johansen 	return error;
538064dc947SJohn Johansen }
539064dc947SJohn Johansen 
apparmor_file_receive(struct file * file)540341c1fdaSJohn Johansen static int apparmor_file_receive(struct file *file)
541341c1fdaSJohn Johansen {
542064dc947SJohn Johansen 	return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
543064dc947SJohn Johansen 				false);
544b5e95b48SJohn Johansen }
545b5e95b48SJohn Johansen 
apparmor_file_permission(struct file * file,int mask)546341c1fdaSJohn Johansen static int apparmor_file_permission(struct file *file, int mask)
547b5e95b48SJohn Johansen {
548b5e95b48SJohn Johansen 	return common_file_perm(OP_FPERM, file, mask, false);
549b5e95b48SJohn Johansen }
550b5e95b48SJohn Johansen 
apparmor_file_lock(struct file * file,unsigned int cmd)551b5e95b48SJohn Johansen static int apparmor_file_lock(struct file *file, unsigned int cmd)
552b5e95b48SJohn Johansen {
553b5e95b48SJohn Johansen 	u32 mask = AA_MAY_LOCK;
554b5e95b48SJohn Johansen 
555b5e95b48SJohn Johansen 	if (cmd == F_WRLCK)
556341c1fdaSJohn Johansen 		mask |= MAY_WRITE;
557b5e95b48SJohn Johansen 
558b5e95b48SJohn Johansen 	return common_file_perm(OP_FLOCK, file, mask, false);
55947f6e5ccSJohn Johansen }
560341c1fdaSJohn Johansen 
common_mmap(const char * op,struct file * file,unsigned long prot,unsigned long flags,bool in_atomic)561b5e95b48SJohn Johansen static int common_mmap(const char *op, struct file *file, unsigned long prot,
562b5e95b48SJohn Johansen 		       unsigned long flags, bool in_atomic)
563b5e95b48SJohn Johansen {
564637f688dSJohn Johansen 	int mask = 0;
565b5e95b48SJohn Johansen 
566b5e95b48SJohn Johansen 	if (!file || !file_ctx(file))
567b5e95b48SJohn Johansen 		return 0;
568b5e95b48SJohn Johansen 
569b5e95b48SJohn Johansen 	if (prot & PROT_READ)
570b5e95b48SJohn Johansen 		mask |= MAY_READ;
571b5e95b48SJohn Johansen 	/*
572b5e95b48SJohn Johansen 	 * Private mappings don't require write perms since they don't
573b5e95b48SJohn Johansen 	 * write back to the files
574b5e95b48SJohn Johansen 	 */
575b5e95b48SJohn Johansen 	if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
576b5e95b48SJohn Johansen 		mask |= MAY_WRITE;
577b5e95b48SJohn Johansen 	if (prot & PROT_EXEC)
578341c1fdaSJohn Johansen 		mask |= AA_EXEC_MMAP;
579b5e95b48SJohn Johansen 
580b5e95b48SJohn Johansen 	return common_file_perm(op, file, mask, in_atomic);
581e5467859SAl Viro }
582e5467859SAl Viro 
apparmor_mmap_file(struct file * file,unsigned long reqprot,unsigned long prot,unsigned long flags)583b5e95b48SJohn Johansen static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
584341c1fdaSJohn Johansen 			      unsigned long prot, unsigned long flags)
585b5e95b48SJohn Johansen {
586b5e95b48SJohn Johansen 	return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
587b5e95b48SJohn Johansen }
588b5e95b48SJohn Johansen 
apparmor_file_mprotect(struct vm_area_struct * vma,unsigned long reqprot,unsigned long prot)589b5e95b48SJohn Johansen static int apparmor_file_mprotect(struct vm_area_struct *vma,
590b5e95b48SJohn Johansen 				  unsigned long reqprot, unsigned long prot)
591341c1fdaSJohn Johansen {
592341c1fdaSJohn Johansen 	return common_mmap(OP_FMPROT, vma->vm_file, prot,
593b5e95b48SJohn Johansen 			   !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
594b5e95b48SJohn Johansen 			   false);
595c4371d90SGeorgia Garcia }
596c4371d90SGeorgia Garcia 
597c4371d90SGeorgia Garcia #ifdef CONFIG_IO_URING
audit_uring_mask(u32 mask)598c4371d90SGeorgia Garcia static const char *audit_uring_mask(u32 mask)
599c4371d90SGeorgia Garcia {
600c4371d90SGeorgia Garcia 	if (mask & AA_MAY_CREATE_SQPOLL)
601c4371d90SGeorgia Garcia 		return "sqpoll";
602c4371d90SGeorgia Garcia 	if (mask & AA_MAY_OVERRIDE_CRED)
603c4371d90SGeorgia Garcia 		return "override_creds";
604c4371d90SGeorgia Garcia 	return "";
605c4371d90SGeorgia Garcia }
606c4371d90SGeorgia Garcia 
audit_uring_cb(struct audit_buffer * ab,void * va)607c4371d90SGeorgia Garcia static void audit_uring_cb(struct audit_buffer *ab, void *va)
608c4371d90SGeorgia Garcia {
609c4371d90SGeorgia Garcia 	struct apparmor_audit_data *ad = aad_of_va(va);
610c4371d90SGeorgia Garcia 
611c4371d90SGeorgia Garcia 	if (ad->request & AA_URING_PERM_MASK) {
612c4371d90SGeorgia Garcia 		audit_log_format(ab, " requested=\"%s\"",
613c4371d90SGeorgia Garcia 				 audit_uring_mask(ad->request));
614c4371d90SGeorgia Garcia 		if (ad->denied & AA_URING_PERM_MASK) {
615c4371d90SGeorgia Garcia 			audit_log_format(ab, " denied=\"%s\"",
616c4371d90SGeorgia Garcia 					 audit_uring_mask(ad->denied));
617c4371d90SGeorgia Garcia 		}
618c4371d90SGeorgia Garcia 	}
619c4371d90SGeorgia Garcia 	if (ad->uring.target) {
620c4371d90SGeorgia Garcia 		audit_log_format(ab, " tcontext=");
621c4371d90SGeorgia Garcia 		aa_label_xaudit(ab, labels_ns(ad->subj_label),
622c4371d90SGeorgia Garcia 				ad->uring.target,
623c4371d90SGeorgia Garcia 				FLAGS_NONE, GFP_ATOMIC);
624c4371d90SGeorgia Garcia 	}
625c4371d90SGeorgia Garcia }
626c4371d90SGeorgia Garcia 
profile_uring(struct aa_profile * profile,u32 request,struct aa_label * new,int cap,struct apparmor_audit_data * ad)627c4371d90SGeorgia Garcia static int profile_uring(struct aa_profile *profile, u32 request,
628c4371d90SGeorgia Garcia 			 struct aa_label *new, int cap,
629c4371d90SGeorgia Garcia 			 struct apparmor_audit_data *ad)
630c4371d90SGeorgia Garcia {
631c4371d90SGeorgia Garcia 	unsigned int state;
632c4371d90SGeorgia Garcia 	struct aa_ruleset *rules;
633c4371d90SGeorgia Garcia 	int error = 0;
634c4371d90SGeorgia Garcia 
635c4371d90SGeorgia Garcia 	AA_BUG(!profile);
636c4371d90SGeorgia Garcia 
637c4371d90SGeorgia Garcia 	rules = profile->label.rules[0];
638c4371d90SGeorgia Garcia 	state = RULE_MEDIATES(rules, AA_CLASS_IO_URING);
639c4371d90SGeorgia Garcia 	if (state) {
640c4371d90SGeorgia Garcia 		struct aa_perms perms = { };
641c4371d90SGeorgia Garcia 
642c4371d90SGeorgia Garcia 		if (new) {
643c4371d90SGeorgia Garcia 			aa_label_match(profile, rules, new, state,
644c4371d90SGeorgia Garcia 				       false, request, &perms);
645c4371d90SGeorgia Garcia 		} else {
646c4371d90SGeorgia Garcia 			perms = *aa_lookup_perms(rules->policy, state);
647c4371d90SGeorgia Garcia 		}
648c4371d90SGeorgia Garcia 		aa_apply_modes_to_perms(profile, &perms);
649c4371d90SGeorgia Garcia 		error = aa_check_perms(profile, &perms, request, ad,
650c4371d90SGeorgia Garcia 				       audit_uring_cb);
651c4371d90SGeorgia Garcia 	}
652c4371d90SGeorgia Garcia 
653c4371d90SGeorgia Garcia 	return error;
654c4371d90SGeorgia Garcia }
655c4371d90SGeorgia Garcia 
656c4371d90SGeorgia Garcia /**
657c4371d90SGeorgia Garcia  * apparmor_uring_override_creds - check the requested cred override
658c4371d90SGeorgia Garcia  * @new: the target creds
659c4371d90SGeorgia Garcia  *
660c4371d90SGeorgia Garcia  * Check to see if the current task is allowed to override it's credentials
6617060d3ccSArnd Bergmann  * to service an io_uring operation.
662c4371d90SGeorgia Garcia  */
apparmor_uring_override_creds(const struct cred * new)663c4371d90SGeorgia Garcia static int apparmor_uring_override_creds(const struct cred *new)
664c4371d90SGeorgia Garcia {
665c4371d90SGeorgia Garcia 	struct aa_profile *profile;
666c4371d90SGeorgia Garcia 	struct aa_label *label;
667c4371d90SGeorgia Garcia 	int error;
668c4371d90SGeorgia Garcia 	bool needput;
669c4371d90SGeorgia Garcia 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
670c4371d90SGeorgia Garcia 			  OP_URING_OVERRIDE);
671c4371d90SGeorgia Garcia 
672c4371d90SGeorgia Garcia 	ad.uring.target = cred_label(new);
673c4371d90SGeorgia Garcia 	label = __begin_current_label_crit_section(&needput);
674c4371d90SGeorgia Garcia 	error = fn_for_each(label, profile,
675c4371d90SGeorgia Garcia 			profile_uring(profile, AA_MAY_OVERRIDE_CRED,
676c4371d90SGeorgia Garcia 				      cred_label(new), CAP_SYS_ADMIN, &ad));
677c4371d90SGeorgia Garcia 	__end_current_label_crit_section(label, needput);
678c4371d90SGeorgia Garcia 
679c4371d90SGeorgia Garcia 	return error;
680c4371d90SGeorgia Garcia }
681c4371d90SGeorgia Garcia 
682c4371d90SGeorgia Garcia /**
683c4371d90SGeorgia Garcia  * apparmor_uring_sqpoll - check if a io_uring polling thread can be created
684c4371d90SGeorgia Garcia  *
6857060d3ccSArnd Bergmann  * Check to see if the current task is allowed to create a new io_uring
686c4371d90SGeorgia Garcia  * kernel polling thread.
687c4371d90SGeorgia Garcia  */
apparmor_uring_sqpoll(void)688c4371d90SGeorgia Garcia static int apparmor_uring_sqpoll(void)
689c4371d90SGeorgia Garcia {
690c4371d90SGeorgia Garcia 	struct aa_profile *profile;
691c4371d90SGeorgia Garcia 	struct aa_label *label;
692c4371d90SGeorgia Garcia 	int error;
693c4371d90SGeorgia Garcia 	bool needput;
694c4371d90SGeorgia Garcia 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
695c4371d90SGeorgia Garcia 			  OP_URING_SQPOLL);
696c4371d90SGeorgia Garcia 
697c4371d90SGeorgia Garcia 	label = __begin_current_label_crit_section(&needput);
698c4371d90SGeorgia Garcia 	error = fn_for_each(label, profile,
699c4371d90SGeorgia Garcia 			profile_uring(profile, AA_MAY_CREATE_SQPOLL,
700c4371d90SGeorgia Garcia 				      NULL, CAP_SYS_ADMIN, &ad));
701c4371d90SGeorgia Garcia 	__end_current_label_crit_section(label, needput);
702c4371d90SGeorgia Garcia 
7032ea3ffb7SJohn Johansen 	return error;
7042ea3ffb7SJohn Johansen }
7052ea3ffb7SJohn Johansen #endif /* CONFIG_IO_URING */
7062ea3ffb7SJohn Johansen 
apparmor_sb_mount(const char * dev_name,const struct path * path,const char * type,unsigned long flags,void * data)7072ea3ffb7SJohn Johansen static int apparmor_sb_mount(const char *dev_name, const struct path *path,
7082ea3ffb7SJohn Johansen 			     const char *type, unsigned long flags, void *data)
7092ea3ffb7SJohn Johansen {
7102ea3ffb7SJohn Johansen 	struct aa_label *label;
7112ea3ffb7SJohn Johansen 	int error = 0;
7122ea3ffb7SJohn Johansen 	bool needput;
7132ea3ffb7SJohn Johansen 
7142ea3ffb7SJohn Johansen 	/* Discard magic */
7152ea3ffb7SJohn Johansen 	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
7162ea3ffb7SJohn Johansen 		flags &= ~MS_MGC_MSK;
7172ea3ffb7SJohn Johansen 
71890c436a6SJohn Johansen 	flags &= ~AA_MS_IGNORE_MASK;
71990c436a6SJohn Johansen 
7202ea3ffb7SJohn Johansen 	label = __begin_current_label_crit_section(&needput);
72190c436a6SJohn Johansen 	if (!unconfined(label)) {
72290c436a6SJohn Johansen 		if (flags & MS_REMOUNT)
7232ea3ffb7SJohn Johansen 			error = aa_remount(current_cred(), label, path, flags,
7242ea3ffb7SJohn Johansen 					   data);
72590c436a6SJohn Johansen 		else if (flags & MS_BIND)
72690c436a6SJohn Johansen 			error = aa_bind_mount(current_cred(), label, path,
7272ea3ffb7SJohn Johansen 					      dev_name, flags);
728157a3537SJohn Johansen 		else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
72990c436a6SJohn Johansen 				  MS_UNBINDABLE))
7302ea3ffb7SJohn Johansen 			error = aa_mount_change_type(current_cred(), label,
73190c436a6SJohn Johansen 						     path, flags);
73290c436a6SJohn Johansen 		else if (flags & MS_MOVE)
7332ea3ffb7SJohn Johansen 			error = aa_move_mount_old(current_cred(), label, path,
7342ea3ffb7SJohn Johansen 						  dev_name);
7352ea3ffb7SJohn Johansen 		else
7362ea3ffb7SJohn Johansen 			error = aa_new_mount(current_cred(), label, dev_name,
7372ea3ffb7SJohn Johansen 					     path, type, flags, data);
7382ea3ffb7SJohn Johansen 	}
739157a3537SJohn Johansen 	__end_current_label_crit_section(label, needput);
740157a3537SJohn Johansen 
741157a3537SJohn Johansen 	return error;
742157a3537SJohn Johansen }
743157a3537SJohn Johansen 
apparmor_move_mount(const struct path * from_path,const struct path * to_path)744157a3537SJohn Johansen static int apparmor_move_mount(const struct path *from_path,
745157a3537SJohn Johansen 			       const struct path *to_path)
746157a3537SJohn Johansen {
747157a3537SJohn Johansen 	struct aa_label *label;
748157a3537SJohn Johansen 	int error = 0;
749157a3537SJohn Johansen 	bool needput;
750157a3537SJohn Johansen 
751157a3537SJohn Johansen 	label = __begin_current_label_crit_section(&needput);
752157a3537SJohn Johansen 	if (!unconfined(label))
753157a3537SJohn Johansen 		error = aa_move_mount(current_cred(), label, from_path,
7542ea3ffb7SJohn Johansen 				      to_path);
7552ea3ffb7SJohn Johansen 	__end_current_label_crit_section(label, needput);
7562ea3ffb7SJohn Johansen 
7572ea3ffb7SJohn Johansen 	return error;
7582ea3ffb7SJohn Johansen }
7592ea3ffb7SJohn Johansen 
apparmor_sb_umount(struct vfsmount * mnt,int flags)7602ea3ffb7SJohn Johansen static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
76190c436a6SJohn Johansen {
7622ea3ffb7SJohn Johansen 	struct aa_label *label;
7632ea3ffb7SJohn Johansen 	int error = 0;
7642ea3ffb7SJohn Johansen 	bool needput;
7652ea3ffb7SJohn Johansen 
7662ea3ffb7SJohn Johansen 	label = __begin_current_label_crit_section(&needput);
7672ea3ffb7SJohn Johansen 	if (!unconfined(label))
7682ea3ffb7SJohn Johansen 		error = aa_umount(current_cred(), label, mnt, flags);
7692ea3ffb7SJohn Johansen 	__end_current_label_crit_section(label, needput);
7702ea3ffb7SJohn Johansen 
7712ea3ffb7SJohn Johansen 	return error;
7722ea3ffb7SJohn Johansen }
7732ea3ffb7SJohn Johansen 
apparmor_sb_pivotroot(const struct path * old_path,const struct path * new_path)7742ea3ffb7SJohn Johansen static int apparmor_sb_pivotroot(const struct path *old_path,
77590c436a6SJohn Johansen 				 const struct path *new_path)
7762ea3ffb7SJohn Johansen {
7772ea3ffb7SJohn Johansen 	struct aa_label *label;
7782ea3ffb7SJohn Johansen 	int error = 0;
7792ea3ffb7SJohn Johansen 
7802ea3ffb7SJohn Johansen 	label = aa_get_current_label();
781223981dbSCasey Schaufler 	if (!unconfined(label))
782223981dbSCasey Schaufler 		error = aa_pivotroot(current_cred(), label, old_path, new_path);
783223981dbSCasey Schaufler 	aa_put_label(label);
784223981dbSCasey Schaufler 
785223981dbSCasey Schaufler 	return error;
786223981dbSCasey Schaufler }
787*6d2fb472SMickaël Salaün 
apparmor_getselfattr(unsigned int attr,struct lsm_ctx __user * lx,u32 * size,u32 flags)788223981dbSCasey Schaufler static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
789223981dbSCasey Schaufler 				u32 *size, u32 flags)
790223981dbSCasey Schaufler {
791223981dbSCasey Schaufler 	int error = -ENOENT;
792223981dbSCasey Schaufler 	struct aa_task_ctx *ctx = task_ctx(current);
793223981dbSCasey Schaufler 	struct aa_label *label = NULL;
794223981dbSCasey Schaufler 	char *value = NULL;
795223981dbSCasey Schaufler 
796223981dbSCasey Schaufler 	switch (attr) {
797223981dbSCasey Schaufler 	case LSM_ATTR_CURRENT:
798223981dbSCasey Schaufler 		label = aa_get_newest_label(cred_label(current_cred()));
799223981dbSCasey Schaufler 		break;
800223981dbSCasey Schaufler 	case LSM_ATTR_PREV:
801223981dbSCasey Schaufler 		if (ctx->previous)
802223981dbSCasey Schaufler 			label = aa_get_newest_label(ctx->previous);
803223981dbSCasey Schaufler 		break;
804223981dbSCasey Schaufler 	case LSM_ATTR_EXEC:
805223981dbSCasey Schaufler 		if (ctx->onexec)
806223981dbSCasey Schaufler 			label = aa_get_newest_label(ctx->onexec);
807223981dbSCasey Schaufler 		break;
808d7cf3412SPaul Moore 	default:
809d7cf3412SPaul Moore 		error = -EOPNOTSUPP;
810223981dbSCasey Schaufler 		break;
811223981dbSCasey Schaufler 	}
812223981dbSCasey Schaufler 
813223981dbSCasey Schaufler 	if (label) {
814223981dbSCasey Schaufler 		error = aa_getprocattr(label, &value, false);
815223981dbSCasey Schaufler 		if (error > 0)
816223981dbSCasey Schaufler 			error = lsm_fill_user_ctx(lx, size, value, error,
817223981dbSCasey Schaufler 						  LSM_ID_APPARMOR, 0);
818223981dbSCasey Schaufler 		kfree(value);
819223981dbSCasey Schaufler 	}
820223981dbSCasey Schaufler 
821c8e477c6SAl Viro 	aa_put_label(label);
822b5e95b48SJohn Johansen 
823b5e95b48SJohn Johansen 	if (error < 0)
824b5e95b48SJohn Johansen 		return error;
825b5e95b48SJohn Johansen 	return 1;
826b5e95b48SJohn Johansen }
827de62de59SJohn Johansen 
apparmor_getprocattr(struct task_struct * task,const char * name,char ** value)828637f688dSJohn Johansen static int apparmor_getprocattr(struct task_struct *task, const char *name,
829b5e95b48SJohn Johansen 				char **value)
830b5e95b48SJohn Johansen {
831d9087c49SJohn Johansen 	int error = -ENOENT;
83255a26ebfSJohn Johansen 	/* released below */
833637f688dSJohn Johansen 	const struct cred *cred = get_task_cred(task);
83455a26ebfSJohn Johansen 	struct aa_task_ctx *ctx = task_ctx(current);
835637f688dSJohn Johansen 	struct aa_label *label = NULL;
836b5e95b48SJohn Johansen 
837b5e95b48SJohn Johansen 	if (strcmp(name, "current") == 0)
838b5e95b48SJohn Johansen 		label = aa_get_newest_label(cred_label(cred));
839637f688dSJohn Johansen 	else if (strcmp(name, "prev") == 0  && ctx->previous)
840223981dbSCasey Schaufler 		label = aa_get_newest_label(ctx->previous);
84177b071b3SJohn Johansen 	else if (strcmp(name, "exec") == 0 && ctx->onexec)
842637f688dSJohn Johansen 		label = aa_get_newest_label(ctx->onexec);
843b5e95b48SJohn Johansen 	else
844b5e95b48SJohn Johansen 		error = -EINVAL;
845b5e95b48SJohn Johansen 
846b5e95b48SJohn Johansen 	if (label)
847b5e95b48SJohn Johansen 		error = aa_getprocattr(label, value, true);
848223981dbSCasey Schaufler 
849b5e95b48SJohn Johansen 	aa_put_label(label);
850e89b8081SVegard Nossum 	put_cred(cred);
851b5e95b48SJohn Johansen 
852b5e95b48SJohn Johansen 	return error;
853bd7bd201SJohn Johansen }
8548c4b785aSJohn Johansen 
do_setattr(u64 attr,void * value,size_t size)855b5e95b48SJohn Johansen static int do_setattr(u64 attr, void *value, size_t size)
856b5e95b48SJohn Johansen {
857b5e95b48SJohn Johansen 	char *command, *largs = NULL, *args = value;
858b5e95b48SJohn Johansen 	size_t arg_size;
859e89b8081SVegard Nossum 	int error;
860e89b8081SVegard Nossum 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
861e89b8081SVegard Nossum 			  OP_SETPROCATTR);
862e89b8081SVegard Nossum 
863e89b8081SVegard Nossum 	if (size == 0)
864e89b8081SVegard Nossum 		return -EINVAL;
865e89b8081SVegard Nossum 
866e89b8081SVegard Nossum 	/* AppArmor requires that the buffer must be null terminated atm */
867e89b8081SVegard Nossum 	if (args[size - 1] != '\0') {
868e89b8081SVegard Nossum 		/* null terminate */
869e89b8081SVegard Nossum 		largs = args = kmalloc(size + 1, GFP_KERNEL);
870b5e95b48SJohn Johansen 		if (!args)
871b5e95b48SJohn Johansen 			return -ENOMEM;
872b5e95b48SJohn Johansen 		memcpy(args, value, size);
873e89b8081SVegard Nossum 		args[size] = '\0';
874b5e95b48SJohn Johansen 	}
875b5e95b48SJohn Johansen 
876e89b8081SVegard Nossum 	error = -EINVAL;
877b5e95b48SJohn Johansen 	args = strim(args);
878d4d03f74SJohn Johansen 	command = strsep(&args, " ");
879223981dbSCasey Schaufler 	if (!args)
880b5e95b48SJohn Johansen 		goto out;
881b5e95b48SJohn Johansen 	args = skip_spaces(args);
882df8073c6SJohn Johansen 	if (!*args)
883b5e95b48SJohn Johansen 		goto out;
884b5e95b48SJohn Johansen 
885df8073c6SJohn Johansen 	arg_size = size - (args - (largs ? largs : (char *) value));
886b5e95b48SJohn Johansen 	if (attr == LSM_ATTR_CURRENT) {
887df8073c6SJohn Johansen 		if (strcmp(command, "changehat") == 0) {
888b5e95b48SJohn Johansen 			error = aa_setprocattr_changehat(args, arg_size,
889df8073c6SJohn Johansen 							 AA_CHANGE_NOFLAGS);
8906c5fc8f1SJohn Johansen 		} else if (strcmp(command, "permhat") == 0) {
8916c5fc8f1SJohn Johansen 			error = aa_setprocattr_changehat(args, arg_size,
8923eea57c2SJohn Johansen 							 AA_CHANGE_TEST);
8933eea57c2SJohn Johansen 		} else if (strcmp(command, "changeprofile") == 0) {
894223981dbSCasey Schaufler 			error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
8953eea57c2SJohn Johansen 		} else if (strcmp(command, "permprofile") == 0) {
896df8073c6SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_TEST);
8976c5fc8f1SJohn Johansen 		} else if (strcmp(command, "stack") == 0) {
8986c5fc8f1SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_STACK);
8996c5fc8f1SJohn Johansen 		} else
9003eea57c2SJohn Johansen 			goto fail;
9013eea57c2SJohn Johansen 	} else if (attr == LSM_ATTR_EXEC) {
9023eea57c2SJohn Johansen 		if (strcmp(command, "exec") == 0)
903b5e95b48SJohn Johansen 			error = aa_change_profile(args, AA_CHANGE_ONEXEC);
904e89b8081SVegard Nossum 		else if (strcmp(command, "stack") == 0)
9053eea57c2SJohn Johansen 			error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
906b5e95b48SJohn Johansen 							 AA_CHANGE_STACK));
907b5e95b48SJohn Johansen 		else
908e89b8081SVegard Nossum 			goto fail;
909e89b8081SVegard Nossum 	} else
910b5e95b48SJohn Johansen 		/* only support the "current" and "exec" process attributes */
9113eea57c2SJohn Johansen 		goto fail;
9123eea57c2SJohn Johansen 
913d20f5a1aSJohn Johansen 	if (!error)
914223981dbSCasey Schaufler 		error = size;
915223981dbSCasey Schaufler out:
916223981dbSCasey Schaufler 	kfree(largs);
917223981dbSCasey Schaufler 	return error;
918223981dbSCasey Schaufler 
919223981dbSCasey Schaufler fail:
920bd7bd201SJohn Johansen 	ad.subj_label = begin_current_label_crit_section();
921bd7bd201SJohn Johansen 	if (attr == LSM_ATTR_CURRENT)
922d20f5a1aSJohn Johansen 		ad.info = "current";
923e89b8081SVegard Nossum 	else if (attr == LSM_ATTR_EXEC)
924b5e95b48SJohn Johansen 		ad.info = "exec";
925b5e95b48SJohn Johansen 	else
926223981dbSCasey Schaufler 		ad.info = "invalid";
927223981dbSCasey Schaufler 	ad.error = error = -EINVAL;
928223981dbSCasey Schaufler 	aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL);
929223981dbSCasey Schaufler 	end_current_label_crit_section(ad.subj_label);
930223981dbSCasey Schaufler 	goto out;
931223981dbSCasey Schaufler }
932223981dbSCasey Schaufler 
apparmor_setselfattr(unsigned int attr,struct lsm_ctx * ctx,u32 size,u32 flags)933223981dbSCasey Schaufler static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
934223981dbSCasey Schaufler 				u32 size, u32 flags)
935223981dbSCasey Schaufler {
936223981dbSCasey Schaufler 	int rc;
937223981dbSCasey Schaufler 
938223981dbSCasey Schaufler 	if (attr != LSM_ATTR_CURRENT && attr != LSM_ATTR_EXEC)
939223981dbSCasey Schaufler 		return -EOPNOTSUPP;
940223981dbSCasey Schaufler 
941223981dbSCasey Schaufler 	rc = do_setattr(attr, ctx->ctx, ctx->ctx_len);
942223981dbSCasey Schaufler 	if (rc > 0)
943223981dbSCasey Schaufler 		return 0;
944223981dbSCasey Schaufler 	return rc;
945223981dbSCasey Schaufler }
946223981dbSCasey Schaufler 
apparmor_setprocattr(const char * name,void * value,size_t size)947223981dbSCasey Schaufler static int apparmor_setprocattr(const char *name, void *value,
948223981dbSCasey Schaufler 				size_t size)
949223981dbSCasey Schaufler {
950fe864821SJohn Johansen 	int attr = lsm_name_to_attr(name);
951fe864821SJohn Johansen 
952fe864821SJohn Johansen 	if (attr)
953fe864821SJohn Johansen 		return do_setattr(attr, value, size);
95464fc9526SKhadija Kamran 	return -EINVAL;
955fe864821SJohn Johansen }
956637f688dSJohn Johansen 
957d9087c49SJohn Johansen /**
958fe864821SJohn Johansen  * apparmor_bprm_committing_creds - do task cleanup on committing new creds
959fe864821SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
960d9087c49SJohn Johansen  */
apparmor_bprm_committing_creds(const struct linux_binprm * bprm)961d9087c49SJohn Johansen static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
962fe864821SJohn Johansen {
963fe864821SJohn Johansen 	struct aa_label *label = aa_current_raw_label();
964192ca6b5SJohn Johansen 	struct aa_label *new_label = cred_label(bprm->cred);
965192ca6b5SJohn Johansen 
966fe864821SJohn Johansen 	/* bail out if unconfined or not changing profile */
967fe864821SJohn Johansen 	if ((new_label->proxy == label->proxy) ||
968637f688dSJohn Johansen 	    (unconfined(new_label)))
969d9087c49SJohn Johansen 		return;
970fe864821SJohn Johansen 
971fe864821SJohn Johansen 	aa_inherit_files(bprm->cred, current->files);
972fe864821SJohn Johansen 
973391f1211SJiapeng Chong 	current->pdeath_signal = 0;
974fe864821SJohn Johansen 
975fe864821SJohn Johansen 	/* reset soft limits and set hard limits for the new label */
976a721f7b8SKhadija Kamran 	__aa_transition_rlimits(label, new_label);
977fe864821SJohn Johansen }
9783b529a76SJohn Johansen 
979de62de59SJohn Johansen /**
9803b529a76SJohn Johansen  * apparmor_bprm_committed_creds() - do cleanup after new creds committed
981fe864821SJohn Johansen  * @bprm: binprm for the exec  (NOT NULL)
982fe864821SJohn Johansen  */
apparmor_bprm_committed_creds(const struct linux_binprm * bprm)983fe864821SJohn Johansen static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm)
9846326948fSPaul Moore {
9856326948fSPaul Moore 	/* clear out temporary/transitional state from the context */
9862516fde1SVinicius Costa Gomes 	aa_clear_task_ctx_trans(task_ctx(current));
9876326948fSPaul Moore 
9882516fde1SVinicius Costa Gomes 	return;
9896326948fSPaul Moore }
9906326948fSPaul Moore 
apparmor_current_getlsmprop_subj(struct lsm_prop * prop)9916326948fSPaul Moore static void apparmor_current_getlsmprop_subj(struct lsm_prop *prop)
992a7ae3645SJohn Johansen {
993a7ae3645SJohn Johansen 	struct aa_label *label;
994a7ae3645SJohn Johansen 	bool needput;
995a7ae3645SJohn Johansen 
996a7ae3645SJohn Johansen 	label = __begin_current_label_crit_section(&needput);
997a7ae3645SJohn Johansen 	prop->apparmor.label = label;
9987cb4dc9fSJiri Slaby 	__end_current_label_crit_section(label, needput);
9997cb4dc9fSJiri Slaby }
1000b5e95b48SJohn Johansen 
apparmor_task_getlsmprop_obj(struct task_struct * p,struct lsm_prop * prop)1001637f688dSJohn Johansen static void apparmor_task_getlsmprop_obj(struct task_struct *p,
1002b5e95b48SJohn Johansen 					  struct lsm_prop *prop)
1003b5e95b48SJohn Johansen {
1004637f688dSJohn Johansen 	struct aa_label *label = aa_get_task_label(p);
100590c436a6SJohn Johansen 
100690c436a6SJohn Johansen 	prop->apparmor.label = label;
1007637f688dSJohn Johansen 	aa_put_label(label);
1008b5e95b48SJohn Johansen }
1009b5e95b48SJohn Johansen 
apparmor_task_setrlimit(struct task_struct * task,unsigned int resource,struct rlimit * new_rlim)1010b5e95b48SJohn Johansen static int apparmor_task_setrlimit(struct task_struct *task,
1011b5e95b48SJohn Johansen 		unsigned int resource, struct rlimit *new_rlim)
1012ae7795bcSEric W. Biederman {
10136b4f3d01SStephen Smalley 	struct aa_label *label;
1014cd1dbf76SJohn Johansen 	int error = 0;
101590c436a6SJohn Johansen 	bool needput;
1016cd1dbf76SJohn Johansen 
1017cd1dbf76SJohn Johansen 	label = __begin_current_label_crit_section(&needput);
1018cd1dbf76SJohn Johansen 
101990c436a6SJohn Johansen 	if (!unconfined(label))
102090c436a6SJohn Johansen 		error = aa_task_setrlimit(current_cred(), label, task,
10216b4f3d01SStephen Smalley 					  resource, new_rlim);
10226b4f3d01SStephen Smalley 	__end_current_label_crit_section(label, needput);
1023cd1dbf76SJohn Johansen 
1024cd1dbf76SJohn Johansen 	return error;
10256b4f3d01SStephen Smalley }
102690c436a6SJohn Johansen 
apparmor_task_kill(struct task_struct * target,struct kernel_siginfo * info,int sig,const struct cred * cred)10276b4f3d01SStephen Smalley static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
102890c436a6SJohn Johansen 			      int sig, const struct cred *cred)
1029cd1dbf76SJohn Johansen {
103090c436a6SJohn Johansen 	const struct cred *tc;
1031cd1dbf76SJohn Johansen 	struct aa_label *cl, *tl;
103290c436a6SJohn Johansen 	int error;
1033cd1dbf76SJohn Johansen 	bool needput;
103490c436a6SJohn Johansen 
1035cd1dbf76SJohn Johansen 	tc = get_task_cred(target);
1036cd1dbf76SJohn Johansen 	tl = aa_get_newest_cred_label(tc);
1037cd1dbf76SJohn Johansen 	if (cred) {
1038cd1dbf76SJohn Johansen 		/*
1039fa9b63adSJohn Johansen 		 * Dealing with USB IO specific behavior
1040fa9b63adSJohn Johansen 		 */
1041fa9b63adSJohn Johansen 		cl = aa_get_newest_cred_label(cred);
1042fa9b63adSJohn Johansen 		error = aa_may_signal(cred, cl, tc, tl, sig);
1043fa9b63adSJohn Johansen 		aa_put_label(cl);
1044fa9b63adSJohn Johansen 	} else {
1045fa9b63adSJohn Johansen 		cl = __begin_current_label_crit_section(&needput);
1046fa9b63adSJohn Johansen 		error = aa_may_signal(current_cred(), cl, tc, tl, sig);
1047fa9b63adSJohn Johansen 		__end_current_label_crit_section(cl, needput);
1048fa9b63adSJohn Johansen 	}
1049fa9b63adSJohn Johansen 	aa_put_label(tl);
1050fa9b63adSJohn Johansen 	put_cred(tc);
1051fa9b63adSJohn Johansen 
1052fa9b63adSJohn Johansen 	return error;
1053fa9b63adSJohn Johansen }
1054fa9b63adSJohn Johansen 
apparmor_userns_create(const struct cred * cred)1055fa9b63adSJohn Johansen static int apparmor_userns_create(const struct cred *cred)
1056cd1dbf76SJohn Johansen {
1057cd1dbf76SJohn Johansen 	struct aa_label *label;
1058cd1dbf76SJohn Johansen 	struct aa_profile *profile;
1059cd1dbf76SJohn Johansen 	int error = 0;
106056974a6fSJohn Johansen 	DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS,
106156974a6fSJohn Johansen 			  OP_USERNS_CREATE);
106256974a6fSJohn Johansen 
106356974a6fSJohn Johansen 	ad.subj_cred = current_cred();
106456974a6fSJohn Johansen 
106556974a6fSJohn Johansen 	label = begin_current_label_crit_section();
106656974a6fSJohn Johansen 	if (!unconfined(label)) {
106756974a6fSJohn Johansen 		error = fn_for_each(label, profile,
106879ddd4a7SJohn Johansen 				    aa_profile_ns_perm(profile, &ad,
106956974a6fSJohn Johansen 						       AA_USERNS_CREATE));
107056974a6fSJohn Johansen 	}
107156974a6fSJohn Johansen 	end_current_label_crit_section(label);
107256974a6fSJohn Johansen 
107356974a6fSJohn Johansen 	return error;
107456974a6fSJohn Johansen }
107579ddd4a7SJohn Johansen 
apparmor_sk_alloc_security(struct sock * sk,int family,gfp_t gfp)107656974a6fSJohn Johansen static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t gfp)
107779ddd4a7SJohn Johansen {
107856974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = aa_sock(sk);
107956974a6fSJohn Johansen 	struct aa_label *label;
108056974a6fSJohn Johansen 	bool needput;
108156974a6fSJohn Johansen 
108256974a6fSJohn Johansen 	label = __begin_current_label_crit_section(&needput);
108356974a6fSJohn Johansen 	//spin_lock_init(&ctx->lock);
10840fc6ab40SYang Li 	rcu_assign_pointer(ctx->label, aa_get_label(label));
10851cba2750SJohn Johansen 	rcu_assign_pointer(ctx->peer, NULL);
10861cba2750SJohn Johansen 	rcu_assign_pointer(ctx->peer_lastupdate, NULL);
108756974a6fSJohn Johansen 	__end_current_label_crit_section(label, needput);
108856974a6fSJohn Johansen 	return 0;
108956974a6fSJohn Johansen }
109056974a6fSJohn Johansen 
apparmor_sk_free_security(struct sock * sk)109179ddd4a7SJohn Johansen static void apparmor_sk_free_security(struct sock *sk)
109279ddd4a7SJohn Johansen {
109356974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = aa_sock(sk);
10943b646abcSMauricio Faria de Oliveira 
10953b646abcSMauricio Faria de Oliveira 	/* dead these won't be updated any more */
109656974a6fSJohn Johansen 	aa_put_label(rcu_dereference_protected(ctx->label, true));
10973b646abcSMauricio Faria de Oliveira 	aa_put_label(rcu_dereference_protected(ctx->peer, true));
10983b646abcSMauricio Faria de Oliveira 	aa_put_label(rcu_dereference_protected(ctx->peer_lastupdate, true));
10993b646abcSMauricio Faria de Oliveira }
110056974a6fSJohn Johansen 
110156974a6fSJohn Johansen /**
110256974a6fSJohn Johansen  * apparmor_sk_clone_security - clone the sk_security field
110356974a6fSJohn Johansen  * @sk: sock to have security cloned
110456974a6fSJohn Johansen  * @newsk: sock getting clone
110556974a6fSJohn Johansen  */
apparmor_sk_clone_security(const struct sock * sk,struct sock * newsk)110656974a6fSJohn Johansen static void apparmor_sk_clone_security(const struct sock *sk,
110756974a6fSJohn Johansen 				       struct sock *newsk)
110856974a6fSJohn Johansen {
110956974a6fSJohn Johansen 	struct aa_sk_ctx *ctx = aa_sock(sk);
111056974a6fSJohn Johansen 	struct aa_sk_ctx *new = aa_sock(newsk);
111156974a6fSJohn Johansen 
111256974a6fSJohn Johansen 	/* not actually in use yet */
111356974a6fSJohn Johansen 	if (rcu_access_pointer(ctx->label) != rcu_access_pointer(new->label)) {
111490c436a6SJohn Johansen 		aa_put_label(rcu_dereference_protected(new->label, true));
111590c436a6SJohn Johansen 		rcu_assign_pointer(new->label, aa_get_label_rcu(&ctx->label));
111656974a6fSJohn Johansen 	}
111756974a6fSJohn Johansen 
111856974a6fSJohn Johansen 	if (rcu_access_pointer(ctx->peer) != rcu_access_pointer(new->peer)) {
111956974a6fSJohn Johansen 		aa_put_label(rcu_dereference_protected(new->peer, true));
112056974a6fSJohn Johansen 		rcu_assign_pointer(new->peer, aa_get_label_rcu(&ctx->peer));
112156974a6fSJohn Johansen 	}
112256974a6fSJohn Johansen 
112356974a6fSJohn Johansen 	if (rcu_access_pointer(ctx->peer_lastupdate) != rcu_access_pointer(new->peer_lastupdate)) {
11241cba2750SJohn Johansen 		aa_put_label(rcu_dereference_protected(new->peer_lastupdate, true));
11251cba2750SJohn Johansen 		rcu_assign_pointer(new->peer_lastupdate,
11261cba2750SJohn Johansen 				   aa_get_label_rcu(&ctx->peer_lastupdate));
11271cba2750SJohn Johansen 	}
11281cba2750SJohn Johansen }
112956974a6fSJohn Johansen 
unix_connect_perm(const struct cred * cred,struct aa_label * label,struct sock * sk,struct sock * peer_sk)113056974a6fSJohn Johansen static int unix_connect_perm(const struct cred *cred, struct aa_label *label,
11311cba2750SJohn Johansen 			     struct sock *sk, struct sock *peer_sk)
113256974a6fSJohn Johansen {
113356974a6fSJohn Johansen 	struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk);
113456974a6fSJohn Johansen 	int error;
113556974a6fSJohn Johansen 
113656974a6fSJohn Johansen 	error = aa_unix_peer_perm(cred, label, OP_CONNECT,
113756974a6fSJohn Johansen 				(AA_MAY_CONNECT | AA_MAY_SEND | AA_MAY_RECEIVE),
113856974a6fSJohn Johansen 				  sk, peer_sk,
113956974a6fSJohn Johansen 				  rcu_dereference_protected(peer_ctx->label,
114056974a6fSJohn Johansen 				     lockdep_is_held(&unix_sk(peer_sk)->lock)));
114156974a6fSJohn Johansen 	if (!is_unix_fs(peer_sk)) {
114295c0581fSJohn Johansen 		last_error(error,
114356974a6fSJohn Johansen 			   aa_unix_peer_perm(cred,
114456974a6fSJohn Johansen 				rcu_dereference_protected(peer_ctx->label,
114556974a6fSJohn Johansen 				     lockdep_is_held(&unix_sk(peer_sk)->lock)),
114656974a6fSJohn Johansen 				OP_CONNECT,
114779ddd4a7SJohn Johansen 				(AA_MAY_ACCEPT | AA_MAY_SEND | AA_MAY_RECEIVE),
114856974a6fSJohn Johansen 							  peer_sk, sk, label));
114956974a6fSJohn Johansen 	}
115056974a6fSJohn Johansen 
115156974a6fSJohn Johansen 	return error;
115256974a6fSJohn Johansen }
115356974a6fSJohn Johansen 
115456974a6fSJohn Johansen /* lockdep check in unix_connect_perm - push sks here to check */
unix_connect_peers(struct aa_sk_ctx * sk_ctx,struct aa_sk_ctx * peer_ctx)115556974a6fSJohn Johansen static void unix_connect_peers(struct aa_sk_ctx *sk_ctx,
115656974a6fSJohn Johansen 			       struct aa_sk_ctx *peer_ctx)
115756974a6fSJohn Johansen {
115856974a6fSJohn Johansen 	/* Cross reference the peer labels for SO_PEERSEC */
115956974a6fSJohn Johansen 	struct aa_label *label = rcu_dereference_protected(sk_ctx->label, true);
116056974a6fSJohn Johansen 
116156974a6fSJohn Johansen 	aa_get_label(label);
116256974a6fSJohn Johansen 	aa_put_label(rcu_dereference_protected(peer_ctx->peer,
116356974a6fSJohn Johansen 					     true));
116456974a6fSJohn Johansen 	rcu_assign_pointer(peer_ctx->peer, label);	/* transfer cnt */
116556974a6fSJohn Johansen 
116656974a6fSJohn Johansen 	label = aa_get_label(rcu_dereference_protected(peer_ctx->label,
116756974a6fSJohn Johansen 					     true));
116856974a6fSJohn Johansen 	//spin_unlock(&peer_ctx->lock);
116956974a6fSJohn Johansen 
117056974a6fSJohn Johansen 	//spin_lock(&sk_ctx->lock);
117156974a6fSJohn Johansen 	aa_put_label(rcu_dereference_protected(sk_ctx->peer,
117256974a6fSJohn Johansen 					       true));
117356974a6fSJohn Johansen 	aa_put_label(rcu_dereference_protected(sk_ctx->peer_lastupdate,
117456974a6fSJohn Johansen 					       true));
117556974a6fSJohn Johansen 
117656974a6fSJohn Johansen 	rcu_assign_pointer(sk_ctx->peer, aa_get_label(label));
117756974a6fSJohn Johansen 	rcu_assign_pointer(sk_ctx->peer_lastupdate, label);     /* transfer cnt */
117856974a6fSJohn Johansen 	//spin_unlock(&sk_ctx->lock);
117956974a6fSJohn Johansen }
118056974a6fSJohn Johansen 
118156974a6fSJohn Johansen /**
118256974a6fSJohn Johansen  * apparmor_unix_stream_connect - check perms before making unix domain conn
118356974a6fSJohn Johansen  * @sk: sk attempting to connect
118456974a6fSJohn Johansen  * @peer_sk: sk that is accepting the connection
118556974a6fSJohn Johansen  * @newsk: new sk created for this connection
118656974a6fSJohn Johansen  * peer is locked when this hook is called
118756974a6fSJohn Johansen  *
118856974a6fSJohn Johansen  * Return:
118956974a6fSJohn Johansen  *   0 if connection is permitted
119056974a6fSJohn Johansen  *   error code on denial or failure
119156974a6fSJohn Johansen  */
apparmor_unix_stream_connect(struct sock * sk,struct sock * peer_sk,struct sock * newsk)119256974a6fSJohn Johansen static int apparmor_unix_stream_connect(struct sock *sk, struct sock *peer_sk,
119356974a6fSJohn Johansen 					struct sock *newsk)
11941cba2750SJohn Johansen {
119556974a6fSJohn Johansen 	struct aa_sk_ctx *sk_ctx = aa_sock(sk);
119656974a6fSJohn Johansen 	struct aa_sk_ctx *peer_ctx = aa_sock(peer_sk);
119756974a6fSJohn Johansen 	struct aa_sk_ctx *new_ctx = aa_sock(newsk);
119856974a6fSJohn Johansen 	struct aa_label *label;
119956974a6fSJohn Johansen 	int error;
120056974a6fSJohn Johansen 	bool needput;
120156974a6fSJohn Johansen 
120256974a6fSJohn Johansen 	label = __begin_current_label_crit_section(&needput);
120356974a6fSJohn Johansen 	error = unix_connect_perm(current_cred(), label, sk, peer_sk);
120456974a6fSJohn Johansen 	__end_current_label_crit_section(label, needput);
120556974a6fSJohn Johansen 
120656974a6fSJohn Johansen 	if (error)
120756974a6fSJohn Johansen 		return error;
120856974a6fSJohn Johansen 
120956974a6fSJohn Johansen 	/* newsk doesn't go through post_create, but does go through
121056974a6fSJohn Johansen 	 * security_sk_alloc()
121156974a6fSJohn Johansen 	 */
121256974a6fSJohn Johansen 	rcu_assign_pointer(new_ctx->label,
121356974a6fSJohn Johansen 			   aa_get_label(rcu_dereference_protected(peer_ctx->label,
121456974a6fSJohn Johansen 								  true)));
121556974a6fSJohn Johansen 
121656974a6fSJohn Johansen 	/* Cross reference the peer labels for SO_PEERSEC */
121756974a6fSJohn Johansen 	unix_connect_peers(sk_ctx, new_ctx);
121856974a6fSJohn Johansen 
121956974a6fSJohn Johansen 	return 0;
122056974a6fSJohn Johansen }
122156974a6fSJohn Johansen 
122256974a6fSJohn Johansen /**
122356974a6fSJohn Johansen  * apparmor_unix_may_send - check perms before conn or sending unix dgrams
122456974a6fSJohn Johansen  * @sock: socket sending the message
122556974a6fSJohn Johansen  * @peer: socket message is being send to
122656974a6fSJohn Johansen  *
122756974a6fSJohn Johansen  * Performs bidirectional permission checks for Unix domain socket communication:
122856974a6fSJohn Johansen  * 1. Verifies sender has AA_MAY_SEND to target socket
122956974a6fSJohn Johansen  * 2. Verifies receiver has AA_MAY_RECEIVE from source socket
123056974a6fSJohn Johansen  *
123156974a6fSJohn Johansen  * sock and peer are locked when this hook is called
123256974a6fSJohn Johansen  * called by: dgram_connect peer setup but path not copied to newsk
123356974a6fSJohn Johansen  *
123456974a6fSJohn Johansen  * Return:
123556974a6fSJohn Johansen  *   0 if transmission is permitted
123656974a6fSJohn Johansen  *   error code on denial or failure
123756974a6fSJohn Johansen  */
apparmor_unix_may_send(struct socket * sock,struct socket * peer)123856974a6fSJohn Johansen static int apparmor_unix_may_send(struct socket *sock, struct socket *peer)
123956974a6fSJohn Johansen {
124056974a6fSJohn Johansen 	struct aa_sk_ctx *peer_ctx = aa_sock(peer->sk);
124156974a6fSJohn Johansen 	struct aa_label *label;
124256974a6fSJohn Johansen 	int error;
124356974a6fSJohn Johansen 	bool needput;
124456974a6fSJohn Johansen 
124556974a6fSJohn Johansen 	label = __begin_current_label_crit_section(&needput);
124656974a6fSJohn Johansen 	error = xcheck(aa_unix_peer_perm(current_cred(),
124756974a6fSJohn Johansen 				label, OP_SENDMSG, AA_MAY_SEND,
124856974a6fSJohn Johansen 				sock->sk, peer->sk,
124956974a6fSJohn Johansen 				rcu_dereference_protected(peer_ctx->label,
125056974a6fSJohn Johansen 							  true)),
125156974a6fSJohn Johansen 		       aa_unix_peer_perm(peer->file ? peer->file->f_cred : NULL,
125256974a6fSJohn Johansen 				rcu_dereference_protected(peer_ctx->label,
125356974a6fSJohn Johansen 							  true),
125456974a6fSJohn Johansen 				OP_SENDMSG, AA_MAY_RECEIVE, peer->sk,
125556974a6fSJohn Johansen 				sock->sk, label));
125656974a6fSJohn Johansen 	__end_current_label_crit_section(label, needput);
125756974a6fSJohn Johansen 
125856974a6fSJohn Johansen 	return error;
125956974a6fSJohn Johansen }
126056974a6fSJohn Johansen 
apparmor_socket_create(int family,int type,int protocol,int kern)126156974a6fSJohn Johansen static int apparmor_socket_create(int family, int type, int protocol, int kern)
126256974a6fSJohn Johansen {
126356974a6fSJohn Johansen 	struct aa_label *label;
126456974a6fSJohn Johansen 	int error = 0;
126556974a6fSJohn Johansen 
126656974a6fSJohn Johansen 	AA_BUG(in_interrupt());
126756974a6fSJohn Johansen 
126856974a6fSJohn Johansen 	if (kern)
126956974a6fSJohn Johansen 		return 0;
127056974a6fSJohn Johansen 
127156974a6fSJohn Johansen 	label = begin_current_label_crit_section();
127256974a6fSJohn Johansen 	if (!unconfined(label)) {
127356974a6fSJohn Johansen 		if (family == PF_UNIX)
127456974a6fSJohn Johansen 			error = aa_unix_create_perm(label, family, type,
127556974a6fSJohn Johansen 						    protocol);
127656974a6fSJohn Johansen 		else
127756974a6fSJohn Johansen 			error = aa_af_perm(current_cred(), label, OP_CREATE,
127856974a6fSJohn Johansen 					   AA_MAY_CREATE, family, type,
127956974a6fSJohn Johansen 					   protocol);
128056974a6fSJohn Johansen 	}
128156974a6fSJohn Johansen 	end_current_label_crit_section(label);
128256974a6fSJohn Johansen 
128356974a6fSJohn Johansen 	return error;
128456974a6fSJohn Johansen }
128556974a6fSJohn Johansen 
128656974a6fSJohn Johansen /**
128756974a6fSJohn Johansen  * apparmor_socket_post_create - setup the per-socket security struct
128856974a6fSJohn Johansen  * @sock: socket that is being setup
1289e1af4779SArnd Bergmann  * @family: family of socket being created
129056974a6fSJohn Johansen  * @type: type of the socket
12910fc6ab40SYang Li  * @protocol: protocol of the socket
12921cba2750SJohn Johansen  * @kern: socket is a special kernel socket
12931cba2750SJohn Johansen  *
129456974a6fSJohn Johansen  * Note:
129556974a6fSJohn Johansen  * -   kernel sockets labeled kernel_t used to use unconfined
129656974a6fSJohn Johansen  * -   socket may not have sk here if created with sock_create_lite or
129756974a6fSJohn Johansen  *     sock_alloc. These should be accept cases which will be handled in
129856974a6fSJohn Johansen  *     sock_graft.
129956974a6fSJohn Johansen  */
apparmor_socket_post_create(struct socket * sock,int family,int type,int protocol,int kern)130056974a6fSJohn Johansen static int apparmor_socket_post_create(struct socket *sock, int family,
130156974a6fSJohn Johansen 				       int type, int protocol, int kern)
130279ddd4a7SJohn Johansen {
1303ab9f2115SMatthew Garrett 	struct aa_label *label;
1304ab9f2115SMatthew Garrett 
130556974a6fSJohn Johansen 	if (kern) {
1306ab9f2115SMatthew Garrett 		label = aa_get_label(kernel_t);
1307ab9f2115SMatthew Garrett 	} else
1308ab9f2115SMatthew Garrett 		label = aa_get_current_label();
130956974a6fSJohn Johansen 
1310e1af4779SArnd Bergmann 	if (sock->sk) {
131156974a6fSJohn Johansen 		struct aa_sk_ctx *ctx = aa_sock(sock->sk);
131256974a6fSJohn Johansen 
131356974a6fSJohn Johansen 		/* still not live */
131456974a6fSJohn Johansen 		aa_put_label(rcu_dereference_protected(ctx->label, true));
131579ddd4a7SJohn Johansen 		rcu_assign_pointer(ctx->label, aa_get_label(label));
131656974a6fSJohn Johansen 	}
131756974a6fSJohn Johansen 	aa_put_label(label);
131856974a6fSJohn Johansen 
131956974a6fSJohn Johansen 	return 0;
132056974a6fSJohn Johansen }
132156974a6fSJohn Johansen 
apparmor_socket_socketpair(struct socket * socka,struct socket * sockb)132256974a6fSJohn Johansen static int apparmor_socket_socketpair(struct socket *socka,
132356974a6fSJohn Johansen 				      struct socket *sockb)
132456974a6fSJohn Johansen {
13251cba2750SJohn Johansen 	struct aa_sk_ctx *a_ctx = aa_sock(socka->sk);
13261cba2750SJohn Johansen 	struct aa_sk_ctx *b_ctx = aa_sock(sockb->sk);
13271cba2750SJohn Johansen 	struct aa_label *label;
13281cba2750SJohn Johansen 
13291cba2750SJohn Johansen 	/* socks not live yet - initial values set in sk_alloc */
133056974a6fSJohn Johansen 	label = begin_current_label_crit_section();
133156974a6fSJohn Johansen 	if (rcu_access_pointer(a_ctx->label) != label) {
133256974a6fSJohn Johansen 		AA_BUG("a_ctx != label");
133356974a6fSJohn Johansen 		aa_put_label(rcu_dereference_protected(a_ctx->label, true));
1334b10b9c34SPaul Moore 		rcu_assign_pointer(a_ctx->label, aa_get_label(label));
133556974a6fSJohn Johansen 	}
133656974a6fSJohn Johansen 	if (rcu_access_pointer(b_ctx->label) != label) {
1337b10b9c34SPaul Moore 		AA_BUG("b_ctx != label");
133856974a6fSJohn Johansen 		aa_put_label(rcu_dereference_protected(b_ctx->label, true));
133956974a6fSJohn Johansen 		rcu_assign_pointer(b_ctx->label, aa_get_label(label));
134056974a6fSJohn Johansen 	}
134156974a6fSJohn Johansen 
134256974a6fSJohn Johansen 	if (socka->sk->sk_family == PF_UNIX) {
134356974a6fSJohn Johansen 		/* unix socket pairs by-pass unix_stream_connect */
134456974a6fSJohn Johansen 		unix_connect_peers(a_ctx, b_ctx);
134556974a6fSJohn Johansen 	}
134656974a6fSJohn Johansen 	end_current_label_crit_section(label);
134756974a6fSJohn Johansen 
134856974a6fSJohn Johansen 	return 0;
134956974a6fSJohn Johansen }
135056974a6fSJohn Johansen 
135156974a6fSJohn Johansen /**
135256974a6fSJohn Johansen  * apparmor_socket_bind - check perms before bind addr to socket
135356974a6fSJohn Johansen  * @sock: socket to bind the address to (must be non-NULL)
1354b10b9c34SPaul Moore  * @address: address that is being bound (must be non-NULL)
1355b10b9c34SPaul Moore  * @addrlen: length of @address
135656974a6fSJohn Johansen  *
135756974a6fSJohn Johansen  * Performs security checks before allowing a socket to bind to an address.
1358b10b9c34SPaul Moore  * Handles Unix domain sockets specially through aa_unix_bind_perm().
135956974a6fSJohn Johansen  * For other socket families, uses generic permission check via aa_sk_perm().
136056974a6fSJohn Johansen  *
1361b10b9c34SPaul Moore  * Return:
1362b10b9c34SPaul Moore  *   0 if binding is permitted
1363b10b9c34SPaul Moore  *   error code on denial or invalid parameters
1364b10b9c34SPaul Moore  */
apparmor_socket_bind(struct socket * sock,struct sockaddr * address,int addrlen)1365b10b9c34SPaul Moore static int apparmor_socket_bind(struct socket *sock,
136656974a6fSJohn Johansen 				struct sockaddr *address, int addrlen)
136756974a6fSJohn Johansen {
1368b10b9c34SPaul Moore 	AA_BUG(!sock);
136956974a6fSJohn Johansen 	AA_BUG(!sock->sk);
137056974a6fSJohn Johansen 	AA_BUG(!address);
137156974a6fSJohn Johansen 	AA_BUG(in_interrupt());
137256974a6fSJohn Johansen 
137356974a6fSJohn Johansen 	if (sock->sk->sk_family == PF_UNIX)
137456974a6fSJohn Johansen 		return aa_unix_bind_perm(sock, address, addrlen);
137556974a6fSJohn Johansen 	return aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk);
137656974a6fSJohn Johansen }
137756974a6fSJohn Johansen 
apparmor_socket_connect(struct socket * sock,struct sockaddr * address,int addrlen)137856974a6fSJohn Johansen static int apparmor_socket_connect(struct socket *sock,
137956974a6fSJohn Johansen 				   struct sockaddr *address, int addrlen)
138056974a6fSJohn Johansen {
138156974a6fSJohn Johansen 	AA_BUG(!sock);
138256974a6fSJohn Johansen 	AA_BUG(!sock->sk);
138356974a6fSJohn Johansen 	AA_BUG(!address);
138456974a6fSJohn Johansen 	AA_BUG(in_interrupt());
138556974a6fSJohn Johansen 
138656974a6fSJohn Johansen 	/* PF_UNIX goes through unix_stream_connect && unix_may_send */
138756974a6fSJohn Johansen 	if (sock->sk->sk_family == PF_UNIX)
138856974a6fSJohn Johansen 		return 0;
138956974a6fSJohn Johansen 	return aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk);
139056974a6fSJohn Johansen }
139156974a6fSJohn Johansen 
apparmor_socket_listen(struct socket * sock,int backlog)139256974a6fSJohn Johansen static int apparmor_socket_listen(struct socket *sock, int backlog)
139356974a6fSJohn Johansen {
139456974a6fSJohn Johansen 	AA_BUG(!sock);
139556974a6fSJohn Johansen 	AA_BUG(!sock->sk);
139656974a6fSJohn Johansen 	AA_BUG(in_interrupt());
139756974a6fSJohn Johansen 
139856974a6fSJohn Johansen 	if (sock->sk->sk_family == PF_UNIX)
139956974a6fSJohn Johansen 		return aa_unix_listen_perm(sock, backlog);
140056974a6fSJohn Johansen 	return aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk);
140179ddd4a7SJohn Johansen }
140256974a6fSJohn Johansen 
140356974a6fSJohn Johansen /*
140456974a6fSJohn Johansen  * Note: while @newsock is created and has some information, the accept
140556974a6fSJohn Johansen  *       has not been done.
140656974a6fSJohn Johansen  */
apparmor_socket_accept(struct socket * sock,struct socket * newsock)1407e1af4779SArnd Bergmann static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
140841dd9596SFlorian Westphal {
1409ab9f2115SMatthew Garrett 	AA_BUG(!sock);
1410ab9f2115SMatthew Garrett 	AA_BUG(!sock->sk);
141179ddd4a7SJohn Johansen 	AA_BUG(!newsock);
1412ab9f2115SMatthew Garrett 	AA_BUG(in_interrupt());
1413ab9f2115SMatthew Garrett 
1414ab9f2115SMatthew Garrett 	if (sock->sk->sk_family == PF_UNIX)
1415ab9f2115SMatthew Garrett 		return aa_unix_accept_perm(sock, newsock);
1416ab9f2115SMatthew Garrett 	return aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk);
1417ab9f2115SMatthew Garrett }
1418ab9f2115SMatthew Garrett 
aa_sock_msg_perm(const char * op,u32 request,struct socket * sock,struct msghdr * msg,int size)1419e1af4779SArnd Bergmann static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
1420ab9f2115SMatthew Garrett 			    struct msghdr *msg, int size)
1421bbd3662aSCasey Schaufler {
142237923d43SXiu Jianfeng 	AA_BUG(!sock);
1423bbd3662aSCasey Schaufler 	AA_BUG(!sock->sk);
1424f22f9aafSPaul Moore 	AA_BUG(!msg);
142537923d43SXiu Jianfeng 	AA_BUG(in_interrupt());
142633bf60caSCasey Schaufler 
1427f4ad8f2cSCasey Schaufler 	/* PF_UNIX goes through unix_may_send */
1428bbd3662aSCasey Schaufler 	if (sock->sk->sk_family == PF_UNIX)
1429bbd3662aSCasey Schaufler 		return 0;
1430b1a867eeSPaul Moore 	return aa_sk_perm(op, request, sock->sk);
1431f3b8788cSCasey Schaufler }
1432f3b8788cSCasey Schaufler 
apparmor_socket_sendmsg(struct socket * sock,struct msghdr * msg,int size)1433f3b8788cSCasey Schaufler static int apparmor_socket_sendmsg(struct socket *sock,
1434f3b8788cSCasey Schaufler 				   struct msghdr *msg, int size)
1435f22f9aafSPaul Moore {
1436e20b043aSCasey Schaufler 	return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
1437e20b043aSCasey Schaufler }
1438e20b043aSCasey Schaufler 
apparmor_socket_recvmsg(struct socket * sock,struct msghdr * msg,int size,int flags)1439e20b043aSCasey Schaufler static int apparmor_socket_recvmsg(struct socket *sock,
1440b5e95b48SJohn Johansen 				   struct msghdr *msg, int size, int flags)
1441157a3537SJohn Johansen {
14422ea3ffb7SJohn Johansen 	return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
14432ea3ffb7SJohn Johansen }
14442ea3ffb7SJohn Johansen 
14452ea3ffb7SJohn Johansen /* revaliation, get/set attr, shutdown */
aa_sock_perm(const char * op,u32 request,struct socket * sock)1446e20b043aSCasey Schaufler static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
1447e20b043aSCasey Schaufler {
1448e20b043aSCasey Schaufler 	AA_BUG(!sock);
1449e20b043aSCasey Schaufler 	AA_BUG(!sock->sk);
1450e20b043aSCasey Schaufler 	AA_BUG(in_interrupt());
1451e20b043aSCasey Schaufler 
1452e20b043aSCasey Schaufler 	if (sock->sk->sk_family == PF_UNIX)
1453e20b043aSCasey Schaufler 		return aa_unix_sock_perm(op, request, sock);
1454e20b043aSCasey Schaufler 	return aa_sk_perm(op, request, sock->sk);
1455e20b043aSCasey Schaufler }
1456e20b043aSCasey Schaufler 
apparmor_socket_getsockname(struct socket * sock)1457b5e95b48SJohn Johansen static int apparmor_socket_getsockname(struct socket *sock)
1458e20b043aSCasey Schaufler {
1459064dc947SJohn Johansen 	return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
1460e20b043aSCasey Schaufler }
1461e20b043aSCasey Schaufler 
apparmor_socket_getpeername(struct socket * sock)1462e20b043aSCasey Schaufler static int apparmor_socket_getpeername(struct socket *sock)
1463e20b043aSCasey Schaufler {
1464e20b043aSCasey Schaufler 	return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
1465e20b043aSCasey Schaufler }
14663350607dSGünther Noack 
1467b5e95b48SJohn Johansen /* revaliation, get/set attr, opt */
aa_sock_opt_perm(const char * op,u32 request,struct socket * sock,int level,int optname)1468223981dbSCasey Schaufler static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
1469223981dbSCasey Schaufler 			    int level, int optname)
1470e20b043aSCasey Schaufler {
1471e20b043aSCasey Schaufler 	AA_BUG(!sock);
1472b5e95b48SJohn Johansen 	AA_BUG(!sock->sk);
147356974a6fSJohn Johansen 	AA_BUG(in_interrupt());
147456974a6fSJohn Johansen 
147556974a6fSJohn Johansen 	if (sock->sk->sk_family == PF_UNIX)
147656974a6fSJohn Johansen 		return aa_unix_opt_perm(op, request, sock, level, optname);
147756974a6fSJohn Johansen 	return aa_sk_perm(op, request, sock->sk);
147856974a6fSJohn Johansen }
147956974a6fSJohn Johansen 
apparmor_socket_getsockopt(struct socket * sock,int level,int optname)148056974a6fSJohn Johansen static int apparmor_socket_getsockopt(struct socket *sock, int level,
148156974a6fSJohn Johansen 				      int optname)
148256974a6fSJohn Johansen {
148356974a6fSJohn Johansen 	return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
148456974a6fSJohn Johansen 				level, optname);
148556974a6fSJohn Johansen }
148656974a6fSJohn Johansen 
apparmor_socket_setsockopt(struct socket * sock,int level,int optname)148756974a6fSJohn Johansen static int apparmor_socket_setsockopt(struct socket *sock, int level,
148856974a6fSJohn Johansen 				      int optname)
148956974a6fSJohn Johansen {
1490e1af4779SArnd Bergmann 	return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
149156974a6fSJohn Johansen 				level, optname);
1492e1af4779SArnd Bergmann }
149356974a6fSJohn Johansen 
apparmor_socket_shutdown(struct socket * sock,int how)149456974a6fSJohn Johansen static int apparmor_socket_shutdown(struct socket *sock, int how)
149556974a6fSJohn Johansen {
149656974a6fSJohn Johansen 	return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
149756974a6fSJohn Johansen }
1498e1af4779SArnd Bergmann 
1499ab9f2115SMatthew Garrett #ifdef CONFIG_NETWORK_SECMARK
1500e1af4779SArnd Bergmann /**
150156974a6fSJohn Johansen  * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
1502e20b043aSCasey Schaufler  * @sk: sk to associate @skb with
1503e20b043aSCasey Schaufler  * @skb: skb to check for perms
1504e20b043aSCasey Schaufler  *
1505e20b043aSCasey Schaufler  * Note: can not sleep may be called with locks held
1506b5e95b48SJohn Johansen  *
1507b8bff599SEric W. Biederman  * dont want protocol specific in __skb_recv_datagram()
1508e20b043aSCasey Schaufler  * to deny an incoming connection  socket_sock_rcv_skb()
1509e20b043aSCasey Schaufler  */
apparmor_socket_sock_rcv_skb(struct sock * sk,struct sk_buff * skb)1510b5e95b48SJohn Johansen static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
15113b529a76SJohn Johansen {
15123b529a76SJohn Johansen 	struct aa_sk_ctx *ctx = aa_sock(sk);
15136326948fSPaul Moore 	int error;
15146326948fSPaul Moore 
1515e20b043aSCasey Schaufler 	if (!skb->secmark)
1516cd1dbf76SJohn Johansen 		return 0;
1517fa9b63adSJohn Johansen 
1518c0929212SJohn Johansen 	/*
1519e79c26d0SMatthew Garrett 	 * If reach here before socket_post_create hook is called, in which
1520e79c26d0SMatthew Garrett 	 * case label is null, drop the packet.
1521e79c26d0SMatthew Garrett 	 */
1522e79c26d0SMatthew Garrett 	if (!rcu_access_pointer(ctx->label))
1523e79c26d0SMatthew Garrett 		return -EACCES;
1524e79c26d0SMatthew Garrett 
1525e79c26d0SMatthew Garrett 	rcu_read_lock();
1526c0929212SJohn Johansen 	error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_RECVMSG,
1527c0929212SJohn Johansen 				       AA_MAY_RECEIVE, skb->secmark, sk);
1528c0929212SJohn Johansen 	rcu_read_unlock();
1529c4371d90SGeorgia Garcia 
1530c4371d90SGeorgia Garcia 	return error;
1531c4371d90SGeorgia Garcia }
1532c4371d90SGeorgia Garcia #endif
1533c4371d90SGeorgia Garcia 
1534b5e95b48SJohn Johansen 
sk_peer_get_label(struct sock * sk)1535b5e95b48SJohn Johansen static struct aa_label *sk_peer_get_label(struct sock *sk)
1536b5e95b48SJohn Johansen {
1537b5e95b48SJohn Johansen 	struct aa_sk_ctx *ctx = aa_sock(sk);
1538b5e95b48SJohn Johansen 	struct aa_label *label = ERR_PTR(-ENOPROTOOPT);
1539b5e95b48SJohn Johansen 
1540101d6c82SStephen Rothwell 	if (rcu_access_pointer(ctx->peer))
1541101d6c82SStephen Rothwell 		return aa_get_label_rcu(&ctx->peer);
1542b8aa09fdSRusty Russell 
15439c27847dSLuis R. Rodriguez 	if (sk->sk_family != PF_UNIX)
15446a4c2643SJani Nikula 		return ERR_PTR(-ENOPROTOOPT);
1545101d6c82SStephen Rothwell 
1546101d6c82SStephen Rothwell 	return label;
1547101d6c82SStephen Rothwell }
1548b5e95b48SJohn Johansen 
1549101d6c82SStephen Rothwell /**
1550101d6c82SStephen Rothwell  * apparmor_socket_getpeersec_stream - get security context of peer
1551b8aa09fdSRusty Russell  * @sock: socket that we are trying to get the peer context of
15529c27847dSLuis R. Rodriguez  * @optval: output - buffer to copy peer name to
1553101d6c82SStephen Rothwell  * @optlen: output - size of copied name in @optval
1554101d6c82SStephen Rothwell  * @len: size of @optval buffer
1555101d6c82SStephen Rothwell  * Returns: 0 on success, -errno of failure
1556b5e95b48SJohn Johansen  *
155763c16c3aSChris Coulson  * Note: for tcp only valid if using ipsec or cipso on lan
155863c16c3aSChris Coulson  */
apparmor_socket_getpeersec_stream(struct socket * sock,sockptr_t optval,sockptr_t optlen,unsigned int len)155963c16c3aSChris Coulson static int apparmor_socket_getpeersec_stream(struct socket *sock,
156063c16c3aSChris Coulson 					     sockptr_t optval, sockptr_t optlen,
156163c16c3aSChris Coulson 					     unsigned int len)
156263c16c3aSChris Coulson {
156363c16c3aSChris Coulson 	char *name = NULL;
156463c16c3aSChris Coulson 	int slen, error = 0;
156563c16c3aSChris Coulson 	struct aa_label *label;
156663c16c3aSChris Coulson 	struct aa_label *peer;
1567101d6c82SStephen Rothwell 
1568101d6c82SStephen Rothwell 	peer = sk_peer_get_label(sock->sk);
1569b8aa09fdSRusty Russell 	if (IS_ERR(peer)) {
15709c27847dSLuis R. Rodriguez 		error = PTR_ERR(peer);
15716a4c2643SJani Nikula 		goto done;
1572101d6c82SStephen Rothwell 	}
1573101d6c82SStephen Rothwell 	label = begin_current_label_crit_section();
1574101d6c82SStephen Rothwell 	slen = aa_label_asxprint(&name, labels_ns(label), peer,
1575b5e95b48SJohn Johansen 				 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1576e4dca7b7SKees Cook 				 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1577e4dca7b7SKees Cook 	/* don't include terminating \0 in slen, it breaks some apps */
1578b5e95b48SJohn Johansen 	if (slen < 0) {
1579e4dca7b7SKees Cook 		error = -ENOMEM;
1580e4dca7b7SKees Cook 		goto done_put;
1581b5e95b48SJohn Johansen 	}
1582b5e95b48SJohn Johansen 	if (slen > len) {
1583b5e95b48SJohn Johansen 		error = -ERANGE;
1584b5e95b48SJohn Johansen 		goto done_len;
1585b5e95b48SJohn Johansen 	}
1586b5e95b48SJohn Johansen 
1587b5e95b48SJohn Johansen 	if (copy_to_sockptr(optval, name, slen))
1588b5e95b48SJohn Johansen 		error = -EFAULT;
1589b5e95b48SJohn Johansen done_len:
1590b5e95b48SJohn Johansen 	if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
15916059f71fSJohn Johansen 		error = -EFAULT;
15927616ac70SArnd Bergmann 
15933ccb76c5SJohn Johansen done_put:
15946059f71fSJohn Johansen 	end_current_label_crit_section(label);
15957616ac70SArnd Bergmann 	aa_put_label(peer);
15966059f71fSJohn Johansen done:
1597d61c57fdSJohn Johansen 	kfree(name);
1598d61c57fdSJohn Johansen 	return error;
1599d61c57fdSJohn Johansen }
1600d61c57fdSJohn Johansen 
1601d61c57fdSJohn Johansen /**
1602d61c57fdSJohn Johansen  * apparmor_socket_getpeersec_dgram - get security label of packet
160363c16c3aSChris Coulson  * @sock: the peer socket
160470f24a9fSJohn Johansen  * @skb: packet data
160563c16c3aSChris Coulson  * @secid: pointer to where to put the secid of the packet
160663c16c3aSChris Coulson  *
160763c16c3aSChris Coulson  * Sets the netlabel socket state on sk from parent
1608b5e95b48SJohn Johansen  */
apparmor_socket_getpeersec_dgram(struct socket * sock,struct sk_buff * skb,u32 * secid)1609eea7a05fSValentin Rothberg static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1610b5e95b48SJohn Johansen 					    struct sk_buff *skb, u32 *secid)
1611b5e95b48SJohn Johansen 
1612b5e95b48SJohn Johansen {
1613b5e95b48SJohn Johansen 	/* TODO: requires secid support */
1614b5e95b48SJohn Johansen 	return -ENOPROTOOPT;
1615b5e95b48SJohn Johansen }
1616b5e95b48SJohn Johansen 
1617b5e95b48SJohn Johansen /**
1618b5e95b48SJohn Johansen  * apparmor_sock_graft - Initialize newly created socket
1619b5e95b48SJohn Johansen  * @sk: child sock
1620954317feSThomas Meyer  * @parent: parent socket
1621b5e95b48SJohn Johansen  *
1622b5e95b48SJohn Johansen  * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1623b5e95b48SJohn Johansen  *       just set sk security information off of current creating process label
1624b5e95b48SJohn Johansen  *       Labeling of sk for accept case - probably should be sock based
1625b5e95b48SJohn Johansen  *       instead of task, because of the case where an implicitly labeled
1626b5e95b48SJohn Johansen  *       socket is shared by different tasks.
1627b5e95b48SJohn Johansen  */
apparmor_sock_graft(struct sock * sk,struct socket * parent)162890ab5ee9SRusty Russell static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1629b5e95b48SJohn Johansen {
1630b5e95b48SJohn Johansen 	struct aa_sk_ctx *ctx = aa_sock(sk);
1631b5e95b48SJohn Johansen 
1632b5e95b48SJohn Johansen 	/* setup - not live */
163390ab5ee9SRusty Russell 	if (!rcu_access_pointer(ctx->label))
1634b5e95b48SJohn Johansen 		rcu_assign_pointer(ctx->label, aa_get_current_label());
1635b5e95b48SJohn Johansen }
1636b5e95b48SJohn Johansen 
1637b5e95b48SJohn Johansen #ifdef CONFIG_NETWORK_SECMARK
apparmor_inet_conn_request(const struct sock * sk,struct sk_buff * skb,struct request_sock * req)1638622f6e32SJohn Johansen static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1639b5e95b48SJohn Johansen 				      struct request_sock *req)
1640b5e95b48SJohn Johansen {
1641b5e95b48SJohn Johansen 	struct aa_sk_ctx *ctx = aa_sock(sk);
1642abbf8734SJohn Johansen 	int error;
1643abbf8734SJohn Johansen 
1644b5e95b48SJohn Johansen 	if (!skb->secmark)
164579eb2711SLukas Bulwahn 		return 0;
1646abbf8734SJohn Johansen 
1647b5e95b48SJohn Johansen 	rcu_read_lock();
1648e33c1b99SKees Cook 	error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_CONNECT,
1649e33c1b99SKees Cook 				       AA_MAY_CONNECT, skb->secmark, sk);
1650e33c1b99SKees Cook 	rcu_read_unlock();
1651e33c1b99SKees Cook 
1652e33c1b99SKees Cook 	return error;
1653e33c1b99SKees Cook }
1654e33c1b99SKees Cook #endif
1655b5e95b48SJohn Johansen 
1656f22f9aafSPaul Moore /*
1657e33c1b99SKees Cook  * The cred blob is a pointer to, not an instance of, an aa_label.
1658b5e95b48SJohn Johansen  */
1659b5e95b48SJohn Johansen struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
1660b5e95b48SJohn Johansen 	.lbs_cred = sizeof(struct aa_label *),
1661b5e95b48SJohn Johansen 	.lbs_file = sizeof(struct aa_file_ctx),
166229707b20SJingoo Han 	.lbs_task = sizeof(struct aa_task_ctx),
1663b5e95b48SJohn Johansen 	.lbs_sock = sizeof(struct aa_sk_ctx),
1664b5e95b48SJohn Johansen };
1665b5e95b48SJohn Johansen 
1666b5e95b48SJohn Johansen static const struct lsm_id apparmor_lsmid = {
1667b5e95b48SJohn Johansen 	.name = "apparmor",
1668b5e95b48SJohn Johansen 	.id = LSM_ID_APPARMOR,
1669b5e95b48SJohn Johansen };
1670b5e95b48SJohn Johansen 
1671101d6c82SStephen Rothwell static struct security_hook_list apparmor_hooks[] __ro_after_init = {
1672b5e95b48SJohn Johansen 	LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1673545de8feSJohn Johansen 	LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1674545de8feSJohn Johansen 	LSM_HOOK_INIT(capget, apparmor_capget),
167592de220aSJohn Johansen 	LSM_HOOK_INIT(capable, apparmor_capable),
1676b5e95b48SJohn Johansen 
1677b5e95b48SJohn Johansen 	LSM_HOOK_INIT(move_mount, apparmor_move_mount),
1678b5e95b48SJohn Johansen 	LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1679b5e95b48SJohn Johansen 	LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1680101d6c82SStephen Rothwell 	LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1681b5e95b48SJohn Johansen 
1682ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(path_link, apparmor_path_link),
1683ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
168492de220aSJohn Johansen 	LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1685545de8feSJohn Johansen 	LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1686b5e95b48SJohn Johansen 	LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1687b5e95b48SJohn Johansen 	LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1688b5e95b48SJohn Johansen 	LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1689101d6c82SStephen Rothwell 	LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1690b5e95b48SJohn Johansen 	LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1691ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1692ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
169392de220aSJohn Johansen 
1694545de8feSJohn Johansen 	LSM_HOOK_INIT(file_open, apparmor_file_open),
1695b5e95b48SJohn Johansen 	LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1696b5e95b48SJohn Johansen 	LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1697b5e95b48SJohn Johansen 	LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1698101d6c82SStephen Rothwell 	LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1699b5e95b48SJohn Johansen 	LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1700ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1701ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(file_lock, apparmor_file_lock),
170292de220aSJohn Johansen 	LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
1703545de8feSJohn Johansen 
1704b5e95b48SJohn Johansen 	LSM_HOOK_INIT(getselfattr, apparmor_getselfattr),
1705b5e95b48SJohn Johansen 	LSM_HOOK_INIT(setselfattr, apparmor_setselfattr),
1706b5e95b48SJohn Johansen 	LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1707101d6c82SStephen Rothwell 	LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1708b5e95b48SJohn Johansen 
170939d84824SJohn Johansen 	LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
171039d84824SJohn Johansen 	LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1711ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1712ca4bd5aeSJohn Johansen 
171339d84824SJohn Johansen 	LSM_HOOK_INIT(unix_stream_connect, apparmor_unix_stream_connect),
171439d84824SJohn Johansen 	LSM_HOOK_INIT(unix_may_send, apparmor_unix_may_send),
1715545de8feSJohn Johansen 
171639d84824SJohn Johansen 	LSM_HOOK_INIT(socket_create, apparmor_socket_create),
171739d84824SJohn Johansen 	LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1718df323337SSebastian Andrzej Siewior 	LSM_HOOK_INIT(socket_socketpair, apparmor_socket_socketpair),
171939d84824SJohn Johansen 	LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
172039d84824SJohn Johansen 	LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
172139d84824SJohn Johansen 	LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1722b5e95b48SJohn Johansen 	LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1723b5e95b48SJohn Johansen 	LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1724101d6c82SStephen Rothwell 	LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1725b5e95b48SJohn Johansen 	LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1726ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1727ca4bd5aeSJohn Johansen 	LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
172892de220aSJohn Johansen 	LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1729545de8feSJohn Johansen 	LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1730b5e95b48SJohn Johansen #ifdef CONFIG_NETWORK_SECMARK
1731b5e95b48SJohn Johansen 	LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1732b5e95b48SJohn Johansen #endif
1733e33c1b99SKees Cook 	LSM_HOOK_INIT(socket_getpeersec_stream,
1734e33c1b99SKees Cook 		      apparmor_socket_getpeersec_stream),
1735e33c1b99SKees Cook 	LSM_HOOK_INIT(socket_getpeersec_dgram,
1736e33c1b99SKees Cook 		      apparmor_socket_getpeersec_dgram),
1737e33c1b99SKees Cook 	LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1738e33c1b99SKees Cook #ifdef CONFIG_NETWORK_SECMARK
1739e33c1b99SKees Cook 	LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1740e33c1b99SKees Cook #endif
1741e33c1b99SKees Cook 
1742e33c1b99SKees Cook 	LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1743e33c1b99SKees Cook 	LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1744e33c1b99SKees Cook 	LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1745e33c1b99SKees Cook 	LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1746e33c1b99SKees Cook 
1747e33c1b99SKees Cook 	LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1748e33c1b99SKees Cook 	LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1749e33c1b99SKees Cook 	LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1750e33c1b99SKees Cook 
1751e33c1b99SKees Cook 	LSM_HOOK_INIT(task_free, apparmor_task_free),
1752e33c1b99SKees Cook 	LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
1753e33c1b99SKees Cook 	LSM_HOOK_INIT(current_getlsmprop_subj,
1754e33c1b99SKees Cook 		      apparmor_current_getlsmprop_subj),
1755e33c1b99SKees Cook 	LSM_HOOK_INIT(task_getlsmprop_obj, apparmor_task_getlsmprop_obj),
1756e33c1b99SKees Cook 	LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1757e33c1b99SKees Cook 	LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1758e33c1b99SKees Cook 	LSM_HOOK_INIT(userns_create, apparmor_userns_create),
1759e33c1b99SKees Cook 
1760e33c1b99SKees Cook #ifdef CONFIG_AUDIT
1761e33c1b99SKees Cook 	LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1762e33c1b99SKees Cook 	LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1763e33c1b99SKees Cook 	LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1764e33c1b99SKees Cook 	LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1765e33c1b99SKees Cook #endif
1766e33c1b99SKees Cook 
1767e33c1b99SKees Cook 	LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1768e33c1b99SKees Cook 	LSM_HOOK_INIT(lsmprop_to_secctx, apparmor_lsmprop_to_secctx),
1769e33c1b99SKees Cook 	LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1770e33c1b99SKees Cook 	LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1771e33c1b99SKees Cook 
1772e33c1b99SKees Cook #ifdef CONFIG_IO_URING
177363c16c3aSChris Coulson 	LSM_HOOK_INIT(uring_override_creds, apparmor_uring_override_creds),
177463c16c3aSChris Coulson 	LSM_HOOK_INIT(uring_sqpoll, apparmor_uring_sqpoll),
177563c16c3aSChris Coulson #endif
177663c16c3aSChris Coulson };
177763c16c3aSChris Coulson 
177863c16c3aSChris Coulson /*
177963c16c3aSChris Coulson  * AppArmor sysfs module parameters
178063c16c3aSChris Coulson  */
178163c16c3aSChris Coulson 
178263c16c3aSChris Coulson static int param_set_aabool(const char *val, const struct kernel_param *kp);
178363c16c3aSChris Coulson static int param_get_aabool(char *buffer, const struct kernel_param *kp);
178463c16c3aSChris Coulson #define param_check_aabool param_check_bool
178563c16c3aSChris Coulson static const struct kernel_param_ops param_ops_aabool = {
178670f24a9fSJohn Johansen 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1787f4d6b94bSJon Tourville 	.set = param_set_aabool,
178863c16c3aSChris Coulson 	.get = param_get_aabool
178963c16c3aSChris Coulson };
179063c16c3aSChris Coulson 
179163c16c3aSChris Coulson static int param_set_aauint(const char *val, const struct kernel_param *kp);
179263c16c3aSChris Coulson static int param_get_aauint(char *buffer, const struct kernel_param *kp);
179363c16c3aSChris Coulson #define param_check_aauint param_check_uint
179463c16c3aSChris Coulson static const struct kernel_param_ops param_ops_aauint = {
179563c16c3aSChris Coulson 	.set = param_set_aauint,
179663c16c3aSChris Coulson 	.get = param_get_aauint
179763c16c3aSChris Coulson };
179892de220aSJohn Johansen 
179963c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val,
180063c16c3aSChris Coulson 					const struct kernel_param *kp);
180163c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer,
180263c16c3aSChris Coulson 					const struct kernel_param *kp);
1803e4dca7b7SKees Cook #define param_check_aacompressionlevel param_check_int
1804b5e95b48SJohn Johansen static const struct kernel_param_ops param_ops_aacompressionlevel = {
1805b5e95b48SJohn Johansen 	.set = param_set_aacompressionlevel,
1806b5e95b48SJohn Johansen 	.get = param_get_aacompressionlevel
180792de220aSJohn Johansen };
1808545de8feSJohn Johansen 
1809b5e95b48SJohn Johansen static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1810b5e95b48SJohn Johansen static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1811b5e95b48SJohn Johansen #define param_check_aalockpolicy param_check_bool
1812e4dca7b7SKees Cook static const struct kernel_param_ops param_ops_aalockpolicy = {
1813b5e95b48SJohn Johansen 	.flags = KERNEL_PARAM_OPS_FL_NOARG,
1814b5e95b48SJohn Johansen 	.set = param_set_aalockpolicy,
1815b5e95b48SJohn Johansen 	.get = param_get_aalockpolicy
1816b5e95b48SJohn Johansen };
1817b5e95b48SJohn Johansen 
1818b5e95b48SJohn Johansen static int param_set_debug(const char *val, const struct kernel_param *kp);
1819b5e95b48SJohn Johansen static int param_get_debug(char *buffer, const struct kernel_param *kp);
182092de220aSJohn Johansen 
1821545de8feSJohn Johansen static int param_set_audit(const char *val, const struct kernel_param *kp);
1822b5e95b48SJohn Johansen static int param_get_audit(char *buffer, const struct kernel_param *kp);
18235d8779a5SAndy Shevchenko 
18245d8779a5SAndy Shevchenko static int param_set_mode(const char *val, const struct kernel_param *kp);
18255d8779a5SAndy Shevchenko static int param_get_mode(char *buffer, const struct kernel_param *kp);
18265d8779a5SAndy Shevchenko 
1827b5e95b48SJohn Johansen /* Flag values, also controllable via /sys/module/apparmor/parameters
1828b5e95b48SJohn Johansen  * We define special types as we want to do additional mediation.
1829b5e95b48SJohn Johansen  */
1830b5e95b48SJohn Johansen 
1831e4dca7b7SKees Cook /* AppArmor global enforcement switch - complain, enforce, kill */
1832b5e95b48SJohn Johansen enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1833b5e95b48SJohn Johansen module_param_call(mode, param_set_mode, param_get_mode,
1834b5e95b48SJohn Johansen 		  &aa_g_profile_mode, S_IRUSR | S_IWUSR);
183592de220aSJohn Johansen 
1836545de8feSJohn Johansen /* whether policy verification hashing is enabled */
1837b5e95b48SJohn Johansen bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
18380d259f04SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_HASH
1839b5e95b48SJohn Johansen module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1840b5e95b48SJohn Johansen #endif
1841e4dca7b7SKees Cook 
1842b5e95b48SJohn Johansen /* whether policy exactly as loaded is retained for debug and checkpointing */
1843b5e95b48SJohn Johansen bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1844b5e95b48SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1845b5e95b48SJohn Johansen module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1846b5e95b48SJohn Johansen #endif
1847b5e95b48SJohn Johansen 
1848b5e95b48SJohn Johansen /* policy loaddata compression level */
184992de220aSJohn Johansen int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
1850545de8feSJohn Johansen module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
1851b5e95b48SJohn Johansen 		   aacompressionlevel, 0400);
18525d8779a5SAndy Shevchenko 
18535d8779a5SAndy Shevchenko /* Debug mode */
18545d8779a5SAndy Shevchenko int aa_g_debug;
18555d8779a5SAndy Shevchenko module_param_call(debug, param_set_debug, param_get_debug,
18565d8779a5SAndy Shevchenko 		  &aa_g_debug, 0600);
1857b5e95b48SJohn Johansen 
1858b5e95b48SJohn Johansen /* Audit mode */
1859b5e95b48SJohn Johansen enum audit_mode aa_g_audit;
1860b5e95b48SJohn Johansen module_param_call(audit, param_set_audit, param_get_audit,
1861341c1fdaSJohn Johansen 		  &aa_g_audit, S_IRUSR | S_IWUSR);
1862df323337SSebastian Andrzej Siewior 
1863df323337SSebastian Andrzej Siewior /* Determines if audit header is included in audited messages.  This
1864ea9bae12SJohn Johansen  * provides more context if the audit daemon is not running
1865df323337SSebastian Andrzej Siewior  */
1866341c1fdaSJohn Johansen bool aa_g_audit_header = true;
1867df323337SSebastian Andrzej Siewior module_param_named(audit_header, aa_g_audit_header, aabool,
1868ea9bae12SJohn Johansen 		   S_IRUSR | S_IWUSR);
1869ea9bae12SJohn Johansen 
1870ea9bae12SJohn Johansen /* lock out loading/removal of policy
1871ea9bae12SJohn Johansen  * TODO: add in at boot loading of policy, which is the only way to
1872ea9bae12SJohn Johansen  *       load policy, if lock_policy is set
1873ea9bae12SJohn Johansen  */
1874ea9bae12SJohn Johansen bool aa_g_lock_policy;
1875ea9bae12SJohn Johansen module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1876ea9bae12SJohn Johansen 		   S_IRUSR | S_IWUSR);
1877ea9bae12SJohn Johansen 
1878ea9bae12SJohn Johansen /* Syscall logging mode */
1879ea9bae12SJohn Johansen bool aa_g_logsyscall;
1880ea9bae12SJohn Johansen module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1881ea9bae12SJohn Johansen 
1882ea9bae12SJohn Johansen /* Maximum pathname length before accesses will start getting rejected */
1883ea9bae12SJohn Johansen unsigned int aa_g_path_max = 2 * PATH_MAX;
1884df323337SSebastian Andrzej Siewior module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1885ea9bae12SJohn Johansen 
1886ea9bae12SJohn Johansen /* Determines how paranoid loading of policy is and how much verification
1887ea9bae12SJohn Johansen  * on the loaded policy is done.
1888ea9bae12SJohn Johansen  * DEPRECATED: read only as strict checking of load is always done now
1889ea9bae12SJohn Johansen  * that none root users (user namespaces) can load policy.
1890341c1fdaSJohn Johansen  */
1891341c1fdaSJohn Johansen bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1892df323337SSebastian Andrzej Siewior module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1893df323337SSebastian Andrzej Siewior 
1894df323337SSebastian Andrzej Siewior static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1895341c1fdaSJohn Johansen static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1896df323337SSebastian Andrzej Siewior #define param_check_aaintbool param_check_int
1897ba808cb5SKees Cook static const struct kernel_param_ops param_ops_aaintbool = {
1898df323337SSebastian Andrzej Siewior 	.set = param_set_aaintbool,
1899341c1fdaSJohn Johansen 	.get = param_get_aaintbool
1900341c1fdaSJohn Johansen };
1901341c1fdaSJohn Johansen /* Boot time disable flag */
1902341c1fdaSJohn Johansen static int apparmor_enabled __ro_after_init = 1;
1903341c1fdaSJohn Johansen module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1904341c1fdaSJohn Johansen 
apparmor_enabled_setup(char * str)1905341c1fdaSJohn Johansen static int __init apparmor_enabled_setup(char *str)
1906341c1fdaSJohn Johansen {
1907df323337SSebastian Andrzej Siewior 	unsigned long enabled;
1908df323337SSebastian Andrzej Siewior 	int error = kstrtoul(str, 0, &enabled);
1909341c1fdaSJohn Johansen 	if (!error)
1910341c1fdaSJohn Johansen 		apparmor_enabled = enabled ? 1 : 0;
1911341c1fdaSJohn Johansen 	return 1;
1912df323337SSebastian Andrzej Siewior }
1913df323337SSebastian Andrzej Siewior 
1914df323337SSebastian Andrzej Siewior __setup("apparmor=", apparmor_enabled_setup);
1915ea9bae12SJohn Johansen 
1916df323337SSebastian Andrzej Siewior /* set global flag turning off the ability to load policy */
param_set_aalockpolicy(const char * val,const struct kernel_param * kp)1917df323337SSebastian Andrzej Siewior static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1918df323337SSebastian Andrzej Siewior {
1919df323337SSebastian Andrzej Siewior 	if (!apparmor_enabled)
1920df323337SSebastian Andrzej Siewior 		return -EINVAL;
1921ba808cb5SKees Cook 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1922df323337SSebastian Andrzej Siewior 		return -EPERM;
1923df323337SSebastian Andrzej Siewior 	return param_set_bool(val, kp);
1924df323337SSebastian Andrzej Siewior }
1925df323337SSebastian Andrzej Siewior 
param_get_aalockpolicy(char * buffer,const struct kernel_param * kp)1926df323337SSebastian Andrzej Siewior static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1927ea9bae12SJohn Johansen {
1928df323337SSebastian Andrzej Siewior 	if (!apparmor_enabled)
1929df323337SSebastian Andrzej Siewior 		return -EINVAL;
1930df323337SSebastian Andrzej Siewior 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1931df323337SSebastian Andrzej Siewior 		return -EPERM;
1932df323337SSebastian Andrzej Siewior 	return param_get_bool(buffer, kp);
1933ea9bae12SJohn Johansen }
1934ea9bae12SJohn Johansen 
param_set_aabool(const char * val,const struct kernel_param * kp)1935ea9bae12SJohn Johansen static int param_set_aabool(const char *val, const struct kernel_param *kp)
1936ea9bae12SJohn Johansen {
1937ea9bae12SJohn Johansen 	if (!apparmor_enabled)
1938ea9bae12SJohn Johansen 		return -EINVAL;
1939df323337SSebastian Andrzej Siewior 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1940341c1fdaSJohn Johansen 		return -EPERM;
1941df323337SSebastian Andrzej Siewior 	return param_set_bool(val, kp);
1942ea9bae12SJohn Johansen }
1943ea9bae12SJohn Johansen 
param_get_aabool(char * buffer,const struct kernel_param * kp)1944ea9bae12SJohn Johansen static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1945ea9bae12SJohn Johansen {
1946ea9bae12SJohn Johansen 	if (!apparmor_enabled)
1947ea9bae12SJohn Johansen 		return -EINVAL;
1948ea9bae12SJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1949ea9bae12SJohn Johansen 		return -EPERM;
1950ea9bae12SJohn Johansen 	return param_get_bool(buffer, kp);
1951ea9bae12SJohn Johansen }
1952ea9bae12SJohn Johansen 
param_set_aauint(const char * val,const struct kernel_param * kp)1953ea9bae12SJohn Johansen static int param_set_aauint(const char *val, const struct kernel_param *kp)
1954ea9bae12SJohn Johansen {
1955df323337SSebastian Andrzej Siewior 	int error;
1956df323337SSebastian Andrzej Siewior 
1957b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1958b5e95b48SJohn Johansen 		return -EINVAL;
1959b5e95b48SJohn Johansen 	/* file is ro but enforce 2nd line check */
1960b5e95b48SJohn Johansen 	if (apparmor_initialized)
1961b5e95b48SJohn Johansen 		return -EPERM;
196255a26ebfSJohn Johansen 
1963b5e95b48SJohn Johansen 	error = param_set_uint(val, kp);
1964b5e95b48SJohn Johansen 	aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
1965b5e95b48SJohn Johansen 	pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
196655a26ebfSJohn Johansen 
1967b5e95b48SJohn Johansen 	return error;
1968bf1d2ee7SBharath Vedartham }
1969b5e95b48SJohn Johansen 
param_get_aauint(char * buffer,const struct kernel_param * kp)197069b5a44aSCasey Schaufler static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1971b5e95b48SJohn Johansen {
1972b5e95b48SJohn Johansen 	if (!apparmor_enabled)
1973b5e95b48SJohn Johansen 		return -EINVAL;
1974b5e95b48SJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1975d4669f0bSJohn Johansen 		return -EPERM;
1976d4669f0bSJohn Johansen 	return param_get_uint(buffer, kp);
1977df323337SSebastian Andrzej Siewior }
1978d4669f0bSJohn Johansen 
1979df323337SSebastian Andrzej Siewior /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
param_set_aaintbool(const char * val,const struct kernel_param * kp)1980df323337SSebastian Andrzej Siewior static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1981df323337SSebastian Andrzej Siewior {
1982df323337SSebastian Andrzej Siewior 	struct kernel_param kp_local;
1983df323337SSebastian Andrzej Siewior 	bool value;
1984df323337SSebastian Andrzej Siewior 	int error;
1985df323337SSebastian Andrzej Siewior 
1986df323337SSebastian Andrzej Siewior 	if (apparmor_initialized)
1987d4669f0bSJohn Johansen 		return -EPERM;
1988df323337SSebastian Andrzej Siewior 
1989d4669f0bSJohn Johansen 	/* Create local copy, with arg pointing to bool type. */
1990d4669f0bSJohn Johansen 	value = !!*((int *)kp->arg);
1991d4669f0bSJohn Johansen 	memcpy(&kp_local, kp, sizeof(kp_local));
1992d4669f0bSJohn Johansen 	kp_local.arg = &value;
1993df323337SSebastian Andrzej Siewior 
1994df323337SSebastian Andrzej Siewior 	error = param_set_bool(val, &kp_local);
1995d4669f0bSJohn Johansen 	if (!error)
1996df323337SSebastian Andrzej Siewior 		*((int *)kp->arg) = *((bool *)kp_local.arg);
1997ea9bae12SJohn Johansen 	return error;
1998ea9bae12SJohn Johansen }
1999ea9bae12SJohn Johansen 
2000ea9bae12SJohn Johansen /*
2001ea9bae12SJohn Johansen  * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
2002ea9bae12SJohn Johansen  * 1/0, this converts the "int that is actually bool" back to bool for
2003ea9bae12SJohn Johansen  * display in the /sys filesystem, while keeping it "int" for the LSM
2004ea9bae12SJohn Johansen  * infrastructure.
2005ea9bae12SJohn Johansen  */
param_get_aaintbool(char * buffer,const struct kernel_param * kp)2006df323337SSebastian Andrzej Siewior static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
2007df323337SSebastian Andrzej Siewior {
2008df323337SSebastian Andrzej Siewior 	struct kernel_param kp_local;
2009df323337SSebastian Andrzej Siewior 	bool value;
2010df323337SSebastian Andrzej Siewior 
2011df323337SSebastian Andrzej Siewior 	/* Create local copy, with arg pointing to bool type. */
2012df323337SSebastian Andrzej Siewior 	value = !!*((int *)kp->arg);
2013df323337SSebastian Andrzej Siewior 	memcpy(&kp_local, kp, sizeof(kp_local));
2014341c1fdaSJohn Johansen 	kp_local.arg = &value;
2015d4669f0bSJohn Johansen 
2016341c1fdaSJohn Johansen 	return param_get_bool(buffer, &kp_local);
2017df323337SSebastian Andrzej Siewior }
2018df323337SSebastian Andrzej Siewior 
param_set_aacompressionlevel(const char * val,const struct kernel_param * kp)2019df323337SSebastian Andrzej Siewior static int param_set_aacompressionlevel(const char *val,
2020df323337SSebastian Andrzej Siewior 					const struct kernel_param *kp)
2021df323337SSebastian Andrzej Siewior {
2022df323337SSebastian Andrzej Siewior 	int error;
2023d4669f0bSJohn Johansen 
2024d4669f0bSJohn Johansen 	if (!apparmor_enabled)
2025d4669f0bSJohn Johansen 		return -EINVAL;
2026ba808cb5SKees Cook 	if (apparmor_initialized)
2027d4669f0bSJohn Johansen 		return -EPERM;
2028d4669f0bSJohn Johansen 
2029d4669f0bSJohn Johansen 	error = param_set_int(val, kp);
2030d4669f0bSJohn Johansen 
2031e3ea1ca5STyler Hicks 	aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
2032e3ea1ca5STyler Hicks 					       AA_MIN_CLEVEL, AA_MAX_CLEVEL);
203332927393SChristoph Hellwig 	pr_info("AppArmor: policy rawdata compression level set to %d\n",
2034e3ea1ca5STyler Hicks 		aa_g_rawdata_compression_level);
203592de220aSJohn Johansen 
2036e3ea1ca5STyler Hicks 	return error;
2037e3ea1ca5STyler Hicks }
2038e3ea1ca5STyler Hicks 
param_get_aacompressionlevel(char * buffer,const struct kernel_param * kp)2039e3ea1ca5STyler Hicks static int param_get_aacompressionlevel(char *buffer,
2040e3ea1ca5STyler Hicks 					const struct kernel_param *kp)
2041e3ea1ca5STyler Hicks {
2042e3ea1ca5STyler Hicks 	if (!apparmor_enabled)
2043e3ea1ca5STyler Hicks 		return -EINVAL;
2044fa9b63adSJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
2045e3ea1ca5STyler Hicks 		return -EPERM;
2046e3ea1ca5STyler Hicks 	return param_get_int(buffer, kp);
2047e3ea1ca5STyler Hicks }
2048e3ea1ca5STyler Hicks 
param_get_debug(char * buffer,const struct kernel_param * kp)2049e3ea1ca5STyler Hicks static int param_get_debug(char *buffer, const struct kernel_param *kp)
2050e3ea1ca5STyler Hicks {
2051e3ea1ca5STyler Hicks 	if (!apparmor_enabled)
2052fa9b63adSJohn Johansen 		return -EINVAL;
2053524d8e14SJohn Johansen 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
2054524d8e14SJohn Johansen 		return -EPERM;
2055524d8e14SJohn Johansen 	return aa_print_debug_params(buffer);
2056524d8e14SJohn Johansen }
2057524d8e14SJohn Johansen 
param_set_debug(const char * val,const struct kernel_param * kp)2058524d8e14SJohn Johansen static int param_set_debug(const char *val, const struct kernel_param *kp)
2059524d8e14SJohn Johansen {
20602d9da9b1SJohn Johansen 	int i;
20612d9da9b1SJohn Johansen 
20622d9da9b1SJohn Johansen 	if (!apparmor_enabled)
20632d9da9b1SJohn Johansen 		return -EINVAL;
20642d9da9b1SJohn Johansen 	if (!val)
20652d9da9b1SJohn Johansen 		return -EINVAL;
20662d9da9b1SJohn Johansen 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
2067e3ea1ca5STyler Hicks 		return -EPERM;
2068e3ea1ca5STyler Hicks 
2069e3ea1ca5STyler Hicks 	i = aa_parse_debug_params(val);
2070e3ea1ca5STyler Hicks 	if (i == DEBUG_PARSE_ERROR)
2071e3ea1ca5STyler Hicks 		return -EINVAL;
207296200952SLuis Chamberlain 
2073e3ea1ca5STyler Hicks 	aa_g_debug = i;
2074e3ea1ca5STyler Hicks 	return 0;
2075e3ea1ca5STyler Hicks }
2076e3ea1ca5STyler Hicks 
param_get_audit(char * buffer,const struct kernel_param * kp)2077e3ea1ca5STyler Hicks static int param_get_audit(char *buffer, const struct kernel_param *kp)
2078e3ea1ca5STyler Hicks {
2079e3ea1ca5STyler Hicks 	if (!apparmor_enabled)
2080e3ea1ca5STyler Hicks 		return -EINVAL;
2081e1af4779SArnd Bergmann 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
2082ab9f2115SMatthew Garrett 		return -EPERM;
2083ab9f2115SMatthew Garrett 	return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
2084ab9f2115SMatthew Garrett }
2085ab9f2115SMatthew Garrett 
param_set_audit(const char * val,const struct kernel_param * kp)2086ab9f2115SMatthew Garrett static int param_set_audit(const char *val, const struct kernel_param *kp)
2087ab9f2115SMatthew Garrett {
2088ab9f2115SMatthew Garrett 	int i;
2089ab9f2115SMatthew Garrett 
2090ab9f2115SMatthew Garrett 	if (!apparmor_enabled)
2091ab9f2115SMatthew Garrett 		return -EINVAL;
2092ab9f2115SMatthew Garrett 	if (!val)
2093ab9f2115SMatthew Garrett 		return -EINVAL;
2094ab9f2115SMatthew Garrett 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
2095ab9f2115SMatthew Garrett 		return -EPERM;
209679ddd4a7SJohn Johansen 
2097ab9f2115SMatthew Garrett 	i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
2098ab9f2115SMatthew Garrett 	if (i < 0)
2099ab9f2115SMatthew Garrett 		return -EINVAL;
2100ab9f2115SMatthew Garrett 
2101ab9f2115SMatthew Garrett 	aa_g_audit = i;
2102ab9f2115SMatthew Garrett 	return 0;
2103ab9f2115SMatthew Garrett }
2104ab9f2115SMatthew Garrett 
param_get_mode(char * buffer,const struct kernel_param * kp)2105ab9f2115SMatthew Garrett static int param_get_mode(char *buffer, const struct kernel_param *kp)
2106ab9f2115SMatthew Garrett {
21077b721124SFlorian Westphal 	if (!apparmor_enabled)
2108ab9f2115SMatthew Garrett 		return -EINVAL;
2109ab9f2115SMatthew Garrett 	if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
2110ab9f2115SMatthew Garrett 		return -EPERM;
2111ab9f2115SMatthew Garrett 
2112ab9f2115SMatthew Garrett 	return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
2113ab9f2115SMatthew Garrett }
21147b721124SFlorian Westphal 
param_set_mode(const char * val,const struct kernel_param * kp)2115ab9f2115SMatthew Garrett static int param_set_mode(const char *val, const struct kernel_param *kp)
2116ab9f2115SMatthew Garrett {
2117ab9f2115SMatthew Garrett 	int i;
2118ab9f2115SMatthew Garrett 
2119ab9f2115SMatthew Garrett 	if (!apparmor_enabled)
2120ab9f2115SMatthew Garrett 		return -EINVAL;
2121ab9f2115SMatthew Garrett 	if (!val)
2122ab9f2115SMatthew Garrett 		return -EINVAL;
2123ab9f2115SMatthew Garrett 	if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
212484117994SMinghao Chi 		return -EPERM;
2125ab9f2115SMatthew Garrett 
2126ab9f2115SMatthew Garrett 	i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
2127ab9f2115SMatthew Garrett 			 val);
2128ab9f2115SMatthew Garrett 	if (i < 0)
2129ab9f2115SMatthew Garrett 		return -EINVAL;
2130ab9f2115SMatthew Garrett 
2131ab9f2115SMatthew Garrett 	aa_g_profile_mode = i;
2132ab9f2115SMatthew Garrett 	return 0;
2133ab9f2115SMatthew Garrett }
2134ab9f2115SMatthew Garrett 
aa_get_buffer(bool in_atomic)2135ab9f2115SMatthew Garrett char *aa_get_buffer(bool in_atomic)
2136ab9f2115SMatthew Garrett {
2137ab9f2115SMatthew Garrett 	union aa_buffer *aa_buf;
2138ab9f2115SMatthew Garrett 	struct aa_local_cache *cache;
2139ab9f2115SMatthew Garrett 	bool try_again = true;
2140ab9f2115SMatthew Garrett 	gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2141ab9f2115SMatthew Garrett 
2142ab9f2115SMatthew Garrett 	/* use per cpu cached buffers first */
2143ab9f2115SMatthew Garrett 	cache = get_cpu_ptr(&aa_local_buffers);
2144ab9f2115SMatthew Garrett 	if (!list_empty(&cache->head)) {
2145ab9f2115SMatthew Garrett 		aa_buf = list_first_entry(&cache->head, union aa_buffer, list);
2146ab9f2115SMatthew Garrett 		list_del(&aa_buf->list);
2147ab9f2115SMatthew Garrett 		cache->hold--;
2148ab9f2115SMatthew Garrett 		cache->count--;
2149ab9f2115SMatthew Garrett 		put_cpu_ptr(&aa_local_buffers);
2150ab9f2115SMatthew Garrett 		return &aa_buf->buffer[0];
2151ab9f2115SMatthew Garrett 	}
2152ab9f2115SMatthew Garrett 	put_cpu_ptr(&aa_local_buffers);
2153e1af4779SArnd Bergmann 
2154ab9f2115SMatthew Garrett 	if (!spin_trylock(&aa_buffers_lock)) {
215598b824ffSJohn Johansen 		cache = get_cpu_ptr(&aa_local_buffers);
215698b824ffSJohn Johansen 		cache->hold += 1;
215798b824ffSJohn Johansen 		put_cpu_ptr(&aa_local_buffers);
2158735ad5d1SJohn Johansen 		spin_lock(&aa_buffers_lock);
215998b824ffSJohn Johansen 	} else {
216098b824ffSJohn Johansen 		cache = get_cpu_ptr(&aa_local_buffers);
216198b824ffSJohn Johansen 		put_cpu_ptr(&aa_local_buffers);
216298b824ffSJohn Johansen 	}
216398b824ffSJohn Johansen retry:
216498b824ffSJohn Johansen 	if (buffer_count > reserve_count ||
216598b824ffSJohn Johansen 	    (in_atomic && !list_empty(&aa_global_buffers))) {
216698b824ffSJohn Johansen 		aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
216798b824ffSJohn Johansen 					  list);
216898b824ffSJohn Johansen 		list_del(&aa_buf->list);
216998b824ffSJohn Johansen 		buffer_count--;
217098b824ffSJohn Johansen 		spin_unlock(&aa_buffers_lock);
217198b824ffSJohn Johansen 		return aa_buf->buffer;
217298b824ffSJohn Johansen 	}
217398b824ffSJohn Johansen 	if (in_atomic) {
217498b824ffSJohn Johansen 		/*
217598b824ffSJohn Johansen 		 * out of reserve buffers and in atomic context so increase
217698b824ffSJohn Johansen 		 * how many buffers to keep in reserve
217798b824ffSJohn Johansen 		 */
217898b824ffSJohn Johansen 		reserve_count++;
217998b824ffSJohn Johansen 		flags = GFP_ATOMIC;
218098b824ffSJohn Johansen 	}
218198b824ffSJohn Johansen 	spin_unlock(&aa_buffers_lock);
218298b824ffSJohn Johansen 
218398b824ffSJohn Johansen 	if (!in_atomic)
218498b824ffSJohn Johansen 		might_sleep();
218598b824ffSJohn Johansen 	aa_buf = kmalloc(aa_g_path_max, flags);
218698b824ffSJohn Johansen 	if (!aa_buf) {
218798b824ffSJohn Johansen 		if (try_again) {
218898b824ffSJohn Johansen 			try_again = false;
218998b824ffSJohn Johansen 			spin_lock(&aa_buffers_lock);
219098b824ffSJohn Johansen 			goto retry;
219198b824ffSJohn Johansen 		}
219298b824ffSJohn Johansen 		pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
219398b824ffSJohn Johansen 		return NULL;
219498b824ffSJohn Johansen 	}
219598b824ffSJohn Johansen 	return aa_buf->buffer;
219698b824ffSJohn Johansen }
219798b824ffSJohn Johansen 
aa_put_buffer(char * buf)219898b824ffSJohn Johansen void aa_put_buffer(char *buf)
219998b824ffSJohn Johansen {
220098b824ffSJohn Johansen 	union aa_buffer *aa_buf;
220198b824ffSJohn Johansen 	struct aa_local_cache *cache;
220298b824ffSJohn Johansen 
220398b824ffSJohn Johansen 	if (!buf)
220498b824ffSJohn Johansen 		return;
220598b824ffSJohn Johansen 	aa_buf = container_of(buf, union aa_buffer, buffer[0]);
220698b824ffSJohn Johansen 
220798b824ffSJohn Johansen 	cache = get_cpu_ptr(&aa_local_buffers);
220898b824ffSJohn Johansen 	if (!cache->hold) {
220998b824ffSJohn Johansen 		put_cpu_ptr(&aa_local_buffers);
221098b824ffSJohn Johansen 
221198b824ffSJohn Johansen 		if (spin_trylock(&aa_buffers_lock)) {
221298b824ffSJohn Johansen 			/* put back on global list */
221398b824ffSJohn Johansen 			list_add(&aa_buf->list, &aa_global_buffers);
221498b824ffSJohn Johansen 			buffer_count++;
221598b824ffSJohn Johansen 			spin_unlock(&aa_buffers_lock);
221698b824ffSJohn Johansen 			cache = get_cpu_ptr(&aa_local_buffers);
221798b824ffSJohn Johansen 			put_cpu_ptr(&aa_local_buffers);
2218b5e95b48SJohn Johansen 			return;
2219b5e95b48SJohn Johansen 		}
2220b5e95b48SJohn Johansen 		/* contention on global list, fallback to percpu */
2221b5e95b48SJohn Johansen 		cache = get_cpu_ptr(&aa_local_buffers);
222211c236b8SJohn Johansen 		cache->hold += 1;
222311c236b8SJohn Johansen 	}
222411c236b8SJohn Johansen 
222511c236b8SJohn Johansen 	/* cache in percpu list */
222611c236b8SJohn Johansen 	list_add(&aa_buf->list, &cache->head);
222711c236b8SJohn Johansen 	cache->count++;
2228b5e95b48SJohn Johansen 	put_cpu_ptr(&aa_local_buffers);
2229b5e95b48SJohn Johansen }
2230b5e95b48SJohn Johansen 
2231b5e95b48SJohn Johansen /*
2232b5e95b48SJohn Johansen  * AppArmor init functions
2233b5e95b48SJohn Johansen  */
2234e3ea1ca5STyler Hicks 
2235e3ea1ca5STyler Hicks /**
2236e3ea1ca5STyler Hicks  * set_init_ctx - set a task context and profile on the first task.
2237e3ea1ca5STyler Hicks  *
2238e3ea1ca5STyler Hicks  * TODO: allow setting an alternate profile than unconfined
2239e3ea1ca5STyler Hicks  */
set_init_ctx(void)2240e3ea1ca5STyler Hicks static int __init set_init_ctx(void)
2241d4669f0bSJohn Johansen {
2242d4669f0bSJohn Johansen 	struct cred *cred = (__force struct cred *)current->real_cred;
2243d4669f0bSJohn Johansen 
2244df323337SSebastian Andrzej Siewior 	set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
2245d4669f0bSJohn Johansen 
2246d4669f0bSJohn Johansen 	return 0;
224755a26ebfSJohn Johansen }
2248b5e95b48SJohn Johansen 
destroy_buffers(void)2249b5e95b48SJohn Johansen static void destroy_buffers(void)
2250b1d9e6b0SCasey Schaufler {
2251d4669f0bSJohn Johansen 	union aa_buffer *aa_buf;
2252b5e95b48SJohn Johansen 
2253d69dece5SCasey Schaufler 	spin_lock(&aa_buffers_lock);
2254f3b8788cSCasey Schaufler 	while (!list_empty(&aa_global_buffers)) {
2255b5e95b48SJohn Johansen 		aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
2256b5e95b48SJohn Johansen 					 list);
2257b5e95b48SJohn Johansen 		list_del(&aa_buf->list);
2258b5e95b48SJohn Johansen 		spin_unlock(&aa_buffers_lock);
2259b5e95b48SJohn Johansen 		kfree(aa_buf);
2260b5e95b48SJohn Johansen 		spin_lock(&aa_buffers_lock);
2261b5e95b48SJohn Johansen 	}
2262b5e95b48SJohn Johansen 	spin_unlock(&aa_buffers_lock);
2263b5e95b48SJohn Johansen }
2264b5e95b48SJohn Johansen 
alloc_buffers(void)2265b5e95b48SJohn Johansen static int __init alloc_buffers(void)
2266b5e95b48SJohn Johansen {
2267d4669f0bSJohn Johansen 	union aa_buffer *aa_buf;
2268d4669f0bSJohn Johansen 	int i, num;
2269b5e95b48SJohn Johansen 
2270b5e95b48SJohn Johansen 	/*
227111c236b8SJohn Johansen 	 * per cpu set of cached allocated buffers used to help reduce
2272b5e95b48SJohn Johansen 	 * lock contention
2273954317feSThomas Meyer 	 */
2274b5e95b48SJohn Johansen 	for_each_possible_cpu(i) {
2275b5e95b48SJohn Johansen 		per_cpu(aa_local_buffers, i).hold = 0;
2276b5e95b48SJohn Johansen 		per_cpu(aa_local_buffers, i).count = 0;
22773d6e5f6dSKees Cook 		INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head);
227807aed2f2SKees Cook 	}
227914bd99c8SKees Cook 	/*
2280c5459b82SKees Cook 	 * A function may require two buffers at once. Usually the buffers are
2281bbd3662aSCasey Schaufler 	 * used for a short period of time and are shared. On UP kernel buffers
22823d6e5f6dSKees Cook 	 * two should be enough, with more CPUs it is possible that more
22833d6e5f6dSKees Cook 	 * buffers will be used simultaneously. The preallocated pool may grow.
2284 	 * This preallocation has also the side-effect that AppArmor will be
2285 	 * disabled early at boot if aa_g_path_max is extremely high.
2286 	 */
2287 	if (num_online_cpus() > 1)
2288 		num = 4 + RESERVE_COUNT;
2289 	else
2290 		num = 2 + RESERVE_COUNT;
2291 
2292 	for (i = 0; i < num; i++) {
2293 
2294 		aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
2295 				 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2296 		if (!aa_buf) {
2297 			destroy_buffers();
2298 			return -ENOMEM;
2299 		}
2300 		aa_put_buffer(aa_buf->buffer);
2301 	}
2302 	return 0;
2303 }
2304 
2305 #ifdef CONFIG_SYSCTL
apparmor_dointvec(const struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)2306 static int apparmor_dointvec(const struct ctl_table *table, int write,
2307 			     void *buffer, size_t *lenp, loff_t *ppos)
2308 {
2309 	if (!aa_current_policy_admin_capable(NULL))
2310 		return -EPERM;
2311 	if (!apparmor_enabled)
2312 		return -EINVAL;
2313 
2314 	return proc_dointvec(table, write, buffer, lenp, ppos);
2315 }
2316 
2317 static const struct ctl_table apparmor_sysctl_table[] = {
2318 #ifdef CONFIG_USER_NS
2319 	{
2320 		.procname       = "unprivileged_userns_apparmor_policy",
2321 		.data           = &unprivileged_userns_apparmor_policy,
2322 		.maxlen         = sizeof(int),
2323 		.mode           = 0600,
2324 		.proc_handler   = apparmor_dointvec,
2325 	},
2326 #endif /* CONFIG_USER_NS */
2327 	{
2328 		.procname       = "apparmor_display_secid_mode",
2329 		.data           = &apparmor_display_secid_mode,
2330 		.maxlen         = sizeof(int),
2331 		.mode           = 0600,
2332 		.proc_handler   = apparmor_dointvec,
2333 	},
2334 	{
2335 		.procname       = "apparmor_restrict_unprivileged_unconfined",
2336 		.data           = &aa_unprivileged_unconfined_restricted,
2337 		.maxlen         = sizeof(int),
2338 		.mode           = 0600,
2339 		.proc_handler   = apparmor_dointvec,
2340 	},
2341 };
2342 
apparmor_init_sysctl(void)2343 static int __init apparmor_init_sysctl(void)
2344 {
2345 	return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
2346 }
2347 #else
apparmor_init_sysctl(void)2348 static inline int apparmor_init_sysctl(void)
2349 {
2350 	return 0;
2351 }
2352 #endif /* CONFIG_SYSCTL */
2353 
2354 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
apparmor_ip_postroute(void * priv,struct sk_buff * skb,const struct nf_hook_state * state)2355 static unsigned int apparmor_ip_postroute(void *priv,
2356 					  struct sk_buff *skb,
2357 					  const struct nf_hook_state *state)
2358 {
2359 	struct aa_sk_ctx *ctx;
2360 	struct sock *sk;
2361 	int error;
2362 
2363 	if (!skb->secmark)
2364 		return NF_ACCEPT;
2365 
2366 	sk = skb_to_full_sk(skb);
2367 	if (sk == NULL)
2368 		return NF_ACCEPT;
2369 
2370 	ctx = aa_sock(sk);
2371 	rcu_read_lock();
2372 	error = apparmor_secmark_check(rcu_dereference(ctx->label), OP_SENDMSG,
2373 				       AA_MAY_SEND, skb->secmark, sk);
2374 	rcu_read_unlock();
2375 	if (!error)
2376 		return NF_ACCEPT;
2377 
2378 	return NF_DROP_ERR(-ECONNREFUSED);
2379 
2380 }
2381 
2382 static const struct nf_hook_ops apparmor_nf_ops[] = {
2383 	{
2384 		.hook =         apparmor_ip_postroute,
2385 		.pf =           NFPROTO_IPV4,
2386 		.hooknum =      NF_INET_POST_ROUTING,
2387 		.priority =     NF_IP_PRI_SELINUX_FIRST,
2388 	},
2389 #if IS_ENABLED(CONFIG_IPV6)
2390 	{
2391 		.hook =         apparmor_ip_postroute,
2392 		.pf =           NFPROTO_IPV6,
2393 		.hooknum =      NF_INET_POST_ROUTING,
2394 		.priority =     NF_IP6_PRI_SELINUX_FIRST,
2395 	},
2396 #endif
2397 };
2398 
apparmor_nf_register(struct net * net)2399 static int __net_init apparmor_nf_register(struct net *net)
2400 {
2401 	return nf_register_net_hooks(net, apparmor_nf_ops,
2402 				    ARRAY_SIZE(apparmor_nf_ops));
2403 }
2404 
apparmor_nf_unregister(struct net * net)2405 static void __net_exit apparmor_nf_unregister(struct net *net)
2406 {
2407 	nf_unregister_net_hooks(net, apparmor_nf_ops,
2408 				ARRAY_SIZE(apparmor_nf_ops));
2409 }
2410 
2411 static struct pernet_operations apparmor_net_ops = {
2412 	.init = apparmor_nf_register,
2413 	.exit = apparmor_nf_unregister,
2414 };
2415 
apparmor_nf_ip_init(void)2416 static int __init apparmor_nf_ip_init(void)
2417 {
2418 	int err;
2419 
2420 	if (!apparmor_enabled)
2421 		return 0;
2422 
2423 	err = register_pernet_subsys(&apparmor_net_ops);
2424 	if (err)
2425 		panic("Apparmor: register_pernet_subsys: error %d\n", err);
2426 
2427 	return 0;
2428 }
2429 __initcall(apparmor_nf_ip_init);
2430 #endif
2431 
2432 static char nulldfa_src[] __aligned(8) = {
2433 	#include "nulldfa.in"
2434 };
2435 static struct aa_dfa *nulldfa;
2436 
2437 static char stacksplitdfa_src[] __aligned(8) = {
2438 	#include "stacksplitdfa.in"
2439 };
2440 struct aa_dfa *stacksplitdfa;
2441 struct aa_policydb *nullpdb;
2442 
aa_setup_dfa_engine(void)2443 static int __init aa_setup_dfa_engine(void)
2444 {
2445 	int error = -ENOMEM;
2446 
2447 	nullpdb = aa_alloc_pdb(GFP_KERNEL);
2448 	if (!nullpdb)
2449 		return -ENOMEM;
2450 
2451 	nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
2452 			    TO_ACCEPT1_FLAG(YYTD_DATA32) |
2453 			    TO_ACCEPT2_FLAG(YYTD_DATA32));
2454 	if (IS_ERR(nulldfa)) {
2455 		error = PTR_ERR(nulldfa);
2456 		goto fail;
2457 	}
2458 	nullpdb->dfa = aa_get_dfa(nulldfa);
2459 	nullpdb->perms = kcalloc(2, sizeof(struct aa_perms), GFP_KERNEL);
2460 	if (!nullpdb->perms)
2461 		goto fail;
2462 	nullpdb->size = 2;
2463 
2464 	stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
2465 				      sizeof(stacksplitdfa_src),
2466 				      TO_ACCEPT1_FLAG(YYTD_DATA32) |
2467 				      TO_ACCEPT2_FLAG(YYTD_DATA32));
2468 	if (IS_ERR(stacksplitdfa)) {
2469 		error = PTR_ERR(stacksplitdfa);
2470 		goto fail;
2471 	}
2472 
2473 	return 0;
2474 
2475 fail:
2476 	aa_put_pdb(nullpdb);
2477 	aa_put_dfa(nulldfa);
2478 	nullpdb = NULL;
2479 	nulldfa = NULL;
2480 	stacksplitdfa = NULL;
2481 
2482 	return error;
2483 }
2484 
aa_teardown_dfa_engine(void)2485 static void __init aa_teardown_dfa_engine(void)
2486 {
2487 	aa_put_dfa(stacksplitdfa);
2488 	aa_put_dfa(nulldfa);
2489 	aa_put_pdb(nullpdb);
2490 	nullpdb = NULL;
2491 	stacksplitdfa = NULL;
2492 	nulldfa = NULL;
2493 }
2494 
apparmor_init(void)2495 static int __init apparmor_init(void)
2496 {
2497 	int error;
2498 
2499 	error = aa_setup_dfa_engine();
2500 	if (error) {
2501 		AA_ERROR("Unable to setup dfa engine\n");
2502 		goto alloc_out;
2503 	}
2504 
2505 	error = aa_alloc_root_ns();
2506 	if (error) {
2507 		AA_ERROR("Unable to allocate default profile namespace\n");
2508 		goto alloc_out;
2509 	}
2510 
2511 	error = apparmor_init_sysctl();
2512 	if (error) {
2513 		AA_ERROR("Unable to register sysctls\n");
2514 		goto alloc_out;
2515 
2516 	}
2517 
2518 	error = alloc_buffers();
2519 	if (error) {
2520 		AA_ERROR("Unable to allocate work buffers\n");
2521 		goto alloc_out;
2522 	}
2523 
2524 	error = set_init_ctx();
2525 	if (error) {
2526 		AA_ERROR("Failed to set context on init task\n");
2527 		aa_free_root_ns();
2528 		goto buffers_out;
2529 	}
2530 	security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
2531 				&apparmor_lsmid);
2532 
2533 	/* Inform the audit system that secctx is used */
2534 	audit_cfg_lsm(&apparmor_lsmid, AUDIT_CFG_LSM_SECCTX_SUBJECT);
2535 
2536 	/* Report that AppArmor successfully initialized */
2537 	apparmor_initialized = 1;
2538 	if (aa_g_profile_mode == APPARMOR_COMPLAIN)
2539 		aa_info_message("AppArmor initialized: complain mode enabled");
2540 	else if (aa_g_profile_mode == APPARMOR_KILL)
2541 		aa_info_message("AppArmor initialized: kill mode enabled");
2542 	else
2543 		aa_info_message("AppArmor initialized");
2544 
2545 	return error;
2546 
2547 buffers_out:
2548 	destroy_buffers();
2549 alloc_out:
2550 	aa_destroy_aafs();
2551 	aa_teardown_dfa_engine();
2552 
2553 	apparmor_enabled = false;
2554 	return error;
2555 }
2556 
2557 DEFINE_LSM(apparmor) = {
2558 	.name = "apparmor",
2559 	.flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
2560 	.enabled = &apparmor_enabled,
2561 	.blobs = &apparmor_blob_sizes,
2562 	.init = apparmor_init,
2563 };
2564