All of lore.kernel.org
 help / color / mirror / Atom feed
From: Baoquan He <bhe@redhat.com>
To: Kairui Song <ryncsn@gmail.com>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>,
	akpm@linux-foundation.org, 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: Fri, 30 May 2025 10:55:19 +0800	[thread overview]
Message-ID: <aDkeF/7hkHVfaRHq@MiWiFi-R3L-srv> (raw)
In-Reply-To: <CAMgjq7BHzid81f2cBDwQqN6enfJOh8XLjzOS6e+kiu6TMuu0ZA@mail.gmail.com>

On 05/26/25 at 02:44am, 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.

There's aready warning in read_swap_header():
static unsigned long read_swap_header(struct swap_info_struct *si,
                                        union swap_header *swap_header,
                                        struct inode *inode)
{
	......
	if (last_page > maxpages) {
                pr_warn("Truncating oversized swap area, only using %luk out of %luk\n",
                        K(maxpages), K(last_page));
        }
	...
}

And if we add pr_err|warn here, we also need add it in setup_swap_map()
when filling swap_map.

> 
> 
> > +       }
> >         for (i = maxpages; i < round_up(maxpages, SWAPFILE_CLUSTER); i++)
> >                 inc_cluster_info_page(si, cluster_info, i);
> >
> > --
> > 2.30.0
> >
> >
> 



  reply	other threads:[~2025-05-30  2:55 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 [this message]
2025-06-11  8:27     ` Kemeng Shi
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=aDkeF/7hkHVfaRHq@MiWiFi-R3L-srv \
    --to=bhe@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ryncsn@gmail.com \
    --cc=shikemeng@huaweicloud.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.