All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] mm/rmap: fix signedness bug in make_device_exclusive_range()
@ 2021-06-22 19:03 Dan Carpenter
  2021-06-22 20:04 ` Peter Xu
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Carpenter @ 2021-06-22 19:03 UTC (permalink / raw)
  To: Andrew Morton, Alistair Popple
  Cc: Stephen Rothwell, linux-mm, kernel-janitors

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] mm/rmap: fix signedness bug in make_device_exclusive_range()
  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
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Xu @ 2021-06-22 20:04 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Andrew Morton, Alistair Popple, Stephen Rothwell, linux-mm,
	kernel-janitors

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-06-22 20:04 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 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.