1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2018 by Datto Inc. All rights reserved.
19#
20
21. $STF_SUITE/tests/functional/rsend/rsend.kshlib
22
23#
24# DESCRIPTION:
25# Verify that zfs properly handles encryption properties when receiving
26# send streams.
27#
28# STRATEGY:
29# 1. Create a few unencrypted and encrypted test datasets with some data
30# 2. Take snapshots of these datasets in preparation for sending
31# 3. Verify that 'zfs recv -o keylocation=prompt' fails
32# 4. Verify that 'zfs recv -x <encryption prop>' fails on a raw send stream
33# 5. Verify that encryption properties cannot be changed on incrementals
34# 6. Verify that a simple send can be received as an encryption root
35# 7. Verify that an unencrypted props send can be received as an
36#    encryption root
37# 8. Verify that an unencrypted recursive send can be received as an
38#    encryption root
39# 9. Verify that an unencrypted props send can be received as an
40#    encryption child
41# 10. Verify that an unencrypted recursive send can be received as an
42#     encryption child
43#
44
45verify_runnable "both"
46
47function cleanup
48{
49	destroy_dataset $TESTPOOL/recv "-r"
50	destroy_dataset $TESTPOOL/crypt "-r"
51	destroy_dataset $TESTPOOL/ds "-r"
52	[[ -f $sendfile ]] && log_must rm $sendfile
53	[[ -f $keyfile ]] && log_must rm $keyfile
54}
55log_onexit cleanup
56
57log_assert "'zfs recv' must properly handle encryption properties"
58
59typeset keyfile=/$TESTPOOL/pkey
60typeset sendfile=/$TESTPOOL/sendfile
61typeset snap=$TESTPOOL/ds@snap
62typeset esnap=$TESTPOOL/crypt@snap1
63typeset esnap2=$TESTPOOL/crypt@snap2
64
65log_must eval "echo 'password' > $keyfile"
66
67log_must zfs create $TESTPOOL/ds
68log_must zfs create $TESTPOOL/ds/ds1
69
70log_must zfs create -o encryption=on -o keyformat=passphrase \
71	-o keylocation=file://$keyfile $TESTPOOL/crypt
72log_must zfs create $TESTPOOL/crypt/ds1
73log_must zfs create -o keyformat=passphrase -o keylocation=file://$keyfile \
74	$TESTPOOL/crypt/ds2
75
76log_must mkfile 1M /$TESTPOOL/ds/$TESTFILE0
77log_must cp /$TESTPOOL/ds/$TESTFILE0 /$TESTPOOL/crypt/$TESTFILE0
78typeset cksum=$(md5sum /$TESTPOOL/ds/$TESTFILE0 | awk '{  print $1 }')
79
80log_must zfs snap -r $snap
81log_must zfs snap -r $esnap
82log_must zfs snap -r $esnap2
83
84# Embedded data is incompatible with encrypted datasets, so we cannot
85# allow embedded streams to be received.
86log_note "Must not be able to receive an embedded stream as encrypted"
87log_mustnot eval "zfs send -e $TESTPOOL/crypt/ds1 | zfs recv $TESTPOOL/recv"
88
89# We currently don't have an elegant and secure way to pass the passphrase
90# in via prompt because the send stream itself is using stdin.
91log_note "Must not be able to use 'keylocation=prompt' on receive"
92log_must eval "zfs send $snap > $sendfile"
93log_mustnot eval "zfs recv -o encryption=on -o keyformat=passphrase" \
94	"$TESTPOOL/recv < $sendfile"
95log_mustnot eval "zfs recv -o encryption=on -o keyformat=passphrase" \
96	"-o keylocation=prompt $TESTPOOL/recv < $sendfile"
97
98# Raw sends do not have access to the decrypted data so we cannot override
99# the encryption settings without losing the data.
100log_note "Must not be able to disable encryption properties on raw send"
101log_must eval "zfs send -w $esnap > $sendfile"
102log_mustnot eval "zfs recv -x encryption $TESTPOOL/recv < $sendfile"
103log_mustnot eval "zfs recv -x keyformat $TESTPOOL/recv < $sendfile"
104log_mustnot eval "zfs recv -x pbkdf2iters $TESTPOOL/recv < $sendfile"
105
106# Encryption properties are set upon creating the dataset. Changing them
107# afterwards requires using 'zfs change-key' or an update from a raw send.
108log_note "Must not be able to change encryption properties on incrementals"
109log_must eval "zfs send $esnap | zfs recv -o encryption=on" \
110	"-o keyformat=passphrase -o keylocation=file://$keyfile $TESTPOOL/recv"
111log_mustnot eval "zfs send -i $esnap $esnap2 |" \
112	"zfs recv -o encryption=aes-128-ccm $TESTPOOL/recv"
113log_mustnot eval "zfs send -i $esnap $esnap2 |" \
114	"zfs recv -o keyformat=hex $TESTPOOL/recv"
115log_mustnot eval "zfs send -i $esnap $esnap2 |" \
116	"zfs recv -o pbkdf2iters=100k $TESTPOOL/recv"
117log_must zfs destroy -r $TESTPOOL/recv
118
119# Test that we can receive a simple stream as an encryption root.
120log_note "Must be able to receive stream as encryption root"
121ds=$TESTPOOL/recv
122log_must eval "zfs send $snap > $sendfile"
123log_must eval "zfs recv -o encryption=on -o keyformat=passphrase" \
124	"-o keylocation=file://$keyfile $ds < $sendfile"
125log_must test "$(get_prop 'encryption' $ds)" == "aes-256-ccm"
126log_must test "$(get_prop 'encryptionroot' $ds)" == "$ds"
127log_must test "$(get_prop 'keyformat' $ds)" == "passphrase"
128log_must test "$(get_prop 'keylocation' $ds)" == "file://$keyfile"
129log_must test "$(get_prop 'mounted' $ds)" == "yes"
130recv_cksum=$(md5sum /$ds/$TESTFILE0 | awk '{ print $1 }')
131log_must test "$recv_cksum" == "$cksum"
132log_must zfs destroy -r $ds
133
134# Test that we can override encryption properties on a properties stream
135# of an unencrypted dataset, turning it into an encryption root.
136log_note "Must be able to receive stream with props as encryption root"
137ds=$TESTPOOL/recv
138log_must eval "zfs send -p $snap > $sendfile"
139log_must eval "zfs recv -o encryption=on -o keyformat=passphrase" \
140	"-o keylocation=file://$keyfile $ds < $sendfile"
141log_must test "$(get_prop 'encryption' $ds)" == "aes-256-ccm"
142log_must test "$(get_prop 'encryptionroot' $ds)" == "$ds"
143log_must test "$(get_prop 'keyformat' $ds)" == "passphrase"
144log_must test "$(get_prop 'keylocation' $ds)" == "file://$keyfile"
145log_must test "$(get_prop 'mounted' $ds)" == "yes"
146recv_cksum=$(md5sum /$ds/$TESTFILE0 | awk '{ print $1 }')
147log_must test "$recv_cksum" == "$cksum"
148log_must zfs destroy -r $ds
149
150# Test that we can override encryption properties on a recursive stream
151# of an unencrypted dataset, turning it into an encryption root. The root
152# dataset of the stream should become an encryption root with all children
153# inheriting from it.
154log_note "Must be able to receive recursive stream as encryption root"
155ds=$TESTPOOL/recv
156log_must eval "zfs send -R $snap > $sendfile"
157log_must eval "zfs recv -o encryption=on -o keyformat=passphrase" \
158	"-o keylocation=file://$keyfile $ds < $sendfile"
159log_must test "$(get_prop 'encryption' $ds)" == "aes-256-ccm"
160log_must test "$(get_prop 'encryptionroot' $ds)" == "$ds"
161log_must test "$(get_prop 'keyformat' $ds)" == "passphrase"
162log_must test "$(get_prop 'keylocation' $ds)" == "file://$keyfile"
163log_must test "$(get_prop 'mounted' $ds)" == "yes"
164recv_cksum=$(md5sum /$ds/$TESTFILE0 | awk '{ print $1 }')
165log_must test "$recv_cksum" == "$cksum"
166log_must zfs destroy -r $ds
167
168# Test that we can override an unencrypted properties stream's encryption
169# settings, receiving it as an encrypted child.
170log_note "Must be able to receive stream with props to encrypted child"
171ds=$TESTPOOL/crypt/recv
172log_must eval "zfs send -p $snap > $sendfile"
173log_must eval "zfs recv -x encryption $ds < $sendfile"
174log_must test "$(get_prop 'encryptionroot' $ds)" == "$TESTPOOL/crypt"
175log_must test "$(get_prop 'encryption' $ds)" == "aes-256-ccm"
176log_must test "$(get_prop 'keyformat' $ds)" == "passphrase"
177log_must test "$(get_prop 'mounted' $ds)" == "yes"
178recv_cksum=$(md5sum /$ds/$TESTFILE0 | awk '{ print $1 }')
179log_must test "$recv_cksum" == "$cksum"
180log_must zfs destroy -r $ds
181
182# Test that we can override an unencrypted recursive stream's encryption
183# settings, receiving all datasets as encrypted children.
184log_note "Must be able to receive recursive stream to encrypted child"
185ds=$TESTPOOL/crypt/recv
186log_must eval "zfs send -R $snap > $sendfile"
187log_must eval "zfs recv -x encryption $ds < $sendfile"
188log_must test "$(get_prop 'encryptionroot' $ds)" == "$TESTPOOL/crypt"
189log_must test "$(get_prop 'encryption' $ds)" == "aes-256-ccm"
190log_must test "$(get_prop 'keyformat' $ds)" == "passphrase"
191log_must test "$(get_prop 'mounted' $ds)" == "yes"
192recv_cksum=$(md5sum /$ds/$TESTFILE0 | awk '{ print $1 }')
193log_must test "$recv_cksum" == "$cksum"
194log_must zfs destroy -r $ds
195
196# Check that we haven't printed the key to the zpool history log
197log_mustnot eval "zpool history -i | grep -q 'wkeydata'"
198
199log_pass "'zfs recv' properly handles encryption properties"
200