Add missing zfs_refcount_destroy() in key_mapping_rele()
[zfs.git] / module / zfs / dsl_crypt.c
blob581876d7778e9203abcbbdf0442dcc84adebc6c0
1 /*
2 * CDDL HEADER START
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.
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.
13 * CDDL HEADER END
17 * Copyright (c) 2017, Datto, Inc. All rights reserved.
18 * Copyright (c) 2018 by Delphix. All rights reserved.
21 #include <sys/dsl_crypt.h>
22 #include <sys/dsl_pool.h>
23 #include <sys/zap.h>
24 #include <sys/zil.h>
25 #include <sys/dsl_dir.h>
26 #include <sys/dsl_prop.h>
27 #include <sys/spa_impl.h>
28 #include <sys/dmu_objset.h>
29 #include <sys/zvol.h>
32 * This file's primary purpose is for managing master encryption keys in
33 * memory and on disk. For more info on how these keys are used, see the
34 * block comment in zio_crypt.c.
36 * All master keys are stored encrypted on disk in the form of the DSL
37 * Crypto Key ZAP object. The binary key data in this object is always
38 * randomly generated and is encrypted with the user's wrapping key. This
39 * layer of indirection allows the user to change their key without
40 * needing to re-encrypt the entire dataset. The ZAP also holds on to the
41 * (non-encrypted) encryption algorithm identifier, IV, and MAC needed to
42 * safely decrypt the master key. For more info on the user's key see the
43 * block comment in libzfs_crypto.c
45 * In-memory encryption keys are managed through the spa_keystore. The
46 * keystore consists of 3 AVL trees, which are as follows:
48 * The Wrapping Key Tree:
49 * The wrapping key (wkey) tree stores the user's keys that are fed into the
50 * kernel through 'zfs load-key' and related commands. Datasets inherit their
51 * parent's wkey by default, so these structures are refcounted. The wrapping
52 * keys remain in memory until they are explicitly unloaded (with
53 * "zfs unload-key"). Unloading is only possible when no datasets are using
54 * them (refcount=0).
56 * The DSL Crypto Key Tree:
57 * The DSL Crypto Keys (DCK) are the in-memory representation of decrypted
58 * master keys. They are used by the functions in zio_crypt.c to perform
59 * encryption, decryption, and authentication. Snapshots and clones of a given
60 * dataset will share a DSL Crypto Key, so they are also refcounted. Once the
61 * refcount on a key hits zero, it is immediately zeroed out and freed.
63 * The Crypto Key Mapping Tree:
64 * The zio layer needs to lookup master keys by their dataset object id. Since
65 * the DSL Crypto Keys can belong to multiple datasets, we maintain a tree of
66 * dsl_key_mapping_t's which essentially just map the dataset object id to its
67 * appropriate DSL Crypto Key. The management for creating and destroying these
68 * mappings hooks into the code for owning and disowning datasets. Usually,
69 * there will only be one active dataset owner, but there are times
70 * (particularly during dataset creation and destruction) when this may not be
71 * true or the dataset may not be initialized enough to own. As a result, this
72 * object is also refcounted.
76 * This tunable allows datasets to be raw received even if the stream does
77 * not include IVset guids or if the guids don't match. This is used as part
78 * of the resolution for ZPOOL_ERRATA_ZOL_8308_ENCRYPTION.
80 int zfs_disable_ivset_guid_check = 0;
82 static void
83 dsl_wrapping_key_hold(dsl_wrapping_key_t *wkey, void *tag)
85 (void) zfs_refcount_add(&wkey->wk_refcnt, tag);
88 static void
89 dsl_wrapping_key_rele(dsl_wrapping_key_t *wkey, void *tag)
91 (void) zfs_refcount_remove(&wkey->wk_refcnt, tag);
94 static void
95 dsl_wrapping_key_free(dsl_wrapping_key_t *wkey)
97 ASSERT0(zfs_refcount_count(&wkey->wk_refcnt));
99 if (wkey->wk_key.ck_data) {
100 bzero(wkey->wk_key.ck_data,
101 CRYPTO_BITS2BYTES(wkey->wk_key.ck_length));
102 kmem_free(wkey->wk_key.ck_data,
103 CRYPTO_BITS2BYTES(wkey->wk_key.ck_length));
106 zfs_refcount_destroy(&wkey->wk_refcnt);
107 kmem_free(wkey, sizeof (dsl_wrapping_key_t));
110 static int
111 dsl_wrapping_key_create(uint8_t *wkeydata, zfs_keyformat_t keyformat,
112 uint64_t salt, uint64_t iters, dsl_wrapping_key_t **wkey_out)
114 int ret;
115 dsl_wrapping_key_t *wkey;
117 /* allocate the wrapping key */
118 wkey = kmem_alloc(sizeof (dsl_wrapping_key_t), KM_SLEEP);
119 if (!wkey)
120 return (SET_ERROR(ENOMEM));
122 /* allocate and initialize the underlying crypto key */
123 wkey->wk_key.ck_data = kmem_alloc(WRAPPING_KEY_LEN, KM_SLEEP);
124 if (!wkey->wk_key.ck_data) {
125 ret = ENOMEM;
126 goto error;
129 wkey->wk_key.ck_format = CRYPTO_KEY_RAW;
130 wkey->wk_key.ck_length = CRYPTO_BYTES2BITS(WRAPPING_KEY_LEN);
131 bcopy(wkeydata, wkey->wk_key.ck_data, WRAPPING_KEY_LEN);
133 /* initialize the rest of the struct */
134 zfs_refcount_create(&wkey->wk_refcnt);
135 wkey->wk_keyformat = keyformat;
136 wkey->wk_salt = salt;
137 wkey->wk_iters = iters;
139 *wkey_out = wkey;
140 return (0);
142 error:
143 dsl_wrapping_key_free(wkey);
145 *wkey_out = NULL;
146 return (ret);
150 dsl_crypto_params_create_nvlist(dcp_cmd_t cmd, nvlist_t *props,
151 nvlist_t *crypto_args, dsl_crypto_params_t **dcp_out)
153 int ret;
154 uint64_t crypt = ZIO_CRYPT_INHERIT;
155 uint64_t keyformat = ZFS_KEYFORMAT_NONE;
156 uint64_t salt = 0, iters = 0;
157 dsl_crypto_params_t *dcp = NULL;
158 dsl_wrapping_key_t *wkey = NULL;
159 uint8_t *wkeydata = NULL;
160 uint_t wkeydata_len = 0;
161 char *keylocation = NULL;
163 dcp = kmem_zalloc(sizeof (dsl_crypto_params_t), KM_SLEEP);
164 if (!dcp) {
165 ret = SET_ERROR(ENOMEM);
166 goto error;
169 dcp->cp_cmd = cmd;
171 /* get relevant arguments from the nvlists */
172 if (props != NULL) {
173 (void) nvlist_lookup_uint64(props,
174 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &crypt);
175 (void) nvlist_lookup_uint64(props,
176 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);
177 (void) nvlist_lookup_string(props,
178 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
179 (void) nvlist_lookup_uint64(props,
180 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), &salt);
181 (void) nvlist_lookup_uint64(props,
182 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);
184 dcp->cp_crypt = crypt;
187 if (crypto_args != NULL) {
188 (void) nvlist_lookup_uint8_array(crypto_args, "wkeydata",
189 &wkeydata, &wkeydata_len);
192 /* check for valid command */
193 if (dcp->cp_cmd >= DCP_CMD_MAX) {
194 ret = SET_ERROR(EINVAL);
195 goto error;
196 } else {
197 dcp->cp_cmd = cmd;
200 /* check for valid crypt */
201 if (dcp->cp_crypt >= ZIO_CRYPT_FUNCTIONS) {
202 ret = SET_ERROR(EINVAL);
203 goto error;
204 } else {
205 dcp->cp_crypt = crypt;
208 /* check for valid keyformat */
209 if (keyformat >= ZFS_KEYFORMAT_FORMATS) {
210 ret = SET_ERROR(EINVAL);
211 goto error;
214 /* check for a valid keylocation (of any kind) and copy it in */
215 if (keylocation != NULL) {
216 if (!zfs_prop_valid_keylocation(keylocation, B_FALSE)) {
217 ret = SET_ERROR(EINVAL);
218 goto error;
221 dcp->cp_keylocation = spa_strdup(keylocation);
224 /* check wrapping key length, if given */
225 if (wkeydata != NULL && wkeydata_len != WRAPPING_KEY_LEN) {
226 ret = SET_ERROR(EINVAL);
227 goto error;
230 /* if the user asked for the default crypt, determine that now */
231 if (dcp->cp_crypt == ZIO_CRYPT_ON)
232 dcp->cp_crypt = ZIO_CRYPT_ON_VALUE;
234 /* create the wrapping key from the raw data */
235 if (wkeydata != NULL) {
236 /* create the wrapping key with the verified parameters */
237 ret = dsl_wrapping_key_create(wkeydata, keyformat, salt,
238 iters, &wkey);
239 if (ret != 0)
240 goto error;
242 dcp->cp_wkey = wkey;
246 * Remove the encryption properties from the nvlist since they are not
247 * maintained through the DSL.
249 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_ENCRYPTION));
250 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_KEYFORMAT));
251 (void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT));
252 (void) nvlist_remove_all(props,
253 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS));
255 *dcp_out = dcp;
257 return (0);
259 error:
260 if (wkey != NULL)
261 dsl_wrapping_key_free(wkey);
262 if (dcp != NULL)
263 kmem_free(dcp, sizeof (dsl_crypto_params_t));
265 *dcp_out = NULL;
266 return (ret);
269 void
270 dsl_crypto_params_free(dsl_crypto_params_t *dcp, boolean_t unload)
272 if (dcp == NULL)
273 return;
275 if (dcp->cp_keylocation != NULL)
276 spa_strfree(dcp->cp_keylocation);
277 if (unload && dcp->cp_wkey != NULL)
278 dsl_wrapping_key_free(dcp->cp_wkey);
280 kmem_free(dcp, sizeof (dsl_crypto_params_t));
283 static int
284 spa_crypto_key_compare(const void *a, const void *b)
286 const dsl_crypto_key_t *dcka = a;
287 const dsl_crypto_key_t *dckb = b;
289 if (dcka->dck_obj < dckb->dck_obj)
290 return (-1);
291 if (dcka->dck_obj > dckb->dck_obj)
292 return (1);
293 return (0);
296 static int
297 spa_key_mapping_compare(const void *a, const void *b)
299 const dsl_key_mapping_t *kma = a;
300 const dsl_key_mapping_t *kmb = b;
302 if (kma->km_dsobj < kmb->km_dsobj)
303 return (-1);
304 if (kma->km_dsobj > kmb->km_dsobj)
305 return (1);
306 return (0);
309 static int
310 spa_wkey_compare(const void *a, const void *b)
312 const dsl_wrapping_key_t *wka = a;
313 const dsl_wrapping_key_t *wkb = b;
315 if (wka->wk_ddobj < wkb->wk_ddobj)
316 return (-1);
317 if (wka->wk_ddobj > wkb->wk_ddobj)
318 return (1);
319 return (0);
322 void
323 spa_keystore_init(spa_keystore_t *sk)
325 rw_init(&sk->sk_dk_lock, NULL, RW_DEFAULT, NULL);
326 rw_init(&sk->sk_km_lock, NULL, RW_DEFAULT, NULL);
327 rw_init(&sk->sk_wkeys_lock, NULL, RW_DEFAULT, NULL);
328 avl_create(&sk->sk_dsl_keys, spa_crypto_key_compare,
329 sizeof (dsl_crypto_key_t),
330 offsetof(dsl_crypto_key_t, dck_avl_link));
331 avl_create(&sk->sk_key_mappings, spa_key_mapping_compare,
332 sizeof (dsl_key_mapping_t),
333 offsetof(dsl_key_mapping_t, km_avl_link));
334 avl_create(&sk->sk_wkeys, spa_wkey_compare, sizeof (dsl_wrapping_key_t),
335 offsetof(dsl_wrapping_key_t, wk_avl_link));
338 void
339 spa_keystore_fini(spa_keystore_t *sk)
341 dsl_wrapping_key_t *wkey;
342 void *cookie = NULL;
344 ASSERT(avl_is_empty(&sk->sk_dsl_keys));
345 ASSERT(avl_is_empty(&sk->sk_key_mappings));
347 while ((wkey = avl_destroy_nodes(&sk->sk_wkeys, &cookie)) != NULL)
348 dsl_wrapping_key_free(wkey);
350 avl_destroy(&sk->sk_wkeys);
351 avl_destroy(&sk->sk_key_mappings);
352 avl_destroy(&sk->sk_dsl_keys);
353 rw_destroy(&sk->sk_wkeys_lock);
354 rw_destroy(&sk->sk_km_lock);
355 rw_destroy(&sk->sk_dk_lock);
358 static int
359 dsl_dir_get_encryption_root_ddobj(dsl_dir_t *dd, uint64_t *rddobj)
361 if (dd->dd_crypto_obj == 0)
362 return (SET_ERROR(ENOENT));
364 return (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
365 DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1, rddobj));
369 dsl_dir_get_encryption_version(dsl_dir_t *dd, uint64_t *version)
371 *version = 0;
373 if (dd->dd_crypto_obj == 0)
374 return (SET_ERROR(ENOENT));
376 /* version 0 is implied by ENOENT */
377 (void) zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
378 DSL_CRYPTO_KEY_VERSION, 8, 1, version);
380 return (0);
383 boolean_t
384 dsl_dir_incompatible_encryption_version(dsl_dir_t *dd)
386 int ret;
387 uint64_t version = 0;
389 ret = dsl_dir_get_encryption_version(dd, &version);
390 if (ret != 0)
391 return (B_FALSE);
393 return (version != ZIO_CRYPT_KEY_CURRENT_VERSION);
396 static int
397 spa_keystore_wkey_hold_ddobj_impl(spa_t *spa, uint64_t ddobj,
398 void *tag, dsl_wrapping_key_t **wkey_out)
400 int ret;
401 dsl_wrapping_key_t search_wkey;
402 dsl_wrapping_key_t *found_wkey;
404 ASSERT(RW_LOCK_HELD(&spa->spa_keystore.sk_wkeys_lock));
406 /* init the search wrapping key */
407 search_wkey.wk_ddobj = ddobj;
409 /* lookup the wrapping key */
410 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, &search_wkey, NULL);
411 if (!found_wkey) {
412 ret = SET_ERROR(ENOENT);
413 goto error;
416 /* increment the refcount */
417 dsl_wrapping_key_hold(found_wkey, tag);
419 *wkey_out = found_wkey;
420 return (0);
422 error:
423 *wkey_out = NULL;
424 return (ret);
427 static int
428 spa_keystore_wkey_hold_dd(spa_t *spa, dsl_dir_t *dd, void *tag,
429 dsl_wrapping_key_t **wkey_out)
431 int ret;
432 dsl_wrapping_key_t *wkey;
433 uint64_t rddobj;
434 boolean_t locked = B_FALSE;
436 if (!RW_WRITE_HELD(&spa->spa_keystore.sk_wkeys_lock)) {
437 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_READER);
438 locked = B_TRUE;
441 /* get the ddobj that the keylocation property was inherited from */
442 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
443 if (ret != 0)
444 goto error;
446 /* lookup the wkey in the avl tree */
447 ret = spa_keystore_wkey_hold_ddobj_impl(spa, rddobj, tag, &wkey);
448 if (ret != 0)
449 goto error;
451 /* unlock the wkey tree if we locked it */
452 if (locked)
453 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
455 *wkey_out = wkey;
456 return (0);
458 error:
459 if (locked)
460 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
462 *wkey_out = NULL;
463 return (ret);
467 dsl_crypto_can_set_keylocation(const char *dsname, const char *keylocation)
469 int ret = 0;
470 dsl_dir_t *dd = NULL;
471 dsl_pool_t *dp = NULL;
472 uint64_t rddobj;
474 /* hold the dsl dir */
475 ret = dsl_pool_hold(dsname, FTAG, &dp);
476 if (ret != 0)
477 goto out;
479 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
480 if (ret != 0) {
481 dd = NULL;
482 goto out;
485 /* if dd is not encrypted, the value may only be "none" */
486 if (dd->dd_crypto_obj == 0) {
487 if (strcmp(keylocation, "none") != 0) {
488 ret = SET_ERROR(EACCES);
489 goto out;
492 ret = 0;
493 goto out;
496 /* check for a valid keylocation for encrypted datasets */
497 if (!zfs_prop_valid_keylocation(keylocation, B_TRUE)) {
498 ret = SET_ERROR(EINVAL);
499 goto out;
502 /* check that this is an encryption root */
503 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
504 if (ret != 0)
505 goto out;
507 if (rddobj != dd->dd_object) {
508 ret = SET_ERROR(EACCES);
509 goto out;
512 dsl_dir_rele(dd, FTAG);
513 dsl_pool_rele(dp, FTAG);
515 return (0);
517 out:
518 if (dd != NULL)
519 dsl_dir_rele(dd, FTAG);
520 if (dp != NULL)
521 dsl_pool_rele(dp, FTAG);
523 return (ret);
526 static void
527 dsl_crypto_key_free(dsl_crypto_key_t *dck)
529 ASSERT(zfs_refcount_count(&dck->dck_holds) == 0);
531 /* destroy the zio_crypt_key_t */
532 zio_crypt_key_destroy(&dck->dck_key);
534 /* free the refcount, wrapping key, and lock */
535 zfs_refcount_destroy(&dck->dck_holds);
536 if (dck->dck_wkey)
537 dsl_wrapping_key_rele(dck->dck_wkey, dck);
539 /* free the key */
540 kmem_free(dck, sizeof (dsl_crypto_key_t));
543 static void
544 dsl_crypto_key_rele(dsl_crypto_key_t *dck, void *tag)
546 if (zfs_refcount_remove(&dck->dck_holds, tag) == 0)
547 dsl_crypto_key_free(dck);
550 static int
551 dsl_crypto_key_open(objset_t *mos, dsl_wrapping_key_t *wkey,
552 uint64_t dckobj, void *tag, dsl_crypto_key_t **dck_out)
554 int ret;
555 uint64_t crypt = 0, guid = 0, version = 0;
556 uint8_t raw_keydata[MASTER_KEY_MAX_LEN];
557 uint8_t raw_hmac_keydata[SHA512_HMAC_KEYLEN];
558 uint8_t iv[WRAPPING_IV_LEN];
559 uint8_t mac[WRAPPING_MAC_LEN];
560 dsl_crypto_key_t *dck;
562 /* allocate and initialize the key */
563 dck = kmem_zalloc(sizeof (dsl_crypto_key_t), KM_SLEEP);
564 if (!dck)
565 return (SET_ERROR(ENOMEM));
567 /* fetch all of the values we need from the ZAP */
568 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
569 &crypt);
570 if (ret != 0)
571 goto error;
573 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1, &guid);
574 if (ret != 0)
575 goto error;
577 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
578 MASTER_KEY_MAX_LEN, raw_keydata);
579 if (ret != 0)
580 goto error;
582 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
583 SHA512_HMAC_KEYLEN, raw_hmac_keydata);
584 if (ret != 0)
585 goto error;
587 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
588 iv);
589 if (ret != 0)
590 goto error;
592 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
593 mac);
594 if (ret != 0)
595 goto error;
597 /* the initial on-disk format for encryption did not have a version */
598 (void) zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_VERSION, 8, 1, &version);
601 * Unwrap the keys. If there is an error return EACCES to indicate
602 * an authentication failure.
604 ret = zio_crypt_key_unwrap(&wkey->wk_key, crypt, version, guid,
605 raw_keydata, raw_hmac_keydata, iv, mac, &dck->dck_key);
606 if (ret != 0) {
607 ret = SET_ERROR(EACCES);
608 goto error;
611 /* finish initializing the dsl_crypto_key_t */
612 zfs_refcount_create(&dck->dck_holds);
613 dsl_wrapping_key_hold(wkey, dck);
614 dck->dck_wkey = wkey;
615 dck->dck_obj = dckobj;
616 zfs_refcount_add(&dck->dck_holds, tag);
618 *dck_out = dck;
619 return (0);
621 error:
622 if (dck != NULL) {
623 bzero(dck, sizeof (dsl_crypto_key_t));
624 kmem_free(dck, sizeof (dsl_crypto_key_t));
627 *dck_out = NULL;
628 return (ret);
631 static int
632 spa_keystore_dsl_key_hold_impl(spa_t *spa, uint64_t dckobj, void *tag,
633 dsl_crypto_key_t **dck_out)
635 int ret;
636 dsl_crypto_key_t search_dck;
637 dsl_crypto_key_t *found_dck;
639 ASSERT(RW_LOCK_HELD(&spa->spa_keystore.sk_dk_lock));
641 /* init the search key */
642 search_dck.dck_obj = dckobj;
644 /* find the matching key in the keystore */
645 found_dck = avl_find(&spa->spa_keystore.sk_dsl_keys, &search_dck, NULL);
646 if (!found_dck) {
647 ret = SET_ERROR(ENOENT);
648 goto error;
651 /* increment the refcount */
652 zfs_refcount_add(&found_dck->dck_holds, tag);
654 *dck_out = found_dck;
655 return (0);
657 error:
658 *dck_out = NULL;
659 return (ret);
662 static int
663 spa_keystore_dsl_key_hold_dd(spa_t *spa, dsl_dir_t *dd, void *tag,
664 dsl_crypto_key_t **dck_out)
666 int ret;
667 avl_index_t where;
668 dsl_crypto_key_t *dck_io = NULL, *dck_ks = NULL;
669 dsl_wrapping_key_t *wkey = NULL;
670 uint64_t dckobj = dd->dd_crypto_obj;
672 /* Lookup the key in the tree of currently loaded keys */
673 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_READER);
674 ret = spa_keystore_dsl_key_hold_impl(spa, dckobj, tag, &dck_ks);
675 rw_exit(&spa->spa_keystore.sk_dk_lock);
676 if (ret == 0) {
677 *dck_out = dck_ks;
678 return (0);
681 /* Lookup the wrapping key from the keystore */
682 ret = spa_keystore_wkey_hold_dd(spa, dd, FTAG, &wkey);
683 if (ret != 0) {
684 *dck_out = NULL;
685 return (SET_ERROR(EACCES));
688 /* Read the key from disk */
689 ret = dsl_crypto_key_open(spa->spa_meta_objset, wkey, dckobj,
690 tag, &dck_io);
691 if (ret != 0) {
692 dsl_wrapping_key_rele(wkey, FTAG);
693 *dck_out = NULL;
694 return (ret);
698 * Add the key to the keystore. It may already exist if it was
699 * added while performing the read from disk. In this case discard
700 * it and return the key from the keystore.
702 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_WRITER);
703 ret = spa_keystore_dsl_key_hold_impl(spa, dckobj, tag, &dck_ks);
704 if (ret != 0) {
705 avl_find(&spa->spa_keystore.sk_dsl_keys, dck_io, &where);
706 avl_insert(&spa->spa_keystore.sk_dsl_keys, dck_io, where);
707 *dck_out = dck_io;
708 } else {
709 dsl_crypto_key_free(dck_io);
710 *dck_out = dck_ks;
713 /* Release the wrapping key (the dsl key now has a reference to it) */
714 dsl_wrapping_key_rele(wkey, FTAG);
715 rw_exit(&spa->spa_keystore.sk_dk_lock);
717 return (0);
720 void
721 spa_keystore_dsl_key_rele(spa_t *spa, dsl_crypto_key_t *dck, void *tag)
723 rw_enter(&spa->spa_keystore.sk_dk_lock, RW_WRITER);
725 if (zfs_refcount_remove(&dck->dck_holds, tag) == 0) {
726 avl_remove(&spa->spa_keystore.sk_dsl_keys, dck);
727 dsl_crypto_key_free(dck);
730 rw_exit(&spa->spa_keystore.sk_dk_lock);
734 spa_keystore_load_wkey_impl(spa_t *spa, dsl_wrapping_key_t *wkey)
736 int ret;
737 avl_index_t where;
738 dsl_wrapping_key_t *found_wkey;
740 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
742 /* insert the wrapping key into the keystore */
743 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, wkey, &where);
744 if (found_wkey != NULL) {
745 ret = SET_ERROR(EEXIST);
746 goto error_unlock;
748 avl_insert(&spa->spa_keystore.sk_wkeys, wkey, where);
750 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
752 return (0);
754 error_unlock:
755 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
756 return (ret);
760 spa_keystore_load_wkey(const char *dsname, dsl_crypto_params_t *dcp,
761 boolean_t noop)
763 int ret;
764 dsl_dir_t *dd = NULL;
765 dsl_crypto_key_t *dck = NULL;
766 dsl_wrapping_key_t *wkey = dcp->cp_wkey;
767 dsl_pool_t *dp = NULL;
768 uint64_t rddobj, keyformat, salt, iters;
771 * We don't validate the wrapping key's keyformat, salt, or iters
772 * since they will never be needed after the DCK has been wrapped.
774 if (dcp->cp_wkey == NULL ||
775 dcp->cp_cmd != DCP_CMD_NONE ||
776 dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
777 dcp->cp_keylocation != NULL)
778 return (SET_ERROR(EINVAL));
780 ret = dsl_pool_hold(dsname, FTAG, &dp);
781 if (ret != 0)
782 goto error;
784 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
785 ret = SET_ERROR(ENOTSUP);
786 goto error;
789 /* hold the dsl dir */
790 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
791 if (ret != 0) {
792 dd = NULL;
793 goto error;
796 /* confirm that dd is the encryption root */
797 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
798 if (ret != 0 || rddobj != dd->dd_object) {
799 ret = SET_ERROR(EINVAL);
800 goto error;
803 /* initialize the wkey's ddobj */
804 wkey->wk_ddobj = dd->dd_object;
806 /* verify that the wkey is correct by opening its dsl key */
807 ret = dsl_crypto_key_open(dp->dp_meta_objset, wkey,
808 dd->dd_crypto_obj, FTAG, &dck);
809 if (ret != 0)
810 goto error;
812 /* initialize the wkey encryption parameters from the DSL Crypto Key */
813 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
814 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &keyformat);
815 if (ret != 0)
816 goto error;
818 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
819 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &salt);
820 if (ret != 0)
821 goto error;
823 ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,
824 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &iters);
825 if (ret != 0)
826 goto error;
828 ASSERT3U(keyformat, <, ZFS_KEYFORMAT_FORMATS);
829 ASSERT3U(keyformat, !=, ZFS_KEYFORMAT_NONE);
830 IMPLY(keyformat == ZFS_KEYFORMAT_PASSPHRASE, iters != 0);
831 IMPLY(keyformat == ZFS_KEYFORMAT_PASSPHRASE, salt != 0);
832 IMPLY(keyformat != ZFS_KEYFORMAT_PASSPHRASE, iters == 0);
833 IMPLY(keyformat != ZFS_KEYFORMAT_PASSPHRASE, salt == 0);
835 wkey->wk_keyformat = keyformat;
836 wkey->wk_salt = salt;
837 wkey->wk_iters = iters;
840 * At this point we have verified the wkey and confirmed that it can
841 * be used to decrypt a DSL Crypto Key. We can simply cleanup and
842 * return if this is all the user wanted to do.
844 if (noop)
845 goto error;
847 /* insert the wrapping key into the keystore */
848 ret = spa_keystore_load_wkey_impl(dp->dp_spa, wkey);
849 if (ret != 0)
850 goto error;
852 dsl_crypto_key_rele(dck, FTAG);
853 dsl_dir_rele(dd, FTAG);
854 dsl_pool_rele(dp, FTAG);
856 /* create any zvols under this ds */
857 zvol_create_minors(dp->dp_spa, dsname, B_TRUE);
859 return (0);
861 error:
862 if (dck != NULL)
863 dsl_crypto_key_rele(dck, FTAG);
864 if (dd != NULL)
865 dsl_dir_rele(dd, FTAG);
866 if (dp != NULL)
867 dsl_pool_rele(dp, FTAG);
869 return (ret);
873 spa_keystore_unload_wkey_impl(spa_t *spa, uint64_t ddobj)
875 int ret;
876 dsl_wrapping_key_t search_wkey;
877 dsl_wrapping_key_t *found_wkey;
879 /* init the search wrapping key */
880 search_wkey.wk_ddobj = ddobj;
882 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
884 /* remove the wrapping key from the keystore */
885 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys,
886 &search_wkey, NULL);
887 if (!found_wkey) {
888 ret = SET_ERROR(EACCES);
889 goto error_unlock;
890 } else if (zfs_refcount_count(&found_wkey->wk_refcnt) != 0) {
891 ret = SET_ERROR(EBUSY);
892 goto error_unlock;
894 avl_remove(&spa->spa_keystore.sk_wkeys, found_wkey);
896 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
898 /* free the wrapping key */
899 dsl_wrapping_key_free(found_wkey);
901 return (0);
903 error_unlock:
904 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
905 return (ret);
909 spa_keystore_unload_wkey(const char *dsname)
911 int ret = 0;
912 dsl_dir_t *dd = NULL;
913 dsl_pool_t *dp = NULL;
914 spa_t *spa = NULL;
916 ret = spa_open(dsname, &spa, FTAG);
917 if (ret != 0)
918 return (ret);
921 * Wait for any outstanding txg IO to complete, releasing any
922 * remaining references on the wkey.
924 if (spa_mode(spa) != FREAD)
925 txg_wait_synced(spa->spa_dsl_pool, 0);
927 spa_close(spa, FTAG);
929 /* hold the dsl dir */
930 ret = dsl_pool_hold(dsname, FTAG, &dp);
931 if (ret != 0)
932 goto error;
934 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
935 ret = (SET_ERROR(ENOTSUP));
936 goto error;
939 ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);
940 if (ret != 0) {
941 dd = NULL;
942 goto error;
945 /* unload the wkey */
946 ret = spa_keystore_unload_wkey_impl(dp->dp_spa, dd->dd_object);
947 if (ret != 0)
948 goto error;
950 dsl_dir_rele(dd, FTAG);
951 dsl_pool_rele(dp, FTAG);
953 /* remove any zvols under this ds */
954 zvol_remove_minors(dp->dp_spa, dsname, B_TRUE);
956 return (0);
958 error:
959 if (dd != NULL)
960 dsl_dir_rele(dd, FTAG);
961 if (dp != NULL)
962 dsl_pool_rele(dp, FTAG);
964 return (ret);
967 void
968 key_mapping_add_ref(dsl_key_mapping_t *km, void *tag)
970 ASSERT3U(zfs_refcount_count(&km->km_refcnt), >=, 1);
971 zfs_refcount_add(&km->km_refcnt, tag);
975 * The locking here is a little tricky to ensure we don't cause unnecessary
976 * performance problems. We want to release a key mapping whenever someone
977 * decrements the refcount to 0, but freeing the mapping requires removing
978 * it from the spa_keystore, which requires holding sk_km_lock as a writer.
979 * Most of the time we don't want to hold this lock as a writer, since the
980 * same lock is held as a reader for each IO that needs to encrypt / decrypt
981 * data for any dataset and in practice we will only actually free the
982 * mapping after unmounting a dataset.
984 void
985 key_mapping_rele(spa_t *spa, dsl_key_mapping_t *km, void *tag)
987 ASSERT3U(zfs_refcount_count(&km->km_refcnt), >=, 1);
989 if (zfs_refcount_remove(&km->km_refcnt, tag) != 0)
990 return;
993 * We think we are going to need to free the mapping. Add a
994 * reference to prevent most other releasers from thinking
995 * this might be their responsibility. This is inherently
996 * racy, so we will confirm that we are legitimately the
997 * last holder once we have the sk_km_lock as a writer.
999 zfs_refcount_add(&km->km_refcnt, FTAG);
1001 rw_enter(&spa->spa_keystore.sk_km_lock, RW_WRITER);
1002 if (zfs_refcount_remove(&km->km_refcnt, FTAG) != 0) {
1003 rw_exit(&spa->spa_keystore.sk_km_lock);
1004 return;
1007 avl_remove(&spa->spa_keystore.sk_key_mappings, km);
1008 rw_exit(&spa->spa_keystore.sk_km_lock);
1010 spa_keystore_dsl_key_rele(spa, km->km_key, km);
1011 zfs_refcount_destroy(&km->km_refcnt);
1012 kmem_free(km, sizeof (dsl_key_mapping_t));
1016 spa_keystore_create_mapping(spa_t *spa, dsl_dataset_t *ds, void *tag,
1017 dsl_key_mapping_t **km_out)
1019 int ret;
1020 avl_index_t where;
1021 dsl_key_mapping_t *km, *found_km;
1022 boolean_t should_free = B_FALSE;
1024 /* Allocate and initialize the mapping */
1025 km = kmem_zalloc(sizeof (dsl_key_mapping_t), KM_SLEEP);
1026 zfs_refcount_create(&km->km_refcnt);
1028 ret = spa_keystore_dsl_key_hold_dd(spa, ds->ds_dir, km, &km->km_key);
1029 if (ret != 0) {
1030 zfs_refcount_destroy(&km->km_refcnt);
1031 kmem_free(km, sizeof (dsl_key_mapping_t));
1033 if (km_out != NULL)
1034 *km_out = NULL;
1035 return (ret);
1038 km->km_dsobj = ds->ds_object;
1040 rw_enter(&spa->spa_keystore.sk_km_lock, RW_WRITER);
1043 * If a mapping already exists, simply increment its refcount and
1044 * cleanup the one we made. We want to allocate / free outside of
1045 * the lock because this lock is also used by the zio layer to lookup
1046 * key mappings. Otherwise, use the one we created. Normally, there will
1047 * only be one active reference at a time (the objset owner), but there
1048 * are times when there could be multiple async users.
1050 found_km = avl_find(&spa->spa_keystore.sk_key_mappings, km, &where);
1051 if (found_km != NULL) {
1052 should_free = B_TRUE;
1053 zfs_refcount_add(&found_km->km_refcnt, tag);
1054 if (km_out != NULL)
1055 *km_out = found_km;
1056 } else {
1057 zfs_refcount_add(&km->km_refcnt, tag);
1058 avl_insert(&spa->spa_keystore.sk_key_mappings, km, where);
1059 if (km_out != NULL)
1060 *km_out = km;
1063 rw_exit(&spa->spa_keystore.sk_km_lock);
1065 if (should_free) {
1066 spa_keystore_dsl_key_rele(spa, km->km_key, km);
1067 zfs_refcount_destroy(&km->km_refcnt);
1068 kmem_free(km, sizeof (dsl_key_mapping_t));
1071 return (0);
1075 spa_keystore_remove_mapping(spa_t *spa, uint64_t dsobj, void *tag)
1077 int ret;
1078 dsl_key_mapping_t search_km;
1079 dsl_key_mapping_t *found_km;
1081 /* init the search key mapping */
1082 search_km.km_dsobj = dsobj;
1084 rw_enter(&spa->spa_keystore.sk_km_lock, RW_READER);
1086 /* find the matching mapping */
1087 found_km = avl_find(&spa->spa_keystore.sk_key_mappings,
1088 &search_km, NULL);
1089 if (found_km == NULL) {
1090 ret = SET_ERROR(ENOENT);
1091 goto error_unlock;
1094 rw_exit(&spa->spa_keystore.sk_km_lock);
1096 key_mapping_rele(spa, found_km, tag);
1098 return (0);
1100 error_unlock:
1101 rw_exit(&spa->spa_keystore.sk_km_lock);
1102 return (ret);
1106 * This function is primarily used by the zio and arc layer to lookup
1107 * DSL Crypto Keys for encryption. Callers must release the key with
1108 * spa_keystore_dsl_key_rele(). The function may also be called with
1109 * dck_out == NULL and tag == NULL to simply check that a key exists
1110 * without getting a reference to it.
1113 spa_keystore_lookup_key(spa_t *spa, uint64_t dsobj, void *tag,
1114 dsl_crypto_key_t **dck_out)
1116 int ret;
1117 dsl_key_mapping_t search_km;
1118 dsl_key_mapping_t *found_km;
1120 ASSERT((tag != NULL && dck_out != NULL) ||
1121 (tag == NULL && dck_out == NULL));
1123 /* init the search key mapping */
1124 search_km.km_dsobj = dsobj;
1126 rw_enter(&spa->spa_keystore.sk_km_lock, RW_READER);
1128 /* remove the mapping from the tree */
1129 found_km = avl_find(&spa->spa_keystore.sk_key_mappings, &search_km,
1130 NULL);
1131 if (found_km == NULL) {
1132 ret = SET_ERROR(ENOENT);
1133 goto error_unlock;
1136 if (found_km && tag)
1137 zfs_refcount_add(&found_km->km_key->dck_holds, tag);
1139 rw_exit(&spa->spa_keystore.sk_km_lock);
1141 if (dck_out != NULL)
1142 *dck_out = found_km->km_key;
1143 return (0);
1145 error_unlock:
1146 rw_exit(&spa->spa_keystore.sk_km_lock);
1148 if (dck_out != NULL)
1149 *dck_out = NULL;
1150 return (ret);
1153 static int
1154 dmu_objset_check_wkey_loaded(dsl_dir_t *dd)
1156 int ret;
1157 dsl_wrapping_key_t *wkey = NULL;
1159 ret = spa_keystore_wkey_hold_dd(dd->dd_pool->dp_spa, dd, FTAG,
1160 &wkey);
1161 if (ret != 0)
1162 return (SET_ERROR(EACCES));
1164 dsl_wrapping_key_rele(wkey, FTAG);
1166 return (0);
1169 static zfs_keystatus_t
1170 dsl_dataset_get_keystatus(dsl_dir_t *dd)
1172 /* check if this dd has a has a dsl key */
1173 if (dd->dd_crypto_obj == 0)
1174 return (ZFS_KEYSTATUS_NONE);
1176 return (dmu_objset_check_wkey_loaded(dd) == 0 ?
1177 ZFS_KEYSTATUS_AVAILABLE : ZFS_KEYSTATUS_UNAVAILABLE);
1180 static int
1181 dsl_dir_get_crypt(dsl_dir_t *dd, uint64_t *crypt)
1183 if (dd->dd_crypto_obj == 0) {
1184 *crypt = ZIO_CRYPT_OFF;
1185 return (0);
1188 return (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
1189 DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1, crypt));
1192 static void
1193 dsl_crypto_key_sync_impl(objset_t *mos, uint64_t dckobj, uint64_t crypt,
1194 uint64_t root_ddobj, uint64_t guid, uint8_t *iv, uint8_t *mac,
1195 uint8_t *keydata, uint8_t *hmac_keydata, uint64_t keyformat,
1196 uint64_t salt, uint64_t iters, dmu_tx_t *tx)
1198 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
1199 &crypt, tx));
1200 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1,
1201 &root_ddobj, tx));
1202 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1,
1203 &guid, tx));
1204 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
1205 iv, tx));
1206 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
1207 mac, tx));
1208 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
1209 MASTER_KEY_MAX_LEN, keydata, tx));
1210 VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
1211 SHA512_HMAC_KEYLEN, hmac_keydata, tx));
1212 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
1213 8, 1, &keyformat, tx));
1214 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),
1215 8, 1, &salt, tx));
1216 VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),
1217 8, 1, &iters, tx));
1220 static void
1221 dsl_crypto_key_sync(dsl_crypto_key_t *dck, dmu_tx_t *tx)
1223 zio_crypt_key_t *key = &dck->dck_key;
1224 dsl_wrapping_key_t *wkey = dck->dck_wkey;
1225 uint8_t keydata[MASTER_KEY_MAX_LEN];
1226 uint8_t hmac_keydata[SHA512_HMAC_KEYLEN];
1227 uint8_t iv[WRAPPING_IV_LEN];
1228 uint8_t mac[WRAPPING_MAC_LEN];
1230 ASSERT(dmu_tx_is_syncing(tx));
1231 ASSERT3U(key->zk_crypt, <, ZIO_CRYPT_FUNCTIONS);
1233 /* encrypt and store the keys along with the IV and MAC */
1234 VERIFY0(zio_crypt_key_wrap(&dck->dck_wkey->wk_key, key, iv, mac,
1235 keydata, hmac_keydata));
1237 /* update the ZAP with the obtained values */
1238 dsl_crypto_key_sync_impl(tx->tx_pool->dp_meta_objset, dck->dck_obj,
1239 key->zk_crypt, wkey->wk_ddobj, key->zk_guid, iv, mac, keydata,
1240 hmac_keydata, wkey->wk_keyformat, wkey->wk_salt, wkey->wk_iters,
1241 tx);
1244 typedef struct spa_keystore_change_key_args {
1245 const char *skcka_dsname;
1246 dsl_crypto_params_t *skcka_cp;
1247 } spa_keystore_change_key_args_t;
1249 static int
1250 spa_keystore_change_key_check(void *arg, dmu_tx_t *tx)
1252 int ret;
1253 dsl_dir_t *dd = NULL;
1254 dsl_pool_t *dp = dmu_tx_pool(tx);
1255 spa_keystore_change_key_args_t *skcka = arg;
1256 dsl_crypto_params_t *dcp = skcka->skcka_cp;
1257 uint64_t rddobj;
1259 /* check for the encryption feature */
1260 if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {
1261 ret = SET_ERROR(ENOTSUP);
1262 goto error;
1265 /* check for valid key change command */
1266 if (dcp->cp_cmd != DCP_CMD_NEW_KEY &&
1267 dcp->cp_cmd != DCP_CMD_INHERIT &&
1268 dcp->cp_cmd != DCP_CMD_FORCE_NEW_KEY &&
1269 dcp->cp_cmd != DCP_CMD_FORCE_INHERIT) {
1270 ret = SET_ERROR(EINVAL);
1271 goto error;
1274 /* hold the dd */
1275 ret = dsl_dir_hold(dp, skcka->skcka_dsname, FTAG, &dd, NULL);
1276 if (ret != 0) {
1277 dd = NULL;
1278 goto error;
1281 /* verify that the dataset is encrypted */
1282 if (dd->dd_crypto_obj == 0) {
1283 ret = SET_ERROR(EINVAL);
1284 goto error;
1287 /* clones must always use their origin's key */
1288 if (dsl_dir_is_clone(dd)) {
1289 ret = SET_ERROR(EINVAL);
1290 goto error;
1293 /* lookup the ddobj we are inheriting the keylocation from */
1294 ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);
1295 if (ret != 0)
1296 goto error;
1298 /* Handle inheritance */
1299 if (dcp->cp_cmd == DCP_CMD_INHERIT ||
1300 dcp->cp_cmd == DCP_CMD_FORCE_INHERIT) {
1301 /* no other encryption params should be given */
1302 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
1303 dcp->cp_keylocation != NULL ||
1304 dcp->cp_wkey != NULL) {
1305 ret = SET_ERROR(EINVAL);
1306 goto error;
1309 /* check that this is an encryption root */
1310 if (dd->dd_object != rddobj) {
1311 ret = SET_ERROR(EINVAL);
1312 goto error;
1315 /* check that the parent is encrypted */
1316 if (dd->dd_parent->dd_crypto_obj == 0) {
1317 ret = SET_ERROR(EINVAL);
1318 goto error;
1321 /* if we are rewrapping check that both keys are loaded */
1322 if (dcp->cp_cmd == DCP_CMD_INHERIT) {
1323 ret = dmu_objset_check_wkey_loaded(dd);
1324 if (ret != 0)
1325 goto error;
1327 ret = dmu_objset_check_wkey_loaded(dd->dd_parent);
1328 if (ret != 0)
1329 goto error;
1332 dsl_dir_rele(dd, FTAG);
1333 return (0);
1336 /* handle forcing an encryption root without rewrapping */
1337 if (dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY) {
1338 /* no other encryption params should be given */
1339 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT ||
1340 dcp->cp_keylocation != NULL ||
1341 dcp->cp_wkey != NULL) {
1342 ret = SET_ERROR(EINVAL);
1343 goto error;
1346 /* check that this is not an encryption root */
1347 if (dd->dd_object == rddobj) {
1348 ret = SET_ERROR(EINVAL);
1349 goto error;
1352 dsl_dir_rele(dd, FTAG);
1353 return (0);
1356 /* crypt cannot be changed after creation */
1357 if (dcp->cp_crypt != ZIO_CRYPT_INHERIT) {
1358 ret = SET_ERROR(EINVAL);
1359 goto error;
1362 /* we are not inheritting our parent's wkey so we need one ourselves */
1363 if (dcp->cp_wkey == NULL) {
1364 ret = SET_ERROR(EINVAL);
1365 goto error;
1368 /* check for a valid keyformat for the new wrapping key */
1369 if (dcp->cp_wkey->wk_keyformat >= ZFS_KEYFORMAT_FORMATS ||
1370 dcp->cp_wkey->wk_keyformat == ZFS_KEYFORMAT_NONE) {
1371 ret = SET_ERROR(EINVAL);
1372 goto error;
1376 * If this dataset is not currently an encryption root we need a new
1377 * keylocation for this dataset's new wrapping key. Otherwise we can
1378 * just keep the one we already had.
1380 if (dd->dd_object != rddobj && dcp->cp_keylocation == NULL) {
1381 ret = SET_ERROR(EINVAL);
1382 goto error;
1385 /* check that the keylocation is valid if it is not NULL */
1386 if (dcp->cp_keylocation != NULL &&
1387 !zfs_prop_valid_keylocation(dcp->cp_keylocation, B_TRUE)) {
1388 ret = SET_ERROR(EINVAL);
1389 goto error;
1392 /* passphrases require pbkdf2 salt and iters */
1393 if (dcp->cp_wkey->wk_keyformat == ZFS_KEYFORMAT_PASSPHRASE) {
1394 if (dcp->cp_wkey->wk_salt == 0 ||
1395 dcp->cp_wkey->wk_iters < MIN_PBKDF2_ITERATIONS) {
1396 ret = SET_ERROR(EINVAL);
1397 goto error;
1399 } else {
1400 if (dcp->cp_wkey->wk_salt != 0 || dcp->cp_wkey->wk_iters != 0) {
1401 ret = SET_ERROR(EINVAL);
1402 goto error;
1406 /* make sure the dd's wkey is loaded */
1407 ret = dmu_objset_check_wkey_loaded(dd);
1408 if (ret != 0)
1409 goto error;
1411 dsl_dir_rele(dd, FTAG);
1413 return (0);
1415 error:
1416 if (dd != NULL)
1417 dsl_dir_rele(dd, FTAG);
1419 return (ret);
1423 * This function deals with the intricacies of updating wrapping
1424 * key references and encryption roots recursively in the event
1425 * of a call to 'zfs change-key' or 'zfs promote'. The 'skip'
1426 * parameter should always be set to B_FALSE when called
1427 * externally.
1429 static void
1430 spa_keystore_change_key_sync_impl(uint64_t rddobj, uint64_t ddobj,
1431 uint64_t new_rddobj, dsl_wrapping_key_t *wkey, boolean_t skip,
1432 dmu_tx_t *tx)
1434 int ret;
1435 zap_cursor_t *zc;
1436 zap_attribute_t *za;
1437 dsl_pool_t *dp = dmu_tx_pool(tx);
1438 dsl_dir_t *dd = NULL;
1439 dsl_crypto_key_t *dck = NULL;
1440 uint64_t curr_rddobj;
1442 ASSERT(RW_WRITE_HELD(&dp->dp_spa->spa_keystore.sk_wkeys_lock));
1444 /* hold the dd */
1445 VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));
1447 /* ignore special dsl dirs */
1448 if (dd->dd_myname[0] == '$' || dd->dd_myname[0] == '%') {
1449 dsl_dir_rele(dd, FTAG);
1450 return;
1453 ret = dsl_dir_get_encryption_root_ddobj(dd, &curr_rddobj);
1454 VERIFY(ret == 0 || ret == ENOENT);
1457 * Stop recursing if this dsl dir didn't inherit from the root
1458 * or if this dd is a clone.
1460 if (ret == ENOENT ||
1461 (!skip && (curr_rddobj != rddobj || dsl_dir_is_clone(dd)))) {
1462 dsl_dir_rele(dd, FTAG);
1463 return;
1467 * If we don't have a wrapping key just update the dck to reflect the
1468 * new encryption root. Otherwise rewrap the entire dck and re-sync it
1469 * to disk. If skip is set, we don't do any of this work.
1471 if (!skip) {
1472 if (wkey == NULL) {
1473 VERIFY0(zap_update(dp->dp_meta_objset,
1474 dd->dd_crypto_obj,
1475 DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1,
1476 &new_rddobj, tx));
1477 } else {
1478 VERIFY0(spa_keystore_dsl_key_hold_dd(dp->dp_spa, dd,
1479 FTAG, &dck));
1480 dsl_wrapping_key_hold(wkey, dck);
1481 dsl_wrapping_key_rele(dck->dck_wkey, dck);
1482 dck->dck_wkey = wkey;
1483 dsl_crypto_key_sync(dck, tx);
1484 spa_keystore_dsl_key_rele(dp->dp_spa, dck, FTAG);
1488 zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);
1489 za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP);
1491 /* Recurse into all child dsl dirs. */
1492 for (zap_cursor_init(zc, dp->dp_meta_objset,
1493 dsl_dir_phys(dd)->dd_child_dir_zapobj);
1494 zap_cursor_retrieve(zc, za) == 0;
1495 zap_cursor_advance(zc)) {
1496 spa_keystore_change_key_sync_impl(rddobj,
1497 za->za_first_integer, new_rddobj, wkey, B_FALSE, tx);
1499 zap_cursor_fini(zc);
1502 * Recurse into all dsl dirs of clones. We utilize the skip parameter
1503 * here so that we don't attempt to process the clones directly. This
1504 * is because the clone and its origin share the same dck, which has
1505 * already been updated.
1507 for (zap_cursor_init(zc, dp->dp_meta_objset,
1508 dsl_dir_phys(dd)->dd_clones);
1509 zap_cursor_retrieve(zc, za) == 0;
1510 zap_cursor_advance(zc)) {
1511 dsl_dataset_t *clone;
1513 VERIFY0(dsl_dataset_hold_obj(dp, za->za_first_integer,
1514 FTAG, &clone));
1515 spa_keystore_change_key_sync_impl(rddobj,
1516 clone->ds_dir->dd_object, new_rddobj, wkey, B_TRUE, tx);
1517 dsl_dataset_rele(clone, FTAG);
1519 zap_cursor_fini(zc);
1521 kmem_free(za, sizeof (zap_attribute_t));
1522 kmem_free(zc, sizeof (zap_cursor_t));
1524 dsl_dir_rele(dd, FTAG);
1527 static void
1528 spa_keystore_change_key_sync(void *arg, dmu_tx_t *tx)
1530 dsl_dataset_t *ds;
1531 avl_index_t where;
1532 dsl_pool_t *dp = dmu_tx_pool(tx);
1533 spa_t *spa = dp->dp_spa;
1534 spa_keystore_change_key_args_t *skcka = arg;
1535 dsl_crypto_params_t *dcp = skcka->skcka_cp;
1536 dsl_wrapping_key_t *wkey = NULL, *found_wkey;
1537 dsl_wrapping_key_t wkey_search;
1538 char *keylocation = dcp->cp_keylocation;
1539 uint64_t rddobj, new_rddobj;
1541 /* create and initialize the wrapping key */
1542 VERIFY0(dsl_dataset_hold(dp, skcka->skcka_dsname, FTAG, &ds));
1543 ASSERT(!ds->ds_is_snapshot);
1545 if (dcp->cp_cmd == DCP_CMD_NEW_KEY ||
1546 dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY) {
1548 * We are changing to a new wkey. Set additional properties
1549 * which can be sent along with this ioctl. Note that this
1550 * command can set keylocation even if it can't normally be
1551 * set via 'zfs set' due to a non-local keylocation.
1553 if (dcp->cp_cmd == DCP_CMD_NEW_KEY) {
1554 wkey = dcp->cp_wkey;
1555 wkey->wk_ddobj = ds->ds_dir->dd_object;
1556 } else {
1557 keylocation = "prompt";
1560 if (keylocation != NULL) {
1561 dsl_prop_set_sync_impl(ds,
1562 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1563 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1,
1564 keylocation, tx);
1567 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds->ds_dir, &rddobj));
1568 new_rddobj = ds->ds_dir->dd_object;
1569 } else {
1571 * We are inheritting the parent's wkey. Unset any local
1572 * keylocation and grab a reference to the wkey.
1574 if (dcp->cp_cmd == DCP_CMD_INHERIT) {
1575 VERIFY0(spa_keystore_wkey_hold_dd(spa,
1576 ds->ds_dir->dd_parent, FTAG, &wkey));
1579 dsl_prop_set_sync_impl(ds,
1580 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), ZPROP_SRC_NONE,
1581 0, 0, NULL, tx);
1583 rddobj = ds->ds_dir->dd_object;
1584 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds->ds_dir->dd_parent,
1585 &new_rddobj));
1588 if (wkey == NULL) {
1589 ASSERT(dcp->cp_cmd == DCP_CMD_FORCE_INHERIT ||
1590 dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY);
1593 rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
1595 /* recurse through all children and rewrap their keys */
1596 spa_keystore_change_key_sync_impl(rddobj, ds->ds_dir->dd_object,
1597 new_rddobj, wkey, B_FALSE, tx);
1600 * All references to the old wkey should be released now (if it
1601 * existed). Replace the wrapping key.
1603 wkey_search.wk_ddobj = ds->ds_dir->dd_object;
1604 found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, &wkey_search, NULL);
1605 if (found_wkey != NULL) {
1606 ASSERT0(zfs_refcount_count(&found_wkey->wk_refcnt));
1607 avl_remove(&spa->spa_keystore.sk_wkeys, found_wkey);
1608 dsl_wrapping_key_free(found_wkey);
1611 if (dcp->cp_cmd == DCP_CMD_NEW_KEY) {
1612 avl_find(&spa->spa_keystore.sk_wkeys, wkey, &where);
1613 avl_insert(&spa->spa_keystore.sk_wkeys, wkey, where);
1614 } else if (wkey != NULL) {
1615 dsl_wrapping_key_rele(wkey, FTAG);
1618 rw_exit(&spa->spa_keystore.sk_wkeys_lock);
1620 dsl_dataset_rele(ds, FTAG);
1624 spa_keystore_change_key(const char *dsname, dsl_crypto_params_t *dcp)
1626 spa_keystore_change_key_args_t skcka;
1628 /* initialize the args struct */
1629 skcka.skcka_dsname = dsname;
1630 skcka.skcka_cp = dcp;
1633 * Perform the actual work in syncing context. The blocks modified
1634 * here could be calculated but it would require holding the pool
1635 * lock and traversing all of the datasets that will have their keys
1636 * changed.
1638 return (dsl_sync_task(dsname, spa_keystore_change_key_check,
1639 spa_keystore_change_key_sync, &skcka, 15,
1640 ZFS_SPACE_CHECK_RESERVED));
1644 dsl_dir_rename_crypt_check(dsl_dir_t *dd, dsl_dir_t *newparent)
1646 int ret;
1647 uint64_t curr_rddobj, parent_rddobj;
1649 if (dd->dd_crypto_obj == 0)
1650 return (0);
1652 ret = dsl_dir_get_encryption_root_ddobj(dd, &curr_rddobj);
1653 if (ret != 0)
1654 goto error;
1657 * if this is not an encryption root, we must make sure we are not
1658 * moving dd to a new encryption root
1660 if (dd->dd_object != curr_rddobj) {
1661 ret = dsl_dir_get_encryption_root_ddobj(newparent,
1662 &parent_rddobj);
1663 if (ret != 0)
1664 goto error;
1666 if (parent_rddobj != curr_rddobj) {
1667 ret = SET_ERROR(EACCES);
1668 goto error;
1672 return (0);
1674 error:
1675 return (ret);
1679 * Check to make sure that a promote from targetdd to origindd will not require
1680 * any key rewraps.
1683 dsl_dataset_promote_crypt_check(dsl_dir_t *target, dsl_dir_t *origin)
1685 int ret;
1686 uint64_t rddobj, op_rddobj, tp_rddobj;
1688 /* If the dataset is not encrypted we don't need to check anything */
1689 if (origin->dd_crypto_obj == 0)
1690 return (0);
1693 * If we are not changing the first origin snapshot in a chain
1694 * the encryption root won't change either.
1696 if (dsl_dir_is_clone(origin))
1697 return (0);
1700 * If the origin is the encryption root we will update
1701 * the DSL Crypto Key to point to the target instead.
1703 ret = dsl_dir_get_encryption_root_ddobj(origin, &rddobj);
1704 if (ret != 0)
1705 return (ret);
1707 if (rddobj == origin->dd_object)
1708 return (0);
1711 * The origin is inheriting its encryption root from its parent.
1712 * Check that the parent of the target has the same encryption root.
1714 ret = dsl_dir_get_encryption_root_ddobj(origin->dd_parent, &op_rddobj);
1715 if (ret == ENOENT)
1716 return (SET_ERROR(EACCES));
1717 else if (ret != 0)
1718 return (ret);
1720 ret = dsl_dir_get_encryption_root_ddobj(target->dd_parent, &tp_rddobj);
1721 if (ret == ENOENT)
1722 return (SET_ERROR(EACCES));
1723 else if (ret != 0)
1724 return (ret);
1726 if (op_rddobj != tp_rddobj)
1727 return (SET_ERROR(EACCES));
1729 return (0);
1732 void
1733 dsl_dataset_promote_crypt_sync(dsl_dir_t *target, dsl_dir_t *origin,
1734 dmu_tx_t *tx)
1736 uint64_t rddobj;
1737 dsl_pool_t *dp = target->dd_pool;
1738 dsl_dataset_t *targetds;
1739 dsl_dataset_t *originds;
1740 char *keylocation;
1742 if (origin->dd_crypto_obj == 0)
1743 return;
1744 if (dsl_dir_is_clone(origin))
1745 return;
1747 VERIFY0(dsl_dir_get_encryption_root_ddobj(origin, &rddobj));
1749 if (rddobj != origin->dd_object)
1750 return;
1753 * If the target is being promoted to the encryption root update the
1754 * DSL Crypto Key and keylocation to reflect that. We also need to
1755 * update the DSL Crypto Keys of all children inheritting their
1756 * encryption root to point to the new target. Otherwise, the check
1757 * function ensured that the encryption root will not change.
1759 keylocation = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);
1761 VERIFY0(dsl_dataset_hold_obj(dp,
1762 dsl_dir_phys(target)->dd_head_dataset_obj, FTAG, &targetds));
1763 VERIFY0(dsl_dataset_hold_obj(dp,
1764 dsl_dir_phys(origin)->dd_head_dataset_obj, FTAG, &originds));
1766 VERIFY0(dsl_prop_get_dd(origin, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1767 1, ZAP_MAXVALUELEN, keylocation, NULL, B_FALSE));
1768 dsl_prop_set_sync_impl(targetds, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1769 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1, keylocation, tx);
1770 dsl_prop_set_sync_impl(originds, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
1771 ZPROP_SRC_NONE, 0, 0, NULL, tx);
1773 rw_enter(&dp->dp_spa->spa_keystore.sk_wkeys_lock, RW_WRITER);
1774 spa_keystore_change_key_sync_impl(rddobj, origin->dd_object,
1775 target->dd_object, NULL, B_FALSE, tx);
1776 rw_exit(&dp->dp_spa->spa_keystore.sk_wkeys_lock);
1778 dsl_dataset_rele(targetds, FTAG);
1779 dsl_dataset_rele(originds, FTAG);
1780 kmem_free(keylocation, ZAP_MAXVALUELEN);
1784 dmu_objset_create_crypt_check(dsl_dir_t *parentdd, dsl_crypto_params_t *dcp,
1785 boolean_t *will_encrypt)
1787 int ret;
1788 uint64_t pcrypt, crypt;
1789 dsl_crypto_params_t dummy_dcp = { 0 };
1791 if (will_encrypt != NULL)
1792 *will_encrypt = B_FALSE;
1794 if (dcp == NULL)
1795 dcp = &dummy_dcp;
1797 if (dcp->cp_cmd != DCP_CMD_NONE)
1798 return (SET_ERROR(EINVAL));
1800 if (parentdd != NULL) {
1801 ret = dsl_dir_get_crypt(parentdd, &pcrypt);
1802 if (ret != 0)
1803 return (ret);
1804 } else {
1805 pcrypt = ZIO_CRYPT_OFF;
1808 crypt = (dcp->cp_crypt == ZIO_CRYPT_INHERIT) ? pcrypt : dcp->cp_crypt;
1810 ASSERT3U(pcrypt, !=, ZIO_CRYPT_INHERIT);
1811 ASSERT3U(crypt, !=, ZIO_CRYPT_INHERIT);
1813 /* check for valid dcp with no encryption (inherited or local) */
1814 if (crypt == ZIO_CRYPT_OFF) {
1815 /* Must not specify encryption params */
1816 if (dcp->cp_wkey != NULL ||
1817 (dcp->cp_keylocation != NULL &&
1818 strcmp(dcp->cp_keylocation, "none") != 0))
1819 return (SET_ERROR(EINVAL));
1821 return (0);
1824 if (will_encrypt != NULL)
1825 *will_encrypt = B_TRUE;
1828 * We will now definitely be encrypting. Check the feature flag. When
1829 * creating the pool the caller will check this for us since we won't
1830 * technically have the feature activated yet.
1832 if (parentdd != NULL &&
1833 !spa_feature_is_enabled(parentdd->dd_pool->dp_spa,
1834 SPA_FEATURE_ENCRYPTION)) {
1835 return (SET_ERROR(EOPNOTSUPP));
1838 /* Check for errata #4 (encryption enabled, bookmark_v2 disabled) */
1839 if (parentdd != NULL &&
1840 !spa_feature_is_enabled(parentdd->dd_pool->dp_spa,
1841 SPA_FEATURE_BOOKMARK_V2)) {
1842 return (SET_ERROR(EOPNOTSUPP));
1845 /* handle inheritance */
1846 if (dcp->cp_wkey == NULL) {
1847 ASSERT3P(parentdd, !=, NULL);
1849 /* key must be fully unspecified */
1850 if (dcp->cp_keylocation != NULL)
1851 return (SET_ERROR(EINVAL));
1853 /* parent must have a key to inherit */
1854 if (pcrypt == ZIO_CRYPT_OFF)
1855 return (SET_ERROR(EINVAL));
1857 /* check for parent key */
1858 ret = dmu_objset_check_wkey_loaded(parentdd);
1859 if (ret != 0)
1860 return (ret);
1862 return (0);
1865 /* At this point we should have a fully specified key. Check location */
1866 if (dcp->cp_keylocation == NULL ||
1867 !zfs_prop_valid_keylocation(dcp->cp_keylocation, B_TRUE))
1868 return (SET_ERROR(EINVAL));
1870 /* Must have fully specified keyformat */
1871 switch (dcp->cp_wkey->wk_keyformat) {
1872 case ZFS_KEYFORMAT_HEX:
1873 case ZFS_KEYFORMAT_RAW:
1874 /* requires no pbkdf2 iters and salt */
1875 if (dcp->cp_wkey->wk_salt != 0 || dcp->cp_wkey->wk_iters != 0)
1876 return (SET_ERROR(EINVAL));
1877 break;
1878 case ZFS_KEYFORMAT_PASSPHRASE:
1879 /* requires pbkdf2 iters and salt */
1880 if (dcp->cp_wkey->wk_salt == 0 ||
1881 dcp->cp_wkey->wk_iters < MIN_PBKDF2_ITERATIONS)
1882 return (SET_ERROR(EINVAL));
1883 break;
1884 case ZFS_KEYFORMAT_NONE:
1885 default:
1886 /* keyformat must be specified and valid */
1887 return (SET_ERROR(EINVAL));
1890 return (0);
1893 void
1894 dsl_dataset_create_crypt_sync(uint64_t dsobj, dsl_dir_t *dd,
1895 dsl_dataset_t *origin, dsl_crypto_params_t *dcp, dmu_tx_t *tx)
1897 dsl_pool_t *dp = dd->dd_pool;
1898 uint64_t crypt;
1899 dsl_wrapping_key_t *wkey;
1901 /* clones always use their origin's wrapping key */
1902 if (dsl_dir_is_clone(dd)) {
1903 ASSERT3P(dcp, ==, NULL);
1906 * If this is an encrypted clone we just need to clone the
1907 * dck into dd. Zapify the dd so we can do that.
1909 if (origin->ds_dir->dd_crypto_obj != 0) {
1910 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1911 dsl_dir_zapify(dd, tx);
1913 dd->dd_crypto_obj =
1914 dsl_crypto_key_clone_sync(origin->ds_dir, tx);
1915 VERIFY0(zap_add(dp->dp_meta_objset, dd->dd_object,
1916 DD_FIELD_CRYPTO_KEY_OBJ, sizeof (uint64_t), 1,
1917 &dd->dd_crypto_obj, tx));
1920 return;
1924 * A NULL dcp at this point indicates this is the origin dataset
1925 * which does not have an objset to encrypt. Raw receives will handle
1926 * encryption separately later. In both cases we can simply return.
1928 if (dcp == NULL || dcp->cp_cmd == DCP_CMD_RAW_RECV)
1929 return;
1931 crypt = dcp->cp_crypt;
1932 wkey = dcp->cp_wkey;
1934 /* figure out the effective crypt */
1935 if (crypt == ZIO_CRYPT_INHERIT && dd->dd_parent != NULL)
1936 VERIFY0(dsl_dir_get_crypt(dd->dd_parent, &crypt));
1938 /* if we aren't doing encryption just return */
1939 if (crypt == ZIO_CRYPT_OFF || crypt == ZIO_CRYPT_INHERIT)
1940 return;
1942 /* zapify the dd so that we can add the crypto key obj to it */
1943 dmu_buf_will_dirty(dd->dd_dbuf, tx);
1944 dsl_dir_zapify(dd, tx);
1946 /* use the new key if given or inherit from the parent */
1947 if (wkey == NULL) {
1948 VERIFY0(spa_keystore_wkey_hold_dd(dp->dp_spa,
1949 dd->dd_parent, FTAG, &wkey));
1950 } else {
1951 wkey->wk_ddobj = dd->dd_object;
1954 ASSERT3P(wkey, !=, NULL);
1956 /* Create or clone the DSL crypto key and activate the feature */
1957 dd->dd_crypto_obj = dsl_crypto_key_create_sync(crypt, wkey, tx);
1958 VERIFY0(zap_add(dp->dp_meta_objset, dd->dd_object,
1959 DD_FIELD_CRYPTO_KEY_OBJ, sizeof (uint64_t), 1, &dd->dd_crypto_obj,
1960 tx));
1961 dsl_dataset_activate_feature(dsobj, SPA_FEATURE_ENCRYPTION,
1962 (void *)B_TRUE, tx);
1965 * If we inherited the wrapping key we release our reference now.
1966 * Otherwise, this is a new key and we need to load it into the
1967 * keystore.
1969 if (dcp->cp_wkey == NULL) {
1970 dsl_wrapping_key_rele(wkey, FTAG);
1971 } else {
1972 VERIFY0(spa_keystore_load_wkey_impl(dp->dp_spa, wkey));
1976 typedef struct dsl_crypto_recv_key_arg {
1977 uint64_t dcrka_dsobj;
1978 uint64_t dcrka_fromobj;
1979 dmu_objset_type_t dcrka_ostype;
1980 nvlist_t *dcrka_nvl;
1981 boolean_t dcrka_do_key;
1982 } dsl_crypto_recv_key_arg_t;
1984 static int
1985 dsl_crypto_recv_raw_objset_check(dsl_dataset_t *ds, dsl_dataset_t *fromds,
1986 dmu_objset_type_t ostype, nvlist_t *nvl, dmu_tx_t *tx)
1988 int ret;
1989 objset_t *os;
1990 dnode_t *mdn;
1991 uint8_t *buf = NULL;
1992 uint_t len;
1993 uint64_t intval, nlevels, blksz, ibs;
1994 uint64_t nblkptr, maxblkid;
1996 if (ostype != DMU_OST_ZFS && ostype != DMU_OST_ZVOL)
1997 return (SET_ERROR(EINVAL));
1999 /* raw receives also need info about the structure of the metadnode */
2000 ret = nvlist_lookup_uint64(nvl, "mdn_compress", &intval);
2001 if (ret != 0 || intval >= ZIO_COMPRESS_LEGACY_FUNCTIONS)
2002 return (SET_ERROR(EINVAL));
2004 ret = nvlist_lookup_uint64(nvl, "mdn_checksum", &intval);
2005 if (ret != 0 || intval >= ZIO_CHECKSUM_LEGACY_FUNCTIONS)
2006 return (SET_ERROR(EINVAL));
2008 ret = nvlist_lookup_uint64(nvl, "mdn_nlevels", &nlevels);
2009 if (ret != 0 || nlevels > DN_MAX_LEVELS)
2010 return (SET_ERROR(EINVAL));
2012 ret = nvlist_lookup_uint64(nvl, "mdn_blksz", &blksz);
2013 if (ret != 0 || blksz < SPA_MINBLOCKSIZE)
2014 return (SET_ERROR(EINVAL));
2015 else if (blksz > spa_maxblocksize(tx->tx_pool->dp_spa))
2016 return (SET_ERROR(ENOTSUP));
2018 ret = nvlist_lookup_uint64(nvl, "mdn_indblkshift", &ibs);
2019 if (ret != 0 || ibs < DN_MIN_INDBLKSHIFT || ibs > DN_MAX_INDBLKSHIFT)
2020 return (SET_ERROR(ENOTSUP));
2022 ret = nvlist_lookup_uint64(nvl, "mdn_nblkptr", &nblkptr);
2023 if (ret != 0 || nblkptr != DN_MAX_NBLKPTR)
2024 return (SET_ERROR(ENOTSUP));
2026 ret = nvlist_lookup_uint64(nvl, "mdn_maxblkid", &maxblkid);
2027 if (ret != 0)
2028 return (SET_ERROR(EINVAL));
2030 ret = nvlist_lookup_uint8_array(nvl, "portable_mac", &buf, &len);
2031 if (ret != 0 || len != ZIO_OBJSET_MAC_LEN)
2032 return (SET_ERROR(EINVAL));
2034 ret = dmu_objset_from_ds(ds, &os);
2035 if (ret != 0)
2036 return (ret);
2039 * Useraccounting is not portable and must be done with the keys loaded.
2040 * Therefore, whenever we do any kind of receive the useraccounting
2041 * must not be present.
2043 ASSERT0(os->os_flags & OBJSET_FLAG_USERACCOUNTING_COMPLETE);
2044 ASSERT0(os->os_flags & OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE);
2046 mdn = DMU_META_DNODE(os);
2049 * If we already created the objset, make sure its unchangeable
2050 * properties match the ones received in the nvlist.
2052 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2053 if (!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)) &&
2054 (mdn->dn_nlevels != nlevels || mdn->dn_datablksz != blksz ||
2055 mdn->dn_indblkshift != ibs || mdn->dn_nblkptr != nblkptr)) {
2056 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2057 return (SET_ERROR(EINVAL));
2059 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2062 * Check that the ivset guid of the fromds matches the one from the
2063 * send stream. Older versions of the encryption code did not have
2064 * an ivset guid on the from dataset and did not send one in the
2065 * stream. For these streams we provide the
2066 * zfs_disable_ivset_guid_check tunable to allow these datasets to
2067 * be received with a generated ivset guid.
2069 if (fromds != NULL && !zfs_disable_ivset_guid_check) {
2070 uint64_t from_ivset_guid = 0;
2071 intval = 0;
2073 (void) nvlist_lookup_uint64(nvl, "from_ivset_guid", &intval);
2074 (void) zap_lookup(tx->tx_pool->dp_meta_objset,
2075 fromds->ds_object, DS_FIELD_IVSET_GUID,
2076 sizeof (from_ivset_guid), 1, &from_ivset_guid);
2078 if (intval == 0 || from_ivset_guid == 0)
2079 return (SET_ERROR(ZFS_ERR_FROM_IVSET_GUID_MISSING));
2081 if (intval != from_ivset_guid)
2082 return (SET_ERROR(ZFS_ERR_FROM_IVSET_GUID_MISMATCH));
2085 return (0);
2088 static void
2089 dsl_crypto_recv_raw_objset_sync(dsl_dataset_t *ds, dmu_objset_type_t ostype,
2090 nvlist_t *nvl, dmu_tx_t *tx)
2092 dsl_pool_t *dp = tx->tx_pool;
2093 objset_t *os;
2094 dnode_t *mdn;
2095 zio_t *zio;
2096 uint8_t *portable_mac;
2097 uint_t len;
2098 uint64_t compress, checksum, nlevels, blksz, ibs, maxblkid;
2099 boolean_t newds = B_FALSE;
2101 VERIFY0(dmu_objset_from_ds(ds, &os));
2102 mdn = DMU_META_DNODE(os);
2105 * Fetch the values we need from the nvlist. "to_ivset_guid" must
2106 * be set on the snapshot, which doesn't exist yet. The receive
2107 * code will take care of this for us later.
2109 compress = fnvlist_lookup_uint64(nvl, "mdn_compress");
2110 checksum = fnvlist_lookup_uint64(nvl, "mdn_checksum");
2111 nlevels = fnvlist_lookup_uint64(nvl, "mdn_nlevels");
2112 blksz = fnvlist_lookup_uint64(nvl, "mdn_blksz");
2113 ibs = fnvlist_lookup_uint64(nvl, "mdn_indblkshift");
2114 maxblkid = fnvlist_lookup_uint64(nvl, "mdn_maxblkid");
2115 VERIFY0(nvlist_lookup_uint8_array(nvl, "portable_mac", &portable_mac,
2116 &len));
2118 /* if we haven't created an objset for the ds yet, do that now */
2119 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
2120 if (BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {
2121 (void) dmu_objset_create_impl_dnstats(dp->dp_spa, ds,
2122 dsl_dataset_get_blkptr(ds), ostype, nlevels, blksz,
2123 ibs, tx);
2124 newds = B_TRUE;
2126 rrw_exit(&ds->ds_bp_rwlock, FTAG);
2129 * Set the portable MAC. The local MAC will always be zero since the
2130 * incoming data will all be portable and user accounting will be
2131 * deferred until the next mount. Afterwards, flag the os to be
2132 * written out raw next time.
2134 arc_release(os->os_phys_buf, &os->os_phys_buf);
2135 bcopy(portable_mac, os->os_phys->os_portable_mac, ZIO_OBJSET_MAC_LEN);
2136 bzero(os->os_phys->os_local_mac, ZIO_OBJSET_MAC_LEN);
2137 os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;
2139 /* set metadnode compression and checksum */
2140 mdn->dn_compress = compress;
2141 mdn->dn_checksum = checksum;
2143 rw_enter(&mdn->dn_struct_rwlock, RW_WRITER);
2144 dnode_new_blkid(mdn, maxblkid, tx, B_FALSE, B_TRUE);
2145 rw_exit(&mdn->dn_struct_rwlock);
2148 * We can't normally dirty the dataset in syncing context unless
2149 * we are creating a new dataset. In this case, we perform a
2150 * pseudo txg sync here instead.
2152 if (newds) {
2153 dsl_dataset_dirty(ds, tx);
2154 } else {
2155 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
2156 dsl_dataset_sync(ds, zio, tx);
2157 VERIFY0(zio_wait(zio));
2159 /* dsl_dataset_sync_done will drop this reference. */
2160 dmu_buf_add_ref(ds->ds_dbuf, ds);
2161 dsl_dataset_sync_done(ds, tx);
2166 dsl_crypto_recv_raw_key_check(dsl_dataset_t *ds, nvlist_t *nvl, dmu_tx_t *tx)
2168 int ret;
2169 objset_t *mos = tx->tx_pool->dp_meta_objset;
2170 uint8_t *buf = NULL;
2171 uint_t len;
2172 uint64_t intval, key_guid, version;
2173 boolean_t is_passphrase = B_FALSE;
2175 ASSERT(dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT);
2178 * Read and check all the encryption values from the nvlist. We need
2179 * all of the fields of a DSL Crypto Key, as well as a fully specified
2180 * wrapping key.
2182 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE, &intval);
2183 if (ret != 0 || intval >= ZIO_CRYPT_FUNCTIONS ||
2184 intval <= ZIO_CRYPT_OFF)
2185 return (SET_ERROR(EINVAL));
2187 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_GUID, &intval);
2188 if (ret != 0)
2189 return (SET_ERROR(EINVAL));
2192 * If this is an incremental receive make sure the given key guid
2193 * matches the one we already have.
2195 if (ds->ds_dir->dd_crypto_obj != 0) {
2196 ret = zap_lookup(mos, ds->ds_dir->dd_crypto_obj,
2197 DSL_CRYPTO_KEY_GUID, 8, 1, &key_guid);
2198 if (ret != 0)
2199 return (ret);
2200 if (intval != key_guid)
2201 return (SET_ERROR(EACCES));
2204 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2205 &buf, &len);
2206 if (ret != 0 || len != MASTER_KEY_MAX_LEN)
2207 return (SET_ERROR(EINVAL));
2209 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2210 &buf, &len);
2211 if (ret != 0 || len != SHA512_HMAC_KEYLEN)
2212 return (SET_ERROR(EINVAL));
2214 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_IV, &buf, &len);
2215 if (ret != 0 || len != WRAPPING_IV_LEN)
2216 return (SET_ERROR(EINVAL));
2218 ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, &buf, &len);
2219 if (ret != 0 || len != WRAPPING_MAC_LEN)
2220 return (SET_ERROR(EINVAL));
2223 * We don't support receiving old on-disk formats. The version 0
2224 * implementation protected several fields in an objset that were
2225 * not always portable during a raw receive. As a result, we call
2226 * the old version an on-disk errata #3.
2228 ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_VERSION, &version);
2229 if (ret != 0 || version != ZIO_CRYPT_KEY_CURRENT_VERSION)
2230 return (SET_ERROR(ENOTSUP));
2232 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_KEYFORMAT),
2233 &intval);
2234 if (ret != 0 || intval >= ZFS_KEYFORMAT_FORMATS ||
2235 intval == ZFS_KEYFORMAT_NONE)
2236 return (SET_ERROR(EINVAL));
2238 is_passphrase = (intval == ZFS_KEYFORMAT_PASSPHRASE);
2241 * for raw receives we allow any number of pbkdf2iters since there
2242 * won't be a chance for the user to change it.
2244 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),
2245 &intval);
2246 if (ret != 0 || (is_passphrase == (intval == 0)))
2247 return (SET_ERROR(EINVAL));
2249 ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),
2250 &intval);
2251 if (ret != 0 || (is_passphrase == (intval == 0)))
2252 return (SET_ERROR(EINVAL));
2254 return (0);
2257 void
2258 dsl_crypto_recv_raw_key_sync(dsl_dataset_t *ds, nvlist_t *nvl, dmu_tx_t *tx)
2260 dsl_pool_t *dp = tx->tx_pool;
2261 objset_t *mos = dp->dp_meta_objset;
2262 dsl_dir_t *dd = ds->ds_dir;
2263 uint_t len;
2264 uint64_t rddobj, one = 1;
2265 uint8_t *keydata, *hmac_keydata, *iv, *mac;
2266 uint64_t crypt, key_guid, keyformat, iters, salt;
2267 uint64_t version = ZIO_CRYPT_KEY_CURRENT_VERSION;
2268 char *keylocation = "prompt";
2270 /* lookup the values we need to create the DSL Crypto Key */
2271 crypt = fnvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE);
2272 key_guid = fnvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_GUID);
2273 keyformat = fnvlist_lookup_uint64(nvl,
2274 zfs_prop_to_name(ZFS_PROP_KEYFORMAT));
2275 iters = fnvlist_lookup_uint64(nvl,
2276 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS));
2277 salt = fnvlist_lookup_uint64(nvl,
2278 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT));
2279 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2280 &keydata, &len));
2281 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2282 &hmac_keydata, &len));
2283 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_IV, &iv, &len));
2284 VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, &mac, &len));
2286 /* if this is a new dataset setup the DSL Crypto Key. */
2287 if (dd->dd_crypto_obj == 0) {
2288 /* zapify the dsl dir so we can add the key object to it */
2289 dmu_buf_will_dirty(dd->dd_dbuf, tx);
2290 dsl_dir_zapify(dd, tx);
2292 /* create the DSL Crypto Key on disk and activate the feature */
2293 dd->dd_crypto_obj = zap_create(mos,
2294 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
2295 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2296 dd->dd_crypto_obj, DSL_CRYPTO_KEY_REFCOUNT,
2297 sizeof (uint64_t), 1, &one, tx));
2298 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,
2299 dd->dd_crypto_obj, DSL_CRYPTO_KEY_VERSION,
2300 sizeof (uint64_t), 1, &version, tx));
2302 dsl_dataset_activate_feature(ds->ds_object,
2303 SPA_FEATURE_ENCRYPTION, (void *)B_TRUE, tx);
2304 ds->ds_feature[SPA_FEATURE_ENCRYPTION] = (void *)B_TRUE;
2306 /* save the dd_crypto_obj on disk */
2307 VERIFY0(zap_add(mos, dd->dd_object, DD_FIELD_CRYPTO_KEY_OBJ,
2308 sizeof (uint64_t), 1, &dd->dd_crypto_obj, tx));
2311 * Set the keylocation to prompt by default. If keylocation
2312 * has been provided via the properties, this will be overridden
2313 * later.
2315 dsl_prop_set_sync_impl(ds,
2316 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2317 ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1,
2318 keylocation, tx);
2320 rddobj = dd->dd_object;
2321 } else {
2322 VERIFY0(dsl_dir_get_encryption_root_ddobj(dd, &rddobj));
2325 /* sync the key data to the ZAP object on disk */
2326 dsl_crypto_key_sync_impl(mos, dd->dd_crypto_obj, crypt,
2327 rddobj, key_guid, iv, mac, keydata, hmac_keydata, keyformat, salt,
2328 iters, tx);
2332 dsl_crypto_recv_key_check(void *arg, dmu_tx_t *tx)
2334 int ret;
2335 dsl_crypto_recv_key_arg_t *dcrka = arg;
2336 dsl_dataset_t *ds = NULL, *fromds = NULL;
2338 ret = dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_dsobj,
2339 FTAG, &ds);
2340 if (ret != 0)
2341 goto out;
2343 if (dcrka->dcrka_fromobj != 0) {
2344 ret = dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_fromobj,
2345 FTAG, &fromds);
2346 if (ret != 0)
2347 goto out;
2350 ret = dsl_crypto_recv_raw_objset_check(ds, fromds,
2351 dcrka->dcrka_ostype, dcrka->dcrka_nvl, tx);
2352 if (ret != 0)
2353 goto out;
2356 * We run this check even if we won't be doing this part of
2357 * the receive now so that we don't make the user wait until
2358 * the receive finishes to fail.
2360 ret = dsl_crypto_recv_raw_key_check(ds, dcrka->dcrka_nvl, tx);
2361 if (ret != 0)
2362 goto out;
2364 out:
2365 if (ds != NULL)
2366 dsl_dataset_rele(ds, FTAG);
2367 if (fromds != NULL)
2368 dsl_dataset_rele(fromds, FTAG);
2369 return (ret);
2372 void
2373 dsl_crypto_recv_key_sync(void *arg, dmu_tx_t *tx)
2375 dsl_crypto_recv_key_arg_t *dcrka = arg;
2376 dsl_dataset_t *ds;
2378 VERIFY0(dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_dsobj,
2379 FTAG, &ds));
2380 dsl_crypto_recv_raw_objset_sync(ds, dcrka->dcrka_ostype,
2381 dcrka->dcrka_nvl, tx);
2382 if (dcrka->dcrka_do_key)
2383 dsl_crypto_recv_raw_key_sync(ds, dcrka->dcrka_nvl, tx);
2384 dsl_dataset_rele(ds, FTAG);
2388 * This function is used to sync an nvlist representing a DSL Crypto Key and
2389 * the associated encryption parameters. The key will be written exactly as is
2390 * without wrapping it.
2393 dsl_crypto_recv_raw(const char *poolname, uint64_t dsobj, uint64_t fromobj,
2394 dmu_objset_type_t ostype, nvlist_t *nvl, boolean_t do_key)
2396 dsl_crypto_recv_key_arg_t dcrka;
2398 dcrka.dcrka_dsobj = dsobj;
2399 dcrka.dcrka_fromobj = fromobj;
2400 dcrka.dcrka_ostype = ostype;
2401 dcrka.dcrka_nvl = nvl;
2402 dcrka.dcrka_do_key = do_key;
2404 return (dsl_sync_task(poolname, dsl_crypto_recv_key_check,
2405 dsl_crypto_recv_key_sync, &dcrka, 1, ZFS_SPACE_CHECK_NORMAL));
2409 dsl_crypto_populate_key_nvlist(dsl_dataset_t *ds, uint64_t from_ivset_guid,
2410 nvlist_t **nvl_out)
2412 int ret;
2413 objset_t *os;
2414 dnode_t *mdn;
2415 uint64_t rddobj;
2416 nvlist_t *nvl = NULL;
2417 uint64_t dckobj = ds->ds_dir->dd_crypto_obj;
2418 dsl_dir_t *rdd = NULL;
2419 dsl_pool_t *dp = ds->ds_dir->dd_pool;
2420 objset_t *mos = dp->dp_meta_objset;
2421 uint64_t crypt = 0, key_guid = 0, format = 0;
2422 uint64_t iters = 0, salt = 0, version = 0;
2423 uint64_t to_ivset_guid = 0;
2424 uint8_t raw_keydata[MASTER_KEY_MAX_LEN];
2425 uint8_t raw_hmac_keydata[SHA512_HMAC_KEYLEN];
2426 uint8_t iv[WRAPPING_IV_LEN];
2427 uint8_t mac[WRAPPING_MAC_LEN];
2429 ASSERT(dckobj != 0);
2431 VERIFY0(dmu_objset_from_ds(ds, &os));
2432 mdn = DMU_META_DNODE(os);
2434 ret = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP);
2435 if (ret != 0)
2436 goto error;
2438 /* lookup values from the DSL Crypto Key */
2439 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,
2440 &crypt);
2441 if (ret != 0)
2442 goto error;
2444 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1, &key_guid);
2445 if (ret != 0)
2446 goto error;
2448 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,
2449 MASTER_KEY_MAX_LEN, raw_keydata);
2450 if (ret != 0)
2451 goto error;
2453 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,
2454 SHA512_HMAC_KEYLEN, raw_hmac_keydata);
2455 if (ret != 0)
2456 goto error;
2458 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,
2459 iv);
2460 if (ret != 0)
2461 goto error;
2463 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,
2464 mac);
2465 if (ret != 0)
2466 goto error;
2468 /* see zfs_disable_ivset_guid_check tunable for errata info */
2469 ret = zap_lookup(mos, ds->ds_object, DS_FIELD_IVSET_GUID, 8, 1,
2470 &to_ivset_guid);
2471 if (ret != 0)
2472 ASSERT3U(dp->dp_spa->spa_errata, !=, 0);
2475 * We don't support raw sends of legacy on-disk formats. See the
2476 * comment in dsl_crypto_recv_key_check() for details.
2478 ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_VERSION, 8, 1, &version);
2479 if (ret != 0 || version != ZIO_CRYPT_KEY_CURRENT_VERSION) {
2480 dp->dp_spa->spa_errata = ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;
2481 ret = SET_ERROR(ENOTSUP);
2482 goto error;
2486 * Lookup wrapping key properties. An early version of the code did
2487 * not correctly add these values to the wrapping key or the DSL
2488 * Crypto Key on disk for non encryption roots, so to be safe we
2489 * always take the slightly circuitous route of looking it up from
2490 * the encryption root's key.
2492 ret = dsl_dir_get_encryption_root_ddobj(ds->ds_dir, &rddobj);
2493 if (ret != 0)
2494 goto error;
2496 dsl_pool_config_enter(dp, FTAG);
2498 ret = dsl_dir_hold_obj(dp, rddobj, NULL, FTAG, &rdd);
2499 if (ret != 0)
2500 goto error_unlock;
2502 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
2503 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &format);
2504 if (ret != 0)
2505 goto error_unlock;
2507 if (format == ZFS_KEYFORMAT_PASSPHRASE) {
2508 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
2509 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &iters);
2510 if (ret != 0)
2511 goto error_unlock;
2513 ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,
2514 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &salt);
2515 if (ret != 0)
2516 goto error_unlock;
2519 dsl_dir_rele(rdd, FTAG);
2520 dsl_pool_config_exit(dp, FTAG);
2522 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE, crypt);
2523 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_GUID, key_guid);
2524 fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_VERSION, version);
2525 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,
2526 raw_keydata, MASTER_KEY_MAX_LEN));
2527 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,
2528 raw_hmac_keydata, SHA512_HMAC_KEYLEN));
2529 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_IV, iv,
2530 WRAPPING_IV_LEN));
2531 VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, mac,
2532 WRAPPING_MAC_LEN));
2533 VERIFY0(nvlist_add_uint8_array(nvl, "portable_mac",
2534 os->os_phys->os_portable_mac, ZIO_OBJSET_MAC_LEN));
2535 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_KEYFORMAT), format);
2536 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), iters);
2537 fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), salt);
2538 fnvlist_add_uint64(nvl, "mdn_checksum", mdn->dn_checksum);
2539 fnvlist_add_uint64(nvl, "mdn_compress", mdn->dn_compress);
2540 fnvlist_add_uint64(nvl, "mdn_nlevels", mdn->dn_nlevels);
2541 fnvlist_add_uint64(nvl, "mdn_blksz", mdn->dn_datablksz);
2542 fnvlist_add_uint64(nvl, "mdn_indblkshift", mdn->dn_indblkshift);
2543 fnvlist_add_uint64(nvl, "mdn_nblkptr", mdn->dn_nblkptr);
2544 fnvlist_add_uint64(nvl, "mdn_maxblkid", mdn->dn_maxblkid);
2545 fnvlist_add_uint64(nvl, "to_ivset_guid", to_ivset_guid);
2546 fnvlist_add_uint64(nvl, "from_ivset_guid", from_ivset_guid);
2548 *nvl_out = nvl;
2549 return (0);
2551 error_unlock:
2552 dsl_pool_config_exit(dp, FTAG);
2553 error:
2554 if (rdd != NULL)
2555 dsl_dir_rele(rdd, FTAG);
2556 nvlist_free(nvl);
2558 *nvl_out = NULL;
2559 return (ret);
2562 uint64_t
2563 dsl_crypto_key_create_sync(uint64_t crypt, dsl_wrapping_key_t *wkey,
2564 dmu_tx_t *tx)
2566 dsl_crypto_key_t dck;
2567 uint64_t version = ZIO_CRYPT_KEY_CURRENT_VERSION;
2568 uint64_t one = 1ULL;
2570 ASSERT(dmu_tx_is_syncing(tx));
2571 ASSERT3U(crypt, <, ZIO_CRYPT_FUNCTIONS);
2572 ASSERT3U(crypt, >, ZIO_CRYPT_OFF);
2574 /* create the DSL Crypto Key ZAP object */
2575 dck.dck_obj = zap_create(tx->tx_pool->dp_meta_objset,
2576 DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);
2578 /* fill in the key (on the stack) and sync it to disk */
2579 dck.dck_wkey = wkey;
2580 VERIFY0(zio_crypt_key_init(crypt, &dck.dck_key));
2582 dsl_crypto_key_sync(&dck, tx);
2583 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, dck.dck_obj,
2584 DSL_CRYPTO_KEY_REFCOUNT, sizeof (uint64_t), 1, &one, tx));
2585 VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, dck.dck_obj,
2586 DSL_CRYPTO_KEY_VERSION, sizeof (uint64_t), 1, &version, tx));
2588 zio_crypt_key_destroy(&dck.dck_key);
2589 bzero(&dck.dck_key, sizeof (zio_crypt_key_t));
2591 return (dck.dck_obj);
2594 uint64_t
2595 dsl_crypto_key_clone_sync(dsl_dir_t *origindd, dmu_tx_t *tx)
2597 objset_t *mos = tx->tx_pool->dp_meta_objset;
2599 ASSERT(dmu_tx_is_syncing(tx));
2601 VERIFY0(zap_increment(mos, origindd->dd_crypto_obj,
2602 DSL_CRYPTO_KEY_REFCOUNT, 1, tx));
2604 return (origindd->dd_crypto_obj);
2607 void
2608 dsl_crypto_key_destroy_sync(uint64_t dckobj, dmu_tx_t *tx)
2610 objset_t *mos = tx->tx_pool->dp_meta_objset;
2611 uint64_t refcnt;
2613 /* Decrement the refcount, destroy if this is the last reference */
2614 VERIFY0(zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_REFCOUNT,
2615 sizeof (uint64_t), 1, &refcnt));
2617 if (refcnt != 1) {
2618 VERIFY0(zap_increment(mos, dckobj, DSL_CRYPTO_KEY_REFCOUNT,
2619 -1, tx));
2620 } else {
2621 VERIFY0(zap_destroy(mos, dckobj, tx));
2625 void
2626 dsl_dataset_crypt_stats(dsl_dataset_t *ds, nvlist_t *nv)
2628 uint64_t intval;
2629 dsl_dir_t *dd = ds->ds_dir;
2630 dsl_dir_t *enc_root;
2631 char buf[ZFS_MAX_DATASET_NAME_LEN];
2633 if (dd->dd_crypto_obj == 0)
2634 return;
2636 intval = dsl_dataset_get_keystatus(dd);
2637 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEYSTATUS, intval);
2639 if (dsl_dir_get_crypt(dd, &intval) == 0)
2640 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_ENCRYPTION, intval);
2641 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2642 DSL_CRYPTO_KEY_GUID, 8, 1, &intval) == 0) {
2643 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEY_GUID, intval);
2645 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2646 zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &intval) == 0) {
2647 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEYFORMAT, intval);
2649 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2650 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &intval) == 0) {
2651 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_PBKDF2_SALT, intval);
2653 if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
2654 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &intval) == 0) {
2655 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_PBKDF2_ITERS, intval);
2657 if (zap_lookup(dd->dd_pool->dp_meta_objset, ds->ds_object,
2658 DS_FIELD_IVSET_GUID, 8, 1, &intval) == 0) {
2659 dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_IVSET_GUID, intval);
2662 if (dsl_dir_get_encryption_root_ddobj(dd, &intval) == 0) {
2663 if (dsl_dir_hold_obj(dd->dd_pool, intval, NULL, FTAG,
2664 &enc_root) == 0) {
2665 dsl_dir_name(enc_root, buf);
2666 dsl_dir_rele(enc_root, FTAG);
2667 dsl_prop_nvlist_add_string(nv,
2668 ZFS_PROP_ENCRYPTION_ROOT, buf);
2674 spa_crypt_get_salt(spa_t *spa, uint64_t dsobj, uint8_t *salt)
2676 int ret;
2677 dsl_crypto_key_t *dck = NULL;
2679 /* look up the key from the spa's keystore */
2680 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2681 if (ret != 0)
2682 goto error;
2684 ret = zio_crypt_key_get_salt(&dck->dck_key, salt);
2685 if (ret != 0)
2686 goto error;
2688 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2689 return (0);
2691 error:
2692 if (dck != NULL)
2693 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2694 return (ret);
2698 * Objset blocks are a special case for MAC generation. These blocks have 2
2699 * 256-bit MACs which are embedded within the block itself, rather than a
2700 * single 128 bit MAC. As a result, this function handles encoding and decoding
2701 * the MACs on its own, unlike other functions in this file.
2704 spa_do_crypt_objset_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj,
2705 abd_t *abd, uint_t datalen, boolean_t byteswap)
2707 int ret;
2708 dsl_crypto_key_t *dck = NULL;
2709 void *buf = abd_borrow_buf_copy(abd, datalen);
2710 objset_phys_t *osp = buf;
2711 uint8_t portable_mac[ZIO_OBJSET_MAC_LEN];
2712 uint8_t local_mac[ZIO_OBJSET_MAC_LEN];
2714 /* look up the key from the spa's keystore */
2715 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2716 if (ret != 0)
2717 goto error;
2719 /* calculate both HMACs */
2720 ret = zio_crypt_do_objset_hmacs(&dck->dck_key, buf, datalen,
2721 byteswap, portable_mac, local_mac);
2722 if (ret != 0)
2723 goto error;
2725 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2727 /* if we are generating encode the HMACs in the objset_phys_t */
2728 if (generate) {
2729 bcopy(portable_mac, osp->os_portable_mac, ZIO_OBJSET_MAC_LEN);
2730 bcopy(local_mac, osp->os_local_mac, ZIO_OBJSET_MAC_LEN);
2731 abd_return_buf_copy(abd, buf, datalen);
2732 return (0);
2735 if (bcmp(portable_mac, osp->os_portable_mac, ZIO_OBJSET_MAC_LEN) != 0 ||
2736 bcmp(local_mac, osp->os_local_mac, ZIO_OBJSET_MAC_LEN) != 0) {
2737 abd_return_buf(abd, buf, datalen);
2738 return (SET_ERROR(ECKSUM));
2741 abd_return_buf(abd, buf, datalen);
2743 return (0);
2745 error:
2746 if (dck != NULL)
2747 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2748 abd_return_buf(abd, buf, datalen);
2749 return (ret);
2753 spa_do_crypt_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj, abd_t *abd,
2754 uint_t datalen, uint8_t *mac)
2756 int ret;
2757 dsl_crypto_key_t *dck = NULL;
2758 uint8_t *buf = abd_borrow_buf_copy(abd, datalen);
2759 uint8_t digestbuf[ZIO_DATA_MAC_LEN];
2761 /* look up the key from the spa's keystore */
2762 ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);
2763 if (ret != 0)
2764 goto error;
2766 /* perform the hmac */
2767 ret = zio_crypt_do_hmac(&dck->dck_key, buf, datalen,
2768 digestbuf, ZIO_DATA_MAC_LEN);
2769 if (ret != 0)
2770 goto error;
2772 abd_return_buf(abd, buf, datalen);
2773 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2776 * Truncate and fill in mac buffer if we were asked to generate a MAC.
2777 * Otherwise verify that the MAC matched what we expected.
2779 if (generate) {
2780 bcopy(digestbuf, mac, ZIO_DATA_MAC_LEN);
2781 return (0);
2784 if (bcmp(digestbuf, mac, ZIO_DATA_MAC_LEN) != 0)
2785 return (SET_ERROR(ECKSUM));
2787 return (0);
2789 error:
2790 if (dck != NULL)
2791 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2792 abd_return_buf(abd, buf, datalen);
2793 return (ret);
2797 * This function serves as a multiplexer for encryption and decryption of
2798 * all blocks (except the L2ARC). For encryption, it will populate the IV,
2799 * salt, MAC, and cabd (the ciphertext). On decryption it will simply use
2800 * these fields to populate pabd (the plaintext).
2803 spa_do_crypt_abd(boolean_t encrypt, spa_t *spa, const zbookmark_phys_t *zb,
2804 dmu_object_type_t ot, boolean_t dedup, boolean_t bswap, uint8_t *salt,
2805 uint8_t *iv, uint8_t *mac, uint_t datalen, abd_t *pabd, abd_t *cabd,
2806 boolean_t *no_crypt)
2808 int ret;
2809 dsl_crypto_key_t *dck = NULL;
2810 uint8_t *plainbuf = NULL, *cipherbuf = NULL;
2812 ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));
2814 /* look up the key from the spa's keystore */
2815 ret = spa_keystore_lookup_key(spa, zb->zb_objset, FTAG, &dck);
2816 if (ret != 0) {
2817 ret = SET_ERROR(EACCES);
2818 return (ret);
2821 if (encrypt) {
2822 plainbuf = abd_borrow_buf_copy(pabd, datalen);
2823 cipherbuf = abd_borrow_buf(cabd, datalen);
2824 } else {
2825 plainbuf = abd_borrow_buf(pabd, datalen);
2826 cipherbuf = abd_borrow_buf_copy(cabd, datalen);
2830 * Both encryption and decryption functions need a salt for key
2831 * generation and an IV. When encrypting a non-dedup block, we
2832 * generate the salt and IV randomly to be stored by the caller. Dedup
2833 * blocks perform a (more expensive) HMAC of the plaintext to obtain
2834 * the salt and the IV. ZIL blocks have their salt and IV generated
2835 * at allocation time in zio_alloc_zil(). On decryption, we simply use
2836 * the provided values.
2838 if (encrypt && ot != DMU_OT_INTENT_LOG && !dedup) {
2839 ret = zio_crypt_key_get_salt(&dck->dck_key, salt);
2840 if (ret != 0)
2841 goto error;
2843 ret = zio_crypt_generate_iv(iv);
2844 if (ret != 0)
2845 goto error;
2846 } else if (encrypt && dedup) {
2847 ret = zio_crypt_generate_iv_salt_dedup(&dck->dck_key,
2848 plainbuf, datalen, iv, salt);
2849 if (ret != 0)
2850 goto error;
2853 /* call lower level function to perform encryption / decryption */
2854 ret = zio_do_crypt_data(encrypt, &dck->dck_key, ot, bswap, salt, iv,
2855 mac, datalen, plainbuf, cipherbuf, no_crypt);
2858 * Handle injected decryption faults. Unfortunately, we cannot inject
2859 * faults for dnode blocks because we might trigger the panic in
2860 * dbuf_prepare_encrypted_dnode_leaf(), which exists because syncing
2861 * context is not prepared to handle malicious decryption failures.
2863 if (zio_injection_enabled && !encrypt && ot != DMU_OT_DNODE && ret == 0)
2864 ret = zio_handle_decrypt_injection(spa, zb, ot, ECKSUM);
2865 if (ret != 0)
2866 goto error;
2868 if (encrypt) {
2869 abd_return_buf(pabd, plainbuf, datalen);
2870 abd_return_buf_copy(cabd, cipherbuf, datalen);
2871 } else {
2872 abd_return_buf_copy(pabd, plainbuf, datalen);
2873 abd_return_buf(cabd, cipherbuf, datalen);
2876 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2878 return (0);
2880 error:
2881 if (encrypt) {
2882 /* zero out any state we might have changed while encrypting */
2883 bzero(salt, ZIO_DATA_SALT_LEN);
2884 bzero(iv, ZIO_DATA_IV_LEN);
2885 bzero(mac, ZIO_DATA_MAC_LEN);
2886 abd_return_buf(pabd, plainbuf, datalen);
2887 abd_return_buf_copy(cabd, cipherbuf, datalen);
2888 } else {
2889 abd_return_buf_copy(pabd, plainbuf, datalen);
2890 abd_return_buf(cabd, cipherbuf, datalen);
2893 spa_keystore_dsl_key_rele(spa, dck, FTAG);
2895 return (ret);
2898 #if defined(_KERNEL)
2899 module_param(zfs_disable_ivset_guid_check, int, 0644);
2900 MODULE_PARM_DESC(zfs_disable_ivset_guid_check,
2901 "Set to allow raw receives without IVset guids");
2902 #endif