e2fsck: check for consistent encryption policies
Eric Biggers
maintainerPATCH v3 1/1 e2fsck: check for consistent encryption policies
From: Eric Biggers <[email protected]>
By design, the kernel enforces that all files in an encrypted directory use the same encryption policy as the directory. It's not possible to violate this constraint using syscalls. Lookups of files that violate this constraint also fail, in case the disk was manipulated.
But this constraint can also be violated by accidental filesystem corruption. E.g., a power cut when using ext4 without a journal might leave new files without the encryption bit and/or xattr. Thus, it's important that e2fsck correct this condition.
Therefore, this patch makes the following changes to e2fsck:
- During pass 1 (inode table scan), create a map from inode number to encryption policy for all encrypted inodes. But it's optimized so that the full xattrs aren't saved but rather only 32-bit "policy IDs", since usually many inodes share the same encryption policy. Also, if an encryption xattr is missing, offer to clear the encrypt flag. If an encryption xattr is clearly corrupt, offer to clear the inode.
- During pass 2 (directory structure check), use the map to verify that all regular files, directories, and symlinks in encrypted directories use the directory's encryption policy. Offer to clear any directory entries for which this isn't the case.
Add a new test "f_bad_encryption" to test the new behavior.
Due to the new checks, it was also necessary to update the existing test "f_short_encrypted_dirent" to add an encryption xattr to the test file, since it was missing one before, which is now considered invalid.
Google-Bug-Id: 135138675
---
Changes v2 => v3:
- Rebased onto latest master branch.
- A few minor cleanups in encrypted_files.c (no behavior changes)
Changes v1 => v2:
- For each encrypted inode, store only a 32-bit encryption policy ID, not the full encryption xattr. A map from policy => policy ID, implemented using an rbtree, is used to assign policy IDs.
- If an inode with the encrypt bit is missing its encryption xattr, just clear the encrypt bit rather than clearing the whole inode. Note that any encrypted directory entries that reference the inode will also be cleared by the encryption policy check in pass 2.
- Rename struct encrypted_files => struct encrypted_file_info.
- Make the encrypted file information structures private to encrypted_files.c.
- Other cleanups.
e2fsck/Android.bp | 1 + e2fsck/Makefile.in | 18 +- e2fsck/e2fsck.c | 5 +- e2fsck/e2fsck.h | 22 +- e2fsck/encrypted_files.c | 335 ++++++++++++++++++++++++ e2fsck/pass1.c | 29 +- e2fsck/pass2.c | 106 ++++++-- e2fsck/problem.c | 20 ++ e2fsck/problem.h | 11 + po/POTFILES.in | 1 + tests/f_bad_encryption/expect.1 | 115 ++++++++ tests/f_bad_encryption/expect.2 | 7 + tests/f_bad_encryption/image.gz | Bin 0 -> 2009 bytes tests/f_bad_encryption/mkimage.sh | 143 ++++++++++ tests/f_bad_encryption/name | 1 + tests/f_short_encrypted_dirent/expect.1 | 2 +- tests/f_short_encrypted_dirent/expect.2 | 2 +- tests/f_short_encrypted_dirent/image.gz | Bin 925 -> 1008 bytes 18 files changed, 768 insertions(+), 50 deletions(-) create mode 100644 e2fsck/encrypted_files.c create mode 100644 tests/f_bad_encryption/expect.1 create mode 100644 tests/f_bad_encryption/expect.2 create mode 100644 tests/f_bad_encryption/image.gz create mode 100755 tests/f_bad_encryption/mkimage.sh create mode 100644 tests/f_bad_encryption/name
e2fsck/Android.bp +1 -0
@@ -34,6 +34,7 @@ cc_defaults { "sigcatcher.c", "readahead.c", "extents.c", + "encrypted_files.c", ], cflags: [ "-Wno-sign-compare",
e2fsck/Makefile.in +16 -2
@@ -63,7 +63,7 @@ OBJS= unix.o e2fsck.o super.o pass1.o pass1b.o pass2.o \ dx_dirinfo.o ehandler.o problem.o message.o quota.o recovery.o \ region.o revoke.o ea_refcount.o rehash.o \ logfile.o sigcatcher.o $(MTRACE_OBJ) readahead.o \ - extents.o + extents.o encrypted_files.o PROFILED_OBJS= profiled/unix.o profiled/e2fsck.o \ profiled/super.o profiled/pass1.o profiled/pass1b.o \
@@ -74,7 +74,8 @@ PROFILED_OBJS= profiled/unix.o profiled/e2fsck.o \ profiled/recovery.o profiled/region.o profiled/revoke.o \ profiled/ea_refcount.o profiled/rehash.o \ profiled/logfile.o profiled/sigcatcher.o \ - profiled/readahead.o profiled/extents.o + profiled/readahead.o profiled/extents.o \ + profiled/encrypted_files.o SRCS= $(srcdir)/e2fsck.c \ $(srcdir)/super.c \
@@ -103,6 +104,7 @@ SRCS= $(srcdir)/e2fsck.c \ $(srcdir)/logfile.c \ $(srcdir)/quota.c \ $(srcdir)/extents.c \ + $(srcdir)/encrypted_files.c \ $(MTRACE_SRC) all:: profiled $(PROGS) e2fsck $(MANPAGES) $(FMANPAGES)
@@ -572,3 +574,15 @@ extents.o: $(srcdir)/extents.c $(top_builddir)/lib/config.h \ $(top_builddir)/lib/support/prof_err.h $(top_srcdir)/lib/support/quotaio.h \ $(top_srcdir)/lib/support/dqblk_v2.h \ $(top_srcdir)/lib/support/quotaio_tree.h $(srcdir)/problem.h +encrypted_files.o: $(srcdir)/encrypted_files.c $(top_builddir)/lib/config.h \ + $(top_builddir)/lib/dirpaths.h $(srcdir)/e2fsck.h \ + $(top_srcdir)/lib/ext2fs/ext2_fs.h $(top_builddir)/lib/ext2fs/ext2_types.h \ + $(top_srcdir)/lib/ext2fs/ext2fs.h $(top_srcdir)/lib/ext2fs/ext3_extents.h \ + $(top_srcdir)/lib/et/com_err.h $(top_srcdir)/lib/ext2fs/ext2_io.h \ + $(top_builddir)/lib/ext2fs/ext2_err.h \ + $(top_srcdir)/lib/ext2fs/ext2_ext_attr.h $(top_srcdir)/lib/ext2fs/hashmap.h \ + $(top_srcdir)/lib/ext2fs/bitops.h $(top_srcdir)/lib/support/profile.h \ + $(top_builddir)/lib/support/prof_err.h $(top_srcdir)/lib/support/quotaio.h \ + $(top_srcdir)/lib/support/dqblk_v2.h \ + $(top_srcdir)/lib/support/quotaio_tree.h $(srcdir)/problem.h \ + $(top_srcdir)/lib/ext2fs/rbtree.h
e2fsck/e2fsck.c +1 -4
@@ -138,6 +138,7 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)ext2fs_u32_list_free (ctx->dirs_to_hash); ctx->dirs_to_hash =0 ; } +destroy_encrypted_file_info (ctx);/* * Clear the array of invalid meta-data flags
@@ -154,10 +155,6 @@ errcode_t e2fsck_reset_context(e2fsck_t ctx)ext2fs_free_mem (&ctx->invalid_inode_table_flag); ctx->invalid_inode_table_flag =0 ; } -if (ctx->encrypted_dirs) { -ext2fs_u32_list_free (ctx->encrypted_dirs); - ctx->encrypted_dirs =0 ; - }if (ctx->inode_count) {ext2fs_free_icount (ctx->inode_count); ctx->inode_count =0 ;
e2fsck/e2fsck.h +20 -2
@@ -135,6 +135,8 @@ struct dx_dirblock_info {#define DX_FLAG_FIRST 4#define DX_FLAG_LAST 8 +struct encrypted_file_info ; +#define RESOURCE_TRACK #ifdef RESOURCE_TRACK
@@ -328,6 +330,11 @@ struct e2fsck_struct {*/ ext2_u32_list dirs_to_hash; +/* +* Encrypted file information +*/ +struct encrypted_file_info *encrypted_files; +/* * Tuning parameters */
@@ -390,7 +397,6 @@ struct e2fsck_struct {int ext_attr_ver;profile_t profile;int blocks_per_page; -ext2_u32_list encrypted_dirs;/* Reserve blocks for root and l+f re-creation */ blk64_t root_repair_block, lnf_repair_block;
@@ -505,8 +511,20 @@ extern ea_key_t ea_refcount_intr_next(ext2_refcount_t refcount,extern const char *ehandler_operation (const char *op);extern void ehandler_init (io_channel channel); -/* extents.c */ +/* encrypted_files.c */ +struct problem_context ; +int add_encrypted_file (e2fsck_t ctx,struct problem_context *pctx); + +#define NO_ENCRYPTION_POLICY ((__u32)-1) +#define CORRUPT_ENCRYPTION_POLICY ((__u32)-2) +#define UNRECOGNIZED_ENCRYPTION_POLICY ((__u32)-3) +__u32 find_encryption_policy (e2fsck_t ctx,ext2_ino_t ino); + +void destroy_encryption_policy_map (e2fsck_t ctx); +void destroy_encrypted_file_info (e2fsck_t ctx); + +/* extents.c */ errcode_t e2fsck_rebuild_extents_later (e2fsck_t ctx,ext2_ino_t ino);int e2fsck_ino_will_be_rebuilt (e2fsck_t ctx,ext2_ino_t ino);void e2fsck_pass1e (e2fsck_t ctx);
e2fsck/encrypted_files.c new file +335 -0
335 lines not on this page · raw message
e2fsck/pass1.c +8 -21
@@ -27,6 +27,7 @@* - A bitmap of which blocks are in use by two inodes (block_dup_map) * - The data blocks of the directory inodes. (dir_map) * - Ref counts for ea_inodes. (ea_inode_refs) +* - The encryption policy ID of each encrypted inode. (encrypted_files) * * Pass 1 is designed to stash away enough information so that the * other passes should not need to read in the inode information
@@ -78,7 +79,6 @@ static void mark_table_blocks(e2fsck_t ctx);static void alloc_bb_map (e2fsck_t ctx);static void alloc_imagic_map (e2fsck_t ctx);static void mark_inode_bad (e2fsck_t ctx,ino_t ino); -static void add_encrypted_dir (e2fsck_t ctx,ino_t ino);static void handle_fs_bad_blocks (e2fsck_t ctx);static void process_inodes (e2fsck_t ctx,char *block_buf);static EXT2_QSORT_TYPE process_inode_cmp (const void *a,const void *b);
@@ -1871,12 +1871,14 @@ void e2fsck_pass1(e2fsck_t ctx) failed_csum =0 ; } +if ((inode->i_flags &EXT4_ENCRYPT_FL ) && +add_encrypted_file (ctx, &pctx) <0 ) + goto clear_inode; +if (LINUX_S_ISDIR (inode->i_mode)) {ext2fs_mark_inode_bitmap2 (ctx->inode_dir_map, ino);e2fsck_add_dir_info (ctx, ino,0 ); ctx->fs_directory_count++; -if (inode->i_flags &EXT4_ENCRYPT_FL ) -add_encrypted_dir (ctx, ino); }else if (LINUX_S_ISREG (inode->i_mode)) {ext2fs_mark_inode_bitmap2 (ctx->inode_reg_map, ino); ctx->fs_regular_count++;
@@ -2005,6 +2007,9 @@ void e2fsck_pass1(e2fsck_t ctx) ctx->block_ea_map =0 ; } +/* We don't need the encryption policy => ID map any more */ +destroy_encryption_policy_map (ctx); +if (ctx->flags &E2F_FLAG_RESIZE_INODE ) {clear_problem_context (&pctx); pctx.errcode =ext2fs_create_resize_inode (fs);
@@ -2189,24 +2194,6 @@ static void mark_inode_bad(e2fsck_t ctx, ino_t ino)ext2fs_mark_inode_bitmap2 (ctx->inode_bad_map, ino); } -static void add_encrypted_dir (e2fsck_t ctx,ino_t ino) -{ -struct problem_context pctx; - -if (!ctx->encrypted_dirs) { - pctx.errcode =ext2fs_u32_list_create (&ctx->encrypted_dirs,0 ); -if (pctx.errcode) - goto error; - } - pctx.errcode =ext2fs_u32_list_add (ctx->encrypted_dirs, ino); -if (pctx.errcode ==0 ) -return ; -error: -fix_problem (ctx,PR_1_ALLOCATE_ENCRYPTED_DIRLIST , &pctx); -/* Should never get here */ - ctx->flags |=E2F_FLAG_ABORT ; -} -/* * This procedure will allocate the inode "bb" (badblock) map table */
e2fsck/pass2.c +87 -19
160 lines not on this page · raw message
e2fsck/problem.c +20 -0
@@ -1246,6 +1246,16 @@ static struct e2fsck_problem problem_table[] = {N_ ("@i %i has @x marked uninitialized at @b %c (len %N). " ), PROMPT_CLEAR, PR_PREEN_OK, 0, 0, 0 }, +/* Inode has encrypt flag but no encryption extended attribute */ + { PR_1_MISSING_ENCRYPTION_XATTR, +N_ ("@i %i has encrypt flag but no encryption @a.\n" ), + PROMPT_FIX, 0, 0, 0, 0 }, + +/* Encrypted inode has corrupt encryption extended attribute */ + { PR_1_CORRUPT_ENCRYPTION_XATTR, +N_ ("Encrypted @i %i has corrupt encryption @a.\n" ), + PROMPT_CLEAR_INODE, 0, 0, 0, 0 }, +/* Pass 1b errors */ /* Pass 1B: Rescan for duplicate/bad blocks */
@@ -1772,6 +1782,16 @@ static struct e2fsck_problem problem_table[] = {N_ ("Encrypted @E is too short.\n" ), PROMPT_CLEAR, 0, 0, 0, 0 }, +/* Encrypted directory contains unencrypted file */ + { PR_2_UNENCRYPTED_FILE, +N_ ("Encrypted @E references unencrypted @i %Di.\n" ), + PROMPT_CLEAR, 0, 0, 0, 0 }, + +/* Encrypted directory contains file with different encryption policy */ + { PR_2_INCONSISTENT_ENCRYPTION_POLICY, +N_ ("Encrypted @E references @i %Di, which has a different encryption policy.\n" ), + PROMPT_CLEAR, 0, 0, 0, 0 }, +/* Pass 3 errors */ /* Pass 3: Checking directory connectivity */
e2fsck/problem.h +11 -0
@@ -696,6 +696,11 @@ struct problem_context {/* Offer to clear uninitialized flag on an extent */ #define PR_1_CLEAR_UNINIT_EXTENT 0x010087 +/* Inode has encrypt flag but no encryption extended attribute */ +#define PR_1_MISSING_ENCRYPTION_XATTR 0x010088 + +/* Encrypted inode has corrupt encryption extended attribute */ +#define PR_1_CORRUPT_ENCRYPTION_XATTR 0x010089/* * Pass 1b errors
@@ -1012,6 +1017,12 @@ struct problem_context {/* Encrypted directory entry is too short */ #define PR_2_BAD_ENCRYPTED_NAME 0x020050 +/* Encrypted directory contains unencrypted file */ +#define PR_2_UNENCRYPTED_FILE 0x020051 + +/* Encrypted directory contains file with different encryption policy */ +#define PR_2_INCONSISTENT_ENCRYPTION_POLICY 0x020052 +/* * Pass 3 errors */
po/POTFILES.in +1 -0
@@ -5,6 +5,7 @@ e2fsck/e2fsck.c e2fsck/ea_refcount.c e2fsck/ehandler.c e2fsck/emptydir.c +e2fsck/encrypted_files.c e2fsck/extend.c e2fsck/extents.c e2fsck/flushb.c
tests/f_bad_encryption/expect.1 new file +115 -0
115 lines not on this page · raw message
tests/f_bad_encryption/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: 25/128 files (0.0% non-contiguous), 37/128 blocks +Exit status is 0
tests/f_bad_encryption/image.gz new file binary +0 -0
tests/f_bad_encryption/mkimage.sh new file +143 -0
143 lines not on this page · raw message
tests/f_bad_encryption/name new file +1 -0
@@ -0,0 +1 @@ +missing, corrupt, and inconsistent encryption policies
tests/f_short_encrypted_dirent/expect.1 +1 -1
@@ -13,5 +13,5 @@ Inode 13 ref count is 2, should be 1. Fix? yes Pass 5: Checking group summary information test_filesys: ***** FILE SYSTEM WAS MODIFIED ***** -test_filesys: 13/16 files (0.0% non-contiguous), 23/100 blocks +test_filesys: 13/16 files (0.0% non-contiguous), 24/100 blocks Exit status is 1
tests/f_short_encrypted_dirent/expect.2 +1 -1
@@ -3,5 +3,5 @@ Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information -test_filesys: 13/16 files (0.0% non-contiguous), 23/100 blocks +test_filesys: 13/16 files (0.0% non-contiguous), 24/100 blocks Exit status is 0
tests/f_short_encrypted_dirent/image.gz binary +0 -0
Signature · 1 line
2.23.0.187.g17f5b7556c-goog
Eric Biggers
maintainerQuoted 26 lines · Eric Biggers
From: Eric Biggers <[email protected]>
By design, the kernel enforces that all files in an encrypted directory use the same encryption policy as the directory. It's not possible to violate this constraint using syscalls. Lookups of files that violate this constraint also fail, in case the disk was manipulated.
But this constraint can also be violated by accidental filesystem corruption. E.g., a power cut when using ext4 without a journal might leave new files without the encryption bit and/or xattr. Thus, it's important that e2fsck correct this condition.
Therefore, this patch makes the following changes to e2fsck:
- During pass 1 (inode table scan), create a map from inode number to encryption policy for all encrypted inodes. But it's optimized so that the full xattrs aren't saved but rather only 32-bit "policy IDs", since usually many inodes share the same encryption policy. Also, if an encryption xattr is missing, offer to clear the encrypt flag. If an encryption xattr is clearly corrupt, offer to clear the inode.
- During pass 2 (directory structure check), use the map to verify that all regular files, directories, and symlinks in encrypted directories use the directory's encryption policy. Offer to clear any directory entries for which this isn't the case.
Quoted 6 lines
Add a new test "
f_bad_encryption" to test the new behavior.Due to the new checks, it was also necessary to update the existing test "
f_short_encrypted_dirent" to add an encryption xattr to the test file, since it was missing one before, which is now considered invalid.
Any comments on this patch?
- Eric
Quoted 27 lines · Eric Biggers
Quoted 26 lines · Eric Biggers
From: Eric Biggers <[email protected]>
By design, the kernel enforces that all files in an encrypted directory use the same encryption policy as the directory. It's not possible to violate this constraint using syscalls. Lookups of files that violate this constraint also fail, in case the disk was manipulated.
But this constraint can also be violated by accidental filesystem corruption. E.g., a power cut when using ext4 without a journal might leave new files without the encryption bit and/or xattr. Thus, it's important that e2fsck correct this condition.
Therefore, this patch makes the following changes to e2fsck:
- During pass 1 (inode table scan), create a map from inode number to encryption policy for all encrypted inodes. But it's optimized so that the full xattrs aren't saved but rather only 32-bit "policy IDs", since usually many inodes share the same encryption policy. Also, if an encryption xattr is missing, offer to clear the encrypt flag. If an encryption xattr is clearly corrupt, offer to clear the inode.
- During pass 2 (directory structure check), use the map to verify that all regular files, directories, and symlinks in encrypted directories use the directory's encryption policy. Offer to clear any directory entries for which this isn't the case.
Quoted 8 lines
Quoted 6 lines
Add a new test "
f_bad_encryption" to test the new behavior.Due to the new checks, it was also necessary to update the existing test "
f_short_encrypted_dirent" to add an encryption xattr to the test file, since it was missing one before, which is now considered invalid.Any comments on this patch?
I didn't see the original email for this patch, but I found it on patchworks.
One change is needed if the filesystem is very large and has a lot of encrypted files. While your typical use case is going to be Android handsets, on the flip side I'm often dealing with filesystems with a few billion files in them, and there are definitely users that want to use the on-disk encryption.
Quoted 10 lines
e2fsck/encrypted_files.c line 257
Quoted 10 lines
e2fsck/encrypted_files.c new file +10
-0+/* Append the (ino, policy_id) pair to the list. */ +if (info->files_count == info->files_capacity) { +size_t new_capacity = info->files_capacity *2 ; + +if (new_capacity <128 ) + new_capacity =128 ; + +if (ext2fs_resize_mem (info->files_capacity *sizeof (*file), + new_capacity *sizeof (*file), + &info->files) !=0 )
If the number of files in the array get very large, then doubling the array size at the end may consume a *lot* of memory. It would be somewhat better to cap new_capacity by the number of inodes in the filesystem, and better yet scale the array size by a fraction of the total number of inodes that have already been processed, but this array might still be several GB of RAM.
What about using run-length encoding for this? It is unlikely that many different encryption policies are in a filesystem, and inodes tend to be allocated in groups by users, so it is likely that you will get large runs of inodes with the same policy_id, and this could save considerable space.
Cheers, Andreas
Theodore Y. Ts'o
maintainerQuoted 11 lines · Andreas Dilger
If the number of files in the array get very large, then doubling the array size at the end may consume a *lot* of memory. It would be somewhat better to cap
new_capacityby the number of inodes in the filesystem, and better yet scale the array size by a fraction of the total number of inodes that have already been processed, but this array might still be several GB of RAM.What about using run-length encoding for this? It is unlikely that many different encryption policies are in a filesystem, and inodes tend to be allocated in groups by users, so it is likely that you will get large runs of inodes with the same
policy_id, and this could save considerable space.
One approach that we could use is to allocate a separate bitmap for each policy. EXT2FS_BMAP_RBTREE effectively will use RLE. The downside is that if the inodes are not sparse, it will be quite heavyweight; each extent costs 40 bytes.
So for file system with a very large number of policies (as opposed less than two or three, which will be the case with the vast majority of Android devices) this could be quite expensive.
Of course, we don't have to use an rbtree; the bitarray will be created sequentially, so in theory we could create a new bitmap backend which uses a sorted list, which is O(1) for ordered insert and o(log n) for lookups. That could be about 12 bytes per extent. And of course, we don't have to implement the sorted list back end right away, switching it is just a matter of changing a parameter to ext2fs_alloc_generic_bitmap().
- Ted
Eric Biggers
maintainerQuoted 22 lines · Theodore Y. Ts'o
Quoted 11 lines · Andreas Dilger
If the number of files in the array get very large, then doubling the array size at the end may consume a *lot* of memory. It would be somewhat better to cap
new_capacityby the number of inodes in the filesystem, and better yet scale the array size by a fraction of the total number of inodes that have already been processed, but this array might still be several GB of RAM.What about using run-length encoding for this? It is unlikely that many different encryption policies are in a filesystem, and inodes tend to be allocated in groups by users, so it is likely that you will get large runs of inodes with the same
policy_id, and this could save considerable space.One approach that we could use is to allocate a separate bitmap for each policy.
EXT2FS_BMAP_RBTREEeffectively will use RLE. The downside is that if the inodes are not sparse, it will be quite heavyweight; each extent costs 40 bytes.So for file system with a very large number of policies (as opposed less than two or three, which will be the case with the vast majority of Android devices) this could be quite expensive.
Quoted 8 lines
Of course, we don't have to use an rbtree; the bitarray will be created sequentially, so in theory we could create a new bitmap backend which uses a sorted list, which is O(1) for ordered insert and o(log n) for lookups. That could be about 12 bytes per extent. And of course, we don't have to implement the sorted list back end right away, switching it is just a matter of changing a parameter to
ext2fs_alloc_generic_bitmap().
I don't think a bitmap per policy is a good idea, even if it was actually represented as an rbtree or a sorted list. The problem is that to look up an inode's encryption policy ID that way, you'd have to iterate through every encryption policy, of which there could be a huge number.
Instead I'll try just changing:
struct encrypted_file {
ext2_ino_t ino;
__u32 policy_id;
};to the following:
struct encrypted_file_range {
ext2_ino_t first_ino;
ext2_ino_t last_ino;
__u32 policy_id;
};- Eric