Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp6405222ybh; Wed, 7 Aug 2019 23:29:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqxWCs4765NX+pSzX/gTxXLWC8VPTE5ggMC6jAfbEn+m8D1Aj3fH//fKyWsJF/bj7AXRUBRY X-Received: by 2002:a63:a346:: with SMTP id v6mr11244652pgn.57.1565245780632; Wed, 07 Aug 2019 23:29:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565245780; cv=none; d=google.com; s=arc-20160816; b=loa7WPeJCxc371AsVom95X84m2OTjMT32l6Pj9lxIUDQuXq1Gedf02Q8Il3TzpzH3h sSUmYEa1o2GS1ukXW2LzJqJUus0860MCRQe4fZMxPJqGJa3CdlgE0oj2cLly81760sJ4 Hsa0zqCAqdaDju9IhFKWRIPo9a4ixWrm2nUvBue8FVXmhLlo58gJ6na7H4g2gj2l4zDd BXpnkgrkHTn0sc5FWCZkEaMRy+rwKUSVfzi5xZkjlSQTjtSd1HiEokTniXqEJgUgOf1N D/t4oJV7Ggvf5QuY3D4Xc7U9gHY297cyqjtmA18I17/tf71is3G0s57eDuS861LujNFw MCGg== 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=nIWYWxti2h+YKJsCHEKPojnLfBDVTb9i7wBNtAUzrEQ=; b=jw/x4z0iJ7Glf6/SOWhhtHtWpgcZKyMFFfOHWXwHPl1RdilVnh6wnzl2Xcn6Vzogu5 jR4JoftDJHoozxfkL5dzMYdxlMweipDu+EcU4Nj240v/guzi3JAkVMnjpzCRqdDZ3uZr wh2mLxY74HK8VMTN0g5B/L2PfJXq7iP+XIR52KxA0uUEkSNLNJnm03cpkODiE4XVHcrn dKsPsn4F5z/vNtSr0YbA5llrP1DL6SvVEmDMYih9dc2YDpqOX59BxBfdBKt83MFD/iei uG+bg5Fs1r5Tw0LY01CP1i6kADwpv2hviK5f18YtWxI8IvVmGowjiJqTS/Smc2k9RXO3 F08A== 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 cj14si48885471plb.141.2019.08.07.23.29.25; Wed, 07 Aug 2019 23:29:40 -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 S1731293AbfHHG16 (ORCPT + 99 others); Thu, 8 Aug 2019 02:27:58 -0400 Received: from relay11.mail.gandi.net ([217.70.178.231]:52269 "EHLO relay11.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725817AbfHHG16 (ORCPT ); Thu, 8 Aug 2019 02:27:58 -0400 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 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