ext4: Add checks to validate extent entries.
PATCH 1/1 ext4: Add checks to validate extent entries.
This patch adds check to validate the extent entries along with extent headers. Should handle crash with corrupt filesystem.
--- fs/ext4/extents.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 71 insertions(+), 10 deletions(-)
fs/ext4/extents.c +71 -10
@@ -301,7 +301,64 @@ ext4_ext_max_entries(struct inode *inode, int depth)return max; } -static int __ext4_ext_check_header (const char *function,struct inode *inode, +static int ext4_valid_extent (struct inode *inode,struct ext4_extent *ext) +{ +ext4_fsblk_t block =ext_pblock (ext); +int len =ext4_ext_get_actual_len (ext); +struct ext4_super_block *es =EXT4_SB (inode->i_sb)->s_es; +if (unlikely (block <le32_to_cpu (es->s_first_data_block) || + ((block + len) >ext4_blocks_count (es)))) +return 0 ; +else +return 1 ; +} + +static int ext4_valid_extent_idx (struct inode *inode, +struct ext4_extent_idx *ext_idx) +{ +ext4_fsblk_t block =idx_pblock (ext_idx); +struct ext4_super_block *es =EXT4_SB (inode->i_sb)->s_es; +if (unlikely (block <le32_to_cpu (es->s_first_data_block) || + (block >ext4_blocks_count (es)))) +return 0 ; +else +return 1 ; +} + +static int ext4_valid_extent_entries (struct inode *inode, +struct ext4_extent_header *eh, +int depth) +{ +struct ext4_extent *ext; +struct ext4_extent_idx *ext_idx; +unsigned short entries; +if (eh->eh_entries ==0 ) +return 1 ; + + entries =le16_to_cpu (eh->eh_entries); + +if (depth ==0 ) { +/* leaf entries */ + ext =EXT_FIRST_EXTENT (eh); +while (entries) { +if (!ext4_valid_extent (inode, ext)) +return 0 ; + ext++; + entries--; + } + }else { + ext_idx =EXT_FIRST_INDEX (eh); +while (entries) { +if (!ext4_valid_extent_idx (inode, ext_idx)) +return 0 ; + ext_idx++; + entries--; + } + } +return 1 ; +} + +static int __ext4_ext_check (const char *function,struct inode *inode,struct ext4_extent_header *eh,int depth) {
@@ -329,11 +386,15 @@ static int __ext4_ext_check_header(const char *function, struct inode *inode, error_msg ="invalid eh_entries" ; goto corrupted; } +if (!ext4_valid_extent_entries (inode, eh, depth)) { + error_msg ="invalid extent entries" ; + goto corrupted; + }return 0 ; corrupted: ext4_error(inode->i_sb, function, - "bad header in inode #%lu: %s - magic %x, " + "bad header/extent in inode #%lu: %s - magic %x, " "entries %u, max %u(%u), depth %u(%u)", inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
@@ -342,8 +403,8 @@ corrupted:return -EIO ; } -#define ext4_ext_check_header (inode, eh, depth) \ - __ext4_ext_check_header(__func__, inode, eh, depth) +#define ext4_ext_check (inode, eh, depth) \ + __ext4_ext_check(__func__, inode, eh, depth)#ifdef EXT_DEBUG static void ext4_ext_show_path (struct inode *inode,struct ext4_ext_path *path)
@@ -547,7 +608,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, eh =ext_inode_hdr (inode); depth =ext_depth (inode); -if (ext4_ext_check_header (inode, eh, depth)) +if (ext4_ext_check (inode, eh, depth))return ERR_PTR (-EIO );
@@ -584,7 +645,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, path[ppos].p_hdr = eh; i--; -if (ext4_ext_check_header (inode, eh, i)) +if (ext4_ext_check (inode, eh, i)) goto err; }
@@ -1179,7 +1240,7 @@ got_index:if (bh ==NULL )return -EIO ; eh =ext_block_hdr (bh); -if (ext4_ext_check_header (inode, eh, depth)) { +if (ext4_ext_check (inode, eh, depth)) {put_bh (bh);return -EIO ; }
@@ -1192,7 +1253,7 @@ got_index:if (bh ==NULL )return -EIO ; eh =ext_block_hdr (bh); -if (ext4_ext_check_header (inode, eh, path->p_depth - depth)) { +if (ext4_ext_check (inode, eh, path->p_depth - depth)) {put_bh (bh);return -EIO ; }
@@ -2135,7 +2196,7 @@ static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)return -ENOMEM ; } path[0 ].p_hdr =ext_inode_hdr (inode); -if (ext4_ext_check_header (inode, path[0 ].p_hdr, depth)) { +if (ext4_ext_check (inode, path[0 ].p_hdr, depth)) { err = -EIO ; goto out; }
@@ -2189,7 +2250,7 @@ static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) err = -EIO ;break ; } -if (ext4_ext_check_header (inode,ext_block_hdr (bh), +if (ext4_ext_check (inode,ext_block_hdr (bh), depth - i -1 )) { err = -EIO ;break ;
Signature · 1 line
tg: (d2f8d7e..) extent_validate (depends on: master)
PATCH 1/1 ext4: Validate extent details only when read from the disk
Make sure we validate extent details only when read from the disk.
--- fs/ext4/ext4_extents.h | 1 + fs/ext4/extents.c | 24 +++++++++++++++++------- fs/ext4/inode.c | 10 ++++++++++ 3 files changed, 28 insertions(+), 7 deletions(-)
fs/ext4/ext4_extents.h +1 -0
@@ -241,5 +241,6 @@ extern int ext4_ext_search_left(struct inode *, struct ext4_ext_path *,extern int ext4_ext_search_right (struct inode *,struct ext4_ext_path *,ext4_lblk_t *,ext4_fsblk_t *);extern void ext4_ext_drop_refs (struct ext4_ext_path *); +extern int ext4_ext_check_inode (struct inode *inode);#endif /* _EXT4_EXTENTS */
fs/ext4/extents.c +17 -7
@@ -406,6 +406,11 @@ corrupted:#define ext4_ext_check (inode, eh, depth) \ __ext4_ext_check(__func__, inode, eh, depth) +int ext4_ext_check_inode (struct inode *inode) +{ +return ext4_ext_check (inode,ext_inode_hdr (inode),ext_depth (inode)); +} +#ifdef EXT_DEBUG static void ext4_ext_show_path (struct inode *inode,struct ext4_ext_path *path) {
@@ -602,15 +607,13 @@ struct ext4_ext_path * ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, struct ext4_ext_path *path) { + int need_to_validate = 0;struct ext4_extent_header *eh;struct buffer_head *bh;short int depth, i, ppos =0 , alloc =0 ; eh =ext_inode_hdr (inode); depth =ext_depth (inode); -if (ext4_ext_check (inode, eh, depth)) -return ERR_PTR (-EIO ); -/* account possible depth increase */ if (!path) {
@@ -634,10 +637,17 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, path[ppos].p_depth = i; path[ppos].p_ext =NULL ; - bh =sb_bread (inode->i_sb, path[ppos].p_block); -if (!bh) + bh =sb_getblk (inode->i_sb, path[ppos].p_block); +if (unlikely (!bh)) goto err; - +if (!bh_uptodate_or_lock (bh)) { +if (bh_submit_read (bh) <0 ) { +put_bh (bh); + goto err; + } +/* validate the extent entries */ + need_to_validate =1 ; + } eh =ext_block_hdr (bh); ppos++;BUG_ON (ppos > depth);
@@ -645,7 +655,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, path[ppos].p_hdr = eh; i--; -if (ext4_ext_check (inode, eh, i)) +if (need_to_validate &&ext4_ext_check (inode, eh, i)) goto err; }
fs/ext4/inode.c +10 -0
@@ -4273,6 +4273,16 @@ struct inode *ext4_iget(struct super_block *sb, unsigned long ino) (__u64)(le32_to_cpu (raw_inode->i_version_hi)) <<32 ; } +if (ei->i_flags &EXT4_EXTENTS_FL ) { +/* Validate extent which is part of inode */ + ret =ext4_ext_check_inode (inode); +if (ret) { +brelse (bh); + goto bad_inode; + } + + } +if (S_ISREG (inode->i_mode)) { inode->i_op = &ext4_file_inode_operations; inode->i_fop = &ext4_file_operations;
Signature · 1 line
tg: (b76c986..) extent_validate2 (depends on: extent_validate)
PATCH 1/1 ext4: Validate extent details only when read from the disk
Re: [PATCH -V2] ext4: Validate extent details only when read from the disk
In reply to Aneesh Kumar K.VQuoted 3 lines · Aneesh Kumar K.V
Make sure we validate extent details only when read from the disk.
Signed-off-by Aneesh Kumar K.V <[email protected]>
Your patches work well for my test case.
Kind regards,
Thiemo
Theodore Tso
maintainerRe: [PATCH -V2] ext4: Validate extent details only when read from the disk
In reply to Aneesh Kumar K.VThanks, I've applied both of these patches to the ext4 patch queue.
- Ted