Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Filesystem-level keyring for fscrypt |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * This file implements management of fscrypt master keys in the |
| 10 | * filesystem-level keyring, including the ioctls: |
| 11 | * |
| 12 | * - FS_IOC_ADD_ENCRYPTION_KEY |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 13 | * - FS_IOC_REMOVE_ENCRYPTION_KEY |
Eric Biggers | 5a7e299 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 14 | * - FS_IOC_GET_ENCRYPTION_KEY_STATUS |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 15 | * |
| 16 | * See the "User API" section of Documentation/filesystems/fscrypt.rst for more |
| 17 | * information about these ioctls. |
| 18 | */ |
| 19 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame^] | 20 | #include <crypto/skcipher.h> |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 21 | #include <linux/key-type.h> |
| 22 | #include <linux/seq_file.h> |
| 23 | |
| 24 | #include "fscrypt_private.h" |
| 25 | |
| 26 | static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret) |
| 27 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame^] | 28 | fscrypt_destroy_hkdf(&secret->hkdf); |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 29 | memzero_explicit(secret, sizeof(*secret)); |
| 30 | } |
| 31 | |
| 32 | static void move_master_key_secret(struct fscrypt_master_key_secret *dst, |
| 33 | struct fscrypt_master_key_secret *src) |
| 34 | { |
| 35 | memcpy(dst, src, sizeof(*dst)); |
| 36 | memzero_explicit(src, sizeof(*src)); |
| 37 | } |
| 38 | |
| 39 | static void free_master_key(struct fscrypt_master_key *mk) |
| 40 | { |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame^] | 41 | size_t i; |
| 42 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 43 | wipe_master_key_secret(&mk->mk_secret); |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame^] | 44 | |
| 45 | for (i = 0; i < ARRAY_SIZE(mk->mk_mode_keys); i++) |
| 46 | crypto_free_skcipher(mk->mk_mode_keys[i]); |
| 47 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 48 | kzfree(mk); |
| 49 | } |
| 50 | |
| 51 | static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec) |
| 52 | { |
| 53 | if (spec->__reserved) |
| 54 | return false; |
| 55 | return master_key_spec_len(spec) != 0; |
| 56 | } |
| 57 | |
| 58 | static int fscrypt_key_instantiate(struct key *key, |
| 59 | struct key_preparsed_payload *prep) |
| 60 | { |
| 61 | key->payload.data[0] = (struct fscrypt_master_key *)prep->data; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static void fscrypt_key_destroy(struct key *key) |
| 66 | { |
| 67 | free_master_key(key->payload.data[0]); |
| 68 | } |
| 69 | |
| 70 | static void fscrypt_key_describe(const struct key *key, struct seq_file *m) |
| 71 | { |
| 72 | seq_puts(m, key->description); |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 73 | |
| 74 | if (key_is_positive(key)) { |
| 75 | const struct fscrypt_master_key *mk = key->payload.data[0]; |
| 76 | |
| 77 | if (!is_master_key_secret_present(&mk->mk_secret)) |
| 78 | seq_puts(m, ": secret removed"); |
| 79 | } |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Type of key in ->s_master_keys. Each key of this type represents a master |
| 84 | * key which has been added to the filesystem. Its payload is a |
| 85 | * 'struct fscrypt_master_key'. The "." prefix in the key type name prevents |
| 86 | * users from adding keys of this type via the keyrings syscalls rather than via |
| 87 | * the intended method of FS_IOC_ADD_ENCRYPTION_KEY. |
| 88 | */ |
| 89 | static struct key_type key_type_fscrypt = { |
| 90 | .name = "._fscrypt", |
| 91 | .instantiate = fscrypt_key_instantiate, |
| 92 | .destroy = fscrypt_key_destroy, |
| 93 | .describe = fscrypt_key_describe, |
| 94 | }; |
| 95 | |
| 96 | /* Search ->s_master_keys */ |
| 97 | static struct key *search_fscrypt_keyring(struct key *keyring, |
| 98 | struct key_type *type, |
| 99 | const char *description) |
| 100 | { |
| 101 | /* |
| 102 | * We need to mark the keyring reference as "possessed" so that we |
| 103 | * acquire permission to search it, via the KEY_POS_SEARCH permission. |
| 104 | */ |
| 105 | key_ref_t keyref = make_key_ref(keyring, true /* possessed */); |
| 106 | |
| 107 | keyref = keyring_search(keyref, type, description, false); |
| 108 | if (IS_ERR(keyref)) { |
| 109 | if (PTR_ERR(keyref) == -EAGAIN || /* not found */ |
| 110 | PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */ |
| 111 | keyref = ERR_PTR(-ENOKEY); |
| 112 | return ERR_CAST(keyref); |
| 113 | } |
| 114 | return key_ref_to_ptr(keyref); |
| 115 | } |
| 116 | |
| 117 | #define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \ |
| 118 | (CONST_STRLEN("fscrypt-") + FIELD_SIZEOF(struct super_block, s_id)) |
| 119 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame^] | 120 | #define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1) |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 121 | |
| 122 | static void format_fs_keyring_description( |
| 123 | char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE], |
| 124 | const struct super_block *sb) |
| 125 | { |
| 126 | sprintf(description, "fscrypt-%s", sb->s_id); |
| 127 | } |
| 128 | |
| 129 | static void format_mk_description( |
| 130 | char description[FSCRYPT_MK_DESCRIPTION_SIZE], |
| 131 | const struct fscrypt_key_specifier *mk_spec) |
| 132 | { |
| 133 | sprintf(description, "%*phN", |
| 134 | master_key_spec_len(mk_spec), (u8 *)&mk_spec->u); |
| 135 | } |
| 136 | |
| 137 | /* Create ->s_master_keys if needed. Synchronized by fscrypt_add_key_mutex. */ |
| 138 | static int allocate_filesystem_keyring(struct super_block *sb) |
| 139 | { |
| 140 | char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE]; |
| 141 | struct key *keyring; |
| 142 | |
| 143 | if (sb->s_master_keys) |
| 144 | return 0; |
| 145 | |
| 146 | format_fs_keyring_description(description, sb); |
| 147 | keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, |
| 148 | current_cred(), KEY_POS_SEARCH | |
| 149 | KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW, |
| 150 | KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL); |
| 151 | if (IS_ERR(keyring)) |
| 152 | return PTR_ERR(keyring); |
| 153 | |
| 154 | /* Pairs with READ_ONCE() in fscrypt_find_master_key() */ |
| 155 | smp_store_release(&sb->s_master_keys, keyring); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | void fscrypt_sb_free(struct super_block *sb) |
| 160 | { |
| 161 | key_put(sb->s_master_keys); |
| 162 | sb->s_master_keys = NULL; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Find the specified master key in ->s_master_keys. |
| 167 | * Returns ERR_PTR(-ENOKEY) if not found. |
| 168 | */ |
| 169 | struct key *fscrypt_find_master_key(struct super_block *sb, |
| 170 | const struct fscrypt_key_specifier *mk_spec) |
| 171 | { |
| 172 | struct key *keyring; |
| 173 | char description[FSCRYPT_MK_DESCRIPTION_SIZE]; |
| 174 | |
| 175 | /* pairs with smp_store_release() in allocate_filesystem_keyring() */ |
| 176 | keyring = READ_ONCE(sb->s_master_keys); |
| 177 | if (keyring == NULL) |
| 178 | return ERR_PTR(-ENOKEY); /* No keyring yet, so no keys yet. */ |
| 179 | |
| 180 | format_mk_description(description, mk_spec); |
| 181 | return search_fscrypt_keyring(keyring, &key_type_fscrypt, description); |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Allocate a new fscrypt_master_key which contains the given secret, set it as |
| 186 | * the payload of a new 'struct key' of type fscrypt, and link the 'struct key' |
| 187 | * into the given keyring. Synchronized by fscrypt_add_key_mutex. |
| 188 | */ |
| 189 | static int add_new_master_key(struct fscrypt_master_key_secret *secret, |
| 190 | const struct fscrypt_key_specifier *mk_spec, |
| 191 | struct key *keyring) |
| 192 | { |
| 193 | struct fscrypt_master_key *mk; |
| 194 | char description[FSCRYPT_MK_DESCRIPTION_SIZE]; |
| 195 | struct key *key; |
| 196 | int err; |
| 197 | |
| 198 | mk = kzalloc(sizeof(*mk), GFP_KERNEL); |
| 199 | if (!mk) |
| 200 | return -ENOMEM; |
| 201 | |
| 202 | mk->mk_spec = *mk_spec; |
| 203 | |
| 204 | move_master_key_secret(&mk->mk_secret, secret); |
| 205 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 206 | refcount_set(&mk->mk_refcount, 1); /* secret is present */ |
| 207 | INIT_LIST_HEAD(&mk->mk_decrypted_inodes); |
| 208 | spin_lock_init(&mk->mk_decrypted_inodes_lock); |
| 209 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 210 | format_mk_description(description, mk_spec); |
| 211 | key = key_alloc(&key_type_fscrypt, description, |
| 212 | GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(), |
| 213 | KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW, |
| 214 | KEY_ALLOC_NOT_IN_QUOTA, NULL); |
| 215 | if (IS_ERR(key)) { |
| 216 | err = PTR_ERR(key); |
| 217 | goto out_free_mk; |
| 218 | } |
| 219 | err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL); |
| 220 | key_put(key); |
| 221 | if (err) |
| 222 | goto out_free_mk; |
| 223 | |
| 224 | return 0; |
| 225 | |
| 226 | out_free_mk: |
| 227 | free_master_key(mk); |
| 228 | return err; |
| 229 | } |
| 230 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 231 | #define KEY_DEAD 1 |
| 232 | |
| 233 | static int add_existing_master_key(struct fscrypt_master_key *mk, |
| 234 | struct fscrypt_master_key_secret *secret) |
| 235 | { |
| 236 | if (is_master_key_secret_present(&mk->mk_secret)) |
| 237 | return 0; |
| 238 | |
| 239 | if (!refcount_inc_not_zero(&mk->mk_refcount)) |
| 240 | return KEY_DEAD; |
| 241 | |
| 242 | move_master_key_secret(&mk->mk_secret, secret); |
| 243 | return 0; |
| 244 | } |
| 245 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 246 | static int add_master_key(struct super_block *sb, |
| 247 | struct fscrypt_master_key_secret *secret, |
| 248 | const struct fscrypt_key_specifier *mk_spec) |
| 249 | { |
| 250 | static DEFINE_MUTEX(fscrypt_add_key_mutex); |
| 251 | struct key *key; |
| 252 | int err; |
| 253 | |
| 254 | mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */ |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 255 | retry: |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 256 | key = fscrypt_find_master_key(sb, mk_spec); |
| 257 | if (IS_ERR(key)) { |
| 258 | err = PTR_ERR(key); |
| 259 | if (err != -ENOKEY) |
| 260 | goto out_unlock; |
| 261 | /* Didn't find the key in ->s_master_keys. Add it. */ |
| 262 | err = allocate_filesystem_keyring(sb); |
| 263 | if (err) |
| 264 | goto out_unlock; |
| 265 | err = add_new_master_key(secret, mk_spec, sb->s_master_keys); |
| 266 | } else { |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 267 | /* |
| 268 | * Found the key in ->s_master_keys. Re-add the secret if |
| 269 | * needed. |
| 270 | */ |
| 271 | down_write(&key->sem); |
| 272 | err = add_existing_master_key(key->payload.data[0], secret); |
| 273 | up_write(&key->sem); |
| 274 | if (err == KEY_DEAD) { |
| 275 | /* Key being removed or needs to be removed */ |
| 276 | key_invalidate(key); |
| 277 | key_put(key); |
| 278 | goto retry; |
| 279 | } |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 280 | key_put(key); |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 281 | } |
| 282 | out_unlock: |
| 283 | mutex_unlock(&fscrypt_add_key_mutex); |
| 284 | return err; |
| 285 | } |
| 286 | |
| 287 | /* |
| 288 | * Add a master encryption key to the filesystem, causing all files which were |
| 289 | * encrypted with it to appear "unlocked" (decrypted) when accessed. |
| 290 | * |
| 291 | * For more details, see the "FS_IOC_ADD_ENCRYPTION_KEY" section of |
| 292 | * Documentation/filesystems/fscrypt.rst. |
| 293 | */ |
| 294 | int fscrypt_ioctl_add_key(struct file *filp, void __user *_uarg) |
| 295 | { |
| 296 | struct super_block *sb = file_inode(filp)->i_sb; |
| 297 | struct fscrypt_add_key_arg __user *uarg = _uarg; |
| 298 | struct fscrypt_add_key_arg arg; |
| 299 | struct fscrypt_master_key_secret secret; |
| 300 | int err; |
| 301 | |
| 302 | if (copy_from_user(&arg, uarg, sizeof(arg))) |
| 303 | return -EFAULT; |
| 304 | |
| 305 | if (!valid_key_spec(&arg.key_spec)) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | if (arg.raw_size < FSCRYPT_MIN_KEY_SIZE || |
| 309 | arg.raw_size > FSCRYPT_MAX_KEY_SIZE) |
| 310 | return -EINVAL; |
| 311 | |
| 312 | if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) |
| 313 | return -EINVAL; |
| 314 | |
| 315 | memset(&secret, 0, sizeof(secret)); |
| 316 | secret.size = arg.raw_size; |
| 317 | err = -EFAULT; |
| 318 | if (copy_from_user(secret.raw, uarg->raw, secret.size)) |
| 319 | goto out_wipe_secret; |
| 320 | |
| 321 | err = -EACCES; |
| 322 | if (!capable(CAP_SYS_ADMIN)) |
| 323 | goto out_wipe_secret; |
| 324 | |
Eric Biggers | 5dae460 | 2019-08-04 19:35:47 -0700 | [diff] [blame^] | 325 | if (arg.key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) { |
| 326 | err = fscrypt_init_hkdf(&secret.hkdf, secret.raw, secret.size); |
| 327 | if (err) |
| 328 | goto out_wipe_secret; |
| 329 | |
| 330 | /* |
| 331 | * Now that the HKDF context is initialized, the raw key is no |
| 332 | * longer needed. |
| 333 | */ |
| 334 | memzero_explicit(secret.raw, secret.size); |
| 335 | |
| 336 | /* Calculate the key identifier and return it to userspace. */ |
| 337 | err = fscrypt_hkdf_expand(&secret.hkdf, |
| 338 | HKDF_CONTEXT_KEY_IDENTIFIER, |
| 339 | NULL, 0, arg.key_spec.u.identifier, |
| 340 | FSCRYPT_KEY_IDENTIFIER_SIZE); |
| 341 | if (err) |
| 342 | goto out_wipe_secret; |
| 343 | err = -EFAULT; |
| 344 | if (copy_to_user(uarg->key_spec.u.identifier, |
| 345 | arg.key_spec.u.identifier, |
| 346 | FSCRYPT_KEY_IDENTIFIER_SIZE)) |
| 347 | goto out_wipe_secret; |
| 348 | } |
| 349 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 350 | err = add_master_key(sb, &secret, &arg.key_spec); |
| 351 | out_wipe_secret: |
| 352 | wipe_master_key_secret(&secret); |
| 353 | return err; |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_add_key); |
| 356 | |
Eric Biggers | b1c0ec3 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 357 | /* |
| 358 | * Try to evict the inode's dentries from the dentry cache. If the inode is a |
| 359 | * directory, then it can have at most one dentry; however, that dentry may be |
| 360 | * pinned by child dentries, so first try to evict the children too. |
| 361 | */ |
| 362 | static void shrink_dcache_inode(struct inode *inode) |
| 363 | { |
| 364 | struct dentry *dentry; |
| 365 | |
| 366 | if (S_ISDIR(inode->i_mode)) { |
| 367 | dentry = d_find_any_alias(inode); |
| 368 | if (dentry) { |
| 369 | shrink_dcache_parent(dentry); |
| 370 | dput(dentry); |
| 371 | } |
| 372 | } |
| 373 | d_prune_aliases(inode); |
| 374 | } |
| 375 | |
| 376 | static void evict_dentries_for_decrypted_inodes(struct fscrypt_master_key *mk) |
| 377 | { |
| 378 | struct fscrypt_info *ci; |
| 379 | struct inode *inode; |
| 380 | struct inode *toput_inode = NULL; |
| 381 | |
| 382 | spin_lock(&mk->mk_decrypted_inodes_lock); |
| 383 | |
| 384 | list_for_each_entry(ci, &mk->mk_decrypted_inodes, ci_master_key_link) { |
| 385 | inode = ci->ci_inode; |
| 386 | spin_lock(&inode->i_lock); |
| 387 | if (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW)) { |
| 388 | spin_unlock(&inode->i_lock); |
| 389 | continue; |
| 390 | } |
| 391 | __iget(inode); |
| 392 | spin_unlock(&inode->i_lock); |
| 393 | spin_unlock(&mk->mk_decrypted_inodes_lock); |
| 394 | |
| 395 | shrink_dcache_inode(inode); |
| 396 | iput(toput_inode); |
| 397 | toput_inode = inode; |
| 398 | |
| 399 | spin_lock(&mk->mk_decrypted_inodes_lock); |
| 400 | } |
| 401 | |
| 402 | spin_unlock(&mk->mk_decrypted_inodes_lock); |
| 403 | iput(toput_inode); |
| 404 | } |
| 405 | |
| 406 | static int check_for_busy_inodes(struct super_block *sb, |
| 407 | struct fscrypt_master_key *mk) |
| 408 | { |
| 409 | struct list_head *pos; |
| 410 | size_t busy_count = 0; |
| 411 | unsigned long ino; |
| 412 | struct dentry *dentry; |
| 413 | char _path[256]; |
| 414 | char *path = NULL; |
| 415 | |
| 416 | spin_lock(&mk->mk_decrypted_inodes_lock); |
| 417 | |
| 418 | list_for_each(pos, &mk->mk_decrypted_inodes) |
| 419 | busy_count++; |
| 420 | |
| 421 | if (busy_count == 0) { |
| 422 | spin_unlock(&mk->mk_decrypted_inodes_lock); |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | { |
| 427 | /* select an example file to show for debugging purposes */ |
| 428 | struct inode *inode = |
| 429 | list_first_entry(&mk->mk_decrypted_inodes, |
| 430 | struct fscrypt_info, |
| 431 | ci_master_key_link)->ci_inode; |
| 432 | ino = inode->i_ino; |
| 433 | dentry = d_find_alias(inode); |
| 434 | } |
| 435 | spin_unlock(&mk->mk_decrypted_inodes_lock); |
| 436 | |
| 437 | if (dentry) { |
| 438 | path = dentry_path(dentry, _path, sizeof(_path)); |
| 439 | dput(dentry); |
| 440 | } |
| 441 | if (IS_ERR_OR_NULL(path)) |
| 442 | path = "(unknown)"; |
| 443 | |
| 444 | fscrypt_warn(NULL, |
| 445 | "%s: %zu inode(s) still busy after removing key with %s %*phN, including ino %lu (%s)", |
| 446 | sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec), |
| 447 | master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u, |
| 448 | ino, path); |
| 449 | return -EBUSY; |
| 450 | } |
| 451 | |
| 452 | static int try_to_lock_encrypted_files(struct super_block *sb, |
| 453 | struct fscrypt_master_key *mk) |
| 454 | { |
| 455 | int err1; |
| 456 | int err2; |
| 457 | |
| 458 | /* |
| 459 | * An inode can't be evicted while it is dirty or has dirty pages. |
| 460 | * Thus, we first have to clean the inodes in ->mk_decrypted_inodes. |
| 461 | * |
| 462 | * Just do it the easy way: call sync_filesystem(). It's overkill, but |
| 463 | * it works, and it's more important to minimize the amount of caches we |
| 464 | * drop than the amount of data we sync. Also, unprivileged users can |
| 465 | * already call sync_filesystem() via sys_syncfs() or sys_sync(). |
| 466 | */ |
| 467 | down_read(&sb->s_umount); |
| 468 | err1 = sync_filesystem(sb); |
| 469 | up_read(&sb->s_umount); |
| 470 | /* If a sync error occurs, still try to evict as much as possible. */ |
| 471 | |
| 472 | /* |
| 473 | * Inodes are pinned by their dentries, so we have to evict their |
| 474 | * dentries. shrink_dcache_sb() would suffice, but would be overkill |
| 475 | * and inappropriate for use by unprivileged users. So instead go |
| 476 | * through the inodes' alias lists and try to evict each dentry. |
| 477 | */ |
| 478 | evict_dentries_for_decrypted_inodes(mk); |
| 479 | |
| 480 | /* |
| 481 | * evict_dentries_for_decrypted_inodes() already iput() each inode in |
| 482 | * the list; any inodes for which that dropped the last reference will |
| 483 | * have been evicted due to fscrypt_drop_inode() detecting the key |
| 484 | * removal and telling the VFS to evict the inode. So to finish, we |
| 485 | * just need to check whether any inodes couldn't be evicted. |
| 486 | */ |
| 487 | err2 = check_for_busy_inodes(sb, mk); |
| 488 | |
| 489 | return err1 ?: err2; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Try to remove an fscrypt master encryption key. |
| 494 | * |
| 495 | * First we wipe the actual master key secret, so that no more inodes can be |
| 496 | * unlocked with it. Then we try to evict all cached inodes that had been |
| 497 | * unlocked with the key. |
| 498 | * |
| 499 | * If all inodes were evicted, then we unlink the fscrypt_master_key from the |
| 500 | * keyring. Otherwise it remains in the keyring in the "incompletely removed" |
| 501 | * state (without the actual secret key) where it tracks the list of remaining |
| 502 | * inodes. Userspace can execute the ioctl again later to retry eviction, or |
| 503 | * alternatively can re-add the secret key again. |
| 504 | * |
| 505 | * For more details, see the "Removing keys" section of |
| 506 | * Documentation/filesystems/fscrypt.rst. |
| 507 | */ |
| 508 | int fscrypt_ioctl_remove_key(struct file *filp, void __user *_uarg) |
| 509 | { |
| 510 | struct super_block *sb = file_inode(filp)->i_sb; |
| 511 | struct fscrypt_remove_key_arg __user *uarg = _uarg; |
| 512 | struct fscrypt_remove_key_arg arg; |
| 513 | struct key *key; |
| 514 | struct fscrypt_master_key *mk; |
| 515 | u32 status_flags = 0; |
| 516 | int err; |
| 517 | bool dead; |
| 518 | |
| 519 | if (copy_from_user(&arg, uarg, sizeof(arg))) |
| 520 | return -EFAULT; |
| 521 | |
| 522 | if (!valid_key_spec(&arg.key_spec)) |
| 523 | return -EINVAL; |
| 524 | |
| 525 | if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) |
| 526 | return -EINVAL; |
| 527 | |
| 528 | if (!capable(CAP_SYS_ADMIN)) |
| 529 | return -EACCES; |
| 530 | |
| 531 | /* Find the key being removed. */ |
| 532 | key = fscrypt_find_master_key(sb, &arg.key_spec); |
| 533 | if (IS_ERR(key)) |
| 534 | return PTR_ERR(key); |
| 535 | mk = key->payload.data[0]; |
| 536 | |
| 537 | down_write(&key->sem); |
| 538 | |
| 539 | /* Wipe the secret. */ |
| 540 | dead = false; |
| 541 | if (is_master_key_secret_present(&mk->mk_secret)) { |
| 542 | wipe_master_key_secret(&mk->mk_secret); |
| 543 | dead = refcount_dec_and_test(&mk->mk_refcount); |
| 544 | } |
| 545 | up_write(&key->sem); |
| 546 | if (dead) { |
| 547 | /* |
| 548 | * No inodes reference the key, and we wiped the secret, so the |
| 549 | * key object is free to be removed from the keyring. |
| 550 | */ |
| 551 | key_invalidate(key); |
| 552 | err = 0; |
| 553 | } else { |
| 554 | /* Some inodes still reference this key; try to evict them. */ |
| 555 | err = try_to_lock_encrypted_files(sb, mk); |
| 556 | if (err == -EBUSY) { |
| 557 | status_flags |= |
| 558 | FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY; |
| 559 | err = 0; |
| 560 | } |
| 561 | } |
| 562 | /* |
| 563 | * We return 0 if we successfully did something: wiped the secret, or |
| 564 | * tried locking the files again. Users need to check the informational |
| 565 | * status flags if they care whether the key has been fully removed |
| 566 | * including all files locked. |
| 567 | */ |
| 568 | key_put(key); |
| 569 | if (err == 0) |
| 570 | err = put_user(status_flags, &uarg->removal_status_flags); |
| 571 | return err; |
| 572 | } |
| 573 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_remove_key); |
| 574 | |
Eric Biggers | 5a7e299 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 575 | /* |
| 576 | * Retrieve the status of an fscrypt master encryption key. |
| 577 | * |
| 578 | * We set ->status to indicate whether the key is absent, present, or |
| 579 | * incompletely removed. "Incompletely removed" means that the master key |
| 580 | * secret has been removed, but some files which had been unlocked with it are |
| 581 | * still in use. This field allows applications to easily determine the state |
| 582 | * of an encrypted directory without using a hack such as trying to open a |
| 583 | * regular file in it (which can confuse the "incompletely removed" state with |
| 584 | * absent or present). |
| 585 | * |
| 586 | * For more details, see the "FS_IOC_GET_ENCRYPTION_KEY_STATUS" section of |
| 587 | * Documentation/filesystems/fscrypt.rst. |
| 588 | */ |
| 589 | int fscrypt_ioctl_get_key_status(struct file *filp, void __user *uarg) |
| 590 | { |
| 591 | struct super_block *sb = file_inode(filp)->i_sb; |
| 592 | struct fscrypt_get_key_status_arg arg; |
| 593 | struct key *key; |
| 594 | struct fscrypt_master_key *mk; |
| 595 | int err; |
| 596 | |
| 597 | if (copy_from_user(&arg, uarg, sizeof(arg))) |
| 598 | return -EFAULT; |
| 599 | |
| 600 | if (!valid_key_spec(&arg.key_spec)) |
| 601 | return -EINVAL; |
| 602 | |
| 603 | if (memchr_inv(arg.__reserved, 0, sizeof(arg.__reserved))) |
| 604 | return -EINVAL; |
| 605 | |
| 606 | memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved)); |
| 607 | |
| 608 | key = fscrypt_find_master_key(sb, &arg.key_spec); |
| 609 | if (IS_ERR(key)) { |
| 610 | if (key != ERR_PTR(-ENOKEY)) |
| 611 | return PTR_ERR(key); |
| 612 | arg.status = FSCRYPT_KEY_STATUS_ABSENT; |
| 613 | err = 0; |
| 614 | goto out; |
| 615 | } |
| 616 | mk = key->payload.data[0]; |
| 617 | down_read(&key->sem); |
| 618 | |
| 619 | if (!is_master_key_secret_present(&mk->mk_secret)) { |
| 620 | arg.status = FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED; |
| 621 | err = 0; |
| 622 | goto out_release_key; |
| 623 | } |
| 624 | |
| 625 | arg.status = FSCRYPT_KEY_STATUS_PRESENT; |
| 626 | err = 0; |
| 627 | out_release_key: |
| 628 | up_read(&key->sem); |
| 629 | key_put(key); |
| 630 | out: |
| 631 | if (!err && copy_to_user(uarg, &arg, sizeof(arg))) |
| 632 | err = -EFAULT; |
| 633 | return err; |
| 634 | } |
| 635 | EXPORT_SYMBOL_GPL(fscrypt_ioctl_get_key_status); |
| 636 | |
Eric Biggers | 22d94f4 | 2019-08-04 19:35:46 -0700 | [diff] [blame] | 637 | int __init fscrypt_init_keyring(void) |
| 638 | { |
| 639 | return register_key_type(&key_type_fscrypt); |
| 640 | } |