Skip to content

Commit 5c38029

Browse files
authored
zdb: add ZFS_KEYFORMAT_RAW support for -K option
This change adds support for ZFS_KEYFORMAT_RAW to zdb_derive_key in zdb.c. The implementation reads the raw key from the file specified by the -K option which is consistent with how raw keys are handled in the other parts of ZFS, along with a check to ensure that the keyfile doesn't have too many bytes. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Patrick Xia <[email protected]> Closes #17783
1 parent 26b0f56 commit 5c38029

File tree

4 files changed

+100
-4
lines changed

4 files changed

+100
-4
lines changed

cmd/zdb/zdb.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,6 +3301,7 @@ zdb_derive_key(dsl_dir_t *dd, uint8_t *key_out)
33013301
uint64_t keyformat, salt, iters;
33023302
int i;
33033303
unsigned char c;
3304+
FILE *f;
33043305

33053306
VERIFY0(zap_lookup(dd->dd_pool->dp_meta_objset, dd->dd_crypto_obj,
33063307
zfs_prop_to_name(ZFS_PROP_KEYFORMAT), sizeof (uint64_t),
@@ -3333,6 +3334,25 @@ zdb_derive_key(dsl_dir_t *dd, uint8_t *key_out)
33333334

33343335
break;
33353336

3337+
case ZFS_KEYFORMAT_RAW:
3338+
if ((f = fopen(key_material, "r")) == NULL)
3339+
return (B_FALSE);
3340+
3341+
if (fread(key_out, 1, WRAPPING_KEY_LEN, f) !=
3342+
WRAPPING_KEY_LEN) {
3343+
(void) fclose(f);
3344+
return (B_FALSE);
3345+
}
3346+
3347+
/* Check the key length */
3348+
if (fgetc(f) != EOF) {
3349+
(void) fclose(f);
3350+
return (B_FALSE);
3351+
}
3352+
3353+
(void) fclose(f);
3354+
break;
3355+
33363356
default:
33373357
fatal("no support for key format %u\n",
33383358
(unsigned int) keyformat);

tests/runfiles/common.run

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,10 @@ tags = ['functional', 'cli_root', 'zinject']
168168
tests = ['zdb_002_pos', 'zdb_003_pos', 'zdb_004_pos', 'zdb_005_pos',
169169
'zdb_006_pos', 'zdb_args_neg', 'zdb_args_pos',
170170
'zdb_block_size_histogram', 'zdb_checksum', 'zdb_decompress',
171-
'zdb_display_block', 'zdb_encrypted', 'zdb_label_checksum',
172-
'zdb_object_range_neg', 'zdb_object_range_pos', 'zdb_objset_id',
173-
'zdb_decompress_zstd', 'zdb_recover', 'zdb_recover_2', 'zdb_backup',
174-
'zdb_tunables']
171+
'zdb_display_block', 'zdb_encrypted', 'zdb_encrypted_raw',
172+
'zdb_label_checksum', 'zdb_object_range_neg', 'zdb_object_range_pos',
173+
'zdb_objset_id', 'zdb_decompress_zstd', 'zdb_recover', 'zdb_recover_2',
174+
'zdb_backup', 'zdb_tunables']
175175
pre =
176176
post =
177177
tags = ['functional', 'cli_root', 'zdb']

tests/zfs-tests/tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,7 @@ nobase_dist_datadir_zfs_tests_tests_SCRIPTS += \
640640
functional/cli_root/zdb/zdb_decompress_zstd.ksh \
641641
functional/cli_root/zdb/zdb_display_block.ksh \
642642
functional/cli_root/zdb/zdb_encrypted.ksh \
643+
functional/cli_root/zdb/zdb_encrypted_raw.ksh \
643644
functional/cli_root/zdb/zdb_label_checksum.ksh \
644645
functional/cli_root/zdb/zdb_object_range_neg.ksh \
645646
functional/cli_root/zdb/zdb_object_range_pos.ksh \
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/ksh -p
2+
# SPDX-License-Identifier: CDDL-1.0
3+
#
4+
# CDDL HEADER START
5+
#
6+
# This file and its contents are supplied under the terms of the
7+
# Common Development and Distribution License ("CDDL"), version 1.0.
8+
# You may only use this file in accordance with the terms of version
9+
# 1.0 of the CDDL.
10+
#
11+
# A full copy of the text of the CDDL should have accompanied this
12+
# source. A copy of the CDDL is also available via the Internet at
13+
# http://www.illumos.org/license/CDDL.
14+
#
15+
# CDDL HEADER END
16+
#
17+
18+
#
19+
# Copyright (c) 2023, Klara Inc.
20+
#
21+
22+
. $STF_SUITE/include/libtest.shlib
23+
. $STF_SUITE/tests/functional/cli_root/zfs_load-key/zfs_load-key_common.kshlib
24+
25+
#
26+
# DESCRIPTION:
27+
# 'zdb -K ...' should enable reading from a raw-encrypted dataset
28+
#
29+
# STRATEGY:
30+
# 1. Create an encrypted dataset
31+
# 2. Write some data to a file
32+
# 3. Run zdb -dddd on the file, confirm it can't be read
33+
# 4. Run zdb -K ... -ddddd on the file, confirm it can be read
34+
#
35+
36+
verify_runnable "both"
37+
38+
dataset="$TESTPOOL/$TESTFS2"
39+
file="$TESTDIR2/somefile"
40+
keyfile="$TEST_BASE_DIR/keyfile"
41+
42+
function cleanup
43+
{
44+
datasetexists "$dataset" && destroy_dataset "$dataset" -f
45+
rm -f "$keyfile"
46+
default_cleanup_noexit
47+
}
48+
49+
log_onexit cleanup
50+
51+
log_must default_setup_noexit $DISKS
52+
53+
log_assert "'zdb -K' should enable reading from a raw-encrypted dataset"
54+
55+
# The key must be 32 bytes long.
56+
echo -n "$RAWKEY" > "$keyfile"
57+
58+
log_must zfs create -o mountpoint="$TESTDIR2" \
59+
-o encryption=on -o keyformat=raw -o keylocation="file://$keyfile" \
60+
"$dataset"
61+
62+
echo 'my great encrypted text' > "$file"
63+
64+
typeset -i obj=$(ls -i "$file" | cut -d' ' -f1)
65+
typeset -i size=$(wc -c < "$file")
66+
67+
log_note "test file $file is objid $obj, size $size"
68+
69+
sync_pool "$TESTPOOL" true
70+
71+
log_must eval "zdb -dddd $dataset $obj | grep -q 'object encrypted'"
72+
73+
log_must eval "zdb -K $keyfile -dddd $dataset $obj | grep -q 'size\s$size$'"
74+
75+
log_pass "'zdb -K' enables reading from a raw-encrypted dataset"

0 commit comments

Comments
 (0)