163e2b423SJohn Johansen /*
263e2b423SJohn Johansen  * AppArmor security module
363e2b423SJohn Johansen  *
463e2b423SJohn Johansen  * This file contains AppArmor /sys/kernel/security/apparmor interface functions
563e2b423SJohn Johansen  *
663e2b423SJohn Johansen  * Copyright (C) 1998-2008 Novell/SUSE
763e2b423SJohn Johansen  * Copyright 2009-2010 Canonical Ltd.
863e2b423SJohn Johansen  *
963e2b423SJohn Johansen  * This program is free software; you can redistribute it and/or
1063e2b423SJohn Johansen  * modify it under the terms of the GNU General Public License as
1163e2b423SJohn Johansen  * published by the Free Software Foundation, version 2 of the
1263e2b423SJohn Johansen  * License.
1363e2b423SJohn Johansen  */
1463e2b423SJohn Johansen 
150d259f04SJohn Johansen #include <linux/ctype.h>
1663e2b423SJohn Johansen #include <linux/security.h>
1763e2b423SJohn Johansen #include <linux/vmalloc.h>
1863e2b423SJohn Johansen #include <linux/module.h>
1963e2b423SJohn Johansen #include <linux/seq_file.h>
2063e2b423SJohn Johansen #include <linux/uaccess.h>
21a71ada30SJohn Johansen #include <linux/mount.h>
2263e2b423SJohn Johansen #include <linux/namei.h>
23e74abcf3SKees Cook #include <linux/capability.h>
2429b3822fSJohn Johansen #include <linux/rcupdate.h>
25a71ada30SJohn Johansen #include <uapi/linux/major.h>
26a71ada30SJohn Johansen #include <linux/fs.h>
2763e2b423SJohn Johansen 
2863e2b423SJohn Johansen #include "include/apparmor.h"
2963e2b423SJohn Johansen #include "include/apparmorfs.h"
3063e2b423SJohn Johansen #include "include/audit.h"
3163e2b423SJohn Johansen #include "include/context.h"
32f8eb8a13SJohn Johansen #include "include/crypto.h"
3363e2b423SJohn Johansen #include "include/policy.h"
34cff281f6SJohn Johansen #include "include/policy_ns.h"
35d384b0a1SKees Cook #include "include/resource.h"
365ac8c355SJohn Johansen #include "include/policy_unpack.h"
3763e2b423SJohn Johansen 
3863e2b423SJohn Johansen /**
390d259f04SJohn Johansen  * aa_mangle_name - mangle a profile name to std profile layout form
400d259f04SJohn Johansen  * @name: profile name to mangle  (NOT NULL)
410d259f04SJohn Johansen  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
420d259f04SJohn Johansen  *
430d259f04SJohn Johansen  * Returns: length of mangled name
440d259f04SJohn Johansen  */
45bbe4a7c8SJohn Johansen static int mangle_name(const char *name, char *target)
460d259f04SJohn Johansen {
470d259f04SJohn Johansen 	char *t = target;
480d259f04SJohn Johansen 
490d259f04SJohn Johansen 	while (*name == '/' || *name == '.')
500d259f04SJohn Johansen 		name++;
510d259f04SJohn Johansen 
520d259f04SJohn Johansen 	if (target) {
530d259f04SJohn Johansen 		for (; *name; name++) {
540d259f04SJohn Johansen 			if (*name == '/')
550d259f04SJohn Johansen 				*(t)++ = '.';
560d259f04SJohn Johansen 			else if (isspace(*name))
570d259f04SJohn Johansen 				*(t)++ = '_';
580d259f04SJohn Johansen 			else if (isalnum(*name) || strchr("._-", *name))
590d259f04SJohn Johansen 				*(t)++ = *name;
600d259f04SJohn Johansen 		}
610d259f04SJohn Johansen 
620d259f04SJohn Johansen 		*t = 0;
630d259f04SJohn Johansen 	} else {
640d259f04SJohn Johansen 		int len = 0;
650d259f04SJohn Johansen 		for (; *name; name++) {
660d259f04SJohn Johansen 			if (isalnum(*name) || isspace(*name) ||
670d259f04SJohn Johansen 			    strchr("/._-", *name))
680d259f04SJohn Johansen 				len++;
690d259f04SJohn Johansen 		}
700d259f04SJohn Johansen 
710d259f04SJohn Johansen 		return len;
720d259f04SJohn Johansen 	}
730d259f04SJohn Johansen 
740d259f04SJohn Johansen 	return t - target;
750d259f04SJohn Johansen }
760d259f04SJohn Johansen 
770d259f04SJohn Johansen /**
7863e2b423SJohn Johansen  * aa_simple_write_to_buffer - common routine for getting policy from user
7963e2b423SJohn Johansen  * @userbuf: user buffer to copy data from  (NOT NULL)
803ed02adaSJohn Johansen  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
8163e2b423SJohn Johansen  * @copy_size: size of data to copy from user buffer
8263e2b423SJohn Johansen  * @pos: position write is at in the file (NOT NULL)
8363e2b423SJohn Johansen  *
8463e2b423SJohn Johansen  * Returns: kernel buffer containing copy of user buffer data or an
8563e2b423SJohn Johansen  *          ERR_PTR on failure.
8663e2b423SJohn Johansen  */
875ef50d01SJohn Johansen static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
885ac8c355SJohn Johansen 						     size_t alloc_size,
895ac8c355SJohn Johansen 						     size_t copy_size,
9063e2b423SJohn Johansen 						     loff_t *pos)
9163e2b423SJohn Johansen {
925ac8c355SJohn Johansen 	struct aa_loaddata *data;
9363e2b423SJohn Johansen 
94e6bfa25dSJohn Johansen 	AA_BUG(copy_size > alloc_size);
953ed02adaSJohn Johansen 
9663e2b423SJohn Johansen 	if (*pos != 0)
9763e2b423SJohn Johansen 		/* only writes from pos 0, that is complete writes */
9863e2b423SJohn Johansen 		return ERR_PTR(-ESPIPE);
9963e2b423SJohn Johansen 
10063e2b423SJohn Johansen 	/* freed by caller to simple_write_to_buffer */
1015d5182caSJohn Johansen 	data = aa_loaddata_alloc(alloc_size);
1025d5182caSJohn Johansen 	if (IS_ERR(data))
1035d5182caSJohn Johansen 		return data;
10463e2b423SJohn Johansen 
1055d5182caSJohn Johansen 	data->size = copy_size;
1065ac8c355SJohn Johansen 	if (copy_from_user(data->data, userbuf, copy_size)) {
10763e2b423SJohn Johansen 		kvfree(data);
10863e2b423SJohn Johansen 		return ERR_PTR(-EFAULT);
10963e2b423SJohn Johansen 	}
11063e2b423SJohn Johansen 
11163e2b423SJohn Johansen 	return data;
11263e2b423SJohn Johansen }
11363e2b423SJohn Johansen 
1145ac8c355SJohn Johansen static ssize_t policy_update(int binop, const char __user *buf, size_t size,
115b7fd2c03SJohn Johansen 			     loff_t *pos, struct aa_ns *ns)
1165ac8c355SJohn Johansen {
1175ac8c355SJohn Johansen 	ssize_t error;
1185ac8c355SJohn Johansen 	struct aa_loaddata *data;
1195ac8c355SJohn Johansen 	struct aa_profile *profile = aa_current_profile();
12047f6e5ccSJohn Johansen 	const char *op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL;
1215ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
1225ac8c355SJohn Johansen 	 * below after unpack
1235ac8c355SJohn Johansen 	 */
124b7fd2c03SJohn Johansen 	error = aa_may_manage_policy(profile, ns, op);
1255ac8c355SJohn Johansen 	if (error)
1265ac8c355SJohn Johansen 		return error;
12763e2b423SJohn Johansen 
1285ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size, size, pos);
1295ac8c355SJohn Johansen 	error = PTR_ERR(data);
1305ac8c355SJohn Johansen 	if (!IS_ERR(data)) {
131b7fd2c03SJohn Johansen 		error = aa_replace_profiles(ns ? ns : profile->ns, profile,
132b7fd2c03SJohn Johansen 					    binop, data);
1335ac8c355SJohn Johansen 		aa_put_loaddata(data);
1345ac8c355SJohn Johansen 	}
1355ac8c355SJohn Johansen 
1365ac8c355SJohn Johansen 	return error;
1375ac8c355SJohn Johansen }
1385ac8c355SJohn Johansen 
139b7fd2c03SJohn Johansen /* .load file hook fn to load policy */
14063e2b423SJohn Johansen static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
14163e2b423SJohn Johansen 			    loff_t *pos)
14263e2b423SJohn Johansen {
143b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
144b7fd2c03SJohn Johansen 	int error = policy_update(PROF_ADD, buf, size, pos, ns);
145b7fd2c03SJohn Johansen 
146b7fd2c03SJohn Johansen 	aa_put_ns(ns);
14763e2b423SJohn Johansen 
14863e2b423SJohn Johansen 	return error;
14963e2b423SJohn Johansen }
15063e2b423SJohn Johansen 
15163e2b423SJohn Johansen static const struct file_operations aa_fs_profile_load = {
1526038f373SArnd Bergmann 	.write = profile_load,
1536038f373SArnd Bergmann 	.llseek = default_llseek,
15463e2b423SJohn Johansen };
15563e2b423SJohn Johansen 
15663e2b423SJohn Johansen /* .replace file hook fn to load and/or replace policy */
15763e2b423SJohn Johansen static ssize_t profile_replace(struct file *f, const char __user *buf,
15863e2b423SJohn Johansen 			       size_t size, loff_t *pos)
15963e2b423SJohn Johansen {
160b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
161b7fd2c03SJohn Johansen 	int error = policy_update(PROF_REPLACE, buf, size, pos, ns);
162b7fd2c03SJohn Johansen 
163b7fd2c03SJohn Johansen 	aa_put_ns(ns);
16463e2b423SJohn Johansen 
16563e2b423SJohn Johansen 	return error;
16663e2b423SJohn Johansen }
16763e2b423SJohn Johansen 
16863e2b423SJohn Johansen static const struct file_operations aa_fs_profile_replace = {
1696038f373SArnd Bergmann 	.write = profile_replace,
1706038f373SArnd Bergmann 	.llseek = default_llseek,
17163e2b423SJohn Johansen };
17263e2b423SJohn Johansen 
173b7fd2c03SJohn Johansen /* .remove file hook fn to remove loaded policy */
17463e2b423SJohn Johansen static ssize_t profile_remove(struct file *f, const char __user *buf,
17563e2b423SJohn Johansen 			      size_t size, loff_t *pos)
17663e2b423SJohn Johansen {
1775ac8c355SJohn Johansen 	struct aa_loaddata *data;
1785ac8c355SJohn Johansen 	struct aa_profile *profile;
17963e2b423SJohn Johansen 	ssize_t error;
180b7fd2c03SJohn Johansen 	struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
18163e2b423SJohn Johansen 
1825ac8c355SJohn Johansen 	profile = aa_current_profile();
1835ac8c355SJohn Johansen 	/* high level check about policy management - fine grained in
1845ac8c355SJohn Johansen 	 * below after unpack
1855ac8c355SJohn Johansen 	 */
186b7fd2c03SJohn Johansen 	error = aa_may_manage_policy(profile, ns, OP_PROF_RM);
1875ac8c355SJohn Johansen 	if (error)
1885ac8c355SJohn Johansen 		goto out;
1895ac8c355SJohn Johansen 
19063e2b423SJohn Johansen 	/*
19163e2b423SJohn Johansen 	 * aa_remove_profile needs a null terminated string so 1 extra
19263e2b423SJohn Johansen 	 * byte is allocated and the copied data is null terminated.
19363e2b423SJohn Johansen 	 */
1945ef50d01SJohn Johansen 	data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
19563e2b423SJohn Johansen 
19663e2b423SJohn Johansen 	error = PTR_ERR(data);
19763e2b423SJohn Johansen 	if (!IS_ERR(data)) {
1985ac8c355SJohn Johansen 		data->data[size] = 0;
199b7fd2c03SJohn Johansen 		error = aa_remove_profiles(ns ? ns : profile->ns, profile,
200b7fd2c03SJohn Johansen 					   data->data, size);
2015ac8c355SJohn Johansen 		aa_put_loaddata(data);
20263e2b423SJohn Johansen 	}
2035ac8c355SJohn Johansen  out:
204b7fd2c03SJohn Johansen 	aa_put_ns(ns);
20563e2b423SJohn Johansen 	return error;
20663e2b423SJohn Johansen }
20763e2b423SJohn Johansen 
20863e2b423SJohn Johansen static const struct file_operations aa_fs_profile_remove = {
2096038f373SArnd Bergmann 	.write = profile_remove,
2106038f373SArnd Bergmann 	.llseek = default_llseek,
21163e2b423SJohn Johansen };
21263e2b423SJohn Johansen 
2135d5182caSJohn Johansen void __aa_bump_ns_revision(struct aa_ns *ns)
2145d5182caSJohn Johansen {
2155d5182caSJohn Johansen 	ns->revision++;
2165d5182caSJohn Johansen }
2175d5182caSJohn Johansen 
218e025be0fSWilliam Hua /**
219e025be0fSWilliam Hua  * query_data - queries a policy and writes its data to buf
220e025be0fSWilliam Hua  * @buf: the resulting data is stored here (NOT NULL)
221e025be0fSWilliam Hua  * @buf_len: size of buf
222e025be0fSWilliam Hua  * @query: query string used to retrieve data
223e025be0fSWilliam Hua  * @query_len: size of query including second NUL byte
224e025be0fSWilliam Hua  *
225e025be0fSWilliam Hua  * The buffers pointed to by buf and query may overlap. The query buffer is
226e025be0fSWilliam Hua  * parsed before buf is written to.
227e025be0fSWilliam Hua  *
228e025be0fSWilliam Hua  * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
229e025be0fSWilliam Hua  * the security confinement context and <KEY> is the name of the data to
230e025be0fSWilliam Hua  * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
231e025be0fSWilliam Hua  *
232e025be0fSWilliam Hua  * Don't expect the contents of buf to be preserved on failure.
233e025be0fSWilliam Hua  *
234e025be0fSWilliam Hua  * Returns: number of characters written to buf or -errno on failure
235e025be0fSWilliam Hua  */
236e025be0fSWilliam Hua static ssize_t query_data(char *buf, size_t buf_len,
237e025be0fSWilliam Hua 			  char *query, size_t query_len)
238e025be0fSWilliam Hua {
239e025be0fSWilliam Hua 	char *out;
240e025be0fSWilliam Hua 	const char *key;
241e025be0fSWilliam Hua 	struct aa_profile *profile;
242e025be0fSWilliam Hua 	struct aa_data *data;
243e025be0fSWilliam Hua 	u32 bytes, blocks;
244e025be0fSWilliam Hua 	__le32 outle32;
245e025be0fSWilliam Hua 
246e025be0fSWilliam Hua 	if (!query_len)
247e025be0fSWilliam Hua 		return -EINVAL; /* need a query */
248e025be0fSWilliam Hua 
249e025be0fSWilliam Hua 	key = query + strnlen(query, query_len) + 1;
250e025be0fSWilliam Hua 	if (key + 1 >= query + query_len)
251e025be0fSWilliam Hua 		return -EINVAL; /* not enough space for a non-empty key */
252e025be0fSWilliam Hua 	if (key + strnlen(key, query + query_len - key) >= query + query_len)
253e025be0fSWilliam Hua 		return -EINVAL; /* must end with NUL */
254e025be0fSWilliam Hua 
255e025be0fSWilliam Hua 	if (buf_len < sizeof(bytes) + sizeof(blocks))
256e025be0fSWilliam Hua 		return -EINVAL; /* not enough space */
257e025be0fSWilliam Hua 
258e025be0fSWilliam Hua 	profile = aa_current_profile();
259e025be0fSWilliam Hua 
260e025be0fSWilliam Hua 	/* We are going to leave space for two numbers. The first is the total
261e025be0fSWilliam Hua 	 * number of bytes we are writing after the first number. This is so
262e025be0fSWilliam Hua 	 * users can read the full output without reallocation.
263e025be0fSWilliam Hua 	 *
264e025be0fSWilliam Hua 	 * The second number is the number of data blocks we're writing. An
265e025be0fSWilliam Hua 	 * application might be confined by multiple policies having data in
266e025be0fSWilliam Hua 	 * the same key.
267e025be0fSWilliam Hua 	 */
268e025be0fSWilliam Hua 	memset(buf, 0, sizeof(bytes) + sizeof(blocks));
269e025be0fSWilliam Hua 	out = buf + sizeof(bytes) + sizeof(blocks);
270e025be0fSWilliam Hua 
271e025be0fSWilliam Hua 	blocks = 0;
272e025be0fSWilliam Hua 	if (profile->data) {
273e025be0fSWilliam Hua 		data = rhashtable_lookup_fast(profile->data, &key,
274e025be0fSWilliam Hua 					      profile->data->p);
275e025be0fSWilliam Hua 
276e025be0fSWilliam Hua 		if (data) {
277e025be0fSWilliam Hua 			if (out + sizeof(outle32) + data->size > buf + buf_len)
278e025be0fSWilliam Hua 				return -EINVAL; /* not enough space */
279e025be0fSWilliam Hua 			outle32 = __cpu_to_le32(data->size);
280e025be0fSWilliam Hua 			memcpy(out, &outle32, sizeof(outle32));
281e025be0fSWilliam Hua 			out += sizeof(outle32);
282e025be0fSWilliam Hua 			memcpy(out, data->data, data->size);
283e025be0fSWilliam Hua 			out += data->size;
284e025be0fSWilliam Hua 			blocks++;
285e025be0fSWilliam Hua 		}
286e025be0fSWilliam Hua 	}
287e025be0fSWilliam Hua 
288e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
289e025be0fSWilliam Hua 	memcpy(buf, &outle32, sizeof(outle32));
290e025be0fSWilliam Hua 	outle32 = __cpu_to_le32(blocks);
291e025be0fSWilliam Hua 	memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
292e025be0fSWilliam Hua 
293e025be0fSWilliam Hua 	return out - buf;
294e025be0fSWilliam Hua }
295e025be0fSWilliam Hua 
296e025be0fSWilliam Hua #define QUERY_CMD_DATA		"data\0"
297e025be0fSWilliam Hua #define QUERY_CMD_DATA_LEN	5
298e025be0fSWilliam Hua 
299e025be0fSWilliam Hua /**
300e025be0fSWilliam Hua  * aa_write_access - generic permissions and data query
301e025be0fSWilliam Hua  * @file: pointer to open apparmorfs/access file
302e025be0fSWilliam Hua  * @ubuf: user buffer containing the complete query string (NOT NULL)
303e025be0fSWilliam Hua  * @count: size of ubuf
304e025be0fSWilliam Hua  * @ppos: position in the file (MUST BE ZERO)
305e025be0fSWilliam Hua  *
306e025be0fSWilliam Hua  * Allows for one permissions or data query per open(), write(), and read()
307e025be0fSWilliam Hua  * sequence. The only queries currently supported are label-based queries for
308e025be0fSWilliam Hua  * permissions or data.
309e025be0fSWilliam Hua  *
310e025be0fSWilliam Hua  * For permissions queries, ubuf must begin with "label\0", followed by the
311e025be0fSWilliam Hua  * profile query specific format described in the query_label() function
312e025be0fSWilliam Hua  * documentation.
313e025be0fSWilliam Hua  *
314e025be0fSWilliam Hua  * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
315e025be0fSWilliam Hua  * <LABEL> is the name of the security confinement context and <KEY> is the
316e025be0fSWilliam Hua  * name of the data to retrieve.
317e025be0fSWilliam Hua  *
318e025be0fSWilliam Hua  * Returns: number of bytes written or -errno on failure
319e025be0fSWilliam Hua  */
320e025be0fSWilliam Hua static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
321e025be0fSWilliam Hua 			       size_t count, loff_t *ppos)
322e025be0fSWilliam Hua {
323e025be0fSWilliam Hua 	char *buf;
324e025be0fSWilliam Hua 	ssize_t len;
325e025be0fSWilliam Hua 
326e025be0fSWilliam Hua 	if (*ppos)
327e025be0fSWilliam Hua 		return -ESPIPE;
328e025be0fSWilliam Hua 
329e025be0fSWilliam Hua 	buf = simple_transaction_get(file, ubuf, count);
330e025be0fSWilliam Hua 	if (IS_ERR(buf))
331e025be0fSWilliam Hua 		return PTR_ERR(buf);
332e025be0fSWilliam Hua 
333e025be0fSWilliam Hua 	if (count > QUERY_CMD_DATA_LEN &&
334e025be0fSWilliam Hua 		   !memcmp(buf, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
335e025be0fSWilliam Hua 		len = query_data(buf, SIMPLE_TRANSACTION_LIMIT,
336e025be0fSWilliam Hua 				 buf + QUERY_CMD_DATA_LEN,
337e025be0fSWilliam Hua 				 count - QUERY_CMD_DATA_LEN);
338e025be0fSWilliam Hua 	} else
339e025be0fSWilliam Hua 		len = -EINVAL;
340e025be0fSWilliam Hua 
341e025be0fSWilliam Hua 	if (len < 0)
342e025be0fSWilliam Hua 		return len;
343e025be0fSWilliam Hua 
344e025be0fSWilliam Hua 	simple_transaction_set(file, len);
345e025be0fSWilliam Hua 
346e025be0fSWilliam Hua 	return count;
347e025be0fSWilliam Hua }
348e025be0fSWilliam Hua 
349e025be0fSWilliam Hua static const struct file_operations aa_fs_access = {
350e025be0fSWilliam Hua 	.write		= aa_write_access,
351e025be0fSWilliam Hua 	.read		= simple_transaction_read,
352e025be0fSWilliam Hua 	.release	= simple_transaction_release,
353e025be0fSWilliam Hua 	.llseek		= generic_file_llseek,
354e025be0fSWilliam Hua };
355e025be0fSWilliam Hua 
356e74abcf3SKees Cook static int aa_fs_seq_show(struct seq_file *seq, void *v)
357e74abcf3SKees Cook {
358e74abcf3SKees Cook 	struct aa_fs_entry *fs_file = seq->private;
359e74abcf3SKees Cook 
360e74abcf3SKees Cook 	if (!fs_file)
361e74abcf3SKees Cook 		return 0;
362e74abcf3SKees Cook 
363e74abcf3SKees Cook 	switch (fs_file->v_type) {
364e74abcf3SKees Cook 	case AA_FS_TYPE_BOOLEAN:
365e74abcf3SKees Cook 		seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
366e74abcf3SKees Cook 		break;
367a9bf8e9fSKees Cook 	case AA_FS_TYPE_STRING:
368a9bf8e9fSKees Cook 		seq_printf(seq, "%s\n", fs_file->v.string);
369a9bf8e9fSKees Cook 		break;
370e74abcf3SKees Cook 	case AA_FS_TYPE_U64:
371e74abcf3SKees Cook 		seq_printf(seq, "%#08lx\n", fs_file->v.u64);
372e74abcf3SKees Cook 		break;
373e74abcf3SKees Cook 	default:
374e74abcf3SKees Cook 		/* Ignore unpritable entry types. */
375e74abcf3SKees Cook 		break;
376e74abcf3SKees Cook 	}
377e74abcf3SKees Cook 
378e74abcf3SKees Cook 	return 0;
379e74abcf3SKees Cook }
380e74abcf3SKees Cook 
381e74abcf3SKees Cook static int aa_fs_seq_open(struct inode *inode, struct file *file)
382e74abcf3SKees Cook {
383e74abcf3SKees Cook 	return single_open(file, aa_fs_seq_show, inode->i_private);
384e74abcf3SKees Cook }
385e74abcf3SKees Cook 
386e74abcf3SKees Cook const struct file_operations aa_fs_seq_file_ops = {
387e74abcf3SKees Cook 	.owner		= THIS_MODULE,
388e74abcf3SKees Cook 	.open		= aa_fs_seq_open,
389e74abcf3SKees Cook 	.read		= seq_read,
390e74abcf3SKees Cook 	.llseek		= seq_lseek,
391e74abcf3SKees Cook 	.release	= single_release,
392e74abcf3SKees Cook };
393e74abcf3SKees Cook 
39452b97de3SJohn Johansen /*
39552b97de3SJohn Johansen  * profile based file operations
39652b97de3SJohn Johansen  *     policy/profiles/XXXX/profiles/ *
39752b97de3SJohn Johansen  */
39852b97de3SJohn Johansen 
39952b97de3SJohn Johansen #define SEQ_PROFILE_FOPS(NAME)						      \
40052b97de3SJohn Johansen static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
40152b97de3SJohn Johansen {									      \
40252b97de3SJohn Johansen 	return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show);    \
40352b97de3SJohn Johansen }									      \
40452b97de3SJohn Johansen 									      \
40552b97de3SJohn Johansen static const struct file_operations seq_profile_ ##NAME ##_fops = {	      \
40652b97de3SJohn Johansen 	.owner		= THIS_MODULE,					      \
40752b97de3SJohn Johansen 	.open		= seq_profile_ ##NAME ##_open,			      \
40852b97de3SJohn Johansen 	.read		= seq_read,					      \
40952b97de3SJohn Johansen 	.llseek		= seq_lseek,					      \
41052b97de3SJohn Johansen 	.release	= seq_profile_release,				      \
41152b97de3SJohn Johansen }									      \
41252b97de3SJohn Johansen 
41352b97de3SJohn Johansen static int seq_profile_open(struct inode *inode, struct file *file,
4140d259f04SJohn Johansen 			    int (*show)(struct seq_file *, void *))
4150d259f04SJohn Johansen {
4168399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
4178399588aSJohn Johansen 	int error = single_open(file, show, proxy);
41863e2b423SJohn Johansen 
4190d259f04SJohn Johansen 	if (error) {
4200d259f04SJohn Johansen 		file->private_data = NULL;
4218399588aSJohn Johansen 		aa_put_proxy(proxy);
4220d259f04SJohn Johansen 	}
4230d259f04SJohn Johansen 
4240d259f04SJohn Johansen 	return error;
4250d259f04SJohn Johansen }
4260d259f04SJohn Johansen 
42752b97de3SJohn Johansen static int seq_profile_release(struct inode *inode, struct file *file)
4280d259f04SJohn Johansen {
4290d259f04SJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
4300d259f04SJohn Johansen 	if (seq)
4318399588aSJohn Johansen 		aa_put_proxy(seq->private);
4320d259f04SJohn Johansen 	return single_release(inode, file);
4330d259f04SJohn Johansen }
4340d259f04SJohn Johansen 
43552b97de3SJohn Johansen static int seq_profile_name_show(struct seq_file *seq, void *v)
4360d259f04SJohn Johansen {
4378399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
4388399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
4390d259f04SJohn Johansen 	seq_printf(seq, "%s\n", profile->base.name);
4400d259f04SJohn Johansen 	aa_put_profile(profile);
4410d259f04SJohn Johansen 
4420d259f04SJohn Johansen 	return 0;
4430d259f04SJohn Johansen }
4440d259f04SJohn Johansen 
44552b97de3SJohn Johansen static int seq_profile_mode_show(struct seq_file *seq, void *v)
4460d259f04SJohn Johansen {
4478399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
4488399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
4490d259f04SJohn Johansen 	seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
4500d259f04SJohn Johansen 	aa_put_profile(profile);
4510d259f04SJohn Johansen 
4520d259f04SJohn Johansen 	return 0;
4530d259f04SJohn Johansen }
4540d259f04SJohn Johansen 
45552b97de3SJohn Johansen static int seq_profile_attach_show(struct seq_file *seq, void *v)
456556d0be7SJohn Johansen {
4578399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
4588399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
459556d0be7SJohn Johansen 	if (profile->attach)
460556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->attach);
461556d0be7SJohn Johansen 	else if (profile->xmatch)
462556d0be7SJohn Johansen 		seq_puts(seq, "<unknown>\n");
463556d0be7SJohn Johansen 	else
464556d0be7SJohn Johansen 		seq_printf(seq, "%s\n", profile->base.name);
465556d0be7SJohn Johansen 	aa_put_profile(profile);
466556d0be7SJohn Johansen 
467556d0be7SJohn Johansen 	return 0;
468556d0be7SJohn Johansen }
469556d0be7SJohn Johansen 
47052b97de3SJohn Johansen static int seq_profile_hash_show(struct seq_file *seq, void *v)
471f8eb8a13SJohn Johansen {
4728399588aSJohn Johansen 	struct aa_proxy *proxy = seq->private;
4738399588aSJohn Johansen 	struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
474f8eb8a13SJohn Johansen 	unsigned int i, size = aa_hash_size();
475f8eb8a13SJohn Johansen 
476f8eb8a13SJohn Johansen 	if (profile->hash) {
477f8eb8a13SJohn Johansen 		for (i = 0; i < size; i++)
478f8eb8a13SJohn Johansen 			seq_printf(seq, "%.2x", profile->hash[i]);
47947dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
480f8eb8a13SJohn Johansen 	}
4810b938a2eSJohn Johansen 	aa_put_profile(profile);
482f8eb8a13SJohn Johansen 
483f8eb8a13SJohn Johansen 	return 0;
484f8eb8a13SJohn Johansen }
485f8eb8a13SJohn Johansen 
48652b97de3SJohn Johansen SEQ_PROFILE_FOPS(name);
48752b97de3SJohn Johansen SEQ_PROFILE_FOPS(mode);
48852b97de3SJohn Johansen SEQ_PROFILE_FOPS(attach);
48952b97de3SJohn Johansen SEQ_PROFILE_FOPS(hash);
490f8eb8a13SJohn Johansen 
491f8eb8a13SJohn Johansen 
4923e3e5695SJohn Johansen 
493a71ada30SJohn Johansen static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v)
494a71ada30SJohn Johansen {
495a71ada30SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
496a71ada30SJohn Johansen 
497a71ada30SJohn Johansen 	seq_printf(seq, "%d\n", ns->level);
498a71ada30SJohn Johansen 
499a71ada30SJohn Johansen 	return 0;
500a71ada30SJohn Johansen }
501a71ada30SJohn Johansen 
502a71ada30SJohn Johansen static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file)
503a71ada30SJohn Johansen {
504a71ada30SJohn Johansen 	return single_open(file, aa_fs_seq_show_ns_level, inode->i_private);
505a71ada30SJohn Johansen }
506a71ada30SJohn Johansen 
507a71ada30SJohn Johansen static const struct file_operations aa_fs_ns_level = {
508a71ada30SJohn Johansen 	.owner		= THIS_MODULE,
509a71ada30SJohn Johansen 	.open		= aa_fs_seq_open_ns_level,
510a71ada30SJohn Johansen 	.read		= seq_read,
511a71ada30SJohn Johansen 	.llseek		= seq_lseek,
512a71ada30SJohn Johansen 	.release	= single_release,
513a71ada30SJohn Johansen };
514a71ada30SJohn Johansen 
5153e3e5695SJohn Johansen static int aa_fs_seq_show_ns_name(struct seq_file *seq, void *v)
5163e3e5695SJohn Johansen {
5173e3e5695SJohn Johansen 	struct aa_ns *ns = aa_current_profile()->ns;
5183e3e5695SJohn Johansen 
5193e3e5695SJohn Johansen 	seq_printf(seq, "%s\n", ns->base.name);
5203e3e5695SJohn Johansen 
5213e3e5695SJohn Johansen 	return 0;
5223e3e5695SJohn Johansen }
5233e3e5695SJohn Johansen 
5243e3e5695SJohn Johansen static int aa_fs_seq_open_ns_name(struct inode *inode, struct file *file)
5253e3e5695SJohn Johansen {
5263e3e5695SJohn Johansen 	return single_open(file, aa_fs_seq_show_ns_name, inode->i_private);
5273e3e5695SJohn Johansen }
5283e3e5695SJohn Johansen 
5293e3e5695SJohn Johansen static const struct file_operations aa_fs_ns_name = {
5303e3e5695SJohn Johansen 	.owner		= THIS_MODULE,
5313e3e5695SJohn Johansen 	.open		= aa_fs_seq_open_ns_name,
5323e3e5695SJohn Johansen 	.read		= seq_read,
5333e3e5695SJohn Johansen 	.llseek		= seq_lseek,
5343e3e5695SJohn Johansen 	.release	= single_release,
5353e3e5695SJohn Johansen };
5363e3e5695SJohn Johansen 
5375d5182caSJohn Johansen 
5385d5182caSJohn Johansen /* policy/raw_data/ * file ops */
5395d5182caSJohn Johansen 
5405d5182caSJohn Johansen #define SEQ_RAWDATA_FOPS(NAME)						      \
5415d5182caSJohn Johansen static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
5425d5182caSJohn Johansen {									      \
5435d5182caSJohn Johansen 	return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show);    \
5445d5182caSJohn Johansen }									      \
5455d5182caSJohn Johansen 									      \
5465d5182caSJohn Johansen static const struct file_operations seq_rawdata_ ##NAME ##_fops = {	      \
5475d5182caSJohn Johansen 	.owner		= THIS_MODULE,					      \
5485d5182caSJohn Johansen 	.open		= seq_rawdata_ ##NAME ##_open,			      \
5495d5182caSJohn Johansen 	.read		= seq_read,					      \
5505d5182caSJohn Johansen 	.llseek		= seq_lseek,					      \
5515d5182caSJohn Johansen 	.release	= seq_rawdata_release,				      \
5525d5182caSJohn Johansen }									      \
5535d5182caSJohn Johansen 
5545d5182caSJohn Johansen static int seq_rawdata_open(struct inode *inode, struct file *file,
5555d5182caSJohn Johansen 			    int (*show)(struct seq_file *, void *))
5565ac8c355SJohn Johansen {
5575d5182caSJohn Johansen 	struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
5585d5182caSJohn Johansen 	int error;
5595d5182caSJohn Johansen 
5605d5182caSJohn Johansen 	if (!data)
5615d5182caSJohn Johansen 		/* lost race this ent is being reaped */
5625d5182caSJohn Johansen 		return -ENOENT;
5635d5182caSJohn Johansen 
5645d5182caSJohn Johansen 	error = single_open(file, show, data);
5655d5182caSJohn Johansen 	if (error) {
5665d5182caSJohn Johansen 		AA_BUG(file->private_data &&
5675d5182caSJohn Johansen 		       ((struct seq_file *)file->private_data)->private);
5685d5182caSJohn Johansen 		aa_put_loaddata(data);
5695d5182caSJohn Johansen 	}
5705d5182caSJohn Johansen 
5715d5182caSJohn Johansen 	return error;
5725d5182caSJohn Johansen }
5735d5182caSJohn Johansen 
5745d5182caSJohn Johansen static int seq_rawdata_release(struct inode *inode, struct file *file)
5755d5182caSJohn Johansen {
5765d5182caSJohn Johansen 	struct seq_file *seq = (struct seq_file *) file->private_data;
5775d5182caSJohn Johansen 
5785d5182caSJohn Johansen 	if (seq)
5795d5182caSJohn Johansen 		aa_put_loaddata(seq->private);
5805d5182caSJohn Johansen 
5815d5182caSJohn Johansen 	return single_release(inode, file);
5825d5182caSJohn Johansen }
5835d5182caSJohn Johansen 
5845d5182caSJohn Johansen static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
5855d5182caSJohn Johansen {
5865d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
5875d5182caSJohn Johansen 
5885d5182caSJohn Johansen 	seq_printf(seq, "v%d\n", data->abi);
5895ac8c355SJohn Johansen 
5905ac8c355SJohn Johansen 	return 0;
5915ac8c355SJohn Johansen }
5925ac8c355SJohn Johansen 
5935d5182caSJohn Johansen static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
5945ac8c355SJohn Johansen {
5955d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
5965ac8c355SJohn Johansen 
5975d5182caSJohn Johansen 	seq_printf(seq, "%ld\n", data->revision);
5985ac8c355SJohn Johansen 
5995ac8c355SJohn Johansen 	return 0;
6005ac8c355SJohn Johansen }
6015ac8c355SJohn Johansen 
6025d5182caSJohn Johansen static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
6035ac8c355SJohn Johansen {
6045d5182caSJohn Johansen 	struct aa_loaddata *data = seq->private;
6055ac8c355SJohn Johansen 	unsigned int i, size = aa_hash_size();
6065ac8c355SJohn Johansen 
6075d5182caSJohn Johansen 	if (data->hash) {
6085ac8c355SJohn Johansen 		for (i = 0; i < size; i++)
6095d5182caSJohn Johansen 			seq_printf(seq, "%.2x", data->hash[i]);
61047dbd1cdSMarkus Elfring 		seq_putc(seq, '\n');
6115ac8c355SJohn Johansen 	}
6125ac8c355SJohn Johansen 
6135ac8c355SJohn Johansen 	return 0;
6145ac8c355SJohn Johansen }
6155ac8c355SJohn Johansen 
6165d5182caSJohn Johansen SEQ_RAWDATA_FOPS(abi);
6175d5182caSJohn Johansen SEQ_RAWDATA_FOPS(revision);
6185d5182caSJohn Johansen SEQ_RAWDATA_FOPS(hash);
6195ac8c355SJohn Johansen 
6205ac8c355SJohn Johansen static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
6215ac8c355SJohn Johansen 			    loff_t *ppos)
6225ac8c355SJohn Johansen {
6235ac8c355SJohn Johansen 	struct aa_loaddata *rawdata = file->private_data;
6245ac8c355SJohn Johansen 
6255ac8c355SJohn Johansen 	return simple_read_from_buffer(buf, size, ppos, rawdata->data,
6265ac8c355SJohn Johansen 				       rawdata->size);
6275ac8c355SJohn Johansen }
6285ac8c355SJohn Johansen 
6295d5182caSJohn Johansen static int rawdata_release(struct inode *inode, struct file *file)
6305ac8c355SJohn Johansen {
6315d5182caSJohn Johansen 	aa_put_loaddata(file->private_data);
6325ac8c355SJohn Johansen 
6335ac8c355SJohn Johansen 	return 0;
6345ac8c355SJohn Johansen }
6355ac8c355SJohn Johansen 
6365d5182caSJohn Johansen static int rawdata_open(struct inode *inode, struct file *file)
6375d5182caSJohn Johansen {
6385d5182caSJohn Johansen 	if (!policy_view_capable(NULL))
6395d5182caSJohn Johansen 		return -EACCES;
6405d5182caSJohn Johansen 	file->private_data = __aa_get_loaddata(inode->i_private);
6415d5182caSJohn Johansen 	if (!file->private_data)
6425d5182caSJohn Johansen 		/* lost race: this entry is being reaped */
6435d5182caSJohn Johansen 		return -ENOENT;
6445d5182caSJohn Johansen 
6455d5182caSJohn Johansen 	return 0;
6465d5182caSJohn Johansen }
6475d5182caSJohn Johansen 
6485d5182caSJohn Johansen static const struct file_operations rawdata_fops = {
6495ac8c355SJohn Johansen 	.open = rawdata_open,
6505ac8c355SJohn Johansen 	.read = rawdata_read,
6515ac8c355SJohn Johansen 	.llseek = generic_file_llseek,
6525ac8c355SJohn Johansen 	.release = rawdata_release,
6535ac8c355SJohn Johansen };
6545ac8c355SJohn Johansen 
6555d5182caSJohn Johansen static void remove_rawdata_dents(struct aa_loaddata *rawdata)
6565d5182caSJohn Johansen {
6575d5182caSJohn Johansen 	int i;
6585d5182caSJohn Johansen 
6595d5182caSJohn Johansen 	for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
6605d5182caSJohn Johansen 		if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
6615d5182caSJohn Johansen 			/* no refcounts on i_private */
6625d5182caSJohn Johansen 			securityfs_remove(rawdata->dents[i]);
6635d5182caSJohn Johansen 			rawdata->dents[i] = NULL;
6645d5182caSJohn Johansen 		}
6655d5182caSJohn Johansen 	}
6665d5182caSJohn Johansen }
6675d5182caSJohn Johansen 
6685d5182caSJohn Johansen void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
6695d5182caSJohn Johansen {
6705d5182caSJohn Johansen 	AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
6715d5182caSJohn Johansen 
6725d5182caSJohn Johansen 	if (rawdata->ns) {
6735d5182caSJohn Johansen 		remove_rawdata_dents(rawdata);
6745d5182caSJohn Johansen 		list_del_init(&rawdata->list);
6755d5182caSJohn Johansen 		aa_put_ns(rawdata->ns);
6765d5182caSJohn Johansen 		rawdata->ns = NULL;
6775d5182caSJohn Johansen 	}
6785d5182caSJohn Johansen }
6795d5182caSJohn Johansen 
6805d5182caSJohn Johansen int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
6815d5182caSJohn Johansen {
6825d5182caSJohn Johansen 	struct dentry *dent, *dir;
6835d5182caSJohn Johansen 
6845d5182caSJohn Johansen 	AA_BUG(!ns);
6855d5182caSJohn Johansen 	AA_BUG(!rawdata);
6865d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
6875d5182caSJohn Johansen 	AA_BUG(!ns_subdata_dir(ns));
6885d5182caSJohn Johansen 
6895d5182caSJohn Johansen 	/*
6905d5182caSJohn Johansen 	 * just use ns revision dir was originally created at. This is
6915d5182caSJohn Johansen 	 * under ns->lock and if load is successful revision will be
6925d5182caSJohn Johansen 	 * bumped and is guaranteed to be unique
6935d5182caSJohn Johansen 	 */
6945d5182caSJohn Johansen 	rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
6955d5182caSJohn Johansen 	if (!rawdata->name)
6965d5182caSJohn Johansen 		return -ENOMEM;
6975d5182caSJohn Johansen 
6985d5182caSJohn Johansen 	dir = securityfs_create_dir(rawdata->name, ns_subdata_dir(ns));
6995d5182caSJohn Johansen 	if (IS_ERR(dir))
7005d5182caSJohn Johansen 		/* ->name freed when rawdata freed */
7015d5182caSJohn Johansen 		return PTR_ERR(dir);
7025d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DIR] = dir;
7035d5182caSJohn Johansen 
7045d5182caSJohn Johansen 	dent = securityfs_create_file("abi", S_IFREG | 0444, dir, rawdata,
7055d5182caSJohn Johansen 				      &seq_rawdata_abi_fops);
7065d5182caSJohn Johansen 	if (IS_ERR(dent))
7075d5182caSJohn Johansen 		goto fail;
7085d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_ABI] = dent;
7095d5182caSJohn Johansen 
7105d5182caSJohn Johansen 	dent = securityfs_create_file("revision", S_IFREG | 0444, dir, rawdata,
7115d5182caSJohn Johansen 				      &seq_rawdata_revision_fops);
7125d5182caSJohn Johansen 	if (IS_ERR(dent))
7135d5182caSJohn Johansen 		goto fail;
7145d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
7155d5182caSJohn Johansen 
7165d5182caSJohn Johansen 	if (aa_g_hash_policy) {
7175d5182caSJohn Johansen 		dent = securityfs_create_file("sha1", S_IFREG | 0444, dir,
7185d5182caSJohn Johansen 					      rawdata, &seq_rawdata_hash_fops);
7195d5182caSJohn Johansen 		if (IS_ERR(dent))
7205d5182caSJohn Johansen 			goto fail;
7215d5182caSJohn Johansen 		rawdata->dents[AAFS_LOADDATA_HASH] = dent;
7225d5182caSJohn Johansen 	}
7235d5182caSJohn Johansen 
7245d5182caSJohn Johansen 	dent = securityfs_create_file("raw_data", S_IFREG | 0444,
7255d5182caSJohn Johansen 				      dir, rawdata, &rawdata_fops);
7265d5182caSJohn Johansen 	if (IS_ERR(dent))
7275d5182caSJohn Johansen 		goto fail;
7285d5182caSJohn Johansen 	rawdata->dents[AAFS_LOADDATA_DATA] = dent;
7295d5182caSJohn Johansen 	d_inode(dent)->i_size = rawdata->size;
7305d5182caSJohn Johansen 
7315d5182caSJohn Johansen 	rawdata->ns = aa_get_ns(ns);
7325d5182caSJohn Johansen 	list_add(&rawdata->list, &ns->rawdata_list);
7335d5182caSJohn Johansen 	/* no refcount on inode rawdata */
7345d5182caSJohn Johansen 
7355d5182caSJohn Johansen 	return 0;
7365d5182caSJohn Johansen 
7375d5182caSJohn Johansen fail:
7385d5182caSJohn Johansen 	remove_rawdata_dents(rawdata);
7395d5182caSJohn Johansen 
7405d5182caSJohn Johansen 	return PTR_ERR(dent);
7415d5182caSJohn Johansen }
7425d5182caSJohn Johansen 
7430d259f04SJohn Johansen /** fns to setup dynamic per profile/namespace files **/
7440d259f04SJohn Johansen void __aa_fs_profile_rmdir(struct aa_profile *profile)
7450d259f04SJohn Johansen {
7460d259f04SJohn Johansen 	struct aa_profile *child;
7470d259f04SJohn Johansen 	int i;
7480d259f04SJohn Johansen 
7490d259f04SJohn Johansen 	if (!profile)
7500d259f04SJohn Johansen 		return;
7510d259f04SJohn Johansen 
7520d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list)
7530d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
7540d259f04SJohn Johansen 
7550d259f04SJohn Johansen 	for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
7568399588aSJohn Johansen 		struct aa_proxy *proxy;
7570d259f04SJohn Johansen 		if (!profile->dents[i])
7580d259f04SJohn Johansen 			continue;
7590d259f04SJohn Johansen 
7608399588aSJohn Johansen 		proxy = d_inode(profile->dents[i])->i_private;
7610d259f04SJohn Johansen 		securityfs_remove(profile->dents[i]);
7628399588aSJohn Johansen 		aa_put_proxy(proxy);
7630d259f04SJohn Johansen 		profile->dents[i] = NULL;
7640d259f04SJohn Johansen 	}
7650d259f04SJohn Johansen }
7660d259f04SJohn Johansen 
7670d259f04SJohn Johansen void __aa_fs_profile_migrate_dents(struct aa_profile *old,
7680d259f04SJohn Johansen 				   struct aa_profile *new)
7690d259f04SJohn Johansen {
7700d259f04SJohn Johansen 	int i;
7710d259f04SJohn Johansen 
7720d259f04SJohn Johansen 	for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
7730d259f04SJohn Johansen 		new->dents[i] = old->dents[i];
774d671e890SJohn Johansen 		if (new->dents[i])
775078cd827SDeepa Dinamani 			new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
7760d259f04SJohn Johansen 		old->dents[i] = NULL;
7770d259f04SJohn Johansen 	}
7780d259f04SJohn Johansen }
7790d259f04SJohn Johansen 
7800d259f04SJohn Johansen static struct dentry *create_profile_file(struct dentry *dir, const char *name,
7810d259f04SJohn Johansen 					  struct aa_profile *profile,
7820d259f04SJohn Johansen 					  const struct file_operations *fops)
7830d259f04SJohn Johansen {
7848399588aSJohn Johansen 	struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
7850d259f04SJohn Johansen 	struct dentry *dent;
7860d259f04SJohn Johansen 
7878399588aSJohn Johansen 	dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
7880d259f04SJohn Johansen 	if (IS_ERR(dent))
7898399588aSJohn Johansen 		aa_put_proxy(proxy);
7900d259f04SJohn Johansen 
7910d259f04SJohn Johansen 	return dent;
7920d259f04SJohn Johansen }
7930d259f04SJohn Johansen 
7945d5182caSJohn Johansen static int profile_depth(struct aa_profile *profile)
7955d5182caSJohn Johansen {
7965d5182caSJohn Johansen 	int depth = 0;
7975d5182caSJohn Johansen 
7985d5182caSJohn Johansen 	rcu_read_lock();
7995d5182caSJohn Johansen 	for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
8005d5182caSJohn Johansen 		depth++;
8015d5182caSJohn Johansen 	rcu_read_unlock();
8025d5182caSJohn Johansen 
8035d5182caSJohn Johansen 	return depth;
8045d5182caSJohn Johansen }
8055d5182caSJohn Johansen 
8065d5182caSJohn Johansen static int gen_symlink_name(char *buffer, size_t bsize, int depth,
8075d5182caSJohn Johansen 			    const char *dirname, const char *fname)
8085d5182caSJohn Johansen {
8095d5182caSJohn Johansen 	int error;
8105d5182caSJohn Johansen 
8115d5182caSJohn Johansen 	for (; depth > 0; depth--) {
8125d5182caSJohn Johansen 		if (bsize < 7)
8135d5182caSJohn Johansen 			return -ENAMETOOLONG;
8145d5182caSJohn Johansen 		strcpy(buffer, "../../");
8155d5182caSJohn Johansen 		buffer += 6;
8165d5182caSJohn Johansen 		bsize -= 6;
8175d5182caSJohn Johansen 	}
8185d5182caSJohn Johansen 
8195d5182caSJohn Johansen 	error = snprintf(buffer, bsize, "raw_data/%s/%s", dirname, fname);
8205d5182caSJohn Johansen 	if (error >= bsize || error < 0)
8215d5182caSJohn Johansen 		return -ENAMETOOLONG;
8225d5182caSJohn Johansen 
8235d5182caSJohn Johansen 	return 0;
8245d5182caSJohn Johansen }
8255d5182caSJohn Johansen 
8265d5182caSJohn Johansen /*
8275d5182caSJohn Johansen  * Requires: @profile->ns->lock held
8285d5182caSJohn Johansen  */
8290d259f04SJohn Johansen int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
8300d259f04SJohn Johansen {
8310d259f04SJohn Johansen 	struct aa_profile *child;
8320d259f04SJohn Johansen 	struct dentry *dent = NULL, *dir;
8330d259f04SJohn Johansen 	int error;
8340d259f04SJohn Johansen 
8350d259f04SJohn Johansen 	if (!parent) {
8360d259f04SJohn Johansen 		struct aa_profile *p;
8370d259f04SJohn Johansen 		p = aa_deref_parent(profile);
8380d259f04SJohn Johansen 		dent = prof_dir(p);
8390d259f04SJohn Johansen 		/* adding to parent that previously didn't have children */
8400d259f04SJohn Johansen 		dent = securityfs_create_dir("profiles", dent);
8410d259f04SJohn Johansen 		if (IS_ERR(dent))
8420d259f04SJohn Johansen 			goto fail;
8430d259f04SJohn Johansen 		prof_child_dir(p) = parent = dent;
8440d259f04SJohn Johansen 	}
8450d259f04SJohn Johansen 
8460d259f04SJohn Johansen 	if (!profile->dirname) {
8470d259f04SJohn Johansen 		int len, id_len;
8480d259f04SJohn Johansen 		len = mangle_name(profile->base.name, NULL);
8490d259f04SJohn Johansen 		id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
8500d259f04SJohn Johansen 
8510d259f04SJohn Johansen 		profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
852ffac1de6SDan Carpenter 		if (!profile->dirname) {
853ffac1de6SDan Carpenter 			error = -ENOMEM;
854ffac1de6SDan Carpenter 			goto fail2;
855ffac1de6SDan Carpenter 		}
8560d259f04SJohn Johansen 
8570d259f04SJohn Johansen 		mangle_name(profile->base.name, profile->dirname);
8580d259f04SJohn Johansen 		sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
8590d259f04SJohn Johansen 	}
8600d259f04SJohn Johansen 
8610d259f04SJohn Johansen 	dent = securityfs_create_dir(profile->dirname, parent);
8620d259f04SJohn Johansen 	if (IS_ERR(dent))
8630d259f04SJohn Johansen 		goto fail;
8640d259f04SJohn Johansen 	prof_dir(profile) = dir = dent;
8650d259f04SJohn Johansen 
86652b97de3SJohn Johansen 	dent = create_profile_file(dir, "name", profile,
86752b97de3SJohn Johansen 				   &seq_profile_name_fops);
8680d259f04SJohn Johansen 	if (IS_ERR(dent))
8690d259f04SJohn Johansen 		goto fail;
8700d259f04SJohn Johansen 	profile->dents[AAFS_PROF_NAME] = dent;
8710d259f04SJohn Johansen 
87252b97de3SJohn Johansen 	dent = create_profile_file(dir, "mode", profile,
87352b97de3SJohn Johansen 				   &seq_profile_mode_fops);
8740d259f04SJohn Johansen 	if (IS_ERR(dent))
8750d259f04SJohn Johansen 		goto fail;
8760d259f04SJohn Johansen 	profile->dents[AAFS_PROF_MODE] = dent;
8770d259f04SJohn Johansen 
878556d0be7SJohn Johansen 	dent = create_profile_file(dir, "attach", profile,
87952b97de3SJohn Johansen 				   &seq_profile_attach_fops);
880556d0be7SJohn Johansen 	if (IS_ERR(dent))
881556d0be7SJohn Johansen 		goto fail;
882556d0be7SJohn Johansen 	profile->dents[AAFS_PROF_ATTACH] = dent;
883556d0be7SJohn Johansen 
884f8eb8a13SJohn Johansen 	if (profile->hash) {
885f8eb8a13SJohn Johansen 		dent = create_profile_file(dir, "sha1", profile,
88652b97de3SJohn Johansen 					   &seq_profile_hash_fops);
887f8eb8a13SJohn Johansen 		if (IS_ERR(dent))
888f8eb8a13SJohn Johansen 			goto fail;
889f8eb8a13SJohn Johansen 		profile->dents[AAFS_PROF_HASH] = dent;
890f8eb8a13SJohn Johansen 	}
891f8eb8a13SJohn Johansen 
8925ac8c355SJohn Johansen 	if (profile->rawdata) {
8935d5182caSJohn Johansen 		char target[64];
8945d5182caSJohn Johansen 		int depth = profile_depth(profile);
8955d5182caSJohn Johansen 
8965d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
8975d5182caSJohn Johansen 					 profile->rawdata->name, "sha1");
8985d5182caSJohn Johansen 		if (error < 0)
8995d5182caSJohn Johansen 			goto fail2;
9005d5182caSJohn Johansen 		dent = securityfs_create_symlink("raw_sha1", dir, target, NULL);
9015ac8c355SJohn Johansen 		if (IS_ERR(dent))
9025ac8c355SJohn Johansen 			goto fail;
9035ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_HASH] = dent;
9045ac8c355SJohn Johansen 
9055d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
9065d5182caSJohn Johansen 					 profile->rawdata->name, "abi");
9075d5182caSJohn Johansen 		if (error < 0)
9085d5182caSJohn Johansen 			goto fail2;
9095d5182caSJohn Johansen 		dent = securityfs_create_symlink("raw_abi", dir, target, NULL);
9105ac8c355SJohn Johansen 		if (IS_ERR(dent))
9115ac8c355SJohn Johansen 			goto fail;
9125ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_ABI] = dent;
9135ac8c355SJohn Johansen 
9145d5182caSJohn Johansen 		error = gen_symlink_name(target, sizeof(target), depth,
9155d5182caSJohn Johansen 					 profile->rawdata->name, "raw_data");
9165d5182caSJohn Johansen 		if (error < 0)
9175d5182caSJohn Johansen 			goto fail2;
9185d5182caSJohn Johansen 		dent = securityfs_create_symlink("raw_data", dir, target, NULL);
9195ac8c355SJohn Johansen 		if (IS_ERR(dent))
9205ac8c355SJohn Johansen 			goto fail;
9215ac8c355SJohn Johansen 		profile->dents[AAFS_PROF_RAW_DATA] = dent;
9225ac8c355SJohn Johansen 	}
9235ac8c355SJohn Johansen 
9240d259f04SJohn Johansen 	list_for_each_entry(child, &profile->base.profiles, base.list) {
9250d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
9260d259f04SJohn Johansen 		if (error)
9270d259f04SJohn Johansen 			goto fail2;
9280d259f04SJohn Johansen 	}
9290d259f04SJohn Johansen 
9300d259f04SJohn Johansen 	return 0;
9310d259f04SJohn Johansen 
9320d259f04SJohn Johansen fail:
9330d259f04SJohn Johansen 	error = PTR_ERR(dent);
9340d259f04SJohn Johansen 
9350d259f04SJohn Johansen fail2:
9360d259f04SJohn Johansen 	__aa_fs_profile_rmdir(profile);
9370d259f04SJohn Johansen 
9380d259f04SJohn Johansen 	return error;
9390d259f04SJohn Johansen }
9400d259f04SJohn Johansen 
9415d5182caSJohn Johansen static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
9425d5182caSJohn Johansen {
9435d5182caSJohn Johansen 	struct aa_loaddata *ent, *tmp;
9445d5182caSJohn Johansen 
9455d5182caSJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
9465d5182caSJohn Johansen 
9475d5182caSJohn Johansen 	list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
9485d5182caSJohn Johansen 		__aa_fs_remove_rawdata(ent);
9495d5182caSJohn Johansen }
9505d5182caSJohn Johansen 
95198849dffSJohn Johansen void __aa_fs_ns_rmdir(struct aa_ns *ns)
9520d259f04SJohn Johansen {
95398849dffSJohn Johansen 	struct aa_ns *sub;
9540d259f04SJohn Johansen 	struct aa_profile *child;
9550d259f04SJohn Johansen 	int i;
9560d259f04SJohn Johansen 
9570d259f04SJohn Johansen 	if (!ns)
9580d259f04SJohn Johansen 		return;
9590d259f04SJohn Johansen 
9600d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list)
9610d259f04SJohn Johansen 		__aa_fs_profile_rmdir(child);
9620d259f04SJohn Johansen 
9630d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
9640d259f04SJohn Johansen 		mutex_lock(&sub->lock);
96598849dffSJohn Johansen 		__aa_fs_ns_rmdir(sub);
9660d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
9670d259f04SJohn Johansen 	}
9680d259f04SJohn Johansen 
9695d5182caSJohn Johansen 	__aa_fs_list_remove_rawdata(ns);
9705d5182caSJohn Johansen 
971b7fd2c03SJohn Johansen 	if (ns_subns_dir(ns)) {
972b7fd2c03SJohn Johansen 		sub = d_inode(ns_subns_dir(ns))->i_private;
973b7fd2c03SJohn Johansen 		aa_put_ns(sub);
974b7fd2c03SJohn Johansen 	}
975b7fd2c03SJohn Johansen 	if (ns_subload(ns)) {
976b7fd2c03SJohn Johansen 		sub = d_inode(ns_subload(ns))->i_private;
977b7fd2c03SJohn Johansen 		aa_put_ns(sub);
978b7fd2c03SJohn Johansen 	}
979b7fd2c03SJohn Johansen 	if (ns_subreplace(ns)) {
980b7fd2c03SJohn Johansen 		sub = d_inode(ns_subreplace(ns))->i_private;
981b7fd2c03SJohn Johansen 		aa_put_ns(sub);
982b7fd2c03SJohn Johansen 	}
983b7fd2c03SJohn Johansen 	if (ns_subremove(ns)) {
984b7fd2c03SJohn Johansen 		sub = d_inode(ns_subremove(ns))->i_private;
985b7fd2c03SJohn Johansen 		aa_put_ns(sub);
986b7fd2c03SJohn Johansen 	}
987b7fd2c03SJohn Johansen 
9880d259f04SJohn Johansen 	for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
9890d259f04SJohn Johansen 		securityfs_remove(ns->dents[i]);
9900d259f04SJohn Johansen 		ns->dents[i] = NULL;
9910d259f04SJohn Johansen 	}
9920d259f04SJohn Johansen }
9930d259f04SJohn Johansen 
994b7fd2c03SJohn Johansen /* assumes cleanup in caller */
995b7fd2c03SJohn Johansen static int __aa_fs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
996b7fd2c03SJohn Johansen {
997b7fd2c03SJohn Johansen 	struct dentry *dent;
998b7fd2c03SJohn Johansen 
999b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1000b7fd2c03SJohn Johansen 	AA_BUG(!dir);
1001b7fd2c03SJohn Johansen 
1002b7fd2c03SJohn Johansen 	dent = securityfs_create_dir("profiles", dir);
1003b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1004b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1005b7fd2c03SJohn Johansen 	ns_subprofs_dir(ns) = dent;
1006b7fd2c03SJohn Johansen 
1007b7fd2c03SJohn Johansen 	dent = securityfs_create_dir("raw_data", dir);
1008b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1009b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1010b7fd2c03SJohn Johansen 	ns_subdata_dir(ns) = dent;
1011b7fd2c03SJohn Johansen 
1012b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".load", 0640, dir, ns,
1013b7fd2c03SJohn Johansen 				      &aa_fs_profile_load);
1014b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1015b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1016b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1017b7fd2c03SJohn Johansen 	ns_subload(ns) = dent;
1018b7fd2c03SJohn Johansen 
1019b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".replace", 0640, dir, ns,
1020b7fd2c03SJohn Johansen 				      &aa_fs_profile_replace);
1021b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1022b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1023b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1024b7fd2c03SJohn Johansen 	ns_subreplace(ns) = dent;
1025b7fd2c03SJohn Johansen 
1026b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".remove", 0640, dir, ns,
1027b7fd2c03SJohn Johansen 				      &aa_fs_profile_remove);
1028b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1029b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1030b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1031b7fd2c03SJohn Johansen 	ns_subremove(ns) = dent;
1032b7fd2c03SJohn Johansen 
1033b7fd2c03SJohn Johansen 	dent = securityfs_create_dir("namespaces", dir);
1034b7fd2c03SJohn Johansen 	if (IS_ERR(dent))
1035b7fd2c03SJohn Johansen 		return PTR_ERR(dent);
1036b7fd2c03SJohn Johansen 	aa_get_ns(ns);
1037b7fd2c03SJohn Johansen 	ns_subns_dir(ns) = dent;
1038b7fd2c03SJohn Johansen 
1039b7fd2c03SJohn Johansen 	return 0;
1040b7fd2c03SJohn Johansen }
1041b7fd2c03SJohn Johansen 
104298849dffSJohn Johansen int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
10430d259f04SJohn Johansen {
104498849dffSJohn Johansen 	struct aa_ns *sub;
10450d259f04SJohn Johansen 	struct aa_profile *child;
10460d259f04SJohn Johansen 	struct dentry *dent, *dir;
10470d259f04SJohn Johansen 	int error;
10480d259f04SJohn Johansen 
1049b7fd2c03SJohn Johansen 	AA_BUG(!ns);
1050b7fd2c03SJohn Johansen 	AA_BUG(!parent);
1051b7fd2c03SJohn Johansen 	AA_BUG(!mutex_is_locked(&ns->lock));
1052b7fd2c03SJohn Johansen 
10530d259f04SJohn Johansen 	if (!name)
10540d259f04SJohn Johansen 		name = ns->base.name;
10550d259f04SJohn Johansen 
1056b7fd2c03SJohn Johansen 	/* create ns dir if it doesn't already exist */
10570d259f04SJohn Johansen 	dent = securityfs_create_dir(name, parent);
10580d259f04SJohn Johansen 	if (IS_ERR(dent))
10590d259f04SJohn Johansen 		goto fail;
1060b7fd2c03SJohn Johansen 
10610d259f04SJohn Johansen 	ns_dir(ns) = dir = dent;
1062b7fd2c03SJohn Johansen 	error = __aa_fs_ns_mkdir_entries(ns, dir);
1063b7fd2c03SJohn Johansen 	if (error)
1064b7fd2c03SJohn Johansen 		goto fail2;
10650d259f04SJohn Johansen 
1066b7fd2c03SJohn Johansen 	/* profiles */
10670d259f04SJohn Johansen 	list_for_each_entry(child, &ns->base.profiles, base.list) {
10680d259f04SJohn Johansen 		error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
10690d259f04SJohn Johansen 		if (error)
10700d259f04SJohn Johansen 			goto fail2;
10710d259f04SJohn Johansen 	}
10720d259f04SJohn Johansen 
1073b7fd2c03SJohn Johansen 	/* subnamespaces */
10740d259f04SJohn Johansen 	list_for_each_entry(sub, &ns->sub_ns, base.list) {
10750d259f04SJohn Johansen 		mutex_lock(&sub->lock);
107698849dffSJohn Johansen 		error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
10770d259f04SJohn Johansen 		mutex_unlock(&sub->lock);
10780d259f04SJohn Johansen 		if (error)
10790d259f04SJohn Johansen 			goto fail2;
10800d259f04SJohn Johansen 	}
10810d259f04SJohn Johansen 
10820d259f04SJohn Johansen 	return 0;
10830d259f04SJohn Johansen 
10840d259f04SJohn Johansen fail:
10850d259f04SJohn Johansen 	error = PTR_ERR(dent);
10860d259f04SJohn Johansen 
10870d259f04SJohn Johansen fail2:
108898849dffSJohn Johansen 	__aa_fs_ns_rmdir(ns);
10890d259f04SJohn Johansen 
10900d259f04SJohn Johansen 	return error;
10910d259f04SJohn Johansen }
10920d259f04SJohn Johansen 
10930d259f04SJohn Johansen 
109429b3822fSJohn Johansen #define list_entry_is_head(pos, head, member) (&pos->member == (head))
109529b3822fSJohn Johansen 
109629b3822fSJohn Johansen /**
109798849dffSJohn Johansen  * __next_ns - find the next namespace to list
109829b3822fSJohn Johansen  * @root: root namespace to stop search at (NOT NULL)
109929b3822fSJohn Johansen  * @ns: current ns position (NOT NULL)
110029b3822fSJohn Johansen  *
110129b3822fSJohn Johansen  * Find the next namespace from @ns under @root and handle all locking needed
110229b3822fSJohn Johansen  * while switching current namespace.
110329b3822fSJohn Johansen  *
110429b3822fSJohn Johansen  * Returns: next namespace or NULL if at last namespace under @root
110529b3822fSJohn Johansen  * Requires: ns->parent->lock to be held
110629b3822fSJohn Johansen  * NOTE: will not unlock root->lock
110729b3822fSJohn Johansen  */
110898849dffSJohn Johansen static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
110929b3822fSJohn Johansen {
111098849dffSJohn Johansen 	struct aa_ns *parent, *next;
111129b3822fSJohn Johansen 
111229b3822fSJohn Johansen 	/* is next namespace a child */
111329b3822fSJohn Johansen 	if (!list_empty(&ns->sub_ns)) {
111429b3822fSJohn Johansen 		next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
111529b3822fSJohn Johansen 		mutex_lock(&next->lock);
111629b3822fSJohn Johansen 		return next;
111729b3822fSJohn Johansen 	}
111829b3822fSJohn Johansen 
111929b3822fSJohn Johansen 	/* check if the next ns is a sibling, parent, gp, .. */
112029b3822fSJohn Johansen 	parent = ns->parent;
1121ed2c7da3SJohn Johansen 	while (ns != root) {
112229b3822fSJohn Johansen 		mutex_unlock(&ns->lock);
112338dbd7d8SGeliang Tang 		next = list_next_entry(ns, base.list);
112429b3822fSJohn Johansen 		if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
112529b3822fSJohn Johansen 			mutex_lock(&next->lock);
112629b3822fSJohn Johansen 			return next;
112729b3822fSJohn Johansen 		}
112829b3822fSJohn Johansen 		ns = parent;
112929b3822fSJohn Johansen 		parent = parent->parent;
113029b3822fSJohn Johansen 	}
113129b3822fSJohn Johansen 
113229b3822fSJohn Johansen 	return NULL;
113329b3822fSJohn Johansen }
113429b3822fSJohn Johansen 
113529b3822fSJohn Johansen /**
113629b3822fSJohn Johansen  * __first_profile - find the first profile in a namespace
113729b3822fSJohn Johansen  * @root: namespace that is root of profiles being displayed (NOT NULL)
113829b3822fSJohn Johansen  * @ns: namespace to start in   (NOT NULL)
113929b3822fSJohn Johansen  *
114029b3822fSJohn Johansen  * Returns: unrefcounted profile or NULL if no profile
114129b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
114229b3822fSJohn Johansen  */
114398849dffSJohn Johansen static struct aa_profile *__first_profile(struct aa_ns *root,
114498849dffSJohn Johansen 					  struct aa_ns *ns)
114529b3822fSJohn Johansen {
114698849dffSJohn Johansen 	for (; ns; ns = __next_ns(root, ns)) {
114729b3822fSJohn Johansen 		if (!list_empty(&ns->base.profiles))
114829b3822fSJohn Johansen 			return list_first_entry(&ns->base.profiles,
114929b3822fSJohn Johansen 						struct aa_profile, base.list);
115029b3822fSJohn Johansen 	}
115129b3822fSJohn Johansen 	return NULL;
115229b3822fSJohn Johansen }
115329b3822fSJohn Johansen 
115429b3822fSJohn Johansen /**
115529b3822fSJohn Johansen  * __next_profile - step to the next profile in a profile tree
115629b3822fSJohn Johansen  * @profile: current profile in tree (NOT NULL)
115729b3822fSJohn Johansen  *
115829b3822fSJohn Johansen  * Perform a depth first traversal on the profile tree in a namespace
115929b3822fSJohn Johansen  *
116029b3822fSJohn Johansen  * Returns: next profile or NULL if done
116129b3822fSJohn Johansen  * Requires: profile->ns.lock to be held
116229b3822fSJohn Johansen  */
116329b3822fSJohn Johansen static struct aa_profile *__next_profile(struct aa_profile *p)
116429b3822fSJohn Johansen {
116529b3822fSJohn Johansen 	struct aa_profile *parent;
116698849dffSJohn Johansen 	struct aa_ns *ns = p->ns;
116729b3822fSJohn Johansen 
116829b3822fSJohn Johansen 	/* is next profile a child */
116929b3822fSJohn Johansen 	if (!list_empty(&p->base.profiles))
117029b3822fSJohn Johansen 		return list_first_entry(&p->base.profiles, typeof(*p),
117129b3822fSJohn Johansen 					base.list);
117229b3822fSJohn Johansen 
117329b3822fSJohn Johansen 	/* is next profile a sibling, parent sibling, gp, sibling, .. */
117429b3822fSJohn Johansen 	parent = rcu_dereference_protected(p->parent,
117529b3822fSJohn Johansen 					   mutex_is_locked(&p->ns->lock));
117629b3822fSJohn Johansen 	while (parent) {
117738dbd7d8SGeliang Tang 		p = list_next_entry(p, base.list);
117829b3822fSJohn Johansen 		if (!list_entry_is_head(p, &parent->base.profiles, base.list))
117929b3822fSJohn Johansen 			return p;
118029b3822fSJohn Johansen 		p = parent;
118129b3822fSJohn Johansen 		parent = rcu_dereference_protected(parent->parent,
118229b3822fSJohn Johansen 					    mutex_is_locked(&parent->ns->lock));
118329b3822fSJohn Johansen 	}
118429b3822fSJohn Johansen 
118529b3822fSJohn Johansen 	/* is next another profile in the namespace */
118638dbd7d8SGeliang Tang 	p = list_next_entry(p, base.list);
118729b3822fSJohn Johansen 	if (!list_entry_is_head(p, &ns->base.profiles, base.list))
118829b3822fSJohn Johansen 		return p;
118929b3822fSJohn Johansen 
119029b3822fSJohn Johansen 	return NULL;
119129b3822fSJohn Johansen }
119229b3822fSJohn Johansen 
119329b3822fSJohn Johansen /**
119429b3822fSJohn Johansen  * next_profile - step to the next profile in where ever it may be
119529b3822fSJohn Johansen  * @root: root namespace  (NOT NULL)
119629b3822fSJohn Johansen  * @profile: current profile  (NOT NULL)
119729b3822fSJohn Johansen  *
119829b3822fSJohn Johansen  * Returns: next profile or NULL if there isn't one
119929b3822fSJohn Johansen  */
120098849dffSJohn Johansen static struct aa_profile *next_profile(struct aa_ns *root,
120129b3822fSJohn Johansen 				       struct aa_profile *profile)
120229b3822fSJohn Johansen {
120329b3822fSJohn Johansen 	struct aa_profile *next = __next_profile(profile);
120429b3822fSJohn Johansen 	if (next)
120529b3822fSJohn Johansen 		return next;
120629b3822fSJohn Johansen 
120729b3822fSJohn Johansen 	/* finished all profiles in namespace move to next namespace */
120898849dffSJohn Johansen 	return __first_profile(root, __next_ns(root, profile->ns));
120929b3822fSJohn Johansen }
121029b3822fSJohn Johansen 
121129b3822fSJohn Johansen /**
121229b3822fSJohn Johansen  * p_start - start a depth first traversal of profile tree
121329b3822fSJohn Johansen  * @f: seq_file to fill
121429b3822fSJohn Johansen  * @pos: current position
121529b3822fSJohn Johansen  *
121629b3822fSJohn Johansen  * Returns: first profile under current namespace or NULL if none found
121729b3822fSJohn Johansen  *
121829b3822fSJohn Johansen  * acquires first ns->lock
121929b3822fSJohn Johansen  */
122029b3822fSJohn Johansen static void *p_start(struct seq_file *f, loff_t *pos)
122129b3822fSJohn Johansen {
122229b3822fSJohn Johansen 	struct aa_profile *profile = NULL;
122398849dffSJohn Johansen 	struct aa_ns *root = aa_current_profile()->ns;
122429b3822fSJohn Johansen 	loff_t l = *pos;
122598849dffSJohn Johansen 	f->private = aa_get_ns(root);
122629b3822fSJohn Johansen 
122729b3822fSJohn Johansen 
122829b3822fSJohn Johansen 	/* find the first profile */
122929b3822fSJohn Johansen 	mutex_lock(&root->lock);
123029b3822fSJohn Johansen 	profile = __first_profile(root, root);
123129b3822fSJohn Johansen 
123229b3822fSJohn Johansen 	/* skip to position */
123329b3822fSJohn Johansen 	for (; profile && l > 0; l--)
123429b3822fSJohn Johansen 		profile = next_profile(root, profile);
123529b3822fSJohn Johansen 
123629b3822fSJohn Johansen 	return profile;
123729b3822fSJohn Johansen }
123829b3822fSJohn Johansen 
123929b3822fSJohn Johansen /**
124029b3822fSJohn Johansen  * p_next - read the next profile entry
124129b3822fSJohn Johansen  * @f: seq_file to fill
124229b3822fSJohn Johansen  * @p: profile previously returned
124329b3822fSJohn Johansen  * @pos: current position
124429b3822fSJohn Johansen  *
124529b3822fSJohn Johansen  * Returns: next profile after @p or NULL if none
124629b3822fSJohn Johansen  *
124729b3822fSJohn Johansen  * may acquire/release locks in namespace tree as necessary
124829b3822fSJohn Johansen  */
124929b3822fSJohn Johansen static void *p_next(struct seq_file *f, void *p, loff_t *pos)
125029b3822fSJohn Johansen {
125129b3822fSJohn Johansen 	struct aa_profile *profile = p;
125298849dffSJohn Johansen 	struct aa_ns *ns = f->private;
125329b3822fSJohn Johansen 	(*pos)++;
125429b3822fSJohn Johansen 
125529b3822fSJohn Johansen 	return next_profile(ns, profile);
125629b3822fSJohn Johansen }
125729b3822fSJohn Johansen 
125829b3822fSJohn Johansen /**
125929b3822fSJohn Johansen  * p_stop - stop depth first traversal
126029b3822fSJohn Johansen  * @f: seq_file we are filling
126129b3822fSJohn Johansen  * @p: the last profile writen
126229b3822fSJohn Johansen  *
126329b3822fSJohn Johansen  * Release all locking done by p_start/p_next on namespace tree
126429b3822fSJohn Johansen  */
126529b3822fSJohn Johansen static void p_stop(struct seq_file *f, void *p)
126629b3822fSJohn Johansen {
126729b3822fSJohn Johansen 	struct aa_profile *profile = p;
126898849dffSJohn Johansen 	struct aa_ns *root = f->private, *ns;
126929b3822fSJohn Johansen 
127029b3822fSJohn Johansen 	if (profile) {
127129b3822fSJohn Johansen 		for (ns = profile->ns; ns && ns != root; ns = ns->parent)
127229b3822fSJohn Johansen 			mutex_unlock(&ns->lock);
127329b3822fSJohn Johansen 	}
127429b3822fSJohn Johansen 	mutex_unlock(&root->lock);
127598849dffSJohn Johansen 	aa_put_ns(root);
127629b3822fSJohn Johansen }
127729b3822fSJohn Johansen 
127829b3822fSJohn Johansen /**
127929b3822fSJohn Johansen  * seq_show_profile - show a profile entry
128029b3822fSJohn Johansen  * @f: seq_file to file
128129b3822fSJohn Johansen  * @p: current position (profile)    (NOT NULL)
128229b3822fSJohn Johansen  *
128329b3822fSJohn Johansen  * Returns: error on failure
128429b3822fSJohn Johansen  */
128529b3822fSJohn Johansen static int seq_show_profile(struct seq_file *f, void *p)
128629b3822fSJohn Johansen {
128729b3822fSJohn Johansen 	struct aa_profile *profile = (struct aa_profile *)p;
128898849dffSJohn Johansen 	struct aa_ns *root = f->private;
128929b3822fSJohn Johansen 
129029b3822fSJohn Johansen 	if (profile->ns != root)
129192b6d8efSJohn Johansen 		seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
129229b3822fSJohn Johansen 	seq_printf(f, "%s (%s)\n", profile->base.hname,
129329b3822fSJohn Johansen 		   aa_profile_mode_names[profile->mode]);
129429b3822fSJohn Johansen 
129529b3822fSJohn Johansen 	return 0;
129629b3822fSJohn Johansen }
129729b3822fSJohn Johansen 
129829b3822fSJohn Johansen static const struct seq_operations aa_fs_profiles_op = {
129929b3822fSJohn Johansen 	.start = p_start,
130029b3822fSJohn Johansen 	.next = p_next,
130129b3822fSJohn Johansen 	.stop = p_stop,
130229b3822fSJohn Johansen 	.show = seq_show_profile,
130329b3822fSJohn Johansen };
130429b3822fSJohn Johansen 
130529b3822fSJohn Johansen static int profiles_open(struct inode *inode, struct file *file)
130629b3822fSJohn Johansen {
13075ac8c355SJohn Johansen 	if (!policy_view_capable(NULL))
13085ac8c355SJohn Johansen 		return -EACCES;
13095ac8c355SJohn Johansen 
131029b3822fSJohn Johansen 	return seq_open(file, &aa_fs_profiles_op);
131129b3822fSJohn Johansen }
131229b3822fSJohn Johansen 
131329b3822fSJohn Johansen static int profiles_release(struct inode *inode, struct file *file)
131429b3822fSJohn Johansen {
131529b3822fSJohn Johansen 	return seq_release(inode, file);
131629b3822fSJohn Johansen }
131729b3822fSJohn Johansen 
131829b3822fSJohn Johansen static const struct file_operations aa_fs_profiles_fops = {
131929b3822fSJohn Johansen 	.open = profiles_open,
132029b3822fSJohn Johansen 	.read = seq_read,
132129b3822fSJohn Johansen 	.llseek = seq_lseek,
132229b3822fSJohn Johansen 	.release = profiles_release,
132329b3822fSJohn Johansen };
132429b3822fSJohn Johansen 
132529b3822fSJohn Johansen 
13260d259f04SJohn Johansen /** Base file system setup **/
1327a9bf8e9fSKees Cook static struct aa_fs_entry aa_fs_entry_file[] = {
1328a9bf8e9fSKees Cook 	AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
1329a9bf8e9fSKees Cook 				  "link lock"),
1330a9bf8e9fSKees Cook 	{ }
1331a9bf8e9fSKees Cook };
1332a9bf8e9fSKees Cook 
1333e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_domain[] = {
1334e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hat",	1),
1335e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_hatv",	1),
1336e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_onexec",	1),
1337e74abcf3SKees Cook 	AA_FS_FILE_BOOLEAN("change_profile",	1),
133834c426acSJohn Johansen 	AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap",	1),
1339aa9a39adSJohn Johansen 	AA_FS_FILE_STRING("version", "1.2"),
1340e74abcf3SKees Cook 	{ }
1341e74abcf3SKees Cook };
1342e74abcf3SKees Cook 
1343474d6b75SJohn Johansen static struct aa_fs_entry aa_fs_entry_versions[] = {
1344474d6b75SJohn Johansen 	AA_FS_FILE_BOOLEAN("v5",	1),
1345474d6b75SJohn Johansen 	{ }
1346474d6b75SJohn Johansen };
1347474d6b75SJohn Johansen 
13489d910a3bSJohn Johansen static struct aa_fs_entry aa_fs_entry_policy[] = {
1349474d6b75SJohn Johansen 	AA_FS_DIR("versions",                   aa_fs_entry_versions),
1350dd51c848SJohn Johansen 	AA_FS_FILE_BOOLEAN("set_load",		1),
13519d910a3bSJohn Johansen 	{ }
13529d910a3bSJohn Johansen };
13539d910a3bSJohn Johansen 
1354e74abcf3SKees Cook static struct aa_fs_entry aa_fs_entry_features[] = {
13559d910a3bSJohn Johansen 	AA_FS_DIR("policy",			aa_fs_entry_policy),
1356e74abcf3SKees Cook 	AA_FS_DIR("domain",			aa_fs_entry_domain),
1357a9bf8e9fSKees Cook 	AA_FS_DIR("file",			aa_fs_entry_file),
1358e74abcf3SKees Cook 	AA_FS_FILE_U64("capability",		VFS_CAP_FLAGS_MASK),
1359d384b0a1SKees Cook 	AA_FS_DIR("rlimit",			aa_fs_entry_rlimit),
136084f1f787SJohn Johansen 	AA_FS_DIR("caps",			aa_fs_entry_caps),
1361e74abcf3SKees Cook 	{ }
1362e74abcf3SKees Cook };
1363e74abcf3SKees Cook 
13649acd494bSKees Cook static struct aa_fs_entry aa_fs_entry_apparmor[] = {
1365e025be0fSWilliam Hua 	AA_FS_FILE_FOPS(".access", 0640, &aa_fs_access),
1366a71ada30SJohn Johansen 	AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level),
13673e3e5695SJohn Johansen 	AA_FS_FILE_FOPS(".ns_name", 0640, &aa_fs_ns_name),
1368b7fd2c03SJohn Johansen 	AA_FS_FILE_FOPS("profiles", 0440, &aa_fs_profiles_fops),
1369e74abcf3SKees Cook 	AA_FS_DIR("features", aa_fs_entry_features),
13709acd494bSKees Cook 	{ }
13719acd494bSKees Cook };
137263e2b423SJohn Johansen 
13739acd494bSKees Cook static struct aa_fs_entry aa_fs_entry =
13749acd494bSKees Cook 	AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
13759acd494bSKees Cook 
13769acd494bSKees Cook /**
13779acd494bSKees Cook  * aafs_create_file - create a file entry in the apparmor securityfs
13789acd494bSKees Cook  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
13799acd494bSKees Cook  * @parent: the parent dentry in the securityfs
13809acd494bSKees Cook  *
13819acd494bSKees Cook  * Use aafs_remove_file to remove entries created with this fn.
13829acd494bSKees Cook  */
13839acd494bSKees Cook static int __init aafs_create_file(struct aa_fs_entry *fs_file,
13849acd494bSKees Cook 				   struct dentry *parent)
138563e2b423SJohn Johansen {
13869acd494bSKees Cook 	int error = 0;
138763e2b423SJohn Johansen 
13889acd494bSKees Cook 	fs_file->dentry = securityfs_create_file(fs_file->name,
13899acd494bSKees Cook 						 S_IFREG | fs_file->mode,
13909acd494bSKees Cook 						 parent, fs_file,
13919acd494bSKees Cook 						 fs_file->file_ops);
13929acd494bSKees Cook 	if (IS_ERR(fs_file->dentry)) {
13939acd494bSKees Cook 		error = PTR_ERR(fs_file->dentry);
13949acd494bSKees Cook 		fs_file->dentry = NULL;
139563e2b423SJohn Johansen 	}
13969acd494bSKees Cook 	return error;
139763e2b423SJohn Johansen }
139863e2b423SJohn Johansen 
13990d259f04SJohn Johansen static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
140063e2b423SJohn Johansen /**
14019acd494bSKees Cook  * aafs_create_dir - recursively create a directory entry in the securityfs
14029acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
14039acd494bSKees Cook  * @parent: the parent dentry in the securityfs
140463e2b423SJohn Johansen  *
14059acd494bSKees Cook  * Use aafs_remove_dir to remove entries created with this fn.
140663e2b423SJohn Johansen  */
14079acd494bSKees Cook static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
14089acd494bSKees Cook 				  struct dentry *parent)
140963e2b423SJohn Johansen {
14109acd494bSKees Cook 	struct aa_fs_entry *fs_file;
14110d259f04SJohn Johansen 	struct dentry *dir;
14120d259f04SJohn Johansen 	int error;
141363e2b423SJohn Johansen 
14140d259f04SJohn Johansen 	dir = securityfs_create_dir(fs_dir->name, parent);
14150d259f04SJohn Johansen 	if (IS_ERR(dir))
14160d259f04SJohn Johansen 		return PTR_ERR(dir);
14170d259f04SJohn Johansen 	fs_dir->dentry = dir;
141863e2b423SJohn Johansen 
14190d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
14209acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
14219acd494bSKees Cook 			error = aafs_create_dir(fs_file, fs_dir->dentry);
14229acd494bSKees Cook 		else
14239acd494bSKees Cook 			error = aafs_create_file(fs_file, fs_dir->dentry);
14249acd494bSKees Cook 		if (error)
14259acd494bSKees Cook 			goto failed;
14269acd494bSKees Cook 	}
14279acd494bSKees Cook 
14289acd494bSKees Cook 	return 0;
14299acd494bSKees Cook 
14309acd494bSKees Cook failed:
14310d259f04SJohn Johansen 	aafs_remove_dir(fs_dir);
14320d259f04SJohn Johansen 
14339acd494bSKees Cook 	return error;
14349acd494bSKees Cook }
14359acd494bSKees Cook 
14369acd494bSKees Cook /**
14379acd494bSKees Cook  * aafs_remove_file - drop a single file entry in the apparmor securityfs
14389acd494bSKees Cook  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
14399acd494bSKees Cook  */
14409acd494bSKees Cook static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
14419acd494bSKees Cook {
14429acd494bSKees Cook 	if (!fs_file->dentry)
14439acd494bSKees Cook 		return;
14449acd494bSKees Cook 
14459acd494bSKees Cook 	securityfs_remove(fs_file->dentry);
14469acd494bSKees Cook 	fs_file->dentry = NULL;
14479acd494bSKees Cook }
14489acd494bSKees Cook 
14499acd494bSKees Cook /**
14509acd494bSKees Cook  * aafs_remove_dir - recursively drop a directory entry from the securityfs
14519acd494bSKees Cook  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
14529acd494bSKees Cook  */
14539acd494bSKees Cook static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
14549acd494bSKees Cook {
14559acd494bSKees Cook 	struct aa_fs_entry *fs_file;
14569acd494bSKees Cook 
14570d259f04SJohn Johansen 	for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
14589acd494bSKees Cook 		if (fs_file->v_type == AA_FS_TYPE_DIR)
14599acd494bSKees Cook 			aafs_remove_dir(fs_file);
14609acd494bSKees Cook 		else
14619acd494bSKees Cook 			aafs_remove_file(fs_file);
14629acd494bSKees Cook 	}
14639acd494bSKees Cook 
14649acd494bSKees Cook 	aafs_remove_file(fs_dir);
146563e2b423SJohn Johansen }
146663e2b423SJohn Johansen 
146763e2b423SJohn Johansen /**
146863e2b423SJohn Johansen  * aa_destroy_aafs - cleanup and free aafs
146963e2b423SJohn Johansen  *
147063e2b423SJohn Johansen  * releases dentries allocated by aa_create_aafs
147163e2b423SJohn Johansen  */
147263e2b423SJohn Johansen void __init aa_destroy_aafs(void)
147363e2b423SJohn Johansen {
14749acd494bSKees Cook 	aafs_remove_dir(&aa_fs_entry);
147563e2b423SJohn Johansen }
147663e2b423SJohn Johansen 
1477a71ada30SJohn Johansen 
1478a71ada30SJohn Johansen #define NULL_FILE_NAME ".null"
1479a71ada30SJohn Johansen struct path aa_null;
1480a71ada30SJohn Johansen 
1481a71ada30SJohn Johansen static int aa_mk_null_file(struct dentry *parent)
1482a71ada30SJohn Johansen {
1483a71ada30SJohn Johansen 	struct vfsmount *mount = NULL;
1484a71ada30SJohn Johansen 	struct dentry *dentry;
1485a71ada30SJohn Johansen 	struct inode *inode;
1486a71ada30SJohn Johansen 	int count = 0;
1487a71ada30SJohn Johansen 	int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
1488a71ada30SJohn Johansen 
1489a71ada30SJohn Johansen 	if (error)
1490a71ada30SJohn Johansen 		return error;
1491a71ada30SJohn Johansen 
1492a71ada30SJohn Johansen 	inode_lock(d_inode(parent));
1493a71ada30SJohn Johansen 	dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
1494a71ada30SJohn Johansen 	if (IS_ERR(dentry)) {
1495a71ada30SJohn Johansen 		error = PTR_ERR(dentry);
1496a71ada30SJohn Johansen 		goto out;
1497a71ada30SJohn Johansen 	}
1498a71ada30SJohn Johansen 	inode = new_inode(parent->d_inode->i_sb);
1499a71ada30SJohn Johansen 	if (!inode) {
1500a71ada30SJohn Johansen 		error = -ENOMEM;
1501a71ada30SJohn Johansen 		goto out1;
1502a71ada30SJohn Johansen 	}
1503a71ada30SJohn Johansen 
1504a71ada30SJohn Johansen 	inode->i_ino = get_next_ino();
1505a71ada30SJohn Johansen 	inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
150624d0d03cSDeepa Dinamani 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1507a71ada30SJohn Johansen 	init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
1508a71ada30SJohn Johansen 			   MKDEV(MEM_MAJOR, 3));
1509a71ada30SJohn Johansen 	d_instantiate(dentry, inode);
1510a71ada30SJohn Johansen 	aa_null.dentry = dget(dentry);
1511a71ada30SJohn Johansen 	aa_null.mnt = mntget(mount);
1512a71ada30SJohn Johansen 
1513a71ada30SJohn Johansen 	error = 0;
1514a71ada30SJohn Johansen 
1515a71ada30SJohn Johansen out1:
1516a71ada30SJohn Johansen 	dput(dentry);
1517a71ada30SJohn Johansen out:
1518a71ada30SJohn Johansen 	inode_unlock(d_inode(parent));
1519a71ada30SJohn Johansen 	simple_release_fs(&mount, &count);
1520a71ada30SJohn Johansen 	return error;
1521a71ada30SJohn Johansen }
1522a71ada30SJohn Johansen 
152363e2b423SJohn Johansen /**
152463e2b423SJohn Johansen  * aa_create_aafs - create the apparmor security filesystem
152563e2b423SJohn Johansen  *
152663e2b423SJohn Johansen  * dentries created here are released by aa_destroy_aafs
152763e2b423SJohn Johansen  *
152863e2b423SJohn Johansen  * Returns: error on failure
152963e2b423SJohn Johansen  */
15303417d8d5SJames Morris static int __init aa_create_aafs(void)
153163e2b423SJohn Johansen {
1532b7fd2c03SJohn Johansen 	struct dentry *dent;
153363e2b423SJohn Johansen 	int error;
153463e2b423SJohn Johansen 
153563e2b423SJohn Johansen 	if (!apparmor_initialized)
153663e2b423SJohn Johansen 		return 0;
153763e2b423SJohn Johansen 
15389acd494bSKees Cook 	if (aa_fs_entry.dentry) {
153963e2b423SJohn Johansen 		AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
154063e2b423SJohn Johansen 		return -EEXIST;
154163e2b423SJohn Johansen 	}
154263e2b423SJohn Johansen 
15439acd494bSKees Cook 	/* Populate fs tree. */
15449acd494bSKees Cook 	error = aafs_create_dir(&aa_fs_entry, NULL);
154563e2b423SJohn Johansen 	if (error)
154663e2b423SJohn Johansen 		goto error;
154763e2b423SJohn Johansen 
1548b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".load", 0666, aa_fs_entry.dentry,
1549b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_load);
1550b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1551b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1552b7fd2c03SJohn Johansen 		goto error;
1553b7fd2c03SJohn Johansen 	}
1554b7fd2c03SJohn Johansen 	ns_subload(root_ns) = dent;
1555b7fd2c03SJohn Johansen 
1556b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".replace", 0666, aa_fs_entry.dentry,
1557b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_replace);
1558b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1559b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1560b7fd2c03SJohn Johansen 		goto error;
1561b7fd2c03SJohn Johansen 	}
1562b7fd2c03SJohn Johansen 	ns_subreplace(root_ns) = dent;
1563b7fd2c03SJohn Johansen 
1564b7fd2c03SJohn Johansen 	dent = securityfs_create_file(".remove", 0666, aa_fs_entry.dentry,
1565b7fd2c03SJohn Johansen 				      NULL, &aa_fs_profile_remove);
1566b7fd2c03SJohn Johansen 	if (IS_ERR(dent)) {
1567b7fd2c03SJohn Johansen 		error = PTR_ERR(dent);
1568b7fd2c03SJohn Johansen 		goto error;
1569b7fd2c03SJohn Johansen 	}
1570b7fd2c03SJohn Johansen 	ns_subremove(root_ns) = dent;
1571b7fd2c03SJohn Johansen 
1572b7fd2c03SJohn Johansen 	mutex_lock(&root_ns->lock);
157398849dffSJohn Johansen 	error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
1574b7fd2c03SJohn Johansen 	mutex_unlock(&root_ns->lock);
1575b7fd2c03SJohn Johansen 
15760d259f04SJohn Johansen 	if (error)
15770d259f04SJohn Johansen 		goto error;
15780d259f04SJohn Johansen 
1579a71ada30SJohn Johansen 	error = aa_mk_null_file(aa_fs_entry.dentry);
1580a71ada30SJohn Johansen 	if (error)
1581a71ada30SJohn Johansen 		goto error;
1582a71ada30SJohn Johansen 
1583a71ada30SJohn Johansen 	/* TODO: add default profile to apparmorfs */
158463e2b423SJohn Johansen 
158563e2b423SJohn Johansen 	/* Report that AppArmor fs is enabled */
158663e2b423SJohn Johansen 	aa_info_message("AppArmor Filesystem Enabled");
158763e2b423SJohn Johansen 	return 0;
158863e2b423SJohn Johansen 
158963e2b423SJohn Johansen error:
159063e2b423SJohn Johansen 	aa_destroy_aafs();
159163e2b423SJohn Johansen 	AA_ERROR("Error creating AppArmor securityfs\n");
159263e2b423SJohn Johansen 	return error;
159363e2b423SJohn Johansen }
159463e2b423SJohn Johansen 
159563e2b423SJohn Johansen fs_initcall(aa_create_aafs);
1596