]> git.ipfire.org Git - thirdparty/linux.git/blob - security/apparmor/apparmorfs.c
Merge tag 'io_uring-5.7-2020-05-22' of git://git.kernel.dk/linux-block
[thirdparty/linux.git] / security / apparmor / apparmorfs.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AppArmor security module
4 *
5 * This file contains AppArmor /sys/kernel/security/apparmor interface functions
6 *
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
9 */
10
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>
27
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"
39
40 /*
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.
46 *
47 * fns and data are prefixed with
48 * aa_sfs_
49 *
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.
56 *
57 * fns and data are prefixed with
58 * aafs_
59 *
60 * The aa_fs_ prefix is used to indicate the fn is used by both the
61 * securityfs and apparmorfs filesystems.
62 */
63
64
65 /*
66 * support fns
67 */
68
69 struct rawdata_f_data {
70 struct aa_loaddata *loaddata;
71 };
72
73 #define RAWDATA_F_DATA_BUF(p) (char *)(p + 1)
74
75 static void rawdata_f_data_free(struct rawdata_f_data *private)
76 {
77 if (!private)
78 return;
79
80 aa_put_loaddata(private->loaddata);
81 kvfree(private);
82 }
83
84 static struct rawdata_f_data *rawdata_f_data_alloc(size_t size)
85 {
86 struct rawdata_f_data *ret;
87
88 if (size > SIZE_MAX - sizeof(*ret))
89 return ERR_PTR(-EINVAL);
90
91 ret = kvzalloc(sizeof(*ret) + size, GFP_KERNEL);
92 if (!ret)
93 return ERR_PTR(-ENOMEM);
94
95 return ret;
96 }
97
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)
102 *
103 * Returns: length of mangled name
104 */
105 static int mangle_name(const char *name, char *target)
106 {
107 char *t = target;
108
109 while (*name == '/' || *name == '.')
110 name++;
111
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;
120 }
121
122 *t = 0;
123 } else {
124 int len = 0;
125 for (; *name; name++) {
126 if (isalnum(*name) || isspace(*name) ||
127 strchr("/._-", *name))
128 len++;
129 }
130
131 return len;
132 }
133
134 return t - target;
135 }
136
137
138 /*
139 * aafs - core fns and data for the policy tree
140 */
141
142 #define AAFS_NAME "apparmorfs"
143 static struct vfsmount *aafs_mnt;
144 static int aafs_count;
145
146
147 static int aafs_show_path(struct seq_file *seq, struct dentry *dentry)
148 {
149 seq_printf(seq, "%s:[%lu]", AAFS_NAME, d_inode(dentry)->i_ino);
150 return 0;
151 }
152
153 static void aafs_free_inode(struct inode *inode)
154 {
155 if (S_ISLNK(inode->i_mode))
156 kfree(inode->i_link);
157 free_inode_nonrcu(inode);
158 }
159
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,
164 };
165
166 static int apparmorfs_fill_super(struct super_block *sb, struct fs_context *fc)
167 {
168 static struct tree_descr files[] = { {""} };
169 int error;
170
171 error = simple_fill_super(sb, AAFS_MAGIC, files);
172 if (error)
173 return error;
174 sb->s_op = &aafs_super_ops;
175
176 return 0;
177 }
178
179 static int apparmorfs_get_tree(struct fs_context *fc)
180 {
181 return get_tree_single(fc, apparmorfs_fill_super);
182 }
183
184 static const struct fs_context_operations apparmorfs_context_ops = {
185 .get_tree = apparmorfs_get_tree,
186 };
187
188 static int apparmorfs_init_fs_context(struct fs_context *fc)
189 {
190 fc->ops = &apparmorfs_context_ops;
191 return 0;
192 }
193
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,
199 };
200
201 /**
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
210 */
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)
215 {
216 struct inode *inode = new_inode(dir->i_sb);
217
218 AA_BUG(!dir);
219 AA_BUG(!dentry);
220
221 if (!inode)
222 return -ENOMEM;
223
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;
238 }
239 d_instantiate(dentry, inode);
240 dget(dentry);
241
242 return 0;
243 }
244
245 /**
246 * aafs_create - create a dentry in the apparmorfs filesystem
247 *
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
255 *
256 * This is the basic "create a xxx" function for apparmorfs.
257 *
258 * Returns a pointer to a dentry if it succeeds, that must be free with
259 * aafs_remove(). Will return ERR_PTR on failure.
260 */
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)
265 {
266 struct dentry *dentry;
267 struct inode *dir;
268 int error;
269
270 AA_BUG(!name);
271 AA_BUG(!parent);
272
273 if (!(mode & S_IFMT))
274 mode = (mode & S_IALLUGO) | S_IFREG;
275
276 error = simple_pin_fs(&aafs_ops, &aafs_mnt, &aafs_count);
277 if (error)
278 return ERR_PTR(error);
279
280 dir = d_inode(parent);
281
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;
287 }
288
289 if (d_really_is_positive(dentry)) {
290 error = -EEXIST;
291 goto fail_dentry;
292 }
293
294 error = __aafs_setup_d_inode(dir, dentry, mode, data, link, fops, iops);
295 if (error)
296 goto fail_dentry;
297 inode_unlock(dir);
298
299 return dentry;
300
301 fail_dentry:
302 dput(dentry);
303
304 fail_lock:
305 inode_unlock(dir);
306 simple_release_fs(&aafs_mnt, &aafs_count);
307
308 return ERR_PTR(error);
309 }
310
311 /**
312 * aafs_create_file - create a file in the apparmorfs filesystem
313 *
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
319 *
320 * see aafs_create
321 */
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)
325 {
326 return aafs_create(name, mode, parent, data, NULL, fops, NULL);
327 }
328
329 /**
330 * aafs_create_dir - create a directory in the apparmorfs filesystem
331 *
332 * @name: name of dentry to create
333 * @parent: parent directory for this dentry
334 *
335 * see aafs_create
336 */
337 static struct dentry *aafs_create_dir(const char *name, struct dentry *parent)
338 {
339 return aafs_create(name, S_IFDIR | 0755, parent, NULL, NULL, NULL,
340 NULL);
341 }
342
343 /**
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
350 *
351 * If @target parameter is %NULL, then the @iops parameter needs to be
352 * setup to handle .readlink and .get_link inode_operations.
353 */
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)
359 {
360 struct dentry *dent;
361 char *link = NULL;
362
363 if (target) {
364 if (!link)
365 return ERR_PTR(-ENOMEM);
366 }
367 dent = aafs_create(name, S_IFLNK | 0444, parent, private, link, NULL,
368 iops);
369 if (IS_ERR(dent))
370 kfree(link);
371
372 return dent;
373 }
374
375 /**
376 * aafs_remove - removes a file or directory from the apparmorfs filesystem
377 *
378 * @dentry: dentry of the file/directory/symlink to removed.
379 */
380 static void aafs_remove(struct dentry *dentry)
381 {
382 struct inode *dir;
383
384 if (!dentry || IS_ERR(dentry))
385 return;
386
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);
396 }
397 inode_unlock(dir);
398 simple_release_fs(&aafs_mnt, &aafs_count);
399 }
400
401
402 /*
403 * aa_fs - policy load/replace/remove
404 */
405
406 /**
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)
412 *
413 * Returns: kernel buffer containing copy of user buffer data or an
414 * ERR_PTR on failure.
415 */
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)
420 {
421 struct aa_loaddata *data;
422
423 AA_BUG(copy_size > alloc_size);
424
425 if (*pos != 0)
426 /* only writes from pos 0, that is complete writes */
427 return ERR_PTR(-ESPIPE);
428
429 /* freed by caller to simple_write_to_buffer */
430 data = aa_loaddata_alloc(alloc_size);
431 if (IS_ERR(data))
432 return data;
433
434 data->size = copy_size;
435 if (copy_from_user(data->data, userbuf, copy_size)) {
436 kvfree(data);
437 return ERR_PTR(-EFAULT);
438 }
439
440 return data;
441 }
442
443 static ssize_t policy_update(u32 mask, const char __user *buf, size_t size,
444 loff_t *pos, struct aa_ns *ns)
445 {
446 struct aa_loaddata *data;
447 struct aa_label *label;
448 ssize_t error;
449
450 label = begin_current_label_crit_section();
451
452 /* high level check about policy management - fine grained in
453 * below after unpack
454 */
455 error = aa_may_manage_policy(label, ns, mask);
456 if (error)
457 goto end_section;
458
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);
464 }
465 end_section:
466 end_current_label_crit_section(label);
467
468 return error;
469 }
470
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)
474 {
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);
477
478 aa_put_ns(ns);
479
480 return error;
481 }
482
483 static const struct file_operations aa_fs_profile_load = {
484 .write = profile_load,
485 .llseek = default_llseek,
486 };
487
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)
491 {
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);
496
497 return error;
498 }
499
500 static const struct file_operations aa_fs_profile_replace = {
501 .write = profile_replace,
502 .llseek = default_llseek,
503 };
504
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)
508 {
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);
513
514 label = begin_current_label_crit_section();
515 /* high level check about policy management - fine grained in
516 * below after unpack
517 */
518 error = aa_may_manage_policy(label, ns, AA_MAY_REMOVE_POLICY);
519 if (error)
520 goto out;
521
522 /*
523 * aa_remove_profile needs a null terminated string so 1 extra
524 * byte is allocated and the copied data is null terminated.
525 */
526 data = aa_simple_write_to_buffer(buf, size + 1, size, pos);
527
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);
533 }
534 out:
535 end_current_label_crit_section(label);
536 aa_put_ns(ns);
537 return error;
538 }
539
540 static const struct file_operations aa_fs_profile_remove = {
541 .write = profile_remove,
542 .llseek = default_llseek,
543 };
544
545 struct aa_revision {
546 struct aa_ns *ns;
547 long last_read;
548 };
549
550 /* revision file hook fn for policy loads */
551 static int ns_revision_release(struct inode *inode, struct file *file)
552 {
553 struct aa_revision *rev = file->private_data;
554
555 if (rev) {
556 aa_put_ns(rev->ns);
557 kfree(rev);
558 }
559
560 return 0;
561 }
562
563 static ssize_t ns_revision_read(struct file *file, char __user *buf,
564 size_t size, loff_t *ppos)
565 {
566 struct aa_revision *rev = file->private_data;
567 char buffer[32];
568 long last_read;
569 int avail;
570
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);
582 }
583
584 avail = sprintf(buffer, "%ld\n", rev->ns->revision);
585 if (*ppos + size > avail) {
586 rev->last_read = rev->ns->revision;
587 *ppos = 0;
588 }
589 mutex_unlock(&rev->ns->lock);
590
591 return simple_read_from_buffer(buf, size, ppos, buffer, avail);
592 }
593
594 static int ns_revision_open(struct inode *inode, struct file *file)
595 {
596 struct aa_revision *rev = kzalloc(sizeof(*rev), GFP_KERNEL);
597
598 if (!rev)
599 return -ENOMEM;
600
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;
605
606 return 0;
607 }
608
609 static __poll_t ns_revision_poll(struct file *file, poll_table *pt)
610 {
611 struct aa_revision *rev = file->private_data;
612 __poll_t mask = 0;
613
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);
620 }
621
622 return mask;
623 }
624
625 void __aa_bump_ns_revision(struct aa_ns *ns)
626 {
627 WRITE_ONCE(ns->revision, ns->revision + 1);
628 wake_up_interruptible(&ns->wait);
629 }
630
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,
638 };
639
640 static void profile_query_cb(struct aa_profile *profile, struct aa_perms *perms,
641 const char *match_str, size_t match_len)
642 {
643 struct aa_perms tmp = { };
644 struct aa_dfa *dfa;
645 unsigned int state = 0;
646
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 = { };
655
656 tmp = aa_compute_fperms(dfa, state, &cond);
657 }
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);
666 }
667 aa_apply_modes_to_perms(profile, &tmp);
668 aa_perms_accum_raw(perms, &tmp);
669 }
670
671
672 /**
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
678 *
679 * The buffers pointed to by buf and query may overlap. The query buffer is
680 * parsed before buf is written to.
681 *
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.
685 *
686 * Don't expect the contents of buf to be preserved on failure.
687 *
688 * Returns: number of characters written to buf or -errno on failure
689 */
690 static ssize_t query_data(char *buf, size_t buf_len,
691 char *query, size_t query_len)
692 {
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;
701
702 if (!query_len)
703 return -EINVAL; /* need a query */
704
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 */
710
711 if (buf_len < sizeof(bytes) + sizeof(blocks))
712 return -EINVAL; /* not enough space */
713
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);
719
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.
723 *
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.
727 */
728 memset(buf, 0, sizeof(bytes) + sizeof(blocks));
729 out = buf + sizeof(bytes) + sizeof(blocks);
730
731 blocks = 0;
732 label_for_each_confined(i, label, profile) {
733 if (!profile->data)
734 continue;
735
736 data = rhashtable_lookup_fast(profile->data, &key,
737 profile->data->p);
738
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 */
744 }
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++;
751 }
752 }
753 aa_put_label(label);
754
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));
759
760 return out - buf;
761 }
762
763 /**
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
770 *
771 * The buffers pointed to by buf and query may overlap. The query buffer is
772 * parsed before buf is written to.
773 *
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.
777 *
778 * LABEL_NAME must be NUL terminated. DFA_STRING may contain NUL characters
779 * but must *not* be NUL terminated.
780 *
781 * Returns: number of characters written to buf or -errno on failure
782 */
783 static ssize_t query_label(char *buf, size_t buf_len,
784 char *query, size_t query_len, bool view_only)
785 {
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;
792
793 if (!query_len)
794 return -EINVAL;
795
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;
800
801 /**
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.
806 */
807 match_str = label_name + label_name_len + 1;
808 match_len = query_len - label_name_len - 1;
809
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);
815
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);
820 }
821 } else {
822 label_for_each(i, label, profile) {
823 profile_query_cb(profile, &perms, match_str, match_len);
824 }
825 }
826 aa_put_label(label);
827
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);
831 }
832
833 /*
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.
839 */
840 struct multi_transaction {
841 struct kref count;
842 ssize_t size;
843 char data[0];
844 };
845
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);
849
850 static void multi_transaction_kref(struct kref *kref)
851 {
852 struct multi_transaction *t;
853
854 t = container_of(kref, struct multi_transaction, count);
855 free_page((unsigned long) t);
856 }
857
858 static struct multi_transaction *
859 get_multi_transaction(struct multi_transaction *t)
860 {
861 if (t)
862 kref_get(&(t->count));
863
864 return t;
865 }
866
867 static void put_multi_transaction(struct multi_transaction *t)
868 {
869 if (t)
870 kref_put(&(t->count), multi_transaction_kref);
871 }
872
873 /* does not increment @new's count */
874 static void multi_transaction_set(struct file *file,
875 struct multi_transaction *new, size_t n)
876 {
877 struct multi_transaction *old;
878
879 AA_BUG(n > MULTI_TRANSACTION_LIMIT);
880
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);
887 }
888
889 static struct multi_transaction *multi_transaction_new(struct file *file,
890 const char __user *buf,
891 size_t size)
892 {
893 struct multi_transaction *t;
894
895 if (size > MULTI_TRANSACTION_LIMIT - 1)
896 return ERR_PTR(-EFBIG);
897
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);
904
905 return t;
906 }
907
908 static ssize_t multi_transaction_read(struct file *file, char __user *buf,
909 size_t size, loff_t *pos)
910 {
911 struct multi_transaction *t;
912 ssize_t ret;
913
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;
919
920 ret = simple_read_from_buffer(buf, size, pos, t->data, t->size);
921 put_multi_transaction(t);
922
923 return ret;
924 }
925
926 static int multi_transaction_release(struct inode *inode, struct file *file)
927 {
928 put_multi_transaction(file->private_data);
929
930 return 0;
931 }
932
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
941
942 /**
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)
948 *
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.
952 *
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.
956 *
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.
960 *
961 * Returns: number of bytes written or -errno on failure
962 */
963 static ssize_t aa_write_access(struct file *file, const char __user *ubuf,
964 size_t count, loff_t *ppos)
965 {
966 struct multi_transaction *t;
967 ssize_t len;
968
969 if (*ppos)
970 return -ESPIPE;
971
972 t = multi_transaction_new(file, ubuf, count);
973 if (IS_ERR(t))
974 return PTR_ERR(t);
975
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;
999
1000 if (len < 0) {
1001 put_multi_transaction(t);
1002 return len;
1003 }
1004
1005 multi_transaction_set(file, t, len);
1006
1007 return count;
1008 }
1009
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,
1015 };
1016
1017 static int aa_sfs_seq_show(struct seq_file *seq, void *v)
1018 {
1019 struct aa_sfs_entry *fs_file = seq->private;
1020
1021 if (!fs_file)
1022 return 0;
1023
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;
1037 }
1038
1039 return 0;
1040 }
1041
1042 static int aa_sfs_seq_open(struct inode *inode, struct file *file)
1043 {
1044 return single_open(file, aa_sfs_seq_show, inode->i_private);
1045 }
1046
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,
1053 };
1054
1055 /*
1056 * profile based file operations
1057 * policy/profiles/XXXX/profiles/ *
1058 */
1059
1060 #define SEQ_PROFILE_FOPS(NAME) \
1061 static int seq_profile_ ##NAME ##_open(struct inode *inode, struct file *file)\
1062 { \
1063 return seq_profile_open(inode, file, seq_profile_ ##NAME ##_show); \
1064 } \
1065 \
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, \
1072 } \
1073
1074 static int seq_profile_open(struct inode *inode, struct file *file,
1075 int (*show)(struct seq_file *, void *))
1076 {
1077 struct aa_proxy *proxy = aa_get_proxy(inode->i_private);
1078 int error = single_open(file, show, proxy);
1079
1080 if (error) {
1081 file->private_data = NULL;
1082 aa_put_proxy(proxy);
1083 }
1084
1085 return error;
1086 }
1087
1088 static int seq_profile_release(struct inode *inode, struct file *file)
1089 {
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);
1094 }
1095
1096 static int seq_profile_name_show(struct seq_file *seq, void *v)
1097 {
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);
1103
1104 return 0;
1105 }
1106
1107 static int seq_profile_mode_show(struct seq_file *seq, void *v)
1108 {
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);
1114
1115 return 0;
1116 }
1117
1118 static int seq_profile_attach_show(struct seq_file *seq, void *v)
1119 {
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);
1130
1131 return 0;
1132 }
1133
1134 static int seq_profile_hash_show(struct seq_file *seq, void *v)
1135 {
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();
1140
1141 if (profile->hash) {
1142 for (i = 0; i < size; i++)
1143 seq_printf(seq, "%.2x", profile->hash[i]);
1144 seq_putc(seq, '\n');
1145 }
1146 aa_put_label(label);
1147
1148 return 0;
1149 }
1150
1151 SEQ_PROFILE_FOPS(name);
1152 SEQ_PROFILE_FOPS(mode);
1153 SEQ_PROFILE_FOPS(attach);
1154 SEQ_PROFILE_FOPS(hash);
1155
1156 /*
1157 * namespace based files
1158 * several root files and
1159 * policy/ *
1160 */
1161
1162 #define SEQ_NS_FOPS(NAME) \
1163 static int seq_ns_ ##NAME ##_open(struct inode *inode, struct file *file) \
1164 { \
1165 return single_open(file, seq_ns_ ##NAME ##_show, inode->i_private); \
1166 } \
1167 \
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, \
1174 } \
1175
1176 static int seq_ns_stacked_show(struct seq_file *seq, void *v)
1177 {
1178 struct aa_label *label;
1179
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);
1183
1184 return 0;
1185 }
1186
1187 static int seq_ns_nsstacked_show(struct seq_file *seq, void *v)
1188 {
1189 struct aa_label *label;
1190 struct aa_profile *profile;
1191 struct label_it it;
1192 int count = 1;
1193
1194 label = begin_current_label_crit_section();
1195
1196 if (label->size > 1) {
1197 label_for_each(it, label, profile)
1198 if (profile->ns != labels_ns(label)) {
1199 count++;
1200 break;
1201 }
1202 }
1203
1204 seq_printf(seq, "%s\n", count > 1 ? "yes" : "no");
1205 end_current_label_crit_section(label);
1206
1207 return 0;
1208 }
1209
1210 static int seq_ns_level_show(struct seq_file *seq, void *v)
1211 {
1212 struct aa_label *label;
1213
1214 label = begin_current_label_crit_section();
1215 seq_printf(seq, "%d\n", labels_ns(label)->level);
1216 end_current_label_crit_section(label);
1217
1218 return 0;
1219 }
1220
1221 static int seq_ns_name_show(struct seq_file *seq, void *v)
1222 {
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);
1226
1227 return 0;
1228 }
1229
1230 SEQ_NS_FOPS(stacked);
1231 SEQ_NS_FOPS(nsstacked);
1232 SEQ_NS_FOPS(level);
1233 SEQ_NS_FOPS(name);
1234
1235
1236 /* policy/raw_data/ * file ops */
1237
1238 #define SEQ_RAWDATA_FOPS(NAME) \
1239 static int seq_rawdata_ ##NAME ##_open(struct inode *inode, struct file *file)\
1240 { \
1241 return seq_rawdata_open(inode, file, seq_rawdata_ ##NAME ##_show); \
1242 } \
1243 \
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, \
1250 } \
1251
1252 static int seq_rawdata_open(struct inode *inode, struct file *file,
1253 int (*show)(struct seq_file *, void *))
1254 {
1255 struct aa_loaddata *data = __aa_get_loaddata(inode->i_private);
1256 int error;
1257
1258 if (!data)
1259 /* lost race this ent is being reaped */
1260 return -ENOENT;
1261
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);
1267 }
1268
1269 return error;
1270 }
1271
1272 static int seq_rawdata_release(struct inode *inode, struct file *file)
1273 {
1274 struct seq_file *seq = (struct seq_file *) file->private_data;
1275
1276 if (seq)
1277 aa_put_loaddata(seq->private);
1278
1279 return single_release(inode, file);
1280 }
1281
1282 static int seq_rawdata_abi_show(struct seq_file *seq, void *v)
1283 {
1284 struct aa_loaddata *data = seq->private;
1285
1286 seq_printf(seq, "v%d\n", data->abi);
1287
1288 return 0;
1289 }
1290
1291 static int seq_rawdata_revision_show(struct seq_file *seq, void *v)
1292 {
1293 struct aa_loaddata *data = seq->private;
1294
1295 seq_printf(seq, "%ld\n", data->revision);
1296
1297 return 0;
1298 }
1299
1300 static int seq_rawdata_hash_show(struct seq_file *seq, void *v)
1301 {
1302 struct aa_loaddata *data = seq->private;
1303 unsigned int i, size = aa_hash_size();
1304
1305 if (data->hash) {
1306 for (i = 0; i < size; i++)
1307 seq_printf(seq, "%.2x", data->hash[i]);
1308 seq_putc(seq, '\n');
1309 }
1310
1311 return 0;
1312 }
1313
1314 static int seq_rawdata_compressed_size_show(struct seq_file *seq, void *v)
1315 {
1316 struct aa_loaddata *data = seq->private;
1317
1318 seq_printf(seq, "%zu\n", data->compressed_size);
1319
1320 return 0;
1321 }
1322
1323 SEQ_RAWDATA_FOPS(abi);
1324 SEQ_RAWDATA_FOPS(revision);
1325 SEQ_RAWDATA_FOPS(hash);
1326 SEQ_RAWDATA_FOPS(compressed_size);
1327
1328 static int deflate_decompress(char *src, size_t slen, char *dst, size_t dlen)
1329 {
1330 int error;
1331 struct z_stream_s strm;
1332
1333 if (aa_g_rawdata_compression_level == 0) {
1334 if (dlen < slen)
1335 return -EINVAL;
1336 memcpy(dst, src, slen);
1337 return 0;
1338 }
1339
1340 memset(&strm, 0, sizeof(strm));
1341
1342 strm.workspace = kvzalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
1343 if (!strm.workspace)
1344 return -ENOMEM;
1345
1346 strm.next_in = src;
1347 strm.avail_in = slen;
1348
1349 error = zlib_inflateInit(&strm);
1350 if (error != Z_OK) {
1351 error = -ENOMEM;
1352 goto fail_inflate_init;
1353 }
1354
1355 strm.next_out = dst;
1356 strm.avail_out = dlen;
1357
1358 error = zlib_inflate(&strm, Z_FINISH);
1359 if (error != Z_STREAM_END)
1360 error = -EINVAL;
1361 else
1362 error = 0;
1363
1364 zlib_inflateEnd(&strm);
1365 fail_inflate_init:
1366 kvfree(strm.workspace);
1367 return error;
1368 }
1369
1370 static ssize_t rawdata_read(struct file *file, char __user *buf, size_t size,
1371 loff_t *ppos)
1372 {
1373 struct rawdata_f_data *private = file->private_data;
1374
1375 return simple_read_from_buffer(buf, size, ppos,
1376 RAWDATA_F_DATA_BUF(private),
1377 private->loaddata->size);
1378 }
1379
1380 static int rawdata_release(struct inode *inode, struct file *file)
1381 {
1382 rawdata_f_data_free(file->private_data);
1383
1384 return 0;
1385 }
1386
1387 static int rawdata_open(struct inode *inode, struct file *file)
1388 {
1389 int error;
1390 struct aa_loaddata *loaddata;
1391 struct rawdata_f_data *private;
1392
1393 if (!policy_view_capable(NULL))
1394 return -EACCES;
1395
1396 loaddata = __aa_get_loaddata(inode->i_private);
1397 if (!loaddata)
1398 /* lost race: this entry is being reaped */
1399 return -ENOENT;
1400
1401 private = rawdata_f_data_alloc(loaddata->size);
1402 if (IS_ERR(private)) {
1403 error = PTR_ERR(private);
1404 goto fail_private_alloc;
1405 }
1406
1407 private->loaddata = loaddata;
1408
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;
1414
1415 file->private_data = private;
1416 return 0;
1417
1418 fail_decompress:
1419 rawdata_f_data_free(private);
1420 return error;
1421
1422 fail_private_alloc:
1423 aa_put_loaddata(loaddata);
1424 return error;
1425 }
1426
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,
1432 };
1433
1434 static void remove_rawdata_dents(struct aa_loaddata *rawdata)
1435 {
1436 int i;
1437
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;
1443 }
1444 }
1445 }
1446
1447 void __aa_fs_remove_rawdata(struct aa_loaddata *rawdata)
1448 {
1449 AA_BUG(rawdata->ns && !mutex_is_locked(&rawdata->ns->lock));
1450
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;
1456 }
1457 }
1458
1459 int __aa_fs_create_rawdata(struct aa_ns *ns, struct aa_loaddata *rawdata)
1460 {
1461 struct dentry *dent, *dir;
1462
1463 AA_BUG(!ns);
1464 AA_BUG(!rawdata);
1465 AA_BUG(!mutex_is_locked(&ns->lock));
1466 AA_BUG(!ns_subdata_dir(ns));
1467
1468 /*
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
1472 */
1473 rawdata->name = kasprintf(GFP_KERNEL, "%ld", ns->revision);
1474 if (!rawdata->name)
1475 return -ENOMEM;
1476
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;
1482
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;
1488
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;
1494
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;
1501 }
1502
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;
1509
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;
1516
1517 rawdata->ns = aa_get_ns(ns);
1518 list_add(&rawdata->list, &ns->rawdata_list);
1519 /* no refcount on inode rawdata */
1520
1521 return 0;
1522
1523 fail:
1524 remove_rawdata_dents(rawdata);
1525
1526 return PTR_ERR(dent);
1527 }
1528
1529 /** fns to setup dynamic per profile/namespace files **/
1530
1531 /**
1532 *
1533 * Requires: @profile->ns->lock held
1534 */
1535 void __aafs_profile_rmdir(struct aa_profile *profile)
1536 {
1537 struct aa_profile *child;
1538 int i;
1539
1540 if (!profile)
1541 return;
1542
1543 list_for_each_entry(child, &profile->base.profiles, base.list)
1544 __aafs_profile_rmdir(child);
1545
1546 for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
1547 struct aa_proxy *proxy;
1548 if (!profile->dents[i])
1549 continue;
1550
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;
1555 }
1556 }
1557
1558 /**
1559 *
1560 * Requires: @old->ns->lock held
1561 */
1562 void __aafs_profile_migrate_dents(struct aa_profile *old,
1563 struct aa_profile *new)
1564 {
1565 int i;
1566
1567 AA_BUG(!old);
1568 AA_BUG(!new);
1569 AA_BUG(!mutex_is_locked(&profiles_ns(old)->lock));
1570
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;
1576 }
1577 }
1578
1579 static struct dentry *create_profile_file(struct dentry *dir, const char *name,
1580 struct aa_profile *profile,
1581 const struct file_operations *fops)
1582 {
1583 struct aa_proxy *proxy = aa_get_proxy(profile->label.proxy);
1584 struct dentry *dent;
1585
1586 dent = aafs_create_file(name, S_IFREG | 0444, dir, proxy, fops);
1587 if (IS_ERR(dent))
1588 aa_put_proxy(proxy);
1589
1590 return dent;
1591 }
1592
1593 static int profile_depth(struct aa_profile *profile)
1594 {
1595 int depth = 0;
1596
1597 rcu_read_lock();
1598 for (depth = 0; profile; profile = rcu_access_pointer(profile->parent))
1599 depth++;
1600 rcu_read_unlock();
1601
1602 return depth;
1603 }
1604
1605 static char *gen_symlink_name(int depth, const char *dirname, const char *fname)
1606 {
1607 char *buffer, *s;
1608 int error;
1609 int size = depth * 6 + strlen(dirname) + strlen(fname) + 11;
1610
1611 s = buffer = kmalloc(size, GFP_KERNEL);
1612 if (!buffer)
1613 return ERR_PTR(-ENOMEM);
1614
1615 for (; depth > 0; depth--) {
1616 strcpy(s, "../../");
1617 s += 6;
1618 size -= 6;
1619 }
1620
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);
1625 }
1626
1627 return buffer;
1628 }
1629
1630 static void rawdata_link_cb(void *arg)
1631 {
1632 kfree(arg);
1633 }
1634
1635 static const char *rawdata_get_link_base(struct dentry *dentry,
1636 struct inode *inode,
1637 struct delayed_call *done,
1638 const char *name)
1639 {
1640 struct aa_proxy *proxy = inode->i_private;
1641 struct aa_label *label;
1642 struct aa_profile *profile;
1643 char *target;
1644 int depth;
1645
1646 if (!dentry)
1647 return ERR_PTR(-ECHILD);
1648
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);
1654
1655 if (IS_ERR(target))
1656 return target;
1657
1658 set_delayed_call(done, rawdata_link_cb, target);
1659
1660 return target;
1661 }
1662
1663 static const char *rawdata_get_link_sha1(struct dentry *dentry,
1664 struct inode *inode,
1665 struct delayed_call *done)
1666 {
1667 return rawdata_get_link_base(dentry, inode, done, "sha1");
1668 }
1669
1670 static const char *rawdata_get_link_abi(struct dentry *dentry,
1671 struct inode *inode,
1672 struct delayed_call *done)
1673 {
1674 return rawdata_get_link_base(dentry, inode, done, "abi");
1675 }
1676
1677 static const char *rawdata_get_link_data(struct dentry *dentry,
1678 struct inode *inode,
1679 struct delayed_call *done)
1680 {
1681 return rawdata_get_link_base(dentry, inode, done, "raw_data");
1682 }
1683
1684 static const struct inode_operations rawdata_link_sha1_iops = {
1685 .get_link = rawdata_get_link_sha1,
1686 };
1687
1688 static const struct inode_operations rawdata_link_abi_iops = {
1689 .get_link = rawdata_get_link_abi,
1690 };
1691 static const struct inode_operations rawdata_link_data_iops = {
1692 .get_link = rawdata_get_link_data,
1693 };
1694
1695
1696 /*
1697 * Requires: @profile->ns->lock held
1698 */
1699 int __aafs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
1700 {
1701 struct aa_profile *child;
1702 struct dentry *dent = NULL, *dir;
1703 int error;
1704
1705 AA_BUG(!profile);
1706 AA_BUG(!mutex_is_locked(&profiles_ns(profile)->lock));
1707
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;
1717 }
1718
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);
1723
1724 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
1725 if (!profile->dirname) {
1726 error = -ENOMEM;
1727 goto fail2;
1728 }
1729
1730 mangle_name(profile->base.name, profile->dirname);
1731 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
1732 }
1733
1734 dent = aafs_create_dir(profile->dirname, parent);
1735 if (IS_ERR(dent))
1736 goto fail;
1737 prof_dir(profile) = dir = dent;
1738
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;
1744
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;
1750
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;
1756
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;
1763 }
1764
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;
1773
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;
1781
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;
1789 }
1790
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;
1795 }
1796
1797 return 0;
1798
1799 fail:
1800 error = PTR_ERR(dent);
1801
1802 fail2:
1803 __aafs_profile_rmdir(profile);
1804
1805 return error;
1806 }
1807
1808 static int ns_mkdir_op(struct inode *dir, struct dentry *dentry, umode_t mode)
1809 {
1810 struct aa_ns *ns, *parent;
1811 /* TODO: improve permission check */
1812 struct aa_label *label;
1813 int error;
1814
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;
1820
1821 parent = aa_get_ns(dir->i_private);
1822 AA_BUG(d_inode(ns_subns_dir(parent)) != dir);
1823
1824 /* we have to unlock and then relock to get locking order right
1825 * for pin_fs
1826 */
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;
1833
1834 error = __aafs_setup_d_inode(dir, dentry, mode | S_IFDIR, NULL,
1835 NULL, NULL, NULL);
1836 if (error)
1837 goto out_pin;
1838
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;
1844 }
1845
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);
1853
1854 return error;
1855 }
1856
1857 static int ns_rmdir_op(struct inode *dir, struct dentry *dentry)
1858 {
1859 struct aa_ns *ns, *parent;
1860 /* TODO: improve permission check */
1861 struct aa_label *label;
1862 int error;
1863
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;
1869
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.
1874 */
1875 inode_unlock(dir);
1876 inode_unlock(dentry->d_inode);
1877
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;
1884 }
1885 AA_BUG(ns_dir(ns) != dentry);
1886
1887 __aa_remove_ns(ns);
1888 aa_put_ns(ns);
1889
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);
1895
1896 return error;
1897 }
1898
1899 static const struct inode_operations ns_dir_inode_operations = {
1900 .lookup = simple_lookup,
1901 .mkdir = ns_mkdir_op,
1902 .rmdir = ns_rmdir_op,
1903 };
1904
1905 static void __aa_fs_list_remove_rawdata(struct aa_ns *ns)
1906 {
1907 struct aa_loaddata *ent, *tmp;
1908
1909 AA_BUG(!mutex_is_locked(&ns->lock));
1910
1911 list_for_each_entry_safe(ent, tmp, &ns->rawdata_list, list)
1912 __aa_fs_remove_rawdata(ent);
1913 }
1914
1915 /**
1916 *
1917 * Requires: @ns->lock held
1918 */
1919 void __aafs_ns_rmdir(struct aa_ns *ns)
1920 {
1921 struct aa_ns *sub;
1922 struct aa_profile *child;
1923 int i;
1924
1925 if (!ns)
1926 return;
1927 AA_BUG(!mutex_is_locked(&ns->lock));
1928
1929 list_for_each_entry(child, &ns->base.profiles, base.list)
1930 __aafs_profile_rmdir(child);
1931
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);
1936 }
1937
1938 __aa_fs_list_remove_rawdata(ns);
1939
1940 if (ns_subns_dir(ns)) {
1941 sub = d_inode(ns_subns_dir(ns))->i_private;
1942 aa_put_ns(sub);
1943 }
1944 if (ns_subload(ns)) {
1945 sub = d_inode(ns_subload(ns))->i_private;
1946 aa_put_ns(sub);
1947 }
1948 if (ns_subreplace(ns)) {
1949 sub = d_inode(ns_subreplace(ns))->i_private;
1950 aa_put_ns(sub);
1951 }
1952 if (ns_subremove(ns)) {
1953 sub = d_inode(ns_subremove(ns))->i_private;
1954 aa_put_ns(sub);
1955 }
1956 if (ns_subrevision(ns)) {
1957 sub = d_inode(ns_subrevision(ns))->i_private;
1958 aa_put_ns(sub);
1959 }
1960
1961 for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
1962 aafs_remove(ns->dents[i]);
1963 ns->dents[i] = NULL;
1964 }
1965 }
1966
1967 /* assumes cleanup in caller */
1968 static int __aafs_ns_mkdir_entries(struct aa_ns *ns, struct dentry *dir)
1969 {
1970 struct dentry *dent;
1971
1972 AA_BUG(!ns);
1973 AA_BUG(!dir);
1974
1975 dent = aafs_create_dir("profiles", dir);
1976 if (IS_ERR(dent))
1977 return PTR_ERR(dent);
1978 ns_subprofs_dir(ns) = dent;
1979
1980 dent = aafs_create_dir("raw_data", dir);
1981 if (IS_ERR(dent))
1982 return PTR_ERR(dent);
1983 ns_subdata_dir(ns) = dent;
1984
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;
1991
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;
1998
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;
2005
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;
2012
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;
2020
2021 return 0;
2022 }
2023
2024 /*
2025 * Requires: @ns->lock held
2026 */
2027 int __aafs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name,
2028 struct dentry *dent)
2029 {
2030 struct aa_ns *sub;
2031 struct aa_profile *child;
2032 struct dentry *dir;
2033 int error;
2034
2035 AA_BUG(!ns);
2036 AA_BUG(!parent);
2037 AA_BUG(!mutex_is_locked(&ns->lock));
2038
2039 if (!name)
2040 name = ns->base.name;
2041
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;
2053
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;
2059 }
2060
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;
2068 }
2069
2070 return 0;
2071
2072 fail:
2073 error = PTR_ERR(dent);
2074
2075 fail2:
2076 __aafs_ns_rmdir(ns);
2077
2078 return error;
2079 }
2080
2081
2082 #define list_entry_is_head(pos, head, member) (&pos->member == (head))
2083
2084 /**
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)
2088 *
2089 * Find the next namespace from @ns under @root and handle all locking needed
2090 * while switching current namespace.
2091 *
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
2095 */
2096 static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
2097 {
2098 struct aa_ns *parent, *next;
2099
2100 AA_BUG(!root);
2101 AA_BUG(!ns);
2102 AA_BUG(ns != root && !mutex_is_locked(&ns->parent->lock));
2103
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;
2109 }
2110
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;
2119 }
2120 ns = parent;
2121 parent = parent->parent;
2122 }
2123
2124 return NULL;
2125 }
2126
2127 /**
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)
2131 *
2132 * Returns: unrefcounted profile or NULL if no profile
2133 * Requires: profile->ns.lock to be held
2134 */
2135 static struct aa_profile *__first_profile(struct aa_ns *root,
2136 struct aa_ns *ns)
2137 {
2138 AA_BUG(!root);
2139 AA_BUG(ns && !mutex_is_locked(&ns->lock));
2140
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);
2145 }
2146 return NULL;
2147 }
2148
2149 /**
2150 * __next_profile - step to the next profile in a profile tree
2151 * @profile: current profile in tree (NOT NULL)
2152 *
2153 * Perform a depth first traversal on the profile tree in a namespace
2154 *
2155 * Returns: next profile or NULL if done
2156 * Requires: profile->ns.lock to be held
2157 */
2158 static struct aa_profile *__next_profile(struct aa_profile *p)
2159 {
2160 struct aa_profile *parent;
2161 struct aa_ns *ns = p->ns;
2162
2163 AA_BUG(!mutex_is_locked(&profiles_ns(p)->lock));
2164
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);
2169
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));
2180 }
2181
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;
2186
2187 return NULL;
2188 }
2189
2190 /**
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)
2194 *
2195 * Returns: next profile or NULL if there isn't one
2196 */
2197 static struct aa_profile *next_profile(struct aa_ns *root,
2198 struct aa_profile *profile)
2199 {
2200 struct aa_profile *next = __next_profile(profile);
2201 if (next)
2202 return next;
2203
2204 /* finished all profiles in namespace move to next namespace */
2205 return __first_profile(root, __next_ns(root, profile->ns));
2206 }
2207
2208 /**
2209 * p_start - start a depth first traversal of profile tree
2210 * @f: seq_file to fill
2211 * @pos: current position
2212 *
2213 * Returns: first profile under current namespace or NULL if none found
2214 *
2215 * acquires first ns->lock
2216 */
2217 static void *p_start(struct seq_file *f, loff_t *pos)
2218 {
2219 struct aa_profile *profile = NULL;
2220 struct aa_ns *root = aa_get_current_ns();
2221 loff_t l = *pos;
2222 f->private = root;
2223
2224 /* find the first profile */
2225 mutex_lock_nested(&root->lock, root->level);
2226 profile = __first_profile(root, root);
2227
2228 /* skip to position */
2229 for (; profile && l > 0; l--)
2230 profile = next_profile(root, profile);
2231
2232 return profile;
2233 }
2234
2235 /**
2236 * p_next - read the next profile entry
2237 * @f: seq_file to fill
2238 * @p: profile previously returned
2239 * @pos: current position
2240 *
2241 * Returns: next profile after @p or NULL if none
2242 *
2243 * may acquire/release locks in namespace tree as necessary
2244 */
2245 static void *p_next(struct seq_file *f, void *p, loff_t *pos)
2246 {
2247 struct aa_profile *profile = p;
2248 struct aa_ns *ns = f->private;
2249 (*pos)++;
2250
2251 return next_profile(ns, profile);
2252 }
2253
2254 /**
2255 * p_stop - stop depth first traversal
2256 * @f: seq_file we are filling
2257 * @p: the last profile writen
2258 *
2259 * Release all locking done by p_start/p_next on namespace tree
2260 */
2261 static void p_stop(struct seq_file *f, void *p)
2262 {
2263 struct aa_profile *profile = p;
2264 struct aa_ns *root = f->private, *ns;
2265
2266 if (profile) {
2267 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
2268 mutex_unlock(&ns->lock);
2269 }
2270 mutex_unlock(&root->lock);
2271 aa_put_ns(root);
2272 }
2273
2274 /**
2275 * seq_show_profile - show a profile entry
2276 * @f: seq_file to file
2277 * @p: current position (profile) (NOT NULL)
2278 *
2279 * Returns: error on failure
2280 */
2281 static int seq_show_profile(struct seq_file *f, void *p)
2282 {
2283 struct aa_profile *profile = (struct aa_profile *)p;
2284 struct aa_ns *root = f->private;
2285
2286 aa_label_seq_xprint(f, root, &profile->label,
2287 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS, GFP_KERNEL);
2288 seq_putc(f, '\n');
2289
2290 return 0;
2291 }
2292
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,
2298 };
2299
2300 static int profiles_open(struct inode *inode, struct file *file)
2301 {
2302 if (!policy_view_capable(NULL))
2303 return -EACCES;
2304
2305 return seq_open(file, &aa_sfs_profiles_op);
2306 }
2307
2308 static int profiles_release(struct inode *inode, struct file *file)
2309 {
2310 return seq_release(inode, file);
2311 }
2312
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,
2318 };
2319
2320
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"),
2325 { }
2326 };
2327
2328 static struct aa_sfs_entry aa_sfs_entry_ptrace[] = {
2329 AA_SFS_FILE_STRING("mask", "read trace"),
2330 { }
2331 };
2332
2333 static struct aa_sfs_entry aa_sfs_entry_signal[] = {
2334 AA_SFS_FILE_STRING("mask", AA_SFS_SIG_MASK),
2335 { }
2336 };
2337
2338 static struct aa_sfs_entry aa_sfs_entry_attach[] = {
2339 AA_SFS_FILE_BOOLEAN("xattr", 1),
2340 { }
2341 };
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"),
2353 { }
2354 };
2355
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),
2361 { }
2362 };
2363
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),
2367 { }
2368 };
2369
2370 static struct aa_sfs_entry aa_sfs_entry_mount[] = {
2371 AA_SFS_FILE_STRING("mask", "mount umount pivot_root"),
2372 { }
2373 };
2374
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),
2378 { }
2379 };
2380
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),
2385 { }
2386 };
2387
2388 static struct aa_sfs_entry aa_sfs_entry_query[] = {
2389 AA_SFS_DIR("label", aa_sfs_entry_query_label),
2390 { }
2391 };
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),
2405 { }
2406 };
2407
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),
2416 { }
2417 };
2418
2419 static struct aa_sfs_entry aa_sfs_entry =
2420 AA_SFS_DIR("apparmor", aa_sfs_entry_apparmor);
2421
2422 /**
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
2426 *
2427 * Use entry_remove_file to remove entries created with this fn.
2428 */
2429 static int __init entry_create_file(struct aa_sfs_entry *fs_file,
2430 struct dentry *parent)
2431 {
2432 int error = 0;
2433
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;
2441 }
2442 return error;
2443 }
2444
2445 static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir);
2446 /**
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
2450 *
2451 * Use entry_remove_dir to remove entries created with this fn.
2452 */
2453 static int __init entry_create_dir(struct aa_sfs_entry *fs_dir,
2454 struct dentry *parent)
2455 {
2456 struct aa_sfs_entry *fs_file;
2457 struct dentry *dir;
2458 int error;
2459
2460 dir = securityfs_create_dir(fs_dir->name, parent);
2461 if (IS_ERR(dir))
2462 return PTR_ERR(dir);
2463 fs_dir->dentry = dir;
2464
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;
2472 }
2473
2474 return 0;
2475
2476 failed:
2477 entry_remove_dir(fs_dir);
2478
2479 return error;
2480 }
2481
2482 /**
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)
2485 */
2486 static void __init entry_remove_file(struct aa_sfs_entry *fs_file)
2487 {
2488 if (!fs_file->dentry)
2489 return;
2490
2491 securityfs_remove(fs_file->dentry);
2492 fs_file->dentry = NULL;
2493 }
2494
2495 /**
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)
2498 */
2499 static void __init entry_remove_dir(struct aa_sfs_entry *fs_dir)
2500 {
2501 struct aa_sfs_entry *fs_file;
2502
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);
2508 }
2509
2510 entry_remove_file(fs_dir);
2511 }
2512
2513 /**
2514 * aa_destroy_aafs - cleanup and free aafs
2515 *
2516 * releases dentries allocated by aa_create_aafs
2517 */
2518 void __init aa_destroy_aafs(void)
2519 {
2520 entry_remove_dir(&aa_sfs_entry);
2521 }
2522
2523
2524 #define NULL_FILE_NAME ".null"
2525 struct path aa_null;
2526
2527 static int aa_mk_null_file(struct dentry *parent)
2528 {
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);
2534
2535 if (error)
2536 return error;
2537
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;
2543 }
2544 inode = new_inode(parent->d_inode->i_sb);
2545 if (!inode) {
2546 error = -ENOMEM;
2547 goto out1;
2548 }
2549
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);
2558
2559 error = 0;
2560
2561 out1:
2562 dput(dentry);
2563 out:
2564 inode_unlock(d_inode(parent));
2565 simple_release_fs(&mount, &count);
2566 return error;
2567 }
2568
2569
2570
2571 static const char *policy_get_link(struct dentry *dentry,
2572 struct inode *inode,
2573 struct delayed_call *done)
2574 {
2575 struct aa_ns *ns;
2576 struct path path;
2577 int error;
2578
2579 if (!dentry)
2580 return ERR_PTR(-ECHILD);
2581
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);
2587
2588 return ERR_PTR(error);
2589 }
2590
2591 static int policy_readlink(struct dentry *dentry, char __user *buffer,
2592 int buflen)
2593 {
2594 char name[32];
2595 int res;
2596
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;
2603
2604 return res;
2605 }
2606
2607 static const struct inode_operations policy_link_iops = {
2608 .readlink = policy_readlink,
2609 .get_link = policy_get_link,
2610 };
2611
2612
2613 /**
2614 * aa_create_aafs - create the apparmor security filesystem
2615 *
2616 * dentries created here are released by aa_destroy_aafs
2617 *
2618 * Returns: error on failure
2619 */
2620 static int __init aa_create_aafs(void)
2621 {
2622 struct dentry *dent;
2623 int error;
2624
2625 if (!apparmor_initialized)
2626 return 0;
2627
2628 if (aa_sfs_entry.dentry) {
2629 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
2630 return -EEXIST;
2631 }
2632
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;
2638
2639 /* Populate fs tree. */
2640 error = entry_create_dir(&aa_sfs_entry, NULL);
2641 if (error)
2642 goto error;
2643
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;
2649
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;
2655
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;
2661
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;
2667
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;
2675
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;
2681
2682 error = aa_mk_null_file(aa_sfs_entry.dentry);
2683 if (error)
2684 goto error;
2685
2686 /* TODO: add default profile to apparmorfs */
2687
2688 /* Report that AppArmor fs is enabled */
2689 aa_info_message("AppArmor Filesystem Enabled");
2690 return 0;
2691
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;
2698 }
2699
2700 fs_initcall(aa_create_aafs);