Skip to content

Commit 815c528

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/[email protected] Fixes: b843786 ("mm: swapfile: fix SSD detection with swapfile on btrfs") Signed-off-by: Kemeng Shi <[email protected]> Reviewed-by: Baoquan He <[email protected]> Cc: Johannes Weiner <[email protected]> Cc: Kairui Song <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent bc9e95b commit 815c528

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

mm/swapfile.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3208,9 +3208,13 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,
32083208
* and the EOF part of the last cluster.
32093209
*/
32103210
inc_cluster_info_page(si, cluster_info, 0);
3211-
for (i = 0; i < swap_header->info.nr_badpages; i++)
3212-
inc_cluster_info_page(si, cluster_info,
3213-
swap_header->info.badpages[i]);
3211+
for (i = 0; i < swap_header->info.nr_badpages; i++) {
3212+
unsigned int page_nr = swap_header->info.badpages[i];
3213+
3214+
if (page_nr >= maxpages)
3215+
continue;
3216+
inc_cluster_info_page(si, cluster_info, page_nr);
3217+
}
32143218
for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
32153219
inc_cluster_info_page(si, cluster_info, i);
32163220

0 commit comments

Comments
 (0)