From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 42B26C3DA4A for ; Mon, 5 Aug 2024 17:26:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D7F3B6B009C; Mon, 5 Aug 2024 13:26:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D310F6B00A2; Mon, 5 Aug 2024 13:26:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BF7146B00A4; Mon, 5 Aug 2024 13:26:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A0E016B009C for ; Mon, 5 Aug 2024 13:26:40 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 57319C1D62 for ; Mon, 5 Aug 2024 17:26:40 +0000 (UTC) X-FDA: 82418871360.30.8E83A7D Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf09.hostedemail.com (Postfix) with ESMTP id A5EAD14002A for ; Mon, 5 Aug 2024 17:26:38 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WMS7ER2M; spf=pass (imf09.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722878791; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xeGdgLusLh58pBlndxsOdraJRJHXml2OvWz58SJqlaM=; b=CLffATsxRD3QkPqneV6PnNFo2WIUt6np9//I0RrMdBqStr80cT3Ybz9pNe8r+bCz6/PrFn TKOLoSN4hAIFNo9khGb1R0s+weqfgVOVtPAbCCg6m06ZOC5lv5oMOUVM/Xqq9UtEB9lnnI tiM1b7ngQfts5bGwlwzmtKVtnWrohCo= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=WMS7ER2M; spf=pass (imf09.hostedemail.com: domain of kees@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=kees@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722878791; a=rsa-sha256; cv=none; b=oPdp2FzzJbU5Q2UOatNOepNh4i49y3qeMS6IXb6LrR3aK+pJvClyaKClmCic1K4x7L5+6x 087nNDRFRK9vRtqVxtEzxACHgTiupnNjupXJAB3hZRhFYt4TACr6MM4XfqXxT6guYqYzBA 5aqIvv9sN9i716yOw/2w3hiOjycdXeE= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 8026B60D3E; Mon, 5 Aug 2024 17:26:37 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2ED30C32782; Mon, 5 Aug 2024 17:26:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1722878797; bh=O2CB6rBEhu4NPb7P1V0duKORxnAkoxB5yHW2RdhWdm4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=WMS7ER2M6Z2koFW9a18Ae/3nWSPJneSjgvK/5/Ydnxgf9okBs5Cee9IFS/sYos2pE wPtyRTdw0a5EuK7BLvm0OrUs+2UKcF9KSoMyn9yQ6IaLgsbAvsaXqd3aYuZgoznuOU YBB5NWDe65BlODpBqPhumGLYymm00SjBj5+FwASxRzHPcbDe8fUi0e0j5PrMqDnWp4 V5tCXMm/qTvdAwYw1ayNhtp3kxCOdusphzr5/J8ZIWpdojUVxMOVpJs4DpuLDL0+oq 0ue4qtnXFqTmj5n6sS9i7aeB1i7dCui7I7sAucsR/YsC5+CEPeFQrCmN/1qobD5Yhx mh3Mo0CH+WdTw== Date: Mon, 5 Aug 2024 10:26:36 -0700 From: Kees Cook To: David Gow Cc: Alexandre Ghiti , Luis Chamberlain , Russell King , Andrew Morton , Linus Walleij , Mark Rutland , linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org Subject: Re: [PATCH] mm: Only enforce minimum stack gap size if it's sensible Message-ID: <202408051025.33CA44DEE6@keescook> References: <20240803074642.1849623-2-davidgow@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240803074642.1849623-2-davidgow@google.com> X-Rspam-User: X-Stat-Signature: e14funfc54esp1dhkrd4u9yb7sxpqtok X-Rspamd-Queue-Id: A5EAD14002A X-Rspamd-Server: rspam11 X-HE-Tag: 1722878798-967514 X-HE-Meta: U2FsdGVkX1/T+rUfLB8LhDx1eNbrs0Rbdpxyr31vhsUwSYMFp0Dlb9OXQwJaj6fvOEIx5hr176S2luaOenjILUoV4b3hJmieiTjNDFRD+P6sax3xDSJo6OCBlIB2J/Qdy8/9X8p75CxzpQaDAJc9pdYf1HwXnHIG0wJjcHn9gI7ZtP/v9QhkGmttu0T6AcC0zX0ZjP8IQwgycJG+TOoC7/zGsQFD3zHYARX9n2abEQyfOb5thf7D0EFwPnRA4yvPc/F/p5KGxoYBq7srSpJYN8F2wEDalc6yvjhiEKiGxeGnDasdD0+EkqN37AyuMSvu9tgg1/rCe0eSaU78guo9W8AJms28+4h9XE1zucyHG/APcZ/MWiJ6SiBFXhFeEOu4wUbQYDrEmAvvLHO6W1xyLNlo7BTYVPnGe2/+UPC5s5iZu8up7Ub5W4J6qUOkbeUjw3RtNgj7Q7zTfcVs/bKWDmaV2WhMQIFQTnbc6odr71wi5bBnsj7Hjb9eBeFst9KESaisUs3LZohkmhtxsQVHQnxCvRYwVm1c7V6sTztFyvmSYhsQNaVMtvrkABTiGfCz1iOmQyvMXjvPe5FznUrwJzy5m8ZkT6Ht+yjUvRN/0qntru10cra0PP3UiSdppRxXnZyrOgnnXfdcVBrWTbXHIey909CWY3s2uVL3Og4qcId0N+TgOHCrxViLvlNT08MVBWDJucESzGN0n7Avd1nJdNqoCc4NoPa/7GaKgH3hohymyXqgfXtVx5pYPOKTk537pOKxcOni0xWMj3dZgcQfXXlUZeyB1vYGRN5fUf1zcMLwQ+7rqKt4bx9EDZU7jh1+HjFG2xZszOHI79HzhBdV0/IfMcx3j/WfAH6vDjkqlf9csaD70gJN4ahOdUGiepZm7FcvfwxfWRxqaRlwEoBjG5PQf5tA7uQZ9c29WB6WR2uiyN883lf+MBxlVzIgJsnBmldP556lQ0BgXRcG0bp HI9VEGYR RosioY5nnFops6ujUKC3PatlKGrXyiU9WT9UAUIRqmFvlFIZY3P23jNYRD7JX+L0npwzQNyLD6T8QAACpvDE3O0ycMO+/tLcDrDOy9S58O1L+GbMLqi2Gs0ixfPVneREpgjV3VR/y6+NRdEszobi2RtyULTPp1THXGWE8e7jzZH8twgSlumdHgpd3i75NO3Aonz/qsIRadjAnPoiyGHtBUj0/RSn1BQO8jXzTnxys5naXFIUk5zCr54PqZsiHCj8qYgQn9nbCBlfWUoqVUHUfb1Q9Wj6RbJNsXREY5KVmBtT+Lwk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Sat, Aug 03, 2024 at 03:46:41PM +0800, David Gow wrote: > The generic mmap_base code tries to leave a gap between the top > of the stack and the mmap base address, but enforces a minimum > gap size (MIN_GAP) of 128MB, which is too large on some setups. In > particular, on arm tasks without ADDR_LIMIT_32BIT, the STACK_TOP > value is less than 128MB, so it's impossible to fit such a gap in. > > Only enforce this minimum if MIN_GAP < MAX_GAP, as we'd prefer to honour > MAX_GAP, which is defined proportionally, so scales better and always > leaves us with both _some_ stack space and some room for mmap. > > This fixes the usercopy KUnit test suite on 32-bit arm, as it doesn't > set any personality flags so gets the default (in this case 26-bit) > task size. This test can be run with: > ./tools/testing/kunit/kunit.py run --arch arm usercopy --make_options LLVM=1 > > Fixes: dba79c3df4a2 ("arm: use generic mmap top-down layout and brk randomization") > Signed-off-by: David Gow > --- > > This is one possible fix for an issue with the usercopy_kunit suite > (and, indeed, the KUnit user_alloc features) on 32-bit arm. The other > options are to: > - hack the KUnit allocation to force ADDR_LIMIT_32BIT or > ADDR_COMPAT_LAYOUT; or > - similarly, use an unlimited stack, which forces the legacy layout > behind the scenes; or > - adjust MIN_GAP based on either STACK_TOP or architecture. > > Of them, I made the arbitrary call that this was least hacky, but am > happy to go with something else if someone who actually knows what's > going on suggests it. > > (Also, does this issue actually mean some strange legacy binaries have > been broken with an rlimit-ed stack for ages? Or am I missing something?) > > Cheers, > -- David I see akpm already snagged this, but yeah, this looks like a totally sane fix. Thanks for digging in and finding the problem! Reviewed-by: Kees Cook -Kees > > --- > mm/util.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/mm/util.c b/mm/util.c > index bd283e2132e0..baca6cafc9f1 100644 > --- a/mm/util.c > +++ b/mm/util.c > @@ -463,7 +463,7 @@ static unsigned long mmap_base(unsigned long rnd, struct rlimit *rlim_stack) > if (gap + pad > gap) > gap += pad; > > - if (gap < MIN_GAP) > + if (gap < MIN_GAP && MIN_GAP < MAX_GAP) > gap = MIN_GAP; > else if (gap > MAX_GAP) > gap = MAX_GAP; > -- > 2.46.0.rc2.264.g509ed76dc8-goog > -- Kees Cook