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> |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 14 | #include <linux/seq_file.h> |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 15 | #include <linux/string.h> |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 16 | #include <linux/mount.h> |
Theodore Ts'o | cc4e0df | 2016-11-26 22:05:18 -0500 | [diff] [blame] | 17 | #include "fscrypt_private.h" |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 18 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 19 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 20 | * fscrypt_policies_equal() - check whether two encryption policies are the same |
| 21 | * @policy1: the first policy |
| 22 | * @policy2: the second policy |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 23 | * |
| 24 | * Return: %true if equal, else %false |
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 | bool fscrypt_policies_equal(const union fscrypt_policy *policy1, |
| 27 | const union fscrypt_policy *policy2) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 28 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 29 | if (policy1->version != policy2->version) |
| 30 | return false; |
| 31 | |
| 32 | return !memcmp(policy1, policy2, fscrypt_policy_size(policy1)); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 35 | static const union fscrypt_policy * |
| 36 | fscrypt_get_dummy_policy(struct super_block *sb) |
| 37 | { |
| 38 | if (!sb->s_cop->get_dummy_policy) |
| 39 | return NULL; |
| 40 | return sb->s_cop->get_dummy_policy(sb); |
| 41 | } |
| 42 | |
Eric Biggers | ef5b18b | 2019-12-09 13:18:28 -0800 | [diff] [blame] | 43 | static bool fscrypt_valid_enc_modes(u32 contents_mode, u32 filenames_mode) |
| 44 | { |
| 45 | if (contents_mode == FSCRYPT_MODE_AES_256_XTS && |
| 46 | filenames_mode == FSCRYPT_MODE_AES_256_CTS) |
| 47 | return true; |
| 48 | |
| 49 | if (contents_mode == FSCRYPT_MODE_AES_128_CBC && |
| 50 | filenames_mode == FSCRYPT_MODE_AES_128_CTS) |
| 51 | return true; |
| 52 | |
| 53 | if (contents_mode == FSCRYPT_MODE_ADIANTUM && |
| 54 | filenames_mode == FSCRYPT_MODE_ADIANTUM) |
| 55 | return true; |
| 56 | |
| 57 | return false; |
| 58 | } |
| 59 | |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 60 | static bool supported_direct_key_modes(const struct inode *inode, |
| 61 | u32 contents_mode, u32 filenames_mode) |
| 62 | { |
| 63 | const struct fscrypt_mode *mode; |
| 64 | |
| 65 | if (contents_mode != filenames_mode) { |
| 66 | fscrypt_warn(inode, |
| 67 | "Direct key flag not allowed with different contents and filenames modes"); |
| 68 | return false; |
| 69 | } |
| 70 | mode = &fscrypt_modes[contents_mode]; |
| 71 | |
| 72 | if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) { |
| 73 | fscrypt_warn(inode, "Direct key flag not allowed with %s", |
| 74 | mode->friendly_name); |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 80 | static bool supported_iv_ino_lblk_policy(const struct fscrypt_policy_v2 *policy, |
| 81 | const struct inode *inode, |
| 82 | const char *type, |
| 83 | int max_ino_bits, int max_lblk_bits) |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 84 | { |
| 85 | struct super_block *sb = inode->i_sb; |
| 86 | int ino_bits = 64, lblk_bits = 64; |
| 87 | |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 88 | /* |
Eric Biggers | f000223 | 2020-07-21 11:10:12 -0700 | [diff] [blame] | 89 | * IV_INO_LBLK_* exist only because of hardware limitations, and |
| 90 | * currently the only known use case for them involves AES-256-XTS. |
| 91 | * That's also all we test currently. For these reasons, for now only |
| 92 | * allow AES-256-XTS here. This can be relaxed later if a use case for |
| 93 | * IV_INO_LBLK_* with other encryption modes arises. |
| 94 | */ |
| 95 | if (policy->contents_encryption_mode != FSCRYPT_MODE_AES_256_XTS) { |
| 96 | fscrypt_warn(inode, |
| 97 | "Can't use %s policy with contents mode other than AES-256-XTS", |
| 98 | type); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | /* |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 103 | * It's unsafe to include inode numbers in the IVs if the filesystem can |
| 104 | * potentially renumber inodes, e.g. via filesystem shrinking. |
| 105 | */ |
| 106 | if (!sb->s_cop->has_stable_inodes || |
| 107 | !sb->s_cop->has_stable_inodes(sb)) { |
| 108 | fscrypt_warn(inode, |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 109 | "Can't use %s policy on filesystem '%s' because it doesn't have stable inode numbers", |
| 110 | type, sb->s_id); |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | if (sb->s_cop->get_ino_and_lblk_bits) |
| 114 | sb->s_cop->get_ino_and_lblk_bits(sb, &ino_bits, &lblk_bits); |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 115 | if (ino_bits > max_ino_bits) { |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 116 | fscrypt_warn(inode, |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 117 | "Can't use %s policy on filesystem '%s' because its inode numbers are too long", |
| 118 | type, sb->s_id); |
| 119 | return false; |
| 120 | } |
| 121 | if (lblk_bits > max_lblk_bits) { |
| 122 | fscrypt_warn(inode, |
| 123 | "Can't use %s policy on filesystem '%s' because its block numbers are too long", |
| 124 | type, sb->s_id); |
Eric Biggers | b103fb7 | 2019-10-24 14:54:36 -0700 | [diff] [blame] | 125 | return false; |
| 126 | } |
| 127 | return true; |
| 128 | } |
| 129 | |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame] | 130 | static bool fscrypt_supported_v1_policy(const struct fscrypt_policy_v1 *policy, |
| 131 | const struct inode *inode) |
| 132 | { |
| 133 | if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, |
| 134 | policy->filenames_encryption_mode)) { |
| 135 | fscrypt_warn(inode, |
| 136 | "Unsupported encryption modes (contents %d, filenames %d)", |
| 137 | policy->contents_encryption_mode, |
| 138 | policy->filenames_encryption_mode); |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK | |
| 143 | FSCRYPT_POLICY_FLAG_DIRECT_KEY)) { |
| 144 | fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)", |
| 145 | policy->flags); |
| 146 | return false; |
| 147 | } |
| 148 | |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 149 | if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) && |
| 150 | !supported_direct_key_modes(inode, policy->contents_encryption_mode, |
| 151 | policy->filenames_encryption_mode)) |
| 152 | return false; |
| 153 | |
Daniel Rosenberg | 6e1918c | 2020-01-20 14:31:56 -0800 | [diff] [blame] | 154 | if (IS_CASEFOLDED(inode)) { |
| 155 | /* With v1, there's no way to derive dirhash keys. */ |
| 156 | fscrypt_warn(inode, |
| 157 | "v1 policies can't be used on casefolded directories"); |
| 158 | return false; |
| 159 | } |
| 160 | |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame] | 161 | return true; |
| 162 | } |
| 163 | |
| 164 | static bool fscrypt_supported_v2_policy(const struct fscrypt_policy_v2 *policy, |
| 165 | const struct inode *inode) |
| 166 | { |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 167 | int count = 0; |
| 168 | |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame] | 169 | if (!fscrypt_valid_enc_modes(policy->contents_encryption_mode, |
| 170 | policy->filenames_encryption_mode)) { |
| 171 | fscrypt_warn(inode, |
| 172 | "Unsupported encryption modes (contents %d, filenames %d)", |
| 173 | policy->contents_encryption_mode, |
| 174 | policy->filenames_encryption_mode); |
| 175 | return false; |
| 176 | } |
| 177 | |
Eric Biggers | 3ceb654 | 2020-10-23 17:51:31 -0700 | [diff] [blame] | 178 | if (policy->flags & ~(FSCRYPT_POLICY_FLAGS_PAD_MASK | |
| 179 | FSCRYPT_POLICY_FLAG_DIRECT_KEY | |
| 180 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64 | |
| 181 | FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) { |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame] | 182 | fscrypt_warn(inode, "Unsupported encryption flags (0x%02x)", |
| 183 | policy->flags); |
| 184 | return false; |
| 185 | } |
| 186 | |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 187 | count += !!(policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY); |
| 188 | count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64); |
| 189 | count += !!(policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32); |
| 190 | if (count > 1) { |
| 191 | fscrypt_warn(inode, "Mutually exclusive encryption flags (0x%02x)", |
| 192 | policy->flags); |
| 193 | return false; |
| 194 | } |
| 195 | |
Eric Biggers | 85af90e | 2019-12-09 13:18:27 -0800 | [diff] [blame] | 196 | if ((policy->flags & FSCRYPT_POLICY_FLAG_DIRECT_KEY) && |
| 197 | !supported_direct_key_modes(inode, policy->contents_encryption_mode, |
| 198 | policy->filenames_encryption_mode)) |
| 199 | return false; |
| 200 | |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame] | 201 | if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_64) && |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 202 | !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_64", |
| 203 | 32, 32)) |
| 204 | return false; |
| 205 | |
Eric Biggers | 5e895bd | 2020-08-24 13:38:41 -0700 | [diff] [blame] | 206 | /* |
| 207 | * IV_INO_LBLK_32 hashes the inode number, so in principle it can |
| 208 | * support any ino_bits. However, currently the inode number is gotten |
| 209 | * from inode::i_ino which is 'unsigned long'. So for now the |
| 210 | * implementation limit is 32 bits. |
| 211 | */ |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 212 | if ((policy->flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) && |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 213 | !supported_iv_ino_lblk_policy(policy, inode, "IV_INO_LBLK_32", |
Eric Biggers | 5e895bd | 2020-08-24 13:38:41 -0700 | [diff] [blame] | 214 | 32, 32)) |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame] | 215 | return false; |
| 216 | |
| 217 | if (memchr_inv(policy->__reserved, 0, sizeof(policy->__reserved))) { |
| 218 | fscrypt_warn(inode, "Reserved bits set in encryption policy"); |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 225 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 226 | * fscrypt_supported_policy() - check whether an encryption policy is supported |
| 227 | * @policy_u: the encryption policy |
| 228 | * @inode: the inode on which the policy will be used |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 229 | * |
| 230 | * Given an encryption policy, check whether all its encryption modes and other |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame] | 231 | * settings are supported by this kernel on the given inode. (But we don't |
| 232 | * currently don't check for crypto API support here, so attempting to use an |
| 233 | * algorithm not configured into the crypto API will still fail later.) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 234 | * |
| 235 | * Return: %true if supported, else %false |
| 236 | */ |
| 237 | bool fscrypt_supported_policy(const union fscrypt_policy *policy_u, |
| 238 | const struct inode *inode) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 239 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 240 | switch (policy_u->version) { |
Eric Biggers | 393a24a | 2019-12-09 13:18:26 -0800 | [diff] [blame] | 241 | case FSCRYPT_POLICY_V1: |
| 242 | return fscrypt_supported_v1_policy(&policy_u->v1, inode); |
| 243 | case FSCRYPT_POLICY_V2: |
| 244 | return fscrypt_supported_v2_policy(&policy_u->v2, inode); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 245 | } |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | /** |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 250 | * fscrypt_new_context() - create a new fscrypt_context |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 251 | * @ctx_u: output context |
| 252 | * @policy_u: input policy |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 253 | * @nonce: nonce to use |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 254 | * |
| 255 | * Create an fscrypt_context for an inode that is being assigned the given |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 256 | * encryption policy. @nonce must be a new random nonce. |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 257 | * |
| 258 | * Return: the size of the new context in bytes. |
| 259 | */ |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 260 | static int fscrypt_new_context(union fscrypt_context *ctx_u, |
| 261 | const union fscrypt_policy *policy_u, |
| 262 | const u8 nonce[FSCRYPT_FILE_NONCE_SIZE]) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 263 | { |
| 264 | memset(ctx_u, 0, sizeof(*ctx_u)); |
| 265 | |
| 266 | switch (policy_u->version) { |
| 267 | case FSCRYPT_POLICY_V1: { |
| 268 | const struct fscrypt_policy_v1 *policy = &policy_u->v1; |
| 269 | struct fscrypt_context_v1 *ctx = &ctx_u->v1; |
| 270 | |
| 271 | ctx->version = FSCRYPT_CONTEXT_V1; |
| 272 | ctx->contents_encryption_mode = |
| 273 | policy->contents_encryption_mode; |
| 274 | ctx->filenames_encryption_mode = |
| 275 | policy->filenames_encryption_mode; |
| 276 | ctx->flags = policy->flags; |
| 277 | memcpy(ctx->master_key_descriptor, |
| 278 | policy->master_key_descriptor, |
| 279 | sizeof(ctx->master_key_descriptor)); |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 280 | memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 281 | return sizeof(*ctx); |
| 282 | } |
| 283 | case FSCRYPT_POLICY_V2: { |
| 284 | const struct fscrypt_policy_v2 *policy = &policy_u->v2; |
| 285 | struct fscrypt_context_v2 *ctx = &ctx_u->v2; |
| 286 | |
| 287 | ctx->version = FSCRYPT_CONTEXT_V2; |
| 288 | ctx->contents_encryption_mode = |
| 289 | policy->contents_encryption_mode; |
| 290 | ctx->filenames_encryption_mode = |
| 291 | policy->filenames_encryption_mode; |
| 292 | ctx->flags = policy->flags; |
| 293 | memcpy(ctx->master_key_identifier, |
| 294 | policy->master_key_identifier, |
| 295 | sizeof(ctx->master_key_identifier)); |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 296 | memcpy(ctx->nonce, nonce, FSCRYPT_FILE_NONCE_SIZE); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 297 | return sizeof(*ctx); |
| 298 | } |
| 299 | } |
| 300 | BUG(); |
| 301 | } |
| 302 | |
| 303 | /** |
Eric Biggers | d2fe975 | 2020-05-11 12:13:56 -0700 | [diff] [blame] | 304 | * fscrypt_policy_from_context() - convert an fscrypt_context to |
| 305 | * an fscrypt_policy |
| 306 | * @policy_u: output policy |
| 307 | * @ctx_u: input context |
| 308 | * @ctx_size: size of input context in bytes |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 309 | * |
| 310 | * Given an fscrypt_context, build the corresponding fscrypt_policy. |
| 311 | * |
| 312 | * Return: 0 on success, or -EINVAL if the fscrypt_context has an unrecognized |
| 313 | * version number or size. |
| 314 | * |
| 315 | * This does *not* validate the settings within the policy itself, e.g. the |
| 316 | * modes, flags, and reserved bits. Use fscrypt_supported_policy() for that. |
| 317 | */ |
| 318 | int fscrypt_policy_from_context(union fscrypt_policy *policy_u, |
| 319 | const union fscrypt_context *ctx_u, |
| 320 | int ctx_size) |
| 321 | { |
| 322 | memset(policy_u, 0, sizeof(*policy_u)); |
| 323 | |
Eric Biggers | e98ad46 | 2020-03-14 13:50:49 -0700 | [diff] [blame] | 324 | if (!fscrypt_context_is_valid(ctx_u, ctx_size)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 325 | return -EINVAL; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 326 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 327 | switch (ctx_u->version) { |
| 328 | case FSCRYPT_CONTEXT_V1: { |
| 329 | const struct fscrypt_context_v1 *ctx = &ctx_u->v1; |
| 330 | struct fscrypt_policy_v1 *policy = &policy_u->v1; |
| 331 | |
| 332 | policy->version = FSCRYPT_POLICY_V1; |
| 333 | policy->contents_encryption_mode = |
| 334 | ctx->contents_encryption_mode; |
| 335 | policy->filenames_encryption_mode = |
| 336 | ctx->filenames_encryption_mode; |
| 337 | policy->flags = ctx->flags; |
| 338 | memcpy(policy->master_key_descriptor, |
| 339 | ctx->master_key_descriptor, |
| 340 | sizeof(policy->master_key_descriptor)); |
| 341 | return 0; |
| 342 | } |
| 343 | case FSCRYPT_CONTEXT_V2: { |
| 344 | const struct fscrypt_context_v2 *ctx = &ctx_u->v2; |
| 345 | struct fscrypt_policy_v2 *policy = &policy_u->v2; |
| 346 | |
| 347 | policy->version = FSCRYPT_POLICY_V2; |
| 348 | policy->contents_encryption_mode = |
| 349 | ctx->contents_encryption_mode; |
| 350 | policy->filenames_encryption_mode = |
| 351 | ctx->filenames_encryption_mode; |
| 352 | policy->flags = ctx->flags; |
| 353 | memcpy(policy->__reserved, ctx->__reserved, |
| 354 | sizeof(policy->__reserved)); |
| 355 | memcpy(policy->master_key_identifier, |
| 356 | ctx->master_key_identifier, |
| 357 | sizeof(policy->master_key_identifier)); |
| 358 | return 0; |
| 359 | } |
| 360 | } |
| 361 | /* unreachable */ |
| 362 | return -EINVAL; |
| 363 | } |
| 364 | |
| 365 | /* Retrieve an inode's encryption policy */ |
| 366 | static int fscrypt_get_policy(struct inode *inode, union fscrypt_policy *policy) |
| 367 | { |
| 368 | const struct fscrypt_info *ci; |
| 369 | union fscrypt_context ctx; |
| 370 | int ret; |
| 371 | |
Eric Biggers | ab673b9 | 2020-07-21 15:59:19 -0700 | [diff] [blame] | 372 | ci = fscrypt_get_info(inode); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 373 | if (ci) { |
| 374 | /* key available, use the cached policy */ |
| 375 | *policy = ci->ci_policy; |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | if (!IS_ENCRYPTED(inode)) |
| 380 | return -ENODATA; |
| 381 | |
| 382 | ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 383 | if (ret < 0) |
| 384 | return (ret == -ERANGE) ? -EINVAL : ret; |
| 385 | |
| 386 | return fscrypt_policy_from_context(policy, &ctx, ret); |
| 387 | } |
| 388 | |
| 389 | static int set_encryption_policy(struct inode *inode, |
| 390 | const union fscrypt_policy *policy) |
| 391 | { |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 392 | u8 nonce[FSCRYPT_FILE_NONCE_SIZE]; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 393 | union fscrypt_context ctx; |
| 394 | int ctxsize; |
Eric Biggers | 5ab7189 | 2019-08-04 19:35:48 -0700 | [diff] [blame] | 395 | int err; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 396 | |
| 397 | if (!fscrypt_supported_policy(policy, inode)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 398 | return -EINVAL; |
| 399 | |
Eric Biggers | 5ab7189 | 2019-08-04 19:35:48 -0700 | [diff] [blame] | 400 | switch (policy->version) { |
| 401 | case FSCRYPT_POLICY_V1: |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 402 | /* |
| 403 | * The original encryption policy version provided no way of |
| 404 | * verifying that the correct master key was supplied, which was |
| 405 | * insecure in scenarios where multiple users have access to the |
| 406 | * same encrypted files (even just read-only access). The new |
| 407 | * encryption policy version fixes this and also implies use of |
| 408 | * an improved key derivation function and allows non-root users |
| 409 | * to securely remove keys. So as long as compatibility with |
| 410 | * old kernels isn't required, it is recommended to use the new |
| 411 | * policy version for all new encrypted directories. |
| 412 | */ |
| 413 | pr_warn_once("%s (pid %d) is setting deprecated v1 encryption policy; recommend upgrading to v2.\n", |
| 414 | current->comm, current->pid); |
Eric Biggers | 5ab7189 | 2019-08-04 19:35:48 -0700 | [diff] [blame] | 415 | break; |
| 416 | case FSCRYPT_POLICY_V2: |
| 417 | err = fscrypt_verify_key_added(inode->i_sb, |
| 418 | policy->v2.master_key_identifier); |
| 419 | if (err) |
| 420 | return err; |
Eric Biggers | e3b1078 | 2020-05-15 13:41:41 -0700 | [diff] [blame] | 421 | if (policy->v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32) |
| 422 | pr_warn_once("%s (pid %d) is setting an IV_INO_LBLK_32 encryption policy. This should only be used if there are certain hardware limitations.\n", |
| 423 | current->comm, current->pid); |
Eric Biggers | 5ab7189 | 2019-08-04 19:35:48 -0700 | [diff] [blame] | 424 | break; |
| 425 | default: |
| 426 | WARN_ON(1); |
| 427 | return -EINVAL; |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 428 | } |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 429 | |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 430 | get_random_bytes(nonce, FSCRYPT_FILE_NONCE_SIZE); |
| 431 | ctxsize = fscrypt_new_context(&ctx, policy, nonce); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 432 | |
| 433 | return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, NULL); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 436 | int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 437 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 438 | union fscrypt_policy policy; |
| 439 | union fscrypt_policy existing_policy; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 440 | struct inode *inode = file_inode(filp); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 441 | u8 version; |
| 442 | int size; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 443 | int ret; |
| 444 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 445 | if (get_user(policy.version, (const u8 __user *)arg)) |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 446 | return -EFAULT; |
| 447 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 448 | size = fscrypt_policy_size(&policy); |
| 449 | if (size <= 0) |
| 450 | return -EINVAL; |
| 451 | |
| 452 | /* |
| 453 | * We should just copy the remaining 'size - 1' bytes here, but a |
| 454 | * bizarre bug in gcc 7 and earlier (fixed by gcc r255731) causes gcc to |
| 455 | * think that size can be 0 here (despite the check above!) *and* that |
| 456 | * it's a compile-time constant. Thus it would think copy_from_user() |
| 457 | * is passed compile-time constant ULONG_MAX, causing the compile-time |
| 458 | * buffer overflow check to fail, breaking the build. This only occurred |
| 459 | * when building an i386 kernel with -Os and branch profiling enabled. |
| 460 | * |
| 461 | * Work around it by just copying the first byte again... |
| 462 | */ |
| 463 | version = policy.version; |
| 464 | if (copy_from_user(&policy, arg, size)) |
| 465 | return -EFAULT; |
| 466 | policy.version = version; |
| 467 | |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 468 | if (!inode_owner_or_capable(&init_user_ns, inode)) |
Eric Biggers | 163ae1c | 2016-09-08 10:57:08 -0700 | [diff] [blame] | 469 | return -EACCES; |
| 470 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 471 | ret = mnt_want_write_file(filp); |
| 472 | if (ret) |
| 473 | return ret; |
| 474 | |
Eric Biggers | 8906a82 | 2016-10-15 09:48:50 -0400 | [diff] [blame] | 475 | inode_lock(inode); |
| 476 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 477 | ret = fscrypt_get_policy(inode, &existing_policy); |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 478 | if (ret == -ENODATA) { |
Eric Biggers | 002ced4 | 2016-09-08 11:36:39 -0700 | [diff] [blame] | 479 | if (!S_ISDIR(inode->i_mode)) |
Eric Biggers | dffd0cf | 2016-12-05 11:12:45 -0800 | [diff] [blame] | 480 | ret = -ENOTDIR; |
Hongjie Fang | 5858bda | 2019-05-22 10:02:53 +0800 | [diff] [blame] | 481 | else if (IS_DEADDIR(inode)) |
| 482 | ret = -ENOENT; |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 483 | else if (!inode->i_sb->s_cop->empty_dir(inode)) |
| 484 | ret = -ENOTEMPTY; |
| 485 | else |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 486 | ret = set_encryption_policy(inode, &policy); |
| 487 | } else if (ret == -EINVAL || |
| 488 | (ret == 0 && !fscrypt_policies_equal(&policy, |
| 489 | &existing_policy))) { |
Eric Biggers | efee590e | 2016-12-05 11:12:48 -0800 | [diff] [blame] | 490 | /* The file already uses a different encryption policy. */ |
Eric Biggers | 8488cd9 | 2016-12-05 11:12:46 -0800 | [diff] [blame] | 491 | ret = -EEXIST; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Eric Biggers | 8906a82 | 2016-10-15 09:48:50 -0400 | [diff] [blame] | 494 | inode_unlock(inode); |
| 495 | |
Eric Biggers | ba63f23 | 2016-09-08 14:20:38 -0700 | [diff] [blame] | 496 | mnt_drop_write_file(filp); |
| 497 | return ret; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 498 | } |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 499 | EXPORT_SYMBOL(fscrypt_ioctl_set_policy); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 500 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 501 | /* Original ioctl version; can only get the original policy version */ |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 502 | int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 503 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 504 | union fscrypt_policy policy; |
| 505 | int err; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 506 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 507 | err = fscrypt_get_policy(file_inode(filp), &policy); |
| 508 | if (err) |
| 509 | return err; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 510 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 511 | if (policy.version != FSCRYPT_POLICY_V1) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 512 | return -EINVAL; |
| 513 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 514 | if (copy_to_user(arg, &policy, sizeof(policy.v1))) |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 515 | return -EFAULT; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 516 | return 0; |
| 517 | } |
Eric Biggers | db717d8 | 2016-11-26 19:07:49 -0500 | [diff] [blame] | 518 | EXPORT_SYMBOL(fscrypt_ioctl_get_policy); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 519 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 520 | /* Extended ioctl version; can get policies of any version */ |
| 521 | int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *uarg) |
| 522 | { |
| 523 | struct fscrypt_get_policy_ex_arg arg; |
| 524 | union fscrypt_policy *policy = (union fscrypt_policy *)&arg.policy; |
| 525 | size_t policy_size; |
| 526 | int err; |
| 527 | |
| 528 | /* arg is policy_size, then policy */ |
| 529 | BUILD_BUG_ON(offsetof(typeof(arg), policy_size) != 0); |
| 530 | BUILD_BUG_ON(offsetofend(typeof(arg), policy_size) != |
| 531 | offsetof(typeof(arg), policy)); |
| 532 | BUILD_BUG_ON(sizeof(arg.policy) != sizeof(*policy)); |
| 533 | |
| 534 | err = fscrypt_get_policy(file_inode(filp), policy); |
| 535 | if (err) |
| 536 | return err; |
| 537 | policy_size = fscrypt_policy_size(policy); |
| 538 | |
| 539 | if (copy_from_user(&arg, uarg, sizeof(arg.policy_size))) |
| 540 | return -EFAULT; |
| 541 | |
| 542 | if (policy_size > arg.policy_size) |
| 543 | return -EOVERFLOW; |
| 544 | arg.policy_size = policy_size; |
| 545 | |
| 546 | if (copy_to_user(uarg, &arg, sizeof(arg.policy_size) + policy_size)) |
| 547 | return -EFAULT; |
| 548 | return 0; |
| 549 | } |
| 550 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_policy_ex); |
| 551 | |
Eric Biggers | e98ad46 | 2020-03-14 13:50:49 -0700 | [diff] [blame] | 552 | /* FS_IOC_GET_ENCRYPTION_NONCE: retrieve file's encryption nonce for testing */ |
| 553 | int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg) |
| 554 | { |
| 555 | struct inode *inode = file_inode(filp); |
| 556 | union fscrypt_context ctx; |
| 557 | int ret; |
| 558 | |
| 559 | ret = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx)); |
| 560 | if (ret < 0) |
| 561 | return ret; |
| 562 | if (!fscrypt_context_is_valid(&ctx, ret)) |
| 563 | return -EINVAL; |
| 564 | if (copy_to_user(arg, fscrypt_context_nonce(&ctx), |
Eric Biggers | 1d6217a4 | 2020-07-08 14:57:22 -0700 | [diff] [blame] | 565 | FSCRYPT_FILE_NONCE_SIZE)) |
Eric Biggers | e98ad46 | 2020-03-14 13:50:49 -0700 | [diff] [blame] | 566 | return -EFAULT; |
| 567 | return 0; |
| 568 | } |
| 569 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_nonce); |
| 570 | |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 571 | /** |
| 572 | * fscrypt_has_permitted_context() - is a file's encryption policy permitted |
| 573 | * within its directory? |
| 574 | * |
| 575 | * @parent: inode for parent directory |
| 576 | * @child: inode for file being looked up, opened, or linked into @parent |
| 577 | * |
| 578 | * Filesystems must call this before permitting access to an inode in a |
| 579 | * situation where the parent directory is encrypted (either before allowing |
| 580 | * ->lookup() to succeed, or for a regular file before allowing it to be opened) |
| 581 | * and before any operation that involves linking an inode into an encrypted |
| 582 | * directory, including link, rename, and cross rename. It enforces the |
| 583 | * constraint that within a given encrypted directory tree, all files use the |
| 584 | * same encryption policy. The pre-access check is needed to detect potentially |
| 585 | * malicious offline violations of this constraint, while the link and rename |
| 586 | * checks are needed to prevent online violations of this constraint. |
| 587 | * |
Eric Biggers | f5e55e7 | 2019-01-22 16:20:21 -0800 | [diff] [blame] | 588 | * Return: 1 if permitted, 0 if forbidden. |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 589 | */ |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 590 | int fscrypt_has_permitted_context(struct inode *parent, struct inode *child) |
| 591 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 592 | union fscrypt_policy parent_policy, child_policy; |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 593 | int err, err1, err2; |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 594 | |
Eric Biggers | 42d97eb | 2016-12-19 14:20:13 -0800 | [diff] [blame] | 595 | /* No restrictions on file types which are never encrypted */ |
| 596 | if (!S_ISREG(child->i_mode) && !S_ISDIR(child->i_mode) && |
| 597 | !S_ISLNK(child->i_mode)) |
| 598 | return 1; |
| 599 | |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 600 | /* No restrictions if the parent directory is unencrypted */ |
Eric Biggers | e0428a2 | 2017-10-09 12:15:36 -0700 | [diff] [blame] | 601 | if (!IS_ENCRYPTED(parent)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 602 | return 1; |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 603 | |
| 604 | /* Encrypted directories must not contain unencrypted files */ |
Eric Biggers | e0428a2 | 2017-10-09 12:15:36 -0700 | [diff] [blame] | 605 | if (!IS_ENCRYPTED(child)) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 606 | return 0; |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 607 | |
| 608 | /* |
| 609 | * Both parent and child are encrypted, so verify they use the same |
| 610 | * encryption policy. Compare the fscrypt_info structs if the keys are |
| 611 | * available, otherwise retrieve and compare the fscrypt_contexts. |
| 612 | * |
| 613 | * Note that the fscrypt_context retrieval will be required frequently |
| 614 | * when accessing an encrypted directory tree without the key. |
| 615 | * Performance-wise this is not a big deal because we already don't |
| 616 | * really optimize for file access without the key (to the extent that |
| 617 | * such access is even possible), given that any attempted access |
| 618 | * already causes a fscrypt_context retrieval and keyring search. |
| 619 | * |
| 620 | * In any case, if an unexpected error occurs, fall back to "forbidden". |
| 621 | */ |
| 622 | |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 623 | err = fscrypt_get_encryption_info(parent, true); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 624 | if (err) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 625 | return 0; |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 626 | err = fscrypt_get_encryption_info(child, true); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 627 | if (err) |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 628 | return 0; |
| 629 | |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 630 | err1 = fscrypt_get_policy(parent, &parent_policy); |
| 631 | err2 = fscrypt_get_policy(child, &child_policy); |
Eric Biggers | 272f98f | 2017-04-07 10:58:37 -0700 | [diff] [blame] | 632 | |
Eric Biggers | a14d0b6 | 2020-12-02 18:20:41 -0800 | [diff] [blame] | 633 | /* |
| 634 | * Allow the case where the parent and child both have an unrecognized |
| 635 | * encryption policy, so that files with an unrecognized encryption |
| 636 | * policy can be deleted. |
| 637 | */ |
| 638 | if (err1 == -EINVAL && err2 == -EINVAL) |
| 639 | return 1; |
| 640 | |
| 641 | if (err1 || err2) |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame] | 642 | return 0; |
| 643 | |
| 644 | return fscrypt_policies_equal(&parent_policy, &child_policy); |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 645 | } |
| 646 | EXPORT_SYMBOL(fscrypt_has_permitted_context); |
| 647 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 648 | /* |
| 649 | * Return the encryption policy that new files in the directory will inherit, or |
| 650 | * NULL if none, or an ERR_PTR() on error. If the directory is encrypted, also |
| 651 | * ensure that its key is set up, so that the new filename can be encrypted. |
| 652 | */ |
| 653 | const union fscrypt_policy *fscrypt_policy_to_inherit(struct inode *dir) |
| 654 | { |
| 655 | int err; |
| 656 | |
| 657 | if (IS_ENCRYPTED(dir)) { |
| 658 | err = fscrypt_require_key(dir); |
| 659 | if (err) |
| 660 | return ERR_PTR(err); |
| 661 | return &dir->i_crypt_info->ci_policy; |
| 662 | } |
| 663 | |
| 664 | return fscrypt_get_dummy_policy(dir->i_sb); |
| 665 | } |
| 666 | |
Jaegeuk Kim | 0b81d07 | 2015-05-15 16:26:10 -0700 | [diff] [blame] | 667 | /** |
Eric Biggers | a992b20 | 2020-09-16 21:11:24 -0700 | [diff] [blame] | 668 | * fscrypt_set_context() - Set the fscrypt context of a new inode |
| 669 | * @inode: a new inode |
| 670 | * @fs_data: private data given by FS and passed to ->set_context() |
| 671 | * |
| 672 | * This should be called after fscrypt_prepare_new_inode(), generally during a |
| 673 | * filesystem transaction. Everything here must be %GFP_NOFS-safe. |
| 674 | * |
| 675 | * Return: 0 on success, -errno on failure |
| 676 | */ |
| 677 | int fscrypt_set_context(struct inode *inode, void *fs_data) |
| 678 | { |
| 679 | struct fscrypt_info *ci = inode->i_crypt_info; |
| 680 | union fscrypt_context ctx; |
| 681 | int ctxsize; |
| 682 | |
| 683 | /* fscrypt_prepare_new_inode() should have set up the key already. */ |
| 684 | if (WARN_ON_ONCE(!ci)) |
| 685 | return -ENOKEY; |
| 686 | |
| 687 | BUILD_BUG_ON(sizeof(ctx) != FSCRYPT_SET_CONTEXT_MAX_SIZE); |
| 688 | ctxsize = fscrypt_new_context(&ctx, &ci->ci_policy, ci->ci_nonce); |
| 689 | |
| 690 | /* |
| 691 | * This may be the first time the inode number is available, so do any |
| 692 | * delayed key setup that requires the inode number. |
| 693 | */ |
| 694 | if (ci->ci_policy.version == FSCRYPT_POLICY_V2 && |
| 695 | (ci->ci_policy.v2.flags & FSCRYPT_POLICY_FLAG_IV_INO_LBLK_32)) { |
| 696 | const struct fscrypt_master_key *mk = |
| 697 | ci->ci_master_key->payload.data[0]; |
| 698 | |
| 699 | fscrypt_hash_inode_number(ci, mk); |
| 700 | } |
| 701 | |
| 702 | return inode->i_sb->s_cop->set_context(inode, &ctx, ctxsize, fs_data); |
| 703 | } |
| 704 | EXPORT_SYMBOL_GPL(fscrypt_set_context); |
| 705 | |
| 706 | /** |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 707 | * fscrypt_set_test_dummy_encryption() - handle '-o test_dummy_encryption' |
| 708 | * @sb: the filesystem on which test_dummy_encryption is being specified |
Eric Biggers | c8c868a | 2020-09-16 21:11:36 -0700 | [diff] [blame] | 709 | * @arg: the argument to the test_dummy_encryption option. May be NULL. |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 710 | * @dummy_policy: the filesystem's current dummy policy (input/output, see |
| 711 | * below) |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 712 | * |
| 713 | * Handle the test_dummy_encryption mount option by creating a dummy encryption |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 714 | * policy, saving it in @dummy_policy, and adding the corresponding dummy |
| 715 | * encryption key to the filesystem. If the @dummy_policy is already set, then |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 716 | * instead validate that it matches @arg. Don't support changing it via |
| 717 | * remount, as that is difficult to do safely. |
| 718 | * |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 719 | * Return: 0 on success (dummy policy set, or the same policy is already set); |
| 720 | * -EEXIST if a different dummy policy is already set; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 721 | * or another -errno value. |
| 722 | */ |
Eric Biggers | c8c868a | 2020-09-16 21:11:36 -0700 | [diff] [blame] | 723 | int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg, |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 724 | struct fscrypt_dummy_policy *dummy_policy) |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 725 | { |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 726 | struct fscrypt_key_specifier key_spec = { 0 }; |
| 727 | int version; |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 728 | union fscrypt_policy *policy = NULL; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 729 | int err; |
| 730 | |
Eric Biggers | c8c868a | 2020-09-16 21:11:36 -0700 | [diff] [blame] | 731 | if (!arg) |
| 732 | arg = "v2"; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 733 | |
Eric Biggers | c8c868a | 2020-09-16 21:11:36 -0700 | [diff] [blame] | 734 | if (!strcmp(arg, "v1")) { |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 735 | version = FSCRYPT_POLICY_V1; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 736 | key_spec.type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR; |
| 737 | memset(key_spec.u.descriptor, 0x42, |
| 738 | FSCRYPT_KEY_DESCRIPTOR_SIZE); |
Eric Biggers | c8c868a | 2020-09-16 21:11:36 -0700 | [diff] [blame] | 739 | } else if (!strcmp(arg, "v2")) { |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 740 | version = FSCRYPT_POLICY_V2; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 741 | key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER; |
| 742 | /* key_spec.u.identifier gets filled in when adding the key */ |
| 743 | } else { |
| 744 | err = -EINVAL; |
| 745 | goto out; |
| 746 | } |
| 747 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 748 | policy = kzalloc(sizeof(*policy), GFP_KERNEL); |
| 749 | if (!policy) { |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 750 | err = -ENOMEM; |
| 751 | goto out; |
| 752 | } |
| 753 | |
| 754 | err = fscrypt_add_test_dummy_key(sb, &key_spec); |
| 755 | if (err) |
| 756 | goto out; |
| 757 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 758 | policy->version = version; |
| 759 | switch (policy->version) { |
| 760 | case FSCRYPT_POLICY_V1: |
| 761 | policy->v1.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; |
| 762 | policy->v1.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; |
| 763 | memcpy(policy->v1.master_key_descriptor, key_spec.u.descriptor, |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 764 | FSCRYPT_KEY_DESCRIPTOR_SIZE); |
| 765 | break; |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 766 | case FSCRYPT_POLICY_V2: |
| 767 | policy->v2.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS; |
| 768 | policy->v2.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS; |
| 769 | memcpy(policy->v2.master_key_identifier, key_spec.u.identifier, |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 770 | FSCRYPT_KEY_IDENTIFIER_SIZE); |
| 771 | break; |
| 772 | default: |
| 773 | WARN_ON(1); |
| 774 | err = -EINVAL; |
| 775 | goto out; |
| 776 | } |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 777 | |
| 778 | if (dummy_policy->policy) { |
| 779 | if (fscrypt_policies_equal(policy, dummy_policy->policy)) |
| 780 | err = 0; |
| 781 | else |
| 782 | err = -EEXIST; |
| 783 | goto out; |
| 784 | } |
| 785 | dummy_policy->policy = policy; |
| 786 | policy = NULL; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 787 | err = 0; |
| 788 | out: |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 789 | kfree(policy); |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 790 | return err; |
| 791 | } |
| 792 | EXPORT_SYMBOL_GPL(fscrypt_set_test_dummy_encryption); |
| 793 | |
| 794 | /** |
| 795 | * fscrypt_show_test_dummy_encryption() - show '-o test_dummy_encryption' |
| 796 | * @seq: the seq_file to print the option to |
| 797 | * @sep: the separator character to use |
| 798 | * @sb: the filesystem whose options are being shown |
| 799 | * |
| 800 | * Show the test_dummy_encryption mount option, if it was specified. |
| 801 | * This is mainly used for /proc/mounts. |
| 802 | */ |
| 803 | void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep, |
| 804 | struct super_block *sb) |
| 805 | { |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 806 | const union fscrypt_policy *policy = fscrypt_get_dummy_policy(sb); |
| 807 | int vers; |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 808 | |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 809 | if (!policy) |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 810 | return; |
Eric Biggers | ac4acb1 | 2020-09-16 21:11:35 -0700 | [diff] [blame] | 811 | |
| 812 | vers = policy->version; |
| 813 | if (vers == FSCRYPT_POLICY_V1) /* Handle numbering quirk */ |
| 814 | vers = 1; |
| 815 | |
| 816 | seq_printf(seq, "%ctest_dummy_encryption=v%d", sep, vers); |
Eric Biggers | ed318a6 | 2020-05-12 16:32:50 -0700 | [diff] [blame] | 817 | } |
| 818 | EXPORT_SYMBOL_GPL(fscrypt_show_test_dummy_encryption); |