From: Peter Xu <peterx@redhat.com>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Alistair Popple <apopple@nvidia.com>,
Stephen Rothwell <sfr@canb.auug.org.au>,
linux-mm@kvack.org, kernel-janitors@vger.kernel.org
Subject: Re: [PATCH] mm/rmap: fix signedness bug in make_device_exclusive_range()
Date: Tue, 22 Jun 2021 16:04:49 -0400 [thread overview]
Message-ID: <YNJCYf/VS4Nfm9j6@t490s> (raw)
In-Reply-To: <YNIz5NVnZ5GiZ3u1@mwanda>
On Tue, Jun 22, 2021 at 10:03:00PM +0300, Dan Carpenter wrote:
> The get_user_pages_remote() function returns a long type, but we are
> using "unsigned long i;" as the list iterator. If "npages" is -ENOMEM,
> the comparison "i < npages" is type promoted and "npages" becomes a very
> high positive value. The loop will then iterate until the kernel
> crashes.
>
> There are two ways to fix this. Declare "i" as a long type or add an
> explicit check for get_user_pages_remote() error returns. Either
> approach will work so let's do both.
>
> Fixes: fa1e686e5f53 ("mm: device exclusive memory access")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> mm/rmap.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/mm/rmap.c b/mm/rmap.c
> index e5210dde0c4d..fb5c59b95826 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -2187,11 +2187,14 @@ int make_device_exclusive_range(struct mm_struct *mm, unsigned long start,
> void *owner)
> {
> long npages = (end - start) >> PAGE_SHIFT;
> - unsigned long i;
> + long i;
>
> npages = get_user_pages_remote(mm, start, npages,
> FOLL_GET | FOLL_WRITE | FOLL_SPLIT_PMD,
> pages, NULL, NULL);
> + if (npages < 0)
> + return npages;
> +
> for (i = 0; i < npages; i++, start += PAGE_SIZE) {
> if (!trylock_page(pages[i])) {
> put_page(pages[i]);
> --
> 2.30.2
Ouch.. The check should be enough, imho; looping over an long seems a tiny
little bit odd, but still looks okay.
Reviewed-by: Peter Xu <peterx@redhat.com>
Thanks,
--
Peter Xu
prev parent reply other threads:[~2021-06-22 20:04 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-22 19:03 [PATCH] mm/rmap: fix signedness bug in make_device_exclusive_range() Dan Carpenter
2021-06-22 20:04 ` Peter Xu [this message]
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=YNJCYf/VS4Nfm9j6@t490s \
--to=peterx@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.com \
--cc=dan.carpenter@oracle.com \
--cc=kernel-janitors@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=sfr@canb.auug.org.au \
/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).