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"
3663e2b423SJohn Johansen 
3763e2b423SJohn Johansen /**
380d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
390d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
400d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
410d259f04SJohn Johansen  *
420d259f04SJohn Johansen  * Returns: length of mangled name
430d259f04SJohn Johansen  */
44bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
450d259f04SJohn Johansen {
460d259f04SJohn Johansen 	char *t = target;
470d259f04SJohn Johansen 
480d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
490d259f04SJohn Johansen 		name++;
500d259f04SJohn Johansen 
510d259f04SJohn Johansen 	if (target) {
520d259f04SJohn Johansen 		for (; *name; name++) {
530d259f04SJohn Johansen 			if (*name == '/')
540d259f04SJohn Johansen 				*(t)++ = '.';
550d259f04SJohn Johansen 			else if (isspace(*name))
560d259f04SJohn Johansen 				*(t)++ = '_';
570d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
580d259f04SJohn Johansen 				*(t)++ = *name;
590d259f04SJohn Johansen 		}
600d259f04SJohn Johansen 
610d259f04SJohn Johansen 		*t = 0;
620d259f04SJohn Johansen 	} else {
630d259f04SJohn Johansen 		int len = 0;
640d259f04SJohn Johansen 		for (; *name; name++) {
650d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
660d259f04SJohn Johansen 			    strchr("/._-", *name))
670d259f04SJohn Johansen 				len++;
680d259f04SJohn Johansen 		}
690d259f04SJohn Johansen 
700d259f04SJohn Johansen 		return len;
710d259f04SJohn Johansen 	}
720d259f04SJohn Johansen 
730d259f04SJohn Johansen 	return t - target;
740d259f04SJohn Johansen }
750d259f04SJohn Johansen 
760d259f04SJohn Johansen /**
7763e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
7863e2b423SJohn Johansen  * @op: operation doing the user buffer copy
7963e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
803ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
8163e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
8263e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
8363e2b423SJohn Johansen  *
8463e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
8563e2b423SJohn Johansen  *          ERR_PTR on failure.
8663e2b423SJohn Johansen  */
8763e2b423SJohn Johansen static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
8863e2b423SJohn Johansen 				       size_t alloc_size, size_t copy_size,
8963e2b423SJohn Johansen 				       loff_t *pos)
9063e2b423SJohn Johansen {
9163e2b423SJohn Johansen 	char *data;
9263e2b423SJohn Johansen 
933ed02adaSJohn Johansen 	BUG_ON(copy_size > alloc_size);
943ed02adaSJohn Johansen 
9563e2b423SJohn Johansen 	if (*pos != 0)
9663e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
9763e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
9863e2b423SJohn Johansen 
9963e2b423SJohn Johansen 	/*
10063e2b423SJohn Johansen 	 * Don't allow profile load/replace/remove from profiles that don't
10163e2b423SJohn Johansen 	 * have CAP_MAC_ADMIN
10263e2b423SJohn Johansen 	 */
10363e2b423SJohn Johansen 	if (!aa_may_manage_policy(op))
10463e2b423SJohn Johansen 		return ERR_PTR(-EACCES);
10563e2b423SJohn Johansen 
10663e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
10763e2b423SJohn Johansen 	data = kvmalloc(alloc_size);
10863e2b423SJohn Johansen 	if (data == NULL)
10963e2b423SJohn Johansen 		return ERR_PTR(-ENOMEM);
11063e2b423SJohn Johansen 
11163e2b423SJohn Johansen 	if (copy_from_user(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 
11963e2b423SJohn Johansen 
12063e2b423SJohn Johansen /* .load file hook fn to load policy */
12163e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
12263e2b423SJohn Johansen 			    loff_t *pos)
12363e2b423SJohn Johansen {
12463e2b423SJohn Johansen 	char *data;
12563e2b423SJohn Johansen 	ssize_t error;
12663e2b423SJohn Johansen 
12763e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
12863e2b423SJohn Johansen 
12963e2b423SJohn Johansen 	error = PTR_ERR(data);
13063e2b423SJohn Johansen 	if (!IS_ERR(data)) {
13173688d1eSJohn Johansen 		error = aa_replace_profiles(__aa_current_profile()->ns, data,
13273688d1eSJohn Johansen 					    size, PROF_ADD);
13363e2b423SJohn Johansen 		kvfree(data);
13463e2b423SJohn Johansen 	}
13563e2b423SJohn Johansen 
13663e2b423SJohn Johansen 	return error;
13763e2b423SJohn Johansen }
13863e2b423SJohn Johansen 
13963e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
1406038f373SArnd Bergmann 	.write = profile_load,
1416038f373SArnd Bergmann 	.llseek = default_llseek,
14263e2b423SJohn Johansen };
14363e2b423SJohn Johansen 
14463e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
14563e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
14663e2b423SJohn Johansen 			       size_t size, loff_t *pos)
14763e2b423SJohn Johansen {
14863e2b423SJohn Johansen 	char *data;
14963e2b423SJohn Johansen 	ssize_t error;
15063e2b423SJohn Johansen 
15163e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
15263e2b423SJohn Johansen 	error = PTR_ERR(data);
15363e2b423SJohn Johansen 	if (!IS_ERR(data)) {
15473688d1eSJohn Johansen 		error = aa_replace_profiles(__aa_current_profile()->ns, data,
15573688d1eSJohn Johansen 					    size, PROF_REPLACE);
15663e2b423SJohn Johansen 		kvfree(data);
15763e2b423SJohn Johansen 	}
15863e2b423SJohn Johansen 
15963e2b423SJohn Johansen 	return error;
16063e2b423SJohn Johansen }
16163e2b423SJohn Johansen 
16263e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
1636038f373SArnd Bergmann 	.write = profile_replace,
1646038f373SArnd Bergmann 	.llseek = default_llseek,
16563e2b423SJohn Johansen };
16663e2b423SJohn Johansen 
16763e2b423SJohn Johansen /* .remove file hook fn to remove loaded policy */
16863e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
16963e2b423SJohn Johansen 			      size_t size, loff_t *pos)
17063e2b423SJohn Johansen {
17163e2b423SJohn Johansen 	char *data;
17263e2b423SJohn Johansen 	ssize_t error;
17363e2b423SJohn Johansen 
17463e2b423SJohn Johansen 	/*
17563e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
17663e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
17763e2b423SJohn Johansen 	 */
17863e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
17963e2b423SJohn Johansen 
18063e2b423SJohn Johansen 	error = PTR_ERR(data);
18163e2b423SJohn Johansen 	if (!IS_ERR(data)) {
18263e2b423SJohn Johansen 		data[size] = 0;
18363e2b423SJohn Johansen 		error = aa_remove_profiles(data, size);
18463e2b423SJohn Johansen 		kvfree(data);
18563e2b423SJohn Johansen 	}
18663e2b423SJohn Johansen 
18763e2b423SJohn Johansen 	return error;
18863e2b423SJohn Johansen }
18963e2b423SJohn Johansen 
19063e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
1916038f373SArnd Bergmann 	.write = profile_remove,
1926038f373SArnd Bergmann 	.llseek = default_llseek,
19363e2b423SJohn Johansen };
19463e2b423SJohn Johansen 
195e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v)
196e74abcf3SKees Cook {
197e74abcf3SKees Cook 	struct aa_fs_entry *fs_file = seq->private;
198e74abcf3SKees Cook 
199e74abcf3SKees Cook 	if (!fs_file)
200e74abcf3SKees Cook 		return 0;
201e74abcf3SKees Cook 
202e74abcf3SKees Cook 	switch (fs_file->v_type) {
203e74abcf3SKees Cook 	case AA_FS_TYPE_BOOLEAN:
204e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
205e74abcf3SKees Cook 		break;
206a9bf8e9fSKees Cook 	case AA_FS_TYPE_STRING:
207a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
208a9bf8e9fSKees Cook 		break;
209e74abcf3SKees Cook 	case AA_FS_TYPE_U64:
210e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
211e74abcf3SKees Cook 		break;
212e74abcf3SKees Cook 	default:
213e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
214e74abcf3SKees Cook 		break;
215e74abcf3SKees Cook 	}
216e74abcf3SKees Cook 
217e74abcf3SKees Cook 	return 0;
218e74abcf3SKees Cook }
219e74abcf3SKees Cook 
220e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file)
221e74abcf3SKees Cook {
222e74abcf3SKees Cook 	return single_open(file, aa_fs_seq_show, inode->i_private);
223e74abcf3SKees Cook }
224e74abcf3SKees Cook 
225e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = {
226e74abcf3SKees Cook 	.owner		= THIS_MODULE,
227e74abcf3SKees Cook 	.open		= aa_fs_seq_open,
228e74abcf3SKees Cook 	.read		= seq_read,
229e74abcf3SKees Cook 	.llseek		= seq_lseek,
230e74abcf3SKees Cook 	.release	= single_release,
231e74abcf3SKees Cook };
232e74abcf3SKees Cook 
2330d259f04SJohn Johansen static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
2340d259f04SJohn Johansen 				  int (*show)(struct seq_file *, void *))
2350d259f04SJohn Johansen {
2368399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
2378399588aSJohn Johansen 	int error = single_open(file, show, proxy);
23863e2b423SJohn Johansen 
2390d259f04SJohn Johansen 	if (error) {
2400d259f04SJohn Johansen 		file->private_data = NULL;
2418399588aSJohn Johansen 		aa_put_proxy(proxy);
2420d259f04SJohn Johansen 	}
2430d259f04SJohn Johansen 
2440d259f04SJohn Johansen 	return error;
2450d259f04SJohn Johansen }
2460d259f04SJohn Johansen 
2470d259f04SJohn Johansen static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
2480d259f04SJohn Johansen {
2490d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
2500d259f04SJohn Johansen 	if (seq)
2518399588aSJohn Johansen 		aa_put_proxy(seq->private);
2520d259f04SJohn Johansen 	return single_release(inode, file);
2530d259f04SJohn Johansen }
2540d259f04SJohn Johansen 
2550d259f04SJohn Johansen static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
2560d259f04SJohn Johansen {
2578399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
2588399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
2590d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
2600d259f04SJohn Johansen 	aa_put_profile(profile);
2610d259f04SJohn Johansen 
2620d259f04SJohn Johansen 	return 0;
2630d259f04SJohn Johansen }
2640d259f04SJohn Johansen 
2650d259f04SJohn Johansen static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
2660d259f04SJohn Johansen {
2670d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
2680d259f04SJohn Johansen }
2690d259f04SJohn Johansen 
2700d259f04SJohn Johansen static const struct file_operations aa_fs_profname_fops = {
2710d259f04SJohn Johansen 	.owner		= THIS_MODULE,
2720d259f04SJohn Johansen 	.open		= aa_fs_seq_profname_open,
2730d259f04SJohn Johansen 	.read		= seq_read,
2740d259f04SJohn Johansen 	.llseek		= seq_lseek,
2750d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
2760d259f04SJohn Johansen };
2770d259f04SJohn Johansen 
2780d259f04SJohn Johansen static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
2790d259f04SJohn Johansen {
2808399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
2818399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
2820d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
2830d259f04SJohn Johansen 	aa_put_profile(profile);
2840d259f04SJohn Johansen 
2850d259f04SJohn Johansen 	return 0;
2860d259f04SJohn Johansen }
2870d259f04SJohn Johansen 
2880d259f04SJohn Johansen static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
2890d259f04SJohn Johansen {
2900d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
2910d259f04SJohn Johansen }
2920d259f04SJohn Johansen 
2930d259f04SJohn Johansen static const struct file_operations aa_fs_profmode_fops = {
2940d259f04SJohn Johansen 	.owner		= THIS_MODULE,
2950d259f04SJohn Johansen 	.open		= aa_fs_seq_profmode_open,
2960d259f04SJohn Johansen 	.read		= seq_read,
2970d259f04SJohn Johansen 	.llseek		= seq_lseek,
2980d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
2990d259f04SJohn Johansen };
3000d259f04SJohn Johansen 
301556d0be7SJohn Johansen static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
302556d0be7SJohn Johansen {
3038399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3048399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
305556d0be7SJohn Johansen 	if (profile->attach)
306556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
307556d0be7SJohn Johansen 	else if (profile->xmatch)
308556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
309556d0be7SJohn Johansen 	else
310556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
311556d0be7SJohn Johansen 	aa_put_profile(profile);
312556d0be7SJohn Johansen 
313556d0be7SJohn Johansen 	return 0;
314556d0be7SJohn Johansen }
315556d0be7SJohn Johansen 
316556d0be7SJohn Johansen static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
317556d0be7SJohn Johansen {
318556d0be7SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
319556d0be7SJohn Johansen }
320556d0be7SJohn Johansen 
321556d0be7SJohn Johansen static const struct file_operations aa_fs_profattach_fops = {
322556d0be7SJohn Johansen 	.owner		= THIS_MODULE,
323556d0be7SJohn Johansen 	.open		= aa_fs_seq_profattach_open,
324556d0be7SJohn Johansen 	.read		= seq_read,
325556d0be7SJohn Johansen 	.llseek		= seq_lseek,
326556d0be7SJohn Johansen 	.release	= aa_fs_seq_profile_release,
327556d0be7SJohn Johansen };
328556d0be7SJohn Johansen 
329f8eb8a13SJohn Johansen static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
330f8eb8a13SJohn Johansen {
3318399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
3328399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
333f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
334f8eb8a13SJohn Johansen 
335f8eb8a13SJohn Johansen 	if (profile->hash) {
336f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
337f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
338f8eb8a13SJohn Johansen 		seq_puts(seq, "\n");
339f8eb8a13SJohn Johansen 	}
3400b938a2eSJohn Johansen 	aa_put_profile(profile);
341f8eb8a13SJohn Johansen 
342f8eb8a13SJohn Johansen 	return 0;
343f8eb8a13SJohn Johansen }
344f8eb8a13SJohn Johansen 
345f8eb8a13SJohn Johansen static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
346f8eb8a13SJohn Johansen {
347f8eb8a13SJohn Johansen 	return single_open(file, aa_fs_seq_hash_show, inode->i_private);
348f8eb8a13SJohn Johansen }
349f8eb8a13SJohn Johansen 
350f8eb8a13SJohn Johansen static const struct file_operations aa_fs_seq_hash_fops = {
351f8eb8a13SJohn Johansen 	.owner		= THIS_MODULE,
352f8eb8a13SJohn Johansen 	.open		= aa_fs_seq_hash_open,
353f8eb8a13SJohn Johansen 	.read		= seq_read,
354f8eb8a13SJohn Johansen 	.llseek		= seq_lseek,
355f8eb8a13SJohn Johansen 	.release	= single_release,
356f8eb8a13SJohn Johansen };
357f8eb8a13SJohn Johansen 
358a71ada30SJohn Johansen static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v)
359a71ada30SJohn Johansen {
360a71ada30SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
361a71ada30SJohn Johansen 
362a71ada30SJohn Johansen 	seq_printf(seq, "%d\n", ns->level);
363a71ada30SJohn Johansen 
364a71ada30SJohn Johansen 	return 0;
365a71ada30SJohn Johansen }
366a71ada30SJohn Johansen 
367a71ada30SJohn Johansen static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file)
368a71ada30SJohn Johansen {
369a71ada30SJohn Johansen 	return single_open(file, aa_fs_seq_show_ns_level, inode->i_private);
370a71ada30SJohn Johansen }
371a71ada30SJohn Johansen 
372a71ada30SJohn Johansen static const struct file_operations aa_fs_ns_level = {
373a71ada30SJohn Johansen 	.owner		= THIS_MODULE,
374a71ada30SJohn Johansen 	.open		= aa_fs_seq_open_ns_level,
375a71ada30SJohn Johansen 	.read		= seq_read,
376a71ada30SJohn Johansen 	.llseek		= seq_lseek,
377a71ada30SJohn Johansen 	.release	= single_release,
378a71ada30SJohn Johansen };
379a71ada30SJohn Johansen 
3800d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
3810d259f04SJohn Johansen void __aa_fs_profile_rmdir(struct aa_profile *profile)
3820d259f04SJohn Johansen {
3830d259f04SJohn Johansen 	struct aa_profile *child;
3840d259f04SJohn Johansen 	int i;
3850d259f04SJohn Johansen 
3860d259f04SJohn Johansen 	if (!profile)
3870d259f04SJohn Johansen 		return;
3880d259f04SJohn Johansen 
3890d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
3900d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
3910d259f04SJohn Johansen 
3920d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
3938399588aSJohn Johansen 		struct aa_proxy *proxy;
3940d259f04SJohn Johansen 		if (!profile->dents[i])
3950d259f04SJohn Johansen 			continue;
3960d259f04SJohn Johansen 
3978399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
3980d259f04SJohn Johansen 		securityfs_remove(profile->dents[i]);
3998399588aSJohn Johansen 		aa_put_proxy(proxy);
4000d259f04SJohn Johansen 		profile->dents[i] = NULL;
4010d259f04SJohn Johansen 	}
4020d259f04SJohn Johansen }
4030d259f04SJohn Johansen 
4040d259f04SJohn Johansen void __aa_fs_profile_migrate_dents(struct aa_profile *old,
4050d259f04SJohn Johansen 				   struct aa_profile *new)
4060d259f04SJohn Johansen {
4070d259f04SJohn Johansen 	int i;
4080d259f04SJohn Johansen 
4090d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
4100d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
411d671e890SJohn Johansen 		if (new->dents[i])
412078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
4130d259f04SJohn Johansen 		old->dents[i] = NULL;
4140d259f04SJohn Johansen 	}
4150d259f04SJohn Johansen }
4160d259f04SJohn Johansen 
4170d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
4180d259f04SJohn Johansen 					  struct aa_profile *profile,
4190d259f04SJohn Johansen 					  const struct file_operations *fops)
4200d259f04SJohn Johansen {
4218399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
4220d259f04SJohn Johansen 	struct dentry *dent;
4230d259f04SJohn Johansen 
4248399588aSJohn Johansen 	dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
4250d259f04SJohn Johansen 	if (IS_ERR(dent))
4268399588aSJohn Johansen 		aa_put_proxy(proxy);
4270d259f04SJohn Johansen 
4280d259f04SJohn Johansen 	return dent;
4290d259f04SJohn Johansen }
4300d259f04SJohn Johansen 
4310d259f04SJohn Johansen /* requires lock be held */
4320d259f04SJohn Johansen int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
4330d259f04SJohn Johansen {
4340d259f04SJohn Johansen 	struct aa_profile *child;
4350d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
4360d259f04SJohn Johansen 	int error;
4370d259f04SJohn Johansen 
4380d259f04SJohn Johansen 	if (!parent) {
4390d259f04SJohn Johansen 		struct aa_profile *p;
4400d259f04SJohn Johansen 		p = aa_deref_parent(profile);
4410d259f04SJohn Johansen 		dent = prof_dir(p);
4420d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
4430d259f04SJohn Johansen 		dent = securityfs_create_dir("profiles", dent);
4440d259f04SJohn Johansen 		if (IS_ERR(dent))
4450d259f04SJohn Johansen 			goto fail;
4460d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
4470d259f04SJohn Johansen 	}
4480d259f04SJohn Johansen 
4490d259f04SJohn Johansen 	if (!profile->dirname) {
4500d259f04SJohn Johansen 		int len, id_len;
4510d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
4520d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
4530d259f04SJohn Johansen 
4540d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
4550d259f04SJohn Johansen 		if (!profile->dirname)
4560d259f04SJohn Johansen 			goto fail;
4570d259f04SJohn Johansen 
4580d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
4590d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
4600d259f04SJohn Johansen 	}
4610d259f04SJohn Johansen 
4620d259f04SJohn Johansen 	dent = securityfs_create_dir(profile->dirname, parent);
4630d259f04SJohn Johansen 	if (IS_ERR(dent))
4640d259f04SJohn Johansen 		goto fail;
4650d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
4660d259f04SJohn Johansen 
4670d259f04SJohn Johansen 	dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
4680d259f04SJohn Johansen 	if (IS_ERR(dent))
4690d259f04SJohn Johansen 		goto fail;
4700d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
4710d259f04SJohn Johansen 
4720d259f04SJohn Johansen 	dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
4730d259f04SJohn Johansen 	if (IS_ERR(dent))
4740d259f04SJohn Johansen 		goto fail;
4750d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
4760d259f04SJohn Johansen 
477556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
478556d0be7SJohn Johansen 				   &aa_fs_profattach_fops);
479556d0be7SJohn Johansen 	if (IS_ERR(dent))
480556d0be7SJohn Johansen 		goto fail;
481556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
482556d0be7SJohn Johansen 
483f8eb8a13SJohn Johansen 	if (profile->hash) {
484f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
485f8eb8a13SJohn Johansen 					   &aa_fs_seq_hash_fops);
486f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
487f8eb8a13SJohn Johansen 			goto fail;
488f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
489f8eb8a13SJohn Johansen 	}
490f8eb8a13SJohn Johansen 
4910d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
4920d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
4930d259f04SJohn Johansen 		if (error)
4940d259f04SJohn Johansen 			goto fail2;
4950d259f04SJohn Johansen 	}
4960d259f04SJohn Johansen 
4970d259f04SJohn Johansen 	return 0;
4980d259f04SJohn Johansen 
4990d259f04SJohn Johansen fail:
5000d259f04SJohn Johansen 	error = PTR_ERR(dent);
5010d259f04SJohn Johansen 
5020d259f04SJohn Johansen fail2:
5030d259f04SJohn Johansen 	__aa_fs_profile_rmdir(profile);
5040d259f04SJohn Johansen 
5050d259f04SJohn Johansen 	return error;
5060d259f04SJohn Johansen }
5070d259f04SJohn Johansen 
50898849dffSJohn Johansen void __aa_fs_ns_rmdir(struct aa_ns *ns)
5090d259f04SJohn Johansen {
51098849dffSJohn Johansen 	struct aa_ns *sub;
5110d259f04SJohn Johansen 	struct aa_profile *child;
5120d259f04SJohn Johansen 	int i;
5130d259f04SJohn Johansen 
5140d259f04SJohn Johansen 	if (!ns)
5150d259f04SJohn Johansen 		return;
5160d259f04SJohn Johansen 
5170d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
5180d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
5190d259f04SJohn Johansen 
5200d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
5210d259f04SJohn Johansen 		mutex_lock(&sub->lock);
52298849dffSJohn Johansen 		__aa_fs_ns_rmdir(sub);
5230d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
5240d259f04SJohn Johansen 	}
5250d259f04SJohn Johansen 
5260d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
5270d259f04SJohn Johansen 		securityfs_remove(ns->dents[i]);
5280d259f04SJohn Johansen 		ns->dents[i] = NULL;
5290d259f04SJohn Johansen 	}
5300d259f04SJohn Johansen }
5310d259f04SJohn Johansen 
53298849dffSJohn Johansen int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
5330d259f04SJohn Johansen {
53498849dffSJohn Johansen 	struct aa_ns *sub;
5350d259f04SJohn Johansen 	struct aa_profile *child;
5360d259f04SJohn Johansen 	struct dentry *dent, *dir;
5370d259f04SJohn Johansen 	int error;
5380d259f04SJohn Johansen 
5390d259f04SJohn Johansen 	if (!name)
5400d259f04SJohn Johansen 		name = ns->base.name;
5410d259f04SJohn Johansen 
5420d259f04SJohn Johansen 	dent = securityfs_create_dir(name, parent);
5430d259f04SJohn Johansen 	if (IS_ERR(dent))
5440d259f04SJohn Johansen 		goto fail;
5450d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
5460d259f04SJohn Johansen 
5470d259f04SJohn Johansen 	dent = securityfs_create_dir("profiles", dir);
5480d259f04SJohn Johansen 	if (IS_ERR(dent))
5490d259f04SJohn Johansen 		goto fail;
5500d259f04SJohn Johansen 	ns_subprofs_dir(ns) = dent;
5510d259f04SJohn Johansen 
5520d259f04SJohn Johansen 	dent = securityfs_create_dir("namespaces", dir);
5530d259f04SJohn Johansen 	if (IS_ERR(dent))
5540d259f04SJohn Johansen 		goto fail;
5550d259f04SJohn Johansen 	ns_subns_dir(ns) = dent;
5560d259f04SJohn Johansen 
5570d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
5580d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
5590d259f04SJohn Johansen 		if (error)
5600d259f04SJohn Johansen 			goto fail2;
5610d259f04SJohn Johansen 	}
5620d259f04SJohn Johansen 
5630d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
5640d259f04SJohn Johansen 		mutex_lock(&sub->lock);
56598849dffSJohn Johansen 		error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
5660d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
5670d259f04SJohn Johansen 		if (error)
5680d259f04SJohn Johansen 			goto fail2;
5690d259f04SJohn Johansen 	}
5700d259f04SJohn Johansen 
5710d259f04SJohn Johansen 	return 0;
5720d259f04SJohn Johansen 
5730d259f04SJohn Johansen fail:
5740d259f04SJohn Johansen 	error = PTR_ERR(dent);
5750d259f04SJohn Johansen 
5760d259f04SJohn Johansen fail2:
57798849dffSJohn Johansen 	__aa_fs_ns_rmdir(ns);
5780d259f04SJohn Johansen 
5790d259f04SJohn Johansen 	return error;
5800d259f04SJohn Johansen }
5810d259f04SJohn Johansen 
5820d259f04SJohn Johansen 
58329b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
58429b3822fSJohn Johansen 
58529b3822fSJohn Johansen /**
58698849dffSJohn Johansen  * __next_ns - find the next namespace to list
58729b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
58829b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
58929b3822fSJohn Johansen  *
59029b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
59129b3822fSJohn Johansen  * while switching current namespace.
59229b3822fSJohn Johansen  *
59329b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
59429b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
59529b3822fSJohn Johansen  * NOTE: will not unlock root->lock
59629b3822fSJohn Johansen  */
59798849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
59829b3822fSJohn Johansen {
59998849dffSJohn Johansen 	struct aa_ns *parent, *next;
60029b3822fSJohn Johansen 
60129b3822fSJohn Johansen 	/* is next namespace a child */
60229b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
60329b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
60429b3822fSJohn Johansen 		mutex_lock(&next->lock);
60529b3822fSJohn Johansen 		return next;
60629b3822fSJohn Johansen 	}
60729b3822fSJohn Johansen 
60829b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
60929b3822fSJohn Johansen 	parent = ns->parent;
610ed2c7da3SJohn Johansen 	while (ns != root) {
61129b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
61238dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
61329b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
61429b3822fSJohn Johansen 			mutex_lock(&next->lock);
61529b3822fSJohn Johansen 			return next;
61629b3822fSJohn Johansen 		}
61729b3822fSJohn Johansen 		ns = parent;
61829b3822fSJohn Johansen 		parent = parent->parent;
61929b3822fSJohn Johansen 	}
62029b3822fSJohn Johansen 
62129b3822fSJohn Johansen 	return NULL;
62229b3822fSJohn Johansen }
62329b3822fSJohn Johansen 
62429b3822fSJohn Johansen /**
62529b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
62629b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
62729b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
62829b3822fSJohn Johansen  *
62929b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
63029b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
63129b3822fSJohn Johansen  */
63298849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
63398849dffSJohn Johansen 					  struct aa_ns *ns)
63429b3822fSJohn Johansen {
63598849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
63629b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
63729b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
63829b3822fSJohn Johansen 						struct aa_profile, base.list);
63929b3822fSJohn Johansen 	}
64029b3822fSJohn Johansen 	return NULL;
64129b3822fSJohn Johansen }
64229b3822fSJohn Johansen 
64329b3822fSJohn Johansen /**
64429b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
64529b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
64629b3822fSJohn Johansen  *
64729b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
64829b3822fSJohn Johansen  *
64929b3822fSJohn Johansen  * Returns: next profile or NULL if done
65029b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
65129b3822fSJohn Johansen  */
65229b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
65329b3822fSJohn Johansen {
65429b3822fSJohn Johansen 	struct aa_profile *parent;
65598849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
65629b3822fSJohn Johansen 
65729b3822fSJohn Johansen 	/* is next profile a child */
65829b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
65929b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
66029b3822fSJohn Johansen 					base.list);
66129b3822fSJohn Johansen 
66229b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
66329b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
66429b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
66529b3822fSJohn Johansen 	while (parent) {
66638dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
66729b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
66829b3822fSJohn Johansen 			return p;
66929b3822fSJohn Johansen 		p = parent;
67029b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
67129b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
67229b3822fSJohn Johansen 	}
67329b3822fSJohn Johansen 
67429b3822fSJohn Johansen 	/* is next another profile in the namespace */
67538dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
67629b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
67729b3822fSJohn Johansen 		return p;
67829b3822fSJohn Johansen 
67929b3822fSJohn Johansen 	return NULL;
68029b3822fSJohn Johansen }
68129b3822fSJohn Johansen 
68229b3822fSJohn Johansen /**
68329b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
68429b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
68529b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
68629b3822fSJohn Johansen  *
68729b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
68829b3822fSJohn Johansen  */
68998849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
69029b3822fSJohn Johansen 				       struct aa_profile *profile)
69129b3822fSJohn Johansen {
69229b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
69329b3822fSJohn Johansen 	if (next)
69429b3822fSJohn Johansen 		return next;
69529b3822fSJohn Johansen 
69629b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
69798849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
69829b3822fSJohn Johansen }
69929b3822fSJohn Johansen 
70029b3822fSJohn Johansen /**
70129b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
70229b3822fSJohn Johansen  * @f: seq_file to fill
70329b3822fSJohn Johansen  * @pos: current position
70429b3822fSJohn Johansen  *
70529b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
70629b3822fSJohn Johansen  *
70729b3822fSJohn Johansen  * acquires first ns->lock
70829b3822fSJohn Johansen  */
70929b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
71029b3822fSJohn Johansen {
71129b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
71298849dffSJohn Johansen 	struct aa_ns *root = aa_current_profile()->ns;
71329b3822fSJohn Johansen 	loff_t l = *pos;
71498849dffSJohn Johansen 	f->private = aa_get_ns(root);
71529b3822fSJohn Johansen 
71629b3822fSJohn Johansen 
71729b3822fSJohn Johansen 	/* find the first profile */
71829b3822fSJohn Johansen 	mutex_lock(&root->lock);
71929b3822fSJohn Johansen 	profile = __first_profile(root, root);
72029b3822fSJohn Johansen 
72129b3822fSJohn Johansen 	/* skip to position */
72229b3822fSJohn Johansen 	for (; profile && l > 0; l--)
72329b3822fSJohn Johansen 		profile = next_profile(root, profile);
72429b3822fSJohn Johansen 
72529b3822fSJohn Johansen 	return profile;
72629b3822fSJohn Johansen }
72729b3822fSJohn Johansen 
72829b3822fSJohn Johansen /**
72929b3822fSJohn Johansen  * p_next - read the next profile entry
73029b3822fSJohn Johansen  * @f: seq_file to fill
73129b3822fSJohn Johansen  * @p: profile previously returned
73229b3822fSJohn Johansen  * @pos: current position
73329b3822fSJohn Johansen  *
73429b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
73529b3822fSJohn Johansen  *
73629b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
73729b3822fSJohn Johansen  */
73829b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
73929b3822fSJohn Johansen {
74029b3822fSJohn Johansen 	struct aa_profile *profile = p;
74198849dffSJohn Johansen 	struct aa_ns *ns = f->private;
74229b3822fSJohn Johansen 	(*pos)++;
74329b3822fSJohn Johansen 
74429b3822fSJohn Johansen 	return next_profile(ns, profile);
74529b3822fSJohn Johansen }
74629b3822fSJohn Johansen 
74729b3822fSJohn Johansen /**
74829b3822fSJohn Johansen  * p_stop - stop depth first traversal
74929b3822fSJohn Johansen  * @f: seq_file we are filling
75029b3822fSJohn Johansen  * @p: the last profile writen
75129b3822fSJohn Johansen  *
75229b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
75329b3822fSJohn Johansen  */
75429b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
75529b3822fSJohn Johansen {
75629b3822fSJohn Johansen 	struct aa_profile *profile = p;
75798849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
75829b3822fSJohn Johansen 
75929b3822fSJohn Johansen 	if (profile) {
76029b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
76129b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
76229b3822fSJohn Johansen 	}
76329b3822fSJohn Johansen 	mutex_unlock(&root->lock);
76498849dffSJohn Johansen 	aa_put_ns(root);
76529b3822fSJohn Johansen }
76629b3822fSJohn Johansen 
76729b3822fSJohn Johansen /**
76829b3822fSJohn Johansen  * seq_show_profile - show a profile entry
76929b3822fSJohn Johansen  * @f: seq_file to file
77029b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
77129b3822fSJohn Johansen  *
77229b3822fSJohn Johansen  * Returns: error on failure
77329b3822fSJohn Johansen  */
77429b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
77529b3822fSJohn Johansen {
77629b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
77798849dffSJohn Johansen 	struct aa_ns *root = f->private;
77829b3822fSJohn Johansen 
77929b3822fSJohn Johansen 	if (profile->ns != root)
78092b6d8efSJohn Johansen 		seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
78129b3822fSJohn Johansen 	seq_printf(f, "%s (%s)\n", profile->base.hname,
78229b3822fSJohn Johansen 		   aa_profile_mode_names[profile->mode]);
78329b3822fSJohn Johansen 
78429b3822fSJohn Johansen 	return 0;
78529b3822fSJohn Johansen }
78629b3822fSJohn Johansen 
78729b3822fSJohn Johansen static const struct seq_operations aa_fs_profiles_op = {
78829b3822fSJohn Johansen 	.start = p_start,
78929b3822fSJohn Johansen 	.next = p_next,
79029b3822fSJohn Johansen 	.stop = p_stop,
79129b3822fSJohn Johansen 	.show = seq_show_profile,
79229b3822fSJohn Johansen };
79329b3822fSJohn Johansen 
79429b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
79529b3822fSJohn Johansen {
79629b3822fSJohn Johansen 	return seq_open(file, &aa_fs_profiles_op);
79729b3822fSJohn Johansen }
79829b3822fSJohn Johansen 
79929b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
80029b3822fSJohn Johansen {
80129b3822fSJohn Johansen 	return seq_release(inode, file);
80229b3822fSJohn Johansen }
80329b3822fSJohn Johansen 
80429b3822fSJohn Johansen static const struct file_operations aa_fs_profiles_fops = {
80529b3822fSJohn Johansen 	.open = profiles_open,
80629b3822fSJohn Johansen 	.read = seq_read,
80729b3822fSJohn Johansen 	.llseek = seq_lseek,
80829b3822fSJohn Johansen 	.release = profiles_release,
80929b3822fSJohn Johansen };
81029b3822fSJohn Johansen 
81129b3822fSJohn Johansen 
8120d259f04SJohn Johansen /** Base file system setup **/
813a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = {
814a9bf8e9fSKees Cook 	AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
815a9bf8e9fSKees Cook 				  "link lock"),
816a9bf8e9fSKees Cook 	{ }
817a9bf8e9fSKees Cook };
818a9bf8e9fSKees Cook 
819e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = {
820e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hat",	1),
821e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hatv",	1),
822e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_onexec",	1),
823e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_profile",	1),
82434c426acSJohn Johansen 	AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
825e74abcf3SKees Cook 	{ }
826e74abcf3SKees Cook };
827e74abcf3SKees Cook 
828474d6b75SJohn Johansen static struct aa_fs_entry aa_fs_entry_versions[] = {
829474d6b75SJohn Johansen 	AA_FS_FILE_BOOLEAN("v5",	1),
830474d6b75SJohn Johansen 	{ }
831474d6b75SJohn Johansen };
832474d6b75SJohn Johansen 
8339d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = {
834474d6b75SJohn Johansen 	AA_FS_DIR("versions",                   aa_fs_entry_versions),
835dd51c848SJohn Johansen 	AA_FS_FILE_BOOLEAN("set_load",		1),
8369d910a3bSJohn Johansen 	{ }
8379d910a3bSJohn Johansen };
8389d910a3bSJohn Johansen 
839e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = {
8409d910a3bSJohn Johansen 	AA_FS_DIR("policy",			aa_fs_entry_policy),
841e74abcf3SKees Cook 	AA_FS_DIR("domain",			aa_fs_entry_domain),
842a9bf8e9fSKees Cook 	AA_FS_DIR("file",			aa_fs_entry_file),
843e74abcf3SKees Cook 	AA_FS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
844d384b0a1SKees Cook 	AA_FS_DIR("rlimit",			aa_fs_entry_rlimit),
84584f1f787SJohn Johansen 	AA_FS_DIR("caps",			aa_fs_entry_caps),
846e74abcf3SKees Cook 	{ }
847e74abcf3SKees Cook };
848e74abcf3SKees Cook 
8499acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = {
8509acd494bSKees Cook 	AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
8519acd494bSKees Cook 	AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
8529acd494bSKees Cook 	AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
853a71ada30SJohn Johansen 	AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level),
85429b3822fSJohn Johansen 	AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
855e74abcf3SKees Cook 	AA_FS_DIR("features", aa_fs_entry_features),
8569acd494bSKees Cook 	{ }
8579acd494bSKees Cook };
85863e2b423SJohn Johansen 
8599acd494bSKees Cook static struct aa_fs_entry aa_fs_entry =
8609acd494bSKees Cook 	AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
8619acd494bSKees Cook 
8629acd494bSKees Cook /**
8639acd494bSKees Cook  * aafs_create_file - create a file entry in the apparmor securityfs
8649acd494bSKees Cook  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
8659acd494bSKees Cook  * @parent: the parent dentry in the securityfs
8669acd494bSKees Cook  *
8679acd494bSKees Cook  * Use aafs_remove_file to remove entries created with this fn.
8689acd494bSKees Cook  */
8699acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file,
8709acd494bSKees Cook 				   struct dentry *parent)
87163e2b423SJohn Johansen {
8729acd494bSKees Cook 	int error = 0;
87363e2b423SJohn Johansen 
8749acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
8759acd494bSKees Cook 						 S_IFREG | fs_file->mode,
8769acd494bSKees Cook 						 parent, fs_file,
8779acd494bSKees Cook 						 fs_file->file_ops);
8789acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
8799acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
8809acd494bSKees Cook 		fs_file->dentry = NULL;
88163e2b423SJohn Johansen 	}
8829acd494bSKees Cook 	return error;
88363e2b423SJohn Johansen }
88463e2b423SJohn Johansen 
8850d259f04SJohn Johansen static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
88663e2b423SJohn Johansen /**
8879acd494bSKees Cook  * aafs_create_dir - recursively create a directory entry in the securityfs
8889acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
8899acd494bSKees Cook  * @parent: the parent dentry in the securityfs
89063e2b423SJohn Johansen  *
8919acd494bSKees Cook  * Use aafs_remove_dir to remove entries created with this fn.
89263e2b423SJohn Johansen  */
8939acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
8949acd494bSKees Cook 				  struct dentry *parent)
89563e2b423SJohn Johansen {
8969acd494bSKees Cook 	struct aa_fs_entry *fs_file;
8970d259f04SJohn Johansen 	struct dentry *dir;
8980d259f04SJohn Johansen 	int error;
89963e2b423SJohn Johansen 
9000d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
9010d259f04SJohn Johansen 	if (IS_ERR(dir))
9020d259f04SJohn Johansen 		return PTR_ERR(dir);
9030d259f04SJohn Johansen 	fs_dir->dentry = dir;
90463e2b423SJohn Johansen 
9050d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
9069acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
9079acd494bSKees Cook 			error = aafs_create_dir(fs_file, fs_dir->dentry);
9089acd494bSKees Cook 		else
9099acd494bSKees Cook 			error = aafs_create_file(fs_file, fs_dir->dentry);
9109acd494bSKees Cook 		if (error)
9119acd494bSKees Cook 			goto failed;
9129acd494bSKees Cook 	}
9139acd494bSKees Cook 
9149acd494bSKees Cook 	return 0;
9159acd494bSKees Cook 
9169acd494bSKees Cook failed:
9170d259f04SJohn Johansen 	aafs_remove_dir(fs_dir);
9180d259f04SJohn Johansen 
9199acd494bSKees Cook 	return error;
9209acd494bSKees Cook }
9219acd494bSKees Cook 
9229acd494bSKees Cook /**
9239acd494bSKees Cook  * aafs_remove_file - drop a single file entry in the apparmor securityfs
9249acd494bSKees Cook  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
9259acd494bSKees Cook  */
9269acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
9279acd494bSKees Cook {
9289acd494bSKees Cook 	if (!fs_file->dentry)
9299acd494bSKees Cook 		return;
9309acd494bSKees Cook 
9319acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
9329acd494bSKees Cook 	fs_file->dentry = NULL;
9339acd494bSKees Cook }
9349acd494bSKees Cook 
9359acd494bSKees Cook /**
9369acd494bSKees Cook  * aafs_remove_dir - recursively drop a directory entry from the securityfs
9379acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
9389acd494bSKees Cook  */
9399acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
9409acd494bSKees Cook {
9419acd494bSKees Cook 	struct aa_fs_entry *fs_file;
9429acd494bSKees Cook 
9430d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
9449acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
9459acd494bSKees Cook 			aafs_remove_dir(fs_file);
9469acd494bSKees Cook 		else
9479acd494bSKees Cook 			aafs_remove_file(fs_file);
9489acd494bSKees Cook 	}
9499acd494bSKees Cook 
9509acd494bSKees Cook 	aafs_remove_file(fs_dir);
95163e2b423SJohn Johansen }
95263e2b423SJohn Johansen 
95363e2b423SJohn Johansen /**
95463e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
95563e2b423SJohn Johansen  *
95663e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
95763e2b423SJohn Johansen  */
95863e2b423SJohn Johansen void __init aa_destroy_aafs(void)
95963e2b423SJohn Johansen {
9609acd494bSKees Cook 	aafs_remove_dir(&aa_fs_entry);
96163e2b423SJohn Johansen }
96263e2b423SJohn Johansen 
963a71ada30SJohn Johansen 
964a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
965a71ada30SJohn Johansen struct path aa_null;
966a71ada30SJohn Johansen 
967a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
968a71ada30SJohn Johansen {
969a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
970a71ada30SJohn Johansen 	struct dentry *dentry;
971a71ada30SJohn Johansen 	struct inode *inode;
972a71ada30SJohn Johansen 	int count = 0;
973a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
974a71ada30SJohn Johansen 
975a71ada30SJohn Johansen 	if (error)
976a71ada30SJohn Johansen 		return error;
977a71ada30SJohn Johansen 
978a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
979a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
980a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
981a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
982a71ada30SJohn Johansen 		goto out;
983a71ada30SJohn Johansen 	}
984a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
985a71ada30SJohn Johansen 	if (!inode) {
986a71ada30SJohn Johansen 		error = -ENOMEM;
987a71ada30SJohn Johansen 		goto out1;
988a71ada30SJohn Johansen 	}
989a71ada30SJohn Johansen 
990a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
991a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
992a71ada30SJohn Johansen 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
993a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
994a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
995a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
996a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
997a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
998a71ada30SJohn Johansen 
999a71ada30SJohn Johansen 	error = 0;
1000a71ada30SJohn Johansen 
1001a71ada30SJohn Johansen out1:
1002a71ada30SJohn Johansen 	dput(dentry);
1003a71ada30SJohn Johansen out:
1004a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
1005a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
1006a71ada30SJohn Johansen 	return error;
1007a71ada30SJohn Johansen }
1008a71ada30SJohn Johansen 
100963e2b423SJohn Johansen /**
101063e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
101163e2b423SJohn Johansen  *
101263e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
101363e2b423SJohn Johansen  *
101463e2b423SJohn Johansen  * Returns: error on failure
101563e2b423SJohn Johansen  */
10163417d8d5SJames Morris static int __init aa_create_aafs(void)
101763e2b423SJohn Johansen {
101863e2b423SJohn Johansen 	int error;
101963e2b423SJohn Johansen 
102063e2b423SJohn Johansen 	if (!apparmor_initialized)
102163e2b423SJohn Johansen 		return 0;
102263e2b423SJohn Johansen 
10239acd494bSKees Cook 	if (aa_fs_entry.dentry) {
102463e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
102563e2b423SJohn Johansen 		return -EEXIST;
102663e2b423SJohn Johansen 	}
102763e2b423SJohn Johansen 
10289acd494bSKees Cook 	/* Populate fs tree. */
10299acd494bSKees Cook 	error = aafs_create_dir(&aa_fs_entry, NULL);
103063e2b423SJohn Johansen 	if (error)
103163e2b423SJohn Johansen 		goto error;
103263e2b423SJohn Johansen 
103398849dffSJohn Johansen 	error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
10340d259f04SJohn Johansen 	if (error)
10350d259f04SJohn Johansen 		goto error;
10360d259f04SJohn Johansen 
1037a71ada30SJohn Johansen 	error = aa_mk_null_file(aa_fs_entry.dentry);
1038a71ada30SJohn Johansen 	if (error)
1039a71ada30SJohn Johansen 		goto error;
1040a71ada30SJohn Johansen 
1041a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
104263e2b423SJohn Johansen 
104363e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
104463e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
104563e2b423SJohn Johansen 	return 0;
104663e2b423SJohn Johansen 
104763e2b423SJohn Johansen error:
104863e2b423SJohn Johansen 	aa_destroy_aafs();
104963e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
105063e2b423SJohn Johansen 	return error;
105163e2b423SJohn Johansen }
105263e2b423SJohn Johansen 
105363e2b423SJohn Johansen fs_initcall(aa_create_aafs);
1054