Skip to contentSkip to conversation

ext4: fix syzbot report: kernel BUG in ext4_get_group_info

PATCH In review Theodore Ts'o 2 participants 7 messages Apr 30, 2023 – May 10, 2023 ext4 file system

lore
Share & tools
lore

Theodore Ts'o

author maintainer
#0 lore raw

cover ext4: fix syzbot report: kernel BUG in ext4_get_group_info

These two patches address this syzbot report[1]. See also the discussion here[2].

[1] https://syzkaller.appspot.com/bug?id=69b28112e098b070f639efb356393af3ffec4220
[2] https://lore.kernel.org/r/[email protected]

Theodore Ts'o (2):
ext4: allow ext4_get_group_info() to fail
ext4: remove a BUG_ON in ext4_mb_release_group_pa()

 fs/ext4/balloc.c  | 19 +++++++++++-
 fs/ext4/ext4.h    | 15 ++-------
 fs/ext4/ialloc.c  | 12 +++++---
 fs/ext4/mballoc.c | 78 +++++++++++++++++++++++++++++++++++++++--------
 fs/ext4/super.c   |  2 ++
 5 files changed, 96 insertions(+), 30 deletions(-)
Signature · 1 line
2.31.0
author maintainer
#1 lore raw

PATCH 1/2 ext4: allow ext4_get_group_info() to fail

Previously, ext4_get_group_info() would treat an invalid group number as BUG(), since in theory it should never happen. However, if a malicious attaker (or fuzzer) modifies the superblock via the block device while it is the file system is mounted, it is possible for s_first_data_block to get set to a very large number. In that case, when calculating the block group of some block number (such as the starting block of a preallocation region), could result in an underflow and very large block group number. Then the BUG_ON check in ext4_get_group_info() would fire, resutling in a denial of service attack that can be triggered by root or someone with write access to the block device.

For a quality of implementation perspective, it's best that even if the system administrator does something that they shouldn't, that it will not trigger a BUG. So instead of BUG'ing, ext4_get_group_info() will call ext4_error and return NULL. We also add fallback code in all of the callers of ext4_get_group_info() that it might NULL.

Also, since ext4_get_group_info() was already borderline to be an inline function, un-inline it. The results in a next reduction of the compiled text size of ext4 by roughly 2k.

---
 fs/ext4/balloc.c  | 19 ++++++++++++-
 fs/ext4/ext4.h    | 15 ++--------
 fs/ext4/ialloc.c  | 12 +++++---
 fs/ext4/mballoc.c | 72 +++++++++++++++++++++++++++++++++++++++--------
 fs/ext4/super.c   |  2 ++
 5 files changed, 91 insertions(+), 29 deletions(-)
fs/ext4/balloc.c +18 -1
index 094269488183..106e169e2524 100644
@@ -305,6 +305,23 @@ struct ext4_group_desc * ext4_get_group_desc(struct super_block *sb,
 	return desc;
 }
 
+struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
+					    ext4_group_t group)
+{
+	 struct ext4_group_info **grp_info;
+	 long indexv, indexh;
+
+	 if (unlikely(group >= EXT4_SB(sb)->s_groups_count)) {
+		 ext4_error(sb, "invalid group %u", group);
+		 return NULL;
+	 }
+	 indexv = group >> (EXT4_DESC_PER_BLOCK_BITS(sb));
+	 indexh = group & ((EXT4_DESC_PER_BLOCK(sb)) - 1);
+	 grp_info = sbi_array_rcu_deref(EXT4_SB(sb), s_group_info, indexv);
+	 return grp_info[indexh];
+}
+
+
 /*
  * Return the block number which was discovered to be invalid, or 0 if
  * the block bitmap is valid.
@@ -379,7 +396,7 @@ static int ext4_validate_block_bitmap(struct super_block *sb,
 
 	if (buffer_verified(bh))
 		return 0;
-	if (EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
+	if (!grp || EXT4_MB_GRP_BBITMAP_CORRUPT(grp))
 		return -EFSCORRUPTED;
 
 	ext4_lock_group(sb, block_group);
fs/ext4/ext4.h +2 -13
index 18cb2680dc39..7e8f66ba17f4 100644
@@ -2625,6 +2625,8 @@ extern void ext4_check_blocks_bitmap(struct super_block *);
 extern struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb,
 						    ext4_group_t block_group,
 						    struct buffer_head ** bh);
+extern struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
+						   ext4_group_t group);
 extern int ext4_should_retry_alloc(struct super_block *sb, int *retries);
 
 extern struct buffer_head *ext4_read_block_bitmap_nowait(struct super_block *sb,
@@ -3232,19 +3234,6 @@ static inline void ext4_isize_set(struct ext4_inode *raw_inode, loff_t i_size)
 	raw_inode->i_size_high = cpu_to_le32(i_size >> 32);
 }
 
-static inline
-struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
-					    ext4_group_t group)
-{
-	 struct ext4_group_info **grp_info;
-	 long indexv, indexh;
-	 BUG_ON(group >= EXT4_SB(sb)->s_groups_count);
-	 indexv = group >> (EXT4_DESC_PER_BLOCK_BITS(sb));
-	 indexh = group & ((EXT4_DESC_PER_BLOCK(sb)) - 1);
-	 grp_info = sbi_array_rcu_deref(EXT4_SB(sb), s_group_info, indexv);
-	 return grp_info[indexh];
-}
-
 /*
  * Reading s_groups_count requires using smp_rmb() afterwards.  See
  * the locking protocol documented in the comments of ext4_group_add()
fs/ext4/ialloc.c +8 -4
index 787ab89c2c26..754f961cd9fd 100644
@@ -91,7 +91,7 @@ static int ext4_validate_inode_bitmap(struct super_block *sb,
 
 	if (buffer_verified(bh))
 		return 0;
-	if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
+	if (!grp || EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
 		return -EFSCORRUPTED;
 
 	ext4_lock_group(sb, block_group);
@@ -293,7 +293,7 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
 	}
 	if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
 		grp = ext4_get_group_info(sb, block_group);
-		if (unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
+		if (!grp || unlikely(EXT4_MB_GRP_IBITMAP_CORRUPT(grp))) {
 			fatal = -EFSCORRUPTED;
 			goto error_return;
 		}
@@ -1046,7 +1046,7 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 			 * Skip groups with already-known suspicious inode
 			 * tables
 			 */
-			if (EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
+			if (!grp || EXT4_MB_GRP_IBITMAP_CORRUPT(grp))
 				goto next_group;
 		}
 
@@ -1183,6 +1183,10 @@ struct inode *__ext4_new_inode(struct mnt_idmap *idmap,
 
 		if (!(sbi->s_mount_state & EXT4_FC_REPLAY)) {
 			grp = ext4_get_group_info(sb, group);
+			if (!grp) {
+				err = -EFSCORRUPTED;
+				goto out;
+			}
 			down_read(&grp->alloc_sem); /*
 						     * protect vs itable
 						     * lazyinit
@@ -1526,7 +1530,7 @@ int ext4_init_inode_table(struct super_block *sb, ext4_group_t group,
 	}
 
 	gdp = ext4_get_group_desc(sb, group, &group_desc_bh);
-	if (!gdp)
+	if (!gdp || !grp)
 		goto out;
 
 	/*
fs/ext4/mballoc.c +61 -11

229 lines not on this page · raw message

fs/ext4/super.c +2 -0
index 39f00f05f981..40ab4f78161d 100644
@@ -1048,6 +1048,8 @@ void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
 	struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
 	int ret;
 
+	if (!grp || !gdp)
+		return;
 	if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
 		ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
 					    &grp->bb_state);
Signature · 1 line
2.31.0
reviewer Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] ext4: allow ext4_get_group_info() to fail

Quoted 18 lines · Theodore Ts'o

Previously, ext4_get_group_info() would treat an invalid group number as BUG(), since in theory it should never happen. However, if a malicious attaker (or fuzzer) modifies the superblock via the block device while it is the file system is mounted, it is possible for s_first_data_block to get set to a very large number. In that case, when calculating the block group of some block number (such as the starting block of a preallocation region), could result in an underflow and very large block group number. Then the BUG_ON check in ext4_get_group_info() would fire, resutling in a denial of service attack that can be triggered by root or someone with write access to the block device.

For a quality of implementation perspective, it's best that even if the system administrator does something that they shouldn't, that it will not trigger a BUG. So instead of BUG'ing, ext4_get_group_info() will call ext4_error and return NULL. We also add fallback code in all of the callers of ext4_get_group_info() that it might NULL.

Quoted 7 lines

Also, since ext4_get_group_info() was already borderline to be an inline function, un-inline it. The results in a next reduction of the compiled text size of ext4 by roughly 2k.

The patch looks good except for one small problem already found by Julia:

fs/ext4/mballoc.c void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group, · line 2598

Quoted 6 lines
fs/ext4/mballoc.c +1 -1
@@ -2578,7 +2595,7 @@ void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
 		gdp = ext4_get_group_desc(sb, group, NULL);
 		grp = ext4_get_group_info(sb, group);
 
-		if (EXT4_MB_GRP_NEED_INIT(grp) &&
+		if (grp && grp && EXT4_MB_GRP_NEED_INIT(grp) &&

^^^ one of these should be gdp.

With this fixed feel free to add:

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#3 lore raw

Re: [PATCH 1/2] ext4: allow ext4_get_group_info() to fail

Quoted 6 lines · Jan Kara

The patch looks good except for one small problem already found by Julia:

Quoted 4 lines
fs/ext4/mballoc.c +0 -0
@@ -2578,7 +2595,7 @@ void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
 		gdp = ext4_get_group_desc(sb, group, NULL);
 		grp = ext4_get_group_info(sb, group);
 
Quoted 7 lines

fs/ext4/mballoc.c void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group, · line 2598

Quoted 2 lines
fs/ext4/mballoc.c +1 -1
-		if (EXT4_MB_GRP_NEED_INIT(grp) &&
+		if (grp && grp && EXT4_MB_GRP_NEED_INIT(grp) &&

^^^ one of these should be gdp.

With this fixed feel free to add:

Thanks for the review, fixed in my tree.

- Ted

author maintainer
#4 lore raw

PATCH 2/2 ext4: remove a BUG_ON in ext4_mb_release_group_pa()

In reply to Theodore Ts'o

If a malicious fuzzer overwrites the ext4 superblock while it is mounted such that the s_first_data_block is set to a very large number, the calculation of the block group can underflow, and trigger a BUG_ON check. Change this to be an ext4_warning so that we don't crash the kernel.

---
 fs/ext4/mballoc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/ext4/mballoc.c +5 -1
index dc13734f399d..9c7881a4ea75 100644
@@ -5047,7 +5047,11 @@ ext4_mb_release_group_pa(struct ext4_buddy *e4b,
 	trace_ext4_mb_release_group_pa(sb, pa);
 	BUG_ON(pa->pa_deleted == 0);
 	ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
-	BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
+	if (unlikely(group != e4b->bd_group && pa->pa_len != 0)) {
+		ext4_warning(sb, "bad group: expected %u, group %u, pa_start %llu",
+			     e4b->bd_group, group, pa->pa_pstart);
+		return 0;
+	}
 	mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
 	atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
 	trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Signature · 1 line
2.31.0
reviewer Reviewed-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] ext4: remove a BUG_ON in ext4_mb_release_group_pa()

Quoted 9 lines · Theodore Ts'o

If a malicious fuzzer overwrites the ext4 superblock while it is mounted such that the s_first_data_block is set to a very large number, the calculation of the block group can underflow, and trigger a BUG_ON check. Change this to be an ext4_warning so that we don't crash the kernel.

OK, looks good to me. But frankly there are many other interesting ways how bogus group numbers output when this happens can return is fun stuff - e.g. ext4_group_first_block_no() is going to return invalid blocks etc. So it feels a bit like endless whack-a-mole game. Anyway I agree the series seem to fix a big chunk of these sites so feel free to add:

Honza

Quoted 24 lines
---
 fs/ext4/mballoc.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
fs/ext4/mballoc.c +5 -1
index dc13734f399d..9c7881a4ea75 100644
@@ -5047,7 +5047,11 @@ ext4_mb_release_group_pa(struct ext4_buddy *e4b,
 	trace_ext4_mb_release_group_pa(sb, pa);
 	BUG_ON(pa->pa_deleted == 0);
 	ext4_get_group_no_and_offset(sb, pa->pa_pstart, &group, &bit);
-	BUG_ON(group != e4b->bd_group && pa->pa_len != 0);
+	if (unlikely(group != e4b->bd_group && pa->pa_len != 0)) {
+		ext4_warning(sb, "bad group: expected %u, group %u, pa_start %llu",
+			     e4b->bd_group, group, pa->pa_pstart);
+		return 0;
+	}
 	mb_free_blocks(pa->pa_inode, e4b, bit, pa->pa_len);
 	atomic_add(pa->pa_len, &EXT4_SB(sb)->s_mb_discarded);
 	trace_ext4_mballoc_discard(sb, NULL, group, bit, pa->pa_len);
Signature · 1 line
2.31.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
author maintainer
#6 lore raw

Re: [PATCH 2/2] ext4: remove a BUG_ON in ext4_mb_release_group_pa()

Quoted 5 lines · Jan Kara

OK, looks good to me. But frankly there are many other interesting ways how bogus group numbers output when this happens can return is fun stuff - e.g. ext4_group_first_block_no() is going to return invalid blocks etc. So it feels a bit like endless whack-a-mole game. Anyway I agree the series seem to fix a big chunk of these sites so feel free to add:

The main thing I'm trying to avoid is a kernel crash or possible out-of-bounds read or write, which could lead to a security vulnerability. If a a bad block number being returned by. say, ext4_group_first_block_no() "only" results in an I/O error when or (further) corruption of the block device, that's not a problem as far as I'm concerned. After all, if a malicious root access has read/write access to the block device, they can corrupt the file system *anyway*.

I wasn't able to find cases where a crazy return value from ext4_group_first_block_no() which would cause a BUG or a buffer overrun. If we (or syzbot) finds a case where this could happen, we could copy s_es->s_first_data_block to sbi->s_first_data_block and then validate it during the file system mount.

I also did a quick spot check what nastiness could be caused by real-time frobbing of s_blocks_count s_inodes_count and couldn't find anything there either. So it looks like s_first_data_block is the one which is the most problematic.

Cheers,

- Ted