From: Kemeng Shi <shikemeng@huaweicloud.com>
To: Kairui Song <ryncsn@gmail.com>
Cc: akpm@linux-foundation.org, bhe@redhat.com, hannes@cmpxchg.org,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/4] mm: swap: fix potensial buffer overflow in setup_clusters()
Date: Wed, 11 Jun 2025 16:27:02 +0800 [thread overview]
Message-ID: <b2d54e47-a0a2-eba8-561a-01f3ca35c71e@huaweicloud.com> (raw)
In-Reply-To: <CAMgjq7BHzid81f2cBDwQqN6enfJOh8XLjzOS6e+kiu6TMuu0ZA@mail.gmail.com>
on 5/26/2025 2:44 AM, Kairui Song wrote:
> On Thu, May 22, 2025 at 11:32 AM Kemeng Shi <shikemeng@huaweicloud.com> wrote:
>>
>> 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.
>>
>> Fixes: b843786b0bd01 ("mm: swapfile: fix SSD detection with swapfile on btrfs")
>> Signed-off-by: Kemeng Shi <shikemeng@huaweicloud.com>
>> ---
>> mm/swapfile.c | 10 +++++++---
>> 1 file changed, 7 insertions(+), 3 deletions(-)
>>
>> diff --git a/mm/swapfile.c b/mm/swapfile.c
>> index a82f4ebefca3..63ab9f14b2c6 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);
>
> I think we might need a pr_err or pr_warn here, this means mkswap
> marked the wrong region as a bad block? Or some fs side things went
> wrong.
As Baoquan metioned that there is already warning in read_swap_header().
Besides, I think last_page in swap_header already indicates the range
which is acceptable to fs and we only need to explicitly handle page_nr
which is > last_page.
>
>
>> + }
>> for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
>> inc_cluster_info_page(si, cluster_info, i);
>>
>> --
>> 2.30.0
>>
>>
>
next prev parent reply other threads:[~2025-06-11 8:27 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-22 12:25 [PATCH 0/4] Some randome fixes and cleanups to swapfile Kemeng Shi
2025-05-22 12:25 ` [PATCH 1/4] mm: swap: move nr_swap_pages counter decrement from folio_alloc_swap() to swap_range_alloc() Kemeng Shi
2025-05-22 3:55 ` Kairui Song
2025-05-30 1:31 ` Baoquan He
2025-05-22 12:25 ` [PATCH 2/4] mm: swap: correctly use maxpages in swapon syscall to avoid potensial deadloop Kemeng Shi
2025-05-25 17:08 ` Kairui Song
2025-06-11 7:54 ` Kemeng Shi
2025-07-17 23:21 ` Andrew Morton
2025-07-18 6:12 ` Kemeng Shi
2025-05-30 2:50 ` Baoquan He
2025-06-11 8:27 ` Kemeng Shi
2025-05-22 12:25 ` [PATCH 3/4] mm: swap: fix potensial buffer overflow in setup_clusters() Kemeng Shi
2025-05-25 18:44 ` Kairui Song
2025-05-30 2:55 ` Baoquan He
2025-06-11 8:27 ` Kemeng Shi [this message]
2025-05-30 2:56 ` Baoquan He
2025-05-22 12:25 ` [PATCH 4/4] mm: swap: remove stale comment stale comment in cluster_alloc_swap_entry() Kemeng Shi
2025-05-25 17:05 ` Kairui Song
2025-05-30 5:24 ` Baoquan He
2025-05-22 21:41 ` [PATCH 0/4] Some randome fixes and cleanups to swapfile Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b2d54e47-a0a2-eba8-561a-01f3ca35c71e@huaweicloud.com \
--to=shikemeng@huaweicloud.com \
--cc=akpm@linux-foundation.org \
--cc=bhe@redhat.com \
--cc=hannes@cmpxchg.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=ryncsn@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).