John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * AppArmor security module |
| 3 | * |
| 4 | * This file contains AppArmor LSM hooks. |
| 5 | * |
| 6 | * Copyright (C) 1998-2008 Novell/SUSE |
| 7 | * Copyright 2009-2010 Canonical Ltd. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation, version 2 of the |
| 12 | * License. |
| 13 | */ |
| 14 | |
Casey Schaufler | 3c4ed7b | 2015-05-02 15:10:46 -0700 | [diff] [blame] | 15 | #include <linux/lsm_hooks.h> |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 16 | #include <linux/moduleparam.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/mman.h> |
| 19 | #include <linux/mount.h> |
| 20 | #include <linux/namei.h> |
| 21 | #include <linux/ptrace.h> |
| 22 | #include <linux/ctype.h> |
| 23 | #include <linux/sysctl.h> |
| 24 | #include <linux/audit.h> |
Serge E. Hallyn | 3486740 | 2011-03-23 16:43:17 -0700 | [diff] [blame] | 25 | #include <linux/user_namespace.h> |
Matthew Garrett | ab9f211 | 2018-05-24 13:27:47 -0700 | [diff] [blame] | 26 | #include <linux/netfilter_ipv4.h> |
| 27 | #include <linux/netfilter_ipv6.h> |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 28 | #include <net/sock.h> |
David Howells | e262e32d | 2018-11-01 23:07:23 +0000 | [diff] [blame] | 29 | #include <uapi/linux/mount.h> |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 30 | |
| 31 | #include "include/apparmor.h" |
| 32 | #include "include/apparmorfs.h" |
| 33 | #include "include/audit.h" |
| 34 | #include "include/capability.h" |
John Johansen | d8889d4 | 2017-10-11 01:04:48 -0700 | [diff] [blame] | 35 | #include "include/cred.h" |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 36 | #include "include/file.h" |
| 37 | #include "include/ipc.h" |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 38 | #include "include/net.h" |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 39 | #include "include/path.h" |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 40 | #include "include/label.h" |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 41 | #include "include/policy.h" |
John Johansen | cff281f | 2017-01-16 00:42:15 -0800 | [diff] [blame] | 42 | #include "include/policy_ns.h" |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 43 | #include "include/procattr.h" |
John Johansen | 2ea3ffb | 2017-07-18 23:04:47 -0700 | [diff] [blame] | 44 | #include "include/mount.h" |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 45 | #include "include/secid.h" |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 46 | |
| 47 | /* Flag indicating whether initialization completed */ |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 48 | int apparmor_initialized; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 49 | |
John Johansen | d4669f0 | 2017-01-16 00:43:10 -0800 | [diff] [blame] | 50 | DEFINE_PER_CPU(struct aa_buffers, aa_buffers); |
| 51 | |
| 52 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 53 | /* |
| 54 | * LSM hook functions |
| 55 | */ |
| 56 | |
| 57 | /* |
John Johansen | d9087c4 | 2017-01-27 03:53:53 -0800 | [diff] [blame] | 58 | * put the associated labels |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 59 | */ |
| 60 | static void apparmor_cred_free(struct cred *cred) |
| 61 | { |
John Johansen | d9087c4 | 2017-01-27 03:53:53 -0800 | [diff] [blame] | 62 | aa_put_label(cred_label(cred)); |
Casey Schaufler | 69b5a44 | 2018-09-21 17:17:59 -0700 | [diff] [blame] | 63 | set_cred_label(cred, NULL); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /* |
| 67 | * allocate the apparmor part of blank credentials |
| 68 | */ |
| 69 | static int apparmor_cred_alloc_blank(struct cred *cred, gfp_t gfp) |
| 70 | { |
Casey Schaufler | 69b5a44 | 2018-09-21 17:17:59 -0700 | [diff] [blame] | 71 | set_cred_label(cred, NULL); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | /* |
John Johansen | d9087c4 | 2017-01-27 03:53:53 -0800 | [diff] [blame] | 76 | * prepare new cred label for modification by prepare_cred block |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 77 | */ |
| 78 | static int apparmor_cred_prepare(struct cred *new, const struct cred *old, |
| 79 | gfp_t gfp) |
| 80 | { |
Casey Schaufler | 69b5a44 | 2018-09-21 17:17:59 -0700 | [diff] [blame] | 81 | set_cred_label(new, aa_get_newest_label(cred_label(old))); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * transfer the apparmor data to a blank set of creds |
| 87 | */ |
| 88 | static void apparmor_cred_transfer(struct cred *new, const struct cred *old) |
| 89 | { |
Casey Schaufler | 69b5a44 | 2018-09-21 17:17:59 -0700 | [diff] [blame] | 90 | set_cred_label(new, aa_get_newest_label(cred_label(old))); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 91 | } |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 92 | |
John Johansen | 3b529a7 | 2017-01-20 01:59:25 -0800 | [diff] [blame] | 93 | static void apparmor_task_free(struct task_struct *task) |
| 94 | { |
| 95 | |
| 96 | aa_free_task_ctx(task_ctx(task)); |
| 97 | task_ctx(task) = NULL; |
| 98 | } |
| 99 | |
| 100 | static int apparmor_task_alloc(struct task_struct *task, |
| 101 | unsigned long clone_flags) |
| 102 | { |
| 103 | struct aa_task_ctx *new = aa_alloc_task_ctx(GFP_KERNEL); |
| 104 | |
| 105 | if (!new) |
| 106 | return -ENOMEM; |
| 107 | |
John Johansen | de62de5 | 2017-10-08 00:43:02 -0700 | [diff] [blame] | 108 | aa_dup_task_ctx(new, task_ctx(current)); |
John Johansen | 3b529a7 | 2017-01-20 01:59:25 -0800 | [diff] [blame] | 109 | task_ctx(task) = new; |
| 110 | |
| 111 | return 0; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | static int apparmor_ptrace_access_check(struct task_struct *child, |
| 115 | unsigned int mode) |
| 116 | { |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 117 | struct aa_label *tracer, *tracee; |
| 118 | int error; |
| 119 | |
Jann Horn | 1f8266f | 2018-09-13 18:12:09 +0200 | [diff] [blame] | 120 | tracer = __begin_current_label_crit_section(); |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 121 | tracee = aa_get_task_label(child); |
| 122 | error = aa_may_ptrace(tracer, tracee, |
John Johansen | 338d0be | 2018-06-07 00:45:30 -0700 | [diff] [blame] | 123 | (mode & PTRACE_MODE_READ) ? AA_PTRACE_READ |
| 124 | : AA_PTRACE_TRACE); |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 125 | aa_put_label(tracee); |
Jann Horn | 1f8266f | 2018-09-13 18:12:09 +0200 | [diff] [blame] | 126 | __end_current_label_crit_section(tracer); |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 127 | |
| 128 | return error; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int apparmor_ptrace_traceme(struct task_struct *parent) |
| 132 | { |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 133 | struct aa_label *tracer, *tracee; |
| 134 | int error; |
| 135 | |
Jann Horn | ca3fde5 | 2018-09-29 03:49:26 +0200 | [diff] [blame] | 136 | tracee = __begin_current_label_crit_section(); |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 137 | tracer = aa_get_task_label(parent); |
| 138 | error = aa_may_ptrace(tracer, tracee, AA_PTRACE_TRACE); |
| 139 | aa_put_label(tracer); |
Jann Horn | ca3fde5 | 2018-09-29 03:49:26 +0200 | [diff] [blame] | 140 | __end_current_label_crit_section(tracee); |
John Johansen | b2d09ae | 2017-06-09 14:22:14 -0700 | [diff] [blame] | 141 | |
| 142 | return error; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Derived from security/commoncap.c:cap_capget */ |
| 146 | static int apparmor_capget(struct task_struct *target, kernel_cap_t *effective, |
| 147 | kernel_cap_t *inheritable, kernel_cap_t *permitted) |
| 148 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 149 | struct aa_label *label; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 150 | const struct cred *cred; |
| 151 | |
| 152 | rcu_read_lock(); |
| 153 | cred = __task_cred(target); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 154 | label = aa_get_newest_cred_label(cred); |
John Johansen | c70c86c | 2017-06-09 14:07:02 -0700 | [diff] [blame] | 155 | |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 156 | /* |
| 157 | * cap_capget is stacked ahead of this and will |
| 158 | * initialize effective and permitted. |
| 159 | */ |
John Johansen | c70c86c | 2017-06-09 14:07:02 -0700 | [diff] [blame] | 160 | if (!unconfined(label)) { |
| 161 | struct aa_profile *profile; |
| 162 | struct label_it i; |
| 163 | |
| 164 | label_for_each_confined(i, label, profile) { |
| 165 | if (COMPLAIN_MODE(profile)) |
| 166 | continue; |
| 167 | *effective = cap_intersect(*effective, |
| 168 | profile->caps.allow); |
| 169 | *permitted = cap_intersect(*permitted, |
| 170 | profile->caps.allow); |
| 171 | } |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 172 | } |
| 173 | rcu_read_unlock(); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 174 | aa_put_label(label); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Eric Paris | 6a9de49 | 2012-01-03 12:25:14 -0500 | [diff] [blame] | 179 | static int apparmor_capable(const struct cred *cred, struct user_namespace *ns, |
| 180 | int cap, int audit) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 181 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 182 | struct aa_label *label; |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 183 | int error = 0; |
| 184 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 185 | label = aa_get_newest_cred_label(cred); |
| 186 | if (!unconfined(label)) |
John Johansen | c70c86c | 2017-06-09 14:07:02 -0700 | [diff] [blame] | 187 | error = aa_capable(label, cap, audit); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 188 | aa_put_label(label); |
John Johansen | cf797c0 | 2017-06-09 02:08:28 -0700 | [diff] [blame] | 189 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 190 | return error; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * common_perm - basic common permission check wrapper fn for paths |
| 195 | * @op: operation being checked |
| 196 | * @path: path to check permission of (NOT NULL) |
| 197 | * @mask: requested permissions mask |
| 198 | * @cond: conditional info for the permission request (NOT NULL) |
| 199 | * |
| 200 | * Returns: %0 else error code if error or permission denied |
| 201 | */ |
John Johansen | 47f6e5c | 2017-01-16 00:43:01 -0800 | [diff] [blame] | 202 | static int common_perm(const char *op, const struct path *path, u32 mask, |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 203 | struct path_cond *cond) |
| 204 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 205 | struct aa_label *label; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 206 | int error = 0; |
| 207 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 208 | label = __begin_current_label_crit_section(); |
| 209 | if (!unconfined(label)) |
John Johansen | aebd873 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 210 | error = aa_path_perm(op, label, path, 0, mask, cond); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 211 | __end_current_label_crit_section(label); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 212 | |
| 213 | return error; |
| 214 | } |
| 215 | |
| 216 | /** |
John Johansen | 31f75bf | 2017-01-16 00:43:07 -0800 | [diff] [blame] | 217 | * common_perm_cond - common permission wrapper around inode cond |
| 218 | * @op: operation being checked |
| 219 | * @path: location to check (NOT NULL) |
| 220 | * @mask: requested permissions mask |
| 221 | * |
| 222 | * Returns: %0 else error code if error or permission denied |
| 223 | */ |
| 224 | static int common_perm_cond(const char *op, const struct path *path, u32 mask) |
| 225 | { |
| 226 | struct path_cond cond = { d_backing_inode(path->dentry)->i_uid, |
| 227 | d_backing_inode(path->dentry)->i_mode |
| 228 | }; |
| 229 | |
| 230 | if (!path_mediated_fs(path->dentry)) |
| 231 | return 0; |
| 232 | |
| 233 | return common_perm(op, path, mask, &cond); |
| 234 | } |
| 235 | |
| 236 | /** |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 237 | * common_perm_dir_dentry - common permission wrapper when path is dir, dentry |
| 238 | * @op: operation being checked |
| 239 | * @dir: directory of the dentry (NOT NULL) |
| 240 | * @dentry: dentry to check (NOT NULL) |
| 241 | * @mask: requested permissions mask |
| 242 | * @cond: conditional info for the permission request (NOT NULL) |
| 243 | * |
| 244 | * Returns: %0 else error code if error or permission denied |
| 245 | */ |
John Johansen | 47f6e5c | 2017-01-16 00:43:01 -0800 | [diff] [blame] | 246 | static int common_perm_dir_dentry(const char *op, const struct path *dir, |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 247 | struct dentry *dentry, u32 mask, |
| 248 | struct path_cond *cond) |
| 249 | { |
Kees Cook | 8486adf | 2016-12-16 17:04:13 -0800 | [diff] [blame] | 250 | struct path path = { .mnt = dir->mnt, .dentry = dentry }; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 251 | |
| 252 | return common_perm(op, &path, mask, cond); |
| 253 | } |
| 254 | |
| 255 | /** |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 256 | * common_perm_rm - common permission wrapper for operations doing rm |
| 257 | * @op: operation being checked |
| 258 | * @dir: directory that the dentry is in (NOT NULL) |
| 259 | * @dentry: dentry being rm'd (NOT NULL) |
| 260 | * @mask: requested permission mask |
| 261 | * |
| 262 | * Returns: %0 else error code if error or permission denied |
| 263 | */ |
John Johansen | 47f6e5c | 2017-01-16 00:43:01 -0800 | [diff] [blame] | 264 | static int common_perm_rm(const char *op, const struct path *dir, |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 265 | struct dentry *dentry, u32 mask) |
| 266 | { |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 267 | struct inode *inode = d_backing_inode(dentry); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 268 | struct path_cond cond = { }; |
| 269 | |
John Johansen | efeee83 | 2017-01-16 00:42:28 -0800 | [diff] [blame] | 270 | if (!inode || !path_mediated_fs(dentry)) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 271 | return 0; |
| 272 | |
| 273 | cond.uid = inode->i_uid; |
| 274 | cond.mode = inode->i_mode; |
| 275 | |
| 276 | return common_perm_dir_dentry(op, dir, dentry, mask, &cond); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * common_perm_create - common permission wrapper for operations doing create |
| 281 | * @op: operation being checked |
| 282 | * @dir: directory that dentry will be created in (NOT NULL) |
| 283 | * @dentry: dentry to create (NOT NULL) |
| 284 | * @mask: request permission mask |
| 285 | * @mode: created file mode |
| 286 | * |
| 287 | * Returns: %0 else error code if error or permission denied |
| 288 | */ |
John Johansen | 47f6e5c | 2017-01-16 00:43:01 -0800 | [diff] [blame] | 289 | static int common_perm_create(const char *op, const struct path *dir, |
Al Viro | d6b49f7 | 2016-03-25 15:10:04 -0400 | [diff] [blame] | 290 | struct dentry *dentry, u32 mask, umode_t mode) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 291 | { |
| 292 | struct path_cond cond = { current_fsuid(), mode }; |
| 293 | |
John Johansen | efeee83 | 2017-01-16 00:42:28 -0800 | [diff] [blame] | 294 | if (!path_mediated_fs(dir->dentry)) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 295 | return 0; |
| 296 | |
| 297 | return common_perm_dir_dentry(op, dir, dentry, mask, &cond); |
| 298 | } |
| 299 | |
Al Viro | 989f74e | 2016-03-25 15:13:39 -0400 | [diff] [blame] | 300 | static int apparmor_path_unlink(const struct path *dir, struct dentry *dentry) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 301 | { |
| 302 | return common_perm_rm(OP_UNLINK, dir, dentry, AA_MAY_DELETE); |
| 303 | } |
| 304 | |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 305 | static int apparmor_path_mkdir(const struct path *dir, struct dentry *dentry, |
Al Viro | 4572bef | 2011-11-21 14:56:21 -0500 | [diff] [blame] | 306 | umode_t mode) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 307 | { |
| 308 | return common_perm_create(OP_MKDIR, dir, dentry, AA_MAY_CREATE, |
| 309 | S_IFDIR); |
| 310 | } |
| 311 | |
Al Viro | 989f74e | 2016-03-25 15:13:39 -0400 | [diff] [blame] | 312 | static int apparmor_path_rmdir(const struct path *dir, struct dentry *dentry) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 313 | { |
| 314 | return common_perm_rm(OP_RMDIR, dir, dentry, AA_MAY_DELETE); |
| 315 | } |
| 316 | |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 317 | static int apparmor_path_mknod(const struct path *dir, struct dentry *dentry, |
Al Viro | 04fc66e | 2011-11-21 14:58:38 -0500 | [diff] [blame] | 318 | umode_t mode, unsigned int dev) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 319 | { |
| 320 | return common_perm_create(OP_MKNOD, dir, dentry, AA_MAY_CREATE, mode); |
| 321 | } |
| 322 | |
Al Viro | 81f4c50 | 2016-03-25 14:22:01 -0400 | [diff] [blame] | 323 | static int apparmor_path_truncate(const struct path *path) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 324 | { |
John Johansen | e53cfe6 | 2017-05-26 15:07:22 -0700 | [diff] [blame] | 325 | return common_perm_cond(OP_TRUNC, path, MAY_WRITE | AA_MAY_SETATTR); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Al Viro | d360775 | 2016-03-25 15:21:09 -0400 | [diff] [blame] | 328 | static int apparmor_path_symlink(const struct path *dir, struct dentry *dentry, |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 329 | const char *old_name) |
| 330 | { |
| 331 | return common_perm_create(OP_SYMLINK, dir, dentry, AA_MAY_CREATE, |
| 332 | S_IFLNK); |
| 333 | } |
| 334 | |
Al Viro | 3ccee46 | 2016-03-25 15:27:45 -0400 | [diff] [blame] | 335 | static int apparmor_path_link(struct dentry *old_dentry, const struct path *new_dir, |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 336 | struct dentry *new_dentry) |
| 337 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 338 | struct aa_label *label; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 339 | int error = 0; |
| 340 | |
John Johansen | efeee83 | 2017-01-16 00:42:28 -0800 | [diff] [blame] | 341 | if (!path_mediated_fs(old_dentry)) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 342 | return 0; |
| 343 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 344 | label = begin_current_label_crit_section(); |
| 345 | if (!unconfined(label)) |
John Johansen | 8014370 | 2017-06-09 16:06:21 -0700 | [diff] [blame] | 346 | error = aa_path_link(label, old_dentry, new_dir, new_dentry); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 347 | end_current_label_crit_section(label); |
John Johansen | cf797c0 | 2017-06-09 02:08:28 -0700 | [diff] [blame] | 348 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 349 | return error; |
| 350 | } |
| 351 | |
Al Viro | 3ccee46 | 2016-03-25 15:27:45 -0400 | [diff] [blame] | 352 | static int apparmor_path_rename(const struct path *old_dir, struct dentry *old_dentry, |
| 353 | const struct path *new_dir, struct dentry *new_dentry) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 354 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 355 | struct aa_label *label; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 356 | int error = 0; |
| 357 | |
John Johansen | efeee83 | 2017-01-16 00:42:28 -0800 | [diff] [blame] | 358 | if (!path_mediated_fs(old_dentry)) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 359 | return 0; |
| 360 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 361 | label = begin_current_label_crit_section(); |
| 362 | if (!unconfined(label)) { |
Kees Cook | 8486adf | 2016-12-16 17:04:13 -0800 | [diff] [blame] | 363 | struct path old_path = { .mnt = old_dir->mnt, |
| 364 | .dentry = old_dentry }; |
| 365 | struct path new_path = { .mnt = new_dir->mnt, |
| 366 | .dentry = new_dentry }; |
David Howells | c6f493d | 2015-03-17 22:26:22 +0000 | [diff] [blame] | 367 | struct path_cond cond = { d_backing_inode(old_dentry)->i_uid, |
| 368 | d_backing_inode(old_dentry)->i_mode |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 369 | }; |
| 370 | |
John Johansen | aebd873 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 371 | error = aa_path_perm(OP_RENAME_SRC, label, &old_path, 0, |
John Johansen | e53cfe6 | 2017-05-26 15:07:22 -0700 | [diff] [blame] | 372 | MAY_READ | AA_MAY_GETATTR | MAY_WRITE | |
| 373 | AA_MAY_SETATTR | AA_MAY_DELETE, |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 374 | &cond); |
| 375 | if (!error) |
John Johansen | aebd873 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 376 | error = aa_path_perm(OP_RENAME_DEST, label, &new_path, |
John Johansen | e53cfe6 | 2017-05-26 15:07:22 -0700 | [diff] [blame] | 377 | 0, MAY_WRITE | AA_MAY_SETATTR | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 378 | AA_MAY_CREATE, &cond); |
| 379 | |
| 380 | } |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 381 | end_current_label_crit_section(label); |
John Johansen | cf797c0 | 2017-06-09 02:08:28 -0700 | [diff] [blame] | 382 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 383 | return error; |
| 384 | } |
| 385 | |
Al Viro | be01f9f | 2016-03-25 14:56:23 -0400 | [diff] [blame] | 386 | static int apparmor_path_chmod(const struct path *path, umode_t mode) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 387 | { |
John Johansen | 31f75bf | 2017-01-16 00:43:07 -0800 | [diff] [blame] | 388 | return common_perm_cond(OP_CHMOD, path, AA_MAY_CHMOD); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Al Viro | 7fd25da | 2016-03-25 14:44:41 -0400 | [diff] [blame] | 391 | static int apparmor_path_chown(const struct path *path, kuid_t uid, kgid_t gid) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 392 | { |
John Johansen | 31f75bf | 2017-01-16 00:43:07 -0800 | [diff] [blame] | 393 | return common_perm_cond(OP_CHOWN, path, AA_MAY_CHOWN); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Al Viro | 3f7036a | 2015-03-08 19:28:30 -0400 | [diff] [blame] | 396 | static int apparmor_inode_getattr(const struct path *path) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 397 | { |
John Johansen | e53cfe6 | 2017-05-26 15:07:22 -0700 | [diff] [blame] | 398 | return common_perm_cond(OP_GETATTR, path, AA_MAY_GETATTR); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 401 | static int apparmor_file_open(struct file *file) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 402 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 403 | struct aa_file_ctx *fctx = file_ctx(file); |
| 404 | struct aa_label *label; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 405 | int error = 0; |
| 406 | |
John Johansen | efeee83 | 2017-01-16 00:42:28 -0800 | [diff] [blame] | 407 | if (!path_mediated_fs(file->f_path.dentry)) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 408 | return 0; |
| 409 | |
| 410 | /* If in exec, permission is handled by bprm hooks. |
| 411 | * Cache permissions granted by the previous exec check, with |
| 412 | * implicit read and executable mmap which are required to |
| 413 | * actually execute the image. |
| 414 | */ |
| 415 | if (current->in_execve) { |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 416 | fctx->allow = MAY_EXEC | MAY_READ | AA_EXEC_MMAP; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Al Viro | 9481769 | 2018-07-10 14:13:18 -0400 | [diff] [blame] | 420 | label = aa_get_newest_cred_label(file->f_cred); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 421 | if (!unconfined(label)) { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 422 | struct inode *inode = file_inode(file); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 423 | struct path_cond cond = { inode->i_uid, inode->i_mode }; |
| 424 | |
John Johansen | aebd873 | 2017-06-09 16:02:25 -0700 | [diff] [blame] | 425 | error = aa_path_perm(OP_OPEN, label, &file->f_path, 0, |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 426 | aa_map_file_to_perms(file), &cond); |
| 427 | /* todo cache full allowed permissions set and state */ |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 428 | fctx->allow = aa_map_file_to_perms(file); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 429 | } |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 430 | aa_put_label(label); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 431 | |
| 432 | return error; |
| 433 | } |
| 434 | |
| 435 | static int apparmor_file_alloc_security(struct file *file) |
| 436 | { |
Casey Schaufler | 33bf60c | 2018-11-12 12:02:49 -0800 | [diff] [blame^] | 437 | struct aa_file_ctx *ctx = file_ctx(file); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 438 | struct aa_label *label = begin_current_label_crit_section(); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 439 | |
Casey Schaufler | 33bf60c | 2018-11-12 12:02:49 -0800 | [diff] [blame^] | 440 | spin_lock_init(&ctx->lock); |
| 441 | rcu_assign_pointer(ctx->label, aa_get_label(label)); |
| 442 | end_current_label_crit_section(label); |
| 443 | return 0; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | static void apparmor_file_free_security(struct file *file) |
| 447 | { |
Casey Schaufler | 33bf60c | 2018-11-12 12:02:49 -0800 | [diff] [blame^] | 448 | struct aa_file_ctx *ctx = file_ctx(file); |
| 449 | |
| 450 | if (ctx) |
| 451 | aa_put_label(rcu_access_pointer(ctx->label)); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 452 | } |
| 453 | |
John Johansen | 47f6e5c | 2017-01-16 00:43:01 -0800 | [diff] [blame] | 454 | static int common_file_perm(const char *op, struct file *file, u32 mask) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 455 | { |
John Johansen | 190a951 | 2017-06-09 14:59:51 -0700 | [diff] [blame] | 456 | struct aa_label *label; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 457 | int error = 0; |
| 458 | |
John Johansen | 192ca6b | 2017-06-09 11:58:42 -0700 | [diff] [blame] | 459 | /* don't reaudit files closed during inheritance */ |
| 460 | if (file->f_path.dentry == aa_null.dentry) |
| 461 | return -EACCES; |
| 462 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 463 | label = __begin_current_label_crit_section(); |
John Johansen | 190a951 | 2017-06-09 14:59:51 -0700 | [diff] [blame] | 464 | error = aa_file_perm(op, label, file, mask); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 465 | __end_current_label_crit_section(label); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 466 | |
| 467 | return error; |
| 468 | } |
| 469 | |
John Johansen | 064dc94 | 2017-06-09 17:15:56 -0700 | [diff] [blame] | 470 | static int apparmor_file_receive(struct file *file) |
| 471 | { |
| 472 | return common_file_perm(OP_FRECEIVE, file, aa_map_file_to_perms(file)); |
| 473 | } |
| 474 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 475 | static int apparmor_file_permission(struct file *file, int mask) |
| 476 | { |
| 477 | return common_file_perm(OP_FPERM, file, mask); |
| 478 | } |
| 479 | |
| 480 | static int apparmor_file_lock(struct file *file, unsigned int cmd) |
| 481 | { |
| 482 | u32 mask = AA_MAY_LOCK; |
| 483 | |
| 484 | if (cmd == F_WRLCK) |
| 485 | mask |= MAY_WRITE; |
| 486 | |
| 487 | return common_file_perm(OP_FLOCK, file, mask); |
| 488 | } |
| 489 | |
John Johansen | 47f6e5c | 2017-01-16 00:43:01 -0800 | [diff] [blame] | 490 | static int common_mmap(const char *op, struct file *file, unsigned long prot, |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 491 | unsigned long flags) |
| 492 | { |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 493 | int mask = 0; |
| 494 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 495 | if (!file || !file_ctx(file)) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 496 | return 0; |
| 497 | |
| 498 | if (prot & PROT_READ) |
| 499 | mask |= MAY_READ; |
| 500 | /* |
| 501 | * Private mappings don't require write perms since they don't |
| 502 | * write back to the files |
| 503 | */ |
| 504 | if ((prot & PROT_WRITE) && !(flags & MAP_PRIVATE)) |
| 505 | mask |= MAY_WRITE; |
| 506 | if (prot & PROT_EXEC) |
| 507 | mask |= AA_EXEC_MMAP; |
| 508 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 509 | return common_file_perm(op, file, mask); |
| 510 | } |
| 511 | |
Al Viro | e546785 | 2012-05-30 13:30:51 -0400 | [diff] [blame] | 512 | static int apparmor_mmap_file(struct file *file, unsigned long reqprot, |
| 513 | unsigned long prot, unsigned long flags) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 514 | { |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 515 | return common_mmap(OP_FMMAP, file, prot, flags); |
| 516 | } |
| 517 | |
| 518 | static int apparmor_file_mprotect(struct vm_area_struct *vma, |
| 519 | unsigned long reqprot, unsigned long prot) |
| 520 | { |
| 521 | return common_mmap(OP_FMPROT, vma->vm_file, prot, |
| 522 | !(vma->vm_flags & VM_SHARED) ? MAP_PRIVATE : 0); |
| 523 | } |
| 524 | |
John Johansen | 2ea3ffb | 2017-07-18 23:04:47 -0700 | [diff] [blame] | 525 | static int apparmor_sb_mount(const char *dev_name, const struct path *path, |
| 526 | const char *type, unsigned long flags, void *data) |
| 527 | { |
| 528 | struct aa_label *label; |
| 529 | int error = 0; |
| 530 | |
| 531 | /* Discard magic */ |
| 532 | if ((flags & MS_MGC_MSK) == MS_MGC_VAL) |
| 533 | flags &= ~MS_MGC_MSK; |
| 534 | |
| 535 | flags &= ~AA_MS_IGNORE_MASK; |
| 536 | |
| 537 | label = __begin_current_label_crit_section(); |
| 538 | if (!unconfined(label)) { |
| 539 | if (flags & MS_REMOUNT) |
| 540 | error = aa_remount(label, path, flags, data); |
| 541 | else if (flags & MS_BIND) |
| 542 | error = aa_bind_mount(label, path, dev_name, flags); |
| 543 | else if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE | |
| 544 | MS_UNBINDABLE)) |
| 545 | error = aa_mount_change_type(label, path, flags); |
| 546 | else if (flags & MS_MOVE) |
| 547 | error = aa_move_mount(label, path, dev_name); |
| 548 | else |
| 549 | error = aa_new_mount(label, dev_name, path, type, |
| 550 | flags, data); |
| 551 | } |
| 552 | __end_current_label_crit_section(label); |
| 553 | |
| 554 | return error; |
| 555 | } |
| 556 | |
| 557 | static int apparmor_sb_umount(struct vfsmount *mnt, int flags) |
| 558 | { |
| 559 | struct aa_label *label; |
| 560 | int error = 0; |
| 561 | |
| 562 | label = __begin_current_label_crit_section(); |
| 563 | if (!unconfined(label)) |
| 564 | error = aa_umount(label, mnt, flags); |
| 565 | __end_current_label_crit_section(label); |
| 566 | |
| 567 | return error; |
| 568 | } |
| 569 | |
| 570 | static int apparmor_sb_pivotroot(const struct path *old_path, |
| 571 | const struct path *new_path) |
| 572 | { |
| 573 | struct aa_label *label; |
| 574 | int error = 0; |
| 575 | |
| 576 | label = aa_get_current_label(); |
| 577 | if (!unconfined(label)) |
| 578 | error = aa_pivotroot(label, old_path, new_path); |
| 579 | aa_put_label(label); |
| 580 | |
| 581 | return error; |
| 582 | } |
| 583 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 584 | static int apparmor_getprocattr(struct task_struct *task, char *name, |
| 585 | char **value) |
| 586 | { |
| 587 | int error = -ENOENT; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 588 | /* released below */ |
| 589 | const struct cred *cred = get_task_cred(task); |
John Johansen | de62de5 | 2017-10-08 00:43:02 -0700 | [diff] [blame] | 590 | struct aa_task_ctx *ctx = task_ctx(current); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 591 | struct aa_label *label = NULL; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 592 | |
| 593 | if (strcmp(name, "current") == 0) |
John Johansen | d9087c4 | 2017-01-27 03:53:53 -0800 | [diff] [blame] | 594 | label = aa_get_newest_label(cred_label(cred)); |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 595 | else if (strcmp(name, "prev") == 0 && ctx->previous) |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 596 | label = aa_get_newest_label(ctx->previous); |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 597 | else if (strcmp(name, "exec") == 0 && ctx->onexec) |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 598 | label = aa_get_newest_label(ctx->onexec); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 599 | else |
| 600 | error = -EINVAL; |
| 601 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 602 | if (label) |
John Johansen | 76a1d26 | 2017-06-09 12:47:17 -0700 | [diff] [blame] | 603 | error = aa_getprocattr(label, value); |
John Johansen | 77b071b | 2013-07-10 21:07:43 -0700 | [diff] [blame] | 604 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 605 | aa_put_label(label); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 606 | put_cred(cred); |
| 607 | |
| 608 | return error; |
| 609 | } |
| 610 | |
Stephen Smalley | b21507e | 2017-01-09 10:07:31 -0500 | [diff] [blame] | 611 | static int apparmor_setprocattr(const char *name, void *value, |
| 612 | size_t size) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 613 | { |
Vegard Nossum | e89b808 | 2016-07-07 13:41:11 -0700 | [diff] [blame] | 614 | char *command, *largs = NULL, *args = value; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 615 | size_t arg_size; |
| 616 | int error; |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 617 | DEFINE_AUDIT_DATA(sa, LSM_AUDIT_DATA_NONE, OP_SETPROCATTR); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 618 | |
| 619 | if (size == 0) |
| 620 | return -EINVAL; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 621 | |
Vegard Nossum | e89b808 | 2016-07-07 13:41:11 -0700 | [diff] [blame] | 622 | /* AppArmor requires that the buffer must be null terminated atm */ |
| 623 | if (args[size - 1] != '\0') { |
| 624 | /* null terminate */ |
| 625 | largs = args = kmalloc(size + 1, GFP_KERNEL); |
| 626 | if (!args) |
| 627 | return -ENOMEM; |
| 628 | memcpy(args, value, size); |
| 629 | args[size] = '\0'; |
| 630 | } |
| 631 | |
| 632 | error = -EINVAL; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 633 | args = strim(args); |
| 634 | command = strsep(&args, " "); |
| 635 | if (!args) |
Vegard Nossum | e89b808 | 2016-07-07 13:41:11 -0700 | [diff] [blame] | 636 | goto out; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 637 | args = skip_spaces(args); |
| 638 | if (!*args) |
Vegard Nossum | e89b808 | 2016-07-07 13:41:11 -0700 | [diff] [blame] | 639 | goto out; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 640 | |
John Johansen | d4d03f7 | 2016-07-09 23:46:33 -0700 | [diff] [blame] | 641 | arg_size = size - (args - (largs ? largs : (char *) value)); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 642 | if (strcmp(name, "current") == 0) { |
| 643 | if (strcmp(command, "changehat") == 0) { |
| 644 | error = aa_setprocattr_changehat(args, arg_size, |
John Johansen | df8073c | 2017-06-09 11:36:48 -0700 | [diff] [blame] | 645 | AA_CHANGE_NOFLAGS); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 646 | } else if (strcmp(command, "permhat") == 0) { |
| 647 | error = aa_setprocattr_changehat(args, arg_size, |
John Johansen | df8073c | 2017-06-09 11:36:48 -0700 | [diff] [blame] | 648 | AA_CHANGE_TEST); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 649 | } else if (strcmp(command, "changeprofile") == 0) { |
John Johansen | df8073c | 2017-06-09 11:36:48 -0700 | [diff] [blame] | 650 | error = aa_change_profile(args, AA_CHANGE_NOFLAGS); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 651 | } else if (strcmp(command, "permprofile") == 0) { |
John Johansen | df8073c | 2017-06-09 11:36:48 -0700 | [diff] [blame] | 652 | error = aa_change_profile(args, AA_CHANGE_TEST); |
John Johansen | 6c5fc8f | 2017-06-09 17:22:50 -0700 | [diff] [blame] | 653 | } else if (strcmp(command, "stack") == 0) { |
| 654 | error = aa_change_profile(args, AA_CHANGE_STACK); |
John Johansen | 3eea57c | 2013-02-27 03:44:40 -0800 | [diff] [blame] | 655 | } else |
| 656 | goto fail; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 657 | } else if (strcmp(name, "exec") == 0) { |
John Johansen | 3eea57c | 2013-02-27 03:44:40 -0800 | [diff] [blame] | 658 | if (strcmp(command, "exec") == 0) |
John Johansen | df8073c | 2017-06-09 11:36:48 -0700 | [diff] [blame] | 659 | error = aa_change_profile(args, AA_CHANGE_ONEXEC); |
John Johansen | 6c5fc8f | 2017-06-09 17:22:50 -0700 | [diff] [blame] | 660 | else if (strcmp(command, "stack") == 0) |
| 661 | error = aa_change_profile(args, (AA_CHANGE_ONEXEC | |
| 662 | AA_CHANGE_STACK)); |
John Johansen | 3eea57c | 2013-02-27 03:44:40 -0800 | [diff] [blame] | 663 | else |
| 664 | goto fail; |
| 665 | } else |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 666 | /* only support the "current" and "exec" process attributes */ |
Vegard Nossum | e89b808 | 2016-07-07 13:41:11 -0700 | [diff] [blame] | 667 | goto fail; |
John Johansen | 3eea57c | 2013-02-27 03:44:40 -0800 | [diff] [blame] | 668 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 669 | if (!error) |
| 670 | error = size; |
Vegard Nossum | e89b808 | 2016-07-07 13:41:11 -0700 | [diff] [blame] | 671 | out: |
| 672 | kfree(largs); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 673 | return error; |
John Johansen | 3eea57c | 2013-02-27 03:44:40 -0800 | [diff] [blame] | 674 | |
| 675 | fail: |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 676 | aad(&sa)->label = begin_current_label_crit_section(); |
John Johansen | ef88a7a | 2017-01-16 00:43:02 -0800 | [diff] [blame] | 677 | aad(&sa)->info = name; |
| 678 | aad(&sa)->error = error = -EINVAL; |
John Johansen | 3eea57c | 2013-02-27 03:44:40 -0800 | [diff] [blame] | 679 | aa_audit_msg(AUDIT_APPARMOR_DENIED, &sa, NULL); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 680 | end_current_label_crit_section(aad(&sa)->label); |
Vegard Nossum | e89b808 | 2016-07-07 13:41:11 -0700 | [diff] [blame] | 681 | goto out; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 682 | } |
| 683 | |
John Johansen | fe86482 | 2017-06-09 05:27:50 -0700 | [diff] [blame] | 684 | /** |
| 685 | * apparmor_bprm_committing_creds - do task cleanup on committing new creds |
| 686 | * @bprm: binprm for the exec (NOT NULL) |
| 687 | */ |
| 688 | static void apparmor_bprm_committing_creds(struct linux_binprm *bprm) |
| 689 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 690 | struct aa_label *label = aa_current_raw_label(); |
John Johansen | d9087c4 | 2017-01-27 03:53:53 -0800 | [diff] [blame] | 691 | struct aa_label *new_label = cred_label(bprm->cred); |
John Johansen | fe86482 | 2017-06-09 05:27:50 -0700 | [diff] [blame] | 692 | |
| 693 | /* bail out if unconfined or not changing profile */ |
John Johansen | d9087c4 | 2017-01-27 03:53:53 -0800 | [diff] [blame] | 694 | if ((new_label->proxy == label->proxy) || |
| 695 | (unconfined(new_label))) |
John Johansen | fe86482 | 2017-06-09 05:27:50 -0700 | [diff] [blame] | 696 | return; |
| 697 | |
John Johansen | 192ca6b | 2017-06-09 11:58:42 -0700 | [diff] [blame] | 698 | aa_inherit_files(bprm->cred, current->files); |
| 699 | |
John Johansen | fe86482 | 2017-06-09 05:27:50 -0700 | [diff] [blame] | 700 | current->pdeath_signal = 0; |
| 701 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 702 | /* reset soft limits and set hard limits for the new label */ |
John Johansen | d9087c4 | 2017-01-27 03:53:53 -0800 | [diff] [blame] | 703 | __aa_transition_rlimits(label, new_label); |
John Johansen | fe86482 | 2017-06-09 05:27:50 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | /** |
| 707 | * apparmor_bprm_committed_cred - do cleanup after new creds committed |
| 708 | * @bprm: binprm for the exec (NOT NULL) |
| 709 | */ |
| 710 | static void apparmor_bprm_committed_creds(struct linux_binprm *bprm) |
| 711 | { |
John Johansen | 3b529a7 | 2017-01-20 01:59:25 -0800 | [diff] [blame] | 712 | /* clear out temporary/transitional state from the context */ |
John Johansen | de62de5 | 2017-10-08 00:43:02 -0700 | [diff] [blame] | 713 | aa_clear_task_ctx_trans(task_ctx(current)); |
John Johansen | 3b529a7 | 2017-01-20 01:59:25 -0800 | [diff] [blame] | 714 | |
John Johansen | fe86482 | 2017-06-09 05:27:50 -0700 | [diff] [blame] | 715 | return; |
| 716 | } |
| 717 | |
John Johansen | a7ae364 | 2017-09-11 11:29:53 -0700 | [diff] [blame] | 718 | static void apparmor_task_getsecid(struct task_struct *p, u32 *secid) |
| 719 | { |
| 720 | struct aa_label *label = aa_get_task_label(p); |
| 721 | *secid = label->secid; |
| 722 | aa_put_label(label); |
| 723 | } |
| 724 | |
Jiri Slaby | 7cb4dc9 | 2010-08-11 11:28:02 +0200 | [diff] [blame] | 725 | static int apparmor_task_setrlimit(struct task_struct *task, |
| 726 | unsigned int resource, struct rlimit *new_rlim) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 727 | { |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 728 | struct aa_label *label = __begin_current_label_crit_section(); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 729 | int error = 0; |
| 730 | |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 731 | if (!unconfined(label)) |
John Johansen | 86b92cb | 2017-06-09 14:15:20 -0700 | [diff] [blame] | 732 | error = aa_task_setrlimit(label, task, resource, new_rlim); |
John Johansen | 637f688 | 2017-06-09 08:14:28 -0700 | [diff] [blame] | 733 | __end_current_label_crit_section(label); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 734 | |
| 735 | return error; |
| 736 | } |
| 737 | |
Eric W. Biederman | ae7795b | 2018-09-25 11:27:20 +0200 | [diff] [blame] | 738 | static int apparmor_task_kill(struct task_struct *target, struct kernel_siginfo *info, |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 739 | int sig, const struct cred *cred) |
John Johansen | cd1dbf7 | 2017-07-18 22:56:22 -0700 | [diff] [blame] | 740 | { |
| 741 | struct aa_label *cl, *tl; |
| 742 | int error; |
| 743 | |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 744 | if (cred) { |
| 745 | /* |
| 746 | * Dealing with USB IO specific behavior |
John Johansen | cd1dbf7 | 2017-07-18 22:56:22 -0700 | [diff] [blame] | 747 | */ |
Stephen Smalley | 6b4f3d0 | 2017-09-08 12:40:01 -0400 | [diff] [blame] | 748 | cl = aa_get_newest_cred_label(cred); |
| 749 | tl = aa_get_task_label(target); |
| 750 | error = aa_may_signal(cl, tl, sig); |
| 751 | aa_put_label(cl); |
| 752 | aa_put_label(tl); |
| 753 | return error; |
| 754 | } |
| 755 | |
John Johansen | cd1dbf7 | 2017-07-18 22:56:22 -0700 | [diff] [blame] | 756 | cl = __begin_current_label_crit_section(); |
| 757 | tl = aa_get_task_label(target); |
| 758 | error = aa_may_signal(cl, tl, sig); |
| 759 | aa_put_label(tl); |
| 760 | __end_current_label_crit_section(cl); |
| 761 | |
| 762 | return error; |
| 763 | } |
| 764 | |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 765 | /** |
| 766 | * apparmor_sk_alloc_security - allocate and attach the sk_security field |
| 767 | */ |
| 768 | static int apparmor_sk_alloc_security(struct sock *sk, int family, gfp_t flags) |
| 769 | { |
| 770 | struct aa_sk_ctx *ctx; |
| 771 | |
| 772 | ctx = kzalloc(sizeof(*ctx), flags); |
| 773 | if (!ctx) |
| 774 | return -ENOMEM; |
| 775 | |
| 776 | SK_CTX(sk) = ctx; |
| 777 | |
| 778 | return 0; |
| 779 | } |
| 780 | |
| 781 | /** |
| 782 | * apparmor_sk_free_security - free the sk_security field |
| 783 | */ |
| 784 | static void apparmor_sk_free_security(struct sock *sk) |
| 785 | { |
| 786 | struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 787 | |
| 788 | SK_CTX(sk) = NULL; |
| 789 | aa_put_label(ctx->label); |
| 790 | aa_put_label(ctx->peer); |
| 791 | kfree(ctx); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * apparmor_clone_security - clone the sk_security field |
| 796 | */ |
| 797 | static void apparmor_sk_clone_security(const struct sock *sk, |
| 798 | struct sock *newsk) |
| 799 | { |
| 800 | struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 801 | struct aa_sk_ctx *new = SK_CTX(newsk); |
| 802 | |
| 803 | new->label = aa_get_label(ctx->label); |
| 804 | new->peer = aa_get_label(ctx->peer); |
| 805 | } |
| 806 | |
| 807 | /** |
| 808 | * apparmor_socket_create - check perms before creating a new socket |
| 809 | */ |
| 810 | static int apparmor_socket_create(int family, int type, int protocol, int kern) |
| 811 | { |
| 812 | struct aa_label *label; |
| 813 | int error = 0; |
| 814 | |
| 815 | AA_BUG(in_interrupt()); |
| 816 | |
| 817 | label = begin_current_label_crit_section(); |
| 818 | if (!(kern || unconfined(label))) |
| 819 | error = af_select(family, |
| 820 | create_perm(label, family, type, protocol), |
| 821 | aa_af_perm(label, OP_CREATE, AA_MAY_CREATE, |
| 822 | family, type, protocol)); |
| 823 | end_current_label_crit_section(label); |
| 824 | |
| 825 | return error; |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * apparmor_socket_post_create - setup the per-socket security struct |
| 830 | * |
| 831 | * Note: |
| 832 | * - kernel sockets currently labeled unconfined but we may want to |
| 833 | * move to a special kernel label |
| 834 | * - socket may not have sk here if created with sock_create_lite or |
| 835 | * sock_alloc. These should be accept cases which will be handled in |
| 836 | * sock_graft. |
| 837 | */ |
| 838 | static int apparmor_socket_post_create(struct socket *sock, int family, |
| 839 | int type, int protocol, int kern) |
| 840 | { |
| 841 | struct aa_label *label; |
| 842 | |
| 843 | if (kern) { |
| 844 | struct aa_ns *ns = aa_get_current_ns(); |
| 845 | |
| 846 | label = aa_get_label(ns_unconfined(ns)); |
| 847 | aa_put_ns(ns); |
| 848 | } else |
| 849 | label = aa_get_current_label(); |
| 850 | |
| 851 | if (sock->sk) { |
| 852 | struct aa_sk_ctx *ctx = SK_CTX(sock->sk); |
| 853 | |
| 854 | aa_put_label(ctx->label); |
| 855 | ctx->label = aa_get_label(label); |
| 856 | } |
| 857 | aa_put_label(label); |
| 858 | |
| 859 | return 0; |
| 860 | } |
| 861 | |
| 862 | /** |
| 863 | * apparmor_socket_bind - check perms before bind addr to socket |
| 864 | */ |
| 865 | static int apparmor_socket_bind(struct socket *sock, |
| 866 | struct sockaddr *address, int addrlen) |
| 867 | { |
| 868 | AA_BUG(!sock); |
| 869 | AA_BUG(!sock->sk); |
| 870 | AA_BUG(!address); |
| 871 | AA_BUG(in_interrupt()); |
| 872 | |
| 873 | return af_select(sock->sk->sk_family, |
| 874 | bind_perm(sock, address, addrlen), |
| 875 | aa_sk_perm(OP_BIND, AA_MAY_BIND, sock->sk)); |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * apparmor_socket_connect - check perms before connecting @sock to @address |
| 880 | */ |
| 881 | static int apparmor_socket_connect(struct socket *sock, |
| 882 | struct sockaddr *address, int addrlen) |
| 883 | { |
| 884 | AA_BUG(!sock); |
| 885 | AA_BUG(!sock->sk); |
| 886 | AA_BUG(!address); |
| 887 | AA_BUG(in_interrupt()); |
| 888 | |
| 889 | return af_select(sock->sk->sk_family, |
| 890 | connect_perm(sock, address, addrlen), |
| 891 | aa_sk_perm(OP_CONNECT, AA_MAY_CONNECT, sock->sk)); |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * apparmor_socket_list - check perms before allowing listen |
| 896 | */ |
| 897 | static int apparmor_socket_listen(struct socket *sock, int backlog) |
| 898 | { |
| 899 | AA_BUG(!sock); |
| 900 | AA_BUG(!sock->sk); |
| 901 | AA_BUG(in_interrupt()); |
| 902 | |
| 903 | return af_select(sock->sk->sk_family, |
| 904 | listen_perm(sock, backlog), |
| 905 | aa_sk_perm(OP_LISTEN, AA_MAY_LISTEN, sock->sk)); |
| 906 | } |
| 907 | |
| 908 | /** |
| 909 | * apparmor_socket_accept - check perms before accepting a new connection. |
| 910 | * |
| 911 | * Note: while @newsock is created and has some information, the accept |
| 912 | * has not been done. |
| 913 | */ |
| 914 | static int apparmor_socket_accept(struct socket *sock, struct socket *newsock) |
| 915 | { |
| 916 | AA_BUG(!sock); |
| 917 | AA_BUG(!sock->sk); |
| 918 | AA_BUG(!newsock); |
| 919 | AA_BUG(in_interrupt()); |
| 920 | |
| 921 | return af_select(sock->sk->sk_family, |
| 922 | accept_perm(sock, newsock), |
| 923 | aa_sk_perm(OP_ACCEPT, AA_MAY_ACCEPT, sock->sk)); |
| 924 | } |
| 925 | |
| 926 | static int aa_sock_msg_perm(const char *op, u32 request, struct socket *sock, |
| 927 | struct msghdr *msg, int size) |
| 928 | { |
| 929 | AA_BUG(!sock); |
| 930 | AA_BUG(!sock->sk); |
| 931 | AA_BUG(!msg); |
| 932 | AA_BUG(in_interrupt()); |
| 933 | |
| 934 | return af_select(sock->sk->sk_family, |
| 935 | msg_perm(op, request, sock, msg, size), |
| 936 | aa_sk_perm(op, request, sock->sk)); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * apparmor_socket_sendmsg - check perms before sending msg to another socket |
| 941 | */ |
| 942 | static int apparmor_socket_sendmsg(struct socket *sock, |
| 943 | struct msghdr *msg, int size) |
| 944 | { |
| 945 | return aa_sock_msg_perm(OP_SENDMSG, AA_MAY_SEND, sock, msg, size); |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * apparmor_socket_recvmsg - check perms before receiving a message |
| 950 | */ |
| 951 | static int apparmor_socket_recvmsg(struct socket *sock, |
| 952 | struct msghdr *msg, int size, int flags) |
| 953 | { |
| 954 | return aa_sock_msg_perm(OP_RECVMSG, AA_MAY_RECEIVE, sock, msg, size); |
| 955 | } |
| 956 | |
| 957 | /* revaliation, get/set attr, shutdown */ |
| 958 | static int aa_sock_perm(const char *op, u32 request, struct socket *sock) |
| 959 | { |
| 960 | AA_BUG(!sock); |
| 961 | AA_BUG(!sock->sk); |
| 962 | AA_BUG(in_interrupt()); |
| 963 | |
| 964 | return af_select(sock->sk->sk_family, |
| 965 | sock_perm(op, request, sock), |
| 966 | aa_sk_perm(op, request, sock->sk)); |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * apparmor_socket_getsockname - check perms before getting the local address |
| 971 | */ |
| 972 | static int apparmor_socket_getsockname(struct socket *sock) |
| 973 | { |
| 974 | return aa_sock_perm(OP_GETSOCKNAME, AA_MAY_GETATTR, sock); |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * apparmor_socket_getpeername - check perms before getting remote address |
| 979 | */ |
| 980 | static int apparmor_socket_getpeername(struct socket *sock) |
| 981 | { |
| 982 | return aa_sock_perm(OP_GETPEERNAME, AA_MAY_GETATTR, sock); |
| 983 | } |
| 984 | |
| 985 | /* revaliation, get/set attr, opt */ |
| 986 | static int aa_sock_opt_perm(const char *op, u32 request, struct socket *sock, |
| 987 | int level, int optname) |
| 988 | { |
| 989 | AA_BUG(!sock); |
| 990 | AA_BUG(!sock->sk); |
| 991 | AA_BUG(in_interrupt()); |
| 992 | |
| 993 | return af_select(sock->sk->sk_family, |
| 994 | opt_perm(op, request, sock, level, optname), |
| 995 | aa_sk_perm(op, request, sock->sk)); |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * apparmor_getsockopt - check perms before getting socket options |
| 1000 | */ |
| 1001 | static int apparmor_socket_getsockopt(struct socket *sock, int level, |
| 1002 | int optname) |
| 1003 | { |
| 1004 | return aa_sock_opt_perm(OP_GETSOCKOPT, AA_MAY_GETOPT, sock, |
| 1005 | level, optname); |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * apparmor_setsockopt - check perms before setting socket options |
| 1010 | */ |
| 1011 | static int apparmor_socket_setsockopt(struct socket *sock, int level, |
| 1012 | int optname) |
| 1013 | { |
| 1014 | return aa_sock_opt_perm(OP_SETSOCKOPT, AA_MAY_SETOPT, sock, |
| 1015 | level, optname); |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * apparmor_socket_shutdown - check perms before shutting down @sock conn |
| 1020 | */ |
| 1021 | static int apparmor_socket_shutdown(struct socket *sock, int how) |
| 1022 | { |
| 1023 | return aa_sock_perm(OP_SHUTDOWN, AA_MAY_SHUTDOWN, sock); |
| 1024 | } |
| 1025 | |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1026 | #ifdef CONFIG_NETWORK_SECMARK |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 1027 | /** |
| 1028 | * apparmor_socket_sock_recv_skb - check perms before associating skb to sk |
| 1029 | * |
| 1030 | * Note: can not sleep may be called with locks held |
| 1031 | * |
| 1032 | * dont want protocol specific in __skb_recv_datagram() |
| 1033 | * to deny an incoming connection socket_sock_rcv_skb() |
| 1034 | */ |
| 1035 | static int apparmor_socket_sock_rcv_skb(struct sock *sk, struct sk_buff *skb) |
| 1036 | { |
Matthew Garrett | ab9f211 | 2018-05-24 13:27:47 -0700 | [diff] [blame] | 1037 | struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 1038 | |
| 1039 | if (!skb->secmark) |
| 1040 | return 0; |
| 1041 | |
| 1042 | return apparmor_secmark_check(ctx->label, OP_RECVMSG, AA_MAY_RECEIVE, |
| 1043 | skb->secmark, sk); |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 1044 | } |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1045 | #endif |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 1046 | |
| 1047 | |
| 1048 | static struct aa_label *sk_peer_label(struct sock *sk) |
| 1049 | { |
| 1050 | struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 1051 | |
| 1052 | if (ctx->peer) |
| 1053 | return ctx->peer; |
| 1054 | |
| 1055 | return ERR_PTR(-ENOPROTOOPT); |
| 1056 | } |
| 1057 | |
| 1058 | /** |
| 1059 | * apparmor_socket_getpeersec_stream - get security context of peer |
| 1060 | * |
| 1061 | * Note: for tcp only valid if using ipsec or cipso on lan |
| 1062 | */ |
| 1063 | static int apparmor_socket_getpeersec_stream(struct socket *sock, |
| 1064 | char __user *optval, |
| 1065 | int __user *optlen, |
| 1066 | unsigned int len) |
| 1067 | { |
| 1068 | char *name; |
| 1069 | int slen, error = 0; |
| 1070 | struct aa_label *label; |
| 1071 | struct aa_label *peer; |
| 1072 | |
| 1073 | label = begin_current_label_crit_section(); |
| 1074 | peer = sk_peer_label(sock->sk); |
| 1075 | if (IS_ERR(peer)) { |
| 1076 | error = PTR_ERR(peer); |
| 1077 | goto done; |
| 1078 | } |
| 1079 | slen = aa_label_asxprint(&name, labels_ns(label), peer, |
| 1080 | FLAG_SHOW_MODE | FLAG_VIEW_SUBNS | |
| 1081 | FLAG_HIDDEN_UNCONFINED, GFP_KERNEL); |
| 1082 | /* don't include terminating \0 in slen, it breaks some apps */ |
| 1083 | if (slen < 0) { |
| 1084 | error = -ENOMEM; |
| 1085 | } else { |
| 1086 | if (slen > len) { |
| 1087 | error = -ERANGE; |
| 1088 | } else if (copy_to_user(optval, name, slen)) { |
| 1089 | error = -EFAULT; |
| 1090 | goto out; |
| 1091 | } |
| 1092 | if (put_user(slen, optlen)) |
| 1093 | error = -EFAULT; |
| 1094 | out: |
| 1095 | kfree(name); |
| 1096 | |
| 1097 | } |
| 1098 | |
| 1099 | done: |
| 1100 | end_current_label_crit_section(label); |
| 1101 | |
| 1102 | return error; |
| 1103 | } |
| 1104 | |
| 1105 | /** |
| 1106 | * apparmor_socket_getpeersec_dgram - get security label of packet |
| 1107 | * @sock: the peer socket |
| 1108 | * @skb: packet data |
| 1109 | * @secid: pointer to where to put the secid of the packet |
| 1110 | * |
| 1111 | * Sets the netlabel socket state on sk from parent |
| 1112 | */ |
| 1113 | static int apparmor_socket_getpeersec_dgram(struct socket *sock, |
| 1114 | struct sk_buff *skb, u32 *secid) |
| 1115 | |
| 1116 | { |
| 1117 | /* TODO: requires secid support */ |
| 1118 | return -ENOPROTOOPT; |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * apparmor_sock_graft - Initialize newly created socket |
| 1123 | * @sk: child sock |
| 1124 | * @parent: parent socket |
| 1125 | * |
| 1126 | * Note: could set off of SOCK_CTX(parent) but need to track inode and we can |
| 1127 | * just set sk security information off of current creating process label |
| 1128 | * Labeling of sk for accept case - probably should be sock based |
| 1129 | * instead of task, because of the case where an implicitly labeled |
| 1130 | * socket is shared by different tasks. |
| 1131 | */ |
| 1132 | static void apparmor_sock_graft(struct sock *sk, struct socket *parent) |
| 1133 | { |
| 1134 | struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 1135 | |
| 1136 | if (!ctx->label) |
| 1137 | ctx->label = aa_get_current_label(); |
| 1138 | } |
| 1139 | |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1140 | #ifdef CONFIG_NETWORK_SECMARK |
Matthew Garrett | ab9f211 | 2018-05-24 13:27:47 -0700 | [diff] [blame] | 1141 | static int apparmor_inet_conn_request(struct sock *sk, struct sk_buff *skb, |
| 1142 | struct request_sock *req) |
| 1143 | { |
| 1144 | struct aa_sk_ctx *ctx = SK_CTX(sk); |
| 1145 | |
| 1146 | if (!skb->secmark) |
| 1147 | return 0; |
| 1148 | |
| 1149 | return apparmor_secmark_check(ctx->label, OP_CONNECT, AA_MAY_CONNECT, |
| 1150 | skb->secmark, sk); |
| 1151 | } |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1152 | #endif |
Matthew Garrett | ab9f211 | 2018-05-24 13:27:47 -0700 | [diff] [blame] | 1153 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 1154 | /* |
| 1155 | * The cred blob is a pointer to, not an instance of, an aa_task_ctx. |
| 1156 | */ |
| 1157 | struct lsm_blob_sizes apparmor_blob_sizes __lsm_ro_after_init = { |
| 1158 | .lbs_cred = sizeof(struct aa_task_ctx *), |
Casey Schaufler | 33bf60c | 2018-11-12 12:02:49 -0800 | [diff] [blame^] | 1159 | .lbs_file = sizeof(struct aa_file_ctx), |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 1160 | }; |
| 1161 | |
James Morris | ca97d93 | 2017-02-15 00:18:51 +1100 | [diff] [blame] | 1162 | static struct security_hook_list apparmor_hooks[] __lsm_ro_after_init = { |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1163 | LSM_HOOK_INIT(ptrace_access_check, apparmor_ptrace_access_check), |
| 1164 | LSM_HOOK_INIT(ptrace_traceme, apparmor_ptrace_traceme), |
| 1165 | LSM_HOOK_INIT(capget, apparmor_capget), |
| 1166 | LSM_HOOK_INIT(capable, apparmor_capable), |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1167 | |
John Johansen | 2ea3ffb | 2017-07-18 23:04:47 -0700 | [diff] [blame] | 1168 | LSM_HOOK_INIT(sb_mount, apparmor_sb_mount), |
| 1169 | LSM_HOOK_INIT(sb_umount, apparmor_sb_umount), |
| 1170 | LSM_HOOK_INIT(sb_pivotroot, apparmor_sb_pivotroot), |
| 1171 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1172 | LSM_HOOK_INIT(path_link, apparmor_path_link), |
| 1173 | LSM_HOOK_INIT(path_unlink, apparmor_path_unlink), |
| 1174 | LSM_HOOK_INIT(path_symlink, apparmor_path_symlink), |
| 1175 | LSM_HOOK_INIT(path_mkdir, apparmor_path_mkdir), |
| 1176 | LSM_HOOK_INIT(path_rmdir, apparmor_path_rmdir), |
| 1177 | LSM_HOOK_INIT(path_mknod, apparmor_path_mknod), |
| 1178 | LSM_HOOK_INIT(path_rename, apparmor_path_rename), |
| 1179 | LSM_HOOK_INIT(path_chmod, apparmor_path_chmod), |
| 1180 | LSM_HOOK_INIT(path_chown, apparmor_path_chown), |
| 1181 | LSM_HOOK_INIT(path_truncate, apparmor_path_truncate), |
| 1182 | LSM_HOOK_INIT(inode_getattr, apparmor_inode_getattr), |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1183 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1184 | LSM_HOOK_INIT(file_open, apparmor_file_open), |
John Johansen | 064dc94 | 2017-06-09 17:15:56 -0700 | [diff] [blame] | 1185 | LSM_HOOK_INIT(file_receive, apparmor_file_receive), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1186 | LSM_HOOK_INIT(file_permission, apparmor_file_permission), |
| 1187 | LSM_HOOK_INIT(file_alloc_security, apparmor_file_alloc_security), |
| 1188 | LSM_HOOK_INIT(file_free_security, apparmor_file_free_security), |
| 1189 | LSM_HOOK_INIT(mmap_file, apparmor_mmap_file), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1190 | LSM_HOOK_INIT(file_mprotect, apparmor_file_mprotect), |
| 1191 | LSM_HOOK_INIT(file_lock, apparmor_file_lock), |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1192 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1193 | LSM_HOOK_INIT(getprocattr, apparmor_getprocattr), |
| 1194 | LSM_HOOK_INIT(setprocattr, apparmor_setprocattr), |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1195 | |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 1196 | LSM_HOOK_INIT(sk_alloc_security, apparmor_sk_alloc_security), |
| 1197 | LSM_HOOK_INIT(sk_free_security, apparmor_sk_free_security), |
| 1198 | LSM_HOOK_INIT(sk_clone_security, apparmor_sk_clone_security), |
| 1199 | |
| 1200 | LSM_HOOK_INIT(socket_create, apparmor_socket_create), |
| 1201 | LSM_HOOK_INIT(socket_post_create, apparmor_socket_post_create), |
| 1202 | LSM_HOOK_INIT(socket_bind, apparmor_socket_bind), |
| 1203 | LSM_HOOK_INIT(socket_connect, apparmor_socket_connect), |
| 1204 | LSM_HOOK_INIT(socket_listen, apparmor_socket_listen), |
| 1205 | LSM_HOOK_INIT(socket_accept, apparmor_socket_accept), |
| 1206 | LSM_HOOK_INIT(socket_sendmsg, apparmor_socket_sendmsg), |
| 1207 | LSM_HOOK_INIT(socket_recvmsg, apparmor_socket_recvmsg), |
| 1208 | LSM_HOOK_INIT(socket_getsockname, apparmor_socket_getsockname), |
| 1209 | LSM_HOOK_INIT(socket_getpeername, apparmor_socket_getpeername), |
| 1210 | LSM_HOOK_INIT(socket_getsockopt, apparmor_socket_getsockopt), |
| 1211 | LSM_HOOK_INIT(socket_setsockopt, apparmor_socket_setsockopt), |
| 1212 | LSM_HOOK_INIT(socket_shutdown, apparmor_socket_shutdown), |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1213 | #ifdef CONFIG_NETWORK_SECMARK |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 1214 | LSM_HOOK_INIT(socket_sock_rcv_skb, apparmor_socket_sock_rcv_skb), |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1215 | #endif |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 1216 | LSM_HOOK_INIT(socket_getpeersec_stream, |
| 1217 | apparmor_socket_getpeersec_stream), |
| 1218 | LSM_HOOK_INIT(socket_getpeersec_dgram, |
| 1219 | apparmor_socket_getpeersec_dgram), |
| 1220 | LSM_HOOK_INIT(sock_graft, apparmor_sock_graft), |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1221 | #ifdef CONFIG_NETWORK_SECMARK |
Matthew Garrett | ab9f211 | 2018-05-24 13:27:47 -0700 | [diff] [blame] | 1222 | LSM_HOOK_INIT(inet_conn_request, apparmor_inet_conn_request), |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1223 | #endif |
John Johansen | 56974a6 | 2017-07-18 23:18:33 -0700 | [diff] [blame] | 1224 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1225 | LSM_HOOK_INIT(cred_alloc_blank, apparmor_cred_alloc_blank), |
| 1226 | LSM_HOOK_INIT(cred_free, apparmor_cred_free), |
| 1227 | LSM_HOOK_INIT(cred_prepare, apparmor_cred_prepare), |
| 1228 | LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer), |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1229 | |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1230 | LSM_HOOK_INIT(bprm_set_creds, apparmor_bprm_set_creds), |
| 1231 | LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds), |
| 1232 | LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds), |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1233 | |
John Johansen | 3b529a7 | 2017-01-20 01:59:25 -0800 | [diff] [blame] | 1234 | LSM_HOOK_INIT(task_free, apparmor_task_free), |
| 1235 | LSM_HOOK_INIT(task_alloc, apparmor_task_alloc), |
John Johansen | a7ae364 | 2017-09-11 11:29:53 -0700 | [diff] [blame] | 1236 | LSM_HOOK_INIT(task_getsecid, apparmor_task_getsecid), |
Casey Schaufler | e20b043 | 2015-05-02 15:11:36 -0700 | [diff] [blame] | 1237 | LSM_HOOK_INIT(task_setrlimit, apparmor_task_setrlimit), |
John Johansen | cd1dbf7 | 2017-07-18 22:56:22 -0700 | [diff] [blame] | 1238 | LSM_HOOK_INIT(task_kill, apparmor_task_kill), |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 1239 | |
Matthew Garrett | e79c26d | 2018-04-16 11:23:58 -0700 | [diff] [blame] | 1240 | #ifdef CONFIG_AUDIT |
| 1241 | LSM_HOOK_INIT(audit_rule_init, aa_audit_rule_init), |
| 1242 | LSM_HOOK_INIT(audit_rule_known, aa_audit_rule_known), |
| 1243 | LSM_HOOK_INIT(audit_rule_match, aa_audit_rule_match), |
| 1244 | LSM_HOOK_INIT(audit_rule_free, aa_audit_rule_free), |
| 1245 | #endif |
| 1246 | |
John Johansen | c092921 | 2017-07-31 17:36:45 -0700 | [diff] [blame] | 1247 | LSM_HOOK_INIT(secid_to_secctx, apparmor_secid_to_secctx), |
| 1248 | LSM_HOOK_INIT(secctx_to_secid, apparmor_secctx_to_secid), |
| 1249 | LSM_HOOK_INIT(release_secctx, apparmor_release_secctx), |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1250 | }; |
| 1251 | |
| 1252 | /* |
| 1253 | * AppArmor sysfs module parameters |
| 1254 | */ |
| 1255 | |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1256 | static int param_set_aabool(const char *val, const struct kernel_param *kp); |
| 1257 | static int param_get_aabool(char *buffer, const struct kernel_param *kp); |
Rusty Russell | b8aa09f | 2011-12-15 13:41:32 +1030 | [diff] [blame] | 1258 | #define param_check_aabool param_check_bool |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 1259 | static const struct kernel_param_ops param_ops_aabool = { |
Jani Nikula | 6a4c264 | 2014-08-27 06:21:23 +0930 | [diff] [blame] | 1260 | .flags = KERNEL_PARAM_OPS_FL_NOARG, |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1261 | .set = param_set_aabool, |
| 1262 | .get = param_get_aabool |
| 1263 | }; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1264 | |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1265 | static int param_set_aauint(const char *val, const struct kernel_param *kp); |
| 1266 | static int param_get_aauint(char *buffer, const struct kernel_param *kp); |
Rusty Russell | b8aa09f | 2011-12-15 13:41:32 +1030 | [diff] [blame] | 1267 | #define param_check_aauint param_check_uint |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 1268 | static const struct kernel_param_ops param_ops_aauint = { |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1269 | .set = param_set_aauint, |
| 1270 | .get = param_get_aauint |
| 1271 | }; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1272 | |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1273 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp); |
| 1274 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp); |
Rusty Russell | b8aa09f | 2011-12-15 13:41:32 +1030 | [diff] [blame] | 1275 | #define param_check_aalockpolicy param_check_bool |
Luis R. Rodriguez | 9c27847 | 2015-05-27 11:09:38 +0930 | [diff] [blame] | 1276 | static const struct kernel_param_ops param_ops_aalockpolicy = { |
Jani Nikula | 6a4c264 | 2014-08-27 06:21:23 +0930 | [diff] [blame] | 1277 | .flags = KERNEL_PARAM_OPS_FL_NOARG, |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1278 | .set = param_set_aalockpolicy, |
| 1279 | .get = param_get_aalockpolicy |
| 1280 | }; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1281 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 1282 | static int param_set_audit(const char *val, const struct kernel_param *kp); |
| 1283 | static int param_get_audit(char *buffer, const struct kernel_param *kp); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1284 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 1285 | static int param_set_mode(const char *val, const struct kernel_param *kp); |
| 1286 | static int param_get_mode(char *buffer, const struct kernel_param *kp); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1287 | |
| 1288 | /* Flag values, also controllable via /sys/module/apparmor/parameters |
| 1289 | * We define special types as we want to do additional mediation. |
| 1290 | */ |
| 1291 | |
| 1292 | /* AppArmor global enforcement switch - complain, enforce, kill */ |
| 1293 | enum profile_mode aa_g_profile_mode = APPARMOR_ENFORCE; |
| 1294 | module_param_call(mode, param_set_mode, param_get_mode, |
| 1295 | &aa_g_profile_mode, S_IRUSR | S_IWUSR); |
| 1296 | |
John Johansen | 6059f71 | 2014-10-24 09:16:14 -0700 | [diff] [blame] | 1297 | /* whether policy verification hashing is enabled */ |
Arnd Bergmann | 7616ac7 | 2016-07-25 10:59:07 -0700 | [diff] [blame] | 1298 | bool aa_g_hash_policy = IS_ENABLED(CONFIG_SECURITY_APPARMOR_HASH_DEFAULT); |
John Johansen | 3ccb76c | 2017-01-16 13:21:27 -0800 | [diff] [blame] | 1299 | #ifdef CONFIG_SECURITY_APPARMOR_HASH |
John Johansen | 6059f71 | 2014-10-24 09:16:14 -0700 | [diff] [blame] | 1300 | module_param_named(hash_policy, aa_g_hash_policy, aabool, S_IRUSR | S_IWUSR); |
Arnd Bergmann | 7616ac7 | 2016-07-25 10:59:07 -0700 | [diff] [blame] | 1301 | #endif |
John Johansen | 6059f71 | 2014-10-24 09:16:14 -0700 | [diff] [blame] | 1302 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1303 | /* Debug mode */ |
Valentin Rothberg | eea7a05 | 2017-04-06 06:55:20 -0700 | [diff] [blame] | 1304 | bool aa_g_debug = IS_ENABLED(CONFIG_SECURITY_APPARMOR_DEBUG_MESSAGES); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1305 | module_param_named(debug, aa_g_debug, aabool, S_IRUSR | S_IWUSR); |
| 1306 | |
| 1307 | /* Audit mode */ |
| 1308 | enum audit_mode aa_g_audit; |
| 1309 | module_param_call(audit, param_set_audit, param_get_audit, |
| 1310 | &aa_g_audit, S_IRUSR | S_IWUSR); |
| 1311 | |
| 1312 | /* Determines if audit header is included in audited messages. This |
| 1313 | * provides more context if the audit daemon is not running |
| 1314 | */ |
Thomas Meyer | 954317f | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 1315 | bool aa_g_audit_header = true; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1316 | module_param_named(audit_header, aa_g_audit_header, aabool, |
| 1317 | S_IRUSR | S_IWUSR); |
| 1318 | |
| 1319 | /* lock out loading/removal of policy |
| 1320 | * TODO: add in at boot loading of policy, which is the only way to |
| 1321 | * load policy, if lock_policy is set |
| 1322 | */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 1323 | bool aa_g_lock_policy; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1324 | module_param_named(lock_policy, aa_g_lock_policy, aalockpolicy, |
| 1325 | S_IRUSR | S_IWUSR); |
| 1326 | |
| 1327 | /* Syscall logging mode */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 1328 | bool aa_g_logsyscall; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1329 | module_param_named(logsyscall, aa_g_logsyscall, aabool, S_IRUSR | S_IWUSR); |
| 1330 | |
| 1331 | /* Maximum pathname length before accesses will start getting rejected */ |
| 1332 | unsigned int aa_g_path_max = 2 * PATH_MAX; |
John Johansen | 622f6e3 | 2017-04-06 06:55:24 -0700 | [diff] [blame] | 1333 | module_param_named(path_max, aa_g_path_max, aauint, S_IRUSR); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1334 | |
| 1335 | /* Determines how paranoid loading of policy is and how much verification |
| 1336 | * on the loaded policy is done. |
John Johansen | abbf873 | 2017-01-16 00:42:37 -0800 | [diff] [blame] | 1337 | * DEPRECATED: read only as strict checking of load is always done now |
| 1338 | * that none root users (user namespaces) can load policy. |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1339 | */ |
Thomas Meyer | 954317f | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 1340 | bool aa_g_paranoid_load = true; |
John Johansen | abbf873 | 2017-01-16 00:42:37 -0800 | [diff] [blame] | 1341 | module_param_named(paranoid_load, aa_g_paranoid_load, aabool, S_IRUGO); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1342 | |
| 1343 | /* Boot time disable flag */ |
Kees Cook | 0102fb8 | 2018-10-01 17:08:57 -0700 | [diff] [blame] | 1344 | static int apparmor_enabled __lsm_ro_after_init = 1; |
Kees Cook | c5459b8 | 2018-09-13 22:28:48 -0700 | [diff] [blame] | 1345 | module_param_named(enabled, apparmor_enabled, int, 0444); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1346 | |
| 1347 | static int __init apparmor_enabled_setup(char *str) |
| 1348 | { |
| 1349 | unsigned long enabled; |
Jingoo Han | 29707b2 | 2014-02-05 15:13:14 +0900 | [diff] [blame] | 1350 | int error = kstrtoul(str, 0, &enabled); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1351 | if (!error) |
| 1352 | apparmor_enabled = enabled ? 1 : 0; |
| 1353 | return 1; |
| 1354 | } |
| 1355 | |
| 1356 | __setup("apparmor=", apparmor_enabled_setup); |
| 1357 | |
| 1358 | /* set global flag turning off the ability to load policy */ |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1359 | static int param_set_aalockpolicy(const char *val, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1360 | { |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1361 | if (!apparmor_enabled) |
| 1362 | return -EINVAL; |
| 1363 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1364 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1365 | return param_set_bool(val, kp); |
| 1366 | } |
| 1367 | |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1368 | static int param_get_aalockpolicy(char *buffer, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1369 | { |
John Johansen | ca4bd5a | 2017-01-16 00:43:11 -0800 | [diff] [blame] | 1370 | if (!apparmor_enabled) |
| 1371 | return -EINVAL; |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1372 | if (apparmor_initialized && !policy_view_capable(NULL)) |
| 1373 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1374 | return param_get_bool(buffer, kp); |
| 1375 | } |
| 1376 | |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1377 | static int param_set_aabool(const char *val, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1378 | { |
John Johansen | ca4bd5a | 2017-01-16 00:43:11 -0800 | [diff] [blame] | 1379 | if (!apparmor_enabled) |
| 1380 | return -EINVAL; |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1381 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
| 1382 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1383 | return param_set_bool(val, kp); |
| 1384 | } |
| 1385 | |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1386 | static int param_get_aabool(char *buffer, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1387 | { |
John Johansen | ca4bd5a | 2017-01-16 00:43:11 -0800 | [diff] [blame] | 1388 | if (!apparmor_enabled) |
| 1389 | return -EINVAL; |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1390 | if (apparmor_initialized && !policy_view_capable(NULL)) |
| 1391 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1392 | return param_get_bool(buffer, kp); |
| 1393 | } |
| 1394 | |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1395 | static int param_set_aauint(const char *val, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1396 | { |
John Johansen | 39d8482 | 2017-03-30 05:25:23 -0700 | [diff] [blame] | 1397 | int error; |
| 1398 | |
John Johansen | ca4bd5a | 2017-01-16 00:43:11 -0800 | [diff] [blame] | 1399 | if (!apparmor_enabled) |
| 1400 | return -EINVAL; |
John Johansen | 39d8482 | 2017-03-30 05:25:23 -0700 | [diff] [blame] | 1401 | /* file is ro but enforce 2nd line check */ |
| 1402 | if (apparmor_initialized) |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1403 | return -EPERM; |
John Johansen | 39d8482 | 2017-03-30 05:25:23 -0700 | [diff] [blame] | 1404 | |
| 1405 | error = param_set_uint(val, kp); |
| 1406 | pr_info("AppArmor: buffer size set to %d bytes\n", aa_g_path_max); |
| 1407 | |
| 1408 | return error; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1409 | } |
| 1410 | |
Stephen Rothwell | 101d6c8 | 2010-08-02 12:00:43 +1000 | [diff] [blame] | 1411 | static int param_get_aauint(char *buffer, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1412 | { |
John Johansen | ca4bd5a | 2017-01-16 00:43:11 -0800 | [diff] [blame] | 1413 | if (!apparmor_enabled) |
| 1414 | return -EINVAL; |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1415 | if (apparmor_initialized && !policy_view_capable(NULL)) |
| 1416 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1417 | return param_get_uint(buffer, kp); |
| 1418 | } |
| 1419 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 1420 | static int param_get_audit(char *buffer, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1421 | { |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1422 | if (!apparmor_enabled) |
| 1423 | return -EINVAL; |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1424 | if (apparmor_initialized && !policy_view_capable(NULL)) |
| 1425 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1426 | return sprintf(buffer, "%s", audit_mode_names[aa_g_audit]); |
| 1427 | } |
| 1428 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 1429 | static int param_set_audit(const char *val, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1430 | { |
| 1431 | int i; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1432 | |
| 1433 | if (!apparmor_enabled) |
| 1434 | return -EINVAL; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1435 | if (!val) |
| 1436 | return -EINVAL; |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1437 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
| 1438 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1439 | |
Andy Shevchenko | 5d8779a | 2018-05-07 16:39:03 +0300 | [diff] [blame] | 1440 | i = match_string(audit_mode_names, AUDIT_MAX_INDEX, val); |
| 1441 | if (i < 0) |
| 1442 | return -EINVAL; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1443 | |
Andy Shevchenko | 5d8779a | 2018-05-07 16:39:03 +0300 | [diff] [blame] | 1444 | aa_g_audit = i; |
| 1445 | return 0; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1446 | } |
| 1447 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 1448 | static int param_get_mode(char *buffer, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1449 | { |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1450 | if (!apparmor_enabled) |
| 1451 | return -EINVAL; |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1452 | if (apparmor_initialized && !policy_view_capable(NULL)) |
| 1453 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1454 | |
John Johansen | 0d259f0 | 2013-07-10 21:13:43 -0700 | [diff] [blame] | 1455 | return sprintf(buffer, "%s", aa_profile_mode_names[aa_g_profile_mode]); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Kees Cook | e4dca7b | 2017-10-17 19:04:42 -0700 | [diff] [blame] | 1458 | static int param_set_mode(const char *val, const struct kernel_param *kp) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1459 | { |
| 1460 | int i; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1461 | |
| 1462 | if (!apparmor_enabled) |
| 1463 | return -EINVAL; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1464 | if (!val) |
| 1465 | return -EINVAL; |
John Johansen | 545de8f | 2017-04-06 06:55:23 -0700 | [diff] [blame] | 1466 | if (apparmor_initialized && !policy_admin_capable(NULL)) |
| 1467 | return -EPERM; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1468 | |
Andy Shevchenko | 5d8779a | 2018-05-07 16:39:03 +0300 | [diff] [blame] | 1469 | i = match_string(aa_profile_mode_names, APPARMOR_MODE_NAMES_MAX_INDEX, |
| 1470 | val); |
| 1471 | if (i < 0) |
| 1472 | return -EINVAL; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1473 | |
Andy Shevchenko | 5d8779a | 2018-05-07 16:39:03 +0300 | [diff] [blame] | 1474 | aa_g_profile_mode = i; |
| 1475 | return 0; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /* |
| 1479 | * AppArmor init functions |
| 1480 | */ |
| 1481 | |
| 1482 | /** |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 1483 | * set_init_ctx - set a task context and profile on the first task. |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1484 | * |
| 1485 | * TODO: allow setting an alternate profile than unconfined |
| 1486 | */ |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 1487 | static int __init set_init_ctx(void) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1488 | { |
| 1489 | struct cred *cred = (struct cred *)current->real_cred; |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 1490 | struct aa_task_ctx *ctx; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1491 | |
John Johansen | f175221 | 2017-01-27 04:09:40 -0800 | [diff] [blame] | 1492 | ctx = aa_alloc_task_ctx(GFP_KERNEL); |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 1493 | if (!ctx) |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1494 | return -ENOMEM; |
| 1495 | |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 1496 | lsm_early_cred(cred); |
Casey Schaufler | 69b5a44 | 2018-09-21 17:17:59 -0700 | [diff] [blame] | 1497 | set_cred_label(cred, aa_get_label(ns_unconfined(root_ns))); |
John Johansen | f175221 | 2017-01-27 04:09:40 -0800 | [diff] [blame] | 1498 | task_ctx(current) = ctx; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1499 | |
| 1500 | return 0; |
| 1501 | } |
| 1502 | |
John Johansen | d4669f0 | 2017-01-16 00:43:10 -0800 | [diff] [blame] | 1503 | static void destroy_buffers(void) |
| 1504 | { |
| 1505 | u32 i, j; |
| 1506 | |
| 1507 | for_each_possible_cpu(i) { |
| 1508 | for_each_cpu_buffer(j) { |
| 1509 | kfree(per_cpu(aa_buffers, i).buf[j]); |
| 1510 | per_cpu(aa_buffers, i).buf[j] = NULL; |
| 1511 | } |
| 1512 | } |
| 1513 | } |
| 1514 | |
| 1515 | static int __init alloc_buffers(void) |
| 1516 | { |
| 1517 | u32 i, j; |
| 1518 | |
| 1519 | for_each_possible_cpu(i) { |
| 1520 | for_each_cpu_buffer(j) { |
| 1521 | char *buffer; |
| 1522 | |
| 1523 | if (cpu_to_node(i) > num_online_nodes()) |
| 1524 | /* fallback to kmalloc for offline nodes */ |
| 1525 | buffer = kmalloc(aa_g_path_max, GFP_KERNEL); |
| 1526 | else |
| 1527 | buffer = kmalloc_node(aa_g_path_max, GFP_KERNEL, |
| 1528 | cpu_to_node(i)); |
| 1529 | if (!buffer) { |
| 1530 | destroy_buffers(); |
| 1531 | return -ENOMEM; |
| 1532 | } |
| 1533 | per_cpu(aa_buffers, i).buf[j] = buffer; |
| 1534 | } |
| 1535 | } |
| 1536 | |
| 1537 | return 0; |
| 1538 | } |
| 1539 | |
Tyler Hicks | e3ea1ca | 2016-03-16 19:19:10 -0500 | [diff] [blame] | 1540 | #ifdef CONFIG_SYSCTL |
| 1541 | static int apparmor_dointvec(struct ctl_table *table, int write, |
| 1542 | void __user *buffer, size_t *lenp, loff_t *ppos) |
| 1543 | { |
| 1544 | if (!policy_admin_capable(NULL)) |
| 1545 | return -EPERM; |
| 1546 | if (!apparmor_enabled) |
| 1547 | return -EINVAL; |
| 1548 | |
| 1549 | return proc_dointvec(table, write, buffer, lenp, ppos); |
| 1550 | } |
| 1551 | |
| 1552 | static struct ctl_path apparmor_sysctl_path[] = { |
| 1553 | { .procname = "kernel", }, |
| 1554 | { } |
| 1555 | }; |
| 1556 | |
| 1557 | static struct ctl_table apparmor_sysctl_table[] = { |
| 1558 | { |
| 1559 | .procname = "unprivileged_userns_apparmor_policy", |
| 1560 | .data = &unprivileged_userns_apparmor_policy, |
| 1561 | .maxlen = sizeof(int), |
| 1562 | .mode = 0600, |
| 1563 | .proc_handler = apparmor_dointvec, |
| 1564 | }, |
| 1565 | { } |
| 1566 | }; |
| 1567 | |
| 1568 | static int __init apparmor_init_sysctl(void) |
| 1569 | { |
| 1570 | return register_sysctl_paths(apparmor_sysctl_path, |
| 1571 | apparmor_sysctl_table) ? 0 : -ENOMEM; |
| 1572 | } |
| 1573 | #else |
| 1574 | static inline int apparmor_init_sysctl(void) |
| 1575 | { |
| 1576 | return 0; |
| 1577 | } |
| 1578 | #endif /* CONFIG_SYSCTL */ |
| 1579 | |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1580 | #if defined(CONFIG_NETFILTER) && defined(CONFIG_NETWORK_SECMARK) |
Matthew Garrett | ab9f211 | 2018-05-24 13:27:47 -0700 | [diff] [blame] | 1581 | static unsigned int apparmor_ip_postroute(void *priv, |
| 1582 | struct sk_buff *skb, |
| 1583 | const struct nf_hook_state *state) |
| 1584 | { |
| 1585 | struct aa_sk_ctx *ctx; |
| 1586 | struct sock *sk; |
| 1587 | |
| 1588 | if (!skb->secmark) |
| 1589 | return NF_ACCEPT; |
| 1590 | |
| 1591 | sk = skb_to_full_sk(skb); |
| 1592 | if (sk == NULL) |
| 1593 | return NF_ACCEPT; |
| 1594 | |
| 1595 | ctx = SK_CTX(sk); |
| 1596 | if (!apparmor_secmark_check(ctx->label, OP_SENDMSG, AA_MAY_SEND, |
| 1597 | skb->secmark, sk)) |
| 1598 | return NF_ACCEPT; |
| 1599 | |
| 1600 | return NF_DROP_ERR(-ECONNREFUSED); |
| 1601 | |
| 1602 | } |
| 1603 | |
| 1604 | static unsigned int apparmor_ipv4_postroute(void *priv, |
| 1605 | struct sk_buff *skb, |
| 1606 | const struct nf_hook_state *state) |
| 1607 | { |
| 1608 | return apparmor_ip_postroute(priv, skb, state); |
| 1609 | } |
| 1610 | |
| 1611 | static unsigned int apparmor_ipv6_postroute(void *priv, |
| 1612 | struct sk_buff *skb, |
| 1613 | const struct nf_hook_state *state) |
| 1614 | { |
| 1615 | return apparmor_ip_postroute(priv, skb, state); |
| 1616 | } |
| 1617 | |
| 1618 | static const struct nf_hook_ops apparmor_nf_ops[] = { |
| 1619 | { |
| 1620 | .hook = apparmor_ipv4_postroute, |
| 1621 | .pf = NFPROTO_IPV4, |
| 1622 | .hooknum = NF_INET_POST_ROUTING, |
| 1623 | .priority = NF_IP_PRI_SELINUX_FIRST, |
| 1624 | }, |
| 1625 | #if IS_ENABLED(CONFIG_IPV6) |
| 1626 | { |
| 1627 | .hook = apparmor_ipv6_postroute, |
| 1628 | .pf = NFPROTO_IPV6, |
| 1629 | .hooknum = NF_INET_POST_ROUTING, |
| 1630 | .priority = NF_IP6_PRI_SELINUX_FIRST, |
| 1631 | }, |
| 1632 | #endif |
| 1633 | }; |
| 1634 | |
| 1635 | static int __net_init apparmor_nf_register(struct net *net) |
| 1636 | { |
| 1637 | int ret; |
| 1638 | |
| 1639 | ret = nf_register_net_hooks(net, apparmor_nf_ops, |
| 1640 | ARRAY_SIZE(apparmor_nf_ops)); |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
| 1644 | static void __net_exit apparmor_nf_unregister(struct net *net) |
| 1645 | { |
| 1646 | nf_unregister_net_hooks(net, apparmor_nf_ops, |
| 1647 | ARRAY_SIZE(apparmor_nf_ops)); |
| 1648 | } |
| 1649 | |
| 1650 | static struct pernet_operations apparmor_net_ops = { |
| 1651 | .init = apparmor_nf_register, |
| 1652 | .exit = apparmor_nf_unregister, |
| 1653 | }; |
| 1654 | |
| 1655 | static int __init apparmor_nf_ip_init(void) |
| 1656 | { |
| 1657 | int err; |
| 1658 | |
| 1659 | if (!apparmor_enabled) |
| 1660 | return 0; |
| 1661 | |
| 1662 | err = register_pernet_subsys(&apparmor_net_ops); |
| 1663 | if (err) |
| 1664 | panic("Apparmor: register_pernet_subsys: error %d\n", err); |
| 1665 | |
| 1666 | return 0; |
| 1667 | } |
| 1668 | __initcall(apparmor_nf_ip_init); |
Arnd Bergmann | e1af477 | 2018-10-05 18:11:47 +0200 | [diff] [blame] | 1669 | #endif |
Matthew Garrett | ab9f211 | 2018-05-24 13:27:47 -0700 | [diff] [blame] | 1670 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1671 | static int __init apparmor_init(void) |
| 1672 | { |
| 1673 | int error; |
| 1674 | |
John Johansen | a4c3f89 | 2018-06-04 19:44:59 -0700 | [diff] [blame] | 1675 | aa_secids_init(); |
| 1676 | |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 1677 | error = aa_setup_dfa_engine(); |
| 1678 | if (error) { |
| 1679 | AA_ERROR("Unable to setup dfa engine\n"); |
| 1680 | goto alloc_out; |
| 1681 | } |
| 1682 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1683 | error = aa_alloc_root_ns(); |
| 1684 | if (error) { |
| 1685 | AA_ERROR("Unable to allocate default profile namespace\n"); |
| 1686 | goto alloc_out; |
| 1687 | } |
| 1688 | |
Tyler Hicks | e3ea1ca | 2016-03-16 19:19:10 -0500 | [diff] [blame] | 1689 | error = apparmor_init_sysctl(); |
| 1690 | if (error) { |
| 1691 | AA_ERROR("Unable to register sysctls\n"); |
| 1692 | goto alloc_out; |
| 1693 | |
| 1694 | } |
| 1695 | |
John Johansen | d4669f0 | 2017-01-16 00:43:10 -0800 | [diff] [blame] | 1696 | error = alloc_buffers(); |
| 1697 | if (error) { |
| 1698 | AA_ERROR("Unable to allocate work buffers\n"); |
| 1699 | goto buffers_out; |
| 1700 | } |
| 1701 | |
John Johansen | 55a26eb | 2017-01-16 00:43:00 -0800 | [diff] [blame] | 1702 | error = set_init_ctx(); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1703 | if (error) { |
| 1704 | AA_ERROR("Failed to set context on init task\n"); |
Casey Schaufler | b1d9e6b | 2015-05-02 15:11:42 -0700 | [diff] [blame] | 1705 | aa_free_root_ns(); |
John Johansen | d4669f0 | 2017-01-16 00:43:10 -0800 | [diff] [blame] | 1706 | goto buffers_out; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1707 | } |
Casey Schaufler | d69dece5 | 2017-01-18 17:09:05 -0800 | [diff] [blame] | 1708 | security_add_hooks(apparmor_hooks, ARRAY_SIZE(apparmor_hooks), |
| 1709 | "apparmor"); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1710 | |
| 1711 | /* Report that AppArmor successfully initialized */ |
| 1712 | apparmor_initialized = 1; |
| 1713 | if (aa_g_profile_mode == APPARMOR_COMPLAIN) |
| 1714 | aa_info_message("AppArmor initialized: complain mode enabled"); |
| 1715 | else if (aa_g_profile_mode == APPARMOR_KILL) |
| 1716 | aa_info_message("AppArmor initialized: kill mode enabled"); |
| 1717 | else |
| 1718 | aa_info_message("AppArmor initialized"); |
| 1719 | |
| 1720 | return error; |
| 1721 | |
John Johansen | d4669f0 | 2017-01-16 00:43:10 -0800 | [diff] [blame] | 1722 | buffers_out: |
| 1723 | destroy_buffers(); |
| 1724 | |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1725 | alloc_out: |
| 1726 | aa_destroy_aafs(); |
John Johansen | 11c236b | 2017-01-16 00:42:42 -0800 | [diff] [blame] | 1727 | aa_teardown_dfa_engine(); |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1728 | |
Thomas Meyer | 954317f | 2017-10-07 16:02:21 +0200 | [diff] [blame] | 1729 | apparmor_enabled = false; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1730 | return error; |
John Johansen | b5e95b4 | 2010-07-29 14:48:07 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 1733 | DEFINE_LSM(apparmor) = { |
Kees Cook | 07aed2f | 2018-10-10 17:18:24 -0700 | [diff] [blame] | 1734 | .name = "apparmor", |
Kees Cook | 14bd99c | 2018-09-19 19:57:06 -0700 | [diff] [blame] | 1735 | .flags = LSM_FLAG_LEGACY_MAJOR | LSM_FLAG_EXCLUSIVE, |
Kees Cook | c5459b8 | 2018-09-13 22:28:48 -0700 | [diff] [blame] | 1736 | .enabled = &apparmor_enabled, |
Casey Schaufler | bbd3662 | 2018-11-12 09:30:56 -0800 | [diff] [blame] | 1737 | .blobs = &apparmor_blob_sizes, |
Kees Cook | 3d6e5f6 | 2018-10-10 17:18:23 -0700 | [diff] [blame] | 1738 | .init = apparmor_init, |
| 1739 | }; |