blob: 4409b63f0dd7f265e80c64a004052f52da50381a [file] [log] [blame]
John Johansen63e2b422010-07-29 14:48:03 -07001/*
2 * AppArmor security module
3 *
4 * This file contains AppArmor /sys/kernel/security/apparmor interface functions
5 *
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
13 */
14
John Johansen0d259f02013-07-10 21:13:43 -070015#include <linux/ctype.h>
John Johansen63e2b422010-07-29 14:48:03 -070016#include <linux/security.h>
17#include <linux/vmalloc.h>
18#include <linux/module.h>
19#include <linux/seq_file.h>
20#include <linux/uaccess.h>
21#include <linux/namei.h>
Kees Cooke74abcf2012-01-26 16:29:21 -080022#include <linux/capability.h>
John Johansen29b38222013-07-10 21:18:43 -070023#include <linux/rcupdate.h>
John Johansen63e2b422010-07-29 14:48:03 -070024
25#include "include/apparmor.h"
26#include "include/apparmorfs.h"
27#include "include/audit.h"
28#include "include/context.h"
John Johansenf8eb8a12013-08-14 11:27:36 -070029#include "include/crypto.h"
John Johansen63e2b422010-07-29 14:48:03 -070030#include "include/policy.h"
John Johansencff281f2017-01-16 00:42:15 -080031#include "include/policy_ns.h"
Kees Cookd384b0a2012-01-26 16:29:23 -080032#include "include/resource.h"
John Johansen63e2b422010-07-29 14:48:03 -070033
34/**
John Johansen0d259f02013-07-10 21:13:43 -070035 * aa_mangle_name - mangle a profile name to std profile layout form
36 * @name: profile name to mangle (NOT NULL)
37 * @target: buffer to store mangled name, same length as @name (MAYBE NULL)
38 *
39 * Returns: length of mangled name
40 */
41static int mangle_name(char *name, char *target)
42{
43 char *t = target;
44
45 while (*name == '/' || *name == '.')
46 name++;
47
48 if (target) {
49 for (; *name; name++) {
50 if (*name == '/')
51 *(t)++ = '.';
52 else if (isspace(*name))
53 *(t)++ = '_';
54 else if (isalnum(*name) || strchr("._-", *name))
55 *(t)++ = *name;
56 }
57
58 *t = 0;
59 } else {
60 int len = 0;
61 for (; *name; name++) {
62 if (isalnum(*name) || isspace(*name) ||
63 strchr("/._-", *name))
64 len++;
65 }
66
67 return len;
68 }
69
70 return t - target;
71}
72
73/**
John Johansen63e2b422010-07-29 14:48:03 -070074 * aa_simple_write_to_buffer - common routine for getting policy from user
75 * @op: operation doing the user buffer copy
76 * @userbuf: user buffer to copy data from (NOT NULL)
John Johansen3ed02ad2010-10-09 00:47:53 -070077 * @alloc_size: size of user buffer (REQUIRES: @alloc_size >= @copy_size)
John Johansen63e2b422010-07-29 14:48:03 -070078 * @copy_size: size of data to copy from user buffer
79 * @pos: position write is at in the file (NOT NULL)
80 *
81 * Returns: kernel buffer containing copy of user buffer data or an
82 * ERR_PTR on failure.
83 */
84static char *aa_simple_write_to_buffer(int op, const char __user *userbuf,
85 size_t alloc_size, size_t copy_size,
86 loff_t *pos)
87{
88 char *data;
89
John Johansen3ed02ad2010-10-09 00:47:53 -070090 BUG_ON(copy_size > alloc_size);
91
John Johansen63e2b422010-07-29 14:48:03 -070092 if (*pos != 0)
93 /* only writes from pos 0, that is complete writes */
94 return ERR_PTR(-ESPIPE);
95
96 /*
97 * Don't allow profile load/replace/remove from profiles that don't
98 * have CAP_MAC_ADMIN
99 */
100 if (!aa_may_manage_policy(op))
101 return ERR_PTR(-EACCES);
102
103 /* freed by caller to simple_write_to_buffer */
104 data = kvmalloc(alloc_size);
105 if (data == NULL)
106 return ERR_PTR(-ENOMEM);
107
108 if (copy_from_user(data, userbuf, copy_size)) {
109 kvfree(data);
110 return ERR_PTR(-EFAULT);
111 }
112
113 return data;
114}
115
116
117/* .load file hook fn to load policy */
118static ssize_t profile_load(struct file *f, const char __user *buf, size_t size,
119 loff_t *pos)
120{
121 char *data;
122 ssize_t error;
123
124 data = aa_simple_write_to_buffer(OP_PROF_LOAD, buf, size, size, pos);
125
126 error = PTR_ERR(data);
127 if (!IS_ERR(data)) {
128 error = aa_replace_profiles(data, size, PROF_ADD);
129 kvfree(data);
130 }
131
132 return error;
133}
134
135static const struct file_operations aa_fs_profile_load = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200136 .write = profile_load,
137 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700138};
139
140/* .replace file hook fn to load and/or replace policy */
141static ssize_t profile_replace(struct file *f, const char __user *buf,
142 size_t size, loff_t *pos)
143{
144 char *data;
145 ssize_t error;
146
147 data = aa_simple_write_to_buffer(OP_PROF_REPL, buf, size, size, pos);
148 error = PTR_ERR(data);
149 if (!IS_ERR(data)) {
150 error = aa_replace_profiles(data, size, PROF_REPLACE);
151 kvfree(data);
152 }
153
154 return error;
155}
156
157static const struct file_operations aa_fs_profile_replace = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200158 .write = profile_replace,
159 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700160};
161
162/* .remove file hook fn to remove loaded policy */
163static ssize_t profile_remove(struct file *f, const char __user *buf,
164 size_t size, loff_t *pos)
165{
166 char *data;
167 ssize_t error;
168
169 /*
170 * aa_remove_profile needs a null terminated string so 1 extra
171 * byte is allocated and the copied data is null terminated.
172 */
173 data = aa_simple_write_to_buffer(OP_PROF_RM, buf, size + 1, size, pos);
174
175 error = PTR_ERR(data);
176 if (!IS_ERR(data)) {
177 data[size] = 0;
178 error = aa_remove_profiles(data, size);
179 kvfree(data);
180 }
181
182 return error;
183}
184
185static const struct file_operations aa_fs_profile_remove = {
Arnd Bergmann6038f372010-08-15 18:52:59 +0200186 .write = profile_remove,
187 .llseek = default_llseek,
John Johansen63e2b422010-07-29 14:48:03 -0700188};
189
Kees Cooke74abcf2012-01-26 16:29:21 -0800190static int aa_fs_seq_show(struct seq_file *seq, void *v)
191{
192 struct aa_fs_entry *fs_file = seq->private;
193
194 if (!fs_file)
195 return 0;
196
197 switch (fs_file->v_type) {
198 case AA_FS_TYPE_BOOLEAN:
199 seq_printf(seq, "%s\n", fs_file->v.boolean ? "yes" : "no");
200 break;
Kees Cooka9bf8e92012-01-26 16:29:22 -0800201 case AA_FS_TYPE_STRING:
202 seq_printf(seq, "%s\n", fs_file->v.string);
203 break;
Kees Cooke74abcf2012-01-26 16:29:21 -0800204 case AA_FS_TYPE_U64:
205 seq_printf(seq, "%#08lx\n", fs_file->v.u64);
206 break;
207 default:
208 /* Ignore unpritable entry types. */
209 break;
210 }
211
212 return 0;
213}
214
215static int aa_fs_seq_open(struct inode *inode, struct file *file)
216{
217 return single_open(file, aa_fs_seq_show, inode->i_private);
218}
219
220const struct file_operations aa_fs_seq_file_ops = {
221 .owner = THIS_MODULE,
222 .open = aa_fs_seq_open,
223 .read = seq_read,
224 .llseek = seq_lseek,
225 .release = single_release,
226};
227
John Johansen0d259f02013-07-10 21:13:43 -0700228static int aa_fs_seq_profile_open(struct inode *inode, struct file *file,
229 int (*show)(struct seq_file *, void *))
230{
231 struct aa_replacedby *r = aa_get_replacedby(inode->i_private);
232 int error = single_open(file, show, r);
John Johansen63e2b422010-07-29 14:48:03 -0700233
John Johansen0d259f02013-07-10 21:13:43 -0700234 if (error) {
235 file->private_data = NULL;
236 aa_put_replacedby(r);
237 }
238
239 return error;
240}
241
242static int aa_fs_seq_profile_release(struct inode *inode, struct file *file)
243{
244 struct seq_file *seq = (struct seq_file *) file->private_data;
245 if (seq)
246 aa_put_replacedby(seq->private);
247 return single_release(inode, file);
248}
249
250static int aa_fs_seq_profname_show(struct seq_file *seq, void *v)
251{
252 struct aa_replacedby *r = seq->private;
253 struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
254 seq_printf(seq, "%s\n", profile->base.name);
255 aa_put_profile(profile);
256
257 return 0;
258}
259
260static int aa_fs_seq_profname_open(struct inode *inode, struct file *file)
261{
262 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profname_show);
263}
264
265static const struct file_operations aa_fs_profname_fops = {
266 .owner = THIS_MODULE,
267 .open = aa_fs_seq_profname_open,
268 .read = seq_read,
269 .llseek = seq_lseek,
270 .release = aa_fs_seq_profile_release,
271};
272
273static int aa_fs_seq_profmode_show(struct seq_file *seq, void *v)
274{
275 struct aa_replacedby *r = seq->private;
276 struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
277 seq_printf(seq, "%s\n", aa_profile_mode_names[profile->mode]);
278 aa_put_profile(profile);
279
280 return 0;
281}
282
283static int aa_fs_seq_profmode_open(struct inode *inode, struct file *file)
284{
285 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profmode_show);
286}
287
288static const struct file_operations aa_fs_profmode_fops = {
289 .owner = THIS_MODULE,
290 .open = aa_fs_seq_profmode_open,
291 .read = seq_read,
292 .llseek = seq_lseek,
293 .release = aa_fs_seq_profile_release,
294};
295
John Johansen556d0be2013-07-10 21:17:43 -0700296static int aa_fs_seq_profattach_show(struct seq_file *seq, void *v)
297{
298 struct aa_replacedby *r = seq->private;
299 struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
300 if (profile->attach)
301 seq_printf(seq, "%s\n", profile->attach);
302 else if (profile->xmatch)
303 seq_puts(seq, "<unknown>\n");
304 else
305 seq_printf(seq, "%s\n", profile->base.name);
306 aa_put_profile(profile);
307
308 return 0;
309}
310
311static int aa_fs_seq_profattach_open(struct inode *inode, struct file *file)
312{
313 return aa_fs_seq_profile_open(inode, file, aa_fs_seq_profattach_show);
314}
315
316static const struct file_operations aa_fs_profattach_fops = {
317 .owner = THIS_MODULE,
318 .open = aa_fs_seq_profattach_open,
319 .read = seq_read,
320 .llseek = seq_lseek,
321 .release = aa_fs_seq_profile_release,
322};
323
John Johansenf8eb8a12013-08-14 11:27:36 -0700324static int aa_fs_seq_hash_show(struct seq_file *seq, void *v)
325{
326 struct aa_replacedby *r = seq->private;
327 struct aa_profile *profile = aa_get_profile_rcu(&r->profile);
328 unsigned int i, size = aa_hash_size();
329
330 if (profile->hash) {
331 for (i = 0; i < size; i++)
332 seq_printf(seq, "%.2x", profile->hash[i]);
333 seq_puts(seq, "\n");
334 }
John Johansen0b938a22015-11-18 11:41:05 -0800335 aa_put_profile(profile);
John Johansenf8eb8a12013-08-14 11:27:36 -0700336
337 return 0;
338}
339
340static int aa_fs_seq_hash_open(struct inode *inode, struct file *file)
341{
342 return single_open(file, aa_fs_seq_hash_show, inode->i_private);
343}
344
345static const struct file_operations aa_fs_seq_hash_fops = {
346 .owner = THIS_MODULE,
347 .open = aa_fs_seq_hash_open,
348 .read = seq_read,
349 .llseek = seq_lseek,
350 .release = single_release,
351};
352
John Johansen0d259f02013-07-10 21:13:43 -0700353/** fns to setup dynamic per profile/namespace files **/
354void __aa_fs_profile_rmdir(struct aa_profile *profile)
355{
356 struct aa_profile *child;
357 int i;
358
359 if (!profile)
360 return;
361
362 list_for_each_entry(child, &profile->base.profiles, base.list)
363 __aa_fs_profile_rmdir(child);
364
365 for (i = AAFS_PROF_SIZEOF - 1; i >= 0; --i) {
366 struct aa_replacedby *r;
367 if (!profile->dents[i])
368 continue;
369
David Howellsce0b16d2015-02-19 10:47:02 +0000370 r = d_inode(profile->dents[i])->i_private;
John Johansen0d259f02013-07-10 21:13:43 -0700371 securityfs_remove(profile->dents[i]);
372 aa_put_replacedby(r);
373 profile->dents[i] = NULL;
374 }
375}
376
377void __aa_fs_profile_migrate_dents(struct aa_profile *old,
378 struct aa_profile *new)
379{
380 int i;
381
382 for (i = 0; i < AAFS_PROF_SIZEOF; i++) {
383 new->dents[i] = old->dents[i];
John Johansend671e8902014-07-25 04:01:56 -0700384 if (new->dents[i])
Deepa Dinamani078cd822016-09-14 07:48:04 -0700385 new->dents[i]->d_inode->i_mtime = current_time(new->dents[i]->d_inode);
John Johansen0d259f02013-07-10 21:13:43 -0700386 old->dents[i] = NULL;
387 }
388}
389
390static struct dentry *create_profile_file(struct dentry *dir, const char *name,
391 struct aa_profile *profile,
392 const struct file_operations *fops)
393{
394 struct aa_replacedby *r = aa_get_replacedby(profile->replacedby);
395 struct dentry *dent;
396
397 dent = securityfs_create_file(name, S_IFREG | 0444, dir, r, fops);
398 if (IS_ERR(dent))
399 aa_put_replacedby(r);
400
401 return dent;
402}
403
404/* requires lock be held */
405int __aa_fs_profile_mkdir(struct aa_profile *profile, struct dentry *parent)
406{
407 struct aa_profile *child;
408 struct dentry *dent = NULL, *dir;
409 int error;
410
411 if (!parent) {
412 struct aa_profile *p;
413 p = aa_deref_parent(profile);
414 dent = prof_dir(p);
415 /* adding to parent that previously didn't have children */
416 dent = securityfs_create_dir("profiles", dent);
417 if (IS_ERR(dent))
418 goto fail;
419 prof_child_dir(p) = parent = dent;
420 }
421
422 if (!profile->dirname) {
423 int len, id_len;
424 len = mangle_name(profile->base.name, NULL);
425 id_len = snprintf(NULL, 0, ".%ld", profile->ns->uniq_id);
426
427 profile->dirname = kmalloc(len + id_len + 1, GFP_KERNEL);
428 if (!profile->dirname)
429 goto fail;
430
431 mangle_name(profile->base.name, profile->dirname);
432 sprintf(profile->dirname + len, ".%ld", profile->ns->uniq_id++);
433 }
434
435 dent = securityfs_create_dir(profile->dirname, parent);
436 if (IS_ERR(dent))
437 goto fail;
438 prof_dir(profile) = dir = dent;
439
440 dent = create_profile_file(dir, "name", profile, &aa_fs_profname_fops);
441 if (IS_ERR(dent))
442 goto fail;
443 profile->dents[AAFS_PROF_NAME] = dent;
444
445 dent = create_profile_file(dir, "mode", profile, &aa_fs_profmode_fops);
446 if (IS_ERR(dent))
447 goto fail;
448 profile->dents[AAFS_PROF_MODE] = dent;
449
John Johansen556d0be2013-07-10 21:17:43 -0700450 dent = create_profile_file(dir, "attach", profile,
451 &aa_fs_profattach_fops);
452 if (IS_ERR(dent))
453 goto fail;
454 profile->dents[AAFS_PROF_ATTACH] = dent;
455
John Johansenf8eb8a12013-08-14 11:27:36 -0700456 if (profile->hash) {
457 dent = create_profile_file(dir, "sha1", profile,
458 &aa_fs_seq_hash_fops);
459 if (IS_ERR(dent))
460 goto fail;
461 profile->dents[AAFS_PROF_HASH] = dent;
462 }
463
John Johansen0d259f02013-07-10 21:13:43 -0700464 list_for_each_entry(child, &profile->base.profiles, base.list) {
465 error = __aa_fs_profile_mkdir(child, prof_child_dir(profile));
466 if (error)
467 goto fail2;
468 }
469
470 return 0;
471
472fail:
473 error = PTR_ERR(dent);
474
475fail2:
476 __aa_fs_profile_rmdir(profile);
477
478 return error;
479}
480
John Johansen98849df2017-01-16 00:42:16 -0800481void __aa_fs_ns_rmdir(struct aa_ns *ns)
John Johansen0d259f02013-07-10 21:13:43 -0700482{
John Johansen98849df2017-01-16 00:42:16 -0800483 struct aa_ns *sub;
John Johansen0d259f02013-07-10 21:13:43 -0700484 struct aa_profile *child;
485 int i;
486
487 if (!ns)
488 return;
489
490 list_for_each_entry(child, &ns->base.profiles, base.list)
491 __aa_fs_profile_rmdir(child);
492
493 list_for_each_entry(sub, &ns->sub_ns, base.list) {
494 mutex_lock(&sub->lock);
John Johansen98849df2017-01-16 00:42:16 -0800495 __aa_fs_ns_rmdir(sub);
John Johansen0d259f02013-07-10 21:13:43 -0700496 mutex_unlock(&sub->lock);
497 }
498
499 for (i = AAFS_NS_SIZEOF - 1; i >= 0; --i) {
500 securityfs_remove(ns->dents[i]);
501 ns->dents[i] = NULL;
502 }
503}
504
John Johansen98849df2017-01-16 00:42:16 -0800505int __aa_fs_ns_mkdir(struct aa_ns *ns, struct dentry *parent, const char *name)
John Johansen0d259f02013-07-10 21:13:43 -0700506{
John Johansen98849df2017-01-16 00:42:16 -0800507 struct aa_ns *sub;
John Johansen0d259f02013-07-10 21:13:43 -0700508 struct aa_profile *child;
509 struct dentry *dent, *dir;
510 int error;
511
512 if (!name)
513 name = ns->base.name;
514
515 dent = securityfs_create_dir(name, parent);
516 if (IS_ERR(dent))
517 goto fail;
518 ns_dir(ns) = dir = dent;
519
520 dent = securityfs_create_dir("profiles", dir);
521 if (IS_ERR(dent))
522 goto fail;
523 ns_subprofs_dir(ns) = dent;
524
525 dent = securityfs_create_dir("namespaces", dir);
526 if (IS_ERR(dent))
527 goto fail;
528 ns_subns_dir(ns) = dent;
529
530 list_for_each_entry(child, &ns->base.profiles, base.list) {
531 error = __aa_fs_profile_mkdir(child, ns_subprofs_dir(ns));
532 if (error)
533 goto fail2;
534 }
535
536 list_for_each_entry(sub, &ns->sub_ns, base.list) {
537 mutex_lock(&sub->lock);
John Johansen98849df2017-01-16 00:42:16 -0800538 error = __aa_fs_ns_mkdir(sub, ns_subns_dir(ns), NULL);
John Johansen0d259f02013-07-10 21:13:43 -0700539 mutex_unlock(&sub->lock);
540 if (error)
541 goto fail2;
542 }
543
544 return 0;
545
546fail:
547 error = PTR_ERR(dent);
548
549fail2:
John Johansen98849df2017-01-16 00:42:16 -0800550 __aa_fs_ns_rmdir(ns);
John Johansen0d259f02013-07-10 21:13:43 -0700551
552 return error;
553}
554
555
John Johansen29b38222013-07-10 21:18:43 -0700556#define list_entry_is_head(pos, head, member) (&pos->member == (head))
557
558/**
John Johansen98849df2017-01-16 00:42:16 -0800559 * __next_ns - find the next namespace to list
John Johansen29b38222013-07-10 21:18:43 -0700560 * @root: root namespace to stop search at (NOT NULL)
561 * @ns: current ns position (NOT NULL)
562 *
563 * Find the next namespace from @ns under @root and handle all locking needed
564 * while switching current namespace.
565 *
566 * Returns: next namespace or NULL if at last namespace under @root
567 * Requires: ns->parent->lock to be held
568 * NOTE: will not unlock root->lock
569 */
John Johansen98849df2017-01-16 00:42:16 -0800570static struct aa_ns *__next_ns(struct aa_ns *root, struct aa_ns *ns)
John Johansen29b38222013-07-10 21:18:43 -0700571{
John Johansen98849df2017-01-16 00:42:16 -0800572 struct aa_ns *parent, *next;
John Johansen29b38222013-07-10 21:18:43 -0700573
574 /* is next namespace a child */
575 if (!list_empty(&ns->sub_ns)) {
576 next = list_first_entry(&ns->sub_ns, typeof(*ns), base.list);
577 mutex_lock(&next->lock);
578 return next;
579 }
580
581 /* check if the next ns is a sibling, parent, gp, .. */
582 parent = ns->parent;
John Johansened2c7da2013-10-14 11:46:27 -0700583 while (ns != root) {
John Johansen29b38222013-07-10 21:18:43 -0700584 mutex_unlock(&ns->lock);
Geliang Tang38dbd7d2015-11-16 21:46:33 +0800585 next = list_next_entry(ns, base.list);
John Johansen29b38222013-07-10 21:18:43 -0700586 if (!list_entry_is_head(next, &parent->sub_ns, base.list)) {
587 mutex_lock(&next->lock);
588 return next;
589 }
John Johansen29b38222013-07-10 21:18:43 -0700590 ns = parent;
591 parent = parent->parent;
592 }
593
594 return NULL;
595}
596
597/**
598 * __first_profile - find the first profile in a namespace
599 * @root: namespace that is root of profiles being displayed (NOT NULL)
600 * @ns: namespace to start in (NOT NULL)
601 *
602 * Returns: unrefcounted profile or NULL if no profile
603 * Requires: profile->ns.lock to be held
604 */
John Johansen98849df2017-01-16 00:42:16 -0800605static struct aa_profile *__first_profile(struct aa_ns *root,
606 struct aa_ns *ns)
John Johansen29b38222013-07-10 21:18:43 -0700607{
John Johansen98849df2017-01-16 00:42:16 -0800608 for (; ns; ns = __next_ns(root, ns)) {
John Johansen29b38222013-07-10 21:18:43 -0700609 if (!list_empty(&ns->base.profiles))
610 return list_first_entry(&ns->base.profiles,
611 struct aa_profile, base.list);
612 }
613 return NULL;
614}
615
616/**
617 * __next_profile - step to the next profile in a profile tree
618 * @profile: current profile in tree (NOT NULL)
619 *
620 * Perform a depth first traversal on the profile tree in a namespace
621 *
622 * Returns: next profile or NULL if done
623 * Requires: profile->ns.lock to be held
624 */
625static struct aa_profile *__next_profile(struct aa_profile *p)
626{
627 struct aa_profile *parent;
John Johansen98849df2017-01-16 00:42:16 -0800628 struct aa_ns *ns = p->ns;
John Johansen29b38222013-07-10 21:18:43 -0700629
630 /* is next profile a child */
631 if (!list_empty(&p->base.profiles))
632 return list_first_entry(&p->base.profiles, typeof(*p),
633 base.list);
634
635 /* is next profile a sibling, parent sibling, gp, sibling, .. */
636 parent = rcu_dereference_protected(p->parent,
637 mutex_is_locked(&p->ns->lock));
638 while (parent) {
Geliang Tang38dbd7d2015-11-16 21:46:33 +0800639 p = list_next_entry(p, base.list);
John Johansen29b38222013-07-10 21:18:43 -0700640 if (!list_entry_is_head(p, &parent->base.profiles, base.list))
641 return p;
642 p = parent;
643 parent = rcu_dereference_protected(parent->parent,
644 mutex_is_locked(&parent->ns->lock));
645 }
646
647 /* is next another profile in the namespace */
Geliang Tang38dbd7d2015-11-16 21:46:33 +0800648 p = list_next_entry(p, base.list);
John Johansen29b38222013-07-10 21:18:43 -0700649 if (!list_entry_is_head(p, &ns->base.profiles, base.list))
650 return p;
651
652 return NULL;
653}
654
655/**
656 * next_profile - step to the next profile in where ever it may be
657 * @root: root namespace (NOT NULL)
658 * @profile: current profile (NOT NULL)
659 *
660 * Returns: next profile or NULL if there isn't one
661 */
John Johansen98849df2017-01-16 00:42:16 -0800662static struct aa_profile *next_profile(struct aa_ns *root,
John Johansen29b38222013-07-10 21:18:43 -0700663 struct aa_profile *profile)
664{
665 struct aa_profile *next = __next_profile(profile);
666 if (next)
667 return next;
668
669 /* finished all profiles in namespace move to next namespace */
John Johansen98849df2017-01-16 00:42:16 -0800670 return __first_profile(root, __next_ns(root, profile->ns));
John Johansen29b38222013-07-10 21:18:43 -0700671}
672
673/**
674 * p_start - start a depth first traversal of profile tree
675 * @f: seq_file to fill
676 * @pos: current position
677 *
678 * Returns: first profile under current namespace or NULL if none found
679 *
680 * acquires first ns->lock
681 */
682static void *p_start(struct seq_file *f, loff_t *pos)
683{
684 struct aa_profile *profile = NULL;
John Johansen98849df2017-01-16 00:42:16 -0800685 struct aa_ns *root = aa_current_profile()->ns;
John Johansen29b38222013-07-10 21:18:43 -0700686 loff_t l = *pos;
John Johansen98849df2017-01-16 00:42:16 -0800687 f->private = aa_get_ns(root);
John Johansen29b38222013-07-10 21:18:43 -0700688
689
690 /* find the first profile */
691 mutex_lock(&root->lock);
692 profile = __first_profile(root, root);
693
694 /* skip to position */
695 for (; profile && l > 0; l--)
696 profile = next_profile(root, profile);
697
698 return profile;
699}
700
701/**
702 * p_next - read the next profile entry
703 * @f: seq_file to fill
704 * @p: profile previously returned
705 * @pos: current position
706 *
707 * Returns: next profile after @p or NULL if none
708 *
709 * may acquire/release locks in namespace tree as necessary
710 */
711static void *p_next(struct seq_file *f, void *p, loff_t *pos)
712{
713 struct aa_profile *profile = p;
John Johansen98849df2017-01-16 00:42:16 -0800714 struct aa_ns *ns = f->private;
John Johansen29b38222013-07-10 21:18:43 -0700715 (*pos)++;
716
717 return next_profile(ns, profile);
718}
719
720/**
721 * p_stop - stop depth first traversal
722 * @f: seq_file we are filling
723 * @p: the last profile writen
724 *
725 * Release all locking done by p_start/p_next on namespace tree
726 */
727static void p_stop(struct seq_file *f, void *p)
728{
729 struct aa_profile *profile = p;
John Johansen98849df2017-01-16 00:42:16 -0800730 struct aa_ns *root = f->private, *ns;
John Johansen29b38222013-07-10 21:18:43 -0700731
732 if (profile) {
733 for (ns = profile->ns; ns && ns != root; ns = ns->parent)
734 mutex_unlock(&ns->lock);
735 }
736 mutex_unlock(&root->lock);
John Johansen98849df2017-01-16 00:42:16 -0800737 aa_put_ns(root);
John Johansen29b38222013-07-10 21:18:43 -0700738}
739
740/**
741 * seq_show_profile - show a profile entry
742 * @f: seq_file to file
743 * @p: current position (profile) (NOT NULL)
744 *
745 * Returns: error on failure
746 */
747static int seq_show_profile(struct seq_file *f, void *p)
748{
749 struct aa_profile *profile = (struct aa_profile *)p;
John Johansen98849df2017-01-16 00:42:16 -0800750 struct aa_ns *root = f->private;
John Johansen29b38222013-07-10 21:18:43 -0700751
752 if (profile->ns != root)
753 seq_printf(f, ":%s://", aa_ns_name(root, profile->ns));
754 seq_printf(f, "%s (%s)\n", profile->base.hname,
755 aa_profile_mode_names[profile->mode]);
756
757 return 0;
758}
759
760static const struct seq_operations aa_fs_profiles_op = {
761 .start = p_start,
762 .next = p_next,
763 .stop = p_stop,
764 .show = seq_show_profile,
765};
766
767static int profiles_open(struct inode *inode, struct file *file)
768{
769 return seq_open(file, &aa_fs_profiles_op);
770}
771
772static int profiles_release(struct inode *inode, struct file *file)
773{
774 return seq_release(inode, file);
775}
776
777static const struct file_operations aa_fs_profiles_fops = {
778 .open = profiles_open,
779 .read = seq_read,
780 .llseek = seq_lseek,
781 .release = profiles_release,
782};
783
784
John Johansen0d259f02013-07-10 21:13:43 -0700785/** Base file system setup **/
Kees Cooka9bf8e92012-01-26 16:29:22 -0800786static struct aa_fs_entry aa_fs_entry_file[] = {
787 AA_FS_FILE_STRING("mask", "create read write exec append mmap_exec " \
788 "link lock"),
789 { }
790};
791
Kees Cooke74abcf2012-01-26 16:29:21 -0800792static struct aa_fs_entry aa_fs_entry_domain[] = {
793 AA_FS_FILE_BOOLEAN("change_hat", 1),
794 AA_FS_FILE_BOOLEAN("change_hatv", 1),
795 AA_FS_FILE_BOOLEAN("change_onexec", 1),
796 AA_FS_FILE_BOOLEAN("change_profile", 1),
797 { }
798};
799
John Johansen9d910a32013-07-10 21:04:43 -0700800static struct aa_fs_entry aa_fs_entry_policy[] = {
John Johansendd51c8482013-07-10 21:05:43 -0700801 AA_FS_FILE_BOOLEAN("set_load", 1),
John Johansen9d910a32013-07-10 21:04:43 -0700802 {}
803};
804
Kees Cooke74abcf2012-01-26 16:29:21 -0800805static struct aa_fs_entry aa_fs_entry_features[] = {
John Johansen9d910a32013-07-10 21:04:43 -0700806 AA_FS_DIR("policy", aa_fs_entry_policy),
Kees Cooke74abcf2012-01-26 16:29:21 -0800807 AA_FS_DIR("domain", aa_fs_entry_domain),
Kees Cooka9bf8e92012-01-26 16:29:22 -0800808 AA_FS_DIR("file", aa_fs_entry_file),
Kees Cooke74abcf2012-01-26 16:29:21 -0800809 AA_FS_FILE_U64("capability", VFS_CAP_FLAGS_MASK),
Kees Cookd384b0a2012-01-26 16:29:23 -0800810 AA_FS_DIR("rlimit", aa_fs_entry_rlimit),
John Johansen84f1f782013-08-14 11:27:32 -0700811 AA_FS_DIR("caps", aa_fs_entry_caps),
Kees Cooke74abcf2012-01-26 16:29:21 -0800812 { }
813};
814
Kees Cook9acd4942012-01-26 16:29:20 -0800815static struct aa_fs_entry aa_fs_entry_apparmor[] = {
816 AA_FS_FILE_FOPS(".load", 0640, &aa_fs_profile_load),
817 AA_FS_FILE_FOPS(".replace", 0640, &aa_fs_profile_replace),
818 AA_FS_FILE_FOPS(".remove", 0640, &aa_fs_profile_remove),
John Johansen29b38222013-07-10 21:18:43 -0700819 AA_FS_FILE_FOPS("profiles", 0640, &aa_fs_profiles_fops),
Kees Cooke74abcf2012-01-26 16:29:21 -0800820 AA_FS_DIR("features", aa_fs_entry_features),
Kees Cook9acd4942012-01-26 16:29:20 -0800821 { }
822};
John Johansen63e2b422010-07-29 14:48:03 -0700823
Kees Cook9acd4942012-01-26 16:29:20 -0800824static struct aa_fs_entry aa_fs_entry =
825 AA_FS_DIR("apparmor", aa_fs_entry_apparmor);
826
827/**
828 * aafs_create_file - create a file entry in the apparmor securityfs
829 * @fs_file: aa_fs_entry to build an entry for (NOT NULL)
830 * @parent: the parent dentry in the securityfs
831 *
832 * Use aafs_remove_file to remove entries created with this fn.
833 */
834static int __init aafs_create_file(struct aa_fs_entry *fs_file,
835 struct dentry *parent)
John Johansen63e2b422010-07-29 14:48:03 -0700836{
Kees Cook9acd4942012-01-26 16:29:20 -0800837 int error = 0;
John Johansen63e2b422010-07-29 14:48:03 -0700838
Kees Cook9acd4942012-01-26 16:29:20 -0800839 fs_file->dentry = securityfs_create_file(fs_file->name,
840 S_IFREG | fs_file->mode,
841 parent, fs_file,
842 fs_file->file_ops);
843 if (IS_ERR(fs_file->dentry)) {
844 error = PTR_ERR(fs_file->dentry);
845 fs_file->dentry = NULL;
John Johansen63e2b422010-07-29 14:48:03 -0700846 }
Kees Cook9acd4942012-01-26 16:29:20 -0800847 return error;
John Johansen63e2b422010-07-29 14:48:03 -0700848}
849
John Johansen0d259f02013-07-10 21:13:43 -0700850static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir);
John Johansen63e2b422010-07-29 14:48:03 -0700851/**
Kees Cook9acd4942012-01-26 16:29:20 -0800852 * aafs_create_dir - recursively create a directory entry in the securityfs
853 * @fs_dir: aa_fs_entry (and all child entries) to build (NOT NULL)
854 * @parent: the parent dentry in the securityfs
John Johansen63e2b422010-07-29 14:48:03 -0700855 *
Kees Cook9acd4942012-01-26 16:29:20 -0800856 * Use aafs_remove_dir to remove entries created with this fn.
John Johansen63e2b422010-07-29 14:48:03 -0700857 */
Kees Cook9acd4942012-01-26 16:29:20 -0800858static int __init aafs_create_dir(struct aa_fs_entry *fs_dir,
859 struct dentry *parent)
John Johansen63e2b422010-07-29 14:48:03 -0700860{
Kees Cook9acd4942012-01-26 16:29:20 -0800861 struct aa_fs_entry *fs_file;
John Johansen0d259f02013-07-10 21:13:43 -0700862 struct dentry *dir;
863 int error;
John Johansen63e2b422010-07-29 14:48:03 -0700864
John Johansen0d259f02013-07-10 21:13:43 -0700865 dir = securityfs_create_dir(fs_dir->name, parent);
866 if (IS_ERR(dir))
867 return PTR_ERR(dir);
868 fs_dir->dentry = dir;
John Johansen63e2b422010-07-29 14:48:03 -0700869
John Johansen0d259f02013-07-10 21:13:43 -0700870 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
Kees Cook9acd4942012-01-26 16:29:20 -0800871 if (fs_file->v_type == AA_FS_TYPE_DIR)
872 error = aafs_create_dir(fs_file, fs_dir->dentry);
873 else
874 error = aafs_create_file(fs_file, fs_dir->dentry);
875 if (error)
876 goto failed;
877 }
878
879 return 0;
880
881failed:
John Johansen0d259f02013-07-10 21:13:43 -0700882 aafs_remove_dir(fs_dir);
883
Kees Cook9acd4942012-01-26 16:29:20 -0800884 return error;
885}
886
887/**
888 * aafs_remove_file - drop a single file entry in the apparmor securityfs
889 * @fs_file: aa_fs_entry to detach from the securityfs (NOT NULL)
890 */
891static void __init aafs_remove_file(struct aa_fs_entry *fs_file)
892{
893 if (!fs_file->dentry)
894 return;
895
896 securityfs_remove(fs_file->dentry);
897 fs_file->dentry = NULL;
898}
899
900/**
901 * aafs_remove_dir - recursively drop a directory entry from the securityfs
902 * @fs_dir: aa_fs_entry (and all child entries) to detach (NOT NULL)
903 */
904static void __init aafs_remove_dir(struct aa_fs_entry *fs_dir)
905{
906 struct aa_fs_entry *fs_file;
907
John Johansen0d259f02013-07-10 21:13:43 -0700908 for (fs_file = fs_dir->v.files; fs_file && fs_file->name; ++fs_file) {
Kees Cook9acd4942012-01-26 16:29:20 -0800909 if (fs_file->v_type == AA_FS_TYPE_DIR)
910 aafs_remove_dir(fs_file);
911 else
912 aafs_remove_file(fs_file);
913 }
914
915 aafs_remove_file(fs_dir);
John Johansen63e2b422010-07-29 14:48:03 -0700916}
917
918/**
919 * aa_destroy_aafs - cleanup and free aafs
920 *
921 * releases dentries allocated by aa_create_aafs
922 */
923void __init aa_destroy_aafs(void)
924{
Kees Cook9acd4942012-01-26 16:29:20 -0800925 aafs_remove_dir(&aa_fs_entry);
John Johansen63e2b422010-07-29 14:48:03 -0700926}
927
928/**
929 * aa_create_aafs - create the apparmor security filesystem
930 *
931 * dentries created here are released by aa_destroy_aafs
932 *
933 * Returns: error on failure
934 */
James Morris3417d8d2011-08-17 11:05:21 +1000935static int __init aa_create_aafs(void)
John Johansen63e2b422010-07-29 14:48:03 -0700936{
937 int error;
938
939 if (!apparmor_initialized)
940 return 0;
941
Kees Cook9acd4942012-01-26 16:29:20 -0800942 if (aa_fs_entry.dentry) {
John Johansen63e2b422010-07-29 14:48:03 -0700943 AA_ERROR("%s: AppArmor securityfs already exists\n", __func__);
944 return -EEXIST;
945 }
946
Kees Cook9acd4942012-01-26 16:29:20 -0800947 /* Populate fs tree. */
948 error = aafs_create_dir(&aa_fs_entry, NULL);
John Johansen63e2b422010-07-29 14:48:03 -0700949 if (error)
950 goto error;
951
John Johansen98849df2017-01-16 00:42:16 -0800952 error = __aa_fs_ns_mkdir(root_ns, aa_fs_entry.dentry, "policy");
John Johansen0d259f02013-07-10 21:13:43 -0700953 if (error)
954 goto error;
955
John Johansen63e2b422010-07-29 14:48:03 -0700956 /* TODO: add support for apparmorfs_null and apparmorfs_mnt */
957
958 /* Report that AppArmor fs is enabled */
959 aa_info_message("AppArmor Filesystem Enabled");
960 return 0;
961
962error:
963 aa_destroy_aafs();
964 AA_ERROR("Error creating AppArmor securityfs\n");
965 return error;
966}
967
968fs_initcall(aa_create_aafs);