ext4: avoid potential data overflow in next_linear_group
authorKemeng Shi <shikemeng@huaweicloud.com>
Tue, 1 Aug 2023 14:31:56 +0000 (22:31 +0800)
committerTheodore Ts'o <tytso@mit.edu>
Thu, 3 Aug 2023 14:47:29 +0000 (10:47 -0400)
ngroups is ext4_group_t (unsigned int) while next_linear_group treat it
in int. If ngroups is bigger than max number described by int, it will
be treat as a negative number. Then "return group + 1 >= ngroups ? 0 :
group + 1;" may keep returning 0.
Switch int to ext4_group_t in next_linear_group to fix the overflow.

Fixes: 196e402adf2e ("ext4: improve cr 0 / cr 1 group scanning")
Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
Reviewed-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
Link: https://lore.kernel.org/r/20230801143204.2284343-3-shikemeng@huaweicloud.com
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
fs/ext4/mballoc.c

index e07d2a4fbcd107c46f07f15bed9efbd009e28165..bf041932c5997a4d18d6f9fd6fe7bf3e2e5135ea 100644 (file)
@@ -1080,8 +1080,9 @@ static inline int should_optimize_scan(struct ext4_allocation_context *ac)
  * Return next linear group for allocation. If linear traversal should not be
  * performed, this function just returns the same group
  */
-static int
-next_linear_group(struct ext4_allocation_context *ac, int group, int ngroups)
+static ext4_group_t
+next_linear_group(struct ext4_allocation_context *ac, ext4_group_t group,
+                 ext4_group_t ngroups)
 {
        if (!should_optimize_scan(ac))
                goto inc_and_return;