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> 24f4d6b94bSJon Tourville #include <linux/zstd.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) { 1661ad22fccSJohn Johansen struct aa_ruleset *rules; 167c70c86c4SJohn Johansen if (COMPLAIN_MODE(profile)) 168c70c86c4SJohn Johansen continue; 1691ad22fccSJohn Johansen rules = list_first_entry(&profile->rules, 1701ad22fccSJohn Johansen typeof(*rules), list); 171c70c86c4SJohn Johansen *effective = cap_intersect(*effective, 1721ad22fccSJohn Johansen rules->caps.allow); 173c70c86c4SJohn Johansen *permitted = cap_intersect(*permitted, 1741ad22fccSJohn Johansen rules->caps.allow); 175c70c86c4SJohn Johansen } 176b5e95b48SJohn Johansen } 177b5e95b48SJohn Johansen rcu_read_unlock(); 178637f688dSJohn Johansen aa_put_label(label); 179b5e95b48SJohn Johansen 180b5e95b48SJohn Johansen return 0; 181b5e95b48SJohn Johansen } 182b5e95b48SJohn Johansen 1836a9de491SEric Paris static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, 184c1a85a00SMicah Morton int cap, unsigned int opts) 185b5e95b48SJohn Johansen { 186637f688dSJohn Johansen struct aa_label *label; 187b1d9e6b0SCasey Schaufler int error = 0; 188b1d9e6b0SCasey Schaufler 189637f688dSJohn Johansen label = aa_get_newest_cred_label(cred); 190637f688dSJohn Johansen if (!unconfined(label)) 191c1a85a00SMicah Morton error = aa_capable(label, cap, opts); 192637f688dSJohn Johansen aa_put_label(label); 193cf797c0eSJohn Johansen 194b5e95b48SJohn Johansen return error; 195b5e95b48SJohn Johansen } 196b5e95b48SJohn Johansen 197b5e95b48SJohn Johansen /** 198b5e95b48SJohn Johansen * common_perm - basic common permission check wrapper fn for paths 199b5e95b48SJohn Johansen * @op: operation being checked 200b5e95b48SJohn Johansen * @path: path to check permission of (NOT NULL) 201b5e95b48SJohn Johansen * @mask: requested permissions mask 202b5e95b48SJohn Johansen * @cond: conditional info for the permission request (NOT NULL) 203b5e95b48SJohn Johansen * 204b5e95b48SJohn Johansen * Returns: %0 else error code if error or permission denied 205b5e95b48SJohn Johansen */ 20647f6e5ccSJohn Johansen static int common_perm(const char *op, const struct path *path, u32 mask, 207b5e95b48SJohn Johansen struct path_cond *cond) 208b5e95b48SJohn Johansen { 209637f688dSJohn Johansen struct aa_label *label; 210b5e95b48SJohn Johansen int error = 0; 211b5e95b48SJohn Johansen 212637f688dSJohn Johansen label = __begin_current_label_crit_section(); 213637f688dSJohn Johansen if (!unconfined(label)) 214aebd873eSJohn Johansen error = aa_path_perm(op, label, path, 0, mask, cond); 215637f688dSJohn Johansen __end_current_label_crit_section(label); 216b5e95b48SJohn Johansen 217b5e95b48SJohn Johansen return error; 218b5e95b48SJohn Johansen } 219b5e95b48SJohn Johansen 220b5e95b48SJohn Johansen /** 22131f75bfeSJohn Johansen * common_perm_cond - common permission wrapper around inode cond 22231f75bfeSJohn Johansen * @op: operation being checked 22331f75bfeSJohn Johansen * @path: location to check (NOT NULL) 22431f75bfeSJohn Johansen * @mask: requested permissions mask 22531f75bfeSJohn Johansen * 22631f75bfeSJohn Johansen * Returns: %0 else error code if error or permission denied 22731f75bfeSJohn Johansen */ 22831f75bfeSJohn Johansen static int common_perm_cond(const char *op, const struct path *path, u32 mask) 22931f75bfeSJohn Johansen { 2303cee6079SChristian Brauner struct user_namespace *mnt_userns = mnt_user_ns(path->mnt); 2313cee6079SChristian Brauner struct path_cond cond = { 2323cee6079SChristian Brauner i_uid_into_mnt(mnt_userns, d_backing_inode(path->dentry)), 23331f75bfeSJohn Johansen d_backing_inode(path->dentry)->i_mode 23431f75bfeSJohn Johansen }; 23531f75bfeSJohn Johansen 23631f75bfeSJohn Johansen if (!path_mediated_fs(path->dentry)) 23731f75bfeSJohn Johansen return 0; 23831f75bfeSJohn Johansen 23931f75bfeSJohn Johansen return common_perm(op, path, mask, &cond); 24031f75bfeSJohn Johansen } 24131f75bfeSJohn Johansen 24231f75bfeSJohn Johansen /** 243b5e95b48SJohn Johansen * common_perm_dir_dentry - common permission wrapper when path is dir, dentry 244b5e95b48SJohn Johansen * @op: operation being checked 245b5e95b48SJohn Johansen * @dir: directory of the dentry (NOT NULL) 246b5e95b48SJohn Johansen * @dentry: dentry to check (NOT NULL) 247b5e95b48SJohn Johansen * @mask: requested permissions mask 248b5e95b48SJohn Johansen * @cond: conditional info for the permission request (NOT NULL) 249b5e95b48SJohn Johansen * 250b5e95b48SJohn Johansen * Returns: %0 else error code if error or permission denied 251b5e95b48SJohn Johansen */ 25247f6e5ccSJohn Johansen static int common_perm_dir_dentry(const char *op, const struct path *dir, 253b5e95b48SJohn Johansen struct dentry *dentry, u32 mask, 254b5e95b48SJohn Johansen struct path_cond *cond) 255b5e95b48SJohn Johansen { 2568486adf0SKees Cook struct path path = { .mnt = dir->mnt, .dentry = dentry }; 257b5e95b48SJohn Johansen 258b5e95b48SJohn Johansen return common_perm(op, &path, mask, cond); 259b5e95b48SJohn Johansen } 260b5e95b48SJohn Johansen 261b5e95b48SJohn Johansen /** 262b5e95b48SJohn Johansen * common_perm_rm - common permission wrapper for operations doing rm 263b5e95b48SJohn Johansen * @op: operation being checked 264b5e95b48SJohn Johansen * @dir: directory that the dentry is in (NOT NULL) 265b5e95b48SJohn Johansen * @dentry: dentry being rm'd (NOT NULL) 266b5e95b48SJohn Johansen * @mask: requested permission mask 267b5e95b48SJohn Johansen * 268b5e95b48SJohn Johansen * Returns: %0 else error code if error or permission denied 269b5e95b48SJohn Johansen */ 27047f6e5ccSJohn Johansen static int common_perm_rm(const char *op, const struct path *dir, 271b5e95b48SJohn Johansen struct dentry *dentry, u32 mask) 272b5e95b48SJohn Johansen { 273c6f493d6SDavid Howells struct inode *inode = d_backing_inode(dentry); 2743cee6079SChristian Brauner struct user_namespace *mnt_userns = mnt_user_ns(dir->mnt); 275b5e95b48SJohn Johansen struct path_cond cond = { }; 276b5e95b48SJohn Johansen 277efeee83aSJohn Johansen if (!inode || !path_mediated_fs(dentry)) 278b5e95b48SJohn Johansen return 0; 279b5e95b48SJohn Johansen 2803cee6079SChristian Brauner cond.uid = i_uid_into_mnt(mnt_userns, inode); 281b5e95b48SJohn Johansen cond.mode = inode->i_mode; 282b5e95b48SJohn Johansen 283b5e95b48SJohn Johansen return common_perm_dir_dentry(op, dir, dentry, mask, &cond); 284b5e95b48SJohn Johansen } 285b5e95b48SJohn Johansen 286b5e95b48SJohn Johansen /** 287b5e95b48SJohn Johansen * common_perm_create - common permission wrapper for operations doing create 288b5e95b48SJohn Johansen * @op: operation being checked 289b5e95b48SJohn Johansen * @dir: directory that dentry will be created in (NOT NULL) 290b5e95b48SJohn Johansen * @dentry: dentry to create (NOT NULL) 291b5e95b48SJohn Johansen * @mask: request permission mask 292b5e95b48SJohn Johansen * @mode: created file mode 293b5e95b48SJohn Johansen * 294b5e95b48SJohn Johansen * Returns: %0 else error code if error or permission denied 295b5e95b48SJohn Johansen */ 29647f6e5ccSJohn Johansen static int common_perm_create(const char *op, const struct path *dir, 297d6b49f7aSAl Viro struct dentry *dentry, u32 mask, umode_t mode) 298b5e95b48SJohn Johansen { 299b5e95b48SJohn Johansen struct path_cond cond = { current_fsuid(), mode }; 300b5e95b48SJohn Johansen 301efeee83aSJohn Johansen if (!path_mediated_fs(dir->dentry)) 302b5e95b48SJohn Johansen return 0; 303b5e95b48SJohn Johansen 304b5e95b48SJohn Johansen return common_perm_dir_dentry(op, dir, dentry, mask, &cond); 305b5e95b48SJohn Johansen } 306b5e95b48SJohn Johansen 307989f74e0SAl Viro static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry) 308b5e95b48SJohn Johansen { 309b5e95b48SJohn Johansen return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE); 310b5e95b48SJohn Johansen } 311b5e95b48SJohn Johansen 312d3607752SAl Viro static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry, 3134572befeSAl Viro umode_t mode) 314b5e95b48SJohn Johansen { 315b5e95b48SJohn Johansen return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE, 316b5e95b48SJohn Johansen S_IFDIR); 317b5e95b48SJohn Johansen } 318b5e95b48SJohn Johansen 319989f74e0SAl Viro static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry) 320b5e95b48SJohn Johansen { 321b5e95b48SJohn Johansen return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE); 322b5e95b48SJohn Johansen } 323b5e95b48SJohn Johansen 324d3607752SAl Viro static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry, 32504fc66e7SAl Viro umode_t mode, unsigned int dev) 326b5e95b48SJohn Johansen { 327b5e95b48SJohn Johansen return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode); 328b5e95b48SJohn Johansen } 329b5e95b48SJohn Johansen 33081f4c506SAl Viro static int apparmor_path_truncate(const struct path *path) 331b5e95b48SJohn Johansen { 332e53cfe6cSJohn Johansen return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); 333b5e95b48SJohn Johansen } 334b5e95b48SJohn Johansen 335d3607752SAl Viro static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, 336b5e95b48SJohn Johansen const char *old_name) 337b5e95b48SJohn Johansen { 338b5e95b48SJohn Johansen return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE, 339b5e95b48SJohn Johansen S_IFLNK); 340b5e95b48SJohn Johansen } 341b5e95b48SJohn Johansen 3423ccee46aSAl Viro static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir, 343b5e95b48SJohn Johansen struct dentry *new_dentry) 344b5e95b48SJohn Johansen { 345637f688dSJohn Johansen struct aa_label *label; 346b5e95b48SJohn Johansen int error = 0; 347b5e95b48SJohn Johansen 348efeee83aSJohn Johansen if (!path_mediated_fs(old_dentry)) 349b5e95b48SJohn Johansen return 0; 350b5e95b48SJohn Johansen 351637f688dSJohn Johansen label = begin_current_label_crit_section(); 352637f688dSJohn Johansen if (!unconfined(label)) 3538014370fSJohn Johansen error = aa_path_link(label, old_dentry, new_dir, new_dentry); 354637f688dSJohn Johansen end_current_label_crit_section(label); 355cf797c0eSJohn Johansen 356b5e95b48SJohn Johansen return error; 357b5e95b48SJohn Johansen } 358b5e95b48SJohn Johansen 3593ccee46aSAl Viro static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, 360100f59d9SMickaël Salaün const struct path *new_dir, struct dentry *new_dentry, 361100f59d9SMickaël Salaün const unsigned int flags) 362b5e95b48SJohn Johansen { 363637f688dSJohn Johansen struct aa_label *label; 364b5e95b48SJohn Johansen int error = 0; 365b5e95b48SJohn Johansen 366efeee83aSJohn Johansen if (!path_mediated_fs(old_dentry)) 367b5e95b48SJohn Johansen return 0; 368100f59d9SMickaël Salaün if ((flags & RENAME_EXCHANGE) && !path_mediated_fs(new_dentry)) 369100f59d9SMickaël Salaün return 0; 370b5e95b48SJohn Johansen 371637f688dSJohn Johansen label = begin_current_label_crit_section(); 372637f688dSJohn Johansen if (!unconfined(label)) { 3733cee6079SChristian Brauner struct user_namespace *mnt_userns = mnt_user_ns(old_dir->mnt); 3748486adf0SKees Cook struct path old_path = { .mnt = old_dir->mnt, 3758486adf0SKees Cook .dentry = old_dentry }; 3768486adf0SKees Cook struct path new_path = { .mnt = new_dir->mnt, 3778486adf0SKees Cook .dentry = new_dentry }; 3783cee6079SChristian Brauner struct path_cond cond = { 3793cee6079SChristian Brauner i_uid_into_mnt(mnt_userns, d_backing_inode(old_dentry)), 380c6f493d6SDavid Howells d_backing_inode(old_dentry)->i_mode 381b5e95b48SJohn Johansen }; 382b5e95b48SJohn Johansen 383100f59d9SMickaël Salaün if (flags & RENAME_EXCHANGE) { 384100f59d9SMickaël Salaün struct path_cond cond_exchange = { 385100f59d9SMickaël Salaün i_uid_into_mnt(mnt_userns, d_backing_inode(new_dentry)), 386100f59d9SMickaël Salaün d_backing_inode(new_dentry)->i_mode 387100f59d9SMickaël Salaün }; 388100f59d9SMickaël Salaün 389100f59d9SMickaël Salaün error = aa_path_perm(OP_RENAME_SRC, label, &new_path, 0, 390100f59d9SMickaël Salaün MAY_READ | AA_MAY_GETATTR | MAY_WRITE | 391100f59d9SMickaël Salaün AA_MAY_SETATTR | AA_MAY_DELETE, 392100f59d9SMickaël Salaün &cond_exchange); 393100f59d9SMickaël Salaün if (!error) 394100f59d9SMickaël Salaün error = aa_path_perm(OP_RENAME_DEST, label, &old_path, 395100f59d9SMickaël Salaün 0, MAY_WRITE | AA_MAY_SETATTR | 396100f59d9SMickaël Salaün AA_MAY_CREATE, &cond_exchange); 397100f59d9SMickaël Salaün } 398100f59d9SMickaël Salaün 399100f59d9SMickaël Salaün if (!error) 400aebd873eSJohn Johansen error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, 401e53cfe6cSJohn Johansen MAY_READ | AA_MAY_GETATTR | MAY_WRITE | 402e53cfe6cSJohn Johansen AA_MAY_SETATTR | AA_MAY_DELETE, 403b5e95b48SJohn Johansen &cond); 404b5e95b48SJohn Johansen if (!error) 405aebd873eSJohn Johansen error = aa_path_perm(OP_RENAME_DEST, label, &new_path, 406e53cfe6cSJohn Johansen 0, MAY_WRITE | AA_MAY_SETATTR | 407b5e95b48SJohn Johansen AA_MAY_CREATE, &cond); 408b5e95b48SJohn Johansen 409b5e95b48SJohn Johansen } 410637f688dSJohn Johansen end_current_label_crit_section(label); 411cf797c0eSJohn Johansen 412b5e95b48SJohn Johansen return error; 413b5e95b48SJohn Johansen } 414b5e95b48SJohn Johansen 415be01f9f2SAl Viro static int apparmor_path_chmod(const struct path *path, umode_t mode) 416b5e95b48SJohn Johansen { 41731f75bfeSJohn Johansen return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD); 418b5e95b48SJohn Johansen } 419b5e95b48SJohn Johansen 4207fd25dacSAl Viro static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid) 421b5e95b48SJohn Johansen { 42231f75bfeSJohn Johansen return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN); 423b5e95b48SJohn Johansen } 424b5e95b48SJohn Johansen 4253f7036a0SAl Viro static int apparmor_inode_getattr(const struct path *path) 426b5e95b48SJohn Johansen { 427e53cfe6cSJohn Johansen return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR); 428b5e95b48SJohn Johansen } 429b5e95b48SJohn Johansen 43094817692SAl Viro static int apparmor_file_open(struct file *file) 431b5e95b48SJohn Johansen { 432637f688dSJohn Johansen struct aa_file_ctx *fctx = file_ctx(file); 433637f688dSJohn Johansen struct aa_label *label; 434b5e95b48SJohn Johansen int error = 0; 435b5e95b48SJohn Johansen 436efeee83aSJohn Johansen if (!path_mediated_fs(file->f_path.dentry)) 437b5e95b48SJohn Johansen return 0; 438b5e95b48SJohn Johansen 439b5e95b48SJohn Johansen /* If in exec, permission is handled by bprm hooks. 440b5e95b48SJohn Johansen * Cache permissions granted by the previous exec check, with 441b5e95b48SJohn Johansen * implicit read and executable mmap which are required to 442b5e95b48SJohn Johansen * actually execute the image. 443b5e95b48SJohn Johansen */ 444b5e95b48SJohn Johansen if (current->in_execve) { 44555a26ebfSJohn Johansen fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP; 446b5e95b48SJohn Johansen return 0; 447b5e95b48SJohn Johansen } 448b5e95b48SJohn Johansen 44994817692SAl Viro label = aa_get_newest_cred_label(file->f_cred); 450637f688dSJohn Johansen if (!unconfined(label)) { 4513cee6079SChristian Brauner struct user_namespace *mnt_userns = file_mnt_user_ns(file); 452496ad9aaSAl Viro struct inode *inode = file_inode(file); 4533cee6079SChristian Brauner struct path_cond cond = { 4543cee6079SChristian Brauner i_uid_into_mnt(mnt_userns, inode), 4553cee6079SChristian Brauner inode->i_mode 4563cee6079SChristian Brauner }; 457b5e95b48SJohn Johansen 458aebd873eSJohn Johansen error = aa_path_perm(OP_OPEN, label, &file->f_path, 0, 459b5e95b48SJohn Johansen aa_map_file_to_perms(file), &cond); 460b5e95b48SJohn Johansen /* todo cache full allowed permissions set and state */ 46155a26ebfSJohn Johansen fctx->allow = aa_map_file_to_perms(file); 462b5e95b48SJohn Johansen } 463637f688dSJohn Johansen aa_put_label(label); 464b5e95b48SJohn Johansen 465b5e95b48SJohn Johansen return error; 466b5e95b48SJohn Johansen } 467b5e95b48SJohn Johansen 468b5e95b48SJohn Johansen static int apparmor_file_alloc_security(struct file *file) 469b5e95b48SJohn Johansen { 47033bf60caSCasey Schaufler struct aa_file_ctx *ctx = file_ctx(file); 471637f688dSJohn Johansen struct aa_label *label = begin_current_label_crit_section(); 472b5e95b48SJohn Johansen 47333bf60caSCasey Schaufler spin_lock_init(&ctx->lock); 47433bf60caSCasey Schaufler rcu_assign_pointer(ctx->label, aa_get_label(label)); 47533bf60caSCasey Schaufler end_current_label_crit_section(label); 47633bf60caSCasey Schaufler return 0; 477b5e95b48SJohn Johansen } 478b5e95b48SJohn Johansen 479b5e95b48SJohn Johansen static void apparmor_file_free_security(struct file *file) 480b5e95b48SJohn Johansen { 48133bf60caSCasey Schaufler struct aa_file_ctx *ctx = file_ctx(file); 48233bf60caSCasey Schaufler 48333bf60caSCasey Schaufler if (ctx) 48433bf60caSCasey Schaufler aa_put_label(rcu_access_pointer(ctx->label)); 485b5e95b48SJohn Johansen } 486b5e95b48SJohn Johansen 487341c1fdaSJohn Johansen static int common_file_perm(const char *op, struct file *file, u32 mask, 488341c1fdaSJohn Johansen bool in_atomic) 489b5e95b48SJohn Johansen { 490190a9518SJohn Johansen struct aa_label *label; 491b5e95b48SJohn Johansen int error = 0; 492b5e95b48SJohn Johansen 493192ca6b5SJohn Johansen /* don't reaudit files closed during inheritance */ 494192ca6b5SJohn Johansen if (file->f_path.dentry == aa_null.dentry) 495192ca6b5SJohn Johansen return -EACCES; 496192ca6b5SJohn Johansen 497637f688dSJohn Johansen label = __begin_current_label_crit_section(); 498341c1fdaSJohn Johansen error = aa_file_perm(op, label, file, mask, in_atomic); 499637f688dSJohn Johansen __end_current_label_crit_section(label); 500b5e95b48SJohn Johansen 501b5e95b48SJohn Johansen return error; 502b5e95b48SJohn Johansen } 503b5e95b48SJohn Johansen 504064dc947SJohn Johansen static int apparmor_file_receive(struct file *file) 505064dc947SJohn Johansen { 506341c1fdaSJohn Johansen return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file), 507341c1fdaSJohn Johansen false); 508064dc947SJohn Johansen } 509064dc947SJohn Johansen 510b5e95b48SJohn Johansen static int apparmor_file_permission(struct file *file, int mask) 511b5e95b48SJohn Johansen { 512341c1fdaSJohn Johansen return common_file_perm(OP_FPERM, file, mask, false); 513b5e95b48SJohn Johansen } 514b5e95b48SJohn Johansen 515b5e95b48SJohn Johansen static int apparmor_file_lock(struct file *file, unsigned int cmd) 516b5e95b48SJohn Johansen { 517b5e95b48SJohn Johansen u32 mask = AA_MAY_LOCK; 518b5e95b48SJohn Johansen 519b5e95b48SJohn Johansen if (cmd == F_WRLCK) 520b5e95b48SJohn Johansen mask |= MAY_WRITE; 521b5e95b48SJohn Johansen 522341c1fdaSJohn Johansen return common_file_perm(OP_FLOCK, file, mask, false); 523b5e95b48SJohn Johansen } 524b5e95b48SJohn Johansen 52547f6e5ccSJohn Johansen static int common_mmap(const char *op, struct file *file, unsigned long prot, 526341c1fdaSJohn Johansen unsigned long flags, bool in_atomic) 527b5e95b48SJohn Johansen { 528b5e95b48SJohn Johansen int mask = 0; 529b5e95b48SJohn Johansen 530637f688dSJohn Johansen if (!file || !file_ctx(file)) 531b5e95b48SJohn Johansen return 0; 532b5e95b48SJohn Johansen 533b5e95b48SJohn Johansen if (prot & PROT_READ) 534b5e95b48SJohn Johansen mask |= MAY_READ; 535b5e95b48SJohn Johansen /* 536b5e95b48SJohn Johansen * Private mappings don't require write perms since they don't 537b5e95b48SJohn Johansen * write back to the files 538b5e95b48SJohn Johansen */ 539b5e95b48SJohn Johansen if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE)) 540b5e95b48SJohn Johansen mask |= MAY_WRITE; 541b5e95b48SJohn Johansen if (prot & PROT_EXEC) 542b5e95b48SJohn Johansen mask |= AA_EXEC_MMAP; 543b5e95b48SJohn Johansen 544341c1fdaSJohn Johansen return common_file_perm(op, file, mask, in_atomic); 545b5e95b48SJohn Johansen } 546b5e95b48SJohn Johansen 547e5467859SAl Viro static int apparmor_mmap_file(struct file *file, unsigned long reqprot, 548e5467859SAl Viro unsigned long prot, unsigned long flags) 549b5e95b48SJohn Johansen { 550341c1fdaSJohn Johansen return common_mmap(OP_FMMAP, file, prot, flags, GFP_ATOMIC); 551b5e95b48SJohn Johansen } 552b5e95b48SJohn Johansen 553b5e95b48SJohn Johansen static int apparmor_file_mprotect(struct vm_area_struct *vma, 554b5e95b48SJohn Johansen unsigned long reqprot, unsigned long prot) 555b5e95b48SJohn Johansen { 556b5e95b48SJohn Johansen return common_mmap(OP_FMPROT, vma->vm_file, prot, 557341c1fdaSJohn Johansen !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0, 558341c1fdaSJohn Johansen false); 559b5e95b48SJohn Johansen } 560b5e95b48SJohn Johansen 5612ea3ffb7SJohn Johansen static int apparmor_sb_mount(const char *dev_name, const struct path *path, 5622ea3ffb7SJohn Johansen const char *type, unsigned long flags, void *data) 5632ea3ffb7SJohn Johansen { 5642ea3ffb7SJohn Johansen struct aa_label *label; 5652ea3ffb7SJohn Johansen int error = 0; 5662ea3ffb7SJohn Johansen 5672ea3ffb7SJohn Johansen /* Discard magic */ 5682ea3ffb7SJohn Johansen if ((flags & MS_MGC_MSK) == MS_MGC_VAL) 5692ea3ffb7SJohn Johansen flags &= ~MS_MGC_MSK; 5702ea3ffb7SJohn Johansen 5712ea3ffb7SJohn Johansen flags &= ~AA_MS_IGNORE_MASK; 5722ea3ffb7SJohn Johansen 5732ea3ffb7SJohn Johansen label = __begin_current_label_crit_section(); 5742ea3ffb7SJohn Johansen if (!unconfined(label)) { 5752ea3ffb7SJohn Johansen if (flags & MS_REMOUNT) 5762ea3ffb7SJohn Johansen error = aa_remount(label, path, flags, data); 5772ea3ffb7SJohn Johansen else if (flags & MS_BIND) 5782ea3ffb7SJohn Johansen error = aa_bind_mount(label, path, dev_name, flags); 5792ea3ffb7SJohn Johansen else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | 5802ea3ffb7SJohn Johansen MS_UNBINDABLE)) 5812ea3ffb7SJohn Johansen error = aa_mount_change_type(label, path, flags); 5822ea3ffb7SJohn Johansen else if (flags & MS_MOVE) 5832ea3ffb7SJohn Johansen error = aa_move_mount(label, path, dev_name); 5842ea3ffb7SJohn Johansen else 5852ea3ffb7SJohn Johansen error = aa_new_mount(label, dev_name, path, type, 5862ea3ffb7SJohn Johansen flags, data); 5872ea3ffb7SJohn Johansen } 5882ea3ffb7SJohn Johansen __end_current_label_crit_section(label); 5892ea3ffb7SJohn Johansen 5902ea3ffb7SJohn Johansen return error; 5912ea3ffb7SJohn Johansen } 5922ea3ffb7SJohn Johansen 5932ea3ffb7SJohn Johansen static int apparmor_sb_umount(struct vfsmount *mnt, int flags) 5942ea3ffb7SJohn Johansen { 5952ea3ffb7SJohn Johansen struct aa_label *label; 5962ea3ffb7SJohn Johansen int error = 0; 5972ea3ffb7SJohn Johansen 5982ea3ffb7SJohn Johansen label = __begin_current_label_crit_section(); 5992ea3ffb7SJohn Johansen if (!unconfined(label)) 6002ea3ffb7SJohn Johansen error = aa_umount(label, mnt, flags); 6012ea3ffb7SJohn Johansen __end_current_label_crit_section(label); 6022ea3ffb7SJohn Johansen 6032ea3ffb7SJohn Johansen return error; 6042ea3ffb7SJohn Johansen } 6052ea3ffb7SJohn Johansen 6062ea3ffb7SJohn Johansen static int apparmor_sb_pivotroot(const struct path *old_path, 6072ea3ffb7SJohn Johansen const struct path *new_path) 6082ea3ffb7SJohn Johansen { 6092ea3ffb7SJohn Johansen struct aa_label *label; 6102ea3ffb7SJohn Johansen int error = 0; 6112ea3ffb7SJohn Johansen 6122ea3ffb7SJohn Johansen label = aa_get_current_label(); 6132ea3ffb7SJohn Johansen if (!unconfined(label)) 6142ea3ffb7SJohn Johansen error = aa_pivotroot(label, old_path, new_path); 6152ea3ffb7SJohn Johansen aa_put_label(label); 6162ea3ffb7SJohn Johansen 6172ea3ffb7SJohn Johansen return error; 6182ea3ffb7SJohn Johansen } 6192ea3ffb7SJohn Johansen 620b5e95b48SJohn Johansen static int apparmor_getprocattr(struct task_struct *task, char *name, 621b5e95b48SJohn Johansen char **value) 622b5e95b48SJohn Johansen { 623b5e95b48SJohn Johansen int error = -ENOENT; 624b5e95b48SJohn Johansen /* released below */ 625b5e95b48SJohn Johansen const struct cred *cred = get_task_cred(task); 626de62de59SJohn Johansen struct aa_task_ctx *ctx = task_ctx(current); 627637f688dSJohn Johansen struct aa_label *label = NULL; 628b5e95b48SJohn Johansen 629b5e95b48SJohn Johansen if (strcmp(name, "current") == 0) 630d9087c49SJohn Johansen label = aa_get_newest_label(cred_label(cred)); 63155a26ebfSJohn Johansen else if (strcmp(name, "prev") == 0 && ctx->previous) 632637f688dSJohn Johansen label = aa_get_newest_label(ctx->previous); 63355a26ebfSJohn Johansen else if (strcmp(name, "exec") == 0 && ctx->onexec) 634637f688dSJohn Johansen label = aa_get_newest_label(ctx->onexec); 635b5e95b48SJohn Johansen else 636b5e95b48SJohn Johansen error = -EINVAL; 637b5e95b48SJohn Johansen 638637f688dSJohn Johansen if (label) 63976a1d263SJohn Johansen error = aa_getprocattr(label, value); 64077b071b3SJohn Johansen 641637f688dSJohn Johansen aa_put_label(label); 642b5e95b48SJohn Johansen put_cred(cred); 643b5e95b48SJohn Johansen 644b5e95b48SJohn Johansen return error; 645b5e95b48SJohn Johansen } 646b5e95b48SJohn Johansen 647b21507e2SStephen Smalley static int apparmor_setprocattr(const char *name, void *value, 648b21507e2SStephen Smalley size_t size) 649b5e95b48SJohn Johansen { 650e89b8081SVegard Nossum char *command, *largs = NULL, *args = value; 651b5e95b48SJohn Johansen size_t arg_size; 652b5e95b48SJohn Johansen int error; 6538c4b785aSJohn Johansen DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, AA_CLASS_NONE, 6548c4b785aSJohn Johansen OP_SETPROCATTR); 655b5e95b48SJohn Johansen 656b5e95b48SJohn Johansen if (size == 0) 657b5e95b48SJohn Johansen return -EINVAL; 658b5e95b48SJohn Johansen 659e89b8081SVegard Nossum /* AppArmor requires that the buffer must be null terminated atm */ 660e89b8081SVegard Nossum if (args[size - 1] != '\0') { 661e89b8081SVegard Nossum /* null terminate */ 662e89b8081SVegard Nossum largs = args = kmalloc(size + 1, GFP_KERNEL); 663e89b8081SVegard Nossum if (!args) 664e89b8081SVegard Nossum return -ENOMEM; 665e89b8081SVegard Nossum memcpy(args, value, size); 666e89b8081SVegard Nossum args[size] = '\0'; 667e89b8081SVegard Nossum } 668e89b8081SVegard Nossum 669e89b8081SVegard Nossum error = -EINVAL; 670b5e95b48SJohn Johansen args = strim(args); 671b5e95b48SJohn Johansen command = strsep(&args, " "); 672b5e95b48SJohn Johansen if (!args) 673e89b8081SVegard Nossum goto out; 674b5e95b48SJohn Johansen args = skip_spaces(args); 675b5e95b48SJohn Johansen if (!*args) 676e89b8081SVegard Nossum goto out; 677b5e95b48SJohn Johansen 678d4d03f74SJohn Johansen arg_size = size - (args - (largs ? largs : (char *) value)); 679b5e95b48SJohn Johansen if (strcmp(name, "current") == 0) { 680b5e95b48SJohn Johansen if (strcmp(command, "changehat") == 0) { 681b5e95b48SJohn Johansen error = aa_setprocattr_changehat(args, arg_size, 682df8073c6SJohn Johansen AA_CHANGE_NOFLAGS); 683b5e95b48SJohn Johansen } else if (strcmp(command, "permhat") == 0) { 684b5e95b48SJohn Johansen error = aa_setprocattr_changehat(args, arg_size, 685df8073c6SJohn Johansen AA_CHANGE_TEST); 686b5e95b48SJohn Johansen } else if (strcmp(command, "changeprofile") == 0) { 687df8073c6SJohn Johansen error = aa_change_profile(args, AA_CHANGE_NOFLAGS); 688b5e95b48SJohn Johansen } else if (strcmp(command, "permprofile") == 0) { 689df8073c6SJohn Johansen error = aa_change_profile(args, AA_CHANGE_TEST); 6906c5fc8f1SJohn Johansen } else if (strcmp(command, "stack") == 0) { 6916c5fc8f1SJohn Johansen error = aa_change_profile(args, AA_CHANGE_STACK); 6923eea57c2SJohn Johansen } else 6933eea57c2SJohn Johansen goto fail; 694b5e95b48SJohn Johansen } else if (strcmp(name, "exec") == 0) { 6953eea57c2SJohn Johansen if (strcmp(command, "exec") == 0) 696df8073c6SJohn Johansen error = aa_change_profile(args, AA_CHANGE_ONEXEC); 6976c5fc8f1SJohn Johansen else if (strcmp(command, "stack") == 0) 6986c5fc8f1SJohn Johansen error = aa_change_profile(args, (AA_CHANGE_ONEXEC | 6996c5fc8f1SJohn Johansen AA_CHANGE_STACK)); 7003eea57c2SJohn Johansen else 7013eea57c2SJohn Johansen goto fail; 7023eea57c2SJohn Johansen } else 703b5e95b48SJohn Johansen /* only support the "current" and "exec" process attributes */ 704e89b8081SVegard Nossum goto fail; 7053eea57c2SJohn Johansen 706b5e95b48SJohn Johansen if (!error) 707b5e95b48SJohn Johansen error = size; 708e89b8081SVegard Nossum out: 709e89b8081SVegard Nossum kfree(largs); 710b5e95b48SJohn Johansen return error; 7113eea57c2SJohn Johansen 7123eea57c2SJohn Johansen fail: 713637f688dSJohn Johansen aad(&sa)->label = begin_current_label_crit_section(); 714ef88a7acSJohn Johansen aad(&sa)->info = name; 715ef88a7acSJohn Johansen aad(&sa)->error = error = -EINVAL; 7163eea57c2SJohn Johansen aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL); 717637f688dSJohn Johansen end_current_label_crit_section(aad(&sa)->label); 718e89b8081SVegard Nossum goto out; 719b5e95b48SJohn Johansen } 720b5e95b48SJohn Johansen 721fe864821SJohn Johansen /** 722fe864821SJohn Johansen * apparmor_bprm_committing_creds - do task cleanup on committing new creds 723fe864821SJohn Johansen * @bprm: binprm for the exec (NOT NULL) 724fe864821SJohn Johansen */ 725fe864821SJohn Johansen static void apparmor_bprm_committing_creds(struct linux_binprm *bprm) 726fe864821SJohn Johansen { 727637f688dSJohn Johansen struct aa_label *label = aa_current_raw_label(); 728d9087c49SJohn Johansen struct aa_label *new_label = cred_label(bprm->cred); 729fe864821SJohn Johansen 730fe864821SJohn Johansen /* bail out if unconfined or not changing profile */ 731d9087c49SJohn Johansen if ((new_label->proxy == label->proxy) || 732d9087c49SJohn Johansen (unconfined(new_label))) 733fe864821SJohn Johansen return; 734fe864821SJohn Johansen 735192ca6b5SJohn Johansen aa_inherit_files(bprm->cred, current->files); 736192ca6b5SJohn Johansen 737fe864821SJohn Johansen current->pdeath_signal = 0; 738fe864821SJohn Johansen 739637f688dSJohn Johansen /* reset soft limits and set hard limits for the new label */ 740d9087c49SJohn Johansen __aa_transition_rlimits(label, new_label); 741fe864821SJohn Johansen } 742fe864821SJohn Johansen 743fe864821SJohn Johansen /** 744391f1211SJiapeng Chong * apparmor_bprm_committed_creds() - do cleanup after new creds committed 745fe864821SJohn Johansen * @bprm: binprm for the exec (NOT NULL) 746fe864821SJohn Johansen */ 747fe864821SJohn Johansen static void apparmor_bprm_committed_creds(struct linux_binprm *bprm) 748fe864821SJohn Johansen { 7493b529a76SJohn Johansen /* clear out temporary/transitional state from the context */ 750de62de59SJohn Johansen aa_clear_task_ctx_trans(task_ctx(current)); 7513b529a76SJohn Johansen 752fe864821SJohn Johansen return; 753fe864821SJohn Johansen } 754fe864821SJohn Johansen 7556326948fSPaul Moore static void apparmor_current_getsecid_subj(u32 *secid) 7566326948fSPaul Moore { 7576326948fSPaul Moore struct aa_label *label = aa_get_current_label(); 7586326948fSPaul Moore *secid = label->secid; 7596326948fSPaul Moore aa_put_label(label); 7606326948fSPaul Moore } 7616326948fSPaul Moore 7626326948fSPaul Moore static void apparmor_task_getsecid_obj(struct task_struct *p, u32 *secid) 763a7ae3645SJohn Johansen { 764a7ae3645SJohn Johansen struct aa_label *label = aa_get_task_label(p); 765a7ae3645SJohn Johansen *secid = label->secid; 766a7ae3645SJohn Johansen aa_put_label(label); 767a7ae3645SJohn Johansen } 768a7ae3645SJohn Johansen 7697cb4dc9fSJiri Slaby static int apparmor_task_setrlimit(struct task_struct *task, 7707cb4dc9fSJiri Slaby unsigned int resource, struct rlimit *new_rlim) 771b5e95b48SJohn Johansen { 772637f688dSJohn Johansen struct aa_label *label = __begin_current_label_crit_section(); 773b5e95b48SJohn Johansen int error = 0; 774b5e95b48SJohn Johansen 775637f688dSJohn Johansen if (!unconfined(label)) 77686b92cb7SJohn Johansen error = aa_task_setrlimit(label, task, resource, new_rlim); 777637f688dSJohn Johansen __end_current_label_crit_section(label); 778b5e95b48SJohn Johansen 779b5e95b48SJohn Johansen return error; 780b5e95b48SJohn Johansen } 781b5e95b48SJohn Johansen 782ae7795bcSEric W. Biederman static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info, 7836b4f3d01SStephen Smalley int sig, const struct cred *cred) 784cd1dbf76SJohn Johansen { 785cd1dbf76SJohn Johansen struct aa_label *cl, *tl; 786cd1dbf76SJohn Johansen int error; 787cd1dbf76SJohn Johansen 7886b4f3d01SStephen Smalley if (cred) { 7896b4f3d01SStephen Smalley /* 790cd1dbf76SJohn Johansen * Dealing with USB IO specific behavior 791cd1dbf76SJohn Johansen */ 7926b4f3d01SStephen Smalley cl = aa_get_newest_cred_label(cred); 7936b4f3d01SStephen Smalley tl = aa_get_task_label(target); 7946b4f3d01SStephen Smalley error = aa_may_signal(cl, tl, sig); 7956b4f3d01SStephen Smalley aa_put_label(cl); 7966b4f3d01SStephen Smalley aa_put_label(tl); 7976b4f3d01SStephen Smalley return error; 7986b4f3d01SStephen Smalley } 7996b4f3d01SStephen Smalley 800cd1dbf76SJohn Johansen cl = __begin_current_label_crit_section(); 801cd1dbf76SJohn Johansen tl = aa_get_task_label(target); 802cd1dbf76SJohn Johansen error = aa_may_signal(cl, tl, sig); 803cd1dbf76SJohn Johansen aa_put_label(tl); 804cd1dbf76SJohn Johansen __end_current_label_crit_section(cl); 805cd1dbf76SJohn Johansen 806cd1dbf76SJohn Johansen return error; 807cd1dbf76SJohn Johansen } 808cd1dbf76SJohn Johansen 80956974a6fSJohn Johansen /** 81056974a6fSJohn Johansen * apparmor_sk_alloc_security - allocate and attach the sk_security field 81156974a6fSJohn Johansen */ 81256974a6fSJohn Johansen static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags) 81356974a6fSJohn Johansen { 81456974a6fSJohn Johansen struct aa_sk_ctx *ctx; 81556974a6fSJohn Johansen 81656974a6fSJohn Johansen ctx = kzalloc(sizeof(*ctx), flags); 81756974a6fSJohn Johansen if (!ctx) 81856974a6fSJohn Johansen return -ENOMEM; 81956974a6fSJohn Johansen 82056974a6fSJohn Johansen SK_CTX(sk) = ctx; 82156974a6fSJohn Johansen 82256974a6fSJohn Johansen return 0; 82356974a6fSJohn Johansen } 82456974a6fSJohn Johansen 82556974a6fSJohn Johansen /** 82656974a6fSJohn Johansen * apparmor_sk_free_security - free the sk_security field 82756974a6fSJohn Johansen */ 82856974a6fSJohn Johansen static void apparmor_sk_free_security(struct sock *sk) 82956974a6fSJohn Johansen { 83056974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sk); 83156974a6fSJohn Johansen 83256974a6fSJohn Johansen SK_CTX(sk) = NULL; 83356974a6fSJohn Johansen aa_put_label(ctx->label); 83456974a6fSJohn Johansen aa_put_label(ctx->peer); 83556974a6fSJohn Johansen kfree(ctx); 83656974a6fSJohn Johansen } 83756974a6fSJohn Johansen 83856974a6fSJohn Johansen /** 8390fc6ab40SYang Li * apparmor_sk_clone_security - clone the sk_security field 84056974a6fSJohn Johansen */ 84156974a6fSJohn Johansen static void apparmor_sk_clone_security(const struct sock *sk, 84256974a6fSJohn Johansen struct sock *newsk) 84356974a6fSJohn Johansen { 84456974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sk); 84556974a6fSJohn Johansen struct aa_sk_ctx *new = SK_CTX(newsk); 84656974a6fSJohn Johansen 8473b646abcSMauricio Faria de Oliveira if (new->label) 8483b646abcSMauricio Faria de Oliveira aa_put_label(new->label); 84956974a6fSJohn Johansen new->label = aa_get_label(ctx->label); 8503b646abcSMauricio Faria de Oliveira 8513b646abcSMauricio Faria de Oliveira if (new->peer) 8523b646abcSMauricio Faria de Oliveira aa_put_label(new->peer); 85356974a6fSJohn Johansen new->peer = aa_get_label(ctx->peer); 85456974a6fSJohn Johansen } 85556974a6fSJohn Johansen 85656974a6fSJohn Johansen /** 85756974a6fSJohn Johansen * apparmor_socket_create - check perms before creating a new socket 85856974a6fSJohn Johansen */ 85956974a6fSJohn Johansen static int apparmor_socket_create(int family, int type, int protocol, int kern) 86056974a6fSJohn Johansen { 86156974a6fSJohn Johansen struct aa_label *label; 86256974a6fSJohn Johansen int error = 0; 86356974a6fSJohn Johansen 86456974a6fSJohn Johansen AA_BUG(in_interrupt()); 86556974a6fSJohn Johansen 86656974a6fSJohn Johansen label = begin_current_label_crit_section(); 86756974a6fSJohn Johansen if (!(kern || unconfined(label))) 86856974a6fSJohn Johansen error = af_select(family, 86956974a6fSJohn Johansen create_perm(label, family, type, protocol), 87056974a6fSJohn Johansen aa_af_perm(label, OP_CREATE, AA_MAY_CREATE, 87156974a6fSJohn Johansen family, type, protocol)); 87256974a6fSJohn Johansen end_current_label_crit_section(label); 87356974a6fSJohn Johansen 87456974a6fSJohn Johansen return error; 87556974a6fSJohn Johansen } 87656974a6fSJohn Johansen 87756974a6fSJohn Johansen /** 87856974a6fSJohn Johansen * apparmor_socket_post_create - setup the per-socket security struct 87956974a6fSJohn Johansen * 88056974a6fSJohn Johansen * Note: 88156974a6fSJohn Johansen * - kernel sockets currently labeled unconfined but we may want to 88256974a6fSJohn Johansen * move to a special kernel label 88356974a6fSJohn Johansen * - socket may not have sk here if created with sock_create_lite or 88456974a6fSJohn Johansen * sock_alloc. These should be accept cases which will be handled in 88556974a6fSJohn Johansen * sock_graft. 88656974a6fSJohn Johansen */ 88756974a6fSJohn Johansen static int apparmor_socket_post_create(struct socket *sock, int family, 88856974a6fSJohn Johansen int type, int protocol, int kern) 88956974a6fSJohn Johansen { 89056974a6fSJohn Johansen struct aa_label *label; 89156974a6fSJohn Johansen 89256974a6fSJohn Johansen if (kern) { 89395c0581fSJohn Johansen label = aa_get_label(kernel_t); 89456974a6fSJohn Johansen } else 89556974a6fSJohn Johansen label = aa_get_current_label(); 89656974a6fSJohn Johansen 89756974a6fSJohn Johansen if (sock->sk) { 89856974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sock->sk); 89956974a6fSJohn Johansen 90056974a6fSJohn Johansen aa_put_label(ctx->label); 90156974a6fSJohn Johansen ctx->label = aa_get_label(label); 90256974a6fSJohn Johansen } 90356974a6fSJohn Johansen aa_put_label(label); 90456974a6fSJohn Johansen 90556974a6fSJohn Johansen return 0; 90656974a6fSJohn Johansen } 90756974a6fSJohn Johansen 90856974a6fSJohn Johansen /** 90956974a6fSJohn Johansen * apparmor_socket_bind - check perms before bind addr to socket 91056974a6fSJohn Johansen */ 91156974a6fSJohn Johansen static int apparmor_socket_bind(struct socket *sock, 91256974a6fSJohn Johansen struct sockaddr *address, int addrlen) 91356974a6fSJohn Johansen { 91456974a6fSJohn Johansen AA_BUG(!sock); 91556974a6fSJohn Johansen AA_BUG(!sock->sk); 91656974a6fSJohn Johansen AA_BUG(!address); 91756974a6fSJohn Johansen AA_BUG(in_interrupt()); 91856974a6fSJohn Johansen 91956974a6fSJohn Johansen return af_select(sock->sk->sk_family, 92056974a6fSJohn Johansen bind_perm(sock, address, addrlen), 92156974a6fSJohn Johansen aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk)); 92256974a6fSJohn Johansen } 92356974a6fSJohn Johansen 92456974a6fSJohn Johansen /** 92556974a6fSJohn Johansen * apparmor_socket_connect - check perms before connecting @sock to @address 92656974a6fSJohn Johansen */ 92756974a6fSJohn Johansen static int apparmor_socket_connect(struct socket *sock, 92856974a6fSJohn Johansen struct sockaddr *address, int addrlen) 92956974a6fSJohn Johansen { 93056974a6fSJohn Johansen AA_BUG(!sock); 93156974a6fSJohn Johansen AA_BUG(!sock->sk); 93256974a6fSJohn Johansen AA_BUG(!address); 93356974a6fSJohn Johansen AA_BUG(in_interrupt()); 93456974a6fSJohn Johansen 93556974a6fSJohn Johansen return af_select(sock->sk->sk_family, 93656974a6fSJohn Johansen connect_perm(sock, address, addrlen), 93756974a6fSJohn Johansen aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk)); 93856974a6fSJohn Johansen } 93956974a6fSJohn Johansen 94056974a6fSJohn Johansen /** 9410fc6ab40SYang Li * apparmor_socket_listen - check perms before allowing listen 94256974a6fSJohn Johansen */ 94356974a6fSJohn Johansen static int apparmor_socket_listen(struct socket *sock, int backlog) 94456974a6fSJohn Johansen { 94556974a6fSJohn Johansen AA_BUG(!sock); 94656974a6fSJohn Johansen AA_BUG(!sock->sk); 94756974a6fSJohn Johansen AA_BUG(in_interrupt()); 94856974a6fSJohn Johansen 94956974a6fSJohn Johansen return af_select(sock->sk->sk_family, 95056974a6fSJohn Johansen listen_perm(sock, backlog), 95156974a6fSJohn Johansen aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk)); 95256974a6fSJohn Johansen } 95356974a6fSJohn Johansen 95456974a6fSJohn Johansen /** 95556974a6fSJohn Johansen * apparmor_socket_accept - check perms before accepting a new connection. 95656974a6fSJohn Johansen * 95756974a6fSJohn Johansen * Note: while @newsock is created and has some information, the accept 95856974a6fSJohn Johansen * has not been done. 95956974a6fSJohn Johansen */ 96056974a6fSJohn Johansen static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) 96156974a6fSJohn Johansen { 96256974a6fSJohn Johansen AA_BUG(!sock); 96356974a6fSJohn Johansen AA_BUG(!sock->sk); 96456974a6fSJohn Johansen AA_BUG(!newsock); 96556974a6fSJohn Johansen AA_BUG(in_interrupt()); 96656974a6fSJohn Johansen 96756974a6fSJohn Johansen return af_select(sock->sk->sk_family, 96856974a6fSJohn Johansen accept_perm(sock, newsock), 96956974a6fSJohn Johansen aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk)); 97056974a6fSJohn Johansen } 97156974a6fSJohn Johansen 97256974a6fSJohn Johansen static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, 97356974a6fSJohn Johansen struct msghdr *msg, int size) 97456974a6fSJohn Johansen { 97556974a6fSJohn Johansen AA_BUG(!sock); 97656974a6fSJohn Johansen AA_BUG(!sock->sk); 97756974a6fSJohn Johansen AA_BUG(!msg); 97856974a6fSJohn Johansen AA_BUG(in_interrupt()); 97956974a6fSJohn Johansen 98056974a6fSJohn Johansen return af_select(sock->sk->sk_family, 98156974a6fSJohn Johansen msg_perm(op, request, sock, msg, size), 98256974a6fSJohn Johansen aa_sk_perm(op, request, sock->sk)); 98356974a6fSJohn Johansen } 98456974a6fSJohn Johansen 98556974a6fSJohn Johansen /** 98656974a6fSJohn Johansen * apparmor_socket_sendmsg - check perms before sending msg to another socket 98756974a6fSJohn Johansen */ 98856974a6fSJohn Johansen static int apparmor_socket_sendmsg(struct socket *sock, 98956974a6fSJohn Johansen struct msghdr *msg, int size) 99056974a6fSJohn Johansen { 99156974a6fSJohn Johansen return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); 99256974a6fSJohn Johansen } 99356974a6fSJohn Johansen 99456974a6fSJohn Johansen /** 99556974a6fSJohn Johansen * apparmor_socket_recvmsg - check perms before receiving a message 99656974a6fSJohn Johansen */ 99756974a6fSJohn Johansen static int apparmor_socket_recvmsg(struct socket *sock, 99856974a6fSJohn Johansen struct msghdr *msg, int size, int flags) 99956974a6fSJohn Johansen { 100056974a6fSJohn Johansen return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); 100156974a6fSJohn Johansen } 100256974a6fSJohn Johansen 100356974a6fSJohn Johansen /* revaliation, get/set attr, shutdown */ 100456974a6fSJohn Johansen static int aa_sock_perm(const char *op, u32 request, struct socket *sock) 100556974a6fSJohn Johansen { 100656974a6fSJohn Johansen AA_BUG(!sock); 100756974a6fSJohn Johansen AA_BUG(!sock->sk); 100856974a6fSJohn Johansen AA_BUG(in_interrupt()); 100956974a6fSJohn Johansen 101056974a6fSJohn Johansen return af_select(sock->sk->sk_family, 101156974a6fSJohn Johansen sock_perm(op, request, sock), 101256974a6fSJohn Johansen aa_sk_perm(op, request, sock->sk)); 101356974a6fSJohn Johansen } 101456974a6fSJohn Johansen 101556974a6fSJohn Johansen /** 101656974a6fSJohn Johansen * apparmor_socket_getsockname - check perms before getting the local address 101756974a6fSJohn Johansen */ 101856974a6fSJohn Johansen static int apparmor_socket_getsockname(struct socket *sock) 101956974a6fSJohn Johansen { 102056974a6fSJohn Johansen return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); 102156974a6fSJohn Johansen } 102256974a6fSJohn Johansen 102356974a6fSJohn Johansen /** 102456974a6fSJohn Johansen * apparmor_socket_getpeername - check perms before getting remote address 102556974a6fSJohn Johansen */ 102656974a6fSJohn Johansen static int apparmor_socket_getpeername(struct socket *sock) 102756974a6fSJohn Johansen { 102856974a6fSJohn Johansen return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); 102956974a6fSJohn Johansen } 103056974a6fSJohn Johansen 103156974a6fSJohn Johansen /* revaliation, get/set attr, opt */ 103256974a6fSJohn Johansen static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, 103356974a6fSJohn Johansen int level, int optname) 103456974a6fSJohn Johansen { 103556974a6fSJohn Johansen AA_BUG(!sock); 103656974a6fSJohn Johansen AA_BUG(!sock->sk); 103756974a6fSJohn Johansen AA_BUG(in_interrupt()); 103856974a6fSJohn Johansen 103956974a6fSJohn Johansen return af_select(sock->sk->sk_family, 104056974a6fSJohn Johansen opt_perm(op, request, sock, level, optname), 104156974a6fSJohn Johansen aa_sk_perm(op, request, sock->sk)); 104256974a6fSJohn Johansen } 104356974a6fSJohn Johansen 104456974a6fSJohn Johansen /** 10450fc6ab40SYang Li * apparmor_socket_getsockopt - check perms before getting socket options 104656974a6fSJohn Johansen */ 104756974a6fSJohn Johansen static int apparmor_socket_getsockopt(struct socket *sock, int level, 104856974a6fSJohn Johansen int optname) 104956974a6fSJohn Johansen { 105056974a6fSJohn Johansen return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, 105156974a6fSJohn Johansen level, optname); 105256974a6fSJohn Johansen } 105356974a6fSJohn Johansen 105456974a6fSJohn Johansen /** 10550fc6ab40SYang Li * apparmor_socket_setsockopt - check perms before setting socket options 105656974a6fSJohn Johansen */ 105756974a6fSJohn Johansen static int apparmor_socket_setsockopt(struct socket *sock, int level, 105856974a6fSJohn Johansen int optname) 105956974a6fSJohn Johansen { 106056974a6fSJohn Johansen return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, 106156974a6fSJohn Johansen level, optname); 106256974a6fSJohn Johansen } 106356974a6fSJohn Johansen 106456974a6fSJohn Johansen /** 106556974a6fSJohn Johansen * apparmor_socket_shutdown - check perms before shutting down @sock conn 106656974a6fSJohn Johansen */ 106756974a6fSJohn Johansen static int apparmor_socket_shutdown(struct socket *sock, int how) 106856974a6fSJohn Johansen { 106956974a6fSJohn Johansen return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); 107056974a6fSJohn Johansen } 107156974a6fSJohn Johansen 1072e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK 107356974a6fSJohn Johansen /** 10740fc6ab40SYang Li * apparmor_socket_sock_rcv_skb - check perms before associating skb to sk 107556974a6fSJohn Johansen * 107656974a6fSJohn Johansen * Note: can not sleep may be called with locks held 107756974a6fSJohn Johansen * 107856974a6fSJohn Johansen * dont want protocol specific in __skb_recv_datagram() 107956974a6fSJohn Johansen * to deny an incoming connection socket_sock_rcv_skb() 108056974a6fSJohn Johansen */ 108156974a6fSJohn Johansen static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) 108256974a6fSJohn Johansen { 1083ab9f2115SMatthew Garrett struct aa_sk_ctx *ctx = SK_CTX(sk); 1084ab9f2115SMatthew Garrett 1085ab9f2115SMatthew Garrett if (!skb->secmark) 108656974a6fSJohn Johansen return 0; 1087ab9f2115SMatthew Garrett 1088ab9f2115SMatthew Garrett return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, 1089ab9f2115SMatthew Garrett skb->secmark, sk); 109056974a6fSJohn Johansen } 1091e1af4779SArnd Bergmann #endif 109256974a6fSJohn Johansen 109356974a6fSJohn Johansen 109456974a6fSJohn Johansen static struct aa_label *sk_peer_label(struct sock *sk) 109556974a6fSJohn Johansen { 109656974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sk); 109756974a6fSJohn Johansen 109856974a6fSJohn Johansen if (ctx->peer) 109956974a6fSJohn Johansen return ctx->peer; 110056974a6fSJohn Johansen 110156974a6fSJohn Johansen return ERR_PTR(-ENOPROTOOPT); 110256974a6fSJohn Johansen } 110356974a6fSJohn Johansen 110456974a6fSJohn Johansen /** 110556974a6fSJohn Johansen * apparmor_socket_getpeersec_stream - get security context of peer 110656974a6fSJohn Johansen * 110756974a6fSJohn Johansen * Note: for tcp only valid if using ipsec or cipso on lan 110856974a6fSJohn Johansen */ 110956974a6fSJohn Johansen static int apparmor_socket_getpeersec_stream(struct socket *sock, 111056974a6fSJohn Johansen char __user *optval, 111156974a6fSJohn Johansen int __user *optlen, 111256974a6fSJohn Johansen unsigned int len) 111356974a6fSJohn Johansen { 111456974a6fSJohn Johansen char *name; 111556974a6fSJohn Johansen int slen, error = 0; 111656974a6fSJohn Johansen struct aa_label *label; 111756974a6fSJohn Johansen struct aa_label *peer; 111856974a6fSJohn Johansen 111956974a6fSJohn Johansen label = begin_current_label_crit_section(); 112056974a6fSJohn Johansen peer = sk_peer_label(sock->sk); 112156974a6fSJohn Johansen if (IS_ERR(peer)) { 112256974a6fSJohn Johansen error = PTR_ERR(peer); 112356974a6fSJohn Johansen goto done; 112456974a6fSJohn Johansen } 112556974a6fSJohn Johansen slen = aa_label_asxprint(&name, labels_ns(label), peer, 112656974a6fSJohn Johansen FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | 112756974a6fSJohn Johansen FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); 112856974a6fSJohn Johansen /* don't include terminating \0 in slen, it breaks some apps */ 112956974a6fSJohn Johansen if (slen < 0) { 113056974a6fSJohn Johansen error = -ENOMEM; 113156974a6fSJohn Johansen } else { 113256974a6fSJohn Johansen if (slen > len) { 113356974a6fSJohn Johansen error = -ERANGE; 113456974a6fSJohn Johansen } else if (copy_to_user(optval, name, slen)) { 113556974a6fSJohn Johansen error = -EFAULT; 113656974a6fSJohn Johansen goto out; 113756974a6fSJohn Johansen } 113856974a6fSJohn Johansen if (put_user(slen, optlen)) 113956974a6fSJohn Johansen error = -EFAULT; 114056974a6fSJohn Johansen out: 114156974a6fSJohn Johansen kfree(name); 114256974a6fSJohn Johansen 114356974a6fSJohn Johansen } 114456974a6fSJohn Johansen 114556974a6fSJohn Johansen done: 114656974a6fSJohn Johansen end_current_label_crit_section(label); 114756974a6fSJohn Johansen 114856974a6fSJohn Johansen return error; 114956974a6fSJohn Johansen } 115056974a6fSJohn Johansen 115156974a6fSJohn Johansen /** 115256974a6fSJohn Johansen * apparmor_socket_getpeersec_dgram - get security label of packet 115356974a6fSJohn Johansen * @sock: the peer socket 115456974a6fSJohn Johansen * @skb: packet data 115556974a6fSJohn Johansen * @secid: pointer to where to put the secid of the packet 115656974a6fSJohn Johansen * 115756974a6fSJohn Johansen * Sets the netlabel socket state on sk from parent 115856974a6fSJohn Johansen */ 115956974a6fSJohn Johansen static int apparmor_socket_getpeersec_dgram(struct socket *sock, 116056974a6fSJohn Johansen struct sk_buff *skb, u32 *secid) 116156974a6fSJohn Johansen 116256974a6fSJohn Johansen { 116356974a6fSJohn Johansen /* TODO: requires secid support */ 116456974a6fSJohn Johansen return -ENOPROTOOPT; 116556974a6fSJohn Johansen } 116656974a6fSJohn Johansen 116756974a6fSJohn Johansen /** 116856974a6fSJohn Johansen * apparmor_sock_graft - Initialize newly created socket 116956974a6fSJohn Johansen * @sk: child sock 117056974a6fSJohn Johansen * @parent: parent socket 117156974a6fSJohn Johansen * 117256974a6fSJohn Johansen * Note: could set off of SOCK_CTX(parent) but need to track inode and we can 117356974a6fSJohn Johansen * just set sk security information off of current creating process label 117456974a6fSJohn Johansen * Labeling of sk for accept case - probably should be sock based 117556974a6fSJohn Johansen * instead of task, because of the case where an implicitly labeled 117656974a6fSJohn Johansen * socket is shared by different tasks. 117756974a6fSJohn Johansen */ 117856974a6fSJohn Johansen static void apparmor_sock_graft(struct sock *sk, struct socket *parent) 117956974a6fSJohn Johansen { 118056974a6fSJohn Johansen struct aa_sk_ctx *ctx = SK_CTX(sk); 118156974a6fSJohn Johansen 118256974a6fSJohn Johansen if (!ctx->label) 118356974a6fSJohn Johansen ctx->label = aa_get_current_label(); 118456974a6fSJohn Johansen } 118556974a6fSJohn Johansen 1186e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK 118741dd9596SFlorian Westphal static int apparmor_inet_conn_request(const struct sock *sk, struct sk_buff *skb, 1188ab9f2115SMatthew Garrett struct request_sock *req) 1189ab9f2115SMatthew Garrett { 1190ab9f2115SMatthew Garrett struct aa_sk_ctx *ctx = SK_CTX(sk); 1191ab9f2115SMatthew Garrett 1192ab9f2115SMatthew Garrett if (!skb->secmark) 1193ab9f2115SMatthew Garrett return 0; 1194ab9f2115SMatthew Garrett 1195ab9f2115SMatthew Garrett return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, 1196ab9f2115SMatthew Garrett skb->secmark, sk); 1197ab9f2115SMatthew Garrett } 1198e1af4779SArnd Bergmann #endif 1199ab9f2115SMatthew Garrett 1200bbd3662aSCasey Schaufler /* 1201*37923d43SXiu Jianfeng * The cred blob is a pointer to, not an instance of, an aa_label. 1202bbd3662aSCasey Schaufler */ 1203bbd3662aSCasey Schaufler struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = { 1204*37923d43SXiu Jianfeng .lbs_cred = sizeof(struct aa_label *), 120533bf60caSCasey Schaufler .lbs_file = sizeof(struct aa_file_ctx), 1206f4ad8f2cSCasey Schaufler .lbs_task = sizeof(struct aa_task_ctx), 1207bbd3662aSCasey Schaufler }; 1208bbd3662aSCasey Schaufler 1209ca97d939SJames Morris static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { 1210e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), 1211e20b043aSCasey Schaufler LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), 1212e20b043aSCasey Schaufler LSM_HOOK_INIT(capget, apparmor_capget), 1213e20b043aSCasey Schaufler LSM_HOOK_INIT(capable, apparmor_capable), 1214b5e95b48SJohn Johansen 12152ea3ffb7SJohn Johansen LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), 12162ea3ffb7SJohn Johansen LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), 12172ea3ffb7SJohn Johansen LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), 12182ea3ffb7SJohn Johansen 1219e20b043aSCasey Schaufler LSM_HOOK_INIT(path_link, apparmor_path_link), 1220e20b043aSCasey Schaufler LSM_HOOK_INIT(path_unlink, apparmor_path_unlink), 1221e20b043aSCasey Schaufler LSM_HOOK_INIT(path_symlink, apparmor_path_symlink), 1222e20b043aSCasey Schaufler LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir), 1223e20b043aSCasey Schaufler LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir), 1224e20b043aSCasey Schaufler LSM_HOOK_INIT(path_mknod, apparmor_path_mknod), 1225e20b043aSCasey Schaufler LSM_HOOK_INIT(path_rename, apparmor_path_rename), 1226e20b043aSCasey Schaufler LSM_HOOK_INIT(path_chmod, apparmor_path_chmod), 1227e20b043aSCasey Schaufler LSM_HOOK_INIT(path_chown, apparmor_path_chown), 1228e20b043aSCasey Schaufler LSM_HOOK_INIT(path_truncate, apparmor_path_truncate), 1229e20b043aSCasey Schaufler LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr), 1230b5e95b48SJohn Johansen 1231e20b043aSCasey Schaufler LSM_HOOK_INIT(file_open, apparmor_file_open), 1232064dc947SJohn Johansen LSM_HOOK_INIT(file_receive, apparmor_file_receive), 1233e20b043aSCasey Schaufler LSM_HOOK_INIT(file_permission, apparmor_file_permission), 1234e20b043aSCasey Schaufler LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), 1235e20b043aSCasey Schaufler LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), 1236e20b043aSCasey Schaufler LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), 1237e20b043aSCasey Schaufler LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), 1238e20b043aSCasey Schaufler LSM_HOOK_INIT(file_lock, apparmor_file_lock), 1239b5e95b48SJohn Johansen 1240e20b043aSCasey Schaufler LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), 1241e20b043aSCasey Schaufler LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), 1242b5e95b48SJohn Johansen 124356974a6fSJohn Johansen LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security), 124456974a6fSJohn Johansen LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), 124556974a6fSJohn Johansen LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), 124656974a6fSJohn Johansen 124756974a6fSJohn Johansen LSM_HOOK_INIT(socket_create, apparmor_socket_create), 124856974a6fSJohn Johansen LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), 124956974a6fSJohn Johansen LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), 125056974a6fSJohn Johansen LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), 125156974a6fSJohn Johansen LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), 125256974a6fSJohn Johansen LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), 125356974a6fSJohn Johansen LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), 125456974a6fSJohn Johansen LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), 125556974a6fSJohn Johansen LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), 125656974a6fSJohn Johansen LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), 125756974a6fSJohn Johansen LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), 125856974a6fSJohn Johansen LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), 125956974a6fSJohn Johansen LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), 1260e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK 126156974a6fSJohn Johansen LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), 1262e1af4779SArnd Bergmann #endif 126356974a6fSJohn Johansen LSM_HOOK_INIT(socket_getpeersec_stream, 126456974a6fSJohn Johansen apparmor_socket_getpeersec_stream), 126556974a6fSJohn Johansen LSM_HOOK_INIT(socket_getpeersec_dgram, 126656974a6fSJohn Johansen apparmor_socket_getpeersec_dgram), 126756974a6fSJohn Johansen LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), 1268e1af4779SArnd Bergmann #ifdef CONFIG_NETWORK_SECMARK 1269ab9f2115SMatthew Garrett LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), 1270e1af4779SArnd Bergmann #endif 127156974a6fSJohn Johansen 1272e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), 1273e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_free, apparmor_cred_free), 1274e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), 1275e20b043aSCasey Schaufler LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), 1276b5e95b48SJohn Johansen 1277b8bff599SEric W. Biederman LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec), 1278e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), 1279e20b043aSCasey Schaufler LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), 1280b5e95b48SJohn Johansen 12813b529a76SJohn Johansen LSM_HOOK_INIT(task_free, apparmor_task_free), 12823b529a76SJohn Johansen LSM_HOOK_INIT(task_alloc, apparmor_task_alloc), 12836326948fSPaul Moore LSM_HOOK_INIT(current_getsecid_subj, apparmor_current_getsecid_subj), 12846326948fSPaul Moore LSM_HOOK_INIT(task_getsecid_obj, apparmor_task_getsecid_obj), 1285e20b043aSCasey Schaufler LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit), 1286cd1dbf76SJohn Johansen LSM_HOOK_INIT(task_kill, apparmor_task_kill), 1287c0929212SJohn Johansen 1288e79c26d0SMatthew Garrett #ifdef CONFIG_AUDIT 1289e79c26d0SMatthew Garrett LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init), 1290e79c26d0SMatthew Garrett LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known), 1291e79c26d0SMatthew Garrett LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match), 1292e79c26d0SMatthew Garrett LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free), 1293e79c26d0SMatthew Garrett #endif 1294e79c26d0SMatthew Garrett 1295c0929212SJohn Johansen LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx), 1296c0929212SJohn Johansen LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid), 1297c0929212SJohn Johansen LSM_HOOK_INIT(release_secctx, apparmor_release_secctx), 1298b5e95b48SJohn Johansen }; 1299b5e95b48SJohn Johansen 1300b5e95b48SJohn Johansen /* 1301b5e95b48SJohn Johansen * AppArmor sysfs module parameters 1302b5e95b48SJohn Johansen */ 1303b5e95b48SJohn Johansen 1304101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp); 1305101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp); 1306b8aa09fdSRusty Russell #define param_check_aabool param_check_bool 13079c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aabool = { 13086a4c2643SJani Nikula .flags = KERNEL_PARAM_OPS_FL_NOARG, 1309101d6c82SStephen Rothwell .set = param_set_aabool, 1310101d6c82SStephen Rothwell .get = param_get_aabool 1311101d6c82SStephen Rothwell }; 1312b5e95b48SJohn Johansen 1313101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp); 1314101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp); 1315b8aa09fdSRusty Russell #define param_check_aauint param_check_uint 13169c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aauint = { 1317101d6c82SStephen Rothwell .set = param_set_aauint, 1318101d6c82SStephen Rothwell .get = param_get_aauint 1319101d6c82SStephen Rothwell }; 1320b5e95b48SJohn Johansen 132163c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val, 132263c16c3aSChris Coulson const struct kernel_param *kp); 132363c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer, 132463c16c3aSChris Coulson const struct kernel_param *kp); 132563c16c3aSChris Coulson #define param_check_aacompressionlevel param_check_int 132663c16c3aSChris Coulson static const struct kernel_param_ops param_ops_aacompressionlevel = { 132763c16c3aSChris Coulson .set = param_set_aacompressionlevel, 132863c16c3aSChris Coulson .get = param_get_aacompressionlevel 132963c16c3aSChris Coulson }; 133063c16c3aSChris Coulson 1331101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); 1332101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); 1333b8aa09fdSRusty Russell #define param_check_aalockpolicy param_check_bool 13349c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_aalockpolicy = { 13356a4c2643SJani Nikula .flags = KERNEL_PARAM_OPS_FL_NOARG, 1336101d6c82SStephen Rothwell .set = param_set_aalockpolicy, 1337101d6c82SStephen Rothwell .get = param_get_aalockpolicy 1338101d6c82SStephen Rothwell }; 1339b5e95b48SJohn Johansen 1340e4dca7b7SKees Cook static int param_set_audit(const char *val, const struct kernel_param *kp); 1341e4dca7b7SKees Cook static int param_get_audit(char *buffer, const struct kernel_param *kp); 1342b5e95b48SJohn Johansen 1343e4dca7b7SKees Cook static int param_set_mode(const char *val, const struct kernel_param *kp); 1344e4dca7b7SKees Cook static int param_get_mode(char *buffer, const struct kernel_param *kp); 1345b5e95b48SJohn Johansen 1346b5e95b48SJohn Johansen /* Flag values, also controllable via /sys/module/apparmor/parameters 1347b5e95b48SJohn Johansen * We define special types as we want to do additional mediation. 1348b5e95b48SJohn Johansen */ 1349b5e95b48SJohn Johansen 1350b5e95b48SJohn Johansen /* AppArmor global enforcement switch - complain, enforce, kill */ 1351b5e95b48SJohn Johansen enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE; 1352b5e95b48SJohn Johansen module_param_call(mode, param_set_mode, param_get_mode, 1353b5e95b48SJohn Johansen &aa_g_profile_mode, S_IRUSR | S_IWUSR); 1354b5e95b48SJohn Johansen 13556059f71fSJohn Johansen /* whether policy verification hashing is enabled */ 13567616ac70SArnd Bergmann bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT); 13573ccb76c5SJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_HASH 13586059f71fSJohn Johansen module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); 13597616ac70SArnd Bergmann #endif 13606059f71fSJohn Johansen 1361d61c57fdSJohn Johansen /* whether policy exactly as loaded is retained for debug and checkpointing */ 1362d61c57fdSJohn Johansen bool aa_g_export_binary = IS_ENABLED(CONFIG_SECURITY_APPARMOR_EXPORT_BINARY); 1363d61c57fdSJohn Johansen #ifdef CONFIG_SECURITY_APPARMOR_EXPORT_BINARY 1364d61c57fdSJohn Johansen module_param_named(export_binary, aa_g_export_binary, aabool, 0600); 1365d61c57fdSJohn Johansen #endif 1366d61c57fdSJohn Johansen 136763c16c3aSChris Coulson /* policy loaddata compression level */ 136870f24a9fSJohn Johansen int aa_g_rawdata_compression_level = AA_DEFAULT_CLEVEL; 136963c16c3aSChris Coulson module_param_named(rawdata_compression_level, aa_g_rawdata_compression_level, 137063c16c3aSChris Coulson aacompressionlevel, 0400); 137163c16c3aSChris Coulson 1372b5e95b48SJohn Johansen /* Debug mode */ 1373eea7a05fSValentin Rothberg bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES); 1374b5e95b48SJohn Johansen module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR); 1375b5e95b48SJohn Johansen 1376b5e95b48SJohn Johansen /* Audit mode */ 1377b5e95b48SJohn Johansen enum audit_mode aa_g_audit; 1378b5e95b48SJohn Johansen module_param_call(audit, param_set_audit, param_get_audit, 1379b5e95b48SJohn Johansen &aa_g_audit, S_IRUSR | S_IWUSR); 1380b5e95b48SJohn Johansen 1381b5e95b48SJohn Johansen /* Determines if audit header is included in audited messages. This 1382b5e95b48SJohn Johansen * provides more context if the audit daemon is not running 1383b5e95b48SJohn Johansen */ 1384954317feSThomas Meyer bool aa_g_audit_header = true; 1385b5e95b48SJohn Johansen module_param_named(audit_header, aa_g_audit_header, aabool, 1386b5e95b48SJohn Johansen S_IRUSR | S_IWUSR); 1387b5e95b48SJohn Johansen 1388b5e95b48SJohn Johansen /* lock out loading/removal of policy 1389b5e95b48SJohn Johansen * TODO: add in at boot loading of policy, which is the only way to 1390b5e95b48SJohn Johansen * load policy, if lock_policy is set 1391b5e95b48SJohn Johansen */ 139290ab5ee9SRusty Russell bool aa_g_lock_policy; 1393b5e95b48SJohn Johansen module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy, 1394b5e95b48SJohn Johansen S_IRUSR | S_IWUSR); 1395b5e95b48SJohn Johansen 1396b5e95b48SJohn Johansen /* Syscall logging mode */ 139790ab5ee9SRusty Russell bool aa_g_logsyscall; 1398b5e95b48SJohn Johansen module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR); 1399b5e95b48SJohn Johansen 1400b5e95b48SJohn Johansen /* Maximum pathname length before accesses will start getting rejected */ 1401b5e95b48SJohn Johansen unsigned int aa_g_path_max = 2 * PATH_MAX; 1402622f6e32SJohn Johansen module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR); 1403b5e95b48SJohn Johansen 1404b5e95b48SJohn Johansen /* Determines how paranoid loading of policy is and how much verification 1405b5e95b48SJohn Johansen * on the loaded policy is done. 1406abbf8734SJohn Johansen * DEPRECATED: read only as strict checking of load is always done now 1407abbf8734SJohn Johansen * that none root users (user namespaces) can load policy. 1408b5e95b48SJohn Johansen */ 140979eb2711SLukas Bulwahn bool aa_g_paranoid_load = IS_ENABLED(CONFIG_SECURITY_APPARMOR_PARANOID_LOAD); 1410abbf8734SJohn Johansen module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); 1411b5e95b48SJohn Johansen 1412e33c1b99SKees Cook static int param_get_aaintbool(char *buffer, const struct kernel_param *kp); 1413e33c1b99SKees Cook static int param_set_aaintbool(const char *val, const struct kernel_param *kp); 1414e33c1b99SKees Cook #define param_check_aaintbool param_check_int 1415e33c1b99SKees Cook static const struct kernel_param_ops param_ops_aaintbool = { 1416e33c1b99SKees Cook .set = param_set_aaintbool, 1417e33c1b99SKees Cook .get = param_get_aaintbool 1418e33c1b99SKees Cook }; 1419b5e95b48SJohn Johansen /* Boot time disable flag */ 14200102fb83SKees Cook static int apparmor_enabled __lsm_ro_after_init = 1; 1421e33c1b99SKees Cook module_param_named(enabled, apparmor_enabled, aaintbool, 0444); 1422b5e95b48SJohn Johansen 1423b5e95b48SJohn Johansen static int __init apparmor_enabled_setup(char *str) 1424b5e95b48SJohn Johansen { 1425b5e95b48SJohn Johansen unsigned long enabled; 142629707b20SJingoo Han int error = kstrtoul(str, 0, &enabled); 1427b5e95b48SJohn Johansen if (!error) 1428b5e95b48SJohn Johansen apparmor_enabled = enabled ? 1 : 0; 1429b5e95b48SJohn Johansen return 1; 1430b5e95b48SJohn Johansen } 1431b5e95b48SJohn Johansen 1432b5e95b48SJohn Johansen __setup("apparmor=", apparmor_enabled_setup); 1433b5e95b48SJohn Johansen 1434b5e95b48SJohn Johansen /* set global flag turning off the ability to load policy */ 1435101d6c82SStephen Rothwell static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp) 1436b5e95b48SJohn Johansen { 1437545de8feSJohn Johansen if (!apparmor_enabled) 1438545de8feSJohn Johansen return -EINVAL; 143992de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1440b5e95b48SJohn Johansen return -EPERM; 1441b5e95b48SJohn Johansen return param_set_bool(val, kp); 1442b5e95b48SJohn Johansen } 1443b5e95b48SJohn Johansen 1444101d6c82SStephen Rothwell static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp) 1445b5e95b48SJohn Johansen { 1446ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1447ca4bd5aeSJohn Johansen return -EINVAL; 144892de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1449545de8feSJohn Johansen return -EPERM; 1450b5e95b48SJohn Johansen return param_get_bool(buffer, kp); 1451b5e95b48SJohn Johansen } 1452b5e95b48SJohn Johansen 1453101d6c82SStephen Rothwell static int param_set_aabool(const char *val, const struct kernel_param *kp) 1454b5e95b48SJohn Johansen { 1455ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1456ca4bd5aeSJohn Johansen return -EINVAL; 145792de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1458545de8feSJohn Johansen return -EPERM; 1459b5e95b48SJohn Johansen return param_set_bool(val, kp); 1460b5e95b48SJohn Johansen } 1461b5e95b48SJohn Johansen 1462101d6c82SStephen Rothwell static int param_get_aabool(char *buffer, const struct kernel_param *kp) 1463b5e95b48SJohn Johansen { 1464ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1465ca4bd5aeSJohn Johansen return -EINVAL; 146692de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1467545de8feSJohn Johansen return -EPERM; 1468b5e95b48SJohn Johansen return param_get_bool(buffer, kp); 1469b5e95b48SJohn Johansen } 1470b5e95b48SJohn Johansen 1471101d6c82SStephen Rothwell static int param_set_aauint(const char *val, const struct kernel_param *kp) 1472b5e95b48SJohn Johansen { 147339d84824SJohn Johansen int error; 147439d84824SJohn Johansen 1475ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1476ca4bd5aeSJohn Johansen return -EINVAL; 147739d84824SJohn Johansen /* file is ro but enforce 2nd line check */ 147839d84824SJohn Johansen if (apparmor_initialized) 1479545de8feSJohn Johansen return -EPERM; 148039d84824SJohn Johansen 148139d84824SJohn Johansen error = param_set_uint(val, kp); 1482df323337SSebastian Andrzej Siewior aa_g_path_max = max_t(uint32_t, aa_g_path_max, sizeof(union aa_buffer)); 148339d84824SJohn Johansen pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); 148439d84824SJohn Johansen 148539d84824SJohn Johansen return error; 1486b5e95b48SJohn Johansen } 1487b5e95b48SJohn Johansen 1488101d6c82SStephen Rothwell static int param_get_aauint(char *buffer, const struct kernel_param *kp) 1489b5e95b48SJohn Johansen { 1490ca4bd5aeSJohn Johansen if (!apparmor_enabled) 1491ca4bd5aeSJohn Johansen return -EINVAL; 149292de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1493545de8feSJohn Johansen return -EPERM; 1494b5e95b48SJohn Johansen return param_get_uint(buffer, kp); 1495b5e95b48SJohn Johansen } 1496b5e95b48SJohn Johansen 1497e33c1b99SKees Cook /* Can only be set before AppArmor is initialized (i.e. on boot cmdline). */ 1498e33c1b99SKees Cook static int param_set_aaintbool(const char *val, const struct kernel_param *kp) 1499e33c1b99SKees Cook { 1500e33c1b99SKees Cook struct kernel_param kp_local; 1501e33c1b99SKees Cook bool value; 1502e33c1b99SKees Cook int error; 1503e33c1b99SKees Cook 1504e33c1b99SKees Cook if (apparmor_initialized) 1505e33c1b99SKees Cook return -EPERM; 1506e33c1b99SKees Cook 1507e33c1b99SKees Cook /* Create local copy, with arg pointing to bool type. */ 1508e33c1b99SKees Cook value = !!*((int *)kp->arg); 1509e33c1b99SKees Cook memcpy(&kp_local, kp, sizeof(kp_local)); 1510e33c1b99SKees Cook kp_local.arg = &value; 1511e33c1b99SKees Cook 1512e33c1b99SKees Cook error = param_set_bool(val, &kp_local); 1513e33c1b99SKees Cook if (!error) 1514e33c1b99SKees Cook *((int *)kp->arg) = *((bool *)kp_local.arg); 1515e33c1b99SKees Cook return error; 1516e33c1b99SKees Cook } 1517e33c1b99SKees Cook 1518e33c1b99SKees Cook /* 1519e33c1b99SKees Cook * To avoid changing /sys/module/apparmor/parameters/enabled from Y/N to 1520e33c1b99SKees Cook * 1/0, this converts the "int that is actually bool" back to bool for 1521e33c1b99SKees Cook * display in the /sys filesystem, while keeping it "int" for the LSM 1522e33c1b99SKees Cook * infrastructure. 1523e33c1b99SKees Cook */ 1524e33c1b99SKees Cook static int param_get_aaintbool(char *buffer, const struct kernel_param *kp) 1525e33c1b99SKees Cook { 1526e33c1b99SKees Cook struct kernel_param kp_local; 1527e33c1b99SKees Cook bool value; 1528e33c1b99SKees Cook 1529e33c1b99SKees Cook /* Create local copy, with arg pointing to bool type. */ 1530e33c1b99SKees Cook value = !!*((int *)kp->arg); 1531e33c1b99SKees Cook memcpy(&kp_local, kp, sizeof(kp_local)); 1532e33c1b99SKees Cook kp_local.arg = &value; 1533e33c1b99SKees Cook 1534e33c1b99SKees Cook return param_get_bool(buffer, &kp_local); 1535e33c1b99SKees Cook } 1536e33c1b99SKees Cook 153763c16c3aSChris Coulson static int param_set_aacompressionlevel(const char *val, 153863c16c3aSChris Coulson const struct kernel_param *kp) 153963c16c3aSChris Coulson { 154063c16c3aSChris Coulson int error; 154163c16c3aSChris Coulson 154263c16c3aSChris Coulson if (!apparmor_enabled) 154363c16c3aSChris Coulson return -EINVAL; 154463c16c3aSChris Coulson if (apparmor_initialized) 154563c16c3aSChris Coulson return -EPERM; 154663c16c3aSChris Coulson 154763c16c3aSChris Coulson error = param_set_int(val, kp); 154863c16c3aSChris Coulson 154963c16c3aSChris Coulson aa_g_rawdata_compression_level = clamp(aa_g_rawdata_compression_level, 155070f24a9fSJohn Johansen AA_MIN_CLEVEL, AA_MAX_CLEVEL); 1551f4d6b94bSJon Tourville pr_info("AppArmor: policy rawdata compression level set to %d\n", 155263c16c3aSChris Coulson aa_g_rawdata_compression_level); 155363c16c3aSChris Coulson 155463c16c3aSChris Coulson return error; 155563c16c3aSChris Coulson } 155663c16c3aSChris Coulson 155763c16c3aSChris Coulson static int param_get_aacompressionlevel(char *buffer, 155863c16c3aSChris Coulson const struct kernel_param *kp) 155963c16c3aSChris Coulson { 156063c16c3aSChris Coulson if (!apparmor_enabled) 156163c16c3aSChris Coulson return -EINVAL; 156292de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 156363c16c3aSChris Coulson return -EPERM; 156463c16c3aSChris Coulson return param_get_int(buffer, kp); 156563c16c3aSChris Coulson } 156663c16c3aSChris Coulson 1567e4dca7b7SKees Cook static int param_get_audit(char *buffer, const struct kernel_param *kp) 1568b5e95b48SJohn Johansen { 1569b5e95b48SJohn Johansen if (!apparmor_enabled) 1570b5e95b48SJohn Johansen return -EINVAL; 157192de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1572545de8feSJohn Johansen return -EPERM; 1573b5e95b48SJohn Johansen return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]); 1574b5e95b48SJohn Johansen } 1575b5e95b48SJohn Johansen 1576e4dca7b7SKees Cook static int param_set_audit(const char *val, const struct kernel_param *kp) 1577b5e95b48SJohn Johansen { 1578b5e95b48SJohn Johansen int i; 1579b5e95b48SJohn Johansen 1580b5e95b48SJohn Johansen if (!apparmor_enabled) 1581b5e95b48SJohn Johansen return -EINVAL; 1582b5e95b48SJohn Johansen if (!val) 1583b5e95b48SJohn Johansen return -EINVAL; 158492de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1585545de8feSJohn Johansen return -EPERM; 1586b5e95b48SJohn Johansen 15875d8779a5SAndy Shevchenko i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val); 15885d8779a5SAndy Shevchenko if (i < 0) 15895d8779a5SAndy Shevchenko return -EINVAL; 15905d8779a5SAndy Shevchenko 1591b5e95b48SJohn Johansen aa_g_audit = i; 1592b5e95b48SJohn Johansen return 0; 1593b5e95b48SJohn Johansen } 1594b5e95b48SJohn Johansen 1595e4dca7b7SKees Cook static int param_get_mode(char *buffer, const struct kernel_param *kp) 1596b5e95b48SJohn Johansen { 1597b5e95b48SJohn Johansen if (!apparmor_enabled) 1598b5e95b48SJohn Johansen return -EINVAL; 159992de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_view_capable(NULL)) 1600545de8feSJohn Johansen return -EPERM; 1601b5e95b48SJohn Johansen 16020d259f04SJohn Johansen return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]); 1603b5e95b48SJohn Johansen } 1604b5e95b48SJohn Johansen 1605e4dca7b7SKees Cook static int param_set_mode(const char *val, const struct kernel_param *kp) 1606b5e95b48SJohn Johansen { 1607b5e95b48SJohn Johansen int i; 1608b5e95b48SJohn Johansen 1609b5e95b48SJohn Johansen if (!apparmor_enabled) 1610b5e95b48SJohn Johansen return -EINVAL; 1611b5e95b48SJohn Johansen if (!val) 1612b5e95b48SJohn Johansen return -EINVAL; 161392de220aSJohn Johansen if (apparmor_initialized && !aa_current_policy_admin_capable(NULL)) 1614545de8feSJohn Johansen return -EPERM; 1615b5e95b48SJohn Johansen 16165d8779a5SAndy Shevchenko i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX, 16175d8779a5SAndy Shevchenko val); 16185d8779a5SAndy Shevchenko if (i < 0) 16195d8779a5SAndy Shevchenko return -EINVAL; 16205d8779a5SAndy Shevchenko 1621b5e95b48SJohn Johansen aa_g_profile_mode = i; 1622b5e95b48SJohn Johansen return 0; 1623b5e95b48SJohn Johansen } 1624b5e95b48SJohn Johansen 1625341c1fdaSJohn Johansen char *aa_get_buffer(bool in_atomic) 1626df323337SSebastian Andrzej Siewior { 1627df323337SSebastian Andrzej Siewior union aa_buffer *aa_buf; 1628df323337SSebastian Andrzej Siewior bool try_again = true; 1629341c1fdaSJohn Johansen gfp_t flags = (GFP_KERNEL | __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 1630df323337SSebastian Andrzej Siewior 1631df323337SSebastian Andrzej Siewior retry: 1632df323337SSebastian Andrzej Siewior spin_lock(&aa_buffers_lock); 1633341c1fdaSJohn Johansen if (buffer_count > reserve_count || 1634341c1fdaSJohn Johansen (in_atomic && !list_empty(&aa_global_buffers))) { 1635df323337SSebastian Andrzej Siewior aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, 1636df323337SSebastian Andrzej Siewior list); 1637df323337SSebastian Andrzej Siewior list_del(&aa_buf->list); 1638341c1fdaSJohn Johansen buffer_count--; 1639df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1640df323337SSebastian Andrzej Siewior return &aa_buf->buffer[0]; 1641df323337SSebastian Andrzej Siewior } 1642341c1fdaSJohn Johansen if (in_atomic) { 1643341c1fdaSJohn Johansen /* 1644341c1fdaSJohn Johansen * out of reserve buffers and in atomic context so increase 1645341c1fdaSJohn Johansen * how many buffers to keep in reserve 1646341c1fdaSJohn Johansen */ 1647341c1fdaSJohn Johansen reserve_count++; 1648341c1fdaSJohn Johansen flags = GFP_ATOMIC; 1649341c1fdaSJohn Johansen } 1650df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1651df323337SSebastian Andrzej Siewior 1652341c1fdaSJohn Johansen if (!in_atomic) 1653341c1fdaSJohn Johansen might_sleep(); 1654341c1fdaSJohn Johansen aa_buf = kmalloc(aa_g_path_max, flags); 1655df323337SSebastian Andrzej Siewior if (!aa_buf) { 1656df323337SSebastian Andrzej Siewior if (try_again) { 1657df323337SSebastian Andrzej Siewior try_again = false; 1658df323337SSebastian Andrzej Siewior goto retry; 1659df323337SSebastian Andrzej Siewior } 1660df323337SSebastian Andrzej Siewior pr_warn_once("AppArmor: Failed to allocate a memory buffer.\n"); 1661df323337SSebastian Andrzej Siewior return NULL; 1662df323337SSebastian Andrzej Siewior } 1663df323337SSebastian Andrzej Siewior return &aa_buf->buffer[0]; 1664df323337SSebastian Andrzej Siewior } 1665df323337SSebastian Andrzej Siewior 1666df323337SSebastian Andrzej Siewior void aa_put_buffer(char *buf) 1667df323337SSebastian Andrzej Siewior { 1668df323337SSebastian Andrzej Siewior union aa_buffer *aa_buf; 1669df323337SSebastian Andrzej Siewior 1670df323337SSebastian Andrzej Siewior if (!buf) 1671df323337SSebastian Andrzej Siewior return; 1672df323337SSebastian Andrzej Siewior aa_buf = container_of(buf, union aa_buffer, buffer[0]); 1673df323337SSebastian Andrzej Siewior 1674df323337SSebastian Andrzej Siewior spin_lock(&aa_buffers_lock); 1675df323337SSebastian Andrzej Siewior list_add(&aa_buf->list, &aa_global_buffers); 1676341c1fdaSJohn Johansen buffer_count++; 1677df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1678df323337SSebastian Andrzej Siewior } 1679df323337SSebastian Andrzej Siewior 1680b5e95b48SJohn Johansen /* 1681b5e95b48SJohn Johansen * AppArmor init functions 1682b5e95b48SJohn Johansen */ 1683b5e95b48SJohn Johansen 1684b5e95b48SJohn Johansen /** 168555a26ebfSJohn Johansen * set_init_ctx - set a task context and profile on the first task. 1686b5e95b48SJohn Johansen * 1687b5e95b48SJohn Johansen * TODO: allow setting an alternate profile than unconfined 1688b5e95b48SJohn Johansen */ 168955a26ebfSJohn Johansen static int __init set_init_ctx(void) 1690b5e95b48SJohn Johansen { 1691bf1d2ee7SBharath Vedartham struct cred *cred = (__force struct cred *)current->real_cred; 1692b5e95b48SJohn Johansen 169369b5a44aSCasey Schaufler set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); 1694b5e95b48SJohn Johansen 1695b5e95b48SJohn Johansen return 0; 1696b5e95b48SJohn Johansen } 1697b5e95b48SJohn Johansen 1698d4669f0bSJohn Johansen static void destroy_buffers(void) 1699d4669f0bSJohn Johansen { 1700df323337SSebastian Andrzej Siewior union aa_buffer *aa_buf; 1701d4669f0bSJohn Johansen 1702df323337SSebastian Andrzej Siewior spin_lock(&aa_buffers_lock); 1703df323337SSebastian Andrzej Siewior while (!list_empty(&aa_global_buffers)) { 1704df323337SSebastian Andrzej Siewior aa_buf = list_first_entry(&aa_global_buffers, union aa_buffer, 1705df323337SSebastian Andrzej Siewior list); 1706df323337SSebastian Andrzej Siewior list_del(&aa_buf->list); 1707df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1708df323337SSebastian Andrzej Siewior kfree(aa_buf); 1709df323337SSebastian Andrzej Siewior spin_lock(&aa_buffers_lock); 1710d4669f0bSJohn Johansen } 1711df323337SSebastian Andrzej Siewior spin_unlock(&aa_buffers_lock); 1712d4669f0bSJohn Johansen } 1713d4669f0bSJohn Johansen 1714d4669f0bSJohn Johansen static int __init alloc_buffers(void) 1715d4669f0bSJohn Johansen { 1716df323337SSebastian Andrzej Siewior union aa_buffer *aa_buf; 1717df323337SSebastian Andrzej Siewior int i, num; 1718d4669f0bSJohn Johansen 1719df323337SSebastian Andrzej Siewior /* 1720df323337SSebastian Andrzej Siewior * A function may require two buffers at once. Usually the buffers are 1721df323337SSebastian Andrzej Siewior * used for a short period of time and are shared. On UP kernel buffers 1722df323337SSebastian Andrzej Siewior * two should be enough, with more CPUs it is possible that more 1723df323337SSebastian Andrzej Siewior * buffers will be used simultaneously. The preallocated pool may grow. 1724df323337SSebastian Andrzej Siewior * This preallocation has also the side-effect that AppArmor will be 1725df323337SSebastian Andrzej Siewior * disabled early at boot if aa_g_path_max is extremly high. 1726df323337SSebastian Andrzej Siewior */ 1727df323337SSebastian Andrzej Siewior if (num_online_cpus() > 1) 1728341c1fdaSJohn Johansen num = 4 + RESERVE_COUNT; 1729d4669f0bSJohn Johansen else 1730341c1fdaSJohn Johansen num = 2 + RESERVE_COUNT; 1731df323337SSebastian Andrzej Siewior 1732df323337SSebastian Andrzej Siewior for (i = 0; i < num; i++) { 1733df323337SSebastian Andrzej Siewior 1734df323337SSebastian Andrzej Siewior aa_buf = kmalloc(aa_g_path_max, GFP_KERNEL | 1735df323337SSebastian Andrzej Siewior __GFP_RETRY_MAYFAIL | __GFP_NOWARN); 1736df323337SSebastian Andrzej Siewior if (!aa_buf) { 1737d4669f0bSJohn Johansen destroy_buffers(); 1738d4669f0bSJohn Johansen return -ENOMEM; 1739d4669f0bSJohn Johansen } 1740df323337SSebastian Andrzej Siewior aa_put_buffer(&aa_buf->buffer[0]); 1741d4669f0bSJohn Johansen } 1742d4669f0bSJohn Johansen return 0; 1743d4669f0bSJohn Johansen } 1744d4669f0bSJohn Johansen 1745e3ea1ca5STyler Hicks #ifdef CONFIG_SYSCTL 1746e3ea1ca5STyler Hicks static int apparmor_dointvec(struct ctl_table *table, int write, 174732927393SChristoph Hellwig void *buffer, size_t *lenp, loff_t *ppos) 1748e3ea1ca5STyler Hicks { 174992de220aSJohn Johansen if (!aa_current_policy_admin_capable(NULL)) 1750e3ea1ca5STyler Hicks return -EPERM; 1751e3ea1ca5STyler Hicks if (!apparmor_enabled) 1752e3ea1ca5STyler Hicks return -EINVAL; 1753e3ea1ca5STyler Hicks 1754e3ea1ca5STyler Hicks return proc_dointvec(table, write, buffer, lenp, ppos); 1755e3ea1ca5STyler Hicks } 1756e3ea1ca5STyler Hicks 1757e3ea1ca5STyler Hicks static struct ctl_path apparmor_sysctl_path[] = { 1758e3ea1ca5STyler Hicks { .procname = "kernel", }, 1759e3ea1ca5STyler Hicks { } 1760e3ea1ca5STyler Hicks }; 1761e3ea1ca5STyler Hicks 1762e3ea1ca5STyler Hicks static struct ctl_table apparmor_sysctl_table[] = { 1763e3ea1ca5STyler Hicks { 1764e3ea1ca5STyler Hicks .procname = "unprivileged_userns_apparmor_policy", 1765e3ea1ca5STyler Hicks .data = &unprivileged_userns_apparmor_policy, 1766e3ea1ca5STyler Hicks .maxlen = sizeof(int), 1767e3ea1ca5STyler Hicks .mode = 0600, 1768e3ea1ca5STyler Hicks .proc_handler = apparmor_dointvec, 1769e3ea1ca5STyler Hicks }, 1770524d8e14SJohn Johansen { 1771524d8e14SJohn Johansen .procname = "apparmor_display_secid_mode", 1772524d8e14SJohn Johansen .data = &apparmor_display_secid_mode, 1773524d8e14SJohn Johansen .maxlen = sizeof(int), 1774524d8e14SJohn Johansen .mode = 0600, 1775524d8e14SJohn Johansen .proc_handler = apparmor_dointvec, 1776524d8e14SJohn Johansen }, 1777524d8e14SJohn Johansen 1778e3ea1ca5STyler Hicks { } 1779e3ea1ca5STyler Hicks }; 1780e3ea1ca5STyler Hicks 1781e3ea1ca5STyler Hicks static int __init apparmor_init_sysctl(void) 1782e3ea1ca5STyler Hicks { 1783e3ea1ca5STyler Hicks return register_sysctl_paths(apparmor_sysctl_path, 1784e3ea1ca5STyler Hicks apparmor_sysctl_table) ? 0 : -ENOMEM; 1785e3ea1ca5STyler Hicks } 1786e3ea1ca5STyler Hicks #else 1787e3ea1ca5STyler Hicks static inline int apparmor_init_sysctl(void) 1788e3ea1ca5STyler Hicks { 1789e3ea1ca5STyler Hicks return 0; 1790e3ea1ca5STyler Hicks } 1791e3ea1ca5STyler Hicks #endif /* CONFIG_SYSCTL */ 1792e3ea1ca5STyler Hicks 1793e1af4779SArnd Bergmann #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) 1794ab9f2115SMatthew Garrett static unsigned int apparmor_ip_postroute(void *priv, 1795ab9f2115SMatthew Garrett struct sk_buff *skb, 1796ab9f2115SMatthew Garrett const struct nf_hook_state *state) 1797ab9f2115SMatthew Garrett { 1798ab9f2115SMatthew Garrett struct aa_sk_ctx *ctx; 1799ab9f2115SMatthew Garrett struct sock *sk; 1800ab9f2115SMatthew Garrett 1801ab9f2115SMatthew Garrett if (!skb->secmark) 1802ab9f2115SMatthew Garrett return NF_ACCEPT; 1803ab9f2115SMatthew Garrett 1804ab9f2115SMatthew Garrett sk = skb_to_full_sk(skb); 1805ab9f2115SMatthew Garrett if (sk == NULL) 1806ab9f2115SMatthew Garrett return NF_ACCEPT; 1807ab9f2115SMatthew Garrett 1808ab9f2115SMatthew Garrett ctx = SK_CTX(sk); 1809ab9f2115SMatthew Garrett if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, 1810ab9f2115SMatthew Garrett skb->secmark, sk)) 1811ab9f2115SMatthew Garrett return NF_ACCEPT; 1812ab9f2115SMatthew Garrett 1813ab9f2115SMatthew Garrett return NF_DROP_ERR(-ECONNREFUSED); 1814ab9f2115SMatthew Garrett 1815ab9f2115SMatthew Garrett } 1816ab9f2115SMatthew Garrett 1817ab9f2115SMatthew Garrett static const struct nf_hook_ops apparmor_nf_ops[] = { 1818ab9f2115SMatthew Garrett { 18197b721124SFlorian Westphal .hook = apparmor_ip_postroute, 1820ab9f2115SMatthew Garrett .pf = NFPROTO_IPV4, 1821ab9f2115SMatthew Garrett .hooknum = NF_INET_POST_ROUTING, 1822ab9f2115SMatthew Garrett .priority = NF_IP_PRI_SELINUX_FIRST, 1823ab9f2115SMatthew Garrett }, 1824ab9f2115SMatthew Garrett #if IS_ENABLED(CONFIG_IPV6) 1825ab9f2115SMatthew Garrett { 18267b721124SFlorian Westphal .hook = apparmor_ip_postroute, 1827ab9f2115SMatthew Garrett .pf = NFPROTO_IPV6, 1828ab9f2115SMatthew Garrett .hooknum = NF_INET_POST_ROUTING, 1829ab9f2115SMatthew Garrett .priority = NF_IP6_PRI_SELINUX_FIRST, 1830ab9f2115SMatthew Garrett }, 1831ab9f2115SMatthew Garrett #endif 1832ab9f2115SMatthew Garrett }; 1833ab9f2115SMatthew Garrett 1834ab9f2115SMatthew Garrett static int __net_init apparmor_nf_register(struct net *net) 1835ab9f2115SMatthew Garrett { 183684117994SMinghao Chi return nf_register_net_hooks(net, apparmor_nf_ops, 1837ab9f2115SMatthew Garrett ARRAY_SIZE(apparmor_nf_ops)); 1838ab9f2115SMatthew Garrett } 1839ab9f2115SMatthew Garrett 1840ab9f2115SMatthew Garrett static void __net_exit apparmor_nf_unregister(struct net *net) 1841ab9f2115SMatthew Garrett { 1842ab9f2115SMatthew Garrett nf_unregister_net_hooks(net, apparmor_nf_ops, 1843ab9f2115SMatthew Garrett ARRAY_SIZE(apparmor_nf_ops)); 1844ab9f2115SMatthew Garrett } 1845ab9f2115SMatthew Garrett 1846ab9f2115SMatthew Garrett static struct pernet_operations apparmor_net_ops = { 1847ab9f2115SMatthew Garrett .init = apparmor_nf_register, 1848ab9f2115SMatthew Garrett .exit = apparmor_nf_unregister, 1849ab9f2115SMatthew Garrett }; 1850ab9f2115SMatthew Garrett 1851ab9f2115SMatthew Garrett static int __init apparmor_nf_ip_init(void) 1852ab9f2115SMatthew Garrett { 1853ab9f2115SMatthew Garrett int err; 1854ab9f2115SMatthew Garrett 1855ab9f2115SMatthew Garrett if (!apparmor_enabled) 1856ab9f2115SMatthew Garrett return 0; 1857ab9f2115SMatthew Garrett 1858ab9f2115SMatthew Garrett err = register_pernet_subsys(&apparmor_net_ops); 1859ab9f2115SMatthew Garrett if (err) 1860ab9f2115SMatthew Garrett panic("Apparmor: register_pernet_subsys: error %d\n", err); 1861ab9f2115SMatthew Garrett 1862ab9f2115SMatthew Garrett return 0; 1863ab9f2115SMatthew Garrett } 1864ab9f2115SMatthew Garrett __initcall(apparmor_nf_ip_init); 1865e1af4779SArnd Bergmann #endif 1866ab9f2115SMatthew Garrett 1867b5e95b48SJohn Johansen static int __init apparmor_init(void) 1868b5e95b48SJohn Johansen { 1869b5e95b48SJohn Johansen int error; 1870b5e95b48SJohn Johansen 187111c236b8SJohn Johansen error = aa_setup_dfa_engine(); 187211c236b8SJohn Johansen if (error) { 187311c236b8SJohn Johansen AA_ERROR("Unable to setup dfa engine\n"); 187411c236b8SJohn Johansen goto alloc_out; 187511c236b8SJohn Johansen } 187611c236b8SJohn Johansen 1877b5e95b48SJohn Johansen error = aa_alloc_root_ns(); 1878b5e95b48SJohn Johansen if (error) { 1879b5e95b48SJohn Johansen AA_ERROR("Unable to allocate default profile namespace\n"); 1880b5e95b48SJohn Johansen goto alloc_out; 1881b5e95b48SJohn Johansen } 1882b5e95b48SJohn Johansen 1883e3ea1ca5STyler Hicks error = apparmor_init_sysctl(); 1884e3ea1ca5STyler Hicks if (error) { 1885e3ea1ca5STyler Hicks AA_ERROR("Unable to register sysctls\n"); 1886e3ea1ca5STyler Hicks goto alloc_out; 1887e3ea1ca5STyler Hicks 1888e3ea1ca5STyler Hicks } 1889e3ea1ca5STyler Hicks 1890d4669f0bSJohn Johansen error = alloc_buffers(); 1891d4669f0bSJohn Johansen if (error) { 1892d4669f0bSJohn Johansen AA_ERROR("Unable to allocate work buffers\n"); 1893df323337SSebastian Andrzej Siewior goto alloc_out; 1894d4669f0bSJohn Johansen } 1895d4669f0bSJohn Johansen 189655a26ebfSJohn Johansen error = set_init_ctx(); 1897b5e95b48SJohn Johansen if (error) { 1898b5e95b48SJohn Johansen AA_ERROR("Failed to set context on init task\n"); 1899b1d9e6b0SCasey Schaufler aa_free_root_ns(); 1900d4669f0bSJohn Johansen goto buffers_out; 1901b5e95b48SJohn Johansen } 1902d69dece5SCasey Schaufler security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks), 1903d69dece5SCasey Schaufler "apparmor"); 1904b5e95b48SJohn Johansen 1905b5e95b48SJohn Johansen /* Report that AppArmor successfully initialized */ 1906b5e95b48SJohn Johansen apparmor_initialized = 1; 1907b5e95b48SJohn Johansen if (aa_g_profile_mode == APPARMOR_COMPLAIN) 1908b5e95b48SJohn Johansen aa_info_message("AppArmor initialized: complain mode enabled"); 1909b5e95b48SJohn Johansen else if (aa_g_profile_mode == APPARMOR_KILL) 1910b5e95b48SJohn Johansen aa_info_message("AppArmor initialized: kill mode enabled"); 1911b5e95b48SJohn Johansen else 1912b5e95b48SJohn Johansen aa_info_message("AppArmor initialized"); 1913b5e95b48SJohn Johansen 1914b5e95b48SJohn Johansen return error; 1915b5e95b48SJohn Johansen 1916d4669f0bSJohn Johansen buffers_out: 1917d4669f0bSJohn Johansen destroy_buffers(); 1918b5e95b48SJohn Johansen alloc_out: 1919b5e95b48SJohn Johansen aa_destroy_aafs(); 192011c236b8SJohn Johansen aa_teardown_dfa_engine(); 1921b5e95b48SJohn Johansen 1922954317feSThomas Meyer apparmor_enabled = false; 1923b5e95b48SJohn Johansen return error; 1924b5e95b48SJohn Johansen } 1925b5e95b48SJohn Johansen 19263d6e5f6dSKees Cook DEFINE_LSM(apparmor) = { 192707aed2f2SKees Cook .name = "apparmor", 192814bd99c8SKees Cook .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, 1929c5459b82SKees Cook .enabled = &apparmor_enabled, 1930bbd3662aSCasey Schaufler .blobs = &apparmor_blob_sizes, 19313d6e5f6dSKees Cook .init = apparmor_init, 19323d6e5f6dSKees Cook }; 1933