Skip to contentSkip to conversation

ext4: fix signed vs unsigned comparison in ext4_valid_extent()

PATCH In review Theodore Ts'o 3 participants 5 messages Oct 23, 2019 ext4 file system

lore
Share & tools
lore

Theodore Ts'o

author maintainer
#0 lore raw

PATCH 1/1 ext4: fix signed vs unsigned comparison in ext4_valid_extent()

Due to a signed vs unsigned comparison, an invalid extent where ee_block (the logical block) is so large that lblk + len overflow wasn't getting flagged as invalid.

As a result, we tripped the BUG_ON(end < lblk) in ext4_es_cache_extent() when trying to mount a file system with a corrupted journal inode was corrupted.

https://bugzilla.kernel.org/show_bug.cgi?id=205197

---
 fs/ext4/extents.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/extents.c +1 -1
index fb0f99dc8c22..d12bc287abdc 100644
@@ -367,7 +367,7 @@ 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 = ext4_ext_pblock(ext);
-	int len = ext4_ext_get_actual_len(ext);
+	unsigned int len = ext4_ext_get_actual_len(ext);
 	ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
 
 	/*
Signature · 1 line
2.23.0
#1 lore raw
Quoted 32 lines · Theodore Ts'o

Due to a signed vs unsigned comparison, an invalid extent where ee_block (the logical block) is so large that lblk + len overflow wasn't getting flagged as invalid.

As a result, we tripped the BUG_ON(end < lblk) in ext4_es_cache_extent() when trying to mount a file system with a corrupted journal inode was corrupted.

https://bugzilla.kernel.org/show_bug.cgi?id=205197

---
 fs/ext4/extents.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext4/extents.c +1 -1
index fb0f99dc8c22..d12bc287abdc 100644
@@ -367,7 +367,7 @@ 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 = ext4_ext_pblock(ext);
-	int len = ext4_ext_get_actual_len(ext);
+	unsigned int len = ext4_ext_get_actual_len(ext);
 	ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
 
 	/*
Signature · 1 line
2.23.0

This patch can't be fixing anything because the comparison is unsigned both before and after this patch.

- Eric

author maintainer
#2 lore raw
Quoted 3 lines · Eric Biggers

This patch can't be fixing anything because the comparison is unsigned both before and after this patch.

Thanks, you're right; I had forgotten C's signed/unsigned rules for addition. The funny thing is the original reporter of BZ #205197 reported that the problem went away he tried a similar patch.

- Ted

#3 lore raw
Quoted 8 lines · Theodore Y. Ts'o
Quoted 3 lines · Eric Biggers

This patch can't be fixing anything because the comparison is unsigned both before and after this patch.

Thanks, you're right; I had forgotten C's signed/unsigned rules for addition. The funny thing is the original reporter of BZ #205197 reported that the problem went away he tried a similar patch.

Not trying to stick my nose in too much here but:

What does it mean if ext4_ext_get_actual_len() to return < 0?

Ira

Quoted 2 lines

- Ted

author maintainer
#4 lore raw
Quoted 6 lines · Ira Weiny
Quoted 5 lines · Theodore Y. Ts'o
Quoted 3 lines · Eric Biggers

This patch can't be fixing anything because the comparison is unsigned both before and after this patch.

Quoted 7 lines
Quoted 3 lines

Thanks, you're right; I had forgotten C's signed/unsigned rules for addition. The funny thing is the original reporter of BZ #205197 reported that the problem went away he tried a similar patch.

Not trying to stick my nose in too much here but:

What does it mean if ext4_ext_get_actual_len() to return < 0?

It's not possible for it to return < 0. We probably should clean it up to make it return an unsigned int, but that's a longer-term clean-up.

- Ted