Skip to content

Commit ff0031d

Browse files
cmaiolinotytso
authored andcommitted
ext2: fix filesystem deadlock while reading corrupted xattr block
This bug can be reproducible with fsfuzzer, although, I couldn't reproduce it 100% of my tries, it is quite easily reproducible. During the deletion of an inode, ext2_xattr_delete_inode() does not check if the block pointed by EXT2_I(inode)->i_file_acl is a valid data block, this might lead to a deadlock, when i_file_acl == 1, and the filesystem block size is 1024. In that situation, ext2_xattr_delete_inode, will load the superblock's buffer head (instead of a valid i_file_acl block), and then lock that buffer head, which, ext2_sync_super will also try to lock, making the filesystem deadlock in the following stack trace: root 17180 0.0 0.0 113660 660 pts/0 D+ 07:08 0:00 rmdir /media/test/dir1 [<ffffffff8125da9f>] __sync_dirty_buffer+0xaf/0x100 [<ffffffff8125db03>] sync_dirty_buffer+0x13/0x20 [<ffffffffa03f0d57>] ext2_sync_super+0xb7/0xc0 [ext2] [<ffffffffa03f10b9>] ext2_error+0x119/0x130 [ext2] [<ffffffffa03e9d93>] ext2_free_blocks+0x83/0x350 [ext2] [<ffffffffa03f3d03>] ext2_xattr_delete_inode+0x173/0x190 [ext2] [<ffffffffa03ee9e9>] ext2_evict_inode+0xc9/0x130 [ext2] [<ffffffff8123fd23>] evict+0xb3/0x180 [<ffffffff81240008>] iput+0x1b8/0x240 [<ffffffff8123c4ac>] d_delete+0x11c/0x150 [<ffffffff8122fa7e>] vfs_rmdir+0xfe/0x120 [<ffffffff812340ee>] do_rmdir+0x17e/0x1f0 [<ffffffff81234dd6>] SyS_rmdir+0x16/0x20 [<ffffffff81838cf2>] entry_SYSCALL_64_fastpath+0x1a/0xa4 [<ffffffffffffffff>] 0xffffffffffffffff Fix this by using the same approach ext4 uses to test data blocks validity, implementing ext2_data_block_valid. An another possibility when the superblock is very corrupted, is that i_file_acl is 1, block_count is 1 and first_data_block is 0. For such situations, we might have i_file_acl pointing to a 'valid' block, but still step over the superblock. The approach I used was to also test if the superblock is not in the range described by ext2_data_block_valid() arguments Signed-off-by: Carlos Maiolino <cmaiolino@redhat.com> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 079788d commit ff0031d

4 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎fs/ext2/balloc.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,27 @@ static int ext2_has_free_blocks(struct ext2_sb_info *sbi)
11931193
return 1;
11941194
}
11951195

1196+
/*
1197+
* Returns 1 if the passed-in block region is valid; 0 if some part overlaps
1198+
* with filesystem metadata blocksi.
1199+
*/
1200+
int ext2_data_block_valid(struct ext2_sb_info *sbi, ext2_fsblk_t start_blk,
1201+
unsigned int count)
1202+
{
1203+
if ((start_blk <= le32_to_cpu(sbi->s_es->s_first_data_block)) ||
1204+
(start_blk + count < start_blk) ||
1205+
(start_blk > le32_to_cpu(sbi->s_es->s_blocks_count)))
1206+
return 0;
1207+
1208+
/* Ensure we do not step over superblock */
1209+
if ((start_blk <= sbi->s_sb_block) &&
1210+
(start_blk + count >= sbi->s_sb_block))
1211+
return 0;
1212+
1213+
1214+
return 1;
1215+
}
1216+
11961217
/*
11971218
* ext2_new_blocks() -- core block(s) allocation function
11981219
* @inode: file inode

‎fs/ext2/ext2.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ struct ext2_inode {
367367
*/
368368
#define EXT2_VALID_FS 0x0001 /* Unmounted cleanly */
369369
#define EXT2_ERROR_FS 0x0002 /* Errors detected */
370+
#define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
370371

371372
/*
372373
* Mount flags
@@ -739,6 +740,8 @@ extern unsigned long ext2_bg_num_gdb(struct super_block *sb, int group);
739740
extern ext2_fsblk_t ext2_new_block(struct inode *, unsigned long, int *);
740741
extern ext2_fsblk_t ext2_new_blocks(struct inode *, unsigned long,
741742
unsigned long *, int *);
743+
extern int ext2_data_block_valid(struct ext2_sb_info *sbi, ext2_fsblk_t start_blk,
744+
unsigned int count);
742745
extern void ext2_free_blocks (struct inode *, unsigned long,
743746
unsigned long);
744747
extern unsigned long ext2_count_free_blocks (struct super_block *);

‎fs/ext2/inode.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,16 @@ struct inode *ext2_iget (struct super_block *sb, unsigned long ino)
13891389
ei->i_frag_size = raw_inode->i_fsize;
13901390
ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl);
13911391
ei->i_dir_acl = 0;
1392+
1393+
if (ei->i_file_acl &&
1394+
!ext2_data_block_valid(EXT2_SB(sb), ei->i_file_acl, 1)) {
1395+
ext2_error(sb, "ext2_iget", "bad extended attribute block %u",
1396+
ei->i_file_acl);
1397+
brelse(bh);
1398+
ret = -EFSCORRUPTED;
1399+
goto bad_inode;
1400+
}
1401+
13921402
if (S_ISREG(inode->i_mode))
13931403
inode->i_size |= ((__u64)le32_to_cpu(raw_inode->i_size_high)) << 32;
13941404
else

‎fs/ext2/xattr.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,19 @@ void
759759
ext2_xattr_delete_inode(struct inode *inode)
760760
{
761761
struct buffer_head *bh = NULL;
762+
struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
762763

763764
down_write(&EXT2_I(inode)->xattr_sem);
764765
if (!EXT2_I(inode)->i_file_acl)
765766
goto cleanup;
767+
768+
if (!ext2_data_block_valid(sbi, EXT2_I(inode)->i_file_acl, 0)) {
769+
ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
770+
"inode %ld: xattr block %d is out of data blocks range",
771+
inode->i_ino, EXT2_I(inode)->i_file_acl);
772+
goto cleanup;
773+
}
774+
766775
bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
767776
if (!bh) {
768777
ext2_error(inode->i_sb, "ext2_xattr_delete_inode",

0 commit comments

Comments
 (0)