xfstests: verify fscrypt-encrypted contents and filenames
Eric Biggers
maintainerv2 cover xfstests: verify fscrypt-encrypted contents and filenames
Hello,
This series adds xfstests which verify that encrypted contents and filenames on ext4 and f2fs are actually correct, i.e. that the encryption uses the correct algorithms, keys, IVs, and padding amounts. The new tests work by creating encrypted files, unmounting the filesystem, reading the ciphertext from disk using dd and debugfs or dump.f2fs, and then comparing it against ciphertext computed independently by a new test program that implements the same algorithms.
These tests are important because:
- The whole point of file encryption is that the files are actually encrypted correctly on-disk. Except for generic/399, current xfstests only tests the filesystem semantics, not the actual encryption. generic/399 only tests for incompressibility of encrypted file contents using one particular encryption setting, which isn't much.
- fscrypt now supports 4 main combinations of encryption settings, rather than 1 as it did originally. This may be doubled to 8 soon
(https://patchwork.kernel.org/patch/10952059/), and support for ext4 encryption with sub-page blocks is in progress too. We should test all settings. And without tests, even if the initial implementation is correct, breakage in one specific setting could go undetected.
- Though Linux's crypto API has self-tests, these only test the algorithms themselves, not how they are used, e.g. by fscrypt.
Patch 1 is a cleanup patch. Patches 2-4 add the common helpers for ciphertext verification tests. Patches 5-7 add the actual tests.
For ext4 these tests require e2fsprogs v1.45.1 or later, for a recent debugfs fix. For f2fs they require f2fs-tools built from the "dev" branch (which should eventually become v1.13.0), for a dump.f2fs fix. The tests check for the presence of these fixes before they run.
This series can also be retrieved from git at
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/xfstests-dev.git
branch "ciphertext-verification".
I also have patches on top of this series which verify the ciphertext produced from v2 encryption policies, which are proposed by my kernel patch series "fscrypt: key management improvements"
(https://patchwork.kernel.org/cover/10951999/). v2 encryption policies will use a different key derivation function, and thus their ciphertext will be different. These additional patches can be found at branch "fscrypt-key-mgmt-improvements" of my git repo above. But I've arranged things such that this shorter series can be applied earlier, to test what's in the kernel now.
Changed since v1:
- Drop the _require_get_encpolicy() helper function.
- Rename some functions:
- _get_on_disk_filename() => _get_ciphertext_filename()
- _get_file_block_list() => _get_ciphertext_block_list()
- _dump_file_blocks() => _dump_ciphertext_blocks()
- Mention minimum e2fsprogs and f2fs-tools versions in comments.
- Use _fail() instead of _notrun() when support was already checked.
- Minor cleanups to fscrypt-crypt-util.
Eric Biggers (7):
common/encrypt: introduce helpers for set_encpolicy and get_encpolicy
fscrypt-crypt-util: add utility for reproducing fscrypt encrypted data common/encrypt: support requiring other encryption settings common/encrypt: add helper for ciphertext verification tests
generic: verify ciphertext of v1 encryption policies with AES-256
generic: verify ciphertext of v1 encryption policies with AES-128
generic: verify ciphertext of v1 encryption policies with Adiantum
.gitignore | 1 + common/encrypt | 479 ++++++++++- src/Makefile | 3 +- src/fscrypt-crypt-util.c | 1633 ++++++++++++++++++++++++++++++++++++++ tests/ext4/024 | 3 +- tests/generic/395 | 26 +- tests/generic/395.out | 2 +- tests/generic/396 | 15 +- tests/generic/397 | 3 +- tests/generic/398 | 5 +- tests/generic/399 | 3 +- tests/generic/419 | 3 +- tests/generic/421 | 3 +- tests/generic/429 | 3 +- tests/generic/435 | 3 +- tests/generic/440 | 5 +- tests/generic/700 | 41 + tests/generic/700.out | 5 + tests/generic/701 | 41 + tests/generic/701.out | 5 + tests/generic/702 | 43 + tests/generic/702.out | 10 + tests/generic/group | 3 + 23 files changed, 2292 insertions(+), 46 deletions(-) create mode 100644 src/fscrypt-crypt-util.c create mode 100755 tests/generic/700 create mode 100644 tests/generic/700.out create mode 100755 tests/generic/701 create mode 100644 tests/generic/701.out create mode 100755 tests/generic/702 create mode 100644 tests/generic/702.out
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Eric Biggers
maintainerPATCH v2 1/7 common/encrypt: introduce helpers for set_encpolicy and get_encpolicy
From: Eric Biggers <[email protected]>
For conciseness in tests, add helper functions that wrap the xfs_io commands 'set_encpolicy' and 'get_encpolicy'. Then update all encryption tests to use them.
--- common/encrypt | 29 +++++++++++++++++++++++++++-- tests/ext4/024 | 3 +-- tests/generic/395 | 26 ++++++++++++-------------- tests/generic/395.out | 2 +- tests/generic/396 | 15 +++++++-------- tests/generic/397 | 3 +-- tests/generic/398 | 5 ++--- tests/generic/399 | 3 +-- tests/generic/419 | 3 +-- tests/generic/421 | 3 +-- tests/generic/429 | 3 +-- tests/generic/435 | 3 +-- tests/generic/440 | 5 ++--- 13 files changed, 58 insertions(+), 45 deletions(-)
common/encrypt +27 -2
@@ -38,8 +38,7 @@ _require_scratch_encryption() # presence of /sys/fs/ext4/features/encryption, but this is broken on # some older kernels and is ext4-specific anyway.) mkdir $SCRATCH_MNT/tmpdir - if $XFS_IO_PROG -c set_encpolicy $SCRATCH_MNT/tmpdir \ - 2>&1 >>$seqres.full | \ + if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \ egrep -q 'Inappropriate ioctl for device|Operation not supported' then _notrun "kernel does not support $FSTYP encryption"
@@ -175,3 +174,29 @@ _revoke_encryption_key() local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc) $KEYCTL_PROG revoke $keyid >>$seqres.full } + +# Set an encryption policy on the specified directory. +_set_encpolicy() +{ + local dir=$1 + shift + + $XFS_IO_PROG -c "set_encpolicy $*" "$dir" +} + +_user_do_set_encpolicy() +{ + local dir=$1 + shift + + _user_do "$XFS_IO_PROG -c \"set_encpolicy $*\" \"$dir\"" +} + +# Display the specified file or directory's encryption policy. +_get_encpolicy() +{ + local file=$1 + shift + + $XFS_IO_PROG -c "get_encpolicy $*" "$file" +}
tests/ext4/024 +1 -2
@@ -33,7 +33,6 @@ rm -f $seqres.full _supported_fs ext4 _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _require_command "$KEYCTL_PROG" keyctl _new_session_keyring
@@ -55,7 +54,7 @@ _scratch_mkfs_encrypted &>>$seqres.full _scratch_mount mkdir $SCRATCH_MNT/edir keydesc=$(_generate_encryption_key) -$XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir +_set_encpolicy $SCRATCH_MNT/edir $keydesc echo foo > $SCRATCH_MNT/edir/file inum=$(stat -c '%i' $SCRATCH_MNT/edir/file) _scratch_unmount
tests/generic/395 +12 -14
@@ -34,7 +34,6 @@ _supported_fs generic _supported_os Linux _require_scratch_encryption _require_xfs_io_command "get_encpolicy" -_require_xfs_io_command "set_encpolicy" _require_user _scratch_mkfs_encrypted &>> $seqres.full
@@ -45,7 +44,7 @@ check_no_policy() # When a file is unencrypted, FS_IOC_GET_ENCRYPTION_POLICY currently # fails with ENOENT on ext4 but with ENODATA on f2fs. TODO: it's # planned to consistently use ENODATA. For now this test accepts both. - $XFS_IO_PROG -c "get_encpolicy" $1 |& + _get_encpolicy $1 |& sed -e 's/No such file or directory/No data available/' }
@@ -54,25 +53,25 @@ empty_dir=$SCRATCH_MNT/empty_dir echo -e "\n*** Setting encryption policy on empty directory ***" mkdir $empty_dir check_no_policy $empty_dir |& _filter_scratch -$XFS_IO_PROG -c "set_encpolicy 0000111122223333" $empty_dir -$XFS_IO_PROG -c "get_encpolicy" $empty_dir | _filter_scratch +_set_encpolicy $empty_dir 0000111122223333 +_get_encpolicy $empty_dir | _filter_scratch # Should be able to set the same policy again, but not a different one. # TODO: the error code for "already has a different policy" is planned to switch # from EINVAL to EEXIST. For now this test accepts both. echo -e "\n*** Setting encryption policy again ***" -$XFS_IO_PROG -c "set_encpolicy 0000111122223333" $empty_dir -$XFS_IO_PROG -c "get_encpolicy" $empty_dir | _filter_scratch -$XFS_IO_PROG -c "set_encpolicy 4444555566667777" $empty_dir |& \ +_set_encpolicy $empty_dir 0000111122223333 +_get_encpolicy $empty_dir | _filter_scratch +_set_encpolicy $empty_dir 4444555566667777 |& \ _filter_scratch | sed -e 's/Invalid argument/File exists/' -$XFS_IO_PROG -c "get_encpolicy" $empty_dir | _filter_scratch +_get_encpolicy $empty_dir | _filter_scratch # Should *not* be able to set an encryption policy on a nonempty directory nonempty_dir=$SCRATCH_MNT/nonempty_dir echo -e "\n*** Setting encryption policy on nonempty directory ***" mkdir $nonempty_dir touch $nonempty_dir/file -$XFS_IO_PROG -c "set_encpolicy" $nonempty_dir |& _filter_scratch +_set_encpolicy $nonempty_dir |& _filter_scratch check_no_policy $nonempty_dir |& _filter_scratch # Should *not* be able to set an encryption policy on a nondirectory file, even
@@ -83,7 +82,7 @@ check_no_policy $nonempty_dir |& _filter_scratch nondirectory=$SCRATCH_MNT/nondirectory echo -e "\n*** Setting encryption policy on nondirectory ***" touch $nondirectory -$XFS_IO_PROG -c "set_encpolicy" $nondirectory |& \ +_set_encpolicy $nondirectory |& \ _filter_scratch | sed -e 's/Invalid argument/Not a directory/' check_no_policy $nondirectory |& _filter_scratch
@@ -93,8 +92,7 @@ check_no_policy $nondirectory |& _filter_scratch unauthorized_dir=$SCRATCH_MNT/unauthorized_dir echo -e "\n*** Setting encryption policy on another user's directory ***" mkdir $unauthorized_dir -su $qa_user -c "$XFS_IO_PROG -c \"set_encpolicy\" $unauthorized_dir" |& \ - _filter_scratch +_user_do_set_encpolicy $unauthorized_dir |& _filter_scratch check_no_policy $unauthorized_dir |& _filter_scratch # Should *not* be able to set an encryption policy on a directory on a
@@ -104,12 +102,12 @@ check_no_policy $unauthorized_dir |& _filter_scratch echo -e "\n*** Setting encryption policy on readonly filesystem ***" mkdir $SCRATCH_MNT/ro_dir $SCRATCH_MNT/ro_bind_mnt _scratch_remount ro -$XFS_IO_PROG -c "set_encpolicy" $SCRATCH_MNT/ro_dir |& _filter_scratch +_set_encpolicy $SCRATCH_MNT/ro_dir |& _filter_scratch check_no_policy $SCRATCH_MNT/ro_dir |& _filter_scratch _scratch_remount rw mount --bind $SCRATCH_MNT $SCRATCH_MNT/ro_bind_mnt mount -o remount,ro,bind $SCRATCH_MNT/ro_bind_mnt -$XFS_IO_PROG -c "set_encpolicy" $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch +_set_encpolicy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch check_no_policy $SCRATCH_MNT/ro_bind_mnt/ro_dir |& _filter_scratch umount $SCRATCH_MNT/ro_bind_mnt
tests/generic/395.out +1 -1
@@ -33,7 +33,7 @@ SCRATCH_MNT/nondirectory: failed to set encryption policy: Not a directory SCRATCH_MNT/nondirectory: failed to get encryption policy: No data available *** Setting encryption policy on another user's directory *** -SCRATCH_MNT/unauthorized_dir: failed to set encryption policy: Permission denied +Permission denied SCRATCH_MNT/unauthorized_dir: failed to get encryption policy: No data available *** Setting encryption policy on readonly filesystem ***
tests/generic/396 +7 -8
@@ -34,7 +34,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _scratch_mkfs_encrypted &>> $seqres.full _scratch_mount
@@ -42,23 +41,23 @@ dir=$SCRATCH_MNT/dir mkdir $dir echo -e "\n*** Invalid contents encryption mode ***" -$XFS_IO_PROG -c "set_encpolicy -c 0xFF" $dir |& _filter_scratch +_set_encpolicy $dir -c 0xFF |& _filter_scratch echo -e "\n*** Invalid filenames encryption mode ***" -$XFS_IO_PROG -c "set_encpolicy -n 0xFF" $dir |& _filter_scratch +_set_encpolicy $dir -n 0xFF |& _filter_scratch echo -e "\n*** Invalid flags ***" -$XFS_IO_PROG -c "set_encpolicy -f 0xFF" $dir |& _filter_scratch +_set_encpolicy $dir -f 0xFF |& _filter_scratch echo -e "\n*** Invalid policy version ***" -$XFS_IO_PROG -c "set_encpolicy -v 0xFF" $dir |& _filter_scratch +_set_encpolicy $dir -v 0xFF |& _filter_scratch # Currently, the only supported combination of modes is AES-256-XTS for contents # and AES-256-CTS for filenames. Nothing else should be accepted. echo -e "\n*** Invalid combinations of modes ***" -$XFS_IO_PROG -c "set_encpolicy -c AES-256-CTS -n AES-256-CTS" $dir |& _filter_scratch -$XFS_IO_PROG -c "set_encpolicy -c AES-256-CTS -n AES-256-XTS" $dir |& _filter_scratch -$XFS_IO_PROG -c "set_encpolicy -c AES-256-XTS -n AES-256-XTS" $dir |& _filter_scratch +_set_encpolicy $dir -c AES-256-CTS -n AES-256-CTS |& _filter_scratch +_set_encpolicy $dir -c AES-256-CTS -n AES-256-XTS |& _filter_scratch +_set_encpolicy $dir -c AES-256-XTS -n AES-256-XTS |& _filter_scratch # success, all done status=0
tests/generic/397 +1 -2
@@ -37,7 +37,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _require_command "$KEYCTL_PROG" keyctl _new_session_keyring
@@ -47,7 +46,7 @@ _scratch_mount mkdir $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir keydesc=$(_generate_encryption_key) -$XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir +_set_encpolicy $SCRATCH_MNT/edir $keydesc for dir in $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir; do touch $dir/empty > /dev/null $XFS_IO_PROG -t -f -c "pwrite 0 4k" $dir/a > /dev/null
tests/generic/398 +2 -3
@@ -56,7 +56,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _requires_renameat2 _new_session_keyring
@@ -71,8 +70,8 @@ udir=$SCRATCH_MNT/udir mkdir $edir1 $edir2 $udir keydesc1=$(_generate_encryption_key) keydesc2=$(_generate_encryption_key) -$XFS_IO_PROG -c "set_encpolicy $keydesc1" $edir1 -$XFS_IO_PROG -c "set_encpolicy $keydesc2" $edir2 +_set_encpolicy $edir1 $keydesc1 +_set_encpolicy $edir2 $keydesc2 touch $edir1/efile1 touch $edir2/efile2 touch $udir/ufile
tests/generic/399 +1 -2
@@ -43,7 +43,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _require_command "$XZ_PROG" xz _require_command "$KEYCTL_PROG" keyctl
@@ -64,7 +63,7 @@ _scratch_mount keydesc=$(_generate_encryption_key) mkdir $SCRATCH_MNT/encrypted_dir -$XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/encrypted_dir +_set_encpolicy $SCRATCH_MNT/encrypted_dir $keydesc # Create the "same" symlink in two different directories. # Later we'll check both the name and target of the symlink.
tests/generic/419 +1 -2
@@ -38,7 +38,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _require_command "$KEYCTL_PROG" keyctl _requires_renameat2
@@ -49,7 +48,7 @@ _scratch_mount mkdir $SCRATCH_MNT/edir keydesc=$(_generate_encryption_key) -$XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir +_set_encpolicy $SCRATCH_MNT/edir $keydesc echo a > $SCRATCH_MNT/edir/a echo b > $SCRATCH_MNT/edir/b _unlink_encryption_key $keydesc
tests/generic/421 +1 -2
@@ -35,7 +35,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _require_command "$KEYCTL_PROG" keyctl _new_session_keyring
@@ -53,7 +52,7 @@ slice=2 rm -rf $dir mkdir $dir keydesc=$(_generate_encryption_key) -$XFS_IO_PROG -c "set_encpolicy $keydesc" $dir +_set_encpolicy $dir $keydesc $XFS_IO_PROG -f $file -c "pwrite 0 $((nproc*slice))M" -c "fsync" > /dev/null # Create processes to read from the encrypted file. Use fadvise to wipe the
tests/generic/429 +1 -2
@@ -46,7 +46,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _require_command "$KEYCTL_PROG" keyctl _require_test_program "t_encrypted_d_revalidate"
@@ -58,7 +57,7 @@ keydesc=$(_generate_key_descriptor) raw_key=$(_generate_raw_encryption_key) mkdir $SCRATCH_MNT/edir _add_encryption_key $keydesc $raw_key -$XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir +_set_encpolicy $SCRATCH_MNT/edir $keydesc # Create two files in the directory: one whose name is valid in the base64 # format used for encoding ciphertext filenames, and one whose name is not. The
tests/generic/435 +1 -2
@@ -42,7 +42,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _require_command "$KEYCTL_PROG" keyctl # set up an encrypted directory
@@ -53,7 +52,7 @@ _scratch_mount mkdir $SCRATCH_MNT/edir keydesc=$(_generate_encryption_key) # -f 0x2: zero-pad to 16-byte boundary (i.e. encryption block boundary) -$XFS_IO_PROG -c "set_encpolicy -f 0x2 $keydesc" $SCRATCH_MNT/edir +_set_encpolicy $SCRATCH_MNT/edir $keydesc -f 0x2 # Create files with long names (> 32 bytes, long enough to trigger the use of # "digested" names) in the encrypted directory.
tests/generic/440 +2 -3
@@ -38,7 +38,6 @@ rm -f $seqres.full _supported_fs generic _supported_os Linux _require_scratch_encryption -_require_xfs_io_command "set_encpolicy" _require_command "$KEYCTL_PROG" keyctl # Set up an encryption-capable filesystem and an encryption key.
@@ -52,7 +51,7 @@ _add_encryption_key $keydesc $raw_key # Set up an encrypted directory containing a regular file, a subdirectory, and a # symlink. mkdir $SCRATCH_MNT/edir -$XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir +_set_encpolicy $SCRATCH_MNT/edir $keydesc mkdir $SCRATCH_MNT/edir/subdir ln -s target $SCRATCH_MNT/edir/symlink echo contents > $SCRATCH_MNT/edir/file
@@ -82,7 +81,7 @@ echo echo "***** Child has key, but parent doesn't *****" _add_encryption_key $keydesc $raw_key mkdir $SCRATCH_MNT/edir2 -$XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/edir2 +_set_encpolicy $SCRATCH_MNT/edir2 $keydesc ln $SCRATCH_MNT/edir/file $SCRATCH_MNT/edir2/link _scratch_cycle_mount cat $SCRATCH_MNT/edir2/link
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Eric Biggers
maintainerPATCH v2 2/7 fscrypt-crypt-util: add utility for reproducing fscrypt encrypted data
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Add a utility program that can reproduce encrypted contents and filenames. It implements all encryption algorithms currently supported by fscrypt (a.k.a. ext4, f2fs, and ubifs encryption), and it generates IVs in the same way. The program takes the algorithm and master key on the command line, and encrypts stdin to stdout.
A file nonce may also be passed on the command line, and the program will "tweak" the encryption using this nonce in the same way the kernel does -- either by deriving a subkey, or by including the nonce in the IVs. The block size and padding amount may also be specified.
No dependencies are added, as all algorithms implemented from scratch.
--- .gitignore | 1 + src/Makefile | 3 +- src/fscrypt-crypt-util.c | 1633 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 1636 insertions(+), 1 deletion(-) create mode 100644 src/fscrypt-crypt-util.c
.gitignore +1 -0
@@ -73,6 +73,7 @@ /src/fill /src/fill2 /src/fs_perms +/src/fscrypt-crypt-util /src/fssum /src/fstest /src/fsync-err
src/Makefile +2 -1
@@ -27,7 +27,8 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \ renameat2 t_getcwd e4compact test-nextquota punch-alternating \ attr-list-by-handle-cursor-test listxattr dio-interleaved t_dir_type \ dio-invalidate-cache stat_test t_encrypted_d_revalidate \ - attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles + attr_replace_test swapon mkswap t_attr_corruption t_open_tmpfiles \ + fscrypt-crypt-util SUBDIRS = log-writes perf
src/fscrypt-crypt-util.c new file +1633 -0
1,633 lines not on this page · raw message
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Eric Biggers
maintainerPATCH v2 3/7 common/encrypt: support requiring other encryption settings
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Update _require_scratch_encryption() to support checking for kernel support for contents and filenames encryption modes besides the default. This will be used by some of the ciphertext verification tests.
--- common/encrypt | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+)
common/encrypt +58 -0
@@ -4,6 +4,15 @@ # # Functions for setting up and testing file encryption +# +# _require_scratch_encryption [-c CONTENTS_MODE] [-n FILENAMES_MODE] +# +# Require encryption support on the scratch device. +# +# This checks for support for the default type of encryption policy (AES-256-XTS +# and AES-256-CTS). Options can be specified to also require support for a +# different type of encryption policy. +# _require_scratch_encryption() { _require_scratch
@@ -44,9 +53,58 @@ _require_scratch_encryption() _notrun "kernel does not support $FSTYP encryption" fi rmdir $SCRATCH_MNT/tmpdir + + # If required, check for support for the specific type of encryption + # policy required by the test. + if [ $# -ne 0 ]; then + _require_encryption_policy_support $SCRATCH_MNT "$@" + fi + _scratch_unmount } +_require_encryption_policy_support() +{ + local mnt=$1 + local dir=$mnt/tmpdir + local set_encpolicy_args="" + local c + + OPTIND=2 + while getopts "c:n:" c; do + case $c in + c|n) + set_encpolicy_args+=" -$c $OPTARG" + ;; + *) + _fail "Unrecognized option '$c'" + ;; + esac + done + set_encpolicy_args=${set_encpolicy_args# } + + echo "Checking whether kernel supports encryption policy: $set_encpolicy_args" \ + >> $seqres.full + + mkdir $dir + _require_command "$KEYCTL_PROG" keyctl + _new_session_keyring + local keydesc=$(_generate_encryption_key) + if _set_encpolicy $dir $keydesc $set_encpolicy_args \ + 2>&1 >>$seqres.full | egrep -q 'Invalid argument'; then + _notrun "kernel does not support encryption policy: '$set_encpolicy_args'" + fi + # fscrypt allows setting policies with modes it knows about, even + # without kernel crypto API support. E.g. a policy using Adiantum + # encryption can be set on a kernel without CONFIG_CRYPTO_ADIANTUM. + # But actually trying to use such an encrypted directory will fail. + if ! touch $dir/file; then + _notrun "encryption policy '$set_encpolicy_args' is unusable; probably missing kernel crypto API support" + fi + $KEYCTL_PROG clear @s + rm -r $dir +} + _scratch_mkfs_encrypted() { case $FSTYP in
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Eric Biggers
maintainerPATCH v2 4/7 common/encrypt: add helper for ciphertext verification tests
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Introduce a function _verify_ciphertext_for_encryption_policy() which verifies the correctness of encryption with the specified settings.
Basically, it does the following:
1. If missing any prerequisites, skip the test.
2. Create files in encrypted directories on the scratch device.
3. Unmount the scratch device and compare the actual ciphertext stored on-disk to the ciphertext computed by the fscrypt-crypt-util program.
Both file contents and names are verified, and non-default encryption modes are supported. Previously, non-default encryption modes were untested by xfstests. Also, while there's an existing test generic/399 that checks that encrypted contents seem random, it doesn't actually test for correctness, nor does it test filenames encryption.
--- common/encrypt | 392 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 392 insertions(+)
common/encrypt +392 -0
@@ -258,3 +258,395 @@ _get_encpolicy() $XFS_IO_PROG -c "get_encpolicy $*" "$file" } + +# Retrieve the encryption nonce of the given inode as a hex string. The nonce +# was randomly generated by the filesystem and isn't exposed directly to +# userspace. But it can be read using the filesystem's debugging tools. +_get_encryption_nonce() +{ + local device=$1 + local inode=$2 + + case $FSTYP in + ext4) + # Use debugfs to dump the special xattr named "c", which is the + # file's fscrypt_context. This produces a line like: + # + # c (28) = 01 01 04 02 00 00 00 00 00 00 00 00 ef bd 18 76 5d f6 41 4e c0 a2 cd 5f 91 29 7e 12 + # + # Then filter it to get just the 16-byte 'nonce' field at the end: + # + # efbd18765df6414ec0a2cd5f91297e12 + # + $DEBUGFS_PROG $device -R "ea_get <$inode> c" 2>>$seqres.full \ + | grep '^c ' | sed 's/^.*=//' | tr -d ' \n' | tail -c 32 + ;; + f2fs) + # dump.f2fs prints the fscrypt_context like: + # + # xattr: e_name_index:9 e_name:c e_name_len:1 e_value_size:28 e_value: + # format: 1 + # contents_encryption_mode: 0x1 + # filenames_encryption_mode: 0x4 + # flags: 0x2 + # master_key_descriptor: 0000000000000000 + # nonce: EFBD18765DF6414EC0A2CD5F91297E12 + $DUMP_F2FS_PROG -i $inode $device | awk ' + /\<e_name:c\>/ { found = 1 } + /^nonce:/ && found { + print substr($0, length($0) - 31, 32); + found = 0; + }' + ;; + *) + _fail "_get_encryption_nonce() isn't implemented on $FSTYP" + ;; + esac +} + +# Require support for _get_encryption_nonce() +_require_get_encryption_nonce_support() +{ + echo "Checking for _get_encryption_nonce() support for $FSTYP" >> $seqres.full + case $FSTYP in + ext4) + _require_command "$DEBUGFS_PROG" debugfs + ;; + f2fs) + _require_command "$DUMP_F2FS_PROG" dump.f2fs + ;; + *) + _notrun "_get_encryption_nonce() isn't implemented on $FSTYP" + ;; + esac +} + +# Retrieve the encrypted filename stored on-disk for the given file. +# The name is printed to stdout in binary. +_get_ciphertext_filename() +{ + local device=$1 + local inode=$2 + local dir_inode=$3 + + case $FSTYP in + ext4) + # Extract the filename from the debugfs output line like: + # + # 131075 100644 (1) 0 0 0 22-Apr-2019 16:54 \xa2\x85\xb0z\x13\xe9\x09\x86R\xed\xdc\xce\xad\x14d\x19 + # + # Bytes are shown either literally or as \xHH-style escape + # sequences; we have to decode the escaped bytes here. + # + $DEBUGFS_PROG $device -R "ls -l -r <$dir_inode>" \ + 2>>$seqres.full | perl -ne ' + next if not /^\s*'$inode'\s+/; + s/.*?\d\d:\d\d //; + chomp; + s/\\x([[:xdigit:]]{2})/chr hex $1/eg; + print;' + ;; + f2fs) + # Extract the filename from the dump.f2fs output line like: + # + # i_name [UpkzIPuts9by1oDmE+Ivfw] + # + # The name is shown base64-encoded; we have to decode it here. + # + $DUMP_F2FS_PROG $device -i $inode | perl -ne ' + next if not /^i_name\s+\[([A-Za-z0-9+,]+)\]/; + chomp $1; + my @chars = split //, $1; + my $ac = 0; + my $bits = 0; + my $table = join "", (A..Z, a..z, 0..9, "+", ","); + foreach (@chars) { + $ac += index($table, $_) << $bits; + $bits += 6; + if ($bits >= 8) { + print chr($ac & 0xff); + $ac >>= 8; + $bits -= 8; + } + } + if ($ac != 0) { + print STDERR "Invalid base64-encoded string!\n"; + }' + ;; + *) + _fail "_get_ciphertext_filename() isn't implemented on $FSTYP" + ;; + esac +} + +# Require support for _get_ciphertext_filename(). +_require_get_ciphertext_filename_support() +{ + echo "Checking for _get_ciphertext_filename() support for $FSTYP" >> $seqres.full + case $FSTYP in + ext4) + # Verify that the "ls -l -r" debugfs command is supported and + # that it hex-encodes non-ASCII characters, rather than using an + # ambiguous escaping method. This requires e2fsprogs v1.45.1 or + # later; or more specifically, a version that has the commit + # "debugfs: avoid ambiguity when printing filenames". + _require_command "$DEBUGFS_PROG" debugfs + _scratch_mount + touch $SCRATCH_MNT/$'\xc1' + _scratch_unmount + if ! $DEBUGFS_PROG $SCRATCH_DEV -R "ls -l -r /" 2>&1 \ + | tee -a $seqres.full | grep -E -q '\s+\\xc1\s*$'; then + _notrun "debugfs (e2fsprogs) is too old; doesn't support showing unambiguous on-disk filenames" + fi + ;; + f2fs) + # Verify that dump.f2fs shows encrypted filenames in full. This + # requires f2fs-tools v1.13.0 or later; or more specifically, a + # version that has the commit + # "f2fs-tools: improve filename printing". + + _require_command "$DUMP_F2FS_PROG" dump.f2fs + _require_command "$KEYCTL_PROG" keyctl + _scratch_mount + _new_session_keyring + + local keydesc=$(_generate_encryption_key) + local dir=$SCRATCH_MNT/test.${FUNCNAME[0]} + local file=$dir/$(perl -e 'print "A" x 255') + mkdir $dir + _set_encpolicy $dir $keydesc + touch $file + local inode=$(stat -c %i $file) + + _scratch_unmount + $KEYCTL_PROG clear @s + + # 255-character filename should result in 340 base64 characters. + if ! $DUMP_F2FS_PROG -i $inode $SCRATCH_DEV \ + | grep -E -q '^i_name[[:space:]]+\[[A-Za-z0-9+,]{340}\]'; then + _notrun "dump.f2fs (f2fs-tools) is too old; doesn't support showing unambiguous on-disk filenames" + fi + ;; + *) + _notrun "_get_ciphertext_filename() isn't implemented on $FSTYP" + ;; + esac +} + +# Get an encrypted file's list of on-disk blocks as a comma-separated list of +# block offsets from the start of the device. "Blocks" are 512 bytes each here. +_get_ciphertext_block_list() +{ + local file=$1 + + sync + $XFS_IO_PROG -c fiemap $file | perl -ne ' + next if not /^\s*\d+: \[\d+\.\.\d+\]: (\d+)\.\.(\d+)/; + print $_ . "," foreach $1..$2;' | sed 's/,$//' +} + +# Dump a block list that was previously saved by _get_ciphertext_block_list(). +_dump_ciphertext_blocks() +{ + local device=$1 + local blocklist=$2 + local block + + for block in $(tr ',' ' ' <<< $blocklist); do + dd if=$device bs=512 count=1 skip=$block status=none + done +} + +_do_verify_ciphertext_for_encryption_policy() +{ + local contents_encryption_mode=$1 + local filenames_encryption_mode=$2 + local policy_flags=$3 + local set_encpolicy_args=$4 + local keydesc=$5 + local raw_key_hex=$6 + local crypt_cmd="src/fscrypt-crypt-util $7" + + local blocksize=$(_get_block_size $SCRATCH_MNT) + local test_contents_files=() + local test_filenames_files=() + local i src dir dst inode blocklist \ + padding_flag padding dir_inode len name f nonce decrypted_name + + # Create files whose encrypted contents we'll verify. For each, save + # the information: (copy of original file, inode number of encrypted + # file, comma-separated block list) into test_contents_files[]. + echo "Creating files for contents verification" >> $seqres.full + i=1 + rm -f $tmp.testfile_* + for src in /dev/zero /dev/urandom; do + head -c $((4 * blocksize)) $src > $tmp.testfile_$i + (( i++ )) + done + dir=$SCRATCH_MNT/encdir + mkdir $dir + _set_encpolicy $dir $keydesc $set_encpolicy_args -f $policy_flags + for src in $tmp.testfile_*; do + dst=$dir/${src##*.} + cp $src $dst + inode=$(stat -c %i $dst) + blocklist=$(_get_ciphertext_block_list $dst) + test_contents_files+=("$src $inode $blocklist") + done + + # Create files whose encrypted names we'll verify. For each, save the + # information: (original filename, inode number of encrypted file, inode + # of parent directory, padding amount) into test_filenames_files[]. Try + # each padding amount: 4, 8, 16, or 32 bytes. Also try various filename + # lengths, including boundary cases. Assume NAME_MAX == 255. + echo "Creating files for filenames verification" >> $seqres.full + for padding_flag in 0 1 2 3; do + padding=$((4 << padding_flag)) + dir=$SCRATCH_MNT/encdir.pad$padding + mkdir $dir + dir_inode=$(stat -c %i $dir) + _set_encpolicy $dir $keydesc $set_encpolicy_args \ + -f $((policy_flags | padding_flag)) + for len in 1 3 15 16 17 32 100 254 255; do + name=$(tr -d -C a-zA-Z0-9 < /dev/urandom | head -c $len) + touch $dir/$name + inode=$(stat -c %i $dir/$name) + test_filenames_files+=("$name $inode $dir_inode $padding") + done + done + + # Now unmount the filesystem and verify the ciphertext we just wrote. + _scratch_unmount + + echo "Verifying encrypted file contents" >> $seqres.full + for f in "${test_contents_files[@]}"; do + read -r src inode blocklist <<< "$f" + nonce=$(_get_encryption_nonce $SCRATCH_DEV $inode) + _dump_ciphertext_blocks $SCRATCH_DEV $blocklist > $tmp.actual_contents + $crypt_cmd $contents_encryption_mode $raw_key_hex \ + --file-nonce=$nonce --block-size=$blocksize \ + < $src > $tmp.expected_contents + if ! cmp $tmp.expected_contents $tmp.actual_contents; then + _fail "Expected encrypted contents != actual encrypted contents. File: $f" + fi + $crypt_cmd $contents_encryption_mode $raw_key_hex --decrypt \ + --file-nonce=$nonce --block-size=$blocksize \ + < $tmp.actual_contents > $tmp.decrypted_contents + if ! cmp $src $tmp.decrypted_contents; then + _fail "Contents decryption sanity check failed. File: $f" + fi + done + + echo "Verifying encrypted file names" >> $seqres.full + for f in "${test_filenames_files[@]}"; do + read -r name inode dir_inode padding <<< "$f" + nonce=$(_get_encryption_nonce $SCRATCH_DEV $dir_inode) + _get_ciphertext_filename $SCRATCH_DEV $inode $dir_inode \ + > $tmp.actual_name + echo -n "$name" | \ + $crypt_cmd $filenames_encryption_mode $raw_key_hex \ + --file-nonce=$nonce --padding=$padding \ + --block-size=255 > $tmp.expected_name + if ! cmp $tmp.expected_name $tmp.actual_name; then + _fail "Expected encrypted filename != actual encrypted filename. File: $f" + fi + $crypt_cmd $filenames_encryption_mode $raw_key_hex --decrypt \ + --file-nonce=$nonce --padding=$padding \ + --block-size=255 < $tmp.actual_name \ + > $tmp.decrypted_name + decrypted_name=$(tr -d '\0' < $tmp.decrypted_name) + if [ "$name" != "$decrypted_name" ]; then + _fail "Filename decryption sanity check failed ($name != $decrypted_name). File: $f" + fi + done +} + +_fscrypt_mode_name_to_num() +{ + local name=$1 + + case "$name" in + AES-256-XTS) echo 1 ;; # FS_ENCRYPTION_MODE_AES_256_XTS + AES-256-CTS-CBC) echo 4 ;; # FS_ENCRYPTION_MODE_AES_256_CTS + AES-128-CBC-ESSIV) echo 5 ;; # FS_ENCRYPTION_MODE_AES_128_CBC + AES-128-CTS-CBC) echo 6 ;; # FS_ENCRYPTION_MODE_AES_128_CTS + Adiantum) echo 9 ;; # FS_ENCRYPTION_MODE_ADIANTUM + *) _fail "Unknown fscrypt mode: $name" ;; + esac +} + +# Verify that file contents and names are encrypted correctly when an encryption +# policy of the specified type is used. +# +# The first two parameters are the contents and filenames encryption modes to +# test. Optionally, also specify 'direct' to test the DIRECT_KEY flag. +_verify_ciphertext_for_encryption_policy() +{ + local contents_encryption_mode=$1 + local filenames_encryption_mode=$2 + local opt + local policy_flags=0 + local set_encpolicy_args="" + local crypt_util_args="" + + shift 2 + for opt; do + case "$opt" in + direct) + if [ $contents_encryption_mode != \ + $filenames_encryption_mode ]; then + _fail "For direct key mode, contents and filenames modes must match" + fi + (( policy_flags |= 0x04 )) # FS_POLICY_FLAG_DIRECT_KEY + ;; + *) + _fail "Unknown option '$opt' passed to ${FUNCNAME[0]}" + ;; + esac + done + local contents_mode_num=$(_fscrypt_mode_name_to_num $contents_encryption_mode) + local filenames_mode_num=$(_fscrypt_mode_name_to_num $filenames_encryption_mode) + + set_encpolicy_args+=" -c $contents_mode_num" + set_encpolicy_args+=" -n $filenames_mode_num" + + if (( policy_flags & 0x04 )); then + crypt_util_args+=" --kdf=none" + else + crypt_util_args+=" --kdf=AES-128-ECB" + fi + set_encpolicy_args=${set_encpolicy_args# } + + _require_scratch_encryption $set_encpolicy_args + _require_test_program "fscrypt-crypt-util" + _require_xfs_io_command "fiemap" + _require_get_encryption_nonce_support + _require_get_ciphertext_filename_support + _require_command "$KEYCTL_PROG" keyctl + + echo "Creating encryption-capable filesystem" >> $seqres.full + _scratch_mkfs_encrypted &>> $seqres.full + _scratch_mount + + echo "Generating encryption key" >> $seqres.full + local raw_key=$(_generate_raw_encryption_key) + local keydesc=$(_generate_key_descriptor) + _new_session_keyring + _add_encryption_key $keydesc $raw_key + local raw_key_hex=$(echo "$raw_key" | tr -d '\\x') + + echo + echo -e "Verifying ciphertext with parameters:" + echo -e "\tcontents_encryption_mode: $contents_encryption_mode" + echo -e "\tfilenames_encryption_mode: $filenames_encryption_mode" + [ $# -ne 0 ] && echo -e "\toptions: $*" + + _do_verify_ciphertext_for_encryption_policy \ + "$contents_encryption_mode" \ + "$filenames_encryption_mode" \ + "$policy_flags" \ + "$set_encpolicy_args" \ + "$keydesc" \ + "$raw_key_hex" \ + "$crypt_util_args" +}
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Eric Biggers
maintainerPATCH v2 5/7 generic: verify ciphertext of v1 encryption policies with AES-256
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Verify ciphertext for v1 encryption policies that use AES-256-XTS to encrypt file contents and AES-256-CTS-CBC to encrypt file names.
--- tests/generic/700 | 41 +++++++++++++++++++++++++++++++++++++++++ tests/generic/700.out | 5 +++++ tests/generic/group | 1 + 3 files changed, 47 insertions(+) create mode 100755 tests/generic/700 create mode 100644 tests/generic/700.out
tests/generic/700 new file +41 -0
@@ -0,0 +1,41 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright 2019 Google LLC +# +# FS QA Test generic/700 +# +# Verify ciphertext for v1 encryption policies that use AES-256-XTS to encrypt +# file contents and AES-256-CTS-CBC to encrypt file names. +# +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap "_cleanup; exit \$status" 0 1 2 3 15 + +_cleanup() +{ + cd / + rm -f $tmp.* +} + +# get standard environment, filters and checks +. ./common/rc +. ./common/filter +. ./common/encrypt + +# remove previous $seqres.full before test +rm -f $seqres.full + +# real QA test starts here +_supported_fs generic +_supported_os Linux + +_verify_ciphertext_for_encryption_policy AES-256-XTS AES-256-CTS-CBC + +# success, all done +status=0 +exit
tests/generic/700.out new file +5 -0
@@ -0,0 +1,5 @@ +QA output created by 700 + +Verifying ciphertext with parameters: + contents_encryption_mode: AES-256-XTS + filenames_encryption_mode: AES-256-CTS-CBC
tests/generic/group +1 -0
@@ -550,3 +550,4 @@ 545 auto quick cap 546 auto quick clone enospc log 547 auto quick log +700 auto quick encrypt
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Eric Biggers
maintainerPATCH v2 6/7 generic: verify ciphertext of v1 encryption policies with AES-128
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Verify ciphertext for v1 encryption policies that use AES-128-CBC-ESSIV to encrypt file contents and AES-128-CTS-CBC to encrypt file names.
--- tests/generic/701 | 41 +++++++++++++++++++++++++++++++++++++++++ tests/generic/701.out | 5 +++++ tests/generic/group | 1 + 3 files changed, 47 insertions(+) create mode 100755 tests/generic/701 create mode 100644 tests/generic/701.out
tests/generic/701 new file +41 -0
@@ -0,0 +1,41 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright 2019 Google LLC +# +# FS QA Test generic/701 +# +# Verify ciphertext for v1 encryption policies that use AES-128-CBC-ESSIV to +# encrypt file contents and AES-128-CTS-CBC to encrypt file names. +# +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap "_cleanup; exit \$status" 0 1 2 3 15 + +_cleanup() +{ + cd / + rm -f $tmp.* +} + +# get standard environment, filters and checks +. ./common/rc +. ./common/filter +. ./common/encrypt + +# remove previous $seqres.full before test +rm -f $seqres.full + +# real QA test starts here +_supported_fs generic +_supported_os Linux + +_verify_ciphertext_for_encryption_policy AES-128-CBC-ESSIV AES-128-CTS-CBC + +# success, all done +status=0 +exit
tests/generic/701.out new file +5 -0
@@ -0,0 +1,5 @@ +QA output created by 701 + +Verifying ciphertext with parameters: + contents_encryption_mode: AES-128-CBC-ESSIV + filenames_encryption_mode: AES-128-CTS-CBC
tests/generic/group +1 -0
@@ -551,3 +551,4 @@ 546 auto quick clone enospc log 547 auto quick log 700 auto quick encrypt +701 auto quick encrypt
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog
Eric Biggers
maintainerPATCH v2 7/7 generic: verify ciphertext of v1 encryption policies with Adiantum
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Verify ciphertext for v1 encryption policies that use Adiantum to encrypt file contents and file names.
--- tests/generic/702 | 43 +++++++++++++++++++++++++++++++++++++++++++ tests/generic/702.out | 10 ++++++++++ tests/generic/group | 1 + 3 files changed, 54 insertions(+) create mode 100755 tests/generic/702 create mode 100644 tests/generic/702.out
tests/generic/702 new file +43 -0
@@ -0,0 +1,43 @@ +#! /bin/bash +# SPDX-License-Identifier: GPL-2.0 +# Copyright 2019 Google LLC +# +# FS QA Test generic/702 +# +# Verify ciphertext for v1 encryption policies that use Adiantum to encrypt file +# contents and file names. +# +seq=`basename $0` +seqres=$RESULT_DIR/$seq +echo "QA output created by $seq" + +here=`pwd` +tmp=/tmp/$$ +status=1 # failure is the default! +trap "_cleanup; exit \$status" 0 1 2 3 15 + +_cleanup() +{ + cd / + rm -f $tmp.* +} + +# get standard environment, filters and checks +. ./common/rc +. ./common/filter +. ./common/encrypt + +# remove previous $seqres.full before test +rm -f $seqres.full + +# real QA test starts here +_supported_fs generic +_supported_os Linux + +# Test both with and without the DIRECT_KEY flag. +_verify_ciphertext_for_encryption_policy Adiantum Adiantum +_verify_ciphertext_for_encryption_policy Adiantum Adiantum direct + +# success, all done +status=0 +exit
tests/generic/702.out new file +10 -0
@@ -0,0 +1,10 @@ +QA output created by 702 + +Verifying ciphertext with parameters: + contents_encryption_mode: Adiantum + filenames_encryption_mode: Adiantum + +Verifying ciphertext with parameters: + contents_encryption_mode: Adiantum + filenames_encryption_mode: Adiantum + options: direct
tests/generic/group +1 -0
@@ -552,3 +552,4 @@ 547 auto quick log 700 auto quick encrypt 701 auto quick encrypt +702 auto quick encrypt
Signature · 1 line
2.22.0.rc1.257.g3120a18244-goog