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