Skip to contentSkip to conversation

xfstests: add tests for fscrypt key management improvements

PATCH In review Eric Biggers 2 participants 15 messages Oct 15, 2019 – Jan 29, 2020 fscrypt: file system level encryption support

lore
Share & tools
lore

Eric Biggers

author maintainer
#0 lore raw

v3 cover xfstests: add tests for fscrypt key management improvements

Hello,

This patchset adds xfstests for the new fscrypt functionality that was merged for 5.4 (https://git.kernel.org/torvalds/c/734d1ed83e1f9b7b),
namely the new ioctls for managing filesystem encryption keys and the new/updated ioctls for v2 encryption policy support. It also includes ciphertext verification tests for v2 encryption policies.

These tests require new xfs_io commands, which are present in the for-next branch of xfsprogs. They also need a kernel v5.4-rc1 or later. As is usual for xfstests, the tests will skip themselves if their prerequisites aren't met.

Note: currently only ext4, f2fs, and ubifs support encryption. But I was told previously that since the fscrypt API is generic and may be supported by XFS in the future, the command-line wrappers for the fscrypt ioctls should be in xfs_io rather than in xfstests directly
(https://marc.info/?l=fstests&m=147976255831951&w=2).

This patchset can also be retrieved from tag
"fscrypt-key-mgmt-improvements_2019-10-15" of
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/xfstests-dev.git

Changes since v2:

- Updated "common/encrypt: disambiguate session encryption keys" to rename the new instance of _generate_encryption_key() in generic/576.

Changes since v1:

- Addressed comments from Eryu Guan regarding
_require_encryption_policy_support().

- In generic/801, handle the fsgqa user having part of their key quota already consumed before beginning the test, in order to avoid a false test failure on some systems.

Eric Biggers (9):
common/encrypt: disambiguate session encryption keys common/encrypt: add helper functions that wrap new xfs_io commands common/encrypt: support checking for v2 encryption policy support common/encrypt: support verifying ciphertext of v2 encryption policies
generic: add basic test for fscrypt API additions
generic: add test for non-root use of fscrypt API additions
generic: verify ciphertext of v2 encryption policies with AES-256
generic: verify ciphertext of v2 encryption policies with AES-128
generic: verify ciphertext of v2 encryption policies with Adiantum

 common/encrypt           | 181 +++++++++++++++++++----
 src/fscrypt-crypt-util.c | 304 ++++++++++++++++++++++++++++++++++-----
 tests/ext4/024           |   2 +-
 tests/generic/397        |   4 +-
 tests/generic/398        |   8 +-
 tests/generic/399        |   4 +-
 tests/generic/419        |   4 +-
 tests/generic/421        |   4 +-
 tests/generic/429        |   8 +-
 tests/generic/435        |   4 +-
 tests/generic/440        |   8 +-
 tests/generic/576        |   2 +-
 tests/generic/800        | 127 ++++++++++++++++
 tests/generic/800.out    |  91 ++++++++++++
 tests/generic/801        | 144 +++++++++++++++++++
 tests/generic/801.out    |  62 ++++++++
 tests/generic/802        |  43 ++++++
 tests/generic/802.out    |   6 +
 tests/generic/803        |  43 ++++++
 tests/generic/803.out    |   6 +
 tests/generic/804        |  45 ++++++
 tests/generic/804.out    |  11 ++
 tests/generic/group      |   5 +
 23 files changed, 1029 insertions(+), 87 deletions(-)
 create mode 100755 tests/generic/800
 create mode 100644 tests/generic/800.out
 create mode 100755 tests/generic/801
 create mode 100644 tests/generic/801.out
 create mode 100755 tests/generic/802
 create mode 100644 tests/generic/802.out
 create mode 100755 tests/generic/803
 create mode 100644 tests/generic/803.out
 create mode 100755 tests/generic/804
 create mode 100644 tests/generic/804.out
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#1 lore raw

PATCH v3 1/9 common/encrypt: disambiguate session encryption keys

From: Eric Biggers <[email protected]>

Rename the helper functions that add/remove keys from the session keyring, in order to distinguish them from the helper functions I'll be adding to add/remove keys from the new filesystem-level keyring.

---
 common/encrypt    | 20 ++++++++++----------
 tests/ext4/024    |  2 +-
 tests/generic/397 |  4 ++--
 tests/generic/398 |  8 ++++----
 tests/generic/399 |  4 ++--
 tests/generic/419 |  4 ++--
 tests/generic/421 |  4 ++--
 tests/generic/429 |  8 ++++----
 tests/generic/435 |  4 ++--
 tests/generic/440 |  8 ++++----
 tests/generic/576 |  2 +-
 11 files changed, 34 insertions(+), 34 deletions(-)
common/encrypt +10 -10
index 06a56ed9..7bbe1936 100644
@@ -89,7 +89,7 @@ _require_encryption_policy_support()
 	mkdir $dir
 	_require_command "$KEYCTL_PROG" keyctl
 	_new_session_keyring
-	local keydesc=$(_generate_encryption_key)
+	local keydesc=$(_generate_session_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'"
@@ -153,7 +153,7 @@ _generate_key_descriptor()
 	echo $keydesc
 }
 
-# Generate a raw encryption key, but don't add it to the keyring yet.
+# Generate a raw encryption key, but don't add it to any keyring yet.
 _generate_raw_encryption_key()
 {
 	local raw=""
@@ -166,7 +166,7 @@ _generate_raw_encryption_key()
 
 # Add the specified raw encryption key to the session keyring, using the
 # specified key descriptor.
-_add_encryption_key()
+_add_session_encryption_key()
 {
 	local keydesc=$1
 	local raw=$2
@@ -209,26 +209,26 @@ _add_encryption_key()
 # keyctl program.  It's assumed the caller has already set up a test-scoped
 # session keyring using _new_session_keyring.
 #
-_generate_encryption_key()
+_generate_session_encryption_key()
 {
 	local keydesc=$(_generate_key_descriptor)
 	local raw=$(_generate_raw_encryption_key)
 
-	_add_encryption_key $keydesc $raw
+	_add_session_encryption_key $keydesc $raw
 
 	echo $keydesc
 }
 
 # Unlink an encryption key from the session keyring, given its key descriptor.
-_unlink_encryption_key()
+_unlink_session_encryption_key()
 {
 	local keydesc=$1
 	local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
 	$KEYCTL_PROG unlink $keyid >>$seqres.full
 }
 
-# Revoke an encryption key from the keyring, given its key descriptor.
-_revoke_encryption_key()
+# Revoke an encryption key from the session keyring, given its key descriptor.
+_revoke_session_encryption_key()
 {
 	local keydesc=$1
 	local keyid=$($KEYCTL_PROG search @s logon $FSTYP:$keydesc)
@@ -412,7 +412,7 @@ _require_get_ciphertext_filename_support()
 		_scratch_mount
 		_new_session_keyring
 
-		local keydesc=$(_generate_encryption_key)
+		local keydesc=$(_generate_session_encryption_key)
 		local dir=$SCRATCH_MNT/test.${FUNCNAME[0]}
 		local file=$dir/$(perl -e 'print "A" x 255')
 		mkdir $dir
@@ -634,7 +634,7 @@ _verify_ciphertext_for_encryption_policy()
 	local raw_key=$(_generate_raw_encryption_key)
 	local keydesc=$(_generate_key_descriptor)
 	_new_session_keyring
-	_add_encryption_key $keydesc $raw_key
+	_add_session_encryption_key $keydesc $raw_key
 	local raw_key_hex=$(echo "$raw_key" | tr -d '\\x')
 
 	echo
tests/ext4/024 +1 -1
index a86cc417..95243b70 100755
@@ -53,7 +53,7 @@ _new_session_keyring
 _scratch_mkfs_encrypted &>>$seqres.full
 _scratch_mount
 mkdir $SCRATCH_MNT/edir
-keydesc=$(_generate_encryption_key)
+keydesc=$(_generate_session_encryption_key)
 _set_encpolicy $SCRATCH_MNT/edir $keydesc
 echo foo > $SCRATCH_MNT/edir/file
 inum=$(stat -c '%i' $SCRATCH_MNT/edir/file)
tests/generic/397 +2 -2
index a97e866b..f2e22950 100755
@@ -45,7 +45,7 @@ _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
 
 mkdir $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir
-keydesc=$(_generate_encryption_key)
+keydesc=$(_generate_session_encryption_key)
 _set_encpolicy $SCRATCH_MNT/edir $keydesc
 for dir in $SCRATCH_MNT/edir $SCRATCH_MNT/ref_dir; do
 	touch $dir/empty > /dev/null
@@ -92,7 +92,7 @@ filter_create_errors()
 	    -e 's/Operation not permitted/Required key not available/'
 }
 
-_unlink_encryption_key $keydesc
+_unlink_session_encryption_key $keydesc
 _scratch_cycle_mount
 
 # Check that unencrypted names aren't there
tests/generic/398 +4 -4
index d224e021..9727b2de 100755
@@ -68,8 +68,8 @@ edir1=$SCRATCH_MNT/edir1
 edir2=$SCRATCH_MNT/edir2
 udir=$SCRATCH_MNT/udir
 mkdir $edir1 $edir2 $udir
-keydesc1=$(_generate_encryption_key)
-keydesc2=$(_generate_encryption_key)
+keydesc1=$(_generate_session_encryption_key)
+keydesc2=$(_generate_session_encryption_key)
 _set_encpolicy $edir1 $keydesc1
 _set_encpolicy $edir2 $keydesc2
 touch $edir1/efile1
@@ -141,8 +141,8 @@ rm $edir1/fifo $edir2/fifo $udir/fifo
 # Now test that *without* access to the encrypted key, we cannot use an exchange
 # (cross rename) operation to move a forbidden file into an encrypted directory.
 
-_unlink_encryption_key $keydesc1
-_unlink_encryption_key $keydesc2
+_unlink_session_encryption_key $keydesc1
+_unlink_session_encryption_key $keydesc2
 _scratch_cycle_mount
 efile1=$(find $edir1 -type f)
 efile2=$(find $edir2 -type f)
tests/generic/399 +2 -2
index dfd8d3c2..b2aaac13 100755
@@ -61,7 +61,7 @@ dd if=/dev/zero of=$SCRATCH_DEV bs=$((1024 * 1024)) \
 _scratch_mkfs_sized_encrypted $fs_size &>> $seqres.full
 _scratch_mount
 
-keydesc=$(_generate_encryption_key)
+keydesc=$(_generate_session_encryption_key)
 mkdir $SCRATCH_MNT/encrypted_dir
 _set_encpolicy $SCRATCH_MNT/encrypted_dir $keydesc
 
@@ -127,7 +127,7 @@ done
 # memory than the '-9' preset.  The memory needed with our settings will be
 # 64 * 6.5 = 416 MB; see xz(1).
 #
-_unlink_encryption_key $keydesc
+_unlink_session_encryption_key $keydesc
 _scratch_unmount
 fs_compressed_size=$(head -c $fs_size $SCRATCH_DEV | \
 	xz --lzma2=dict=64M,mf=hc4,mode=fast,nice=16 | \
tests/generic/419 +2 -2
index 0abbac90..d07aec05 100755
@@ -47,11 +47,11 @@ _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
 
 mkdir $SCRATCH_MNT/edir
-keydesc=$(_generate_encryption_key)
+keydesc=$(_generate_session_encryption_key)
 _set_encpolicy $SCRATCH_MNT/edir $keydesc
 echo a > $SCRATCH_MNT/edir/a
 echo b > $SCRATCH_MNT/edir/b
-_unlink_encryption_key $keydesc
+_unlink_session_encryption_key $keydesc
 _scratch_cycle_mount
 
 # Note that because encrypted filenames are unpredictable, this needs to be
tests/generic/421 +2 -2
index c8cc2dcc..f634a431 100755
@@ -51,7 +51,7 @@ slice=2
 # Create an encrypted file and sync its data to disk.
 rm -rf $dir
 mkdir $dir
-keydesc=$(_generate_encryption_key)
+keydesc=$(_generate_session_encryption_key)
 _set_encpolicy $dir $keydesc
 $XFS_IO_PROG -f $file -c "pwrite 0 $((nproc*slice))M" -c "fsync" > /dev/null
 
@@ -71,7 +71,7 @@ done
 sleep 1
 
 # Revoke the encryption key.
-keyid=$(_revoke_encryption_key $keydesc)
+keyid=$(_revoke_session_encryption_key $keydesc)
 
 # Now try to open the file again.  In buggy kernels this caused concurrent
 # readers to crash with a NULL pointer dereference during decryption.
tests/generic/429 +4 -4
index 472fdbd9..6c18c543 100755
@@ -56,7 +56,7 @@ _new_session_keyring
 keydesc=$(_generate_key_descriptor)
 raw_key=$(_generate_raw_encryption_key)
 mkdir $SCRATCH_MNT/edir
-_add_encryption_key $keydesc $raw_key
+_add_session_encryption_key $keydesc $raw_key
 _set_encpolicy $SCRATCH_MNT/edir $keydesc
 
 # Create two files in the directory: one whose name is valid in the base64
@@ -96,7 +96,7 @@ show_directory_with_key()
 # the correct number of them are listed by readdir, and save them for later.
 echo
 echo "***** Without encryption key *****"
-_unlink_encryption_key $keydesc
+_unlink_session_encryption_key $keydesc
 _scratch_cycle_mount
 echo "--- Directory listing:"
 ciphertext_names=( $(find $SCRATCH_MNT/edir -mindepth 1 | sort) )
@@ -109,7 +109,7 @@ show_file_contents
 # stale dentries.
 echo
 echo "***** With encryption key *****"
-_add_encryption_key $keydesc $raw_key
+_add_session_encryption_key $keydesc $raw_key
 show_directory_with_key
 
 # Test for ->d_revalidate() race conditions.
@@ -127,7 +127,7 @@ echo "***** After key revocation *****"
 	exec 3<$SCRATCH_MNT/edir
 	exec 4<$SCRATCH_MNT/edir/@@@
 	exec 5<$SCRATCH_MNT/edir/abcd
-	_revoke_encryption_key $keydesc
+	_revoke_session_encryption_key $keydesc
 	show_directory_with_key
 )
 
tests/generic/435 +2 -2
index 073596f3..f12d2be8 100755
@@ -50,7 +50,7 @@ _new_session_keyring
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
 mkdir $SCRATCH_MNT/edir
-keydesc=$(_generate_encryption_key)
+keydesc=$(_generate_session_encryption_key)
 # -f 0x2: zero-pad to 16-byte boundary (i.e. encryption block boundary)
 _set_encpolicy $SCRATCH_MNT/edir $keydesc -f 0x2
 
@@ -66,7 +66,7 @@ _set_encpolicy $SCRATCH_MNT/edir $keydesc -f 0x2
 seq -f "$SCRATCH_MNT/edir/abcdefghijklmnopqrstuvwxyz012345%.0f" 100000 | xargs touch
 find $SCRATCH_MNT/edir/ -type f | xargs stat -c %i | sort | uniq | wc -l
 
-_unlink_encryption_key $keydesc
+_unlink_session_encryption_key $keydesc
 _scratch_cycle_mount
 
 # Verify that every file has a unique inode number and can be removed without
tests/generic/440 +4 -4
index 434286f4..1ec1ed48 100755
@@ -46,7 +46,7 @@ _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
 keydesc=$(_generate_key_descriptor)
 raw_key=$(_generate_raw_encryption_key)
-_add_encryption_key $keydesc $raw_key
+_add_session_encryption_key $keydesc $raw_key
 
 # Set up an encrypted directory containing a regular file, a subdirectory, and a
 # symlink.
@@ -65,7 +65,7 @@ echo
 echo "***** Parent has key, but child doesn't *****"
 exec 3< $SCRATCH_MNT/edir # pin inode with cached key in memory
 ls $SCRATCH_MNT/edir | sort
-_unlink_encryption_key $keydesc
+_unlink_session_encryption_key $keydesc
 cat $SCRATCH_MNT/edir/file |& _filter_scratch
 ls $SCRATCH_MNT/edir/subdir
 cat $SCRATCH_MNT/edir/symlink |& _filter_scratch
@@ -79,14 +79,14 @@ exec 3>&-
 # plaintext contents, even though its filename is shown in ciphertext!
 echo
 echo "***** Child has key, but parent doesn't *****"
-_add_encryption_key $keydesc $raw_key
+_add_session_encryption_key $keydesc $raw_key
 mkdir $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
 exec 3< $SCRATCH_MNT/edir2/link # pin inode with cached key in memory
-_unlink_encryption_key $keydesc
+_unlink_session_encryption_key $keydesc
 stat $SCRATCH_MNT/edir/file |& _filter_scratch
 cat "$(find $SCRATCH_MNT/edir/ -type f)"
 exec 3>&-
tests/generic/576 +1 -1
index 254199ca..58525295 100755
@@ -47,7 +47,7 @@ fsv_file=$edir/file.fsv
 
 # Set up an encrypted directory.
 _new_session_keyring
-keydesc=$(_generate_encryption_key)
+keydesc=$(_generate_session_encryption_key)
 mkdir $edir
 _set_encpolicy $edir $keydesc
 
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#2 lore raw

PATCH v3 2/9 common/encrypt: add helper functions that wrap new xfs_io commands

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Wrap the new xfs_io commands 'add_enckey', 'rm_enckey', and 'enckey_status' with helper functions.

Also add _user_do_get_encpolicy(), so that all encryption xfs_io commands have a _user_do() version.

---
 common/encrypt | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
common/encrypt +65 -0
index 7bbe1936..a086e80f 100644
@@ -261,6 +261,71 @@ _get_encpolicy()
 	$XFS_IO_PROG -c "get_encpolicy $*" "$file"
 }
 
+_user_do_get_encpolicy()
+{
+	local file=$1
+	shift
+
+	_user_do "$XFS_IO_PROG -c \"get_encpolicy $*\" \"$file\""
+}
+
+# Add an encryption key to the given filesystem.
+_add_enckey()
+{
+	local mnt=$1
+	local raw_key=$2
+	shift 2
+
+	echo -ne "$raw_key" | $XFS_IO_PROG -c "add_enckey $*" "$mnt"
+}
+
+_user_do_add_enckey()
+{
+	local mnt=$1
+	local raw_key=$2
+	shift 2
+
+	_user_do "echo -ne \"$raw_key\" | $XFS_IO_PROG -c \"add_enckey $*\" \"$mnt\""
+}
+
+# Remove the given encryption key from the given filesystem.
+_rm_enckey()
+{
+	local mnt=$1
+	local keyspec=$2
+	shift 2
+
+	$XFS_IO_PROG -c "rm_enckey $* $keyspec" "$mnt"
+}
+
+_user_do_rm_enckey()
+{
+	local mnt=$1
+	local keyspec=$2
+	shift 2
+
+	_user_do "$XFS_IO_PROG -c \"rm_enckey $* $keyspec\" \"$mnt\""
+}
+
+# Get the status of the given encryption key on the given filesystem.
+_enckey_status()
+{
+	local mnt=$1
+	local keyspec=$2
+	shift 2
+
+	$XFS_IO_PROG -c "enckey_status $* $keyspec" "$mnt"
+}
+
+_user_do_enckey_status()
+{
+	local mnt=$1
+	local keyspec=$2
+	shift 2
+
+	_user_do "$XFS_IO_PROG -c \"enckey_status $* $keyspec\" \"$mnt\""
+}
+
 # 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.
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#3 lore raw

PATCH v3 3/9 common/encrypt: support checking for v2 encryption policy support

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Allow passing '-v 2' to _require_scratch_encryption() to check for v2 encryption policy support on the scratch device, and for xfs_io support for setting and getting v2 encryption policies.

---
 common/encrypt | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)
common/encrypt +33 -9
index a086e80f..eec585a0 100644
@@ -6,12 +6,13 @@
 
 #
 # _require_scratch_encryption [-c CONTENTS_MODE] [-n FILENAMES_MODE]
+#			      [-v POLICY_VERSION]
 #
 # 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.
+# This checks for support for the default type of encryption policy (v1 with
+# 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()
 {
@@ -68,14 +69,19 @@ _require_encryption_policy_support()
 	local mnt=$1
 	local dir=$mnt/tmpdir
 	local set_encpolicy_args=""
+	local policy_version=1
 	local c
 
 	OPTIND=2
-	while getopts "c:n:" c; do
+	while getopts "c:n:v:" c; do
 		case $c in
 		c|n)
 			set_encpolicy_args+=" -$c $OPTARG"
 			;;
+		v)
+			set_encpolicy_args+=" -$c $OPTARG"
+			policy_version=$OPTARG
+			;;
 		*)
 			_fail "Unrecognized option '$c'"
 			;;
@@ -87,10 +93,26 @@ _require_encryption_policy_support()
 		>> $seqres.full
 
 	mkdir $dir
-	_require_command "$KEYCTL_PROG" keyctl
-	_new_session_keyring
-	local keydesc=$(_generate_session_encryption_key)
-	if _set_encpolicy $dir $keydesc $set_encpolicy_args \
+	if (( policy_version > 1 )); then
+		_require_xfs_io_command "get_encpolicy" "-t"
+		local output=$(_get_encpolicy $dir -t)
+		if [ "$output" != "supported" ]; then
+			if [ "$output" = "unsupported" ]; then
+				_notrun "kernel does not support $FSTYP encryption v2 API"
+			fi
+			_fail "Unexpected output from 'get_encpolicy -t': $output"
+		fi
+		# Both the kernel and xfs_io support v2 encryption policies, and
+		# therefore also filesystem-level keys -- since that's the only
+		# way to provide keys for v2 policies.
+		local raw_key=$(_generate_raw_encryption_key)
+		local keyspec=$(_add_enckey $mnt "$raw_key" | awk '{print $NF}')
+	else
+		_require_command "$KEYCTL_PROG" keyctl
+		_new_session_keyring
+		local keyspec=$(_generate_session_encryption_key)
+	fi
+	if _set_encpolicy $dir $keyspec $set_encpolicy_args \
 		2>&1 >>$seqres.full | egrep -q 'Invalid argument'; then
 		_notrun "kernel does not support encryption policy: '$set_encpolicy_args'"
 	fi
@@ -103,7 +125,9 @@ _require_encryption_policy_support()
 	if ! echo foo > $dir/file; then
 		_notrun "encryption policy '$set_encpolicy_args' is unusable; probably missing kernel crypto API support"
 	fi
-	$KEYCTL_PROG clear @s
+	if (( policy_version <= 1 )); then
+		$KEYCTL_PROG clear @s
+	fi
 	rm -r $dir
 }
 
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#4 lore raw

PATCH v3 4/9 common/encrypt: support verifying ciphertext of v2 encryption policies

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Update _verify_ciphertext_for_encryption_policy() to support v2 encryption policies.

This also required adding HKDF-SHA512 support to fscrypt-crypt-util.

---
 common/encrypt           |  58 ++++++--
 src/fscrypt-crypt-util.c | 304 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 316 insertions(+), 46 deletions(-)
common/encrypt +45 -13
index eec585a0..f82fb7bf 100644
@@ -382,9 +382,16 @@ _get_encryption_nonce()
 		#	flags: 0x2
 		#	master_key_descriptor: 0000000000000000
 		#	nonce: EFBD18765DF6414EC0A2CD5F91297E12
+		#
+		# Also support the case where the whole xattr is printed as hex,
+		# as is the case for fscrypt_context_v2.
+		#
+		#	xattr: e_name_index:9 e_name:c e_name_len:1 e_value_size:40 e_value:
+		#	020104020000000033809BFEBE68A4AD264079B30861DD5E6B9E72D07523C58794ACF52534BAA756
+		#
 		$DUMP_F2FS_PROG -i $inode $device | awk '
 			/\<e_name:c\>/ { found = 1 }
-			/^nonce:/ && found {
+			(/^nonce:/ || /^[[:xdigit:]]+$/) && found {
 				print substr($0, length($0) - 31, 32);
 				found = 0;
 			}'
@@ -405,6 +412,11 @@ _require_get_encryption_nonce_support()
 		;;
 	f2fs)
 		_require_command "$DUMP_F2FS_PROG" dump.f2fs
+		# For fscrypt_context_v2, we actually need a f2fs-tools version
+		# that has the patch "f2fs-tools: improve xattr value printing"
+		# (https://sourceforge.net/p/linux-f2fs/mailman/message/36648640/).
+		# Otherwise the xattr is incorrectly parsed as v1.  But just let
+		# the test fail in that case, as it was an f2fs-tools bug...
 		;;
 	*)
 		_notrun "_get_encryption_nonce() isn't implemented on $FSTYP"
@@ -554,7 +566,7 @@ _do_verify_ciphertext_for_encryption_policy()
 	local filenames_encryption_mode=$2
 	local policy_flags=$3
 	local set_encpolicy_args=$4
-	local keydesc=$5
+	local keyspec=$5
 	local raw_key_hex=$6
 	local crypt_cmd="src/fscrypt-crypt-util $7"
 
@@ -576,7 +588,7 @@ _do_verify_ciphertext_for_encryption_policy()
 	done
 	dir=$SCRATCH_MNT/encdir
 	mkdir $dir
-	_set_encpolicy $dir $keydesc $set_encpolicy_args -f $policy_flags
+	_set_encpolicy $dir $keyspec $set_encpolicy_args -f $policy_flags
 	for src in $tmp.testfile_*; do
 		dst=$dir/${src##*.}
 		cp $src $dst
@@ -596,7 +608,7 @@ _do_verify_ciphertext_for_encryption_policy()
 		dir=$SCRATCH_MNT/encdir.pad$padding
 		mkdir $dir
 		dir_inode=$(stat -c %i $dir)
-		_set_encpolicy $dir $keydesc $set_encpolicy_args \
+		_set_encpolicy $dir $keyspec $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)
@@ -670,12 +682,14 @@ _fscrypt_mode_name_to_num()
 # 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.
+# test.  Optionally, also specify 'direct' to test the DIRECT_KEY flag, and/or
+# 'v2' to test v2 policies.
 _verify_ciphertext_for_encryption_policy()
 {
 	local contents_encryption_mode=$1
 	local filenames_encryption_mode=$2
 	local opt
+	local policy_version=1
 	local policy_flags=0
 	local set_encpolicy_args=""
 	local crypt_util_args=""
@@ -683,6 +697,9 @@ _verify_ciphertext_for_encryption_policy()
 	shift 2
 	for opt; do
 		case "$opt" in
+		v2)
+			policy_version=2
+			;;
 		direct)
 			if [ $contents_encryption_mode != \
 			     $filenames_encryption_mode ]; then
@@ -701,10 +718,18 @@ _verify_ciphertext_for_encryption_policy()
 	set_encpolicy_args+=" -c $contents_mode_num"
 	set_encpolicy_args+=" -n $filenames_mode_num"
 
-	if (( policy_flags & 0x04 )); then
-		crypt_util_args+=" --kdf=none"
+	if (( policy_version > 1 )); then
+		set_encpolicy_args+=" -v 2"
+		crypt_util_args+=" --kdf=HKDF-SHA512"
+		if (( policy_flags & 0x04 )); then
+			crypt_util_args+=" --mode-num=$contents_mode_num"
+		fi
 	else
-		crypt_util_args+=" --kdf=AES-128-ECB"
+		if (( policy_flags & 0x04 )); then
+			crypt_util_args+=" --kdf=none"
+		else
+			crypt_util_args+=" --kdf=AES-128-ECB"
+		fi
 	fi
 	set_encpolicy_args=${set_encpolicy_args# }
 
@@ -713,7 +738,9 @@ _verify_ciphertext_for_encryption_policy()
 	_require_xfs_io_command "fiemap"
 	_require_get_encryption_nonce_support
 	_require_get_ciphertext_filename_support
-	_require_command "$KEYCTL_PROG" keyctl
+	if (( policy_version == 1 )); then
+		_require_command "$KEYCTL_PROG" keyctl
+	fi
 
 	echo "Creating encryption-capable filesystem" >> $seqres.full
 	_scratch_mkfs_encrypted &>> $seqres.full
@@ -721,9 +748,14 @@ _verify_ciphertext_for_encryption_policy()
 
 	echo "Generating encryption key" >> $seqres.full
 	local raw_key=$(_generate_raw_encryption_key)
-	local keydesc=$(_generate_key_descriptor)
-	_new_session_keyring
-	_add_session_encryption_key $keydesc $raw_key
+	if (( policy_version > 1 )); then
+		local keyspec=$(_add_enckey $SCRATCH_MNT "$raw_key" \
+				| awk '{print $NF}')
+	else
+		local keyspec=$(_generate_key_descriptor)
+		_new_session_keyring
+		_add_session_encryption_key $keyspec $raw_key
+	fi
 	local raw_key_hex=$(echo "$raw_key" | tr -d '\\x')
 
 	echo
@@ -737,7 +769,7 @@ _verify_ciphertext_for_encryption_policy()
 		"$filenames_encryption_mode" \
 		"$policy_flags" \
 		"$set_encpolicy_args" \
-		"$keydesc" \
+		"$keyspec" \
 		"$raw_key_hex" \
 		"$crypt_util_args"
 }
src/fscrypt-crypt-util.c +271 -33

452 lines not on this page · raw message

Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#5 lore raw

PATCH v3 5/9 generic: add basic test for fscrypt API additions

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Add a basic test of the fscrypt filesystem-level encryption keyring and v2 encryption policies.

---
 tests/generic/800     | 127 ++++++++++++++++++++++++++++++++++++++++++
 tests/generic/800.out |  91 ++++++++++++++++++++++++++++++
 tests/generic/group   |   1 +
 3 files changed, 219 insertions(+)
 create mode 100755 tests/generic/800
 create mode 100644 tests/generic/800.out
tests/generic/800 new file +127 -0
new file mode 100755
index 00000000..79a101f6
@@ -0,0 +1,127 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2019 Google LLC
+#
+# FS QA Test generic/800
+#
+# Basic test of the fscrypt filesystem-level encryption keyring
+# and v2 encryption policies.
+#
+
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+echo
+
+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
+_require_scratch_encryption -v 2
+
+_scratch_mkfs_encrypted &>> $seqres.full
+_scratch_mount
+
+test_with_policy_version()
+{
+	local vers=$1
+	local raw_key=""
+	local i
+
+	for i in {1..64}; do
+		raw_key+="\\x$(printf "%02x" $i)"
+	done
+
+	if (( vers == 1 )); then
+		# Key descriptor: arbitrary value
+		local keyspec="0000111122223333"
+		local add_enckey_args="-d $keyspec"
+	else
+		# Key identifier:
+		# HKDF-SHA512(key=raw_key, salt="", info="fscrypt\0\x01")
+		local keyspec="69b2f6edeee720cce0577937eb8a6751"
+		local add_enckey_args=""
+	fi
+
+	mkdir $dir
+	echo "# Setting v$vers encryption policy"
+	_set_encpolicy $dir $keyspec
+	echo "# Getting v$vers encryption policy"
+	_get_encpolicy $dir | _filter_scratch
+	if (( vers == 1 )); then
+		echo "# Getting v1 encryption policy using old ioctl"
+		_get_encpolicy $dir -1 | _filter_scratch
+	fi
+	echo "# Trying to create file without key added yet"
+	$XFS_IO_PROG -f $dir/file |& _filter_scratch
+	echo "# Getting encryption key status"
+	_enckey_status $SCRATCH_MNT $keyspec
+	echo "# Adding encryption key"
+	_add_enckey $SCRATCH_MNT "$raw_key" $add_enckey_args
+	echo "# Creating encrypted file"
+	echo contents > $dir/file
+	echo "# Getting encryption key status"
+	_enckey_status $SCRATCH_MNT $keyspec
+	echo "# Removing encryption key"
+	_rm_enckey $SCRATCH_MNT $keyspec
+	echo "# Getting encryption key status"
+	_enckey_status $SCRATCH_MNT $keyspec
+	echo "# Verifying that the encrypted directory was \"locked\""
+	cat $dir/file |& _filter_scratch
+	cat "$(find $dir -type f)" |& _filter_scratch | cut -d ' ' -f3-
+
+	# Test removing key with a file open.
+	echo "# Re-adding encryption key"
+	_add_enckey $SCRATCH_MNT "$raw_key" $add_enckey_args
+	echo "# Creating another encrypted file"
+	echo foo > $dir/file2
+	echo "# Removing key while an encrypted file is open"
+	exec 3< $dir/file
+	_rm_enckey $SCRATCH_MNT $keyspec
+	echo "# Non-open file should have been evicted"
+	cat $dir/file2 |& _filter_scratch
+	echo "# Open file shouldn't have been evicted"
+	cat $dir/file
+	echo "# Key should be in \"incompletely removed\" state"
+	_enckey_status $SCRATCH_MNT $keyspec
+	echo "# Closing file and removing key for real now"
+	exec 3<&-
+	_rm_enckey $SCRATCH_MNT $keyspec
+	cat $dir/file |& _filter_scratch
+
+	echo "# Cleaning up"
+	rm -r $dir
+	_scratch_cycle_mount	# Clear all keys
+	echo
+}
+
+dir=$SCRATCH_MNT/dir
+
+test_with_policy_version 1
+
+test_with_policy_version 2
+
+echo "# Trying to remove absent key"
+_rm_enckey $SCRATCH_MNT abcdabcdabcdabcd
+
+# success, all done
+status=0
+exit
tests/generic/800.out new file +91 -0

91 lines not on this page · raw message

tests/generic/group +1 -0
index 6f9c4e12..cf2240ec 100644
@@ -581,3 +581,4 @@
 576 auto quick verity encrypt
 577 auto quick verity
 578 auto quick rw clone
+800 auto quick encrypt
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#6 lore raw

PATCH v3 6/9 generic: add test for non-root use of fscrypt API additions

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Test non-root use of the fscrypt filesystem-level encryption keyring and v2 encryption policies.

---
 tests/generic/801     | 144 ++++++++++++++++++++++++++++++++++++++++++
 tests/generic/801.out |  62 ++++++++++++++++++
 tests/generic/group   |   1 +
 3 files changed, 207 insertions(+)
 create mode 100755 tests/generic/801
 create mode 100644 tests/generic/801.out
tests/generic/801 new file +144 -0
new file mode 100755
index 00000000..c759ec94
@@ -0,0 +1,144 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2019 Google LLC
+#
+# FS QA Test generic/801
+#
+# Test non-root use of the fscrypt filesystem-level encryption keyring
+# and v2 encryption policies.
+#
+
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+echo
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+orig_maxkeys=
+
+_cleanup()
+{
+	cd /
+	rm -f $tmp.*
+	if [ -n "$orig_maxkeys" ]; then
+		echo "$orig_maxkeys" > /proc/sys/kernel/keys/maxkeys
+	fi
+}
+
+# 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
+_require_user
+_require_scratch_encryption -v 2
+
+_scratch_mkfs_encrypted &>> $seqres.full
+_scratch_mount
+
+# Set the fsgqa user's key quota to their current number of keys plus 5.
+orig_keys=$(_user_do "awk '/^[[:space:]]*$(id -u fsgqa):/{print \$4}' /proc/key-users | cut -d/ -f1")
+: ${orig_keys:=0}
+echo "orig_keys=$orig_keys" >> $seqres.full
+orig_maxkeys=$(</proc/sys/kernel/keys/maxkeys)
+keys_to_add=5
+echo $((orig_keys + keys_to_add)) > /proc/sys/kernel/keys/maxkeys
+
+dir=$SCRATCH_MNT/dir
+
+raw_key=""
+for i in `seq 64`; do
+	raw_key+="\\x$(printf "%02x" $i)"
+done
+keydesc="0000111122223333"
+keyid="69b2f6edeee720cce0577937eb8a6751"
+chmod 777 $SCRATCH_MNT
+
+_user_do "mkdir $dir"
+
+echo "# Setting v1 policy as regular user (should succeed)"
+_user_do_set_encpolicy $dir $keydesc
+
+echo "# Getting v1 policy as regular user (should succeed)"
+_user_do_get_encpolicy $dir | _filter_scratch
+
+echo "# Adding v1 policy key as regular user (should fail with EACCES)"
+_user_do_add_enckey $SCRATCH_MNT "$raw_key" -d $keydesc
+
+rm -rf $dir
+echo
+_user_do "mkdir $dir"
+
+echo "# Setting v2 policy as regular user without key already added (should fail with ENOKEY)"
+_user_do_set_encpolicy $dir $keyid |& _filter_scratch
+
+echo "# Adding v2 policy key as regular user (should succeed)"
+_user_do_add_enckey $SCRATCH_MNT "$raw_key"
+
+echo "# Setting v2 policy as regular user with key added (should succeed)"
+_user_do_set_encpolicy $dir $keyid
+
+echo "# Getting v2 policy as regular user (should succeed)"
+_user_do_get_encpolicy $dir | _filter_scratch
+
+echo "# Creating encrypted file as regular user (should succeed)"
+_user_do "echo contents > $dir/file"
+
+echo "# Removing v2 policy key as regular user (should succeed)"
+_user_do_rm_enckey $SCRATCH_MNT $keyid
+
+_scratch_cycle_mount	# Clear all keys
+
+echo
+echo "# Testing user key quota"
+for i in `seq $((keys_to_add + 1))`; do
+	rand_raw_key=$(_generate_raw_encryption_key)
+	_user_do_add_enckey $SCRATCH_MNT "$rand_raw_key" \
+	    | sed 's/ with identifier .*$//'
+done
+
+rm -rf $dir
+echo
+_user_do "mkdir $dir"
+_scratch_cycle_mount	# Clear all keys
+
+# Test multiple users adding the same key.
+echo "# Adding key as root"
+_add_enckey $SCRATCH_MNT "$raw_key"
+echo "# Getting key status as regular user"
+_user_do_enckey_status $SCRATCH_MNT $keyid
+echo "# Removing key only added by another user (should fail with ENOKEY)"
+_user_do_rm_enckey $SCRATCH_MNT $keyid
+echo "# Setting v2 encryption policy with key only added by another user (should fail with ENOKEY)"
+_user_do_set_encpolicy $dir $keyid |& _filter_scratch
+echo "# Adding second user of key"
+_user_do_add_enckey $SCRATCH_MNT "$raw_key"
+echo "# Getting key status as regular user"
+_user_do_enckey_status $SCRATCH_MNT $keyid
+echo "# Setting v2 encryption policy as regular user"
+_user_do_set_encpolicy $dir $keyid
+echo "# Removing this user's claim to the key"
+_user_do_rm_enckey $SCRATCH_MNT $keyid
+echo "# Getting key status as regular user"
+_user_do_enckey_status $SCRATCH_MNT $keyid
+echo "# Adding back second user of key"
+_user_do_add_enckey $SCRATCH_MNT "$raw_key"
+echo "# Remove key for \"all users\", as regular user (should fail with EACCES)"
+_user_do_rm_enckey $SCRATCH_MNT $keyid -a |& _filter_scratch
+_enckey_status $SCRATCH_MNT $keyid
+echo "# Remove key for \"all users\", as root"
+_rm_enckey $SCRATCH_MNT $keyid -a
+_enckey_status $SCRATCH_MNT $keyid
+
+# success, all done
+status=0
+exit
tests/generic/801.out new file +62 -0

62 lines not on this page · raw message

tests/generic/group +1 -0
index cf2240ec..6d1ecf5a 100644
@@ -582,3 +582,4 @@
 577 auto quick verity
 578 auto quick rw clone
 800 auto quick encrypt
+801 auto quick encrypt
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
#7 lore raw

Re: [PATCH v3 6/9] generic: add test for non-root use of fscrypt API additions

Hi Eric,

Quoted 4 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Test non-root use of the fscrypt filesystem-level encryption keyring and v2 encryption policies.

This testcase now fails on latest Linus tree with latest e2fsprogs on ext4:

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 dell-pesc430-01 5.4.0+ #1 SMP Mon Nov 25 16:40:55 EST 2019
MKFS_OPTIONS  -- /dev/sda3

MOUNT_OPTIONS -- -o acl,user_xattr -o context=system_u:object_r:nfs_t:s0 /dev/sda3 /mnt/xfstests/mnt2
generic/581 - output mismatch (see /var/lib/xfstests/results//generic/581.out.bad)
--- tests/generic/581.out 2019-11-25 20:30:04.536051638 -0500
+++ /var/lib/xfstests/results//generic/581.out.bad 2019-11-26 01:04:17.318332220 -0500
@@ -33,7 +33,7 @@
Added encryption key
Added encryption key
Added encryption key
-Error adding encryption key: Disk quota exceeded
+Added encryption key

# Adding key as root
...

A rough looking back shows that this probably started since your fscrypt update for 5.5, added support for IV_INO_LBLK_64 encryption policies.

I guess you are aware of this :-)

Thanks,
Murphy

Quoted 240 lines
---
 tests/generic/801     | 144 ++++++++++++++++++++++++++++++++++++++++++
 tests/generic/801.out |  62 ++++++++++++++++++
 tests/generic/group   |   1 +
 3 files changed, 207 insertions(+)
 create mode 100755 tests/generic/801
 create mode 100644 tests/generic/801.out
tests/generic/801 new file +144 -0
new file mode 100755
index 00000000..c759ec94
@@ -0,0 +1,144 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2019 Google LLC
+#
+# FS QA Test generic/801
+#
+# Test non-root use of the fscrypt filesystem-level encryption keyring
+# and v2 encryption policies.
+#
+
+seq=`basename $0`
+seqres=$RESULT_DIR/$seq
+echo "QA output created by $seq"
+echo
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+orig_maxkeys=
+
+_cleanup()
+{
+	cd /
+	rm -f $tmp.*
+	if [ -n "$orig_maxkeys" ]; then
+		echo "$orig_maxkeys" > /proc/sys/kernel/keys/maxkeys
+	fi
+}
+
+# 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
+_require_user
+_require_scratch_encryption -v 2
+
+_scratch_mkfs_encrypted &>> $seqres.full
+_scratch_mount
+
+# Set the fsgqa user's key quota to their current number of keys plus 5.
+orig_keys=$(_user_do "awk '/^[[:space:]]*$(id -u fsgqa):/{print \$4}' /proc/key-users | cut -d/ -f1")
+: ${orig_keys:=0}
+echo "orig_keys=$orig_keys" >> $seqres.full
+orig_maxkeys=$(</proc/sys/kernel/keys/maxkeys)
+keys_to_add=5
+echo $((orig_keys + keys_to_add)) > /proc/sys/kernel/keys/maxkeys
+
+dir=$SCRATCH_MNT/dir
+
+raw_key=""
+for i in `seq 64`; do
+	raw_key+="\\x$(printf "%02x" $i)"
+done
+keydesc="0000111122223333"
+keyid="69b2f6edeee720cce0577937eb8a6751"
+chmod 777 $SCRATCH_MNT
+
+_user_do "mkdir $dir"
+
+echo "# Setting v1 policy as regular user (should succeed)"
+_user_do_set_encpolicy $dir $keydesc
+
+echo "# Getting v1 policy as regular user (should succeed)"
+_user_do_get_encpolicy $dir | _filter_scratch
+
+echo "# Adding v1 policy key as regular user (should fail with EACCES)"
+_user_do_add_enckey $SCRATCH_MNT "$raw_key" -d $keydesc
+
+rm -rf $dir
+echo
+_user_do "mkdir $dir"
+
+echo "# Setting v2 policy as regular user without key already added (should fail with ENOKEY)"
+_user_do_set_encpolicy $dir $keyid |& _filter_scratch
+
+echo "# Adding v2 policy key as regular user (should succeed)"
+_user_do_add_enckey $SCRATCH_MNT "$raw_key"
+
+echo "# Setting v2 policy as regular user with key added (should succeed)"
+_user_do_set_encpolicy $dir $keyid
+
+echo "# Getting v2 policy as regular user (should succeed)"
+_user_do_get_encpolicy $dir | _filter_scratch
+
+echo "# Creating encrypted file as regular user (should succeed)"
+_user_do "echo contents > $dir/file"
+
+echo "# Removing v2 policy key as regular user (should succeed)"
+_user_do_rm_enckey $SCRATCH_MNT $keyid
+
+_scratch_cycle_mount	# Clear all keys
+
+echo
+echo "# Testing user key quota"
+for i in `seq $((keys_to_add + 1))`; do
+	rand_raw_key=$(_generate_raw_encryption_key)
+	_user_do_add_enckey $SCRATCH_MNT "$rand_raw_key" \
+	    | sed 's/ with identifier .*$//'
+done
+
+rm -rf $dir
+echo
+_user_do "mkdir $dir"
+_scratch_cycle_mount	# Clear all keys
+
+# Test multiple users adding the same key.
+echo "# Adding key as root"
+_add_enckey $SCRATCH_MNT "$raw_key"
+echo "# Getting key status as regular user"
+_user_do_enckey_status $SCRATCH_MNT $keyid
+echo "# Removing key only added by another user (should fail with ENOKEY)"
+_user_do_rm_enckey $SCRATCH_MNT $keyid
+echo "# Setting v2 encryption policy with key only added by another user (should fail with ENOKEY)"
+_user_do_set_encpolicy $dir $keyid |& _filter_scratch
+echo "# Adding second user of key"
+_user_do_add_enckey $SCRATCH_MNT "$raw_key"
+echo "# Getting key status as regular user"
+_user_do_enckey_status $SCRATCH_MNT $keyid
+echo "# Setting v2 encryption policy as regular user"
+_user_do_set_encpolicy $dir $keyid
+echo "# Removing this user's claim to the key"
+_user_do_rm_enckey $SCRATCH_MNT $keyid
+echo "# Getting key status as regular user"
+_user_do_enckey_status $SCRATCH_MNT $keyid
+echo "# Adding back second user of key"
+_user_do_add_enckey $SCRATCH_MNT "$raw_key"
+echo "# Remove key for \"all users\", as regular user (should fail with EACCES)"
+_user_do_rm_enckey $SCRATCH_MNT $keyid -a |& _filter_scratch
+_enckey_status $SCRATCH_MNT $keyid
+echo "# Remove key for \"all users\", as root"
+_rm_enckey $SCRATCH_MNT $keyid -a
+_enckey_status $SCRATCH_MNT $keyid
+
+# success, all done
+status=0
+exit
tests/generic/801.out new file +62 -0
new file mode 100644
index 00000000..b5b6cec8
@@ -0,0 +1,62 @@
+QA output created by 801
+
+# Setting v1 policy as regular user (should succeed)
+# Getting v1 policy as regular user (should succeed)
+Encryption policy for SCRATCH_MNT/dir:
+	Policy version: 0
+	Master key descriptor: 0000111122223333
+	Contents encryption mode: 1 (AES-256-XTS)
+	Filenames encryption mode: 4 (AES-256-CTS)
+	Flags: 0x02
+# Adding v1 policy key as regular user (should fail with EACCES)
+Permission denied
+
+# Setting v2 policy as regular user without key already added (should fail with ENOKEY)
+SCRATCH_MNT/dir: failed to set encryption policy: Required key not available
+# Adding v2 policy key as regular user (should succeed)
+Added encryption key with identifier 69b2f6edeee720cce0577937eb8a6751
+# Setting v2 policy as regular user with key added (should succeed)
+# Getting v2 policy as regular user (should succeed)
+Encryption policy for SCRATCH_MNT/dir:
+	Policy version: 2
+	Master key identifier: 69b2f6edeee720cce0577937eb8a6751
+	Contents encryption mode: 1 (AES-256-XTS)
+	Filenames encryption mode: 4 (AES-256-CTS)
+	Flags: 0x02
+# Creating encrypted file as regular user (should succeed)
+# Removing v2 policy key as regular user (should succeed)
+Removed encryption key with identifier 69b2f6edeee720cce0577937eb8a6751
+
+# Testing user key quota
+Added encryption key
+Added encryption key
+Added encryption key
+Added encryption key
+Added encryption key
+Error adding encryption key: Disk quota exceeded
+
+# Adding key as root
+Added encryption key with identifier 69b2f6edeee720cce0577937eb8a6751
+# Getting key status as regular user
+Present (user_count=1)
+# Removing key only added by another user (should fail with ENOKEY)
+Error removing encryption key: Required key not available
+# Setting v2 encryption policy with key only added by another user (should fail with ENOKEY)
+SCRATCH_MNT/dir: failed to set encryption policy: Required key not available
+# Adding second user of key
+Added encryption key with identifier 69b2f6edeee720cce0577937eb8a6751
+# Getting key status as regular user
+Present (user_count=2, added_by_self)
+# Setting v2 encryption policy as regular user
+# Removing this user's claim to the key
+Removed user's claim to encryption key with identifier 69b2f6edeee720cce0577937eb8a6751
+# Getting key status as regular user
+Present (user_count=1)
+# Adding back second user of key
+Added encryption key with identifier 69b2f6edeee720cce0577937eb8a6751
+# Remove key for "all users", as regular user (should fail with EACCES)
+Permission denied
+Present (user_count=2, added_by_self)
+# Remove key for "all users", as root
+Removed encryption key with identifier 69b2f6edeee720cce0577937eb8a6751
+Absent
tests/generic/group +1 -0
index cf2240ec..6d1ecf5a 100644
@@ -582,3 +582,4 @@
 577 auto quick verity
 578 auto quick rw clone
 800 auto quick encrypt
+801 auto quick encrypt
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#8 lore raw

Re: [PATCH v3 6/9] generic: add test for non-root use of fscrypt API additions

Quoted 25 lines · Murphy Zhou

Hi Eric,

Quoted 4 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Test non-root use of the fscrypt filesystem-level encryption keyring and v2 encryption policies.

This testcase now fails on latest Linus tree with latest e2fsprogs on ext4:

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 dell-pesc430-01 5.4.0+ #1 SMP Mon Nov 25 16:40:55 EST 2019
MKFS_OPTIONS  -- /dev/sda3

MOUNT_OPTIONS -- -o acl,user_xattr -o context=system_u:object_r:nfs_t:s0 /dev/sda3 /mnt/xfstests/mnt2
generic/581 - output mismatch (see /var/lib/xfstests/results//generic/581.out.bad)
--- tests/generic/581.out 2019-11-25 20:30:04.536051638 -0500
+++ /var/lib/xfstests/results//generic/581.out.bad 2019-11-26 01:04:17.318332220 -0500
@@ -33,7 +33,7 @@
Added encryption key
Added encryption key
Added encryption key
-Error adding encryption key: Disk quota exceeded
+Added encryption key

Quoted 8 lines

# Adding key as root
...

A rough looking back shows that this probably started since your fscrypt update for 5.5, added support for IV_INO_LBLK_64 encryption policies.

I guess you are aware of this :-)

Nope, this has been passing for me. I don't see how this can have anything to do with the fscrypt update for 5.5. I'm guessing this is a race condition in the test caused by the kernel's keyrings subsystem reclaiming the keys quota asynchronously. I'll see if I can find a way to reproduce it and make the test more reliable.

- Eric

#9 lore raw

Re: [PATCH v3 6/9] generic: add test for non-root use of fscrypt API additions

Quoted 32 lines · Eric Biggers
Quoted 31 lines · Murphy Zhou

Hi Eric,

Quoted 4 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Test non-root use of the fscrypt filesystem-level encryption keyring and v2 encryption policies.

This testcase now fails on latest Linus tree with latest e2fsprogs on ext4:

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 dell-pesc430-01 5.4.0+ #1 SMP Mon Nov 25 16:40:55 EST 2019
MKFS_OPTIONS  -- /dev/sda3

MOUNT_OPTIONS -- -o acl,user_xattr -o context=system_u:object_r:nfs_t:s0 /dev/sda3 /mnt/xfstests/mnt2
generic/581 - output mismatch (see /var/lib/xfstests/results//generic/581.out.bad)
--- tests/generic/581.out 2019-11-25 20:30:04.536051638 -0500
+++ /var/lib/xfstests/results//generic/581.out.bad 2019-11-26 01:04:17.318332220 -0500
@@ -33,7 +33,7 @@
Added encryption key
Added encryption key
Added encryption key
-Error adding encryption key: Disk quota exceeded
+Added encryption key

# Adding key as root
...

A rough looking back shows that this probably started since your fscrypt update for 5.5, added support for IV_INO_LBLK_64 encryption policies.

Quoted 8 lines
Quoted 2 lines

I guess you are aware of this :-)

Nope, this has been passing for me. I don't see how this can have anything to do with the fscrypt update for 5.5. I'm guessing this is a race condition in the test caused by the kernel's keyrings subsystem reclaiming the keys quota asynchronously. I'll see if I can find a way to reproduce it and make the test more reliable.

OK, Thanks for the info. I'll try to bisect.

Quoted 3 lines

- Eric

author maintainer
#10 lore raw

Re: [PATCH v3 6/9] generic: add test for non-root use of fscrypt API additions

Quoted 36 lines · Murphy Zhou
Quoted 35 lines · Eric Biggers
Quoted 33 lines · Murphy Zhou

Hi Eric,

Quoted 4 lines · Eric Biggers

From: Eric Biggers <[email protected]>

Test non-root use of the fscrypt filesystem-level encryption keyring and v2 encryption policies.

This testcase now fails on latest Linus tree with latest e2fsprogs on ext4:

FSTYP         -- ext4
PLATFORM      -- Linux/x86_64 dell-pesc430-01 5.4.0+ #1 SMP Mon Nov 25 16:40:55 EST 2019
MKFS_OPTIONS  -- /dev/sda3

MOUNT_OPTIONS -- -o acl,user_xattr -o context=system_u:object_r:nfs_t:s0 /dev/sda3 /mnt/xfstests/mnt2
generic/581 - output mismatch (see /var/lib/xfstests/results//generic/581.out.bad)
--- tests/generic/581.out 2019-11-25 20:30:04.536051638 -0500
+++ /var/lib/xfstests/results//generic/581.out.bad 2019-11-26 01:04:17.318332220 -0500
@@ -33,7 +33,7 @@
Added encryption key
Added encryption key
Added encryption key
-Error adding encryption key: Disk quota exceeded
+Added encryption key

# Adding key as root
...

A rough looking back shows that this probably started since your fscrypt update for 5.5, added support for IV_INO_LBLK_64 encryption policies.

I guess you are aware of this :-)

Quoted 8 lines
Quoted 5 lines

Nope, this has been passing for me. I don't see how this can have anything to do with the fscrypt update for 5.5. I'm guessing this is a race condition in the test caused by the kernel's keyrings subsystem reclaiming the keys quota asynchronously. I'll see if I can find a way to reproduce it and make the test more reliable.

OK, Thanks for the info. I'll try to bisect.

Can you check whether the test passes reliably for you if you apply
https://lkml.kernel.org/fstests/[email protected]/?

Thanks,

- Eric

author maintainer
#11 lore raw

PATCH v3 7/9 generic: verify ciphertext of v2 encryption policies with AES-256

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Verify ciphertext for v2 encryption policies that use AES-256-XTS to encrypt file contents and AES-256-CTS-CBC to encrypt file names.

---
 tests/generic/802     | 43 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/802.out |  6 ++++++
 tests/generic/group   |  1 +
 3 files changed, 50 insertions(+)
 create mode 100755 tests/generic/802
 create mode 100644 tests/generic/802.out
tests/generic/802 new file +43 -0
new file mode 100755
index 00000000..457260b2
@@ -0,0 +1,43 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2019 Google LLC
+#
+# FS QA Test generic/802
+#
+# Verify ciphertext for v2 encryption policies that use AES-256-XTS to encrypt
+# file contents and AES-256-CTS-CBC to encrypt file names.
+#
+# This is the same as generic/548, except using v2 policies.
+#
+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 v2
+
+# success, all done
+status=0
+exit
tests/generic/802.out new file +6 -0
new file mode 100644
index 00000000..c0930809
@@ -0,0 +1,6 @@
+QA output created by 802
+
+Verifying ciphertext with parameters:
+	contents_encryption_mode: AES-256-XTS
+	filenames_encryption_mode: AES-256-CTS-CBC
+	options: v2
tests/generic/group +1 -0
index 6d1ecf5a..6b6002e1 100644
@@ -583,3 +583,4 @@
 578 auto quick rw clone
 800 auto quick encrypt
 801 auto quick encrypt
+802 auto quick encrypt
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#12 lore raw

PATCH v3 8/9 generic: verify ciphertext of v2 encryption policies with AES-128

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Verify ciphertext for v2 encryption policies that use AES-128-CBC-ESSIV to encrypt file contents and AES-128-CTS-CBC to encrypt file names.

---
 tests/generic/803     | 43 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/803.out |  6 ++++++
 tests/generic/group   |  1 +
 3 files changed, 50 insertions(+)
 create mode 100755 tests/generic/803
 create mode 100644 tests/generic/803.out
tests/generic/803 new file +43 -0
new file mode 100755
index 00000000..c12daeff
@@ -0,0 +1,43 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2019 Google LLC
+#
+# FS QA Test generic/803
+#
+# Verify ciphertext for v2 encryption policies that use AES-128-CBC-ESSIV to
+# encrypt file contents and AES-128-CTS-CBC to encrypt file names.
+#
+# This is the same as generic/549, except using v2 policies.
+#
+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 v2
+
+# success, all done
+status=0
+exit
tests/generic/803.out new file +6 -0
new file mode 100644
index 00000000..f4051d27
@@ -0,0 +1,6 @@
+QA output created by 803
+
+Verifying ciphertext with parameters:
+	contents_encryption_mode: AES-128-CBC-ESSIV
+	filenames_encryption_mode: AES-128-CTS-CBC
+	options: v2
tests/generic/group +1 -0
index 6b6002e1..6f5c15b2 100644
@@ -584,3 +584,4 @@
 800 auto quick encrypt
 801 auto quick encrypt
 802 auto quick encrypt
+803 auto quick encrypt
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#13 lore raw

PATCH v3 9/9 generic: verify ciphertext of v2 encryption policies with Adiantum

In reply to Eric Biggers

From: Eric Biggers <[email protected]>

Verify ciphertext for v2 encryption policies that use Adiantum to encrypt file contents and file names.

---
 tests/generic/804     | 45 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/804.out | 11 +++++++++++
 tests/generic/group   |  1 +
 3 files changed, 57 insertions(+)
 create mode 100755 tests/generic/804
 create mode 100644 tests/generic/804.out
tests/generic/804 new file +45 -0
new file mode 100755
index 00000000..72e7b025
@@ -0,0 +1,45 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright 2019 Google LLC
+#
+# FS QA Test generic/804
+#
+# Verify ciphertext for v2 encryption policies that use Adiantum to encrypt file
+# contents and file names.
+#
+# This is the same as generic/550, except using v2 policies.
+#
+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 v2
+_verify_ciphertext_for_encryption_policy Adiantum Adiantum v2 direct
+
+# success, all done
+status=0
+exit
tests/generic/804.out new file +11 -0
new file mode 100644
index 00000000..726376b2
@@ -0,0 +1,11 @@
+QA output created by 804
+
+Verifying ciphertext with parameters:
+	contents_encryption_mode: Adiantum
+	filenames_encryption_mode: Adiantum
+	options: v2
+
+Verifying ciphertext with parameters:
+	contents_encryption_mode: Adiantum
+	filenames_encryption_mode: Adiantum
+	options: v2 direct
tests/generic/group +1 -0
index 6f5c15b2..3373980d 100644
@@ -585,3 +585,4 @@
 801 auto quick encrypt
 802 auto quick encrypt
 803 auto quick encrypt
+804 auto quick encrypt
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
author maintainer
#14 lore raw
In reply to Eric Biggers
Quoted 37 lines · Eric Biggers

Hello,

This patchset adds xfstests for the new fscrypt functionality that was merged for 5.4 (https://git.kernel.org/torvalds/c/734d1ed83e1f9b7b),
namely the new ioctls for managing filesystem encryption keys and the new/updated ioctls for v2 encryption policy support. It also includes ciphertext verification tests for v2 encryption policies.

These tests require new xfs_io commands, which are present in the for-next branch of xfsprogs. They also need a kernel v5.4-rc1 or later. As is usual for xfstests, the tests will skip themselves if their prerequisites aren't met.

Note: currently only ext4, f2fs, and ubifs support encryption. But I was told previously that since the fscrypt API is generic and may be supported by XFS in the future, the command-line wrappers for the fscrypt ioctls should be in xfs_io rather than in xfstests directly
(https://marc.info/?l=fstests&m=147976255831951&w=2).

This patchset can also be retrieved from tag
"fscrypt-key-mgmt-improvements_2019-10-15" of
https://git.kernel.org/pub/scm/linux/kernel/git/ebiggers/xfstests-dev.git

Changes since v2:

- Updated "common/encrypt: disambiguate session encryption keys" to rename the new instance of _generate_encryption_key() in generic/576.

Changes since v1:

- Addressed comments from Eryu Guan regarding
_require_encryption_policy_support().

- In generic/801, handle the fsgqa user having part of their key quota already consumed before beginning the test, in order to avoid a false test failure on some systems.

Quoted 11 lines

Eric Biggers (9):
common/encrypt: disambiguate session encryption keys common/encrypt: add helper functions that wrap new xfs_io commands common/encrypt: support checking for v2 encryption policy support common/encrypt: support verifying ciphertext of v2 encryption policies
generic: add basic test for fscrypt API additions
generic: add test for non-root use of fscrypt API additions
generic: verify ciphertext of v2 encryption policies with AES-256
generic: verify ciphertext of v2 encryption policies with AES-128
generic: verify ciphertext of v2 encryption policies with Adiantum

Does anyone have any more comments on these tests?

- Eric