From patchwork Thu Sep 7 17:35:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tycho Andersen X-Patchwork-Id: 9942571 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5B5D7600CB for ; Thu, 7 Sep 2017 17:37:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4C7F0285B5 for ; Thu, 7 Sep 2017 17:37:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 413D6285C2; Thu, 7 Sep 2017 17:37:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from mother.openwall.net (mother.openwall.net [195.42.179.200]) by mail.wl.linuxfoundation.org (Postfix) with SMTP id 67FEA285B5 for ; Thu, 7 Sep 2017 17:37:49 +0000 (UTC) Received: (qmail 19970 invoked by uid 550); 7 Sep 2017 17:37:16 -0000 Mailing-List: contact kernel-hardening-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Delivered-To: mailing list kernel-hardening@lists.openwall.com Received: (qmail 19924 invoked from network); 7 Sep 2017 17:37:15 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=docker.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uSIJLK5JrJe35DMUq1DY+zlDmHZ8FtlouBdKamnMos8=; b=T3EGuBy/RyZmec3XCe9P1exn/0m6u/Z9pCEAsFhJpCtjjDLyN89d4zcaZLemZcx0qc pDSAesDzkJrZ4k/BG/QvuYsNsurLNsnD6n16egA893HqPc/NzRqfzkHb7ZaU85sbggUJ IYNPbwDxCU6YPHM+tUcOUOq4m8ZucR8O7DURg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=uSIJLK5JrJe35DMUq1DY+zlDmHZ8FtlouBdKamnMos8=; b=TC0SReL3QL0dahJG5qAB8JZEnPWSaxoHg9IfuXYIf9W2KEDnFTKLW2Lp+blXbN0EcC HXXQPaPNTdy7aLdbB75L29Plx7GOWx2n8VF0bzfHx5dTSbGCJcbaVresaClPtD6SEVCy 5pruBZpjXcwM3lBs+E0Jgw1SlufxYdjuarfmEZTd3w4DXkqMrYLiN8Z4lozLrcg5Gepu xg0eeDcMXAcHmKCP6bbDTUoG10jkiWvxRA1b2yLle8joh5AoSldZ4QYGaCqnvPSVPd6u sFByZr96ci5GoUJOP0AroneCL+CGGVbSmp4LwnZEcxw2Uz2aE+A5e/Tq0FTAf+NvRH7H tcGQ== X-Gm-Message-State: AHPjjUiq2V8a37ruYk3/dyvkTdp4Ct1ZMGXSMgGo97CyT1c/mDE8IXyB gdPGSGQS7KKbcLaK X-Google-Smtp-Source: AOwi7QB3O9DPK0Lb7qLeA36Yb+eLcMAulFOoIgL0QSKP0MAbsvTuMqTy1CjXa2QIHrfEX2zT71g7KQ== X-Received: by 10.107.69.1 with SMTP id s1mr75923ioa.267.1504805823612; Thu, 07 Sep 2017 10:37:03 -0700 (PDT) From: Tycho Andersen To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, kernel-hardening@lists.openwall.com, Marco Benatto , Juerg Haefliger , Tycho Andersen Date: Thu, 7 Sep 2017 11:35:59 -0600 Message-Id: <20170907173609.22696-2-tycho@docker.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170907173609.22696-1-tycho@docker.com> References: <20170907173609.22696-1-tycho@docker.com> Subject: [kernel-hardening] [PATCH v6 01/11] mm: add MAP_HUGETLB support to vm_mmap X-Virus-Scanned: ClamAV using ClamSMTP vm_mmap is exported, which means kernel modules can use it. In particular, for testing XPFO support, we want to use it with the MAP_HUGETLB flag, so let's support it via vm_mmap. Signed-off-by: Tycho Andersen Tested-by: Marco Benatto --- include/linux/mm.h | 2 ++ mm/mmap.c | 19 +------------------ mm/util.c | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index c1f6c95f3496..5dfe009adcb9 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2141,6 +2141,8 @@ struct vm_unmapped_area_info { extern unsigned long unmapped_area(struct vm_unmapped_area_info *info); extern unsigned long unmapped_area_topdown(struct vm_unmapped_area_info *info); +struct file *map_hugetlb_setup(unsigned long *len, unsigned long flags); + /* * Search for an unmapped address range. * diff --git a/mm/mmap.c b/mm/mmap.c index f19efcf75418..f24fc14808e1 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1490,24 +1490,7 @@ SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len, if (unlikely(flags & MAP_HUGETLB && !is_file_hugepages(file))) goto out_fput; } else if (flags & MAP_HUGETLB) { - struct user_struct *user = NULL; - struct hstate *hs; - - hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); - if (!hs) - return -EINVAL; - - len = ALIGN(len, huge_page_size(hs)); - /* - * VM_NORESERVE is used because the reservations will be - * taken when vm_ops->mmap() is called - * A dummy user value is used because we are not locking - * memory so no accounting is necessary - */ - file = hugetlb_file_setup(HUGETLB_ANON_FILE, len, - VM_NORESERVE, - &user, HUGETLB_ANONHUGE_INODE, - (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); + file = map_hugetlb_setup(&len, flags); if (IS_ERR(file)) return PTR_ERR(file); } diff --git a/mm/util.c b/mm/util.c index 9ecddf568fe3..93c253512aaa 100644 --- a/mm/util.c +++ b/mm/util.c @@ -340,6 +340,29 @@ unsigned long vm_mmap_pgoff(struct file *file, unsigned long addr, return ret; } +struct file *map_hugetlb_setup(unsigned long *len, unsigned long flags) +{ + struct user_struct *user = NULL; + struct hstate *hs; + + hs = hstate_sizelog((flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); + if (!hs) + return ERR_PTR(-EINVAL); + + *len = ALIGN(*len, huge_page_size(hs)); + + /* + * VM_NORESERVE is used because the reservations will be + * taken when vm_ops->mmap() is called + * A dummy user value is used because we are not locking + * memory so no accounting is necessary + */ + return hugetlb_file_setup(HUGETLB_ANON_FILE, *len, + VM_NORESERVE, + &user, HUGETLB_ANONHUGE_INODE, + (flags >> MAP_HUGE_SHIFT) & MAP_HUGE_MASK); +} + unsigned long vm_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flag, unsigned long offset) @@ -349,6 +372,15 @@ unsigned long vm_mmap(struct file *file, unsigned long addr, if (unlikely(offset_in_page(offset))) return -EINVAL; + if (flag & MAP_HUGETLB) { + if (file) + return -EINVAL; + + file = map_hugetlb_setup(&len, flag); + if (IS_ERR(file)) + return PTR_ERR(file); + } + return vm_mmap_pgoff(file, addr, len, prot, flag, offset >> PAGE_SHIFT); } EXPORT_SYMBOL(vm_mmap);