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 <linux/fs.h>
26a481f4d9SJohn Johansen #include <uapi/linux/major.h>
27a481f4d9SJohn Johansen #include <uapi/linux/magic.h>
2863e2b423SJohn Johansen 
2963e2b423SJohn Johansen #include "include/apparmor.h"
3063e2b423SJohn Johansen #include "include/apparmorfs.h"
3163e2b423SJohn Johansen #include "include/audit.h"
3263e2b423SJohn Johansen #include "include/context.h"
33f8eb8a13SJohn Johansen #include "include/crypto.h"
3463e2b423SJohn Johansen #include "include/policy.h"
35cff281f6SJohn Johansen #include "include/policy_ns.h"
36d384b0a1SKees Cook #include "include/resource.h"
375ac8c355SJohn Johansen #include "include/policy_unpack.h"
3863e2b423SJohn Johansen 
39c97204baSJohn Johansen /*
40c97204baSJohn Johansen  * The apparmor filesystem interface used for policy load and introspection
41c97204baSJohn Johansen  * The interface is split into two main components based on their function
42c97204baSJohn Johansen  * a securityfs component:
43c97204baSJohn Johansen  *   used for static files that are always available, and which allows
44c97204baSJohn Johansen  *   userspace to specificy the location of the security filesystem.
45c97204baSJohn Johansen  *
46c97204baSJohn Johansen  *   fns and data are prefixed with
47c97204baSJohn Johansen  *      aa_sfs_
48c97204baSJohn Johansen  *
49c97204baSJohn Johansen  * an apparmorfs component:
50c97204baSJohn Johansen  *   used loaded policy content and introspection. It is not part of  a
51c97204baSJohn Johansen  *   regular mounted filesystem and is available only through the magic
52c97204baSJohn Johansen  *   policy symlink in the root of the securityfs apparmor/ directory.
53c97204baSJohn Johansen  *   Tasks queries will be magically redirected to the correct portion
54c97204baSJohn Johansen  *   of the policy tree based on their confinement.
55c97204baSJohn Johansen  *
56c97204baSJohn Johansen  *   fns and data are prefixed with
57c97204baSJohn Johansen  *      aafs_
58c97204baSJohn Johansen  *
59c97204baSJohn Johansen  * The aa_fs_ prefix is used to indicate the fn is used by both the
60c97204baSJohn Johansen  * securityfs and apparmorfs filesystems.
61c97204baSJohn Johansen  */
62c97204baSJohn Johansen 
63c97204baSJohn Johansen 
64c97204baSJohn Johansen /*
65c97204baSJohn Johansen  * support fns
66c97204baSJohn Johansen  */
67c97204baSJohn Johansen 
6863e2b423SJohn Johansen /**
690d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
700d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
710d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
720d259f04SJohn Johansen  *
730d259f04SJohn Johansen  * Returns: length of mangled name
740d259f04SJohn Johansen  */
75bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
760d259f04SJohn Johansen {
770d259f04SJohn Johansen 	char *t = target;
780d259f04SJohn Johansen 
790d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
800d259f04SJohn Johansen 		name++;
810d259f04SJohn Johansen 
820d259f04SJohn Johansen 	if (target) {
830d259f04SJohn Johansen 		for (; *name; name++) {
840d259f04SJohn Johansen 			if (*name == '/')
850d259f04SJohn Johansen 				*(t)++ = '.';
860d259f04SJohn Johansen 			else if (isspace(*name))
870d259f04SJohn Johansen 				*(t)++ = '_';
880d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
890d259f04SJohn Johansen 				*(t)++ = *name;
900d259f04SJohn Johansen 		}
910d259f04SJohn Johansen 
920d259f04SJohn Johansen 		*t = 0;
930d259f04SJohn Johansen 	} else {
940d259f04SJohn Johansen 		int len = 0;
950d259f04SJohn Johansen 		for (; *name; name++) {
960d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
970d259f04SJohn Johansen 			    strchr("/._-", *name))
980d259f04SJohn Johansen 				len++;
990d259f04SJohn Johansen 		}
1000d259f04SJohn Johansen 
1010d259f04SJohn Johansen 		return len;
1020d259f04SJohn Johansen 	}
1030d259f04SJohn Johansen 
1040d259f04SJohn Johansen 	return t - target;
1050d259f04SJohn Johansen }
1060d259f04SJohn Johansen 
107a481f4d9SJohn Johansen 
108a481f4d9SJohn Johansen /*
109a481f4d9SJohn Johansen  * aafs - core fns and data for the policy tree
110a481f4d9SJohn Johansen  */
111a481f4d9SJohn Johansen 
112a481f4d9SJohn Johansen #define AAFS_NAME		"apparmorfs"
113a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt;
114a481f4d9SJohn Johansen static int aafs_count;
115a481f4d9SJohn Johansen 
116a481f4d9SJohn Johansen 
117a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
118a481f4d9SJohn Johansen {
119a481f4d9SJohn Johansen 	struct inode *inode = d_inode(dentry);
120a481f4d9SJohn Johansen 
121a481f4d9SJohn Johansen 	seq_printf(seq, "%s:[%lu]", AAFS_NAME, inode->i_ino);
122a481f4d9SJohn Johansen 	return 0;
123a481f4d9SJohn Johansen }
124a481f4d9SJohn Johansen 
125a481f4d9SJohn Johansen static void aafs_evict_inode(struct inode *inode)
126a481f4d9SJohn Johansen {
127a481f4d9SJohn Johansen 	truncate_inode_pages_final(&inode->i_data);
128a481f4d9SJohn Johansen 	clear_inode(inode);
129a481f4d9SJohn Johansen 	if (S_ISLNK(inode->i_mode))
130a481f4d9SJohn Johansen 		kfree(inode->i_link);
131a481f4d9SJohn Johansen }
132a481f4d9SJohn Johansen 
133a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = {
134a481f4d9SJohn Johansen 	.statfs = simple_statfs,
135a481f4d9SJohn Johansen 	.evict_inode = aafs_evict_inode,
136a481f4d9SJohn Johansen 	.show_path = aafs_show_path,
137a481f4d9SJohn Johansen };
138a481f4d9SJohn Johansen 
139a481f4d9SJohn Johansen static int fill_super(struct super_block *sb, void *data, int silent)
140a481f4d9SJohn Johansen {
141a481f4d9SJohn Johansen 	static struct tree_descr files[] = { {""} };
142a481f4d9SJohn Johansen 	int error;
143a481f4d9SJohn Johansen 
144a481f4d9SJohn Johansen 	error = simple_fill_super(sb, AAFS_MAGIC, files);
145a481f4d9SJohn Johansen 	if (error)
146a481f4d9SJohn Johansen 		return error;
147a481f4d9SJohn Johansen 	sb->s_op = &aafs_super_ops;
148a481f4d9SJohn Johansen 
149a481f4d9SJohn Johansen 	return 0;
150a481f4d9SJohn Johansen }
151a481f4d9SJohn Johansen 
152a481f4d9SJohn Johansen static struct dentry *aafs_mount(struct file_system_type *fs_type,
153a481f4d9SJohn Johansen 				 int flags, const char *dev_name, void *data)
154a481f4d9SJohn Johansen {
155a481f4d9SJohn Johansen 	return mount_single(fs_type, flags, data, fill_super);
156a481f4d9SJohn Johansen }
157a481f4d9SJohn Johansen 
158a481f4d9SJohn Johansen static struct file_system_type aafs_ops = {
159a481f4d9SJohn Johansen 	.owner = THIS_MODULE,
160a481f4d9SJohn Johansen 	.name = AAFS_NAME,
161a481f4d9SJohn Johansen 	.mount = aafs_mount,
162a481f4d9SJohn Johansen 	.kill_sb = kill_anon_super,
163a481f4d9SJohn Johansen };
164a481f4d9SJohn Johansen 
165a481f4d9SJohn Johansen /**
166a481f4d9SJohn Johansen  * __aafs_setup_d_inode - basic inode setup for apparmorfs
167a481f4d9SJohn Johansen  * @dir: parent directory for the dentry
168a481f4d9SJohn Johansen  * @dentry: dentry we are seting the inode up for
169a481f4d9SJohn Johansen  * @mode: permissions the file should have
170a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
171a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
172a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used
173a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
174a481f4d9SJohn Johansen  */
175a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
176a481f4d9SJohn Johansen 			       umode_t mode, void *data, char *link,
177a481f4d9SJohn Johansen 			       const struct file_operations *fops,
178a481f4d9SJohn Johansen 			       const struct inode_operations *iops)
179a481f4d9SJohn Johansen {
180a481f4d9SJohn Johansen 	struct inode *inode = new_inode(dir->i_sb);
181a481f4d9SJohn Johansen 
182a481f4d9SJohn Johansen 	AA_BUG(!dir);
183a481f4d9SJohn Johansen 	AA_BUG(!dentry);
184a481f4d9SJohn Johansen 
185a481f4d9SJohn Johansen 	if (!inode)
186a481f4d9SJohn Johansen 		return -ENOMEM;
187a481f4d9SJohn Johansen 
188a481f4d9SJohn Johansen 	inode->i_ino = get_next_ino();
189a481f4d9SJohn Johansen 	inode->i_mode = mode;
190a481f4d9SJohn Johansen 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
191a481f4d9SJohn Johansen 	inode->i_private = data;
192a481f4d9SJohn Johansen 	if (S_ISDIR(mode)) {
193a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_dir_inode_operations;
194a481f4d9SJohn Johansen 		inode->i_fop = &simple_dir_operations;
195a481f4d9SJohn Johansen 		inc_nlink(inode);
196a481f4d9SJohn Johansen 		inc_nlink(dir);
197a481f4d9SJohn Johansen 	} else if (S_ISLNK(mode)) {
198a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_symlink_inode_operations;
199a481f4d9SJohn Johansen 		inode->i_link = link;
200a481f4d9SJohn Johansen 	} else {
201a481f4d9SJohn Johansen 		inode->i_fop = fops;
202a481f4d9SJohn Johansen 	}
203a481f4d9SJohn Johansen 	d_instantiate(dentry, inode);
204a481f4d9SJohn Johansen 	dget(dentry);
205a481f4d9SJohn Johansen 
206a481f4d9SJohn Johansen 	return 0;
207a481f4d9SJohn Johansen }
208a481f4d9SJohn Johansen 
209a481f4d9SJohn Johansen /**
210a481f4d9SJohn Johansen  * aafs_create - create a dentry in the apparmorfs filesystem
211a481f4d9SJohn Johansen  *
212a481f4d9SJohn Johansen  * @name: name of dentry to create
213a481f4d9SJohn Johansen  * @mode: permissions the file should have
214a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
215a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
216a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
217a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
218a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
219a481f4d9SJohn Johansen  *
220a481f4d9SJohn Johansen  * This is the basic "create a xxx" function for apparmorfs.
221a481f4d9SJohn Johansen  *
222a481f4d9SJohn Johansen  * Returns a pointer to a dentry if it succeeds, that must be free with
223a481f4d9SJohn Johansen  * aafs_remove(). Will return ERR_PTR on failure.
224a481f4d9SJohn Johansen  */
225a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode,
226a481f4d9SJohn Johansen 				  struct dentry *parent, void *data, void *link,
227a481f4d9SJohn Johansen 				  const struct file_operations *fops,
228a481f4d9SJohn Johansen 				  const struct inode_operations *iops)
229a481f4d9SJohn Johansen {
230a481f4d9SJohn Johansen 	struct dentry *dentry;
231a481f4d9SJohn Johansen 	struct inode *dir;
232a481f4d9SJohn Johansen 	int error;
233a481f4d9SJohn Johansen 
234a481f4d9SJohn Johansen 	AA_BUG(!name);
235a481f4d9SJohn Johansen 	AA_BUG(!parent);
236a481f4d9SJohn Johansen 
237a481f4d9SJohn Johansen 	if (!(mode & S_IFMT))
238a481f4d9SJohn Johansen 		mode = (mode & S_IALLUGO) | S_IFREG;
239a481f4d9SJohn Johansen 
240a481f4d9SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
241a481f4d9SJohn Johansen 	if (error)
242a481f4d9SJohn Johansen 		return ERR_PTR(error);
243a481f4d9SJohn Johansen 
244a481f4d9SJohn Johansen 	dir = d_inode(parent);
245a481f4d9SJohn Johansen 
246a481f4d9SJohn Johansen 	inode_lock(dir);
247a481f4d9SJohn Johansen 	dentry = lookup_one_len(name, parent, strlen(name));
248a481f4d9SJohn Johansen 	if (IS_ERR(dentry))
249a481f4d9SJohn Johansen 		goto fail_lock;
250a481f4d9SJohn Johansen 
251a481f4d9SJohn Johansen 	if (d_really_is_positive(dentry)) {
252a481f4d9SJohn Johansen 		error = -EEXIST;
253a481f4d9SJohn Johansen 		goto fail_dentry;
254a481f4d9SJohn Johansen 	}
255a481f4d9SJohn Johansen 
256a481f4d9SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
257a481f4d9SJohn Johansen 	if (error)
258a481f4d9SJohn Johansen 		goto fail_dentry;
259a481f4d9SJohn Johansen 	inode_unlock(dir);
260a481f4d9SJohn Johansen 
261a481f4d9SJohn Johansen 	return dentry;
262a481f4d9SJohn Johansen 
263a481f4d9SJohn Johansen fail_dentry:
264a481f4d9SJohn Johansen 	dput(dentry);
265a481f4d9SJohn Johansen 
266a481f4d9SJohn Johansen fail_lock:
267a481f4d9SJohn Johansen 	inode_unlock(dir);
268a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
269a481f4d9SJohn Johansen 
270a481f4d9SJohn Johansen 	return ERR_PTR(error);
271a481f4d9SJohn Johansen }
272a481f4d9SJohn Johansen 
273a481f4d9SJohn Johansen /**
274a481f4d9SJohn Johansen  * aafs_create_file - create a file in the apparmorfs filesystem
275a481f4d9SJohn Johansen  *
276a481f4d9SJohn Johansen  * @name: name of dentry to create
277a481f4d9SJohn Johansen  * @mode: permissions the file should have
278a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
279a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
280a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
281a481f4d9SJohn Johansen  *
282a481f4d9SJohn Johansen  * see aafs_create
283a481f4d9SJohn Johansen  */
284a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode,
285a481f4d9SJohn Johansen 				       struct dentry *parent, void *data,
286a481f4d9SJohn Johansen 				       const struct file_operations *fops)
287a481f4d9SJohn Johansen {
288a481f4d9SJohn Johansen 	return aafs_create(name, mode, parent, data, NULL, fops, NULL);
289a481f4d9SJohn Johansen }
290a481f4d9SJohn Johansen 
291a481f4d9SJohn Johansen /**
292a481f4d9SJohn Johansen  * aafs_create_dir - create a directory in the apparmorfs filesystem
293a481f4d9SJohn Johansen  *
294a481f4d9SJohn Johansen  * @name: name of dentry to create
295a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
296a481f4d9SJohn Johansen  *
297a481f4d9SJohn Johansen  * see aafs_create
298a481f4d9SJohn Johansen  */
299a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
300a481f4d9SJohn Johansen {
301a481f4d9SJohn Johansen 	return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
302a481f4d9SJohn Johansen 			   NULL);
303a481f4d9SJohn Johansen }
304a481f4d9SJohn Johansen 
305a481f4d9SJohn Johansen /**
306a481f4d9SJohn Johansen  * aafs_create_symlink - create a symlink in the apparmorfs filesystem
307a481f4d9SJohn Johansen  * @name: name of dentry to create
308a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
309a481f4d9SJohn Johansen  * @target: if symlink, symlink target string
310a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
311a481f4d9SJohn Johansen  *
312a481f4d9SJohn Johansen  * If @target parameter is %NULL, then the @iops parameter needs to be
313a481f4d9SJohn Johansen  * setup to handle .readlink and .get_link inode_operations.
314a481f4d9SJohn Johansen  */
315a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name,
316a481f4d9SJohn Johansen 					  struct dentry *parent,
317a481f4d9SJohn Johansen 					  const char *target,
318a481f4d9SJohn Johansen 					  const struct inode_operations *iops)
319a481f4d9SJohn Johansen {
320a481f4d9SJohn Johansen 	struct dentry *dent;
321a481f4d9SJohn Johansen 	char *link = NULL;
322a481f4d9SJohn Johansen 
323a481f4d9SJohn Johansen 	if (target) {
324a481f4d9SJohn Johansen 		link = kstrdup(target, GFP_KERNEL);
325a481f4d9SJohn Johansen 		if (!link)
326a481f4d9SJohn Johansen 			return ERR_PTR(-ENOMEM);
327a481f4d9SJohn Johansen 	}
328a481f4d9SJohn Johansen 	dent = aafs_create(name, S_IFLNK | 0444, parent, NULL, link, NULL,
329a481f4d9SJohn Johansen 			   iops);
330a481f4d9SJohn Johansen 	if (IS_ERR(dent))
331a481f4d9SJohn Johansen 		kfree(link);
332a481f4d9SJohn Johansen 
333a481f4d9SJohn Johansen 	return dent;
334a481f4d9SJohn Johansen }
335a481f4d9SJohn Johansen 
336a481f4d9SJohn Johansen /**
337a481f4d9SJohn Johansen  * aafs_remove - removes a file or directory from the apparmorfs filesystem
338a481f4d9SJohn Johansen  *
339a481f4d9SJohn Johansen  * @dentry: dentry of the file/directory/symlink to removed.
340a481f4d9SJohn Johansen  */
341a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry)
342a481f4d9SJohn Johansen {
343a481f4d9SJohn Johansen 	struct inode *dir;
344a481f4d9SJohn Johansen 
345a481f4d9SJohn Johansen 	if (!dentry || IS_ERR(dentry))
346a481f4d9SJohn Johansen 		return;
347a481f4d9SJohn Johansen 
348a481f4d9SJohn Johansen 	dir = d_inode(dentry->d_parent);
349a481f4d9SJohn Johansen 	inode_lock(dir);
350a481f4d9SJohn Johansen 	if (simple_positive(dentry)) {
351a481f4d9SJohn Johansen 		if (d_is_dir(dentry))
352a481f4d9SJohn Johansen 			simple_rmdir(dir, dentry);
353a481f4d9SJohn Johansen 		else
354a481f4d9SJohn Johansen 			simple_unlink(dir, dentry);
355a481f4d9SJohn Johansen 		dput(dentry);
356a481f4d9SJohn Johansen 	}
357a481f4d9SJohn Johansen 	inode_unlock(dir);
358a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
359a481f4d9SJohn Johansen }
360a481f4d9SJohn Johansen 
361a481f4d9SJohn Johansen 
362a481f4d9SJohn Johansen /*
363a481f4d9SJohn Johansen  * aa_fs - policy load/replace/remove
364a481f4d9SJohn Johansen  */
365a481f4d9SJohn Johansen 
3660d259f04SJohn Johansen /**
36763e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
36863e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
3693ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
37063e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
37163e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
37263e2b423SJohn Johansen  *
37363e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
37463e2b423SJohn Johansen  *          ERR_PTR on failure.
37563e2b423SJohn Johansen  */
3765ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
3775ac8c355SJohn Johansen 						     size_t alloc_size,
3785ac8c355SJohn Johansen 						     size_t copy_size,
37963e2b423SJohn Johansen 						     loff_t *pos)
38063e2b423SJohn Johansen {
3815ac8c355SJohn Johansen 	struct aa_loaddata *data;
38263e2b423SJohn Johansen 
383e6bfa25dSJohn Johansen 	AA_BUG(copy_size > alloc_size);
3843ed02adaSJohn Johansen 
38563e2b423SJohn Johansen 	if (*pos != 0)
38663e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
38763e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
38863e2b423SJohn Johansen 
38963e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
3905d5182caSJohn Johansen 	data = aa_loaddata_alloc(alloc_size);
3915d5182caSJohn Johansen 	if (IS_ERR(data))
3925d5182caSJohn Johansen 		return data;
39363e2b423SJohn Johansen 
3945d5182caSJohn Johansen 	data->size = copy_size;
3955ac8c355SJohn Johansen 	if (copy_from_user(data->data, userbuf, copy_size)) {
39663e2b423SJohn Johansen 		kvfree(data);
39763e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
39863e2b423SJohn Johansen 	}
39963e2b423SJohn Johansen 
40063e2b423SJohn Johansen 	return data;
40163e2b423SJohn Johansen }
40263e2b423SJohn Johansen 
40318e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
404b7fd2c03SJohn Johansen 			     loff_t *pos, struct aa_ns *ns)
4055ac8c355SJohn Johansen {
4065ac8c355SJohn Johansen 	ssize_t error;
4075ac8c355SJohn Johansen 	struct aa_loaddata *data;
4085ac8c355SJohn Johansen 	struct aa_profile *profile = aa_current_profile();
4095ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4105ac8c355SJohn Johansen 	 * below after unpack
4115ac8c355SJohn Johansen 	 */
41218e99f19SJohn Johansen 	error = aa_may_manage_policy(profile, ns, mask);
4135ac8c355SJohn Johansen 	if (error)
4145ac8c355SJohn Johansen 		return error;
41563e2b423SJohn Johansen 
4165ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size, size, pos);
4175ac8c355SJohn Johansen 	error = PTR_ERR(data);
4185ac8c355SJohn Johansen 	if (!IS_ERR(data)) {
419b7fd2c03SJohn Johansen 		error = aa_replace_profiles(ns ? ns : profile->ns, profile,
42018e99f19SJohn Johansen 					    mask, data);
4215ac8c355SJohn Johansen 		aa_put_loaddata(data);
4225ac8c355SJohn Johansen 	}
4235ac8c355SJohn Johansen 
4245ac8c355SJohn Johansen 	return error;
4255ac8c355SJohn Johansen }
4265ac8c355SJohn Johansen 
427b7fd2c03SJohn Johansen /* .load file hook fn to load policy */
42863e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
42963e2b423SJohn Johansen 			    loff_t *pos)
43063e2b423SJohn Johansen {
431b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
43218e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
433b7fd2c03SJohn Johansen 
434b7fd2c03SJohn Johansen 	aa_put_ns(ns);
43563e2b423SJohn Johansen 
43663e2b423SJohn Johansen 	return error;
43763e2b423SJohn Johansen }
43863e2b423SJohn Johansen 
43963e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
4406038f373SArnd Bergmann 	.write = profile_load,
4416038f373SArnd Bergmann 	.llseek = default_llseek,
44263e2b423SJohn Johansen };
44363e2b423SJohn Johansen 
44463e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
44563e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
44663e2b423SJohn Johansen 			       size_t size, loff_t *pos)
44763e2b423SJohn Johansen {
448b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
44918e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
45018e99f19SJohn Johansen 				  buf, size, pos, ns);
451b7fd2c03SJohn Johansen 	aa_put_ns(ns);
45263e2b423SJohn Johansen 
45363e2b423SJohn Johansen 	return error;
45463e2b423SJohn Johansen }
45563e2b423SJohn Johansen 
45663e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
4576038f373SArnd Bergmann 	.write = profile_replace,
4586038f373SArnd Bergmann 	.llseek = default_llseek,
45963e2b423SJohn Johansen };
46063e2b423SJohn Johansen 
461b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */
46263e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
46363e2b423SJohn Johansen 			      size_t size, loff_t *pos)
46463e2b423SJohn Johansen {
4655ac8c355SJohn Johansen 	struct aa_loaddata *data;
4665ac8c355SJohn Johansen 	struct aa_profile *profile;
46763e2b423SJohn Johansen 	ssize_t error;
468b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
46963e2b423SJohn Johansen 
4705ac8c355SJohn Johansen 	profile = aa_current_profile();
4715ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4725ac8c355SJohn Johansen 	 * below after unpack
4735ac8c355SJohn Johansen 	 */
47418e99f19SJohn Johansen 	error = aa_may_manage_policy(profile, ns, AA_MAY_REMOVE_POLICY);
4755ac8c355SJohn Johansen 	if (error)
4765ac8c355SJohn Johansen 		goto out;
4775ac8c355SJohn Johansen 
47863e2b423SJohn Johansen 	/*
47963e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
48063e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
48163e2b423SJohn Johansen 	 */
4825ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
48363e2b423SJohn Johansen 
48463e2b423SJohn Johansen 	error = PTR_ERR(data);
48563e2b423SJohn Johansen 	if (!IS_ERR(data)) {
4865ac8c355SJohn Johansen 		data->data[size] = 0;
487b7fd2c03SJohn Johansen 		error = aa_remove_profiles(ns ? ns : profile->ns, profile,
488b7fd2c03SJohn Johansen 					   data->data, size);
4895ac8c355SJohn Johansen 		aa_put_loaddata(data);
49063e2b423SJohn Johansen 	}
4915ac8c355SJohn Johansen  out:
492b7fd2c03SJohn Johansen 	aa_put_ns(ns);
49363e2b423SJohn Johansen 	return error;
49463e2b423SJohn Johansen }
49563e2b423SJohn Johansen 
49663e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
4976038f373SArnd Bergmann 	.write = profile_remove,
4986038f373SArnd Bergmann 	.llseek = default_llseek,
49963e2b423SJohn Johansen };
50063e2b423SJohn Johansen 
5015d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns)
5025d5182caSJohn Johansen {
5035d5182caSJohn Johansen 	ns->revision++;
5045d5182caSJohn Johansen }
5055d5182caSJohn Johansen 
506e025be0fSWilliam Hua /**
507e025be0fSWilliam Hua  * query_data - queries a policy and writes its data to buf
508e025be0fSWilliam Hua  * @buf: the resulting data is stored here (NOT NULL)
509e025be0fSWilliam Hua  * @buf_len: size of buf
510e025be0fSWilliam Hua  * @query: query string used to retrieve data
511e025be0fSWilliam Hua  * @query_len: size of query including second NUL byte
512e025be0fSWilliam Hua  *
513e025be0fSWilliam Hua  * The buffers pointed to by buf and query may overlap. The query buffer is
514e025be0fSWilliam Hua  * parsed before buf is written to.
515e025be0fSWilliam Hua  *
516e025be0fSWilliam Hua  * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
517e025be0fSWilliam Hua  * the security confinement context and <KEY> is the name of the data to
518e025be0fSWilliam Hua  * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
519e025be0fSWilliam Hua  *
520e025be0fSWilliam Hua  * Don't expect the contents of buf to be preserved on failure.
521e025be0fSWilliam Hua  *
522e025be0fSWilliam Hua  * Returns: number of characters written to buf or -errno on failure
523e025be0fSWilliam Hua  */
524e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len,
525e025be0fSWilliam Hua 			  char *query, size_t query_len)
526e025be0fSWilliam Hua {
527e025be0fSWilliam Hua 	char *out;
528e025be0fSWilliam Hua 	const char *key;
529e025be0fSWilliam Hua 	struct aa_profile *profile;
530e025be0fSWilliam Hua 	struct aa_data *data;
531e025be0fSWilliam Hua 	u32 bytes, blocks;
532e025be0fSWilliam Hua 	__le32 outle32;
533e025be0fSWilliam Hua 
534e025be0fSWilliam Hua 	if (!query_len)
535e025be0fSWilliam Hua 		return -EINVAL; /* need a query */
536e025be0fSWilliam Hua 
537e025be0fSWilliam Hua 	key = query + strnlen(query, query_len) + 1;
538e025be0fSWilliam Hua 	if (key + 1 >= query + query_len)
539e025be0fSWilliam Hua 		return -EINVAL; /* not enough space for a non-empty key */
540e025be0fSWilliam Hua 	if (key + strnlen(key, query + query_len - key) >= query + query_len)
541e025be0fSWilliam Hua 		return -EINVAL; /* must end with NUL */
542e025be0fSWilliam Hua 
543e025be0fSWilliam Hua 	if (buf_len < sizeof(bytes) + sizeof(blocks))
544e025be0fSWilliam Hua 		return -EINVAL; /* not enough space */
545e025be0fSWilliam Hua 
546e025be0fSWilliam Hua 	profile = aa_current_profile();
547e025be0fSWilliam Hua 
548e025be0fSWilliam Hua 	/* We are going to leave space for two numbers. The first is the total
549e025be0fSWilliam Hua 	 * number of bytes we are writing after the first number. This is so
550e025be0fSWilliam Hua 	 * users can read the full output without reallocation.
551e025be0fSWilliam Hua 	 *
552e025be0fSWilliam Hua 	 * The second number is the number of data blocks we're writing. An
553e025be0fSWilliam Hua 	 * application might be confined by multiple policies having data in
554e025be0fSWilliam Hua 	 * the same key.
555e025be0fSWilliam Hua 	 */
556e025be0fSWilliam Hua 	memset(buf, 0, sizeof(bytes) + sizeof(blocks));
557e025be0fSWilliam Hua 	out = buf + sizeof(bytes) + sizeof(blocks);
558e025be0fSWilliam Hua 
559e025be0fSWilliam Hua 	blocks = 0;
560e025be0fSWilliam Hua 	if (profile->data) {
561e025be0fSWilliam Hua 		data = rhashtable_lookup_fast(profile->data, &key,
562e025be0fSWilliam Hua 					      profile->data->p);
563e025be0fSWilliam Hua 
564e025be0fSWilliam Hua 		if (data) {
565e025be0fSWilliam Hua 			if (out + sizeof(outle32) + data->size > buf + buf_len)
566e025be0fSWilliam Hua 				return -EINVAL; /* not enough space */
567e025be0fSWilliam Hua 			outle32 = __cpu_to_le32(data->size);
568e025be0fSWilliam Hua 			memcpy(out, &outle32, sizeof(outle32));
569e025be0fSWilliam Hua 			out += sizeof(outle32);
570e025be0fSWilliam Hua 			memcpy(out, data->data, data->size);
571e025be0fSWilliam Hua 			out += data->size;
572e025be0fSWilliam Hua 			blocks++;
573e025be0fSWilliam Hua 		}
574e025be0fSWilliam Hua 	}
575e025be0fSWilliam Hua 
576e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
577e025be0fSWilliam Hua 	memcpy(buf, &outle32, sizeof(outle32));
578e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(blocks);
579e025be0fSWilliam Hua 	memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
580e025be0fSWilliam Hua 
581e025be0fSWilliam Hua 	return out - buf;
582e025be0fSWilliam Hua }
583e025be0fSWilliam Hua 
584e025be0fSWilliam Hua #define QUERY_CMD_DATA		"data\0"
585e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN	5
586e025be0fSWilliam Hua 
587e025be0fSWilliam Hua /**
588e025be0fSWilliam Hua  * aa_write_access - generic permissions and data query
589e025be0fSWilliam Hua  * @file: pointer to open apparmorfs/access file
590e025be0fSWilliam Hua  * @ubuf: user buffer containing the complete query string (NOT NULL)
591e025be0fSWilliam Hua  * @count: size of ubuf
592e025be0fSWilliam Hua  * @ppos: position in the file (MUST BE ZERO)
593e025be0fSWilliam Hua  *
594e025be0fSWilliam Hua  * Allows for one permissions or data query per open(), write(), and read()
595e025be0fSWilliam Hua  * sequence. The only queries currently supported are label-based queries for
596e025be0fSWilliam Hua  * permissions or data.
597e025be0fSWilliam Hua  *
598e025be0fSWilliam Hua  * For permissions queries, ubuf must begin with "label\0", followed by the
599e025be0fSWilliam Hua  * profile query specific format described in the query_label() function
600e025be0fSWilliam Hua  * documentation.
601e025be0fSWilliam Hua  *
602e025be0fSWilliam Hua  * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
603e025be0fSWilliam Hua  * <LABEL> is the name of the security confinement context and <KEY> is the
604e025be0fSWilliam Hua  * name of the data to retrieve.
605e025be0fSWilliam Hua  *
606e025be0fSWilliam Hua  * Returns: number of bytes written or -errno on failure
607e025be0fSWilliam Hua  */
608e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
609e025be0fSWilliam Hua 			       size_t count, loff_t *ppos)
610e025be0fSWilliam Hua {
611e025be0fSWilliam Hua 	char *buf;
612e025be0fSWilliam Hua 	ssize_t len;
613e025be0fSWilliam Hua 
614e025be0fSWilliam Hua 	if (*ppos)
615e025be0fSWilliam Hua 		return -ESPIPE;
616e025be0fSWilliam Hua 
617e025be0fSWilliam Hua 	buf = simple_transaction_get(file, ubuf, count);
618e025be0fSWilliam Hua 	if (IS_ERR(buf))
619e025be0fSWilliam Hua 		return PTR_ERR(buf);
620e025be0fSWilliam Hua 
621e025be0fSWilliam Hua 	if (count > QUERY_CMD_DATA_LEN &&
622e025be0fSWilliam Hua 		   !memcmp(buf, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
623e025be0fSWilliam Hua 		len = query_data(buf, SIMPLE_TRANSACTION_LIMIT,
624e025be0fSWilliam Hua 				 buf + QUERY_CMD_DATA_LEN,
625e025be0fSWilliam Hua 				 count - QUERY_CMD_DATA_LEN);
626e025be0fSWilliam Hua 	} else
627e025be0fSWilliam Hua 		len = -EINVAL;
628e025be0fSWilliam Hua 
629e025be0fSWilliam Hua 	if (len < 0)
630e025be0fSWilliam Hua 		return len;
631e025be0fSWilliam Hua 
632e025be0fSWilliam Hua 	simple_transaction_set(file, len);
633e025be0fSWilliam Hua 
634e025be0fSWilliam Hua 	return count;
635e025be0fSWilliam Hua }
636e025be0fSWilliam Hua 
637c97204baSJohn Johansen static const struct file_operations aa_sfs_access = {
638e025be0fSWilliam Hua 	.write		= aa_write_access,
639e025be0fSWilliam Hua 	.read		= simple_transaction_read,
640e025be0fSWilliam Hua 	.release	= simple_transaction_release,
641e025be0fSWilliam Hua 	.llseek		= generic_file_llseek,
642e025be0fSWilliam Hua };
643e025be0fSWilliam Hua 
644c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v)
645e74abcf3SKees Cook {
646c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file = seq->private;
647e74abcf3SKees Cook 
648e74abcf3SKees Cook 	if (!fs_file)
649e74abcf3SKees Cook 		return 0;
650e74abcf3SKees Cook 
651e74abcf3SKees Cook 	switch (fs_file->v_type) {
652c97204baSJohn Johansen 	case AA_SFS_TYPE_BOOLEAN:
653e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
654e74abcf3SKees Cook 		break;
655c97204baSJohn Johansen 	case AA_SFS_TYPE_STRING:
656a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
657a9bf8e9fSKees Cook 		break;
658c97204baSJohn Johansen 	case AA_SFS_TYPE_U64:
659e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
660e74abcf3SKees Cook 		break;
661e74abcf3SKees Cook 	default:
662e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
663e74abcf3SKees Cook 		break;
664e74abcf3SKees Cook 	}
665e74abcf3SKees Cook 
666e74abcf3SKees Cook 	return 0;
667e74abcf3SKees Cook }
668e74abcf3SKees Cook 
669c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file)
670e74abcf3SKees Cook {
671c97204baSJohn Johansen 	return single_open(file, aa_sfs_seq_show, inode->i_private);
672e74abcf3SKees Cook }
673e74abcf3SKees Cook 
674c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = {
675e74abcf3SKees Cook 	.owner		= THIS_MODULE,
676c97204baSJohn Johansen 	.open		= aa_sfs_seq_open,
677e74abcf3SKees Cook 	.read		= seq_read,
678e74abcf3SKees Cook 	.llseek		= seq_lseek,
679e74abcf3SKees Cook 	.release	= single_release,
680e74abcf3SKees Cook };
681e74abcf3SKees Cook 
68252b97de3SJohn Johansen /*
68352b97de3SJohn Johansen  * profile based file operations
68452b97de3SJohn Johansen  *     policy/profiles/XXXX/profiles/ *
68552b97de3SJohn Johansen  */
68652b97de3SJohn Johansen 
68752b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME)						      \
68852b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
68952b97de3SJohn Johansen {									      \
69052b97de3SJohn Johansen 	return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show);    \
69152b97de3SJohn Johansen }									      \
69252b97de3SJohn Johansen 									      \
69352b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = {	      \
69452b97de3SJohn Johansen 	.owner		= THIS_MODULE,					      \
69552b97de3SJohn Johansen 	.open		= seq_profile_ ##NAME ##_open,			      \
69652b97de3SJohn Johansen 	.read		= seq_read,					      \
69752b97de3SJohn Johansen 	.llseek		= seq_lseek,					      \
69852b97de3SJohn Johansen 	.release	= seq_profile_release,				      \
69952b97de3SJohn Johansen }									      \
70052b97de3SJohn Johansen 
70152b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file,
7020d259f04SJohn Johansen 			    int (*show)(struct seq_file *, void *))
7030d259f04SJohn Johansen {
7048399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
7058399588aSJohn Johansen 	int error = single_open(file, show, proxy);
70663e2b423SJohn Johansen 
7070d259f04SJohn Johansen 	if (error) {
7080d259f04SJohn Johansen 		file->private_data = NULL;
7098399588aSJohn Johansen 		aa_put_proxy(proxy);
7100d259f04SJohn Johansen 	}
7110d259f04SJohn Johansen 
7120d259f04SJohn Johansen 	return error;
7130d259f04SJohn Johansen }
7140d259f04SJohn Johansen 
71552b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file)
7160d259f04SJohn Johansen {
7170d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
7180d259f04SJohn Johansen 	if (seq)
7198399588aSJohn Johansen 		aa_put_proxy(seq->private);
7200d259f04SJohn Johansen 	return single_release(inode, file);
7210d259f04SJohn Johansen }
7220d259f04SJohn Johansen 
72352b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v)
7240d259f04SJohn Johansen {
7258399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
7268399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
7270d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
7280d259f04SJohn Johansen 	aa_put_profile(profile);
7290d259f04SJohn Johansen 
7300d259f04SJohn Johansen 	return 0;
7310d259f04SJohn Johansen }
7320d259f04SJohn Johansen 
73352b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v)
7340d259f04SJohn Johansen {
7358399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
7368399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
7370d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
7380d259f04SJohn Johansen 	aa_put_profile(profile);
7390d259f04SJohn Johansen 
7400d259f04SJohn Johansen 	return 0;
7410d259f04SJohn Johansen }
7420d259f04SJohn Johansen 
74352b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v)
744556d0be7SJohn Johansen {
7458399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
7468399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
747556d0be7SJohn Johansen 	if (profile->attach)
748556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
749556d0be7SJohn Johansen 	else if (profile->xmatch)
750556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
751556d0be7SJohn Johansen 	else
752556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
753556d0be7SJohn Johansen 	aa_put_profile(profile);
754556d0be7SJohn Johansen 
755556d0be7SJohn Johansen 	return 0;
756556d0be7SJohn Johansen }
757556d0be7SJohn Johansen 
75852b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v)
759f8eb8a13SJohn Johansen {
7608399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
7618399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
762f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
763f8eb8a13SJohn Johansen 
764f8eb8a13SJohn Johansen 	if (profile->hash) {
765f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
766f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
76747dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
768f8eb8a13SJohn Johansen 	}
7690b938a2eSJohn Johansen 	aa_put_profile(profile);
770f8eb8a13SJohn Johansen 
771f8eb8a13SJohn Johansen 	return 0;
772f8eb8a13SJohn Johansen }
773f8eb8a13SJohn Johansen 
77452b97de3SJohn Johansen SEQ_PROFILE_FOPS(name);
77552b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode);
77652b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach);
77752b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash);
778f8eb8a13SJohn Johansen 
77964c86970SJohn Johansen /*
78064c86970SJohn Johansen  * namespace based files
78164c86970SJohn Johansen  *     several root files and
78264c86970SJohn Johansen  *     policy/ *
78364c86970SJohn Johansen  */
784f8eb8a13SJohn Johansen 
78564c86970SJohn Johansen #define SEQ_NS_FOPS(NAME)						      \
78664c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file)     \
78764c86970SJohn Johansen {									      \
78864c86970SJohn Johansen 	return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private);   \
78964c86970SJohn Johansen }									      \
79064c86970SJohn Johansen 									      \
79164c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = {	      \
79264c86970SJohn Johansen 	.owner		= THIS_MODULE,					      \
79364c86970SJohn Johansen 	.open		= seq_ns_ ##NAME ##_open,			      \
79464c86970SJohn Johansen 	.read		= seq_read,					      \
79564c86970SJohn Johansen 	.llseek		= seq_lseek,					      \
79664c86970SJohn Johansen 	.release	= single_release,				      \
79764c86970SJohn Johansen }									      \
7983e3e5695SJohn Johansen 
79964c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v)
800a71ada30SJohn Johansen {
801a71ada30SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
802a71ada30SJohn Johansen 
803a71ada30SJohn Johansen 	seq_printf(seq, "%d\n", ns->level);
804a71ada30SJohn Johansen 
805a71ada30SJohn Johansen 	return 0;
806a71ada30SJohn Johansen }
807a71ada30SJohn Johansen 
80864c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v)
8093e3e5695SJohn Johansen {
8103e3e5695SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
8113e3e5695SJohn Johansen 
8123e3e5695SJohn Johansen 	seq_printf(seq, "%s\n", ns->base.name);
8133e3e5695SJohn Johansen 
8143e3e5695SJohn Johansen 	return 0;
8153e3e5695SJohn Johansen }
8163e3e5695SJohn Johansen 
81764c86970SJohn Johansen SEQ_NS_FOPS(level);
81864c86970SJohn Johansen SEQ_NS_FOPS(name);
8193e3e5695SJohn Johansen 
8205d5182caSJohn Johansen 
8215d5182caSJohn Johansen /* policy/raw_data/ * file ops */
8225d5182caSJohn Johansen 
8235d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME)						      \
8245d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
8255d5182caSJohn Johansen {									      \
8265d5182caSJohn Johansen 	return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show);    \
8275d5182caSJohn Johansen }									      \
8285d5182caSJohn Johansen 									      \
8295d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = {	      \
8305d5182caSJohn Johansen 	.owner		= THIS_MODULE,					      \
8315d5182caSJohn Johansen 	.open		= seq_rawdata_ ##NAME ##_open,			      \
8325d5182caSJohn Johansen 	.read		= seq_read,					      \
8335d5182caSJohn Johansen 	.llseek		= seq_lseek,					      \
8345d5182caSJohn Johansen 	.release	= seq_rawdata_release,				      \
8355d5182caSJohn Johansen }									      \
8365d5182caSJohn Johansen 
8375d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file,
8385d5182caSJohn Johansen 			    int (*show)(struct seq_file *, void *))
8395ac8c355SJohn Johansen {
8405d5182caSJohn Johansen 	struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
8415d5182caSJohn Johansen 	int error;
8425d5182caSJohn Johansen 
8435d5182caSJohn Johansen 	if (!data)
8445d5182caSJohn Johansen 		/* lost race this ent is being reaped */
8455d5182caSJohn Johansen 		return -ENOENT;
8465d5182caSJohn Johansen 
8475d5182caSJohn Johansen 	error = single_open(file, show, data);
8485d5182caSJohn Johansen 	if (error) {
8495d5182caSJohn Johansen 		AA_BUG(file->private_data &&
8505d5182caSJohn Johansen 		       ((struct seq_file *)file->private_data)->private);
8515d5182caSJohn Johansen 		aa_put_loaddata(data);
8525d5182caSJohn Johansen 	}
8535d5182caSJohn Johansen 
8545d5182caSJohn Johansen 	return error;
8555d5182caSJohn Johansen }
8565d5182caSJohn Johansen 
8575d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file)
8585d5182caSJohn Johansen {
8595d5182caSJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
8605d5182caSJohn Johansen 
8615d5182caSJohn Johansen 	if (seq)
8625d5182caSJohn Johansen 		aa_put_loaddata(seq->private);
8635d5182caSJohn Johansen 
8645d5182caSJohn Johansen 	return single_release(inode, file);
8655d5182caSJohn Johansen }
8665d5182caSJohn Johansen 
8675d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
8685d5182caSJohn Johansen {
8695d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
8705d5182caSJohn Johansen 
8715d5182caSJohn Johansen 	seq_printf(seq, "v%d\n", data->abi);
8725ac8c355SJohn Johansen 
8735ac8c355SJohn Johansen 	return 0;
8745ac8c355SJohn Johansen }
8755ac8c355SJohn Johansen 
8765d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
8775ac8c355SJohn Johansen {
8785d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
8795ac8c355SJohn Johansen 
8805d5182caSJohn Johansen 	seq_printf(seq, "%ld\n", data->revision);
8815ac8c355SJohn Johansen 
8825ac8c355SJohn Johansen 	return 0;
8835ac8c355SJohn Johansen }
8845ac8c355SJohn Johansen 
8855d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
8865ac8c355SJohn Johansen {
8875d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
8885ac8c355SJohn Johansen 	unsigned int i, size = aa_hash_size();
8895ac8c355SJohn Johansen 
8905d5182caSJohn Johansen 	if (data->hash) {
8915ac8c355SJohn Johansen 		for (i = 0; i < size; i++)
8925d5182caSJohn Johansen 			seq_printf(seq, "%.2x", data->hash[i]);
89347dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
8945ac8c355SJohn Johansen 	}
8955ac8c355SJohn Johansen 
8965ac8c355SJohn Johansen 	return 0;
8975ac8c355SJohn Johansen }
8985ac8c355SJohn Johansen 
8995d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi);
9005d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision);
9015d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash);
9025ac8c355SJohn Johansen 
9035ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
9045ac8c355SJohn Johansen 			    loff_t *ppos)
9055ac8c355SJohn Johansen {
9065ac8c355SJohn Johansen 	struct aa_loaddata *rawdata = file->private_data;
9075ac8c355SJohn Johansen 
9085ac8c355SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, rawdata->data,
9095ac8c355SJohn Johansen 				       rawdata->size);
9105ac8c355SJohn Johansen }
9115ac8c355SJohn Johansen 
9125d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file)
9135ac8c355SJohn Johansen {
9145d5182caSJohn Johansen 	aa_put_loaddata(file->private_data);
9155ac8c355SJohn Johansen 
9165ac8c355SJohn Johansen 	return 0;
9175ac8c355SJohn Johansen }
9185ac8c355SJohn Johansen 
9195d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file)
9205d5182caSJohn Johansen {
9215d5182caSJohn Johansen 	if (!policy_view_capable(NULL))
9225d5182caSJohn Johansen 		return -EACCES;
9235d5182caSJohn Johansen 	file->private_data = __aa_get_loaddata(inode->i_private);
9245d5182caSJohn Johansen 	if (!file->private_data)
9255d5182caSJohn Johansen 		/* lost race: this entry is being reaped */
9265d5182caSJohn Johansen 		return -ENOENT;
9275d5182caSJohn Johansen 
9285d5182caSJohn Johansen 	return 0;
9295d5182caSJohn Johansen }
9305d5182caSJohn Johansen 
9315d5182caSJohn Johansen static const struct file_operations rawdata_fops = {
9325ac8c355SJohn Johansen 	.open = rawdata_open,
9335ac8c355SJohn Johansen 	.read = rawdata_read,
9345ac8c355SJohn Johansen 	.llseek = generic_file_llseek,
9355ac8c355SJohn Johansen 	.release = rawdata_release,
9365ac8c355SJohn Johansen };
9375ac8c355SJohn Johansen 
9385d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata)
9395d5182caSJohn Johansen {
9405d5182caSJohn Johansen 	int i;
9415d5182caSJohn Johansen 
9425d5182caSJohn Johansen 	for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
9435d5182caSJohn Johansen 		if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
9445d5182caSJohn Johansen 			/* no refcounts on i_private */
945c961ee5fSJohn Johansen 			aafs_remove(rawdata->dents[i]);
9465d5182caSJohn Johansen 			rawdata->dents[i] = NULL;
9475d5182caSJohn Johansen 		}
9485d5182caSJohn Johansen 	}
9495d5182caSJohn Johansen }
9505d5182caSJohn Johansen 
9515d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
9525d5182caSJohn Johansen {
9535d5182caSJohn Johansen 	AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
9545d5182caSJohn Johansen 
9555d5182caSJohn Johansen 	if (rawdata->ns) {
9565d5182caSJohn Johansen 		remove_rawdata_dents(rawdata);
9575d5182caSJohn Johansen 		list_del_init(&rawdata->list);
9585d5182caSJohn Johansen 		aa_put_ns(rawdata->ns);
9595d5182caSJohn Johansen 		rawdata->ns = NULL;
9605d5182caSJohn Johansen 	}
9615d5182caSJohn Johansen }
9625d5182caSJohn Johansen 
9635d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
9645d5182caSJohn Johansen {
9655d5182caSJohn Johansen 	struct dentry *dent, *dir;
9665d5182caSJohn Johansen 
9675d5182caSJohn Johansen 	AA_BUG(!ns);
9685d5182caSJohn Johansen 	AA_BUG(!rawdata);
9695d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
9705d5182caSJohn Johansen 	AA_BUG(!ns_subdata_dir(ns));
9715d5182caSJohn Johansen 
9725d5182caSJohn Johansen 	/*
9735d5182caSJohn Johansen 	 * just use ns revision dir was originally created at. This is
9745d5182caSJohn Johansen 	 * under ns->lock and if load is successful revision will be
9755d5182caSJohn Johansen 	 * bumped and is guaranteed to be unique
9765d5182caSJohn Johansen 	 */
9775d5182caSJohn Johansen 	rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
9785d5182caSJohn Johansen 	if (!rawdata->name)
9795d5182caSJohn Johansen 		return -ENOMEM;
9805d5182caSJohn Johansen 
981c961ee5fSJohn Johansen 	dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
9825d5182caSJohn Johansen 	if (IS_ERR(dir))
9835d5182caSJohn Johansen 		/* ->name freed when rawdata freed */
9845d5182caSJohn Johansen 		return PTR_ERR(dir);
9855d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DIR] = dir;
9865d5182caSJohn Johansen 
987c961ee5fSJohn Johansen 	dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
9885d5182caSJohn Johansen 				      &seq_rawdata_abi_fops);
9895d5182caSJohn Johansen 	if (IS_ERR(dent))
9905d5182caSJohn Johansen 		goto fail;
9915d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_ABI] = dent;
9925d5182caSJohn Johansen 
993c961ee5fSJohn Johansen 	dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
9945d5182caSJohn Johansen 				      &seq_rawdata_revision_fops);
9955d5182caSJohn Johansen 	if (IS_ERR(dent))
9965d5182caSJohn Johansen 		goto fail;
9975d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
9985d5182caSJohn Johansen 
9995d5182caSJohn Johansen 	if (aa_g_hash_policy) {
1000c961ee5fSJohn Johansen 		dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
10015d5182caSJohn Johansen 					      rawdata, &seq_rawdata_hash_fops);
10025d5182caSJohn Johansen 		if (IS_ERR(dent))
10035d5182caSJohn Johansen 			goto fail;
10045d5182caSJohn Johansen 		rawdata->dents[AAFS_LOADDATA_HASH] = dent;
10055d5182caSJohn Johansen 	}
10065d5182caSJohn Johansen 
1007c961ee5fSJohn Johansen 	dent = aafs_create_file("raw_data", S_IFREG | 0444,
10085d5182caSJohn Johansen 				      dir, rawdata, &rawdata_fops);
10095d5182caSJohn Johansen 	if (IS_ERR(dent))
10105d5182caSJohn Johansen 		goto fail;
10115d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DATA] = dent;
10125d5182caSJohn Johansen 	d_inode(dent)->i_size = rawdata->size;
10135d5182caSJohn Johansen 
10145d5182caSJohn Johansen 	rawdata->ns = aa_get_ns(ns);
10155d5182caSJohn Johansen 	list_add(&rawdata->list, &ns->rawdata_list);
10165d5182caSJohn Johansen 	/* no refcount on inode rawdata */
10175d5182caSJohn Johansen 
10185d5182caSJohn Johansen 	return 0;
10195d5182caSJohn Johansen 
10205d5182caSJohn Johansen fail:
10215d5182caSJohn Johansen 	remove_rawdata_dents(rawdata);
10225d5182caSJohn Johansen 
10235d5182caSJohn Johansen 	return PTR_ERR(dent);
10245d5182caSJohn Johansen }
10255d5182caSJohn Johansen 
10260d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
1027c97204baSJohn Johansen 
1028c97204baSJohn Johansen /**
1029c97204baSJohn Johansen  *
1030c97204baSJohn Johansen  * Requires: @profile->ns->lock held
1031c97204baSJohn Johansen  */
1032c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile)
10330d259f04SJohn Johansen {
10340d259f04SJohn Johansen 	struct aa_profile *child;
10350d259f04SJohn Johansen 	int i;
10360d259f04SJohn Johansen 
10370d259f04SJohn Johansen 	if (!profile)
10380d259f04SJohn Johansen 		return;
10390d259f04SJohn Johansen 
10400d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
1041c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
10420d259f04SJohn Johansen 
10430d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
10448399588aSJohn Johansen 		struct aa_proxy *proxy;
10450d259f04SJohn Johansen 		if (!profile->dents[i])
10460d259f04SJohn Johansen 			continue;
10470d259f04SJohn Johansen 
10488399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
1049c961ee5fSJohn Johansen 		aafs_remove(profile->dents[i]);
10508399588aSJohn Johansen 		aa_put_proxy(proxy);
10510d259f04SJohn Johansen 		profile->dents[i] = NULL;
10520d259f04SJohn Johansen 	}
10530d259f04SJohn Johansen }
10540d259f04SJohn Johansen 
1055c97204baSJohn Johansen /**
1056c97204baSJohn Johansen  *
1057c97204baSJohn Johansen  * Requires: @old->ns->lock held
1058c97204baSJohn Johansen  */
1059c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old,
10600d259f04SJohn Johansen 				  struct aa_profile *new)
10610d259f04SJohn Johansen {
10620d259f04SJohn Johansen 	int i;
10630d259f04SJohn Johansen 
10640d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
10650d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
1066d671e890SJohn Johansen 		if (new->dents[i])
1067078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
10680d259f04SJohn Johansen 		old->dents[i] = NULL;
10690d259f04SJohn Johansen 	}
10700d259f04SJohn Johansen }
10710d259f04SJohn Johansen 
10720d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
10730d259f04SJohn Johansen 					  struct aa_profile *profile,
10740d259f04SJohn Johansen 					  const struct file_operations *fops)
10750d259f04SJohn Johansen {
10768399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
10770d259f04SJohn Johansen 	struct dentry *dent;
10780d259f04SJohn Johansen 
1079c961ee5fSJohn Johansen 	dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
10800d259f04SJohn Johansen 	if (IS_ERR(dent))
10818399588aSJohn Johansen 		aa_put_proxy(proxy);
10820d259f04SJohn Johansen 
10830d259f04SJohn Johansen 	return dent;
10840d259f04SJohn Johansen }
10850d259f04SJohn Johansen 
10865d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile)
10875d5182caSJohn Johansen {
10885d5182caSJohn Johansen 	int depth = 0;
10895d5182caSJohn Johansen 
10905d5182caSJohn Johansen 	rcu_read_lock();
10915d5182caSJohn Johansen 	for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
10925d5182caSJohn Johansen 		depth++;
10935d5182caSJohn Johansen 	rcu_read_unlock();
10945d5182caSJohn Johansen 
10955d5182caSJohn Johansen 	return depth;
10965d5182caSJohn Johansen }
10975d5182caSJohn Johansen 
10985d5182caSJohn Johansen static int gen_symlink_name(char *buffer, size_t bsize, int depth,
10995d5182caSJohn Johansen 			    const char *dirname, const char *fname)
11005d5182caSJohn Johansen {
11015d5182caSJohn Johansen 	int error;
11025d5182caSJohn Johansen 
11035d5182caSJohn Johansen 	for (; depth > 0; depth--) {
11045d5182caSJohn Johansen 		if (bsize < 7)
11055d5182caSJohn Johansen 			return -ENAMETOOLONG;
11065d5182caSJohn Johansen 		strcpy(buffer, "../../");
11075d5182caSJohn Johansen 		buffer += 6;
11085d5182caSJohn Johansen 		bsize -= 6;
11095d5182caSJohn Johansen 	}
11105d5182caSJohn Johansen 
11115d5182caSJohn Johansen 	error = snprintf(buffer, bsize, "raw_data/%s/%s", dirname, fname);
11125d5182caSJohn Johansen 	if (error >= bsize || error < 0)
11135d5182caSJohn Johansen 		return -ENAMETOOLONG;
11145d5182caSJohn Johansen 
11155d5182caSJohn Johansen 	return 0;
11165d5182caSJohn Johansen }
11175d5182caSJohn Johansen 
11185d5182caSJohn Johansen /*
11195d5182caSJohn Johansen  * Requires: @profile->ns->lock held
11205d5182caSJohn Johansen  */
1121c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
11220d259f04SJohn Johansen {
11230d259f04SJohn Johansen 	struct aa_profile *child;
11240d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
11250d259f04SJohn Johansen 	int error;
11260d259f04SJohn Johansen 
11270d259f04SJohn Johansen 	if (!parent) {
11280d259f04SJohn Johansen 		struct aa_profile *p;
11290d259f04SJohn Johansen 		p = aa_deref_parent(profile);
11300d259f04SJohn Johansen 		dent = prof_dir(p);
11310d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
1132c961ee5fSJohn Johansen 		dent = aafs_create_dir("profiles", dent);
11330d259f04SJohn Johansen 		if (IS_ERR(dent))
11340d259f04SJohn Johansen 			goto fail;
11350d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
11360d259f04SJohn Johansen 	}
11370d259f04SJohn Johansen 
11380d259f04SJohn Johansen 	if (!profile->dirname) {
11390d259f04SJohn Johansen 		int len, id_len;
11400d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
11410d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
11420d259f04SJohn Johansen 
11430d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
1144ffac1de6SDan Carpenter 		if (!profile->dirname) {
1145ffac1de6SDan Carpenter 			error = -ENOMEM;
1146ffac1de6SDan Carpenter 			goto fail2;
1147ffac1de6SDan Carpenter 		}
11480d259f04SJohn Johansen 
11490d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
11500d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
11510d259f04SJohn Johansen 	}
11520d259f04SJohn Johansen 
1153c961ee5fSJohn Johansen 	dent = aafs_create_dir(profile->dirname, parent);
11540d259f04SJohn Johansen 	if (IS_ERR(dent))
11550d259f04SJohn Johansen 		goto fail;
11560d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
11570d259f04SJohn Johansen 
115852b97de3SJohn Johansen 	dent = create_profile_file(dir, "name", profile,
115952b97de3SJohn Johansen 				   &seq_profile_name_fops);
11600d259f04SJohn Johansen 	if (IS_ERR(dent))
11610d259f04SJohn Johansen 		goto fail;
11620d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
11630d259f04SJohn Johansen 
116452b97de3SJohn Johansen 	dent = create_profile_file(dir, "mode", profile,
116552b97de3SJohn Johansen 				   &seq_profile_mode_fops);
11660d259f04SJohn Johansen 	if (IS_ERR(dent))
11670d259f04SJohn Johansen 		goto fail;
11680d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
11690d259f04SJohn Johansen 
1170556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
117152b97de3SJohn Johansen 				   &seq_profile_attach_fops);
1172556d0be7SJohn Johansen 	if (IS_ERR(dent))
1173556d0be7SJohn Johansen 		goto fail;
1174556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
1175556d0be7SJohn Johansen 
1176f8eb8a13SJohn Johansen 	if (profile->hash) {
1177f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
117852b97de3SJohn Johansen 					   &seq_profile_hash_fops);
1179f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
1180f8eb8a13SJohn Johansen 			goto fail;
1181f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
1182f8eb8a13SJohn Johansen 	}
1183f8eb8a13SJohn Johansen 
11845ac8c355SJohn Johansen 	if (profile->rawdata) {
11855d5182caSJohn Johansen 		char target[64];
11865d5182caSJohn Johansen 		int depth = profile_depth(profile);
11875d5182caSJohn Johansen 
11885d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
11895d5182caSJohn Johansen 					 profile->rawdata->name, "sha1");
11905d5182caSJohn Johansen 		if (error < 0)
11915d5182caSJohn Johansen 			goto fail2;
1192c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_sha1", dir, target, NULL);
11935ac8c355SJohn Johansen 		if (IS_ERR(dent))
11945ac8c355SJohn Johansen 			goto fail;
11955ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
11965ac8c355SJohn Johansen 
11975d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
11985d5182caSJohn Johansen 					 profile->rawdata->name, "abi");
11995d5182caSJohn Johansen 		if (error < 0)
12005d5182caSJohn Johansen 			goto fail2;
1201c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_abi", dir, target, NULL);
12025ac8c355SJohn Johansen 		if (IS_ERR(dent))
12035ac8c355SJohn Johansen 			goto fail;
12045ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
12055ac8c355SJohn Johansen 
12065d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
12075d5182caSJohn Johansen 					 profile->rawdata->name, "raw_data");
12085d5182caSJohn Johansen 		if (error < 0)
12095d5182caSJohn Johansen 			goto fail2;
1210c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_data", dir, target, NULL);
12115ac8c355SJohn Johansen 		if (IS_ERR(dent))
12125ac8c355SJohn Johansen 			goto fail;
12135ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
12145ac8c355SJohn Johansen 	}
12155ac8c355SJohn Johansen 
12160d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
1217c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, prof_child_dir(profile));
12180d259f04SJohn Johansen 		if (error)
12190d259f04SJohn Johansen 			goto fail2;
12200d259f04SJohn Johansen 	}
12210d259f04SJohn Johansen 
12220d259f04SJohn Johansen 	return 0;
12230d259f04SJohn Johansen 
12240d259f04SJohn Johansen fail:
12250d259f04SJohn Johansen 	error = PTR_ERR(dent);
12260d259f04SJohn Johansen 
12270d259f04SJohn Johansen fail2:
1228c97204baSJohn Johansen 	__aafs_profile_rmdir(profile);
12290d259f04SJohn Johansen 
12300d259f04SJohn Johansen 	return error;
12310d259f04SJohn Johansen }
12320d259f04SJohn Johansen 
12335d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
12345d5182caSJohn Johansen {
12355d5182caSJohn Johansen 	struct aa_loaddata *ent, *tmp;
12365d5182caSJohn Johansen 
12375d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
12385d5182caSJohn Johansen 
12395d5182caSJohn Johansen 	list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
12405d5182caSJohn Johansen 		__aa_fs_remove_rawdata(ent);
12415d5182caSJohn Johansen }
12425d5182caSJohn Johansen 
1243c97204baSJohn Johansen /**
1244c97204baSJohn Johansen  *
1245c97204baSJohn Johansen  * Requires: @ns->lock held
1246c97204baSJohn Johansen  */
1247c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns)
12480d259f04SJohn Johansen {
124998849dffSJohn Johansen 	struct aa_ns *sub;
12500d259f04SJohn Johansen 	struct aa_profile *child;
12510d259f04SJohn Johansen 	int i;
12520d259f04SJohn Johansen 
12530d259f04SJohn Johansen 	if (!ns)
12540d259f04SJohn Johansen 		return;
12550d259f04SJohn Johansen 
12560d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
1257c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
12580d259f04SJohn Johansen 
12590d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
12600d259f04SJohn Johansen 		mutex_lock(&sub->lock);
1261c97204baSJohn Johansen 		__aafs_ns_rmdir(sub);
12620d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
12630d259f04SJohn Johansen 	}
12640d259f04SJohn Johansen 
12655d5182caSJohn Johansen 	__aa_fs_list_remove_rawdata(ns);
12665d5182caSJohn Johansen 
1267b7fd2c03SJohn Johansen 	if (ns_subns_dir(ns)) {
1268b7fd2c03SJohn Johansen 		sub = d_inode(ns_subns_dir(ns))->i_private;
1269b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1270b7fd2c03SJohn Johansen 	}
1271b7fd2c03SJohn Johansen 	if (ns_subload(ns)) {
1272b7fd2c03SJohn Johansen 		sub = d_inode(ns_subload(ns))->i_private;
1273b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1274b7fd2c03SJohn Johansen 	}
1275b7fd2c03SJohn Johansen 	if (ns_subreplace(ns)) {
1276b7fd2c03SJohn Johansen 		sub = d_inode(ns_subreplace(ns))->i_private;
1277b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1278b7fd2c03SJohn Johansen 	}
1279b7fd2c03SJohn Johansen 	if (ns_subremove(ns)) {
1280b7fd2c03SJohn Johansen 		sub = d_inode(ns_subremove(ns))->i_private;
1281b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1282b7fd2c03SJohn Johansen 	}
1283b7fd2c03SJohn Johansen 
12840d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
1285c961ee5fSJohn Johansen 		aafs_remove(ns->dents[i]);
12860d259f04SJohn Johansen 		ns->dents[i] = NULL;
12870d259f04SJohn Johansen 	}
12880d259f04SJohn Johansen }
12890d259f04SJohn Johansen 
1290b7fd2c03SJohn Johansen /* assumes cleanup in caller */
1291c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
1292b7fd2c03SJohn Johansen {
1293b7fd2c03SJohn Johansen 	struct dentry *dent;
1294b7fd2c03SJohn Johansen 
1295b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1296b7fd2c03SJohn Johansen 	AA_BUG(!dir);
1297b7fd2c03SJohn Johansen 
1298c961ee5fSJohn Johansen 	dent = aafs_create_dir("profiles", dir);
1299b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1300b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1301b7fd2c03SJohn Johansen 	ns_subprofs_dir(ns) = dent;
1302b7fd2c03SJohn Johansen 
1303c961ee5fSJohn Johansen 	dent = aafs_create_dir("raw_data", dir);
1304b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1305b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1306b7fd2c03SJohn Johansen 	ns_subdata_dir(ns) = dent;
1307b7fd2c03SJohn Johansen 
1308c961ee5fSJohn Johansen 	dent = aafs_create_file(".load", 0640, dir, ns,
1309b7fd2c03SJohn Johansen 				      &aa_fs_profile_load);
1310b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1311b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1312b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1313b7fd2c03SJohn Johansen 	ns_subload(ns) = dent;
1314b7fd2c03SJohn Johansen 
1315c961ee5fSJohn Johansen 	dent = aafs_create_file(".replace", 0640, dir, ns,
1316b7fd2c03SJohn Johansen 				      &aa_fs_profile_replace);
1317b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1318b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1319b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1320b7fd2c03SJohn Johansen 	ns_subreplace(ns) = dent;
1321b7fd2c03SJohn Johansen 
1322c961ee5fSJohn Johansen 	dent = aafs_create_file(".remove", 0640, dir, ns,
1323b7fd2c03SJohn Johansen 				      &aa_fs_profile_remove);
1324b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1325b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1326b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1327b7fd2c03SJohn Johansen 	ns_subremove(ns) = dent;
1328b7fd2c03SJohn Johansen 
1329c961ee5fSJohn Johansen 	dent = aafs_create_dir("namespaces", dir);
1330b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1331b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1332b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1333b7fd2c03SJohn Johansen 	ns_subns_dir(ns) = dent;
1334b7fd2c03SJohn Johansen 
1335b7fd2c03SJohn Johansen 	return 0;
1336b7fd2c03SJohn Johansen }
1337b7fd2c03SJohn Johansen 
1338c97204baSJohn Johansen /*
1339c97204baSJohn Johansen  * Requires: @ns->lock held
1340c97204baSJohn Johansen  */
134198407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
134298407f0aSJohn Johansen 		    struct dentry *dent)
13430d259f04SJohn Johansen {
134498849dffSJohn Johansen 	struct aa_ns *sub;
13450d259f04SJohn Johansen 	struct aa_profile *child;
134698407f0aSJohn Johansen 	struct dentry *dir;
13470d259f04SJohn Johansen 	int error;
13480d259f04SJohn Johansen 
1349b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1350b7fd2c03SJohn Johansen 	AA_BUG(!parent);
1351b7fd2c03SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
1352b7fd2c03SJohn Johansen 
13530d259f04SJohn Johansen 	if (!name)
13540d259f04SJohn Johansen 		name = ns->base.name;
13550d259f04SJohn Johansen 
1356c961ee5fSJohn Johansen 	if (!dent) {
1357b7fd2c03SJohn Johansen 		/* create ns dir if it doesn't already exist */
1358c961ee5fSJohn Johansen 		dent = aafs_create_dir(name, parent);
13590d259f04SJohn Johansen 		if (IS_ERR(dent))
13600d259f04SJohn Johansen 			goto fail;
1361c961ee5fSJohn Johansen 	} else
1362c961ee5fSJohn Johansen 		dget(dent);
13630d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
1364c97204baSJohn Johansen 	error = __aafs_ns_mkdir_entries(ns, dir);
1365b7fd2c03SJohn Johansen 	if (error)
1366b7fd2c03SJohn Johansen 		goto fail2;
13670d259f04SJohn Johansen 
1368b7fd2c03SJohn Johansen 	/* profiles */
13690d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
1370c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
13710d259f04SJohn Johansen 		if (error)
13720d259f04SJohn Johansen 			goto fail2;
13730d259f04SJohn Johansen 	}
13740d259f04SJohn Johansen 
1375b7fd2c03SJohn Johansen 	/* subnamespaces */
13760d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
13770d259f04SJohn Johansen 		mutex_lock(&sub->lock);
137898407f0aSJohn Johansen 		error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
13790d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
13800d259f04SJohn Johansen 		if (error)
13810d259f04SJohn Johansen 			goto fail2;
13820d259f04SJohn Johansen 	}
13830d259f04SJohn Johansen 
13840d259f04SJohn Johansen 	return 0;
13850d259f04SJohn Johansen 
13860d259f04SJohn Johansen fail:
13870d259f04SJohn Johansen 	error = PTR_ERR(dent);
13880d259f04SJohn Johansen 
13890d259f04SJohn Johansen fail2:
1390c97204baSJohn Johansen 	__aafs_ns_rmdir(ns);
13910d259f04SJohn Johansen 
13920d259f04SJohn Johansen 	return error;
13930d259f04SJohn Johansen }
13940d259f04SJohn Johansen 
13950d259f04SJohn Johansen 
139629b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
139729b3822fSJohn Johansen 
139829b3822fSJohn Johansen /**
139998849dffSJohn Johansen  * __next_ns - find the next namespace to list
140029b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
140129b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
140229b3822fSJohn Johansen  *
140329b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
140429b3822fSJohn Johansen  * while switching current namespace.
140529b3822fSJohn Johansen  *
140629b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
140729b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
140829b3822fSJohn Johansen  * NOTE: will not unlock root->lock
140929b3822fSJohn Johansen  */
141098849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
141129b3822fSJohn Johansen {
141298849dffSJohn Johansen 	struct aa_ns *parent, *next;
141329b3822fSJohn Johansen 
141429b3822fSJohn Johansen 	/* is next namespace a child */
141529b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
141629b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
141729b3822fSJohn Johansen 		mutex_lock(&next->lock);
141829b3822fSJohn Johansen 		return next;
141929b3822fSJohn Johansen 	}
142029b3822fSJohn Johansen 
142129b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
142229b3822fSJohn Johansen 	parent = ns->parent;
1423ed2c7da3SJohn Johansen 	while (ns != root) {
142429b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
142538dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
142629b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
142729b3822fSJohn Johansen 			mutex_lock(&next->lock);
142829b3822fSJohn Johansen 			return next;
142929b3822fSJohn Johansen 		}
143029b3822fSJohn Johansen 		ns = parent;
143129b3822fSJohn Johansen 		parent = parent->parent;
143229b3822fSJohn Johansen 	}
143329b3822fSJohn Johansen 
143429b3822fSJohn Johansen 	return NULL;
143529b3822fSJohn Johansen }
143629b3822fSJohn Johansen 
143729b3822fSJohn Johansen /**
143829b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
143929b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
144029b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
144129b3822fSJohn Johansen  *
144229b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
144329b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
144429b3822fSJohn Johansen  */
144598849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
144698849dffSJohn Johansen 					  struct aa_ns *ns)
144729b3822fSJohn Johansen {
144898849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
144929b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
145029b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
145129b3822fSJohn Johansen 						struct aa_profile, base.list);
145229b3822fSJohn Johansen 	}
145329b3822fSJohn Johansen 	return NULL;
145429b3822fSJohn Johansen }
145529b3822fSJohn Johansen 
145629b3822fSJohn Johansen /**
145729b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
145829b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
145929b3822fSJohn Johansen  *
146029b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
146129b3822fSJohn Johansen  *
146229b3822fSJohn Johansen  * Returns: next profile or NULL if done
146329b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
146429b3822fSJohn Johansen  */
146529b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
146629b3822fSJohn Johansen {
146729b3822fSJohn Johansen 	struct aa_profile *parent;
146898849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
146929b3822fSJohn Johansen 
147029b3822fSJohn Johansen 	/* is next profile a child */
147129b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
147229b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
147329b3822fSJohn Johansen 					base.list);
147429b3822fSJohn Johansen 
147529b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
147629b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
147729b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
147829b3822fSJohn Johansen 	while (parent) {
147938dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
148029b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
148129b3822fSJohn Johansen 			return p;
148229b3822fSJohn Johansen 		p = parent;
148329b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
148429b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
148529b3822fSJohn Johansen 	}
148629b3822fSJohn Johansen 
148729b3822fSJohn Johansen 	/* is next another profile in the namespace */
148838dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
148929b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
149029b3822fSJohn Johansen 		return p;
149129b3822fSJohn Johansen 
149229b3822fSJohn Johansen 	return NULL;
149329b3822fSJohn Johansen }
149429b3822fSJohn Johansen 
149529b3822fSJohn Johansen /**
149629b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
149729b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
149829b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
149929b3822fSJohn Johansen  *
150029b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
150129b3822fSJohn Johansen  */
150298849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
150329b3822fSJohn Johansen 				       struct aa_profile *profile)
150429b3822fSJohn Johansen {
150529b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
150629b3822fSJohn Johansen 	if (next)
150729b3822fSJohn Johansen 		return next;
150829b3822fSJohn Johansen 
150929b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
151098849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
151129b3822fSJohn Johansen }
151229b3822fSJohn Johansen 
151329b3822fSJohn Johansen /**
151429b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
151529b3822fSJohn Johansen  * @f: seq_file to fill
151629b3822fSJohn Johansen  * @pos: current position
151729b3822fSJohn Johansen  *
151829b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
151929b3822fSJohn Johansen  *
152029b3822fSJohn Johansen  * acquires first ns->lock
152129b3822fSJohn Johansen  */
152229b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
152329b3822fSJohn Johansen {
152429b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
152598849dffSJohn Johansen 	struct aa_ns *root = aa_current_profile()->ns;
152629b3822fSJohn Johansen 	loff_t l = *pos;
152798849dffSJohn Johansen 	f->private = aa_get_ns(root);
152829b3822fSJohn Johansen 
152929b3822fSJohn Johansen 
153029b3822fSJohn Johansen 	/* find the first profile */
153129b3822fSJohn Johansen 	mutex_lock(&root->lock);
153229b3822fSJohn Johansen 	profile = __first_profile(root, root);
153329b3822fSJohn Johansen 
153429b3822fSJohn Johansen 	/* skip to position */
153529b3822fSJohn Johansen 	for (; profile && l > 0; l--)
153629b3822fSJohn Johansen 		profile = next_profile(root, profile);
153729b3822fSJohn Johansen 
153829b3822fSJohn Johansen 	return profile;
153929b3822fSJohn Johansen }
154029b3822fSJohn Johansen 
154129b3822fSJohn Johansen /**
154229b3822fSJohn Johansen  * p_next - read the next profile entry
154329b3822fSJohn Johansen  * @f: seq_file to fill
154429b3822fSJohn Johansen  * @p: profile previously returned
154529b3822fSJohn Johansen  * @pos: current position
154629b3822fSJohn Johansen  *
154729b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
154829b3822fSJohn Johansen  *
154929b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
155029b3822fSJohn Johansen  */
155129b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
155229b3822fSJohn Johansen {
155329b3822fSJohn Johansen 	struct aa_profile *profile = p;
155498849dffSJohn Johansen 	struct aa_ns *ns = f->private;
155529b3822fSJohn Johansen 	(*pos)++;
155629b3822fSJohn Johansen 
155729b3822fSJohn Johansen 	return next_profile(ns, profile);
155829b3822fSJohn Johansen }
155929b3822fSJohn Johansen 
156029b3822fSJohn Johansen /**
156129b3822fSJohn Johansen  * p_stop - stop depth first traversal
156229b3822fSJohn Johansen  * @f: seq_file we are filling
156329b3822fSJohn Johansen  * @p: the last profile writen
156429b3822fSJohn Johansen  *
156529b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
156629b3822fSJohn Johansen  */
156729b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
156829b3822fSJohn Johansen {
156929b3822fSJohn Johansen 	struct aa_profile *profile = p;
157098849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
157129b3822fSJohn Johansen 
157229b3822fSJohn Johansen 	if (profile) {
157329b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
157429b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
157529b3822fSJohn Johansen 	}
157629b3822fSJohn Johansen 	mutex_unlock(&root->lock);
157798849dffSJohn Johansen 	aa_put_ns(root);
157829b3822fSJohn Johansen }
157929b3822fSJohn Johansen 
158029b3822fSJohn Johansen /**
158129b3822fSJohn Johansen  * seq_show_profile - show a profile entry
158229b3822fSJohn Johansen  * @f: seq_file to file
158329b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
158429b3822fSJohn Johansen  *
158529b3822fSJohn Johansen  * Returns: error on failure
158629b3822fSJohn Johansen  */
158729b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
158829b3822fSJohn Johansen {
158929b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
159098849dffSJohn Johansen 	struct aa_ns *root = f->private;
159129b3822fSJohn Johansen 
159229b3822fSJohn Johansen 	if (profile->ns != root)
159392b6d8efSJohn Johansen 		seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
159429b3822fSJohn Johansen 	seq_printf(f, "%s (%s)\n", profile->base.hname,
159529b3822fSJohn Johansen 		   aa_profile_mode_names[profile->mode]);
159629b3822fSJohn Johansen 
159729b3822fSJohn Johansen 	return 0;
159829b3822fSJohn Johansen }
159929b3822fSJohn Johansen 
1600c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = {
160129b3822fSJohn Johansen 	.start = p_start,
160229b3822fSJohn Johansen 	.next = p_next,
160329b3822fSJohn Johansen 	.stop = p_stop,
160429b3822fSJohn Johansen 	.show = seq_show_profile,
160529b3822fSJohn Johansen };
160629b3822fSJohn Johansen 
160729b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
160829b3822fSJohn Johansen {
16095ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
16105ac8c355SJohn Johansen 		return -EACCES;
16115ac8c355SJohn Johansen 
1612c97204baSJohn Johansen 	return seq_open(file, &aa_sfs_profiles_op);
161329b3822fSJohn Johansen }
161429b3822fSJohn Johansen 
161529b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
161629b3822fSJohn Johansen {
161729b3822fSJohn Johansen 	return seq_release(inode, file);
161829b3822fSJohn Johansen }
161929b3822fSJohn Johansen 
1620c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = {
162129b3822fSJohn Johansen 	.open = profiles_open,
162229b3822fSJohn Johansen 	.read = seq_read,
162329b3822fSJohn Johansen 	.llseek = seq_lseek,
162429b3822fSJohn Johansen 	.release = profiles_release,
162529b3822fSJohn Johansen };
162629b3822fSJohn Johansen 
162729b3822fSJohn Johansen 
16280d259f04SJohn Johansen /** Base file system setup **/
1629c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = {
1630c97204baSJohn Johansen 	AA_SFS_FILE_STRING("mask",
1631c97204baSJohn Johansen 			   "create read write exec append mmap_exec link lock"),
1632a9bf8e9fSKees Cook 	{ }
1633a9bf8e9fSKees Cook };
1634a9bf8e9fSKees Cook 
1635c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = {
1636c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hat",	1),
1637c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hatv",	1),
1638c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_onexec",	1),
1639c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_profile",	1),
1640c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
1641c97204baSJohn Johansen 	AA_SFS_FILE_STRING("version", "1.2"),
1642e74abcf3SKees Cook 	{ }
1643e74abcf3SKees Cook };
1644e74abcf3SKees Cook 
1645c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = {
1646c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("v5",	1),
1647474d6b75SJohn Johansen 	{ }
1648474d6b75SJohn Johansen };
1649474d6b75SJohn Johansen 
1650c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = {
1651c97204baSJohn Johansen 	AA_SFS_DIR("versions",			aa_sfs_entry_versions),
1652c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("set_load",		1),
16539d910a3bSJohn Johansen 	{ }
16549d910a3bSJohn Johansen };
16559d910a3bSJohn Johansen 
1656c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = {
1657c97204baSJohn Johansen 	AA_SFS_DIR("policy",			aa_sfs_entry_policy),
1658c97204baSJohn Johansen 	AA_SFS_DIR("domain",			aa_sfs_entry_domain),
1659c97204baSJohn Johansen 	AA_SFS_DIR("file",			aa_sfs_entry_file),
1660c97204baSJohn Johansen 	AA_SFS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
1661c97204baSJohn Johansen 	AA_SFS_DIR("rlimit",			aa_sfs_entry_rlimit),
1662c97204baSJohn Johansen 	AA_SFS_DIR("caps",			aa_sfs_entry_caps),
1663e74abcf3SKees Cook 	{ }
1664e74abcf3SKees Cook };
1665e74abcf3SKees Cook 
1666c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
1667c97204baSJohn Johansen 	AA_SFS_FILE_FOPS(".access", 0640, &aa_sfs_access),
1668c97204baSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_level", 0666, &seq_ns_level_fops),
1669c97204baSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_name", 0640, &seq_ns_name_fops),
1670c97204baSJohn Johansen 	AA_SFS_FILE_FOPS("profiles", 0440, &aa_sfs_profiles_fops),
1671c97204baSJohn Johansen 	AA_SFS_DIR("features", aa_sfs_entry_features),
16729acd494bSKees Cook 	{ }
16739acd494bSKees Cook };
167463e2b423SJohn Johansen 
1675c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry =
1676c97204baSJohn Johansen 	AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
16779acd494bSKees Cook 
16789acd494bSKees Cook /**
1679a481f4d9SJohn Johansen  * entry_create_file - create a file entry in the apparmor securityfs
1680c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
16819acd494bSKees Cook  * @parent: the parent dentry in the securityfs
16829acd494bSKees Cook  *
1683a481f4d9SJohn Johansen  * Use entry_remove_file to remove entries created with this fn.
16849acd494bSKees Cook  */
1685c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file,
16869acd494bSKees Cook 				    struct dentry *parent)
168763e2b423SJohn Johansen {
16889acd494bSKees Cook 	int error = 0;
168963e2b423SJohn Johansen 
16909acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
16919acd494bSKees Cook 						 S_IFREG | fs_file->mode,
16929acd494bSKees Cook 						 parent, fs_file,
16939acd494bSKees Cook 						 fs_file->file_ops);
16949acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
16959acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
16969acd494bSKees Cook 		fs_file->dentry = NULL;
169763e2b423SJohn Johansen 	}
16989acd494bSKees Cook 	return error;
169963e2b423SJohn Johansen }
170063e2b423SJohn Johansen 
1701c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
170263e2b423SJohn Johansen /**
1703a481f4d9SJohn Johansen  * entry_create_dir - recursively create a directory entry in the securityfs
1704c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
17059acd494bSKees Cook  * @parent: the parent dentry in the securityfs
170663e2b423SJohn Johansen  *
1707a481f4d9SJohn Johansen  * Use entry_remove_dir to remove entries created with this fn.
170863e2b423SJohn Johansen  */
1709c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
17109acd494bSKees Cook 				   struct dentry *parent)
171163e2b423SJohn Johansen {
1712c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
17130d259f04SJohn Johansen 	struct dentry *dir;
17140d259f04SJohn Johansen 	int error;
171563e2b423SJohn Johansen 
17160d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
17170d259f04SJohn Johansen 	if (IS_ERR(dir))
17180d259f04SJohn Johansen 		return PTR_ERR(dir);
17190d259f04SJohn Johansen 	fs_dir->dentry = dir;
172063e2b423SJohn Johansen 
17210d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
1722c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
1723a481f4d9SJohn Johansen 			error = entry_create_dir(fs_file, fs_dir->dentry);
17249acd494bSKees Cook 		else
1725a481f4d9SJohn Johansen 			error = entry_create_file(fs_file, fs_dir->dentry);
17269acd494bSKees Cook 		if (error)
17279acd494bSKees Cook 			goto failed;
17289acd494bSKees Cook 	}
17299acd494bSKees Cook 
17309acd494bSKees Cook 	return 0;
17319acd494bSKees Cook 
17329acd494bSKees Cook failed:
1733a481f4d9SJohn Johansen 	entry_remove_dir(fs_dir);
17340d259f04SJohn Johansen 
17359acd494bSKees Cook 	return error;
17369acd494bSKees Cook }
17379acd494bSKees Cook 
17389acd494bSKees Cook /**
1739c97204baSJohn Johansen  * entry_remove_file - drop a single file entry in the apparmor securityfs
1740c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
17419acd494bSKees Cook  */
1742c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
17439acd494bSKees Cook {
17449acd494bSKees Cook 	if (!fs_file->dentry)
17459acd494bSKees Cook 		return;
17469acd494bSKees Cook 
17479acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
17489acd494bSKees Cook 	fs_file->dentry = NULL;
17499acd494bSKees Cook }
17509acd494bSKees Cook 
17519acd494bSKees Cook /**
1752a481f4d9SJohn Johansen  * entry_remove_dir - recursively drop a directory entry from the securityfs
1753c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
17549acd494bSKees Cook  */
1755c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
17569acd494bSKees Cook {
1757c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
17589acd494bSKees Cook 
17590d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
1760c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
1761a481f4d9SJohn Johansen 			entry_remove_dir(fs_file);
17629acd494bSKees Cook 		else
1763c97204baSJohn Johansen 			entry_remove_file(fs_file);
17649acd494bSKees Cook 	}
17659acd494bSKees Cook 
1766c97204baSJohn Johansen 	entry_remove_file(fs_dir);
176763e2b423SJohn Johansen }
176863e2b423SJohn Johansen 
176963e2b423SJohn Johansen /**
177063e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
177163e2b423SJohn Johansen  *
177263e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
177363e2b423SJohn Johansen  */
177463e2b423SJohn Johansen void __init aa_destroy_aafs(void)
177563e2b423SJohn Johansen {
1776c97204baSJohn Johansen 	entry_remove_dir(&aa_sfs_entry);
177763e2b423SJohn Johansen }
177863e2b423SJohn Johansen 
1779a71ada30SJohn Johansen 
1780a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
1781a71ada30SJohn Johansen struct path aa_null;
1782a71ada30SJohn Johansen 
1783a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
1784a71ada30SJohn Johansen {
1785a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
1786a71ada30SJohn Johansen 	struct dentry *dentry;
1787a71ada30SJohn Johansen 	struct inode *inode;
1788a71ada30SJohn Johansen 	int count = 0;
1789a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
1790a71ada30SJohn Johansen 
1791a71ada30SJohn Johansen 	if (error)
1792a71ada30SJohn Johansen 		return error;
1793a71ada30SJohn Johansen 
1794a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
1795a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
1796a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
1797a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
1798a71ada30SJohn Johansen 		goto out;
1799a71ada30SJohn Johansen 	}
1800a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
1801a71ada30SJohn Johansen 	if (!inode) {
1802a71ada30SJohn Johansen 		error = -ENOMEM;
1803a71ada30SJohn Johansen 		goto out1;
1804a71ada30SJohn Johansen 	}
1805a71ada30SJohn Johansen 
1806a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
1807a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
180824d0d03cSDeepa Dinamani 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1809a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
1810a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
1811a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
1812a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
1813a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
1814a71ada30SJohn Johansen 
1815a71ada30SJohn Johansen 	error = 0;
1816a71ada30SJohn Johansen 
1817a71ada30SJohn Johansen out1:
1818a71ada30SJohn Johansen 	dput(dentry);
1819a71ada30SJohn Johansen out:
1820a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
1821a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
1822a71ada30SJohn Johansen 	return error;
1823a71ada30SJohn Johansen }
1824a71ada30SJohn Johansen 
1825a481f4d9SJohn Johansen 
1826a481f4d9SJohn Johansen 
1827a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry,
1828a481f4d9SJohn Johansen 				   struct inode *inode,
1829a481f4d9SJohn Johansen 				   struct delayed_call *done)
1830a481f4d9SJohn Johansen {
1831a481f4d9SJohn Johansen 	struct aa_ns *ns;
1832a481f4d9SJohn Johansen 	struct path path;
1833a481f4d9SJohn Johansen 
1834a481f4d9SJohn Johansen 	if (!dentry)
1835a481f4d9SJohn Johansen 		return ERR_PTR(-ECHILD);
1836a481f4d9SJohn Johansen 	ns = aa_get_current_ns();
1837a481f4d9SJohn Johansen 	path.mnt = mntget(aafs_mnt);
1838a481f4d9SJohn Johansen 	path.dentry = dget(ns_dir(ns));
1839a481f4d9SJohn Johansen 	nd_jump_link(&path);
1840a481f4d9SJohn Johansen 	aa_put_ns(ns);
1841a481f4d9SJohn Johansen 
1842a481f4d9SJohn Johansen 	return NULL;
1843a481f4d9SJohn Johansen }
1844a481f4d9SJohn Johansen 
1845a481f4d9SJohn Johansen static int ns_get_name(char *buf, size_t size, struct aa_ns *ns,
1846a481f4d9SJohn Johansen 		       struct inode *inode)
1847a481f4d9SJohn Johansen {
1848a481f4d9SJohn Johansen 	int res = snprintf(buf, size, "%s:[%lu]", AAFS_NAME, inode->i_ino);
1849a481f4d9SJohn Johansen 
1850a481f4d9SJohn Johansen 	if (res < 0 || res >= size)
1851a481f4d9SJohn Johansen 		res = -ENOENT;
1852a481f4d9SJohn Johansen 
1853a481f4d9SJohn Johansen 	return res;
1854a481f4d9SJohn Johansen }
1855a481f4d9SJohn Johansen 
1856a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer,
1857a481f4d9SJohn Johansen 			   int buflen)
1858a481f4d9SJohn Johansen {
1859a481f4d9SJohn Johansen 	struct aa_ns *ns;
1860a481f4d9SJohn Johansen 	char name[32];
1861a481f4d9SJohn Johansen 	int res;
1862a481f4d9SJohn Johansen 
1863a481f4d9SJohn Johansen 	ns = aa_get_current_ns();
1864a481f4d9SJohn Johansen 	res = ns_get_name(name, sizeof(name), ns, d_inode(dentry));
1865a481f4d9SJohn Johansen 	if (res >= 0)
1866a481f4d9SJohn Johansen 		res = readlink_copy(buffer, buflen, name);
1867a481f4d9SJohn Johansen 	aa_put_ns(ns);
1868a481f4d9SJohn Johansen 
1869a481f4d9SJohn Johansen 	return res;
1870a481f4d9SJohn Johansen }
1871a481f4d9SJohn Johansen 
1872a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = {
1873a481f4d9SJohn Johansen 	.readlink	= policy_readlink,
1874a481f4d9SJohn Johansen 	.get_link	= policy_get_link,
1875a481f4d9SJohn Johansen };
1876a481f4d9SJohn Johansen 
1877a481f4d9SJohn Johansen 
187863e2b423SJohn Johansen /**
187963e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
188063e2b423SJohn Johansen  *
188163e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
188263e2b423SJohn Johansen  *
188363e2b423SJohn Johansen  * Returns: error on failure
188463e2b423SJohn Johansen  */
18853417d8d5SJames Morris static int __init aa_create_aafs(void)
188663e2b423SJohn Johansen {
1887b7fd2c03SJohn Johansen 	struct dentry *dent;
188863e2b423SJohn Johansen 	int error;
188963e2b423SJohn Johansen 
189063e2b423SJohn Johansen 	if (!apparmor_initialized)
189163e2b423SJohn Johansen 		return 0;
189263e2b423SJohn Johansen 
1893c97204baSJohn Johansen 	if (aa_sfs_entry.dentry) {
189463e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
189563e2b423SJohn Johansen 		return -EEXIST;
189663e2b423SJohn Johansen 	}
189763e2b423SJohn Johansen 
1898a481f4d9SJohn Johansen 	/* setup apparmorfs used to virtualize policy/ */
1899a481f4d9SJohn Johansen 	aafs_mnt = kern_mount(&aafs_ops);
1900a481f4d9SJohn Johansen 	if (IS_ERR(aafs_mnt))
1901a481f4d9SJohn Johansen 		panic("can't set apparmorfs up\n");
1902a481f4d9SJohn Johansen 	aafs_mnt->mnt_sb->s_flags &= ~MS_NOUSER;
1903a481f4d9SJohn Johansen 
19049acd494bSKees Cook 	/* Populate fs tree. */
1905c97204baSJohn Johansen 	error = entry_create_dir(&aa_sfs_entry, NULL);
190663e2b423SJohn Johansen 	if (error)
190763e2b423SJohn Johansen 		goto error;
190863e2b423SJohn Johansen 
1909c97204baSJohn Johansen 	dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
1910b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_load);
1911b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1912b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1913b7fd2c03SJohn Johansen 		goto error;
1914b7fd2c03SJohn Johansen 	}
1915b7fd2c03SJohn Johansen 	ns_subload(root_ns) = dent;
1916b7fd2c03SJohn Johansen 
1917c97204baSJohn Johansen 	dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
1918b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_replace);
1919b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1920b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1921b7fd2c03SJohn Johansen 		goto error;
1922b7fd2c03SJohn Johansen 	}
1923b7fd2c03SJohn Johansen 	ns_subreplace(root_ns) = dent;
1924b7fd2c03SJohn Johansen 
1925c97204baSJohn Johansen 	dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
1926b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_remove);
1927b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1928b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1929b7fd2c03SJohn Johansen 		goto error;
1930b7fd2c03SJohn Johansen 	}
1931b7fd2c03SJohn Johansen 	ns_subremove(root_ns) = dent;
1932b7fd2c03SJohn Johansen 
1933b7fd2c03SJohn Johansen 	mutex_lock(&root_ns->lock);
1934c961ee5fSJohn Johansen 	error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
1935c961ee5fSJohn Johansen 				aafs_mnt->mnt_root);
1936b7fd2c03SJohn Johansen 	mutex_unlock(&root_ns->lock);
1937b7fd2c03SJohn Johansen 
19380d259f04SJohn Johansen 	if (error)
19390d259f04SJohn Johansen 		goto error;
19400d259f04SJohn Johansen 
1941c961ee5fSJohn Johansen 	/* magic symlink similar to nsfs redirects based on task policy */
1942c961ee5fSJohn Johansen 	dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
1943c961ee5fSJohn Johansen 					 NULL, &policy_link_iops);
1944c961ee5fSJohn Johansen 	if (IS_ERR(dent)) {
1945c961ee5fSJohn Johansen 		error = PTR_ERR(dent);
1946c961ee5fSJohn Johansen 		goto error;
1947c961ee5fSJohn Johansen 	}
1948c961ee5fSJohn Johansen 
1949c97204baSJohn Johansen 	error = aa_mk_null_file(aa_sfs_entry.dentry);
1950a71ada30SJohn Johansen 	if (error)
1951a71ada30SJohn Johansen 		goto error;
1952a71ada30SJohn Johansen 
1953a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
195463e2b423SJohn Johansen 
195563e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
195663e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
195763e2b423SJohn Johansen 	return 0;
195863e2b423SJohn Johansen 
195963e2b423SJohn Johansen error:
196063e2b423SJohn Johansen 	aa_destroy_aafs();
196163e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
196263e2b423SJohn Johansen 	return error;
196363e2b423SJohn Johansen }
196463e2b423SJohn Johansen 
196563e2b423SJohn Johansen fs_initcall(aa_create_aafs);
1966