]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/dsl_crypt.c
deadlock between mm_sem and tx assign in zfs_write() and page fault
[mirror_zfs.git] / module / zfs / dsl_crypt.c
CommitLineData
b5256303
TC
1/*
2 * CDDL HEADER START
3 *
4 * This file and its contents are supplied under the terms of the
5 * Common Development and Distribution License ("CDDL"), version 1.0.
6 * You may only use this file in accordance with the terms of version
7 * 1.0 of the CDDL.
8 *
9 * A full copy of the text of the CDDL should have accompanied this
10 * source. A copy of the CDDL is also available via the Internet at
11 * http://www.illumos.org/license/CDDL.
12 *
13 * CDDL HEADER END
14 */
15
16/*
17 * Copyright (c) 2017, Datto, Inc. All rights reserved.
18 */
19
20#include <sys/dsl_crypt.h>
21#include <sys/dsl_pool.h>
22#include <sys/zap.h>
23#include <sys/zil.h>
24#include <sys/dsl_dir.h>
25#include <sys/dsl_prop.h>
26#include <sys/spa_impl.h>
27#include <sys/dmu_objset.h>
28#include <sys/zvol.h>
29
30/*
31 * This file's primary purpose is for managing master encryption keys in
32 * memory and on disk. For more info on how these keys are used, see the
33 * block comment in zio_crypt.c.
34 *
35 * All master keys are stored encrypted on disk in the form of the DSL
36 * Crypto Key ZAP object. The binary key data in this object is always
37 * randomly generated and is encrypted with the user's wrapping key. This
38 * layer of indirection allows the user to change their key without
39 * needing to re-encrypt the entire dataset. The ZAP also holds on to the
40 * (non-encrypted) encryption algorithm identifier, IV, and MAC needed to
41 * safely decrypt the master key. For more info on the user's key see the
42 * block comment in libzfs_crypto.c
43 *
44 * In-memory encryption keys are managed through the spa_keystore. The
45 * keystore consists of 3 AVL trees, which are as follows:
46 *
47 * The Wrapping Key Tree:
48 * The wrapping key (wkey) tree stores the user's keys that are fed into the
49 * kernel through 'zfs load-key' and related commands. Datasets inherit their
50 * parent's wkey by default, so these structures are refcounted. The wrapping
51 * keys remain in memory until they are explicitly unloaded (with
52 * "zfs unload-key"). Unloading is only possible when no datasets are using
53 * them (refcount=0).
54 *
55 * The DSL Crypto Key Tree:
56 * The DSL Crypto Keys (DCK) are the in-memory representation of decrypted
57 * master keys. They are used by the functions in zio_crypt.c to perform
58 * encryption, decryption, and authentication. Snapshots and clones of a given
59 * dataset will share a DSL Crypto Key, so they are also refcounted. Once the
60 * refcount on a key hits zero, it is immediately zeroed out and freed.
61 *
62 * The Crypto Key Mapping Tree:
63 * The zio layer needs to lookup master keys by their dataset object id. Since
64 * the DSL Crypto Keys can belong to multiple datasets, we maintain a tree of
65 * dsl_key_mapping_t's which essentially just map the dataset object id to its
66 * appropriate DSL Crypto Key. The management for creating and destroying these
67 * mappings hooks into the code for owning and disowning datasets. Usually,
68 * there will only be one active dataset owner, but there are times
69 * (particularly during dataset creation and destruction) when this may not be
70 * true or the dataset may not be initialized enough to own. As a result, this
71 * object is also refcounted.
72 */
73
74static void
75dsl_wrapping_key_hold(dsl_wrapping_key_t *wkey, void *tag)
76{
c13060e4 77 (void) zfs_refcount_add(&wkey->wk_refcnt, tag);
b5256303
TC
78}
79
80static void
81dsl_wrapping_key_rele(dsl_wrapping_key_t *wkey, void *tag)
82{
424fd7c3 83 (void) zfs_refcount_remove(&wkey->wk_refcnt, tag);
b5256303
TC
84}
85
86static void
87dsl_wrapping_key_free(dsl_wrapping_key_t *wkey)
88{
424fd7c3 89 ASSERT0(zfs_refcount_count(&wkey->wk_refcnt));
b5256303
TC
90
91 if (wkey->wk_key.ck_data) {
92 bzero(wkey->wk_key.ck_data,
4807c0ba 93 CRYPTO_BITS2BYTES(wkey->wk_key.ck_length));
b5256303 94 kmem_free(wkey->wk_key.ck_data,
4807c0ba 95 CRYPTO_BITS2BYTES(wkey->wk_key.ck_length));
b5256303
TC
96 }
97
424fd7c3 98 zfs_refcount_destroy(&wkey->wk_refcnt);
b5256303
TC
99 kmem_free(wkey, sizeof (dsl_wrapping_key_t));
100}
101
102static int
103dsl_wrapping_key_create(uint8_t *wkeydata, zfs_keyformat_t keyformat,
104 uint64_t salt, uint64_t iters, dsl_wrapping_key_t **wkey_out)
105{
106 int ret;
107 dsl_wrapping_key_t *wkey;
108
109 /* allocate the wrapping key */
110 wkey = kmem_alloc(sizeof (dsl_wrapping_key_t), KM_SLEEP);
111 if (!wkey)
112 return (SET_ERROR(ENOMEM));
113
114 /* allocate and initialize the underlying crypto key */
115 wkey->wk_key.ck_data = kmem_alloc(WRAPPING_KEY_LEN, KM_SLEEP);
116 if (!wkey->wk_key.ck_data) {
117 ret = ENOMEM;
118 goto error;
119 }
120
121 wkey->wk_key.ck_format = CRYPTO_KEY_RAW;
4807c0ba 122 wkey->wk_key.ck_length = CRYPTO_BYTES2BITS(WRAPPING_KEY_LEN);
b5256303
TC
123 bcopy(wkeydata, wkey->wk_key.ck_data, WRAPPING_KEY_LEN);
124
125 /* initialize the rest of the struct */
424fd7c3 126 zfs_refcount_create(&wkey->wk_refcnt);
b5256303
TC
127 wkey->wk_keyformat = keyformat;
128 wkey->wk_salt = salt;
129 wkey->wk_iters = iters;
130
131 *wkey_out = wkey;
132 return (0);
133
134error:
135 dsl_wrapping_key_free(wkey);
136
137 *wkey_out = NULL;
138 return (ret);
139}
140
141int
142dsl_crypto_params_create_nvlist(dcp_cmd_t cmd, nvlist_t *props,
143 nvlist_t *crypto_args, dsl_crypto_params_t **dcp_out)
144{
145 int ret;
146 uint64_t crypt = ZIO_CRYPT_INHERIT;
147 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
148 uint64_t salt = 0, iters = 0;
149 dsl_crypto_params_t *dcp = NULL;
150 dsl_wrapping_key_t *wkey = NULL;
151 uint8_t *wkeydata = NULL;
152 uint_t wkeydata_len = 0;
153 char *keylocation = NULL;
154
155 dcp = kmem_zalloc(sizeof (dsl_crypto_params_t), KM_SLEEP);
156 if (!dcp) {
157 ret = SET_ERROR(ENOMEM);
158 goto error;
159 }
160
161 dcp->cp_cmd = cmd;
162
163 /* get relevant arguments from the nvlists */
164 if (props != NULL) {
165 (void) nvlist_lookup_uint64(props,
166 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &crypt);
167 (void) nvlist_lookup_uint64(props,
168 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
169 (void) nvlist_lookup_string(props,
170 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
171 (void) nvlist_lookup_uint64(props,
172 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), &salt);
173 (void) nvlist_lookup_uint64(props,
174 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
175
176 dcp->cp_crypt = crypt;
177 }
178
179 if (crypto_args != NULL) {
180 (void) nvlist_lookup_uint8_array(crypto_args, "wkeydata",
181 &wkeydata, &wkeydata_len);
182 }
183
184 /* check for valid command */
185 if (dcp->cp_cmd >= DCP_CMD_MAX) {
186 ret = SET_ERROR(EINVAL);
187 goto error;
188 } else {
189 dcp->cp_cmd = cmd;
190 }
191
192 /* check for valid crypt */
193 if (dcp->cp_crypt >= ZIO_CRYPT_FUNCTIONS) {
194 ret = SET_ERROR(EINVAL);
195 goto error;
196 } else {
197 dcp->cp_crypt = crypt;
198 }
199
200 /* check for valid keyformat */
201 if (keyformat >= ZFS_KEYFORMAT_FORMATS) {
202 ret = SET_ERROR(EINVAL);
203 goto error;
204 }
205
206 /* check for a valid keylocation (of any kind) and copy it in */
207 if (keylocation != NULL) {
208 if (!zfs_prop_valid_keylocation(keylocation, B_FALSE)) {
209 ret = SET_ERROR(EINVAL);
210 goto error;
211 }
212
213 dcp->cp_keylocation = spa_strdup(keylocation);
214 }
215
216 /* check wrapping key length, if given */
217 if (wkeydata != NULL && wkeydata_len != WRAPPING_KEY_LEN) {
218 ret = SET_ERROR(EINVAL);
219 goto error;
220 }
221
222 /* if the user asked for the deault crypt, determine that now */
223 if (dcp->cp_crypt == ZIO_CRYPT_ON)
224 dcp->cp_crypt = ZIO_CRYPT_ON_VALUE;
225
226 /* create the wrapping key from the raw data */
227 if (wkeydata != NULL) {
228 /* create the wrapping key with the verified parameters */
229 ret = dsl_wrapping_key_create(wkeydata, keyformat, salt,
230 iters, &wkey);
231 if (ret != 0)
232 goto error;
233
234 dcp->cp_wkey = wkey;
235 }
236
237 /*
238 * Remove the encryption properties from the nvlist since they are not
239 * maintained through the DSL.
240 */
241 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_ENCRYPTION));
242 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_KEYFORMAT));
243 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT));
244 (void) nvlist_remove_all(props,
245 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS));
246
247 *dcp_out = dcp;
248
249 return (0);
250
251error:
252 if (wkey != NULL)
253 dsl_wrapping_key_free(wkey);
254 if (dcp != NULL)
255 kmem_free(dcp, sizeof (dsl_crypto_params_t));
256
257 *dcp_out = NULL;
258 return (ret);
259}
260
261void
262dsl_crypto_params_free(dsl_crypto_params_t *dcp, boolean_t unload)
263{
264 if (dcp == NULL)
265 return;
266
267 if (dcp->cp_keylocation != NULL)
268 spa_strfree(dcp->cp_keylocation);
269 if (unload && dcp->cp_wkey != NULL)
270 dsl_wrapping_key_free(dcp->cp_wkey);
271
272 kmem_free(dcp, sizeof (dsl_crypto_params_t));
273}
274
275static int
276spa_crypto_key_compare(const void *a, const void *b)
277{
278 const dsl_crypto_key_t *dcka = a;
279 const dsl_crypto_key_t *dckb = b;
280
281 if (dcka->dck_obj < dckb->dck_obj)
282 return (-1);
283 if (dcka->dck_obj > dckb->dck_obj)
284 return (1);
285 return (0);
286}
287
288static int
289spa_key_mapping_compare(const void *a, const void *b)
290{
291 const dsl_key_mapping_t *kma = a;
292 const dsl_key_mapping_t *kmb = b;
293
294 if (kma->km_dsobj < kmb->km_dsobj)
295 return (-1);
296 if (kma->km_dsobj > kmb->km_dsobj)
297 return (1);
298 return (0);
299}
300
301static int
302spa_wkey_compare(const void *a, const void *b)
303{
304 const dsl_wrapping_key_t *wka = a;
305 const dsl_wrapping_key_t *wkb = b;
306
307 if (wka->wk_ddobj < wkb->wk_ddobj)
308 return (-1);
309 if (wka->wk_ddobj > wkb->wk_ddobj)
310 return (1);
311 return (0);
312}
313
314void
315spa_keystore_init(spa_keystore_t *sk)
316{
317 rw_init(&sk->sk_dk_lock, NULL, RW_DEFAULT, NULL);
318 rw_init(&sk->sk_km_lock, NULL, RW_DEFAULT, NULL);
319 rw_init(&sk->sk_wkeys_lock, NULL, RW_DEFAULT, NULL);
320 avl_create(&sk->sk_dsl_keys, spa_crypto_key_compare,
321 sizeof (dsl_crypto_key_t),
322 offsetof(dsl_crypto_key_t, dck_avl_link));
323 avl_create(&sk->sk_key_mappings, spa_key_mapping_compare,
324 sizeof (dsl_key_mapping_t),
325 offsetof(dsl_key_mapping_t, km_avl_link));
326 avl_create(&sk->sk_wkeys, spa_wkey_compare, sizeof (dsl_wrapping_key_t),
327 offsetof(dsl_wrapping_key_t, wk_avl_link));
328}
329
330void
331spa_keystore_fini(spa_keystore_t *sk)
332{
333 dsl_wrapping_key_t *wkey;
334 void *cookie = NULL;
335
336 ASSERT(avl_is_empty(&sk->sk_dsl_keys));
337 ASSERT(avl_is_empty(&sk->sk_key_mappings));
338
339 while ((wkey = avl_destroy_nodes(&sk->sk_wkeys, &cookie)) != NULL)
340 dsl_wrapping_key_free(wkey);
341
342 avl_destroy(&sk->sk_wkeys);
343 avl_destroy(&sk->sk_key_mappings);
344 avl_destroy(&sk->sk_dsl_keys);
345 rw_destroy(&sk->sk_wkeys_lock);
346 rw_destroy(&sk->sk_km_lock);
347 rw_destroy(&sk->sk_dk_lock);
348}
349
ae76f45c 350static int
b5256303
TC
351dsl_dir_get_encryption_root_ddobj(dsl_dir_t *dd, uint64_t *rddobj)
352{
353 if (dd->dd_crypto_obj == 0)
354 return (SET_ERROR(ENOENT));
355
356 return (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
357 DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1, rddobj));
358}
359
ae76f45c
TC
360int
361dsl_dir_get_encryption_version(dsl_dir_t *dd, uint64_t *version)
362{
363 *version = 0;
364
365 if (dd->dd_crypto_obj == 0)
366 return (SET_ERROR(ENOENT));
367
368 /* version 0 is implied by ENOENT */
369 (void) zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
370 DSL_CRYPTO_KEY_VERSION, 8, 1, version);
371
372 return (0);
373}
374
375boolean_t
376dsl_dir_incompatible_encryption_version(dsl_dir_t *dd)
377{
378 int ret;
379 uint64_t version = 0;
380
381 ret = dsl_dir_get_encryption_version(dd, &version);
382 if (ret != 0)
383 return (B_FALSE);
384
385 return (version != ZIO_CRYPT_KEY_CURRENT_VERSION);
386}
387
b5256303
TC
388static int
389spa_keystore_wkey_hold_ddobj_impl(spa_t *spa, uint64_t ddobj,
390 void *tag, dsl_wrapping_key_t **wkey_out)
391{
392 int ret;
393 dsl_wrapping_key_t search_wkey;
394 dsl_wrapping_key_t *found_wkey;
395
396 ASSERT(RW_LOCK_HELD(&spa->spa_keystore.sk_wkeys_lock));
397
398 /* init the search wrapping key */
399 search_wkey.wk_ddobj = ddobj;
400
401 /* lookup the wrapping key */
402 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, &search_wkey, NULL);
403 if (!found_wkey) {
404 ret = SET_ERROR(ENOENT);
405 goto error;
406 }
407
408 /* increment the refcount */
409 dsl_wrapping_key_hold(found_wkey, tag);
410
411 *wkey_out = found_wkey;
412 return (0);
413
414error:
415 *wkey_out = NULL;
416 return (ret);
417}
418
419static int
420spa_keystore_wkey_hold_dd(spa_t *spa, dsl_dir_t *dd, void *tag,
421 dsl_wrapping_key_t **wkey_out)
422{
423 int ret;
424 dsl_wrapping_key_t *wkey;
425 uint64_t rddobj;
426 boolean_t locked = B_FALSE;
427
428 if (!RW_WRITE_HELD(&spa->spa_keystore.sk_wkeys_lock)) {
429 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_READER);
430 locked = B_TRUE;
431 }
432
433 /* get the ddobj that the keylocation property was inherited from */
434 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
435 if (ret != 0)
436 goto error;
437
438 /* lookup the wkey in the avl tree */
439 ret = spa_keystore_wkey_hold_ddobj_impl(spa, rddobj, tag, &wkey);
440 if (ret != 0)
441 goto error;
442
443 /* unlock the wkey tree if we locked it */
444 if (locked)
445 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
446
447 *wkey_out = wkey;
448 return (0);
449
450error:
451 if (locked)
452 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
453
454 *wkey_out = NULL;
455 return (ret);
456}
457
458int
459dsl_crypto_can_set_keylocation(const char *dsname, const char *keylocation)
460{
461 int ret = 0;
462 dsl_dir_t *dd = NULL;
463 dsl_pool_t *dp = NULL;
b5256303
TC
464 uint64_t rddobj;
465
466 /* hold the dsl dir */
467 ret = dsl_pool_hold(dsname, FTAG, &dp);
468 if (ret != 0)
469 goto out;
470
471 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
fc1ecd16
DB
472 if (ret != 0) {
473 dd = NULL;
b5256303 474 goto out;
fc1ecd16 475 }
b5256303
TC
476
477 /* if dd is not encrypted, the value may only be "none" */
478 if (dd->dd_crypto_obj == 0) {
479 if (strcmp(keylocation, "none") != 0) {
480 ret = SET_ERROR(EACCES);
481 goto out;
482 }
483
484 ret = 0;
485 goto out;
486 }
487
488 /* check for a valid keylocation for encrypted datasets */
489 if (!zfs_prop_valid_keylocation(keylocation, B_TRUE)) {
490 ret = SET_ERROR(EINVAL);
491 goto out;
492 }
493
494 /* check that this is an encryption root */
495 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
496 if (ret != 0)
497 goto out;
498
499 if (rddobj != dd->dd_object) {
500 ret = SET_ERROR(EACCES);
501 goto out;
502 }
503
b5256303
TC
504 dsl_dir_rele(dd, FTAG);
505 dsl_pool_rele(dp, FTAG);
506
507 return (0);
508
509out:
b5256303
TC
510 if (dd != NULL)
511 dsl_dir_rele(dd, FTAG);
512 if (dp != NULL)
513 dsl_pool_rele(dp, FTAG);
514
515 return (ret);
516}
517
518static void
519dsl_crypto_key_free(dsl_crypto_key_t *dck)
520{
424fd7c3 521 ASSERT(zfs_refcount_count(&dck->dck_holds) == 0);
b5256303
TC
522
523 /* destroy the zio_crypt_key_t */
524 zio_crypt_key_destroy(&dck->dck_key);
525
526 /* free the refcount, wrapping key, and lock */
424fd7c3 527 zfs_refcount_destroy(&dck->dck_holds);
b5256303
TC
528 if (dck->dck_wkey)
529 dsl_wrapping_key_rele(dck->dck_wkey, dck);
530
531 /* free the key */
532 kmem_free(dck, sizeof (dsl_crypto_key_t));
533}
534
535static void
536dsl_crypto_key_rele(dsl_crypto_key_t *dck, void *tag)
537{
424fd7c3 538 if (zfs_refcount_remove(&dck->dck_holds, tag) == 0)
b5256303
TC
539 dsl_crypto_key_free(dck);
540}
541
542static int
543dsl_crypto_key_open(objset_t *mos, dsl_wrapping_key_t *wkey,
544 uint64_t dckobj, void *tag, dsl_crypto_key_t **dck_out)
545{
546 int ret;
ae76f45c 547 uint64_t crypt = 0, guid = 0, version = 0;
b5256303
TC
548 uint8_t raw_keydata[MASTER_KEY_MAX_LEN];
549 uint8_t raw_hmac_keydata[SHA512_HMAC_KEYLEN];
550 uint8_t iv[WRAPPING_IV_LEN];
551 uint8_t mac[WRAPPING_MAC_LEN];
552 dsl_crypto_key_t *dck;
553
554 /* allocate and initialize the key */
555 dck = kmem_zalloc(sizeof (dsl_crypto_key_t), KM_SLEEP);
556 if (!dck)
557 return (SET_ERROR(ENOMEM));
558
559 /* fetch all of the values we need from the ZAP */
560 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
561 &crypt);
562 if (ret != 0)
563 goto error;
564
565 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1, &guid);
566 if (ret != 0)
567 goto error;
568
569 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
570 MASTER_KEY_MAX_LEN, raw_keydata);
571 if (ret != 0)
572 goto error;
573
574 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
575 SHA512_HMAC_KEYLEN, raw_hmac_keydata);
576 if (ret != 0)
577 goto error;
578
579 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
580 iv);
581 if (ret != 0)
582 goto error;
583
584 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
585 mac);
586 if (ret != 0)
587 goto error;
588
ae76f45c
TC
589 /* the initial on-disk format for encryption did not have a version */
590 (void) zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_VERSION, 8, 1, &version);
591
b5256303
TC
592 /*
593 * Unwrap the keys. If there is an error return EACCES to indicate
594 * an authentication failure.
595 */
ae76f45c
TC
596 ret = zio_crypt_key_unwrap(&wkey->wk_key, crypt, version, guid,
597 raw_keydata, raw_hmac_keydata, iv, mac, &dck->dck_key);
b5256303
TC
598 if (ret != 0) {
599 ret = SET_ERROR(EACCES);
600 goto error;
601 }
602
603 /* finish initializing the dsl_crypto_key_t */
424fd7c3 604 zfs_refcount_create(&dck->dck_holds);
b5256303
TC
605 dsl_wrapping_key_hold(wkey, dck);
606 dck->dck_wkey = wkey;
607 dck->dck_obj = dckobj;
c13060e4 608 zfs_refcount_add(&dck->dck_holds, tag);
b5256303
TC
609
610 *dck_out = dck;
611 return (0);
612
613error:
614 if (dck != NULL) {
615 bzero(dck, sizeof (dsl_crypto_key_t));
616 kmem_free(dck, sizeof (dsl_crypto_key_t));
617 }
618
619 *dck_out = NULL;
620 return (ret);
621}
622
623static int
624spa_keystore_dsl_key_hold_impl(spa_t *spa, uint64_t dckobj, void *tag,
625 dsl_crypto_key_t **dck_out)
626{
627 int ret;
628 dsl_crypto_key_t search_dck;
629 dsl_crypto_key_t *found_dck;
630
631 ASSERT(RW_LOCK_HELD(&spa->spa_keystore.sk_dk_lock));
632
633 /* init the search key */
634 search_dck.dck_obj = dckobj;
635
636 /* find the matching key in the keystore */
637 found_dck = avl_find(&spa->spa_keystore.sk_dsl_keys, &search_dck, NULL);
638 if (!found_dck) {
639 ret = SET_ERROR(ENOENT);
640 goto error;
641 }
642
643 /* increment the refcount */
c13060e4 644 zfs_refcount_add(&found_dck->dck_holds, tag);
b5256303
TC
645
646 *dck_out = found_dck;
647 return (0);
648
649error:
650 *dck_out = NULL;
651 return (ret);
652}
653
654static int
655spa_keystore_dsl_key_hold_dd(spa_t *spa, dsl_dir_t *dd, void *tag,
656 dsl_crypto_key_t **dck_out)
657{
658 int ret;
659 avl_index_t where;
0d23f5e2 660 dsl_crypto_key_t *dck_io = NULL, *dck_ks = NULL;
b5256303
TC
661 dsl_wrapping_key_t *wkey = NULL;
662 uint64_t dckobj = dd->dd_crypto_obj;
663
0d23f5e2
BB
664 /* Lookup the key in the tree of currently loaded keys */
665 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_READER);
666 ret = spa_keystore_dsl_key_hold_impl(spa, dckobj, tag, &dck_ks);
667 rw_exit(&spa->spa_keystore.sk_dk_lock);
668 if (ret == 0) {
669 *dck_out = dck_ks;
b5256303
TC
670 return (0);
671 }
672
0d23f5e2 673 /* Lookup the wrapping key from the keystore */
b5256303
TC
674 ret = spa_keystore_wkey_hold_dd(spa, dd, FTAG, &wkey);
675 if (ret != 0) {
0d23f5e2
BB
676 *dck_out = NULL;
677 return (SET_ERROR(EACCES));
b5256303
TC
678 }
679
0d23f5e2 680 /* Read the key from disk */
b5256303 681 ret = dsl_crypto_key_open(spa->spa_meta_objset, wkey, dckobj,
0d23f5e2
BB
682 tag, &dck_io);
683 if (ret != 0) {
684 dsl_wrapping_key_rele(wkey, FTAG);
685 *dck_out = NULL;
686 return (ret);
687 }
b5256303
TC
688
689 /*
0d23f5e2
BB
690 * Add the key to the keystore. It may already exist if it was
691 * added while performing the read from disk. In this case discard
692 * it and return the key from the keystore.
b5256303 693 */
0d23f5e2
BB
694 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_WRITER);
695 ret = spa_keystore_dsl_key_hold_impl(spa, dckobj, tag, &dck_ks);
696 if (ret != 0) {
697 avl_find(&spa->spa_keystore.sk_dsl_keys, dck_io, &where);
698 avl_insert(&spa->spa_keystore.sk_dsl_keys, dck_io, where);
699 *dck_out = dck_io;
700 } else {
701 dsl_crypto_key_free(dck_io);
702 *dck_out = dck_ks;
703 }
b5256303 704
0d23f5e2 705 /* Release the wrapping key (the dsl key now has a reference to it) */
b5256303 706 dsl_wrapping_key_rele(wkey, FTAG);
b5256303
TC
707 rw_exit(&spa->spa_keystore.sk_dk_lock);
708
b5256303 709 return (0);
b5256303
TC
710}
711
712void
713spa_keystore_dsl_key_rele(spa_t *spa, dsl_crypto_key_t *dck, void *tag)
714{
715 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_WRITER);
716
424fd7c3 717 if (zfs_refcount_remove(&dck->dck_holds, tag) == 0) {
b5256303
TC
718 avl_remove(&spa->spa_keystore.sk_dsl_keys, dck);
719 dsl_crypto_key_free(dck);
720 }
721
722 rw_exit(&spa->spa_keystore.sk_dk_lock);
723}
724
725int
726spa_keystore_load_wkey_impl(spa_t *spa, dsl_wrapping_key_t *wkey)
727{
728 int ret;
729 avl_index_t where;
730 dsl_wrapping_key_t *found_wkey;
731
732 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
733
734 /* insert the wrapping key into the keystore */
735 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, wkey, &where);
736 if (found_wkey != NULL) {
737 ret = SET_ERROR(EEXIST);
738 goto error_unlock;
739 }
740 avl_insert(&spa->spa_keystore.sk_wkeys, wkey, where);
741
742 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
743
744 return (0);
745
746error_unlock:
747 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
748 return (ret);
749}
750
751int
752spa_keystore_load_wkey(const char *dsname, dsl_crypto_params_t *dcp,
753 boolean_t noop)
754{
755 int ret;
756 dsl_dir_t *dd = NULL;
757 dsl_crypto_key_t *dck = NULL;
758 dsl_wrapping_key_t *wkey = dcp->cp_wkey;
759 dsl_pool_t *dp = NULL;
2637dda8 760 uint64_t keyformat, salt, iters;
b5256303
TC
761
762 /*
763 * We don't validate the wrapping key's keyformat, salt, or iters
764 * since they will never be needed after the DCK has been wrapped.
765 */
766 if (dcp->cp_wkey == NULL ||
767 dcp->cp_cmd != DCP_CMD_NONE ||
768 dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
769 dcp->cp_keylocation != NULL)
770 return (SET_ERROR(EINVAL));
771
772 ret = dsl_pool_hold(dsname, FTAG, &dp);
773 if (ret != 0)
774 goto error;
775
776 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
777 ret = (SET_ERROR(ENOTSUP));
778 goto error;
779 }
780
781 /* hold the dsl dir */
782 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
fc1ecd16
DB
783 if (ret != 0) {
784 dd = NULL;
b5256303 785 goto error;
fc1ecd16 786 }
b5256303
TC
787
788 /* initialize the wkey's ddobj */
789 wkey->wk_ddobj = dd->dd_object;
790
791 /* verify that the wkey is correct by opening its dsl key */
792 ret = dsl_crypto_key_open(dp->dp_meta_objset, wkey,
793 dd->dd_crypto_obj, FTAG, &dck);
794 if (ret != 0)
795 goto error;
796
2637dda8
TC
797 /* initialize the wkey encryption parameters from the DSL Crypto Key */
798 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
799 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &keyformat);
800 if (ret != 0)
801 goto error;
802
803 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
804 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &salt);
805 if (ret != 0)
806 goto error;
807
808 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
809 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &iters);
810 if (ret != 0)
811 goto error;
812
813 ASSERT3U(keyformat, <, ZFS_KEYFORMAT_FORMATS);
814 ASSERT3U(keyformat, !=, ZFS_KEYFORMAT_NONE);
815 IMPLY(keyformat == ZFS_KEYFORMAT_PASSPHRASE, iters != 0);
816 IMPLY(keyformat == ZFS_KEYFORMAT_PASSPHRASE, salt != 0);
817 IMPLY(keyformat != ZFS_KEYFORMAT_PASSPHRASE, iters == 0);
818 IMPLY(keyformat != ZFS_KEYFORMAT_PASSPHRASE, salt == 0);
819
820 wkey->wk_keyformat = keyformat;
821 wkey->wk_salt = salt;
822 wkey->wk_iters = iters;
823
b5256303 824 /*
2637dda8
TC
825 * At this point we have verified the wkey and confirmed that it can
826 * be used to decrypt a DSL Crypto Key. We can simply cleanup and
b5256303
TC
827 * return if this is all the user wanted to do.
828 */
829 if (noop)
830 goto error;
831
832 /* insert the wrapping key into the keystore */
833 ret = spa_keystore_load_wkey_impl(dp->dp_spa, wkey);
834 if (ret != 0)
835 goto error;
836
837 dsl_crypto_key_rele(dck, FTAG);
838 dsl_dir_rele(dd, FTAG);
839 dsl_pool_rele(dp, FTAG);
840
841 /* create any zvols under this ds */
842 zvol_create_minors(dp->dp_spa, dsname, B_TRUE);
843
844 return (0);
845
846error:
847 if (dck != NULL)
848 dsl_crypto_key_rele(dck, FTAG);
849 if (dd != NULL)
850 dsl_dir_rele(dd, FTAG);
851 if (dp != NULL)
852 dsl_pool_rele(dp, FTAG);
853
854 return (ret);
855}
856
857int
858spa_keystore_unload_wkey_impl(spa_t *spa, uint64_t ddobj)
859{
860 int ret;
861 dsl_wrapping_key_t search_wkey;
862 dsl_wrapping_key_t *found_wkey;
863
864 /* init the search wrapping key */
865 search_wkey.wk_ddobj = ddobj;
866
867 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
868
869 /* remove the wrapping key from the keystore */
870 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys,
871 &search_wkey, NULL);
872 if (!found_wkey) {
85ce3f4f 873 ret = SET_ERROR(EACCES);
b5256303 874 goto error_unlock;
424fd7c3 875 } else if (zfs_refcount_count(&found_wkey->wk_refcnt) != 0) {
b5256303
TC
876 ret = SET_ERROR(EBUSY);
877 goto error_unlock;
878 }
879 avl_remove(&spa->spa_keystore.sk_wkeys, found_wkey);
880
881 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
882
883 /* free the wrapping key */
884 dsl_wrapping_key_free(found_wkey);
885
886 return (0);
887
888error_unlock:
889 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
890 return (ret);
891}
892
893int
894spa_keystore_unload_wkey(const char *dsname)
895{
896 int ret = 0;
897 dsl_dir_t *dd = NULL;
898 dsl_pool_t *dp = NULL;
52ce99dd
TC
899 spa_t *spa = NULL;
900
901 ret = spa_open(dsname, &spa, FTAG);
902 if (ret != 0)
903 return (ret);
904
905 /*
906 * Wait for any outstanding txg IO to complete, releasing any
907 * remaining references on the wkey.
908 */
909 if (spa_mode(spa) != FREAD)
910 txg_wait_synced(spa->spa_dsl_pool, 0);
911
912 spa_close(spa, FTAG);
b5256303
TC
913
914 /* hold the dsl dir */
915 ret = dsl_pool_hold(dsname, FTAG, &dp);
916 if (ret != 0)
917 goto error;
918
919 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
920 ret = (SET_ERROR(ENOTSUP));
921 goto error;
922 }
923
924 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
fc1ecd16
DB
925 if (ret != 0) {
926 dd = NULL;
b5256303 927 goto error;
fc1ecd16 928 }
b5256303
TC
929
930 /* unload the wkey */
931 ret = spa_keystore_unload_wkey_impl(dp->dp_spa, dd->dd_object);
932 if (ret != 0)
933 goto error;
934
935 dsl_dir_rele(dd, FTAG);
936 dsl_pool_rele(dp, FTAG);
937
938 /* remove any zvols under this ds */
939 zvol_remove_minors(dp->dp_spa, dsname, B_TRUE);
940
941 return (0);
942
943error:
944 if (dd != NULL)
945 dsl_dir_rele(dd, FTAG);
946 if (dp != NULL)
947 dsl_pool_rele(dp, FTAG);
948
949 return (ret);
950}
951
52ce99dd
TC
952void
953key_mapping_add_ref(dsl_key_mapping_t *km, void *tag)
954{
955 ASSERT3U(zfs_refcount_count(&km->km_refcnt), >=, 1);
956 zfs_refcount_add(&km->km_refcnt, tag);
957}
958
959/*
960 * The locking here is a little tricky to ensure we don't cause unnecessary
961 * performance problems. We want to release a key mapping whenever someone
962 * decrements the refcount to 0, but freeing the mapping requires removing
963 * it from the spa_keystore, which requires holding sk_km_lock as a writer.
964 * Most of the time we don't want to hold this lock as a writer, since the
965 * same lock is held as a reader for each IO that needs to encrypt / decrypt
966 * data for any dataset and in practice we will only actually free the
967 * mapping after unmounting a dataset.
968 */
969void
970key_mapping_rele(spa_t *spa, dsl_key_mapping_t *km, void *tag)
971{
972 ASSERT3U(zfs_refcount_count(&km->km_refcnt), >=, 1);
973
974 if (zfs_refcount_remove(&km->km_refcnt, tag) != 0)
975 return;
976
977 /*
978 * We think we are going to need to free the mapping. Add a
979 * reference to prevent most other releasers from thinking
980 * this might be their responsibility. This is inherently
981 * racy, so we will confirm that we are legitimately the
982 * last holder once we have the sk_km_lock as a writer.
983 */
984 zfs_refcount_add(&km->km_refcnt, FTAG);
985
986 rw_enter(&spa->spa_keystore.sk_km_lock, RW_WRITER);
987 if (zfs_refcount_remove(&km->km_refcnt, FTAG) != 0) {
988 rw_exit(&spa->spa_keystore.sk_km_lock);
989 return;
990 }
991
992 avl_remove(&spa->spa_keystore.sk_key_mappings, km);
993 rw_exit(&spa->spa_keystore.sk_km_lock);
994
995 spa_keystore_dsl_key_rele(spa, km->km_key, km);
996 kmem_free(km, sizeof (dsl_key_mapping_t));
997}
998
b5256303 999int
52ce99dd
TC
1000spa_keystore_create_mapping(spa_t *spa, dsl_dataset_t *ds, void *tag,
1001 dsl_key_mapping_t **km_out)
b5256303
TC
1002{
1003 int ret;
1004 avl_index_t where;
0d23f5e2 1005 dsl_key_mapping_t *km, *found_km;
b5256303
TC
1006 boolean_t should_free = B_FALSE;
1007
0d23f5e2
BB
1008 /* Allocate and initialize the mapping */
1009 km = kmem_zalloc(sizeof (dsl_key_mapping_t), KM_SLEEP);
424fd7c3 1010 zfs_refcount_create(&km->km_refcnt);
b5256303 1011
52ce99dd 1012 ret = spa_keystore_dsl_key_hold_dd(spa, ds->ds_dir, km, &km->km_key);
0d23f5e2 1013 if (ret != 0) {
424fd7c3 1014 zfs_refcount_destroy(&km->km_refcnt);
0d23f5e2 1015 kmem_free(km, sizeof (dsl_key_mapping_t));
52ce99dd
TC
1016
1017 if (km_out != NULL)
1018 *km_out = NULL;
0d23f5e2
BB
1019 return (ret);
1020 }
b5256303 1021
52ce99dd 1022 km->km_dsobj = ds->ds_object;
b5256303
TC
1023
1024 rw_enter(&spa->spa_keystore.sk_km_lock, RW_WRITER);
1025
1026 /*
1027 * If a mapping already exists, simply increment its refcount and
1028 * cleanup the one we made. We want to allocate / free outside of
1029 * the lock because this lock is also used by the zio layer to lookup
1030 * key mappings. Otherwise, use the one we created. Normally, there will
1031 * only be one active reference at a time (the objset owner), but there
1032 * are times when there could be multiple async users.
1033 */
1034 found_km = avl_find(&spa->spa_keystore.sk_key_mappings, km, &where);
1035 if (found_km != NULL) {
1036 should_free = B_TRUE;
c13060e4 1037 zfs_refcount_add(&found_km->km_refcnt, tag);
52ce99dd
TC
1038 if (km_out != NULL)
1039 *km_out = found_km;
b5256303 1040 } else {
c13060e4 1041 zfs_refcount_add(&km->km_refcnt, tag);
b5256303 1042 avl_insert(&spa->spa_keystore.sk_key_mappings, km, where);
52ce99dd
TC
1043 if (km_out != NULL)
1044 *km_out = km;
b5256303
TC
1045 }
1046
1047 rw_exit(&spa->spa_keystore.sk_km_lock);
1048
1049 if (should_free) {
1050 spa_keystore_dsl_key_rele(spa, km->km_key, km);
424fd7c3 1051 zfs_refcount_destroy(&km->km_refcnt);
b5256303
TC
1052 kmem_free(km, sizeof (dsl_key_mapping_t));
1053 }
1054
1055 return (0);
b5256303
TC
1056}
1057
b5256303
TC
1058int
1059spa_keystore_remove_mapping(spa_t *spa, uint64_t dsobj, void *tag)
1060{
1061 int ret;
1062 dsl_key_mapping_t search_km;
1063 dsl_key_mapping_t *found_km;
b5256303
TC
1064
1065 /* init the search key mapping */
1066 search_km.km_dsobj = dsobj;
1067
52ce99dd 1068 rw_enter(&spa->spa_keystore.sk_km_lock, RW_READER);
b5256303
TC
1069
1070 /* find the matching mapping */
1071 found_km = avl_find(&spa->spa_keystore.sk_key_mappings,
1072 &search_km, NULL);
1073 if (found_km == NULL) {
1074 ret = SET_ERROR(ENOENT);
1075 goto error_unlock;
1076 }
1077
b5256303
TC
1078 rw_exit(&spa->spa_keystore.sk_km_lock);
1079
52ce99dd 1080 key_mapping_rele(spa, found_km, tag);
b5256303
TC
1081
1082 return (0);
1083
1084error_unlock:
1085 rw_exit(&spa->spa_keystore.sk_km_lock);
1086 return (ret);
1087}
1088
1089/*
1090 * This function is primarily used by the zio and arc layer to lookup
1091 * DSL Crypto Keys for encryption. Callers must release the key with
1092 * spa_keystore_dsl_key_rele(). The function may also be called with
1093 * dck_out == NULL and tag == NULL to simply check that a key exists
1094 * without getting a reference to it.
1095 */
1096int
1097spa_keystore_lookup_key(spa_t *spa, uint64_t dsobj, void *tag,
1098 dsl_crypto_key_t **dck_out)
1099{
1100 int ret;
1101 dsl_key_mapping_t search_km;
1102 dsl_key_mapping_t *found_km;
1103
1104 ASSERT((tag != NULL && dck_out != NULL) ||
1105 (tag == NULL && dck_out == NULL));
1106
1107 /* init the search key mapping */
1108 search_km.km_dsobj = dsobj;
1109
1110 rw_enter(&spa->spa_keystore.sk_km_lock, RW_READER);
1111
1112 /* remove the mapping from the tree */
1113 found_km = avl_find(&spa->spa_keystore.sk_key_mappings, &search_km,
1114 NULL);
1115 if (found_km == NULL) {
1116 ret = SET_ERROR(ENOENT);
1117 goto error_unlock;
1118 }
1119
1120 if (found_km && tag)
c13060e4 1121 zfs_refcount_add(&found_km->km_key->dck_holds, tag);
b5256303
TC
1122
1123 rw_exit(&spa->spa_keystore.sk_km_lock);
1124
1125 if (dck_out != NULL)
1126 *dck_out = found_km->km_key;
1127 return (0);
1128
1129error_unlock:
1130 rw_exit(&spa->spa_keystore.sk_km_lock);
1131
1132 if (dck_out != NULL)
1133 *dck_out = NULL;
1134 return (ret);
1135}
1136
1137static int
1138dmu_objset_check_wkey_loaded(dsl_dir_t *dd)
1139{
1140 int ret;
1141 dsl_wrapping_key_t *wkey = NULL;
1142
1143 ret = spa_keystore_wkey_hold_dd(dd->dd_pool->dp_spa, dd, FTAG,
1144 &wkey);
1145 if (ret != 0)
1146 return (SET_ERROR(EACCES));
1147
1148 dsl_wrapping_key_rele(wkey, FTAG);
1149
1150 return (0);
1151}
1152
1153static zfs_keystatus_t
1154dsl_dataset_get_keystatus(dsl_dir_t *dd)
1155{
1156 /* check if this dd has a has a dsl key */
1157 if (dd->dd_crypto_obj == 0)
1158 return (ZFS_KEYSTATUS_NONE);
1159
1160 return (dmu_objset_check_wkey_loaded(dd) == 0 ?
1161 ZFS_KEYSTATUS_AVAILABLE : ZFS_KEYSTATUS_UNAVAILABLE);
1162}
1163
1164static int
1165dsl_dir_get_crypt(dsl_dir_t *dd, uint64_t *crypt)
1166{
1167 if (dd->dd_crypto_obj == 0) {
1168 *crypt = ZIO_CRYPT_OFF;
1169 return (0);
1170 }
1171
1172 return (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
1173 DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1, crypt));
1174}
1175
1176static void
1177dsl_crypto_key_sync_impl(objset_t *mos, uint64_t dckobj, uint64_t crypt,
1178 uint64_t root_ddobj, uint64_t guid, uint8_t *iv, uint8_t *mac,
1179 uint8_t *keydata, uint8_t *hmac_keydata, uint64_t keyformat,
1180 uint64_t salt, uint64_t iters, dmu_tx_t *tx)
1181{
1182 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
1183 &crypt, tx));
1184 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1,
1185 &root_ddobj, tx));
1186 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1,
1187 &guid, tx));
1188 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
1189 iv, tx));
1190 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
1191 mac, tx));
1192 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
1193 MASTER_KEY_MAX_LEN, keydata, tx));
1194 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
1195 SHA512_HMAC_KEYLEN, hmac_keydata, tx));
1196 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
1197 8, 1, &keyformat, tx));
1198 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),
1199 8, 1, &salt, tx));
1200 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),
1201 8, 1, &iters, tx));
1202}
1203
1204static void
1205dsl_crypto_key_sync(dsl_crypto_key_t *dck, dmu_tx_t *tx)
1206{
1207 zio_crypt_key_t *key = &dck->dck_key;
1208 dsl_wrapping_key_t *wkey = dck->dck_wkey;
1209 uint8_t keydata[MASTER_KEY_MAX_LEN];
1210 uint8_t hmac_keydata[SHA512_HMAC_KEYLEN];
1211 uint8_t iv[WRAPPING_IV_LEN];
1212 uint8_t mac[WRAPPING_MAC_LEN];
1213
1214 ASSERT(dmu_tx_is_syncing(tx));
1215 ASSERT3U(key->zk_crypt, <, ZIO_CRYPT_FUNCTIONS);
1216
1217 /* encrypt and store the keys along with the IV and MAC */
1218 VERIFY0(zio_crypt_key_wrap(&dck->dck_wkey->wk_key, key, iv, mac,
1219 keydata, hmac_keydata));
1220
1221 /* update the ZAP with the obtained values */
1222 dsl_crypto_key_sync_impl(tx->tx_pool->dp_meta_objset, dck->dck_obj,
1223 key->zk_crypt, wkey->wk_ddobj, key->zk_guid, iv, mac, keydata,
1224 hmac_keydata, wkey->wk_keyformat, wkey->wk_salt, wkey->wk_iters,
1225 tx);
1226}
1227
1228typedef struct spa_keystore_change_key_args {
1229 const char *skcka_dsname;
1230 dsl_crypto_params_t *skcka_cp;
1231} spa_keystore_change_key_args_t;
1232
1233static int
1234spa_keystore_change_key_check(void *arg, dmu_tx_t *tx)
1235{
1236 int ret;
1237 dsl_dir_t *dd = NULL;
1238 dsl_pool_t *dp = dmu_tx_pool(tx);
1239 spa_keystore_change_key_args_t *skcka = arg;
1240 dsl_crypto_params_t *dcp = skcka->skcka_cp;
1241 uint64_t rddobj;
1242
1243 /* check for the encryption feature */
1244 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
1245 ret = SET_ERROR(ENOTSUP);
1246 goto error;
1247 }
1248
1249 /* check for valid key change command */
1250 if (dcp->cp_cmd != DCP_CMD_NEW_KEY &&
1251 dcp->cp_cmd != DCP_CMD_INHERIT &&
1252 dcp->cp_cmd != DCP_CMD_FORCE_NEW_KEY &&
1253 dcp->cp_cmd != DCP_CMD_FORCE_INHERIT) {
1254 ret = SET_ERROR(EINVAL);
1255 goto error;
1256 }
1257
1258 /* hold the dd */
1259 ret = dsl_dir_hold(dp, skcka->skcka_dsname, FTAG, &dd, NULL);
fc1ecd16
DB
1260 if (ret != 0) {
1261 dd = NULL;
b5256303 1262 goto error;
fc1ecd16 1263 }
b5256303
TC
1264
1265 /* verify that the dataset is encrypted */
1266 if (dd->dd_crypto_obj == 0) {
1267 ret = SET_ERROR(EINVAL);
1268 goto error;
1269 }
1270
1271 /* clones must always use their origin's key */
1272 if (dsl_dir_is_clone(dd)) {
1273 ret = SET_ERROR(EINVAL);
1274 goto error;
1275 }
1276
1277 /* lookup the ddobj we are inheriting the keylocation from */
1278 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
1279 if (ret != 0)
1280 goto error;
1281
85ce3f4f 1282 /* Handle inheritance */
b5256303
TC
1283 if (dcp->cp_cmd == DCP_CMD_INHERIT ||
1284 dcp->cp_cmd == DCP_CMD_FORCE_INHERIT) {
1285 /* no other encryption params should be given */
1286 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
1287 dcp->cp_keylocation != NULL ||
1288 dcp->cp_wkey != NULL) {
1289 ret = SET_ERROR(EINVAL);
1290 goto error;
1291 }
1292
1293 /* check that this is an encryption root */
1294 if (dd->dd_object != rddobj) {
1295 ret = SET_ERROR(EINVAL);
1296 goto error;
1297 }
1298
1299 /* check that the parent is encrypted */
1300 if (dd->dd_parent->dd_crypto_obj == 0) {
1301 ret = SET_ERROR(EINVAL);
1302 goto error;
1303 }
1304
1305 /* if we are rewrapping check that both keys are loaded */
1306 if (dcp->cp_cmd == DCP_CMD_INHERIT) {
1307 ret = dmu_objset_check_wkey_loaded(dd);
1308 if (ret != 0)
1309 goto error;
1310
1311 ret = dmu_objset_check_wkey_loaded(dd->dd_parent);
1312 if (ret != 0)
1313 goto error;
1314 }
1315
1316 dsl_dir_rele(dd, FTAG);
1317 return (0);
1318 }
1319
1320 /* handle forcing an encryption root without rewrapping */
1321 if (dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY) {
1322 /* no other encryption params should be given */
1323 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
1324 dcp->cp_keylocation != NULL ||
1325 dcp->cp_wkey != NULL) {
1326 ret = SET_ERROR(EINVAL);
1327 goto error;
1328 }
1329
1330 /* check that this is not an encryption root */
1331 if (dd->dd_object == rddobj) {
1332 ret = SET_ERROR(EINVAL);
1333 goto error;
1334 }
1335
1336 dsl_dir_rele(dd, FTAG);
1337 return (0);
1338 }
1339
1340 /* crypt cannot be changed after creation */
1341 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT) {
1342 ret = SET_ERROR(EINVAL);
1343 goto error;
1344 }
1345
1346 /* we are not inheritting our parent's wkey so we need one ourselves */
1347 if (dcp->cp_wkey == NULL) {
1348 ret = SET_ERROR(EINVAL);
1349 goto error;
1350 }
1351
1352 /* check for a valid keyformat for the new wrapping key */
1353 if (dcp->cp_wkey->wk_keyformat >= ZFS_KEYFORMAT_FORMATS ||
1354 dcp->cp_wkey->wk_keyformat == ZFS_KEYFORMAT_NONE) {
1355 ret = SET_ERROR(EINVAL);
1356 goto error;
1357 }
1358
1359 /*
1360 * If this dataset is not currently an encryption root we need a new
1361 * keylocation for this dataset's new wrapping key. Otherwise we can
1362 * just keep the one we already had.
1363 */
1364 if (dd->dd_object != rddobj && dcp->cp_keylocation == NULL) {
1365 ret = SET_ERROR(EINVAL);
1366 goto error;
1367 }
1368
1369 /* check that the keylocation is valid if it is not NULL */
1370 if (dcp->cp_keylocation != NULL &&
1371 !zfs_prop_valid_keylocation(dcp->cp_keylocation, B_TRUE)) {
1372 ret = SET_ERROR(EINVAL);
1373 goto error;
1374 }
1375
1376 /* passphrases require pbkdf2 salt and iters */
1377 if (dcp->cp_wkey->wk_keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
1378 if (dcp->cp_wkey->wk_salt == 0 ||
1379 dcp->cp_wkey->wk_iters < MIN_PBKDF2_ITERATIONS) {
1380 ret = SET_ERROR(EINVAL);
1381 goto error;
1382 }
1383 } else {
1384 if (dcp->cp_wkey->wk_salt != 0 || dcp->cp_wkey->wk_iters != 0) {
1385 ret = SET_ERROR(EINVAL);
1386 goto error;
1387 }
1388 }
1389
1390 /* make sure the dd's wkey is loaded */
1391 ret = dmu_objset_check_wkey_loaded(dd);
1392 if (ret != 0)
1393 goto error;
1394
1395 dsl_dir_rele(dd, FTAG);
1396
1397 return (0);
1398
1399error:
1400 if (dd != NULL)
1401 dsl_dir_rele(dd, FTAG);
1402
1403 return (ret);
1404}
1405
1406
1407static void
1408spa_keystore_change_key_sync_impl(uint64_t rddobj, uint64_t ddobj,
1409 uint64_t new_rddobj, dsl_wrapping_key_t *wkey, dmu_tx_t *tx)
1410{
1411 zap_cursor_t *zc;
1412 zap_attribute_t *za;
1413 dsl_pool_t *dp = dmu_tx_pool(tx);
1414 dsl_dir_t *dd = NULL;
1415 dsl_crypto_key_t *dck = NULL;
1416 uint64_t curr_rddobj;
1417
1418 ASSERT(RW_WRITE_HELD(&dp->dp_spa->spa_keystore.sk_wkeys_lock));
1419
1420 /* hold the dd */
1421 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
1422
1423 /* ignore hidden dsl dirs */
1424 if (dd->dd_myname[0] == '$' || dd->dd_myname[0] == '%') {
1425 dsl_dir_rele(dd, FTAG);
1426 return;
1427 }
1428
b135b9f1
TC
1429 /*
1430 * Stop recursing if this dsl dir didn't inherit from the root
1431 * or if this dd is a clone.
1432 */
b5256303 1433 VERIFY0(dsl_dir_get_encryption_root_ddobj(dd, &curr_rddobj));
b135b9f1 1434 if (curr_rddobj != rddobj || dsl_dir_is_clone(dd)) {
b5256303
TC
1435 dsl_dir_rele(dd, FTAG);
1436 return;
1437 }
1438
1439 /*
1440 * If we don't have a wrapping key just update the dck to reflect the
1441 * new encryption root. Otherwise rewrap the entire dck and re-sync it
1442 * to disk.
1443 */
1444 if (wkey == NULL) {
1445 VERIFY0(zap_update(dp->dp_meta_objset, dd->dd_crypto_obj,
1446 DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1, &new_rddobj, tx));
1447 } else {
1448 VERIFY0(spa_keystore_dsl_key_hold_dd(dp->dp_spa, dd,
1449 FTAG, &dck));
1450 dsl_wrapping_key_hold(wkey, dck);
1451 dsl_wrapping_key_rele(dck->dck_wkey, dck);
1452 dck->dck_wkey = wkey;
1453 dsl_crypto_key_sync(dck, tx);
1454 spa_keystore_dsl_key_rele(dp->dp_spa, dck, FTAG);
1455 }
1456
1457 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
1458 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1459
b135b9f1 1460 /* Recurse into all child dsl dirs. */
b5256303
TC
1461 for (zap_cursor_init(zc, dp->dp_meta_objset,
1462 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1463 zap_cursor_retrieve(zc, za) == 0;
1464 zap_cursor_advance(zc)) {
1465 spa_keystore_change_key_sync_impl(rddobj,
1466 za->za_first_integer, new_rddobj, wkey, tx);
1467 }
1468 zap_cursor_fini(zc);
1469
b5256303
TC
1470 kmem_free(za, sizeof (zap_attribute_t));
1471 kmem_free(zc, sizeof (zap_cursor_t));
1472
1473 dsl_dir_rele(dd, FTAG);
1474}
1475
1476static void
1477spa_keystore_change_key_sync(void *arg, dmu_tx_t *tx)
1478{
1479 dsl_dataset_t *ds;
1480 avl_index_t where;
1481 dsl_pool_t *dp = dmu_tx_pool(tx);
1482 spa_t *spa = dp->dp_spa;
1483 spa_keystore_change_key_args_t *skcka = arg;
1484 dsl_crypto_params_t *dcp = skcka->skcka_cp;
1485 dsl_wrapping_key_t *wkey = NULL, *found_wkey;
1486 dsl_wrapping_key_t wkey_search;
1487 char *keylocation = dcp->cp_keylocation;
1488 uint64_t rddobj, new_rddobj;
1489
1490 /* create and initialize the wrapping key */
1491 VERIFY0(dsl_dataset_hold(dp, skcka->skcka_dsname, FTAG, &ds));
1492 ASSERT(!ds->ds_is_snapshot);
1493
1494 if (dcp->cp_cmd == DCP_CMD_NEW_KEY ||
1495 dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY) {
1496 /*
1497 * We are changing to a new wkey. Set additional properties
1498 * which can be sent along with this ioctl. Note that this
1499 * command can set keylocation even if it can't normally be
1500 * set via 'zfs set' due to a non-local keylocation.
1501 */
1502 if (dcp->cp_cmd == DCP_CMD_NEW_KEY) {
1503 wkey = dcp->cp_wkey;
1504 wkey->wk_ddobj = ds->ds_dir->dd_object;
1505 } else {
1506 keylocation = "prompt";
1507 }
1508
1509 if (keylocation != NULL) {
1510 dsl_prop_set_sync_impl(ds,
1511 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1512 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1,
1513 keylocation, tx);
1514 }
1515
1516 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds->ds_dir, &rddobj));
1517 new_rddobj = ds->ds_dir->dd_object;
1518 } else {
1519 /*
1520 * We are inheritting the parent's wkey. Unset any local
1521 * keylocation and grab a reference to the wkey.
1522 */
1523 if (dcp->cp_cmd == DCP_CMD_INHERIT) {
1524 VERIFY0(spa_keystore_wkey_hold_dd(spa,
1525 ds->ds_dir->dd_parent, FTAG, &wkey));
1526 }
1527
1528 dsl_prop_set_sync_impl(ds,
1529 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), ZPROP_SRC_NONE,
1530 0, 0, NULL, tx);
1531
1532 rddobj = ds->ds_dir->dd_object;
62df1bc8
TC
1533 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds->ds_dir->dd_parent,
1534 &new_rddobj));
b5256303
TC
1535 }
1536
1537 if (wkey == NULL) {
1538 ASSERT(dcp->cp_cmd == DCP_CMD_FORCE_INHERIT ||
1539 dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY);
1540 }
1541
1542 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
1543
1544 /* recurse through all children and rewrap their keys */
1545 spa_keystore_change_key_sync_impl(rddobj, ds->ds_dir->dd_object,
1546 new_rddobj, wkey, tx);
1547
1548 /*
1549 * All references to the old wkey should be released now (if it
1550 * existed). Replace the wrapping key.
1551 */
1552 wkey_search.wk_ddobj = ds->ds_dir->dd_object;
1553 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, &wkey_search, NULL);
1554 if (found_wkey != NULL) {
424fd7c3 1555 ASSERT0(zfs_refcount_count(&found_wkey->wk_refcnt));
b5256303
TC
1556 avl_remove(&spa->spa_keystore.sk_wkeys, found_wkey);
1557 dsl_wrapping_key_free(found_wkey);
1558 }
1559
1560 if (dcp->cp_cmd == DCP_CMD_NEW_KEY) {
1561 avl_find(&spa->spa_keystore.sk_wkeys, wkey, &where);
1562 avl_insert(&spa->spa_keystore.sk_wkeys, wkey, where);
1563 } else if (wkey != NULL) {
1564 dsl_wrapping_key_rele(wkey, FTAG);
1565 }
1566
1567 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
1568
1569 dsl_dataset_rele(ds, FTAG);
1570}
1571
1572int
1573spa_keystore_change_key(const char *dsname, dsl_crypto_params_t *dcp)
1574{
1575 spa_keystore_change_key_args_t skcka;
1576
1577 /* initialize the args struct */
1578 skcka.skcka_dsname = dsname;
1579 skcka.skcka_cp = dcp;
1580
1581 /*
1582 * Perform the actual work in syncing context. The blocks modified
1583 * here could be calculated but it would require holding the pool
1584 * lock and tarversing all of the datasets that will have their keys
1585 * changed.
1586 */
1587 return (dsl_sync_task(dsname, spa_keystore_change_key_check,
1588 spa_keystore_change_key_sync, &skcka, 15,
1589 ZFS_SPACE_CHECK_RESERVED));
1590}
1591
1592int
1593dsl_dir_rename_crypt_check(dsl_dir_t *dd, dsl_dir_t *newparent)
1594{
1595 int ret;
1596 uint64_t curr_rddobj, parent_rddobj;
1597
1598 if (dd->dd_crypto_obj == 0) {
1599 /* children of encrypted parents must be encrypted */
1600 if (newparent->dd_crypto_obj != 0) {
1601 ret = SET_ERROR(EACCES);
1602 goto error;
1603 }
1604
1605 return (0);
1606 }
1607
1608 ret = dsl_dir_get_encryption_root_ddobj(dd, &curr_rddobj);
1609 if (ret != 0)
1610 goto error;
1611
1612 /*
1613 * if this is not an encryption root, we must make sure we are not
1614 * moving dd to a new encryption root
1615 */
1616 if (dd->dd_object != curr_rddobj) {
1617 ret = dsl_dir_get_encryption_root_ddobj(newparent,
1618 &parent_rddobj);
1619 if (ret != 0)
1620 goto error;
1621
1622 if (parent_rddobj != curr_rddobj) {
1623 ret = SET_ERROR(EACCES);
1624 goto error;
1625 }
1626 }
1627
1628 return (0);
1629
1630error:
1631 return (ret);
1632}
1633
1634/*
1635 * Check to make sure that a promote from targetdd to origindd will not require
1636 * any key rewraps.
1637 */
1638int
1639dsl_dataset_promote_crypt_check(dsl_dir_t *target, dsl_dir_t *origin)
1640{
1641 int ret;
1642 uint64_t rddobj, op_rddobj, tp_rddobj;
1643
1644 /* If the dataset is not encrypted we don't need to check anything */
1645 if (origin->dd_crypto_obj == 0)
1646 return (0);
1647
1648 /*
1649 * If we are not changing the first origin snapshot in a chain
1650 * the encryption root won't change either.
1651 */
1652 if (dsl_dir_is_clone(origin))
1653 return (0);
1654
1655 /*
1656 * If the origin is the encryption root we will update
1657 * the DSL Crypto Key to point to the target instead.
1658 */
1659 ret = dsl_dir_get_encryption_root_ddobj(origin, &rddobj);
1660 if (ret != 0)
1661 return (ret);
1662
1663 if (rddobj == origin->dd_object)
1664 return (0);
1665
1666 /*
1667 * The origin is inheriting its encryption root from its parent.
1668 * Check that the parent of the target has the same encryption root.
1669 */
1670 ret = dsl_dir_get_encryption_root_ddobj(origin->dd_parent, &op_rddobj);
1671 if (ret != 0)
1672 return (ret);
1673
1674 ret = dsl_dir_get_encryption_root_ddobj(target->dd_parent, &tp_rddobj);
1675 if (ret != 0)
1676 return (ret);
1677
1678 if (op_rddobj != tp_rddobj)
1679 return (SET_ERROR(EACCES));
1680
1681 return (0);
1682}
1683
1684void
1685dsl_dataset_promote_crypt_sync(dsl_dir_t *target, dsl_dir_t *origin,
1686 dmu_tx_t *tx)
1687{
1688 uint64_t rddobj;
1689 dsl_pool_t *dp = target->dd_pool;
1690 dsl_dataset_t *targetds;
1691 dsl_dataset_t *originds;
1692 char *keylocation;
1693
1694 if (origin->dd_crypto_obj == 0)
1695 return;
1696 if (dsl_dir_is_clone(origin))
1697 return;
1698
1699 VERIFY0(dsl_dir_get_encryption_root_ddobj(origin, &rddobj));
1700
1701 if (rddobj != origin->dd_object)
1702 return;
1703
1704 /*
1705 * If the target is being promoted to the encyrption root update the
1706 * DSL Crypto Key and keylocation to reflect that. We also need to
1707 * update the DSL Crypto Keys of all children inheritting their
1708 * encryption root to point to the new target. Otherwise, the check
1709 * function ensured that the encryption root will not change.
1710 */
1711 keylocation = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
1712
1713 VERIFY0(dsl_dataset_hold_obj(dp,
1714 dsl_dir_phys(target)->dd_head_dataset_obj, FTAG, &targetds));
1715 VERIFY0(dsl_dataset_hold_obj(dp,
1716 dsl_dir_phys(origin)->dd_head_dataset_obj, FTAG, &originds));
1717
1718 VERIFY0(dsl_prop_get_dd(origin, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1719 1, ZAP_MAXVALUELEN, keylocation, NULL, B_FALSE));
1720 dsl_prop_set_sync_impl(targetds, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1721 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1, keylocation, tx);
1722 dsl_prop_set_sync_impl(originds, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1723 ZPROP_SRC_NONE, 0, 0, NULL, tx);
1724
1725 rw_enter(&dp->dp_spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
1726 spa_keystore_change_key_sync_impl(rddobj, origin->dd_object,
1727 target->dd_object, NULL, tx);
1728 rw_exit(&dp->dp_spa->spa_keystore.sk_wkeys_lock);
1729
1730 dsl_dataset_rele(targetds, FTAG);
1731 dsl_dataset_rele(originds, FTAG);
1732 kmem_free(keylocation, ZAP_MAXVALUELEN);
1733}
1734
1735int
1736dmu_objset_clone_crypt_check(dsl_dir_t *parentdd, dsl_dir_t *origindd)
1737{
1738 int ret;
1739 uint64_t pcrypt, crypt;
1740
1741 /*
1742 * Check that we are not making an unencrypted child of an
1743 * encrypted parent.
1744 */
1745 ret = dsl_dir_get_crypt(parentdd, &pcrypt);
1746 if (ret != 0)
1747 return (ret);
1748
1749 ret = dsl_dir_get_crypt(origindd, &crypt);
1750 if (ret != 0)
1751 return (ret);
1752
1753 ASSERT3U(pcrypt, !=, ZIO_CRYPT_INHERIT);
1754 ASSERT3U(crypt, !=, ZIO_CRYPT_INHERIT);
1755
1756 if (crypt == ZIO_CRYPT_OFF && pcrypt != ZIO_CRYPT_OFF)
1757 return (SET_ERROR(EINVAL));
1758
1759 return (0);
1760}
1761
1762
1763int
1fff937a
TC
1764dmu_objset_create_crypt_check(dsl_dir_t *parentdd, dsl_crypto_params_t *dcp,
1765 boolean_t *will_encrypt)
b5256303
TC
1766{
1767 int ret;
1768 uint64_t pcrypt, crypt;
d9c460a0
TC
1769 dsl_crypto_params_t dummy_dcp = { 0 };
1770
1fff937a
TC
1771 if (will_encrypt != NULL)
1772 *will_encrypt = B_FALSE;
1773
d9c460a0
TC
1774 if (dcp == NULL)
1775 dcp = &dummy_dcp;
b5256303
TC
1776
1777 if (dcp->cp_cmd != DCP_CMD_NONE)
1778 return (SET_ERROR(EINVAL));
1779
1780 if (parentdd != NULL) {
1781 ret = dsl_dir_get_crypt(parentdd, &pcrypt);
1782 if (ret != 0)
1783 return (ret);
1784 } else {
1785 pcrypt = ZIO_CRYPT_OFF;
1786 }
1787
1788 crypt = (dcp->cp_crypt == ZIO_CRYPT_INHERIT) ? pcrypt : dcp->cp_crypt;
1789
1790 ASSERT3U(pcrypt, !=, ZIO_CRYPT_INHERIT);
1791 ASSERT3U(crypt, !=, ZIO_CRYPT_INHERIT);
1792
1793 /*
1794 * We can't create an unencrypted child of an encrypted parent
1795 * under any circumstances.
1796 */
1797 if (crypt == ZIO_CRYPT_OFF && pcrypt != ZIO_CRYPT_OFF)
1798 return (SET_ERROR(EINVAL));
1799
1800 /* check for valid dcp with no encryption (inherited or local) */
1801 if (crypt == ZIO_CRYPT_OFF) {
1802 /* Must not specify encryption params */
1803 if (dcp->cp_wkey != NULL ||
1804 (dcp->cp_keylocation != NULL &&
1805 strcmp(dcp->cp_keylocation, "none") != 0))
1806 return (SET_ERROR(EINVAL));
1807
1808 return (0);
1809 }
1810
1fff937a
TC
1811 if (will_encrypt != NULL)
1812 *will_encrypt = B_TRUE;
1813
b5256303
TC
1814 /*
1815 * We will now definitely be encrypting. Check the feature flag. When
1816 * creating the pool the caller will check this for us since we won't
1fff937a 1817 * technically have the feature activated yet.
b5256303
TC
1818 */
1819 if (parentdd != NULL &&
1820 !spa_feature_is_enabled(parentdd->dd_pool->dp_spa,
1821 SPA_FEATURE_ENCRYPTION)) {
1822 return (SET_ERROR(EOPNOTSUPP));
1823 }
1824
85ce3f4f 1825 /* handle inheritance */
b5256303
TC
1826 if (dcp->cp_wkey == NULL) {
1827 ASSERT3P(parentdd, !=, NULL);
1828
1829 /* key must be fully unspecified */
1830 if (dcp->cp_keylocation != NULL)
1831 return (SET_ERROR(EINVAL));
1832
1833 /* parent must have a key to inherit */
1834 if (pcrypt == ZIO_CRYPT_OFF)
1835 return (SET_ERROR(EINVAL));
1836
1837 /* check for parent key */
1838 ret = dmu_objset_check_wkey_loaded(parentdd);
1839 if (ret != 0)
1840 return (ret);
1841
1842 return (0);
1843 }
1844
1845 /* At this point we should have a fully specified key. Check location */
1846 if (dcp->cp_keylocation == NULL ||
1847 !zfs_prop_valid_keylocation(dcp->cp_keylocation, B_TRUE))
1848 return (SET_ERROR(EINVAL));
1849
1850 /* Must have fully specified keyformat */
1851 switch (dcp->cp_wkey->wk_keyformat) {
1852 case ZFS_KEYFORMAT_HEX:
1853 case ZFS_KEYFORMAT_RAW:
1854 /* requires no pbkdf2 iters and salt */
1855 if (dcp->cp_wkey->wk_salt != 0 || dcp->cp_wkey->wk_iters != 0)
1856 return (SET_ERROR(EINVAL));
1857 break;
1858 case ZFS_KEYFORMAT_PASSPHRASE:
1859 /* requires pbkdf2 iters and salt */
1860 if (dcp->cp_wkey->wk_salt == 0 ||
1861 dcp->cp_wkey->wk_iters < MIN_PBKDF2_ITERATIONS)
1862 return (SET_ERROR(EINVAL));
1863 break;
1864 case ZFS_KEYFORMAT_NONE:
1865 default:
1866 /* keyformat must be specified and valid */
1867 return (SET_ERROR(EINVAL));
1868 }
1869
1870 return (0);
1871}
1872
1873void
1874dsl_dataset_create_crypt_sync(uint64_t dsobj, dsl_dir_t *dd,
1875 dsl_dataset_t *origin, dsl_crypto_params_t *dcp, dmu_tx_t *tx)
1876{
1877 dsl_pool_t *dp = dd->dd_pool;
1878 uint64_t crypt;
1879 dsl_wrapping_key_t *wkey;
1880
1881 /* clones always use their origin's wrapping key */
1882 if (dsl_dir_is_clone(dd)) {
1883 ASSERT3P(dcp, ==, NULL);
1884
1885 /*
1886 * If this is an encrypted clone we just need to clone the
1887 * dck into dd. Zapify the dd so we can do that.
1888 */
1889 if (origin->ds_dir->dd_crypto_obj != 0) {
1890 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1891 dsl_dir_zapify(dd, tx);
1892
1893 dd->dd_crypto_obj =
1894 dsl_crypto_key_clone_sync(origin->ds_dir, tx);
1895 VERIFY0(zap_add(dp->dp_meta_objset, dd->dd_object,
1896 DD_FIELD_CRYPTO_KEY_OBJ, sizeof (uint64_t), 1,
1897 &dd->dd_crypto_obj, tx));
1898 }
1899
1900 return;
1901 }
1902
1903 /*
1904 * A NULL dcp at this point indicates this is the origin dataset
1905 * which does not have an objset to encrypt. Raw receives will handle
b0918402 1906 * encryption separately later. In both cases we can simply return.
b5256303
TC
1907 */
1908 if (dcp == NULL || dcp->cp_cmd == DCP_CMD_RAW_RECV)
1909 return;
1910
1911 crypt = dcp->cp_crypt;
1912 wkey = dcp->cp_wkey;
1913
1914 /* figure out the effective crypt */
1915 if (crypt == ZIO_CRYPT_INHERIT && dd->dd_parent != NULL)
1916 VERIFY0(dsl_dir_get_crypt(dd->dd_parent, &crypt));
1917
1918 /* if we aren't doing encryption just return */
1919 if (crypt == ZIO_CRYPT_OFF || crypt == ZIO_CRYPT_INHERIT)
1920 return;
1921
1922 /* zapify the dd so that we can add the crypto key obj to it */
1923 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1924 dsl_dir_zapify(dd, tx);
1925
1926 /* use the new key if given or inherit from the parent */
1927 if (wkey == NULL) {
1928 VERIFY0(spa_keystore_wkey_hold_dd(dp->dp_spa,
1929 dd->dd_parent, FTAG, &wkey));
1930 } else {
1931 wkey->wk_ddobj = dd->dd_object;
1932 }
1933
4807c0ba
TC
1934 ASSERT3P(wkey, !=, NULL);
1935
b5256303
TC
1936 /* Create or clone the DSL crypto key and activate the feature */
1937 dd->dd_crypto_obj = dsl_crypto_key_create_sync(crypt, wkey, tx);
1938 VERIFY0(zap_add(dp->dp_meta_objset, dd->dd_object,
1939 DD_FIELD_CRYPTO_KEY_OBJ, sizeof (uint64_t), 1, &dd->dd_crypto_obj,
1940 tx));
1941 dsl_dataset_activate_feature(dsobj, SPA_FEATURE_ENCRYPTION, tx);
1942
1943 /*
1944 * If we inherited the wrapping key we release our reference now.
1945 * Otherwise, this is a new key and we need to load it into the
1946 * keystore.
1947 */
1948 if (dcp->cp_wkey == NULL) {
1949 dsl_wrapping_key_rele(wkey, FTAG);
1950 } else {
1951 VERIFY0(spa_keystore_load_wkey_impl(dp->dp_spa, wkey));
1952 }
1953}
1954
1955typedef struct dsl_crypto_recv_key_arg {
1956 uint64_t dcrka_dsobj;
b5256303 1957 dmu_objset_type_t dcrka_ostype;
b0918402
TC
1958 nvlist_t *dcrka_nvl;
1959 boolean_t dcrka_do_key;
b5256303
TC
1960} dsl_crypto_recv_key_arg_t;
1961
b0918402
TC
1962static int
1963dsl_crypto_recv_raw_objset_check(dsl_dataset_t *ds, dmu_objset_type_t ostype,
1964 nvlist_t *nvl, dmu_tx_t *tx)
b5256303
TC
1965{
1966 int ret;
b5256303
TC
1967 objset_t *os;
1968 dnode_t *mdn;
b5256303
TC
1969 uint8_t *buf = NULL;
1970 uint_t len;
b0918402 1971 uint64_t intval, nlevels, blksz, ibs, nblkptr, maxblkid;
b5256303 1972
b0918402
TC
1973 if (ostype != DMU_OST_ZFS && ostype != DMU_OST_ZVOL)
1974 return (SET_ERROR(EINVAL));
b5256303
TC
1975
1976 /* raw receives also need info about the structure of the metadnode */
b5256303 1977 ret = nvlist_lookup_uint64(nvl, "mdn_compress", &intval);
b0918402
TC
1978 if (ret != 0 || intval >= ZIO_COMPRESS_LEGACY_FUNCTIONS)
1979 return (SET_ERROR(EINVAL));
1980
1981 ret = nvlist_lookup_uint64(nvl, "mdn_checksum", &intval);
1982 if (ret != 0 || intval >= ZIO_CHECKSUM_LEGACY_FUNCTIONS)
1983 return (SET_ERROR(EINVAL));
b5256303
TC
1984
1985 ret = nvlist_lookup_uint64(nvl, "mdn_nlevels", &nlevels);
b0918402
TC
1986 if (ret != 0 || nlevels > DN_MAX_LEVELS)
1987 return (SET_ERROR(EINVAL));
b5256303
TC
1988
1989 ret = nvlist_lookup_uint64(nvl, "mdn_blksz", &blksz);
b0918402
TC
1990 if (ret != 0 || blksz < SPA_MINBLOCKSIZE)
1991 return (SET_ERROR(EINVAL));
1992 else if (blksz > spa_maxblocksize(tx->tx_pool->dp_spa))
1993 return (SET_ERROR(ENOTSUP));
b5256303
TC
1994
1995 ret = nvlist_lookup_uint64(nvl, "mdn_indblkshift", &ibs);
b0918402
TC
1996 if (ret != 0 || ibs < DN_MIN_INDBLKSHIFT || ibs > DN_MAX_INDBLKSHIFT)
1997 return (SET_ERROR(ENOTSUP));
b5256303
TC
1998
1999 ret = nvlist_lookup_uint64(nvl, "mdn_nblkptr", &nblkptr);
b0918402
TC
2000 if (ret != 0 || nblkptr != DN_MAX_NBLKPTR)
2001 return (SET_ERROR(ENOTSUP));
b5256303 2002
ae76f45c 2003 ret = nvlist_lookup_uint64(nvl, "mdn_maxblkid", &maxblkid);
b0918402
TC
2004 if (ret != 0)
2005 return (SET_ERROR(EINVAL));
2006
2007 ret = nvlist_lookup_uint8_array(nvl, "portable_mac", &buf, &len);
2008 if (ret != 0 || len != ZIO_OBJSET_MAC_LEN)
2009 return (SET_ERROR(EINVAL));
ae76f45c 2010
b5256303
TC
2011 ret = dmu_objset_from_ds(ds, &os);
2012 if (ret != 0)
b0918402 2013 return (ret);
b5256303
TC
2014
2015 /*
2016 * Useraccounting is not portable and must be done with the keys loaded.
2017 * Therefore, whenever we do any kind of receive the useraccounting
2018 * must not be present.
2019 */
2020 ASSERT0(os->os_flags & OBJSET_FLAG_USERACCOUNTING_COMPLETE);
2021 ASSERT0(os->os_flags & OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2022
2023 mdn = DMU_META_DNODE(os);
2024
2025 /*
b0918402 2026 * If we already created the objset, make sure its unchangeable
b5256303
TC
2027 * properties match the ones received in the nvlist.
2028 */
2029 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2030 if (!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)) &&
2031 (mdn->dn_nlevels != nlevels || mdn->dn_datablksz != blksz ||
2032 mdn->dn_indblkshift != ibs || mdn->dn_nblkptr != nblkptr)) {
b0918402
TC
2033 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2034 return (SET_ERROR(EINVAL));
b5256303
TC
2035 }
2036 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2037
b5256303 2038 return (0);
b5256303
TC
2039}
2040
2041static void
b0918402
TC
2042dsl_crypto_recv_raw_objset_sync(dsl_dataset_t *ds, dmu_objset_type_t ostype,
2043 nvlist_t *nvl, dmu_tx_t *tx)
b5256303 2044{
b5256303 2045 dsl_pool_t *dp = tx->tx_pool;
b5256303
TC
2046 objset_t *os;
2047 dnode_t *mdn;
b0918402
TC
2048 zio_t *zio;
2049 uint8_t *portable_mac;
b5256303 2050 uint_t len;
ae76f45c 2051 uint64_t compress, checksum, nlevels, blksz, ibs, maxblkid;
b0918402 2052 boolean_t newds = B_FALSE;
b5256303 2053
b5256303
TC
2054 VERIFY0(dmu_objset_from_ds(ds, &os));
2055 mdn = DMU_META_DNODE(os);
2056
b0918402 2057 /* fetch the values we need from the nvlist */
b5256303
TC
2058 compress = fnvlist_lookup_uint64(nvl, "mdn_compress");
2059 checksum = fnvlist_lookup_uint64(nvl, "mdn_checksum");
2060 nlevels = fnvlist_lookup_uint64(nvl, "mdn_nlevels");
2061 blksz = fnvlist_lookup_uint64(nvl, "mdn_blksz");
2062 ibs = fnvlist_lookup_uint64(nvl, "mdn_indblkshift");
ae76f45c 2063 maxblkid = fnvlist_lookup_uint64(nvl, "mdn_maxblkid");
b0918402
TC
2064 VERIFY0(nvlist_lookup_uint8_array(nvl, "portable_mac", &portable_mac,
2065 &len));
b5256303
TC
2066
2067 /* if we haven't created an objset for the ds yet, do that now */
2068 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2069 if (BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {
2070 (void) dmu_objset_create_impl_dnstats(dp->dp_spa, ds,
b0918402
TC
2071 dsl_dataset_get_blkptr(ds), ostype, nlevels, blksz,
2072 ibs, tx);
2073 newds = B_TRUE;
b5256303
TC
2074 }
2075 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2076
2077 /*
2078 * Set the portable MAC. The local MAC will always be zero since the
2079 * incoming data will all be portable and user accounting will be
2080 * deferred until the next mount. Afterwards, flag the os to be
2081 * written out raw next time.
2082 */
2083 arc_release(os->os_phys_buf, &os->os_phys_buf);
2084 bcopy(portable_mac, os->os_phys->os_portable_mac, ZIO_OBJSET_MAC_LEN);
2085 bzero(os->os_phys->os_local_mac, ZIO_OBJSET_MAC_LEN);
1b66810b 2086 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
b5256303
TC
2087
2088 /* set metadnode compression and checksum */
2089 mdn->dn_compress = compress;
2090 mdn->dn_checksum = checksum;
ae76f45c
TC
2091
2092 rw_enter(&mdn->dn_struct_rwlock, RW_WRITER);
2093 dnode_new_blkid(mdn, maxblkid, tx, B_FALSE);
2094 rw_exit(&mdn->dn_struct_rwlock);
2095
b0918402
TC
2096 /*
2097 * We can't normally dirty the dataset in syncing context unless
2098 * we are creating a new dataset. In this case, we perform a
2099 * pseudo txg sync here instead.
2100 */
2101 if (newds) {
2102 dsl_dataset_dirty(ds, tx);
2103 } else {
2104 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
2105 dsl_dataset_sync(ds, zio, tx);
2106 VERIFY0(zio_wait(zio));
2107
2108 /* dsl_dataset_sync_done will drop this reference. */
2109 dmu_buf_add_ref(ds->ds_dbuf, ds);
2110 dsl_dataset_sync_done(ds, tx);
2111 }
2112}
2113
2114int
2115dsl_crypto_recv_raw_key_check(dsl_dataset_t *ds, nvlist_t *nvl, dmu_tx_t *tx)
2116{
2117 int ret;
2118 objset_t *mos = tx->tx_pool->dp_meta_objset;
2119 uint8_t *buf = NULL;
2120 uint_t len;
2121 uint64_t intval, guid, version;
2122 boolean_t is_passphrase = B_FALSE;
2123
2124 ASSERT(dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT);
2125
2126 /*
2127 * Read and check all the encryption values from the nvlist. We need
2128 * all of the fields of a DSL Crypto Key, as well as a fully specified
2129 * wrapping key.
2130 */
2131 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE, &intval);
2132 if (ret != 0 || intval >= ZIO_CRYPT_FUNCTIONS ||
2133 intval <= ZIO_CRYPT_OFF)
2134 return (SET_ERROR(EINVAL));
2135
2136 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_GUID, &intval);
2137 if (ret != 0)
2138 return (SET_ERROR(EINVAL));
2139
2140 /*
2141 * If this is an incremental receive make sure the given key guid
2142 * matches the one we already have.
2143 */
2144 if (ds->ds_dir->dd_crypto_obj != 0) {
2145 ret = zap_lookup(mos, ds->ds_dir->dd_crypto_obj,
2146 DSL_CRYPTO_KEY_GUID, 8, 1, &guid);
2147 if (ret != 0)
2148 return (ret);
2149 if (intval != guid)
2150 return (SET_ERROR(EACCES));
2151 }
2152
2153 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2154 &buf, &len);
2155 if (ret != 0 || len != MASTER_KEY_MAX_LEN)
2156 return (SET_ERROR(EINVAL));
2157
2158 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2159 &buf, &len);
2160 if (ret != 0 || len != SHA512_HMAC_KEYLEN)
2161 return (SET_ERROR(EINVAL));
2162
2163 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_IV, &buf, &len);
2164 if (ret != 0 || len != WRAPPING_IV_LEN)
2165 return (SET_ERROR(EINVAL));
2166
2167 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, &buf, &len);
2168 if (ret != 0 || len != WRAPPING_MAC_LEN)
2169 return (SET_ERROR(EINVAL));
2170
2171 /*
2172 * We don't support receiving old on-disk formats. The version 0
2173 * implementation protected several fields in an objset that were
2174 * not always portable during a raw receive. As a result, we call
2175 * the old version an on-disk errata #3.
2176 */
2177 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_VERSION, &version);
2178 if (ret != 0 || version != ZIO_CRYPT_KEY_CURRENT_VERSION)
2179 return (SET_ERROR(ENOTSUP));
2180
2181 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
2182 &intval);
2183 if (ret != 0 || intval >= ZFS_KEYFORMAT_FORMATS ||
2184 intval == ZFS_KEYFORMAT_NONE)
2185 return (SET_ERROR(EINVAL));
2186
2187 is_passphrase = (intval == ZFS_KEYFORMAT_PASSPHRASE);
2188
2189 /*
2190 * for raw receives we allow any number of pbkdf2iters since there
2191 * won't be a chance for the user to change it.
2192 */
2193 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),
2194 &intval);
2195 if (ret != 0 || (is_passphrase == (intval == 0)))
2196 return (SET_ERROR(EINVAL));
2197
2198 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),
2199 &intval);
2200 if (ret != 0 || (is_passphrase == (intval == 0)))
2201 return (SET_ERROR(EINVAL));
2202
2203 return (0);
2204}
2205
2206void
2207dsl_crypto_recv_raw_key_sync(dsl_dataset_t *ds, nvlist_t *nvl, dmu_tx_t *tx)
2208{
2209 dsl_pool_t *dp = tx->tx_pool;
2210 objset_t *mos = dp->dp_meta_objset;
2211 dsl_dir_t *dd = ds->ds_dir;
2212 uint_t len;
2213 uint64_t rddobj, one = 1;
2214 uint8_t *keydata, *hmac_keydata, *iv, *mac;
2215 uint64_t crypt, guid, keyformat, iters, salt;
2216 uint64_t version = ZIO_CRYPT_KEY_CURRENT_VERSION;
2217 char *keylocation = "prompt";
2218
2219 /* lookup the values we need to create the DSL Crypto Key */
2220 crypt = fnvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE);
2221 guid = fnvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_GUID);
2222 keyformat = fnvlist_lookup_uint64(nvl,
2223 zfs_prop_to_name(ZFS_PROP_KEYFORMAT));
2224 iters = fnvlist_lookup_uint64(nvl,
2225 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS));
2226 salt = fnvlist_lookup_uint64(nvl,
2227 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT));
2228 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2229 &keydata, &len));
2230 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2231 &hmac_keydata, &len));
2232 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_IV, &iv, &len));
2233 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, &mac, &len));
b5256303
TC
2234
2235 /* if this is a new dataset setup the DSL Crypto Key. */
b0918402 2236 if (dd->dd_crypto_obj == 0) {
b5256303 2237 /* zapify the dsl dir so we can add the key object to it */
b0918402
TC
2238 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2239 dsl_dir_zapify(dd, tx);
b5256303
TC
2240
2241 /* create the DSL Crypto Key on disk and activate the feature */
b0918402 2242 dd->dd_crypto_obj = zap_create(mos,
b5256303
TC
2243 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
2244 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
b0918402 2245 dd->dd_crypto_obj, DSL_CRYPTO_KEY_REFCOUNT,
b5256303 2246 sizeof (uint64_t), 1, &one, tx));
ae76f45c 2247 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
b0918402 2248 dd->dd_crypto_obj, DSL_CRYPTO_KEY_VERSION,
ae76f45c 2249 sizeof (uint64_t), 1, &version, tx));
b5256303 2250
b0918402
TC
2251 dsl_dataset_activate_feature(ds->ds_object,
2252 SPA_FEATURE_ENCRYPTION, tx);
b5256303
TC
2253 ds->ds_feature_inuse[SPA_FEATURE_ENCRYPTION] = B_TRUE;
2254
2255 /* save the dd_crypto_obj on disk */
b0918402
TC
2256 VERIFY0(zap_add(mos, dd->dd_object, DD_FIELD_CRYPTO_KEY_OBJ,
2257 sizeof (uint64_t), 1, &dd->dd_crypto_obj, tx));
b5256303
TC
2258
2259 /*
2260 * Set the keylocation to prompt by default. If keylocation
b0918402 2261 * has been provided via the properties, this will be overridden
b5256303
TC
2262 * later.
2263 */
2264 dsl_prop_set_sync_impl(ds,
2265 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2266 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1,
2267 keylocation, tx);
2268
b0918402 2269 rddobj = dd->dd_object;
b5256303 2270 } else {
b0918402 2271 VERIFY0(dsl_dir_get_encryption_root_ddobj(dd, &rddobj));
b5256303
TC
2272 }
2273
2274 /* sync the key data to the ZAP object on disk */
b0918402 2275 dsl_crypto_key_sync_impl(mos, dd->dd_crypto_obj, crypt,
b5256303
TC
2276 rddobj, guid, iv, mac, keydata, hmac_keydata, keyformat, salt,
2277 iters, tx);
b0918402 2278}
b5256303 2279
b0918402
TC
2280int
2281dsl_crypto_recv_key_check(void *arg, dmu_tx_t *tx)
2282{
2283 int ret;
2284 dsl_crypto_recv_key_arg_t *dcrka = arg;
2285 dsl_dataset_t *ds = NULL;
2286
2287 ret = dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_dsobj,
2288 FTAG, &ds);
2289 if (ret != 0)
2290 goto error;
2291
2292 ret = dsl_crypto_recv_raw_objset_check(ds,
2293 dcrka->dcrka_ostype, dcrka->dcrka_nvl, tx);
2294 if (ret != 0)
2295 goto error;
2296
2297 /*
2298 * We run this check even if we won't be doing this part of
2299 * the receive now so that we don't make the user wait until
2300 * the receive finishes to fail.
2301 */
2302 ret = dsl_crypto_recv_raw_key_check(ds, dcrka->dcrka_nvl, tx);
2303 if (ret != 0)
2304 goto error;
2305
2306 dsl_dataset_rele(ds, FTAG);
2307 return (0);
2308
2309error:
2310 if (ds != NULL)
2311 dsl_dataset_rele(ds, FTAG);
2312 return (ret);
2313}
2314
2315void
2316dsl_crypto_recv_key_sync(void *arg, dmu_tx_t *tx)
2317{
2318 dsl_crypto_recv_key_arg_t *dcrka = arg;
2319 dsl_dataset_t *ds;
2320
2321 VERIFY0(dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_dsobj,
2322 FTAG, &ds));
2323 dsl_crypto_recv_raw_objset_sync(ds, dcrka->dcrka_ostype,
2324 dcrka->dcrka_nvl, tx);
2325 if (dcrka->dcrka_do_key)
2326 dsl_crypto_recv_raw_key_sync(ds, dcrka->dcrka_nvl, tx);
b5256303
TC
2327 dsl_dataset_rele(ds, FTAG);
2328}
2329
2330/*
2331 * This function is used to sync an nvlist representing a DSL Crypto Key and
2332 * the associated encryption parameters. The key will be written exactly as is
2333 * without wrapping it.
2334 */
2335int
b0918402
TC
2336dsl_crypto_recv_raw(const char *poolname, uint64_t dsobj,
2337 dmu_objset_type_t ostype, nvlist_t *nvl, boolean_t do_key)
b5256303
TC
2338{
2339 dsl_crypto_recv_key_arg_t dcrka;
2340
2341 dcrka.dcrka_dsobj = dsobj;
b5256303 2342 dcrka.dcrka_ostype = ostype;
b0918402
TC
2343 dcrka.dcrka_nvl = nvl;
2344 dcrka.dcrka_do_key = do_key;
b5256303
TC
2345
2346 return (dsl_sync_task(poolname, dsl_crypto_recv_key_check,
2347 dsl_crypto_recv_key_sync, &dcrka, 1, ZFS_SPACE_CHECK_NORMAL));
2348}
2349
2350int
2351dsl_crypto_populate_key_nvlist(dsl_dataset_t *ds, nvlist_t **nvl_out)
2352{
2353 int ret;
2354 objset_t *os;
2355 dnode_t *mdn;
2356 uint64_t rddobj;
2357 nvlist_t *nvl = NULL;
2358 uint64_t dckobj = ds->ds_dir->dd_crypto_obj;
2637dda8 2359 dsl_dir_t *rdd = NULL;
b5256303
TC
2360 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2361 objset_t *mos = dp->dp_meta_objset;
ae76f45c
TC
2362 uint64_t crypt = 0, guid = 0, format = 0;
2363 uint64_t iters = 0, salt = 0, version = 0;
b5256303
TC
2364 uint8_t raw_keydata[MASTER_KEY_MAX_LEN];
2365 uint8_t raw_hmac_keydata[SHA512_HMAC_KEYLEN];
2366 uint8_t iv[WRAPPING_IV_LEN];
2367 uint8_t mac[WRAPPING_MAC_LEN];
2368
2369 ASSERT(dckobj != 0);
2370
2371 VERIFY0(dmu_objset_from_ds(ds, &os));
2372 mdn = DMU_META_DNODE(os);
2373
2374 ret = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
2375 if (ret != 0)
2376 goto error;
2377
2378 /* lookup values from the DSL Crypto Key */
b5256303
TC
2379 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
2380 &crypt);
2381 if (ret != 0)
2382 goto error;
2383
2384 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1, &guid);
2385 if (ret != 0)
2386 goto error;
2387
2388 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
2389 MASTER_KEY_MAX_LEN, raw_keydata);
2390 if (ret != 0)
2391 goto error;
2392
2393 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
2394 SHA512_HMAC_KEYLEN, raw_hmac_keydata);
2395 if (ret != 0)
2396 goto error;
2397
2398 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
2399 iv);
2400 if (ret != 0)
2401 goto error;
2402
2403 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
2404 mac);
2405 if (ret != 0)
2406 goto error;
2407
ae76f45c
TC
2408 /*
2409 * We don't support raw sends of legacy on-disk formats. See the
2410 * comment in dsl_crypto_recv_key_check() for details.
2411 */
2412 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_VERSION, 8, 1, &version);
2413 if (ret != 0 || version != ZIO_CRYPT_KEY_CURRENT_VERSION) {
2414 dp->dp_spa->spa_errata = ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
2415 ret = SET_ERROR(ENOTSUP);
2416 goto error;
2417 }
2418
2637dda8
TC
2419 /*
2420 * Lookup wrapping key properties. An early version of the code did
2421 * not correctly add these values to the wrapping key or the DSL
2422 * Crypto Key on disk for non encryption roots, so to be safe we
2423 * always take the slightly circuitous route of looking it up from
2424 * the encryption root's key.
2425 */
2426 ret = dsl_dir_get_encryption_root_ddobj(ds->ds_dir, &rddobj);
b5256303
TC
2427 if (ret != 0)
2428 goto error;
2429
2637dda8
TC
2430 dsl_pool_config_enter(dp, FTAG);
2431
2432 ret = dsl_dir_hold_obj(dp, rddobj, NULL, FTAG, &rdd);
2433 if (ret != 0)
2434 goto error_unlock;
2435
2436 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
2437 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &format);
2438 if (ret != 0)
2439 goto error_unlock;
2440
b5256303 2441 if (format == ZFS_KEYFORMAT_PASSPHRASE) {
2637dda8 2442 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
b5256303
TC
2443 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &iters);
2444 if (ret != 0)
2637dda8 2445 goto error_unlock;
b5256303 2446
2637dda8 2447 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
b5256303
TC
2448 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &salt);
2449 if (ret != 0)
2637dda8 2450 goto error_unlock;
b5256303
TC
2451 }
2452
2637dda8
TC
2453 dsl_dir_rele(rdd, FTAG);
2454 dsl_pool_config_exit(dp, FTAG);
2455
b5256303
TC
2456 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE, crypt);
2457 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_GUID, guid);
ae76f45c 2458 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_VERSION, version);
b5256303
TC
2459 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2460 raw_keydata, MASTER_KEY_MAX_LEN));
2461 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2462 raw_hmac_keydata, SHA512_HMAC_KEYLEN));
2463 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_IV, iv,
2464 WRAPPING_IV_LEN));
2465 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, mac,
2466 WRAPPING_MAC_LEN));
2467 VERIFY0(nvlist_add_uint8_array(nvl, "portable_mac",
2468 os->os_phys->os_portable_mac, ZIO_OBJSET_MAC_LEN));
2469 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_KEYFORMAT), format);
2470 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), iters);
2471 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), salt);
2472 fnvlist_add_uint64(nvl, "mdn_checksum", mdn->dn_checksum);
2473 fnvlist_add_uint64(nvl, "mdn_compress", mdn->dn_compress);
2474 fnvlist_add_uint64(nvl, "mdn_nlevels", mdn->dn_nlevels);
2475 fnvlist_add_uint64(nvl, "mdn_blksz", mdn->dn_datablksz);
2476 fnvlist_add_uint64(nvl, "mdn_indblkshift", mdn->dn_indblkshift);
2477 fnvlist_add_uint64(nvl, "mdn_nblkptr", mdn->dn_nblkptr);
ae76f45c 2478 fnvlist_add_uint64(nvl, "mdn_maxblkid", mdn->dn_maxblkid);
b5256303
TC
2479
2480 *nvl_out = nvl;
2481 return (0);
2482
2637dda8
TC
2483error_unlock:
2484 dsl_pool_config_exit(dp, FTAG);
b5256303 2485error:
2637dda8
TC
2486 if (rdd != NULL)
2487 dsl_dir_rele(rdd, FTAG);
b5256303
TC
2488 nvlist_free(nvl);
2489
2490 *nvl_out = NULL;
2491 return (ret);
2492}
2493
2494uint64_t
2495dsl_crypto_key_create_sync(uint64_t crypt, dsl_wrapping_key_t *wkey,
2496 dmu_tx_t *tx)
2497{
2498 dsl_crypto_key_t dck;
ae76f45c
TC
2499 uint64_t version = ZIO_CRYPT_KEY_CURRENT_VERSION;
2500 uint64_t one = 1ULL;
b5256303
TC
2501
2502 ASSERT(dmu_tx_is_syncing(tx));
2503 ASSERT3U(crypt, <, ZIO_CRYPT_FUNCTIONS);
2504 ASSERT3U(crypt, >, ZIO_CRYPT_OFF);
2505
2506 /* create the DSL Crypto Key ZAP object */
2507 dck.dck_obj = zap_create(tx->tx_pool->dp_meta_objset,
2508 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
2509
2510 /* fill in the key (on the stack) and sync it to disk */
2511 dck.dck_wkey = wkey;
2512 VERIFY0(zio_crypt_key_init(crypt, &dck.dck_key));
2513
2514 dsl_crypto_key_sync(&dck, tx);
2515 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, dck.dck_obj,
2516 DSL_CRYPTO_KEY_REFCOUNT, sizeof (uint64_t), 1, &one, tx));
ae76f45c
TC
2517 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, dck.dck_obj,
2518 DSL_CRYPTO_KEY_VERSION, sizeof (uint64_t), 1, &version, tx));
b5256303
TC
2519
2520 zio_crypt_key_destroy(&dck.dck_key);
2521 bzero(&dck.dck_key, sizeof (zio_crypt_key_t));
2522
2523 return (dck.dck_obj);
2524}
2525
2526uint64_t
2527dsl_crypto_key_clone_sync(dsl_dir_t *origindd, dmu_tx_t *tx)
2528{
2529 objset_t *mos = tx->tx_pool->dp_meta_objset;
2530
2531 ASSERT(dmu_tx_is_syncing(tx));
2532
2533 VERIFY0(zap_increment(mos, origindd->dd_crypto_obj,
2534 DSL_CRYPTO_KEY_REFCOUNT, 1, tx));
2535
2536 return (origindd->dd_crypto_obj);
2537}
2538
2539void
2540dsl_crypto_key_destroy_sync(uint64_t dckobj, dmu_tx_t *tx)
2541{
2542 objset_t *mos = tx->tx_pool->dp_meta_objset;
2543 uint64_t refcnt;
2544
2545 /* Decrement the refcount, destroy if this is the last reference */
2546 VERIFY0(zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_REFCOUNT,
2547 sizeof (uint64_t), 1, &refcnt));
2548
2549 if (refcnt != 1) {
2550 VERIFY0(zap_increment(mos, dckobj, DSL_CRYPTO_KEY_REFCOUNT,
2551 -1, tx));
2552 } else {
2553 VERIFY0(zap_destroy(mos, dckobj, tx));
2554 }
2555}
2556
2557void
2558dsl_dataset_crypt_stats(dsl_dataset_t *ds, nvlist_t *nv)
2559{
2560 uint64_t intval;
2561 dsl_dir_t *dd = ds->ds_dir;
2562 dsl_dir_t *enc_root;
2563 char buf[ZFS_MAX_DATASET_NAME_LEN];
2564
2565 if (dd->dd_crypto_obj == 0)
2566 return;
2567
2568 intval = dsl_dataset_get_keystatus(dd);
2569 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEYSTATUS, intval);
2570
2571 if (dsl_dir_get_crypt(dd, &intval) == 0)
2572 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_ENCRYPTION, intval);
2573 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2574 DSL_CRYPTO_KEY_GUID, 8, 1, &intval) == 0) {
2575 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEY_GUID, intval);
2576 }
2577 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2578 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &intval) == 0) {
2579 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEYFORMAT, intval);
2580 }
2581 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2582 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &intval) == 0) {
2583 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_PBKDF2_SALT, intval);
2584 }
2585 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2586 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &intval) == 0) {
2587 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_PBKDF2_ITERS, intval);
2588 }
2589
2590 if (dsl_dir_get_encryption_root_ddobj(dd, &intval) == 0) {
2591 VERIFY0(dsl_dir_hold_obj(dd->dd_pool, intval, NULL, FTAG,
2592 &enc_root));
2593 dsl_dir_name(enc_root, buf);
2594 dsl_dir_rele(enc_root, FTAG);
2595 dsl_prop_nvlist_add_string(nv, ZFS_PROP_ENCRYPTION_ROOT, buf);
2596 }
2597}
2598
2599int
2600spa_crypt_get_salt(spa_t *spa, uint64_t dsobj, uint8_t *salt)
2601{
2602 int ret;
2603 dsl_crypto_key_t *dck = NULL;
2604
2605 /* look up the key from the spa's keystore */
2606 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2607 if (ret != 0)
2608 goto error;
2609
2610 ret = zio_crypt_key_get_salt(&dck->dck_key, salt);
2611 if (ret != 0)
2612 goto error;
2613
2614 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2615 return (0);
2616
2617error:
2618 if (dck != NULL)
2619 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2620 return (ret);
2621}
2622
2623/*
2624 * Objset blocks are a special case for MAC generation. These blocks have 2
2625 * 256-bit MACs which are embedded within the block itself, rather than a
2626 * single 128 bit MAC. As a result, this function handles encoding and decoding
2627 * the MACs on its own, unlike other functions in this file.
2628 */
2629int
2630spa_do_crypt_objset_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj,
2631 abd_t *abd, uint_t datalen, boolean_t byteswap)
2632{
2633 int ret;
2634 dsl_crypto_key_t *dck = NULL;
2635 void *buf = abd_borrow_buf_copy(abd, datalen);
2636 objset_phys_t *osp = buf;
2637 uint8_t portable_mac[ZIO_OBJSET_MAC_LEN];
2638 uint8_t local_mac[ZIO_OBJSET_MAC_LEN];
2639
2640 /* look up the key from the spa's keystore */
2641 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2642 if (ret != 0)
2643 goto error;
2644
2645 /* calculate both HMACs */
2646 ret = zio_crypt_do_objset_hmacs(&dck->dck_key, buf, datalen,
2647 byteswap, portable_mac, local_mac);
2648 if (ret != 0)
2649 goto error;
2650
2651 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2652
2653 /* if we are generating encode the HMACs in the objset_phys_t */
2654 if (generate) {
2655 bcopy(portable_mac, osp->os_portable_mac, ZIO_OBJSET_MAC_LEN);
2656 bcopy(local_mac, osp->os_local_mac, ZIO_OBJSET_MAC_LEN);
2657 abd_return_buf_copy(abd, buf, datalen);
2658 return (0);
2659 }
2660
2661 if (bcmp(portable_mac, osp->os_portable_mac, ZIO_OBJSET_MAC_LEN) != 0 ||
2662 bcmp(local_mac, osp->os_local_mac, ZIO_OBJSET_MAC_LEN) != 0) {
2663 abd_return_buf(abd, buf, datalen);
2664 return (SET_ERROR(ECKSUM));
2665 }
2666
2667 abd_return_buf(abd, buf, datalen);
2668
2669 return (0);
2670
2671error:
2672 if (dck != NULL)
2673 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2674 abd_return_buf(abd, buf, datalen);
2675 return (ret);
2676}
2677
2678int
2679spa_do_crypt_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj, abd_t *abd,
2680 uint_t datalen, uint8_t *mac)
2681{
2682 int ret;
2683 dsl_crypto_key_t *dck = NULL;
2684 uint8_t *buf = abd_borrow_buf_copy(abd, datalen);
2685 uint8_t digestbuf[ZIO_DATA_MAC_LEN];
2686
2687 /* look up the key from the spa's keystore */
2688 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2689 if (ret != 0)
2690 goto error;
2691
2692 /* perform the hmac */
4807c0ba
TC
2693 ret = zio_crypt_do_hmac(&dck->dck_key, buf, datalen,
2694 digestbuf, ZIO_DATA_MAC_LEN);
b5256303
TC
2695 if (ret != 0)
2696 goto error;
2697
2698 abd_return_buf(abd, buf, datalen);
2699 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2700
2701 /*
2702 * Truncate and fill in mac buffer if we were asked to generate a MAC.
2703 * Otherwise verify that the MAC matched what we expected.
2704 */
2705 if (generate) {
2706 bcopy(digestbuf, mac, ZIO_DATA_MAC_LEN);
2707 return (0);
2708 }
2709
2710 if (bcmp(digestbuf, mac, ZIO_DATA_MAC_LEN) != 0)
2711 return (SET_ERROR(ECKSUM));
2712
2713 return (0);
2714
2715error:
2716 if (dck != NULL)
2717 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2718 abd_return_buf(abd, buf, datalen);
2719 return (ret);
2720}
2721
2722/*
2723 * This function serves as a multiplexer for encryption and decryption of
2724 * all blocks (except the L2ARC). For encryption, it will populate the IV,
2725 * salt, MAC, and cabd (the ciphertext). On decryption it will simply use
2726 * these fields to populate pabd (the plaintext).
2727 */
2728int
be9a5c35
TC
2729spa_do_crypt_abd(boolean_t encrypt, spa_t *spa, const zbookmark_phys_t *zb,
2730 dmu_object_type_t ot, boolean_t dedup, boolean_t bswap, uint8_t *salt,
2731 uint8_t *iv, uint8_t *mac, uint_t datalen, abd_t *pabd, abd_t *cabd,
2732 boolean_t *no_crypt)
b5256303
TC
2733{
2734 int ret;
b5256303
TC
2735 dsl_crypto_key_t *dck = NULL;
2736 uint8_t *plainbuf = NULL, *cipherbuf = NULL;
2737
2738 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
b5256303
TC
2739
2740 /* look up the key from the spa's keystore */
be9a5c35
TC
2741 ret = spa_keystore_lookup_key(spa, zb->zb_objset, FTAG, &dck);
2742 if (ret != 0) {
2743 ret = SET_ERROR(EACCES);
b5256303 2744 return (ret);
be9a5c35 2745 }
b5256303
TC
2746
2747 if (encrypt) {
2748 plainbuf = abd_borrow_buf_copy(pabd, datalen);
2749 cipherbuf = abd_borrow_buf(cabd, datalen);
2750 } else {
2751 plainbuf = abd_borrow_buf(pabd, datalen);
2752 cipherbuf = abd_borrow_buf_copy(cabd, datalen);
2753 }
2754
2755 /*
2756 * Both encryption and decryption functions need a salt for key
2757 * generation and an IV. When encrypting a non-dedup block, we
2758 * generate the salt and IV randomly to be stored by the caller. Dedup
2759 * blocks perform a (more expensive) HMAC of the plaintext to obtain
2760 * the salt and the IV. ZIL blocks have their salt and IV generated
2761 * at allocation time in zio_alloc_zil(). On decryption, we simply use
2762 * the provided values.
2763 */
be9a5c35 2764 if (encrypt && ot != DMU_OT_INTENT_LOG && !dedup) {
b5256303
TC
2765 ret = zio_crypt_key_get_salt(&dck->dck_key, salt);
2766 if (ret != 0)
2767 goto error;
2768
2769 ret = zio_crypt_generate_iv(iv);
2770 if (ret != 0)
2771 goto error;
be9a5c35 2772 } else if (encrypt && dedup) {
b5256303
TC
2773 ret = zio_crypt_generate_iv_salt_dedup(&dck->dck_key,
2774 plainbuf, datalen, iv, salt);
2775 if (ret != 0)
2776 goto error;
2777 }
2778
2779 /* call lower level function to perform encryption / decryption */
be9a5c35
TC
2780 ret = zio_do_crypt_data(encrypt, &dck->dck_key, ot, bswap, salt, iv,
2781 mac, datalen, plainbuf, cipherbuf, no_crypt);
2782
2783 /*
2784 * Handle injected decryption faults. Unfortunately, we cannot inject
2785 * faults for dnode blocks because we might trigger the panic in
2786 * dbuf_prepare_encrypted_dnode_leaf(), which exists because syncing
2787 * context is not prepared to handle malicious decryption failures.
2788 */
2789 if (zio_injection_enabled && !encrypt && ot != DMU_OT_DNODE && ret == 0)
2790 ret = zio_handle_decrypt_injection(spa, zb, ot, ECKSUM);
b5256303
TC
2791 if (ret != 0)
2792 goto error;
2793
2794 if (encrypt) {
2795 abd_return_buf(pabd, plainbuf, datalen);
2796 abd_return_buf_copy(cabd, cipherbuf, datalen);
2797 } else {
2798 abd_return_buf_copy(pabd, plainbuf, datalen);
2799 abd_return_buf(cabd, cipherbuf, datalen);
2800 }
2801
2802 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2803
2804 return (0);
2805
2806error:
2807 if (encrypt) {
2808 /* zero out any state we might have changed while encrypting */
2809 bzero(salt, ZIO_DATA_SALT_LEN);
2810 bzero(iv, ZIO_DATA_IV_LEN);
2811 bzero(mac, ZIO_DATA_MAC_LEN);
2812 abd_return_buf(pabd, plainbuf, datalen);
2813 abd_return_buf_copy(cabd, cipherbuf, datalen);
2814 } else {
2815 abd_return_buf_copy(pabd, plainbuf, datalen);
2816 abd_return_buf(cabd, cipherbuf, datalen);
2817 }
2818
4807c0ba 2819 spa_keystore_dsl_key_rele(spa, dck, FTAG);
b5256303
TC
2820
2821 return (ret);
2822}