linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][V2][next] mm: selftests: fix potential integer overflow on shift of a int
@ 2021-05-26 17:05 Colin King
  2021-05-26 19:21 ` Jason Gunthorpe
  0 siblings, 1 reply; 2+ messages in thread
From: Colin King @ 2021-05-26 17:05 UTC (permalink / raw)
  To: Jérôme Glisse, Andrew Morton, Ralph Campbell,
	Jason Gunthorpe, Stephen Rothwell, Alistair Popple, 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 making mapped an unsigned long.

Addresses-Coverity: ("Uninitentional integer overflow")
Fixes: 8b2a105c3794 ("mm: selftests for exclusive device memory")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---

V2: Make mapped an unsigned long rather than casting it to unsigned long

---
 lib/test_hmm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lib/test_hmm.c b/lib/test_hmm.c
index 74d69f87691e..8c55c4723692 100644
--- a/lib/test_hmm.c
+++ b/lib/test_hmm.c
@@ -733,7 +733,8 @@ static int dmirror_exclusive(struct dmirror *dmirror,
 
 	mmap_read_lock(mm);
 	for (addr = start; addr < end; addr = next) {
-		int i, mapped;
+		unsigned long mapped;
+		int i;
 
 		if (end < addr + (ARRAY_SIZE(pages) << PAGE_SHIFT))
 			next = end;
-- 
2.31.1



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

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

On Wed, May 26, 2021 at 06:05:30PM +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 making mapped an unsigned long.
> 
> Addresses-Coverity: ("Uninitentional integer overflow")
> Fixes: 8b2a105c3794 ("mm: selftests for exclusive device memory")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
> 
> V2: Make mapped an unsigned long rather than casting it to unsigned long

Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>

Jason


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

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

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

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).