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