Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp3860758yba; Tue, 16 Apr 2019 22:33:11 -0700 (PDT) X-Google-Smtp-Source: APXvYqzkyMZbsvv9dM45x7Ndl0aoCw0fhXEzgK/C3IYU310uk/C681EGa+LHLt2XZqBMhjLeiNzo X-Received: by 2002:a63:bd52:: with SMTP id d18mr80607499pgp.52.1555479191668; Tue, 16 Apr 2019 22:33:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555479191; cv=none; d=google.com; s=arc-20160816; b=XweI7NGkAmUGoEGorOdwhvAWJS/6BcYckVCKSR9QzRxWpOisQN+phl2uoWhBHiQ9ks cOvYrVHkRQIQnkXDodP/1Qno4jtLO9cmgRdBkkhXa6Bv8eU1fnmtu77473bXrZkTRgdI qQ7cB8QCIkLzFiFhdQa6fNB1Mrn1qwPUUvO7Ddfvfihm/5DKaLi+SyhNiFAQ4o068wtJ ITuiDtcdZ3aHweUxMu6cGuxXD5qi7RvCf2z0FjME8yhyx5dzsXTJRAISWBtWGad5nmck zcqdz623M7ns6XPJWrHYdO0RZy+yEy36aAQL4MQzfiPxlz9PK6ZxYXi7gwHS8Y1SRzMq yWSA== 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=3oOhKgf5++Zcd2NhUlCVUeA8BDKs7cw/2jMalEBWqIE=; b=wpN/IsajPDCpLTcJnqy7dV3p599st31DYNjN+Irw1GUKeJA4my2415rHx4GUc2F8pG 6oUHuhvE4/fQw+rubjVpAvji0YI6fuIPFgfmBPrgMyoMICjSuQx03CWWLZDgjtDFbTZu PMZsKsLxq4F/1mjWnhoC0SPax6d5fKTo5wS+awISs1rTbhV5JEatDBz7qcSUuPSgTQ82 zFAg513mOHsrYo20BoXK8elDVvi4fSaDVuWy1cvv8q92h823xPbuSUakkUX8OMzS9aSQ JJkBYgzYRtO8NkoMrbFRVkLyQFW27wPRWGKjodNzYr0lM8rePy4+wjJvuDRGYxJ0LZv1 XVvA== 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 h8si34956297pgg.573.2019.04.16.22.32.56; Tue, 16 Apr 2019 22:33:11 -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 S1729995AbfDQFbj (ORCPT + 99 others); Wed, 17 Apr 2019 01:31:39 -0400 Received: from relay9-d.mail.gandi.net ([217.70.183.199]:56257 "EHLO relay9-d.mail.gandi.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725774AbfDQFbj (ORCPT ); Wed, 17 Apr 2019 01:31:39 -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 relay9-d.mail.gandi.net (Postfix) with ESMTPSA id 4BE7CFF804; Wed, 17 Apr 2019 05:31:33 +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 v3 08/11] mips: Properly account for stack randomization and stack guard gap Date: Wed, 17 Apr 2019 01:22:44 -0400 Message-Id: <20190417052247.17809-9-alex@ghiti.fr> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190417052247.17809-1-alex@ghiti.fr> References: <20190417052247.17809-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 mips here: https://www.mail-archive.com/linux-kernel@vger.kernel.org/msg1429066.html Signed-off-by: Alexandre Ghiti --- 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