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>
2163e2b423SJohn Johansen #include <linux/namei.h>
22e74abcf3SKees Cook #include <linux/capability.h>
2329b3822fSJohn Johansen #include <linux/rcupdate.h>
2463e2b423SJohn Johansen 
2563e2b423SJohn Johansen #include "include/apparmor.h"
2663e2b423SJohn Johansen #include "include/apparmorfs.h"
2763e2b423SJohn Johansen #include "include/audit.h"
2863e2b423SJohn Johansen #include "include/context.h"
29f8eb8a13SJohn Johansen #include "include/crypto.h"
3063e2b423SJohn Johansen #include "include/policy.h"
31cff281f6SJohn Johansen #include "include/policy_ns.h"
32d384b0a1SKees Cook #include "include/resource.h"
3363e2b423SJohn Johansen 
3463e2b423SJohn Johansen /**
350d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
360d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
370d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
380d259f04SJohn Johansen  *
390d259f04SJohn Johansen  * Returns: length of mangled name
400d259f04SJohn Johansen  */
41bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
420d259f04SJohn Johansen {
430d259f04SJohn Johansen 	char *t = target;
440d259f04SJohn Johansen 
450d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
460d259f04SJohn Johansen 		name++;
470d259f04SJohn Johansen 
480d259f04SJohn Johansen 	if (target) {
490d259f04SJohn Johansen 		for (; *name; name++) {
500d259f04SJohn Johansen 			if (*name == '/')
510d259f04SJohn Johansen 				*(t)++ = '.';
520d259f04SJohn Johansen 			else if (isspace(*name))
530d259f04SJohn Johansen 				*(t)++ = '_';
540d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
550d259f04SJohn Johansen 				*(t)++ = *name;
560d259f04SJohn Johansen 		}
570d259f04SJohn Johansen 
580d259f04SJohn Johansen 		*t = 0;
590d259f04SJohn Johansen 	} else {
600d259f04SJohn Johansen 		int len = 0;
610d259f04SJohn Johansen 		for (; *name; name++) {
620d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
630d259f04SJohn Johansen 			    strchr("/._-", *name))
640d259f04SJohn Johansen 				len++;
650d259f04SJohn Johansen 		}
660d259f04SJohn Johansen 
670d259f04SJohn Johansen 		return len;
680d259f04SJohn Johansen 	}
690d259f04SJohn Johansen 
700d259f04SJohn Johansen 	return t - target;
710d259f04SJohn Johansen }
720d259f04SJohn Johansen 
730d259f04SJohn Johansen /**
7463e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
7563e2b423SJohn Johansen  * @op: operation doing the user buffer copy
7663e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
773ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
7863e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
7963e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
8063e2b423SJohn Johansen  *
8163e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
8263e2b423SJohn Johansen  *          ERR_PTR on failure.
8363e2b423SJohn Johansen  */
8463e2b423SJohn Johansen static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
8563e2b423SJohn Johansen 				       size_t alloc_size, size_t copy_size,
8663e2b423SJohn Johansen 				       loff_t *pos)
8763e2b423SJohn Johansen {
8863e2b423SJohn Johansen 	char *data;
8963e2b423SJohn Johansen 
903ed02adaSJohn Johansen 	BUG_ON(copy_size > alloc_size);
913ed02adaSJohn Johansen 
9263e2b423SJohn Johansen 	if (*pos != 0)
9363e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
9463e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
9563e2b423SJohn Johansen 
9663e2b423SJohn Johansen 	/*
9763e2b423SJohn Johansen 	 * Don't allow profile load/replace/remove from profiles that don't
9863e2b423SJohn Johansen 	 * have CAP_MAC_ADMIN
9963e2b423SJohn Johansen 	 */
10063e2b423SJohn Johansen 	if (!aa_may_manage_policy(op))
10163e2b423SJohn Johansen 		return ERR_PTR(-EACCES);
10263e2b423SJohn Johansen 
10363e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
10463e2b423SJohn Johansen 	data = kvmalloc(alloc_size);
10563e2b423SJohn Johansen 	if (data == NULL)
10663e2b423SJohn Johansen 		return ERR_PTR(-ENOMEM);
10763e2b423SJohn Johansen 
10863e2b423SJohn Johansen 	if (copy_from_user(data, userbuf, copy_size)) {
10963e2b423SJohn Johansen 		kvfree(data);
11063e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
11163e2b423SJohn Johansen 	}
11263e2b423SJohn Johansen 
11363e2b423SJohn Johansen 	return data;
11463e2b423SJohn Johansen }
11563e2b423SJohn Johansen 
11663e2b423SJohn Johansen 
11763e2b423SJohn Johansen /* .load file hook fn to load policy */
11863e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
11963e2b423SJohn Johansen 			    loff_t *pos)
12063e2b423SJohn Johansen {
12163e2b423SJohn Johansen 	char *data;
12263e2b423SJohn Johansen 	ssize_t error;
12363e2b423SJohn Johansen 
12463e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
12563e2b423SJohn Johansen 
12663e2b423SJohn Johansen 	error = PTR_ERR(data);
12763e2b423SJohn Johansen 	if (!IS_ERR(data)) {
12873688d1eSJohn Johansen 		error = aa_replace_profiles(__aa_current_profile()->ns, data,
12973688d1eSJohn Johansen 					    size, PROF_ADD);
13063e2b423SJohn Johansen 		kvfree(data);
13163e2b423SJohn Johansen 	}
13263e2b423SJohn Johansen 
13363e2b423SJohn Johansen 	return error;
13463e2b423SJohn Johansen }
13563e2b423SJohn Johansen 
13663e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
1376038f373SArnd Bergmann 	.write = profile_load,
1386038f373SArnd Bergmann 	.llseek = default_llseek,
13963e2b423SJohn Johansen };
14063e2b423SJohn Johansen 
14163e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
14263e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
14363e2b423SJohn Johansen 			       size_t size, loff_t *pos)
14463e2b423SJohn Johansen {
14563e2b423SJohn Johansen 	char *data;
14663e2b423SJohn Johansen 	ssize_t error;
14763e2b423SJohn Johansen 
14863e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
14963e2b423SJohn Johansen 	error = PTR_ERR(data);
15063e2b423SJohn Johansen 	if (!IS_ERR(data)) {
15173688d1eSJohn Johansen 		error = aa_replace_profiles(__aa_current_profile()->ns, data,
15273688d1eSJohn Johansen 					    size, PROF_REPLACE);
15363e2b423SJohn Johansen 		kvfree(data);
15463e2b423SJohn Johansen 	}
15563e2b423SJohn Johansen 
15663e2b423SJohn Johansen 	return error;
15763e2b423SJohn Johansen }
15863e2b423SJohn Johansen 
15963e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
1606038f373SArnd Bergmann 	.write = profile_replace,
1616038f373SArnd Bergmann 	.llseek = default_llseek,
16263e2b423SJohn Johansen };
16363e2b423SJohn Johansen 
16463e2b423SJohn Johansen /* .remove file hook fn to remove loaded policy */
16563e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
16663e2b423SJohn Johansen 			      size_t size, loff_t *pos)
16763e2b423SJohn Johansen {
16863e2b423SJohn Johansen 	char *data;
16963e2b423SJohn Johansen 	ssize_t error;
17063e2b423SJohn Johansen 
17163e2b423SJohn Johansen 	/*
17263e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
17363e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
17463e2b423SJohn Johansen 	 */
17563e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
17663e2b423SJohn Johansen 
17763e2b423SJohn Johansen 	error = PTR_ERR(data);
17863e2b423SJohn Johansen 	if (!IS_ERR(data)) {
17963e2b423SJohn Johansen 		data[size] = 0;
18063e2b423SJohn Johansen 		error = aa_remove_profiles(data, size);
18163e2b423SJohn Johansen 		kvfree(data);
18263e2b423SJohn Johansen 	}
18363e2b423SJohn Johansen 
18463e2b423SJohn Johansen 	return error;
18563e2b423SJohn Johansen }
18663e2b423SJohn Johansen 
18763e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
1886038f373SArnd Bergmann 	.write = profile_remove,
1896038f373SArnd Bergmann 	.llseek = default_llseek,
19063e2b423SJohn Johansen };
19163e2b423SJohn Johansen 
192e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v)
193e74abcf3SKees Cook {
194e74abcf3SKees Cook 	struct aa_fs_entry *fs_file = seq->private;
195e74abcf3SKees Cook 
196e74abcf3SKees Cook 	if (!fs_file)
197e74abcf3SKees Cook 		return 0;
198e74abcf3SKees Cook 
199e74abcf3SKees Cook 	switch (fs_file->v_type) {
200e74abcf3SKees Cook 	case AA_FS_TYPE_BOOLEAN:
201e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
202e74abcf3SKees Cook 		break;
203a9bf8e9fSKees Cook 	case AA_FS_TYPE_STRING:
204a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
205a9bf8e9fSKees Cook 		break;
206e74abcf3SKees Cook 	case AA_FS_TYPE_U64:
207e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
208e74abcf3SKees Cook 		break;
209e74abcf3SKees Cook 	default:
210e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
211e74abcf3SKees Cook 		break;
212e74abcf3SKees Cook 	}
213e74abcf3SKees Cook 
214e74abcf3SKees Cook 	return 0;
215e74abcf3SKees Cook }
216e74abcf3SKees Cook 
217e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file)
218e74abcf3SKees Cook {
219e74abcf3SKees Cook 	return single_open(file, aa_fs_seq_show, inode->i_private);
220e74abcf3SKees Cook }
221e74abcf3SKees Cook 
222e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = {
223e74abcf3SKees Cook 	.owner		= THIS_MODULE,
224e74abcf3SKees Cook 	.open		= aa_fs_seq_open,
225e74abcf3SKees Cook 	.read		= seq_read,
226e74abcf3SKees Cook 	.llseek		= seq_lseek,
227e74abcf3SKees Cook 	.release	= single_release,
228e74abcf3SKees Cook };
229e74abcf3SKees Cook 
2300d259f04SJohn Johansen static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
2310d259f04SJohn Johansen 				  int (*show)(struct seq_file *, void *))
2320d259f04SJohn Johansen {
2338399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
2348399588aSJohn Johansen 	int error = single_open(file, show, proxy);
23563e2b423SJohn Johansen 
2360d259f04SJohn Johansen 	if (error) {
2370d259f04SJohn Johansen 		file->private_data = NULL;
2388399588aSJohn Johansen 		aa_put_proxy(proxy);
2390d259f04SJohn Johansen 	}
2400d259f04SJohn Johansen 
2410d259f04SJohn Johansen 	return error;
2420d259f04SJohn Johansen }
2430d259f04SJohn Johansen 
2440d259f04SJohn Johansen static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
2450d259f04SJohn Johansen {
2460d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
2470d259f04SJohn Johansen 	if (seq)
2488399588aSJohn Johansen 		aa_put_proxy(seq->private);
2490d259f04SJohn Johansen 	return single_release(inode, file);
2500d259f04SJohn Johansen }
2510d259f04SJohn Johansen 
2520d259f04SJohn Johansen static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
2530d259f04SJohn Johansen {
2548399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
2558399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
2560d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
2570d259f04SJohn Johansen 	aa_put_profile(profile);
2580d259f04SJohn Johansen 
2590d259f04SJohn Johansen 	return 0;
2600d259f04SJohn Johansen }
2610d259f04SJohn Johansen 
2620d259f04SJohn Johansen static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
2630d259f04SJohn Johansen {
2640d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
2650d259f04SJohn Johansen }
2660d259f04SJohn Johansen 
2670d259f04SJohn Johansen static const struct file_operations aa_fs_profname_fops = {
2680d259f04SJohn Johansen 	.owner		= THIS_MODULE,
2690d259f04SJohn Johansen 	.open		= aa_fs_seq_profname_open,
2700d259f04SJohn Johansen 	.read		= seq_read,
2710d259f04SJohn Johansen 	.llseek		= seq_lseek,
2720d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
2730d259f04SJohn Johansen };
2740d259f04SJohn Johansen 
2750d259f04SJohn Johansen static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
2760d259f04SJohn Johansen {
2778399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
2788399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
2790d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
2800d259f04SJohn Johansen 	aa_put_profile(profile);
2810d259f04SJohn Johansen 
2820d259f04SJohn Johansen 	return 0;
2830d259f04SJohn Johansen }
2840d259f04SJohn Johansen 
2850d259f04SJohn Johansen static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
2860d259f04SJohn Johansen {
2870d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
2880d259f04SJohn Johansen }
2890d259f04SJohn Johansen 
2900d259f04SJohn Johansen static const struct file_operations aa_fs_profmode_fops = {
2910d259f04SJohn Johansen 	.owner		= THIS_MODULE,
2920d259f04SJohn Johansen 	.open		= aa_fs_seq_profmode_open,
2930d259f04SJohn Johansen 	.read		= seq_read,
2940d259f04SJohn Johansen 	.llseek		= seq_lseek,
2950d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
2960d259f04SJohn Johansen };
2970d259f04SJohn Johansen 
298556d0be7SJohn Johansen static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
299556d0be7SJohn Johansen {
3008399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3018399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
302556d0be7SJohn Johansen 	if (profile->attach)
303556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
304556d0be7SJohn Johansen 	else if (profile->xmatch)
305556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
306556d0be7SJohn Johansen 	else
307556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
308556d0be7SJohn Johansen 	aa_put_profile(profile);
309556d0be7SJohn Johansen 
310556d0be7SJohn Johansen 	return 0;
311556d0be7SJohn Johansen }
312556d0be7SJohn Johansen 
313556d0be7SJohn Johansen static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
314556d0be7SJohn Johansen {
315556d0be7SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
316556d0be7SJohn Johansen }
317556d0be7SJohn Johansen 
318556d0be7SJohn Johansen static const struct file_operations aa_fs_profattach_fops = {
319556d0be7SJohn Johansen 	.owner		= THIS_MODULE,
320556d0be7SJohn Johansen 	.open		= aa_fs_seq_profattach_open,
321556d0be7SJohn Johansen 	.read		= seq_read,
322556d0be7SJohn Johansen 	.llseek		= seq_lseek,
323556d0be7SJohn Johansen 	.release	= aa_fs_seq_profile_release,
324556d0be7SJohn Johansen };
325556d0be7SJohn Johansen 
326f8eb8a13SJohn Johansen static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
327f8eb8a13SJohn Johansen {
3288399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3298399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
330f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
331f8eb8a13SJohn Johansen 
332f8eb8a13SJohn Johansen 	if (profile->hash) {
333f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
334f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
335f8eb8a13SJohn Johansen 		seq_puts(seq, "\n");
336f8eb8a13SJohn Johansen 	}
3370b938a2eSJohn Johansen 	aa_put_profile(profile);
338f8eb8a13SJohn Johansen 
339f8eb8a13SJohn Johansen 	return 0;
340f8eb8a13SJohn Johansen }
341f8eb8a13SJohn Johansen 
342f8eb8a13SJohn Johansen static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
343f8eb8a13SJohn Johansen {
344f8eb8a13SJohn Johansen 	return single_open(file, aa_fs_seq_hash_show, inode->i_private);
345f8eb8a13SJohn Johansen }
346f8eb8a13SJohn Johansen 
347f8eb8a13SJohn Johansen static const struct file_operations aa_fs_seq_hash_fops = {
348f8eb8a13SJohn Johansen 	.owner		= THIS_MODULE,
349f8eb8a13SJohn Johansen 	.open		= aa_fs_seq_hash_open,
350f8eb8a13SJohn Johansen 	.read		= seq_read,
351f8eb8a13SJohn Johansen 	.llseek		= seq_lseek,
352f8eb8a13SJohn Johansen 	.release	= single_release,
353f8eb8a13SJohn Johansen };
354f8eb8a13SJohn Johansen 
3550d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
3560d259f04SJohn Johansen void __aa_fs_profile_rmdir(struct aa_profile *profile)
3570d259f04SJohn Johansen {
3580d259f04SJohn Johansen 	struct aa_profile *child;
3590d259f04SJohn Johansen 	int i;
3600d259f04SJohn Johansen 
3610d259f04SJohn Johansen 	if (!profile)
3620d259f04SJohn Johansen 		return;
3630d259f04SJohn Johansen 
3640d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
3650d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
3660d259f04SJohn Johansen 
3670d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
3688399588aSJohn Johansen 		struct aa_proxy *proxy;
3690d259f04SJohn Johansen 		if (!profile->dents[i])
3700d259f04SJohn Johansen 			continue;
3710d259f04SJohn Johansen 
3728399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
3730d259f04SJohn Johansen 		securityfs_remove(profile->dents[i]);
3748399588aSJohn Johansen 		aa_put_proxy(proxy);
3750d259f04SJohn Johansen 		profile->dents[i] = NULL;
3760d259f04SJohn Johansen 	}
3770d259f04SJohn Johansen }
3780d259f04SJohn Johansen 
3790d259f04SJohn Johansen void __aa_fs_profile_migrate_dents(struct aa_profile *old,
3800d259f04SJohn Johansen 				   struct aa_profile *new)
3810d259f04SJohn Johansen {
3820d259f04SJohn Johansen 	int i;
3830d259f04SJohn Johansen 
3840d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
3850d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
386d671e890SJohn Johansen 		if (new->dents[i])
387078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
3880d259f04SJohn Johansen 		old->dents[i] = NULL;
3890d259f04SJohn Johansen 	}
3900d259f04SJohn Johansen }
3910d259f04SJohn Johansen 
3920d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
3930d259f04SJohn Johansen 					  struct aa_profile *profile,
3940d259f04SJohn Johansen 					  const struct file_operations *fops)
3950d259f04SJohn Johansen {
3968399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
3970d259f04SJohn Johansen 	struct dentry *dent;
3980d259f04SJohn Johansen 
3998399588aSJohn Johansen 	dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
4000d259f04SJohn Johansen 	if (IS_ERR(dent))
4018399588aSJohn Johansen 		aa_put_proxy(proxy);
4020d259f04SJohn Johansen 
4030d259f04SJohn Johansen 	return dent;
4040d259f04SJohn Johansen }
4050d259f04SJohn Johansen 
4060d259f04SJohn Johansen /* requires lock be held */
4070d259f04SJohn Johansen int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
4080d259f04SJohn Johansen {
4090d259f04SJohn Johansen 	struct aa_profile *child;
4100d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
4110d259f04SJohn Johansen 	int error;
4120d259f04SJohn Johansen 
4130d259f04SJohn Johansen 	if (!parent) {
4140d259f04SJohn Johansen 		struct aa_profile *p;
4150d259f04SJohn Johansen 		p = aa_deref_parent(profile);
4160d259f04SJohn Johansen 		dent = prof_dir(p);
4170d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
4180d259f04SJohn Johansen 		dent = securityfs_create_dir("profiles", dent);
4190d259f04SJohn Johansen 		if (IS_ERR(dent))
4200d259f04SJohn Johansen 			goto fail;
4210d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
4220d259f04SJohn Johansen 	}
4230d259f04SJohn Johansen 
4240d259f04SJohn Johansen 	if (!profile->dirname) {
4250d259f04SJohn Johansen 		int len, id_len;
4260d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
4270d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
4280d259f04SJohn Johansen 
4290d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
4300d259f04SJohn Johansen 		if (!profile->dirname)
4310d259f04SJohn Johansen 			goto fail;
4320d259f04SJohn Johansen 
4330d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
4340d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
4350d259f04SJohn Johansen 	}
4360d259f04SJohn Johansen 
4370d259f04SJohn Johansen 	dent = securityfs_create_dir(profile->dirname, parent);
4380d259f04SJohn Johansen 	if (IS_ERR(dent))
4390d259f04SJohn Johansen 		goto fail;
4400d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
4410d259f04SJohn Johansen 
4420d259f04SJohn Johansen 	dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
4430d259f04SJohn Johansen 	if (IS_ERR(dent))
4440d259f04SJohn Johansen 		goto fail;
4450d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
4460d259f04SJohn Johansen 
4470d259f04SJohn Johansen 	dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
4480d259f04SJohn Johansen 	if (IS_ERR(dent))
4490d259f04SJohn Johansen 		goto fail;
4500d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
4510d259f04SJohn Johansen 
452556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
453556d0be7SJohn Johansen 				   &aa_fs_profattach_fops);
454556d0be7SJohn Johansen 	if (IS_ERR(dent))
455556d0be7SJohn Johansen 		goto fail;
456556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
457556d0be7SJohn Johansen 
458f8eb8a13SJohn Johansen 	if (profile->hash) {
459f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
460f8eb8a13SJohn Johansen 					   &aa_fs_seq_hash_fops);
461f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
462f8eb8a13SJohn Johansen 			goto fail;
463f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
464f8eb8a13SJohn Johansen 	}
465f8eb8a13SJohn Johansen 
4660d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
4670d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
4680d259f04SJohn Johansen 		if (error)
4690d259f04SJohn Johansen 			goto fail2;
4700d259f04SJohn Johansen 	}
4710d259f04SJohn Johansen 
4720d259f04SJohn Johansen 	return 0;
4730d259f04SJohn Johansen 
4740d259f04SJohn Johansen fail:
4750d259f04SJohn Johansen 	error = PTR_ERR(dent);
4760d259f04SJohn Johansen 
4770d259f04SJohn Johansen fail2:
4780d259f04SJohn Johansen 	__aa_fs_profile_rmdir(profile);
4790d259f04SJohn Johansen 
4800d259f04SJohn Johansen 	return error;
4810d259f04SJohn Johansen }
4820d259f04SJohn Johansen 
48398849dffSJohn Johansen void __aa_fs_ns_rmdir(struct aa_ns *ns)
4840d259f04SJohn Johansen {
48598849dffSJohn Johansen 	struct aa_ns *sub;
4860d259f04SJohn Johansen 	struct aa_profile *child;
4870d259f04SJohn Johansen 	int i;
4880d259f04SJohn Johansen 
4890d259f04SJohn Johansen 	if (!ns)
4900d259f04SJohn Johansen 		return;
4910d259f04SJohn Johansen 
4920d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
4930d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
4940d259f04SJohn Johansen 
4950d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
4960d259f04SJohn Johansen 		mutex_lock(&sub->lock);
49798849dffSJohn Johansen 		__aa_fs_ns_rmdir(sub);
4980d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
4990d259f04SJohn Johansen 	}
5000d259f04SJohn Johansen 
5010d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
5020d259f04SJohn Johansen 		securityfs_remove(ns->dents[i]);
5030d259f04SJohn Johansen 		ns->dents[i] = NULL;
5040d259f04SJohn Johansen 	}
5050d259f04SJohn Johansen }
5060d259f04SJohn Johansen 
50798849dffSJohn Johansen int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
5080d259f04SJohn Johansen {
50998849dffSJohn Johansen 	struct aa_ns *sub;
5100d259f04SJohn Johansen 	struct aa_profile *child;
5110d259f04SJohn Johansen 	struct dentry *dent, *dir;
5120d259f04SJohn Johansen 	int error;
5130d259f04SJohn Johansen 
5140d259f04SJohn Johansen 	if (!name)
5150d259f04SJohn Johansen 		name = ns->base.name;
5160d259f04SJohn Johansen 
5170d259f04SJohn Johansen 	dent = securityfs_create_dir(name, parent);
5180d259f04SJohn Johansen 	if (IS_ERR(dent))
5190d259f04SJohn Johansen 		goto fail;
5200d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
5210d259f04SJohn Johansen 
5220d259f04SJohn Johansen 	dent = securityfs_create_dir("profiles", dir);
5230d259f04SJohn Johansen 	if (IS_ERR(dent))
5240d259f04SJohn Johansen 		goto fail;
5250d259f04SJohn Johansen 	ns_subprofs_dir(ns) = dent;
5260d259f04SJohn Johansen 
5270d259f04SJohn Johansen 	dent = securityfs_create_dir("namespaces", dir);
5280d259f04SJohn Johansen 	if (IS_ERR(dent))
5290d259f04SJohn Johansen 		goto fail;
5300d259f04SJohn Johansen 	ns_subns_dir(ns) = dent;
5310d259f04SJohn Johansen 
5320d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
5330d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
5340d259f04SJohn Johansen 		if (error)
5350d259f04SJohn Johansen 			goto fail2;
5360d259f04SJohn Johansen 	}
5370d259f04SJohn Johansen 
5380d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
5390d259f04SJohn Johansen 		mutex_lock(&sub->lock);
54098849dffSJohn Johansen 		error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
5410d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
5420d259f04SJohn Johansen 		if (error)
5430d259f04SJohn Johansen 			goto fail2;
5440d259f04SJohn Johansen 	}
5450d259f04SJohn Johansen 
5460d259f04SJohn Johansen 	return 0;
5470d259f04SJohn Johansen 
5480d259f04SJohn Johansen fail:
5490d259f04SJohn Johansen 	error = PTR_ERR(dent);
5500d259f04SJohn Johansen 
5510d259f04SJohn Johansen fail2:
55298849dffSJohn Johansen 	__aa_fs_ns_rmdir(ns);
5530d259f04SJohn Johansen 
5540d259f04SJohn Johansen 	return error;
5550d259f04SJohn Johansen }
5560d259f04SJohn Johansen 
5570d259f04SJohn Johansen 
55829b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
55929b3822fSJohn Johansen 
56029b3822fSJohn Johansen /**
56198849dffSJohn Johansen  * __next_ns - find the next namespace to list
56229b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
56329b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
56429b3822fSJohn Johansen  *
56529b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
56629b3822fSJohn Johansen  * while switching current namespace.
56729b3822fSJohn Johansen  *
56829b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
56929b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
57029b3822fSJohn Johansen  * NOTE: will not unlock root->lock
57129b3822fSJohn Johansen  */
57298849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
57329b3822fSJohn Johansen {
57498849dffSJohn Johansen 	struct aa_ns *parent, *next;
57529b3822fSJohn Johansen 
57629b3822fSJohn Johansen 	/* is next namespace a child */
57729b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
57829b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
57929b3822fSJohn Johansen 		mutex_lock(&next->lock);
58029b3822fSJohn Johansen 		return next;
58129b3822fSJohn Johansen 	}
58229b3822fSJohn Johansen 
58329b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
58429b3822fSJohn Johansen 	parent = ns->parent;
585ed2c7da3SJohn Johansen 	while (ns != root) {
58629b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
58738dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
58829b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
58929b3822fSJohn Johansen 			mutex_lock(&next->lock);
59029b3822fSJohn Johansen 			return next;
59129b3822fSJohn Johansen 		}
59229b3822fSJohn Johansen 		ns = parent;
59329b3822fSJohn Johansen 		parent = parent->parent;
59429b3822fSJohn Johansen 	}
59529b3822fSJohn Johansen 
59629b3822fSJohn Johansen 	return NULL;
59729b3822fSJohn Johansen }
59829b3822fSJohn Johansen 
59929b3822fSJohn Johansen /**
60029b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
60129b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
60229b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
60329b3822fSJohn Johansen  *
60429b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
60529b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
60629b3822fSJohn Johansen  */
60798849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
60898849dffSJohn Johansen 					  struct aa_ns *ns)
60929b3822fSJohn Johansen {
61098849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
61129b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
61229b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
61329b3822fSJohn Johansen 						struct aa_profile, base.list);
61429b3822fSJohn Johansen 	}
61529b3822fSJohn Johansen 	return NULL;
61629b3822fSJohn Johansen }
61729b3822fSJohn Johansen 
61829b3822fSJohn Johansen /**
61929b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
62029b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
62129b3822fSJohn Johansen  *
62229b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
62329b3822fSJohn Johansen  *
62429b3822fSJohn Johansen  * Returns: next profile or NULL if done
62529b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
62629b3822fSJohn Johansen  */
62729b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
62829b3822fSJohn Johansen {
62929b3822fSJohn Johansen 	struct aa_profile *parent;
63098849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
63129b3822fSJohn Johansen 
63229b3822fSJohn Johansen 	/* is next profile a child */
63329b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
63429b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
63529b3822fSJohn Johansen 					base.list);
63629b3822fSJohn Johansen 
63729b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
63829b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
63929b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
64029b3822fSJohn Johansen 	while (parent) {
64138dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
64229b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
64329b3822fSJohn Johansen 			return p;
64429b3822fSJohn Johansen 		p = parent;
64529b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
64629b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
64729b3822fSJohn Johansen 	}
64829b3822fSJohn Johansen 
64929b3822fSJohn Johansen 	/* is next another profile in the namespace */
65038dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
65129b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
65229b3822fSJohn Johansen 		return p;
65329b3822fSJohn Johansen 
65429b3822fSJohn Johansen 	return NULL;
65529b3822fSJohn Johansen }
65629b3822fSJohn Johansen 
65729b3822fSJohn Johansen /**
65829b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
65929b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
66029b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
66129b3822fSJohn Johansen  *
66229b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
66329b3822fSJohn Johansen  */
66498849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
66529b3822fSJohn Johansen 				       struct aa_profile *profile)
66629b3822fSJohn Johansen {
66729b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
66829b3822fSJohn Johansen 	if (next)
66929b3822fSJohn Johansen 		return next;
67029b3822fSJohn Johansen 
67129b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
67298849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
67329b3822fSJohn Johansen }
67429b3822fSJohn Johansen 
67529b3822fSJohn Johansen /**
67629b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
67729b3822fSJohn Johansen  * @f: seq_file to fill
67829b3822fSJohn Johansen  * @pos: current position
67929b3822fSJohn Johansen  *
68029b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
68129b3822fSJohn Johansen  *
68229b3822fSJohn Johansen  * acquires first ns->lock
68329b3822fSJohn Johansen  */
68429b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
68529b3822fSJohn Johansen {
68629b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
68798849dffSJohn Johansen 	struct aa_ns *root = aa_current_profile()->ns;
68829b3822fSJohn Johansen 	loff_t l = *pos;
68998849dffSJohn Johansen 	f->private = aa_get_ns(root);
69029b3822fSJohn Johansen 
69129b3822fSJohn Johansen 
69229b3822fSJohn Johansen 	/* find the first profile */
69329b3822fSJohn Johansen 	mutex_lock(&root->lock);
69429b3822fSJohn Johansen 	profile = __first_profile(root, root);
69529b3822fSJohn Johansen 
69629b3822fSJohn Johansen 	/* skip to position */
69729b3822fSJohn Johansen 	for (; profile && l > 0; l--)
69829b3822fSJohn Johansen 		profile = next_profile(root, profile);
69929b3822fSJohn Johansen 
70029b3822fSJohn Johansen 	return profile;
70129b3822fSJohn Johansen }
70229b3822fSJohn Johansen 
70329b3822fSJohn Johansen /**
70429b3822fSJohn Johansen  * p_next - read the next profile entry
70529b3822fSJohn Johansen  * @f: seq_file to fill
70629b3822fSJohn Johansen  * @p: profile previously returned
70729b3822fSJohn Johansen  * @pos: current position
70829b3822fSJohn Johansen  *
70929b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
71029b3822fSJohn Johansen  *
71129b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
71229b3822fSJohn Johansen  */
71329b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
71429b3822fSJohn Johansen {
71529b3822fSJohn Johansen 	struct aa_profile *profile = p;
71698849dffSJohn Johansen 	struct aa_ns *ns = f->private;
71729b3822fSJohn Johansen 	(*pos)++;
71829b3822fSJohn Johansen 
71929b3822fSJohn Johansen 	return next_profile(ns, profile);
72029b3822fSJohn Johansen }
72129b3822fSJohn Johansen 
72229b3822fSJohn Johansen /**
72329b3822fSJohn Johansen  * p_stop - stop depth first traversal
72429b3822fSJohn Johansen  * @f: seq_file we are filling
72529b3822fSJohn Johansen  * @p: the last profile writen
72629b3822fSJohn Johansen  *
72729b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
72829b3822fSJohn Johansen  */
72929b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
73029b3822fSJohn Johansen {
73129b3822fSJohn Johansen 	struct aa_profile *profile = p;
73298849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
73329b3822fSJohn Johansen 
73429b3822fSJohn Johansen 	if (profile) {
73529b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
73629b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
73729b3822fSJohn Johansen 	}
73829b3822fSJohn Johansen 	mutex_unlock(&root->lock);
73998849dffSJohn Johansen 	aa_put_ns(root);
74029b3822fSJohn Johansen }
74129b3822fSJohn Johansen 
74229b3822fSJohn Johansen /**
74329b3822fSJohn Johansen  * seq_show_profile - show a profile entry
74429b3822fSJohn Johansen  * @f: seq_file to file
74529b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
74629b3822fSJohn Johansen  *
74729b3822fSJohn Johansen  * Returns: error on failure
74829b3822fSJohn Johansen  */
74929b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
75029b3822fSJohn Johansen {
75129b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
75298849dffSJohn Johansen 	struct aa_ns *root = f->private;
75329b3822fSJohn Johansen 
75429b3822fSJohn Johansen 	if (profile->ns != root)
75592b6d8efSJohn Johansen 		seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
75629b3822fSJohn Johansen 	seq_printf(f, "%s (%s)\n", profile->base.hname,
75729b3822fSJohn Johansen 		   aa_profile_mode_names[profile->mode]);
75829b3822fSJohn Johansen 
75929b3822fSJohn Johansen 	return 0;
76029b3822fSJohn Johansen }
76129b3822fSJohn Johansen 
76229b3822fSJohn Johansen static const struct seq_operations aa_fs_profiles_op = {
76329b3822fSJohn Johansen 	.start = p_start,
76429b3822fSJohn Johansen 	.next = p_next,
76529b3822fSJohn Johansen 	.stop = p_stop,
76629b3822fSJohn Johansen 	.show = seq_show_profile,
76729b3822fSJohn Johansen };
76829b3822fSJohn Johansen 
76929b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
77029b3822fSJohn Johansen {
77129b3822fSJohn Johansen 	return seq_open(file, &aa_fs_profiles_op);
77229b3822fSJohn Johansen }
77329b3822fSJohn Johansen 
77429b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
77529b3822fSJohn Johansen {
77629b3822fSJohn Johansen 	return seq_release(inode, file);
77729b3822fSJohn Johansen }
77829b3822fSJohn Johansen 
77929b3822fSJohn Johansen static const struct file_operations aa_fs_profiles_fops = {
78029b3822fSJohn Johansen 	.open = profiles_open,
78129b3822fSJohn Johansen 	.read = seq_read,
78229b3822fSJohn Johansen 	.llseek = seq_lseek,
78329b3822fSJohn Johansen 	.release = profiles_release,
78429b3822fSJohn Johansen };
78529b3822fSJohn Johansen 
78629b3822fSJohn Johansen 
7870d259f04SJohn Johansen /** Base file system setup **/
788a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = {
789a9bf8e9fSKees Cook 	AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
790a9bf8e9fSKees Cook 				  "link lock"),
791a9bf8e9fSKees Cook 	{ }
792a9bf8e9fSKees Cook };
793a9bf8e9fSKees Cook 
794e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = {
795e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hat",	1),
796e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hatv",	1),
797e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_onexec",	1),
798e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_profile",	1),
799e74abcf3SKees Cook 	{ }
800e74abcf3SKees Cook };
801e74abcf3SKees Cook 
802474d6b75SJohn Johansen static struct aa_fs_entry aa_fs_entry_versions[] = {
803474d6b75SJohn Johansen 	AA_FS_FILE_BOOLEAN("v5",	1),
804474d6b75SJohn Johansen 	{ }
805474d6b75SJohn Johansen };
806474d6b75SJohn Johansen 
8079d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = {
808474d6b75SJohn Johansen 	AA_FS_DIR("versions",                   aa_fs_entry_versions),
809dd51c848SJohn Johansen 	AA_FS_FILE_BOOLEAN("set_load",		1),
8109d910a3bSJohn Johansen 	{ }
8119d910a3bSJohn Johansen };
8129d910a3bSJohn Johansen 
813e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = {
8149d910a3bSJohn Johansen 	AA_FS_DIR("policy",			aa_fs_entry_policy),
815e74abcf3SKees Cook 	AA_FS_DIR("domain",			aa_fs_entry_domain),
816a9bf8e9fSKees Cook 	AA_FS_DIR("file",			aa_fs_entry_file),
817e74abcf3SKees Cook 	AA_FS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
818d384b0a1SKees Cook 	AA_FS_DIR("rlimit",			aa_fs_entry_rlimit),
81984f1f787SJohn Johansen 	AA_FS_DIR("caps",			aa_fs_entry_caps),
820e74abcf3SKees Cook 	{ }
821e74abcf3SKees Cook };
822e74abcf3SKees Cook 
8239acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = {
8249acd494bSKees Cook 	AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
8259acd494bSKees Cook 	AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
8269acd494bSKees Cook 	AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
82729b3822fSJohn Johansen 	AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
828e74abcf3SKees Cook 	AA_FS_DIR("features", aa_fs_entry_features),
8299acd494bSKees Cook 	{ }
8309acd494bSKees Cook };
83163e2b423SJohn Johansen 
8329acd494bSKees Cook static struct aa_fs_entry aa_fs_entry =
8339acd494bSKees Cook 	AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
8349acd494bSKees Cook 
8359acd494bSKees Cook /**
8369acd494bSKees Cook  * aafs_create_file - create a file entry in the apparmor securityfs
8379acd494bSKees Cook  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
8389acd494bSKees Cook  * @parent: the parent dentry in the securityfs
8399acd494bSKees Cook  *
8409acd494bSKees Cook  * Use aafs_remove_file to remove entries created with this fn.
8419acd494bSKees Cook  */
8429acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file,
8439acd494bSKees Cook 				   struct dentry *parent)
84463e2b423SJohn Johansen {
8459acd494bSKees Cook 	int error = 0;
84663e2b423SJohn Johansen 
8479acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
8489acd494bSKees Cook 						 S_IFREG | fs_file->mode,
8499acd494bSKees Cook 						 parent, fs_file,
8509acd494bSKees Cook 						 fs_file->file_ops);
8519acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
8529acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
8539acd494bSKees Cook 		fs_file->dentry = NULL;
85463e2b423SJohn Johansen 	}
8559acd494bSKees Cook 	return error;
85663e2b423SJohn Johansen }
85763e2b423SJohn Johansen 
8580d259f04SJohn Johansen static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
85963e2b423SJohn Johansen /**
8609acd494bSKees Cook  * aafs_create_dir - recursively create a directory entry in the securityfs
8619acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
8629acd494bSKees Cook  * @parent: the parent dentry in the securityfs
86363e2b423SJohn Johansen  *
8649acd494bSKees Cook  * Use aafs_remove_dir to remove entries created with this fn.
86563e2b423SJohn Johansen  */
8669acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
8679acd494bSKees Cook 				  struct dentry *parent)
86863e2b423SJohn Johansen {
8699acd494bSKees Cook 	struct aa_fs_entry *fs_file;
8700d259f04SJohn Johansen 	struct dentry *dir;
8710d259f04SJohn Johansen 	int error;
87263e2b423SJohn Johansen 
8730d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
8740d259f04SJohn Johansen 	if (IS_ERR(dir))
8750d259f04SJohn Johansen 		return PTR_ERR(dir);
8760d259f04SJohn Johansen 	fs_dir->dentry = dir;
87763e2b423SJohn Johansen 
8780d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
8799acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
8809acd494bSKees Cook 			error = aafs_create_dir(fs_file, fs_dir->dentry);
8819acd494bSKees Cook 		else
8829acd494bSKees Cook 			error = aafs_create_file(fs_file, fs_dir->dentry);
8839acd494bSKees Cook 		if (error)
8849acd494bSKees Cook 			goto failed;
8859acd494bSKees Cook 	}
8869acd494bSKees Cook 
8879acd494bSKees Cook 	return 0;
8889acd494bSKees Cook 
8899acd494bSKees Cook failed:
8900d259f04SJohn Johansen 	aafs_remove_dir(fs_dir);
8910d259f04SJohn Johansen 
8929acd494bSKees Cook 	return error;
8939acd494bSKees Cook }
8949acd494bSKees Cook 
8959acd494bSKees Cook /**
8969acd494bSKees Cook  * aafs_remove_file - drop a single file entry in the apparmor securityfs
8979acd494bSKees Cook  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
8989acd494bSKees Cook  */
8999acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
9009acd494bSKees Cook {
9019acd494bSKees Cook 	if (!fs_file->dentry)
9029acd494bSKees Cook 		return;
9039acd494bSKees Cook 
9049acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
9059acd494bSKees Cook 	fs_file->dentry = NULL;
9069acd494bSKees Cook }
9079acd494bSKees Cook 
9089acd494bSKees Cook /**
9099acd494bSKees Cook  * aafs_remove_dir - recursively drop a directory entry from the securityfs
9109acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
9119acd494bSKees Cook  */
9129acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
9139acd494bSKees Cook {
9149acd494bSKees Cook 	struct aa_fs_entry *fs_file;
9159acd494bSKees Cook 
9160d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
9179acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
9189acd494bSKees Cook 			aafs_remove_dir(fs_file);
9199acd494bSKees Cook 		else
9209acd494bSKees Cook 			aafs_remove_file(fs_file);
9219acd494bSKees Cook 	}
9229acd494bSKees Cook 
9239acd494bSKees Cook 	aafs_remove_file(fs_dir);
92463e2b423SJohn Johansen }
92563e2b423SJohn Johansen 
92663e2b423SJohn Johansen /**
92763e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
92863e2b423SJohn Johansen  *
92963e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
93063e2b423SJohn Johansen  */
93163e2b423SJohn Johansen void __init aa_destroy_aafs(void)
93263e2b423SJohn Johansen {
9339acd494bSKees Cook 	aafs_remove_dir(&aa_fs_entry);
93463e2b423SJohn Johansen }
93563e2b423SJohn Johansen 
93663e2b423SJohn Johansen /**
93763e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
93863e2b423SJohn Johansen  *
93963e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
94063e2b423SJohn Johansen  *
94163e2b423SJohn Johansen  * Returns: error on failure
94263e2b423SJohn Johansen  */
9433417d8d5SJames Morris static int __init aa_create_aafs(void)
94463e2b423SJohn Johansen {
94563e2b423SJohn Johansen 	int error;
94663e2b423SJohn Johansen 
94763e2b423SJohn Johansen 	if (!apparmor_initialized)
94863e2b423SJohn Johansen 		return 0;
94963e2b423SJohn Johansen 
9509acd494bSKees Cook 	if (aa_fs_entry.dentry) {
95163e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
95263e2b423SJohn Johansen 		return -EEXIST;
95363e2b423SJohn Johansen 	}
95463e2b423SJohn Johansen 
9559acd494bSKees Cook 	/* Populate fs tree. */
9569acd494bSKees Cook 	error = aafs_create_dir(&aa_fs_entry, NULL);
95763e2b423SJohn Johansen 	if (error)
95863e2b423SJohn Johansen 		goto error;
95963e2b423SJohn Johansen 
96098849dffSJohn Johansen 	error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
9610d259f04SJohn Johansen 	if (error)
9620d259f04SJohn Johansen 		goto error;
9630d259f04SJohn Johansen 
96463e2b423SJohn Johansen 	/* TODO: add support for apparmorfs_null and apparmorfs_mnt */
96563e2b423SJohn Johansen 
96663e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
96763e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
96863e2b423SJohn Johansen 	return 0;
96963e2b423SJohn Johansen 
97063e2b423SJohn Johansen error:
97163e2b423SJohn Johansen 	aa_destroy_aafs();
97263e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
97363e2b423SJohn Johansen 	return error;
97463e2b423SJohn Johansen }
97563e2b423SJohn Johansen 
97663e2b423SJohn Johansen fs_initcall(aa_create_aafs);
977