All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH][next] mm: selftests: fix potential integer overflow on shift of a int
@ 2021-05-26 15:09 Colin King
  2021-05-26 16:49 ` Jason Gunthorpe
  0 siblings, 1 reply; 2+ messages in thread
From: Colin King @ 2021-05-26 15:09 UTC (permalink / raw)
  To: Jérôme Glisse, Alistair Popple, Ralph Campbell,
	Stephen Rothwell, Andrew Morton, Jason Gunthorpe, linux-mm
  Cc: kernel-janitors, linux-kernel

From: Colin Ian King <colin.king@canonical.com>

The left shift of the int mapped is evaluated using 32 bit arithmetic
and then assigned to an unsigned long. In the case where mapped is
0x80000 when PAGE_SHIFT is 12 will lead to the upper bits being
sign extended in the unsigned long. Larger values can lead to an
int overflow. Avoid this by casting mapped to unsigned long before
shifting.

Addresses-Coverity: ("Uninitentional integer overflow")
Fixes: 8b2a105c3794 ("mm: selftests for exclusive device memory")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 lib/test_hmm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/test_hmm.c b/lib/test_hmm.c
index 74d69f87691e..b54657701b3a 100644
--- a/lib/test_hmm.c
+++ b/lib/test_hmm.c
@@ -749,7 +749,7 @@ static int dmirror_exclusive(struct dmirror *dmirror,
 			}
 		}
 
-		if (addr + (mapped << PAGE_SHIFT) < next) {
+		if (addr + ((unsigned int)mapped << PAGE_SHIFT) < next) {
 			mmap_read_unlock(mm);
 			mmput(mm);
 			return -EBUSY;
-- 
2.31.1


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

* Re: [PATCH][next] mm: selftests: fix potential integer overflow on shift of a int
  2021-05-26 15:09 [PATCH][next] mm: selftests: fix potential integer overflow on shift of a int Colin King
@ 2021-05-26 16:49 ` Jason Gunthorpe
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Gunthorpe @ 2021-05-26 16:49 UTC (permalink / raw)
  To: Colin King
  Cc: Jérôme Glisse, Alistair Popple, Ralph Campbell,
	Stephen Rothwell, Andrew Morton, linux-mm, kernel-janitors,
	linux-kernel

On Wed, May 26, 2021 at 04:09:47PM +0100, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The left shift of the int mapped is evaluated using 32 bit arithmetic
> and then assigned to an unsigned long. In the case where mapped is
> 0x80000 when PAGE_SHIFT is 12 will lead to the upper bits being
> sign extended in the unsigned long. Larger values can lead to an
> int overflow. Avoid this by casting mapped to unsigned long before
> shifting.
> 
> Addresses-Coverity: ("Uninitentional integer overflow")
> Fixes: 8b2a105c3794 ("mm: selftests for exclusive device memory")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>  lib/test_hmm.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/lib/test_hmm.c b/lib/test_hmm.c
> index 74d69f87691e..b54657701b3a 100644
> +++ b/lib/test_hmm.c
> @@ -749,7 +749,7 @@ static int dmirror_exclusive(struct dmirror *dmirror,
>  			}
>  		}
>  
> -		if (addr + (mapped << PAGE_SHIFT) < next) {
> +		if (addr + ((unsigned int)mapped << PAGE_SHIFT) < next) {

Just fix the type for mapped. It started out as an unsigned long in
dmirror_atomic_map() and wrongly became an int here

Jason

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

end of thread, other threads:[~2021-05-26 16:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-26 15:09 [PATCH][next] mm: selftests: fix potential integer overflow on shift of a int Colin King
2021-05-26 16:49 ` Jason Gunthorpe

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.