From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 41BE7C4CED1 for ; Sun, 29 Sep 2019 17:32:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1CAB821D56 for ; Sun, 29 Sep 2019 17:32:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569778354; bh=w2whw1Hr/775putsFnagWM9RGPe7vXYrduAUtGNqhKA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:List-ID:From; b=yiRIHInPl0XWIRMRAJt59QUzI1fed9XL/whxATRgeCPyTm6/rtLZrVngPa8SGQ2bd tNR5b87XssKfLnE8qpZR0MKjq1TqqGrwpA6dZRXNJJmeXed2QespdRKJUAqte6aFhw 9Q0sR7qWT1veqMwItv710D8G0oS5lAMVTOGL5oUM= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729795AbfI2Rcd (ORCPT ); Sun, 29 Sep 2019 13:32:33 -0400 Received: from mail.kernel.org ([198.145.29.99]:43450 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729690AbfI2Rcc (ORCPT ); Sun, 29 Sep 2019 13:32:32 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5D9E321906; Sun, 29 Sep 2019 17:32:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1569778351; bh=w2whw1Hr/775putsFnagWM9RGPe7vXYrduAUtGNqhKA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XK4bDxCr35bFmglfCyHYtA/PMAzRv5uRLo69vxwvq/9zu+44Km/P40sMffn4iSKz9 LM9MszH/rVVPS2OjMOuqiNao/xzQ6KDPfbg7O2gaYlpdtSyMF+fwBJCzOy8DrMYxFC frfk0TcN60sTRniUF2+qfLFSg+2U17HxUgEqt//U= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Alexandre Ghiti , Kees Cook , Paul Burton , Luis Chamberlain , Albert Ou , Alexander Viro , Catalin Marinas , Christoph Hellwig , Christoph Hellwig , James Hogan , Palmer Dabbelt , Ralf Baechle , Russell King , Will Deacon , Andrew Morton , Linus Torvalds , Sasha Levin , linux-mips@vger.kernel.org Subject: [PATCH AUTOSEL 5.3 47/49] mips: properly account for stack randomization and stack guard gap Date: Sun, 29 Sep 2019 13:30:47 -0400 Message-Id: <20190929173053.8400-47-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190929173053.8400-1-sashal@kernel.org> References: <20190929173053.8400-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexandre Ghiti [ Upstream commit b1f61b5bde3a1f50392c97b4c8513d1b8efb1cf2 ] 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 Link: http://lkml.kernel.org/r/20190730055113.23635-10-alex@ghiti.fr Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook Acked-by: Paul Burton Reviewed-by: Luis Chamberlain Cc: Albert Ou Cc: Alexander Viro Cc: Catalin Marinas Cc: Christoph Hellwig Cc: Christoph Hellwig Cc: James Hogan Cc: Palmer Dabbelt Cc: Ralf Baechle Cc: Russell King Cc: Will Deacon Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- 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 d79f2b4323187..f5c778113384b 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