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  * @op: operation doing the user buffer copy
8063e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
813ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
8263e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
8363e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
8463e2b423SJohn Johansen  *
8563e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
8663e2b423SJohn Johansen  *          ERR_PTR on failure.
8763e2b423SJohn Johansen  */
885ac8c355SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(int op,
895ac8c355SJohn Johansen 						     const char __user *userbuf,
905ac8c355SJohn Johansen 						     size_t alloc_size,
915ac8c355SJohn Johansen 						     size_t copy_size,
9263e2b423SJohn Johansen 						     loff_t *pos)
9363e2b423SJohn Johansen {
945ac8c355SJohn Johansen 	struct aa_loaddata *data;
9563e2b423SJohn Johansen 
963ed02adaSJohn Johansen 	BUG_ON(copy_size > alloc_size);
973ed02adaSJohn Johansen 
9863e2b423SJohn Johansen 	if (*pos != 0)
9963e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
10063e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
10163e2b423SJohn Johansen 
10263e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
1035ac8c355SJohn Johansen 	data = kvmalloc(sizeof(*data) + alloc_size);
10463e2b423SJohn Johansen 	if (data == NULL)
10563e2b423SJohn Johansen 		return ERR_PTR(-ENOMEM);
1065ac8c355SJohn Johansen 	kref_init(&data->count);
1075ac8c355SJohn Johansen 	data->size = copy_size;
1085ac8c355SJohn Johansen 	data->hash = NULL;
1095ac8c355SJohn Johansen 	data->abi = 0;
11063e2b423SJohn Johansen 
1115ac8c355SJohn Johansen 	if (copy_from_user(data->data, userbuf, copy_size)) {
11263e2b423SJohn Johansen 		kvfree(data);
11363e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
11463e2b423SJohn Johansen 	}
11563e2b423SJohn Johansen 
11663e2b423SJohn Johansen 	return data;
11763e2b423SJohn Johansen }
11863e2b423SJohn Johansen 
1195ac8c355SJohn Johansen static ssize_t policy_update(int binop, const char __user *buf, size_t size,
1205ac8c355SJohn Johansen 			     loff_t *pos)
1215ac8c355SJohn Johansen {
1225ac8c355SJohn Johansen 	ssize_t error;
1235ac8c355SJohn Johansen 	struct aa_loaddata *data;
1245ac8c355SJohn Johansen 	struct aa_profile *profile = aa_current_profile();
1255ac8c355SJohn Johansen 	int op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL;
1265ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
1275ac8c355SJohn Johansen 	 * below after unpack
1285ac8c355SJohn Johansen 	 */
1295ac8c355SJohn Johansen 	error = aa_may_manage_policy(profile, profile->ns, op);
1305ac8c355SJohn Johansen 	if (error)
1315ac8c355SJohn Johansen 		return error;
13263e2b423SJohn Johansen 
1335ac8c355SJohn Johansen 	data = aa_simple_write_to_buffer(op, buf, size, size, pos);
1345ac8c355SJohn Johansen 	error = PTR_ERR(data);
1355ac8c355SJohn Johansen 	if (!IS_ERR(data)) {
1365ac8c355SJohn Johansen 		error = aa_replace_profiles(profile->ns, binop, data);
1375ac8c355SJohn Johansen 		aa_put_loaddata(data);
1385ac8c355SJohn Johansen 	}
1395ac8c355SJohn Johansen 
1405ac8c355SJohn Johansen 	return error;
1415ac8c355SJohn Johansen }
1425ac8c355SJohn Johansen 
14363e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
14463e2b423SJohn Johansen 			    loff_t *pos)
14563e2b423SJohn Johansen {
1465ac8c355SJohn Johansen 	int error = policy_update(PROF_ADD, buf, size, pos);
14763e2b423SJohn Johansen 
14863e2b423SJohn Johansen 	return error;
14963e2b423SJohn Johansen }
15063e2b423SJohn Johansen 
15163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
1526038f373SArnd Bergmann 	.write = profile_load,
1536038f373SArnd Bergmann 	.llseek = default_llseek,
15463e2b423SJohn Johansen };
15563e2b423SJohn Johansen 
15663e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
15763e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
15863e2b423SJohn Johansen 			       size_t size, loff_t *pos)
15963e2b423SJohn Johansen {
1605ac8c355SJohn Johansen 	int error = policy_update(PROF_REPLACE, buf, size, pos);
16163e2b423SJohn Johansen 
16263e2b423SJohn Johansen 	return error;
16363e2b423SJohn Johansen }
16463e2b423SJohn Johansen 
16563e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
1666038f373SArnd Bergmann 	.write = profile_replace,
1676038f373SArnd Bergmann 	.llseek = default_llseek,
16863e2b423SJohn Johansen };
16963e2b423SJohn Johansen 
17063e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
17163e2b423SJohn Johansen 			      size_t size, loff_t *pos)
17263e2b423SJohn Johansen {
1735ac8c355SJohn Johansen 	struct aa_loaddata *data;
1745ac8c355SJohn Johansen 	struct aa_profile *profile;
17563e2b423SJohn Johansen 	ssize_t error;
17663e2b423SJohn Johansen 
1775ac8c355SJohn Johansen 	profile = aa_current_profile();
1785ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
1795ac8c355SJohn Johansen 	 * below after unpack
1805ac8c355SJohn Johansen 	 */
1815ac8c355SJohn Johansen 	error = aa_may_manage_policy(profile, profile->ns, OP_PROF_RM);
1825ac8c355SJohn Johansen 	if (error)
1835ac8c355SJohn Johansen 		goto out;
1845ac8c355SJohn Johansen 
18563e2b423SJohn Johansen 	/*
18663e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
18763e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
18863e2b423SJohn Johansen 	 */
1895ac8c355SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size,
1905ac8c355SJohn Johansen 					 pos);
19163e2b423SJohn Johansen 
19263e2b423SJohn Johansen 	error = PTR_ERR(data);
19363e2b423SJohn Johansen 	if (!IS_ERR(data)) {
1945ac8c355SJohn Johansen 		data->data[size] = 0;
1955ac8c355SJohn Johansen 		error = aa_remove_profiles(profile->ns, data->data, size);
1965ac8c355SJohn Johansen 		aa_put_loaddata(data);
19763e2b423SJohn Johansen 	}
1985ac8c355SJohn Johansen  out:
19963e2b423SJohn Johansen 	return error;
20063e2b423SJohn Johansen }
20163e2b423SJohn Johansen 
20263e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
2036038f373SArnd Bergmann 	.write = profile_remove,
2046038f373SArnd Bergmann 	.llseek = default_llseek,
20563e2b423SJohn Johansen };
20663e2b423SJohn Johansen 
207e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v)
208e74abcf3SKees Cook {
209e74abcf3SKees Cook 	struct aa_fs_entry *fs_file = seq->private;
210e74abcf3SKees Cook 
211e74abcf3SKees Cook 	if (!fs_file)
212e74abcf3SKees Cook 		return 0;
213e74abcf3SKees Cook 
214e74abcf3SKees Cook 	switch (fs_file->v_type) {
215e74abcf3SKees Cook 	case AA_FS_TYPE_BOOLEAN:
216e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
217e74abcf3SKees Cook 		break;
218a9bf8e9fSKees Cook 	case AA_FS_TYPE_STRING:
219a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
220a9bf8e9fSKees Cook 		break;
221e74abcf3SKees Cook 	case AA_FS_TYPE_U64:
222e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
223e74abcf3SKees Cook 		break;
224e74abcf3SKees Cook 	default:
225e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
226e74abcf3SKees Cook 		break;
227e74abcf3SKees Cook 	}
228e74abcf3SKees Cook 
229e74abcf3SKees Cook 	return 0;
230e74abcf3SKees Cook }
231e74abcf3SKees Cook 
232e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file)
233e74abcf3SKees Cook {
234e74abcf3SKees Cook 	return single_open(file, aa_fs_seq_show, inode->i_private);
235e74abcf3SKees Cook }
236e74abcf3SKees Cook 
237e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = {
238e74abcf3SKees Cook 	.owner		= THIS_MODULE,
239e74abcf3SKees Cook 	.open		= aa_fs_seq_open,
240e74abcf3SKees Cook 	.read		= seq_read,
241e74abcf3SKees Cook 	.llseek		= seq_lseek,
242e74abcf3SKees Cook 	.release	= single_release,
243e74abcf3SKees Cook };
244e74abcf3SKees Cook 
2450d259f04SJohn Johansen static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
2460d259f04SJohn Johansen 				  int (*show)(struct seq_file *, void *))
2470d259f04SJohn Johansen {
2488399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
2498399588aSJohn Johansen 	int error = single_open(file, show, proxy);
25063e2b423SJohn Johansen 
2510d259f04SJohn Johansen 	if (error) {
2520d259f04SJohn Johansen 		file->private_data = NULL;
2538399588aSJohn Johansen 		aa_put_proxy(proxy);
2540d259f04SJohn Johansen 	}
2550d259f04SJohn Johansen 
2560d259f04SJohn Johansen 	return error;
2570d259f04SJohn Johansen }
2580d259f04SJohn Johansen 
2590d259f04SJohn Johansen static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
2600d259f04SJohn Johansen {
2610d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
2620d259f04SJohn Johansen 	if (seq)
2638399588aSJohn Johansen 		aa_put_proxy(seq->private);
2640d259f04SJohn Johansen 	return single_release(inode, file);
2650d259f04SJohn Johansen }
2660d259f04SJohn Johansen 
2670d259f04SJohn Johansen static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
2680d259f04SJohn Johansen {
2698399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
2708399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
2710d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
2720d259f04SJohn Johansen 	aa_put_profile(profile);
2730d259f04SJohn Johansen 
2740d259f04SJohn Johansen 	return 0;
2750d259f04SJohn Johansen }
2760d259f04SJohn Johansen 
2770d259f04SJohn Johansen static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
2780d259f04SJohn Johansen {
2790d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
2800d259f04SJohn Johansen }
2810d259f04SJohn Johansen 
2820d259f04SJohn Johansen static const struct file_operations aa_fs_profname_fops = {
2830d259f04SJohn Johansen 	.owner		= THIS_MODULE,
2840d259f04SJohn Johansen 	.open		= aa_fs_seq_profname_open,
2850d259f04SJohn Johansen 	.read		= seq_read,
2860d259f04SJohn Johansen 	.llseek		= seq_lseek,
2870d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
2880d259f04SJohn Johansen };
2890d259f04SJohn Johansen 
2900d259f04SJohn Johansen static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
2910d259f04SJohn Johansen {
2928399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
2938399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
2940d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
2950d259f04SJohn Johansen 	aa_put_profile(profile);
2960d259f04SJohn Johansen 
2970d259f04SJohn Johansen 	return 0;
2980d259f04SJohn Johansen }
2990d259f04SJohn Johansen 
3000d259f04SJohn Johansen static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
3010d259f04SJohn Johansen {
3020d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
3030d259f04SJohn Johansen }
3040d259f04SJohn Johansen 
3050d259f04SJohn Johansen static const struct file_operations aa_fs_profmode_fops = {
3060d259f04SJohn Johansen 	.owner		= THIS_MODULE,
3070d259f04SJohn Johansen 	.open		= aa_fs_seq_profmode_open,
3080d259f04SJohn Johansen 	.read		= seq_read,
3090d259f04SJohn Johansen 	.llseek		= seq_lseek,
3100d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
3110d259f04SJohn Johansen };
3120d259f04SJohn Johansen 
313556d0be7SJohn Johansen static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
314556d0be7SJohn Johansen {
3158399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3168399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
317556d0be7SJohn Johansen 	if (profile->attach)
318556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
319556d0be7SJohn Johansen 	else if (profile->xmatch)
320556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
321556d0be7SJohn Johansen 	else
322556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
323556d0be7SJohn Johansen 	aa_put_profile(profile);
324556d0be7SJohn Johansen 
325556d0be7SJohn Johansen 	return 0;
326556d0be7SJohn Johansen }
327556d0be7SJohn Johansen 
328556d0be7SJohn Johansen static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
329556d0be7SJohn Johansen {
330556d0be7SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
331556d0be7SJohn Johansen }
332556d0be7SJohn Johansen 
333556d0be7SJohn Johansen static const struct file_operations aa_fs_profattach_fops = {
334556d0be7SJohn Johansen 	.owner		= THIS_MODULE,
335556d0be7SJohn Johansen 	.open		= aa_fs_seq_profattach_open,
336556d0be7SJohn Johansen 	.read		= seq_read,
337556d0be7SJohn Johansen 	.llseek		= seq_lseek,
338556d0be7SJohn Johansen 	.release	= aa_fs_seq_profile_release,
339556d0be7SJohn Johansen };
340556d0be7SJohn Johansen 
341f8eb8a13SJohn Johansen static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
342f8eb8a13SJohn Johansen {
3438399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3448399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
345f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
346f8eb8a13SJohn Johansen 
347f8eb8a13SJohn Johansen 	if (profile->hash) {
348f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
349f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
350f8eb8a13SJohn Johansen 		seq_puts(seq, "\n");
351f8eb8a13SJohn Johansen 	}
3520b938a2eSJohn Johansen 	aa_put_profile(profile);
353f8eb8a13SJohn Johansen 
354f8eb8a13SJohn Johansen 	return 0;
355f8eb8a13SJohn Johansen }
356f8eb8a13SJohn Johansen 
357f8eb8a13SJohn Johansen static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
358f8eb8a13SJohn Johansen {
359f8eb8a13SJohn Johansen 	return single_open(file, aa_fs_seq_hash_show, inode->i_private);
360f8eb8a13SJohn Johansen }
361f8eb8a13SJohn Johansen 
362f8eb8a13SJohn Johansen static const struct file_operations aa_fs_seq_hash_fops = {
363f8eb8a13SJohn Johansen 	.owner		= THIS_MODULE,
364f8eb8a13SJohn Johansen 	.open		= aa_fs_seq_hash_open,
365f8eb8a13SJohn Johansen 	.read		= seq_read,
366f8eb8a13SJohn Johansen 	.llseek		= seq_lseek,
367f8eb8a13SJohn Johansen 	.release	= single_release,
368f8eb8a13SJohn Johansen };
369f8eb8a13SJohn Johansen 
3703e3e5695SJohn Johansen 
371a71ada30SJohn Johansen static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v)
372a71ada30SJohn Johansen {
373a71ada30SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
374a71ada30SJohn Johansen 
375a71ada30SJohn Johansen 	seq_printf(seq, "%d\n", ns->level);
376a71ada30SJohn Johansen 
377a71ada30SJohn Johansen 	return 0;
378a71ada30SJohn Johansen }
379a71ada30SJohn Johansen 
380a71ada30SJohn Johansen static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file)
381a71ada30SJohn Johansen {
382a71ada30SJohn Johansen 	return single_open(file, aa_fs_seq_show_ns_level, inode->i_private);
383a71ada30SJohn Johansen }
384a71ada30SJohn Johansen 
385a71ada30SJohn Johansen static const struct file_operations aa_fs_ns_level = {
386a71ada30SJohn Johansen 	.owner		= THIS_MODULE,
387a71ada30SJohn Johansen 	.open		= aa_fs_seq_open_ns_level,
388a71ada30SJohn Johansen 	.read		= seq_read,
389a71ada30SJohn Johansen 	.llseek		= seq_lseek,
390a71ada30SJohn Johansen 	.release	= single_release,
391a71ada30SJohn Johansen };
392a71ada30SJohn Johansen 
3933e3e5695SJohn Johansen static int aa_fs_seq_show_ns_name(struct seq_file *seq, void *v)
3943e3e5695SJohn Johansen {
3953e3e5695SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
3963e3e5695SJohn Johansen 
3973e3e5695SJohn Johansen 	seq_printf(seq, "%s\n", ns->base.name);
3983e3e5695SJohn Johansen 
3993e3e5695SJohn Johansen 	return 0;
4003e3e5695SJohn Johansen }
4013e3e5695SJohn Johansen 
4023e3e5695SJohn Johansen static int aa_fs_seq_open_ns_name(struct inode *inode, struct file *file)
4033e3e5695SJohn Johansen {
4043e3e5695SJohn Johansen 	return single_open(file, aa_fs_seq_show_ns_name, inode->i_private);
4053e3e5695SJohn Johansen }
4063e3e5695SJohn Johansen 
4073e3e5695SJohn Johansen static const struct file_operations aa_fs_ns_name = {
4083e3e5695SJohn Johansen 	.owner		= THIS_MODULE,
4093e3e5695SJohn Johansen 	.open		= aa_fs_seq_open_ns_name,
4103e3e5695SJohn Johansen 	.read		= seq_read,
4113e3e5695SJohn Johansen 	.llseek		= seq_lseek,
4123e3e5695SJohn Johansen 	.release	= single_release,
4133e3e5695SJohn Johansen };
4143e3e5695SJohn Johansen 
4155ac8c355SJohn Johansen static int rawdata_release(struct inode *inode, struct file *file)
4165ac8c355SJohn Johansen {
4175ac8c355SJohn Johansen 	/* TODO: switch to loaddata when profile switched to symlink */
4185ac8c355SJohn Johansen 	aa_put_loaddata(file->private_data);
4195ac8c355SJohn Johansen 
4205ac8c355SJohn Johansen 	return 0;
4215ac8c355SJohn Johansen }
4225ac8c355SJohn Johansen 
4235ac8c355SJohn Johansen static int aa_fs_seq_raw_abi_show(struct seq_file *seq, void *v)
4245ac8c355SJohn Johansen {
4255ac8c355SJohn Johansen 	struct aa_proxy *proxy = seq->private;
4265ac8c355SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
4275ac8c355SJohn Johansen 
4285ac8c355SJohn Johansen 	if (profile->rawdata->abi) {
4295ac8c355SJohn Johansen 		seq_printf(seq, "v%d", profile->rawdata->abi);
4305ac8c355SJohn Johansen 		seq_puts(seq, "\n");
4315ac8c355SJohn Johansen 	}
4325ac8c355SJohn Johansen 	aa_put_profile(profile);
4335ac8c355SJohn Johansen 
4345ac8c355SJohn Johansen 	return 0;
4355ac8c355SJohn Johansen }
4365ac8c355SJohn Johansen 
4375ac8c355SJohn Johansen static int aa_fs_seq_raw_abi_open(struct inode *inode, struct file *file)
4385ac8c355SJohn Johansen {
4395ac8c355SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_abi_show);
4405ac8c355SJohn Johansen }
4415ac8c355SJohn Johansen 
4425ac8c355SJohn Johansen static const struct file_operations aa_fs_seq_raw_abi_fops = {
4435ac8c355SJohn Johansen 	.owner		= THIS_MODULE,
4445ac8c355SJohn Johansen 	.open		= aa_fs_seq_raw_abi_open,
4455ac8c355SJohn Johansen 	.read		= seq_read,
4465ac8c355SJohn Johansen 	.llseek		= seq_lseek,
4475ac8c355SJohn Johansen 	.release	= aa_fs_seq_profile_release,
4485ac8c355SJohn Johansen };
4495ac8c355SJohn Johansen 
4505ac8c355SJohn Johansen static int aa_fs_seq_raw_hash_show(struct seq_file *seq, void *v)
4515ac8c355SJohn Johansen {
4525ac8c355SJohn Johansen 	struct aa_proxy *proxy = seq->private;
4535ac8c355SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
4545ac8c355SJohn Johansen 	unsigned int i, size = aa_hash_size();
4555ac8c355SJohn Johansen 
4565ac8c355SJohn Johansen 	if (profile->rawdata->hash) {
4575ac8c355SJohn Johansen 		for (i = 0; i < size; i++)
4585ac8c355SJohn Johansen 			seq_printf(seq, "%.2x", profile->rawdata->hash[i]);
4595ac8c355SJohn Johansen 		seq_puts(seq, "\n");
4605ac8c355SJohn Johansen 	}
4615ac8c355SJohn Johansen 	aa_put_profile(profile);
4625ac8c355SJohn Johansen 
4635ac8c355SJohn Johansen 	return 0;
4645ac8c355SJohn Johansen }
4655ac8c355SJohn Johansen 
4665ac8c355SJohn Johansen static int aa_fs_seq_raw_hash_open(struct inode *inode, struct file *file)
4675ac8c355SJohn Johansen {
4685ac8c355SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_hash_show);
4695ac8c355SJohn Johansen }
4705ac8c355SJohn Johansen 
4715ac8c355SJohn Johansen static const struct file_operations aa_fs_seq_raw_hash_fops = {
4725ac8c355SJohn Johansen 	.owner		= THIS_MODULE,
4735ac8c355SJohn Johansen 	.open		= aa_fs_seq_raw_hash_open,
4745ac8c355SJohn Johansen 	.read		= seq_read,
4755ac8c355SJohn Johansen 	.llseek		= seq_lseek,
4765ac8c355SJohn Johansen 	.release	= aa_fs_seq_profile_release,
4775ac8c355SJohn Johansen };
4785ac8c355SJohn Johansen 
4795ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
4805ac8c355SJohn Johansen 			    loff_t *ppos)
4815ac8c355SJohn Johansen {
4825ac8c355SJohn Johansen 	struct aa_loaddata *rawdata = file->private_data;
4835ac8c355SJohn Johansen 
4845ac8c355SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, rawdata->data,
4855ac8c355SJohn Johansen 				       rawdata->size);
4865ac8c355SJohn Johansen }
4875ac8c355SJohn Johansen 
4885ac8c355SJohn Johansen static int rawdata_open(struct inode *inode, struct file *file)
4895ac8c355SJohn Johansen {
4905ac8c355SJohn Johansen 	struct aa_proxy *proxy = inode->i_private;
4915ac8c355SJohn Johansen 	struct aa_profile *profile;
4925ac8c355SJohn Johansen 
4935ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
4945ac8c355SJohn Johansen 		return -EACCES;
4955ac8c355SJohn Johansen 	profile = aa_get_profile_rcu(&proxy->profile);
4965ac8c355SJohn Johansen 	file->private_data = aa_get_loaddata(profile->rawdata);
4975ac8c355SJohn Johansen 	aa_put_profile(profile);
4985ac8c355SJohn Johansen 
4995ac8c355SJohn Johansen 	return 0;
5005ac8c355SJohn Johansen }
5015ac8c355SJohn Johansen 
5025ac8c355SJohn Johansen static const struct file_operations aa_fs_rawdata_fops = {
5035ac8c355SJohn Johansen 	.open = rawdata_open,
5045ac8c355SJohn Johansen 	.read = rawdata_read,
5055ac8c355SJohn Johansen 	.llseek = generic_file_llseek,
5065ac8c355SJohn Johansen 	.release = rawdata_release,
5075ac8c355SJohn Johansen };
5085ac8c355SJohn Johansen 
5090d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
5100d259f04SJohn Johansen void __aa_fs_profile_rmdir(struct aa_profile *profile)
5110d259f04SJohn Johansen {
5120d259f04SJohn Johansen 	struct aa_profile *child;
5130d259f04SJohn Johansen 	int i;
5140d259f04SJohn Johansen 
5150d259f04SJohn Johansen 	if (!profile)
5160d259f04SJohn Johansen 		return;
5170d259f04SJohn Johansen 
5180d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
5190d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
5200d259f04SJohn Johansen 
5210d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
5228399588aSJohn Johansen 		struct aa_proxy *proxy;
5230d259f04SJohn Johansen 		if (!profile->dents[i])
5240d259f04SJohn Johansen 			continue;
5250d259f04SJohn Johansen 
5268399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
5270d259f04SJohn Johansen 		securityfs_remove(profile->dents[i]);
5288399588aSJohn Johansen 		aa_put_proxy(proxy);
5290d259f04SJohn Johansen 		profile->dents[i] = NULL;
5300d259f04SJohn Johansen 	}
5310d259f04SJohn Johansen }
5320d259f04SJohn Johansen 
5330d259f04SJohn Johansen void __aa_fs_profile_migrate_dents(struct aa_profile *old,
5340d259f04SJohn Johansen 				   struct aa_profile *new)
5350d259f04SJohn Johansen {
5360d259f04SJohn Johansen 	int i;
5370d259f04SJohn Johansen 
5380d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
5390d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
540d671e890SJohn Johansen 		if (new->dents[i])
541078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
5420d259f04SJohn Johansen 		old->dents[i] = NULL;
5430d259f04SJohn Johansen 	}
5440d259f04SJohn Johansen }
5450d259f04SJohn Johansen 
5460d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
5470d259f04SJohn Johansen 					  struct aa_profile *profile,
5480d259f04SJohn Johansen 					  const struct file_operations *fops)
5490d259f04SJohn Johansen {
5508399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
5510d259f04SJohn Johansen 	struct dentry *dent;
5520d259f04SJohn Johansen 
5538399588aSJohn Johansen 	dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
5540d259f04SJohn Johansen 	if (IS_ERR(dent))
5558399588aSJohn Johansen 		aa_put_proxy(proxy);
5560d259f04SJohn Johansen 
5570d259f04SJohn Johansen 	return dent;
5580d259f04SJohn Johansen }
5590d259f04SJohn Johansen 
5600d259f04SJohn Johansen /* requires lock be held */
5610d259f04SJohn Johansen int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
5620d259f04SJohn Johansen {
5630d259f04SJohn Johansen 	struct aa_profile *child;
5640d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
5650d259f04SJohn Johansen 	int error;
5660d259f04SJohn Johansen 
5670d259f04SJohn Johansen 	if (!parent) {
5680d259f04SJohn Johansen 		struct aa_profile *p;
5690d259f04SJohn Johansen 		p = aa_deref_parent(profile);
5700d259f04SJohn Johansen 		dent = prof_dir(p);
5710d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
5720d259f04SJohn Johansen 		dent = securityfs_create_dir("profiles", dent);
5730d259f04SJohn Johansen 		if (IS_ERR(dent))
5740d259f04SJohn Johansen 			goto fail;
5750d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
5760d259f04SJohn Johansen 	}
5770d259f04SJohn Johansen 
5780d259f04SJohn Johansen 	if (!profile->dirname) {
5790d259f04SJohn Johansen 		int len, id_len;
5800d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
5810d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
5820d259f04SJohn Johansen 
5830d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
5840d259f04SJohn Johansen 		if (!profile->dirname)
5850d259f04SJohn Johansen 			goto fail;
5860d259f04SJohn Johansen 
5870d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
5880d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
5890d259f04SJohn Johansen 	}
5900d259f04SJohn Johansen 
5910d259f04SJohn Johansen 	dent = securityfs_create_dir(profile->dirname, parent);
5920d259f04SJohn Johansen 	if (IS_ERR(dent))
5930d259f04SJohn Johansen 		goto fail;
5940d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
5950d259f04SJohn Johansen 
5960d259f04SJohn Johansen 	dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
5970d259f04SJohn Johansen 	if (IS_ERR(dent))
5980d259f04SJohn Johansen 		goto fail;
5990d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
6000d259f04SJohn Johansen 
6010d259f04SJohn Johansen 	dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
6020d259f04SJohn Johansen 	if (IS_ERR(dent))
6030d259f04SJohn Johansen 		goto fail;
6040d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
6050d259f04SJohn Johansen 
606556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
607556d0be7SJohn Johansen 				   &aa_fs_profattach_fops);
608556d0be7SJohn Johansen 	if (IS_ERR(dent))
609556d0be7SJohn Johansen 		goto fail;
610556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
611556d0be7SJohn Johansen 
612f8eb8a13SJohn Johansen 	if (profile->hash) {
613f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
614f8eb8a13SJohn Johansen 					   &aa_fs_seq_hash_fops);
615f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
616f8eb8a13SJohn Johansen 			goto fail;
617f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
618f8eb8a13SJohn Johansen 	}
619f8eb8a13SJohn Johansen 
6205ac8c355SJohn Johansen 	if (profile->rawdata) {
6215ac8c355SJohn Johansen 		dent = create_profile_file(dir, "raw_sha1", profile,
6225ac8c355SJohn Johansen 					   &aa_fs_seq_raw_hash_fops);
6235ac8c355SJohn Johansen 		if (IS_ERR(dent))
6245ac8c355SJohn Johansen 			goto fail;
6255ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
6265ac8c355SJohn Johansen 
6275ac8c355SJohn Johansen 		dent = create_profile_file(dir, "raw_abi", profile,
6285ac8c355SJohn Johansen 					   &aa_fs_seq_raw_abi_fops);
6295ac8c355SJohn Johansen 		if (IS_ERR(dent))
6305ac8c355SJohn Johansen 			goto fail;
6315ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
6325ac8c355SJohn Johansen 
6335ac8c355SJohn Johansen 		dent = securityfs_create_file("raw_data", S_IFREG | 0444, dir,
6345ac8c355SJohn Johansen 					      profile->proxy,
6355ac8c355SJohn Johansen 					      &aa_fs_rawdata_fops);
6365ac8c355SJohn Johansen 		if (IS_ERR(dent))
6375ac8c355SJohn Johansen 			goto fail;
6385ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
6395ac8c355SJohn Johansen 		d_inode(dent)->i_size = profile->rawdata->size;
6405ac8c355SJohn Johansen 		aa_get_proxy(profile->proxy);
6415ac8c355SJohn Johansen 	}
6425ac8c355SJohn Johansen 
6430d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
6440d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
6450d259f04SJohn Johansen 		if (error)
6460d259f04SJohn Johansen 			goto fail2;
6470d259f04SJohn Johansen 	}
6480d259f04SJohn Johansen 
6490d259f04SJohn Johansen 	return 0;
6500d259f04SJohn Johansen 
6510d259f04SJohn Johansen fail:
6520d259f04SJohn Johansen 	error = PTR_ERR(dent);
6530d259f04SJohn Johansen 
6540d259f04SJohn Johansen fail2:
6550d259f04SJohn Johansen 	__aa_fs_profile_rmdir(profile);
6560d259f04SJohn Johansen 
6570d259f04SJohn Johansen 	return error;
6580d259f04SJohn Johansen }
6590d259f04SJohn Johansen 
66098849dffSJohn Johansen void __aa_fs_ns_rmdir(struct aa_ns *ns)
6610d259f04SJohn Johansen {
66298849dffSJohn Johansen 	struct aa_ns *sub;
6630d259f04SJohn Johansen 	struct aa_profile *child;
6640d259f04SJohn Johansen 	int i;
6650d259f04SJohn Johansen 
6660d259f04SJohn Johansen 	if (!ns)
6670d259f04SJohn Johansen 		return;
6680d259f04SJohn Johansen 
6690d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
6700d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
6710d259f04SJohn Johansen 
6720d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
6730d259f04SJohn Johansen 		mutex_lock(&sub->lock);
67498849dffSJohn Johansen 		__aa_fs_ns_rmdir(sub);
6750d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
6760d259f04SJohn Johansen 	}
6770d259f04SJohn Johansen 
6780d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
6790d259f04SJohn Johansen 		securityfs_remove(ns->dents[i]);
6800d259f04SJohn Johansen 		ns->dents[i] = NULL;
6810d259f04SJohn Johansen 	}
6820d259f04SJohn Johansen }
6830d259f04SJohn Johansen 
68498849dffSJohn Johansen int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
6850d259f04SJohn Johansen {
68698849dffSJohn Johansen 	struct aa_ns *sub;
6870d259f04SJohn Johansen 	struct aa_profile *child;
6880d259f04SJohn Johansen 	struct dentry *dent, *dir;
6890d259f04SJohn Johansen 	int error;
6900d259f04SJohn Johansen 
6910d259f04SJohn Johansen 	if (!name)
6920d259f04SJohn Johansen 		name = ns->base.name;
6930d259f04SJohn Johansen 
6940d259f04SJohn Johansen 	dent = securityfs_create_dir(name, parent);
6950d259f04SJohn Johansen 	if (IS_ERR(dent))
6960d259f04SJohn Johansen 		goto fail;
6970d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
6980d259f04SJohn Johansen 
6990d259f04SJohn Johansen 	dent = securityfs_create_dir("profiles", dir);
7000d259f04SJohn Johansen 	if (IS_ERR(dent))
7010d259f04SJohn Johansen 		goto fail;
7020d259f04SJohn Johansen 	ns_subprofs_dir(ns) = dent;
7030d259f04SJohn Johansen 
7040d259f04SJohn Johansen 	dent = securityfs_create_dir("namespaces", dir);
7050d259f04SJohn Johansen 	if (IS_ERR(dent))
7060d259f04SJohn Johansen 		goto fail;
7070d259f04SJohn Johansen 	ns_subns_dir(ns) = dent;
7080d259f04SJohn Johansen 
7090d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
7100d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
7110d259f04SJohn Johansen 		if (error)
7120d259f04SJohn Johansen 			goto fail2;
7130d259f04SJohn Johansen 	}
7140d259f04SJohn Johansen 
7150d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
7160d259f04SJohn Johansen 		mutex_lock(&sub->lock);
71798849dffSJohn Johansen 		error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
7180d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
7190d259f04SJohn Johansen 		if (error)
7200d259f04SJohn Johansen 			goto fail2;
7210d259f04SJohn Johansen 	}
7220d259f04SJohn Johansen 
7230d259f04SJohn Johansen 	return 0;
7240d259f04SJohn Johansen 
7250d259f04SJohn Johansen fail:
7260d259f04SJohn Johansen 	error = PTR_ERR(dent);
7270d259f04SJohn Johansen 
7280d259f04SJohn Johansen fail2:
72998849dffSJohn Johansen 	__aa_fs_ns_rmdir(ns);
7300d259f04SJohn Johansen 
7310d259f04SJohn Johansen 	return error;
7320d259f04SJohn Johansen }
7330d259f04SJohn Johansen 
7340d259f04SJohn Johansen 
73529b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
73629b3822fSJohn Johansen 
73729b3822fSJohn Johansen /**
73898849dffSJohn Johansen  * __next_ns - find the next namespace to list
73929b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
74029b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
74129b3822fSJohn Johansen  *
74229b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
74329b3822fSJohn Johansen  * while switching current namespace.
74429b3822fSJohn Johansen  *
74529b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
74629b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
74729b3822fSJohn Johansen  * NOTE: will not unlock root->lock
74829b3822fSJohn Johansen  */
74998849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
75029b3822fSJohn Johansen {
75198849dffSJohn Johansen 	struct aa_ns *parent, *next;
75229b3822fSJohn Johansen 
75329b3822fSJohn Johansen 	/* is next namespace a child */
75429b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
75529b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
75629b3822fSJohn Johansen 		mutex_lock(&next->lock);
75729b3822fSJohn Johansen 		return next;
75829b3822fSJohn Johansen 	}
75929b3822fSJohn Johansen 
76029b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
76129b3822fSJohn Johansen 	parent = ns->parent;
762ed2c7da3SJohn Johansen 	while (ns != root) {
76329b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
76438dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
76529b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
76629b3822fSJohn Johansen 			mutex_lock(&next->lock);
76729b3822fSJohn Johansen 			return next;
76829b3822fSJohn Johansen 		}
76929b3822fSJohn Johansen 		ns = parent;
77029b3822fSJohn Johansen 		parent = parent->parent;
77129b3822fSJohn Johansen 	}
77229b3822fSJohn Johansen 
77329b3822fSJohn Johansen 	return NULL;
77429b3822fSJohn Johansen }
77529b3822fSJohn Johansen 
77629b3822fSJohn Johansen /**
77729b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
77829b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
77929b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
78029b3822fSJohn Johansen  *
78129b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
78229b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
78329b3822fSJohn Johansen  */
78498849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
78598849dffSJohn Johansen 					  struct aa_ns *ns)
78629b3822fSJohn Johansen {
78798849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
78829b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
78929b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
79029b3822fSJohn Johansen 						struct aa_profile, base.list);
79129b3822fSJohn Johansen 	}
79229b3822fSJohn Johansen 	return NULL;
79329b3822fSJohn Johansen }
79429b3822fSJohn Johansen 
79529b3822fSJohn Johansen /**
79629b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
79729b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
79829b3822fSJohn Johansen  *
79929b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
80029b3822fSJohn Johansen  *
80129b3822fSJohn Johansen  * Returns: next profile or NULL if done
80229b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
80329b3822fSJohn Johansen  */
80429b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
80529b3822fSJohn Johansen {
80629b3822fSJohn Johansen 	struct aa_profile *parent;
80798849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
80829b3822fSJohn Johansen 
80929b3822fSJohn Johansen 	/* is next profile a child */
81029b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
81129b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
81229b3822fSJohn Johansen 					base.list);
81329b3822fSJohn Johansen 
81429b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
81529b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
81629b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
81729b3822fSJohn Johansen 	while (parent) {
81838dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
81929b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
82029b3822fSJohn Johansen 			return p;
82129b3822fSJohn Johansen 		p = parent;
82229b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
82329b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
82429b3822fSJohn Johansen 	}
82529b3822fSJohn Johansen 
82629b3822fSJohn Johansen 	/* is next another profile in the namespace */
82738dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
82829b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
82929b3822fSJohn Johansen 		return p;
83029b3822fSJohn Johansen 
83129b3822fSJohn Johansen 	return NULL;
83229b3822fSJohn Johansen }
83329b3822fSJohn Johansen 
83429b3822fSJohn Johansen /**
83529b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
83629b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
83729b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
83829b3822fSJohn Johansen  *
83929b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
84029b3822fSJohn Johansen  */
84198849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
84229b3822fSJohn Johansen 				       struct aa_profile *profile)
84329b3822fSJohn Johansen {
84429b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
84529b3822fSJohn Johansen 	if (next)
84629b3822fSJohn Johansen 		return next;
84729b3822fSJohn Johansen 
84829b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
84998849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
85029b3822fSJohn Johansen }
85129b3822fSJohn Johansen 
85229b3822fSJohn Johansen /**
85329b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
85429b3822fSJohn Johansen  * @f: seq_file to fill
85529b3822fSJohn Johansen  * @pos: current position
85629b3822fSJohn Johansen  *
85729b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
85829b3822fSJohn Johansen  *
85929b3822fSJohn Johansen  * acquires first ns->lock
86029b3822fSJohn Johansen  */
86129b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
86229b3822fSJohn Johansen {
86329b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
86498849dffSJohn Johansen 	struct aa_ns *root = aa_current_profile()->ns;
86529b3822fSJohn Johansen 	loff_t l = *pos;
86698849dffSJohn Johansen 	f->private = aa_get_ns(root);
86729b3822fSJohn Johansen 
86829b3822fSJohn Johansen 
86929b3822fSJohn Johansen 	/* find the first profile */
87029b3822fSJohn Johansen 	mutex_lock(&root->lock);
87129b3822fSJohn Johansen 	profile = __first_profile(root, root);
87229b3822fSJohn Johansen 
87329b3822fSJohn Johansen 	/* skip to position */
87429b3822fSJohn Johansen 	for (; profile && l > 0; l--)
87529b3822fSJohn Johansen 		profile = next_profile(root, profile);
87629b3822fSJohn Johansen 
87729b3822fSJohn Johansen 	return profile;
87829b3822fSJohn Johansen }
87929b3822fSJohn Johansen 
88029b3822fSJohn Johansen /**
88129b3822fSJohn Johansen  * p_next - read the next profile entry
88229b3822fSJohn Johansen  * @f: seq_file to fill
88329b3822fSJohn Johansen  * @p: profile previously returned
88429b3822fSJohn Johansen  * @pos: current position
88529b3822fSJohn Johansen  *
88629b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
88729b3822fSJohn Johansen  *
88829b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
88929b3822fSJohn Johansen  */
89029b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
89129b3822fSJohn Johansen {
89229b3822fSJohn Johansen 	struct aa_profile *profile = p;
89398849dffSJohn Johansen 	struct aa_ns *ns = f->private;
89429b3822fSJohn Johansen 	(*pos)++;
89529b3822fSJohn Johansen 
89629b3822fSJohn Johansen 	return next_profile(ns, profile);
89729b3822fSJohn Johansen }
89829b3822fSJohn Johansen 
89929b3822fSJohn Johansen /**
90029b3822fSJohn Johansen  * p_stop - stop depth first traversal
90129b3822fSJohn Johansen  * @f: seq_file we are filling
90229b3822fSJohn Johansen  * @p: the last profile writen
90329b3822fSJohn Johansen  *
90429b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
90529b3822fSJohn Johansen  */
90629b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
90729b3822fSJohn Johansen {
90829b3822fSJohn Johansen 	struct aa_profile *profile = p;
90998849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
91029b3822fSJohn Johansen 
91129b3822fSJohn Johansen 	if (profile) {
91229b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
91329b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
91429b3822fSJohn Johansen 	}
91529b3822fSJohn Johansen 	mutex_unlock(&root->lock);
91698849dffSJohn Johansen 	aa_put_ns(root);
91729b3822fSJohn Johansen }
91829b3822fSJohn Johansen 
91929b3822fSJohn Johansen /**
92029b3822fSJohn Johansen  * seq_show_profile - show a profile entry
92129b3822fSJohn Johansen  * @f: seq_file to file
92229b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
92329b3822fSJohn Johansen  *
92429b3822fSJohn Johansen  * Returns: error on failure
92529b3822fSJohn Johansen  */
92629b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
92729b3822fSJohn Johansen {
92829b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
92998849dffSJohn Johansen 	struct aa_ns *root = f->private;
93029b3822fSJohn Johansen 
93129b3822fSJohn Johansen 	if (profile->ns != root)
93292b6d8efSJohn Johansen 		seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
93329b3822fSJohn Johansen 	seq_printf(f, "%s (%s)\n", profile->base.hname,
93429b3822fSJohn Johansen 		   aa_profile_mode_names[profile->mode]);
93529b3822fSJohn Johansen 
93629b3822fSJohn Johansen 	return 0;
93729b3822fSJohn Johansen }
93829b3822fSJohn Johansen 
93929b3822fSJohn Johansen static const struct seq_operations aa_fs_profiles_op = {
94029b3822fSJohn Johansen 	.start = p_start,
94129b3822fSJohn Johansen 	.next = p_next,
94229b3822fSJohn Johansen 	.stop = p_stop,
94329b3822fSJohn Johansen 	.show = seq_show_profile,
94429b3822fSJohn Johansen };
94529b3822fSJohn Johansen 
94629b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
94729b3822fSJohn Johansen {
9485ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
9495ac8c355SJohn Johansen 		return -EACCES;
9505ac8c355SJohn Johansen 
95129b3822fSJohn Johansen 	return seq_open(file, &aa_fs_profiles_op);
95229b3822fSJohn Johansen }
95329b3822fSJohn Johansen 
95429b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
95529b3822fSJohn Johansen {
95629b3822fSJohn Johansen 	return seq_release(inode, file);
95729b3822fSJohn Johansen }
95829b3822fSJohn Johansen 
95929b3822fSJohn Johansen static const struct file_operations aa_fs_profiles_fops = {
96029b3822fSJohn Johansen 	.open = profiles_open,
96129b3822fSJohn Johansen 	.read = seq_read,
96229b3822fSJohn Johansen 	.llseek = seq_lseek,
96329b3822fSJohn Johansen 	.release = profiles_release,
96429b3822fSJohn Johansen };
96529b3822fSJohn Johansen 
96629b3822fSJohn Johansen 
9670d259f04SJohn Johansen /** Base file system setup **/
968a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = {
969a9bf8e9fSKees Cook 	AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
970a9bf8e9fSKees Cook 				  "link lock"),
971a9bf8e9fSKees Cook 	{ }
972a9bf8e9fSKees Cook };
973a9bf8e9fSKees Cook 
974e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = {
975e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hat",	1),
976e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hatv",	1),
977e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_onexec",	1),
978e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_profile",	1),
97934c426acSJohn Johansen 	AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
980e74abcf3SKees Cook 	{ }
981e74abcf3SKees Cook };
982e74abcf3SKees Cook 
983474d6b75SJohn Johansen static struct aa_fs_entry aa_fs_entry_versions[] = {
984474d6b75SJohn Johansen 	AA_FS_FILE_BOOLEAN("v5",	1),
985474d6b75SJohn Johansen 	{ }
986474d6b75SJohn Johansen };
987474d6b75SJohn Johansen 
9889d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = {
989474d6b75SJohn Johansen 	AA_FS_DIR("versions",                   aa_fs_entry_versions),
990dd51c848SJohn Johansen 	AA_FS_FILE_BOOLEAN("set_load",		1),
9919d910a3bSJohn Johansen 	{ }
9929d910a3bSJohn Johansen };
9939d910a3bSJohn Johansen 
994e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = {
9959d910a3bSJohn Johansen 	AA_FS_DIR("policy",			aa_fs_entry_policy),
996e74abcf3SKees Cook 	AA_FS_DIR("domain",			aa_fs_entry_domain),
997a9bf8e9fSKees Cook 	AA_FS_DIR("file",			aa_fs_entry_file),
998e74abcf3SKees Cook 	AA_FS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
999d384b0a1SKees Cook 	AA_FS_DIR("rlimit",			aa_fs_entry_rlimit),
100084f1f787SJohn Johansen 	AA_FS_DIR("caps",			aa_fs_entry_caps),
1001e74abcf3SKees Cook 	{ }
1002e74abcf3SKees Cook };
1003e74abcf3SKees Cook 
10049acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = {
10059acd494bSKees Cook 	AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
10069acd494bSKees Cook 	AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
10079acd494bSKees Cook 	AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
1008a71ada30SJohn Johansen 	AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level),
10093e3e5695SJohn Johansen 	AA_FS_FILE_FOPS(".ns_name", 0640, &aa_fs_ns_name),
101029b3822fSJohn Johansen 	AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
1011e74abcf3SKees Cook 	AA_FS_DIR("features", aa_fs_entry_features),
10129acd494bSKees Cook 	{ }
10139acd494bSKees Cook };
101463e2b423SJohn Johansen 
10159acd494bSKees Cook static struct aa_fs_entry aa_fs_entry =
10169acd494bSKees Cook 	AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
10179acd494bSKees Cook 
10189acd494bSKees Cook /**
10199acd494bSKees Cook  * aafs_create_file - create a file entry in the apparmor securityfs
10209acd494bSKees Cook  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
10219acd494bSKees Cook  * @parent: the parent dentry in the securityfs
10229acd494bSKees Cook  *
10239acd494bSKees Cook  * Use aafs_remove_file to remove entries created with this fn.
10249acd494bSKees Cook  */
10259acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file,
10269acd494bSKees Cook 				   struct dentry *parent)
102763e2b423SJohn Johansen {
10289acd494bSKees Cook 	int error = 0;
102963e2b423SJohn Johansen 
10309acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
10319acd494bSKees Cook 						 S_IFREG | fs_file->mode,
10329acd494bSKees Cook 						 parent, fs_file,
10339acd494bSKees Cook 						 fs_file->file_ops);
10349acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
10359acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
10369acd494bSKees Cook 		fs_file->dentry = NULL;
103763e2b423SJohn Johansen 	}
10389acd494bSKees Cook 	return error;
103963e2b423SJohn Johansen }
104063e2b423SJohn Johansen 
10410d259f04SJohn Johansen static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
104263e2b423SJohn Johansen /**
10439acd494bSKees Cook  * aafs_create_dir - recursively create a directory entry in the securityfs
10449acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
10459acd494bSKees Cook  * @parent: the parent dentry in the securityfs
104663e2b423SJohn Johansen  *
10479acd494bSKees Cook  * Use aafs_remove_dir to remove entries created with this fn.
104863e2b423SJohn Johansen  */
10499acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
10509acd494bSKees Cook 				  struct dentry *parent)
105163e2b423SJohn Johansen {
10529acd494bSKees Cook 	struct aa_fs_entry *fs_file;
10530d259f04SJohn Johansen 	struct dentry *dir;
10540d259f04SJohn Johansen 	int error;
105563e2b423SJohn Johansen 
10560d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
10570d259f04SJohn Johansen 	if (IS_ERR(dir))
10580d259f04SJohn Johansen 		return PTR_ERR(dir);
10590d259f04SJohn Johansen 	fs_dir->dentry = dir;
106063e2b423SJohn Johansen 
10610d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
10629acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
10639acd494bSKees Cook 			error = aafs_create_dir(fs_file, fs_dir->dentry);
10649acd494bSKees Cook 		else
10659acd494bSKees Cook 			error = aafs_create_file(fs_file, fs_dir->dentry);
10669acd494bSKees Cook 		if (error)
10679acd494bSKees Cook 			goto failed;
10689acd494bSKees Cook 	}
10699acd494bSKees Cook 
10709acd494bSKees Cook 	return 0;
10719acd494bSKees Cook 
10729acd494bSKees Cook failed:
10730d259f04SJohn Johansen 	aafs_remove_dir(fs_dir);
10740d259f04SJohn Johansen 
10759acd494bSKees Cook 	return error;
10769acd494bSKees Cook }
10779acd494bSKees Cook 
10789acd494bSKees Cook /**
10799acd494bSKees Cook  * aafs_remove_file - drop a single file entry in the apparmor securityfs
10809acd494bSKees Cook  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
10819acd494bSKees Cook  */
10829acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
10839acd494bSKees Cook {
10849acd494bSKees Cook 	if (!fs_file->dentry)
10859acd494bSKees Cook 		return;
10869acd494bSKees Cook 
10879acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
10889acd494bSKees Cook 	fs_file->dentry = NULL;
10899acd494bSKees Cook }
10909acd494bSKees Cook 
10919acd494bSKees Cook /**
10929acd494bSKees Cook  * aafs_remove_dir - recursively drop a directory entry from the securityfs
10939acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
10949acd494bSKees Cook  */
10959acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
10969acd494bSKees Cook {
10979acd494bSKees Cook 	struct aa_fs_entry *fs_file;
10989acd494bSKees Cook 
10990d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
11009acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
11019acd494bSKees Cook 			aafs_remove_dir(fs_file);
11029acd494bSKees Cook 		else
11039acd494bSKees Cook 			aafs_remove_file(fs_file);
11049acd494bSKees Cook 	}
11059acd494bSKees Cook 
11069acd494bSKees Cook 	aafs_remove_file(fs_dir);
110763e2b423SJohn Johansen }
110863e2b423SJohn Johansen 
110963e2b423SJohn Johansen /**
111063e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
111163e2b423SJohn Johansen  *
111263e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
111363e2b423SJohn Johansen  */
111463e2b423SJohn Johansen void __init aa_destroy_aafs(void)
111563e2b423SJohn Johansen {
11169acd494bSKees Cook 	aafs_remove_dir(&aa_fs_entry);
111763e2b423SJohn Johansen }
111863e2b423SJohn Johansen 
1119a71ada30SJohn Johansen 
1120a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
1121a71ada30SJohn Johansen struct path aa_null;
1122a71ada30SJohn Johansen 
1123a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
1124a71ada30SJohn Johansen {
1125a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
1126a71ada30SJohn Johansen 	struct dentry *dentry;
1127a71ada30SJohn Johansen 	struct inode *inode;
1128a71ada30SJohn Johansen 	int count = 0;
1129a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
1130a71ada30SJohn Johansen 
1131a71ada30SJohn Johansen 	if (error)
1132a71ada30SJohn Johansen 		return error;
1133a71ada30SJohn Johansen 
1134a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
1135a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
1136a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
1137a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
1138a71ada30SJohn Johansen 		goto out;
1139a71ada30SJohn Johansen 	}
1140a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
1141a71ada30SJohn Johansen 	if (!inode) {
1142a71ada30SJohn Johansen 		error = -ENOMEM;
1143a71ada30SJohn Johansen 		goto out1;
1144a71ada30SJohn Johansen 	}
1145a71ada30SJohn Johansen 
1146a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
1147a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
1148a71ada30SJohn Johansen 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1149a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
1150a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
1151a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
1152a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
1153a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
1154a71ada30SJohn Johansen 
1155a71ada30SJohn Johansen 	error = 0;
1156a71ada30SJohn Johansen 
1157a71ada30SJohn Johansen out1:
1158a71ada30SJohn Johansen 	dput(dentry);
1159a71ada30SJohn Johansen out:
1160a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
1161a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
1162a71ada30SJohn Johansen 	return error;
1163a71ada30SJohn Johansen }
1164a71ada30SJohn Johansen 
116563e2b423SJohn Johansen /**
116663e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
116763e2b423SJohn Johansen  *
116863e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
116963e2b423SJohn Johansen  *
117063e2b423SJohn Johansen  * Returns: error on failure
117163e2b423SJohn Johansen  */
11723417d8d5SJames Morris static int __init aa_create_aafs(void)
117363e2b423SJohn Johansen {
117463e2b423SJohn Johansen 	int error;
117563e2b423SJohn Johansen 
117663e2b423SJohn Johansen 	if (!apparmor_initialized)
117763e2b423SJohn Johansen 		return 0;
117863e2b423SJohn Johansen 
11799acd494bSKees Cook 	if (aa_fs_entry.dentry) {
118063e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
118163e2b423SJohn Johansen 		return -EEXIST;
118263e2b423SJohn Johansen 	}
118363e2b423SJohn Johansen 
11849acd494bSKees Cook 	/* Populate fs tree. */
11859acd494bSKees Cook 	error = aafs_create_dir(&aa_fs_entry, NULL);
118663e2b423SJohn Johansen 	if (error)
118763e2b423SJohn Johansen 		goto error;
118863e2b423SJohn Johansen 
118998849dffSJohn Johansen 	error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
11900d259f04SJohn Johansen 	if (error)
11910d259f04SJohn Johansen 		goto error;
11920d259f04SJohn Johansen 
1193a71ada30SJohn Johansen 	error = aa_mk_null_file(aa_fs_entry.dentry);
1194a71ada30SJohn Johansen 	if (error)
1195a71ada30SJohn Johansen 		goto error;
1196a71ada30SJohn Johansen 
1197a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
119863e2b423SJohn Johansen 
119963e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
120063e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
120163e2b423SJohn Johansen 	return 0;
120263e2b423SJohn Johansen 
120363e2b423SJohn Johansen error:
120463e2b423SJohn Johansen 	aa_destroy_aafs();
120563e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
120663e2b423SJohn Johansen 	return error;
120763e2b423SJohn Johansen }
120863e2b423SJohn Johansen 
120963e2b423SJohn Johansen fs_initcall(aa_create_aafs);
1210