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