Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754943AbdDOW7W (ORCPT ); Sat, 15 Apr 2017 18:59:22 -0400 Received: from userp1040.oracle.com ([156.151.31.81]:28031 "EHLO userp1040.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754537AbdDOW7T (ORCPT ); Sat, 15 Apr 2017 18:59:19 -0400 Subject: Re: [PATCH] hugetlbfs: fix offset overflow in huegtlbfs mmap To: Naoya Horiguchi References: <1491951118-30678-1-git-send-email-mike.kravetz@oracle.com> <20170414033210.GA12973@hori1.linux.bs1.fc.nec.co.jp> Cc: "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , Vegard Nossum , Dmitry Vyukov , Hillf Danton , Michal Hocko , "Kirill A . Shutemov" , Andrey Ryabinin , Andrew Morton From: Mike Kravetz Message-ID: Date: Sat, 15 Apr 2017 15:58:59 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <20170414033210.GA12973@hori1.linux.bs1.fc.nec.co.jp> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Source-IP: aserv0021.oracle.com [141.146.126.233] Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1856 Lines: 48 On 04/13/2017 08:32 PM, Naoya Horiguchi wrote: > On Tue, Apr 11, 2017 at 03:51:58PM -0700, Mike Kravetz wrote: > ... >> diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c >> index 7163fe0..dde8613 100644 >> --- a/fs/hugetlbfs/inode.c >> +++ b/fs/hugetlbfs/inode.c >> @@ -136,17 +136,26 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) >> vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND; >> vma->vm_ops = &hugetlb_vm_ops; >> >> + /* >> + * Offset passed to mmap (before page shift) could have been >> + * negative when represented as a (l)off_t. >> + */ >> + if (((loff_t)vma->vm_pgoff << PAGE_SHIFT) < 0) >> + return -EINVAL; >> + >> 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) >> + return -EINVAL; > > Andrew sent this patch to Linus today, so I know it's a little too late, but > I think that getting len directly from vma like below might be a simpler fix. > > len = (loff_t)(vma->vm_end - vma->vm_start + (vma->vm_pgoff << PAGE_SHIFT)); > > This shouldn't overflow because vma->vm_{end|start|pgoff} are unsigned long, > but if worried you can add VM_BUG_ON_VMA(len < 0, vma). Thanks Naoya, I am pretty sure the checks are necessary. You are correct in that vma->vm_{end|start|pgoff} are unsigned long. However, pgoff can be a REALLY big value that becomes negative when shifted. Note that pgoff is simply the off_t offset value passed from the user cast to unsigned long and shifted right by PAGE_SHIFT. There is nothing to prevent a user from passing a 'signed' negative value. In the reproducer provided, the value passed from user space is 0x8000000000000000ULL. -- Mike Kravetz