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>
27 #include <uapi/linux/lsm.h>
29 #include "include/apparmor.h"
30 #include "include/apparmorfs.h"
31 #include "include/audit.h"
32 #include "include/capability.h"
33 #include "include/cred.h"
34 #include "include/file.h"
35 #include "include/ipc.h"
36 #include "include/net.h"
37 #include "include/path.h"
38 #include "include/label.h"
39 #include "include/policy.h"
40 #include "include/policy_ns.h"
41 #include "include/procattr.h"
42 #include "include/mount.h"
43 #include "include/secid.h"
45 /* Flag indicating whether initialization completed */
46 int apparmor_initialized;
49 struct list_head list;
50 DECLARE_FLEX_ARRAY(char, buffer);
53 struct aa_local_cache {
56 struct list_head head;
59 #define RESERVE_COUNT 2
60 static int reserve_count = RESERVE_COUNT;
61 static int buffer_count;
63 static LIST_HEAD(aa_global_buffers);
64 static DEFINE_SPINLOCK(aa_buffers_lock);
65 static DEFINE_PER_CPU(struct aa_local_cache, aa_local_buffers);
72 * put the associated labels
74 static void apparmor_cred_free(struct cred *cred)
76 aa_put_label(cred_label(cred));
77 set_cred_label(cred, NULL);
81 * allocate the apparmor part of blank credentials
83 static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp)
85 set_cred_label(cred, NULL);
90 * prepare new cred label for modification by prepare_cred block
92 static int apparmor_cred_prepare(struct cred *new, const struct cred *old,
95 set_cred_label(new, aa_get_newest_label(cred_label(old)));
100 * transfer the apparmor data to a blank set of creds
102 static void apparmor_cred_transfer(struct cred *new, const struct cred *old)
104 set_cred_label(new, aa_get_newest_label(cred_label(old)));
107 static void apparmor_task_free(struct task_struct *task)
110 aa_free_task_ctx(task_ctx(task));
113 static int apparmor_task_alloc(struct task_struct *task,
114 unsigned long clone_flags)
116 struct aa_task_ctx *new = task_ctx(task);
118 aa_dup_task_ctx(new, task_ctx(current));
123 static int apparmor_ptrace_access_check(struct task_struct *child,
126 struct aa_label *tracer, *tracee;
127 const struct cred *cred;
130 cred = get_task_cred(child);
131 tracee = cred_label(cred); /* ref count on cred */
132 tracer = __begin_current_label_crit_section();
133 error = aa_may_ptrace(current_cred(), tracer, cred, tracee,
134 (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ
136 __end_current_label_crit_section(tracer);
142 static int apparmor_ptrace_traceme(struct task_struct *parent)
144 struct aa_label *tracer, *tracee;
145 const struct cred *cred;
148 tracee = __begin_current_label_crit_section();
149 cred = get_task_cred(parent);
150 tracer = cred_label(cred); /* ref count on cred */
151 error = aa_may_ptrace(cred, tracer, current_cred(), tracee,
154 __end_current_label_crit_section(tracee);
159 /* Derived from security/commoncap.c:cap_capget */
160 static int apparmor_capget(const struct task_struct *target, kernel_cap_t *effective,
161 kernel_cap_t *inheritable, kernel_cap_t *permitted)
163 struct aa_label *label;
164 const struct cred *cred;
167 cred = __task_cred(target);
168 label = aa_get_newest_cred_label(cred);
171 * cap_capget is stacked ahead of this and will
172 * initialize effective and permitted.
174 if (!unconfined(label)) {
175 struct aa_profile *profile;
178 label_for_each_confined(i, label, profile) {
179 struct aa_ruleset *rules;
180 if (COMPLAIN_MODE(profile))
182 rules = list_first_entry(&profile->rules,
183 typeof(*rules), list);
184 *effective = cap_intersect(*effective,
186 *permitted = cap_intersect(*permitted,
196 static int apparmor_capable(const struct cred *cred, struct user_namespace *ns,
197 int cap, unsigned int opts)
199 struct aa_label *label;
202 label = aa_get_newest_cred_label(cred);
203 if (!unconfined(label))
204 error = aa_capable(cred, label, cap, opts);
211 * common_perm - basic common permission check wrapper fn for paths
212 * @op: operation being checked
213 * @path: path to check permission of (NOT NULL)
214 * @mask: requested permissions mask
215 * @cond: conditional info for the permission request (NOT NULL)
217 * Returns: %0 else error code if error or permission denied
219 static int common_perm(const char *op, const struct path *path, u32 mask,
220 struct path_cond *cond)
222 struct aa_label *label;
225 label = __begin_current_label_crit_section();
226 if (!unconfined(label))
227 error = aa_path_perm(op, current_cred(), label, path, 0, mask,
229 __end_current_label_crit_section(label);
235 * common_perm_cond - common permission wrapper around inode cond
236 * @op: operation being checked
237 * @path: location to check (NOT NULL)
238 * @mask: requested permissions mask
240 * Returns: %0 else error code if error or permission denied
242 static int common_perm_cond(const char *op, const struct path *path, u32 mask)
244 vfsuid_t vfsuid = i_uid_into_vfsuid(mnt_idmap(path->mnt),
245 d_backing_inode(path->dentry));
246 struct path_cond cond = {
247 vfsuid_into_kuid(vfsuid),
248 d_backing_inode(path->dentry)->i_mode
251 if (!path_mediated_fs(path->dentry))
254 return common_perm(op, path, mask, &cond);
258 * common_perm_dir_dentry - common permission wrapper when path is dir, dentry
259 * @op: operation being checked
260 * @dir: directory of the dentry (NOT NULL)
261 * @dentry: dentry to check (NOT NULL)
262 * @mask: requested permissions mask
263 * @cond: conditional info for the permission request (NOT NULL)
265 * Returns: %0 else error code if error or permission denied
267 static int common_perm_dir_dentry(const char *op, const struct path *dir,
268 struct dentry *dentry, u32 mask,
269 struct path_cond *cond)
271 struct path path = { .mnt = dir->mnt, .dentry = dentry };
273 return common_perm(op, &path, mask, cond);
277 * common_perm_rm - common permission wrapper for operations doing rm
278 * @op: operation being checked
279 * @dir: directory that the dentry is in (NOT NULL)
280 * @dentry: dentry being rm'd (NOT NULL)
281 * @mask: requested permission mask
283 * Returns: %0 else error code if error or permission denied
285 static int common_perm_rm(const char *op, const struct path *dir,
286 struct dentry *dentry, u32 mask)
288 struct inode *inode = d_backing_inode(dentry);
289 struct path_cond cond = { };
292 if (!inode || !path_mediated_fs(dentry))
295 vfsuid = i_uid_into_vfsuid(mnt_idmap(dir->mnt), inode);
296 cond.uid = vfsuid_into_kuid(vfsuid);
297 cond.mode = inode->i_mode;
299 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
303 * common_perm_create - common permission wrapper for operations doing create
304 * @op: operation being checked
305 * @dir: directory that dentry will be created in (NOT NULL)
306 * @dentry: dentry to create (NOT NULL)
307 * @mask: request permission mask
308 * @mode: created file mode
310 * Returns: %0 else error code if error or permission denied
312 static int common_perm_create(const char *op, const struct path *dir,
313 struct dentry *dentry, u32 mask, umode_t mode)
315 struct path_cond cond = { current_fsuid(), mode };
317 if (!path_mediated_fs(dir->dentry))
320 return common_perm_dir_dentry(op, dir, dentry, mask, &cond);
323 static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry)
325 return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE);
328 static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry,
331 return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE,
335 static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry)
337 return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE);
340 static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry,
341 umode_t mode, unsigned int dev)
343 return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode);
346 static int apparmor_path_truncate(const struct path *path)
348 return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR);
351 static int apparmor_file_truncate(struct file *file)
353 return apparmor_path_truncate(&file->f_path);
356 static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry,
357 const char *old_name)
359 return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE,
363 static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir,
364 struct dentry *new_dentry)
366 struct aa_label *label;
369 if (!path_mediated_fs(old_dentry))
372 label = begin_current_label_crit_section();
373 if (!unconfined(label))
374 error = aa_path_link(current_cred(), label, old_dentry, new_dir,
376 end_current_label_crit_section(label);
381 static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry,
382 const struct path *new_dir, struct dentry *new_dentry,
383 const unsigned int flags)
385 struct aa_label *label;
388 if (!path_mediated_fs(old_dentry))
390 if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry))
393 label = begin_current_label_crit_section();
394 if (!unconfined(label)) {
395 struct mnt_idmap *idmap = mnt_idmap(old_dir->mnt);
397 struct path old_path = { .mnt = old_dir->mnt,
398 .dentry = old_dentry };
399 struct path new_path = { .mnt = new_dir->mnt,
400 .dentry = new_dentry };
401 struct path_cond cond = {
402 .mode = d_backing_inode(old_dentry)->i_mode
404 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
405 cond.uid = vfsuid_into_kuid(vfsuid);
407 if (flags & RENAME_EXCHANGE) {
408 struct path_cond cond_exchange = {
409 .mode = d_backing_inode(new_dentry)->i_mode,
411 vfsuid = i_uid_into_vfsuid(idmap, d_backing_inode(old_dentry));
412 cond_exchange.uid = vfsuid_into_kuid(vfsuid);
414 error = aa_path_perm(OP_RENAME_SRC, current_cred(),
416 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
417 AA_MAY_SETATTR | AA_MAY_DELETE,
420 error = aa_path_perm(OP_RENAME_DEST, current_cred(),
422 0, MAY_WRITE | AA_MAY_SETATTR |
423 AA_MAY_CREATE, &cond_exchange);
427 error = aa_path_perm(OP_RENAME_SRC, current_cred(),
429 MAY_READ | AA_MAY_GETATTR | MAY_WRITE |
430 AA_MAY_SETATTR | AA_MAY_DELETE,
433 error = aa_path_perm(OP_RENAME_DEST, current_cred(),
435 0, MAY_WRITE | AA_MAY_SETATTR |
436 AA_MAY_CREATE, &cond);
439 end_current_label_crit_section(label);
444 static int apparmor_path_chmod(const struct path *path, umode_t mode)
446 return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD);
449 static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
451 return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN);
454 static int apparmor_inode_getattr(const struct path *path)
456 return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR);
459 static int apparmor_file_open(struct file *file)
461 struct aa_file_ctx *fctx = file_ctx(file);
462 struct aa_label *label;
465 if (!path_mediated_fs(file->f_path.dentry))
468 /* If in exec, permission is handled by bprm hooks.
469 * Cache permissions granted by the previous exec check, with
470 * implicit read and executable mmap which are required to
471 * actually execute the image.
473 * Illogically, FMODE_EXEC is in f_flags, not f_mode.
475 if (file->f_flags & __FMODE_EXEC) {
476 fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP;
480 label = aa_get_newest_cred_label(file->f_cred);
481 if (!unconfined(label)) {
482 struct mnt_idmap *idmap = file_mnt_idmap(file);
483 struct inode *inode = file_inode(file);
485 struct path_cond cond = {
486 .mode = inode->i_mode,
488 vfsuid = i_uid_into_vfsuid(idmap, inode);
489 cond.uid = vfsuid_into_kuid(vfsuid);
491 error = aa_path_perm(OP_OPEN, file->f_cred,
492 label, &file->f_path, 0,
493 aa_map_file_to_perms(file), &cond);
494 /* todo cache full allowed permissions set and state */
495 fctx->allow = aa_map_file_to_perms(file);
502 static int apparmor_file_alloc_security(struct file *file)
504 struct aa_file_ctx *ctx = file_ctx(file);
505 struct aa_label *label = begin_current_label_crit_section();
507 spin_lock_init(&ctx->lock);
508 rcu_assign_pointer(ctx->label, aa_get_label(label));
509 end_current_label_crit_section(label);
513 static void apparmor_file_free_security(struct file *file)
515 struct aa_file_ctx *ctx = file_ctx(file);
518 aa_put_label(rcu_access_pointer(ctx->label));
521 static int common_file_perm(const char *op, struct file *file, u32 mask,
524 struct aa_label *label;
527 /* don't reaudit files closed during inheritance */
528 if (file->f_path.dentry == aa_null.dentry)
531 label = __begin_current_label_crit_section();
532 error = aa_file_perm(op, current_cred(), label, file, mask, in_atomic);
533 __end_current_label_crit_section(label);
538 static int apparmor_file_receive(struct file *file)
540 return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file),
544 static int apparmor_file_permission(struct file *file, int mask)
546 return common_file_perm(OP_FPERM, file, mask, false);
549 static int apparmor_file_lock(struct file *file, unsigned int cmd)
551 u32 mask = AA_MAY_LOCK;
556 return common_file_perm(OP_FLOCK, file, mask, false);
559 static int common_mmap(const char *op, struct file *file, unsigned long prot,
560 unsigned long flags, bool in_atomic)
564 if (!file || !file_ctx(file))
567 if (prot & PROT_READ)
570 * Private mappings don't require write perms since they don't
571 * write back to the files
573 if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE))
575 if (prot & PROT_EXEC)
576 mask |= AA_EXEC_MMAP;
578 return common_file_perm(op, file, mask, in_atomic);
581 static int apparmor_mmap_file(struct file *file, unsigned long reqprot,
582 unsigned long prot, unsigned long flags)
584 return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC);
587 static int apparmor_file_mprotect(struct vm_area_struct *vma,
588 unsigned long reqprot, unsigned long prot)
590 return common_mmap(OP_FMPROT, vma->vm_file, prot,
591 !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0,
595 #ifdef CONFIG_IO_URING
596 static const char *audit_uring_mask(u32 mask)
598 if (mask & AA_MAY_CREATE_SQPOLL)
600 if (mask & AA_MAY_OVERRIDE_CRED)
601 return "override_creds";
605 static void audit_uring_cb(struct audit_buffer *ab, void *va)
607 struct apparmor_audit_data *ad = aad_of_va(va);
609 if (ad->request & AA_URING_PERM_MASK) {
610 audit_log_format(ab, " requested=\"%s\"",
611 audit_uring_mask(ad->request));
612 if (ad->denied & AA_URING_PERM_MASK) {
613 audit_log_format(ab, " denied=\"%s\"",
614 audit_uring_mask(ad->denied));
617 if (ad->uring.target) {
618 audit_log_format(ab, " tcontext=");
619 aa_label_xaudit(ab, labels_ns(ad->subj_label),
621 FLAGS_NONE, GFP_ATOMIC);
625 static int profile_uring(struct aa_profile *profile, u32 request,
626 struct aa_label *new, int cap,
627 struct apparmor_audit_data *ad)
630 struct aa_ruleset *rules;
635 rules = list_first_entry(&profile->rules, typeof(*rules), list);
636 state = RULE_MEDIATES(rules, AA_CLASS_IO_URING);
638 struct aa_perms perms = { };
641 aa_label_match(profile, rules, new, state,
642 false, request, &perms);
644 perms = *aa_lookup_perms(rules->policy, state);
646 aa_apply_modes_to_perms(profile, &perms);
647 error = aa_check_perms(profile, &perms, request, ad,
655 * apparmor_uring_override_creds - check the requested cred override
656 * @new: the target creds
658 * Check to see if the current task is allowed to override it's credentials
659 * to service an io_uring operation.
661 static int apparmor_uring_override_creds(const struct cred *new)
663 struct aa_profile *profile;
664 struct aa_label *label;
666 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
669 ad.uring.target = cred_label(new);
670 label = __begin_current_label_crit_section();
671 error = fn_for_each(label, profile,
672 profile_uring(profile, AA_MAY_OVERRIDE_CRED,
673 cred_label(new), CAP_SYS_ADMIN, &ad));
674 __end_current_label_crit_section(label);
680 * apparmor_uring_sqpoll - check if a io_uring polling thread can be created
682 * Check to see if the current task is allowed to create a new io_uring
683 * kernel polling thread.
685 static int apparmor_uring_sqpoll(void)
687 struct aa_profile *profile;
688 struct aa_label *label;
690 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_IO_URING,
693 label = __begin_current_label_crit_section();
694 error = fn_for_each(label, profile,
695 profile_uring(profile, AA_MAY_CREATE_SQPOLL,
696 NULL, CAP_SYS_ADMIN, &ad));
697 __end_current_label_crit_section(label);
701 #endif /* CONFIG_IO_URING */
703 static int apparmor_sb_mount(const char *dev_name, const struct path *path,
704 const char *type, unsigned long flags, void *data)
706 struct aa_label *label;
710 if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
711 flags &= ~MS_MGC_MSK;
713 flags &= ~AA_MS_IGNORE_MASK;
715 label = __begin_current_label_crit_section();
716 if (!unconfined(label)) {
717 if (flags & MS_REMOUNT)
718 error = aa_remount(current_cred(), label, path, flags,
720 else if (flags & MS_BIND)
721 error = aa_bind_mount(current_cred(), label, path,
723 else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE |
725 error = aa_mount_change_type(current_cred(), label,
727 else if (flags & MS_MOVE)
728 error = aa_move_mount_old(current_cred(), label, path,
731 error = aa_new_mount(current_cred(), label, dev_name,
732 path, type, flags, data);
734 __end_current_label_crit_section(label);
739 static int apparmor_move_mount(const struct path *from_path,
740 const struct path *to_path)
742 struct aa_label *label;
745 label = __begin_current_label_crit_section();
746 if (!unconfined(label))
747 error = aa_move_mount(current_cred(), label, from_path,
749 __end_current_label_crit_section(label);
754 static int apparmor_sb_umount(struct vfsmount *mnt, int flags)
756 struct aa_label *label;
759 label = __begin_current_label_crit_section();
760 if (!unconfined(label))
761 error = aa_umount(current_cred(), label, mnt, flags);
762 __end_current_label_crit_section(label);
767 static int apparmor_sb_pivotroot(const struct path *old_path,
768 const struct path *new_path)
770 struct aa_label *label;
773 label = aa_get_current_label();
774 if (!unconfined(label))
775 error = aa_pivotroot(current_cred(), label, old_path, new_path);
781 static int apparmor_getselfattr(unsigned int attr, struct lsm_ctx __user *lx,
782 u32 *size, u32 flags)
785 struct aa_task_ctx *ctx = task_ctx(current);
786 struct aa_label *label = NULL;
790 case LSM_ATTR_CURRENT:
791 label = aa_get_newest_label(cred_label(current_cred()));
795 label = aa_get_newest_label(ctx->previous);
799 label = aa_get_newest_label(ctx->onexec);
807 error = aa_getprocattr(label, &value, false);
809 error = lsm_fill_user_ctx(lx, size, value, error,
821 static int apparmor_getprocattr(struct task_struct *task, const char *name,
826 const struct cred *cred = get_task_cred(task);
827 struct aa_task_ctx *ctx = task_ctx(current);
828 struct aa_label *label = NULL;
830 if (strcmp(name, "current") == 0)
831 label = aa_get_newest_label(cred_label(cred));
832 else if (strcmp(name, "prev") == 0 && ctx->previous)
833 label = aa_get_newest_label(ctx->previous);
834 else if (strcmp(name, "exec") == 0 && ctx->onexec)
835 label = aa_get_newest_label(ctx->onexec);
840 error = aa_getprocattr(label, value, true);
848 static int do_setattr(u64 attr, void *value, size_t size)
850 char *command, *largs = NULL, *args = value;
853 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE,
859 /* AppArmor requires that the buffer must be null terminated atm */
860 if (args[size - 1] != '\0') {
862 largs = args = kmalloc(size + 1, GFP_KERNEL);
865 memcpy(args, value, size);
871 command = strsep(&args, " ");
874 args = skip_spaces(args);
878 arg_size = size - (args - (largs ? largs : (char *) value));
879 if (attr == LSM_ATTR_CURRENT) {
880 if (strcmp(command, "changehat") == 0) {
881 error = aa_setprocattr_changehat(args, arg_size,
883 } else if (strcmp(command, "permhat") == 0) {
884 error = aa_setprocattr_changehat(args, arg_size,
886 } else if (strcmp(command, "changeprofile") == 0) {
887 error = aa_change_profile(args, AA_CHANGE_NOFLAGS);
888 } else if (strcmp(command, "permprofile") == 0) {
889 error = aa_change_profile(args, AA_CHANGE_TEST);
890 } else if (strcmp(command, "stack") == 0) {
891 error = aa_change_profile(args, AA_CHANGE_STACK);
894 } else if (attr == LSM_ATTR_EXEC) {
895 if (strcmp(command, "exec") == 0)
896 error = aa_change_profile(args, AA_CHANGE_ONEXEC);
897 else if (strcmp(command, "stack") == 0)
898 error = aa_change_profile(args, (AA_CHANGE_ONEXEC |
903 /* only support the "current" and "exec" process attributes */
913 ad.subj_label = begin_current_label_crit_section();
914 if (attr == LSM_ATTR_CURRENT)
916 else if (attr == LSM_ATTR_EXEC)
920 ad.error = error = -EINVAL;
921 aa_audit_msg(AUDIT_APPARMOR_DENIED, &ad, NULL);
922 end_current_label_crit_section(ad.subj_label);
926 static int apparmor_setselfattr(unsigned int attr, struct lsm_ctx *ctx,
931 if (attr != LSM_ATTR_CURRENT && attr != LSM_ATTR_EXEC)
934 rc = do_setattr(attr, ctx->ctx, ctx->ctx_len);
940 static int apparmor_setprocattr(const char *name, void *value,
943 int attr = lsm_name_to_attr(name);
946 return do_setattr(attr, value, size);
951 * apparmor_bprm_committing_creds - do task cleanup on committing new creds
952 * @bprm: binprm for the exec (NOT NULL)
954 static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
956 struct aa_label *label = aa_current_raw_label();
957 struct aa_label *new_label = cred_label(bprm->cred);
959 /* bail out if unconfined or not changing profile */
960 if ((new_label->proxy == label->proxy) ||
961 (unconfined(new_label)))
964 aa_inherit_files(bprm->cred, current->files);
966 current->pdeath_signal = 0;
968 /* reset soft limits and set hard limits for the new label */
969 __aa_transition_rlimits(label, new_label);
973 * apparmor_bprm_committed_creds() - do cleanup after new creds committed
974 * @bprm: binprm for the exec (NOT NULL)
976 static void apparmor_bprm_committed_creds(const struct linux_binprm *bprm)
978 /* clear out temporary/transitional state from the context */
979 aa_clear_task_ctx_trans(task_ctx(current));
984 static void apparmor_current_getsecid_subj(u32 *secid)
986 struct aa_label *label = __begin_current_label_crit_section();
987 *secid = label->secid;
988 __end_current_label_crit_section(label);
991 static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid)
993 struct aa_label *label = aa_get_task_label(p);
994 *secid = label->secid;
998 static int apparmor_task_setrlimit(struct task_struct *task,
999 unsigned int resource, struct rlimit *new_rlim)
1001 struct aa_label *label = __begin_current_label_crit_section();
1004 if (!unconfined(label))
1005 error = aa_task_setrlimit(current_cred(), label, task,
1006 resource, new_rlim);
1007 __end_current_label_crit_section(label);
1012 static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info,
1013 int sig, const struct cred *cred)
1015 const struct cred *tc;
1016 struct aa_label *cl, *tl;
1019 tc = get_task_cred(target);
1020 tl = aa_get_newest_cred_label(tc);
1023 * Dealing with USB IO specific behavior
1025 cl = aa_get_newest_cred_label(cred);
1026 error = aa_may_signal(cred, cl, tc, tl, sig);
1029 cl = __begin_current_label_crit_section();
1030 error = aa_may_signal(current_cred(), cl, tc, tl, sig);
1031 __end_current_label_crit_section(cl);
1039 static int apparmor_userns_create(const struct cred *cred)
1041 struct aa_label *label;
1042 struct aa_profile *profile;
1044 DEFINE_AUDIT_DATA(ad, LSM_AUDIT_DATA_TASK, AA_CLASS_NS,
1047 ad.subj_cred = current_cred();
1049 label = begin_current_label_crit_section();
1050 if (!unconfined(label)) {
1051 error = fn_for_each(label, profile,
1052 aa_profile_ns_perm(profile, &ad,
1055 end_current_label_crit_section(label);
1060 static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags)
1062 struct aa_sk_ctx *ctx;
1064 ctx = kzalloc(sizeof(*ctx), flags);
1068 sk->sk_security = ctx;
1073 static void apparmor_sk_free_security(struct sock *sk)
1075 struct aa_sk_ctx *ctx = aa_sock(sk);
1077 sk->sk_security = NULL;
1078 aa_put_label(ctx->label);
1079 aa_put_label(ctx->peer);
1084 * apparmor_sk_clone_security - clone the sk_security field
1085 * @sk: sock to have security cloned
1086 * @newsk: sock getting clone
1088 static void apparmor_sk_clone_security(const struct sock *sk,
1091 struct aa_sk_ctx *ctx = aa_sock(sk);
1092 struct aa_sk_ctx *new = aa_sock(newsk);
1095 aa_put_label(new->label);
1096 new->label = aa_get_label(ctx->label);
1099 aa_put_label(new->peer);
1100 new->peer = aa_get_label(ctx->peer);
1103 static int apparmor_socket_create(int family, int type, int protocol, int kern)
1105 struct aa_label *label;
1108 AA_BUG(in_interrupt());
1110 label = begin_current_label_crit_section();
1111 if (!(kern || unconfined(label)))
1112 error = af_select(family,
1113 create_perm(label, family, type, protocol),
1114 aa_af_perm(current_cred(), label,
1115 OP_CREATE, AA_MAY_CREATE,
1116 family, type, protocol));
1117 end_current_label_crit_section(label);
1123 * apparmor_socket_post_create - setup the per-socket security struct
1124 * @sock: socket that is being setup
1125 * @family: family of socket being created
1126 * @type: type of the socket
1127 * @ptotocol: protocol of the socket
1128 * @kern: socket is a special kernel socket
1131 * - kernel sockets labeled kernel_t used to use unconfined
1132 * - socket may not have sk here if created with sock_create_lite or
1133 * sock_alloc. These should be accept cases which will be handled in
1136 static int apparmor_socket_post_create(struct socket *sock, int family,
1137 int type, int protocol, int kern)
1139 struct aa_label *label;
1142 label = aa_get_label(kernel_t);
1144 label = aa_get_current_label();
1147 struct aa_sk_ctx *ctx = aa_sock(sock->sk);
1149 aa_put_label(ctx->label);
1150 ctx->label = aa_get_label(label);
1152 aa_put_label(label);
1157 static int apparmor_socket_bind(struct socket *sock,
1158 struct sockaddr *address, int addrlen)
1163 AA_BUG(in_interrupt());
1165 return af_select(sock->sk->sk_family,
1166 bind_perm(sock, address, addrlen),
1167 aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk));
1170 static int apparmor_socket_connect(struct socket *sock,
1171 struct sockaddr *address, int addrlen)
1176 AA_BUG(in_interrupt());
1178 return af_select(sock->sk->sk_family,
1179 connect_perm(sock, address, addrlen),
1180 aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk));
1183 static int apparmor_socket_listen(struct socket *sock, int backlog)
1187 AA_BUG(in_interrupt());
1189 return af_select(sock->sk->sk_family,
1190 listen_perm(sock, backlog),
1191 aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk));
1195 * Note: while @newsock is created and has some information, the accept
1196 * has not been done.
1198 static int apparmor_socket_accept(struct socket *sock, struct socket *newsock)
1203 AA_BUG(in_interrupt());
1205 return af_select(sock->sk->sk_family,
1206 accept_perm(sock, newsock),
1207 aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk));
1210 static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock,
1211 struct msghdr *msg, int size)
1216 AA_BUG(in_interrupt());
1218 return af_select(sock->sk->sk_family,
1219 msg_perm(op, request, sock, msg, size),
1220 aa_sk_perm(op, request, sock->sk));
1223 static int apparmor_socket_sendmsg(struct socket *sock,
1224 struct msghdr *msg, int size)
1226 return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size);
1229 static int apparmor_socket_recvmsg(struct socket *sock,
1230 struct msghdr *msg, int size, int flags)
1232 return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size);
1235 /* revaliation, get/set attr, shutdown */
1236 static int aa_sock_perm(const char *op, u32 request, struct socket *sock)
1240 AA_BUG(in_interrupt());
1242 return af_select(sock->sk->sk_family,
1243 sock_perm(op, request, sock),
1244 aa_sk_perm(op, request, sock->sk));
1247 static int apparmor_socket_getsockname(struct socket *sock)
1249 return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock);
1252 static int apparmor_socket_getpeername(struct socket *sock)
1254 return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock);
1257 /* revaliation, get/set attr, opt */
1258 static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock,
1259 int level, int optname)
1263 AA_BUG(in_interrupt());
1265 return af_select(sock->sk->sk_family,
1266 opt_perm(op, request, sock, level, optname),
1267 aa_sk_perm(op, request, sock->sk));
1270 static int apparmor_socket_getsockopt(struct socket *sock, int level,
1273 return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock,
1277 static int apparmor_socket_setsockopt(struct socket *sock, int level,
1280 return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock,
1284 static int apparmor_socket_shutdown(struct socket *sock, int how)
1286 return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock);
1289 #ifdef CONFIG_NETWORK_SECMARK
1291 * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk
1292 * @sk: sk to associate @skb with
1293 * @skb: skb to check for perms
1295 * Note: can not sleep may be called with locks held
1297 * dont want protocol specific in __skb_recv_datagram()
1298 * to deny an incoming connection socket_sock_rcv_skb()
1300 static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb)
1302 struct aa_sk_ctx *ctx = aa_sock(sk);
1307 return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE,
1313 static struct aa_label *sk_peer_label(struct sock *sk)
1315 struct aa_sk_ctx *ctx = aa_sock(sk);
1320 return ERR_PTR(-ENOPROTOOPT);
1324 * apparmor_socket_getpeersec_stream - get security context of peer
1325 * @sock: socket that we are trying to get the peer context of
1326 * @optval: output - buffer to copy peer name to
1327 * @optlen: output - size of copied name in @optval
1328 * @len: size of @optval buffer
1329 * Returns: 0 on success, -errno of failure
1331 * Note: for tcp only valid if using ipsec or cipso on lan
1333 static int apparmor_socket_getpeersec_stream(struct socket *sock,
1334 sockptr_t optval, sockptr_t optlen,
1338 int slen, error = 0;
1339 struct aa_label *label;
1340 struct aa_label *peer;
1342 label = begin_current_label_crit_section();
1343 peer = sk_peer_label(sock->sk);
1345 error = PTR_ERR(peer);
1348 slen = aa_label_asxprint(&name, labels_ns(label), peer,
1349 FLAG_SHOW_MODE | FLAG_VIEW_SUBNS |
1350 FLAG_HIDDEN_UNCONFINED, GFP_KERNEL);
1351 /* don't include terminating \0 in slen, it breaks some apps */
1361 if (copy_to_sockptr(optval, name, slen))
1364 if (copy_to_sockptr(optlen, &slen, sizeof(slen)))
1367 end_current_label_crit_section(label);
1373 * apparmor_socket_getpeersec_dgram - get security label of packet
1374 * @sock: the peer socket
1376 * @secid: pointer to where to put the secid of the packet
1378 * Sets the netlabel socket state on sk from parent
1380 static int apparmor_socket_getpeersec_dgram(struct socket *sock,
1381 struct sk_buff *skb, u32 *secid)
1384 /* TODO: requires secid support */
1385 return -ENOPROTOOPT;
1389 * apparmor_sock_graft - Initialize newly created socket
1391 * @parent: parent socket
1393 * Note: could set off of SOCK_CTX(parent) but need to track inode and we can
1394 * just set sk security information off of current creating process label
1395 * Labeling of sk for accept case - probably should be sock based
1396 * instead of task, because of the case where an implicitly labeled
1397 * socket is shared by different tasks.
1399 static void apparmor_sock_graft(struct sock *sk, struct socket *parent)
1401 struct aa_sk_ctx *ctx = aa_sock(sk);
1404 ctx->label = aa_get_current_label();
1407 #ifdef CONFIG_NETWORK_SECMARK
1408 static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb,
1409 struct request_sock *req)
1411 struct aa_sk_ctx *ctx = aa_sock(sk);
1416 return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT,
1422 * The cred blob is a pointer to, not an instance of, an aa_label.
1424 struct lsm_blob_sizes apparmor_blob_sizes __ro_after_init = {
1425 .lbs_cred = sizeof(struct aa_label *),
1426 .lbs_file = sizeof(struct aa_file_ctx),
1427 .lbs_task = sizeof(struct aa_task_ctx),
1430 static const struct lsm_id apparmor_lsmid = {
1432 .id = LSM_ID_APPARMOR,
1435 static struct security_hook_list apparmor_hooks[] __ro_after_init = {
1436 LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check),
1437 LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme),
1438 LSM_HOOK_INIT(capget, apparmor_capget),
1439 LSM_HOOK_INIT(capable, apparmor_capable),
1441 LSM_HOOK_INIT(move_mount, apparmor_move_mount),
1442 LSM_HOOK_INIT(sb_mount, apparmor_sb_mount),
1443 LSM_HOOK_INIT(sb_umount, apparmor_sb_umount),
1444 LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot),
1446 LSM_HOOK_INIT(path_link, apparmor_path_link),
1447 LSM_HOOK_INIT(path_unlink, apparmor_path_unlink),
1448 LSM_HOOK_INIT(path_symlink, apparmor_path_symlink),
1449 LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir),
1450 LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir),
1451 LSM_HOOK_INIT(path_mknod, apparmor_path_mknod),
1452 LSM_HOOK_INIT(path_rename, apparmor_path_rename),
1453 LSM_HOOK_INIT(path_chmod, apparmor_path_chmod),
1454 LSM_HOOK_INIT(path_chown, apparmor_path_chown),
1455 LSM_HOOK_INIT(path_truncate, apparmor_path_truncate),
1456 LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr),
1458 LSM_HOOK_INIT(file_open, apparmor_file_open),
1459 LSM_HOOK_INIT(file_receive, apparmor_file_receive),
1460 LSM_HOOK_INIT(file_permission, apparmor_file_permission),
1461 LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security),
1462 LSM_HOOK_INIT(file_free_security, apparmor_file_free_security),
1463 LSM_HOOK_INIT(mmap_file, apparmor_mmap_file),
1464 LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect),
1465 LSM_HOOK_INIT(file_lock, apparmor_file_lock),
1466 LSM_HOOK_INIT(file_truncate, apparmor_file_truncate),
1468 LSM_HOOK_INIT(getselfattr, apparmor_getselfattr),
1469 LSM_HOOK_INIT(setselfattr, apparmor_setselfattr),
1470 LSM_HOOK_INIT(getprocattr, apparmor_getprocattr),
1471 LSM_HOOK_INIT(setprocattr, apparmor_setprocattr),
1473 LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security),
1474 LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security),
1475 LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security),
1477 LSM_HOOK_INIT(socket_create, apparmor_socket_create),
1478 LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create),
1479 LSM_HOOK_INIT(socket_bind, apparmor_socket_bind),
1480 LSM_HOOK_INIT(socket_connect, apparmor_socket_connect),
1481 LSM_HOOK_INIT(socket_listen, apparmor_socket_listen),
1482 LSM_HOOK_INIT(socket_accept, apparmor_socket_accept),
1483 LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg),
1484 LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg),
1485 LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname),
1486 LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername),
1487 LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt),
1488 LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt),
1489 LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown),
1490 #ifdef CONFIG_NETWORK_SECMARK
1491 LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb),
1493 LSM_HOOK_INIT(socket_getpeersec_stream,
1494 apparmor_socket_getpeersec_stream),
1495 LSM_HOOK_INIT(socket_getpeersec_dgram,
1496 apparmor_socket_getpeersec_dgram),
1497 LSM_HOOK_INIT(sock_graft, apparmor_sock_graft),
1498 #ifdef CONFIG_NETWORK_SECMARK
1499 LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request),
1502 LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank),
1503 LSM_HOOK_INIT(cred_free, apparmor_cred_free),
1504 LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare),
1505 LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
1507 LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
1508 LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
1509 LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
1511 LSM_HOOK_INIT(task_free, apparmor_task_free),
1512 LSM_HOOK_INIT(task_alloc, apparmor_task_alloc),
1513 LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj),
1514 LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj),
1515 LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit),
1516 LSM_HOOK_INIT(task_kill, apparmor_task_kill),
1517 LSM_HOOK_INIT(userns_create, apparmor_userns_create),
1520 LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init),
1521 LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known),
1522 LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match),
1523 LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free),
1526 LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx),
1527 LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid),
1528 LSM_HOOK_INIT(release_secctx, apparmor_release_secctx),
1530 #ifdef CONFIG_IO_URING
1531 LSM_HOOK_INIT(uring_override_creds, apparmor_uring_override_creds),
1532 LSM_HOOK_INIT(uring_sqpoll, apparmor_uring_sqpoll),
1537 * AppArmor sysfs module parameters
1540 static int param_set_aabool(const char *val, const struct kernel_param *kp);
1541 static int param_get_aabool(char *buffer, const struct kernel_param *kp);
1542 #define param_check_aabool param_check_bool
1543 static const struct kernel_param_ops param_ops_aabool = {
1544 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1545 .set = param_set_aabool,
1546 .get = param_get_aabool
1549 static int param_set_aauint(const char *val, const struct kernel_param *kp);
1550 static int param_get_aauint(char *buffer, const struct kernel_param *kp);
1551 #define param_check_aauint param_check_uint
1552 static const struct kernel_param_ops param_ops_aauint = {
1553 .set = param_set_aauint,
1554 .get = param_get_aauint
1557 static int param_set_aacompressionlevel(const char *val,
1558 const struct kernel_param *kp);
1559 static int param_get_aacompressionlevel(char *buffer,
1560 const struct kernel_param *kp);
1561 #define param_check_aacompressionlevel param_check_int
1562 static const struct kernel_param_ops param_ops_aacompressionlevel = {
1563 .set = param_set_aacompressionlevel,
1564 .get = param_get_aacompressionlevel
1567 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp);
1568 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp);
1569 #define param_check_aalockpolicy param_check_bool
1570 static const struct kernel_param_ops param_ops_aalockpolicy = {
1571 .flags = KERNEL_PARAM_OPS_FL_NOARG,
1572 .set = param_set_aalockpolicy,
1573 .get = param_get_aalockpolicy
1576 static int param_set_audit(const char *val, const struct kernel_param *kp);
1577 static int param_get_audit(char *buffer, const struct kernel_param *kp);
1579 static int param_set_mode(const char *val, const struct kernel_param *kp);
1580 static int param_get_mode(char *buffer, const struct kernel_param *kp);
1582 /* Flag values, also controllable via /sys/module/apparmor/parameters
1583 * We define special types as we want to do additional mediation.
1586 /* AppArmor global enforcement switch - complain, enforce, kill */
1587 enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE;
1588 module_param_call(mode, param_set_mode, param_get_mode,
1589 &aa_g_profile_mode, S_IRUSR | S_IWUSR);
1591 /* whether policy verification hashing is enabled */
1592 bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT);
1593 #ifdef CONFIG_SECURITY_APPARMOR_HASH
1594 module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR);
1597 /* whether policy exactly as loaded is retained for debug and checkpointing */
1598 bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY);
1599 #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY
1600 module_param_named(export_binary, aa_g_export_binary, aabool, 0600);
1603 /* policy loaddata compression level */
1604 int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL;
1605 module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level,
1606 aacompressionlevel, 0400);
1609 bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES);
1610 module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR);
1613 enum audit_mode aa_g_audit;
1614 module_param_call(audit, param_set_audit, param_get_audit,
1615 &aa_g_audit, S_IRUSR | S_IWUSR);
1617 /* Determines if audit header is included in audited messages. This
1618 * provides more context if the audit daemon is not running
1620 bool aa_g_audit_header = true;
1621 module_param_named(audit_header, aa_g_audit_header, aabool,
1624 /* lock out loading/removal of policy
1625 * TODO: add in at boot loading of policy, which is the only way to
1626 * load policy, if lock_policy is set
1628 bool aa_g_lock_policy;
1629 module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy,
1632 /* Syscall logging mode */
1633 bool aa_g_logsyscall;
1634 module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR);
1636 /* Maximum pathname length before accesses will start getting rejected */
1637 unsigned int aa_g_path_max = 2 * PATH_MAX;
1638 module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR);
1640 /* Determines how paranoid loading of policy is and how much verification
1641 * on the loaded policy is done.
1642 * DEPRECATED: read only as strict checking of load is always done now
1643 * that none root users (user namespaces) can load policy.
1645 bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD);
1646 module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO);
1648 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp);
1649 static int param_set_aaintbool(const char *val, const struct kernel_param *kp);
1650 #define param_check_aaintbool param_check_int
1651 static const struct kernel_param_ops param_ops_aaintbool = {
1652 .set = param_set_aaintbool,
1653 .get = param_get_aaintbool
1655 /* Boot time disable flag */
1656 static int apparmor_enabled __ro_after_init = 1;
1657 module_param_named(enabled, apparmor_enabled, aaintbool, 0444);
1659 static int __init apparmor_enabled_setup(char *str)
1661 unsigned long enabled;
1662 int error = kstrtoul(str, 0, &enabled);
1664 apparmor_enabled = enabled ? 1 : 0;
1668 __setup("apparmor=", apparmor_enabled_setup);
1670 /* set global flag turning off the ability to load policy */
1671 static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp)
1673 if (!apparmor_enabled)
1675 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1677 return param_set_bool(val, kp);
1680 static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp)
1682 if (!apparmor_enabled)
1684 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1686 return param_get_bool(buffer, kp);
1689 static int param_set_aabool(const char *val, const struct kernel_param *kp)
1691 if (!apparmor_enabled)
1693 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1695 return param_set_bool(val, kp);
1698 static int param_get_aabool(char *buffer, const struct kernel_param *kp)
1700 if (!apparmor_enabled)
1702 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1704 return param_get_bool(buffer, kp);
1707 static int param_set_aauint(const char *val, const struct kernel_param *kp)
1711 if (!apparmor_enabled)
1713 /* file is ro but enforce 2nd line check */
1714 if (apparmor_initialized)
1717 error = param_set_uint(val, kp);
1718 aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer));
1719 pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max);
1724 static int param_get_aauint(char *buffer, const struct kernel_param *kp)
1726 if (!apparmor_enabled)
1728 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1730 return param_get_uint(buffer, kp);
1733 /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */
1734 static int param_set_aaintbool(const char *val, const struct kernel_param *kp)
1736 struct kernel_param kp_local;
1740 if (apparmor_initialized)
1743 /* Create local copy, with arg pointing to bool type. */
1744 value = !!*((int *)kp->arg);
1745 memcpy(&kp_local, kp, sizeof(kp_local));
1746 kp_local.arg = &value;
1748 error = param_set_bool(val, &kp_local);
1750 *((int *)kp->arg) = *((bool *)kp_local.arg);
1755 * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to
1756 * 1/0, this converts the "int that is actually bool" back to bool for
1757 * display in the /sys filesystem, while keeping it "int" for the LSM
1760 static int param_get_aaintbool(char *buffer, const struct kernel_param *kp)
1762 struct kernel_param kp_local;
1765 /* Create local copy, with arg pointing to bool type. */
1766 value = !!*((int *)kp->arg);
1767 memcpy(&kp_local, kp, sizeof(kp_local));
1768 kp_local.arg = &value;
1770 return param_get_bool(buffer, &kp_local);
1773 static int param_set_aacompressionlevel(const char *val,
1774 const struct kernel_param *kp)
1778 if (!apparmor_enabled)
1780 if (apparmor_initialized)
1783 error = param_set_int(val, kp);
1785 aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level,
1786 AA_MIN_CLEVEL, AA_MAX_CLEVEL);
1787 pr_info("AppArmor: policy rawdata compression level set to %d\n",
1788 aa_g_rawdata_compression_level);
1793 static int param_get_aacompressionlevel(char *buffer,
1794 const struct kernel_param *kp)
1796 if (!apparmor_enabled)
1798 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1800 return param_get_int(buffer, kp);
1803 static int param_get_audit(char *buffer, const struct kernel_param *kp)
1805 if (!apparmor_enabled)
1807 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1809 return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]);
1812 static int param_set_audit(const char *val, const struct kernel_param *kp)
1816 if (!apparmor_enabled)
1820 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1823 i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val);
1831 static int param_get_mode(char *buffer, const struct kernel_param *kp)
1833 if (!apparmor_enabled)
1835 if (apparmor_initialized && !aa_current_policy_view_capable(NULL))
1838 return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]);
1841 static int param_set_mode(const char *val, const struct kernel_param *kp)
1845 if (!apparmor_enabled)
1849 if (apparmor_initialized && !aa_current_policy_admin_capable(NULL))
1852 i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX,
1857 aa_g_profile_mode = i;
1861 char *aa_get_buffer(bool in_atomic)
1863 union aa_buffer *aa_buf;
1864 struct aa_local_cache *cache;
1865 bool try_again = true;
1866 gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
1868 /* use per cpu cached buffers first */
1869 cache = get_cpu_ptr(&aa_local_buffers);
1870 if (!list_empty(&cache->head)) {
1871 aa_buf = list_first_entry(&cache->head, union aa_buffer, list);
1872 list_del(&aa_buf->list);
1875 put_cpu_ptr(&aa_local_buffers);
1876 return &aa_buf->buffer[0];
1878 put_cpu_ptr(&aa_local_buffers);
1880 if (!spin_trylock(&aa_buffers_lock)) {
1881 cache = get_cpu_ptr(&aa_local_buffers);
1883 put_cpu_ptr(&aa_local_buffers);
1884 spin_lock(&aa_buffers_lock);
1886 cache = get_cpu_ptr(&aa_local_buffers);
1887 put_cpu_ptr(&aa_local_buffers);
1890 if (buffer_count > reserve_count ||
1891 (in_atomic && !list_empty(&aa_global_buffers))) {
1892 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1894 list_del(&aa_buf->list);
1896 spin_unlock(&aa_buffers_lock);
1897 return aa_buf->buffer;
1901 * out of reserve buffers and in atomic context so increase
1902 * how many buffers to keep in reserve
1907 spin_unlock(&aa_buffers_lock);
1911 aa_buf = kmalloc(aa_g_path_max, flags);
1915 spin_lock(&aa_buffers_lock);
1918 pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n");
1921 return aa_buf->buffer;
1924 void aa_put_buffer(char *buf)
1926 union aa_buffer *aa_buf;
1927 struct aa_local_cache *cache;
1931 aa_buf = container_of(buf, union aa_buffer, buffer[0]);
1933 cache = get_cpu_ptr(&aa_local_buffers);
1935 put_cpu_ptr(&aa_local_buffers);
1937 if (spin_trylock(&aa_buffers_lock)) {
1938 /* put back on global list */
1939 list_add(&aa_buf->list, &aa_global_buffers);
1941 spin_unlock(&aa_buffers_lock);
1942 cache = get_cpu_ptr(&aa_local_buffers);
1943 put_cpu_ptr(&aa_local_buffers);
1946 /* contention on global list, fallback to percpu */
1947 cache = get_cpu_ptr(&aa_local_buffers);
1951 /* cache in percpu list */
1952 list_add(&aa_buf->list, &cache->head);
1954 put_cpu_ptr(&aa_local_buffers);
1958 * AppArmor init functions
1962 * set_init_ctx - set a task context and profile on the first task.
1964 * TODO: allow setting an alternate profile than unconfined
1966 static int __init set_init_ctx(void)
1968 struct cred *cred = (__force struct cred *)current->real_cred;
1970 set_cred_label(cred, aa_get_label(ns_unconfined(root_ns)));
1975 static void destroy_buffers(void)
1977 union aa_buffer *aa_buf;
1979 spin_lock(&aa_buffers_lock);
1980 while (!list_empty(&aa_global_buffers)) {
1981 aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer,
1983 list_del(&aa_buf->list);
1984 spin_unlock(&aa_buffers_lock);
1986 spin_lock(&aa_buffers_lock);
1988 spin_unlock(&aa_buffers_lock);
1991 static int __init alloc_buffers(void)
1993 union aa_buffer *aa_buf;
1997 * per cpu set of cached allocated buffers used to help reduce
2000 for_each_possible_cpu(i) {
2001 per_cpu(aa_local_buffers, i).hold = 0;
2002 per_cpu(aa_local_buffers, i).count = 0;
2003 INIT_LIST_HEAD(&per_cpu(aa_local_buffers, i).head);
2006 * A function may require two buffers at once. Usually the buffers are
2007 * used for a short period of time and are shared. On UP kernel buffers
2008 * two should be enough, with more CPUs it is possible that more
2009 * buffers will be used simultaneously. The preallocated pool may grow.
2010 * This preallocation has also the side-effect that AppArmor will be
2011 * disabled early at boot if aa_g_path_max is extremly high.
2013 if (num_online_cpus() > 1)
2014 num = 4 + RESERVE_COUNT;
2016 num = 2 + RESERVE_COUNT;
2018 for (i = 0; i < num; i++) {
2020 aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL |
2021 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
2026 aa_put_buffer(aa_buf->buffer);
2031 #ifdef CONFIG_SYSCTL
2032 static int apparmor_dointvec(struct ctl_table *table, int write,
2033 void *buffer, size_t *lenp, loff_t *ppos)
2035 if (!aa_current_policy_admin_capable(NULL))
2037 if (!apparmor_enabled)
2040 return proc_dointvec(table, write, buffer, lenp, ppos);
2043 static struct ctl_table apparmor_sysctl_table[] = {
2044 #ifdef CONFIG_USER_NS
2046 .procname = "unprivileged_userns_apparmor_policy",
2047 .data = &unprivileged_userns_apparmor_policy,
2048 .maxlen = sizeof(int),
2050 .proc_handler = apparmor_dointvec,
2052 #endif /* CONFIG_USER_NS */
2054 .procname = "apparmor_display_secid_mode",
2055 .data = &apparmor_display_secid_mode,
2056 .maxlen = sizeof(int),
2058 .proc_handler = apparmor_dointvec,
2061 .procname = "apparmor_restrict_unprivileged_unconfined",
2062 .data = &aa_unprivileged_unconfined_restricted,
2063 .maxlen = sizeof(int),
2065 .proc_handler = apparmor_dointvec,
2070 static int __init apparmor_init_sysctl(void)
2072 return register_sysctl("kernel", apparmor_sysctl_table) ? 0 : -ENOMEM;
2075 static inline int apparmor_init_sysctl(void)
2079 #endif /* CONFIG_SYSCTL */
2081 #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK)
2082 static unsigned int apparmor_ip_postroute(void *priv,
2083 struct sk_buff *skb,
2084 const struct nf_hook_state *state)
2086 struct aa_sk_ctx *ctx;
2092 sk = skb_to_full_sk(skb);
2097 if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND,
2101 return NF_DROP_ERR(-ECONNREFUSED);
2105 static const struct nf_hook_ops apparmor_nf_ops[] = {
2107 .hook = apparmor_ip_postroute,
2109 .hooknum = NF_INET_POST_ROUTING,
2110 .priority = NF_IP_PRI_SELINUX_FIRST,
2112 #if IS_ENABLED(CONFIG_IPV6)
2114 .hook = apparmor_ip_postroute,
2116 .hooknum = NF_INET_POST_ROUTING,
2117 .priority = NF_IP6_PRI_SELINUX_FIRST,
2122 static int __net_init apparmor_nf_register(struct net *net)
2124 return nf_register_net_hooks(net, apparmor_nf_ops,
2125 ARRAY_SIZE(apparmor_nf_ops));
2128 static void __net_exit apparmor_nf_unregister(struct net *net)
2130 nf_unregister_net_hooks(net, apparmor_nf_ops,
2131 ARRAY_SIZE(apparmor_nf_ops));
2134 static struct pernet_operations apparmor_net_ops = {
2135 .init = apparmor_nf_register,
2136 .exit = apparmor_nf_unregister,
2139 static int __init apparmor_nf_ip_init(void)
2143 if (!apparmor_enabled)
2146 err = register_pernet_subsys(&apparmor_net_ops);
2148 panic("Apparmor: register_pernet_subsys: error %d\n", err);
2152 __initcall(apparmor_nf_ip_init);
2155 static char nulldfa_src[] = {
2156 #include "nulldfa.in"
2158 static struct aa_dfa *nulldfa;
2160 static char stacksplitdfa_src[] = {
2161 #include "stacksplitdfa.in"
2163 struct aa_dfa *stacksplitdfa;
2164 struct aa_policydb *nullpdb;
2166 static int __init aa_setup_dfa_engine(void)
2168 int error = -ENOMEM;
2170 nullpdb = aa_alloc_pdb(GFP_KERNEL);
2174 nulldfa = aa_dfa_unpack(nulldfa_src, sizeof(nulldfa_src),
2175 TO_ACCEPT1_FLAG(YYTD_DATA32) |
2176 TO_ACCEPT2_FLAG(YYTD_DATA32));
2177 if (IS_ERR(nulldfa)) {
2178 error = PTR_ERR(nulldfa);
2181 nullpdb->dfa = aa_get_dfa(nulldfa);
2182 nullpdb->perms = kcalloc(2, sizeof(struct aa_perms), GFP_KERNEL);
2183 if (!nullpdb->perms)
2187 stacksplitdfa = aa_dfa_unpack(stacksplitdfa_src,
2188 sizeof(stacksplitdfa_src),
2189 TO_ACCEPT1_FLAG(YYTD_DATA32) |
2190 TO_ACCEPT2_FLAG(YYTD_DATA32));
2191 if (IS_ERR(stacksplitdfa)) {
2192 error = PTR_ERR(stacksplitdfa);
2199 aa_put_pdb(nullpdb);
2200 aa_put_dfa(nulldfa);
2203 stacksplitdfa = NULL;
2208 static void __init aa_teardown_dfa_engine(void)
2210 aa_put_dfa(stacksplitdfa);
2211 aa_put_dfa(nulldfa);
2212 aa_put_pdb(nullpdb);
2214 stacksplitdfa = NULL;
2218 static int __init apparmor_init(void)
2222 error = aa_setup_dfa_engine();
2224 AA_ERROR("Unable to setup dfa engine\n");
2228 error = aa_alloc_root_ns();
2230 AA_ERROR("Unable to allocate default profile namespace\n");
2234 error = apparmor_init_sysctl();
2236 AA_ERROR("Unable to register sysctls\n");
2241 error = alloc_buffers();
2243 AA_ERROR("Unable to allocate work buffers\n");
2247 error = set_init_ctx();
2249 AA_ERROR("Failed to set context on init task\n");
2253 security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks),
2256 /* Report that AppArmor successfully initialized */
2257 apparmor_initialized = 1;
2258 if (aa_g_profile_mode == APPARMOR_COMPLAIN)
2259 aa_info_message("AppArmor initialized: complain mode enabled");
2260 else if (aa_g_profile_mode == APPARMOR_KILL)
2261 aa_info_message("AppArmor initialized: kill mode enabled");
2263 aa_info_message("AppArmor initialized");
2271 aa_teardown_dfa_engine();
2273 apparmor_enabled = false;
2277 DEFINE_LSM(apparmor) = {
2279 .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE,
2280 .enabled = &apparmor_enabled,
2281 .blobs = &apparmor_blob_sizes,
2282 .init = apparmor_init,