1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
263e2b423SJohn Johansen /*
363e2b423SJohn Johansen  * AppArmor security module
463e2b423SJohn Johansen  *
563e2b423SJohn Johansen  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
663e2b423SJohn Johansen  *
763e2b423SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
863e2b423SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
963e2b423SJohn Johansen  */
1063e2b423SJohn Johansen 
110d259f04SJohn Johansen #include <linux/ctype.h>
1263e2b423SJohn Johansen #include <linux/security.h>
1363e2b423SJohn Johansen #include <linux/vmalloc.h>
14876979c9SPaul Gortmaker #include <linux/init.h>
1563e2b423SJohn Johansen #include <linux/seq_file.h>
1663e2b423SJohn Johansen #include <linux/uaccess.h>
17a71ada30SJohn Johansen #include <linux/mount.h>
1863e2b423SJohn Johansen #include <linux/namei.h>
19e74abcf3SKees Cook #include <linux/capability.h>
2029b3822fSJohn Johansen #include <linux/rcupdate.h>
21a71ada30SJohn Johansen #include <linux/fs.h>
22b0ecc9daSDavid Howells #include <linux/fs_context.h>
23d9bf2c26SJohn Johansen #include <linux/poll.h>
2463c16c3aSChris Coulson #include <linux/zlib.h>
25a481f4d9SJohn Johansen #include <uapi/linux/major.h>
26a481f4d9SJohn Johansen #include <uapi/linux/magic.h>
2763e2b423SJohn Johansen 
2863e2b423SJohn Johansen #include "include/apparmor.h"
2963e2b423SJohn Johansen #include "include/apparmorfs.h"
3063e2b423SJohn Johansen #include "include/audit.h"
31d8889d49SJohn Johansen #include "include/cred.h"
32f8eb8a13SJohn Johansen #include "include/crypto.h"
33cd1dbf76SJohn Johansen #include "include/ipc.h"
34317d9a05SJohn Johansen #include "include/label.h"
3563e2b423SJohn Johansen #include "include/policy.h"
36cff281f6SJohn Johansen #include "include/policy_ns.h"
37d384b0a1SKees Cook #include "include/resource.h"
385ac8c355SJohn Johansen #include "include/policy_unpack.h"
3963e2b423SJohn Johansen 
40c97204baSJohn Johansen /*
41c97204baSJohn Johansen  * The apparmor filesystem interface used for policy load and introspection
42c97204baSJohn Johansen  * The interface is split into two main components based on their function
43c97204baSJohn Johansen  * a securityfs component:
44c97204baSJohn Johansen  *   used for static files that are always available, and which allows
45c97204baSJohn Johansen  *   userspace to specificy the location of the security filesystem.
46c97204baSJohn Johansen  *
47c97204baSJohn Johansen  *   fns and data are prefixed with
48c97204baSJohn Johansen  *      aa_sfs_
49c97204baSJohn Johansen  *
50c97204baSJohn Johansen  * an apparmorfs component:
51c97204baSJohn Johansen  *   used loaded policy content and introspection. It is not part of  a
52c97204baSJohn Johansen  *   regular mounted filesystem and is available only through the magic
53c97204baSJohn Johansen  *   policy symlink in the root of the securityfs apparmor/ directory.
54c97204baSJohn Johansen  *   Tasks queries will be magically redirected to the correct portion
55c97204baSJohn Johansen  *   of the policy tree based on their confinement.
56c97204baSJohn Johansen  *
57c97204baSJohn Johansen  *   fns and data are prefixed with
58c97204baSJohn Johansen  *      aafs_
59c97204baSJohn Johansen  *
60c97204baSJohn Johansen  * The aa_fs_ prefix is used to indicate the fn is used by both the
61c97204baSJohn Johansen  * securityfs and apparmorfs filesystems.
62c97204baSJohn Johansen  */
63c97204baSJohn Johansen 
64c97204baSJohn Johansen 
65c97204baSJohn Johansen /*
66c97204baSJohn Johansen  * support fns
67c97204baSJohn Johansen  */
68c97204baSJohn Johansen 
6963c16c3aSChris Coulson struct rawdata_f_data {
7063c16c3aSChris Coulson 	struct aa_loaddata *loaddata;
7163c16c3aSChris Coulson };
7263c16c3aSChris Coulson 
7363c16c3aSChris Coulson #define RAWDATA_F_DATA_BUF(p) (char *)(p + 1)
7463c16c3aSChris Coulson 
7563c16c3aSChris Coulson static void rawdata_f_data_free(struct rawdata_f_data *private)
7663c16c3aSChris Coulson {
7763c16c3aSChris Coulson 	if (!private)
7863c16c3aSChris Coulson 		return;
7963c16c3aSChris Coulson 
8063c16c3aSChris Coulson 	aa_put_loaddata(private->loaddata);
8163c16c3aSChris Coulson 	kvfree(private);
8263c16c3aSChris Coulson }
8363c16c3aSChris Coulson 
8463c16c3aSChris Coulson static struct rawdata_f_data *rawdata_f_data_alloc(size_t size)
8563c16c3aSChris Coulson {
8663c16c3aSChris Coulson 	struct rawdata_f_data *ret;
8763c16c3aSChris Coulson 
8863c16c3aSChris Coulson 	if (size > SIZE_MAX - sizeof(*ret))
8963c16c3aSChris Coulson 		return ERR_PTR(-EINVAL);
9063c16c3aSChris Coulson 
9163c16c3aSChris Coulson 	ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL);
9263c16c3aSChris Coulson 	if (!ret)
9363c16c3aSChris Coulson 		return ERR_PTR(-ENOMEM);
9463c16c3aSChris Coulson 
9563c16c3aSChris Coulson 	return ret;
9663c16c3aSChris Coulson }
9763c16c3aSChris Coulson 
9863e2b423SJohn Johansen /**
990d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
1000d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
1010d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
1020d259f04SJohn Johansen  *
1030d259f04SJohn Johansen  * Returns: length of mangled name
1040d259f04SJohn Johansen  */
105bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
1060d259f04SJohn Johansen {
1070d259f04SJohn Johansen 	char *t = target;
1080d259f04SJohn Johansen 
1090d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
1100d259f04SJohn Johansen 		name++;
1110d259f04SJohn Johansen 
1120d259f04SJohn Johansen 	if (target) {
1130d259f04SJohn Johansen 		for (; *name; name++) {
1140d259f04SJohn Johansen 			if (*name == '/')
1150d259f04SJohn Johansen 				*(t)++ = '.';
1160d259f04SJohn Johansen 			else if (isspace(*name))
1170d259f04SJohn Johansen 				*(t)++ = '_';
1180d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
1190d259f04SJohn Johansen 				*(t)++ = *name;
1200d259f04SJohn Johansen 		}
1210d259f04SJohn Johansen 
1220d259f04SJohn Johansen 		*t = 0;
1230d259f04SJohn Johansen 	} else {
1240d259f04SJohn Johansen 		int len = 0;
1250d259f04SJohn Johansen 		for (; *name; name++) {
1260d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
1270d259f04SJohn Johansen 			    strchr("/._-", *name))
1280d259f04SJohn Johansen 				len++;
1290d259f04SJohn Johansen 		}
1300d259f04SJohn Johansen 
1310d259f04SJohn Johansen 		return len;
1320d259f04SJohn Johansen 	}
1330d259f04SJohn Johansen 
1340d259f04SJohn Johansen 	return t - target;
1350d259f04SJohn Johansen }
1360d259f04SJohn Johansen 
137a481f4d9SJohn Johansen 
138a481f4d9SJohn Johansen /*
139a481f4d9SJohn Johansen  * aafs - core fns and data for the policy tree
140a481f4d9SJohn Johansen  */
141a481f4d9SJohn Johansen 
142a481f4d9SJohn Johansen #define AAFS_NAME		"apparmorfs"
143a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt;
144a481f4d9SJohn Johansen static int aafs_count;
145a481f4d9SJohn Johansen 
146a481f4d9SJohn Johansen 
147a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
148a481f4d9SJohn Johansen {
149a0781209SJohn Johansen 	seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino);
150a481f4d9SJohn Johansen 	return 0;
151a481f4d9SJohn Johansen }
152a481f4d9SJohn Johansen 
15327afa27dSAl Viro static void aafs_free_inode(struct inode *inode)
154a481f4d9SJohn Johansen {
155a481f4d9SJohn Johansen 	if (S_ISLNK(inode->i_mode))
156a481f4d9SJohn Johansen 		kfree(inode->i_link);
157f51dcd0fSAl Viro 	free_inode_nonrcu(inode);
158f51dcd0fSAl Viro }
159f51dcd0fSAl Viro 
160a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = {
161a481f4d9SJohn Johansen 	.statfs = simple_statfs,
16227afa27dSAl Viro 	.free_inode = aafs_free_inode,
163a481f4d9SJohn Johansen 	.show_path = aafs_show_path,
164a481f4d9SJohn Johansen };
165a481f4d9SJohn Johansen 
166b0ecc9daSDavid Howells static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)
167a481f4d9SJohn Johansen {
168a481f4d9SJohn Johansen 	static struct tree_descr files[] = { {""} };
169a481f4d9SJohn Johansen 	int error;
170a481f4d9SJohn Johansen 
171a481f4d9SJohn Johansen 	error = simple_fill_super(sb, AAFS_MAGIC, files);
172a481f4d9SJohn Johansen 	if (error)
173a481f4d9SJohn Johansen 		return error;
174a481f4d9SJohn Johansen 	sb->s_op = &aafs_super_ops;
175a481f4d9SJohn Johansen 
176a481f4d9SJohn Johansen 	return 0;
177a481f4d9SJohn Johansen }
178a481f4d9SJohn Johansen 
179b0ecc9daSDavid Howells static int apparmorfs_get_tree(struct fs_context *fc)
180a481f4d9SJohn Johansen {
181b0ecc9daSDavid Howells 	return get_tree_single(fc, apparmorfs_fill_super);
182b0ecc9daSDavid Howells }
183b0ecc9daSDavid Howells 
184b0ecc9daSDavid Howells static const struct fs_context_operations apparmorfs_context_ops = {
185b0ecc9daSDavid Howells 	.get_tree	= apparmorfs_get_tree,
186b0ecc9daSDavid Howells };
187b0ecc9daSDavid Howells 
188b0ecc9daSDavid Howells static int apparmorfs_init_fs_context(struct fs_context *fc)
189b0ecc9daSDavid Howells {
190b0ecc9daSDavid Howells 	fc->ops = &apparmorfs_context_ops;
191b0ecc9daSDavid Howells 	return 0;
192a481f4d9SJohn Johansen }
193a481f4d9SJohn Johansen 
194a481f4d9SJohn Johansen static struct file_system_type aafs_ops = {
195a481f4d9SJohn Johansen 	.owner = THIS_MODULE,
196a481f4d9SJohn Johansen 	.name = AAFS_NAME,
197b0ecc9daSDavid Howells 	.init_fs_context = apparmorfs_init_fs_context,
198a481f4d9SJohn Johansen 	.kill_sb = kill_anon_super,
199a481f4d9SJohn Johansen };
200a481f4d9SJohn Johansen 
201a481f4d9SJohn Johansen /**
202a481f4d9SJohn Johansen  * __aafs_setup_d_inode - basic inode setup for apparmorfs
203a481f4d9SJohn Johansen  * @dir: parent directory for the dentry
204a481f4d9SJohn Johansen  * @dentry: dentry we are seting the inode up for
205a481f4d9SJohn Johansen  * @mode: permissions the file should have
206a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
207a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
208a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used
209a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
210a481f4d9SJohn Johansen  */
211a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
212a481f4d9SJohn Johansen 			       umode_t mode, void *data, char *link,
213a481f4d9SJohn Johansen 			       const struct file_operations *fops,
214a481f4d9SJohn Johansen 			       const struct inode_operations *iops)
215a481f4d9SJohn Johansen {
216a481f4d9SJohn Johansen 	struct inode *inode = new_inode(dir->i_sb);
217a481f4d9SJohn Johansen 
218a481f4d9SJohn Johansen 	AA_BUG(!dir);
219a481f4d9SJohn Johansen 	AA_BUG(!dentry);
220a481f4d9SJohn Johansen 
221a481f4d9SJohn Johansen 	if (!inode)
222a481f4d9SJohn Johansen 		return -ENOMEM;
223a481f4d9SJohn Johansen 
224a481f4d9SJohn Johansen 	inode->i_ino = get_next_ino();
225a481f4d9SJohn Johansen 	inode->i_mode = mode;
226a481f4d9SJohn Johansen 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
227a481f4d9SJohn Johansen 	inode->i_private = data;
228a481f4d9SJohn Johansen 	if (S_ISDIR(mode)) {
229a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_dir_inode_operations;
230a481f4d9SJohn Johansen 		inode->i_fop = &simple_dir_operations;
231a481f4d9SJohn Johansen 		inc_nlink(inode);
232a481f4d9SJohn Johansen 		inc_nlink(dir);
233a481f4d9SJohn Johansen 	} else if (S_ISLNK(mode)) {
234a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_symlink_inode_operations;
235a481f4d9SJohn Johansen 		inode->i_link = link;
236a481f4d9SJohn Johansen 	} else {
237a481f4d9SJohn Johansen 		inode->i_fop = fops;
238a481f4d9SJohn Johansen 	}
239a481f4d9SJohn Johansen 	d_instantiate(dentry, inode);
240a481f4d9SJohn Johansen 	dget(dentry);
241a481f4d9SJohn Johansen 
242a481f4d9SJohn Johansen 	return 0;
243a481f4d9SJohn Johansen }
244a481f4d9SJohn Johansen 
245a481f4d9SJohn Johansen /**
246a481f4d9SJohn Johansen  * aafs_create - create a dentry in the apparmorfs filesystem
247a481f4d9SJohn Johansen  *
248a481f4d9SJohn Johansen  * @name: name of dentry to create
249a481f4d9SJohn Johansen  * @mode: permissions the file should have
250a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
251a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
252a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
253a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
254a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
255a481f4d9SJohn Johansen  *
256a481f4d9SJohn Johansen  * This is the basic "create a xxx" function for apparmorfs.
257a481f4d9SJohn Johansen  *
258a481f4d9SJohn Johansen  * Returns a pointer to a dentry if it succeeds, that must be free with
259a481f4d9SJohn Johansen  * aafs_remove(). Will return ERR_PTR on failure.
260a481f4d9SJohn Johansen  */
261a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode,
262a481f4d9SJohn Johansen 				  struct dentry *parent, void *data, void *link,
263a481f4d9SJohn Johansen 				  const struct file_operations *fops,
264a481f4d9SJohn Johansen 				  const struct inode_operations *iops)
265a481f4d9SJohn Johansen {
266a481f4d9SJohn Johansen 	struct dentry *dentry;
267a481f4d9SJohn Johansen 	struct inode *dir;
268a481f4d9SJohn Johansen 	int error;
269a481f4d9SJohn Johansen 
270a481f4d9SJohn Johansen 	AA_BUG(!name);
271a481f4d9SJohn Johansen 	AA_BUG(!parent);
272a481f4d9SJohn Johansen 
273a481f4d9SJohn Johansen 	if (!(mode & S_IFMT))
274a481f4d9SJohn Johansen 		mode = (mode & S_IALLUGO) | S_IFREG;
275a481f4d9SJohn Johansen 
276a481f4d9SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
277a481f4d9SJohn Johansen 	if (error)
278a481f4d9SJohn Johansen 		return ERR_PTR(error);
279a481f4d9SJohn Johansen 
280a481f4d9SJohn Johansen 	dir = d_inode(parent);
281a481f4d9SJohn Johansen 
282a481f4d9SJohn Johansen 	inode_lock(dir);
283a481f4d9SJohn Johansen 	dentry = lookup_one_len(name, parent, strlen(name));
2845d314a81SDan Carpenter 	if (IS_ERR(dentry)) {
2855d314a81SDan Carpenter 		error = PTR_ERR(dentry);
286a481f4d9SJohn Johansen 		goto fail_lock;
2875d314a81SDan Carpenter 	}
288a481f4d9SJohn Johansen 
289a481f4d9SJohn Johansen 	if (d_really_is_positive(dentry)) {
290a481f4d9SJohn Johansen 		error = -EEXIST;
291a481f4d9SJohn Johansen 		goto fail_dentry;
292a481f4d9SJohn Johansen 	}
293a481f4d9SJohn Johansen 
294a481f4d9SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
295a481f4d9SJohn Johansen 	if (error)
296a481f4d9SJohn Johansen 		goto fail_dentry;
297a481f4d9SJohn Johansen 	inode_unlock(dir);
298a481f4d9SJohn Johansen 
299a481f4d9SJohn Johansen 	return dentry;
300a481f4d9SJohn Johansen 
301a481f4d9SJohn Johansen fail_dentry:
302a481f4d9SJohn Johansen 	dput(dentry);
303a481f4d9SJohn Johansen 
304a481f4d9SJohn Johansen fail_lock:
305a481f4d9SJohn Johansen 	inode_unlock(dir);
306a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
307a481f4d9SJohn Johansen 
308a481f4d9SJohn Johansen 	return ERR_PTR(error);
309a481f4d9SJohn Johansen }
310a481f4d9SJohn Johansen 
311a481f4d9SJohn Johansen /**
312a481f4d9SJohn Johansen  * aafs_create_file - create a file in the apparmorfs filesystem
313a481f4d9SJohn Johansen  *
314a481f4d9SJohn Johansen  * @name: name of dentry to create
315a481f4d9SJohn Johansen  * @mode: permissions the file should have
316a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
317a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
318a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
319a481f4d9SJohn Johansen  *
320a481f4d9SJohn Johansen  * see aafs_create
321a481f4d9SJohn Johansen  */
322a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode,
323a481f4d9SJohn Johansen 				       struct dentry *parent, void *data,
324a481f4d9SJohn Johansen 				       const struct file_operations *fops)
325a481f4d9SJohn Johansen {
326a481f4d9SJohn Johansen 	return aafs_create(name, mode, parent, data, NULL, fops, NULL);
327a481f4d9SJohn Johansen }
328a481f4d9SJohn Johansen 
329a481f4d9SJohn Johansen /**
330a481f4d9SJohn Johansen  * aafs_create_dir - create a directory in the apparmorfs filesystem
331a481f4d9SJohn Johansen  *
332a481f4d9SJohn Johansen  * @name: name of dentry to create
333a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
334a481f4d9SJohn Johansen  *
335a481f4d9SJohn Johansen  * see aafs_create
336a481f4d9SJohn Johansen  */
337a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
338a481f4d9SJohn Johansen {
339a481f4d9SJohn Johansen 	return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
340a481f4d9SJohn Johansen 			   NULL);
341a481f4d9SJohn Johansen }
342a481f4d9SJohn Johansen 
343a481f4d9SJohn Johansen /**
344a481f4d9SJohn Johansen  * aafs_remove - removes a file or directory from the apparmorfs filesystem
345a481f4d9SJohn Johansen  *
346a481f4d9SJohn Johansen  * @dentry: dentry of the file/directory/symlink to removed.
347a481f4d9SJohn Johansen  */
348a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry)
349a481f4d9SJohn Johansen {
350a481f4d9SJohn Johansen 	struct inode *dir;
351a481f4d9SJohn Johansen 
352a481f4d9SJohn Johansen 	if (!dentry || IS_ERR(dentry))
353a481f4d9SJohn Johansen 		return;
354a481f4d9SJohn Johansen 
355a481f4d9SJohn Johansen 	dir = d_inode(dentry->d_parent);
356a481f4d9SJohn Johansen 	inode_lock(dir);
357a481f4d9SJohn Johansen 	if (simple_positive(dentry)) {
358a481f4d9SJohn Johansen 		if (d_is_dir(dentry))
359a481f4d9SJohn Johansen 			simple_rmdir(dir, dentry);
360a481f4d9SJohn Johansen 		else
361a481f4d9SJohn Johansen 			simple_unlink(dir, dentry);
362201218e4SChris Coulson 		d_delete(dentry);
363a481f4d9SJohn Johansen 		dput(dentry);
364a481f4d9SJohn Johansen 	}
365a481f4d9SJohn Johansen 	inode_unlock(dir);
366a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
367a481f4d9SJohn Johansen }
368a481f4d9SJohn Johansen 
369a481f4d9SJohn Johansen 
370a481f4d9SJohn Johansen /*
371a481f4d9SJohn Johansen  * aa_fs - policy load/replace/remove
372a481f4d9SJohn Johansen  */
373a481f4d9SJohn Johansen 
3740d259f04SJohn Johansen /**
37563e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
37663e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
3773ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
37863e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
37963e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
38063e2b423SJohn Johansen  *
38163e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
38263e2b423SJohn Johansen  *          ERR_PTR on failure.
38363e2b423SJohn Johansen  */
3845ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
3855ac8c355SJohn Johansen 						     size_t alloc_size,
3865ac8c355SJohn Johansen 						     size_t copy_size,
38763e2b423SJohn Johansen 						     loff_t *pos)
38863e2b423SJohn Johansen {
3895ac8c355SJohn Johansen 	struct aa_loaddata *data;
39063e2b423SJohn Johansen 
391e6bfa25dSJohn Johansen 	AA_BUG(copy_size > alloc_size);
3923ed02adaSJohn Johansen 
39363e2b423SJohn Johansen 	if (*pos != 0)
39463e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
39563e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
39663e2b423SJohn Johansen 
39763e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
3985d5182caSJohn Johansen 	data = aa_loaddata_alloc(alloc_size);
3995d5182caSJohn Johansen 	if (IS_ERR(data))
4005d5182caSJohn Johansen 		return data;
40163e2b423SJohn Johansen 
4025d5182caSJohn Johansen 	data->size = copy_size;
4035ac8c355SJohn Johansen 	if (copy_from_user(data->data, userbuf, copy_size)) {
40463e2b423SJohn Johansen 		kvfree(data);
40563e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
40663e2b423SJohn Johansen 	}
40763e2b423SJohn Johansen 
40863e2b423SJohn Johansen 	return data;
40963e2b423SJohn Johansen }
41063e2b423SJohn Johansen 
41118e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
412b7fd2c03SJohn Johansen 			     loff_t *pos, struct aa_ns *ns)
4135ac8c355SJohn Johansen {
4145ac8c355SJohn Johansen 	struct aa_loaddata *data;
415637f688dSJohn Johansen 	struct aa_label *label;
416637f688dSJohn Johansen 	ssize_t error;
417cf797c0eSJohn Johansen 
418637f688dSJohn Johansen 	label = begin_current_label_crit_section();
419cf797c0eSJohn Johansen 
4205ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4215ac8c355SJohn Johansen 	 * below after unpack
4225ac8c355SJohn Johansen 	 */
423637f688dSJohn Johansen 	error = aa_may_manage_policy(label, ns, mask);
4245ac8c355SJohn Johansen 	if (error)
4255ac8c355SJohn Johansen 		return error;
42663e2b423SJohn Johansen 
4275ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size, size, pos);
4285ac8c355SJohn Johansen 	error = PTR_ERR(data);
4295ac8c355SJohn Johansen 	if (!IS_ERR(data)) {
430637f688dSJohn Johansen 		error = aa_replace_profiles(ns, label, mask, data);
4315ac8c355SJohn Johansen 		aa_put_loaddata(data);
4325ac8c355SJohn Johansen 	}
433637f688dSJohn Johansen 	end_current_label_crit_section(label);
4345ac8c355SJohn Johansen 
4355ac8c355SJohn Johansen 	return error;
4365ac8c355SJohn Johansen }
4375ac8c355SJohn Johansen 
438b7fd2c03SJohn Johansen /* .load file hook fn to load policy */
43963e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
44063e2b423SJohn Johansen 			    loff_t *pos)
44163e2b423SJohn Johansen {
442b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
44318e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
444b7fd2c03SJohn Johansen 
445b7fd2c03SJohn Johansen 	aa_put_ns(ns);
44663e2b423SJohn Johansen 
44763e2b423SJohn Johansen 	return error;
44863e2b423SJohn Johansen }
44963e2b423SJohn Johansen 
45063e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
4516038f373SArnd Bergmann 	.write = profile_load,
4526038f373SArnd Bergmann 	.llseek = default_llseek,
45363e2b423SJohn Johansen };
45463e2b423SJohn Johansen 
45563e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
45663e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
45763e2b423SJohn Johansen 			       size_t size, loff_t *pos)
45863e2b423SJohn Johansen {
459b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
46018e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
46118e99f19SJohn Johansen 				  buf, size, pos, ns);
462b7fd2c03SJohn Johansen 	aa_put_ns(ns);
46363e2b423SJohn Johansen 
46463e2b423SJohn Johansen 	return error;
46563e2b423SJohn Johansen }
46663e2b423SJohn Johansen 
46763e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
4686038f373SArnd Bergmann 	.write = profile_replace,
4696038f373SArnd Bergmann 	.llseek = default_llseek,
47063e2b423SJohn Johansen };
47163e2b423SJohn Johansen 
472b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */
47363e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
47463e2b423SJohn Johansen 			      size_t size, loff_t *pos)
47563e2b423SJohn Johansen {
4765ac8c355SJohn Johansen 	struct aa_loaddata *data;
477637f688dSJohn Johansen 	struct aa_label *label;
47863e2b423SJohn Johansen 	ssize_t error;
479b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
48063e2b423SJohn Johansen 
481637f688dSJohn Johansen 	label = begin_current_label_crit_section();
4825ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4835ac8c355SJohn Johansen 	 * below after unpack
4845ac8c355SJohn Johansen 	 */
485637f688dSJohn Johansen 	error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY);
4865ac8c355SJohn Johansen 	if (error)
4875ac8c355SJohn Johansen 		goto out;
4885ac8c355SJohn Johansen 
48963e2b423SJohn Johansen 	/*
49063e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
49163e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
49263e2b423SJohn Johansen 	 */
4935ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
49463e2b423SJohn Johansen 
49563e2b423SJohn Johansen 	error = PTR_ERR(data);
49663e2b423SJohn Johansen 	if (!IS_ERR(data)) {
4975ac8c355SJohn Johansen 		data->data[size] = 0;
498637f688dSJohn Johansen 		error = aa_remove_profiles(ns, label, data->data, size);
4995ac8c355SJohn Johansen 		aa_put_loaddata(data);
50063e2b423SJohn Johansen 	}
5015ac8c355SJohn Johansen  out:
502637f688dSJohn Johansen 	end_current_label_crit_section(label);
503b7fd2c03SJohn Johansen 	aa_put_ns(ns);
50463e2b423SJohn Johansen 	return error;
50563e2b423SJohn Johansen }
50663e2b423SJohn Johansen 
50763e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
5086038f373SArnd Bergmann 	.write = profile_remove,
5096038f373SArnd Bergmann 	.llseek = default_llseek,
51063e2b423SJohn Johansen };
51163e2b423SJohn Johansen 
512d9bf2c26SJohn Johansen struct aa_revision {
513d9bf2c26SJohn Johansen 	struct aa_ns *ns;
514d9bf2c26SJohn Johansen 	long last_read;
515d9bf2c26SJohn Johansen };
516d9bf2c26SJohn Johansen 
517d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */
518d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file)
519d9bf2c26SJohn Johansen {
520d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
521d9bf2c26SJohn Johansen 
522d9bf2c26SJohn Johansen 	if (rev) {
523d9bf2c26SJohn Johansen 		aa_put_ns(rev->ns);
524d9bf2c26SJohn Johansen 		kfree(rev);
525d9bf2c26SJohn Johansen 	}
526d9bf2c26SJohn Johansen 
527d9bf2c26SJohn Johansen 	return 0;
528d9bf2c26SJohn Johansen }
529d9bf2c26SJohn Johansen 
530d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf,
531d9bf2c26SJohn Johansen 				size_t size, loff_t *ppos)
532d9bf2c26SJohn Johansen {
533d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
534d9bf2c26SJohn Johansen 	char buffer[32];
535d9bf2c26SJohn Johansen 	long last_read;
536d9bf2c26SJohn Johansen 	int avail;
537d9bf2c26SJohn Johansen 
538feb3c766SJohn Johansen 	mutex_lock_nested(&rev->ns->lock, rev->ns->level);
539d9bf2c26SJohn Johansen 	last_read = rev->last_read;
540d9bf2c26SJohn Johansen 	if (last_read == rev->ns->revision) {
541d9bf2c26SJohn Johansen 		mutex_unlock(&rev->ns->lock);
542d9bf2c26SJohn Johansen 		if (file->f_flags & O_NONBLOCK)
543d9bf2c26SJohn Johansen 			return -EAGAIN;
544d9bf2c26SJohn Johansen 		if (wait_event_interruptible(rev->ns->wait,
545d9bf2c26SJohn Johansen 					     last_read !=
546d9bf2c26SJohn Johansen 					     READ_ONCE(rev->ns->revision)))
547d9bf2c26SJohn Johansen 			return -ERESTARTSYS;
548feb3c766SJohn Johansen 		mutex_lock_nested(&rev->ns->lock, rev->ns->level);
549d9bf2c26SJohn Johansen 	}
550d9bf2c26SJohn Johansen 
551d9bf2c26SJohn Johansen 	avail = sprintf(buffer, "%ld\n", rev->ns->revision);
552d9bf2c26SJohn Johansen 	if (*ppos + size > avail) {
553d9bf2c26SJohn Johansen 		rev->last_read = rev->ns->revision;
554d9bf2c26SJohn Johansen 		*ppos = 0;
555d9bf2c26SJohn Johansen 	}
556d9bf2c26SJohn Johansen 	mutex_unlock(&rev->ns->lock);
557d9bf2c26SJohn Johansen 
558d9bf2c26SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, buffer, avail);
559d9bf2c26SJohn Johansen }
560d9bf2c26SJohn Johansen 
561d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file)
562d9bf2c26SJohn Johansen {
563d9bf2c26SJohn Johansen 	struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
564d9bf2c26SJohn Johansen 
565d9bf2c26SJohn Johansen 	if (!rev)
566d9bf2c26SJohn Johansen 		return -ENOMEM;
567d9bf2c26SJohn Johansen 
568d9bf2c26SJohn Johansen 	rev->ns = aa_get_ns(inode->i_private);
569d9bf2c26SJohn Johansen 	if (!rev->ns)
570d9bf2c26SJohn Johansen 		rev->ns = aa_get_current_ns();
571d9bf2c26SJohn Johansen 	file->private_data = rev;
572d9bf2c26SJohn Johansen 
573d9bf2c26SJohn Johansen 	return 0;
574d9bf2c26SJohn Johansen }
575d9bf2c26SJohn Johansen 
576e6c5a7d9SAl Viro static __poll_t ns_revision_poll(struct file *file, poll_table *pt)
577d9bf2c26SJohn Johansen {
578d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
579e6c5a7d9SAl Viro 	__poll_t mask = 0;
580d9bf2c26SJohn Johansen 
581d9bf2c26SJohn Johansen 	if (rev) {
582feb3c766SJohn Johansen 		mutex_lock_nested(&rev->ns->lock, rev->ns->level);
583d9bf2c26SJohn Johansen 		poll_wait(file, &rev->ns->wait, pt);
584d9bf2c26SJohn Johansen 		if (rev->last_read < rev->ns->revision)
585a9a08845SLinus Torvalds 			mask |= EPOLLIN | EPOLLRDNORM;
586d9bf2c26SJohn Johansen 		mutex_unlock(&rev->ns->lock);
587d9bf2c26SJohn Johansen 	}
588d9bf2c26SJohn Johansen 
589d9bf2c26SJohn Johansen 	return mask;
590d9bf2c26SJohn Johansen }
591d9bf2c26SJohn Johansen 
5925d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns)
5935d5182caSJohn Johansen {
5940df34a64SJohn Johansen 	WRITE_ONCE(ns->revision, READ_ONCE(ns->revision) + 1);
595d9bf2c26SJohn Johansen 	wake_up_interruptible(&ns->wait);
5965d5182caSJohn Johansen }
5975d5182caSJohn Johansen 
598d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = {
599d9bf2c26SJohn Johansen 	.owner		= THIS_MODULE,
600d9bf2c26SJohn Johansen 	.open		= ns_revision_open,
601d9bf2c26SJohn Johansen 	.poll		= ns_revision_poll,
602d9bf2c26SJohn Johansen 	.read		= ns_revision_read,
603d9bf2c26SJohn Johansen 	.llseek		= generic_file_llseek,
604d9bf2c26SJohn Johansen 	.release	= ns_revision_release,
605d9bf2c26SJohn Johansen };
606d9bf2c26SJohn Johansen 
6074f3b3f2dSJohn Johansen static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
6084f3b3f2dSJohn Johansen 			     const char *match_str, size_t match_len)
6094f3b3f2dSJohn Johansen {
610f4585bc2STyler Hicks 	struct aa_perms tmp = { };
6114f3b3f2dSJohn Johansen 	struct aa_dfa *dfa;
6124f3b3f2dSJohn Johansen 	unsigned int state = 0;
6134f3b3f2dSJohn Johansen 
614637f688dSJohn Johansen 	if (profile_unconfined(profile))
6154f3b3f2dSJohn Johansen 		return;
6164f3b3f2dSJohn Johansen 	if (profile->file.dfa && *match_str == AA_CLASS_FILE) {
6174f3b3f2dSJohn Johansen 		dfa = profile->file.dfa;
6184f3b3f2dSJohn Johansen 		state = aa_dfa_match_len(dfa, profile->file.start,
6194f3b3f2dSJohn Johansen 					 match_str + 1, match_len - 1);
6204f3b3f2dSJohn Johansen 		if (state) {
6214f3b3f2dSJohn Johansen 			struct path_cond cond = { };
6224f3b3f2dSJohn Johansen 
6234f3b3f2dSJohn Johansen 			tmp = aa_compute_fperms(dfa, state, &cond);
6244f3b3f2dSJohn Johansen 		}
6254f3b3f2dSJohn Johansen 	} else if (profile->policy.dfa) {
626b9590ad4SJohn Johansen 		if (!PROFILE_MEDIATES(profile, *match_str))
6274f3b3f2dSJohn Johansen 			return;	/* no change to current perms */
6284f3b3f2dSJohn Johansen 		dfa = profile->policy.dfa;
6294f3b3f2dSJohn Johansen 		state = aa_dfa_match_len(dfa, profile->policy.start[0],
6304f3b3f2dSJohn Johansen 					 match_str, match_len);
6314f3b3f2dSJohn Johansen 		if (state)
6324f3b3f2dSJohn Johansen 			aa_compute_perms(dfa, state, &tmp);
6334f3b3f2dSJohn Johansen 	}
6344f3b3f2dSJohn Johansen 	aa_apply_modes_to_perms(profile, &tmp);
635317d9a05SJohn Johansen 	aa_perms_accum_raw(perms, &tmp);
6364f3b3f2dSJohn Johansen }
6374f3b3f2dSJohn Johansen 
6384f3b3f2dSJohn Johansen 
639e025be0fSWilliam Hua /**
640e025be0fSWilliam Hua  * query_data - queries a policy and writes its data to buf
641e025be0fSWilliam Hua  * @buf: the resulting data is stored here (NOT NULL)
642e025be0fSWilliam Hua  * @buf_len: size of buf
643e025be0fSWilliam Hua  * @query: query string used to retrieve data
644e025be0fSWilliam Hua  * @query_len: size of query including second NUL byte
645e025be0fSWilliam Hua  *
646e025be0fSWilliam Hua  * The buffers pointed to by buf and query may overlap. The query buffer is
647e025be0fSWilliam Hua  * parsed before buf is written to.
648e025be0fSWilliam Hua  *
649e025be0fSWilliam Hua  * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
650e025be0fSWilliam Hua  * the security confinement context and <KEY> is the name of the data to
651e025be0fSWilliam Hua  * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
652e025be0fSWilliam Hua  *
653e025be0fSWilliam Hua  * Don't expect the contents of buf to be preserved on failure.
654e025be0fSWilliam Hua  *
655e025be0fSWilliam Hua  * Returns: number of characters written to buf or -errno on failure
656e025be0fSWilliam Hua  */
657e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len,
658e025be0fSWilliam Hua 			  char *query, size_t query_len)
659e025be0fSWilliam Hua {
660e025be0fSWilliam Hua 	char *out;
661e025be0fSWilliam Hua 	const char *key;
662317d9a05SJohn Johansen 	struct label_it i;
663637f688dSJohn Johansen 	struct aa_label *label, *curr;
664317d9a05SJohn Johansen 	struct aa_profile *profile;
665e025be0fSWilliam Hua 	struct aa_data *data;
666e025be0fSWilliam Hua 	u32 bytes, blocks;
667e025be0fSWilliam Hua 	__le32 outle32;
668e025be0fSWilliam Hua 
669e025be0fSWilliam Hua 	if (!query_len)
670e025be0fSWilliam Hua 		return -EINVAL; /* need a query */
671e025be0fSWilliam Hua 
672e025be0fSWilliam Hua 	key = query + strnlen(query, query_len) + 1;
673e025be0fSWilliam Hua 	if (key + 1 >= query + query_len)
674e025be0fSWilliam Hua 		return -EINVAL; /* not enough space for a non-empty key */
675e025be0fSWilliam Hua 	if (key + strnlen(key, query + query_len - key) >= query + query_len)
676e025be0fSWilliam Hua 		return -EINVAL; /* must end with NUL */
677e025be0fSWilliam Hua 
678e025be0fSWilliam Hua 	if (buf_len < sizeof(bytes) + sizeof(blocks))
679e025be0fSWilliam Hua 		return -EINVAL; /* not enough space */
680e025be0fSWilliam Hua 
681637f688dSJohn Johansen 	curr = begin_current_label_crit_section();
682637f688dSJohn Johansen 	label = aa_label_parse(curr, query, GFP_KERNEL, false, false);
683637f688dSJohn Johansen 	end_current_label_crit_section(curr);
684637f688dSJohn Johansen 	if (IS_ERR(label))
685637f688dSJohn Johansen 		return PTR_ERR(label);
686e025be0fSWilliam Hua 
687e025be0fSWilliam Hua 	/* We are going to leave space for two numbers. The first is the total
688e025be0fSWilliam Hua 	 * number of bytes we are writing after the first number. This is so
689e025be0fSWilliam Hua 	 * users can read the full output without reallocation.
690e025be0fSWilliam Hua 	 *
691e025be0fSWilliam Hua 	 * The second number is the number of data blocks we're writing. An
692e025be0fSWilliam Hua 	 * application might be confined by multiple policies having data in
693e025be0fSWilliam Hua 	 * the same key.
694e025be0fSWilliam Hua 	 */
695e025be0fSWilliam Hua 	memset(buf, 0, sizeof(bytes) + sizeof(blocks));
696e025be0fSWilliam Hua 	out = buf + sizeof(bytes) + sizeof(blocks);
697e025be0fSWilliam Hua 
698e025be0fSWilliam Hua 	blocks = 0;
699317d9a05SJohn Johansen 	label_for_each_confined(i, label, profile) {
700317d9a05SJohn Johansen 		if (!profile->data)
701317d9a05SJohn Johansen 			continue;
702317d9a05SJohn Johansen 
703317d9a05SJohn Johansen 		data = rhashtable_lookup_fast(profile->data, &key,
704317d9a05SJohn Johansen 					      profile->data->p);
705e025be0fSWilliam Hua 
706e025be0fSWilliam Hua 		if (data) {
707317d9a05SJohn Johansen 			if (out + sizeof(outle32) + data->size > buf +
708317d9a05SJohn Johansen 			    buf_len) {
709637f688dSJohn Johansen 				aa_put_label(label);
710e025be0fSWilliam Hua 				return -EINVAL; /* not enough space */
711637f688dSJohn Johansen 			}
712e025be0fSWilliam Hua 			outle32 = __cpu_to_le32(data->size);
713e025be0fSWilliam Hua 			memcpy(out, &outle32, sizeof(outle32));
714e025be0fSWilliam Hua 			out += sizeof(outle32);
715e025be0fSWilliam Hua 			memcpy(out, data->data, data->size);
716e025be0fSWilliam Hua 			out += data->size;
717e025be0fSWilliam Hua 			blocks++;
718e025be0fSWilliam Hua 		}
719e025be0fSWilliam Hua 	}
720637f688dSJohn Johansen 	aa_put_label(label);
721e025be0fSWilliam Hua 
722e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
723e025be0fSWilliam Hua 	memcpy(buf, &outle32, sizeof(outle32));
724e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(blocks);
725e025be0fSWilliam Hua 	memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
726e025be0fSWilliam Hua 
727e025be0fSWilliam Hua 	return out - buf;
728e025be0fSWilliam Hua }
729e025be0fSWilliam Hua 
7304f3b3f2dSJohn Johansen /**
7314f3b3f2dSJohn Johansen  * query_label - queries a label and writes permissions to buf
7324f3b3f2dSJohn Johansen  * @buf: the resulting permissions string is stored here (NOT NULL)
7334f3b3f2dSJohn Johansen  * @buf_len: size of buf
7344f3b3f2dSJohn Johansen  * @query: binary query string to match against the dfa
7354f3b3f2dSJohn Johansen  * @query_len: size of query
7364f3b3f2dSJohn Johansen  * @view_only: only compute for querier's view
7374f3b3f2dSJohn Johansen  *
7384f3b3f2dSJohn Johansen  * The buffers pointed to by buf and query may overlap. The query buffer is
7394f3b3f2dSJohn Johansen  * parsed before buf is written to.
7404f3b3f2dSJohn Johansen  *
7414f3b3f2dSJohn Johansen  * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is
7424f3b3f2dSJohn Johansen  * the name of the label, in the current namespace, that is to be queried and
7434f3b3f2dSJohn Johansen  * DFA_STRING is a binary string to match against the label(s)'s DFA.
7444f3b3f2dSJohn Johansen  *
7454f3b3f2dSJohn Johansen  * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters
7464f3b3f2dSJohn Johansen  * but must *not* be NUL terminated.
7474f3b3f2dSJohn Johansen  *
7484f3b3f2dSJohn Johansen  * Returns: number of characters written to buf or -errno on failure
7494f3b3f2dSJohn Johansen  */
7504f3b3f2dSJohn Johansen static ssize_t query_label(char *buf, size_t buf_len,
7514f3b3f2dSJohn Johansen 			   char *query, size_t query_len, bool view_only)
7524f3b3f2dSJohn Johansen {
753317d9a05SJohn Johansen 	struct aa_profile *profile;
754637f688dSJohn Johansen 	struct aa_label *label, *curr;
7554f3b3f2dSJohn Johansen 	char *label_name, *match_str;
7564f3b3f2dSJohn Johansen 	size_t label_name_len, match_len;
7574f3b3f2dSJohn Johansen 	struct aa_perms perms;
758317d9a05SJohn Johansen 	struct label_it i;
7594f3b3f2dSJohn Johansen 
7604f3b3f2dSJohn Johansen 	if (!query_len)
7614f3b3f2dSJohn Johansen 		return -EINVAL;
7624f3b3f2dSJohn Johansen 
7634f3b3f2dSJohn Johansen 	label_name = query;
7644f3b3f2dSJohn Johansen 	label_name_len = strnlen(query, query_len);
7654f3b3f2dSJohn Johansen 	if (!label_name_len || label_name_len == query_len)
7664f3b3f2dSJohn Johansen 		return -EINVAL;
7674f3b3f2dSJohn Johansen 
7684f3b3f2dSJohn Johansen 	/**
7694f3b3f2dSJohn Johansen 	 * The extra byte is to account for the null byte between the
7704f3b3f2dSJohn Johansen 	 * profile name and dfa string. profile_name_len is greater
7714f3b3f2dSJohn Johansen 	 * than zero and less than query_len, so a byte can be safely
7724f3b3f2dSJohn Johansen 	 * added or subtracted.
7734f3b3f2dSJohn Johansen 	 */
7744f3b3f2dSJohn Johansen 	match_str = label_name + label_name_len + 1;
7754f3b3f2dSJohn Johansen 	match_len = query_len - label_name_len - 1;
7764f3b3f2dSJohn Johansen 
777637f688dSJohn Johansen 	curr = begin_current_label_crit_section();
778637f688dSJohn Johansen 	label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false);
779637f688dSJohn Johansen 	end_current_label_crit_section(curr);
780637f688dSJohn Johansen 	if (IS_ERR(label))
781637f688dSJohn Johansen 		return PTR_ERR(label);
7824f3b3f2dSJohn Johansen 
7834f3b3f2dSJohn Johansen 	perms = allperms;
784317d9a05SJohn Johansen 	if (view_only) {
785317d9a05SJohn Johansen 		label_for_each_in_ns(i, labels_ns(label), label, profile) {
786317d9a05SJohn Johansen 			profile_query_cb(profile, &perms, match_str, match_len);
787317d9a05SJohn Johansen 		}
788317d9a05SJohn Johansen 	} else {
789317d9a05SJohn Johansen 		label_for_each(i, label, profile) {
790317d9a05SJohn Johansen 			profile_query_cb(profile, &perms, match_str, match_len);
791317d9a05SJohn Johansen 		}
792317d9a05SJohn Johansen 	}
793317d9a05SJohn Johansen 	aa_put_label(label);
7944f3b3f2dSJohn Johansen 
7954f3b3f2dSJohn Johansen 	return scnprintf(buf, buf_len,
7964f3b3f2dSJohn Johansen 		      "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n",
7974f3b3f2dSJohn Johansen 		      perms.allow, perms.deny, perms.audit, perms.quiet);
7984f3b3f2dSJohn Johansen }
7994f3b3f2dSJohn Johansen 
8001dea3b41SJohn Johansen /*
8011dea3b41SJohn Johansen  * Transaction based IO.
8021dea3b41SJohn Johansen  * The file expects a write which triggers the transaction, and then
8031dea3b41SJohn Johansen  * possibly a read(s) which collects the result - which is stored in a
8041dea3b41SJohn Johansen  * file-local buffer. Once a new write is performed, a new set of results
8051dea3b41SJohn Johansen  * are stored in the file-local buffer.
8061dea3b41SJohn Johansen  */
8071dea3b41SJohn Johansen struct multi_transaction {
8081dea3b41SJohn Johansen 	struct kref count;
8091dea3b41SJohn Johansen 	ssize_t size;
810fe9fd23eSGustavo A. R. Silva 	char data[];
8111dea3b41SJohn Johansen };
8121dea3b41SJohn Johansen 
8131dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction))
8141dea3b41SJohn Johansen 
8151dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref)
8161dea3b41SJohn Johansen {
8171dea3b41SJohn Johansen 	struct multi_transaction *t;
8181dea3b41SJohn Johansen 
8191dea3b41SJohn Johansen 	t = container_of(kref, struct multi_transaction, count);
8201dea3b41SJohn Johansen 	free_page((unsigned long) t);
8211dea3b41SJohn Johansen }
8221dea3b41SJohn Johansen 
8231dea3b41SJohn Johansen static struct multi_transaction *
8241dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t)
8251dea3b41SJohn Johansen {
8261dea3b41SJohn Johansen 	if  (t)
8271dea3b41SJohn Johansen 		kref_get(&(t->count));
8281dea3b41SJohn Johansen 
8291dea3b41SJohn Johansen 	return t;
8301dea3b41SJohn Johansen }
8311dea3b41SJohn Johansen 
8321dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t)
8331dea3b41SJohn Johansen {
8341dea3b41SJohn Johansen 	if (t)
8351dea3b41SJohn Johansen 		kref_put(&(t->count), multi_transaction_kref);
8361dea3b41SJohn Johansen }
8371dea3b41SJohn Johansen 
8381dea3b41SJohn Johansen /* does not increment @new's count */
8391dea3b41SJohn Johansen static void multi_transaction_set(struct file *file,
8401dea3b41SJohn Johansen 				  struct multi_transaction *new, size_t n)
8411dea3b41SJohn Johansen {
8421dea3b41SJohn Johansen 	struct multi_transaction *old;
8431dea3b41SJohn Johansen 
8441dea3b41SJohn Johansen 	AA_BUG(n > MULTI_TRANSACTION_LIMIT);
8451dea3b41SJohn Johansen 
8461dea3b41SJohn Johansen 	new->size = n;
847*d0d845a7SHamza Mahfooz 	spin_lock(&file->f_lock);
8481dea3b41SJohn Johansen 	old = (struct multi_transaction *) file->private_data;
8491dea3b41SJohn Johansen 	file->private_data = new;
850*d0d845a7SHamza Mahfooz 	spin_unlock(&file->f_lock);
8511dea3b41SJohn Johansen 	put_multi_transaction(old);
8521dea3b41SJohn Johansen }
8531dea3b41SJohn Johansen 
8541dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file,
8551dea3b41SJohn Johansen 						       const char __user *buf,
8561dea3b41SJohn Johansen 						       size_t size)
8571dea3b41SJohn Johansen {
8581dea3b41SJohn Johansen 	struct multi_transaction *t;
8591dea3b41SJohn Johansen 
8601dea3b41SJohn Johansen 	if (size > MULTI_TRANSACTION_LIMIT - 1)
8611dea3b41SJohn Johansen 		return ERR_PTR(-EFBIG);
8621dea3b41SJohn Johansen 
8631dea3b41SJohn Johansen 	t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
8641dea3b41SJohn Johansen 	if (!t)
8651dea3b41SJohn Johansen 		return ERR_PTR(-ENOMEM);
8661dea3b41SJohn Johansen 	kref_init(&t->count);
8671dea3b41SJohn Johansen 	if (copy_from_user(t->data, buf, size))
8681dea3b41SJohn Johansen 		return ERR_PTR(-EFAULT);
8691dea3b41SJohn Johansen 
8701dea3b41SJohn Johansen 	return t;
8711dea3b41SJohn Johansen }
8721dea3b41SJohn Johansen 
8731dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf,
8741dea3b41SJohn Johansen 				       size_t size, loff_t *pos)
8751dea3b41SJohn Johansen {
8761dea3b41SJohn Johansen 	struct multi_transaction *t;
8771dea3b41SJohn Johansen 	ssize_t ret;
8781dea3b41SJohn Johansen 
879*d0d845a7SHamza Mahfooz 	spin_lock(&file->f_lock);
8801dea3b41SJohn Johansen 	t = get_multi_transaction(file->private_data);
881*d0d845a7SHamza Mahfooz 	spin_unlock(&file->f_lock);
882*d0d845a7SHamza Mahfooz 
8831dea3b41SJohn Johansen 	if (!t)
8841dea3b41SJohn Johansen 		return 0;
8851dea3b41SJohn Johansen 
8861dea3b41SJohn Johansen 	ret = simple_read_from_buffer(buf, size, pos, t->data, t->size);
8871dea3b41SJohn Johansen 	put_multi_transaction(t);
8881dea3b41SJohn Johansen 
8891dea3b41SJohn Johansen 	return ret;
8901dea3b41SJohn Johansen }
8911dea3b41SJohn Johansen 
8921dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file)
8931dea3b41SJohn Johansen {
8941dea3b41SJohn Johansen 	put_multi_transaction(file->private_data);
8951dea3b41SJohn Johansen 
8961dea3b41SJohn Johansen 	return 0;
8971dea3b41SJohn Johansen }
8981dea3b41SJohn Johansen 
899317d9a05SJohn Johansen #define QUERY_CMD_LABEL		"label\0"
900317d9a05SJohn Johansen #define QUERY_CMD_LABEL_LEN	6
9014f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE	"profile\0"
9024f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE_LEN	8
903317d9a05SJohn Johansen #define QUERY_CMD_LABELALL	"labelall\0"
904317d9a05SJohn Johansen #define QUERY_CMD_LABELALL_LEN	9
905e025be0fSWilliam Hua #define QUERY_CMD_DATA		"data\0"
906e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN	5
907e025be0fSWilliam Hua 
908e025be0fSWilliam Hua /**
909e025be0fSWilliam Hua  * aa_write_access - generic permissions and data query
910e025be0fSWilliam Hua  * @file: pointer to open apparmorfs/access file
911e025be0fSWilliam Hua  * @ubuf: user buffer containing the complete query string (NOT NULL)
912e025be0fSWilliam Hua  * @count: size of ubuf
913e025be0fSWilliam Hua  * @ppos: position in the file (MUST BE ZERO)
914e025be0fSWilliam Hua  *
915e025be0fSWilliam Hua  * Allows for one permissions or data query per open(), write(), and read()
916e025be0fSWilliam Hua  * sequence. The only queries currently supported are label-based queries for
917e025be0fSWilliam Hua  * permissions or data.
918e025be0fSWilliam Hua  *
919e025be0fSWilliam Hua  * For permissions queries, ubuf must begin with "label\0", followed by the
920e025be0fSWilliam Hua  * profile query specific format described in the query_label() function
921e025be0fSWilliam Hua  * documentation.
922e025be0fSWilliam Hua  *
923e025be0fSWilliam Hua  * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
924e025be0fSWilliam Hua  * <LABEL> is the name of the security confinement context and <KEY> is the
925e025be0fSWilliam Hua  * name of the data to retrieve.
926e025be0fSWilliam Hua  *
927e025be0fSWilliam Hua  * Returns: number of bytes written or -errno on failure
928e025be0fSWilliam Hua  */
929e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
930e025be0fSWilliam Hua 			       size_t count, loff_t *ppos)
931e025be0fSWilliam Hua {
9321dea3b41SJohn Johansen 	struct multi_transaction *t;
933e025be0fSWilliam Hua 	ssize_t len;
934e025be0fSWilliam Hua 
935e025be0fSWilliam Hua 	if (*ppos)
936e025be0fSWilliam Hua 		return -ESPIPE;
937e025be0fSWilliam Hua 
9381dea3b41SJohn Johansen 	t = multi_transaction_new(file, ubuf, count);
9391dea3b41SJohn Johansen 	if (IS_ERR(t))
9401dea3b41SJohn Johansen 		return PTR_ERR(t);
941e025be0fSWilliam Hua 
9424f3b3f2dSJohn Johansen 	if (count > QUERY_CMD_PROFILE_LEN &&
9434f3b3f2dSJohn Johansen 	    !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) {
9444f3b3f2dSJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
9454f3b3f2dSJohn Johansen 				  t->data + QUERY_CMD_PROFILE_LEN,
9464f3b3f2dSJohn Johansen 				  count - QUERY_CMD_PROFILE_LEN, true);
947317d9a05SJohn Johansen 	} else if (count > QUERY_CMD_LABEL_LEN &&
948317d9a05SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) {
949317d9a05SJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
950317d9a05SJohn Johansen 				  t->data + QUERY_CMD_LABEL_LEN,
951317d9a05SJohn Johansen 				  count - QUERY_CMD_LABEL_LEN, true);
952317d9a05SJohn Johansen 	} else if (count > QUERY_CMD_LABELALL_LEN &&
953317d9a05SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_LABELALL,
954317d9a05SJohn Johansen 			   QUERY_CMD_LABELALL_LEN)) {
955317d9a05SJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
956317d9a05SJohn Johansen 				  t->data + QUERY_CMD_LABELALL_LEN,
957317d9a05SJohn Johansen 				  count - QUERY_CMD_LABELALL_LEN, false);
9584f3b3f2dSJohn Johansen 	} else if (count > QUERY_CMD_DATA_LEN &&
9591dea3b41SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
9601dea3b41SJohn Johansen 		len = query_data(t->data, MULTI_TRANSACTION_LIMIT,
9611dea3b41SJohn Johansen 				 t->data + QUERY_CMD_DATA_LEN,
962e025be0fSWilliam Hua 				 count - QUERY_CMD_DATA_LEN);
963e025be0fSWilliam Hua 	} else
964e025be0fSWilliam Hua 		len = -EINVAL;
965e025be0fSWilliam Hua 
9661dea3b41SJohn Johansen 	if (len < 0) {
9671dea3b41SJohn Johansen 		put_multi_transaction(t);
968e025be0fSWilliam Hua 		return len;
9691dea3b41SJohn Johansen 	}
970e025be0fSWilliam Hua 
9711dea3b41SJohn Johansen 	multi_transaction_set(file, t, len);
972e025be0fSWilliam Hua 
973e025be0fSWilliam Hua 	return count;
974e025be0fSWilliam Hua }
975e025be0fSWilliam Hua 
976c97204baSJohn Johansen static const struct file_operations aa_sfs_access = {
977e025be0fSWilliam Hua 	.write		= aa_write_access,
9781dea3b41SJohn Johansen 	.read		= multi_transaction_read,
9791dea3b41SJohn Johansen 	.release	= multi_transaction_release,
980e025be0fSWilliam Hua 	.llseek		= generic_file_llseek,
981e025be0fSWilliam Hua };
982e025be0fSWilliam Hua 
983c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v)
984e74abcf3SKees Cook {
985c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file = seq->private;
986e74abcf3SKees Cook 
987e74abcf3SKees Cook 	if (!fs_file)
988e74abcf3SKees Cook 		return 0;
989e74abcf3SKees Cook 
990e74abcf3SKees Cook 	switch (fs_file->v_type) {
991c97204baSJohn Johansen 	case AA_SFS_TYPE_BOOLEAN:
992e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
993e74abcf3SKees Cook 		break;
994c97204baSJohn Johansen 	case AA_SFS_TYPE_STRING:
995a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
996a9bf8e9fSKees Cook 		break;
997c97204baSJohn Johansen 	case AA_SFS_TYPE_U64:
998e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
999e74abcf3SKees Cook 		break;
1000e74abcf3SKees Cook 	default:
1001e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
1002e74abcf3SKees Cook 		break;
1003e74abcf3SKees Cook 	}
1004e74abcf3SKees Cook 
1005e74abcf3SKees Cook 	return 0;
1006e74abcf3SKees Cook }
1007e74abcf3SKees Cook 
1008c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file)
1009e74abcf3SKees Cook {
1010c97204baSJohn Johansen 	return single_open(file, aa_sfs_seq_show, inode->i_private);
1011e74abcf3SKees Cook }
1012e74abcf3SKees Cook 
1013c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = {
1014e74abcf3SKees Cook 	.owner		= THIS_MODULE,
1015c97204baSJohn Johansen 	.open		= aa_sfs_seq_open,
1016e74abcf3SKees Cook 	.read		= seq_read,
1017e74abcf3SKees Cook 	.llseek		= seq_lseek,
1018e74abcf3SKees Cook 	.release	= single_release,
1019e74abcf3SKees Cook };
1020e74abcf3SKees Cook 
102152b97de3SJohn Johansen /*
102252b97de3SJohn Johansen  * profile based file operations
102352b97de3SJohn Johansen  *     policy/profiles/XXXX/profiles/ *
102452b97de3SJohn Johansen  */
102552b97de3SJohn Johansen 
102652b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME)						      \
102752b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
102852b97de3SJohn Johansen {									      \
102952b97de3SJohn Johansen 	return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show);    \
103052b97de3SJohn Johansen }									      \
103152b97de3SJohn Johansen 									      \
103252b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = {	      \
103352b97de3SJohn Johansen 	.owner		= THIS_MODULE,					      \
103452b97de3SJohn Johansen 	.open		= seq_profile_ ##NAME ##_open,			      \
103552b97de3SJohn Johansen 	.read		= seq_read,					      \
103652b97de3SJohn Johansen 	.llseek		= seq_lseek,					      \
103752b97de3SJohn Johansen 	.release	= seq_profile_release,				      \
103852b97de3SJohn Johansen }									      \
103952b97de3SJohn Johansen 
104052b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file,
10410d259f04SJohn Johansen 			    int (*show)(struct seq_file *, void *))
10420d259f04SJohn Johansen {
10438399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
10448399588aSJohn Johansen 	int error = single_open(file, show, proxy);
104563e2b423SJohn Johansen 
10460d259f04SJohn Johansen 	if (error) {
10470d259f04SJohn Johansen 		file->private_data = NULL;
10488399588aSJohn Johansen 		aa_put_proxy(proxy);
10490d259f04SJohn Johansen 	}
10500d259f04SJohn Johansen 
10510d259f04SJohn Johansen 	return error;
10520d259f04SJohn Johansen }
10530d259f04SJohn Johansen 
105452b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file)
10550d259f04SJohn Johansen {
10560d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
10570d259f04SJohn Johansen 	if (seq)
10588399588aSJohn Johansen 		aa_put_proxy(seq->private);
10590d259f04SJohn Johansen 	return single_release(inode, file);
10600d259f04SJohn Johansen }
10610d259f04SJohn Johansen 
106252b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v)
10630d259f04SJohn Johansen {
10648399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1065637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1066637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
10670d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
1068637f688dSJohn Johansen 	aa_put_label(label);
10690d259f04SJohn Johansen 
10700d259f04SJohn Johansen 	return 0;
10710d259f04SJohn Johansen }
10720d259f04SJohn Johansen 
107352b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v)
10740d259f04SJohn Johansen {
10758399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1076637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1077637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
10780d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
1079637f688dSJohn Johansen 	aa_put_label(label);
10800d259f04SJohn Johansen 
10810d259f04SJohn Johansen 	return 0;
10820d259f04SJohn Johansen }
10830d259f04SJohn Johansen 
108452b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v)
1085556d0be7SJohn Johansen {
10868399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1087637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1088637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
1089556d0be7SJohn Johansen 	if (profile->attach)
1090556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
1091556d0be7SJohn Johansen 	else if (profile->xmatch)
1092556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
1093556d0be7SJohn Johansen 	else
1094556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
1095637f688dSJohn Johansen 	aa_put_label(label);
1096556d0be7SJohn Johansen 
1097556d0be7SJohn Johansen 	return 0;
1098556d0be7SJohn Johansen }
1099556d0be7SJohn Johansen 
110052b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v)
1101f8eb8a13SJohn Johansen {
11028399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1103637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1104637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
1105f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
1106f8eb8a13SJohn Johansen 
1107f8eb8a13SJohn Johansen 	if (profile->hash) {
1108f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
1109f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
111047dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
1111f8eb8a13SJohn Johansen 	}
1112637f688dSJohn Johansen 	aa_put_label(label);
1113f8eb8a13SJohn Johansen 
1114f8eb8a13SJohn Johansen 	return 0;
1115f8eb8a13SJohn Johansen }
1116f8eb8a13SJohn Johansen 
111752b97de3SJohn Johansen SEQ_PROFILE_FOPS(name);
111852b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode);
111952b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach);
112052b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash);
1121f8eb8a13SJohn Johansen 
112264c86970SJohn Johansen /*
112364c86970SJohn Johansen  * namespace based files
112464c86970SJohn Johansen  *     several root files and
112564c86970SJohn Johansen  *     policy/ *
112664c86970SJohn Johansen  */
1127f8eb8a13SJohn Johansen 
112864c86970SJohn Johansen #define SEQ_NS_FOPS(NAME)						      \
112964c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file)     \
113064c86970SJohn Johansen {									      \
113164c86970SJohn Johansen 	return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private);   \
113264c86970SJohn Johansen }									      \
113364c86970SJohn Johansen 									      \
113464c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = {	      \
113564c86970SJohn Johansen 	.owner		= THIS_MODULE,					      \
113664c86970SJohn Johansen 	.open		= seq_ns_ ##NAME ##_open,			      \
113764c86970SJohn Johansen 	.read		= seq_read,					      \
113864c86970SJohn Johansen 	.llseek		= seq_lseek,					      \
113964c86970SJohn Johansen 	.release	= single_release,				      \
114064c86970SJohn Johansen }									      \
11413e3e5695SJohn Johansen 
114240cde7fcSJohn Johansen static int seq_ns_stacked_show(struct seq_file *seq, void *v)
114340cde7fcSJohn Johansen {
114440cde7fcSJohn Johansen 	struct aa_label *label;
114540cde7fcSJohn Johansen 
114640cde7fcSJohn Johansen 	label = begin_current_label_crit_section();
114740cde7fcSJohn Johansen 	seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no");
114840cde7fcSJohn Johansen 	end_current_label_crit_section(label);
114940cde7fcSJohn Johansen 
115040cde7fcSJohn Johansen 	return 0;
115140cde7fcSJohn Johansen }
115240cde7fcSJohn Johansen 
115340cde7fcSJohn Johansen static int seq_ns_nsstacked_show(struct seq_file *seq, void *v)
115440cde7fcSJohn Johansen {
115540cde7fcSJohn Johansen 	struct aa_label *label;
115640cde7fcSJohn Johansen 	struct aa_profile *profile;
115740cde7fcSJohn Johansen 	struct label_it it;
115840cde7fcSJohn Johansen 	int count = 1;
115940cde7fcSJohn Johansen 
116040cde7fcSJohn Johansen 	label = begin_current_label_crit_section();
116140cde7fcSJohn Johansen 
116240cde7fcSJohn Johansen 	if (label->size > 1) {
116340cde7fcSJohn Johansen 		label_for_each(it, label, profile)
116440cde7fcSJohn Johansen 			if (profile->ns != labels_ns(label)) {
116540cde7fcSJohn Johansen 				count++;
116640cde7fcSJohn Johansen 				break;
116740cde7fcSJohn Johansen 			}
116840cde7fcSJohn Johansen 	}
116940cde7fcSJohn Johansen 
117040cde7fcSJohn Johansen 	seq_printf(seq, "%s\n", count > 1 ? "yes" : "no");
117140cde7fcSJohn Johansen 	end_current_label_crit_section(label);
117240cde7fcSJohn Johansen 
117340cde7fcSJohn Johansen 	return 0;
117440cde7fcSJohn Johansen }
117540cde7fcSJohn Johansen 
117664c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v)
1177a71ada30SJohn Johansen {
1178637f688dSJohn Johansen 	struct aa_label *label;
1179a71ada30SJohn Johansen 
1180637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1181637f688dSJohn Johansen 	seq_printf(seq, "%d\n", labels_ns(label)->level);
1182637f688dSJohn Johansen 	end_current_label_crit_section(label);
1183a71ada30SJohn Johansen 
1184a71ada30SJohn Johansen 	return 0;
1185a71ada30SJohn Johansen }
1186a71ada30SJohn Johansen 
118764c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v)
11883e3e5695SJohn Johansen {
1189637f688dSJohn Johansen 	struct aa_label *label = begin_current_label_crit_section();
1190040d9e2bSJohn Johansen 	seq_printf(seq, "%s\n", labels_ns(label)->base.name);
1191637f688dSJohn Johansen 	end_current_label_crit_section(label);
11923e3e5695SJohn Johansen 
11933e3e5695SJohn Johansen 	return 0;
11943e3e5695SJohn Johansen }
11953e3e5695SJohn Johansen 
119640cde7fcSJohn Johansen SEQ_NS_FOPS(stacked);
119740cde7fcSJohn Johansen SEQ_NS_FOPS(nsstacked);
119864c86970SJohn Johansen SEQ_NS_FOPS(level);
119964c86970SJohn Johansen SEQ_NS_FOPS(name);
12003e3e5695SJohn Johansen 
12015d5182caSJohn Johansen 
12025d5182caSJohn Johansen /* policy/raw_data/ * file ops */
12035d5182caSJohn Johansen 
12045d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME)						      \
12055d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
12065d5182caSJohn Johansen {									      \
12075d5182caSJohn Johansen 	return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show);    \
12085d5182caSJohn Johansen }									      \
12095d5182caSJohn Johansen 									      \
12105d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = {	      \
12115d5182caSJohn Johansen 	.owner		= THIS_MODULE,					      \
12125d5182caSJohn Johansen 	.open		= seq_rawdata_ ##NAME ##_open,			      \
12135d5182caSJohn Johansen 	.read		= seq_read,					      \
12145d5182caSJohn Johansen 	.llseek		= seq_lseek,					      \
12155d5182caSJohn Johansen 	.release	= seq_rawdata_release,				      \
12165d5182caSJohn Johansen }									      \
12175d5182caSJohn Johansen 
12185d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file,
12195d5182caSJohn Johansen 			    int (*show)(struct seq_file *, void *))
12205ac8c355SJohn Johansen {
12215d5182caSJohn Johansen 	struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
12225d5182caSJohn Johansen 	int error;
12235d5182caSJohn Johansen 
12245d5182caSJohn Johansen 	if (!data)
12255d5182caSJohn Johansen 		/* lost race this ent is being reaped */
12265d5182caSJohn Johansen 		return -ENOENT;
12275d5182caSJohn Johansen 
12285d5182caSJohn Johansen 	error = single_open(file, show, data);
12295d5182caSJohn Johansen 	if (error) {
12305d5182caSJohn Johansen 		AA_BUG(file->private_data &&
12315d5182caSJohn Johansen 		       ((struct seq_file *)file->private_data)->private);
12325d5182caSJohn Johansen 		aa_put_loaddata(data);
12335d5182caSJohn Johansen 	}
12345d5182caSJohn Johansen 
12355d5182caSJohn Johansen 	return error;
12365d5182caSJohn Johansen }
12375d5182caSJohn Johansen 
12385d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file)
12395d5182caSJohn Johansen {
12405d5182caSJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
12415d5182caSJohn Johansen 
12425d5182caSJohn Johansen 	if (seq)
12435d5182caSJohn Johansen 		aa_put_loaddata(seq->private);
12445d5182caSJohn Johansen 
12455d5182caSJohn Johansen 	return single_release(inode, file);
12465d5182caSJohn Johansen }
12475d5182caSJohn Johansen 
12485d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
12495d5182caSJohn Johansen {
12505d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12515d5182caSJohn Johansen 
12525d5182caSJohn Johansen 	seq_printf(seq, "v%d\n", data->abi);
12535ac8c355SJohn Johansen 
12545ac8c355SJohn Johansen 	return 0;
12555ac8c355SJohn Johansen }
12565ac8c355SJohn Johansen 
12575d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
12585ac8c355SJohn Johansen {
12595d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12605ac8c355SJohn Johansen 
12615d5182caSJohn Johansen 	seq_printf(seq, "%ld\n", data->revision);
12625ac8c355SJohn Johansen 
12635ac8c355SJohn Johansen 	return 0;
12645ac8c355SJohn Johansen }
12655ac8c355SJohn Johansen 
12665d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
12675ac8c355SJohn Johansen {
12685d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12695ac8c355SJohn Johansen 	unsigned int i, size = aa_hash_size();
12705ac8c355SJohn Johansen 
12715d5182caSJohn Johansen 	if (data->hash) {
12725ac8c355SJohn Johansen 		for (i = 0; i < size; i++)
12735d5182caSJohn Johansen 			seq_printf(seq, "%.2x", data->hash[i]);
127447dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
12755ac8c355SJohn Johansen 	}
12765ac8c355SJohn Johansen 
12775ac8c355SJohn Johansen 	return 0;
12785ac8c355SJohn Johansen }
12795ac8c355SJohn Johansen 
128063c16c3aSChris Coulson static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v)
128163c16c3aSChris Coulson {
128263c16c3aSChris Coulson 	struct aa_loaddata *data = seq->private;
128363c16c3aSChris Coulson 
128463c16c3aSChris Coulson 	seq_printf(seq, "%zu\n", data->compressed_size);
128563c16c3aSChris Coulson 
128663c16c3aSChris Coulson 	return 0;
128763c16c3aSChris Coulson }
128863c16c3aSChris Coulson 
12895d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi);
12905d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision);
12915d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash);
129263c16c3aSChris Coulson SEQ_RAWDATA_FOPS(compressed_size);
129363c16c3aSChris Coulson 
129463c16c3aSChris Coulson static int deflate_decompress(char *src, size_t slen, char *dst, size_t dlen)
129563c16c3aSChris Coulson {
129663c16c3aSChris Coulson 	int error;
129763c16c3aSChris Coulson 	struct z_stream_s strm;
129863c16c3aSChris Coulson 
129963c16c3aSChris Coulson 	if (aa_g_rawdata_compression_level == 0) {
130063c16c3aSChris Coulson 		if (dlen < slen)
130163c16c3aSChris Coulson 			return -EINVAL;
130263c16c3aSChris Coulson 		memcpy(dst, src, slen);
130363c16c3aSChris Coulson 		return 0;
130463c16c3aSChris Coulson 	}
130563c16c3aSChris Coulson 
130663c16c3aSChris Coulson 	memset(&strm, 0, sizeof(strm));
130763c16c3aSChris Coulson 
130863c16c3aSChris Coulson 	strm.workspace = kvzalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
130963c16c3aSChris Coulson 	if (!strm.workspace)
131063c16c3aSChris Coulson 		return -ENOMEM;
131163c16c3aSChris Coulson 
131263c16c3aSChris Coulson 	strm.next_in = src;
131363c16c3aSChris Coulson 	strm.avail_in = slen;
131463c16c3aSChris Coulson 
131563c16c3aSChris Coulson 	error = zlib_inflateInit(&strm);
131663c16c3aSChris Coulson 	if (error != Z_OK) {
131763c16c3aSChris Coulson 		error = -ENOMEM;
131863c16c3aSChris Coulson 		goto fail_inflate_init;
131963c16c3aSChris Coulson 	}
132063c16c3aSChris Coulson 
132163c16c3aSChris Coulson 	strm.next_out = dst;
132263c16c3aSChris Coulson 	strm.avail_out = dlen;
132363c16c3aSChris Coulson 
132463c16c3aSChris Coulson 	error = zlib_inflate(&strm, Z_FINISH);
132563c16c3aSChris Coulson 	if (error != Z_STREAM_END)
132663c16c3aSChris Coulson 		error = -EINVAL;
132763c16c3aSChris Coulson 	else
132863c16c3aSChris Coulson 		error = 0;
132963c16c3aSChris Coulson 
133063c16c3aSChris Coulson 	zlib_inflateEnd(&strm);
133163c16c3aSChris Coulson fail_inflate_init:
133263c16c3aSChris Coulson 	kvfree(strm.workspace);
133363c16c3aSChris Coulson 	return error;
133463c16c3aSChris Coulson }
13355ac8c355SJohn Johansen 
13365ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
13375ac8c355SJohn Johansen 			    loff_t *ppos)
13385ac8c355SJohn Johansen {
133963c16c3aSChris Coulson 	struct rawdata_f_data *private = file->private_data;
13405ac8c355SJohn Johansen 
134163c16c3aSChris Coulson 	return simple_read_from_buffer(buf, size, ppos,
134263c16c3aSChris Coulson 				       RAWDATA_F_DATA_BUF(private),
134363c16c3aSChris Coulson 				       private->loaddata->size);
13445ac8c355SJohn Johansen }
13455ac8c355SJohn Johansen 
13465d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file)
13475ac8c355SJohn Johansen {
134863c16c3aSChris Coulson 	rawdata_f_data_free(file->private_data);
13495ac8c355SJohn Johansen 
13505ac8c355SJohn Johansen 	return 0;
13515ac8c355SJohn Johansen }
13525ac8c355SJohn Johansen 
13535d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file)
13545d5182caSJohn Johansen {
135563c16c3aSChris Coulson 	int error;
135663c16c3aSChris Coulson 	struct aa_loaddata *loaddata;
135763c16c3aSChris Coulson 	struct rawdata_f_data *private;
135863c16c3aSChris Coulson 
135992de220aSJohn Johansen 	if (!aa_current_policy_view_capable(NULL))
13605d5182caSJohn Johansen 		return -EACCES;
136163c16c3aSChris Coulson 
136263c16c3aSChris Coulson 	loaddata = __aa_get_loaddata(inode->i_private);
136363c16c3aSChris Coulson 	if (!loaddata)
13645d5182caSJohn Johansen 		/* lost race: this entry is being reaped */
13655d5182caSJohn Johansen 		return -ENOENT;
13665d5182caSJohn Johansen 
136763c16c3aSChris Coulson 	private = rawdata_f_data_alloc(loaddata->size);
136863c16c3aSChris Coulson 	if (IS_ERR(private)) {
136963c16c3aSChris Coulson 		error = PTR_ERR(private);
137063c16c3aSChris Coulson 		goto fail_private_alloc;
137163c16c3aSChris Coulson 	}
137263c16c3aSChris Coulson 
137363c16c3aSChris Coulson 	private->loaddata = loaddata;
137463c16c3aSChris Coulson 
137563c16c3aSChris Coulson 	error = deflate_decompress(loaddata->data, loaddata->compressed_size,
137663c16c3aSChris Coulson 				   RAWDATA_F_DATA_BUF(private),
137763c16c3aSChris Coulson 				   loaddata->size);
137863c16c3aSChris Coulson 	if (error)
137963c16c3aSChris Coulson 		goto fail_decompress;
138063c16c3aSChris Coulson 
138163c16c3aSChris Coulson 	file->private_data = private;
13825d5182caSJohn Johansen 	return 0;
138363c16c3aSChris Coulson 
138463c16c3aSChris Coulson fail_decompress:
138563c16c3aSChris Coulson 	rawdata_f_data_free(private);
138663c16c3aSChris Coulson 	return error;
138763c16c3aSChris Coulson 
138863c16c3aSChris Coulson fail_private_alloc:
138963c16c3aSChris Coulson 	aa_put_loaddata(loaddata);
139063c16c3aSChris Coulson 	return error;
13915d5182caSJohn Johansen }
13925d5182caSJohn Johansen 
13935d5182caSJohn Johansen static const struct file_operations rawdata_fops = {
13945ac8c355SJohn Johansen 	.open = rawdata_open,
13955ac8c355SJohn Johansen 	.read = rawdata_read,
13965ac8c355SJohn Johansen 	.llseek = generic_file_llseek,
13975ac8c355SJohn Johansen 	.release = rawdata_release,
13985ac8c355SJohn Johansen };
13995ac8c355SJohn Johansen 
14005d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata)
14015d5182caSJohn Johansen {
14025d5182caSJohn Johansen 	int i;
14035d5182caSJohn Johansen 
14045d5182caSJohn Johansen 	for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
14055d5182caSJohn Johansen 		if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
14065d5182caSJohn Johansen 			/* no refcounts on i_private */
1407c961ee5fSJohn Johansen 			aafs_remove(rawdata->dents[i]);
14085d5182caSJohn Johansen 			rawdata->dents[i] = NULL;
14095d5182caSJohn Johansen 		}
14105d5182caSJohn Johansen 	}
14115d5182caSJohn Johansen }
14125d5182caSJohn Johansen 
14135d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
14145d5182caSJohn Johansen {
14155d5182caSJohn Johansen 	AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
14165d5182caSJohn Johansen 
14175d5182caSJohn Johansen 	if (rawdata->ns) {
14185d5182caSJohn Johansen 		remove_rawdata_dents(rawdata);
14195d5182caSJohn Johansen 		list_del_init(&rawdata->list);
14205d5182caSJohn Johansen 		aa_put_ns(rawdata->ns);
14215d5182caSJohn Johansen 		rawdata->ns = NULL;
14225d5182caSJohn Johansen 	}
14235d5182caSJohn Johansen }
14245d5182caSJohn Johansen 
14255d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
14265d5182caSJohn Johansen {
14275d5182caSJohn Johansen 	struct dentry *dent, *dir;
14285d5182caSJohn Johansen 
14295d5182caSJohn Johansen 	AA_BUG(!ns);
14305d5182caSJohn Johansen 	AA_BUG(!rawdata);
14315d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
14325d5182caSJohn Johansen 	AA_BUG(!ns_subdata_dir(ns));
14335d5182caSJohn Johansen 
14345d5182caSJohn Johansen 	/*
14355d5182caSJohn Johansen 	 * just use ns revision dir was originally created at. This is
14365d5182caSJohn Johansen 	 * under ns->lock and if load is successful revision will be
14375d5182caSJohn Johansen 	 * bumped and is guaranteed to be unique
14385d5182caSJohn Johansen 	 */
14395d5182caSJohn Johansen 	rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
14405d5182caSJohn Johansen 	if (!rawdata->name)
14415d5182caSJohn Johansen 		return -ENOMEM;
14425d5182caSJohn Johansen 
1443c961ee5fSJohn Johansen 	dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
14445d5182caSJohn Johansen 	if (IS_ERR(dir))
14455d5182caSJohn Johansen 		/* ->name freed when rawdata freed */
14465d5182caSJohn Johansen 		return PTR_ERR(dir);
14475d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DIR] = dir;
14485d5182caSJohn Johansen 
1449c961ee5fSJohn Johansen 	dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
14505d5182caSJohn Johansen 				      &seq_rawdata_abi_fops);
14515d5182caSJohn Johansen 	if (IS_ERR(dent))
14525d5182caSJohn Johansen 		goto fail;
14535d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_ABI] = dent;
14545d5182caSJohn Johansen 
1455c961ee5fSJohn Johansen 	dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
14565d5182caSJohn Johansen 				      &seq_rawdata_revision_fops);
14575d5182caSJohn Johansen 	if (IS_ERR(dent))
14585d5182caSJohn Johansen 		goto fail;
14595d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
14605d5182caSJohn Johansen 
14615d5182caSJohn Johansen 	if (aa_g_hash_policy) {
1462c961ee5fSJohn Johansen 		dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
14635d5182caSJohn Johansen 					      rawdata, &seq_rawdata_hash_fops);
14645d5182caSJohn Johansen 		if (IS_ERR(dent))
14655d5182caSJohn Johansen 			goto fail;
14665d5182caSJohn Johansen 		rawdata->dents[AAFS_LOADDATA_HASH] = dent;
14675d5182caSJohn Johansen 	}
14685d5182caSJohn Johansen 
146963c16c3aSChris Coulson 	dent = aafs_create_file("compressed_size", S_IFREG | 0444, dir,
147063c16c3aSChris Coulson 				rawdata,
147163c16c3aSChris Coulson 				&seq_rawdata_compressed_size_fops);
147263c16c3aSChris Coulson 	if (IS_ERR(dent))
147363c16c3aSChris Coulson 		goto fail;
147463c16c3aSChris Coulson 	rawdata->dents[AAFS_LOADDATA_COMPRESSED_SIZE] = dent;
147563c16c3aSChris Coulson 
1476c961ee5fSJohn Johansen 	dent = aafs_create_file("raw_data", S_IFREG | 0444,
14775d5182caSJohn Johansen 				      dir, rawdata, &rawdata_fops);
14785d5182caSJohn Johansen 	if (IS_ERR(dent))
14795d5182caSJohn Johansen 		goto fail;
14805d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DATA] = dent;
14815d5182caSJohn Johansen 	d_inode(dent)->i_size = rawdata->size;
14825d5182caSJohn Johansen 
14835d5182caSJohn Johansen 	rawdata->ns = aa_get_ns(ns);
14845d5182caSJohn Johansen 	list_add(&rawdata->list, &ns->rawdata_list);
14855d5182caSJohn Johansen 	/* no refcount on inode rawdata */
14865d5182caSJohn Johansen 
14875d5182caSJohn Johansen 	return 0;
14885d5182caSJohn Johansen 
14895d5182caSJohn Johansen fail:
14905d5182caSJohn Johansen 	remove_rawdata_dents(rawdata);
14915d5182caSJohn Johansen 
14925d5182caSJohn Johansen 	return PTR_ERR(dent);
14935d5182caSJohn Johansen }
14945d5182caSJohn Johansen 
14950d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
1496c97204baSJohn Johansen 
1497c97204baSJohn Johansen /**
1498c97204baSJohn Johansen  *
1499c97204baSJohn Johansen  * Requires: @profile->ns->lock held
1500c97204baSJohn Johansen  */
1501c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile)
15020d259f04SJohn Johansen {
15030d259f04SJohn Johansen 	struct aa_profile *child;
15040d259f04SJohn Johansen 	int i;
15050d259f04SJohn Johansen 
15060d259f04SJohn Johansen 	if (!profile)
15070d259f04SJohn Johansen 		return;
15080d259f04SJohn Johansen 
15090d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
1510c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
15110d259f04SJohn Johansen 
15120d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
15138399588aSJohn Johansen 		struct aa_proxy *proxy;
15140d259f04SJohn Johansen 		if (!profile->dents[i])
15150d259f04SJohn Johansen 			continue;
15160d259f04SJohn Johansen 
15178399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
1518c961ee5fSJohn Johansen 		aafs_remove(profile->dents[i]);
15198399588aSJohn Johansen 		aa_put_proxy(proxy);
15200d259f04SJohn Johansen 		profile->dents[i] = NULL;
15210d259f04SJohn Johansen 	}
15220d259f04SJohn Johansen }
15230d259f04SJohn Johansen 
1524c97204baSJohn Johansen /**
1525c97204baSJohn Johansen  *
1526c97204baSJohn Johansen  * Requires: @old->ns->lock held
1527c97204baSJohn Johansen  */
1528c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old,
15290d259f04SJohn Johansen 				  struct aa_profile *new)
15300d259f04SJohn Johansen {
15310d259f04SJohn Johansen 	int i;
15320d259f04SJohn Johansen 
1533cbf2d0e1SJohn Johansen 	AA_BUG(!old);
1534cbf2d0e1SJohn Johansen 	AA_BUG(!new);
1535cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock));
1536cbf2d0e1SJohn Johansen 
15370d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
15380d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
1539d671e890SJohn Johansen 		if (new->dents[i])
1540078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
15410d259f04SJohn Johansen 		old->dents[i] = NULL;
15420d259f04SJohn Johansen 	}
15430d259f04SJohn Johansen }
15440d259f04SJohn Johansen 
15450d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
15460d259f04SJohn Johansen 					  struct aa_profile *profile,
15470d259f04SJohn Johansen 					  const struct file_operations *fops)
15480d259f04SJohn Johansen {
1549637f688dSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
15500d259f04SJohn Johansen 	struct dentry *dent;
15510d259f04SJohn Johansen 
1552c961ee5fSJohn Johansen 	dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
15530d259f04SJohn Johansen 	if (IS_ERR(dent))
15548399588aSJohn Johansen 		aa_put_proxy(proxy);
15550d259f04SJohn Johansen 
15560d259f04SJohn Johansen 	return dent;
15570d259f04SJohn Johansen }
15580d259f04SJohn Johansen 
15595d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile)
15605d5182caSJohn Johansen {
15615d5182caSJohn Johansen 	int depth = 0;
15625d5182caSJohn Johansen 
15635d5182caSJohn Johansen 	rcu_read_lock();
15645d5182caSJohn Johansen 	for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
15655d5182caSJohn Johansen 		depth++;
15665d5182caSJohn Johansen 	rcu_read_unlock();
15675d5182caSJohn Johansen 
15685d5182caSJohn Johansen 	return depth;
15695d5182caSJohn Johansen }
15705d5182caSJohn Johansen 
15711180b4c7SJohn Johansen static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
15725d5182caSJohn Johansen {
15731180b4c7SJohn Johansen 	char *buffer, *s;
15745d5182caSJohn Johansen 	int error;
15751180b4c7SJohn Johansen 	int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
15761180b4c7SJohn Johansen 
15771180b4c7SJohn Johansen 	s = buffer = kmalloc(size, GFP_KERNEL);
15781180b4c7SJohn Johansen 	if (!buffer)
15791180b4c7SJohn Johansen 		return ERR_PTR(-ENOMEM);
15805d5182caSJohn Johansen 
15815d5182caSJohn Johansen 	for (; depth > 0; depth--) {
15821180b4c7SJohn Johansen 		strcpy(s, "../../");
15831180b4c7SJohn Johansen 		s += 6;
15841180b4c7SJohn Johansen 		size -= 6;
15855d5182caSJohn Johansen 	}
15865d5182caSJohn Johansen 
15871180b4c7SJohn Johansen 	error = snprintf(s, size, "raw_data/%s/%s", dirname, fname);
1588588558ebSColin Ian King 	if (error >= size || error < 0) {
1589588558ebSColin Ian King 		kfree(buffer);
15901180b4c7SJohn Johansen 		return ERR_PTR(-ENAMETOOLONG);
15915d5182caSJohn Johansen 	}
15925d5182caSJohn Johansen 
15931180b4c7SJohn Johansen 	return buffer;
15945d5182caSJohn Johansen }
15955d5182caSJohn Johansen 
15961180b4c7SJohn Johansen static void rawdata_link_cb(void *arg)
15971180b4c7SJohn Johansen {
15981180b4c7SJohn Johansen 	kfree(arg);
15991180b4c7SJohn Johansen }
16001180b4c7SJohn Johansen 
16011180b4c7SJohn Johansen static const char *rawdata_get_link_base(struct dentry *dentry,
16021180b4c7SJohn Johansen 					 struct inode *inode,
16031180b4c7SJohn Johansen 					 struct delayed_call *done,
16041180b4c7SJohn Johansen 					 const char *name)
16051180b4c7SJohn Johansen {
16061180b4c7SJohn Johansen 	struct aa_proxy *proxy = inode->i_private;
16071180b4c7SJohn Johansen 	struct aa_label *label;
16081180b4c7SJohn Johansen 	struct aa_profile *profile;
16091180b4c7SJohn Johansen 	char *target;
16101180b4c7SJohn Johansen 	int depth;
16111180b4c7SJohn Johansen 
16121180b4c7SJohn Johansen 	if (!dentry)
16131180b4c7SJohn Johansen 		return ERR_PTR(-ECHILD);
16141180b4c7SJohn Johansen 
16151180b4c7SJohn Johansen 	label = aa_get_label_rcu(&proxy->label);
16161180b4c7SJohn Johansen 	profile = labels_profile(label);
16171180b4c7SJohn Johansen 	depth = profile_depth(profile);
16181180b4c7SJohn Johansen 	target = gen_symlink_name(depth, profile->rawdata->name, name);
16191180b4c7SJohn Johansen 	aa_put_label(label);
16201180b4c7SJohn Johansen 
16211180b4c7SJohn Johansen 	if (IS_ERR(target))
16221180b4c7SJohn Johansen 		return target;
16231180b4c7SJohn Johansen 
16241180b4c7SJohn Johansen 	set_delayed_call(done, rawdata_link_cb, target);
16251180b4c7SJohn Johansen 
16261180b4c7SJohn Johansen 	return target;
16271180b4c7SJohn Johansen }
16281180b4c7SJohn Johansen 
16291180b4c7SJohn Johansen static const char *rawdata_get_link_sha1(struct dentry *dentry,
16301180b4c7SJohn Johansen 					 struct inode *inode,
16311180b4c7SJohn Johansen 					 struct delayed_call *done)
16321180b4c7SJohn Johansen {
16331180b4c7SJohn Johansen 	return rawdata_get_link_base(dentry, inode, done, "sha1");
16341180b4c7SJohn Johansen }
16351180b4c7SJohn Johansen 
16361180b4c7SJohn Johansen static const char *rawdata_get_link_abi(struct dentry *dentry,
16371180b4c7SJohn Johansen 					struct inode *inode,
16381180b4c7SJohn Johansen 					struct delayed_call *done)
16391180b4c7SJohn Johansen {
16401180b4c7SJohn Johansen 	return rawdata_get_link_base(dentry, inode, done, "abi");
16411180b4c7SJohn Johansen }
16421180b4c7SJohn Johansen 
16431180b4c7SJohn Johansen static const char *rawdata_get_link_data(struct dentry *dentry,
16441180b4c7SJohn Johansen 					 struct inode *inode,
16451180b4c7SJohn Johansen 					 struct delayed_call *done)
16461180b4c7SJohn Johansen {
16471180b4c7SJohn Johansen 	return rawdata_get_link_base(dentry, inode, done, "raw_data");
16481180b4c7SJohn Johansen }
16491180b4c7SJohn Johansen 
16501180b4c7SJohn Johansen static const struct inode_operations rawdata_link_sha1_iops = {
16511180b4c7SJohn Johansen 	.get_link	= rawdata_get_link_sha1,
16521180b4c7SJohn Johansen };
16531180b4c7SJohn Johansen 
16541180b4c7SJohn Johansen static const struct inode_operations rawdata_link_abi_iops = {
16551180b4c7SJohn Johansen 	.get_link	= rawdata_get_link_abi,
16561180b4c7SJohn Johansen };
16571180b4c7SJohn Johansen static const struct inode_operations rawdata_link_data_iops = {
16581180b4c7SJohn Johansen 	.get_link	= rawdata_get_link_data,
16591180b4c7SJohn Johansen };
16601180b4c7SJohn Johansen 
16611180b4c7SJohn Johansen 
16625d5182caSJohn Johansen /*
16635d5182caSJohn Johansen  * Requires: @profile->ns->lock held
16645d5182caSJohn Johansen  */
1665c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
16660d259f04SJohn Johansen {
16670d259f04SJohn Johansen 	struct aa_profile *child;
16680d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
16690d259f04SJohn Johansen 	int error;
16700d259f04SJohn Johansen 
1671cbf2d0e1SJohn Johansen 	AA_BUG(!profile);
1672cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock));
1673cbf2d0e1SJohn Johansen 
16740d259f04SJohn Johansen 	if (!parent) {
16750d259f04SJohn Johansen 		struct aa_profile *p;
16760d259f04SJohn Johansen 		p = aa_deref_parent(profile);
16770d259f04SJohn Johansen 		dent = prof_dir(p);
16780d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
1679c961ee5fSJohn Johansen 		dent = aafs_create_dir("profiles", dent);
16800d259f04SJohn Johansen 		if (IS_ERR(dent))
16810d259f04SJohn Johansen 			goto fail;
16820d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
16830d259f04SJohn Johansen 	}
16840d259f04SJohn Johansen 
16850d259f04SJohn Johansen 	if (!profile->dirname) {
16860d259f04SJohn Johansen 		int len, id_len;
16870d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
16880d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
16890d259f04SJohn Johansen 
16900d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
1691ffac1de6SDan Carpenter 		if (!profile->dirname) {
1692ffac1de6SDan Carpenter 			error = -ENOMEM;
1693ffac1de6SDan Carpenter 			goto fail2;
1694ffac1de6SDan Carpenter 		}
16950d259f04SJohn Johansen 
16960d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
16970d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
16980d259f04SJohn Johansen 	}
16990d259f04SJohn Johansen 
1700c961ee5fSJohn Johansen 	dent = aafs_create_dir(profile->dirname, parent);
17010d259f04SJohn Johansen 	if (IS_ERR(dent))
17020d259f04SJohn Johansen 		goto fail;
17030d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
17040d259f04SJohn Johansen 
170552b97de3SJohn Johansen 	dent = create_profile_file(dir, "name", profile,
170652b97de3SJohn Johansen 				   &seq_profile_name_fops);
17070d259f04SJohn Johansen 	if (IS_ERR(dent))
17080d259f04SJohn Johansen 		goto fail;
17090d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
17100d259f04SJohn Johansen 
171152b97de3SJohn Johansen 	dent = create_profile_file(dir, "mode", profile,
171252b97de3SJohn Johansen 				   &seq_profile_mode_fops);
17130d259f04SJohn Johansen 	if (IS_ERR(dent))
17140d259f04SJohn Johansen 		goto fail;
17150d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
17160d259f04SJohn Johansen 
1717556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
171852b97de3SJohn Johansen 				   &seq_profile_attach_fops);
1719556d0be7SJohn Johansen 	if (IS_ERR(dent))
1720556d0be7SJohn Johansen 		goto fail;
1721556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
1722556d0be7SJohn Johansen 
1723f8eb8a13SJohn Johansen 	if (profile->hash) {
1724f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
172552b97de3SJohn Johansen 					   &seq_profile_hash_fops);
1726f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
1727f8eb8a13SJohn Johansen 			goto fail;
1728f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
1729f8eb8a13SJohn Johansen 	}
1730f8eb8a13SJohn Johansen 
17315ac8c355SJohn Johansen 	if (profile->rawdata) {
1732a68d59ffSJohn Johansen 		dent = aafs_create("raw_sha1", S_IFLNK | 0444, dir,
1733a68d59ffSJohn Johansen 				   profile->label.proxy, NULL, NULL,
17341180b4c7SJohn Johansen 				   &rawdata_link_sha1_iops);
17355ac8c355SJohn Johansen 		if (IS_ERR(dent))
17365ac8c355SJohn Johansen 			goto fail;
17371180b4c7SJohn Johansen 		aa_get_proxy(profile->label.proxy);
17385ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
17395ac8c355SJohn Johansen 
1740a68d59ffSJohn Johansen 		dent = aafs_create("raw_abi", S_IFLNK | 0444, dir,
1741a68d59ffSJohn Johansen 				   profile->label.proxy, NULL, NULL,
17421180b4c7SJohn Johansen 				   &rawdata_link_abi_iops);
17435ac8c355SJohn Johansen 		if (IS_ERR(dent))
17445ac8c355SJohn Johansen 			goto fail;
17451180b4c7SJohn Johansen 		aa_get_proxy(profile->label.proxy);
17465ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
17475ac8c355SJohn Johansen 
1748a68d59ffSJohn Johansen 		dent = aafs_create("raw_data", S_IFLNK | 0444, dir,
1749a68d59ffSJohn Johansen 				   profile->label.proxy, NULL, NULL,
17501180b4c7SJohn Johansen 				   &rawdata_link_data_iops);
17515ac8c355SJohn Johansen 		if (IS_ERR(dent))
17525ac8c355SJohn Johansen 			goto fail;
17531180b4c7SJohn Johansen 		aa_get_proxy(profile->label.proxy);
17545ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
17555ac8c355SJohn Johansen 	}
17565ac8c355SJohn Johansen 
17570d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
1758c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, prof_child_dir(profile));
17590d259f04SJohn Johansen 		if (error)
17600d259f04SJohn Johansen 			goto fail2;
17610d259f04SJohn Johansen 	}
17620d259f04SJohn Johansen 
17630d259f04SJohn Johansen 	return 0;
17640d259f04SJohn Johansen 
17650d259f04SJohn Johansen fail:
17660d259f04SJohn Johansen 	error = PTR_ERR(dent);
17670d259f04SJohn Johansen 
17680d259f04SJohn Johansen fail2:
1769c97204baSJohn Johansen 	__aafs_profile_rmdir(profile);
17700d259f04SJohn Johansen 
17710d259f04SJohn Johansen 	return error;
17720d259f04SJohn Johansen }
17730d259f04SJohn Johansen 
17744ae47f33SJohn Johansen static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode)
17754ae47f33SJohn Johansen {
17764ae47f33SJohn Johansen 	struct aa_ns *ns, *parent;
17774ae47f33SJohn Johansen 	/* TODO: improve permission check */
1778637f688dSJohn Johansen 	struct aa_label *label;
1779637f688dSJohn Johansen 	int error;
1780637f688dSJohn Johansen 
1781637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1782637f688dSJohn Johansen 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1783637f688dSJohn Johansen 	end_current_label_crit_section(label);
17844ae47f33SJohn Johansen 	if (error)
17854ae47f33SJohn Johansen 		return error;
17864ae47f33SJohn Johansen 
17874ae47f33SJohn Johansen 	parent = aa_get_ns(dir->i_private);
17884ae47f33SJohn Johansen 	AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
17894ae47f33SJohn Johansen 
17904ae47f33SJohn Johansen 	/* we have to unlock and then relock to get locking order right
17914ae47f33SJohn Johansen 	 * for pin_fs
17924ae47f33SJohn Johansen 	 */
17934ae47f33SJohn Johansen 	inode_unlock(dir);
17944ae47f33SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
1795feb3c766SJohn Johansen 	mutex_lock_nested(&parent->lock, parent->level);
17964ae47f33SJohn Johansen 	inode_lock_nested(dir, I_MUTEX_PARENT);
17974ae47f33SJohn Johansen 	if (error)
17984ae47f33SJohn Johansen 		goto out;
17994ae47f33SJohn Johansen 
18004ae47f33SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR,  NULL,
18014ae47f33SJohn Johansen 				     NULL, NULL, NULL);
18024ae47f33SJohn Johansen 	if (error)
18034ae47f33SJohn Johansen 		goto out_pin;
18044ae47f33SJohn Johansen 
18054ae47f33SJohn Johansen 	ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name),
18064ae47f33SJohn Johansen 				    dentry);
18074ae47f33SJohn Johansen 	if (IS_ERR(ns)) {
18084ae47f33SJohn Johansen 		error = PTR_ERR(ns);
18094ae47f33SJohn Johansen 		ns = NULL;
18104ae47f33SJohn Johansen 	}
18114ae47f33SJohn Johansen 
18124ae47f33SJohn Johansen 	aa_put_ns(ns);		/* list ref remains */
18134ae47f33SJohn Johansen out_pin:
18144ae47f33SJohn Johansen 	if (error)
18154ae47f33SJohn Johansen 		simple_release_fs(&aafs_mnt, &aafs_count);
18164ae47f33SJohn Johansen out:
18174ae47f33SJohn Johansen 	mutex_unlock(&parent->lock);
18184ae47f33SJohn Johansen 	aa_put_ns(parent);
18194ae47f33SJohn Johansen 
18204ae47f33SJohn Johansen 	return error;
18214ae47f33SJohn Johansen }
18224ae47f33SJohn Johansen 
18234ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
18244ae47f33SJohn Johansen {
18254ae47f33SJohn Johansen 	struct aa_ns *ns, *parent;
18264ae47f33SJohn Johansen 	/* TODO: improve permission check */
1827637f688dSJohn Johansen 	struct aa_label *label;
1828637f688dSJohn Johansen 	int error;
1829637f688dSJohn Johansen 
1830637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1831637f688dSJohn Johansen 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1832637f688dSJohn Johansen 	end_current_label_crit_section(label);
18334ae47f33SJohn Johansen 	if (error)
18344ae47f33SJohn Johansen 		return error;
18354ae47f33SJohn Johansen 
18364ae47f33SJohn Johansen 	parent = aa_get_ns(dir->i_private);
18374ae47f33SJohn Johansen 	/* rmdir calls the generic securityfs functions to remove files
18384ae47f33SJohn Johansen 	 * from the apparmor dir. It is up to the apparmor ns locking
18394ae47f33SJohn Johansen 	 * to avoid races.
18404ae47f33SJohn Johansen 	 */
18414ae47f33SJohn Johansen 	inode_unlock(dir);
18424ae47f33SJohn Johansen 	inode_unlock(dentry->d_inode);
18434ae47f33SJohn Johansen 
1844feb3c766SJohn Johansen 	mutex_lock_nested(&parent->lock, parent->level);
18454ae47f33SJohn Johansen 	ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name,
18464ae47f33SJohn Johansen 				     dentry->d_name.len));
18474ae47f33SJohn Johansen 	if (!ns) {
18484ae47f33SJohn Johansen 		error = -ENOENT;
18494ae47f33SJohn Johansen 		goto out;
18504ae47f33SJohn Johansen 	}
18514ae47f33SJohn Johansen 	AA_BUG(ns_dir(ns) != dentry);
18524ae47f33SJohn Johansen 
18534ae47f33SJohn Johansen 	__aa_remove_ns(ns);
18544ae47f33SJohn Johansen 	aa_put_ns(ns);
18554ae47f33SJohn Johansen 
18564ae47f33SJohn Johansen out:
18574ae47f33SJohn Johansen 	mutex_unlock(&parent->lock);
18584ae47f33SJohn Johansen 	inode_lock_nested(dir, I_MUTEX_PARENT);
18594ae47f33SJohn Johansen 	inode_lock(dentry->d_inode);
18604ae47f33SJohn Johansen 	aa_put_ns(parent);
18614ae47f33SJohn Johansen 
18624ae47f33SJohn Johansen 	return error;
18634ae47f33SJohn Johansen }
18644ae47f33SJohn Johansen 
18654ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = {
18664ae47f33SJohn Johansen 	.lookup		= simple_lookup,
18674ae47f33SJohn Johansen 	.mkdir		= ns_mkdir_op,
18684ae47f33SJohn Johansen 	.rmdir		= ns_rmdir_op,
18694ae47f33SJohn Johansen };
18704ae47f33SJohn Johansen 
18715d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
18725d5182caSJohn Johansen {
18735d5182caSJohn Johansen 	struct aa_loaddata *ent, *tmp;
18745d5182caSJohn Johansen 
18755d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
18765d5182caSJohn Johansen 
18775d5182caSJohn Johansen 	list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
18785d5182caSJohn Johansen 		__aa_fs_remove_rawdata(ent);
18795d5182caSJohn Johansen }
18805d5182caSJohn Johansen 
1881c97204baSJohn Johansen /**
1882c97204baSJohn Johansen  *
1883c97204baSJohn Johansen  * Requires: @ns->lock held
1884c97204baSJohn Johansen  */
1885c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns)
18860d259f04SJohn Johansen {
188798849dffSJohn Johansen 	struct aa_ns *sub;
18880d259f04SJohn Johansen 	struct aa_profile *child;
18890d259f04SJohn Johansen 	int i;
18900d259f04SJohn Johansen 
18910d259f04SJohn Johansen 	if (!ns)
18920d259f04SJohn Johansen 		return;
1893cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
18940d259f04SJohn Johansen 
18950d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
1896c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
18970d259f04SJohn Johansen 
18980d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
1899feb3c766SJohn Johansen 		mutex_lock_nested(&sub->lock, sub->level);
1900c97204baSJohn Johansen 		__aafs_ns_rmdir(sub);
19010d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
19020d259f04SJohn Johansen 	}
19030d259f04SJohn Johansen 
19045d5182caSJohn Johansen 	__aa_fs_list_remove_rawdata(ns);
19055d5182caSJohn Johansen 
1906b7fd2c03SJohn Johansen 	if (ns_subns_dir(ns)) {
1907b7fd2c03SJohn Johansen 		sub = d_inode(ns_subns_dir(ns))->i_private;
1908b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1909b7fd2c03SJohn Johansen 	}
1910b7fd2c03SJohn Johansen 	if (ns_subload(ns)) {
1911b7fd2c03SJohn Johansen 		sub = d_inode(ns_subload(ns))->i_private;
1912b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1913b7fd2c03SJohn Johansen 	}
1914b7fd2c03SJohn Johansen 	if (ns_subreplace(ns)) {
1915b7fd2c03SJohn Johansen 		sub = d_inode(ns_subreplace(ns))->i_private;
1916b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1917b7fd2c03SJohn Johansen 	}
1918b7fd2c03SJohn Johansen 	if (ns_subremove(ns)) {
1919b7fd2c03SJohn Johansen 		sub = d_inode(ns_subremove(ns))->i_private;
1920b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1921b7fd2c03SJohn Johansen 	}
1922d9bf2c26SJohn Johansen 	if (ns_subrevision(ns)) {
1923d9bf2c26SJohn Johansen 		sub = d_inode(ns_subrevision(ns))->i_private;
1924d9bf2c26SJohn Johansen 		aa_put_ns(sub);
1925d9bf2c26SJohn Johansen 	}
1926b7fd2c03SJohn Johansen 
19270d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
1928c961ee5fSJohn Johansen 		aafs_remove(ns->dents[i]);
19290d259f04SJohn Johansen 		ns->dents[i] = NULL;
19300d259f04SJohn Johansen 	}
19310d259f04SJohn Johansen }
19320d259f04SJohn Johansen 
1933b7fd2c03SJohn Johansen /* assumes cleanup in caller */
1934c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
1935b7fd2c03SJohn Johansen {
1936b7fd2c03SJohn Johansen 	struct dentry *dent;
1937b7fd2c03SJohn Johansen 
1938b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1939b7fd2c03SJohn Johansen 	AA_BUG(!dir);
1940b7fd2c03SJohn Johansen 
1941c961ee5fSJohn Johansen 	dent = aafs_create_dir("profiles", dir);
1942b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1943b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1944b7fd2c03SJohn Johansen 	ns_subprofs_dir(ns) = dent;
1945b7fd2c03SJohn Johansen 
1946c961ee5fSJohn Johansen 	dent = aafs_create_dir("raw_data", dir);
1947b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1948b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1949b7fd2c03SJohn Johansen 	ns_subdata_dir(ns) = dent;
1950b7fd2c03SJohn Johansen 
1951d9bf2c26SJohn Johansen 	dent = aafs_create_file("revision", 0444, dir, ns,
1952d9bf2c26SJohn Johansen 				&aa_fs_ns_revision_fops);
1953d9bf2c26SJohn Johansen 	if (IS_ERR(dent))
1954d9bf2c26SJohn Johansen 		return PTR_ERR(dent);
1955d9bf2c26SJohn Johansen 	aa_get_ns(ns);
1956d9bf2c26SJohn Johansen 	ns_subrevision(ns) = dent;
1957d9bf2c26SJohn Johansen 
1958c961ee5fSJohn Johansen 	dent = aafs_create_file(".load", 0640, dir, ns,
1959b7fd2c03SJohn Johansen 				      &aa_fs_profile_load);
1960b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1961b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1962b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1963b7fd2c03SJohn Johansen 	ns_subload(ns) = dent;
1964b7fd2c03SJohn Johansen 
1965c961ee5fSJohn Johansen 	dent = aafs_create_file(".replace", 0640, dir, ns,
1966b7fd2c03SJohn Johansen 				      &aa_fs_profile_replace);
1967b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1968b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1969b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1970b7fd2c03SJohn Johansen 	ns_subreplace(ns) = dent;
1971b7fd2c03SJohn Johansen 
1972c961ee5fSJohn Johansen 	dent = aafs_create_file(".remove", 0640, dir, ns,
1973b7fd2c03SJohn Johansen 				      &aa_fs_profile_remove);
1974b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1975b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1976b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1977b7fd2c03SJohn Johansen 	ns_subremove(ns) = dent;
1978b7fd2c03SJohn Johansen 
19794ae47f33SJohn Johansen 	  /* use create_dentry so we can supply private data */
19804ae47f33SJohn Johansen 	dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL,
19814ae47f33SJohn Johansen 			   &ns_dir_inode_operations);
1982b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1983b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1984b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1985b7fd2c03SJohn Johansen 	ns_subns_dir(ns) = dent;
1986b7fd2c03SJohn Johansen 
1987b7fd2c03SJohn Johansen 	return 0;
1988b7fd2c03SJohn Johansen }
1989b7fd2c03SJohn Johansen 
1990c97204baSJohn Johansen /*
1991c97204baSJohn Johansen  * Requires: @ns->lock held
1992c97204baSJohn Johansen  */
199398407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
199498407f0aSJohn Johansen 		    struct dentry *dent)
19950d259f04SJohn Johansen {
199698849dffSJohn Johansen 	struct aa_ns *sub;
19970d259f04SJohn Johansen 	struct aa_profile *child;
199898407f0aSJohn Johansen 	struct dentry *dir;
19990d259f04SJohn Johansen 	int error;
20000d259f04SJohn Johansen 
2001b7fd2c03SJohn Johansen 	AA_BUG(!ns);
2002b7fd2c03SJohn Johansen 	AA_BUG(!parent);
2003b7fd2c03SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
2004b7fd2c03SJohn Johansen 
20050d259f04SJohn Johansen 	if (!name)
20060d259f04SJohn Johansen 		name = ns->base.name;
20070d259f04SJohn Johansen 
2008c961ee5fSJohn Johansen 	if (!dent) {
2009b7fd2c03SJohn Johansen 		/* create ns dir if it doesn't already exist */
2010c961ee5fSJohn Johansen 		dent = aafs_create_dir(name, parent);
20110d259f04SJohn Johansen 		if (IS_ERR(dent))
20120d259f04SJohn Johansen 			goto fail;
2013c961ee5fSJohn Johansen 	} else
2014c961ee5fSJohn Johansen 		dget(dent);
20150d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
2016c97204baSJohn Johansen 	error = __aafs_ns_mkdir_entries(ns, dir);
2017b7fd2c03SJohn Johansen 	if (error)
2018b7fd2c03SJohn Johansen 		goto fail2;
20190d259f04SJohn Johansen 
2020b7fd2c03SJohn Johansen 	/* profiles */
20210d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
2022c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
20230d259f04SJohn Johansen 		if (error)
20240d259f04SJohn Johansen 			goto fail2;
20250d259f04SJohn Johansen 	}
20260d259f04SJohn Johansen 
2027b7fd2c03SJohn Johansen 	/* subnamespaces */
20280d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
2029feb3c766SJohn Johansen 		mutex_lock_nested(&sub->lock, sub->level);
203098407f0aSJohn Johansen 		error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
20310d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
20320d259f04SJohn Johansen 		if (error)
20330d259f04SJohn Johansen 			goto fail2;
20340d259f04SJohn Johansen 	}
20350d259f04SJohn Johansen 
20360d259f04SJohn Johansen 	return 0;
20370d259f04SJohn Johansen 
20380d259f04SJohn Johansen fail:
20390d259f04SJohn Johansen 	error = PTR_ERR(dent);
20400d259f04SJohn Johansen 
20410d259f04SJohn Johansen fail2:
2042c97204baSJohn Johansen 	__aafs_ns_rmdir(ns);
20430d259f04SJohn Johansen 
20440d259f04SJohn Johansen 	return error;
20450d259f04SJohn Johansen }
20460d259f04SJohn Johansen 
20470d259f04SJohn Johansen 
204829b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
204929b3822fSJohn Johansen 
205029b3822fSJohn Johansen /**
205198849dffSJohn Johansen  * __next_ns - find the next namespace to list
205229b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
205329b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
205429b3822fSJohn Johansen  *
205529b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
205629b3822fSJohn Johansen  * while switching current namespace.
205729b3822fSJohn Johansen  *
205829b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
205929b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
206029b3822fSJohn Johansen  * NOTE: will not unlock root->lock
206129b3822fSJohn Johansen  */
206298849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
206329b3822fSJohn Johansen {
206498849dffSJohn Johansen 	struct aa_ns *parent, *next;
206529b3822fSJohn Johansen 
2066cbf2d0e1SJohn Johansen 	AA_BUG(!root);
2067cbf2d0e1SJohn Johansen 	AA_BUG(!ns);
2068cbf2d0e1SJohn Johansen 	AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock));
2069cbf2d0e1SJohn Johansen 
207029b3822fSJohn Johansen 	/* is next namespace a child */
207129b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
207229b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
2073feb3c766SJohn Johansen 		mutex_lock_nested(&next->lock, next->level);
207429b3822fSJohn Johansen 		return next;
207529b3822fSJohn Johansen 	}
207629b3822fSJohn Johansen 
207729b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
207829b3822fSJohn Johansen 	parent = ns->parent;
2079ed2c7da3SJohn Johansen 	while (ns != root) {
208029b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
208138dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
208229b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
2083feb3c766SJohn Johansen 			mutex_lock_nested(&next->lock, next->level);
208429b3822fSJohn Johansen 			return next;
208529b3822fSJohn Johansen 		}
208629b3822fSJohn Johansen 		ns = parent;
208729b3822fSJohn Johansen 		parent = parent->parent;
208829b3822fSJohn Johansen 	}
208929b3822fSJohn Johansen 
209029b3822fSJohn Johansen 	return NULL;
209129b3822fSJohn Johansen }
209229b3822fSJohn Johansen 
209329b3822fSJohn Johansen /**
209429b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
209529b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
209629b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
209729b3822fSJohn Johansen  *
209829b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
209929b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
210029b3822fSJohn Johansen  */
210198849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
210298849dffSJohn Johansen 					  struct aa_ns *ns)
210329b3822fSJohn Johansen {
2104cbf2d0e1SJohn Johansen 	AA_BUG(!root);
2105cbf2d0e1SJohn Johansen 	AA_BUG(ns && !mutex_is_locked(&ns->lock));
2106cbf2d0e1SJohn Johansen 
210798849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
210829b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
210929b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
211029b3822fSJohn Johansen 						struct aa_profile, base.list);
211129b3822fSJohn Johansen 	}
211229b3822fSJohn Johansen 	return NULL;
211329b3822fSJohn Johansen }
211429b3822fSJohn Johansen 
211529b3822fSJohn Johansen /**
211629b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
2117aa4ceed7SChenXiaoSong  * @p: current profile in tree (NOT NULL)
211829b3822fSJohn Johansen  *
211929b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
212029b3822fSJohn Johansen  *
212129b3822fSJohn Johansen  * Returns: next profile or NULL if done
212229b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
212329b3822fSJohn Johansen  */
212429b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
212529b3822fSJohn Johansen {
212629b3822fSJohn Johansen 	struct aa_profile *parent;
212798849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
212829b3822fSJohn Johansen 
2129cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock));
2130cbf2d0e1SJohn Johansen 
213129b3822fSJohn Johansen 	/* is next profile a child */
213229b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
213329b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
213429b3822fSJohn Johansen 					base.list);
213529b3822fSJohn Johansen 
213629b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
213729b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
213829b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
213929b3822fSJohn Johansen 	while (parent) {
214038dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
214129b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
214229b3822fSJohn Johansen 			return p;
214329b3822fSJohn Johansen 		p = parent;
214429b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
214529b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
214629b3822fSJohn Johansen 	}
214729b3822fSJohn Johansen 
214829b3822fSJohn Johansen 	/* is next another profile in the namespace */
214938dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
215029b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
215129b3822fSJohn Johansen 		return p;
215229b3822fSJohn Johansen 
215329b3822fSJohn Johansen 	return NULL;
215429b3822fSJohn Johansen }
215529b3822fSJohn Johansen 
215629b3822fSJohn Johansen /**
215729b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
215829b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
215929b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
216029b3822fSJohn Johansen  *
216129b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
216229b3822fSJohn Johansen  */
216398849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
216429b3822fSJohn Johansen 				       struct aa_profile *profile)
216529b3822fSJohn Johansen {
216629b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
216729b3822fSJohn Johansen 	if (next)
216829b3822fSJohn Johansen 		return next;
216929b3822fSJohn Johansen 
217029b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
217198849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
217229b3822fSJohn Johansen }
217329b3822fSJohn Johansen 
217429b3822fSJohn Johansen /**
217529b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
217629b3822fSJohn Johansen  * @f: seq_file to fill
217729b3822fSJohn Johansen  * @pos: current position
217829b3822fSJohn Johansen  *
217929b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
218029b3822fSJohn Johansen  *
218129b3822fSJohn Johansen  * acquires first ns->lock
218229b3822fSJohn Johansen  */
218329b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
218429b3822fSJohn Johansen {
218529b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
2186cf797c0eSJohn Johansen 	struct aa_ns *root = aa_get_current_ns();
218729b3822fSJohn Johansen 	loff_t l = *pos;
2188cf797c0eSJohn Johansen 	f->private = root;
218929b3822fSJohn Johansen 
219029b3822fSJohn Johansen 	/* find the first profile */
2191feb3c766SJohn Johansen 	mutex_lock_nested(&root->lock, root->level);
219229b3822fSJohn Johansen 	profile = __first_profile(root, root);
219329b3822fSJohn Johansen 
219429b3822fSJohn Johansen 	/* skip to position */
219529b3822fSJohn Johansen 	for (; profile && l > 0; l--)
219629b3822fSJohn Johansen 		profile = next_profile(root, profile);
219729b3822fSJohn Johansen 
219829b3822fSJohn Johansen 	return profile;
219929b3822fSJohn Johansen }
220029b3822fSJohn Johansen 
220129b3822fSJohn Johansen /**
220229b3822fSJohn Johansen  * p_next - read the next profile entry
220329b3822fSJohn Johansen  * @f: seq_file to fill
220429b3822fSJohn Johansen  * @p: profile previously returned
220529b3822fSJohn Johansen  * @pos: current position
220629b3822fSJohn Johansen  *
220729b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
220829b3822fSJohn Johansen  *
220929b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
221029b3822fSJohn Johansen  */
221129b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
221229b3822fSJohn Johansen {
221329b3822fSJohn Johansen 	struct aa_profile *profile = p;
221498849dffSJohn Johansen 	struct aa_ns *ns = f->private;
221529b3822fSJohn Johansen 	(*pos)++;
221629b3822fSJohn Johansen 
221729b3822fSJohn Johansen 	return next_profile(ns, profile);
221829b3822fSJohn Johansen }
221929b3822fSJohn Johansen 
222029b3822fSJohn Johansen /**
222129b3822fSJohn Johansen  * p_stop - stop depth first traversal
222229b3822fSJohn Johansen  * @f: seq_file we are filling
222329b3822fSJohn Johansen  * @p: the last profile writen
222429b3822fSJohn Johansen  *
222529b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
222629b3822fSJohn Johansen  */
222729b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
222829b3822fSJohn Johansen {
222929b3822fSJohn Johansen 	struct aa_profile *profile = p;
223098849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
223129b3822fSJohn Johansen 
223229b3822fSJohn Johansen 	if (profile) {
223329b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
223429b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
223529b3822fSJohn Johansen 	}
223629b3822fSJohn Johansen 	mutex_unlock(&root->lock);
223798849dffSJohn Johansen 	aa_put_ns(root);
223829b3822fSJohn Johansen }
223929b3822fSJohn Johansen 
224029b3822fSJohn Johansen /**
224129b3822fSJohn Johansen  * seq_show_profile - show a profile entry
224229b3822fSJohn Johansen  * @f: seq_file to file
224329b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
224429b3822fSJohn Johansen  *
224529b3822fSJohn Johansen  * Returns: error on failure
224629b3822fSJohn Johansen  */
224729b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
224829b3822fSJohn Johansen {
224929b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
225098849dffSJohn Johansen 	struct aa_ns *root = f->private;
225129b3822fSJohn Johansen 
2252637f688dSJohn Johansen 	aa_label_seq_xprint(f, root, &profile->label,
2253637f688dSJohn Johansen 			    FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
2254637f688dSJohn Johansen 	seq_putc(f, '\n');
225529b3822fSJohn Johansen 
225629b3822fSJohn Johansen 	return 0;
225729b3822fSJohn Johansen }
225829b3822fSJohn Johansen 
2259c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = {
226029b3822fSJohn Johansen 	.start = p_start,
226129b3822fSJohn Johansen 	.next = p_next,
226229b3822fSJohn Johansen 	.stop = p_stop,
226329b3822fSJohn Johansen 	.show = seq_show_profile,
226429b3822fSJohn Johansen };
226529b3822fSJohn Johansen 
226629b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
226729b3822fSJohn Johansen {
226892de220aSJohn Johansen 	if (!aa_current_policy_view_capable(NULL))
22695ac8c355SJohn Johansen 		return -EACCES;
22705ac8c355SJohn Johansen 
2271c97204baSJohn Johansen 	return seq_open(file, &aa_sfs_profiles_op);
227229b3822fSJohn Johansen }
227329b3822fSJohn Johansen 
227429b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
227529b3822fSJohn Johansen {
227629b3822fSJohn Johansen 	return seq_release(inode, file);
227729b3822fSJohn Johansen }
227829b3822fSJohn Johansen 
2279c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = {
228029b3822fSJohn Johansen 	.open = profiles_open,
228129b3822fSJohn Johansen 	.read = seq_read,
228229b3822fSJohn Johansen 	.llseek = seq_lseek,
228329b3822fSJohn Johansen 	.release = profiles_release,
228429b3822fSJohn Johansen };
228529b3822fSJohn Johansen 
228629b3822fSJohn Johansen 
22870d259f04SJohn Johansen /** Base file system setup **/
2288c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = {
2289c97204baSJohn Johansen 	AA_SFS_FILE_STRING("mask",
2290c97204baSJohn Johansen 			   "create read write exec append mmap_exec link lock"),
2291a9bf8e9fSKees Cook 	{ }
2292a9bf8e9fSKees Cook };
2293a9bf8e9fSKees Cook 
2294290f458aSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ptrace[] = {
2295290f458aSJohn Johansen 	AA_SFS_FILE_STRING("mask", "read trace"),
2296290f458aSJohn Johansen 	{ }
2297290f458aSJohn Johansen };
2298290f458aSJohn Johansen 
2299cd1dbf76SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_signal[] = {
2300cd1dbf76SJohn Johansen 	AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK),
2301cd1dbf76SJohn Johansen 	{ }
2302cd1dbf76SJohn Johansen };
2303cd1dbf76SJohn Johansen 
230473f488cdSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_attach[] = {
230573f488cdSJohn Johansen 	AA_SFS_FILE_BOOLEAN("xattr", 1),
230673f488cdSJohn Johansen 	{ }
230773f488cdSJohn Johansen };
2308c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = {
2309c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hat",	1),
2310c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hatv",	1),
2311c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_onexec",	1),
2312c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_profile",	1),
23136c5fc8f1SJohn Johansen 	AA_SFS_FILE_BOOLEAN("stack",		1),
2314c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
23159fcf78ccSJohn Johansen 	AA_SFS_FILE_BOOLEAN("post_nnp_subset",	1),
231621f60661SJohn Johansen 	AA_SFS_FILE_BOOLEAN("computed_longest_left",	1),
231773f488cdSJohn Johansen 	AA_SFS_DIR("attach_conditions",		aa_sfs_entry_attach),
2318c97204baSJohn Johansen 	AA_SFS_FILE_STRING("version", "1.2"),
2319e74abcf3SKees Cook 	{ }
2320e74abcf3SKees Cook };
2321e74abcf3SKees Cook 
2322c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = {
2323c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("v5",	1),
23245379a331SJohn Johansen 	AA_SFS_FILE_BOOLEAN("v6",	1),
23255379a331SJohn Johansen 	AA_SFS_FILE_BOOLEAN("v7",	1),
232656974a6fSJohn Johansen 	AA_SFS_FILE_BOOLEAN("v8",	1),
2327474d6b75SJohn Johansen 	{ }
2328474d6b75SJohn Johansen };
2329474d6b75SJohn Johansen 
2330c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = {
2331c97204baSJohn Johansen 	AA_SFS_DIR("versions",			aa_sfs_entry_versions),
2332c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("set_load",		1),
23330df34a64SJohn Johansen 	/* number of out of band transitions supported */
23340df34a64SJohn Johansen 	AA_SFS_FILE_U64("outofband",		MAX_OOB_SUPPORTED),
23359d910a3bSJohn Johansen 	{ }
23369d910a3bSJohn Johansen };
23379d910a3bSJohn Johansen 
23382ea3ffb7SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_mount[] = {
23392ea3ffb7SJohn Johansen 	AA_SFS_FILE_STRING("mask", "mount umount pivot_root"),
23402ea3ffb7SJohn Johansen 	{ }
23412ea3ffb7SJohn Johansen };
23422ea3ffb7SJohn Johansen 
234333f2eadaSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ns[] = {
234433f2eadaSJohn Johansen 	AA_SFS_FILE_BOOLEAN("profile",		1),
23452ea3ffb7SJohn Johansen 	AA_SFS_FILE_BOOLEAN("pivot_root",	0),
234633f2eadaSJohn Johansen 	{ }
234733f2eadaSJohn Johansen };
234833f2eadaSJohn Johansen 
2349a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = {
23504f3b3f2dSJohn Johansen 	AA_SFS_FILE_STRING("perms", "allow deny audit quiet"),
2351a83bd86eSJohn Johansen 	AA_SFS_FILE_BOOLEAN("data",		1),
23521dea3b41SJohn Johansen 	AA_SFS_FILE_BOOLEAN("multi_transaction",	1),
2353a83bd86eSJohn Johansen 	{ }
2354a83bd86eSJohn Johansen };
2355a83bd86eSJohn Johansen 
2356a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = {
2357a83bd86eSJohn Johansen 	AA_SFS_DIR("label",			aa_sfs_entry_query_label),
2358a83bd86eSJohn Johansen 	{ }
2359a83bd86eSJohn Johansen };
2360c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = {
2361c97204baSJohn Johansen 	AA_SFS_DIR("policy",			aa_sfs_entry_policy),
2362c97204baSJohn Johansen 	AA_SFS_DIR("domain",			aa_sfs_entry_domain),
2363c97204baSJohn Johansen 	AA_SFS_DIR("file",			aa_sfs_entry_file),
236456974a6fSJohn Johansen 	AA_SFS_DIR("network_v8",		aa_sfs_entry_network),
23652ea3ffb7SJohn Johansen 	AA_SFS_DIR("mount",			aa_sfs_entry_mount),
236633f2eadaSJohn Johansen 	AA_SFS_DIR("namespaces",		aa_sfs_entry_ns),
2367c97204baSJohn Johansen 	AA_SFS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
2368c97204baSJohn Johansen 	AA_SFS_DIR("rlimit",			aa_sfs_entry_rlimit),
2369c97204baSJohn Johansen 	AA_SFS_DIR("caps",			aa_sfs_entry_caps),
2370290f458aSJohn Johansen 	AA_SFS_DIR("ptrace",			aa_sfs_entry_ptrace),
2371cd1dbf76SJohn Johansen 	AA_SFS_DIR("signal",			aa_sfs_entry_signal),
2372a83bd86eSJohn Johansen 	AA_SFS_DIR("query",			aa_sfs_entry_query),
2373e74abcf3SKees Cook 	{ }
2374e74abcf3SKees Cook };
2375e74abcf3SKees Cook 
2376c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
2377bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access),
23786c5fc8f1SJohn Johansen 	AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops),
23796c5fc8f1SJohn Johansen 	AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops),
2380bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops),
2381bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops),
2382bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops),
2383c97204baSJohn Johansen 	AA_SFS_DIR("features", aa_sfs_entry_features),
23849acd494bSKees Cook 	{ }
23859acd494bSKees Cook };
238663e2b423SJohn Johansen 
2387c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry =
2388c97204baSJohn Johansen 	AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
23899acd494bSKees Cook 
23909acd494bSKees Cook /**
2391a481f4d9SJohn Johansen  * entry_create_file - create a file entry in the apparmor securityfs
2392c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
23939acd494bSKees Cook  * @parent: the parent dentry in the securityfs
23949acd494bSKees Cook  *
2395a481f4d9SJohn Johansen  * Use entry_remove_file to remove entries created with this fn.
23969acd494bSKees Cook  */
2397c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file,
23989acd494bSKees Cook 				    struct dentry *parent)
239963e2b423SJohn Johansen {
24009acd494bSKees Cook 	int error = 0;
240163e2b423SJohn Johansen 
24029acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
24039acd494bSKees Cook 						 S_IFREG | fs_file->mode,
24049acd494bSKees Cook 						 parent, fs_file,
24059acd494bSKees Cook 						 fs_file->file_ops);
24069acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
24079acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
24089acd494bSKees Cook 		fs_file->dentry = NULL;
240963e2b423SJohn Johansen 	}
24109acd494bSKees Cook 	return error;
241163e2b423SJohn Johansen }
241263e2b423SJohn Johansen 
2413c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
241463e2b423SJohn Johansen /**
2415a481f4d9SJohn Johansen  * entry_create_dir - recursively create a directory entry in the securityfs
2416c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
24179acd494bSKees Cook  * @parent: the parent dentry in the securityfs
241863e2b423SJohn Johansen  *
2419a481f4d9SJohn Johansen  * Use entry_remove_dir to remove entries created with this fn.
242063e2b423SJohn Johansen  */
2421c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
24229acd494bSKees Cook 				   struct dentry *parent)
242363e2b423SJohn Johansen {
2424c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
24250d259f04SJohn Johansen 	struct dentry *dir;
24260d259f04SJohn Johansen 	int error;
242763e2b423SJohn Johansen 
24280d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
24290d259f04SJohn Johansen 	if (IS_ERR(dir))
24300d259f04SJohn Johansen 		return PTR_ERR(dir);
24310d259f04SJohn Johansen 	fs_dir->dentry = dir;
243263e2b423SJohn Johansen 
24330d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2434c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2435a481f4d9SJohn Johansen 			error = entry_create_dir(fs_file, fs_dir->dentry);
24369acd494bSKees Cook 		else
2437a481f4d9SJohn Johansen 			error = entry_create_file(fs_file, fs_dir->dentry);
24389acd494bSKees Cook 		if (error)
24399acd494bSKees Cook 			goto failed;
24409acd494bSKees Cook 	}
24419acd494bSKees Cook 
24429acd494bSKees Cook 	return 0;
24439acd494bSKees Cook 
24449acd494bSKees Cook failed:
2445a481f4d9SJohn Johansen 	entry_remove_dir(fs_dir);
24460d259f04SJohn Johansen 
24479acd494bSKees Cook 	return error;
24489acd494bSKees Cook }
24499acd494bSKees Cook 
24509acd494bSKees Cook /**
2451c97204baSJohn Johansen  * entry_remove_file - drop a single file entry in the apparmor securityfs
2452c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
24539acd494bSKees Cook  */
2454c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
24559acd494bSKees Cook {
24569acd494bSKees Cook 	if (!fs_file->dentry)
24579acd494bSKees Cook 		return;
24589acd494bSKees Cook 
24599acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
24609acd494bSKees Cook 	fs_file->dentry = NULL;
24619acd494bSKees Cook }
24629acd494bSKees Cook 
24639acd494bSKees Cook /**
2464a481f4d9SJohn Johansen  * entry_remove_dir - recursively drop a directory entry from the securityfs
2465c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
24669acd494bSKees Cook  */
2467c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
24689acd494bSKees Cook {
2469c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
24709acd494bSKees Cook 
24710d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2472c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2473a481f4d9SJohn Johansen 			entry_remove_dir(fs_file);
24749acd494bSKees Cook 		else
2475c97204baSJohn Johansen 			entry_remove_file(fs_file);
24769acd494bSKees Cook 	}
24779acd494bSKees Cook 
2478c97204baSJohn Johansen 	entry_remove_file(fs_dir);
247963e2b423SJohn Johansen }
248063e2b423SJohn Johansen 
248163e2b423SJohn Johansen /**
248263e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
248363e2b423SJohn Johansen  *
248463e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
248563e2b423SJohn Johansen  */
248663e2b423SJohn Johansen void __init aa_destroy_aafs(void)
248763e2b423SJohn Johansen {
2488c97204baSJohn Johansen 	entry_remove_dir(&aa_sfs_entry);
248963e2b423SJohn Johansen }
249063e2b423SJohn Johansen 
2491a71ada30SJohn Johansen 
2492a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
2493a71ada30SJohn Johansen struct path aa_null;
2494a71ada30SJohn Johansen 
2495a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
2496a71ada30SJohn Johansen {
2497a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
2498a71ada30SJohn Johansen 	struct dentry *dentry;
2499a71ada30SJohn Johansen 	struct inode *inode;
2500a71ada30SJohn Johansen 	int count = 0;
2501a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
2502a71ada30SJohn Johansen 
2503a71ada30SJohn Johansen 	if (error)
2504a71ada30SJohn Johansen 		return error;
2505a71ada30SJohn Johansen 
2506a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
2507a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
2508a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
2509a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
2510a71ada30SJohn Johansen 		goto out;
2511a71ada30SJohn Johansen 	}
2512a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
2513a71ada30SJohn Johansen 	if (!inode) {
2514a71ada30SJohn Johansen 		error = -ENOMEM;
2515a71ada30SJohn Johansen 		goto out1;
2516a71ada30SJohn Johansen 	}
2517a71ada30SJohn Johansen 
2518a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
2519a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
252024d0d03cSDeepa Dinamani 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
2521a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
2522a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
2523a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
2524a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
2525a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
2526a71ada30SJohn Johansen 
2527a71ada30SJohn Johansen 	error = 0;
2528a71ada30SJohn Johansen 
2529a71ada30SJohn Johansen out1:
2530a71ada30SJohn Johansen 	dput(dentry);
2531a71ada30SJohn Johansen out:
2532a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
2533a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
2534a71ada30SJohn Johansen 	return error;
2535a71ada30SJohn Johansen }
2536a71ada30SJohn Johansen 
2537a481f4d9SJohn Johansen 
2538a481f4d9SJohn Johansen 
2539a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry,
2540a481f4d9SJohn Johansen 				   struct inode *inode,
2541a481f4d9SJohn Johansen 				   struct delayed_call *done)
2542a481f4d9SJohn Johansen {
2543a481f4d9SJohn Johansen 	struct aa_ns *ns;
2544a481f4d9SJohn Johansen 	struct path path;
2545a481f4d9SJohn Johansen 
2546a481f4d9SJohn Johansen 	if (!dentry)
2547a481f4d9SJohn Johansen 		return ERR_PTR(-ECHILD);
2548a481f4d9SJohn Johansen 	ns = aa_get_current_ns();
2549a481f4d9SJohn Johansen 	path.mnt = mntget(aafs_mnt);
2550a481f4d9SJohn Johansen 	path.dentry = dget(ns_dir(ns));
2551a481f4d9SJohn Johansen 	nd_jump_link(&path);
2552a481f4d9SJohn Johansen 	aa_put_ns(ns);
2553a481f4d9SJohn Johansen 
2554a481f4d9SJohn Johansen 	return NULL;
2555a481f4d9SJohn Johansen }
2556a481f4d9SJohn Johansen 
2557a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer,
2558a481f4d9SJohn Johansen 			   int buflen)
2559a481f4d9SJohn Johansen {
2560a481f4d9SJohn Johansen 	char name[32];
2561a481f4d9SJohn Johansen 	int res;
2562a481f4d9SJohn Johansen 
2563a0781209SJohn Johansen 	res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME,
2564a0781209SJohn Johansen 		       d_inode(dentry)->i_ino);
2565a0781209SJohn Johansen 	if (res > 0 && res < sizeof(name))
2566a481f4d9SJohn Johansen 		res = readlink_copy(buffer, buflen, name);
2567a0781209SJohn Johansen 	else
2568a0781209SJohn Johansen 		res = -ENOENT;
2569a481f4d9SJohn Johansen 
2570a481f4d9SJohn Johansen 	return res;
2571a481f4d9SJohn Johansen }
2572a481f4d9SJohn Johansen 
2573a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = {
2574a481f4d9SJohn Johansen 	.readlink	= policy_readlink,
2575a481f4d9SJohn Johansen 	.get_link	= policy_get_link,
2576a481f4d9SJohn Johansen };
2577a481f4d9SJohn Johansen 
2578a481f4d9SJohn Johansen 
257963e2b423SJohn Johansen /**
258063e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
258163e2b423SJohn Johansen  *
258263e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
258363e2b423SJohn Johansen  *
258463e2b423SJohn Johansen  * Returns: error on failure
258563e2b423SJohn Johansen  */
25863417d8d5SJames Morris static int __init aa_create_aafs(void)
258763e2b423SJohn Johansen {
2588b7fd2c03SJohn Johansen 	struct dentry *dent;
258963e2b423SJohn Johansen 	int error;
259063e2b423SJohn Johansen 
259163e2b423SJohn Johansen 	if (!apparmor_initialized)
259263e2b423SJohn Johansen 		return 0;
259363e2b423SJohn Johansen 
2594c97204baSJohn Johansen 	if (aa_sfs_entry.dentry) {
259563e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
259663e2b423SJohn Johansen 		return -EEXIST;
259763e2b423SJohn Johansen 	}
259863e2b423SJohn Johansen 
2599a481f4d9SJohn Johansen 	/* setup apparmorfs used to virtualize policy/ */
2600a481f4d9SJohn Johansen 	aafs_mnt = kern_mount(&aafs_ops);
2601a481f4d9SJohn Johansen 	if (IS_ERR(aafs_mnt))
2602a481f4d9SJohn Johansen 		panic("can't set apparmorfs up\n");
26031751e8a6SLinus Torvalds 	aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
2604a481f4d9SJohn Johansen 
26059acd494bSKees Cook 	/* Populate fs tree. */
2606c97204baSJohn Johansen 	error = entry_create_dir(&aa_sfs_entry, NULL);
260763e2b423SJohn Johansen 	if (error)
260863e2b423SJohn Johansen 		goto error;
260963e2b423SJohn Johansen 
2610c97204baSJohn Johansen 	dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
2611b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_load);
2612cf916000SJohn Johansen 	if (IS_ERR(dent))
2613cf916000SJohn Johansen 		goto dent_error;
2614b7fd2c03SJohn Johansen 	ns_subload(root_ns) = dent;
2615b7fd2c03SJohn Johansen 
2616c97204baSJohn Johansen 	dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
2617b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_replace);
2618cf916000SJohn Johansen 	if (IS_ERR(dent))
2619cf916000SJohn Johansen 		goto dent_error;
2620b7fd2c03SJohn Johansen 	ns_subreplace(root_ns) = dent;
2621b7fd2c03SJohn Johansen 
2622c97204baSJohn Johansen 	dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
2623b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_remove);
2624cf916000SJohn Johansen 	if (IS_ERR(dent))
2625cf916000SJohn Johansen 		goto dent_error;
2626b7fd2c03SJohn Johansen 	ns_subremove(root_ns) = dent;
2627b7fd2c03SJohn Johansen 
2628d9bf2c26SJohn Johansen 	dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry,
2629d9bf2c26SJohn Johansen 				      NULL, &aa_fs_ns_revision_fops);
2630cf916000SJohn Johansen 	if (IS_ERR(dent))
2631cf916000SJohn Johansen 		goto dent_error;
2632d9bf2c26SJohn Johansen 	ns_subrevision(root_ns) = dent;
2633d9bf2c26SJohn Johansen 
2634d9bf2c26SJohn Johansen 	/* policy tree referenced by magic policy symlink */
2635feb3c766SJohn Johansen 	mutex_lock_nested(&root_ns->lock, root_ns->level);
2636c961ee5fSJohn Johansen 	error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
2637c961ee5fSJohn Johansen 				aafs_mnt->mnt_root);
2638b7fd2c03SJohn Johansen 	mutex_unlock(&root_ns->lock);
26390d259f04SJohn Johansen 	if (error)
26400d259f04SJohn Johansen 		goto error;
26410d259f04SJohn Johansen 
2642c961ee5fSJohn Johansen 	/* magic symlink similar to nsfs redirects based on task policy */
2643c961ee5fSJohn Johansen 	dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
2644c961ee5fSJohn Johansen 					 NULL, &policy_link_iops);
2645cf916000SJohn Johansen 	if (IS_ERR(dent))
2646cf916000SJohn Johansen 		goto dent_error;
2647c961ee5fSJohn Johansen 
2648c97204baSJohn Johansen 	error = aa_mk_null_file(aa_sfs_entry.dentry);
2649a71ada30SJohn Johansen 	if (error)
2650a71ada30SJohn Johansen 		goto error;
2651a71ada30SJohn Johansen 
2652a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
265363e2b423SJohn Johansen 
265463e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
265563e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
265663e2b423SJohn Johansen 	return 0;
265763e2b423SJohn Johansen 
2658cf916000SJohn Johansen dent_error:
2659cf916000SJohn Johansen 	error = PTR_ERR(dent);
266063e2b423SJohn Johansen error:
266163e2b423SJohn Johansen 	aa_destroy_aafs();
266263e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
266363e2b423SJohn Johansen 	return error;
266463e2b423SJohn Johansen }
266563e2b423SJohn Johansen 
266663e2b423SJohn Johansen fs_initcall(aa_create_aafs);
2667