xref: /openbmc/linux/security/apparmor/apparmorfs.c (revision 5ef50d014c59240c2cac38594377583c4e9ea4fa)
163e2b423SJohn Johansen /*
263e2b423SJohn Johansen  * AppArmor security module
363e2b423SJohn Johansen  *
463e2b423SJohn Johansen  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
563e2b423SJohn Johansen  *
663e2b423SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
763e2b423SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
863e2b423SJohn Johansen  *
963e2b423SJohn Johansen  * This program is free software; you can redistribute it and/or
1063e2b423SJohn Johansen  * modify it under the terms of the GNU General Public License as
1163e2b423SJohn Johansen  * published by the Free Software Foundation, version 2 of the
1263e2b423SJohn Johansen  * License.
1363e2b423SJohn Johansen  */
1463e2b423SJohn Johansen 
150d259f04SJohn Johansen #include <linux/ctype.h>
1663e2b423SJohn Johansen #include <linux/security.h>
1763e2b423SJohn Johansen #include <linux/vmalloc.h>
1863e2b423SJohn Johansen #include <linux/module.h>
1963e2b423SJohn Johansen #include <linux/seq_file.h>
2063e2b423SJohn Johansen #include <linux/uaccess.h>
21a71ada30SJohn Johansen #include <linux/mount.h>
2263e2b423SJohn Johansen #include <linux/namei.h>
23e74abcf3SKees Cook #include <linux/capability.h>
2429b3822fSJohn Johansen #include <linux/rcupdate.h>
25a71ada30SJohn Johansen #include <uapi/linux/major.h>
26a71ada30SJohn Johansen #include <linux/fs.h>
2763e2b423SJohn Johansen 
2863e2b423SJohn Johansen #include "include/apparmor.h"
2963e2b423SJohn Johansen #include "include/apparmorfs.h"
3063e2b423SJohn Johansen #include "include/audit.h"
3163e2b423SJohn Johansen #include "include/context.h"
32f8eb8a13SJohn Johansen #include "include/crypto.h"
3363e2b423SJohn Johansen #include "include/policy.h"
34cff281f6SJohn Johansen #include "include/policy_ns.h"
35d384b0a1SKees Cook #include "include/resource.h"
365ac8c355SJohn Johansen #include "include/policy_unpack.h"
3763e2b423SJohn Johansen 
3863e2b423SJohn Johansen /**
390d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
400d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
410d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
420d259f04SJohn Johansen  *
430d259f04SJohn Johansen  * Returns: length of mangled name
440d259f04SJohn Johansen  */
45bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
460d259f04SJohn Johansen {
470d259f04SJohn Johansen 	char *t = target;
480d259f04SJohn Johansen 
490d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
500d259f04SJohn Johansen 		name++;
510d259f04SJohn Johansen 
520d259f04SJohn Johansen 	if (target) {
530d259f04SJohn Johansen 		for (; *name; name++) {
540d259f04SJohn Johansen 			if (*name == '/')
550d259f04SJohn Johansen 				*(t)++ = '.';
560d259f04SJohn Johansen 			else if (isspace(*name))
570d259f04SJohn Johansen 				*(t)++ = '_';
580d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
590d259f04SJohn Johansen 				*(t)++ = *name;
600d259f04SJohn Johansen 		}
610d259f04SJohn Johansen 
620d259f04SJohn Johansen 		*t = 0;
630d259f04SJohn Johansen 	} else {
640d259f04SJohn Johansen 		int len = 0;
650d259f04SJohn Johansen 		for (; *name; name++) {
660d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
670d259f04SJohn Johansen 			    strchr("/._-", *name))
680d259f04SJohn Johansen 				len++;
690d259f04SJohn Johansen 		}
700d259f04SJohn Johansen 
710d259f04SJohn Johansen 		return len;
720d259f04SJohn Johansen 	}
730d259f04SJohn Johansen 
740d259f04SJohn Johansen 	return t - target;
750d259f04SJohn Johansen }
760d259f04SJohn Johansen 
770d259f04SJohn Johansen /**
7863e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
7963e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
803ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
8163e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
8263e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
8363e2b423SJohn Johansen  *
8463e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
8563e2b423SJohn Johansen  *          ERR_PTR on failure.
8663e2b423SJohn Johansen  */
87*5ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
885ac8c355SJohn Johansen 						     size_t alloc_size,
895ac8c355SJohn Johansen 						     size_t copy_size,
9063e2b423SJohn Johansen 						     loff_t *pos)
9163e2b423SJohn Johansen {
925ac8c355SJohn Johansen 	struct aa_loaddata *data;
9363e2b423SJohn Johansen 
943ed02adaSJohn Johansen 	BUG_ON(copy_size > alloc_size);
953ed02adaSJohn Johansen 
9663e2b423SJohn Johansen 	if (*pos != 0)
9763e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
9863e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
9963e2b423SJohn Johansen 
10063e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
1015ac8c355SJohn Johansen 	data = kvmalloc(sizeof(*data) + alloc_size);
10263e2b423SJohn Johansen 	if (data == NULL)
10363e2b423SJohn Johansen 		return ERR_PTR(-ENOMEM);
1045ac8c355SJohn Johansen 	kref_init(&data->count);
1055ac8c355SJohn Johansen 	data->size = copy_size;
1065ac8c355SJohn Johansen 	data->hash = NULL;
1075ac8c355SJohn Johansen 	data->abi = 0;
10863e2b423SJohn Johansen 
1095ac8c355SJohn Johansen 	if (copy_from_user(data->data, userbuf, copy_size)) {
11063e2b423SJohn Johansen 		kvfree(data);
11163e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
11263e2b423SJohn Johansen 	}
11363e2b423SJohn Johansen 
11463e2b423SJohn Johansen 	return data;
11563e2b423SJohn Johansen }
11663e2b423SJohn Johansen 
1175ac8c355SJohn Johansen static ssize_t policy_update(int binop, const char __user *buf, size_t size,
118b7fd2c03SJohn Johansen 			     loff_t *pos, struct aa_ns *ns)
1195ac8c355SJohn Johansen {
1205ac8c355SJohn Johansen 	ssize_t error;
1215ac8c355SJohn Johansen 	struct aa_loaddata *data;
1225ac8c355SJohn Johansen 	struct aa_profile *profile = aa_current_profile();
12347f6e5ccSJohn Johansen 	const char *op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL;
1245ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
1255ac8c355SJohn Johansen 	 * below after unpack
1265ac8c355SJohn Johansen 	 */
127b7fd2c03SJohn Johansen 	error = aa_may_manage_policy(profile, ns, op);
1285ac8c355SJohn Johansen 	if (error)
1295ac8c355SJohn Johansen 		return error;
13063e2b423SJohn Johansen 
131*5ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size, size, pos);
1325ac8c355SJohn Johansen 	error = PTR_ERR(data);
1335ac8c355SJohn Johansen 	if (!IS_ERR(data)) {
134b7fd2c03SJohn Johansen 		error = aa_replace_profiles(ns ? ns : profile->ns, profile,
135b7fd2c03SJohn Johansen 					    binop, data);
1365ac8c355SJohn Johansen 		aa_put_loaddata(data);
1375ac8c355SJohn Johansen 	}
1385ac8c355SJohn Johansen 
1395ac8c355SJohn Johansen 	return error;
1405ac8c355SJohn Johansen }
1415ac8c355SJohn Johansen 
142b7fd2c03SJohn Johansen /* .load file hook fn to load policy */
14363e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
14463e2b423SJohn Johansen 			    loff_t *pos)
14563e2b423SJohn Johansen {
146b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
147b7fd2c03SJohn Johansen 	int error = policy_update(PROF_ADD, buf, size, pos, ns);
148b7fd2c03SJohn Johansen 
149b7fd2c03SJohn Johansen 	aa_put_ns(ns);
15063e2b423SJohn Johansen 
15163e2b423SJohn Johansen 	return error;
15263e2b423SJohn Johansen }
15363e2b423SJohn Johansen 
15463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
1556038f373SArnd Bergmann 	.write = profile_load,
1566038f373SArnd Bergmann 	.llseek = default_llseek,
15763e2b423SJohn Johansen };
15863e2b423SJohn Johansen 
15963e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
16063e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
16163e2b423SJohn Johansen 			       size_t size, loff_t *pos)
16263e2b423SJohn Johansen {
163b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
164b7fd2c03SJohn Johansen 	int error = policy_update(PROF_REPLACE, buf, size, pos, ns);
165b7fd2c03SJohn Johansen 
166b7fd2c03SJohn Johansen 	aa_put_ns(ns);
16763e2b423SJohn Johansen 
16863e2b423SJohn Johansen 	return error;
16963e2b423SJohn Johansen }
17063e2b423SJohn Johansen 
17163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
1726038f373SArnd Bergmann 	.write = profile_replace,
1736038f373SArnd Bergmann 	.llseek = default_llseek,
17463e2b423SJohn Johansen };
17563e2b423SJohn Johansen 
176b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */
17763e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
17863e2b423SJohn Johansen 			      size_t size, loff_t *pos)
17963e2b423SJohn Johansen {
1805ac8c355SJohn Johansen 	struct aa_loaddata *data;
1815ac8c355SJohn Johansen 	struct aa_profile *profile;
18263e2b423SJohn Johansen 	ssize_t error;
183b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
18463e2b423SJohn Johansen 
1855ac8c355SJohn Johansen 	profile = aa_current_profile();
1865ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
1875ac8c355SJohn Johansen 	 * below after unpack
1885ac8c355SJohn Johansen 	 */
189b7fd2c03SJohn Johansen 	error = aa_may_manage_policy(profile, ns, OP_PROF_RM);
1905ac8c355SJohn Johansen 	if (error)
1915ac8c355SJohn Johansen 		goto out;
1925ac8c355SJohn Johansen 
19363e2b423SJohn Johansen 	/*
19463e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
19563e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
19663e2b423SJohn Johansen 	 */
197*5ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
19863e2b423SJohn Johansen 
19963e2b423SJohn Johansen 	error = PTR_ERR(data);
20063e2b423SJohn Johansen 	if (!IS_ERR(data)) {
2015ac8c355SJohn Johansen 		data->data[size] = 0;
202b7fd2c03SJohn Johansen 		error = aa_remove_profiles(ns ? ns : profile->ns, profile,
203b7fd2c03SJohn Johansen 					   data->data, size);
2045ac8c355SJohn Johansen 		aa_put_loaddata(data);
20563e2b423SJohn Johansen 	}
2065ac8c355SJohn Johansen  out:
207b7fd2c03SJohn Johansen 	aa_put_ns(ns);
20863e2b423SJohn Johansen 	return error;
20963e2b423SJohn Johansen }
21063e2b423SJohn Johansen 
21163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
2126038f373SArnd Bergmann 	.write = profile_remove,
2136038f373SArnd Bergmann 	.llseek = default_llseek,
21463e2b423SJohn Johansen };
21563e2b423SJohn Johansen 
216e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v)
217e74abcf3SKees Cook {
218e74abcf3SKees Cook 	struct aa_fs_entry *fs_file = seq->private;
219e74abcf3SKees Cook 
220e74abcf3SKees Cook 	if (!fs_file)
221e74abcf3SKees Cook 		return 0;
222e74abcf3SKees Cook 
223e74abcf3SKees Cook 	switch (fs_file->v_type) {
224e74abcf3SKees Cook 	case AA_FS_TYPE_BOOLEAN:
225e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
226e74abcf3SKees Cook 		break;
227a9bf8e9fSKees Cook 	case AA_FS_TYPE_STRING:
228a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
229a9bf8e9fSKees Cook 		break;
230e74abcf3SKees Cook 	case AA_FS_TYPE_U64:
231e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
232e74abcf3SKees Cook 		break;
233e74abcf3SKees Cook 	default:
234e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
235e74abcf3SKees Cook 		break;
236e74abcf3SKees Cook 	}
237e74abcf3SKees Cook 
238e74abcf3SKees Cook 	return 0;
239e74abcf3SKees Cook }
240e74abcf3SKees Cook 
241e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file)
242e74abcf3SKees Cook {
243e74abcf3SKees Cook 	return single_open(file, aa_fs_seq_show, inode->i_private);
244e74abcf3SKees Cook }
245e74abcf3SKees Cook 
246e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = {
247e74abcf3SKees Cook 	.owner		= THIS_MODULE,
248e74abcf3SKees Cook 	.open		= aa_fs_seq_open,
249e74abcf3SKees Cook 	.read		= seq_read,
250e74abcf3SKees Cook 	.llseek		= seq_lseek,
251e74abcf3SKees Cook 	.release	= single_release,
252e74abcf3SKees Cook };
253e74abcf3SKees Cook 
2540d259f04SJohn Johansen static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
2550d259f04SJohn Johansen 				  int (*show)(struct seq_file *, void *))
2560d259f04SJohn Johansen {
2578399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
2588399588aSJohn Johansen 	int error = single_open(file, show, proxy);
25963e2b423SJohn Johansen 
2600d259f04SJohn Johansen 	if (error) {
2610d259f04SJohn Johansen 		file->private_data = NULL;
2628399588aSJohn Johansen 		aa_put_proxy(proxy);
2630d259f04SJohn Johansen 	}
2640d259f04SJohn Johansen 
2650d259f04SJohn Johansen 	return error;
2660d259f04SJohn Johansen }
2670d259f04SJohn Johansen 
2680d259f04SJohn Johansen static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
2690d259f04SJohn Johansen {
2700d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
2710d259f04SJohn Johansen 	if (seq)
2728399588aSJohn Johansen 		aa_put_proxy(seq->private);
2730d259f04SJohn Johansen 	return single_release(inode, file);
2740d259f04SJohn Johansen }
2750d259f04SJohn Johansen 
2760d259f04SJohn Johansen static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
2770d259f04SJohn Johansen {
2788399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
2798399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
2800d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
2810d259f04SJohn Johansen 	aa_put_profile(profile);
2820d259f04SJohn Johansen 
2830d259f04SJohn Johansen 	return 0;
2840d259f04SJohn Johansen }
2850d259f04SJohn Johansen 
2860d259f04SJohn Johansen static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
2870d259f04SJohn Johansen {
2880d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
2890d259f04SJohn Johansen }
2900d259f04SJohn Johansen 
2910d259f04SJohn Johansen static const struct file_operations aa_fs_profname_fops = {
2920d259f04SJohn Johansen 	.owner		= THIS_MODULE,
2930d259f04SJohn Johansen 	.open		= aa_fs_seq_profname_open,
2940d259f04SJohn Johansen 	.read		= seq_read,
2950d259f04SJohn Johansen 	.llseek		= seq_lseek,
2960d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
2970d259f04SJohn Johansen };
2980d259f04SJohn Johansen 
2990d259f04SJohn Johansen static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
3000d259f04SJohn Johansen {
3018399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3028399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
3030d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
3040d259f04SJohn Johansen 	aa_put_profile(profile);
3050d259f04SJohn Johansen 
3060d259f04SJohn Johansen 	return 0;
3070d259f04SJohn Johansen }
3080d259f04SJohn Johansen 
3090d259f04SJohn Johansen static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
3100d259f04SJohn Johansen {
3110d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
3120d259f04SJohn Johansen }
3130d259f04SJohn Johansen 
3140d259f04SJohn Johansen static const struct file_operations aa_fs_profmode_fops = {
3150d259f04SJohn Johansen 	.owner		= THIS_MODULE,
3160d259f04SJohn Johansen 	.open		= aa_fs_seq_profmode_open,
3170d259f04SJohn Johansen 	.read		= seq_read,
3180d259f04SJohn Johansen 	.llseek		= seq_lseek,
3190d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
3200d259f04SJohn Johansen };
3210d259f04SJohn Johansen 
322556d0be7SJohn Johansen static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
323556d0be7SJohn Johansen {
3248399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3258399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
326556d0be7SJohn Johansen 	if (profile->attach)
327556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
328556d0be7SJohn Johansen 	else if (profile->xmatch)
329556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
330556d0be7SJohn Johansen 	else
331556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
332556d0be7SJohn Johansen 	aa_put_profile(profile);
333556d0be7SJohn Johansen 
334556d0be7SJohn Johansen 	return 0;
335556d0be7SJohn Johansen }
336556d0be7SJohn Johansen 
337556d0be7SJohn Johansen static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
338556d0be7SJohn Johansen {
339556d0be7SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
340556d0be7SJohn Johansen }
341556d0be7SJohn Johansen 
342556d0be7SJohn Johansen static const struct file_operations aa_fs_profattach_fops = {
343556d0be7SJohn Johansen 	.owner		= THIS_MODULE,
344556d0be7SJohn Johansen 	.open		= aa_fs_seq_profattach_open,
345556d0be7SJohn Johansen 	.read		= seq_read,
346556d0be7SJohn Johansen 	.llseek		= seq_lseek,
347556d0be7SJohn Johansen 	.release	= aa_fs_seq_profile_release,
348556d0be7SJohn Johansen };
349556d0be7SJohn Johansen 
350f8eb8a13SJohn Johansen static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
351f8eb8a13SJohn Johansen {
3528399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3538399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
354f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
355f8eb8a13SJohn Johansen 
356f8eb8a13SJohn Johansen 	if (profile->hash) {
357f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
358f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
359f8eb8a13SJohn Johansen 		seq_puts(seq, "\n");
360f8eb8a13SJohn Johansen 	}
3610b938a2eSJohn Johansen 	aa_put_profile(profile);
362f8eb8a13SJohn Johansen 
363f8eb8a13SJohn Johansen 	return 0;
364f8eb8a13SJohn Johansen }
365f8eb8a13SJohn Johansen 
366f8eb8a13SJohn Johansen static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
367f8eb8a13SJohn Johansen {
368f8eb8a13SJohn Johansen 	return single_open(file, aa_fs_seq_hash_show, inode->i_private);
369f8eb8a13SJohn Johansen }
370f8eb8a13SJohn Johansen 
371f8eb8a13SJohn Johansen static const struct file_operations aa_fs_seq_hash_fops = {
372f8eb8a13SJohn Johansen 	.owner		= THIS_MODULE,
373f8eb8a13SJohn Johansen 	.open		= aa_fs_seq_hash_open,
374f8eb8a13SJohn Johansen 	.read		= seq_read,
375f8eb8a13SJohn Johansen 	.llseek		= seq_lseek,
376f8eb8a13SJohn Johansen 	.release	= single_release,
377f8eb8a13SJohn Johansen };
378f8eb8a13SJohn Johansen 
3793e3e5695SJohn Johansen 
380a71ada30SJohn Johansen static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v)
381a71ada30SJohn Johansen {
382a71ada30SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
383a71ada30SJohn Johansen 
384a71ada30SJohn Johansen 	seq_printf(seq, "%d\n", ns->level);
385a71ada30SJohn Johansen 
386a71ada30SJohn Johansen 	return 0;
387a71ada30SJohn Johansen }
388a71ada30SJohn Johansen 
389a71ada30SJohn Johansen static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file)
390a71ada30SJohn Johansen {
391a71ada30SJohn Johansen 	return single_open(file, aa_fs_seq_show_ns_level, inode->i_private);
392a71ada30SJohn Johansen }
393a71ada30SJohn Johansen 
394a71ada30SJohn Johansen static const struct file_operations aa_fs_ns_level = {
395a71ada30SJohn Johansen 	.owner		= THIS_MODULE,
396a71ada30SJohn Johansen 	.open		= aa_fs_seq_open_ns_level,
397a71ada30SJohn Johansen 	.read		= seq_read,
398a71ada30SJohn Johansen 	.llseek		= seq_lseek,
399a71ada30SJohn Johansen 	.release	= single_release,
400a71ada30SJohn Johansen };
401a71ada30SJohn Johansen 
4023e3e5695SJohn Johansen static int aa_fs_seq_show_ns_name(struct seq_file *seq, void *v)
4033e3e5695SJohn Johansen {
4043e3e5695SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
4053e3e5695SJohn Johansen 
4063e3e5695SJohn Johansen 	seq_printf(seq, "%s\n", ns->base.name);
4073e3e5695SJohn Johansen 
4083e3e5695SJohn Johansen 	return 0;
4093e3e5695SJohn Johansen }
4103e3e5695SJohn Johansen 
4113e3e5695SJohn Johansen static int aa_fs_seq_open_ns_name(struct inode *inode, struct file *file)
4123e3e5695SJohn Johansen {
4133e3e5695SJohn Johansen 	return single_open(file, aa_fs_seq_show_ns_name, inode->i_private);
4143e3e5695SJohn Johansen }
4153e3e5695SJohn Johansen 
4163e3e5695SJohn Johansen static const struct file_operations aa_fs_ns_name = {
4173e3e5695SJohn Johansen 	.owner		= THIS_MODULE,
4183e3e5695SJohn Johansen 	.open		= aa_fs_seq_open_ns_name,
4193e3e5695SJohn Johansen 	.read		= seq_read,
4203e3e5695SJohn Johansen 	.llseek		= seq_lseek,
4213e3e5695SJohn Johansen 	.release	= single_release,
4223e3e5695SJohn Johansen };
4233e3e5695SJohn Johansen 
4245ac8c355SJohn Johansen static int rawdata_release(struct inode *inode, struct file *file)
4255ac8c355SJohn Johansen {
4265ac8c355SJohn Johansen 	/* TODO: switch to loaddata when profile switched to symlink */
4275ac8c355SJohn Johansen 	aa_put_loaddata(file->private_data);
4285ac8c355SJohn Johansen 
4295ac8c355SJohn Johansen 	return 0;
4305ac8c355SJohn Johansen }
4315ac8c355SJohn Johansen 
4325ac8c355SJohn Johansen static int aa_fs_seq_raw_abi_show(struct seq_file *seq, void *v)
4335ac8c355SJohn Johansen {
4345ac8c355SJohn Johansen 	struct aa_proxy *proxy = seq->private;
4355ac8c355SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
4365ac8c355SJohn Johansen 
4375ac8c355SJohn Johansen 	if (profile->rawdata->abi) {
4385ac8c355SJohn Johansen 		seq_printf(seq, "v%d", profile->rawdata->abi);
4395ac8c355SJohn Johansen 		seq_puts(seq, "\n");
4405ac8c355SJohn Johansen 	}
4415ac8c355SJohn Johansen 	aa_put_profile(profile);
4425ac8c355SJohn Johansen 
4435ac8c355SJohn Johansen 	return 0;
4445ac8c355SJohn Johansen }
4455ac8c355SJohn Johansen 
4465ac8c355SJohn Johansen static int aa_fs_seq_raw_abi_open(struct inode *inode, struct file *file)
4475ac8c355SJohn Johansen {
4485ac8c355SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_abi_show);
4495ac8c355SJohn Johansen }
4505ac8c355SJohn Johansen 
4515ac8c355SJohn Johansen static const struct file_operations aa_fs_seq_raw_abi_fops = {
4525ac8c355SJohn Johansen 	.owner		= THIS_MODULE,
4535ac8c355SJohn Johansen 	.open		= aa_fs_seq_raw_abi_open,
4545ac8c355SJohn Johansen 	.read		= seq_read,
4555ac8c355SJohn Johansen 	.llseek		= seq_lseek,
4565ac8c355SJohn Johansen 	.release	= aa_fs_seq_profile_release,
4575ac8c355SJohn Johansen };
4585ac8c355SJohn Johansen 
4595ac8c355SJohn Johansen static int aa_fs_seq_raw_hash_show(struct seq_file *seq, void *v)
4605ac8c355SJohn Johansen {
4615ac8c355SJohn Johansen 	struct aa_proxy *proxy = seq->private;
4625ac8c355SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
4635ac8c355SJohn Johansen 	unsigned int i, size = aa_hash_size();
4645ac8c355SJohn Johansen 
4655ac8c355SJohn Johansen 	if (profile->rawdata->hash) {
4665ac8c355SJohn Johansen 		for (i = 0; i < size; i++)
4675ac8c355SJohn Johansen 			seq_printf(seq, "%.2x", profile->rawdata->hash[i]);
4685ac8c355SJohn Johansen 		seq_puts(seq, "\n");
4695ac8c355SJohn Johansen 	}
4705ac8c355SJohn Johansen 	aa_put_profile(profile);
4715ac8c355SJohn Johansen 
4725ac8c355SJohn Johansen 	return 0;
4735ac8c355SJohn Johansen }
4745ac8c355SJohn Johansen 
4755ac8c355SJohn Johansen static int aa_fs_seq_raw_hash_open(struct inode *inode, struct file *file)
4765ac8c355SJohn Johansen {
4775ac8c355SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_hash_show);
4785ac8c355SJohn Johansen }
4795ac8c355SJohn Johansen 
4805ac8c355SJohn Johansen static const struct file_operations aa_fs_seq_raw_hash_fops = {
4815ac8c355SJohn Johansen 	.owner		= THIS_MODULE,
4825ac8c355SJohn Johansen 	.open		= aa_fs_seq_raw_hash_open,
4835ac8c355SJohn Johansen 	.read		= seq_read,
4845ac8c355SJohn Johansen 	.llseek		= seq_lseek,
4855ac8c355SJohn Johansen 	.release	= aa_fs_seq_profile_release,
4865ac8c355SJohn Johansen };
4875ac8c355SJohn Johansen 
4885ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
4895ac8c355SJohn Johansen 			    loff_t *ppos)
4905ac8c355SJohn Johansen {
4915ac8c355SJohn Johansen 	struct aa_loaddata *rawdata = file->private_data;
4925ac8c355SJohn Johansen 
4935ac8c355SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, rawdata->data,
4945ac8c355SJohn Johansen 				       rawdata->size);
4955ac8c355SJohn Johansen }
4965ac8c355SJohn Johansen 
4975ac8c355SJohn Johansen static int rawdata_open(struct inode *inode, struct file *file)
4985ac8c355SJohn Johansen {
4995ac8c355SJohn Johansen 	struct aa_proxy *proxy = inode->i_private;
5005ac8c355SJohn Johansen 	struct aa_profile *profile;
5015ac8c355SJohn Johansen 
5025ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
5035ac8c355SJohn Johansen 		return -EACCES;
5045ac8c355SJohn Johansen 	profile = aa_get_profile_rcu(&proxy->profile);
5055ac8c355SJohn Johansen 	file->private_data = aa_get_loaddata(profile->rawdata);
5065ac8c355SJohn Johansen 	aa_put_profile(profile);
5075ac8c355SJohn Johansen 
5085ac8c355SJohn Johansen 	return 0;
5095ac8c355SJohn Johansen }
5105ac8c355SJohn Johansen 
5115ac8c355SJohn Johansen static const struct file_operations aa_fs_rawdata_fops = {
5125ac8c355SJohn Johansen 	.open = rawdata_open,
5135ac8c355SJohn Johansen 	.read = rawdata_read,
5145ac8c355SJohn Johansen 	.llseek = generic_file_llseek,
5155ac8c355SJohn Johansen 	.release = rawdata_release,
5165ac8c355SJohn Johansen };
5175ac8c355SJohn Johansen 
5180d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
5190d259f04SJohn Johansen void __aa_fs_profile_rmdir(struct aa_profile *profile)
5200d259f04SJohn Johansen {
5210d259f04SJohn Johansen 	struct aa_profile *child;
5220d259f04SJohn Johansen 	int i;
5230d259f04SJohn Johansen 
5240d259f04SJohn Johansen 	if (!profile)
5250d259f04SJohn Johansen 		return;
5260d259f04SJohn Johansen 
5270d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
5280d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
5290d259f04SJohn Johansen 
5300d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
5318399588aSJohn Johansen 		struct aa_proxy *proxy;
5320d259f04SJohn Johansen 		if (!profile->dents[i])
5330d259f04SJohn Johansen 			continue;
5340d259f04SJohn Johansen 
5358399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
5360d259f04SJohn Johansen 		securityfs_remove(profile->dents[i]);
5378399588aSJohn Johansen 		aa_put_proxy(proxy);
5380d259f04SJohn Johansen 		profile->dents[i] = NULL;
5390d259f04SJohn Johansen 	}
5400d259f04SJohn Johansen }
5410d259f04SJohn Johansen 
5420d259f04SJohn Johansen void __aa_fs_profile_migrate_dents(struct aa_profile *old,
5430d259f04SJohn Johansen 				   struct aa_profile *new)
5440d259f04SJohn Johansen {
5450d259f04SJohn Johansen 	int i;
5460d259f04SJohn Johansen 
5470d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
5480d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
549d671e890SJohn Johansen 		if (new->dents[i])
550078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
5510d259f04SJohn Johansen 		old->dents[i] = NULL;
5520d259f04SJohn Johansen 	}
5530d259f04SJohn Johansen }
5540d259f04SJohn Johansen 
5550d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
5560d259f04SJohn Johansen 					  struct aa_profile *profile,
5570d259f04SJohn Johansen 					  const struct file_operations *fops)
5580d259f04SJohn Johansen {
5598399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
5600d259f04SJohn Johansen 	struct dentry *dent;
5610d259f04SJohn Johansen 
5628399588aSJohn Johansen 	dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
5630d259f04SJohn Johansen 	if (IS_ERR(dent))
5648399588aSJohn Johansen 		aa_put_proxy(proxy);
5650d259f04SJohn Johansen 
5660d259f04SJohn Johansen 	return dent;
5670d259f04SJohn Johansen }
5680d259f04SJohn Johansen 
5690d259f04SJohn Johansen /* requires lock be held */
5700d259f04SJohn Johansen int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
5710d259f04SJohn Johansen {
5720d259f04SJohn Johansen 	struct aa_profile *child;
5730d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
5740d259f04SJohn Johansen 	int error;
5750d259f04SJohn Johansen 
5760d259f04SJohn Johansen 	if (!parent) {
5770d259f04SJohn Johansen 		struct aa_profile *p;
5780d259f04SJohn Johansen 		p = aa_deref_parent(profile);
5790d259f04SJohn Johansen 		dent = prof_dir(p);
5800d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
5810d259f04SJohn Johansen 		dent = securityfs_create_dir("profiles", dent);
5820d259f04SJohn Johansen 		if (IS_ERR(dent))
5830d259f04SJohn Johansen 			goto fail;
5840d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
5850d259f04SJohn Johansen 	}
5860d259f04SJohn Johansen 
5870d259f04SJohn Johansen 	if (!profile->dirname) {
5880d259f04SJohn Johansen 		int len, id_len;
5890d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
5900d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
5910d259f04SJohn Johansen 
5920d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
5930d259f04SJohn Johansen 		if (!profile->dirname)
5940d259f04SJohn Johansen 			goto fail;
5950d259f04SJohn Johansen 
5960d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
5970d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
5980d259f04SJohn Johansen 	}
5990d259f04SJohn Johansen 
6000d259f04SJohn Johansen 	dent = securityfs_create_dir(profile->dirname, parent);
6010d259f04SJohn Johansen 	if (IS_ERR(dent))
6020d259f04SJohn Johansen 		goto fail;
6030d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
6040d259f04SJohn Johansen 
6050d259f04SJohn Johansen 	dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
6060d259f04SJohn Johansen 	if (IS_ERR(dent))
6070d259f04SJohn Johansen 		goto fail;
6080d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
6090d259f04SJohn Johansen 
6100d259f04SJohn Johansen 	dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
6110d259f04SJohn Johansen 	if (IS_ERR(dent))
6120d259f04SJohn Johansen 		goto fail;
6130d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
6140d259f04SJohn Johansen 
615556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
616556d0be7SJohn Johansen 				   &aa_fs_profattach_fops);
617556d0be7SJohn Johansen 	if (IS_ERR(dent))
618556d0be7SJohn Johansen 		goto fail;
619556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
620556d0be7SJohn Johansen 
621f8eb8a13SJohn Johansen 	if (profile->hash) {
622f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
623f8eb8a13SJohn Johansen 					   &aa_fs_seq_hash_fops);
624f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
625f8eb8a13SJohn Johansen 			goto fail;
626f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
627f8eb8a13SJohn Johansen 	}
628f8eb8a13SJohn Johansen 
6295ac8c355SJohn Johansen 	if (profile->rawdata) {
6305ac8c355SJohn Johansen 		dent = create_profile_file(dir, "raw_sha1", profile,
6315ac8c355SJohn Johansen 					   &aa_fs_seq_raw_hash_fops);
6325ac8c355SJohn Johansen 		if (IS_ERR(dent))
6335ac8c355SJohn Johansen 			goto fail;
6345ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
6355ac8c355SJohn Johansen 
6365ac8c355SJohn Johansen 		dent = create_profile_file(dir, "raw_abi", profile,
6375ac8c355SJohn Johansen 					   &aa_fs_seq_raw_abi_fops);
6385ac8c355SJohn Johansen 		if (IS_ERR(dent))
6395ac8c355SJohn Johansen 			goto fail;
6405ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
6415ac8c355SJohn Johansen 
6425ac8c355SJohn Johansen 		dent = securityfs_create_file("raw_data", S_IFREG | 0444, dir,
6435ac8c355SJohn Johansen 					      profile->proxy,
6445ac8c355SJohn Johansen 					      &aa_fs_rawdata_fops);
6455ac8c355SJohn Johansen 		if (IS_ERR(dent))
6465ac8c355SJohn Johansen 			goto fail;
6475ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
6485ac8c355SJohn Johansen 		d_inode(dent)->i_size = profile->rawdata->size;
6495ac8c355SJohn Johansen 		aa_get_proxy(profile->proxy);
6505ac8c355SJohn Johansen 	}
6515ac8c355SJohn Johansen 
6520d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
6530d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
6540d259f04SJohn Johansen 		if (error)
6550d259f04SJohn Johansen 			goto fail2;
6560d259f04SJohn Johansen 	}
6570d259f04SJohn Johansen 
6580d259f04SJohn Johansen 	return 0;
6590d259f04SJohn Johansen 
6600d259f04SJohn Johansen fail:
6610d259f04SJohn Johansen 	error = PTR_ERR(dent);
6620d259f04SJohn Johansen 
6630d259f04SJohn Johansen fail2:
6640d259f04SJohn Johansen 	__aa_fs_profile_rmdir(profile);
6650d259f04SJohn Johansen 
6660d259f04SJohn Johansen 	return error;
6670d259f04SJohn Johansen }
6680d259f04SJohn Johansen 
66998849dffSJohn Johansen void __aa_fs_ns_rmdir(struct aa_ns *ns)
6700d259f04SJohn Johansen {
67198849dffSJohn Johansen 	struct aa_ns *sub;
6720d259f04SJohn Johansen 	struct aa_profile *child;
6730d259f04SJohn Johansen 	int i;
6740d259f04SJohn Johansen 
6750d259f04SJohn Johansen 	if (!ns)
6760d259f04SJohn Johansen 		return;
6770d259f04SJohn Johansen 
6780d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
6790d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
6800d259f04SJohn Johansen 
6810d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
6820d259f04SJohn Johansen 		mutex_lock(&sub->lock);
68398849dffSJohn Johansen 		__aa_fs_ns_rmdir(sub);
6840d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
6850d259f04SJohn Johansen 	}
6860d259f04SJohn Johansen 
687b7fd2c03SJohn Johansen 	if (ns_subns_dir(ns)) {
688b7fd2c03SJohn Johansen 		sub = d_inode(ns_subns_dir(ns))->i_private;
689b7fd2c03SJohn Johansen 		aa_put_ns(sub);
690b7fd2c03SJohn Johansen 	}
691b7fd2c03SJohn Johansen 	if (ns_subload(ns)) {
692b7fd2c03SJohn Johansen 		sub = d_inode(ns_subload(ns))->i_private;
693b7fd2c03SJohn Johansen 		aa_put_ns(sub);
694b7fd2c03SJohn Johansen 	}
695b7fd2c03SJohn Johansen 	if (ns_subreplace(ns)) {
696b7fd2c03SJohn Johansen 		sub = d_inode(ns_subreplace(ns))->i_private;
697b7fd2c03SJohn Johansen 		aa_put_ns(sub);
698b7fd2c03SJohn Johansen 	}
699b7fd2c03SJohn Johansen 	if (ns_subremove(ns)) {
700b7fd2c03SJohn Johansen 		sub = d_inode(ns_subremove(ns))->i_private;
701b7fd2c03SJohn Johansen 		aa_put_ns(sub);
702b7fd2c03SJohn Johansen 	}
703b7fd2c03SJohn Johansen 
7040d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
7050d259f04SJohn Johansen 		securityfs_remove(ns->dents[i]);
7060d259f04SJohn Johansen 		ns->dents[i] = NULL;
7070d259f04SJohn Johansen 	}
7080d259f04SJohn Johansen }
7090d259f04SJohn Johansen 
710b7fd2c03SJohn Johansen /* assumes cleanup in caller */
711b7fd2c03SJohn Johansen static int __aa_fs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
712b7fd2c03SJohn Johansen {
713b7fd2c03SJohn Johansen 	struct dentry *dent;
714b7fd2c03SJohn Johansen 
715b7fd2c03SJohn Johansen 	AA_BUG(!ns);
716b7fd2c03SJohn Johansen 	AA_BUG(!dir);
717b7fd2c03SJohn Johansen 
718b7fd2c03SJohn Johansen 	dent = securityfs_create_dir("profiles", dir);
719b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
720b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
721b7fd2c03SJohn Johansen 	ns_subprofs_dir(ns) = dent;
722b7fd2c03SJohn Johansen 
723b7fd2c03SJohn Johansen 	dent = securityfs_create_dir("raw_data", dir);
724b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
725b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
726b7fd2c03SJohn Johansen 	ns_subdata_dir(ns) = dent;
727b7fd2c03SJohn Johansen 
728b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".load", 0640, dir, ns,
729b7fd2c03SJohn Johansen 				      &aa_fs_profile_load);
730b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
731b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
732b7fd2c03SJohn Johansen 	aa_get_ns(ns);
733b7fd2c03SJohn Johansen 	ns_subload(ns) = dent;
734b7fd2c03SJohn Johansen 
735b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".replace", 0640, dir, ns,
736b7fd2c03SJohn Johansen 				      &aa_fs_profile_replace);
737b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
738b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
739b7fd2c03SJohn Johansen 	aa_get_ns(ns);
740b7fd2c03SJohn Johansen 	ns_subreplace(ns) = dent;
741b7fd2c03SJohn Johansen 
742b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".remove", 0640, dir, ns,
743b7fd2c03SJohn Johansen 				      &aa_fs_profile_remove);
744b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
745b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
746b7fd2c03SJohn Johansen 	aa_get_ns(ns);
747b7fd2c03SJohn Johansen 	ns_subremove(ns) = dent;
748b7fd2c03SJohn Johansen 
749b7fd2c03SJohn Johansen 	dent = securityfs_create_dir("namespaces", dir);
750b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
751b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
752b7fd2c03SJohn Johansen 	aa_get_ns(ns);
753b7fd2c03SJohn Johansen 	ns_subns_dir(ns) = dent;
754b7fd2c03SJohn Johansen 
755b7fd2c03SJohn Johansen 	return 0;
756b7fd2c03SJohn Johansen }
757b7fd2c03SJohn Johansen 
75898849dffSJohn Johansen int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
7590d259f04SJohn Johansen {
76098849dffSJohn Johansen 	struct aa_ns *sub;
7610d259f04SJohn Johansen 	struct aa_profile *child;
7620d259f04SJohn Johansen 	struct dentry *dent, *dir;
7630d259f04SJohn Johansen 	int error;
7640d259f04SJohn Johansen 
765b7fd2c03SJohn Johansen 	AA_BUG(!ns);
766b7fd2c03SJohn Johansen 	AA_BUG(!parent);
767b7fd2c03SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
768b7fd2c03SJohn Johansen 
7690d259f04SJohn Johansen 	if (!name)
7700d259f04SJohn Johansen 		name = ns->base.name;
7710d259f04SJohn Johansen 
772b7fd2c03SJohn Johansen 	/* create ns dir if it doesn't already exist */
7730d259f04SJohn Johansen 	dent = securityfs_create_dir(name, parent);
7740d259f04SJohn Johansen 	if (IS_ERR(dent))
7750d259f04SJohn Johansen 		goto fail;
776b7fd2c03SJohn Johansen 
7770d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
778b7fd2c03SJohn Johansen 	error = __aa_fs_ns_mkdir_entries(ns, dir);
779b7fd2c03SJohn Johansen 	if (error)
780b7fd2c03SJohn Johansen 		goto fail2;
7810d259f04SJohn Johansen 
782b7fd2c03SJohn Johansen 	/* profiles */
7830d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
7840d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
7850d259f04SJohn Johansen 		if (error)
7860d259f04SJohn Johansen 			goto fail2;
7870d259f04SJohn Johansen 	}
7880d259f04SJohn Johansen 
789b7fd2c03SJohn Johansen 	/* subnamespaces */
7900d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
7910d259f04SJohn Johansen 		mutex_lock(&sub->lock);
79298849dffSJohn Johansen 		error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
7930d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
7940d259f04SJohn Johansen 		if (error)
7950d259f04SJohn Johansen 			goto fail2;
7960d259f04SJohn Johansen 	}
7970d259f04SJohn Johansen 
7980d259f04SJohn Johansen 	return 0;
7990d259f04SJohn Johansen 
8000d259f04SJohn Johansen fail:
8010d259f04SJohn Johansen 	error = PTR_ERR(dent);
8020d259f04SJohn Johansen 
8030d259f04SJohn Johansen fail2:
80498849dffSJohn Johansen 	__aa_fs_ns_rmdir(ns);
8050d259f04SJohn Johansen 
8060d259f04SJohn Johansen 	return error;
8070d259f04SJohn Johansen }
8080d259f04SJohn Johansen 
8090d259f04SJohn Johansen 
81029b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
81129b3822fSJohn Johansen 
81229b3822fSJohn Johansen /**
81398849dffSJohn Johansen  * __next_ns - find the next namespace to list
81429b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
81529b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
81629b3822fSJohn Johansen  *
81729b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
81829b3822fSJohn Johansen  * while switching current namespace.
81929b3822fSJohn Johansen  *
82029b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
82129b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
82229b3822fSJohn Johansen  * NOTE: will not unlock root->lock
82329b3822fSJohn Johansen  */
82498849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
82529b3822fSJohn Johansen {
82698849dffSJohn Johansen 	struct aa_ns *parent, *next;
82729b3822fSJohn Johansen 
82829b3822fSJohn Johansen 	/* is next namespace a child */
82929b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
83029b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
83129b3822fSJohn Johansen 		mutex_lock(&next->lock);
83229b3822fSJohn Johansen 		return next;
83329b3822fSJohn Johansen 	}
83429b3822fSJohn Johansen 
83529b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
83629b3822fSJohn Johansen 	parent = ns->parent;
837ed2c7da3SJohn Johansen 	while (ns != root) {
83829b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
83938dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
84029b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
84129b3822fSJohn Johansen 			mutex_lock(&next->lock);
84229b3822fSJohn Johansen 			return next;
84329b3822fSJohn Johansen 		}
84429b3822fSJohn Johansen 		ns = parent;
84529b3822fSJohn Johansen 		parent = parent->parent;
84629b3822fSJohn Johansen 	}
84729b3822fSJohn Johansen 
84829b3822fSJohn Johansen 	return NULL;
84929b3822fSJohn Johansen }
85029b3822fSJohn Johansen 
85129b3822fSJohn Johansen /**
85229b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
85329b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
85429b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
85529b3822fSJohn Johansen  *
85629b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
85729b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
85829b3822fSJohn Johansen  */
85998849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
86098849dffSJohn Johansen 					  struct aa_ns *ns)
86129b3822fSJohn Johansen {
86298849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
86329b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
86429b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
86529b3822fSJohn Johansen 						struct aa_profile, base.list);
86629b3822fSJohn Johansen 	}
86729b3822fSJohn Johansen 	return NULL;
86829b3822fSJohn Johansen }
86929b3822fSJohn Johansen 
87029b3822fSJohn Johansen /**
87129b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
87229b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
87329b3822fSJohn Johansen  *
87429b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
87529b3822fSJohn Johansen  *
87629b3822fSJohn Johansen  * Returns: next profile or NULL if done
87729b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
87829b3822fSJohn Johansen  */
87929b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
88029b3822fSJohn Johansen {
88129b3822fSJohn Johansen 	struct aa_profile *parent;
88298849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
88329b3822fSJohn Johansen 
88429b3822fSJohn Johansen 	/* is next profile a child */
88529b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
88629b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
88729b3822fSJohn Johansen 					base.list);
88829b3822fSJohn Johansen 
88929b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
89029b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
89129b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
89229b3822fSJohn Johansen 	while (parent) {
89338dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
89429b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
89529b3822fSJohn Johansen 			return p;
89629b3822fSJohn Johansen 		p = parent;
89729b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
89829b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
89929b3822fSJohn Johansen 	}
90029b3822fSJohn Johansen 
90129b3822fSJohn Johansen 	/* is next another profile in the namespace */
90238dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
90329b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
90429b3822fSJohn Johansen 		return p;
90529b3822fSJohn Johansen 
90629b3822fSJohn Johansen 	return NULL;
90729b3822fSJohn Johansen }
90829b3822fSJohn Johansen 
90929b3822fSJohn Johansen /**
91029b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
91129b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
91229b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
91329b3822fSJohn Johansen  *
91429b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
91529b3822fSJohn Johansen  */
91698849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
91729b3822fSJohn Johansen 				       struct aa_profile *profile)
91829b3822fSJohn Johansen {
91929b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
92029b3822fSJohn Johansen 	if (next)
92129b3822fSJohn Johansen 		return next;
92229b3822fSJohn Johansen 
92329b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
92498849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
92529b3822fSJohn Johansen }
92629b3822fSJohn Johansen 
92729b3822fSJohn Johansen /**
92829b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
92929b3822fSJohn Johansen  * @f: seq_file to fill
93029b3822fSJohn Johansen  * @pos: current position
93129b3822fSJohn Johansen  *
93229b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
93329b3822fSJohn Johansen  *
93429b3822fSJohn Johansen  * acquires first ns->lock
93529b3822fSJohn Johansen  */
93629b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
93729b3822fSJohn Johansen {
93829b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
93998849dffSJohn Johansen 	struct aa_ns *root = aa_current_profile()->ns;
94029b3822fSJohn Johansen 	loff_t l = *pos;
94198849dffSJohn Johansen 	f->private = aa_get_ns(root);
94229b3822fSJohn Johansen 
94329b3822fSJohn Johansen 
94429b3822fSJohn Johansen 	/* find the first profile */
94529b3822fSJohn Johansen 	mutex_lock(&root->lock);
94629b3822fSJohn Johansen 	profile = __first_profile(root, root);
94729b3822fSJohn Johansen 
94829b3822fSJohn Johansen 	/* skip to position */
94929b3822fSJohn Johansen 	for (; profile && l > 0; l--)
95029b3822fSJohn Johansen 		profile = next_profile(root, profile);
95129b3822fSJohn Johansen 
95229b3822fSJohn Johansen 	return profile;
95329b3822fSJohn Johansen }
95429b3822fSJohn Johansen 
95529b3822fSJohn Johansen /**
95629b3822fSJohn Johansen  * p_next - read the next profile entry
95729b3822fSJohn Johansen  * @f: seq_file to fill
95829b3822fSJohn Johansen  * @p: profile previously returned
95929b3822fSJohn Johansen  * @pos: current position
96029b3822fSJohn Johansen  *
96129b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
96229b3822fSJohn Johansen  *
96329b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
96429b3822fSJohn Johansen  */
96529b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
96629b3822fSJohn Johansen {
96729b3822fSJohn Johansen 	struct aa_profile *profile = p;
96898849dffSJohn Johansen 	struct aa_ns *ns = f->private;
96929b3822fSJohn Johansen 	(*pos)++;
97029b3822fSJohn Johansen 
97129b3822fSJohn Johansen 	return next_profile(ns, profile);
97229b3822fSJohn Johansen }
97329b3822fSJohn Johansen 
97429b3822fSJohn Johansen /**
97529b3822fSJohn Johansen  * p_stop - stop depth first traversal
97629b3822fSJohn Johansen  * @f: seq_file we are filling
97729b3822fSJohn Johansen  * @p: the last profile writen
97829b3822fSJohn Johansen  *
97929b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
98029b3822fSJohn Johansen  */
98129b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
98229b3822fSJohn Johansen {
98329b3822fSJohn Johansen 	struct aa_profile *profile = p;
98498849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
98529b3822fSJohn Johansen 
98629b3822fSJohn Johansen 	if (profile) {
98729b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
98829b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
98929b3822fSJohn Johansen 	}
99029b3822fSJohn Johansen 	mutex_unlock(&root->lock);
99198849dffSJohn Johansen 	aa_put_ns(root);
99229b3822fSJohn Johansen }
99329b3822fSJohn Johansen 
99429b3822fSJohn Johansen /**
99529b3822fSJohn Johansen  * seq_show_profile - show a profile entry
99629b3822fSJohn Johansen  * @f: seq_file to file
99729b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
99829b3822fSJohn Johansen  *
99929b3822fSJohn Johansen  * Returns: error on failure
100029b3822fSJohn Johansen  */
100129b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
100229b3822fSJohn Johansen {
100329b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
100498849dffSJohn Johansen 	struct aa_ns *root = f->private;
100529b3822fSJohn Johansen 
100629b3822fSJohn Johansen 	if (profile->ns != root)
100792b6d8efSJohn Johansen 		seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
100829b3822fSJohn Johansen 	seq_printf(f, "%s (%s)\n", profile->base.hname,
100929b3822fSJohn Johansen 		   aa_profile_mode_names[profile->mode]);
101029b3822fSJohn Johansen 
101129b3822fSJohn Johansen 	return 0;
101229b3822fSJohn Johansen }
101329b3822fSJohn Johansen 
101429b3822fSJohn Johansen static const struct seq_operations aa_fs_profiles_op = {
101529b3822fSJohn Johansen 	.start = p_start,
101629b3822fSJohn Johansen 	.next = p_next,
101729b3822fSJohn Johansen 	.stop = p_stop,
101829b3822fSJohn Johansen 	.show = seq_show_profile,
101929b3822fSJohn Johansen };
102029b3822fSJohn Johansen 
102129b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
102229b3822fSJohn Johansen {
10235ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
10245ac8c355SJohn Johansen 		return -EACCES;
10255ac8c355SJohn Johansen 
102629b3822fSJohn Johansen 	return seq_open(file, &aa_fs_profiles_op);
102729b3822fSJohn Johansen }
102829b3822fSJohn Johansen 
102929b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
103029b3822fSJohn Johansen {
103129b3822fSJohn Johansen 	return seq_release(inode, file);
103229b3822fSJohn Johansen }
103329b3822fSJohn Johansen 
103429b3822fSJohn Johansen static const struct file_operations aa_fs_profiles_fops = {
103529b3822fSJohn Johansen 	.open = profiles_open,
103629b3822fSJohn Johansen 	.read = seq_read,
103729b3822fSJohn Johansen 	.llseek = seq_lseek,
103829b3822fSJohn Johansen 	.release = profiles_release,
103929b3822fSJohn Johansen };
104029b3822fSJohn Johansen 
104129b3822fSJohn Johansen 
10420d259f04SJohn Johansen /** Base file system setup **/
1043a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = {
1044a9bf8e9fSKees Cook 	AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
1045a9bf8e9fSKees Cook 				  "link lock"),
1046a9bf8e9fSKees Cook 	{ }
1047a9bf8e9fSKees Cook };
1048a9bf8e9fSKees Cook 
1049e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = {
1050e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hat",	1),
1051e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hatv",	1),
1052e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_onexec",	1),
1053e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_profile",	1),
105434c426acSJohn Johansen 	AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
1055e74abcf3SKees Cook 	{ }
1056e74abcf3SKees Cook };
1057e74abcf3SKees Cook 
1058474d6b75SJohn Johansen static struct aa_fs_entry aa_fs_entry_versions[] = {
1059474d6b75SJohn Johansen 	AA_FS_FILE_BOOLEAN("v5",	1),
1060474d6b75SJohn Johansen 	{ }
1061474d6b75SJohn Johansen };
1062474d6b75SJohn Johansen 
10639d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = {
1064474d6b75SJohn Johansen 	AA_FS_DIR("versions",                   aa_fs_entry_versions),
1065dd51c848SJohn Johansen 	AA_FS_FILE_BOOLEAN("set_load",		1),
10669d910a3bSJohn Johansen 	{ }
10679d910a3bSJohn Johansen };
10689d910a3bSJohn Johansen 
1069e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = {
10709d910a3bSJohn Johansen 	AA_FS_DIR("policy",			aa_fs_entry_policy),
1071e74abcf3SKees Cook 	AA_FS_DIR("domain",			aa_fs_entry_domain),
1072a9bf8e9fSKees Cook 	AA_FS_DIR("file",			aa_fs_entry_file),
1073e74abcf3SKees Cook 	AA_FS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
1074d384b0a1SKees Cook 	AA_FS_DIR("rlimit",			aa_fs_entry_rlimit),
107584f1f787SJohn Johansen 	AA_FS_DIR("caps",			aa_fs_entry_caps),
1076e74abcf3SKees Cook 	{ }
1077e74abcf3SKees Cook };
1078e74abcf3SKees Cook 
10799acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = {
1080a71ada30SJohn Johansen 	AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level),
10813e3e5695SJohn Johansen 	AA_FS_FILE_FOPS(".ns_name", 0640, &aa_fs_ns_name),
1082b7fd2c03SJohn Johansen 	AA_FS_FILE_FOPS("profiles", 0440, &aa_fs_profiles_fops),
1083e74abcf3SKees Cook 	AA_FS_DIR("features", aa_fs_entry_features),
10849acd494bSKees Cook 	{ }
10859acd494bSKees Cook };
108663e2b423SJohn Johansen 
10879acd494bSKees Cook static struct aa_fs_entry aa_fs_entry =
10889acd494bSKees Cook 	AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
10899acd494bSKees Cook 
10909acd494bSKees Cook /**
10919acd494bSKees Cook  * aafs_create_file - create a file entry in the apparmor securityfs
10929acd494bSKees Cook  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
10939acd494bSKees Cook  * @parent: the parent dentry in the securityfs
10949acd494bSKees Cook  *
10959acd494bSKees Cook  * Use aafs_remove_file to remove entries created with this fn.
10969acd494bSKees Cook  */
10979acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file,
10989acd494bSKees Cook 				   struct dentry *parent)
109963e2b423SJohn Johansen {
11009acd494bSKees Cook 	int error = 0;
110163e2b423SJohn Johansen 
11029acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
11039acd494bSKees Cook 						 S_IFREG | fs_file->mode,
11049acd494bSKees Cook 						 parent, fs_file,
11059acd494bSKees Cook 						 fs_file->file_ops);
11069acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
11079acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
11089acd494bSKees Cook 		fs_file->dentry = NULL;
110963e2b423SJohn Johansen 	}
11109acd494bSKees Cook 	return error;
111163e2b423SJohn Johansen }
111263e2b423SJohn Johansen 
11130d259f04SJohn Johansen static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
111463e2b423SJohn Johansen /**
11159acd494bSKees Cook  * aafs_create_dir - recursively create a directory entry in the securityfs
11169acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
11179acd494bSKees Cook  * @parent: the parent dentry in the securityfs
111863e2b423SJohn Johansen  *
11199acd494bSKees Cook  * Use aafs_remove_dir to remove entries created with this fn.
112063e2b423SJohn Johansen  */
11219acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
11229acd494bSKees Cook 				  struct dentry *parent)
112363e2b423SJohn Johansen {
11249acd494bSKees Cook 	struct aa_fs_entry *fs_file;
11250d259f04SJohn Johansen 	struct dentry *dir;
11260d259f04SJohn Johansen 	int error;
112763e2b423SJohn Johansen 
11280d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
11290d259f04SJohn Johansen 	if (IS_ERR(dir))
11300d259f04SJohn Johansen 		return PTR_ERR(dir);
11310d259f04SJohn Johansen 	fs_dir->dentry = dir;
113263e2b423SJohn Johansen 
11330d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
11349acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
11359acd494bSKees Cook 			error = aafs_create_dir(fs_file, fs_dir->dentry);
11369acd494bSKees Cook 		else
11379acd494bSKees Cook 			error = aafs_create_file(fs_file, fs_dir->dentry);
11389acd494bSKees Cook 		if (error)
11399acd494bSKees Cook 			goto failed;
11409acd494bSKees Cook 	}
11419acd494bSKees Cook 
11429acd494bSKees Cook 	return 0;
11439acd494bSKees Cook 
11449acd494bSKees Cook failed:
11450d259f04SJohn Johansen 	aafs_remove_dir(fs_dir);
11460d259f04SJohn Johansen 
11479acd494bSKees Cook 	return error;
11489acd494bSKees Cook }
11499acd494bSKees Cook 
11509acd494bSKees Cook /**
11519acd494bSKees Cook  * aafs_remove_file - drop a single file entry in the apparmor securityfs
11529acd494bSKees Cook  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
11539acd494bSKees Cook  */
11549acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
11559acd494bSKees Cook {
11569acd494bSKees Cook 	if (!fs_file->dentry)
11579acd494bSKees Cook 		return;
11589acd494bSKees Cook 
11599acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
11609acd494bSKees Cook 	fs_file->dentry = NULL;
11619acd494bSKees Cook }
11629acd494bSKees Cook 
11639acd494bSKees Cook /**
11649acd494bSKees Cook  * aafs_remove_dir - recursively drop a directory entry from the securityfs
11659acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
11669acd494bSKees Cook  */
11679acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
11689acd494bSKees Cook {
11699acd494bSKees Cook 	struct aa_fs_entry *fs_file;
11709acd494bSKees Cook 
11710d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
11729acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
11739acd494bSKees Cook 			aafs_remove_dir(fs_file);
11749acd494bSKees Cook 		else
11759acd494bSKees Cook 			aafs_remove_file(fs_file);
11769acd494bSKees Cook 	}
11779acd494bSKees Cook 
11789acd494bSKees Cook 	aafs_remove_file(fs_dir);
117963e2b423SJohn Johansen }
118063e2b423SJohn Johansen 
118163e2b423SJohn Johansen /**
118263e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
118363e2b423SJohn Johansen  *
118463e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
118563e2b423SJohn Johansen  */
118663e2b423SJohn Johansen void __init aa_destroy_aafs(void)
118763e2b423SJohn Johansen {
11889acd494bSKees Cook 	aafs_remove_dir(&aa_fs_entry);
118963e2b423SJohn Johansen }
119063e2b423SJohn Johansen 
1191a71ada30SJohn Johansen 
1192a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
1193a71ada30SJohn Johansen struct path aa_null;
1194a71ada30SJohn Johansen 
1195a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
1196a71ada30SJohn Johansen {
1197a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
1198a71ada30SJohn Johansen 	struct dentry *dentry;
1199a71ada30SJohn Johansen 	struct inode *inode;
1200a71ada30SJohn Johansen 	int count = 0;
1201a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
1202a71ada30SJohn Johansen 
1203a71ada30SJohn Johansen 	if (error)
1204a71ada30SJohn Johansen 		return error;
1205a71ada30SJohn Johansen 
1206a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
1207a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
1208a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
1209a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
1210a71ada30SJohn Johansen 		goto out;
1211a71ada30SJohn Johansen 	}
1212a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
1213a71ada30SJohn Johansen 	if (!inode) {
1214a71ada30SJohn Johansen 		error = -ENOMEM;
1215a71ada30SJohn Johansen 		goto out1;
1216a71ada30SJohn Johansen 	}
1217a71ada30SJohn Johansen 
1218a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
1219a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
1220a71ada30SJohn Johansen 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1221a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
1222a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
1223a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
1224a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
1225a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
1226a71ada30SJohn Johansen 
1227a71ada30SJohn Johansen 	error = 0;
1228a71ada30SJohn Johansen 
1229a71ada30SJohn Johansen out1:
1230a71ada30SJohn Johansen 	dput(dentry);
1231a71ada30SJohn Johansen out:
1232a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
1233a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
1234a71ada30SJohn Johansen 	return error;
1235a71ada30SJohn Johansen }
1236a71ada30SJohn Johansen 
123763e2b423SJohn Johansen /**
123863e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
123963e2b423SJohn Johansen  *
124063e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
124163e2b423SJohn Johansen  *
124263e2b423SJohn Johansen  * Returns: error on failure
124363e2b423SJohn Johansen  */
12443417d8d5SJames Morris static int __init aa_create_aafs(void)
124563e2b423SJohn Johansen {
1246b7fd2c03SJohn Johansen 	struct dentry *dent;
124763e2b423SJohn Johansen 	int error;
124863e2b423SJohn Johansen 
124963e2b423SJohn Johansen 	if (!apparmor_initialized)
125063e2b423SJohn Johansen 		return 0;
125163e2b423SJohn Johansen 
12529acd494bSKees Cook 	if (aa_fs_entry.dentry) {
125363e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
125463e2b423SJohn Johansen 		return -EEXIST;
125563e2b423SJohn Johansen 	}
125663e2b423SJohn Johansen 
12579acd494bSKees Cook 	/* Populate fs tree. */
12589acd494bSKees Cook 	error = aafs_create_dir(&aa_fs_entry, NULL);
125963e2b423SJohn Johansen 	if (error)
126063e2b423SJohn Johansen 		goto error;
126163e2b423SJohn Johansen 
1262b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".load", 0666, aa_fs_entry.dentry,
1263b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_load);
1264b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1265b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1266b7fd2c03SJohn Johansen 		goto error;
1267b7fd2c03SJohn Johansen 	}
1268b7fd2c03SJohn Johansen 	ns_subload(root_ns) = dent;
1269b7fd2c03SJohn Johansen 
1270b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".replace", 0666, aa_fs_entry.dentry,
1271b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_replace);
1272b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1273b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1274b7fd2c03SJohn Johansen 		goto error;
1275b7fd2c03SJohn Johansen 	}
1276b7fd2c03SJohn Johansen 	ns_subreplace(root_ns) = dent;
1277b7fd2c03SJohn Johansen 
1278b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".remove", 0666, aa_fs_entry.dentry,
1279b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_remove);
1280b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1281b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1282b7fd2c03SJohn Johansen 		goto error;
1283b7fd2c03SJohn Johansen 	}
1284b7fd2c03SJohn Johansen 	ns_subremove(root_ns) = dent;
1285b7fd2c03SJohn Johansen 
1286b7fd2c03SJohn Johansen 	mutex_lock(&root_ns->lock);
128798849dffSJohn Johansen 	error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
1288b7fd2c03SJohn Johansen 	mutex_unlock(&root_ns->lock);
1289b7fd2c03SJohn Johansen 
12900d259f04SJohn Johansen 	if (error)
12910d259f04SJohn Johansen 		goto error;
12920d259f04SJohn Johansen 
1293a71ada30SJohn Johansen 	error = aa_mk_null_file(aa_fs_entry.dentry);
1294a71ada30SJohn Johansen 	if (error)
1295a71ada30SJohn Johansen 		goto error;
1296a71ada30SJohn Johansen 
1297a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
129863e2b423SJohn Johansen 
129963e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
130063e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
130163e2b423SJohn Johansen 	return 0;
130263e2b423SJohn Johansen 
130363e2b423SJohn Johansen error:
130463e2b423SJohn Johansen 	aa_destroy_aafs();
130563e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
130663e2b423SJohn Johansen 	return error;
130763e2b423SJohn Johansen }
130863e2b423SJohn Johansen 
130963e2b423SJohn Johansen fs_initcall(aa_create_aafs);
1310