1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
263e2b423SJohn Johansen /*
363e2b423SJohn Johansen  * AppArmor security module
463e2b423SJohn Johansen  *
563e2b423SJohn Johansen  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
663e2b423SJohn Johansen  *
763e2b423SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
863e2b423SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
963e2b423SJohn Johansen  */
1063e2b423SJohn Johansen 
110d259f04SJohn Johansen #include <linux/ctype.h>
1263e2b423SJohn Johansen #include <linux/security.h>
1363e2b423SJohn Johansen #include <linux/vmalloc.h>
14876979c9SPaul Gortmaker #include <linux/init.h>
1563e2b423SJohn Johansen #include <linux/seq_file.h>
1663e2b423SJohn Johansen #include <linux/uaccess.h>
17a71ada30SJohn Johansen #include <linux/mount.h>
1863e2b423SJohn Johansen #include <linux/namei.h>
19e74abcf3SKees Cook #include <linux/capability.h>
2029b3822fSJohn Johansen #include <linux/rcupdate.h>
21a71ada30SJohn Johansen #include <linux/fs.h>
22d9bf2c26SJohn Johansen #include <linux/poll.h>
23a481f4d9SJohn Johansen #include <uapi/linux/major.h>
24a481f4d9SJohn Johansen #include <uapi/linux/magic.h>
2563e2b423SJohn Johansen 
2663e2b423SJohn Johansen #include "include/apparmor.h"
2763e2b423SJohn Johansen #include "include/apparmorfs.h"
2863e2b423SJohn Johansen #include "include/audit.h"
29d8889d49SJohn Johansen #include "include/cred.h"
30f8eb8a13SJohn Johansen #include "include/crypto.h"
31cd1dbf76SJohn Johansen #include "include/ipc.h"
32317d9a05SJohn Johansen #include "include/label.h"
3363e2b423SJohn Johansen #include "include/policy.h"
34cff281f6SJohn Johansen #include "include/policy_ns.h"
35d384b0a1SKees Cook #include "include/resource.h"
365ac8c355SJohn Johansen #include "include/policy_unpack.h"
3763e2b423SJohn Johansen 
38c97204baSJohn Johansen /*
39c97204baSJohn Johansen  * The apparmor filesystem interface used for policy load and introspection
40c97204baSJohn Johansen  * The interface is split into two main components based on their function
41c97204baSJohn Johansen  * a securityfs component:
42c97204baSJohn Johansen  *   used for static files that are always available, and which allows
43c97204baSJohn Johansen  *   userspace to specificy the location of the security filesystem.
44c97204baSJohn Johansen  *
45c97204baSJohn Johansen  *   fns and data are prefixed with
46c97204baSJohn Johansen  *      aa_sfs_
47c97204baSJohn Johansen  *
48c97204baSJohn Johansen  * an apparmorfs component:
49c97204baSJohn Johansen  *   used loaded policy content and introspection. It is not part of  a
50c97204baSJohn Johansen  *   regular mounted filesystem and is available only through the magic
51c97204baSJohn Johansen  *   policy symlink in the root of the securityfs apparmor/ directory.
52c97204baSJohn Johansen  *   Tasks queries will be magically redirected to the correct portion
53c97204baSJohn Johansen  *   of the policy tree based on their confinement.
54c97204baSJohn Johansen  *
55c97204baSJohn Johansen  *   fns and data are prefixed with
56c97204baSJohn Johansen  *      aafs_
57c97204baSJohn Johansen  *
58c97204baSJohn Johansen  * The aa_fs_ prefix is used to indicate the fn is used by both the
59c97204baSJohn Johansen  * securityfs and apparmorfs filesystems.
60c97204baSJohn Johansen  */
61c97204baSJohn Johansen 
62c97204baSJohn Johansen 
63c97204baSJohn Johansen /*
64c97204baSJohn Johansen  * support fns
65c97204baSJohn Johansen  */
66c97204baSJohn Johansen 
6763e2b423SJohn Johansen /**
680d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
690d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
700d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
710d259f04SJohn Johansen  *
720d259f04SJohn Johansen  * Returns: length of mangled name
730d259f04SJohn Johansen  */
74bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
750d259f04SJohn Johansen {
760d259f04SJohn Johansen 	char *t = target;
770d259f04SJohn Johansen 
780d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
790d259f04SJohn Johansen 		name++;
800d259f04SJohn Johansen 
810d259f04SJohn Johansen 	if (target) {
820d259f04SJohn Johansen 		for (; *name; name++) {
830d259f04SJohn Johansen 			if (*name == '/')
840d259f04SJohn Johansen 				*(t)++ = '.';
850d259f04SJohn Johansen 			else if (isspace(*name))
860d259f04SJohn Johansen 				*(t)++ = '_';
870d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
880d259f04SJohn Johansen 				*(t)++ = *name;
890d259f04SJohn Johansen 		}
900d259f04SJohn Johansen 
910d259f04SJohn Johansen 		*t = 0;
920d259f04SJohn Johansen 	} else {
930d259f04SJohn Johansen 		int len = 0;
940d259f04SJohn Johansen 		for (; *name; name++) {
950d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
960d259f04SJohn Johansen 			    strchr("/._-", *name))
970d259f04SJohn Johansen 				len++;
980d259f04SJohn Johansen 		}
990d259f04SJohn Johansen 
1000d259f04SJohn Johansen 		return len;
1010d259f04SJohn Johansen 	}
1020d259f04SJohn Johansen 
1030d259f04SJohn Johansen 	return t - target;
1040d259f04SJohn Johansen }
1050d259f04SJohn Johansen 
106a481f4d9SJohn Johansen 
107a481f4d9SJohn Johansen /*
108a481f4d9SJohn Johansen  * aafs - core fns and data for the policy tree
109a481f4d9SJohn Johansen  */
110a481f4d9SJohn Johansen 
111a481f4d9SJohn Johansen #define AAFS_NAME		"apparmorfs"
112a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt;
113a481f4d9SJohn Johansen static int aafs_count;
114a481f4d9SJohn Johansen 
115a481f4d9SJohn Johansen 
116a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
117a481f4d9SJohn Johansen {
118a0781209SJohn Johansen 	seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino);
119a481f4d9SJohn Johansen 	return 0;
120a481f4d9SJohn Johansen }
121a481f4d9SJohn Johansen 
12227afa27dSAl Viro static void aafs_free_inode(struct inode *inode)
123a481f4d9SJohn Johansen {
124a481f4d9SJohn Johansen 	if (S_ISLNK(inode->i_mode))
125a481f4d9SJohn Johansen 		kfree(inode->i_link);
126f51dcd0fSAl Viro 	free_inode_nonrcu(inode);
127f51dcd0fSAl Viro }
128f51dcd0fSAl Viro 
129a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = {
130a481f4d9SJohn Johansen 	.statfs = simple_statfs,
13127afa27dSAl Viro 	.free_inode = aafs_free_inode,
132a481f4d9SJohn Johansen 	.show_path = aafs_show_path,
133a481f4d9SJohn Johansen };
134a481f4d9SJohn Johansen 
135a481f4d9SJohn Johansen static int fill_super(struct super_block *sb, void *data, int silent)
136a481f4d9SJohn Johansen {
137a481f4d9SJohn Johansen 	static struct tree_descr files[] = { {""} };
138a481f4d9SJohn Johansen 	int error;
139a481f4d9SJohn Johansen 
140a481f4d9SJohn Johansen 	error = simple_fill_super(sb, AAFS_MAGIC, files);
141a481f4d9SJohn Johansen 	if (error)
142a481f4d9SJohn Johansen 		return error;
143a481f4d9SJohn Johansen 	sb->s_op = &aafs_super_ops;
144a481f4d9SJohn Johansen 
145a481f4d9SJohn Johansen 	return 0;
146a481f4d9SJohn Johansen }
147a481f4d9SJohn Johansen 
148a481f4d9SJohn Johansen static struct dentry *aafs_mount(struct file_system_type *fs_type,
149a481f4d9SJohn Johansen 				 int flags, const char *dev_name, void *data)
150a481f4d9SJohn Johansen {
151a481f4d9SJohn Johansen 	return mount_single(fs_type, flags, data, fill_super);
152a481f4d9SJohn Johansen }
153a481f4d9SJohn Johansen 
154a481f4d9SJohn Johansen static struct file_system_type aafs_ops = {
155a481f4d9SJohn Johansen 	.owner = THIS_MODULE,
156a481f4d9SJohn Johansen 	.name = AAFS_NAME,
157a481f4d9SJohn Johansen 	.mount = aafs_mount,
158a481f4d9SJohn Johansen 	.kill_sb = kill_anon_super,
159a481f4d9SJohn Johansen };
160a481f4d9SJohn Johansen 
161a481f4d9SJohn Johansen /**
162a481f4d9SJohn Johansen  * __aafs_setup_d_inode - basic inode setup for apparmorfs
163a481f4d9SJohn Johansen  * @dir: parent directory for the dentry
164a481f4d9SJohn Johansen  * @dentry: dentry we are seting the inode up for
165a481f4d9SJohn Johansen  * @mode: permissions the file should have
166a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
167a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
168a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used
169a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
170a481f4d9SJohn Johansen  */
171a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
172a481f4d9SJohn Johansen 			       umode_t mode, void *data, char *link,
173a481f4d9SJohn Johansen 			       const struct file_operations *fops,
174a481f4d9SJohn Johansen 			       const struct inode_operations *iops)
175a481f4d9SJohn Johansen {
176a481f4d9SJohn Johansen 	struct inode *inode = new_inode(dir->i_sb);
177a481f4d9SJohn Johansen 
178a481f4d9SJohn Johansen 	AA_BUG(!dir);
179a481f4d9SJohn Johansen 	AA_BUG(!dentry);
180a481f4d9SJohn Johansen 
181a481f4d9SJohn Johansen 	if (!inode)
182a481f4d9SJohn Johansen 		return -ENOMEM;
183a481f4d9SJohn Johansen 
184a481f4d9SJohn Johansen 	inode->i_ino = get_next_ino();
185a481f4d9SJohn Johansen 	inode->i_mode = mode;
186a481f4d9SJohn Johansen 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
187a481f4d9SJohn Johansen 	inode->i_private = data;
188a481f4d9SJohn Johansen 	if (S_ISDIR(mode)) {
189a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_dir_inode_operations;
190a481f4d9SJohn Johansen 		inode->i_fop = &simple_dir_operations;
191a481f4d9SJohn Johansen 		inc_nlink(inode);
192a481f4d9SJohn Johansen 		inc_nlink(dir);
193a481f4d9SJohn Johansen 	} else if (S_ISLNK(mode)) {
194a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_symlink_inode_operations;
195a481f4d9SJohn Johansen 		inode->i_link = link;
196a481f4d9SJohn Johansen 	} else {
197a481f4d9SJohn Johansen 		inode->i_fop = fops;
198a481f4d9SJohn Johansen 	}
199a481f4d9SJohn Johansen 	d_instantiate(dentry, inode);
200a481f4d9SJohn Johansen 	dget(dentry);
201a481f4d9SJohn Johansen 
202a481f4d9SJohn Johansen 	return 0;
203a481f4d9SJohn Johansen }
204a481f4d9SJohn Johansen 
205a481f4d9SJohn Johansen /**
206a481f4d9SJohn Johansen  * aafs_create - create a dentry in the apparmorfs filesystem
207a481f4d9SJohn Johansen  *
208a481f4d9SJohn Johansen  * @name: name of dentry to create
209a481f4d9SJohn Johansen  * @mode: permissions the file should have
210a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
211a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
212a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
213a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
214a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
215a481f4d9SJohn Johansen  *
216a481f4d9SJohn Johansen  * This is the basic "create a xxx" function for apparmorfs.
217a481f4d9SJohn Johansen  *
218a481f4d9SJohn Johansen  * Returns a pointer to a dentry if it succeeds, that must be free with
219a481f4d9SJohn Johansen  * aafs_remove(). Will return ERR_PTR on failure.
220a481f4d9SJohn Johansen  */
221a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode,
222a481f4d9SJohn Johansen 				  struct dentry *parent, void *data, void *link,
223a481f4d9SJohn Johansen 				  const struct file_operations *fops,
224a481f4d9SJohn Johansen 				  const struct inode_operations *iops)
225a481f4d9SJohn Johansen {
226a481f4d9SJohn Johansen 	struct dentry *dentry;
227a481f4d9SJohn Johansen 	struct inode *dir;
228a481f4d9SJohn Johansen 	int error;
229a481f4d9SJohn Johansen 
230a481f4d9SJohn Johansen 	AA_BUG(!name);
231a481f4d9SJohn Johansen 	AA_BUG(!parent);
232a481f4d9SJohn Johansen 
233a481f4d9SJohn Johansen 	if (!(mode & S_IFMT))
234a481f4d9SJohn Johansen 		mode = (mode & S_IALLUGO) | S_IFREG;
235a481f4d9SJohn Johansen 
236a481f4d9SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
237a481f4d9SJohn Johansen 	if (error)
238a481f4d9SJohn Johansen 		return ERR_PTR(error);
239a481f4d9SJohn Johansen 
240a481f4d9SJohn Johansen 	dir = d_inode(parent);
241a481f4d9SJohn Johansen 
242a481f4d9SJohn Johansen 	inode_lock(dir);
243a481f4d9SJohn Johansen 	dentry = lookup_one_len(name, parent, strlen(name));
2445d314a81SDan Carpenter 	if (IS_ERR(dentry)) {
2455d314a81SDan Carpenter 		error = PTR_ERR(dentry);
246a481f4d9SJohn Johansen 		goto fail_lock;
2475d314a81SDan Carpenter 	}
248a481f4d9SJohn Johansen 
249a481f4d9SJohn Johansen 	if (d_really_is_positive(dentry)) {
250a481f4d9SJohn Johansen 		error = -EEXIST;
251a481f4d9SJohn Johansen 		goto fail_dentry;
252a481f4d9SJohn Johansen 	}
253a481f4d9SJohn Johansen 
254a481f4d9SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
255a481f4d9SJohn Johansen 	if (error)
256a481f4d9SJohn Johansen 		goto fail_dentry;
257a481f4d9SJohn Johansen 	inode_unlock(dir);
258a481f4d9SJohn Johansen 
259a481f4d9SJohn Johansen 	return dentry;
260a481f4d9SJohn Johansen 
261a481f4d9SJohn Johansen fail_dentry:
262a481f4d9SJohn Johansen 	dput(dentry);
263a481f4d9SJohn Johansen 
264a481f4d9SJohn Johansen fail_lock:
265a481f4d9SJohn Johansen 	inode_unlock(dir);
266a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
267a481f4d9SJohn Johansen 
268a481f4d9SJohn Johansen 	return ERR_PTR(error);
269a481f4d9SJohn Johansen }
270a481f4d9SJohn Johansen 
271a481f4d9SJohn Johansen /**
272a481f4d9SJohn Johansen  * aafs_create_file - create a file in the apparmorfs filesystem
273a481f4d9SJohn Johansen  *
274a481f4d9SJohn Johansen  * @name: name of dentry to create
275a481f4d9SJohn Johansen  * @mode: permissions the file should have
276a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
277a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
278a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
279a481f4d9SJohn Johansen  *
280a481f4d9SJohn Johansen  * see aafs_create
281a481f4d9SJohn Johansen  */
282a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode,
283a481f4d9SJohn Johansen 				       struct dentry *parent, void *data,
284a481f4d9SJohn Johansen 				       const struct file_operations *fops)
285a481f4d9SJohn Johansen {
286a481f4d9SJohn Johansen 	return aafs_create(name, mode, parent, data, NULL, fops, NULL);
287a481f4d9SJohn Johansen }
288a481f4d9SJohn Johansen 
289a481f4d9SJohn Johansen /**
290a481f4d9SJohn Johansen  * aafs_create_dir - create a directory in the apparmorfs filesystem
291a481f4d9SJohn Johansen  *
292a481f4d9SJohn Johansen  * @name: name of dentry to create
293a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
294a481f4d9SJohn Johansen  *
295a481f4d9SJohn Johansen  * see aafs_create
296a481f4d9SJohn Johansen  */
297a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
298a481f4d9SJohn Johansen {
299a481f4d9SJohn Johansen 	return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
300a481f4d9SJohn Johansen 			   NULL);
301a481f4d9SJohn Johansen }
302a481f4d9SJohn Johansen 
303a481f4d9SJohn Johansen /**
304a481f4d9SJohn Johansen  * aafs_create_symlink - create a symlink in the apparmorfs filesystem
305a481f4d9SJohn Johansen  * @name: name of dentry to create
306a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
307a481f4d9SJohn Johansen  * @target: if symlink, symlink target string
3081180b4c7SJohn Johansen  * @private: private data
309a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
310a481f4d9SJohn Johansen  *
311a481f4d9SJohn Johansen  * If @target parameter is %NULL, then the @iops parameter needs to be
312a481f4d9SJohn Johansen  * setup to handle .readlink and .get_link inode_operations.
313a481f4d9SJohn Johansen  */
314a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name,
315a481f4d9SJohn Johansen 					  struct dentry *parent,
316a481f4d9SJohn Johansen 					  const char *target,
3171180b4c7SJohn Johansen 					  void *private,
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 		if (!link)
325a481f4d9SJohn Johansen 			return ERR_PTR(-ENOMEM);
326a481f4d9SJohn Johansen 	}
3271180b4c7SJohn Johansen 	dent = aafs_create(name, S_IFLNK | 0444, parent, private, link, NULL,
328a481f4d9SJohn Johansen 			   iops);
329a481f4d9SJohn Johansen 	if (IS_ERR(dent))
330a481f4d9SJohn Johansen 		kfree(link);
331a481f4d9SJohn Johansen 
332a481f4d9SJohn Johansen 	return dent;
333a481f4d9SJohn Johansen }
334a481f4d9SJohn Johansen 
335a481f4d9SJohn Johansen /**
336a481f4d9SJohn Johansen  * aafs_remove - removes a file or directory from the apparmorfs filesystem
337a481f4d9SJohn Johansen  *
338a481f4d9SJohn Johansen  * @dentry: dentry of the file/directory/symlink to removed.
339a481f4d9SJohn Johansen  */
340a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry)
341a481f4d9SJohn Johansen {
342a481f4d9SJohn Johansen 	struct inode *dir;
343a481f4d9SJohn Johansen 
344a481f4d9SJohn Johansen 	if (!dentry || IS_ERR(dentry))
345a481f4d9SJohn Johansen 		return;
346a481f4d9SJohn Johansen 
347a481f4d9SJohn Johansen 	dir = d_inode(dentry->d_parent);
348a481f4d9SJohn Johansen 	inode_lock(dir);
349a481f4d9SJohn Johansen 	if (simple_positive(dentry)) {
350a481f4d9SJohn Johansen 		if (d_is_dir(dentry))
351a481f4d9SJohn Johansen 			simple_rmdir(dir, dentry);
352a481f4d9SJohn Johansen 		else
353a481f4d9SJohn Johansen 			simple_unlink(dir, dentry);
354201218e4SChris Coulson 		d_delete(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 	struct aa_loaddata *data;
407637f688dSJohn Johansen 	struct aa_label *label;
408637f688dSJohn Johansen 	ssize_t error;
409cf797c0eSJohn Johansen 
410637f688dSJohn Johansen 	label = begin_current_label_crit_section();
411cf797c0eSJohn Johansen 
4125ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4135ac8c355SJohn Johansen 	 * below after unpack
4145ac8c355SJohn Johansen 	 */
415637f688dSJohn Johansen 	error = aa_may_manage_policy(label, ns, mask);
4165ac8c355SJohn Johansen 	if (error)
4175ac8c355SJohn Johansen 		return error;
41863e2b423SJohn Johansen 
4195ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size, size, pos);
4205ac8c355SJohn Johansen 	error = PTR_ERR(data);
4215ac8c355SJohn Johansen 	if (!IS_ERR(data)) {
422637f688dSJohn Johansen 		error = aa_replace_profiles(ns, label, mask, data);
4235ac8c355SJohn Johansen 		aa_put_loaddata(data);
4245ac8c355SJohn Johansen 	}
425637f688dSJohn Johansen 	end_current_label_crit_section(label);
4265ac8c355SJohn Johansen 
4275ac8c355SJohn Johansen 	return error;
4285ac8c355SJohn Johansen }
4295ac8c355SJohn Johansen 
430b7fd2c03SJohn Johansen /* .load file hook fn to load policy */
43163e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
43263e2b423SJohn Johansen 			    loff_t *pos)
43363e2b423SJohn Johansen {
434b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
43518e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
436b7fd2c03SJohn Johansen 
437b7fd2c03SJohn Johansen 	aa_put_ns(ns);
43863e2b423SJohn Johansen 
43963e2b423SJohn Johansen 	return error;
44063e2b423SJohn Johansen }
44163e2b423SJohn Johansen 
44263e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
4436038f373SArnd Bergmann 	.write = profile_load,
4446038f373SArnd Bergmann 	.llseek = default_llseek,
44563e2b423SJohn Johansen };
44663e2b423SJohn Johansen 
44763e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
44863e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
44963e2b423SJohn Johansen 			       size_t size, loff_t *pos)
45063e2b423SJohn Johansen {
451b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
45218e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
45318e99f19SJohn Johansen 				  buf, size, pos, ns);
454b7fd2c03SJohn Johansen 	aa_put_ns(ns);
45563e2b423SJohn Johansen 
45663e2b423SJohn Johansen 	return error;
45763e2b423SJohn Johansen }
45863e2b423SJohn Johansen 
45963e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
4606038f373SArnd Bergmann 	.write = profile_replace,
4616038f373SArnd Bergmann 	.llseek = default_llseek,
46263e2b423SJohn Johansen };
46363e2b423SJohn Johansen 
464b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */
46563e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
46663e2b423SJohn Johansen 			      size_t size, loff_t *pos)
46763e2b423SJohn Johansen {
4685ac8c355SJohn Johansen 	struct aa_loaddata *data;
469637f688dSJohn Johansen 	struct aa_label *label;
47063e2b423SJohn Johansen 	ssize_t error;
471b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
47263e2b423SJohn Johansen 
473637f688dSJohn Johansen 	label = begin_current_label_crit_section();
4745ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4755ac8c355SJohn Johansen 	 * below after unpack
4765ac8c355SJohn Johansen 	 */
477637f688dSJohn Johansen 	error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY);
4785ac8c355SJohn Johansen 	if (error)
4795ac8c355SJohn Johansen 		goto out;
4805ac8c355SJohn Johansen 
48163e2b423SJohn Johansen 	/*
48263e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
48363e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
48463e2b423SJohn Johansen 	 */
4855ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
48663e2b423SJohn Johansen 
48763e2b423SJohn Johansen 	error = PTR_ERR(data);
48863e2b423SJohn Johansen 	if (!IS_ERR(data)) {
4895ac8c355SJohn Johansen 		data->data[size] = 0;
490637f688dSJohn Johansen 		error = aa_remove_profiles(ns, label, data->data, size);
4915ac8c355SJohn Johansen 		aa_put_loaddata(data);
49263e2b423SJohn Johansen 	}
4935ac8c355SJohn Johansen  out:
494637f688dSJohn Johansen 	end_current_label_crit_section(label);
495b7fd2c03SJohn Johansen 	aa_put_ns(ns);
49663e2b423SJohn Johansen 	return error;
49763e2b423SJohn Johansen }
49863e2b423SJohn Johansen 
49963e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
5006038f373SArnd Bergmann 	.write = profile_remove,
5016038f373SArnd Bergmann 	.llseek = default_llseek,
50263e2b423SJohn Johansen };
50363e2b423SJohn Johansen 
504d9bf2c26SJohn Johansen struct aa_revision {
505d9bf2c26SJohn Johansen 	struct aa_ns *ns;
506d9bf2c26SJohn Johansen 	long last_read;
507d9bf2c26SJohn Johansen };
508d9bf2c26SJohn Johansen 
509d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */
510d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file)
511d9bf2c26SJohn Johansen {
512d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
513d9bf2c26SJohn Johansen 
514d9bf2c26SJohn Johansen 	if (rev) {
515d9bf2c26SJohn Johansen 		aa_put_ns(rev->ns);
516d9bf2c26SJohn Johansen 		kfree(rev);
517d9bf2c26SJohn Johansen 	}
518d9bf2c26SJohn Johansen 
519d9bf2c26SJohn Johansen 	return 0;
520d9bf2c26SJohn Johansen }
521d9bf2c26SJohn Johansen 
522d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf,
523d9bf2c26SJohn Johansen 				size_t size, loff_t *ppos)
524d9bf2c26SJohn Johansen {
525d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
526d9bf2c26SJohn Johansen 	char buffer[32];
527d9bf2c26SJohn Johansen 	long last_read;
528d9bf2c26SJohn Johansen 	int avail;
529d9bf2c26SJohn Johansen 
530feb3c766SJohn Johansen 	mutex_lock_nested(&rev->ns->lock, rev->ns->level);
531d9bf2c26SJohn Johansen 	last_read = rev->last_read;
532d9bf2c26SJohn Johansen 	if (last_read == rev->ns->revision) {
533d9bf2c26SJohn Johansen 		mutex_unlock(&rev->ns->lock);
534d9bf2c26SJohn Johansen 		if (file->f_flags & O_NONBLOCK)
535d9bf2c26SJohn Johansen 			return -EAGAIN;
536d9bf2c26SJohn Johansen 		if (wait_event_interruptible(rev->ns->wait,
537d9bf2c26SJohn Johansen 					     last_read !=
538d9bf2c26SJohn Johansen 					     READ_ONCE(rev->ns->revision)))
539d9bf2c26SJohn Johansen 			return -ERESTARTSYS;
540feb3c766SJohn Johansen 		mutex_lock_nested(&rev->ns->lock, rev->ns->level);
541d9bf2c26SJohn Johansen 	}
542d9bf2c26SJohn Johansen 
543d9bf2c26SJohn Johansen 	avail = sprintf(buffer, "%ld\n", rev->ns->revision);
544d9bf2c26SJohn Johansen 	if (*ppos + size > avail) {
545d9bf2c26SJohn Johansen 		rev->last_read = rev->ns->revision;
546d9bf2c26SJohn Johansen 		*ppos = 0;
547d9bf2c26SJohn Johansen 	}
548d9bf2c26SJohn Johansen 	mutex_unlock(&rev->ns->lock);
549d9bf2c26SJohn Johansen 
550d9bf2c26SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, buffer, avail);
551d9bf2c26SJohn Johansen }
552d9bf2c26SJohn Johansen 
553d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file)
554d9bf2c26SJohn Johansen {
555d9bf2c26SJohn Johansen 	struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
556d9bf2c26SJohn Johansen 
557d9bf2c26SJohn Johansen 	if (!rev)
558d9bf2c26SJohn Johansen 		return -ENOMEM;
559d9bf2c26SJohn Johansen 
560d9bf2c26SJohn Johansen 	rev->ns = aa_get_ns(inode->i_private);
561d9bf2c26SJohn Johansen 	if (!rev->ns)
562d9bf2c26SJohn Johansen 		rev->ns = aa_get_current_ns();
563d9bf2c26SJohn Johansen 	file->private_data = rev;
564d9bf2c26SJohn Johansen 
565d9bf2c26SJohn Johansen 	return 0;
566d9bf2c26SJohn Johansen }
567d9bf2c26SJohn Johansen 
568e6c5a7d9SAl Viro static __poll_t ns_revision_poll(struct file *file, poll_table *pt)
569d9bf2c26SJohn Johansen {
570d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
571e6c5a7d9SAl Viro 	__poll_t mask = 0;
572d9bf2c26SJohn Johansen 
573d9bf2c26SJohn Johansen 	if (rev) {
574feb3c766SJohn Johansen 		mutex_lock_nested(&rev->ns->lock, rev->ns->level);
575d9bf2c26SJohn Johansen 		poll_wait(file, &rev->ns->wait, pt);
576d9bf2c26SJohn Johansen 		if (rev->last_read < rev->ns->revision)
577a9a08845SLinus Torvalds 			mask |= EPOLLIN | EPOLLRDNORM;
578d9bf2c26SJohn Johansen 		mutex_unlock(&rev->ns->lock);
579d9bf2c26SJohn Johansen 	}
580d9bf2c26SJohn Johansen 
581d9bf2c26SJohn Johansen 	return mask;
582d9bf2c26SJohn Johansen }
583d9bf2c26SJohn Johansen 
5845d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns)
5855d5182caSJohn Johansen {
5865d5182caSJohn Johansen 	ns->revision++;
587d9bf2c26SJohn Johansen 	wake_up_interruptible(&ns->wait);
5885d5182caSJohn Johansen }
5895d5182caSJohn Johansen 
590d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = {
591d9bf2c26SJohn Johansen 	.owner		= THIS_MODULE,
592d9bf2c26SJohn Johansen 	.open		= ns_revision_open,
593d9bf2c26SJohn Johansen 	.poll		= ns_revision_poll,
594d9bf2c26SJohn Johansen 	.read		= ns_revision_read,
595d9bf2c26SJohn Johansen 	.llseek		= generic_file_llseek,
596d9bf2c26SJohn Johansen 	.release	= ns_revision_release,
597d9bf2c26SJohn Johansen };
598d9bf2c26SJohn Johansen 
5994f3b3f2dSJohn Johansen static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
6004f3b3f2dSJohn Johansen 			     const char *match_str, size_t match_len)
6014f3b3f2dSJohn Johansen {
602f4585bc2STyler Hicks 	struct aa_perms tmp = { };
6034f3b3f2dSJohn Johansen 	struct aa_dfa *dfa;
6044f3b3f2dSJohn Johansen 	unsigned int state = 0;
6054f3b3f2dSJohn Johansen 
606637f688dSJohn Johansen 	if (profile_unconfined(profile))
6074f3b3f2dSJohn Johansen 		return;
6084f3b3f2dSJohn Johansen 	if (profile->file.dfa && *match_str == AA_CLASS_FILE) {
6094f3b3f2dSJohn Johansen 		dfa = profile->file.dfa;
6104f3b3f2dSJohn Johansen 		state = aa_dfa_match_len(dfa, profile->file.start,
6114f3b3f2dSJohn Johansen 					 match_str + 1, match_len - 1);
6124f3b3f2dSJohn Johansen 		if (state) {
6134f3b3f2dSJohn Johansen 			struct path_cond cond = { };
6144f3b3f2dSJohn Johansen 
6154f3b3f2dSJohn Johansen 			tmp = aa_compute_fperms(dfa, state, &cond);
6164f3b3f2dSJohn Johansen 		}
6174f3b3f2dSJohn Johansen 	} else if (profile->policy.dfa) {
618b9590ad4SJohn Johansen 		if (!PROFILE_MEDIATES(profile, *match_str))
6194f3b3f2dSJohn Johansen 			return;	/* no change to current perms */
6204f3b3f2dSJohn Johansen 		dfa = profile->policy.dfa;
6214f3b3f2dSJohn Johansen 		state = aa_dfa_match_len(dfa, profile->policy.start[0],
6224f3b3f2dSJohn Johansen 					 match_str, match_len);
6234f3b3f2dSJohn Johansen 		if (state)
6244f3b3f2dSJohn Johansen 			aa_compute_perms(dfa, state, &tmp);
6254f3b3f2dSJohn Johansen 	}
6264f3b3f2dSJohn Johansen 	aa_apply_modes_to_perms(profile, &tmp);
627317d9a05SJohn Johansen 	aa_perms_accum_raw(perms, &tmp);
6284f3b3f2dSJohn Johansen }
6294f3b3f2dSJohn Johansen 
6304f3b3f2dSJohn Johansen 
631e025be0fSWilliam Hua /**
632e025be0fSWilliam Hua  * query_data - queries a policy and writes its data to buf
633e025be0fSWilliam Hua  * @buf: the resulting data is stored here (NOT NULL)
634e025be0fSWilliam Hua  * @buf_len: size of buf
635e025be0fSWilliam Hua  * @query: query string used to retrieve data
636e025be0fSWilliam Hua  * @query_len: size of query including second NUL byte
637e025be0fSWilliam Hua  *
638e025be0fSWilliam Hua  * The buffers pointed to by buf and query may overlap. The query buffer is
639e025be0fSWilliam Hua  * parsed before buf is written to.
640e025be0fSWilliam Hua  *
641e025be0fSWilliam Hua  * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
642e025be0fSWilliam Hua  * the security confinement context and <KEY> is the name of the data to
643e025be0fSWilliam Hua  * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
644e025be0fSWilliam Hua  *
645e025be0fSWilliam Hua  * Don't expect the contents of buf to be preserved on failure.
646e025be0fSWilliam Hua  *
647e025be0fSWilliam Hua  * Returns: number of characters written to buf or -errno on failure
648e025be0fSWilliam Hua  */
649e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len,
650e025be0fSWilliam Hua 			  char *query, size_t query_len)
651e025be0fSWilliam Hua {
652e025be0fSWilliam Hua 	char *out;
653e025be0fSWilliam Hua 	const char *key;
654317d9a05SJohn Johansen 	struct label_it i;
655637f688dSJohn Johansen 	struct aa_label *label, *curr;
656317d9a05SJohn Johansen 	struct aa_profile *profile;
657e025be0fSWilliam Hua 	struct aa_data *data;
658e025be0fSWilliam Hua 	u32 bytes, blocks;
659e025be0fSWilliam Hua 	__le32 outle32;
660e025be0fSWilliam Hua 
661e025be0fSWilliam Hua 	if (!query_len)
662e025be0fSWilliam Hua 		return -EINVAL; /* need a query */
663e025be0fSWilliam Hua 
664e025be0fSWilliam Hua 	key = query + strnlen(query, query_len) + 1;
665e025be0fSWilliam Hua 	if (key + 1 >= query + query_len)
666e025be0fSWilliam Hua 		return -EINVAL; /* not enough space for a non-empty key */
667e025be0fSWilliam Hua 	if (key + strnlen(key, query + query_len - key) >= query + query_len)
668e025be0fSWilliam Hua 		return -EINVAL; /* must end with NUL */
669e025be0fSWilliam Hua 
670e025be0fSWilliam Hua 	if (buf_len < sizeof(bytes) + sizeof(blocks))
671e025be0fSWilliam Hua 		return -EINVAL; /* not enough space */
672e025be0fSWilliam Hua 
673637f688dSJohn Johansen 	curr = begin_current_label_crit_section();
674637f688dSJohn Johansen 	label = aa_label_parse(curr, query, GFP_KERNEL, false, false);
675637f688dSJohn Johansen 	end_current_label_crit_section(curr);
676637f688dSJohn Johansen 	if (IS_ERR(label))
677637f688dSJohn Johansen 		return PTR_ERR(label);
678e025be0fSWilliam Hua 
679e025be0fSWilliam Hua 	/* We are going to leave space for two numbers. The first is the total
680e025be0fSWilliam Hua 	 * number of bytes we are writing after the first number. This is so
681e025be0fSWilliam Hua 	 * users can read the full output without reallocation.
682e025be0fSWilliam Hua 	 *
683e025be0fSWilliam Hua 	 * The second number is the number of data blocks we're writing. An
684e025be0fSWilliam Hua 	 * application might be confined by multiple policies having data in
685e025be0fSWilliam Hua 	 * the same key.
686e025be0fSWilliam Hua 	 */
687e025be0fSWilliam Hua 	memset(buf, 0, sizeof(bytes) + sizeof(blocks));
688e025be0fSWilliam Hua 	out = buf + sizeof(bytes) + sizeof(blocks);
689e025be0fSWilliam Hua 
690e025be0fSWilliam Hua 	blocks = 0;
691317d9a05SJohn Johansen 	label_for_each_confined(i, label, profile) {
692317d9a05SJohn Johansen 		if (!profile->data)
693317d9a05SJohn Johansen 			continue;
694317d9a05SJohn Johansen 
695317d9a05SJohn Johansen 		data = rhashtable_lookup_fast(profile->data, &key,
696317d9a05SJohn Johansen 					      profile->data->p);
697e025be0fSWilliam Hua 
698e025be0fSWilliam Hua 		if (data) {
699317d9a05SJohn Johansen 			if (out + sizeof(outle32) + data->size > buf +
700317d9a05SJohn Johansen 			    buf_len) {
701637f688dSJohn Johansen 				aa_put_label(label);
702e025be0fSWilliam Hua 				return -EINVAL; /* not enough space */
703637f688dSJohn Johansen 			}
704e025be0fSWilliam Hua 			outle32 = __cpu_to_le32(data->size);
705e025be0fSWilliam Hua 			memcpy(out, &outle32, sizeof(outle32));
706e025be0fSWilliam Hua 			out += sizeof(outle32);
707e025be0fSWilliam Hua 			memcpy(out, data->data, data->size);
708e025be0fSWilliam Hua 			out += data->size;
709e025be0fSWilliam Hua 			blocks++;
710e025be0fSWilliam Hua 		}
711e025be0fSWilliam Hua 	}
712637f688dSJohn Johansen 	aa_put_label(label);
713e025be0fSWilliam Hua 
714e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
715e025be0fSWilliam Hua 	memcpy(buf, &outle32, sizeof(outle32));
716e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(blocks);
717e025be0fSWilliam Hua 	memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
718e025be0fSWilliam Hua 
719e025be0fSWilliam Hua 	return out - buf;
720e025be0fSWilliam Hua }
721e025be0fSWilliam Hua 
7224f3b3f2dSJohn Johansen /**
7234f3b3f2dSJohn Johansen  * query_label - queries a label and writes permissions to buf
7244f3b3f2dSJohn Johansen  * @buf: the resulting permissions string is stored here (NOT NULL)
7254f3b3f2dSJohn Johansen  * @buf_len: size of buf
7264f3b3f2dSJohn Johansen  * @query: binary query string to match against the dfa
7274f3b3f2dSJohn Johansen  * @query_len: size of query
7284f3b3f2dSJohn Johansen  * @view_only: only compute for querier's view
7294f3b3f2dSJohn Johansen  *
7304f3b3f2dSJohn Johansen  * The buffers pointed to by buf and query may overlap. The query buffer is
7314f3b3f2dSJohn Johansen  * parsed before buf is written to.
7324f3b3f2dSJohn Johansen  *
7334f3b3f2dSJohn Johansen  * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is
7344f3b3f2dSJohn Johansen  * the name of the label, in the current namespace, that is to be queried and
7354f3b3f2dSJohn Johansen  * DFA_STRING is a binary string to match against the label(s)'s DFA.
7364f3b3f2dSJohn Johansen  *
7374f3b3f2dSJohn Johansen  * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters
7384f3b3f2dSJohn Johansen  * but must *not* be NUL terminated.
7394f3b3f2dSJohn Johansen  *
7404f3b3f2dSJohn Johansen  * Returns: number of characters written to buf or -errno on failure
7414f3b3f2dSJohn Johansen  */
7424f3b3f2dSJohn Johansen static ssize_t query_label(char *buf, size_t buf_len,
7434f3b3f2dSJohn Johansen 			   char *query, size_t query_len, bool view_only)
7444f3b3f2dSJohn Johansen {
745317d9a05SJohn Johansen 	struct aa_profile *profile;
746637f688dSJohn Johansen 	struct aa_label *label, *curr;
7474f3b3f2dSJohn Johansen 	char *label_name, *match_str;
7484f3b3f2dSJohn Johansen 	size_t label_name_len, match_len;
7494f3b3f2dSJohn Johansen 	struct aa_perms perms;
750317d9a05SJohn Johansen 	struct label_it i;
7514f3b3f2dSJohn Johansen 
7524f3b3f2dSJohn Johansen 	if (!query_len)
7534f3b3f2dSJohn Johansen 		return -EINVAL;
7544f3b3f2dSJohn Johansen 
7554f3b3f2dSJohn Johansen 	label_name = query;
7564f3b3f2dSJohn Johansen 	label_name_len = strnlen(query, query_len);
7574f3b3f2dSJohn Johansen 	if (!label_name_len || label_name_len == query_len)
7584f3b3f2dSJohn Johansen 		return -EINVAL;
7594f3b3f2dSJohn Johansen 
7604f3b3f2dSJohn Johansen 	/**
7614f3b3f2dSJohn Johansen 	 * The extra byte is to account for the null byte between the
7624f3b3f2dSJohn Johansen 	 * profile name and dfa string. profile_name_len is greater
7634f3b3f2dSJohn Johansen 	 * than zero and less than query_len, so a byte can be safely
7644f3b3f2dSJohn Johansen 	 * added or subtracted.
7654f3b3f2dSJohn Johansen 	 */
7664f3b3f2dSJohn Johansen 	match_str = label_name + label_name_len + 1;
7674f3b3f2dSJohn Johansen 	match_len = query_len - label_name_len - 1;
7684f3b3f2dSJohn Johansen 
769637f688dSJohn Johansen 	curr = begin_current_label_crit_section();
770637f688dSJohn Johansen 	label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false);
771637f688dSJohn Johansen 	end_current_label_crit_section(curr);
772637f688dSJohn Johansen 	if (IS_ERR(label))
773637f688dSJohn Johansen 		return PTR_ERR(label);
7744f3b3f2dSJohn Johansen 
7754f3b3f2dSJohn Johansen 	perms = allperms;
776317d9a05SJohn Johansen 	if (view_only) {
777317d9a05SJohn Johansen 		label_for_each_in_ns(i, labels_ns(label), label, profile) {
778317d9a05SJohn Johansen 			profile_query_cb(profile, &perms, match_str, match_len);
779317d9a05SJohn Johansen 		}
780317d9a05SJohn Johansen 	} else {
781317d9a05SJohn Johansen 		label_for_each(i, label, profile) {
782317d9a05SJohn Johansen 			profile_query_cb(profile, &perms, match_str, match_len);
783317d9a05SJohn Johansen 		}
784317d9a05SJohn Johansen 	}
785317d9a05SJohn Johansen 	aa_put_label(label);
7864f3b3f2dSJohn Johansen 
7874f3b3f2dSJohn Johansen 	return scnprintf(buf, buf_len,
7884f3b3f2dSJohn Johansen 		      "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n",
7894f3b3f2dSJohn Johansen 		      perms.allow, perms.deny, perms.audit, perms.quiet);
7904f3b3f2dSJohn Johansen }
7914f3b3f2dSJohn Johansen 
7921dea3b41SJohn Johansen /*
7931dea3b41SJohn Johansen  * Transaction based IO.
7941dea3b41SJohn Johansen  * The file expects a write which triggers the transaction, and then
7951dea3b41SJohn Johansen  * possibly a read(s) which collects the result - which is stored in a
7961dea3b41SJohn Johansen  * file-local buffer. Once a new write is performed, a new set of results
7971dea3b41SJohn Johansen  * are stored in the file-local buffer.
7981dea3b41SJohn Johansen  */
7991dea3b41SJohn Johansen struct multi_transaction {
8001dea3b41SJohn Johansen 	struct kref count;
8011dea3b41SJohn Johansen 	ssize_t size;
8021dea3b41SJohn Johansen 	char data[0];
8031dea3b41SJohn Johansen };
8041dea3b41SJohn Johansen 
8051dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction))
8061dea3b41SJohn Johansen /* TODO: replace with per file lock */
8071dea3b41SJohn Johansen static DEFINE_SPINLOCK(multi_transaction_lock);
8081dea3b41SJohn Johansen 
8091dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref)
8101dea3b41SJohn Johansen {
8111dea3b41SJohn Johansen 	struct multi_transaction *t;
8121dea3b41SJohn Johansen 
8131dea3b41SJohn Johansen 	t = container_of(kref, struct multi_transaction, count);
8141dea3b41SJohn Johansen 	free_page((unsigned long) t);
8151dea3b41SJohn Johansen }
8161dea3b41SJohn Johansen 
8171dea3b41SJohn Johansen static struct multi_transaction *
8181dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t)
8191dea3b41SJohn Johansen {
8201dea3b41SJohn Johansen 	if  (t)
8211dea3b41SJohn Johansen 		kref_get(&(t->count));
8221dea3b41SJohn Johansen 
8231dea3b41SJohn Johansen 	return t;
8241dea3b41SJohn Johansen }
8251dea3b41SJohn Johansen 
8261dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t)
8271dea3b41SJohn Johansen {
8281dea3b41SJohn Johansen 	if (t)
8291dea3b41SJohn Johansen 		kref_put(&(t->count), multi_transaction_kref);
8301dea3b41SJohn Johansen }
8311dea3b41SJohn Johansen 
8321dea3b41SJohn Johansen /* does not increment @new's count */
8331dea3b41SJohn Johansen static void multi_transaction_set(struct file *file,
8341dea3b41SJohn Johansen 				  struct multi_transaction *new, size_t n)
8351dea3b41SJohn Johansen {
8361dea3b41SJohn Johansen 	struct multi_transaction *old;
8371dea3b41SJohn Johansen 
8381dea3b41SJohn Johansen 	AA_BUG(n > MULTI_TRANSACTION_LIMIT);
8391dea3b41SJohn Johansen 
8401dea3b41SJohn Johansen 	new->size = n;
8411dea3b41SJohn Johansen 	spin_lock(&multi_transaction_lock);
8421dea3b41SJohn Johansen 	old = (struct multi_transaction *) file->private_data;
8431dea3b41SJohn Johansen 	file->private_data = new;
8441dea3b41SJohn Johansen 	spin_unlock(&multi_transaction_lock);
8451dea3b41SJohn Johansen 	put_multi_transaction(old);
8461dea3b41SJohn Johansen }
8471dea3b41SJohn Johansen 
8481dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file,
8491dea3b41SJohn Johansen 						       const char __user *buf,
8501dea3b41SJohn Johansen 						       size_t size)
8511dea3b41SJohn Johansen {
8521dea3b41SJohn Johansen 	struct multi_transaction *t;
8531dea3b41SJohn Johansen 
8541dea3b41SJohn Johansen 	if (size > MULTI_TRANSACTION_LIMIT - 1)
8551dea3b41SJohn Johansen 		return ERR_PTR(-EFBIG);
8561dea3b41SJohn Johansen 
8571dea3b41SJohn Johansen 	t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
8581dea3b41SJohn Johansen 	if (!t)
8591dea3b41SJohn Johansen 		return ERR_PTR(-ENOMEM);
8601dea3b41SJohn Johansen 	kref_init(&t->count);
8611dea3b41SJohn Johansen 	if (copy_from_user(t->data, buf, size))
8621dea3b41SJohn Johansen 		return ERR_PTR(-EFAULT);
8631dea3b41SJohn Johansen 
8641dea3b41SJohn Johansen 	return t;
8651dea3b41SJohn Johansen }
8661dea3b41SJohn Johansen 
8671dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf,
8681dea3b41SJohn Johansen 				       size_t size, loff_t *pos)
8691dea3b41SJohn Johansen {
8701dea3b41SJohn Johansen 	struct multi_transaction *t;
8711dea3b41SJohn Johansen 	ssize_t ret;
8721dea3b41SJohn Johansen 
8731dea3b41SJohn Johansen 	spin_lock(&multi_transaction_lock);
8741dea3b41SJohn Johansen 	t = get_multi_transaction(file->private_data);
8751dea3b41SJohn Johansen 	spin_unlock(&multi_transaction_lock);
8761dea3b41SJohn Johansen 	if (!t)
8771dea3b41SJohn Johansen 		return 0;
8781dea3b41SJohn Johansen 
8791dea3b41SJohn Johansen 	ret = simple_read_from_buffer(buf, size, pos, t->data, t->size);
8801dea3b41SJohn Johansen 	put_multi_transaction(t);
8811dea3b41SJohn Johansen 
8821dea3b41SJohn Johansen 	return ret;
8831dea3b41SJohn Johansen }
8841dea3b41SJohn Johansen 
8851dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file)
8861dea3b41SJohn Johansen {
8871dea3b41SJohn Johansen 	put_multi_transaction(file->private_data);
8881dea3b41SJohn Johansen 
8891dea3b41SJohn Johansen 	return 0;
8901dea3b41SJohn Johansen }
8911dea3b41SJohn Johansen 
892317d9a05SJohn Johansen #define QUERY_CMD_LABEL		"label\0"
893317d9a05SJohn Johansen #define QUERY_CMD_LABEL_LEN	6
8944f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE	"profile\0"
8954f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE_LEN	8
896317d9a05SJohn Johansen #define QUERY_CMD_LABELALL	"labelall\0"
897317d9a05SJohn Johansen #define QUERY_CMD_LABELALL_LEN	9
898e025be0fSWilliam Hua #define QUERY_CMD_DATA		"data\0"
899e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN	5
900e025be0fSWilliam Hua 
901e025be0fSWilliam Hua /**
902e025be0fSWilliam Hua  * aa_write_access - generic permissions and data query
903e025be0fSWilliam Hua  * @file: pointer to open apparmorfs/access file
904e025be0fSWilliam Hua  * @ubuf: user buffer containing the complete query string (NOT NULL)
905e025be0fSWilliam Hua  * @count: size of ubuf
906e025be0fSWilliam Hua  * @ppos: position in the file (MUST BE ZERO)
907e025be0fSWilliam Hua  *
908e025be0fSWilliam Hua  * Allows for one permissions or data query per open(), write(), and read()
909e025be0fSWilliam Hua  * sequence. The only queries currently supported are label-based queries for
910e025be0fSWilliam Hua  * permissions or data.
911e025be0fSWilliam Hua  *
912e025be0fSWilliam Hua  * For permissions queries, ubuf must begin with "label\0", followed by the
913e025be0fSWilliam Hua  * profile query specific format described in the query_label() function
914e025be0fSWilliam Hua  * documentation.
915e025be0fSWilliam Hua  *
916e025be0fSWilliam Hua  * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
917e025be0fSWilliam Hua  * <LABEL> is the name of the security confinement context and <KEY> is the
918e025be0fSWilliam Hua  * name of the data to retrieve.
919e025be0fSWilliam Hua  *
920e025be0fSWilliam Hua  * Returns: number of bytes written or -errno on failure
921e025be0fSWilliam Hua  */
922e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
923e025be0fSWilliam Hua 			       size_t count, loff_t *ppos)
924e025be0fSWilliam Hua {
9251dea3b41SJohn Johansen 	struct multi_transaction *t;
926e025be0fSWilliam Hua 	ssize_t len;
927e025be0fSWilliam Hua 
928e025be0fSWilliam Hua 	if (*ppos)
929e025be0fSWilliam Hua 		return -ESPIPE;
930e025be0fSWilliam Hua 
9311dea3b41SJohn Johansen 	t = multi_transaction_new(file, ubuf, count);
9321dea3b41SJohn Johansen 	if (IS_ERR(t))
9331dea3b41SJohn Johansen 		return PTR_ERR(t);
934e025be0fSWilliam Hua 
9354f3b3f2dSJohn Johansen 	if (count > QUERY_CMD_PROFILE_LEN &&
9364f3b3f2dSJohn Johansen 	    !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) {
9374f3b3f2dSJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
9384f3b3f2dSJohn Johansen 				  t->data + QUERY_CMD_PROFILE_LEN,
9394f3b3f2dSJohn Johansen 				  count - QUERY_CMD_PROFILE_LEN, true);
940317d9a05SJohn Johansen 	} else if (count > QUERY_CMD_LABEL_LEN &&
941317d9a05SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) {
942317d9a05SJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
943317d9a05SJohn Johansen 				  t->data + QUERY_CMD_LABEL_LEN,
944317d9a05SJohn Johansen 				  count - QUERY_CMD_LABEL_LEN, true);
945317d9a05SJohn Johansen 	} else if (count > QUERY_CMD_LABELALL_LEN &&
946317d9a05SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_LABELALL,
947317d9a05SJohn Johansen 			   QUERY_CMD_LABELALL_LEN)) {
948317d9a05SJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
949317d9a05SJohn Johansen 				  t->data + QUERY_CMD_LABELALL_LEN,
950317d9a05SJohn Johansen 				  count - QUERY_CMD_LABELALL_LEN, false);
9514f3b3f2dSJohn Johansen 	} else if (count > QUERY_CMD_DATA_LEN &&
9521dea3b41SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
9531dea3b41SJohn Johansen 		len = query_data(t->data, MULTI_TRANSACTION_LIMIT,
9541dea3b41SJohn Johansen 				 t->data + QUERY_CMD_DATA_LEN,
955e025be0fSWilliam Hua 				 count - QUERY_CMD_DATA_LEN);
956e025be0fSWilliam Hua 	} else
957e025be0fSWilliam Hua 		len = -EINVAL;
958e025be0fSWilliam Hua 
9591dea3b41SJohn Johansen 	if (len < 0) {
9601dea3b41SJohn Johansen 		put_multi_transaction(t);
961e025be0fSWilliam Hua 		return len;
9621dea3b41SJohn Johansen 	}
963e025be0fSWilliam Hua 
9641dea3b41SJohn Johansen 	multi_transaction_set(file, t, len);
965e025be0fSWilliam Hua 
966e025be0fSWilliam Hua 	return count;
967e025be0fSWilliam Hua }
968e025be0fSWilliam Hua 
969c97204baSJohn Johansen static const struct file_operations aa_sfs_access = {
970e025be0fSWilliam Hua 	.write		= aa_write_access,
9711dea3b41SJohn Johansen 	.read		= multi_transaction_read,
9721dea3b41SJohn Johansen 	.release	= multi_transaction_release,
973e025be0fSWilliam Hua 	.llseek		= generic_file_llseek,
974e025be0fSWilliam Hua };
975e025be0fSWilliam Hua 
976c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v)
977e74abcf3SKees Cook {
978c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file = seq->private;
979e74abcf3SKees Cook 
980e74abcf3SKees Cook 	if (!fs_file)
981e74abcf3SKees Cook 		return 0;
982e74abcf3SKees Cook 
983e74abcf3SKees Cook 	switch (fs_file->v_type) {
984c97204baSJohn Johansen 	case AA_SFS_TYPE_BOOLEAN:
985e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
986e74abcf3SKees Cook 		break;
987c97204baSJohn Johansen 	case AA_SFS_TYPE_STRING:
988a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
989a9bf8e9fSKees Cook 		break;
990c97204baSJohn Johansen 	case AA_SFS_TYPE_U64:
991e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
992e74abcf3SKees Cook 		break;
993e74abcf3SKees Cook 	default:
994e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
995e74abcf3SKees Cook 		break;
996e74abcf3SKees Cook 	}
997e74abcf3SKees Cook 
998e74abcf3SKees Cook 	return 0;
999e74abcf3SKees Cook }
1000e74abcf3SKees Cook 
1001c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file)
1002e74abcf3SKees Cook {
1003c97204baSJohn Johansen 	return single_open(file, aa_sfs_seq_show, inode->i_private);
1004e74abcf3SKees Cook }
1005e74abcf3SKees Cook 
1006c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = {
1007e74abcf3SKees Cook 	.owner		= THIS_MODULE,
1008c97204baSJohn Johansen 	.open		= aa_sfs_seq_open,
1009e74abcf3SKees Cook 	.read		= seq_read,
1010e74abcf3SKees Cook 	.llseek		= seq_lseek,
1011e74abcf3SKees Cook 	.release	= single_release,
1012e74abcf3SKees Cook };
1013e74abcf3SKees Cook 
101452b97de3SJohn Johansen /*
101552b97de3SJohn Johansen  * profile based file operations
101652b97de3SJohn Johansen  *     policy/profiles/XXXX/profiles/ *
101752b97de3SJohn Johansen  */
101852b97de3SJohn Johansen 
101952b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME)						      \
102052b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
102152b97de3SJohn Johansen {									      \
102252b97de3SJohn Johansen 	return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show);    \
102352b97de3SJohn Johansen }									      \
102452b97de3SJohn Johansen 									      \
102552b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = {	      \
102652b97de3SJohn Johansen 	.owner		= THIS_MODULE,					      \
102752b97de3SJohn Johansen 	.open		= seq_profile_ ##NAME ##_open,			      \
102852b97de3SJohn Johansen 	.read		= seq_read,					      \
102952b97de3SJohn Johansen 	.llseek		= seq_lseek,					      \
103052b97de3SJohn Johansen 	.release	= seq_profile_release,				      \
103152b97de3SJohn Johansen }									      \
103252b97de3SJohn Johansen 
103352b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file,
10340d259f04SJohn Johansen 			    int (*show)(struct seq_file *, void *))
10350d259f04SJohn Johansen {
10368399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
10378399588aSJohn Johansen 	int error = single_open(file, show, proxy);
103863e2b423SJohn Johansen 
10390d259f04SJohn Johansen 	if (error) {
10400d259f04SJohn Johansen 		file->private_data = NULL;
10418399588aSJohn Johansen 		aa_put_proxy(proxy);
10420d259f04SJohn Johansen 	}
10430d259f04SJohn Johansen 
10440d259f04SJohn Johansen 	return error;
10450d259f04SJohn Johansen }
10460d259f04SJohn Johansen 
104752b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file)
10480d259f04SJohn Johansen {
10490d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
10500d259f04SJohn Johansen 	if (seq)
10518399588aSJohn Johansen 		aa_put_proxy(seq->private);
10520d259f04SJohn Johansen 	return single_release(inode, file);
10530d259f04SJohn Johansen }
10540d259f04SJohn Johansen 
105552b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v)
10560d259f04SJohn Johansen {
10578399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1058637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1059637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
10600d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
1061637f688dSJohn Johansen 	aa_put_label(label);
10620d259f04SJohn Johansen 
10630d259f04SJohn Johansen 	return 0;
10640d259f04SJohn Johansen }
10650d259f04SJohn Johansen 
106652b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v)
10670d259f04SJohn Johansen {
10688399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1069637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1070637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
10710d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
1072637f688dSJohn Johansen 	aa_put_label(label);
10730d259f04SJohn Johansen 
10740d259f04SJohn Johansen 	return 0;
10750d259f04SJohn Johansen }
10760d259f04SJohn Johansen 
107752b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v)
1078556d0be7SJohn Johansen {
10798399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1080637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1081637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
1082556d0be7SJohn Johansen 	if (profile->attach)
1083556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
1084556d0be7SJohn Johansen 	else if (profile->xmatch)
1085556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
1086556d0be7SJohn Johansen 	else
1087556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
1088637f688dSJohn Johansen 	aa_put_label(label);
1089556d0be7SJohn Johansen 
1090556d0be7SJohn Johansen 	return 0;
1091556d0be7SJohn Johansen }
1092556d0be7SJohn Johansen 
109352b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v)
1094f8eb8a13SJohn Johansen {
10958399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1096637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1097637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
1098f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
1099f8eb8a13SJohn Johansen 
1100f8eb8a13SJohn Johansen 	if (profile->hash) {
1101f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
1102f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
110347dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
1104f8eb8a13SJohn Johansen 	}
1105637f688dSJohn Johansen 	aa_put_label(label);
1106f8eb8a13SJohn Johansen 
1107f8eb8a13SJohn Johansen 	return 0;
1108f8eb8a13SJohn Johansen }
1109f8eb8a13SJohn Johansen 
111052b97de3SJohn Johansen SEQ_PROFILE_FOPS(name);
111152b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode);
111252b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach);
111352b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash);
1114f8eb8a13SJohn Johansen 
111564c86970SJohn Johansen /*
111664c86970SJohn Johansen  * namespace based files
111764c86970SJohn Johansen  *     several root files and
111864c86970SJohn Johansen  *     policy/ *
111964c86970SJohn Johansen  */
1120f8eb8a13SJohn Johansen 
112164c86970SJohn Johansen #define SEQ_NS_FOPS(NAME)						      \
112264c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file)     \
112364c86970SJohn Johansen {									      \
112464c86970SJohn Johansen 	return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private);   \
112564c86970SJohn Johansen }									      \
112664c86970SJohn Johansen 									      \
112764c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = {	      \
112864c86970SJohn Johansen 	.owner		= THIS_MODULE,					      \
112964c86970SJohn Johansen 	.open		= seq_ns_ ##NAME ##_open,			      \
113064c86970SJohn Johansen 	.read		= seq_read,					      \
113164c86970SJohn Johansen 	.llseek		= seq_lseek,					      \
113264c86970SJohn Johansen 	.release	= single_release,				      \
113364c86970SJohn Johansen }									      \
11343e3e5695SJohn Johansen 
113540cde7fcSJohn Johansen static int seq_ns_stacked_show(struct seq_file *seq, void *v)
113640cde7fcSJohn Johansen {
113740cde7fcSJohn Johansen 	struct aa_label *label;
113840cde7fcSJohn Johansen 
113940cde7fcSJohn Johansen 	label = begin_current_label_crit_section();
114040cde7fcSJohn Johansen 	seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no");
114140cde7fcSJohn Johansen 	end_current_label_crit_section(label);
114240cde7fcSJohn Johansen 
114340cde7fcSJohn Johansen 	return 0;
114440cde7fcSJohn Johansen }
114540cde7fcSJohn Johansen 
114640cde7fcSJohn Johansen static int seq_ns_nsstacked_show(struct seq_file *seq, void *v)
114740cde7fcSJohn Johansen {
114840cde7fcSJohn Johansen 	struct aa_label *label;
114940cde7fcSJohn Johansen 	struct aa_profile *profile;
115040cde7fcSJohn Johansen 	struct label_it it;
115140cde7fcSJohn Johansen 	int count = 1;
115240cde7fcSJohn Johansen 
115340cde7fcSJohn Johansen 	label = begin_current_label_crit_section();
115440cde7fcSJohn Johansen 
115540cde7fcSJohn Johansen 	if (label->size > 1) {
115640cde7fcSJohn Johansen 		label_for_each(it, label, profile)
115740cde7fcSJohn Johansen 			if (profile->ns != labels_ns(label)) {
115840cde7fcSJohn Johansen 				count++;
115940cde7fcSJohn Johansen 				break;
116040cde7fcSJohn Johansen 			}
116140cde7fcSJohn Johansen 	}
116240cde7fcSJohn Johansen 
116340cde7fcSJohn Johansen 	seq_printf(seq, "%s\n", count > 1 ? "yes" : "no");
116440cde7fcSJohn Johansen 	end_current_label_crit_section(label);
116540cde7fcSJohn Johansen 
116640cde7fcSJohn Johansen 	return 0;
116740cde7fcSJohn Johansen }
116840cde7fcSJohn Johansen 
116964c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v)
1170a71ada30SJohn Johansen {
1171637f688dSJohn Johansen 	struct aa_label *label;
1172a71ada30SJohn Johansen 
1173637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1174637f688dSJohn Johansen 	seq_printf(seq, "%d\n", labels_ns(label)->level);
1175637f688dSJohn Johansen 	end_current_label_crit_section(label);
1176a71ada30SJohn Johansen 
1177a71ada30SJohn Johansen 	return 0;
1178a71ada30SJohn Johansen }
1179a71ada30SJohn Johansen 
118064c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v)
11813e3e5695SJohn Johansen {
1182637f688dSJohn Johansen 	struct aa_label *label = begin_current_label_crit_section();
1183040d9e2bSJohn Johansen 	seq_printf(seq, "%s\n", labels_ns(label)->base.name);
1184637f688dSJohn Johansen 	end_current_label_crit_section(label);
11853e3e5695SJohn Johansen 
11863e3e5695SJohn Johansen 	return 0;
11873e3e5695SJohn Johansen }
11883e3e5695SJohn Johansen 
118940cde7fcSJohn Johansen SEQ_NS_FOPS(stacked);
119040cde7fcSJohn Johansen SEQ_NS_FOPS(nsstacked);
119164c86970SJohn Johansen SEQ_NS_FOPS(level);
119264c86970SJohn Johansen SEQ_NS_FOPS(name);
11933e3e5695SJohn Johansen 
11945d5182caSJohn Johansen 
11955d5182caSJohn Johansen /* policy/raw_data/ * file ops */
11965d5182caSJohn Johansen 
11975d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME)						      \
11985d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
11995d5182caSJohn Johansen {									      \
12005d5182caSJohn Johansen 	return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show);    \
12015d5182caSJohn Johansen }									      \
12025d5182caSJohn Johansen 									      \
12035d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = {	      \
12045d5182caSJohn Johansen 	.owner		= THIS_MODULE,					      \
12055d5182caSJohn Johansen 	.open		= seq_rawdata_ ##NAME ##_open,			      \
12065d5182caSJohn Johansen 	.read		= seq_read,					      \
12075d5182caSJohn Johansen 	.llseek		= seq_lseek,					      \
12085d5182caSJohn Johansen 	.release	= seq_rawdata_release,				      \
12095d5182caSJohn Johansen }									      \
12105d5182caSJohn Johansen 
12115d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file,
12125d5182caSJohn Johansen 			    int (*show)(struct seq_file *, void *))
12135ac8c355SJohn Johansen {
12145d5182caSJohn Johansen 	struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
12155d5182caSJohn Johansen 	int error;
12165d5182caSJohn Johansen 
12175d5182caSJohn Johansen 	if (!data)
12185d5182caSJohn Johansen 		/* lost race this ent is being reaped */
12195d5182caSJohn Johansen 		return -ENOENT;
12205d5182caSJohn Johansen 
12215d5182caSJohn Johansen 	error = single_open(file, show, data);
12225d5182caSJohn Johansen 	if (error) {
12235d5182caSJohn Johansen 		AA_BUG(file->private_data &&
12245d5182caSJohn Johansen 		       ((struct seq_file *)file->private_data)->private);
12255d5182caSJohn Johansen 		aa_put_loaddata(data);
12265d5182caSJohn Johansen 	}
12275d5182caSJohn Johansen 
12285d5182caSJohn Johansen 	return error;
12295d5182caSJohn Johansen }
12305d5182caSJohn Johansen 
12315d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file)
12325d5182caSJohn Johansen {
12335d5182caSJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
12345d5182caSJohn Johansen 
12355d5182caSJohn Johansen 	if (seq)
12365d5182caSJohn Johansen 		aa_put_loaddata(seq->private);
12375d5182caSJohn Johansen 
12385d5182caSJohn Johansen 	return single_release(inode, file);
12395d5182caSJohn Johansen }
12405d5182caSJohn Johansen 
12415d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
12425d5182caSJohn Johansen {
12435d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12445d5182caSJohn Johansen 
12455d5182caSJohn Johansen 	seq_printf(seq, "v%d\n", data->abi);
12465ac8c355SJohn Johansen 
12475ac8c355SJohn Johansen 	return 0;
12485ac8c355SJohn Johansen }
12495ac8c355SJohn Johansen 
12505d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
12515ac8c355SJohn Johansen {
12525d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12535ac8c355SJohn Johansen 
12545d5182caSJohn Johansen 	seq_printf(seq, "%ld\n", data->revision);
12555ac8c355SJohn Johansen 
12565ac8c355SJohn Johansen 	return 0;
12575ac8c355SJohn Johansen }
12585ac8c355SJohn Johansen 
12595d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
12605ac8c355SJohn Johansen {
12615d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12625ac8c355SJohn Johansen 	unsigned int i, size = aa_hash_size();
12635ac8c355SJohn Johansen 
12645d5182caSJohn Johansen 	if (data->hash) {
12655ac8c355SJohn Johansen 		for (i = 0; i < size; i++)
12665d5182caSJohn Johansen 			seq_printf(seq, "%.2x", data->hash[i]);
126747dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
12685ac8c355SJohn Johansen 	}
12695ac8c355SJohn Johansen 
12705ac8c355SJohn Johansen 	return 0;
12715ac8c355SJohn Johansen }
12725ac8c355SJohn Johansen 
12735d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi);
12745d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision);
12755d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash);
12765ac8c355SJohn Johansen 
12775ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
12785ac8c355SJohn Johansen 			    loff_t *ppos)
12795ac8c355SJohn Johansen {
12805ac8c355SJohn Johansen 	struct aa_loaddata *rawdata = file->private_data;
12815ac8c355SJohn Johansen 
12825ac8c355SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, rawdata->data,
12835ac8c355SJohn Johansen 				       rawdata->size);
12845ac8c355SJohn Johansen }
12855ac8c355SJohn Johansen 
12865d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file)
12875ac8c355SJohn Johansen {
12885d5182caSJohn Johansen 	aa_put_loaddata(file->private_data);
12895ac8c355SJohn Johansen 
12905ac8c355SJohn Johansen 	return 0;
12915ac8c355SJohn Johansen }
12925ac8c355SJohn Johansen 
12935d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file)
12945d5182caSJohn Johansen {
12955d5182caSJohn Johansen 	if (!policy_view_capable(NULL))
12965d5182caSJohn Johansen 		return -EACCES;
12975d5182caSJohn Johansen 	file->private_data = __aa_get_loaddata(inode->i_private);
12985d5182caSJohn Johansen 	if (!file->private_data)
12995d5182caSJohn Johansen 		/* lost race: this entry is being reaped */
13005d5182caSJohn Johansen 		return -ENOENT;
13015d5182caSJohn Johansen 
13025d5182caSJohn Johansen 	return 0;
13035d5182caSJohn Johansen }
13045d5182caSJohn Johansen 
13055d5182caSJohn Johansen static const struct file_operations rawdata_fops = {
13065ac8c355SJohn Johansen 	.open = rawdata_open,
13075ac8c355SJohn Johansen 	.read = rawdata_read,
13085ac8c355SJohn Johansen 	.llseek = generic_file_llseek,
13095ac8c355SJohn Johansen 	.release = rawdata_release,
13105ac8c355SJohn Johansen };
13115ac8c355SJohn Johansen 
13125d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata)
13135d5182caSJohn Johansen {
13145d5182caSJohn Johansen 	int i;
13155d5182caSJohn Johansen 
13165d5182caSJohn Johansen 	for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
13175d5182caSJohn Johansen 		if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
13185d5182caSJohn Johansen 			/* no refcounts on i_private */
1319c961ee5fSJohn Johansen 			aafs_remove(rawdata->dents[i]);
13205d5182caSJohn Johansen 			rawdata->dents[i] = NULL;
13215d5182caSJohn Johansen 		}
13225d5182caSJohn Johansen 	}
13235d5182caSJohn Johansen }
13245d5182caSJohn Johansen 
13255d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
13265d5182caSJohn Johansen {
13275d5182caSJohn Johansen 	AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
13285d5182caSJohn Johansen 
13295d5182caSJohn Johansen 	if (rawdata->ns) {
13305d5182caSJohn Johansen 		remove_rawdata_dents(rawdata);
13315d5182caSJohn Johansen 		list_del_init(&rawdata->list);
13325d5182caSJohn Johansen 		aa_put_ns(rawdata->ns);
13335d5182caSJohn Johansen 		rawdata->ns = NULL;
13345d5182caSJohn Johansen 	}
13355d5182caSJohn Johansen }
13365d5182caSJohn Johansen 
13375d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
13385d5182caSJohn Johansen {
13395d5182caSJohn Johansen 	struct dentry *dent, *dir;
13405d5182caSJohn Johansen 
13415d5182caSJohn Johansen 	AA_BUG(!ns);
13425d5182caSJohn Johansen 	AA_BUG(!rawdata);
13435d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
13445d5182caSJohn Johansen 	AA_BUG(!ns_subdata_dir(ns));
13455d5182caSJohn Johansen 
13465d5182caSJohn Johansen 	/*
13475d5182caSJohn Johansen 	 * just use ns revision dir was originally created at. This is
13485d5182caSJohn Johansen 	 * under ns->lock and if load is successful revision will be
13495d5182caSJohn Johansen 	 * bumped and is guaranteed to be unique
13505d5182caSJohn Johansen 	 */
13515d5182caSJohn Johansen 	rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
13525d5182caSJohn Johansen 	if (!rawdata->name)
13535d5182caSJohn Johansen 		return -ENOMEM;
13545d5182caSJohn Johansen 
1355c961ee5fSJohn Johansen 	dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
13565d5182caSJohn Johansen 	if (IS_ERR(dir))
13575d5182caSJohn Johansen 		/* ->name freed when rawdata freed */
13585d5182caSJohn Johansen 		return PTR_ERR(dir);
13595d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DIR] = dir;
13605d5182caSJohn Johansen 
1361c961ee5fSJohn Johansen 	dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
13625d5182caSJohn Johansen 				      &seq_rawdata_abi_fops);
13635d5182caSJohn Johansen 	if (IS_ERR(dent))
13645d5182caSJohn Johansen 		goto fail;
13655d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_ABI] = dent;
13665d5182caSJohn Johansen 
1367c961ee5fSJohn Johansen 	dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
13685d5182caSJohn Johansen 				      &seq_rawdata_revision_fops);
13695d5182caSJohn Johansen 	if (IS_ERR(dent))
13705d5182caSJohn Johansen 		goto fail;
13715d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
13725d5182caSJohn Johansen 
13735d5182caSJohn Johansen 	if (aa_g_hash_policy) {
1374c961ee5fSJohn Johansen 		dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
13755d5182caSJohn Johansen 					      rawdata, &seq_rawdata_hash_fops);
13765d5182caSJohn Johansen 		if (IS_ERR(dent))
13775d5182caSJohn Johansen 			goto fail;
13785d5182caSJohn Johansen 		rawdata->dents[AAFS_LOADDATA_HASH] = dent;
13795d5182caSJohn Johansen 	}
13805d5182caSJohn Johansen 
1381c961ee5fSJohn Johansen 	dent = aafs_create_file("raw_data", S_IFREG | 0444,
13825d5182caSJohn Johansen 				      dir, rawdata, &rawdata_fops);
13835d5182caSJohn Johansen 	if (IS_ERR(dent))
13845d5182caSJohn Johansen 		goto fail;
13855d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DATA] = dent;
13865d5182caSJohn Johansen 	d_inode(dent)->i_size = rawdata->size;
13875d5182caSJohn Johansen 
13885d5182caSJohn Johansen 	rawdata->ns = aa_get_ns(ns);
13895d5182caSJohn Johansen 	list_add(&rawdata->list, &ns->rawdata_list);
13905d5182caSJohn Johansen 	/* no refcount on inode rawdata */
13915d5182caSJohn Johansen 
13925d5182caSJohn Johansen 	return 0;
13935d5182caSJohn Johansen 
13945d5182caSJohn Johansen fail:
13955d5182caSJohn Johansen 	remove_rawdata_dents(rawdata);
13965d5182caSJohn Johansen 
13975d5182caSJohn Johansen 	return PTR_ERR(dent);
13985d5182caSJohn Johansen }
13995d5182caSJohn Johansen 
14000d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
1401c97204baSJohn Johansen 
1402c97204baSJohn Johansen /**
1403c97204baSJohn Johansen  *
1404c97204baSJohn Johansen  * Requires: @profile->ns->lock held
1405c97204baSJohn Johansen  */
1406c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile)
14070d259f04SJohn Johansen {
14080d259f04SJohn Johansen 	struct aa_profile *child;
14090d259f04SJohn Johansen 	int i;
14100d259f04SJohn Johansen 
14110d259f04SJohn Johansen 	if (!profile)
14120d259f04SJohn Johansen 		return;
14130d259f04SJohn Johansen 
14140d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
1415c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
14160d259f04SJohn Johansen 
14170d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
14188399588aSJohn Johansen 		struct aa_proxy *proxy;
14190d259f04SJohn Johansen 		if (!profile->dents[i])
14200d259f04SJohn Johansen 			continue;
14210d259f04SJohn Johansen 
14228399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
1423c961ee5fSJohn Johansen 		aafs_remove(profile->dents[i]);
14248399588aSJohn Johansen 		aa_put_proxy(proxy);
14250d259f04SJohn Johansen 		profile->dents[i] = NULL;
14260d259f04SJohn Johansen 	}
14270d259f04SJohn Johansen }
14280d259f04SJohn Johansen 
1429c97204baSJohn Johansen /**
1430c97204baSJohn Johansen  *
1431c97204baSJohn Johansen  * Requires: @old->ns->lock held
1432c97204baSJohn Johansen  */
1433c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old,
14340d259f04SJohn Johansen 				  struct aa_profile *new)
14350d259f04SJohn Johansen {
14360d259f04SJohn Johansen 	int i;
14370d259f04SJohn Johansen 
1438cbf2d0e1SJohn Johansen 	AA_BUG(!old);
1439cbf2d0e1SJohn Johansen 	AA_BUG(!new);
1440cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock));
1441cbf2d0e1SJohn Johansen 
14420d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
14430d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
1444d671e890SJohn Johansen 		if (new->dents[i])
1445078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
14460d259f04SJohn Johansen 		old->dents[i] = NULL;
14470d259f04SJohn Johansen 	}
14480d259f04SJohn Johansen }
14490d259f04SJohn Johansen 
14500d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
14510d259f04SJohn Johansen 					  struct aa_profile *profile,
14520d259f04SJohn Johansen 					  const struct file_operations *fops)
14530d259f04SJohn Johansen {
1454637f688dSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
14550d259f04SJohn Johansen 	struct dentry *dent;
14560d259f04SJohn Johansen 
1457c961ee5fSJohn Johansen 	dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
14580d259f04SJohn Johansen 	if (IS_ERR(dent))
14598399588aSJohn Johansen 		aa_put_proxy(proxy);
14600d259f04SJohn Johansen 
14610d259f04SJohn Johansen 	return dent;
14620d259f04SJohn Johansen }
14630d259f04SJohn Johansen 
14645d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile)
14655d5182caSJohn Johansen {
14665d5182caSJohn Johansen 	int depth = 0;
14675d5182caSJohn Johansen 
14685d5182caSJohn Johansen 	rcu_read_lock();
14695d5182caSJohn Johansen 	for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
14705d5182caSJohn Johansen 		depth++;
14715d5182caSJohn Johansen 	rcu_read_unlock();
14725d5182caSJohn Johansen 
14735d5182caSJohn Johansen 	return depth;
14745d5182caSJohn Johansen }
14755d5182caSJohn Johansen 
14761180b4c7SJohn Johansen static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
14775d5182caSJohn Johansen {
14781180b4c7SJohn Johansen 	char *buffer, *s;
14795d5182caSJohn Johansen 	int error;
14801180b4c7SJohn Johansen 	int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
14811180b4c7SJohn Johansen 
14821180b4c7SJohn Johansen 	s = buffer = kmalloc(size, GFP_KERNEL);
14831180b4c7SJohn Johansen 	if (!buffer)
14841180b4c7SJohn Johansen 		return ERR_PTR(-ENOMEM);
14855d5182caSJohn Johansen 
14865d5182caSJohn Johansen 	for (; depth > 0; depth--) {
14871180b4c7SJohn Johansen 		strcpy(s, "../../");
14881180b4c7SJohn Johansen 		s += 6;
14891180b4c7SJohn Johansen 		size -= 6;
14905d5182caSJohn Johansen 	}
14915d5182caSJohn Johansen 
14921180b4c7SJohn Johansen 	error = snprintf(s, size, "raw_data/%s/%s", dirname, fname);
1493588558ebSColin Ian King 	if (error >= size || error < 0) {
1494588558ebSColin Ian King 		kfree(buffer);
14951180b4c7SJohn Johansen 		return ERR_PTR(-ENAMETOOLONG);
14965d5182caSJohn Johansen 	}
14975d5182caSJohn Johansen 
14981180b4c7SJohn Johansen 	return buffer;
14995d5182caSJohn Johansen }
15005d5182caSJohn Johansen 
15011180b4c7SJohn Johansen static void rawdata_link_cb(void *arg)
15021180b4c7SJohn Johansen {
15031180b4c7SJohn Johansen 	kfree(arg);
15041180b4c7SJohn Johansen }
15051180b4c7SJohn Johansen 
15061180b4c7SJohn Johansen static const char *rawdata_get_link_base(struct dentry *dentry,
15071180b4c7SJohn Johansen 					 struct inode *inode,
15081180b4c7SJohn Johansen 					 struct delayed_call *done,
15091180b4c7SJohn Johansen 					 const char *name)
15101180b4c7SJohn Johansen {
15111180b4c7SJohn Johansen 	struct aa_proxy *proxy = inode->i_private;
15121180b4c7SJohn Johansen 	struct aa_label *label;
15131180b4c7SJohn Johansen 	struct aa_profile *profile;
15141180b4c7SJohn Johansen 	char *target;
15151180b4c7SJohn Johansen 	int depth;
15161180b4c7SJohn Johansen 
15171180b4c7SJohn Johansen 	if (!dentry)
15181180b4c7SJohn Johansen 		return ERR_PTR(-ECHILD);
15191180b4c7SJohn Johansen 
15201180b4c7SJohn Johansen 	label = aa_get_label_rcu(&proxy->label);
15211180b4c7SJohn Johansen 	profile = labels_profile(label);
15221180b4c7SJohn Johansen 	depth = profile_depth(profile);
15231180b4c7SJohn Johansen 	target = gen_symlink_name(depth, profile->rawdata->name, name);
15241180b4c7SJohn Johansen 	aa_put_label(label);
15251180b4c7SJohn Johansen 
15261180b4c7SJohn Johansen 	if (IS_ERR(target))
15271180b4c7SJohn Johansen 		return target;
15281180b4c7SJohn Johansen 
15291180b4c7SJohn Johansen 	set_delayed_call(done, rawdata_link_cb, target);
15301180b4c7SJohn Johansen 
15311180b4c7SJohn Johansen 	return target;
15321180b4c7SJohn Johansen }
15331180b4c7SJohn Johansen 
15341180b4c7SJohn Johansen static const char *rawdata_get_link_sha1(struct dentry *dentry,
15351180b4c7SJohn Johansen 					 struct inode *inode,
15361180b4c7SJohn Johansen 					 struct delayed_call *done)
15371180b4c7SJohn Johansen {
15381180b4c7SJohn Johansen 	return rawdata_get_link_base(dentry, inode, done, "sha1");
15391180b4c7SJohn Johansen }
15401180b4c7SJohn Johansen 
15411180b4c7SJohn Johansen static const char *rawdata_get_link_abi(struct dentry *dentry,
15421180b4c7SJohn Johansen 					struct inode *inode,
15431180b4c7SJohn Johansen 					struct delayed_call *done)
15441180b4c7SJohn Johansen {
15451180b4c7SJohn Johansen 	return rawdata_get_link_base(dentry, inode, done, "abi");
15461180b4c7SJohn Johansen }
15471180b4c7SJohn Johansen 
15481180b4c7SJohn Johansen static const char *rawdata_get_link_data(struct dentry *dentry,
15491180b4c7SJohn Johansen 					 struct inode *inode,
15501180b4c7SJohn Johansen 					 struct delayed_call *done)
15511180b4c7SJohn Johansen {
15521180b4c7SJohn Johansen 	return rawdata_get_link_base(dentry, inode, done, "raw_data");
15531180b4c7SJohn Johansen }
15541180b4c7SJohn Johansen 
15551180b4c7SJohn Johansen static const struct inode_operations rawdata_link_sha1_iops = {
15561180b4c7SJohn Johansen 	.get_link	= rawdata_get_link_sha1,
15571180b4c7SJohn Johansen };
15581180b4c7SJohn Johansen 
15591180b4c7SJohn Johansen static const struct inode_operations rawdata_link_abi_iops = {
15601180b4c7SJohn Johansen 	.get_link	= rawdata_get_link_abi,
15611180b4c7SJohn Johansen };
15621180b4c7SJohn Johansen static const struct inode_operations rawdata_link_data_iops = {
15631180b4c7SJohn Johansen 	.get_link	= rawdata_get_link_data,
15641180b4c7SJohn Johansen };
15651180b4c7SJohn Johansen 
15661180b4c7SJohn Johansen 
15675d5182caSJohn Johansen /*
15685d5182caSJohn Johansen  * Requires: @profile->ns->lock held
15695d5182caSJohn Johansen  */
1570c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
15710d259f04SJohn Johansen {
15720d259f04SJohn Johansen 	struct aa_profile *child;
15730d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
15740d259f04SJohn Johansen 	int error;
15750d259f04SJohn Johansen 
1576cbf2d0e1SJohn Johansen 	AA_BUG(!profile);
1577cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock));
1578cbf2d0e1SJohn Johansen 
15790d259f04SJohn Johansen 	if (!parent) {
15800d259f04SJohn Johansen 		struct aa_profile *p;
15810d259f04SJohn Johansen 		p = aa_deref_parent(profile);
15820d259f04SJohn Johansen 		dent = prof_dir(p);
15830d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
1584c961ee5fSJohn Johansen 		dent = aafs_create_dir("profiles", dent);
15850d259f04SJohn Johansen 		if (IS_ERR(dent))
15860d259f04SJohn Johansen 			goto fail;
15870d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
15880d259f04SJohn Johansen 	}
15890d259f04SJohn Johansen 
15900d259f04SJohn Johansen 	if (!profile->dirname) {
15910d259f04SJohn Johansen 		int len, id_len;
15920d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
15930d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
15940d259f04SJohn Johansen 
15950d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
1596ffac1de6SDan Carpenter 		if (!profile->dirname) {
1597ffac1de6SDan Carpenter 			error = -ENOMEM;
1598ffac1de6SDan Carpenter 			goto fail2;
1599ffac1de6SDan Carpenter 		}
16000d259f04SJohn Johansen 
16010d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
16020d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
16030d259f04SJohn Johansen 	}
16040d259f04SJohn Johansen 
1605c961ee5fSJohn Johansen 	dent = aafs_create_dir(profile->dirname, parent);
16060d259f04SJohn Johansen 	if (IS_ERR(dent))
16070d259f04SJohn Johansen 		goto fail;
16080d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
16090d259f04SJohn Johansen 
161052b97de3SJohn Johansen 	dent = create_profile_file(dir, "name", profile,
161152b97de3SJohn Johansen 				   &seq_profile_name_fops);
16120d259f04SJohn Johansen 	if (IS_ERR(dent))
16130d259f04SJohn Johansen 		goto fail;
16140d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
16150d259f04SJohn Johansen 
161652b97de3SJohn Johansen 	dent = create_profile_file(dir, "mode", profile,
161752b97de3SJohn Johansen 				   &seq_profile_mode_fops);
16180d259f04SJohn Johansen 	if (IS_ERR(dent))
16190d259f04SJohn Johansen 		goto fail;
16200d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
16210d259f04SJohn Johansen 
1622556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
162352b97de3SJohn Johansen 				   &seq_profile_attach_fops);
1624556d0be7SJohn Johansen 	if (IS_ERR(dent))
1625556d0be7SJohn Johansen 		goto fail;
1626556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
1627556d0be7SJohn Johansen 
1628f8eb8a13SJohn Johansen 	if (profile->hash) {
1629f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
163052b97de3SJohn Johansen 					   &seq_profile_hash_fops);
1631f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
1632f8eb8a13SJohn Johansen 			goto fail;
1633f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
1634f8eb8a13SJohn Johansen 	}
1635f8eb8a13SJohn Johansen 
16365ac8c355SJohn Johansen 	if (profile->rawdata) {
16371180b4c7SJohn Johansen 		dent = aafs_create_symlink("raw_sha1", dir, NULL,
16381180b4c7SJohn Johansen 					   profile->label.proxy,
16391180b4c7SJohn Johansen 					   &rawdata_link_sha1_iops);
16405ac8c355SJohn Johansen 		if (IS_ERR(dent))
16415ac8c355SJohn Johansen 			goto fail;
16421180b4c7SJohn Johansen 		aa_get_proxy(profile->label.proxy);
16435ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
16445ac8c355SJohn Johansen 
16451180b4c7SJohn Johansen 		dent = aafs_create_symlink("raw_abi", dir, NULL,
16461180b4c7SJohn Johansen 					   profile->label.proxy,
16471180b4c7SJohn Johansen 					   &rawdata_link_abi_iops);
16485ac8c355SJohn Johansen 		if (IS_ERR(dent))
16495ac8c355SJohn Johansen 			goto fail;
16501180b4c7SJohn Johansen 		aa_get_proxy(profile->label.proxy);
16515ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
16525ac8c355SJohn Johansen 
16531180b4c7SJohn Johansen 		dent = aafs_create_symlink("raw_data", dir, NULL,
16541180b4c7SJohn Johansen 					   profile->label.proxy,
16551180b4c7SJohn Johansen 					   &rawdata_link_data_iops);
16565ac8c355SJohn Johansen 		if (IS_ERR(dent))
16575ac8c355SJohn Johansen 			goto fail;
16581180b4c7SJohn Johansen 		aa_get_proxy(profile->label.proxy);
16595ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
16605ac8c355SJohn Johansen 	}
16615ac8c355SJohn Johansen 
16620d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
1663c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, prof_child_dir(profile));
16640d259f04SJohn Johansen 		if (error)
16650d259f04SJohn Johansen 			goto fail2;
16660d259f04SJohn Johansen 	}
16670d259f04SJohn Johansen 
16680d259f04SJohn Johansen 	return 0;
16690d259f04SJohn Johansen 
16700d259f04SJohn Johansen fail:
16710d259f04SJohn Johansen 	error = PTR_ERR(dent);
16720d259f04SJohn Johansen 
16730d259f04SJohn Johansen fail2:
1674c97204baSJohn Johansen 	__aafs_profile_rmdir(profile);
16750d259f04SJohn Johansen 
16760d259f04SJohn Johansen 	return error;
16770d259f04SJohn Johansen }
16780d259f04SJohn Johansen 
16794ae47f33SJohn Johansen static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode)
16804ae47f33SJohn Johansen {
16814ae47f33SJohn Johansen 	struct aa_ns *ns, *parent;
16824ae47f33SJohn Johansen 	/* TODO: improve permission check */
1683637f688dSJohn Johansen 	struct aa_label *label;
1684637f688dSJohn Johansen 	int error;
1685637f688dSJohn Johansen 
1686637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1687637f688dSJohn Johansen 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1688637f688dSJohn Johansen 	end_current_label_crit_section(label);
16894ae47f33SJohn Johansen 	if (error)
16904ae47f33SJohn Johansen 		return error;
16914ae47f33SJohn Johansen 
16924ae47f33SJohn Johansen 	parent = aa_get_ns(dir->i_private);
16934ae47f33SJohn Johansen 	AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
16944ae47f33SJohn Johansen 
16954ae47f33SJohn Johansen 	/* we have to unlock and then relock to get locking order right
16964ae47f33SJohn Johansen 	 * for pin_fs
16974ae47f33SJohn Johansen 	 */
16984ae47f33SJohn Johansen 	inode_unlock(dir);
16994ae47f33SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
1700feb3c766SJohn Johansen 	mutex_lock_nested(&parent->lock, parent->level);
17014ae47f33SJohn Johansen 	inode_lock_nested(dir, I_MUTEX_PARENT);
17024ae47f33SJohn Johansen 	if (error)
17034ae47f33SJohn Johansen 		goto out;
17044ae47f33SJohn Johansen 
17054ae47f33SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR,  NULL,
17064ae47f33SJohn Johansen 				     NULL, NULL, NULL);
17074ae47f33SJohn Johansen 	if (error)
17084ae47f33SJohn Johansen 		goto out_pin;
17094ae47f33SJohn Johansen 
17104ae47f33SJohn Johansen 	ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name),
17114ae47f33SJohn Johansen 				    dentry);
17124ae47f33SJohn Johansen 	if (IS_ERR(ns)) {
17134ae47f33SJohn Johansen 		error = PTR_ERR(ns);
17144ae47f33SJohn Johansen 		ns = NULL;
17154ae47f33SJohn Johansen 	}
17164ae47f33SJohn Johansen 
17174ae47f33SJohn Johansen 	aa_put_ns(ns);		/* list ref remains */
17184ae47f33SJohn Johansen out_pin:
17194ae47f33SJohn Johansen 	if (error)
17204ae47f33SJohn Johansen 		simple_release_fs(&aafs_mnt, &aafs_count);
17214ae47f33SJohn Johansen out:
17224ae47f33SJohn Johansen 	mutex_unlock(&parent->lock);
17234ae47f33SJohn Johansen 	aa_put_ns(parent);
17244ae47f33SJohn Johansen 
17254ae47f33SJohn Johansen 	return error;
17264ae47f33SJohn Johansen }
17274ae47f33SJohn Johansen 
17284ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
17294ae47f33SJohn Johansen {
17304ae47f33SJohn Johansen 	struct aa_ns *ns, *parent;
17314ae47f33SJohn Johansen 	/* TODO: improve permission check */
1732637f688dSJohn Johansen 	struct aa_label *label;
1733637f688dSJohn Johansen 	int error;
1734637f688dSJohn Johansen 
1735637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1736637f688dSJohn Johansen 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1737637f688dSJohn Johansen 	end_current_label_crit_section(label);
17384ae47f33SJohn Johansen 	if (error)
17394ae47f33SJohn Johansen 		return error;
17404ae47f33SJohn Johansen 
17414ae47f33SJohn Johansen 	parent = aa_get_ns(dir->i_private);
17424ae47f33SJohn Johansen 	/* rmdir calls the generic securityfs functions to remove files
17434ae47f33SJohn Johansen 	 * from the apparmor dir. It is up to the apparmor ns locking
17444ae47f33SJohn Johansen 	 * to avoid races.
17454ae47f33SJohn Johansen 	 */
17464ae47f33SJohn Johansen 	inode_unlock(dir);
17474ae47f33SJohn Johansen 	inode_unlock(dentry->d_inode);
17484ae47f33SJohn Johansen 
1749feb3c766SJohn Johansen 	mutex_lock_nested(&parent->lock, parent->level);
17504ae47f33SJohn Johansen 	ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name,
17514ae47f33SJohn Johansen 				     dentry->d_name.len));
17524ae47f33SJohn Johansen 	if (!ns) {
17534ae47f33SJohn Johansen 		error = -ENOENT;
17544ae47f33SJohn Johansen 		goto out;
17554ae47f33SJohn Johansen 	}
17564ae47f33SJohn Johansen 	AA_BUG(ns_dir(ns) != dentry);
17574ae47f33SJohn Johansen 
17584ae47f33SJohn Johansen 	__aa_remove_ns(ns);
17594ae47f33SJohn Johansen 	aa_put_ns(ns);
17604ae47f33SJohn Johansen 
17614ae47f33SJohn Johansen out:
17624ae47f33SJohn Johansen 	mutex_unlock(&parent->lock);
17634ae47f33SJohn Johansen 	inode_lock_nested(dir, I_MUTEX_PARENT);
17644ae47f33SJohn Johansen 	inode_lock(dentry->d_inode);
17654ae47f33SJohn Johansen 	aa_put_ns(parent);
17664ae47f33SJohn Johansen 
17674ae47f33SJohn Johansen 	return error;
17684ae47f33SJohn Johansen }
17694ae47f33SJohn Johansen 
17704ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = {
17714ae47f33SJohn Johansen 	.lookup		= simple_lookup,
17724ae47f33SJohn Johansen 	.mkdir		= ns_mkdir_op,
17734ae47f33SJohn Johansen 	.rmdir		= ns_rmdir_op,
17744ae47f33SJohn Johansen };
17754ae47f33SJohn Johansen 
17765d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
17775d5182caSJohn Johansen {
17785d5182caSJohn Johansen 	struct aa_loaddata *ent, *tmp;
17795d5182caSJohn Johansen 
17805d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
17815d5182caSJohn Johansen 
17825d5182caSJohn Johansen 	list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
17835d5182caSJohn Johansen 		__aa_fs_remove_rawdata(ent);
17845d5182caSJohn Johansen }
17855d5182caSJohn Johansen 
1786c97204baSJohn Johansen /**
1787c97204baSJohn Johansen  *
1788c97204baSJohn Johansen  * Requires: @ns->lock held
1789c97204baSJohn Johansen  */
1790c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns)
17910d259f04SJohn Johansen {
179298849dffSJohn Johansen 	struct aa_ns *sub;
17930d259f04SJohn Johansen 	struct aa_profile *child;
17940d259f04SJohn Johansen 	int i;
17950d259f04SJohn Johansen 
17960d259f04SJohn Johansen 	if (!ns)
17970d259f04SJohn Johansen 		return;
1798cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
17990d259f04SJohn Johansen 
18000d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
1801c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
18020d259f04SJohn Johansen 
18030d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
1804feb3c766SJohn Johansen 		mutex_lock_nested(&sub->lock, sub->level);
1805c97204baSJohn Johansen 		__aafs_ns_rmdir(sub);
18060d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
18070d259f04SJohn Johansen 	}
18080d259f04SJohn Johansen 
18095d5182caSJohn Johansen 	__aa_fs_list_remove_rawdata(ns);
18105d5182caSJohn Johansen 
1811b7fd2c03SJohn Johansen 	if (ns_subns_dir(ns)) {
1812b7fd2c03SJohn Johansen 		sub = d_inode(ns_subns_dir(ns))->i_private;
1813b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1814b7fd2c03SJohn Johansen 	}
1815b7fd2c03SJohn Johansen 	if (ns_subload(ns)) {
1816b7fd2c03SJohn Johansen 		sub = d_inode(ns_subload(ns))->i_private;
1817b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1818b7fd2c03SJohn Johansen 	}
1819b7fd2c03SJohn Johansen 	if (ns_subreplace(ns)) {
1820b7fd2c03SJohn Johansen 		sub = d_inode(ns_subreplace(ns))->i_private;
1821b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1822b7fd2c03SJohn Johansen 	}
1823b7fd2c03SJohn Johansen 	if (ns_subremove(ns)) {
1824b7fd2c03SJohn Johansen 		sub = d_inode(ns_subremove(ns))->i_private;
1825b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1826b7fd2c03SJohn Johansen 	}
1827d9bf2c26SJohn Johansen 	if (ns_subrevision(ns)) {
1828d9bf2c26SJohn Johansen 		sub = d_inode(ns_subrevision(ns))->i_private;
1829d9bf2c26SJohn Johansen 		aa_put_ns(sub);
1830d9bf2c26SJohn Johansen 	}
1831b7fd2c03SJohn Johansen 
18320d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
1833c961ee5fSJohn Johansen 		aafs_remove(ns->dents[i]);
18340d259f04SJohn Johansen 		ns->dents[i] = NULL;
18350d259f04SJohn Johansen 	}
18360d259f04SJohn Johansen }
18370d259f04SJohn Johansen 
1838b7fd2c03SJohn Johansen /* assumes cleanup in caller */
1839c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
1840b7fd2c03SJohn Johansen {
1841b7fd2c03SJohn Johansen 	struct dentry *dent;
1842b7fd2c03SJohn Johansen 
1843b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1844b7fd2c03SJohn Johansen 	AA_BUG(!dir);
1845b7fd2c03SJohn Johansen 
1846c961ee5fSJohn Johansen 	dent = aafs_create_dir("profiles", dir);
1847b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1848b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1849b7fd2c03SJohn Johansen 	ns_subprofs_dir(ns) = dent;
1850b7fd2c03SJohn Johansen 
1851c961ee5fSJohn Johansen 	dent = aafs_create_dir("raw_data", dir);
1852b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1853b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1854b7fd2c03SJohn Johansen 	ns_subdata_dir(ns) = dent;
1855b7fd2c03SJohn Johansen 
1856d9bf2c26SJohn Johansen 	dent = aafs_create_file("revision", 0444, dir, ns,
1857d9bf2c26SJohn Johansen 				&aa_fs_ns_revision_fops);
1858d9bf2c26SJohn Johansen 	if (IS_ERR(dent))
1859d9bf2c26SJohn Johansen 		return PTR_ERR(dent);
1860d9bf2c26SJohn Johansen 	aa_get_ns(ns);
1861d9bf2c26SJohn Johansen 	ns_subrevision(ns) = dent;
1862d9bf2c26SJohn Johansen 
1863c961ee5fSJohn Johansen 	dent = aafs_create_file(".load", 0640, dir, ns,
1864b7fd2c03SJohn Johansen 				      &aa_fs_profile_load);
1865b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1866b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1867b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1868b7fd2c03SJohn Johansen 	ns_subload(ns) = dent;
1869b7fd2c03SJohn Johansen 
1870c961ee5fSJohn Johansen 	dent = aafs_create_file(".replace", 0640, dir, ns,
1871b7fd2c03SJohn Johansen 				      &aa_fs_profile_replace);
1872b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1873b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1874b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1875b7fd2c03SJohn Johansen 	ns_subreplace(ns) = dent;
1876b7fd2c03SJohn Johansen 
1877c961ee5fSJohn Johansen 	dent = aafs_create_file(".remove", 0640, dir, ns,
1878b7fd2c03SJohn Johansen 				      &aa_fs_profile_remove);
1879b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1880b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1881b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1882b7fd2c03SJohn Johansen 	ns_subremove(ns) = dent;
1883b7fd2c03SJohn Johansen 
18844ae47f33SJohn Johansen 	  /* use create_dentry so we can supply private data */
18854ae47f33SJohn Johansen 	dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL,
18864ae47f33SJohn Johansen 			   &ns_dir_inode_operations);
1887b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1888b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1889b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1890b7fd2c03SJohn Johansen 	ns_subns_dir(ns) = dent;
1891b7fd2c03SJohn Johansen 
1892b7fd2c03SJohn Johansen 	return 0;
1893b7fd2c03SJohn Johansen }
1894b7fd2c03SJohn Johansen 
1895c97204baSJohn Johansen /*
1896c97204baSJohn Johansen  * Requires: @ns->lock held
1897c97204baSJohn Johansen  */
189898407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
189998407f0aSJohn Johansen 		    struct dentry *dent)
19000d259f04SJohn Johansen {
190198849dffSJohn Johansen 	struct aa_ns *sub;
19020d259f04SJohn Johansen 	struct aa_profile *child;
190398407f0aSJohn Johansen 	struct dentry *dir;
19040d259f04SJohn Johansen 	int error;
19050d259f04SJohn Johansen 
1906b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1907b7fd2c03SJohn Johansen 	AA_BUG(!parent);
1908b7fd2c03SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
1909b7fd2c03SJohn Johansen 
19100d259f04SJohn Johansen 	if (!name)
19110d259f04SJohn Johansen 		name = ns->base.name;
19120d259f04SJohn Johansen 
1913c961ee5fSJohn Johansen 	if (!dent) {
1914b7fd2c03SJohn Johansen 		/* create ns dir if it doesn't already exist */
1915c961ee5fSJohn Johansen 		dent = aafs_create_dir(name, parent);
19160d259f04SJohn Johansen 		if (IS_ERR(dent))
19170d259f04SJohn Johansen 			goto fail;
1918c961ee5fSJohn Johansen 	} else
1919c961ee5fSJohn Johansen 		dget(dent);
19200d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
1921c97204baSJohn Johansen 	error = __aafs_ns_mkdir_entries(ns, dir);
1922b7fd2c03SJohn Johansen 	if (error)
1923b7fd2c03SJohn Johansen 		goto fail2;
19240d259f04SJohn Johansen 
1925b7fd2c03SJohn Johansen 	/* profiles */
19260d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
1927c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
19280d259f04SJohn Johansen 		if (error)
19290d259f04SJohn Johansen 			goto fail2;
19300d259f04SJohn Johansen 	}
19310d259f04SJohn Johansen 
1932b7fd2c03SJohn Johansen 	/* subnamespaces */
19330d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
1934feb3c766SJohn Johansen 		mutex_lock_nested(&sub->lock, sub->level);
193598407f0aSJohn Johansen 		error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
19360d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
19370d259f04SJohn Johansen 		if (error)
19380d259f04SJohn Johansen 			goto fail2;
19390d259f04SJohn Johansen 	}
19400d259f04SJohn Johansen 
19410d259f04SJohn Johansen 	return 0;
19420d259f04SJohn Johansen 
19430d259f04SJohn Johansen fail:
19440d259f04SJohn Johansen 	error = PTR_ERR(dent);
19450d259f04SJohn Johansen 
19460d259f04SJohn Johansen fail2:
1947c97204baSJohn Johansen 	__aafs_ns_rmdir(ns);
19480d259f04SJohn Johansen 
19490d259f04SJohn Johansen 	return error;
19500d259f04SJohn Johansen }
19510d259f04SJohn Johansen 
19520d259f04SJohn Johansen 
195329b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
195429b3822fSJohn Johansen 
195529b3822fSJohn Johansen /**
195698849dffSJohn Johansen  * __next_ns - find the next namespace to list
195729b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
195829b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
195929b3822fSJohn Johansen  *
196029b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
196129b3822fSJohn Johansen  * while switching current namespace.
196229b3822fSJohn Johansen  *
196329b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
196429b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
196529b3822fSJohn Johansen  * NOTE: will not unlock root->lock
196629b3822fSJohn Johansen  */
196798849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
196829b3822fSJohn Johansen {
196998849dffSJohn Johansen 	struct aa_ns *parent, *next;
197029b3822fSJohn Johansen 
1971cbf2d0e1SJohn Johansen 	AA_BUG(!root);
1972cbf2d0e1SJohn Johansen 	AA_BUG(!ns);
1973cbf2d0e1SJohn Johansen 	AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock));
1974cbf2d0e1SJohn Johansen 
197529b3822fSJohn Johansen 	/* is next namespace a child */
197629b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
197729b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
1978feb3c766SJohn Johansen 		mutex_lock_nested(&next->lock, next->level);
197929b3822fSJohn Johansen 		return next;
198029b3822fSJohn Johansen 	}
198129b3822fSJohn Johansen 
198229b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
198329b3822fSJohn Johansen 	parent = ns->parent;
1984ed2c7da3SJohn Johansen 	while (ns != root) {
198529b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
198638dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
198729b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
1988feb3c766SJohn Johansen 			mutex_lock_nested(&next->lock, next->level);
198929b3822fSJohn Johansen 			return next;
199029b3822fSJohn Johansen 		}
199129b3822fSJohn Johansen 		ns = parent;
199229b3822fSJohn Johansen 		parent = parent->parent;
199329b3822fSJohn Johansen 	}
199429b3822fSJohn Johansen 
199529b3822fSJohn Johansen 	return NULL;
199629b3822fSJohn Johansen }
199729b3822fSJohn Johansen 
199829b3822fSJohn Johansen /**
199929b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
200029b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
200129b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
200229b3822fSJohn Johansen  *
200329b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
200429b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
200529b3822fSJohn Johansen  */
200698849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
200798849dffSJohn Johansen 					  struct aa_ns *ns)
200829b3822fSJohn Johansen {
2009cbf2d0e1SJohn Johansen 	AA_BUG(!root);
2010cbf2d0e1SJohn Johansen 	AA_BUG(ns && !mutex_is_locked(&ns->lock));
2011cbf2d0e1SJohn Johansen 
201298849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
201329b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
201429b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
201529b3822fSJohn Johansen 						struct aa_profile, base.list);
201629b3822fSJohn Johansen 	}
201729b3822fSJohn Johansen 	return NULL;
201829b3822fSJohn Johansen }
201929b3822fSJohn Johansen 
202029b3822fSJohn Johansen /**
202129b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
202229b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
202329b3822fSJohn Johansen  *
202429b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
202529b3822fSJohn Johansen  *
202629b3822fSJohn Johansen  * Returns: next profile or NULL if done
202729b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
202829b3822fSJohn Johansen  */
202929b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
203029b3822fSJohn Johansen {
203129b3822fSJohn Johansen 	struct aa_profile *parent;
203298849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
203329b3822fSJohn Johansen 
2034cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock));
2035cbf2d0e1SJohn Johansen 
203629b3822fSJohn Johansen 	/* is next profile a child */
203729b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
203829b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
203929b3822fSJohn Johansen 					base.list);
204029b3822fSJohn Johansen 
204129b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
204229b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
204329b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
204429b3822fSJohn Johansen 	while (parent) {
204538dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
204629b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
204729b3822fSJohn Johansen 			return p;
204829b3822fSJohn Johansen 		p = parent;
204929b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
205029b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
205129b3822fSJohn Johansen 	}
205229b3822fSJohn Johansen 
205329b3822fSJohn Johansen 	/* is next another profile in the namespace */
205438dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
205529b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
205629b3822fSJohn Johansen 		return p;
205729b3822fSJohn Johansen 
205829b3822fSJohn Johansen 	return NULL;
205929b3822fSJohn Johansen }
206029b3822fSJohn Johansen 
206129b3822fSJohn Johansen /**
206229b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
206329b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
206429b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
206529b3822fSJohn Johansen  *
206629b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
206729b3822fSJohn Johansen  */
206898849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
206929b3822fSJohn Johansen 				       struct aa_profile *profile)
207029b3822fSJohn Johansen {
207129b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
207229b3822fSJohn Johansen 	if (next)
207329b3822fSJohn Johansen 		return next;
207429b3822fSJohn Johansen 
207529b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
207698849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
207729b3822fSJohn Johansen }
207829b3822fSJohn Johansen 
207929b3822fSJohn Johansen /**
208029b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
208129b3822fSJohn Johansen  * @f: seq_file to fill
208229b3822fSJohn Johansen  * @pos: current position
208329b3822fSJohn Johansen  *
208429b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
208529b3822fSJohn Johansen  *
208629b3822fSJohn Johansen  * acquires first ns->lock
208729b3822fSJohn Johansen  */
208829b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
208929b3822fSJohn Johansen {
209029b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
2091cf797c0eSJohn Johansen 	struct aa_ns *root = aa_get_current_ns();
209229b3822fSJohn Johansen 	loff_t l = *pos;
2093cf797c0eSJohn Johansen 	f->private = root;
209429b3822fSJohn Johansen 
209529b3822fSJohn Johansen 	/* find the first profile */
2096feb3c766SJohn Johansen 	mutex_lock_nested(&root->lock, root->level);
209729b3822fSJohn Johansen 	profile = __first_profile(root, root);
209829b3822fSJohn Johansen 
209929b3822fSJohn Johansen 	/* skip to position */
210029b3822fSJohn Johansen 	for (; profile && l > 0; l--)
210129b3822fSJohn Johansen 		profile = next_profile(root, profile);
210229b3822fSJohn Johansen 
210329b3822fSJohn Johansen 	return profile;
210429b3822fSJohn Johansen }
210529b3822fSJohn Johansen 
210629b3822fSJohn Johansen /**
210729b3822fSJohn Johansen  * p_next - read the next profile entry
210829b3822fSJohn Johansen  * @f: seq_file to fill
210929b3822fSJohn Johansen  * @p: profile previously returned
211029b3822fSJohn Johansen  * @pos: current position
211129b3822fSJohn Johansen  *
211229b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
211329b3822fSJohn Johansen  *
211429b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
211529b3822fSJohn Johansen  */
211629b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
211729b3822fSJohn Johansen {
211829b3822fSJohn Johansen 	struct aa_profile *profile = p;
211998849dffSJohn Johansen 	struct aa_ns *ns = f->private;
212029b3822fSJohn Johansen 	(*pos)++;
212129b3822fSJohn Johansen 
212229b3822fSJohn Johansen 	return next_profile(ns, profile);
212329b3822fSJohn Johansen }
212429b3822fSJohn Johansen 
212529b3822fSJohn Johansen /**
212629b3822fSJohn Johansen  * p_stop - stop depth first traversal
212729b3822fSJohn Johansen  * @f: seq_file we are filling
212829b3822fSJohn Johansen  * @p: the last profile writen
212929b3822fSJohn Johansen  *
213029b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
213129b3822fSJohn Johansen  */
213229b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
213329b3822fSJohn Johansen {
213429b3822fSJohn Johansen 	struct aa_profile *profile = p;
213598849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
213629b3822fSJohn Johansen 
213729b3822fSJohn Johansen 	if (profile) {
213829b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
213929b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
214029b3822fSJohn Johansen 	}
214129b3822fSJohn Johansen 	mutex_unlock(&root->lock);
214298849dffSJohn Johansen 	aa_put_ns(root);
214329b3822fSJohn Johansen }
214429b3822fSJohn Johansen 
214529b3822fSJohn Johansen /**
214629b3822fSJohn Johansen  * seq_show_profile - show a profile entry
214729b3822fSJohn Johansen  * @f: seq_file to file
214829b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
214929b3822fSJohn Johansen  *
215029b3822fSJohn Johansen  * Returns: error on failure
215129b3822fSJohn Johansen  */
215229b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
215329b3822fSJohn Johansen {
215429b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
215598849dffSJohn Johansen 	struct aa_ns *root = f->private;
215629b3822fSJohn Johansen 
2157637f688dSJohn Johansen 	aa_label_seq_xprint(f, root, &profile->label,
2158637f688dSJohn Johansen 			    FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
2159637f688dSJohn Johansen 	seq_putc(f, '\n');
216029b3822fSJohn Johansen 
216129b3822fSJohn Johansen 	return 0;
216229b3822fSJohn Johansen }
216329b3822fSJohn Johansen 
2164c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = {
216529b3822fSJohn Johansen 	.start = p_start,
216629b3822fSJohn Johansen 	.next = p_next,
216729b3822fSJohn Johansen 	.stop = p_stop,
216829b3822fSJohn Johansen 	.show = seq_show_profile,
216929b3822fSJohn Johansen };
217029b3822fSJohn Johansen 
217129b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
217229b3822fSJohn Johansen {
21735ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
21745ac8c355SJohn Johansen 		return -EACCES;
21755ac8c355SJohn Johansen 
2176c97204baSJohn Johansen 	return seq_open(file, &aa_sfs_profiles_op);
217729b3822fSJohn Johansen }
217829b3822fSJohn Johansen 
217929b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
218029b3822fSJohn Johansen {
218129b3822fSJohn Johansen 	return seq_release(inode, file);
218229b3822fSJohn Johansen }
218329b3822fSJohn Johansen 
2184c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = {
218529b3822fSJohn Johansen 	.open = profiles_open,
218629b3822fSJohn Johansen 	.read = seq_read,
218729b3822fSJohn Johansen 	.llseek = seq_lseek,
218829b3822fSJohn Johansen 	.release = profiles_release,
218929b3822fSJohn Johansen };
219029b3822fSJohn Johansen 
219129b3822fSJohn Johansen 
21920d259f04SJohn Johansen /** Base file system setup **/
2193c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = {
2194c97204baSJohn Johansen 	AA_SFS_FILE_STRING("mask",
2195c97204baSJohn Johansen 			   "create read write exec append mmap_exec link lock"),
2196a9bf8e9fSKees Cook 	{ }
2197a9bf8e9fSKees Cook };
2198a9bf8e9fSKees Cook 
2199290f458aSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ptrace[] = {
2200290f458aSJohn Johansen 	AA_SFS_FILE_STRING("mask", "read trace"),
2201290f458aSJohn Johansen 	{ }
2202290f458aSJohn Johansen };
2203290f458aSJohn Johansen 
2204cd1dbf76SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_signal[] = {
2205cd1dbf76SJohn Johansen 	AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK),
2206cd1dbf76SJohn Johansen 	{ }
2207cd1dbf76SJohn Johansen };
2208cd1dbf76SJohn Johansen 
220973f488cdSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_attach[] = {
221073f488cdSJohn Johansen 	AA_SFS_FILE_BOOLEAN("xattr", 1),
221173f488cdSJohn Johansen 	{ }
221273f488cdSJohn Johansen };
2213c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = {
2214c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hat",	1),
2215c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hatv",	1),
2216c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_onexec",	1),
2217c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_profile",	1),
22186c5fc8f1SJohn Johansen 	AA_SFS_FILE_BOOLEAN("stack",		1),
2219c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
22209fcf78ccSJohn Johansen 	AA_SFS_FILE_BOOLEAN("post_nnp_subset",	1),
222121f60661SJohn Johansen 	AA_SFS_FILE_BOOLEAN("computed_longest_left",	1),
222273f488cdSJohn Johansen 	AA_SFS_DIR("attach_conditions",		aa_sfs_entry_attach),
2223c97204baSJohn Johansen 	AA_SFS_FILE_STRING("version", "1.2"),
2224e74abcf3SKees Cook 	{ }
2225e74abcf3SKees Cook };
2226e74abcf3SKees Cook 
2227c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = {
2228c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("v5",	1),
22295379a331SJohn Johansen 	AA_SFS_FILE_BOOLEAN("v6",	1),
22305379a331SJohn Johansen 	AA_SFS_FILE_BOOLEAN("v7",	1),
223156974a6fSJohn Johansen 	AA_SFS_FILE_BOOLEAN("v8",	1),
2232474d6b75SJohn Johansen 	{ }
2233474d6b75SJohn Johansen };
2234474d6b75SJohn Johansen 
2235c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = {
2236c97204baSJohn Johansen 	AA_SFS_DIR("versions",			aa_sfs_entry_versions),
2237c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("set_load",		1),
22389d910a3bSJohn Johansen 	{ }
22399d910a3bSJohn Johansen };
22409d910a3bSJohn Johansen 
22412ea3ffb7SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_mount[] = {
22422ea3ffb7SJohn Johansen 	AA_SFS_FILE_STRING("mask", "mount umount pivot_root"),
22432ea3ffb7SJohn Johansen 	{ }
22442ea3ffb7SJohn Johansen };
22452ea3ffb7SJohn Johansen 
224633f2eadaSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ns[] = {
224733f2eadaSJohn Johansen 	AA_SFS_FILE_BOOLEAN("profile",		1),
22482ea3ffb7SJohn Johansen 	AA_SFS_FILE_BOOLEAN("pivot_root",	0),
224933f2eadaSJohn Johansen 	{ }
225033f2eadaSJohn Johansen };
225133f2eadaSJohn Johansen 
2252a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = {
22534f3b3f2dSJohn Johansen 	AA_SFS_FILE_STRING("perms", "allow deny audit quiet"),
2254a83bd86eSJohn Johansen 	AA_SFS_FILE_BOOLEAN("data",		1),
22551dea3b41SJohn Johansen 	AA_SFS_FILE_BOOLEAN("multi_transaction",	1),
2256a83bd86eSJohn Johansen 	{ }
2257a83bd86eSJohn Johansen };
2258a83bd86eSJohn Johansen 
2259a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = {
2260a83bd86eSJohn Johansen 	AA_SFS_DIR("label",			aa_sfs_entry_query_label),
2261a83bd86eSJohn Johansen 	{ }
2262a83bd86eSJohn Johansen };
2263c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = {
2264c97204baSJohn Johansen 	AA_SFS_DIR("policy",			aa_sfs_entry_policy),
2265c97204baSJohn Johansen 	AA_SFS_DIR("domain",			aa_sfs_entry_domain),
2266c97204baSJohn Johansen 	AA_SFS_DIR("file",			aa_sfs_entry_file),
226756974a6fSJohn Johansen 	AA_SFS_DIR("network_v8",		aa_sfs_entry_network),
22682ea3ffb7SJohn Johansen 	AA_SFS_DIR("mount",			aa_sfs_entry_mount),
226933f2eadaSJohn Johansen 	AA_SFS_DIR("namespaces",		aa_sfs_entry_ns),
2270c97204baSJohn Johansen 	AA_SFS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
2271c97204baSJohn Johansen 	AA_SFS_DIR("rlimit",			aa_sfs_entry_rlimit),
2272c97204baSJohn Johansen 	AA_SFS_DIR("caps",			aa_sfs_entry_caps),
2273290f458aSJohn Johansen 	AA_SFS_DIR("ptrace",			aa_sfs_entry_ptrace),
2274cd1dbf76SJohn Johansen 	AA_SFS_DIR("signal",			aa_sfs_entry_signal),
2275a83bd86eSJohn Johansen 	AA_SFS_DIR("query",			aa_sfs_entry_query),
2276e74abcf3SKees Cook 	{ }
2277e74abcf3SKees Cook };
2278e74abcf3SKees Cook 
2279c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
2280bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access),
22816c5fc8f1SJohn Johansen 	AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops),
22826c5fc8f1SJohn Johansen 	AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops),
2283bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops),
2284bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops),
2285bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops),
2286c97204baSJohn Johansen 	AA_SFS_DIR("features", aa_sfs_entry_features),
22879acd494bSKees Cook 	{ }
22889acd494bSKees Cook };
228963e2b423SJohn Johansen 
2290c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry =
2291c97204baSJohn Johansen 	AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
22929acd494bSKees Cook 
22939acd494bSKees Cook /**
2294a481f4d9SJohn Johansen  * entry_create_file - create a file entry in the apparmor securityfs
2295c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
22969acd494bSKees Cook  * @parent: the parent dentry in the securityfs
22979acd494bSKees Cook  *
2298a481f4d9SJohn Johansen  * Use entry_remove_file to remove entries created with this fn.
22999acd494bSKees Cook  */
2300c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file,
23019acd494bSKees Cook 				    struct dentry *parent)
230263e2b423SJohn Johansen {
23039acd494bSKees Cook 	int error = 0;
230463e2b423SJohn Johansen 
23059acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
23069acd494bSKees Cook 						 S_IFREG | fs_file->mode,
23079acd494bSKees Cook 						 parent, fs_file,
23089acd494bSKees Cook 						 fs_file->file_ops);
23099acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
23109acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
23119acd494bSKees Cook 		fs_file->dentry = NULL;
231263e2b423SJohn Johansen 	}
23139acd494bSKees Cook 	return error;
231463e2b423SJohn Johansen }
231563e2b423SJohn Johansen 
2316c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
231763e2b423SJohn Johansen /**
2318a481f4d9SJohn Johansen  * entry_create_dir - recursively create a directory entry in the securityfs
2319c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
23209acd494bSKees Cook  * @parent: the parent dentry in the securityfs
232163e2b423SJohn Johansen  *
2322a481f4d9SJohn Johansen  * Use entry_remove_dir to remove entries created with this fn.
232363e2b423SJohn Johansen  */
2324c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
23259acd494bSKees Cook 				   struct dentry *parent)
232663e2b423SJohn Johansen {
2327c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
23280d259f04SJohn Johansen 	struct dentry *dir;
23290d259f04SJohn Johansen 	int error;
233063e2b423SJohn Johansen 
23310d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
23320d259f04SJohn Johansen 	if (IS_ERR(dir))
23330d259f04SJohn Johansen 		return PTR_ERR(dir);
23340d259f04SJohn Johansen 	fs_dir->dentry = dir;
233563e2b423SJohn Johansen 
23360d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2337c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2338a481f4d9SJohn Johansen 			error = entry_create_dir(fs_file, fs_dir->dentry);
23399acd494bSKees Cook 		else
2340a481f4d9SJohn Johansen 			error = entry_create_file(fs_file, fs_dir->dentry);
23419acd494bSKees Cook 		if (error)
23429acd494bSKees Cook 			goto failed;
23439acd494bSKees Cook 	}
23449acd494bSKees Cook 
23459acd494bSKees Cook 	return 0;
23469acd494bSKees Cook 
23479acd494bSKees Cook failed:
2348a481f4d9SJohn Johansen 	entry_remove_dir(fs_dir);
23490d259f04SJohn Johansen 
23509acd494bSKees Cook 	return error;
23519acd494bSKees Cook }
23529acd494bSKees Cook 
23539acd494bSKees Cook /**
2354c97204baSJohn Johansen  * entry_remove_file - drop a single file entry in the apparmor securityfs
2355c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
23569acd494bSKees Cook  */
2357c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
23589acd494bSKees Cook {
23599acd494bSKees Cook 	if (!fs_file->dentry)
23609acd494bSKees Cook 		return;
23619acd494bSKees Cook 
23629acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
23639acd494bSKees Cook 	fs_file->dentry = NULL;
23649acd494bSKees Cook }
23659acd494bSKees Cook 
23669acd494bSKees Cook /**
2367a481f4d9SJohn Johansen  * entry_remove_dir - recursively drop a directory entry from the securityfs
2368c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
23699acd494bSKees Cook  */
2370c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
23719acd494bSKees Cook {
2372c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
23739acd494bSKees Cook 
23740d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2375c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2376a481f4d9SJohn Johansen 			entry_remove_dir(fs_file);
23779acd494bSKees Cook 		else
2378c97204baSJohn Johansen 			entry_remove_file(fs_file);
23799acd494bSKees Cook 	}
23809acd494bSKees Cook 
2381c97204baSJohn Johansen 	entry_remove_file(fs_dir);
238263e2b423SJohn Johansen }
238363e2b423SJohn Johansen 
238463e2b423SJohn Johansen /**
238563e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
238663e2b423SJohn Johansen  *
238763e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
238863e2b423SJohn Johansen  */
238963e2b423SJohn Johansen void __init aa_destroy_aafs(void)
239063e2b423SJohn Johansen {
2391c97204baSJohn Johansen 	entry_remove_dir(&aa_sfs_entry);
239263e2b423SJohn Johansen }
239363e2b423SJohn Johansen 
2394a71ada30SJohn Johansen 
2395a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
2396a71ada30SJohn Johansen struct path aa_null;
2397a71ada30SJohn Johansen 
2398a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
2399a71ada30SJohn Johansen {
2400a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
2401a71ada30SJohn Johansen 	struct dentry *dentry;
2402a71ada30SJohn Johansen 	struct inode *inode;
2403a71ada30SJohn Johansen 	int count = 0;
2404a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
2405a71ada30SJohn Johansen 
2406a71ada30SJohn Johansen 	if (error)
2407a71ada30SJohn Johansen 		return error;
2408a71ada30SJohn Johansen 
2409a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
2410a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
2411a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
2412a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
2413a71ada30SJohn Johansen 		goto out;
2414a71ada30SJohn Johansen 	}
2415a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
2416a71ada30SJohn Johansen 	if (!inode) {
2417a71ada30SJohn Johansen 		error = -ENOMEM;
2418a71ada30SJohn Johansen 		goto out1;
2419a71ada30SJohn Johansen 	}
2420a71ada30SJohn Johansen 
2421a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
2422a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
242324d0d03cSDeepa Dinamani 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
2424a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
2425a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
2426a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
2427a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
2428a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
2429a71ada30SJohn Johansen 
2430a71ada30SJohn Johansen 	error = 0;
2431a71ada30SJohn Johansen 
2432a71ada30SJohn Johansen out1:
2433a71ada30SJohn Johansen 	dput(dentry);
2434a71ada30SJohn Johansen out:
2435a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
2436a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
2437a71ada30SJohn Johansen 	return error;
2438a71ada30SJohn Johansen }
2439a71ada30SJohn Johansen 
2440a481f4d9SJohn Johansen 
2441a481f4d9SJohn Johansen 
2442a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry,
2443a481f4d9SJohn Johansen 				   struct inode *inode,
2444a481f4d9SJohn Johansen 				   struct delayed_call *done)
2445a481f4d9SJohn Johansen {
2446a481f4d9SJohn Johansen 	struct aa_ns *ns;
2447a481f4d9SJohn Johansen 	struct path path;
2448a481f4d9SJohn Johansen 
2449a481f4d9SJohn Johansen 	if (!dentry)
2450a481f4d9SJohn Johansen 		return ERR_PTR(-ECHILD);
2451a481f4d9SJohn Johansen 	ns = aa_get_current_ns();
2452a481f4d9SJohn Johansen 	path.mnt = mntget(aafs_mnt);
2453a481f4d9SJohn Johansen 	path.dentry = dget(ns_dir(ns));
2454a481f4d9SJohn Johansen 	nd_jump_link(&path);
2455a481f4d9SJohn Johansen 	aa_put_ns(ns);
2456a481f4d9SJohn Johansen 
2457a481f4d9SJohn Johansen 	return NULL;
2458a481f4d9SJohn Johansen }
2459a481f4d9SJohn Johansen 
2460a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer,
2461a481f4d9SJohn Johansen 			   int buflen)
2462a481f4d9SJohn Johansen {
2463a481f4d9SJohn Johansen 	char name[32];
2464a481f4d9SJohn Johansen 	int res;
2465a481f4d9SJohn Johansen 
2466a0781209SJohn Johansen 	res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME,
2467a0781209SJohn Johansen 		       d_inode(dentry)->i_ino);
2468a0781209SJohn Johansen 	if (res > 0 && res < sizeof(name))
2469a481f4d9SJohn Johansen 		res = readlink_copy(buffer, buflen, name);
2470a0781209SJohn Johansen 	else
2471a0781209SJohn Johansen 		res = -ENOENT;
2472a481f4d9SJohn Johansen 
2473a481f4d9SJohn Johansen 	return res;
2474a481f4d9SJohn Johansen }
2475a481f4d9SJohn Johansen 
2476a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = {
2477a481f4d9SJohn Johansen 	.readlink	= policy_readlink,
2478a481f4d9SJohn Johansen 	.get_link	= policy_get_link,
2479a481f4d9SJohn Johansen };
2480a481f4d9SJohn Johansen 
2481a481f4d9SJohn Johansen 
248263e2b423SJohn Johansen /**
248363e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
248463e2b423SJohn Johansen  *
248563e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
248663e2b423SJohn Johansen  *
248763e2b423SJohn Johansen  * Returns: error on failure
248863e2b423SJohn Johansen  */
24893417d8d5SJames Morris static int __init aa_create_aafs(void)
249063e2b423SJohn Johansen {
2491b7fd2c03SJohn Johansen 	struct dentry *dent;
249263e2b423SJohn Johansen 	int error;
249363e2b423SJohn Johansen 
249463e2b423SJohn Johansen 	if (!apparmor_initialized)
249563e2b423SJohn Johansen 		return 0;
249663e2b423SJohn Johansen 
2497c97204baSJohn Johansen 	if (aa_sfs_entry.dentry) {
249863e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
249963e2b423SJohn Johansen 		return -EEXIST;
250063e2b423SJohn Johansen 	}
250163e2b423SJohn Johansen 
2502a481f4d9SJohn Johansen 	/* setup apparmorfs used to virtualize policy/ */
2503a481f4d9SJohn Johansen 	aafs_mnt = kern_mount(&aafs_ops);
2504a481f4d9SJohn Johansen 	if (IS_ERR(aafs_mnt))
2505a481f4d9SJohn Johansen 		panic("can't set apparmorfs up\n");
25061751e8a6SLinus Torvalds 	aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
2507a481f4d9SJohn Johansen 
25089acd494bSKees Cook 	/* Populate fs tree. */
2509c97204baSJohn Johansen 	error = entry_create_dir(&aa_sfs_entry, NULL);
251063e2b423SJohn Johansen 	if (error)
251163e2b423SJohn Johansen 		goto error;
251263e2b423SJohn Johansen 
2513c97204baSJohn Johansen 	dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
2514b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_load);
2515cf916000SJohn Johansen 	if (IS_ERR(dent))
2516cf916000SJohn Johansen 		goto dent_error;
2517b7fd2c03SJohn Johansen 	ns_subload(root_ns) = dent;
2518b7fd2c03SJohn Johansen 
2519c97204baSJohn Johansen 	dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
2520b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_replace);
2521cf916000SJohn Johansen 	if (IS_ERR(dent))
2522cf916000SJohn Johansen 		goto dent_error;
2523b7fd2c03SJohn Johansen 	ns_subreplace(root_ns) = dent;
2524b7fd2c03SJohn Johansen 
2525c97204baSJohn Johansen 	dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
2526b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_remove);
2527cf916000SJohn Johansen 	if (IS_ERR(dent))
2528cf916000SJohn Johansen 		goto dent_error;
2529b7fd2c03SJohn Johansen 	ns_subremove(root_ns) = dent;
2530b7fd2c03SJohn Johansen 
2531d9bf2c26SJohn Johansen 	dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry,
2532d9bf2c26SJohn Johansen 				      NULL, &aa_fs_ns_revision_fops);
2533cf916000SJohn Johansen 	if (IS_ERR(dent))
2534cf916000SJohn Johansen 		goto dent_error;
2535d9bf2c26SJohn Johansen 	ns_subrevision(root_ns) = dent;
2536d9bf2c26SJohn Johansen 
2537d9bf2c26SJohn Johansen 	/* policy tree referenced by magic policy symlink */
2538feb3c766SJohn Johansen 	mutex_lock_nested(&root_ns->lock, root_ns->level);
2539c961ee5fSJohn Johansen 	error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
2540c961ee5fSJohn Johansen 				aafs_mnt->mnt_root);
2541b7fd2c03SJohn Johansen 	mutex_unlock(&root_ns->lock);
25420d259f04SJohn Johansen 	if (error)
25430d259f04SJohn Johansen 		goto error;
25440d259f04SJohn Johansen 
2545c961ee5fSJohn Johansen 	/* magic symlink similar to nsfs redirects based on task policy */
2546c961ee5fSJohn Johansen 	dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
2547c961ee5fSJohn Johansen 					 NULL, &policy_link_iops);
2548cf916000SJohn Johansen 	if (IS_ERR(dent))
2549cf916000SJohn Johansen 		goto dent_error;
2550c961ee5fSJohn Johansen 
2551c97204baSJohn Johansen 	error = aa_mk_null_file(aa_sfs_entry.dentry);
2552a71ada30SJohn Johansen 	if (error)
2553a71ada30SJohn Johansen 		goto error;
2554a71ada30SJohn Johansen 
2555a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
255663e2b423SJohn Johansen 
255763e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
255863e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
255963e2b423SJohn Johansen 	return 0;
256063e2b423SJohn Johansen 
2561cf916000SJohn Johansen dent_error:
2562cf916000SJohn Johansen 	error = PTR_ERR(dent);
256363e2b423SJohn Johansen error:
256463e2b423SJohn Johansen 	aa_destroy_aafs();
256563e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
256663e2b423SJohn Johansen 	return error;
256763e2b423SJohn Johansen }
256863e2b423SJohn Johansen 
256963e2b423SJohn Johansen fs_initcall(aa_create_aafs);
2570