e2fsck: set the encrypt feature if the filesystem has encrypted inodes
PATCH 1/1 e2fsck: set the encrypt feature if the filesystem has encrypted inodes
A filesystem without the encrypt feature can still contain encrypted inodes, either due to corruption or because the image was crafted that way.
e2fsck currently accepts such inodes without complaint. Since those inodes can be decrypted, this should be fixed by setting the encrypt feature on the filesystem.
Note that the Linux kernel does not currently support encryption when the block size is larger than the page size. After this change, such a filesystem will be rejected at mount time (see the kernel commit 709f0f1f1bf5, "ext4: add checks for large folio incompatibilities when BS > PS"). However, since this depends on the page size of the machine where the filesystem is mounted, and the kernel could lift that limitation in the future, e2fsck does not take it into account.
Fix this by adding a new problem (PR_1_ENCRYPT_FEATURE) and a regression test.
--- e2fsck/encrypted_files.c | 7 ++++ e2fsck/problem.c | 8 ++++ e2fsck/problem.h | 3 ++ tests/f_encrypt_feature_missing/expect.1 | 12 ++++++ tests/f_encrypt_feature_missing/expect.2 | 7 ++++ tests/f_encrypt_feature_missing/image.gz | Bin 0 -> 5566 bytes tests/f_encrypt_feature_missing/mkimage.sh | 43 +++++++++++++++++++++ tests/f_encrypt_feature_missing/name | 1 + 8 files changed, 81 insertions(+) create mode 100644 tests/f_encrypt_feature_missing/expect.1 create mode 100644 tests/f_encrypt_feature_missing/expect.2 create mode 100644 tests/f_encrypt_feature_missing/image.gz create mode 100755 tests/f_encrypt_feature_missing/mkimage.sh create mode 100644 tests/f_encrypt_feature_missing/name
e2fsck/encrypted_files.c +7 -0
@@ -389,6 +389,13 @@ int add_encrypted_file(e2fsck_t ctx, struct problem_context *pctx)return 0 ; } +/* The inode is encrypted, so the filesystem needs the encrypt feature */ +if (!ext2fs_has_feature_encrypt (ctx->fs->super) && +fix_problem (ctx,PR_1_ENCRYPT_FEATURE , pctx)) { +ext2fs_set_feature_encrypt (ctx->fs->super); +ext2fs_mark_super_dirty (ctx->fs); + } +/* Store this ino => policy_id mapping in the encrypted_file_info. */ return append_ino_and_policy_id (ctx, pctx, ino, policy_id); }
e2fsck/problem.c +8 -0
@@ -1319,6 +1319,14 @@ static struct e2fsck_problem problem_table[] = {N_ ("@i %i references EA inode but @S is missing EA_INODE feature\n" ), PROMPT_FIX, PR_PREEN_OK, 0, 0, 0 }, +/* +* Encrypted inode found but the filesystem is missing the +* encrypt feature +*/ + { PR_1_ENCRYPT_FEATURE, +N_ ("@i %i is encrypted but @S is missing ENCRYPT feature\n" ), + PROMPT_FIX, PR_PREEN_OK, 0, 0, 0 }, +/* Pass 1b errors */ /* Pass 1B: Rescan for duplicate/bad blocks */
e2fsck/problem.h +3 -0
@@ -740,6 +740,9 @@ struct problem_context {/* Inode references EA inode but ea_inode feature is not enabled */ #define PR_1_EA_INODE_FEATURE 0x010092 +/* Encrypted inode found but encrypt feature is not enabled */ +#define PR_1_ENCRYPT_FEATURE 0x010093 +/* * Pass 1b errors */
tests/f_encrypt_feature_missing/expect.1 new file +12 -0
@@ -0,0 +1,12 @@ +Pass 1: Checking inodes, blocks, and sizes +Inode 12 is encrypted but superblock is missing ENCRYPT feature +Fix? yes + +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Pass 4: Checking reference counts +Pass 5: Checking group summary information + +test_filesys: ***** FILE SYSTEM WAS MODIFIED ***** +test_filesys: 13/128 files (0.0% non-contiguous), 20/128 blocks +Exit status is 1
tests/f_encrypt_feature_missing/expect.2 new file +7 -0
@@ -0,0 +1,7 @@ +Pass 1: Checking inodes, blocks, and sizes +Pass 2: Checking directory structure +Pass 3: Checking directory connectivity +Pass 4: Checking reference counts +Pass 5: Checking group summary information +test_filesys: 13/128 files (0.0% non-contiguous), 20/128 blocks +Exit status is 0
tests/f_encrypt_feature_missing/image.gz new file binary +0 -0
tests/f_encrypt_feature_missing/mkimage.sh new file +43 -0
43 lines not on this page · raw message
tests/f_encrypt_feature_missing/name new file +1 -0
@@ -0,0 +1 @@ +encrypted inode but no encrypt feature
Signature · 1 line
2.47.3
Quoted 16 lines · Alberto Garcia
A filesystem without the encrypt feature can still contain encrypted inodes, either due to corruption or because the image was crafted that way.
e2fsck currently accepts such inodes without complaint. Since those inodes can be decrypted, this should be fixed by setting the encrypt feature on the filesystem.
Note that the Linux kernel does not currently support encryption when the block size is larger than the page size. After this change, such a filesystem will be rejected at mount time (see the kernel commit
709f0f1f1bf5, "ext4: add checks for large folio incompatibilities when BS > PS"). However, since this depends on the page size of the machine where the filesystem is mounted, and the kernel could lift that limitation in the future, e2fsck does not take it into account.
Quoted 4 lines
Fix this by adding a new problem (
PR_1_ENCRYPT_FEATURE) and a regression test.Signed-off-by Alberto Garcia <[email protected]>
It's probably safe to do this by now, as I mentioned previously
(https://lore.kernel.org/linux-ext4/20260925190751.GB2060@quark/).
A few comments on the diff:
Quoted 1 line
tests/f_encrypt_feature_missing/image.gz | Bin 0 -> 5566 bytes
This image has a 4096-byte block of incompressible ciphertext in it. An empty file would exercise this new code just as well and result in a filesystem image of around 1.5 KB.
e2fsck/encrypted_files.c int add_encrypted_file(e2fsck_t ctx, struct problem_context *pctx) · line 389
Quoted 2 lines
e2fsck/encrypted_files.c +0
-0@@ -389,6 +389,13 @@ int add_encrypted_file(e2fsck_t ctx, struct problem_context *pctx)return 0 ;
The function comment of add_encrypted_file() should be updated to mention the new behavior.
e2fsck/encrypted_files.c int add_encrypted_file(e2fsck_t ctx, struct problem_context *pctx) · line 397
Quoted 6 lines
e2fsck/encrypted_files.c +6
-0+/* The inode is encrypted, so the filesystem needs the encrypt feature */ +if (!ext2fs_has_feature_encrypt (ctx->fs->super) && +fix_problem (ctx,PR_1_ENCRYPT_FEATURE , pctx)) { +ext2fs_set_feature_encrypt (ctx->fs->super); +ext2fs_mark_super_dirty (ctx->fs); + }
I'm a little surprised the fix is to set the feature flag, rather than clear the inode. It looks like e2fsck does this for other feature flags though, so I guess this is consistent with that.
Given that it's done this way, though, let's consider the case where the user declines to fix this problem, either interactively or via 'e2fsck -n'. In that case, the problem gets reported for all thousands/millions of inodes that could have the problem. Perhaps it should be reported only for the first, by checking 'info->file_ranges_count == 0'?
Quoted 5 lines
e2fsck/problem.c +0
-0
e2fsck/problem.c static struct e2fsck_problem problem_table[] = { · line 1328
Quoted 10 lines
e2fsck/problem.c +7
-0@@ -1319,6 +1319,14 @@ static struct e2fsck_problem problem_table[] = {N_ ("@i %i references EA inode but @S is missing EA_INODE feature\n" ), PROMPT_FIX, PR_PREEN_OK, 0, 0, 0 }, +/* +* Encrypted inode found but the filesystem is missing the +* encrypt feature +*/ + { PR_1_ENCRYPT_FEATURE, +N_ ("@i %i is encrypted but @S is missing ENCRYPT feature\n" ), + PROMPT_FIX, PR_PREEN_OK, 0, 0, 0 },
Isn't this missing '/* xgettext:no-c-format */'?
- Eric
Quoted 7 lines · Eric Biggers
A few comments on the diff:
Quoted 1 line
tests/f_encrypt_feature_missing/image.gz | Bin 0 -> 5566 bytesThis image has a 4096-byte block of incompressible ciphertext in it. An empty file would exercise this new code just as well and result in a filesystem image of around 1.5 KB.
Sounds good, I'll update the test.
Quoted 5 lines
e2fsck/encrypted_files.c int add_encrypted_file(e2fsck_t ctx, struct problem_context *pctx) · line 389
Quoted 2 lines
e2fsck/encrypted_files.c +0
-0@@ -389,6 +389,13 @@ int add_encrypted_file(e2fsck_t ctx, struct problem_context *pctx)return 0 ;The function comment of
add_encrypted_file()should be updated to mention the new behavior.
Done.
Quoted 10 lines
e2fsck/encrypted_files.c int add_encrypted_file(e2fsck_t ctx, struct problem_context *pctx) · line 397
Quoted 6 lines
e2fsck/encrypted_files.c +6
-0+/* The inode is encrypted, so the filesystem needs the encrypt feature */ +if (!ext2fs_has_feature_encrypt (ctx->fs->super) && +fix_problem (ctx,PR_1_ENCRYPT_FEATURE , pctx)) { +ext2fs_set_feature_encrypt (ctx->fs->super); +ext2fs_mark_super_dirty (ctx->fs); + }I'm a little surprised the fix is to set the feature flag, rather than clear the inode. It looks like e2fsck does this for other feature flags though, so I guess this is consistent with that.
That's also what I understood from the previous conversation at
https://lore.kernel.org/linux-ext4/[email protected]/
So this patch assumes that the inode is correctly encrypted but the feature flag is missing (because it was accidentally removed or something).
A different situation would be if the inode has the encryption flag when it shouldn't, but the basic case is already handled by e2fsck (would we need an additional test for this?):
# dd if=/dev/zero of=image bs=4096 count=128 # mke2fs -b 4096 -N 128 image # debugfs -w -R "mkdir edir" image # debugfs -w -R "sif edir flags 0x800" image # e2fsck -vf image e2fsck 1.47.4 (6-Mar-2025) Pass 1: Checking inodes, blocks, and sizes Inode 12 has encrypt flag but no encryption extended attribute. Clear flag<y>?
Other possibilities:
- The flag is set, the xattr is corrupt: this is already checked (immediately before the code added by this patch).
- The flag is unset, the xattr is present: this situation is not detected, but I don't think it has any consequences?
- The flag is set, the xattr seems valid (or unknown) although the file is not supposed to be encrypted. This is the only problematic case, here e2fsck would suggest to set the feature flag. But this looks like a very unlikely scenario, or am I missing something?
Quoted 6 lines
Given that it's done this way, though, let's consider the case where the user declines to fix this problem, either interactively or via 'e2fsck -n'. In that case, the problem gets reported for all thousands/millions of inodes that could have the problem. Perhaps it should be reported only for the first, by checking '
info->file_ranges_count== 0'?
Yes, I think that's a good idea, thanks for the suggestion.
Quoted 9 lines
e2fsck/problem.c static struct e2fsck_problem problem_table[] = { · line 1328
Quoted 7 lines
e2fsck/problem.c +7
-0+/* +* Encrypted inode found but the filesystem is missing the +* encrypt feature +*/ + {PR_1_ENCRYPT_FEATURE , +N_ ("@i %i is encrypted but @S is missing ENCRYPT feature\n" ), + PROMPT_FIX, PR_PREEN_OK, 0, 0, 0 },Isn't this missing '/* xgettext:no-c-format */'?
Hmmm... I think you're right, and it looks like there are more strings with this problem on the same file. I was using other entries as an example and I didn't notice this.
Berto
Quoted 34 lines · Alberto Garcia
Quoted 10 lines
e2fsck/encrypted_files.c int add_encrypted_file(e2fsck_t ctx, struct problem_context *pctx) · line 397
Quoted 6 lines
e2fsck/encrypted_files.c +6
-0+/* The inode is encrypted, so the filesystem needs the encrypt feature */ +if (!ext2fs_has_feature_encrypt (ctx->fs->super) && +fix_problem (ctx,PR_1_ENCRYPT_FEATURE , pctx)) { +ext2fs_set_feature_encrypt (ctx->fs->super); +ext2fs_mark_super_dirty (ctx->fs); + }I'm a little surprised the fix is to set the feature flag, rather than clear the inode. It looks like e2fsck does this for other feature flags though, so I guess this is consistent with that.
That's also what I understood from the previous conversation at
https://lore.kernel.org/linux-ext4/[email protected]/So this patch assumes that the inode is correctly encrypted but the feature flag is missing (because it was accidentally removed or something).
A different situation would be if the inode has the encryption flag when it shouldn't, but the basic case is already handled by e2fsck (would we need an additional test for this?):
# dd if=/dev/zero of=image bs=4096 count=128 # mke2fs -b 4096 -N 128 image # debugfs -w -R "mkdir edir" image # debugfs -w -R "sif edir flags 0x800" image # e2fsck -vf image e2fsck 1.47.4 (6-Mar-2025) Pass 1: Checking inodes, blocks, and sizes Inode 12 has encrypt flag but no encryption extended attribute. Clear flag<y>?Other possibilities:
Quoted 8 lines
- The flag is set, the xattr is corrupt: this is already checked (immediately before the code added by this patch).
- The flag is unset, the xattr is present: this situation is not detected, but I don't think it has any consequences?
- The flag is set, the xattr seems valid (or unknown) although the file is not supposed to be encrypted. This is the only problematic case, here e2fsck would suggest to set the feature flag. But this looks like a very unlikely scenario, or am I missing something?
Yes, that sounds right. If a bit flip enables EXT4_ENCRYPT_FL on an inode, it will almost certainly trigger the NO_ENCRYPTION_POLICY case (which clears EXT4_ENCRYPT_FL) or CORRUPT_ENCRYPTION_POLICY case (which clears the inode). This new check is after that, when the inode has both EXT4_ENCRYPT_FL and a valid-looking xattr. So setting the feature flag there should be relatively safe.
- Eric