* [PATCH] lib/test_hmm.c: fix harmless shift wrapping bug
@ 2021-03-16 11:43 Dan Carpenter
2021-03-16 16:43 ` Ralph Campbell
0 siblings, 1 reply; 2+ messages in thread
From: Dan Carpenter @ 2021-03-16 11:43 UTC (permalink / raw)
To: Jérôme Glisse, Ralph Campbell
Cc: linux-mm, linux-kernel, kernel-janitors
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")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
lib/test_hmm.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/lib/test_hmm.c b/lib/test_hmm.c
index 80a78877bd93..541466034a6b 100644
--- a/lib/test_hmm.c
+++ b/lib/test_hmm.c
@@ -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;
--
2.30.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] lib/test_hmm.c: fix harmless shift wrapping bug
2021-03-16 11:43 [PATCH] lib/test_hmm.c: fix harmless shift wrapping bug Dan Carpenter
@ 2021-03-16 16:43 ` Ralph Campbell
0 siblings, 0 replies; 2+ messages in thread
From: Ralph Campbell @ 2021-03-16 16:43 UTC (permalink / raw)
To: Dan Carpenter, Jérôme Glisse
Cc: linux-mm, linux-kernel, kernel-janitors
On 3/16/21 4:43 AM, Dan Carpenter wrote:
> 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")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
> lib/test_hmm.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/lib/test_hmm.c b/lib/test_hmm.c
> index 80a78877bd93..541466034a6b 100644
> --- a/lib/test_hmm.c
> +++ b/lib/test_hmm.c
> @@ -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.
Reviewed-by: Ralph Campbell <rcampbell@nvidia.com>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-03-16 16:44 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-16 11:43 [PATCH] lib/test_hmm.c: fix harmless shift wrapping bug Dan Carpenter
2021-03-16 16:43 ` Ralph Campbell
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.