Received: by 10.223.185.116 with SMTP id b49csp5736246wrg; Wed, 7 Mar 2018 17:37:56 -0800 (PST) X-Google-Smtp-Source: AG47ELv3PM7qVyNn8HZO7dbBGMPV6KNLvDNnAmZ04bWdCv7lZHtXxGaXXtmZMFdt7HdAG8mm9B0q X-Received: by 10.101.76.134 with SMTP id m6mr20217569pgt.445.1520473076454; Wed, 07 Mar 2018 17:37:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520473076; cv=none; d=google.com; s=arc-20160816; b=K5/N0ZBMbllGFiXB39qWSDnuUYI323H4YJqHKVtJipiPTyKSSYdD9gXnzrlnGfMgP0 uIWPTik43btMcZGuXOQtk2jAEjHrB7uhusiJfzgz8UPTMR6/pVAm/mIHOdWxe2zPXQPC sPNMdQmxoW2A+fPAwz4eBGemffgufbgPlLQqHuG2ZeKiOZRAviqoYC4QHBT2D88wRgce JmX34kxW/qt0EBW7XzhZtU+4tskAdW7AQnz0vGIpca51jO4nkOxk3T8BXiFEYyAhe7UT rXZ8PA/39oPbwaArEjHe9/gAfvHCnsRzi2IY1/2mwa+RXndX2tJEea6peyCYChRXjFwy EvVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:in-reply-to :mime-version:user-agent:date:message-id:from:cc:references:to :subject:arc-authentication-results; bh=y+GUy89KTXGZHeoxojiGzxgWl5tH6/nCyvzuNG+Tr7E=; b=minjBSJiun22bSftLkngGowoXhGEM2ipvHZg/ZodQvJqelbOmHBGBCgm458WAFXiS4 /NRQFWKLO44gfcJmw6TU36ydDW5KfPF5VALuZQiHtD/1CjUHUNY2188JSunlFp6PXots x5+RGgfxv6HezO5viWYjEea22XqIkmcwMNHttIoTbu6jfGcGczjDbfwIPRMArTTCDh0K 0X+rX/47NduAexcBl65+FLQi6ia2d9xTjVEJyXFaTX6Ls0U5hDxf9wSLhr2uKOW+PqPT BOFw0qW12gyRO8tWj9an/vYGKpgKyE9by91RR3NezG64I7iHg6AYWvyPXCa9D2pD1PEa WNRQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x5-v6si13724285plv.94.2018.03.07.17.37.42; Wed, 07 Mar 2018 17:37:56 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934574AbeCHBgJ (ORCPT + 99 others); Wed, 7 Mar 2018 20:36:09 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:6175 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S933351AbeCHBgH (ORCPT ); Wed, 7 Mar 2018 20:36:07 -0500 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id E8FAE205D19F1; Thu, 8 Mar 2018 09:35:54 +0800 (CST) Received: from [127.0.0.1] (10.177.29.40) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.361.1; Thu, 8 Mar 2018 09:35:53 +0800 Subject: Re: [PATCH] hugetlbfs: check for pgoff value overflow To: Mike Kravetz , , , References: <20180306133135.4dc344e478d98f0e29f47698@linux-foundation.org> <20180307235923.12469-1-mike.kravetz@oracle.com> CC: Michal Hocko , "Kirill A . Shutemov" , Hillf Danton , Nic Losby , Andrew Morton From: Yisheng Xie Message-ID: <8a0863a2-1890-11e0-1fc2-c96e1794e809@huawei.com> Date: Thu, 8 Mar 2018 09:35:52 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.1.0 MIME-Version: 1.0 In-Reply-To: <20180307235923.12469-1-mike.kravetz@oracle.com> Content-Type: text/plain; charset="windows-1252" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.177.29.40] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Mike, On 2018/3/8 7:59, Mike Kravetz wrote: > A vma with vm_pgoff large enough to overflow a loff_t type when > converted to a byte offset can be passed via the remap_file_pages > system call. The hugetlbfs mmap routine uses the byte offset to > calculate reservations and file size. > > A sequence such as: > mmap(0x20a00000, 0x600000, 0, 0x66033, -1, 0); > remap_file_pages(0x20a00000, 0x600000, 0, 0x20000000000000, 0); > will result in the following when task exits/file closed, > kernel BUG at mm/hugetlb.c:749! > Call Trace: > hugetlbfs_evict_inode+0x2f/0x40 > evict+0xcb/0x190 > __dentry_kill+0xcb/0x150 > __fput+0x164/0x1e0 > task_work_run+0x84/0xa0 > exit_to_usermode_loop+0x7d/0x80 > do_syscall_64+0x18b/0x190 > entry_SYSCALL_64_after_hwframe+0x3d/0xa2 > > The overflowed pgoff value causes hugetlbfs to try to set up a > mapping with a negative range (end < start) that leaves invalid > state which causes the BUG. > > Reported-by: Nic Losby > Signed-off-by: Mike Kravetz > --- > fs/hugetlbfs/inode.c | 7 ++++--- > 1 file changed, 4 insertions(+), 3 deletions(-) > > diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c > index 8fe1b0aa2896..cb288dec5564 100644 > --- a/fs/hugetlbfs/inode.c > +++ b/fs/hugetlbfs/inode.c > @@ -127,12 +127,13 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) > vma->vm_ops = &hugetlb_vm_ops; > > /* > - * Offset passed to mmap (before page shift) could have been > - * negative when represented as a (l)off_t. > + * page based offset in vm_pgoff could be sufficiently large to > + * overflow a (l)off_t when converted to byte offset. > */ > - if (((loff_t)vma->vm_pgoff << PAGE_SHIFT) < 0) > + if (vma->vm_pgoff && ((loff_t)vma->vm_pgoff << PAGE_SHIFT) <= 0) > return -EINVAL; This seems still no the right fix, taking the following case as an example: mmap(0x20a00000, 0x600000, 0, 0x66033, -1, 0); remap_file_pages(0x20a00000, 0x600000, 0, 0x0020001000000000, 0); You should just check the highest PAGE_SHIFT+1 bits of pgoff in you want check at this point, right? However, region_chg makes me a litter puzzle that when its return value < 0, sometime adds_in_progress is added like this case, while sometime it is not. so why not just change at the beginning of region_chg ? if (f > t) return -EINVAL; Thanks Yisheng > > + /* must be huge page aligned */ > if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT)) > return -EINVAL; > >