Skip to contentSkip to conversation

ext4: Make the extent validity check more paranoid

PATCH In review Theodore Ts'o 2 participants 4 messages Apr 23, 2009 ext4 file system

lore
Share & tools
lore

Theodore Ts'o

author maintainer
#0 lore raw

PATCH 1/1 ext4: Make the extent validity check more paranoid

Instead of just checking that the extent block number is greater or equal than s_first_data_block, make sure it it is not pointing into the block group descriptors, since that is clearly wrong. This helps prevent filesystem from getting very badly corrupted in case an extent block is corrupted.

---
 fs/ext4/extents.c |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)
fs/ext4/extents.c +12 -6
index 6132353..c28ffe2 100644
@@ -326,11 +326,14 @@ ext4_ext_max_entries(struct inode *inode, int depth)
 
 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
 {
-	ext4_fsblk_t block = ext_pblock(ext);
+	ext4_fsblk_t block = ext_pblock(ext), valid_block;
 	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))))
+
+	valid_block = le32_to_cpu(es->s_first_data_block) +
+		EXT4_SB(inode->i_sb)->s_gdb_count;
+	if (unlikely(block <= valid_block ||
+		     ((block + len) > ext4_blocks_count(es))))
 		return 0;
 	else
 		return 1;
@@ -339,10 +342,13 @@ static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
 static int ext4_valid_extent_idx(struct inode *inode,
 				struct ext4_extent_idx *ext_idx)
 {
-	ext4_fsblk_t block = idx_pblock(ext_idx);
+	ext4_fsblk_t block = idx_pblock(ext_idx), valid_block;
 	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))))
+
+	valid_block = le32_to_cpu(es->s_first_data_block) +
+		EXT4_SB(inode->i_sb)->s_gdb_count;
+	if (unlikely(block <= valid_block ||
+		     (block >= ext4_blocks_count(es))))
 		return 0;
 	else
 		return 1;
Signature · 1 line
1.5.6.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Theodore Ts'o

Instead of just checking that the extent block number is greater or equal than s_first_data_block, make sure it it is not pointing into the block group descriptors, since that is clearly wrong. This helps prevent filesystem from getting very badly corrupted in case an extent block is corrupted.

Good idea. Maybe we can get our friends with the corrupted fs to run with these validation patches... I can get this into rawhide at least.

Quoted 43 lines
---
 fs/ext4/extents.c |   18 ++++++++++++------
 1 files changed, 12 insertions(+), 6 deletions(-)
fs/ext4/extents.c +12 -6
index 6132353..c28ffe2 100644
@@ -326,11 +326,14 @@ ext4_ext_max_entries(struct inode *inode, int depth)
 
 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
 {
-	ext4_fsblk_t block = ext_pblock(ext);
+	ext4_fsblk_t block = ext_pblock(ext), valid_block;
 	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))))
+
+	valid_block = le32_to_cpu(es->s_first_data_block) +
+		EXT4_SB(inode->i_sb)->s_gdb_count;
+	if (unlikely(block <= valid_block ||
+		     ((block + len) > ext4_blocks_count(es))))
 		return 0;
 	else
 		return 1;
@@ -339,10 +342,13 @@ static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
 static int ext4_valid_extent_idx(struct inode *inode,
 				struct ext4_extent_idx *ext_idx)
 {
-	ext4_fsblk_t block = idx_pblock(ext_idx);
+	ext4_fsblk_t block = idx_pblock(ext_idx), valid_block;
 	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))))
+
+	valid_block = le32_to_cpu(es->s_first_data_block) +
+		EXT4_SB(inode->i_sb)->s_gdb_count;
+	if (unlikely(block <= valid_block ||
+		     (block >= ext4_blocks_count(es))))
 		return 0;
 	else
 		return 1;
author maintainer
#2 lore raw
Quoted 11 lines · Eric Sandeen
Quoted 7 lines · Theodore Ts'o

Instead of just checking that the extent block number is greater or equal than s_first_data_block, make sure it it is not pointing into the block group descriptors, since that is clearly wrong. This helps prevent filesystem from getting very badly corrupted in case an extent block is corrupted.

Good idea. Maybe we can get our friends with the corrupted fs to run with these validation patches... I can get this into rawhide at least.

Yeah, unfortunately this patch requires some other patches that went in during the 2.6.30 merge window, so some extra back-porting would be needed for our friends running a 2.6.29.1 Fedora kernel.

I'm still trying figure out what's the best way to add the right kind of checking for 2.6.29 based kernels. Looking at the dump files which Kevin Shanahan, it doesn't look like they are coming from interior nodes of extent trees. Some kind of kludge patch into the block device layer might be the most fool-proof way to do things.

- Ted

#3 lore raw
Quoted 8 lines · Theodore Tso
Quoted 7 lines · Eric Sandeen
Quoted 6 lines · Theodore Ts'o

Instead of just checking that the extent block number is greater or equal than s_first_data_block, make sure it it is not pointing into the block group descriptors, since that is clearly wrong. This helps prevent filesystem from getting very badly corrupted in case an extent block is corrupted.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Good idea. Maybe we can get our friends with the corrupted fs to run with these validation patches... I can get this into rawhide at least.

Yeah, unfortunately this patch requires some other patches that went in during the 2.6.30 merge window, so some extra back-porting would be needed for our friends running a 2.6.29.1 Fedora kernel.

Right, but rawhide has .30-etc, so a test on that might be helpful.

-Eric