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