]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
mm: swap: fix potential buffer overflow in setup_clusters()
authorKemeng Shi <shikemeng@huaweicloud.com>
Thu, 22 May 2025 12:25:53 +0000 (20:25 +0800)
committerAndrew Morton <akpm@linux-foundation.org>
Fri, 25 Jul 2025 02:12:34 +0000 (19:12 -0700)
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>
mm/swapfile.c

index 119aa706e92d4d679faf1086b809b43ed24ebc65..4f47ec9118f84af1a33eb0f82e9b706ccd2d88d9 100644 (file)
@@ -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);