diff options
| author | Kemeng Shi <shikemeng@huaweicloud.com> | 2025-05-22 20:25:53 +0800 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2025-08-15 16:39:35 +0200 |
| commit | 815c528b13f2bb9b3130c13bedeabf2351a68129 (patch) | |
| tree | 3ee4ad7775c89e51d5937e7b2e6f36f9d807dc82 /mm | |
| parent | bc9e95bac80c9a86c1e24e3f676123c5ffbcc3f5 (diff) | |
| download | linux-815c528b13f2bb9b3130c13bedeabf2351a68129.tar.gz linux-815c528b13f2bb9b3130c13bedeabf2351a68129.tar.bz2 linux-815c528b13f2bb9b3130c13bedeabf2351a68129.zip | |
mm: swap: fix potential buffer overflow in setup_clusters()
commit 152c1339dc13ad46f1b136e8693de15980750835 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: b843786b0bd0 ("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>
Diffstat (limited to 'mm')
| -rw-r--r-- | mm/swapfile.c | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/mm/swapfile.c b/mm/swapfile.c index 57397434929e..e03084725355 100644 --- a/mm/swapfile.c +++ b/mm/swapfile.c @@ -3208,9 +3208,13 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, * and the EOF part of the last cluster. */ inc_cluster_info_page(si, cluster_info, 0); - for (i = 0; i < swap_header->info.nr_badpages; i++) - inc_cluster_info_page(si, cluster_info, - swap_header->info.badpages[i]); + for (i = 0; i < swap_header->info.nr_badpages; i++) { + unsigned int page_nr = swap_header->info.badpages[i]; + + if (page_nr >= maxpages) + continue; + inc_cluster_info_page(si, cluster_info, page_nr); + } for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++) inc_cluster_info_page(si, cluster_info, i); |
