blob: 0d8dd71f989e9c90fa3041433208f1d6c32e461a [file] [log] [blame]
John Johansen63e2b422010-07-29 14:48:03 -07001/*
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
John Johansen0d259f02013-07-10 21:13:43 -070015#include <linux/ctype.h>
John Johansen63e2b422010-07-29 14:48:03 -070016#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>
21#include <linux/namei.h>
Kees Cooke74abcf2012-01-26 16:29:21 -080022#include <linux/capability.h>
John Johansen29b38222013-07-10 21:18:43 -070023#include <linux/rcupdate.h>
John Johansen63e2b422010-07-29 14:48:03 -070024
25#include "include/apparmor.h"
26#include "include/apparmorfs.h"
27#include "include/audit.h"
28#include "include/context.h"
John Johansenf8eb8a12013-08-14 11:27:36 -070029#include "include/crypto.h"
John Johansen63e2b422010-07-29 14:48:03 -070030#include "include/policy.h"
Kees Cookd384b0a2012-01-26 16:29:23 -080031#include "include/resource.h"
John Johansen63e2b422010-07-29 14:48:03 -070032
33/**
John Johansen0d259f02013-07-10 21:13:43 -070034 * aa_mangle_name - mangle a profile name to std profile layout form
35 * @name: profile name to mangle (NOT NULL)
36 * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
37 *
38 * Returns: length of mangled name
39 */
40static int mangle_name(char *name, char *target)
41{
42 char *t = target;
43
44 while (*name == '/' || *name == '.')
45 name++;
46
47 if (target) {
48 for (; *name; name++) {
49 if (*name == '/')
50 *(t)++ = '.';
51 else if (isspace(*name))
52 *(t)++ = '_';
53 else if (isalnum(*name) || strchr("._-", *name))
54 *(t)++ = *name;
55 }
56
57 *t = 0;
58 } else {
59 int len = 0;
60 for (; *name; name++) {
61 if (isalnum(*name) || isspace(*name) ||
62 strchr("/._-", *name))
63 len++;
64 }
65
66 return len;
67 }
68
69 return t - target;
70}
71
72/**
John Johansen63e2b422010-07-29 14:48:03 -070073 * aa_simple_write_to_buffer - common routine for getting policy from user
74 * @op: operation doing the user buffer copy
75 * @userbuf: user buffer to copy data from (NOT NULL)
John Johansen3ed02ad2010-10-09 00:47:53 -070076 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
John Johansen63e2b422010-07-29 14:48:03 -070077 * @copy_size: size of data to copy from user buffer
78 * @pos: position write is at in the file (NOT NULL)
79 *
80 * Returns: kernel buffer containing copy of user buffer data or an
81 * ERR_PTR on failure.
82 */
83static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
84 size_t alloc_size, size_t copy_size,
85 loff_t *pos)
86{
87 char *data;
88
John Johansen3ed02ad2010-10-09 00:47:53 -070089 BUG_ON(copy_size > alloc_size);
90
John Johansen63e2b422010-07-29 14:48:03 -070091 if (*pos != 0)
92 /* only writes from pos 0, that is complete writes */
93 return ERR_PTR(-ESPIPE);
94
95 /*
96 * Don't allow profile load/replace/remove from profiles that don't
97 * have CAP_MAC_ADMIN
98 */
99 if (!aa_may_manage_policy(op))
100 return ERR_PTR(-EACCES);
101
102 /* freed by caller to simple_write_to_buffer */
103 data = kvmalloc(alloc_size);
104 if (data == NULL)
105 return ERR_PTR(-ENOMEM);
106
107 if (copy_from_user(data, userbuf, copy_size)) {
108 kvfree(data);
109 return ERR_PTR(-EFAULT);
110 }
111
112 return data;
113}
114
115
116/* .load file hook fn to load policy */
117static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
118 loff_t *pos)
119{
120 char *data;
121 ssize_t error;
122
123 data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
124
125 error = PTR_ERR(data);
126 if (!IS_ERR(data)) {
127 error = aa_replace_profiles(data, size, PROF_ADD);
128 kvfree(data);
129 }
130
131 return error;
132}
133
134static const struct file_operations aa_fs_profile_load = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200135 .write = profile_load,
136 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700137};
138
139/* .replace file hook fn to load and/or replace policy */
140static ssize_t profile_replace(struct file *f, const char __user *buf,
141 size_t size, loff_t *pos)
142{
143 char *data;
144 ssize_t error;
145
146 data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
147 error = PTR_ERR(data);
148 if (!IS_ERR(data)) {
149 error = aa_replace_profiles(data, size, PROF_REPLACE);
150 kvfree(data);
151 }
152
153 return error;
154}
155
156static const struct file_operations aa_fs_profile_replace = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200157 .write = profile_replace,
158 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700159};
160
161/* .remove file hook fn to remove loaded policy */
162static ssize_t profile_remove(struct file *f, const char __user *buf,
163 size_t size, loff_t *pos)
164{
165 char *data;
166 ssize_t error;
167
168 /*
169 * aa_remove_profile needs a null terminated string so 1 extra
170 * byte is allocated and the copied data is null terminated.
171 */
172 data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
173
174 error = PTR_ERR(data);
175 if (!IS_ERR(data)) {
176 data[size] = 0;
177 error = aa_remove_profiles(data, size);
178 kvfree(data);
179 }
180
181 return error;
182}
183
184static const struct file_operations aa_fs_profile_remove = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200185 .write = profile_remove,
186 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700187};
188
Kees Cooke74abcf2012-01-26 16:29:21 -0800189static int aa_fs_seq_show(struct seq_file *seq, void *v)
190{
191 struct aa_fs_entry *fs_file = seq->private;
192
193 if (!fs_file)
194 return 0;
195
196 switch (fs_file->v_type) {
197 case AA_FS_TYPE_BOOLEAN:
198 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
199 break;
Kees Cooka9bf8e92012-01-26 16:29:22 -0800200 case AA_FS_TYPE_STRING:
201 seq_printf(seq, "%s\n", fs_file->v.string);
202 break;
Kees Cooke74abcf2012-01-26 16:29:21 -0800203 case AA_FS_TYPE_U64:
204 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
205 break;
206 default:
207 /* Ignore unpritable entry types. */
208 break;
209 }
210
211 return 0;
212}
213
214static int aa_fs_seq_open(struct inode *inode, struct file *file)
215{
216 return single_open(file, aa_fs_seq_show, inode->i_private);
217}
218
219const struct file_operations aa_fs_seq_file_ops = {
220 .owner = THIS_MODULE,
221 .open = aa_fs_seq_open,
222 .read = seq_read,
223 .llseek = seq_lseek,
224 .release = single_release,
225};
226
John Johansen0d259f02013-07-10 21:13:43 -0700227static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
228 int (*show)(struct seq_file *, void *))
229{
230 struct aa_replacedby *r = aa_get_replacedby(inode->i_private);
231 int error = single_open(file, show, r);
John Johansen63e2b422010-07-29 14:48:03 -0700232
John Johansen0d259f02013-07-10 21:13:43 -0700233 if (error) {
234 file->private_data = NULL;
235 aa_put_replacedby(r);
236 }
237
238 return error;
239}
240
241static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
242{
243 struct seq_file *seq = (struct seq_file *) file->private_data;
244 if (seq)
245 aa_put_replacedby(seq->private);
246 return single_release(inode, file);
247}
248
249static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
250{
251 struct aa_replacedby *r = seq->private;
252 struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
253 seq_printf(seq, "%s\n", profile->base.name);
254 aa_put_profile(profile);
255
256 return 0;
257}
258
259static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
260{
261 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
262}
263
264static const struct file_operations aa_fs_profname_fops = {
265 .owner = THIS_MODULE,
266 .open = aa_fs_seq_profname_open,
267 .read = seq_read,
268 .llseek = seq_lseek,
269 .release = aa_fs_seq_profile_release,
270};
271
272static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
273{
274 struct aa_replacedby *r = seq->private;
275 struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
276 seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
277 aa_put_profile(profile);
278
279 return 0;
280}
281
282static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
283{
284 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
285}
286
287static const struct file_operations aa_fs_profmode_fops = {
288 .owner = THIS_MODULE,
289 .open = aa_fs_seq_profmode_open,
290 .read = seq_read,
291 .llseek = seq_lseek,
292 .release = aa_fs_seq_profile_release,
293};
294
John Johansen556d0be2013-07-10 21:17:43 -0700295static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
296{
297 struct aa_replacedby *r = seq->private;
298 struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
299 if (profile->attach)
300 seq_printf(seq, "%s\n", profile->attach);
301 else if (profile->xmatch)
302 seq_puts(seq, "<unknown>\n");
303 else
304 seq_printf(seq, "%s\n", profile->base.name);
305 aa_put_profile(profile);
306
307 return 0;
308}
309
310static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
311{
312 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
313}
314
315static const struct file_operations aa_fs_profattach_fops = {
316 .owner = THIS_MODULE,
317 .open = aa_fs_seq_profattach_open,
318 .read = seq_read,
319 .llseek = seq_lseek,
320 .release = aa_fs_seq_profile_release,
321};
322
John Johansenf8eb8a12013-08-14 11:27:36 -0700323static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
324{
325 struct aa_replacedby *r = seq->private;
326 struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
327 unsigned int i, size = aa_hash_size();
328
329 if (profile->hash) {
330 for (i = 0; i < size; i++)
331 seq_printf(seq, "%.2x", profile->hash[i]);
332 seq_puts(seq, "\n");
333 }
John Johansen0b938a22015-11-18 11:41:05 -0800334 aa_put_profile(profile);
John Johansenf8eb8a12013-08-14 11:27:36 -0700335
336 return 0;
337}
338
339static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
340{
341 return single_open(file, aa_fs_seq_hash_show, inode->i_private);
342}
343
344static const struct file_operations aa_fs_seq_hash_fops = {
345 .owner = THIS_MODULE,
346 .open = aa_fs_seq_hash_open,
347 .read = seq_read,
348 .llseek = seq_lseek,
349 .release = single_release,
350};
351
John Johansen0d259f02013-07-10 21:13:43 -0700352/** fns to setup dynamic per profile/namespace files **/
353void __aa_fs_profile_rmdir(struct aa_profile *profile)
354{
355 struct aa_profile *child;
356 int i;
357
358 if (!profile)
359 return;
360
361 list_for_each_entry(child, &profile->base.profiles, base.list)
362 __aa_fs_profile_rmdir(child);
363
364 for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
365 struct aa_replacedby *r;
366 if (!profile->dents[i])
367 continue;
368
David Howellsce0b16d2015-02-19 10:47:02 +0000369 r = d_inode(profile->dents[i])->i_private;
John Johansen0d259f02013-07-10 21:13:43 -0700370 securityfs_remove(profile->dents[i]);
371 aa_put_replacedby(r);
372 profile->dents[i] = NULL;
373 }
374}
375
376void __aa_fs_profile_migrate_dents(struct aa_profile *old,
377 struct aa_profile *new)
378{
379 int i;
380
381 for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
382 new->dents[i] = old->dents[i];
John Johansend671e8902014-07-25 04:01:56 -0700383 if (new->dents[i])
384 new->dents[i]->d_inode->i_mtime = CURRENT_TIME;
John Johansen0d259f02013-07-10 21:13:43 -0700385 old->dents[i] = NULL;
386 }
387}
388
389static struct dentry *create_profile_file(struct dentry *dir, const char *name,
390 struct aa_profile *profile,
391 const struct file_operations *fops)
392{
393 struct aa_replacedby *r = aa_get_replacedby(profile->replacedby);
394 struct dentry *dent;
395
396 dent = securityfs_create_file(name, S_IFREG | 0444, dir, r, fops);
397 if (IS_ERR(dent))
398 aa_put_replacedby(r);
399
400 return dent;
401}
402
403/* requires lock be held */
404int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
405{
406 struct aa_profile *child;
407 struct dentry *dent = NULL, *dir;
408 int error;
409
410 if (!parent) {
411 struct aa_profile *p;
412 p = aa_deref_parent(profile);
413 dent = prof_dir(p);
414 /* adding to parent that previously didn't have children */
415 dent = securityfs_create_dir("profiles", dent);
416 if (IS_ERR(dent))
417 goto fail;
418 prof_child_dir(p) = parent = dent;
419 }
420
421 if (!profile->dirname) {
422 int len, id_len;
423 len = mangle_name(profile->base.name, NULL);
424 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
425
426 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
427 if (!profile->dirname)
428 goto fail;
429
430 mangle_name(profile->base.name, profile->dirname);
431 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
432 }
433
434 dent = securityfs_create_dir(profile->dirname, parent);
435 if (IS_ERR(dent))
436 goto fail;
437 prof_dir(profile) = dir = dent;
438
439 dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
440 if (IS_ERR(dent))
441 goto fail;
442 profile->dents[AAFS_PROF_NAME] = dent;
443
444 dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
445 if (IS_ERR(dent))
446 goto fail;
447 profile->dents[AAFS_PROF_MODE] = dent;
448
John Johansen556d0be2013-07-10 21:17:43 -0700449 dent = create_profile_file(dir, "attach", profile,
450 &aa_fs_profattach_fops);
451 if (IS_ERR(dent))
452 goto fail;
453 profile->dents[AAFS_PROF_ATTACH] = dent;
454
John Johansenf8eb8a12013-08-14 11:27:36 -0700455 if (profile->hash) {
456 dent = create_profile_file(dir, "sha1", profile,
457 &aa_fs_seq_hash_fops);
458 if (IS_ERR(dent))
459 goto fail;
460 profile->dents[AAFS_PROF_HASH] = dent;
461 }
462
John Johansen0d259f02013-07-10 21:13:43 -0700463 list_for_each_entry(child, &profile->base.profiles, base.list) {
464 error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
465 if (error)
466 goto fail2;
467 }
468
469 return 0;
470
471fail:
472 error = PTR_ERR(dent);
473
474fail2:
475 __aa_fs_profile_rmdir(profile);
476
477 return error;
478}
479
480void __aa_fs_namespace_rmdir(struct aa_namespace *ns)
481{
482 struct aa_namespace *sub;
483 struct aa_profile *child;
484 int i;
485
486 if (!ns)
487 return;
488
489 list_for_each_entry(child, &ns->base.profiles, base.list)
490 __aa_fs_profile_rmdir(child);
491
492 list_for_each_entry(sub, &ns->sub_ns, base.list) {
493 mutex_lock(&sub->lock);
494 __aa_fs_namespace_rmdir(sub);
495 mutex_unlock(&sub->lock);
496 }
497
498 for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
499 securityfs_remove(ns->dents[i]);
500 ns->dents[i] = NULL;
501 }
502}
503
504int __aa_fs_namespace_mkdir(struct aa_namespace *ns, struct dentry *parent,
505 const char *name)
506{
507 struct aa_namespace *sub;
508 struct aa_profile *child;
509 struct dentry *dent, *dir;
510 int error;
511
512 if (!name)
513 name = ns->base.name;
514
515 dent = securityfs_create_dir(name, parent);
516 if (IS_ERR(dent))
517 goto fail;
518 ns_dir(ns) = dir = dent;
519
520 dent = securityfs_create_dir("profiles", dir);
521 if (IS_ERR(dent))
522 goto fail;
523 ns_subprofs_dir(ns) = dent;
524
525 dent = securityfs_create_dir("namespaces", dir);
526 if (IS_ERR(dent))
527 goto fail;
528 ns_subns_dir(ns) = dent;
529
530 list_for_each_entry(child, &ns->base.profiles, base.list) {
531 error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
532 if (error)
533 goto fail2;
534 }
535
536 list_for_each_entry(sub, &ns->sub_ns, base.list) {
537 mutex_lock(&sub->lock);
538 error = __aa_fs_namespace_mkdir(sub, ns_subns_dir(ns), NULL);
539 mutex_unlock(&sub->lock);
540 if (error)
541 goto fail2;
542 }
543
544 return 0;
545
546fail:
547 error = PTR_ERR(dent);
548
549fail2:
550 __aa_fs_namespace_rmdir(ns);
551
552 return error;
553}
554
555
John Johansen29b38222013-07-10 21:18:43 -0700556#define list_entry_next(pos, member) \
557 list_entry(pos->member.next, typeof(*pos), member)
558#define list_entry_is_head(pos, head, member) (&pos->member == (head))
559
560/**
561 * __next_namespace - find the next namespace to list
562 * @root: root namespace to stop search at (NOT NULL)
563 * @ns: current ns position (NOT NULL)
564 *
565 * Find the next namespace from @ns under @root and handle all locking needed
566 * while switching current namespace.
567 *
568 * Returns: next namespace or NULL if at last namespace under @root
569 * Requires: ns->parent->lock to be held
570 * NOTE: will not unlock root->lock
571 */
572static struct aa_namespace *__next_namespace(struct aa_namespace *root,
573 struct aa_namespace *ns)
574{
575 struct aa_namespace *parent, *next;
576
577 /* is next namespace a child */
578 if (!list_empty(&ns->sub_ns)) {
579 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
580 mutex_lock(&next->lock);
581 return next;
582 }
583
584 /* check if the next ns is a sibling, parent, gp, .. */
585 parent = ns->parent;
John Johansened2c7da2013-10-14 11:46:27 -0700586 while (ns != root) {
John Johansen29b38222013-07-10 21:18:43 -0700587 mutex_unlock(&ns->lock);
588 next = list_entry_next(ns, base.list);
589 if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
590 mutex_lock(&next->lock);
591 return next;
592 }
John Johansen29b38222013-07-10 21:18:43 -0700593 ns = parent;
594 parent = parent->parent;
595 }
596
597 return NULL;
598}
599
600/**
601 * __first_profile - find the first profile in a namespace
602 * @root: namespace that is root of profiles being displayed (NOT NULL)
603 * @ns: namespace to start in (NOT NULL)
604 *
605 * Returns: unrefcounted profile or NULL if no profile
606 * Requires: profile->ns.lock to be held
607 */
608static struct aa_profile *__first_profile(struct aa_namespace *root,
609 struct aa_namespace *ns)
610{
611 for (; ns; ns = __next_namespace(root, ns)) {
612 if (!list_empty(&ns->base.profiles))
613 return list_first_entry(&ns->base.profiles,
614 struct aa_profile, base.list);
615 }
616 return NULL;
617}
618
619/**
620 * __next_profile - step to the next profile in a profile tree
621 * @profile: current profile in tree (NOT NULL)
622 *
623 * Perform a depth first traversal on the profile tree in a namespace
624 *
625 * Returns: next profile or NULL if done
626 * Requires: profile->ns.lock to be held
627 */
628static struct aa_profile *__next_profile(struct aa_profile *p)
629{
630 struct aa_profile *parent;
631 struct aa_namespace *ns = p->ns;
632
633 /* is next profile a child */
634 if (!list_empty(&p->base.profiles))
635 return list_first_entry(&p->base.profiles, typeof(*p),
636 base.list);
637
638 /* is next profile a sibling, parent sibling, gp, sibling, .. */
639 parent = rcu_dereference_protected(p->parent,
640 mutex_is_locked(&p->ns->lock));
641 while (parent) {
642 p = list_entry_next(p, base.list);
643 if (!list_entry_is_head(p, &parent->base.profiles, base.list))
644 return p;
645 p = parent;
646 parent = rcu_dereference_protected(parent->parent,
647 mutex_is_locked(&parent->ns->lock));
648 }
649
650 /* is next another profile in the namespace */
651 p = list_entry_next(p, base.list);
652 if (!list_entry_is_head(p, &ns->base.profiles, base.list))
653 return p;
654
655 return NULL;
656}
657
658/**
659 * next_profile - step to the next profile in where ever it may be
660 * @root: root namespace (NOT NULL)
661 * @profile: current profile (NOT NULL)
662 *
663 * Returns: next profile or NULL if there isn't one
664 */
665static struct aa_profile *next_profile(struct aa_namespace *root,
666 struct aa_profile *profile)
667{
668 struct aa_profile *next = __next_profile(profile);
669 if (next)
670 return next;
671
672 /* finished all profiles in namespace move to next namespace */
673 return __first_profile(root, __next_namespace(root, profile->ns));
674}
675
676/**
677 * p_start - start a depth first traversal of profile tree
678 * @f: seq_file to fill
679 * @pos: current position
680 *
681 * Returns: first profile under current namespace or NULL if none found
682 *
683 * acquires first ns->lock
684 */
685static void *p_start(struct seq_file *f, loff_t *pos)
686{
687 struct aa_profile *profile = NULL;
688 struct aa_namespace *root = aa_current_profile()->ns;
689 loff_t l = *pos;
690 f->private = aa_get_namespace(root);
691
692
693 /* find the first profile */
694 mutex_lock(&root->lock);
695 profile = __first_profile(root, root);
696
697 /* skip to position */
698 for (; profile && l > 0; l--)
699 profile = next_profile(root, profile);
700
701 return profile;
702}
703
704/**
705 * p_next - read the next profile entry
706 * @f: seq_file to fill
707 * @p: profile previously returned
708 * @pos: current position
709 *
710 * Returns: next profile after @p or NULL if none
711 *
712 * may acquire/release locks in namespace tree as necessary
713 */
714static void *p_next(struct seq_file *f, void *p, loff_t *pos)
715{
716 struct aa_profile *profile = p;
717 struct aa_namespace *ns = f->private;
718 (*pos)++;
719
720 return next_profile(ns, profile);
721}
722
723/**
724 * p_stop - stop depth first traversal
725 * @f: seq_file we are filling
726 * @p: the last profile writen
727 *
728 * Release all locking done by p_start/p_next on namespace tree
729 */
730static void p_stop(struct seq_file *f, void *p)
731{
732 struct aa_profile *profile = p;
733 struct aa_namespace *root = f->private, *ns;
734
735 if (profile) {
736 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
737 mutex_unlock(&ns->lock);
738 }
739 mutex_unlock(&root->lock);
740 aa_put_namespace(root);
741}
742
743/**
744 * seq_show_profile - show a profile entry
745 * @f: seq_file to file
746 * @p: current position (profile) (NOT NULL)
747 *
748 * Returns: error on failure
749 */
750static int seq_show_profile(struct seq_file *f, void *p)
751{
752 struct aa_profile *profile = (struct aa_profile *)p;
753 struct aa_namespace *root = f->private;
754
755 if (profile->ns != root)
756 seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
757 seq_printf(f, "%s (%s)\n", profile->base.hname,
758 aa_profile_mode_names[profile->mode]);
759
760 return 0;
761}
762
763static const struct seq_operations aa_fs_profiles_op = {
764 .start = p_start,
765 .next = p_next,
766 .stop = p_stop,
767 .show = seq_show_profile,
768};
769
770static int profiles_open(struct inode *inode, struct file *file)
771{
772 return seq_open(file, &aa_fs_profiles_op);
773}
774
775static int profiles_release(struct inode *inode, struct file *file)
776{
777 return seq_release(inode, file);
778}
779
780static const struct file_operations aa_fs_profiles_fops = {
781 .open = profiles_open,
782 .read = seq_read,
783 .llseek = seq_lseek,
784 .release = profiles_release,
785};
786
787
John Johansen0d259f02013-07-10 21:13:43 -0700788/** Base file system setup **/
Kees Cooka9bf8e92012-01-26 16:29:22 -0800789static struct aa_fs_entry aa_fs_entry_file[] = {
790 AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
791 "link lock"),
792 { }
793};
794
Kees Cooke74abcf2012-01-26 16:29:21 -0800795static struct aa_fs_entry aa_fs_entry_domain[] = {
796 AA_FS_FILE_BOOLEAN("change_hat", 1),
797 AA_FS_FILE_BOOLEAN("change_hatv", 1),
798 AA_FS_FILE_BOOLEAN("change_onexec", 1),
799 AA_FS_FILE_BOOLEAN("change_profile", 1),
800 { }
801};
802
John Johansen9d910a32013-07-10 21:04:43 -0700803static struct aa_fs_entry aa_fs_entry_policy[] = {
John Johansendd51c8482013-07-10 21:05:43 -0700804 AA_FS_FILE_BOOLEAN("set_load", 1),
John Johansen9d910a32013-07-10 21:04:43 -0700805 {}
806};
807
Kees Cooke74abcf2012-01-26 16:29:21 -0800808static struct aa_fs_entry aa_fs_entry_features[] = {
John Johansen9d910a32013-07-10 21:04:43 -0700809 AA_FS_DIR("policy", aa_fs_entry_policy),
Kees Cooke74abcf2012-01-26 16:29:21 -0800810 AA_FS_DIR("domain", aa_fs_entry_domain),
Kees Cooka9bf8e92012-01-26 16:29:22 -0800811 AA_FS_DIR("file", aa_fs_entry_file),
Kees Cooke74abcf2012-01-26 16:29:21 -0800812 AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
Kees Cookd384b0a2012-01-26 16:29:23 -0800813 AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
John Johansen84f1f782013-08-14 11:27:32 -0700814 AA_FS_DIR("caps", aa_fs_entry_caps),
Kees Cooke74abcf2012-01-26 16:29:21 -0800815 { }
816};
817
Kees Cook9acd4942012-01-26 16:29:20 -0800818static struct aa_fs_entry aa_fs_entry_apparmor[] = {
819 AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
820 AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
821 AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
John Johansen29b38222013-07-10 21:18:43 -0700822 AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
Kees Cooke74abcf2012-01-26 16:29:21 -0800823 AA_FS_DIR("features", aa_fs_entry_features),
Kees Cook9acd4942012-01-26 16:29:20 -0800824 { }
825};
John Johansen63e2b422010-07-29 14:48:03 -0700826
Kees Cook9acd4942012-01-26 16:29:20 -0800827static struct aa_fs_entry aa_fs_entry =
828 AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
829
830/**
831 * aafs_create_file - create a file entry in the apparmor securityfs
832 * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
833 * @parent: the parent dentry in the securityfs
834 *
835 * Use aafs_remove_file to remove entries created with this fn.
836 */
837static int __init aafs_create_file(struct aa_fs_entry *fs_file,
838 struct dentry *parent)
John Johansen63e2b422010-07-29 14:48:03 -0700839{
Kees Cook9acd4942012-01-26 16:29:20 -0800840 int error = 0;
John Johansen63e2b422010-07-29 14:48:03 -0700841
Kees Cook9acd4942012-01-26 16:29:20 -0800842 fs_file->dentry = securityfs_create_file(fs_file->name,
843 S_IFREG | fs_file->mode,
844 parent, fs_file,
845 fs_file->file_ops);
846 if (IS_ERR(fs_file->dentry)) {
847 error = PTR_ERR(fs_file->dentry);
848 fs_file->dentry = NULL;
John Johansen63e2b422010-07-29 14:48:03 -0700849 }
Kees Cook9acd4942012-01-26 16:29:20 -0800850 return error;
John Johansen63e2b422010-07-29 14:48:03 -0700851}
852
John Johansen0d259f02013-07-10 21:13:43 -0700853static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
John Johansen63e2b422010-07-29 14:48:03 -0700854/**
Kees Cook9acd4942012-01-26 16:29:20 -0800855 * aafs_create_dir - recursively create a directory entry in the securityfs
856 * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
857 * @parent: the parent dentry in the securityfs
John Johansen63e2b422010-07-29 14:48:03 -0700858 *
Kees Cook9acd4942012-01-26 16:29:20 -0800859 * Use aafs_remove_dir to remove entries created with this fn.
John Johansen63e2b422010-07-29 14:48:03 -0700860 */
Kees Cook9acd4942012-01-26 16:29:20 -0800861static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
862 struct dentry *parent)
John Johansen63e2b422010-07-29 14:48:03 -0700863{
Kees Cook9acd4942012-01-26 16:29:20 -0800864 struct aa_fs_entry *fs_file;
John Johansen0d259f02013-07-10 21:13:43 -0700865 struct dentry *dir;
866 int error;
John Johansen63e2b422010-07-29 14:48:03 -0700867
John Johansen0d259f02013-07-10 21:13:43 -0700868 dir = securityfs_create_dir(fs_dir->name, parent);
869 if (IS_ERR(dir))
870 return PTR_ERR(dir);
871 fs_dir->dentry = dir;
John Johansen63e2b422010-07-29 14:48:03 -0700872
John Johansen0d259f02013-07-10 21:13:43 -0700873 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
Kees Cook9acd4942012-01-26 16:29:20 -0800874 if (fs_file->v_type == AA_FS_TYPE_DIR)
875 error = aafs_create_dir(fs_file, fs_dir->dentry);
876 else
877 error = aafs_create_file(fs_file, fs_dir->dentry);
878 if (error)
879 goto failed;
880 }
881
882 return 0;
883
884failed:
John Johansen0d259f02013-07-10 21:13:43 -0700885 aafs_remove_dir(fs_dir);
886
Kees Cook9acd4942012-01-26 16:29:20 -0800887 return error;
888}
889
890/**
891 * aafs_remove_file - drop a single file entry in the apparmor securityfs
892 * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
893 */
894static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
895{
896 if (!fs_file->dentry)
897 return;
898
899 securityfs_remove(fs_file->dentry);
900 fs_file->dentry = NULL;
901}
902
903/**
904 * aafs_remove_dir - recursively drop a directory entry from the securityfs
905 * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
906 */
907static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
908{
909 struct aa_fs_entry *fs_file;
910
John Johansen0d259f02013-07-10 21:13:43 -0700911 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
Kees Cook9acd4942012-01-26 16:29:20 -0800912 if (fs_file->v_type == AA_FS_TYPE_DIR)
913 aafs_remove_dir(fs_file);
914 else
915 aafs_remove_file(fs_file);
916 }
917
918 aafs_remove_file(fs_dir);
John Johansen63e2b422010-07-29 14:48:03 -0700919}
920
921/**
922 * aa_destroy_aafs - cleanup and free aafs
923 *
924 * releases dentries allocated by aa_create_aafs
925 */
926void __init aa_destroy_aafs(void)
927{
Kees Cook9acd4942012-01-26 16:29:20 -0800928 aafs_remove_dir(&aa_fs_entry);
John Johansen63e2b422010-07-29 14:48:03 -0700929}
930
931/**
932 * aa_create_aafs - create the apparmor security filesystem
933 *
934 * dentries created here are released by aa_destroy_aafs
935 *
936 * Returns: error on failure
937 */
James Morris3417d8d2011-08-17 11:05:21 +1000938static int __init aa_create_aafs(void)
John Johansen63e2b422010-07-29 14:48:03 -0700939{
940 int error;
941
942 if (!apparmor_initialized)
943 return 0;
944
Kees Cook9acd4942012-01-26 16:29:20 -0800945 if (aa_fs_entry.dentry) {
John Johansen63e2b422010-07-29 14:48:03 -0700946 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
947 return -EEXIST;
948 }
949
Kees Cook9acd4942012-01-26 16:29:20 -0800950 /* Populate fs tree. */
951 error = aafs_create_dir(&aa_fs_entry, NULL);
John Johansen63e2b422010-07-29 14:48:03 -0700952 if (error)
953 goto error;
954
John Johansen0d259f02013-07-10 21:13:43 -0700955 error = __aa_fs_namespace_mkdir(root_ns, aa_fs_entry.dentry,
956 "policy");
957 if (error)
958 goto error;
959
John Johansen63e2b422010-07-29 14:48:03 -0700960 /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
961
962 /* Report that AppArmor fs is enabled */
963 aa_info_message("AppArmor Filesystem Enabled");
964 return 0;
965
966error:
967 aa_destroy_aafs();
968 AA_ERROR("Error creating AppArmor securityfs\n");
969 return error;
970}
971
972fs_initcall(aa_create_aafs);