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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 145DBC3DA4A for ; Mon, 5 Aug 2024 17:27:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=xeGdgLusLh58pBlndxsOdraJRJHXml2OvWz58SJqlaM=; b=Rj1NHPICLbrRo/65g6Z7GIlpfd KLoumLeAJnisJNglsrtmaH0MtOWqnC9andRNhwualJBJ/m22SbtST+fjaCuc/sn85GM0Z1N4zF0aE MCV+0CEyTi8Y30MUVVpSgxnbGUoo8MnWeGsR1B5C9cRgcSpshLQosedyxOdfjUhlWTfe+NVpeGsR+ 3ySMsXn2eoxu1iQxHkZPf+IhraLwq9ZIRBA0Y5vmQuf0mYEFmSIo0dpaZgua+T6esUNaYiYoJpyen xWj5tRk3gt8bON0Li2Rvx9uM5mniKirw5iSnVQ1xoxVNlC0xKu3CpxCO/cVcbBq8e+RjETb32KmFN 1iyI8Oqw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sb1UE-0000000GgYJ-3Ngx; Mon, 05 Aug 2024 17:27:10 +0000 Received: from dfw.source.kernel.org ([2604:1380:4641:c500::1]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sb1Ti-0000000GgOG-1tJq for linux-arm-kernel@lists.infradead.org; Mon, 05 Aug 2024 17:26:39 +0000 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-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240805_102638_595903_DDED384F X-CRM114-Status: GOOD ( 31.88 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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