Linux 5.7.7
[linux/fpc-iii.git] / security / apparmor / apparmorfs.c
blobf6a3ecfadf8054d5f3a1142c205040714a0e58c2
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AppArmor security module
5 * This file contains AppArmor /sys/kernel/security/apparmor interface functions
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
9 */
11 #include <linux/ctype.h>
12 #include <linux/security.h>
13 #include <linux/vmalloc.h>
14 #include <linux/init.h>
15 #include <linux/seq_file.h>
16 #include <linux/uaccess.h>
17 #include <linux/mount.h>
18 #include <linux/namei.h>
19 #include <linux/capability.h>
20 #include <linux/rcupdate.h>
21 #include <linux/fs.h>
22 #include <linux/fs_context.h>
23 #include <linux/poll.h>
24 #include <linux/zlib.h>
25 #include <uapi/linux/major.h>
26 #include <uapi/linux/magic.h>
28 #include "include/apparmor.h"
29 #include "include/apparmorfs.h"
30 #include "include/audit.h"
31 #include "include/cred.h"
32 #include "include/crypto.h"
33 #include "include/ipc.h"
34 #include "include/label.h"
35 #include "include/policy.h"
36 #include "include/policy_ns.h"
37 #include "include/resource.h"
38 #include "include/policy_unpack.h"
41 * The apparmor filesystem interface used for policy load and introspection
42 * The interface is split into two main components based on their function
43 * a securityfs component:
44 * used for static files that are always available, and which allows
45 * userspace to specificy the location of the security filesystem.
47 * fns and data are prefixed with
48 * aa_sfs_
50 * an apparmorfs component:
51 * used loaded policy content and introspection. It is not part of a
52 * regular mounted filesystem and is available only through the magic
53 * policy symlink in the root of the securityfs apparmor/ directory.
54 * Tasks queries will be magically redirected to the correct portion
55 * of the policy tree based on their confinement.
57 * fns and data are prefixed with
58 * aafs_
60 * The aa_fs_ prefix is used to indicate the fn is used by both the
61 * securityfs and apparmorfs filesystems.
66 * support fns
69 struct rawdata_f_data {
70 struct aa_loaddata *loaddata;
73 #define RAWDATA_F_DATA_BUF(p) (char *)(p + 1)
75 static void rawdata_f_data_free(struct rawdata_f_data *private)
77 if (!private)
78 return;
80 aa_put_loaddata(private->loaddata);
81 kvfree(private);
84 static struct rawdata_f_data *rawdata_f_data_alloc(size_t size)
86 struct rawdata_f_data *ret;
88 if (size > SIZE_MAX - sizeof(*ret))
89 return ERR_PTR(-EINVAL);
91 ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL);
92 if (!ret)
93 return ERR_PTR(-ENOMEM);
95 return ret;
98 /**
99 * aa_mangle_name - mangle a profile name to std profile layout form
100 * @name: profile name to mangle (NOT NULL)
101 * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
103 * Returns: length of mangled name
105 static int mangle_name(const char *name, char *target)
107 char *t = target;
109 while (*name == '/' || *name == '.')
110 name++;
112 if (target) {
113 for (; *name; name++) {
114 if (*name == '/')
115 *(t)++ = '.';
116 else if (isspace(*name))
117 *(t)++ = '_';
118 else if (isalnum(*name) || strchr("._-", *name))
119 *(t)++ = *name;
122 *t = 0;
123 } else {
124 int len = 0;
125 for (; *name; name++) {
126 if (isalnum(*name) || isspace(*name) ||
127 strchr("/._-", *name))
128 len++;
131 return len;
134 return t - target;
139 * aafs - core fns and data for the policy tree
142 #define AAFS_NAME "apparmorfs"
143 static struct vfsmount *aafs_mnt;
144 static int aafs_count;
147 static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
149 seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino);
150 return 0;
153 static void aafs_free_inode(struct inode *inode)
155 if (S_ISLNK(inode->i_mode))
156 kfree(inode->i_link);
157 free_inode_nonrcu(inode);
160 static const struct super_operations aafs_super_ops = {
161 .statfs = simple_statfs,
162 .free_inode = aafs_free_inode,
163 .show_path = aafs_show_path,
166 static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)
168 static struct tree_descr files[] = { {""} };
169 int error;
171 error = simple_fill_super(sb, AAFS_MAGIC, files);
172 if (error)
173 return error;
174 sb->s_op = &aafs_super_ops;
176 return 0;
179 static int apparmorfs_get_tree(struct fs_context *fc)
181 return get_tree_single(fc, apparmorfs_fill_super);
184 static const struct fs_context_operations apparmorfs_context_ops = {
185 .get_tree = apparmorfs_get_tree,
188 static int apparmorfs_init_fs_context(struct fs_context *fc)
190 fc->ops = &apparmorfs_context_ops;
191 return 0;
194 static struct file_system_type aafs_ops = {
195 .owner = THIS_MODULE,
196 .name = AAFS_NAME,
197 .init_fs_context = apparmorfs_init_fs_context,
198 .kill_sb = kill_anon_super,
202 * __aafs_setup_d_inode - basic inode setup for apparmorfs
203 * @dir: parent directory for the dentry
204 * @dentry: dentry we are seting the inode up for
205 * @mode: permissions the file should have
206 * @data: data to store on inode.i_private, available in open()
207 * @link: if symlink, symlink target string
208 * @fops: struct file_operations that should be used
209 * @iops: struct of inode_operations that should be used
211 static int __aafs_setup_d_inode(struct inode *dir, struct dentry *dentry,
212 umode_t mode, void *data, char *link,
213 const struct file_operations *fops,
214 const struct inode_operations *iops)
216 struct inode *inode = new_inode(dir->i_sb);
218 AA_BUG(!dir);
219 AA_BUG(!dentry);
221 if (!inode)
222 return -ENOMEM;
224 inode->i_ino = get_next_ino();
225 inode->i_mode = mode;
226 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
227 inode->i_private = data;
228 if (S_ISDIR(mode)) {
229 inode->i_op = iops ? iops : &simple_dir_inode_operations;
230 inode->i_fop = &simple_dir_operations;
231 inc_nlink(inode);
232 inc_nlink(dir);
233 } else if (S_ISLNK(mode)) {
234 inode->i_op = iops ? iops : &simple_symlink_inode_operations;
235 inode->i_link = link;
236 } else {
237 inode->i_fop = fops;
239 d_instantiate(dentry, inode);
240 dget(dentry);
242 return 0;
246 * aafs_create - create a dentry in the apparmorfs filesystem
248 * @name: name of dentry to create
249 * @mode: permissions the file should have
250 * @parent: parent directory for this dentry
251 * @data: data to store on inode.i_private, available in open()
252 * @link: if symlink, symlink target string
253 * @fops: struct file_operations that should be used for
254 * @iops: struct of inode_operations that should be used
256 * This is the basic "create a xxx" function for apparmorfs.
258 * Returns a pointer to a dentry if it succeeds, that must be free with
259 * aafs_remove(). Will return ERR_PTR on failure.
261 static struct dentry *aafs_create(const char *name, umode_t mode,
262 struct dentry *parent, void *data, void *link,
263 const struct file_operations *fops,
264 const struct inode_operations *iops)
266 struct dentry *dentry;
267 struct inode *dir;
268 int error;
270 AA_BUG(!name);
271 AA_BUG(!parent);
273 if (!(mode & S_IFMT))
274 mode = (mode & S_IALLUGO) | S_IFREG;
276 error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
277 if (error)
278 return ERR_PTR(error);
280 dir = d_inode(parent);
282 inode_lock(dir);
283 dentry = lookup_one_len(name, parent, strlen(name));
284 if (IS_ERR(dentry)) {
285 error = PTR_ERR(dentry);
286 goto fail_lock;
289 if (d_really_is_positive(dentry)) {
290 error = -EEXIST;
291 goto fail_dentry;
294 error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
295 if (error)
296 goto fail_dentry;
297 inode_unlock(dir);
299 return dentry;
301 fail_dentry:
302 dput(dentry);
304 fail_lock:
305 inode_unlock(dir);
306 simple_release_fs(&aafs_mnt, &aafs_count);
308 return ERR_PTR(error);
312 * aafs_create_file - create a file in the apparmorfs filesystem
314 * @name: name of dentry to create
315 * @mode: permissions the file should have
316 * @parent: parent directory for this dentry
317 * @data: data to store on inode.i_private, available in open()
318 * @fops: struct file_operations that should be used for
320 * see aafs_create
322 static struct dentry *aafs_create_file(const char *name, umode_t mode,
323 struct dentry *parent, void *data,
324 const struct file_operations *fops)
326 return aafs_create(name, mode, parent, data, NULL, fops, NULL);
330 * aafs_create_dir - create a directory in the apparmorfs filesystem
332 * @name: name of dentry to create
333 * @parent: parent directory for this dentry
335 * see aafs_create
337 static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
339 return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
340 NULL);
344 * aafs_create_symlink - create a symlink in the apparmorfs filesystem
345 * @name: name of dentry to create
346 * @parent: parent directory for this dentry
347 * @target: if symlink, symlink target string
348 * @private: private data
349 * @iops: struct of inode_operations that should be used
351 * If @target parameter is %NULL, then the @iops parameter needs to be
352 * setup to handle .readlink and .get_link inode_operations.
354 static struct dentry *aafs_create_symlink(const char *name,
355 struct dentry *parent,
356 const char *target,
357 void *private,
358 const struct inode_operations *iops)
360 struct dentry *dent;
361 char *link = NULL;
363 if (target) {
364 if (!link)
365 return ERR_PTR(-ENOMEM);
367 dent = aafs_create(name, S_IFLNK | 0444, parent, private, link, NULL,
368 iops);
369 if (IS_ERR(dent))
370 kfree(link);
372 return dent;
376 * aafs_remove - removes a file or directory from the apparmorfs filesystem
378 * @dentry: dentry of the file/directory/symlink to removed.
380 static void aafs_remove(struct dentry *dentry)
382 struct inode *dir;
384 if (!dentry || IS_ERR(dentry))
385 return;
387 dir = d_inode(dentry->d_parent);
388 inode_lock(dir);
389 if (simple_positive(dentry)) {
390 if (d_is_dir(dentry))
391 simple_rmdir(dir, dentry);
392 else
393 simple_unlink(dir, dentry);
394 d_delete(dentry);
395 dput(dentry);
397 inode_unlock(dir);
398 simple_release_fs(&aafs_mnt, &aafs_count);
403 * aa_fs - policy load/replace/remove
407 * aa_simple_write_to_buffer - common routine for getting policy from user
408 * @userbuf: user buffer to copy data from (NOT NULL)
409 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
410 * @copy_size: size of data to copy from user buffer
411 * @pos: position write is at in the file (NOT NULL)
413 * Returns: kernel buffer containing copy of user buffer data or an
414 * ERR_PTR on failure.
416 static struct aa_loaddata *aa_simple_write_to_buffer(const char __user *userbuf,
417 size_t alloc_size,
418 size_t copy_size,
419 loff_t *pos)
421 struct aa_loaddata *data;
423 AA_BUG(copy_size > alloc_size);
425 if (*pos != 0)
426 /* only writes from pos 0, that is complete writes */
427 return ERR_PTR(-ESPIPE);
429 /* freed by caller to simple_write_to_buffer */
430 data = aa_loaddata_alloc(alloc_size);
431 if (IS_ERR(data))
432 return data;
434 data->size = copy_size;
435 if (copy_from_user(data->data, userbuf, copy_size)) {
436 kvfree(data);
437 return ERR_PTR(-EFAULT);
440 return data;
443 static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
444 loff_t *pos, struct aa_ns *ns)
446 struct aa_loaddata *data;
447 struct aa_label *label;
448 ssize_t error;
450 label = begin_current_label_crit_section();
452 /* high level check about policy management - fine grained in
453 * below after unpack
455 error = aa_may_manage_policy(label, ns, mask);
456 if (error)
457 goto end_section;
459 data = aa_simple_write_to_buffer(buf, size, size, pos);
460 error = PTR_ERR(data);
461 if (!IS_ERR(data)) {
462 error = aa_replace_profiles(ns, label, mask, data);
463 aa_put_loaddata(data);
465 end_section:
466 end_current_label_crit_section(label);
468 return error;
471 /* .load file hook fn to load policy */
472 static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
473 loff_t *pos)
475 struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
476 int error = policy_update(AA_MAY_LOAD_POLICY, buf, size, pos, ns);
478 aa_put_ns(ns);
480 return error;
483 static const struct file_operations aa_fs_profile_load = {
484 .write = profile_load,
485 .llseek = default_llseek,
488 /* .replace file hook fn to load and/or replace policy */
489 static ssize_t profile_replace(struct file *f, const char __user *buf,
490 size_t size, loff_t *pos)
492 struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
493 int error = policy_update(AA_MAY_LOAD_POLICY | AA_MAY_REPLACE_POLICY,
494 buf, size, pos, ns);
495 aa_put_ns(ns);
497 return error;
500 static const struct file_operations aa_fs_profile_replace = {
501 .write = profile_replace,
502 .llseek = default_llseek,
505 /* .remove file hook fn to remove loaded policy */
506 static ssize_t profile_remove(struct file *f, const char __user *buf,
507 size_t size, loff_t *pos)
509 struct aa_loaddata *data;
510 struct aa_label *label;
511 ssize_t error;
512 struct aa_ns *ns = aa_get_ns(f->f_inode->i_private);
514 label = begin_current_label_crit_section();
515 /* high level check about policy management - fine grained in
516 * below after unpack
518 error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY);
519 if (error)
520 goto out;
523 * aa_remove_profile needs a null terminated string so 1 extra
524 * byte is allocated and the copied data is null terminated.
526 data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
528 error = PTR_ERR(data);
529 if (!IS_ERR(data)) {
530 data->data[size] = 0;
531 error = aa_remove_profiles(ns, label, data->data, size);
532 aa_put_loaddata(data);
534 out:
535 end_current_label_crit_section(label);
536 aa_put_ns(ns);
537 return error;
540 static const struct file_operations aa_fs_profile_remove = {
541 .write = profile_remove,
542 .llseek = default_llseek,
545 struct aa_revision {
546 struct aa_ns *ns;
547 long last_read;
550 /* revision file hook fn for policy loads */
551 static int ns_revision_release(struct inode *inode, struct file *file)
553 struct aa_revision *rev = file->private_data;
555 if (rev) {
556 aa_put_ns(rev->ns);
557 kfree(rev);
560 return 0;
563 static ssize_t ns_revision_read(struct file *file, char __user *buf,
564 size_t size, loff_t *ppos)
566 struct aa_revision *rev = file->private_data;
567 char buffer[32];
568 long last_read;
569 int avail;
571 mutex_lock_nested(&rev->ns->lock, rev->ns->level);
572 last_read = rev->last_read;
573 if (last_read == rev->ns->revision) {
574 mutex_unlock(&rev->ns->lock);
575 if (file->f_flags & O_NONBLOCK)
576 return -EAGAIN;
577 if (wait_event_interruptible(rev->ns->wait,
578 last_read !=
579 READ_ONCE(rev->ns->revision)))
580 return -ERESTARTSYS;
581 mutex_lock_nested(&rev->ns->lock, rev->ns->level);
584 avail = sprintf(buffer, "%ld\n", rev->ns->revision);
585 if (*ppos + size > avail) {
586 rev->last_read = rev->ns->revision;
587 *ppos = 0;
589 mutex_unlock(&rev->ns->lock);
591 return simple_read_from_buffer(buf, size, ppos, buffer, avail);
594 static int ns_revision_open(struct inode *inode, struct file *file)
596 struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
598 if (!rev)
599 return -ENOMEM;
601 rev->ns = aa_get_ns(inode->i_private);
602 if (!rev->ns)
603 rev->ns = aa_get_current_ns();
604 file->private_data = rev;
606 return 0;
609 static __poll_t ns_revision_poll(struct file *file, poll_table *pt)
611 struct aa_revision *rev = file->private_data;
612 __poll_t mask = 0;
614 if (rev) {
615 mutex_lock_nested(&rev->ns->lock, rev->ns->level);
616 poll_wait(file, &rev->ns->wait, pt);
617 if (rev->last_read < rev->ns->revision)
618 mask |= EPOLLIN | EPOLLRDNORM;
619 mutex_unlock(&rev->ns->lock);
622 return mask;
625 void __aa_bump_ns_revision(struct aa_ns *ns)
627 WRITE_ONCE(ns->revision, ns->revision + 1);
628 wake_up_interruptible(&ns->wait);
631 static const struct file_operations aa_fs_ns_revision_fops = {
632 .owner = THIS_MODULE,
633 .open = ns_revision_open,
634 .poll = ns_revision_poll,
635 .read = ns_revision_read,
636 .llseek = generic_file_llseek,
637 .release = ns_revision_release,
640 static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
641 const char *match_str, size_t match_len)
643 struct aa_perms tmp = { };
644 struct aa_dfa *dfa;
645 unsigned int state = 0;
647 if (profile_unconfined(profile))
648 return;
649 if (profile->file.dfa && *match_str == AA_CLASS_FILE) {
650 dfa = profile->file.dfa;
651 state = aa_dfa_match_len(dfa, profile->file.start,
652 match_str + 1, match_len - 1);
653 if (state) {
654 struct path_cond cond = { };
656 tmp = aa_compute_fperms(dfa, state, &cond);
658 } else if (profile->policy.dfa) {
659 if (!PROFILE_MEDIATES(profile, *match_str))
660 return; /* no change to current perms */
661 dfa = profile->policy.dfa;
662 state = aa_dfa_match_len(dfa, profile->policy.start[0],
663 match_str, match_len);
664 if (state)
665 aa_compute_perms(dfa, state, &tmp);
667 aa_apply_modes_to_perms(profile, &tmp);
668 aa_perms_accum_raw(perms, &tmp);
673 * query_data - queries a policy and writes its data to buf
674 * @buf: the resulting data is stored here (NOT NULL)
675 * @buf_len: size of buf
676 * @query: query string used to retrieve data
677 * @query_len: size of query including second NUL byte
679 * The buffers pointed to by buf and query may overlap. The query buffer is
680 * parsed before buf is written to.
682 * The query should look like "<LABEL>\0<KEY>\0", where <LABEL> is the name of
683 * the security confinement context and <KEY> is the name of the data to
684 * retrieve. <LABEL> and <KEY> must not be NUL-terminated.
686 * Don't expect the contents of buf to be preserved on failure.
688 * Returns: number of characters written to buf or -errno on failure
690 static ssize_t query_data(char *buf, size_t buf_len,
691 char *query, size_t query_len)
693 char *out;
694 const char *key;
695 struct label_it i;
696 struct aa_label *label, *curr;
697 struct aa_profile *profile;
698 struct aa_data *data;
699 u32 bytes, blocks;
700 __le32 outle32;
702 if (!query_len)
703 return -EINVAL; /* need a query */
705 key = query + strnlen(query, query_len) + 1;
706 if (key + 1 >= query + query_len)
707 return -EINVAL; /* not enough space for a non-empty key */
708 if (key + strnlen(key, query + query_len - key) >= query + query_len)
709 return -EINVAL; /* must end with NUL */
711 if (buf_len < sizeof(bytes) + sizeof(blocks))
712 return -EINVAL; /* not enough space */
714 curr = begin_current_label_crit_section();
715 label = aa_label_parse(curr, query, GFP_KERNEL, false, false);
716 end_current_label_crit_section(curr);
717 if (IS_ERR(label))
718 return PTR_ERR(label);
720 /* We are going to leave space for two numbers. The first is the total
721 * number of bytes we are writing after the first number. This is so
722 * users can read the full output without reallocation.
724 * The second number is the number of data blocks we're writing. An
725 * application might be confined by multiple policies having data in
726 * the same key.
728 memset(buf, 0, sizeof(bytes) + sizeof(blocks));
729 out = buf + sizeof(bytes) + sizeof(blocks);
731 blocks = 0;
732 label_for_each_confined(i, label, profile) {
733 if (!profile->data)
734 continue;
736 data = rhashtable_lookup_fast(profile->data, &key,
737 profile->data->p);
739 if (data) {
740 if (out + sizeof(outle32) + data->size > buf +
741 buf_len) {
742 aa_put_label(label);
743 return -EINVAL; /* not enough space */
745 outle32 = __cpu_to_le32(data->size);
746 memcpy(out, &outle32, sizeof(outle32));
747 out += sizeof(outle32);
748 memcpy(out, data->data, data->size);
749 out += data->size;
750 blocks++;
753 aa_put_label(label);
755 outle32 = __cpu_to_le32(out - buf - sizeof(bytes));
756 memcpy(buf, &outle32, sizeof(outle32));
757 outle32 = __cpu_to_le32(blocks);
758 memcpy(buf + sizeof(bytes), &outle32, sizeof(outle32));
760 return out - buf;
764 * query_label - queries a label and writes permissions to buf
765 * @buf: the resulting permissions string is stored here (NOT NULL)
766 * @buf_len: size of buf
767 * @query: binary query string to match against the dfa
768 * @query_len: size of query
769 * @view_only: only compute for querier's view
771 * The buffers pointed to by buf and query may overlap. The query buffer is
772 * parsed before buf is written to.
774 * The query should look like "LABEL_NAME\0DFA_STRING" where LABEL_NAME is
775 * the name of the label, in the current namespace, that is to be queried and
776 * DFA_STRING is a binary string to match against the label(s)'s DFA.
778 * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters
779 * but must *not* be NUL terminated.
781 * Returns: number of characters written to buf or -errno on failure
783 static ssize_t query_label(char *buf, size_t buf_len,
784 char *query, size_t query_len, bool view_only)
786 struct aa_profile *profile;
787 struct aa_label *label, *curr;
788 char *label_name, *match_str;
789 size_t label_name_len, match_len;
790 struct aa_perms perms;
791 struct label_it i;
793 if (!query_len)
794 return -EINVAL;
796 label_name = query;
797 label_name_len = strnlen(query, query_len);
798 if (!label_name_len || label_name_len == query_len)
799 return -EINVAL;
802 * The extra byte is to account for the null byte between the
803 * profile name and dfa string. profile_name_len is greater
804 * than zero and less than query_len, so a byte can be safely
805 * added or subtracted.
807 match_str = label_name + label_name_len + 1;
808 match_len = query_len - label_name_len - 1;
810 curr = begin_current_label_crit_section();
811 label = aa_label_parse(curr, label_name, GFP_KERNEL, false, false);
812 end_current_label_crit_section(curr);
813 if (IS_ERR(label))
814 return PTR_ERR(label);
816 perms = allperms;
817 if (view_only) {
818 label_for_each_in_ns(i, labels_ns(label), label, profile) {
819 profile_query_cb(profile, &perms, match_str, match_len);
821 } else {
822 label_for_each(i, label, profile) {
823 profile_query_cb(profile, &perms, match_str, match_len);
826 aa_put_label(label);
828 return scnprintf(buf, buf_len,
829 "allow 0x%08x\ndeny 0x%08x\naudit 0x%08x\nquiet 0x%08x\n",
830 perms.allow, perms.deny, perms.audit, perms.quiet);
834 * Transaction based IO.
835 * The file expects a write which triggers the transaction, and then
836 * possibly a read(s) which collects the result - which is stored in a
837 * file-local buffer. Once a new write is performed, a new set of results
838 * are stored in the file-local buffer.
840 struct multi_transaction {
841 struct kref count;
842 ssize_t size;
843 char data[0];
846 #define MULTI_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct multi_transaction))
847 /* TODO: replace with per file lock */
848 static DEFINE_SPINLOCK(multi_transaction_lock);
850 static void multi_transaction_kref(struct kref *kref)
852 struct multi_transaction *t;
854 t = container_of(kref, struct multi_transaction, count);
855 free_page((unsigned long) t);
858 static struct multi_transaction *
859 get_multi_transaction(struct multi_transaction *t)
861 if (t)
862 kref_get(&(t->count));
864 return t;
867 static void put_multi_transaction(struct multi_transaction *t)
869 if (t)
870 kref_put(&(t->count), multi_transaction_kref);
873 /* does not increment @new's count */
874 static void multi_transaction_set(struct file *file,
875 struct multi_transaction *new, size_t n)
877 struct multi_transaction *old;
879 AA_BUG(n > MULTI_TRANSACTION_LIMIT);
881 new->size = n;
882 spin_lock(&multi_transaction_lock);
883 old = (struct multi_transaction *) file->private_data;
884 file->private_data = new;
885 spin_unlock(&multi_transaction_lock);
886 put_multi_transaction(old);
889 static struct multi_transaction *multi_transaction_new(struct file *file,
890 const char __user *buf,
891 size_t size)
893 struct multi_transaction *t;
895 if (size > MULTI_TRANSACTION_LIMIT - 1)
896 return ERR_PTR(-EFBIG);
898 t = (struct multi_transaction *)get_zeroed_page(GFP_KERNEL);
899 if (!t)
900 return ERR_PTR(-ENOMEM);
901 kref_init(&t->count);
902 if (copy_from_user(t->data, buf, size))
903 return ERR_PTR(-EFAULT);
905 return t;
908 static ssize_t multi_transaction_read(struct file *file, char __user *buf,
909 size_t size, loff_t *pos)
911 struct multi_transaction *t;
912 ssize_t ret;
914 spin_lock(&multi_transaction_lock);
915 t = get_multi_transaction(file->private_data);
916 spin_unlock(&multi_transaction_lock);
917 if (!t)
918 return 0;
920 ret = simple_read_from_buffer(buf, size, pos, t->data, t->size);
921 put_multi_transaction(t);
923 return ret;
926 static int multi_transaction_release(struct inode *inode, struct file *file)
928 put_multi_transaction(file->private_data);
930 return 0;
933 #define QUERY_CMD_LABEL "label\0"
934 #define QUERY_CMD_LABEL_LEN 6
935 #define QUERY_CMD_PROFILE "profile\0"
936 #define QUERY_CMD_PROFILE_LEN 8
937 #define QUERY_CMD_LABELALL "labelall\0"
938 #define QUERY_CMD_LABELALL_LEN 9
939 #define QUERY_CMD_DATA "data\0"
940 #define QUERY_CMD_DATA_LEN 5
943 * aa_write_access - generic permissions and data query
944 * @file: pointer to open apparmorfs/access file
945 * @ubuf: user buffer containing the complete query string (NOT NULL)
946 * @count: size of ubuf
947 * @ppos: position in the file (MUST BE ZERO)
949 * Allows for one permissions or data query per open(), write(), and read()
950 * sequence. The only queries currently supported are label-based queries for
951 * permissions or data.
953 * For permissions queries, ubuf must begin with "label\0", followed by the
954 * profile query specific format described in the query_label() function
955 * documentation.
957 * For data queries, ubuf must have the form "data\0<LABEL>\0<KEY>\0", where
958 * <LABEL> is the name of the security confinement context and <KEY> is the
959 * name of the data to retrieve.
961 * Returns: number of bytes written or -errno on failure
963 static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
964 size_t count, loff_t *ppos)
966 struct multi_transaction *t;
967 ssize_t len;
969 if (*ppos)
970 return -ESPIPE;
972 t = multi_transaction_new(file, ubuf, count);
973 if (IS_ERR(t))
974 return PTR_ERR(t);
976 if (count > QUERY_CMD_PROFILE_LEN &&
977 !memcmp(t->data, QUERY_CMD_PROFILE, QUERY_CMD_PROFILE_LEN)) {
978 len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
979 t->data + QUERY_CMD_PROFILE_LEN,
980 count - QUERY_CMD_PROFILE_LEN, true);
981 } else if (count > QUERY_CMD_LABEL_LEN &&
982 !memcmp(t->data, QUERY_CMD_LABEL, QUERY_CMD_LABEL_LEN)) {
983 len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
984 t->data + QUERY_CMD_LABEL_LEN,
985 count - QUERY_CMD_LABEL_LEN, true);
986 } else if (count > QUERY_CMD_LABELALL_LEN &&
987 !memcmp(t->data, QUERY_CMD_LABELALL,
988 QUERY_CMD_LABELALL_LEN)) {
989 len = query_label(t->data, MULTI_TRANSACTION_LIMIT,
990 t->data + QUERY_CMD_LABELALL_LEN,
991 count - QUERY_CMD_LABELALL_LEN, false);
992 } else if (count > QUERY_CMD_DATA_LEN &&
993 !memcmp(t->data, QUERY_CMD_DATA, QUERY_CMD_DATA_LEN)) {
994 len = query_data(t->data, MULTI_TRANSACTION_LIMIT,
995 t->data + QUERY_CMD_DATA_LEN,
996 count - QUERY_CMD_DATA_LEN);
997 } else
998 len = -EINVAL;
1000 if (len < 0) {
1001 put_multi_transaction(t);
1002 return len;
1005 multi_transaction_set(file, t, len);
1007 return count;
1010 static const struct file_operations aa_sfs_access = {
1011 .write = aa_write_access,
1012 .read = multi_transaction_read,
1013 .release = multi_transaction_release,
1014 .llseek = generic_file_llseek,
1017 static int aa_sfs_seq_show(struct seq_file *seq, void *v)
1019 struct aa_sfs_entry *fs_file = seq->private;
1021 if (!fs_file)
1022 return 0;
1024 switch (fs_file->v_type) {
1025 case AA_SFS_TYPE_BOOLEAN:
1026 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
1027 break;
1028 case AA_SFS_TYPE_STRING:
1029 seq_printf(seq, "%s\n", fs_file->v.string);
1030 break;
1031 case AA_SFS_TYPE_U64:
1032 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
1033 break;
1034 default:
1035 /* Ignore unpritable entry types. */
1036 break;
1039 return 0;
1042 static int aa_sfs_seq_open(struct inode *inode, struct file *file)
1044 return single_open(file, aa_sfs_seq_show, inode->i_private);
1047 const struct file_operations aa_sfs_seq_file_ops = {
1048 .owner = THIS_MODULE,
1049 .open = aa_sfs_seq_open,
1050 .read = seq_read,
1051 .llseek = seq_lseek,
1052 .release = single_release,
1056 * profile based file operations
1057 * policy/profiles/XXXX/profiles/ *
1060 #define SEQ_PROFILE_FOPS(NAME) \
1061 static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
1063 return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \
1066 static const struct file_operations seq_profile_ ##NAME ##_fops = { \
1067 .owner = THIS_MODULE, \
1068 .open = seq_profile_ ##NAME ##_open, \
1069 .read = seq_read, \
1070 .llseek = seq_lseek, \
1071 .release = seq_profile_release, \
1074 static int seq_profile_open(struct inode *inode, struct file *file,
1075 int (*show)(struct seq_file *, void *))
1077 struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
1078 int error = single_open(file, show, proxy);
1080 if (error) {
1081 file->private_data = NULL;
1082 aa_put_proxy(proxy);
1085 return error;
1088 static int seq_profile_release(struct inode *inode, struct file *file)
1090 struct seq_file *seq = (struct seq_file *) file->private_data;
1091 if (seq)
1092 aa_put_proxy(seq->private);
1093 return single_release(inode, file);
1096 static int seq_profile_name_show(struct seq_file *seq, void *v)
1098 struct aa_proxy *proxy = seq->private;
1099 struct aa_label *label = aa_get_label_rcu(&proxy->label);
1100 struct aa_profile *profile = labels_profile(label);
1101 seq_printf(seq, "%s\n", profile->base.name);
1102 aa_put_label(label);
1104 return 0;
1107 static int seq_profile_mode_show(struct seq_file *seq, void *v)
1109 struct aa_proxy *proxy = seq->private;
1110 struct aa_label *label = aa_get_label_rcu(&proxy->label);
1111 struct aa_profile *profile = labels_profile(label);
1112 seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
1113 aa_put_label(label);
1115 return 0;
1118 static int seq_profile_attach_show(struct seq_file *seq, void *v)
1120 struct aa_proxy *proxy = seq->private;
1121 struct aa_label *label = aa_get_label_rcu(&proxy->label);
1122 struct aa_profile *profile = labels_profile(label);
1123 if (profile->attach)
1124 seq_printf(seq, "%s\n", profile->attach);
1125 else if (profile->xmatch)
1126 seq_puts(seq, "<unknown>\n");
1127 else
1128 seq_printf(seq, "%s\n", profile->base.name);
1129 aa_put_label(label);
1131 return 0;
1134 static int seq_profile_hash_show(struct seq_file *seq, void *v)
1136 struct aa_proxy *proxy = seq->private;
1137 struct aa_label *label = aa_get_label_rcu(&proxy->label);
1138 struct aa_profile *profile = labels_profile(label);
1139 unsigned int i, size = aa_hash_size();
1141 if (profile->hash) {
1142 for (i = 0; i < size; i++)
1143 seq_printf(seq, "%.2x", profile->hash[i]);
1144 seq_putc(seq, '\n');
1146 aa_put_label(label);
1148 return 0;
1151 SEQ_PROFILE_FOPS(name);
1152 SEQ_PROFILE_FOPS(mode);
1153 SEQ_PROFILE_FOPS(attach);
1154 SEQ_PROFILE_FOPS(hash);
1157 * namespace based files
1158 * several root files and
1159 * policy/ *
1162 #define SEQ_NS_FOPS(NAME) \
1163 static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \
1165 return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \
1168 static const struct file_operations seq_ns_ ##NAME ##_fops = { \
1169 .owner = THIS_MODULE, \
1170 .open = seq_ns_ ##NAME ##_open, \
1171 .read = seq_read, \
1172 .llseek = seq_lseek, \
1173 .release = single_release, \
1176 static int seq_ns_stacked_show(struct seq_file *seq, void *v)
1178 struct aa_label *label;
1180 label = begin_current_label_crit_section();
1181 seq_printf(seq, "%s\n", label->size > 1 ? "yes" : "no");
1182 end_current_label_crit_section(label);
1184 return 0;
1187 static int seq_ns_nsstacked_show(struct seq_file *seq, void *v)
1189 struct aa_label *label;
1190 struct aa_profile *profile;
1191 struct label_it it;
1192 int count = 1;
1194 label = begin_current_label_crit_section();
1196 if (label->size > 1) {
1197 label_for_each(it, label, profile)
1198 if (profile->ns != labels_ns(label)) {
1199 count++;
1200 break;
1204 seq_printf(seq, "%s\n", count > 1 ? "yes" : "no");
1205 end_current_label_crit_section(label);
1207 return 0;
1210 static int seq_ns_level_show(struct seq_file *seq, void *v)
1212 struct aa_label *label;
1214 label = begin_current_label_crit_section();
1215 seq_printf(seq, "%d\n", labels_ns(label)->level);
1216 end_current_label_crit_section(label);
1218 return 0;
1221 static int seq_ns_name_show(struct seq_file *seq, void *v)
1223 struct aa_label *label = begin_current_label_crit_section();
1224 seq_printf(seq, "%s\n", labels_ns(label)->base.name);
1225 end_current_label_crit_section(label);
1227 return 0;
1230 SEQ_NS_FOPS(stacked);
1231 SEQ_NS_FOPS(nsstacked);
1232 SEQ_NS_FOPS(level);
1233 SEQ_NS_FOPS(name);
1236 /* policy/raw_data/ * file ops */
1238 #define SEQ_RAWDATA_FOPS(NAME) \
1239 static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
1241 return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \
1244 static const struct file_operations seq_rawdata_ ##NAME ##_fops = { \
1245 .owner = THIS_MODULE, \
1246 .open = seq_rawdata_ ##NAME ##_open, \
1247 .read = seq_read, \
1248 .llseek = seq_lseek, \
1249 .release = seq_rawdata_release, \
1252 static int seq_rawdata_open(struct inode *inode, struct file *file,
1253 int (*show)(struct seq_file *, void *))
1255 struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
1256 int error;
1258 if (!data)
1259 /* lost race this ent is being reaped */
1260 return -ENOENT;
1262 error = single_open(file, show, data);
1263 if (error) {
1264 AA_BUG(file->private_data &&
1265 ((struct seq_file *)file->private_data)->private);
1266 aa_put_loaddata(data);
1269 return error;
1272 static int seq_rawdata_release(struct inode *inode, struct file *file)
1274 struct seq_file *seq = (struct seq_file *) file->private_data;
1276 if (seq)
1277 aa_put_loaddata(seq->private);
1279 return single_release(inode, file);
1282 static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
1284 struct aa_loaddata *data = seq->private;
1286 seq_printf(seq, "v%d\n", data->abi);
1288 return 0;
1291 static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
1293 struct aa_loaddata *data = seq->private;
1295 seq_printf(seq, "%ld\n", data->revision);
1297 return 0;
1300 static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
1302 struct aa_loaddata *data = seq->private;
1303 unsigned int i, size = aa_hash_size();
1305 if (data->hash) {
1306 for (i = 0; i < size; i++)
1307 seq_printf(seq, "%.2x", data->hash[i]);
1308 seq_putc(seq, '\n');
1311 return 0;
1314 static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v)
1316 struct aa_loaddata *data = seq->private;
1318 seq_printf(seq, "%zu\n", data->compressed_size);
1320 return 0;
1323 SEQ_RAWDATA_FOPS(abi);
1324 SEQ_RAWDATA_FOPS(revision);
1325 SEQ_RAWDATA_FOPS(hash);
1326 SEQ_RAWDATA_FOPS(compressed_size);
1328 static int deflate_decompress(char *src, size_t slen, char *dst, size_t dlen)
1330 int error;
1331 struct z_stream_s strm;
1333 if (aa_g_rawdata_compression_level == 0) {
1334 if (dlen < slen)
1335 return -EINVAL;
1336 memcpy(dst, src, slen);
1337 return 0;
1340 memset(&strm, 0, sizeof(strm));
1342 strm.workspace = kvzalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
1343 if (!strm.workspace)
1344 return -ENOMEM;
1346 strm.next_in = src;
1347 strm.avail_in = slen;
1349 error = zlib_inflateInit(&strm);
1350 if (error != Z_OK) {
1351 error = -ENOMEM;
1352 goto fail_inflate_init;
1355 strm.next_out = dst;
1356 strm.avail_out = dlen;
1358 error = zlib_inflate(&strm, Z_FINISH);
1359 if (error != Z_STREAM_END)
1360 error = -EINVAL;
1361 else
1362 error = 0;
1364 zlib_inflateEnd(&strm);
1365 fail_inflate_init:
1366 kvfree(strm.workspace);
1367 return error;
1370 static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
1371 loff_t *ppos)
1373 struct rawdata_f_data *private = file->private_data;
1375 return simple_read_from_buffer(buf, size, ppos,
1376 RAWDATA_F_DATA_BUF(private),
1377 private->loaddata->size);
1380 static int rawdata_release(struct inode *inode, struct file *file)
1382 rawdata_f_data_free(file->private_data);
1384 return 0;
1387 static int rawdata_open(struct inode *inode, struct file *file)
1389 int error;
1390 struct aa_loaddata *loaddata;
1391 struct rawdata_f_data *private;
1393 if (!policy_view_capable(NULL))
1394 return -EACCES;
1396 loaddata = __aa_get_loaddata(inode->i_private);
1397 if (!loaddata)
1398 /* lost race: this entry is being reaped */
1399 return -ENOENT;
1401 private = rawdata_f_data_alloc(loaddata->size);
1402 if (IS_ERR(private)) {
1403 error = PTR_ERR(private);
1404 goto fail_private_alloc;
1407 private->loaddata = loaddata;
1409 error = deflate_decompress(loaddata->data, loaddata->compressed_size,
1410 RAWDATA_F_DATA_BUF(private),
1411 loaddata->size);
1412 if (error)
1413 goto fail_decompress;
1415 file->private_data = private;
1416 return 0;
1418 fail_decompress:
1419 rawdata_f_data_free(private);
1420 return error;
1422 fail_private_alloc:
1423 aa_put_loaddata(loaddata);
1424 return error;
1427 static const struct file_operations rawdata_fops = {
1428 .open = rawdata_open,
1429 .read = rawdata_read,
1430 .llseek = generic_file_llseek,
1431 .release = rawdata_release,
1434 static void remove_rawdata_dents(struct aa_loaddata *rawdata)
1436 int i;
1438 for (i = 0; i < AAFS_LOADDATA_NDENTS; i++) {
1439 if (!IS_ERR_OR_NULL(rawdata->dents[i])) {
1440 /* no refcounts on i_private */
1441 aafs_remove(rawdata->dents[i]);
1442 rawdata->dents[i] = NULL;
1447 void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
1449 AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
1451 if (rawdata->ns) {
1452 remove_rawdata_dents(rawdata);
1453 list_del_init(&rawdata->list);
1454 aa_put_ns(rawdata->ns);
1455 rawdata->ns = NULL;
1459 int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
1461 struct dentry *dent, *dir;
1463 AA_BUG(!ns);
1464 AA_BUG(!rawdata);
1465 AA_BUG(!mutex_is_locked(&ns->lock));
1466 AA_BUG(!ns_subdata_dir(ns));
1469 * just use ns revision dir was originally created at. This is
1470 * under ns->lock and if load is successful revision will be
1471 * bumped and is guaranteed to be unique
1473 rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
1474 if (!rawdata->name)
1475 return -ENOMEM;
1477 dir = aafs_create_dir(rawdata->name, ns_subdata_dir(ns));
1478 if (IS_ERR(dir))
1479 /* ->name freed when rawdata freed */
1480 return PTR_ERR(dir);
1481 rawdata->dents[AAFS_LOADDATA_DIR] = dir;
1483 dent = aafs_create_file("abi", S_IFREG | 0444, dir, rawdata,
1484 &seq_rawdata_abi_fops);
1485 if (IS_ERR(dent))
1486 goto fail;
1487 rawdata->dents[AAFS_LOADDATA_ABI] = dent;
1489 dent = aafs_create_file("revision", S_IFREG | 0444, dir, rawdata,
1490 &seq_rawdata_revision_fops);
1491 if (IS_ERR(dent))
1492 goto fail;
1493 rawdata->dents[AAFS_LOADDATA_REVISION] = dent;
1495 if (aa_g_hash_policy) {
1496 dent = aafs_create_file("sha1", S_IFREG | 0444, dir,
1497 rawdata, &seq_rawdata_hash_fops);
1498 if (IS_ERR(dent))
1499 goto fail;
1500 rawdata->dents[AAFS_LOADDATA_HASH] = dent;
1503 dent = aafs_create_file("compressed_size", S_IFREG | 0444, dir,
1504 rawdata,
1505 &seq_rawdata_compressed_size_fops);
1506 if (IS_ERR(dent))
1507 goto fail;
1508 rawdata->dents[AAFS_LOADDATA_COMPRESSED_SIZE] = dent;
1510 dent = aafs_create_file("raw_data", S_IFREG | 0444,
1511 dir, rawdata, &rawdata_fops);
1512 if (IS_ERR(dent))
1513 goto fail;
1514 rawdata->dents[AAFS_LOADDATA_DATA] = dent;
1515 d_inode(dent)->i_size = rawdata->size;
1517 rawdata->ns = aa_get_ns(ns);
1518 list_add(&rawdata->list, &ns->rawdata_list);
1519 /* no refcount on inode rawdata */
1521 return 0;
1523 fail:
1524 remove_rawdata_dents(rawdata);
1526 return PTR_ERR(dent);
1529 /** fns to setup dynamic per profile/namespace files **/
1533 * Requires: @profile->ns->lock held
1535 void __aafs_profile_rmdir(struct aa_profile *profile)
1537 struct aa_profile *child;
1538 int i;
1540 if (!profile)
1541 return;
1543 list_for_each_entry(child, &profile->base.profiles, base.list)
1544 __aafs_profile_rmdir(child);
1546 for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
1547 struct aa_proxy *proxy;
1548 if (!profile->dents[i])
1549 continue;
1551 proxy = d_inode(profile->dents[i])->i_private;
1552 aafs_remove(profile->dents[i]);
1553 aa_put_proxy(proxy);
1554 profile->dents[i] = NULL;
1560 * Requires: @old->ns->lock held
1562 void __aafs_profile_migrate_dents(struct aa_profile *old,
1563 struct aa_profile *new)
1565 int i;
1567 AA_BUG(!old);
1568 AA_BUG(!new);
1569 AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock));
1571 for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
1572 new->dents[i] = old->dents[i];
1573 if (new->dents[i])
1574 new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
1575 old->dents[i] = NULL;
1579 static struct dentry *create_profile_file(struct dentry *dir, const char *name,
1580 struct aa_profile *profile,
1581 const struct file_operations *fops)
1583 struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
1584 struct dentry *dent;
1586 dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
1587 if (IS_ERR(dent))
1588 aa_put_proxy(proxy);
1590 return dent;
1593 static int profile_depth(struct aa_profile *profile)
1595 int depth = 0;
1597 rcu_read_lock();
1598 for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
1599 depth++;
1600 rcu_read_unlock();
1602 return depth;
1605 static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
1607 char *buffer, *s;
1608 int error;
1609 int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
1611 s = buffer = kmalloc(size, GFP_KERNEL);
1612 if (!buffer)
1613 return ERR_PTR(-ENOMEM);
1615 for (; depth > 0; depth--) {
1616 strcpy(s, "../../");
1617 s += 6;
1618 size -= 6;
1621 error = snprintf(s, size, "raw_data/%s/%s", dirname, fname);
1622 if (error >= size || error < 0) {
1623 kfree(buffer);
1624 return ERR_PTR(-ENAMETOOLONG);
1627 return buffer;
1630 static void rawdata_link_cb(void *arg)
1632 kfree(arg);
1635 static const char *rawdata_get_link_base(struct dentry *dentry,
1636 struct inode *inode,
1637 struct delayed_call *done,
1638 const char *name)
1640 struct aa_proxy *proxy = inode->i_private;
1641 struct aa_label *label;
1642 struct aa_profile *profile;
1643 char *target;
1644 int depth;
1646 if (!dentry)
1647 return ERR_PTR(-ECHILD);
1649 label = aa_get_label_rcu(&proxy->label);
1650 profile = labels_profile(label);
1651 depth = profile_depth(profile);
1652 target = gen_symlink_name(depth, profile->rawdata->name, name);
1653 aa_put_label(label);
1655 if (IS_ERR(target))
1656 return target;
1658 set_delayed_call(done, rawdata_link_cb, target);
1660 return target;
1663 static const char *rawdata_get_link_sha1(struct dentry *dentry,
1664 struct inode *inode,
1665 struct delayed_call *done)
1667 return rawdata_get_link_base(dentry, inode, done, "sha1");
1670 static const char *rawdata_get_link_abi(struct dentry *dentry,
1671 struct inode *inode,
1672 struct delayed_call *done)
1674 return rawdata_get_link_base(dentry, inode, done, "abi");
1677 static const char *rawdata_get_link_data(struct dentry *dentry,
1678 struct inode *inode,
1679 struct delayed_call *done)
1681 return rawdata_get_link_base(dentry, inode, done, "raw_data");
1684 static const struct inode_operations rawdata_link_sha1_iops = {
1685 .get_link = rawdata_get_link_sha1,
1688 static const struct inode_operations rawdata_link_abi_iops = {
1689 .get_link = rawdata_get_link_abi,
1691 static const struct inode_operations rawdata_link_data_iops = {
1692 .get_link = rawdata_get_link_data,
1697 * Requires: @profile->ns->lock held
1699 int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
1701 struct aa_profile *child;
1702 struct dentry *dent = NULL, *dir;
1703 int error;
1705 AA_BUG(!profile);
1706 AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock));
1708 if (!parent) {
1709 struct aa_profile *p;
1710 p = aa_deref_parent(profile);
1711 dent = prof_dir(p);
1712 /* adding to parent that previously didn't have children */
1713 dent = aafs_create_dir("profiles", dent);
1714 if (IS_ERR(dent))
1715 goto fail;
1716 prof_child_dir(p) = parent = dent;
1719 if (!profile->dirname) {
1720 int len, id_len;
1721 len = mangle_name(profile->base.name, NULL);
1722 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
1724 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
1725 if (!profile->dirname) {
1726 error = -ENOMEM;
1727 goto fail2;
1730 mangle_name(profile->base.name, profile->dirname);
1731 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
1734 dent = aafs_create_dir(profile->dirname, parent);
1735 if (IS_ERR(dent))
1736 goto fail;
1737 prof_dir(profile) = dir = dent;
1739 dent = create_profile_file(dir, "name", profile,
1740 &seq_profile_name_fops);
1741 if (IS_ERR(dent))
1742 goto fail;
1743 profile->dents[AAFS_PROF_NAME] = dent;
1745 dent = create_profile_file(dir, "mode", profile,
1746 &seq_profile_mode_fops);
1747 if (IS_ERR(dent))
1748 goto fail;
1749 profile->dents[AAFS_PROF_MODE] = dent;
1751 dent = create_profile_file(dir, "attach", profile,
1752 &seq_profile_attach_fops);
1753 if (IS_ERR(dent))
1754 goto fail;
1755 profile->dents[AAFS_PROF_ATTACH] = dent;
1757 if (profile->hash) {
1758 dent = create_profile_file(dir, "sha1", profile,
1759 &seq_profile_hash_fops);
1760 if (IS_ERR(dent))
1761 goto fail;
1762 profile->dents[AAFS_PROF_HASH] = dent;
1765 if (profile->rawdata) {
1766 dent = aafs_create_symlink("raw_sha1", dir, NULL,
1767 profile->label.proxy,
1768 &rawdata_link_sha1_iops);
1769 if (IS_ERR(dent))
1770 goto fail;
1771 aa_get_proxy(profile->label.proxy);
1772 profile->dents[AAFS_PROF_RAW_HASH] = dent;
1774 dent = aafs_create_symlink("raw_abi", dir, NULL,
1775 profile->label.proxy,
1776 &rawdata_link_abi_iops);
1777 if (IS_ERR(dent))
1778 goto fail;
1779 aa_get_proxy(profile->label.proxy);
1780 profile->dents[AAFS_PROF_RAW_ABI] = dent;
1782 dent = aafs_create_symlink("raw_data", dir, NULL,
1783 profile->label.proxy,
1784 &rawdata_link_data_iops);
1785 if (IS_ERR(dent))
1786 goto fail;
1787 aa_get_proxy(profile->label.proxy);
1788 profile->dents[AAFS_PROF_RAW_DATA] = dent;
1791 list_for_each_entry(child, &profile->base.profiles, base.list) {
1792 error = __aafs_profile_mkdir(child, prof_child_dir(profile));
1793 if (error)
1794 goto fail2;
1797 return 0;
1799 fail:
1800 error = PTR_ERR(dent);
1802 fail2:
1803 __aafs_profile_rmdir(profile);
1805 return error;
1808 static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode)
1810 struct aa_ns *ns, *parent;
1811 /* TODO: improve permission check */
1812 struct aa_label *label;
1813 int error;
1815 label = begin_current_label_crit_section();
1816 error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1817 end_current_label_crit_section(label);
1818 if (error)
1819 return error;
1821 parent = aa_get_ns(dir->i_private);
1822 AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
1824 /* we have to unlock and then relock to get locking order right
1825 * for pin_fs
1827 inode_unlock(dir);
1828 error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
1829 mutex_lock_nested(&parent->lock, parent->level);
1830 inode_lock_nested(dir, I_MUTEX_PARENT);
1831 if (error)
1832 goto out;
1834 error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL,
1835 NULL, NULL, NULL);
1836 if (error)
1837 goto out_pin;
1839 ns = __aa_find_or_create_ns(parent, READ_ONCE(dentry->d_name.name),
1840 dentry);
1841 if (IS_ERR(ns)) {
1842 error = PTR_ERR(ns);
1843 ns = NULL;
1846 aa_put_ns(ns); /* list ref remains */
1847 out_pin:
1848 if (error)
1849 simple_release_fs(&aafs_mnt, &aafs_count);
1850 out:
1851 mutex_unlock(&parent->lock);
1852 aa_put_ns(parent);
1854 return error;
1857 static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
1859 struct aa_ns *ns, *parent;
1860 /* TODO: improve permission check */
1861 struct aa_label *label;
1862 int error;
1864 label = begin_current_label_crit_section();
1865 error = aa_may_manage_policy(label, NULL, AA_MAY_LOAD_POLICY);
1866 end_current_label_crit_section(label);
1867 if (error)
1868 return error;
1870 parent = aa_get_ns(dir->i_private);
1871 /* rmdir calls the generic securityfs functions to remove files
1872 * from the apparmor dir. It is up to the apparmor ns locking
1873 * to avoid races.
1875 inode_unlock(dir);
1876 inode_unlock(dentry->d_inode);
1878 mutex_lock_nested(&parent->lock, parent->level);
1879 ns = aa_get_ns(__aa_findn_ns(&parent->sub_ns, dentry->d_name.name,
1880 dentry->d_name.len));
1881 if (!ns) {
1882 error = -ENOENT;
1883 goto out;
1885 AA_BUG(ns_dir(ns) != dentry);
1887 __aa_remove_ns(ns);
1888 aa_put_ns(ns);
1890 out:
1891 mutex_unlock(&parent->lock);
1892 inode_lock_nested(dir, I_MUTEX_PARENT);
1893 inode_lock(dentry->d_inode);
1894 aa_put_ns(parent);
1896 return error;
1899 static const struct inode_operations ns_dir_inode_operations = {
1900 .lookup = simple_lookup,
1901 .mkdir = ns_mkdir_op,
1902 .rmdir = ns_rmdir_op,
1905 static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
1907 struct aa_loaddata *ent, *tmp;
1909 AA_BUG(!mutex_is_locked(&ns->lock));
1911 list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
1912 __aa_fs_remove_rawdata(ent);
1917 * Requires: @ns->lock held
1919 void __aafs_ns_rmdir(struct aa_ns *ns)
1921 struct aa_ns *sub;
1922 struct aa_profile *child;
1923 int i;
1925 if (!ns)
1926 return;
1927 AA_BUG(!mutex_is_locked(&ns->lock));
1929 list_for_each_entry(child, &ns->base.profiles, base.list)
1930 __aafs_profile_rmdir(child);
1932 list_for_each_entry(sub, &ns->sub_ns, base.list) {
1933 mutex_lock_nested(&sub->lock, sub->level);
1934 __aafs_ns_rmdir(sub);
1935 mutex_unlock(&sub->lock);
1938 __aa_fs_list_remove_rawdata(ns);
1940 if (ns_subns_dir(ns)) {
1941 sub = d_inode(ns_subns_dir(ns))->i_private;
1942 aa_put_ns(sub);
1944 if (ns_subload(ns)) {
1945 sub = d_inode(ns_subload(ns))->i_private;
1946 aa_put_ns(sub);
1948 if (ns_subreplace(ns)) {
1949 sub = d_inode(ns_subreplace(ns))->i_private;
1950 aa_put_ns(sub);
1952 if (ns_subremove(ns)) {
1953 sub = d_inode(ns_subremove(ns))->i_private;
1954 aa_put_ns(sub);
1956 if (ns_subrevision(ns)) {
1957 sub = d_inode(ns_subrevision(ns))->i_private;
1958 aa_put_ns(sub);
1961 for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
1962 aafs_remove(ns->dents[i]);
1963 ns->dents[i] = NULL;
1967 /* assumes cleanup in caller */
1968 static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
1970 struct dentry *dent;
1972 AA_BUG(!ns);
1973 AA_BUG(!dir);
1975 dent = aafs_create_dir("profiles", dir);
1976 if (IS_ERR(dent))
1977 return PTR_ERR(dent);
1978 ns_subprofs_dir(ns) = dent;
1980 dent = aafs_create_dir("raw_data", dir);
1981 if (IS_ERR(dent))
1982 return PTR_ERR(dent);
1983 ns_subdata_dir(ns) = dent;
1985 dent = aafs_create_file("revision", 0444, dir, ns,
1986 &aa_fs_ns_revision_fops);
1987 if (IS_ERR(dent))
1988 return PTR_ERR(dent);
1989 aa_get_ns(ns);
1990 ns_subrevision(ns) = dent;
1992 dent = aafs_create_file(".load", 0640, dir, ns,
1993 &aa_fs_profile_load);
1994 if (IS_ERR(dent))
1995 return PTR_ERR(dent);
1996 aa_get_ns(ns);
1997 ns_subload(ns) = dent;
1999 dent = aafs_create_file(".replace", 0640, dir, ns,
2000 &aa_fs_profile_replace);
2001 if (IS_ERR(dent))
2002 return PTR_ERR(dent);
2003 aa_get_ns(ns);
2004 ns_subreplace(ns) = dent;
2006 dent = aafs_create_file(".remove", 0640, dir, ns,
2007 &aa_fs_profile_remove);
2008 if (IS_ERR(dent))
2009 return PTR_ERR(dent);
2010 aa_get_ns(ns);
2011 ns_subremove(ns) = dent;
2013 /* use create_dentry so we can supply private data */
2014 dent = aafs_create("namespaces", S_IFDIR | 0755, dir, ns, NULL, NULL,
2015 &ns_dir_inode_operations);
2016 if (IS_ERR(dent))
2017 return PTR_ERR(dent);
2018 aa_get_ns(ns);
2019 ns_subns_dir(ns) = dent;
2021 return 0;
2025 * Requires: @ns->lock held
2027 int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
2028 struct dentry *dent)
2030 struct aa_ns *sub;
2031 struct aa_profile *child;
2032 struct dentry *dir;
2033 int error;
2035 AA_BUG(!ns);
2036 AA_BUG(!parent);
2037 AA_BUG(!mutex_is_locked(&ns->lock));
2039 if (!name)
2040 name = ns->base.name;
2042 if (!dent) {
2043 /* create ns dir if it doesn't already exist */
2044 dent = aafs_create_dir(name, parent);
2045 if (IS_ERR(dent))
2046 goto fail;
2047 } else
2048 dget(dent);
2049 ns_dir(ns) = dir = dent;
2050 error = __aafs_ns_mkdir_entries(ns, dir);
2051 if (error)
2052 goto fail2;
2054 /* profiles */
2055 list_for_each_entry(child, &ns->base.profiles, base.list) {
2056 error = __aafs_profile_mkdir(child, ns_subprofs_dir(ns));
2057 if (error)
2058 goto fail2;
2061 /* subnamespaces */
2062 list_for_each_entry(sub, &ns->sub_ns, base.list) {
2063 mutex_lock_nested(&sub->lock, sub->level);
2064 error = __aafs_ns_mkdir(sub, ns_subns_dir(ns), NULL, NULL);
2065 mutex_unlock(&sub->lock);
2066 if (error)
2067 goto fail2;
2070 return 0;
2072 fail:
2073 error = PTR_ERR(dent);
2075 fail2:
2076 __aafs_ns_rmdir(ns);
2078 return error;
2082 #define list_entry_is_head(pos, head, member) (&pos->member == (head))
2085 * __next_ns - find the next namespace to list
2086 * @root: root namespace to stop search at (NOT NULL)
2087 * @ns: current ns position (NOT NULL)
2089 * Find the next namespace from @ns under @root and handle all locking needed
2090 * while switching current namespace.
2092 * Returns: next namespace or NULL if at last namespace under @root
2093 * Requires: ns->parent->lock to be held
2094 * NOTE: will not unlock root->lock
2096 static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
2098 struct aa_ns *parent, *next;
2100 AA_BUG(!root);
2101 AA_BUG(!ns);
2102 AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock));
2104 /* is next namespace a child */
2105 if (!list_empty(&ns->sub_ns)) {
2106 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
2107 mutex_lock_nested(&next->lock, next->level);
2108 return next;
2111 /* check if the next ns is a sibling, parent, gp, .. */
2112 parent = ns->parent;
2113 while (ns != root) {
2114 mutex_unlock(&ns->lock);
2115 next = list_next_entry(ns, base.list);
2116 if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
2117 mutex_lock_nested(&next->lock, next->level);
2118 return next;
2120 ns = parent;
2121 parent = parent->parent;
2124 return NULL;
2128 * __first_profile - find the first profile in a namespace
2129 * @root: namespace that is root of profiles being displayed (NOT NULL)
2130 * @ns: namespace to start in (NOT NULL)
2132 * Returns: unrefcounted profile or NULL if no profile
2133 * Requires: profile->ns.lock to be held
2135 static struct aa_profile *__first_profile(struct aa_ns *root,
2136 struct aa_ns *ns)
2138 AA_BUG(!root);
2139 AA_BUG(ns && !mutex_is_locked(&ns->lock));
2141 for (; ns; ns = __next_ns(root, ns)) {
2142 if (!list_empty(&ns->base.profiles))
2143 return list_first_entry(&ns->base.profiles,
2144 struct aa_profile, base.list);
2146 return NULL;
2150 * __next_profile - step to the next profile in a profile tree
2151 * @profile: current profile in tree (NOT NULL)
2153 * Perform a depth first traversal on the profile tree in a namespace
2155 * Returns: next profile or NULL if done
2156 * Requires: profile->ns.lock to be held
2158 static struct aa_profile *__next_profile(struct aa_profile *p)
2160 struct aa_profile *parent;
2161 struct aa_ns *ns = p->ns;
2163 AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock));
2165 /* is next profile a child */
2166 if (!list_empty(&p->base.profiles))
2167 return list_first_entry(&p->base.profiles, typeof(*p),
2168 base.list);
2170 /* is next profile a sibling, parent sibling, gp, sibling, .. */
2171 parent = rcu_dereference_protected(p->parent,
2172 mutex_is_locked(&p->ns->lock));
2173 while (parent) {
2174 p = list_next_entry(p, base.list);
2175 if (!list_entry_is_head(p, &parent->base.profiles, base.list))
2176 return p;
2177 p = parent;
2178 parent = rcu_dereference_protected(parent->parent,
2179 mutex_is_locked(&parent->ns->lock));
2182 /* is next another profile in the namespace */
2183 p = list_next_entry(p, base.list);
2184 if (!list_entry_is_head(p, &ns->base.profiles, base.list))
2185 return p;
2187 return NULL;
2191 * next_profile - step to the next profile in where ever it may be
2192 * @root: root namespace (NOT NULL)
2193 * @profile: current profile (NOT NULL)
2195 * Returns: next profile or NULL if there isn't one
2197 static struct aa_profile *next_profile(struct aa_ns *root,
2198 struct aa_profile *profile)
2200 struct aa_profile *next = __next_profile(profile);
2201 if (next)
2202 return next;
2204 /* finished all profiles in namespace move to next namespace */
2205 return __first_profile(root, __next_ns(root, profile->ns));
2209 * p_start - start a depth first traversal of profile tree
2210 * @f: seq_file to fill
2211 * @pos: current position
2213 * Returns: first profile under current namespace or NULL if none found
2215 * acquires first ns->lock
2217 static void *p_start(struct seq_file *f, loff_t *pos)
2219 struct aa_profile *profile = NULL;
2220 struct aa_ns *root = aa_get_current_ns();
2221 loff_t l = *pos;
2222 f->private = root;
2224 /* find the first profile */
2225 mutex_lock_nested(&root->lock, root->level);
2226 profile = __first_profile(root, root);
2228 /* skip to position */
2229 for (; profile && l > 0; l--)
2230 profile = next_profile(root, profile);
2232 return profile;
2236 * p_next - read the next profile entry
2237 * @f: seq_file to fill
2238 * @p: profile previously returned
2239 * @pos: current position
2241 * Returns: next profile after @p or NULL if none
2243 * may acquire/release locks in namespace tree as necessary
2245 static void *p_next(struct seq_file *f, void *p, loff_t *pos)
2247 struct aa_profile *profile = p;
2248 struct aa_ns *ns = f->private;
2249 (*pos)++;
2251 return next_profile(ns, profile);
2255 * p_stop - stop depth first traversal
2256 * @f: seq_file we are filling
2257 * @p: the last profile writen
2259 * Release all locking done by p_start/p_next on namespace tree
2261 static void p_stop(struct seq_file *f, void *p)
2263 struct aa_profile *profile = p;
2264 struct aa_ns *root = f->private, *ns;
2266 if (profile) {
2267 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
2268 mutex_unlock(&ns->lock);
2270 mutex_unlock(&root->lock);
2271 aa_put_ns(root);
2275 * seq_show_profile - show a profile entry
2276 * @f: seq_file to file
2277 * @p: current position (profile) (NOT NULL)
2279 * Returns: error on failure
2281 static int seq_show_profile(struct seq_file *f, void *p)
2283 struct aa_profile *profile = (struct aa_profile *)p;
2284 struct aa_ns *root = f->private;
2286 aa_label_seq_xprint(f, root, &profile->label,
2287 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
2288 seq_putc(f, '\n');
2290 return 0;
2293 static const struct seq_operations aa_sfs_profiles_op = {
2294 .start = p_start,
2295 .next = p_next,
2296 .stop = p_stop,
2297 .show = seq_show_profile,
2300 static int profiles_open(struct inode *inode, struct file *file)
2302 if (!policy_view_capable(NULL))
2303 return -EACCES;
2305 return seq_open(file, &aa_sfs_profiles_op);
2308 static int profiles_release(struct inode *inode, struct file *file)
2310 return seq_release(inode, file);
2313 static const struct file_operations aa_sfs_profiles_fops = {
2314 .open = profiles_open,
2315 .read = seq_read,
2316 .llseek = seq_lseek,
2317 .release = profiles_release,
2321 /** Base file system setup **/
2322 static struct aa_sfs_entry aa_sfs_entry_file[] = {
2323 AA_SFS_FILE_STRING("mask",
2324 "create read write exec append mmap_exec link lock"),
2328 static struct aa_sfs_entry aa_sfs_entry_ptrace[] = {
2329 AA_SFS_FILE_STRING("mask", "read trace"),
2333 static struct aa_sfs_entry aa_sfs_entry_signal[] = {
2334 AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK),
2338 static struct aa_sfs_entry aa_sfs_entry_attach[] = {
2339 AA_SFS_FILE_BOOLEAN("xattr", 1),
2342 static struct aa_sfs_entry aa_sfs_entry_domain[] = {
2343 AA_SFS_FILE_BOOLEAN("change_hat", 1),
2344 AA_SFS_FILE_BOOLEAN("change_hatv", 1),
2345 AA_SFS_FILE_BOOLEAN("change_onexec", 1),
2346 AA_SFS_FILE_BOOLEAN("change_profile", 1),
2347 AA_SFS_FILE_BOOLEAN("stack", 1),
2348 AA_SFS_FILE_BOOLEAN("fix_binfmt_elf_mmap", 1),
2349 AA_SFS_FILE_BOOLEAN("post_nnp_subset", 1),
2350 AA_SFS_FILE_BOOLEAN("computed_longest_left", 1),
2351 AA_SFS_DIR("attach_conditions", aa_sfs_entry_attach),
2352 AA_SFS_FILE_STRING("version", "1.2"),
2356 static struct aa_sfs_entry aa_sfs_entry_versions[] = {
2357 AA_SFS_FILE_BOOLEAN("v5", 1),
2358 AA_SFS_FILE_BOOLEAN("v6", 1),
2359 AA_SFS_FILE_BOOLEAN("v7", 1),
2360 AA_SFS_FILE_BOOLEAN("v8", 1),
2364 static struct aa_sfs_entry aa_sfs_entry_policy[] = {
2365 AA_SFS_DIR("versions", aa_sfs_entry_versions),
2366 AA_SFS_FILE_BOOLEAN("set_load", 1),
2370 static struct aa_sfs_entry aa_sfs_entry_mount[] = {
2371 AA_SFS_FILE_STRING("mask", "mount umount pivot_root"),
2375 static struct aa_sfs_entry aa_sfs_entry_ns[] = {
2376 AA_SFS_FILE_BOOLEAN("profile", 1),
2377 AA_SFS_FILE_BOOLEAN("pivot_root", 0),
2381 static struct aa_sfs_entry aa_sfs_entry_query_label[] = {
2382 AA_SFS_FILE_STRING("perms", "allow deny audit quiet"),
2383 AA_SFS_FILE_BOOLEAN("data", 1),
2384 AA_SFS_FILE_BOOLEAN("multi_transaction", 1),
2388 static struct aa_sfs_entry aa_sfs_entry_query[] = {
2389 AA_SFS_DIR("label", aa_sfs_entry_query_label),
2392 static struct aa_sfs_entry aa_sfs_entry_features[] = {
2393 AA_SFS_DIR("policy", aa_sfs_entry_policy),
2394 AA_SFS_DIR("domain", aa_sfs_entry_domain),
2395 AA_SFS_DIR("file", aa_sfs_entry_file),
2396 AA_SFS_DIR("network_v8", aa_sfs_entry_network),
2397 AA_SFS_DIR("mount", aa_sfs_entry_mount),
2398 AA_SFS_DIR("namespaces", aa_sfs_entry_ns),
2399 AA_SFS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
2400 AA_SFS_DIR("rlimit", aa_sfs_entry_rlimit),
2401 AA_SFS_DIR("caps", aa_sfs_entry_caps),
2402 AA_SFS_DIR("ptrace", aa_sfs_entry_ptrace),
2403 AA_SFS_DIR("signal", aa_sfs_entry_signal),
2404 AA_SFS_DIR("query", aa_sfs_entry_query),
2408 static struct aa_sfs_entry aa_sfs_entry_apparmor[] = {
2409 AA_SFS_FILE_FOPS(".access", 0666, &aa_sfs_access),
2410 AA_SFS_FILE_FOPS(".stacked", 0444, &seq_ns_stacked_fops),
2411 AA_SFS_FILE_FOPS(".ns_stacked", 0444, &seq_ns_nsstacked_fops),
2412 AA_SFS_FILE_FOPS(".ns_level", 0444, &seq_ns_level_fops),
2413 AA_SFS_FILE_FOPS(".ns_name", 0444, &seq_ns_name_fops),
2414 AA_SFS_FILE_FOPS("profiles", 0444, &aa_sfs_profiles_fops),
2415 AA_SFS_DIR("features", aa_sfs_entry_features),
2419 static struct aa_sfs_entry aa_sfs_entry =
2420 AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
2423 * entry_create_file - create a file entry in the apparmor securityfs
2424 * @fs_file: aa_sfs_entry to build an entry for (NOT NULL)
2425 * @parent: the parent dentry in the securityfs
2427 * Use entry_remove_file to remove entries created with this fn.
2429 static int __init entry_create_file(struct aa_sfs_entry *fs_file,
2430 struct dentry *parent)
2432 int error = 0;
2434 fs_file->dentry = securityfs_create_file(fs_file->name,
2435 S_IFREG | fs_file->mode,
2436 parent, fs_file,
2437 fs_file->file_ops);
2438 if (IS_ERR(fs_file->dentry)) {
2439 error = PTR_ERR(fs_file->dentry);
2440 fs_file->dentry = NULL;
2442 return error;
2445 static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
2447 * entry_create_dir - recursively create a directory entry in the securityfs
2448 * @fs_dir: aa_sfs_entry (and all child entries) to build (NOT NULL)
2449 * @parent: the parent dentry in the securityfs
2451 * Use entry_remove_dir to remove entries created with this fn.
2453 static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
2454 struct dentry *parent)
2456 struct aa_sfs_entry *fs_file;
2457 struct dentry *dir;
2458 int error;
2460 dir = securityfs_create_dir(fs_dir->name, parent);
2461 if (IS_ERR(dir))
2462 return PTR_ERR(dir);
2463 fs_dir->dentry = dir;
2465 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2466 if (fs_file->v_type == AA_SFS_TYPE_DIR)
2467 error = entry_create_dir(fs_file, fs_dir->dentry);
2468 else
2469 error = entry_create_file(fs_file, fs_dir->dentry);
2470 if (error)
2471 goto failed;
2474 return 0;
2476 failed:
2477 entry_remove_dir(fs_dir);
2479 return error;
2483 * entry_remove_file - drop a single file entry in the apparmor securityfs
2484 * @fs_file: aa_sfs_entry to detach from the securityfs (NOT NULL)
2486 static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
2488 if (!fs_file->dentry)
2489 return;
2491 securityfs_remove(fs_file->dentry);
2492 fs_file->dentry = NULL;
2496 * entry_remove_dir - recursively drop a directory entry from the securityfs
2497 * @fs_dir: aa_sfs_entry (and all child entries) to detach (NOT NULL)
2499 static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
2501 struct aa_sfs_entry *fs_file;
2503 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
2504 if (fs_file->v_type == AA_SFS_TYPE_DIR)
2505 entry_remove_dir(fs_file);
2506 else
2507 entry_remove_file(fs_file);
2510 entry_remove_file(fs_dir);
2514 * aa_destroy_aafs - cleanup and free aafs
2516 * releases dentries allocated by aa_create_aafs
2518 void __init aa_destroy_aafs(void)
2520 entry_remove_dir(&aa_sfs_entry);
2524 #define NULL_FILE_NAME ".null"
2525 struct path aa_null;
2527 static int aa_mk_null_file(struct dentry *parent)
2529 struct vfsmount *mount = NULL;
2530 struct dentry *dentry;
2531 struct inode *inode;
2532 int count = 0;
2533 int error = simple_pin_fs(parent->d_sb->s_type, &mount, &count);
2535 if (error)
2536 return error;
2538 inode_lock(d_inode(parent));
2539 dentry = lookup_one_len(NULL_FILE_NAME, parent, strlen(NULL_FILE_NAME));
2540 if (IS_ERR(dentry)) {
2541 error = PTR_ERR(dentry);
2542 goto out;
2544 inode = new_inode(parent->d_inode->i_sb);
2545 if (!inode) {
2546 error = -ENOMEM;
2547 goto out1;
2550 inode->i_ino = get_next_ino();
2551 inode->i_mode = S_IFCHR | S_IRUGO | S_IWUGO;
2552 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
2553 init_special_inode(inode, S_IFCHR | S_IRUGO | S_IWUGO,
2554 MKDEV(MEM_MAJOR, 3));
2555 d_instantiate(dentry, inode);
2556 aa_null.dentry = dget(dentry);
2557 aa_null.mnt = mntget(mount);
2559 error = 0;
2561 out1:
2562 dput(dentry);
2563 out:
2564 inode_unlock(d_inode(parent));
2565 simple_release_fs(&mount, &count);
2566 return error;
2571 static const char *policy_get_link(struct dentry *dentry,
2572 struct inode *inode,
2573 struct delayed_call *done)
2575 struct aa_ns *ns;
2576 struct path path;
2577 int error;
2579 if (!dentry)
2580 return ERR_PTR(-ECHILD);
2582 ns = aa_get_current_ns();
2583 path.mnt = mntget(aafs_mnt);
2584 path.dentry = dget(ns_dir(ns));
2585 error = nd_jump_link(&path);
2586 aa_put_ns(ns);
2588 return ERR_PTR(error);
2591 static int policy_readlink(struct dentry *dentry, char __user *buffer,
2592 int buflen)
2594 char name[32];
2595 int res;
2597 res = snprintf(name, sizeof(name), "%s:[%lu]", AAFS_NAME,
2598 d_inode(dentry)->i_ino);
2599 if (res > 0 && res < sizeof(name))
2600 res = readlink_copy(buffer, buflen, name);
2601 else
2602 res = -ENOENT;
2604 return res;
2607 static const struct inode_operations policy_link_iops = {
2608 .readlink = policy_readlink,
2609 .get_link = policy_get_link,
2614 * aa_create_aafs - create the apparmor security filesystem
2616 * dentries created here are released by aa_destroy_aafs
2618 * Returns: error on failure
2620 static int __init aa_create_aafs(void)
2622 struct dentry *dent;
2623 int error;
2625 if (!apparmor_initialized)
2626 return 0;
2628 if (aa_sfs_entry.dentry) {
2629 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
2630 return -EEXIST;
2633 /* setup apparmorfs used to virtualize policy/ */
2634 aafs_mnt = kern_mount(&aafs_ops);
2635 if (IS_ERR(aafs_mnt))
2636 panic("can't set apparmorfs up\n");
2637 aafs_mnt->mnt_sb->s_flags &= ~SB_NOUSER;
2639 /* Populate fs tree. */
2640 error = entry_create_dir(&aa_sfs_entry, NULL);
2641 if (error)
2642 goto error;
2644 dent = securityfs_create_file(".load", 0666, aa_sfs_entry.dentry,
2645 NULL, &aa_fs_profile_load);
2646 if (IS_ERR(dent))
2647 goto dent_error;
2648 ns_subload(root_ns) = dent;
2650 dent = securityfs_create_file(".replace", 0666, aa_sfs_entry.dentry,
2651 NULL, &aa_fs_profile_replace);
2652 if (IS_ERR(dent))
2653 goto dent_error;
2654 ns_subreplace(root_ns) = dent;
2656 dent = securityfs_create_file(".remove", 0666, aa_sfs_entry.dentry,
2657 NULL, &aa_fs_profile_remove);
2658 if (IS_ERR(dent))
2659 goto dent_error;
2660 ns_subremove(root_ns) = dent;
2662 dent = securityfs_create_file("revision", 0444, aa_sfs_entry.dentry,
2663 NULL, &aa_fs_ns_revision_fops);
2664 if (IS_ERR(dent))
2665 goto dent_error;
2666 ns_subrevision(root_ns) = dent;
2668 /* policy tree referenced by magic policy symlink */
2669 mutex_lock_nested(&root_ns->lock, root_ns->level);
2670 error = __aafs_ns_mkdir(root_ns, aafs_mnt->mnt_root, ".policy",
2671 aafs_mnt->mnt_root);
2672 mutex_unlock(&root_ns->lock);
2673 if (error)
2674 goto error;
2676 /* magic symlink similar to nsfs redirects based on task policy */
2677 dent = securityfs_create_symlink("policy", aa_sfs_entry.dentry,
2678 NULL, &policy_link_iops);
2679 if (IS_ERR(dent))
2680 goto dent_error;
2682 error = aa_mk_null_file(aa_sfs_entry.dentry);
2683 if (error)
2684 goto error;
2686 /* TODO: add default profile to apparmorfs */
2688 /* Report that AppArmor fs is enabled */
2689 aa_info_message("AppArmor Filesystem Enabled");
2690 return 0;
2692 dent_error:
2693 error = PTR_ERR(dent);
2694 error:
2695 aa_destroy_aafs();
2696 AA_ERROR("Error creating AppArmor securityfs\n");
2697 return error;
2700 fs_initcall(aa_create_aafs);