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
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/policy_ns.h"
32 #include "include/resource.h"
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)
39 * Returns: length of mangled name
41 static int mangle_name(const char *name, char *target)
45 while (*name == '/' || *name == '.')
49 for (; *name; name++) {
52 else if (isspace(*name))
54 else if (isalnum(*name) || strchr("._-", *name))
61 for (; *name; name++) {
62 if (isalnum(*name) || isspace(*name) ||
63 strchr("/._-", *name))
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)
81 * Returns: kernel buffer containing copy of user buffer data or an
84 static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
85 size_t alloc_size, size_t copy_size,
90 BUG_ON(copy_size > alloc_size);
93 /* only writes from pos 0, that is complete writes */
94 return ERR_PTR(-ESPIPE);
97 * Don't allow profile load/replace/remove from profiles that don't
100 if (!aa_may_manage_policy(op))
101 return ERR_PTR(-EACCES);
103 /* freed by caller to simple_write_to_buffer */
104 data = kvmalloc(alloc_size);
106 return ERR_PTR(-ENOMEM);
108 if (copy_from_user(data, userbuf, copy_size)) {
110 return ERR_PTR(-EFAULT);
117 /* .load file hook fn to load policy */
118 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
124 data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
126 error = PTR_ERR(data);
128 error = aa_replace_profiles(__aa_current_profile()->ns, data,
136 static const struct file_operations aa_fs_profile_load = {
137 .write = profile_load,
138 .llseek = default_llseek,
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)
148 data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
149 error = PTR_ERR(data);
151 error = aa_replace_profiles(__aa_current_profile()->ns, data,
159 static const struct file_operations aa_fs_profile_replace = {
160 .write = profile_replace,
161 .llseek = default_llseek,
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)
172 * aa_remove_profile needs a null terminated string so 1 extra
173 * byte is allocated and the copied data is null terminated.
175 data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
177 error = PTR_ERR(data);
180 error = aa_remove_profiles(data, size);
187 static const struct file_operations aa_fs_profile_remove = {
188 .write = profile_remove,
189 .llseek = default_llseek,
192 static int aa_fs_seq_show(struct seq_file *seq, void *v)
194 struct aa_fs_entry *fs_file = seq->private;
199 switch (fs_file->v_type) {
200 case AA_FS_TYPE_BOOLEAN:
201 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
203 case AA_FS_TYPE_STRING:
204 seq_printf(seq, "%s\n", fs_file->v.string);
207 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
210 /* Ignore unpritable entry types. */
217 static int aa_fs_seq_open(struct inode *inode, struct file *file)
219 return single_open(file, aa_fs_seq_show, inode->i_private);
222 const struct file_operations aa_fs_seq_file_ops = {
223 .owner = THIS_MODULE,
224 .open = aa_fs_seq_open,
227 .release = single_release,
230 static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
231 int (*show)(struct seq_file *, void *))
233 struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
234 int error = single_open(file, show, proxy);
237 file->private_data = NULL;
244 static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
246 struct seq_file *seq = (struct seq_file *) file->private_data;
248 aa_put_proxy(seq->private);
249 return single_release(inode, file);
252 static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
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);
262 static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
264 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
267 static const struct file_operations aa_fs_profname_fops = {
268 .owner = THIS_MODULE,
269 .open = aa_fs_seq_profname_open,
272 .release = aa_fs_seq_profile_release,
275 static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
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);
285 static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
287 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
290 static const struct file_operations aa_fs_profmode_fops = {
291 .owner = THIS_MODULE,
292 .open = aa_fs_seq_profmode_open,
295 .release = aa_fs_seq_profile_release,
298 static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
300 struct aa_proxy *proxy = seq->private;
301 struct aa_profile *profile = aa_get_profile_rcu(&proxy->profile);
303 seq_printf(seq, "%s\n", profile->attach);
304 else if (profile->xmatch)
305 seq_puts(seq, "<unknown>\n");
307 seq_printf(seq, "%s\n", profile->base.name);
308 aa_put_profile(profile);
313 static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
315 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
318 static const struct file_operations aa_fs_profattach_fops = {
319 .owner = THIS_MODULE,
320 .open = aa_fs_seq_profattach_open,
323 .release = aa_fs_seq_profile_release,
326 static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
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();
333 for (i = 0; i < size; i++)
334 seq_printf(seq, "%.2x", profile->hash[i]);
337 aa_put_profile(profile);
342 static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
344 return single_open(file, aa_fs_seq_hash_show, inode->i_private);
347 static const struct file_operations aa_fs_seq_hash_fops = {
348 .owner = THIS_MODULE,
349 .open = aa_fs_seq_hash_open,
352 .release = single_release,
355 /** fns to setup dynamic per profile/namespace files **/
356 void __aa_fs_profile_rmdir(struct aa_profile *profile)
358 struct aa_profile *child;
364 list_for_each_entry(child, &profile->base.profiles, base.list)
365 __aa_fs_profile_rmdir(child);
367 for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
368 struct aa_proxy *proxy;
369 if (!profile->dents[i])
372 proxy = d_inode(profile->dents[i])->i_private;
373 securityfs_remove(profile->dents[i]);
375 profile->dents[i] = NULL;
379 void __aa_fs_profile_migrate_dents(struct aa_profile *old,
380 struct aa_profile *new)
384 for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
385 new->dents[i] = old->dents[i];
387 new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
388 old->dents[i] = NULL;
392 static struct dentry *create_profile_file(struct dentry *dir, const char *name,
393 struct aa_profile *profile,
394 const struct file_operations *fops)
396 struct aa_proxy *proxy = aa_get_proxy(profile->proxy);
399 dent = securityfs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
406 /* requires lock be held */
407 int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
409 struct aa_profile *child;
410 struct dentry *dent = NULL, *dir;
414 struct aa_profile *p;
415 p = aa_deref_parent(profile);
417 /* adding to parent that previously didn't have children */
418 dent = securityfs_create_dir("profiles", dent);
421 prof_child_dir(p) = parent = dent;
424 if (!profile->dirname) {
426 len = mangle_name(profile->base.name, NULL);
427 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
429 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
430 if (!profile->dirname)
433 mangle_name(profile->base.name, profile->dirname);
434 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
437 dent = securityfs_create_dir(profile->dirname, parent);
440 prof_dir(profile) = dir = dent;
442 dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
445 profile->dents[AAFS_PROF_NAME] = dent;
447 dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
450 profile->dents[AAFS_PROF_MODE] = dent;
452 dent = create_profile_file(dir, "attach", profile,
453 &aa_fs_profattach_fops);
456 profile->dents[AAFS_PROF_ATTACH] = dent;
459 dent = create_profile_file(dir, "sha1", profile,
460 &aa_fs_seq_hash_fops);
463 profile->dents[AAFS_PROF_HASH] = dent;
466 list_for_each_entry(child, &profile->base.profiles, base.list) {
467 error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
475 error = PTR_ERR(dent);
478 __aa_fs_profile_rmdir(profile);
483 void __aa_fs_ns_rmdir(struct aa_ns *ns)
486 struct aa_profile *child;
492 list_for_each_entry(child, &ns->base.profiles, base.list)
493 __aa_fs_profile_rmdir(child);
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);
501 for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
502 securityfs_remove(ns->dents[i]);
507 int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
510 struct aa_profile *child;
511 struct dentry *dent, *dir;
515 name = ns->base.name;
517 dent = securityfs_create_dir(name, parent);
520 ns_dir(ns) = dir = dent;
522 dent = securityfs_create_dir("profiles", dir);
525 ns_subprofs_dir(ns) = dent;
527 dent = securityfs_create_dir("namespaces", dir);
530 ns_subns_dir(ns) = dent;
532 list_for_each_entry(child, &ns->base.profiles, base.list) {
533 error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
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);
549 error = PTR_ERR(dent);
552 __aa_fs_ns_rmdir(ns);
558 #define list_entry_is_head(pos, head, member) (&pos->member == (head))
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)
565 * Find the next namespace from @ns under @root and handle all locking needed
566 * while switching current namespace.
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
572 static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
574 struct aa_ns *parent, *next;
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);
583 /* check if the next ns is a sibling, parent, gp, .. */
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);
593 parent = parent->parent;
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_ns *root,
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);
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_ns *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),
637 /* is next profile a sibling, parent sibling, gp, sibling, .. */
638 parent = rcu_dereference_protected(p->parent,
639 mutex_is_locked(&p->ns->lock));
641 p = list_next_entry(p, base.list);
642 if (!list_entry_is_head(p, &parent->base.profiles, base.list))
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_next_entry(p, base.list);
651 if (!list_entry_is_head(p, &ns->base.profiles, base.list))
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_ns *root,
665 struct aa_profile *profile)
667 struct aa_profile *next = __next_profile(profile);
671 /* finished all profiles in namespace move to next namespace */
672 return __first_profile(root, __next_ns(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_ns *root = aa_current_profile()->ns;
689 f->private = aa_get_ns(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);
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_ns *ns = f->private;
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_ns *root = f->private, *ns;
735 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
736 mutex_unlock(&ns->lock);
738 mutex_unlock(&root->lock);
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_ns *root = f->private;
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]);
762 static const struct seq_operations aa_fs_profiles_op = {
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,
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 " \
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 AA_FS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1),
803 static struct aa_fs_entry aa_fs_entry_versions[] = {
804 AA_FS_FILE_BOOLEAN("v5", 1),
808 static struct aa_fs_entry aa_fs_entry_policy[] = {
809 AA_FS_DIR("versions", aa_fs_entry_versions),
810 AA_FS_FILE_BOOLEAN("set_load", 1),
814 static struct aa_fs_entry aa_fs_entry_features[] = {
815 AA_FS_DIR("policy", aa_fs_entry_policy),
816 AA_FS_DIR("domain", aa_fs_entry_domain),
817 AA_FS_DIR("file", aa_fs_entry_file),
818 AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
819 AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
820 AA_FS_DIR("caps", aa_fs_entry_caps),
824 static struct aa_fs_entry aa_fs_entry_apparmor[] = {
825 AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
826 AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
827 AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
828 AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
829 AA_FS_DIR("features", aa_fs_entry_features),
833 static struct aa_fs_entry aa_fs_entry =
834 AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
837 * aafs_create_file - create a file entry in the apparmor securityfs
838 * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
839 * @parent: the parent dentry in the securityfs
841 * Use aafs_remove_file to remove entries created with this fn.
843 static int __init aafs_create_file(struct aa_fs_entry *fs_file,
844 struct dentry *parent)
848 fs_file->dentry = securityfs_create_file(fs_file->name,
849 S_IFREG | fs_file->mode,
852 if (IS_ERR(fs_file->dentry)) {
853 error = PTR_ERR(fs_file->dentry);
854 fs_file->dentry = NULL;
859 static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
861 * aafs_create_dir - recursively create a directory entry in the securityfs
862 * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
863 * @parent: the parent dentry in the securityfs
865 * Use aafs_remove_dir to remove entries created with this fn.
867 static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
868 struct dentry *parent)
870 struct aa_fs_entry *fs_file;
874 dir = securityfs_create_dir(fs_dir->name, parent);
877 fs_dir->dentry = dir;
879 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
880 if (fs_file->v_type == AA_FS_TYPE_DIR)
881 error = aafs_create_dir(fs_file, fs_dir->dentry);
883 error = aafs_create_file(fs_file, fs_dir->dentry);
891 aafs_remove_dir(fs_dir);
897 * aafs_remove_file - drop a single file entry in the apparmor securityfs
898 * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
900 static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
902 if (!fs_file->dentry)
905 securityfs_remove(fs_file->dentry);
906 fs_file->dentry = NULL;
910 * aafs_remove_dir - recursively drop a directory entry from the securityfs
911 * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
913 static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
915 struct aa_fs_entry *fs_file;
917 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
918 if (fs_file->v_type == AA_FS_TYPE_DIR)
919 aafs_remove_dir(fs_file);
921 aafs_remove_file(fs_file);
924 aafs_remove_file(fs_dir);
928 * aa_destroy_aafs - cleanup and free aafs
930 * releases dentries allocated by aa_create_aafs
932 void __init aa_destroy_aafs(void)
934 aafs_remove_dir(&aa_fs_entry);
938 * aa_create_aafs - create the apparmor security filesystem
940 * dentries created here are released by aa_destroy_aafs
942 * Returns: error on failure
944 static int __init aa_create_aafs(void)
948 if (!apparmor_initialized)
951 if (aa_fs_entry.dentry) {
952 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
956 /* Populate fs tree. */
957 error = aafs_create_dir(&aa_fs_entry, NULL);
961 error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
965 /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
967 /* Report that AppArmor fs is enabled */
968 aa_info_message("AppArmor Filesystem Enabled");
973 AA_ERROR("Error creating AppArmor securityfs\n");
977 fs_initcall(aa_create_aafs);