Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp6572055ybh; Thu, 8 Aug 2019 02:17:31 -0700 (PDT) X-Google-Smtp-Source: APXvYqzl8xsW2I+rmVasGIbZNksKgpPzKvrnX9kYlqKPTRA1ob4eYSae0F90xbvDXTV5jkFc5DU7 X-Received: by 2002:a63:4e05:: with SMTP id c5mr8943720pgb.82.1565255851172; Thu, 08 Aug 2019 02:17:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565255851; cv=none; d=google.com; s=arc-20160816; b=AeDszTc0jgKxMSP8rWqVfzNt1DiEvDYSaGrJeG0IILWgt0m7HdyC4Ll1EIFogOHlqx nRRDynGDkfQKg9DBTd+TYcZlqinawcR94YjhF9TkTiRVmPh1WsakaWYf+Jlq/0tBmT18 Pid3dO6qZqbRW7PFQcZ7SAnODGtuvBgUBZvEyPGnNYwtHX+12B3GgT5fZoe/VbwznIRF 1tk+6LdPfv1jBq2K6HmtWXaS5X85c7mX5B0it7/wDwVdn9uHC7GtBKG1bqi1EaMi0hc/ Hwoo27lJ1Tvc3fRSYWlOqBuMGrdez5uqspplCpIHdn+wWeDq2OVPe7LiLBJ+Idt7YIi8 /Dkg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=M0Lkp7TI/1lX0PjPf1h84OWxjj1q7OPrwwtMkQUMB80=; b=PCcFoU5/eqdndEA0MgOUA8wDnwukMkFucMsbyiyD+Q9a3DcMA2g7i81YQZcyvgke+L 9ir4KBa33aUeKDBUKgMnG6A1Uf39Oh6ktaoR87yUAkoCDEMAVutXtvCV7CLG8wdOuoLm Y+DyNLADRwHwfk6AmGz8wb4245Dm29dlRkDxRYraGQEFFweh27WpaiHCEYcu0b0khgO8 G0JqKq6RfMf3LsqmWB/CPHQnKngzqDqQ7JYTHZItoiEgecHG3PsRUAfOys5dW8c32TZw S5JN/VSJpu2cP+ZfnRl/Bn5CaJTUXeR7MU17w95z3U5ry0fhy8tz5kMCwX0MbEPjNuG7 kFdQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@cogentembedded-com.20150623.gappssmtp.com header.s=20150623 header.b=Z1XISyDm; 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 c11si49918303pga.118.2019.08.08.02.17.15; Thu, 08 Aug 2019 02:17:31 -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=@cogentembedded-com.20150623.gappssmtp.com header.s=20150623 header.b=Z1XISyDm; 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 S1732011AbfHHJQU (ORCPT + 99 others); Thu, 8 Aug 2019 05:16:20 -0400 Received: from mail-lf1-f66.google.com ([209.85.167.66]:45216 "EHLO mail-lf1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731393AbfHHJQU (ORCPT ); Thu, 8 Aug 2019 05:16:20 -0400 Received: by mail-lf1-f66.google.com with SMTP id a30so3216733lfk.12 for ; Thu, 08 Aug 2019 02:16:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cogentembedded-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=M0Lkp7TI/1lX0PjPf1h84OWxjj1q7OPrwwtMkQUMB80=; b=Z1XISyDmNMrKzhH5jpoXRm+UDpftCfPHsXSHiMp386hpt6qzA47mI+MZCjhh8/Zeyw jokbLjILXaPIhi16o8/c61dqditvR/MIYHeLbkQyQnW1HIi9UnftiHokpg6qK507fm7J Af4qYSS1b0OGvvXKHezXLIVBZAA22zNmMLXIgrCO7kd7HL9v99JBKOfmG2a8I4SKpTHT bYqs+AyT4ajWAbpBYl2QGZEPQO6YYKHDcpVDFpxR617xywNAkq8MTsHvEe09qcGQm0N/ Q2SBZWzsoeIcTUgNNHs8jSQHhyhyqyNLDF5nH869msJXyFVwIVvvUIWwVSw8UZZBa3BW /xHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=M0Lkp7TI/1lX0PjPf1h84OWxjj1q7OPrwwtMkQUMB80=; b=erGg0SWoud3mfHWJRryXOi31pWOz953gJwghaCNEXfJt5u8j8/Q7uIS+W0FC9MxIE1 v9GYybDfYkFxo+MMdbpKRqStkq73SL1aaj4pSjz3n2EwnvhvzCkjY+domvibEP1deOXO 1kFtSNhCwfQ6ACnhdv7A8usUq2j7QwGnPU6itQcKSO6yWMZLlO4/fa3RroYf/nYMY/ox /NiT+S3Gadm+d/ND4bFv3tBsdRbMuQjx/VXemEpl/7pLusZCtpRHY31eFs6bsbg8mWyr fQlc3npy0MMf6jNOYBORpAO/gdSITzrRGQnsfM44VHMAAayAXb3QRduMhfEGkxSVU4A5 wflw== X-Gm-Message-State: APjAAAW6+TTv1XZJ51rPzSH67uZkugTiErvD61h1SDvrVtUXZGcBmyBs pjDKRMXoQsMkOY+8Z8EVhMfFdQ== X-Received: by 2002:a19:c711:: with SMTP id x17mr8605646lff.147.1565255778647; Thu, 08 Aug 2019 02:16:18 -0700 (PDT) Received: from ?IPv6:2a00:1fa0:8c7:ada9:25b2:24d8:3973:eb87? ([2a00:1fa0:8c7:ada9:25b2:24d8:3973:eb87]) by smtp.gmail.com with ESMTPSA id f23sm406561lfc.25.2019.08.08.02.16.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Aug 2019 02:16:17 -0700 (PDT) Subject: Re: [PATCH v6 09/14] mips: Properly account for stack randomization and stack guard gap To: Alexandre Ghiti , 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 References: <20190808061756.19712-1-alex@ghiti.fr> <20190808061756.19712-10-alex@ghiti.fr> From: Sergei Shtylyov Message-ID: Date: Thu, 8 Aug 2019 12:16:04 +0300 User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <20190808061756.19712-10-alex@ghiti.fr> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello! On 08.08.2019 9:17, 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 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) Could add spaces around *, while touching this anyway? And parens around TASK_SIZE shouldn't be needed... > +#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); Parens not needed here. > + > + /* Values close to RLIM_INFINITY can overflow. */ > + if (gap + pad > gap) > + gap += pad; > > if (gap < MIN_GAP) > gap = MIN_GAP; >