Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2353266imm; Thu, 7 Jun 2018 09:13:47 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLCR/3S91vulhteLwjVScy1usxrH9D7L1pPbHpMTIybeB7sUh10Qkh+CbptQA6Wh67AQq8+ X-Received: by 2002:a17:902:28ab:: with SMTP id f40-v6mr2698656plb.208.1528388027595; Thu, 07 Jun 2018 09:13:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528388027; cv=none; d=google.com; s=arc-20160816; b=PwQI0nLk+AMOd3KaSVyjQRcou6YT9tOjaPZEZwbvMsADBr/MBRfyziwAverOQ4jQI2 1xR0R45B6xXKhBPJ7H8tHovgW2KuPNSOXYc7fkVwFqsl24RMBn5x3tSlgw1hgwbtefHb JB1YAACz9opG65g1TywkLk19lErzPEEZPN7nNtD3KqYcPyGjJepy09xa510+Rkd8Suvh bjXAEzxf8QWYmuf9a7ZTglkE1xwbZoTbi73AcV8A+FPNzt5zHcT4njc4dFP3B5Hay0qr r71c/Tv5SUMRfOQ99kwEW6s4dKAIzBl3eFlQdIl/iwOl9Y1aNSTT1dWrCj2nPMT3kkLk 3Etw== 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=VKB8yfhQgpDqTrPGsjceQgTByF8U3Sp5zhMAb8uTsRM=; b=CAt9RLSKCAwJvKP5VUi7R6YuF8XpAr9F+GHOaxEiA9JMmvktwrpN7ljnlNRgGJzBtw ZXCY1GFnCWBMpI5Lg20y/ubWGF4GClQZWvKic4K6M+RXwL+U/akkg7Kod/FMTjIDVQ3X +Px5x+aWjXTG5oFlqijsS2mchxsFc/ZlnuvPSuDF9Uc3mf4rw47Aqh1R/cACmQGYYRIn KAEUoTVBRmUIyNXORm26UaRfLXmhA4PVk5rgGM/OjYiJYLRaWwMcfacrZazcsYV/BsKb JKPBSRZHctwPF5xd1VK7hKR3i0KJr9wQxG95VLrhTxuZ4I0dOhtXOUkn/Z8LgnQyCLSm 3xkg== 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 b9-v6si47561416plk.111.2018.06.07.09.13.33; Thu, 07 Jun 2018 09:13:47 -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 S934338AbeFGQMK (ORCPT + 99 others); Thu, 7 Jun 2018 12:12:10 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:39166 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932969AbeFGOJB (ORCPT ); Thu, 7 Jun 2018 10:09:01 -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-0005Zn-C5; Thu, 07 Jun 2018 15:08:59 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvax-0002iG-Ts; 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, "Michal Hocko" , "Nic Losby" , "Mike Kravetz" , "Kirill A . Shutemov" , "Yisheng Xie" , "Linus Torvalds" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 028/410] hugetlbfs: check for pgoff value overflow 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 63489f8e821144000e0bdca7e65a8d1cc23a7ee7 upstream. 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. The previous overflow fix to this code was incomplete and did not take the remap_file_pages system call into account. [mike.kravetz@oracle.com: v3] Link: http://lkml.kernel.org/r/20180309002726.7248-1-mike.kravetz@oracle.com [akpm@linux-foundation.org: include mmdebug.h] [akpm@linux-foundation.org: fix -ve left shift count on sh] Link: http://lkml.kernel.org/r/20180308210502.15952-1-mike.kravetz@oracle.com Fixes: 045c7a3f53d9 ("hugetlbfs: fix offset overflow in hugetlbfs mmap") Signed-off-by: Mike Kravetz Reported-by: Nic Losby Acked-by: Michal Hocko Cc: "Kirill A . Shutemov" Cc: Yisheng Xie Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds [bwh: Backported to 3.16: - Use a conditional WARN() instead of VM_WARN() - Adjust context] Signed-off-by: Ben Hutchings --- fs/hugetlbfs/inode.c | 17 ++++++++++++++--- mm/hugetlb.c | 7 +++++++ 2 files changed, 21 insertions(+), 3 deletions(-) --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -97,6 +97,16 @@ static void huge_pagevec_release(struct pagevec_reinit(pvec); } +/* + * Mask used when checking the page offset value passed in via system + * calls. This value will be converted to a loff_t which is signed. + * Therefore, we want to check the upper PAGE_SHIFT + 1 bits of the + * value. The extra bit (- 1 in the shift value) is to take the sign + * bit into account. + */ +#define PGOFF_LOFFT_MAX \ + (((1UL << (PAGE_SHIFT + 1)) - 1) << (BITS_PER_LONG - (PAGE_SHIFT + 1))) + static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) { struct inode *inode = file_inode(file); @@ -116,12 +126,13 @@ static int hugetlbfs_file_mmap(struct fi 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 & PGOFF_LOFFT_MAX) return -EINVAL; + /* must be huge page aligned */ if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT)) return -EINVAL; --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -3504,6 +3505,14 @@ int hugetlb_reserve_pages(struct inode * struct hugepage_subpool *spool = subpool_inode(inode); struct resv_map *resv_map; + /* This should never happen */ + if (from > to) { +#ifdef CONFIG_DEBUG_VM + WARN(1, "%s called with a negative range\n", __func__); +#endif + return -EINVAL; + } + /* * Only apply hugepage reservation if asked. At fault time, an * attempt will be made for VM_NORESERVE to allocate a page