Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / security / apparmor / lsm.c
blob9a65eeaf7dfa22ab3b76d05b1445c56031195cd3
1 /*
2 * AppArmor security module
4 * This file contains AppArmor LSM hooks.
6 * Copyright (C) 1998-2008 Novell/SUSE
7 * Copyright 2009-2010 Canonical Ltd.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation, version 2 of the
12 * License.
15 #include <linux/lsm_hooks.h>
16 #include <linux/moduleparam.h>
17 #include <linux/mm.h>
18 #include <linux/mman.h>
19 #include <linux/mount.h>
20 #include <linux/namei.h>
21 #include <linux/ptrace.h>
22 #include <linux/ctype.h>
23 #include <linux/sysctl.h>
24 #include <linux/audit.h>
25 #include <linux/user_namespace.h>
26 #include <linux/kmemleak.h>
27 #include <net/sock.h>
29 #include "include/apparmor.h"
30 #include "include/apparmorfs.h"
31 #include "include/audit.h"
32 #include "include/capability.h"
33 #include "include/context.h"
34 #include "include/file.h"
35 #include "include/ipc.h"
36 #include "include/path.h"
37 #include "include/label.h"
38 #include "include/policy.h"
39 #include "include/policy_ns.h"
40 #include "include/procattr.h"
41 #include "include/mount.h"
43 /* Flag indicating whether initialization completed */
44 int apparmor_initialized;
46 DEFINE_PER_CPU(struct aa_buffers, aa_buffers);
50 * LSM hook functions
54 * free the associated aa_task_ctx and put its labels
56 static void apparmor_cred_free(struct cred *cred)
58 aa_free_task_context(cred_ctx(cred));
59 cred_ctx(cred) = NULL;
63 * allocate the apparmor part of blank credentials
65 static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
67 /* freed by apparmor_cred_free */
68 struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
70 if (!ctx)
71 return -ENOMEM;
73 cred_ctx(cred) = ctx;
74 return 0;
78 * prepare new aa_task_ctx for modification by prepare_cred block
80 static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
81 gfp_t gfp)
83 /* freed by apparmor_cred_free */
84 struct aa_task_ctx *ctx = aa_alloc_task_context(gfp);
86 if (!ctx)
87 return -ENOMEM;
89 aa_dup_task_context(ctx, cred_ctx(old));
90 cred_ctx(new) = ctx;
91 return 0;
95 * transfer the apparmor data to a blank set of creds
97 static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
99 const struct aa_task_ctx *old_ctx = cred_ctx(old);
100 struct aa_task_ctx *new_ctx = cred_ctx(new);
102 aa_dup_task_context(new_ctx, old_ctx);
105 static int apparmor_ptrace_access_check(struct task_struct *child,
106 unsigned int mode)
108 struct aa_label *tracer, *tracee;
109 int error;
111 tracer = begin_current_label_crit_section();
112 tracee = aa_get_task_label(child);
113 error = aa_may_ptrace(tracer, tracee,
114 mode == PTRACE_MODE_READ ? AA_PTRACE_READ : AA_PTRACE_TRACE);
115 aa_put_label(tracee);
116 end_current_label_crit_section(tracer);
118 return error;
121 static int apparmor_ptrace_traceme(struct task_struct *parent)
123 struct aa_label *tracer, *tracee;
124 int error;
126 tracee = begin_current_label_crit_section();
127 tracer = aa_get_task_label(parent);
128 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
129 aa_put_label(tracer);
130 end_current_label_crit_section(tracee);
132 return error;
135 /* Derived from security/commoncap.c:cap_capget */
136 static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective,
137 kernel_cap_t *inheritable, kernel_cap_t *permitted)
139 struct aa_label *label;
140 const struct cred *cred;
142 rcu_read_lock();
143 cred = __task_cred(target);
144 label = aa_get_newest_cred_label(cred);
147 * cap_capget is stacked ahead of this and will
148 * initialize effective and permitted.
150 if (!unconfined(label)) {
151 struct aa_profile *profile;
152 struct label_it i;
154 label_for_each_confined(i, label, profile) {
155 if (COMPLAIN_MODE(profile))
156 continue;
157 *effective = cap_intersect(*effective,
158 profile->caps.allow);
159 *permitted = cap_intersect(*permitted,
160 profile->caps.allow);
163 rcu_read_unlock();
164 aa_put_label(label);
166 return 0;
169 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
170 int cap, int audit)
172 struct aa_label *label;
173 int error = 0;
175 label = aa_get_newest_cred_label(cred);
176 if (!unconfined(label))
177 error = aa_capable(label, cap, audit);
178 aa_put_label(label);
180 return error;
184 * common_perm - basic common permission check wrapper fn for paths
185 * @op: operation being checked
186 * @path: path to check permission of (NOT NULL)
187 * @mask: requested permissions mask
188 * @cond: conditional info for the permission request (NOT NULL)
190 * Returns: %0 else error code if error or permission denied
192 static int common_perm(const char *op, const struct path *path, u32 mask,
193 struct path_cond *cond)
195 struct aa_label *label;
196 int error = 0;
198 label = __begin_current_label_crit_section();
199 if (!unconfined(label))
200 error = aa_path_perm(op, label, path, 0, mask, cond);
201 __end_current_label_crit_section(label);
203 return error;
207 * common_perm_cond - common permission wrapper around inode cond
208 * @op: operation being checked
209 * @path: location to check (NOT NULL)
210 * @mask: requested permissions mask
212 * Returns: %0 else error code if error or permission denied
214 static int common_perm_cond(const char *op, const struct path *path, u32 mask)
216 struct path_cond cond = { d_backing_inode(path->dentry)->i_uid,
217 d_backing_inode(path->dentry)->i_mode
220 if (!path_mediated_fs(path->dentry))
221 return 0;
223 return common_perm(op, path, mask, &cond);
227 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
228 * @op: operation being checked
229 * @dir: directory of the dentry (NOT NULL)
230 * @dentry: dentry to check (NOT NULL)
231 * @mask: requested permissions mask
232 * @cond: conditional info for the permission request (NOT NULL)
234 * Returns: %0 else error code if error or permission denied
236 static int common_perm_dir_dentry(const char *op, const struct path *dir,
237 struct dentry *dentry, u32 mask,
238 struct path_cond *cond)
240 struct path path = { .mnt = dir->mnt, .dentry = dentry };
242 return common_perm(op, &path, mask, cond);
246 * common_perm_rm - common permission wrapper for operations doing rm
247 * @op: operation being checked
248 * @dir: directory that the dentry is in (NOT NULL)
249 * @dentry: dentry being rm'd (NOT NULL)
250 * @mask: requested permission mask
252 * Returns: %0 else error code if error or permission denied
254 static int common_perm_rm(const char *op, const struct path *dir,
255 struct dentry *dentry, u32 mask)
257 struct inode *inode = d_backing_inode(dentry);
258 struct path_cond cond = { };
260 if (!inode || !path_mediated_fs(dentry))
261 return 0;
263 cond.uid = inode->i_uid;
264 cond.mode = inode->i_mode;
266 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
270 * common_perm_create - common permission wrapper for operations doing create
271 * @op: operation being checked
272 * @dir: directory that dentry will be created in (NOT NULL)
273 * @dentry: dentry to create (NOT NULL)
274 * @mask: request permission mask
275 * @mode: created file mode
277 * Returns: %0 else error code if error or permission denied
279 static int common_perm_create(const char *op, const struct path *dir,
280 struct dentry *dentry, u32 mask, umode_t mode)
282 struct path_cond cond = { current_fsuid(), mode };
284 if (!path_mediated_fs(dir->dentry))
285 return 0;
287 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
290 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
292 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
295 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
296 umode_t mode)
298 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
299 S_IFDIR);
302 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
304 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
307 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
308 umode_t mode, unsigned int dev)
310 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
313 static int apparmor_path_truncate(const struct path *path)
315 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
318 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
319 const char *old_name)
321 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
322 S_IFLNK);
325 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
326 struct dentry *new_dentry)
328 struct aa_label *label;
329 int error = 0;
331 if (!path_mediated_fs(old_dentry))
332 return 0;
334 label = begin_current_label_crit_section();
335 if (!unconfined(label))
336 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
337 end_current_label_crit_section(label);
339 return error;
342 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
343 const struct path *new_dir, struct dentry *new_dentry)
345 struct aa_label *label;
346 int error = 0;
348 if (!path_mediated_fs(old_dentry))
349 return 0;
351 label = begin_current_label_crit_section();
352 if (!unconfined(label)) {
353 struct path old_path = { .mnt = old_dir->mnt,
354 .dentry = old_dentry };
355 struct path new_path = { .mnt = new_dir->mnt,
356 .dentry = new_dentry };
357 struct path_cond cond = { d_backing_inode(old_dentry)->i_uid,
358 d_backing_inode(old_dentry)->i_mode
361 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
362 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
363 AA_MAY_SETATTR | AA_MAY_DELETE,
364 &cond);
365 if (!error)
366 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
367 0, MAY_WRITE | AA_MAY_SETATTR |
368 AA_MAY_CREATE, &cond);
371 end_current_label_crit_section(label);
373 return error;
376 static int apparmor_path_chmod(const struct path *path, umode_t mode)
378 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
381 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
383 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
386 static int apparmor_inode_getattr(const struct path *path)
388 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
391 static int apparmor_file_open(struct file *file, const struct cred *cred)
393 struct aa_file_ctx *fctx = file_ctx(file);
394 struct aa_label *label;
395 int error = 0;
397 if (!path_mediated_fs(file->f_path.dentry))
398 return 0;
400 /* If in exec, permission is handled by bprm hooks.
401 * Cache permissions granted by the previous exec check, with
402 * implicit read and executable mmap which are required to
403 * actually execute the image.
405 if (current->in_execve) {
406 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
407 return 0;
410 label = aa_get_newest_cred_label(cred);
411 if (!unconfined(label)) {
412 struct inode *inode = file_inode(file);
413 struct path_cond cond = { inode->i_uid, inode->i_mode };
415 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
416 aa_map_file_to_perms(file), &cond);
417 /* todo cache full allowed permissions set and state */
418 fctx->allow = aa_map_file_to_perms(file);
420 aa_put_label(label);
422 return error;
425 static int apparmor_file_alloc_security(struct file *file)
427 int error = 0;
429 /* freed by apparmor_file_free_security */
430 struct aa_label *label = begin_current_label_crit_section();
431 file->f_security = aa_alloc_file_ctx(label, GFP_KERNEL);
432 if (!file_ctx(file))
433 error = -ENOMEM;
434 end_current_label_crit_section(label);
436 return error;
439 static void apparmor_file_free_security(struct file *file)
441 aa_free_file_ctx(file_ctx(file));
444 static int common_file_perm(const char *op, struct file *file, u32 mask)
446 struct aa_label *label;
447 int error = 0;
449 /* don't reaudit files closed during inheritance */
450 if (file->f_path.dentry == aa_null.dentry)
451 return -EACCES;
453 label = __begin_current_label_crit_section();
454 error = aa_file_perm(op, label, file, mask);
455 __end_current_label_crit_section(label);
457 return error;
460 static int apparmor_file_receive(struct file *file)
462 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file));
465 static int apparmor_file_permission(struct file *file, int mask)
467 return common_file_perm(OP_FPERM, file, mask);
470 static int apparmor_file_lock(struct file *file, unsigned int cmd)
472 u32 mask = AA_MAY_LOCK;
474 if (cmd == F_WRLCK)
475 mask |= MAY_WRITE;
477 return common_file_perm(OP_FLOCK, file, mask);
480 static int common_mmap(const char *op, struct file *file, unsigned long prot,
481 unsigned long flags)
483 int mask = 0;
485 if (!file || !file_ctx(file))
486 return 0;
488 if (prot & PROT_READ)
489 mask |= MAY_READ;
491 * Private mappings don't require write perms since they don't
492 * write back to the files
494 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
495 mask |= MAY_WRITE;
496 if (prot & PROT_EXEC)
497 mask |= AA_EXEC_MMAP;
499 return common_file_perm(op, file, mask);
502 static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
503 unsigned long prot, unsigned long flags)
505 return common_mmap(OP_FMMAP, file, prot, flags);
508 static int apparmor_file_mprotect(struct vm_area_struct *vma,
509 unsigned long reqprot, unsigned long prot)
511 return common_mmap(OP_FMPROT, vma->vm_file, prot,
512 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0);
515 static int apparmor_sb_mount(const char *dev_name, const struct path *path,
516 const char *type, unsigned long flags, void *data)
518 struct aa_label *label;
519 int error = 0;
521 /* Discard magic */
522 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
523 flags &= ~MS_MGC_MSK;
525 flags &= ~AA_MS_IGNORE_MASK;
527 label = __begin_current_label_crit_section();
528 if (!unconfined(label)) {
529 if (flags & MS_REMOUNT)
530 error = aa_remount(label, path, flags, data);
531 else if (flags & MS_BIND)
532 error = aa_bind_mount(label, path, dev_name, flags);
533 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
534 MS_UNBINDABLE))
535 error = aa_mount_change_type(label, path, flags);
536 else if (flags & MS_MOVE)
537 error = aa_move_mount(label, path, dev_name);
538 else
539 error = aa_new_mount(label, dev_name, path, type,
540 flags, data);
542 __end_current_label_crit_section(label);
544 return error;
547 static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
549 struct aa_label *label;
550 int error = 0;
552 label = __begin_current_label_crit_section();
553 if (!unconfined(label))
554 error = aa_umount(label, mnt, flags);
555 __end_current_label_crit_section(label);
557 return error;
560 static int apparmor_sb_pivotroot(const struct path *old_path,
561 const struct path *new_path)
563 struct aa_label *label;
564 int error = 0;
566 label = aa_get_current_label();
567 if (!unconfined(label))
568 error = aa_pivotroot(label, old_path, new_path);
569 aa_put_label(label);
571 return error;
574 static int apparmor_getprocattr(struct task_struct *task, char *name,
575 char **value)
577 int error = -ENOENT;
578 /* released below */
579 const struct cred *cred = get_task_cred(task);
580 struct aa_task_ctx *ctx = cred_ctx(cred);
581 struct aa_label *label = NULL;
583 if (strcmp(name, "current") == 0)
584 label = aa_get_newest_label(ctx->label);
585 else if (strcmp(name, "prev") == 0 && ctx->previous)
586 label = aa_get_newest_label(ctx->previous);
587 else if (strcmp(name, "exec") == 0 && ctx->onexec)
588 label = aa_get_newest_label(ctx->onexec);
589 else
590 error = -EINVAL;
592 if (label)
593 error = aa_getprocattr(label, value);
595 aa_put_label(label);
596 put_cred(cred);
598 return error;
601 static int apparmor_setprocattr(const char *name, void *value,
602 size_t size)
604 char *command, *largs = NULL, *args = value;
605 size_t arg_size;
606 int error;
607 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR);
609 if (size == 0)
610 return -EINVAL;
612 /* AppArmor requires that the buffer must be null terminated atm */
613 if (args[size - 1] != '\0') {
614 /* null terminate */
615 largs = args = kmalloc(size + 1, GFP_KERNEL);
616 if (!args)
617 return -ENOMEM;
618 memcpy(args, value, size);
619 args[size] = '\0';
622 error = -EINVAL;
623 args = strim(args);
624 command = strsep(&args, " ");
625 if (!args)
626 goto out;
627 args = skip_spaces(args);
628 if (!*args)
629 goto out;
631 arg_size = size - (args - (largs ? largs : (char *) value));
632 if (strcmp(name, "current") == 0) {
633 if (strcmp(command, "changehat") == 0) {
634 error = aa_setprocattr_changehat(args, arg_size,
635 AA_CHANGE_NOFLAGS);
636 } else if (strcmp(command, "permhat") == 0) {
637 error = aa_setprocattr_changehat(args, arg_size,
638 AA_CHANGE_TEST);
639 } else if (strcmp(command, "changeprofile") == 0) {
640 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
641 } else if (strcmp(command, "permprofile") == 0) {
642 error = aa_change_profile(args, AA_CHANGE_TEST);
643 } else if (strcmp(command, "stack") == 0) {
644 error = aa_change_profile(args, AA_CHANGE_STACK);
645 } else
646 goto fail;
647 } else if (strcmp(name, "exec") == 0) {
648 if (strcmp(command, "exec") == 0)
649 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
650 else if (strcmp(command, "stack") == 0)
651 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
652 AA_CHANGE_STACK));
653 else
654 goto fail;
655 } else
656 /* only support the "current" and "exec" process attributes */
657 goto fail;
659 if (!error)
660 error = size;
661 out:
662 kfree(largs);
663 return error;
665 fail:
666 aad(&sa)->label = begin_current_label_crit_section();
667 aad(&sa)->info = name;
668 aad(&sa)->error = error = -EINVAL;
669 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
670 end_current_label_crit_section(aad(&sa)->label);
671 goto out;
675 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
676 * @bprm: binprm for the exec (NOT NULL)
678 static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
680 struct aa_label *label = aa_current_raw_label();
681 struct aa_task_ctx *new_ctx = cred_ctx(bprm->cred);
683 /* bail out if unconfined or not changing profile */
684 if ((new_ctx->label->proxy == label->proxy) ||
685 (unconfined(new_ctx->label)))
686 return;
688 aa_inherit_files(bprm->cred, current->files);
690 current->pdeath_signal = 0;
692 /* reset soft limits and set hard limits for the new label */
693 __aa_transition_rlimits(label, new_ctx->label);
697 * apparmor_bprm_committed_cred - do cleanup after new creds committed
698 * @bprm: binprm for the exec (NOT NULL)
700 static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
702 /* TODO: cleanup signals - ipc mediation */
703 return;
706 static int apparmor_task_setrlimit(struct task_struct *task,
707 unsigned int resource, struct rlimit *new_rlim)
709 struct aa_label *label = __begin_current_label_crit_section();
710 int error = 0;
712 if (!unconfined(label))
713 error = aa_task_setrlimit(label, task, resource, new_rlim);
714 __end_current_label_crit_section(label);
716 return error;
719 static int apparmor_task_kill(struct task_struct *target, struct siginfo *info,
720 int sig, u32 secid)
722 struct aa_label *cl, *tl;
723 int error;
725 if (secid)
726 /* TODO: after secid to label mapping is done.
727 * Dealing with USB IO specific behavior
729 return 0;
730 cl = __begin_current_label_crit_section();
731 tl = aa_get_task_label(target);
732 error = aa_may_signal(cl, tl, sig);
733 aa_put_label(tl);
734 __end_current_label_crit_section(cl);
736 return error;
739 static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
740 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
741 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
742 LSM_HOOK_INIT(capget, apparmor_capget),
743 LSM_HOOK_INIT(capable, apparmor_capable),
745 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
746 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
747 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
749 LSM_HOOK_INIT(path_link, apparmor_path_link),
750 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
751 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
752 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
753 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
754 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
755 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
756 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
757 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
758 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
759 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
761 LSM_HOOK_INIT(file_open, apparmor_file_open),
762 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
763 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
764 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
765 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
766 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
767 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
768 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
770 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
771 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
773 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
774 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
775 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
776 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
778 LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds),
779 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
780 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
782 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
783 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
787 * AppArmor sysfs module parameters
790 static int param_set_aabool(const char *val, const struct kernel_param *kp);
791 static int param_get_aabool(char *buffer, const struct kernel_param *kp);
792 #define param_check_aabool param_check_bool
793 static const struct kernel_param_ops param_ops_aabool = {
794 .flags = KERNEL_PARAM_OPS_FL_NOARG,
795 .set = param_set_aabool,
796 .get = param_get_aabool
799 static int param_set_aauint(const char *val, const struct kernel_param *kp);
800 static int param_get_aauint(char *buffer, const struct kernel_param *kp);
801 #define param_check_aauint param_check_uint
802 static const struct kernel_param_ops param_ops_aauint = {
803 .set = param_set_aauint,
804 .get = param_get_aauint
807 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
808 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
809 #define param_check_aalockpolicy param_check_bool
810 static const struct kernel_param_ops param_ops_aalockpolicy = {
811 .flags = KERNEL_PARAM_OPS_FL_NOARG,
812 .set = param_set_aalockpolicy,
813 .get = param_get_aalockpolicy
816 static int param_set_audit(const char *val, const struct kernel_param *kp);
817 static int param_get_audit(char *buffer, const struct kernel_param *kp);
819 static int param_set_mode(const char *val, const struct kernel_param *kp);
820 static int param_get_mode(char *buffer, const struct kernel_param *kp);
822 /* Flag values, also controllable via /sys/module/apparmor/parameters
823 * We define special types as we want to do additional mediation.
826 /* AppArmor global enforcement switch - complain, enforce, kill */
827 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
828 module_param_call(mode, param_set_mode, param_get_mode,
829 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
831 /* whether policy verification hashing is enabled */
832 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
833 #ifdef CONFIG_SECURITY_APPARMOR_HASH
834 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
835 #endif
837 /* Debug mode */
838 bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
839 module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
841 /* Audit mode */
842 enum audit_mode aa_g_audit;
843 module_param_call(audit, param_set_audit, param_get_audit,
844 &aa_g_audit, S_IRUSR | S_IWUSR);
846 /* Determines if audit header is included in audited messages. This
847 * provides more context if the audit daemon is not running
849 bool aa_g_audit_header = true;
850 module_param_named(audit_header, aa_g_audit_header, aabool,
851 S_IRUSR | S_IWUSR);
853 /* lock out loading/removal of policy
854 * TODO: add in at boot loading of policy, which is the only way to
855 * load policy, if lock_policy is set
857 bool aa_g_lock_policy;
858 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
859 S_IRUSR | S_IWUSR);
861 /* Syscall logging mode */
862 bool aa_g_logsyscall;
863 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
865 /* Maximum pathname length before accesses will start getting rejected */
866 unsigned int aa_g_path_max = 2 * PATH_MAX;
867 module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
869 /* Determines how paranoid loading of policy is and how much verification
870 * on the loaded policy is done.
871 * DEPRECATED: read only as strict checking of load is always done now
872 * that none root users (user namespaces) can load policy.
874 bool aa_g_paranoid_load = true;
875 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
877 /* Boot time disable flag */
878 static bool apparmor_enabled = CONFIG_SECURITY_APPARMOR_BOOTPARAM_VALUE;
879 module_param_named(enabled, apparmor_enabled, bool, S_IRUGO);
881 static int __init apparmor_enabled_setup(char *str)
883 unsigned long enabled;
884 int error = kstrtoul(str, 0, &enabled);
885 if (!error)
886 apparmor_enabled = enabled ? 1 : 0;
887 return 1;
890 __setup("apparmor=", apparmor_enabled_setup);
892 /* set global flag turning off the ability to load policy */
893 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
895 if (!apparmor_enabled)
896 return -EINVAL;
897 if (apparmor_initialized && !policy_admin_capable(NULL))
898 return -EPERM;
899 return param_set_bool(val, kp);
902 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
904 if (!apparmor_enabled)
905 return -EINVAL;
906 if (apparmor_initialized && !policy_view_capable(NULL))
907 return -EPERM;
908 return param_get_bool(buffer, kp);
911 static int param_set_aabool(const char *val, const struct kernel_param *kp)
913 if (!apparmor_enabled)
914 return -EINVAL;
915 if (apparmor_initialized && !policy_admin_capable(NULL))
916 return -EPERM;
917 return param_set_bool(val, kp);
920 static int param_get_aabool(char *buffer, const struct kernel_param *kp)
922 if (!apparmor_enabled)
923 return -EINVAL;
924 if (apparmor_initialized && !policy_view_capable(NULL))
925 return -EPERM;
926 return param_get_bool(buffer, kp);
929 static int param_set_aauint(const char *val, const struct kernel_param *kp)
931 int error;
933 if (!apparmor_enabled)
934 return -EINVAL;
935 /* file is ro but enforce 2nd line check */
936 if (apparmor_initialized)
937 return -EPERM;
939 error = param_set_uint(val, kp);
940 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
942 return error;
945 static int param_get_aauint(char *buffer, const struct kernel_param *kp)
947 if (!apparmor_enabled)
948 return -EINVAL;
949 if (apparmor_initialized && !policy_view_capable(NULL))
950 return -EPERM;
951 return param_get_uint(buffer, kp);
954 static int param_get_audit(char *buffer, const struct kernel_param *kp)
956 if (!apparmor_enabled)
957 return -EINVAL;
958 if (apparmor_initialized && !policy_view_capable(NULL))
959 return -EPERM;
960 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
963 static int param_set_audit(const char *val, const struct kernel_param *kp)
965 int i;
967 if (!apparmor_enabled)
968 return -EINVAL;
969 if (!val)
970 return -EINVAL;
971 if (apparmor_initialized && !policy_admin_capable(NULL))
972 return -EPERM;
974 for (i = 0; i < AUDIT_MAX_INDEX; i++) {
975 if (strcmp(val, audit_mode_names[i]) == 0) {
976 aa_g_audit = i;
977 return 0;
981 return -EINVAL;
984 static int param_get_mode(char *buffer, const struct kernel_param *kp)
986 if (!apparmor_enabled)
987 return -EINVAL;
988 if (apparmor_initialized && !policy_view_capable(NULL))
989 return -EPERM;
991 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
994 static int param_set_mode(const char *val, const struct kernel_param *kp)
996 int i;
998 if (!apparmor_enabled)
999 return -EINVAL;
1000 if (!val)
1001 return -EINVAL;
1002 if (apparmor_initialized && !policy_admin_capable(NULL))
1003 return -EPERM;
1005 for (i = 0; i < APPARMOR_MODE_NAMES_MAX_INDEX; i++) {
1006 if (strcmp(val, aa_profile_mode_names[i]) == 0) {
1007 aa_g_profile_mode = i;
1008 return 0;
1012 return -EINVAL;
1016 * AppArmor init functions
1020 * set_init_ctx - set a task context and profile on the first task.
1022 * TODO: allow setting an alternate profile than unconfined
1024 static int __init set_init_ctx(void)
1026 struct cred *cred = (struct cred *)current->real_cred;
1027 struct aa_task_ctx *ctx;
1029 ctx = aa_alloc_task_context(GFP_KERNEL);
1030 if (!ctx)
1031 return -ENOMEM;
1033 ctx->label = aa_get_label(ns_unconfined(root_ns));
1034 cred_ctx(cred) = ctx;
1036 return 0;
1039 static void destroy_buffers(void)
1041 u32 i, j;
1043 for_each_possible_cpu(i) {
1044 for_each_cpu_buffer(j) {
1045 kfree(per_cpu(aa_buffers, i).buf[j]);
1046 per_cpu(aa_buffers, i).buf[j] = NULL;
1051 static int __init alloc_buffers(void)
1053 u32 i, j;
1055 for_each_possible_cpu(i) {
1056 for_each_cpu_buffer(j) {
1057 char *buffer;
1059 if (cpu_to_node(i) > num_online_nodes())
1060 /* fallback to kmalloc for offline nodes */
1061 buffer = kmalloc(aa_g_path_max, GFP_KERNEL);
1062 else
1063 buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL,
1064 cpu_to_node(i));
1065 if (!buffer) {
1066 destroy_buffers();
1067 return -ENOMEM;
1069 per_cpu(aa_buffers, i).buf[j] = buffer;
1073 return 0;
1076 #ifdef CONFIG_SYSCTL
1077 static int apparmor_dointvec(struct ctl_table *table, int write,
1078 void __user *buffer, size_t *lenp, loff_t *ppos)
1080 if (!policy_admin_capable(NULL))
1081 return -EPERM;
1082 if (!apparmor_enabled)
1083 return -EINVAL;
1085 return proc_dointvec(table, write, buffer, lenp, ppos);
1088 static struct ctl_path apparmor_sysctl_path[] = {
1089 { .procname = "kernel", },
1093 static struct ctl_table apparmor_sysctl_table[] = {
1095 .procname = "unprivileged_userns_apparmor_policy",
1096 .data = &unprivileged_userns_apparmor_policy,
1097 .maxlen = sizeof(int),
1098 .mode = 0600,
1099 .proc_handler = apparmor_dointvec,
1104 static int __init apparmor_init_sysctl(void)
1106 return register_sysctl_paths(apparmor_sysctl_path,
1107 apparmor_sysctl_table) ? 0 : -ENOMEM;
1109 #else
1110 static inline int apparmor_init_sysctl(void)
1112 return 0;
1114 #endif /* CONFIG_SYSCTL */
1116 static int __init apparmor_init(void)
1118 int error;
1120 if (!apparmor_enabled || !security_module_enable("apparmor")) {
1121 aa_info_message("AppArmor disabled by boot time parameter");
1122 apparmor_enabled = false;
1123 return 0;
1126 error = aa_setup_dfa_engine();
1127 if (error) {
1128 AA_ERROR("Unable to setup dfa engine\n");
1129 goto alloc_out;
1132 error = aa_alloc_root_ns();
1133 if (error) {
1134 AA_ERROR("Unable to allocate default profile namespace\n");
1135 goto alloc_out;
1138 error = apparmor_init_sysctl();
1139 if (error) {
1140 AA_ERROR("Unable to register sysctls\n");
1141 goto alloc_out;
1145 error = alloc_buffers();
1146 if (error) {
1147 AA_ERROR("Unable to allocate work buffers\n");
1148 goto buffers_out;
1151 error = set_init_ctx();
1152 if (error) {
1153 AA_ERROR("Failed to set context on init task\n");
1154 aa_free_root_ns();
1155 goto buffers_out;
1157 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1158 "apparmor");
1160 /* Report that AppArmor successfully initialized */
1161 apparmor_initialized = 1;
1162 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1163 aa_info_message("AppArmor initialized: complain mode enabled");
1164 else if (aa_g_profile_mode == APPARMOR_KILL)
1165 aa_info_message("AppArmor initialized: kill mode enabled");
1166 else
1167 aa_info_message("AppArmor initialized");
1169 return error;
1171 buffers_out:
1172 destroy_buffers();
1174 alloc_out:
1175 aa_destroy_aafs();
1176 aa_teardown_dfa_engine();
1178 apparmor_enabled = false;
1179 return error;
1182 security_initcall(apparmor_init);