xfstests: test f2fs compression+encryption
Eric Biggers
maintainercover xfstests: test f2fs compression+encryption
Add a test which verifies that encryption is done correctly when a file on f2fs uses both compression and encryption at the same time.
Patches 1-4 add prerequisites for the test, while patch 5 adds the actual test. Patch 2 also fixes a bug which could cause the existing test generic/602 to fail in extremely rare cases. See the commit messages for details.
The new test passes both with and without the inlinecrypt mount option. It requires CONFIG_F2FS_FS_COMPRESSION=y.
I'd appreciate the f2fs developers taking a look.
Note, there is a quirk where the IVs in compressed files are off by one from the "natural" values. It's still secure, though it made the test slightly harder to write. I'm not sure how intentional this quirk was.
Eric Biggers (5):
fscrypt-crypt-util: clean up parsing --block-size and --inode-number
fscrypt-crypt-util: fix IV incrementing for --iv-ino-lblk-32
fscrypt-crypt-util: add --block-number option common/f2fs: add _require_scratch_f2fs_compression()
f2fs: verify ciphertext of compressed+encrypted file
common/config | 1 + common/f2fs | 27 +++++ src/fscrypt-crypt-util.c | 98 ++++++++++++------ tests/f2fs/002 | 217 +++++++++++++++++++++++++++++++++++++++ tests/f2fs/002.out | 21 ++++ tests/f2fs/group | 1 + 6 files changed, 334 insertions(+), 31 deletions(-) create mode 100644 common/f2fs create mode 100755 tests/f2fs/002 create mode 100644 tests/f2fs/002.out
Signature · 1 line
2.28.0
Eric Biggers
maintainerPATCH 1/5 fscrypt-crypt-util: clean up parsing --block-size and --inode-number
From: Eric Biggers <[email protected]>
For --block-size, check for strtoul() reporting an overflow.
For --inode-number, check for strtoull() reporting an overflow.
Also, move the check for 32-bit inode numbers into a more logical place (the place where we check the encryption format-specific limitations).
--- src/fscrypt-crypt-util.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-)
src/fscrypt-crypt-util.c +10 -15
@@ -26,6 +26,7 @@#include <linux/types.h> #include <stdarg.h> #include <stdbool.h> +#include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h>
@@ -1756,18 +1757,6 @@ static u8 parse_mode_number(const char *arg)return num; } -static u32 parse_inode_number (const char *arg) -{ -char *tmp; -unsigned long long num =strtoull (arg, &tmp,10 ); - -if (num <=0 || *tmp) -die ("Invalid inode number: %s" , arg); -if ((u32 )num != num) -die ("Inode number %s is too large; must be 32-bit" , arg); -return num; -} -struct key_and_iv_params {u8 master_key[MAX_KEY_SIZE ];int master_key_size;
@@ -1777,7 +1766,7 @@ struct key_and_iv_params {bool file_nonce_specified;bool iv_ino_lblk_64;bool iv_ino_lblk_32; -u32 inode_number; +u64 inode_number;u8 fs_uuid[UUID_SIZE ];bool fs_uuid_specified; };
@@ -1842,6 +1831,8 @@ static void get_key_and_iv(const struct key_and_iv_params *params,die ("%s requires --inode-number" , opt);if (params->mode_num ==0 )die ("%s requires --mode-num" , opt); +if (params->inode_number >UINT32_MAX ) +die ("%s can't use --inode-number > UINT32_MAX" , opt); }switch (params->kdf) {
@@ -1957,8 +1948,9 @@ int main(int argc, char *argv[])while ((c =getopt_long (argc, argv,"" , longopts,NULL )) !=-1 ) {switch (c) {case OPT_BLOCK_SIZE : + errno =0 ; block_size =strtoul (optarg, &tmp,10 ); -if (block_size <=0 || *tmp) +if (block_size <=0 || *tmp || errno)die ("Invalid block size: %s" , optarg);break ;case OPT_DECRYPT :
@@ -1980,7 +1972,10 @@ int main(int argc, char *argv[])usage (stdout);return 0 ;case OPT_INODE_NUMBER : - params.inode_number =parse_inode_number (optarg); + errno =0 ; + params.inode_number =strtoull (optarg, &tmp,10 ); +if (params.inode_number <=0 || *tmp || errno) +die ("Invalid inode number: %s" , optarg);break ;case OPT_IV_INO_LBLK_32 : params.iv_ino_lblk_32 = true;
Signature · 1 line
2.28.0
Eric Biggers
maintainerPATCH 2/5 fscrypt-crypt-util: fix IV incrementing for --iv-ino-lblk-32
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
fscrypt-crypt-util treats the "block number" part of the IV as 64-bit when incrementing it. That's wrong for --iv-ino-lblk-32 and --iv-ino-lblk-64, as in those cases the block number should be 32-bit.
Fix this by using the correct length for the block number.
For --iv-ino-lblk-64 this doesn't actually matter, since in that case the block number starts at 0 and never exceeds UINT32_MAX.
But for --iv-ino-lblk-32, the hashed inode number gets added to the original block number to produce the IV block number, which can later wrap around from UINT32_MAX to 0. As a result, this change fixes generic/602, though since the wraparound case isn't specifically tested currently, the chance of failure was extremely small.
--- src/fscrypt-crypt-util.c | 54 ++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 16 deletions(-)
src/fscrypt-crypt-util.c +38 -16
@@ -1692,16 +1692,32 @@ static const struct fscrypt_cipher *find_fscrypt_cipher(const char *name)return NULL ; } -struct fscrypt_iv { -union { -__le64 block_num; -u8 bytes[32 ]; +union fscrypt_iv { +/* usual IV format */ +struct { +/* logical block number within the file */ +__le64 block_number; + +/* per-file nonce; only set in DIRECT_KEY mode */ +u8 nonce[FILE_NONCE_SIZE ]; + }; +/* IV format for IV_INO_LBLK_* modes */ +struct { +/* +* IV_INO_LBLK_64: logical block number within the file +* IV_INO_LBLK_32: hashed inode number + logical block number +* within the file, mod 2^32 +*/ +__le32 block_number32; + +/* IV_INO_LBLK_64: inode number */ +__le32 inode_number; }; };static void crypt_loop (const struct fscrypt_cipher *cipher,const u8 *key, -struct fscrypt_iv *iv,bool decrypting, -size_t block_size,size_t padding) +union fscrypt_iv *iv,bool decrypting, +size_t block_size,size_t padding,bool is_bnum_32bit) {u8 *buf =xmalloc (block_size);size_t res;
@@ -1718,13 +1734,18 @@ static void crypt_loop(const struct fscrypt_cipher *cipher, const u8 *key,memset (&buf[res],0 , crypt_len - res);if (decrypting) - cipher->decrypt (key, iv->bytes, buf, buf, crypt_len); + cipher->decrypt (key, (u8 *)iv, buf, buf, crypt_len);else - cipher->encrypt (key, iv->bytes, buf, buf, crypt_len); + cipher->encrypt (key, (u8 *)iv, buf, buf, crypt_len);full_write (STDOUT_FILENO , buf, crypt_len); - iv->block_num =cpu_to_le64 (le64_to_cpu (iv->block_num) +1 ); +if (is_bnum_32bit) + iv->block_number32 =cpu_to_le32 ( +le32_to_cpu (iv->block_number32) +1 ); +else + iv->block_number =cpu_to_le64 ( +le64_to_cpu (iv->block_number) +1 ); }free (buf); }
@@ -1806,7 +1827,7 @@ static u32 hash_inode_number(const struct key_and_iv_params *params)*/ static void get_key_and_iv (const struct key_and_iv_params *params,u8 *real_key,size_t real_key_size, -struct fscrypt_iv *iv) +union fscrypt_iv *iv) {bool file_nonce_in_iv = false;struct aes_key aes_key;
@@ -1860,14 +1881,14 @@ static void get_key_and_iv(const struct key_and_iv_params *params, info[infolen++] = params->mode_num;memcpy (&info[infolen], params->fs_uuid,UUID_SIZE ); infolen +=UUID_SIZE ; -put_unaligned_le32 (params->inode_number, &iv->bytes[4 ]); + iv->inode_number =cpu_to_le32 (params->inode_number); }else if (params->iv_ino_lblk_32) { info[infolen++] =HKDF_CONTEXT_IV_INO_LBLK_32_KEY ; info[infolen++] = params->mode_num;memcpy (&info[infolen], params->fs_uuid,UUID_SIZE ); infolen +=UUID_SIZE ; -put_unaligned_le32 (hash_inode_number (params), - iv->bytes); + iv->block_number32 = +cpu_to_le32 (hash_inode_number (params)); }else if (params->mode_num !=0 ) { info[infolen++] =HKDF_CONTEXT_DIRECT_KEY ; info[infolen++] = params->mode_num;
@@ -1888,7 +1909,7 @@ static void get_key_and_iv(const struct key_and_iv_params *params, }if (file_nonce_in_iv && params->file_nonce_specified) -memcpy (&iv->bytes[8 ], params->file_nonce,FILE_NONCE_SIZE ); +memcpy (iv->nonce, params->file_nonce,FILE_NONCE_SIZE ); }enum {
@@ -1928,7 +1949,7 @@ int main(int argc, char *argv[])size_t padding =0 ;const struct fscrypt_cipher *cipher;u8 real_key[MAX_KEY_SIZE ]; -struct fscrypt_iv iv; +union fscrypt_iv iv;char *tmp;int c;
@@ -2025,6 +2046,7 @@ int main(int argc, char *argv[])get_key_and_iv (¶ms, real_key, cipher->keysize, &iv); -crypt_loop (cipher, real_key, &iv, decrypting, block_size, padding); +crypt_loop (cipher, real_key, &iv, decrypting, block_size, padding, + params.iv_ino_lblk_64 || params.iv_ino_lblk_32);return 0 ; }
Signature · 1 line
2.28.0
Eric Biggers
maintainerPATCH 3/5 fscrypt-crypt-util: add --block-number option
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Currently fscrypt-crypt-util assumes that the number of the first block encrypted/decrypted is 0. I.e., it replicates either contents encryption from the start of a file, or encryption of a filename.
However, to easily test compression+encryption on f2fs, we need the ability to specify a different starting block number.
Add a --block-number option which does this.
--- src/fscrypt-crypt-util.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
src/fscrypt-crypt-util.c +20 -1
@@ -59,6 +59,8 @@ static void usage(FILE *fp)"WARNING: this program is only meant for testing, not for \"real\" use!\n" "\n" "Options:\n" +" --block-number=BNUM Starting block number for IV generation.\n" +" Default: 0\n" " --block-size=BLOCK_SIZE Encrypt each BLOCK_SIZE bytes independently.\n" " Default: 4096 bytes\n" " --decrypt Decrypt instead of encrypt\n"
@@ -1787,6 +1789,7 @@ struct key_and_iv_params {bool file_nonce_specified;bool iv_ino_lblk_64;bool iv_ino_lblk_32; +u64 block_number;u64 inode_number;u8 fs_uuid[UUID_SIZE ];bool fs_uuid_specified;
@@ -1839,6 +1842,9 @@ static void get_key_and_iv(const struct key_and_iv_params *params,memset (iv,0 ,sizeof (*iv)); +/* Overridden later for iv_ino_lblk_{64,32} */ + iv->block_number =cpu_to_le64 (params->block_number); +if (params->iv_ino_lblk_64 || params->iv_ino_lblk_32) {const char *opt = params->iv_ino_lblk_64 ?"--iv-ino-lblk-64" :"--iv-ino-lblk-32" ;
@@ -1852,6 +1858,8 @@ static void get_key_and_iv(const struct key_and_iv_params *params,die ("%s requires --inode-number" , opt);if (params->mode_num ==0 )die ("%s requires --mode-num" , opt); +if (params->block_number >UINT32_MAX ) +die ("%s can't use --block-number > UINT32_MAX" , opt);if (params->inode_number >UINT32_MAX )die ("%s can't use --inode-number > UINT32_MAX" , opt); }
@@ -1881,6 +1889,7 @@ static void get_key_and_iv(const struct key_and_iv_params *params, info[infolen++] = params->mode_num;memcpy (&info[infolen], params->fs_uuid,UUID_SIZE ); infolen +=UUID_SIZE ; + iv->block_number32 =cpu_to_le32 (params->block_number); iv->inode_number =cpu_to_le32 (params->inode_number); }else if (params->iv_ino_lblk_32) { info[infolen++] =HKDF_CONTEXT_IV_INO_LBLK_32_KEY ;
@@ -1888,7 +1897,9 @@ static void get_key_and_iv(const struct key_and_iv_params *params,memcpy (&info[infolen], params->fs_uuid,UUID_SIZE ); infolen +=UUID_SIZE ; iv->block_number32 = -cpu_to_le32 (hash_inode_number (params)); +cpu_to_le32 (hash_inode_number (params) + + params->block_number); + iv->inode_number =0 ; }else if (params->mode_num !=0 ) { info[infolen++] =HKDF_CONTEXT_DIRECT_KEY ; info[infolen++] = params->mode_num;
@@ -1913,6 +1924,7 @@ static void get_key_and_iv(const struct key_and_iv_params *params, }enum { +OPT_BLOCK_NUMBER ,OPT_BLOCK_SIZE ,OPT_DECRYPT ,OPT_FILE_NONCE ,
@@ -1927,6 +1939,7 @@ enum { };static const struct option longopts[] = { + {"block-number" , required_argument,NULL ,OPT_BLOCK_NUMBER }, {"block-size" , required_argument,NULL ,OPT_BLOCK_SIZE }, {"decrypt" , no_argument,NULL ,OPT_DECRYPT }, {"file-nonce" , required_argument,NULL ,OPT_FILE_NONCE },
@@ -1968,6 +1981,12 @@ int main(int argc, char *argv[])while ((c =getopt_long (argc, argv,"" , longopts,NULL )) !=-1 ) {switch (c) { +case OPT_BLOCK_NUMBER : + errno =0 ; + params.block_number =strtoull (optarg, &tmp,10 ); +if (*tmp || errno) +die ("Invalid block number: %s" , optarg); +break ;case OPT_BLOCK_SIZE : errno =0 ; block_size =strtoul (optarg, &tmp,10 );
Signature · 1 line
2.28.0
Eric Biggers
maintainerPATCH 4/5 common/f2fs: add _require_scratch_f2fs_compression()
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
Create the file common/f2fs, which will contain f2fs-specific utilities.
Then add a function _require_scratch_f2fs_compression(), which checks for f2fs compression support on the scratch filesystem.
--- common/f2fs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 common/f2fs
common/f2fs new file +27 -0
@@ -0,0 +1,27 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-only +# Copyright 2020 Google LLC + +# Require f2fs compression support on the scratch filesystem. +# Optionally, check for support for a specific compression algorithm. +_require_scratch_f2fs_compression() +{ + local algorithm=$1 + + _require_scratch + + if [ ! -e /sys/fs/f2fs/features/compression ]; then + _notrun "Kernel doesn't support f2fs compression" + fi + # Note: '-O compression' is only accepted when used in + # combination with extra_attr. + if ! _scratch_mkfs -O compression,extra_attr >> $seqres.full; then + _notrun "f2fs-tools doesn't support compression" + fi + if [ -n "$algorithm" ]; then + if ! _scratch_mount "-o compress_algorithm=$algorithm"; then + _notrun "Kernel doesn't support $algorithm compression algorithm for f2fs" + fi + _scratch_unmount + fi +}
Signature · 1 line
2.28.0
Eric Biggers
maintainerPATCH 5/5 f2fs: verify ciphertext of compressed+encrypted file
In reply to Eric BiggersFrom: Eric Biggers <[email protected]>
In Linux v5.6, f2fs added support for per-file compression. f2fs compression can be used in combination with the existing f2fs encryption support (a.k.a. fscrypt), in which case the compressed data is encrypted rather than the uncompressed data.
We need to verify that the encryption is actually being done as expected in this case. So add a test which verifies it.
For now this is a f2fs-specific test. It's possible that ext4 will implement compression in the same way as f2fs (in which case this could be made a generic test), but for now there are no plans for that.
This complements the existing ciphertext verification tests, e.g. generic/548, which don't handle compression. Encryption+compression has some unique considerations, so it requires its own test.
--- common/config | 1 + tests/f2fs/002 | 217 +++++++++++++++++++++++++++++++++++++++++++++ tests/f2fs/002.out | 21 +++++ tests/f2fs/group | 1 + 4 files changed, 240 insertions(+) create mode 100755 tests/f2fs/002 create mode 100644 tests/f2fs/002.out
common/config +1 -0
@@ -202,6 +202,7 @@ export GETRICHACL_PROG="$(type -P getrichacl)" export SETRICHACL_PROG="$(type -P setrichacl)" export KEYCTL_PROG="$(type -P keyctl)" export XZ_PROG="$(type -P xz)" +export LZ4_PROG="$(type -P lz4)" export FLOCK_PROG="$(type -P flock)" export LDD_PROG="$(type -P ldd)" export TIMEOUT_PROG="$(type -P timeout)"
tests/f2fs/002 new file +217 -0
217 lines not on this page · raw message
tests/f2fs/002.out new file +21 -0
@@ -0,0 +1,21 @@ +QA output created by 002 + +# Creating filesystem that supports encryption and compression + +# Creating directory + +# Enabling encryption on the directory + +# Enabling compression on the directory + +# Creating compressed+encrypted file + +# Getting file's encryption nonce + +# Dumping the file's raw data + +# Decrypting the file's data + +# Decompressing the file's data + +# Comparing to original data
tests/f2fs/group +1 -0
@@ -4,3 +4,4 @@ # - comment line before each group is "new" description # 001 auto quick rw +002 auto quick rw encrypt compress
Signature · 1 line
2.28.0
Eric Biggers
maintainerQuoted 17 lines · Eric Biggers
Add a test which verifies that encryption is done correctly when a file on f2fs uses both compression and encryption at the same time.
Patches 1-4 add prerequisites for the test, while patch 5 adds the actual test. Patch 2 also fixes a bug which could cause the existing test generic/602 to fail in extremely rare cases. See the commit messages for details.
The new test passes both with and without the inlinecrypt mount option. It requires
CONFIG_F2FS_FS_COMPRESSION=y.I'd appreciate the f2fs developers taking a look.
Note, there is a quirk where the IVs in compressed files are off by one from the "natural" values. It's still secure, though it made the test slightly harder to write. I'm not sure how intentional this quirk was.
Quoted 6 lines
Eric Biggers (5):
fscrypt-crypt-util: clean up parsing --block-size and --inode-number
fscrypt-crypt-util: fix IV incrementing for --iv-ino-lblk-32
fscrypt-crypt-util: add --block-number option common/f2fs: add_require_scratch_f2fs_compression()
f2fs: verify ciphertext of compressed+encrypted file
Jaegeuk, Chao, Daeho: any comments on this?
- Eric
Sorry for the late reply. We had a long holiday last week. The patch looks good to me~
Thanks,
2020년 10월 7일 (수) 오후 12:49, Eric Biggers <[email protected]>님이 작성:
Quoted 35 lines · Eric Biggers
Quoted 17 lines
Add a test which verifies that encryption is done correctly when a file on f2fs uses both compression and encryption at the same time.
Patches 1-4 add prerequisites for the test, while patch 5 adds the actual test. Patch 2 also fixes a bug which could cause the existing test generic/602 to fail in extremely rare cases. See the commit messages for details.
The new test passes both with and without the inlinecrypt mount option. It requires
CONFIG_F2FS_FS_COMPRESSION=y.I'd appreciate the f2fs developers taking a look.
Note, there is a quirk where the IVs in compressed files are off by one from the "natural" values. It's still secure, though it made the test slightly harder to write. I'm not sure how intentional this quirk was.
Quoted 6 lines
Eric Biggers (5):
fscrypt-crypt-util: clean up parsing --block-size and --inode-number
fscrypt-crypt-util: fix IV incrementing for --iv-ino-lblk-32
fscrypt-crypt-util: add --block-number option common/f2fs: add_require_scratch_f2fs_compression()
f2fs: verify ciphertext of compressed+encrypted fileJaegeuk, Chao, Daeho: any comments on this?
- Eric
_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel