Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Encryption policy functions for per-file encryption support. |
| 4 | * |
| 5 | * Copyright (C) 2015, Google, Inc. |
| 6 | * Copyright (C) 2015, Motorola Mobility. |
| 7 | * |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 8 | * Originally written by Michael Halcrow, 2015. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 9 | * Modified by Jaegeuk Kim, 2015. |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 10 | * Modified by Eric Biggers, 2019 for v2 policy support. |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/random.h> |
| 14 | #include <linux/string.h> |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 15 | #include <linux/mount.h> |
Theodore Ts'o | cc4e0df | 2016-11-26 22:05:18 -0500 | [diff] [blame] | 16 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 17 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 18 | /** |
| 19 | * fscrypt_policies_equal - check whether two encryption policies are the same |
| 20 | * |
| 21 | * Return: %true if equal, else %false |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 22 | */ |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 23 | bool fscrypt_policies_equal(const union fscrypt_policy *policy1, |
| 24 | const union fscrypt_policy *policy2) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 25 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 26 | if (policy1->version != policy2->version) |
| 27 | return false; |
| 28 | |
| 29 | return !memcmp(policy1, policy2, fscrypt_policy_size(policy1)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 32 | static bool supported_iv_ino_lblk_64_policy( |
| 33 | const struct fscrypt_policy_v2 *policy, |
| 34 | const struct inode *inode) |
| 35 | { |
| 36 | struct super_block *sb = inode->i_sb; |
| 37 | int ino_bits = 64, lblk_bits = 64; |
| 38 | |
| 39 | if (policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) { |
| 40 | fscrypt_warn(inode, |
| 41 | "The DIRECT_KEY and IV_INO_LBLK_64 flags are mutually exclusive"); |
| 42 | return false; |
| 43 | } |
| 44 | /* |
| 45 | * It's unsafe to include inode numbers in the IVs if the filesystem can |
| 46 | * potentially renumber inodes, e.g. via filesystem shrinking. |
| 47 | */ |
| 48 | if (!sb->s_cop->has_stable_inodes || |
| 49 | !sb->s_cop->has_stable_inodes(sb)) { |
| 50 | fscrypt_warn(inode, |
| 51 | "Can't use IV_INO_LBLK_64 policy on filesystem '%s' because it doesn't have stable inode numbers", |
| 52 | sb->s_id); |
| 53 | return false; |
| 54 | } |
| 55 | if (sb->s_cop->get_ino_and_lblk_bits) |
| 56 | sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits); |
| 57 | if (ino_bits > 32 || lblk_bits > 32) { |
| 58 | fscrypt_warn(inode, |
| 59 | "Can't use IV_INO_LBLK_64 policy on filesystem '%s' because it doesn't use 32-bit inode and block numbers", |
| 60 | sb->s_id); |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame^] | 66 | static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy, |
| 67 | const struct inode *inode) |
| 68 | { |
| 69 | if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, |
| 70 | policy->filenames_encryption_mode)) { |
| 71 | fscrypt_warn(inode, |
| 72 | "Unsupported encryption modes (contents %d, filenames %d)", |
| 73 | policy->contents_encryption_mode, |
| 74 | policy->filenames_encryption_mode); |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK | |
| 79 | FSCRYPT_POLICY_FLAG_DIRECT_KEY)) { |
| 80 | fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)", |
| 81 | policy->flags); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy, |
| 89 | const struct inode *inode) |
| 90 | { |
| 91 | if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, |
| 92 | policy->filenames_encryption_mode)) { |
| 93 | fscrypt_warn(inode, |
| 94 | "Unsupported encryption modes (contents %d, filenames %d)", |
| 95 | policy->contents_encryption_mode, |
| 96 | policy->filenames_encryption_mode); |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | if (policy->flags & ~FSCRYPT_POLICY_FLAGS_VALID) { |
| 101 | fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)", |
| 102 | policy->flags); |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) && |
| 107 | !supported_iv_ino_lblk_64_policy(policy, inode)) |
| 108 | return false; |
| 109 | |
| 110 | if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) { |
| 111 | fscrypt_warn(inode, "Reserved bits set in encryption policy"); |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 118 | /** |
| 119 | * fscrypt_supported_policy - check whether an encryption policy is supported |
| 120 | * |
| 121 | * Given an encryption policy, check whether all its encryption modes and other |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame^] | 122 | * settings are supported by this kernel on the given inode. (But we don't |
| 123 | * currently don't check for crypto API support here, so attempting to use an |
| 124 | * algorithm not configured into the crypto API will still fail later.) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 125 | * |
| 126 | * Return: %true if supported, else %false |
| 127 | */ |
| 128 | bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, |
| 129 | const struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 130 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 131 | switch (policy_u->version) { |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame^] | 132 | case FSCRYPT_POLICY_V1: |
| 133 | return fscrypt_supported_v1_policy(&policy_u->v1, inode); |
| 134 | case FSCRYPT_POLICY_V2: |
| 135 | return fscrypt_supported_v2_policy(&policy_u->v2, inode); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * fscrypt_new_context_from_policy - create a new fscrypt_context from a policy |
| 142 | * |
| 143 | * Create an fscrypt_context for an inode that is being assigned the given |
| 144 | * encryption policy. A new nonce is randomly generated. |
| 145 | * |
| 146 | * Return: the size of the new context in bytes. |
| 147 | */ |
| 148 | static int fscrypt_new_context_from_policy(union fscrypt_context *ctx_u, |
| 149 | const union fscrypt_policy *policy_u) |
| 150 | { |
| 151 | memset(ctx_u, 0, sizeof(*ctx_u)); |
| 152 | |
| 153 | switch (policy_u->version) { |
| 154 | case FSCRYPT_POLICY_V1: { |
| 155 | const struct fscrypt_policy_v1 *policy = &policy_u->v1; |
| 156 | struct fscrypt_context_v1 *ctx = &ctx_u->v1; |
| 157 | |
| 158 | ctx->version = FSCRYPT_CONTEXT_V1; |
| 159 | ctx->contents_encryption_mode = |
| 160 | policy->contents_encryption_mode; |
| 161 | ctx->filenames_encryption_mode = |
| 162 | policy->filenames_encryption_mode; |
| 163 | ctx->flags = policy->flags; |
| 164 | memcpy(ctx->master_key_descriptor, |
| 165 | policy->master_key_descriptor, |
| 166 | sizeof(ctx->master_key_descriptor)); |
| 167 | get_random_bytes(ctx->nonce, sizeof(ctx->nonce)); |
| 168 | return sizeof(*ctx); |
| 169 | } |
| 170 | case FSCRYPT_POLICY_V2: { |
| 171 | const struct fscrypt_policy_v2 *policy = &policy_u->v2; |
| 172 | struct fscrypt_context_v2 *ctx = &ctx_u->v2; |
| 173 | |
| 174 | ctx->version = FSCRYPT_CONTEXT_V2; |
| 175 | ctx->contents_encryption_mode = |
| 176 | policy->contents_encryption_mode; |
| 177 | ctx->filenames_encryption_mode = |
| 178 | policy->filenames_encryption_mode; |
| 179 | ctx->flags = policy->flags; |
| 180 | memcpy(ctx->master_key_identifier, |
| 181 | policy->master_key_identifier, |
| 182 | sizeof(ctx->master_key_identifier)); |
| 183 | get_random_bytes(ctx->nonce, sizeof(ctx->nonce)); |
| 184 | return sizeof(*ctx); |
| 185 | } |
| 186 | } |
| 187 | BUG(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * fscrypt_policy_from_context - convert an fscrypt_context to an fscrypt_policy |
| 192 | * |
| 193 | * Given an fscrypt_context, build the corresponding fscrypt_policy. |
| 194 | * |
| 195 | * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized |
| 196 | * version number or size. |
| 197 | * |
| 198 | * This does *not* validate the settings within the policy itself, e.g. the |
| 199 | * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that. |
| 200 | */ |
| 201 | int fscrypt_policy_from_context(union fscrypt_policy *policy_u, |
| 202 | const union fscrypt_context *ctx_u, |
| 203 | int ctx_size) |
| 204 | { |
| 205 | memset(policy_u, 0, sizeof(*policy_u)); |
| 206 | |
| 207 | if (ctx_size <= 0 || ctx_size != fscrypt_context_size(ctx_u)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 208 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 209 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 210 | switch (ctx_u->version) { |
| 211 | case FSCRYPT_CONTEXT_V1: { |
| 212 | const struct fscrypt_context_v1 *ctx = &ctx_u->v1; |
| 213 | struct fscrypt_policy_v1 *policy = &policy_u->v1; |
| 214 | |
| 215 | policy->version = FSCRYPT_POLICY_V1; |
| 216 | policy->contents_encryption_mode = |
| 217 | ctx->contents_encryption_mode; |
| 218 | policy->filenames_encryption_mode = |
| 219 | ctx->filenames_encryption_mode; |
| 220 | policy->flags = ctx->flags; |
| 221 | memcpy(policy->master_key_descriptor, |
| 222 | ctx->master_key_descriptor, |
| 223 | sizeof(policy->master_key_descriptor)); |
| 224 | return 0; |
| 225 | } |
| 226 | case FSCRYPT_CONTEXT_V2: { |
| 227 | const struct fscrypt_context_v2 *ctx = &ctx_u->v2; |
| 228 | struct fscrypt_policy_v2 *policy = &policy_u->v2; |
| 229 | |
| 230 | policy->version = FSCRYPT_POLICY_V2; |
| 231 | policy->contents_encryption_mode = |
| 232 | ctx->contents_encryption_mode; |
| 233 | policy->filenames_encryption_mode = |
| 234 | ctx->filenames_encryption_mode; |
| 235 | policy->flags = ctx->flags; |
| 236 | memcpy(policy->__reserved, ctx->__reserved, |
| 237 | sizeof(policy->__reserved)); |
| 238 | memcpy(policy->master_key_identifier, |
| 239 | ctx->master_key_identifier, |
| 240 | sizeof(policy->master_key_identifier)); |
| 241 | return 0; |
| 242 | } |
| 243 | } |
| 244 | /* unreachable */ |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | /* Retrieve an inode's encryption policy */ |
| 249 | static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy) |
| 250 | { |
| 251 | const struct fscrypt_info *ci; |
| 252 | union fscrypt_context ctx; |
| 253 | int ret; |
| 254 | |
| 255 | ci = READ_ONCE(inode->i_crypt_info); |
| 256 | if (ci) { |
| 257 | /* key available, use the cached policy */ |
| 258 | *policy = ci->ci_policy; |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | if (!IS_ENCRYPTED(inode)) |
| 263 | return -ENODATA; |
| 264 | |
| 265 | ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 266 | if (ret < 0) |
| 267 | return (ret == -ERANGE) ? -EINVAL : ret; |
| 268 | |
| 269 | return fscrypt_policy_from_context(policy, &ctx, ret); |
| 270 | } |
| 271 | |
| 272 | static int set_encryption_policy(struct inode *inode, |
| 273 | const union fscrypt_policy *policy) |
| 274 | { |
| 275 | union fscrypt_context ctx; |
| 276 | int ctxsize; |
Eric Biggers | 5ab7189 | 2019-08-04 19:35:48 -0700 | [diff] [blame] | 277 | int err; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 278 | |
| 279 | if (!fscrypt_supported_policy(policy, inode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 280 | return -EINVAL; |
| 281 | |
Eric Biggers | 5ab7189 | 2019-08-04 19:35:48 -0700 | [diff] [blame] | 282 | switch (policy->version) { |
| 283 | case FSCRYPT_POLICY_V1: |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 284 | /* |
| 285 | * The original encryption policy version provided no way of |
| 286 | * verifying that the correct master key was supplied, which was |
| 287 | * insecure in scenarios where multiple users have access to the |
| 288 | * same encrypted files (even just read-only access). The new |
| 289 | * encryption policy version fixes this and also implies use of |
| 290 | * an improved key derivation function and allows non-root users |
| 291 | * to securely remove keys. So as long as compatibility with |
| 292 | * old kernels isn't required, it is recommended to use the new |
| 293 | * policy version for all new encrypted directories. |
| 294 | */ |
| 295 | pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n", |
| 296 | current->comm, current->pid); |
Eric Biggers | 5ab7189 | 2019-08-04 19:35:48 -0700 | [diff] [blame] | 297 | break; |
| 298 | case FSCRYPT_POLICY_V2: |
| 299 | err = fscrypt_verify_key_added(inode->i_sb, |
| 300 | policy->v2.master_key_identifier); |
| 301 | if (err) |
| 302 | return err; |
| 303 | break; |
| 304 | default: |
| 305 | WARN_ON(1); |
| 306 | return -EINVAL; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 307 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 308 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 309 | ctxsize = fscrypt_new_context_from_policy(&ctx, policy); |
| 310 | |
| 311 | return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 314 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 315 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 316 | union fscrypt_policy policy; |
| 317 | union fscrypt_policy existing_policy; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 318 | struct inode *inode = file_inode(filp); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 319 | u8 version; |
| 320 | int size; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 321 | int ret; |
| 322 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 323 | if (get_user(policy.version, (const u8 __user *)arg)) |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 324 | return -EFAULT; |
| 325 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 326 | size = fscrypt_policy_size(&policy); |
| 327 | if (size <= 0) |
| 328 | return -EINVAL; |
| 329 | |
| 330 | /* |
| 331 | * We should just copy the remaining 'size - 1' bytes here, but a |
| 332 | * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to |
| 333 | * think that size can be 0 here (despite the check above!) *and* that |
| 334 | * it's a compile-time constant. Thus it would think copy_from_user() |
| 335 | * is passed compile-time constant ULONG_MAX, causing the compile-time |
| 336 | * buffer overflow check to fail, breaking the build. This only occurred |
| 337 | * when building an i386 kernel with -Os and branch profiling enabled. |
| 338 | * |
| 339 | * Work around it by just copying the first byte again... |
| 340 | */ |
| 341 | version = policy.version; |
| 342 | if (copy_from_user(&policy, arg, size)) |
| 343 | return -EFAULT; |
| 344 | policy.version = version; |
| 345 | |
Eric Biggers | 163ae1c | 2016-09-08 10:57:08 -0700 | [diff] [blame] | 346 | if (!inode_owner_or_capable(inode)) |
| 347 | return -EACCES; |
| 348 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 349 | ret = mnt_want_write_file(filp); |
| 350 | if (ret) |
| 351 | return ret; |
| 352 | |
Eric Biggers | 8906a82 | 2016-10-15 09:48:50 -0400 | [diff] [blame] | 353 | inode_lock(inode); |
| 354 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 355 | ret = fscrypt_get_policy(inode, &existing_policy); |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 356 | if (ret == -ENODATA) { |
Eric Biggers | 002ced4 | 2016-09-08 11:36:39 -0700 | [diff] [blame] | 357 | if (!S_ISDIR(inode->i_mode)) |
Eric Biggers | dffd0cf | 2016-12-05 11:12:45 -0800 | [diff] [blame] | 358 | ret = -ENOTDIR; |
Hongjie Fang | 5858bda | 2019-05-22 10:02:53 +0800 | [diff] [blame] | 359 | else if (IS_DEADDIR(inode)) |
| 360 | ret = -ENOENT; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 361 | else if (!inode->i_sb->s_cop->empty_dir(inode)) |
| 362 | ret = -ENOTEMPTY; |
| 363 | else |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 364 | ret = set_encryption_policy(inode, &policy); |
| 365 | } else if (ret == -EINVAL || |
| 366 | (ret == 0 && !fscrypt_policies_equal(&policy, |
| 367 | &existing_policy))) { |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 368 | /* The file already uses a different encryption policy. */ |
Eric Biggers | 8488cd9 | 2016-12-05 11:12:46 -0800 | [diff] [blame] | 369 | ret = -EEXIST; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Eric Biggers | 8906a82 | 2016-10-15 09:48:50 -0400 | [diff] [blame] | 372 | inode_unlock(inode); |
| 373 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 374 | mnt_drop_write_file(filp); |
| 375 | return ret; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 376 | } |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 377 | EXPORT_SYMBOL(fscrypt_ioctl_set_policy); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 378 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 379 | /* Original ioctl version; can only get the original policy version */ |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 380 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 381 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 382 | union fscrypt_policy policy; |
| 383 | int err; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 384 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 385 | err = fscrypt_get_policy(file_inode(filp), &policy); |
| 386 | if (err) |
| 387 | return err; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 388 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 389 | if (policy.version != FSCRYPT_POLICY_V1) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 390 | return -EINVAL; |
| 391 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 392 | if (copy_to_user(arg, &policy, sizeof(policy.v1))) |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 393 | return -EFAULT; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 394 | return 0; |
| 395 | } |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 396 | EXPORT_SYMBOL(fscrypt_ioctl_get_policy); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 397 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 398 | /* Extended ioctl version; can get policies of any version */ |
| 399 | int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg) |
| 400 | { |
| 401 | struct fscrypt_get_policy_ex_arg arg; |
| 402 | union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy; |
| 403 | size_t policy_size; |
| 404 | int err; |
| 405 | |
| 406 | /* arg is policy_size, then policy */ |
| 407 | BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0); |
| 408 | BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) != |
| 409 | offsetof(typeof(arg), policy)); |
| 410 | BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy)); |
| 411 | |
| 412 | err = fscrypt_get_policy(file_inode(filp), policy); |
| 413 | if (err) |
| 414 | return err; |
| 415 | policy_size = fscrypt_policy_size(policy); |
| 416 | |
| 417 | if (copy_from_user(&arg, uarg, sizeof(arg.policy_size))) |
| 418 | return -EFAULT; |
| 419 | |
| 420 | if (policy_size > arg.policy_size) |
| 421 | return -EOVERFLOW; |
| 422 | arg.policy_size = policy_size; |
| 423 | |
| 424 | if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size)) |
| 425 | return -EFAULT; |
| 426 | return 0; |
| 427 | } |
| 428 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex); |
| 429 | |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 430 | /** |
| 431 | * fscrypt_has_permitted_context() - is a file's encryption policy permitted |
| 432 | * within its directory? |
| 433 | * |
| 434 | * @parent: inode for parent directory |
| 435 | * @child: inode for file being looked up, opened, or linked into @parent |
| 436 | * |
| 437 | * Filesystems must call this before permitting access to an inode in a |
| 438 | * situation where the parent directory is encrypted (either before allowing |
| 439 | * ->lookup() to succeed, or for a regular file before allowing it to be opened) |
| 440 | * and before any operation that involves linking an inode into an encrypted |
| 441 | * directory, including link, rename, and cross rename. It enforces the |
| 442 | * constraint that within a given encrypted directory tree, all files use the |
| 443 | * same encryption policy. The pre-access check is needed to detect potentially |
| 444 | * malicious offline violations of this constraint, while the link and rename |
| 445 | * checks are needed to prevent online violations of this constraint. |
| 446 | * |
Eric Biggers | f5e55e7 | 2019-01-22 16:20:21 -0800 | [diff] [blame] | 447 | * Return: 1 if permitted, 0 if forbidden. |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 448 | */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 449 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) |
| 450 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 451 | union fscrypt_policy parent_policy, child_policy; |
| 452 | int err; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 453 | |
Eric Biggers | 42d97eb | 2016-12-19 14:20:13 -0800 | [diff] [blame] | 454 | /* No restrictions on file types which are never encrypted */ |
| 455 | if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && |
| 456 | !S_ISLNK(child->i_mode)) |
| 457 | return 1; |
| 458 | |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 459 | /* No restrictions if the parent directory is unencrypted */ |
Eric Biggers | e0428a2 | 2017-10-09 12:15:36 -0700 | [diff] [blame] | 460 | if (!IS_ENCRYPTED(parent)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 461 | return 1; |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 462 | |
| 463 | /* Encrypted directories must not contain unencrypted files */ |
Eric Biggers | e0428a2 | 2017-10-09 12:15:36 -0700 | [diff] [blame] | 464 | if (!IS_ENCRYPTED(child)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 465 | return 0; |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 466 | |
| 467 | /* |
| 468 | * Both parent and child are encrypted, so verify they use the same |
| 469 | * encryption policy. Compare the fscrypt_info structs if the keys are |
| 470 | * available, otherwise retrieve and compare the fscrypt_contexts. |
| 471 | * |
| 472 | * Note that the fscrypt_context retrieval will be required frequently |
| 473 | * when accessing an encrypted directory tree without the key. |
| 474 | * Performance-wise this is not a big deal because we already don't |
| 475 | * really optimize for file access without the key (to the extent that |
| 476 | * such access is even possible), given that any attempted access |
| 477 | * already causes a fscrypt_context retrieval and keyring search. |
| 478 | * |
| 479 | * In any case, if an unexpected error occurs, fall back to "forbidden". |
| 480 | */ |
| 481 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 482 | err = fscrypt_get_encryption_info(parent); |
| 483 | if (err) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 484 | return 0; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 485 | err = fscrypt_get_encryption_info(child); |
| 486 | if (err) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 487 | return 0; |
| 488 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 489 | err = fscrypt_get_policy(parent, &parent_policy); |
| 490 | if (err) |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 491 | return 0; |
| 492 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 493 | err = fscrypt_get_policy(child, &child_policy); |
| 494 | if (err) |
| 495 | return 0; |
| 496 | |
| 497 | return fscrypt_policies_equal(&parent_policy, &child_policy); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 498 | } |
| 499 | EXPORT_SYMBOL(fscrypt_has_permitted_context); |
| 500 | |
| 501 | /** |
| 502 | * fscrypt_inherit_context() - Sets a child context from its parent |
| 503 | * @parent: Parent inode from which the context is inherited. |
| 504 | * @child: Child inode that inherits the context from @parent. |
| 505 | * @fs_data: private data given by FS. |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 506 | * @preload: preload child i_crypt_info if true |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 507 | * |
Theodore Ts'o | 5bbdcbb | 2017-01-02 15:12:17 -0500 | [diff] [blame] | 508 | * Return: 0 on success, -errno on failure |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 509 | */ |
| 510 | int fscrypt_inherit_context(struct inode *parent, struct inode *child, |
| 511 | void *fs_data, bool preload) |
| 512 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 513 | union fscrypt_context ctx; |
| 514 | int ctxsize; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 515 | struct fscrypt_info *ci; |
| 516 | int res; |
| 517 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 518 | res = fscrypt_get_encryption_info(parent); |
| 519 | if (res < 0) |
| 520 | return res; |
| 521 | |
Eric Biggers | e37a784 | 2019-04-11 14:32:15 -0700 | [diff] [blame] | 522 | ci = READ_ONCE(parent->i_crypt_info); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 523 | if (ci == NULL) |
| 524 | return -ENOKEY; |
| 525 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 526 | ctxsize = fscrypt_new_context_from_policy(&ctx, &ci->ci_policy); |
| 527 | |
Tahsin Erdogan | af65207 | 2017-07-06 00:01:59 -0400 | [diff] [blame] | 528 | BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 529 | res = parent->i_sb->s_cop->set_context(child, &ctx, ctxsize, fs_data); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 530 | if (res) |
| 531 | return res; |
| 532 | return preload ? fscrypt_get_encryption_info(child): 0; |
| 533 | } |
| 534 | EXPORT_SYMBOL(fscrypt_inherit_context); |