ARM: mm: Recreate kernel mappings in early_paging_init()
[linux/fpc-iii.git] / security / apparmor / apparmorfs.c
blob95c2b2689a03aa521bc923323f521a8c191d107d
1 /*
2 * AppArmor security module
4 * This file contains AppArmor /sys/kernel/security/apparmor interface functions
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
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.
15 #include <linux/ctype.h>
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>
21 #include <linux/namei.h>
22 #include <linux/capability.h>
23 #include <linux/rcupdate.h>
25 #include "include/apparmor.h"
26 #include "include/apparmorfs.h"
27 #include "include/audit.h"
28 #include "include/context.h"
29 #include "include/crypto.h"
30 #include "include/policy.h"
31 #include "include/resource.h"
33 /**
34 * 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)
38 * Returns: length of mangled name
40 static int mangle_name(char *name, char *target)
42 char *t = target;
44 while (*name == '/' || *name == '.')
45 name++;
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;
57 *t = 0;
58 } else {
59 int len = 0;
60 for (; *name; name++) {
61 if (isalnum(*name) || isspace(*name) ||
62 strchr("/._-", *name))
63 len++;
66 return len;
69 return t - target;
72 /**
73 * 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)
76 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
77 * @copy_size: size of data to copy from user buffer
78 * @pos: position write is at in the file (NOT NULL)
80 * Returns: kernel buffer containing copy of user buffer data or an
81 * ERR_PTR on failure.
83 static 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)
87 char *data;
89 BUG_ON(copy_size > alloc_size);
91 if (*pos != 0)
92 /* only writes from pos 0, that is complete writes */
93 return ERR_PTR(-ESPIPE);
96 * Don't allow profile load/replace/remove from profiles that don't
97 * have CAP_MAC_ADMIN
99 if (!aa_may_manage_policy(op))
100 return ERR_PTR(-EACCES);
102 /* freed by caller to simple_write_to_buffer */
103 data = kvmalloc(alloc_size);
104 if (data == NULL)
105 return ERR_PTR(-ENOMEM);
107 if (copy_from_user(data, userbuf, copy_size)) {
108 kvfree(data);
109 return ERR_PTR(-EFAULT);
112 return data;
116 /* .load file hook fn to load policy */
117 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
118 loff_t *pos)
120 char *data;
121 ssize_t error;
123 data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
125 error = PTR_ERR(data);
126 if (!IS_ERR(data)) {
127 error = aa_replace_profiles(data, size, PROF_ADD);
128 kvfree(data);
131 return error;
134 static const struct file_operations aa_fs_profile_load = {
135 .write = profile_load,
136 .llseek = default_llseek,
139 /* .replace file hook fn to load and/or replace policy */
140 static ssize_t profile_replace(struct file *f, const char __user *buf,
141 size_t size, loff_t *pos)
143 char *data;
144 ssize_t error;
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);
153 return error;
156 static const struct file_operations aa_fs_profile_replace = {
157 .write = profile_replace,
158 .llseek = default_llseek,
161 /* .remove file hook fn to remove loaded policy */
162 static ssize_t profile_remove(struct file *f, const char __user *buf,
163 size_t size, loff_t *pos)
165 char *data;
166 ssize_t error;
169 * aa_remove_profile needs a null terminated string so 1 extra
170 * byte is allocated and the copied data is null terminated.
172 data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
174 error = PTR_ERR(data);
175 if (!IS_ERR(data)) {
176 data[size] = 0;
177 error = aa_remove_profiles(data, size);
178 kvfree(data);
181 return error;
184 static const struct file_operations aa_fs_profile_remove = {
185 .write = profile_remove,
186 .llseek = default_llseek,
189 static int aa_fs_seq_show(struct seq_file *seq, void *v)
191 struct aa_fs_entry *fs_file = seq->private;
193 if (!fs_file)
194 return 0;
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;
200 case AA_FS_TYPE_STRING:
201 seq_printf(seq, "%s\n", fs_file->v.string);
202 break;
203 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;
211 return 0;
214 static int aa_fs_seq_open(struct inode *inode, struct file *file)
216 return single_open(file, aa_fs_seq_show, inode->i_private);
219 const 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,
227 static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
228 int (*show)(struct seq_file *, void *))
230 struct aa_replacedby *r = aa_get_replacedby(inode->i_private);
231 int error = single_open(file, show, r);
233 if (error) {
234 file->private_data = NULL;
235 aa_put_replacedby(r);
238 return error;
241 static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
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);
249 static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
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);
256 return 0;
259 static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
261 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
264 static 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,
272 static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
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);
279 return 0;
282 static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
284 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
287 static 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,
295 static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
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);
307 return 0;
310 static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
312 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
315 static 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,
323 static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
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();
329 if (profile->hash) {
330 for (i = 0; i < size; i++)
331 seq_printf(seq, "%.2x", profile->hash[i]);
332 seq_puts(seq, "\n");
335 return 0;
338 static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
340 return single_open(file, aa_fs_seq_hash_show, inode->i_private);
343 static const struct file_operations aa_fs_seq_hash_fops = {
344 .owner = THIS_MODULE,
345 .open = aa_fs_seq_hash_open,
346 .read = seq_read,
347 .llseek = seq_lseek,
348 .release = single_release,
351 /** fns to setup dynamic per profile/namespace files **/
352 void __aa_fs_profile_rmdir(struct aa_profile *profile)
354 struct aa_profile *child;
355 int i;
357 if (!profile)
358 return;
360 list_for_each_entry(child, &profile->base.profiles, base.list)
361 __aa_fs_profile_rmdir(child);
363 for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
364 struct aa_replacedby *r;
365 if (!profile->dents[i])
366 continue;
368 r = profile->dents[i]->d_inode->i_private;
369 securityfs_remove(profile->dents[i]);
370 aa_put_replacedby(r);
371 profile->dents[i] = NULL;
375 void __aa_fs_profile_migrate_dents(struct aa_profile *old,
376 struct aa_profile *new)
378 int i;
380 for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
381 new->dents[i] = old->dents[i];
382 old->dents[i] = NULL;
386 static struct dentry *create_profile_file(struct dentry *dir, const char *name,
387 struct aa_profile *profile,
388 const struct file_operations *fops)
390 struct aa_replacedby *r = aa_get_replacedby(profile->replacedby);
391 struct dentry *dent;
393 dent = securityfs_create_file(name, S_IFREG | 0444, dir, r, fops);
394 if (IS_ERR(dent))
395 aa_put_replacedby(r);
397 return dent;
400 /* requires lock be held */
401 int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
403 struct aa_profile *child;
404 struct dentry *dent = NULL, *dir;
405 int error;
407 if (!parent) {
408 struct aa_profile *p;
409 p = aa_deref_parent(profile);
410 dent = prof_dir(p);
411 /* adding to parent that previously didn't have children */
412 dent = securityfs_create_dir("profiles", dent);
413 if (IS_ERR(dent))
414 goto fail;
415 prof_child_dir(p) = parent = dent;
418 if (!profile->dirname) {
419 int len, id_len;
420 len = mangle_name(profile->base.name, NULL);
421 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
423 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
424 if (!profile->dirname)
425 goto fail;
427 mangle_name(profile->base.name, profile->dirname);
428 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
431 dent = securityfs_create_dir(profile->dirname, parent);
432 if (IS_ERR(dent))
433 goto fail;
434 prof_dir(profile) = dir = dent;
436 dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
437 if (IS_ERR(dent))
438 goto fail;
439 profile->dents[AAFS_PROF_NAME] = dent;
441 dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
442 if (IS_ERR(dent))
443 goto fail;
444 profile->dents[AAFS_PROF_MODE] = dent;
446 dent = create_profile_file(dir, "attach", profile,
447 &aa_fs_profattach_fops);
448 if (IS_ERR(dent))
449 goto fail;
450 profile->dents[AAFS_PROF_ATTACH] = dent;
452 if (profile->hash) {
453 dent = create_profile_file(dir, "sha1", profile,
454 &aa_fs_seq_hash_fops);
455 if (IS_ERR(dent))
456 goto fail;
457 profile->dents[AAFS_PROF_HASH] = dent;
460 list_for_each_entry(child, &profile->base.profiles, base.list) {
461 error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
462 if (error)
463 goto fail2;
466 return 0;
468 fail:
469 error = PTR_ERR(dent);
471 fail2:
472 __aa_fs_profile_rmdir(profile);
474 return error;
477 void __aa_fs_namespace_rmdir(struct aa_namespace *ns)
479 struct aa_namespace *sub;
480 struct aa_profile *child;
481 int i;
483 if (!ns)
484 return;
486 list_for_each_entry(child, &ns->base.profiles, base.list)
487 __aa_fs_profile_rmdir(child);
489 list_for_each_entry(sub, &ns->sub_ns, base.list) {
490 mutex_lock(&sub->lock);
491 __aa_fs_namespace_rmdir(sub);
492 mutex_unlock(&sub->lock);
495 for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
496 securityfs_remove(ns->dents[i]);
497 ns->dents[i] = NULL;
501 int __aa_fs_namespace_mkdir(struct aa_namespace *ns, struct dentry *parent,
502 const char *name)
504 struct aa_namespace *sub;
505 struct aa_profile *child;
506 struct dentry *dent, *dir;
507 int error;
509 if (!name)
510 name = ns->base.name;
512 dent = securityfs_create_dir(name, parent);
513 if (IS_ERR(dent))
514 goto fail;
515 ns_dir(ns) = dir = dent;
517 dent = securityfs_create_dir("profiles", dir);
518 if (IS_ERR(dent))
519 goto fail;
520 ns_subprofs_dir(ns) = dent;
522 dent = securityfs_create_dir("namespaces", dir);
523 if (IS_ERR(dent))
524 goto fail;
525 ns_subns_dir(ns) = dent;
527 list_for_each_entry(child, &ns->base.profiles, base.list) {
528 error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
529 if (error)
530 goto fail2;
533 list_for_each_entry(sub, &ns->sub_ns, base.list) {
534 mutex_lock(&sub->lock);
535 error = __aa_fs_namespace_mkdir(sub, ns_subns_dir(ns), NULL);
536 mutex_unlock(&sub->lock);
537 if (error)
538 goto fail2;
541 return 0;
543 fail:
544 error = PTR_ERR(dent);
546 fail2:
547 __aa_fs_namespace_rmdir(ns);
549 return error;
553 #define list_entry_next(pos, member) \
554 list_entry(pos->member.next, typeof(*pos), member)
555 #define list_entry_is_head(pos, head, member) (&pos->member == (head))
558 * __next_namespace - find the next namespace to list
559 * @root: root namespace to stop search at (NOT NULL)
560 * @ns: current ns position (NOT NULL)
562 * Find the next namespace from @ns under @root and handle all locking needed
563 * while switching current namespace.
565 * Returns: next namespace or NULL if at last namespace under @root
566 * Requires: ns->parent->lock to be held
567 * NOTE: will not unlock root->lock
569 static struct aa_namespace *__next_namespace(struct aa_namespace *root,
570 struct aa_namespace *ns)
572 struct aa_namespace *parent, *next;
574 /* is next namespace a child */
575 if (!list_empty(&ns->sub_ns)) {
576 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
577 mutex_lock(&next->lock);
578 return next;
581 /* check if the next ns is a sibling, parent, gp, .. */
582 parent = ns->parent;
583 while (parent) {
584 mutex_unlock(&ns->lock);
585 next = list_entry_next(ns, base.list);
586 if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
587 mutex_lock(&next->lock);
588 return next;
590 if (parent == root)
591 return NULL;
592 ns = parent;
593 parent = parent->parent;
596 return NULL;
600 * __first_profile - find the first profile in a namespace
601 * @root: namespace that is root of profiles being displayed (NOT NULL)
602 * @ns: namespace to start in (NOT NULL)
604 * Returns: unrefcounted profile or NULL if no profile
605 * Requires: profile->ns.lock to be held
607 static struct aa_profile *__first_profile(struct aa_namespace *root,
608 struct aa_namespace *ns)
610 for (; ns; ns = __next_namespace(root, ns)) {
611 if (!list_empty(&ns->base.profiles))
612 return list_first_entry(&ns->base.profiles,
613 struct aa_profile, base.list);
615 return NULL;
619 * __next_profile - step to the next profile in a profile tree
620 * @profile: current profile in tree (NOT NULL)
622 * Perform a depth first traversal on the profile tree in a namespace
624 * Returns: next profile or NULL if done
625 * Requires: profile->ns.lock to be held
627 static struct aa_profile *__next_profile(struct aa_profile *p)
629 struct aa_profile *parent;
630 struct aa_namespace *ns = p->ns;
632 /* is next profile a child */
633 if (!list_empty(&p->base.profiles))
634 return list_first_entry(&p->base.profiles, typeof(*p),
635 base.list);
637 /* is next profile a sibling, parent sibling, gp, sibling, .. */
638 parent = rcu_dereference_protected(p->parent,
639 mutex_is_locked(&p->ns->lock));
640 while (parent) {
641 p = list_entry_next(p, base.list);
642 if (!list_entry_is_head(p, &parent->base.profiles, base.list))
643 return p;
644 p = parent;
645 parent = rcu_dereference_protected(parent->parent,
646 mutex_is_locked(&parent->ns->lock));
649 /* is next another profile in the namespace */
650 p = list_entry_next(p, base.list);
651 if (!list_entry_is_head(p, &ns->base.profiles, base.list))
652 return p;
654 return NULL;
658 * next_profile - step to the next profile in where ever it may be
659 * @root: root namespace (NOT NULL)
660 * @profile: current profile (NOT NULL)
662 * Returns: next profile or NULL if there isn't one
664 static struct aa_profile *next_profile(struct aa_namespace *root,
665 struct aa_profile *profile)
667 struct aa_profile *next = __next_profile(profile);
668 if (next)
669 return next;
671 /* finished all profiles in namespace move to next namespace */
672 return __first_profile(root, __next_namespace(root, profile->ns));
676 * p_start - start a depth first traversal of profile tree
677 * @f: seq_file to fill
678 * @pos: current position
680 * Returns: first profile under current namespace or NULL if none found
682 * acquires first ns->lock
684 static void *p_start(struct seq_file *f, loff_t *pos)
686 struct aa_profile *profile = NULL;
687 struct aa_namespace *root = aa_current_profile()->ns;
688 loff_t l = *pos;
689 f->private = aa_get_namespace(root);
692 /* find the first profile */
693 mutex_lock(&root->lock);
694 profile = __first_profile(root, root);
696 /* skip to position */
697 for (; profile && l > 0; l--)
698 profile = next_profile(root, profile);
700 return profile;
704 * p_next - read the next profile entry
705 * @f: seq_file to fill
706 * @p: profile previously returned
707 * @pos: current position
709 * Returns: next profile after @p or NULL if none
711 * may acquire/release locks in namespace tree as necessary
713 static void *p_next(struct seq_file *f, void *p, loff_t *pos)
715 struct aa_profile *profile = p;
716 struct aa_namespace *ns = f->private;
717 (*pos)++;
719 return next_profile(ns, profile);
723 * p_stop - stop depth first traversal
724 * @f: seq_file we are filling
725 * @p: the last profile writen
727 * Release all locking done by p_start/p_next on namespace tree
729 static void p_stop(struct seq_file *f, void *p)
731 struct aa_profile *profile = p;
732 struct aa_namespace *root = f->private, *ns;
734 if (profile) {
735 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
736 mutex_unlock(&ns->lock);
738 mutex_unlock(&root->lock);
739 aa_put_namespace(root);
743 * seq_show_profile - show a profile entry
744 * @f: seq_file to file
745 * @p: current position (profile) (NOT NULL)
747 * Returns: error on failure
749 static int seq_show_profile(struct seq_file *f, void *p)
751 struct aa_profile *profile = (struct aa_profile *)p;
752 struct aa_namespace *root = f->private;
754 if (profile->ns != root)
755 seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
756 seq_printf(f, "%s (%s)\n", profile->base.hname,
757 aa_profile_mode_names[profile->mode]);
759 return 0;
762 static const struct seq_operations aa_fs_profiles_op = {
763 .start = p_start,
764 .next = p_next,
765 .stop = p_stop,
766 .show = seq_show_profile,
769 static int profiles_open(struct inode *inode, struct file *file)
771 return seq_open(file, &aa_fs_profiles_op);
774 static int profiles_release(struct inode *inode, struct file *file)
776 return seq_release(inode, file);
779 static const struct file_operations aa_fs_profiles_fops = {
780 .open = profiles_open,
781 .read = seq_read,
782 .llseek = seq_lseek,
783 .release = profiles_release,
787 /** Base file system setup **/
788 static struct aa_fs_entry aa_fs_entry_file[] = {
789 AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
790 "link lock"),
794 static struct aa_fs_entry aa_fs_entry_domain[] = {
795 AA_FS_FILE_BOOLEAN("change_hat", 1),
796 AA_FS_FILE_BOOLEAN("change_hatv", 1),
797 AA_FS_FILE_BOOLEAN("change_onexec", 1),
798 AA_FS_FILE_BOOLEAN("change_profile", 1),
802 static struct aa_fs_entry aa_fs_entry_policy[] = {
803 AA_FS_FILE_BOOLEAN("set_load", 1),
807 static struct aa_fs_entry aa_fs_entry_features[] = {
808 AA_FS_DIR("policy", aa_fs_entry_policy),
809 AA_FS_DIR("domain", aa_fs_entry_domain),
810 AA_FS_DIR("file", aa_fs_entry_file),
811 AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
812 AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
813 AA_FS_DIR("caps", aa_fs_entry_caps),
817 static struct aa_fs_entry aa_fs_entry_apparmor[] = {
818 AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
819 AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
820 AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
821 AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
822 AA_FS_DIR("features", aa_fs_entry_features),
826 static struct aa_fs_entry aa_fs_entry =
827 AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
830 * aafs_create_file - create a file entry in the apparmor securityfs
831 * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
832 * @parent: the parent dentry in the securityfs
834 * Use aafs_remove_file to remove entries created with this fn.
836 static int __init aafs_create_file(struct aa_fs_entry *fs_file,
837 struct dentry *parent)
839 int error = 0;
841 fs_file->dentry = securityfs_create_file(fs_file->name,
842 S_IFREG | fs_file->mode,
843 parent, fs_file,
844 fs_file->file_ops);
845 if (IS_ERR(fs_file->dentry)) {
846 error = PTR_ERR(fs_file->dentry);
847 fs_file->dentry = NULL;
849 return error;
852 static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
854 * aafs_create_dir - recursively create a directory entry in the securityfs
855 * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
856 * @parent: the parent dentry in the securityfs
858 * Use aafs_remove_dir to remove entries created with this fn.
860 static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
861 struct dentry *parent)
863 struct aa_fs_entry *fs_file;
864 struct dentry *dir;
865 int error;
867 dir = securityfs_create_dir(fs_dir->name, parent);
868 if (IS_ERR(dir))
869 return PTR_ERR(dir);
870 fs_dir->dentry = dir;
872 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
873 if (fs_file->v_type == AA_FS_TYPE_DIR)
874 error = aafs_create_dir(fs_file, fs_dir->dentry);
875 else
876 error = aafs_create_file(fs_file, fs_dir->dentry);
877 if (error)
878 goto failed;
881 return 0;
883 failed:
884 aafs_remove_dir(fs_dir);
886 return error;
890 * aafs_remove_file - drop a single file entry in the apparmor securityfs
891 * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
893 static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
895 if (!fs_file->dentry)
896 return;
898 securityfs_remove(fs_file->dentry);
899 fs_file->dentry = NULL;
903 * aafs_remove_dir - recursively drop a directory entry from the securityfs
904 * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
906 static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
908 struct aa_fs_entry *fs_file;
910 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
911 if (fs_file->v_type == AA_FS_TYPE_DIR)
912 aafs_remove_dir(fs_file);
913 else
914 aafs_remove_file(fs_file);
917 aafs_remove_file(fs_dir);
921 * aa_destroy_aafs - cleanup and free aafs
923 * releases dentries allocated by aa_create_aafs
925 void __init aa_destroy_aafs(void)
927 aafs_remove_dir(&aa_fs_entry);
931 * aa_create_aafs - create the apparmor security filesystem
933 * dentries created here are released by aa_destroy_aafs
935 * Returns: error on failure
937 static int __init aa_create_aafs(void)
939 int error;
941 if (!apparmor_initialized)
942 return 0;
944 if (aa_fs_entry.dentry) {
945 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
946 return -EEXIST;
949 /* Populate fs tree. */
950 error = aafs_create_dir(&aa_fs_entry, NULL);
951 if (error)
952 goto error;
954 error = __aa_fs_namespace_mkdir(root_ns, aa_fs_entry.dentry,
955 "policy");
956 if (error)
957 goto error;
959 /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
961 /* Report that AppArmor fs is enabled */
962 aa_info_message("AppArmor Filesystem Enabled");
963 return 0;
965 error:
966 aa_destroy_aafs();
967 AA_ERROR("Error creating AppArmor securityfs\n");
968 return error;
971 fs_initcall(aa_create_aafs);