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