From patchwork Wed Aug 9 20:07:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tycho Andersen X-Patchwork-Id: 9891875 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 6D48460363 for ; Wed, 9 Aug 2017 20:09:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5F01D2898B for ; Wed, 9 Aug 2017 20:09:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 53B93289E2; Wed, 9 Aug 2017 20:09:34 +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 7FDAE2898B for ; Wed, 9 Aug 2017 20:09:33 +0000 (UTC) Received: (qmail 32243 invoked by uid 550); 9 Aug 2017 20:09:11 -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 32074 invoked from network); 9 Aug 2017 20:09:06 -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=IXDloaTLeeBG1WZdgxZkuxQ9HzeYxo6MTBLEU6u7auk=; b=A7sIkrBvMS/tevDw0mgDuxBQ5DD/ccdwGJP9ADNpQtodocc3ooYkXEKVq5oq0dJcJH i+lf/PoAeAwPnnPGy8rup59zOjllcrfAM7BrpEn6x+D+dIjxw0tPQbC5W30eqM/YzPGk jk48RlYfCHLZd43OzEfmNOLUnTOnypMcZ7Y8s= 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=IXDloaTLeeBG1WZdgxZkuxQ9HzeYxo6MTBLEU6u7auk=; b=UPZb4dv5XfdWB1v8e0ihUZAJ+r3Ei8VuhgHyOppITQyQuBZ18ppa2NxUV8H/O0MPj/ RL+YvzedaS336YjXmrUHOiQR3upjMi60qUgxXyDdEN5k0yNlETkiDUUz8XIU/RyZBsv1 XzLMhXkeVC37eO3O/uDMECyqkf8hewaBMIfeTw7V/LHhk2BSWHBwkKHpPITJx62aNZQO 2h2j9vGR/pY3qZN5SWHCEJUoxaK1VF53ArJiGb6fcCs9yExpyjPALw7KrG/mXHM0WwlJ pFmr51AT9/6d7/JPiezmWAcJwg0jjSOzYeIgj25EL8pxSLxPez54gak38RVvg3nMlNWM jEzA== X-Gm-Message-State: AHYfb5j0ecIBrx0aYBFkPb2it2EpU9qmaXZjcFGAtRTiLFPeCbbblELW bVtkGwa4RdoJjICb X-Received: by 10.36.39.198 with SMTP id g189mr8082611ita.89.1502309335175; Wed, 09 Aug 2017 13:08:55 -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: Wed, 9 Aug 2017 14:07:46 -0600 Message-Id: <20170809200755.11234-2-tycho@docker.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170809200755.11234-1-tycho@docker.com> References: <20170809200755.11234-1-tycho@docker.com> Subject: [kernel-hardening] [PATCH v5 01/10] 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 46b9ac5e8569..e3bce22fe0f7 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2140,6 +2140,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 7b07ec852e01..aac2c58d6f52 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);