linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Mike Kravetz <mike.kravetz@oracle.com>
To: Matthew Wilcox <willy@infradead.org>
Cc: Linke Li <lilinke99@foxmail.com>,
	linux-mm@kvack.org, muchun.song@linux.dev, nathan@kernel.org,
	ndesaulniers@google.com, trix@redhat.com,
	linux-kernel@vger.kernel.org, llvm@lists.linux.dev,
	dan.carpenter@linaro.org, Linke Li <lilinke99@gmail.com>
Subject: Re: [PATCH v3] hugetlbfs: Fix integer overflow check in hugetlbfs_file_mmap()
Date: Mon, 24 Jul 2023 11:59:00 -0700	[thread overview]
Message-ID: <20230724185900.GA3240@monkey> (raw)
In-Reply-To: <ZL30Vaj8RYxmsF9o@casper.infradead.org>

On 07/24/23 04:47, Matthew Wilcox wrote:
> On Thu, Jul 20, 2023 at 10:49:52PM +0800, Linke Li wrote:
> > +++ b/fs/hugetlbfs/inode.c
> > @@ -154,10 +154,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
> >  	if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
> >  		return -EINVAL;
> >  
> > -	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 (check_add_overflow(vma_len, (loff_t)vma->vm_pgoff << PAGE_SHIFT, &len))
> >  		return -EINVAL;
> 
> Doesn't this check duplicate that performed by file_mmap_ok()?  Can't we
> just delete the check, or is there a code path that leads here while
> avoiding file_mmap_ok()?

Thanks for pointing that out.
Yes, from my reading/understanding that is a repeat.

It looks like most of the overflow checking in hugetlbfs_file_mmap is a
repeat of checks done previously.  I remember adding this code in
response to a checker or someone pointing out the potential for overflow:

	/*
	 * page based offset in vm_pgoff could be sufficiently large to
	 * overflow a loff_t when converted to byte offset.  This can
	 * only happen on architectures where sizeof(loff_t) ==
	 * sizeof(unsigned long).  So, only check in those instances.
	 */
	if (sizeof(unsigned long) == sizeof(loff_t)) {
		if (vma->vm_pgoff & PGOFF_LOFFT_MAX)
			return -EINVAL;
	}

However, file_mmap_ok seems to handle this as well.  The important thing that
needs to be done in hugetlbfs_file_mmap is checking for huge page alignment.

I have added this code cleanup to my list if someone does not do it first.
-- 
Mike Kravetz


  reply	other threads:[~2023-07-24 18:59 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-20 14:49 [PATCH v3] hugetlbfs: Fix integer overflow check in hugetlbfs_file_mmap() Linke Li
2023-07-20 18:41 ` Mike Kravetz
2023-07-20 19:03 ` Nick Desaulniers
2023-07-20 23:36 ` kernel test robot
2023-07-24  3:47 ` Matthew Wilcox
2023-07-24 18:59   ` Mike Kravetz [this message]
2023-07-24  8:11 ` kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230724185900.GA3240@monkey \
    --to=mike.kravetz@oracle.com \
    --cc=dan.carpenter@linaro.org \
    --cc=lilinke99@foxmail.com \
    --cc=lilinke99@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=llvm@lists.linux.dev \
    --cc=muchun.song@linux.dev \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=trix@redhat.com \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).