Path: blob/main/sys/contrib/openzfs/module/zfs/dsl_crypt.c
48383 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* This file and its contents are supplied under the terms of the5* Common Development and Distribution License ("CDDL"), version 1.0.6* You may only use this file in accordance with the terms of version7* 1.0 of the CDDL.8*9* A full copy of the text of the CDDL should have accompanied this10* source. A copy of the CDDL is also available via the Internet at11* http://www.illumos.org/license/CDDL.12*13* CDDL HEADER END14*/1516/*17* Copyright (c) 2017, Datto, Inc. All rights reserved.18* Copyright (c) 2018 by Delphix. All rights reserved.19*/2021#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>3031/*32* This file's primary purpose is for managing master encryption keys in33* memory and on disk. For more info on how these keys are used, see the34* block comment in zio_crypt.c.35*36* All master keys are stored encrypted on disk in the form of the DSL37* Crypto Key ZAP object. The binary key data in this object is always38* randomly generated and is encrypted with the user's wrapping key. This39* layer of indirection allows the user to change their key without40* needing to re-encrypt the entire dataset. The ZAP also holds on to the41* (non-encrypted) encryption algorithm identifier, IV, and MAC needed to42* safely decrypt the master key. For more info on the user's key see the43* block comment in libzfs_crypto.c44*45* In-memory encryption keys are managed through the spa_keystore. The46* keystore consists of 3 AVL trees, which are as follows:47*48* The Wrapping Key Tree:49* The wrapping key (wkey) tree stores the user's keys that are fed into the50* kernel through 'zfs load-key' and related commands. Datasets inherit their51* parent's wkey by default, so these structures are refcounted. The wrapping52* keys remain in memory until they are explicitly unloaded (with53* "zfs unload-key"). Unloading is only possible when no datasets are using54* them (refcount=0).55*56* The DSL Crypto Key Tree:57* The DSL Crypto Keys (DCK) are the in-memory representation of decrypted58* master keys. They are used by the functions in zio_crypt.c to perform59* encryption, decryption, and authentication. Snapshots and clones of a given60* dataset will share a DSL Crypto Key, so they are also refcounted. Once the61* refcount on a key hits zero, it is immediately zeroed out and freed.62*63* The Crypto Key Mapping Tree:64* The zio layer needs to lookup master keys by their dataset object id. Since65* the DSL Crypto Keys can belong to multiple datasets, we maintain a tree of66* dsl_key_mapping_t's which essentially just map the dataset object id to its67* appropriate DSL Crypto Key. The management for creating and destroying these68* mappings hooks into the code for owning and disowning datasets. Usually,69* there will only be one active dataset owner, but there are times70* (particularly during dataset creation and destruction) when this may not be71* true or the dataset may not be initialized enough to own. As a result, this72* object is also refcounted.73*/7475/*76* This tunable allows datasets to be raw received even if the stream does77* not include IVset guids or if the guids don't match. This is used as part78* of the resolution for ZPOOL_ERRATA_ZOL_8308_ENCRYPTION.79*/80int zfs_disable_ivset_guid_check = 0;8182static void83dsl_wrapping_key_hold(dsl_wrapping_key_t *wkey, const void *tag)84{85(void) zfs_refcount_add(&wkey->wk_refcnt, tag);86}8788static void89dsl_wrapping_key_rele(dsl_wrapping_key_t *wkey, const void *tag)90{91(void) zfs_refcount_remove(&wkey->wk_refcnt, tag);92}9394static void95dsl_wrapping_key_free(dsl_wrapping_key_t *wkey)96{97ASSERT0(zfs_refcount_count(&wkey->wk_refcnt));9899if (wkey->wk_key.ck_data) {100memset(wkey->wk_key.ck_data, 0,101CRYPTO_BITS2BYTES(wkey->wk_key.ck_length));102kmem_free(wkey->wk_key.ck_data,103CRYPTO_BITS2BYTES(wkey->wk_key.ck_length));104}105106zfs_refcount_destroy(&wkey->wk_refcnt);107kmem_free(wkey, sizeof (dsl_wrapping_key_t));108}109110static void111dsl_wrapping_key_create(uint8_t *wkeydata, zfs_keyformat_t keyformat,112uint64_t salt, uint64_t iters, dsl_wrapping_key_t **wkey_out)113{114dsl_wrapping_key_t *wkey;115116/* allocate the wrapping key */117wkey = kmem_alloc(sizeof (dsl_wrapping_key_t), KM_SLEEP);118119/* allocate and initialize the underlying crypto key */120wkey->wk_key.ck_data = kmem_alloc(WRAPPING_KEY_LEN, KM_SLEEP);121122wkey->wk_key.ck_length = CRYPTO_BYTES2BITS(WRAPPING_KEY_LEN);123memcpy(wkey->wk_key.ck_data, wkeydata, WRAPPING_KEY_LEN);124125/* initialize the rest of the struct */126zfs_refcount_create(&wkey->wk_refcnt);127wkey->wk_keyformat = keyformat;128wkey->wk_salt = salt;129wkey->wk_iters = iters;130131*wkey_out = wkey;132}133134int135dsl_crypto_params_create_nvlist(dcp_cmd_t cmd, nvlist_t *props,136nvlist_t *crypto_args, dsl_crypto_params_t **dcp_out)137{138int ret;139uint64_t crypt = ZIO_CRYPT_INHERIT;140uint64_t keyformat = ZFS_KEYFORMAT_NONE;141uint64_t salt = 0, iters = 0;142dsl_crypto_params_t *dcp = NULL;143dsl_wrapping_key_t *wkey = NULL;144uint8_t *wkeydata = NULL;145uint_t wkeydata_len = 0;146const char *keylocation = NULL;147148dcp = kmem_zalloc(sizeof (dsl_crypto_params_t), KM_SLEEP);149dcp->cp_cmd = cmd;150151/* get relevant arguments from the nvlists */152if (props != NULL) {153(void) nvlist_lookup_uint64(props,154zfs_prop_to_name(ZFS_PROP_ENCRYPTION), &crypt);155(void) nvlist_lookup_uint64(props,156zfs_prop_to_name(ZFS_PROP_KEYFORMAT), &keyformat);157(void) nvlist_lookup_string(props,158zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);159(void) nvlist_lookup_uint64(props,160zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), &salt);161(void) nvlist_lookup_uint64(props,162zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), &iters);163164dcp->cp_crypt = crypt;165}166167if (crypto_args != NULL) {168(void) nvlist_lookup_uint8_array(crypto_args, "wkeydata",169&wkeydata, &wkeydata_len);170}171172/* check for valid command */173if (dcp->cp_cmd >= DCP_CMD_MAX) {174ret = SET_ERROR(EINVAL);175goto error;176} else {177dcp->cp_cmd = cmd;178}179180/* check for valid crypt */181if (dcp->cp_crypt >= ZIO_CRYPT_FUNCTIONS) {182ret = SET_ERROR(EINVAL);183goto error;184} else {185dcp->cp_crypt = crypt;186}187188/* check for valid keyformat */189if (keyformat >= ZFS_KEYFORMAT_FORMATS) {190ret = SET_ERROR(EINVAL);191goto error;192}193194/* check for a valid keylocation (of any kind) and copy it in */195if (keylocation != NULL) {196if (!zfs_prop_valid_keylocation(keylocation, B_FALSE)) {197ret = SET_ERROR(EINVAL);198goto error;199}200201dcp->cp_keylocation = spa_strdup(keylocation);202}203204/* check wrapping key length, if given */205if (wkeydata != NULL && wkeydata_len != WRAPPING_KEY_LEN) {206ret = SET_ERROR(EINVAL);207goto error;208}209210/* if the user asked for the default crypt, determine that now */211if (dcp->cp_crypt == ZIO_CRYPT_ON)212dcp->cp_crypt = ZIO_CRYPT_ON_VALUE;213214/* create the wrapping key from the raw data */215if (wkeydata != NULL) {216/* create the wrapping key with the verified parameters */217dsl_wrapping_key_create(wkeydata, keyformat, salt,218iters, &wkey);219dcp->cp_wkey = wkey;220}221222/*223* Remove the encryption properties from the nvlist since they are not224* maintained through the DSL.225*/226(void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_ENCRYPTION));227(void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_KEYFORMAT));228(void) nvlist_remove_all(props, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT));229(void) nvlist_remove_all(props,230zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS));231232*dcp_out = dcp;233234return (0);235236error:237kmem_free(dcp, sizeof (dsl_crypto_params_t));238*dcp_out = NULL;239return (ret);240}241242void243dsl_crypto_params_free(dsl_crypto_params_t *dcp, boolean_t unload)244{245if (dcp == NULL)246return;247248if (dcp->cp_keylocation != NULL)249spa_strfree(dcp->cp_keylocation);250if (unload && dcp->cp_wkey != NULL)251dsl_wrapping_key_free(dcp->cp_wkey);252253kmem_free(dcp, sizeof (dsl_crypto_params_t));254}255256static int257spa_crypto_key_compare(const void *a, const void *b)258{259const dsl_crypto_key_t *dcka = a;260const dsl_crypto_key_t *dckb = b;261262if (dcka->dck_obj < dckb->dck_obj)263return (-1);264if (dcka->dck_obj > dckb->dck_obj)265return (1);266return (0);267}268269/*270* this compares a crypto key based on zk_guid. See comment on271* spa_crypto_key_compare for more information.272*/273boolean_t274dmu_objset_crypto_key_equal(objset_t *osa, objset_t *osb)275{276dsl_crypto_key_t *dcka = NULL;277dsl_crypto_key_t *dckb = NULL;278uint64_t obja, objb;279boolean_t equal;280spa_t *spa;281282spa = dmu_objset_spa(osa);283if (spa != dmu_objset_spa(osb))284return (B_FALSE);285obja = dmu_objset_ds(osa)->ds_object;286objb = dmu_objset_ds(osb)->ds_object;287288if (spa_keystore_lookup_key(spa, obja, FTAG, &dcka) != 0)289return (B_FALSE);290if (spa_keystore_lookup_key(spa, objb, FTAG, &dckb) != 0) {291spa_keystore_dsl_key_rele(spa, dcka, FTAG);292return (B_FALSE);293}294295equal = (dcka->dck_key.zk_guid == dckb->dck_key.zk_guid);296297spa_keystore_dsl_key_rele(spa, dcka, FTAG);298spa_keystore_dsl_key_rele(spa, dckb, FTAG);299300return (equal);301}302303static int304spa_key_mapping_compare(const void *a, const void *b)305{306const dsl_key_mapping_t *kma = a;307const dsl_key_mapping_t *kmb = b;308309if (kma->km_dsobj < kmb->km_dsobj)310return (-1);311if (kma->km_dsobj > kmb->km_dsobj)312return (1);313return (0);314}315316static int317spa_wkey_compare(const void *a, const void *b)318{319const dsl_wrapping_key_t *wka = a;320const dsl_wrapping_key_t *wkb = b;321322if (wka->wk_ddobj < wkb->wk_ddobj)323return (-1);324if (wka->wk_ddobj > wkb->wk_ddobj)325return (1);326return (0);327}328329void330spa_keystore_init(spa_keystore_t *sk)331{332rw_init(&sk->sk_dk_lock, NULL, RW_DEFAULT, NULL);333rw_init(&sk->sk_km_lock, NULL, RW_DEFAULT, NULL);334rw_init(&sk->sk_wkeys_lock, NULL, RW_DEFAULT, NULL);335avl_create(&sk->sk_dsl_keys, spa_crypto_key_compare,336sizeof (dsl_crypto_key_t),337offsetof(dsl_crypto_key_t, dck_avl_link));338avl_create(&sk->sk_key_mappings, spa_key_mapping_compare,339sizeof (dsl_key_mapping_t),340offsetof(dsl_key_mapping_t, km_avl_link));341avl_create(&sk->sk_wkeys, spa_wkey_compare, sizeof (dsl_wrapping_key_t),342offsetof(dsl_wrapping_key_t, wk_avl_link));343}344345void346spa_keystore_fini(spa_keystore_t *sk)347{348dsl_wrapping_key_t *wkey;349void *cookie = NULL;350351ASSERT(avl_is_empty(&sk->sk_dsl_keys));352ASSERT(avl_is_empty(&sk->sk_key_mappings));353354while ((wkey = avl_destroy_nodes(&sk->sk_wkeys, &cookie)) != NULL)355dsl_wrapping_key_free(wkey);356357avl_destroy(&sk->sk_wkeys);358avl_destroy(&sk->sk_key_mappings);359avl_destroy(&sk->sk_dsl_keys);360rw_destroy(&sk->sk_wkeys_lock);361rw_destroy(&sk->sk_km_lock);362rw_destroy(&sk->sk_dk_lock);363}364365static int366dsl_dir_get_encryption_root_ddobj(dsl_dir_t *dd, uint64_t *rddobj)367{368if (dd->dd_crypto_obj == 0)369return (SET_ERROR(ENOENT));370371return (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,372DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1, rddobj));373}374375static int376dsl_dir_get_encryption_version(dsl_dir_t *dd, uint64_t *version)377{378*version = 0;379380if (dd->dd_crypto_obj == 0)381return (SET_ERROR(ENOENT));382383/* version 0 is implied by ENOENT */384(void) zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,385DSL_CRYPTO_KEY_VERSION, 8, 1, version);386387return (0);388}389390boolean_t391dsl_dir_incompatible_encryption_version(dsl_dir_t *dd)392{393int ret;394uint64_t version = 0;395396ret = dsl_dir_get_encryption_version(dd, &version);397if (ret != 0)398return (B_FALSE);399400return (version != ZIO_CRYPT_KEY_CURRENT_VERSION);401}402403static int404spa_keystore_wkey_hold_ddobj_impl(spa_t *spa, uint64_t ddobj,405const void *tag, dsl_wrapping_key_t **wkey_out)406{407int ret;408dsl_wrapping_key_t search_wkey;409dsl_wrapping_key_t *found_wkey;410411ASSERT(RW_LOCK_HELD(&spa->spa_keystore.sk_wkeys_lock));412413/* init the search wrapping key */414search_wkey.wk_ddobj = ddobj;415416/* lookup the wrapping key */417found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, &search_wkey, NULL);418if (!found_wkey) {419ret = SET_ERROR(ENOENT);420goto error;421}422423/* increment the refcount */424dsl_wrapping_key_hold(found_wkey, tag);425426*wkey_out = found_wkey;427return (0);428429error:430*wkey_out = NULL;431return (ret);432}433434static int435spa_keystore_wkey_hold_dd(spa_t *spa, dsl_dir_t *dd, const void *tag,436dsl_wrapping_key_t **wkey_out)437{438int ret;439dsl_wrapping_key_t *wkey;440uint64_t rddobj;441boolean_t locked = B_FALSE;442443if (!RW_WRITE_HELD(&spa->spa_keystore.sk_wkeys_lock)) {444rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_READER);445locked = B_TRUE;446}447448/* get the ddobj that the keylocation property was inherited from */449ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);450if (ret != 0)451goto error;452453/* lookup the wkey in the avl tree */454ret = spa_keystore_wkey_hold_ddobj_impl(spa, rddobj, tag, &wkey);455if (ret != 0)456goto error;457458/* unlock the wkey tree if we locked it */459if (locked)460rw_exit(&spa->spa_keystore.sk_wkeys_lock);461462*wkey_out = wkey;463return (0);464465error:466if (locked)467rw_exit(&spa->spa_keystore.sk_wkeys_lock);468469*wkey_out = NULL;470return (ret);471}472473int474dsl_crypto_can_set_keylocation(const char *dsname, const char *keylocation)475{476int ret = 0;477dsl_dir_t *dd = NULL;478dsl_pool_t *dp = NULL;479uint64_t rddobj;480481/* hold the dsl dir */482ret = dsl_pool_hold(dsname, FTAG, &dp);483if (ret != 0)484goto out;485486ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);487if (ret != 0) {488dd = NULL;489goto out;490}491492/* if dd is not encrypted, the value may only be "none" */493if (dd->dd_crypto_obj == 0) {494if (strcmp(keylocation, "none") != 0) {495ret = SET_ERROR(EACCES);496goto out;497}498499ret = 0;500goto out;501}502503/* check for a valid keylocation for encrypted datasets */504if (!zfs_prop_valid_keylocation(keylocation, B_TRUE)) {505ret = SET_ERROR(EINVAL);506goto out;507}508509/* check that this is an encryption root */510ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);511if (ret != 0)512goto out;513514if (rddobj != dd->dd_object) {515ret = SET_ERROR(EACCES);516goto out;517}518519dsl_dir_rele(dd, FTAG);520dsl_pool_rele(dp, FTAG);521522return (0);523524out:525if (dd != NULL)526dsl_dir_rele(dd, FTAG);527if (dp != NULL)528dsl_pool_rele(dp, FTAG);529530return (ret);531}532533static void534dsl_crypto_key_free(dsl_crypto_key_t *dck)535{536ASSERT0(zfs_refcount_count(&dck->dck_holds));537538/* destroy the zio_crypt_key_t */539zio_crypt_key_destroy(&dck->dck_key);540541/* free the refcount, wrapping key, and lock */542zfs_refcount_destroy(&dck->dck_holds);543if (dck->dck_wkey)544dsl_wrapping_key_rele(dck->dck_wkey, dck);545546/* free the key */547kmem_free(dck, sizeof (dsl_crypto_key_t));548}549550static void551dsl_crypto_key_rele(dsl_crypto_key_t *dck, const void *tag)552{553if (zfs_refcount_remove(&dck->dck_holds, tag) == 0)554dsl_crypto_key_free(dck);555}556557static int558dsl_crypto_key_open(objset_t *mos, dsl_wrapping_key_t *wkey,559uint64_t dckobj, const void *tag, dsl_crypto_key_t **dck_out)560{561int ret;562uint64_t crypt = 0, guid = 0, version = 0;563uint8_t raw_keydata[MASTER_KEY_MAX_LEN];564uint8_t raw_hmac_keydata[SHA512_HMAC_KEYLEN];565uint8_t iv[WRAPPING_IV_LEN];566uint8_t mac[WRAPPING_MAC_LEN];567dsl_crypto_key_t *dck;568569/* allocate and initialize the key */570dck = kmem_zalloc(sizeof (dsl_crypto_key_t), KM_SLEEP);571572/* fetch all of the values we need from the ZAP */573ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,574&crypt);575if (ret != 0)576goto error;577578/* handle a future crypto suite that we don't support */579if (crypt >= ZIO_CRYPT_FUNCTIONS) {580ret = (SET_ERROR(ZFS_ERR_CRYPTO_NOTSUP));581goto error;582}583584ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1, &guid);585if (ret != 0)586goto error;587588ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,589MASTER_KEY_MAX_LEN, raw_keydata);590if (ret != 0)591goto error;592593ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,594SHA512_HMAC_KEYLEN, raw_hmac_keydata);595if (ret != 0)596goto error;597598ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,599iv);600if (ret != 0)601goto error;602603ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,604mac);605if (ret != 0)606goto error;607608/* the initial on-disk format for encryption did not have a version */609(void) zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_VERSION, 8, 1, &version);610611/*612* Unwrap the keys. If there is an error return EACCES to indicate613* an authentication failure.614*/615ret = zio_crypt_key_unwrap(&wkey->wk_key, crypt, version, guid,616raw_keydata, raw_hmac_keydata, iv, mac, &dck->dck_key);617if (ret != 0) {618ret = SET_ERROR(EACCES);619goto error;620}621622/* finish initializing the dsl_crypto_key_t */623zfs_refcount_create(&dck->dck_holds);624dsl_wrapping_key_hold(wkey, dck);625dck->dck_wkey = wkey;626dck->dck_obj = dckobj;627zfs_refcount_add(&dck->dck_holds, tag);628629*dck_out = dck;630return (0);631632error:633if (dck != NULL) {634memset(dck, 0, sizeof (dsl_crypto_key_t));635kmem_free(dck, sizeof (dsl_crypto_key_t));636}637638*dck_out = NULL;639return (ret);640}641642static int643spa_keystore_dsl_key_hold_impl(spa_t *spa, uint64_t dckobj, const void *tag,644dsl_crypto_key_t **dck_out)645{646int ret;647dsl_crypto_key_t search_dck;648dsl_crypto_key_t *found_dck;649650ASSERT(RW_LOCK_HELD(&spa->spa_keystore.sk_dk_lock));651652/* init the search key */653search_dck.dck_obj = dckobj;654655/* find the matching key in the keystore */656found_dck = avl_find(&spa->spa_keystore.sk_dsl_keys, &search_dck, NULL);657if (!found_dck) {658ret = SET_ERROR(ENOENT);659goto error;660}661662/* increment the refcount */663zfs_refcount_add(&found_dck->dck_holds, tag);664665*dck_out = found_dck;666return (0);667668error:669*dck_out = NULL;670return (ret);671}672673static int674spa_keystore_dsl_key_hold_dd(spa_t *spa, dsl_dir_t *dd, const void *tag,675dsl_crypto_key_t **dck_out)676{677int ret;678avl_index_t where;679dsl_crypto_key_t *dck_io = NULL, *dck_ks = NULL;680dsl_wrapping_key_t *wkey = NULL;681uint64_t dckobj = dd->dd_crypto_obj;682683/* Lookup the key in the tree of currently loaded keys */684rw_enter(&spa->spa_keystore.sk_dk_lock, RW_READER);685ret = spa_keystore_dsl_key_hold_impl(spa, dckobj, tag, &dck_ks);686rw_exit(&spa->spa_keystore.sk_dk_lock);687if (ret == 0) {688*dck_out = dck_ks;689return (0);690}691692/* Lookup the wrapping key from the keystore */693ret = spa_keystore_wkey_hold_dd(spa, dd, FTAG, &wkey);694if (ret != 0) {695*dck_out = NULL;696return (SET_ERROR(EACCES));697}698699/* Read the key from disk */700ret = dsl_crypto_key_open(spa->spa_meta_objset, wkey, dckobj,701tag, &dck_io);702if (ret != 0) {703dsl_wrapping_key_rele(wkey, FTAG);704*dck_out = NULL;705return (ret);706}707708/*709* Add the key to the keystore. It may already exist if it was710* added while performing the read from disk. In this case discard711* it and return the key from the keystore.712*/713rw_enter(&spa->spa_keystore.sk_dk_lock, RW_WRITER);714ret = spa_keystore_dsl_key_hold_impl(spa, dckobj, tag, &dck_ks);715if (ret != 0) {716avl_find(&spa->spa_keystore.sk_dsl_keys, dck_io, &where);717avl_insert(&spa->spa_keystore.sk_dsl_keys, dck_io, where);718*dck_out = dck_io;719} else {720dsl_crypto_key_rele(dck_io, tag);721*dck_out = dck_ks;722}723724/* Release the wrapping key (the dsl key now has a reference to it) */725dsl_wrapping_key_rele(wkey, FTAG);726rw_exit(&spa->spa_keystore.sk_dk_lock);727728return (0);729}730731void732spa_keystore_dsl_key_rele(spa_t *spa, dsl_crypto_key_t *dck, const void *tag)733{734rw_enter(&spa->spa_keystore.sk_dk_lock, RW_WRITER);735736if (zfs_refcount_remove(&dck->dck_holds, tag) == 0) {737avl_remove(&spa->spa_keystore.sk_dsl_keys, dck);738dsl_crypto_key_free(dck);739}740741rw_exit(&spa->spa_keystore.sk_dk_lock);742}743744int745spa_keystore_load_wkey_impl(spa_t *spa, dsl_wrapping_key_t *wkey)746{747int ret;748avl_index_t where;749dsl_wrapping_key_t *found_wkey;750751rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);752753/* insert the wrapping key into the keystore */754found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, wkey, &where);755if (found_wkey != NULL) {756ret = SET_ERROR(EEXIST);757goto error_unlock;758}759avl_insert(&spa->spa_keystore.sk_wkeys, wkey, where);760761rw_exit(&spa->spa_keystore.sk_wkeys_lock);762763return (0);764765error_unlock:766rw_exit(&spa->spa_keystore.sk_wkeys_lock);767return (ret);768}769770int771spa_keystore_load_wkey(const char *dsname, dsl_crypto_params_t *dcp,772boolean_t noop)773{774int ret;775dsl_dir_t *dd = NULL;776dsl_crypto_key_t *dck = NULL;777dsl_wrapping_key_t *wkey = dcp->cp_wkey;778dsl_pool_t *dp = NULL;779uint64_t rddobj, keyformat, salt, iters;780781/*782* We don't validate the wrapping key's keyformat, salt, or iters783* since they will never be needed after the DCK has been wrapped.784*/785if (dcp->cp_wkey == NULL ||786dcp->cp_cmd != DCP_CMD_NONE ||787dcp->cp_crypt != ZIO_CRYPT_INHERIT ||788dcp->cp_keylocation != NULL)789return (SET_ERROR(EINVAL));790791ret = dsl_pool_hold(dsname, FTAG, &dp);792if (ret != 0)793goto error;794795if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {796ret = SET_ERROR(ENOTSUP);797goto error;798}799800/* hold the dsl dir */801ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);802if (ret != 0) {803dd = NULL;804goto error;805}806807/* confirm that dd is the encryption root */808ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);809if (ret != 0 || rddobj != dd->dd_object) {810ret = SET_ERROR(EINVAL);811goto error;812}813814/* initialize the wkey's ddobj */815wkey->wk_ddobj = dd->dd_object;816817/* verify that the wkey is correct by opening its dsl key */818ret = dsl_crypto_key_open(dp->dp_meta_objset, wkey,819dd->dd_crypto_obj, FTAG, &dck);820if (ret != 0)821goto error;822823/* initialize the wkey encryption parameters from the DSL Crypto Key */824ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,825zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &keyformat);826if (ret != 0)827goto error;828829ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,830zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &salt);831if (ret != 0)832goto error;833834ret = zap_lookup(dp->dp_meta_objset, dd->dd_crypto_obj,835zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &iters);836if (ret != 0)837goto error;838839ASSERT3U(keyformat, <, ZFS_KEYFORMAT_FORMATS);840ASSERT3U(keyformat, !=, ZFS_KEYFORMAT_NONE);841IMPLY(keyformat == ZFS_KEYFORMAT_PASSPHRASE, iters != 0);842IMPLY(keyformat == ZFS_KEYFORMAT_PASSPHRASE, salt != 0);843IMPLY(keyformat != ZFS_KEYFORMAT_PASSPHRASE, iters == 0);844IMPLY(keyformat != ZFS_KEYFORMAT_PASSPHRASE, salt == 0);845846wkey->wk_keyformat = keyformat;847wkey->wk_salt = salt;848wkey->wk_iters = iters;849850/*851* At this point we have verified the wkey and confirmed that it can852* be used to decrypt a DSL Crypto Key. We can simply cleanup and853* return if this is all the user wanted to do.854*/855if (noop)856goto error;857858/* insert the wrapping key into the keystore */859ret = spa_keystore_load_wkey_impl(dp->dp_spa, wkey);860if (ret != 0)861goto error;862863dsl_crypto_key_rele(dck, FTAG);864dsl_dir_rele(dd, FTAG);865dsl_pool_rele(dp, FTAG);866867/* create any zvols under this ds */868zvol_create_minors(dsname);869870return (0);871872error:873if (dck != NULL)874dsl_crypto_key_rele(dck, FTAG);875if (dd != NULL)876dsl_dir_rele(dd, FTAG);877if (dp != NULL)878dsl_pool_rele(dp, FTAG);879880return (ret);881}882883int884spa_keystore_unload_wkey_impl(spa_t *spa, uint64_t ddobj)885{886int ret;887dsl_wrapping_key_t search_wkey;888dsl_wrapping_key_t *found_wkey;889890/* init the search wrapping key */891search_wkey.wk_ddobj = ddobj;892893rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);894895/* remove the wrapping key from the keystore */896found_wkey = avl_find(&spa->spa_keystore.sk_wkeys,897&search_wkey, NULL);898if (!found_wkey) {899ret = SET_ERROR(EACCES);900goto error_unlock;901} else if (zfs_refcount_count(&found_wkey->wk_refcnt) != 0) {902ret = SET_ERROR(EBUSY);903goto error_unlock;904}905avl_remove(&spa->spa_keystore.sk_wkeys, found_wkey);906907rw_exit(&spa->spa_keystore.sk_wkeys_lock);908909/* free the wrapping key */910dsl_wrapping_key_free(found_wkey);911912return (0);913914error_unlock:915rw_exit(&spa->spa_keystore.sk_wkeys_lock);916return (ret);917}918919int920spa_keystore_unload_wkey(const char *dsname)921{922int ret = 0;923dsl_dir_t *dd = NULL;924dsl_pool_t *dp = NULL;925spa_t *spa = NULL;926927ret = spa_open(dsname, &spa, FTAG);928if (ret != 0)929return (ret);930931/*932* Wait for any outstanding txg IO to complete, releasing any933* remaining references on the wkey.934*/935if (spa_mode(spa) != SPA_MODE_READ)936txg_wait_synced(spa->spa_dsl_pool, 0);937938spa_close(spa, FTAG);939940/* hold the dsl dir */941ret = dsl_pool_hold(dsname, FTAG, &dp);942if (ret != 0)943goto error;944945if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {946ret = (SET_ERROR(ENOTSUP));947goto error;948}949950ret = dsl_dir_hold(dp, dsname, FTAG, &dd, NULL);951if (ret != 0) {952dd = NULL;953goto error;954}955956/* unload the wkey */957ret = spa_keystore_unload_wkey_impl(dp->dp_spa, dd->dd_object);958if (ret != 0)959goto error;960961dsl_dir_rele(dd, FTAG);962dsl_pool_rele(dp, FTAG);963964/* remove any zvols under this ds */965zvol_remove_minors(dp->dp_spa, dsname, B_TRUE);966967return (0);968969error:970if (dd != NULL)971dsl_dir_rele(dd, FTAG);972if (dp != NULL)973dsl_pool_rele(dp, FTAG);974975return (ret);976}977978void979key_mapping_add_ref(dsl_key_mapping_t *km, const void *tag)980{981ASSERT3U(zfs_refcount_count(&km->km_refcnt), >=, 1);982zfs_refcount_add(&km->km_refcnt, tag);983}984985/*986* The locking here is a little tricky to ensure we don't cause unnecessary987* performance problems. We want to release a key mapping whenever someone988* decrements the refcount to 0, but freeing the mapping requires removing989* it from the spa_keystore, which requires holding sk_km_lock as a writer.990* Most of the time we don't want to hold this lock as a writer, since the991* same lock is held as a reader for each IO that needs to encrypt / decrypt992* data for any dataset and in practice we will only actually free the993* mapping after unmounting a dataset.994*/995void996key_mapping_rele(spa_t *spa, dsl_key_mapping_t *km, const void *tag)997{998ASSERT3U(zfs_refcount_count(&km->km_refcnt), >=, 1);9991000if (zfs_refcount_remove(&km->km_refcnt, tag) != 0)1001return;10021003/*1004* We think we are going to need to free the mapping. Add a1005* reference to prevent most other releasers from thinking1006* this might be their responsibility. This is inherently1007* racy, so we will confirm that we are legitimately the1008* last holder once we have the sk_km_lock as a writer.1009*/1010zfs_refcount_add(&km->km_refcnt, FTAG);10111012rw_enter(&spa->spa_keystore.sk_km_lock, RW_WRITER);1013if (zfs_refcount_remove(&km->km_refcnt, FTAG) != 0) {1014rw_exit(&spa->spa_keystore.sk_km_lock);1015return;1016}10171018avl_remove(&spa->spa_keystore.sk_key_mappings, km);1019rw_exit(&spa->spa_keystore.sk_km_lock);10201021spa_keystore_dsl_key_rele(spa, km->km_key, km);1022zfs_refcount_destroy(&km->km_refcnt);1023kmem_free(km, sizeof (dsl_key_mapping_t));1024}10251026int1027spa_keystore_create_mapping(spa_t *spa, dsl_dataset_t *ds, const void *tag,1028dsl_key_mapping_t **km_out)1029{1030int ret;1031avl_index_t where;1032dsl_key_mapping_t *km, *found_km;1033boolean_t should_free = B_FALSE;10341035/* Allocate and initialize the mapping */1036km = kmem_zalloc(sizeof (dsl_key_mapping_t), KM_SLEEP);1037zfs_refcount_create(&km->km_refcnt);10381039ret = spa_keystore_dsl_key_hold_dd(spa, ds->ds_dir, km, &km->km_key);1040if (ret != 0) {1041zfs_refcount_destroy(&km->km_refcnt);1042kmem_free(km, sizeof (dsl_key_mapping_t));10431044if (km_out != NULL)1045*km_out = NULL;1046return (ret);1047}10481049km->km_dsobj = ds->ds_object;10501051rw_enter(&spa->spa_keystore.sk_km_lock, RW_WRITER);10521053/*1054* If a mapping already exists, simply increment its refcount and1055* cleanup the one we made. We want to allocate / free outside of1056* the lock because this lock is also used by the zio layer to lookup1057* key mappings. Otherwise, use the one we created. Normally, there will1058* only be one active reference at a time (the objset owner), but there1059* are times when there could be multiple async users.1060*/1061found_km = avl_find(&spa->spa_keystore.sk_key_mappings, km, &where);1062if (found_km != NULL) {1063should_free = B_TRUE;1064zfs_refcount_add(&found_km->km_refcnt, tag);1065if (km_out != NULL)1066*km_out = found_km;1067} else {1068zfs_refcount_add(&km->km_refcnt, tag);1069avl_insert(&spa->spa_keystore.sk_key_mappings, km, where);1070if (km_out != NULL)1071*km_out = km;1072}10731074rw_exit(&spa->spa_keystore.sk_km_lock);10751076if (should_free) {1077spa_keystore_dsl_key_rele(spa, km->km_key, km);1078zfs_refcount_destroy(&km->km_refcnt);1079kmem_free(km, sizeof (dsl_key_mapping_t));1080}10811082return (0);1083}10841085int1086spa_keystore_remove_mapping(spa_t *spa, uint64_t dsobj, const void *tag)1087{1088int ret;1089dsl_key_mapping_t search_km;1090dsl_key_mapping_t *found_km;10911092/* init the search key mapping */1093search_km.km_dsobj = dsobj;10941095rw_enter(&spa->spa_keystore.sk_km_lock, RW_READER);10961097/* find the matching mapping */1098found_km = avl_find(&spa->spa_keystore.sk_key_mappings,1099&search_km, NULL);1100if (found_km == NULL) {1101ret = SET_ERROR(ENOENT);1102goto error_unlock;1103}11041105rw_exit(&spa->spa_keystore.sk_km_lock);11061107key_mapping_rele(spa, found_km, tag);11081109return (0);11101111error_unlock:1112rw_exit(&spa->spa_keystore.sk_km_lock);1113return (ret);1114}11151116/*1117* This function is primarily used by the zio and arc layer to lookup1118* DSL Crypto Keys for encryption. Callers must release the key with1119* spa_keystore_dsl_key_rele(). The function may also be called with1120* dck_out == NULL and tag == NULL to simply check that a key exists1121* without getting a reference to it.1122*/1123int1124spa_keystore_lookup_key(spa_t *spa, uint64_t dsobj, const void *tag,1125dsl_crypto_key_t **dck_out)1126{1127int ret;1128dsl_key_mapping_t search_km;1129dsl_key_mapping_t *found_km;11301131ASSERT((tag != NULL && dck_out != NULL) ||1132(tag == NULL && dck_out == NULL));11331134/* init the search key mapping */1135search_km.km_dsobj = dsobj;11361137rw_enter(&spa->spa_keystore.sk_km_lock, RW_READER);11381139/* remove the mapping from the tree */1140found_km = avl_find(&spa->spa_keystore.sk_key_mappings, &search_km,1141NULL);1142if (found_km == NULL) {1143ret = SET_ERROR(ENOENT);1144goto error_unlock;1145}11461147if (found_km && tag)1148zfs_refcount_add(&found_km->km_key->dck_holds, tag);11491150rw_exit(&spa->spa_keystore.sk_km_lock);11511152if (dck_out != NULL)1153*dck_out = found_km->km_key;1154return (0);11551156error_unlock:1157rw_exit(&spa->spa_keystore.sk_km_lock);11581159if (dck_out != NULL)1160*dck_out = NULL;1161return (ret);1162}11631164static int1165dmu_objset_check_wkey_loaded(dsl_dir_t *dd)1166{1167int ret;1168dsl_wrapping_key_t *wkey = NULL;11691170ret = spa_keystore_wkey_hold_dd(dd->dd_pool->dp_spa, dd, FTAG,1171&wkey);1172if (ret != 0)1173return (SET_ERROR(EACCES));11741175dsl_wrapping_key_rele(wkey, FTAG);11761177return (0);1178}11791180zfs_keystatus_t1181dsl_dataset_get_keystatus(dsl_dir_t *dd)1182{1183/* check if this dd has a has a dsl key */1184if (dd->dd_crypto_obj == 0)1185return (ZFS_KEYSTATUS_NONE);11861187return (dmu_objset_check_wkey_loaded(dd) == 0 ?1188ZFS_KEYSTATUS_AVAILABLE : ZFS_KEYSTATUS_UNAVAILABLE);1189}11901191static int1192dsl_dir_get_crypt(dsl_dir_t *dd, uint64_t *crypt)1193{1194if (dd->dd_crypto_obj == 0) {1195*crypt = ZIO_CRYPT_OFF;1196return (0);1197}11981199return (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,1200DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1, crypt));1201}12021203static void1204dsl_crypto_key_sync_impl(objset_t *mos, uint64_t dckobj, uint64_t crypt,1205uint64_t root_ddobj, uint64_t guid, uint8_t *iv, uint8_t *mac,1206uint8_t *keydata, uint8_t *hmac_keydata, uint64_t keyformat,1207uint64_t salt, uint64_t iters, dmu_tx_t *tx)1208{1209VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,1210&crypt, tx));1211VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1,1212&root_ddobj, tx));1213VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1,1214&guid, tx));1215VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,1216iv, tx));1217VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,1218mac, tx));1219VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,1220MASTER_KEY_MAX_LEN, keydata, tx));1221VERIFY0(zap_update(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,1222SHA512_HMAC_KEYLEN, hmac_keydata, tx));1223VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_KEYFORMAT),12248, 1, &keyformat, tx));1225VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),12268, 1, &salt, tx));1227VERIFY0(zap_update(mos, dckobj, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),12288, 1, &iters, tx));1229}12301231static void1232dsl_crypto_key_sync(dsl_crypto_key_t *dck, dmu_tx_t *tx)1233{1234zio_crypt_key_t *key = &dck->dck_key;1235dsl_wrapping_key_t *wkey = dck->dck_wkey;1236uint8_t keydata[MASTER_KEY_MAX_LEN];1237uint8_t hmac_keydata[SHA512_HMAC_KEYLEN];1238uint8_t iv[WRAPPING_IV_LEN];1239uint8_t mac[WRAPPING_MAC_LEN];12401241ASSERT(dmu_tx_is_syncing(tx));1242ASSERT3U(key->zk_crypt, <, ZIO_CRYPT_FUNCTIONS);12431244/* encrypt and store the keys along with the IV and MAC */1245VERIFY0(zio_crypt_key_wrap(&dck->dck_wkey->wk_key, key, iv, mac,1246keydata, hmac_keydata));12471248/* update the ZAP with the obtained values */1249dsl_crypto_key_sync_impl(tx->tx_pool->dp_meta_objset, dck->dck_obj,1250key->zk_crypt, wkey->wk_ddobj, key->zk_guid, iv, mac, keydata,1251hmac_keydata, wkey->wk_keyformat, wkey->wk_salt, wkey->wk_iters,1252tx);1253}12541255typedef struct spa_keystore_change_key_args {1256const char *skcka_dsname;1257dsl_crypto_params_t *skcka_cp;1258} spa_keystore_change_key_args_t;12591260static int1261spa_keystore_change_key_check(void *arg, dmu_tx_t *tx)1262{1263int ret;1264dsl_dir_t *dd = NULL;1265dsl_pool_t *dp = dmu_tx_pool(tx);1266spa_keystore_change_key_args_t *skcka = arg;1267dsl_crypto_params_t *dcp = skcka->skcka_cp;1268uint64_t rddobj;12691270/* check for the encryption feature */1271if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_ENCRYPTION)) {1272ret = SET_ERROR(ENOTSUP);1273goto error;1274}12751276/* check for valid key change command */1277if (dcp->cp_cmd != DCP_CMD_NEW_KEY &&1278dcp->cp_cmd != DCP_CMD_INHERIT &&1279dcp->cp_cmd != DCP_CMD_FORCE_NEW_KEY &&1280dcp->cp_cmd != DCP_CMD_FORCE_INHERIT) {1281ret = SET_ERROR(EINVAL);1282goto error;1283}12841285/* hold the dd */1286ret = dsl_dir_hold(dp, skcka->skcka_dsname, FTAG, &dd, NULL);1287if (ret != 0) {1288dd = NULL;1289goto error;1290}12911292/* verify that the dataset is encrypted */1293if (dd->dd_crypto_obj == 0) {1294ret = SET_ERROR(EINVAL);1295goto error;1296}12971298/* clones must always use their origin's key */1299if (dsl_dir_is_clone(dd)) {1300ret = SET_ERROR(EINVAL);1301goto error;1302}13031304/* lookup the ddobj we are inheriting the keylocation from */1305ret = dsl_dir_get_encryption_root_ddobj(dd, &rddobj);1306if (ret != 0)1307goto error;13081309/* Handle inheritance */1310if (dcp->cp_cmd == DCP_CMD_INHERIT ||1311dcp->cp_cmd == DCP_CMD_FORCE_INHERIT) {1312/* no other encryption params should be given */1313if (dcp->cp_crypt != ZIO_CRYPT_INHERIT ||1314dcp->cp_keylocation != NULL ||1315dcp->cp_wkey != NULL) {1316ret = SET_ERROR(EINVAL);1317goto error;1318}13191320/* check that this is an encryption root */1321if (dd->dd_object != rddobj) {1322ret = SET_ERROR(EINVAL);1323goto error;1324}13251326/* check that the parent is encrypted */1327if (dd->dd_parent->dd_crypto_obj == 0) {1328ret = SET_ERROR(EINVAL);1329goto error;1330}13311332/* if we are rewrapping check that both keys are loaded */1333if (dcp->cp_cmd == DCP_CMD_INHERIT) {1334ret = dmu_objset_check_wkey_loaded(dd);1335if (ret != 0)1336goto error;13371338ret = dmu_objset_check_wkey_loaded(dd->dd_parent);1339if (ret != 0)1340goto error;1341}13421343dsl_dir_rele(dd, FTAG);1344return (0);1345}13461347/* handle forcing an encryption root without rewrapping */1348if (dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY) {1349/* no other encryption params should be given */1350if (dcp->cp_crypt != ZIO_CRYPT_INHERIT ||1351dcp->cp_keylocation != NULL ||1352dcp->cp_wkey != NULL) {1353ret = SET_ERROR(EINVAL);1354goto error;1355}13561357/* check that this is not an encryption root */1358if (dd->dd_object == rddobj) {1359ret = SET_ERROR(EINVAL);1360goto error;1361}13621363dsl_dir_rele(dd, FTAG);1364return (0);1365}13661367/* crypt cannot be changed after creation */1368if (dcp->cp_crypt != ZIO_CRYPT_INHERIT) {1369ret = SET_ERROR(EINVAL);1370goto error;1371}13721373/* we are not inheritting our parent's wkey so we need one ourselves */1374if (dcp->cp_wkey == NULL) {1375ret = SET_ERROR(EINVAL);1376goto error;1377}13781379/* check for a valid keyformat for the new wrapping key */1380if (dcp->cp_wkey->wk_keyformat >= ZFS_KEYFORMAT_FORMATS ||1381dcp->cp_wkey->wk_keyformat == ZFS_KEYFORMAT_NONE) {1382ret = SET_ERROR(EINVAL);1383goto error;1384}13851386/*1387* If this dataset is not currently an encryption root we need a new1388* keylocation for this dataset's new wrapping key. Otherwise we can1389* just keep the one we already had.1390*/1391if (dd->dd_object != rddobj && dcp->cp_keylocation == NULL) {1392ret = SET_ERROR(EINVAL);1393goto error;1394}13951396/* check that the keylocation is valid if it is not NULL */1397if (dcp->cp_keylocation != NULL &&1398!zfs_prop_valid_keylocation(dcp->cp_keylocation, B_TRUE)) {1399ret = SET_ERROR(EINVAL);1400goto error;1401}14021403/* passphrases require pbkdf2 salt and iters */1404if (dcp->cp_wkey->wk_keyformat == ZFS_KEYFORMAT_PASSPHRASE) {1405if (dcp->cp_wkey->wk_salt == 0 ||1406dcp->cp_wkey->wk_iters < MIN_PBKDF2_ITERATIONS) {1407ret = SET_ERROR(EINVAL);1408goto error;1409}1410} else {1411if (dcp->cp_wkey->wk_salt != 0 || dcp->cp_wkey->wk_iters != 0) {1412ret = SET_ERROR(EINVAL);1413goto error;1414}1415}14161417/* make sure the dd's wkey is loaded */1418ret = dmu_objset_check_wkey_loaded(dd);1419if (ret != 0)1420goto error;14211422dsl_dir_rele(dd, FTAG);14231424return (0);14251426error:1427if (dd != NULL)1428dsl_dir_rele(dd, FTAG);14291430return (ret);1431}14321433/*1434* This function deals with the intricacies of updating wrapping1435* key references and encryption roots recursively in the event1436* of a call to 'zfs change-key' or 'zfs promote'. The 'skip'1437* parameter should always be set to B_FALSE when called1438* externally.1439*/1440static void1441spa_keystore_change_key_sync_impl(uint64_t rddobj, uint64_t ddobj,1442uint64_t new_rddobj, dsl_wrapping_key_t *wkey, boolean_t skip,1443dmu_tx_t *tx)1444{1445int ret;1446zap_cursor_t *zc;1447zap_attribute_t *za;1448dsl_pool_t *dp = dmu_tx_pool(tx);1449dsl_dir_t *dd = NULL;1450dsl_crypto_key_t *dck = NULL;1451uint64_t curr_rddobj;14521453ASSERT(RW_WRITE_HELD(&dp->dp_spa->spa_keystore.sk_wkeys_lock));14541455/* hold the dd */1456VERIFY0(dsl_dir_hold_obj(dp, ddobj, NULL, FTAG, &dd));14571458/* ignore special dsl dirs */1459if (dd->dd_myname[0] == '$' || dd->dd_myname[0] == '%') {1460dsl_dir_rele(dd, FTAG);1461return;1462}14631464ret = dsl_dir_get_encryption_root_ddobj(dd, &curr_rddobj);1465VERIFY(ret == 0 || ret == ENOENT);14661467/*1468* Stop recursing if this dsl dir didn't inherit from the root1469* or if this dd is a clone.1470*/1471if (ret == ENOENT ||1472(!skip && (curr_rddobj != rddobj || dsl_dir_is_clone(dd)))) {1473dsl_dir_rele(dd, FTAG);1474return;1475}14761477/*1478* If we don't have a wrapping key just update the dck to reflect the1479* new encryption root. Otherwise rewrap the entire dck and re-sync it1480* to disk. If skip is set, we don't do any of this work.1481*/1482if (!skip) {1483if (wkey == NULL) {1484VERIFY0(zap_update(dp->dp_meta_objset,1485dd->dd_crypto_obj,1486DSL_CRYPTO_KEY_ROOT_DDOBJ, 8, 1,1487&new_rddobj, tx));1488} else {1489VERIFY0(spa_keystore_dsl_key_hold_dd(dp->dp_spa, dd,1490FTAG, &dck));1491dsl_wrapping_key_hold(wkey, dck);1492dsl_wrapping_key_rele(dck->dck_wkey, dck);1493dck->dck_wkey = wkey;1494dsl_crypto_key_sync(dck, tx);1495spa_keystore_dsl_key_rele(dp->dp_spa, dck, FTAG);1496}1497}14981499zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP);1500za = zap_attribute_alloc();15011502/* Recurse into all child dsl dirs. */1503for (zap_cursor_init(zc, dp->dp_meta_objset,1504dsl_dir_phys(dd)->dd_child_dir_zapobj);1505zap_cursor_retrieve(zc, za) == 0;1506zap_cursor_advance(zc)) {1507spa_keystore_change_key_sync_impl(rddobj,1508za->za_first_integer, new_rddobj, wkey, B_FALSE, tx);1509}1510zap_cursor_fini(zc);15111512/*1513* Recurse into all dsl dirs of clones. We utilize the skip parameter1514* here so that we don't attempt to process the clones directly. This1515* is because the clone and its origin share the same dck, which has1516* already been updated.1517*/1518for (zap_cursor_init(zc, dp->dp_meta_objset,1519dsl_dir_phys(dd)->dd_clones);1520zap_cursor_retrieve(zc, za) == 0;1521zap_cursor_advance(zc)) {1522dsl_dataset_t *clone;15231524VERIFY0(dsl_dataset_hold_obj(dp, za->za_first_integer,1525FTAG, &clone));1526spa_keystore_change_key_sync_impl(rddobj,1527clone->ds_dir->dd_object, new_rddobj, wkey, B_TRUE, tx);1528dsl_dataset_rele(clone, FTAG);1529}1530zap_cursor_fini(zc);15311532zap_attribute_free(za);1533kmem_free(zc, sizeof (zap_cursor_t));15341535dsl_dir_rele(dd, FTAG);1536}15371538static void1539spa_keystore_change_key_sync(void *arg, dmu_tx_t *tx)1540{1541dsl_dataset_t *ds;1542avl_index_t where;1543dsl_pool_t *dp = dmu_tx_pool(tx);1544spa_t *spa = dp->dp_spa;1545spa_keystore_change_key_args_t *skcka = arg;1546dsl_crypto_params_t *dcp = skcka->skcka_cp;1547dsl_wrapping_key_t *wkey = NULL, *found_wkey;1548dsl_wrapping_key_t wkey_search;1549const char *keylocation = dcp->cp_keylocation;1550uint64_t rddobj, new_rddobj;15511552/* create and initialize the wrapping key */1553VERIFY0(dsl_dataset_hold(dp, skcka->skcka_dsname, FTAG, &ds));1554ASSERT(!ds->ds_is_snapshot);15551556if (dcp->cp_cmd == DCP_CMD_NEW_KEY ||1557dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY) {1558/*1559* We are changing to a new wkey. Set additional properties1560* which can be sent along with this ioctl. Note that this1561* command can set keylocation even if it can't normally be1562* set via 'zfs set' due to a non-local keylocation.1563*/1564if (dcp->cp_cmd == DCP_CMD_NEW_KEY) {1565wkey = dcp->cp_wkey;1566wkey->wk_ddobj = ds->ds_dir->dd_object;1567} else {1568keylocation = "prompt";1569}15701571if (keylocation != NULL) {1572dsl_prop_set_sync_impl(ds,1573zfs_prop_to_name(ZFS_PROP_KEYLOCATION),1574ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1,1575keylocation, tx);1576}15771578VERIFY0(dsl_dir_get_encryption_root_ddobj(ds->ds_dir, &rddobj));1579new_rddobj = ds->ds_dir->dd_object;1580} else {1581/*1582* We are inheritting the parent's wkey. Unset any local1583* keylocation and grab a reference to the wkey.1584*/1585if (dcp->cp_cmd == DCP_CMD_INHERIT) {1586VERIFY0(spa_keystore_wkey_hold_dd(spa,1587ds->ds_dir->dd_parent, FTAG, &wkey));1588}15891590dsl_prop_set_sync_impl(ds,1591zfs_prop_to_name(ZFS_PROP_KEYLOCATION), ZPROP_SRC_NONE,15920, 0, NULL, tx);15931594rddobj = ds->ds_dir->dd_object;1595VERIFY0(dsl_dir_get_encryption_root_ddobj(ds->ds_dir->dd_parent,1596&new_rddobj));1597}15981599if (wkey == NULL) {1600ASSERT(dcp->cp_cmd == DCP_CMD_FORCE_INHERIT ||1601dcp->cp_cmd == DCP_CMD_FORCE_NEW_KEY);1602}16031604rw_enter(&spa->spa_keystore.sk_wkeys_lock, RW_WRITER);16051606/* recurse through all children and rewrap their keys */1607spa_keystore_change_key_sync_impl(rddobj, ds->ds_dir->dd_object,1608new_rddobj, wkey, B_FALSE, tx);16091610/*1611* All references to the old wkey should be released now (if it1612* existed). Replace the wrapping key.1613*/1614wkey_search.wk_ddobj = ds->ds_dir->dd_object;1615found_wkey = avl_find(&spa->spa_keystore.sk_wkeys, &wkey_search, NULL);1616if (found_wkey != NULL) {1617ASSERT0(zfs_refcount_count(&found_wkey->wk_refcnt));1618avl_remove(&spa->spa_keystore.sk_wkeys, found_wkey);1619dsl_wrapping_key_free(found_wkey);1620}16211622if (dcp->cp_cmd == DCP_CMD_NEW_KEY) {1623avl_find(&spa->spa_keystore.sk_wkeys, wkey, &where);1624avl_insert(&spa->spa_keystore.sk_wkeys, wkey, where);1625} else if (wkey != NULL) {1626dsl_wrapping_key_rele(wkey, FTAG);1627}16281629rw_exit(&spa->spa_keystore.sk_wkeys_lock);16301631dsl_dataset_rele(ds, FTAG);1632}16331634int1635spa_keystore_change_key(const char *dsname, dsl_crypto_params_t *dcp)1636{1637spa_keystore_change_key_args_t skcka;16381639/* initialize the args struct */1640skcka.skcka_dsname = dsname;1641skcka.skcka_cp = dcp;16421643/*1644* Perform the actual work in syncing context. The blocks modified1645* here could be calculated but it would require holding the pool1646* lock and traversing all of the datasets that will have their keys1647* changed.1648*/1649return (dsl_sync_task(dsname, spa_keystore_change_key_check,1650spa_keystore_change_key_sync, &skcka, 15,1651ZFS_SPACE_CHECK_RESERVED));1652}16531654int1655dsl_dir_rename_crypt_check(dsl_dir_t *dd, dsl_dir_t *newparent)1656{1657int ret;1658uint64_t curr_rddobj, parent_rddobj;16591660if (dd->dd_crypto_obj == 0)1661return (0);16621663ret = dsl_dir_get_encryption_root_ddobj(dd, &curr_rddobj);1664if (ret != 0)1665goto error;16661667/*1668* if this is not an encryption root, we must make sure we are not1669* moving dd to a new encryption root1670*/1671if (dd->dd_object != curr_rddobj) {1672ret = dsl_dir_get_encryption_root_ddobj(newparent,1673&parent_rddobj);1674if (ret != 0)1675goto error;16761677if (parent_rddobj != curr_rddobj) {1678ret = SET_ERROR(EACCES);1679goto error;1680}1681}16821683return (0);16841685error:1686return (ret);1687}16881689/*1690* Check to make sure that a promote from targetdd to origindd will not require1691* any key rewraps.1692*/1693int1694dsl_dataset_promote_crypt_check(dsl_dir_t *target, dsl_dir_t *origin)1695{1696int ret;1697uint64_t rddobj, op_rddobj, tp_rddobj;16981699/* If the dataset is not encrypted we don't need to check anything */1700if (origin->dd_crypto_obj == 0)1701return (0);17021703/*1704* If we are not changing the first origin snapshot in a chain1705* the encryption root won't change either.1706*/1707if (dsl_dir_is_clone(origin))1708return (0);17091710/*1711* If the origin is the encryption root we will update1712* the DSL Crypto Key to point to the target instead.1713*/1714ret = dsl_dir_get_encryption_root_ddobj(origin, &rddobj);1715if (ret != 0)1716return (ret);17171718if (rddobj == origin->dd_object)1719return (0);17201721/*1722* The origin is inheriting its encryption root from its parent.1723* Check that the parent of the target has the same encryption root.1724*/1725ret = dsl_dir_get_encryption_root_ddobj(origin->dd_parent, &op_rddobj);1726if (ret == ENOENT)1727return (SET_ERROR(EACCES));1728else if (ret != 0)1729return (ret);17301731ret = dsl_dir_get_encryption_root_ddobj(target->dd_parent, &tp_rddobj);1732if (ret == ENOENT)1733return (SET_ERROR(EACCES));1734else if (ret != 0)1735return (ret);17361737if (op_rddobj != tp_rddobj)1738return (SET_ERROR(EACCES));17391740return (0);1741}17421743void1744dsl_dataset_promote_crypt_sync(dsl_dir_t *target, dsl_dir_t *origin,1745dmu_tx_t *tx)1746{1747uint64_t rddobj;1748dsl_pool_t *dp = target->dd_pool;1749dsl_dataset_t *targetds;1750dsl_dataset_t *originds;1751char *keylocation;17521753if (origin->dd_crypto_obj == 0)1754return;1755if (dsl_dir_is_clone(origin))1756return;17571758VERIFY0(dsl_dir_get_encryption_root_ddobj(origin, &rddobj));17591760if (rddobj != origin->dd_object)1761return;17621763/*1764* If the target is being promoted to the encryption root update the1765* DSL Crypto Key and keylocation to reflect that. We also need to1766* update the DSL Crypto Keys of all children inheritting their1767* encryption root to point to the new target. Otherwise, the check1768* function ensured that the encryption root will not change.1769*/1770keylocation = kmem_alloc(ZAP_MAXVALUELEN, KM_SLEEP);17711772VERIFY0(dsl_dataset_hold_obj(dp,1773dsl_dir_phys(target)->dd_head_dataset_obj, FTAG, &targetds));1774VERIFY0(dsl_dataset_hold_obj(dp,1775dsl_dir_phys(origin)->dd_head_dataset_obj, FTAG, &originds));17761777VERIFY0(dsl_prop_get_dd(origin, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),17781, ZAP_MAXVALUELEN, keylocation, NULL, B_FALSE));1779dsl_prop_set_sync_impl(targetds, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),1780ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1, keylocation, tx);1781dsl_prop_set_sync_impl(originds, zfs_prop_to_name(ZFS_PROP_KEYLOCATION),1782ZPROP_SRC_NONE, 0, 0, NULL, tx);17831784rw_enter(&dp->dp_spa->spa_keystore.sk_wkeys_lock, RW_WRITER);1785spa_keystore_change_key_sync_impl(rddobj, origin->dd_object,1786target->dd_object, NULL, B_FALSE, tx);1787rw_exit(&dp->dp_spa->spa_keystore.sk_wkeys_lock);17881789dsl_dataset_rele(targetds, FTAG);1790dsl_dataset_rele(originds, FTAG);1791kmem_free(keylocation, ZAP_MAXVALUELEN);1792}17931794int1795dmu_objset_create_crypt_check(dsl_dir_t *parentdd, dsl_crypto_params_t *dcp,1796boolean_t *will_encrypt)1797{1798int ret;1799uint64_t pcrypt, crypt;1800dsl_crypto_params_t dummy_dcp = { 0 };18011802if (will_encrypt != NULL)1803*will_encrypt = B_FALSE;18041805if (dcp == NULL)1806dcp = &dummy_dcp;18071808if (dcp->cp_cmd != DCP_CMD_NONE)1809return (SET_ERROR(EINVAL));18101811if (parentdd != NULL) {1812ret = dsl_dir_get_crypt(parentdd, &pcrypt);1813if (ret != 0)1814return (ret);1815} else {1816pcrypt = ZIO_CRYPT_OFF;1817}18181819crypt = (dcp->cp_crypt == ZIO_CRYPT_INHERIT) ? pcrypt : dcp->cp_crypt;18201821ASSERT3U(pcrypt, !=, ZIO_CRYPT_INHERIT);1822ASSERT3U(crypt, !=, ZIO_CRYPT_INHERIT);18231824/* check for valid dcp with no encryption (inherited or local) */1825if (crypt == ZIO_CRYPT_OFF) {1826/* Must not specify encryption params */1827if (dcp->cp_wkey != NULL ||1828(dcp->cp_keylocation != NULL &&1829strcmp(dcp->cp_keylocation, "none") != 0))1830return (SET_ERROR(EINVAL));18311832return (0);1833}18341835if (will_encrypt != NULL)1836*will_encrypt = B_TRUE;18371838/*1839* We will now definitely be encrypting. Check the feature flag. When1840* creating the pool the caller will check this for us since we won't1841* technically have the feature activated yet.1842*/1843if (parentdd != NULL &&1844!spa_feature_is_enabled(parentdd->dd_pool->dp_spa,1845SPA_FEATURE_ENCRYPTION)) {1846return (SET_ERROR(EOPNOTSUPP));1847}18481849/* Check for errata #4 (encryption enabled, bookmark_v2 disabled) */1850if (parentdd != NULL &&1851!spa_feature_is_enabled(parentdd->dd_pool->dp_spa,1852SPA_FEATURE_BOOKMARK_V2)) {1853return (SET_ERROR(EOPNOTSUPP));1854}18551856/* handle inheritance */1857if (dcp->cp_wkey == NULL) {1858ASSERT3P(parentdd, !=, NULL);18591860/* key must be fully unspecified */1861if (dcp->cp_keylocation != NULL)1862return (SET_ERROR(EINVAL));18631864/* parent must have a key to inherit */1865if (pcrypt == ZIO_CRYPT_OFF)1866return (SET_ERROR(EINVAL));18671868/* check for parent key */1869ret = dmu_objset_check_wkey_loaded(parentdd);1870if (ret != 0)1871return (ret);18721873return (0);1874}18751876/* At this point we should have a fully specified key. Check location */1877if (dcp->cp_keylocation == NULL ||1878!zfs_prop_valid_keylocation(dcp->cp_keylocation, B_TRUE))1879return (SET_ERROR(EINVAL));18801881/* Must have fully specified keyformat */1882switch (dcp->cp_wkey->wk_keyformat) {1883case ZFS_KEYFORMAT_HEX:1884case ZFS_KEYFORMAT_RAW:1885/* requires no pbkdf2 iters and salt */1886if (dcp->cp_wkey->wk_salt != 0 || dcp->cp_wkey->wk_iters != 0)1887return (SET_ERROR(EINVAL));1888break;1889case ZFS_KEYFORMAT_PASSPHRASE:1890/* requires pbkdf2 iters and salt */1891if (dcp->cp_wkey->wk_salt == 0 ||1892dcp->cp_wkey->wk_iters < MIN_PBKDF2_ITERATIONS)1893return (SET_ERROR(EINVAL));1894break;1895case ZFS_KEYFORMAT_NONE:1896default:1897/* keyformat must be specified and valid */1898return (SET_ERROR(EINVAL));1899}19001901return (0);1902}19031904void1905dsl_dataset_create_crypt_sync(uint64_t dsobj, dsl_dir_t *dd,1906dsl_dataset_t *origin, dsl_crypto_params_t *dcp, dmu_tx_t *tx)1907{1908dsl_pool_t *dp = dd->dd_pool;1909uint64_t crypt;1910dsl_wrapping_key_t *wkey;19111912/* clones always use their origin's wrapping key */1913if (dsl_dir_is_clone(dd)) {1914ASSERT0P(dcp);19151916/*1917* If this is an encrypted clone we just need to clone the1918* dck into dd. Zapify the dd so we can do that.1919*/1920if (origin->ds_dir->dd_crypto_obj != 0) {1921dmu_buf_will_dirty(dd->dd_dbuf, tx);1922dsl_dir_zapify(dd, tx);19231924dd->dd_crypto_obj =1925dsl_crypto_key_clone_sync(origin->ds_dir, tx);1926VERIFY0(zap_add(dp->dp_meta_objset, dd->dd_object,1927DD_FIELD_CRYPTO_KEY_OBJ, sizeof (uint64_t), 1,1928&dd->dd_crypto_obj, tx));1929}19301931return;1932}19331934/*1935* A NULL dcp at this point indicates this is the origin dataset1936* which does not have an objset to encrypt. Raw receives will handle1937* encryption separately later. In both cases we can simply return.1938*/1939if (dcp == NULL || dcp->cp_cmd == DCP_CMD_RAW_RECV)1940return;19411942crypt = dcp->cp_crypt;1943wkey = dcp->cp_wkey;19441945/* figure out the effective crypt */1946if (crypt == ZIO_CRYPT_INHERIT && dd->dd_parent != NULL)1947VERIFY0(dsl_dir_get_crypt(dd->dd_parent, &crypt));19481949/* if we aren't doing encryption just return */1950if (crypt == ZIO_CRYPT_OFF || crypt == ZIO_CRYPT_INHERIT)1951return;19521953/* zapify the dd so that we can add the crypto key obj to it */1954dmu_buf_will_dirty(dd->dd_dbuf, tx);1955dsl_dir_zapify(dd, tx);19561957/* use the new key if given or inherit from the parent */1958if (wkey == NULL) {1959VERIFY0(spa_keystore_wkey_hold_dd(dp->dp_spa,1960dd->dd_parent, FTAG, &wkey));1961} else {1962wkey->wk_ddobj = dd->dd_object;1963}19641965ASSERT3P(wkey, !=, NULL);19661967/* Create or clone the DSL crypto key and activate the feature */1968dd->dd_crypto_obj = dsl_crypto_key_create_sync(crypt, wkey, tx);1969VERIFY0(zap_add(dp->dp_meta_objset, dd->dd_object,1970DD_FIELD_CRYPTO_KEY_OBJ, sizeof (uint64_t), 1, &dd->dd_crypto_obj,1971tx));1972dsl_dataset_activate_feature(dsobj, SPA_FEATURE_ENCRYPTION,1973(void *)B_TRUE, tx);19741975/*1976* If we inherited the wrapping key we release our reference now.1977* Otherwise, this is a new key and we need to load it into the1978* keystore.1979*/1980if (dcp->cp_wkey == NULL) {1981dsl_wrapping_key_rele(wkey, FTAG);1982} else {1983VERIFY0(spa_keystore_load_wkey_impl(dp->dp_spa, wkey));1984}1985}19861987typedef struct dsl_crypto_recv_key_arg {1988uint64_t dcrka_dsobj;1989uint64_t dcrka_fromobj;1990dmu_objset_type_t dcrka_ostype;1991nvlist_t *dcrka_nvl;1992boolean_t dcrka_do_key;1993} dsl_crypto_recv_key_arg_t;19941995static int1996dsl_crypto_recv_raw_objset_check(dsl_dataset_t *ds, dsl_dataset_t *fromds,1997dmu_objset_type_t ostype, nvlist_t *nvl, dmu_tx_t *tx)1998{1999int ret;2000objset_t *os;2001dnode_t *mdn;2002uint8_t *buf = NULL;2003uint_t len;2004uint64_t intval, nlevels, blksz, ibs;2005uint64_t nblkptr, maxblkid;20062007if (ostype != DMU_OST_ZFS && ostype != DMU_OST_ZVOL)2008return (SET_ERROR(EINVAL));20092010/* raw receives also need info about the structure of the metadnode */2011ret = nvlist_lookup_uint64(nvl, "mdn_compress", &intval);2012if (ret != 0 || intval >= ZIO_COMPRESS_LEGACY_FUNCTIONS)2013return (SET_ERROR(EINVAL));20142015ret = nvlist_lookup_uint64(nvl, "mdn_checksum", &intval);2016if (ret != 0 || intval >= ZIO_CHECKSUM_LEGACY_FUNCTIONS)2017return (SET_ERROR(EINVAL));20182019ret = nvlist_lookup_uint64(nvl, "mdn_nlevels", &nlevels);2020if (ret != 0 || nlevels > DN_MAX_LEVELS)2021return (SET_ERROR(EINVAL));20222023ret = nvlist_lookup_uint64(nvl, "mdn_blksz", &blksz);2024if (ret != 0 || blksz < SPA_MINBLOCKSIZE)2025return (SET_ERROR(EINVAL));2026else if (blksz > spa_maxblocksize(tx->tx_pool->dp_spa))2027return (SET_ERROR(ENOTSUP));20282029ret = nvlist_lookup_uint64(nvl, "mdn_indblkshift", &ibs);2030if (ret != 0 || ibs < DN_MIN_INDBLKSHIFT || ibs > DN_MAX_INDBLKSHIFT)2031return (SET_ERROR(ENOTSUP));20322033ret = nvlist_lookup_uint64(nvl, "mdn_nblkptr", &nblkptr);2034if (ret != 0 || nblkptr != DN_MAX_NBLKPTR)2035return (SET_ERROR(ENOTSUP));20362037ret = nvlist_lookup_uint64(nvl, "mdn_maxblkid", &maxblkid);2038if (ret != 0)2039return (SET_ERROR(EINVAL));20402041ret = nvlist_lookup_uint8_array(nvl, "portable_mac", &buf, &len);2042if (ret != 0 || len != ZIO_OBJSET_MAC_LEN)2043return (SET_ERROR(EINVAL));20442045ret = dmu_objset_from_ds(ds, &os);2046if (ret != 0)2047return (ret);20482049mdn = DMU_META_DNODE(os);20502051/*2052* If we already created the objset, make sure its unchangeable2053* properties match the ones received in the nvlist.2054*/2055rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);2056if (!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)) &&2057(mdn->dn_nlevels != nlevels || mdn->dn_datablksz != blksz ||2058mdn->dn_indblkshift != ibs || mdn->dn_nblkptr != nblkptr)) {2059rrw_exit(&ds->ds_bp_rwlock, FTAG);2060return (SET_ERROR(EINVAL));2061}2062rrw_exit(&ds->ds_bp_rwlock, FTAG);20632064/*2065* Check that the ivset guid of the fromds matches the one from the2066* send stream. Older versions of the encryption code did not have2067* an ivset guid on the from dataset and did not send one in the2068* stream. For these streams we provide the2069* zfs_disable_ivset_guid_check tunable to allow these datasets to2070* be received with a generated ivset guid.2071*/2072if (fromds != NULL && !zfs_disable_ivset_guid_check) {2073uint64_t from_ivset_guid = 0;2074intval = 0;20752076(void) nvlist_lookup_uint64(nvl, "from_ivset_guid", &intval);2077(void) zap_lookup(tx->tx_pool->dp_meta_objset,2078fromds->ds_object, DS_FIELD_IVSET_GUID,2079sizeof (from_ivset_guid), 1, &from_ivset_guid);20802081if (intval == 0 || from_ivset_guid == 0)2082return (SET_ERROR(ZFS_ERR_FROM_IVSET_GUID_MISSING));20832084if (intval != from_ivset_guid)2085return (SET_ERROR(ZFS_ERR_FROM_IVSET_GUID_MISMATCH));2086}20872088return (0);2089}20902091static void2092dsl_crypto_recv_raw_objset_sync(dsl_dataset_t *ds, dmu_objset_type_t ostype,2093nvlist_t *nvl, dmu_tx_t *tx)2094{2095dsl_pool_t *dp = tx->tx_pool;2096objset_t *os;2097dnode_t *mdn;2098zio_t *zio;2099uint8_t *portable_mac;2100uint_t len;2101uint64_t compress, checksum, nlevels, blksz, ibs, maxblkid;2102boolean_t newds = B_FALSE;21032104VERIFY0(dmu_objset_from_ds(ds, &os));2105mdn = DMU_META_DNODE(os);21062107/*2108* Fetch the values we need from the nvlist. "to_ivset_guid" must2109* be set on the snapshot, which doesn't exist yet. The receive2110* code will take care of this for us later.2111*/2112compress = fnvlist_lookup_uint64(nvl, "mdn_compress");2113checksum = fnvlist_lookup_uint64(nvl, "mdn_checksum");2114nlevels = fnvlist_lookup_uint64(nvl, "mdn_nlevels");2115blksz = fnvlist_lookup_uint64(nvl, "mdn_blksz");2116ibs = fnvlist_lookup_uint64(nvl, "mdn_indblkshift");2117maxblkid = fnvlist_lookup_uint64(nvl, "mdn_maxblkid");2118VERIFY0(nvlist_lookup_uint8_array(nvl, "portable_mac", &portable_mac,2119&len));21202121/* if we haven't created an objset for the ds yet, do that now */2122rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);2123if (BP_IS_HOLE(dsl_dataset_get_blkptr(ds))) {2124(void) dmu_objset_create_impl_dnstats(dp->dp_spa, ds,2125dsl_dataset_get_blkptr(ds), ostype, nlevels, blksz,2126ibs, tx);2127newds = B_TRUE;2128}2129rrw_exit(&ds->ds_bp_rwlock, FTAG);21302131/*2132* Set the portable MAC. The local MAC will always be zero since the2133* incoming data will all be portable and user accounting will be2134* deferred until the next mount. Afterwards, flag the os to be2135* written out raw next time.2136*/2137arc_release(os->os_phys_buf, &os->os_phys_buf);2138memcpy(os->os_phys->os_portable_mac, portable_mac, ZIO_OBJSET_MAC_LEN);2139memset(os->os_phys->os_local_mac, 0, ZIO_OBJSET_MAC_LEN);2140os->os_flags &= ~OBJSET_FLAG_USERACCOUNTING_COMPLETE;2141os->os_next_write_raw[tx->tx_txg & TXG_MASK] = B_TRUE;21422143/* set metadnode compression and checksum */2144mdn->dn_compress = compress;2145mdn->dn_checksum = checksum;21462147rw_enter(&mdn->dn_struct_rwlock, RW_WRITER);2148dnode_new_blkid(mdn, maxblkid, tx, B_FALSE, B_TRUE);2149rw_exit(&mdn->dn_struct_rwlock);21502151/*2152* We can't normally dirty the dataset in syncing context unless2153* we are creating a new dataset. In this case, we perform a2154* pseudo txg sync here instead.2155*/2156if (newds) {2157dsl_dataset_dirty(ds, tx);2158} else {2159zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);2160dsl_dataset_sync(ds, zio, tx);2161VERIFY0(zio_wait(zio));2162dsl_dataset_sync_done(ds, tx);2163}2164}21652166int2167dsl_crypto_recv_raw_key_check(dsl_dataset_t *ds, nvlist_t *nvl, dmu_tx_t *tx)2168{2169int ret;2170objset_t *mos = tx->tx_pool->dp_meta_objset;2171uint8_t *buf = NULL;2172uint_t len;2173uint64_t intval, key_guid, version;2174boolean_t is_passphrase = B_FALSE;21752176ASSERT(dsl_dataset_phys(ds)->ds_flags & DS_FLAG_INCONSISTENT);21772178/*2179* Read and check all the encryption values from the nvlist. We need2180* all of the fields of a DSL Crypto Key, as well as a fully specified2181* wrapping key.2182*/2183ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE, &intval);2184if (ret != 0 || intval <= ZIO_CRYPT_OFF)2185return (SET_ERROR(EINVAL));21862187/*2188* Flag a future crypto suite that we don't support differently, so2189* we can return a more useful error to the user.2190*/2191if (intval >= ZIO_CRYPT_FUNCTIONS)2192return (SET_ERROR(ZFS_ERR_CRYPTO_NOTSUP));21932194ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_GUID, &intval);2195if (ret != 0)2196return (SET_ERROR(EINVAL));21972198/*2199* If this is an incremental receive make sure the given key guid2200* matches the one we already have.2201*/2202if (ds->ds_dir->dd_crypto_obj != 0) {2203ret = zap_lookup(mos, ds->ds_dir->dd_crypto_obj,2204DSL_CRYPTO_KEY_GUID, 8, 1, &key_guid);2205if (ret != 0)2206return (ret);2207if (intval != key_guid)2208return (SET_ERROR(EACCES));2209}22102211ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,2212&buf, &len);2213if (ret != 0 || len != MASTER_KEY_MAX_LEN)2214return (SET_ERROR(EINVAL));22152216ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,2217&buf, &len);2218if (ret != 0 || len != SHA512_HMAC_KEYLEN)2219return (SET_ERROR(EINVAL));22202221ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_IV, &buf, &len);2222if (ret != 0 || len != WRAPPING_IV_LEN)2223return (SET_ERROR(EINVAL));22242225ret = nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, &buf, &len);2226if (ret != 0 || len != WRAPPING_MAC_LEN)2227return (SET_ERROR(EINVAL));22282229/*2230* We don't support receiving old on-disk formats. The version 02231* implementation protected several fields in an objset that were2232* not always portable during a raw receive. As a result, we call2233* the old version an on-disk errata #3.2234*/2235ret = nvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_VERSION, &version);2236if (ret != 0 || version != ZIO_CRYPT_KEY_CURRENT_VERSION)2237return (SET_ERROR(ENOTSUP));22382239ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_KEYFORMAT),2240&intval);2241if (ret != 0 || intval >= ZFS_KEYFORMAT_FORMATS ||2242intval == ZFS_KEYFORMAT_NONE)2243return (SET_ERROR(EINVAL));22442245is_passphrase = (intval == ZFS_KEYFORMAT_PASSPHRASE);22462247/*2248* for raw receives we allow any number of pbkdf2iters since there2249* won't be a chance for the user to change it.2250*/2251ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS),2252&intval);2253if (ret != 0 || (is_passphrase == (intval == 0)))2254return (SET_ERROR(EINVAL));22552256ret = nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT),2257&intval);2258if (ret != 0 || (is_passphrase == (intval == 0)))2259return (SET_ERROR(EINVAL));22602261return (0);2262}22632264void2265dsl_crypto_recv_raw_key_sync(dsl_dataset_t *ds, nvlist_t *nvl, dmu_tx_t *tx)2266{2267dsl_pool_t *dp = tx->tx_pool;2268objset_t *mos = dp->dp_meta_objset;2269dsl_dir_t *dd = ds->ds_dir;2270uint_t len;2271uint64_t rddobj, one = 1;2272uint8_t *keydata, *hmac_keydata, *iv, *mac;2273uint64_t crypt, key_guid, keyformat, iters, salt;2274uint64_t version = ZIO_CRYPT_KEY_CURRENT_VERSION;2275const char *keylocation = "prompt";22762277/* lookup the values we need to create the DSL Crypto Key */2278crypt = fnvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE);2279key_guid = fnvlist_lookup_uint64(nvl, DSL_CRYPTO_KEY_GUID);2280keyformat = fnvlist_lookup_uint64(nvl,2281zfs_prop_to_name(ZFS_PROP_KEYFORMAT));2282iters = fnvlist_lookup_uint64(nvl,2283zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS));2284salt = fnvlist_lookup_uint64(nvl,2285zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT));2286VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,2287&keydata, &len));2288VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,2289&hmac_keydata, &len));2290VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_IV, &iv, &len));2291VERIFY0(nvlist_lookup_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, &mac, &len));22922293/* if this is a new dataset setup the DSL Crypto Key. */2294if (dd->dd_crypto_obj == 0) {2295/* zapify the dsl dir so we can add the key object to it */2296dmu_buf_will_dirty(dd->dd_dbuf, tx);2297dsl_dir_zapify(dd, tx);22982299/* create the DSL Crypto Key on disk and activate the feature */2300dd->dd_crypto_obj = zap_create(mos,2301DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);2302VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,2303dd->dd_crypto_obj, DSL_CRYPTO_KEY_REFCOUNT,2304sizeof (uint64_t), 1, &one, tx));2305VERIFY0(zap_update(tx->tx_pool->dp_meta_objset,2306dd->dd_crypto_obj, DSL_CRYPTO_KEY_VERSION,2307sizeof (uint64_t), 1, &version, tx));23082309dsl_dataset_activate_feature(ds->ds_object,2310SPA_FEATURE_ENCRYPTION, (void *)B_TRUE, tx);2311ds->ds_feature[SPA_FEATURE_ENCRYPTION] = (void *)B_TRUE;23122313/* save the dd_crypto_obj on disk */2314VERIFY0(zap_add(mos, dd->dd_object, DD_FIELD_CRYPTO_KEY_OBJ,2315sizeof (uint64_t), 1, &dd->dd_crypto_obj, tx));23162317/*2318* Set the keylocation to prompt by default. If keylocation2319* has been provided via the properties, this will be overridden2320* later.2321*/2322dsl_prop_set_sync_impl(ds,2323zfs_prop_to_name(ZFS_PROP_KEYLOCATION),2324ZPROP_SRC_LOCAL, 1, strlen(keylocation) + 1,2325keylocation, tx);23262327rddobj = dd->dd_object;2328} else {2329VERIFY0(dsl_dir_get_encryption_root_ddobj(dd, &rddobj));2330}23312332/* sync the key data to the ZAP object on disk */2333dsl_crypto_key_sync_impl(mos, dd->dd_crypto_obj, crypt,2334rddobj, key_guid, iv, mac, keydata, hmac_keydata, keyformat, salt,2335iters, tx);2336}23372338static int2339dsl_crypto_recv_key_check(void *arg, dmu_tx_t *tx)2340{2341int ret;2342dsl_crypto_recv_key_arg_t *dcrka = arg;2343dsl_dataset_t *ds = NULL, *fromds = NULL;23442345ret = dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_dsobj,2346FTAG, &ds);2347if (ret != 0)2348goto out;23492350if (dcrka->dcrka_fromobj != 0) {2351ret = dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_fromobj,2352FTAG, &fromds);2353if (ret != 0)2354goto out;2355}23562357ret = dsl_crypto_recv_raw_objset_check(ds, fromds,2358dcrka->dcrka_ostype, dcrka->dcrka_nvl, tx);2359if (ret != 0)2360goto out;23612362/*2363* We run this check even if we won't be doing this part of2364* the receive now so that we don't make the user wait until2365* the receive finishes to fail.2366*/2367ret = dsl_crypto_recv_raw_key_check(ds, dcrka->dcrka_nvl, tx);2368if (ret != 0)2369goto out;23702371out:2372if (ds != NULL)2373dsl_dataset_rele(ds, FTAG);2374if (fromds != NULL)2375dsl_dataset_rele(fromds, FTAG);2376return (ret);2377}23782379static void2380dsl_crypto_recv_key_sync(void *arg, dmu_tx_t *tx)2381{2382dsl_crypto_recv_key_arg_t *dcrka = arg;2383dsl_dataset_t *ds;23842385VERIFY0(dsl_dataset_hold_obj(tx->tx_pool, dcrka->dcrka_dsobj,2386FTAG, &ds));2387dsl_crypto_recv_raw_objset_sync(ds, dcrka->dcrka_ostype,2388dcrka->dcrka_nvl, tx);2389if (dcrka->dcrka_do_key)2390dsl_crypto_recv_raw_key_sync(ds, dcrka->dcrka_nvl, tx);2391dsl_dataset_rele(ds, FTAG);2392}23932394/*2395* This function is used to sync an nvlist representing a DSL Crypto Key and2396* the associated encryption parameters. The key will be written exactly as is2397* without wrapping it.2398*/2399int2400dsl_crypto_recv_raw(const char *poolname, uint64_t dsobj, uint64_t fromobj,2401dmu_objset_type_t ostype, nvlist_t *nvl, boolean_t do_key)2402{2403dsl_crypto_recv_key_arg_t dcrka;24042405dcrka.dcrka_dsobj = dsobj;2406dcrka.dcrka_fromobj = fromobj;2407dcrka.dcrka_ostype = ostype;2408dcrka.dcrka_nvl = nvl;2409dcrka.dcrka_do_key = do_key;24102411return (dsl_sync_task(poolname, dsl_crypto_recv_key_check,2412dsl_crypto_recv_key_sync, &dcrka, 1, ZFS_SPACE_CHECK_NORMAL));2413}24142415int2416dsl_crypto_populate_key_nvlist(objset_t *os, uint64_t from_ivset_guid,2417nvlist_t **nvl_out)2418{2419int ret;2420dsl_dataset_t *ds = os->os_dsl_dataset;2421dnode_t *mdn;2422uint64_t rddobj;2423nvlist_t *nvl = NULL;2424uint64_t dckobj = ds->ds_dir->dd_crypto_obj;2425dsl_dir_t *rdd = NULL;2426dsl_pool_t *dp = ds->ds_dir->dd_pool;2427objset_t *mos = dp->dp_meta_objset;2428uint64_t crypt = 0, key_guid = 0, format = 0;2429uint64_t iters = 0, salt = 0, version = 0;2430uint64_t to_ivset_guid = 0;2431uint8_t raw_keydata[MASTER_KEY_MAX_LEN];2432uint8_t raw_hmac_keydata[SHA512_HMAC_KEYLEN];2433uint8_t iv[WRAPPING_IV_LEN];2434uint8_t mac[WRAPPING_MAC_LEN];24352436ASSERT(dckobj != 0);24372438mdn = DMU_META_DNODE(os);24392440nvl = fnvlist_alloc();24412442/* lookup values from the DSL Crypto Key */2443ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_CRYPTO_SUITE, 8, 1,2444&crypt);2445if (ret != 0)2446goto error;24472448ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_GUID, 8, 1, &key_guid);2449if (ret != 0)2450goto error;24512452ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MASTER_KEY, 1,2453MASTER_KEY_MAX_LEN, raw_keydata);2454if (ret != 0)2455goto error;24562457ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_HMAC_KEY, 1,2458SHA512_HMAC_KEYLEN, raw_hmac_keydata);2459if (ret != 0)2460goto error;24612462ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_IV, 1, WRAPPING_IV_LEN,2463iv);2464if (ret != 0)2465goto error;24662467ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_MAC, 1, WRAPPING_MAC_LEN,2468mac);2469if (ret != 0)2470goto error;24712472/* see zfs_disable_ivset_guid_check tunable for errata info */2473ret = zap_lookup(mos, ds->ds_object, DS_FIELD_IVSET_GUID, 8, 1,2474&to_ivset_guid);2475if (ret != 0)2476ASSERT3U(dp->dp_spa->spa_errata, !=, 0);24772478/*2479* We don't support raw sends of legacy on-disk formats. See the2480* comment in dsl_crypto_recv_key_check() for details.2481*/2482ret = zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_VERSION, 8, 1, &version);2483if (ret != 0 || version != ZIO_CRYPT_KEY_CURRENT_VERSION) {2484dp->dp_spa->spa_errata = ZPOOL_ERRATA_ZOL_6845_ENCRYPTION;2485ret = SET_ERROR(ENOTSUP);2486goto error;2487}24882489/*2490* Lookup wrapping key properties. An early version of the code did2491* not correctly add these values to the wrapping key or the DSL2492* Crypto Key on disk for non encryption roots, so to be safe we2493* always take the slightly circuitous route of looking it up from2494* the encryption root's key.2495*/2496ret = dsl_dir_get_encryption_root_ddobj(ds->ds_dir, &rddobj);2497if (ret != 0)2498goto error;24992500dsl_pool_config_enter(dp, FTAG);25012502ret = dsl_dir_hold_obj(dp, rddobj, NULL, FTAG, &rdd);2503if (ret != 0)2504goto error_unlock;25052506ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,2507zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &format);2508if (ret != 0)2509goto error_unlock;25102511if (format == ZFS_KEYFORMAT_PASSPHRASE) {2512ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,2513zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &iters);2514if (ret != 0)2515goto error_unlock;25162517ret = zap_lookup(dp->dp_meta_objset, rdd->dd_crypto_obj,2518zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &salt);2519if (ret != 0)2520goto error_unlock;2521}25222523dsl_dir_rele(rdd, FTAG);2524dsl_pool_config_exit(dp, FTAG);25252526fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_CRYPTO_SUITE, crypt);2527fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_GUID, key_guid);2528fnvlist_add_uint64(nvl, DSL_CRYPTO_KEY_VERSION, version);2529VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_MASTER_KEY,2530raw_keydata, MASTER_KEY_MAX_LEN));2531VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_HMAC_KEY,2532raw_hmac_keydata, SHA512_HMAC_KEYLEN));2533VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_IV, iv,2534WRAPPING_IV_LEN));2535VERIFY0(nvlist_add_uint8_array(nvl, DSL_CRYPTO_KEY_MAC, mac,2536WRAPPING_MAC_LEN));2537VERIFY0(nvlist_add_uint8_array(nvl, "portable_mac",2538os->os_phys->os_portable_mac, ZIO_OBJSET_MAC_LEN));2539fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_KEYFORMAT), format);2540fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), iters);2541fnvlist_add_uint64(nvl, zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), salt);2542fnvlist_add_uint64(nvl, "mdn_checksum", mdn->dn_checksum);2543fnvlist_add_uint64(nvl, "mdn_compress", mdn->dn_compress);2544fnvlist_add_uint64(nvl, "mdn_nlevels", mdn->dn_nlevels);2545fnvlist_add_uint64(nvl, "mdn_blksz", mdn->dn_datablksz);2546fnvlist_add_uint64(nvl, "mdn_indblkshift", mdn->dn_indblkshift);2547fnvlist_add_uint64(nvl, "mdn_nblkptr", mdn->dn_nblkptr);2548fnvlist_add_uint64(nvl, "mdn_maxblkid", mdn->dn_maxblkid);2549fnvlist_add_uint64(nvl, "to_ivset_guid", to_ivset_guid);2550fnvlist_add_uint64(nvl, "from_ivset_guid", from_ivset_guid);25512552*nvl_out = nvl;2553return (0);25542555error_unlock:2556dsl_pool_config_exit(dp, FTAG);2557error:2558if (rdd != NULL)2559dsl_dir_rele(rdd, FTAG);2560nvlist_free(nvl);25612562*nvl_out = NULL;2563return (ret);2564}25652566uint64_t2567dsl_crypto_key_create_sync(uint64_t crypt, dsl_wrapping_key_t *wkey,2568dmu_tx_t *tx)2569{2570dsl_crypto_key_t dck;2571uint64_t version = ZIO_CRYPT_KEY_CURRENT_VERSION;2572uint64_t one = 1ULL;25732574ASSERT(dmu_tx_is_syncing(tx));2575ASSERT3U(crypt, <, ZIO_CRYPT_FUNCTIONS);2576ASSERT3U(crypt, >, ZIO_CRYPT_OFF);25772578/* create the DSL Crypto Key ZAP object */2579dck.dck_obj = zap_create(tx->tx_pool->dp_meta_objset,2580DMU_OTN_ZAP_METADATA, DMU_OT_NONE, 0, tx);25812582/* fill in the key (on the stack) and sync it to disk */2583dck.dck_wkey = wkey;2584VERIFY0(zio_crypt_key_init(crypt, &dck.dck_key));25852586dsl_crypto_key_sync(&dck, tx);2587VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, dck.dck_obj,2588DSL_CRYPTO_KEY_REFCOUNT, sizeof (uint64_t), 1, &one, tx));2589VERIFY0(zap_update(tx->tx_pool->dp_meta_objset, dck.dck_obj,2590DSL_CRYPTO_KEY_VERSION, sizeof (uint64_t), 1, &version, tx));25912592zio_crypt_key_destroy(&dck.dck_key);2593memset(&dck.dck_key, 0, sizeof (zio_crypt_key_t));25942595return (dck.dck_obj);2596}25972598uint64_t2599dsl_crypto_key_clone_sync(dsl_dir_t *origindd, dmu_tx_t *tx)2600{2601objset_t *mos = tx->tx_pool->dp_meta_objset;26022603ASSERT(dmu_tx_is_syncing(tx));26042605VERIFY0(zap_increment(mos, origindd->dd_crypto_obj,2606DSL_CRYPTO_KEY_REFCOUNT, 1, tx));26072608return (origindd->dd_crypto_obj);2609}26102611void2612dsl_crypto_key_destroy_sync(uint64_t dckobj, dmu_tx_t *tx)2613{2614objset_t *mos = tx->tx_pool->dp_meta_objset;2615uint64_t refcnt;26162617/* Decrement the refcount, destroy if this is the last reference */2618VERIFY0(zap_lookup(mos, dckobj, DSL_CRYPTO_KEY_REFCOUNT,2619sizeof (uint64_t), 1, &refcnt));26202621if (refcnt != 1) {2622VERIFY0(zap_increment(mos, dckobj, DSL_CRYPTO_KEY_REFCOUNT,2623-1, tx));2624} else {2625VERIFY0(zap_destroy(mos, dckobj, tx));2626}2627}26282629void2630dsl_dataset_crypt_stats(dsl_dataset_t *ds, nvlist_t *nv)2631{2632uint64_t intval;2633dsl_dir_t *dd = ds->ds_dir;2634dsl_dir_t *enc_root;2635char buf[ZFS_MAX_DATASET_NAME_LEN];26362637if (dd->dd_crypto_obj == 0)2638return;26392640intval = dsl_dataset_get_keystatus(dd);2641dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEYSTATUS, intval);26422643if (dsl_dir_get_crypt(dd, &intval) == 0)2644dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_ENCRYPTION, intval);2645if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,2646DSL_CRYPTO_KEY_GUID, 8, 1, &intval) == 0) {2647dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEY_GUID, intval);2648}2649if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,2650zfs_prop_to_name(ZFS_PROP_KEYFORMAT), 8, 1, &intval) == 0) {2651dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_KEYFORMAT, intval);2652}2653if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,2654zfs_prop_to_name(ZFS_PROP_PBKDF2_SALT), 8, 1, &intval) == 0) {2655dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_PBKDF2_SALT, intval);2656}2657if (zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,2658zfs_prop_to_name(ZFS_PROP_PBKDF2_ITERS), 8, 1, &intval) == 0) {2659dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_PBKDF2_ITERS, intval);2660}2661if (zap_lookup(dd->dd_pool->dp_meta_objset, ds->ds_object,2662DS_FIELD_IVSET_GUID, 8, 1, &intval) == 0) {2663dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_IVSET_GUID, intval);2664}26652666if (dsl_dir_get_encryption_root_ddobj(dd, &intval) == 0) {2667if (dsl_dir_hold_obj(dd->dd_pool, intval, NULL, FTAG,2668&enc_root) == 0) {2669dsl_dir_name(enc_root, buf);2670dsl_dir_rele(enc_root, FTAG);2671dsl_prop_nvlist_add_string(nv,2672ZFS_PROP_ENCRYPTION_ROOT, buf);2673}2674}2675}26762677int2678spa_crypt_get_salt(spa_t *spa, uint64_t dsobj, uint8_t *salt)2679{2680int ret;2681dsl_crypto_key_t *dck = NULL;26822683/* look up the key from the spa's keystore */2684ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);2685if (ret != 0)2686goto error;26872688ret = zio_crypt_key_get_salt(&dck->dck_key, salt);2689if (ret != 0)2690goto error;26912692spa_keystore_dsl_key_rele(spa, dck, FTAG);2693return (0);26942695error:2696if (dck != NULL)2697spa_keystore_dsl_key_rele(spa, dck, FTAG);2698return (ret);2699}27002701/*2702* Objset blocks are a special case for MAC generation. These blocks have 22703* 256-bit MACs which are embedded within the block itself, rather than a2704* single 128 bit MAC. As a result, this function handles encoding and decoding2705* the MACs on its own, unlike other functions in this file.2706*/2707int2708spa_do_crypt_objset_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj,2709abd_t *abd, uint_t datalen, boolean_t byteswap)2710{2711int ret;2712dsl_crypto_key_t *dck = NULL;2713void *buf = abd_borrow_buf_copy(abd, datalen);2714objset_phys_t *osp = buf;2715uint8_t portable_mac[ZIO_OBJSET_MAC_LEN];2716uint8_t local_mac[ZIO_OBJSET_MAC_LEN];2717const uint8_t zeroed_mac[ZIO_OBJSET_MAC_LEN] = {0};27182719/* look up the key from the spa's keystore */2720ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);2721if (ret != 0)2722goto error;27232724/* calculate both HMACs */2725ret = zio_crypt_do_objset_hmacs(&dck->dck_key, buf, datalen,2726byteswap, portable_mac, local_mac);2727if (ret != 0)2728goto error;27292730spa_keystore_dsl_key_rele(spa, dck, FTAG);27312732/* if we are generating encode the HMACs in the objset_phys_t */2733if (generate) {2734memcpy(osp->os_portable_mac, portable_mac, ZIO_OBJSET_MAC_LEN);2735memcpy(osp->os_local_mac, local_mac, ZIO_OBJSET_MAC_LEN);2736abd_return_buf_copy(abd, buf, datalen);2737return (0);2738}27392740if (memcmp(portable_mac, osp->os_portable_mac,2741ZIO_OBJSET_MAC_LEN) != 0) {2742abd_return_buf(abd, buf, datalen);2743return (SET_ERROR(ECKSUM));2744}2745if (memcmp(local_mac, osp->os_local_mac, ZIO_OBJSET_MAC_LEN) != 0) {2746/*2747* If the MAC is zeroed out, we failed to decrypt it.2748* This should only arise, at least on Linux,2749* if we hit edge case handling for useraccounting, since we2750* shouldn't get here without bailing out on error earlier2751* otherwise.2752*2753* So if we're in that case, we can just fall through and2754* special-casing noticing that it's zero will handle it2755* elsewhere, since we can just regenerate it.2756*/2757if (memcmp(local_mac, zeroed_mac, ZIO_OBJSET_MAC_LEN) != 0) {2758abd_return_buf(abd, buf, datalen);2759return (SET_ERROR(ECKSUM));2760}2761}27622763abd_return_buf(abd, buf, datalen);27642765return (0);27662767error:2768if (dck != NULL)2769spa_keystore_dsl_key_rele(spa, dck, FTAG);2770abd_return_buf(abd, buf, datalen);2771return (ret);2772}27732774int2775spa_do_crypt_mac_abd(boolean_t generate, spa_t *spa, uint64_t dsobj, abd_t *abd,2776uint_t datalen, uint8_t *mac)2777{2778int ret;2779dsl_crypto_key_t *dck = NULL;2780uint8_t *buf = abd_borrow_buf_copy(abd, datalen);2781uint8_t digestbuf[ZIO_DATA_MAC_LEN];27822783/* look up the key from the spa's keystore */2784ret = spa_keystore_lookup_key(spa, dsobj, FTAG, &dck);2785if (ret != 0)2786goto error;27872788/* perform the hmac */2789ret = zio_crypt_do_hmac(&dck->dck_key, buf, datalen,2790digestbuf, ZIO_DATA_MAC_LEN);2791if (ret != 0)2792goto error;27932794abd_return_buf(abd, buf, datalen);2795spa_keystore_dsl_key_rele(spa, dck, FTAG);27962797/*2798* Truncate and fill in mac buffer if we were asked to generate a MAC.2799* Otherwise verify that the MAC matched what we expected.2800*/2801if (generate) {2802memcpy(mac, digestbuf, ZIO_DATA_MAC_LEN);2803return (0);2804}28052806if (memcmp(digestbuf, mac, ZIO_DATA_MAC_LEN) != 0)2807return (SET_ERROR(ECKSUM));28082809return (0);28102811error:2812if (dck != NULL)2813spa_keystore_dsl_key_rele(spa, dck, FTAG);2814abd_return_buf(abd, buf, datalen);2815return (ret);2816}28172818/*2819* This function serves as a multiplexer for encryption and decryption of2820* all blocks (except the L2ARC). For encryption, it will populate the IV,2821* salt, MAC, and cabd (the ciphertext). On decryption it will simply use2822* these fields to populate pabd (the plaintext).2823*/2824int2825spa_do_crypt_abd(boolean_t encrypt, spa_t *spa, const zbookmark_phys_t *zb,2826dmu_object_type_t ot, boolean_t dedup, boolean_t bswap, uint8_t *salt,2827uint8_t *iv, uint8_t *mac, uint_t datalen, abd_t *pabd, abd_t *cabd,2828boolean_t *no_crypt)2829{2830int ret;2831dsl_crypto_key_t *dck = NULL;2832uint8_t *plainbuf = NULL, *cipherbuf = NULL;28332834ASSERT(spa_feature_is_active(spa, SPA_FEATURE_ENCRYPTION));28352836/* look up the key from the spa's keystore */2837ret = spa_keystore_lookup_key(spa, zb->zb_objset, FTAG, &dck);2838if (ret != 0) {2839ret = SET_ERROR(EACCES);2840return (ret);2841}28422843if (encrypt) {2844plainbuf = abd_borrow_buf_copy(pabd, datalen);2845cipherbuf = abd_borrow_buf(cabd, datalen);2846} else {2847plainbuf = abd_borrow_buf(pabd, datalen);2848cipherbuf = abd_borrow_buf_copy(cabd, datalen);2849}28502851/*2852* Both encryption and decryption functions need a salt for key2853* generation and an IV. When encrypting a non-dedup block, we2854* generate the salt and IV randomly to be stored by the caller. Dedup2855* blocks perform a (more expensive) HMAC of the plaintext to obtain2856* the salt and the IV. ZIL blocks have their salt and IV generated2857* at allocation time in zio_alloc_zil(). On decryption, we simply use2858* the provided values.2859*/2860if (encrypt && ot != DMU_OT_INTENT_LOG && !dedup) {2861ret = zio_crypt_key_get_salt(&dck->dck_key, salt);2862if (ret != 0)2863goto error;28642865ret = zio_crypt_generate_iv(iv);2866if (ret != 0)2867goto error;2868} else if (encrypt && dedup) {2869ret = zio_crypt_generate_iv_salt_dedup(&dck->dck_key,2870plainbuf, datalen, iv, salt);2871if (ret != 0)2872goto error;2873}28742875/* call lower level function to perform encryption / decryption */2876ret = zio_do_crypt_data(encrypt, &dck->dck_key, ot, bswap, salt, iv,2877mac, datalen, plainbuf, cipherbuf, no_crypt);28782879/*2880* Handle injected decryption faults. Unfortunately, we cannot inject2881* faults for dnode blocks because we might trigger the panic in2882* dbuf_prepare_encrypted_dnode_leaf(), which exists because syncing2883* context is not prepared to handle malicious decryption failures.2884*/2885if (zio_injection_enabled && !encrypt && ot != DMU_OT_DNODE && ret == 0)2886ret = zio_handle_decrypt_injection(spa, zb, ot, ECKSUM);2887if (ret != 0)2888goto error;28892890if (encrypt) {2891abd_return_buf(pabd, plainbuf, datalen);2892abd_return_buf_copy(cabd, cipherbuf, datalen);2893} else {2894abd_return_buf_copy(pabd, plainbuf, datalen);2895abd_return_buf(cabd, cipherbuf, datalen);2896}28972898spa_keystore_dsl_key_rele(spa, dck, FTAG);28992900return (0);29012902error:2903if (encrypt) {2904/* zero out any state we might have changed while encrypting */2905memset(salt, 0, ZIO_DATA_SALT_LEN);2906memset(iv, 0, ZIO_DATA_IV_LEN);2907memset(mac, 0, ZIO_DATA_MAC_LEN);2908abd_return_buf(pabd, plainbuf, datalen);2909abd_return_buf_copy(cabd, cipherbuf, datalen);2910} else {2911abd_return_buf_copy(pabd, plainbuf, datalen);2912abd_return_buf(cabd, cipherbuf, datalen);2913}29142915spa_keystore_dsl_key_rele(spa, dck, FTAG);29162917return (ret);2918}29192920ZFS_MODULE_PARAM(zfs, zfs_, disable_ivset_guid_check, INT, ZMOD_RW,2921"Set to allow raw receives without IVset guids");292229232924