From: Artem Blagodarenko Split monolithic definition of dx_root struct to separate dx_root_info from fake struct ext4_dir_entry2 for improved code readability. This allows "." and ".." dirents to have different sizes if necessary, since we can't assume the rec_len 12 if dx_root dirents have dirdata. Adds dx_get_dx_info() accessor instead of complex typecast at callers. Also add the ext4_dir_entry_len() helper, which dx_get_dx_info() uses to step over the ".." entry now that it may carry dirdata. dx_get_dx_info() finds ".." through the rec_len of ".", as the other users of the root block do, and checks that the root has room for dx_root_info, two dx_entry slots and the dx_tail. make_indexed_dir() calls it before it changes the directory, so a failure leaves the directory as it was. Signed-off-by: Pravin Shelar Signed-off-by: Artem Blagodarenko Assisted-by: Claude:claude-opus-5-5 --- fs/ext4/ext4.h | 31 ++++++ fs/ext4/namei-test.c | 2 +- fs/ext4/namei.c | 219 ++++++++++++++++++++++++++++++------------- fs/ext4/namei.h | 23 +++-- 4 files changed, 199 insertions(+), 76 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index a75a8cedd52f..aa45735a9bb7 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -4501,6 +4501,37 @@ static inline int ext4_dirent_get_data_len(struct ext4_dir_entry_2 *de, return dlen; } +/* + * ext4_dir_entry_len() - Compute the required rec_len for a directory entry. + * @de: directory entry (used to read name_len and any dirdata length) + * @blocksize: size of the buffer @de lives in (the real directory block + * size, or the smaller inline-data buffer size for inline + * directories) -- used only to decode @de->rec_len's "0/65535 + * means rest of buffer" sentinel correctly. + * @dir: directory inode (may be NULL for '.' and '..' entries, which + * never carry the casefold+fscrypt hash regardless of the + * directory's feature flags) + * + * Returns the minimum record length needed to hold @de, rounded up to the + * directory alignment and including room for the casefold+fscrypt hash if + * the directory requires it. An entry that carries the hash as a CFHASH + * record has it counted in its dirdata length. Any other entry in such a + * directory may hold the hash at the fixed offset after the name, which is + * where readers look when there is no valid CFHASH record, so the result + * always covers that too. + */ +static inline unsigned int ext4_dir_entry_len(struct ext4_dir_entry_2 *de, + unsigned int blocksize, + const struct inode *dir) +{ + unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, blocksize); + unsigned int dirdata = ext4_dirent_get_data_len(de, rec_len); + + return max(ext4_dir_rec_len(de->name_len + dirdata, NULL), + ext4_dir_rec_len(de->name_len, dir)); +} + + DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next); DECLARE_IOMAP_ITER_NEXT(ext4_iomap_next_report); DECLARE_IOMAP_ITER_NEXT(ext4_iomap_buffered_write_next); diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c index 30cba531a621..2a918fd12b05 100644 --- a/fs/ext4/namei-test.c +++ b/fs/ext4/namei-test.c @@ -1493,7 +1493,7 @@ static void test_namei_check_dx_root_and_probe(struct kunit *test) ext4_htree_next_block_test(dir, 0x40000000U, frame, frames, NULL), 0); } - dx_release_test(frames); + dx_release_test(dir, frames); } /* diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index 8fc39c91b6ef..edbe033b623e 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -443,6 +443,46 @@ static inline int ext4_handle_dirty_dx_node(handle_t *handle, return ext4_handle_dirty_metadata(handle, inode, bh); } +/* + * dx_get_dx_info() - locate the dx_root_info in an htree root block + * @dir: directory inode + * @de_buf: start of the root block (the '.' entry) + * + * '..' is found through the rec_len of '.', as every other user of the + * root block does. The rec_len of '..' covers the rest of the block, so + * dx_root_info starts at the real length of '..'. '.' and '..' never carry + * the casefold+fscrypt hash, so their length is computed with a NULL @dir. + * The root must have room for dx_root_info, two dx_entry slots (the first + * split needs them) and, with metadata_csum, the dx_tail. + */ +static struct dx_root_info *dx_get_dx_info(struct inode *dir, void *de_buf) +{ + unsigned int blocksize = dir->i_sb->s_blocksize; + struct ext4_dir_entry_2 *de = de_buf; + unsigned int off, rlen, need; + + rlen = ext4_rec_len_from_disk(de->rec_len, blocksize); + if (rlen < EXT4_BASE_DIR_LEN || rlen + EXT4_BASE_DIR_LEN > blocksize || + ext4_dir_entry_len(de, blocksize, NULL) > rlen) + return ERR_PTR(-EFSCORRUPTED); + off = rlen; + + de = (struct ext4_dir_entry_2 *)((char *)de_buf + off); + rlen = ext4_rec_len_from_disk(de->rec_len, blocksize); + if (rlen < EXT4_BASE_DIR_LEN || off + rlen > blocksize || + ext4_dir_entry_len(de, blocksize, NULL) > rlen) + return ERR_PTR(-EFSCORRUPTED); + off += ext4_dir_entry_len(de, blocksize, NULL); + + need = sizeof(struct dx_root_info) + 2 * sizeof(struct dx_entry); + if (ext4_has_feature_metadata_csum(dir->i_sb)) + need += sizeof(struct dx_tail); + if (off + need > blocksize) + return ERR_PTR(-EFSCORRUPTED); + + return (struct dx_root_info *)((char *)de_buf + off); +} + /* * Debug */ @@ -628,7 +668,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, { unsigned count, indirect, level, i; struct dx_entry *at, *entries, *p, *q, *m; - struct dx_root *root; + struct dx_root_info *info; struct dx_frame *frame = frame_in; struct dx_frame *ret_err = ERR_PTR(ERR_BAD_DX_DIR); u32 hash; @@ -640,23 +680,31 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, if (IS_ERR(frame->bh)) return (struct dx_frame *) frame->bh; - root = (struct dx_root *) frame->bh->b_data; - if (root->info.hash_version != DX_HASH_TEA && - root->info.hash_version != DX_HASH_HALF_MD4 && - root->info.hash_version != DX_HASH_LEGACY && - root->info.hash_version != DX_HASH_SIPHASH) { - ext4_warning_inode(dir, "Unrecognised inode hash code %u", - root->info.hash_version); + info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)frame->bh->b_data); + if (IS_ERR(info)) + goto fail; + if (info->info_length != sizeof(struct dx_root_info)) { + ext4_warning_inode(dir, "corrupted htree: info_length %u", + info->info_length); + goto fail; + } + if (info->hash_version != DX_HASH_TEA && + info->hash_version != DX_HASH_HALF_MD4 && + info->hash_version != DX_HASH_LEGACY && + info->hash_version != DX_HASH_SIPHASH) { + ext4_warning(dir->i_sb, + "Unrecognised inode hash code %d for directory #%llu", + info->hash_version, dir->i_ino); goto fail; } if (ext4_hash_in_dirent(dir)) { - if (root->info.hash_version != DX_HASH_SIPHASH) { + if (info->hash_version != DX_HASH_SIPHASH) { ext4_warning_inode(dir, "Hash in dirent, but hash is not SIPHASH"); goto fail; } } else { - if (root->info.hash_version == DX_HASH_SIPHASH) { + if (info->hash_version == DX_HASH_SIPHASH) { ext4_warning_inode(dir, "Hash code is SIPHASH, but hash not in dirent"); goto fail; @@ -664,7 +712,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, } if (fname) hinfo = &fname->hinfo; - hinfo->hash_version = root->info.hash_version; + hinfo->hash_version = info->hash_version; if (hinfo->hash_version <= DX_HASH_TEA) hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed; @@ -680,13 +728,13 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, } hash = hinfo->hash; - if (root->info.unused_flags & 1) { + if (info->unused_flags & 1) { ext4_warning_inode(dir, "Unimplemented hash flags: %#06x", - root->info.unused_flags); + info->unused_flags); goto fail; } - indirect = root->info.indirect_levels; + indirect = info->indirect_levels; if (indirect >= ext4_dir_htree_level(dir->i_sb)) { ext4_warning(dir->i_sb, "Directory (ino: %llu) htree depth %#06x exceed" @@ -699,14 +747,12 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, goto fail; } - entries = (struct dx_entry *)(((char *)&root->info) + - root->info.info_length); + entries = (struct dx_entry *)(((char *)info) + info->info_length); - if (dx_get_limit(entries) != dx_root_limit(dir, - root->info.info_length)) { + if (dx_get_limit(entries) != dx_root_limit(dir, info->info_length)) { ext4_warning_inode(dir, "dx entry: limit %u != root limit %u", dx_get_limit(entries), - dx_root_limit(dir, root->info.info_length)); + dx_root_limit(dir, info->info_length)); goto fail; } @@ -783,7 +829,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir, return ret_err; } -static void dx_release(struct dx_frame *frames) +static void dx_release(struct inode *dir, struct dx_frame *frames) { struct dx_root_info *info; int i; @@ -792,7 +838,18 @@ static void dx_release(struct dx_frame *frames) if (frames[0].bh == NULL) return; - info = &((struct dx_root *)frames[0].bh->b_data)->info; + info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)frames[0].bh->b_data); + if (IS_ERR(info)) { + /* Cannot determine indirect_levels; release all frame buffers + * that may have been loaded to avoid leaking them. */ + for (i = 0; i < EXT4_HTREE_LEVEL; i++) { + if (!frames[i].bh) + break; + brelse(frames[i].bh); + frames[i].bh = NULL; + } + return; + } /* save local copy, "info" may be freed after brelse() */ indirect_levels = info->indirect_levels; for (i = 0; i <= indirect_levels; i++) { @@ -1101,12 +1158,12 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, (count && ((hashval & 1) == 0))) break; } - dx_release(frames); + dx_release(dir, frames); dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, " "next hash: %x\n", count, *next_hash)); return count; errout: - dx_release(frames); + dx_release(dir, frames); return (err); } EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_htree_fill_tree); @@ -1609,7 +1666,7 @@ static struct buffer_head * ext4_dx_find_entry(struct inode *dir, errout: dxtrace(printk(KERN_DEBUG "%s not found\n", fname->usr_fname->name)); success: - dx_release(frames); + dx_release(dir, frames); return bh; } @@ -2015,44 +2072,57 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname, return err ? err : err2; } -static bool ext4_check_dx_root(struct inode *dir, struct dx_root *root) +/* + * Check '.' and '..' at the start of a one-block directory that is about to + * become an htree root. On success *@dotdot is '..' and *@entry is the first + * entry after it, which lies before the checksum tail of the block. + */ +static bool ext4_check_dx_root(struct inode *dir, + struct ext4_dir_entry_2 *dot_de, + struct ext4_dir_entry_2 **dotdot, + struct ext4_dir_entry_2 **entry) { - struct fake_dirent *fde; const char *error_msg; - unsigned int rlen; unsigned int blocksize = dir->i_sb->s_blocksize; - char *blockend = (char *)root + dir->i_sb->s_blocksize; + char *end = (char *)dot_de + blocksize; + struct ext4_dir_entry_2 *dotdot_de, *de; + unsigned int rlen; + + if (ext4_has_feature_metadata_csum(dir->i_sb)) + end -= sizeof(struct ext4_dir_entry_tail); - fde = &root->dot; - if (unlikely(fde->name_len != 1)) { + if (unlikely(dot_de->name_len != 1)) { error_msg = "invalid name_len for '.'"; goto corrupted; } - if (unlikely(strncmp(root->dot_name, ".", fde->name_len))) { + if (unlikely(strncmp(dot_de->name, ".", dot_de->name_len))) { error_msg = "invalid name for '.'"; goto corrupted; } - rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize); - if (unlikely((char *)fde + rlen >= blockend)) { + rlen = ext4_rec_len_from_disk(dot_de->rec_len, blocksize); + if (unlikely(rlen < EXT4_BASE_DIR_LEN || + (char *)dot_de + rlen + EXT4_BASE_DIR_LEN > end)) { error_msg = "invalid rec_len for '.'"; goto corrupted; } - fde = &root->dotdot; - if (unlikely(fde->name_len != 2)) { + dotdot_de = ext4_next_entry(dot_de, blocksize); + if (unlikely(dotdot_de->name_len != 2)) { error_msg = "invalid name_len for '..'"; goto corrupted; } - if (unlikely(strncmp(root->dotdot_name, "..", fde->name_len))) { + if (unlikely(strncmp(dotdot_de->name, "..", dotdot_de->name_len))) { error_msg = "invalid name for '..'"; goto corrupted; } - rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize); - if (unlikely((char *)fde + rlen >= blockend)) { + rlen = ext4_rec_len_from_disk(dotdot_de->rec_len, blocksize); + de = ext4_next_entry(dotdot_de, blocksize); + if (unlikely(rlen < EXT4_BASE_DIR_LEN || (char *)de >= end)) { error_msg = "invalid rec_len for '..'"; goto corrupted; } - + *dotdot = dotdot_de; + *entry = de; return true; corrupted: @@ -2070,16 +2140,15 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname, struct inode *inode, struct buffer_head *bh) { struct buffer_head *bh2; - struct dx_root *root; struct dx_frame frames[EXT4_HTREE_LEVEL], *frame; struct dx_entry *entries; - struct ext4_dir_entry_2 *de, *de2; + struct ext4_dir_entry_2 *de, *de2, *dot_de, *dotdot_de; char *data2, *top; unsigned len; int retval; unsigned blocksize; ext4_lblk_t block; - struct fake_dirent *fde; + struct dx_root_info *dx_info; int csum_size = 0; if (ext4_has_feature_metadata_csum(inode->i_sb)) @@ -2096,17 +2165,22 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname, return retval; } - root = (struct dx_root *) bh->b_data; - if (!ext4_check_dx_root(dir, root)) { + dot_de = (struct ext4_dir_entry_2 *)bh->b_data; + if (!ext4_check_dx_root(dir, dot_de, &dotdot_de, &de)) { brelse(bh); return -EFSCORRUPTED; } + /* Find room for the hashing info before the directory is changed */ + dx_info = dx_get_dx_info(dir, dot_de); + if (IS_ERR(dx_info)) { + EXT4_ERROR_INODE(dir, "no room for htree root info"); + brelse(bh); + return PTR_ERR(dx_info); + } + /* The 0th block becomes the root, move the dirents out */ - fde = &root->dotdot; - de = (struct ext4_dir_entry_2 *)((char *)fde + - ext4_rec_len_from_disk(fde->rec_len, blocksize)); - len = ((char *) root) + (blocksize - csum_size) - (char *) de; + len = ((char *)dot_de) + (blocksize - csum_size) - (char *)de; /* Allocate new block for the 0th block's dirents */ bh2 = ext4_append(handle, dir, &block); @@ -2137,24 +2211,26 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname, ext4_initialize_dirent_tail(bh2, blocksize); /* Initialize the root; the dot dirents already exist */ - de = (struct ext4_dir_entry_2 *) (&root->dotdot); - de->rec_len = ext4_rec_len_to_disk( - blocksize - ext4_dir_rec_len(2, NULL), blocksize); - memset (&root->info, 0, sizeof(root->info)); - root->info.info_length = sizeof(root->info); + dotdot_de->rec_len = + ext4_rec_len_to_disk(blocksize - le16_to_cpu(dot_de->rec_len), + blocksize); + + /* initialize hashing info */ + memset(dx_info, 0, sizeof(*dx_info)); + dx_info->info_length = sizeof(*dx_info); if (ext4_hash_in_dirent(dir)) - root->info.hash_version = DX_HASH_SIPHASH; + dx_info->hash_version = DX_HASH_SIPHASH; else - root->info.hash_version = + dx_info->hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; - entries = root->entries; + entries = (void *)dx_info + sizeof(*dx_info); dx_set_block(entries, 1); dx_set_count(entries, 1); - dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info))); + dx_set_limit(entries, dx_root_limit(dir, sizeof(*dx_info))); /* Initialize as for dx_probe */ - fname->hinfo.hash_version = root->info.hash_version; + fname->hinfo.hash_version = dx_info->hash_version; if (fname->hinfo.hash_version <= DX_HASH_TEA) fname->hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; fname->hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; @@ -2197,7 +2273,7 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname, */ if (retval) ext4_mark_inode_dirty(handle, dir); - dx_release(frames); + dx_release(dir, frames); brelse(bh2); return retval; } @@ -2464,7 +2540,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname, if (restart || err) goto journal_error; } else { - struct dx_root *dxroot; + struct dx_root_info *info; memcpy((char *) entries2, (char *) entries, icount * sizeof(struct dx_entry)); dx_set_limit(entries2, dx_node_limit(dir)); @@ -2472,11 +2548,17 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname, /* Set up root */ dx_set_count(entries, 1); dx_set_block(entries + 0, newblock); - dxroot = (struct dx_root *)frames[0].bh->b_data; - dxroot->info.indirect_levels += 1; + info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *) + frames[0].bh->b_data); + if (IS_ERR(info)) { + err = PTR_ERR(info); + brelse(bh2); + goto journal_error; + } + info->indirect_levels += 1; dxtrace(printk(KERN_DEBUG "Creating %d level index...\n", - dxroot->info.indirect_levels)); + info->indirect_levels)); err = ext4_handle_dirty_dx_node(handle, dir, frame->bh); if (err) { brelse(bh2); @@ -2500,7 +2582,7 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname, ext4_std_error(dir->i_sb, err); /* this is a no-op if err == 0 */ cleanup: brelse(bh); - dx_release(frames); + dx_release(dir, frames); /* @restart is true means htree-path has been changed, we need to * repeat dx_probe() to find out valid htree-path */ @@ -4153,9 +4235,9 @@ struct dx_frame *dx_probe_test(struct ext4_filename *fname, struct inode *dir, } EXPORT_SYMBOL_FOR_EXT4_TEST(dx_probe_test); -void dx_release_test(struct dx_frame *frames) +void dx_release_test(struct inode *dir, struct dx_frame *frames) { - dx_release(frames); + dx_release(dir, frames); } EXPORT_SYMBOL_FOR_EXT4_TEST(dx_release_test); @@ -4215,7 +4297,10 @@ EXPORT_SYMBOL_FOR_EXT4_TEST(do_split_test); bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root) { - return ext4_check_dx_root(dir, root); + struct ext4_dir_entry_2 *dotdot_de, *de; + + return ext4_check_dx_root(dir, (struct ext4_dir_entry_2 *)root, + &dotdot_de, &de); } EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_check_dx_root_test); diff --git a/fs/ext4/namei.h b/fs/ext4/namei.h index d5c261d0faf3..e66c5d1c2aae 100644 --- a/fs/ext4/namei.h +++ b/fs/ext4/namei.h @@ -43,19 +43,26 @@ struct dx_entry { * dx_root_info is laid out so that if it should somehow get overlaid by a * dirent the two low bits of the hash version will be zero. Therefore, the * hash version mod 4 should never be 0. Sincerely, the paranoia department. + * + * It follows the '..' entry of the htree root block. With dirdata, '.' and + * '..' can be longer than the 12 bytes struct dx_root assumes, so the kernel + * finds it with dx_get_dx_info() and never through struct dx_root. */ +struct dx_root_info { + __le32 reserved_zero; + u8 hash_version; + u8 info_length; /* 8 */ + u8 indirect_levels; + u8 unused_flags; +}; + +/* Layout of an htree root block whose '.' and '..' carry no dirdata. */ struct dx_root { struct fake_dirent dot; char dot_name[4]; struct fake_dirent dotdot; char dotdot_name[4]; - struct dx_root_info { - __le32 reserved_zero; - u8 hash_version; - u8 info_length; /* 8 */ - u8 indirect_levels; - u8 unused_flags; - } info; + struct dx_root_info info; struct dx_entry entries[]; }; @@ -192,7 +199,7 @@ int ext4_dx_csum_verify_test(struct inode *inode, struct dx_frame *dx_probe_test(struct ext4_filename *fname, struct inode *dir, struct dx_hash_info *hinfo, struct dx_frame *frame_in); -void dx_release_test(struct dx_frame *frames); +void dx_release_test(struct inode *dir, struct dx_frame *frames); int ext4_htree_next_block_test(struct inode *dir, __u32 hash, struct dx_frame *frame, struct dx_frame *frames, __u32 *start_hash); -- 2.52.0