From: Dan Carpenter <dan.carpenter@oracle.com>
To: Andrew Morton <akpm@linux-foundation.org>,
Alistair Popple <apopple@nvidia.com>
Cc: Stephen Rothwell <sfr@canb.auug.org.au>,
linux-mm@kvack.org, kernel-janitors@vger.kernel.org
Subject: [PATCH] mm/rmap: fix signedness bug in make_device_exclusive_range()
Date: Tue, 22 Jun 2021 22:03:00 +0300 [thread overview]
Message-ID: <YNIz5NVnZ5GiZ3u1@mwanda> (raw)
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
next reply other threads:[~2021-06-22 19:03 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-06-22 19:03 Dan Carpenter [this message]
2021-06-22 20:04 ` [PATCH] mm/rmap: fix signedness bug in make_device_exclusive_range() Peter Xu
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=YNIz5NVnZ5GiZ3u1@mwanda \
--to=dan.carpenter@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=apopple@nvidia.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).