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;
121 tracer = __begin_current_label_crit_section();
122 tracee = aa_get_task_label(child);
123 error = aa_may_ptrace(tracer, tracee,
124 (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
126 aa_put_label(tracee);
127 __end_current_label_crit_section(tracer);
132 static int apparmor_ptrace_traceme(struct task_struct *parent)
134 struct aa_label *tracer, *tracee;
137 tracee = __begin_current_label_crit_section();
138 tracer = aa_get_task_label(parent);
139 error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE);
140 aa_put_label(tracer);
141 __end_current_label_crit_section(tracee);
146 /* Derived from security/commoncap.c:cap_capget */
147 static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
148 kernel_cap_t *inheritable, kernel_cap_t *permitted)
150 struct aa_label *label;
151 const struct cred *cred;
154 cred = __task_cred(target);
155 label = aa_get_newest_cred_label(cred);
158 * cap_capget is stacked ahead of this and will
159 * initialize effective and permitted.
161 if (!unconfined(label)) {
162 struct aa_profile *profile;
165 label_for_each_confined(i, label, profile) {
166 struct aa_ruleset *rules;
167 if (COMPLAIN_MODE(profile))
169 rules = list_first_entry(&profile->rules,
170 typeof(*rules), list);
171 *effective = cap_intersect(*effective,
173 *permitted = cap_intersect(*permitted,
183 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
184 int cap, unsigned int opts)
186 struct aa_label *label;
189 label = aa_get_newest_cred_label(cred);
190 if (!unconfined(label))
191 error = aa_capable(label, cap, opts);
198 * common_perm - basic common permission check wrapper fn for paths
199 * @op: operation being checked
200 * @path: path to check permission of (NOT NULL)
201 * @mask: requested permissions mask
202 * @cond: conditional info for the permission request (NOT NULL)
204 * Returns: %0 else error code if error or permission denied
206 static int common_perm(const char *op, const struct path *path, u32 mask,
207 struct path_cond *cond)
209 struct aa_label *label;
212 label = __begin_current_label_crit_section();
213 if (!unconfined(label))
214 error = aa_path_perm(op, label, path, 0, mask, cond);
215 __end_current_label_crit_section(label);
221 * common_perm_cond - common permission wrapper around inode cond
222 * @op: operation being checked
223 * @path: location to check (NOT NULL)
224 * @mask: requested permissions mask
226 * Returns: %0 else error code if error or permission denied
228 static int common_perm_cond(const char *op, const struct path *path, u32 mask)
230 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
231 d_backing_inode(path->dentry));
232 struct path_cond cond = {
233 vfsuid_into_kuid(vfsuid),
234 d_backing_inode(path->dentry)->i_mode
237 if (!path_mediated_fs(path->dentry))
240 return common_perm(op, path, mask, &cond);
244 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
245 * @op: operation being checked
246 * @dir: directory of the dentry (NOT NULL)
247 * @dentry: dentry to check (NOT NULL)
248 * @mask: requested permissions mask
249 * @cond: conditional info for the permission request (NOT NULL)
251 * Returns: %0 else error code if error or permission denied
253 static int common_perm_dir_dentry(const char *op, const struct path *dir,
254 struct dentry *dentry, u32 mask,
255 struct path_cond *cond)
257 struct path path = { .mnt = dir->mnt, .dentry = dentry };
259 return common_perm(op, &path, mask, cond);
263 * common_perm_rm - common permission wrapper for operations doing rm
264 * @op: operation being checked
265 * @dir: directory that the dentry is in (NOT NULL)
266 * @dentry: dentry being rm'd (NOT NULL)
267 * @mask: requested permission mask
269 * Returns: %0 else error code if error or permission denied
271 static int common_perm_rm(const char *op, const struct path *dir,
272 struct dentry *dentry, u32 mask)
274 struct inode *inode = d_backing_inode(dentry);
275 struct path_cond cond = { };
278 if (!inode || !path_mediated_fs(dentry))
281 vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
282 cond.uid = vfsuid_into_kuid(vfsuid);
283 cond.mode = inode->i_mode;
285 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
289 * common_perm_create - common permission wrapper for operations doing create
290 * @op: operation being checked
291 * @dir: directory that dentry will be created in (NOT NULL)
292 * @dentry: dentry to create (NOT NULL)
293 * @mask: request permission mask
294 * @mode: created file mode
296 * Returns: %0 else error code if error or permission denied
298 static int common_perm_create(const char *op, const struct path *dir,
299 struct dentry *dentry, u32 mask, umode_t mode)
301 struct path_cond cond = { current_fsuid(), mode };
303 if (!path_mediated_fs(dir->dentry))
306 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
309 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
311 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
314 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
317 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
321 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
323 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
326 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
327 umode_t mode, unsigned int dev)
329 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
332 static int apparmor_path_truncate(const struct path *path)
334 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
337 static int apparmor_file_truncate(struct file *file)
339 return apparmor_path_truncate(&file->f_path);
342 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
343 const char *old_name)
345 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
349 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
350 struct dentry *new_dentry)
352 struct aa_label *label;
355 if (!path_mediated_fs(old_dentry))
358 label = begin_current_label_crit_section();
359 if (!unconfined(label))
360 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
361 end_current_label_crit_section(label);
366 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
367 const struct path *new_dir, struct dentry *new_dentry,
368 const unsigned int flags)
370 struct aa_label *label;
373 if (!path_mediated_fs(old_dentry))
375 if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
378 label = begin_current_label_crit_section();
379 if (!unconfined(label)) {
380 struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
382 struct path old_path = { .mnt = old_dir->mnt,
383 .dentry = old_dentry };
384 struct path new_path = { .mnt = new_dir->mnt,
385 .dentry = new_dentry };
386 struct path_cond cond = {
387 .mode = d_backing_inode(old_dentry)->i_mode
389 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
390 cond.uid = vfsuid_into_kuid(vfsuid);
392 if (flags & RENAME_EXCHANGE) {
393 struct path_cond cond_exchange = {
394 .mode = d_backing_inode(new_dentry)->i_mode,
396 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
397 cond_exchange.uid = vfsuid_into_kuid(vfsuid);
399 error = aa_path_perm(OP_RENAME_SRC, label, &new_path, 0,
400 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
401 AA_MAY_SETATTR | AA_MAY_DELETE,
404 error = aa_path_perm(OP_RENAME_DEST, label, &old_path,
405 0, MAY_WRITE | AA_MAY_SETATTR |
406 AA_MAY_CREATE, &cond_exchange);
410 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
411 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
412 AA_MAY_SETATTR | AA_MAY_DELETE,
415 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
416 0, MAY_WRITE | AA_MAY_SETATTR |
417 AA_MAY_CREATE, &cond);
420 end_current_label_crit_section(label);
425 static int apparmor_path_chmod(const struct path *path, umode_t mode)
427 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
430 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
432 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
435 static int apparmor_inode_getattr(const struct path *path)
437 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
440 static int apparmor_file_open(struct file *file)
442 struct aa_file_ctx *fctx = file_ctx(file);
443 struct aa_label *label;
446 if (!path_mediated_fs(file->f_path.dentry))
449 /* If in exec, permission is handled by bprm hooks.
450 * Cache permissions granted by the previous exec check, with
451 * implicit read and executable mmap which are required to
452 * actually execute the image.
454 if (current->in_execve) {
455 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
459 label = aa_get_newest_cred_label(file->f_cred);
460 if (!unconfined(label)) {
461 struct mnt_idmap *idmap = file_mnt_idmap(file);
462 struct inode *inode = file_inode(file);
464 struct path_cond cond = {
465 .mode = inode->i_mode,
467 vfsuid = i_uid_into_vfsuid(idmap, inode);
468 cond.uid = vfsuid_into_kuid(vfsuid);
470 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
471 aa_map_file_to_perms(file), &cond);
472 /* todo cache full allowed permissions set and state */
473 fctx->allow = aa_map_file_to_perms(file);
480 static int apparmor_file_alloc_security(struct file *file)
482 struct aa_file_ctx *ctx = file_ctx(file);
483 struct aa_label *label = begin_current_label_crit_section();
485 spin_lock_init(&ctx->lock);
486 rcu_assign_pointer(ctx->label, aa_get_label(label));
487 end_current_label_crit_section(label);
491 static void apparmor_file_free_security(struct file *file)
493 struct aa_file_ctx *ctx = file_ctx(file);
496 aa_put_label(rcu_access_pointer(ctx->label));
499 static int common_file_perm(const char *op, struct file *file, u32 mask,
502 struct aa_label *label;
505 /* don't reaudit files closed during inheritance */
506 if (file->f_path.dentry == aa_null.dentry)
509 label = __begin_current_label_crit_section();
510 error = aa_file_perm(op, label, file, mask, in_atomic);
511 __end_current_label_crit_section(label);
516 static int apparmor_file_receive(struct file *file)
518 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
522 static int apparmor_file_permission(struct file *file, int mask)
524 return common_file_perm(OP_FPERM, file, mask, false);
527 static int apparmor_file_lock(struct file *file, unsigned int cmd)
529 u32 mask = AA_MAY_LOCK;
534 return common_file_perm(OP_FLOCK, file, mask, false);
537 static int common_mmap(const char *op, struct file *file, unsigned long prot,
538 unsigned long flags, bool in_atomic)
542 if (!file || !file_ctx(file))
545 if (prot & PROT_READ)
548 * Private mappings don't require write perms since they don't
549 * write back to the files
551 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
553 if (prot & PROT_EXEC)
554 mask |= AA_EXEC_MMAP;
556 return common_file_perm(op, file, mask, in_atomic);
559 static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
560 unsigned long prot, unsigned long flags)
562 return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
565 static int apparmor_file_mprotect(struct vm_area_struct *vma,
566 unsigned long reqprot, unsigned long prot)
568 return common_mmap(OP_FMPROT, vma->vm_file, prot,
569 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
573 static int apparmor_sb_mount(const char *dev_name, const struct path *path,
574 const char *type, unsigned long flags, void *data)
576 struct aa_label *label;
580 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
581 flags &= ~MS_MGC_MSK;
583 flags &= ~AA_MS_IGNORE_MASK;
585 label = __begin_current_label_crit_section();
586 if (!unconfined(label)) {
587 if (flags & MS_REMOUNT)
588 error = aa_remount(label, path, flags, data);
589 else if (flags & MS_BIND)
590 error = aa_bind_mount(label, path, dev_name, flags);
591 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
593 error = aa_mount_change_type(label, path, flags);
594 else if (flags & MS_MOVE)
595 error = aa_move_mount(label, path, dev_name);
597 error = aa_new_mount(label, dev_name, path, type,
600 __end_current_label_crit_section(label);
605 static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
607 struct aa_label *label;
610 label = __begin_current_label_crit_section();
611 if (!unconfined(label))
612 error = aa_umount(label, mnt, flags);
613 __end_current_label_crit_section(label);
618 static int apparmor_sb_pivotroot(const struct path *old_path,
619 const struct path *new_path)
621 struct aa_label *label;
624 label = aa_get_current_label();
625 if (!unconfined(label))
626 error = aa_pivotroot(label, old_path, new_path);
632 static int apparmor_getprocattr(struct task_struct *task, const char *name,
637 const struct cred *cred = get_task_cred(task);
638 struct aa_task_ctx *ctx = task_ctx(current);
639 struct aa_label *label = NULL;
641 if (strcmp(name, "current") == 0)
642 label = aa_get_newest_label(cred_label(cred));
643 else if (strcmp(name, "prev") == 0 && ctx->previous)
644 label = aa_get_newest_label(ctx->previous);
645 else if (strcmp(name, "exec") == 0 && ctx->onexec)
646 label = aa_get_newest_label(ctx->onexec);
651 error = aa_getprocattr(label, value);
659 static int apparmor_setprocattr(const char *name, void *value,
662 char *command, *largs = NULL, *args = value;
665 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
671 /* AppArmor requires that the buffer must be null terminated atm */
672 if (args[size - 1] != '\0') {
674 largs = args = kmalloc(size + 1, GFP_KERNEL);
677 memcpy(args, value, size);
683 command = strsep(&args, " ");
686 args = skip_spaces(args);
690 arg_size = size - (args - (largs ? largs : (char *) value));
691 if (strcmp(name, "current") == 0) {
692 if (strcmp(command, "changehat") == 0) {
693 error = aa_setprocattr_changehat(args, arg_size,
695 } else if (strcmp(command, "permhat") == 0) {
696 error = aa_setprocattr_changehat(args, arg_size,
698 } else if (strcmp(command, "changeprofile") == 0) {
699 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
700 } else if (strcmp(command, "permprofile") == 0) {
701 error = aa_change_profile(args, AA_CHANGE_TEST);
702 } else if (strcmp(command, "stack") == 0) {
703 error = aa_change_profile(args, AA_CHANGE_STACK);
706 } else if (strcmp(name, "exec") == 0) {
707 if (strcmp(command, "exec") == 0)
708 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
709 else if (strcmp(command, "stack") == 0)
710 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
715 /* only support the "current" and "exec" process attributes */
725 aad(&sa)->label = begin_current_label_crit_section();
726 aad(&sa)->info = name;
727 aad(&sa)->error = error = -EINVAL;
728 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
729 end_current_label_crit_section(aad(&sa)->label);
734 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
735 * @bprm: binprm for the exec (NOT NULL)
737 static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
739 struct aa_label *label = aa_current_raw_label();
740 struct aa_label *new_label = cred_label(bprm->cred);
742 /* bail out if unconfined or not changing profile */
743 if ((new_label->proxy == label->proxy) ||
744 (unconfined(new_label)))
747 aa_inherit_files(bprm->cred, current->files);
749 current->pdeath_signal = 0;
751 /* reset soft limits and set hard limits for the new label */
752 __aa_transition_rlimits(label, new_label);
756 * apparmor_bprm_committed_creds() - do cleanup after new creds committed
757 * @bprm: binprm for the exec (NOT NULL)
759 static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
761 /* clear out temporary/transitional state from the context */
762 aa_clear_task_ctx_trans(task_ctx(current));
767 static void apparmor_current_getsecid_subj(u32 *secid)
769 struct aa_label *label = aa_get_current_label();
770 *secid = label->secid;
774 static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
776 struct aa_label *label = aa_get_task_label(p);
777 *secid = label->secid;
781 static int apparmor_task_setrlimit(struct task_struct *task,
782 unsigned int resource, struct rlimit *new_rlim)
784 struct aa_label *label = __begin_current_label_crit_section();
787 if (!unconfined(label))
788 error = aa_task_setrlimit(label, task, resource, new_rlim);
789 __end_current_label_crit_section(label);
794 static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
795 int sig, const struct cred *cred)
797 struct aa_label *cl, *tl;
802 * Dealing with USB IO specific behavior
804 cl = aa_get_newest_cred_label(cred);
805 tl = aa_get_task_label(target);
806 error = aa_may_signal(cl, tl, sig);
812 cl = __begin_current_label_crit_section();
813 tl = aa_get_task_label(target);
814 error = aa_may_signal(cl, tl, sig);
816 __end_current_label_crit_section(cl);
822 * apparmor_sk_alloc_security - allocate and attach the sk_security field
824 static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
826 struct aa_sk_ctx *ctx;
828 ctx = kzalloc(sizeof(*ctx), flags);
838 * apparmor_sk_free_security - free the sk_security field
840 static void apparmor_sk_free_security(struct sock *sk)
842 struct aa_sk_ctx *ctx = SK_CTX(sk);
845 aa_put_label(ctx->label);
846 aa_put_label(ctx->peer);
851 * apparmor_sk_clone_security - clone the sk_security field
853 static void apparmor_sk_clone_security(const struct sock *sk,
856 struct aa_sk_ctx *ctx = SK_CTX(sk);
857 struct aa_sk_ctx *new = SK_CTX(newsk);
860 aa_put_label(new->label);
861 new->label = aa_get_label(ctx->label);
864 aa_put_label(new->peer);
865 new->peer = aa_get_label(ctx->peer);
869 * apparmor_socket_create - check perms before creating a new socket
871 static int apparmor_socket_create(int family, int type, int protocol, int kern)
873 struct aa_label *label;
876 AA_BUG(in_interrupt());
878 label = begin_current_label_crit_section();
879 if (!(kern || unconfined(label)))
880 error = af_select(family,
881 create_perm(label, family, type, protocol),
882 aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
883 family, type, protocol));
884 end_current_label_crit_section(label);
890 * apparmor_socket_post_create - setup the per-socket security struct
893 * - kernel sockets currently labeled unconfined but we may want to
894 * move to a special kernel label
895 * - socket may not have sk here if created with sock_create_lite or
896 * sock_alloc. These should be accept cases which will be handled in
899 static int apparmor_socket_post_create(struct socket *sock, int family,
900 int type, int protocol, int kern)
902 struct aa_label *label;
905 label = aa_get_label(kernel_t);
907 label = aa_get_current_label();
910 struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
912 aa_put_label(ctx->label);
913 ctx->label = aa_get_label(label);
921 * apparmor_socket_bind - check perms before bind addr to socket
923 static int apparmor_socket_bind(struct socket *sock,
924 struct sockaddr *address, int addrlen)
929 AA_BUG(in_interrupt());
931 return af_select(sock->sk->sk_family,
932 bind_perm(sock, address, addrlen),
933 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
937 * apparmor_socket_connect - check perms before connecting @sock to @address
939 static int apparmor_socket_connect(struct socket *sock,
940 struct sockaddr *address, int addrlen)
945 AA_BUG(in_interrupt());
947 return af_select(sock->sk->sk_family,
948 connect_perm(sock, address, addrlen),
949 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
953 * apparmor_socket_listen - check perms before allowing listen
955 static int apparmor_socket_listen(struct socket *sock, int backlog)
959 AA_BUG(in_interrupt());
961 return af_select(sock->sk->sk_family,
962 listen_perm(sock, backlog),
963 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
967 * apparmor_socket_accept - check perms before accepting a new connection.
969 * Note: while @newsock is created and has some information, the accept
972 static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
977 AA_BUG(in_interrupt());
979 return af_select(sock->sk->sk_family,
980 accept_perm(sock, newsock),
981 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
984 static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
985 struct msghdr *msg, int size)
990 AA_BUG(in_interrupt());
992 return af_select(sock->sk->sk_family,
993 msg_perm(op, request, sock, msg, size),
994 aa_sk_perm(op, request, sock->sk));
998 * apparmor_socket_sendmsg - check perms before sending msg to another socket
1000 static int apparmor_socket_sendmsg(struct socket *sock,
1001 struct msghdr *msg, int size)
1003 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
1007 * apparmor_socket_recvmsg - check perms before receiving a message
1009 static int apparmor_socket_recvmsg(struct socket *sock,
1010 struct msghdr *msg, int size, int flags)
1012 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
1015 /* revaliation, get/set attr, shutdown */
1016 static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
1020 AA_BUG(in_interrupt());
1022 return af_select(sock->sk->sk_family,
1023 sock_perm(op, request, sock),
1024 aa_sk_perm(op, request, sock->sk));
1028 * apparmor_socket_getsockname - check perms before getting the local address
1030 static int apparmor_socket_getsockname(struct socket *sock)
1032 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
1036 * apparmor_socket_getpeername - check perms before getting remote address
1038 static int apparmor_socket_getpeername(struct socket *sock)
1040 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
1043 /* revaliation, get/set attr, opt */
1044 static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
1045 int level, int optname)
1049 AA_BUG(in_interrupt());
1051 return af_select(sock->sk->sk_family,
1052 opt_perm(op, request, sock, level, optname),
1053 aa_sk_perm(op, request, sock->sk));
1057 * apparmor_socket_getsockopt - check perms before getting socket options
1059 static int apparmor_socket_getsockopt(struct socket *sock, int level,
1062 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1067 * apparmor_socket_setsockopt - check perms before setting socket options
1069 static int apparmor_socket_setsockopt(struct socket *sock, int level,
1072 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1077 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1079 static int apparmor_socket_shutdown(struct socket *sock, int how)
1081 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1084 #ifdef CONFIG_NETWORK_SECMARK
1086 * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
1088 * Note: can not sleep may be called with locks held
1090 * dont want protocol specific in __skb_recv_datagram()
1091 * to deny an incoming connection socket_sock_rcv_skb()
1093 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1095 struct aa_sk_ctx *ctx = SK_CTX(sk);
1100 return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1106 static struct aa_label *sk_peer_label(struct sock *sk)
1108 struct aa_sk_ctx *ctx = SK_CTX(sk);
1113 return ERR_PTR(-ENOPROTOOPT);
1117 * apparmor_socket_getpeersec_stream - get security context of peer
1119 * Note: for tcp only valid if using ipsec or cipso on lan
1121 static int apparmor_socket_getpeersec_stream(struct socket *sock,
1122 sockptr_t optval, sockptr_t optlen,
1126 int slen, error = 0;
1127 struct aa_label *label;
1128 struct aa_label *peer;
1130 label = begin_current_label_crit_section();
1131 peer = sk_peer_label(sock->sk);
1133 error = PTR_ERR(peer);
1136 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1137 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1138 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1139 /* don't include terminating \0 in slen, it breaks some apps */
1149 if (copy_to_sockptr(optval, name, slen))
1152 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
1155 end_current_label_crit_section(label);
1161 * apparmor_socket_getpeersec_dgram - get security label of packet
1162 * @sock: the peer socket
1164 * @secid: pointer to where to put the secid of the packet
1166 * Sets the netlabel socket state on sk from parent
1168 static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1169 struct sk_buff *skb, u32 *secid)
1172 /* TODO: requires secid support */
1173 return -ENOPROTOOPT;
1177 * apparmor_sock_graft - Initialize newly created socket
1179 * @parent: parent socket
1181 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1182 * just set sk security information off of current creating process label
1183 * Labeling of sk for accept case - probably should be sock based
1184 * instead of task, because of the case where an implicitly labeled
1185 * socket is shared by different tasks.
1187 static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1189 struct aa_sk_ctx *ctx = SK_CTX(sk);
1192 ctx->label = aa_get_current_label();
1195 #ifdef CONFIG_NETWORK_SECMARK
1196 static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1197 struct request_sock *req)
1199 struct aa_sk_ctx *ctx = SK_CTX(sk);
1204 return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1210 * The cred blob is a pointer to, not an instance of, an aa_label.
1212 struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
1213 .lbs_cred = sizeof(struct aa_label *),
1214 .lbs_file = sizeof(struct aa_file_ctx),
1215 .lbs_task = sizeof(struct aa_task_ctx),
1218 static struct security_hook_list apparmor_hooks[] __ro_after_init = {
1219 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1220 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1221 LSM_HOOK_INIT(capget, apparmor_capget),
1222 LSM_HOOK_INIT(capable, apparmor_capable),
1224 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1225 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1226 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1228 LSM_HOOK_INIT(path_link, apparmor_path_link),
1229 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1230 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1231 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1232 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1233 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1234 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1235 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1236 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1237 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1238 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1240 LSM_HOOK_INIT(file_open, apparmor_file_open),
1241 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1242 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1243 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1244 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1245 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1246 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1247 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
1248 LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
1250 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1251 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1253 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1254 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1255 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1257 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1258 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1259 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1260 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1261 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1262 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1263 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1264 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1265 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1266 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1267 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1268 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1269 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1270 #ifdef CONFIG_NETWORK_SECMARK
1271 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1273 LSM_HOOK_INIT(socket_getpeersec_stream,
1274 apparmor_socket_getpeersec_stream),
1275 LSM_HOOK_INIT(socket_getpeersec_dgram,
1276 apparmor_socket_getpeersec_dgram),
1277 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1278 #ifdef CONFIG_NETWORK_SECMARK
1279 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1282 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1283 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1284 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1285 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1287 LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1288 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1289 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1291 LSM_HOOK_INIT(task_free, apparmor_task_free),
1292 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
1293 LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj),
1294 LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
1295 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1296 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1299 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1300 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1301 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1302 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1305 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1306 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1307 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1311 * AppArmor sysfs module parameters
1314 static int param_set_aabool(const char *val, const struct kernel_param *kp);
1315 static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1316 #define param_check_aabool param_check_bool
1317 static const struct kernel_param_ops param_ops_aabool = {
1318 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1319 .set = param_set_aabool,
1320 .get = param_get_aabool
1323 static int param_set_aauint(const char *val, const struct kernel_param *kp);
1324 static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1325 #define param_check_aauint param_check_uint
1326 static const struct kernel_param_ops param_ops_aauint = {
1327 .set = param_set_aauint,
1328 .get = param_get_aauint
1331 static int param_set_aacompressionlevel(const char *val,
1332 const struct kernel_param *kp);
1333 static int param_get_aacompressionlevel(char *buffer,
1334 const struct kernel_param *kp);
1335 #define param_check_aacompressionlevel param_check_int
1336 static const struct kernel_param_ops param_ops_aacompressionlevel = {
1337 .set = param_set_aacompressionlevel,
1338 .get = param_get_aacompressionlevel
1341 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1342 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1343 #define param_check_aalockpolicy param_check_bool
1344 static const struct kernel_param_ops param_ops_aalockpolicy = {
1345 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1346 .set = param_set_aalockpolicy,
1347 .get = param_get_aalockpolicy
1350 static int param_set_audit(const char *val, const struct kernel_param *kp);
1351 static int param_get_audit(char *buffer, const struct kernel_param *kp);
1353 static int param_set_mode(const char *val, const struct kernel_param *kp);
1354 static int param_get_mode(char *buffer, const struct kernel_param *kp);
1356 /* Flag values, also controllable via /sys/module/apparmor/parameters
1357 * We define special types as we want to do additional mediation.
1360 /* AppArmor global enforcement switch - complain, enforce, kill */
1361 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1362 module_param_call(mode, param_set_mode, param_get_mode,
1363 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1365 /* whether policy verification hashing is enabled */
1366 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
1367 #ifdef CONFIG_SECURITY_APPARMOR_HASH
1368 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1371 /* whether policy exactly as loaded is retained for debug and checkpointing */
1372 bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1373 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1374 module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1377 /* policy loaddata compression level */
1378 int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
1379 module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
1380 aacompressionlevel, 0400);
1383 bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
1384 module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1387 enum audit_mode aa_g_audit;
1388 module_param_call(audit, param_set_audit, param_get_audit,
1389 &aa_g_audit, S_IRUSR | S_IWUSR);
1391 /* Determines if audit header is included in audited messages. This
1392 * provides more context if the audit daemon is not running
1394 bool aa_g_audit_header = true;
1395 module_param_named(audit_header, aa_g_audit_header, aabool,
1398 /* lock out loading/removal of policy
1399 * TODO: add in at boot loading of policy, which is the only way to
1400 * load policy, if lock_policy is set
1402 bool aa_g_lock_policy;
1403 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1406 /* Syscall logging mode */
1407 bool aa_g_logsyscall;
1408 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1410 /* Maximum pathname length before accesses will start getting rejected */
1411 unsigned int aa_g_path_max = 2 * PATH_MAX;
1412 module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1414 /* Determines how paranoid loading of policy is and how much verification
1415 * on the loaded policy is done.
1416 * DEPRECATED: read only as strict checking of load is always done now
1417 * that none root users (user namespaces) can load policy.
1419 bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1420 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1422 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1423 static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1424 #define param_check_aaintbool param_check_int
1425 static const struct kernel_param_ops param_ops_aaintbool = {
1426 .set = param_set_aaintbool,
1427 .get = param_get_aaintbool
1429 /* Boot time disable flag */
1430 static int apparmor_enabled __ro_after_init = 1;
1431 module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1433 static int __init apparmor_enabled_setup(char *str)
1435 unsigned long enabled;
1436 int error = kstrtoul(str, 0, &enabled);
1438 apparmor_enabled = enabled ? 1 : 0;
1442 __setup("apparmor=", apparmor_enabled_setup);
1444 /* set global flag turning off the ability to load policy */
1445 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1447 if (!apparmor_enabled)
1449 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1451 return param_set_bool(val, kp);
1454 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1456 if (!apparmor_enabled)
1458 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1460 return param_get_bool(buffer, kp);
1463 static int param_set_aabool(const char *val, const struct kernel_param *kp)
1465 if (!apparmor_enabled)
1467 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1469 return param_set_bool(val, kp);
1472 static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1474 if (!apparmor_enabled)
1476 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1478 return param_get_bool(buffer, kp);
1481 static int param_set_aauint(const char *val, const struct kernel_param *kp)
1485 if (!apparmor_enabled)
1487 /* file is ro but enforce 2nd line check */
1488 if (apparmor_initialized)
1491 error = param_set_uint(val, kp);
1492 aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
1493 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1498 static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1500 if (!apparmor_enabled)
1502 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1504 return param_get_uint(buffer, kp);
1507 /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
1508 static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1510 struct kernel_param kp_local;
1514 if (apparmor_initialized)
1517 /* Create local copy, with arg pointing to bool type. */
1518 value = !!*((int *)kp->arg);
1519 memcpy(&kp_local, kp, sizeof(kp_local));
1520 kp_local.arg = &value;
1522 error = param_set_bool(val, &kp_local);
1524 *((int *)kp->arg) = *((bool *)kp_local.arg);
1529 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1530 * 1/0, this converts the "int that is actually bool" back to bool for
1531 * display in the /sys filesystem, while keeping it "int" for the LSM
1534 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1536 struct kernel_param kp_local;
1539 /* Create local copy, with arg pointing to bool type. */
1540 value = !!*((int *)kp->arg);
1541 memcpy(&kp_local, kp, sizeof(kp_local));
1542 kp_local.arg = &value;
1544 return param_get_bool(buffer, &kp_local);
1547 static int param_set_aacompressionlevel(const char *val,
1548 const struct kernel_param *kp)
1552 if (!apparmor_enabled)
1554 if (apparmor_initialized)
1557 error = param_set_int(val, kp);
1559 aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
1560 AA_MIN_CLEVEL, AA_MAX_CLEVEL);
1561 pr_info("AppArmor: policy rawdata compression level set to %d\n",
1562 aa_g_rawdata_compression_level);
1567 static int param_get_aacompressionlevel(char *buffer,
1568 const struct kernel_param *kp)
1570 if (!apparmor_enabled)
1572 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1574 return param_get_int(buffer, kp);
1577 static int param_get_audit(char *buffer, const struct kernel_param *kp)
1579 if (!apparmor_enabled)
1581 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1583 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1586 static int param_set_audit(const char *val, const struct kernel_param *kp)
1590 if (!apparmor_enabled)
1594 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1597 i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
1605 static int param_get_mode(char *buffer, const struct kernel_param *kp)
1607 if (!apparmor_enabled)
1609 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1612 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1615 static int param_set_mode(const char *val, const struct kernel_param *kp)
1619 if (!apparmor_enabled)
1623 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1626 i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
1631 aa_g_profile_mode = i;
1635 char *aa_get_buffer(bool in_atomic)
1637 union aa_buffer *aa_buf;
1638 bool try_again = true;
1639 gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1642 spin_lock(&aa_buffers_lock);
1643 if (buffer_count > reserve_count ||
1644 (in_atomic && !list_empty(&aa_global_buffers))) {
1645 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1647 list_del(&aa_buf->list);
1649 spin_unlock(&aa_buffers_lock);
1650 return aa_buf->buffer;
1654 * out of reserve buffers and in atomic context so increase
1655 * how many buffers to keep in reserve
1660 spin_unlock(&aa_buffers_lock);
1664 aa_buf = kmalloc(aa_g_path_max, flags);
1670 pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
1673 return aa_buf->buffer;
1676 void aa_put_buffer(char *buf)
1678 union aa_buffer *aa_buf;
1682 aa_buf = container_of(buf, union aa_buffer, buffer[0]);
1684 spin_lock(&aa_buffers_lock);
1685 list_add(&aa_buf->list, &aa_global_buffers);
1687 spin_unlock(&aa_buffers_lock);
1691 * AppArmor init functions
1695 * set_init_ctx - set a task context and profile on the first task.
1697 * TODO: allow setting an alternate profile than unconfined
1699 static int __init set_init_ctx(void)
1701 struct cred *cred = (__force struct cred *)current->real_cred;
1703 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
1708 static void destroy_buffers(void)
1710 union aa_buffer *aa_buf;
1712 spin_lock(&aa_buffers_lock);
1713 while (!list_empty(&aa_global_buffers)) {
1714 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1716 list_del(&aa_buf->list);
1717 spin_unlock(&aa_buffers_lock);
1719 spin_lock(&aa_buffers_lock);
1721 spin_unlock(&aa_buffers_lock);
1724 static int __init alloc_buffers(void)
1726 union aa_buffer *aa_buf;
1730 * A function may require two buffers at once. Usually the buffers are
1731 * used for a short period of time and are shared. On UP kernel buffers
1732 * two should be enough, with more CPUs it is possible that more
1733 * buffers will be used simultaneously. The preallocated pool may grow.
1734 * This preallocation has also the side-effect that AppArmor will be
1735 * disabled early at boot if aa_g_path_max is extremly high.
1737 if (num_online_cpus() > 1)
1738 num = 4 + RESERVE_COUNT;
1740 num = 2 + RESERVE_COUNT;
1742 for (i = 0; i < num; i++) {
1744 aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
1745 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1750 aa_put_buffer(aa_buf->buffer);
1755 #ifdef CONFIG_SYSCTL
1756 static int apparmor_dointvec(struct ctl_table *table, int write,
1757 void *buffer, size_t *lenp, loff_t *ppos)
1759 if (!aa_current_policy_admin_capable(NULL))
1761 if (!apparmor_enabled)
1764 return proc_dointvec(table, write, buffer, lenp, ppos);
1767 static struct ctl_table apparmor_sysctl_table[] = {
1769 .procname = "unprivileged_userns_apparmor_policy",
1770 .data = &unprivileged_userns_apparmor_policy,
1771 .maxlen = sizeof(int),
1773 .proc_handler = apparmor_dointvec,
1776 .procname = "apparmor_display_secid_mode",
1777 .data = &apparmor_display_secid_mode,
1778 .maxlen = sizeof(int),
1780 .proc_handler = apparmor_dointvec,
1786 static int __init apparmor_init_sysctl(void)
1788 return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
1791 static inline int apparmor_init_sysctl(void)
1795 #endif /* CONFIG_SYSCTL */
1797 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
1798 static unsigned int apparmor_ip_postroute(void *priv,
1799 struct sk_buff *skb,
1800 const struct nf_hook_state *state)
1802 struct aa_sk_ctx *ctx;
1808 sk = skb_to_full_sk(skb);
1813 if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
1817 return NF_DROP_ERR(-ECONNREFUSED);
1821 static const struct nf_hook_ops apparmor_nf_ops[] = {
1823 .hook = apparmor_ip_postroute,
1825 .hooknum = NF_INET_POST_ROUTING,
1826 .priority = NF_IP_PRI_SELINUX_FIRST,
1828 #if IS_ENABLED(CONFIG_IPV6)
1830 .hook = apparmor_ip_postroute,
1832 .hooknum = NF_INET_POST_ROUTING,
1833 .priority = NF_IP6_PRI_SELINUX_FIRST,
1838 static int __net_init apparmor_nf_register(struct net *net)
1840 return nf_register_net_hooks(net, apparmor_nf_ops,
1841 ARRAY_SIZE(apparmor_nf_ops));
1844 static void __net_exit apparmor_nf_unregister(struct net *net)
1846 nf_unregister_net_hooks(net, apparmor_nf_ops,
1847 ARRAY_SIZE(apparmor_nf_ops));
1850 static struct pernet_operations apparmor_net_ops = {
1851 .init = apparmor_nf_register,
1852 .exit = apparmor_nf_unregister,
1855 static int __init apparmor_nf_ip_init(void)
1859 if (!apparmor_enabled)
1862 err = register_pernet_subsys(&apparmor_net_ops);
1864 panic("Apparmor: register_pernet_subsys: error %d\n", err);
1868 __initcall(apparmor_nf_ip_init);
1871 static int __init apparmor_init(void)
1875 error = aa_setup_dfa_engine();
1877 AA_ERROR("Unable to setup dfa engine\n");
1881 error = aa_alloc_root_ns();
1883 AA_ERROR("Unable to allocate default profile namespace\n");
1887 error = apparmor_init_sysctl();
1889 AA_ERROR("Unable to register sysctls\n");
1894 error = alloc_buffers();
1896 AA_ERROR("Unable to allocate work buffers\n");
1900 error = set_init_ctx();
1902 AA_ERROR("Failed to set context on init task\n");
1906 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1909 /* Report that AppArmor successfully initialized */
1910 apparmor_initialized = 1;
1911 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1912 aa_info_message("AppArmor initialized: complain mode enabled");
1913 else if (aa_g_profile_mode == APPARMOR_KILL)
1914 aa_info_message("AppArmor initialized: kill mode enabled");
1916 aa_info_message("AppArmor initialized");
1924 aa_teardown_dfa_engine();
1926 apparmor_enabled = false;
1930 DEFINE_LSM(apparmor) = {
1932 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
1933 .enabled = &apparmor_enabled,
1934 .blobs = &apparmor_blob_sizes,
1935 .init = apparmor_init,