1 // SPDX-License-Identifier: GPL-2.0-only
3 * AppArmor security module
5 * This file contains AppArmor LSM hooks.
7 * Copyright (C) 1998-2008 Novell/SUSE
8 * Copyright 2009-2010 Canonical Ltd.
11 #include <linux/lsm_hooks.h>
12 #include <linux/moduleparam.h>
14 #include <linux/mman.h>
15 #include <linux/mount.h>
16 #include <linux/namei.h>
17 #include <linux/ptrace.h>
18 #include <linux/ctype.h>
19 #include <linux/sysctl.h>
20 #include <linux/audit.h>
21 #include <linux/user_namespace.h>
22 #include <linux/netfilter_ipv4.h>
23 #include <linux/netfilter_ipv6.h>
24 #include <linux/zstd.h>
26 #include <uapi/linux/mount.h>
28 #include "include/apparmor.h"
29 #include "include/apparmorfs.h"
30 #include "include/audit.h"
31 #include "include/capability.h"
32 #include "include/cred.h"
33 #include "include/file.h"
34 #include "include/ipc.h"
35 #include "include/net.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"
42 #include "include/secid.h"
44 /* Flag indicating whether initialization completed */
45 int apparmor_initialized;
48 struct list_head list;
49 DECLARE_FLEX_ARRAY(char, buffer);
52 #define RESERVE_COUNT 2
53 static int reserve_count = RESERVE_COUNT;
54 static int buffer_count;
56 static LIST_HEAD(aa_global_buffers);
57 static DEFINE_SPINLOCK(aa_buffers_lock);
64 * put the associated labels
66 static void apparmor_cred_free(struct cred *cred)
68 aa_put_label(cred_label(cred));
69 set_cred_label(cred, NULL);
73 * allocate the apparmor part of blank credentials
75 static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
77 set_cred_label(cred, NULL);
82 * prepare new cred label for modification by prepare_cred block
84 static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
87 set_cred_label(new, aa_get_newest_label(cred_label(old)));
92 * transfer the apparmor data to a blank set of creds
94 static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
96 set_cred_label(new, aa_get_newest_label(cred_label(old)));
99 static void apparmor_task_free(struct task_struct *task)
102 aa_free_task_ctx(task_ctx(task));
105 static int apparmor_task_alloc(struct task_struct *task,
106 unsigned long clone_flags)
108 struct aa_task_ctx *new = task_ctx(task);
110 aa_dup_task_ctx(new, task_ctx(current));
115 static int apparmor_ptrace_access_check(struct task_struct *child,
118 struct aa_label *tracer, *tracee;
119 const struct cred *cred;
122 cred = get_task_cred(child);
123 tracee = cred_label(cred); /* ref count on cred */
124 tracer = __begin_current_label_crit_section();
125 error = aa_may_ptrace(current_cred(), tracer, cred, tracee,
126 (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
128 __end_current_label_crit_section(tracer);
134 static int apparmor_ptrace_traceme(struct task_struct *parent)
136 struct aa_label *tracer, *tracee;
137 const struct cred *cred;
140 tracee = __begin_current_label_crit_section();
141 cred = get_task_cred(parent);
142 tracer = cred_label(cred); /* ref count on cred */
143 error = aa_may_ptrace(cred, tracer, current_cred(), tracee,
146 __end_current_label_crit_section(tracee);
151 /* Derived from security/commoncap.c:cap_capget */
152 static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
153 kernel_cap_t *inheritable, kernel_cap_t *permitted)
155 struct aa_label *label;
156 const struct cred *cred;
159 cred = __task_cred(target);
160 label = aa_get_newest_cred_label(cred);
163 * cap_capget is stacked ahead of this and will
164 * initialize effective and permitted.
166 if (!unconfined(label)) {
167 struct aa_profile *profile;
170 label_for_each_confined(i, label, profile) {
171 struct aa_ruleset *rules;
172 if (COMPLAIN_MODE(profile))
174 rules = list_first_entry(&profile->rules,
175 typeof(*rules), list);
176 *effective = cap_intersect(*effective,
178 *permitted = cap_intersect(*permitted,
188 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
189 int cap, unsigned int opts)
191 struct aa_label *label;
194 label = aa_get_newest_cred_label(cred);
195 if (!unconfined(label))
196 error = aa_capable(cred, label, cap, opts);
203 * common_perm - basic common permission check wrapper fn for paths
204 * @op: operation being checked
205 * @path: path to check permission of (NOT NULL)
206 * @mask: requested permissions mask
207 * @cond: conditional info for the permission request (NOT NULL)
209 * Returns: %0 else error code if error or permission denied
211 static int common_perm(const char *op, const struct path *path, u32 mask,
212 struct path_cond *cond)
214 struct aa_label *label;
217 label = __begin_current_label_crit_section();
218 if (!unconfined(label))
219 error = aa_path_perm(op, current_cred(), label, path, 0, mask,
221 __end_current_label_crit_section(label);
227 * common_perm_cond - common permission wrapper around inode cond
228 * @op: operation being checked
229 * @path: location to check (NOT NULL)
230 * @mask: requested permissions mask
232 * Returns: %0 else error code if error or permission denied
234 static int common_perm_cond(const char *op, const struct path *path, u32 mask)
236 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
237 d_backing_inode(path->dentry));
238 struct path_cond cond = {
239 vfsuid_into_kuid(vfsuid),
240 d_backing_inode(path->dentry)->i_mode
243 if (!path_mediated_fs(path->dentry))
246 return common_perm(op, path, mask, &cond);
250 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
251 * @op: operation being checked
252 * @dir: directory of the dentry (NOT NULL)
253 * @dentry: dentry to check (NOT NULL)
254 * @mask: requested permissions mask
255 * @cond: conditional info for the permission request (NOT NULL)
257 * Returns: %0 else error code if error or permission denied
259 static int common_perm_dir_dentry(const char *op, const struct path *dir,
260 struct dentry *dentry, u32 mask,
261 struct path_cond *cond)
263 struct path path = { .mnt = dir->mnt, .dentry = dentry };
265 return common_perm(op, &path, mask, cond);
269 * common_perm_rm - common permission wrapper for operations doing rm
270 * @op: operation being checked
271 * @dir: directory that the dentry is in (NOT NULL)
272 * @dentry: dentry being rm'd (NOT NULL)
273 * @mask: requested permission mask
275 * Returns: %0 else error code if error or permission denied
277 static int common_perm_rm(const char *op, const struct path *dir,
278 struct dentry *dentry, u32 mask)
280 struct inode *inode = d_backing_inode(dentry);
281 struct path_cond cond = { };
284 if (!inode || !path_mediated_fs(dentry))
287 vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
288 cond.uid = vfsuid_into_kuid(vfsuid);
289 cond.mode = inode->i_mode;
291 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
295 * common_perm_create - common permission wrapper for operations doing create
296 * @op: operation being checked
297 * @dir: directory that dentry will be created in (NOT NULL)
298 * @dentry: dentry to create (NOT NULL)
299 * @mask: request permission mask
300 * @mode: created file mode
302 * Returns: %0 else error code if error or permission denied
304 static int common_perm_create(const char *op, const struct path *dir,
305 struct dentry *dentry, u32 mask, umode_t mode)
307 struct path_cond cond = { current_fsuid(), mode };
309 if (!path_mediated_fs(dir->dentry))
312 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
315 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
317 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
320 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
323 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
327 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
329 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
332 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
333 umode_t mode, unsigned int dev)
335 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
338 static int apparmor_path_truncate(const struct path *path)
340 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
343 static int apparmor_file_truncate(struct file *file)
345 return apparmor_path_truncate(&file->f_path);
348 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
349 const char *old_name)
351 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
355 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
356 struct dentry *new_dentry)
358 struct aa_label *label;
361 if (!path_mediated_fs(old_dentry))
364 label = begin_current_label_crit_section();
365 if (!unconfined(label))
366 error = aa_path_link(current_cred(), label, old_dentry, new_dir,
368 end_current_label_crit_section(label);
373 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
374 const struct path *new_dir, struct dentry *new_dentry,
375 const unsigned int flags)
377 struct aa_label *label;
380 if (!path_mediated_fs(old_dentry))
382 if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
385 label = begin_current_label_crit_section();
386 if (!unconfined(label)) {
387 struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
389 struct path old_path = { .mnt = old_dir->mnt,
390 .dentry = old_dentry };
391 struct path new_path = { .mnt = new_dir->mnt,
392 .dentry = new_dentry };
393 struct path_cond cond = {
394 .mode = d_backing_inode(old_dentry)->i_mode
396 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
397 cond.uid = vfsuid_into_kuid(vfsuid);
399 if (flags & RENAME_EXCHANGE) {
400 struct path_cond cond_exchange = {
401 .mode = d_backing_inode(new_dentry)->i_mode,
403 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
404 cond_exchange.uid = vfsuid_into_kuid(vfsuid);
406 error = aa_path_perm(OP_RENAME_SRC, current_cred(),
408 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
409 AA_MAY_SETATTR | AA_MAY_DELETE,
412 error = aa_path_perm(OP_RENAME_DEST, current_cred(),
414 0, MAY_WRITE | AA_MAY_SETATTR |
415 AA_MAY_CREATE, &cond_exchange);
419 error = aa_path_perm(OP_RENAME_SRC, current_cred(),
421 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
422 AA_MAY_SETATTR | AA_MAY_DELETE,
425 error = aa_path_perm(OP_RENAME_DEST, current_cred(),
427 0, MAY_WRITE | AA_MAY_SETATTR |
428 AA_MAY_CREATE, &cond);
431 end_current_label_crit_section(label);
436 static int apparmor_path_chmod(const struct path *path, umode_t mode)
438 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
441 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
443 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
446 static int apparmor_inode_getattr(const struct path *path)
448 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
451 static int apparmor_file_open(struct file *file)
453 struct aa_file_ctx *fctx = file_ctx(file);
454 struct aa_label *label;
457 if (!path_mediated_fs(file->f_path.dentry))
460 /* If in exec, permission is handled by bprm hooks.
461 * Cache permissions granted by the previous exec check, with
462 * implicit read and executable mmap which are required to
463 * actually execute the image.
465 if (current->in_execve) {
466 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
470 label = aa_get_newest_cred_label(file->f_cred);
471 if (!unconfined(label)) {
472 struct mnt_idmap *idmap = file_mnt_idmap(file);
473 struct inode *inode = file_inode(file);
475 struct path_cond cond = {
476 .mode = inode->i_mode,
478 vfsuid = i_uid_into_vfsuid(idmap, inode);
479 cond.uid = vfsuid_into_kuid(vfsuid);
481 error = aa_path_perm(OP_OPEN, file->f_cred,
482 label, &file->f_path, 0,
483 aa_map_file_to_perms(file), &cond);
484 /* todo cache full allowed permissions set and state */
485 fctx->allow = aa_map_file_to_perms(file);
492 static int apparmor_file_alloc_security(struct file *file)
494 struct aa_file_ctx *ctx = file_ctx(file);
495 struct aa_label *label = begin_current_label_crit_section();
497 spin_lock_init(&ctx->lock);
498 rcu_assign_pointer(ctx->label, aa_get_label(label));
499 end_current_label_crit_section(label);
503 static void apparmor_file_free_security(struct file *file)
505 struct aa_file_ctx *ctx = file_ctx(file);
508 aa_put_label(rcu_access_pointer(ctx->label));
511 static int common_file_perm(const char *op, struct file *file, u32 mask,
514 struct aa_label *label;
517 /* don't reaudit files closed during inheritance */
518 if (file->f_path.dentry == aa_null.dentry)
521 label = __begin_current_label_crit_section();
522 error = aa_file_perm(op, current_cred(), label, file, mask, in_atomic);
523 __end_current_label_crit_section(label);
528 static int apparmor_file_receive(struct file *file)
530 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
534 static int apparmor_file_permission(struct file *file, int mask)
536 return common_file_perm(OP_FPERM, file, mask, false);
539 static int apparmor_file_lock(struct file *file, unsigned int cmd)
541 u32 mask = AA_MAY_LOCK;
546 return common_file_perm(OP_FLOCK, file, mask, false);
549 static int common_mmap(const char *op, struct file *file, unsigned long prot,
550 unsigned long flags, bool in_atomic)
554 if (!file || !file_ctx(file))
557 if (prot & PROT_READ)
560 * Private mappings don't require write perms since they don't
561 * write back to the files
563 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
565 if (prot & PROT_EXEC)
566 mask |= AA_EXEC_MMAP;
568 return common_file_perm(op, file, mask, in_atomic);
571 static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
572 unsigned long prot, unsigned long flags)
574 return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
577 static int apparmor_file_mprotect(struct vm_area_struct *vma,
578 unsigned long reqprot, unsigned long prot)
580 return common_mmap(OP_FMPROT, vma->vm_file, prot,
581 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
585 static int apparmor_sb_mount(const char *dev_name, const struct path *path,
586 const char *type, unsigned long flags, void *data)
588 struct aa_label *label;
592 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
593 flags &= ~MS_MGC_MSK;
595 flags &= ~AA_MS_IGNORE_MASK;
597 label = __begin_current_label_crit_section();
598 if (!unconfined(label)) {
599 if (flags & MS_REMOUNT)
600 error = aa_remount(current_cred(), label, path, flags,
602 else if (flags & MS_BIND)
603 error = aa_bind_mount(current_cred(), label, path,
605 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
607 error = aa_mount_change_type(current_cred(), label,
609 else if (flags & MS_MOVE)
610 error = aa_move_mount_old(current_cred(), label, path,
613 error = aa_new_mount(current_cred(), label, dev_name,
614 path, type, flags, data);
616 __end_current_label_crit_section(label);
621 static int apparmor_move_mount(const struct path *from_path,
622 const struct path *to_path)
624 struct aa_label *label;
627 label = __begin_current_label_crit_section();
628 if (!unconfined(label))
629 error = aa_move_mount(current_cred(), label, from_path,
631 __end_current_label_crit_section(label);
636 static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
638 struct aa_label *label;
641 label = __begin_current_label_crit_section();
642 if (!unconfined(label))
643 error = aa_umount(current_cred(), label, mnt, flags);
644 __end_current_label_crit_section(label);
649 static int apparmor_sb_pivotroot(const struct path *old_path,
650 const struct path *new_path)
652 struct aa_label *label;
655 label = aa_get_current_label();
656 if (!unconfined(label))
657 error = aa_pivotroot(current_cred(), label, old_path, new_path);
663 static int apparmor_getprocattr(struct task_struct *task, const char *name,
668 const struct cred *cred = get_task_cred(task);
669 struct aa_task_ctx *ctx = task_ctx(current);
670 struct aa_label *label = NULL;
672 if (strcmp(name, "current") == 0)
673 label = aa_get_newest_label(cred_label(cred));
674 else if (strcmp(name, "prev") == 0 && ctx->previous)
675 label = aa_get_newest_label(ctx->previous);
676 else if (strcmp(name, "exec") == 0 && ctx->onexec)
677 label = aa_get_newest_label(ctx->onexec);
682 error = aa_getprocattr(label, value);
690 static int apparmor_setprocattr(const char *name, void *value,
693 char *command, *largs = NULL, *args = value;
696 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
702 /* AppArmor requires that the buffer must be null terminated atm */
703 if (args[size - 1] != '\0') {
705 largs = args = kmalloc(size + 1, GFP_KERNEL);
708 memcpy(args, value, size);
714 command = strsep(&args, " ");
717 args = skip_spaces(args);
721 arg_size = size - (args - (largs ? largs : (char *) value));
722 if (strcmp(name, "current") == 0) {
723 if (strcmp(command, "changehat") == 0) {
724 error = aa_setprocattr_changehat(args, arg_size,
726 } else if (strcmp(command, "permhat") == 0) {
727 error = aa_setprocattr_changehat(args, arg_size,
729 } else if (strcmp(command, "changeprofile") == 0) {
730 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
731 } else if (strcmp(command, "permprofile") == 0) {
732 error = aa_change_profile(args, AA_CHANGE_TEST);
733 } else if (strcmp(command, "stack") == 0) {
734 error = aa_change_profile(args, AA_CHANGE_STACK);
737 } else if (strcmp(name, "exec") == 0) {
738 if (strcmp(command, "exec") == 0)
739 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
740 else if (strcmp(command, "stack") == 0)
741 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
746 /* only support the "current" and "exec" process attributes */
756 ad.subj_label = begin_current_label_crit_section();
758 ad.error = error = -EINVAL;
759 aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL);
760 end_current_label_crit_section(ad.subj_label);
765 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
766 * @bprm: binprm for the exec (NOT NULL)
768 static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
770 struct aa_label *label = aa_current_raw_label();
771 struct aa_label *new_label = cred_label(bprm->cred);
773 /* bail out if unconfined or not changing profile */
774 if ((new_label->proxy == label->proxy) ||
775 (unconfined(new_label)))
778 aa_inherit_files(bprm->cred, current->files);
780 current->pdeath_signal = 0;
782 /* reset soft limits and set hard limits for the new label */
783 __aa_transition_rlimits(label, new_label);
787 * apparmor_bprm_committed_creds() - do cleanup after new creds committed
788 * @bprm: binprm for the exec (NOT NULL)
790 static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
792 /* clear out temporary/transitional state from the context */
793 aa_clear_task_ctx_trans(task_ctx(current));
798 static void apparmor_current_getsecid_subj(u32 *secid)
800 struct aa_label *label = aa_get_current_label();
801 *secid = label->secid;
805 static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
807 struct aa_label *label = aa_get_task_label(p);
808 *secid = label->secid;
812 static int apparmor_task_setrlimit(struct task_struct *task,
813 unsigned int resource, struct rlimit *new_rlim)
815 struct aa_label *label = __begin_current_label_crit_section();
818 if (!unconfined(label))
819 error = aa_task_setrlimit(current_cred(), label, task,
821 __end_current_label_crit_section(label);
826 static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
827 int sig, const struct cred *cred)
829 const struct cred *tc;
830 struct aa_label *cl, *tl;
833 tc = get_task_cred(target);
834 tl = aa_get_newest_cred_label(tc);
837 * Dealing with USB IO specific behavior
839 cl = aa_get_newest_cred_label(cred);
840 error = aa_may_signal(cred, cl, tc, tl, sig);
843 cl = __begin_current_label_crit_section();
844 error = aa_may_signal(current_cred(), cl, tc, tl, sig);
845 __end_current_label_crit_section(cl);
854 * apparmor_sk_alloc_security - allocate and attach the sk_security field
856 static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
858 struct aa_sk_ctx *ctx;
860 ctx = kzalloc(sizeof(*ctx), flags);
870 * apparmor_sk_free_security - free the sk_security field
872 static void apparmor_sk_free_security(struct sock *sk)
874 struct aa_sk_ctx *ctx = SK_CTX(sk);
877 aa_put_label(ctx->label);
878 aa_put_label(ctx->peer);
883 * apparmor_sk_clone_security - clone the sk_security field
885 static void apparmor_sk_clone_security(const struct sock *sk,
888 struct aa_sk_ctx *ctx = SK_CTX(sk);
889 struct aa_sk_ctx *new = SK_CTX(newsk);
892 aa_put_label(new->label);
893 new->label = aa_get_label(ctx->label);
896 aa_put_label(new->peer);
897 new->peer = aa_get_label(ctx->peer);
901 * apparmor_socket_create - check perms before creating a new socket
903 static int apparmor_socket_create(int family, int type, int protocol, int kern)
905 struct aa_label *label;
908 AA_BUG(in_interrupt());
910 label = begin_current_label_crit_section();
911 if (!(kern || unconfined(label)))
912 error = af_select(family,
913 create_perm(label, family, type, protocol),
914 aa_af_perm(current_cred(), label,
915 OP_CREATE, AA_MAY_CREATE,
916 family, type, protocol));
917 end_current_label_crit_section(label);
923 * apparmor_socket_post_create - setup the per-socket security struct
926 * - kernel sockets currently labeled unconfined but we may want to
927 * move to a special kernel label
928 * - socket may not have sk here if created with sock_create_lite or
929 * sock_alloc. These should be accept cases which will be handled in
932 static int apparmor_socket_post_create(struct socket *sock, int family,
933 int type, int protocol, int kern)
935 struct aa_label *label;
938 label = aa_get_label(kernel_t);
940 label = aa_get_current_label();
943 struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
945 aa_put_label(ctx->label);
946 ctx->label = aa_get_label(label);
954 * apparmor_socket_bind - check perms before bind addr to socket
956 static int apparmor_socket_bind(struct socket *sock,
957 struct sockaddr *address, int addrlen)
962 AA_BUG(in_interrupt());
964 return af_select(sock->sk->sk_family,
965 bind_perm(sock, address, addrlen),
966 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
970 * apparmor_socket_connect - check perms before connecting @sock to @address
972 static int apparmor_socket_connect(struct socket *sock,
973 struct sockaddr *address, int addrlen)
978 AA_BUG(in_interrupt());
980 return af_select(sock->sk->sk_family,
981 connect_perm(sock, address, addrlen),
982 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
986 * apparmor_socket_listen - check perms before allowing listen
988 static int apparmor_socket_listen(struct socket *sock, int backlog)
992 AA_BUG(in_interrupt());
994 return af_select(sock->sk->sk_family,
995 listen_perm(sock, backlog),
996 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
1000 * apparmor_socket_accept - check perms before accepting a new connection.
1002 * Note: while @newsock is created and has some information, the accept
1003 * has not been done.
1005 static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
1010 AA_BUG(in_interrupt());
1012 return af_select(sock->sk->sk_family,
1013 accept_perm(sock, newsock),
1014 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
1017 static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
1018 struct msghdr *msg, int size)
1023 AA_BUG(in_interrupt());
1025 return af_select(sock->sk->sk_family,
1026 msg_perm(op, request, sock, msg, size),
1027 aa_sk_perm(op, request, sock->sk));
1031 * apparmor_socket_sendmsg - check perms before sending msg to another socket
1033 static int apparmor_socket_sendmsg(struct socket *sock,
1034 struct msghdr *msg, int size)
1036 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
1040 * apparmor_socket_recvmsg - check perms before receiving a message
1042 static int apparmor_socket_recvmsg(struct socket *sock,
1043 struct msghdr *msg, int size, int flags)
1045 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
1048 /* revaliation, get/set attr, shutdown */
1049 static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
1053 AA_BUG(in_interrupt());
1055 return af_select(sock->sk->sk_family,
1056 sock_perm(op, request, sock),
1057 aa_sk_perm(op, request, sock->sk));
1061 * apparmor_socket_getsockname - check perms before getting the local address
1063 static int apparmor_socket_getsockname(struct socket *sock)
1065 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
1069 * apparmor_socket_getpeername - check perms before getting remote address
1071 static int apparmor_socket_getpeername(struct socket *sock)
1073 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
1076 /* revaliation, get/set attr, opt */
1077 static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
1078 int level, int optname)
1082 AA_BUG(in_interrupt());
1084 return af_select(sock->sk->sk_family,
1085 opt_perm(op, request, sock, level, optname),
1086 aa_sk_perm(op, request, sock->sk));
1090 * apparmor_socket_getsockopt - check perms before getting socket options
1092 static int apparmor_socket_getsockopt(struct socket *sock, int level,
1095 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1100 * apparmor_socket_setsockopt - check perms before setting socket options
1102 static int apparmor_socket_setsockopt(struct socket *sock, int level,
1105 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1110 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1112 static int apparmor_socket_shutdown(struct socket *sock, int how)
1114 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1117 #ifdef CONFIG_NETWORK_SECMARK
1119 * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
1121 * Note: can not sleep may be called with locks held
1123 * dont want protocol specific in __skb_recv_datagram()
1124 * to deny an incoming connection socket_sock_rcv_skb()
1126 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1128 struct aa_sk_ctx *ctx = SK_CTX(sk);
1133 return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1139 static struct aa_label *sk_peer_label(struct sock *sk)
1141 struct aa_sk_ctx *ctx = SK_CTX(sk);
1146 return ERR_PTR(-ENOPROTOOPT);
1150 * apparmor_socket_getpeersec_stream - get security context of peer
1152 * Note: for tcp only valid if using ipsec or cipso on lan
1154 static int apparmor_socket_getpeersec_stream(struct socket *sock,
1155 sockptr_t optval, sockptr_t optlen,
1159 int slen, error = 0;
1160 struct aa_label *label;
1161 struct aa_label *peer;
1163 label = begin_current_label_crit_section();
1164 peer = sk_peer_label(sock->sk);
1166 error = PTR_ERR(peer);
1169 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1170 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1171 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1172 /* don't include terminating \0 in slen, it breaks some apps */
1182 if (copy_to_sockptr(optval, name, slen))
1185 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
1188 end_current_label_crit_section(label);
1194 * apparmor_socket_getpeersec_dgram - get security label of packet
1195 * @sock: the peer socket
1197 * @secid: pointer to where to put the secid of the packet
1199 * Sets the netlabel socket state on sk from parent
1201 static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1202 struct sk_buff *skb, u32 *secid)
1205 /* TODO: requires secid support */
1206 return -ENOPROTOOPT;
1210 * apparmor_sock_graft - Initialize newly created socket
1212 * @parent: parent socket
1214 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1215 * just set sk security information off of current creating process label
1216 * Labeling of sk for accept case - probably should be sock based
1217 * instead of task, because of the case where an implicitly labeled
1218 * socket is shared by different tasks.
1220 static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1222 struct aa_sk_ctx *ctx = SK_CTX(sk);
1225 ctx->label = aa_get_current_label();
1228 #ifdef CONFIG_NETWORK_SECMARK
1229 static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1230 struct request_sock *req)
1232 struct aa_sk_ctx *ctx = SK_CTX(sk);
1237 return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1243 * The cred blob is a pointer to, not an instance of, an aa_label.
1245 struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
1246 .lbs_cred = sizeof(struct aa_label *),
1247 .lbs_file = sizeof(struct aa_file_ctx),
1248 .lbs_task = sizeof(struct aa_task_ctx),
1251 static struct security_hook_list apparmor_hooks[] __ro_after_init = {
1252 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1253 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1254 LSM_HOOK_INIT(capget, apparmor_capget),
1255 LSM_HOOK_INIT(capable, apparmor_capable),
1257 LSM_HOOK_INIT(move_mount, apparmor_move_mount),
1258 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1259 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1260 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1262 LSM_HOOK_INIT(path_link, apparmor_path_link),
1263 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1264 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1265 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1266 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1267 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1268 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1269 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1270 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1271 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1272 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1274 LSM_HOOK_INIT(file_open, apparmor_file_open),
1275 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1276 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1277 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1278 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1279 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1280 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1281 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
1282 LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
1284 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1285 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1287 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1288 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1289 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1291 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1292 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1293 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1294 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1295 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1296 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1297 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1298 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1299 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1300 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1301 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1302 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1303 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1304 #ifdef CONFIG_NETWORK_SECMARK
1305 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1307 LSM_HOOK_INIT(socket_getpeersec_stream,
1308 apparmor_socket_getpeersec_stream),
1309 LSM_HOOK_INIT(socket_getpeersec_dgram,
1310 apparmor_socket_getpeersec_dgram),
1311 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1312 #ifdef CONFIG_NETWORK_SECMARK
1313 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1316 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1317 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1318 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1319 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1321 LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1322 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1323 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1325 LSM_HOOK_INIT(task_free, apparmor_task_free),
1326 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
1327 LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj),
1328 LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
1329 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1330 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1333 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1334 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1335 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1336 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1339 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1340 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1341 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1345 * AppArmor sysfs module parameters
1348 static int param_set_aabool(const char *val, const struct kernel_param *kp);
1349 static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1350 #define param_check_aabool param_check_bool
1351 static const struct kernel_param_ops param_ops_aabool = {
1352 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1353 .set = param_set_aabool,
1354 .get = param_get_aabool
1357 static int param_set_aauint(const char *val, const struct kernel_param *kp);
1358 static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1359 #define param_check_aauint param_check_uint
1360 static const struct kernel_param_ops param_ops_aauint = {
1361 .set = param_set_aauint,
1362 .get = param_get_aauint
1365 static int param_set_aacompressionlevel(const char *val,
1366 const struct kernel_param *kp);
1367 static int param_get_aacompressionlevel(char *buffer,
1368 const struct kernel_param *kp);
1369 #define param_check_aacompressionlevel param_check_int
1370 static const struct kernel_param_ops param_ops_aacompressionlevel = {
1371 .set = param_set_aacompressionlevel,
1372 .get = param_get_aacompressionlevel
1375 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1376 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1377 #define param_check_aalockpolicy param_check_bool
1378 static const struct kernel_param_ops param_ops_aalockpolicy = {
1379 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1380 .set = param_set_aalockpolicy,
1381 .get = param_get_aalockpolicy
1384 static int param_set_audit(const char *val, const struct kernel_param *kp);
1385 static int param_get_audit(char *buffer, const struct kernel_param *kp);
1387 static int param_set_mode(const char *val, const struct kernel_param *kp);
1388 static int param_get_mode(char *buffer, const struct kernel_param *kp);
1390 /* Flag values, also controllable via /sys/module/apparmor/parameters
1391 * We define special types as we want to do additional mediation.
1394 /* AppArmor global enforcement switch - complain, enforce, kill */
1395 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1396 module_param_call(mode, param_set_mode, param_get_mode,
1397 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1399 /* whether policy verification hashing is enabled */
1400 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
1401 #ifdef CONFIG_SECURITY_APPARMOR_HASH
1402 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1405 /* whether policy exactly as loaded is retained for debug and checkpointing */
1406 bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1407 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1408 module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1411 /* policy loaddata compression level */
1412 int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
1413 module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
1414 aacompressionlevel, 0400);
1417 bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
1418 module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1421 enum audit_mode aa_g_audit;
1422 module_param_call(audit, param_set_audit, param_get_audit,
1423 &aa_g_audit, S_IRUSR | S_IWUSR);
1425 /* Determines if audit header is included in audited messages. This
1426 * provides more context if the audit daemon is not running
1428 bool aa_g_audit_header = true;
1429 module_param_named(audit_header, aa_g_audit_header, aabool,
1432 /* lock out loading/removal of policy
1433 * TODO: add in at boot loading of policy, which is the only way to
1434 * load policy, if lock_policy is set
1436 bool aa_g_lock_policy;
1437 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1440 /* Syscall logging mode */
1441 bool aa_g_logsyscall;
1442 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1444 /* Maximum pathname length before accesses will start getting rejected */
1445 unsigned int aa_g_path_max = 2 * PATH_MAX;
1446 module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1448 /* Determines how paranoid loading of policy is and how much verification
1449 * on the loaded policy is done.
1450 * DEPRECATED: read only as strict checking of load is always done now
1451 * that none root users (user namespaces) can load policy.
1453 bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1454 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1456 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1457 static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1458 #define param_check_aaintbool param_check_int
1459 static const struct kernel_param_ops param_ops_aaintbool = {
1460 .set = param_set_aaintbool,
1461 .get = param_get_aaintbool
1463 /* Boot time disable flag */
1464 static int apparmor_enabled __ro_after_init = 1;
1465 module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1467 static int __init apparmor_enabled_setup(char *str)
1469 unsigned long enabled;
1470 int error = kstrtoul(str, 0, &enabled);
1472 apparmor_enabled = enabled ? 1 : 0;
1476 __setup("apparmor=", apparmor_enabled_setup);
1478 /* set global flag turning off the ability to load policy */
1479 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1481 if (!apparmor_enabled)
1483 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1485 return param_set_bool(val, kp);
1488 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1490 if (!apparmor_enabled)
1492 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1494 return param_get_bool(buffer, kp);
1497 static int param_set_aabool(const char *val, const struct kernel_param *kp)
1499 if (!apparmor_enabled)
1501 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1503 return param_set_bool(val, kp);
1506 static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1508 if (!apparmor_enabled)
1510 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1512 return param_get_bool(buffer, kp);
1515 static int param_set_aauint(const char *val, const struct kernel_param *kp)
1519 if (!apparmor_enabled)
1521 /* file is ro but enforce 2nd line check */
1522 if (apparmor_initialized)
1525 error = param_set_uint(val, kp);
1526 aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
1527 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1532 static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1534 if (!apparmor_enabled)
1536 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1538 return param_get_uint(buffer, kp);
1541 /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
1542 static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1544 struct kernel_param kp_local;
1548 if (apparmor_initialized)
1551 /* Create local copy, with arg pointing to bool type. */
1552 value = !!*((int *)kp->arg);
1553 memcpy(&kp_local, kp, sizeof(kp_local));
1554 kp_local.arg = &value;
1556 error = param_set_bool(val, &kp_local);
1558 *((int *)kp->arg) = *((bool *)kp_local.arg);
1563 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1564 * 1/0, this converts the "int that is actually bool" back to bool for
1565 * display in the /sys filesystem, while keeping it "int" for the LSM
1568 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1570 struct kernel_param kp_local;
1573 /* Create local copy, with arg pointing to bool type. */
1574 value = !!*((int *)kp->arg);
1575 memcpy(&kp_local, kp, sizeof(kp_local));
1576 kp_local.arg = &value;
1578 return param_get_bool(buffer, &kp_local);
1581 static int param_set_aacompressionlevel(const char *val,
1582 const struct kernel_param *kp)
1586 if (!apparmor_enabled)
1588 if (apparmor_initialized)
1591 error = param_set_int(val, kp);
1593 aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
1594 AA_MIN_CLEVEL, AA_MAX_CLEVEL);
1595 pr_info("AppArmor: policy rawdata compression level set to %d\n",
1596 aa_g_rawdata_compression_level);
1601 static int param_get_aacompressionlevel(char *buffer,
1602 const struct kernel_param *kp)
1604 if (!apparmor_enabled)
1606 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1608 return param_get_int(buffer, kp);
1611 static int param_get_audit(char *buffer, const struct kernel_param *kp)
1613 if (!apparmor_enabled)
1615 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1617 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1620 static int param_set_audit(const char *val, const struct kernel_param *kp)
1624 if (!apparmor_enabled)
1628 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1631 i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
1639 static int param_get_mode(char *buffer, const struct kernel_param *kp)
1641 if (!apparmor_enabled)
1643 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1646 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1649 static int param_set_mode(const char *val, const struct kernel_param *kp)
1653 if (!apparmor_enabled)
1657 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1660 i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
1665 aa_g_profile_mode = i;
1669 char *aa_get_buffer(bool in_atomic)
1671 union aa_buffer *aa_buf;
1672 bool try_again = true;
1673 gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1676 spin_lock(&aa_buffers_lock);
1677 if (buffer_count > reserve_count ||
1678 (in_atomic && !list_empty(&aa_global_buffers))) {
1679 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1681 list_del(&aa_buf->list);
1683 spin_unlock(&aa_buffers_lock);
1684 return aa_buf->buffer;
1688 * out of reserve buffers and in atomic context so increase
1689 * how many buffers to keep in reserve
1694 spin_unlock(&aa_buffers_lock);
1698 aa_buf = kmalloc(aa_g_path_max, flags);
1704 pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
1707 return aa_buf->buffer;
1710 void aa_put_buffer(char *buf)
1712 union aa_buffer *aa_buf;
1716 aa_buf = container_of(buf, union aa_buffer, buffer[0]);
1718 spin_lock(&aa_buffers_lock);
1719 list_add(&aa_buf->list, &aa_global_buffers);
1721 spin_unlock(&aa_buffers_lock);
1725 * AppArmor init functions
1729 * set_init_ctx - set a task context and profile on the first task.
1731 * TODO: allow setting an alternate profile than unconfined
1733 static int __init set_init_ctx(void)
1735 struct cred *cred = (__force struct cred *)current->real_cred;
1737 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
1742 static void destroy_buffers(void)
1744 union aa_buffer *aa_buf;
1746 spin_lock(&aa_buffers_lock);
1747 while (!list_empty(&aa_global_buffers)) {
1748 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1750 list_del(&aa_buf->list);
1751 spin_unlock(&aa_buffers_lock);
1753 spin_lock(&aa_buffers_lock);
1755 spin_unlock(&aa_buffers_lock);
1758 static int __init alloc_buffers(void)
1760 union aa_buffer *aa_buf;
1764 * A function may require two buffers at once. Usually the buffers are
1765 * used for a short period of time and are shared. On UP kernel buffers
1766 * two should be enough, with more CPUs it is possible that more
1767 * buffers will be used simultaneously. The preallocated pool may grow.
1768 * This preallocation has also the side-effect that AppArmor will be
1769 * disabled early at boot if aa_g_path_max is extremly high.
1771 if (num_online_cpus() > 1)
1772 num = 4 + RESERVE_COUNT;
1774 num = 2 + RESERVE_COUNT;
1776 for (i = 0; i < num; i++) {
1778 aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
1779 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1784 aa_put_buffer(aa_buf->buffer);
1789 #ifdef CONFIG_SYSCTL
1790 static int apparmor_dointvec(struct ctl_table *table, int write,
1791 void *buffer, size_t *lenp, loff_t *ppos)
1793 if (!aa_current_policy_admin_capable(NULL))
1795 if (!apparmor_enabled)
1798 return proc_dointvec(table, write, buffer, lenp, ppos);
1801 static struct ctl_table apparmor_sysctl_table[] = {
1803 .procname = "unprivileged_userns_apparmor_policy",
1804 .data = &unprivileged_userns_apparmor_policy,
1805 .maxlen = sizeof(int),
1807 .proc_handler = apparmor_dointvec,
1810 .procname = "apparmor_display_secid_mode",
1811 .data = &apparmor_display_secid_mode,
1812 .maxlen = sizeof(int),
1814 .proc_handler = apparmor_dointvec,
1820 static int __init apparmor_init_sysctl(void)
1822 return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
1825 static inline int apparmor_init_sysctl(void)
1829 #endif /* CONFIG_SYSCTL */
1831 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
1832 static unsigned int apparmor_ip_postroute(void *priv,
1833 struct sk_buff *skb,
1834 const struct nf_hook_state *state)
1836 struct aa_sk_ctx *ctx;
1842 sk = skb_to_full_sk(skb);
1847 if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
1851 return NF_DROP_ERR(-ECONNREFUSED);
1855 static const struct nf_hook_ops apparmor_nf_ops[] = {
1857 .hook = apparmor_ip_postroute,
1859 .hooknum = NF_INET_POST_ROUTING,
1860 .priority = NF_IP_PRI_SELINUX_FIRST,
1862 #if IS_ENABLED(CONFIG_IPV6)
1864 .hook = apparmor_ip_postroute,
1866 .hooknum = NF_INET_POST_ROUTING,
1867 .priority = NF_IP6_PRI_SELINUX_FIRST,
1872 static int __net_init apparmor_nf_register(struct net *net)
1874 return nf_register_net_hooks(net, apparmor_nf_ops,
1875 ARRAY_SIZE(apparmor_nf_ops));
1878 static void __net_exit apparmor_nf_unregister(struct net *net)
1880 nf_unregister_net_hooks(net, apparmor_nf_ops,
1881 ARRAY_SIZE(apparmor_nf_ops));
1884 static struct pernet_operations apparmor_net_ops = {
1885 .init = apparmor_nf_register,
1886 .exit = apparmor_nf_unregister,
1889 static int __init apparmor_nf_ip_init(void)
1893 if (!apparmor_enabled)
1896 err = register_pernet_subsys(&apparmor_net_ops);
1898 panic("Apparmor: register_pernet_subsys: error %d\n", err);
1902 __initcall(apparmor_nf_ip_init);
1905 static int __init apparmor_init(void)
1909 error = aa_setup_dfa_engine();
1911 AA_ERROR("Unable to setup dfa engine\n");
1915 error = aa_alloc_root_ns();
1917 AA_ERROR("Unable to allocate default profile namespace\n");
1921 error = apparmor_init_sysctl();
1923 AA_ERROR("Unable to register sysctls\n");
1928 error = alloc_buffers();
1930 AA_ERROR("Unable to allocate work buffers\n");
1934 error = set_init_ctx();
1936 AA_ERROR("Failed to set context on init task\n");
1940 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1943 /* Report that AppArmor successfully initialized */
1944 apparmor_initialized = 1;
1945 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1946 aa_info_message("AppArmor initialized: complain mode enabled");
1947 else if (aa_g_profile_mode == APPARMOR_KILL)
1948 aa_info_message("AppArmor initialized: kill mode enabled");
1950 aa_info_message("AppArmor initialized");
1958 aa_teardown_dfa_engine();
1960 apparmor_enabled = false;
1964 DEFINE_LSM(apparmor) = {
1966 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
1967 .enabled = &apparmor_enabled,
1968 .blobs = &apparmor_blob_sizes,
1969 .init = apparmor_init,