1b886d83cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2b5e95b48SJohn Johansen /* 3b5e95b48SJohn Johansen * AppArmor security module 4b5e95b48SJohn Johansen * 5b5e95b48SJohn Johansen * This file contains AppArmor LSM hooks. 6b5e95b48SJohn Johansen * 7b5e95b48SJohn Johansen * Copyright (C) 1998-2008 Novell/SUSE 8b5e95b48SJohn Johansen * Copyright 2009-2010 Canonical Ltd. 9b5e95b48SJohn Johansen */ 10b5e95b48SJohn Johansen 113c4ed7bdSCasey Schaufler #include <linux/lsm_hooks.h> 12b5e95b48SJohn Johansen #include <linux/moduleparam.h> 13b5e95b48SJohn Johansen #include <linux/mm.h> 14b5e95b48SJohn Johansen #include <linux/mman.h> 15b5e95b48SJohn Johansen #include <linux/mount.h> 16b5e95b48SJohn Johansen #include <linux/namei.h> 17b5e95b48SJohn Johansen #include <linux/ptrace.h> 18b5e95b48SJohn Johansen #include <linux/ctype.h> 19b5e95b48SJohn Johansen #include <linux/sysctl.h> 20b5e95b48SJohn Johansen #include <linux/audit.h> 213486740aSSerge E. Hallyn #include <linux/user_namespace.h> 22ab9f2115SMatthew Garrett #include <linux/netfilter_ipv4.h> 23ab9f2115SMatthew Garrett #include <linux/netfilter_ipv6.h> 2463c16c3aSChris Coulson #include <linux/zlib.h> 25b5e95b48SJohn Johansen #include <net/sock.h> 26e262e32dSDavid Howells #include <uapi/linux/mount.h> 27b5e95b48SJohn Johansen 28b5e95b48SJohn Johansen #include "include/apparmor.h" 29b5e95b48SJohn Johansen #include "include/apparmorfs.h" 30b5e95b48SJohn Johansen #include "include/audit.h" 31b5e95b48SJohn Johansen #include "include/capability.h" 32d8889d49SJohn Johansen #include "include/cred.h" 33b5e95b48SJohn Johansen #include "include/file.h" 34b5e95b48SJohn Johansen #include "include/ipc.h" 3556974a6fSJohn Johansen #include "include/net.h" 36b5e95b48SJohn Johansen #include "include/path.h" 37637f688dSJohn Johansen #include "include/label.h" 38b5e95b48SJohn Johansen #include "include/policy.h" 39cff281f6SJohn Johansen #include "include/policy_ns.h" 40b5e95b48SJohn Johansen #include "include/procattr.h" 412ea3ffb7SJohn Johansen #include "include/mount.h" 42c0929212SJohn Johansen #include "include/secid.h" 43b5e95b48SJohn Johansen 44b5e95b48SJohn Johansen /* Flag indicating whether initialization completed */ 45545de8feSJohn Johansen int apparmor_initialized; 46b5e95b48SJohn Johansen 47df323337SSebastian Andrzej Siewior union aa_buffer { 48df323337SSebastian Andrzej Siewior struct list_head list; 49df323337SSebastian Andrzej Siewior char buffer[1]; 50df323337SSebastian Andrzej Siewior }; 51d4669f0bSJohn Johansen 52341c1fdaSJohn Johansen #define RESERVE_COUNT 2 53341c1fdaSJohn Johansen static int reserve_count = RESERVE_COUNT; 54341c1fdaSJohn Johansen static int buffer_count; 55341c1fdaSJohn Johansen 56df323337SSebastian Andrzej Siewior static LIST_HEAD(aa_global_buffers); 57df323337SSebastian Andrzej Siewior static DEFINE_SPINLOCK(aa_buffers_lock); 58d4669f0bSJohn Johansen 59b5e95b48SJohn Johansen /* 60b5e95b48SJohn Johansen * LSM hook functions 61b5e95b48SJohn Johansen */ 62b5e95b48SJohn Johansen 63b5e95b48SJohn Johansen /* 64d9087c49SJohn Johansen * put the associated labels 65b5e95b48SJohn Johansen */ 66b5e95b48SJohn Johansen static void apparmor_cred_free(struct cred *cred) 67b5e95b48SJohn Johansen { 68d9087c49SJohn Johansen aa_put_label(cred_label(cred)); 6969b5a44aSCasey Schaufler set_cred_label(cred, NULL); 70b5e95b48SJohn Johansen } 71b5e95b48SJohn Johansen 72b5e95b48SJohn Johansen /* 73b5e95b48SJohn Johansen * allocate the apparmor part of blank credentials 74b5e95b48SJohn Johansen */ 75b5e95b48SJohn Johansen static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) 76b5e95b48SJohn Johansen { 7769b5a44aSCasey Schaufler set_cred_label(cred, NULL); 78b5e95b48SJohn Johansen return 0; 79b5e95b48SJohn Johansen } 80b5e95b48SJohn Johansen 81b5e95b48SJohn Johansen /* 82d9087c49SJohn Johansen * prepare new cred label for modification by prepare_cred block 83b5e95b48SJohn Johansen */ 84b5e95b48SJohn Johansen static int apparmor_cred_prepare(struct cred *new, const struct cred *old, 85b5e95b48SJohn Johansen gfp_t gfp) 86b5e95b48SJohn Johansen { 8769b5a44aSCasey Schaufler set_cred_label(new, aa_get_newest_label(cred_label(old))); 88b5e95b48SJohn Johansen return 0; 89b5e95b48SJohn Johansen } 90b5e95b48SJohn Johansen 91b5e95b48SJohn Johansen /* 92b5e95b48SJohn Johansen * transfer the apparmor data to a blank set of creds 93b5e95b48SJohn Johansen */ 94b5e95b48SJohn Johansen static void apparmor_cred_transfer(struct cred *new, const struct cred *old) 95b5e95b48SJohn Johansen { 9669b5a44aSCasey Schaufler set_cred_label(new, aa_get_newest_label(cred_label(old))); 97b5e95b48SJohn Johansen } 98b5e95b48SJohn Johansen 993b529a76SJohn Johansen static void apparmor_task_free(struct task_struct *task) 1003b529a76SJohn Johansen { 1013b529a76SJohn Johansen 1023b529a76SJohn Johansen aa_free_task_ctx(task_ctx(task)); 1033b529a76SJohn Johansen } 1043b529a76SJohn Johansen 1053b529a76SJohn Johansen static int apparmor_task_alloc(struct task_struct *task, 1063b529a76SJohn Johansen unsigned long clone_flags) 1073b529a76SJohn Johansen { 108f4ad8f2cSCasey Schaufler struct aa_task_ctx *new = task_ctx(task); 1093b529a76SJohn Johansen 110de62de59SJohn Johansen aa_dup_task_ctx(new, task_ctx(current)); 1113b529a76SJohn Johansen 1123b529a76SJohn Johansen return 0; 113b5e95b48SJohn Johansen } 114b5e95b48SJohn Johansen 115b5e95b48SJohn Johansen static int apparmor_ptrace_access_check(struct task_struct *child, 116b5e95b48SJohn Johansen unsigned int mode) 117b5e95b48SJohn Johansen { 118b2d09ae4SJohn Johansen struct aa_label *tracer, *tracee; 119b2d09ae4SJohn Johansen int error; 120b2d09ae4SJohn Johansen 1211f8266ffSJann Horn tracer = __begin_current_label_crit_section(); 122b2d09ae4SJohn Johansen tracee = aa_get_task_label(child); 123b2d09ae4SJohn Johansen error = aa_may_ptrace(tracer, tracee, 124338d0be4SJohn Johansen (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ 125338d0be4SJohn Johansen : AA_PTRACE_TRACE); 126b2d09ae4SJohn Johansen aa_put_label(tracee); 1271f8266ffSJann Horn __end_current_label_crit_section(tracer); 128b2d09ae4SJohn Johansen 129b2d09ae4SJohn Johansen return error; 130b5e95b48SJohn Johansen } 131b5e95b48SJohn Johansen 132b5e95b48SJohn Johansen static int apparmor_ptrace_traceme(struct task_struct *parent) 133b5e95b48SJohn Johansen { 134b2d09ae4SJohn Johansen struct aa_label *tracer, *tracee; 135b2d09ae4SJohn Johansen int error; 136b2d09ae4SJohn Johansen 137ca3fde52SJann Horn tracee = __begin_current_label_crit_section(); 138b2d09ae4SJohn Johansen tracer = aa_get_task_label(parent); 139b2d09ae4SJohn Johansen error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE); 140b2d09ae4SJohn Johansen aa_put_label(tracer); 141ca3fde52SJann Horn __end_current_label_crit_section(tracee); 142b2d09ae4SJohn Johansen 143b2d09ae4SJohn Johansen return error; 144b5e95b48SJohn Johansen } 145b5e95b48SJohn Johansen 146b5e95b48SJohn Johansen /* Derived from security/commoncap.c:cap_capget */ 147b5e95b48SJohn Johansen static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective, 148b5e95b48SJohn Johansen kernel_cap_t *inheritable, kernel_cap_t *permitted) 149b5e95b48SJohn Johansen { 150637f688dSJohn Johansen struct aa_label *label; 151b5e95b48SJohn Johansen const struct cred *cred; 152b5e95b48SJohn Johansen 153b5e95b48SJohn Johansen rcu_read_lock(); 154b5e95b48SJohn Johansen cred = __task_cred(target); 155637f688dSJohn Johansen label = aa_get_newest_cred_label(cred); 156c70c86c4SJohn Johansen 157b1d9e6b0SCasey Schaufler /* 158b1d9e6b0SCasey Schaufler * cap_capget is stacked ahead of this and will 159b1d9e6b0SCasey Schaufler * initialize effective and permitted. 160b1d9e6b0SCasey Schaufler */ 161c70c86c4SJohn Johansen if (!unconfined(label)) { 162c70c86c4SJohn Johansen struct aa_profile *profile; 163c70c86c4SJohn Johansen struct label_it i; 164c70c86c4SJohn Johansen 165c70c86c4SJohn Johansen label_for_each_confined(i, label, profile) { 166c70c86c4SJohn Johansen if (COMPLAIN_MODE(profile)) 167c70c86c4SJohn Johansen continue; 168c70c86c4SJohn Johansen *effective = cap_intersect(*effective, 169c70c86c4SJohn Johansen profile->caps.allow); 170c70c86c4SJohn Johansen *permitted = cap_intersect(*permitted, 171c70c86c4SJohn Johansen profile->caps.allow); 172c70c86c4SJohn Johansen } 173b5e95b48SJohn Johansen } 174b5e95b48SJohn Johansen rcu_read_unlock(); 175637f688dSJohn Johansen aa_put_label(label); 176b5e95b48SJohn Johansen 177b5e95b48SJohn Johansen return 0; 178b5e95b48SJohn Johansen } 179b5e95b48SJohn Johansen 1806a9de491SEric Paris static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, 181c1a85a00SMicah Morton int cap, unsigned int opts) 182b5e95b48SJohn Johansen { 183637f688dSJohn Johansen struct aa_label *label; 184b1d9e6b0SCasey Schaufler int error = 0; 185b1d9e6b0SCasey Schaufler 186637f688dSJohn Johansen label = aa_get_newest_cred_label(cred); 187637f688dSJohn Johansen if (!unconfined(label)) 188c1a85a00SMicah Morton error = aa_capable(label, cap, opts); 189637f688dSJohn Johansen aa_put_label(label); 190cf797c0eSJohn Johansen 191b5e95b48SJohn Johansen return error; 192b5e95b48SJohn Johansen } 193b5e95b48SJohn Johansen 194b5e95b48SJohn Johansen /** 195b5e95b48SJohn Johansen * common_perm - basic common permission check wrapper fn for paths 196b5e95b48SJohn Johansen * @op: operation being checked 197b5e95b48SJohn Johansen * @path: path to check permission of (NOT NULL) 198b5e95b48SJohn Johansen * @mask: requested permissions mask 199b5e95b48SJohn Johansen * @cond: conditional info for the permission request (NOT NULL) 200b5e95b48SJohn Johansen * 201b5e95b48SJohn Johansen * Returns: %0 else error code if error or permission denied 202b5e95b48SJohn Johansen */ 20347f6e5ccSJohn Johansen static int common_perm(const char *op, const struct path *path, u32 mask, 204b5e95b48SJohn Johansen struct path_cond *cond) 205b5e95b48SJohn Johansen { 206637f688dSJohn Johansen struct aa_label *label; 207b5e95b48SJohn Johansen int error = 0; 208b5e95b48SJohn Johansen 209637f688dSJohn Johansen label = __begin_current_label_crit_section(); 210637f688dSJohn Johansen if (!unconfined(label)) 211aebd873eSJohn Johansen error = aa_path_perm(op, label, path, 0, mask, cond); 212637f688dSJohn Johansen __end_current_label_crit_section(label); 213b5e95b48SJohn Johansen 214b5e95b48SJohn Johansen return error; 215b5e95b48SJohn Johansen } 216b5e95b48SJohn Johansen 217b5e95b48SJohn Johansen /** 21831f75bfeSJohn Johansen * common_perm_cond - common permission wrapper around inode cond 21931f75bfeSJohn Johansen * @op: operation being checked 22031f75bfeSJohn Johansen * @path: location to check (NOT NULL) 22131f75bfeSJohn Johansen * @mask: requested permissions mask 22231f75bfeSJohn Johansen * 22331f75bfeSJohn Johansen * Returns: %0 else error code if error or permission denied 22431f75bfeSJohn Johansen */ 22531f75bfeSJohn Johansen static int common_perm_cond(const char *op, const struct path *path, u32 mask) 22631f75bfeSJohn Johansen { 2273cee6079SChristian Brauner struct user_namespace *mnt_userns = mnt_user_ns(path->mnt); 2283cee6079SChristian Brauner struct path_cond cond = { 2293cee6079SChristian Brauner i_uid_into_mnt(mnt_userns, d_backing_inode(path->dentry)), 23031f75bfeSJohn Johansen d_backing_inode(path->dentry)->i_mode 23131f75bfeSJohn Johansen }; 23231f75bfeSJohn Johansen 23331f75bfeSJohn Johansen if (!path_mediated_fs(path->dentry)) 23431f75bfeSJohn Johansen return 0; 23531f75bfeSJohn Johansen 23631f75bfeSJohn Johansen return common_perm(op, path, mask, &cond); 23731f75bfeSJohn Johansen } 23831f75bfeSJohn Johansen 23931f75bfeSJohn Johansen /** 240b5e95b48SJohn Johansen * common_perm_dir_dentry - common permission wrapper when path is dir, dentry 241b5e95b48SJohn Johansen * @op: operation being checked 242b5e95b48SJohn Johansen * @dir: directory of the dentry (NOT NULL) 243b5e95b48SJohn Johansen * @dentry: dentry to check (NOT NULL) 244b5e95b48SJohn Johansen * @mask: requested permissions mask 245b5e95b48SJohn Johansen * @cond: conditional info for the permission request (NOT NULL) 246b5e95b48SJohn Johansen * 247b5e95b48SJohn Johansen * Returns: %0 else error code if error or permission denied 248b5e95b48SJohn Johansen */ 24947f6e5ccSJohn Johansen static int common_perm_dir_dentry(const char *op, const struct path *dir, 250b5e95b48SJohn Johansen struct dentry *dentry, u32 mask, 251b5e95b48SJohn Johansen struct path_cond *cond) 252b5e95b48SJohn Johansen { 2538486adf0SKees Cook struct path path = { .mnt = dir->mnt, .dentry = dentry }; 254b5e95b48SJohn Johansen 255b5e95b48SJohn Johansen return common_perm(op, &path, mask, cond); 256b5e95b48SJohn Johansen } 257b5e95b48SJohn Johansen 258b5e95b48SJohn Johansen /** 259b5e95b48SJohn Johansen * common_perm_rm - common permission wrapper for operations doing rm 260b5e95b48SJohn Johansen * @op: operation being checked 261b5e95b48SJohn Johansen * @dir: directory that the dentry is in (NOT NULL) 262b5e95b48SJohn Johansen * @dentry: dentry being rm'd (NOT NULL) 263b5e95b48SJohn Johansen * @mask: requested permission mask 264b5e95b48SJohn Johansen * 265b5e95b48SJohn Johansen * Returns: %0 else error code if error or permission denied 266b5e95b48SJohn Johansen */ 26747f6e5ccSJohn Johansen static int common_perm_rm(const char *op, const struct path *dir, 268b5e95b48SJohn Johansen struct dentry *dentry, u32 mask) 269b5e95b48SJohn Johansen { 270c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 2713cee6079SChristian Brauner struct user_namespace *mnt_userns = mnt_user_ns(dir->mnt); 272b5e95b48SJohn Johansen struct path_cond cond = { }; 273b5e95b48SJohn Johansen 274efeee83aSJohn Johansen if (!inode || !path_mediated_fs(dentry)) 275b5e95b48SJohn Johansen return 0; 276b5e95b48SJohn Johansen 2773cee6079SChristian Brauner cond.uid = i_uid_into_mnt(mnt_userns, inode); 278b5e95b48SJohn Johansen cond.mode = inode->i_mode; 279b5e95b48SJohn Johansen 280b5e95b48SJohn Johansen return common_perm_dir_dentry(op, dir, dentry, mask, &cond); 281b5e95b48SJohn Johansen } 282b5e95b48SJohn Johansen 283b5e95b48SJohn Johansen /** 284b5e95b48SJohn Johansen * common_perm_create - common permission wrapper for operations doing create 285b5e95b48SJohn Johansen * @op: operation being checked 286b5e95b48SJohn Johansen * @dir: directory that dentry will be created in (NOT NULL) 287b5e95b48SJohn Johansen * @dentry: dentry to create (NOT NULL) 288b5e95b48SJohn Johansen * @mask: request permission mask 289b5e95b48SJohn Johansen * @mode: created file mode 290b5e95b48SJohn Johansen * 291b5e95b48SJohn Johansen * Returns: %0 else error code if error or permission denied 292b5e95b48SJohn Johansen */ 29347f6e5ccSJohn Johansen static int common_perm_create(const char *op, const struct path *dir, 294d6b49f7aSAl Viro struct dentry *dentry, u32 mask, umode_t mode) 295b5e95b48SJohn Johansen { 296b5e95b48SJohn Johansen struct path_cond cond = { current_fsuid(), mode }; 297b5e95b48SJohn Johansen 298efeee83aSJohn Johansen if (!path_mediated_fs(dir->dentry)) 299b5e95b48SJohn Johansen return 0; 300b5e95b48SJohn Johansen 301b5e95b48SJohn Johansen return common_perm_dir_dentry(op, dir, dentry, mask, &cond); 302b5e95b48SJohn Johansen } 303b5e95b48SJohn Johansen 304989f74e0SAl Viro static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry) 305b5e95b48SJohn Johansen { 306b5e95b48SJohn Johansen return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE); 307b5e95b48SJohn Johansen } 308b5e95b48SJohn Johansen 309d3607752SAl Viro static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry, 3104572befeSAl Viro umode_t mode) 311b5e95b48SJohn Johansen { 312b5e95b48SJohn Johansen return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE, 313b5e95b48SJohn Johansen S_IFDIR); 314b5e95b48SJohn Johansen } 315b5e95b48SJohn Johansen 316989f74e0SAl Viro static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry) 317b5e95b48SJohn Johansen { 318b5e95b48SJohn Johansen return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE); 319b5e95b48SJohn Johansen } 320b5e95b48SJohn Johansen 321d3607752SAl Viro static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry, 32204fc66e7SAl Viro umode_t mode, unsigned int dev) 323b5e95b48SJohn Johansen { 324b5e95b48SJohn Johansen return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode); 325b5e95b48SJohn Johansen } 326b5e95b48SJohn Johansen 32781f4c506SAl Viro static int apparmor_path_truncate(const struct path *path) 328b5e95b48SJohn Johansen { 329e53cfe6cSJohn Johansen return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); 330b5e95b48SJohn Johansen } 331b5e95b48SJohn Johansen 332d3607752SAl Viro static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, 333b5e95b48SJohn Johansen const char *old_name) 334b5e95b48SJohn Johansen { 335b5e95b48SJohn Johansen return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE, 336b5e95b48SJohn Johansen S_IFLNK); 337b5e95b48SJohn Johansen } 338b5e95b48SJohn Johansen 3393ccee46aSAl Viro static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir, 340b5e95b48SJohn Johansen struct dentry *new_dentry) 341b5e95b48SJohn Johansen { 342637f688dSJohn Johansen struct aa_label *label; 343b5e95b48SJohn Johansen int error = 0; 344b5e95b48SJohn Johansen 345efeee83aSJohn Johansen if (!path_mediated_fs(old_dentry)) 346b5e95b48SJohn Johansen return 0; 347b5e95b48SJohn Johansen 348637f688dSJohn Johansen label = begin_current_label_crit_section(); 349637f688dSJohn Johansen if (!unconfined(label)) 3508014370fSJohn Johansen error = aa_path_link(label, old_dentry, new_dir, new_dentry); 351637f688dSJohn Johansen end_current_label_crit_section(label); 352cf797c0eSJohn Johansen 353b5e95b48SJohn Johansen return error; 354b5e95b48SJohn Johansen } 355b5e95b48SJohn Johansen 3563ccee46aSAl Viro static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, 3573ccee46aSAl Viro const struct path *new_dir, struct dentry *new_dentry) 358b5e95b48SJohn Johansen { 359637f688dSJohn Johansen struct aa_label *label; 360b5e95b48SJohn Johansen int error = 0; 361b5e95b48SJohn Johansen 362efeee83aSJohn Johansen if (!path_mediated_fs(old_dentry)) 363b5e95b48SJohn Johansen return 0; 364b5e95b48SJohn Johansen 365637f688dSJohn Johansen label = begin_current_label_crit_section(); 366637f688dSJohn Johansen if (!unconfined(label)) { 3673cee6079SChristian Brauner struct user_namespace *mnt_userns = mnt_user_ns(old_dir->mnt); 3688486adf0SKees Cook struct path old_path = { .mnt = old_dir->mnt, 3698486adf0SKees Cook .dentry = old_dentry }; 3708486adf0SKees Cook struct path new_path = { .mnt = new_dir->mnt, 3718486adf0SKees Cook .dentry = new_dentry }; 3723cee6079SChristian Brauner struct path_cond cond = { 3733cee6079SChristian Brauner i_uid_into_mnt(mnt_userns, d_backing_inode(old_dentry)), 374c6f493d6SDavid Howells d_backing_inode(old_dentry)->i_mode 375b5e95b48SJohn Johansen }; 376b5e95b48SJohn Johansen 377aebd873eSJohn Johansen error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, 378e53cfe6cSJohn Johansen MAY_READ | AA_MAY_GETATTR | MAY_WRITE | 379e53cfe6cSJohn Johansen AA_MAY_SETATTR | AA_MAY_DELETE, 380b5e95b48SJohn Johansen &cond); 381b5e95b48SJohn Johansen if (!error) 382aebd873eSJohn Johansen error = aa_path_perm(OP_RENAME_DEST, label, &new_path, 383e53cfe6cSJohn Johansen 0, MAY_WRITE | AA_MAY_SETATTR | 384b5e95b48SJohn Johansen AA_MAY_CREATE, &cond); 385b5e95b48SJohn Johansen 386b5e95b48SJohn Johansen } 387637f688dSJohn Johansen end_current_label_crit_section(label); 388cf797c0eSJohn Johansen 389b5e95b48SJohn Johansen return error; 390b5e95b48SJohn Johansen } 391b5e95b48SJohn Johansen 392be01f9f2SAl Viro static int apparmor_path_chmod(const struct path *path, umode_t mode) 393b5e95b48SJohn Johansen { 39431f75bfeSJohn Johansen return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD); 395b5e95b48SJohn Johansen } 396b5e95b48SJohn Johansen 3977fd25dacSAl Viro static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid) 398b5e95b48SJohn Johansen { 39931f75bfeSJohn Johansen return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN); 400b5e95b48SJohn Johansen } 401b5e95b48SJohn Johansen 4023f7036a0SAl Viro static int apparmor_inode_getattr(const struct path *path) 403b5e95b48SJohn Johansen { 404e53cfe6cSJohn Johansen return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR); 405b5e95b48SJohn Johansen } 406b5e95b48SJohn Johansen 40794817692SAl Viro static int apparmor_file_open(struct file *file) 408b5e95b48SJohn Johansen { 409637f688dSJohn Johansen struct aa_file_ctx *fctx = file_ctx(file); 410637f688dSJohn Johansen struct aa_label *label; 411b5e95b48SJohn Johansen int error = 0; 412b5e95b48SJohn Johansen 413efeee83aSJohn Johansen if (!path_mediated_fs(file->f_path.dentry)) 414b5e95b48SJohn Johansen return 0; 415b5e95b48SJohn Johansen 416b5e95b48SJohn Johansen /* If in exec, permission is handled by bprm hooks. 417b5e95b48SJohn Johansen * Cache permissions granted by the previous exec check, with 418b5e95b48SJohn Johansen * implicit read and executable mmap which are required to 419b5e95b48SJohn Johansen * actually execute the image. 420b5e95b48SJohn Johansen */ 421b5e95b48SJohn Johansen if (current->in_execve) { 42255a26ebfSJohn Johansen fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP; 423b5e95b48SJohn Johansen return 0; 424b5e95b48SJohn Johansen } 425b5e95b48SJohn Johansen 42694817692SAl Viro label = aa_get_newest_cred_label(file->f_cred); 427637f688dSJohn Johansen if (!unconfined(label)) { 4283cee6079SChristian Brauner struct user_namespace *mnt_userns = file_mnt_user_ns(file); 429496ad9aaSAl Viro struct inode *inode = file_inode(file); 4303cee6079SChristian Brauner struct path_cond cond = { 4313cee6079SChristian Brauner i_uid_into_mnt(mnt_userns, inode), 4323cee6079SChristian Brauner inode->i_mode 4333cee6079SChristian Brauner }; 434b5e95b48SJohn Johansen 435aebd873eSJohn Johansen error = aa_path_perm(OP_OPEN, label, &file->f_path, 0, 436b5e95b48SJohn Johansen aa_map_file_to_perms(file), &cond); 437b5e95b48SJohn Johansen /* todo cache full allowed permissions set and state */ 43855a26ebfSJohn Johansen fctx->allow = aa_map_file_to_perms(file); 439b5e95b48SJohn Johansen } 440637f688dSJohn Johansen aa_put_label(label); 441b5e95b48SJohn Johansen 442b5e95b48SJohn Johansen return error; 443b5e95b48SJohn Johansen } 444b5e95b48SJohn Johansen 445b5e95b48SJohn Johansen static int apparmor_file_alloc_security(struct file *file) 446b5e95b48SJohn Johansen { 44733bf60caSCasey Schaufler struct aa_file_ctx *ctx = file_ctx(file); 448637f688dSJohn Johansen struct aa_label *label = begin_current_label_crit_section(); 449b5e95b48SJohn Johansen 45033bf60caSCasey Schaufler spin_lock_init(&ctx->lock); 45133bf60caSCasey Schaufler rcu_assign_pointer(ctx->label, aa_get_label(label)); 45233bf60caSCasey Schaufler end_current_label_crit_section(label); 45333bf60caSCasey Schaufler return 0; 454b5e95b48SJohn Johansen } 455b5e95b48SJohn Johansen 456b5e95b48SJohn Johansen static void apparmor_file_free_security(struct file *file) 457b5e95b48SJohn Johansen { 45833bf60caSCasey Schaufler struct aa_file_ctx *ctx = file_ctx(file); 45933bf60caSCasey Schaufler 46033bf60caSCasey Schaufler if (ctx) 46133bf60caSCasey Schaufler aa_put_label(rcu_access_pointer(ctx->label)); 462b5e95b48SJohn Johansen } 463b5e95b48SJohn Johansen 464341c1fdaSJohn Johansen static int common_file_perm(const char *op, struct file *file, u32 mask, 465341c1fdaSJohn Johansen bool in_atomic) 466b5e95b48SJohn Johansen { 467190a9518SJohn Johansen struct aa_label *label; 468b5e95b48SJohn Johansen int error = 0; 469b5e95b48SJohn Johansen 470192ca6b5SJohn Johansen /* don't reaudit files closed during inheritance */ 471192ca6b5SJohn Johansen if (file->f_path.dentry == aa_null.dentry) 472192ca6b5SJohn Johansen return -EACCES; 473192ca6b5SJohn Johansen 474637f688dSJohn Johansen label = __begin_current_label_crit_section(); 475341c1fdaSJohn Johansen error = aa_file_perm(op, label, file, mask, in_atomic); 476637f688dSJohn Johansen __end_current_label_crit_section(label); 477b5e95b48SJohn Johansen 478b5e95b48SJohn Johansen return error; 479b5e95b48SJohn Johansen } 480b5e95b48SJohn Johansen 481064dc947SJohn Johansen static int apparmor_file_receive(struct file *file) 482064dc947SJohn Johansen { 483341c1fdaSJohn Johansen return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file), 484341c1fdaSJohn Johansen false); 485064dc947SJohn Johansen } 486064dc947SJohn Johansen 487b5e95b48SJohn Johansen static int apparmor_file_permission(struct file *file, int mask) 488b5e95b48SJohn Johansen { 489341c1fdaSJohn Johansen return common_file_perm(OP_FPERM, file, mask, false); 490b5e95b48SJohn Johansen } 491b5e95b48SJohn Johansen 492b5e95b48SJohn Johansen static int apparmor_file_lock(struct file *file, unsigned int cmd) 493b5e95b48SJohn Johansen { 494b5e95b48SJohn Johansen u32 mask = AA_MAY_LOCK; 495b5e95b48SJohn Johansen 496b5e95b48SJohn Johansen if (cmd == F_WRLCK) 497b5e95b48SJohn Johansen mask |= MAY_WRITE; 498b5e95b48SJohn Johansen 499341c1fdaSJohn Johansen return common_file_perm(OP_FLOCK, file, mask, false); 500b5e95b48SJohn Johansen } 501b5e95b48SJohn Johansen 50247f6e5ccSJohn Johansen static int common_mmap(const char *op, struct file *file, unsigned long prot, 503341c1fdaSJohn Johansen unsigned long flags, bool in_atomic) 504b5e95b48SJohn Johansen { 505b5e95b48SJohn Johansen int mask = 0; 506b5e95b48SJohn Johansen 507637f688dSJohn Johansen if (!file || !file_ctx(file)) 508b5e95b48SJohn Johansen return 0; 509b5e95b48SJohn Johansen 510b5e95b48SJohn Johansen if (prot & PROT_READ) 511b5e95b48SJohn Johansen mask |= MAY_READ; 512b5e95b48SJohn Johansen /* 513b5e95b48SJohn Johansen * Private mappings don't require write perms since they don't 514b5e95b48SJohn Johansen * write back to the files 515b5e95b48SJohn Johansen */ 516b5e95b48SJohn Johansen if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE)) 517b5e95b48SJohn Johansen mask |= MAY_WRITE; 518b5e95b48SJohn Johansen if (prot & PROT_EXEC) 519b5e95b48SJohn Johansen mask |= AA_EXEC_MMAP; 520b5e95b48SJohn Johansen 521341c1fdaSJohn Johansen return common_file_perm(op, file, mask, in_atomic); 522b5e95b48SJohn Johansen } 523b5e95b48SJohn Johansen 524e5467859SAl Viro static int apparmor_mmap_file(struct file *file, unsigned long reqprot, 525e5467859SAl Viro unsigned long prot, unsigned long flags) 526b5e95b48SJohn Johansen { 527341c1fdaSJohn Johansen return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC); 528b5e95b48SJohn Johansen } 529b5e95b48SJohn Johansen 530b5e95b48SJohn Johansen static int apparmor_file_mprotect(struct vm_area_struct *vma, 531b5e95b48SJohn Johansen unsigned long reqprot, unsigned long prot) 532b5e95b48SJohn Johansen { 533b5e95b48SJohn Johansen return common_mmap(OP_FMPROT, vma->vm_file, prot, 534341c1fdaSJohn Johansen !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0, 535341c1fdaSJohn Johansen false); 536b5e95b48SJohn Johansen } 537b5e95b48SJohn Johansen 5382ea3ffb7SJohn Johansen static int apparmor_sb_mount(const char *dev_name, const struct path *path, 5392ea3ffb7SJohn Johansen const char *type, unsigned long flags, void *data) 5402ea3ffb7SJohn Johansen { 5412ea3ffb7SJohn Johansen struct aa_label *label; 5422ea3ffb7SJohn Johansen int error = 0; 5432ea3ffb7SJohn Johansen 5442ea3ffb7SJohn Johansen /* Discard magic */ 5452ea3ffb7SJohn Johansen if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 5462ea3ffb7SJohn Johansen flags &= ~MS_MGC_MSK; 5472ea3ffb7SJohn Johansen 5482ea3ffb7SJohn Johansen flags &= ~AA_MS_IGNORE_MASK; 5492ea3ffb7SJohn Johansen 5502ea3ffb7SJohn Johansen label = __begin_current_label_crit_section(); 5512ea3ffb7SJohn Johansen if (!unconfined(label)) { 5522ea3ffb7SJohn Johansen if (flags & MS_REMOUNT) 5532ea3ffb7SJohn Johansen error = aa_remount(label, path, flags, data); 5542ea3ffb7SJohn Johansen else if (flags & MS_BIND) 5552ea3ffb7SJohn Johansen error = aa_bind_mount(label, path, dev_name, flags); 5562ea3ffb7SJohn Johansen else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | 5572ea3ffb7SJohn Johansen MS_UNBINDABLE)) 5582ea3ffb7SJohn Johansen error = aa_mount_change_type(label, path, flags); 5592ea3ffb7SJohn Johansen else if (flags & MS_MOVE) 5602ea3ffb7SJohn Johansen error = aa_move_mount(label, path, dev_name); 5612ea3ffb7SJohn Johansen else 5622ea3ffb7SJohn Johansen error = aa_new_mount(label, dev_name, path, type, 5632ea3ffb7SJohn Johansen flags, data); 5642ea3ffb7SJohn Johansen } 5652ea3ffb7SJohn Johansen __end_current_label_crit_section(label); 5662ea3ffb7SJohn Johansen 5672ea3ffb7SJohn Johansen return error; 5682ea3ffb7SJohn Johansen } 5692ea3ffb7SJohn Johansen 5702ea3ffb7SJohn Johansen static int apparmor_sb_umount(struct vfsmount *mnt, int flags) 5712ea3ffb7SJohn Johansen { 5722ea3ffb7SJohn Johansen struct aa_label *label; 5732ea3ffb7SJohn Johansen int error = 0; 5742ea3ffb7SJohn Johansen 5752ea3ffb7SJohn Johansen label = __begin_current_label_crit_section(); 5762ea3ffb7SJohn Johansen if (!unconfined(label)) 5772ea3ffb7SJohn Johansen error = aa_umount(label, mnt, flags); 5782ea3ffb7SJohn Johansen __end_current_label_crit_section(label); 5792ea3ffb7SJohn Johansen 5802ea3ffb7SJohn Johansen return error; 5812ea3ffb7SJohn Johansen } 5822ea3ffb7SJohn Johansen 5832ea3ffb7SJohn Johansen static int apparmor_sb_pivotroot(const struct path *old_path, 5842ea3ffb7SJohn Johansen const struct path *new_path) 5852ea3ffb7SJohn Johansen { 5862ea3ffb7SJohn Johansen struct aa_label *label; 5872ea3ffb7SJohn Johansen int error = 0; 5882ea3ffb7SJohn Johansen 5892ea3ffb7SJohn Johansen label = aa_get_current_label(); 5902ea3ffb7SJohn Johansen if (!unconfined(label)) 5912ea3ffb7SJohn Johansen error = aa_pivotroot(label, old_path, new_path); 5922ea3ffb7SJohn Johansen aa_put_label(label); 5932ea3ffb7SJohn Johansen 5942ea3ffb7SJohn Johansen return error; 5952ea3ffb7SJohn Johansen } 5962ea3ffb7SJohn Johansen 597b5e95b48SJohn Johansen static int apparmor_getprocattr(struct task_struct *task, char *name, 598b5e95b48SJohn Johansen char **value) 599b5e95b48SJohn Johansen { 600b5e95b48SJohn Johansen int error = -ENOENT; 601b5e95b48SJohn Johansen /* released below */ 602b5e95b48SJohn Johansen const struct cred *cred = get_task_cred(task); 603de62de59SJohn Johansen struct aa_task_ctx *ctx = task_ctx(current); 604637f688dSJohn Johansen struct aa_label *label = NULL; 605b5e95b48SJohn Johansen 606b5e95b48SJohn Johansen if (strcmp(name, "current") == 0) 607d9087c49SJohn Johansen label = aa_get_newest_label(cred_label(cred)); 60855a26ebfSJohn Johansen else if (strcmp(name, "prev") == 0 && ctx->previous) 609637f688dSJohn Johansen label = aa_get_newest_label(ctx->previous); 61055a26ebfSJohn Johansen else if (strcmp(name, "exec") == 0 && ctx->onexec) 611637f688dSJohn Johansen label = aa_get_newest_label(ctx->onexec); 612b5e95b48SJohn Johansen else 613b5e95b48SJohn Johansen error = -EINVAL; 614b5e95b48SJohn Johansen 615637f688dSJohn Johansen if (label) 61676a1d263SJohn Johansen error = aa_getprocattr(label, value); 61777b071b3SJohn Johansen 618637f688dSJohn Johansen aa_put_label(label); 619b5e95b48SJohn Johansen put_cred(cred); 620b5e95b48SJohn Johansen 621b5e95b48SJohn Johansen return error; 622b5e95b48SJohn Johansen } 623b5e95b48SJohn Johansen 624b21507e2SStephen Smalley static int apparmor_setprocattr(const char *name, void *value, 625b21507e2SStephen Smalley size_t size) 626b5e95b48SJohn Johansen { 627e89b8081SVegard Nossum char *command, *largs = NULL, *args = value; 628b5e95b48SJohn Johansen size_t arg_size; 629b5e95b48SJohn Johansen int error; 630ef88a7acSJohn Johansen DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR); 631b5e95b48SJohn Johansen 632b5e95b48SJohn Johansen if (size == 0) 633b5e95b48SJohn Johansen return -EINVAL; 634b5e95b48SJohn Johansen 635e89b8081SVegard Nossum /* AppArmor requires that the buffer must be null terminated atm */ 636e89b8081SVegard Nossum if (args[size - 1] != '\0') { 637e89b8081SVegard Nossum /* null terminate */ 638e89b8081SVegard Nossum largs = args = kmalloc(size + 1, GFP_KERNEL); 639e89b8081SVegard Nossum if (!args) 640e89b8081SVegard Nossum return -ENOMEM; 641e89b8081SVegard Nossum memcpy(args, value, size); 642e89b8081SVegard Nossum args[size] = '\0'; 643e89b8081SVegard Nossum } 644e89b8081SVegard Nossum 645e89b8081SVegard Nossum error = -EINVAL; 646b5e95b48SJohn Johansen args = strim(args); 647b5e95b48SJohn Johansen command = strsep(&args, " "); 648b5e95b48SJohn Johansen if (!args) 649e89b8081SVegard Nossum goto out; 650b5e95b48SJohn Johansen args = skip_spaces(args); 651b5e95b48SJohn Johansen if (!*args) 652e89b8081SVegard Nossum goto out; 653b5e95b48SJohn Johansen 654d4d03f74SJohn Johansen arg_size = size - (args - (largs ? largs : (char *) value)); 655b5e95b48SJohn Johansen if (strcmp(name, "current") == 0) { 656b5e95b48SJohn Johansen if (strcmp(command, "changehat") == 0) { 657b5e95b48SJohn Johansen error = aa_setprocattr_changehat(args, arg_size, 658df8073c6SJohn Johansen AA_CHANGE_NOFLAGS); 659b5e95b48SJohn Johansen } else if (strcmp(command, "permhat") == 0) { 660b5e95b48SJohn Johansen error = aa_setprocattr_changehat(args, arg_size, 661df8073c6SJohn Johansen AA_CHANGE_TEST); 662b5e95b48SJohn Johansen } else if (strcmp(command, "changeprofile") == 0) { 663df8073c6SJohn Johansen error = aa_change_profile(args, AA_CHANGE_NOFLAGS); 664b5e95b48SJohn Johansen } else if (strcmp(command, "permprofile") == 0) { 665df8073c6SJohn Johansen error = aa_change_profile(args, AA_CHANGE_TEST); 6666c5fc8f1SJohn Johansen } else if (strcmp(command, "stack") == 0) { 6676c5fc8f1SJohn Johansen error = aa_change_profile(args, AA_CHANGE_STACK); 6683eea57c2SJohn Johansen } else 6693eea57c2SJohn Johansen goto fail; 670b5e95b48SJohn Johansen } else if (strcmp(name, "exec") == 0) { 6713eea57c2SJohn Johansen if (strcmp(command, "exec") == 0) 672df8073c6SJohn Johansen error = aa_change_profile(args, AA_CHANGE_ONEXEC); 6736c5fc8f1SJohn Johansen else if (strcmp(command, "stack") == 0) 6746c5fc8f1SJohn Johansen error = aa_change_profile(args, (AA_CHANGE_ONEXEC | 6756c5fc8f1SJohn Johansen AA_CHANGE_STACK)); 6763eea57c2SJohn Johansen else 6773eea57c2SJohn Johansen goto fail; 6783eea57c2SJohn Johansen } else 679b5e95b48SJohn Johansen /* only support the "current" and "exec" process attributes */ 680e89b8081SVegard Nossum goto fail; 6813eea57c2SJohn Johansen 682b5e95b48SJohn Johansen if (!error) 683b5e95b48SJohn Johansen error = size; 684e89b8081SVegard Nossum out: 685e89b8081SVegard Nossum kfree(largs); 686b5e95b48SJohn Johansen return error; 6873eea57c2SJohn Johansen 6883eea57c2SJohn Johansen fail: 689637f688dSJohn Johansen aad(&sa)->label = begin_current_label_crit_section(); 690ef88a7acSJohn Johansen aad(&sa)->info = name; 691ef88a7acSJohn Johansen aad(&sa)->error = error = -EINVAL; 6923eea57c2SJohn Johansen aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL); 693637f688dSJohn Johansen end_current_label_crit_section(aad(&sa)->label); 694e89b8081SVegard Nossum goto out; 695b5e95b48SJohn Johansen } 696b5e95b48SJohn Johansen 697fe864821SJohn Johansen /** 698fe864821SJohn Johansen * apparmor_bprm_committing_creds - do task cleanup on committing new creds 699fe864821SJohn Johansen * @bprm: binprm for the exec (NOT NULL) 700fe864821SJohn Johansen */ 701fe864821SJohn Johansen static void apparmor_bprm_committing_creds(struct linux_binprm *bprm) 702fe864821SJohn Johansen { 703637f688dSJohn Johansen struct aa_label *label = aa_current_raw_label(); 704d9087c49SJohn Johansen struct aa_label *new_label = cred_label(bprm->cred); 705fe864821SJohn Johansen 706fe864821SJohn Johansen /* bail out if unconfined or not changing profile */ 707d9087c49SJohn Johansen if ((new_label->proxy == label->proxy) || 708d9087c49SJohn Johansen (unconfined(new_label))) 709fe864821SJohn Johansen return; 710fe864821SJohn Johansen 711192ca6b5SJohn Johansen aa_inherit_files(bprm->cred, current->files); 712192ca6b5SJohn Johansen 713fe864821SJohn Johansen current->pdeath_signal = 0; 714fe864821SJohn Johansen 715637f688dSJohn Johansen /* reset soft limits and set hard limits for the new label */ 716d9087c49SJohn Johansen __aa_transition_rlimits(label, new_label); 717fe864821SJohn Johansen } 718fe864821SJohn Johansen 719fe864821SJohn Johansen /** 720fe864821SJohn Johansen * apparmor_bprm_committed_cred - do cleanup after new creds committed 721fe864821SJohn Johansen * @bprm: binprm for the exec (NOT NULL) 722fe864821SJohn Johansen */ 723fe864821SJohn Johansen static void apparmor_bprm_committed_creds(struct linux_binprm *bprm) 724fe864821SJohn Johansen { 7253b529a76SJohn Johansen /* clear out temporary/transitional state from the context */ 726de62de59SJohn Johansen aa_clear_task_ctx_trans(task_ctx(current)); 7273b529a76SJohn Johansen 728fe864821SJohn Johansen return; 729fe864821SJohn Johansen } 730fe864821SJohn Johansen 731*6326948fSPaul Moore static void apparmor_current_getsecid_subj(u32 *secid) 732*6326948fSPaul Moore { 733*6326948fSPaul Moore struct aa_label *label = aa_get_current_label(); 734*6326948fSPaul Moore *secid = label->secid; 735*6326948fSPaul Moore aa_put_label(label); 736*6326948fSPaul Moore } 737*6326948fSPaul Moore 738*6326948fSPaul Moore static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid) 739a7ae3645SJohn Johansen { 740a7ae3645SJohn Johansen struct aa_label *label = aa_get_task_label(p); 741a7ae3645SJohn Johansen *secid = label->secid; 742a7ae3645SJohn Johansen aa_put_label(label); 743a7ae3645SJohn Johansen } 744a7ae3645SJohn Johansen 7457cb4dc9fSJiri Slaby static int apparmor_task_setrlimit(struct task_struct *task, 7467cb4dc9fSJiri Slaby unsigned int resource, struct rlimit *new_rlim) 747b5e95b48SJohn Johansen { 748637f688dSJohn Johansen struct aa_label *label = __begin_current_label_crit_section(); 749b5e95b48SJohn Johansen int error = 0; 750b5e95b48SJohn Johansen 751637f688dSJohn Johansen if (!unconfined(label)) 75286b92cb7SJohn Johansen error = aa_task_setrlimit(label, task, resource, new_rlim); 753637f688dSJohn Johansen __end_current_label_crit_section(label); 754b5e95b48SJohn Johansen 755b5e95b48SJohn Johansen return error; 756b5e95b48SJohn Johansen } 757b5e95b48SJohn Johansen 758ae7795bcSEric W. Biederman static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info, 7596b4f3d01SStephen Smalley int sig, const struct cred *cred) 760cd1dbf76SJohn Johansen { 761cd1dbf76SJohn Johansen struct aa_label *cl, *tl; 762cd1dbf76SJohn Johansen int error; 763cd1dbf76SJohn Johansen 7646b4f3d01SStephen Smalley if (cred) { 7656b4f3d01SStephen Smalley /* 766cd1dbf76SJohn Johansen * Dealing with USB IO specific behavior 767cd1dbf76SJohn Johansen */ 7686b4f3d01SStephen Smalley cl = aa_get_newest_cred_label(cred); 7696b4f3d01SStephen Smalley tl = aa_get_task_label(target); 7706b4f3d01SStephen Smalley error = aa_may_signal(cl, tl, sig); 7716b4f3d01SStephen Smalley aa_put_label(cl); 7726b4f3d01SStephen Smalley aa_put_label(tl); 7736b4f3d01SStephen Smalley return error; 7746b4f3d01SStephen Smalley } 7756b4f3d01SStephen Smalley 776cd1dbf76SJohn Johansen cl = __begin_current_label_crit_section(); 777cd1dbf76SJohn Johansen tl = aa_get_task_label(target); 778cd1dbf76SJohn Johansen error = aa_may_signal(cl, tl, sig); 779cd1dbf76SJohn Johansen aa_put_label(tl); 780cd1dbf76SJohn Johansen __end_current_label_crit_section(cl); 781cd1dbf76SJohn Johansen 782cd1dbf76SJohn Johansen return error; 783cd1dbf76SJohn Johansen } 784cd1dbf76SJohn Johansen 78556974a6fSJohn Johansen /** 78656974a6fSJohn Johansen * apparmor_sk_alloc_security - allocate and attach the sk_security field 78756974a6fSJohn Johansen */ 78856974a6fSJohn Johansen static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags) 78956974a6fSJohn Johansen { 79056974a6fSJohn Johansen struct aa_sk_ctx *ctx; 79156974a6fSJohn Johansen 79256974a6fSJohn Johansen ctx = kzalloc(sizeof(*ctx), flags); 79356974a6fSJohn Johansen if (!ctx) 79456974a6fSJohn Johansen return -ENOMEM; 79556974a6fSJohn Johansen 79656974a6fSJohn Johansen SK_CTX(sk) = ctx; 79756974a6fSJohn Johansen 79856974a6fSJohn Johansen return 0; 79956974a6fSJohn Johansen } 80056974a6fSJohn Johansen 80156974a6fSJohn Johansen /** 80256974a6fSJohn Johansen * apparmor_sk_free_security - free the sk_security field 80356974a6fSJohn Johansen */ 80456974a6fSJohn Johansen static void apparmor_sk_free_security(struct sock *sk) 80556974a6fSJohn Johansen { 80656974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sk); 80756974a6fSJohn Johansen 80856974a6fSJohn Johansen SK_CTX(sk) = NULL; 80956974a6fSJohn Johansen aa_put_label(ctx->label); 81056974a6fSJohn Johansen aa_put_label(ctx->peer); 81156974a6fSJohn Johansen kfree(ctx); 81256974a6fSJohn Johansen } 81356974a6fSJohn Johansen 81456974a6fSJohn Johansen /** 81556974a6fSJohn Johansen * apparmor_clone_security - clone the sk_security field 81656974a6fSJohn Johansen */ 81756974a6fSJohn Johansen static void apparmor_sk_clone_security(const struct sock *sk, 81856974a6fSJohn Johansen struct sock *newsk) 81956974a6fSJohn Johansen { 82056974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sk); 82156974a6fSJohn Johansen struct aa_sk_ctx *new = SK_CTX(newsk); 82256974a6fSJohn Johansen 8233b646abcSMauricio Faria de Oliveira if (new->label) 8243b646abcSMauricio Faria de Oliveira aa_put_label(new->label); 82556974a6fSJohn Johansen new->label = aa_get_label(ctx->label); 8263b646abcSMauricio Faria de Oliveira 8273b646abcSMauricio Faria de Oliveira if (new->peer) 8283b646abcSMauricio Faria de Oliveira aa_put_label(new->peer); 82956974a6fSJohn Johansen new->peer = aa_get_label(ctx->peer); 83056974a6fSJohn Johansen } 83156974a6fSJohn Johansen 83256974a6fSJohn Johansen /** 83356974a6fSJohn Johansen * apparmor_socket_create - check perms before creating a new socket 83456974a6fSJohn Johansen */ 83556974a6fSJohn Johansen static int apparmor_socket_create(int family, int type, int protocol, int kern) 83656974a6fSJohn Johansen { 83756974a6fSJohn Johansen struct aa_label *label; 83856974a6fSJohn Johansen int error = 0; 83956974a6fSJohn Johansen 84056974a6fSJohn Johansen AA_BUG(in_interrupt()); 84156974a6fSJohn Johansen 84256974a6fSJohn Johansen label = begin_current_label_crit_section(); 84356974a6fSJohn Johansen if (!(kern || unconfined(label))) 84456974a6fSJohn Johansen error = af_select(family, 84556974a6fSJohn Johansen create_perm(label, family, type, protocol), 84656974a6fSJohn Johansen aa_af_perm(label, OP_CREATE, AA_MAY_CREATE, 84756974a6fSJohn Johansen family, type, protocol)); 84856974a6fSJohn Johansen end_current_label_crit_section(label); 84956974a6fSJohn Johansen 85056974a6fSJohn Johansen return error; 85156974a6fSJohn Johansen } 85256974a6fSJohn Johansen 85356974a6fSJohn Johansen /** 85456974a6fSJohn Johansen * apparmor_socket_post_create - setup the per-socket security struct 85556974a6fSJohn Johansen * 85656974a6fSJohn Johansen * Note: 85756974a6fSJohn Johansen * - kernel sockets currently labeled unconfined but we may want to 85856974a6fSJohn Johansen * move to a special kernel label 85956974a6fSJohn Johansen * - socket may not have sk here if created with sock_create_lite or 86056974a6fSJohn Johansen * sock_alloc. These should be accept cases which will be handled in 86156974a6fSJohn Johansen * sock_graft. 86256974a6fSJohn Johansen */ 86356974a6fSJohn Johansen static int apparmor_socket_post_create(struct socket *sock, int family, 86456974a6fSJohn Johansen int type, int protocol, int kern) 86556974a6fSJohn Johansen { 86656974a6fSJohn Johansen struct aa_label *label; 86756974a6fSJohn Johansen 86856974a6fSJohn Johansen if (kern) { 86956974a6fSJohn Johansen struct aa_ns *ns = aa_get_current_ns(); 87056974a6fSJohn Johansen 87156974a6fSJohn Johansen label = aa_get_label(ns_unconfined(ns)); 87256974a6fSJohn Johansen aa_put_ns(ns); 87356974a6fSJohn Johansen } else 87456974a6fSJohn Johansen label = aa_get_current_label(); 87556974a6fSJohn Johansen 87656974a6fSJohn Johansen if (sock->sk) { 87756974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sock->sk); 87856974a6fSJohn Johansen 87956974a6fSJohn Johansen aa_put_label(ctx->label); 88056974a6fSJohn Johansen ctx->label = aa_get_label(label); 88156974a6fSJohn Johansen } 88256974a6fSJohn Johansen aa_put_label(label); 88356974a6fSJohn Johansen 88456974a6fSJohn Johansen return 0; 88556974a6fSJohn Johansen } 88656974a6fSJohn Johansen 88756974a6fSJohn Johansen /** 88856974a6fSJohn Johansen * apparmor_socket_bind - check perms before bind addr to socket 88956974a6fSJohn Johansen */ 89056974a6fSJohn Johansen static int apparmor_socket_bind(struct socket *sock, 89156974a6fSJohn Johansen struct sockaddr *address, int addrlen) 89256974a6fSJohn Johansen { 89356974a6fSJohn Johansen AA_BUG(!sock); 89456974a6fSJohn Johansen AA_BUG(!sock->sk); 89556974a6fSJohn Johansen AA_BUG(!address); 89656974a6fSJohn Johansen AA_BUG(in_interrupt()); 89756974a6fSJohn Johansen 89856974a6fSJohn Johansen return af_select(sock->sk->sk_family, 89956974a6fSJohn Johansen bind_perm(sock, address, addrlen), 90056974a6fSJohn Johansen aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk)); 90156974a6fSJohn Johansen } 90256974a6fSJohn Johansen 90356974a6fSJohn Johansen /** 90456974a6fSJohn Johansen * apparmor_socket_connect - check perms before connecting @sock to @address 90556974a6fSJohn Johansen */ 90656974a6fSJohn Johansen static int apparmor_socket_connect(struct socket *sock, 90756974a6fSJohn Johansen struct sockaddr *address, int addrlen) 90856974a6fSJohn Johansen { 90956974a6fSJohn Johansen AA_BUG(!sock); 91056974a6fSJohn Johansen AA_BUG(!sock->sk); 91156974a6fSJohn Johansen AA_BUG(!address); 91256974a6fSJohn Johansen AA_BUG(in_interrupt()); 91356974a6fSJohn Johansen 91456974a6fSJohn Johansen return af_select(sock->sk->sk_family, 91556974a6fSJohn Johansen connect_perm(sock, address, addrlen), 91656974a6fSJohn Johansen aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk)); 91756974a6fSJohn Johansen } 91856974a6fSJohn Johansen 91956974a6fSJohn Johansen /** 92056974a6fSJohn Johansen * apparmor_socket_list - check perms before allowing listen 92156974a6fSJohn Johansen */ 92256974a6fSJohn Johansen static int apparmor_socket_listen(struct socket *sock, int backlog) 92356974a6fSJohn Johansen { 92456974a6fSJohn Johansen AA_BUG(!sock); 92556974a6fSJohn Johansen AA_BUG(!sock->sk); 92656974a6fSJohn Johansen AA_BUG(in_interrupt()); 92756974a6fSJohn Johansen 92856974a6fSJohn Johansen return af_select(sock->sk->sk_family, 92956974a6fSJohn Johansen listen_perm(sock, backlog), 93056974a6fSJohn Johansen aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk)); 93156974a6fSJohn Johansen } 93256974a6fSJohn Johansen 93356974a6fSJohn Johansen /** 93456974a6fSJohn Johansen * apparmor_socket_accept - check perms before accepting a new connection. 93556974a6fSJohn Johansen * 93656974a6fSJohn Johansen * Note: while @newsock is created and has some information, the accept 93756974a6fSJohn Johansen * has not been done. 93856974a6fSJohn Johansen */ 93956974a6fSJohn Johansen static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) 94056974a6fSJohn Johansen { 94156974a6fSJohn Johansen AA_BUG(!sock); 94256974a6fSJohn Johansen AA_BUG(!sock->sk); 94356974a6fSJohn Johansen AA_BUG(!newsock); 94456974a6fSJohn Johansen AA_BUG(in_interrupt()); 94556974a6fSJohn Johansen 94656974a6fSJohn Johansen return af_select(sock->sk->sk_family, 94756974a6fSJohn Johansen accept_perm(sock, newsock), 94856974a6fSJohn Johansen aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk)); 94956974a6fSJohn Johansen } 95056974a6fSJohn Johansen 95156974a6fSJohn Johansen static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, 95256974a6fSJohn Johansen struct msghdr *msg, int size) 95356974a6fSJohn Johansen { 95456974a6fSJohn Johansen AA_BUG(!sock); 95556974a6fSJohn Johansen AA_BUG(!sock->sk); 95656974a6fSJohn Johansen AA_BUG(!msg); 95756974a6fSJohn Johansen AA_BUG(in_interrupt()); 95856974a6fSJohn Johansen 95956974a6fSJohn Johansen return af_select(sock->sk->sk_family, 96056974a6fSJohn Johansen msg_perm(op, request, sock, msg, size), 96156974a6fSJohn Johansen aa_sk_perm(op, request, sock->sk)); 96256974a6fSJohn Johansen } 96356974a6fSJohn Johansen 96456974a6fSJohn Johansen /** 96556974a6fSJohn Johansen * apparmor_socket_sendmsg - check perms before sending msg to another socket 96656974a6fSJohn Johansen */ 96756974a6fSJohn Johansen static int apparmor_socket_sendmsg(struct socket *sock, 96856974a6fSJohn Johansen struct msghdr *msg, int size) 96956974a6fSJohn Johansen { 97056974a6fSJohn Johansen return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); 97156974a6fSJohn Johansen } 97256974a6fSJohn Johansen 97356974a6fSJohn Johansen /** 97456974a6fSJohn Johansen * apparmor_socket_recvmsg - check perms before receiving a message 97556974a6fSJohn Johansen */ 97656974a6fSJohn Johansen static int apparmor_socket_recvmsg(struct socket *sock, 97756974a6fSJohn Johansen struct msghdr *msg, int size, int flags) 97856974a6fSJohn Johansen { 97956974a6fSJohn Johansen return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); 98056974a6fSJohn Johansen } 98156974a6fSJohn Johansen 98256974a6fSJohn Johansen /* revaliation, get/set attr, shutdown */ 98356974a6fSJohn Johansen static int aa_sock_perm(const char *op, u32 request, struct socket *sock) 98456974a6fSJohn Johansen { 98556974a6fSJohn Johansen AA_BUG(!sock); 98656974a6fSJohn Johansen AA_BUG(!sock->sk); 98756974a6fSJohn Johansen AA_BUG(in_interrupt()); 98856974a6fSJohn Johansen 98956974a6fSJohn Johansen return af_select(sock->sk->sk_family, 99056974a6fSJohn Johansen sock_perm(op, request, sock), 99156974a6fSJohn Johansen aa_sk_perm(op, request, sock->sk)); 99256974a6fSJohn Johansen } 99356974a6fSJohn Johansen 99456974a6fSJohn Johansen /** 99556974a6fSJohn Johansen * apparmor_socket_getsockname - check perms before getting the local address 99656974a6fSJohn Johansen */ 99756974a6fSJohn Johansen static int apparmor_socket_getsockname(struct socket *sock) 99856974a6fSJohn Johansen { 99956974a6fSJohn Johansen return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); 100056974a6fSJohn Johansen } 100156974a6fSJohn Johansen 100256974a6fSJohn Johansen /** 100356974a6fSJohn Johansen * apparmor_socket_getpeername - check perms before getting remote address 100456974a6fSJohn Johansen */ 100556974a6fSJohn Johansen static int apparmor_socket_getpeername(struct socket *sock) 100656974a6fSJohn Johansen { 100756974a6fSJohn Johansen return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); 100856974a6fSJohn Johansen } 100956974a6fSJohn Johansen 101056974a6fSJohn Johansen /* revaliation, get/set attr, opt */ 101156974a6fSJohn Johansen static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, 101256974a6fSJohn Johansen int level, int optname) 101356974a6fSJohn Johansen { 101456974a6fSJohn Johansen AA_BUG(!sock); 101556974a6fSJohn Johansen AA_BUG(!sock->sk); 101656974a6fSJohn Johansen AA_BUG(in_interrupt()); 101756974a6fSJohn Johansen 101856974a6fSJohn Johansen return af_select(sock->sk->sk_family, 101956974a6fSJohn Johansen opt_perm(op, request, sock, level, optname), 102056974a6fSJohn Johansen aa_sk_perm(op, request, sock->sk)); 102156974a6fSJohn Johansen } 102256974a6fSJohn Johansen 102356974a6fSJohn Johansen /** 102456974a6fSJohn Johansen * apparmor_getsockopt - check perms before getting socket options 102556974a6fSJohn Johansen */ 102656974a6fSJohn Johansen static int apparmor_socket_getsockopt(struct socket *sock, int level, 102756974a6fSJohn Johansen int optname) 102856974a6fSJohn Johansen { 102956974a6fSJohn Johansen return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, 103056974a6fSJohn Johansen level, optname); 103156974a6fSJohn Johansen } 103256974a6fSJohn Johansen 103356974a6fSJohn Johansen /** 103456974a6fSJohn Johansen * apparmor_setsockopt - check perms before setting socket options 103556974a6fSJohn Johansen */ 103656974a6fSJohn Johansen static int apparmor_socket_setsockopt(struct socket *sock, int level, 103756974a6fSJohn Johansen int optname) 103856974a6fSJohn Johansen { 103956974a6fSJohn Johansen return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, 104056974a6fSJohn Johansen level, optname); 104156974a6fSJohn Johansen } 104256974a6fSJohn Johansen 104356974a6fSJohn Johansen /** 104456974a6fSJohn Johansen * apparmor_socket_shutdown - check perms before shutting down @sock conn 104556974a6fSJohn Johansen */ 104656974a6fSJohn Johansen static int apparmor_socket_shutdown(struct socket *sock, int how) 104756974a6fSJohn Johansen { 104856974a6fSJohn Johansen return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); 104956974a6fSJohn Johansen } 105056974a6fSJohn Johansen 1051e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK 105256974a6fSJohn Johansen /** 105356974a6fSJohn Johansen * apparmor_socket_sock_recv_skb - check perms before associating skb to sk 105456974a6fSJohn Johansen * 105556974a6fSJohn Johansen * Note: can not sleep may be called with locks held 105656974a6fSJohn Johansen * 105756974a6fSJohn Johansen * dont want protocol specific in __skb_recv_datagram() 105856974a6fSJohn Johansen * to deny an incoming connection socket_sock_rcv_skb() 105956974a6fSJohn Johansen */ 106056974a6fSJohn Johansen static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 106156974a6fSJohn Johansen { 1062ab9f2115SMatthew Garrett struct aa_sk_ctx *ctx = SK_CTX(sk); 1063ab9f2115SMatthew Garrett 1064ab9f2115SMatthew Garrett if (!skb->secmark) 106556974a6fSJohn Johansen return 0; 1066ab9f2115SMatthew Garrett 1067ab9f2115SMatthew Garrett return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, 1068ab9f2115SMatthew Garrett skb->secmark, sk); 106956974a6fSJohn Johansen } 1070e1af4779SArnd Bergmann #endif 107156974a6fSJohn Johansen 107256974a6fSJohn Johansen 107356974a6fSJohn Johansen static struct aa_label *sk_peer_label(struct sock *sk) 107456974a6fSJohn Johansen { 107556974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sk); 107656974a6fSJohn Johansen 107756974a6fSJohn Johansen if (ctx->peer) 107856974a6fSJohn Johansen return ctx->peer; 107956974a6fSJohn Johansen 108056974a6fSJohn Johansen return ERR_PTR(-ENOPROTOOPT); 108156974a6fSJohn Johansen } 108256974a6fSJohn Johansen 108356974a6fSJohn Johansen /** 108456974a6fSJohn Johansen * apparmor_socket_getpeersec_stream - get security context of peer 108556974a6fSJohn Johansen * 108656974a6fSJohn Johansen * Note: for tcp only valid if using ipsec or cipso on lan 108756974a6fSJohn Johansen */ 108856974a6fSJohn Johansen static int apparmor_socket_getpeersec_stream(struct socket *sock, 108956974a6fSJohn Johansen char __user *optval, 109056974a6fSJohn Johansen int __user *optlen, 109156974a6fSJohn Johansen unsigned int len) 109256974a6fSJohn Johansen { 109356974a6fSJohn Johansen char *name; 109456974a6fSJohn Johansen int slen, error = 0; 109556974a6fSJohn Johansen struct aa_label *label; 109656974a6fSJohn Johansen struct aa_label *peer; 109756974a6fSJohn Johansen 109856974a6fSJohn Johansen label = begin_current_label_crit_section(); 109956974a6fSJohn Johansen peer = sk_peer_label(sock->sk); 110056974a6fSJohn Johansen if (IS_ERR(peer)) { 110156974a6fSJohn Johansen error = PTR_ERR(peer); 110256974a6fSJohn Johansen goto done; 110356974a6fSJohn Johansen } 110456974a6fSJohn Johansen slen = aa_label_asxprint(&name, labels_ns(label), peer, 110556974a6fSJohn Johansen FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | 110656974a6fSJohn Johansen FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); 110756974a6fSJohn Johansen /* don't include terminating \0 in slen, it breaks some apps */ 110856974a6fSJohn Johansen if (slen < 0) { 110956974a6fSJohn Johansen error = -ENOMEM; 111056974a6fSJohn Johansen } else { 111156974a6fSJohn Johansen if (slen > len) { 111256974a6fSJohn Johansen error = -ERANGE; 111356974a6fSJohn Johansen } else if (copy_to_user(optval, name, slen)) { 111456974a6fSJohn Johansen error = -EFAULT; 111556974a6fSJohn Johansen goto out; 111656974a6fSJohn Johansen } 111756974a6fSJohn Johansen if (put_user(slen, optlen)) 111856974a6fSJohn Johansen error = -EFAULT; 111956974a6fSJohn Johansen out: 112056974a6fSJohn Johansen kfree(name); 112156974a6fSJohn Johansen 112256974a6fSJohn Johansen } 112356974a6fSJohn Johansen 112456974a6fSJohn Johansen done: 112556974a6fSJohn Johansen end_current_label_crit_section(label); 112656974a6fSJohn Johansen 112756974a6fSJohn Johansen return error; 112856974a6fSJohn Johansen } 112956974a6fSJohn Johansen 113056974a6fSJohn Johansen /** 113156974a6fSJohn Johansen * apparmor_socket_getpeersec_dgram - get security label of packet 113256974a6fSJohn Johansen * @sock: the peer socket 113356974a6fSJohn Johansen * @skb: packet data 113456974a6fSJohn Johansen * @secid: pointer to where to put the secid of the packet 113556974a6fSJohn Johansen * 113656974a6fSJohn Johansen * Sets the netlabel socket state on sk from parent 113756974a6fSJohn Johansen */ 113856974a6fSJohn Johansen static int apparmor_socket_getpeersec_dgram(struct socket *sock, 113956974a6fSJohn Johansen struct sk_buff *skb, u32 *secid) 114056974a6fSJohn Johansen 114156974a6fSJohn Johansen { 114256974a6fSJohn Johansen /* TODO: requires secid support */ 114356974a6fSJohn Johansen return -ENOPROTOOPT; 114456974a6fSJohn Johansen } 114556974a6fSJohn Johansen 114656974a6fSJohn Johansen /** 114756974a6fSJohn Johansen * apparmor_sock_graft - Initialize newly created socket 114856974a6fSJohn Johansen * @sk: child sock 114956974a6fSJohn Johansen * @parent: parent socket 115056974a6fSJohn Johansen * 115156974a6fSJohn Johansen * Note: could set off of SOCK_CTX(parent) but need to track inode and we can 115256974a6fSJohn Johansen * just set sk security information off of current creating process label 115356974a6fSJohn Johansen * Labeling of sk for accept case - probably should be sock based 115456974a6fSJohn Johansen * instead of task, because of the case where an implicitly labeled 115556974a6fSJohn Johansen * socket is shared by different tasks. 115656974a6fSJohn Johansen */ 115756974a6fSJohn Johansen static void apparmor_sock_graft(struct sock *sk, struct socket *parent) 115856974a6fSJohn Johansen { 115956974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sk); 116056974a6fSJohn Johansen 116156974a6fSJohn Johansen if (!ctx->label) 116256974a6fSJohn Johansen ctx->label = aa_get_current_label(); 116356974a6fSJohn Johansen } 116456974a6fSJohn Johansen 1165e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK 116641dd9596SFlorian Westphal static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb, 1167ab9f2115SMatthew Garrett struct request_sock *req) 1168ab9f2115SMatthew Garrett { 1169ab9f2115SMatthew Garrett struct aa_sk_ctx *ctx = SK_CTX(sk); 1170ab9f2115SMatthew Garrett 1171ab9f2115SMatthew Garrett if (!skb->secmark) 1172ab9f2115SMatthew Garrett return 0; 1173ab9f2115SMatthew Garrett 1174ab9f2115SMatthew Garrett return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, 1175ab9f2115SMatthew Garrett skb->secmark, sk); 1176ab9f2115SMatthew Garrett } 1177e1af4779SArnd Bergmann #endif 1178ab9f2115SMatthew Garrett 1179bbd3662aSCasey Schaufler /* 1180bbd3662aSCasey Schaufler * The cred blob is a pointer to, not an instance of, an aa_task_ctx. 1181bbd3662aSCasey Schaufler */ 1182bbd3662aSCasey Schaufler struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = { 1183bbd3662aSCasey Schaufler .lbs_cred = sizeof(struct aa_task_ctx *), 118433bf60caSCasey Schaufler .lbs_file = sizeof(struct aa_file_ctx), 1185f4ad8f2cSCasey Schaufler .lbs_task = sizeof(struct aa_task_ctx), 1186bbd3662aSCasey Schaufler }; 1187bbd3662aSCasey Schaufler 1188ca97d939SJames Morris static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { 1189e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), 1190e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), 1191e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, apparmor_capget), 1192e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, apparmor_capable), 1193b5e95b48SJohn Johansen 11942ea3ffb7SJohn Johansen LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), 11952ea3ffb7SJohn Johansen LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), 11962ea3ffb7SJohn Johansen LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), 11972ea3ffb7SJohn Johansen 1198e20b043aSCasey Schaufler LSM_HOOK_INIT(path_link, apparmor_path_link), 1199e20b043aSCasey Schaufler LSM_HOOK_INIT(path_unlink, apparmor_path_unlink), 1200e20b043aSCasey Schaufler LSM_HOOK_INIT(path_symlink, apparmor_path_symlink), 1201e20b043aSCasey Schaufler LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir), 1202e20b043aSCasey Schaufler LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir), 1203e20b043aSCasey Schaufler LSM_HOOK_INIT(path_mknod, apparmor_path_mknod), 1204e20b043aSCasey Schaufler LSM_HOOK_INIT(path_rename, apparmor_path_rename), 1205e20b043aSCasey Schaufler LSM_HOOK_INIT(path_chmod, apparmor_path_chmod), 1206e20b043aSCasey Schaufler LSM_HOOK_INIT(path_chown, apparmor_path_chown), 1207e20b043aSCasey Schaufler LSM_HOOK_INIT(path_truncate, apparmor_path_truncate), 1208e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr), 1209b5e95b48SJohn Johansen 1210e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, apparmor_file_open), 1211064dc947SJohn Johansen LSM_HOOK_INIT(file_receive, apparmor_file_receive), 1212e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, apparmor_file_permission), 1213e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), 1214e20b043aSCasey Schaufler LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), 1215e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), 1216e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), 1217e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, apparmor_file_lock), 1218b5e95b48SJohn Johansen 1219e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), 1220e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), 1221b5e95b48SJohn Johansen 122256974a6fSJohn Johansen LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security), 122356974a6fSJohn Johansen LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), 122456974a6fSJohn Johansen LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), 122556974a6fSJohn Johansen 122656974a6fSJohn Johansen LSM_HOOK_INIT(socket_create, apparmor_socket_create), 122756974a6fSJohn Johansen LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), 122856974a6fSJohn Johansen LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), 122956974a6fSJohn Johansen LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), 123056974a6fSJohn Johansen LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), 123156974a6fSJohn Johansen LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), 123256974a6fSJohn Johansen LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), 123356974a6fSJohn Johansen LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), 123456974a6fSJohn Johansen LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), 123556974a6fSJohn Johansen LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), 123656974a6fSJohn Johansen LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), 123756974a6fSJohn Johansen LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), 123856974a6fSJohn Johansen LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), 1239e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK 124056974a6fSJohn Johansen LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), 1241e1af4779SArnd Bergmann #endif 124256974a6fSJohn Johansen LSM_HOOK_INIT(socket_getpeersec_stream, 124356974a6fSJohn Johansen apparmor_socket_getpeersec_stream), 124456974a6fSJohn Johansen LSM_HOOK_INIT(socket_getpeersec_dgram, 124556974a6fSJohn Johansen apparmor_socket_getpeersec_dgram), 124656974a6fSJohn Johansen LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), 1247e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK 1248ab9f2115SMatthew Garrett LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), 1249e1af4779SArnd Bergmann #endif 125056974a6fSJohn Johansen 1251e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), 1252e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_free, apparmor_cred_free), 1253e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), 1254e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), 1255b5e95b48SJohn Johansen 1256b8bff599SEric W. Biederman LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec), 1257e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), 1258e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), 1259b5e95b48SJohn Johansen 12603b529a76SJohn Johansen LSM_HOOK_INIT(task_free, apparmor_task_free), 12613b529a76SJohn Johansen LSM_HOOK_INIT(task_alloc, apparmor_task_alloc), 1262*6326948fSPaul Moore LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj), 1263*6326948fSPaul Moore LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj), 1264e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit), 1265cd1dbf76SJohn Johansen LSM_HOOK_INIT(task_kill, apparmor_task_kill), 1266c0929212SJohn Johansen 1267e79c26d0SMatthew Garrett #ifdef CONFIG_AUDIT 1268e79c26d0SMatthew Garrett LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init), 1269e79c26d0SMatthew Garrett LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known), 1270e79c26d0SMatthew Garrett LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match), 1271e79c26d0SMatthew Garrett LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free), 1272e79c26d0SMatthew Garrett #endif 1273e79c26d0SMatthew Garrett 1274c0929212SJohn Johansen LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx), 1275c0929212SJohn Johansen LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid), 1276c0929212SJohn Johansen LSM_HOOK_INIT(release_secctx, apparmor_release_secctx), 1277b5e95b48SJohn Johansen }; 1278b5e95b48SJohn Johansen 1279b5e95b48SJohn Johansen /* 1280b5e95b48SJohn Johansen * AppArmor sysfs module parameters 1281b5e95b48SJohn Johansen */ 1282b5e95b48SJohn Johansen 1283101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp); 1284101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp); 1285b8aa09fdSRusty Russell #define param_check_aabool param_check_bool 12869c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aabool = { 12876a4c2643SJani Nikula .flags = KERNEL_PARAM_OPS_FL_NOARG, 1288101d6c82SStephen Rothwell .set = param_set_aabool, 1289101d6c82SStephen Rothwell .get = param_get_aabool 1290101d6c82SStephen Rothwell }; 1291b5e95b48SJohn Johansen 1292101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp); 1293101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp); 1294b8aa09fdSRusty Russell #define param_check_aauint param_check_uint 12959c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aauint = { 1296101d6c82SStephen Rothwell .set = param_set_aauint, 1297101d6c82SStephen Rothwell .get = param_get_aauint 1298101d6c82SStephen Rothwell }; 1299b5e95b48SJohn Johansen 130063c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val, 130163c16c3aSChris Coulson const struct kernel_param *kp); 130263c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer, 130363c16c3aSChris Coulson const struct kernel_param *kp); 130463c16c3aSChris Coulson #define param_check_aacompressionlevel param_check_int 130563c16c3aSChris Coulson static const struct kernel_param_ops param_ops_aacompressionlevel = { 130663c16c3aSChris Coulson .set = param_set_aacompressionlevel, 130763c16c3aSChris Coulson .get = param_get_aacompressionlevel 130863c16c3aSChris Coulson }; 130963c16c3aSChris Coulson 1310101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); 1311101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); 1312b8aa09fdSRusty Russell #define param_check_aalockpolicy param_check_bool 13139c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aalockpolicy = { 13146a4c2643SJani Nikula .flags = KERNEL_PARAM_OPS_FL_NOARG, 1315101d6c82SStephen Rothwell .set = param_set_aalockpolicy, 1316101d6c82SStephen Rothwell .get = param_get_aalockpolicy 1317101d6c82SStephen Rothwell }; 1318b5e95b48SJohn Johansen 1319e4dca7b7SKees Cook static int param_set_audit(const char *val, const struct kernel_param *kp); 1320e4dca7b7SKees Cook static int param_get_audit(char *buffer, const struct kernel_param *kp); 1321b5e95b48SJohn Johansen 1322e4dca7b7SKees Cook static int param_set_mode(const char *val, const struct kernel_param *kp); 1323e4dca7b7SKees Cook static int param_get_mode(char *buffer, const struct kernel_param *kp); 1324b5e95b48SJohn Johansen 1325b5e95b48SJohn Johansen /* Flag values, also controllable via /sys/module/apparmor/parameters 1326b5e95b48SJohn Johansen * We define special types as we want to do additional mediation. 1327b5e95b48SJohn Johansen */ 1328b5e95b48SJohn Johansen 1329b5e95b48SJohn Johansen /* AppArmor global enforcement switch - complain, enforce, kill */ 1330b5e95b48SJohn Johansen enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE; 1331b5e95b48SJohn Johansen module_param_call(mode, param_set_mode, param_get_mode, 1332b5e95b48SJohn Johansen &aa_g_profile_mode, S_IRUSR | S_IWUSR); 1333b5e95b48SJohn Johansen 13346059f71fSJohn Johansen /* whether policy verification hashing is enabled */ 13357616ac70SArnd Bergmann bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT); 13363ccb76c5SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_HASH 13376059f71fSJohn Johansen module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); 13387616ac70SArnd Bergmann #endif 13396059f71fSJohn Johansen 134063c16c3aSChris Coulson /* policy loaddata compression level */ 134163c16c3aSChris Coulson int aa_g_rawdata_compression_level = Z_DEFAULT_COMPRESSION; 134263c16c3aSChris Coulson module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level, 134363c16c3aSChris Coulson aacompressionlevel, 0400); 134463c16c3aSChris Coulson 1345b5e95b48SJohn Johansen /* Debug mode */ 1346eea7a05fSValentin Rothberg bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES); 1347b5e95b48SJohn Johansen module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR); 1348b5e95b48SJohn Johansen 1349b5e95b48SJohn Johansen /* Audit mode */ 1350b5e95b48SJohn Johansen enum audit_mode aa_g_audit; 1351b5e95b48SJohn Johansen module_param_call(audit, param_set_audit, param_get_audit, 1352b5e95b48SJohn Johansen &aa_g_audit, S_IRUSR | S_IWUSR); 1353b5e95b48SJohn Johansen 1354b5e95b48SJohn Johansen /* Determines if audit header is included in audited messages. This 1355b5e95b48SJohn Johansen * provides more context if the audit daemon is not running 1356b5e95b48SJohn Johansen */ 1357954317feSThomas Meyer bool aa_g_audit_header = true; 1358b5e95b48SJohn Johansen module_param_named(audit_header, aa_g_audit_header, aabool, 1359b5e95b48SJohn Johansen S_IRUSR | S_IWUSR); 1360b5e95b48SJohn Johansen 1361b5e95b48SJohn Johansen /* lock out loading/removal of policy 1362b5e95b48SJohn Johansen * TODO: add in at boot loading of policy, which is the only way to 1363b5e95b48SJohn Johansen * load policy, if lock_policy is set 1364b5e95b48SJohn Johansen */ 136590ab5ee9SRusty Russell bool aa_g_lock_policy; 1366b5e95b48SJohn Johansen module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy, 1367b5e95b48SJohn Johansen S_IRUSR | S_IWUSR); 1368b5e95b48SJohn Johansen 1369b5e95b48SJohn Johansen /* Syscall logging mode */ 137090ab5ee9SRusty Russell bool aa_g_logsyscall; 1371b5e95b48SJohn Johansen module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR); 1372b5e95b48SJohn Johansen 1373b5e95b48SJohn Johansen /* Maximum pathname length before accesses will start getting rejected */ 1374b5e95b48SJohn Johansen unsigned int aa_g_path_max = 2 * PATH_MAX; 1375622f6e32SJohn Johansen module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR); 1376b5e95b48SJohn Johansen 1377b5e95b48SJohn Johansen /* Determines how paranoid loading of policy is and how much verification 1378b5e95b48SJohn Johansen * on the loaded policy is done. 1379abbf8734SJohn Johansen * DEPRECATED: read only as strict checking of load is always done now 1380abbf8734SJohn Johansen * that none root users (user namespaces) can load policy. 1381b5e95b48SJohn Johansen */ 1382954317feSThomas Meyer bool aa_g_paranoid_load = true; 1383abbf8734SJohn Johansen module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); 1384b5e95b48SJohn Johansen 1385e33c1b99SKees Cook static int param_get_aaintbool(char *buffer, const struct kernel_param *kp); 1386e33c1b99SKees Cook static int param_set_aaintbool(const char *val, const struct kernel_param *kp); 1387e33c1b99SKees Cook #define param_check_aaintbool param_check_int 1388e33c1b99SKees Cook static const struct kernel_param_ops param_ops_aaintbool = { 1389e33c1b99SKees Cook .set = param_set_aaintbool, 1390e33c1b99SKees Cook .get = param_get_aaintbool 1391e33c1b99SKees Cook }; 1392b5e95b48SJohn Johansen /* Boot time disable flag */ 13930102fb83SKees Cook static int apparmor_enabled __lsm_ro_after_init = 1; 1394e33c1b99SKees Cook module_param_named(enabled, apparmor_enabled, aaintbool, 0444); 1395b5e95b48SJohn Johansen 1396b5e95b48SJohn Johansen static int __init apparmor_enabled_setup(char *str) 1397b5e95b48SJohn Johansen { 1398b5e95b48SJohn Johansen unsigned long enabled; 139929707b20SJingoo Han int error = kstrtoul(str, 0, &enabled); 1400b5e95b48SJohn Johansen if (!error) 1401b5e95b48SJohn Johansen apparmor_enabled = enabled ? 1 : 0; 1402b5e95b48SJohn Johansen return 1; 1403b5e95b48SJohn Johansen } 1404b5e95b48SJohn Johansen 1405b5e95b48SJohn Johansen __setup("apparmor=", apparmor_enabled_setup); 1406b5e95b48SJohn Johansen 1407b5e95b48SJohn Johansen /* set global flag turning off the ability to load policy */ 1408101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp) 1409b5e95b48SJohn Johansen { 1410545de8feSJohn Johansen if (!apparmor_enabled) 1411545de8feSJohn Johansen return -EINVAL; 141292de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1413b5e95b48SJohn Johansen return -EPERM; 1414b5e95b48SJohn Johansen return param_set_bool(val, kp); 1415b5e95b48SJohn Johansen } 1416b5e95b48SJohn Johansen 1417101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp) 1418b5e95b48SJohn Johansen { 1419ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1420ca4bd5aeSJohn Johansen return -EINVAL; 142192de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1422545de8feSJohn Johansen return -EPERM; 1423b5e95b48SJohn Johansen return param_get_bool(buffer, kp); 1424b5e95b48SJohn Johansen } 1425b5e95b48SJohn Johansen 1426101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp) 1427b5e95b48SJohn Johansen { 1428ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1429ca4bd5aeSJohn Johansen return -EINVAL; 143092de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1431545de8feSJohn Johansen return -EPERM; 1432b5e95b48SJohn Johansen return param_set_bool(val, kp); 1433b5e95b48SJohn Johansen } 1434b5e95b48SJohn Johansen 1435101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp) 1436b5e95b48SJohn Johansen { 1437ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1438ca4bd5aeSJohn Johansen return -EINVAL; 143992de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1440545de8feSJohn Johansen return -EPERM; 1441b5e95b48SJohn Johansen return param_get_bool(buffer, kp); 1442b5e95b48SJohn Johansen } 1443b5e95b48SJohn Johansen 1444101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp) 1445b5e95b48SJohn Johansen { 144639d84824SJohn Johansen int error; 144739d84824SJohn Johansen 1448ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1449ca4bd5aeSJohn Johansen return -EINVAL; 145039d84824SJohn Johansen /* file is ro but enforce 2nd line check */ 145139d84824SJohn Johansen if (apparmor_initialized) 1452545de8feSJohn Johansen return -EPERM; 145339d84824SJohn Johansen 145439d84824SJohn Johansen error = param_set_uint(val, kp); 1455df323337SSebastian Andrzej Siewior aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer)); 145639d84824SJohn Johansen pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); 145739d84824SJohn Johansen 145839d84824SJohn Johansen return error; 1459b5e95b48SJohn Johansen } 1460b5e95b48SJohn Johansen 1461101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp) 1462b5e95b48SJohn Johansen { 1463ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1464ca4bd5aeSJohn Johansen return -EINVAL; 146592de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1466545de8feSJohn Johansen return -EPERM; 1467b5e95b48SJohn Johansen return param_get_uint(buffer, kp); 1468b5e95b48SJohn Johansen } 1469b5e95b48SJohn Johansen 1470e33c1b99SKees Cook /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */ 1471e33c1b99SKees Cook static int param_set_aaintbool(const char *val, const struct kernel_param *kp) 1472e33c1b99SKees Cook { 1473e33c1b99SKees Cook struct kernel_param kp_local; 1474e33c1b99SKees Cook bool value; 1475e33c1b99SKees Cook int error; 1476e33c1b99SKees Cook 1477e33c1b99SKees Cook if (apparmor_initialized) 1478e33c1b99SKees Cook return -EPERM; 1479e33c1b99SKees Cook 1480e33c1b99SKees Cook /* Create local copy, with arg pointing to bool type. */ 1481e33c1b99SKees Cook value = !!*((int *)kp->arg); 1482e33c1b99SKees Cook memcpy(&kp_local, kp, sizeof(kp_local)); 1483e33c1b99SKees Cook kp_local.arg = &value; 1484e33c1b99SKees Cook 1485e33c1b99SKees Cook error = param_set_bool(val, &kp_local); 1486e33c1b99SKees Cook if (!error) 1487e33c1b99SKees Cook *((int *)kp->arg) = *((bool *)kp_local.arg); 1488e33c1b99SKees Cook return error; 1489e33c1b99SKees Cook } 1490e33c1b99SKees Cook 1491e33c1b99SKees Cook /* 1492e33c1b99SKees Cook * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to 1493e33c1b99SKees Cook * 1/0, this converts the "int that is actually bool" back to bool for 1494e33c1b99SKees Cook * display in the /sys filesystem, while keeping it "int" for the LSM 1495e33c1b99SKees Cook * infrastructure. 1496e33c1b99SKees Cook */ 1497e33c1b99SKees Cook static int param_get_aaintbool(char *buffer, const struct kernel_param *kp) 1498e33c1b99SKees Cook { 1499e33c1b99SKees Cook struct kernel_param kp_local; 1500e33c1b99SKees Cook bool value; 1501e33c1b99SKees Cook 1502e33c1b99SKees Cook /* Create local copy, with arg pointing to bool type. */ 1503e33c1b99SKees Cook value = !!*((int *)kp->arg); 1504e33c1b99SKees Cook memcpy(&kp_local, kp, sizeof(kp_local)); 1505e33c1b99SKees Cook kp_local.arg = &value; 1506e33c1b99SKees Cook 1507e33c1b99SKees Cook return param_get_bool(buffer, &kp_local); 1508e33c1b99SKees Cook } 1509e33c1b99SKees Cook 151063c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val, 151163c16c3aSChris Coulson const struct kernel_param *kp) 151263c16c3aSChris Coulson { 151363c16c3aSChris Coulson int error; 151463c16c3aSChris Coulson 151563c16c3aSChris Coulson if (!apparmor_enabled) 151663c16c3aSChris Coulson return -EINVAL; 151763c16c3aSChris Coulson if (apparmor_initialized) 151863c16c3aSChris Coulson return -EPERM; 151963c16c3aSChris Coulson 152063c16c3aSChris Coulson error = param_set_int(val, kp); 152163c16c3aSChris Coulson 152263c16c3aSChris Coulson aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level, 152363c16c3aSChris Coulson Z_NO_COMPRESSION, 152463c16c3aSChris Coulson Z_BEST_COMPRESSION); 152563c16c3aSChris Coulson pr_info("AppArmor: policy rawdata compression level set to %u\n", 152663c16c3aSChris Coulson aa_g_rawdata_compression_level); 152763c16c3aSChris Coulson 152863c16c3aSChris Coulson return error; 152963c16c3aSChris Coulson } 153063c16c3aSChris Coulson 153163c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer, 153263c16c3aSChris Coulson const struct kernel_param *kp) 153363c16c3aSChris Coulson { 153463c16c3aSChris Coulson if (!apparmor_enabled) 153563c16c3aSChris Coulson return -EINVAL; 153692de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 153763c16c3aSChris Coulson return -EPERM; 153863c16c3aSChris Coulson return param_get_int(buffer, kp); 153963c16c3aSChris Coulson } 154063c16c3aSChris Coulson 1541e4dca7b7SKees Cook static int param_get_audit(char *buffer, const struct kernel_param *kp) 1542b5e95b48SJohn Johansen { 1543b5e95b48SJohn Johansen if (!apparmor_enabled) 1544b5e95b48SJohn Johansen return -EINVAL; 154592de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1546545de8feSJohn Johansen return -EPERM; 1547b5e95b48SJohn Johansen return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]); 1548b5e95b48SJohn Johansen } 1549b5e95b48SJohn Johansen 1550e4dca7b7SKees Cook static int param_set_audit(const char *val, const struct kernel_param *kp) 1551b5e95b48SJohn Johansen { 1552b5e95b48SJohn Johansen int i; 1553b5e95b48SJohn Johansen 1554b5e95b48SJohn Johansen if (!apparmor_enabled) 1555b5e95b48SJohn Johansen return -EINVAL; 1556b5e95b48SJohn Johansen if (!val) 1557b5e95b48SJohn Johansen return -EINVAL; 155892de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1559545de8feSJohn Johansen return -EPERM; 1560b5e95b48SJohn Johansen 15615d8779a5SAndy Shevchenko i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val); 15625d8779a5SAndy Shevchenko if (i < 0) 15635d8779a5SAndy Shevchenko return -EINVAL; 15645d8779a5SAndy Shevchenko 1565b5e95b48SJohn Johansen aa_g_audit = i; 1566b5e95b48SJohn Johansen return 0; 1567b5e95b48SJohn Johansen } 1568b5e95b48SJohn Johansen 1569e4dca7b7SKees Cook static int param_get_mode(char *buffer, const struct kernel_param *kp) 1570b5e95b48SJohn Johansen { 1571b5e95b48SJohn Johansen if (!apparmor_enabled) 1572b5e95b48SJohn Johansen return -EINVAL; 157392de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1574545de8feSJohn Johansen return -EPERM; 1575b5e95b48SJohn Johansen 15760d259f04SJohn Johansen return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]); 1577b5e95b48SJohn Johansen } 1578b5e95b48SJohn Johansen 1579e4dca7b7SKees Cook static int param_set_mode(const char *val, const struct kernel_param *kp) 1580b5e95b48SJohn Johansen { 1581b5e95b48SJohn Johansen int i; 1582b5e95b48SJohn Johansen 1583b5e95b48SJohn Johansen if (!apparmor_enabled) 1584b5e95b48SJohn Johansen return -EINVAL; 1585b5e95b48SJohn Johansen if (!val) 1586b5e95b48SJohn Johansen return -EINVAL; 158792de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1588545de8feSJohn Johansen return -EPERM; 1589b5e95b48SJohn Johansen 15905d8779a5SAndy Shevchenko i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX, 15915d8779a5SAndy Shevchenko val); 15925d8779a5SAndy Shevchenko if (i < 0) 15935d8779a5SAndy Shevchenko return -EINVAL; 15945d8779a5SAndy Shevchenko 1595b5e95b48SJohn Johansen aa_g_profile_mode = i; 1596b5e95b48SJohn Johansen return 0; 1597b5e95b48SJohn Johansen } 1598b5e95b48SJohn Johansen 1599341c1fdaSJohn Johansen char *aa_get_buffer(bool in_atomic) 1600df323337SSebastian Andrzej Siewior { 1601df323337SSebastian Andrzej Siewior union aa_buffer *aa_buf; 1602df323337SSebastian Andrzej Siewior bool try_again = true; 1603341c1fdaSJohn Johansen gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 1604df323337SSebastian Andrzej Siewior 1605df323337SSebastian Andrzej Siewior retry: 1606df323337SSebastian Andrzej Siewior spin_lock(&aa_buffers_lock); 1607341c1fdaSJohn Johansen if (buffer_count > reserve_count || 1608341c1fdaSJohn Johansen (in_atomic && !list_empty(&aa_global_buffers))) { 1609df323337SSebastian Andrzej Siewior aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, 1610df323337SSebastian Andrzej Siewior list); 1611df323337SSebastian Andrzej Siewior list_del(&aa_buf->list); 1612341c1fdaSJohn Johansen buffer_count--; 1613df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1614df323337SSebastian Andrzej Siewior return &aa_buf->buffer[0]; 1615df323337SSebastian Andrzej Siewior } 1616341c1fdaSJohn Johansen if (in_atomic) { 1617341c1fdaSJohn Johansen /* 1618341c1fdaSJohn Johansen * out of reserve buffers and in atomic context so increase 1619341c1fdaSJohn Johansen * how many buffers to keep in reserve 1620341c1fdaSJohn Johansen */ 1621341c1fdaSJohn Johansen reserve_count++; 1622341c1fdaSJohn Johansen flags = GFP_ATOMIC; 1623341c1fdaSJohn Johansen } 1624df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1625df323337SSebastian Andrzej Siewior 1626341c1fdaSJohn Johansen if (!in_atomic) 1627341c1fdaSJohn Johansen might_sleep(); 1628341c1fdaSJohn Johansen aa_buf = kmalloc(aa_g_path_max, flags); 1629df323337SSebastian Andrzej Siewior if (!aa_buf) { 1630df323337SSebastian Andrzej Siewior if (try_again) { 1631df323337SSebastian Andrzej Siewior try_again = false; 1632df323337SSebastian Andrzej Siewior goto retry; 1633df323337SSebastian Andrzej Siewior } 1634df323337SSebastian Andrzej Siewior pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n"); 1635df323337SSebastian Andrzej Siewior return NULL; 1636df323337SSebastian Andrzej Siewior } 1637df323337SSebastian Andrzej Siewior return &aa_buf->buffer[0]; 1638df323337SSebastian Andrzej Siewior } 1639df323337SSebastian Andrzej Siewior 1640df323337SSebastian Andrzej Siewior void aa_put_buffer(char *buf) 1641df323337SSebastian Andrzej Siewior { 1642df323337SSebastian Andrzej Siewior union aa_buffer *aa_buf; 1643df323337SSebastian Andrzej Siewior 1644df323337SSebastian Andrzej Siewior if (!buf) 1645df323337SSebastian Andrzej Siewior return; 1646df323337SSebastian Andrzej Siewior aa_buf = container_of(buf, union aa_buffer, buffer[0]); 1647df323337SSebastian Andrzej Siewior 1648df323337SSebastian Andrzej Siewior spin_lock(&aa_buffers_lock); 1649df323337SSebastian Andrzej Siewior list_add(&aa_buf->list, &aa_global_buffers); 1650341c1fdaSJohn Johansen buffer_count++; 1651df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1652df323337SSebastian Andrzej Siewior } 1653df323337SSebastian Andrzej Siewior 1654b5e95b48SJohn Johansen /* 1655b5e95b48SJohn Johansen * AppArmor init functions 1656b5e95b48SJohn Johansen */ 1657b5e95b48SJohn Johansen 1658b5e95b48SJohn Johansen /** 165955a26ebfSJohn Johansen * set_init_ctx - set a task context and profile on the first task. 1660b5e95b48SJohn Johansen * 1661b5e95b48SJohn Johansen * TODO: allow setting an alternate profile than unconfined 1662b5e95b48SJohn Johansen */ 166355a26ebfSJohn Johansen static int __init set_init_ctx(void) 1664b5e95b48SJohn Johansen { 1665bf1d2ee7SBharath Vedartham struct cred *cred = (__force struct cred *)current->real_cred; 1666b5e95b48SJohn Johansen 166769b5a44aSCasey Schaufler set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); 1668b5e95b48SJohn Johansen 1669b5e95b48SJohn Johansen return 0; 1670b5e95b48SJohn Johansen } 1671b5e95b48SJohn Johansen 1672d4669f0bSJohn Johansen static void destroy_buffers(void) 1673d4669f0bSJohn Johansen { 1674df323337SSebastian Andrzej Siewior union aa_buffer *aa_buf; 1675d4669f0bSJohn Johansen 1676df323337SSebastian Andrzej Siewior spin_lock(&aa_buffers_lock); 1677df323337SSebastian Andrzej Siewior while (!list_empty(&aa_global_buffers)) { 1678df323337SSebastian Andrzej Siewior aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, 1679df323337SSebastian Andrzej Siewior list); 1680df323337SSebastian Andrzej Siewior list_del(&aa_buf->list); 1681df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1682df323337SSebastian Andrzej Siewior kfree(aa_buf); 1683df323337SSebastian Andrzej Siewior spin_lock(&aa_buffers_lock); 1684d4669f0bSJohn Johansen } 1685df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1686d4669f0bSJohn Johansen } 1687d4669f0bSJohn Johansen 1688d4669f0bSJohn Johansen static int __init alloc_buffers(void) 1689d4669f0bSJohn Johansen { 1690df323337SSebastian Andrzej Siewior union aa_buffer *aa_buf; 1691df323337SSebastian Andrzej Siewior int i, num; 1692d4669f0bSJohn Johansen 1693df323337SSebastian Andrzej Siewior /* 1694df323337SSebastian Andrzej Siewior * A function may require two buffers at once. Usually the buffers are 1695df323337SSebastian Andrzej Siewior * used for a short period of time and are shared. On UP kernel buffers 1696df323337SSebastian Andrzej Siewior * two should be enough, with more CPUs it is possible that more 1697df323337SSebastian Andrzej Siewior * buffers will be used simultaneously. The preallocated pool may grow. 1698df323337SSebastian Andrzej Siewior * This preallocation has also the side-effect that AppArmor will be 1699df323337SSebastian Andrzej Siewior * disabled early at boot if aa_g_path_max is extremly high. 1700df323337SSebastian Andrzej Siewior */ 1701df323337SSebastian Andrzej Siewior if (num_online_cpus() > 1) 1702341c1fdaSJohn Johansen num = 4 + RESERVE_COUNT; 1703d4669f0bSJohn Johansen else 1704341c1fdaSJohn Johansen num = 2 + RESERVE_COUNT; 1705df323337SSebastian Andrzej Siewior 1706df323337SSebastian Andrzej Siewior for (i = 0; i < num; i++) { 1707df323337SSebastian Andrzej Siewior 1708df323337SSebastian Andrzej Siewior aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL | 1709df323337SSebastian Andrzej Siewior __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 1710df323337SSebastian Andrzej Siewior if (!aa_buf) { 1711d4669f0bSJohn Johansen destroy_buffers(); 1712d4669f0bSJohn Johansen return -ENOMEM; 1713d4669f0bSJohn Johansen } 1714df323337SSebastian Andrzej Siewior aa_put_buffer(&aa_buf->buffer[0]); 1715d4669f0bSJohn Johansen } 1716d4669f0bSJohn Johansen return 0; 1717d4669f0bSJohn Johansen } 1718d4669f0bSJohn Johansen 1719e3ea1ca5STyler Hicks #ifdef CONFIG_SYSCTL 1720e3ea1ca5STyler Hicks static int apparmor_dointvec(struct ctl_table *table, int write, 172132927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 1722e3ea1ca5STyler Hicks { 172392de220aSJohn Johansen if (!aa_current_policy_admin_capable(NULL)) 1724e3ea1ca5STyler Hicks return -EPERM; 1725e3ea1ca5STyler Hicks if (!apparmor_enabled) 1726e3ea1ca5STyler Hicks return -EINVAL; 1727e3ea1ca5STyler Hicks 1728e3ea1ca5STyler Hicks return proc_dointvec(table, write, buffer, lenp, ppos); 1729e3ea1ca5STyler Hicks } 1730e3ea1ca5STyler Hicks 1731e3ea1ca5STyler Hicks static struct ctl_path apparmor_sysctl_path[] = { 1732e3ea1ca5STyler Hicks { .procname = "kernel", }, 1733e3ea1ca5STyler Hicks { } 1734e3ea1ca5STyler Hicks }; 1735e3ea1ca5STyler Hicks 1736e3ea1ca5STyler Hicks static struct ctl_table apparmor_sysctl_table[] = { 1737e3ea1ca5STyler Hicks { 1738e3ea1ca5STyler Hicks .procname = "unprivileged_userns_apparmor_policy", 1739e3ea1ca5STyler Hicks .data = &unprivileged_userns_apparmor_policy, 1740e3ea1ca5STyler Hicks .maxlen = sizeof(int), 1741e3ea1ca5STyler Hicks .mode = 0600, 1742e3ea1ca5STyler Hicks .proc_handler = apparmor_dointvec, 1743e3ea1ca5STyler Hicks }, 1744e3ea1ca5STyler Hicks { } 1745e3ea1ca5STyler Hicks }; 1746e3ea1ca5STyler Hicks 1747e3ea1ca5STyler Hicks static int __init apparmor_init_sysctl(void) 1748e3ea1ca5STyler Hicks { 1749e3ea1ca5STyler Hicks return register_sysctl_paths(apparmor_sysctl_path, 1750e3ea1ca5STyler Hicks apparmor_sysctl_table) ? 0 : -ENOMEM; 1751e3ea1ca5STyler Hicks } 1752e3ea1ca5STyler Hicks #else 1753e3ea1ca5STyler Hicks static inline int apparmor_init_sysctl(void) 1754e3ea1ca5STyler Hicks { 1755e3ea1ca5STyler Hicks return 0; 1756e3ea1ca5STyler Hicks } 1757e3ea1ca5STyler Hicks #endif /* CONFIG_SYSCTL */ 1758e3ea1ca5STyler Hicks 1759e1af4779SArnd Bergmann #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) 1760ab9f2115SMatthew Garrett static unsigned int apparmor_ip_postroute(void *priv, 1761ab9f2115SMatthew Garrett struct sk_buff *skb, 1762ab9f2115SMatthew Garrett const struct nf_hook_state *state) 1763ab9f2115SMatthew Garrett { 1764ab9f2115SMatthew Garrett struct aa_sk_ctx *ctx; 1765ab9f2115SMatthew Garrett struct sock *sk; 1766ab9f2115SMatthew Garrett 1767ab9f2115SMatthew Garrett if (!skb->secmark) 1768ab9f2115SMatthew Garrett return NF_ACCEPT; 1769ab9f2115SMatthew Garrett 1770ab9f2115SMatthew Garrett sk = skb_to_full_sk(skb); 1771ab9f2115SMatthew Garrett if (sk == NULL) 1772ab9f2115SMatthew Garrett return NF_ACCEPT; 1773ab9f2115SMatthew Garrett 1774ab9f2115SMatthew Garrett ctx = SK_CTX(sk); 1775ab9f2115SMatthew Garrett if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, 1776ab9f2115SMatthew Garrett skb->secmark, sk)) 1777ab9f2115SMatthew Garrett return NF_ACCEPT; 1778ab9f2115SMatthew Garrett 1779ab9f2115SMatthew Garrett return NF_DROP_ERR(-ECONNREFUSED); 1780ab9f2115SMatthew Garrett 1781ab9f2115SMatthew Garrett } 1782ab9f2115SMatthew Garrett 1783ab9f2115SMatthew Garrett static const struct nf_hook_ops apparmor_nf_ops[] = { 1784ab9f2115SMatthew Garrett { 17857b721124SFlorian Westphal .hook = apparmor_ip_postroute, 1786ab9f2115SMatthew Garrett .pf = NFPROTO_IPV4, 1787ab9f2115SMatthew Garrett .hooknum = NF_INET_POST_ROUTING, 1788ab9f2115SMatthew Garrett .priority = NF_IP_PRI_SELINUX_FIRST, 1789ab9f2115SMatthew Garrett }, 1790ab9f2115SMatthew Garrett #if IS_ENABLED(CONFIG_IPV6) 1791ab9f2115SMatthew Garrett { 17927b721124SFlorian Westphal .hook = apparmor_ip_postroute, 1793ab9f2115SMatthew Garrett .pf = NFPROTO_IPV6, 1794ab9f2115SMatthew Garrett .hooknum = NF_INET_POST_ROUTING, 1795ab9f2115SMatthew Garrett .priority = NF_IP6_PRI_SELINUX_FIRST, 1796ab9f2115SMatthew Garrett }, 1797ab9f2115SMatthew Garrett #endif 1798ab9f2115SMatthew Garrett }; 1799ab9f2115SMatthew Garrett 1800ab9f2115SMatthew Garrett static int __net_init apparmor_nf_register(struct net *net) 1801ab9f2115SMatthew Garrett { 1802ab9f2115SMatthew Garrett int ret; 1803ab9f2115SMatthew Garrett 1804ab9f2115SMatthew Garrett ret = nf_register_net_hooks(net, apparmor_nf_ops, 1805ab9f2115SMatthew Garrett ARRAY_SIZE(apparmor_nf_ops)); 1806ab9f2115SMatthew Garrett return ret; 1807ab9f2115SMatthew Garrett } 1808ab9f2115SMatthew Garrett 1809ab9f2115SMatthew Garrett static void __net_exit apparmor_nf_unregister(struct net *net) 1810ab9f2115SMatthew Garrett { 1811ab9f2115SMatthew Garrett nf_unregister_net_hooks(net, apparmor_nf_ops, 1812ab9f2115SMatthew Garrett ARRAY_SIZE(apparmor_nf_ops)); 1813ab9f2115SMatthew Garrett } 1814ab9f2115SMatthew Garrett 1815ab9f2115SMatthew Garrett static struct pernet_operations apparmor_net_ops = { 1816ab9f2115SMatthew Garrett .init = apparmor_nf_register, 1817ab9f2115SMatthew Garrett .exit = apparmor_nf_unregister, 1818ab9f2115SMatthew Garrett }; 1819ab9f2115SMatthew Garrett 1820ab9f2115SMatthew Garrett static int __init apparmor_nf_ip_init(void) 1821ab9f2115SMatthew Garrett { 1822ab9f2115SMatthew Garrett int err; 1823ab9f2115SMatthew Garrett 1824ab9f2115SMatthew Garrett if (!apparmor_enabled) 1825ab9f2115SMatthew Garrett return 0; 1826ab9f2115SMatthew Garrett 1827ab9f2115SMatthew Garrett err = register_pernet_subsys(&apparmor_net_ops); 1828ab9f2115SMatthew Garrett if (err) 1829ab9f2115SMatthew Garrett panic("Apparmor: register_pernet_subsys: error %d\n", err); 1830ab9f2115SMatthew Garrett 1831ab9f2115SMatthew Garrett return 0; 1832ab9f2115SMatthew Garrett } 1833ab9f2115SMatthew Garrett __initcall(apparmor_nf_ip_init); 1834e1af4779SArnd Bergmann #endif 1835ab9f2115SMatthew Garrett 1836b5e95b48SJohn Johansen static int __init apparmor_init(void) 1837b5e95b48SJohn Johansen { 1838b5e95b48SJohn Johansen int error; 1839b5e95b48SJohn Johansen 1840a4c3f89cSJohn Johansen aa_secids_init(); 1841a4c3f89cSJohn Johansen 184211c236b8SJohn Johansen error = aa_setup_dfa_engine(); 184311c236b8SJohn Johansen if (error) { 184411c236b8SJohn Johansen AA_ERROR("Unable to setup dfa engine\n"); 184511c236b8SJohn Johansen goto alloc_out; 184611c236b8SJohn Johansen } 184711c236b8SJohn Johansen 1848b5e95b48SJohn Johansen error = aa_alloc_root_ns(); 1849b5e95b48SJohn Johansen if (error) { 1850b5e95b48SJohn Johansen AA_ERROR("Unable to allocate default profile namespace\n"); 1851b5e95b48SJohn Johansen goto alloc_out; 1852b5e95b48SJohn Johansen } 1853b5e95b48SJohn Johansen 1854e3ea1ca5STyler Hicks error = apparmor_init_sysctl(); 1855e3ea1ca5STyler Hicks if (error) { 1856e3ea1ca5STyler Hicks AA_ERROR("Unable to register sysctls\n"); 1857e3ea1ca5STyler Hicks goto alloc_out; 1858e3ea1ca5STyler Hicks 1859e3ea1ca5STyler Hicks } 1860e3ea1ca5STyler Hicks 1861d4669f0bSJohn Johansen error = alloc_buffers(); 1862d4669f0bSJohn Johansen if (error) { 1863d4669f0bSJohn Johansen AA_ERROR("Unable to allocate work buffers\n"); 1864df323337SSebastian Andrzej Siewior goto alloc_out; 1865d4669f0bSJohn Johansen } 1866d4669f0bSJohn Johansen 186755a26ebfSJohn Johansen error = set_init_ctx(); 1868b5e95b48SJohn Johansen if (error) { 1869b5e95b48SJohn Johansen AA_ERROR("Failed to set context on init task\n"); 1870b1d9e6b0SCasey Schaufler aa_free_root_ns(); 1871d4669f0bSJohn Johansen goto buffers_out; 1872b5e95b48SJohn Johansen } 1873d69dece5SCasey Schaufler security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks), 1874d69dece5SCasey Schaufler "apparmor"); 1875b5e95b48SJohn Johansen 1876b5e95b48SJohn Johansen /* Report that AppArmor successfully initialized */ 1877b5e95b48SJohn Johansen apparmor_initialized = 1; 1878b5e95b48SJohn Johansen if (aa_g_profile_mode == APPARMOR_COMPLAIN) 1879b5e95b48SJohn Johansen aa_info_message("AppArmor initialized: complain mode enabled"); 1880b5e95b48SJohn Johansen else if (aa_g_profile_mode == APPARMOR_KILL) 1881b5e95b48SJohn Johansen aa_info_message("AppArmor initialized: kill mode enabled"); 1882b5e95b48SJohn Johansen else 1883b5e95b48SJohn Johansen aa_info_message("AppArmor initialized"); 1884b5e95b48SJohn Johansen 1885b5e95b48SJohn Johansen return error; 1886b5e95b48SJohn Johansen 1887d4669f0bSJohn Johansen buffers_out: 1888d4669f0bSJohn Johansen destroy_buffers(); 1889b5e95b48SJohn Johansen alloc_out: 1890b5e95b48SJohn Johansen aa_destroy_aafs(); 189111c236b8SJohn Johansen aa_teardown_dfa_engine(); 1892b5e95b48SJohn Johansen 1893954317feSThomas Meyer apparmor_enabled = false; 1894b5e95b48SJohn Johansen return error; 1895b5e95b48SJohn Johansen } 1896b5e95b48SJohn Johansen 18973d6e5f6dSKees Cook DEFINE_LSM(apparmor) = { 189807aed2f2SKees Cook .name = "apparmor", 189914bd99c8SKees Cook .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 1900c5459b82SKees Cook .enabled = &apparmor_enabled, 1901bbd3662aSCasey Schaufler .blobs = &apparmor_blob_sizes, 19023d6e5f6dSKees Cook .init = apparmor_init, 19033d6e5f6dSKees Cook }; 1904