From: Kiryl Shutsemau <kas@kernel.org>
To: Sarthak Sharma <sarthak.sharma@arm.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
David Hildenbrand <david@kernel.org>,
Jason Gunthorpe <jgg@ziepe.ca>,
John Hubbard <jhubbard@nvidia.com>, Peter Xu <peterx@redhat.com>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 1/2] mm/gup_test: prevent overflow in GUP batch calculation
Date: Tue, 1 Sep 2026 11:35:18 +0100 [thread overview]
Message-ID: <apaqJk91uAN-zD66@thinkstation> (raw)
In-Reply-To: <20260901083452.115365-2-sarthak.sharma@arm.com>
On Tue, Sep 01, 2026 at 02:04:51PM +0530, Sarthak Sharma wrote:
> __gup_test_ioctl() calculates the end of a GUP batch using:
>
> next = addr + nr * PAGE_SIZE;
>
> If nr is too large, it can cause the next to overflow and wrap around.
> If it wraps, the next > end check is bypassed and a large value
> of nr is passed to the gup call, even though the pages array was
> allocated according to gup->size. This can lead to out of bounds writes.
>
> Compare nr with the number of pages remaining before performing
> the multiplication. Clamp it to remaining range so that next does
> not overflow or exceed end.
>
> Fixes: 64c349f4ae78 ("mm: add infrastructure for get_user_pages_fast() benchmarking")
> Signed-off-by: Sarthak Sharma <sarthak.sharma@arm.com>
> ---
> mm/gup_test.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/mm/gup_test.c b/mm/gup_test.c
> index 44c1cdfb9c37..910cbef709b4 100644
> --- a/mm/gup_test.c
> +++ b/mm/gup_test.c
> @@ -139,10 +139,11 @@ static int __gup_test_ioctl(unsigned int cmd,
> if (nr != gup->nr_pages_per_call)
> break;
>
> - next = addr + nr * PAGE_SIZE;
> - if (next > end) {
> + if (nr > (end - addr) / PAGE_SIZE) {
> next = end;
> nr = (next - addr) / PAGE_SIZE;
> + } else {
> + next = addr + nr * PAGE_SIZE;
> }
>
> switch (cmd) {
What about this:
nr = min(nr, (end - addr) / PAGE_SIZE);
next = addr + nr * PAGE_SIZE;
Seems to be easier to follow, no?
--
Kiryl Shutsemau / Kirill A. Shutemov
next prev parent reply other threads:[~2026-09-01 10:35 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-01 8:34 [PATCH v3 0/2] mm/gup_test: prevent overflow and report actual pinned bytes Sarthak Sharma
2026-09-01 8:34 ` [PATCH v3 1/2] mm/gup_test: prevent overflow in GUP batch calculation Sarthak Sharma
2026-09-01 10:35 ` Kiryl Shutsemau [this message]
2026-09-01 11:22 ` Sarthak Sharma
2026-09-01 13:25 ` Kiryl Shutsemau
2026-09-01 8:34 ` [PATCH v3 2/2] mm/gup_test: report actual pinned bytes Sarthak Sharma
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=apaqJk91uAN-zD66@thinkstation \
--to=kas@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=david@kernel.org \
--cc=jgg@ziepe.ca \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=peterx@redhat.com \
--cc=sarthak.sharma@arm.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