]> git.karo-electronics.de Git - karo-tx-linux.git/blob - security/apparmor/apparmorfs.c
apparmor: prepare to support newer versions of policy
[karo-tx-linux.git] / security / apparmor / apparmorfs.c
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
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>
24
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/policy_ns.h"
32 #include "include/resource.h"
33
34 /**
35  * aa_mangle_name - mangle a profile name to std profile layout form
36  * @name: profile name to mangle  (NOT NULL)
37  * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
38  *
39  * Returns: length of mangled name
40  */
41 static int mangle_name(const char *name, char *target)
42 {
43         char *t = target;
44
45         while (*name == '/' || *name == '.')
46                 name++;
47
48         if (target) {
49                 for (; *name; name++) {
50                         if (*name == '/')
51                                 *(t)++ = '.';
52                         else if (isspace(*name))
53                                 *(t)++ = '_';
54                         else if (isalnum(*name) || strchr("._-", *name))
55                                 *(t)++ = *name;
56                 }
57
58                 *t = 0;
59         } else {
60                 int len = 0;
61                 for (; *name; name++) {
62                         if (isalnum(*name) || isspace(*name) ||
63                             strchr("/._-", *name))
64                                 len++;
65                 }
66
67                 return len;
68         }
69
70         return t - target;
71 }
72
73 /**
74  * aa_simple_write_to_buffer - common routine for getting policy from user
75  * @op: operation doing the user buffer copy
76  * @userbuf: user buffer to copy data from  (NOT NULL)
77  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
78  * @copy_size: size of data to copy from user buffer
79  * @pos: position write is at in the file (NOT NULL)
80  *
81  * Returns: kernel buffer containing copy of user buffer data or an
82  *          ERR_PTR on failure.
83  */
84 static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
85                                        size_t alloc_size, size_t copy_size,
86                                        loff_t *pos)
87 {
88         char *data;
89
90         BUG_ON(copy_size > alloc_size);
91
92         if (*pos != 0)
93                 /* only writes from pos 0, that is complete writes */
94                 return ERR_PTR(-ESPIPE);
95
96         /*
97          * Don't allow profile load/replace/remove from profiles that don't
98          * have CAP_MAC_ADMIN
99          */
100         if (!aa_may_manage_policy(op))
101                 return ERR_PTR(-EACCES);
102
103         /* freed by caller to simple_write_to_buffer */
104         data = kvmalloc(alloc_size);
105         if (data == NULL)
106                 return ERR_PTR(-ENOMEM);
107
108         if (copy_from_user(data, userbuf, copy_size)) {
109                 kvfree(data);
110                 return ERR_PTR(-EFAULT);
111         }
112
113         return data;
114 }
115
116
117 /* .load file hook fn to load policy */
118 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
119                             loff_t *pos)
120 {
121         char *data;
122         ssize_t error;
123
124         data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
125
126         error = PTR_ERR(data);
127         if (!IS_ERR(data)) {
128                 error = aa_replace_profiles(__aa_current_profile()->ns, data,
129                                             size, PROF_ADD);
130                 kvfree(data);
131         }
132
133         return error;
134 }
135
136 static const struct file_operations aa_fs_profile_load = {
137         .write = profile_load,
138         .llseek = default_llseek,
139 };
140
141 /* .replace file hook fn to load and/or replace policy */
142 static ssize_t profile_replace(struct file *f, const char __user *buf,
143                                size_t size, loff_t *pos)
144 {
145         char *data;
146         ssize_t error;
147
148         data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
149         error = PTR_ERR(data);
150         if (!IS_ERR(data)) {
151                 error = aa_replace_profiles(__aa_current_profile()->ns, data,
152                                             size, PROF_REPLACE);
153                 kvfree(data);
154         }
155
156         return error;
157 }
158
159 static const struct file_operations aa_fs_profile_replace = {
160         .write = profile_replace,
161         .llseek = default_llseek,
162 };
163
164 /* .remove file hook fn to remove loaded policy */
165 static ssize_t profile_remove(struct file *f, const char __user *buf,
166                               size_t size, loff_t *pos)
167 {
168         char *data;
169         ssize_t error;
170
171         /*
172          * aa_remove_profile needs a null terminated string so 1 extra
173          * byte is allocated and the copied data is null terminated.
174          */
175         data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
176
177         error = PTR_ERR(data);
178         if (!IS_ERR(data)) {
179                 data[size] = 0;
180                 error = aa_remove_profiles(data, size);
181                 kvfree(data);
182         }
183
184         return error;
185 }
186
187 static const struct file_operations aa_fs_profile_remove = {
188         .write = profile_remove,
189         .llseek = default_llseek,
190 };
191
192 static int aa_fs_seq_show(struct seq_file *seq, void *v)
193 {
194         struct aa_fs_entry *fs_file = seq->private;
195
196         if (!fs_file)
197                 return 0;
198
199         switch (fs_file->v_type) {
200         case AA_FS_TYPE_BOOLEAN:
201                 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
202                 break;
203         case AA_FS_TYPE_STRING:
204                 seq_printf(seq, "%s\n", fs_file->v.string);
205                 break;
206         case AA_FS_TYPE_U64:
207                 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
208                 break;
209         default:
210                 /* Ignore unpritable entry types. */
211                 break;
212         }
213
214         return 0;
215 }
216
217 static int aa_fs_seq_open(struct inode *inode, struct file *file)
218 {
219         return single_open(file, aa_fs_seq_show, inode->i_private);
220 }
221
222 const struct file_operations aa_fs_seq_file_ops = {
223         .owner          = THIS_MODULE,
224         .open           = aa_fs_seq_open,
225         .read           = seq_read,
226         .llseek         = seq_lseek,
227         .release        = single_release,
228 };
229
230 static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
231                                   int (*show)(struct seq_file *, void *))
232 {
233         struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
234         int error = single_open(file, show, proxy);
235
236         if (error) {
237                 file->private_data = NULL;
238                 aa_put_proxy(proxy);
239         }
240
241         return error;
242 }
243
244 static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
245 {
246         struct seq_file *seq = (struct seq_file *) file->private_data;
247         if (seq)
248                 aa_put_proxy(seq->private);
249         return single_release(inode, file);
250 }
251
252 static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
253 {
254         struct aa_proxy *proxy = seq->private;
255         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
256         seq_printf(seq, "%s\n", profile->base.name);
257         aa_put_profile(profile);
258
259         return 0;
260 }
261
262 static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
263 {
264         return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
265 }
266
267 static const struct file_operations aa_fs_profname_fops = {
268         .owner          = THIS_MODULE,
269         .open           = aa_fs_seq_profname_open,
270         .read           = seq_read,
271         .llseek         = seq_lseek,
272         .release        = aa_fs_seq_profile_release,
273 };
274
275 static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
276 {
277         struct aa_proxy *proxy = seq->private;
278         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
279         seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
280         aa_put_profile(profile);
281
282         return 0;
283 }
284
285 static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
286 {
287         return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
288 }
289
290 static const struct file_operations aa_fs_profmode_fops = {
291         .owner          = THIS_MODULE,
292         .open           = aa_fs_seq_profmode_open,
293         .read           = seq_read,
294         .llseek         = seq_lseek,
295         .release        = aa_fs_seq_profile_release,
296 };
297
298 static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
299 {
300         struct aa_proxy *proxy = seq->private;
301         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
302         if (profile->attach)
303                 seq_printf(seq, "%s\n", profile->attach);
304         else if (profile->xmatch)
305                 seq_puts(seq, "<unknown>\n");
306         else
307                 seq_printf(seq, "%s\n", profile->base.name);
308         aa_put_profile(profile);
309
310         return 0;
311 }
312
313 static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
314 {
315         return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
316 }
317
318 static const struct file_operations aa_fs_profattach_fops = {
319         .owner          = THIS_MODULE,
320         .open           = aa_fs_seq_profattach_open,
321         .read           = seq_read,
322         .llseek         = seq_lseek,
323         .release        = aa_fs_seq_profile_release,
324 };
325
326 static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
327 {
328         struct aa_proxy *proxy = seq->private;
329         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
330         unsigned int i, size = aa_hash_size();
331
332         if (profile->hash) {
333                 for (i = 0; i < size; i++)
334                         seq_printf(seq, "%.2x", profile->hash[i]);
335                 seq_puts(seq, "\n");
336         }
337         aa_put_profile(profile);
338
339         return 0;
340 }
341
342 static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
343 {
344         return single_open(file, aa_fs_seq_hash_show, inode->i_private);
345 }
346
347 static const struct file_operations aa_fs_seq_hash_fops = {
348         .owner          = THIS_MODULE,
349         .open           = aa_fs_seq_hash_open,
350         .read           = seq_read,
351         .llseek         = seq_lseek,
352         .release        = single_release,
353 };
354
355 /** fns to setup dynamic per profile/namespace files **/
356 void __aa_fs_profile_rmdir(struct aa_profile *profile)
357 {
358         struct aa_profile *child;
359         int i;
360
361         if (!profile)
362                 return;
363
364         list_for_each_entry(child, &profile->base.profiles, base.list)
365                 __aa_fs_profile_rmdir(child);
366
367         for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
368                 struct aa_proxy *proxy;
369                 if (!profile->dents[i])
370                         continue;
371
372                 proxy = d_inode(profile->dents[i])->i_private;
373                 securityfs_remove(profile->dents[i]);
374                 aa_put_proxy(proxy);
375                 profile->dents[i] = NULL;
376         }
377 }
378
379 void __aa_fs_profile_migrate_dents(struct aa_profile *old,
380                                    struct aa_profile *new)
381 {
382         int i;
383
384         for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
385                 new->dents[i] = old->dents[i];
386                 if (new->dents[i])
387                         new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
388                 old->dents[i] = NULL;
389         }
390 }
391
392 static struct dentry *create_profile_file(struct dentry *dir, const char *name,
393                                           struct aa_profile *profile,
394                                           const struct file_operations *fops)
395 {
396         struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
397         struct dentry *dent;
398
399         dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
400         if (IS_ERR(dent))
401                 aa_put_proxy(proxy);
402
403         return dent;
404 }
405
406 /* requires lock be held */
407 int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
408 {
409         struct aa_profile *child;
410         struct dentry *dent = NULL, *dir;
411         int error;
412
413         if (!parent) {
414                 struct aa_profile *p;
415                 p = aa_deref_parent(profile);
416                 dent = prof_dir(p);
417                 /* adding to parent that previously didn't have children */
418                 dent = securityfs_create_dir("profiles", dent);
419                 if (IS_ERR(dent))
420                         goto fail;
421                 prof_child_dir(p) = parent = dent;
422         }
423
424         if (!profile->dirname) {
425                 int len, id_len;
426                 len = mangle_name(profile->base.name, NULL);
427                 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
428
429                 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
430                 if (!profile->dirname)
431                         goto fail;
432
433                 mangle_name(profile->base.name, profile->dirname);
434                 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
435         }
436
437         dent = securityfs_create_dir(profile->dirname, parent);
438         if (IS_ERR(dent))
439                 goto fail;
440         prof_dir(profile) = dir = dent;
441
442         dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
443         if (IS_ERR(dent))
444                 goto fail;
445         profile->dents[AAFS_PROF_NAME] = dent;
446
447         dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
448         if (IS_ERR(dent))
449                 goto fail;
450         profile->dents[AAFS_PROF_MODE] = dent;
451
452         dent = create_profile_file(dir, "attach", profile,
453                                    &aa_fs_profattach_fops);
454         if (IS_ERR(dent))
455                 goto fail;
456         profile->dents[AAFS_PROF_ATTACH] = dent;
457
458         if (profile->hash) {
459                 dent = create_profile_file(dir, "sha1", profile,
460                                            &aa_fs_seq_hash_fops);
461                 if (IS_ERR(dent))
462                         goto fail;
463                 profile->dents[AAFS_PROF_HASH] = dent;
464         }
465
466         list_for_each_entry(child, &profile->base.profiles, base.list) {
467                 error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
468                 if (error)
469                         goto fail2;
470         }
471
472         return 0;
473
474 fail:
475         error = PTR_ERR(dent);
476
477 fail2:
478         __aa_fs_profile_rmdir(profile);
479
480         return error;
481 }
482
483 void __aa_fs_ns_rmdir(struct aa_ns *ns)
484 {
485         struct aa_ns *sub;
486         struct aa_profile *child;
487         int i;
488
489         if (!ns)
490                 return;
491
492         list_for_each_entry(child, &ns->base.profiles, base.list)
493                 __aa_fs_profile_rmdir(child);
494
495         list_for_each_entry(sub, &ns->sub_ns, base.list) {
496                 mutex_lock(&sub->lock);
497                 __aa_fs_ns_rmdir(sub);
498                 mutex_unlock(&sub->lock);
499         }
500
501         for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
502                 securityfs_remove(ns->dents[i]);
503                 ns->dents[i] = NULL;
504         }
505 }
506
507 int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
508 {
509         struct aa_ns *sub;
510         struct aa_profile *child;
511         struct dentry *dent, *dir;
512         int error;
513
514         if (!name)
515                 name = ns->base.name;
516
517         dent = securityfs_create_dir(name, parent);
518         if (IS_ERR(dent))
519                 goto fail;
520         ns_dir(ns) = dir = dent;
521
522         dent = securityfs_create_dir("profiles", dir);
523         if (IS_ERR(dent))
524                 goto fail;
525         ns_subprofs_dir(ns) = dent;
526
527         dent = securityfs_create_dir("namespaces", dir);
528         if (IS_ERR(dent))
529                 goto fail;
530         ns_subns_dir(ns) = dent;
531
532         list_for_each_entry(child, &ns->base.profiles, base.list) {
533                 error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
534                 if (error)
535                         goto fail2;
536         }
537
538         list_for_each_entry(sub, &ns->sub_ns, base.list) {
539                 mutex_lock(&sub->lock);
540                 error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
541                 mutex_unlock(&sub->lock);
542                 if (error)
543                         goto fail2;
544         }
545
546         return 0;
547
548 fail:
549         error = PTR_ERR(dent);
550
551 fail2:
552         __aa_fs_ns_rmdir(ns);
553
554         return error;
555 }
556
557
558 #define list_entry_is_head(pos, head, member) (&pos->member == (head))
559
560 /**
561  * __next_ns - 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  */
572 static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
573 {
574         struct aa_ns *parent, *next;
575
576         /* is next namespace a child */
577         if (!list_empty(&ns->sub_ns)) {
578                 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
579                 mutex_lock(&next->lock);
580                 return next;
581         }
582
583         /* check if the next ns is a sibling, parent, gp, .. */
584         parent = ns->parent;
585         while (ns != root) {
586                 mutex_unlock(&ns->lock);
587                 next = list_next_entry(ns, base.list);
588                 if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
589                         mutex_lock(&next->lock);
590                         return next;
591                 }
592                 ns = parent;
593                 parent = parent->parent;
594         }
595
596         return NULL;
597 }
598
599 /**
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)
603  *
604  * Returns: unrefcounted profile or NULL if no profile
605  * Requires: profile->ns.lock to be held
606  */
607 static struct aa_profile *__first_profile(struct aa_ns *root,
608                                           struct aa_ns *ns)
609 {
610         for (; ns; ns = __next_ns(root, ns)) {
611                 if (!list_empty(&ns->base.profiles))
612                         return list_first_entry(&ns->base.profiles,
613                                                 struct aa_profile, base.list);
614         }
615         return NULL;
616 }
617
618 /**
619  * __next_profile - step to the next profile in a profile tree
620  * @profile: current profile in tree (NOT NULL)
621  *
622  * Perform a depth first traversal on the profile tree in a namespace
623  *
624  * Returns: next profile or NULL if done
625  * Requires: profile->ns.lock to be held
626  */
627 static struct aa_profile *__next_profile(struct aa_profile *p)
628 {
629         struct aa_profile *parent;
630         struct aa_ns *ns = p->ns;
631
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);
636
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_next_entry(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));
647         }
648
649         /* is next another profile in the namespace */
650         p = list_next_entry(p, base.list);
651         if (!list_entry_is_head(p, &ns->base.profiles, base.list))
652                 return p;
653
654         return NULL;
655 }
656
657 /**
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)
661  *
662  * Returns: next profile or NULL if there isn't one
663  */
664 static struct aa_profile *next_profile(struct aa_ns *root,
665                                        struct aa_profile *profile)
666 {
667         struct aa_profile *next = __next_profile(profile);
668         if (next)
669                 return next;
670
671         /* finished all profiles in namespace move to next namespace */
672         return __first_profile(root, __next_ns(root, profile->ns));
673 }
674
675 /**
676  * p_start - start a depth first traversal of profile tree
677  * @f: seq_file to fill
678  * @pos: current position
679  *
680  * Returns: first profile under current namespace or NULL if none found
681  *
682  * acquires first ns->lock
683  */
684 static void *p_start(struct seq_file *f, loff_t *pos)
685 {
686         struct aa_profile *profile = NULL;
687         struct aa_ns *root = aa_current_profile()->ns;
688         loff_t l = *pos;
689         f->private = aa_get_ns(root);
690
691
692         /* find the first profile */
693         mutex_lock(&root->lock);
694         profile = __first_profile(root, root);
695
696         /* skip to position */
697         for (; profile && l > 0; l--)
698                 profile = next_profile(root, profile);
699
700         return profile;
701 }
702
703 /**
704  * p_next - read the next profile entry
705  * @f: seq_file to fill
706  * @p: profile previously returned
707  * @pos: current position
708  *
709  * Returns: next profile after @p or NULL if none
710  *
711  * may acquire/release locks in namespace tree as necessary
712  */
713 static void *p_next(struct seq_file *f, void *p, loff_t *pos)
714 {
715         struct aa_profile *profile = p;
716         struct aa_ns *ns = f->private;
717         (*pos)++;
718
719         return next_profile(ns, profile);
720 }
721
722 /**
723  * p_stop - stop depth first traversal
724  * @f: seq_file we are filling
725  * @p: the last profile writen
726  *
727  * Release all locking done by p_start/p_next on namespace tree
728  */
729 static void p_stop(struct seq_file *f, void *p)
730 {
731         struct aa_profile *profile = p;
732         struct aa_ns *root = f->private, *ns;
733
734         if (profile) {
735                 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
736                         mutex_unlock(&ns->lock);
737         }
738         mutex_unlock(&root->lock);
739         aa_put_ns(root);
740 }
741
742 /**
743  * seq_show_profile - show a profile entry
744  * @f: seq_file to file
745  * @p: current position (profile)    (NOT NULL)
746  *
747  * Returns: error on failure
748  */
749 static int seq_show_profile(struct seq_file *f, void *p)
750 {
751         struct aa_profile *profile = (struct aa_profile *)p;
752         struct aa_ns *root = f->private;
753
754         if (profile->ns != root)
755                 seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
756         seq_printf(f, "%s (%s)\n", profile->base.hname,
757                    aa_profile_mode_names[profile->mode]);
758
759         return 0;
760 }
761
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,
767 };
768
769 static int profiles_open(struct inode *inode, struct file *file)
770 {
771         return seq_open(file, &aa_fs_profiles_op);
772 }
773
774 static int profiles_release(struct inode *inode, struct file *file)
775 {
776         return seq_release(inode, file);
777 }
778
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,
784 };
785
786
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"),
791         { }
792 };
793
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),
799         { }
800 };
801
802 static struct aa_fs_entry aa_fs_entry_versions[] = {
803         AA_FS_FILE_BOOLEAN("v5",        1),
804         { }
805 };
806
807 static struct aa_fs_entry aa_fs_entry_policy[] = {
808         AA_FS_DIR("versions",                   aa_fs_entry_versions),
809         AA_FS_FILE_BOOLEAN("set_load",          1),
810         { }
811 };
812
813 static struct aa_fs_entry aa_fs_entry_features[] = {
814         AA_FS_DIR("policy",                     aa_fs_entry_policy),
815         AA_FS_DIR("domain",                     aa_fs_entry_domain),
816         AA_FS_DIR("file",                       aa_fs_entry_file),
817         AA_FS_FILE_U64("capability",            VFS_CAP_FLAGS_MASK),
818         AA_FS_DIR("rlimit",                     aa_fs_entry_rlimit),
819         AA_FS_DIR("caps",                       aa_fs_entry_caps),
820         { }
821 };
822
823 static struct aa_fs_entry aa_fs_entry_apparmor[] = {
824         AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
825         AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
826         AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
827         AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
828         AA_FS_DIR("features", aa_fs_entry_features),
829         { }
830 };
831
832 static struct aa_fs_entry aa_fs_entry =
833         AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
834
835 /**
836  * aafs_create_file - create a file entry in the apparmor securityfs
837  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
838  * @parent: the parent dentry in the securityfs
839  *
840  * Use aafs_remove_file to remove entries created with this fn.
841  */
842 static int __init aafs_create_file(struct aa_fs_entry *fs_file,
843                                    struct dentry *parent)
844 {
845         int error = 0;
846
847         fs_file->dentry = securityfs_create_file(fs_file->name,
848                                                  S_IFREG | fs_file->mode,
849                                                  parent, fs_file,
850                                                  fs_file->file_ops);
851         if (IS_ERR(fs_file->dentry)) {
852                 error = PTR_ERR(fs_file->dentry);
853                 fs_file->dentry = NULL;
854         }
855         return error;
856 }
857
858 static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
859 /**
860  * aafs_create_dir - recursively create a directory entry in the securityfs
861  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
862  * @parent: the parent dentry in the securityfs
863  *
864  * Use aafs_remove_dir to remove entries created with this fn.
865  */
866 static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
867                                   struct dentry *parent)
868 {
869         struct aa_fs_entry *fs_file;
870         struct dentry *dir;
871         int error;
872
873         dir = securityfs_create_dir(fs_dir->name, parent);
874         if (IS_ERR(dir))
875                 return PTR_ERR(dir);
876         fs_dir->dentry = dir;
877
878         for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
879                 if (fs_file->v_type == AA_FS_TYPE_DIR)
880                         error = aafs_create_dir(fs_file, fs_dir->dentry);
881                 else
882                         error = aafs_create_file(fs_file, fs_dir->dentry);
883                 if (error)
884                         goto failed;
885         }
886
887         return 0;
888
889 failed:
890         aafs_remove_dir(fs_dir);
891
892         return error;
893 }
894
895 /**
896  * aafs_remove_file - drop a single file entry in the apparmor securityfs
897  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
898  */
899 static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
900 {
901         if (!fs_file->dentry)
902                 return;
903
904         securityfs_remove(fs_file->dentry);
905         fs_file->dentry = NULL;
906 }
907
908 /**
909  * aafs_remove_dir - recursively drop a directory entry from the securityfs
910  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
911  */
912 static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
913 {
914         struct aa_fs_entry *fs_file;
915
916         for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
917                 if (fs_file->v_type == AA_FS_TYPE_DIR)
918                         aafs_remove_dir(fs_file);
919                 else
920                         aafs_remove_file(fs_file);
921         }
922
923         aafs_remove_file(fs_dir);
924 }
925
926 /**
927  * aa_destroy_aafs - cleanup and free aafs
928  *
929  * releases dentries allocated by aa_create_aafs
930  */
931 void __init aa_destroy_aafs(void)
932 {
933         aafs_remove_dir(&aa_fs_entry);
934 }
935
936 /**
937  * aa_create_aafs - create the apparmor security filesystem
938  *
939  * dentries created here are released by aa_destroy_aafs
940  *
941  * Returns: error on failure
942  */
943 static int __init aa_create_aafs(void)
944 {
945         int error;
946
947         if (!apparmor_initialized)
948                 return 0;
949
950         if (aa_fs_entry.dentry) {
951                 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
952                 return -EEXIST;
953         }
954
955         /* Populate fs tree. */
956         error = aafs_create_dir(&aa_fs_entry, NULL);
957         if (error)
958                 goto error;
959
960         error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
961         if (error)
962                 goto error;
963
964         /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
965
966         /* Report that AppArmor fs is enabled */
967         aa_info_message("AppArmor Filesystem Enabled");
968         return 0;
969
970 error:
971         aa_destroy_aafs();
972         AA_ERROR("Error creating AppArmor securityfs\n");
973         return error;
974 }
975
976 fs_initcall(aa_create_aafs);