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