xref: /openbmc/linux/security/apparmor/apparmorfs.c (revision 637f688dc3dc304a89f441d76f49a0e35bc49c08)
163e2b423SJohn Johansen /*
263e2b423SJohn Johansen  * AppArmor security module
363e2b423SJohn Johansen  *
463e2b423SJohn Johansen  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
563e2b423SJohn Johansen  *
663e2b423SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
763e2b423SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
863e2b423SJohn Johansen  *
963e2b423SJohn Johansen  * This program is free software; you can redistribute it and/or
1063e2b423SJohn Johansen  * modify it under the terms of the GNU General Public License as
1163e2b423SJohn Johansen  * published by the Free Software Foundation, version 2 of the
1263e2b423SJohn Johansen  * License.
1363e2b423SJohn Johansen  */
1463e2b423SJohn Johansen 
150d259f04SJohn Johansen #include <linux/ctype.h>
1663e2b423SJohn Johansen #include <linux/security.h>
1763e2b423SJohn Johansen #include <linux/vmalloc.h>
1863e2b423SJohn Johansen #include <linux/module.h>
1963e2b423SJohn Johansen #include <linux/seq_file.h>
2063e2b423SJohn Johansen #include <linux/uaccess.h>
21a71ada30SJohn Johansen #include <linux/mount.h>
2263e2b423SJohn Johansen #include <linux/namei.h>
23e74abcf3SKees Cook #include <linux/capability.h>
2429b3822fSJohn Johansen #include <linux/rcupdate.h>
25a71ada30SJohn Johansen #include <linux/fs.h>
26d9bf2c26SJohn Johansen #include <linux/poll.h>
27a481f4d9SJohn Johansen #include <uapi/linux/major.h>
28a481f4d9SJohn Johansen #include <uapi/linux/magic.h>
2963e2b423SJohn Johansen 
3063e2b423SJohn Johansen #include "include/apparmor.h"
3163e2b423SJohn Johansen #include "include/apparmorfs.h"
3263e2b423SJohn Johansen #include "include/audit.h"
3363e2b423SJohn Johansen #include "include/context.h"
34f8eb8a13SJohn Johansen #include "include/crypto.h"
35d9bf2c26SJohn Johansen #include "include/policy_ns.h"
3663e2b423SJohn Johansen #include "include/policy.h"
37cff281f6SJohn Johansen #include "include/policy_ns.h"
38d384b0a1SKees Cook #include "include/resource.h"
395ac8c355SJohn Johansen #include "include/policy_unpack.h"
4063e2b423SJohn Johansen 
41c97204baSJohn Johansen /*
42c97204baSJohn Johansen  * The apparmor filesystem interface used for policy load and introspection
43c97204baSJohn Johansen  * The interface is split into two main components based on their function
44c97204baSJohn Johansen  * a securityfs component:
45c97204baSJohn Johansen  *   used for static files that are always available, and which allows
46c97204baSJohn Johansen  *   userspace to specificy the location of the security filesystem.
47c97204baSJohn Johansen  *
48c97204baSJohn Johansen  *   fns and data are prefixed with
49c97204baSJohn Johansen  *      aa_sfs_
50c97204baSJohn Johansen  *
51c97204baSJohn Johansen  * an apparmorfs component:
52c97204baSJohn Johansen  *   used loaded policy content and introspection. It is not part of  a
53c97204baSJohn Johansen  *   regular mounted filesystem and is available only through the magic
54c97204baSJohn Johansen  *   policy symlink in the root of the securityfs apparmor/ directory.
55c97204baSJohn Johansen  *   Tasks queries will be magically redirected to the correct portion
56c97204baSJohn Johansen  *   of the policy tree based on their confinement.
57c97204baSJohn Johansen  *
58c97204baSJohn Johansen  *   fns and data are prefixed with
59c97204baSJohn Johansen  *      aafs_
60c97204baSJohn Johansen  *
61c97204baSJohn Johansen  * The aa_fs_ prefix is used to indicate the fn is used by both the
62c97204baSJohn Johansen  * securityfs and apparmorfs filesystems.
63c97204baSJohn Johansen  */
64c97204baSJohn Johansen 
65c97204baSJohn Johansen 
66c97204baSJohn Johansen /*
67c97204baSJohn Johansen  * support fns
68c97204baSJohn Johansen  */
69c97204baSJohn Johansen 
7063e2b423SJohn Johansen /**
710d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
720d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
730d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
740d259f04SJohn Johansen  *
750d259f04SJohn Johansen  * Returns: length of mangled name
760d259f04SJohn Johansen  */
77bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
780d259f04SJohn Johansen {
790d259f04SJohn Johansen 	char *t = target;
800d259f04SJohn Johansen 
810d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
820d259f04SJohn Johansen 		name++;
830d259f04SJohn Johansen 
840d259f04SJohn Johansen 	if (target) {
850d259f04SJohn Johansen 		for (; *name; name++) {
860d259f04SJohn Johansen 			if (*name == '/')
870d259f04SJohn Johansen 				*(t)++ = '.';
880d259f04SJohn Johansen 			else if (isspace(*name))
890d259f04SJohn Johansen 				*(t)++ = '_';
900d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
910d259f04SJohn Johansen 				*(t)++ = *name;
920d259f04SJohn Johansen 		}
930d259f04SJohn Johansen 
940d259f04SJohn Johansen 		*t = 0;
950d259f04SJohn Johansen 	} else {
960d259f04SJohn Johansen 		int len = 0;
970d259f04SJohn Johansen 		for (; *name; name++) {
980d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
990d259f04SJohn Johansen 			    strchr("/._-", *name))
1000d259f04SJohn Johansen 				len++;
1010d259f04SJohn Johansen 		}
1020d259f04SJohn Johansen 
1030d259f04SJohn Johansen 		return len;
1040d259f04SJohn Johansen 	}
1050d259f04SJohn Johansen 
1060d259f04SJohn Johansen 	return t - target;
1070d259f04SJohn Johansen }
1080d259f04SJohn Johansen 
109a481f4d9SJohn Johansen 
110a481f4d9SJohn Johansen /*
111a481f4d9SJohn Johansen  * aafs - core fns and data for the policy tree
112a481f4d9SJohn Johansen  */
113a481f4d9SJohn Johansen 
114a481f4d9SJohn Johansen #define AAFS_NAME		"apparmorfs"
115a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt;
116a481f4d9SJohn Johansen static int aafs_count;
117a481f4d9SJohn Johansen 
118a481f4d9SJohn Johansen 
119a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
120a481f4d9SJohn Johansen {
121a481f4d9SJohn Johansen 	struct inode *inode = d_inode(dentry);
122a481f4d9SJohn Johansen 
123a481f4d9SJohn Johansen 	seq_printf(seq, "%s:[%lu]", AAFS_NAME, inode->i_ino);
124a481f4d9SJohn Johansen 	return 0;
125a481f4d9SJohn Johansen }
126a481f4d9SJohn Johansen 
127a481f4d9SJohn Johansen static void aafs_evict_inode(struct inode *inode)
128a481f4d9SJohn Johansen {
129a481f4d9SJohn Johansen 	truncate_inode_pages_final(&inode->i_data);
130a481f4d9SJohn Johansen 	clear_inode(inode);
131a481f4d9SJohn Johansen 	if (S_ISLNK(inode->i_mode))
132a481f4d9SJohn Johansen 		kfree(inode->i_link);
133a481f4d9SJohn Johansen }
134a481f4d9SJohn Johansen 
135a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = {
136a481f4d9SJohn Johansen 	.statfs = simple_statfs,
137a481f4d9SJohn Johansen 	.evict_inode = aafs_evict_inode,
138a481f4d9SJohn Johansen 	.show_path = aafs_show_path,
139a481f4d9SJohn Johansen };
140a481f4d9SJohn Johansen 
141a481f4d9SJohn Johansen static int fill_super(struct super_block *sb, void *data, int silent)
142a481f4d9SJohn Johansen {
143a481f4d9SJohn Johansen 	static struct tree_descr files[] = { {""} };
144a481f4d9SJohn Johansen 	int error;
145a481f4d9SJohn Johansen 
146a481f4d9SJohn Johansen 	error = simple_fill_super(sb, AAFS_MAGIC, files);
147a481f4d9SJohn Johansen 	if (error)
148a481f4d9SJohn Johansen 		return error;
149a481f4d9SJohn Johansen 	sb->s_op = &aafs_super_ops;
150a481f4d9SJohn Johansen 
151a481f4d9SJohn Johansen 	return 0;
152a481f4d9SJohn Johansen }
153a481f4d9SJohn Johansen 
154a481f4d9SJohn Johansen static struct dentry *aafs_mount(struct file_system_type *fs_type,
155a481f4d9SJohn Johansen 				 int flags, const char *dev_name, void *data)
156a481f4d9SJohn Johansen {
157a481f4d9SJohn Johansen 	return mount_single(fs_type, flags, data, fill_super);
158a481f4d9SJohn Johansen }
159a481f4d9SJohn Johansen 
160a481f4d9SJohn Johansen static struct file_system_type aafs_ops = {
161a481f4d9SJohn Johansen 	.owner = THIS_MODULE,
162a481f4d9SJohn Johansen 	.name = AAFS_NAME,
163a481f4d9SJohn Johansen 	.mount = aafs_mount,
164a481f4d9SJohn Johansen 	.kill_sb = kill_anon_super,
165a481f4d9SJohn Johansen };
166a481f4d9SJohn Johansen 
167a481f4d9SJohn Johansen /**
168a481f4d9SJohn Johansen  * __aafs_setup_d_inode - basic inode setup for apparmorfs
169a481f4d9SJohn Johansen  * @dir: parent directory for the dentry
170a481f4d9SJohn Johansen  * @dentry: dentry we are seting the inode up for
171a481f4d9SJohn Johansen  * @mode: permissions the file should have
172a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
173a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
174a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used
175a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
176a481f4d9SJohn Johansen  */
177a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
178a481f4d9SJohn Johansen 			       umode_t mode, void *data, char *link,
179a481f4d9SJohn Johansen 			       const struct file_operations *fops,
180a481f4d9SJohn Johansen 			       const struct inode_operations *iops)
181a481f4d9SJohn Johansen {
182a481f4d9SJohn Johansen 	struct inode *inode = new_inode(dir->i_sb);
183a481f4d9SJohn Johansen 
184a481f4d9SJohn Johansen 	AA_BUG(!dir);
185a481f4d9SJohn Johansen 	AA_BUG(!dentry);
186a481f4d9SJohn Johansen 
187a481f4d9SJohn Johansen 	if (!inode)
188a481f4d9SJohn Johansen 		return -ENOMEM;
189a481f4d9SJohn Johansen 
190a481f4d9SJohn Johansen 	inode->i_ino = get_next_ino();
191a481f4d9SJohn Johansen 	inode->i_mode = mode;
192a481f4d9SJohn Johansen 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
193a481f4d9SJohn Johansen 	inode->i_private = data;
194a481f4d9SJohn Johansen 	if (S_ISDIR(mode)) {
195a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_dir_inode_operations;
196a481f4d9SJohn Johansen 		inode->i_fop = &simple_dir_operations;
197a481f4d9SJohn Johansen 		inc_nlink(inode);
198a481f4d9SJohn Johansen 		inc_nlink(dir);
199a481f4d9SJohn Johansen 	} else if (S_ISLNK(mode)) {
200a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_symlink_inode_operations;
201a481f4d9SJohn Johansen 		inode->i_link = link;
202a481f4d9SJohn Johansen 	} else {
203a481f4d9SJohn Johansen 		inode->i_fop = fops;
204a481f4d9SJohn Johansen 	}
205a481f4d9SJohn Johansen 	d_instantiate(dentry, inode);
206a481f4d9SJohn Johansen 	dget(dentry);
207a481f4d9SJohn Johansen 
208a481f4d9SJohn Johansen 	return 0;
209a481f4d9SJohn Johansen }
210a481f4d9SJohn Johansen 
211a481f4d9SJohn Johansen /**
212a481f4d9SJohn Johansen  * aafs_create - create a dentry in the apparmorfs filesystem
213a481f4d9SJohn Johansen  *
214a481f4d9SJohn Johansen  * @name: name of dentry to create
215a481f4d9SJohn Johansen  * @mode: permissions the file should have
216a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
217a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
218a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
219a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
220a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
221a481f4d9SJohn Johansen  *
222a481f4d9SJohn Johansen  * This is the basic "create a xxx" function for apparmorfs.
223a481f4d9SJohn Johansen  *
224a481f4d9SJohn Johansen  * Returns a pointer to a dentry if it succeeds, that must be free with
225a481f4d9SJohn Johansen  * aafs_remove(). Will return ERR_PTR on failure.
226a481f4d9SJohn Johansen  */
227a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode,
228a481f4d9SJohn Johansen 				  struct dentry *parent, void *data, void *link,
229a481f4d9SJohn Johansen 				  const struct file_operations *fops,
230a481f4d9SJohn Johansen 				  const struct inode_operations *iops)
231a481f4d9SJohn Johansen {
232a481f4d9SJohn Johansen 	struct dentry *dentry;
233a481f4d9SJohn Johansen 	struct inode *dir;
234a481f4d9SJohn Johansen 	int error;
235a481f4d9SJohn Johansen 
236a481f4d9SJohn Johansen 	AA_BUG(!name);
237a481f4d9SJohn Johansen 	AA_BUG(!parent);
238a481f4d9SJohn Johansen 
239a481f4d9SJohn Johansen 	if (!(mode & S_IFMT))
240a481f4d9SJohn Johansen 		mode = (mode & S_IALLUGO) | S_IFREG;
241a481f4d9SJohn Johansen 
242a481f4d9SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
243a481f4d9SJohn Johansen 	if (error)
244a481f4d9SJohn Johansen 		return ERR_PTR(error);
245a481f4d9SJohn Johansen 
246a481f4d9SJohn Johansen 	dir = d_inode(parent);
247a481f4d9SJohn Johansen 
248a481f4d9SJohn Johansen 	inode_lock(dir);
249a481f4d9SJohn Johansen 	dentry = lookup_one_len(name, parent, strlen(name));
250a481f4d9SJohn Johansen 	if (IS_ERR(dentry))
251a481f4d9SJohn Johansen 		goto fail_lock;
252a481f4d9SJohn Johansen 
253a481f4d9SJohn Johansen 	if (d_really_is_positive(dentry)) {
254a481f4d9SJohn Johansen 		error = -EEXIST;
255a481f4d9SJohn Johansen 		goto fail_dentry;
256a481f4d9SJohn Johansen 	}
257a481f4d9SJohn Johansen 
258a481f4d9SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
259a481f4d9SJohn Johansen 	if (error)
260a481f4d9SJohn Johansen 		goto fail_dentry;
261a481f4d9SJohn Johansen 	inode_unlock(dir);
262a481f4d9SJohn Johansen 
263a481f4d9SJohn Johansen 	return dentry;
264a481f4d9SJohn Johansen 
265a481f4d9SJohn Johansen fail_dentry:
266a481f4d9SJohn Johansen 	dput(dentry);
267a481f4d9SJohn Johansen 
268a481f4d9SJohn Johansen fail_lock:
269a481f4d9SJohn Johansen 	inode_unlock(dir);
270a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
271a481f4d9SJohn Johansen 
272a481f4d9SJohn Johansen 	return ERR_PTR(error);
273a481f4d9SJohn Johansen }
274a481f4d9SJohn Johansen 
275a481f4d9SJohn Johansen /**
276a481f4d9SJohn Johansen  * aafs_create_file - create a file in the apparmorfs filesystem
277a481f4d9SJohn Johansen  *
278a481f4d9SJohn Johansen  * @name: name of dentry to create
279a481f4d9SJohn Johansen  * @mode: permissions the file should have
280a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
281a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
282a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
283a481f4d9SJohn Johansen  *
284a481f4d9SJohn Johansen  * see aafs_create
285a481f4d9SJohn Johansen  */
286a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode,
287a481f4d9SJohn Johansen 				       struct dentry *parent, void *data,
288a481f4d9SJohn Johansen 				       const struct file_operations *fops)
289a481f4d9SJohn Johansen {
290a481f4d9SJohn Johansen 	return aafs_create(name, mode, parent, data, NULL, fops, NULL);
291a481f4d9SJohn Johansen }
292a481f4d9SJohn Johansen 
293a481f4d9SJohn Johansen /**
294a481f4d9SJohn Johansen  * aafs_create_dir - create a directory in the apparmorfs filesystem
295a481f4d9SJohn Johansen  *
296a481f4d9SJohn Johansen  * @name: name of dentry to create
297a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
298a481f4d9SJohn Johansen  *
299a481f4d9SJohn Johansen  * see aafs_create
300a481f4d9SJohn Johansen  */
301a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
302a481f4d9SJohn Johansen {
303a481f4d9SJohn Johansen 	return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
304a481f4d9SJohn Johansen 			   NULL);
305a481f4d9SJohn Johansen }
306a481f4d9SJohn Johansen 
307a481f4d9SJohn Johansen /**
308a481f4d9SJohn Johansen  * aafs_create_symlink - create a symlink in the apparmorfs filesystem
309a481f4d9SJohn Johansen  * @name: name of dentry to create
310a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
311a481f4d9SJohn Johansen  * @target: if symlink, symlink target string
312a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
313a481f4d9SJohn Johansen  *
314a481f4d9SJohn Johansen  * If @target parameter is %NULL, then the @iops parameter needs to be
315a481f4d9SJohn Johansen  * setup to handle .readlink and .get_link inode_operations.
316a481f4d9SJohn Johansen  */
317a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name,
318a481f4d9SJohn Johansen 					  struct dentry *parent,
319a481f4d9SJohn Johansen 					  const char *target,
320a481f4d9SJohn Johansen 					  const struct inode_operations *iops)
321a481f4d9SJohn Johansen {
322a481f4d9SJohn Johansen 	struct dentry *dent;
323a481f4d9SJohn Johansen 	char *link = NULL;
324a481f4d9SJohn Johansen 
325a481f4d9SJohn Johansen 	if (target) {
326a481f4d9SJohn Johansen 		link = kstrdup(target, GFP_KERNEL);
327a481f4d9SJohn Johansen 		if (!link)
328a481f4d9SJohn Johansen 			return ERR_PTR(-ENOMEM);
329a481f4d9SJohn Johansen 	}
330a481f4d9SJohn Johansen 	dent = aafs_create(name, S_IFLNK | 0444, parent, NULL, link, NULL,
331a481f4d9SJohn Johansen 			   iops);
332a481f4d9SJohn Johansen 	if (IS_ERR(dent))
333a481f4d9SJohn Johansen 		kfree(link);
334a481f4d9SJohn Johansen 
335a481f4d9SJohn Johansen 	return dent;
336a481f4d9SJohn Johansen }
337a481f4d9SJohn Johansen 
338a481f4d9SJohn Johansen /**
339a481f4d9SJohn Johansen  * aafs_remove - removes a file or directory from the apparmorfs filesystem
340a481f4d9SJohn Johansen  *
341a481f4d9SJohn Johansen  * @dentry: dentry of the file/directory/symlink to removed.
342a481f4d9SJohn Johansen  */
343a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry)
344a481f4d9SJohn Johansen {
345a481f4d9SJohn Johansen 	struct inode *dir;
346a481f4d9SJohn Johansen 
347a481f4d9SJohn Johansen 	if (!dentry || IS_ERR(dentry))
348a481f4d9SJohn Johansen 		return;
349a481f4d9SJohn Johansen 
350a481f4d9SJohn Johansen 	dir = d_inode(dentry->d_parent);
351a481f4d9SJohn Johansen 	inode_lock(dir);
352a481f4d9SJohn Johansen 	if (simple_positive(dentry)) {
353a481f4d9SJohn Johansen 		if (d_is_dir(dentry))
354a481f4d9SJohn Johansen 			simple_rmdir(dir, dentry);
355a481f4d9SJohn Johansen 		else
356a481f4d9SJohn Johansen 			simple_unlink(dir, dentry);
357a481f4d9SJohn Johansen 		dput(dentry);
358a481f4d9SJohn Johansen 	}
359a481f4d9SJohn Johansen 	inode_unlock(dir);
360a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
361a481f4d9SJohn Johansen }
362a481f4d9SJohn Johansen 
363a481f4d9SJohn Johansen 
364a481f4d9SJohn Johansen /*
365a481f4d9SJohn Johansen  * aa_fs - policy load/replace/remove
366a481f4d9SJohn Johansen  */
367a481f4d9SJohn Johansen 
3680d259f04SJohn Johansen /**
36963e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
37063e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
3713ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
37263e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
37363e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
37463e2b423SJohn Johansen  *
37563e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
37663e2b423SJohn Johansen  *          ERR_PTR on failure.
37763e2b423SJohn Johansen  */
3785ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
3795ac8c355SJohn Johansen 						     size_t alloc_size,
3805ac8c355SJohn Johansen 						     size_t copy_size,
38163e2b423SJohn Johansen 						     loff_t *pos)
38263e2b423SJohn Johansen {
3835ac8c355SJohn Johansen 	struct aa_loaddata *data;
38463e2b423SJohn Johansen 
385e6bfa25dSJohn Johansen 	AA_BUG(copy_size > alloc_size);
3863ed02adaSJohn Johansen 
38763e2b423SJohn Johansen 	if (*pos != 0)
38863e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
38963e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
39063e2b423SJohn Johansen 
39163e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
3925d5182caSJohn Johansen 	data = aa_loaddata_alloc(alloc_size);
3935d5182caSJohn Johansen 	if (IS_ERR(data))
3945d5182caSJohn Johansen 		return data;
39563e2b423SJohn Johansen 
3965d5182caSJohn Johansen 	data->size = copy_size;
3975ac8c355SJohn Johansen 	if (copy_from_user(data->data, userbuf, copy_size)) {
39863e2b423SJohn Johansen 		kvfree(data);
39963e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
40063e2b423SJohn Johansen 	}
40163e2b423SJohn Johansen 
40263e2b423SJohn Johansen 	return data;
40363e2b423SJohn Johansen }
40463e2b423SJohn Johansen 
40518e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
406b7fd2c03SJohn Johansen 			     loff_t *pos, struct aa_ns *ns)
4075ac8c355SJohn Johansen {
4085ac8c355SJohn Johansen 	struct aa_loaddata *data;
409*637f688dSJohn Johansen 	struct aa_label *label;
410*637f688dSJohn Johansen 	ssize_t error;
411cf797c0eSJohn Johansen 
412*637f688dSJohn Johansen 	label = begin_current_label_crit_section();
413cf797c0eSJohn Johansen 
4145ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4155ac8c355SJohn Johansen 	 * below after unpack
4165ac8c355SJohn Johansen 	 */
417*637f688dSJohn Johansen 	error = aa_may_manage_policy(label, ns, mask);
4185ac8c355SJohn Johansen 	if (error)
4195ac8c355SJohn Johansen 		return error;
42063e2b423SJohn Johansen 
4215ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size, size, pos);
4225ac8c355SJohn Johansen 	error = PTR_ERR(data);
4235ac8c355SJohn Johansen 	if (!IS_ERR(data)) {
424*637f688dSJohn Johansen 		error = aa_replace_profiles(ns, label, mask, data);
4255ac8c355SJohn Johansen 		aa_put_loaddata(data);
4265ac8c355SJohn Johansen 	}
427*637f688dSJohn Johansen 	end_current_label_crit_section(label);
4285ac8c355SJohn Johansen 
4295ac8c355SJohn Johansen 	return error;
4305ac8c355SJohn Johansen }
4315ac8c355SJohn Johansen 
432b7fd2c03SJohn Johansen /* .load file hook fn to load policy */
43363e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
43463e2b423SJohn Johansen 			    loff_t *pos)
43563e2b423SJohn Johansen {
436b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
43718e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
438b7fd2c03SJohn Johansen 
439b7fd2c03SJohn Johansen 	aa_put_ns(ns);
44063e2b423SJohn Johansen 
44163e2b423SJohn Johansen 	return error;
44263e2b423SJohn Johansen }
44363e2b423SJohn Johansen 
44463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
4456038f373SArnd Bergmann 	.write = profile_load,
4466038f373SArnd Bergmann 	.llseek = default_llseek,
44763e2b423SJohn Johansen };
44863e2b423SJohn Johansen 
44963e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
45063e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
45163e2b423SJohn Johansen 			       size_t size, loff_t *pos)
45263e2b423SJohn Johansen {
453b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
45418e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
45518e99f19SJohn Johansen 				  buf, size, pos, ns);
456b7fd2c03SJohn Johansen 	aa_put_ns(ns);
45763e2b423SJohn Johansen 
45863e2b423SJohn Johansen 	return error;
45963e2b423SJohn Johansen }
46063e2b423SJohn Johansen 
46163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
4626038f373SArnd Bergmann 	.write = profile_replace,
4636038f373SArnd Bergmann 	.llseek = default_llseek,
46463e2b423SJohn Johansen };
46563e2b423SJohn Johansen 
466b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */
46763e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
46863e2b423SJohn Johansen 			      size_t size, loff_t *pos)
46963e2b423SJohn Johansen {
4705ac8c355SJohn Johansen 	struct aa_loaddata *data;
471*637f688dSJohn Johansen 	struct aa_label *label;
47263e2b423SJohn Johansen 	ssize_t error;
473b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
47463e2b423SJohn Johansen 
475*637f688dSJohn Johansen 	label = begin_current_label_crit_section();
4765ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4775ac8c355SJohn Johansen 	 * below after unpack
4785ac8c355SJohn Johansen 	 */
479*637f688dSJohn Johansen 	error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY);
4805ac8c355SJohn Johansen 	if (error)
4815ac8c355SJohn Johansen 		goto out;
4825ac8c355SJohn Johansen 
48363e2b423SJohn Johansen 	/*
48463e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
48563e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
48663e2b423SJohn Johansen 	 */
4875ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
48863e2b423SJohn Johansen 
48963e2b423SJohn Johansen 	error = PTR_ERR(data);
49063e2b423SJohn Johansen 	if (!IS_ERR(data)) {
4915ac8c355SJohn Johansen 		data->data[size] = 0;
492*637f688dSJohn Johansen 		error = aa_remove_profiles(ns, label, data->data, size);
4935ac8c355SJohn Johansen 		aa_put_loaddata(data);
49463e2b423SJohn Johansen 	}
4955ac8c355SJohn Johansen  out:
496*637f688dSJohn Johansen 	end_current_label_crit_section(label);
497b7fd2c03SJohn Johansen 	aa_put_ns(ns);
49863e2b423SJohn Johansen 	return error;
49963e2b423SJohn Johansen }
50063e2b423SJohn Johansen 
50163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
5026038f373SArnd Bergmann 	.write = profile_remove,
5036038f373SArnd Bergmann 	.llseek = default_llseek,
50463e2b423SJohn Johansen };
50563e2b423SJohn Johansen 
506d9bf2c26SJohn Johansen struct aa_revision {
507d9bf2c26SJohn Johansen 	struct aa_ns *ns;
508d9bf2c26SJohn Johansen 	long last_read;
509d9bf2c26SJohn Johansen };
510d9bf2c26SJohn Johansen 
511d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */
512d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file)
513d9bf2c26SJohn Johansen {
514d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
515d9bf2c26SJohn Johansen 
516d9bf2c26SJohn Johansen 	if (rev) {
517d9bf2c26SJohn Johansen 		aa_put_ns(rev->ns);
518d9bf2c26SJohn Johansen 		kfree(rev);
519d9bf2c26SJohn Johansen 	}
520d9bf2c26SJohn Johansen 
521d9bf2c26SJohn Johansen 	return 0;
522d9bf2c26SJohn Johansen }
523d9bf2c26SJohn Johansen 
524d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf,
525d9bf2c26SJohn Johansen 				size_t size, loff_t *ppos)
526d9bf2c26SJohn Johansen {
527d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
528d9bf2c26SJohn Johansen 	char buffer[32];
529d9bf2c26SJohn Johansen 	long last_read;
530d9bf2c26SJohn Johansen 	int avail;
531d9bf2c26SJohn Johansen 
532d9bf2c26SJohn Johansen 	mutex_lock(&rev->ns->lock);
533d9bf2c26SJohn Johansen 	last_read = rev->last_read;
534d9bf2c26SJohn Johansen 	if (last_read == rev->ns->revision) {
535d9bf2c26SJohn Johansen 		mutex_unlock(&rev->ns->lock);
536d9bf2c26SJohn Johansen 		if (file->f_flags & O_NONBLOCK)
537d9bf2c26SJohn Johansen 			return -EAGAIN;
538d9bf2c26SJohn Johansen 		if (wait_event_interruptible(rev->ns->wait,
539d9bf2c26SJohn Johansen 					     last_read !=
540d9bf2c26SJohn Johansen 					     READ_ONCE(rev->ns->revision)))
541d9bf2c26SJohn Johansen 			return -ERESTARTSYS;
542d9bf2c26SJohn Johansen 		mutex_lock(&rev->ns->lock);
543d9bf2c26SJohn Johansen 	}
544d9bf2c26SJohn Johansen 
545d9bf2c26SJohn Johansen 	avail = sprintf(buffer, "%ld\n", rev->ns->revision);
546d9bf2c26SJohn Johansen 	if (*ppos + size > avail) {
547d9bf2c26SJohn Johansen 		rev->last_read = rev->ns->revision;
548d9bf2c26SJohn Johansen 		*ppos = 0;
549d9bf2c26SJohn Johansen 	}
550d9bf2c26SJohn Johansen 	mutex_unlock(&rev->ns->lock);
551d9bf2c26SJohn Johansen 
552d9bf2c26SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, buffer, avail);
553d9bf2c26SJohn Johansen }
554d9bf2c26SJohn Johansen 
555d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file)
556d9bf2c26SJohn Johansen {
557d9bf2c26SJohn Johansen 	struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
558d9bf2c26SJohn Johansen 
559d9bf2c26SJohn Johansen 	if (!rev)
560d9bf2c26SJohn Johansen 		return -ENOMEM;
561d9bf2c26SJohn Johansen 
562d9bf2c26SJohn Johansen 	rev->ns = aa_get_ns(inode->i_private);
563d9bf2c26SJohn Johansen 	if (!rev->ns)
564d9bf2c26SJohn Johansen 		rev->ns = aa_get_current_ns();
565d9bf2c26SJohn Johansen 	file->private_data = rev;
566d9bf2c26SJohn Johansen 
567d9bf2c26SJohn Johansen 	return 0;
568d9bf2c26SJohn Johansen }
569d9bf2c26SJohn Johansen 
570d9bf2c26SJohn Johansen static unsigned int ns_revision_poll(struct file *file, poll_table *pt)
571d9bf2c26SJohn Johansen {
572d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
573d9bf2c26SJohn Johansen 	unsigned int mask = 0;
574d9bf2c26SJohn Johansen 
575d9bf2c26SJohn Johansen 	if (rev) {
576d9bf2c26SJohn Johansen 		mutex_lock(&rev->ns->lock);
577d9bf2c26SJohn Johansen 		poll_wait(file, &rev->ns->wait, pt);
578d9bf2c26SJohn Johansen 		if (rev->last_read < rev->ns->revision)
579d9bf2c26SJohn Johansen 			mask |= POLLIN | POLLRDNORM;
580d9bf2c26SJohn Johansen 		mutex_unlock(&rev->ns->lock);
581d9bf2c26SJohn Johansen 	}
582d9bf2c26SJohn Johansen 
583d9bf2c26SJohn Johansen 	return mask;
584d9bf2c26SJohn Johansen }
585d9bf2c26SJohn Johansen 
5865d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns)
5875d5182caSJohn Johansen {
5885d5182caSJohn Johansen 	ns->revision++;
589d9bf2c26SJohn Johansen 	wake_up_interruptible(&ns->wait);
5905d5182caSJohn Johansen }
5915d5182caSJohn Johansen 
592d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = {
593d9bf2c26SJohn Johansen 	.owner		= THIS_MODULE,
594d9bf2c26SJohn Johansen 	.open		= ns_revision_open,
595d9bf2c26SJohn Johansen 	.poll		= ns_revision_poll,
596d9bf2c26SJohn Johansen 	.read		= ns_revision_read,
597d9bf2c26SJohn Johansen 	.llseek		= generic_file_llseek,
598d9bf2c26SJohn Johansen 	.release	= ns_revision_release,
599d9bf2c26SJohn Johansen };
600d9bf2c26SJohn Johansen 
6014f3b3f2dSJohn Johansen static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
6024f3b3f2dSJohn Johansen 			     const char *match_str, size_t match_len)
6034f3b3f2dSJohn Johansen {
6044f3b3f2dSJohn Johansen 	struct aa_perms tmp;
6054f3b3f2dSJohn Johansen 	struct aa_dfa *dfa;
6064f3b3f2dSJohn Johansen 	unsigned int state = 0;
6074f3b3f2dSJohn Johansen 
608*637f688dSJohn Johansen 	if (profile_unconfined(profile))
6094f3b3f2dSJohn Johansen 		return;
6104f3b3f2dSJohn Johansen 	if (profile->file.dfa && *match_str == AA_CLASS_FILE) {
6114f3b3f2dSJohn Johansen 		dfa = profile->file.dfa;
6124f3b3f2dSJohn Johansen 		state = aa_dfa_match_len(dfa, profile->file.start,
6134f3b3f2dSJohn Johansen 					 match_str + 1, match_len - 1);
6144f3b3f2dSJohn Johansen 		tmp = nullperms;
6154f3b3f2dSJohn Johansen 		if (state) {
6164f3b3f2dSJohn Johansen 			struct path_cond cond = { };
6174f3b3f2dSJohn Johansen 
6184f3b3f2dSJohn Johansen 			tmp = aa_compute_fperms(dfa, state, &cond);
6194f3b3f2dSJohn Johansen 		}
6204f3b3f2dSJohn Johansen 	} else if (profile->policy.dfa) {
6214f3b3f2dSJohn Johansen 		if (!PROFILE_MEDIATES_SAFE(profile, *match_str))
6224f3b3f2dSJohn Johansen 			return;	/* no change to current perms */
6234f3b3f2dSJohn Johansen 		dfa = profile->policy.dfa;
6244f3b3f2dSJohn Johansen 		state = aa_dfa_match_len(dfa, profile->policy.start[0],
6254f3b3f2dSJohn Johansen 					 match_str, match_len);
6264f3b3f2dSJohn Johansen 		if (state)
6274f3b3f2dSJohn Johansen 			aa_compute_perms(dfa, state, &tmp);
6284f3b3f2dSJohn Johansen 		else
6294f3b3f2dSJohn Johansen 			tmp = nullperms;
6304f3b3f2dSJohn Johansen 	}
6314f3b3f2dSJohn Johansen 	aa_apply_modes_to_perms(profile, &tmp);
6324f3b3f2dSJohn Johansen }
6334f3b3f2dSJohn Johansen 
6344f3b3f2dSJohn Johansen 
635e025be0fSWilliam Hua /**
636e025be0fSWilliam Hua  * query_data - queries a policy and writes its data to buf
637e025be0fSWilliam Hua  * @buf: the resulting data is stored here (NOT NULL)
638e025be0fSWilliam Hua  * @buf_len: size of buf
639e025be0fSWilliam Hua  * @query: query string used to retrieve data
640e025be0fSWilliam Hua  * @query_len: size of query including second NUL byte
641e025be0fSWilliam Hua  *
642e025be0fSWilliam Hua  * The buffers pointed to by buf and query may overlap. The query buffer is
643e025be0fSWilliam Hua  * parsed before buf is written to.
644e025be0fSWilliam Hua  *
645e025be0fSWilliam Hua  * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
646e025be0fSWilliam Hua  * the security confinement context and <KEY> is the name of the data to
647e025be0fSWilliam Hua  * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
648e025be0fSWilliam Hua  *
649e025be0fSWilliam Hua  * Don't expect the contents of buf to be preserved on failure.
650e025be0fSWilliam Hua  *
651e025be0fSWilliam Hua  * Returns: number of characters written to buf or -errno on failure
652e025be0fSWilliam Hua  */
653e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len,
654e025be0fSWilliam Hua 			  char *query, size_t query_len)
655e025be0fSWilliam Hua {
656e025be0fSWilliam Hua 	char *out;
657e025be0fSWilliam Hua 	const char *key;
658*637f688dSJohn Johansen 	struct aa_label *label, *curr;
659e025be0fSWilliam Hua 	struct aa_data *data;
660e025be0fSWilliam Hua 	u32 bytes, blocks;
661e025be0fSWilliam Hua 	__le32 outle32;
662e025be0fSWilliam Hua 
663e025be0fSWilliam Hua 	if (!query_len)
664e025be0fSWilliam Hua 		return -EINVAL; /* need a query */
665e025be0fSWilliam Hua 
666e025be0fSWilliam Hua 	key = query + strnlen(query, query_len) + 1;
667e025be0fSWilliam Hua 	if (key + 1 >= query + query_len)
668e025be0fSWilliam Hua 		return -EINVAL; /* not enough space for a non-empty key */
669e025be0fSWilliam Hua 	if (key + strnlen(key, query + query_len - key) >= query + query_len)
670e025be0fSWilliam Hua 		return -EINVAL; /* must end with NUL */
671e025be0fSWilliam Hua 
672e025be0fSWilliam Hua 	if (buf_len < sizeof(bytes) + sizeof(blocks))
673e025be0fSWilliam Hua 		return -EINVAL; /* not enough space */
674e025be0fSWilliam Hua 
675*637f688dSJohn Johansen 	curr = begin_current_label_crit_section();
676*637f688dSJohn Johansen 	label = aa_label_parse(curr, query, GFP_KERNEL, false, false);
677*637f688dSJohn Johansen 	end_current_label_crit_section(curr);
678*637f688dSJohn Johansen 	if (IS_ERR(label))
679*637f688dSJohn Johansen 		return PTR_ERR(label);
680e025be0fSWilliam Hua 
681e025be0fSWilliam Hua 	/* We are going to leave space for two numbers. The first is the total
682e025be0fSWilliam Hua 	 * number of bytes we are writing after the first number. This is so
683e025be0fSWilliam Hua 	 * users can read the full output without reallocation.
684e025be0fSWilliam Hua 	 *
685e025be0fSWilliam Hua 	 * The second number is the number of data blocks we're writing. An
686e025be0fSWilliam Hua 	 * application might be confined by multiple policies having data in
687e025be0fSWilliam Hua 	 * the same key.
688e025be0fSWilliam Hua 	 */
689e025be0fSWilliam Hua 	memset(buf, 0, sizeof(bytes) + sizeof(blocks));
690e025be0fSWilliam Hua 	out = buf + sizeof(bytes) + sizeof(blocks);
691e025be0fSWilliam Hua 
692e025be0fSWilliam Hua 	blocks = 0;
693*637f688dSJohn Johansen 	if (labels_profile(label)->data) {
694*637f688dSJohn Johansen 		data = rhashtable_lookup_fast(labels_profile(label)->data, &key,
695*637f688dSJohn Johansen 					      labels_profile(label)->data->p);
696e025be0fSWilliam Hua 
697e025be0fSWilliam Hua 		if (data) {
698*637f688dSJohn Johansen 			if (out + sizeof(outle32) + data->size >
699*637f688dSJohn Johansen 			    buf + buf_len) {
700*637f688dSJohn Johansen 				aa_put_label(label);
701e025be0fSWilliam Hua 				return -EINVAL; /* not enough space */
702*637f688dSJohn Johansen 			}
703e025be0fSWilliam Hua 			outle32 = __cpu_to_le32(data->size);
704e025be0fSWilliam Hua 			memcpy(out, &outle32, sizeof(outle32));
705e025be0fSWilliam Hua 			out += sizeof(outle32);
706e025be0fSWilliam Hua 			memcpy(out, data->data, data->size);
707e025be0fSWilliam Hua 			out += data->size;
708e025be0fSWilliam Hua 			blocks++;
709e025be0fSWilliam Hua 		}
710e025be0fSWilliam Hua 	}
711*637f688dSJohn Johansen 	aa_put_label(label);
712e025be0fSWilliam Hua 
713e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
714e025be0fSWilliam Hua 	memcpy(buf, &outle32, sizeof(outle32));
715e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(blocks);
716e025be0fSWilliam Hua 	memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
717e025be0fSWilliam Hua 
718e025be0fSWilliam Hua 	return out - buf;
719e025be0fSWilliam Hua }
720e025be0fSWilliam Hua 
7214f3b3f2dSJohn Johansen /**
7224f3b3f2dSJohn Johansen  * query_label - queries a label and writes permissions to buf
7234f3b3f2dSJohn Johansen  * @buf: the resulting permissions string is stored here (NOT NULL)
7244f3b3f2dSJohn Johansen  * @buf_len: size of buf
7254f3b3f2dSJohn Johansen  * @query: binary query string to match against the dfa
7264f3b3f2dSJohn Johansen  * @query_len: size of query
7274f3b3f2dSJohn Johansen  * @view_only: only compute for querier's view
7284f3b3f2dSJohn Johansen  *
7294f3b3f2dSJohn Johansen  * The buffers pointed to by buf and query may overlap. The query buffer is
7304f3b3f2dSJohn Johansen  * parsed before buf is written to.
7314f3b3f2dSJohn Johansen  *
7324f3b3f2dSJohn Johansen  * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is
7334f3b3f2dSJohn Johansen  * the name of the label, in the current namespace, that is to be queried and
7344f3b3f2dSJohn Johansen  * DFA_STRING is a binary string to match against the label(s)'s DFA.
7354f3b3f2dSJohn Johansen  *
7364f3b3f2dSJohn Johansen  * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters
7374f3b3f2dSJohn Johansen  * but must *not* be NUL terminated.
7384f3b3f2dSJohn Johansen  *
7394f3b3f2dSJohn Johansen  * Returns: number of characters written to buf or -errno on failure
7404f3b3f2dSJohn Johansen  */
7414f3b3f2dSJohn Johansen static ssize_t query_label(char *buf, size_t buf_len,
7424f3b3f2dSJohn Johansen 			   char *query, size_t query_len, bool view_only)
7434f3b3f2dSJohn Johansen {
744*637f688dSJohn Johansen 	struct aa_label *label, *curr;
7454f3b3f2dSJohn Johansen 	char *label_name, *match_str;
7464f3b3f2dSJohn Johansen 	size_t label_name_len, match_len;
7474f3b3f2dSJohn Johansen 	struct aa_perms perms;
7484f3b3f2dSJohn Johansen 
7494f3b3f2dSJohn Johansen 	if (!query_len)
7504f3b3f2dSJohn Johansen 		return -EINVAL;
7514f3b3f2dSJohn Johansen 
7524f3b3f2dSJohn Johansen 	label_name = query;
7534f3b3f2dSJohn Johansen 	label_name_len = strnlen(query, query_len);
7544f3b3f2dSJohn Johansen 	if (!label_name_len || label_name_len == query_len)
7554f3b3f2dSJohn Johansen 		return -EINVAL;
7564f3b3f2dSJohn Johansen 
7574f3b3f2dSJohn Johansen 	/**
7584f3b3f2dSJohn Johansen 	 * The extra byte is to account for the null byte between the
7594f3b3f2dSJohn Johansen 	 * profile name and dfa string. profile_name_len is greater
7604f3b3f2dSJohn Johansen 	 * than zero and less than query_len, so a byte can be safely
7614f3b3f2dSJohn Johansen 	 * added or subtracted.
7624f3b3f2dSJohn Johansen 	 */
7634f3b3f2dSJohn Johansen 	match_str = label_name + label_name_len + 1;
7644f3b3f2dSJohn Johansen 	match_len = query_len - label_name_len - 1;
7654f3b3f2dSJohn Johansen 
766*637f688dSJohn Johansen 	curr = begin_current_label_crit_section();
767*637f688dSJohn Johansen 	label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false);
768*637f688dSJohn Johansen 	end_current_label_crit_section(curr);
769*637f688dSJohn Johansen 	if (IS_ERR(label))
770*637f688dSJohn Johansen 		return PTR_ERR(label);
7714f3b3f2dSJohn Johansen 
7724f3b3f2dSJohn Johansen 	perms = allperms;
773*637f688dSJohn Johansen 	profile_query_cb(labels_profile(label), &perms, match_str, match_len);
7744f3b3f2dSJohn Johansen 
7754f3b3f2dSJohn Johansen 	return scnprintf(buf, buf_len,
7764f3b3f2dSJohn Johansen 		      "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n",
7774f3b3f2dSJohn Johansen 		      perms.allow, perms.deny, perms.audit, perms.quiet);
7784f3b3f2dSJohn Johansen }
7794f3b3f2dSJohn Johansen 
7801dea3b41SJohn Johansen /*
7811dea3b41SJohn Johansen  * Transaction based IO.
7821dea3b41SJohn Johansen  * The file expects a write which triggers the transaction, and then
7831dea3b41SJohn Johansen  * possibly a read(s) which collects the result - which is stored in a
7841dea3b41SJohn Johansen  * file-local buffer. Once a new write is performed, a new set of results
7851dea3b41SJohn Johansen  * are stored in the file-local buffer.
7861dea3b41SJohn Johansen  */
7871dea3b41SJohn Johansen struct multi_transaction {
7881dea3b41SJohn Johansen 	struct kref count;
7891dea3b41SJohn Johansen 	ssize_t size;
7901dea3b41SJohn Johansen 	char data[0];
7911dea3b41SJohn Johansen };
7921dea3b41SJohn Johansen 
7931dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction))
7941dea3b41SJohn Johansen /* TODO: replace with per file lock */
7951dea3b41SJohn Johansen static DEFINE_SPINLOCK(multi_transaction_lock);
7961dea3b41SJohn Johansen 
7971dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref)
7981dea3b41SJohn Johansen {
7991dea3b41SJohn Johansen 	struct multi_transaction *t;
8001dea3b41SJohn Johansen 
8011dea3b41SJohn Johansen 	t = container_of(kref, struct multi_transaction, count);
8021dea3b41SJohn Johansen 	free_page((unsigned long) t);
8031dea3b41SJohn Johansen }
8041dea3b41SJohn Johansen 
8051dea3b41SJohn Johansen static struct multi_transaction *
8061dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t)
8071dea3b41SJohn Johansen {
8081dea3b41SJohn Johansen 	if  (t)
8091dea3b41SJohn Johansen 		kref_get(&(t->count));
8101dea3b41SJohn Johansen 
8111dea3b41SJohn Johansen 	return t;
8121dea3b41SJohn Johansen }
8131dea3b41SJohn Johansen 
8141dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t)
8151dea3b41SJohn Johansen {
8161dea3b41SJohn Johansen 	if (t)
8171dea3b41SJohn Johansen 		kref_put(&(t->count), multi_transaction_kref);
8181dea3b41SJohn Johansen }
8191dea3b41SJohn Johansen 
8201dea3b41SJohn Johansen /* does not increment @new's count */
8211dea3b41SJohn Johansen static void multi_transaction_set(struct file *file,
8221dea3b41SJohn Johansen 				  struct multi_transaction *new, size_t n)
8231dea3b41SJohn Johansen {
8241dea3b41SJohn Johansen 	struct multi_transaction *old;
8251dea3b41SJohn Johansen 
8261dea3b41SJohn Johansen 	AA_BUG(n > MULTI_TRANSACTION_LIMIT);
8271dea3b41SJohn Johansen 
8281dea3b41SJohn Johansen 	new->size = n;
8291dea3b41SJohn Johansen 	spin_lock(&multi_transaction_lock);
8301dea3b41SJohn Johansen 	old = (struct multi_transaction *) file->private_data;
8311dea3b41SJohn Johansen 	file->private_data = new;
8321dea3b41SJohn Johansen 	spin_unlock(&multi_transaction_lock);
8331dea3b41SJohn Johansen 	put_multi_transaction(old);
8341dea3b41SJohn Johansen }
8351dea3b41SJohn Johansen 
8361dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file,
8371dea3b41SJohn Johansen 						       const char __user *buf,
8381dea3b41SJohn Johansen 						       size_t size)
8391dea3b41SJohn Johansen {
8401dea3b41SJohn Johansen 	struct multi_transaction *t;
8411dea3b41SJohn Johansen 
8421dea3b41SJohn Johansen 	if (size > MULTI_TRANSACTION_LIMIT - 1)
8431dea3b41SJohn Johansen 		return ERR_PTR(-EFBIG);
8441dea3b41SJohn Johansen 
8451dea3b41SJohn Johansen 	t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
8461dea3b41SJohn Johansen 	if (!t)
8471dea3b41SJohn Johansen 		return ERR_PTR(-ENOMEM);
8481dea3b41SJohn Johansen 	kref_init(&t->count);
8491dea3b41SJohn Johansen 	if (copy_from_user(t->data, buf, size))
8501dea3b41SJohn Johansen 		return ERR_PTR(-EFAULT);
8511dea3b41SJohn Johansen 
8521dea3b41SJohn Johansen 	return t;
8531dea3b41SJohn Johansen }
8541dea3b41SJohn Johansen 
8551dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf,
8561dea3b41SJohn Johansen 				       size_t size, loff_t *pos)
8571dea3b41SJohn Johansen {
8581dea3b41SJohn Johansen 	struct multi_transaction *t;
8591dea3b41SJohn Johansen 	ssize_t ret;
8601dea3b41SJohn Johansen 
8611dea3b41SJohn Johansen 	spin_lock(&multi_transaction_lock);
8621dea3b41SJohn Johansen 	t = get_multi_transaction(file->private_data);
8631dea3b41SJohn Johansen 	spin_unlock(&multi_transaction_lock);
8641dea3b41SJohn Johansen 	if (!t)
8651dea3b41SJohn Johansen 		return 0;
8661dea3b41SJohn Johansen 
8671dea3b41SJohn Johansen 	ret = simple_read_from_buffer(buf, size, pos, t->data, t->size);
8681dea3b41SJohn Johansen 	put_multi_transaction(t);
8691dea3b41SJohn Johansen 
8701dea3b41SJohn Johansen 	return ret;
8711dea3b41SJohn Johansen }
8721dea3b41SJohn Johansen 
8731dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file)
8741dea3b41SJohn Johansen {
8751dea3b41SJohn Johansen 	put_multi_transaction(file->private_data);
8761dea3b41SJohn Johansen 
8771dea3b41SJohn Johansen 	return 0;
8781dea3b41SJohn Johansen }
8791dea3b41SJohn Johansen 
8804f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE	"profile\0"
8814f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE_LEN	8
8824f3b3f2dSJohn Johansen 
883e025be0fSWilliam Hua #define QUERY_CMD_DATA		"data\0"
884e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN	5
885e025be0fSWilliam Hua 
886e025be0fSWilliam Hua /**
887e025be0fSWilliam Hua  * aa_write_access - generic permissions and data query
888e025be0fSWilliam Hua  * @file: pointer to open apparmorfs/access file
889e025be0fSWilliam Hua  * @ubuf: user buffer containing the complete query string (NOT NULL)
890e025be0fSWilliam Hua  * @count: size of ubuf
891e025be0fSWilliam Hua  * @ppos: position in the file (MUST BE ZERO)
892e025be0fSWilliam Hua  *
893e025be0fSWilliam Hua  * Allows for one permissions or data query per open(), write(), and read()
894e025be0fSWilliam Hua  * sequence. The only queries currently supported are label-based queries for
895e025be0fSWilliam Hua  * permissions or data.
896e025be0fSWilliam Hua  *
897e025be0fSWilliam Hua  * For permissions queries, ubuf must begin with "label\0", followed by the
898e025be0fSWilliam Hua  * profile query specific format described in the query_label() function
899e025be0fSWilliam Hua  * documentation.
900e025be0fSWilliam Hua  *
901e025be0fSWilliam Hua  * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
902e025be0fSWilliam Hua  * <LABEL> is the name of the security confinement context and <KEY> is the
903e025be0fSWilliam Hua  * name of the data to retrieve.
904e025be0fSWilliam Hua  *
905e025be0fSWilliam Hua  * Returns: number of bytes written or -errno on failure
906e025be0fSWilliam Hua  */
907e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
908e025be0fSWilliam Hua 			       size_t count, loff_t *ppos)
909e025be0fSWilliam Hua {
9101dea3b41SJohn Johansen 	struct multi_transaction *t;
911e025be0fSWilliam Hua 	ssize_t len;
912e025be0fSWilliam Hua 
913e025be0fSWilliam Hua 	if (*ppos)
914e025be0fSWilliam Hua 		return -ESPIPE;
915e025be0fSWilliam Hua 
9161dea3b41SJohn Johansen 	t = multi_transaction_new(file, ubuf, count);
9171dea3b41SJohn Johansen 	if (IS_ERR(t))
9181dea3b41SJohn Johansen 		return PTR_ERR(t);
919e025be0fSWilliam Hua 
9204f3b3f2dSJohn Johansen 	if (count > QUERY_CMD_PROFILE_LEN &&
9214f3b3f2dSJohn Johansen 	    !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) {
9224f3b3f2dSJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
9234f3b3f2dSJohn Johansen 				  t->data + QUERY_CMD_PROFILE_LEN,
9244f3b3f2dSJohn Johansen 				  count - QUERY_CMD_PROFILE_LEN, true);
9254f3b3f2dSJohn Johansen 	} else if (count > QUERY_CMD_DATA_LEN &&
9261dea3b41SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
9271dea3b41SJohn Johansen 		len = query_data(t->data, MULTI_TRANSACTION_LIMIT,
9281dea3b41SJohn Johansen 				 t->data + QUERY_CMD_DATA_LEN,
929e025be0fSWilliam Hua 				 count - QUERY_CMD_DATA_LEN);
930e025be0fSWilliam Hua 	} else
931e025be0fSWilliam Hua 		len = -EINVAL;
932e025be0fSWilliam Hua 
9331dea3b41SJohn Johansen 	if (len < 0) {
9341dea3b41SJohn Johansen 		put_multi_transaction(t);
935e025be0fSWilliam Hua 		return len;
9361dea3b41SJohn Johansen 	}
937e025be0fSWilliam Hua 
9381dea3b41SJohn Johansen 	multi_transaction_set(file, t, len);
939e025be0fSWilliam Hua 
940e025be0fSWilliam Hua 	return count;
941e025be0fSWilliam Hua }
942e025be0fSWilliam Hua 
943c97204baSJohn Johansen static const struct file_operations aa_sfs_access = {
944e025be0fSWilliam Hua 	.write		= aa_write_access,
9451dea3b41SJohn Johansen 	.read		= multi_transaction_read,
9461dea3b41SJohn Johansen 	.release	= multi_transaction_release,
947e025be0fSWilliam Hua 	.llseek		= generic_file_llseek,
948e025be0fSWilliam Hua };
949e025be0fSWilliam Hua 
950c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v)
951e74abcf3SKees Cook {
952c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file = seq->private;
953e74abcf3SKees Cook 
954e74abcf3SKees Cook 	if (!fs_file)
955e74abcf3SKees Cook 		return 0;
956e74abcf3SKees Cook 
957e74abcf3SKees Cook 	switch (fs_file->v_type) {
958c97204baSJohn Johansen 	case AA_SFS_TYPE_BOOLEAN:
959e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
960e74abcf3SKees Cook 		break;
961c97204baSJohn Johansen 	case AA_SFS_TYPE_STRING:
962a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
963a9bf8e9fSKees Cook 		break;
964c97204baSJohn Johansen 	case AA_SFS_TYPE_U64:
965e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
966e74abcf3SKees Cook 		break;
967e74abcf3SKees Cook 	default:
968e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
969e74abcf3SKees Cook 		break;
970e74abcf3SKees Cook 	}
971e74abcf3SKees Cook 
972e74abcf3SKees Cook 	return 0;
973e74abcf3SKees Cook }
974e74abcf3SKees Cook 
975c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file)
976e74abcf3SKees Cook {
977c97204baSJohn Johansen 	return single_open(file, aa_sfs_seq_show, inode->i_private);
978e74abcf3SKees Cook }
979e74abcf3SKees Cook 
980c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = {
981e74abcf3SKees Cook 	.owner		= THIS_MODULE,
982c97204baSJohn Johansen 	.open		= aa_sfs_seq_open,
983e74abcf3SKees Cook 	.read		= seq_read,
984e74abcf3SKees Cook 	.llseek		= seq_lseek,
985e74abcf3SKees Cook 	.release	= single_release,
986e74abcf3SKees Cook };
987e74abcf3SKees Cook 
98852b97de3SJohn Johansen /*
98952b97de3SJohn Johansen  * profile based file operations
99052b97de3SJohn Johansen  *     policy/profiles/XXXX/profiles/ *
99152b97de3SJohn Johansen  */
99252b97de3SJohn Johansen 
99352b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME)						      \
99452b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
99552b97de3SJohn Johansen {									      \
99652b97de3SJohn Johansen 	return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show);    \
99752b97de3SJohn Johansen }									      \
99852b97de3SJohn Johansen 									      \
99952b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = {	      \
100052b97de3SJohn Johansen 	.owner		= THIS_MODULE,					      \
100152b97de3SJohn Johansen 	.open		= seq_profile_ ##NAME ##_open,			      \
100252b97de3SJohn Johansen 	.read		= seq_read,					      \
100352b97de3SJohn Johansen 	.llseek		= seq_lseek,					      \
100452b97de3SJohn Johansen 	.release	= seq_profile_release,				      \
100552b97de3SJohn Johansen }									      \
100652b97de3SJohn Johansen 
100752b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file,
10080d259f04SJohn Johansen 			    int (*show)(struct seq_file *, void *))
10090d259f04SJohn Johansen {
10108399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
10118399588aSJohn Johansen 	int error = single_open(file, show, proxy);
101263e2b423SJohn Johansen 
10130d259f04SJohn Johansen 	if (error) {
10140d259f04SJohn Johansen 		file->private_data = NULL;
10158399588aSJohn Johansen 		aa_put_proxy(proxy);
10160d259f04SJohn Johansen 	}
10170d259f04SJohn Johansen 
10180d259f04SJohn Johansen 	return error;
10190d259f04SJohn Johansen }
10200d259f04SJohn Johansen 
102152b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file)
10220d259f04SJohn Johansen {
10230d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
10240d259f04SJohn Johansen 	if (seq)
10258399588aSJohn Johansen 		aa_put_proxy(seq->private);
10260d259f04SJohn Johansen 	return single_release(inode, file);
10270d259f04SJohn Johansen }
10280d259f04SJohn Johansen 
102952b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v)
10300d259f04SJohn Johansen {
10318399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1032*637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1033*637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
10340d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
1035*637f688dSJohn Johansen 	aa_put_label(label);
10360d259f04SJohn Johansen 
10370d259f04SJohn Johansen 	return 0;
10380d259f04SJohn Johansen }
10390d259f04SJohn Johansen 
104052b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v)
10410d259f04SJohn Johansen {
10428399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1043*637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1044*637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
10450d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
1046*637f688dSJohn Johansen 	aa_put_label(label);
10470d259f04SJohn Johansen 
10480d259f04SJohn Johansen 	return 0;
10490d259f04SJohn Johansen }
10500d259f04SJohn Johansen 
105152b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v)
1052556d0be7SJohn Johansen {
10538399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1054*637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1055*637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
1056556d0be7SJohn Johansen 	if (profile->attach)
1057556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
1058556d0be7SJohn Johansen 	else if (profile->xmatch)
1059556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
1060556d0be7SJohn Johansen 	else
1061556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
1062*637f688dSJohn Johansen 	aa_put_label(label);
1063556d0be7SJohn Johansen 
1064556d0be7SJohn Johansen 	return 0;
1065556d0be7SJohn Johansen }
1066556d0be7SJohn Johansen 
106752b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v)
1068f8eb8a13SJohn Johansen {
10698399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1070*637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1071*637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
1072f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
1073f8eb8a13SJohn Johansen 
1074f8eb8a13SJohn Johansen 	if (profile->hash) {
1075f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
1076f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
107747dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
1078f8eb8a13SJohn Johansen 	}
1079*637f688dSJohn Johansen 	aa_put_label(label);
1080f8eb8a13SJohn Johansen 
1081f8eb8a13SJohn Johansen 	return 0;
1082f8eb8a13SJohn Johansen }
1083f8eb8a13SJohn Johansen 
108452b97de3SJohn Johansen SEQ_PROFILE_FOPS(name);
108552b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode);
108652b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach);
108752b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash);
1088f8eb8a13SJohn Johansen 
108964c86970SJohn Johansen /*
109064c86970SJohn Johansen  * namespace based files
109164c86970SJohn Johansen  *     several root files and
109264c86970SJohn Johansen  *     policy/ *
109364c86970SJohn Johansen  */
1094f8eb8a13SJohn Johansen 
109564c86970SJohn Johansen #define SEQ_NS_FOPS(NAME)						      \
109664c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file)     \
109764c86970SJohn Johansen {									      \
109864c86970SJohn Johansen 	return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private);   \
109964c86970SJohn Johansen }									      \
110064c86970SJohn Johansen 									      \
110164c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = {	      \
110264c86970SJohn Johansen 	.owner		= THIS_MODULE,					      \
110364c86970SJohn Johansen 	.open		= seq_ns_ ##NAME ##_open,			      \
110464c86970SJohn Johansen 	.read		= seq_read,					      \
110564c86970SJohn Johansen 	.llseek		= seq_lseek,					      \
110664c86970SJohn Johansen 	.release	= single_release,				      \
110764c86970SJohn Johansen }									      \
11083e3e5695SJohn Johansen 
110964c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v)
1110a71ada30SJohn Johansen {
1111*637f688dSJohn Johansen 	struct aa_label *label;
1112a71ada30SJohn Johansen 
1113*637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1114*637f688dSJohn Johansen 	seq_printf(seq, "%d\n", labels_ns(label)->level);
1115*637f688dSJohn Johansen 	end_current_label_crit_section(label);
1116a71ada30SJohn Johansen 
1117a71ada30SJohn Johansen 	return 0;
1118a71ada30SJohn Johansen }
1119a71ada30SJohn Johansen 
112064c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v)
11213e3e5695SJohn Johansen {
1122*637f688dSJohn Johansen 	struct aa_label *label = begin_current_label_crit_section();
11233e3e5695SJohn Johansen 
1124*637f688dSJohn Johansen 	seq_printf(seq, "%s\n", aa_ns_name(labels_ns(label),
1125*637f688dSJohn Johansen 					   labels_ns(label), true));
1126*637f688dSJohn Johansen 	end_current_label_crit_section(label);
11273e3e5695SJohn Johansen 
11283e3e5695SJohn Johansen 	return 0;
11293e3e5695SJohn Johansen }
11303e3e5695SJohn Johansen 
113164c86970SJohn Johansen SEQ_NS_FOPS(level);
113264c86970SJohn Johansen SEQ_NS_FOPS(name);
11333e3e5695SJohn Johansen 
11345d5182caSJohn Johansen 
11355d5182caSJohn Johansen /* policy/raw_data/ * file ops */
11365d5182caSJohn Johansen 
11375d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME)						      \
11385d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
11395d5182caSJohn Johansen {									      \
11405d5182caSJohn Johansen 	return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show);    \
11415d5182caSJohn Johansen }									      \
11425d5182caSJohn Johansen 									      \
11435d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = {	      \
11445d5182caSJohn Johansen 	.owner		= THIS_MODULE,					      \
11455d5182caSJohn Johansen 	.open		= seq_rawdata_ ##NAME ##_open,			      \
11465d5182caSJohn Johansen 	.read		= seq_read,					      \
11475d5182caSJohn Johansen 	.llseek		= seq_lseek,					      \
11485d5182caSJohn Johansen 	.release	= seq_rawdata_release,				      \
11495d5182caSJohn Johansen }									      \
11505d5182caSJohn Johansen 
11515d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file,
11525d5182caSJohn Johansen 			    int (*show)(struct seq_file *, void *))
11535ac8c355SJohn Johansen {
11545d5182caSJohn Johansen 	struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
11555d5182caSJohn Johansen 	int error;
11565d5182caSJohn Johansen 
11575d5182caSJohn Johansen 	if (!data)
11585d5182caSJohn Johansen 		/* lost race this ent is being reaped */
11595d5182caSJohn Johansen 		return -ENOENT;
11605d5182caSJohn Johansen 
11615d5182caSJohn Johansen 	error = single_open(file, show, data);
11625d5182caSJohn Johansen 	if (error) {
11635d5182caSJohn Johansen 		AA_BUG(file->private_data &&
11645d5182caSJohn Johansen 		       ((struct seq_file *)file->private_data)->private);
11655d5182caSJohn Johansen 		aa_put_loaddata(data);
11665d5182caSJohn Johansen 	}
11675d5182caSJohn Johansen 
11685d5182caSJohn Johansen 	return error;
11695d5182caSJohn Johansen }
11705d5182caSJohn Johansen 
11715d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file)
11725d5182caSJohn Johansen {
11735d5182caSJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
11745d5182caSJohn Johansen 
11755d5182caSJohn Johansen 	if (seq)
11765d5182caSJohn Johansen 		aa_put_loaddata(seq->private);
11775d5182caSJohn Johansen 
11785d5182caSJohn Johansen 	return single_release(inode, file);
11795d5182caSJohn Johansen }
11805d5182caSJohn Johansen 
11815d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
11825d5182caSJohn Johansen {
11835d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
11845d5182caSJohn Johansen 
11855d5182caSJohn Johansen 	seq_printf(seq, "v%d\n", data->abi);
11865ac8c355SJohn Johansen 
11875ac8c355SJohn Johansen 	return 0;
11885ac8c355SJohn Johansen }
11895ac8c355SJohn Johansen 
11905d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
11915ac8c355SJohn Johansen {
11925d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
11935ac8c355SJohn Johansen 
11945d5182caSJohn Johansen 	seq_printf(seq, "%ld\n", data->revision);
11955ac8c355SJohn Johansen 
11965ac8c355SJohn Johansen 	return 0;
11975ac8c355SJohn Johansen }
11985ac8c355SJohn Johansen 
11995d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
12005ac8c355SJohn Johansen {
12015d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12025ac8c355SJohn Johansen 	unsigned int i, size = aa_hash_size();
12035ac8c355SJohn Johansen 
12045d5182caSJohn Johansen 	if (data->hash) {
12055ac8c355SJohn Johansen 		for (i = 0; i < size; i++)
12065d5182caSJohn Johansen 			seq_printf(seq, "%.2x", data->hash[i]);
120747dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
12085ac8c355SJohn Johansen 	}
12095ac8c355SJohn Johansen 
12105ac8c355SJohn Johansen 	return 0;
12115ac8c355SJohn Johansen }
12125ac8c355SJohn Johansen 
12135d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi);
12145d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision);
12155d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash);
12165ac8c355SJohn Johansen 
12175ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
12185ac8c355SJohn Johansen 			    loff_t *ppos)
12195ac8c355SJohn Johansen {
12205ac8c355SJohn Johansen 	struct aa_loaddata *rawdata = file->private_data;
12215ac8c355SJohn Johansen 
12225ac8c355SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, rawdata->data,
12235ac8c355SJohn Johansen 				       rawdata->size);
12245ac8c355SJohn Johansen }
12255ac8c355SJohn Johansen 
12265d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file)
12275ac8c355SJohn Johansen {
12285d5182caSJohn Johansen 	aa_put_loaddata(file->private_data);
12295ac8c355SJohn Johansen 
12305ac8c355SJohn Johansen 	return 0;
12315ac8c355SJohn Johansen }
12325ac8c355SJohn Johansen 
12335d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file)
12345d5182caSJohn Johansen {
12355d5182caSJohn Johansen 	if (!policy_view_capable(NULL))
12365d5182caSJohn Johansen 		return -EACCES;
12375d5182caSJohn Johansen 	file->private_data = __aa_get_loaddata(inode->i_private);
12385d5182caSJohn Johansen 	if (!file->private_data)
12395d5182caSJohn Johansen 		/* lost race: this entry is being reaped */
12405d5182caSJohn Johansen 		return -ENOENT;
12415d5182caSJohn Johansen 
12425d5182caSJohn Johansen 	return 0;
12435d5182caSJohn Johansen }
12445d5182caSJohn Johansen 
12455d5182caSJohn Johansen static const struct file_operations rawdata_fops = {
12465ac8c355SJohn Johansen 	.open = rawdata_open,
12475ac8c355SJohn Johansen 	.read = rawdata_read,
12485ac8c355SJohn Johansen 	.llseek = generic_file_llseek,
12495ac8c355SJohn Johansen 	.release = rawdata_release,
12505ac8c355SJohn Johansen };
12515ac8c355SJohn Johansen 
12525d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata)
12535d5182caSJohn Johansen {
12545d5182caSJohn Johansen 	int i;
12555d5182caSJohn Johansen 
12565d5182caSJohn Johansen 	for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
12575d5182caSJohn Johansen 		if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
12585d5182caSJohn Johansen 			/* no refcounts on i_private */
1259c961ee5fSJohn Johansen 			aafs_remove(rawdata->dents[i]);
12605d5182caSJohn Johansen 			rawdata->dents[i] = NULL;
12615d5182caSJohn Johansen 		}
12625d5182caSJohn Johansen 	}
12635d5182caSJohn Johansen }
12645d5182caSJohn Johansen 
12655d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
12665d5182caSJohn Johansen {
12675d5182caSJohn Johansen 	AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
12685d5182caSJohn Johansen 
12695d5182caSJohn Johansen 	if (rawdata->ns) {
12705d5182caSJohn Johansen 		remove_rawdata_dents(rawdata);
12715d5182caSJohn Johansen 		list_del_init(&rawdata->list);
12725d5182caSJohn Johansen 		aa_put_ns(rawdata->ns);
12735d5182caSJohn Johansen 		rawdata->ns = NULL;
12745d5182caSJohn Johansen 	}
12755d5182caSJohn Johansen }
12765d5182caSJohn Johansen 
12775d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
12785d5182caSJohn Johansen {
12795d5182caSJohn Johansen 	struct dentry *dent, *dir;
12805d5182caSJohn Johansen 
12815d5182caSJohn Johansen 	AA_BUG(!ns);
12825d5182caSJohn Johansen 	AA_BUG(!rawdata);
12835d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
12845d5182caSJohn Johansen 	AA_BUG(!ns_subdata_dir(ns));
12855d5182caSJohn Johansen 
12865d5182caSJohn Johansen 	/*
12875d5182caSJohn Johansen 	 * just use ns revision dir was originally created at. This is
12885d5182caSJohn Johansen 	 * under ns->lock and if load is successful revision will be
12895d5182caSJohn Johansen 	 * bumped and is guaranteed to be unique
12905d5182caSJohn Johansen 	 */
12915d5182caSJohn Johansen 	rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
12925d5182caSJohn Johansen 	if (!rawdata->name)
12935d5182caSJohn Johansen 		return -ENOMEM;
12945d5182caSJohn Johansen 
1295c961ee5fSJohn Johansen 	dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
12965d5182caSJohn Johansen 	if (IS_ERR(dir))
12975d5182caSJohn Johansen 		/* ->name freed when rawdata freed */
12985d5182caSJohn Johansen 		return PTR_ERR(dir);
12995d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DIR] = dir;
13005d5182caSJohn Johansen 
1301c961ee5fSJohn Johansen 	dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
13025d5182caSJohn Johansen 				      &seq_rawdata_abi_fops);
13035d5182caSJohn Johansen 	if (IS_ERR(dent))
13045d5182caSJohn Johansen 		goto fail;
13055d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_ABI] = dent;
13065d5182caSJohn Johansen 
1307c961ee5fSJohn Johansen 	dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
13085d5182caSJohn Johansen 				      &seq_rawdata_revision_fops);
13095d5182caSJohn Johansen 	if (IS_ERR(dent))
13105d5182caSJohn Johansen 		goto fail;
13115d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
13125d5182caSJohn Johansen 
13135d5182caSJohn Johansen 	if (aa_g_hash_policy) {
1314c961ee5fSJohn Johansen 		dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
13155d5182caSJohn Johansen 					      rawdata, &seq_rawdata_hash_fops);
13165d5182caSJohn Johansen 		if (IS_ERR(dent))
13175d5182caSJohn Johansen 			goto fail;
13185d5182caSJohn Johansen 		rawdata->dents[AAFS_LOADDATA_HASH] = dent;
13195d5182caSJohn Johansen 	}
13205d5182caSJohn Johansen 
1321c961ee5fSJohn Johansen 	dent = aafs_create_file("raw_data", S_IFREG | 0444,
13225d5182caSJohn Johansen 				      dir, rawdata, &rawdata_fops);
13235d5182caSJohn Johansen 	if (IS_ERR(dent))
13245d5182caSJohn Johansen 		goto fail;
13255d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DATA] = dent;
13265d5182caSJohn Johansen 	d_inode(dent)->i_size = rawdata->size;
13275d5182caSJohn Johansen 
13285d5182caSJohn Johansen 	rawdata->ns = aa_get_ns(ns);
13295d5182caSJohn Johansen 	list_add(&rawdata->list, &ns->rawdata_list);
13305d5182caSJohn Johansen 	/* no refcount on inode rawdata */
13315d5182caSJohn Johansen 
13325d5182caSJohn Johansen 	return 0;
13335d5182caSJohn Johansen 
13345d5182caSJohn Johansen fail:
13355d5182caSJohn Johansen 	remove_rawdata_dents(rawdata);
13365d5182caSJohn Johansen 
13375d5182caSJohn Johansen 	return PTR_ERR(dent);
13385d5182caSJohn Johansen }
13395d5182caSJohn Johansen 
13400d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
1341c97204baSJohn Johansen 
1342c97204baSJohn Johansen /**
1343c97204baSJohn Johansen  *
1344c97204baSJohn Johansen  * Requires: @profile->ns->lock held
1345c97204baSJohn Johansen  */
1346c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile)
13470d259f04SJohn Johansen {
13480d259f04SJohn Johansen 	struct aa_profile *child;
13490d259f04SJohn Johansen 	int i;
13500d259f04SJohn Johansen 
13510d259f04SJohn Johansen 	if (!profile)
13520d259f04SJohn Johansen 		return;
13530d259f04SJohn Johansen 
13540d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
1355c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
13560d259f04SJohn Johansen 
13570d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
13588399588aSJohn Johansen 		struct aa_proxy *proxy;
13590d259f04SJohn Johansen 		if (!profile->dents[i])
13600d259f04SJohn Johansen 			continue;
13610d259f04SJohn Johansen 
13628399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
1363c961ee5fSJohn Johansen 		aafs_remove(profile->dents[i]);
13648399588aSJohn Johansen 		aa_put_proxy(proxy);
13650d259f04SJohn Johansen 		profile->dents[i] = NULL;
13660d259f04SJohn Johansen 	}
13670d259f04SJohn Johansen }
13680d259f04SJohn Johansen 
1369c97204baSJohn Johansen /**
1370c97204baSJohn Johansen  *
1371c97204baSJohn Johansen  * Requires: @old->ns->lock held
1372c97204baSJohn Johansen  */
1373c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old,
13740d259f04SJohn Johansen 				  struct aa_profile *new)
13750d259f04SJohn Johansen {
13760d259f04SJohn Johansen 	int i;
13770d259f04SJohn Johansen 
13780d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
13790d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
1380d671e890SJohn Johansen 		if (new->dents[i])
1381078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
13820d259f04SJohn Johansen 		old->dents[i] = NULL;
13830d259f04SJohn Johansen 	}
13840d259f04SJohn Johansen }
13850d259f04SJohn Johansen 
13860d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
13870d259f04SJohn Johansen 					  struct aa_profile *profile,
13880d259f04SJohn Johansen 					  const struct file_operations *fops)
13890d259f04SJohn Johansen {
1390*637f688dSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
13910d259f04SJohn Johansen 	struct dentry *dent;
13920d259f04SJohn Johansen 
1393c961ee5fSJohn Johansen 	dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
13940d259f04SJohn Johansen 	if (IS_ERR(dent))
13958399588aSJohn Johansen 		aa_put_proxy(proxy);
13960d259f04SJohn Johansen 
13970d259f04SJohn Johansen 	return dent;
13980d259f04SJohn Johansen }
13990d259f04SJohn Johansen 
14005d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile)
14015d5182caSJohn Johansen {
14025d5182caSJohn Johansen 	int depth = 0;
14035d5182caSJohn Johansen 
14045d5182caSJohn Johansen 	rcu_read_lock();
14055d5182caSJohn Johansen 	for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
14065d5182caSJohn Johansen 		depth++;
14075d5182caSJohn Johansen 	rcu_read_unlock();
14085d5182caSJohn Johansen 
14095d5182caSJohn Johansen 	return depth;
14105d5182caSJohn Johansen }
14115d5182caSJohn Johansen 
14125d5182caSJohn Johansen static int gen_symlink_name(char *buffer, size_t bsize, int depth,
14135d5182caSJohn Johansen 			    const char *dirname, const char *fname)
14145d5182caSJohn Johansen {
14155d5182caSJohn Johansen 	int error;
14165d5182caSJohn Johansen 
14175d5182caSJohn Johansen 	for (; depth > 0; depth--) {
14185d5182caSJohn Johansen 		if (bsize < 7)
14195d5182caSJohn Johansen 			return -ENAMETOOLONG;
14205d5182caSJohn Johansen 		strcpy(buffer, "../../");
14215d5182caSJohn Johansen 		buffer += 6;
14225d5182caSJohn Johansen 		bsize -= 6;
14235d5182caSJohn Johansen 	}
14245d5182caSJohn Johansen 
14255d5182caSJohn Johansen 	error = snprintf(buffer, bsize, "raw_data/%s/%s", dirname, fname);
14265d5182caSJohn Johansen 	if (error >= bsize || error < 0)
14275d5182caSJohn Johansen 		return -ENAMETOOLONG;
14285d5182caSJohn Johansen 
14295d5182caSJohn Johansen 	return 0;
14305d5182caSJohn Johansen }
14315d5182caSJohn Johansen 
14325d5182caSJohn Johansen /*
14335d5182caSJohn Johansen  * Requires: @profile->ns->lock held
14345d5182caSJohn Johansen  */
1435c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
14360d259f04SJohn Johansen {
14370d259f04SJohn Johansen 	struct aa_profile *child;
14380d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
14390d259f04SJohn Johansen 	int error;
14400d259f04SJohn Johansen 
14410d259f04SJohn Johansen 	if (!parent) {
14420d259f04SJohn Johansen 		struct aa_profile *p;
14430d259f04SJohn Johansen 		p = aa_deref_parent(profile);
14440d259f04SJohn Johansen 		dent = prof_dir(p);
14450d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
1446c961ee5fSJohn Johansen 		dent = aafs_create_dir("profiles", dent);
14470d259f04SJohn Johansen 		if (IS_ERR(dent))
14480d259f04SJohn Johansen 			goto fail;
14490d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
14500d259f04SJohn Johansen 	}
14510d259f04SJohn Johansen 
14520d259f04SJohn Johansen 	if (!profile->dirname) {
14530d259f04SJohn Johansen 		int len, id_len;
14540d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
14550d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
14560d259f04SJohn Johansen 
14570d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
1458ffac1de6SDan Carpenter 		if (!profile->dirname) {
1459ffac1de6SDan Carpenter 			error = -ENOMEM;
1460ffac1de6SDan Carpenter 			goto fail2;
1461ffac1de6SDan Carpenter 		}
14620d259f04SJohn Johansen 
14630d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
14640d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
14650d259f04SJohn Johansen 	}
14660d259f04SJohn Johansen 
1467c961ee5fSJohn Johansen 	dent = aafs_create_dir(profile->dirname, parent);
14680d259f04SJohn Johansen 	if (IS_ERR(dent))
14690d259f04SJohn Johansen 		goto fail;
14700d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
14710d259f04SJohn Johansen 
147252b97de3SJohn Johansen 	dent = create_profile_file(dir, "name", profile,
147352b97de3SJohn Johansen 				   &seq_profile_name_fops);
14740d259f04SJohn Johansen 	if (IS_ERR(dent))
14750d259f04SJohn Johansen 		goto fail;
14760d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
14770d259f04SJohn Johansen 
147852b97de3SJohn Johansen 	dent = create_profile_file(dir, "mode", profile,
147952b97de3SJohn Johansen 				   &seq_profile_mode_fops);
14800d259f04SJohn Johansen 	if (IS_ERR(dent))
14810d259f04SJohn Johansen 		goto fail;
14820d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
14830d259f04SJohn Johansen 
1484556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
148552b97de3SJohn Johansen 				   &seq_profile_attach_fops);
1486556d0be7SJohn Johansen 	if (IS_ERR(dent))
1487556d0be7SJohn Johansen 		goto fail;
1488556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
1489556d0be7SJohn Johansen 
1490f8eb8a13SJohn Johansen 	if (profile->hash) {
1491f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
149252b97de3SJohn Johansen 					   &seq_profile_hash_fops);
1493f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
1494f8eb8a13SJohn Johansen 			goto fail;
1495f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
1496f8eb8a13SJohn Johansen 	}
1497f8eb8a13SJohn Johansen 
14985ac8c355SJohn Johansen 	if (profile->rawdata) {
14995d5182caSJohn Johansen 		char target[64];
15005d5182caSJohn Johansen 		int depth = profile_depth(profile);
15015d5182caSJohn Johansen 
15025d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
15035d5182caSJohn Johansen 					 profile->rawdata->name, "sha1");
15045d5182caSJohn Johansen 		if (error < 0)
15055d5182caSJohn Johansen 			goto fail2;
1506c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_sha1", dir, target, NULL);
15075ac8c355SJohn Johansen 		if (IS_ERR(dent))
15085ac8c355SJohn Johansen 			goto fail;
15095ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
15105ac8c355SJohn Johansen 
15115d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
15125d5182caSJohn Johansen 					 profile->rawdata->name, "abi");
15135d5182caSJohn Johansen 		if (error < 0)
15145d5182caSJohn Johansen 			goto fail2;
1515c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_abi", dir, target, NULL);
15165ac8c355SJohn Johansen 		if (IS_ERR(dent))
15175ac8c355SJohn Johansen 			goto fail;
15185ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
15195ac8c355SJohn Johansen 
15205d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
15215d5182caSJohn Johansen 					 profile->rawdata->name, "raw_data");
15225d5182caSJohn Johansen 		if (error < 0)
15235d5182caSJohn Johansen 			goto fail2;
1524c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_data", dir, target, NULL);
15255ac8c355SJohn Johansen 		if (IS_ERR(dent))
15265ac8c355SJohn Johansen 			goto fail;
15275ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
15285ac8c355SJohn Johansen 	}
15295ac8c355SJohn Johansen 
15300d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
1531c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, prof_child_dir(profile));
15320d259f04SJohn Johansen 		if (error)
15330d259f04SJohn Johansen 			goto fail2;
15340d259f04SJohn Johansen 	}
15350d259f04SJohn Johansen 
15360d259f04SJohn Johansen 	return 0;
15370d259f04SJohn Johansen 
15380d259f04SJohn Johansen fail:
15390d259f04SJohn Johansen 	error = PTR_ERR(dent);
15400d259f04SJohn Johansen 
15410d259f04SJohn Johansen fail2:
1542c97204baSJohn Johansen 	__aafs_profile_rmdir(profile);
15430d259f04SJohn Johansen 
15440d259f04SJohn Johansen 	return error;
15450d259f04SJohn Johansen }
15460d259f04SJohn Johansen 
15474ae47f33SJohn Johansen static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode)
15484ae47f33SJohn Johansen {
15494ae47f33SJohn Johansen 	struct aa_ns *ns, *parent;
15504ae47f33SJohn Johansen 	/* TODO: improve permission check */
1551*637f688dSJohn Johansen 	struct aa_label *label;
1552*637f688dSJohn Johansen 	int error;
1553*637f688dSJohn Johansen 
1554*637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1555*637f688dSJohn Johansen 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1556*637f688dSJohn Johansen 	end_current_label_crit_section(label);
15574ae47f33SJohn Johansen 	if (error)
15584ae47f33SJohn Johansen 		return error;
15594ae47f33SJohn Johansen 
15604ae47f33SJohn Johansen 	parent = aa_get_ns(dir->i_private);
15614ae47f33SJohn Johansen 	AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
15624ae47f33SJohn Johansen 
15634ae47f33SJohn Johansen 	/* we have to unlock and then relock to get locking order right
15644ae47f33SJohn Johansen 	 * for pin_fs
15654ae47f33SJohn Johansen 	 */
15664ae47f33SJohn Johansen 	inode_unlock(dir);
15674ae47f33SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
15684ae47f33SJohn Johansen 	mutex_lock(&parent->lock);
15694ae47f33SJohn Johansen 	inode_lock_nested(dir, I_MUTEX_PARENT);
15704ae47f33SJohn Johansen 	if (error)
15714ae47f33SJohn Johansen 		goto out;
15724ae47f33SJohn Johansen 
15734ae47f33SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR,  NULL,
15744ae47f33SJohn Johansen 				     NULL, NULL, NULL);
15754ae47f33SJohn Johansen 	if (error)
15764ae47f33SJohn Johansen 		goto out_pin;
15774ae47f33SJohn Johansen 
15784ae47f33SJohn Johansen 	ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name),
15794ae47f33SJohn Johansen 				    dentry);
15804ae47f33SJohn Johansen 	if (IS_ERR(ns)) {
15814ae47f33SJohn Johansen 		error = PTR_ERR(ns);
15824ae47f33SJohn Johansen 		ns = NULL;
15834ae47f33SJohn Johansen 	}
15844ae47f33SJohn Johansen 
15854ae47f33SJohn Johansen 	aa_put_ns(ns);		/* list ref remains */
15864ae47f33SJohn Johansen out_pin:
15874ae47f33SJohn Johansen 	if (error)
15884ae47f33SJohn Johansen 		simple_release_fs(&aafs_mnt, &aafs_count);
15894ae47f33SJohn Johansen out:
15904ae47f33SJohn Johansen 	mutex_unlock(&parent->lock);
15914ae47f33SJohn Johansen 	aa_put_ns(parent);
15924ae47f33SJohn Johansen 
15934ae47f33SJohn Johansen 	return error;
15944ae47f33SJohn Johansen }
15954ae47f33SJohn Johansen 
15964ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
15974ae47f33SJohn Johansen {
15984ae47f33SJohn Johansen 	struct aa_ns *ns, *parent;
15994ae47f33SJohn Johansen 	/* TODO: improve permission check */
1600*637f688dSJohn Johansen 	struct aa_label *label;
1601*637f688dSJohn Johansen 	int error;
1602*637f688dSJohn Johansen 
1603*637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1604*637f688dSJohn Johansen 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1605*637f688dSJohn Johansen 	end_current_label_crit_section(label);
16064ae47f33SJohn Johansen 	if (error)
16074ae47f33SJohn Johansen 		return error;
16084ae47f33SJohn Johansen 
16094ae47f33SJohn Johansen 	 parent = aa_get_ns(dir->i_private);
16104ae47f33SJohn Johansen 	/* rmdir calls the generic securityfs functions to remove files
16114ae47f33SJohn Johansen 	 * from the apparmor dir. It is up to the apparmor ns locking
16124ae47f33SJohn Johansen 	 * to avoid races.
16134ae47f33SJohn Johansen 	 */
16144ae47f33SJohn Johansen 	inode_unlock(dir);
16154ae47f33SJohn Johansen 	inode_unlock(dentry->d_inode);
16164ae47f33SJohn Johansen 
16174ae47f33SJohn Johansen 	mutex_lock(&parent->lock);
16184ae47f33SJohn Johansen 	ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name,
16194ae47f33SJohn Johansen 				     dentry->d_name.len));
16204ae47f33SJohn Johansen 	if (!ns) {
16214ae47f33SJohn Johansen 		error = -ENOENT;
16224ae47f33SJohn Johansen 		goto out;
16234ae47f33SJohn Johansen 	}
16244ae47f33SJohn Johansen 	AA_BUG(ns_dir(ns) != dentry);
16254ae47f33SJohn Johansen 
16264ae47f33SJohn Johansen 	__aa_remove_ns(ns);
16274ae47f33SJohn Johansen 	aa_put_ns(ns);
16284ae47f33SJohn Johansen 
16294ae47f33SJohn Johansen out:
16304ae47f33SJohn Johansen 	mutex_unlock(&parent->lock);
16314ae47f33SJohn Johansen 	inode_lock_nested(dir, I_MUTEX_PARENT);
16324ae47f33SJohn Johansen 	inode_lock(dentry->d_inode);
16334ae47f33SJohn Johansen 	aa_put_ns(parent);
16344ae47f33SJohn Johansen 
16354ae47f33SJohn Johansen 	return error;
16364ae47f33SJohn Johansen }
16374ae47f33SJohn Johansen 
16384ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = {
16394ae47f33SJohn Johansen 	.lookup		= simple_lookup,
16404ae47f33SJohn Johansen 	.mkdir		= ns_mkdir_op,
16414ae47f33SJohn Johansen 	.rmdir		= ns_rmdir_op,
16424ae47f33SJohn Johansen };
16434ae47f33SJohn Johansen 
16445d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
16455d5182caSJohn Johansen {
16465d5182caSJohn Johansen 	struct aa_loaddata *ent, *tmp;
16475d5182caSJohn Johansen 
16485d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
16495d5182caSJohn Johansen 
16505d5182caSJohn Johansen 	list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
16515d5182caSJohn Johansen 		__aa_fs_remove_rawdata(ent);
16525d5182caSJohn Johansen }
16535d5182caSJohn Johansen 
1654c97204baSJohn Johansen /**
1655c97204baSJohn Johansen  *
1656c97204baSJohn Johansen  * Requires: @ns->lock held
1657c97204baSJohn Johansen  */
1658c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns)
16590d259f04SJohn Johansen {
166098849dffSJohn Johansen 	struct aa_ns *sub;
16610d259f04SJohn Johansen 	struct aa_profile *child;
16620d259f04SJohn Johansen 	int i;
16630d259f04SJohn Johansen 
16640d259f04SJohn Johansen 	if (!ns)
16650d259f04SJohn Johansen 		return;
16660d259f04SJohn Johansen 
16670d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
1668c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
16690d259f04SJohn Johansen 
16700d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
16710d259f04SJohn Johansen 		mutex_lock(&sub->lock);
1672c97204baSJohn Johansen 		__aafs_ns_rmdir(sub);
16730d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
16740d259f04SJohn Johansen 	}
16750d259f04SJohn Johansen 
16765d5182caSJohn Johansen 	__aa_fs_list_remove_rawdata(ns);
16775d5182caSJohn Johansen 
1678b7fd2c03SJohn Johansen 	if (ns_subns_dir(ns)) {
1679b7fd2c03SJohn Johansen 		sub = d_inode(ns_subns_dir(ns))->i_private;
1680b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1681b7fd2c03SJohn Johansen 	}
1682b7fd2c03SJohn Johansen 	if (ns_subload(ns)) {
1683b7fd2c03SJohn Johansen 		sub = d_inode(ns_subload(ns))->i_private;
1684b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1685b7fd2c03SJohn Johansen 	}
1686b7fd2c03SJohn Johansen 	if (ns_subreplace(ns)) {
1687b7fd2c03SJohn Johansen 		sub = d_inode(ns_subreplace(ns))->i_private;
1688b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1689b7fd2c03SJohn Johansen 	}
1690b7fd2c03SJohn Johansen 	if (ns_subremove(ns)) {
1691b7fd2c03SJohn Johansen 		sub = d_inode(ns_subremove(ns))->i_private;
1692b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1693b7fd2c03SJohn Johansen 	}
1694d9bf2c26SJohn Johansen 	if (ns_subrevision(ns)) {
1695d9bf2c26SJohn Johansen 		sub = d_inode(ns_subrevision(ns))->i_private;
1696d9bf2c26SJohn Johansen 		aa_put_ns(sub);
1697d9bf2c26SJohn Johansen 	}
1698b7fd2c03SJohn Johansen 
16990d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
1700c961ee5fSJohn Johansen 		aafs_remove(ns->dents[i]);
17010d259f04SJohn Johansen 		ns->dents[i] = NULL;
17020d259f04SJohn Johansen 	}
17030d259f04SJohn Johansen }
17040d259f04SJohn Johansen 
1705b7fd2c03SJohn Johansen /* assumes cleanup in caller */
1706c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
1707b7fd2c03SJohn Johansen {
1708b7fd2c03SJohn Johansen 	struct dentry *dent;
1709b7fd2c03SJohn Johansen 
1710b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1711b7fd2c03SJohn Johansen 	AA_BUG(!dir);
1712b7fd2c03SJohn Johansen 
1713c961ee5fSJohn Johansen 	dent = aafs_create_dir("profiles", dir);
1714b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1715b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1716b7fd2c03SJohn Johansen 	ns_subprofs_dir(ns) = dent;
1717b7fd2c03SJohn Johansen 
1718c961ee5fSJohn Johansen 	dent = aafs_create_dir("raw_data", dir);
1719b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1720b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1721b7fd2c03SJohn Johansen 	ns_subdata_dir(ns) = dent;
1722b7fd2c03SJohn Johansen 
1723d9bf2c26SJohn Johansen 	dent = aafs_create_file("revision", 0444, dir, ns,
1724d9bf2c26SJohn Johansen 				&aa_fs_ns_revision_fops);
1725d9bf2c26SJohn Johansen 	if (IS_ERR(dent))
1726d9bf2c26SJohn Johansen 		return PTR_ERR(dent);
1727d9bf2c26SJohn Johansen 	aa_get_ns(ns);
1728d9bf2c26SJohn Johansen 	ns_subrevision(ns) = dent;
1729d9bf2c26SJohn Johansen 
1730c961ee5fSJohn Johansen 	dent = aafs_create_file(".load", 0640, dir, ns,
1731b7fd2c03SJohn Johansen 				      &aa_fs_profile_load);
1732b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1733b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1734b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1735b7fd2c03SJohn Johansen 	ns_subload(ns) = dent;
1736b7fd2c03SJohn Johansen 
1737c961ee5fSJohn Johansen 	dent = aafs_create_file(".replace", 0640, dir, ns,
1738b7fd2c03SJohn Johansen 				      &aa_fs_profile_replace);
1739b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1740b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1741b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1742b7fd2c03SJohn Johansen 	ns_subreplace(ns) = dent;
1743b7fd2c03SJohn Johansen 
1744c961ee5fSJohn Johansen 	dent = aafs_create_file(".remove", 0640, dir, ns,
1745b7fd2c03SJohn Johansen 				      &aa_fs_profile_remove);
1746b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1747b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1748b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1749b7fd2c03SJohn Johansen 	ns_subremove(ns) = dent;
1750b7fd2c03SJohn Johansen 
17514ae47f33SJohn Johansen 	  /* use create_dentry so we can supply private data */
17524ae47f33SJohn Johansen 	dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL,
17534ae47f33SJohn Johansen 			   &ns_dir_inode_operations);
1754b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1755b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1756b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1757b7fd2c03SJohn Johansen 	ns_subns_dir(ns) = dent;
1758b7fd2c03SJohn Johansen 
1759b7fd2c03SJohn Johansen 	return 0;
1760b7fd2c03SJohn Johansen }
1761b7fd2c03SJohn Johansen 
1762c97204baSJohn Johansen /*
1763c97204baSJohn Johansen  * Requires: @ns->lock held
1764c97204baSJohn Johansen  */
176598407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
176698407f0aSJohn Johansen 		    struct dentry *dent)
17670d259f04SJohn Johansen {
176898849dffSJohn Johansen 	struct aa_ns *sub;
17690d259f04SJohn Johansen 	struct aa_profile *child;
177098407f0aSJohn Johansen 	struct dentry *dir;
17710d259f04SJohn Johansen 	int error;
17720d259f04SJohn Johansen 
1773b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1774b7fd2c03SJohn Johansen 	AA_BUG(!parent);
1775b7fd2c03SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
1776b7fd2c03SJohn Johansen 
17770d259f04SJohn Johansen 	if (!name)
17780d259f04SJohn Johansen 		name = ns->base.name;
17790d259f04SJohn Johansen 
1780c961ee5fSJohn Johansen 	if (!dent) {
1781b7fd2c03SJohn Johansen 		/* create ns dir if it doesn't already exist */
1782c961ee5fSJohn Johansen 		dent = aafs_create_dir(name, parent);
17830d259f04SJohn Johansen 		if (IS_ERR(dent))
17840d259f04SJohn Johansen 			goto fail;
1785c961ee5fSJohn Johansen 	} else
1786c961ee5fSJohn Johansen 		dget(dent);
17870d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
1788c97204baSJohn Johansen 	error = __aafs_ns_mkdir_entries(ns, dir);
1789b7fd2c03SJohn Johansen 	if (error)
1790b7fd2c03SJohn Johansen 		goto fail2;
17910d259f04SJohn Johansen 
1792b7fd2c03SJohn Johansen 	/* profiles */
17930d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
1794c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
17950d259f04SJohn Johansen 		if (error)
17960d259f04SJohn Johansen 			goto fail2;
17970d259f04SJohn Johansen 	}
17980d259f04SJohn Johansen 
1799b7fd2c03SJohn Johansen 	/* subnamespaces */
18000d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
18010d259f04SJohn Johansen 		mutex_lock(&sub->lock);
180298407f0aSJohn Johansen 		error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
18030d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
18040d259f04SJohn Johansen 		if (error)
18050d259f04SJohn Johansen 			goto fail2;
18060d259f04SJohn Johansen 	}
18070d259f04SJohn Johansen 
18080d259f04SJohn Johansen 	return 0;
18090d259f04SJohn Johansen 
18100d259f04SJohn Johansen fail:
18110d259f04SJohn Johansen 	error = PTR_ERR(dent);
18120d259f04SJohn Johansen 
18130d259f04SJohn Johansen fail2:
1814c97204baSJohn Johansen 	__aafs_ns_rmdir(ns);
18150d259f04SJohn Johansen 
18160d259f04SJohn Johansen 	return error;
18170d259f04SJohn Johansen }
18180d259f04SJohn Johansen 
18190d259f04SJohn Johansen 
182029b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
182129b3822fSJohn Johansen 
182229b3822fSJohn Johansen /**
182398849dffSJohn Johansen  * __next_ns - find the next namespace to list
182429b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
182529b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
182629b3822fSJohn Johansen  *
182729b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
182829b3822fSJohn Johansen  * while switching current namespace.
182929b3822fSJohn Johansen  *
183029b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
183129b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
183229b3822fSJohn Johansen  * NOTE: will not unlock root->lock
183329b3822fSJohn Johansen  */
183498849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
183529b3822fSJohn Johansen {
183698849dffSJohn Johansen 	struct aa_ns *parent, *next;
183729b3822fSJohn Johansen 
183829b3822fSJohn Johansen 	/* is next namespace a child */
183929b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
184029b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
184129b3822fSJohn Johansen 		mutex_lock(&next->lock);
184229b3822fSJohn Johansen 		return next;
184329b3822fSJohn Johansen 	}
184429b3822fSJohn Johansen 
184529b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
184629b3822fSJohn Johansen 	parent = ns->parent;
1847ed2c7da3SJohn Johansen 	while (ns != root) {
184829b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
184938dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
185029b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
185129b3822fSJohn Johansen 			mutex_lock(&next->lock);
185229b3822fSJohn Johansen 			return next;
185329b3822fSJohn Johansen 		}
185429b3822fSJohn Johansen 		ns = parent;
185529b3822fSJohn Johansen 		parent = parent->parent;
185629b3822fSJohn Johansen 	}
185729b3822fSJohn Johansen 
185829b3822fSJohn Johansen 	return NULL;
185929b3822fSJohn Johansen }
186029b3822fSJohn Johansen 
186129b3822fSJohn Johansen /**
186229b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
186329b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
186429b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
186529b3822fSJohn Johansen  *
186629b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
186729b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
186829b3822fSJohn Johansen  */
186998849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
187098849dffSJohn Johansen 					  struct aa_ns *ns)
187129b3822fSJohn Johansen {
187298849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
187329b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
187429b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
187529b3822fSJohn Johansen 						struct aa_profile, base.list);
187629b3822fSJohn Johansen 	}
187729b3822fSJohn Johansen 	return NULL;
187829b3822fSJohn Johansen }
187929b3822fSJohn Johansen 
188029b3822fSJohn Johansen /**
188129b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
188229b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
188329b3822fSJohn Johansen  *
188429b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
188529b3822fSJohn Johansen  *
188629b3822fSJohn Johansen  * Returns: next profile or NULL if done
188729b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
188829b3822fSJohn Johansen  */
188929b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
189029b3822fSJohn Johansen {
189129b3822fSJohn Johansen 	struct aa_profile *parent;
189298849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
189329b3822fSJohn Johansen 
189429b3822fSJohn Johansen 	/* is next profile a child */
189529b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
189629b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
189729b3822fSJohn Johansen 					base.list);
189829b3822fSJohn Johansen 
189929b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
190029b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
190129b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
190229b3822fSJohn Johansen 	while (parent) {
190338dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
190429b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
190529b3822fSJohn Johansen 			return p;
190629b3822fSJohn Johansen 		p = parent;
190729b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
190829b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
190929b3822fSJohn Johansen 	}
191029b3822fSJohn Johansen 
191129b3822fSJohn Johansen 	/* is next another profile in the namespace */
191238dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
191329b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
191429b3822fSJohn Johansen 		return p;
191529b3822fSJohn Johansen 
191629b3822fSJohn Johansen 	return NULL;
191729b3822fSJohn Johansen }
191829b3822fSJohn Johansen 
191929b3822fSJohn Johansen /**
192029b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
192129b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
192229b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
192329b3822fSJohn Johansen  *
192429b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
192529b3822fSJohn Johansen  */
192698849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
192729b3822fSJohn Johansen 				       struct aa_profile *profile)
192829b3822fSJohn Johansen {
192929b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
193029b3822fSJohn Johansen 	if (next)
193129b3822fSJohn Johansen 		return next;
193229b3822fSJohn Johansen 
193329b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
193498849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
193529b3822fSJohn Johansen }
193629b3822fSJohn Johansen 
193729b3822fSJohn Johansen /**
193829b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
193929b3822fSJohn Johansen  * @f: seq_file to fill
194029b3822fSJohn Johansen  * @pos: current position
194129b3822fSJohn Johansen  *
194229b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
194329b3822fSJohn Johansen  *
194429b3822fSJohn Johansen  * acquires first ns->lock
194529b3822fSJohn Johansen  */
194629b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
194729b3822fSJohn Johansen {
194829b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
1949cf797c0eSJohn Johansen 	struct aa_ns *root = aa_get_current_ns();
195029b3822fSJohn Johansen 	loff_t l = *pos;
1951cf797c0eSJohn Johansen 	f->private = root;
195229b3822fSJohn Johansen 
195329b3822fSJohn Johansen 	/* find the first profile */
195429b3822fSJohn Johansen 	mutex_lock(&root->lock);
195529b3822fSJohn Johansen 	profile = __first_profile(root, root);
195629b3822fSJohn Johansen 
195729b3822fSJohn Johansen 	/* skip to position */
195829b3822fSJohn Johansen 	for (; profile && l > 0; l--)
195929b3822fSJohn Johansen 		profile = next_profile(root, profile);
196029b3822fSJohn Johansen 
196129b3822fSJohn Johansen 	return profile;
196229b3822fSJohn Johansen }
196329b3822fSJohn Johansen 
196429b3822fSJohn Johansen /**
196529b3822fSJohn Johansen  * p_next - read the next profile entry
196629b3822fSJohn Johansen  * @f: seq_file to fill
196729b3822fSJohn Johansen  * @p: profile previously returned
196829b3822fSJohn Johansen  * @pos: current position
196929b3822fSJohn Johansen  *
197029b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
197129b3822fSJohn Johansen  *
197229b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
197329b3822fSJohn Johansen  */
197429b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
197529b3822fSJohn Johansen {
197629b3822fSJohn Johansen 	struct aa_profile *profile = p;
197798849dffSJohn Johansen 	struct aa_ns *ns = f->private;
197829b3822fSJohn Johansen 	(*pos)++;
197929b3822fSJohn Johansen 
198029b3822fSJohn Johansen 	return next_profile(ns, profile);
198129b3822fSJohn Johansen }
198229b3822fSJohn Johansen 
198329b3822fSJohn Johansen /**
198429b3822fSJohn Johansen  * p_stop - stop depth first traversal
198529b3822fSJohn Johansen  * @f: seq_file we are filling
198629b3822fSJohn Johansen  * @p: the last profile writen
198729b3822fSJohn Johansen  *
198829b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
198929b3822fSJohn Johansen  */
199029b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
199129b3822fSJohn Johansen {
199229b3822fSJohn Johansen 	struct aa_profile *profile = p;
199398849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
199429b3822fSJohn Johansen 
199529b3822fSJohn Johansen 	if (profile) {
199629b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
199729b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
199829b3822fSJohn Johansen 	}
199929b3822fSJohn Johansen 	mutex_unlock(&root->lock);
200098849dffSJohn Johansen 	aa_put_ns(root);
200129b3822fSJohn Johansen }
200229b3822fSJohn Johansen 
200329b3822fSJohn Johansen /**
200429b3822fSJohn Johansen  * seq_show_profile - show a profile entry
200529b3822fSJohn Johansen  * @f: seq_file to file
200629b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
200729b3822fSJohn Johansen  *
200829b3822fSJohn Johansen  * Returns: error on failure
200929b3822fSJohn Johansen  */
201029b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
201129b3822fSJohn Johansen {
201229b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
201398849dffSJohn Johansen 	struct aa_ns *root = f->private;
201429b3822fSJohn Johansen 
2015*637f688dSJohn Johansen 	aa_label_seq_xprint(f, root, &profile->label,
2016*637f688dSJohn Johansen 			    FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
2017*637f688dSJohn Johansen 	seq_putc(f, '\n');
201829b3822fSJohn Johansen 
201929b3822fSJohn Johansen 	return 0;
202029b3822fSJohn Johansen }
202129b3822fSJohn Johansen 
2022c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = {
202329b3822fSJohn Johansen 	.start = p_start,
202429b3822fSJohn Johansen 	.next = p_next,
202529b3822fSJohn Johansen 	.stop = p_stop,
202629b3822fSJohn Johansen 	.show = seq_show_profile,
202729b3822fSJohn Johansen };
202829b3822fSJohn Johansen 
202929b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
203029b3822fSJohn Johansen {
20315ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
20325ac8c355SJohn Johansen 		return -EACCES;
20335ac8c355SJohn Johansen 
2034c97204baSJohn Johansen 	return seq_open(file, &aa_sfs_profiles_op);
203529b3822fSJohn Johansen }
203629b3822fSJohn Johansen 
203729b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
203829b3822fSJohn Johansen {
203929b3822fSJohn Johansen 	return seq_release(inode, file);
204029b3822fSJohn Johansen }
204129b3822fSJohn Johansen 
2042c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = {
204329b3822fSJohn Johansen 	.open = profiles_open,
204429b3822fSJohn Johansen 	.read = seq_read,
204529b3822fSJohn Johansen 	.llseek = seq_lseek,
204629b3822fSJohn Johansen 	.release = profiles_release,
204729b3822fSJohn Johansen };
204829b3822fSJohn Johansen 
204929b3822fSJohn Johansen 
20500d259f04SJohn Johansen /** Base file system setup **/
2051c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = {
2052c97204baSJohn Johansen 	AA_SFS_FILE_STRING("mask",
2053c97204baSJohn Johansen 			   "create read write exec append mmap_exec link lock"),
2054a9bf8e9fSKees Cook 	{ }
2055a9bf8e9fSKees Cook };
2056a9bf8e9fSKees Cook 
2057c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = {
2058c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hat",	1),
2059c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hatv",	1),
2060c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_onexec",	1),
2061c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_profile",	1),
2062c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
2063c97204baSJohn Johansen 	AA_SFS_FILE_STRING("version", "1.2"),
2064e74abcf3SKees Cook 	{ }
2065e74abcf3SKees Cook };
2066e74abcf3SKees Cook 
2067c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = {
2068c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("v5",	1),
2069474d6b75SJohn Johansen 	{ }
2070474d6b75SJohn Johansen };
2071474d6b75SJohn Johansen 
2072c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = {
2073c97204baSJohn Johansen 	AA_SFS_DIR("versions",			aa_sfs_entry_versions),
2074c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("set_load",		1),
20759d910a3bSJohn Johansen 	{ }
20769d910a3bSJohn Johansen };
20779d910a3bSJohn Johansen 
2078a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = {
20794f3b3f2dSJohn Johansen 	AA_SFS_FILE_STRING("perms", "allow deny audit quiet"),
2080a83bd86eSJohn Johansen 	AA_SFS_FILE_BOOLEAN("data",		1),
20811dea3b41SJohn Johansen 	AA_SFS_FILE_BOOLEAN("multi_transaction",	1),
2082a83bd86eSJohn Johansen 	{ }
2083a83bd86eSJohn Johansen };
2084a83bd86eSJohn Johansen 
2085a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = {
2086a83bd86eSJohn Johansen 	AA_SFS_DIR("label",			aa_sfs_entry_query_label),
2087a83bd86eSJohn Johansen 	{ }
2088a83bd86eSJohn Johansen };
2089c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = {
2090c97204baSJohn Johansen 	AA_SFS_DIR("policy",			aa_sfs_entry_policy),
2091c97204baSJohn Johansen 	AA_SFS_DIR("domain",			aa_sfs_entry_domain),
2092c97204baSJohn Johansen 	AA_SFS_DIR("file",			aa_sfs_entry_file),
2093c97204baSJohn Johansen 	AA_SFS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
2094c97204baSJohn Johansen 	AA_SFS_DIR("rlimit",			aa_sfs_entry_rlimit),
2095c97204baSJohn Johansen 	AA_SFS_DIR("caps",			aa_sfs_entry_caps),
2096a83bd86eSJohn Johansen 	AA_SFS_DIR("query",			aa_sfs_entry_query),
2097e74abcf3SKees Cook 	{ }
2098e74abcf3SKees Cook };
2099e74abcf3SKees Cook 
2100c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
2101c97204baSJohn Johansen 	AA_SFS_FILE_FOPS(".access", 0640, &aa_sfs_access),
2102c97204baSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_level", 0666, &seq_ns_level_fops),
2103c97204baSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_name", 0640, &seq_ns_name_fops),
2104c97204baSJohn Johansen 	AA_SFS_FILE_FOPS("profiles", 0440, &aa_sfs_profiles_fops),
2105c97204baSJohn Johansen 	AA_SFS_DIR("features", aa_sfs_entry_features),
21069acd494bSKees Cook 	{ }
21079acd494bSKees Cook };
210863e2b423SJohn Johansen 
2109c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry =
2110c97204baSJohn Johansen 	AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
21119acd494bSKees Cook 
21129acd494bSKees Cook /**
2113a481f4d9SJohn Johansen  * entry_create_file - create a file entry in the apparmor securityfs
2114c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
21159acd494bSKees Cook  * @parent: the parent dentry in the securityfs
21169acd494bSKees Cook  *
2117a481f4d9SJohn Johansen  * Use entry_remove_file to remove entries created with this fn.
21189acd494bSKees Cook  */
2119c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file,
21209acd494bSKees Cook 				    struct dentry *parent)
212163e2b423SJohn Johansen {
21229acd494bSKees Cook 	int error = 0;
212363e2b423SJohn Johansen 
21249acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
21259acd494bSKees Cook 						 S_IFREG | fs_file->mode,
21269acd494bSKees Cook 						 parent, fs_file,
21279acd494bSKees Cook 						 fs_file->file_ops);
21289acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
21299acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
21309acd494bSKees Cook 		fs_file->dentry = NULL;
213163e2b423SJohn Johansen 	}
21329acd494bSKees Cook 	return error;
213363e2b423SJohn Johansen }
213463e2b423SJohn Johansen 
2135c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
213663e2b423SJohn Johansen /**
2137a481f4d9SJohn Johansen  * entry_create_dir - recursively create a directory entry in the securityfs
2138c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
21399acd494bSKees Cook  * @parent: the parent dentry in the securityfs
214063e2b423SJohn Johansen  *
2141a481f4d9SJohn Johansen  * Use entry_remove_dir to remove entries created with this fn.
214263e2b423SJohn Johansen  */
2143c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
21449acd494bSKees Cook 				   struct dentry *parent)
214563e2b423SJohn Johansen {
2146c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
21470d259f04SJohn Johansen 	struct dentry *dir;
21480d259f04SJohn Johansen 	int error;
214963e2b423SJohn Johansen 
21500d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
21510d259f04SJohn Johansen 	if (IS_ERR(dir))
21520d259f04SJohn Johansen 		return PTR_ERR(dir);
21530d259f04SJohn Johansen 	fs_dir->dentry = dir;
215463e2b423SJohn Johansen 
21550d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2156c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2157a481f4d9SJohn Johansen 			error = entry_create_dir(fs_file, fs_dir->dentry);
21589acd494bSKees Cook 		else
2159a481f4d9SJohn Johansen 			error = entry_create_file(fs_file, fs_dir->dentry);
21609acd494bSKees Cook 		if (error)
21619acd494bSKees Cook 			goto failed;
21629acd494bSKees Cook 	}
21639acd494bSKees Cook 
21649acd494bSKees Cook 	return 0;
21659acd494bSKees Cook 
21669acd494bSKees Cook failed:
2167a481f4d9SJohn Johansen 	entry_remove_dir(fs_dir);
21680d259f04SJohn Johansen 
21699acd494bSKees Cook 	return error;
21709acd494bSKees Cook }
21719acd494bSKees Cook 
21729acd494bSKees Cook /**
2173c97204baSJohn Johansen  * entry_remove_file - drop a single file entry in the apparmor securityfs
2174c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
21759acd494bSKees Cook  */
2176c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
21779acd494bSKees Cook {
21789acd494bSKees Cook 	if (!fs_file->dentry)
21799acd494bSKees Cook 		return;
21809acd494bSKees Cook 
21819acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
21829acd494bSKees Cook 	fs_file->dentry = NULL;
21839acd494bSKees Cook }
21849acd494bSKees Cook 
21859acd494bSKees Cook /**
2186a481f4d9SJohn Johansen  * entry_remove_dir - recursively drop a directory entry from the securityfs
2187c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
21889acd494bSKees Cook  */
2189c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
21909acd494bSKees Cook {
2191c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
21929acd494bSKees Cook 
21930d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2194c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2195a481f4d9SJohn Johansen 			entry_remove_dir(fs_file);
21969acd494bSKees Cook 		else
2197c97204baSJohn Johansen 			entry_remove_file(fs_file);
21989acd494bSKees Cook 	}
21999acd494bSKees Cook 
2200c97204baSJohn Johansen 	entry_remove_file(fs_dir);
220163e2b423SJohn Johansen }
220263e2b423SJohn Johansen 
220363e2b423SJohn Johansen /**
220463e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
220563e2b423SJohn Johansen  *
220663e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
220763e2b423SJohn Johansen  */
220863e2b423SJohn Johansen void __init aa_destroy_aafs(void)
220963e2b423SJohn Johansen {
2210c97204baSJohn Johansen 	entry_remove_dir(&aa_sfs_entry);
221163e2b423SJohn Johansen }
221263e2b423SJohn Johansen 
2213a71ada30SJohn Johansen 
2214a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
2215a71ada30SJohn Johansen struct path aa_null;
2216a71ada30SJohn Johansen 
2217a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
2218a71ada30SJohn Johansen {
2219a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
2220a71ada30SJohn Johansen 	struct dentry *dentry;
2221a71ada30SJohn Johansen 	struct inode *inode;
2222a71ada30SJohn Johansen 	int count = 0;
2223a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
2224a71ada30SJohn Johansen 
2225a71ada30SJohn Johansen 	if (error)
2226a71ada30SJohn Johansen 		return error;
2227a71ada30SJohn Johansen 
2228a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
2229a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
2230a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
2231a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
2232a71ada30SJohn Johansen 		goto out;
2233a71ada30SJohn Johansen 	}
2234a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
2235a71ada30SJohn Johansen 	if (!inode) {
2236a71ada30SJohn Johansen 		error = -ENOMEM;
2237a71ada30SJohn Johansen 		goto out1;
2238a71ada30SJohn Johansen 	}
2239a71ada30SJohn Johansen 
2240a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
2241a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
224224d0d03cSDeepa Dinamani 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
2243a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
2244a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
2245a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
2246a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
2247a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
2248a71ada30SJohn Johansen 
2249a71ada30SJohn Johansen 	error = 0;
2250a71ada30SJohn Johansen 
2251a71ada30SJohn Johansen out1:
2252a71ada30SJohn Johansen 	dput(dentry);
2253a71ada30SJohn Johansen out:
2254a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
2255a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
2256a71ada30SJohn Johansen 	return error;
2257a71ada30SJohn Johansen }
2258a71ada30SJohn Johansen 
2259a481f4d9SJohn Johansen 
2260a481f4d9SJohn Johansen 
2261a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry,
2262a481f4d9SJohn Johansen 				   struct inode *inode,
2263a481f4d9SJohn Johansen 				   struct delayed_call *done)
2264a481f4d9SJohn Johansen {
2265a481f4d9SJohn Johansen 	struct aa_ns *ns;
2266a481f4d9SJohn Johansen 	struct path path;
2267a481f4d9SJohn Johansen 
2268a481f4d9SJohn Johansen 	if (!dentry)
2269a481f4d9SJohn Johansen 		return ERR_PTR(-ECHILD);
2270a481f4d9SJohn Johansen 	ns = aa_get_current_ns();
2271a481f4d9SJohn Johansen 	path.mnt = mntget(aafs_mnt);
2272a481f4d9SJohn Johansen 	path.dentry = dget(ns_dir(ns));
2273a481f4d9SJohn Johansen 	nd_jump_link(&path);
2274a481f4d9SJohn Johansen 	aa_put_ns(ns);
2275a481f4d9SJohn Johansen 
2276a481f4d9SJohn Johansen 	return NULL;
2277a481f4d9SJohn Johansen }
2278a481f4d9SJohn Johansen 
2279a481f4d9SJohn Johansen static int ns_get_name(char *buf, size_t size, struct aa_ns *ns,
2280a481f4d9SJohn Johansen 		       struct inode *inode)
2281a481f4d9SJohn Johansen {
2282a481f4d9SJohn Johansen 	int res = snprintf(buf, size, "%s:[%lu]", AAFS_NAME, inode->i_ino);
2283a481f4d9SJohn Johansen 
2284a481f4d9SJohn Johansen 	if (res < 0 || res >= size)
2285a481f4d9SJohn Johansen 		res = -ENOENT;
2286a481f4d9SJohn Johansen 
2287a481f4d9SJohn Johansen 	return res;
2288a481f4d9SJohn Johansen }
2289a481f4d9SJohn Johansen 
2290a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer,
2291a481f4d9SJohn Johansen 			   int buflen)
2292a481f4d9SJohn Johansen {
2293a481f4d9SJohn Johansen 	struct aa_ns *ns;
2294a481f4d9SJohn Johansen 	char name[32];
2295a481f4d9SJohn Johansen 	int res;
2296a481f4d9SJohn Johansen 
2297a481f4d9SJohn Johansen 	ns = aa_get_current_ns();
2298a481f4d9SJohn Johansen 	res = ns_get_name(name, sizeof(name), ns, d_inode(dentry));
2299a481f4d9SJohn Johansen 	if (res >= 0)
2300a481f4d9SJohn Johansen 		res = readlink_copy(buffer, buflen, name);
2301a481f4d9SJohn Johansen 	aa_put_ns(ns);
2302a481f4d9SJohn Johansen 
2303a481f4d9SJohn Johansen 	return res;
2304a481f4d9SJohn Johansen }
2305a481f4d9SJohn Johansen 
2306a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = {
2307a481f4d9SJohn Johansen 	.readlink	= policy_readlink,
2308a481f4d9SJohn Johansen 	.get_link	= policy_get_link,
2309a481f4d9SJohn Johansen };
2310a481f4d9SJohn Johansen 
2311a481f4d9SJohn Johansen 
231263e2b423SJohn Johansen /**
231363e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
231463e2b423SJohn Johansen  *
231563e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
231663e2b423SJohn Johansen  *
231763e2b423SJohn Johansen  * Returns: error on failure
231863e2b423SJohn Johansen  */
23193417d8d5SJames Morris static int __init aa_create_aafs(void)
232063e2b423SJohn Johansen {
2321b7fd2c03SJohn Johansen 	struct dentry *dent;
232263e2b423SJohn Johansen 	int error;
232363e2b423SJohn Johansen 
232463e2b423SJohn Johansen 	if (!apparmor_initialized)
232563e2b423SJohn Johansen 		return 0;
232663e2b423SJohn Johansen 
2327c97204baSJohn Johansen 	if (aa_sfs_entry.dentry) {
232863e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
232963e2b423SJohn Johansen 		return -EEXIST;
233063e2b423SJohn Johansen 	}
233163e2b423SJohn Johansen 
2332a481f4d9SJohn Johansen 	/* setup apparmorfs used to virtualize policy/ */
2333a481f4d9SJohn Johansen 	aafs_mnt = kern_mount(&aafs_ops);
2334a481f4d9SJohn Johansen 	if (IS_ERR(aafs_mnt))
2335a481f4d9SJohn Johansen 		panic("can't set apparmorfs up\n");
2336a481f4d9SJohn Johansen 	aafs_mnt->mnt_sb->s_flags &= ~MS_NOUSER;
2337a481f4d9SJohn Johansen 
23389acd494bSKees Cook 	/* Populate fs tree. */
2339c97204baSJohn Johansen 	error = entry_create_dir(&aa_sfs_entry, NULL);
234063e2b423SJohn Johansen 	if (error)
234163e2b423SJohn Johansen 		goto error;
234263e2b423SJohn Johansen 
2343c97204baSJohn Johansen 	dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
2344b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_load);
2345b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
2346b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
2347b7fd2c03SJohn Johansen 		goto error;
2348b7fd2c03SJohn Johansen 	}
2349b7fd2c03SJohn Johansen 	ns_subload(root_ns) = dent;
2350b7fd2c03SJohn Johansen 
2351c97204baSJohn Johansen 	dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
2352b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_replace);
2353b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
2354b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
2355b7fd2c03SJohn Johansen 		goto error;
2356b7fd2c03SJohn Johansen 	}
2357b7fd2c03SJohn Johansen 	ns_subreplace(root_ns) = dent;
2358b7fd2c03SJohn Johansen 
2359c97204baSJohn Johansen 	dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
2360b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_remove);
2361b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
2362b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
2363b7fd2c03SJohn Johansen 		goto error;
2364b7fd2c03SJohn Johansen 	}
2365b7fd2c03SJohn Johansen 	ns_subremove(root_ns) = dent;
2366b7fd2c03SJohn Johansen 
2367d9bf2c26SJohn Johansen 	dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry,
2368d9bf2c26SJohn Johansen 				      NULL, &aa_fs_ns_revision_fops);
2369d9bf2c26SJohn Johansen 	if (IS_ERR(dent)) {
2370d9bf2c26SJohn Johansen 		error = PTR_ERR(dent);
2371d9bf2c26SJohn Johansen 		goto error;
2372d9bf2c26SJohn Johansen 	}
2373d9bf2c26SJohn Johansen 	ns_subrevision(root_ns) = dent;
2374d9bf2c26SJohn Johansen 
2375d9bf2c26SJohn Johansen 	/* policy tree referenced by magic policy symlink */
2376b7fd2c03SJohn Johansen 	mutex_lock(&root_ns->lock);
2377c961ee5fSJohn Johansen 	error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
2378c961ee5fSJohn Johansen 				aafs_mnt->mnt_root);
2379b7fd2c03SJohn Johansen 	mutex_unlock(&root_ns->lock);
23800d259f04SJohn Johansen 	if (error)
23810d259f04SJohn Johansen 		goto error;
23820d259f04SJohn Johansen 
2383c961ee5fSJohn Johansen 	/* magic symlink similar to nsfs redirects based on task policy */
2384c961ee5fSJohn Johansen 	dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
2385c961ee5fSJohn Johansen 					 NULL, &policy_link_iops);
2386c961ee5fSJohn Johansen 	if (IS_ERR(dent)) {
2387c961ee5fSJohn Johansen 		error = PTR_ERR(dent);
2388c961ee5fSJohn Johansen 		goto error;
2389c961ee5fSJohn Johansen 	}
2390c961ee5fSJohn Johansen 
2391c97204baSJohn Johansen 	error = aa_mk_null_file(aa_sfs_entry.dentry);
2392a71ada30SJohn Johansen 	if (error)
2393a71ada30SJohn Johansen 		goto error;
2394a71ada30SJohn Johansen 
2395a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
239663e2b423SJohn Johansen 
239763e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
239863e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
239963e2b423SJohn Johansen 	return 0;
240063e2b423SJohn Johansen 
240163e2b423SJohn Johansen error:
240263e2b423SJohn Johansen 	aa_destroy_aafs();
240363e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
240463e2b423SJohn Johansen 	return error;
240563e2b423SJohn Johansen }
240663e2b423SJohn Johansen 
240763e2b423SJohn Johansen fs_initcall(aa_create_aafs);
2408