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;
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(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 struct user_namespace *mnt_userns = mnt_user_ns(path->mnt);
231 struct path_cond cond = {
232 i_uid_into_mnt(mnt_userns, d_backing_inode(path->dentry)),
233 d_backing_inode(path->dentry)->i_mode
236 if (!path_mediated_fs(path->dentry))
239 return common_perm(op, path, mask, &cond);
243 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
244 * @op: operation being checked
245 * @dir: directory of the dentry (NOT NULL)
246 * @dentry: dentry to check (NOT NULL)
247 * @mask: requested permissions mask
248 * @cond: conditional info for the permission request (NOT NULL)
250 * Returns: %0 else error code if error or permission denied
252 static int common_perm_dir_dentry(const char *op, const struct path *dir,
253 struct dentry *dentry, u32 mask,
254 struct path_cond *cond)
256 struct path path = { .mnt = dir->mnt, .dentry = dentry };
258 return common_perm(op, &path, mask, cond);
262 * common_perm_rm - common permission wrapper for operations doing rm
263 * @op: operation being checked
264 * @dir: directory that the dentry is in (NOT NULL)
265 * @dentry: dentry being rm'd (NOT NULL)
266 * @mask: requested permission mask
268 * Returns: %0 else error code if error or permission denied
270 static int common_perm_rm(const char *op, const struct path *dir,
271 struct dentry *dentry, u32 mask)
273 struct inode *inode = d_backing_inode(dentry);
274 struct user_namespace *mnt_userns = mnt_user_ns(dir->mnt);
275 struct path_cond cond = { };
277 if (!inode || !path_mediated_fs(dentry))
280 cond.uid = i_uid_into_mnt(mnt_userns, inode);
281 cond.mode = inode->i_mode;
283 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
287 * common_perm_create - common permission wrapper for operations doing create
288 * @op: operation being checked
289 * @dir: directory that dentry will be created in (NOT NULL)
290 * @dentry: dentry to create (NOT NULL)
291 * @mask: request permission mask
292 * @mode: created file mode
294 * Returns: %0 else error code if error or permission denied
296 static int common_perm_create(const char *op, const struct path *dir,
297 struct dentry *dentry, u32 mask, umode_t mode)
299 struct path_cond cond = { current_fsuid(), mode };
301 if (!path_mediated_fs(dir->dentry))
304 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
307 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
309 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
312 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
315 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
319 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
321 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
324 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
325 umode_t mode, unsigned int dev)
327 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
330 static int apparmor_path_truncate(const struct path *path)
332 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
335 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
336 const char *old_name)
338 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
342 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
343 struct dentry *new_dentry)
345 struct aa_label *label;
348 if (!path_mediated_fs(old_dentry))
351 label = begin_current_label_crit_section();
352 if (!unconfined(label))
353 error = aa_path_link(label, old_dentry, new_dir, new_dentry);
354 end_current_label_crit_section(label);
359 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
360 const struct path *new_dir, struct dentry *new_dentry,
361 const unsigned int flags)
363 struct aa_label *label;
366 if (!path_mediated_fs(old_dentry))
368 if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
371 label = begin_current_label_crit_section();
372 if (!unconfined(label)) {
373 struct user_namespace *mnt_userns = mnt_user_ns(old_dir->mnt);
374 struct path old_path = { .mnt = old_dir->mnt,
375 .dentry = old_dentry };
376 struct path new_path = { .mnt = new_dir->mnt,
377 .dentry = new_dentry };
378 struct path_cond cond = {
379 i_uid_into_mnt(mnt_userns, d_backing_inode(old_dentry)),
380 d_backing_inode(old_dentry)->i_mode
383 if (flags & RENAME_EXCHANGE) {
384 struct path_cond cond_exchange = {
385 i_uid_into_mnt(mnt_userns, d_backing_inode(new_dentry)),
386 d_backing_inode(new_dentry)->i_mode
389 error = aa_path_perm(OP_RENAME_SRC, label, &new_path, 0,
390 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
391 AA_MAY_SETATTR | AA_MAY_DELETE,
394 error = aa_path_perm(OP_RENAME_DEST, label, &old_path,
395 0, MAY_WRITE | AA_MAY_SETATTR |
396 AA_MAY_CREATE, &cond_exchange);
400 error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0,
401 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
402 AA_MAY_SETATTR | AA_MAY_DELETE,
405 error = aa_path_perm(OP_RENAME_DEST, label, &new_path,
406 0, MAY_WRITE | AA_MAY_SETATTR |
407 AA_MAY_CREATE, &cond);
410 end_current_label_crit_section(label);
415 static int apparmor_path_chmod(const struct path *path, umode_t mode)
417 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
420 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
422 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
425 static int apparmor_inode_getattr(const struct path *path)
427 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
430 static int apparmor_file_open(struct file *file)
432 struct aa_file_ctx *fctx = file_ctx(file);
433 struct aa_label *label;
436 if (!path_mediated_fs(file->f_path.dentry))
439 /* If in exec, permission is handled by bprm hooks.
440 * Cache permissions granted by the previous exec check, with
441 * implicit read and executable mmap which are required to
442 * actually execute the image.
444 if (current->in_execve) {
445 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
449 label = aa_get_newest_cred_label(file->f_cred);
450 if (!unconfined(label)) {
451 struct user_namespace *mnt_userns = file_mnt_user_ns(file);
452 struct inode *inode = file_inode(file);
453 struct path_cond cond = {
454 i_uid_into_mnt(mnt_userns, inode),
458 error = aa_path_perm(OP_OPEN, label, &file->f_path, 0,
459 aa_map_file_to_perms(file), &cond);
460 /* todo cache full allowed permissions set and state */
461 fctx->allow = aa_map_file_to_perms(file);
468 static int apparmor_file_alloc_security(struct file *file)
470 struct aa_file_ctx *ctx = file_ctx(file);
471 struct aa_label *label = begin_current_label_crit_section();
473 spin_lock_init(&ctx->lock);
474 rcu_assign_pointer(ctx->label, aa_get_label(label));
475 end_current_label_crit_section(label);
479 static void apparmor_file_free_security(struct file *file)
481 struct aa_file_ctx *ctx = file_ctx(file);
484 aa_put_label(rcu_access_pointer(ctx->label));
487 static int common_file_perm(const char *op, struct file *file, u32 mask,
490 struct aa_label *label;
493 /* don't reaudit files closed during inheritance */
494 if (file->f_path.dentry == aa_null.dentry)
497 label = __begin_current_label_crit_section();
498 error = aa_file_perm(op, label, file, mask, in_atomic);
499 __end_current_label_crit_section(label);
504 static int apparmor_file_receive(struct file *file)
506 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
510 static int apparmor_file_permission(struct file *file, int mask)
512 return common_file_perm(OP_FPERM, file, mask, false);
515 static int apparmor_file_lock(struct file *file, unsigned int cmd)
517 u32 mask = AA_MAY_LOCK;
522 return common_file_perm(OP_FLOCK, file, mask, false);
525 static int common_mmap(const char *op, struct file *file, unsigned long prot,
526 unsigned long flags, bool in_atomic)
530 if (!file || !file_ctx(file))
533 if (prot & PROT_READ)
536 * Private mappings don't require write perms since they don't
537 * write back to the files
539 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
541 if (prot & PROT_EXEC)
542 mask |= AA_EXEC_MMAP;
544 return common_file_perm(op, file, mask, in_atomic);
547 static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
548 unsigned long prot, unsigned long flags)
550 return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
553 static int apparmor_file_mprotect(struct vm_area_struct *vma,
554 unsigned long reqprot, unsigned long prot)
556 return common_mmap(OP_FMPROT, vma->vm_file, prot,
557 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
561 static int apparmor_sb_mount(const char *dev_name, const struct path *path,
562 const char *type, unsigned long flags, void *data)
564 struct aa_label *label;
568 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
569 flags &= ~MS_MGC_MSK;
571 flags &= ~AA_MS_IGNORE_MASK;
573 label = __begin_current_label_crit_section();
574 if (!unconfined(label)) {
575 if (flags & MS_REMOUNT)
576 error = aa_remount(label, path, flags, data);
577 else if (flags & MS_BIND)
578 error = aa_bind_mount(label, path, dev_name, flags);
579 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
581 error = aa_mount_change_type(label, path, flags);
582 else if (flags & MS_MOVE)
583 error = aa_move_mount(label, path, dev_name);
585 error = aa_new_mount(label, dev_name, path, type,
588 __end_current_label_crit_section(label);
593 static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
595 struct aa_label *label;
598 label = __begin_current_label_crit_section();
599 if (!unconfined(label))
600 error = aa_umount(label, mnt, flags);
601 __end_current_label_crit_section(label);
606 static int apparmor_sb_pivotroot(const struct path *old_path,
607 const struct path *new_path)
609 struct aa_label *label;
612 label = aa_get_current_label();
613 if (!unconfined(label))
614 error = aa_pivotroot(label, old_path, new_path);
620 static int apparmor_getprocattr(struct task_struct *task, char *name,
625 const struct cred *cred = get_task_cred(task);
626 struct aa_task_ctx *ctx = task_ctx(current);
627 struct aa_label *label = NULL;
629 if (strcmp(name, "current") == 0)
630 label = aa_get_newest_label(cred_label(cred));
631 else if (strcmp(name, "prev") == 0 && ctx->previous)
632 label = aa_get_newest_label(ctx->previous);
633 else if (strcmp(name, "exec") == 0 && ctx->onexec)
634 label = aa_get_newest_label(ctx->onexec);
639 error = aa_getprocattr(label, value);
647 static int apparmor_setprocattr(const char *name, void *value,
650 char *command, *largs = NULL, *args = value;
653 DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
659 /* AppArmor requires that the buffer must be null terminated atm */
660 if (args[size - 1] != '\0') {
662 largs = args = kmalloc(size + 1, GFP_KERNEL);
665 memcpy(args, value, size);
671 command = strsep(&args, " ");
674 args = skip_spaces(args);
678 arg_size = size - (args - (largs ? largs : (char *) value));
679 if (strcmp(name, "current") == 0) {
680 if (strcmp(command, "changehat") == 0) {
681 error = aa_setprocattr_changehat(args, arg_size,
683 } else if (strcmp(command, "permhat") == 0) {
684 error = aa_setprocattr_changehat(args, arg_size,
686 } else if (strcmp(command, "changeprofile") == 0) {
687 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
688 } else if (strcmp(command, "permprofile") == 0) {
689 error = aa_change_profile(args, AA_CHANGE_TEST);
690 } else if (strcmp(command, "stack") == 0) {
691 error = aa_change_profile(args, AA_CHANGE_STACK);
694 } else if (strcmp(name, "exec") == 0) {
695 if (strcmp(command, "exec") == 0)
696 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
697 else if (strcmp(command, "stack") == 0)
698 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
703 /* only support the "current" and "exec" process attributes */
713 aad(&sa)->label = begin_current_label_crit_section();
714 aad(&sa)->info = name;
715 aad(&sa)->error = error = -EINVAL;
716 aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL);
717 end_current_label_crit_section(aad(&sa)->label);
722 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
723 * @bprm: binprm for the exec (NOT NULL)
725 static void apparmor_bprm_committing_creds(struct linux_binprm *bprm)
727 struct aa_label *label = aa_current_raw_label();
728 struct aa_label *new_label = cred_label(bprm->cred);
730 /* bail out if unconfined or not changing profile */
731 if ((new_label->proxy == label->proxy) ||
732 (unconfined(new_label)))
735 aa_inherit_files(bprm->cred, current->files);
737 current->pdeath_signal = 0;
739 /* reset soft limits and set hard limits for the new label */
740 __aa_transition_rlimits(label, new_label);
744 * apparmor_bprm_committed_creds() - do cleanup after new creds committed
745 * @bprm: binprm for the exec (NOT NULL)
747 static void apparmor_bprm_committed_creds(struct linux_binprm *bprm)
749 /* clear out temporary/transitional state from the context */
750 aa_clear_task_ctx_trans(task_ctx(current));
755 static void apparmor_current_getsecid_subj(u32 *secid)
757 struct aa_label *label = aa_get_current_label();
758 *secid = label->secid;
762 static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
764 struct aa_label *label = aa_get_task_label(p);
765 *secid = label->secid;
769 static int apparmor_task_setrlimit(struct task_struct *task,
770 unsigned int resource, struct rlimit *new_rlim)
772 struct aa_label *label = __begin_current_label_crit_section();
775 if (!unconfined(label))
776 error = aa_task_setrlimit(label, task, resource, new_rlim);
777 __end_current_label_crit_section(label);
782 static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
783 int sig, const struct cred *cred)
785 struct aa_label *cl, *tl;
790 * Dealing with USB IO specific behavior
792 cl = aa_get_newest_cred_label(cred);
793 tl = aa_get_task_label(target);
794 error = aa_may_signal(cl, tl, sig);
800 cl = __begin_current_label_crit_section();
801 tl = aa_get_task_label(target);
802 error = aa_may_signal(cl, tl, sig);
804 __end_current_label_crit_section(cl);
810 * apparmor_sk_alloc_security - allocate and attach the sk_security field
812 static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
814 struct aa_sk_ctx *ctx;
816 ctx = kzalloc(sizeof(*ctx), flags);
826 * apparmor_sk_free_security - free the sk_security field
828 static void apparmor_sk_free_security(struct sock *sk)
830 struct aa_sk_ctx *ctx = SK_CTX(sk);
833 aa_put_label(ctx->label);
834 aa_put_label(ctx->peer);
839 * apparmor_sk_clone_security - clone the sk_security field
841 static void apparmor_sk_clone_security(const struct sock *sk,
844 struct aa_sk_ctx *ctx = SK_CTX(sk);
845 struct aa_sk_ctx *new = SK_CTX(newsk);
848 aa_put_label(new->label);
849 new->label = aa_get_label(ctx->label);
852 aa_put_label(new->peer);
853 new->peer = aa_get_label(ctx->peer);
857 * apparmor_socket_create - check perms before creating a new socket
859 static int apparmor_socket_create(int family, int type, int protocol, int kern)
861 struct aa_label *label;
864 AA_BUG(in_interrupt());
866 label = begin_current_label_crit_section();
867 if (!(kern || unconfined(label)))
868 error = af_select(family,
869 create_perm(label, family, type, protocol),
870 aa_af_perm(label, OP_CREATE, AA_MAY_CREATE,
871 family, type, protocol));
872 end_current_label_crit_section(label);
878 * apparmor_socket_post_create - setup the per-socket security struct
881 * - kernel sockets currently labeled unconfined but we may want to
882 * move to a special kernel label
883 * - socket may not have sk here if created with sock_create_lite or
884 * sock_alloc. These should be accept cases which will be handled in
887 static int apparmor_socket_post_create(struct socket *sock, int family,
888 int type, int protocol, int kern)
890 struct aa_label *label;
893 label = aa_get_label(kernel_t);
895 label = aa_get_current_label();
898 struct aa_sk_ctx *ctx = SK_CTX(sock->sk);
900 aa_put_label(ctx->label);
901 ctx->label = aa_get_label(label);
909 * apparmor_socket_bind - check perms before bind addr to socket
911 static int apparmor_socket_bind(struct socket *sock,
912 struct sockaddr *address, int addrlen)
917 AA_BUG(in_interrupt());
919 return af_select(sock->sk->sk_family,
920 bind_perm(sock, address, addrlen),
921 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
925 * apparmor_socket_connect - check perms before connecting @sock to @address
927 static int apparmor_socket_connect(struct socket *sock,
928 struct sockaddr *address, int addrlen)
933 AA_BUG(in_interrupt());
935 return af_select(sock->sk->sk_family,
936 connect_perm(sock, address, addrlen),
937 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
941 * apparmor_socket_listen - check perms before allowing listen
943 static int apparmor_socket_listen(struct socket *sock, int backlog)
947 AA_BUG(in_interrupt());
949 return af_select(sock->sk->sk_family,
950 listen_perm(sock, backlog),
951 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
955 * apparmor_socket_accept - check perms before accepting a new connection.
957 * Note: while @newsock is created and has some information, the accept
960 static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
965 AA_BUG(in_interrupt());
967 return af_select(sock->sk->sk_family,
968 accept_perm(sock, newsock),
969 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
972 static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
973 struct msghdr *msg, int size)
978 AA_BUG(in_interrupt());
980 return af_select(sock->sk->sk_family,
981 msg_perm(op, request, sock, msg, size),
982 aa_sk_perm(op, request, sock->sk));
986 * apparmor_socket_sendmsg - check perms before sending msg to another socket
988 static int apparmor_socket_sendmsg(struct socket *sock,
989 struct msghdr *msg, int size)
991 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
995 * apparmor_socket_recvmsg - check perms before receiving a message
997 static int apparmor_socket_recvmsg(struct socket *sock,
998 struct msghdr *msg, int size, int flags)
1000 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
1003 /* revaliation, get/set attr, shutdown */
1004 static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
1008 AA_BUG(in_interrupt());
1010 return af_select(sock->sk->sk_family,
1011 sock_perm(op, request, sock),
1012 aa_sk_perm(op, request, sock->sk));
1016 * apparmor_socket_getsockname - check perms before getting the local address
1018 static int apparmor_socket_getsockname(struct socket *sock)
1020 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
1024 * apparmor_socket_getpeername - check perms before getting remote address
1026 static int apparmor_socket_getpeername(struct socket *sock)
1028 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
1031 /* revaliation, get/set attr, opt */
1032 static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
1033 int level, int optname)
1037 AA_BUG(in_interrupt());
1039 return af_select(sock->sk->sk_family,
1040 opt_perm(op, request, sock, level, optname),
1041 aa_sk_perm(op, request, sock->sk));
1045 * apparmor_socket_getsockopt - check perms before getting socket options
1047 static int apparmor_socket_getsockopt(struct socket *sock, int level,
1050 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1055 * apparmor_socket_setsockopt - check perms before setting socket options
1057 static int apparmor_socket_setsockopt(struct socket *sock, int level,
1060 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1065 * apparmor_socket_shutdown - check perms before shutting down @sock conn
1067 static int apparmor_socket_shutdown(struct socket *sock, int how)
1069 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1072 #ifdef CONFIG_NETWORK_SECMARK
1074 * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
1076 * Note: can not sleep may be called with locks held
1078 * dont want protocol specific in __skb_recv_datagram()
1079 * to deny an incoming connection socket_sock_rcv_skb()
1081 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1083 struct aa_sk_ctx *ctx = SK_CTX(sk);
1088 return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1094 static struct aa_label *sk_peer_label(struct sock *sk)
1096 struct aa_sk_ctx *ctx = SK_CTX(sk);
1101 return ERR_PTR(-ENOPROTOOPT);
1105 * apparmor_socket_getpeersec_stream - get security context of peer
1107 * Note: for tcp only valid if using ipsec or cipso on lan
1109 static int apparmor_socket_getpeersec_stream(struct socket *sock,
1110 char __user *optval,
1115 int slen, error = 0;
1116 struct aa_label *label;
1117 struct aa_label *peer;
1119 label = begin_current_label_crit_section();
1120 peer = sk_peer_label(sock->sk);
1122 error = PTR_ERR(peer);
1125 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1126 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1127 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1128 /* don't include terminating \0 in slen, it breaks some apps */
1134 } else if (copy_to_user(optval, name, slen)) {
1138 if (put_user(slen, optlen))
1146 end_current_label_crit_section(label);
1152 * apparmor_socket_getpeersec_dgram - get security label of packet
1153 * @sock: the peer socket
1155 * @secid: pointer to where to put the secid of the packet
1157 * Sets the netlabel socket state on sk from parent
1159 static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1160 struct sk_buff *skb, u32 *secid)
1163 /* TODO: requires secid support */
1164 return -ENOPROTOOPT;
1168 * apparmor_sock_graft - Initialize newly created socket
1170 * @parent: parent socket
1172 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1173 * just set sk security information off of current creating process label
1174 * Labeling of sk for accept case - probably should be sock based
1175 * instead of task, because of the case where an implicitly labeled
1176 * socket is shared by different tasks.
1178 static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1180 struct aa_sk_ctx *ctx = SK_CTX(sk);
1183 ctx->label = aa_get_current_label();
1186 #ifdef CONFIG_NETWORK_SECMARK
1187 static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1188 struct request_sock *req)
1190 struct aa_sk_ctx *ctx = SK_CTX(sk);
1195 return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1201 * The cred blob is a pointer to, not an instance of, an aa_label.
1203 struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = {
1204 .lbs_cred = sizeof(struct aa_label *),
1205 .lbs_file = sizeof(struct aa_file_ctx),
1206 .lbs_task = sizeof(struct aa_task_ctx),
1209 static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = {
1210 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1211 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1212 LSM_HOOK_INIT(capget, apparmor_capget),
1213 LSM_HOOK_INIT(capable, apparmor_capable),
1215 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1216 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1217 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1219 LSM_HOOK_INIT(path_link, apparmor_path_link),
1220 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1221 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1222 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1223 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1224 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1225 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1226 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1227 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1228 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1229 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1231 LSM_HOOK_INIT(file_open, apparmor_file_open),
1232 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1233 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1234 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1235 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1236 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1237 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1238 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
1240 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1241 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1243 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1244 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1245 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1247 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1248 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1249 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1250 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1251 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1252 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1253 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1254 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1255 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1256 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1257 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1258 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1259 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1260 #ifdef CONFIG_NETWORK_SECMARK
1261 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1263 LSM_HOOK_INIT(socket_getpeersec_stream,
1264 apparmor_socket_getpeersec_stream),
1265 LSM_HOOK_INIT(socket_getpeersec_dgram,
1266 apparmor_socket_getpeersec_dgram),
1267 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1268 #ifdef CONFIG_NETWORK_SECMARK
1269 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1272 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1273 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1274 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1275 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1277 LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1278 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1279 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1281 LSM_HOOK_INIT(task_free, apparmor_task_free),
1282 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
1283 LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj),
1284 LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
1285 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1286 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1289 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1290 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1291 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1292 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1295 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1296 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1297 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1301 * AppArmor sysfs module parameters
1304 static int param_set_aabool(const char *val, const struct kernel_param *kp);
1305 static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1306 #define param_check_aabool param_check_bool
1307 static const struct kernel_param_ops param_ops_aabool = {
1308 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1309 .set = param_set_aabool,
1310 .get = param_get_aabool
1313 static int param_set_aauint(const char *val, const struct kernel_param *kp);
1314 static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1315 #define param_check_aauint param_check_uint
1316 static const struct kernel_param_ops param_ops_aauint = {
1317 .set = param_set_aauint,
1318 .get = param_get_aauint
1321 static int param_set_aacompressionlevel(const char *val,
1322 const struct kernel_param *kp);
1323 static int param_get_aacompressionlevel(char *buffer,
1324 const struct kernel_param *kp);
1325 #define param_check_aacompressionlevel param_check_int
1326 static const struct kernel_param_ops param_ops_aacompressionlevel = {
1327 .set = param_set_aacompressionlevel,
1328 .get = param_get_aacompressionlevel
1331 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1332 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1333 #define param_check_aalockpolicy param_check_bool
1334 static const struct kernel_param_ops param_ops_aalockpolicy = {
1335 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1336 .set = param_set_aalockpolicy,
1337 .get = param_get_aalockpolicy
1340 static int param_set_audit(const char *val, const struct kernel_param *kp);
1341 static int param_get_audit(char *buffer, const struct kernel_param *kp);
1343 static int param_set_mode(const char *val, const struct kernel_param *kp);
1344 static int param_get_mode(char *buffer, const struct kernel_param *kp);
1346 /* Flag values, also controllable via /sys/module/apparmor/parameters
1347 * We define special types as we want to do additional mediation.
1350 /* AppArmor global enforcement switch - complain, enforce, kill */
1351 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1352 module_param_call(mode, param_set_mode, param_get_mode,
1353 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1355 /* whether policy verification hashing is enabled */
1356 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
1357 #ifdef CONFIG_SECURITY_APPARMOR_HASH
1358 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1361 /* whether policy exactly as loaded is retained for debug and checkpointing */
1362 bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1363 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1364 module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1367 /* policy loaddata compression level */
1368 int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
1369 module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
1370 aacompressionlevel, 0400);
1373 bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
1374 module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1377 enum audit_mode aa_g_audit;
1378 module_param_call(audit, param_set_audit, param_get_audit,
1379 &aa_g_audit, S_IRUSR | S_IWUSR);
1381 /* Determines if audit header is included in audited messages. This
1382 * provides more context if the audit daemon is not running
1384 bool aa_g_audit_header = true;
1385 module_param_named(audit_header, aa_g_audit_header, aabool,
1388 /* lock out loading/removal of policy
1389 * TODO: add in at boot loading of policy, which is the only way to
1390 * load policy, if lock_policy is set
1392 bool aa_g_lock_policy;
1393 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1396 /* Syscall logging mode */
1397 bool aa_g_logsyscall;
1398 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1400 /* Maximum pathname length before accesses will start getting rejected */
1401 unsigned int aa_g_path_max = 2 * PATH_MAX;
1402 module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1404 /* Determines how paranoid loading of policy is and how much verification
1405 * on the loaded policy is done.
1406 * DEPRECATED: read only as strict checking of load is always done now
1407 * that none root users (user namespaces) can load policy.
1409 bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1410 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1412 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1413 static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1414 #define param_check_aaintbool param_check_int
1415 static const struct kernel_param_ops param_ops_aaintbool = {
1416 .set = param_set_aaintbool,
1417 .get = param_get_aaintbool
1419 /* Boot time disable flag */
1420 static int apparmor_enabled __lsm_ro_after_init = 1;
1421 module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1423 static int __init apparmor_enabled_setup(char *str)
1425 unsigned long enabled;
1426 int error = kstrtoul(str, 0, &enabled);
1428 apparmor_enabled = enabled ? 1 : 0;
1432 __setup("apparmor=", apparmor_enabled_setup);
1434 /* set global flag turning off the ability to load policy */
1435 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1437 if (!apparmor_enabled)
1439 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1441 return param_set_bool(val, kp);
1444 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1446 if (!apparmor_enabled)
1448 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1450 return param_get_bool(buffer, kp);
1453 static int param_set_aabool(const char *val, const struct kernel_param *kp)
1455 if (!apparmor_enabled)
1457 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1459 return param_set_bool(val, kp);
1462 static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1464 if (!apparmor_enabled)
1466 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1468 return param_get_bool(buffer, kp);
1471 static int param_set_aauint(const char *val, const struct kernel_param *kp)
1475 if (!apparmor_enabled)
1477 /* file is ro but enforce 2nd line check */
1478 if (apparmor_initialized)
1481 error = param_set_uint(val, kp);
1482 aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
1483 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1488 static int param_get_aauint(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_uint(buffer, kp);
1497 /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
1498 static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1500 struct kernel_param kp_local;
1504 if (apparmor_initialized)
1507 /* Create local copy, with arg pointing to bool type. */
1508 value = !!*((int *)kp->arg);
1509 memcpy(&kp_local, kp, sizeof(kp_local));
1510 kp_local.arg = &value;
1512 error = param_set_bool(val, &kp_local);
1514 *((int *)kp->arg) = *((bool *)kp_local.arg);
1519 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1520 * 1/0, this converts the "int that is actually bool" back to bool for
1521 * display in the /sys filesystem, while keeping it "int" for the LSM
1524 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1526 struct kernel_param kp_local;
1529 /* Create local copy, with arg pointing to bool type. */
1530 value = !!*((int *)kp->arg);
1531 memcpy(&kp_local, kp, sizeof(kp_local));
1532 kp_local.arg = &value;
1534 return param_get_bool(buffer, &kp_local);
1537 static int param_set_aacompressionlevel(const char *val,
1538 const struct kernel_param *kp)
1542 if (!apparmor_enabled)
1544 if (apparmor_initialized)
1547 error = param_set_int(val, kp);
1549 aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
1550 AA_MIN_CLEVEL, AA_MAX_CLEVEL);
1551 pr_info("AppArmor: policy rawdata compression level set to %d\n",
1552 aa_g_rawdata_compression_level);
1557 static int param_get_aacompressionlevel(char *buffer,
1558 const struct kernel_param *kp)
1560 if (!apparmor_enabled)
1562 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1564 return param_get_int(buffer, kp);
1567 static int param_get_audit(char *buffer, const struct kernel_param *kp)
1569 if (!apparmor_enabled)
1571 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1573 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1576 static int param_set_audit(const char *val, const struct kernel_param *kp)
1580 if (!apparmor_enabled)
1584 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1587 i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
1595 static int param_get_mode(char *buffer, const struct kernel_param *kp)
1597 if (!apparmor_enabled)
1599 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1602 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1605 static int param_set_mode(const char *val, const struct kernel_param *kp)
1609 if (!apparmor_enabled)
1613 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1616 i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
1621 aa_g_profile_mode = i;
1625 char *aa_get_buffer(bool in_atomic)
1627 union aa_buffer *aa_buf;
1628 bool try_again = true;
1629 gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1632 spin_lock(&aa_buffers_lock);
1633 if (buffer_count > reserve_count ||
1634 (in_atomic && !list_empty(&aa_global_buffers))) {
1635 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1637 list_del(&aa_buf->list);
1639 spin_unlock(&aa_buffers_lock);
1640 return &aa_buf->buffer[0];
1644 * out of reserve buffers and in atomic context so increase
1645 * how many buffers to keep in reserve
1650 spin_unlock(&aa_buffers_lock);
1654 aa_buf = kmalloc(aa_g_path_max, flags);
1660 pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
1663 return &aa_buf->buffer[0];
1666 void aa_put_buffer(char *buf)
1668 union aa_buffer *aa_buf;
1672 aa_buf = container_of(buf, union aa_buffer, buffer[0]);
1674 spin_lock(&aa_buffers_lock);
1675 list_add(&aa_buf->list, &aa_global_buffers);
1677 spin_unlock(&aa_buffers_lock);
1681 * AppArmor init functions
1685 * set_init_ctx - set a task context and profile on the first task.
1687 * TODO: allow setting an alternate profile than unconfined
1689 static int __init set_init_ctx(void)
1691 struct cred *cred = (__force struct cred *)current->real_cred;
1693 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
1698 static void destroy_buffers(void)
1700 union aa_buffer *aa_buf;
1702 spin_lock(&aa_buffers_lock);
1703 while (!list_empty(&aa_global_buffers)) {
1704 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1706 list_del(&aa_buf->list);
1707 spin_unlock(&aa_buffers_lock);
1709 spin_lock(&aa_buffers_lock);
1711 spin_unlock(&aa_buffers_lock);
1714 static int __init alloc_buffers(void)
1716 union aa_buffer *aa_buf;
1720 * A function may require two buffers at once. Usually the buffers are
1721 * used for a short period of time and are shared. On UP kernel buffers
1722 * two should be enough, with more CPUs it is possible that more
1723 * buffers will be used simultaneously. The preallocated pool may grow.
1724 * This preallocation has also the side-effect that AppArmor will be
1725 * disabled early at boot if aa_g_path_max is extremly high.
1727 if (num_online_cpus() > 1)
1728 num = 4 + RESERVE_COUNT;
1730 num = 2 + RESERVE_COUNT;
1732 for (i = 0; i < num; i++) {
1734 aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
1735 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1740 aa_put_buffer(&aa_buf->buffer[0]);
1745 #ifdef CONFIG_SYSCTL
1746 static int apparmor_dointvec(struct ctl_table *table, int write,
1747 void *buffer, size_t *lenp, loff_t *ppos)
1749 if (!aa_current_policy_admin_capable(NULL))
1751 if (!apparmor_enabled)
1754 return proc_dointvec(table, write, buffer, lenp, ppos);
1757 static struct ctl_path apparmor_sysctl_path[] = {
1758 { .procname = "kernel", },
1762 static struct ctl_table apparmor_sysctl_table[] = {
1764 .procname = "unprivileged_userns_apparmor_policy",
1765 .data = &unprivileged_userns_apparmor_policy,
1766 .maxlen = sizeof(int),
1768 .proc_handler = apparmor_dointvec,
1771 .procname = "apparmor_display_secid_mode",
1772 .data = &apparmor_display_secid_mode,
1773 .maxlen = sizeof(int),
1775 .proc_handler = apparmor_dointvec,
1781 static int __init apparmor_init_sysctl(void)
1783 return register_sysctl_paths(apparmor_sysctl_path,
1784 apparmor_sysctl_table) ? 0 : -ENOMEM;
1787 static inline int apparmor_init_sysctl(void)
1791 #endif /* CONFIG_SYSCTL */
1793 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
1794 static unsigned int apparmor_ip_postroute(void *priv,
1795 struct sk_buff *skb,
1796 const struct nf_hook_state *state)
1798 struct aa_sk_ctx *ctx;
1804 sk = skb_to_full_sk(skb);
1809 if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
1813 return NF_DROP_ERR(-ECONNREFUSED);
1817 static const struct nf_hook_ops apparmor_nf_ops[] = {
1819 .hook = apparmor_ip_postroute,
1821 .hooknum = NF_INET_POST_ROUTING,
1822 .priority = NF_IP_PRI_SELINUX_FIRST,
1824 #if IS_ENABLED(CONFIG_IPV6)
1826 .hook = apparmor_ip_postroute,
1828 .hooknum = NF_INET_POST_ROUTING,
1829 .priority = NF_IP6_PRI_SELINUX_FIRST,
1834 static int __net_init apparmor_nf_register(struct net *net)
1836 return nf_register_net_hooks(net, apparmor_nf_ops,
1837 ARRAY_SIZE(apparmor_nf_ops));
1840 static void __net_exit apparmor_nf_unregister(struct net *net)
1842 nf_unregister_net_hooks(net, apparmor_nf_ops,
1843 ARRAY_SIZE(apparmor_nf_ops));
1846 static struct pernet_operations apparmor_net_ops = {
1847 .init = apparmor_nf_register,
1848 .exit = apparmor_nf_unregister,
1851 static int __init apparmor_nf_ip_init(void)
1855 if (!apparmor_enabled)
1858 err = register_pernet_subsys(&apparmor_net_ops);
1860 panic("Apparmor: register_pernet_subsys: error %d\n", err);
1864 __initcall(apparmor_nf_ip_init);
1867 static int __init apparmor_init(void)
1871 error = aa_setup_dfa_engine();
1873 AA_ERROR("Unable to setup dfa engine\n");
1877 error = aa_alloc_root_ns();
1879 AA_ERROR("Unable to allocate default profile namespace\n");
1883 error = apparmor_init_sysctl();
1885 AA_ERROR("Unable to register sysctls\n");
1890 error = alloc_buffers();
1892 AA_ERROR("Unable to allocate work buffers\n");
1896 error = set_init_ctx();
1898 AA_ERROR("Failed to set context on init task\n");
1902 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
1905 /* Report that AppArmor successfully initialized */
1906 apparmor_initialized = 1;
1907 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
1908 aa_info_message("AppArmor initialized: complain mode enabled");
1909 else if (aa_g_profile_mode == APPARMOR_KILL)
1910 aa_info_message("AppArmor initialized: kill mode enabled");
1912 aa_info_message("AppArmor initialized");
1920 aa_teardown_dfa_engine();
1922 apparmor_enabled = false;
1926 DEFINE_LSM(apparmor) = {
1928 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
1929 .enabled = &apparmor_enabled,
1930 .blobs = &apparmor_blob_sizes,
1931 .init = apparmor_init,