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