From patchwork Thu Aug 8 06:17:43 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083201 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 29B5813AC for ; Thu, 8 Aug 2019 06:19:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1707B28AB0 for ; Thu, 8 Aug 2019 06:19:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0B21128AAC; Thu, 8 Aug 2019 06:19:17 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 98E2C28AA2 for ; Thu, 8 Aug 2019 06:19:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BFD706B0007; Thu, 8 Aug 2019 02:19:15 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BAD786B0008; Thu, 8 Aug 2019 02:19:15 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A76116B000A; Thu, 8 Aug 2019 02:19:15 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id 5B3EF6B0007 for ; Thu, 8 Aug 2019 02:19:15 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id k37so841662eda.7 for ; Wed, 07 Aug 2019 23:19:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=zeV7cRDg//NBM0c2n9mDOnVPZ/BNPft3Bx8vY7oRip8=; b=Nxv5H8iC0tkOhmrol1dUP8wbf3PNXdGZBncEMzOI1QDsu3CHUDOQ8k8fmVfkiOxg2p 1nlidk4lM0m8DhB1F6fBgwrI80vveAhV7eyn9ymQkjIiMcdUpS9Fzckq3Jf/lJB/1pXR NQONrF6LDTQd8uvRsoX+bk+LlKpwdo+5zbfRZXwq4rHdmnglcKmX63pg18bH692xyY3R vuFlbYGeN/Du6TEtZwM3nxIZxvAIfOL8juoZIfL6Rognmtfcb8hAj7OQiF9+u8JECofu LmqPnODU29SVfxxa1ftvNVMPoymlQDn9cQjw6t1UK3Vb7pUZOvK5GyRI6Tc0+cwmB2q4 SKLg== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXInzq+oJig5+lnadiB5W0oYWgU4geXuFFnQKSzR4eCArtb4QY0 wk7Vca/JE5tH2lT/MPAHK/cxrNwq7YPcz9oytlOs8rTiYvPyiVU2vsIX2jj3hBtTAbPKHYgiESU gJoGceE2D6wCjU5RgVROFyN4U15m8PUak9rVu2uy/Ip++byRUNZ8ov+An4HnWiCo= X-Received: by 2002:a50:f5f5:: with SMTP id x50mr13636605edm.89.1565245154946; Wed, 07 Aug 2019 23:19:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqzGAtB6vu3W7RGNOTqs+ED2mQ9NYG1MviLMzUxW/uMyxCszPOh6q/WI5Kymc+9pQq+j3rW1 X-Received: by 2002:a50:f5f5:: with SMTP id x50mr13636573edm.89.1565245154211; Wed, 07 Aug 2019 23:19:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245154; cv=none; d=google.com; s=arc-20160816; b=XZpaWtMY7T9s1lAMQxx1tNOkN/WdP87bZC/zRNY9avsvZ2OK6yS523mHSBFBJsnO4n iUCX4QRT9cHASp69V1PGD7Fma7yJQE8z/IqzowZgBcsA9+Yq2vSIU2Zig2Te1XJDS70a QTtHqBp+yxThGh4xuS5yXvZLgnhLfb4yPf055b2pYY2Oa6hmdYsuioGr0PpzBNN6Tmtu yqr2UGisE5rjJk8Pex4ybxMc40aCp1LGp/aS52XoxlMZl9G94mFEgtobjRzyRZNEivuI IuinOhiFianGrNHM9fjcXa/EQcLRryE4FG3YK23Bj9MuXuEjjmtuachzp/LqlEwO/BSm kGbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=zeV7cRDg//NBM0c2n9mDOnVPZ/BNPft3Bx8vY7oRip8=; b=EWw8896Ln3D34DRktCzpH7qf/UGdAboMYUl+6wLuyBPylpfx97i3+pfrQQL0OQEVgv vyZZErnvokHKfPF3PDa8zNHX5WUOP3AfbyjwFLGhafirbcb3YhidjmNZjI1AmhAuHv1n ZYyNe6sclnm9PgBi2badz5xM230bUfHd1E6MwbyCtXI01yHBGi2CmmdxH15u5UdgIv9E 1kgnmkxSc73k4rp6bc1bm/FBYym2ZWfE0R83SgztXEjgiZyZzDsXtfyftMO2DLjca9xW QneJCuikOOwMjVbCKDKiCat5rtSafi6KFKPm1f7ASuWV6eSShh1urOm14wuJjGHuRLGw 604w== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net. [217.70.183.200]) by mx.google.com with ESMTPS id j5si29948108ejb.211.2019.08.07.23.19.14 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:19:14 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.200; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 3FDAC20003; Thu, 8 Aug 2019 06:19:07 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 01/14] mm, fs: Move randomize_stack_top from fs to mm Date: Thu, 8 Aug 2019 02:17:43 -0400 Message-Id: <20190808061756.19712-2-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP This preparatory commit moves this function so that further introduction of generic topdown mmap layout is contained only in mm/util.c. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Reviewed-by: Christoph Hellwig Reviewed-by: Luis Chamberlain --- fs/binfmt_elf.c | 20 -------------------- include/linux/mm.h | 2 ++ mm/util.c | 22 ++++++++++++++++++++++ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c index d4e11b2e04f6..cec3b4146440 100644 --- a/fs/binfmt_elf.c +++ b/fs/binfmt_elf.c @@ -670,26 +670,6 @@ static unsigned long load_elf_interp(struct elfhdr *interp_elf_ex, * libraries. There is no binary dependent code anywhere else. */ -#ifndef STACK_RND_MASK -#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ -#endif - -static unsigned long randomize_stack_top(unsigned long stack_top) -{ - unsigned long random_variable = 0; - - if (current->flags & PF_RANDOMIZE) { - random_variable = get_random_long(); - random_variable &= STACK_RND_MASK; - random_variable <<= PAGE_SHIFT; - } -#ifdef CONFIG_STACK_GROWSUP - return PAGE_ALIGN(stack_top) + random_variable; -#else - return PAGE_ALIGN(stack_top) - random_variable; -#endif -} - static int load_elf_binary(struct linux_binprm *bprm) { struct file *interpreter = NULL; /* to shut gcc up */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 0334ca97c584..ae0e5d241eb8 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2351,6 +2351,8 @@ extern int install_special_mapping(struct mm_struct *mm, unsigned long addr, unsigned long len, unsigned long flags, struct page **pages); +unsigned long randomize_stack_top(unsigned long stack_top); + extern unsigned long get_unmapped_area(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); extern unsigned long mmap_region(struct file *file, unsigned long addr, diff --git a/mm/util.c b/mm/util.c index e6351a80f248..15a4fb0f5473 100644 --- a/mm/util.c +++ b/mm/util.c @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include @@ -293,6 +295,26 @@ int vma_is_stack_for_current(struct vm_area_struct *vma) return (vma->vm_start <= KSTK_ESP(t) && vma->vm_end >= KSTK_ESP(t)); } +#ifndef STACK_RND_MASK +#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) /* 8MB of VA */ +#endif + +unsigned long randomize_stack_top(unsigned long stack_top) +{ + unsigned long random_variable = 0; + + if (current->flags & PF_RANDOMIZE) { + random_variable = get_random_long(); + random_variable &= STACK_RND_MASK; + random_variable <<= PAGE_SHIFT; + } +#ifdef CONFIG_STACK_GROWSUP + return PAGE_ALIGN(stack_top) + random_variable; +#else + return PAGE_ALIGN(stack_top) - random_variable; +#endif +} + #if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { From patchwork Thu Aug 8 06:17:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083215 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A5A4F1709 for ; Thu, 8 Aug 2019 06:20:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9534D28AAC for ; Thu, 8 Aug 2019 06:20:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8899028ABE; Thu, 8 Aug 2019 06:20:26 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 63ECF28AAC for ; Thu, 8 Aug 2019 06:20:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8A0986B0007; Thu, 8 Aug 2019 02:20:20 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 853EE6B0008; Thu, 8 Aug 2019 02:20:20 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 740856B000A; Thu, 8 Aug 2019 02:20:20 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id 2AADA6B0007 for ; Thu, 8 Aug 2019 02:20:20 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id b12so57581113eds.14 for ; Wed, 07 Aug 2019 23:20:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=LklZI+jjNy9P/Vf1oDAlKWfBdOZ3w4ODTtcULXIYH+w=; b=Ort5jjXsPP4zW9exHzbDeYgE3Gh5oisY0x+gpqe6n4lia6oPwCnkFNJNzz3cdyotzt gkH21Ct/3AEv5x3Um6vcDDWi40JwbgPnf56HcH1WkcKNBDNnf0SCcQdMu0B/gnXsPY3n vtEq0iIhD+RTvtpSXJHZHteFWGjBKiJ4udMqHn/pcDTe3MCpRzhXuaHsiEY0WagvawE3 6sFUr65olvygBn1EviDDjzu2ZeRXnO5yaRCFMPSK28cNKtXI98LmV3MIMI9TP88nfFh/ XW4EfJiTuuitktZ0ht1gnp0bov5b6nvkV4dZjDpd0qDEDHO6ULEAIx1oLV3oWPHRAQHH dhZw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAVxINgwbMRo1UpuIsy+efSZQQ/I/Lv2NbbAmFxAaLwkKFx/Wpaa yc+YkkMBeEZgJw6yOxDfT2N/vtbeUMBfV6YMv5Jzm0E/7b5mXrYWO4wX3/V857coiysNX7IstUx r/18Rx1czEJTwNtXFr3E25QZACWTmmT/098vApMOoO+A4YT19ovOvdHkEVm74LMs= X-Received: by 2002:a17:906:7013:: with SMTP id n19mr11819621ejj.65.1565245219762; Wed, 07 Aug 2019 23:20:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqyimpdztY7bSTArR+mzeIO2+bItD68qfYW9osJpimOHZO66DtjELgjjOPfZGax/yJxtqfFb X-Received: by 2002:a17:906:7013:: with SMTP id n19mr11819567ejj.65.1565245218928; Wed, 07 Aug 2019 23:20:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245218; cv=none; d=google.com; s=arc-20160816; b=KhdXEbC5Bvss21HqJYU3dSocSqza05KKEzjka1qyF0ImQR9RXeEtn26D7IrGfoobqy RTAUstmrBc4ONIxjrwqJBsnzkl0nRUTY9AFce5X/ZNICk0gXNu+IwCVrkVBzuWZbpBOA 5vp1Py2F/DxgaujDm5JPT2Hm4QIAa6jUk1VpOp5i5CB84DpNhVPPPJd0GtsA4b+txXUb W75x3RoFVM93tatPiTKbsDwf8HxkqDfHdKHRBlF7Z5ReTGSOG+S9v3TEnC/dRBhzXAFW PH2MZhznB/3esbjoB88uVdsYZ1z3lp41ZctokYGV1lTZMtIER9qHT7ITYwkKuhtpk/vL x16A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=LklZI+jjNy9P/Vf1oDAlKWfBdOZ3w4ODTtcULXIYH+w=; b=YXe1vm2Y3pnOFWti8cKPUxCHOQ8ymJvNp0Do8QRqa/aI54Gw+5sVfn4ULPxY/uBcRf kzol3pXD09nyhbUUg+VSp6qe6xrZN9CgDItRVREm6LRDppz7EuDFQWVWNok2ZdqmUoM6 diIuAsDHGCRIRHRy/jQLj58ZQnWfyXLS3u/BMo345vY0sGkmk7aC8grdIMM831jjYEGA vuq1wDaVO6ostA711kUM0uaLPq/+hisGLJVqEi4lfuPPGPqsBNQmuzXsTe42uwp6e9Hw QVXsK9Xt21ca8gOo1jU4K3l4ue6q36XkKu4M0gCazJLNrhLNeV1kaHB2G0cVaWPLJdbe +mXw== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net. [217.70.183.200]) by mx.google.com with ESMTPS id g10si30715191ejj.256.2019.08.07.23.20.18 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:20:18 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.200; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.200 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay7-d.mail.gandi.net (Postfix) with ESMTPSA id 50D2420008; Thu, 8 Aug 2019 06:20:14 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 02/14] arm64: Make use of is_compat_task instead of hardcoding this test Date: Thu, 8 Aug 2019 02:17:44 -0400 Message-Id: <20190808061756.19712-3-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Each architecture has its own way to determine if a task is a compat task, by using is_compat_task in arch_mmap_rnd, it allows more genericity and then it prepares its moving to mm/. Signed-off-by: Alexandre Ghiti Acked-by: Catalin Marinas Acked-by: Kees Cook Reviewed-by: Christoph Hellwig Reviewed-by: Luis Chamberlain --- arch/arm64/mm/mmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index b050641b5139..bb0140afed66 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -43,7 +43,7 @@ unsigned long arch_mmap_rnd(void) unsigned long rnd; #ifdef CONFIG_COMPAT - if (test_thread_flag(TIF_32BIT)) + if (is_compat_task()) rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); else #endif From patchwork Thu Aug 8 06:17:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083227 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CA3CC14DB for ; Thu, 8 Aug 2019 06:21:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BAB9B28706 for ; Thu, 8 Aug 2019 06:21:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AE06E28726; Thu, 8 Aug 2019 06:21:29 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BF6CC28ABC for ; Thu, 8 Aug 2019 06:21:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FE896B000A; Thu, 8 Aug 2019 02:21:27 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0894A6B000C; Thu, 8 Aug 2019 02:21:27 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E6BAC6B000D; Thu, 8 Aug 2019 02:21:26 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by kanga.kvack.org (Postfix) with ESMTP id 97CE36B000A for ; Thu, 8 Aug 2019 02:21:26 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id y3so57549541edm.21 for ; Wed, 07 Aug 2019 23:21:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=21mhY1F6D/bVN4FBUSB5kLGwxQH8yFVmkExK3y72E74=; b=U79cHAGvgnjnvcQSQOn7WvqZ93ofeFRBCBr6TAt/Ptb9y7dBF5JoYo68q7RTR8SF3t Wg9DIQqTBJt8xisREQ3sReGYJ/wiS5j2Oy9O4Wpb0xvnuZJeBE9iVVjlgRCYq/gOiUP4 LpZrwu7I0ChB2DrE8yOsP3BY/f5qonMcDkSKgi84owe4SL3tAHZrCcMZdSx+qWRl6+8B pnTFH9jE3A6nIKZlJReCupvKgBOV1B1p5/woHZDLPrxYcTQJpcxOBdYn29SjQzI3slqW h1zQ3gbGc8jAwzySNYn4GJ4RLAsLSrKz8d1W9GlHeu5nSXhvlY9/JjrjMD0UiVvpTTkN ru8A== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.196 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUwh5YKgHv6grbazc6x2zHBTGykMRygVVasR9c1+0Oh4bnK3ovf 3WMK95sKt0fHt4bT6SmeaGz3Rf/pQ074c8MUhgjnMVyf5Xt8ZUivlOp7JGbILVvvZLdztHzvELd 0zfoENrutiTFibEmsuUz8WcNafZac6hMUDN7RJp9n2L2kM98X/7ZhNQTXumaP12w= X-Received: by 2002:a17:906:304d:: with SMTP id d13mr11404720ejd.99.1565245286214; Wed, 07 Aug 2019 23:21:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqylSeSOi03vFwKwaVl6DjpEgsqVcgkgm3y6lZ1tFobF5mYINLJuhsORAQYT5o/Z5OWlEKtG X-Received: by 2002:a17:906:304d:: with SMTP id d13mr11404689ejd.99.1565245285486; Wed, 07 Aug 2019 23:21:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245285; cv=none; d=google.com; s=arc-20160816; b=XwZl+pr1RKugAzque+KKFDKbtFNSk0HisV/v7DvLDfXMTTgoYNWZgS1c1h6wZlZFbT fbOlpYpT+m1Q0hj2QUMuLO0+dSGDh6Y4zC5gj1O/OCbhKKaJREmydHQlg0dx/xAJK+t6 6Hu/OQFHdOdzn68pSSu1Fk62gPE4eV9bjP7RXgmZ80bL9s0XxefHfa77EKPdQCgsDalC W+V9NzNTrcbNb16nFaOkTeRdJZkiU93dSW6R82iXFJDGZvTpoxA9E1zQexHb1d8CtIIJ CchbGR47QsU5XNgZRhYfPHpfqLoo3MXJJDvoPpHrzv03SstHtYxdBVAXIIeRQ2zFn/RT Fj7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=21mhY1F6D/bVN4FBUSB5kLGwxQH8yFVmkExK3y72E74=; b=ww6UImYPceBLRK9Phg/OfLBytuTOEIJYvXS+CDAWUqhuzAp1DUOGimr/g004uCGA83 U3SoPn6eiGikb7ahZrRJXCvPyB7NMvOI3f6znpY6OeYsWknLXalYbUa9rZkfRkVT9WT/ t+h24V6kd77QNUCSq7CB5e4eBzUkcKgut+Ft5aUQYXKwcBcqhYRY/Q8xBavfLsBoeO5u ikb+Rt0wjEps1cK9PqZLLntaZMHXtp2sarTAkRRPt+ErEEMgyFDIoCjUASVsv3Yc7nMU /TMuhkpNE3euv6joZARZsZLKwnjktiDSQRg3qfVGrAwqloZeF3mLjmqlD5Yzj/WeCfRH 0m1A== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.196 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net. [217.70.183.196]) by mx.google.com with ESMTPS id nq5si30507286ejb.124.2019.08.07.23.21.25 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:21:25 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.196 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.196; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.196 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 1AE39E0004; Thu, 8 Aug 2019 06:21:18 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 03/14] arm64: Consider stack randomization for mmap base only when necessary Date: Thu, 8 Aug 2019 02:17:45 -0400 Message-Id: <20190808061756.19712-4-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Do not offset mmap base address because of stack randomization if current task does not want randomization. Note that x86 already implements this behaviour. Signed-off-by: Alexandre Ghiti Acked-by: Catalin Marinas Acked-by: Kees Cook Reviewed-by: Christoph Hellwig Reviewed-by: Luis Chamberlain --- arch/arm64/mm/mmap.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index bb0140afed66..e4acaead67de 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -54,7 +54,11 @@ unsigned long arch_mmap_rnd(void) static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) { unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = (STACK_RND_MASK << PAGE_SHIFT) + stack_guard_gap; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); /* Values close to RLIM_INFINITY can overflow. */ if (gap + pad > gap) From patchwork Thu Aug 8 06:17:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083237 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A34C713AC for ; Thu, 8 Aug 2019 06:22:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 92BA528726 for ; Thu, 8 Aug 2019 06:22:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 85DA928A3D; Thu, 8 Aug 2019 06:22: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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A70EF28797 for ; Thu, 8 Aug 2019 06:22:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C97116B000A; Thu, 8 Aug 2019 02:22:32 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id C22CC6B000D; Thu, 8 Aug 2019 02:22:32 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AC2526B000E; Thu, 8 Aug 2019 02:22:32 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by kanga.kvack.org (Postfix) with ESMTP id 576A96B000A for ; Thu, 8 Aug 2019 02:22:32 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id f19so57584565edv.16 for ; Wed, 07 Aug 2019 23:22:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Szq2k/KYa8HGh/lZff135xvtiKuOQoIRwWhZZ6kmXYk=; b=lSXxGC/iJ+pWz3MeJZD5CgUIbpngMr2JbCbR3MKnuVFosCdqM9/gjbg+pR/k2vmswL lM4DzDUknCzbqlfh+HhzNh7sZYINWknExixwqs/+uE+S2Hov2/DMNWYG9wKCK12cPcdb 9GiW+QbEfutK0pFhuLg1VfEaQ43zacdgm9cNFo3cHkqM3gjbLyR2aIR7tz6QcCxM90yF o0zYtBBZ//cqBy2DfQYDhchHeJvQznHPAZ2AtEAhBQOtIewIiIzX1xBlcxpJDY8xbsym K/8Be5JqqOq2wWO1E8ad/AWsViA27BkwC2FMWGlBGBv4/Ie3Sp/Nuy1a88H7wm1x8/W9 Gc/w== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAV6cSrfL+mpQK277QLrI0tJ8i/lp+KsCom6+F/0z8W3VLKNIj79 kzSSnIiQTCCKencu7uA/9iyTdNIrhZo4hKPpMJW2vy2PNiXeBHrum13EBYCKxaQpi85fxU0WGVt XlG9FFclnEw8Uy1/bowdudczpCC98d7nRuIrC3ddZLKeIn3ToSbFQCZEMIlLbmNg= X-Received: by 2002:aa7:d909:: with SMTP id a9mr14007485edr.261.1565245351902; Wed, 07 Aug 2019 23:22:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqwJk88my6eIkofjzwnUbeYpd4ZK9dxeraeXQsKiWoJAWXbyd9FVIkbjfLX8rsXe1YZpVZ9N X-Received: by 2002:aa7:d909:: with SMTP id a9mr14007412edr.261.1565245350753; Wed, 07 Aug 2019 23:22:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245350; cv=none; d=google.com; s=arc-20160816; b=AznDSzZq8Xi4F4+SKMdxzHopRO9ixTGIjoF8ghNpaK/YqJMXwByaur25j1joBv9Y3z F7AbTMdusjsktrT7tRPZ433lm6GCTbbkJ+uA7R4N2dYu+0ff+mJ5tDswBQsIAVFAdPS1 LBLyRzae4wYBITc+GeSu+Vtk4bU9ZS9C2JFYC+TOvP24GOdvyXPLHNDaXxTZmo6QHOMR /b5GLp2tyQQoiEJXDxcM0+Ll2rnyCAMu84QI6r1EgCfeTKZ5aw+iasxLkYUWWHAuSbC9 qB3TjIvm8zj3422KnsffoKaCQQMWNDt9ddzzhr1AamkJyuw7++8EoI0oGEQGznmdsJzZ BIhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=Szq2k/KYa8HGh/lZff135xvtiKuOQoIRwWhZZ6kmXYk=; b=NAcc8StuYEUHfyi73FuX6A92L8sryVbcrSFP2WKts9tcikrjNmMpKXk0OtDe/rQs7B r7NlAwImVjjPKxrgKHo2Y4JTO7fwLpTVE/aOKlnPwKhapN3llHd/VwtWKI4+PXOAK/ZT R00kTzOJKxmDm9X5CaXPEQpKOkDcNFfMQZkeS/lUOKkZvScd7FNgdXnNdzrRlnM8lYur ztlpxPVYtY9zeEJRjn2jy0BRPN6ZiNQQ9GHEP5fHgzr0/1sBYKjPjdXN/JpbbbrTxk95 acl7Dx6NWmodLSKMKFF179bsS3LsOA5srVXGtJC/GiX87irQSndrY33GV18AYJL5tIW6 EiNQ== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id i3si33668736eda.107.2019.08.07.23.22.30 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:22:30 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 96B1260002; Thu, 8 Aug 2019 06:22:25 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti , Christoph Hellwig Subject: [PATCH v6 04/14] arm64, mm: Move generic mmap layout functions to mm Date: Thu, 8 Aug 2019 02:17:46 -0400 Message-Id: <20190808061756.19712-5-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP arm64 handles top-down mmap layout in a way that can be easily reused by other architectures, so make it available in mm. It then introduces a new config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT that can be set by other architectures to benefit from those functions. Note that this new config depends on MMU being enabled, if selected without MMU support, a warning will be thrown. Suggested-by: Christoph Hellwig Signed-off-by: Alexandre Ghiti Acked-by: Catalin Marinas Acked-by: Kees Cook Reviewed-by: Christoph Hellwig Reviewed-by: Luis Chamberlain --- arch/Kconfig | 10 ++++ arch/arm64/Kconfig | 1 + arch/arm64/include/asm/processor.h | 2 - arch/arm64/mm/mmap.c | 76 ----------------------------- kernel/sysctl.c | 6 ++- mm/util.c | 78 +++++++++++++++++++++++++++++- 6 files changed, 92 insertions(+), 81 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index a7b57dd42c26..a0bb6fa4d381 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -696,6 +696,16 @@ config HAVE_ARCH_COMPAT_MMAP_BASES and vice-versa 32-bit applications to call 64-bit mmap(). Required for applications doing different bitness syscalls. +# This allows to use a set of generic functions to determine mmap base +# address by giving priority to top-down scheme only if the process +# is not in legacy mode (compat task, unlimited stack size or +# sysctl_legacy_va_layout). +# Architecture that selects this option can provide its own version of: +# - STACK_RND_MASK +config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT + bool + depends on MMU + config HAVE_COPY_THREAD_TLS bool help diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 3adcec05b1f6..14a194e63458 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -72,6 +72,7 @@ config ARM64 select ARCH_SUPPORTS_INT128 if GCC_VERSION >= 50000 || CC_IS_CLANG select ARCH_SUPPORTS_NUMA_BALANCING select ARCH_WANT_COMPAT_IPC_PARSE_VERSION if COMPAT + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT select ARCH_WANT_FRAME_POINTERS select ARCH_WANT_HUGE_PMD_SHARE if ARM64_4K_PAGES || (ARM64_16K_PAGES && !ARM64_VA_BITS_36) select ARCH_HAS_UBSAN_SANITIZE_ALL diff --git a/arch/arm64/include/asm/processor.h b/arch/arm64/include/asm/processor.h index 844e2964b0f5..65e2de00913f 100644 --- a/arch/arm64/include/asm/processor.h +++ b/arch/arm64/include/asm/processor.h @@ -281,8 +281,6 @@ static inline void spin_lock_prefetch(const void *ptr) "nop") : : "p" (ptr)); } -#define HAVE_ARCH_PICK_MMAP_LAYOUT - #endif extern unsigned long __ro_after_init signal_minsigstksz; /* sigframe size */ diff --git a/arch/arm64/mm/mmap.c b/arch/arm64/mm/mmap.c index e4acaead67de..3028bacbc4e9 100644 --- a/arch/arm64/mm/mmap.c +++ b/arch/arm64/mm/mmap.c @@ -20,82 +20,6 @@ #include -/* - * Leave enough space between the mmap area and the stack to honour ulimit in - * the face of randomisation. - */ -#define MIN_GAP (SZ_128M) -#define MAX_GAP (STACK_TOP/6*5) - -static int mmap_is_legacy(struct rlimit *rlim_stack) -{ - if (current->personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - -#ifdef CONFIG_COMPAT - if (is_compat_task()) - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); - else -#endif - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - return rnd << PAGE_SHIFT; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = stack_guard_gap; - - /* Account for stack randomization if necessary */ - if (current->flags & PF_RANDOMIZE) - pad += (STACK_RND_MASK << PAGE_SHIFT); - - /* Values close to RLIM_INFINITY can overflow. */ - if (gap + pad > gap) - gap += pad; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(STACK_TOP - gap - rnd); -} - -/* - * This function, called very early during the creation of a new process VM - * image, sets up which VM layout function to use: - */ -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - /* - * Fall back to the standard layout if the personality bit is set, or - * if the expected stack growth is unlimited: - */ - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - /* * You really shouldn't be using read() or write() on /dev/mem. This might go * away in the future. diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 078950d9605b..00fcea236eba 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -264,7 +264,8 @@ extern struct ctl_table epoll_table[]; extern struct ctl_table firmware_config_table[]; #endif -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) int sysctl_legacy_va_layout; #endif @@ -1573,7 +1574,8 @@ static struct ctl_table vm_table[] = { .proc_handler = proc_dointvec, .extra1 = SYSCTL_ZERO, }, -#ifdef HAVE_ARCH_PICK_MMAP_LAYOUT +#if defined(HAVE_ARCH_PICK_MMAP_LAYOUT) || \ + defined(CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT) { .procname = "legacy_va_layout", .data = &sysctl_legacy_va_layout, diff --git a/mm/util.c b/mm/util.c index 15a4fb0f5473..0781e5575cb3 100644 --- a/mm/util.c +++ b/mm/util.c @@ -17,7 +17,12 @@ #include #include #include +#include +#include #include +#include +#include +#include #include @@ -315,7 +320,78 @@ unsigned long randomize_stack_top(unsigned long stack_top) #endif } -#if defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) +#ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT +#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE +unsigned long arch_mmap_rnd(void) +{ + unsigned long rnd; + +#ifdef CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS + if (is_compat_task()) + rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); + else +#endif /* CONFIG_HAVE_ARCH_MMAP_RND_COMPAT_BITS */ + rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); + + return rnd << PAGE_SHIFT; +} +#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */ + +static int mmap_is_legacy(struct rlimit *rlim_stack) +{ + if (current->personality & ADDR_COMPAT_LAYOUT) + return 1; + + if (rlim_stack->rlim_cur == RLIM_INFINITY) + return 1; + + return sysctl_legacy_va_layout; +} + +/* + * Leave enough space between the mmap area and the stack to honour ulimit in + * the face of randomisation. + */ +#define MIN_GAP (SZ_128M) +#define MAX_GAP (STACK_TOP / 6 * 5) + +static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) +{ + unsigned long gap = rlim_stack->rlim_cur; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); + + /* Values close to RLIM_INFINITY can overflow. */ + if (gap + pad > gap) + gap += pad; + + if (gap < MIN_GAP) + gap = MIN_GAP; + else if (gap > MAX_GAP) + gap = MAX_GAP; + + return PAGE_ALIGN(STACK_TOP - gap - rnd); +} + +void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) +{ + unsigned long random_factor = 0UL; + + if (current->flags & PF_RANDOMIZE) + random_factor = arch_mmap_rnd(); + + if (mmap_is_legacy(rlim_stack)) { + mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; + mm->get_unmapped_area = arch_get_unmapped_area; + } else { + mm->mmap_base = mmap_base(random_factor, rlim_stack); + mm->get_unmapped_area = arch_get_unmapped_area_topdown; + } +} +#elif defined(CONFIG_MMU) && !defined(HAVE_ARCH_PICK_MMAP_LAYOUT) void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) { mm->mmap_base = TASK_UNMAPPED_BASE; From patchwork Thu Aug 8 06:17:47 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083247 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 01A0D14DB for ; Thu, 8 Aug 2019 06:23:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E5C702887B for ; Thu, 8 Aug 2019 06:23:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D8EE728A3D; Thu, 8 Aug 2019 06:23:38 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2BF0E28ABC for ; Thu, 8 Aug 2019 06:23:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3466B6B000A; Thu, 8 Aug 2019 02:23:37 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2D14C6B000C; Thu, 8 Aug 2019 02:23:37 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C05B6B000D; Thu, 8 Aug 2019 02:23:37 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id BEACE6B000A for ; Thu, 8 Aug 2019 02:23:36 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id k37so847399eda.7 for ; Wed, 07 Aug 2019 23:23:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=Uc9HCc/6hNvOV8B2vZv1Mz7T1SIEtuaecktNCSj6hGE=; b=DgcxQW8McC/TKfNQ2wPK8bbjRq5JFNjMK/yIgtCfpSE01MfVVixHFB2UbBuzEquaTI SohRhjnBk6kDeHINGBP3Wun7QxVskUN3mRCFcZy3YS057JZW8mHQjdbDZX/xm2l8fGUY ziHGAYPW/pYhfHC7zbMojpsqYZ9U7QZfD+QrsNS8v+su1Hv5TkP1QcEeQD+X7gAYaTBP PB0sXRPCsH71kCCdySSRtsV0OMKqbqux3hpuk5S1NWeiXpQUfAWShHpGshaX4tEqlMWK auQ7evjPsWhPtoHhCexue55pm0nkqP1iH9UyOahJkhkIve6wVBGJlNtjr6Ek/h4e1/oG u2+g== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUtNFF78/MVJuqu9knWyTLRCkH3JHzfFX7ltPCP9dTM4IUweMZu 1PYWxjHAoXyKJUHzdDb1Kuq/ToIWa/iU+9XhUKsKY1eLCfMX+Jo/vuyJf18BaIgclZWkSrfEXYv 31WD1vgK3j4EZrALOcUtjLZRWHAq4whOtlDuQcbFuCXYnk6oAo18hEc87UlD6mSE= X-Received: by 2002:a05:6402:712:: with SMTP id w18mr4932280edx.201.1565245416239; Wed, 07 Aug 2019 23:23:36 -0700 (PDT) X-Google-Smtp-Source: APXvYqz2AAvYy9fKM7jvQGyjwGxdMEEEo0CWTPhKvY31pFbVtO0YB3Lj/HTpTIetYhQdWBi9tXJv X-Received: by 2002:a05:6402:712:: with SMTP id w18mr4932243edx.201.1565245415454; Wed, 07 Aug 2019 23:23:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245415; cv=none; d=google.com; s=arc-20160816; b=ANkZ1thjl+is+O+eHfiPZDjnMfueGI8KPmLM8Wfj1yp8Gw9A766pPSJ1fzZLSN9Ekj 7W9SOETtQ4Z2jS9WrX9PUbA1BCUsN0AHvcx0fxZGHneefezHefoeepTzIht62fabZ0a6 Z36yeLqwEKrT6bV5ZuV2+kxbJ76GsmEFXRExXxuJQNE8S+LpsMasXIJ9ydH23T/f3Xsu nfZ9dDWpWHoeo56HJ2vvZxMY5F3v1OapA/SQo4Co1DVPhctRSJ0o+wlGG8O8GiPWwjFa i2JDQZU7IqASMp3j3hKi7aqL7d3mBphBwqDjqOpWVF6QQjmp2HsnaSiBWSCZeAZdB+Kx Yd2A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=Uc9HCc/6hNvOV8B2vZv1Mz7T1SIEtuaecktNCSj6hGE=; b=Ru4vWwwdqQL4CDkEV1nydSPu9MRw1b8Io0IRt4uq1nbUUrAdNUj4IfEhqEcoD/OQ2C Z2gsq5sezwsGzvUX7BX+m4Zx3b4z1ZmPssniD/9yjQZlvsoc7eCFsQk46vFfv/Lwpgcb rgtTryqzlkeSBoz8SSK9GAa/SeEw4HtqQQn7EkVBi5e1pAtZIUCrJTdnPbmreRqBIx+y KcOOe4Yk8hxqbBjpHk5GgISy+p/7mqMVYUV1uvinD55fD0tqDJnJN7fEbwz8MTm1/g5H QeGA9UNadpfDmO5ekpPl05cXdu/UEjie0dGzGo3QEbmzCDrqO5e7K6vQhI/8JQnc8gYL GDrA== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id b17si638481edb.341.2019.08.07.23.23.35 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:23:35 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id D423E60002; Thu, 8 Aug 2019 06:23:30 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 05/14] arm64, mm: Make randomization selected by generic topdown mmap layout Date: Thu, 8 Aug 2019 02:17:47 -0400 Message-Id: <20190808061756.19712-6-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP This commits selects ARCH_HAS_ELF_RANDOMIZE when an arch uses the generic topdown mmap layout functions so that this security feature is on by default. Note that this commit also removes the possibility for arm64 to have elf randomization and no MMU: without MMU, the security added by randomization is worth nothing. Signed-off-by: Alexandre Ghiti Acked-by: Catalin Marinas Acked-by: Kees Cook Reviewed-by: Christoph Hellwig Reviewed-by: Luis Chamberlain --- arch/Kconfig | 1 + arch/arm64/Kconfig | 1 - arch/arm64/kernel/process.c | 8 -------- mm/util.c | 11 +++++++++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/arch/Kconfig b/arch/Kconfig index a0bb6fa4d381..d4c1f0551dfe 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -705,6 +705,7 @@ config HAVE_ARCH_COMPAT_MMAP_BASES config ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT bool depends on MMU + select ARCH_HAS_ELF_RANDOMIZE config HAVE_COPY_THREAD_TLS bool diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 14a194e63458..399f595ef852 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -16,7 +16,6 @@ config ARM64 select ARCH_HAS_DMA_MMAP_PGPROT select ARCH_HAS_DMA_PREP_COHERENT select ARCH_HAS_ACPI_TABLE_UPGRADE if ACPI - select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FAST_MULTIPLIER select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_GCOV_PROFILE_ALL diff --git a/arch/arm64/kernel/process.c b/arch/arm64/kernel/process.c index f674f28df663..8ddc2471b054 100644 --- a/arch/arm64/kernel/process.c +++ b/arch/arm64/kernel/process.c @@ -548,14 +548,6 @@ unsigned long arch_align_stack(unsigned long sp) return sp & ~0xf; } -unsigned long arch_randomize_brk(struct mm_struct *mm) -{ - if (is_compat_task()) - return randomize_page(mm->brk, SZ_32M); - else - return randomize_page(mm->brk, SZ_1G); -} - /* * Called from setup_new_exec() after (COMPAT_)SET_PERSONALITY. */ diff --git a/mm/util.c b/mm/util.c index 0781e5575cb3..16f1e56e2996 100644 --- a/mm/util.c +++ b/mm/util.c @@ -321,7 +321,15 @@ unsigned long randomize_stack_top(unsigned long stack_top) } #ifdef CONFIG_ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT -#ifdef CONFIG_ARCH_HAS_ELF_RANDOMIZE +unsigned long arch_randomize_brk(struct mm_struct *mm) +{ + /* Is the current task 32bit ? */ + if (!IS_ENABLED(CONFIG_64BIT) || is_compat_task()) + return randomize_page(mm->brk, SZ_32M); + + return randomize_page(mm->brk, SZ_1G); +} + unsigned long arch_mmap_rnd(void) { unsigned long rnd; @@ -335,7 +343,6 @@ unsigned long arch_mmap_rnd(void) return rnd << PAGE_SHIFT; } -#endif /* CONFIG_ARCH_HAS_ELF_RANDOMIZE */ static int mmap_is_legacy(struct rlimit *rlim_stack) { From patchwork Thu Aug 8 06:17:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083257 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A098C13AC for ; Thu, 8 Aug 2019 06:24:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 90C2B28A3D for ; Thu, 8 Aug 2019 06:24:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8494528B0F; Thu, 8 Aug 2019 06:24:43 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 22B9628A3D for ; Thu, 8 Aug 2019 06:24:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 653D76B0003; Thu, 8 Aug 2019 02:24:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6034C6B0006; Thu, 8 Aug 2019 02:24:42 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4CCBB6B000A; Thu, 8 Aug 2019 02:24:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id F22736B0003 for ; Thu, 8 Aug 2019 02:24:41 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id a15so912434edv.2 for ; Wed, 07 Aug 2019 23:24:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=wVOEOj3ZQH/LmpW3WBqdwFnDIfuDrBVXLtErTwdcowY=; b=plHpIg2f1vQMBgq0RhiT80zO7X5xLjZxA4DTvL8G351lEs89ISs4/6PrD19gQyAy7h M4Cq0WnOa9fgv1oRzlKJiCHcvPDLv7lhywluuIT6Trjz5+3xTojHm074lufRNQ/5mBoU +tj6W8zYOkQzO15lgWS1/WbGnwZDxrAXW/P3Zjxt4B5u9HJIOtaShDo9qihVaj3z0uPE pvj/14JWIao7MawCZKtGSjfu3kVzCNmlUNHNGC+iFSrK0P+8VrYGImES5GYrsOCWB6Sz Yf1QAp4/b94JLBXMOq3VAOTO1qBRuHK5BJASuawmc3vR8lxEozl9AufYt6lkNC5IJ/3e 6HeA== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUT3SWSt7/1vZjAe73toa3yMevzAJVMCemekPufDVlDR9qMsL9V Qk2b8juxdtrK9egNns7KX0RYLKY/kNtchlFI0Jjuo4c8xIHR/1ZKW4z7nA+5Af1RBiJc/4tqQGK mp8szdEIE8PZa/vlmnzTqXLXvshB76UFcRHE7iMXvLytJ3PaIGZqC/wmwwgts0E4= X-Received: by 2002:a50:9177:: with SMTP id f52mr14128767eda.294.1565245481568; Wed, 07 Aug 2019 23:24:41 -0700 (PDT) X-Google-Smtp-Source: APXvYqyTlQRD15ioRPIBc2difhmhlSWiy4bcgLndUk2DZVaZ19qgLOMhEDvEwX40A/Mc+huXHjbW X-Received: by 2002:a50:9177:: with SMTP id f52mr14128692eda.294.1565245480143; Wed, 07 Aug 2019 23:24:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245480; cv=none; d=google.com; s=arc-20160816; b=K0kdHbHMcDZeajGwuh5CwUpIo6AGVNFvQKEPcnVjfEXvbIstcEkGBJ3NMJkAg4SsEu llHcvS9/dR636QozKL0tSq58CXvt5ufzrFx12AqqXGQBa11sYJcgi4I/G7wZ19iNsklm auIlWFRZgNTfHkwAPWYfnrbrWEfyXVHTDqJLGUAhL5lwSmYj2AYFmviz9A9Xjd1uH2NP KB2M6pVp0l4C1a1mG2RYln5okyW+HuJHWIAEq3PgzmXq8LwFssuc8ziTOWo/nYQ5G90m 8Xotnz0lpRHtkAok15gw2bhkmWsBAvgPm7gpKXZLqYojkDD2Lzw/TDIhZnfRzPyY8HHi 96Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=wVOEOj3ZQH/LmpW3WBqdwFnDIfuDrBVXLtErTwdcowY=; b=pmNXPR2qjN3aWe+gglbEVrbzMWY8LMXxoFCJM9DkISLPfIjWP4otKVG7LmIu+EEmc4 f2dxdzyTqbZkXXEyjDcCk8xCpT/l1rQD6xYfxt9mTSFkMS7h5P99mS6ZXPILL9fsJAnQ NVtMgPTzRnUzJ5UIKWyGbGMly4YRsWwf25A5BCp0Nfm7dXfxGkzJR5Ytgy4h1i1h7dxq cklGEfQLLtD8dWpqoaPAovFcHGfM/DbRN6ogol/AyNGIHmwq1AeUNosdy8L+F9fcewKx ZYEkCd6LwFlrR5Q217t+70nXE1d3C9KCy1G1XSLUeC049J3SNCMoSlgvv3jEly/paTh4 Pr6A== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id d23si30292430ejb.149.2019.08.07.23.24.39 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:24:40 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 94D2C6000B; Thu, 8 Aug 2019 06:24:35 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 06/14] arm: Properly account for stack randomization and stack guard gap Date: Thu, 8 Aug 2019 02:17:48 -0400 Message-Id: <20190808061756.19712-7-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP This commit takes care of stack randomization and stack guard gap when computing mmap base address and checks if the task asked for randomization. This fixes the problem uncovered and not fixed for arm here: https://lkml.kernel.org/r/20170622200033.25714-1-riel@redhat.com Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Reviewed-by: Luis Chamberlain --- arch/arm/mm/mmap.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index f866870db749..bff3d00bda5b 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -18,8 +18,9 @@ (((pgoff)<> (PAGE_SHIFT - 12)) static int mmap_is_legacy(struct rlimit *rlim_stack) { @@ -35,6 +36,15 @@ static int mmap_is_legacy(struct rlimit *rlim_stack) static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) { unsigned long gap = rlim_stack->rlim_cur; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); + + /* Values close to RLIM_INFINITY can overflow. */ + if (gap + pad > gap) + gap += pad; if (gap < MIN_GAP) gap = MIN_GAP; From patchwork Thu Aug 8 06:17:49 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083269 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2375613AC for ; Thu, 8 Aug 2019 06:25:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 16386288C3 for ; Thu, 8 Aug 2019 06:25:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0A8F62891F; Thu, 8 Aug 2019 06:25:49 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A678928958 for ; Thu, 8 Aug 2019 06:25:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C1F196B0006; Thu, 8 Aug 2019 02:25:47 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id BCEF06B000A; Thu, 8 Aug 2019 02:25:47 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A98406B000C; Thu, 8 Aug 2019 02:25:47 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id 5D0B66B0006 for ; Thu, 8 Aug 2019 02:25:47 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id z20so57620887edr.15 for ; Wed, 07 Aug 2019 23:25:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=WYZFBi/mDjw2AHO/7J+UsrC2CAAthlR5mqmiph32kXg=; b=sBww0825ToerOtv5Q47enaM8tT4Q9VdvOoMxgUT2jgsWlWteXCOGUbF66RNtcNcduX zxQnj6tEuvsOn7bq+m1G0F48SGZ6phKLP1St0LJ8lwm/E95vkD+N9li2a0YkYapvaSer pSNK809VJbItb/RRvGzBO0qau4ag/UlEEAMeTTlkqSLgpEd4ay33otW7JTo1hkf6d9FT bn47iS03VnfMtvzR/gPnPgzQX+lA4OmJlU2MkTr8Bj0SrWPadWHckiCFR91k7yXHPQoX AvdWbM8wjOU+sWiCML9YVP8CVwEib3VqpoWSSwD1x45hcbvD+wvNMoBaW+r2dq04gF9V ZfXw== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAWgT1qPZUMkNguNKCqJgrwrYKf3oOAxfdM5FZgpnWrXYKdwDl8F BKrE+2qvzXayeIX4K8uwbtAHVkQ2cYvjBg+VYib+NJDRj4VQ1BzhOJVniKluKVPZR8MWAqFQyof Xoc6/kxG3B/6c0frGuSQqqiFGYeJUjpCyvVf0Nr3B08dDyIINv8uPi72OQ7ARALI= X-Received: by 2002:a17:906:eb8d:: with SMTP id mh13mr1410653ejb.98.1565245546962; Wed, 07 Aug 2019 23:25:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqw8O3pSi2r+Y9fWiqVF+T1OMv3pGRHRmG37zj3bKLcLf0duJwusD9/MiveOIA6KEmFXr+Mw X-Received: by 2002:a17:906:eb8d:: with SMTP id mh13mr1410631ejb.98.1565245546289; Wed, 07 Aug 2019 23:25:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245546; cv=none; d=google.com; s=arc-20160816; b=r5azZ9l6/trcvXUZk+kRe7fDPIuR8xQSiyRf3Juoma9eRWFAVdTEGuXRu9eThmZG9O Psnp2sO3IHCpgA4vkl2/CojTtx1mcu9K+wj83d9jLBNXNQ3zMGIsaivlzh0tQWQ/r9ha m2ehvOFSlA2P+l7G7LzQkWTh/dviuTArcbKdrdyJY85FgmQbtmW9YR4mpLImf1Hn8u+U m5AI9SLy14t5WgpBLlW/pcPwmViL/WggLd3ngENnLsPV9mvDJ2gwsbpqkKAsCBVJGTGe ksttMYLa69sVR60Ox55vReJGm1T237CeiCUKlhaIHIdeP6rpBIF3GpT56bigc8yavJKs DzLQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=WYZFBi/mDjw2AHO/7J+UsrC2CAAthlR5mqmiph32kXg=; b=QgNbX9qPSiqJu9pnbJLxVOfXioxPBSDSbAcheIdsZm9IG7P9cgM5pcI88iUYIzkU6L uJCB+SIYOnO/b2tveF9d71MVf/EtaWaVEQSrYhuVbkhhnkOxJP6R3gWC13OqKU0KyxJf ftqn2gF7jrcqIqUahX1u8aqJ+Gu7ScmDJVBoHkgJ6oxDDaYmr3zb+fxykCoYNTu+lYLi 25drCAbskSyV+QblZEajWAYsD4c8sGVAizw9U7kRSyxlbkpOiM3Iva1xgqA7HFhYboqo 5pSEQl2cPwRCj+UPUNNQehLY3kiLB5rSk9WAcbRtVBxtXgf8RjgnCec48HEy1rMJ3m3/ fMng== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay11.mail.gandi.net (relay11.mail.gandi.net. [217.70.178.231]) by mx.google.com with ESMTPS id oq6si30989417ejb.160.2019.08.07.23.25.46 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:25:46 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.231; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay11.mail.gandi.net (Postfix) with ESMTPSA id 4A8C8100003; Thu, 8 Aug 2019 06:25:40 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 07/14] arm: Use STACK_TOP when computing mmap base address Date: Thu, 8 Aug 2019 02:17:49 -0400 Message-Id: <20190808061756.19712-8-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP mmap base address must be computed wrt stack top address, using TASK_SIZE is wrong since STACK_TOP and TASK_SIZE are not equivalent. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Reviewed-by: Luis Chamberlain --- arch/arm/mm/mmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index bff3d00bda5b..0b94b674aa91 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -19,7 +19,7 @@ /* gap between mmap and stack */ #define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((TASK_SIZE)/6*5) +#define MAX_GAP ((STACK_TOP)/6*5) #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) static int mmap_is_legacy(struct rlimit *rlim_stack) @@ -51,7 +51,7 @@ static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) else if (gap > MAX_GAP) gap = MAX_GAP; - return PAGE_ALIGN(TASK_SIZE - gap - rnd); + return PAGE_ALIGN(STACK_TOP - gap - rnd); } /* From patchwork Thu Aug 8 06:17:50 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083277 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6250613AC for ; Thu, 8 Aug 2019 06:26:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4FE9B28726 for ; Thu, 8 Aug 2019 06:26:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4188928A41; Thu, 8 Aug 2019 06:26:55 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B022A28726 for ; Thu, 8 Aug 2019 06:26:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D56DC6B0006; Thu, 8 Aug 2019 02:26:53 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D070E6B000A; Thu, 8 Aug 2019 02:26:53 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BCFEB6B000C; Thu, 8 Aug 2019 02:26:53 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id 6DABA6B0006 for ; Thu, 8 Aug 2019 02:26:53 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id k37so851455eda.7 for ; Wed, 07 Aug 2019 23:26:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=F6AwJVNQYUFCq6QO/Xu8IsZAVcgcHmfXp23XkgiBWO0=; b=dMAXYzhx6o/VBxmZVBgtaYj69qIXmqLaQf8SLwdhHOjxmL+HcuRdTmjw1xJR+5JmX+ mXNaZzS3IcMwVrk79NsCeds7tC43NaRP9CH6aaxJKzrAlu8v79SAWSvkUCI9UUwmlnYP VW54mMnwunkXjjf9tbWVUEpZjIn7LoBtF1FtOUaMsetIgU5RJIXelhecyliZGbbj1djP wETP1uW26VGoKExy1G+cAiVgcllOqHWKV9RK9Uk2hwmbCFnVctJcn2z2RarxxMTeZ4lx 0tI5iSMgBcCSelPvsEPhN/Y/D5BbX2RWNw1DHAjvautA2u8Wa6rWdC7bSaBRRISvyRxp v3Ig== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.230 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUg/qQQ24FDyNuwsEm7/V0mBmNs9LcHt7H/YAhHd9YFxcQykp/U 7LZnUdjybZeiUuQdAl4rVsmpFPjFGUueD6tfLAF5c0u9L/Fb2epQArVORIecypqO/5PRoR5GtgT hIkBwn5BQvHUVCXXQARp/S8kPXLem9owc5sLfyboytsE2crWq1YxWhRyhiG7MRXs= X-Received: by 2002:a05:6402:782:: with SMTP id d2mr14223768edy.80.1565245613013; Wed, 07 Aug 2019 23:26:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqwVkgbHNu4upeptNY9wlznyGJ6hiiwJs8ALJoibKRmc1dm0rdTdviFkBo+MQtSDvTwUYceE X-Received: by 2002:a05:6402:782:: with SMTP id d2mr14223701edy.80.1565245611739; Wed, 07 Aug 2019 23:26:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245611; cv=none; d=google.com; s=arc-20160816; b=V0oE0YpbaDoLtbtmxtVyrwCD2HygzgeOKSwlPRsLAhAPN2Qoyh+sCJrSc17HuxQF/e oauGKd+1/Xsn3LpMlpB4POJeUgkIj+aXbqHGkqfcH5ClgNdTDfHaBvDxhqfUbqJSdiz4 AvS03mEukHFIFE3yM3+p/CBRbCcLJr8n9PKeZA4Ih/TnhHth6BBn5uwatdsRzYhgF4tO BlFwBW3uxoOxfbD0tAzF+YULlzmPeRJe05alFgFQIuzCiQIFPW37zJ6ebGRHmt316+7D ZFTwYbnimprx1XBuQ5fM4k6Xc9RFVY9S93n8ZCWwmKQuhenkBY4dgPoASyp+wGbx32RY u6hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=F6AwJVNQYUFCq6QO/Xu8IsZAVcgcHmfXp23XkgiBWO0=; b=ZXqvu2rSL3MmTgAT0b1AzDNHwr4tARjGIPyO+d5CfNGY/g23dFDUHHyFRguSOndgiV HJnhy1CRtttom6csRyWB81N0AAlgHKQePGxWexfXHEgL8dvPxotZveNey6BIBd8oTeax m8yRZSWWHxc8jtWCkPzfWN3hM9BuFTTd2VNw3b/6m82VOM9ulqyNUKDF9gzMKXK+9225 kLnFrcAMAUnYdMh8GJUTgJemzXV6GbHBU6EI1+DPFkxqfxhNfDhOInQLvxpkz6KQSkcn IyIBOy2wcORQ+93Uy2G0eCUsYzKGhWNJ1alKQtAPpqPYDT1CPyTMWGpeYrzuO5/aqk3I jyxw== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.230 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay10.mail.gandi.net (relay10.mail.gandi.net. [217.70.178.230]) by mx.google.com with ESMTPS id p6si10031930ejg.75.2019.08.07.23.26.51 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:26:51 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.230 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.230; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.230 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay10.mail.gandi.net (Postfix) with ESMTPSA id A9D96240006; Thu, 8 Aug 2019 06:26:45 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 08/14] arm: Use generic mmap top-down layout and brk randomization Date: Thu, 8 Aug 2019 02:17:50 -0400 Message-Id: <20190808061756.19712-9-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP arm uses a top-down mmap layout by default that exactly fits the generic functions, so get rid of arch specific code and use the generic version by selecting ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT. As ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT selects ARCH_HAS_ELF_RANDOMIZE, use the generic version of arch_randomize_brk since it also fits. Note that this commit also removes the possibility for arm to have elf randomization and no MMU: without MMU, the security added by randomization is worth nothing. Note that it is safe to remove STACK_RND_MASK since it matches the default value. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Reviewed-by: Luis Chamberlain --- arch/arm/Kconfig | 2 +- arch/arm/include/asm/processor.h | 2 -- arch/arm/kernel/process.c | 5 --- arch/arm/mm/mmap.c | 62 -------------------------------- 4 files changed, 1 insertion(+), 70 deletions(-) diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig index 33b00579beff..81b08b027e4e 100644 --- a/arch/arm/Kconfig +++ b/arch/arm/Kconfig @@ -7,7 +7,6 @@ config ARM select ARCH_HAS_BINFMT_FLAT select ARCH_HAS_DEBUG_VIRTUAL if MMU select ARCH_HAS_DEVMEM_IS_ALLOWED - select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_FORTIFY_SOURCE select ARCH_HAS_KEEPINITRD select ARCH_HAS_KCOV @@ -30,6 +29,7 @@ config ARM select ARCH_SUPPORTS_ATOMIC_RMW select ARCH_USE_BUILTIN_BSWAP select ARCH_USE_CMPXCHG_LOCKREF + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU select ARCH_WANT_IPC_PARSE_VERSION select BINFMT_FLAT_ARGVP_ENVP_ON_STACK select BUILDTIME_EXTABLE_SORT if MMU diff --git a/arch/arm/include/asm/processor.h b/arch/arm/include/asm/processor.h index 20c2f42454b8..614bf829e454 100644 --- a/arch/arm/include/asm/processor.h +++ b/arch/arm/include/asm/processor.h @@ -140,8 +140,6 @@ static inline void prefetchw(const void *ptr) #endif #endif -#define HAVE_ARCH_PICK_MMAP_LAYOUT - #endif #endif /* __ASM_ARM_PROCESSOR_H */ diff --git a/arch/arm/kernel/process.c b/arch/arm/kernel/process.c index f934a6739fc0..9485acc520a4 100644 --- a/arch/arm/kernel/process.c +++ b/arch/arm/kernel/process.c @@ -319,11 +319,6 @@ unsigned long get_wchan(struct task_struct *p) return 0; } -unsigned long arch_randomize_brk(struct mm_struct *mm) -{ - return randomize_page(mm->brk, 0x02000000); -} - #ifdef CONFIG_MMU #ifdef CONFIG_KUSER_HELPERS /* diff --git a/arch/arm/mm/mmap.c b/arch/arm/mm/mmap.c index 0b94b674aa91..b8d912ac9e61 100644 --- a/arch/arm/mm/mmap.c +++ b/arch/arm/mm/mmap.c @@ -17,43 +17,6 @@ ((((addr)+SHMLBA-1)&~(SHMLBA-1)) + \ (((pgoff)<> (PAGE_SHIFT - 12)) - -static int mmap_is_legacy(struct rlimit *rlim_stack) -{ - if (current->personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = stack_guard_gap; - - /* Account for stack randomization if necessary */ - if (current->flags & PF_RANDOMIZE) - pad += (STACK_RND_MASK << PAGE_SHIFT); - - /* Values close to RLIM_INFINITY can overflow. */ - if (gap + pad > gap) - gap += pad; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(STACK_TOP - gap - rnd); -} - /* * We need to ensure that shared mappings are correctly aligned to * avoid aliasing issues with VIPT caches. We need to ensure that @@ -181,31 +144,6 @@ arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0, return addr; } -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - - return rnd << PAGE_SHIFT; -} - -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - /* * You really shouldn't be using read() or write() on /dev/mem. This * might go away in the future. From patchwork Thu Aug 8 06:17:51 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083289 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2FAA213AC for ; Thu, 8 Aug 2019 06:28:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 213BD28726 for ; Thu, 8 Aug 2019 06:28:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1522D28ABC; Thu, 8 Aug 2019 06:28:02 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D64CD28726 for ; Thu, 8 Aug 2019 06:27:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1BCC76B0006; Thu, 8 Aug 2019 02:27:58 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 16E566B000A; Thu, 8 Aug 2019 02:27:58 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 083FE6B000C; Thu, 8 Aug 2019 02:27:58 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) by kanga.kvack.org (Postfix) with ESMTP id B175F6B0006 for ; Thu, 8 Aug 2019 02:27:57 -0400 (EDT) Received: by mail-ed1-f69.google.com with SMTP id f3so57543475edx.10 for ; Wed, 07 Aug 2019 23:27:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=nIWYWxti2h+YKJsCHEKPojnLfBDVTb9i7wBNtAUzrEQ=; b=Xc3/nksMVlQfuHu33HMzCMaGq21znoXf/+4VDWXZDzP2mXiBmZQptm0lZ2jqxCawEw ETjjx5lftydQY6D1671ZpVmVZqkxgGORzqfRZVASIUcrXUoZWZh3kSHQ9mkXB3JkHwjk 6QioDiDqnJSinK8YhniG3CWyGgmAFJyEaKt9UOGi3gWhb1Z9FxKO4d4paHk+4B2DoyLb Bx++C5Lq/FHH3L/WgV+m/bFLPAOKM2il0/gpoE4CinlTR4xxf8siCjhDYKc0rp/QKcdr uzxg+4dfi3JRDEFalCd68qwuae53cNND2PjKNAnwoF34WyYkqYModqUuc+IT/4Y4SUxi GjyQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUZ/ZNwnxWG39rxV/qfdHgITkh3CKPHfRNppV7gcyNqOknzDvCM OY7duQQ8HrY4ZkAWR6AFtbzUACpCYoSCQDlLYciw7y0ju82qJc1TL98tWGEp422rZOkL4K01wuV 7fDdEMTyhPvj6sr2SIDMotLZmCLh50IyrGbxZBsIUvvuFq42Lhnsb2QKRuanNaDg= X-Received: by 2002:a17:906:5446:: with SMTP id d6mr11732286ejp.185.1565245677293; Wed, 07 Aug 2019 23:27:57 -0700 (PDT) X-Google-Smtp-Source: APXvYqz6dzqajtZRAezKDQNmuve9RCQ4JytQRObNcoa9WCTo0xLV5ABGpgwszdU90+Eh+tAf3C3e X-Received: by 2002:a17:906:5446:: with SMTP id d6mr11732254ejp.185.1565245676547; Wed, 07 Aug 2019 23:27:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245676; cv=none; d=google.com; s=arc-20160816; b=eZ2LVse7IzkpcZTA4TT1IWFyAOKninqVnKmYq4vlZOVkjr+F12wCk4/IidumisdLgQ lpAESYnI37Mvv7fOFVZKmd6Sf9qjpOOrTpN0kQ7gGbuqmzRxV8qtl0JXRF03DOHUYl96 oV8Ag2FgQYZLgcVG4GRmHSmma0ST1J9Fr5lgDKNPNdsWdEO9x7N7c8AwJgpRc0KMGUOg NkW6jnNBU6ym1d7kWsAMEHqA8TNjhXDXT+rIo8w5WJ2Q4n59z8r0sSd9U1/ipAdxqSGM 9bscyrMon67DZAjDBJhsSxi+zoolyh0Ss5loYymWdwmObUiEXXOc5819UQtOH6MwlH44 y/Zw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=nIWYWxti2h+YKJsCHEKPojnLfBDVTb9i7wBNtAUzrEQ=; b=yiBhV1bdCfGyPhNv8GmkXmkURrdvp2spf+AQwCZYRq5HEB37p10wYFECiv8rSpXZaK KinQeWIx1A81oN0X5PZdgOxI3DMiGbl3//cr2MtCEw3W2Rk5Ulst82xEdfRnEdSszoU7 t8vZf7rY/D/zD9HF8wc+feKh0clw+TvtsQtuvs9eK0GUTh9yNdJBG/LRreQQ0WlSOArQ FOUZUM4W0GEvDWZQqgNK9YlHyUy658mqFO/0dLyQaKhcmia9JuDhSp9n+9ww+ge2Fd1I 7m1A3MP5BWwjSPLC7eLe3bazvqleAS6yis+B3pmchnTkegsMNoTIoirtMJTQDDItPeTU UijQ== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay11.mail.gandi.net (relay11.mail.gandi.net. [217.70.178.231]) by mx.google.com with ESMTPS id o26si36960896edc.423.2019.08.07.23.27.56 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:27:56 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.178.231; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.178.231 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay11.mail.gandi.net (Postfix) with ESMTPSA id D6A23100005; Thu, 8 Aug 2019 06:27:51 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 09/14] mips: Properly account for stack randomization and stack guard gap Date: Thu, 8 Aug 2019 02:17:51 -0400 Message-Id: <20190808061756.19712-10-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP This commit takes care of stack randomization and stack guard gap when computing mmap base address and checks if the task asked for randomization. This fixes the problem uncovered and not fixed for arm here: https://lkml.kernel.org/r/20170622200033.25714-1-riel@redhat.com Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Acked-by: Paul Burton Reviewed-by: Luis Chamberlain --- arch/mips/mm/mmap.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index d79f2b432318..f5c778113384 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -21,8 +21,9 @@ unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ EXPORT_SYMBOL(shm_align_mask); /* gap between mmap and stack */ -#define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((TASK_SIZE)/6*5) +#define MIN_GAP (128*1024*1024UL) +#define MAX_GAP ((TASK_SIZE)/6*5) +#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) static int mmap_is_legacy(struct rlimit *rlim_stack) { @@ -38,6 +39,15 @@ static int mmap_is_legacy(struct rlimit *rlim_stack) static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) { unsigned long gap = rlim_stack->rlim_cur; + unsigned long pad = stack_guard_gap; + + /* Account for stack randomization if necessary */ + if (current->flags & PF_RANDOMIZE) + pad += (STACK_RND_MASK << PAGE_SHIFT); + + /* Values close to RLIM_INFINITY can overflow. */ + if (gap + pad > gap) + gap += pad; if (gap < MIN_GAP) gap = MIN_GAP; From patchwork Thu Aug 8 06:17:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083297 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D29B113AC for ; Thu, 8 Aug 2019 06:29:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BF25D28726 for ; Thu, 8 Aug 2019 06:29:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AF3EB28ABC; Thu, 8 Aug 2019 06:29:05 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 847C428726 for ; Thu, 8 Aug 2019 06:29:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A94DB6B0006; Thu, 8 Aug 2019 02:29:02 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A456D6B000A; Thu, 8 Aug 2019 02:29:02 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 90E796B000C; Thu, 8 Aug 2019 02:29:02 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by kanga.kvack.org (Postfix) with ESMTP id 451516B0006 for ; Thu, 8 Aug 2019 02:29:02 -0400 (EDT) Received: by mail-ed1-f70.google.com with SMTP id y3so57559516edm.21 for ; Wed, 07 Aug 2019 23:29:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=CozSP8ClhxqYRnTVyauC5EuB32xQZZhYjVFYo6YFdyo=; b=FoY0IiLGB1bYx8NiCSWkq7n+dpT6S/Kb8TFTUfNsepCuWxCU7KmbosHMR5pqaZPBaI GHGk8mVNvpqtaB4eaw1MCHyZMdqAe8ITKvMxR0Wk8Zaw+YotDqy/bz+bVZxiq0uvaHIv IsQftsZzeuAmU2k6xc+NJysuKodtryZYQ0rabklWzajx3WSVR/nFvcqXsO2mHLRYYX2+ OCFhyG31xDtaBH01sI+Rs/gAPXBwSKco88Nci/+37+kt8emuAp9fIm9VokGDm4wrxz5Z zDFFHr5xTiTF8R9lpNPpULl+CckuFuRo0L4Db2mFgIOJhGTKhTjwEYEU8OnXt9E+eahz On7A== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUQwCJqKfIK2VW9mnjh2tbrG44mmPf8se1UrXDrYXXvjrihE7Rx v31TDk8F+bdYSQZHzkBViiWWCnPaQe+V71H872TfCqe3WsPqgXxjBflbpDSksKEVW0qdXOfZRg+ LCCTxpSEb+mhafHAxmHjnFph/uikXqNplIn+NnxvQN/Qv7sQVtrFWjcTg4BLLEyw= X-Received: by 2002:a50:f5f5:: with SMTP id x50mr13667990edm.89.1565245741871; Wed, 07 Aug 2019 23:29:01 -0700 (PDT) X-Google-Smtp-Source: APXvYqyF2uaeEPrwm3AbvmJYHgzOyOtuEKxeZNK+WVPrpE1r1AvKzsNsNReg051sx0pHDGy+pzxD X-Received: by 2002:a50:f5f5:: with SMTP id x50mr13667950edm.89.1565245741179; Wed, 07 Aug 2019 23:29:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245741; cv=none; d=google.com; s=arc-20160816; b=Jij1u8rHyMeL/LhOP8IKbehQbbeWCk4jVZJrAtB0FRHpCPzl8etBeyvP1Ls8A89TWF aZmxdIev9bgIckW/nScNgGzRUyPwX6zM5W5DbyMfJAf8tFkOybAGjHoOUdfk8g9G0VeE y0G7usFH5y7O38rDfi/3okakkzdQuLxMLxKHqxvU0O4ld/tp7yXfKoFqBeGP68GSw+Gb A0s4NGII8iKHiR3NW0jMhaZtlmw3KqFDlJdLVgCrb07nWMIBL7UBtUS51IYbWqZ/wbXf pk5/A2eRsAZg1qbbb9JC1ZyXA/VxiXUsMOYEqWw1cV1fo6ioi0EgE83mUX/n7+Oo/PuG EktQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=CozSP8ClhxqYRnTVyauC5EuB32xQZZhYjVFYo6YFdyo=; b=uura6PTn8hW5fGJXX3m/v1rRDOZyUcFQHKjlGB9t4jpz0r8Lb12boArjNgrsKU5BJ3 ioF/ap/fuRy8YF93EedxJUoMxIdRcCC98bLx4do9CTPyQd1/8Vtjdn/bnOqaDXrJ1C+K EMoZuNT+det6E40dN1yXty17EQuzqQ0wBxu0tnQ3VHUPVylyO8hrKw/IYNCitLZyw+/A XoRWRqoGL8C+qMWHrRugqf5i5t6JzY6rKKjAAT3ECFWQCWgstudUxgTOIL/8Pjx5xEcg s5+QMAbWlw5Xb4IFUaBUQfyzYk4oIwoABtzxgywJQrFcV58TteIb19yhiXYk3D32R1En g5kA== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id x7si34915645edm.177.2019.08.07.23.29.01 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:29:01 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id A498660009; Thu, 8 Aug 2019 06:28:56 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 10/14] mips: Use STACK_TOP when computing mmap base address Date: Thu, 8 Aug 2019 02:17:52 -0400 Message-Id: <20190808061756.19712-11-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP mmap base address must be computed wrt stack top address, using TASK_SIZE is wrong since STACK_TOP and TASK_SIZE are not equivalent. Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Acked-by: Paul Burton Reviewed-by: Luis Chamberlain --- arch/mips/mm/mmap.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index f5c778113384..a7e84b2e71d7 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -22,7 +22,7 @@ EXPORT_SYMBOL(shm_align_mask); /* gap between mmap and stack */ #define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((TASK_SIZE)/6*5) +#define MAX_GAP ((STACK_TOP)/6*5) #define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) static int mmap_is_legacy(struct rlimit *rlim_stack) @@ -54,7 +54,7 @@ static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) else if (gap > MAX_GAP) gap = MAX_GAP; - return PAGE_ALIGN(TASK_SIZE - gap - rnd); + return PAGE_ALIGN(STACK_TOP - gap - rnd); } #define COLOUR_ALIGN(addr, pgoff) \ From patchwork Thu Aug 8 06:17:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083313 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 636EF13AC for ; Thu, 8 Aug 2019 06:30:10 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 546DC28657 for ; Thu, 8 Aug 2019 06:30:10 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 47C5C28A3D; Thu, 8 Aug 2019 06:30:10 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E308828726 for ; Thu, 8 Aug 2019 06:30:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 081E26B0006; Thu, 8 Aug 2019 02:30:09 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 00BB36B000C; Thu, 8 Aug 2019 02:30:08 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEEC06B000D; Thu, 8 Aug 2019 02:30:08 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id 8F1BD6B0006 for ; Thu, 8 Aug 2019 02:30:08 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id a5so57583005edx.12 for ; Wed, 07 Aug 2019 23:30:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=PQ9i7jzea9mGKNH8KcMLWsVLtL6mR1QxNOrNNbBorkI=; b=lX8xtqnZ0BEfCxaUd8VsgWxroci3XIfoHLpqNmk8w+CPOz2DPw/YPiacIc8uWJdV7Y qDJr2hxZzJpRLRCtZ+mNajJFea4ybm0vgwVjN5Y7SqfE2cVlHEq9KKXdsU/TiYEOIw3l 5se6ELCLMdvNTMe3adw7ZbuUZZxfD9l+cl46l1BWjsjg2sAG1kIfG29xkqS1q7NgSQsB cbVf1CR6nXS0xd1FpBpussHa4+cx4ubkLIZu2nCkHQU8yPdadT7fnE1/YfkNhng7gIZe qZTqYu87aKG5wVoSy/NHNJ+Vv6TvTBTSQHzQR1Z4iwB5uVgSqK5PO13esBINSVJLK1E9 wtYA== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.196 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAXKdacajXtjJsPU55doNSpaBGP+dYIy56+VEZb88NHmdFFytVRm 5L49zvJLzOzVwrqymMj4vn9QUhdvMsLZS1ZPBaL9qE1M+/BKC85CEuSBmQhDPJpAVdjlD2GTwtd r7IlemukFB6hRCKzHAmp+/3kpBqZt9qoHpPwtvV7gvxwZUv6I4dVBDC3aTThKBJk= X-Received: by 2002:a17:906:ece7:: with SMTP id qt7mr11964011ejb.155.1565245808157; Wed, 07 Aug 2019 23:30:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqz4E3+hx4b2smncYlYyHNi5cp1g2KEtF4a/T4hTBXKzs6zRdXDpL5YQGQ1ouYWpYj3vLJpU X-Received: by 2002:a17:906:ece7:: with SMTP id qt7mr11963944ejb.155.1565245807210; Wed, 07 Aug 2019 23:30:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245807; cv=none; d=google.com; s=arc-20160816; b=VHleg7JBJGVF1UlrD3tu/QDOuWr/qGSycqzzCM+AqUnjBJcXn/afRUA7ViIWPfxF7C zMgUC2fUgp5ByKvb9JyA2cnVRnqfIlpXGuXFOr5HAMnbkgGxr45DPYIZIMkb1U8uDbCK oA5XdKAwEAejlbGfDnx6L7c4uEoN4lo8H0cT29249Uo3B148LxEyZlrHF5ATgoX+kvbO qsg6/d3ZcF7hhH6wJogqGSS6wmi4rPxsQkV3vYRNeTx0u27eFPqiHmKIyzsGyqfv65L9 NuVuAK0kzG1GBBYGntZKLK6WWNTZ2mN8yAgYBOOs/7QKwnrfff1HlFQFAioLuXCBwVn1 0q0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=PQ9i7jzea9mGKNH8KcMLWsVLtL6mR1QxNOrNNbBorkI=; b=EHhnXmaNSkJU3uRKN/NH53Umnvjqt0SGvzHBchxsu6T6LN0QOiE1uiezCeVeuOhxT3 vs9xrRfOsHkmlucYUOZTwZbG8K6XIoY3C4GHlIItEYNl5k5aV8o4KfUuyJLF+P0pYfOK GgkFlxC17duiX16LEcth888gO7ocwkJpEp/L9IcHdEBqb3PYQz/iqLo2xiTWF2qaA8lj +Qb9ZPhzJuaIi+2Xb5Y8sNQmAWnONTLg33arUDJ1qZMVDkWjWuFxPWfw5POYCg4hMkIq PEb8fr5zvW7GAVeOHgIErPfGwsuGsqJtEN7aPi2fvk2zJTe0pfjcmg8WUpcVucKNXW7n vB1g== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.196 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net. [217.70.183.196]) by mx.google.com with ESMTPS id y50si32961129edc.274.2019.08.07.23.30.07 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:30:07 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.196 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.196; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.196 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 42BAFE000B; Thu, 8 Aug 2019 06:30:01 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 11/14] mips: Adjust brk randomization offset to fit generic version Date: Thu, 8 Aug 2019 02:17:53 -0400 Message-Id: <20190808061756.19712-12-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP This commit simply bumps up to 32MB and 1GB the random offset of brk, compared to 8MB and 256MB, for 32bit and 64bit respectively. Suggested-by: Kees Cook Signed-off-by: Alexandre Ghiti Acked-by: Paul Burton Reviewed-by: Kees Cook Reviewed-by: Luis Chamberlain --- arch/mips/mm/mmap.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index a7e84b2e71d7..ff6ab87e9c56 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -16,6 +16,7 @@ #include #include #include +#include unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ EXPORT_SYMBOL(shm_align_mask); @@ -189,11 +190,11 @@ static inline unsigned long brk_rnd(void) unsigned long rnd = get_random_long(); rnd = rnd << PAGE_SHIFT; - /* 8MB for 32bit, 256MB for 64bit */ + /* 32MB for 32bit, 1GB for 64bit */ if (TASK_IS_32BIT_ADDR) - rnd = rnd & 0x7ffffful; + rnd = rnd & (SZ_32M - 1); else - rnd = rnd & 0xffffffful; + rnd = rnd & (SZ_1G - 1); return rnd; } From patchwork Thu Aug 8 06:17:54 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083321 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9C7D01399 for ; Thu, 8 Aug 2019 06:31:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8AFDC28AC1 for ; Thu, 8 Aug 2019 06:31:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7E0FB28ACB; Thu, 8 Aug 2019 06:31:19 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2854C28AC6 for ; Thu, 8 Aug 2019 06:31:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 478AD6B0006; Thu, 8 Aug 2019 02:31:17 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4029E6B000C; Thu, 8 Aug 2019 02:31:17 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2CAF26B000D; Thu, 8 Aug 2019 02:31:17 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id CF4306B0006 for ; Thu, 8 Aug 2019 02:31:16 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id x40so535245edm.4 for ; Wed, 07 Aug 2019 23:31:16 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=juJS37x5CM43D+6DWMHhdR9jUChuu/SEXc2RnrYUv1M=; b=Owgm3gCivKt74jip65Q5URxxxt9jmVrRxjceUR/ZZzhUup973BZ7yxBuosBVjXIgLm It4SJxl4B7gRgtMrEzzOyL4gAxUTljredIRhgwKpCqFHpMsVsqn3zSbeQsMs7h2BwX44 d+pRl7vdM6kW+Z+veZmD/iaoxBVDNMgQRrTOPQTTkT8h3f/cUUXXCki9pfhJd2LO2Uv+ kZfnfS6n8BFw13IsG8DScGeZ6sFSmddqqyoSseovAL6iGjL0tdKkN09ecKh1ryoSWASV 476ZZCeODwEQ5OOhOtpi0Z6uerRe9/nUoktb0ZfB0yslfFpAeNUi2MXRMl09A8vjWTTJ 4e1w== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUFjamobsypoZVtwxnpxXxU/EpQKEVE12cxEHA1x0XB2jvgk6Yl hYaDr5jd9cOikBO9fsFsJhC0NAT0xrGHXdOzYhtCDjZEyLXZZAr87R90wsX0m3AqvyGrsdLndPk mi8wJ9Ca0bNEP8J+2r0Lmfs0Fi+jJxJxMY+v5JHx11I0o3RlMG+YWKvpTyRithd4= X-Received: by 2002:a50:b13b:: with SMTP id k56mr14398567edd.192.1565245876414; Wed, 07 Aug 2019 23:31:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqwCaEQD9Lk8QaDUjJZ3b6y/J1zEVsfEmfDlI03n/7MmmTCYcEeCv4SnKSq5AR4VaS7AEyEM X-Received: by 2002:a50:b13b:: with SMTP id k56mr14398479edd.192.1565245874747; Wed, 07 Aug 2019 23:31:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245874; cv=none; d=google.com; s=arc-20160816; b=w0OdGiTbG5AuJmZop9xaQYIPjhYmG/gMgxQ2nuokNrJATLAGsJheEV81MVx1n6VPhd S/d3UuHd8MWJdz2UjSAIrQmo3Z39Q9D63Wz8EHFlcA1YCVrsu07Uf5tbh6obC0S+LsZo YAreX+fAvsPPe9ttDHQ9h6jaiqasQvrqUUuWDtTeI20wizSufS21zCBtf8fxRjIODg0k OoafXT3G31on1+1qHlx1+FfCLjyQxQx7JN4S+AF3s3vPSv+TfgA/ai2u0E09MsmrWJw+ /pxVAj1b4o8Zv4xgsYliE2uFsHelrrujWfNWxrExdZYY/juaLWVbNw42RIWluowoeCB1 YX9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=juJS37x5CM43D+6DWMHhdR9jUChuu/SEXc2RnrYUv1M=; b=hOXlhROt9w0kkPeUGYTtYTRW0Pqcpj6YkV01bZwOuOx3MMbVnJJrarmX+uIirQwhhh r5YYx1kG7BBqkrC4vog7hLdUPVQnBUWdQZvG2l0VaNj1TgxCR8LMEuj1PRd1G7+oqqjy u+hXF3J+mOjsJLowXbEje91DUrFxElhQ26s4Pqo1uqL8QKDfIP7sxu7ytAKuHsQI82nt 6xZCabs1+8xXYYgu5hBVCjP6FsOGManxvzgyO1RmsFJe+Zy7qMKpXt7dIbQNyvqfXCc+ 1VlQTSboalk292doIZGGKhjZco6H7jKSzAj48sONxnhYwyc5SyRL4lg7qEuYFd/0zu0G BE2Q== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay9-d.mail.gandi.net (relay9-d.mail.gandi.net. [217.70.183.199]) by mx.google.com with ESMTPS id 5si1065909edz.413.2019.08.07.23.31.14 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:31:14 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.199; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.199 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 60503FF802; Thu, 8 Aug 2019 06:31:07 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 12/14] mips: Replace arch specific way to determine 32bit task with generic version Date: Thu, 8 Aug 2019 02:17:54 -0400 Message-Id: <20190808061756.19712-13-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Mips uses TASK_IS_32BIT_ADDR to determine if a task is 32bit, but this define is mips specific and other arches do not have it: instead, use !IS_ENABLED(CONFIG_64BIT) || is_compat_task() condition. Signed-off-by: Alexandre Ghiti Acked-by: Paul Burton Reviewed-by: Kees Cook Reviewed-by: Luis Chamberlain --- arch/mips/mm/mmap.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index ff6ab87e9c56..d5106c26ac6a 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -17,6 +17,7 @@ #include #include #include +#include unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ EXPORT_SYMBOL(shm_align_mask); @@ -191,7 +192,7 @@ static inline unsigned long brk_rnd(void) rnd = rnd << PAGE_SHIFT; /* 32MB for 32bit, 1GB for 64bit */ - if (TASK_IS_32BIT_ADDR) + if (!IS_ENABLED(CONFIG_64BIT) || is_compat_task()) rnd = rnd & (SZ_32M - 1); else rnd = rnd & (SZ_1G - 1); From patchwork Thu Aug 8 06:17:55 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083333 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A40FB1395 for ; Thu, 8 Aug 2019 06:32:23 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9487028AC1 for ; Thu, 8 Aug 2019 06:32:23 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8754428AC6; Thu, 8 Aug 2019 06:32:23 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DEF6628AC1 for ; Thu, 8 Aug 2019 06:32:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CF0F6B0006; Thu, 8 Aug 2019 02:32:21 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 980316B000C; Thu, 8 Aug 2019 02:32:21 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8208F6B000D; Thu, 8 Aug 2019 02:32:21 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by kanga.kvack.org (Postfix) with ESMTP id 3285A6B0006 for ; Thu, 8 Aug 2019 02:32:21 -0400 (EDT) Received: by mail-ed1-f71.google.com with SMTP id k22so57620712ede.0 for ; Wed, 07 Aug 2019 23:32:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=ai6tRv6NH7TtSvQbIJYxYKlls6rj+pPySz74OsUl8/s=; b=f1aAnehj3K4Q7uce0nuwv/HtjQnk1EOfNwQA5Zj7KZ8AerSw2SpDQZS0ibuMj8PEKe pFDZ80bPGmI20xFl2dSuxHf7N4/p34w5YAH6p+jRrEzflQs08NilfiJWn+DRqSIs0Wsw 8qU57lQvKd3XscPOv8OxJu8XNJu60RFAXn0PPkHpNZZzdY1Nx7xUW6v4XJDg2T4slhhX zO/otoiZNiMTLkpjlHGzEN5S8zRhpwUJkfZRRyj68yrMJCuFKd3mw0KOX999Dh9T4+L1 mjcnU7+kvtpm71HG/RBIu3rEJs1MbH9LTtniwF4tESMSambadkoupvGr16NcL4vhqGDA tBaQ== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAV0jIswEKPFAILne/OKD4DY9BOv7/UnwvZBdIJ2cPlGhDWK9ZMr nkhdX+M/IVzg+Ynpbjp7574eQ72nyZ0LBrmccLVjQgYs3JgYu58jKygIH/wljOVJw8/wq08m0zE BAlxGsncyPov0Rr0Ti+wrflfudK8RUwRxVlJ1yvplWlP5INklfEhx9TeBUw/vefQ= X-Received: by 2002:a17:906:7d12:: with SMTP id u18mr11637444ejo.24.1565245940757; Wed, 07 Aug 2019 23:32:20 -0700 (PDT) X-Google-Smtp-Source: APXvYqyHvBMg2J+mRp3htAoG94lcJ+3/ZedNGVYgBBt4NfvY1fpCR+lfHEBCnF7CXaQQNOXjP/9Q X-Received: by 2002:a17:906:7d12:: with SMTP id u18mr11637377ejo.24.1565245939297; Wed, 07 Aug 2019 23:32:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245939; cv=none; d=google.com; s=arc-20160816; b=YKOBACTIxHWMhw4GFR11T8ZChzVv/LWVAPJ5hNK95vcHc00dPrJLFNr9ffk+LbV/0h 59ZCsphswy6I6kMghwMttQVFZLGdRJUbKG+OMsByrY4PWyTLMkUwGyOrrrjGMFPW0Asn mAsV1cH/wZf4E5K9IZUNVACkoGz/axPKKc94Z08ZCreRFBf3/86FHQfGVpF2G2NDNsHA i/H5nvd4r0NrHXCzGYNgEdlth2THF1Tjcr7wA14/z96U3LhKQPfMwtqSs2kNWlJovBS7 Pxe6aQjBbks9YUNQxESW1LjJJkh8IdVZZYZCEcBr0GET99oRUULVp0EHKuMqHoTo8hKH FZHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=ai6tRv6NH7TtSvQbIJYxYKlls6rj+pPySz74OsUl8/s=; b=WtQGGi8brwpyNwXHzCpc0MR6L53nyOSVMy0C4QIWb4RHRf+DwykjmSIsKVKJAxrB0I FjEh0r5NLv3v0mJjLYQrzfw0Xz969WgpfMlAG0baDGc1LtGbnQf1lDq1JX1Le6f769lO 83D5LbTZ22WglARtMU/+aAQEy5TUmYLEFP4aUEiDB7JD6CgGa2ERr04x8l7QDyzAd/Rp uk2V2R2yMrNqNsfDbSjEPCiTJX6QW3SsVgDARaR7sYlYx65pokqIC0bpRENraVykVD5b lYN0Skjj0uJgKNAoou4rF6tum9g36QlIPDDrWxbpbGOpsgiQU6S5gARHD8q820SeDgif zIrg== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net. [217.70.183.195]) by mx.google.com with ESMTPS id k24si31378163ejz.188.2019.08.07.23.32.19 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:32:19 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.195; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.195 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id B4C8060004; Thu, 8 Aug 2019 06:32:14 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 13/14] mips: Use generic mmap top-down layout and brk randomization Date: Thu, 8 Aug 2019 02:17:55 -0400 Message-Id: <20190808061756.19712-14-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP mips uses a top-down layout by default that exactly fits the generic functions, so get rid of arch specific code and use the generic version by selecting ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT. As ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT selects ARCH_HAS_ELF_RANDOMIZE, use the generic version of arch_randomize_brk since it also fits. Note that this commit also removes the possibility for mips to have elf randomization and no MMU: without MMU, the security added by randomization is worth nothing. Signed-off-by: Alexandre Ghiti Acked-by: Paul Burton Reviewed-by: Kees Cook Reviewed-by: Luis Chamberlain --- arch/mips/Kconfig | 2 +- arch/mips/include/asm/processor.h | 5 -- arch/mips/mm/mmap.c | 96 ------------------------------- 3 files changed, 1 insertion(+), 102 deletions(-) diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig index d50fafd7bf3a..4e85d7d2cf1a 100644 --- a/arch/mips/Kconfig +++ b/arch/mips/Kconfig @@ -5,7 +5,6 @@ config MIPS select ARCH_32BIT_OFF_T if !64BIT select ARCH_BINFMT_ELF_STATE if MIPS_FP_SUPPORT select ARCH_CLOCKSOURCE_DATA - select ARCH_HAS_ELF_RANDOMIZE select ARCH_HAS_TICK_BROADCAST if GENERIC_CLOCKEVENTS_BROADCAST select ARCH_HAS_UBSAN_SANITIZE_ALL select ARCH_SUPPORTS_UPROBES @@ -13,6 +12,7 @@ config MIPS select ARCH_USE_CMPXCHG_LOCKREF if 64BIT select ARCH_USE_QUEUED_RWLOCKS select ARCH_USE_QUEUED_SPINLOCKS + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU select ARCH_WANT_IPC_PARSE_VERSION select BUILDTIME_EXTABLE_SORT select CLONE_BACKWARDS diff --git a/arch/mips/include/asm/processor.h b/arch/mips/include/asm/processor.h index aca909bd7841..fba18d4a9190 100644 --- a/arch/mips/include/asm/processor.h +++ b/arch/mips/include/asm/processor.h @@ -29,11 +29,6 @@ extern unsigned int vced_count, vcei_count; -/* - * MIPS does have an arch_pick_mmap_layout() - */ -#define HAVE_ARCH_PICK_MMAP_LAYOUT 1 - #ifdef CONFIG_32BIT #ifdef CONFIG_KVM_GUEST /* User space process size is limited to 1GB in KVM Guest Mode */ diff --git a/arch/mips/mm/mmap.c b/arch/mips/mm/mmap.c index d5106c26ac6a..00fe90c6db3e 100644 --- a/arch/mips/mm/mmap.c +++ b/arch/mips/mm/mmap.c @@ -16,49 +16,10 @@ #include #include #include -#include -#include unsigned long shm_align_mask = PAGE_SIZE - 1; /* Sane caches */ EXPORT_SYMBOL(shm_align_mask); -/* gap between mmap and stack */ -#define MIN_GAP (128*1024*1024UL) -#define MAX_GAP ((STACK_TOP)/6*5) -#define STACK_RND_MASK (0x7ff >> (PAGE_SHIFT - 12)) - -static int mmap_is_legacy(struct rlimit *rlim_stack) -{ - if (current->personality & ADDR_COMPAT_LAYOUT) - return 1; - - if (rlim_stack->rlim_cur == RLIM_INFINITY) - return 1; - - return sysctl_legacy_va_layout; -} - -static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) -{ - unsigned long gap = rlim_stack->rlim_cur; - unsigned long pad = stack_guard_gap; - - /* Account for stack randomization if necessary */ - if (current->flags & PF_RANDOMIZE) - pad += (STACK_RND_MASK << PAGE_SHIFT); - - /* Values close to RLIM_INFINITY can overflow. */ - if (gap + pad > gap) - gap += pad; - - if (gap < MIN_GAP) - gap = MIN_GAP; - else if (gap > MAX_GAP) - gap = MAX_GAP; - - return PAGE_ALIGN(STACK_TOP - gap - rnd); -} - #define COLOUR_ALIGN(addr, pgoff) \ ((((addr) + shm_align_mask) & ~shm_align_mask) + \ (((pgoff) << PAGE_SHIFT) & shm_align_mask)) @@ -156,63 +117,6 @@ unsigned long arch_get_unmapped_area_topdown(struct file *filp, addr0, len, pgoff, flags, DOWN); } -unsigned long arch_mmap_rnd(void) -{ - unsigned long rnd; - -#ifdef CONFIG_COMPAT - if (TASK_IS_32BIT_ADDR) - rnd = get_random_long() & ((1UL << mmap_rnd_compat_bits) - 1); - else -#endif /* CONFIG_COMPAT */ - rnd = get_random_long() & ((1UL << mmap_rnd_bits) - 1); - - return rnd << PAGE_SHIFT; -} - -void arch_pick_mmap_layout(struct mm_struct *mm, struct rlimit *rlim_stack) -{ - unsigned long random_factor = 0UL; - - if (current->flags & PF_RANDOMIZE) - random_factor = arch_mmap_rnd(); - - if (mmap_is_legacy(rlim_stack)) { - mm->mmap_base = TASK_UNMAPPED_BASE + random_factor; - mm->get_unmapped_area = arch_get_unmapped_area; - } else { - mm->mmap_base = mmap_base(random_factor, rlim_stack); - mm->get_unmapped_area = arch_get_unmapped_area_topdown; - } -} - -static inline unsigned long brk_rnd(void) -{ - unsigned long rnd = get_random_long(); - - rnd = rnd << PAGE_SHIFT; - /* 32MB for 32bit, 1GB for 64bit */ - if (!IS_ENABLED(CONFIG_64BIT) || is_compat_task()) - rnd = rnd & (SZ_32M - 1); - else - rnd = rnd & (SZ_1G - 1); - - return rnd; -} - -unsigned long arch_randomize_brk(struct mm_struct *mm) -{ - unsigned long base = mm->brk; - unsigned long ret; - - ret = PAGE_ALIGN(base + brk_rnd()); - - if (ret < mm->brk) - return mm->brk; - - return ret; -} - bool __virt_addr_valid(const volatile void *kaddr) { unsigned long vaddr = (unsigned long)kaddr; From patchwork Thu Aug 8 06:17:56 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 11083341 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id BDF091395 for ; Thu, 8 Aug 2019 06:33:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ADEF328ACB for ; Thu, 8 Aug 2019 06:33:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A029328ACE; Thu, 8 Aug 2019 06:33:28 +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=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 08F2E28AC2 for ; Thu, 8 Aug 2019 06:33:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 478046B000D; Thu, 8 Aug 2019 02:33:27 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 429246B000E; Thu, 8 Aug 2019 02:33:27 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 33D436B0010; Thu, 8 Aug 2019 02:33:27 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by kanga.kvack.org (Postfix) with ESMTP id DA4F86B000D for ; Thu, 8 Aug 2019 02:33:26 -0400 (EDT) Received: by mail-ed1-f72.google.com with SMTP id b12so57548671ede.23 for ; Wed, 07 Aug 2019 23:33:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=hhD4vBSrp0XSthw0GklS6gaJ3KMWMaDwTu/gQP9IYFM=; b=YNQPfbL+HN4SZuzU5gdowYbwrEOTdUwZLzjkSFRyNFoJJGSkKUcAOeUQADbYatMwLl lG+8n/R90xdHVJgxJrCmJ6G0RC98j9qBIIXe39H/yA2eMCvzHU+OfnmIe5jN7Ttsobin ZMMYiSQb99d3iEY+CX7xWT4gFH1IVa2ieFLPpzUR1SYEbt03mCRzinRwodSRbyGLxLYL 8TFUQJztt+McSaEj6/cE5kO02EJwOMVPHR65q2yxsHuRojpThbXq4yrZB6GLvI9Y/N04 zSrzeg+kWBqQPQRGUATPvCzrmePwbsDJQF8R9DFy9LoGDVCBwprTrkAhgIqDFbboU7ZZ SSQA== X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.201 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Gm-Message-State: APjAAAUNA8cBeBWwIMRjtCoO7cn+UkKvnsFoeCV1hDJ7OVP1I5GSEuxq pQ8hI5sdKx34aWXpWrtiz0yI/VqNZXVJ24it93tor9ohL0dPrkdjzVJ/NlEowPwUBlJBw+ikCQi yre4pF8/K3udWWE0GcYaQseMSlT+grr8Jvf0x45o0uyp/wEJPApK+XRfHjXpMLOY= X-Received: by 2002:a50:ba19:: with SMTP id g25mr13866080edc.123.1565246006486; Wed, 07 Aug 2019 23:33:26 -0700 (PDT) X-Google-Smtp-Source: APXvYqyyLH7Xfv3KNAjnHDRQx/BwnLPJu7BRuPtABTSk2kRXDAB9HKDcywYrLefdtXezRLj3y69+ X-Received: by 2002:a50:ba19:: with SMTP id g25mr13866035edc.123.1565246005705; Wed, 07 Aug 2019 23:33:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565246005; cv=none; d=google.com; s=arc-20160816; b=CR8MnPXBNs1SCj89uEB+E4P71nzcjIJnWq+6Zf2ahowCi+5/fbZaUJqD/Dd5Sw0sul 4lRkbw1eoHW91Xwe4FY2qn55IthCve5dKdodJ3OcyfFjIFTCq3SNr3cMdHmKVGxnPNJX 4oqepvdgzps6SCTJfBL7PWvju5PMYZNlapxVX27wSA6La2oWQq5ATIZ1QiLNwqtz4iYF 4EvggbB4xDxuasXdkzrRopW7EdppwwngrMUubyE3B/AIg2G+kNIQb8kRzLBYjTi1aclI eZKuxe149GIdIMACPpFWvsDqI3mlvvA6AR8Oq6P2/otQO0mqHUo6HzO7erahiG0es9pX z0VA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from; bh=hhD4vBSrp0XSthw0GklS6gaJ3KMWMaDwTu/gQP9IYFM=; b=fbdLWYVLF9G+tr7jaab8DKwAGoFTuCsnCJKLZqMVVLwRpx1+0m1DSQZaAL1vrq2bfv GusPlbYPxe63e4divqrb0KduXbRf6Km7UFSdk6sYkBU9wtuPwVAkgHXvBhbcvtyaa/Vn TpC6uIT66UyEcm8UYRJPZCNpeP71uRU0IgDXZ+xRpD0N2hjiI1KfQTnhiqeDdQismtGB To0mKaWdoruKXKxUZlXo0p2eMpN/AEE3vKHXm26bA5yu8ZHGQnHXjFDdDYLf1yZTKY2a onMP1yH6IdkIXT6D1cYHAwa8QkBKkae3kNTGXNGwYxfozU1FtCygHP1s0694mkREjiQL TExg== ARC-Authentication-Results: i=1; mx.google.com; spf=neutral (google.com: 217.70.183.201 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net. [217.70.183.201]) by mx.google.com with ESMTPS id 39si35107966edq.151.2019.08.07.23.33.25 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Aug 2019 23:33:25 -0700 (PDT) Received-SPF: neutral (google.com: 217.70.183.201 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) client-ip=217.70.183.201; Authentication-Results: mx.google.com; spf=neutral (google.com: 217.70.183.201 is neither permitted nor denied by best guess record for domain of alex@ghiti.fr) smtp.mailfrom=alex@ghiti.fr X-Originating-IP: 79.86.19.127 Received: from alex.numericable.fr (127.19.86.79.rev.sfr.net [79.86.19.127]) (Authenticated sender: alex@ghiti.fr) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id 6957F1BF207; Thu, 8 Aug 2019 06:33:19 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Paul Walmsley , Luis Chamberlain , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Kees Cook , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, Alexandre Ghiti Subject: [PATCH v6 14/14] riscv: Make mmap allocation top-down by default Date: Thu, 8 Aug 2019 02:17:56 -0400 Message-Id: <20190808061756.19712-15-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190808061756.19712-1-alex@ghiti.fr> References: <20190808061756.19712-1-alex@ghiti.fr> MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP In order to avoid wasting user address space by using bottom-up mmap allocation scheme, prefer top-down scheme when possible. Before: root@qemuriscv64:~# cat /proc/self/maps 00010000-00016000 r-xp 00000000 fe:00 6389 /bin/cat.coreutils 00016000-00017000 r--p 00005000 fe:00 6389 /bin/cat.coreutils 00017000-00018000 rw-p 00006000 fe:00 6389 /bin/cat.coreutils 00018000-00039000 rw-p 00000000 00:00 0 [heap] 1555556000-155556d000 r-xp 00000000 fe:00 7193 /lib/ld-2.28.so 155556d000-155556e000 r--p 00016000 fe:00 7193 /lib/ld-2.28.so 155556e000-155556f000 rw-p 00017000 fe:00 7193 /lib/ld-2.28.so 155556f000-1555570000 rw-p 00000000 00:00 0 1555570000-1555572000 r-xp 00000000 00:00 0 [vdso] 1555574000-1555576000 rw-p 00000000 00:00 0 1555576000-1555674000 r-xp 00000000 fe:00 7187 /lib/libc-2.28.so 1555674000-1555678000 r--p 000fd000 fe:00 7187 /lib/libc-2.28.so 1555678000-155567a000 rw-p 00101000 fe:00 7187 /lib/libc-2.28.so 155567a000-15556a0000 rw-p 00000000 00:00 0 3fffb90000-3fffbb1000 rw-p 00000000 00:00 0 [stack] After: root@qemuriscv64:~# cat /proc/self/maps 00010000-00016000 r-xp 00000000 fe:00 6389 /bin/cat.coreutils 00016000-00017000 r--p 00005000 fe:00 6389 /bin/cat.coreutils 00017000-00018000 rw-p 00006000 fe:00 6389 /bin/cat.coreutils 2de81000-2dea2000 rw-p 00000000 00:00 0 [heap] 3ff7eb6000-3ff7ed8000 rw-p 00000000 00:00 0 3ff7ed8000-3ff7fd6000 r-xp 00000000 fe:00 7187 /lib/libc-2.28.so 3ff7fd6000-3ff7fda000 r--p 000fd000 fe:00 7187 /lib/libc-2.28.so 3ff7fda000-3ff7fdc000 rw-p 00101000 fe:00 7187 /lib/libc-2.28.so 3ff7fdc000-3ff7fe2000 rw-p 00000000 00:00 0 3ff7fe4000-3ff7fe6000 r-xp 00000000 00:00 0 [vdso] 3ff7fe6000-3ff7ffd000 r-xp 00000000 fe:00 7193 /lib/ld-2.28.so 3ff7ffd000-3ff7ffe000 r--p 00016000 fe:00 7193 /lib/ld-2.28.so 3ff7ffe000-3ff7fff000 rw-p 00017000 fe:00 7193 /lib/ld-2.28.so 3ff7fff000-3ff8000000 rw-p 00000000 00:00 0 3fff888000-3fff8a9000 rw-p 00000000 00:00 0 [stack] Signed-off-by: Alexandre Ghiti Acked-by: Paul Walmsley Reviewed-by: Christoph Hellwig Reviewed-by: Kees Cook Reviewed-by: Luis Chamberlain --- arch/riscv/Kconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 59a4727ecd6c..87dc5370becb 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -54,6 +54,18 @@ config RISCV select EDAC_SUPPORT select ARCH_HAS_GIGANTIC_PAGE select ARCH_WANT_HUGE_PMD_SHARE if 64BIT + select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU + select HAVE_ARCH_MMAP_RND_BITS + +config ARCH_MMAP_RND_BITS_MIN + default 18 if 64BIT + default 8 + +# max bits determined by the following formula: +# VA_BITS - PAGE_SHIFT - 3 +config ARCH_MMAP_RND_BITS_MAX + default 24 if 64BIT # SV39 based + default 17 config MMU def_bool y