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