Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp183414yba; Wed, 17 Apr 2019 22:37:09 -0700 (PDT) X-Google-Smtp-Source: APXvYqxcClHPtho4Vs8rb0lhOXCGyXf2g8WstUSS2oB9bMPN+cWt2tuaZ54lkcUMDNFPBg7rBkhv X-Received: by 2002:a17:902:e382:: with SMTP id ch2mr90961067plb.94.1555565829021; Wed, 17 Apr 2019 22:37:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555565829; cv=none; d=google.com; s=arc-20160816; b=iFJevId8SH9qhOLWyKIPbUfz6K00Q6C2GwJPAqpgvR39kVpEnhN7KCSaTK6eYCurXz /hjg1PFnYmBMNv+51ZSM2MV4hBYFw+K59L54KO8KLwrBVNH6SIMIpFwzg+Z/UXojTGZ4 Qzb0zf3bQR0FK/gsOiBNZZSggFfQ0xOavHIcdFuhZld78ng9WOLi4DtocErP5AGw7zPK FOf8T86UFayDLrir+CiRcSBd0W+fj13X6OcmqBzOOH7yiATwtegboZuI/TWJJ6Dd0fz4 CAmSLc/Pn0CuLCNif9aWdYOTuaacTrxfc49a7PqBWwGgovRSmQUAAnS2VK0vHfdHI6A0 tU/Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=bLtDzKXu09gPBmOTPhLlCKa/1j1gZ8w+6fLRYpgkXVc=; b=P6OdussgiJ5yS0XCCjE9ydlOAdB+UJej2fx3NlpFB12pkd0iJInG8BZiUI4LAU21by 8bY0u7LeUx75uBerUgMy/74C3R/t0Gd95g1csop4VgjmPJE30lw4YRBLdc4sb0ldMrl4 9rtIQXQKM/uCfF3qAyhj21O/2FuV/7jpUxnwC5R2SMVUv6tEhASU0f7IhFuKDu9b0zj8 Lt+noO7N45A05dHhJLG9KWK4z0zUTx1Qo8Gf3BHt5uGh2as+i04V2CEfCy1wAUMzfB3V B2gu7u+B+eohzBI/UUzRvpR/TE0wcKUemCClVNUZv3N2ED7OxtSrC0oMgTlLTU1EBVgZ z3Vg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=edeVbOaB; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z8si922786pgu.217.2019.04.17.22.36.53; Wed, 17 Apr 2019 22:37:09 -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; dkim=pass header.i=@chromium.org header.s=google header.b=edeVbOaB; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731172AbfDRFgB (ORCPT + 99 others); Thu, 18 Apr 2019 01:36:01 -0400 Received: from mail-vs1-f67.google.com ([209.85.217.67]:37647 "EHLO mail-vs1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725876AbfDRFgA (ORCPT ); Thu, 18 Apr 2019 01:36:00 -0400 Received: by mail-vs1-f67.google.com with SMTP id w13so517276vsc.4 for ; Wed, 17 Apr 2019 22:36:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=bLtDzKXu09gPBmOTPhLlCKa/1j1gZ8w+6fLRYpgkXVc=; b=edeVbOaBMLhN2dBQJXpv+D+6vAjewlRc9kM5lTmzsJMTYvnZuw8u8Y7DqZfh26UEgW OHIgnpHVLQHXols38+FJXscMQV6lYZx93+goYy0WamrsRBkh1x83txAmNxUkD0xqfkb/ 5V9w/CLbuOWB3OiL1UReRwHKvROTO3KbTfB1M= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=bLtDzKXu09gPBmOTPhLlCKa/1j1gZ8w+6fLRYpgkXVc=; b=igAX0YxApteJcFtelnXpGM2SLm7BFZi/PDrLfl0hGMQfhF/4pKw6Q3DCURenX6MuM6 fFpuLIaPJYhxO2q+n1yeWH5tvCyzilZvRoEaqgldZiEkP0wllxGhwKD36Y93xCryONmM ojohiIjYqPoTZv41jCZ9o6ZPQ6kGUJuIU7+WhY7daCNcduXTAG+JJX3dq89/VOgphl7e g8qno9oMmQOFzPURP6tAe8603KL4WXgvcgukY/OuSKEjyB0eVvIUbTF6VNw2ChnKanfK 4ur7XnLfonc2ODf86VLElm0eklf/Cy/gH3FggFLpOq7YkiSHsp3FitGOR/I9TYEH4WC2 42lQ== X-Gm-Message-State: APjAAAU0S08mYYXBCTNB/RGdGSiLNKr3s4Zr8ZiCqiGVpE5sBKY8HTim o/fLd/HW1UDI7D73f/bZWkruz4XftPY= X-Received: by 2002:a67:89c7:: with SMTP id l190mr51053294vsd.199.1555565759642; Wed, 17 Apr 2019 22:35:59 -0700 (PDT) Received: from mail-vs1-f48.google.com (mail-vs1-f48.google.com. [209.85.217.48]) by smtp.gmail.com with ESMTPSA id v22sm272371vkv.35.2019.04.17.22.35.59 for (version=TLS1_3 cipher=AEAD-AES128-GCM-SHA256 bits=128/128); Wed, 17 Apr 2019 22:35:59 -0700 (PDT) Received: by mail-vs1-f48.google.com with SMTP id w13so517264vsc.4 for ; Wed, 17 Apr 2019 22:35:59 -0700 (PDT) X-Received: by 2002:a67:e881:: with SMTP id x1mr52185580vsn.48.1555565442783; Wed, 17 Apr 2019 22:30:42 -0700 (PDT) MIME-Version: 1.0 References: <20190417052247.17809-1-alex@ghiti.fr> <20190417052247.17809-9-alex@ghiti.fr> In-Reply-To: <20190417052247.17809-9-alex@ghiti.fr> From: Kees Cook Date: Thu, 18 Apr 2019 00:30:31 -0500 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v3 08/11] mips: Properly account for stack randomization and stack guard gap To: Alexandre Ghiti Cc: Andrew Morton , Christoph Hellwig , Russell King , Catalin Marinas , Will Deacon , Ralf Baechle , Paul Burton , James Hogan , Palmer Dabbelt , Albert Ou , Alexander Viro , Luis Chamberlain , Kees Cook , LKML , linux-arm-kernel , linux-mips@vger.kernel.org, linux-riscv@lists.infradead.org, "linux-fsdevel@vger.kernel.org" , Linux-MM Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 17, 2019 at 12:31 AM Alexandre Ghiti wrote: > > 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 same URL change here please... > > Signed-off-by: Alexandre Ghiti Acked-by: Kees Cook -Kees > --- > 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 > -- Kees Cook