linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] hugetlbfs: Fix integer overflow check in  hugetlbfs_file_mmap()
@ 2023-07-10  8:32 Linke Li
  2023-07-10 16:12 ` Markus Elfring
  2023-07-11 11:49 ` David Hildenbrand
  0 siblings, 2 replies; 12+ messages in thread
From: Linke Li @ 2023-07-10  8:32 UTC (permalink / raw)
  To: linux-mm
  Cc: llvm, linux-kernel, trix, ndesaulniers, nathan, muchun.song,
	mike.kravetz, Linke Li

From: Linke Li <lilinke99@gmail.com>

	vma_len = (loff_t)(vma->vm_end - vma->vm_start);
	len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
	/* check for overflow */
	if (len < vma_len)
		return -EINVAL;

The existing code includes an integer overflow check, which indicates
that the variable len has the potential to overflow, leading to undefined
 behavior according to the C standard. However, both GCC and Clang
compilers may eliminate this overflow check based on the assumption
that there will be no undefined behavior. Although the Linux kernel
disables these optimizations by using the -fno-strict-overflow option,
there is still a risk if the compilers make mistakes in the future.

To address this potential issue, this patch introduces a new check
that effectively prevents integer overflow. This check ensures the
safe operation of the code even when the Linux kernel is compiled
without the -fno-strict-overflow option, providing an added layer
of protection against potential issues caused by compiler behavior.

Signed-off-by: Linke Li <lilinke99@gmail.com>
---
 fs/hugetlbfs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 7b17ccfa039d..1b4648a8e296 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -157,7 +157,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
 	vma_len = (loff_t)(vma->vm_end - vma->vm_start);
 	len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
 	/* check for overflow */
-	if (len < vma_len)
+	if (vma_len > LLONG_MAX - ((loff_t)vma->vm_pgoff << PAGE_SHIFT))
 		return -EINVAL;
 
 	inode_lock(inode);
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 12+ messages in thread
* Re: [PATCH] hugetlbfs: Fix integer overflow check in hugetlbfs_file_mmap()
@ 2023-07-10  9:02 Alexey Dobriyan
  0 siblings, 0 replies; 12+ messages in thread
From: Alexey Dobriyan @ 2023-07-10  9:02 UTC (permalink / raw)
  To: lilinke99; +Cc: linux-mm, linux-kernel

> --- a/fs/hugetlbfs/inode.c
> +++ b/fs/hugetlbfs/inode.c
> @@ -157,7 +157,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
>  	vma_len = (loff_t)(vma->vm_end - vma->vm_start);
>  	len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
>  	/* check for overflow */
> -	if (len < vma_len)
> +	if (vma_len > LLONG_MAX - ((loff_t)vma->vm_pgoff << PAGE_SHIFT))
>  		return -EINVAL;

Proper fix is to make everything unsigned probably.


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

end of thread, other threads:[~2023-07-20  6:25 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-10  8:32 [PATCH] hugetlbfs: Fix integer overflow check in hugetlbfs_file_mmap() Linke Li
2023-07-10 16:12 ` Markus Elfring
2023-07-11 11:49 ` David Hildenbrand
2023-07-12 23:58   ` Mike Kravetz
2023-07-13  7:57     ` linke li
2023-07-13 15:10       ` Dan Carpenter
2023-07-19 23:22         ` Mike Kravetz
2023-07-20  6:25           ` linke li
2023-07-13  7:55   ` linke li
2023-07-14 12:52     ` Matthew Wilcox
2023-07-17  7:33       ` linke li
  -- strict thread matches above, loose matches on Subject: below --
2023-07-10  9:02 Alexey Dobriyan

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