Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp4276389ybi; Mon, 29 Jul 2019 23:02:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqyRbQQgegAnyc6NUInAgZ2AO/7kgTfxb8zdWsKkJeMZvSz4o0NmAf/POKB63WaJC6reGh+c X-Received: by 2002:aa7:9834:: with SMTP id q20mr40780791pfl.196.1564466528174; Mon, 29 Jul 2019 23:02:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564466528; cv=none; d=google.com; s=arc-20160816; b=CXDwNAVVheIRW9KoYag2w80C3/7q0s4gdL7yizUSgsiKFKdjoRN88kCPkevXNJMbCA Bv0yfHkvkTau2YRjAlECim3govNVSy7OB8IErIH/EJb/xH2k0JWCMzbnn/g1+qstnIGN LrN8ZNXkYuo2pLEIlYdOEQqQpiEY/NJuJldm38NAnJZ/onU36xdArcNTdcrbK6GntD2M 9SqBH5EgqwMxiJV+aoNSEzYWjkJ1Rhvl/JViqIZp2gPp3snUJAyAG52BFfIW53vCUFZD rJKLMG2RLxDtVsTV2x+U6IlhFx8yS9hWIAXu9BzXDKy0C1QQPJgynL1KNCCwomCBikF5 Rdsg== 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=vHBOaLLpKNFl1LEoQI+iZ4UEQKv2kvj0Rp05Hbqo9Es=; b=tI7atzL7hjLKkXydbGccfSVAzuQuAs6RWfAAKYVozwTAikeO7iRyB16pWPGSGYorsI 6EAHhludstu+cdwz0mTt0osyDcSQPJq1Lfif0DP2IBz/1IAghnv/UgVfgEy5iqD6SSx+ zJ7UWyYi6+yi5KqMrF9cMNtCHfjrL7hAvqexK2i5Uc9Jmmif1tgXzrRs+7JTl4rOIXT5 owTTVzBiYIxpjF6s2evVGNJfAvt/b1PjlRMTYAnAr2UiDT2qKCXX8IvJ+eUY7suyZR33 RRTlmI+IzJUUa4m4LhI/0e3y58e9iGnyWO8JFB2ncdbxFj2z/qS7si7DeS1X4ST4gBHU Q/+g== 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 78si27992978pfz.268.2019.07.29.23.01.53; Mon, 29 Jul 2019 23:02:08 -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 S1726266AbfG3GBT (ORCPT + 99 others); Tue, 30 Jul 2019 02:01:19 -0400 Received: from relay8-d.mail.gandi.net ([217.70.183.201]:51183 "EHLO relay8-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726033AbfG3GBT (ORCPT ); Tue, 30 Jul 2019 02:01:19 -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 relay8-d.mail.gandi.net (Postfix) with ESMTPSA id 1E7431BF20D; Tue, 30 Jul 2019 06:01:12 +0000 (UTC) From: Alexandre Ghiti To: Andrew Morton Cc: 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 v5 09/14] mips: Properly account for stack randomization and stack guard gap Date: Tue, 30 Jul 2019 01:51:08 -0400 Message-Id: <20190730055113.23635-10-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190730055113.23635-1-alex@ghiti.fr> References: <20190730055113.23635-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 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; -- 2.20.1