Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2350954imm; Thu, 7 Jun 2018 09:11:38 -0700 (PDT) X-Google-Smtp-Source: ADUXVKK8EROKzw4yypjFSP04JlESlzyr8rVTl5imgv4LE/LpwVzjnlRgPJgS2ouwM9TfvTQVW33G X-Received: by 2002:a17:902:b683:: with SMTP id c3-v6mr2699906pls.158.1528387898264; Thu, 07 Jun 2018 09:11:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528387898; cv=none; d=google.com; s=arc-20160816; b=pZWrkxWBYlgLVh/lgbvxozxFc1igDWO5rFvBx2idEg95t6fJxN2gH6Up9210g5/ErT CR+oH7nGS3VFquRFrOngE+00n+nFtxsJ7pNH/XrIQpz1vS4XXTPqWrUi2GBH/vRdxnJv oSHyR1HnzDzdy7DwpX1/bXX5KjozmENIW7pA3E/SD1Pigo30PvS9FS5Jg3L56UJihoTc DWlEw4i8Ofl7vwA+bVhQwgLarGg91p5VkbV6wGGNl5Mnr77gBnrkDHaJiAxJDvGa0rL8 86ZnsYtQvsvMLDHDqH+H+w9ZCcqtvZo2dQ6HmYVjYO9wPgWVZUrHNMZJ1IncssCiBlKQ bMyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=H3CUEiXKO58h5hF9ZfEQxo6kmjgtBMT11y+lCFPVrkM=; b=rXwSeqj9J3KfYcquayFRBgIEKTNS4D0S6GHJFlxCRH8tdhAgFc6GVGCOeicxoVw9dF por+lwP0kaY9DYJP4MYS5Ok1Jnblz/gXu/wiSaqMAEbfGR0+A2d8OlZC7xWHiwbwMdiq OoC4/LA/RhpAlZi44VQkVP77IHn+LHUwumnJjPRtRVtDbJKm6Byfubia/pFwrAZVqCsz GoDYBCoyUxD/SyUMwO9xYXD42DNG+LBjq/ig6byx8iQK5Az1lDC+ncegpq3EnFYEk4R3 UQH1Ju1OI0sKcvhR7w/H2g4m1UvErUKzhxA+OuXbkr1lxvgIOcACwe3EqBdxp2iylg53 DgPw== 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 z7-v6si12040401pln.145.2018.06.07.09.11.24; Thu, 07 Jun 2018 09:11:38 -0700 (PDT) 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 S934229AbeFGQJH (ORCPT + 99 others); Thu, 7 Jun 2018 12:09:07 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:39219 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932993AbeFGOJC (ORCPT ); Thu, 7 Jun 2018 10:09:02 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvb1-0005Zm-Cp; Thu, 07 Jun 2018 15:08:59 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvax-0002iC-T9; Thu, 07 Jun 2018 15:08:55 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Naoya Horiguchi" , "Mike Kravetz" , "Kirill A . Shutemov" , "Hillf Danton" , "Vegard Nossum" , "Michal Hocko" , "Dmitry Vyukov" , "Linus Torvalds" , "Andrey Ryabinin" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 027/410] hugetlbfs: fix offset overflow in hugetlbfs mmap In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: Mike Kravetz commit 045c7a3f53d9403b62d396b6d051c4be5044cdb4 upstream. If mmap() maps a file, it can be passed an offset into the file at which the mapping is to start. Offset could be a negative value when represented as a loff_t. The offset plus length will be used to update the file size (i_size) which is also a loff_t. Validate the value of offset and offset + length to make sure they do not overflow and appear as negative. Found by syzcaller with commit ff8c0c53c475 ("mm/hugetlb.c: don't call region_abort if region_chg fails") applied. Prior to this commit, the overflow would still occur but we would luckily return ENOMEM. To reproduce: mmap(0, 0x2000, 0, 0x40021, 0xffffffffffffffffULL, 0x8000000000000000ULL); Resulted in, kernel BUG at mm/hugetlb.c:742! Call Trace: hugetlbfs_evict_inode+0x80/0xa0 evict+0x24a/0x620 iput+0x48f/0x8c0 dentry_unlink_inode+0x31f/0x4d0 __dentry_kill+0x292/0x5e0 dput+0x730/0x830 __fput+0x438/0x720 ____fput+0x1a/0x20 task_work_run+0xfe/0x180 exit_to_usermode_loop+0x133/0x150 syscall_return_slowpath+0x184/0x1c0 entry_SYSCALL_64_fastpath+0xab/0xad Fixes: ff8c0c53c475 ("mm/hugetlb.c: don't call region_abort if region_chg fails") Link: http://lkml.kernel.org/r/1491951118-30678-1-git-send-email-mike.kravetz@oracle.com Reported-by: Vegard Nossum Signed-off-by: Mike Kravetz Acked-by: Hillf Danton Cc: Dmitry Vyukov Cc: Michal Hocko Cc: "Kirill A . Shutemov" Cc: Andrey Ryabinin Cc: Naoya Horiguchi Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [bwh: Backported to 3.16: adjust context] Signed-off-by: Ben Hutchings --- fs/hugetlbfs/inode.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -115,17 +115,26 @@ static int hugetlbfs_file_mmap(struct fi 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; mutex_lock(&inode->i_mutex); file_accessed(file); ret = -ENOMEM; - len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); - if (hugetlb_reserve_pages(inode, vma->vm_pgoff >> huge_page_order(h), len >> huge_page_shift(h), vma, @@ -135,7 +144,7 @@ static int hugetlbfs_file_mmap(struct fi ret = 0; hugetlb_prefault_arch_hook(vma->vm_mm); if (vma->vm_flags & VM_WRITE && inode->i_size < len) - inode->i_size = len; + i_size_write(inode, len); out: mutex_unlock(&inode->i_mutex);