apparmor: remove unused op parameter from simple_write_to_buffer()
[platform/kernel/linux-exynos.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/mount.h>
22 #include <linux/namei.h>
23 #include <linux/capability.h>
24 #include <linux/rcupdate.h>
25 #include <uapi/linux/major.h>
26 #include <linux/fs.h>
27
28 #include "include/apparmor.h"
29 #include "include/apparmorfs.h"
30 #include "include/audit.h"
31 #include "include/context.h"
32 #include "include/crypto.h"
33 #include "include/policy.h"
34 #include "include/policy_ns.h"
35 #include "include/resource.h"
36 #include "include/policy_unpack.h"
37
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  */
45 static int mangle_name(const char *name, char *target)
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
77 /**
78  * aa_simple_write_to_buffer - common routine for getting policy from user
79  * @userbuf: user buffer to copy data from  (NOT NULL)
80  * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
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  */
87 static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
88                                                      size_t alloc_size,
89                                                      size_t copy_size,
90                                                      loff_t *pos)
91 {
92         struct aa_loaddata *data;
93
94         BUG_ON(copy_size > alloc_size);
95
96         if (*pos != 0)
97                 /* only writes from pos 0, that is complete writes */
98                 return ERR_PTR(-ESPIPE);
99
100         /* freed by caller to simple_write_to_buffer */
101         data = kvmalloc(sizeof(*data) + alloc_size);
102         if (data == NULL)
103                 return ERR_PTR(-ENOMEM);
104         kref_init(&data->count);
105         data->size = copy_size;
106         data->hash = NULL;
107         data->abi = 0;
108
109         if (copy_from_user(data->data, userbuf, copy_size)) {
110                 kvfree(data);
111                 return ERR_PTR(-EFAULT);
112         }
113
114         return data;
115 }
116
117 static ssize_t policy_update(int binop, const char __user *buf, size_t size,
118                              loff_t *pos, struct aa_ns *ns)
119 {
120         ssize_t error;
121         struct aa_loaddata *data;
122         struct aa_profile *profile = aa_current_profile();
123         const char *op = binop == PROF_ADD ? OP_PROF_LOAD : OP_PROF_REPL;
124         /* high level check about policy management - fine grained in
125          * below after unpack
126          */
127         error = aa_may_manage_policy(profile, ns, op);
128         if (error)
129                 return error;
130
131         data = aa_simple_write_to_buffer(buf, size, size, pos);
132         error = PTR_ERR(data);
133         if (!IS_ERR(data)) {
134                 error = aa_replace_profiles(ns ? ns : profile->ns, profile,
135                                             binop, data);
136                 aa_put_loaddata(data);
137         }
138
139         return error;
140 }
141
142 /* .load file hook fn to load policy */
143 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
144                             loff_t *pos)
145 {
146         struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
147         int error = policy_update(PROF_ADD, buf, size, pos, ns);
148
149         aa_put_ns(ns);
150
151         return error;
152 }
153
154 static const struct file_operations aa_fs_profile_load = {
155         .write = profile_load,
156         .llseek = default_llseek,
157 };
158
159 /* .replace file hook fn to load and/or replace policy */
160 static ssize_t profile_replace(struct file *f, const char __user *buf,
161                                size_t size, loff_t *pos)
162 {
163         struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
164         int error = policy_update(PROF_REPLACE, buf, size, pos, ns);
165
166         aa_put_ns(ns);
167
168         return error;
169 }
170
171 static const struct file_operations aa_fs_profile_replace = {
172         .write = profile_replace,
173         .llseek = default_llseek,
174 };
175
176 /* .remove file hook fn to remove loaded policy */
177 static ssize_t profile_remove(struct file *f, const char __user *buf,
178                               size_t size, loff_t *pos)
179 {
180         struct aa_loaddata *data;
181         struct aa_profile *profile;
182         ssize_t error;
183         struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
184
185         profile = aa_current_profile();
186         /* high level check about policy management - fine grained in
187          * below after unpack
188          */
189         error = aa_may_manage_policy(profile, ns, OP_PROF_RM);
190         if (error)
191                 goto out;
192
193         /*
194          * aa_remove_profile needs a null terminated string so 1 extra
195          * byte is allocated and the copied data is null terminated.
196          */
197         data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
198
199         error = PTR_ERR(data);
200         if (!IS_ERR(data)) {
201                 data->data[size] = 0;
202                 error = aa_remove_profiles(ns ? ns : profile->ns, profile,
203                                            data->data, size);
204                 aa_put_loaddata(data);
205         }
206  out:
207         aa_put_ns(ns);
208         return error;
209 }
210
211 static const struct file_operations aa_fs_profile_remove = {
212         .write = profile_remove,
213         .llseek = default_llseek,
214 };
215
216 static int aa_fs_seq_show(struct seq_file *seq, void *v)
217 {
218         struct aa_fs_entry *fs_file = seq->private;
219
220         if (!fs_file)
221                 return 0;
222
223         switch (fs_file->v_type) {
224         case AA_FS_TYPE_BOOLEAN:
225                 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
226                 break;
227         case AA_FS_TYPE_STRING:
228                 seq_printf(seq, "%s\n", fs_file->v.string);
229                 break;
230         case AA_FS_TYPE_U64:
231                 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
232                 break;
233         default:
234                 /* Ignore unpritable entry types. */
235                 break;
236         }
237
238         return 0;
239 }
240
241 static int aa_fs_seq_open(struct inode *inode, struct file *file)
242 {
243         return single_open(file, aa_fs_seq_show, inode->i_private);
244 }
245
246 const struct file_operations aa_fs_seq_file_ops = {
247         .owner          = THIS_MODULE,
248         .open           = aa_fs_seq_open,
249         .read           = seq_read,
250         .llseek         = seq_lseek,
251         .release        = single_release,
252 };
253
254 static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
255                                   int (*show)(struct seq_file *, void *))
256 {
257         struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
258         int error = single_open(file, show, proxy);
259
260         if (error) {
261                 file->private_data = NULL;
262                 aa_put_proxy(proxy);
263         }
264
265         return error;
266 }
267
268 static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
269 {
270         struct seq_file *seq = (struct seq_file *) file->private_data;
271         if (seq)
272                 aa_put_proxy(seq->private);
273         return single_release(inode, file);
274 }
275
276 static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
277 {
278         struct aa_proxy *proxy = seq->private;
279         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
280         seq_printf(seq, "%s\n", profile->base.name);
281         aa_put_profile(profile);
282
283         return 0;
284 }
285
286 static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
287 {
288         return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
289 }
290
291 static const struct file_operations aa_fs_profname_fops = {
292         .owner          = THIS_MODULE,
293         .open           = aa_fs_seq_profname_open,
294         .read           = seq_read,
295         .llseek         = seq_lseek,
296         .release        = aa_fs_seq_profile_release,
297 };
298
299 static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
300 {
301         struct aa_proxy *proxy = seq->private;
302         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
303         seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
304         aa_put_profile(profile);
305
306         return 0;
307 }
308
309 static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
310 {
311         return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
312 }
313
314 static const struct file_operations aa_fs_profmode_fops = {
315         .owner          = THIS_MODULE,
316         .open           = aa_fs_seq_profmode_open,
317         .read           = seq_read,
318         .llseek         = seq_lseek,
319         .release        = aa_fs_seq_profile_release,
320 };
321
322 static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
323 {
324         struct aa_proxy *proxy = seq->private;
325         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
326         if (profile->attach)
327                 seq_printf(seq, "%s\n", profile->attach);
328         else if (profile->xmatch)
329                 seq_puts(seq, "<unknown>\n");
330         else
331                 seq_printf(seq, "%s\n", profile->base.name);
332         aa_put_profile(profile);
333
334         return 0;
335 }
336
337 static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
338 {
339         return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
340 }
341
342 static const struct file_operations aa_fs_profattach_fops = {
343         .owner          = THIS_MODULE,
344         .open           = aa_fs_seq_profattach_open,
345         .read           = seq_read,
346         .llseek         = seq_lseek,
347         .release        = aa_fs_seq_profile_release,
348 };
349
350 static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
351 {
352         struct aa_proxy *proxy = seq->private;
353         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
354         unsigned int i, size = aa_hash_size();
355
356         if (profile->hash) {
357                 for (i = 0; i < size; i++)
358                         seq_printf(seq, "%.2x", profile->hash[i]);
359                 seq_puts(seq, "\n");
360         }
361         aa_put_profile(profile);
362
363         return 0;
364 }
365
366 static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
367 {
368         return single_open(file, aa_fs_seq_hash_show, inode->i_private);
369 }
370
371 static const struct file_operations aa_fs_seq_hash_fops = {
372         .owner          = THIS_MODULE,
373         .open           = aa_fs_seq_hash_open,
374         .read           = seq_read,
375         .llseek         = seq_lseek,
376         .release        = single_release,
377 };
378
379
380 static int aa_fs_seq_show_ns_level(struct seq_file *seq, void *v)
381 {
382         struct aa_ns *ns = aa_current_profile()->ns;
383
384         seq_printf(seq, "%d\n", ns->level);
385
386         return 0;
387 }
388
389 static int aa_fs_seq_open_ns_level(struct inode *inode, struct file *file)
390 {
391         return single_open(file, aa_fs_seq_show_ns_level, inode->i_private);
392 }
393
394 static const struct file_operations aa_fs_ns_level = {
395         .owner          = THIS_MODULE,
396         .open           = aa_fs_seq_open_ns_level,
397         .read           = seq_read,
398         .llseek         = seq_lseek,
399         .release        = single_release,
400 };
401
402 static int aa_fs_seq_show_ns_name(struct seq_file *seq, void *v)
403 {
404         struct aa_ns *ns = aa_current_profile()->ns;
405
406         seq_printf(seq, "%s\n", ns->base.name);
407
408         return 0;
409 }
410
411 static int aa_fs_seq_open_ns_name(struct inode *inode, struct file *file)
412 {
413         return single_open(file, aa_fs_seq_show_ns_name, inode->i_private);
414 }
415
416 static const struct file_operations aa_fs_ns_name = {
417         .owner          = THIS_MODULE,
418         .open           = aa_fs_seq_open_ns_name,
419         .read           = seq_read,
420         .llseek         = seq_lseek,
421         .release        = single_release,
422 };
423
424 static int rawdata_release(struct inode *inode, struct file *file)
425 {
426         /* TODO: switch to loaddata when profile switched to symlink */
427         aa_put_loaddata(file->private_data);
428
429         return 0;
430 }
431
432 static int aa_fs_seq_raw_abi_show(struct seq_file *seq, void *v)
433 {
434         struct aa_proxy *proxy = seq->private;
435         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
436
437         if (profile->rawdata->abi) {
438                 seq_printf(seq, "v%d", profile->rawdata->abi);
439                 seq_puts(seq, "\n");
440         }
441         aa_put_profile(profile);
442
443         return 0;
444 }
445
446 static int aa_fs_seq_raw_abi_open(struct inode *inode, struct file *file)
447 {
448         return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_abi_show);
449 }
450
451 static const struct file_operations aa_fs_seq_raw_abi_fops = {
452         .owner          = THIS_MODULE,
453         .open           = aa_fs_seq_raw_abi_open,
454         .read           = seq_read,
455         .llseek         = seq_lseek,
456         .release        = aa_fs_seq_profile_release,
457 };
458
459 static int aa_fs_seq_raw_hash_show(struct seq_file *seq, void *v)
460 {
461         struct aa_proxy *proxy = seq->private;
462         struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
463         unsigned int i, size = aa_hash_size();
464
465         if (profile->rawdata->hash) {
466                 for (i = 0; i < size; i++)
467                         seq_printf(seq, "%.2x", profile->rawdata->hash[i]);
468                 seq_puts(seq, "\n");
469         }
470         aa_put_profile(profile);
471
472         return 0;
473 }
474
475 static int aa_fs_seq_raw_hash_open(struct inode *inode, struct file *file)
476 {
477         return aa_fs_seq_profile_open(inode, file, aa_fs_seq_raw_hash_show);
478 }
479
480 static const struct file_operations aa_fs_seq_raw_hash_fops = {
481         .owner          = THIS_MODULE,
482         .open           = aa_fs_seq_raw_hash_open,
483         .read           = seq_read,
484         .llseek         = seq_lseek,
485         .release        = aa_fs_seq_profile_release,
486 };
487
488 static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
489                             loff_t *ppos)
490 {
491         struct aa_loaddata *rawdata = file->private_data;
492
493         return simple_read_from_buffer(buf, size, ppos, rawdata->data,
494                                        rawdata->size);
495 }
496
497 static int rawdata_open(struct inode *inode, struct file *file)
498 {
499         struct aa_proxy *proxy = inode->i_private;
500         struct aa_profile *profile;
501
502         if (!policy_view_capable(NULL))
503                 return -EACCES;
504         profile = aa_get_profile_rcu(&proxy->profile);
505         file->private_data = aa_get_loaddata(profile->rawdata);
506         aa_put_profile(profile);
507
508         return 0;
509 }
510
511 static const struct file_operations aa_fs_rawdata_fops = {
512         .open = rawdata_open,
513         .read = rawdata_read,
514         .llseek = generic_file_llseek,
515         .release = rawdata_release,
516 };
517
518 /** fns to setup dynamic per profile/namespace files **/
519 void __aa_fs_profile_rmdir(struct aa_profile *profile)
520 {
521         struct aa_profile *child;
522         int i;
523
524         if (!profile)
525                 return;
526
527         list_for_each_entry(child, &profile->base.profiles, base.list)
528                 __aa_fs_profile_rmdir(child);
529
530         for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
531                 struct aa_proxy *proxy;
532                 if (!profile->dents[i])
533                         continue;
534
535                 proxy = d_inode(profile->dents[i])->i_private;
536                 securityfs_remove(profile->dents[i]);
537                 aa_put_proxy(proxy);
538                 profile->dents[i] = NULL;
539         }
540 }
541
542 void __aa_fs_profile_migrate_dents(struct aa_profile *old,
543                                    struct aa_profile *new)
544 {
545         int i;
546
547         for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
548                 new->dents[i] = old->dents[i];
549                 if (new->dents[i])
550                         new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
551                 old->dents[i] = NULL;
552         }
553 }
554
555 static struct dentry *create_profile_file(struct dentry *dir, const char *name,
556                                           struct aa_profile *profile,
557                                           const struct file_operations *fops)
558 {
559         struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
560         struct dentry *dent;
561
562         dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
563         if (IS_ERR(dent))
564                 aa_put_proxy(proxy);
565
566         return dent;
567 }
568
569 /* requires lock be held */
570 int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
571 {
572         struct aa_profile *child;
573         struct dentry *dent = NULL, *dir;
574         int error;
575
576         if (!parent) {
577                 struct aa_profile *p;
578                 p = aa_deref_parent(profile);
579                 dent = prof_dir(p);
580                 /* adding to parent that previously didn't have children */
581                 dent = securityfs_create_dir("profiles", dent);
582                 if (IS_ERR(dent))
583                         goto fail;
584                 prof_child_dir(p) = parent = dent;
585         }
586
587         if (!profile->dirname) {
588                 int len, id_len;
589                 len = mangle_name(profile->base.name, NULL);
590                 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
591
592                 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
593                 if (!profile->dirname)
594                         goto fail;
595
596                 mangle_name(profile->base.name, profile->dirname);
597                 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
598         }
599
600         dent = securityfs_create_dir(profile->dirname, parent);
601         if (IS_ERR(dent))
602                 goto fail;
603         prof_dir(profile) = dir = dent;
604
605         dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
606         if (IS_ERR(dent))
607                 goto fail;
608         profile->dents[AAFS_PROF_NAME] = dent;
609
610         dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
611         if (IS_ERR(dent))
612                 goto fail;
613         profile->dents[AAFS_PROF_MODE] = dent;
614
615         dent = create_profile_file(dir, "attach", profile,
616                                    &aa_fs_profattach_fops);
617         if (IS_ERR(dent))
618                 goto fail;
619         profile->dents[AAFS_PROF_ATTACH] = dent;
620
621         if (profile->hash) {
622                 dent = create_profile_file(dir, "sha1", profile,
623                                            &aa_fs_seq_hash_fops);
624                 if (IS_ERR(dent))
625                         goto fail;
626                 profile->dents[AAFS_PROF_HASH] = dent;
627         }
628
629         if (profile->rawdata) {
630                 dent = create_profile_file(dir, "raw_sha1", profile,
631                                            &aa_fs_seq_raw_hash_fops);
632                 if (IS_ERR(dent))
633                         goto fail;
634                 profile->dents[AAFS_PROF_RAW_HASH] = dent;
635
636                 dent = create_profile_file(dir, "raw_abi", profile,
637                                            &aa_fs_seq_raw_abi_fops);
638                 if (IS_ERR(dent))
639                         goto fail;
640                 profile->dents[AAFS_PROF_RAW_ABI] = dent;
641
642                 dent = securityfs_create_file("raw_data", S_IFREG | 0444, dir,
643                                               profile->proxy,
644                                               &aa_fs_rawdata_fops);
645                 if (IS_ERR(dent))
646                         goto fail;
647                 profile->dents[AAFS_PROF_RAW_DATA] = dent;
648                 d_inode(dent)->i_size = profile->rawdata->size;
649                 aa_get_proxy(profile->proxy);
650         }
651
652         list_for_each_entry(child, &profile->base.profiles, base.list) {
653                 error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
654                 if (error)
655                         goto fail2;
656         }
657
658         return 0;
659
660 fail:
661         error = PTR_ERR(dent);
662
663 fail2:
664         __aa_fs_profile_rmdir(profile);
665
666         return error;
667 }
668
669 void __aa_fs_ns_rmdir(struct aa_ns *ns)
670 {
671         struct aa_ns *sub;
672         struct aa_profile *child;
673         int i;
674
675         if (!ns)
676                 return;
677
678         list_for_each_entry(child, &ns->base.profiles, base.list)
679                 __aa_fs_profile_rmdir(child);
680
681         list_for_each_entry(sub, &ns->sub_ns, base.list) {
682                 mutex_lock(&sub->lock);
683                 __aa_fs_ns_rmdir(sub);
684                 mutex_unlock(&sub->lock);
685         }
686
687         if (ns_subns_dir(ns)) {
688                 sub = d_inode(ns_subns_dir(ns))->i_private;
689                 aa_put_ns(sub);
690         }
691         if (ns_subload(ns)) {
692                 sub = d_inode(ns_subload(ns))->i_private;
693                 aa_put_ns(sub);
694         }
695         if (ns_subreplace(ns)) {
696                 sub = d_inode(ns_subreplace(ns))->i_private;
697                 aa_put_ns(sub);
698         }
699         if (ns_subremove(ns)) {
700                 sub = d_inode(ns_subremove(ns))->i_private;
701                 aa_put_ns(sub);
702         }
703
704         for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
705                 securityfs_remove(ns->dents[i]);
706                 ns->dents[i] = NULL;
707         }
708 }
709
710 /* assumes cleanup in caller */
711 static int __aa_fs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
712 {
713         struct dentry *dent;
714
715         AA_BUG(!ns);
716         AA_BUG(!dir);
717
718         dent = securityfs_create_dir("profiles", dir);
719         if (IS_ERR(dent))
720                 return PTR_ERR(dent);
721         ns_subprofs_dir(ns) = dent;
722
723         dent = securityfs_create_dir("raw_data", dir);
724         if (IS_ERR(dent))
725                 return PTR_ERR(dent);
726         ns_subdata_dir(ns) = dent;
727
728         dent = securityfs_create_file(".load", 0640, dir, ns,
729                                       &aa_fs_profile_load);
730         if (IS_ERR(dent))
731                 return PTR_ERR(dent);
732         aa_get_ns(ns);
733         ns_subload(ns) = dent;
734
735         dent = securityfs_create_file(".replace", 0640, dir, ns,
736                                       &aa_fs_profile_replace);
737         if (IS_ERR(dent))
738                 return PTR_ERR(dent);
739         aa_get_ns(ns);
740         ns_subreplace(ns) = dent;
741
742         dent = securityfs_create_file(".remove", 0640, dir, ns,
743                                       &aa_fs_profile_remove);
744         if (IS_ERR(dent))
745                 return PTR_ERR(dent);
746         aa_get_ns(ns);
747         ns_subremove(ns) = dent;
748
749         dent = securityfs_create_dir("namespaces", dir);
750         if (IS_ERR(dent))
751                 return PTR_ERR(dent);
752         aa_get_ns(ns);
753         ns_subns_dir(ns) = dent;
754
755         return 0;
756 }
757
758 int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
759 {
760         struct aa_ns *sub;
761         struct aa_profile *child;
762         struct dentry *dent, *dir;
763         int error;
764
765         AA_BUG(!ns);
766         AA_BUG(!parent);
767         AA_BUG(!mutex_is_locked(&ns->lock));
768
769         if (!name)
770                 name = ns->base.name;
771
772         /* create ns dir if it doesn't already exist */
773         dent = securityfs_create_dir(name, parent);
774         if (IS_ERR(dent))
775                 goto fail;
776
777         ns_dir(ns) = dir = dent;
778         error = __aa_fs_ns_mkdir_entries(ns, dir);
779         if (error)
780                 goto fail2;
781
782         /* profiles */
783         list_for_each_entry(child, &ns->base.profiles, base.list) {
784                 error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
785                 if (error)
786                         goto fail2;
787         }
788
789         /* subnamespaces */
790         list_for_each_entry(sub, &ns->sub_ns, base.list) {
791                 mutex_lock(&sub->lock);
792                 error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
793                 mutex_unlock(&sub->lock);
794                 if (error)
795                         goto fail2;
796         }
797
798         return 0;
799
800 fail:
801         error = PTR_ERR(dent);
802
803 fail2:
804         __aa_fs_ns_rmdir(ns);
805
806         return error;
807 }
808
809
810 #define list_entry_is_head(pos, head, member) (&pos->member == (head))
811
812 /**
813  * __next_ns - find the next namespace to list
814  * @root: root namespace to stop search at (NOT NULL)
815  * @ns: current ns position (NOT NULL)
816  *
817  * Find the next namespace from @ns under @root and handle all locking needed
818  * while switching current namespace.
819  *
820  * Returns: next namespace or NULL if at last namespace under @root
821  * Requires: ns->parent->lock to be held
822  * NOTE: will not unlock root->lock
823  */
824 static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
825 {
826         struct aa_ns *parent, *next;
827
828         /* is next namespace a child */
829         if (!list_empty(&ns->sub_ns)) {
830                 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
831                 mutex_lock(&next->lock);
832                 return next;
833         }
834
835         /* check if the next ns is a sibling, parent, gp, .. */
836         parent = ns->parent;
837         while (ns != root) {
838                 mutex_unlock(&ns->lock);
839                 next = list_next_entry(ns, base.list);
840                 if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
841                         mutex_lock(&next->lock);
842                         return next;
843                 }
844                 ns = parent;
845                 parent = parent->parent;
846         }
847
848         return NULL;
849 }
850
851 /**
852  * __first_profile - find the first profile in a namespace
853  * @root: namespace that is root of profiles being displayed (NOT NULL)
854  * @ns: namespace to start in   (NOT NULL)
855  *
856  * Returns: unrefcounted profile or NULL if no profile
857  * Requires: profile->ns.lock to be held
858  */
859 static struct aa_profile *__first_profile(struct aa_ns *root,
860                                           struct aa_ns *ns)
861 {
862         for (; ns; ns = __next_ns(root, ns)) {
863                 if (!list_empty(&ns->base.profiles))
864                         return list_first_entry(&ns->base.profiles,
865                                                 struct aa_profile, base.list);
866         }
867         return NULL;
868 }
869
870 /**
871  * __next_profile - step to the next profile in a profile tree
872  * @profile: current profile in tree (NOT NULL)
873  *
874  * Perform a depth first traversal on the profile tree in a namespace
875  *
876  * Returns: next profile or NULL if done
877  * Requires: profile->ns.lock to be held
878  */
879 static struct aa_profile *__next_profile(struct aa_profile *p)
880 {
881         struct aa_profile *parent;
882         struct aa_ns *ns = p->ns;
883
884         /* is next profile a child */
885         if (!list_empty(&p->base.profiles))
886                 return list_first_entry(&p->base.profiles, typeof(*p),
887                                         base.list);
888
889         /* is next profile a sibling, parent sibling, gp, sibling, .. */
890         parent = rcu_dereference_protected(p->parent,
891                                            mutex_is_locked(&p->ns->lock));
892         while (parent) {
893                 p = list_next_entry(p, base.list);
894                 if (!list_entry_is_head(p, &parent->base.profiles, base.list))
895                         return p;
896                 p = parent;
897                 parent = rcu_dereference_protected(parent->parent,
898                                             mutex_is_locked(&parent->ns->lock));
899         }
900
901         /* is next another profile in the namespace */
902         p = list_next_entry(p, base.list);
903         if (!list_entry_is_head(p, &ns->base.profiles, base.list))
904                 return p;
905
906         return NULL;
907 }
908
909 /**
910  * next_profile - step to the next profile in where ever it may be
911  * @root: root namespace  (NOT NULL)
912  * @profile: current profile  (NOT NULL)
913  *
914  * Returns: next profile or NULL if there isn't one
915  */
916 static struct aa_profile *next_profile(struct aa_ns *root,
917                                        struct aa_profile *profile)
918 {
919         struct aa_profile *next = __next_profile(profile);
920         if (next)
921                 return next;
922
923         /* finished all profiles in namespace move to next namespace */
924         return __first_profile(root, __next_ns(root, profile->ns));
925 }
926
927 /**
928  * p_start - start a depth first traversal of profile tree
929  * @f: seq_file to fill
930  * @pos: current position
931  *
932  * Returns: first profile under current namespace or NULL if none found
933  *
934  * acquires first ns->lock
935  */
936 static void *p_start(struct seq_file *f, loff_t *pos)
937 {
938         struct aa_profile *profile = NULL;
939         struct aa_ns *root = aa_current_profile()->ns;
940         loff_t l = *pos;
941         f->private = aa_get_ns(root);
942
943
944         /* find the first profile */
945         mutex_lock(&root->lock);
946         profile = __first_profile(root, root);
947
948         /* skip to position */
949         for (; profile && l > 0; l--)
950                 profile = next_profile(root, profile);
951
952         return profile;
953 }
954
955 /**
956  * p_next - read the next profile entry
957  * @f: seq_file to fill
958  * @p: profile previously returned
959  * @pos: current position
960  *
961  * Returns: next profile after @p or NULL if none
962  *
963  * may acquire/release locks in namespace tree as necessary
964  */
965 static void *p_next(struct seq_file *f, void *p, loff_t *pos)
966 {
967         struct aa_profile *profile = p;
968         struct aa_ns *ns = f->private;
969         (*pos)++;
970
971         return next_profile(ns, profile);
972 }
973
974 /**
975  * p_stop - stop depth first traversal
976  * @f: seq_file we are filling
977  * @p: the last profile writen
978  *
979  * Release all locking done by p_start/p_next on namespace tree
980  */
981 static void p_stop(struct seq_file *f, void *p)
982 {
983         struct aa_profile *profile = p;
984         struct aa_ns *root = f->private, *ns;
985
986         if (profile) {
987                 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
988                         mutex_unlock(&ns->lock);
989         }
990         mutex_unlock(&root->lock);
991         aa_put_ns(root);
992 }
993
994 /**
995  * seq_show_profile - show a profile entry
996  * @f: seq_file to file
997  * @p: current position (profile)    (NOT NULL)
998  *
999  * Returns: error on failure
1000  */
1001 static int seq_show_profile(struct seq_file *f, void *p)
1002 {
1003         struct aa_profile *profile = (struct aa_profile *)p;
1004         struct aa_ns *root = f->private;
1005
1006         if (profile->ns != root)
1007                 seq_printf(f, ":%s://", aa_ns_name(root, profile->ns, true));
1008         seq_printf(f, "%s (%s)\n", profile->base.hname,
1009                    aa_profile_mode_names[profile->mode]);
1010
1011         return 0;
1012 }
1013
1014 static const struct seq_operations aa_fs_profiles_op = {
1015         .start = p_start,
1016         .next = p_next,
1017         .stop = p_stop,
1018         .show = seq_show_profile,
1019 };
1020
1021 static int profiles_open(struct inode *inode, struct file *file)
1022 {
1023         if (!policy_view_capable(NULL))
1024                 return -EACCES;
1025
1026         return seq_open(file, &aa_fs_profiles_op);
1027 }
1028
1029 static int profiles_release(struct inode *inode, struct file *file)
1030 {
1031         return seq_release(inode, file);
1032 }
1033
1034 static const struct file_operations aa_fs_profiles_fops = {
1035         .open = profiles_open,
1036         .read = seq_read,
1037         .llseek = seq_lseek,
1038         .release = profiles_release,
1039 };
1040
1041
1042 /** Base file system setup **/
1043 static struct aa_fs_entry aa_fs_entry_file[] = {
1044         AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
1045                                   "link lock"),
1046         { }
1047 };
1048
1049 static struct aa_fs_entry aa_fs_entry_domain[] = {
1050         AA_FS_FILE_BOOLEAN("change_hat",        1),
1051         AA_FS_FILE_BOOLEAN("change_hatv",       1),
1052         AA_FS_FILE_BOOLEAN("change_onexec",     1),
1053         AA_FS_FILE_BOOLEAN("change_profile",    1),
1054         AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap",       1),
1055         { }
1056 };
1057
1058 static struct aa_fs_entry aa_fs_entry_versions[] = {
1059         AA_FS_FILE_BOOLEAN("v5",        1),
1060         { }
1061 };
1062
1063 static struct aa_fs_entry aa_fs_entry_policy[] = {
1064         AA_FS_DIR("versions",                   aa_fs_entry_versions),
1065         AA_FS_FILE_BOOLEAN("set_load",          1),
1066         { }
1067 };
1068
1069 static struct aa_fs_entry aa_fs_entry_features[] = {
1070         AA_FS_DIR("policy",                     aa_fs_entry_policy),
1071         AA_FS_DIR("domain",                     aa_fs_entry_domain),
1072         AA_FS_DIR("file",                       aa_fs_entry_file),
1073         AA_FS_FILE_U64("capability",            VFS_CAP_FLAGS_MASK),
1074         AA_FS_DIR("rlimit",                     aa_fs_entry_rlimit),
1075         AA_FS_DIR("caps",                       aa_fs_entry_caps),
1076         { }
1077 };
1078
1079 static struct aa_fs_entry aa_fs_entry_apparmor[] = {
1080         AA_FS_FILE_FOPS(".ns_level", 0666, &aa_fs_ns_level),
1081         AA_FS_FILE_FOPS(".ns_name", 0640, &aa_fs_ns_name),
1082         AA_FS_FILE_FOPS("profiles", 0440, &aa_fs_profiles_fops),
1083         AA_FS_DIR("features", aa_fs_entry_features),
1084         { }
1085 };
1086
1087 static struct aa_fs_entry aa_fs_entry =
1088         AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
1089
1090 /**
1091  * aafs_create_file - create a file entry in the apparmor securityfs
1092  * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
1093  * @parent: the parent dentry in the securityfs
1094  *
1095  * Use aafs_remove_file to remove entries created with this fn.
1096  */
1097 static int __init aafs_create_file(struct aa_fs_entry *fs_file,
1098                                    struct dentry *parent)
1099 {
1100         int error = 0;
1101
1102         fs_file->dentry = securityfs_create_file(fs_file->name,
1103                                                  S_IFREG | fs_file->mode,
1104                                                  parent, fs_file,
1105                                                  fs_file->file_ops);
1106         if (IS_ERR(fs_file->dentry)) {
1107                 error = PTR_ERR(fs_file->dentry);
1108                 fs_file->dentry = NULL;
1109         }
1110         return error;
1111 }
1112
1113 static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
1114 /**
1115  * aafs_create_dir - recursively create a directory entry in the securityfs
1116  * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
1117  * @parent: the parent dentry in the securityfs
1118  *
1119  * Use aafs_remove_dir to remove entries created with this fn.
1120  */
1121 static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
1122                                   struct dentry *parent)
1123 {
1124         struct aa_fs_entry *fs_file;
1125         struct dentry *dir;
1126         int error;
1127
1128         dir = securityfs_create_dir(fs_dir->name, parent);
1129         if (IS_ERR(dir))
1130                 return PTR_ERR(dir);
1131         fs_dir->dentry = dir;
1132
1133         for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
1134                 if (fs_file->v_type == AA_FS_TYPE_DIR)
1135                         error = aafs_create_dir(fs_file, fs_dir->dentry);
1136                 else
1137                         error = aafs_create_file(fs_file, fs_dir->dentry);
1138                 if (error)
1139                         goto failed;
1140         }
1141
1142         return 0;
1143
1144 failed:
1145         aafs_remove_dir(fs_dir);
1146
1147         return error;
1148 }
1149
1150 /**
1151  * aafs_remove_file - drop a single file entry in the apparmor securityfs
1152  * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
1153  */
1154 static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
1155 {
1156         if (!fs_file->dentry)
1157                 return;
1158
1159         securityfs_remove(fs_file->dentry);
1160         fs_file->dentry = NULL;
1161 }
1162
1163 /**
1164  * aafs_remove_dir - recursively drop a directory entry from the securityfs
1165  * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
1166  */
1167 static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
1168 {
1169         struct aa_fs_entry *fs_file;
1170
1171         for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
1172                 if (fs_file->v_type == AA_FS_TYPE_DIR)
1173                         aafs_remove_dir(fs_file);
1174                 else
1175                         aafs_remove_file(fs_file);
1176         }
1177
1178         aafs_remove_file(fs_dir);
1179 }
1180
1181 /**
1182  * aa_destroy_aafs - cleanup and free aafs
1183  *
1184  * releases dentries allocated by aa_create_aafs
1185  */
1186 void __init aa_destroy_aafs(void)
1187 {
1188         aafs_remove_dir(&aa_fs_entry);
1189 }
1190
1191
1192 #define NULL_FILE_NAME ".null"
1193 struct path aa_null;
1194
1195 static int aa_mk_null_file(struct dentry *parent)
1196 {
1197         struct vfsmount *mount = NULL;
1198         struct dentry *dentry;
1199         struct inode *inode;
1200         int count = 0;
1201         int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
1202
1203         if (error)
1204                 return error;
1205
1206         inode_lock(d_inode(parent));
1207         dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
1208         if (IS_ERR(dentry)) {
1209                 error = PTR_ERR(dentry);
1210                 goto out;
1211         }
1212         inode = new_inode(parent->d_inode->i_sb);
1213         if (!inode) {
1214                 error = -ENOMEM;
1215                 goto out1;
1216         }
1217
1218         inode->i_ino = get_next_ino();
1219         inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
1220         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1221         init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
1222                            MKDEV(MEM_MAJOR, 3));
1223         d_instantiate(dentry, inode);
1224         aa_null.dentry = dget(dentry);
1225         aa_null.mnt = mntget(mount);
1226
1227         error = 0;
1228
1229 out1:
1230         dput(dentry);
1231 out:
1232         inode_unlock(d_inode(parent));
1233         simple_release_fs(&mount, &count);
1234         return error;
1235 }
1236
1237 /**
1238  * aa_create_aafs - create the apparmor security filesystem
1239  *
1240  * dentries created here are released by aa_destroy_aafs
1241  *
1242  * Returns: error on failure
1243  */
1244 static int __init aa_create_aafs(void)
1245 {
1246         struct dentry *dent;
1247         int error;
1248
1249         if (!apparmor_initialized)
1250                 return 0;
1251
1252         if (aa_fs_entry.dentry) {
1253                 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
1254                 return -EEXIST;
1255         }
1256
1257         /* Populate fs tree. */
1258         error = aafs_create_dir(&aa_fs_entry, NULL);
1259         if (error)
1260                 goto error;
1261
1262         dent = securityfs_create_file(".load", 0666, aa_fs_entry.dentry,
1263                                       NULL, &aa_fs_profile_load);
1264         if (IS_ERR(dent)) {
1265                 error = PTR_ERR(dent);
1266                 goto error;
1267         }
1268         ns_subload(root_ns) = dent;
1269
1270         dent = securityfs_create_file(".replace", 0666, aa_fs_entry.dentry,
1271                                       NULL, &aa_fs_profile_replace);
1272         if (IS_ERR(dent)) {
1273                 error = PTR_ERR(dent);
1274                 goto error;
1275         }
1276         ns_subreplace(root_ns) = dent;
1277
1278         dent = securityfs_create_file(".remove", 0666, aa_fs_entry.dentry,
1279                                       NULL, &aa_fs_profile_remove);
1280         if (IS_ERR(dent)) {
1281                 error = PTR_ERR(dent);
1282                 goto error;
1283         }
1284         ns_subremove(root_ns) = dent;
1285
1286         mutex_lock(&root_ns->lock);
1287         error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
1288         mutex_unlock(&root_ns->lock);
1289
1290         if (error)
1291                 goto error;
1292
1293         error = aa_mk_null_file(aa_fs_entry.dentry);
1294         if (error)
1295                 goto error;
1296
1297         /* TODO: add default profile to apparmorfs */
1298
1299         /* Report that AppArmor fs is enabled */
1300         aa_info_message("AppArmor Filesystem Enabled");
1301         return 0;
1302
1303 error:
1304         aa_destroy_aafs();
1305         AA_ERROR("Error creating AppArmor securityfs\n");
1306         return error;
1307 }
1308
1309 fs_initcall(aa_create_aafs);