xref: /OK3568_Linux_fs/kernel/security/apparmor/apparmorfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * AppArmor security module
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Copyright (C) 1998-2008 Novell/SUSE
8*4882a593Smuzhiyun  * Copyright 2009-2010 Canonical Ltd.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/ctype.h>
12*4882a593Smuzhiyun #include <linux/security.h>
13*4882a593Smuzhiyun #include <linux/vmalloc.h>
14*4882a593Smuzhiyun #include <linux/init.h>
15*4882a593Smuzhiyun #include <linux/seq_file.h>
16*4882a593Smuzhiyun #include <linux/uaccess.h>
17*4882a593Smuzhiyun #include <linux/mount.h>
18*4882a593Smuzhiyun #include <linux/namei.h>
19*4882a593Smuzhiyun #include <linux/capability.h>
20*4882a593Smuzhiyun #include <linux/rcupdate.h>
21*4882a593Smuzhiyun #include <linux/fs.h>
22*4882a593Smuzhiyun #include <linux/fs_context.h>
23*4882a593Smuzhiyun #include <linux/poll.h>
24*4882a593Smuzhiyun #include <linux/zlib.h>
25*4882a593Smuzhiyun #include <uapi/linux/major.h>
26*4882a593Smuzhiyun #include <uapi/linux/magic.h>
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #include "include/apparmor.h"
29*4882a593Smuzhiyun #include "include/apparmorfs.h"
30*4882a593Smuzhiyun #include "include/audit.h"
31*4882a593Smuzhiyun #include "include/cred.h"
32*4882a593Smuzhiyun #include "include/crypto.h"
33*4882a593Smuzhiyun #include "include/ipc.h"
34*4882a593Smuzhiyun #include "include/label.h"
35*4882a593Smuzhiyun #include "include/policy.h"
36*4882a593Smuzhiyun #include "include/policy_ns.h"
37*4882a593Smuzhiyun #include "include/resource.h"
38*4882a593Smuzhiyun #include "include/policy_unpack.h"
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun  * The apparmor filesystem interface used for policy load and introspection
42*4882a593Smuzhiyun  * The interface is split into two main components based on their function
43*4882a593Smuzhiyun  * a securityfs component:
44*4882a593Smuzhiyun  *   used for static files that are always available, and which allows
45*4882a593Smuzhiyun  *   userspace to specificy the location of the security filesystem.
46*4882a593Smuzhiyun  *
47*4882a593Smuzhiyun  *   fns and data are prefixed with
48*4882a593Smuzhiyun  *      aa_sfs_
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * an apparmorfs component:
51*4882a593Smuzhiyun  *   used loaded policy content and introspection. It is not part of  a
52*4882a593Smuzhiyun  *   regular mounted filesystem and is available only through the magic
53*4882a593Smuzhiyun  *   policy symlink in the root of the securityfs apparmor/ directory.
54*4882a593Smuzhiyun  *   Tasks queries will be magically redirected to the correct portion
55*4882a593Smuzhiyun  *   of the policy tree based on their confinement.
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  *   fns and data are prefixed with
58*4882a593Smuzhiyun  *      aafs_
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  * The aa_fs_ prefix is used to indicate the fn is used by both the
61*4882a593Smuzhiyun  * securityfs and apparmorfs filesystems.
62*4882a593Smuzhiyun  */
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun  * support fns
67*4882a593Smuzhiyun  */
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun struct rawdata_f_data {
70*4882a593Smuzhiyun 	struct aa_loaddata *loaddata;
71*4882a593Smuzhiyun };
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun #define RAWDATA_F_DATA_BUF(p) (char *)(p + 1)
74*4882a593Smuzhiyun 
rawdata_f_data_free(struct rawdata_f_data * private)75*4882a593Smuzhiyun static void rawdata_f_data_free(struct rawdata_f_data *private)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	if (!private)
78*4882a593Smuzhiyun 		return;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	aa_put_loaddata(private->loaddata);
81*4882a593Smuzhiyun 	kvfree(private);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
rawdata_f_data_alloc(size_t size)84*4882a593Smuzhiyun static struct rawdata_f_data *rawdata_f_data_alloc(size_t size)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun 	struct rawdata_f_data *ret;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	if (size > SIZE_MAX - sizeof(*ret))
89*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL);
92*4882a593Smuzhiyun 	if (!ret)
93*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	return ret;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun  * aa_mangle_name - mangle a profile name to std profile layout form
100*4882a593Smuzhiyun  * @name: profile name to mangle  (NOT NULL)
101*4882a593Smuzhiyun  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
102*4882a593Smuzhiyun  *
103*4882a593Smuzhiyun  * Returns: length of mangled name
104*4882a593Smuzhiyun  */
mangle_name(const char * name,char * target)105*4882a593Smuzhiyun static int mangle_name(const char *name, char *target)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	char *t = target;
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	while (*name == '/' || *name == '.')
110*4882a593Smuzhiyun 		name++;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	if (target) {
113*4882a593Smuzhiyun 		for (; *name; name++) {
114*4882a593Smuzhiyun 			if (*name == '/')
115*4882a593Smuzhiyun 				*(t)++ = '.';
116*4882a593Smuzhiyun 			else if (isspace(*name))
117*4882a593Smuzhiyun 				*(t)++ = '_';
118*4882a593Smuzhiyun 			else if (isalnum(*name) || strchr("._-", *name))
119*4882a593Smuzhiyun 				*(t)++ = *name;
120*4882a593Smuzhiyun 		}
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 		*t = 0;
123*4882a593Smuzhiyun 	} else {
124*4882a593Smuzhiyun 		int len = 0;
125*4882a593Smuzhiyun 		for (; *name; name++) {
126*4882a593Smuzhiyun 			if (isalnum(*name) || isspace(*name) ||
127*4882a593Smuzhiyun 			    strchr("/._-", *name))
128*4882a593Smuzhiyun 				len++;
129*4882a593Smuzhiyun 		}
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 		return len;
132*4882a593Smuzhiyun 	}
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	return t - target;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun /*
139*4882a593Smuzhiyun  * aafs - core fns and data for the policy tree
140*4882a593Smuzhiyun  */
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun #define AAFS_NAME		"apparmorfs"
143*4882a593Smuzhiyun static struct vfsmount *aafs_mnt;
144*4882a593Smuzhiyun static int aafs_count;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 
aafs_show_path(struct seq_file * seq,struct dentry * dentry)147*4882a593Smuzhiyun static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino);
150*4882a593Smuzhiyun 	return 0;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun 
aafs_free_inode(struct inode * inode)153*4882a593Smuzhiyun static void aafs_free_inode(struct inode *inode)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	if (S_ISLNK(inode->i_mode))
156*4882a593Smuzhiyun 		kfree(inode->i_link);
157*4882a593Smuzhiyun 	free_inode_nonrcu(inode);
158*4882a593Smuzhiyun }
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun static const struct super_operations aafs_super_ops = {
161*4882a593Smuzhiyun 	.statfs = simple_statfs,
162*4882a593Smuzhiyun 	.free_inode = aafs_free_inode,
163*4882a593Smuzhiyun 	.show_path = aafs_show_path,
164*4882a593Smuzhiyun };
165*4882a593Smuzhiyun 
apparmorfs_fill_super(struct super_block * sb,struct fs_context * fc)166*4882a593Smuzhiyun static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	static struct tree_descr files[] = { {""} };
169*4882a593Smuzhiyun 	int error;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	error = simple_fill_super(sb, AAFS_MAGIC, files);
172*4882a593Smuzhiyun 	if (error)
173*4882a593Smuzhiyun 		return error;
174*4882a593Smuzhiyun 	sb->s_op = &aafs_super_ops;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	return 0;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
apparmorfs_get_tree(struct fs_context * fc)179*4882a593Smuzhiyun static int apparmorfs_get_tree(struct fs_context *fc)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun 	return get_tree_single(fc, apparmorfs_fill_super);
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun static const struct fs_context_operations apparmorfs_context_ops = {
185*4882a593Smuzhiyun 	.get_tree	= apparmorfs_get_tree,
186*4882a593Smuzhiyun };
187*4882a593Smuzhiyun 
apparmorfs_init_fs_context(struct fs_context * fc)188*4882a593Smuzhiyun static int apparmorfs_init_fs_context(struct fs_context *fc)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	fc->ops = &apparmorfs_context_ops;
191*4882a593Smuzhiyun 	return 0;
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun static struct file_system_type aafs_ops = {
195*4882a593Smuzhiyun 	.owner = THIS_MODULE,
196*4882a593Smuzhiyun 	.name = AAFS_NAME,
197*4882a593Smuzhiyun 	.init_fs_context = apparmorfs_init_fs_context,
198*4882a593Smuzhiyun 	.kill_sb = kill_anon_super,
199*4882a593Smuzhiyun };
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun /**
202*4882a593Smuzhiyun  * __aafs_setup_d_inode - basic inode setup for apparmorfs
203*4882a593Smuzhiyun  * @dir: parent directory for the dentry
204*4882a593Smuzhiyun  * @dentry: dentry we are seting the inode up for
205*4882a593Smuzhiyun  * @mode: permissions the file should have
206*4882a593Smuzhiyun  * @data: data to store on inode.i_private, available in open()
207*4882a593Smuzhiyun  * @link: if symlink, symlink target string
208*4882a593Smuzhiyun  * @fops: struct file_operations that should be used
209*4882a593Smuzhiyun  * @iops: struct of inode_operations that should be used
210*4882a593Smuzhiyun  */
__aafs_setup_d_inode(struct inode * dir,struct dentry * dentry,umode_t mode,void * data,char * link,const struct file_operations * fops,const struct inode_operations * iops)211*4882a593Smuzhiyun static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
212*4882a593Smuzhiyun 			       umode_t mode, void *data, char *link,
213*4882a593Smuzhiyun 			       const struct file_operations *fops,
214*4882a593Smuzhiyun 			       const struct inode_operations *iops)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun 	struct inode *inode = new_inode(dir->i_sb);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	AA_BUG(!dir);
219*4882a593Smuzhiyun 	AA_BUG(!dentry);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	if (!inode)
222*4882a593Smuzhiyun 		return -ENOMEM;
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	inode->i_ino = get_next_ino();
225*4882a593Smuzhiyun 	inode->i_mode = mode;
226*4882a593Smuzhiyun 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
227*4882a593Smuzhiyun 	inode->i_private = data;
228*4882a593Smuzhiyun 	if (S_ISDIR(mode)) {
229*4882a593Smuzhiyun 		inode->i_op = iops ? iops : &simple_dir_inode_operations;
230*4882a593Smuzhiyun 		inode->i_fop = &simple_dir_operations;
231*4882a593Smuzhiyun 		inc_nlink(inode);
232*4882a593Smuzhiyun 		inc_nlink(dir);
233*4882a593Smuzhiyun 	} else if (S_ISLNK(mode)) {
234*4882a593Smuzhiyun 		inode->i_op = iops ? iops : &simple_symlink_inode_operations;
235*4882a593Smuzhiyun 		inode->i_link = link;
236*4882a593Smuzhiyun 	} else {
237*4882a593Smuzhiyun 		inode->i_fop = fops;
238*4882a593Smuzhiyun 	}
239*4882a593Smuzhiyun 	d_instantiate(dentry, inode);
240*4882a593Smuzhiyun 	dget(dentry);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	return 0;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun /**
246*4882a593Smuzhiyun  * aafs_create - create a dentry in the apparmorfs filesystem
247*4882a593Smuzhiyun  *
248*4882a593Smuzhiyun  * @name: name of dentry to create
249*4882a593Smuzhiyun  * @mode: permissions the file should have
250*4882a593Smuzhiyun  * @parent: parent directory for this dentry
251*4882a593Smuzhiyun  * @data: data to store on inode.i_private, available in open()
252*4882a593Smuzhiyun  * @link: if symlink, symlink target string
253*4882a593Smuzhiyun  * @fops: struct file_operations that should be used for
254*4882a593Smuzhiyun  * @iops: struct of inode_operations that should be used
255*4882a593Smuzhiyun  *
256*4882a593Smuzhiyun  * This is the basic "create a xxx" function for apparmorfs.
257*4882a593Smuzhiyun  *
258*4882a593Smuzhiyun  * Returns a pointer to a dentry if it succeeds, that must be free with
259*4882a593Smuzhiyun  * aafs_remove(). Will return ERR_PTR on failure.
260*4882a593Smuzhiyun  */
aafs_create(const char * name,umode_t mode,struct dentry * parent,void * data,void * link,const struct file_operations * fops,const struct inode_operations * iops)261*4882a593Smuzhiyun static struct dentry *aafs_create(const char *name, umode_t mode,
262*4882a593Smuzhiyun 				  struct dentry *parent, void *data, void *link,
263*4882a593Smuzhiyun 				  const struct file_operations *fops,
264*4882a593Smuzhiyun 				  const struct inode_operations *iops)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	struct dentry *dentry;
267*4882a593Smuzhiyun 	struct inode *dir;
268*4882a593Smuzhiyun 	int error;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	AA_BUG(!name);
271*4882a593Smuzhiyun 	AA_BUG(!parent);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	if (!(mode & S_IFMT))
274*4882a593Smuzhiyun 		mode = (mode & S_IALLUGO) | S_IFREG;
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
277*4882a593Smuzhiyun 	if (error)
278*4882a593Smuzhiyun 		return ERR_PTR(error);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	dir = d_inode(parent);
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	inode_lock(dir);
283*4882a593Smuzhiyun 	dentry = lookup_one_len(name, parent, strlen(name));
284*4882a593Smuzhiyun 	if (IS_ERR(dentry)) {
285*4882a593Smuzhiyun 		error = PTR_ERR(dentry);
286*4882a593Smuzhiyun 		goto fail_lock;
287*4882a593Smuzhiyun 	}
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	if (d_really_is_positive(dentry)) {
290*4882a593Smuzhiyun 		error = -EEXIST;
291*4882a593Smuzhiyun 		goto fail_dentry;
292*4882a593Smuzhiyun 	}
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
295*4882a593Smuzhiyun 	if (error)
296*4882a593Smuzhiyun 		goto fail_dentry;
297*4882a593Smuzhiyun 	inode_unlock(dir);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	return dentry;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun fail_dentry:
302*4882a593Smuzhiyun 	dput(dentry);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun fail_lock:
305*4882a593Smuzhiyun 	inode_unlock(dir);
306*4882a593Smuzhiyun 	simple_release_fs(&aafs_mnt, &aafs_count);
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	return ERR_PTR(error);
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun /**
312*4882a593Smuzhiyun  * aafs_create_file - create a file in the apparmorfs filesystem
313*4882a593Smuzhiyun  *
314*4882a593Smuzhiyun  * @name: name of dentry to create
315*4882a593Smuzhiyun  * @mode: permissions the file should have
316*4882a593Smuzhiyun  * @parent: parent directory for this dentry
317*4882a593Smuzhiyun  * @data: data to store on inode.i_private, available in open()
318*4882a593Smuzhiyun  * @fops: struct file_operations that should be used for
319*4882a593Smuzhiyun  *
320*4882a593Smuzhiyun  * see aafs_create
321*4882a593Smuzhiyun  */
aafs_create_file(const char * name,umode_t mode,struct dentry * parent,void * data,const struct file_operations * fops)322*4882a593Smuzhiyun static struct dentry *aafs_create_file(const char *name, umode_t mode,
323*4882a593Smuzhiyun 				       struct dentry *parent, void *data,
324*4882a593Smuzhiyun 				       const struct file_operations *fops)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	return aafs_create(name, mode, parent, data, NULL, fops, NULL);
327*4882a593Smuzhiyun }
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun /**
330*4882a593Smuzhiyun  * aafs_create_dir - create a directory in the apparmorfs filesystem
331*4882a593Smuzhiyun  *
332*4882a593Smuzhiyun  * @name: name of dentry to create
333*4882a593Smuzhiyun  * @parent: parent directory for this dentry
334*4882a593Smuzhiyun  *
335*4882a593Smuzhiyun  * see aafs_create
336*4882a593Smuzhiyun  */
aafs_create_dir(const char * name,struct dentry * parent)337*4882a593Smuzhiyun static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
340*4882a593Smuzhiyun 			   NULL);
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun 
343*4882a593Smuzhiyun /**
344*4882a593Smuzhiyun  * aafs_remove - removes a file or directory from the apparmorfs filesystem
345*4882a593Smuzhiyun  *
346*4882a593Smuzhiyun  * @dentry: dentry of the file/directory/symlink to removed.
347*4882a593Smuzhiyun  */
aafs_remove(struct dentry * dentry)348*4882a593Smuzhiyun static void aafs_remove(struct dentry *dentry)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	struct inode *dir;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (!dentry || IS_ERR(dentry))
353*4882a593Smuzhiyun 		return;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	dir = d_inode(dentry->d_parent);
356*4882a593Smuzhiyun 	inode_lock(dir);
357*4882a593Smuzhiyun 	if (simple_positive(dentry)) {
358*4882a593Smuzhiyun 		if (d_is_dir(dentry))
359*4882a593Smuzhiyun 			simple_rmdir(dir, dentry);
360*4882a593Smuzhiyun 		else
361*4882a593Smuzhiyun 			simple_unlink(dir, dentry);
362*4882a593Smuzhiyun 		d_delete(dentry);
363*4882a593Smuzhiyun 		dput(dentry);
364*4882a593Smuzhiyun 	}
365*4882a593Smuzhiyun 	inode_unlock(dir);
366*4882a593Smuzhiyun 	simple_release_fs(&aafs_mnt, &aafs_count);
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun /*
371*4882a593Smuzhiyun  * aa_fs - policy load/replace/remove
372*4882a593Smuzhiyun  */
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun /**
375*4882a593Smuzhiyun  * aa_simple_write_to_buffer - common routine for getting policy from user
376*4882a593Smuzhiyun  * @userbuf: user buffer to copy data from  (NOT NULL)
377*4882a593Smuzhiyun  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
378*4882a593Smuzhiyun  * @copy_size: size of data to copy from user buffer
379*4882a593Smuzhiyun  * @pos: position write is at in the file (NOT NULL)
380*4882a593Smuzhiyun  *
381*4882a593Smuzhiyun  * Returns: kernel buffer containing copy of user buffer data or an
382*4882a593Smuzhiyun  *          ERR_PTR on failure.
383*4882a593Smuzhiyun  */
aa_simple_write_to_buffer(const char __user * userbuf,size_t alloc_size,size_t copy_size,loff_t * pos)384*4882a593Smuzhiyun static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
385*4882a593Smuzhiyun 						     size_t alloc_size,
386*4882a593Smuzhiyun 						     size_t copy_size,
387*4882a593Smuzhiyun 						     loff_t *pos)
388*4882a593Smuzhiyun {
389*4882a593Smuzhiyun 	struct aa_loaddata *data;
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 	AA_BUG(copy_size > alloc_size);
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	if (*pos != 0)
394*4882a593Smuzhiyun 		/* only writes from pos 0, that is complete writes */
395*4882a593Smuzhiyun 		return ERR_PTR(-ESPIPE);
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	/* freed by caller to simple_write_to_buffer */
398*4882a593Smuzhiyun 	data = aa_loaddata_alloc(alloc_size);
399*4882a593Smuzhiyun 	if (IS_ERR(data))
400*4882a593Smuzhiyun 		return data;
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	data->size = copy_size;
403*4882a593Smuzhiyun 	if (copy_from_user(data->data, userbuf, copy_size)) {
404*4882a593Smuzhiyun 		aa_put_loaddata(data);
405*4882a593Smuzhiyun 		return ERR_PTR(-EFAULT);
406*4882a593Smuzhiyun 	}
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	return data;
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun 
policy_update(u32 mask,const char __user * buf,size_t size,loff_t * pos,struct aa_ns * ns)411*4882a593Smuzhiyun static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
412*4882a593Smuzhiyun 			     loff_t *pos, struct aa_ns *ns)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	struct aa_loaddata *data;
415*4882a593Smuzhiyun 	struct aa_label *label;
416*4882a593Smuzhiyun 	ssize_t error;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	label = begin_current_label_crit_section();
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	/* high level check about policy management - fine grained in
421*4882a593Smuzhiyun 	 * below after unpack
422*4882a593Smuzhiyun 	 */
423*4882a593Smuzhiyun 	error = aa_may_manage_policy(label, ns, mask);
424*4882a593Smuzhiyun 	if (error)
425*4882a593Smuzhiyun 		goto end_section;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	data = aa_simple_write_to_buffer(buf, size, size, pos);
428*4882a593Smuzhiyun 	error = PTR_ERR(data);
429*4882a593Smuzhiyun 	if (!IS_ERR(data)) {
430*4882a593Smuzhiyun 		error = aa_replace_profiles(ns, label, mask, data);
431*4882a593Smuzhiyun 		aa_put_loaddata(data);
432*4882a593Smuzhiyun 	}
433*4882a593Smuzhiyun end_section:
434*4882a593Smuzhiyun 	end_current_label_crit_section(label);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	return error;
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun /* .load file hook fn to load policy */
profile_load(struct file * f,const char __user * buf,size_t size,loff_t * pos)440*4882a593Smuzhiyun static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
441*4882a593Smuzhiyun 			    loff_t *pos)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
444*4882a593Smuzhiyun 	int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 	aa_put_ns(ns);
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	return error;
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun static const struct file_operations aa_fs_profile_load = {
452*4882a593Smuzhiyun 	.write = profile_load,
453*4882a593Smuzhiyun 	.llseek = default_llseek,
454*4882a593Smuzhiyun };
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun /* .replace file hook fn to load and/or replace policy */
profile_replace(struct file * f,const char __user * buf,size_t size,loff_t * pos)457*4882a593Smuzhiyun static ssize_t profile_replace(struct file *f, const char __user *buf,
458*4882a593Smuzhiyun 			       size_t size, loff_t *pos)
459*4882a593Smuzhiyun {
460*4882a593Smuzhiyun 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
461*4882a593Smuzhiyun 	int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
462*4882a593Smuzhiyun 				  buf, size, pos, ns);
463*4882a593Smuzhiyun 	aa_put_ns(ns);
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	return error;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun static const struct file_operations aa_fs_profile_replace = {
469*4882a593Smuzhiyun 	.write = profile_replace,
470*4882a593Smuzhiyun 	.llseek = default_llseek,
471*4882a593Smuzhiyun };
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun /* .remove file hook fn to remove loaded policy */
profile_remove(struct file * f,const char __user * buf,size_t size,loff_t * pos)474*4882a593Smuzhiyun static ssize_t profile_remove(struct file *f, const char __user *buf,
475*4882a593Smuzhiyun 			      size_t size, loff_t *pos)
476*4882a593Smuzhiyun {
477*4882a593Smuzhiyun 	struct aa_loaddata *data;
478*4882a593Smuzhiyun 	struct aa_label *label;
479*4882a593Smuzhiyun 	ssize_t error;
480*4882a593Smuzhiyun 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	label = begin_current_label_crit_section();
483*4882a593Smuzhiyun 	/* high level check about policy management - fine grained in
484*4882a593Smuzhiyun 	 * below after unpack
485*4882a593Smuzhiyun 	 */
486*4882a593Smuzhiyun 	error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY);
487*4882a593Smuzhiyun 	if (error)
488*4882a593Smuzhiyun 		goto out;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	/*
491*4882a593Smuzhiyun 	 * aa_remove_profile needs a null terminated string so 1 extra
492*4882a593Smuzhiyun 	 * byte is allocated and the copied data is null terminated.
493*4882a593Smuzhiyun 	 */
494*4882a593Smuzhiyun 	data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	error = PTR_ERR(data);
497*4882a593Smuzhiyun 	if (!IS_ERR(data)) {
498*4882a593Smuzhiyun 		data->data[size] = 0;
499*4882a593Smuzhiyun 		error = aa_remove_profiles(ns, label, data->data, size);
500*4882a593Smuzhiyun 		aa_put_loaddata(data);
501*4882a593Smuzhiyun 	}
502*4882a593Smuzhiyun  out:
503*4882a593Smuzhiyun 	end_current_label_crit_section(label);
504*4882a593Smuzhiyun 	aa_put_ns(ns);
505*4882a593Smuzhiyun 	return error;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun static const struct file_operations aa_fs_profile_remove = {
509*4882a593Smuzhiyun 	.write = profile_remove,
510*4882a593Smuzhiyun 	.llseek = default_llseek,
511*4882a593Smuzhiyun };
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun struct aa_revision {
514*4882a593Smuzhiyun 	struct aa_ns *ns;
515*4882a593Smuzhiyun 	long last_read;
516*4882a593Smuzhiyun };
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun /* revision file hook fn for policy loads */
ns_revision_release(struct inode * inode,struct file * file)519*4882a593Smuzhiyun static int ns_revision_release(struct inode *inode, struct file *file)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun 	struct aa_revision *rev = file->private_data;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	if (rev) {
524*4882a593Smuzhiyun 		aa_put_ns(rev->ns);
525*4882a593Smuzhiyun 		kfree(rev);
526*4882a593Smuzhiyun 	}
527*4882a593Smuzhiyun 
528*4882a593Smuzhiyun 	return 0;
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
ns_revision_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)531*4882a593Smuzhiyun static ssize_t ns_revision_read(struct file *file, char __user *buf,
532*4882a593Smuzhiyun 				size_t size, loff_t *ppos)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	struct aa_revision *rev = file->private_data;
535*4882a593Smuzhiyun 	char buffer[32];
536*4882a593Smuzhiyun 	long last_read;
537*4882a593Smuzhiyun 	int avail;
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun 	mutex_lock_nested(&rev->ns->lock, rev->ns->level);
540*4882a593Smuzhiyun 	last_read = rev->last_read;
541*4882a593Smuzhiyun 	if (last_read == rev->ns->revision) {
542*4882a593Smuzhiyun 		mutex_unlock(&rev->ns->lock);
543*4882a593Smuzhiyun 		if (file->f_flags & O_NONBLOCK)
544*4882a593Smuzhiyun 			return -EAGAIN;
545*4882a593Smuzhiyun 		if (wait_event_interruptible(rev->ns->wait,
546*4882a593Smuzhiyun 					     last_read !=
547*4882a593Smuzhiyun 					     READ_ONCE(rev->ns->revision)))
548*4882a593Smuzhiyun 			return -ERESTARTSYS;
549*4882a593Smuzhiyun 		mutex_lock_nested(&rev->ns->lock, rev->ns->level);
550*4882a593Smuzhiyun 	}
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	avail = sprintf(buffer, "%ld\n", rev->ns->revision);
553*4882a593Smuzhiyun 	if (*ppos + size > avail) {
554*4882a593Smuzhiyun 		rev->last_read = rev->ns->revision;
555*4882a593Smuzhiyun 		*ppos = 0;
556*4882a593Smuzhiyun 	}
557*4882a593Smuzhiyun 	mutex_unlock(&rev->ns->lock);
558*4882a593Smuzhiyun 
559*4882a593Smuzhiyun 	return simple_read_from_buffer(buf, size, ppos, buffer, avail);
560*4882a593Smuzhiyun }
561*4882a593Smuzhiyun 
ns_revision_open(struct inode * inode,struct file * file)562*4882a593Smuzhiyun static int ns_revision_open(struct inode *inode, struct file *file)
563*4882a593Smuzhiyun {
564*4882a593Smuzhiyun 	struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
565*4882a593Smuzhiyun 
566*4882a593Smuzhiyun 	if (!rev)
567*4882a593Smuzhiyun 		return -ENOMEM;
568*4882a593Smuzhiyun 
569*4882a593Smuzhiyun 	rev->ns = aa_get_ns(inode->i_private);
570*4882a593Smuzhiyun 	if (!rev->ns)
571*4882a593Smuzhiyun 		rev->ns = aa_get_current_ns();
572*4882a593Smuzhiyun 	file->private_data = rev;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	return 0;
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun 
ns_revision_poll(struct file * file,poll_table * pt)577*4882a593Smuzhiyun static __poll_t ns_revision_poll(struct file *file, poll_table *pt)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun 	struct aa_revision *rev = file->private_data;
580*4882a593Smuzhiyun 	__poll_t mask = 0;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	if (rev) {
583*4882a593Smuzhiyun 		mutex_lock_nested(&rev->ns->lock, rev->ns->level);
584*4882a593Smuzhiyun 		poll_wait(file, &rev->ns->wait, pt);
585*4882a593Smuzhiyun 		if (rev->last_read < rev->ns->revision)
586*4882a593Smuzhiyun 			mask |= EPOLLIN | EPOLLRDNORM;
587*4882a593Smuzhiyun 		mutex_unlock(&rev->ns->lock);
588*4882a593Smuzhiyun 	}
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	return mask;
591*4882a593Smuzhiyun }
592*4882a593Smuzhiyun 
__aa_bump_ns_revision(struct aa_ns * ns)593*4882a593Smuzhiyun void __aa_bump_ns_revision(struct aa_ns *ns)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	WRITE_ONCE(ns->revision, READ_ONCE(ns->revision) + 1);
596*4882a593Smuzhiyun 	wake_up_interruptible(&ns->wait);
597*4882a593Smuzhiyun }
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun static const struct file_operations aa_fs_ns_revision_fops = {
600*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
601*4882a593Smuzhiyun 	.open		= ns_revision_open,
602*4882a593Smuzhiyun 	.poll		= ns_revision_poll,
603*4882a593Smuzhiyun 	.read		= ns_revision_read,
604*4882a593Smuzhiyun 	.llseek		= generic_file_llseek,
605*4882a593Smuzhiyun 	.release	= ns_revision_release,
606*4882a593Smuzhiyun };
607*4882a593Smuzhiyun 
profile_query_cb(struct aa_profile * profile,struct aa_perms * perms,const char * match_str,size_t match_len)608*4882a593Smuzhiyun static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
609*4882a593Smuzhiyun 			     const char *match_str, size_t match_len)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	struct aa_perms tmp = { };
612*4882a593Smuzhiyun 	struct aa_dfa *dfa;
613*4882a593Smuzhiyun 	unsigned int state = 0;
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	if (profile_unconfined(profile))
616*4882a593Smuzhiyun 		return;
617*4882a593Smuzhiyun 	if (profile->file.dfa && *match_str == AA_CLASS_FILE) {
618*4882a593Smuzhiyun 		dfa = profile->file.dfa;
619*4882a593Smuzhiyun 		state = aa_dfa_match_len(dfa, profile->file.start,
620*4882a593Smuzhiyun 					 match_str + 1, match_len - 1);
621*4882a593Smuzhiyun 		if (state) {
622*4882a593Smuzhiyun 			struct path_cond cond = { };
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 			tmp = aa_compute_fperms(dfa, state, &cond);
625*4882a593Smuzhiyun 		}
626*4882a593Smuzhiyun 	} else if (profile->policy.dfa) {
627*4882a593Smuzhiyun 		if (!PROFILE_MEDIATES(profile, *match_str))
628*4882a593Smuzhiyun 			return;	/* no change to current perms */
629*4882a593Smuzhiyun 		dfa = profile->policy.dfa;
630*4882a593Smuzhiyun 		state = aa_dfa_match_len(dfa, profile->policy.start[0],
631*4882a593Smuzhiyun 					 match_str, match_len);
632*4882a593Smuzhiyun 		if (state)
633*4882a593Smuzhiyun 			aa_compute_perms(dfa, state, &tmp);
634*4882a593Smuzhiyun 	}
635*4882a593Smuzhiyun 	aa_apply_modes_to_perms(profile, &tmp);
636*4882a593Smuzhiyun 	aa_perms_accum_raw(perms, &tmp);
637*4882a593Smuzhiyun }
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun /**
641*4882a593Smuzhiyun  * query_data - queries a policy and writes its data to buf
642*4882a593Smuzhiyun  * @buf: the resulting data is stored here (NOT NULL)
643*4882a593Smuzhiyun  * @buf_len: size of buf
644*4882a593Smuzhiyun  * @query: query string used to retrieve data
645*4882a593Smuzhiyun  * @query_len: size of query including second NUL byte
646*4882a593Smuzhiyun  *
647*4882a593Smuzhiyun  * The buffers pointed to by buf and query may overlap. The query buffer is
648*4882a593Smuzhiyun  * parsed before buf is written to.
649*4882a593Smuzhiyun  *
650*4882a593Smuzhiyun  * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
651*4882a593Smuzhiyun  * the security confinement context and <KEY> is the name of the data to
652*4882a593Smuzhiyun  * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
653*4882a593Smuzhiyun  *
654*4882a593Smuzhiyun  * Don't expect the contents of buf to be preserved on failure.
655*4882a593Smuzhiyun  *
656*4882a593Smuzhiyun  * Returns: number of characters written to buf or -errno on failure
657*4882a593Smuzhiyun  */
query_data(char * buf,size_t buf_len,char * query,size_t query_len)658*4882a593Smuzhiyun static ssize_t query_data(char *buf, size_t buf_len,
659*4882a593Smuzhiyun 			  char *query, size_t query_len)
660*4882a593Smuzhiyun {
661*4882a593Smuzhiyun 	char *out;
662*4882a593Smuzhiyun 	const char *key;
663*4882a593Smuzhiyun 	struct label_it i;
664*4882a593Smuzhiyun 	struct aa_label *label, *curr;
665*4882a593Smuzhiyun 	struct aa_profile *profile;
666*4882a593Smuzhiyun 	struct aa_data *data;
667*4882a593Smuzhiyun 	u32 bytes, blocks;
668*4882a593Smuzhiyun 	__le32 outle32;
669*4882a593Smuzhiyun 
670*4882a593Smuzhiyun 	if (!query_len)
671*4882a593Smuzhiyun 		return -EINVAL; /* need a query */
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	key = query + strnlen(query, query_len) + 1;
674*4882a593Smuzhiyun 	if (key + 1 >= query + query_len)
675*4882a593Smuzhiyun 		return -EINVAL; /* not enough space for a non-empty key */
676*4882a593Smuzhiyun 	if (key + strnlen(key, query + query_len - key) >= query + query_len)
677*4882a593Smuzhiyun 		return -EINVAL; /* must end with NUL */
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	if (buf_len < sizeof(bytes) + sizeof(blocks))
680*4882a593Smuzhiyun 		return -EINVAL; /* not enough space */
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	curr = begin_current_label_crit_section();
683*4882a593Smuzhiyun 	label = aa_label_parse(curr, query, GFP_KERNEL, false, false);
684*4882a593Smuzhiyun 	end_current_label_crit_section(curr);
685*4882a593Smuzhiyun 	if (IS_ERR(label))
686*4882a593Smuzhiyun 		return PTR_ERR(label);
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	/* We are going to leave space for two numbers. The first is the total
689*4882a593Smuzhiyun 	 * number of bytes we are writing after the first number. This is so
690*4882a593Smuzhiyun 	 * users can read the full output without reallocation.
691*4882a593Smuzhiyun 	 *
692*4882a593Smuzhiyun 	 * The second number is the number of data blocks we're writing. An
693*4882a593Smuzhiyun 	 * application might be confined by multiple policies having data in
694*4882a593Smuzhiyun 	 * the same key.
695*4882a593Smuzhiyun 	 */
696*4882a593Smuzhiyun 	memset(buf, 0, sizeof(bytes) + sizeof(blocks));
697*4882a593Smuzhiyun 	out = buf + sizeof(bytes) + sizeof(blocks);
698*4882a593Smuzhiyun 
699*4882a593Smuzhiyun 	blocks = 0;
700*4882a593Smuzhiyun 	label_for_each_confined(i, label, profile) {
701*4882a593Smuzhiyun 		if (!profile->data)
702*4882a593Smuzhiyun 			continue;
703*4882a593Smuzhiyun 
704*4882a593Smuzhiyun 		data = rhashtable_lookup_fast(profile->data, &key,
705*4882a593Smuzhiyun 					      profile->data->p);
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 		if (data) {
708*4882a593Smuzhiyun 			if (out + sizeof(outle32) + data->size > buf +
709*4882a593Smuzhiyun 			    buf_len) {
710*4882a593Smuzhiyun 				aa_put_label(label);
711*4882a593Smuzhiyun 				return -EINVAL; /* not enough space */
712*4882a593Smuzhiyun 			}
713*4882a593Smuzhiyun 			outle32 = __cpu_to_le32(data->size);
714*4882a593Smuzhiyun 			memcpy(out, &outle32, sizeof(outle32));
715*4882a593Smuzhiyun 			out += sizeof(outle32);
716*4882a593Smuzhiyun 			memcpy(out, data->data, data->size);
717*4882a593Smuzhiyun 			out += data->size;
718*4882a593Smuzhiyun 			blocks++;
719*4882a593Smuzhiyun 		}
720*4882a593Smuzhiyun 	}
721*4882a593Smuzhiyun 	aa_put_label(label);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
724*4882a593Smuzhiyun 	memcpy(buf, &outle32, sizeof(outle32));
725*4882a593Smuzhiyun 	outle32 = __cpu_to_le32(blocks);
726*4882a593Smuzhiyun 	memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun 	return out - buf;
729*4882a593Smuzhiyun }
730*4882a593Smuzhiyun 
731*4882a593Smuzhiyun /**
732*4882a593Smuzhiyun  * query_label - queries a label and writes permissions to buf
733*4882a593Smuzhiyun  * @buf: the resulting permissions string is stored here (NOT NULL)
734*4882a593Smuzhiyun  * @buf_len: size of buf
735*4882a593Smuzhiyun  * @query: binary query string to match against the dfa
736*4882a593Smuzhiyun  * @query_len: size of query
737*4882a593Smuzhiyun  * @view_only: only compute for querier's view
738*4882a593Smuzhiyun  *
739*4882a593Smuzhiyun  * The buffers pointed to by buf and query may overlap. The query buffer is
740*4882a593Smuzhiyun  * parsed before buf is written to.
741*4882a593Smuzhiyun  *
742*4882a593Smuzhiyun  * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is
743*4882a593Smuzhiyun  * the name of the label, in the current namespace, that is to be queried and
744*4882a593Smuzhiyun  * DFA_STRING is a binary string to match against the label(s)'s DFA.
745*4882a593Smuzhiyun  *
746*4882a593Smuzhiyun  * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters
747*4882a593Smuzhiyun  * but must *not* be NUL terminated.
748*4882a593Smuzhiyun  *
749*4882a593Smuzhiyun  * Returns: number of characters written to buf or -errno on failure
750*4882a593Smuzhiyun  */
query_label(char * buf,size_t buf_len,char * query,size_t query_len,bool view_only)751*4882a593Smuzhiyun static ssize_t query_label(char *buf, size_t buf_len,
752*4882a593Smuzhiyun 			   char *query, size_t query_len, bool view_only)
753*4882a593Smuzhiyun {
754*4882a593Smuzhiyun 	struct aa_profile *profile;
755*4882a593Smuzhiyun 	struct aa_label *label, *curr;
756*4882a593Smuzhiyun 	char *label_name, *match_str;
757*4882a593Smuzhiyun 	size_t label_name_len, match_len;
758*4882a593Smuzhiyun 	struct aa_perms perms;
759*4882a593Smuzhiyun 	struct label_it i;
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	if (!query_len)
762*4882a593Smuzhiyun 		return -EINVAL;
763*4882a593Smuzhiyun 
764*4882a593Smuzhiyun 	label_name = query;
765*4882a593Smuzhiyun 	label_name_len = strnlen(query, query_len);
766*4882a593Smuzhiyun 	if (!label_name_len || label_name_len == query_len)
767*4882a593Smuzhiyun 		return -EINVAL;
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun 	/**
770*4882a593Smuzhiyun 	 * The extra byte is to account for the null byte between the
771*4882a593Smuzhiyun 	 * profile name and dfa string. profile_name_len is greater
772*4882a593Smuzhiyun 	 * than zero and less than query_len, so a byte can be safely
773*4882a593Smuzhiyun 	 * added or subtracted.
774*4882a593Smuzhiyun 	 */
775*4882a593Smuzhiyun 	match_str = label_name + label_name_len + 1;
776*4882a593Smuzhiyun 	match_len = query_len - label_name_len - 1;
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	curr = begin_current_label_crit_section();
779*4882a593Smuzhiyun 	label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false);
780*4882a593Smuzhiyun 	end_current_label_crit_section(curr);
781*4882a593Smuzhiyun 	if (IS_ERR(label))
782*4882a593Smuzhiyun 		return PTR_ERR(label);
783*4882a593Smuzhiyun 
784*4882a593Smuzhiyun 	perms = allperms;
785*4882a593Smuzhiyun 	if (view_only) {
786*4882a593Smuzhiyun 		label_for_each_in_ns(i, labels_ns(label), label, profile) {
787*4882a593Smuzhiyun 			profile_query_cb(profile, &perms, match_str, match_len);
788*4882a593Smuzhiyun 		}
789*4882a593Smuzhiyun 	} else {
790*4882a593Smuzhiyun 		label_for_each(i, label, profile) {
791*4882a593Smuzhiyun 			profile_query_cb(profile, &perms, match_str, match_len);
792*4882a593Smuzhiyun 		}
793*4882a593Smuzhiyun 	}
794*4882a593Smuzhiyun 	aa_put_label(label);
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun 	return scnprintf(buf, buf_len,
797*4882a593Smuzhiyun 		      "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n",
798*4882a593Smuzhiyun 		      perms.allow, perms.deny, perms.audit, perms.quiet);
799*4882a593Smuzhiyun }
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun /*
802*4882a593Smuzhiyun  * Transaction based IO.
803*4882a593Smuzhiyun  * The file expects a write which triggers the transaction, and then
804*4882a593Smuzhiyun  * possibly a read(s) which collects the result - which is stored in a
805*4882a593Smuzhiyun  * file-local buffer. Once a new write is performed, a new set of results
806*4882a593Smuzhiyun  * are stored in the file-local buffer.
807*4882a593Smuzhiyun  */
808*4882a593Smuzhiyun struct multi_transaction {
809*4882a593Smuzhiyun 	struct kref count;
810*4882a593Smuzhiyun 	ssize_t size;
811*4882a593Smuzhiyun 	char data[];
812*4882a593Smuzhiyun };
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction))
815*4882a593Smuzhiyun /* TODO: replace with per file lock */
816*4882a593Smuzhiyun static DEFINE_SPINLOCK(multi_transaction_lock);
817*4882a593Smuzhiyun 
multi_transaction_kref(struct kref * kref)818*4882a593Smuzhiyun static void multi_transaction_kref(struct kref *kref)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun 	struct multi_transaction *t;
821*4882a593Smuzhiyun 
822*4882a593Smuzhiyun 	t = container_of(kref, struct multi_transaction, count);
823*4882a593Smuzhiyun 	free_page((unsigned long) t);
824*4882a593Smuzhiyun }
825*4882a593Smuzhiyun 
826*4882a593Smuzhiyun static struct multi_transaction *
get_multi_transaction(struct multi_transaction * t)827*4882a593Smuzhiyun get_multi_transaction(struct multi_transaction *t)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	if  (t)
830*4882a593Smuzhiyun 		kref_get(&(t->count));
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 	return t;
833*4882a593Smuzhiyun }
834*4882a593Smuzhiyun 
put_multi_transaction(struct multi_transaction * t)835*4882a593Smuzhiyun static void put_multi_transaction(struct multi_transaction *t)
836*4882a593Smuzhiyun {
837*4882a593Smuzhiyun 	if (t)
838*4882a593Smuzhiyun 		kref_put(&(t->count), multi_transaction_kref);
839*4882a593Smuzhiyun }
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun /* does not increment @new's count */
multi_transaction_set(struct file * file,struct multi_transaction * new,size_t n)842*4882a593Smuzhiyun static void multi_transaction_set(struct file *file,
843*4882a593Smuzhiyun 				  struct multi_transaction *new, size_t n)
844*4882a593Smuzhiyun {
845*4882a593Smuzhiyun 	struct multi_transaction *old;
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun 	AA_BUG(n > MULTI_TRANSACTION_LIMIT);
848*4882a593Smuzhiyun 
849*4882a593Smuzhiyun 	new->size = n;
850*4882a593Smuzhiyun 	spin_lock(&multi_transaction_lock);
851*4882a593Smuzhiyun 	old = (struct multi_transaction *) file->private_data;
852*4882a593Smuzhiyun 	file->private_data = new;
853*4882a593Smuzhiyun 	spin_unlock(&multi_transaction_lock);
854*4882a593Smuzhiyun 	put_multi_transaction(old);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun 
multi_transaction_new(struct file * file,const char __user * buf,size_t size)857*4882a593Smuzhiyun static struct multi_transaction *multi_transaction_new(struct file *file,
858*4882a593Smuzhiyun 						       const char __user *buf,
859*4882a593Smuzhiyun 						       size_t size)
860*4882a593Smuzhiyun {
861*4882a593Smuzhiyun 	struct multi_transaction *t;
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	if (size > MULTI_TRANSACTION_LIMIT - 1)
864*4882a593Smuzhiyun 		return ERR_PTR(-EFBIG);
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
867*4882a593Smuzhiyun 	if (!t)
868*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
869*4882a593Smuzhiyun 	kref_init(&t->count);
870*4882a593Smuzhiyun 	if (copy_from_user(t->data, buf, size))
871*4882a593Smuzhiyun 		return ERR_PTR(-EFAULT);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	return t;
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun 
multi_transaction_read(struct file * file,char __user * buf,size_t size,loff_t * pos)876*4882a593Smuzhiyun static ssize_t multi_transaction_read(struct file *file, char __user *buf,
877*4882a593Smuzhiyun 				       size_t size, loff_t *pos)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun 	struct multi_transaction *t;
880*4882a593Smuzhiyun 	ssize_t ret;
881*4882a593Smuzhiyun 
882*4882a593Smuzhiyun 	spin_lock(&multi_transaction_lock);
883*4882a593Smuzhiyun 	t = get_multi_transaction(file->private_data);
884*4882a593Smuzhiyun 	spin_unlock(&multi_transaction_lock);
885*4882a593Smuzhiyun 	if (!t)
886*4882a593Smuzhiyun 		return 0;
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	ret = simple_read_from_buffer(buf, size, pos, t->data, t->size);
889*4882a593Smuzhiyun 	put_multi_transaction(t);
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	return ret;
892*4882a593Smuzhiyun }
893*4882a593Smuzhiyun 
multi_transaction_release(struct inode * inode,struct file * file)894*4882a593Smuzhiyun static int multi_transaction_release(struct inode *inode, struct file *file)
895*4882a593Smuzhiyun {
896*4882a593Smuzhiyun 	put_multi_transaction(file->private_data);
897*4882a593Smuzhiyun 
898*4882a593Smuzhiyun 	return 0;
899*4882a593Smuzhiyun }
900*4882a593Smuzhiyun 
901*4882a593Smuzhiyun #define QUERY_CMD_LABEL		"label\0"
902*4882a593Smuzhiyun #define QUERY_CMD_LABEL_LEN	6
903*4882a593Smuzhiyun #define QUERY_CMD_PROFILE	"profile\0"
904*4882a593Smuzhiyun #define QUERY_CMD_PROFILE_LEN	8
905*4882a593Smuzhiyun #define QUERY_CMD_LABELALL	"labelall\0"
906*4882a593Smuzhiyun #define QUERY_CMD_LABELALL_LEN	9
907*4882a593Smuzhiyun #define QUERY_CMD_DATA		"data\0"
908*4882a593Smuzhiyun #define QUERY_CMD_DATA_LEN	5
909*4882a593Smuzhiyun 
910*4882a593Smuzhiyun /**
911*4882a593Smuzhiyun  * aa_write_access - generic permissions and data query
912*4882a593Smuzhiyun  * @file: pointer to open apparmorfs/access file
913*4882a593Smuzhiyun  * @ubuf: user buffer containing the complete query string (NOT NULL)
914*4882a593Smuzhiyun  * @count: size of ubuf
915*4882a593Smuzhiyun  * @ppos: position in the file (MUST BE ZERO)
916*4882a593Smuzhiyun  *
917*4882a593Smuzhiyun  * Allows for one permissions or data query per open(), write(), and read()
918*4882a593Smuzhiyun  * sequence. The only queries currently supported are label-based queries for
919*4882a593Smuzhiyun  * permissions or data.
920*4882a593Smuzhiyun  *
921*4882a593Smuzhiyun  * For permissions queries, ubuf must begin with "label\0", followed by the
922*4882a593Smuzhiyun  * profile query specific format described in the query_label() function
923*4882a593Smuzhiyun  * documentation.
924*4882a593Smuzhiyun  *
925*4882a593Smuzhiyun  * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
926*4882a593Smuzhiyun  * <LABEL> is the name of the security confinement context and <KEY> is the
927*4882a593Smuzhiyun  * name of the data to retrieve.
928*4882a593Smuzhiyun  *
929*4882a593Smuzhiyun  * Returns: number of bytes written or -errno on failure
930*4882a593Smuzhiyun  */
aa_write_access(struct file * file,const char __user * ubuf,size_t count,loff_t * ppos)931*4882a593Smuzhiyun static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
932*4882a593Smuzhiyun 			       size_t count, loff_t *ppos)
933*4882a593Smuzhiyun {
934*4882a593Smuzhiyun 	struct multi_transaction *t;
935*4882a593Smuzhiyun 	ssize_t len;
936*4882a593Smuzhiyun 
937*4882a593Smuzhiyun 	if (*ppos)
938*4882a593Smuzhiyun 		return -ESPIPE;
939*4882a593Smuzhiyun 
940*4882a593Smuzhiyun 	t = multi_transaction_new(file, ubuf, count);
941*4882a593Smuzhiyun 	if (IS_ERR(t))
942*4882a593Smuzhiyun 		return PTR_ERR(t);
943*4882a593Smuzhiyun 
944*4882a593Smuzhiyun 	if (count > QUERY_CMD_PROFILE_LEN &&
945*4882a593Smuzhiyun 	    !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) {
946*4882a593Smuzhiyun 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
947*4882a593Smuzhiyun 				  t->data + QUERY_CMD_PROFILE_LEN,
948*4882a593Smuzhiyun 				  count - QUERY_CMD_PROFILE_LEN, true);
949*4882a593Smuzhiyun 	} else if (count > QUERY_CMD_LABEL_LEN &&
950*4882a593Smuzhiyun 		   !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) {
951*4882a593Smuzhiyun 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
952*4882a593Smuzhiyun 				  t->data + QUERY_CMD_LABEL_LEN,
953*4882a593Smuzhiyun 				  count - QUERY_CMD_LABEL_LEN, true);
954*4882a593Smuzhiyun 	} else if (count > QUERY_CMD_LABELALL_LEN &&
955*4882a593Smuzhiyun 		   !memcmp(t->data, QUERY_CMD_LABELALL,
956*4882a593Smuzhiyun 			   QUERY_CMD_LABELALL_LEN)) {
957*4882a593Smuzhiyun 		len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
958*4882a593Smuzhiyun 				  t->data + QUERY_CMD_LABELALL_LEN,
959*4882a593Smuzhiyun 				  count - QUERY_CMD_LABELALL_LEN, false);
960*4882a593Smuzhiyun 	} else if (count > QUERY_CMD_DATA_LEN &&
961*4882a593Smuzhiyun 		   !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
962*4882a593Smuzhiyun 		len = query_data(t->data, MULTI_TRANSACTION_LIMIT,
963*4882a593Smuzhiyun 				 t->data + QUERY_CMD_DATA_LEN,
964*4882a593Smuzhiyun 				 count - QUERY_CMD_DATA_LEN);
965*4882a593Smuzhiyun 	} else
966*4882a593Smuzhiyun 		len = -EINVAL;
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	if (len < 0) {
969*4882a593Smuzhiyun 		put_multi_transaction(t);
970*4882a593Smuzhiyun 		return len;
971*4882a593Smuzhiyun 	}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	multi_transaction_set(file, t, len);
974*4882a593Smuzhiyun 
975*4882a593Smuzhiyun 	return count;
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun 
978*4882a593Smuzhiyun static const struct file_operations aa_sfs_access = {
979*4882a593Smuzhiyun 	.write		= aa_write_access,
980*4882a593Smuzhiyun 	.read		= multi_transaction_read,
981*4882a593Smuzhiyun 	.release	= multi_transaction_release,
982*4882a593Smuzhiyun 	.llseek		= generic_file_llseek,
983*4882a593Smuzhiyun };
984*4882a593Smuzhiyun 
aa_sfs_seq_show(struct seq_file * seq,void * v)985*4882a593Smuzhiyun static int aa_sfs_seq_show(struct seq_file *seq, void *v)
986*4882a593Smuzhiyun {
987*4882a593Smuzhiyun 	struct aa_sfs_entry *fs_file = seq->private;
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	if (!fs_file)
990*4882a593Smuzhiyun 		return 0;
991*4882a593Smuzhiyun 
992*4882a593Smuzhiyun 	switch (fs_file->v_type) {
993*4882a593Smuzhiyun 	case AA_SFS_TYPE_BOOLEAN:
994*4882a593Smuzhiyun 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
995*4882a593Smuzhiyun 		break;
996*4882a593Smuzhiyun 	case AA_SFS_TYPE_STRING:
997*4882a593Smuzhiyun 		seq_printf(seq, "%s\n", fs_file->v.string);
998*4882a593Smuzhiyun 		break;
999*4882a593Smuzhiyun 	case AA_SFS_TYPE_U64:
1000*4882a593Smuzhiyun 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
1001*4882a593Smuzhiyun 		break;
1002*4882a593Smuzhiyun 	default:
1003*4882a593Smuzhiyun 		/* Ignore unpritable entry types. */
1004*4882a593Smuzhiyun 		break;
1005*4882a593Smuzhiyun 	}
1006*4882a593Smuzhiyun 
1007*4882a593Smuzhiyun 	return 0;
1008*4882a593Smuzhiyun }
1009*4882a593Smuzhiyun 
aa_sfs_seq_open(struct inode * inode,struct file * file)1010*4882a593Smuzhiyun static int aa_sfs_seq_open(struct inode *inode, struct file *file)
1011*4882a593Smuzhiyun {
1012*4882a593Smuzhiyun 	return single_open(file, aa_sfs_seq_show, inode->i_private);
1013*4882a593Smuzhiyun }
1014*4882a593Smuzhiyun 
1015*4882a593Smuzhiyun const struct file_operations aa_sfs_seq_file_ops = {
1016*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
1017*4882a593Smuzhiyun 	.open		= aa_sfs_seq_open,
1018*4882a593Smuzhiyun 	.read		= seq_read,
1019*4882a593Smuzhiyun 	.llseek		= seq_lseek,
1020*4882a593Smuzhiyun 	.release	= single_release,
1021*4882a593Smuzhiyun };
1022*4882a593Smuzhiyun 
1023*4882a593Smuzhiyun /*
1024*4882a593Smuzhiyun  * profile based file operations
1025*4882a593Smuzhiyun  *     policy/profiles/XXXX/profiles/ *
1026*4882a593Smuzhiyun  */
1027*4882a593Smuzhiyun 
1028*4882a593Smuzhiyun #define SEQ_PROFILE_FOPS(NAME)						      \
1029*4882a593Smuzhiyun static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
1030*4882a593Smuzhiyun {									      \
1031*4882a593Smuzhiyun 	return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show);    \
1032*4882a593Smuzhiyun }									      \
1033*4882a593Smuzhiyun 									      \
1034*4882a593Smuzhiyun static const struct file_operations seq_profile_ ##NAME ##_fops = {	      \
1035*4882a593Smuzhiyun 	.owner		= THIS_MODULE,					      \
1036*4882a593Smuzhiyun 	.open		= seq_profile_ ##NAME ##_open,			      \
1037*4882a593Smuzhiyun 	.read		= seq_read,					      \
1038*4882a593Smuzhiyun 	.llseek		= seq_lseek,					      \
1039*4882a593Smuzhiyun 	.release	= seq_profile_release,				      \
1040*4882a593Smuzhiyun }									      \
1041*4882a593Smuzhiyun 
seq_profile_open(struct inode * inode,struct file * file,int (* show)(struct seq_file *,void *))1042*4882a593Smuzhiyun static int seq_profile_open(struct inode *inode, struct file *file,
1043*4882a593Smuzhiyun 			    int (*show)(struct seq_file *, void *))
1044*4882a593Smuzhiyun {
1045*4882a593Smuzhiyun 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
1046*4882a593Smuzhiyun 	int error = single_open(file, show, proxy);
1047*4882a593Smuzhiyun 
1048*4882a593Smuzhiyun 	if (error) {
1049*4882a593Smuzhiyun 		file->private_data = NULL;
1050*4882a593Smuzhiyun 		aa_put_proxy(proxy);
1051*4882a593Smuzhiyun 	}
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	return error;
1054*4882a593Smuzhiyun }
1055*4882a593Smuzhiyun 
seq_profile_release(struct inode * inode,struct file * file)1056*4882a593Smuzhiyun static int seq_profile_release(struct inode *inode, struct file *file)
1057*4882a593Smuzhiyun {
1058*4882a593Smuzhiyun 	struct seq_file *seq = (struct seq_file *) file->private_data;
1059*4882a593Smuzhiyun 	if (seq)
1060*4882a593Smuzhiyun 		aa_put_proxy(seq->private);
1061*4882a593Smuzhiyun 	return single_release(inode, file);
1062*4882a593Smuzhiyun }
1063*4882a593Smuzhiyun 
seq_profile_name_show(struct seq_file * seq,void * v)1064*4882a593Smuzhiyun static int seq_profile_name_show(struct seq_file *seq, void *v)
1065*4882a593Smuzhiyun {
1066*4882a593Smuzhiyun 	struct aa_proxy *proxy = seq->private;
1067*4882a593Smuzhiyun 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1068*4882a593Smuzhiyun 	struct aa_profile *profile = labels_profile(label);
1069*4882a593Smuzhiyun 	seq_printf(seq, "%s\n", profile->base.name);
1070*4882a593Smuzhiyun 	aa_put_label(label);
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	return 0;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun 
seq_profile_mode_show(struct seq_file * seq,void * v)1075*4882a593Smuzhiyun static int seq_profile_mode_show(struct seq_file *seq, void *v)
1076*4882a593Smuzhiyun {
1077*4882a593Smuzhiyun 	struct aa_proxy *proxy = seq->private;
1078*4882a593Smuzhiyun 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1079*4882a593Smuzhiyun 	struct aa_profile *profile = labels_profile(label);
1080*4882a593Smuzhiyun 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
1081*4882a593Smuzhiyun 	aa_put_label(label);
1082*4882a593Smuzhiyun 
1083*4882a593Smuzhiyun 	return 0;
1084*4882a593Smuzhiyun }
1085*4882a593Smuzhiyun 
seq_profile_attach_show(struct seq_file * seq,void * v)1086*4882a593Smuzhiyun static int seq_profile_attach_show(struct seq_file *seq, void *v)
1087*4882a593Smuzhiyun {
1088*4882a593Smuzhiyun 	struct aa_proxy *proxy = seq->private;
1089*4882a593Smuzhiyun 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1090*4882a593Smuzhiyun 	struct aa_profile *profile = labels_profile(label);
1091*4882a593Smuzhiyun 	if (profile->attach)
1092*4882a593Smuzhiyun 		seq_printf(seq, "%s\n", profile->attach);
1093*4882a593Smuzhiyun 	else if (profile->xmatch)
1094*4882a593Smuzhiyun 		seq_puts(seq, "<unknown>\n");
1095*4882a593Smuzhiyun 	else
1096*4882a593Smuzhiyun 		seq_printf(seq, "%s\n", profile->base.name);
1097*4882a593Smuzhiyun 	aa_put_label(label);
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	return 0;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun 
seq_profile_hash_show(struct seq_file * seq,void * v)1102*4882a593Smuzhiyun static int seq_profile_hash_show(struct seq_file *seq, void *v)
1103*4882a593Smuzhiyun {
1104*4882a593Smuzhiyun 	struct aa_proxy *proxy = seq->private;
1105*4882a593Smuzhiyun 	struct aa_label *label = aa_get_label_rcu(&proxy->label);
1106*4882a593Smuzhiyun 	struct aa_profile *profile = labels_profile(label);
1107*4882a593Smuzhiyun 	unsigned int i, size = aa_hash_size();
1108*4882a593Smuzhiyun 
1109*4882a593Smuzhiyun 	if (profile->hash) {
1110*4882a593Smuzhiyun 		for (i = 0; i < size; i++)
1111*4882a593Smuzhiyun 			seq_printf(seq, "%.2x", profile->hash[i]);
1112*4882a593Smuzhiyun 		seq_putc(seq, '\n');
1113*4882a593Smuzhiyun 	}
1114*4882a593Smuzhiyun 	aa_put_label(label);
1115*4882a593Smuzhiyun 
1116*4882a593Smuzhiyun 	return 0;
1117*4882a593Smuzhiyun }
1118*4882a593Smuzhiyun 
1119*4882a593Smuzhiyun SEQ_PROFILE_FOPS(name);
1120*4882a593Smuzhiyun SEQ_PROFILE_FOPS(mode);
1121*4882a593Smuzhiyun SEQ_PROFILE_FOPS(attach);
1122*4882a593Smuzhiyun SEQ_PROFILE_FOPS(hash);
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun /*
1125*4882a593Smuzhiyun  * namespace based files
1126*4882a593Smuzhiyun  *     several root files and
1127*4882a593Smuzhiyun  *     policy/ *
1128*4882a593Smuzhiyun  */
1129*4882a593Smuzhiyun 
1130*4882a593Smuzhiyun #define SEQ_NS_FOPS(NAME)						      \
1131*4882a593Smuzhiyun static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file)     \
1132*4882a593Smuzhiyun {									      \
1133*4882a593Smuzhiyun 	return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private);   \
1134*4882a593Smuzhiyun }									      \
1135*4882a593Smuzhiyun 									      \
1136*4882a593Smuzhiyun static const struct file_operations seq_ns_ ##NAME ##_fops = {	      \
1137*4882a593Smuzhiyun 	.owner		= THIS_MODULE,					      \
1138*4882a593Smuzhiyun 	.open		= seq_ns_ ##NAME ##_open,			      \
1139*4882a593Smuzhiyun 	.read		= seq_read,					      \
1140*4882a593Smuzhiyun 	.llseek		= seq_lseek,					      \
1141*4882a593Smuzhiyun 	.release	= single_release,				      \
1142*4882a593Smuzhiyun }									      \
1143*4882a593Smuzhiyun 
seq_ns_stacked_show(struct seq_file * seq,void * v)1144*4882a593Smuzhiyun static int seq_ns_stacked_show(struct seq_file *seq, void *v)
1145*4882a593Smuzhiyun {
1146*4882a593Smuzhiyun 	struct aa_label *label;
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun 	label = begin_current_label_crit_section();
1149*4882a593Smuzhiyun 	seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no");
1150*4882a593Smuzhiyun 	end_current_label_crit_section(label);
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	return 0;
1153*4882a593Smuzhiyun }
1154*4882a593Smuzhiyun 
seq_ns_nsstacked_show(struct seq_file * seq,void * v)1155*4882a593Smuzhiyun static int seq_ns_nsstacked_show(struct seq_file *seq, void *v)
1156*4882a593Smuzhiyun {
1157*4882a593Smuzhiyun 	struct aa_label *label;
1158*4882a593Smuzhiyun 	struct aa_profile *profile;
1159*4882a593Smuzhiyun 	struct label_it it;
1160*4882a593Smuzhiyun 	int count = 1;
1161*4882a593Smuzhiyun 
1162*4882a593Smuzhiyun 	label = begin_current_label_crit_section();
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	if (label->size > 1) {
1165*4882a593Smuzhiyun 		label_for_each(it, label, profile)
1166*4882a593Smuzhiyun 			if (profile->ns != labels_ns(label)) {
1167*4882a593Smuzhiyun 				count++;
1168*4882a593Smuzhiyun 				break;
1169*4882a593Smuzhiyun 			}
1170*4882a593Smuzhiyun 	}
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	seq_printf(seq, "%s\n", count > 1 ? "yes" : "no");
1173*4882a593Smuzhiyun 	end_current_label_crit_section(label);
1174*4882a593Smuzhiyun 
1175*4882a593Smuzhiyun 	return 0;
1176*4882a593Smuzhiyun }
1177*4882a593Smuzhiyun 
seq_ns_level_show(struct seq_file * seq,void * v)1178*4882a593Smuzhiyun static int seq_ns_level_show(struct seq_file *seq, void *v)
1179*4882a593Smuzhiyun {
1180*4882a593Smuzhiyun 	struct aa_label *label;
1181*4882a593Smuzhiyun 
1182*4882a593Smuzhiyun 	label = begin_current_label_crit_section();
1183*4882a593Smuzhiyun 	seq_printf(seq, "%d\n", labels_ns(label)->level);
1184*4882a593Smuzhiyun 	end_current_label_crit_section(label);
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	return 0;
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun 
seq_ns_name_show(struct seq_file * seq,void * v)1189*4882a593Smuzhiyun static int seq_ns_name_show(struct seq_file *seq, void *v)
1190*4882a593Smuzhiyun {
1191*4882a593Smuzhiyun 	struct aa_label *label = begin_current_label_crit_section();
1192*4882a593Smuzhiyun 	seq_printf(seq, "%s\n", labels_ns(label)->base.name);
1193*4882a593Smuzhiyun 	end_current_label_crit_section(label);
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	return 0;
1196*4882a593Smuzhiyun }
1197*4882a593Smuzhiyun 
1198*4882a593Smuzhiyun SEQ_NS_FOPS(stacked);
1199*4882a593Smuzhiyun SEQ_NS_FOPS(nsstacked);
1200*4882a593Smuzhiyun SEQ_NS_FOPS(level);
1201*4882a593Smuzhiyun SEQ_NS_FOPS(name);
1202*4882a593Smuzhiyun 
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun /* policy/raw_data/ * file ops */
1205*4882a593Smuzhiyun 
1206*4882a593Smuzhiyun #define SEQ_RAWDATA_FOPS(NAME)						      \
1207*4882a593Smuzhiyun static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
1208*4882a593Smuzhiyun {									      \
1209*4882a593Smuzhiyun 	return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show);    \
1210*4882a593Smuzhiyun }									      \
1211*4882a593Smuzhiyun 									      \
1212*4882a593Smuzhiyun static const struct file_operations seq_rawdata_ ##NAME ##_fops = {	      \
1213*4882a593Smuzhiyun 	.owner		= THIS_MODULE,					      \
1214*4882a593Smuzhiyun 	.open		= seq_rawdata_ ##NAME ##_open,			      \
1215*4882a593Smuzhiyun 	.read		= seq_read,					      \
1216*4882a593Smuzhiyun 	.llseek		= seq_lseek,					      \
1217*4882a593Smuzhiyun 	.release	= seq_rawdata_release,				      \
1218*4882a593Smuzhiyun }									      \
1219*4882a593Smuzhiyun 
seq_rawdata_open(struct inode * inode,struct file * file,int (* show)(struct seq_file *,void *))1220*4882a593Smuzhiyun static int seq_rawdata_open(struct inode *inode, struct file *file,
1221*4882a593Smuzhiyun 			    int (*show)(struct seq_file *, void *))
1222*4882a593Smuzhiyun {
1223*4882a593Smuzhiyun 	struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
1224*4882a593Smuzhiyun 	int error;
1225*4882a593Smuzhiyun 
1226*4882a593Smuzhiyun 	if (!data)
1227*4882a593Smuzhiyun 		/* lost race this ent is being reaped */
1228*4882a593Smuzhiyun 		return -ENOENT;
1229*4882a593Smuzhiyun 
1230*4882a593Smuzhiyun 	error = single_open(file, show, data);
1231*4882a593Smuzhiyun 	if (error) {
1232*4882a593Smuzhiyun 		AA_BUG(file->private_data &&
1233*4882a593Smuzhiyun 		       ((struct seq_file *)file->private_data)->private);
1234*4882a593Smuzhiyun 		aa_put_loaddata(data);
1235*4882a593Smuzhiyun 	}
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	return error;
1238*4882a593Smuzhiyun }
1239*4882a593Smuzhiyun 
seq_rawdata_release(struct inode * inode,struct file * file)1240*4882a593Smuzhiyun static int seq_rawdata_release(struct inode *inode, struct file *file)
1241*4882a593Smuzhiyun {
1242*4882a593Smuzhiyun 	struct seq_file *seq = (struct seq_file *) file->private_data;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 	if (seq)
1245*4882a593Smuzhiyun 		aa_put_loaddata(seq->private);
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	return single_release(inode, file);
1248*4882a593Smuzhiyun }
1249*4882a593Smuzhiyun 
seq_rawdata_abi_show(struct seq_file * seq,void * v)1250*4882a593Smuzhiyun static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
1251*4882a593Smuzhiyun {
1252*4882a593Smuzhiyun 	struct aa_loaddata *data = seq->private;
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun 	seq_printf(seq, "v%d\n", data->abi);
1255*4882a593Smuzhiyun 
1256*4882a593Smuzhiyun 	return 0;
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun 
seq_rawdata_revision_show(struct seq_file * seq,void * v)1259*4882a593Smuzhiyun static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
1260*4882a593Smuzhiyun {
1261*4882a593Smuzhiyun 	struct aa_loaddata *data = seq->private;
1262*4882a593Smuzhiyun 
1263*4882a593Smuzhiyun 	seq_printf(seq, "%ld\n", data->revision);
1264*4882a593Smuzhiyun 
1265*4882a593Smuzhiyun 	return 0;
1266*4882a593Smuzhiyun }
1267*4882a593Smuzhiyun 
seq_rawdata_hash_show(struct seq_file * seq,void * v)1268*4882a593Smuzhiyun static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun 	struct aa_loaddata *data = seq->private;
1271*4882a593Smuzhiyun 	unsigned int i, size = aa_hash_size();
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	if (data->hash) {
1274*4882a593Smuzhiyun 		for (i = 0; i < size; i++)
1275*4882a593Smuzhiyun 			seq_printf(seq, "%.2x", data->hash[i]);
1276*4882a593Smuzhiyun 		seq_putc(seq, '\n');
1277*4882a593Smuzhiyun 	}
1278*4882a593Smuzhiyun 
1279*4882a593Smuzhiyun 	return 0;
1280*4882a593Smuzhiyun }
1281*4882a593Smuzhiyun 
seq_rawdata_compressed_size_show(struct seq_file * seq,void * v)1282*4882a593Smuzhiyun static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v)
1283*4882a593Smuzhiyun {
1284*4882a593Smuzhiyun 	struct aa_loaddata *data = seq->private;
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	seq_printf(seq, "%zu\n", data->compressed_size);
1287*4882a593Smuzhiyun 
1288*4882a593Smuzhiyun 	return 0;
1289*4882a593Smuzhiyun }
1290*4882a593Smuzhiyun 
1291*4882a593Smuzhiyun SEQ_RAWDATA_FOPS(abi);
1292*4882a593Smuzhiyun SEQ_RAWDATA_FOPS(revision);
1293*4882a593Smuzhiyun SEQ_RAWDATA_FOPS(hash);
1294*4882a593Smuzhiyun SEQ_RAWDATA_FOPS(compressed_size);
1295*4882a593Smuzhiyun 
deflate_decompress(char * src,size_t slen,char * dst,size_t dlen)1296*4882a593Smuzhiyun static int deflate_decompress(char *src, size_t slen, char *dst, size_t dlen)
1297*4882a593Smuzhiyun {
1298*4882a593Smuzhiyun 	int error;
1299*4882a593Smuzhiyun 	struct z_stream_s strm;
1300*4882a593Smuzhiyun 
1301*4882a593Smuzhiyun 	if (aa_g_rawdata_compression_level == 0) {
1302*4882a593Smuzhiyun 		if (dlen < slen)
1303*4882a593Smuzhiyun 			return -EINVAL;
1304*4882a593Smuzhiyun 		memcpy(dst, src, slen);
1305*4882a593Smuzhiyun 		return 0;
1306*4882a593Smuzhiyun 	}
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 	memset(&strm, 0, sizeof(strm));
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	strm.workspace = kvzalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
1311*4882a593Smuzhiyun 	if (!strm.workspace)
1312*4882a593Smuzhiyun 		return -ENOMEM;
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	strm.next_in = src;
1315*4882a593Smuzhiyun 	strm.avail_in = slen;
1316*4882a593Smuzhiyun 
1317*4882a593Smuzhiyun 	error = zlib_inflateInit(&strm);
1318*4882a593Smuzhiyun 	if (error != Z_OK) {
1319*4882a593Smuzhiyun 		error = -ENOMEM;
1320*4882a593Smuzhiyun 		goto fail_inflate_init;
1321*4882a593Smuzhiyun 	}
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	strm.next_out = dst;
1324*4882a593Smuzhiyun 	strm.avail_out = dlen;
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	error = zlib_inflate(&strm, Z_FINISH);
1327*4882a593Smuzhiyun 	if (error != Z_STREAM_END)
1328*4882a593Smuzhiyun 		error = -EINVAL;
1329*4882a593Smuzhiyun 	else
1330*4882a593Smuzhiyun 		error = 0;
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	zlib_inflateEnd(&strm);
1333*4882a593Smuzhiyun fail_inflate_init:
1334*4882a593Smuzhiyun 	kvfree(strm.workspace);
1335*4882a593Smuzhiyun 	return error;
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun 
rawdata_read(struct file * file,char __user * buf,size_t size,loff_t * ppos)1338*4882a593Smuzhiyun static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
1339*4882a593Smuzhiyun 			    loff_t *ppos)
1340*4882a593Smuzhiyun {
1341*4882a593Smuzhiyun 	struct rawdata_f_data *private = file->private_data;
1342*4882a593Smuzhiyun 
1343*4882a593Smuzhiyun 	return simple_read_from_buffer(buf, size, ppos,
1344*4882a593Smuzhiyun 				       RAWDATA_F_DATA_BUF(private),
1345*4882a593Smuzhiyun 				       private->loaddata->size);
1346*4882a593Smuzhiyun }
1347*4882a593Smuzhiyun 
rawdata_release(struct inode * inode,struct file * file)1348*4882a593Smuzhiyun static int rawdata_release(struct inode *inode, struct file *file)
1349*4882a593Smuzhiyun {
1350*4882a593Smuzhiyun 	rawdata_f_data_free(file->private_data);
1351*4882a593Smuzhiyun 
1352*4882a593Smuzhiyun 	return 0;
1353*4882a593Smuzhiyun }
1354*4882a593Smuzhiyun 
rawdata_open(struct inode * inode,struct file * file)1355*4882a593Smuzhiyun static int rawdata_open(struct inode *inode, struct file *file)
1356*4882a593Smuzhiyun {
1357*4882a593Smuzhiyun 	int error;
1358*4882a593Smuzhiyun 	struct aa_loaddata *loaddata;
1359*4882a593Smuzhiyun 	struct rawdata_f_data *private;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun 	if (!policy_view_capable(NULL))
1362*4882a593Smuzhiyun 		return -EACCES;
1363*4882a593Smuzhiyun 
1364*4882a593Smuzhiyun 	loaddata = __aa_get_loaddata(inode->i_private);
1365*4882a593Smuzhiyun 	if (!loaddata)
1366*4882a593Smuzhiyun 		/* lost race: this entry is being reaped */
1367*4882a593Smuzhiyun 		return -ENOENT;
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun 	private = rawdata_f_data_alloc(loaddata->size);
1370*4882a593Smuzhiyun 	if (IS_ERR(private)) {
1371*4882a593Smuzhiyun 		error = PTR_ERR(private);
1372*4882a593Smuzhiyun 		goto fail_private_alloc;
1373*4882a593Smuzhiyun 	}
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	private->loaddata = loaddata;
1376*4882a593Smuzhiyun 
1377*4882a593Smuzhiyun 	error = deflate_decompress(loaddata->data, loaddata->compressed_size,
1378*4882a593Smuzhiyun 				   RAWDATA_F_DATA_BUF(private),
1379*4882a593Smuzhiyun 				   loaddata->size);
1380*4882a593Smuzhiyun 	if (error)
1381*4882a593Smuzhiyun 		goto fail_decompress;
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun 	file->private_data = private;
1384*4882a593Smuzhiyun 	return 0;
1385*4882a593Smuzhiyun 
1386*4882a593Smuzhiyun fail_decompress:
1387*4882a593Smuzhiyun 	rawdata_f_data_free(private);
1388*4882a593Smuzhiyun 	return error;
1389*4882a593Smuzhiyun 
1390*4882a593Smuzhiyun fail_private_alloc:
1391*4882a593Smuzhiyun 	aa_put_loaddata(loaddata);
1392*4882a593Smuzhiyun 	return error;
1393*4882a593Smuzhiyun }
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun static const struct file_operations rawdata_fops = {
1396*4882a593Smuzhiyun 	.open = rawdata_open,
1397*4882a593Smuzhiyun 	.read = rawdata_read,
1398*4882a593Smuzhiyun 	.llseek = generic_file_llseek,
1399*4882a593Smuzhiyun 	.release = rawdata_release,
1400*4882a593Smuzhiyun };
1401*4882a593Smuzhiyun 
remove_rawdata_dents(struct aa_loaddata * rawdata)1402*4882a593Smuzhiyun static void remove_rawdata_dents(struct aa_loaddata *rawdata)
1403*4882a593Smuzhiyun {
1404*4882a593Smuzhiyun 	int i;
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun 	for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
1407*4882a593Smuzhiyun 		if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
1408*4882a593Smuzhiyun 			/* no refcounts on i_private */
1409*4882a593Smuzhiyun 			aafs_remove(rawdata->dents[i]);
1410*4882a593Smuzhiyun 			rawdata->dents[i] = NULL;
1411*4882a593Smuzhiyun 		}
1412*4882a593Smuzhiyun 	}
1413*4882a593Smuzhiyun }
1414*4882a593Smuzhiyun 
__aa_fs_remove_rawdata(struct aa_loaddata * rawdata)1415*4882a593Smuzhiyun void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
1416*4882a593Smuzhiyun {
1417*4882a593Smuzhiyun 	AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun 	if (rawdata->ns) {
1420*4882a593Smuzhiyun 		remove_rawdata_dents(rawdata);
1421*4882a593Smuzhiyun 		list_del_init(&rawdata->list);
1422*4882a593Smuzhiyun 		aa_put_ns(rawdata->ns);
1423*4882a593Smuzhiyun 		rawdata->ns = NULL;
1424*4882a593Smuzhiyun 	}
1425*4882a593Smuzhiyun }
1426*4882a593Smuzhiyun 
__aa_fs_create_rawdata(struct aa_ns * ns,struct aa_loaddata * rawdata)1427*4882a593Smuzhiyun int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
1428*4882a593Smuzhiyun {
1429*4882a593Smuzhiyun 	struct dentry *dent, *dir;
1430*4882a593Smuzhiyun 
1431*4882a593Smuzhiyun 	AA_BUG(!ns);
1432*4882a593Smuzhiyun 	AA_BUG(!rawdata);
1433*4882a593Smuzhiyun 	AA_BUG(!mutex_is_locked(&ns->lock));
1434*4882a593Smuzhiyun 	AA_BUG(!ns_subdata_dir(ns));
1435*4882a593Smuzhiyun 
1436*4882a593Smuzhiyun 	/*
1437*4882a593Smuzhiyun 	 * just use ns revision dir was originally created at. This is
1438*4882a593Smuzhiyun 	 * under ns->lock and if load is successful revision will be
1439*4882a593Smuzhiyun 	 * bumped and is guaranteed to be unique
1440*4882a593Smuzhiyun 	 */
1441*4882a593Smuzhiyun 	rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
1442*4882a593Smuzhiyun 	if (!rawdata->name)
1443*4882a593Smuzhiyun 		return -ENOMEM;
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
1446*4882a593Smuzhiyun 	if (IS_ERR(dir))
1447*4882a593Smuzhiyun 		/* ->name freed when rawdata freed */
1448*4882a593Smuzhiyun 		return PTR_ERR(dir);
1449*4882a593Smuzhiyun 	rawdata->dents[AAFS_LOADDATA_DIR] = dir;
1450*4882a593Smuzhiyun 
1451*4882a593Smuzhiyun 	dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
1452*4882a593Smuzhiyun 				      &seq_rawdata_abi_fops);
1453*4882a593Smuzhiyun 	if (IS_ERR(dent))
1454*4882a593Smuzhiyun 		goto fail;
1455*4882a593Smuzhiyun 	rawdata->dents[AAFS_LOADDATA_ABI] = dent;
1456*4882a593Smuzhiyun 
1457*4882a593Smuzhiyun 	dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
1458*4882a593Smuzhiyun 				      &seq_rawdata_revision_fops);
1459*4882a593Smuzhiyun 	if (IS_ERR(dent))
1460*4882a593Smuzhiyun 		goto fail;
1461*4882a593Smuzhiyun 	rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
1462*4882a593Smuzhiyun 
1463*4882a593Smuzhiyun 	if (aa_g_hash_policy) {
1464*4882a593Smuzhiyun 		dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
1465*4882a593Smuzhiyun 					      rawdata, &seq_rawdata_hash_fops);
1466*4882a593Smuzhiyun 		if (IS_ERR(dent))
1467*4882a593Smuzhiyun 			goto fail;
1468*4882a593Smuzhiyun 		rawdata->dents[AAFS_LOADDATA_HASH] = dent;
1469*4882a593Smuzhiyun 	}
1470*4882a593Smuzhiyun 
1471*4882a593Smuzhiyun 	dent = aafs_create_file("compressed_size", S_IFREG | 0444, dir,
1472*4882a593Smuzhiyun 				rawdata,
1473*4882a593Smuzhiyun 				&seq_rawdata_compressed_size_fops);
1474*4882a593Smuzhiyun 	if (IS_ERR(dent))
1475*4882a593Smuzhiyun 		goto fail;
1476*4882a593Smuzhiyun 	rawdata->dents[AAFS_LOADDATA_COMPRESSED_SIZE] = dent;
1477*4882a593Smuzhiyun 
1478*4882a593Smuzhiyun 	dent = aafs_create_file("raw_data", S_IFREG | 0444,
1479*4882a593Smuzhiyun 				      dir, rawdata, &rawdata_fops);
1480*4882a593Smuzhiyun 	if (IS_ERR(dent))
1481*4882a593Smuzhiyun 		goto fail;
1482*4882a593Smuzhiyun 	rawdata->dents[AAFS_LOADDATA_DATA] = dent;
1483*4882a593Smuzhiyun 	d_inode(dent)->i_size = rawdata->size;
1484*4882a593Smuzhiyun 
1485*4882a593Smuzhiyun 	rawdata->ns = aa_get_ns(ns);
1486*4882a593Smuzhiyun 	list_add(&rawdata->list, &ns->rawdata_list);
1487*4882a593Smuzhiyun 	/* no refcount on inode rawdata */
1488*4882a593Smuzhiyun 
1489*4882a593Smuzhiyun 	return 0;
1490*4882a593Smuzhiyun 
1491*4882a593Smuzhiyun fail:
1492*4882a593Smuzhiyun 	remove_rawdata_dents(rawdata);
1493*4882a593Smuzhiyun 
1494*4882a593Smuzhiyun 	return PTR_ERR(dent);
1495*4882a593Smuzhiyun }
1496*4882a593Smuzhiyun 
1497*4882a593Smuzhiyun /** fns to setup dynamic per profile/namespace files **/
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun /**
1500*4882a593Smuzhiyun  *
1501*4882a593Smuzhiyun  * Requires: @profile->ns->lock held
1502*4882a593Smuzhiyun  */
__aafs_profile_rmdir(struct aa_profile * profile)1503*4882a593Smuzhiyun void __aafs_profile_rmdir(struct aa_profile *profile)
1504*4882a593Smuzhiyun {
1505*4882a593Smuzhiyun 	struct aa_profile *child;
1506*4882a593Smuzhiyun 	int i;
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun 	if (!profile)
1509*4882a593Smuzhiyun 		return;
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun 	list_for_each_entry(child, &profile->base.profiles, base.list)
1512*4882a593Smuzhiyun 		__aafs_profile_rmdir(child);
1513*4882a593Smuzhiyun 
1514*4882a593Smuzhiyun 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
1515*4882a593Smuzhiyun 		struct aa_proxy *proxy;
1516*4882a593Smuzhiyun 		if (!profile->dents[i])
1517*4882a593Smuzhiyun 			continue;
1518*4882a593Smuzhiyun 
1519*4882a593Smuzhiyun 		proxy = d_inode(profile->dents[i])->i_private;
1520*4882a593Smuzhiyun 		aafs_remove(profile->dents[i]);
1521*4882a593Smuzhiyun 		aa_put_proxy(proxy);
1522*4882a593Smuzhiyun 		profile->dents[i] = NULL;
1523*4882a593Smuzhiyun 	}
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun /**
1527*4882a593Smuzhiyun  *
1528*4882a593Smuzhiyun  * Requires: @old->ns->lock held
1529*4882a593Smuzhiyun  */
__aafs_profile_migrate_dents(struct aa_profile * old,struct aa_profile * new)1530*4882a593Smuzhiyun void __aafs_profile_migrate_dents(struct aa_profile *old,
1531*4882a593Smuzhiyun 				  struct aa_profile *new)
1532*4882a593Smuzhiyun {
1533*4882a593Smuzhiyun 	int i;
1534*4882a593Smuzhiyun 
1535*4882a593Smuzhiyun 	AA_BUG(!old);
1536*4882a593Smuzhiyun 	AA_BUG(!new);
1537*4882a593Smuzhiyun 	AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock));
1538*4882a593Smuzhiyun 
1539*4882a593Smuzhiyun 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
1540*4882a593Smuzhiyun 		new->dents[i] = old->dents[i];
1541*4882a593Smuzhiyun 		if (new->dents[i])
1542*4882a593Smuzhiyun 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
1543*4882a593Smuzhiyun 		old->dents[i] = NULL;
1544*4882a593Smuzhiyun 	}
1545*4882a593Smuzhiyun }
1546*4882a593Smuzhiyun 
create_profile_file(struct dentry * dir,const char * name,struct aa_profile * profile,const struct file_operations * fops)1547*4882a593Smuzhiyun static struct dentry *create_profile_file(struct dentry *dir, const char *name,
1548*4882a593Smuzhiyun 					  struct aa_profile *profile,
1549*4882a593Smuzhiyun 					  const struct file_operations *fops)
1550*4882a593Smuzhiyun {
1551*4882a593Smuzhiyun 	struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
1552*4882a593Smuzhiyun 	struct dentry *dent;
1553*4882a593Smuzhiyun 
1554*4882a593Smuzhiyun 	dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
1555*4882a593Smuzhiyun 	if (IS_ERR(dent))
1556*4882a593Smuzhiyun 		aa_put_proxy(proxy);
1557*4882a593Smuzhiyun 
1558*4882a593Smuzhiyun 	return dent;
1559*4882a593Smuzhiyun }
1560*4882a593Smuzhiyun 
profile_depth(struct aa_profile * profile)1561*4882a593Smuzhiyun static int profile_depth(struct aa_profile *profile)
1562*4882a593Smuzhiyun {
1563*4882a593Smuzhiyun 	int depth = 0;
1564*4882a593Smuzhiyun 
1565*4882a593Smuzhiyun 	rcu_read_lock();
1566*4882a593Smuzhiyun 	for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
1567*4882a593Smuzhiyun 		depth++;
1568*4882a593Smuzhiyun 	rcu_read_unlock();
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	return depth;
1571*4882a593Smuzhiyun }
1572*4882a593Smuzhiyun 
gen_symlink_name(int depth,const char * dirname,const char * fname)1573*4882a593Smuzhiyun static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
1574*4882a593Smuzhiyun {
1575*4882a593Smuzhiyun 	char *buffer, *s;
1576*4882a593Smuzhiyun 	int error;
1577*4882a593Smuzhiyun 	int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
1578*4882a593Smuzhiyun 
1579*4882a593Smuzhiyun 	s = buffer = kmalloc(size, GFP_KERNEL);
1580*4882a593Smuzhiyun 	if (!buffer)
1581*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1582*4882a593Smuzhiyun 
1583*4882a593Smuzhiyun 	for (; depth > 0; depth--) {
1584*4882a593Smuzhiyun 		strcpy(s, "../../");
1585*4882a593Smuzhiyun 		s += 6;
1586*4882a593Smuzhiyun 		size -= 6;
1587*4882a593Smuzhiyun 	}
1588*4882a593Smuzhiyun 
1589*4882a593Smuzhiyun 	error = snprintf(s, size, "raw_data/%s/%s", dirname, fname);
1590*4882a593Smuzhiyun 	if (error >= size || error < 0) {
1591*4882a593Smuzhiyun 		kfree(buffer);
1592*4882a593Smuzhiyun 		return ERR_PTR(-ENAMETOOLONG);
1593*4882a593Smuzhiyun 	}
1594*4882a593Smuzhiyun 
1595*4882a593Smuzhiyun 	return buffer;
1596*4882a593Smuzhiyun }
1597*4882a593Smuzhiyun 
rawdata_link_cb(void * arg)1598*4882a593Smuzhiyun static void rawdata_link_cb(void *arg)
1599*4882a593Smuzhiyun {
1600*4882a593Smuzhiyun 	kfree(arg);
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun 
rawdata_get_link_base(struct dentry * dentry,struct inode * inode,struct delayed_call * done,const char * name)1603*4882a593Smuzhiyun static const char *rawdata_get_link_base(struct dentry *dentry,
1604*4882a593Smuzhiyun 					 struct inode *inode,
1605*4882a593Smuzhiyun 					 struct delayed_call *done,
1606*4882a593Smuzhiyun 					 const char *name)
1607*4882a593Smuzhiyun {
1608*4882a593Smuzhiyun 	struct aa_proxy *proxy = inode->i_private;
1609*4882a593Smuzhiyun 	struct aa_label *label;
1610*4882a593Smuzhiyun 	struct aa_profile *profile;
1611*4882a593Smuzhiyun 	char *target;
1612*4882a593Smuzhiyun 	int depth;
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun 	if (!dentry)
1615*4882a593Smuzhiyun 		return ERR_PTR(-ECHILD);
1616*4882a593Smuzhiyun 
1617*4882a593Smuzhiyun 	label = aa_get_label_rcu(&proxy->label);
1618*4882a593Smuzhiyun 	profile = labels_profile(label);
1619*4882a593Smuzhiyun 	depth = profile_depth(profile);
1620*4882a593Smuzhiyun 	target = gen_symlink_name(depth, profile->rawdata->name, name);
1621*4882a593Smuzhiyun 	aa_put_label(label);
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun 	if (IS_ERR(target))
1624*4882a593Smuzhiyun 		return target;
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	set_delayed_call(done, rawdata_link_cb, target);
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	return target;
1629*4882a593Smuzhiyun }
1630*4882a593Smuzhiyun 
rawdata_get_link_sha1(struct dentry * dentry,struct inode * inode,struct delayed_call * done)1631*4882a593Smuzhiyun static const char *rawdata_get_link_sha1(struct dentry *dentry,
1632*4882a593Smuzhiyun 					 struct inode *inode,
1633*4882a593Smuzhiyun 					 struct delayed_call *done)
1634*4882a593Smuzhiyun {
1635*4882a593Smuzhiyun 	return rawdata_get_link_base(dentry, inode, done, "sha1");
1636*4882a593Smuzhiyun }
1637*4882a593Smuzhiyun 
rawdata_get_link_abi(struct dentry * dentry,struct inode * inode,struct delayed_call * done)1638*4882a593Smuzhiyun static const char *rawdata_get_link_abi(struct dentry *dentry,
1639*4882a593Smuzhiyun 					struct inode *inode,
1640*4882a593Smuzhiyun 					struct delayed_call *done)
1641*4882a593Smuzhiyun {
1642*4882a593Smuzhiyun 	return rawdata_get_link_base(dentry, inode, done, "abi");
1643*4882a593Smuzhiyun }
1644*4882a593Smuzhiyun 
rawdata_get_link_data(struct dentry * dentry,struct inode * inode,struct delayed_call * done)1645*4882a593Smuzhiyun static const char *rawdata_get_link_data(struct dentry *dentry,
1646*4882a593Smuzhiyun 					 struct inode *inode,
1647*4882a593Smuzhiyun 					 struct delayed_call *done)
1648*4882a593Smuzhiyun {
1649*4882a593Smuzhiyun 	return rawdata_get_link_base(dentry, inode, done, "raw_data");
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun 
1652*4882a593Smuzhiyun static const struct inode_operations rawdata_link_sha1_iops = {
1653*4882a593Smuzhiyun 	.get_link	= rawdata_get_link_sha1,
1654*4882a593Smuzhiyun };
1655*4882a593Smuzhiyun 
1656*4882a593Smuzhiyun static const struct inode_operations rawdata_link_abi_iops = {
1657*4882a593Smuzhiyun 	.get_link	= rawdata_get_link_abi,
1658*4882a593Smuzhiyun };
1659*4882a593Smuzhiyun static const struct inode_operations rawdata_link_data_iops = {
1660*4882a593Smuzhiyun 	.get_link	= rawdata_get_link_data,
1661*4882a593Smuzhiyun };
1662*4882a593Smuzhiyun 
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun /*
1665*4882a593Smuzhiyun  * Requires: @profile->ns->lock held
1666*4882a593Smuzhiyun  */
__aafs_profile_mkdir(struct aa_profile * profile,struct dentry * parent)1667*4882a593Smuzhiyun int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
1668*4882a593Smuzhiyun {
1669*4882a593Smuzhiyun 	struct aa_profile *child;
1670*4882a593Smuzhiyun 	struct dentry *dent = NULL, *dir;
1671*4882a593Smuzhiyun 	int error;
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun 	AA_BUG(!profile);
1674*4882a593Smuzhiyun 	AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock));
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun 	if (!parent) {
1677*4882a593Smuzhiyun 		struct aa_profile *p;
1678*4882a593Smuzhiyun 		p = aa_deref_parent(profile);
1679*4882a593Smuzhiyun 		dent = prof_dir(p);
1680*4882a593Smuzhiyun 		/* adding to parent that previously didn't have children */
1681*4882a593Smuzhiyun 		dent = aafs_create_dir("profiles", dent);
1682*4882a593Smuzhiyun 		if (IS_ERR(dent))
1683*4882a593Smuzhiyun 			goto fail;
1684*4882a593Smuzhiyun 		prof_child_dir(p) = parent = dent;
1685*4882a593Smuzhiyun 	}
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	if (!profile->dirname) {
1688*4882a593Smuzhiyun 		int len, id_len;
1689*4882a593Smuzhiyun 		len = mangle_name(profile->base.name, NULL);
1690*4882a593Smuzhiyun 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
1691*4882a593Smuzhiyun 
1692*4882a593Smuzhiyun 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
1693*4882a593Smuzhiyun 		if (!profile->dirname) {
1694*4882a593Smuzhiyun 			error = -ENOMEM;
1695*4882a593Smuzhiyun 			goto fail2;
1696*4882a593Smuzhiyun 		}
1697*4882a593Smuzhiyun 
1698*4882a593Smuzhiyun 		mangle_name(profile->base.name, profile->dirname);
1699*4882a593Smuzhiyun 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
1700*4882a593Smuzhiyun 	}
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	dent = aafs_create_dir(profile->dirname, parent);
1703*4882a593Smuzhiyun 	if (IS_ERR(dent))
1704*4882a593Smuzhiyun 		goto fail;
1705*4882a593Smuzhiyun 	prof_dir(profile) = dir = dent;
1706*4882a593Smuzhiyun 
1707*4882a593Smuzhiyun 	dent = create_profile_file(dir, "name", profile,
1708*4882a593Smuzhiyun 				   &seq_profile_name_fops);
1709*4882a593Smuzhiyun 	if (IS_ERR(dent))
1710*4882a593Smuzhiyun 		goto fail;
1711*4882a593Smuzhiyun 	profile->dents[AAFS_PROF_NAME] = dent;
1712*4882a593Smuzhiyun 
1713*4882a593Smuzhiyun 	dent = create_profile_file(dir, "mode", profile,
1714*4882a593Smuzhiyun 				   &seq_profile_mode_fops);
1715*4882a593Smuzhiyun 	if (IS_ERR(dent))
1716*4882a593Smuzhiyun 		goto fail;
1717*4882a593Smuzhiyun 	profile->dents[AAFS_PROF_MODE] = dent;
1718*4882a593Smuzhiyun 
1719*4882a593Smuzhiyun 	dent = create_profile_file(dir, "attach", profile,
1720*4882a593Smuzhiyun 				   &seq_profile_attach_fops);
1721*4882a593Smuzhiyun 	if (IS_ERR(dent))
1722*4882a593Smuzhiyun 		goto fail;
1723*4882a593Smuzhiyun 	profile->dents[AAFS_PROF_ATTACH] = dent;
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 	if (profile->hash) {
1726*4882a593Smuzhiyun 		dent = create_profile_file(dir, "sha1", profile,
1727*4882a593Smuzhiyun 					   &seq_profile_hash_fops);
1728*4882a593Smuzhiyun 		if (IS_ERR(dent))
1729*4882a593Smuzhiyun 			goto fail;
1730*4882a593Smuzhiyun 		profile->dents[AAFS_PROF_HASH] = dent;
1731*4882a593Smuzhiyun 	}
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	if (profile->rawdata) {
1734*4882a593Smuzhiyun 		dent = aafs_create("raw_sha1", S_IFLNK | 0444, dir,
1735*4882a593Smuzhiyun 				   profile->label.proxy, NULL, NULL,
1736*4882a593Smuzhiyun 				   &rawdata_link_sha1_iops);
1737*4882a593Smuzhiyun 		if (IS_ERR(dent))
1738*4882a593Smuzhiyun 			goto fail;
1739*4882a593Smuzhiyun 		aa_get_proxy(profile->label.proxy);
1740*4882a593Smuzhiyun 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
1741*4882a593Smuzhiyun 
1742*4882a593Smuzhiyun 		dent = aafs_create("raw_abi", S_IFLNK | 0444, dir,
1743*4882a593Smuzhiyun 				   profile->label.proxy, NULL, NULL,
1744*4882a593Smuzhiyun 				   &rawdata_link_abi_iops);
1745*4882a593Smuzhiyun 		if (IS_ERR(dent))
1746*4882a593Smuzhiyun 			goto fail;
1747*4882a593Smuzhiyun 		aa_get_proxy(profile->label.proxy);
1748*4882a593Smuzhiyun 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
1749*4882a593Smuzhiyun 
1750*4882a593Smuzhiyun 		dent = aafs_create("raw_data", S_IFLNK | 0444, dir,
1751*4882a593Smuzhiyun 				   profile->label.proxy, NULL, NULL,
1752*4882a593Smuzhiyun 				   &rawdata_link_data_iops);
1753*4882a593Smuzhiyun 		if (IS_ERR(dent))
1754*4882a593Smuzhiyun 			goto fail;
1755*4882a593Smuzhiyun 		aa_get_proxy(profile->label.proxy);
1756*4882a593Smuzhiyun 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
1757*4882a593Smuzhiyun 	}
1758*4882a593Smuzhiyun 
1759*4882a593Smuzhiyun 	list_for_each_entry(child, &profile->base.profiles, base.list) {
1760*4882a593Smuzhiyun 		error = __aafs_profile_mkdir(child, prof_child_dir(profile));
1761*4882a593Smuzhiyun 		if (error)
1762*4882a593Smuzhiyun 			goto fail2;
1763*4882a593Smuzhiyun 	}
1764*4882a593Smuzhiyun 
1765*4882a593Smuzhiyun 	return 0;
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun fail:
1768*4882a593Smuzhiyun 	error = PTR_ERR(dent);
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun fail2:
1771*4882a593Smuzhiyun 	__aafs_profile_rmdir(profile);
1772*4882a593Smuzhiyun 
1773*4882a593Smuzhiyun 	return error;
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun 
ns_mkdir_op(struct inode * dir,struct dentry * dentry,umode_t mode)1776*4882a593Smuzhiyun static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode)
1777*4882a593Smuzhiyun {
1778*4882a593Smuzhiyun 	struct aa_ns *ns, *parent;
1779*4882a593Smuzhiyun 	/* TODO: improve permission check */
1780*4882a593Smuzhiyun 	struct aa_label *label;
1781*4882a593Smuzhiyun 	int error;
1782*4882a593Smuzhiyun 
1783*4882a593Smuzhiyun 	label = begin_current_label_crit_section();
1784*4882a593Smuzhiyun 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1785*4882a593Smuzhiyun 	end_current_label_crit_section(label);
1786*4882a593Smuzhiyun 	if (error)
1787*4882a593Smuzhiyun 		return error;
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	parent = aa_get_ns(dir->i_private);
1790*4882a593Smuzhiyun 	AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
1791*4882a593Smuzhiyun 
1792*4882a593Smuzhiyun 	/* we have to unlock and then relock to get locking order right
1793*4882a593Smuzhiyun 	 * for pin_fs
1794*4882a593Smuzhiyun 	 */
1795*4882a593Smuzhiyun 	inode_unlock(dir);
1796*4882a593Smuzhiyun 	error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
1797*4882a593Smuzhiyun 	mutex_lock_nested(&parent->lock, parent->level);
1798*4882a593Smuzhiyun 	inode_lock_nested(dir, I_MUTEX_PARENT);
1799*4882a593Smuzhiyun 	if (error)
1800*4882a593Smuzhiyun 		goto out;
1801*4882a593Smuzhiyun 
1802*4882a593Smuzhiyun 	error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR,  NULL,
1803*4882a593Smuzhiyun 				     NULL, NULL, NULL);
1804*4882a593Smuzhiyun 	if (error)
1805*4882a593Smuzhiyun 		goto out_pin;
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun 	ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name),
1808*4882a593Smuzhiyun 				    dentry);
1809*4882a593Smuzhiyun 	if (IS_ERR(ns)) {
1810*4882a593Smuzhiyun 		error = PTR_ERR(ns);
1811*4882a593Smuzhiyun 		ns = NULL;
1812*4882a593Smuzhiyun 	}
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun 	aa_put_ns(ns);		/* list ref remains */
1815*4882a593Smuzhiyun out_pin:
1816*4882a593Smuzhiyun 	if (error)
1817*4882a593Smuzhiyun 		simple_release_fs(&aafs_mnt, &aafs_count);
1818*4882a593Smuzhiyun out:
1819*4882a593Smuzhiyun 	mutex_unlock(&parent->lock);
1820*4882a593Smuzhiyun 	aa_put_ns(parent);
1821*4882a593Smuzhiyun 
1822*4882a593Smuzhiyun 	return error;
1823*4882a593Smuzhiyun }
1824*4882a593Smuzhiyun 
ns_rmdir_op(struct inode * dir,struct dentry * dentry)1825*4882a593Smuzhiyun static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
1826*4882a593Smuzhiyun {
1827*4882a593Smuzhiyun 	struct aa_ns *ns, *parent;
1828*4882a593Smuzhiyun 	/* TODO: improve permission check */
1829*4882a593Smuzhiyun 	struct aa_label *label;
1830*4882a593Smuzhiyun 	int error;
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 	label = begin_current_label_crit_section();
1833*4882a593Smuzhiyun 	error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1834*4882a593Smuzhiyun 	end_current_label_crit_section(label);
1835*4882a593Smuzhiyun 	if (error)
1836*4882a593Smuzhiyun 		return error;
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	parent = aa_get_ns(dir->i_private);
1839*4882a593Smuzhiyun 	/* rmdir calls the generic securityfs functions to remove files
1840*4882a593Smuzhiyun 	 * from the apparmor dir. It is up to the apparmor ns locking
1841*4882a593Smuzhiyun 	 * to avoid races.
1842*4882a593Smuzhiyun 	 */
1843*4882a593Smuzhiyun 	inode_unlock(dir);
1844*4882a593Smuzhiyun 	inode_unlock(dentry->d_inode);
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 	mutex_lock_nested(&parent->lock, parent->level);
1847*4882a593Smuzhiyun 	ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name,
1848*4882a593Smuzhiyun 				     dentry->d_name.len));
1849*4882a593Smuzhiyun 	if (!ns) {
1850*4882a593Smuzhiyun 		error = -ENOENT;
1851*4882a593Smuzhiyun 		goto out;
1852*4882a593Smuzhiyun 	}
1853*4882a593Smuzhiyun 	AA_BUG(ns_dir(ns) != dentry);
1854*4882a593Smuzhiyun 
1855*4882a593Smuzhiyun 	__aa_remove_ns(ns);
1856*4882a593Smuzhiyun 	aa_put_ns(ns);
1857*4882a593Smuzhiyun 
1858*4882a593Smuzhiyun out:
1859*4882a593Smuzhiyun 	mutex_unlock(&parent->lock);
1860*4882a593Smuzhiyun 	inode_lock_nested(dir, I_MUTEX_PARENT);
1861*4882a593Smuzhiyun 	inode_lock(dentry->d_inode);
1862*4882a593Smuzhiyun 	aa_put_ns(parent);
1863*4882a593Smuzhiyun 
1864*4882a593Smuzhiyun 	return error;
1865*4882a593Smuzhiyun }
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun static const struct inode_operations ns_dir_inode_operations = {
1868*4882a593Smuzhiyun 	.lookup		= simple_lookup,
1869*4882a593Smuzhiyun 	.mkdir		= ns_mkdir_op,
1870*4882a593Smuzhiyun 	.rmdir		= ns_rmdir_op,
1871*4882a593Smuzhiyun };
1872*4882a593Smuzhiyun 
__aa_fs_list_remove_rawdata(struct aa_ns * ns)1873*4882a593Smuzhiyun static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
1874*4882a593Smuzhiyun {
1875*4882a593Smuzhiyun 	struct aa_loaddata *ent, *tmp;
1876*4882a593Smuzhiyun 
1877*4882a593Smuzhiyun 	AA_BUG(!mutex_is_locked(&ns->lock));
1878*4882a593Smuzhiyun 
1879*4882a593Smuzhiyun 	list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
1880*4882a593Smuzhiyun 		__aa_fs_remove_rawdata(ent);
1881*4882a593Smuzhiyun }
1882*4882a593Smuzhiyun 
1883*4882a593Smuzhiyun /**
1884*4882a593Smuzhiyun  *
1885*4882a593Smuzhiyun  * Requires: @ns->lock held
1886*4882a593Smuzhiyun  */
__aafs_ns_rmdir(struct aa_ns * ns)1887*4882a593Smuzhiyun void __aafs_ns_rmdir(struct aa_ns *ns)
1888*4882a593Smuzhiyun {
1889*4882a593Smuzhiyun 	struct aa_ns *sub;
1890*4882a593Smuzhiyun 	struct aa_profile *child;
1891*4882a593Smuzhiyun 	int i;
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun 	if (!ns)
1894*4882a593Smuzhiyun 		return;
1895*4882a593Smuzhiyun 	AA_BUG(!mutex_is_locked(&ns->lock));
1896*4882a593Smuzhiyun 
1897*4882a593Smuzhiyun 	list_for_each_entry(child, &ns->base.profiles, base.list)
1898*4882a593Smuzhiyun 		__aafs_profile_rmdir(child);
1899*4882a593Smuzhiyun 
1900*4882a593Smuzhiyun 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
1901*4882a593Smuzhiyun 		mutex_lock_nested(&sub->lock, sub->level);
1902*4882a593Smuzhiyun 		__aafs_ns_rmdir(sub);
1903*4882a593Smuzhiyun 		mutex_unlock(&sub->lock);
1904*4882a593Smuzhiyun 	}
1905*4882a593Smuzhiyun 
1906*4882a593Smuzhiyun 	__aa_fs_list_remove_rawdata(ns);
1907*4882a593Smuzhiyun 
1908*4882a593Smuzhiyun 	if (ns_subns_dir(ns)) {
1909*4882a593Smuzhiyun 		sub = d_inode(ns_subns_dir(ns))->i_private;
1910*4882a593Smuzhiyun 		aa_put_ns(sub);
1911*4882a593Smuzhiyun 	}
1912*4882a593Smuzhiyun 	if (ns_subload(ns)) {
1913*4882a593Smuzhiyun 		sub = d_inode(ns_subload(ns))->i_private;
1914*4882a593Smuzhiyun 		aa_put_ns(sub);
1915*4882a593Smuzhiyun 	}
1916*4882a593Smuzhiyun 	if (ns_subreplace(ns)) {
1917*4882a593Smuzhiyun 		sub = d_inode(ns_subreplace(ns))->i_private;
1918*4882a593Smuzhiyun 		aa_put_ns(sub);
1919*4882a593Smuzhiyun 	}
1920*4882a593Smuzhiyun 	if (ns_subremove(ns)) {
1921*4882a593Smuzhiyun 		sub = d_inode(ns_subremove(ns))->i_private;
1922*4882a593Smuzhiyun 		aa_put_ns(sub);
1923*4882a593Smuzhiyun 	}
1924*4882a593Smuzhiyun 	if (ns_subrevision(ns)) {
1925*4882a593Smuzhiyun 		sub = d_inode(ns_subrevision(ns))->i_private;
1926*4882a593Smuzhiyun 		aa_put_ns(sub);
1927*4882a593Smuzhiyun 	}
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
1930*4882a593Smuzhiyun 		aafs_remove(ns->dents[i]);
1931*4882a593Smuzhiyun 		ns->dents[i] = NULL;
1932*4882a593Smuzhiyun 	}
1933*4882a593Smuzhiyun }
1934*4882a593Smuzhiyun 
1935*4882a593Smuzhiyun /* assumes cleanup in caller */
__aafs_ns_mkdir_entries(struct aa_ns * ns,struct dentry * dir)1936*4882a593Smuzhiyun static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
1937*4882a593Smuzhiyun {
1938*4882a593Smuzhiyun 	struct dentry *dent;
1939*4882a593Smuzhiyun 
1940*4882a593Smuzhiyun 	AA_BUG(!ns);
1941*4882a593Smuzhiyun 	AA_BUG(!dir);
1942*4882a593Smuzhiyun 
1943*4882a593Smuzhiyun 	dent = aafs_create_dir("profiles", dir);
1944*4882a593Smuzhiyun 	if (IS_ERR(dent))
1945*4882a593Smuzhiyun 		return PTR_ERR(dent);
1946*4882a593Smuzhiyun 	ns_subprofs_dir(ns) = dent;
1947*4882a593Smuzhiyun 
1948*4882a593Smuzhiyun 	dent = aafs_create_dir("raw_data", dir);
1949*4882a593Smuzhiyun 	if (IS_ERR(dent))
1950*4882a593Smuzhiyun 		return PTR_ERR(dent);
1951*4882a593Smuzhiyun 	ns_subdata_dir(ns) = dent;
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	dent = aafs_create_file("revision", 0444, dir, ns,
1954*4882a593Smuzhiyun 				&aa_fs_ns_revision_fops);
1955*4882a593Smuzhiyun 	if (IS_ERR(dent))
1956*4882a593Smuzhiyun 		return PTR_ERR(dent);
1957*4882a593Smuzhiyun 	aa_get_ns(ns);
1958*4882a593Smuzhiyun 	ns_subrevision(ns) = dent;
1959*4882a593Smuzhiyun 
1960*4882a593Smuzhiyun 	dent = aafs_create_file(".load", 0640, dir, ns,
1961*4882a593Smuzhiyun 				      &aa_fs_profile_load);
1962*4882a593Smuzhiyun 	if (IS_ERR(dent))
1963*4882a593Smuzhiyun 		return PTR_ERR(dent);
1964*4882a593Smuzhiyun 	aa_get_ns(ns);
1965*4882a593Smuzhiyun 	ns_subload(ns) = dent;
1966*4882a593Smuzhiyun 
1967*4882a593Smuzhiyun 	dent = aafs_create_file(".replace", 0640, dir, ns,
1968*4882a593Smuzhiyun 				      &aa_fs_profile_replace);
1969*4882a593Smuzhiyun 	if (IS_ERR(dent))
1970*4882a593Smuzhiyun 		return PTR_ERR(dent);
1971*4882a593Smuzhiyun 	aa_get_ns(ns);
1972*4882a593Smuzhiyun 	ns_subreplace(ns) = dent;
1973*4882a593Smuzhiyun 
1974*4882a593Smuzhiyun 	dent = aafs_create_file(".remove", 0640, dir, ns,
1975*4882a593Smuzhiyun 				      &aa_fs_profile_remove);
1976*4882a593Smuzhiyun 	if (IS_ERR(dent))
1977*4882a593Smuzhiyun 		return PTR_ERR(dent);
1978*4882a593Smuzhiyun 	aa_get_ns(ns);
1979*4882a593Smuzhiyun 	ns_subremove(ns) = dent;
1980*4882a593Smuzhiyun 
1981*4882a593Smuzhiyun 	  /* use create_dentry so we can supply private data */
1982*4882a593Smuzhiyun 	dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL,
1983*4882a593Smuzhiyun 			   &ns_dir_inode_operations);
1984*4882a593Smuzhiyun 	if (IS_ERR(dent))
1985*4882a593Smuzhiyun 		return PTR_ERR(dent);
1986*4882a593Smuzhiyun 	aa_get_ns(ns);
1987*4882a593Smuzhiyun 	ns_subns_dir(ns) = dent;
1988*4882a593Smuzhiyun 
1989*4882a593Smuzhiyun 	return 0;
1990*4882a593Smuzhiyun }
1991*4882a593Smuzhiyun 
1992*4882a593Smuzhiyun /*
1993*4882a593Smuzhiyun  * Requires: @ns->lock held
1994*4882a593Smuzhiyun  */
__aafs_ns_mkdir(struct aa_ns * ns,struct dentry * parent,const char * name,struct dentry * dent)1995*4882a593Smuzhiyun int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
1996*4882a593Smuzhiyun 		    struct dentry *dent)
1997*4882a593Smuzhiyun {
1998*4882a593Smuzhiyun 	struct aa_ns *sub;
1999*4882a593Smuzhiyun 	struct aa_profile *child;
2000*4882a593Smuzhiyun 	struct dentry *dir;
2001*4882a593Smuzhiyun 	int error;
2002*4882a593Smuzhiyun 
2003*4882a593Smuzhiyun 	AA_BUG(!ns);
2004*4882a593Smuzhiyun 	AA_BUG(!parent);
2005*4882a593Smuzhiyun 	AA_BUG(!mutex_is_locked(&ns->lock));
2006*4882a593Smuzhiyun 
2007*4882a593Smuzhiyun 	if (!name)
2008*4882a593Smuzhiyun 		name = ns->base.name;
2009*4882a593Smuzhiyun 
2010*4882a593Smuzhiyun 	if (!dent) {
2011*4882a593Smuzhiyun 		/* create ns dir if it doesn't already exist */
2012*4882a593Smuzhiyun 		dent = aafs_create_dir(name, parent);
2013*4882a593Smuzhiyun 		if (IS_ERR(dent))
2014*4882a593Smuzhiyun 			goto fail;
2015*4882a593Smuzhiyun 	} else
2016*4882a593Smuzhiyun 		dget(dent);
2017*4882a593Smuzhiyun 	ns_dir(ns) = dir = dent;
2018*4882a593Smuzhiyun 	error = __aafs_ns_mkdir_entries(ns, dir);
2019*4882a593Smuzhiyun 	if (error)
2020*4882a593Smuzhiyun 		goto fail2;
2021*4882a593Smuzhiyun 
2022*4882a593Smuzhiyun 	/* profiles */
2023*4882a593Smuzhiyun 	list_for_each_entry(child, &ns->base.profiles, base.list) {
2024*4882a593Smuzhiyun 		error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
2025*4882a593Smuzhiyun 		if (error)
2026*4882a593Smuzhiyun 			goto fail2;
2027*4882a593Smuzhiyun 	}
2028*4882a593Smuzhiyun 
2029*4882a593Smuzhiyun 	/* subnamespaces */
2030*4882a593Smuzhiyun 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
2031*4882a593Smuzhiyun 		mutex_lock_nested(&sub->lock, sub->level);
2032*4882a593Smuzhiyun 		error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
2033*4882a593Smuzhiyun 		mutex_unlock(&sub->lock);
2034*4882a593Smuzhiyun 		if (error)
2035*4882a593Smuzhiyun 			goto fail2;
2036*4882a593Smuzhiyun 	}
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	return 0;
2039*4882a593Smuzhiyun 
2040*4882a593Smuzhiyun fail:
2041*4882a593Smuzhiyun 	error = PTR_ERR(dent);
2042*4882a593Smuzhiyun 
2043*4882a593Smuzhiyun fail2:
2044*4882a593Smuzhiyun 	__aafs_ns_rmdir(ns);
2045*4882a593Smuzhiyun 
2046*4882a593Smuzhiyun 	return error;
2047*4882a593Smuzhiyun }
2048*4882a593Smuzhiyun 
2049*4882a593Smuzhiyun 
2050*4882a593Smuzhiyun #define list_entry_is_head(pos, head, member) (&pos->member == (head))
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun /**
2053*4882a593Smuzhiyun  * __next_ns - find the next namespace to list
2054*4882a593Smuzhiyun  * @root: root namespace to stop search at (NOT NULL)
2055*4882a593Smuzhiyun  * @ns: current ns position (NOT NULL)
2056*4882a593Smuzhiyun  *
2057*4882a593Smuzhiyun  * Find the next namespace from @ns under @root and handle all locking needed
2058*4882a593Smuzhiyun  * while switching current namespace.
2059*4882a593Smuzhiyun  *
2060*4882a593Smuzhiyun  * Returns: next namespace or NULL if at last namespace under @root
2061*4882a593Smuzhiyun  * Requires: ns->parent->lock to be held
2062*4882a593Smuzhiyun  * NOTE: will not unlock root->lock
2063*4882a593Smuzhiyun  */
__next_ns(struct aa_ns * root,struct aa_ns * ns)2064*4882a593Smuzhiyun static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
2065*4882a593Smuzhiyun {
2066*4882a593Smuzhiyun 	struct aa_ns *parent, *next;
2067*4882a593Smuzhiyun 
2068*4882a593Smuzhiyun 	AA_BUG(!root);
2069*4882a593Smuzhiyun 	AA_BUG(!ns);
2070*4882a593Smuzhiyun 	AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock));
2071*4882a593Smuzhiyun 
2072*4882a593Smuzhiyun 	/* is next namespace a child */
2073*4882a593Smuzhiyun 	if (!list_empty(&ns->sub_ns)) {
2074*4882a593Smuzhiyun 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
2075*4882a593Smuzhiyun 		mutex_lock_nested(&next->lock, next->level);
2076*4882a593Smuzhiyun 		return next;
2077*4882a593Smuzhiyun 	}
2078*4882a593Smuzhiyun 
2079*4882a593Smuzhiyun 	/* check if the next ns is a sibling, parent, gp, .. */
2080*4882a593Smuzhiyun 	parent = ns->parent;
2081*4882a593Smuzhiyun 	while (ns != root) {
2082*4882a593Smuzhiyun 		mutex_unlock(&ns->lock);
2083*4882a593Smuzhiyun 		next = list_next_entry(ns, base.list);
2084*4882a593Smuzhiyun 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
2085*4882a593Smuzhiyun 			mutex_lock_nested(&next->lock, next->level);
2086*4882a593Smuzhiyun 			return next;
2087*4882a593Smuzhiyun 		}
2088*4882a593Smuzhiyun 		ns = parent;
2089*4882a593Smuzhiyun 		parent = parent->parent;
2090*4882a593Smuzhiyun 	}
2091*4882a593Smuzhiyun 
2092*4882a593Smuzhiyun 	return NULL;
2093*4882a593Smuzhiyun }
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun /**
2096*4882a593Smuzhiyun  * __first_profile - find the first profile in a namespace
2097*4882a593Smuzhiyun  * @root: namespace that is root of profiles being displayed (NOT NULL)
2098*4882a593Smuzhiyun  * @ns: namespace to start in   (NOT NULL)
2099*4882a593Smuzhiyun  *
2100*4882a593Smuzhiyun  * Returns: unrefcounted profile or NULL if no profile
2101*4882a593Smuzhiyun  * Requires: profile->ns.lock to be held
2102*4882a593Smuzhiyun  */
__first_profile(struct aa_ns * root,struct aa_ns * ns)2103*4882a593Smuzhiyun static struct aa_profile *__first_profile(struct aa_ns *root,
2104*4882a593Smuzhiyun 					  struct aa_ns *ns)
2105*4882a593Smuzhiyun {
2106*4882a593Smuzhiyun 	AA_BUG(!root);
2107*4882a593Smuzhiyun 	AA_BUG(ns && !mutex_is_locked(&ns->lock));
2108*4882a593Smuzhiyun 
2109*4882a593Smuzhiyun 	for (; ns; ns = __next_ns(root, ns)) {
2110*4882a593Smuzhiyun 		if (!list_empty(&ns->base.profiles))
2111*4882a593Smuzhiyun 			return list_first_entry(&ns->base.profiles,
2112*4882a593Smuzhiyun 						struct aa_profile, base.list);
2113*4882a593Smuzhiyun 	}
2114*4882a593Smuzhiyun 	return NULL;
2115*4882a593Smuzhiyun }
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun /**
2118*4882a593Smuzhiyun  * __next_profile - step to the next profile in a profile tree
2119*4882a593Smuzhiyun  * @profile: current profile in tree (NOT NULL)
2120*4882a593Smuzhiyun  *
2121*4882a593Smuzhiyun  * Perform a depth first traversal on the profile tree in a namespace
2122*4882a593Smuzhiyun  *
2123*4882a593Smuzhiyun  * Returns: next profile or NULL if done
2124*4882a593Smuzhiyun  * Requires: profile->ns.lock to be held
2125*4882a593Smuzhiyun  */
__next_profile(struct aa_profile * p)2126*4882a593Smuzhiyun static struct aa_profile *__next_profile(struct aa_profile *p)
2127*4882a593Smuzhiyun {
2128*4882a593Smuzhiyun 	struct aa_profile *parent;
2129*4882a593Smuzhiyun 	struct aa_ns *ns = p->ns;
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun 	AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock));
2132*4882a593Smuzhiyun 
2133*4882a593Smuzhiyun 	/* is next profile a child */
2134*4882a593Smuzhiyun 	if (!list_empty(&p->base.profiles))
2135*4882a593Smuzhiyun 		return list_first_entry(&p->base.profiles, typeof(*p),
2136*4882a593Smuzhiyun 					base.list);
2137*4882a593Smuzhiyun 
2138*4882a593Smuzhiyun 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
2139*4882a593Smuzhiyun 	parent = rcu_dereference_protected(p->parent,
2140*4882a593Smuzhiyun 					   mutex_is_locked(&p->ns->lock));
2141*4882a593Smuzhiyun 	while (parent) {
2142*4882a593Smuzhiyun 		p = list_next_entry(p, base.list);
2143*4882a593Smuzhiyun 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
2144*4882a593Smuzhiyun 			return p;
2145*4882a593Smuzhiyun 		p = parent;
2146*4882a593Smuzhiyun 		parent = rcu_dereference_protected(parent->parent,
2147*4882a593Smuzhiyun 					    mutex_is_locked(&parent->ns->lock));
2148*4882a593Smuzhiyun 	}
2149*4882a593Smuzhiyun 
2150*4882a593Smuzhiyun 	/* is next another profile in the namespace */
2151*4882a593Smuzhiyun 	p = list_next_entry(p, base.list);
2152*4882a593Smuzhiyun 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
2153*4882a593Smuzhiyun 		return p;
2154*4882a593Smuzhiyun 
2155*4882a593Smuzhiyun 	return NULL;
2156*4882a593Smuzhiyun }
2157*4882a593Smuzhiyun 
2158*4882a593Smuzhiyun /**
2159*4882a593Smuzhiyun  * next_profile - step to the next profile in where ever it may be
2160*4882a593Smuzhiyun  * @root: root namespace  (NOT NULL)
2161*4882a593Smuzhiyun  * @profile: current profile  (NOT NULL)
2162*4882a593Smuzhiyun  *
2163*4882a593Smuzhiyun  * Returns: next profile or NULL if there isn't one
2164*4882a593Smuzhiyun  */
next_profile(struct aa_ns * root,struct aa_profile * profile)2165*4882a593Smuzhiyun static struct aa_profile *next_profile(struct aa_ns *root,
2166*4882a593Smuzhiyun 				       struct aa_profile *profile)
2167*4882a593Smuzhiyun {
2168*4882a593Smuzhiyun 	struct aa_profile *next = __next_profile(profile);
2169*4882a593Smuzhiyun 	if (next)
2170*4882a593Smuzhiyun 		return next;
2171*4882a593Smuzhiyun 
2172*4882a593Smuzhiyun 	/* finished all profiles in namespace move to next namespace */
2173*4882a593Smuzhiyun 	return __first_profile(root, __next_ns(root, profile->ns));
2174*4882a593Smuzhiyun }
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun /**
2177*4882a593Smuzhiyun  * p_start - start a depth first traversal of profile tree
2178*4882a593Smuzhiyun  * @f: seq_file to fill
2179*4882a593Smuzhiyun  * @pos: current position
2180*4882a593Smuzhiyun  *
2181*4882a593Smuzhiyun  * Returns: first profile under current namespace or NULL if none found
2182*4882a593Smuzhiyun  *
2183*4882a593Smuzhiyun  * acquires first ns->lock
2184*4882a593Smuzhiyun  */
p_start(struct seq_file * f,loff_t * pos)2185*4882a593Smuzhiyun static void *p_start(struct seq_file *f, loff_t *pos)
2186*4882a593Smuzhiyun {
2187*4882a593Smuzhiyun 	struct aa_profile *profile = NULL;
2188*4882a593Smuzhiyun 	struct aa_ns *root = aa_get_current_ns();
2189*4882a593Smuzhiyun 	loff_t l = *pos;
2190*4882a593Smuzhiyun 	f->private = root;
2191*4882a593Smuzhiyun 
2192*4882a593Smuzhiyun 	/* find the first profile */
2193*4882a593Smuzhiyun 	mutex_lock_nested(&root->lock, root->level);
2194*4882a593Smuzhiyun 	profile = __first_profile(root, root);
2195*4882a593Smuzhiyun 
2196*4882a593Smuzhiyun 	/* skip to position */
2197*4882a593Smuzhiyun 	for (; profile && l > 0; l--)
2198*4882a593Smuzhiyun 		profile = next_profile(root, profile);
2199*4882a593Smuzhiyun 
2200*4882a593Smuzhiyun 	return profile;
2201*4882a593Smuzhiyun }
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun /**
2204*4882a593Smuzhiyun  * p_next - read the next profile entry
2205*4882a593Smuzhiyun  * @f: seq_file to fill
2206*4882a593Smuzhiyun  * @p: profile previously returned
2207*4882a593Smuzhiyun  * @pos: current position
2208*4882a593Smuzhiyun  *
2209*4882a593Smuzhiyun  * Returns: next profile after @p or NULL if none
2210*4882a593Smuzhiyun  *
2211*4882a593Smuzhiyun  * may acquire/release locks in namespace tree as necessary
2212*4882a593Smuzhiyun  */
p_next(struct seq_file * f,void * p,loff_t * pos)2213*4882a593Smuzhiyun static void *p_next(struct seq_file *f, void *p, loff_t *pos)
2214*4882a593Smuzhiyun {
2215*4882a593Smuzhiyun 	struct aa_profile *profile = p;
2216*4882a593Smuzhiyun 	struct aa_ns *ns = f->private;
2217*4882a593Smuzhiyun 	(*pos)++;
2218*4882a593Smuzhiyun 
2219*4882a593Smuzhiyun 	return next_profile(ns, profile);
2220*4882a593Smuzhiyun }
2221*4882a593Smuzhiyun 
2222*4882a593Smuzhiyun /**
2223*4882a593Smuzhiyun  * p_stop - stop depth first traversal
2224*4882a593Smuzhiyun  * @f: seq_file we are filling
2225*4882a593Smuzhiyun  * @p: the last profile writen
2226*4882a593Smuzhiyun  *
2227*4882a593Smuzhiyun  * Release all locking done by p_start/p_next on namespace tree
2228*4882a593Smuzhiyun  */
p_stop(struct seq_file * f,void * p)2229*4882a593Smuzhiyun static void p_stop(struct seq_file *f, void *p)
2230*4882a593Smuzhiyun {
2231*4882a593Smuzhiyun 	struct aa_profile *profile = p;
2232*4882a593Smuzhiyun 	struct aa_ns *root = f->private, *ns;
2233*4882a593Smuzhiyun 
2234*4882a593Smuzhiyun 	if (profile) {
2235*4882a593Smuzhiyun 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
2236*4882a593Smuzhiyun 			mutex_unlock(&ns->lock);
2237*4882a593Smuzhiyun 	}
2238*4882a593Smuzhiyun 	mutex_unlock(&root->lock);
2239*4882a593Smuzhiyun 	aa_put_ns(root);
2240*4882a593Smuzhiyun }
2241*4882a593Smuzhiyun 
2242*4882a593Smuzhiyun /**
2243*4882a593Smuzhiyun  * seq_show_profile - show a profile entry
2244*4882a593Smuzhiyun  * @f: seq_file to file
2245*4882a593Smuzhiyun  * @p: current position (profile)    (NOT NULL)
2246*4882a593Smuzhiyun  *
2247*4882a593Smuzhiyun  * Returns: error on failure
2248*4882a593Smuzhiyun  */
seq_show_profile(struct seq_file * f,void * p)2249*4882a593Smuzhiyun static int seq_show_profile(struct seq_file *f, void *p)
2250*4882a593Smuzhiyun {
2251*4882a593Smuzhiyun 	struct aa_profile *profile = (struct aa_profile *)p;
2252*4882a593Smuzhiyun 	struct aa_ns *root = f->private;
2253*4882a593Smuzhiyun 
2254*4882a593Smuzhiyun 	aa_label_seq_xprint(f, root, &profile->label,
2255*4882a593Smuzhiyun 			    FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
2256*4882a593Smuzhiyun 	seq_putc(f, '\n');
2257*4882a593Smuzhiyun 
2258*4882a593Smuzhiyun 	return 0;
2259*4882a593Smuzhiyun }
2260*4882a593Smuzhiyun 
2261*4882a593Smuzhiyun static const struct seq_operations aa_sfs_profiles_op = {
2262*4882a593Smuzhiyun 	.start = p_start,
2263*4882a593Smuzhiyun 	.next = p_next,
2264*4882a593Smuzhiyun 	.stop = p_stop,
2265*4882a593Smuzhiyun 	.show = seq_show_profile,
2266*4882a593Smuzhiyun };
2267*4882a593Smuzhiyun 
profiles_open(struct inode * inode,struct file * file)2268*4882a593Smuzhiyun static int profiles_open(struct inode *inode, struct file *file)
2269*4882a593Smuzhiyun {
2270*4882a593Smuzhiyun 	if (!policy_view_capable(NULL))
2271*4882a593Smuzhiyun 		return -EACCES;
2272*4882a593Smuzhiyun 
2273*4882a593Smuzhiyun 	return seq_open(file, &aa_sfs_profiles_op);
2274*4882a593Smuzhiyun }
2275*4882a593Smuzhiyun 
profiles_release(struct inode * inode,struct file * file)2276*4882a593Smuzhiyun static int profiles_release(struct inode *inode, struct file *file)
2277*4882a593Smuzhiyun {
2278*4882a593Smuzhiyun 	return seq_release(inode, file);
2279*4882a593Smuzhiyun }
2280*4882a593Smuzhiyun 
2281*4882a593Smuzhiyun static const struct file_operations aa_sfs_profiles_fops = {
2282*4882a593Smuzhiyun 	.open = profiles_open,
2283*4882a593Smuzhiyun 	.read = seq_read,
2284*4882a593Smuzhiyun 	.llseek = seq_lseek,
2285*4882a593Smuzhiyun 	.release = profiles_release,
2286*4882a593Smuzhiyun };
2287*4882a593Smuzhiyun 
2288*4882a593Smuzhiyun 
2289*4882a593Smuzhiyun /** Base file system setup **/
2290*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_file[] = {
2291*4882a593Smuzhiyun 	AA_SFS_FILE_STRING("mask",
2292*4882a593Smuzhiyun 			   "create read write exec append mmap_exec link lock"),
2293*4882a593Smuzhiyun 	{ }
2294*4882a593Smuzhiyun };
2295*4882a593Smuzhiyun 
2296*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_ptrace[] = {
2297*4882a593Smuzhiyun 	AA_SFS_FILE_STRING("mask", "read trace"),
2298*4882a593Smuzhiyun 	{ }
2299*4882a593Smuzhiyun };
2300*4882a593Smuzhiyun 
2301*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_signal[] = {
2302*4882a593Smuzhiyun 	AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK),
2303*4882a593Smuzhiyun 	{ }
2304*4882a593Smuzhiyun };
2305*4882a593Smuzhiyun 
2306*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_attach[] = {
2307*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("xattr", 1),
2308*4882a593Smuzhiyun 	{ }
2309*4882a593Smuzhiyun };
2310*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_domain[] = {
2311*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("change_hat",	1),
2312*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("change_hatv",	1),
2313*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("change_onexec",	1),
2314*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("change_profile",	1),
2315*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("stack",		1),
2316*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
2317*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("post_nnp_subset",	1),
2318*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("computed_longest_left",	1),
2319*4882a593Smuzhiyun 	AA_SFS_DIR("attach_conditions",		aa_sfs_entry_attach),
2320*4882a593Smuzhiyun 	AA_SFS_FILE_STRING("version", "1.2"),
2321*4882a593Smuzhiyun 	{ }
2322*4882a593Smuzhiyun };
2323*4882a593Smuzhiyun 
2324*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_versions[] = {
2325*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("v5",	1),
2326*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("v6",	1),
2327*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("v7",	1),
2328*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("v8",	1),
2329*4882a593Smuzhiyun 	{ }
2330*4882a593Smuzhiyun };
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_policy[] = {
2333*4882a593Smuzhiyun 	AA_SFS_DIR("versions",			aa_sfs_entry_versions),
2334*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("set_load",		1),
2335*4882a593Smuzhiyun 	/* number of out of band transitions supported */
2336*4882a593Smuzhiyun 	AA_SFS_FILE_U64("outofband",		MAX_OOB_SUPPORTED),
2337*4882a593Smuzhiyun 	{ }
2338*4882a593Smuzhiyun };
2339*4882a593Smuzhiyun 
2340*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_mount[] = {
2341*4882a593Smuzhiyun 	AA_SFS_FILE_STRING("mask", "mount umount pivot_root"),
2342*4882a593Smuzhiyun 	{ }
2343*4882a593Smuzhiyun };
2344*4882a593Smuzhiyun 
2345*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_ns[] = {
2346*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("profile",		1),
2347*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("pivot_root",	0),
2348*4882a593Smuzhiyun 	{ }
2349*4882a593Smuzhiyun };
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_query_label[] = {
2352*4882a593Smuzhiyun 	AA_SFS_FILE_STRING("perms", "allow deny audit quiet"),
2353*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("data",		1),
2354*4882a593Smuzhiyun 	AA_SFS_FILE_BOOLEAN("multi_transaction",	1),
2355*4882a593Smuzhiyun 	{ }
2356*4882a593Smuzhiyun };
2357*4882a593Smuzhiyun 
2358*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_query[] = {
2359*4882a593Smuzhiyun 	AA_SFS_DIR("label",			aa_sfs_entry_query_label),
2360*4882a593Smuzhiyun 	{ }
2361*4882a593Smuzhiyun };
2362*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_features[] = {
2363*4882a593Smuzhiyun 	AA_SFS_DIR("policy",			aa_sfs_entry_policy),
2364*4882a593Smuzhiyun 	AA_SFS_DIR("domain",			aa_sfs_entry_domain),
2365*4882a593Smuzhiyun 	AA_SFS_DIR("file",			aa_sfs_entry_file),
2366*4882a593Smuzhiyun 	AA_SFS_DIR("network_v8",		aa_sfs_entry_network),
2367*4882a593Smuzhiyun 	AA_SFS_DIR("mount",			aa_sfs_entry_mount),
2368*4882a593Smuzhiyun 	AA_SFS_DIR("namespaces",		aa_sfs_entry_ns),
2369*4882a593Smuzhiyun 	AA_SFS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
2370*4882a593Smuzhiyun 	AA_SFS_DIR("rlimit",			aa_sfs_entry_rlimit),
2371*4882a593Smuzhiyun 	AA_SFS_DIR("caps",			aa_sfs_entry_caps),
2372*4882a593Smuzhiyun 	AA_SFS_DIR("ptrace",			aa_sfs_entry_ptrace),
2373*4882a593Smuzhiyun 	AA_SFS_DIR("signal",			aa_sfs_entry_signal),
2374*4882a593Smuzhiyun 	AA_SFS_DIR("query",			aa_sfs_entry_query),
2375*4882a593Smuzhiyun 	{ }
2376*4882a593Smuzhiyun };
2377*4882a593Smuzhiyun 
2378*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
2379*4882a593Smuzhiyun 	AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access),
2380*4882a593Smuzhiyun 	AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops),
2381*4882a593Smuzhiyun 	AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops),
2382*4882a593Smuzhiyun 	AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops),
2383*4882a593Smuzhiyun 	AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops),
2384*4882a593Smuzhiyun 	AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops),
2385*4882a593Smuzhiyun 	AA_SFS_DIR("features", aa_sfs_entry_features),
2386*4882a593Smuzhiyun 	{ }
2387*4882a593Smuzhiyun };
2388*4882a593Smuzhiyun 
2389*4882a593Smuzhiyun static struct aa_sfs_entry aa_sfs_entry =
2390*4882a593Smuzhiyun 	AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
2391*4882a593Smuzhiyun 
2392*4882a593Smuzhiyun /**
2393*4882a593Smuzhiyun  * entry_create_file - create a file entry in the apparmor securityfs
2394*4882a593Smuzhiyun  * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
2395*4882a593Smuzhiyun  * @parent: the parent dentry in the securityfs
2396*4882a593Smuzhiyun  *
2397*4882a593Smuzhiyun  * Use entry_remove_file to remove entries created with this fn.
2398*4882a593Smuzhiyun  */
entry_create_file(struct aa_sfs_entry * fs_file,struct dentry * parent)2399*4882a593Smuzhiyun static int __init entry_create_file(struct aa_sfs_entry *fs_file,
2400*4882a593Smuzhiyun 				    struct dentry *parent)
2401*4882a593Smuzhiyun {
2402*4882a593Smuzhiyun 	int error = 0;
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	fs_file->dentry = securityfs_create_file(fs_file->name,
2405*4882a593Smuzhiyun 						 S_IFREG | fs_file->mode,
2406*4882a593Smuzhiyun 						 parent, fs_file,
2407*4882a593Smuzhiyun 						 fs_file->file_ops);
2408*4882a593Smuzhiyun 	if (IS_ERR(fs_file->dentry)) {
2409*4882a593Smuzhiyun 		error = PTR_ERR(fs_file->dentry);
2410*4882a593Smuzhiyun 		fs_file->dentry = NULL;
2411*4882a593Smuzhiyun 	}
2412*4882a593Smuzhiyun 	return error;
2413*4882a593Smuzhiyun }
2414*4882a593Smuzhiyun 
2415*4882a593Smuzhiyun static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
2416*4882a593Smuzhiyun /**
2417*4882a593Smuzhiyun  * entry_create_dir - recursively create a directory entry in the securityfs
2418*4882a593Smuzhiyun  * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
2419*4882a593Smuzhiyun  * @parent: the parent dentry in the securityfs
2420*4882a593Smuzhiyun  *
2421*4882a593Smuzhiyun  * Use entry_remove_dir to remove entries created with this fn.
2422*4882a593Smuzhiyun  */
entry_create_dir(struct aa_sfs_entry * fs_dir,struct dentry * parent)2423*4882a593Smuzhiyun static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
2424*4882a593Smuzhiyun 				   struct dentry *parent)
2425*4882a593Smuzhiyun {
2426*4882a593Smuzhiyun 	struct aa_sfs_entry *fs_file;
2427*4882a593Smuzhiyun 	struct dentry *dir;
2428*4882a593Smuzhiyun 	int error;
2429*4882a593Smuzhiyun 
2430*4882a593Smuzhiyun 	dir = securityfs_create_dir(fs_dir->name, parent);
2431*4882a593Smuzhiyun 	if (IS_ERR(dir))
2432*4882a593Smuzhiyun 		return PTR_ERR(dir);
2433*4882a593Smuzhiyun 	fs_dir->dentry = dir;
2434*4882a593Smuzhiyun 
2435*4882a593Smuzhiyun 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2436*4882a593Smuzhiyun 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2437*4882a593Smuzhiyun 			error = entry_create_dir(fs_file, fs_dir->dentry);
2438*4882a593Smuzhiyun 		else
2439*4882a593Smuzhiyun 			error = entry_create_file(fs_file, fs_dir->dentry);
2440*4882a593Smuzhiyun 		if (error)
2441*4882a593Smuzhiyun 			goto failed;
2442*4882a593Smuzhiyun 	}
2443*4882a593Smuzhiyun 
2444*4882a593Smuzhiyun 	return 0;
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun failed:
2447*4882a593Smuzhiyun 	entry_remove_dir(fs_dir);
2448*4882a593Smuzhiyun 
2449*4882a593Smuzhiyun 	return error;
2450*4882a593Smuzhiyun }
2451*4882a593Smuzhiyun 
2452*4882a593Smuzhiyun /**
2453*4882a593Smuzhiyun  * entry_remove_file - drop a single file entry in the apparmor securityfs
2454*4882a593Smuzhiyun  * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
2455*4882a593Smuzhiyun  */
entry_remove_file(struct aa_sfs_entry * fs_file)2456*4882a593Smuzhiyun static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
2457*4882a593Smuzhiyun {
2458*4882a593Smuzhiyun 	if (!fs_file->dentry)
2459*4882a593Smuzhiyun 		return;
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun 	securityfs_remove(fs_file->dentry);
2462*4882a593Smuzhiyun 	fs_file->dentry = NULL;
2463*4882a593Smuzhiyun }
2464*4882a593Smuzhiyun 
2465*4882a593Smuzhiyun /**
2466*4882a593Smuzhiyun  * entry_remove_dir - recursively drop a directory entry from the securityfs
2467*4882a593Smuzhiyun  * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
2468*4882a593Smuzhiyun  */
entry_remove_dir(struct aa_sfs_entry * fs_dir)2469*4882a593Smuzhiyun static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
2470*4882a593Smuzhiyun {
2471*4882a593Smuzhiyun 	struct aa_sfs_entry *fs_file;
2472*4882a593Smuzhiyun 
2473*4882a593Smuzhiyun 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2474*4882a593Smuzhiyun 		if (fs_file->v_type == AA_SFS_TYPE_DIR)
2475*4882a593Smuzhiyun 			entry_remove_dir(fs_file);
2476*4882a593Smuzhiyun 		else
2477*4882a593Smuzhiyun 			entry_remove_file(fs_file);
2478*4882a593Smuzhiyun 	}
2479*4882a593Smuzhiyun 
2480*4882a593Smuzhiyun 	entry_remove_file(fs_dir);
2481*4882a593Smuzhiyun }
2482*4882a593Smuzhiyun 
2483*4882a593Smuzhiyun /**
2484*4882a593Smuzhiyun  * aa_destroy_aafs - cleanup and free aafs
2485*4882a593Smuzhiyun  *
2486*4882a593Smuzhiyun  * releases dentries allocated by aa_create_aafs
2487*4882a593Smuzhiyun  */
aa_destroy_aafs(void)2488*4882a593Smuzhiyun void __init aa_destroy_aafs(void)
2489*4882a593Smuzhiyun {
2490*4882a593Smuzhiyun 	entry_remove_dir(&aa_sfs_entry);
2491*4882a593Smuzhiyun }
2492*4882a593Smuzhiyun 
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun #define NULL_FILE_NAME ".null"
2495*4882a593Smuzhiyun struct path aa_null;
2496*4882a593Smuzhiyun 
aa_mk_null_file(struct dentry * parent)2497*4882a593Smuzhiyun static int aa_mk_null_file(struct dentry *parent)
2498*4882a593Smuzhiyun {
2499*4882a593Smuzhiyun 	struct vfsmount *mount = NULL;
2500*4882a593Smuzhiyun 	struct dentry *dentry;
2501*4882a593Smuzhiyun 	struct inode *inode;
2502*4882a593Smuzhiyun 	int count = 0;
2503*4882a593Smuzhiyun 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
2504*4882a593Smuzhiyun 
2505*4882a593Smuzhiyun 	if (error)
2506*4882a593Smuzhiyun 		return error;
2507*4882a593Smuzhiyun 
2508*4882a593Smuzhiyun 	inode_lock(d_inode(parent));
2509*4882a593Smuzhiyun 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
2510*4882a593Smuzhiyun 	if (IS_ERR(dentry)) {
2511*4882a593Smuzhiyun 		error = PTR_ERR(dentry);
2512*4882a593Smuzhiyun 		goto out;
2513*4882a593Smuzhiyun 	}
2514*4882a593Smuzhiyun 	inode = new_inode(parent->d_inode->i_sb);
2515*4882a593Smuzhiyun 	if (!inode) {
2516*4882a593Smuzhiyun 		error = -ENOMEM;
2517*4882a593Smuzhiyun 		goto out1;
2518*4882a593Smuzhiyun 	}
2519*4882a593Smuzhiyun 
2520*4882a593Smuzhiyun 	inode->i_ino = get_next_ino();
2521*4882a593Smuzhiyun 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
2522*4882a593Smuzhiyun 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
2523*4882a593Smuzhiyun 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
2524*4882a593Smuzhiyun 			   MKDEV(MEM_MAJOR, 3));
2525*4882a593Smuzhiyun 	d_instantiate(dentry, inode);
2526*4882a593Smuzhiyun 	aa_null.dentry = dget(dentry);
2527*4882a593Smuzhiyun 	aa_null.mnt = mntget(mount);
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun 	error = 0;
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun out1:
2532*4882a593Smuzhiyun 	dput(dentry);
2533*4882a593Smuzhiyun out:
2534*4882a593Smuzhiyun 	inode_unlock(d_inode(parent));
2535*4882a593Smuzhiyun 	simple_release_fs(&mount, &count);
2536*4882a593Smuzhiyun 	return error;
2537*4882a593Smuzhiyun }
2538*4882a593Smuzhiyun 
2539*4882a593Smuzhiyun 
2540*4882a593Smuzhiyun 
policy_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)2541*4882a593Smuzhiyun static const char *policy_get_link(struct dentry *dentry,
2542*4882a593Smuzhiyun 				   struct inode *inode,
2543*4882a593Smuzhiyun 				   struct delayed_call *done)
2544*4882a593Smuzhiyun {
2545*4882a593Smuzhiyun 	struct aa_ns *ns;
2546*4882a593Smuzhiyun 	struct path path;
2547*4882a593Smuzhiyun 	int error;
2548*4882a593Smuzhiyun 
2549*4882a593Smuzhiyun 	if (!dentry)
2550*4882a593Smuzhiyun 		return ERR_PTR(-ECHILD);
2551*4882a593Smuzhiyun 
2552*4882a593Smuzhiyun 	ns = aa_get_current_ns();
2553*4882a593Smuzhiyun 	path.mnt = mntget(aafs_mnt);
2554*4882a593Smuzhiyun 	path.dentry = dget(ns_dir(ns));
2555*4882a593Smuzhiyun 	error = nd_jump_link(&path);
2556*4882a593Smuzhiyun 	aa_put_ns(ns);
2557*4882a593Smuzhiyun 
2558*4882a593Smuzhiyun 	return ERR_PTR(error);
2559*4882a593Smuzhiyun }
2560*4882a593Smuzhiyun 
policy_readlink(struct dentry * dentry,char __user * buffer,int buflen)2561*4882a593Smuzhiyun static int policy_readlink(struct dentry *dentry, char __user *buffer,
2562*4882a593Smuzhiyun 			   int buflen)
2563*4882a593Smuzhiyun {
2564*4882a593Smuzhiyun 	char name[32];
2565*4882a593Smuzhiyun 	int res;
2566*4882a593Smuzhiyun 
2567*4882a593Smuzhiyun 	res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME,
2568*4882a593Smuzhiyun 		       d_inode(dentry)->i_ino);
2569*4882a593Smuzhiyun 	if (res > 0 && res < sizeof(name))
2570*4882a593Smuzhiyun 		res = readlink_copy(buffer, buflen, name);
2571*4882a593Smuzhiyun 	else
2572*4882a593Smuzhiyun 		res = -ENOENT;
2573*4882a593Smuzhiyun 
2574*4882a593Smuzhiyun 	return res;
2575*4882a593Smuzhiyun }
2576*4882a593Smuzhiyun 
2577*4882a593Smuzhiyun static const struct inode_operations policy_link_iops = {
2578*4882a593Smuzhiyun 	.readlink	= policy_readlink,
2579*4882a593Smuzhiyun 	.get_link	= policy_get_link,
2580*4882a593Smuzhiyun };
2581*4882a593Smuzhiyun 
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun /**
2584*4882a593Smuzhiyun  * aa_create_aafs - create the apparmor security filesystem
2585*4882a593Smuzhiyun  *
2586*4882a593Smuzhiyun  * dentries created here are released by aa_destroy_aafs
2587*4882a593Smuzhiyun  *
2588*4882a593Smuzhiyun  * Returns: error on failure
2589*4882a593Smuzhiyun  */
aa_create_aafs(void)2590*4882a593Smuzhiyun static int __init aa_create_aafs(void)
2591*4882a593Smuzhiyun {
2592*4882a593Smuzhiyun 	struct dentry *dent;
2593*4882a593Smuzhiyun 	int error;
2594*4882a593Smuzhiyun 
2595*4882a593Smuzhiyun 	if (!apparmor_initialized)
2596*4882a593Smuzhiyun 		return 0;
2597*4882a593Smuzhiyun 
2598*4882a593Smuzhiyun 	if (aa_sfs_entry.dentry) {
2599*4882a593Smuzhiyun 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
2600*4882a593Smuzhiyun 		return -EEXIST;
2601*4882a593Smuzhiyun 	}
2602*4882a593Smuzhiyun 
2603*4882a593Smuzhiyun 	/* setup apparmorfs used to virtualize policy/ */
2604*4882a593Smuzhiyun 	aafs_mnt = kern_mount(&aafs_ops);
2605*4882a593Smuzhiyun 	if (IS_ERR(aafs_mnt))
2606*4882a593Smuzhiyun 		panic("can't set apparmorfs up\n");
2607*4882a593Smuzhiyun 	aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
2608*4882a593Smuzhiyun 
2609*4882a593Smuzhiyun 	/* Populate fs tree. */
2610*4882a593Smuzhiyun 	error = entry_create_dir(&aa_sfs_entry, NULL);
2611*4882a593Smuzhiyun 	if (error)
2612*4882a593Smuzhiyun 		goto error;
2613*4882a593Smuzhiyun 
2614*4882a593Smuzhiyun 	dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
2615*4882a593Smuzhiyun 				      NULL, &aa_fs_profile_load);
2616*4882a593Smuzhiyun 	if (IS_ERR(dent))
2617*4882a593Smuzhiyun 		goto dent_error;
2618*4882a593Smuzhiyun 	ns_subload(root_ns) = dent;
2619*4882a593Smuzhiyun 
2620*4882a593Smuzhiyun 	dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
2621*4882a593Smuzhiyun 				      NULL, &aa_fs_profile_replace);
2622*4882a593Smuzhiyun 	if (IS_ERR(dent))
2623*4882a593Smuzhiyun 		goto dent_error;
2624*4882a593Smuzhiyun 	ns_subreplace(root_ns) = dent;
2625*4882a593Smuzhiyun 
2626*4882a593Smuzhiyun 	dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
2627*4882a593Smuzhiyun 				      NULL, &aa_fs_profile_remove);
2628*4882a593Smuzhiyun 	if (IS_ERR(dent))
2629*4882a593Smuzhiyun 		goto dent_error;
2630*4882a593Smuzhiyun 	ns_subremove(root_ns) = dent;
2631*4882a593Smuzhiyun 
2632*4882a593Smuzhiyun 	dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry,
2633*4882a593Smuzhiyun 				      NULL, &aa_fs_ns_revision_fops);
2634*4882a593Smuzhiyun 	if (IS_ERR(dent))
2635*4882a593Smuzhiyun 		goto dent_error;
2636*4882a593Smuzhiyun 	ns_subrevision(root_ns) = dent;
2637*4882a593Smuzhiyun 
2638*4882a593Smuzhiyun 	/* policy tree referenced by magic policy symlink */
2639*4882a593Smuzhiyun 	mutex_lock_nested(&root_ns->lock, root_ns->level);
2640*4882a593Smuzhiyun 	error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
2641*4882a593Smuzhiyun 				aafs_mnt->mnt_root);
2642*4882a593Smuzhiyun 	mutex_unlock(&root_ns->lock);
2643*4882a593Smuzhiyun 	if (error)
2644*4882a593Smuzhiyun 		goto error;
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 	/* magic symlink similar to nsfs redirects based on task policy */
2647*4882a593Smuzhiyun 	dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
2648*4882a593Smuzhiyun 					 NULL, &policy_link_iops);
2649*4882a593Smuzhiyun 	if (IS_ERR(dent))
2650*4882a593Smuzhiyun 		goto dent_error;
2651*4882a593Smuzhiyun 
2652*4882a593Smuzhiyun 	error = aa_mk_null_file(aa_sfs_entry.dentry);
2653*4882a593Smuzhiyun 	if (error)
2654*4882a593Smuzhiyun 		goto error;
2655*4882a593Smuzhiyun 
2656*4882a593Smuzhiyun 	/* TODO: add default profile to apparmorfs */
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun 	/* Report that AppArmor fs is enabled */
2659*4882a593Smuzhiyun 	aa_info_message("AppArmor Filesystem Enabled");
2660*4882a593Smuzhiyun 	return 0;
2661*4882a593Smuzhiyun 
2662*4882a593Smuzhiyun dent_error:
2663*4882a593Smuzhiyun 	error = PTR_ERR(dent);
2664*4882a593Smuzhiyun error:
2665*4882a593Smuzhiyun 	aa_destroy_aafs();
2666*4882a593Smuzhiyun 	AA_ERROR("Error creating AppArmor securityfs\n");
2667*4882a593Smuzhiyun 	return error;
2668*4882a593Smuzhiyun }
2669*4882a593Smuzhiyun 
2670*4882a593Smuzhiyun fs_initcall(aa_create_aafs);
2671