xref: /openbmc/linux/security/apparmor/apparmorfs.c (revision 0d259f043f5f60f74c4fd020aac190cb6450e918)
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 
15*0d259f04SJohn 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 /**
32*0d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
33*0d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
34*0d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
35*0d259f04SJohn Johansen  *
36*0d259f04SJohn Johansen  * Returns: length of mangled name
37*0d259f04SJohn Johansen  */
38*0d259f04SJohn Johansen static int mangle_name(char *name, char *target)
39*0d259f04SJohn Johansen {
40*0d259f04SJohn Johansen 	char *t = target;
41*0d259f04SJohn Johansen 
42*0d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
43*0d259f04SJohn Johansen 		name++;
44*0d259f04SJohn Johansen 
45*0d259f04SJohn Johansen 	if (target) {
46*0d259f04SJohn Johansen 		for (; *name; name++) {
47*0d259f04SJohn Johansen 			if (*name == '/')
48*0d259f04SJohn Johansen 				*(t)++ = '.';
49*0d259f04SJohn Johansen 			else if (isspace(*name))
50*0d259f04SJohn Johansen 				*(t)++ = '_';
51*0d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
52*0d259f04SJohn Johansen 				*(t)++ = *name;
53*0d259f04SJohn Johansen 		}
54*0d259f04SJohn Johansen 
55*0d259f04SJohn Johansen 		*t = 0;
56*0d259f04SJohn Johansen 	} else {
57*0d259f04SJohn Johansen 		int len = 0;
58*0d259f04SJohn Johansen 		for (; *name; name++) {
59*0d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
60*0d259f04SJohn Johansen 			    strchr("/._-", *name))
61*0d259f04SJohn Johansen 				len++;
62*0d259f04SJohn Johansen 		}
63*0d259f04SJohn Johansen 
64*0d259f04SJohn Johansen 		return len;
65*0d259f04SJohn Johansen 	}
66*0d259f04SJohn Johansen 
67*0d259f04SJohn Johansen 	return t - target;
68*0d259f04SJohn Johansen }
69*0d259f04SJohn Johansen 
70*0d259f04SJohn 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 
225*0d259f04SJohn Johansen static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
226*0d259f04SJohn Johansen 				  int (*show)(struct seq_file *, void *))
227*0d259f04SJohn Johansen {
228*0d259f04SJohn Johansen 	struct aa_replacedby *r = aa_get_replacedby(inode->i_private);
229*0d259f04SJohn Johansen 	int error = single_open(file, show, r);
23063e2b423SJohn Johansen 
231*0d259f04SJohn Johansen 	if (error) {
232*0d259f04SJohn Johansen 		file->private_data = NULL;
233*0d259f04SJohn Johansen 		aa_put_replacedby(r);
234*0d259f04SJohn Johansen 	}
235*0d259f04SJohn Johansen 
236*0d259f04SJohn Johansen 	return error;
237*0d259f04SJohn Johansen }
238*0d259f04SJohn Johansen 
239*0d259f04SJohn Johansen static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
240*0d259f04SJohn Johansen {
241*0d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
242*0d259f04SJohn Johansen 	if (seq)
243*0d259f04SJohn Johansen 		aa_put_replacedby(seq->private);
244*0d259f04SJohn Johansen 	return single_release(inode, file);
245*0d259f04SJohn Johansen }
246*0d259f04SJohn Johansen 
247*0d259f04SJohn Johansen static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
248*0d259f04SJohn Johansen {
249*0d259f04SJohn Johansen 	struct aa_replacedby *r = seq->private;
250*0d259f04SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
251*0d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
252*0d259f04SJohn Johansen 	aa_put_profile(profile);
253*0d259f04SJohn Johansen 
254*0d259f04SJohn Johansen 	return 0;
255*0d259f04SJohn Johansen }
256*0d259f04SJohn Johansen 
257*0d259f04SJohn Johansen static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
258*0d259f04SJohn Johansen {
259*0d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
260*0d259f04SJohn Johansen }
261*0d259f04SJohn Johansen 
262*0d259f04SJohn Johansen static const struct file_operations aa_fs_profname_fops = {
263*0d259f04SJohn Johansen 	.owner		= THIS_MODULE,
264*0d259f04SJohn Johansen 	.open		= aa_fs_seq_profname_open,
265*0d259f04SJohn Johansen 	.read		= seq_read,
266*0d259f04SJohn Johansen 	.llseek		= seq_lseek,
267*0d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
268*0d259f04SJohn Johansen };
269*0d259f04SJohn Johansen 
270*0d259f04SJohn Johansen static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
271*0d259f04SJohn Johansen {
272*0d259f04SJohn Johansen 	struct aa_replacedby *r = seq->private;
273*0d259f04SJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
274*0d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
275*0d259f04SJohn Johansen 	aa_put_profile(profile);
276*0d259f04SJohn Johansen 
277*0d259f04SJohn Johansen 	return 0;
278*0d259f04SJohn Johansen }
279*0d259f04SJohn Johansen 
280*0d259f04SJohn Johansen static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
281*0d259f04SJohn Johansen {
282*0d259f04SJohn Johansen 	return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
283*0d259f04SJohn Johansen }
284*0d259f04SJohn Johansen 
285*0d259f04SJohn Johansen static const struct file_operations aa_fs_profmode_fops = {
286*0d259f04SJohn Johansen 	.owner		= THIS_MODULE,
287*0d259f04SJohn Johansen 	.open		= aa_fs_seq_profmode_open,
288*0d259f04SJohn Johansen 	.read		= seq_read,
289*0d259f04SJohn Johansen 	.llseek		= seq_lseek,
290*0d259f04SJohn Johansen 	.release	= aa_fs_seq_profile_release,
291*0d259f04SJohn Johansen };
292*0d259f04SJohn Johansen 
293*0d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
294*0d259f04SJohn Johansen void __aa_fs_profile_rmdir(struct aa_profile *profile)
295*0d259f04SJohn Johansen {
296*0d259f04SJohn Johansen 	struct aa_profile *child;
297*0d259f04SJohn Johansen 	int i;
298*0d259f04SJohn Johansen 
299*0d259f04SJohn Johansen 	if (!profile)
300*0d259f04SJohn Johansen 		return;
301*0d259f04SJohn Johansen 
302*0d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
303*0d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
304*0d259f04SJohn Johansen 
305*0d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
306*0d259f04SJohn Johansen 		struct aa_replacedby *r;
307*0d259f04SJohn Johansen 		if (!profile->dents[i])
308*0d259f04SJohn Johansen 			continue;
309*0d259f04SJohn Johansen 
310*0d259f04SJohn Johansen 		r = profile->dents[i]->d_inode->i_private;
311*0d259f04SJohn Johansen 		securityfs_remove(profile->dents[i]);
312*0d259f04SJohn Johansen 		aa_put_replacedby(r);
313*0d259f04SJohn Johansen 		profile->dents[i] = NULL;
314*0d259f04SJohn Johansen 	}
315*0d259f04SJohn Johansen }
316*0d259f04SJohn Johansen 
317*0d259f04SJohn Johansen void __aa_fs_profile_migrate_dents(struct aa_profile *old,
318*0d259f04SJohn Johansen 				   struct aa_profile *new)
319*0d259f04SJohn Johansen {
320*0d259f04SJohn Johansen 	int i;
321*0d259f04SJohn Johansen 
322*0d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
323*0d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
324*0d259f04SJohn Johansen 		old->dents[i] = NULL;
325*0d259f04SJohn Johansen 	}
326*0d259f04SJohn Johansen }
327*0d259f04SJohn Johansen 
328*0d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
329*0d259f04SJohn Johansen 					  struct aa_profile *profile,
330*0d259f04SJohn Johansen 					  const struct file_operations *fops)
331*0d259f04SJohn Johansen {
332*0d259f04SJohn Johansen 	struct aa_replacedby *r = aa_get_replacedby(profile->replacedby);
333*0d259f04SJohn Johansen 	struct dentry *dent;
334*0d259f04SJohn Johansen 
335*0d259f04SJohn Johansen 	dent = securityfs_create_file(name, S_IFREG | 0444, dir, r, fops);
336*0d259f04SJohn Johansen 	if (IS_ERR(dent))
337*0d259f04SJohn Johansen 		aa_put_replacedby(r);
338*0d259f04SJohn Johansen 
339*0d259f04SJohn Johansen 	return dent;
340*0d259f04SJohn Johansen }
341*0d259f04SJohn Johansen 
342*0d259f04SJohn Johansen /* requires lock be held */
343*0d259f04SJohn Johansen int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
344*0d259f04SJohn Johansen {
345*0d259f04SJohn Johansen 	struct aa_profile *child;
346*0d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
347*0d259f04SJohn Johansen 	int error;
348*0d259f04SJohn Johansen 
349*0d259f04SJohn Johansen 	if (!parent) {
350*0d259f04SJohn Johansen 		struct aa_profile *p;
351*0d259f04SJohn Johansen 		p = aa_deref_parent(profile);
352*0d259f04SJohn Johansen 		dent = prof_dir(p);
353*0d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
354*0d259f04SJohn Johansen 		dent = securityfs_create_dir("profiles", dent);
355*0d259f04SJohn Johansen 		if (IS_ERR(dent))
356*0d259f04SJohn Johansen 			goto fail;
357*0d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
358*0d259f04SJohn Johansen 	}
359*0d259f04SJohn Johansen 
360*0d259f04SJohn Johansen 	if (!profile->dirname) {
361*0d259f04SJohn Johansen 		int len, id_len;
362*0d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
363*0d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
364*0d259f04SJohn Johansen 
365*0d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
366*0d259f04SJohn Johansen 		if (!profile->dirname)
367*0d259f04SJohn Johansen 			goto fail;
368*0d259f04SJohn Johansen 
369*0d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
370*0d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
371*0d259f04SJohn Johansen 	}
372*0d259f04SJohn Johansen 
373*0d259f04SJohn Johansen 	dent = securityfs_create_dir(profile->dirname, parent);
374*0d259f04SJohn Johansen 	if (IS_ERR(dent))
375*0d259f04SJohn Johansen 		goto fail;
376*0d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
377*0d259f04SJohn Johansen 
378*0d259f04SJohn Johansen 	dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
379*0d259f04SJohn Johansen 	if (IS_ERR(dent))
380*0d259f04SJohn Johansen 		goto fail;
381*0d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
382*0d259f04SJohn Johansen 
383*0d259f04SJohn Johansen 	dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
384*0d259f04SJohn Johansen 	if (IS_ERR(dent))
385*0d259f04SJohn Johansen 		goto fail;
386*0d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
387*0d259f04SJohn Johansen 
388*0d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
389*0d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
390*0d259f04SJohn Johansen 		if (error)
391*0d259f04SJohn Johansen 			goto fail2;
392*0d259f04SJohn Johansen 	}
393*0d259f04SJohn Johansen 
394*0d259f04SJohn Johansen 	return 0;
395*0d259f04SJohn Johansen 
396*0d259f04SJohn Johansen fail:
397*0d259f04SJohn Johansen 	error = PTR_ERR(dent);
398*0d259f04SJohn Johansen 
399*0d259f04SJohn Johansen fail2:
400*0d259f04SJohn Johansen 	__aa_fs_profile_rmdir(profile);
401*0d259f04SJohn Johansen 
402*0d259f04SJohn Johansen 	return error;
403*0d259f04SJohn Johansen }
404*0d259f04SJohn Johansen 
405*0d259f04SJohn Johansen void __aa_fs_namespace_rmdir(struct aa_namespace *ns)
406*0d259f04SJohn Johansen {
407*0d259f04SJohn Johansen 	struct aa_namespace *sub;
408*0d259f04SJohn Johansen 	struct aa_profile *child;
409*0d259f04SJohn Johansen 	int i;
410*0d259f04SJohn Johansen 
411*0d259f04SJohn Johansen 	if (!ns)
412*0d259f04SJohn Johansen 		return;
413*0d259f04SJohn Johansen 
414*0d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
415*0d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
416*0d259f04SJohn Johansen 
417*0d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
418*0d259f04SJohn Johansen 		mutex_lock(&sub->lock);
419*0d259f04SJohn Johansen 		__aa_fs_namespace_rmdir(sub);
420*0d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
421*0d259f04SJohn Johansen 	}
422*0d259f04SJohn Johansen 
423*0d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
424*0d259f04SJohn Johansen 		securityfs_remove(ns->dents[i]);
425*0d259f04SJohn Johansen 		ns->dents[i] = NULL;
426*0d259f04SJohn Johansen 	}
427*0d259f04SJohn Johansen }
428*0d259f04SJohn Johansen 
429*0d259f04SJohn Johansen int __aa_fs_namespace_mkdir(struct aa_namespace *ns, struct dentry *parent,
430*0d259f04SJohn Johansen 			    const char *name)
431*0d259f04SJohn Johansen {
432*0d259f04SJohn Johansen 	struct aa_namespace *sub;
433*0d259f04SJohn Johansen 	struct aa_profile *child;
434*0d259f04SJohn Johansen 	struct dentry *dent, *dir;
435*0d259f04SJohn Johansen 	int error;
436*0d259f04SJohn Johansen 
437*0d259f04SJohn Johansen 	if (!name)
438*0d259f04SJohn Johansen 		name = ns->base.name;
439*0d259f04SJohn Johansen 
440*0d259f04SJohn Johansen 	dent = securityfs_create_dir(name, parent);
441*0d259f04SJohn Johansen 	if (IS_ERR(dent))
442*0d259f04SJohn Johansen 		goto fail;
443*0d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
444*0d259f04SJohn Johansen 
445*0d259f04SJohn Johansen 	dent = securityfs_create_dir("profiles", dir);
446*0d259f04SJohn Johansen 	if (IS_ERR(dent))
447*0d259f04SJohn Johansen 		goto fail;
448*0d259f04SJohn Johansen 	ns_subprofs_dir(ns) = dent;
449*0d259f04SJohn Johansen 
450*0d259f04SJohn Johansen 	dent = securityfs_create_dir("namespaces", dir);
451*0d259f04SJohn Johansen 	if (IS_ERR(dent))
452*0d259f04SJohn Johansen 		goto fail;
453*0d259f04SJohn Johansen 	ns_subns_dir(ns) = dent;
454*0d259f04SJohn Johansen 
455*0d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
456*0d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
457*0d259f04SJohn Johansen 		if (error)
458*0d259f04SJohn Johansen 			goto fail2;
459*0d259f04SJohn Johansen 	}
460*0d259f04SJohn Johansen 
461*0d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
462*0d259f04SJohn Johansen 		mutex_lock(&sub->lock);
463*0d259f04SJohn Johansen 		error = __aa_fs_namespace_mkdir(sub, ns_subns_dir(ns), NULL);
464*0d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
465*0d259f04SJohn Johansen 		if (error)
466*0d259f04SJohn Johansen 			goto fail2;
467*0d259f04SJohn Johansen 	}
468*0d259f04SJohn Johansen 
469*0d259f04SJohn Johansen 	return 0;
470*0d259f04SJohn Johansen 
471*0d259f04SJohn Johansen fail:
472*0d259f04SJohn Johansen 	error = PTR_ERR(dent);
473*0d259f04SJohn Johansen 
474*0d259f04SJohn Johansen fail2:
475*0d259f04SJohn Johansen 	__aa_fs_namespace_rmdir(ns);
476*0d259f04SJohn Johansen 
477*0d259f04SJohn Johansen 	return error;
478*0d259f04SJohn Johansen }
479*0d259f04SJohn Johansen 
480*0d259f04SJohn Johansen 
481*0d259f04SJohn Johansen /** Base file system setup **/
482a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = {
483a9bf8e9fSKees Cook 	AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
484a9bf8e9fSKees Cook 				  "link lock"),
485a9bf8e9fSKees Cook 	{ }
486a9bf8e9fSKees Cook };
487a9bf8e9fSKees Cook 
488e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = {
489e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hat",	1),
490e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hatv",	1),
491e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_onexec",	1),
492e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_profile",	1),
493e74abcf3SKees Cook 	{ }
494e74abcf3SKees Cook };
495e74abcf3SKees Cook 
4969d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = {
497dd51c848SJohn Johansen 	AA_FS_FILE_BOOLEAN("set_load",          1),
4989d910a3bSJohn Johansen 	{}
4999d910a3bSJohn Johansen };
5009d910a3bSJohn Johansen 
501e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = {
5029d910a3bSJohn Johansen 	AA_FS_DIR("policy",			aa_fs_entry_policy),
503e74abcf3SKees Cook 	AA_FS_DIR("domain",			aa_fs_entry_domain),
504a9bf8e9fSKees Cook 	AA_FS_DIR("file",			aa_fs_entry_file),
505e74abcf3SKees Cook 	AA_FS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
506d384b0a1SKees Cook 	AA_FS_DIR("rlimit",			aa_fs_entry_rlimit),
507e74abcf3SKees Cook 	{ }
508e74abcf3SKees Cook };
509e74abcf3SKees Cook 
5109acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = {
5119acd494bSKees Cook 	AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
5129acd494bSKees Cook 	AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
5139acd494bSKees Cook 	AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
514e74abcf3SKees Cook 	AA_FS_DIR("features", aa_fs_entry_features),
5159acd494bSKees Cook 	{ }
5169acd494bSKees Cook };
51763e2b423SJohn Johansen 
5189acd494bSKees Cook static struct aa_fs_entry aa_fs_entry =
5199acd494bSKees Cook 	AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
5209acd494bSKees Cook 
5219acd494bSKees Cook /**
5229acd494bSKees Cook  * aafs_create_file - create a file entry in the apparmor securityfs
5239acd494bSKees Cook  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
5249acd494bSKees Cook  * @parent: the parent dentry in the securityfs
5259acd494bSKees Cook  *
5269acd494bSKees Cook  * Use aafs_remove_file to remove entries created with this fn.
5279acd494bSKees Cook  */
5289acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file,
5299acd494bSKees Cook 				   struct dentry *parent)
53063e2b423SJohn Johansen {
5319acd494bSKees Cook 	int error = 0;
53263e2b423SJohn Johansen 
5339acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
5349acd494bSKees Cook 						 S_IFREG | fs_file->mode,
5359acd494bSKees Cook 						 parent, fs_file,
5369acd494bSKees Cook 						 fs_file->file_ops);
5379acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
5389acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
5399acd494bSKees Cook 		fs_file->dentry = NULL;
54063e2b423SJohn Johansen 	}
5419acd494bSKees Cook 	return error;
54263e2b423SJohn Johansen }
54363e2b423SJohn Johansen 
544*0d259f04SJohn Johansen static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
54563e2b423SJohn Johansen /**
5469acd494bSKees Cook  * aafs_create_dir - recursively create a directory entry in the securityfs
5479acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
5489acd494bSKees Cook  * @parent: the parent dentry in the securityfs
54963e2b423SJohn Johansen  *
5509acd494bSKees Cook  * Use aafs_remove_dir to remove entries created with this fn.
55163e2b423SJohn Johansen  */
5529acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
5539acd494bSKees Cook 				  struct dentry *parent)
55463e2b423SJohn Johansen {
5559acd494bSKees Cook 	struct aa_fs_entry *fs_file;
556*0d259f04SJohn Johansen 	struct dentry *dir;
557*0d259f04SJohn Johansen 	int error;
55863e2b423SJohn Johansen 
559*0d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
560*0d259f04SJohn Johansen 	if (IS_ERR(dir))
561*0d259f04SJohn Johansen 		return PTR_ERR(dir);
562*0d259f04SJohn Johansen 	fs_dir->dentry = dir;
56363e2b423SJohn Johansen 
564*0d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
5659acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
5669acd494bSKees Cook 			error = aafs_create_dir(fs_file, fs_dir->dentry);
5679acd494bSKees Cook 		else
5689acd494bSKees Cook 			error = aafs_create_file(fs_file, fs_dir->dentry);
5699acd494bSKees Cook 		if (error)
5709acd494bSKees Cook 			goto failed;
5719acd494bSKees Cook 	}
5729acd494bSKees Cook 
5739acd494bSKees Cook 	return 0;
5749acd494bSKees Cook 
5759acd494bSKees Cook failed:
576*0d259f04SJohn Johansen 	aafs_remove_dir(fs_dir);
577*0d259f04SJohn Johansen 
5789acd494bSKees Cook 	return error;
5799acd494bSKees Cook }
5809acd494bSKees Cook 
5819acd494bSKees Cook /**
5829acd494bSKees Cook  * aafs_remove_file - drop a single file entry in the apparmor securityfs
5839acd494bSKees Cook  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
5849acd494bSKees Cook  */
5859acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
5869acd494bSKees Cook {
5879acd494bSKees Cook 	if (!fs_file->dentry)
5889acd494bSKees Cook 		return;
5899acd494bSKees Cook 
5909acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
5919acd494bSKees Cook 	fs_file->dentry = NULL;
5929acd494bSKees Cook }
5939acd494bSKees Cook 
5949acd494bSKees Cook /**
5959acd494bSKees Cook  * aafs_remove_dir - recursively drop a directory entry from the securityfs
5969acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
5979acd494bSKees Cook  */
5989acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
5999acd494bSKees Cook {
6009acd494bSKees Cook 	struct aa_fs_entry *fs_file;
6019acd494bSKees Cook 
602*0d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
6039acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
6049acd494bSKees Cook 			aafs_remove_dir(fs_file);
6059acd494bSKees Cook 		else
6069acd494bSKees Cook 			aafs_remove_file(fs_file);
6079acd494bSKees Cook 	}
6089acd494bSKees Cook 
6099acd494bSKees Cook 	aafs_remove_file(fs_dir);
61063e2b423SJohn Johansen }
61163e2b423SJohn Johansen 
61263e2b423SJohn Johansen /**
61363e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
61463e2b423SJohn Johansen  *
61563e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
61663e2b423SJohn Johansen  */
61763e2b423SJohn Johansen void __init aa_destroy_aafs(void)
61863e2b423SJohn Johansen {
6199acd494bSKees Cook 	aafs_remove_dir(&aa_fs_entry);
62063e2b423SJohn Johansen }
62163e2b423SJohn Johansen 
62263e2b423SJohn Johansen /**
62363e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
62463e2b423SJohn Johansen  *
62563e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
62663e2b423SJohn Johansen  *
62763e2b423SJohn Johansen  * Returns: error on failure
62863e2b423SJohn Johansen  */
6293417d8d5SJames Morris static int __init aa_create_aafs(void)
63063e2b423SJohn Johansen {
63163e2b423SJohn Johansen 	int error;
63263e2b423SJohn Johansen 
63363e2b423SJohn Johansen 	if (!apparmor_initialized)
63463e2b423SJohn Johansen 		return 0;
63563e2b423SJohn Johansen 
6369acd494bSKees Cook 	if (aa_fs_entry.dentry) {
63763e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
63863e2b423SJohn Johansen 		return -EEXIST;
63963e2b423SJohn Johansen 	}
64063e2b423SJohn Johansen 
6419acd494bSKees Cook 	/* Populate fs tree. */
6429acd494bSKees Cook 	error = aafs_create_dir(&aa_fs_entry, NULL);
64363e2b423SJohn Johansen 	if (error)
64463e2b423SJohn Johansen 		goto error;
64563e2b423SJohn Johansen 
646*0d259f04SJohn Johansen 	error = __aa_fs_namespace_mkdir(root_ns, aa_fs_entry.dentry,
647*0d259f04SJohn Johansen 					"policy");
648*0d259f04SJohn Johansen 	if (error)
649*0d259f04SJohn Johansen 		goto error;
650*0d259f04SJohn Johansen 
65163e2b423SJohn Johansen 	/* TODO: add support for apparmorfs_null and apparmorfs_mnt */
65263e2b423SJohn Johansen 
65363e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
65463e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
65563e2b423SJohn Johansen 	return 0;
65663e2b423SJohn Johansen 
65763e2b423SJohn Johansen error:
65863e2b423SJohn Johansen 	aa_destroy_aafs();
65963e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
66063e2b423SJohn Johansen 	return error;
66163e2b423SJohn Johansen }
66263e2b423SJohn Johansen 
66363e2b423SJohn Johansen fs_initcall(aa_create_aafs);
664