[PATCH][V2][next] mm: selftests: fix potential integer overflow on shift of a int

From: Colin King
Date: Wed May 26 2021 - 13:05:51 EST


From: Colin Ian King <colin.king@xxxxxxxxxxxxx>

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@xxxxxxxxxxxxx>
---

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