[01/10] ext4: correct grp validation in ext4_mb_good_group

Message ID 20230721171007.2065423-2-shikemeng@huaweicloud.com
State New
Headers
Series A few fixes and cleanups to mballoc |

Commit Message

Kemeng Shi July 21, 2023, 5:09 p.m. UTC
  Group corruption check will access memory of grp and will trigger kernel
crash if grp is NULL. So do NULL check before corruption check.

Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
---
 fs/ext4/mballoc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Ritesh Harjani (IBM) July 21, 2023, 3:22 p.m. UTC | #1
Kemeng Shi <shikemeng@huaweicloud.com> writes:

> Group corruption check will access memory of grp and will trigger kernel
> crash if grp is NULL. So do NULL check before corruption check.
>

Fixes: 5354b2af3406 ("ext4: allow ext4_get_group_info() to fail")

> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
> ---
>  fs/ext4/mballoc.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
> index 3ab37533349f..90ffabac100b 100644
> --- a/fs/ext4/mballoc.c
> +++ b/fs/ext4/mballoc.c
> @@ -2554,7 +2554,7 @@ static bool ext4_mb_good_group(struct ext4_allocation_context *ac,
>  
>  	BUG_ON(cr < CR_POWER2_ALIGNED || cr >= EXT4_MB_NUM_CRS);
>  
> -	if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp) || !grp))
> +	if (!grp || unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))

maybe like below?
      if (unlikely(!grp || EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))

-ritesh

>  		return false;
>  
>  	free = grp->bb_free;
> -- 
> 2.30.0
  

Patch

diff --git a/fs/ext4/mballoc.c b/fs/ext4/mballoc.c
index 3ab37533349f..90ffabac100b 100644
--- a/fs/ext4/mballoc.c
+++ b/fs/ext4/mballoc.c
@@ -2554,7 +2554,7 @@  static bool ext4_mb_good_group(struct ext4_allocation_context *ac,
 
 	BUG_ON(cr < CR_POWER2_ALIGNED || cr >= EXT4_MB_NUM_CRS);
 
-	if (unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp) || !grp))
+	if (!grp || unlikely(EXT4_MB_GRP_BBITMAP_CORRUPT(grp)))
 		return false;
 
 	free = grp->bb_free;