163e2b423SJohn Johansen /*
263e2b423SJohn Johansen  * AppArmor security module
363e2b423SJohn Johansen  *
463e2b423SJohn Johansen  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
563e2b423SJohn Johansen  *
663e2b423SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
763e2b423SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
863e2b423SJohn Johansen  *
963e2b423SJohn Johansen  * This program is free software; you can redistribute it and/or
1063e2b423SJohn Johansen  * modify it under the terms of the GNU General Public License as
1163e2b423SJohn Johansen  * published by the Free Software Foundation, version 2 of the
1263e2b423SJohn Johansen  * License.
1363e2b423SJohn Johansen  */
1463e2b423SJohn Johansen 
150d259f04SJohn Johansen #include <linux/ctype.h>
1663e2b423SJohn Johansen #include <linux/security.h>
1763e2b423SJohn Johansen #include <linux/vmalloc.h>
1863e2b423SJohn Johansen #include <linux/module.h>
1963e2b423SJohn Johansen #include <linux/seq_file.h>
2063e2b423SJohn Johansen #include <linux/uaccess.h>
21a71ada30SJohn Johansen #include <linux/mount.h>
2263e2b423SJohn Johansen #include <linux/namei.h>
23e74abcf3SKees Cook #include <linux/capability.h>
2429b3822fSJohn Johansen #include <linux/rcupdate.h>
25a71ada30SJohn Johansen #include <linux/fs.h>
26d9bf2c26SJohn Johansen #include <linux/poll.h>
27a481f4d9SJohn Johansen #include <uapi/linux/major.h>
28a481f4d9SJohn Johansen #include <uapi/linux/magic.h>
2963e2b423SJohn Johansen 
3063e2b423SJohn Johansen #include "include/apparmor.h"
3163e2b423SJohn Johansen #include "include/apparmorfs.h"
3263e2b423SJohn Johansen #include "include/audit.h"
33d8889d49SJohn Johansen #include "include/cred.h"
34f8eb8a13SJohn Johansen #include "include/crypto.h"
35cd1dbf76SJohn Johansen #include "include/ipc.h"
36317d9a05SJohn Johansen #include "include/label.h"
3763e2b423SJohn Johansen #include "include/policy.h"
38cff281f6SJohn Johansen #include "include/policy_ns.h"
39d384b0a1SKees Cook #include "include/resource.h"
405ac8c355SJohn Johansen #include "include/policy_unpack.h"
4163e2b423SJohn Johansen 
42c97204baSJohn Johansen /*
43c97204baSJohn Johansen  * The apparmor filesystem interface used for policy load and introspection
44c97204baSJohn Johansen  * The interface is split into two main components based on their function
45c97204baSJohn Johansen  * a securityfs component:
46c97204baSJohn Johansen  *   used for static files that are always available, and which allows
47c97204baSJohn Johansen  *   userspace to specificy the location of the security filesystem.
48c97204baSJohn Johansen  *
49c97204baSJohn Johansen  *   fns and data are prefixed with
50c97204baSJohn Johansen  *      aa_sfs_
51c97204baSJohn Johansen  *
52c97204baSJohn Johansen  * an apparmorfs component:
53c97204baSJohn Johansen  *   used loaded policy content and introspection. It is not part of  a
54c97204baSJohn Johansen  *   regular mounted filesystem and is available only through the magic
55c97204baSJohn Johansen  *   policy symlink in the root of the securityfs apparmor/ directory.
56c97204baSJohn Johansen  *   Tasks queries will be magically redirected to the correct portion
57c97204baSJohn Johansen  *   of the policy tree based on their confinement.
58c97204baSJohn Johansen  *
59c97204baSJohn Johansen  *   fns and data are prefixed with
60c97204baSJohn Johansen  *      aafs_
61c97204baSJohn Johansen  *
62c97204baSJohn Johansen  * The aa_fs_ prefix is used to indicate the fn is used by both the
63c97204baSJohn Johansen  * securityfs and apparmorfs filesystems.
64c97204baSJohn Johansen  */
65c97204baSJohn Johansen 
66c97204baSJohn Johansen 
67c97204baSJohn Johansen /*
68c97204baSJohn Johansen  * support fns
69c97204baSJohn Johansen  */
70c97204baSJohn Johansen 
7163e2b423SJohn Johansen /**
720d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
730d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
740d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
750d259f04SJohn Johansen  *
760d259f04SJohn Johansen  * Returns: length of mangled name
770d259f04SJohn Johansen  */
78bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
790d259f04SJohn Johansen {
800d259f04SJohn Johansen 	char *t = target;
810d259f04SJohn Johansen 
820d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
830d259f04SJohn Johansen 		name++;
840d259f04SJohn Johansen 
850d259f04SJohn Johansen 	if (target) {
860d259f04SJohn Johansen 		for (; *name; name++) {
870d259f04SJohn Johansen 			if (*name == '/')
880d259f04SJohn Johansen 				*(t)++ = '.';
890d259f04SJohn Johansen 			else if (isspace(*name))
900d259f04SJohn Johansen 				*(t)++ = '_';
910d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
920d259f04SJohn Johansen 				*(t)++ = *name;
930d259f04SJohn Johansen 		}
940d259f04SJohn Johansen 
950d259f04SJohn Johansen 		*t = 0;
960d259f04SJohn Johansen 	} else {
970d259f04SJohn Johansen 		int len = 0;
980d259f04SJohn Johansen 		for (; *name; name++) {
990d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
1000d259f04SJohn Johansen 			    strchr("/._-", *name))
1010d259f04SJohn Johansen 				len++;
1020d259f04SJohn Johansen 		}
1030d259f04SJohn Johansen 
1040d259f04SJohn Johansen 		return len;
1050d259f04SJohn Johansen 	}
1060d259f04SJohn Johansen 
1070d259f04SJohn Johansen 	return t - target;
1080d259f04SJohn Johansen }
1090d259f04SJohn Johansen 
110a481f4d9SJohn Johansen 
111a481f4d9SJohn Johansen /*
112a481f4d9SJohn Johansen  * aafs - core fns and data for the policy tree
113a481f4d9SJohn Johansen  */
114a481f4d9SJohn Johansen 
115a481f4d9SJohn Johansen #define AAFS_NAME		"apparmorfs"
116a481f4d9SJohn Johansen static struct vfsmount *aafs_mnt;
117a481f4d9SJohn Johansen static int aafs_count;
118a481f4d9SJohn Johansen 
119a481f4d9SJohn Johansen 
120a481f4d9SJohn Johansen static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
121a481f4d9SJohn Johansen {
122a481f4d9SJohn Johansen 	struct inode *inode = d_inode(dentry);
123a481f4d9SJohn Johansen 
124a481f4d9SJohn Johansen 	seq_printf(seq, "%s:[%lu]", AAFS_NAME, inode->i_ino);
125a481f4d9SJohn Johansen 	return 0;
126a481f4d9SJohn Johansen }
127a481f4d9SJohn Johansen 
128a481f4d9SJohn Johansen static void aafs_evict_inode(struct inode *inode)
129a481f4d9SJohn Johansen {
130a481f4d9SJohn Johansen 	truncate_inode_pages_final(&inode->i_data);
131a481f4d9SJohn Johansen 	clear_inode(inode);
132a481f4d9SJohn Johansen 	if (S_ISLNK(inode->i_mode))
133a481f4d9SJohn Johansen 		kfree(inode->i_link);
134a481f4d9SJohn Johansen }
135a481f4d9SJohn Johansen 
136a481f4d9SJohn Johansen static const struct super_operations aafs_super_ops = {
137a481f4d9SJohn Johansen 	.statfs = simple_statfs,
138a481f4d9SJohn Johansen 	.evict_inode = aafs_evict_inode,
139a481f4d9SJohn Johansen 	.show_path = aafs_show_path,
140a481f4d9SJohn Johansen };
141a481f4d9SJohn Johansen 
142a481f4d9SJohn Johansen static int fill_super(struct super_block *sb, void *data, int silent)
143a481f4d9SJohn Johansen {
144a481f4d9SJohn Johansen 	static struct tree_descr files[] = { {""} };
145a481f4d9SJohn Johansen 	int error;
146a481f4d9SJohn Johansen 
147a481f4d9SJohn Johansen 	error = simple_fill_super(sb, AAFS_MAGIC, files);
148a481f4d9SJohn Johansen 	if (error)
149a481f4d9SJohn Johansen 		return error;
150a481f4d9SJohn Johansen 	sb->s_op = &aafs_super_ops;
151a481f4d9SJohn Johansen 
152a481f4d9SJohn Johansen 	return 0;
153a481f4d9SJohn Johansen }
154a481f4d9SJohn Johansen 
155a481f4d9SJohn Johansen static struct dentry *aafs_mount(struct file_system_type *fs_type,
156a481f4d9SJohn Johansen 				 int flags, const char *dev_name, void *data)
157a481f4d9SJohn Johansen {
158a481f4d9SJohn Johansen 	return mount_single(fs_type, flags, data, fill_super);
159a481f4d9SJohn Johansen }
160a481f4d9SJohn Johansen 
161a481f4d9SJohn Johansen static struct file_system_type aafs_ops = {
162a481f4d9SJohn Johansen 	.owner = THIS_MODULE,
163a481f4d9SJohn Johansen 	.name = AAFS_NAME,
164a481f4d9SJohn Johansen 	.mount = aafs_mount,
165a481f4d9SJohn Johansen 	.kill_sb = kill_anon_super,
166a481f4d9SJohn Johansen };
167a481f4d9SJohn Johansen 
168a481f4d9SJohn Johansen /**
169a481f4d9SJohn Johansen  * __aafs_setup_d_inode - basic inode setup for apparmorfs
170a481f4d9SJohn Johansen  * @dir: parent directory for the dentry
171a481f4d9SJohn Johansen  * @dentry: dentry we are seting the inode up for
172a481f4d9SJohn Johansen  * @mode: permissions the file should have
173a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
174a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
175a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used
176a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
177a481f4d9SJohn Johansen  */
178a481f4d9SJohn Johansen static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
179a481f4d9SJohn Johansen 			       umode_t mode, void *data, char *link,
180a481f4d9SJohn Johansen 			       const struct file_operations *fops,
181a481f4d9SJohn Johansen 			       const struct inode_operations *iops)
182a481f4d9SJohn Johansen {
183a481f4d9SJohn Johansen 	struct inode *inode = new_inode(dir->i_sb);
184a481f4d9SJohn Johansen 
185a481f4d9SJohn Johansen 	AA_BUG(!dir);
186a481f4d9SJohn Johansen 	AA_BUG(!dentry);
187a481f4d9SJohn Johansen 
188a481f4d9SJohn Johansen 	if (!inode)
189a481f4d9SJohn Johansen 		return -ENOMEM;
190a481f4d9SJohn Johansen 
191a481f4d9SJohn Johansen 	inode->i_ino = get_next_ino();
192a481f4d9SJohn Johansen 	inode->i_mode = mode;
193a481f4d9SJohn Johansen 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
194a481f4d9SJohn Johansen 	inode->i_private = data;
195a481f4d9SJohn Johansen 	if (S_ISDIR(mode)) {
196a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_dir_inode_operations;
197a481f4d9SJohn Johansen 		inode->i_fop = &simple_dir_operations;
198a481f4d9SJohn Johansen 		inc_nlink(inode);
199a481f4d9SJohn Johansen 		inc_nlink(dir);
200a481f4d9SJohn Johansen 	} else if (S_ISLNK(mode)) {
201a481f4d9SJohn Johansen 		inode->i_op = iops ? iops : &simple_symlink_inode_operations;
202a481f4d9SJohn Johansen 		inode->i_link = link;
203a481f4d9SJohn Johansen 	} else {
204a481f4d9SJohn Johansen 		inode->i_fop = fops;
205a481f4d9SJohn Johansen 	}
206a481f4d9SJohn Johansen 	d_instantiate(dentry, inode);
207a481f4d9SJohn Johansen 	dget(dentry);
208a481f4d9SJohn Johansen 
209a481f4d9SJohn Johansen 	return 0;
210a481f4d9SJohn Johansen }
211a481f4d9SJohn Johansen 
212a481f4d9SJohn Johansen /**
213a481f4d9SJohn Johansen  * aafs_create - create a dentry in the apparmorfs filesystem
214a481f4d9SJohn Johansen  *
215a481f4d9SJohn Johansen  * @name: name of dentry to create
216a481f4d9SJohn Johansen  * @mode: permissions the file should have
217a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
218a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
219a481f4d9SJohn Johansen  * @link: if symlink, symlink target string
220a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
221a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
222a481f4d9SJohn Johansen  *
223a481f4d9SJohn Johansen  * This is the basic "create a xxx" function for apparmorfs.
224a481f4d9SJohn Johansen  *
225a481f4d9SJohn Johansen  * Returns a pointer to a dentry if it succeeds, that must be free with
226a481f4d9SJohn Johansen  * aafs_remove(). Will return ERR_PTR on failure.
227a481f4d9SJohn Johansen  */
228a481f4d9SJohn Johansen static struct dentry *aafs_create(const char *name, umode_t mode,
229a481f4d9SJohn Johansen 				  struct dentry *parent, void *data, void *link,
230a481f4d9SJohn Johansen 				  const struct file_operations *fops,
231a481f4d9SJohn Johansen 				  const struct inode_operations *iops)
232a481f4d9SJohn Johansen {
233a481f4d9SJohn Johansen 	struct dentry *dentry;
234a481f4d9SJohn Johansen 	struct inode *dir;
235a481f4d9SJohn Johansen 	int error;
236a481f4d9SJohn Johansen 
237a481f4d9SJohn Johansen 	AA_BUG(!name);
238a481f4d9SJohn Johansen 	AA_BUG(!parent);
239a481f4d9SJohn Johansen 
240a481f4d9SJohn Johansen 	if (!(mode & S_IFMT))
241a481f4d9SJohn Johansen 		mode = (mode & S_IALLUGO) | S_IFREG;
242a481f4d9SJohn Johansen 
243a481f4d9SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
244a481f4d9SJohn Johansen 	if (error)
245a481f4d9SJohn Johansen 		return ERR_PTR(error);
246a481f4d9SJohn Johansen 
247a481f4d9SJohn Johansen 	dir = d_inode(parent);
248a481f4d9SJohn Johansen 
249a481f4d9SJohn Johansen 	inode_lock(dir);
250a481f4d9SJohn Johansen 	dentry = lookup_one_len(name, parent, strlen(name));
2515d314a81SDan Carpenter 	if (IS_ERR(dentry)) {
2525d314a81SDan Carpenter 		error = PTR_ERR(dentry);
253a481f4d9SJohn Johansen 		goto fail_lock;
2545d314a81SDan Carpenter 	}
255a481f4d9SJohn Johansen 
256a481f4d9SJohn Johansen 	if (d_really_is_positive(dentry)) {
257a481f4d9SJohn Johansen 		error = -EEXIST;
258a481f4d9SJohn Johansen 		goto fail_dentry;
259a481f4d9SJohn Johansen 	}
260a481f4d9SJohn Johansen 
261a481f4d9SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
262a481f4d9SJohn Johansen 	if (error)
263a481f4d9SJohn Johansen 		goto fail_dentry;
264a481f4d9SJohn Johansen 	inode_unlock(dir);
265a481f4d9SJohn Johansen 
266a481f4d9SJohn Johansen 	return dentry;
267a481f4d9SJohn Johansen 
268a481f4d9SJohn Johansen fail_dentry:
269a481f4d9SJohn Johansen 	dput(dentry);
270a481f4d9SJohn Johansen 
271a481f4d9SJohn Johansen fail_lock:
272a481f4d9SJohn Johansen 	inode_unlock(dir);
273a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
274a481f4d9SJohn Johansen 
275a481f4d9SJohn Johansen 	return ERR_PTR(error);
276a481f4d9SJohn Johansen }
277a481f4d9SJohn Johansen 
278a481f4d9SJohn Johansen /**
279a481f4d9SJohn Johansen  * aafs_create_file - create a file in the apparmorfs filesystem
280a481f4d9SJohn Johansen  *
281a481f4d9SJohn Johansen  * @name: name of dentry to create
282a481f4d9SJohn Johansen  * @mode: permissions the file should have
283a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
284a481f4d9SJohn Johansen  * @data: data to store on inode.i_private, available in open()
285a481f4d9SJohn Johansen  * @fops: struct file_operations that should be used for
286a481f4d9SJohn Johansen  *
287a481f4d9SJohn Johansen  * see aafs_create
288a481f4d9SJohn Johansen  */
289a481f4d9SJohn Johansen static struct dentry *aafs_create_file(const char *name, umode_t mode,
290a481f4d9SJohn Johansen 				       struct dentry *parent, void *data,
291a481f4d9SJohn Johansen 				       const struct file_operations *fops)
292a481f4d9SJohn Johansen {
293a481f4d9SJohn Johansen 	return aafs_create(name, mode, parent, data, NULL, fops, NULL);
294a481f4d9SJohn Johansen }
295a481f4d9SJohn Johansen 
296a481f4d9SJohn Johansen /**
297a481f4d9SJohn Johansen  * aafs_create_dir - create a directory in the apparmorfs filesystem
298a481f4d9SJohn Johansen  *
299a481f4d9SJohn Johansen  * @name: name of dentry to create
300a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
301a481f4d9SJohn Johansen  *
302a481f4d9SJohn Johansen  * see aafs_create
303a481f4d9SJohn Johansen  */
304a481f4d9SJohn Johansen static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
305a481f4d9SJohn Johansen {
306a481f4d9SJohn Johansen 	return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
307a481f4d9SJohn Johansen 			   NULL);
308a481f4d9SJohn Johansen }
309a481f4d9SJohn Johansen 
310a481f4d9SJohn Johansen /**
311a481f4d9SJohn Johansen  * aafs_create_symlink - create a symlink in the apparmorfs filesystem
312a481f4d9SJohn Johansen  * @name: name of dentry to create
313a481f4d9SJohn Johansen  * @parent: parent directory for this dentry
314a481f4d9SJohn Johansen  * @target: if symlink, symlink target string
315a481f4d9SJohn Johansen  * @iops: struct of inode_operations that should be used
316a481f4d9SJohn Johansen  *
317a481f4d9SJohn Johansen  * If @target parameter is %NULL, then the @iops parameter needs to be
318a481f4d9SJohn Johansen  * setup to handle .readlink and .get_link inode_operations.
319a481f4d9SJohn Johansen  */
320a481f4d9SJohn Johansen static struct dentry *aafs_create_symlink(const char *name,
321a481f4d9SJohn Johansen 					  struct dentry *parent,
322a481f4d9SJohn Johansen 					  const char *target,
323a481f4d9SJohn Johansen 					  const struct inode_operations *iops)
324a481f4d9SJohn Johansen {
325a481f4d9SJohn Johansen 	struct dentry *dent;
326a481f4d9SJohn Johansen 	char *link = NULL;
327a481f4d9SJohn Johansen 
328a481f4d9SJohn Johansen 	if (target) {
329a481f4d9SJohn Johansen 		link = kstrdup(target, GFP_KERNEL);
330a481f4d9SJohn Johansen 		if (!link)
331a481f4d9SJohn Johansen 			return ERR_PTR(-ENOMEM);
332a481f4d9SJohn Johansen 	}
333a481f4d9SJohn Johansen 	dent = aafs_create(name, S_IFLNK | 0444, parent, NULL, link, NULL,
334a481f4d9SJohn Johansen 			   iops);
335a481f4d9SJohn Johansen 	if (IS_ERR(dent))
336a481f4d9SJohn Johansen 		kfree(link);
337a481f4d9SJohn Johansen 
338a481f4d9SJohn Johansen 	return dent;
339a481f4d9SJohn Johansen }
340a481f4d9SJohn Johansen 
341a481f4d9SJohn Johansen /**
342a481f4d9SJohn Johansen  * aafs_remove - removes a file or directory from the apparmorfs filesystem
343a481f4d9SJohn Johansen  *
344a481f4d9SJohn Johansen  * @dentry: dentry of the file/directory/symlink to removed.
345a481f4d9SJohn Johansen  */
346a481f4d9SJohn Johansen static void aafs_remove(struct dentry *dentry)
347a481f4d9SJohn Johansen {
348a481f4d9SJohn Johansen 	struct inode *dir;
349a481f4d9SJohn Johansen 
350a481f4d9SJohn Johansen 	if (!dentry || IS_ERR(dentry))
351a481f4d9SJohn Johansen 		return;
352a481f4d9SJohn Johansen 
353a481f4d9SJohn Johansen 	dir = d_inode(dentry->d_parent);
354a481f4d9SJohn Johansen 	inode_lock(dir);
355a481f4d9SJohn Johansen 	if (simple_positive(dentry)) {
356a481f4d9SJohn Johansen 		if (d_is_dir(dentry))
357a481f4d9SJohn Johansen 			simple_rmdir(dir, dentry);
358a481f4d9SJohn Johansen 		else
359a481f4d9SJohn Johansen 			simple_unlink(dir, dentry);
360a481f4d9SJohn Johansen 		dput(dentry);
361a481f4d9SJohn Johansen 	}
362a481f4d9SJohn Johansen 	inode_unlock(dir);
363a481f4d9SJohn Johansen 	simple_release_fs(&aafs_mnt, &aafs_count);
364a481f4d9SJohn Johansen }
365a481f4d9SJohn Johansen 
366a481f4d9SJohn Johansen 
367a481f4d9SJohn Johansen /*
368a481f4d9SJohn Johansen  * aa_fs - policy load/replace/remove
369a481f4d9SJohn Johansen  */
370a481f4d9SJohn Johansen 
3710d259f04SJohn Johansen /**
37263e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
37363e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
3743ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
37563e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
37663e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
37763e2b423SJohn Johansen  *
37863e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
37963e2b423SJohn Johansen  *          ERR_PTR on failure.
38063e2b423SJohn Johansen  */
3815ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
3825ac8c355SJohn Johansen 						     size_t alloc_size,
3835ac8c355SJohn Johansen 						     size_t copy_size,
38463e2b423SJohn Johansen 						     loff_t *pos)
38563e2b423SJohn Johansen {
3865ac8c355SJohn Johansen 	struct aa_loaddata *data;
38763e2b423SJohn Johansen 
388e6bfa25dSJohn Johansen 	AA_BUG(copy_size > alloc_size);
3893ed02adaSJohn Johansen 
39063e2b423SJohn Johansen 	if (*pos != 0)
39163e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
39263e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
39363e2b423SJohn Johansen 
39463e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
3955d5182caSJohn Johansen 	data = aa_loaddata_alloc(alloc_size);
3965d5182caSJohn Johansen 	if (IS_ERR(data))
3975d5182caSJohn Johansen 		return data;
39863e2b423SJohn Johansen 
3995d5182caSJohn Johansen 	data->size = copy_size;
4005ac8c355SJohn Johansen 	if (copy_from_user(data->data, userbuf, copy_size)) {
40163e2b423SJohn Johansen 		kvfree(data);
40263e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
40363e2b423SJohn Johansen 	}
40463e2b423SJohn Johansen 
40563e2b423SJohn Johansen 	return data;
40663e2b423SJohn Johansen }
40763e2b423SJohn Johansen 
40818e99f19SJohn Johansen static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
409b7fd2c03SJohn Johansen 			     loff_t *pos, struct aa_ns *ns)
4105ac8c355SJohn Johansen {
4115ac8c355SJohn Johansen 	struct aa_loaddata *data;
412637f688dSJohn Johansen 	struct aa_label *label;
413637f688dSJohn Johansen 	ssize_t error;
414cf797c0eSJohn Johansen 
415637f688dSJohn Johansen 	label = begin_current_label_crit_section();
416cf797c0eSJohn Johansen 
4175ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4185ac8c355SJohn Johansen 	 * below after unpack
4195ac8c355SJohn Johansen 	 */
420637f688dSJohn Johansen 	error = aa_may_manage_policy(label, ns, mask);
4215ac8c355SJohn Johansen 	if (error)
4225ac8c355SJohn Johansen 		return error;
42363e2b423SJohn Johansen 
4245ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size, size, pos);
4255ac8c355SJohn Johansen 	error = PTR_ERR(data);
4265ac8c355SJohn Johansen 	if (!IS_ERR(data)) {
427637f688dSJohn Johansen 		error = aa_replace_profiles(ns, label, mask, data);
4285ac8c355SJohn Johansen 		aa_put_loaddata(data);
4295ac8c355SJohn Johansen 	}
430637f688dSJohn Johansen 	end_current_label_crit_section(label);
4315ac8c355SJohn Johansen 
4325ac8c355SJohn Johansen 	return error;
4335ac8c355SJohn Johansen }
4345ac8c355SJohn Johansen 
435b7fd2c03SJohn Johansen /* .load file hook fn to load policy */
43663e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
43763e2b423SJohn Johansen 			    loff_t *pos)
43863e2b423SJohn Johansen {
439b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
44018e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
441b7fd2c03SJohn Johansen 
442b7fd2c03SJohn Johansen 	aa_put_ns(ns);
44363e2b423SJohn Johansen 
44463e2b423SJohn Johansen 	return error;
44563e2b423SJohn Johansen }
44663e2b423SJohn Johansen 
44763e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
4486038f373SArnd Bergmann 	.write = profile_load,
4496038f373SArnd Bergmann 	.llseek = default_llseek,
45063e2b423SJohn Johansen };
45163e2b423SJohn Johansen 
45263e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
45363e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
45463e2b423SJohn Johansen 			       size_t size, loff_t *pos)
45563e2b423SJohn Johansen {
456b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
45718e99f19SJohn Johansen 	int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
45818e99f19SJohn Johansen 				  buf, size, pos, ns);
459b7fd2c03SJohn Johansen 	aa_put_ns(ns);
46063e2b423SJohn Johansen 
46163e2b423SJohn Johansen 	return error;
46263e2b423SJohn Johansen }
46363e2b423SJohn Johansen 
46463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
4656038f373SArnd Bergmann 	.write = profile_replace,
4666038f373SArnd Bergmann 	.llseek = default_llseek,
46763e2b423SJohn Johansen };
46863e2b423SJohn Johansen 
469b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */
47063e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
47163e2b423SJohn Johansen 			      size_t size, loff_t *pos)
47263e2b423SJohn Johansen {
4735ac8c355SJohn Johansen 	struct aa_loaddata *data;
474637f688dSJohn Johansen 	struct aa_label *label;
47563e2b423SJohn Johansen 	ssize_t error;
476b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
47763e2b423SJohn Johansen 
478637f688dSJohn Johansen 	label = begin_current_label_crit_section();
4795ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
4805ac8c355SJohn Johansen 	 * below after unpack
4815ac8c355SJohn Johansen 	 */
482637f688dSJohn Johansen 	error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY);
4835ac8c355SJohn Johansen 	if (error)
4845ac8c355SJohn Johansen 		goto out;
4855ac8c355SJohn Johansen 
48663e2b423SJohn Johansen 	/*
48763e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
48863e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
48963e2b423SJohn Johansen 	 */
4905ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
49163e2b423SJohn Johansen 
49263e2b423SJohn Johansen 	error = PTR_ERR(data);
49363e2b423SJohn Johansen 	if (!IS_ERR(data)) {
4945ac8c355SJohn Johansen 		data->data[size] = 0;
495637f688dSJohn Johansen 		error = aa_remove_profiles(ns, label, data->data, size);
4965ac8c355SJohn Johansen 		aa_put_loaddata(data);
49763e2b423SJohn Johansen 	}
4985ac8c355SJohn Johansen  out:
499637f688dSJohn Johansen 	end_current_label_crit_section(label);
500b7fd2c03SJohn Johansen 	aa_put_ns(ns);
50163e2b423SJohn Johansen 	return error;
50263e2b423SJohn Johansen }
50363e2b423SJohn Johansen 
50463e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
5056038f373SArnd Bergmann 	.write = profile_remove,
5066038f373SArnd Bergmann 	.llseek = default_llseek,
50763e2b423SJohn Johansen };
50863e2b423SJohn Johansen 
509d9bf2c26SJohn Johansen struct aa_revision {
510d9bf2c26SJohn Johansen 	struct aa_ns *ns;
511d9bf2c26SJohn Johansen 	long last_read;
512d9bf2c26SJohn Johansen };
513d9bf2c26SJohn Johansen 
514d9bf2c26SJohn Johansen /* revision file hook fn for policy loads */
515d9bf2c26SJohn Johansen static int ns_revision_release(struct inode *inode, struct file *file)
516d9bf2c26SJohn Johansen {
517d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
518d9bf2c26SJohn Johansen 
519d9bf2c26SJohn Johansen 	if (rev) {
520d9bf2c26SJohn Johansen 		aa_put_ns(rev->ns);
521d9bf2c26SJohn Johansen 		kfree(rev);
522d9bf2c26SJohn Johansen 	}
523d9bf2c26SJohn Johansen 
524d9bf2c26SJohn Johansen 	return 0;
525d9bf2c26SJohn Johansen }
526d9bf2c26SJohn Johansen 
527d9bf2c26SJohn Johansen static ssize_t ns_revision_read(struct file *file, char __user *buf,
528d9bf2c26SJohn Johansen 				size_t size, loff_t *ppos)
529d9bf2c26SJohn Johansen {
530d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
531d9bf2c26SJohn Johansen 	char buffer[32];
532d9bf2c26SJohn Johansen 	long last_read;
533d9bf2c26SJohn Johansen 	int avail;
534d9bf2c26SJohn Johansen 
535feb3c766SJohn Johansen 	mutex_lock_nested(&rev->ns->lock, rev->ns->level);
536d9bf2c26SJohn Johansen 	last_read = rev->last_read;
537d9bf2c26SJohn Johansen 	if (last_read == rev->ns->revision) {
538d9bf2c26SJohn Johansen 		mutex_unlock(&rev->ns->lock);
539d9bf2c26SJohn Johansen 		if (file->f_flags & O_NONBLOCK)
540d9bf2c26SJohn Johansen 			return -EAGAIN;
541d9bf2c26SJohn Johansen 		if (wait_event_interruptible(rev->ns->wait,
542d9bf2c26SJohn Johansen 					     last_read !=
543d9bf2c26SJohn Johansen 					     READ_ONCE(rev->ns->revision)))
544d9bf2c26SJohn Johansen 			return -ERESTARTSYS;
545feb3c766SJohn Johansen 		mutex_lock_nested(&rev->ns->lock, rev->ns->level);
546d9bf2c26SJohn Johansen 	}
547d9bf2c26SJohn Johansen 
548d9bf2c26SJohn Johansen 	avail = sprintf(buffer, "%ld\n", rev->ns->revision);
549d9bf2c26SJohn Johansen 	if (*ppos + size > avail) {
550d9bf2c26SJohn Johansen 		rev->last_read = rev->ns->revision;
551d9bf2c26SJohn Johansen 		*ppos = 0;
552d9bf2c26SJohn Johansen 	}
553d9bf2c26SJohn Johansen 	mutex_unlock(&rev->ns->lock);
554d9bf2c26SJohn Johansen 
555d9bf2c26SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, buffer, avail);
556d9bf2c26SJohn Johansen }
557d9bf2c26SJohn Johansen 
558d9bf2c26SJohn Johansen static int ns_revision_open(struct inode *inode, struct file *file)
559d9bf2c26SJohn Johansen {
560d9bf2c26SJohn Johansen 	struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
561d9bf2c26SJohn Johansen 
562d9bf2c26SJohn Johansen 	if (!rev)
563d9bf2c26SJohn Johansen 		return -ENOMEM;
564d9bf2c26SJohn Johansen 
565d9bf2c26SJohn Johansen 	rev->ns = aa_get_ns(inode->i_private);
566d9bf2c26SJohn Johansen 	if (!rev->ns)
567d9bf2c26SJohn Johansen 		rev->ns = aa_get_current_ns();
568d9bf2c26SJohn Johansen 	file->private_data = rev;
569d9bf2c26SJohn Johansen 
570d9bf2c26SJohn Johansen 	return 0;
571d9bf2c26SJohn Johansen }
572d9bf2c26SJohn Johansen 
573d9bf2c26SJohn Johansen static unsigned int ns_revision_poll(struct file *file, poll_table *pt)
574d9bf2c26SJohn Johansen {
575d9bf2c26SJohn Johansen 	struct aa_revision *rev = file->private_data;
576d9bf2c26SJohn Johansen 	unsigned int mask = 0;
577d9bf2c26SJohn Johansen 
578d9bf2c26SJohn Johansen 	if (rev) {
579feb3c766SJohn Johansen 		mutex_lock_nested(&rev->ns->lock, rev->ns->level);
580d9bf2c26SJohn Johansen 		poll_wait(file, &rev->ns->wait, pt);
581d9bf2c26SJohn Johansen 		if (rev->last_read < rev->ns->revision)
582d9bf2c26SJohn Johansen 			mask |= POLLIN | POLLRDNORM;
583d9bf2c26SJohn Johansen 		mutex_unlock(&rev->ns->lock);
584d9bf2c26SJohn Johansen 	}
585d9bf2c26SJohn Johansen 
586d9bf2c26SJohn Johansen 	return mask;
587d9bf2c26SJohn Johansen }
588d9bf2c26SJohn Johansen 
5895d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns)
5905d5182caSJohn Johansen {
5915d5182caSJohn Johansen 	ns->revision++;
592d9bf2c26SJohn Johansen 	wake_up_interruptible(&ns->wait);
5935d5182caSJohn Johansen }
5945d5182caSJohn Johansen 
595d9bf2c26SJohn Johansen static const struct file_operations aa_fs_ns_revision_fops = {
596d9bf2c26SJohn Johansen 	.owner		= THIS_MODULE,
597d9bf2c26SJohn Johansen 	.open		= ns_revision_open,
598d9bf2c26SJohn Johansen 	.poll		= ns_revision_poll,
599d9bf2c26SJohn Johansen 	.read		= ns_revision_read,
600d9bf2c26SJohn Johansen 	.llseek		= generic_file_llseek,
601d9bf2c26SJohn Johansen 	.release	= ns_revision_release,
602d9bf2c26SJohn Johansen };
603d9bf2c26SJohn Johansen 
6044f3b3f2dSJohn Johansen static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
6054f3b3f2dSJohn Johansen 			     const char *match_str, size_t match_len)
6064f3b3f2dSJohn Johansen {
6074f3b3f2dSJohn Johansen 	struct aa_perms tmp;
6084f3b3f2dSJohn Johansen 	struct aa_dfa *dfa;
6094f3b3f2dSJohn Johansen 	unsigned int state = 0;
6104f3b3f2dSJohn Johansen 
611637f688dSJohn Johansen 	if (profile_unconfined(profile))
6124f3b3f2dSJohn Johansen 		return;
6134f3b3f2dSJohn Johansen 	if (profile->file.dfa && *match_str == AA_CLASS_FILE) {
6144f3b3f2dSJohn Johansen 		dfa = profile->file.dfa;
6154f3b3f2dSJohn Johansen 		state = aa_dfa_match_len(dfa, profile->file.start,
6164f3b3f2dSJohn Johansen 					 match_str + 1, match_len - 1);
6174f3b3f2dSJohn Johansen 		tmp = nullperms;
6184f3b3f2dSJohn Johansen 		if (state) {
6194f3b3f2dSJohn Johansen 			struct path_cond cond = { };
6204f3b3f2dSJohn Johansen 
6214f3b3f2dSJohn Johansen 			tmp = aa_compute_fperms(dfa, state, &cond);
6224f3b3f2dSJohn Johansen 		}
6234f3b3f2dSJohn Johansen 	} else if (profile->policy.dfa) {
6244f3b3f2dSJohn Johansen 		if (!PROFILE_MEDIATES_SAFE(profile, *match_str))
6254f3b3f2dSJohn Johansen 			return;	/* no change to current perms */
6264f3b3f2dSJohn Johansen 		dfa = profile->policy.dfa;
6274f3b3f2dSJohn Johansen 		state = aa_dfa_match_len(dfa, profile->policy.start[0],
6284f3b3f2dSJohn Johansen 					 match_str, match_len);
6294f3b3f2dSJohn Johansen 		if (state)
6304f3b3f2dSJohn Johansen 			aa_compute_perms(dfa, state, &tmp);
6314f3b3f2dSJohn Johansen 		else
6324f3b3f2dSJohn Johansen 			tmp = nullperms;
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;
8101dea3b41SJohn Johansen 	char data[0];
8111dea3b41SJohn Johansen };
8121dea3b41SJohn Johansen 
8131dea3b41SJohn Johansen #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction))
8141dea3b41SJohn Johansen /* TODO: replace with per file lock */
8151dea3b41SJohn Johansen static DEFINE_SPINLOCK(multi_transaction_lock);
8161dea3b41SJohn Johansen 
8171dea3b41SJohn Johansen static void multi_transaction_kref(struct kref *kref)
8181dea3b41SJohn Johansen {
8191dea3b41SJohn Johansen 	struct multi_transaction *t;
8201dea3b41SJohn Johansen 
8211dea3b41SJohn Johansen 	t = container_of(kref, struct multi_transaction, count);
8221dea3b41SJohn Johansen 	free_page((unsigned long) t);
8231dea3b41SJohn Johansen }
8241dea3b41SJohn Johansen 
8251dea3b41SJohn Johansen static struct multi_transaction *
8261dea3b41SJohn Johansen get_multi_transaction(struct multi_transaction *t)
8271dea3b41SJohn Johansen {
8281dea3b41SJohn Johansen 	if  (t)
8291dea3b41SJohn Johansen 		kref_get(&(t->count));
8301dea3b41SJohn Johansen 
8311dea3b41SJohn Johansen 	return t;
8321dea3b41SJohn Johansen }
8331dea3b41SJohn Johansen 
8341dea3b41SJohn Johansen static void put_multi_transaction(struct multi_transaction *t)
8351dea3b41SJohn Johansen {
8361dea3b41SJohn Johansen 	if (t)
8371dea3b41SJohn Johansen 		kref_put(&(t->count), multi_transaction_kref);
8381dea3b41SJohn Johansen }
8391dea3b41SJohn Johansen 
8401dea3b41SJohn Johansen /* does not increment @new's count */
8411dea3b41SJohn Johansen static void multi_transaction_set(struct file *file,
8421dea3b41SJohn Johansen 				  struct multi_transaction *new, size_t n)
8431dea3b41SJohn Johansen {
8441dea3b41SJohn Johansen 	struct multi_transaction *old;
8451dea3b41SJohn Johansen 
8461dea3b41SJohn Johansen 	AA_BUG(n > MULTI_TRANSACTION_LIMIT);
8471dea3b41SJohn Johansen 
8481dea3b41SJohn Johansen 	new->size = n;
8491dea3b41SJohn Johansen 	spin_lock(&multi_transaction_lock);
8501dea3b41SJohn Johansen 	old = (struct multi_transaction *) file->private_data;
8511dea3b41SJohn Johansen 	file->private_data = new;
8521dea3b41SJohn Johansen 	spin_unlock(&multi_transaction_lock);
8531dea3b41SJohn Johansen 	put_multi_transaction(old);
8541dea3b41SJohn Johansen }
8551dea3b41SJohn Johansen 
8561dea3b41SJohn Johansen static struct multi_transaction *multi_transaction_new(struct file *file,
8571dea3b41SJohn Johansen 						       const char __user *buf,
8581dea3b41SJohn Johansen 						       size_t size)
8591dea3b41SJohn Johansen {
8601dea3b41SJohn Johansen 	struct multi_transaction *t;
8611dea3b41SJohn Johansen 
8621dea3b41SJohn Johansen 	if (size > MULTI_TRANSACTION_LIMIT - 1)
8631dea3b41SJohn Johansen 		return ERR_PTR(-EFBIG);
8641dea3b41SJohn Johansen 
8651dea3b41SJohn Johansen 	t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
8661dea3b41SJohn Johansen 	if (!t)
8671dea3b41SJohn Johansen 		return ERR_PTR(-ENOMEM);
8681dea3b41SJohn Johansen 	kref_init(&t->count);
8691dea3b41SJohn Johansen 	if (copy_from_user(t->data, buf, size))
8701dea3b41SJohn Johansen 		return ERR_PTR(-EFAULT);
8711dea3b41SJohn Johansen 
8721dea3b41SJohn Johansen 	return t;
8731dea3b41SJohn Johansen }
8741dea3b41SJohn Johansen 
8751dea3b41SJohn Johansen static ssize_t multi_transaction_read(struct file *file, char __user *buf,
8761dea3b41SJohn Johansen 				       size_t size, loff_t *pos)
8771dea3b41SJohn Johansen {
8781dea3b41SJohn Johansen 	struct multi_transaction *t;
8791dea3b41SJohn Johansen 	ssize_t ret;
8801dea3b41SJohn Johansen 
8811dea3b41SJohn Johansen 	spin_lock(&multi_transaction_lock);
8821dea3b41SJohn Johansen 	t = get_multi_transaction(file->private_data);
8831dea3b41SJohn Johansen 	spin_unlock(&multi_transaction_lock);
8841dea3b41SJohn Johansen 	if (!t)
8851dea3b41SJohn Johansen 		return 0;
8861dea3b41SJohn Johansen 
8871dea3b41SJohn Johansen 	ret = simple_read_from_buffer(buf, size, pos, t->data, t->size);
8881dea3b41SJohn Johansen 	put_multi_transaction(t);
8891dea3b41SJohn Johansen 
8901dea3b41SJohn Johansen 	return ret;
8911dea3b41SJohn Johansen }
8921dea3b41SJohn Johansen 
8931dea3b41SJohn Johansen static int multi_transaction_release(struct inode *inode, struct file *file)
8941dea3b41SJohn Johansen {
8951dea3b41SJohn Johansen 	put_multi_transaction(file->private_data);
8961dea3b41SJohn Johansen 
8971dea3b41SJohn Johansen 	return 0;
8981dea3b41SJohn Johansen }
8991dea3b41SJohn Johansen 
900317d9a05SJohn Johansen #define QUERY_CMD_LABEL		"label\0"
901317d9a05SJohn Johansen #define QUERY_CMD_LABEL_LEN	6
9024f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE	"profile\0"
9034f3b3f2dSJohn Johansen #define QUERY_CMD_PROFILE_LEN	8
904317d9a05SJohn Johansen #define QUERY_CMD_LABELALL	"labelall\0"
905317d9a05SJohn Johansen #define QUERY_CMD_LABELALL_LEN	9
906e025be0fSWilliam Hua #define QUERY_CMD_DATA		"data\0"
907e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN	5
908e025be0fSWilliam Hua 
909e025be0fSWilliam Hua /**
910e025be0fSWilliam Hua  * aa_write_access - generic permissions and data query
911e025be0fSWilliam Hua  * @file: pointer to open apparmorfs/access file
912e025be0fSWilliam Hua  * @ubuf: user buffer containing the complete query string (NOT NULL)
913e025be0fSWilliam Hua  * @count: size of ubuf
914e025be0fSWilliam Hua  * @ppos: position in the file (MUST BE ZERO)
915e025be0fSWilliam Hua  *
916e025be0fSWilliam Hua  * Allows for one permissions or data query per open(), write(), and read()
917e025be0fSWilliam Hua  * sequence. The only queries currently supported are label-based queries for
918e025be0fSWilliam Hua  * permissions or data.
919e025be0fSWilliam Hua  *
920e025be0fSWilliam Hua  * For permissions queries, ubuf must begin with "label\0", followed by the
921e025be0fSWilliam Hua  * profile query specific format described in the query_label() function
922e025be0fSWilliam Hua  * documentation.
923e025be0fSWilliam Hua  *
924e025be0fSWilliam Hua  * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
925e025be0fSWilliam Hua  * <LABEL> is the name of the security confinement context and <KEY> is the
926e025be0fSWilliam Hua  * name of the data to retrieve.
927e025be0fSWilliam Hua  *
928e025be0fSWilliam Hua  * Returns: number of bytes written or -errno on failure
929e025be0fSWilliam Hua  */
930e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
931e025be0fSWilliam Hua 			       size_t count, loff_t *ppos)
932e025be0fSWilliam Hua {
9331dea3b41SJohn Johansen 	struct multi_transaction *t;
934e025be0fSWilliam Hua 	ssize_t len;
935e025be0fSWilliam Hua 
936e025be0fSWilliam Hua 	if (*ppos)
937e025be0fSWilliam Hua 		return -ESPIPE;
938e025be0fSWilliam Hua 
9391dea3b41SJohn Johansen 	t = multi_transaction_new(file, ubuf, count);
9401dea3b41SJohn Johansen 	if (IS_ERR(t))
9411dea3b41SJohn Johansen 		return PTR_ERR(t);
942e025be0fSWilliam Hua 
9434f3b3f2dSJohn Johansen 	if (count > QUERY_CMD_PROFILE_LEN &&
9444f3b3f2dSJohn Johansen 	    !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) {
9454f3b3f2dSJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
9464f3b3f2dSJohn Johansen 				  t->data + QUERY_CMD_PROFILE_LEN,
9474f3b3f2dSJohn Johansen 				  count - QUERY_CMD_PROFILE_LEN, true);
948317d9a05SJohn Johansen 	} else if (count > QUERY_CMD_LABEL_LEN &&
949317d9a05SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) {
950317d9a05SJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
951317d9a05SJohn Johansen 				  t->data + QUERY_CMD_LABEL_LEN,
952317d9a05SJohn Johansen 				  count - QUERY_CMD_LABEL_LEN, true);
953317d9a05SJohn Johansen 	} else if (count > QUERY_CMD_LABELALL_LEN &&
954317d9a05SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_LABELALL,
955317d9a05SJohn Johansen 			   QUERY_CMD_LABELALL_LEN)) {
956317d9a05SJohn Johansen 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
957317d9a05SJohn Johansen 				  t->data + QUERY_CMD_LABELALL_LEN,
958317d9a05SJohn Johansen 				  count - QUERY_CMD_LABELALL_LEN, false);
9594f3b3f2dSJohn Johansen 	} else if (count > QUERY_CMD_DATA_LEN &&
9601dea3b41SJohn Johansen 		   !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
9611dea3b41SJohn Johansen 		len = query_data(t->data, MULTI_TRANSACTION_LIMIT,
9621dea3b41SJohn Johansen 				 t->data + QUERY_CMD_DATA_LEN,
963e025be0fSWilliam Hua 				 count - QUERY_CMD_DATA_LEN);
964e025be0fSWilliam Hua 	} else
965e025be0fSWilliam Hua 		len = -EINVAL;
966e025be0fSWilliam Hua 
9671dea3b41SJohn Johansen 	if (len < 0) {
9681dea3b41SJohn Johansen 		put_multi_transaction(t);
969e025be0fSWilliam Hua 		return len;
9701dea3b41SJohn Johansen 	}
971e025be0fSWilliam Hua 
9721dea3b41SJohn Johansen 	multi_transaction_set(file, t, len);
973e025be0fSWilliam Hua 
974e025be0fSWilliam Hua 	return count;
975e025be0fSWilliam Hua }
976e025be0fSWilliam Hua 
977c97204baSJohn Johansen static const struct file_operations aa_sfs_access = {
978e025be0fSWilliam Hua 	.write		= aa_write_access,
9791dea3b41SJohn Johansen 	.read		= multi_transaction_read,
9801dea3b41SJohn Johansen 	.release	= multi_transaction_release,
981e025be0fSWilliam Hua 	.llseek		= generic_file_llseek,
982e025be0fSWilliam Hua };
983e025be0fSWilliam Hua 
984c97204baSJohn Johansen static int aa_sfs_seq_show(struct seq_file *seq, void *v)
985e74abcf3SKees Cook {
986c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file = seq->private;
987e74abcf3SKees Cook 
988e74abcf3SKees Cook 	if (!fs_file)
989e74abcf3SKees Cook 		return 0;
990e74abcf3SKees Cook 
991e74abcf3SKees Cook 	switch (fs_file->v_type) {
992c97204baSJohn Johansen 	case AA_SFS_TYPE_BOOLEAN:
993e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
994e74abcf3SKees Cook 		break;
995c97204baSJohn Johansen 	case AA_SFS_TYPE_STRING:
996a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
997a9bf8e9fSKees Cook 		break;
998c97204baSJohn Johansen 	case AA_SFS_TYPE_U64:
999e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
1000e74abcf3SKees Cook 		break;
1001e74abcf3SKees Cook 	default:
1002e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
1003e74abcf3SKees Cook 		break;
1004e74abcf3SKees Cook 	}
1005e74abcf3SKees Cook 
1006e74abcf3SKees Cook 	return 0;
1007e74abcf3SKees Cook }
1008e74abcf3SKees Cook 
1009c97204baSJohn Johansen static int aa_sfs_seq_open(struct inode *inode, struct file *file)
1010e74abcf3SKees Cook {
1011c97204baSJohn Johansen 	return single_open(file, aa_sfs_seq_show, inode->i_private);
1012e74abcf3SKees Cook }
1013e74abcf3SKees Cook 
1014c97204baSJohn Johansen const struct file_operations aa_sfs_seq_file_ops = {
1015e74abcf3SKees Cook 	.owner		= THIS_MODULE,
1016c97204baSJohn Johansen 	.open		= aa_sfs_seq_open,
1017e74abcf3SKees Cook 	.read		= seq_read,
1018e74abcf3SKees Cook 	.llseek		= seq_lseek,
1019e74abcf3SKees Cook 	.release	= single_release,
1020e74abcf3SKees Cook };
1021e74abcf3SKees Cook 
102252b97de3SJohn Johansen /*
102352b97de3SJohn Johansen  * profile based file operations
102452b97de3SJohn Johansen  *     policy/profiles/XXXX/profiles/ *
102552b97de3SJohn Johansen  */
102652b97de3SJohn Johansen 
102752b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME)						      \
102852b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
102952b97de3SJohn Johansen {									      \
103052b97de3SJohn Johansen 	return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show);    \
103152b97de3SJohn Johansen }									      \
103252b97de3SJohn Johansen 									      \
103352b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = {	      \
103452b97de3SJohn Johansen 	.owner		= THIS_MODULE,					      \
103552b97de3SJohn Johansen 	.open		= seq_profile_ ##NAME ##_open,			      \
103652b97de3SJohn Johansen 	.read		= seq_read,					      \
103752b97de3SJohn Johansen 	.llseek		= seq_lseek,					      \
103852b97de3SJohn Johansen 	.release	= seq_profile_release,				      \
103952b97de3SJohn Johansen }									      \
104052b97de3SJohn Johansen 
104152b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file,
10420d259f04SJohn Johansen 			    int (*show)(struct seq_file *, void *))
10430d259f04SJohn Johansen {
10448399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
10458399588aSJohn Johansen 	int error = single_open(file, show, proxy);
104663e2b423SJohn Johansen 
10470d259f04SJohn Johansen 	if (error) {
10480d259f04SJohn Johansen 		file->private_data = NULL;
10498399588aSJohn Johansen 		aa_put_proxy(proxy);
10500d259f04SJohn Johansen 	}
10510d259f04SJohn Johansen 
10520d259f04SJohn Johansen 	return error;
10530d259f04SJohn Johansen }
10540d259f04SJohn Johansen 
105552b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file)
10560d259f04SJohn Johansen {
10570d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
10580d259f04SJohn Johansen 	if (seq)
10598399588aSJohn Johansen 		aa_put_proxy(seq->private);
10600d259f04SJohn Johansen 	return single_release(inode, file);
10610d259f04SJohn Johansen }
10620d259f04SJohn Johansen 
106352b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v)
10640d259f04SJohn Johansen {
10658399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1066637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1067637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
10680d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
1069637f688dSJohn Johansen 	aa_put_label(label);
10700d259f04SJohn Johansen 
10710d259f04SJohn Johansen 	return 0;
10720d259f04SJohn Johansen }
10730d259f04SJohn Johansen 
107452b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v)
10750d259f04SJohn Johansen {
10768399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1077637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1078637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
10790d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
1080637f688dSJohn Johansen 	aa_put_label(label);
10810d259f04SJohn Johansen 
10820d259f04SJohn Johansen 	return 0;
10830d259f04SJohn Johansen }
10840d259f04SJohn Johansen 
108552b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v)
1086556d0be7SJohn Johansen {
10878399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1088637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1089637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
1090556d0be7SJohn Johansen 	if (profile->attach)
1091556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
1092556d0be7SJohn Johansen 	else if (profile->xmatch)
1093556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
1094556d0be7SJohn Johansen 	else
1095556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
1096637f688dSJohn Johansen 	aa_put_label(label);
1097556d0be7SJohn Johansen 
1098556d0be7SJohn Johansen 	return 0;
1099556d0be7SJohn Johansen }
1100556d0be7SJohn Johansen 
110152b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v)
1102f8eb8a13SJohn Johansen {
11038399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
1104637f688dSJohn Johansen 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1105637f688dSJohn Johansen 	struct aa_profile *profile = labels_profile(label);
1106f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
1107f8eb8a13SJohn Johansen 
1108f8eb8a13SJohn Johansen 	if (profile->hash) {
1109f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
1110f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
111147dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
1112f8eb8a13SJohn Johansen 	}
1113637f688dSJohn Johansen 	aa_put_label(label);
1114f8eb8a13SJohn Johansen 
1115f8eb8a13SJohn Johansen 	return 0;
1116f8eb8a13SJohn Johansen }
1117f8eb8a13SJohn Johansen 
111852b97de3SJohn Johansen SEQ_PROFILE_FOPS(name);
111952b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode);
112052b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach);
112152b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash);
1122f8eb8a13SJohn Johansen 
112364c86970SJohn Johansen /*
112464c86970SJohn Johansen  * namespace based files
112564c86970SJohn Johansen  *     several root files and
112664c86970SJohn Johansen  *     policy/ *
112764c86970SJohn Johansen  */
1128f8eb8a13SJohn Johansen 
112964c86970SJohn Johansen #define SEQ_NS_FOPS(NAME)						      \
113064c86970SJohn Johansen static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file)     \
113164c86970SJohn Johansen {									      \
113264c86970SJohn Johansen 	return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private);   \
113364c86970SJohn Johansen }									      \
113464c86970SJohn Johansen 									      \
113564c86970SJohn Johansen static const struct file_operations seq_ns_ ##NAME ##_fops = {	      \
113664c86970SJohn Johansen 	.owner		= THIS_MODULE,					      \
113764c86970SJohn Johansen 	.open		= seq_ns_ ##NAME ##_open,			      \
113864c86970SJohn Johansen 	.read		= seq_read,					      \
113964c86970SJohn Johansen 	.llseek		= seq_lseek,					      \
114064c86970SJohn Johansen 	.release	= single_release,				      \
114164c86970SJohn Johansen }									      \
11423e3e5695SJohn Johansen 
114340cde7fcSJohn Johansen static int seq_ns_stacked_show(struct seq_file *seq, void *v)
114440cde7fcSJohn Johansen {
114540cde7fcSJohn Johansen 	struct aa_label *label;
114640cde7fcSJohn Johansen 
114740cde7fcSJohn Johansen 	label = begin_current_label_crit_section();
114840cde7fcSJohn Johansen 	seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no");
114940cde7fcSJohn Johansen 	end_current_label_crit_section(label);
115040cde7fcSJohn Johansen 
115140cde7fcSJohn Johansen 	return 0;
115240cde7fcSJohn Johansen }
115340cde7fcSJohn Johansen 
115440cde7fcSJohn Johansen static int seq_ns_nsstacked_show(struct seq_file *seq, void *v)
115540cde7fcSJohn Johansen {
115640cde7fcSJohn Johansen 	struct aa_label *label;
115740cde7fcSJohn Johansen 	struct aa_profile *profile;
115840cde7fcSJohn Johansen 	struct label_it it;
115940cde7fcSJohn Johansen 	int count = 1;
116040cde7fcSJohn Johansen 
116140cde7fcSJohn Johansen 	label = begin_current_label_crit_section();
116240cde7fcSJohn Johansen 
116340cde7fcSJohn Johansen 	if (label->size > 1) {
116440cde7fcSJohn Johansen 		label_for_each(it, label, profile)
116540cde7fcSJohn Johansen 			if (profile->ns != labels_ns(label)) {
116640cde7fcSJohn Johansen 				count++;
116740cde7fcSJohn Johansen 				break;
116840cde7fcSJohn Johansen 			}
116940cde7fcSJohn Johansen 	}
117040cde7fcSJohn Johansen 
117140cde7fcSJohn Johansen 	seq_printf(seq, "%s\n", count > 1 ? "yes" : "no");
117240cde7fcSJohn Johansen 	end_current_label_crit_section(label);
117340cde7fcSJohn Johansen 
117440cde7fcSJohn Johansen 	return 0;
117540cde7fcSJohn Johansen }
117640cde7fcSJohn Johansen 
117764c86970SJohn Johansen static int seq_ns_level_show(struct seq_file *seq, void *v)
1178a71ada30SJohn Johansen {
1179637f688dSJohn Johansen 	struct aa_label *label;
1180a71ada30SJohn Johansen 
1181637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1182637f688dSJohn Johansen 	seq_printf(seq, "%d\n", labels_ns(label)->level);
1183637f688dSJohn Johansen 	end_current_label_crit_section(label);
1184a71ada30SJohn Johansen 
1185a71ada30SJohn Johansen 	return 0;
1186a71ada30SJohn Johansen }
1187a71ada30SJohn Johansen 
118864c86970SJohn Johansen static int seq_ns_name_show(struct seq_file *seq, void *v)
11893e3e5695SJohn Johansen {
1190637f688dSJohn Johansen 	struct aa_label *label = begin_current_label_crit_section();
1191040d9e2bSJohn Johansen 	seq_printf(seq, "%s\n", labels_ns(label)->base.name);
1192637f688dSJohn Johansen 	end_current_label_crit_section(label);
11933e3e5695SJohn Johansen 
11943e3e5695SJohn Johansen 	return 0;
11953e3e5695SJohn Johansen }
11963e3e5695SJohn Johansen 
119740cde7fcSJohn Johansen SEQ_NS_FOPS(stacked);
119840cde7fcSJohn Johansen SEQ_NS_FOPS(nsstacked);
119964c86970SJohn Johansen SEQ_NS_FOPS(level);
120064c86970SJohn Johansen SEQ_NS_FOPS(name);
12013e3e5695SJohn Johansen 
12025d5182caSJohn Johansen 
12035d5182caSJohn Johansen /* policy/raw_data/ * file ops */
12045d5182caSJohn Johansen 
12055d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME)						      \
12065d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
12075d5182caSJohn Johansen {									      \
12085d5182caSJohn Johansen 	return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show);    \
12095d5182caSJohn Johansen }									      \
12105d5182caSJohn Johansen 									      \
12115d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = {	      \
12125d5182caSJohn Johansen 	.owner		= THIS_MODULE,					      \
12135d5182caSJohn Johansen 	.open		= seq_rawdata_ ##NAME ##_open,			      \
12145d5182caSJohn Johansen 	.read		= seq_read,					      \
12155d5182caSJohn Johansen 	.llseek		= seq_lseek,					      \
12165d5182caSJohn Johansen 	.release	= seq_rawdata_release,				      \
12175d5182caSJohn Johansen }									      \
12185d5182caSJohn Johansen 
12195d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file,
12205d5182caSJohn Johansen 			    int (*show)(struct seq_file *, void *))
12215ac8c355SJohn Johansen {
12225d5182caSJohn Johansen 	struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
12235d5182caSJohn Johansen 	int error;
12245d5182caSJohn Johansen 
12255d5182caSJohn Johansen 	if (!data)
12265d5182caSJohn Johansen 		/* lost race this ent is being reaped */
12275d5182caSJohn Johansen 		return -ENOENT;
12285d5182caSJohn Johansen 
12295d5182caSJohn Johansen 	error = single_open(file, show, data);
12305d5182caSJohn Johansen 	if (error) {
12315d5182caSJohn Johansen 		AA_BUG(file->private_data &&
12325d5182caSJohn Johansen 		       ((struct seq_file *)file->private_data)->private);
12335d5182caSJohn Johansen 		aa_put_loaddata(data);
12345d5182caSJohn Johansen 	}
12355d5182caSJohn Johansen 
12365d5182caSJohn Johansen 	return error;
12375d5182caSJohn Johansen }
12385d5182caSJohn Johansen 
12395d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file)
12405d5182caSJohn Johansen {
12415d5182caSJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
12425d5182caSJohn Johansen 
12435d5182caSJohn Johansen 	if (seq)
12445d5182caSJohn Johansen 		aa_put_loaddata(seq->private);
12455d5182caSJohn Johansen 
12465d5182caSJohn Johansen 	return single_release(inode, file);
12475d5182caSJohn Johansen }
12485d5182caSJohn Johansen 
12495d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
12505d5182caSJohn Johansen {
12515d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12525d5182caSJohn Johansen 
12535d5182caSJohn Johansen 	seq_printf(seq, "v%d\n", data->abi);
12545ac8c355SJohn Johansen 
12555ac8c355SJohn Johansen 	return 0;
12565ac8c355SJohn Johansen }
12575ac8c355SJohn Johansen 
12585d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
12595ac8c355SJohn Johansen {
12605d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12615ac8c355SJohn Johansen 
12625d5182caSJohn Johansen 	seq_printf(seq, "%ld\n", data->revision);
12635ac8c355SJohn Johansen 
12645ac8c355SJohn Johansen 	return 0;
12655ac8c355SJohn Johansen }
12665ac8c355SJohn Johansen 
12675d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
12685ac8c355SJohn Johansen {
12695d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
12705ac8c355SJohn Johansen 	unsigned int i, size = aa_hash_size();
12715ac8c355SJohn Johansen 
12725d5182caSJohn Johansen 	if (data->hash) {
12735ac8c355SJohn Johansen 		for (i = 0; i < size; i++)
12745d5182caSJohn Johansen 			seq_printf(seq, "%.2x", data->hash[i]);
127547dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
12765ac8c355SJohn Johansen 	}
12775ac8c355SJohn Johansen 
12785ac8c355SJohn Johansen 	return 0;
12795ac8c355SJohn Johansen }
12805ac8c355SJohn Johansen 
12815d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi);
12825d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision);
12835d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash);
12845ac8c355SJohn Johansen 
12855ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
12865ac8c355SJohn Johansen 			    loff_t *ppos)
12875ac8c355SJohn Johansen {
12885ac8c355SJohn Johansen 	struct aa_loaddata *rawdata = file->private_data;
12895ac8c355SJohn Johansen 
12905ac8c355SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, rawdata->data,
12915ac8c355SJohn Johansen 				       rawdata->size);
12925ac8c355SJohn Johansen }
12935ac8c355SJohn Johansen 
12945d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file)
12955ac8c355SJohn Johansen {
12965d5182caSJohn Johansen 	aa_put_loaddata(file->private_data);
12975ac8c355SJohn Johansen 
12985ac8c355SJohn Johansen 	return 0;
12995ac8c355SJohn Johansen }
13005ac8c355SJohn Johansen 
13015d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file)
13025d5182caSJohn Johansen {
13035d5182caSJohn Johansen 	if (!policy_view_capable(NULL))
13045d5182caSJohn Johansen 		return -EACCES;
13055d5182caSJohn Johansen 	file->private_data = __aa_get_loaddata(inode->i_private);
13065d5182caSJohn Johansen 	if (!file->private_data)
13075d5182caSJohn Johansen 		/* lost race: this entry is being reaped */
13085d5182caSJohn Johansen 		return -ENOENT;
13095d5182caSJohn Johansen 
13105d5182caSJohn Johansen 	return 0;
13115d5182caSJohn Johansen }
13125d5182caSJohn Johansen 
13135d5182caSJohn Johansen static const struct file_operations rawdata_fops = {
13145ac8c355SJohn Johansen 	.open = rawdata_open,
13155ac8c355SJohn Johansen 	.read = rawdata_read,
13165ac8c355SJohn Johansen 	.llseek = generic_file_llseek,
13175ac8c355SJohn Johansen 	.release = rawdata_release,
13185ac8c355SJohn Johansen };
13195ac8c355SJohn Johansen 
13205d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata)
13215d5182caSJohn Johansen {
13225d5182caSJohn Johansen 	int i;
13235d5182caSJohn Johansen 
13245d5182caSJohn Johansen 	for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
13255d5182caSJohn Johansen 		if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
13265d5182caSJohn Johansen 			/* no refcounts on i_private */
1327c961ee5fSJohn Johansen 			aafs_remove(rawdata->dents[i]);
13285d5182caSJohn Johansen 			rawdata->dents[i] = NULL;
13295d5182caSJohn Johansen 		}
13305d5182caSJohn Johansen 	}
13315d5182caSJohn Johansen }
13325d5182caSJohn Johansen 
13335d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
13345d5182caSJohn Johansen {
13355d5182caSJohn Johansen 	AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
13365d5182caSJohn Johansen 
13375d5182caSJohn Johansen 	if (rawdata->ns) {
13385d5182caSJohn Johansen 		remove_rawdata_dents(rawdata);
13395d5182caSJohn Johansen 		list_del_init(&rawdata->list);
13405d5182caSJohn Johansen 		aa_put_ns(rawdata->ns);
13415d5182caSJohn Johansen 		rawdata->ns = NULL;
13425d5182caSJohn Johansen 	}
13435d5182caSJohn Johansen }
13445d5182caSJohn Johansen 
13455d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
13465d5182caSJohn Johansen {
13475d5182caSJohn Johansen 	struct dentry *dent, *dir;
13485d5182caSJohn Johansen 
13495d5182caSJohn Johansen 	AA_BUG(!ns);
13505d5182caSJohn Johansen 	AA_BUG(!rawdata);
13515d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
13525d5182caSJohn Johansen 	AA_BUG(!ns_subdata_dir(ns));
13535d5182caSJohn Johansen 
13545d5182caSJohn Johansen 	/*
13555d5182caSJohn Johansen 	 * just use ns revision dir was originally created at. This is
13565d5182caSJohn Johansen 	 * under ns->lock and if load is successful revision will be
13575d5182caSJohn Johansen 	 * bumped and is guaranteed to be unique
13585d5182caSJohn Johansen 	 */
13595d5182caSJohn Johansen 	rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
13605d5182caSJohn Johansen 	if (!rawdata->name)
13615d5182caSJohn Johansen 		return -ENOMEM;
13625d5182caSJohn Johansen 
1363c961ee5fSJohn Johansen 	dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
13645d5182caSJohn Johansen 	if (IS_ERR(dir))
13655d5182caSJohn Johansen 		/* ->name freed when rawdata freed */
13665d5182caSJohn Johansen 		return PTR_ERR(dir);
13675d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DIR] = dir;
13685d5182caSJohn Johansen 
1369c961ee5fSJohn Johansen 	dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
13705d5182caSJohn Johansen 				      &seq_rawdata_abi_fops);
13715d5182caSJohn Johansen 	if (IS_ERR(dent))
13725d5182caSJohn Johansen 		goto fail;
13735d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_ABI] = dent;
13745d5182caSJohn Johansen 
1375c961ee5fSJohn Johansen 	dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
13765d5182caSJohn Johansen 				      &seq_rawdata_revision_fops);
13775d5182caSJohn Johansen 	if (IS_ERR(dent))
13785d5182caSJohn Johansen 		goto fail;
13795d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
13805d5182caSJohn Johansen 
13815d5182caSJohn Johansen 	if (aa_g_hash_policy) {
1382c961ee5fSJohn Johansen 		dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
13835d5182caSJohn Johansen 					      rawdata, &seq_rawdata_hash_fops);
13845d5182caSJohn Johansen 		if (IS_ERR(dent))
13855d5182caSJohn Johansen 			goto fail;
13865d5182caSJohn Johansen 		rawdata->dents[AAFS_LOADDATA_HASH] = dent;
13875d5182caSJohn Johansen 	}
13885d5182caSJohn Johansen 
1389c961ee5fSJohn Johansen 	dent = aafs_create_file("raw_data", S_IFREG | 0444,
13905d5182caSJohn Johansen 				      dir, rawdata, &rawdata_fops);
13915d5182caSJohn Johansen 	if (IS_ERR(dent))
13925d5182caSJohn Johansen 		goto fail;
13935d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DATA] = dent;
13945d5182caSJohn Johansen 	d_inode(dent)->i_size = rawdata->size;
13955d5182caSJohn Johansen 
13965d5182caSJohn Johansen 	rawdata->ns = aa_get_ns(ns);
13975d5182caSJohn Johansen 	list_add(&rawdata->list, &ns->rawdata_list);
13985d5182caSJohn Johansen 	/* no refcount on inode rawdata */
13995d5182caSJohn Johansen 
14005d5182caSJohn Johansen 	return 0;
14015d5182caSJohn Johansen 
14025d5182caSJohn Johansen fail:
14035d5182caSJohn Johansen 	remove_rawdata_dents(rawdata);
14045d5182caSJohn Johansen 
14055d5182caSJohn Johansen 	return PTR_ERR(dent);
14065d5182caSJohn Johansen }
14075d5182caSJohn Johansen 
14080d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
1409c97204baSJohn Johansen 
1410c97204baSJohn Johansen /**
1411c97204baSJohn Johansen  *
1412c97204baSJohn Johansen  * Requires: @profile->ns->lock held
1413c97204baSJohn Johansen  */
1414c97204baSJohn Johansen void __aafs_profile_rmdir(struct aa_profile *profile)
14150d259f04SJohn Johansen {
14160d259f04SJohn Johansen 	struct aa_profile *child;
14170d259f04SJohn Johansen 	int i;
14180d259f04SJohn Johansen 
14190d259f04SJohn Johansen 	if (!profile)
14200d259f04SJohn Johansen 		return;
14210d259f04SJohn Johansen 
14220d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
1423c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
14240d259f04SJohn Johansen 
14250d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
14268399588aSJohn Johansen 		struct aa_proxy *proxy;
14270d259f04SJohn Johansen 		if (!profile->dents[i])
14280d259f04SJohn Johansen 			continue;
14290d259f04SJohn Johansen 
14308399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
1431c961ee5fSJohn Johansen 		aafs_remove(profile->dents[i]);
14328399588aSJohn Johansen 		aa_put_proxy(proxy);
14330d259f04SJohn Johansen 		profile->dents[i] = NULL;
14340d259f04SJohn Johansen 	}
14350d259f04SJohn Johansen }
14360d259f04SJohn Johansen 
1437c97204baSJohn Johansen /**
1438c97204baSJohn Johansen  *
1439c97204baSJohn Johansen  * Requires: @old->ns->lock held
1440c97204baSJohn Johansen  */
1441c97204baSJohn Johansen void __aafs_profile_migrate_dents(struct aa_profile *old,
14420d259f04SJohn Johansen 				  struct aa_profile *new)
14430d259f04SJohn Johansen {
14440d259f04SJohn Johansen 	int i;
14450d259f04SJohn Johansen 
1446cbf2d0e1SJohn Johansen 	AA_BUG(!old);
1447cbf2d0e1SJohn Johansen 	AA_BUG(!new);
1448cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock));
1449cbf2d0e1SJohn Johansen 
14500d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
14510d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
1452d671e890SJohn Johansen 		if (new->dents[i])
1453078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
14540d259f04SJohn Johansen 		old->dents[i] = NULL;
14550d259f04SJohn Johansen 	}
14560d259f04SJohn Johansen }
14570d259f04SJohn Johansen 
14580d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
14590d259f04SJohn Johansen 					  struct aa_profile *profile,
14600d259f04SJohn Johansen 					  const struct file_operations *fops)
14610d259f04SJohn Johansen {
1462637f688dSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
14630d259f04SJohn Johansen 	struct dentry *dent;
14640d259f04SJohn Johansen 
1465c961ee5fSJohn Johansen 	dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
14660d259f04SJohn Johansen 	if (IS_ERR(dent))
14678399588aSJohn Johansen 		aa_put_proxy(proxy);
14680d259f04SJohn Johansen 
14690d259f04SJohn Johansen 	return dent;
14700d259f04SJohn Johansen }
14710d259f04SJohn Johansen 
14725d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile)
14735d5182caSJohn Johansen {
14745d5182caSJohn Johansen 	int depth = 0;
14755d5182caSJohn Johansen 
14765d5182caSJohn Johansen 	rcu_read_lock();
14775d5182caSJohn Johansen 	for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
14785d5182caSJohn Johansen 		depth++;
14795d5182caSJohn Johansen 	rcu_read_unlock();
14805d5182caSJohn Johansen 
14815d5182caSJohn Johansen 	return depth;
14825d5182caSJohn Johansen }
14835d5182caSJohn Johansen 
14845d5182caSJohn Johansen static int gen_symlink_name(char *buffer, size_t bsize, int depth,
14855d5182caSJohn Johansen 			    const char *dirname, const char *fname)
14865d5182caSJohn Johansen {
14875d5182caSJohn Johansen 	int error;
14885d5182caSJohn Johansen 
14895d5182caSJohn Johansen 	for (; depth > 0; depth--) {
14905d5182caSJohn Johansen 		if (bsize < 7)
14915d5182caSJohn Johansen 			return -ENAMETOOLONG;
14925d5182caSJohn Johansen 		strcpy(buffer, "../../");
14935d5182caSJohn Johansen 		buffer += 6;
14945d5182caSJohn Johansen 		bsize -= 6;
14955d5182caSJohn Johansen 	}
14965d5182caSJohn Johansen 
14975d5182caSJohn Johansen 	error = snprintf(buffer, bsize, "raw_data/%s/%s", dirname, fname);
14985d5182caSJohn Johansen 	if (error >= bsize || error < 0)
14995d5182caSJohn Johansen 		return -ENAMETOOLONG;
15005d5182caSJohn Johansen 
15015d5182caSJohn Johansen 	return 0;
15025d5182caSJohn Johansen }
15035d5182caSJohn Johansen 
15045d5182caSJohn Johansen /*
15055d5182caSJohn Johansen  * Requires: @profile->ns->lock held
15065d5182caSJohn Johansen  */
1507c97204baSJohn Johansen int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
15080d259f04SJohn Johansen {
15090d259f04SJohn Johansen 	struct aa_profile *child;
15100d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
15110d259f04SJohn Johansen 	int error;
15120d259f04SJohn Johansen 
1513cbf2d0e1SJohn Johansen 	AA_BUG(!profile);
1514cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock));
1515cbf2d0e1SJohn Johansen 
15160d259f04SJohn Johansen 	if (!parent) {
15170d259f04SJohn Johansen 		struct aa_profile *p;
15180d259f04SJohn Johansen 		p = aa_deref_parent(profile);
15190d259f04SJohn Johansen 		dent = prof_dir(p);
15200d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
1521c961ee5fSJohn Johansen 		dent = aafs_create_dir("profiles", dent);
15220d259f04SJohn Johansen 		if (IS_ERR(dent))
15230d259f04SJohn Johansen 			goto fail;
15240d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
15250d259f04SJohn Johansen 	}
15260d259f04SJohn Johansen 
15270d259f04SJohn Johansen 	if (!profile->dirname) {
15280d259f04SJohn Johansen 		int len, id_len;
15290d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
15300d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
15310d259f04SJohn Johansen 
15320d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
1533ffac1de6SDan Carpenter 		if (!profile->dirname) {
1534ffac1de6SDan Carpenter 			error = -ENOMEM;
1535ffac1de6SDan Carpenter 			goto fail2;
1536ffac1de6SDan Carpenter 		}
15370d259f04SJohn Johansen 
15380d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
15390d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
15400d259f04SJohn Johansen 	}
15410d259f04SJohn Johansen 
1542c961ee5fSJohn Johansen 	dent = aafs_create_dir(profile->dirname, parent);
15430d259f04SJohn Johansen 	if (IS_ERR(dent))
15440d259f04SJohn Johansen 		goto fail;
15450d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
15460d259f04SJohn Johansen 
154752b97de3SJohn Johansen 	dent = create_profile_file(dir, "name", profile,
154852b97de3SJohn Johansen 				   &seq_profile_name_fops);
15490d259f04SJohn Johansen 	if (IS_ERR(dent))
15500d259f04SJohn Johansen 		goto fail;
15510d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
15520d259f04SJohn Johansen 
155352b97de3SJohn Johansen 	dent = create_profile_file(dir, "mode", profile,
155452b97de3SJohn Johansen 				   &seq_profile_mode_fops);
15550d259f04SJohn Johansen 	if (IS_ERR(dent))
15560d259f04SJohn Johansen 		goto fail;
15570d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
15580d259f04SJohn Johansen 
1559556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
156052b97de3SJohn Johansen 				   &seq_profile_attach_fops);
1561556d0be7SJohn Johansen 	if (IS_ERR(dent))
1562556d0be7SJohn Johansen 		goto fail;
1563556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
1564556d0be7SJohn Johansen 
1565f8eb8a13SJohn Johansen 	if (profile->hash) {
1566f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
156752b97de3SJohn Johansen 					   &seq_profile_hash_fops);
1568f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
1569f8eb8a13SJohn Johansen 			goto fail;
1570f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
1571f8eb8a13SJohn Johansen 	}
1572f8eb8a13SJohn Johansen 
15735ac8c355SJohn Johansen 	if (profile->rawdata) {
15745d5182caSJohn Johansen 		char target[64];
15755d5182caSJohn Johansen 		int depth = profile_depth(profile);
15765d5182caSJohn Johansen 
15775d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
15785d5182caSJohn Johansen 					 profile->rawdata->name, "sha1");
15795d5182caSJohn Johansen 		if (error < 0)
15805d5182caSJohn Johansen 			goto fail2;
1581c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_sha1", dir, target, NULL);
15825ac8c355SJohn Johansen 		if (IS_ERR(dent))
15835ac8c355SJohn Johansen 			goto fail;
15845ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
15855ac8c355SJohn Johansen 
15865d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
15875d5182caSJohn Johansen 					 profile->rawdata->name, "abi");
15885d5182caSJohn Johansen 		if (error < 0)
15895d5182caSJohn Johansen 			goto fail2;
1590c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_abi", dir, target, NULL);
15915ac8c355SJohn Johansen 		if (IS_ERR(dent))
15925ac8c355SJohn Johansen 			goto fail;
15935ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
15945ac8c355SJohn Johansen 
15955d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
15965d5182caSJohn Johansen 					 profile->rawdata->name, "raw_data");
15975d5182caSJohn Johansen 		if (error < 0)
15985d5182caSJohn Johansen 			goto fail2;
1599c961ee5fSJohn Johansen 		dent = aafs_create_symlink("raw_data", dir, target, NULL);
16005ac8c355SJohn Johansen 		if (IS_ERR(dent))
16015ac8c355SJohn Johansen 			goto fail;
16025ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
16035ac8c355SJohn Johansen 	}
16045ac8c355SJohn Johansen 
16050d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
1606c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, prof_child_dir(profile));
16070d259f04SJohn Johansen 		if (error)
16080d259f04SJohn Johansen 			goto fail2;
16090d259f04SJohn Johansen 	}
16100d259f04SJohn Johansen 
16110d259f04SJohn Johansen 	return 0;
16120d259f04SJohn Johansen 
16130d259f04SJohn Johansen fail:
16140d259f04SJohn Johansen 	error = PTR_ERR(dent);
16150d259f04SJohn Johansen 
16160d259f04SJohn Johansen fail2:
1617c97204baSJohn Johansen 	__aafs_profile_rmdir(profile);
16180d259f04SJohn Johansen 
16190d259f04SJohn Johansen 	return error;
16200d259f04SJohn Johansen }
16210d259f04SJohn Johansen 
16224ae47f33SJohn Johansen static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode)
16234ae47f33SJohn Johansen {
16244ae47f33SJohn Johansen 	struct aa_ns *ns, *parent;
16254ae47f33SJohn Johansen 	/* TODO: improve permission check */
1626637f688dSJohn Johansen 	struct aa_label *label;
1627637f688dSJohn Johansen 	int error;
1628637f688dSJohn Johansen 
1629637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1630637f688dSJohn Johansen 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1631637f688dSJohn Johansen 	end_current_label_crit_section(label);
16324ae47f33SJohn Johansen 	if (error)
16334ae47f33SJohn Johansen 		return error;
16344ae47f33SJohn Johansen 
16354ae47f33SJohn Johansen 	parent = aa_get_ns(dir->i_private);
16364ae47f33SJohn Johansen 	AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
16374ae47f33SJohn Johansen 
16384ae47f33SJohn Johansen 	/* we have to unlock and then relock to get locking order right
16394ae47f33SJohn Johansen 	 * for pin_fs
16404ae47f33SJohn Johansen 	 */
16414ae47f33SJohn Johansen 	inode_unlock(dir);
16424ae47f33SJohn Johansen 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
1643feb3c766SJohn Johansen 	mutex_lock_nested(&parent->lock, parent->level);
16444ae47f33SJohn Johansen 	inode_lock_nested(dir, I_MUTEX_PARENT);
16454ae47f33SJohn Johansen 	if (error)
16464ae47f33SJohn Johansen 		goto out;
16474ae47f33SJohn Johansen 
16484ae47f33SJohn Johansen 	error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR,  NULL,
16494ae47f33SJohn Johansen 				     NULL, NULL, NULL);
16504ae47f33SJohn Johansen 	if (error)
16514ae47f33SJohn Johansen 		goto out_pin;
16524ae47f33SJohn Johansen 
16534ae47f33SJohn Johansen 	ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name),
16544ae47f33SJohn Johansen 				    dentry);
16554ae47f33SJohn Johansen 	if (IS_ERR(ns)) {
16564ae47f33SJohn Johansen 		error = PTR_ERR(ns);
16574ae47f33SJohn Johansen 		ns = NULL;
16584ae47f33SJohn Johansen 	}
16594ae47f33SJohn Johansen 
16604ae47f33SJohn Johansen 	aa_put_ns(ns);		/* list ref remains */
16614ae47f33SJohn Johansen out_pin:
16624ae47f33SJohn Johansen 	if (error)
16634ae47f33SJohn Johansen 		simple_release_fs(&aafs_mnt, &aafs_count);
16644ae47f33SJohn Johansen out:
16654ae47f33SJohn Johansen 	mutex_unlock(&parent->lock);
16664ae47f33SJohn Johansen 	aa_put_ns(parent);
16674ae47f33SJohn Johansen 
16684ae47f33SJohn Johansen 	return error;
16694ae47f33SJohn Johansen }
16704ae47f33SJohn Johansen 
16714ae47f33SJohn Johansen static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
16724ae47f33SJohn Johansen {
16734ae47f33SJohn Johansen 	struct aa_ns *ns, *parent;
16744ae47f33SJohn Johansen 	/* TODO: improve permission check */
1675637f688dSJohn Johansen 	struct aa_label *label;
1676637f688dSJohn Johansen 	int error;
1677637f688dSJohn Johansen 
1678637f688dSJohn Johansen 	label = begin_current_label_crit_section();
1679637f688dSJohn Johansen 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1680637f688dSJohn Johansen 	end_current_label_crit_section(label);
16814ae47f33SJohn Johansen 	if (error)
16824ae47f33SJohn Johansen 		return error;
16834ae47f33SJohn Johansen 
16844ae47f33SJohn Johansen 	 parent = aa_get_ns(dir->i_private);
16854ae47f33SJohn Johansen 	/* rmdir calls the generic securityfs functions to remove files
16864ae47f33SJohn Johansen 	 * from the apparmor dir. It is up to the apparmor ns locking
16874ae47f33SJohn Johansen 	 * to avoid races.
16884ae47f33SJohn Johansen 	 */
16894ae47f33SJohn Johansen 	inode_unlock(dir);
16904ae47f33SJohn Johansen 	inode_unlock(dentry->d_inode);
16914ae47f33SJohn Johansen 
1692feb3c766SJohn Johansen 	mutex_lock_nested(&parent->lock, parent->level);
16934ae47f33SJohn Johansen 	ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name,
16944ae47f33SJohn Johansen 				     dentry->d_name.len));
16954ae47f33SJohn Johansen 	if (!ns) {
16964ae47f33SJohn Johansen 		error = -ENOENT;
16974ae47f33SJohn Johansen 		goto out;
16984ae47f33SJohn Johansen 	}
16994ae47f33SJohn Johansen 	AA_BUG(ns_dir(ns) != dentry);
17004ae47f33SJohn Johansen 
17014ae47f33SJohn Johansen 	__aa_remove_ns(ns);
17024ae47f33SJohn Johansen 	aa_put_ns(ns);
17034ae47f33SJohn Johansen 
17044ae47f33SJohn Johansen out:
17054ae47f33SJohn Johansen 	mutex_unlock(&parent->lock);
17064ae47f33SJohn Johansen 	inode_lock_nested(dir, I_MUTEX_PARENT);
17074ae47f33SJohn Johansen 	inode_lock(dentry->d_inode);
17084ae47f33SJohn Johansen 	aa_put_ns(parent);
17094ae47f33SJohn Johansen 
17104ae47f33SJohn Johansen 	return error;
17114ae47f33SJohn Johansen }
17124ae47f33SJohn Johansen 
17134ae47f33SJohn Johansen static const struct inode_operations ns_dir_inode_operations = {
17144ae47f33SJohn Johansen 	.lookup		= simple_lookup,
17154ae47f33SJohn Johansen 	.mkdir		= ns_mkdir_op,
17164ae47f33SJohn Johansen 	.rmdir		= ns_rmdir_op,
17174ae47f33SJohn Johansen };
17184ae47f33SJohn Johansen 
17195d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
17205d5182caSJohn Johansen {
17215d5182caSJohn Johansen 	struct aa_loaddata *ent, *tmp;
17225d5182caSJohn Johansen 
17235d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
17245d5182caSJohn Johansen 
17255d5182caSJohn Johansen 	list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
17265d5182caSJohn Johansen 		__aa_fs_remove_rawdata(ent);
17275d5182caSJohn Johansen }
17285d5182caSJohn Johansen 
1729c97204baSJohn Johansen /**
1730c97204baSJohn Johansen  *
1731c97204baSJohn Johansen  * Requires: @ns->lock held
1732c97204baSJohn Johansen  */
1733c97204baSJohn Johansen void __aafs_ns_rmdir(struct aa_ns *ns)
17340d259f04SJohn Johansen {
173598849dffSJohn Johansen 	struct aa_ns *sub;
17360d259f04SJohn Johansen 	struct aa_profile *child;
17370d259f04SJohn Johansen 	int i;
17380d259f04SJohn Johansen 
17390d259f04SJohn Johansen 	if (!ns)
17400d259f04SJohn Johansen 		return;
1741cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
17420d259f04SJohn Johansen 
17430d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
1744c97204baSJohn Johansen 		__aafs_profile_rmdir(child);
17450d259f04SJohn Johansen 
17460d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
1747feb3c766SJohn Johansen 		mutex_lock_nested(&sub->lock, sub->level);
1748c97204baSJohn Johansen 		__aafs_ns_rmdir(sub);
17490d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
17500d259f04SJohn Johansen 	}
17510d259f04SJohn Johansen 
17525d5182caSJohn Johansen 	__aa_fs_list_remove_rawdata(ns);
17535d5182caSJohn Johansen 
1754b7fd2c03SJohn Johansen 	if (ns_subns_dir(ns)) {
1755b7fd2c03SJohn Johansen 		sub = d_inode(ns_subns_dir(ns))->i_private;
1756b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1757b7fd2c03SJohn Johansen 	}
1758b7fd2c03SJohn Johansen 	if (ns_subload(ns)) {
1759b7fd2c03SJohn Johansen 		sub = d_inode(ns_subload(ns))->i_private;
1760b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1761b7fd2c03SJohn Johansen 	}
1762b7fd2c03SJohn Johansen 	if (ns_subreplace(ns)) {
1763b7fd2c03SJohn Johansen 		sub = d_inode(ns_subreplace(ns))->i_private;
1764b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1765b7fd2c03SJohn Johansen 	}
1766b7fd2c03SJohn Johansen 	if (ns_subremove(ns)) {
1767b7fd2c03SJohn Johansen 		sub = d_inode(ns_subremove(ns))->i_private;
1768b7fd2c03SJohn Johansen 		aa_put_ns(sub);
1769b7fd2c03SJohn Johansen 	}
1770d9bf2c26SJohn Johansen 	if (ns_subrevision(ns)) {
1771d9bf2c26SJohn Johansen 		sub = d_inode(ns_subrevision(ns))->i_private;
1772d9bf2c26SJohn Johansen 		aa_put_ns(sub);
1773d9bf2c26SJohn Johansen 	}
1774b7fd2c03SJohn Johansen 
17750d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
1776c961ee5fSJohn Johansen 		aafs_remove(ns->dents[i]);
17770d259f04SJohn Johansen 		ns->dents[i] = NULL;
17780d259f04SJohn Johansen 	}
17790d259f04SJohn Johansen }
17800d259f04SJohn Johansen 
1781b7fd2c03SJohn Johansen /* assumes cleanup in caller */
1782c97204baSJohn Johansen static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
1783b7fd2c03SJohn Johansen {
1784b7fd2c03SJohn Johansen 	struct dentry *dent;
1785b7fd2c03SJohn Johansen 
1786b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1787b7fd2c03SJohn Johansen 	AA_BUG(!dir);
1788b7fd2c03SJohn Johansen 
1789c961ee5fSJohn Johansen 	dent = aafs_create_dir("profiles", dir);
1790b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1791b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1792b7fd2c03SJohn Johansen 	ns_subprofs_dir(ns) = dent;
1793b7fd2c03SJohn Johansen 
1794c961ee5fSJohn Johansen 	dent = aafs_create_dir("raw_data", dir);
1795b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1796b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1797b7fd2c03SJohn Johansen 	ns_subdata_dir(ns) = dent;
1798b7fd2c03SJohn Johansen 
1799d9bf2c26SJohn Johansen 	dent = aafs_create_file("revision", 0444, dir, ns,
1800d9bf2c26SJohn Johansen 				&aa_fs_ns_revision_fops);
1801d9bf2c26SJohn Johansen 	if (IS_ERR(dent))
1802d9bf2c26SJohn Johansen 		return PTR_ERR(dent);
1803d9bf2c26SJohn Johansen 	aa_get_ns(ns);
1804d9bf2c26SJohn Johansen 	ns_subrevision(ns) = dent;
1805d9bf2c26SJohn Johansen 
1806c961ee5fSJohn Johansen 	dent = aafs_create_file(".load", 0640, dir, ns,
1807b7fd2c03SJohn Johansen 				      &aa_fs_profile_load);
1808b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1809b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1810b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1811b7fd2c03SJohn Johansen 	ns_subload(ns) = dent;
1812b7fd2c03SJohn Johansen 
1813c961ee5fSJohn Johansen 	dent = aafs_create_file(".replace", 0640, dir, ns,
1814b7fd2c03SJohn Johansen 				      &aa_fs_profile_replace);
1815b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1816b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1817b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1818b7fd2c03SJohn Johansen 	ns_subreplace(ns) = dent;
1819b7fd2c03SJohn Johansen 
1820c961ee5fSJohn Johansen 	dent = aafs_create_file(".remove", 0640, dir, ns,
1821b7fd2c03SJohn Johansen 				      &aa_fs_profile_remove);
1822b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1823b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1824b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1825b7fd2c03SJohn Johansen 	ns_subremove(ns) = dent;
1826b7fd2c03SJohn Johansen 
18274ae47f33SJohn Johansen 	  /* use create_dentry so we can supply private data */
18284ae47f33SJohn Johansen 	dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL,
18294ae47f33SJohn Johansen 			   &ns_dir_inode_operations);
1830b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1831b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1832b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1833b7fd2c03SJohn Johansen 	ns_subns_dir(ns) = dent;
1834b7fd2c03SJohn Johansen 
1835b7fd2c03SJohn Johansen 	return 0;
1836b7fd2c03SJohn Johansen }
1837b7fd2c03SJohn Johansen 
1838c97204baSJohn Johansen /*
1839c97204baSJohn Johansen  * Requires: @ns->lock held
1840c97204baSJohn Johansen  */
184198407f0aSJohn Johansen int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
184298407f0aSJohn Johansen 		    struct dentry *dent)
18430d259f04SJohn Johansen {
184498849dffSJohn Johansen 	struct aa_ns *sub;
18450d259f04SJohn Johansen 	struct aa_profile *child;
184698407f0aSJohn Johansen 	struct dentry *dir;
18470d259f04SJohn Johansen 	int error;
18480d259f04SJohn Johansen 
1849b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1850b7fd2c03SJohn Johansen 	AA_BUG(!parent);
1851b7fd2c03SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
1852b7fd2c03SJohn Johansen 
18530d259f04SJohn Johansen 	if (!name)
18540d259f04SJohn Johansen 		name = ns->base.name;
18550d259f04SJohn Johansen 
1856c961ee5fSJohn Johansen 	if (!dent) {
1857b7fd2c03SJohn Johansen 		/* create ns dir if it doesn't already exist */
1858c961ee5fSJohn Johansen 		dent = aafs_create_dir(name, parent);
18590d259f04SJohn Johansen 		if (IS_ERR(dent))
18600d259f04SJohn Johansen 			goto fail;
1861c961ee5fSJohn Johansen 	} else
1862c961ee5fSJohn Johansen 		dget(dent);
18630d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
1864c97204baSJohn Johansen 	error = __aafs_ns_mkdir_entries(ns, dir);
1865b7fd2c03SJohn Johansen 	if (error)
1866b7fd2c03SJohn Johansen 		goto fail2;
18670d259f04SJohn Johansen 
1868b7fd2c03SJohn Johansen 	/* profiles */
18690d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
1870c97204baSJohn Johansen 		error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
18710d259f04SJohn Johansen 		if (error)
18720d259f04SJohn Johansen 			goto fail2;
18730d259f04SJohn Johansen 	}
18740d259f04SJohn Johansen 
1875b7fd2c03SJohn Johansen 	/* subnamespaces */
18760d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
1877feb3c766SJohn Johansen 		mutex_lock_nested(&sub->lock, sub->level);
187898407f0aSJohn Johansen 		error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
18790d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
18800d259f04SJohn Johansen 		if (error)
18810d259f04SJohn Johansen 			goto fail2;
18820d259f04SJohn Johansen 	}
18830d259f04SJohn Johansen 
18840d259f04SJohn Johansen 	return 0;
18850d259f04SJohn Johansen 
18860d259f04SJohn Johansen fail:
18870d259f04SJohn Johansen 	error = PTR_ERR(dent);
18880d259f04SJohn Johansen 
18890d259f04SJohn Johansen fail2:
1890c97204baSJohn Johansen 	__aafs_ns_rmdir(ns);
18910d259f04SJohn Johansen 
18920d259f04SJohn Johansen 	return error;
18930d259f04SJohn Johansen }
18940d259f04SJohn Johansen 
18950d259f04SJohn Johansen 
189629b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
189729b3822fSJohn Johansen 
189829b3822fSJohn Johansen /**
189998849dffSJohn Johansen  * __next_ns - find the next namespace to list
190029b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
190129b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
190229b3822fSJohn Johansen  *
190329b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
190429b3822fSJohn Johansen  * while switching current namespace.
190529b3822fSJohn Johansen  *
190629b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
190729b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
190829b3822fSJohn Johansen  * NOTE: will not unlock root->lock
190929b3822fSJohn Johansen  */
191098849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
191129b3822fSJohn Johansen {
191298849dffSJohn Johansen 	struct aa_ns *parent, *next;
191329b3822fSJohn Johansen 
1914cbf2d0e1SJohn Johansen 	AA_BUG(!root);
1915cbf2d0e1SJohn Johansen 	AA_BUG(!ns);
1916cbf2d0e1SJohn Johansen 	AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock));
1917cbf2d0e1SJohn Johansen 
191829b3822fSJohn Johansen 	/* is next namespace a child */
191929b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
192029b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
1921feb3c766SJohn Johansen 		mutex_lock_nested(&next->lock, next->level);
192229b3822fSJohn Johansen 		return next;
192329b3822fSJohn Johansen 	}
192429b3822fSJohn Johansen 
192529b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
192629b3822fSJohn Johansen 	parent = ns->parent;
1927ed2c7da3SJohn Johansen 	while (ns != root) {
192829b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
192938dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
193029b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
1931feb3c766SJohn Johansen 			mutex_lock_nested(&next->lock, next->level);
193229b3822fSJohn Johansen 			return next;
193329b3822fSJohn Johansen 		}
193429b3822fSJohn Johansen 		ns = parent;
193529b3822fSJohn Johansen 		parent = parent->parent;
193629b3822fSJohn Johansen 	}
193729b3822fSJohn Johansen 
193829b3822fSJohn Johansen 	return NULL;
193929b3822fSJohn Johansen }
194029b3822fSJohn Johansen 
194129b3822fSJohn Johansen /**
194229b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
194329b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
194429b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
194529b3822fSJohn Johansen  *
194629b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
194729b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
194829b3822fSJohn Johansen  */
194998849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
195098849dffSJohn Johansen 					  struct aa_ns *ns)
195129b3822fSJohn Johansen {
1952cbf2d0e1SJohn Johansen 	AA_BUG(!root);
1953cbf2d0e1SJohn Johansen 	AA_BUG(ns && !mutex_is_locked(&ns->lock));
1954cbf2d0e1SJohn Johansen 
195598849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
195629b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
195729b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
195829b3822fSJohn Johansen 						struct aa_profile, base.list);
195929b3822fSJohn Johansen 	}
196029b3822fSJohn Johansen 	return NULL;
196129b3822fSJohn Johansen }
196229b3822fSJohn Johansen 
196329b3822fSJohn Johansen /**
196429b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
196529b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
196629b3822fSJohn Johansen  *
196729b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
196829b3822fSJohn Johansen  *
196929b3822fSJohn Johansen  * Returns: next profile or NULL if done
197029b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
197129b3822fSJohn Johansen  */
197229b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
197329b3822fSJohn Johansen {
197429b3822fSJohn Johansen 	struct aa_profile *parent;
197598849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
197629b3822fSJohn Johansen 
1977cbf2d0e1SJohn Johansen 	AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock));
1978cbf2d0e1SJohn Johansen 
197929b3822fSJohn Johansen 	/* is next profile a child */
198029b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
198129b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
198229b3822fSJohn Johansen 					base.list);
198329b3822fSJohn Johansen 
198429b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
198529b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
198629b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
198729b3822fSJohn Johansen 	while (parent) {
198838dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
198929b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
199029b3822fSJohn Johansen 			return p;
199129b3822fSJohn Johansen 		p = parent;
199229b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
199329b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
199429b3822fSJohn Johansen 	}
199529b3822fSJohn Johansen 
199629b3822fSJohn Johansen 	/* is next another profile in the namespace */
199738dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
199829b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
199929b3822fSJohn Johansen 		return p;
200029b3822fSJohn Johansen 
200129b3822fSJohn Johansen 	return NULL;
200229b3822fSJohn Johansen }
200329b3822fSJohn Johansen 
200429b3822fSJohn Johansen /**
200529b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
200629b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
200729b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
200829b3822fSJohn Johansen  *
200929b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
201029b3822fSJohn Johansen  */
201198849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
201229b3822fSJohn Johansen 				       struct aa_profile *profile)
201329b3822fSJohn Johansen {
201429b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
201529b3822fSJohn Johansen 	if (next)
201629b3822fSJohn Johansen 		return next;
201729b3822fSJohn Johansen 
201829b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
201998849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
202029b3822fSJohn Johansen }
202129b3822fSJohn Johansen 
202229b3822fSJohn Johansen /**
202329b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
202429b3822fSJohn Johansen  * @f: seq_file to fill
202529b3822fSJohn Johansen  * @pos: current position
202629b3822fSJohn Johansen  *
202729b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
202829b3822fSJohn Johansen  *
202929b3822fSJohn Johansen  * acquires first ns->lock
203029b3822fSJohn Johansen  */
203129b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
203229b3822fSJohn Johansen {
203329b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
2034cf797c0eSJohn Johansen 	struct aa_ns *root = aa_get_current_ns();
203529b3822fSJohn Johansen 	loff_t l = *pos;
2036cf797c0eSJohn Johansen 	f->private = root;
203729b3822fSJohn Johansen 
203829b3822fSJohn Johansen 	/* find the first profile */
2039feb3c766SJohn Johansen 	mutex_lock_nested(&root->lock, root->level);
204029b3822fSJohn Johansen 	profile = __first_profile(root, root);
204129b3822fSJohn Johansen 
204229b3822fSJohn Johansen 	/* skip to position */
204329b3822fSJohn Johansen 	for (; profile && l > 0; l--)
204429b3822fSJohn Johansen 		profile = next_profile(root, profile);
204529b3822fSJohn Johansen 
204629b3822fSJohn Johansen 	return profile;
204729b3822fSJohn Johansen }
204829b3822fSJohn Johansen 
204929b3822fSJohn Johansen /**
205029b3822fSJohn Johansen  * p_next - read the next profile entry
205129b3822fSJohn Johansen  * @f: seq_file to fill
205229b3822fSJohn Johansen  * @p: profile previously returned
205329b3822fSJohn Johansen  * @pos: current position
205429b3822fSJohn Johansen  *
205529b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
205629b3822fSJohn Johansen  *
205729b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
205829b3822fSJohn Johansen  */
205929b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
206029b3822fSJohn Johansen {
206129b3822fSJohn Johansen 	struct aa_profile *profile = p;
206298849dffSJohn Johansen 	struct aa_ns *ns = f->private;
206329b3822fSJohn Johansen 	(*pos)++;
206429b3822fSJohn Johansen 
206529b3822fSJohn Johansen 	return next_profile(ns, profile);
206629b3822fSJohn Johansen }
206729b3822fSJohn Johansen 
206829b3822fSJohn Johansen /**
206929b3822fSJohn Johansen  * p_stop - stop depth first traversal
207029b3822fSJohn Johansen  * @f: seq_file we are filling
207129b3822fSJohn Johansen  * @p: the last profile writen
207229b3822fSJohn Johansen  *
207329b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
207429b3822fSJohn Johansen  */
207529b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
207629b3822fSJohn Johansen {
207729b3822fSJohn Johansen 	struct aa_profile *profile = p;
207898849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
207929b3822fSJohn Johansen 
208029b3822fSJohn Johansen 	if (profile) {
208129b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
208229b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
208329b3822fSJohn Johansen 	}
208429b3822fSJohn Johansen 	mutex_unlock(&root->lock);
208598849dffSJohn Johansen 	aa_put_ns(root);
208629b3822fSJohn Johansen }
208729b3822fSJohn Johansen 
208829b3822fSJohn Johansen /**
208929b3822fSJohn Johansen  * seq_show_profile - show a profile entry
209029b3822fSJohn Johansen  * @f: seq_file to file
209129b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
209229b3822fSJohn Johansen  *
209329b3822fSJohn Johansen  * Returns: error on failure
209429b3822fSJohn Johansen  */
209529b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
209629b3822fSJohn Johansen {
209729b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
209898849dffSJohn Johansen 	struct aa_ns *root = f->private;
209929b3822fSJohn Johansen 
2100637f688dSJohn Johansen 	aa_label_seq_xprint(f, root, &profile->label,
2101637f688dSJohn Johansen 			    FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
2102637f688dSJohn Johansen 	seq_putc(f, '\n');
210329b3822fSJohn Johansen 
210429b3822fSJohn Johansen 	return 0;
210529b3822fSJohn Johansen }
210629b3822fSJohn Johansen 
2107c97204baSJohn Johansen static const struct seq_operations aa_sfs_profiles_op = {
210829b3822fSJohn Johansen 	.start = p_start,
210929b3822fSJohn Johansen 	.next = p_next,
211029b3822fSJohn Johansen 	.stop = p_stop,
211129b3822fSJohn Johansen 	.show = seq_show_profile,
211229b3822fSJohn Johansen };
211329b3822fSJohn Johansen 
211429b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
211529b3822fSJohn Johansen {
21165ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
21175ac8c355SJohn Johansen 		return -EACCES;
21185ac8c355SJohn Johansen 
2119c97204baSJohn Johansen 	return seq_open(file, &aa_sfs_profiles_op);
212029b3822fSJohn Johansen }
212129b3822fSJohn Johansen 
212229b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
212329b3822fSJohn Johansen {
212429b3822fSJohn Johansen 	return seq_release(inode, file);
212529b3822fSJohn Johansen }
212629b3822fSJohn Johansen 
2127c97204baSJohn Johansen static const struct file_operations aa_sfs_profiles_fops = {
212829b3822fSJohn Johansen 	.open = profiles_open,
212929b3822fSJohn Johansen 	.read = seq_read,
213029b3822fSJohn Johansen 	.llseek = seq_lseek,
213129b3822fSJohn Johansen 	.release = profiles_release,
213229b3822fSJohn Johansen };
213329b3822fSJohn Johansen 
213429b3822fSJohn Johansen 
21350d259f04SJohn Johansen /** Base file system setup **/
2136c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_file[] = {
2137c97204baSJohn Johansen 	AA_SFS_FILE_STRING("mask",
2138c97204baSJohn Johansen 			   "create read write exec append mmap_exec link lock"),
2139a9bf8e9fSKees Cook 	{ }
2140a9bf8e9fSKees Cook };
2141a9bf8e9fSKees Cook 
2142290f458aSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ptrace[] = {
2143290f458aSJohn Johansen 	AA_SFS_FILE_STRING("mask", "read trace"),
2144290f458aSJohn Johansen 	{ }
2145290f458aSJohn Johansen };
2146290f458aSJohn Johansen 
2147cd1dbf76SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_signal[] = {
2148cd1dbf76SJohn Johansen 	AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK),
2149cd1dbf76SJohn Johansen 	{ }
2150cd1dbf76SJohn Johansen };
2151cd1dbf76SJohn Johansen 
2152c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_domain[] = {
2153c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hat",	1),
2154c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_hatv",	1),
2155c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_onexec",	1),
2156c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("change_profile",	1),
21576c5fc8f1SJohn Johansen 	AA_SFS_FILE_BOOLEAN("stack",		1),
2158c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
21599fcf78ccSJohn Johansen 	AA_SFS_FILE_BOOLEAN("post_nnp_subset",	1),
2160c97204baSJohn Johansen 	AA_SFS_FILE_STRING("version", "1.2"),
2161e74abcf3SKees Cook 	{ }
2162e74abcf3SKees Cook };
2163e74abcf3SKees Cook 
2164c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_versions[] = {
2165c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("v5",	1),
21665379a331SJohn Johansen 	AA_SFS_FILE_BOOLEAN("v6",	1),
21675379a331SJohn Johansen 	AA_SFS_FILE_BOOLEAN("v7",	1),
2168474d6b75SJohn Johansen 	{ }
2169474d6b75SJohn Johansen };
2170474d6b75SJohn Johansen 
2171c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_policy[] = {
2172c97204baSJohn Johansen 	AA_SFS_DIR("versions",			aa_sfs_entry_versions),
2173c97204baSJohn Johansen 	AA_SFS_FILE_BOOLEAN("set_load",		1),
21749d910a3bSJohn Johansen 	{ }
21759d910a3bSJohn Johansen };
21769d910a3bSJohn Johansen 
21772ea3ffb7SJohn Johansen static struct aa_sfs_entry aa_sfs_entry_mount[] = {
21782ea3ffb7SJohn Johansen 	AA_SFS_FILE_STRING("mask", "mount umount pivot_root"),
21792ea3ffb7SJohn Johansen 	{ }
21802ea3ffb7SJohn Johansen };
21812ea3ffb7SJohn Johansen 
218233f2eadaSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_ns[] = {
218333f2eadaSJohn Johansen 	AA_SFS_FILE_BOOLEAN("profile",		1),
21842ea3ffb7SJohn Johansen 	AA_SFS_FILE_BOOLEAN("pivot_root",	0),
218533f2eadaSJohn Johansen 	{ }
218633f2eadaSJohn Johansen };
218733f2eadaSJohn Johansen 
2188a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query_label[] = {
21894f3b3f2dSJohn Johansen 	AA_SFS_FILE_STRING("perms", "allow deny audit quiet"),
2190a83bd86eSJohn Johansen 	AA_SFS_FILE_BOOLEAN("data",		1),
21911dea3b41SJohn Johansen 	AA_SFS_FILE_BOOLEAN("multi_transaction",	1),
2192a83bd86eSJohn Johansen 	{ }
2193a83bd86eSJohn Johansen };
2194a83bd86eSJohn Johansen 
2195a83bd86eSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_query[] = {
2196a83bd86eSJohn Johansen 	AA_SFS_DIR("label",			aa_sfs_entry_query_label),
2197a83bd86eSJohn Johansen 	{ }
2198a83bd86eSJohn Johansen };
2199c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_features[] = {
2200c97204baSJohn Johansen 	AA_SFS_DIR("policy",			aa_sfs_entry_policy),
2201c97204baSJohn Johansen 	AA_SFS_DIR("domain",			aa_sfs_entry_domain),
2202c97204baSJohn Johansen 	AA_SFS_DIR("file",			aa_sfs_entry_file),
22032ea3ffb7SJohn Johansen 	AA_SFS_DIR("mount",			aa_sfs_entry_mount),
220433f2eadaSJohn Johansen 	AA_SFS_DIR("namespaces",		aa_sfs_entry_ns),
2205c97204baSJohn Johansen 	AA_SFS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
2206c97204baSJohn Johansen 	AA_SFS_DIR("rlimit",			aa_sfs_entry_rlimit),
2207c97204baSJohn Johansen 	AA_SFS_DIR("caps",			aa_sfs_entry_caps),
2208290f458aSJohn Johansen 	AA_SFS_DIR("ptrace",			aa_sfs_entry_ptrace),
2209cd1dbf76SJohn Johansen 	AA_SFS_DIR("signal",			aa_sfs_entry_signal),
2210a83bd86eSJohn Johansen 	AA_SFS_DIR("query",			aa_sfs_entry_query),
2211e74abcf3SKees Cook 	{ }
2212e74abcf3SKees Cook };
2213e74abcf3SKees Cook 
2214c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
2215bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access),
22166c5fc8f1SJohn Johansen 	AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops),
22176c5fc8f1SJohn Johansen 	AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops),
2218bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops),
2219bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops),
2220bf81100fSJohn Johansen 	AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops),
2221c97204baSJohn Johansen 	AA_SFS_DIR("features", aa_sfs_entry_features),
22229acd494bSKees Cook 	{ }
22239acd494bSKees Cook };
222463e2b423SJohn Johansen 
2225c97204baSJohn Johansen static struct aa_sfs_entry aa_sfs_entry =
2226c97204baSJohn Johansen 	AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
22279acd494bSKees Cook 
22289acd494bSKees Cook /**
2229a481f4d9SJohn Johansen  * entry_create_file - create a file entry in the apparmor securityfs
2230c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
22319acd494bSKees Cook  * @parent: the parent dentry in the securityfs
22329acd494bSKees Cook  *
2233a481f4d9SJohn Johansen  * Use entry_remove_file to remove entries created with this fn.
22349acd494bSKees Cook  */
2235c97204baSJohn Johansen static int __init entry_create_file(struct aa_sfs_entry *fs_file,
22369acd494bSKees Cook 				    struct dentry *parent)
223763e2b423SJohn Johansen {
22389acd494bSKees Cook 	int error = 0;
223963e2b423SJohn Johansen 
22409acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
22419acd494bSKees Cook 						 S_IFREG | fs_file->mode,
22429acd494bSKees Cook 						 parent, fs_file,
22439acd494bSKees Cook 						 fs_file->file_ops);
22449acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
22459acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
22469acd494bSKees Cook 		fs_file->dentry = NULL;
224763e2b423SJohn Johansen 	}
22489acd494bSKees Cook 	return error;
224963e2b423SJohn Johansen }
225063e2b423SJohn Johansen 
2251c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
225263e2b423SJohn Johansen /**
2253a481f4d9SJohn Johansen  * entry_create_dir - recursively create a directory entry in the securityfs
2254c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
22559acd494bSKees Cook  * @parent: the parent dentry in the securityfs
225663e2b423SJohn Johansen  *
2257a481f4d9SJohn Johansen  * Use entry_remove_dir to remove entries created with this fn.
225863e2b423SJohn Johansen  */
2259c97204baSJohn Johansen static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
22609acd494bSKees Cook 				   struct dentry *parent)
226163e2b423SJohn Johansen {
2262c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
22630d259f04SJohn Johansen 	struct dentry *dir;
22640d259f04SJohn Johansen 	int error;
226563e2b423SJohn Johansen 
22660d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
22670d259f04SJohn Johansen 	if (IS_ERR(dir))
22680d259f04SJohn Johansen 		return PTR_ERR(dir);
22690d259f04SJohn Johansen 	fs_dir->dentry = dir;
227063e2b423SJohn Johansen 
22710d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2272c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2273a481f4d9SJohn Johansen 			error = entry_create_dir(fs_file, fs_dir->dentry);
22749acd494bSKees Cook 		else
2275a481f4d9SJohn Johansen 			error = entry_create_file(fs_file, fs_dir->dentry);
22769acd494bSKees Cook 		if (error)
22779acd494bSKees Cook 			goto failed;
22789acd494bSKees Cook 	}
22799acd494bSKees Cook 
22809acd494bSKees Cook 	return 0;
22819acd494bSKees Cook 
22829acd494bSKees Cook failed:
2283a481f4d9SJohn Johansen 	entry_remove_dir(fs_dir);
22840d259f04SJohn Johansen 
22859acd494bSKees Cook 	return error;
22869acd494bSKees Cook }
22879acd494bSKees Cook 
22889acd494bSKees Cook /**
2289c97204baSJohn Johansen  * entry_remove_file - drop a single file entry in the apparmor securityfs
2290c97204baSJohn Johansen  * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
22919acd494bSKees Cook  */
2292c97204baSJohn Johansen static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
22939acd494bSKees Cook {
22949acd494bSKees Cook 	if (!fs_file->dentry)
22959acd494bSKees Cook 		return;
22969acd494bSKees Cook 
22979acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
22989acd494bSKees Cook 	fs_file->dentry = NULL;
22999acd494bSKees Cook }
23009acd494bSKees Cook 
23019acd494bSKees Cook /**
2302a481f4d9SJohn Johansen  * entry_remove_dir - recursively drop a directory entry from the securityfs
2303c97204baSJohn Johansen  * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
23049acd494bSKees Cook  */
2305c97204baSJohn Johansen static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
23069acd494bSKees Cook {
2307c97204baSJohn Johansen 	struct aa_sfs_entry *fs_file;
23089acd494bSKees Cook 
23090d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2310c97204baSJohn Johansen 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2311a481f4d9SJohn Johansen 			entry_remove_dir(fs_file);
23129acd494bSKees Cook 		else
2313c97204baSJohn Johansen 			entry_remove_file(fs_file);
23149acd494bSKees Cook 	}
23159acd494bSKees Cook 
2316c97204baSJohn Johansen 	entry_remove_file(fs_dir);
231763e2b423SJohn Johansen }
231863e2b423SJohn Johansen 
231963e2b423SJohn Johansen /**
232063e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
232163e2b423SJohn Johansen  *
232263e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
232363e2b423SJohn Johansen  */
232463e2b423SJohn Johansen void __init aa_destroy_aafs(void)
232563e2b423SJohn Johansen {
2326c97204baSJohn Johansen 	entry_remove_dir(&aa_sfs_entry);
232763e2b423SJohn Johansen }
232863e2b423SJohn Johansen 
2329a71ada30SJohn Johansen 
2330a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
2331a71ada30SJohn Johansen struct path aa_null;
2332a71ada30SJohn Johansen 
2333a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
2334a71ada30SJohn Johansen {
2335a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
2336a71ada30SJohn Johansen 	struct dentry *dentry;
2337a71ada30SJohn Johansen 	struct inode *inode;
2338a71ada30SJohn Johansen 	int count = 0;
2339a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
2340a71ada30SJohn Johansen 
2341a71ada30SJohn Johansen 	if (error)
2342a71ada30SJohn Johansen 		return error;
2343a71ada30SJohn Johansen 
2344a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
2345a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
2346a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
2347a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
2348a71ada30SJohn Johansen 		goto out;
2349a71ada30SJohn Johansen 	}
2350a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
2351a71ada30SJohn Johansen 	if (!inode) {
2352a71ada30SJohn Johansen 		error = -ENOMEM;
2353a71ada30SJohn Johansen 		goto out1;
2354a71ada30SJohn Johansen 	}
2355a71ada30SJohn Johansen 
2356a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
2357a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
235824d0d03cSDeepa Dinamani 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
2359a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
2360a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
2361a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
2362a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
2363a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
2364a71ada30SJohn Johansen 
2365a71ada30SJohn Johansen 	error = 0;
2366a71ada30SJohn Johansen 
2367a71ada30SJohn Johansen out1:
2368a71ada30SJohn Johansen 	dput(dentry);
2369a71ada30SJohn Johansen out:
2370a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
2371a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
2372a71ada30SJohn Johansen 	return error;
2373a71ada30SJohn Johansen }
2374a71ada30SJohn Johansen 
2375a481f4d9SJohn Johansen 
2376a481f4d9SJohn Johansen 
2377a481f4d9SJohn Johansen static const char *policy_get_link(struct dentry *dentry,
2378a481f4d9SJohn Johansen 				   struct inode *inode,
2379a481f4d9SJohn Johansen 				   struct delayed_call *done)
2380a481f4d9SJohn Johansen {
2381a481f4d9SJohn Johansen 	struct aa_ns *ns;
2382a481f4d9SJohn Johansen 	struct path path;
2383a481f4d9SJohn Johansen 
2384a481f4d9SJohn Johansen 	if (!dentry)
2385a481f4d9SJohn Johansen 		return ERR_PTR(-ECHILD);
2386a481f4d9SJohn Johansen 	ns = aa_get_current_ns();
2387a481f4d9SJohn Johansen 	path.mnt = mntget(aafs_mnt);
2388a481f4d9SJohn Johansen 	path.dentry = dget(ns_dir(ns));
2389a481f4d9SJohn Johansen 	nd_jump_link(&path);
2390a481f4d9SJohn Johansen 	aa_put_ns(ns);
2391a481f4d9SJohn Johansen 
2392a481f4d9SJohn Johansen 	return NULL;
2393a481f4d9SJohn Johansen }
2394a481f4d9SJohn Johansen 
2395a481f4d9SJohn Johansen static int ns_get_name(char *buf, size_t size, struct aa_ns *ns,
2396a481f4d9SJohn Johansen 		       struct inode *inode)
2397a481f4d9SJohn Johansen {
2398a481f4d9SJohn Johansen 	int res = snprintf(buf, size, "%s:[%lu]", AAFS_NAME, inode->i_ino);
2399a481f4d9SJohn Johansen 
2400a481f4d9SJohn Johansen 	if (res < 0 || res >= size)
2401a481f4d9SJohn Johansen 		res = -ENOENT;
2402a481f4d9SJohn Johansen 
2403a481f4d9SJohn Johansen 	return res;
2404a481f4d9SJohn Johansen }
2405a481f4d9SJohn Johansen 
2406a481f4d9SJohn Johansen static int policy_readlink(struct dentry *dentry, char __user *buffer,
2407a481f4d9SJohn Johansen 			   int buflen)
2408a481f4d9SJohn Johansen {
2409a481f4d9SJohn Johansen 	struct aa_ns *ns;
2410a481f4d9SJohn Johansen 	char name[32];
2411a481f4d9SJohn Johansen 	int res;
2412a481f4d9SJohn Johansen 
2413a481f4d9SJohn Johansen 	ns = aa_get_current_ns();
2414a481f4d9SJohn Johansen 	res = ns_get_name(name, sizeof(name), ns, d_inode(dentry));
2415a481f4d9SJohn Johansen 	if (res >= 0)
2416a481f4d9SJohn Johansen 		res = readlink_copy(buffer, buflen, name);
2417a481f4d9SJohn Johansen 	aa_put_ns(ns);
2418a481f4d9SJohn Johansen 
2419a481f4d9SJohn Johansen 	return res;
2420a481f4d9SJohn Johansen }
2421a481f4d9SJohn Johansen 
2422a481f4d9SJohn Johansen static const struct inode_operations policy_link_iops = {
2423a481f4d9SJohn Johansen 	.readlink	= policy_readlink,
2424a481f4d9SJohn Johansen 	.get_link	= policy_get_link,
2425a481f4d9SJohn Johansen };
2426a481f4d9SJohn Johansen 
2427a481f4d9SJohn Johansen 
242863e2b423SJohn Johansen /**
242963e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
243063e2b423SJohn Johansen  *
243163e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
243263e2b423SJohn Johansen  *
243363e2b423SJohn Johansen  * Returns: error on failure
243463e2b423SJohn Johansen  */
24353417d8d5SJames Morris static int __init aa_create_aafs(void)
243663e2b423SJohn Johansen {
2437b7fd2c03SJohn Johansen 	struct dentry *dent;
243863e2b423SJohn Johansen 	int error;
243963e2b423SJohn Johansen 
244063e2b423SJohn Johansen 	if (!apparmor_initialized)
244163e2b423SJohn Johansen 		return 0;
244263e2b423SJohn Johansen 
2443c97204baSJohn Johansen 	if (aa_sfs_entry.dentry) {
244463e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
244563e2b423SJohn Johansen 		return -EEXIST;
244663e2b423SJohn Johansen 	}
244763e2b423SJohn Johansen 
2448a481f4d9SJohn Johansen 	/* setup apparmorfs used to virtualize policy/ */
2449a481f4d9SJohn Johansen 	aafs_mnt = kern_mount(&aafs_ops);
2450a481f4d9SJohn Johansen 	if (IS_ERR(aafs_mnt))
2451a481f4d9SJohn Johansen 		panic("can't set apparmorfs up\n");
24521751e8a6SLinus Torvalds 	aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
2453a481f4d9SJohn Johansen 
24549acd494bSKees Cook 	/* Populate fs tree. */
2455c97204baSJohn Johansen 	error = entry_create_dir(&aa_sfs_entry, NULL);
245663e2b423SJohn Johansen 	if (error)
245763e2b423SJohn Johansen 		goto error;
245863e2b423SJohn Johansen 
2459c97204baSJohn Johansen 	dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
2460b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_load);
2461b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
2462b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
2463b7fd2c03SJohn Johansen 		goto error;
2464b7fd2c03SJohn Johansen 	}
2465b7fd2c03SJohn Johansen 	ns_subload(root_ns) = dent;
2466b7fd2c03SJohn Johansen 
2467c97204baSJohn Johansen 	dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
2468b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_replace);
2469b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
2470b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
2471b7fd2c03SJohn Johansen 		goto error;
2472b7fd2c03SJohn Johansen 	}
2473b7fd2c03SJohn Johansen 	ns_subreplace(root_ns) = dent;
2474b7fd2c03SJohn Johansen 
2475c97204baSJohn Johansen 	dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
2476b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_remove);
2477b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
2478b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
2479b7fd2c03SJohn Johansen 		goto error;
2480b7fd2c03SJohn Johansen 	}
2481b7fd2c03SJohn Johansen 	ns_subremove(root_ns) = dent;
2482b7fd2c03SJohn Johansen 
2483d9bf2c26SJohn Johansen 	dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry,
2484d9bf2c26SJohn Johansen 				      NULL, &aa_fs_ns_revision_fops);
2485d9bf2c26SJohn Johansen 	if (IS_ERR(dent)) {
2486d9bf2c26SJohn Johansen 		error = PTR_ERR(dent);
2487d9bf2c26SJohn Johansen 		goto error;
2488d9bf2c26SJohn Johansen 	}
2489d9bf2c26SJohn Johansen 	ns_subrevision(root_ns) = dent;
2490d9bf2c26SJohn Johansen 
2491d9bf2c26SJohn Johansen 	/* policy tree referenced by magic policy symlink */
2492feb3c766SJohn Johansen 	mutex_lock_nested(&root_ns->lock, root_ns->level);
2493c961ee5fSJohn Johansen 	error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
2494c961ee5fSJohn Johansen 				aafs_mnt->mnt_root);
2495b7fd2c03SJohn Johansen 	mutex_unlock(&root_ns->lock);
24960d259f04SJohn Johansen 	if (error)
24970d259f04SJohn Johansen 		goto error;
24980d259f04SJohn Johansen 
2499c961ee5fSJohn Johansen 	/* magic symlink similar to nsfs redirects based on task policy */
2500c961ee5fSJohn Johansen 	dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
2501c961ee5fSJohn Johansen 					 NULL, &policy_link_iops);
2502c961ee5fSJohn Johansen 	if (IS_ERR(dent)) {
2503c961ee5fSJohn Johansen 		error = PTR_ERR(dent);
2504c961ee5fSJohn Johansen 		goto error;
2505c961ee5fSJohn Johansen 	}
2506c961ee5fSJohn Johansen 
2507c97204baSJohn Johansen 	error = aa_mk_null_file(aa_sfs_entry.dentry);
2508a71ada30SJohn Johansen 	if (error)
2509a71ada30SJohn Johansen 		goto error;
2510a71ada30SJohn Johansen 
2511a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
251263e2b423SJohn Johansen 
251363e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
251463e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
251563e2b423SJohn Johansen 	return 0;
251663e2b423SJohn Johansen 
251763e2b423SJohn Johansen error:
251863e2b423SJohn Johansen 	aa_destroy_aafs();
251963e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
252063e2b423SJohn Johansen 	return error;
252163e2b423SJohn Johansen }
252263e2b423SJohn Johansen 
252363e2b423SJohn Johansen fs_initcall(aa_create_aafs);
2524