163e2b423SJohn Johansen /*
263e2b423SJohn Johansen  * AppArmor security module
363e2b423SJohn Johansen  *
463e2b423SJohn Johansen  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
563e2b423SJohn Johansen  *
663e2b423SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
763e2b423SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
863e2b423SJohn Johansen  *
963e2b423SJohn Johansen  * This program is free software; you can redistribute it and/or
1063e2b423SJohn Johansen  * modify it under the terms of the GNU General Public License as
1163e2b423SJohn Johansen  * published by the Free Software Foundation, version 2 of the
1263e2b423SJohn Johansen  * License.
1363e2b423SJohn Johansen  */
1463e2b423SJohn Johansen 
150d259f04SJohn Johansen #include <linux/ctype.h>
1663e2b423SJohn Johansen #include <linux/security.h>
1763e2b423SJohn Johansen #include <linux/vmalloc.h>
1863e2b423SJohn Johansen #include <linux/module.h>
1963e2b423SJohn Johansen #include <linux/seq_file.h>
2063e2b423SJohn Johansen #include <linux/uaccess.h>
2163e2b423SJohn Johansen #include <linux/namei.h>
22e74abcf3SKees Cook #include <linux/capability.h>
2363e2b423SJohn Johansen 
2463e2b423SJohn Johansen #include "include/apparmor.h"
2563e2b423SJohn Johansen #include "include/apparmorfs.h"
2663e2b423SJohn Johansen #include "include/audit.h"
2763e2b423SJohn Johansen #include "include/context.h"
2863e2b423SJohn Johansen #include "include/policy.h"
29d384b0a1SKees Cook #include "include/resource.h"
3063e2b423SJohn Johansen 
3163e2b423SJohn Johansen /**
320d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
330d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
340d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
350d259f04SJohn Johansen  *
360d259f04SJohn Johansen  * Returns: length of mangled name
370d259f04SJohn Johansen  */
380d259f04SJohn Johansen static int mangle_name(char *name, char *target)
390d259f04SJohn Johansen {
400d259f04SJohn Johansen 	char *t = target;
410d259f04SJohn Johansen 
420d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
430d259f04SJohn Johansen 		name++;
440d259f04SJohn Johansen 
450d259f04SJohn Johansen 	if (target) {
460d259f04SJohn Johansen 		for (; *name; name++) {
470d259f04SJohn Johansen 			if (*name == '/')
480d259f04SJohn Johansen 				*(t)++ = '.';
490d259f04SJohn Johansen 			else if (isspace(*name))
500d259f04SJohn Johansen 				*(t)++ = '_';
510d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
520d259f04SJohn Johansen 				*(t)++ = *name;
530d259f04SJohn Johansen 		}
540d259f04SJohn Johansen 
550d259f04SJohn Johansen 		*t = 0;
560d259f04SJohn Johansen 	} else {
570d259f04SJohn Johansen 		int len = 0;
580d259f04SJohn Johansen 		for (; *name; name++) {
590d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
600d259f04SJohn Johansen 			    strchr("/._-", *name))
610d259f04SJohn Johansen 				len++;
620d259f04SJohn Johansen 		}
630d259f04SJohn Johansen 
640d259f04SJohn Johansen 		return len;
650d259f04SJohn Johansen 	}
660d259f04SJohn Johansen 
670d259f04SJohn Johansen 	return t - target;
680d259f04SJohn Johansen }
690d259f04SJohn Johansen 
700d259f04SJohn Johansen /**
7163e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
7263e2b423SJohn Johansen  * @op: operation doing the user buffer copy
7363e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
743ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
7563e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
7663e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
7763e2b423SJohn Johansen  *
7863e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
7963e2b423SJohn Johansen  *          ERR_PTR on failure.
8063e2b423SJohn Johansen  */
8163e2b423SJohn Johansen static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
8263e2b423SJohn Johansen 				       size_t alloc_size, size_t copy_size,
8363e2b423SJohn Johansen 				       loff_t *pos)
8463e2b423SJohn Johansen {
8563e2b423SJohn Johansen 	char *data;
8663e2b423SJohn Johansen 
873ed02adaSJohn Johansen 	BUG_ON(copy_size > alloc_size);
883ed02adaSJohn Johansen 
8963e2b423SJohn Johansen 	if (*pos != 0)
9063e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
9163e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
9263e2b423SJohn Johansen 
9363e2b423SJohn Johansen 	/*
9463e2b423SJohn Johansen 	 * Don't allow profile load/replace/remove from profiles that don't
9563e2b423SJohn Johansen 	 * have CAP_MAC_ADMIN
9663e2b423SJohn Johansen 	 */
9763e2b423SJohn Johansen 	if (!aa_may_manage_policy(op))
9863e2b423SJohn Johansen 		return ERR_PTR(-EACCES);
9963e2b423SJohn Johansen 
10063e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
10163e2b423SJohn Johansen 	data = kvmalloc(alloc_size);
10263e2b423SJohn Johansen 	if (data == NULL)
10363e2b423SJohn Johansen 		return ERR_PTR(-ENOMEM);
10463e2b423SJohn Johansen 
10563e2b423SJohn Johansen 	if (copy_from_user(data, userbuf, copy_size)) {
10663e2b423SJohn Johansen 		kvfree(data);
10763e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
10863e2b423SJohn Johansen 	}
10963e2b423SJohn Johansen 
11063e2b423SJohn Johansen 	return data;
11163e2b423SJohn Johansen }
11263e2b423SJohn Johansen 
11363e2b423SJohn Johansen 
11463e2b423SJohn Johansen /* .load file hook fn to load policy */
11563e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
11663e2b423SJohn Johansen 			    loff_t *pos)
11763e2b423SJohn Johansen {
11863e2b423SJohn Johansen 	char *data;
11963e2b423SJohn Johansen 	ssize_t error;
12063e2b423SJohn Johansen 
12163e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
12263e2b423SJohn Johansen 
12363e2b423SJohn Johansen 	error = PTR_ERR(data);
12463e2b423SJohn Johansen 	if (!IS_ERR(data)) {
12563e2b423SJohn Johansen 		error = aa_replace_profiles(data, size, PROF_ADD);
12663e2b423SJohn Johansen 		kvfree(data);
12763e2b423SJohn Johansen 	}
12863e2b423SJohn Johansen 
12963e2b423SJohn Johansen 	return error;
13063e2b423SJohn Johansen }
13163e2b423SJohn Johansen 
13263e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
1336038f373SArnd Bergmann 	.write = profile_load,
1346038f373SArnd Bergmann 	.llseek = default_llseek,
13563e2b423SJohn Johansen };
13663e2b423SJohn Johansen 
13763e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
13863e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
13963e2b423SJohn Johansen 			       size_t size, loff_t *pos)
14063e2b423SJohn Johansen {
14163e2b423SJohn Johansen 	char *data;
14263e2b423SJohn Johansen 	ssize_t error;
14363e2b423SJohn Johansen 
14463e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
14563e2b423SJohn Johansen 	error = PTR_ERR(data);
14663e2b423SJohn Johansen 	if (!IS_ERR(data)) {
14763e2b423SJohn Johansen 		error = aa_replace_profiles(data, size, PROF_REPLACE);
14863e2b423SJohn Johansen 		kvfree(data);
14963e2b423SJohn Johansen 	}
15063e2b423SJohn Johansen 
15163e2b423SJohn Johansen 	return error;
15263e2b423SJohn Johansen }
15363e2b423SJohn Johansen 
15463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
1556038f373SArnd Bergmann 	.write = profile_replace,
1566038f373SArnd Bergmann 	.llseek = default_llseek,
15763e2b423SJohn Johansen };
15863e2b423SJohn Johansen 
15963e2b423SJohn Johansen /* .remove file hook fn to remove loaded policy */
16063e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
16163e2b423SJohn Johansen 			      size_t size, loff_t *pos)
16263e2b423SJohn Johansen {
16363e2b423SJohn Johansen 	char *data;
16463e2b423SJohn Johansen 	ssize_t error;
16563e2b423SJohn Johansen 
16663e2b423SJohn Johansen 	/*
16763e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
16863e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
16963e2b423SJohn Johansen 	 */
17063e2b423SJohn Johansen 	data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
17163e2b423SJohn Johansen 
17263e2b423SJohn Johansen 	error = PTR_ERR(data);
17363e2b423SJohn Johansen 	if (!IS_ERR(data)) {
17463e2b423SJohn Johansen 		data[size] = 0;
17563e2b423SJohn Johansen 		error = aa_remove_profiles(data, size);
17663e2b423SJohn Johansen 		kvfree(data);
17763e2b423SJohn Johansen 	}
17863e2b423SJohn Johansen 
17963e2b423SJohn Johansen 	return error;
18063e2b423SJohn Johansen }
18163e2b423SJohn Johansen 
18263e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
1836038f373SArnd Bergmann 	.write = profile_remove,
1846038f373SArnd Bergmann 	.llseek = default_llseek,
18563e2b423SJohn Johansen };
18663e2b423SJohn Johansen 
187e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v)
188e74abcf3SKees Cook {
189e74abcf3SKees Cook 	struct aa_fs_entry *fs_file = seq->private;
190e74abcf3SKees Cook 
191e74abcf3SKees Cook 	if (!fs_file)
192e74abcf3SKees Cook 		return 0;
193e74abcf3SKees Cook 
194e74abcf3SKees Cook 	switch (fs_file->v_type) {
195e74abcf3SKees Cook 	case AA_FS_TYPE_BOOLEAN:
196e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
197e74abcf3SKees Cook 		break;
198a9bf8e9fSKees Cook 	case AA_FS_TYPE_STRING:
199a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
200a9bf8e9fSKees Cook 		break;
201e74abcf3SKees Cook 	case AA_FS_TYPE_U64:
202e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
203e74abcf3SKees Cook 		break;
204e74abcf3SKees Cook 	default:
205e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
206e74abcf3SKees Cook 		break;
207e74abcf3SKees Cook 	}
208e74abcf3SKees Cook 
209e74abcf3SKees Cook 	return 0;
210e74abcf3SKees Cook }
211e74abcf3SKees Cook 
212e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file)
213e74abcf3SKees Cook {
214e74abcf3SKees Cook 	return single_open(file, aa_fs_seq_show, inode->i_private);
215e74abcf3SKees Cook }
216e74abcf3SKees Cook 
217e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = {
218e74abcf3SKees Cook 	.owner		= THIS_MODULE,
219e74abcf3SKees Cook 	.open		= aa_fs_seq_open,
220e74abcf3SKees Cook 	.read		= seq_read,
221e74abcf3SKees Cook 	.llseek		= seq_lseek,
222e74abcf3SKees Cook 	.release	= single_release,
223e74abcf3SKees Cook };
224e74abcf3SKees Cook 
2250d259f04SJohn Johansen static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
2260d259f04SJohn Johansen 				  int (*show)(struct seq_file *, void *))
2270d259f04SJohn Johansen {
2280d259f04SJohn Johansen 	struct aa_replacedby *r = aa_get_replacedby(inode->i_private);
2290d259f04SJohn Johansen 	int error = single_open(file, show, r);
23063e2b423SJohn Johansen 
2310d259f04SJohn Johansen 	if (error) {
2320d259f04SJohn Johansen 		file->private_data = NULL;
2330d259f04SJohn Johansen 		aa_put_replacedby(r);
2340d259f04SJohn Johansen 	}
2350d259f04SJohn Johansen 
2360d259f04SJohn Johansen 	return error;
2370d259f04SJohn Johansen }
2380d259f04SJohn Johansen 
2390d259f04SJohn Johansen static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
2400d259f04SJohn Johansen {
2410d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
2420d259f04SJohn Johansen 	if (seq)
2430d259f04SJohn Johansen 		aa_put_replacedby(seq->private);
2440d259f04SJohn Johansen 	return single_release(inode, file);
2450d259f04SJohn Johansen }
2460d259f04SJohn Johansen 
2470d259f04SJohn Johansen static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
2480d259f04SJohn Johansen {
2490d259f04SJohn Johansen 	struct aa_replacedby *r = seq->private;
2500d259f04SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
2510d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
2520d259f04SJohn Johansen 	aa_put_profile(profile);
2530d259f04SJohn Johansen 
2540d259f04SJohn Johansen 	return 0;
2550d259f04SJohn Johansen }
2560d259f04SJohn Johansen 
2570d259f04SJohn Johansen static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
2580d259f04SJohn Johansen {
2590d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
2600d259f04SJohn Johansen }
2610d259f04SJohn Johansen 
2620d259f04SJohn Johansen static const struct file_operations aa_fs_profname_fops = {
2630d259f04SJohn Johansen 	.owner		= THIS_MODULE,
2640d259f04SJohn Johansen 	.open		= aa_fs_seq_profname_open,
2650d259f04SJohn Johansen 	.read		= seq_read,
2660d259f04SJohn Johansen 	.llseek		= seq_lseek,
2670d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
2680d259f04SJohn Johansen };
2690d259f04SJohn Johansen 
2700d259f04SJohn Johansen static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
2710d259f04SJohn Johansen {
2720d259f04SJohn Johansen 	struct aa_replacedby *r = seq->private;
2730d259f04SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
2740d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
2750d259f04SJohn Johansen 	aa_put_profile(profile);
2760d259f04SJohn Johansen 
2770d259f04SJohn Johansen 	return 0;
2780d259f04SJohn Johansen }
2790d259f04SJohn Johansen 
2800d259f04SJohn Johansen static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
2810d259f04SJohn Johansen {
2820d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
2830d259f04SJohn Johansen }
2840d259f04SJohn Johansen 
2850d259f04SJohn Johansen static const struct file_operations aa_fs_profmode_fops = {
2860d259f04SJohn Johansen 	.owner		= THIS_MODULE,
2870d259f04SJohn Johansen 	.open		= aa_fs_seq_profmode_open,
2880d259f04SJohn Johansen 	.read		= seq_read,
2890d259f04SJohn Johansen 	.llseek		= seq_lseek,
2900d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
2910d259f04SJohn Johansen };
2920d259f04SJohn Johansen 
293556d0be7SJohn Johansen static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
294556d0be7SJohn Johansen {
295556d0be7SJohn Johansen 	struct aa_replacedby *r = seq->private;
296556d0be7SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
297556d0be7SJohn Johansen 	if (profile->attach)
298556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
299556d0be7SJohn Johansen 	else if (profile->xmatch)
300556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
301556d0be7SJohn Johansen 	else
302556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
303556d0be7SJohn Johansen 	aa_put_profile(profile);
304556d0be7SJohn Johansen 
305556d0be7SJohn Johansen 	return 0;
306556d0be7SJohn Johansen }
307556d0be7SJohn Johansen 
308556d0be7SJohn Johansen static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
309556d0be7SJohn Johansen {
310556d0be7SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
311556d0be7SJohn Johansen }
312556d0be7SJohn Johansen 
313556d0be7SJohn Johansen static const struct file_operations aa_fs_profattach_fops = {
314556d0be7SJohn Johansen 	.owner		= THIS_MODULE,
315556d0be7SJohn Johansen 	.open		= aa_fs_seq_profattach_open,
316556d0be7SJohn Johansen 	.read		= seq_read,
317556d0be7SJohn Johansen 	.llseek		= seq_lseek,
318556d0be7SJohn Johansen 	.release	= aa_fs_seq_profile_release,
319556d0be7SJohn Johansen };
320556d0be7SJohn Johansen 
3210d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
3220d259f04SJohn Johansen void __aa_fs_profile_rmdir(struct aa_profile *profile)
3230d259f04SJohn Johansen {
3240d259f04SJohn Johansen 	struct aa_profile *child;
3250d259f04SJohn Johansen 	int i;
3260d259f04SJohn Johansen 
3270d259f04SJohn Johansen 	if (!profile)
3280d259f04SJohn Johansen 		return;
3290d259f04SJohn Johansen 
3300d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
3310d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
3320d259f04SJohn Johansen 
3330d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
3340d259f04SJohn Johansen 		struct aa_replacedby *r;
3350d259f04SJohn Johansen 		if (!profile->dents[i])
3360d259f04SJohn Johansen 			continue;
3370d259f04SJohn Johansen 
3380d259f04SJohn Johansen 		r = profile->dents[i]->d_inode->i_private;
3390d259f04SJohn Johansen 		securityfs_remove(profile->dents[i]);
3400d259f04SJohn Johansen 		aa_put_replacedby(r);
3410d259f04SJohn Johansen 		profile->dents[i] = NULL;
3420d259f04SJohn Johansen 	}
3430d259f04SJohn Johansen }
3440d259f04SJohn Johansen 
3450d259f04SJohn Johansen void __aa_fs_profile_migrate_dents(struct aa_profile *old,
3460d259f04SJohn Johansen 				   struct aa_profile *new)
3470d259f04SJohn Johansen {
3480d259f04SJohn Johansen 	int i;
3490d259f04SJohn Johansen 
3500d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
3510d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
3520d259f04SJohn Johansen 		old->dents[i] = NULL;
3530d259f04SJohn Johansen 	}
3540d259f04SJohn Johansen }
3550d259f04SJohn Johansen 
3560d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
3570d259f04SJohn Johansen 					  struct aa_profile *profile,
3580d259f04SJohn Johansen 					  const struct file_operations *fops)
3590d259f04SJohn Johansen {
3600d259f04SJohn Johansen 	struct aa_replacedby *r = aa_get_replacedby(profile->replacedby);
3610d259f04SJohn Johansen 	struct dentry *dent;
3620d259f04SJohn Johansen 
3630d259f04SJohn Johansen 	dent = securityfs_create_file(name, S_IFREG | 0444, dir, r, fops);
3640d259f04SJohn Johansen 	if (IS_ERR(dent))
3650d259f04SJohn Johansen 		aa_put_replacedby(r);
3660d259f04SJohn Johansen 
3670d259f04SJohn Johansen 	return dent;
3680d259f04SJohn Johansen }
3690d259f04SJohn Johansen 
3700d259f04SJohn Johansen /* requires lock be held */
3710d259f04SJohn Johansen int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
3720d259f04SJohn Johansen {
3730d259f04SJohn Johansen 	struct aa_profile *child;
3740d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
3750d259f04SJohn Johansen 	int error;
3760d259f04SJohn Johansen 
3770d259f04SJohn Johansen 	if (!parent) {
3780d259f04SJohn Johansen 		struct aa_profile *p;
3790d259f04SJohn Johansen 		p = aa_deref_parent(profile);
3800d259f04SJohn Johansen 		dent = prof_dir(p);
3810d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
3820d259f04SJohn Johansen 		dent = securityfs_create_dir("profiles", dent);
3830d259f04SJohn Johansen 		if (IS_ERR(dent))
3840d259f04SJohn Johansen 			goto fail;
3850d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
3860d259f04SJohn Johansen 	}
3870d259f04SJohn Johansen 
3880d259f04SJohn Johansen 	if (!profile->dirname) {
3890d259f04SJohn Johansen 		int len, id_len;
3900d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
3910d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
3920d259f04SJohn Johansen 
3930d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
3940d259f04SJohn Johansen 		if (!profile->dirname)
3950d259f04SJohn Johansen 			goto fail;
3960d259f04SJohn Johansen 
3970d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
3980d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
3990d259f04SJohn Johansen 	}
4000d259f04SJohn Johansen 
4010d259f04SJohn Johansen 	dent = securityfs_create_dir(profile->dirname, parent);
4020d259f04SJohn Johansen 	if (IS_ERR(dent))
4030d259f04SJohn Johansen 		goto fail;
4040d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
4050d259f04SJohn Johansen 
4060d259f04SJohn Johansen 	dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
4070d259f04SJohn Johansen 	if (IS_ERR(dent))
4080d259f04SJohn Johansen 		goto fail;
4090d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
4100d259f04SJohn Johansen 
4110d259f04SJohn Johansen 	dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
4120d259f04SJohn Johansen 	if (IS_ERR(dent))
4130d259f04SJohn Johansen 		goto fail;
4140d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
4150d259f04SJohn Johansen 
416556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
417556d0be7SJohn Johansen 				   &aa_fs_profattach_fops);
418556d0be7SJohn Johansen 	if (IS_ERR(dent))
419556d0be7SJohn Johansen 		goto fail;
420556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
421556d0be7SJohn Johansen 
4220d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
4230d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
4240d259f04SJohn Johansen 		if (error)
4250d259f04SJohn Johansen 			goto fail2;
4260d259f04SJohn Johansen 	}
4270d259f04SJohn Johansen 
4280d259f04SJohn Johansen 	return 0;
4290d259f04SJohn Johansen 
4300d259f04SJohn Johansen fail:
4310d259f04SJohn Johansen 	error = PTR_ERR(dent);
4320d259f04SJohn Johansen 
4330d259f04SJohn Johansen fail2:
4340d259f04SJohn Johansen 	__aa_fs_profile_rmdir(profile);
4350d259f04SJohn Johansen 
4360d259f04SJohn Johansen 	return error;
4370d259f04SJohn Johansen }
4380d259f04SJohn Johansen 
4390d259f04SJohn Johansen void __aa_fs_namespace_rmdir(struct aa_namespace *ns)
4400d259f04SJohn Johansen {
4410d259f04SJohn Johansen 	struct aa_namespace *sub;
4420d259f04SJohn Johansen 	struct aa_profile *child;
4430d259f04SJohn Johansen 	int i;
4440d259f04SJohn Johansen 
4450d259f04SJohn Johansen 	if (!ns)
4460d259f04SJohn Johansen 		return;
4470d259f04SJohn Johansen 
4480d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
4490d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
4500d259f04SJohn Johansen 
4510d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
4520d259f04SJohn Johansen 		mutex_lock(&sub->lock);
4530d259f04SJohn Johansen 		__aa_fs_namespace_rmdir(sub);
4540d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
4550d259f04SJohn Johansen 	}
4560d259f04SJohn Johansen 
4570d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
4580d259f04SJohn Johansen 		securityfs_remove(ns->dents[i]);
4590d259f04SJohn Johansen 		ns->dents[i] = NULL;
4600d259f04SJohn Johansen 	}
4610d259f04SJohn Johansen }
4620d259f04SJohn Johansen 
4630d259f04SJohn Johansen int __aa_fs_namespace_mkdir(struct aa_namespace *ns, struct dentry *parent,
4640d259f04SJohn Johansen 			    const char *name)
4650d259f04SJohn Johansen {
4660d259f04SJohn Johansen 	struct aa_namespace *sub;
4670d259f04SJohn Johansen 	struct aa_profile *child;
4680d259f04SJohn Johansen 	struct dentry *dent, *dir;
4690d259f04SJohn Johansen 	int error;
4700d259f04SJohn Johansen 
4710d259f04SJohn Johansen 	if (!name)
4720d259f04SJohn Johansen 		name = ns->base.name;
4730d259f04SJohn Johansen 
4740d259f04SJohn Johansen 	dent = securityfs_create_dir(name, parent);
4750d259f04SJohn Johansen 	if (IS_ERR(dent))
4760d259f04SJohn Johansen 		goto fail;
4770d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
4780d259f04SJohn Johansen 
4790d259f04SJohn Johansen 	dent = securityfs_create_dir("profiles", dir);
4800d259f04SJohn Johansen 	if (IS_ERR(dent))
4810d259f04SJohn Johansen 		goto fail;
4820d259f04SJohn Johansen 	ns_subprofs_dir(ns) = dent;
4830d259f04SJohn Johansen 
4840d259f04SJohn Johansen 	dent = securityfs_create_dir("namespaces", dir);
4850d259f04SJohn Johansen 	if (IS_ERR(dent))
4860d259f04SJohn Johansen 		goto fail;
4870d259f04SJohn Johansen 	ns_subns_dir(ns) = dent;
4880d259f04SJohn Johansen 
4890d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
4900d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
4910d259f04SJohn Johansen 		if (error)
4920d259f04SJohn Johansen 			goto fail2;
4930d259f04SJohn Johansen 	}
4940d259f04SJohn Johansen 
4950d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
4960d259f04SJohn Johansen 		mutex_lock(&sub->lock);
4970d259f04SJohn Johansen 		error = __aa_fs_namespace_mkdir(sub, ns_subns_dir(ns), NULL);
4980d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
4990d259f04SJohn Johansen 		if (error)
5000d259f04SJohn Johansen 			goto fail2;
5010d259f04SJohn Johansen 	}
5020d259f04SJohn Johansen 
5030d259f04SJohn Johansen 	return 0;
5040d259f04SJohn Johansen 
5050d259f04SJohn Johansen fail:
5060d259f04SJohn Johansen 	error = PTR_ERR(dent);
5070d259f04SJohn Johansen 
5080d259f04SJohn Johansen fail2:
5090d259f04SJohn Johansen 	__aa_fs_namespace_rmdir(ns);
5100d259f04SJohn Johansen 
5110d259f04SJohn Johansen 	return error;
5120d259f04SJohn Johansen }
5130d259f04SJohn Johansen 
5140d259f04SJohn Johansen 
5150d259f04SJohn Johansen /** Base file system setup **/
516a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = {
517a9bf8e9fSKees Cook 	AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
518a9bf8e9fSKees Cook 				  "link lock"),
519a9bf8e9fSKees Cook 	{ }
520a9bf8e9fSKees Cook };
521a9bf8e9fSKees Cook 
522e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = {
523e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hat",	1),
524e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hatv",	1),
525e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_onexec",	1),
526e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_profile",	1),
527e74abcf3SKees Cook 	{ }
528e74abcf3SKees Cook };
529e74abcf3SKees Cook 
5309d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = {
531dd51c848SJohn Johansen 	AA_FS_FILE_BOOLEAN("set_load",          1),
5329d910a3bSJohn Johansen 	{}
5339d910a3bSJohn Johansen };
5349d910a3bSJohn Johansen 
535e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = {
5369d910a3bSJohn Johansen 	AA_FS_DIR("policy",			aa_fs_entry_policy),
537e74abcf3SKees Cook 	AA_FS_DIR("domain",			aa_fs_entry_domain),
538a9bf8e9fSKees Cook 	AA_FS_DIR("file",			aa_fs_entry_file),
539e74abcf3SKees Cook 	AA_FS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
540d384b0a1SKees Cook 	AA_FS_DIR("rlimit",			aa_fs_entry_rlimit),
541e74abcf3SKees Cook 	{ }
542e74abcf3SKees Cook };
543e74abcf3SKees Cook 
5449acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = {
5459acd494bSKees Cook 	AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
5469acd494bSKees Cook 	AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
5479acd494bSKees Cook 	AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
548e74abcf3SKees Cook 	AA_FS_DIR("features", aa_fs_entry_features),
5499acd494bSKees Cook 	{ }
5509acd494bSKees Cook };
55163e2b423SJohn Johansen 
5529acd494bSKees Cook static struct aa_fs_entry aa_fs_entry =
5539acd494bSKees Cook 	AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
5549acd494bSKees Cook 
5559acd494bSKees Cook /**
5569acd494bSKees Cook  * aafs_create_file - create a file entry in the apparmor securityfs
5579acd494bSKees Cook  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
5589acd494bSKees Cook  * @parent: the parent dentry in the securityfs
5599acd494bSKees Cook  *
5609acd494bSKees Cook  * Use aafs_remove_file to remove entries created with this fn.
5619acd494bSKees Cook  */
5629acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file,
5639acd494bSKees Cook 				   struct dentry *parent)
56463e2b423SJohn Johansen {
5659acd494bSKees Cook 	int error = 0;
56663e2b423SJohn Johansen 
5679acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
5689acd494bSKees Cook 						 S_IFREG | fs_file->mode,
5699acd494bSKees Cook 						 parent, fs_file,
5709acd494bSKees Cook 						 fs_file->file_ops);
5719acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
5729acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
5739acd494bSKees Cook 		fs_file->dentry = NULL;
57463e2b423SJohn Johansen 	}
5759acd494bSKees Cook 	return error;
57663e2b423SJohn Johansen }
57763e2b423SJohn Johansen 
5780d259f04SJohn Johansen static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
57963e2b423SJohn Johansen /**
5809acd494bSKees Cook  * aafs_create_dir - recursively create a directory entry in the securityfs
5819acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
5829acd494bSKees Cook  * @parent: the parent dentry in the securityfs
58363e2b423SJohn Johansen  *
5849acd494bSKees Cook  * Use aafs_remove_dir to remove entries created with this fn.
58563e2b423SJohn Johansen  */
5869acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
5879acd494bSKees Cook 				  struct dentry *parent)
58863e2b423SJohn Johansen {
5899acd494bSKees Cook 	struct aa_fs_entry *fs_file;
5900d259f04SJohn Johansen 	struct dentry *dir;
5910d259f04SJohn Johansen 	int error;
59263e2b423SJohn Johansen 
5930d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
5940d259f04SJohn Johansen 	if (IS_ERR(dir))
5950d259f04SJohn Johansen 		return PTR_ERR(dir);
5960d259f04SJohn Johansen 	fs_dir->dentry = dir;
59763e2b423SJohn Johansen 
5980d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
5999acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
6009acd494bSKees Cook 			error = aafs_create_dir(fs_file, fs_dir->dentry);
6019acd494bSKees Cook 		else
6029acd494bSKees Cook 			error = aafs_create_file(fs_file, fs_dir->dentry);
6039acd494bSKees Cook 		if (error)
6049acd494bSKees Cook 			goto failed;
6059acd494bSKees Cook 	}
6069acd494bSKees Cook 
6079acd494bSKees Cook 	return 0;
6089acd494bSKees Cook 
6099acd494bSKees Cook failed:
6100d259f04SJohn Johansen 	aafs_remove_dir(fs_dir);
6110d259f04SJohn Johansen 
6129acd494bSKees Cook 	return error;
6139acd494bSKees Cook }
6149acd494bSKees Cook 
6159acd494bSKees Cook /**
6169acd494bSKees Cook  * aafs_remove_file - drop a single file entry in the apparmor securityfs
6179acd494bSKees Cook  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
6189acd494bSKees Cook  */
6199acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
6209acd494bSKees Cook {
6219acd494bSKees Cook 	if (!fs_file->dentry)
6229acd494bSKees Cook 		return;
6239acd494bSKees Cook 
6249acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
6259acd494bSKees Cook 	fs_file->dentry = NULL;
6269acd494bSKees Cook }
6279acd494bSKees Cook 
6289acd494bSKees Cook /**
6299acd494bSKees Cook  * aafs_remove_dir - recursively drop a directory entry from the securityfs
6309acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
6319acd494bSKees Cook  */
6329acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
6339acd494bSKees Cook {
6349acd494bSKees Cook 	struct aa_fs_entry *fs_file;
6359acd494bSKees Cook 
6360d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
6379acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
6389acd494bSKees Cook 			aafs_remove_dir(fs_file);
6399acd494bSKees Cook 		else
6409acd494bSKees Cook 			aafs_remove_file(fs_file);
6419acd494bSKees Cook 	}
6429acd494bSKees Cook 
6439acd494bSKees Cook 	aafs_remove_file(fs_dir);
64463e2b423SJohn Johansen }
64563e2b423SJohn Johansen 
64663e2b423SJohn Johansen /**
64763e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
64863e2b423SJohn Johansen  *
64963e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
65063e2b423SJohn Johansen  */
65163e2b423SJohn Johansen void __init aa_destroy_aafs(void)
65263e2b423SJohn Johansen {
6539acd494bSKees Cook 	aafs_remove_dir(&aa_fs_entry);
65463e2b423SJohn Johansen }
65563e2b423SJohn Johansen 
65663e2b423SJohn Johansen /**
65763e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
65863e2b423SJohn Johansen  *
65963e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
66063e2b423SJohn Johansen  *
66163e2b423SJohn Johansen  * Returns: error on failure
66263e2b423SJohn Johansen  */
6633417d8d5SJames Morris static int __init aa_create_aafs(void)
66463e2b423SJohn Johansen {
66563e2b423SJohn Johansen 	int error;
66663e2b423SJohn Johansen 
66763e2b423SJohn Johansen 	if (!apparmor_initialized)
66863e2b423SJohn Johansen 		return 0;
66963e2b423SJohn Johansen 
6709acd494bSKees Cook 	if (aa_fs_entry.dentry) {
67163e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
67263e2b423SJohn Johansen 		return -EEXIST;
67363e2b423SJohn Johansen 	}
67463e2b423SJohn Johansen 
6759acd494bSKees Cook 	/* Populate fs tree. */
6769acd494bSKees Cook 	error = aafs_create_dir(&aa_fs_entry, NULL);
67763e2b423SJohn Johansen 	if (error)
67863e2b423SJohn Johansen 		goto error;
67963e2b423SJohn Johansen 
6800d259f04SJohn Johansen 	error = __aa_fs_namespace_mkdir(root_ns, aa_fs_entry.dentry,
6810d259f04SJohn Johansen 					"policy");
6820d259f04SJohn Johansen 	if (error)
6830d259f04SJohn Johansen 		goto error;
6840d259f04SJohn Johansen 
68563e2b423SJohn Johansen 	/* TODO: add support for apparmorfs_null and apparmorfs_mnt */
68663e2b423SJohn Johansen 
68763e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
68863e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
68963e2b423SJohn Johansen 	return 0;
69063e2b423SJohn Johansen 
69163e2b423SJohn Johansen error:
69263e2b423SJohn Johansen 	aa_destroy_aafs();
69363e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
69463e2b423SJohn Johansen 	return error;
69563e2b423SJohn Johansen }
69663e2b423SJohn Johansen 
69763e2b423SJohn Johansen fs_initcall(aa_create_aafs);
698