apparmor: use macro template to simplify namespace seq_files
[linux-block.git] / security / apparmor / apparmorfs.c
CommitLineData
63e2b423
JJ
1/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor /sys/kernel/security/apparmor interface functions
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
13 */
14
0d259f04 15#include <linux/ctype.h>
63e2b423
JJ
16#include <linux/security.h>
17#include <linux/vmalloc.h>
18#include <linux/module.h>
19#include <linux/seq_file.h>
20#include <linux/uaccess.h>
a71ada30 21#include <linux/mount.h>
63e2b423 22#include <linux/namei.h>
e74abcf3 23#include <linux/capability.h>
29b3822f 24#include <linux/rcupdate.h>
a71ada30
JJ
25#include <uapi/linux/major.h>
26#include <linux/fs.h>
63e2b423
JJ
27
28#include "include/apparmor.h"
29#include "include/apparmorfs.h"
30#include "include/audit.h"
31#include "include/context.h"
f8eb8a13 32#include "include/crypto.h"
63e2b423 33#include "include/policy.h"
cff281f6 34#include "include/policy_ns.h"
d384b0a1 35#include "include/resource.h"
5ac8c355 36#include "include/policy_unpack.h"
63e2b423 37
0d259f04
JJ
38/**
39 * aa_mangle_name - mangle a profile name to std profile layout form
40 * @name: profile name to mangle (NOT NULL)
41 * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
42 *
43 * Returns: length of mangled name
44 */
bbe4a7c8 45static int mangle_name(const char *name, char *target)
0d259f04
JJ
46{
47 char *t = target;
48
49 while (*name == '/' || *name == '.')
50 name++;
51
52 if (target) {
53 for (; *name; name++) {
54 if (*name == '/')
55 *(t)++ = '.';
56 else if (isspace(*name))
57 *(t)++ = '_';
58 else if (isalnum(*name) || strchr("._-", *name))
59 *(t)++ = *name;
60 }
61
62 *t = 0;
63 } else {
64 int len = 0;
65 for (; *name; name++) {
66 if (isalnum(*name) || isspace(*name) ||
67 strchr("/._-", *name))
68 len++;
69 }
70
71 return len;
72 }
73
74 return t - target;
75}
76
63e2b423
JJ
77/**
78 * aa_simple_write_to_buffer - common routine for getting policy from user
63e2b423 79 * @userbuf: user buffer to copy data from (NOT NULL)
3ed02ada 80 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
63e2b423
JJ
81 * @copy_size: size of data to copy from user buffer
82 * @pos: position write is at in the file (NOT NULL)
83 *
84 * Returns: kernel buffer containing copy of user buffer data or an
85 * ERR_PTR on failure.
86 */
5ef50d01 87static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
5ac8c355
JJ
88 size_t alloc_size,
89 size_t copy_size,
90 loff_t *pos)
63e2b423 91{
5ac8c355 92 struct aa_loaddata *data;
63e2b423 93
e6bfa25d 94 AA_BUG(copy_size > alloc_size);
3ed02ada 95
63e2b423
JJ
96 if (*pos != 0)
97 /* only writes from pos 0, that is complete writes */
98 return ERR_PTR(-ESPIPE);
99
63e2b423 100 /* freed by caller to simple_write_to_buffer */
5d5182ca
JJ
101 data = aa_loaddata_alloc(alloc_size);
102 if (IS_ERR(data))
103 return data;
63e2b423 104
5d5182ca 105 data->size = copy_size;
5ac8c355 106 if (copy_from_user(data->data, userbuf, copy_size)) {
63e2b423
JJ
107 kvfree(data);
108 return ERR_PTR(-EFAULT);
109 }
110
111 return data;
112}
113
5ac8c355 114static ssize_t policy_update(int binop, const char __user *buf, size_t size,
b7fd2c03 115 loff_t *pos, struct aa_ns *ns)
63e2b423 116{
63e2b423 117 ssize_t error;
5ac8c355
JJ
118 struct aa_loaddata *data;
119 struct aa_profile *profile = aa_current_profile();
47f6e5cc 120 const char *op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL;
5ac8c355
JJ
121 /* high level check about policy management - fine grained in
122 * below after unpack
123 */
b7fd2c03 124 error = aa_may_manage_policy(profile, ns, op);
5ac8c355
JJ
125 if (error)
126 return error;
63e2b423 127
5ef50d01 128 data = aa_simple_write_to_buffer(buf, size, size, pos);
63e2b423
JJ
129 error = PTR_ERR(data);
130 if (!IS_ERR(data)) {
b7fd2c03
JJ
131 error = aa_replace_profiles(ns ? ns : profile->ns, profile,
132 binop, data);
5ac8c355 133 aa_put_loaddata(data);
63e2b423
JJ
134 }
135
136 return error;
137}
138
b7fd2c03 139/* .load file hook fn to load policy */
5ac8c355
JJ
140static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
141 loff_t *pos)
142{
b7fd2c03
JJ
143 struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
144 int error = policy_update(PROF_ADD, buf, size, pos, ns);
145
146 aa_put_ns(ns);
5ac8c355
JJ
147
148 return error;
149}
150
63e2b423 151static const struct file_operations aa_fs_profile_load = {
6038f373
AB
152 .write = profile_load,
153 .llseek = default_llseek,
63e2b423
JJ
154};
155
156/* .replace file hook fn to load and/or replace policy */
157static ssize_t profile_replace(struct file *f, const char __user *buf,
158 size_t size, loff_t *pos)
159{
b7fd2c03
JJ
160 struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
161 int error = policy_update(PROF_REPLACE, buf, size, pos, ns);
162
163 aa_put_ns(ns);
63e2b423
JJ
164
165 return error;
166}
167
168static const struct file_operations aa_fs_profile_replace = {
6038f373
AB
169 .write = profile_replace,
170 .llseek = default_llseek,
63e2b423
JJ
171};
172
b7fd2c03 173/* .remove file hook fn to remove loaded policy */
63e2b423
JJ
174static ssize_t profile_remove(struct file *f, const char __user *buf,
175 size_t size, loff_t *pos)
176{
5ac8c355
JJ
177 struct aa_loaddata *data;
178 struct aa_profile *profile;
63e2b423 179 ssize_t error;
b7fd2c03 180 struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
63e2b423 181
5ac8c355
JJ
182 profile = aa_current_profile();
183 /* high level check about policy management - fine grained in
184 * below after unpack
185 */
b7fd2c03 186 error = aa_may_manage_policy(profile, ns, OP_PROF_RM);
5ac8c355
JJ
187 if (error)
188 goto out;
189
63e2b423
JJ
190 /*
191 * aa_remove_profile needs a null terminated string so 1 extra
192 * byte is allocated and the copied data is null terminated.
193 */
5ef50d01 194 data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
63e2b423
JJ
195
196 error = PTR_ERR(data);
197 if (!IS_ERR(data)) {
5ac8c355 198 data->data[size] = 0;
b7fd2c03
JJ
199 error = aa_remove_profiles(ns ? ns : profile->ns, profile,
200 data->data, size);
5ac8c355 201 aa_put_loaddata(data);
63e2b423 202 }
5ac8c355 203 out:
b7fd2c03 204 aa_put_ns(ns);
63e2b423
JJ
205 return error;
206}
207
208static const struct file_operations aa_fs_profile_remove = {
6038f373
AB
209 .write = profile_remove,
210 .llseek = default_llseek,
63e2b423
JJ
211};
212
5d5182ca
JJ
213void __aa_bump_ns_revision(struct aa_ns *ns)
214{
215 ns->revision++;
216}
217
e025be0f
WH
218/**
219 * query_data - queries a policy and writes its data to buf
220 * @buf: the resulting data is stored here (NOT NULL)
221 * @buf_len: size of buf
222 * @query: query string used to retrieve data
223 * @query_len: size of query including second NUL byte
224 *
225 * The buffers pointed to by buf and query may overlap. The query buffer is
226 * parsed before buf is written to.
227 *
228 * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
229 * the security confinement context and <KEY> is the name of the data to
230 * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
231 *
232 * Don't expect the contents of buf to be preserved on failure.
233 *
234 * Returns: number of characters written to buf or -errno on failure
235 */
236static ssize_t query_data(char *buf, size_t buf_len,
237 char *query, size_t query_len)
238{
239 char *out;
240 const char *key;
241 struct aa_profile *profile;
242 struct aa_data *data;
243 u32 bytes, blocks;
244 __le32 outle32;
245
246 if (!query_len)
247 return -EINVAL; /* need a query */
248
249 key = query + strnlen(query, query_len) + 1;
250 if (key + 1 >= query + query_len)
251 return -EINVAL; /* not enough space for a non-empty key */
252 if (key + strnlen(key, query + query_len - key) >= query + query_len)
253 return -EINVAL; /* must end with NUL */
254
255 if (buf_len < sizeof(bytes) + sizeof(blocks))
256 return -EINVAL; /* not enough space */
257
258 profile = aa_current_profile();
259
260 /* We are going to leave space for two numbers. The first is the total
261 * number of bytes we are writing after the first number. This is so
262 * users can read the full output without reallocation.
263 *
264 * The second number is the number of data blocks we're writing. An
265 * application might be confined by multiple policies having data in
266 * the same key.
267 */
268 memset(buf, 0, sizeof(bytes) + sizeof(blocks));
269 out = buf + sizeof(bytes) + sizeof(blocks);
270
271 blocks = 0;
272 if (profile->data) {
273 data = rhashtable_lookup_fast(profile->data, &key,
274 profile->data->p);
275
276 if (data) {
277 if (out + sizeof(outle32) + data->size > buf + buf_len)
278 return -EINVAL; /* not enough space */
279 outle32 = __cpu_to_le32(data->size);
280 memcpy(out, &outle32, sizeof(outle32));
281 out += sizeof(outle32);
282 memcpy(out, data->data, data->size);
283 out += data->size;
284 blocks++;
285 }
286 }
287
288 outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
289 memcpy(buf, &outle32, sizeof(outle32));
290 outle32 = __cpu_to_le32(blocks);
291 memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
292
293 return out - buf;
294}
295
296#define QUERY_CMD_DATA "data\0"
297#define QUERY_CMD_DATA_LEN 5
298
299/**
300 * aa_write_access - generic permissions and data query
301 * @file: pointer to open apparmorfs/access file
302 * @ubuf: user buffer containing the complete query string (NOT NULL)
303 * @count: size of ubuf
304 * @ppos: position in the file (MUST BE ZERO)
305 *
306 * Allows for one permissions or data query per open(), write(), and read()
307 * sequence. The only queries currently supported are label-based queries for
308 * permissions or data.
309 *
310 * For permissions queries, ubuf must begin with "label\0", followed by the
311 * profile query specific format described in the query_label() function
312 * documentation.
313 *
314 * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
315 * <LABEL> is the name of the security confinement context and <KEY> is the
316 * name of the data to retrieve.
317 *
318 * Returns: number of bytes written or -errno on failure
319 */
320static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
321 size_t count, loff_t *ppos)
322{
323 char *buf;
324 ssize_t len;
325
326 if (*ppos)
327 return -ESPIPE;
328
329 buf = simple_transaction_get(file, ubuf, count);
330 if (IS_ERR(buf))
331 return PTR_ERR(buf);
332
333 if (count > QUERY_CMD_DATA_LEN &&
334 !memcmp(buf, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
335 len = query_data(buf, SIMPLE_TRANSACTION_LIMIT,
336 buf + QUERY_CMD_DATA_LEN,
337 count - QUERY_CMD_DATA_LEN);
338 } else
339 len = -EINVAL;
340
341 if (len < 0)
342 return len;
343
344 simple_transaction_set(file, len);
345
346 return count;
347}
348
349static const struct file_operations aa_fs_access = {
350 .write = aa_write_access,
351 .read = simple_transaction_read,
352 .release = simple_transaction_release,
353 .llseek = generic_file_llseek,
354};
355
e74abcf3
KC
356static int aa_fs_seq_show(struct seq_file *seq, void *v)
357{
358 struct aa_fs_entry *fs_file = seq->private;
359
360 if (!fs_file)
361 return 0;
362
363 switch (fs_file->v_type) {
364 case AA_FS_TYPE_BOOLEAN:
365 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
366 break;
a9bf8e9f
KC
367 case AA_FS_TYPE_STRING:
368 seq_printf(seq, "%s\n", fs_file->v.string);
369 break;
e74abcf3
KC
370 case AA_FS_TYPE_U64:
371 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
372 break;
373 default:
374 /* Ignore unpritable entry types. */
375 break;
376 }
377
378 return 0;
379}
380
381static int aa_fs_seq_open(struct inode *inode, struct file *file)
382{
383 return single_open(file, aa_fs_seq_show, inode->i_private);
384}
385
386const struct file_operations aa_fs_seq_file_ops = {
387 .owner = THIS_MODULE,
388 .open = aa_fs_seq_open,
389 .read = seq_read,
390 .llseek = seq_lseek,
391 .release = single_release,
392};
393
52b97de3
JJ
394/*
395 * profile based file operations
396 * policy/profiles/XXXX/profiles/ *
397 */
398
399#define SEQ_PROFILE_FOPS(NAME) \
400static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
401{ \
402 return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \
403} \
404 \
405static const struct file_operations seq_profile_ ##NAME ##_fops = { \
406 .owner = THIS_MODULE, \
407 .open = seq_profile_ ##NAME ##_open, \
408 .read = seq_read, \
409 .llseek = seq_lseek, \
410 .release = seq_profile_release, \
411} \
412
413static int seq_profile_open(struct inode *inode, struct file *file,
414 int (*show)(struct seq_file *, void *))
0d259f04 415{
8399588a
JJ
416 struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
417 int error = single_open(file, show, proxy);
0d259f04
JJ
418
419 if (error) {
420 file->private_data = NULL;
8399588a 421 aa_put_proxy(proxy);
0d259f04
JJ
422 }
423
424 return error;
425}
426
52b97de3 427static int seq_profile_release(struct inode *inode, struct file *file)
0d259f04
JJ
428{
429 struct seq_file *seq = (struct seq_file *) file->private_data;
430 if (seq)
8399588a 431 aa_put_proxy(seq->private);
0d259f04
JJ
432 return single_release(inode, file);
433}
434
52b97de3 435static int seq_profile_name_show(struct seq_file *seq, void *v)
0d259f04 436{
8399588a
JJ
437 struct aa_proxy *proxy = seq->private;
438 struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
0d259f04
JJ
439 seq_printf(seq, "%s\n", profile->base.name);
440 aa_put_profile(profile);
441
442 return 0;
443}
444
52b97de3 445static int seq_profile_mode_show(struct seq_file *seq, void *v)
0d259f04 446{
8399588a
JJ
447 struct aa_proxy *proxy = seq->private;
448 struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
0d259f04
JJ
449 seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
450 aa_put_profile(profile);
451
452 return 0;
453}
454
52b97de3 455static int seq_profile_attach_show(struct seq_file *seq, void *v)
556d0be7 456{
8399588a
JJ
457 struct aa_proxy *proxy = seq->private;
458 struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
556d0be7
JJ
459 if (profile->attach)
460 seq_printf(seq, "%s\n", profile->attach);
461 else if (profile->xmatch)
462 seq_puts(seq, "<unknown>\n");
463 else
464 seq_printf(seq, "%s\n", profile->base.name);
465 aa_put_profile(profile);
466
467 return 0;
468}
469
52b97de3 470static int seq_profile_hash_show(struct seq_file *seq, void *v)
f8eb8a13 471{
8399588a
JJ
472 struct aa_proxy *proxy = seq->private;
473 struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
f8eb8a13
JJ
474 unsigned int i, size = aa_hash_size();
475
476 if (profile->hash) {
477 for (i = 0; i < size; i++)
478 seq_printf(seq, "%.2x", profile->hash[i]);
47dbd1cd 479 seq_putc(seq, '\n');
f8eb8a13 480 }
0b938a2e 481 aa_put_profile(profile);
f8eb8a13
JJ
482
483 return 0;
484}
485
52b97de3
JJ
486SEQ_PROFILE_FOPS(name);
487SEQ_PROFILE_FOPS(mode);
488SEQ_PROFILE_FOPS(attach);
489SEQ_PROFILE_FOPS(hash);
f8eb8a13 490
64c86970
JJ
491/*
492 * namespace based files
493 * several root files and
494 * policy/ *
495 */
f8eb8a13 496
64c86970
JJ
497#define SEQ_NS_FOPS(NAME) \
498static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \
499{ \
500 return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \
501} \
502 \
503static const struct file_operations seq_ns_ ##NAME ##_fops = { \
504 .owner = THIS_MODULE, \
505 .open = seq_ns_ ##NAME ##_open, \
506 .read = seq_read, \
507 .llseek = seq_lseek, \
508 .release = single_release, \
509} \
3e3e5695 510
64c86970 511static int seq_ns_level_show(struct seq_file *seq, void *v)
a71ada30
JJ
512{
513 struct aa_ns *ns = aa_current_profile()->ns;
514
515 seq_printf(seq, "%d\n", ns->level);
516
517 return 0;
518}
519
64c86970 520static int seq_ns_name_show(struct seq_file *seq, void *v)
3e3e5695
JJ
521{
522 struct aa_ns *ns = aa_current_profile()->ns;
523
524 seq_printf(seq, "%s\n", ns->base.name);
525
526 return 0;
527}
528
64c86970
JJ
529SEQ_NS_FOPS(level);
530SEQ_NS_FOPS(name);
3e3e5695 531
5d5182ca
JJ
532
533/* policy/raw_data/ * file ops */
534
535#define SEQ_RAWDATA_FOPS(NAME) \
536static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
537{ \
538 return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \
539} \
540 \
541static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \
542 .owner = THIS_MODULE, \
543 .open = seq_rawdata_ ##NAME ##_open, \
544 .read = seq_read, \
545 .llseek = seq_lseek, \
546 .release = seq_rawdata_release, \
547} \
548
549static int seq_rawdata_open(struct inode *inode, struct file *file,
550 int (*show)(struct seq_file *, void *))
5ac8c355 551{
5d5182ca
JJ
552 struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
553 int error;
5ac8c355 554
5d5182ca
JJ
555 if (!data)
556 /* lost race this ent is being reaped */
557 return -ENOENT;
558
559 error = single_open(file, show, data);
560 if (error) {
561 AA_BUG(file->private_data &&
562 ((struct seq_file *)file->private_data)->private);
563 aa_put_loaddata(data);
564 }
565
566 return error;
5ac8c355
JJ
567}
568
5d5182ca 569static int seq_rawdata_release(struct inode *inode, struct file *file)
5ac8c355 570{
5d5182ca 571 struct seq_file *seq = (struct seq_file *) file->private_data;
5ac8c355 572
5d5182ca
JJ
573 if (seq)
574 aa_put_loaddata(seq->private);
0ff3d97f 575
5d5182ca
JJ
576 return single_release(inode, file);
577}
578
579static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
580{
581 struct aa_loaddata *data = seq->private;
582
583 seq_printf(seq, "v%d\n", data->abi);
5ac8c355
JJ
584
585 return 0;
586}
587
5d5182ca 588static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
5ac8c355 589{
5d5182ca 590 struct aa_loaddata *data = seq->private;
5ac8c355 591
5d5182ca
JJ
592 seq_printf(seq, "%ld\n", data->revision);
593
594 return 0;
595}
5ac8c355 596
5d5182ca 597static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
5ac8c355 598{
5d5182ca 599 struct aa_loaddata *data = seq->private;
5ac8c355
JJ
600 unsigned int i, size = aa_hash_size();
601
5d5182ca 602 if (data->hash) {
5ac8c355 603 for (i = 0; i < size; i++)
5d5182ca 604 seq_printf(seq, "%.2x", data->hash[i]);
47dbd1cd 605 seq_putc(seq, '\n');
5ac8c355 606 }
5ac8c355
JJ
607
608 return 0;
609}
610
5d5182ca
JJ
611SEQ_RAWDATA_FOPS(abi);
612SEQ_RAWDATA_FOPS(revision);
613SEQ_RAWDATA_FOPS(hash);
5ac8c355
JJ
614
615static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
616 loff_t *ppos)
617{
618 struct aa_loaddata *rawdata = file->private_data;
619
620 return simple_read_from_buffer(buf, size, ppos, rawdata->data,
621 rawdata->size);
622}
623
5d5182ca 624static int rawdata_release(struct inode *inode, struct file *file)
5ac8c355 625{
5d5182ca
JJ
626 aa_put_loaddata(file->private_data);
627
628 return 0;
629}
5ac8c355 630
5d5182ca
JJ
631static int rawdata_open(struct inode *inode, struct file *file)
632{
5ac8c355
JJ
633 if (!policy_view_capable(NULL))
634 return -EACCES;
5d5182ca
JJ
635 file->private_data = __aa_get_loaddata(inode->i_private);
636 if (!file->private_data)
637 /* lost race: this entry is being reaped */
638 return -ENOENT;
5ac8c355
JJ
639
640 return 0;
641}
642
5d5182ca 643static const struct file_operations rawdata_fops = {
5ac8c355
JJ
644 .open = rawdata_open,
645 .read = rawdata_read,
646 .llseek = generic_file_llseek,
647 .release = rawdata_release,
648};
649
5d5182ca
JJ
650static void remove_rawdata_dents(struct aa_loaddata *rawdata)
651{
652 int i;
653
654 for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
655 if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
656 /* no refcounts on i_private */
657 securityfs_remove(rawdata->dents[i]);
658 rawdata->dents[i] = NULL;
659 }
660 }
661}
662
663void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
664{
665 AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
666
667 if (rawdata->ns) {
668 remove_rawdata_dents(rawdata);
669 list_del_init(&rawdata->list);
670 aa_put_ns(rawdata->ns);
671 rawdata->ns = NULL;
672 }
673}
674
675int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
676{
677 struct dentry *dent, *dir;
678
679 AA_BUG(!ns);
680 AA_BUG(!rawdata);
681 AA_BUG(!mutex_is_locked(&ns->lock));
682 AA_BUG(!ns_subdata_dir(ns));
683
684 /*
685 * just use ns revision dir was originally created at. This is
686 * under ns->lock and if load is successful revision will be
687 * bumped and is guaranteed to be unique
688 */
689 rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
690 if (!rawdata->name)
691 return -ENOMEM;
692
693 dir = securityfs_create_dir(rawdata->name, ns_subdata_dir(ns));
694 if (IS_ERR(dir))
695 /* ->name freed when rawdata freed */
696 return PTR_ERR(dir);
697 rawdata->dents[AAFS_LOADDATA_DIR] = dir;
698
699 dent = securityfs_create_file("abi", S_IFREG | 0444, dir, rawdata,
700 &seq_rawdata_abi_fops);
701 if (IS_ERR(dent))
702 goto fail;
703 rawdata->dents[AAFS_LOADDATA_ABI] = dent;
704
705 dent = securityfs_create_file("revision", S_IFREG | 0444, dir, rawdata,
706 &seq_rawdata_revision_fops);
707 if (IS_ERR(dent))
708 goto fail;
709 rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
710
711 if (aa_g_hash_policy) {
712 dent = securityfs_create_file("sha1", S_IFREG | 0444, dir,
713 rawdata, &seq_rawdata_hash_fops);
714 if (IS_ERR(dent))
715 goto fail;
716 rawdata->dents[AAFS_LOADDATA_HASH] = dent;
717 }
718
719 dent = securityfs_create_file("raw_data", S_IFREG | 0444,
720 dir, rawdata, &rawdata_fops);
721 if (IS_ERR(dent))
722 goto fail;
723 rawdata->dents[AAFS_LOADDATA_DATA] = dent;
724 d_inode(dent)->i_size = rawdata->size;
725
726 rawdata->ns = aa_get_ns(ns);
727 list_add(&rawdata->list, &ns->rawdata_list);
728 /* no refcount on inode rawdata */
729
730 return 0;
731
732fail:
733 remove_rawdata_dents(rawdata);
734
735 return PTR_ERR(dent);
736}
737
0d259f04
JJ
738/** fns to setup dynamic per profile/namespace files **/
739void __aa_fs_profile_rmdir(struct aa_profile *profile)
740{
741 struct aa_profile *child;
742 int i;
743
744 if (!profile)
745 return;
746
747 list_for_each_entry(child, &profile->base.profiles, base.list)
748 __aa_fs_profile_rmdir(child);
749
750 for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
8399588a 751 struct aa_proxy *proxy;
0d259f04
JJ
752 if (!profile->dents[i])
753 continue;
754
8399588a 755 proxy = d_inode(profile->dents[i])->i_private;
0d259f04 756 securityfs_remove(profile->dents[i]);
8399588a 757 aa_put_proxy(proxy);
0d259f04
JJ
758 profile->dents[i] = NULL;
759 }
760}
761
762void __aa_fs_profile_migrate_dents(struct aa_profile *old,
763 struct aa_profile *new)
764{
765 int i;
766
767 for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
768 new->dents[i] = old->dents[i];
d671e890 769 if (new->dents[i])
078cd827 770 new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
0d259f04
JJ
771 old->dents[i] = NULL;
772 }
773}
774
775static struct dentry *create_profile_file(struct dentry *dir, const char *name,
776 struct aa_profile *profile,
777 const struct file_operations *fops)
778{
8399588a 779 struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
0d259f04
JJ
780 struct dentry *dent;
781
8399588a 782 dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
0d259f04 783 if (IS_ERR(dent))
8399588a 784 aa_put_proxy(proxy);
0d259f04
JJ
785
786 return dent;
787}
788
5d5182ca
JJ
789static int profile_depth(struct aa_profile *profile)
790{
791 int depth = 0;
792
793 rcu_read_lock();
794 for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
795 depth++;
796 rcu_read_unlock();
797
798 return depth;
799}
800
801static int gen_symlink_name(char *buffer, size_t bsize, int depth,
802 const char *dirname, const char *fname)
803{
804 int error;
805
806 for (; depth > 0; depth--) {
807 if (bsize < 7)
808 return -ENAMETOOLONG;
809 strcpy(buffer, "../../");
810 buffer += 6;
811 bsize -= 6;
812 }
813
814 error = snprintf(buffer, bsize, "raw_data/%s/%s", dirname, fname);
815 if (error >= bsize || error < 0)
816 return -ENAMETOOLONG;
817
818 return 0;
819}
820
821/*
822 * Requires: @profile->ns->lock held
823 */
0d259f04
JJ
824int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
825{
826 struct aa_profile *child;
827 struct dentry *dent = NULL, *dir;
828 int error;
829
830 if (!parent) {
831 struct aa_profile *p;
832 p = aa_deref_parent(profile);
833 dent = prof_dir(p);
834 /* adding to parent that previously didn't have children */
835 dent = securityfs_create_dir("profiles", dent);
836 if (IS_ERR(dent))
837 goto fail;
838 prof_child_dir(p) = parent = dent;
839 }
840
841 if (!profile->dirname) {
842 int len, id_len;
843 len = mangle_name(profile->base.name, NULL);
844 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
845
846 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
ffac1de6
DC
847 if (!profile->dirname) {
848 error = -ENOMEM;
849 goto fail2;
850 }
0d259f04
JJ
851
852 mangle_name(profile->base.name, profile->dirname);
853 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
854 }
855
856 dent = securityfs_create_dir(profile->dirname, parent);
857 if (IS_ERR(dent))
858 goto fail;
859 prof_dir(profile) = dir = dent;
860
52b97de3
JJ
861 dent = create_profile_file(dir, "name", profile,
862 &seq_profile_name_fops);
0d259f04
JJ
863 if (IS_ERR(dent))
864 goto fail;
865 profile->dents[AAFS_PROF_NAME] = dent;
866
52b97de3
JJ
867 dent = create_profile_file(dir, "mode", profile,
868 &seq_profile_mode_fops);
0d259f04
JJ
869 if (IS_ERR(dent))
870 goto fail;
871 profile->dents[AAFS_PROF_MODE] = dent;
872
556d0be7 873 dent = create_profile_file(dir, "attach", profile,
52b97de3 874 &seq_profile_attach_fops);
556d0be7
JJ
875 if (IS_ERR(dent))
876 goto fail;
877 profile->dents[AAFS_PROF_ATTACH] = dent;
878
f8eb8a13
JJ
879 if (profile->hash) {
880 dent = create_profile_file(dir, "sha1", profile,
52b97de3 881 &seq_profile_hash_fops);
f8eb8a13
JJ
882 if (IS_ERR(dent))
883 goto fail;
884 profile->dents[AAFS_PROF_HASH] = dent;
885 }
886
5ac8c355 887 if (profile->rawdata) {
5d5182ca
JJ
888 char target[64];
889 int depth = profile_depth(profile);
890
891 error = gen_symlink_name(target, sizeof(target), depth,
892 profile->rawdata->name, "sha1");
893 if (error < 0)
894 goto fail2;
895 dent = securityfs_create_symlink("raw_sha1", dir, target, NULL);
5ac8c355
JJ
896 if (IS_ERR(dent))
897 goto fail;
898 profile->dents[AAFS_PROF_RAW_HASH] = dent;
899
5d5182ca
JJ
900 error = gen_symlink_name(target, sizeof(target), depth,
901 profile->rawdata->name, "abi");
902 if (error < 0)
903 goto fail2;
904 dent = securityfs_create_symlink("raw_abi", dir, target, NULL);
5ac8c355
JJ
905 if (IS_ERR(dent))
906 goto fail;
907 profile->dents[AAFS_PROF_RAW_ABI] = dent;
908
5d5182ca
JJ
909 error = gen_symlink_name(target, sizeof(target), depth,
910 profile->rawdata->name, "raw_data");
911 if (error < 0)
912 goto fail2;
913 dent = securityfs_create_symlink("raw_data", dir, target, NULL);
5ac8c355
JJ
914 if (IS_ERR(dent))
915 goto fail;
916 profile->dents[AAFS_PROF_RAW_DATA] = dent;
5ac8c355
JJ
917 }
918
0d259f04
JJ
919 list_for_each_entry(child, &profile->base.profiles, base.list) {
920 error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
921 if (error)
922 goto fail2;
923 }
924
925 return 0;
926
927fail:
928 error = PTR_ERR(dent);
929
930fail2:
931 __aa_fs_profile_rmdir(profile);
932
933 return error;
934}
935
5d5182ca
JJ
936static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
937{
938 struct aa_loaddata *ent, *tmp;
939
940 AA_BUG(!mutex_is_locked(&ns->lock));
941
942 list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
943 __aa_fs_remove_rawdata(ent);
944}
945
98849dff 946void __aa_fs_ns_rmdir(struct aa_ns *ns)
0d259f04 947{
98849dff 948 struct aa_ns *sub;
0d259f04
JJ
949 struct aa_profile *child;
950 int i;
951
952 if (!ns)
953 return;
954
955 list_for_each_entry(child, &ns->base.profiles, base.list)
956 __aa_fs_profile_rmdir(child);
957
958 list_for_each_entry(sub, &ns->sub_ns, base.list) {
959 mutex_lock(&sub->lock);
98849dff 960 __aa_fs_ns_rmdir(sub);
0d259f04
JJ
961 mutex_unlock(&sub->lock);
962 }
963
5d5182ca
JJ
964 __aa_fs_list_remove_rawdata(ns);
965
b7fd2c03
JJ
966 if (ns_subns_dir(ns)) {
967 sub = d_inode(ns_subns_dir(ns))->i_private;
968 aa_put_ns(sub);
969 }
970 if (ns_subload(ns)) {
971 sub = d_inode(ns_subload(ns))->i_private;
972 aa_put_ns(sub);
973 }
974 if (ns_subreplace(ns)) {
975 sub = d_inode(ns_subreplace(ns))->i_private;
976 aa_put_ns(sub);
977 }
978 if (ns_subremove(ns)) {
979 sub = d_inode(ns_subremove(ns))->i_private;
980 aa_put_ns(sub);
981 }
982
0d259f04
JJ
983 for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
984 securityfs_remove(ns->dents[i]);
985 ns->dents[i] = NULL;
986 }
987}
988
b7fd2c03
JJ
989/* assumes cleanup in caller */
990static int __aa_fs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
991{
992 struct dentry *dent;
993
994 AA_BUG(!ns);
995 AA_BUG(!dir);
996
997 dent = securityfs_create_dir("profiles", dir);
998 if (IS_ERR(dent))
999 return PTR_ERR(dent);
1000 ns_subprofs_dir(ns) = dent;
1001
1002 dent = securityfs_create_dir("raw_data", dir);
1003 if (IS_ERR(dent))
1004 return PTR_ERR(dent);
1005 ns_subdata_dir(ns) = dent;
1006
1007 dent = securityfs_create_file(".load", 0640, dir, ns,
1008 &aa_fs_profile_load);
1009 if (IS_ERR(dent))
1010 return PTR_ERR(dent);
1011 aa_get_ns(ns);
1012 ns_subload(ns) = dent;
1013
1014 dent = securityfs_create_file(".replace", 0640, dir, ns,
1015 &aa_fs_profile_replace);
1016 if (IS_ERR(dent))
1017 return PTR_ERR(dent);
1018 aa_get_ns(ns);
1019 ns_subreplace(ns) = dent;
1020
1021 dent = securityfs_create_file(".remove", 0640, dir, ns,
1022 &aa_fs_profile_remove);
1023 if (IS_ERR(dent))
1024 return PTR_ERR(dent);
1025 aa_get_ns(ns);
1026 ns_subremove(ns) = dent;
1027
1028 dent = securityfs_create_dir("namespaces", dir);
1029 if (IS_ERR(dent))
1030 return PTR_ERR(dent);
1031 aa_get_ns(ns);
1032 ns_subns_dir(ns) = dent;
1033
1034 return 0;
1035}
1036
98849dff 1037int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
0d259f04 1038{
98849dff 1039 struct aa_ns *sub;
0d259f04
JJ
1040 struct aa_profile *child;
1041 struct dentry *dent, *dir;
1042 int error;
1043
b7fd2c03
JJ
1044 AA_BUG(!ns);
1045 AA_BUG(!parent);
1046 AA_BUG(!mutex_is_locked(&ns->lock));
1047
0d259f04
JJ
1048 if (!name)
1049 name = ns->base.name;
1050
b7fd2c03 1051 /* create ns dir if it doesn't already exist */
0d259f04
JJ
1052 dent = securityfs_create_dir(name, parent);
1053 if (IS_ERR(dent))
1054 goto fail;
63e2b423 1055
b7fd2c03
JJ
1056 ns_dir(ns) = dir = dent;
1057 error = __aa_fs_ns_mkdir_entries(ns, dir);
1058 if (error)
1059 goto fail2;
0d259f04 1060
b7fd2c03 1061 /* profiles */
0d259f04
JJ
1062 list_for_each_entry(child, &ns->base.profiles, base.list) {
1063 error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
1064 if (error)
1065 goto fail2;
1066 }
1067
b7fd2c03 1068 /* subnamespaces */
0d259f04
JJ
1069 list_for_each_entry(sub, &ns->sub_ns, base.list) {
1070 mutex_lock(&sub->lock);
98849dff 1071 error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
0d259f04
JJ
1072 mutex_unlock(&sub->lock);
1073 if (error)
1074 goto fail2;
1075 }
1076
1077 return 0;
1078
1079fail:
1080 error = PTR_ERR(dent);
1081
1082fail2:
98849dff 1083 __aa_fs_ns_rmdir(ns);
0d259f04
JJ
1084
1085 return error;
1086}
1087
1088
29b3822f
JJ
1089#define list_entry_is_head(pos, head, member) (&pos->member == (head))
1090
1091/**
98849dff 1092 * __next_ns - find the next namespace to list
29b3822f
JJ
1093 * @root: root namespace to stop search at (NOT NULL)
1094 * @ns: current ns position (NOT NULL)
1095 *
1096 * Find the next namespace from @ns under @root and handle all locking needed
1097 * while switching current namespace.
1098 *
1099 * Returns: next namespace or NULL if at last namespace under @root
1100 * Requires: ns->parent->lock to be held
1101 * NOTE: will not unlock root->lock
1102 */
98849dff 1103static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
29b3822f 1104{
98849dff 1105 struct aa_ns *parent, *next;
29b3822f
JJ
1106
1107 /* is next namespace a child */
1108 if (!list_empty(&ns->sub_ns)) {
1109 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
1110 mutex_lock(&next->lock);
1111 return next;
1112 }
1113
1114 /* check if the next ns is a sibling, parent, gp, .. */
1115 parent = ns->parent;
ed2c7da3 1116 while (ns != root) {
29b3822f 1117 mutex_unlock(&ns->lock);
38dbd7d8 1118 next = list_next_entry(ns, base.list);
29b3822f
JJ
1119 if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
1120 mutex_lock(&next->lock);
1121 return next;
1122 }
29b3822f
JJ
1123 ns = parent;
1124 parent = parent->parent;
1125 }
1126
1127 return NULL;
1128}
1129
1130/**
1131 * __first_profile - find the first profile in a namespace
1132 * @root: namespace that is root of profiles being displayed (NOT NULL)
1133 * @ns: namespace to start in (NOT NULL)
1134 *
1135 * Returns: unrefcounted profile or NULL if no profile
1136 * Requires: profile->ns.lock to be held
1137 */
98849dff
JJ
1138static struct aa_profile *__first_profile(struct aa_ns *root,
1139 struct aa_ns *ns)
29b3822f 1140{
98849dff 1141 for (; ns; ns = __next_ns(root, ns)) {
29b3822f
JJ
1142 if (!list_empty(&ns->base.profiles))
1143 return list_first_entry(&ns->base.profiles,
1144 struct aa_profile, base.list);
1145 }
1146 return NULL;
1147}
1148
1149/**
1150 * __next_profile - step to the next profile in a profile tree
1151 * @profile: current profile in tree (NOT NULL)
1152 *
1153 * Perform a depth first traversal on the profile tree in a namespace
1154 *
1155 * Returns: next profile or NULL if done
1156 * Requires: profile->ns.lock to be held
1157 */
1158static struct aa_profile *__next_profile(struct aa_profile *p)
1159{
1160 struct aa_profile *parent;
98849dff 1161 struct aa_ns *ns = p->ns;
29b3822f
JJ
1162
1163 /* is next profile a child */
1164 if (!list_empty(&p->base.profiles))
1165 return list_first_entry(&p->base.profiles, typeof(*p),
1166 base.list);
1167
1168 /* is next profile a sibling, parent sibling, gp, sibling, .. */
1169 parent = rcu_dereference_protected(p->parent,
1170 mutex_is_locked(&p->ns->lock));
1171 while (parent) {
38dbd7d8 1172 p = list_next_entry(p, base.list);
29b3822f
JJ
1173 if (!list_entry_is_head(p, &parent->base.profiles, base.list))
1174 return p;
1175 p = parent;
1176 parent = rcu_dereference_protected(parent->parent,
1177 mutex_is_locked(&parent->ns->lock));
1178 }
1179
1180 /* is next another profile in the namespace */
38dbd7d8 1181 p = list_next_entry(p, base.list);
29b3822f
JJ
1182 if (!list_entry_is_head(p, &ns->base.profiles, base.list))
1183 return p;
1184
1185 return NULL;
1186}
1187
1188/**
1189 * next_profile - step to the next profile in where ever it may be
1190 * @root: root namespace (NOT NULL)
1191 * @profile: current profile (NOT NULL)
1192 *
1193 * Returns: next profile or NULL if there isn't one
1194 */
98849dff 1195static struct aa_profile *next_profile(struct aa_ns *root,
29b3822f
JJ
1196 struct aa_profile *profile)
1197{
1198 struct aa_profile *next = __next_profile(profile);
1199 if (next)
1200 return next;
1201
1202 /* finished all profiles in namespace move to next namespace */
98849dff 1203 return __first_profile(root, __next_ns(root, profile->ns));
29b3822f
JJ
1204}
1205
1206/**
1207 * p_start - start a depth first traversal of profile tree
1208 * @f: seq_file to fill
1209 * @pos: current position
1210 *
1211 * Returns: first profile under current namespace or NULL if none found
1212 *
1213 * acquires first ns->lock
1214 */
1215static void *p_start(struct seq_file *f, loff_t *pos)
1216{
1217 struct aa_profile *profile = NULL;
98849dff 1218 struct aa_ns *root = aa_current_profile()->ns;
29b3822f 1219 loff_t l = *pos;
98849dff 1220 f->private = aa_get_ns(root);
29b3822f
JJ
1221
1222
1223 /* find the first profile */
1224 mutex_lock(&root->lock);
1225 profile = __first_profile(root, root);
1226
1227 /* skip to position */
1228 for (; profile && l > 0; l--)
1229 profile = next_profile(root, profile);
1230
1231 return profile;
1232}
1233
1234/**
1235 * p_next - read the next profile entry
1236 * @f: seq_file to fill
1237 * @p: profile previously returned
1238 * @pos: current position
1239 *
1240 * Returns: next profile after @p or NULL if none
1241 *
1242 * may acquire/release locks in namespace tree as necessary
1243 */
1244static void *p_next(struct seq_file *f, void *p, loff_t *pos)
1245{
1246 struct aa_profile *profile = p;
98849dff 1247 struct aa_ns *ns = f->private;
29b3822f
JJ
1248 (*pos)++;
1249
1250 return next_profile(ns, profile);
1251}
1252
1253/**
1254 * p_stop - stop depth first traversal
1255 * @f: seq_file we are filling
1256 * @p: the last profile writen
1257 *
1258 * Release all locking done by p_start/p_next on namespace tree
1259 */
1260static void p_stop(struct seq_file *f, void *p)
1261{
1262 struct aa_profile *profile = p;
98849dff 1263 struct aa_ns *root = f->private, *ns;
29b3822f
JJ
1264
1265 if (profile) {
1266 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
1267 mutex_unlock(&ns->lock);
1268 }
1269 mutex_unlock(&root->lock);
98849dff 1270 aa_put_ns(root);
29b3822f
JJ
1271}
1272
1273/**
1274 * seq_show_profile - show a profile entry
1275 * @f: seq_file to file
1276 * @p: current position (profile) (NOT NULL)
1277 *
1278 * Returns: error on failure
1279 */
1280static int seq_show_profile(struct seq_file *f, void *p)
1281{
1282 struct aa_profile *profile = (struct aa_profile *)p;
98849dff 1283 struct aa_ns *root = f->private;
29b3822f
JJ
1284
1285 if (profile->ns != root)
92b6d8ef 1286 seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
29b3822f
JJ
1287 seq_printf(f, "%s (%s)\n", profile->base.hname,
1288 aa_profile_mode_names[profile->mode]);
1289
1290 return 0;
1291}
1292
1293static const struct seq_operations aa_fs_profiles_op = {
1294 .start = p_start,
1295 .next = p_next,
1296 .stop = p_stop,
1297 .show = seq_show_profile,
1298};
1299
1300static int profiles_open(struct inode *inode, struct file *file)
1301{
5ac8c355
JJ
1302 if (!policy_view_capable(NULL))
1303 return -EACCES;
1304
29b3822f
JJ
1305 return seq_open(file, &aa_fs_profiles_op);
1306}
1307
1308static int profiles_release(struct inode *inode, struct file *file)
1309{
1310 return seq_release(inode, file);
1311}
1312
1313static const struct file_operations aa_fs_profiles_fops = {
1314 .open = profiles_open,
1315 .read = seq_read,
1316 .llseek = seq_lseek,
1317 .release = profiles_release,
1318};
1319
1320
0d259f04 1321/** Base file system setup **/
a9bf8e9f
KC
1322static struct aa_fs_entry aa_fs_entry_file[] = {
1323 AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
1324 "link lock"),
1325 { }
1326};
1327
e74abcf3
KC
1328static struct aa_fs_entry aa_fs_entry_domain[] = {
1329 AA_FS_FILE_BOOLEAN("change_hat", 1),
1330 AA_FS_FILE_BOOLEAN("change_hatv", 1),
1331 AA_FS_FILE_BOOLEAN("change_onexec", 1),
1332 AA_FS_FILE_BOOLEAN("change_profile", 1),
34c426ac 1333 AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1),
aa9a39ad 1334 AA_FS_FILE_STRING("version", "1.2"),
e74abcf3
KC
1335 { }
1336};
1337
474d6b75
JJ
1338static struct aa_fs_entry aa_fs_entry_versions[] = {
1339 AA_FS_FILE_BOOLEAN("v5", 1),
1340 { }
1341};
1342
9d910a3b 1343static struct aa_fs_entry aa_fs_entry_policy[] = {
474d6b75
JJ
1344 AA_FS_DIR("versions", aa_fs_entry_versions),
1345 AA_FS_FILE_BOOLEAN("set_load", 1),
1346 { }
9d910a3b
JJ
1347};
1348
e74abcf3 1349static struct aa_fs_entry aa_fs_entry_features[] = {
9d910a3b 1350 AA_FS_DIR("policy", aa_fs_entry_policy),
e74abcf3 1351 AA_FS_DIR("domain", aa_fs_entry_domain),
a9bf8e9f 1352 AA_FS_DIR("file", aa_fs_entry_file),
e74abcf3 1353 AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
d384b0a1 1354 AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
84f1f787 1355 AA_FS_DIR("caps", aa_fs_entry_caps),
e74abcf3
KC
1356 { }
1357};
1358
9acd494b 1359static struct aa_fs_entry aa_fs_entry_apparmor[] = {
e025be0f 1360 AA_FS_FILE_FOPS(".access", 0640, &aa_fs_access),
64c86970
JJ
1361 AA_FS_FILE_FOPS(".ns_level", 0666, &seq_ns_level_fops),
1362 AA_FS_FILE_FOPS(".ns_name", 0640, &seq_ns_name_fops),
b7fd2c03 1363 AA_FS_FILE_FOPS("profiles", 0440, &aa_fs_profiles_fops),
e74abcf3 1364 AA_FS_DIR("features", aa_fs_entry_features),
9acd494b
KC
1365 { }
1366};
63e2b423 1367
9acd494b
KC
1368static struct aa_fs_entry aa_fs_entry =
1369 AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
63e2b423 1370
9acd494b
KC
1371/**
1372 * aafs_create_file - create a file entry in the apparmor securityfs
1373 * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
1374 * @parent: the parent dentry in the securityfs
1375 *
1376 * Use aafs_remove_file to remove entries created with this fn.
1377 */
1378static int __init aafs_create_file(struct aa_fs_entry *fs_file,
1379 struct dentry *parent)
1380{
1381 int error = 0;
1382
1383 fs_file->dentry = securityfs_create_file(fs_file->name,
1384 S_IFREG | fs_file->mode,
1385 parent, fs_file,
1386 fs_file->file_ops);
1387 if (IS_ERR(fs_file->dentry)) {
1388 error = PTR_ERR(fs_file->dentry);
1389 fs_file->dentry = NULL;
63e2b423 1390 }
9acd494b 1391 return error;
63e2b423
JJ
1392}
1393
0d259f04 1394static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
63e2b423 1395/**
9acd494b
KC
1396 * aafs_create_dir - recursively create a directory entry in the securityfs
1397 * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
1398 * @parent: the parent dentry in the securityfs
63e2b423 1399 *
9acd494b 1400 * Use aafs_remove_dir to remove entries created with this fn.
63e2b423 1401 */
9acd494b
KC
1402static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
1403 struct dentry *parent)
63e2b423 1404{
9acd494b 1405 struct aa_fs_entry *fs_file;
0d259f04
JJ
1406 struct dentry *dir;
1407 int error;
63e2b423 1408
0d259f04
JJ
1409 dir = securityfs_create_dir(fs_dir->name, parent);
1410 if (IS_ERR(dir))
1411 return PTR_ERR(dir);
1412 fs_dir->dentry = dir;
63e2b423 1413
0d259f04 1414 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
9acd494b
KC
1415 if (fs_file->v_type == AA_FS_TYPE_DIR)
1416 error = aafs_create_dir(fs_file, fs_dir->dentry);
1417 else
1418 error = aafs_create_file(fs_file, fs_dir->dentry);
1419 if (error)
1420 goto failed;
1421 }
1422
1423 return 0;
1424
1425failed:
0d259f04
JJ
1426 aafs_remove_dir(fs_dir);
1427
9acd494b
KC
1428 return error;
1429}
1430
1431/**
1432 * aafs_remove_file - drop a single file entry in the apparmor securityfs
1433 * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
1434 */
1435static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
1436{
1437 if (!fs_file->dentry)
1438 return;
1439
1440 securityfs_remove(fs_file->dentry);
1441 fs_file->dentry = NULL;
1442}
1443
1444/**
1445 * aafs_remove_dir - recursively drop a directory entry from the securityfs
1446 * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
1447 */
1448static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
1449{
1450 struct aa_fs_entry *fs_file;
1451
0d259f04 1452 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
9acd494b
KC
1453 if (fs_file->v_type == AA_FS_TYPE_DIR)
1454 aafs_remove_dir(fs_file);
1455 else
1456 aafs_remove_file(fs_file);
1457 }
1458
1459 aafs_remove_file(fs_dir);
63e2b423
JJ
1460}
1461
1462/**
1463 * aa_destroy_aafs - cleanup and free aafs
1464 *
1465 * releases dentries allocated by aa_create_aafs
1466 */
1467void __init aa_destroy_aafs(void)
1468{
9acd494b 1469 aafs_remove_dir(&aa_fs_entry);
63e2b423
JJ
1470}
1471
a71ada30
JJ
1472
1473#define NULL_FILE_NAME ".null"
1474struct path aa_null;
1475
1476static int aa_mk_null_file(struct dentry *parent)
1477{
1478 struct vfsmount *mount = NULL;
1479 struct dentry *dentry;
1480 struct inode *inode;
1481 int count = 0;
1482 int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
1483
1484 if (error)
1485 return error;
1486
1487 inode_lock(d_inode(parent));
1488 dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
1489 if (IS_ERR(dentry)) {
1490 error = PTR_ERR(dentry);
1491 goto out;
1492 }
1493 inode = new_inode(parent->d_inode->i_sb);
1494 if (!inode) {
1495 error = -ENOMEM;
1496 goto out1;
1497 }
1498
1499 inode->i_ino = get_next_ino();
1500 inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
24d0d03c 1501 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
a71ada30
JJ
1502 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
1503 MKDEV(MEM_MAJOR, 3));
1504 d_instantiate(dentry, inode);
1505 aa_null.dentry = dget(dentry);
1506 aa_null.mnt = mntget(mount);
1507
1508 error = 0;
1509
1510out1:
1511 dput(dentry);
1512out:
1513 inode_unlock(d_inode(parent));
1514 simple_release_fs(&mount, &count);
1515 return error;
1516}
1517
63e2b423
JJ
1518/**
1519 * aa_create_aafs - create the apparmor security filesystem
1520 *
1521 * dentries created here are released by aa_destroy_aafs
1522 *
1523 * Returns: error on failure
1524 */
3417d8d5 1525static int __init aa_create_aafs(void)
63e2b423 1526{
b7fd2c03 1527 struct dentry *dent;
63e2b423
JJ
1528 int error;
1529
1530 if (!apparmor_initialized)
1531 return 0;
1532
9acd494b 1533 if (aa_fs_entry.dentry) {
63e2b423
JJ
1534 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
1535 return -EEXIST;
1536 }
1537
9acd494b
KC
1538 /* Populate fs tree. */
1539 error = aafs_create_dir(&aa_fs_entry, NULL);
63e2b423
JJ
1540 if (error)
1541 goto error;
1542
b7fd2c03
JJ
1543 dent = securityfs_create_file(".load", 0666, aa_fs_entry.dentry,
1544 NULL, &aa_fs_profile_load);
1545 if (IS_ERR(dent)) {
1546 error = PTR_ERR(dent);
1547 goto error;
1548 }
1549 ns_subload(root_ns) = dent;
1550
1551 dent = securityfs_create_file(".replace", 0666, aa_fs_entry.dentry,
1552 NULL, &aa_fs_profile_replace);
1553 if (IS_ERR(dent)) {
1554 error = PTR_ERR(dent);
1555 goto error;
1556 }
1557 ns_subreplace(root_ns) = dent;
1558
1559 dent = securityfs_create_file(".remove", 0666, aa_fs_entry.dentry,
1560 NULL, &aa_fs_profile_remove);
1561 if (IS_ERR(dent)) {
1562 error = PTR_ERR(dent);
1563 goto error;
1564 }
1565 ns_subremove(root_ns) = dent;
1566
1567 mutex_lock(&root_ns->lock);
98849dff 1568 error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
b7fd2c03
JJ
1569 mutex_unlock(&root_ns->lock);
1570
0d259f04
JJ
1571 if (error)
1572 goto error;
1573
a71ada30
JJ
1574 error = aa_mk_null_file(aa_fs_entry.dentry);
1575 if (error)
1576 goto error;
1577
1578 /* TODO: add default profile to apparmorfs */
63e2b423
JJ
1579
1580 /* Report that AppArmor fs is enabled */
1581 aa_info_message("AppArmor Filesystem Enabled");
1582 return 0;
1583
1584error:
1585 aa_destroy_aafs();
1586 AA_ERROR("Error creating AppArmor securityfs\n");
1587 return error;
1588}
1589
1590fs_initcall(aa_create_aafs);