Skip to contentSkip to conversation

lib/test_hmm.c: fix harmless shift wrapping bug

PATCH In review Dan Carpenter 2 participants 2 messages Mar 16, 2021

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/test_hmm.c: fix harmless shift wrapping bug

The "cmd.npages" variable is a u64 that comes from the user. I noticed during review that it could have a shift wrapping bug when it is used in the integer overflow test on the next line.

It turns out this is harmless. The users all do:

	unsigned long size = cmd->npages << PAGE_SHIFT;

and after that "size" is used consistently and "cmd->npages" is never used again. So even when there is an integer overflow, everything works fine.

Even though this is harmless, I believe syzbot will complain and fixing it makes the code easier to read.

Fixes b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")
---
 lib/test_hmm.c | 2 ++
 1 file changed, 2 insertions(+)
lib/test_hmm.c +2 -0
index 80a78877bd93..541466034a6b 100644
@@ -930,6 +930,8 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp,
 
 	if (cmd.addr & ~PAGE_MASK)
 		return -EINVAL;
+	if (cmd.npages > ULONG_MAX >> PAGE_SHIFT)
+		return -EINVAL;
 	if (cmd.addr >= (cmd.addr + (cmd.npages << PAGE_SHIFT)))
 		return -EINVAL;
 
Signature · 1 line
2.30.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 26 lines · Dan Carpenter

The "cmd.npages" variable is a u64 that comes from the user. I noticed during review that it could have a shift wrapping bug when it is used in the integer overflow test on the next line.

It turns out this is harmless. The users all do:

	unsigned long size = cmd->npages << PAGE_SHIFT;

and after that "size" is used consistently and "cmd->npages" is never used again. So even when there is an integer overflow, everything works fine.

Even though this is harmless, I believe syzbot will complain and fixing it makes the code easier to read.

Fixes b2ef9f5a5cb3 ("mm/hmm/test: add selftest driver for HMM")
---
  lib/test_hmm.c | 2 ++
  1 file changed, 2 insertions(+)
lib/test_hmm.c +0 -0
index 80a78877bd93..541466034a6b 100644

lib/test_hmm.c static long dmirror_fops_unlocked_ioctl(struct file *filp, · line 936

Quoted 7 lines
lib/test_hmm.c +2 -0
@@ -930,6 +930,8 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp,
 
  	if (cmd.addr & ~PAGE_MASK)
  		return -EINVAL;
+	if (cmd.npages > ULONG_MAX >> PAGE_SHIFT)
+		return -EINVAL;
  	if (cmd.addr >= (cmd.addr + (cmd.npages << PAGE_SHIFT)))
  		return -EINVAL;

Looks good to me too. Thanks for sending this.