Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp2739590ybi; Sun, 26 May 2019 07:01:03 -0700 (PDT) X-Google-Smtp-Source: APXvYqzriU/HemYXKWAkC8DtC+uyxOgWtP7XLklk/ZQjaqVlhJX37T03U0vfzy0CbjeyYMze1SQY X-Received: by 2002:a17:90a:214f:: with SMTP id a73mr23807673pje.4.1558879263180; Sun, 26 May 2019 07:01:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1558879263; cv=none; d=google.com; s=arc-20160816; b=Wc6sKdgog5ivRdjEFfF0tH7nIhbFRXVErfXiBJIfk+bHIf+Prw9q5CLbefH+Q3HiS9 HQOKSPS2kzx+9aD3rgoXxc/0vkJCZi9u3d9O/u9ikvJMPcFH/RGgjoP4AEqpDg/tYMHw rXTv51jI5kc2a7G6SEmHkH9FoMf0cRP/UUB4ol+vLVvnAkp+v4l9tJDWWnz6fI2CY8ic PswlxzFwzGXAjuaMBzRARA1xovaOYGCAi8xBSJVHGu3Xmti21DedFnnl3nYiLguB3F2r HeYmG66yMzmdx7KVU0lGI7Q8LvqVUpg83wG+A3ARukeaL+7grexPvwrX1MRUhe9OY1pv gc2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ZDWhH5lHKvu6n/JfphSGR0tadyAKjYAsbuB4tHrJEFI=; b=BubPcqoGgH6NewKQHPBRqls6NNQOm2eEUyxwoQHjcHmyf1mhRQ0lgd2ioIgj/5MrVm u/DmHkUHB+LwIRSU8zGnIDARrEgXQAvgKMERoJI9Yh79r/ZW8/OmnfXDyXeoq3CWG2A4 T8L7cfePdBoLv9pIKmTHN4h/F8xMYcpWIDA7aiacIDAb/H0DfwHMMhv7dJGgoSzDXsCy tX6qlwt6RIg6wuEVEo0HqZq5V6qXiQhEU2GEGf6W5Zj9EX8ZCAezH2ij4WrPrESnarGl gxrhSI12VCxi+szawygekWv89kt0Y3EpNu7G3oLIvRKQ79a9sgi/1ejqvJuBx/f73KMm fdKg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j9si9751211pgl.298.2019.05.26.07.00.46; Sun, 26 May 2019 07:01:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727906AbfEZN6P (ORCPT + 99 others); Sun, 26 May 2019 09:58:15 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:36547 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726995AbfEZN6P (ORCPT ); Sun, 26 May 2019 09:58:15 -0400 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 D80B960006; Sun, 26 May 2019 13:58:06 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , 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 v4 09/14] mips: Properly account for stack randomization and stack guard gap Date: Sun, 26 May 2019 09:47:41 -0400 Message-Id: <20190526134746.9315-10-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190526134746.9315-1-alex@ghiti.fr> References: <20190526134746.9315-1-alex@ghiti.fr> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 2f616ebeb7e0..3ff82c6f7e24 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; -- 2.20.1