Skip to contentSkip to conversation

common/encrypt: support block sizes larger than the page size

PATCH In review Baokun Li 2 participants 4 messages Nov 3 – Nov 23 fscrypt: file system level encryption support

lore
Share & tools
lore

Baokun Li

author
#0 lore raw

PATCH 1/1 common/encrypt: support block sizes larger than the page size

v1 encryption policies use the filesystem block size as their data unit size, so they cannot be used on a filesystem whose block size exceeds the page size. Make _require_scratch_encryption() skip such tests when a v1 policy is requested. Tests that request a v2 policy but also exercise v1 policies, i.e. generic/580, 581, 593, and 613, call _require_v1_encryption_support() explicitly.

A v2 policy with log2_data_unit_size 0 uses min(bs, PS) as its data unit size, not the block size. Add _get_encryption_du_size() and use it to calculate the expected ciphertext.

Also skip the v1 policy probe in _require_scratch_encryption() when a v2 policy is requested. Update the ext4/309 and ext4/310 comments for the new default data unit size.

---
 common/encrypt    | 68 +++++++++++++++++++++++++++++++++++++++--------
 tests/ext4/309    |  6 ++---
 tests/ext4/310    |  2 +-
 tests/generic/580 |  1 +
 tests/generic/581 |  1 +
 tests/generic/593 |  1 +
 tests/generic/613 |  1 +
 7 files changed, 65 insertions(+), 15 deletions(-)
common/encrypt +57 -11
index c9e0853d8d8c..feaef0682a44 100644
@@ -18,15 +18,21 @@
 _require_scratch_encryption()
 {
 	local arg
+	local policy_version=1
+	local prev=
 
 	_require_scratch
 	_require_xfs_io_command "set_encpolicy"
 
 	for arg; do
+		if [ "$prev" = "-v" ]; then
+			policy_version=$arg
+		fi
 		if [ "$arg" = "-s" ]; then
 			# -s option was added later.  Make sure it's available.
 			_require_xfs_io_command "set_encpolicy" "-s"
 		fi
+		prev=$arg
 	done
 
 	# The 'test_dummy_encryption' mount option interferes with trying to use
@@ -56,13 +62,24 @@ _require_scratch_encryption()
 	# to set an encryption policy.  (For ext4 we could instead check for the
 	# 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 _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \
-		grep -Eq 'Inappropriate ioctl for device|Operation not supported'
-	then
-		_notrun "kernel does not support $FSTYP encryption"
+	#
+	# If the test requires a v2 policy, don't probe with a v1 policy:
+	# v1 policies are not supported when the block size exceeds the page
+	# size.  _require_encryption_policy_support() will check for the
+	# requested policy version below.
+	if [ "$policy_version" -le 1 ]; then
+		mkdir $SCRATCH_MNT/tmpdir
+		if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \
+			grep -Eq 'Inappropriate ioctl for device|Operation not supported'
+		then
+			_notrun "kernel does not support $FSTYP encryption"
+		fi
+		rmdir $SCRATCH_MNT/tmpdir
+
+		# v1 policies use the block size as their data unit size,
+		# which doesn't work if the block size exceeds the page size.
+		_require_v1_encryption_support
 	fi
-	rmdir $SCRATCH_MNT/tmpdir
 
 	# If required, check for support for the specific type of encryption
 	# policy required by the test.
@@ -73,6 +90,39 @@ _require_scratch_encryption()
 	_scratch_unmount
 }
 
+# Require that the filesystem supports v1 encryption policies.  v1
+# policies use the filesystem block size as their data unit size, so
+# they aren't supported when the block size exceeds the page size.
+#
+# The scratch filesystem must be mounted.
+_require_v1_encryption_support()
+{
+	local block_size=$(_get_block_size $SCRATCH_MNT)
+
+	if [ "$block_size" -gt "$(_get_page_size)" ]; then
+		_notrun "v1 encryption policies aren't supported on filesystems with block size > page size"
+	fi
+}
+
+# Get the data unit size in bytes for a v2 encryption policy with the
+# given log2_data_unit_size on the filesystem mounted at $1.  A zero
+# value selects min(filesystem block size, page size).
+_get_encryption_du_size()
+{
+	local mnt=$1
+	local log2_dusize=$2
+	local block_size=$(_get_block_size "$mnt")
+	local page_size=$(_get_page_size)
+
+	if [ "$log2_dusize" -ne 0 ]; then
+		echo $((1 << log2_dusize))
+	elif [ "$block_size" -gt "$page_size" ]; then
+		echo "$page_size"
+	else
+		echo "$block_size"
+	fi
+}
+
 _require_encryption_policy_support()
 {
 	local mnt=$1
@@ -1014,11 +1064,7 @@ _verify_ciphertext_for_encryption_policy()
 	fi
 	_scratch_mount
 
-	if (( log2_dusize != 0 )); then
-		crypt_util_contents_args+=" --data-unit-size=$((1 << log2_dusize))"
-	else
-		crypt_util_contents_args+=" --data-unit-size=$(_get_block_size $SCRATCH_MNT)"
-	fi
+	crypt_util_contents_args+=" --data-unit-size=$(_get_encryption_du_size $SCRATCH_MNT $log2_dusize)"
 
 	crypt_util_args+=" --fs-uuid=$(blkid -s UUID -o value $SCRATCH_DEV | tr -d -)"
 
tests/ext4/309 +3 -3
index 6bc9b8cb4a4a..aa1b3b68fc8c 100755
@@ -5,9 +5,9 @@
 # FS QA Test No. ext4/309
 #
 # Verify that file contents encryption works on a filesystem with a block size
-# larger than the page size.  fscrypt uses the block size as the data unit size
-# by default, so this tests data units that span multiple pages, both for the
-# writeback path and for the on-disk ciphertext.
+# larger than the page size.  fscrypt uses min(block size, page size) as the
+# default data unit size, so this tests 4K data units on a 32K-block
+# filesystem, both for the writeback path and for the on-disk ciphertext.
 #
 . ./common/preamble
 _begin_fstest auto quick encrypt
tests/ext4/310 +1 -1
index 215d3a83cd6d..84473930f8cb 100755
@@ -6,7 +6,7 @@
 #
 # Test reading and writing an encrypted file with O_DIRECT on a filesystem
 # with a block size larger than the page size.  The data unit size is the
-# block size by default, so each data unit spans several pages here.
+# page size by default on such a filesystem.
 #
 . ./common/preamble
 _begin_fstest auto quick encrypt
tests/generic/580 +1 -0
index eff3f2104bfe..bcb984d1fd82 100755
@@ -20,6 +20,7 @@ _require_scratch_encryption -v 2
 
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
+_require_v1_encryption_support
 
 test_with_policy_version()
 {
tests/generic/581 +1 -0
index 2773c9108b35..54f31595a832 100755
@@ -33,6 +33,7 @@ _require_scratch_encryption -v 2
 
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
+_require_v1_encryption_support
 
 dir=$SCRATCH_MNT/dir
 
tests/generic/593 +1 -0
index 05f868f92422..61976df8194d 100755
@@ -21,6 +21,7 @@ _require_command "$KEYCTL_PROG" keyctl
 _init_session_keyring
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
+_require_v1_encryption_support
 _require_add_enckey_by_key_id $SCRATCH_MNT
 
 test_with_policy_version()
tests/generic/613 +1 -0
index 499af18ef476..aadb3893e73e 100755
@@ -26,6 +26,7 @@ _require_command "$XZ_PROG" xz
 
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
+_require_v1_encryption_support
 
 echo -e "\n# Adding encryption keys"
 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
Signature · 1 line
2.43.7
#1 lore raw
Quoted 68 lines · Baokun Li

v1 encryption policies use the filesystem block size as their data unit size, so they cannot be used on a filesystem whose block size exceeds the page size. Make _require_scratch_encryption() skip such tests when a v1 policy is requested. Tests that request a v2 policy but also exercise v1 policies, i.e. generic/580, 581, 593, and 613, call _require_v1_encryption_support() explicitly.

A v2 policy with log2_data_unit_size 0 uses min(bs, PS) as its data unit size, not the block size. Add _get_encryption_du_size() and use it to calculate the expected ciphertext.

Also skip the v1 policy probe in _require_scratch_encryption() when a v2 policy is requested. Update the ext4/309 and ext4/310 comments for the new default data unit size.

---
 common/encrypt    | 68 +++++++++++++++++++++++++++++++++++++++--------
 tests/ext4/309    |  6 ++---
 tests/ext4/310    |  2 +-
 tests/generic/580 |  1 +
 tests/generic/581 |  1 +
 tests/generic/593 |  1 +
 tests/generic/613 |  1 +
 7 files changed, 65 insertions(+), 15 deletions(-)
common/encrypt +13 -5
index c9e0853d8d8c..feaef0682a44 100644
@@ -18,15 +18,21 @@
 _require_scratch_encryption()
 {
 	local arg
+	local policy_version=1
+	local prev=
 
 	_require_scratch
 	_require_xfs_io_command "set_encpolicy"
 
 	for arg; do
+		if [ "$prev" = "-v" ]; then
+			policy_version=$arg
+		fi
 		if [ "$arg" = "-s" ]; then
 			# -s option was added later.  Make sure it's available.
 			_require_xfs_io_command "set_encpolicy" "-s"
 		fi
+		prev=$arg
 	done
 
 	# The 'test_dummy_encryption' mount option interferes with trying to use
@@ -56,13 +62,24 @@ _require_scratch_encryption()
 	# to set an encryption policy.  (For ext4 we could instead check for the
 	# 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 _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \
-		grep -Eq 'Inappropriate ioctl for device|Operation not supported'
-	then
-		_notrun "kernel does not support $FSTYP encryption"
+	#
+	# If the test requires a v2 policy, don't probe with a v1 policy:
+	# v1 policies are not supported when the block size exceeds the page
+	# size.  _require_encryption_policy_support() will check for the
+	# requested policy version below.
+	if [ "$policy_version" -le 1 ]; then
+		mkdir $SCRATCH_MNT/tmpdir

common/encrypt _require_scratch_encryption() · line 81

Quoted 10 lines
common/encrypt +10 -0
+		if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \
+			grep -Eq 'Inappropriate ioctl for device|Operation not supported'
+		then
+			_notrun "kernel does not support $FSTYP encryption"
+		fi
+		rmdir $SCRATCH_MNT/tmpdir
+
+		# v1 policies use the block size as their data unit size,
+		# which doesn't work if the block size exceeds the page size.
+		_require_v1_encryption_support

How about do _require_v1_encryption_support at first:

  if [ "$policy_version" -le 1 ]; then
      _require_v1_encryption_support
      mkdir $SCRATCH_MNT/tmpdir
      if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \

In that case, if block_size > page_size, we can directly skip the test with an explicit reason, avoids a redundant mkdir/rmdir and ioctl call.

Quoted 63 lines
common/encrypt +34 -6
 	fi
-	rmdir $SCRATCH_MNT/tmpdir
 
 	# If required, check for support for the specific type of encryption
 	# policy required by the test.
@@ -73,6 +90,39 @@ _require_scratch_encryption()
 	_scratch_unmount
 }
 
+# Require that the filesystem supports v1 encryption policies.  v1
+# policies use the filesystem block size as their data unit size, so
+# they aren't supported when the block size exceeds the page size.
+#
+# The scratch filesystem must be mounted.
+_require_v1_encryption_support()
+{
+	local block_size=$(_get_block_size $SCRATCH_MNT)
+
+	if [ "$block_size" -gt "$(_get_page_size)" ]; then
+		_notrun "v1 encryption policies aren't supported on filesystems with block size > page size"
+	fi
+}
+
+# Get the data unit size in bytes for a v2 encryption policy with the
+# given log2_data_unit_size on the filesystem mounted at $1.  A zero
+# value selects min(filesystem block size, page size).
+_get_encryption_du_size()
+{
+	local mnt=$1
+	local log2_dusize=$2
+	local block_size=$(_get_block_size "$mnt")
+	local page_size=$(_get_page_size)
+
+	if [ "$log2_dusize" -ne 0 ]; then
+		echo $((1 << log2_dusize))
+	elif [ "$block_size" -gt "$page_size" ]; then
+		echo "$page_size"
+	else
+		echo "$block_size"
+	fi
+}
+
 _require_encryption_policy_support()
 {
 	local mnt=$1
@@ -1014,11 +1064,7 @@ _verify_ciphertext_for_encryption_policy()
 	fi
 	_scratch_mount
 
-	if (( log2_dusize != 0 )); then
-		crypt_util_contents_args+=" --data-unit-size=$((1 << log2_dusize))"
-	else
-		crypt_util_contents_args+=" --data-unit-size=$(_get_block_size $SCRATCH_MNT)"
-	fi
+	crypt_util_contents_args+=" --data-unit-size=$(_get_encryption_du_size $SCRATCH_MNT $log2_dusize)"
 
 	crypt_util_args+=" --fs-uuid=$(blkid -s UUID -o value $SCRATCH_DEV | tr -d -)"
 
tests/ext4/309 +0 -0
index 6bc9b8cb4a4a..aa1b3b68fc8c 100755

tests/ext4/309 line 9

Quoted 8 lines
tests/ext4/309 +2 -3
@@ -5,9 +5,9 @@
 # FS QA Test No. ext4/309
 #
 # Verify that file contents encryption works on a filesystem with a block size
-# larger than the page size.  fscrypt uses the block size as the data unit size
-# by default, so this tests data units that span multiple pages, both for the
-# writeback path and for the on-disk ciphertext.
+# larger than the page size.  fscrypt uses min(block size, page size) as the
+# default data unit size, so this tests 4K data units on a 32K-block

Does ext4/309 hardcode 32k ?

Quoted 68 lines
tests/ext4/309 +1 -0
+# filesystem, both for the writeback path and for the on-disk ciphertext.
 #
 . ./common/preamble
 _begin_fstest auto quick encrypt
tests/ext4/310 +1 -1
index 215d3a83cd6d..84473930f8cb 100755
@@ -6,7 +6,7 @@
 #
 # Test reading and writing an encrypted file with O_DIRECT on a filesystem
 # with a block size larger than the page size.  The data unit size is the
-# block size by default, so each data unit spans several pages here.
+# page size by default on such a filesystem.
 #
 . ./common/preamble
 _begin_fstest auto quick encrypt
tests/generic/580 +1 -0
index eff3f2104bfe..bcb984d1fd82 100755
@@ -20,6 +20,7 @@ _require_scratch_encryption -v 2
 
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
+_require_v1_encryption_support
 
 test_with_policy_version()
 {
tests/generic/581 +1 -0
index 2773c9108b35..54f31595a832 100755
@@ -33,6 +33,7 @@ _require_scratch_encryption -v 2
 
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
+_require_v1_encryption_support
 
 dir=$SCRATCH_MNT/dir
 
tests/generic/593 +1 -0
index 05f868f92422..61976df8194d 100755
@@ -21,6 +21,7 @@ _require_command "$KEYCTL_PROG" keyctl
 _init_session_keyring
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
+_require_v1_encryption_support
 _require_add_enckey_by_key_id $SCRATCH_MNT
 
 test_with_policy_version()
tests/generic/613 +1 -0
index 499af18ef476..aadb3893e73e 100755
@@ -26,6 +26,7 @@ _require_command "$XZ_PROG" xz
 
 _scratch_mkfs_encrypted &>> $seqres.full
 _scratch_mount
+_require_v1_encryption_support
 
 echo -e "\n# Adding encryption keys"
 _add_enckey $SCRATCH_MNT "$TEST_RAW_KEY"
Signature · 1 line
2.43.7
author
#2 lore raw

Hi Zorro,

Thank you for your review!

Quoted 21 lines · Zorro Lang
Quoted 7 lines · Baokun Li
common/encrypt +7 -0
+	#
+	# If the test requires a v2 policy, don't probe with a v1 policy:
+	# v1 policies are not supported when the block size exceeds the page
+	# size.  _require_encryption_policy_support() will check for the
+	# requested policy version below.
+	if [ "$policy_version" -le 1 ]; then
+		mkdir $SCRATCH_MNT/tmpdir

common/encrypt _require_scratch_encryption() · line 81

Quoted 10 lines
common/encrypt +10 -0
+		if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \
+			grep -Eq 'Inappropriate ioctl for device|Operation not supported'
+		then
+			_notrun "kernel does not support $FSTYP encryption"
+		fi
+		rmdir $SCRATCH_MNT/tmpdir
+
+		# v1 policies use the block size as their data unit size,
+		# which doesn't work if the block size exceeds the page size.
+		_require_v1_encryption_support

How about do _require_v1_encryption_support at first:

Quoted 8 lines
  if [ "$policy_version" -le 1 ]; then
      _require_v1_encryption_support
      mkdir $SCRATCH_MNT/tmpdir
      if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \

In that case, if block_size > page_size, we can directly skip the test with an explicit reason, avoids a redundant mkdir/rmdir and ioctl call.

Yes, this can avoid some unnecessary operations.

Quoted 11 lines

tests/ext4/309 line 9

Quoted 8 lines

# FS QA Test No. ext4/309
#
# Verify that file contents encryption works on a filesystem with a block size

tests/ext4/309 +2 -3
-# larger than the page size.  fscrypt uses the block size as the data unit size
-# by default, so this tests data units that span multiple pages, both for the
-# writeback path and for the on-disk ciphertext.
+# larger than the page size.  fscrypt uses min(block size, page size) as the
+# default data unit size, so this tests 4K data units on a 32K-block

Does ext4/309 hardcode 32k ?

No, that's actually just an example using a block size of 32k; I'll rephrase that.

Additionally, the final plan has not been fully confirmed yet. I am still working on the details with Eric. It is expected that all v1 use cases will be converted to v2 later, and in the testing process, the data unit size will be explicitly specified. Please refer to:

https://lore.kernel.org/r/20261104195127.GA1971@quark

Cheers,
Baokun

#3 lore raw
Quoted 50 lines · Baokun Li

Hi Zorro,

Thank you for your review!

Quoted 21 lines · Zorro Lang
Quoted 7 lines · Baokun Li
common/encrypt +7 -0
+	#
+	# If the test requires a v2 policy, don't probe with a v1 policy:
+	# v1 policies are not supported when the block size exceeds the page
+	# size.  _require_encryption_policy_support() will check for the
+	# requested policy version below.
+	if [ "$policy_version" -le 1 ]; then
+		mkdir $SCRATCH_MNT/tmpdir

common/encrypt _require_scratch_encryption() · line 81

Quoted 10 lines
common/encrypt +10 -0
+		if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \
+			grep -Eq 'Inappropriate ioctl for device|Operation not supported'
+		then
+			_notrun "kernel does not support $FSTYP encryption"
+		fi
+		rmdir $SCRATCH_MNT/tmpdir
+
+		# v1 policies use the block size as their data unit size,
+		# which doesn't work if the block size exceeds the page size.
+		_require_v1_encryption_support

How about do _require_v1_encryption_support at first:

Quoted 8 lines
  if [ "$policy_version" -le 1 ]; then
      _require_v1_encryption_support
      mkdir $SCRATCH_MNT/tmpdir
      if _set_encpolicy $SCRATCH_MNT/tmpdir 2>&1 >>$seqres.full | \

In that case, if block_size > page_size, we can directly skip the test with an explicit reason, avoids a redundant mkdir/rmdir and ioctl call.

Yes, this can avoid some unnecessary operations.

Quoted 11 lines

tests/ext4/309 line 9

Quoted 8 lines

# FS QA Test No. ext4/309
#
# Verify that file contents encryption works on a filesystem with a block size

tests/ext4/309 +2 -3
-# larger than the page size.  fscrypt uses the block size as the data unit size
-# by default, so this tests data units that span multiple pages, both for the
-# writeback path and for the on-disk ciphertext.
+# larger than the page size.  fscrypt uses min(block size, page size) as the
+# default data unit size, so this tests 4K data units on a 32K-block

Does ext4/309 hardcode 32k ?

Quoted 2 lines

No, that's actually just an example using a block size of 32k; I'll rephrase that.

Great, thanks!

Quoted 7 lines

Additionally, the final plan has not been fully confirmed yet. I am still working on the details with Eric. It is expected that all v1 use cases will be converted to v2 later, and in the testing process, the data unit size will be explicitly specified. Please refer to:

https://lore.kernel.org/r/20261104195127.GA1971@quark

Thanks Baokun, good to know you're working on it with Eric Biggers. As the kernel patch is still under reviewing, please ping the fstests list when it's ready, in case I miss it :-D

Thanks,
Zorro

Quoted 6 lines

Cheers,
Baokun