hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/crypto/keyring.c
....@@ -18,12 +18,25 @@
1818 * information about these ioctls.
1919 */
2020
21
+#include <asm/unaligned.h>
2122 #include <crypto/skcipher.h>
2223 #include <linux/key-type.h>
2324 #include <linux/random.h>
2425 #include <linux/seq_file.h>
2526
2627 #include "fscrypt_private.h"
28
+
29
+/* The master encryption keys for a filesystem (->s_master_keys) */
30
+struct fscrypt_keyring {
31
+ /*
32
+ * Lock that protects ->key_hashtable. It does *not* protect the
33
+ * fscrypt_master_key structs themselves.
34
+ */
35
+ spinlock_t lock;
36
+
37
+ /* Hash table that maps fscrypt_key_specifier to fscrypt_master_key */
38
+ struct hlist_head key_hashtable[128];
39
+};
2740
2841 static void wipe_master_key_secret(struct fscrypt_master_key_secret *secret)
2942 {
....@@ -38,20 +51,70 @@
3851 memzero_explicit(src, sizeof(*src));
3952 }
4053
41
-static void free_master_key(struct fscrypt_master_key *mk)
54
+static void fscrypt_free_master_key(struct rcu_head *head)
4255 {
56
+ struct fscrypt_master_key *mk =
57
+ container_of(head, struct fscrypt_master_key, mk_rcu_head);
58
+ /*
59
+ * The master key secret and any embedded subkeys should have already
60
+ * been wiped when the last active reference to the fscrypt_master_key
61
+ * struct was dropped; doing it here would be unnecessarily late.
62
+ * Nevertheless, use kfree_sensitive() in case anything was missed.
63
+ */
64
+ kfree_sensitive(mk);
65
+}
66
+
67
+void fscrypt_put_master_key(struct fscrypt_master_key *mk)
68
+{
69
+ if (!refcount_dec_and_test(&mk->mk_struct_refs))
70
+ return;
71
+ /*
72
+ * No structural references left, so free ->mk_users, and also free the
73
+ * fscrypt_master_key struct itself after an RCU grace period ensures
74
+ * that concurrent keyring lookups can no longer find it.
75
+ */
76
+ WARN_ON(refcount_read(&mk->mk_active_refs) != 0);
77
+ key_put(mk->mk_users);
78
+ mk->mk_users = NULL;
79
+ call_rcu(&mk->mk_rcu_head, fscrypt_free_master_key);
80
+}
81
+
82
+void fscrypt_put_master_key_activeref(struct fscrypt_master_key *mk)
83
+{
84
+ struct super_block *sb = mk->mk_sb;
85
+ struct fscrypt_keyring *keyring = sb->s_master_keys;
4386 size_t i;
4487
45
- wipe_master_key_secret(&mk->mk_secret);
88
+ if (!refcount_dec_and_test(&mk->mk_active_refs))
89
+ return;
90
+ /*
91
+ * No active references left, so complete the full removal of this
92
+ * fscrypt_master_key struct by removing it from the keyring and
93
+ * destroying any subkeys embedded in it.
94
+ */
4695
47
- for (i = 0; i <= __FSCRYPT_MODE_MAX; i++) {
96
+ spin_lock(&keyring->lock);
97
+ hlist_del_rcu(&mk->mk_node);
98
+ spin_unlock(&keyring->lock);
99
+
100
+ /*
101
+ * ->mk_active_refs == 0 implies that ->mk_secret is not present and
102
+ * that ->mk_decrypted_inodes is empty.
103
+ */
104
+ WARN_ON(is_master_key_secret_present(&mk->mk_secret));
105
+ WARN_ON(!list_empty(&mk->mk_decrypted_inodes));
106
+
107
+ for (i = 0; i <= FSCRYPT_MODE_MAX; i++) {
48108 fscrypt_destroy_prepared_key(&mk->mk_direct_keys[i]);
49109 fscrypt_destroy_prepared_key(&mk->mk_iv_ino_lblk_64_keys[i]);
50110 fscrypt_destroy_prepared_key(&mk->mk_iv_ino_lblk_32_keys[i]);
51111 }
112
+ memzero_explicit(&mk->mk_ino_hash_key,
113
+ sizeof(mk->mk_ino_hash_key));
114
+ mk->mk_ino_hash_key_initialized = false;
52115
53
- key_put(mk->mk_users);
54
- kzfree(mk);
116
+ /* Drop the structural ref associated with the active refs. */
117
+ fscrypt_put_master_key(mk);
55118 }
56119
57120 static inline bool valid_key_spec(const struct fscrypt_key_specifier *spec)
....@@ -60,44 +123,6 @@
60123 return false;
61124 return master_key_spec_len(spec) != 0;
62125 }
63
-
64
-static int fscrypt_key_instantiate(struct key *key,
65
- struct key_preparsed_payload *prep)
66
-{
67
- key->payload.data[0] = (struct fscrypt_master_key *)prep->data;
68
- return 0;
69
-}
70
-
71
-static void fscrypt_key_destroy(struct key *key)
72
-{
73
- free_master_key(key->payload.data[0]);
74
-}
75
-
76
-static void fscrypt_key_describe(const struct key *key, struct seq_file *m)
77
-{
78
- seq_puts(m, key->description);
79
-
80
- if (key_is_positive(key)) {
81
- const struct fscrypt_master_key *mk = key->payload.data[0];
82
-
83
- if (!is_master_key_secret_present(&mk->mk_secret))
84
- seq_puts(m, ": secret removed");
85
- }
86
-}
87
-
88
-/*
89
- * Type of key in ->s_master_keys. Each key of this type represents a master
90
- * key which has been added to the filesystem. Its payload is a
91
- * 'struct fscrypt_master_key'. The "." prefix in the key type name prevents
92
- * users from adding keys of this type via the keyrings syscalls rather than via
93
- * the intended method of FS_IOC_ADD_ENCRYPTION_KEY.
94
- */
95
-static struct key_type key_type_fscrypt = {
96
- .name = "._fscrypt",
97
- .instantiate = fscrypt_key_instantiate,
98
- .destroy = fscrypt_key_destroy,
99
- .describe = fscrypt_key_describe,
100
-};
101126
102127 static int fscrypt_user_key_instantiate(struct key *key,
103128 struct key_preparsed_payload *prep)
....@@ -131,53 +156,12 @@
131156 .describe = fscrypt_user_key_describe,
132157 };
133158
134
-/* Search ->s_master_keys or ->mk_users */
135
-static struct key *search_fscrypt_keyring(struct key *keyring,
136
- struct key_type *type,
137
- const char *description)
138
-{
139
- /*
140
- * We need to mark the keyring reference as "possessed" so that we
141
- * acquire permission to search it, via the KEY_POS_SEARCH permission.
142
- */
143
- key_ref_t keyref = make_key_ref(keyring, true /* possessed */);
144
-
145
- keyref = keyring_search(keyref, type, description);
146
- if (IS_ERR(keyref)) {
147
- if (PTR_ERR(keyref) == -EAGAIN || /* not found */
148
- PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
149
- keyref = ERR_PTR(-ENOKEY);
150
- return ERR_CAST(keyref);
151
- }
152
- return key_ref_to_ptr(keyref);
153
-}
154
-
155
-#define FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE \
156
- (CONST_STRLEN("fscrypt-") + FIELD_SIZEOF(struct super_block, s_id))
157
-
158
-#define FSCRYPT_MK_DESCRIPTION_SIZE (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + 1)
159
-
160159 #define FSCRYPT_MK_USERS_DESCRIPTION_SIZE \
161160 (CONST_STRLEN("fscrypt-") + 2 * FSCRYPT_KEY_IDENTIFIER_SIZE + \
162161 CONST_STRLEN("-users") + 1)
163162
164163 #define FSCRYPT_MK_USER_DESCRIPTION_SIZE \
165164 (2 * FSCRYPT_KEY_IDENTIFIER_SIZE + CONST_STRLEN(".uid.") + 10 + 1)
166
-
167
-static void format_fs_keyring_description(
168
- char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE],
169
- const struct super_block *sb)
170
-{
171
- sprintf(description, "fscrypt-%s", sb->s_id);
172
-}
173
-
174
-static void format_mk_description(
175
- char description[FSCRYPT_MK_DESCRIPTION_SIZE],
176
- const struct fscrypt_key_specifier *mk_spec)
177
-{
178
- sprintf(description, "%*phN",
179
- master_key_spec_len(mk_spec), (u8 *)&mk_spec->u);
180
-}
181165
182166 static void format_mk_users_keyring_description(
183167 char description[FSCRYPT_MK_USERS_DESCRIPTION_SIZE],
....@@ -199,48 +183,139 @@
199183 /* Create ->s_master_keys if needed. Synchronized by fscrypt_add_key_mutex. */
200184 static int allocate_filesystem_keyring(struct super_block *sb)
201185 {
202
- char description[FSCRYPT_FS_KEYRING_DESCRIPTION_SIZE];
203
- struct key *keyring;
186
+ struct fscrypt_keyring *keyring;
204187
205188 if (sb->s_master_keys)
206189 return 0;
207190
208
- format_fs_keyring_description(description, sb);
209
- keyring = keyring_alloc(description, GLOBAL_ROOT_UID, GLOBAL_ROOT_GID,
210
- current_cred(), KEY_POS_SEARCH |
211
- KEY_USR_SEARCH | KEY_USR_READ | KEY_USR_VIEW,
212
- KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
213
- if (IS_ERR(keyring))
214
- return PTR_ERR(keyring);
215
-
216
- /* Pairs with READ_ONCE() in fscrypt_find_master_key() */
191
+ keyring = kzalloc(sizeof(*keyring), GFP_KERNEL);
192
+ if (!keyring)
193
+ return -ENOMEM;
194
+ spin_lock_init(&keyring->lock);
195
+ /*
196
+ * Pairs with the smp_load_acquire() in fscrypt_find_master_key().
197
+ * I.e., here we publish ->s_master_keys with a RELEASE barrier so that
198
+ * concurrent tasks can ACQUIRE it.
199
+ */
217200 smp_store_release(&sb->s_master_keys, keyring);
218201 return 0;
219202 }
220203
221
-void fscrypt_sb_free(struct super_block *sb)
204
+/*
205
+ * Release all encryption keys that have been added to the filesystem, along
206
+ * with the keyring that contains them.
207
+ *
208
+ * This is called at unmount time. The filesystem's underlying block device(s)
209
+ * are still available at this time; this is important because after user file
210
+ * accesses have been allowed, this function may need to evict keys from the
211
+ * keyslots of an inline crypto engine, which requires the block device(s).
212
+ *
213
+ * This is also called when the super_block is being freed. This is needed to
214
+ * avoid a memory leak if mounting fails after the "test_dummy_encryption"
215
+ * option was processed, as in that case the unmount-time call isn't made.
216
+ */
217
+void fscrypt_destroy_keyring(struct super_block *sb)
222218 {
223
- key_put(sb->s_master_keys);
219
+ struct fscrypt_keyring *keyring = sb->s_master_keys;
220
+ size_t i;
221
+
222
+ if (!keyring)
223
+ return;
224
+
225
+ for (i = 0; i < ARRAY_SIZE(keyring->key_hashtable); i++) {
226
+ struct hlist_head *bucket = &keyring->key_hashtable[i];
227
+ struct fscrypt_master_key *mk;
228
+ struct hlist_node *tmp;
229
+
230
+ hlist_for_each_entry_safe(mk, tmp, bucket, mk_node) {
231
+ /*
232
+ * Since all inodes were already evicted, every key
233
+ * remaining in the keyring should have an empty inode
234
+ * list, and should only still be in the keyring due to
235
+ * the single active ref associated with ->mk_secret.
236
+ * There should be no structural refs beyond the one
237
+ * associated with the active ref.
238
+ */
239
+ WARN_ON(refcount_read(&mk->mk_active_refs) != 1);
240
+ WARN_ON(refcount_read(&mk->mk_struct_refs) != 1);
241
+ WARN_ON(!is_master_key_secret_present(&mk->mk_secret));
242
+ wipe_master_key_secret(&mk->mk_secret);
243
+ fscrypt_put_master_key_activeref(mk);
244
+ }
245
+ }
246
+ kfree_sensitive(keyring);
224247 sb->s_master_keys = NULL;
225248 }
226249
227
-/*
228
- * Find the specified master key in ->s_master_keys.
229
- * Returns ERR_PTR(-ENOKEY) if not found.
230
- */
231
-struct key *fscrypt_find_master_key(struct super_block *sb,
232
- const struct fscrypt_key_specifier *mk_spec)
250
+static struct hlist_head *
251
+fscrypt_mk_hash_bucket(struct fscrypt_keyring *keyring,
252
+ const struct fscrypt_key_specifier *mk_spec)
233253 {
234
- struct key *keyring;
235
- char description[FSCRYPT_MK_DESCRIPTION_SIZE];
254
+ /*
255
+ * Since key specifiers should be "random" values, it is sufficient to
256
+ * use a trivial hash function that just takes the first several bits of
257
+ * the key specifier.
258
+ */
259
+ unsigned long i = get_unaligned((unsigned long *)&mk_spec->u);
236260
237
- /* pairs with smp_store_release() in allocate_filesystem_keyring() */
238
- keyring = READ_ONCE(sb->s_master_keys);
261
+ return &keyring->key_hashtable[i % ARRAY_SIZE(keyring->key_hashtable)];
262
+}
263
+
264
+/*
265
+ * Find the specified master key struct in ->s_master_keys and take a structural
266
+ * ref to it. The structural ref guarantees that the key struct continues to
267
+ * exist, but it does *not* guarantee that ->s_master_keys continues to contain
268
+ * the key struct. The structural ref needs to be dropped by
269
+ * fscrypt_put_master_key(). Returns NULL if the key struct is not found.
270
+ */
271
+struct fscrypt_master_key *
272
+fscrypt_find_master_key(struct super_block *sb,
273
+ const struct fscrypt_key_specifier *mk_spec)
274
+{
275
+ struct fscrypt_keyring *keyring;
276
+ struct hlist_head *bucket;
277
+ struct fscrypt_master_key *mk;
278
+
279
+ /*
280
+ * Pairs with the smp_store_release() in allocate_filesystem_keyring().
281
+ * I.e., another task can publish ->s_master_keys concurrently,
282
+ * executing a RELEASE barrier. We need to use smp_load_acquire() here
283
+ * to safely ACQUIRE the memory the other task published.
284
+ */
285
+ keyring = smp_load_acquire(&sb->s_master_keys);
239286 if (keyring == NULL)
240
- return ERR_PTR(-ENOKEY); /* No keyring yet, so no keys yet. */
287
+ return NULL; /* No keyring yet, so no keys yet. */
241288
242
- format_mk_description(description, mk_spec);
243
- return search_fscrypt_keyring(keyring, &key_type_fscrypt, description);
289
+ bucket = fscrypt_mk_hash_bucket(keyring, mk_spec);
290
+ rcu_read_lock();
291
+ switch (mk_spec->type) {
292
+ case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
293
+ hlist_for_each_entry_rcu(mk, bucket, mk_node) {
294
+ if (mk->mk_spec.type ==
295
+ FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR &&
296
+ memcmp(mk->mk_spec.u.descriptor,
297
+ mk_spec->u.descriptor,
298
+ FSCRYPT_KEY_DESCRIPTOR_SIZE) == 0 &&
299
+ refcount_inc_not_zero(&mk->mk_struct_refs))
300
+ goto out;
301
+ }
302
+ break;
303
+ case FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER:
304
+ hlist_for_each_entry_rcu(mk, bucket, mk_node) {
305
+ if (mk->mk_spec.type ==
306
+ FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER &&
307
+ memcmp(mk->mk_spec.u.identifier,
308
+ mk_spec->u.identifier,
309
+ FSCRYPT_KEY_IDENTIFIER_SIZE) == 0 &&
310
+ refcount_inc_not_zero(&mk->mk_struct_refs))
311
+ goto out;
312
+ }
313
+ break;
314
+ }
315
+ mk = NULL;
316
+out:
317
+ rcu_read_unlock();
318
+ return mk;
244319 }
245320
246321 static int allocate_master_key_users_keyring(struct fscrypt_master_key *mk)
....@@ -268,17 +343,30 @@
268343 static struct key *find_master_key_user(struct fscrypt_master_key *mk)
269344 {
270345 char description[FSCRYPT_MK_USER_DESCRIPTION_SIZE];
346
+ key_ref_t keyref;
271347
272348 format_mk_user_description(description, mk->mk_spec.u.identifier);
273
- return search_fscrypt_keyring(mk->mk_users, &key_type_fscrypt_user,
274
- description);
349
+
350
+ /*
351
+ * We need to mark the keyring reference as "possessed" so that we
352
+ * acquire permission to search it, via the KEY_POS_SEARCH permission.
353
+ */
354
+ keyref = keyring_search(make_key_ref(mk->mk_users, true /*possessed*/),
355
+ &key_type_fscrypt_user, description, false);
356
+ if (IS_ERR(keyref)) {
357
+ if (PTR_ERR(keyref) == -EAGAIN || /* not found */
358
+ PTR_ERR(keyref) == -EKEYREVOKED) /* recently invalidated */
359
+ keyref = ERR_PTR(-ENOKEY);
360
+ return ERR_CAST(keyref);
361
+ }
362
+ return key_ref_to_ptr(keyref);
275363 }
276364
277365 /*
278366 * Give the current user a "key" in ->mk_users. This charges the user's quota
279367 * and marks the master key as added by the current user, so that it cannot be
280
- * removed by another user with the key. Either the master key's key->sem must
281
- * be held for write, or the master key must be still undergoing initialization.
368
+ * removed by another user with the key. Either ->mk_sem must be held for
369
+ * write, or the master key must be still undergoing initialization.
282370 */
283371 static int add_master_key_user(struct fscrypt_master_key *mk)
284372 {
....@@ -300,7 +388,7 @@
300388
301389 /*
302390 * Remove the current user's "key" from ->mk_users.
303
- * The master key's key->sem must be held for write.
391
+ * ->mk_sem must be held for write.
304392 *
305393 * Returns 0 if removed, -ENOKEY if not found, or another -errno code.
306394 */
....@@ -318,64 +406,49 @@
318406 }
319407
320408 /*
321
- * Allocate a new fscrypt_master_key which contains the given secret, set it as
322
- * the payload of a new 'struct key' of type fscrypt, and link the 'struct key'
323
- * into the given keyring. Synchronized by fscrypt_add_key_mutex.
409
+ * Allocate a new fscrypt_master_key, transfer the given secret over to it, and
410
+ * insert it into sb->s_master_keys.
324411 */
325
-static int add_new_master_key(struct fscrypt_master_key_secret *secret,
326
- const struct fscrypt_key_specifier *mk_spec,
327
- struct key *keyring)
412
+static int add_new_master_key(struct super_block *sb,
413
+ struct fscrypt_master_key_secret *secret,
414
+ const struct fscrypt_key_specifier *mk_spec)
328415 {
416
+ struct fscrypt_keyring *keyring = sb->s_master_keys;
329417 struct fscrypt_master_key *mk;
330
- char description[FSCRYPT_MK_DESCRIPTION_SIZE];
331
- struct key *key;
332418 int err;
333419
334420 mk = kzalloc(sizeof(*mk), GFP_KERNEL);
335421 if (!mk)
336422 return -ENOMEM;
337423
424
+ mk->mk_sb = sb;
425
+ init_rwsem(&mk->mk_sem);
426
+ refcount_set(&mk->mk_struct_refs, 1);
338427 mk->mk_spec = *mk_spec;
339428
340
- move_master_key_secret(&mk->mk_secret, secret);
341
- init_rwsem(&mk->mk_secret_sem);
342
-
343
- refcount_set(&mk->mk_refcount, 1); /* secret is present */
344429 INIT_LIST_HEAD(&mk->mk_decrypted_inodes);
345430 spin_lock_init(&mk->mk_decrypted_inodes_lock);
346431
347432 if (mk_spec->type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
348433 err = allocate_master_key_users_keyring(mk);
349434 if (err)
350
- goto out_free_mk;
435
+ goto out_put;
351436 err = add_master_key_user(mk);
352437 if (err)
353
- goto out_free_mk;
438
+ goto out_put;
354439 }
355440
356
- /*
357
- * Note that we don't charge this key to anyone's quota, since when
358
- * ->mk_users is in use those keys are charged instead, and otherwise
359
- * (when ->mk_users isn't in use) only root can add these keys.
360
- */
361
- format_mk_description(description, mk_spec);
362
- key = key_alloc(&key_type_fscrypt, description,
363
- GLOBAL_ROOT_UID, GLOBAL_ROOT_GID, current_cred(),
364
- KEY_POS_SEARCH | KEY_USR_SEARCH | KEY_USR_VIEW,
365
- KEY_ALLOC_NOT_IN_QUOTA, NULL);
366
- if (IS_ERR(key)) {
367
- err = PTR_ERR(key);
368
- goto out_free_mk;
369
- }
370
- err = key_instantiate_and_link(key, mk, sizeof(*mk), keyring, NULL);
371
- key_put(key);
372
- if (err)
373
- goto out_free_mk;
441
+ move_master_key_secret(&mk->mk_secret, secret);
442
+ refcount_set(&mk->mk_active_refs, 1); /* ->mk_secret is present */
374443
444
+ spin_lock(&keyring->lock);
445
+ hlist_add_head_rcu(&mk->mk_node,
446
+ fscrypt_mk_hash_bucket(keyring, mk_spec));
447
+ spin_unlock(&keyring->lock);
375448 return 0;
376449
377
-out_free_mk:
378
- free_master_key(mk);
450
+out_put:
451
+ fscrypt_put_master_key(mk);
379452 return err;
380453 }
381454
....@@ -384,45 +457,34 @@
384457 static int add_existing_master_key(struct fscrypt_master_key *mk,
385458 struct fscrypt_master_key_secret *secret)
386459 {
387
- struct key *mk_user;
388
- bool rekey;
389460 int err;
390461
391462 /*
392463 * If the current user is already in ->mk_users, then there's nothing to
393
- * do. (Not applicable for v1 policy keys, which have NULL ->mk_users.)
464
+ * do. Otherwise, we need to add the user to ->mk_users. (Neither is
465
+ * applicable for v1 policy keys, which have NULL ->mk_users.)
394466 */
395467 if (mk->mk_users) {
396
- mk_user = find_master_key_user(mk);
468
+ struct key *mk_user = find_master_key_user(mk);
469
+
397470 if (mk_user != ERR_PTR(-ENOKEY)) {
398471 if (IS_ERR(mk_user))
399472 return PTR_ERR(mk_user);
400473 key_put(mk_user);
401474 return 0;
402475 }
403
- }
404
-
405
- /* If we'll be re-adding ->mk_secret, try to take the reference. */
406
- rekey = !is_master_key_secret_present(&mk->mk_secret);
407
- if (rekey && !refcount_inc_not_zero(&mk->mk_refcount))
408
- return KEY_DEAD;
409
-
410
- /* Add the current user to ->mk_users, if applicable. */
411
- if (mk->mk_users) {
412476 err = add_master_key_user(mk);
413
- if (err) {
414
- if (rekey && refcount_dec_and_test(&mk->mk_refcount))
415
- return KEY_DEAD;
477
+ if (err)
416478 return err;
417
- }
418479 }
419480
420481 /* Re-add the secret if needed. */
421
- if (rekey) {
422
- down_write(&mk->mk_secret_sem);
482
+ if (!is_master_key_secret_present(&mk->mk_secret)) {
483
+ if (!refcount_inc_not_zero(&mk->mk_active_refs))
484
+ return KEY_DEAD;
423485 move_master_key_secret(&mk->mk_secret, secret);
424
- up_write(&mk->mk_secret_sem);
425486 }
487
+
426488 return 0;
427489 }
428490
....@@ -431,38 +493,36 @@
431493 const struct fscrypt_key_specifier *mk_spec)
432494 {
433495 static DEFINE_MUTEX(fscrypt_add_key_mutex);
434
- struct key *key;
496
+ struct fscrypt_master_key *mk;
435497 int err;
436498
437499 mutex_lock(&fscrypt_add_key_mutex); /* serialize find + link */
438
-retry:
439
- key = fscrypt_find_master_key(sb, mk_spec);
440
- if (IS_ERR(key)) {
441
- err = PTR_ERR(key);
442
- if (err != -ENOKEY)
443
- goto out_unlock;
500
+
501
+ mk = fscrypt_find_master_key(sb, mk_spec);
502
+ if (!mk) {
444503 /* Didn't find the key in ->s_master_keys. Add it. */
445504 err = allocate_filesystem_keyring(sb);
446
- if (err)
447
- goto out_unlock;
448
- err = add_new_master_key(secret, mk_spec, sb->s_master_keys);
505
+ if (!err)
506
+ err = add_new_master_key(sb, secret, mk_spec);
449507 } else {
450508 /*
451509 * Found the key in ->s_master_keys. Re-add the secret if
452510 * needed, and add the user to ->mk_users if needed.
453511 */
454
- down_write(&key->sem);
455
- err = add_existing_master_key(key->payload.data[0], secret);
456
- up_write(&key->sem);
512
+ down_write(&mk->mk_sem);
513
+ err = add_existing_master_key(mk, secret);
514
+ up_write(&mk->mk_sem);
457515 if (err == KEY_DEAD) {
458
- /* Key being removed or needs to be removed */
459
- key_invalidate(key);
460
- key_put(key);
461
- goto retry;
516
+ /*
517
+ * We found a key struct, but it's already been fully
518
+ * removed. Ignore the old struct and add a new one.
519
+ * fscrypt_add_key_mutex means we don't need to worry
520
+ * about concurrent adds.
521
+ */
522
+ err = add_new_master_key(sb, secret, mk_spec);
462523 }
463
- key_put(key);
524
+ fscrypt_put_master_key(mk);
464525 }
465
-out_unlock:
466526 mutex_unlock(&fscrypt_add_key_mutex);
467527 return err;
468528 }
....@@ -538,7 +598,7 @@
538598 static void fscrypt_provisioning_key_free_preparse(
539599 struct key_preparsed_payload *prep)
540600 {
541
- kzfree(prep->payload.data[0]);
601
+ kfree_sensitive(prep->payload.data[0]);
542602 }
543603
544604 static void fscrypt_provisioning_key_describe(const struct key *key,
....@@ -555,7 +615,7 @@
555615
556616 static void fscrypt_provisioning_key_destroy(struct key *key)
557617 {
558
- kzfree(key->payload.data[0]);
618
+ kfree_sensitive(key->payload.data[0]);
559619 }
560620
561621 static struct key_type key_type_fscrypt_provisioning = {
....@@ -756,19 +816,19 @@
756816 const u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE])
757817 {
758818 struct fscrypt_key_specifier mk_spec;
759
- struct key *key, *mk_user;
760819 struct fscrypt_master_key *mk;
820
+ struct key *mk_user;
761821 int err;
762822
763823 mk_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
764824 memcpy(mk_spec.u.identifier, identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
765825
766
- key = fscrypt_find_master_key(sb, &mk_spec);
767
- if (IS_ERR(key)) {
768
- err = PTR_ERR(key);
826
+ mk = fscrypt_find_master_key(sb, &mk_spec);
827
+ if (!mk) {
828
+ err = -ENOKEY;
769829 goto out;
770830 }
771
- mk = key->payload.data[0];
831
+ down_read(&mk->mk_sem);
772832 mk_user = find_master_key_user(mk);
773833 if (IS_ERR(mk_user)) {
774834 err = PTR_ERR(mk_user);
....@@ -776,7 +836,8 @@
776836 key_put(mk_user);
777837 err = 0;
778838 }
779
- key_put(key);
839
+ up_read(&mk->mk_sem);
840
+ fscrypt_put_master_key(mk);
780841 out:
781842 if (err == -ENOKEY && capable(CAP_FOWNER))
782843 err = 0;
....@@ -838,6 +899,7 @@
838899 struct list_head *pos;
839900 size_t busy_count = 0;
840901 unsigned long ino;
902
+ char ino_str[50] = "";
841903
842904 spin_lock(&mk->mk_decrypted_inodes_lock);
843905
....@@ -859,41 +921,23 @@
859921 }
860922 spin_unlock(&mk->mk_decrypted_inodes_lock);
861923
924
+ /* If the inode is currently being created, ino may still be 0. */
925
+ if (ino)
926
+ snprintf(ino_str, sizeof(ino_str), ", including ino %lu", ino);
927
+
862928 fscrypt_warn(NULL,
863
- "%s: %zu inode(s) still busy after removing key with %s %*phN, including ino %lu",
929
+ "%s: %zu inode(s) still busy after removing key with %s %*phN%s",
864930 sb->s_id, busy_count, master_key_spec_type(&mk->mk_spec),
865931 master_key_spec_len(&mk->mk_spec), (u8 *)&mk->mk_spec.u,
866
- ino);
932
+ ino_str);
867933 return -EBUSY;
868934 }
869
-
870
-static BLOCKING_NOTIFIER_HEAD(fscrypt_key_removal_notifiers);
871
-
872
-/*
873
- * Register a function to be executed when the FS_IOC_REMOVE_ENCRYPTION_KEY
874
- * ioctl has removed a key and is about to try evicting inodes.
875
- */
876
-int fscrypt_register_key_removal_notifier(struct notifier_block *nb)
877
-{
878
- return blocking_notifier_chain_register(&fscrypt_key_removal_notifiers,
879
- nb);
880
-}
881
-EXPORT_SYMBOL_GPL(fscrypt_register_key_removal_notifier);
882
-
883
-int fscrypt_unregister_key_removal_notifier(struct notifier_block *nb)
884
-{
885
- return blocking_notifier_chain_unregister(&fscrypt_key_removal_notifiers,
886
- nb);
887
-}
888
-EXPORT_SYMBOL_GPL(fscrypt_unregister_key_removal_notifier);
889935
890936 static int try_to_lock_encrypted_files(struct super_block *sb,
891937 struct fscrypt_master_key *mk)
892938 {
893939 int err1;
894940 int err2;
895
-
896
- blocking_notifier_call_chain(&fscrypt_key_removal_notifiers, 0, NULL);
897941
898942 /*
899943 * An inode can't be evicted while it is dirty or has dirty pages.
....@@ -955,11 +999,10 @@
955999 struct super_block *sb = file_inode(filp)->i_sb;
9561000 struct fscrypt_remove_key_arg __user *uarg = _uarg;
9571001 struct fscrypt_remove_key_arg arg;
958
- struct key *key;
9591002 struct fscrypt_master_key *mk;
9601003 u32 status_flags = 0;
9611004 int err;
962
- bool dead;
1005
+ bool inodes_remain;
9631006
9641007 if (copy_from_user(&arg, uarg, sizeof(arg)))
9651008 return -EFAULT;
....@@ -979,12 +1022,10 @@
9791022 return -EACCES;
9801023
9811024 /* Find the key being removed. */
982
- key = fscrypt_find_master_key(sb, &arg.key_spec);
983
- if (IS_ERR(key))
984
- return PTR_ERR(key);
985
- mk = key->payload.data[0];
986
-
987
- down_write(&key->sem);
1025
+ mk = fscrypt_find_master_key(sb, &arg.key_spec);
1026
+ if (!mk)
1027
+ return -ENOKEY;
1028
+ down_write(&mk->mk_sem);
9881029
9891030 /* If relevant, remove current user's (or all users) claim to the key */
9901031 if (mk->mk_users && mk->mk_users->keys.nr_leaves_on_tree != 0) {
....@@ -993,7 +1034,7 @@
9931034 else
9941035 err = remove_master_key_user(mk);
9951036 if (err) {
996
- up_write(&key->sem);
1037
+ up_write(&mk->mk_sem);
9971038 goto out_put_key;
9981039 }
9991040 if (mk->mk_users->keys.nr_leaves_on_tree != 0) {
....@@ -1005,28 +1046,22 @@
10051046 status_flags |=
10061047 FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS;
10071048 err = 0;
1008
- up_write(&key->sem);
1049
+ up_write(&mk->mk_sem);
10091050 goto out_put_key;
10101051 }
10111052 }
10121053
10131054 /* No user claims remaining. Go ahead and wipe the secret. */
1014
- dead = false;
1055
+ err = -ENOKEY;
10151056 if (is_master_key_secret_present(&mk->mk_secret)) {
1016
- down_write(&mk->mk_secret_sem);
10171057 wipe_master_key_secret(&mk->mk_secret);
1018
- dead = refcount_dec_and_test(&mk->mk_refcount);
1019
- up_write(&mk->mk_secret_sem);
1020
- }
1021
- up_write(&key->sem);
1022
- if (dead) {
1023
- /*
1024
- * No inodes reference the key, and we wiped the secret, so the
1025
- * key object is free to be removed from the keyring.
1026
- */
1027
- key_invalidate(key);
1058
+ fscrypt_put_master_key_activeref(mk);
10281059 err = 0;
1029
- } else {
1060
+ }
1061
+ inodes_remain = refcount_read(&mk->mk_active_refs) > 0;
1062
+ up_write(&mk->mk_sem);
1063
+
1064
+ if (inodes_remain) {
10301065 /* Some inodes still reference this key; try to evict them. */
10311066 err = try_to_lock_encrypted_files(sb, mk);
10321067 if (err == -EBUSY) {
....@@ -1042,7 +1077,7 @@
10421077 * has been fully removed including all files locked.
10431078 */
10441079 out_put_key:
1045
- key_put(key);
1080
+ fscrypt_put_master_key(mk);
10461081 if (err == 0)
10471082 err = put_user(status_flags, &uarg->removal_status_flags);
10481083 return err;
....@@ -1089,7 +1124,6 @@
10891124 {
10901125 struct super_block *sb = file_inode(filp)->i_sb;
10911126 struct fscrypt_get_key_status_arg arg;
1092
- struct key *key;
10931127 struct fscrypt_master_key *mk;
10941128 int err;
10951129
....@@ -1106,19 +1140,18 @@
11061140 arg.user_count = 0;
11071141 memset(arg.__out_reserved, 0, sizeof(arg.__out_reserved));
11081142
1109
- key = fscrypt_find_master_key(sb, &arg.key_spec);
1110
- if (IS_ERR(key)) {
1111
- if (key != ERR_PTR(-ENOKEY))
1112
- return PTR_ERR(key);
1143
+ mk = fscrypt_find_master_key(sb, &arg.key_spec);
1144
+ if (!mk) {
11131145 arg.status = FSCRYPT_KEY_STATUS_ABSENT;
11141146 err = 0;
11151147 goto out;
11161148 }
1117
- mk = key->payload.data[0];
1118
- down_read(&key->sem);
1149
+ down_read(&mk->mk_sem);
11191150
11201151 if (!is_master_key_secret_present(&mk->mk_secret)) {
1121
- arg.status = FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED;
1152
+ arg.status = refcount_read(&mk->mk_active_refs) > 0 ?
1153
+ FSCRYPT_KEY_STATUS_INCOMPLETELY_REMOVED :
1154
+ FSCRYPT_KEY_STATUS_ABSENT /* raced with full removal */;
11221155 err = 0;
11231156 goto out_release_key;
11241157 }
....@@ -1140,8 +1173,8 @@
11401173 }
11411174 err = 0;
11421175 out_release_key:
1143
- up_read(&key->sem);
1144
- key_put(key);
1176
+ up_read(&mk->mk_sem);
1177
+ fscrypt_put_master_key(mk);
11451178 out:
11461179 if (!err && copy_to_user(uarg, &arg, sizeof(arg)))
11471180 err = -EFAULT;
....@@ -1153,13 +1186,9 @@
11531186 {
11541187 int err;
11551188
1156
- err = register_key_type(&key_type_fscrypt);
1157
- if (err)
1158
- return err;
1159
-
11601189 err = register_key_type(&key_type_fscrypt_user);
11611190 if (err)
1162
- goto err_unregister_fscrypt;
1191
+ return err;
11631192
11641193 err = register_key_type(&key_type_fscrypt_provisioning);
11651194 if (err)
....@@ -1169,7 +1198,5 @@
11691198
11701199 err_unregister_fscrypt_user:
11711200 unregister_key_type(&key_type_fscrypt_user);
1172
-err_unregister_fscrypt:
1173
- unregister_key_type(&key_type_fscrypt);
11741201 return err;
11751202 }