Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp9167255ybi; Tue, 23 Jul 2019 23:10:55 -0700 (PDT) X-Google-Smtp-Source: APXvYqyTobh0qlfkW/c+SqELRBtkoCtoczHVL/Ocr0W3pnx+xvgvDgKMXyiRjHzK+Q3rmvUu8myP X-Received: by 2002:a17:902:968d:: with SMTP id n13mr36744790plp.257.1563948655801; Tue, 23 Jul 2019 23:10:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563948655; cv=none; d=google.com; s=arc-20160816; b=lmkYNuXcpb83//3nx3gXa333RzH5xQs4KpRkQ4HbVI7juDKlVeT4uo/ONicq4phue8 hFfGZFLtFfDptXCbfantHxcXZsFE1b0GRABAJUvgHCRwGpc6olGaB9KXZeMIxUPGhOvK PztZpwdnhIPAhTpuByVk+0Qhlkg6SOWOKvxhqn5Ptf9+BB78USUSjNwVYUyf5DMf7RMC pL1qQqXwtmpdQz39sO7/+TuAwVSa60ccFUQaxvVOiNqGooTQ1ELriQO0oPQXFWNDjnPh H3WYxKB4B9yd2ec93Y/ktmjc9SpEypVCyOcrKSKaEN9dyTvTECGtC48+mlHt0bnU9cWF o6QA== 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=yu1rRm8vVlWs2k5HxuFICi9Y4uSbUPRh8t0lFYmfJhM=; b=YUsmIP5sHrd/trVT2AmaBWhVAsMQcz+0W+YDtlyk9aQRN5vtQMQTZjxIImXn51Y+sS i5+6WKGuINtvlUwcI4wQgU1UkfZDOro/rC9wUXUJQ9LFENWh1y/kCU+QzFOlUjver7k1 eYhd/K62u2HgQyQNNSUWSKundcOWvHzcO5zCFw1EZTifyzmzUDmAi08L8AYePSJELcBx RrzYFtvutpDu5UFevQS03f3kWzN2AMC9S083wVnp4N54QT48rHofMcnuCK/73NAQFl16 v89+tE+0exbXLRomqI8JR3DPuQdUb38gDjTugLKZHoH28HmmE63Li+yUnMNjRgU+J9sP O5Xg== 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 72si12276078plb.177.2019.07.23.23.10.40; Tue, 23 Jul 2019 23:10:55 -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 S1726347AbfGXGIy (ORCPT + 99 others); Wed, 24 Jul 2019 02:08:54 -0400 Received: from relay3-d.mail.gandi.net ([217.70.183.195]:59115 "EHLO relay3-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725900AbfGXGIy (ORCPT ); Wed, 24 Jul 2019 02:08:54 -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 59E6560008; Wed, 24 Jul 2019 06:08:48 +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 REBASE v4 09/14] mips: Properly account for stack randomization and stack guard gap Date: Wed, 24 Jul 2019 01:58:45 -0400 Message-Id: <20190724055850.6232-10-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190724055850.6232-1-alex@ghiti.fr> References: <20190724055850.6232-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 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