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
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.
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>
25 #include <sys/dsl_dir.h>
26 #include <sys/dsl_prop.h>
27 #include <sys/spa_impl.h>
28 #include <sys/dmu_objset.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
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;
83 dsl_wrapping_key_hold(dsl_wrapping_key_t
*wkey
, void *tag
)
85 (void) zfs_refcount_add(&wkey
->wk_refcnt
, tag
);
89 dsl_wrapping_key_rele(dsl_wrapping_key_t
*wkey
, void *tag
)
91 (void) zfs_refcount_remove(&wkey
->wk_refcnt
, tag
);
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
));
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
)
115 dsl_wrapping_key_t
*wkey
;
117 /* allocate the wrapping key */
118 wkey
= kmem_alloc(sizeof (dsl_wrapping_key_t
), KM_SLEEP
);
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
) {
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
;
143 dsl_wrapping_key_free(wkey
);
150 dsl_crypto_params_create_nvlist(dcp_cmd_t cmd
, nvlist_t
*props
,
151 nvlist_t
*crypto_args
, dsl_crypto_params_t
**dcp_out
)
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
);
165 ret
= SET_ERROR(ENOMEM
);
171 /* get relevant arguments from the nvlists */
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
);
200 /* check for valid crypt */
201 if (dcp
->cp_crypt
>= ZIO_CRYPT_FUNCTIONS
) {
202 ret
= SET_ERROR(EINVAL
);
205 dcp
->cp_crypt
= crypt
;
208 /* check for valid keyformat */
209 if (keyformat
>= ZFS_KEYFORMAT_FORMATS
) {
210 ret
= SET_ERROR(EINVAL
);
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
);
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
);
230 /* if the user asked for the deault 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
,
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
));
261 dsl_wrapping_key_free(wkey
);
263 kmem_free(dcp
, sizeof (dsl_crypto_params_t
));
270 dsl_crypto_params_free(dsl_crypto_params_t
*dcp
, boolean_t unload
)
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
));
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
)
291 if (dcka
->dck_obj
> dckb
->dck_obj
)
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
)
304 if (kma
->km_dsobj
> kmb
->km_dsobj
)
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
)
317 if (wka
->wk_ddobj
> wkb
->wk_ddobj
)
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
));
339 spa_keystore_fini(spa_keystore_t
*sk
)
341 dsl_wrapping_key_t
*wkey
;
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
);
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
)
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
);
384 dsl_dir_incompatible_encryption_version(dsl_dir_t
*dd
)
387 uint64_t version
= 0;
389 ret
= dsl_dir_get_encryption_version(dd
, &version
);
393 return (version
!= ZIO_CRYPT_KEY_CURRENT_VERSION
);
397 spa_keystore_wkey_hold_ddobj_impl(spa_t
*spa
, uint64_t ddobj
,
398 void *tag
, dsl_wrapping_key_t
**wkey_out
)
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
);
412 ret
= SET_ERROR(ENOENT
);
416 /* increment the refcount */
417 dsl_wrapping_key_hold(found_wkey
, tag
);
419 *wkey_out
= found_wkey
;
428 spa_keystore_wkey_hold_dd(spa_t
*spa
, dsl_dir_t
*dd
, void *tag
,
429 dsl_wrapping_key_t
**wkey_out
)
432 dsl_wrapping_key_t
*wkey
;
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
);
441 /* get the ddobj that the keylocation property was inherited from */
442 ret
= dsl_dir_get_encryption_root_ddobj(dd
, &rddobj
);
446 /* lookup the wkey in the avl tree */
447 ret
= spa_keystore_wkey_hold_ddobj_impl(spa
, rddobj
, tag
, &wkey
);
451 /* unlock the wkey tree if we locked it */
453 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
460 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
467 dsl_crypto_can_set_keylocation(const char *dsname
, const char *keylocation
)
470 dsl_dir_t
*dd
= NULL
;
471 dsl_pool_t
*dp
= NULL
;
474 /* hold the dsl dir */
475 ret
= dsl_pool_hold(dsname
, FTAG
, &dp
);
479 ret
= dsl_dir_hold(dp
, dsname
, FTAG
, &dd
, NULL
);
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
);
496 /* check for a valid keylocation for encrypted datasets */
497 if (!zfs_prop_valid_keylocation(keylocation
, B_TRUE
)) {
498 ret
= SET_ERROR(EINVAL
);
502 /* check that this is an encryption root */
503 ret
= dsl_dir_get_encryption_root_ddobj(dd
, &rddobj
);
507 if (rddobj
!= dd
->dd_object
) {
508 ret
= SET_ERROR(EACCES
);
512 dsl_dir_rele(dd
, FTAG
);
513 dsl_pool_rele(dp
, FTAG
);
519 dsl_dir_rele(dd
, FTAG
);
521 dsl_pool_rele(dp
, FTAG
);
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
);
537 dsl_wrapping_key_rele(dck
->dck_wkey
, dck
);
540 kmem_free(dck
, sizeof (dsl_crypto_key_t
));
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
);
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
)
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
);
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,
573 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_GUID
, 8, 1, &guid
);
577 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_MASTER_KEY
, 1,
578 MASTER_KEY_MAX_LEN
, raw_keydata
);
582 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_HMAC_KEY
, 1,
583 SHA512_HMAC_KEYLEN
, raw_hmac_keydata
);
587 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_IV
, 1, WRAPPING_IV_LEN
,
592 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_MAC
, 1, WRAPPING_MAC_LEN
,
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
);
607 ret
= SET_ERROR(EACCES
);
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
);
623 bzero(dck
, sizeof (dsl_crypto_key_t
));
624 kmem_free(dck
, sizeof (dsl_crypto_key_t
));
632 spa_keystore_dsl_key_hold_impl(spa_t
*spa
, uint64_t dckobj
, void *tag
,
633 dsl_crypto_key_t
**dck_out
)
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
);
647 ret
= SET_ERROR(ENOENT
);
651 /* increment the refcount */
652 zfs_refcount_add(&found_dck
->dck_holds
, tag
);
654 *dck_out
= found_dck
;
663 spa_keystore_dsl_key_hold_dd(spa_t
*spa
, dsl_dir_t
*dd
, void *tag
,
664 dsl_crypto_key_t
**dck_out
)
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
);
681 /* Lookup the wrapping key from the keystore */
682 ret
= spa_keystore_wkey_hold_dd(spa
, dd
, FTAG
, &wkey
);
685 return (SET_ERROR(EACCES
));
688 /* Read the key from disk */
689 ret
= dsl_crypto_key_open(spa
->spa_meta_objset
, wkey
, dckobj
,
692 dsl_wrapping_key_rele(wkey
, FTAG
);
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
);
705 avl_find(&spa
->spa_keystore
.sk_dsl_keys
, dck_io
, &where
);
706 avl_insert(&spa
->spa_keystore
.sk_dsl_keys
, dck_io
, where
);
709 dsl_crypto_key_free(dck_io
);
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
);
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
)
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
);
748 avl_insert(&spa
->spa_keystore
.sk_wkeys
, wkey
, where
);
750 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
755 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
760 spa_keystore_load_wkey(const char *dsname
, dsl_crypto_params_t
*dcp
,
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
);
784 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ENCRYPTION
)) {
785 ret
= SET_ERROR(ENOTSUP
);
789 /* hold the dsl dir */
790 ret
= dsl_dir_hold(dp
, dsname
, FTAG
, &dd
, NULL
);
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
);
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
);
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
);
818 ret
= zap_lookup(dp
->dp_meta_objset
, dd
->dd_crypto_obj
,
819 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 8, 1, &salt
);
823 ret
= zap_lookup(dp
->dp_meta_objset
, dd
->dd_crypto_obj
,
824 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 8, 1, &iters
);
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.
847 /* insert the wrapping key into the keystore */
848 ret
= spa_keystore_load_wkey_impl(dp
->dp_spa
, wkey
);
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
);
863 dsl_crypto_key_rele(dck
, FTAG
);
865 dsl_dir_rele(dd
, FTAG
);
867 dsl_pool_rele(dp
, FTAG
);
873 spa_keystore_unload_wkey_impl(spa_t
*spa
, uint64_t ddobj
)
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
,
888 ret
= SET_ERROR(EACCES
);
890 } else if (zfs_refcount_count(&found_wkey
->wk_refcnt
) != 0) {
891 ret
= SET_ERROR(EBUSY
);
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
);
904 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
909 spa_keystore_unload_wkey(const char *dsname
)
912 dsl_dir_t
*dd
= NULL
;
913 dsl_pool_t
*dp
= NULL
;
916 ret
= spa_open(dsname
, &spa
, FTAG
);
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
);
934 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ENCRYPTION
)) {
935 ret
= (SET_ERROR(ENOTSUP
));
939 ret
= dsl_dir_hold(dp
, dsname
, FTAG
, &dd
, NULL
);
945 /* unload the wkey */
946 ret
= spa_keystore_unload_wkey_impl(dp
->dp_spa
, dd
->dd_object
);
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
);
960 dsl_dir_rele(dd
, FTAG
);
962 dsl_pool_rele(dp
, FTAG
);
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.
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)
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
);
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 kmem_free(km
, sizeof (dsl_key_mapping_t
));
1015 spa_keystore_create_mapping(spa_t
*spa
, dsl_dataset_t
*ds
, void *tag
,
1016 dsl_key_mapping_t
**km_out
)
1020 dsl_key_mapping_t
*km
, *found_km
;
1021 boolean_t should_free
= B_FALSE
;
1023 /* Allocate and initialize the mapping */
1024 km
= kmem_zalloc(sizeof (dsl_key_mapping_t
), KM_SLEEP
);
1025 zfs_refcount_create(&km
->km_refcnt
);
1027 ret
= spa_keystore_dsl_key_hold_dd(spa
, ds
->ds_dir
, km
, &km
->km_key
);
1029 zfs_refcount_destroy(&km
->km_refcnt
);
1030 kmem_free(km
, sizeof (dsl_key_mapping_t
));
1037 km
->km_dsobj
= ds
->ds_object
;
1039 rw_enter(&spa
->spa_keystore
.sk_km_lock
, RW_WRITER
);
1042 * If a mapping already exists, simply increment its refcount and
1043 * cleanup the one we made. We want to allocate / free outside of
1044 * the lock because this lock is also used by the zio layer to lookup
1045 * key mappings. Otherwise, use the one we created. Normally, there will
1046 * only be one active reference at a time (the objset owner), but there
1047 * are times when there could be multiple async users.
1049 found_km
= avl_find(&spa
->spa_keystore
.sk_key_mappings
, km
, &where
);
1050 if (found_km
!= NULL
) {
1051 should_free
= B_TRUE
;
1052 zfs_refcount_add(&found_km
->km_refcnt
, tag
);
1056 zfs_refcount_add(&km
->km_refcnt
, tag
);
1057 avl_insert(&spa
->spa_keystore
.sk_key_mappings
, km
, where
);
1062 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1065 spa_keystore_dsl_key_rele(spa
, km
->km_key
, km
);
1066 zfs_refcount_destroy(&km
->km_refcnt
);
1067 kmem_free(km
, sizeof (dsl_key_mapping_t
));
1074 spa_keystore_remove_mapping(spa_t
*spa
, uint64_t dsobj
, void *tag
)
1077 dsl_key_mapping_t search_km
;
1078 dsl_key_mapping_t
*found_km
;
1080 /* init the search key mapping */
1081 search_km
.km_dsobj
= dsobj
;
1083 rw_enter(&spa
->spa_keystore
.sk_km_lock
, RW_READER
);
1085 /* find the matching mapping */
1086 found_km
= avl_find(&spa
->spa_keystore
.sk_key_mappings
,
1088 if (found_km
== NULL
) {
1089 ret
= SET_ERROR(ENOENT
);
1093 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1095 key_mapping_rele(spa
, found_km
, tag
);
1100 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1105 * This function is primarily used by the zio and arc layer to lookup
1106 * DSL Crypto Keys for encryption. Callers must release the key with
1107 * spa_keystore_dsl_key_rele(). The function may also be called with
1108 * dck_out == NULL and tag == NULL to simply check that a key exists
1109 * without getting a reference to it.
1112 spa_keystore_lookup_key(spa_t
*spa
, uint64_t dsobj
, void *tag
,
1113 dsl_crypto_key_t
**dck_out
)
1116 dsl_key_mapping_t search_km
;
1117 dsl_key_mapping_t
*found_km
;
1119 ASSERT((tag
!= NULL
&& dck_out
!= NULL
) ||
1120 (tag
== NULL
&& dck_out
== NULL
));
1122 /* init the search key mapping */
1123 search_km
.km_dsobj
= dsobj
;
1125 rw_enter(&spa
->spa_keystore
.sk_km_lock
, RW_READER
);
1127 /* remove the mapping from the tree */
1128 found_km
= avl_find(&spa
->spa_keystore
.sk_key_mappings
, &search_km
,
1130 if (found_km
== NULL
) {
1131 ret
= SET_ERROR(ENOENT
);
1135 if (found_km
&& tag
)
1136 zfs_refcount_add(&found_km
->km_key
->dck_holds
, tag
);
1138 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1140 if (dck_out
!= NULL
)
1141 *dck_out
= found_km
->km_key
;
1145 rw_exit(&spa
->spa_keystore
.sk_km_lock
);
1147 if (dck_out
!= NULL
)
1153 dmu_objset_check_wkey_loaded(dsl_dir_t
*dd
)
1156 dsl_wrapping_key_t
*wkey
= NULL
;
1158 ret
= spa_keystore_wkey_hold_dd(dd
->dd_pool
->dp_spa
, dd
, FTAG
,
1161 return (SET_ERROR(EACCES
));
1163 dsl_wrapping_key_rele(wkey
, FTAG
);
1168 static zfs_keystatus_t
1169 dsl_dataset_get_keystatus(dsl_dir_t
*dd
)
1171 /* check if this dd has a has a dsl key */
1172 if (dd
->dd_crypto_obj
== 0)
1173 return (ZFS_KEYSTATUS_NONE
);
1175 return (dmu_objset_check_wkey_loaded(dd
) == 0 ?
1176 ZFS_KEYSTATUS_AVAILABLE
: ZFS_KEYSTATUS_UNAVAILABLE
);
1180 dsl_dir_get_crypt(dsl_dir_t
*dd
, uint64_t *crypt
)
1182 if (dd
->dd_crypto_obj
== 0) {
1183 *crypt
= ZIO_CRYPT_OFF
;
1187 return (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
1188 DSL_CRYPTO_KEY_CRYPTO_SUITE
, 8, 1, crypt
));
1192 dsl_crypto_key_sync_impl(objset_t
*mos
, uint64_t dckobj
, uint64_t crypt
,
1193 uint64_t root_ddobj
, uint64_t guid
, uint8_t *iv
, uint8_t *mac
,
1194 uint8_t *keydata
, uint8_t *hmac_keydata
, uint64_t keyformat
,
1195 uint64_t salt
, uint64_t iters
, dmu_tx_t
*tx
)
1197 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, 8, 1,
1199 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_ROOT_DDOBJ
, 8, 1,
1201 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_GUID
, 8, 1,
1203 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_IV
, 1, WRAPPING_IV_LEN
,
1205 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_MAC
, 1, WRAPPING_MAC_LEN
,
1207 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_MASTER_KEY
, 1,
1208 MASTER_KEY_MAX_LEN
, keydata
, tx
));
1209 VERIFY0(zap_update(mos
, dckobj
, DSL_CRYPTO_KEY_HMAC_KEY
, 1,
1210 SHA512_HMAC_KEYLEN
, hmac_keydata
, tx
));
1211 VERIFY0(zap_update(mos
, dckobj
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
),
1212 8, 1, &keyformat
, tx
));
1213 VERIFY0(zap_update(mos
, dckobj
, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
),
1215 VERIFY0(zap_update(mos
, dckobj
, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
),
1220 dsl_crypto_key_sync(dsl_crypto_key_t
*dck
, dmu_tx_t
*tx
)
1222 zio_crypt_key_t
*key
= &dck
->dck_key
;
1223 dsl_wrapping_key_t
*wkey
= dck
->dck_wkey
;
1224 uint8_t keydata
[MASTER_KEY_MAX_LEN
];
1225 uint8_t hmac_keydata
[SHA512_HMAC_KEYLEN
];
1226 uint8_t iv
[WRAPPING_IV_LEN
];
1227 uint8_t mac
[WRAPPING_MAC_LEN
];
1229 ASSERT(dmu_tx_is_syncing(tx
));
1230 ASSERT3U(key
->zk_crypt
, <, ZIO_CRYPT_FUNCTIONS
);
1232 /* encrypt and store the keys along with the IV and MAC */
1233 VERIFY0(zio_crypt_key_wrap(&dck
->dck_wkey
->wk_key
, key
, iv
, mac
,
1234 keydata
, hmac_keydata
));
1236 /* update the ZAP with the obtained values */
1237 dsl_crypto_key_sync_impl(tx
->tx_pool
->dp_meta_objset
, dck
->dck_obj
,
1238 key
->zk_crypt
, wkey
->wk_ddobj
, key
->zk_guid
, iv
, mac
, keydata
,
1239 hmac_keydata
, wkey
->wk_keyformat
, wkey
->wk_salt
, wkey
->wk_iters
,
1243 typedef struct spa_keystore_change_key_args
{
1244 const char *skcka_dsname
;
1245 dsl_crypto_params_t
*skcka_cp
;
1246 } spa_keystore_change_key_args_t
;
1249 spa_keystore_change_key_check(void *arg
, dmu_tx_t
*tx
)
1252 dsl_dir_t
*dd
= NULL
;
1253 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1254 spa_keystore_change_key_args_t
*skcka
= arg
;
1255 dsl_crypto_params_t
*dcp
= skcka
->skcka_cp
;
1258 /* check for the encryption feature */
1259 if (!spa_feature_is_enabled(dp
->dp_spa
, SPA_FEATURE_ENCRYPTION
)) {
1260 ret
= SET_ERROR(ENOTSUP
);
1264 /* check for valid key change command */
1265 if (dcp
->cp_cmd
!= DCP_CMD_NEW_KEY
&&
1266 dcp
->cp_cmd
!= DCP_CMD_INHERIT
&&
1267 dcp
->cp_cmd
!= DCP_CMD_FORCE_NEW_KEY
&&
1268 dcp
->cp_cmd
!= DCP_CMD_FORCE_INHERIT
) {
1269 ret
= SET_ERROR(EINVAL
);
1274 ret
= dsl_dir_hold(dp
, skcka
->skcka_dsname
, FTAG
, &dd
, NULL
);
1280 /* verify that the dataset is encrypted */
1281 if (dd
->dd_crypto_obj
== 0) {
1282 ret
= SET_ERROR(EINVAL
);
1286 /* clones must always use their origin's key */
1287 if (dsl_dir_is_clone(dd
)) {
1288 ret
= SET_ERROR(EINVAL
);
1292 /* lookup the ddobj we are inheriting the keylocation from */
1293 ret
= dsl_dir_get_encryption_root_ddobj(dd
, &rddobj
);
1297 /* Handle inheritance */
1298 if (dcp
->cp_cmd
== DCP_CMD_INHERIT
||
1299 dcp
->cp_cmd
== DCP_CMD_FORCE_INHERIT
) {
1300 /* no other encryption params should be given */
1301 if (dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
||
1302 dcp
->cp_keylocation
!= NULL
||
1303 dcp
->cp_wkey
!= NULL
) {
1304 ret
= SET_ERROR(EINVAL
);
1308 /* check that this is an encryption root */
1309 if (dd
->dd_object
!= rddobj
) {
1310 ret
= SET_ERROR(EINVAL
);
1314 /* check that the parent is encrypted */
1315 if (dd
->dd_parent
->dd_crypto_obj
== 0) {
1316 ret
= SET_ERROR(EINVAL
);
1320 /* if we are rewrapping check that both keys are loaded */
1321 if (dcp
->cp_cmd
== DCP_CMD_INHERIT
) {
1322 ret
= dmu_objset_check_wkey_loaded(dd
);
1326 ret
= dmu_objset_check_wkey_loaded(dd
->dd_parent
);
1331 dsl_dir_rele(dd
, FTAG
);
1335 /* handle forcing an encryption root without rewrapping */
1336 if (dcp
->cp_cmd
== DCP_CMD_FORCE_NEW_KEY
) {
1337 /* no other encryption params should be given */
1338 if (dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
||
1339 dcp
->cp_keylocation
!= NULL
||
1340 dcp
->cp_wkey
!= NULL
) {
1341 ret
= SET_ERROR(EINVAL
);
1345 /* check that this is not an encryption root */
1346 if (dd
->dd_object
== rddobj
) {
1347 ret
= SET_ERROR(EINVAL
);
1351 dsl_dir_rele(dd
, FTAG
);
1355 /* crypt cannot be changed after creation */
1356 if (dcp
->cp_crypt
!= ZIO_CRYPT_INHERIT
) {
1357 ret
= SET_ERROR(EINVAL
);
1361 /* we are not inheritting our parent's wkey so we need one ourselves */
1362 if (dcp
->cp_wkey
== NULL
) {
1363 ret
= SET_ERROR(EINVAL
);
1367 /* check for a valid keyformat for the new wrapping key */
1368 if (dcp
->cp_wkey
->wk_keyformat
>= ZFS_KEYFORMAT_FORMATS
||
1369 dcp
->cp_wkey
->wk_keyformat
== ZFS_KEYFORMAT_NONE
) {
1370 ret
= SET_ERROR(EINVAL
);
1375 * If this dataset is not currently an encryption root we need a new
1376 * keylocation for this dataset's new wrapping key. Otherwise we can
1377 * just keep the one we already had.
1379 if (dd
->dd_object
!= rddobj
&& dcp
->cp_keylocation
== NULL
) {
1380 ret
= SET_ERROR(EINVAL
);
1384 /* check that the keylocation is valid if it is not NULL */
1385 if (dcp
->cp_keylocation
!= NULL
&&
1386 !zfs_prop_valid_keylocation(dcp
->cp_keylocation
, B_TRUE
)) {
1387 ret
= SET_ERROR(EINVAL
);
1391 /* passphrases require pbkdf2 salt and iters */
1392 if (dcp
->cp_wkey
->wk_keyformat
== ZFS_KEYFORMAT_PASSPHRASE
) {
1393 if (dcp
->cp_wkey
->wk_salt
== 0 ||
1394 dcp
->cp_wkey
->wk_iters
< MIN_PBKDF2_ITERATIONS
) {
1395 ret
= SET_ERROR(EINVAL
);
1399 if (dcp
->cp_wkey
->wk_salt
!= 0 || dcp
->cp_wkey
->wk_iters
!= 0) {
1400 ret
= SET_ERROR(EINVAL
);
1405 /* make sure the dd's wkey is loaded */
1406 ret
= dmu_objset_check_wkey_loaded(dd
);
1410 dsl_dir_rele(dd
, FTAG
);
1416 dsl_dir_rele(dd
, FTAG
);
1423 spa_keystore_change_key_sync_impl(uint64_t rddobj
, uint64_t ddobj
,
1424 uint64_t new_rddobj
, dsl_wrapping_key_t
*wkey
, dmu_tx_t
*tx
)
1427 zap_attribute_t
*za
;
1428 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1429 dsl_dir_t
*dd
= NULL
;
1430 dsl_crypto_key_t
*dck
= NULL
;
1431 uint64_t curr_rddobj
;
1433 ASSERT(RW_WRITE_HELD(&dp
->dp_spa
->spa_keystore
.sk_wkeys_lock
));
1436 VERIFY0(dsl_dir_hold_obj(dp
, ddobj
, NULL
, FTAG
, &dd
));
1438 /* ignore hidden dsl dirs */
1439 if (dd
->dd_myname
[0] == '$' || dd
->dd_myname
[0] == '%') {
1440 dsl_dir_rele(dd
, FTAG
);
1445 * Stop recursing if this dsl dir didn't inherit from the root
1446 * or if this dd is a clone.
1448 VERIFY0(dsl_dir_get_encryption_root_ddobj(dd
, &curr_rddobj
));
1449 if (curr_rddobj
!= rddobj
|| dsl_dir_is_clone(dd
)) {
1450 dsl_dir_rele(dd
, FTAG
);
1455 * If we don't have a wrapping key just update the dck to reflect the
1456 * new encryption root. Otherwise rewrap the entire dck and re-sync it
1460 VERIFY0(zap_update(dp
->dp_meta_objset
, dd
->dd_crypto_obj
,
1461 DSL_CRYPTO_KEY_ROOT_DDOBJ
, 8, 1, &new_rddobj
, tx
));
1463 VERIFY0(spa_keystore_dsl_key_hold_dd(dp
->dp_spa
, dd
,
1465 dsl_wrapping_key_hold(wkey
, dck
);
1466 dsl_wrapping_key_rele(dck
->dck_wkey
, dck
);
1467 dck
->dck_wkey
= wkey
;
1468 dsl_crypto_key_sync(dck
, tx
);
1469 spa_keystore_dsl_key_rele(dp
->dp_spa
, dck
, FTAG
);
1472 zc
= kmem_alloc(sizeof (zap_cursor_t
), KM_SLEEP
);
1473 za
= kmem_alloc(sizeof (zap_attribute_t
), KM_SLEEP
);
1475 /* Recurse into all child dsl dirs. */
1476 for (zap_cursor_init(zc
, dp
->dp_meta_objset
,
1477 dsl_dir_phys(dd
)->dd_child_dir_zapobj
);
1478 zap_cursor_retrieve(zc
, za
) == 0;
1479 zap_cursor_advance(zc
)) {
1480 spa_keystore_change_key_sync_impl(rddobj
,
1481 za
->za_first_integer
, new_rddobj
, wkey
, tx
);
1483 zap_cursor_fini(zc
);
1485 kmem_free(za
, sizeof (zap_attribute_t
));
1486 kmem_free(zc
, sizeof (zap_cursor_t
));
1488 dsl_dir_rele(dd
, FTAG
);
1492 spa_keystore_change_key_sync(void *arg
, dmu_tx_t
*tx
)
1496 dsl_pool_t
*dp
= dmu_tx_pool(tx
);
1497 spa_t
*spa
= dp
->dp_spa
;
1498 spa_keystore_change_key_args_t
*skcka
= arg
;
1499 dsl_crypto_params_t
*dcp
= skcka
->skcka_cp
;
1500 dsl_wrapping_key_t
*wkey
= NULL
, *found_wkey
;
1501 dsl_wrapping_key_t wkey_search
;
1502 char *keylocation
= dcp
->cp_keylocation
;
1503 uint64_t rddobj
, new_rddobj
;
1505 /* create and initialize the wrapping key */
1506 VERIFY0(dsl_dataset_hold(dp
, skcka
->skcka_dsname
, FTAG
, &ds
));
1507 ASSERT(!ds
->ds_is_snapshot
);
1509 if (dcp
->cp_cmd
== DCP_CMD_NEW_KEY
||
1510 dcp
->cp_cmd
== DCP_CMD_FORCE_NEW_KEY
) {
1512 * We are changing to a new wkey. Set additional properties
1513 * which can be sent along with this ioctl. Note that this
1514 * command can set keylocation even if it can't normally be
1515 * set via 'zfs set' due to a non-local keylocation.
1517 if (dcp
->cp_cmd
== DCP_CMD_NEW_KEY
) {
1518 wkey
= dcp
->cp_wkey
;
1519 wkey
->wk_ddobj
= ds
->ds_dir
->dd_object
;
1521 keylocation
= "prompt";
1524 if (keylocation
!= NULL
) {
1525 dsl_prop_set_sync_impl(ds
,
1526 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1527 ZPROP_SRC_LOCAL
, 1, strlen(keylocation
) + 1,
1531 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds
->ds_dir
, &rddobj
));
1532 new_rddobj
= ds
->ds_dir
->dd_object
;
1535 * We are inheritting the parent's wkey. Unset any local
1536 * keylocation and grab a reference to the wkey.
1538 if (dcp
->cp_cmd
== DCP_CMD_INHERIT
) {
1539 VERIFY0(spa_keystore_wkey_hold_dd(spa
,
1540 ds
->ds_dir
->dd_parent
, FTAG
, &wkey
));
1543 dsl_prop_set_sync_impl(ds
,
1544 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), ZPROP_SRC_NONE
,
1547 rddobj
= ds
->ds_dir
->dd_object
;
1548 VERIFY0(dsl_dir_get_encryption_root_ddobj(ds
->ds_dir
->dd_parent
,
1553 ASSERT(dcp
->cp_cmd
== DCP_CMD_FORCE_INHERIT
||
1554 dcp
->cp_cmd
== DCP_CMD_FORCE_NEW_KEY
);
1557 rw_enter(&spa
->spa_keystore
.sk_wkeys_lock
, RW_WRITER
);
1559 /* recurse through all children and rewrap their keys */
1560 spa_keystore_change_key_sync_impl(rddobj
, ds
->ds_dir
->dd_object
,
1561 new_rddobj
, wkey
, tx
);
1564 * All references to the old wkey should be released now (if it
1565 * existed). Replace the wrapping key.
1567 wkey_search
.wk_ddobj
= ds
->ds_dir
->dd_object
;
1568 found_wkey
= avl_find(&spa
->spa_keystore
.sk_wkeys
, &wkey_search
, NULL
);
1569 if (found_wkey
!= NULL
) {
1570 ASSERT0(zfs_refcount_count(&found_wkey
->wk_refcnt
));
1571 avl_remove(&spa
->spa_keystore
.sk_wkeys
, found_wkey
);
1572 dsl_wrapping_key_free(found_wkey
);
1575 if (dcp
->cp_cmd
== DCP_CMD_NEW_KEY
) {
1576 avl_find(&spa
->spa_keystore
.sk_wkeys
, wkey
, &where
);
1577 avl_insert(&spa
->spa_keystore
.sk_wkeys
, wkey
, where
);
1578 } else if (wkey
!= NULL
) {
1579 dsl_wrapping_key_rele(wkey
, FTAG
);
1582 rw_exit(&spa
->spa_keystore
.sk_wkeys_lock
);
1584 dsl_dataset_rele(ds
, FTAG
);
1588 spa_keystore_change_key(const char *dsname
, dsl_crypto_params_t
*dcp
)
1590 spa_keystore_change_key_args_t skcka
;
1592 /* initialize the args struct */
1593 skcka
.skcka_dsname
= dsname
;
1594 skcka
.skcka_cp
= dcp
;
1597 * Perform the actual work in syncing context. The blocks modified
1598 * here could be calculated but it would require holding the pool
1599 * lock and tarversing all of the datasets that will have their keys
1602 return (dsl_sync_task(dsname
, spa_keystore_change_key_check
,
1603 spa_keystore_change_key_sync
, &skcka
, 15,
1604 ZFS_SPACE_CHECK_RESERVED
));
1608 dsl_dir_rename_crypt_check(dsl_dir_t
*dd
, dsl_dir_t
*newparent
)
1611 uint64_t curr_rddobj
, parent_rddobj
;
1613 if (dd
->dd_crypto_obj
== 0) {
1614 /* children of encrypted parents must be encrypted */
1615 if (newparent
->dd_crypto_obj
!= 0) {
1616 ret
= SET_ERROR(EACCES
);
1623 ret
= dsl_dir_get_encryption_root_ddobj(dd
, &curr_rddobj
);
1628 * if this is not an encryption root, we must make sure we are not
1629 * moving dd to a new encryption root
1631 if (dd
->dd_object
!= curr_rddobj
) {
1632 ret
= dsl_dir_get_encryption_root_ddobj(newparent
,
1637 if (parent_rddobj
!= curr_rddobj
) {
1638 ret
= SET_ERROR(EACCES
);
1650 * Check to make sure that a promote from targetdd to origindd will not require
1654 dsl_dataset_promote_crypt_check(dsl_dir_t
*target
, dsl_dir_t
*origin
)
1657 uint64_t rddobj
, op_rddobj
, tp_rddobj
;
1659 /* If the dataset is not encrypted we don't need to check anything */
1660 if (origin
->dd_crypto_obj
== 0)
1664 * If we are not changing the first origin snapshot in a chain
1665 * the encryption root won't change either.
1667 if (dsl_dir_is_clone(origin
))
1671 * If the origin is the encryption root we will update
1672 * the DSL Crypto Key to point to the target instead.
1674 ret
= dsl_dir_get_encryption_root_ddobj(origin
, &rddobj
);
1678 if (rddobj
== origin
->dd_object
)
1682 * The origin is inheriting its encryption root from its parent.
1683 * Check that the parent of the target has the same encryption root.
1685 ret
= dsl_dir_get_encryption_root_ddobj(origin
->dd_parent
, &op_rddobj
);
1689 ret
= dsl_dir_get_encryption_root_ddobj(target
->dd_parent
, &tp_rddobj
);
1693 if (op_rddobj
!= tp_rddobj
)
1694 return (SET_ERROR(EACCES
));
1700 dsl_dataset_promote_crypt_sync(dsl_dir_t
*target
, dsl_dir_t
*origin
,
1704 dsl_pool_t
*dp
= target
->dd_pool
;
1705 dsl_dataset_t
*targetds
;
1706 dsl_dataset_t
*originds
;
1709 if (origin
->dd_crypto_obj
== 0)
1711 if (dsl_dir_is_clone(origin
))
1714 VERIFY0(dsl_dir_get_encryption_root_ddobj(origin
, &rddobj
));
1716 if (rddobj
!= origin
->dd_object
)
1720 * If the target is being promoted to the encyrption root update the
1721 * DSL Crypto Key and keylocation to reflect that. We also need to
1722 * update the DSL Crypto Keys of all children inheritting their
1723 * encryption root to point to the new target. Otherwise, the check
1724 * function ensured that the encryption root will not change.
1726 keylocation
= kmem_alloc(ZAP_MAXVALUELEN
, KM_SLEEP
);
1728 VERIFY0(dsl_dataset_hold_obj(dp
,
1729 dsl_dir_phys(target
)->dd_head_dataset_obj
, FTAG
, &targetds
));
1730 VERIFY0(dsl_dataset_hold_obj(dp
,
1731 dsl_dir_phys(origin
)->dd_head_dataset_obj
, FTAG
, &originds
));
1733 VERIFY0(dsl_prop_get_dd(origin
, zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1734 1, ZAP_MAXVALUELEN
, keylocation
, NULL
, B_FALSE
));
1735 dsl_prop_set_sync_impl(targetds
, zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1736 ZPROP_SRC_LOCAL
, 1, strlen(keylocation
) + 1, keylocation
, tx
);
1737 dsl_prop_set_sync_impl(originds
, zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
1738 ZPROP_SRC_NONE
, 0, 0, NULL
, tx
);
1740 rw_enter(&dp
->dp_spa
->spa_keystore
.sk_wkeys_lock
, RW_WRITER
);
1741 spa_keystore_change_key_sync_impl(rddobj
, origin
->dd_object
,
1742 target
->dd_object
, NULL
, tx
);
1743 rw_exit(&dp
->dp_spa
->spa_keystore
.sk_wkeys_lock
);
1745 dsl_dataset_rele(targetds
, FTAG
);
1746 dsl_dataset_rele(originds
, FTAG
);
1747 kmem_free(keylocation
, ZAP_MAXVALUELEN
);
1751 dmu_objset_clone_crypt_check(dsl_dir_t
*parentdd
, dsl_dir_t
*origindd
)
1754 uint64_t pcrypt
, crypt
;
1757 * Check that we are not making an unencrypted child of an
1760 ret
= dsl_dir_get_crypt(parentdd
, &pcrypt
);
1764 ret
= dsl_dir_get_crypt(origindd
, &crypt
);
1768 ASSERT3U(pcrypt
, !=, ZIO_CRYPT_INHERIT
);
1769 ASSERT3U(crypt
, !=, ZIO_CRYPT_INHERIT
);
1771 if (crypt
== ZIO_CRYPT_OFF
&& pcrypt
!= ZIO_CRYPT_OFF
)
1772 return (SET_ERROR(EINVAL
));
1779 dmu_objset_create_crypt_check(dsl_dir_t
*parentdd
, dsl_crypto_params_t
*dcp
,
1780 boolean_t
*will_encrypt
)
1783 uint64_t pcrypt
, crypt
;
1784 dsl_crypto_params_t dummy_dcp
= { 0 };
1786 if (will_encrypt
!= NULL
)
1787 *will_encrypt
= B_FALSE
;
1792 if (dcp
->cp_cmd
!= DCP_CMD_NONE
)
1793 return (SET_ERROR(EINVAL
));
1795 if (parentdd
!= NULL
) {
1796 ret
= dsl_dir_get_crypt(parentdd
, &pcrypt
);
1800 pcrypt
= ZIO_CRYPT_OFF
;
1803 crypt
= (dcp
->cp_crypt
== ZIO_CRYPT_INHERIT
) ? pcrypt
: dcp
->cp_crypt
;
1805 ASSERT3U(pcrypt
, !=, ZIO_CRYPT_INHERIT
);
1806 ASSERT3U(crypt
, !=, ZIO_CRYPT_INHERIT
);
1809 * We can't create an unencrypted child of an encrypted parent
1810 * under any circumstances.
1812 if (crypt
== ZIO_CRYPT_OFF
&& pcrypt
!= ZIO_CRYPT_OFF
)
1813 return (SET_ERROR(EINVAL
));
1815 /* check for valid dcp with no encryption (inherited or local) */
1816 if (crypt
== ZIO_CRYPT_OFF
) {
1817 /* Must not specify encryption params */
1818 if (dcp
->cp_wkey
!= NULL
||
1819 (dcp
->cp_keylocation
!= NULL
&&
1820 strcmp(dcp
->cp_keylocation
, "none") != 0))
1821 return (SET_ERROR(EINVAL
));
1826 if (will_encrypt
!= NULL
)
1827 *will_encrypt
= B_TRUE
;
1830 * We will now definitely be encrypting. Check the feature flag. When
1831 * creating the pool the caller will check this for us since we won't
1832 * technically have the feature activated yet.
1834 if (parentdd
!= NULL
&&
1835 !spa_feature_is_enabled(parentdd
->dd_pool
->dp_spa
,
1836 SPA_FEATURE_ENCRYPTION
)) {
1837 return (SET_ERROR(EOPNOTSUPP
));
1840 /* Check for errata #4 (encryption enabled, bookmark_v2 disabled) */
1841 if (parentdd
!= NULL
&&
1842 !spa_feature_is_enabled(parentdd
->dd_pool
->dp_spa
,
1843 SPA_FEATURE_BOOKMARK_V2
)) {
1844 return (SET_ERROR(EOPNOTSUPP
));
1847 /* handle inheritance */
1848 if (dcp
->cp_wkey
== NULL
) {
1849 ASSERT3P(parentdd
, !=, NULL
);
1851 /* key must be fully unspecified */
1852 if (dcp
->cp_keylocation
!= NULL
)
1853 return (SET_ERROR(EINVAL
));
1855 /* parent must have a key to inherit */
1856 if (pcrypt
== ZIO_CRYPT_OFF
)
1857 return (SET_ERROR(EINVAL
));
1859 /* check for parent key */
1860 ret
= dmu_objset_check_wkey_loaded(parentdd
);
1867 /* At this point we should have a fully specified key. Check location */
1868 if (dcp
->cp_keylocation
== NULL
||
1869 !zfs_prop_valid_keylocation(dcp
->cp_keylocation
, B_TRUE
))
1870 return (SET_ERROR(EINVAL
));
1872 /* Must have fully specified keyformat */
1873 switch (dcp
->cp_wkey
->wk_keyformat
) {
1874 case ZFS_KEYFORMAT_HEX
:
1875 case ZFS_KEYFORMAT_RAW
:
1876 /* requires no pbkdf2 iters and salt */
1877 if (dcp
->cp_wkey
->wk_salt
!= 0 || dcp
->cp_wkey
->wk_iters
!= 0)
1878 return (SET_ERROR(EINVAL
));
1880 case ZFS_KEYFORMAT_PASSPHRASE
:
1881 /* requires pbkdf2 iters and salt */
1882 if (dcp
->cp_wkey
->wk_salt
== 0 ||
1883 dcp
->cp_wkey
->wk_iters
< MIN_PBKDF2_ITERATIONS
)
1884 return (SET_ERROR(EINVAL
));
1886 case ZFS_KEYFORMAT_NONE
:
1888 /* keyformat must be specified and valid */
1889 return (SET_ERROR(EINVAL
));
1896 dsl_dataset_create_crypt_sync(uint64_t dsobj
, dsl_dir_t
*dd
,
1897 dsl_dataset_t
*origin
, dsl_crypto_params_t
*dcp
, dmu_tx_t
*tx
)
1899 dsl_pool_t
*dp
= dd
->dd_pool
;
1901 dsl_wrapping_key_t
*wkey
;
1903 /* clones always use their origin's wrapping key */
1904 if (dsl_dir_is_clone(dd
)) {
1905 ASSERT3P(dcp
, ==, NULL
);
1908 * If this is an encrypted clone we just need to clone the
1909 * dck into dd. Zapify the dd so we can do that.
1911 if (origin
->ds_dir
->dd_crypto_obj
!= 0) {
1912 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1913 dsl_dir_zapify(dd
, tx
);
1916 dsl_crypto_key_clone_sync(origin
->ds_dir
, tx
);
1917 VERIFY0(zap_add(dp
->dp_meta_objset
, dd
->dd_object
,
1918 DD_FIELD_CRYPTO_KEY_OBJ
, sizeof (uint64_t), 1,
1919 &dd
->dd_crypto_obj
, tx
));
1926 * A NULL dcp at this point indicates this is the origin dataset
1927 * which does not have an objset to encrypt. Raw receives will handle
1928 * encryption separately later. In both cases we can simply return.
1930 if (dcp
== NULL
|| dcp
->cp_cmd
== DCP_CMD_RAW_RECV
)
1933 crypt
= dcp
->cp_crypt
;
1934 wkey
= dcp
->cp_wkey
;
1936 /* figure out the effective crypt */
1937 if (crypt
== ZIO_CRYPT_INHERIT
&& dd
->dd_parent
!= NULL
)
1938 VERIFY0(dsl_dir_get_crypt(dd
->dd_parent
, &crypt
));
1940 /* if we aren't doing encryption just return */
1941 if (crypt
== ZIO_CRYPT_OFF
|| crypt
== ZIO_CRYPT_INHERIT
)
1944 /* zapify the dd so that we can add the crypto key obj to it */
1945 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
1946 dsl_dir_zapify(dd
, tx
);
1948 /* use the new key if given or inherit from the parent */
1950 VERIFY0(spa_keystore_wkey_hold_dd(dp
->dp_spa
,
1951 dd
->dd_parent
, FTAG
, &wkey
));
1953 wkey
->wk_ddobj
= dd
->dd_object
;
1956 ASSERT3P(wkey
, !=, NULL
);
1958 /* Create or clone the DSL crypto key and activate the feature */
1959 dd
->dd_crypto_obj
= dsl_crypto_key_create_sync(crypt
, wkey
, tx
);
1960 VERIFY0(zap_add(dp
->dp_meta_objset
, dd
->dd_object
,
1961 DD_FIELD_CRYPTO_KEY_OBJ
, sizeof (uint64_t), 1, &dd
->dd_crypto_obj
,
1963 dsl_dataset_activate_feature(dsobj
, SPA_FEATURE_ENCRYPTION
,
1964 (void *)B_TRUE
, tx
);
1967 * If we inherited the wrapping key we release our reference now.
1968 * Otherwise, this is a new key and we need to load it into the
1971 if (dcp
->cp_wkey
== NULL
) {
1972 dsl_wrapping_key_rele(wkey
, FTAG
);
1974 VERIFY0(spa_keystore_load_wkey_impl(dp
->dp_spa
, wkey
));
1978 typedef struct dsl_crypto_recv_key_arg
{
1979 uint64_t dcrka_dsobj
;
1980 uint64_t dcrka_fromobj
;
1981 dmu_objset_type_t dcrka_ostype
;
1982 nvlist_t
*dcrka_nvl
;
1983 boolean_t dcrka_do_key
;
1984 } dsl_crypto_recv_key_arg_t
;
1987 dsl_crypto_recv_raw_objset_check(dsl_dataset_t
*ds
, dsl_dataset_t
*fromds
,
1988 dmu_objset_type_t ostype
, nvlist_t
*nvl
, dmu_tx_t
*tx
)
1993 uint8_t *buf
= NULL
;
1995 uint64_t intval
, nlevels
, blksz
, ibs
;
1996 uint64_t nblkptr
, maxblkid
;
1998 if (ostype
!= DMU_OST_ZFS
&& ostype
!= DMU_OST_ZVOL
)
1999 return (SET_ERROR(EINVAL
));
2001 /* raw receives also need info about the structure of the metadnode */
2002 ret
= nvlist_lookup_uint64(nvl
, "mdn_compress", &intval
);
2003 if (ret
!= 0 || intval
>= ZIO_COMPRESS_LEGACY_FUNCTIONS
)
2004 return (SET_ERROR(EINVAL
));
2006 ret
= nvlist_lookup_uint64(nvl
, "mdn_checksum", &intval
);
2007 if (ret
!= 0 || intval
>= ZIO_CHECKSUM_LEGACY_FUNCTIONS
)
2008 return (SET_ERROR(EINVAL
));
2010 ret
= nvlist_lookup_uint64(nvl
, "mdn_nlevels", &nlevels
);
2011 if (ret
!= 0 || nlevels
> DN_MAX_LEVELS
)
2012 return (SET_ERROR(EINVAL
));
2014 ret
= nvlist_lookup_uint64(nvl
, "mdn_blksz", &blksz
);
2015 if (ret
!= 0 || blksz
< SPA_MINBLOCKSIZE
)
2016 return (SET_ERROR(EINVAL
));
2017 else if (blksz
> spa_maxblocksize(tx
->tx_pool
->dp_spa
))
2018 return (SET_ERROR(ENOTSUP
));
2020 ret
= nvlist_lookup_uint64(nvl
, "mdn_indblkshift", &ibs
);
2021 if (ret
!= 0 || ibs
< DN_MIN_INDBLKSHIFT
|| ibs
> DN_MAX_INDBLKSHIFT
)
2022 return (SET_ERROR(ENOTSUP
));
2024 ret
= nvlist_lookup_uint64(nvl
, "mdn_nblkptr", &nblkptr
);
2025 if (ret
!= 0 || nblkptr
!= DN_MAX_NBLKPTR
)
2026 return (SET_ERROR(ENOTSUP
));
2028 ret
= nvlist_lookup_uint64(nvl
, "mdn_maxblkid", &maxblkid
);
2030 return (SET_ERROR(EINVAL
));
2032 ret
= nvlist_lookup_uint8_array(nvl
, "portable_mac", &buf
, &len
);
2033 if (ret
!= 0 || len
!= ZIO_OBJSET_MAC_LEN
)
2034 return (SET_ERROR(EINVAL
));
2036 ret
= dmu_objset_from_ds(ds
, &os
);
2041 * Useraccounting is not portable and must be done with the keys loaded.
2042 * Therefore, whenever we do any kind of receive the useraccounting
2043 * must not be present.
2045 ASSERT0(os
->os_flags
& OBJSET_FLAG_USERACCOUNTING_COMPLETE
);
2046 ASSERT0(os
->os_flags
& OBJSET_FLAG_USEROBJACCOUNTING_COMPLETE
);
2048 mdn
= DMU_META_DNODE(os
);
2051 * If we already created the objset, make sure its unchangeable
2052 * properties match the ones received in the nvlist.
2054 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
2055 if (!BP_IS_HOLE(dsl_dataset_get_blkptr(ds
)) &&
2056 (mdn
->dn_nlevels
!= nlevels
|| mdn
->dn_datablksz
!= blksz
||
2057 mdn
->dn_indblkshift
!= ibs
|| mdn
->dn_nblkptr
!= nblkptr
)) {
2058 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
2059 return (SET_ERROR(EINVAL
));
2061 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
2064 * Check that the ivset guid of the fromds matches the one from the
2065 * send stream. Older versions of the encryption code did not have
2066 * an ivset guid on the from dataset and did not send one in the
2067 * stream. For these streams we provide the
2068 * zfs_disable_ivset_guid_check tunable to allow these datasets to
2069 * be received with a generated ivset guid.
2071 if (fromds
!= NULL
&& !zfs_disable_ivset_guid_check
) {
2072 uint64_t from_ivset_guid
= 0;
2075 (void) nvlist_lookup_uint64(nvl
, "from_ivset_guid", &intval
);
2076 (void) zap_lookup(tx
->tx_pool
->dp_meta_objset
,
2077 fromds
->ds_object
, DS_FIELD_IVSET_GUID
,
2078 sizeof (from_ivset_guid
), 1, &from_ivset_guid
);
2080 if (intval
== 0 || from_ivset_guid
== 0)
2081 return (SET_ERROR(ZFS_ERR_FROM_IVSET_GUID_MISSING
));
2083 if (intval
!= from_ivset_guid
)
2084 return (SET_ERROR(ZFS_ERR_FROM_IVSET_GUID_MISMATCH
));
2091 dsl_crypto_recv_raw_objset_sync(dsl_dataset_t
*ds
, dmu_objset_type_t ostype
,
2092 nvlist_t
*nvl
, dmu_tx_t
*tx
)
2094 dsl_pool_t
*dp
= tx
->tx_pool
;
2098 uint8_t *portable_mac
;
2100 uint64_t compress
, checksum
, nlevels
, blksz
, ibs
, maxblkid
;
2101 boolean_t newds
= B_FALSE
;
2103 VERIFY0(dmu_objset_from_ds(ds
, &os
));
2104 mdn
= DMU_META_DNODE(os
);
2107 * Fetch the values we need from the nvlist. "to_ivset_guid" must
2108 * be set on the snapshot, which doesn't exist yet. The receive
2109 * code will take care of this for us later.
2111 compress
= fnvlist_lookup_uint64(nvl
, "mdn_compress");
2112 checksum
= fnvlist_lookup_uint64(nvl
, "mdn_checksum");
2113 nlevels
= fnvlist_lookup_uint64(nvl
, "mdn_nlevels");
2114 blksz
= fnvlist_lookup_uint64(nvl
, "mdn_blksz");
2115 ibs
= fnvlist_lookup_uint64(nvl
, "mdn_indblkshift");
2116 maxblkid
= fnvlist_lookup_uint64(nvl
, "mdn_maxblkid");
2117 VERIFY0(nvlist_lookup_uint8_array(nvl
, "portable_mac", &portable_mac
,
2120 /* if we haven't created an objset for the ds yet, do that now */
2121 rrw_enter(&ds
->ds_bp_rwlock
, RW_READER
, FTAG
);
2122 if (BP_IS_HOLE(dsl_dataset_get_blkptr(ds
))) {
2123 (void) dmu_objset_create_impl_dnstats(dp
->dp_spa
, ds
,
2124 dsl_dataset_get_blkptr(ds
), ostype
, nlevels
, blksz
,
2128 rrw_exit(&ds
->ds_bp_rwlock
, FTAG
);
2131 * Set the portable MAC. The local MAC will always be zero since the
2132 * incoming data will all be portable and user accounting will be
2133 * deferred until the next mount. Afterwards, flag the os to be
2134 * written out raw next time.
2136 arc_release(os
->os_phys_buf
, &os
->os_phys_buf
);
2137 bcopy(portable_mac
, os
->os_phys
->os_portable_mac
, ZIO_OBJSET_MAC_LEN
);
2138 bzero(os
->os_phys
->os_local_mac
, ZIO_OBJSET_MAC_LEN
);
2139 os
->os_next_write_raw
[tx
->tx_txg
& TXG_MASK
] = B_TRUE
;
2141 /* set metadnode compression and checksum */
2142 mdn
->dn_compress
= compress
;
2143 mdn
->dn_checksum
= checksum
;
2145 rw_enter(&mdn
->dn_struct_rwlock
, RW_WRITER
);
2146 dnode_new_blkid(mdn
, maxblkid
, tx
, B_FALSE
, B_TRUE
);
2147 rw_exit(&mdn
->dn_struct_rwlock
);
2150 * We can't normally dirty the dataset in syncing context unless
2151 * we are creating a new dataset. In this case, we perform a
2152 * pseudo txg sync here instead.
2155 dsl_dataset_dirty(ds
, tx
);
2157 zio
= zio_root(dp
->dp_spa
, NULL
, NULL
, ZIO_FLAG_MUSTSUCCEED
);
2158 dsl_dataset_sync(ds
, zio
, tx
);
2159 VERIFY0(zio_wait(zio
));
2161 /* dsl_dataset_sync_done will drop this reference. */
2162 dmu_buf_add_ref(ds
->ds_dbuf
, ds
);
2163 dsl_dataset_sync_done(ds
, tx
);
2168 dsl_crypto_recv_raw_key_check(dsl_dataset_t
*ds
, nvlist_t
*nvl
, dmu_tx_t
*tx
)
2171 objset_t
*mos
= tx
->tx_pool
->dp_meta_objset
;
2172 uint8_t *buf
= NULL
;
2174 uint64_t intval
, key_guid
, version
;
2175 boolean_t is_passphrase
= B_FALSE
;
2177 ASSERT(dsl_dataset_phys(ds
)->ds_flags
& DS_FLAG_INCONSISTENT
);
2180 * Read and check all the encryption values from the nvlist. We need
2181 * all of the fields of a DSL Crypto Key, as well as a fully specified
2184 ret
= nvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, &intval
);
2185 if (ret
!= 0 || intval
>= ZIO_CRYPT_FUNCTIONS
||
2186 intval
<= ZIO_CRYPT_OFF
)
2187 return (SET_ERROR(EINVAL
));
2189 ret
= nvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_GUID
, &intval
);
2191 return (SET_ERROR(EINVAL
));
2194 * If this is an incremental receive make sure the given key guid
2195 * matches the one we already have.
2197 if (ds
->ds_dir
->dd_crypto_obj
!= 0) {
2198 ret
= zap_lookup(mos
, ds
->ds_dir
->dd_crypto_obj
,
2199 DSL_CRYPTO_KEY_GUID
, 8, 1, &key_guid
);
2202 if (intval
!= key_guid
)
2203 return (SET_ERROR(EACCES
));
2206 ret
= nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_MASTER_KEY
,
2208 if (ret
!= 0 || len
!= MASTER_KEY_MAX_LEN
)
2209 return (SET_ERROR(EINVAL
));
2211 ret
= nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_HMAC_KEY
,
2213 if (ret
!= 0 || len
!= SHA512_HMAC_KEYLEN
)
2214 return (SET_ERROR(EINVAL
));
2216 ret
= nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_IV
, &buf
, &len
);
2217 if (ret
!= 0 || len
!= WRAPPING_IV_LEN
)
2218 return (SET_ERROR(EINVAL
));
2220 ret
= nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_MAC
, &buf
, &len
);
2221 if (ret
!= 0 || len
!= WRAPPING_MAC_LEN
)
2222 return (SET_ERROR(EINVAL
));
2225 * We don't support receiving old on-disk formats. The version 0
2226 * implementation protected several fields in an objset that were
2227 * not always portable during a raw receive. As a result, we call
2228 * the old version an on-disk errata #3.
2230 ret
= nvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_VERSION
, &version
);
2231 if (ret
!= 0 || version
!= ZIO_CRYPT_KEY_CURRENT_VERSION
)
2232 return (SET_ERROR(ENOTSUP
));
2234 ret
= nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
),
2236 if (ret
!= 0 || intval
>= ZFS_KEYFORMAT_FORMATS
||
2237 intval
== ZFS_KEYFORMAT_NONE
)
2238 return (SET_ERROR(EINVAL
));
2240 is_passphrase
= (intval
== ZFS_KEYFORMAT_PASSPHRASE
);
2243 * for raw receives we allow any number of pbkdf2iters since there
2244 * won't be a chance for the user to change it.
2246 ret
= nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
),
2248 if (ret
!= 0 || (is_passphrase
== (intval
== 0)))
2249 return (SET_ERROR(EINVAL
));
2251 ret
= nvlist_lookup_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
),
2253 if (ret
!= 0 || (is_passphrase
== (intval
== 0)))
2254 return (SET_ERROR(EINVAL
));
2260 dsl_crypto_recv_raw_key_sync(dsl_dataset_t
*ds
, nvlist_t
*nvl
, dmu_tx_t
*tx
)
2262 dsl_pool_t
*dp
= tx
->tx_pool
;
2263 objset_t
*mos
= dp
->dp_meta_objset
;
2264 dsl_dir_t
*dd
= ds
->ds_dir
;
2266 uint64_t rddobj
, one
= 1;
2267 uint8_t *keydata
, *hmac_keydata
, *iv
, *mac
;
2268 uint64_t crypt
, key_guid
, keyformat
, iters
, salt
;
2269 uint64_t version
= ZIO_CRYPT_KEY_CURRENT_VERSION
;
2270 char *keylocation
= "prompt";
2272 /* lookup the values we need to create the DSL Crypto Key */
2273 crypt
= fnvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_CRYPTO_SUITE
);
2274 key_guid
= fnvlist_lookup_uint64(nvl
, DSL_CRYPTO_KEY_GUID
);
2275 keyformat
= fnvlist_lookup_uint64(nvl
,
2276 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
));
2277 iters
= fnvlist_lookup_uint64(nvl
,
2278 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
));
2279 salt
= fnvlist_lookup_uint64(nvl
,
2280 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
));
2281 VERIFY0(nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_MASTER_KEY
,
2283 VERIFY0(nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_HMAC_KEY
,
2284 &hmac_keydata
, &len
));
2285 VERIFY0(nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_IV
, &iv
, &len
));
2286 VERIFY0(nvlist_lookup_uint8_array(nvl
, DSL_CRYPTO_KEY_MAC
, &mac
, &len
));
2288 /* if this is a new dataset setup the DSL Crypto Key. */
2289 if (dd
->dd_crypto_obj
== 0) {
2290 /* zapify the dsl dir so we can add the key object to it */
2291 dmu_buf_will_dirty(dd
->dd_dbuf
, tx
);
2292 dsl_dir_zapify(dd
, tx
);
2294 /* create the DSL Crypto Key on disk and activate the feature */
2295 dd
->dd_crypto_obj
= zap_create(mos
,
2296 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
2297 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
,
2298 dd
->dd_crypto_obj
, DSL_CRYPTO_KEY_REFCOUNT
,
2299 sizeof (uint64_t), 1, &one
, tx
));
2300 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
,
2301 dd
->dd_crypto_obj
, DSL_CRYPTO_KEY_VERSION
,
2302 sizeof (uint64_t), 1, &version
, tx
));
2304 dsl_dataset_activate_feature(ds
->ds_object
,
2305 SPA_FEATURE_ENCRYPTION
, (void *)B_TRUE
, tx
);
2306 ds
->ds_feature
[SPA_FEATURE_ENCRYPTION
] = (void *)B_TRUE
;
2308 /* save the dd_crypto_obj on disk */
2309 VERIFY0(zap_add(mos
, dd
->dd_object
, DD_FIELD_CRYPTO_KEY_OBJ
,
2310 sizeof (uint64_t), 1, &dd
->dd_crypto_obj
, tx
));
2313 * Set the keylocation to prompt by default. If keylocation
2314 * has been provided via the properties, this will be overridden
2317 dsl_prop_set_sync_impl(ds
,
2318 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
2319 ZPROP_SRC_LOCAL
, 1, strlen(keylocation
) + 1,
2322 rddobj
= dd
->dd_object
;
2324 VERIFY0(dsl_dir_get_encryption_root_ddobj(dd
, &rddobj
));
2327 /* sync the key data to the ZAP object on disk */
2328 dsl_crypto_key_sync_impl(mos
, dd
->dd_crypto_obj
, crypt
,
2329 rddobj
, key_guid
, iv
, mac
, keydata
, hmac_keydata
, keyformat
, salt
,
2334 dsl_crypto_recv_key_check(void *arg
, dmu_tx_t
*tx
)
2337 dsl_crypto_recv_key_arg_t
*dcrka
= arg
;
2338 dsl_dataset_t
*ds
= NULL
, *fromds
= NULL
;
2340 ret
= dsl_dataset_hold_obj(tx
->tx_pool
, dcrka
->dcrka_dsobj
,
2345 if (dcrka
->dcrka_fromobj
!= 0) {
2346 ret
= dsl_dataset_hold_obj(tx
->tx_pool
, dcrka
->dcrka_fromobj
,
2352 ret
= dsl_crypto_recv_raw_objset_check(ds
, fromds
,
2353 dcrka
->dcrka_ostype
, dcrka
->dcrka_nvl
, tx
);
2358 * We run this check even if we won't be doing this part of
2359 * the receive now so that we don't make the user wait until
2360 * the receive finishes to fail.
2362 ret
= dsl_crypto_recv_raw_key_check(ds
, dcrka
->dcrka_nvl
, tx
);
2368 dsl_dataset_rele(ds
, FTAG
);
2370 dsl_dataset_rele(fromds
, FTAG
);
2375 dsl_crypto_recv_key_sync(void *arg
, dmu_tx_t
*tx
)
2377 dsl_crypto_recv_key_arg_t
*dcrka
= arg
;
2380 VERIFY0(dsl_dataset_hold_obj(tx
->tx_pool
, dcrka
->dcrka_dsobj
,
2382 dsl_crypto_recv_raw_objset_sync(ds
, dcrka
->dcrka_ostype
,
2383 dcrka
->dcrka_nvl
, tx
);
2384 if (dcrka
->dcrka_do_key
)
2385 dsl_crypto_recv_raw_key_sync(ds
, dcrka
->dcrka_nvl
, tx
);
2386 dsl_dataset_rele(ds
, FTAG
);
2390 * This function is used to sync an nvlist representing a DSL Crypto Key and
2391 * the associated encryption parameters. The key will be written exactly as is
2392 * without wrapping it.
2395 dsl_crypto_recv_raw(const char *poolname
, uint64_t dsobj
, uint64_t fromobj
,
2396 dmu_objset_type_t ostype
, nvlist_t
*nvl
, boolean_t do_key
)
2398 dsl_crypto_recv_key_arg_t dcrka
;
2400 dcrka
.dcrka_dsobj
= dsobj
;
2401 dcrka
.dcrka_fromobj
= fromobj
;
2402 dcrka
.dcrka_ostype
= ostype
;
2403 dcrka
.dcrka_nvl
= nvl
;
2404 dcrka
.dcrka_do_key
= do_key
;
2406 return (dsl_sync_task(poolname
, dsl_crypto_recv_key_check
,
2407 dsl_crypto_recv_key_sync
, &dcrka
, 1, ZFS_SPACE_CHECK_NORMAL
));
2411 dsl_crypto_populate_key_nvlist(dsl_dataset_t
*ds
, uint64_t from_ivset_guid
,
2418 nvlist_t
*nvl
= NULL
;
2419 uint64_t dckobj
= ds
->ds_dir
->dd_crypto_obj
;
2420 dsl_dir_t
*rdd
= NULL
;
2421 dsl_pool_t
*dp
= ds
->ds_dir
->dd_pool
;
2422 objset_t
*mos
= dp
->dp_meta_objset
;
2423 uint64_t crypt
= 0, key_guid
= 0, format
= 0;
2424 uint64_t iters
= 0, salt
= 0, version
= 0;
2425 uint64_t to_ivset_guid
= 0;
2426 uint8_t raw_keydata
[MASTER_KEY_MAX_LEN
];
2427 uint8_t raw_hmac_keydata
[SHA512_HMAC_KEYLEN
];
2428 uint8_t iv
[WRAPPING_IV_LEN
];
2429 uint8_t mac
[WRAPPING_MAC_LEN
];
2431 ASSERT(dckobj
!= 0);
2433 VERIFY0(dmu_objset_from_ds(ds
, &os
));
2434 mdn
= DMU_META_DNODE(os
);
2436 ret
= nvlist_alloc(&nvl
, NV_UNIQUE_NAME
, KM_SLEEP
);
2440 /* lookup values from the DSL Crypto Key */
2441 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, 8, 1,
2446 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_GUID
, 8, 1, &key_guid
);
2450 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_MASTER_KEY
, 1,
2451 MASTER_KEY_MAX_LEN
, raw_keydata
);
2455 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_HMAC_KEY
, 1,
2456 SHA512_HMAC_KEYLEN
, raw_hmac_keydata
);
2460 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_IV
, 1, WRAPPING_IV_LEN
,
2465 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_MAC
, 1, WRAPPING_MAC_LEN
,
2470 /* see zfs_disable_ivset_guid_check tunable for errata info */
2471 ret
= zap_lookup(mos
, ds
->ds_object
, DS_FIELD_IVSET_GUID
, 8, 1,
2474 ASSERT3U(dp
->dp_spa
->spa_errata
, !=, 0);
2477 * We don't support raw sends of legacy on-disk formats. See the
2478 * comment in dsl_crypto_recv_key_check() for details.
2480 ret
= zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_VERSION
, 8, 1, &version
);
2481 if (ret
!= 0 || version
!= ZIO_CRYPT_KEY_CURRENT_VERSION
) {
2482 dp
->dp_spa
->spa_errata
= ZPOOL_ERRATA_ZOL_6845_ENCRYPTION
;
2483 ret
= SET_ERROR(ENOTSUP
);
2488 * Lookup wrapping key properties. An early version of the code did
2489 * not correctly add these values to the wrapping key or the DSL
2490 * Crypto Key on disk for non encryption roots, so to be safe we
2491 * always take the slightly circuitous route of looking it up from
2492 * the encryption root's key.
2494 ret
= dsl_dir_get_encryption_root_ddobj(ds
->ds_dir
, &rddobj
);
2498 dsl_pool_config_enter(dp
, FTAG
);
2500 ret
= dsl_dir_hold_obj(dp
, rddobj
, NULL
, FTAG
, &rdd
);
2504 ret
= zap_lookup(dp
->dp_meta_objset
, rdd
->dd_crypto_obj
,
2505 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), 8, 1, &format
);
2509 if (format
== ZFS_KEYFORMAT_PASSPHRASE
) {
2510 ret
= zap_lookup(dp
->dp_meta_objset
, rdd
->dd_crypto_obj
,
2511 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 8, 1, &iters
);
2515 ret
= zap_lookup(dp
->dp_meta_objset
, rdd
->dd_crypto_obj
,
2516 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 8, 1, &salt
);
2521 dsl_dir_rele(rdd
, FTAG
);
2522 dsl_pool_config_exit(dp
, FTAG
);
2524 fnvlist_add_uint64(nvl
, DSL_CRYPTO_KEY_CRYPTO_SUITE
, crypt
);
2525 fnvlist_add_uint64(nvl
, DSL_CRYPTO_KEY_GUID
, key_guid
);
2526 fnvlist_add_uint64(nvl
, DSL_CRYPTO_KEY_VERSION
, version
);
2527 VERIFY0(nvlist_add_uint8_array(nvl
, DSL_CRYPTO_KEY_MASTER_KEY
,
2528 raw_keydata
, MASTER_KEY_MAX_LEN
));
2529 VERIFY0(nvlist_add_uint8_array(nvl
, DSL_CRYPTO_KEY_HMAC_KEY
,
2530 raw_hmac_keydata
, SHA512_HMAC_KEYLEN
));
2531 VERIFY0(nvlist_add_uint8_array(nvl
, DSL_CRYPTO_KEY_IV
, iv
,
2533 VERIFY0(nvlist_add_uint8_array(nvl
, DSL_CRYPTO_KEY_MAC
, mac
,
2535 VERIFY0(nvlist_add_uint8_array(nvl
, "portable_mac",
2536 os
->os_phys
->os_portable_mac
, ZIO_OBJSET_MAC_LEN
));
2537 fnvlist_add_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), format
);
2538 fnvlist_add_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), iters
);
2539 fnvlist_add_uint64(nvl
, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), salt
);
2540 fnvlist_add_uint64(nvl
, "mdn_checksum", mdn
->dn_checksum
);
2541 fnvlist_add_uint64(nvl
, "mdn_compress", mdn
->dn_compress
);
2542 fnvlist_add_uint64(nvl
, "mdn_nlevels", mdn
->dn_nlevels
);
2543 fnvlist_add_uint64(nvl
, "mdn_blksz", mdn
->dn_datablksz
);
2544 fnvlist_add_uint64(nvl
, "mdn_indblkshift", mdn
->dn_indblkshift
);
2545 fnvlist_add_uint64(nvl
, "mdn_nblkptr", mdn
->dn_nblkptr
);
2546 fnvlist_add_uint64(nvl
, "mdn_maxblkid", mdn
->dn_maxblkid
);
2547 fnvlist_add_uint64(nvl
, "to_ivset_guid", to_ivset_guid
);
2548 fnvlist_add_uint64(nvl
, "from_ivset_guid", from_ivset_guid
);
2554 dsl_pool_config_exit(dp
, FTAG
);
2557 dsl_dir_rele(rdd
, FTAG
);
2565 dsl_crypto_key_create_sync(uint64_t crypt
, dsl_wrapping_key_t
*wkey
,
2568 dsl_crypto_key_t dck
;
2569 uint64_t version
= ZIO_CRYPT_KEY_CURRENT_VERSION
;
2570 uint64_t one
= 1ULL;
2572 ASSERT(dmu_tx_is_syncing(tx
));
2573 ASSERT3U(crypt
, <, ZIO_CRYPT_FUNCTIONS
);
2574 ASSERT3U(crypt
, >, ZIO_CRYPT_OFF
);
2576 /* create the DSL Crypto Key ZAP object */
2577 dck
.dck_obj
= zap_create(tx
->tx_pool
->dp_meta_objset
,
2578 DMU_OTN_ZAP_METADATA
, DMU_OT_NONE
, 0, tx
);
2580 /* fill in the key (on the stack) and sync it to disk */
2581 dck
.dck_wkey
= wkey
;
2582 VERIFY0(zio_crypt_key_init(crypt
, &dck
.dck_key
));
2584 dsl_crypto_key_sync(&dck
, tx
);
2585 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
, dck
.dck_obj
,
2586 DSL_CRYPTO_KEY_REFCOUNT
, sizeof (uint64_t), 1, &one
, tx
));
2587 VERIFY0(zap_update(tx
->tx_pool
->dp_meta_objset
, dck
.dck_obj
,
2588 DSL_CRYPTO_KEY_VERSION
, sizeof (uint64_t), 1, &version
, tx
));
2590 zio_crypt_key_destroy(&dck
.dck_key
);
2591 bzero(&dck
.dck_key
, sizeof (zio_crypt_key_t
));
2593 return (dck
.dck_obj
);
2597 dsl_crypto_key_clone_sync(dsl_dir_t
*origindd
, dmu_tx_t
*tx
)
2599 objset_t
*mos
= tx
->tx_pool
->dp_meta_objset
;
2601 ASSERT(dmu_tx_is_syncing(tx
));
2603 VERIFY0(zap_increment(mos
, origindd
->dd_crypto_obj
,
2604 DSL_CRYPTO_KEY_REFCOUNT
, 1, tx
));
2606 return (origindd
->dd_crypto_obj
);
2610 dsl_crypto_key_destroy_sync(uint64_t dckobj
, dmu_tx_t
*tx
)
2612 objset_t
*mos
= tx
->tx_pool
->dp_meta_objset
;
2615 /* Decrement the refcount, destroy if this is the last reference */
2616 VERIFY0(zap_lookup(mos
, dckobj
, DSL_CRYPTO_KEY_REFCOUNT
,
2617 sizeof (uint64_t), 1, &refcnt
));
2620 VERIFY0(zap_increment(mos
, dckobj
, DSL_CRYPTO_KEY_REFCOUNT
,
2623 VERIFY0(zap_destroy(mos
, dckobj
, tx
));
2628 dsl_dataset_crypt_stats(dsl_dataset_t
*ds
, nvlist_t
*nv
)
2631 dsl_dir_t
*dd
= ds
->ds_dir
;
2632 dsl_dir_t
*enc_root
;
2633 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2635 if (dd
->dd_crypto_obj
== 0)
2638 intval
= dsl_dataset_get_keystatus(dd
);
2639 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_KEYSTATUS
, intval
);
2641 if (dsl_dir_get_crypt(dd
, &intval
) == 0)
2642 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_ENCRYPTION
, intval
);
2643 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
2644 DSL_CRYPTO_KEY_GUID
, 8, 1, &intval
) == 0) {
2645 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_KEY_GUID
, intval
);
2647 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
2648 zfs_prop_to_name(ZFS_PROP_KEYFORMAT
), 8, 1, &intval
) == 0) {
2649 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_KEYFORMAT
, intval
);
2651 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
2652 zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT
), 8, 1, &intval
) == 0) {
2653 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_PBKDF2_SALT
, intval
);
2655 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, dd
->dd_crypto_obj
,
2656 zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS
), 8, 1, &intval
) == 0) {
2657 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_PBKDF2_ITERS
, intval
);
2659 if (zap_lookup(dd
->dd_pool
->dp_meta_objset
, ds
->ds_object
,
2660 DS_FIELD_IVSET_GUID
, 8, 1, &intval
) == 0) {
2661 dsl_prop_nvlist_add_uint64(nv
, ZFS_PROP_IVSET_GUID
, intval
);
2664 if (dsl_dir_get_encryption_root_ddobj(dd
, &intval
) == 0) {
2665 VERIFY0(dsl_dir_hold_obj(dd
->dd_pool
, intval
, NULL
, FTAG
,
2667 dsl_dir_name(enc_root
, buf
);
2668 dsl_dir_rele(enc_root
, FTAG
);
2669 dsl_prop_nvlist_add_string(nv
, ZFS_PROP_ENCRYPTION_ROOT
, buf
);
2674 spa_crypt_get_salt(spa_t
*spa
, uint64_t dsobj
, uint8_t *salt
)
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
);
2684 ret
= zio_crypt_key_get_salt(&dck
->dck_key
, salt
);
2688 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2693 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
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
)
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
);
2719 /* calculate both HMACs */
2720 ret
= zio_crypt_do_objset_hmacs(&dck
->dck_key
, buf
, datalen
,
2721 byteswap
, portable_mac
, local_mac
);
2725 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2727 /* if we are generating encode the HMACs in the objset_phys_t */
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
);
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
);
2747 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2748 abd_return_buf(abd
, buf
, datalen
);
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
)
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
);
2766 /* perform the hmac */
2767 ret
= zio_crypt_do_hmac(&dck
->dck_key
, buf
, datalen
,
2768 digestbuf
, ZIO_DATA_MAC_LEN
);
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.
2780 bcopy(digestbuf
, mac
, ZIO_DATA_MAC_LEN
);
2784 if (bcmp(digestbuf
, mac
, ZIO_DATA_MAC_LEN
) != 0)
2785 return (SET_ERROR(ECKSUM
));
2791 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
2792 abd_return_buf(abd
, buf
, datalen
);
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
)
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
);
2817 ret
= SET_ERROR(EACCES
);
2822 plainbuf
= abd_borrow_buf_copy(pabd
, datalen
);
2823 cipherbuf
= abd_borrow_buf(cabd
, datalen
);
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
);
2843 ret
= zio_crypt_generate_iv(iv
);
2846 } else if (encrypt
&& dedup
) {
2847 ret
= zio_crypt_generate_iv_salt_dedup(&dck
->dck_key
,
2848 plainbuf
, datalen
, iv
, salt
);
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
);
2869 abd_return_buf(pabd
, plainbuf
, datalen
);
2870 abd_return_buf_copy(cabd
, cipherbuf
, datalen
);
2872 abd_return_buf_copy(pabd
, plainbuf
, datalen
);
2873 abd_return_buf(cabd
, cipherbuf
, datalen
);
2876 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
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
);
2889 abd_return_buf_copy(pabd
, plainbuf
, datalen
);
2890 abd_return_buf(cabd
, cipherbuf
, datalen
);
2893 spa_keystore_dsl_key_rele(spa
, dck
, FTAG
);
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");