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