e2fsck: check for consistent encryption policies
Eric Biggers
maintainerPATCH v4 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 v3 => v4:
- Save memory in common cases by storing ranges of inodes that share the same encryption policy.
- Rebased onto latest master branch.
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 | 368 ++++++++++++++++++++++++ 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, 801 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 +368 -0
368 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);
@@ -1883,12 +1883,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++;
@@ -2017,6 +2019,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);
@@ -2201,24 +2206,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
@@ -1259,6 +1259,16 @@ static struct e2fsck_problem problem_table[] = {N_ ("@d %p has the casefold flag, but the\ncasefold feature is not enabled. " ), PROMPT_CLEAR_FLAG, 0, 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 */
@@ -1785,6 +1795,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
@@ -701,6 +701,11 @@ struct problem_context {/* Casefold flag set, but file system is missing the casefold feature */ #define PR_1_CASEFOLD_FEATURE 0x010089 +/* Inode has encrypt flag but no encryption extended attribute */ +#define PR_1_MISSING_ENCRYPTION_XATTR 0x01008A + +/* Encrypted inode has corrupt encryption extended attribute */ +#define PR_1_CORRUPT_ENCRYPTION_XATTR 0x01008B/* * Pass 1b errors
@@ -1017,6 +1022,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
Quoted 27 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
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
Signed-off-by Eric Biggers <[email protected]>
Looks much better. One minor nit below, but at this point you could add:
Quoted 16 lines
---
Changes v3 => v4:
- Save memory in common cases by storing ranges of inodes that share the same encryption policy.
- Rebased onto latest master branch.
e2fsck/encrypted_files.c new file +0
-0
e2fsck/encrypted_files.c line 58
Quoted 7 lines
e2fsck/encrypted_files.c new file +6
-0@@ -0,0 +1,368 @@ +/* A range of inodes which share the same encryption policy */ +struct encrypted_file_range { +ext2_ino_t first_ino; +ext2_ino_t last_ino; +__u32 policy_id; +};
This seems like a clear win... As long as we have at least two inodes in a row with the same policy ID it will take less space than the previous version of the patch.
e2fsck/encrypted_files.c line 199
Quoted 7 lines
e2fsck/encrypted_files.c new file +7
-0+static int handle_nomem (e2fsck_t ctx,struct problem_context *pctx) +{ +fix_problem (ctx,PR_1_ALLOCATE_ENCRYPTED_DIRLIST , pctx); +/* Should never get here */ + ctx->flags |=E2F_FLAG_ABORT ; +return 0 ; +}
It would be useful if the error message for PR_1_ALLOCATE_ENCRYPTED_DIRLIST printed the actual allocation size that failed, so that the user has some idea of how much memory would be needed. The underlying ext2fs_resize_mem() code doesn't print anything, just returns EXT2_ET_NO_MEMORY.
Quoted 29 lines
e2fsck/encrypted_files.c new file +29
-0+static int append_ino_and_policy_id (e2fsck_t ctx,struct problem_context *pctx, +ext2_ino_t ino,__u32 policy_id) +{ +struct encrypted_file_info *info = ctx->encrypted_files; +struct encrypted_file_range *range; + +/* See if we can just extend the last range. */ +if (info->file_ranges_count >0 ) { + range = &info->file_ranges[info->file_ranges_count -1 ]; + +if (ino <= range->last_ino) { +/* Should never get here */ +fatal_error (ctx, +"Encrypted inodes processed out of order" ); + } + +if (ino == range->last_ino +1 && + policy_id == range->policy_id) { + range->last_ino++; +return 0 ; + } + } +/* Nope, a new range is needed. */ + +if (info->file_ranges_count == info->file_ranges_capacity) { +/* Double the capacity by default. */ +size_t new_capacity = info->file_ranges_capacity *2 ; + +/* ... but go from 0 to 128 right away. */
e2fsck/encrypted_files.c line 239
Quoted 10 lines
e2fsck/encrypted_files.c new file +10
-0+if (new_capacity <128 ) + new_capacity =128 ; + +/* We won't need more than the filesystem's inode count. */ +if (new_capacity > ctx->fs->super->s_inodes_count) + new_capacity = ctx->fs->super->s_inodes_count; + +/* To be safe, ensure the capacity really increases. */ +if (new_capacity < info->file_ranges_capacity +1 ) + new_capacity = info->file_ranges_capacity +1 ;
Not sure how this could happen (more inodes than s_inodes_count?), but better safe than sorry I guess?
e2fsck/encrypted_files.c line 245
Quoted 5 lines
e2fsck/encrypted_files.c new file +5
-0+if (ext2fs_resize_mem (info->file_ranges_capacity * +sizeof (*range), + new_capacity *sizeof (*range), + &info->file_ranges) !=0 ) +return handle_nomem (ctx, pctx);
This is the only thing that gives me pause, potentially having a huge allocation, but I think the RLE encoding of entries and the fact we have overwhelmingly 64-bit CPUs means we could still run with swap (on an internal NVMe M.2 device) if really needed. A problem to fix if it ever actually rears its head, so long as there is a decent error message printed.
Quoted 8 lines
e2fsck/encrypted_files.c new file +8
-0+/* +* Find the ID of an inode's encryption policy, using the information saved +* earlier. +* +* If the inode is encrypted, returns the policy ID or +* UNRECOGNIZED_ENCRYPTION_POLICY. Else, returns NO_ENCRYPTION_POLICY. +*/ +__u32 find_encryption_policy (e2fsck_t ctx,ext2_ino_t ino)
e2fsck/encrypted_files.c line 323
Quoted 10 lines
e2fsck/encrypted_files.c new file +10
-0+{ +const struct encrypted_file_info *info = ctx->encrypted_files; +size_t l, r; + +if (info ==NULL ) +return NO_ENCRYPTION_POLICY ; + l =0 ; + r = info->file_ranges_count; +while (l < r) { +size_t m = l + (r - l) /2 ;
Using the RLE encoding for the entries should also speed up searching here considerably. In theory, for a single-user Android filesystem there might only be one or two entries here. It would be interesting to run this on some of your filesystems to see what the average count of inodes per entry is.
e2fsck/encrypted_files.c line 335
Quoted 12 lines
e2fsck/encrypted_files.c new file +12
-0+const struct encrypted_file_range *range = + &info->file_ranges[m]; + +if (ino < range->first_ino) + r = m; +else if (ino > range->last_ino) + l = m +1 ; +else +return range->policy_id; + } +return NO_ENCRYPTION_POLICY ; +}
Cheers, Andreas
Eric Biggers
maintainerQuoted 18 lines · Andreas Dilger
e2fsck/encrypted_files.c line 58
Quoted 13 lines
e2fsck/encrypted_files.c new file +6
-0@@ -0,0 +1,368 @@ +/* A range of inodes which share the same encryption policy */ +struct encrypted_file_range { +ext2_ino_t first_ino; +ext2_ino_t last_ino; +__u32 policy_id; +};This seems like a clear win... As long as we have at least two inodes in a row with the same policy ID it will take less space than the previous version of the patch.
Quoted 13 lines
e2fsck/encrypted_files.c line 199
Quoted 7 lines
e2fsck/encrypted_files.c new file +7
-0+static int handle_nomem (e2fsck_t ctx,struct problem_context *pctx) +{ +fix_problem (ctx,PR_1_ALLOCATE_ENCRYPTED_DIRLIST , pctx); +/* Should never get here */ + ctx->flags |=E2F_FLAG_ABORT ; +return 0 ; +}It would be useful if the error message for
PR_1_ALLOCATE_ENCRYPTED_DIRLISTprinted the actual allocation size that failed, so that the user has some idea of how much memory would be needed. The underlyingext2fs_resize_mem()code doesn't print anything, just returnsEXT2_ET_NO_MEMORY.
Yes, I can make it print the number of bytes using %N.
Quoted 10 lines
Quoted 10 lines
e2fsck/encrypted_files.c new file +10
-0+if (info->file_ranges_count == info->file_ranges_capacity) { +/* Double the capacity by default. */ +size_t new_capacity = info->file_ranges_capacity *2 ; + +/* ... but go from 0 to 128 right away. */ +if (new_capacity <128 ) + new_capacity =128 ; + +/* We won't need more than the filesystem's inode count. */ +if (new_capacity > ctx->fs->super->s_inodes_count)
Quoted 9 lines
e2fsck/encrypted_files.c line 239
Quoted 5 lines
e2fsck/encrypted_files.c new file +5
-0+ new_capacity = ctx->fs->super->s_inodes_count; + +/* To be safe, ensure the capacity really increases. */ +if (new_capacity < info->file_ranges_capacity +1 ) + new_capacity = info->file_ranges_capacity +1 ;Not sure how this could happen (more inodes than
s_inodes_count?), but better safe than sorry I guess?
Either that, or an integer overflow. It shouldn't really happen, but I think we should have this check to be safe.
Quoted 29 lines
e2fsck/encrypted_files.c line 245
Quoted 5 lines
e2fsck/encrypted_files.c new file +5
-0+if (ext2fs_resize_mem (info->file_ranges_capacity * +sizeof (*range), + new_capacity *sizeof (*range), + &info->file_ranges) !=0 ) +return handle_nomem (ctx, pctx);This is the only thing that gives me pause, potentially having a huge allocation, but I think the RLE encoding of entries and the fact we have overwhelmingly 64-bit CPUs means we could still run with swap (on an internal NVMe M.2 device) if really needed. A problem to fix if it ever actually rears its head, so long as there is a decent error message printed.
Quoted 16 lines
e2fsck/encrypted_files.c new file +16
-0+/* +* Find the ID of an inode's encryption policy, using the information saved +* earlier. +* +* If the inode is encrypted, returns the policy ID or +* UNRECOGNIZED_ENCRYPTION_POLICY. Else, returns NO_ENCRYPTION_POLICY. +*/ +__u32 find_encryption_policy (e2fsck_t ctx,ext2_ino_t ino) +{ +const struct encrypted_file_info *info = ctx->encrypted_files; +size_t l, r; + +if (info ==NULL ) +return NO_ENCRYPTION_POLICY ; + l =0 ; + r = info->file_ranges_count;
Quoted 9 lines
e2fsck/encrypted_files.c line 323
Quoted 2 lines
e2fsck/encrypted_files.c new file +2
-0+while (l < r) { +size_t m = l + (r - l) /2 ;Using the RLE encoding for the entries should also speed up searching here considerably. In theory, for a single-user Android filesystem there might only be one or two entries here. It would be interesting to run this on some of your filesystems to see what the average count of inodes per entry is.
On a freshly reset Android device I'm seeing 58 ranges for 4705 encrypted inodes, so it's not quite *that* good, but it still helps a lot.
Note that there are actually 4 encryption policies on a single-user Android
device: system device-encrypted, user device-encrypted, user credential-encrypted, and (recently added) per-boot encrypted.
- Eric