Skip to content

Commit 91b3708

Browse files
Kemeng Shigregkh
authored andcommitted
mm: swap: fix potential buffer overflow in setup_clusters()
commit 152c133 upstream. In setup_swap_map(), we only ensure badpages are in range (0, last_page]. As maxpages might be < last_page, setup_clusters() will encounter a buffer overflow when a badpage is >= maxpages. Only call inc_cluster_info_page() for badpage which is < maxpages to fix the issue. Link: https://lkml.kernel.org/r/20250522122554.12209-4-shikemeng@huaweicloud.com Fixes: b843786 ("mm: swapfile: fix SSD detection with swapfile on btrfs") Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com> Reviewed-by: Baoquan He <bhe@redhat.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Kairui Song <kasong@tencent.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f7c7540 commit 91b3708

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

mm/swapfile.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3309,9 +3309,13 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
33093309
* and the EOF part of the last cluster.
33103310
*/
33113311
inc_cluster_info_page(si, cluster_info, 0);
3312-
for (i = 0; i < swap_header->info.nr_badpages; i++)
3313-
inc_cluster_info_page(si, cluster_info,
3314-
swap_header->info.badpages[i]);
3312+
for (i = 0; i < swap_header->info.nr_badpages; i++) {
3313+
unsigned int page_nr = swap_header->info.badpages[i];
3314+
3315+
if (page_nr >= maxpages)
3316+
continue;
3317+
inc_cluster_info_page(si, cluster_info, page_nr);
3318+
}
33153319
for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
33163320
inc_cluster_info_page(si, cluster_info, i);
33173321

0 commit comments

Comments
 (0)