Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp10786157imu; Thu, 6 Dec 2018 06:48:42 -0800 (PST) X-Google-Smtp-Source: AFSGD/UzTF82+tHreqOKoFjigXHoU4ejntB+wZa3dX6zHqMTFnEP4ucEiyEICcggHqiMzgaSu4Bj X-Received: by 2002:a17:902:9692:: with SMTP id n18mr29153951plp.333.1544107722309; Thu, 06 Dec 2018 06:48:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544107722; cv=none; d=google.com; s=arc-20160816; b=JWUD9IITqdRGT/GLJq9vlU9XqLDR7fxpP3CVJBoFTB6mdnCdJ7SN6iLoa0b6+et+la Afpyi50DGnTtHyH0d2b2KTtKFViMiGxIaHVRSy04H+dgZlrCsf8foiMFfd/EjSWXs/Br x4w/qMRGLXp1A8fy1IYghIQNOeIYPM7wq71bMV6enxSfqKOwTtBaB44Q8CXiUyJ9r3kh jEvt4vL4TuSu+eEjd1T48oXYX9f514CIyoeeAvmYKuQ1fVXSuuH+Ui6xfpD8VzwRvt+5 ObMtMDhhk6eFxvCc4AkIdXnCAHL1eFvcYp07KngTzKnTUdkEknaerY8Ahuh3ZlkO85vD 59WQ== 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:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=U0u7pODSYxVwU5px1NCIDGz2bTqpAQqI5llCP6uWSEw=; b=LZaTCFY/FXwk0mOWLRTc7QqOXPNQe/hLL96BCOYHHEH+zkYmzw0km1LYdxNhUY8UQh OO5XbImGah/SbkONCPj5XoLcLM3ArhvLfpdmdyNZXETAI7fGzzI07DXkSf93jnHAqeAN aiCZv2VgQA0kNP+Q+7VQaraVyYsRoI1ThqzrUr9STcwJPU24QiAym+GuiLH+NX5jdPgz rMX1uRG29pN4HoTBMgVIVg2npKpGSjTar70T2a1DBNfjxcepbx40kyO/ww/Qyp91fKMN M8KddHEebzc4mAVvu49R3SKUJMgZMYmP63z+pCjxLpJTv6XKx6l416SGORvPpwMPWqHp tzYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="UXxKAsA/"; 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 l184si365099pgd.523.2018.12.06.06.48.25; Thu, 06 Dec 2018 06:48:42 -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; dkim=pass header.i=@kernel.org header.s=default header.b="UXxKAsA/"; 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 S1731265AbeLFOqd (ORCPT + 99 others); Thu, 6 Dec 2018 09:46:33 -0500 Received: from mail.kernel.org ([198.145.29.99]:51244 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729944AbeLFOqa (ORCPT ); Thu, 6 Dec 2018 09:46:30 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 56722214DE; Thu, 6 Dec 2018 14:46:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1544107589; bh=QWoFiP/RLOsDMaadl0975nB55fX4r6E2+SPtINFCxs0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UXxKAsA/7q8SmcvZ2vL/quL7p0RiN5a+ZmV76tq13pfuF8qTtmkZnjWNFlax+83Jl jWb5zpgelABLoHfbHFZoVMFtuTXCRlWaEwPyVRTmmz5LJuXgbGmEuvbAIp7GanBRT4 DJJ6lcT5ygIvl7a8Kc2nyVkEfsKriPfJsfKKNzQI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vegard Nossum , Mike Kravetz , Hillf Danton , Dmitry Vyukov , Michal Hocko , "Kirill A . Shutemov" , Andrey Ryabinin , Naoya Horiguchi , Andrew Morton , Linus Torvalds , Ben Hutchings Subject: [PATCH 4.9 063/101] hugetlbfs: fix offset overflow in hugetlbfs mmap Date: Thu, 6 Dec 2018 15:39:02 +0100 Message-Id: <20181206143015.275040390@linuxfoundation.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181206143011.174892052@linuxfoundation.org> References: <20181206143011.174892052@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable 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 Signed-off-by: Ben Hutchings Signed-off-by: Greg Kroah-Hartman --- fs/hugetlbfs/inode.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -136,17 +136,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; inode_lock(inode); 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, @@ -155,7 +164,7 @@ static int hugetlbfs_file_mmap(struct fi ret = 0; if (vma->vm_flags & VM_WRITE && inode->i_size < len) - inode->i_size = len; + i_size_write(inode, len); out: inode_unlock(inode);