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 4CC13C433EF for ; Mon, 28 Feb 2022 22:30:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7CED28D0002; Mon, 28 Feb 2022 17:30:28 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 77E678D0001; Mon, 28 Feb 2022 17:30:28 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 646B08D0002; Mon, 28 Feb 2022 17:30:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0032.hostedemail.com [216.40.44.32]) by kanga.kvack.org (Postfix) with ESMTP id 559D58D0001 for ; Mon, 28 Feb 2022 17:30:28 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 0C03D181C49D8 for ; Mon, 28 Feb 2022 22:30:28 +0000 (UTC) X-FDA: 79193633736.29.F8C97A3 Received: from matoro.tk (matoro.tk [104.188.251.153]) by imf17.hostedemail.com (Postfix) with ESMTP id 4EE9C40006 for ; Mon, 28 Feb 2022 22:30:27 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=CMg65jXpaFZltUQYgNoVUODmgfV4a5Gt/jgOwLpN2Vk=; c=relaxed/relaxed; d=matoro.tk; h=Subject:Subject:Sender:To:To:Cc:Cc:From:From:Date:Date:MIME-Version:MIME-Version:Content-Type:Content-Type:Content-Transfer-Encoding:Content-Transfer-Encoding:Reply-To:In-Reply-To:In-Reply-To:Message-Id:Message-Id:References:References:Autocrypt:Openpgp; i=@matoro.tk; s=20220111215046; t=1646086466; v=1; x=1646518466; b=liCwhzLM5gcBk9AbRejqNqqD03KZZjvsbftjT52rBX+PyrAJWiCo0hlshB3hxd8NANeQVI4A KsG/n++GPEZcsWueUArKoi6tgLgbTt/G+1dlldQkow2gkTIetZjX6dYInMravLdk5Kqg5uQjCHo y2wQLkaLEqLYxr4q0h1IG1RVwOmRV/Psn0BnJQekN8/VUILqebObj32oAMawLxij3MDZdh07jA7 7+EQ7Roj3ZNECHml9j8VErF+h0Ns/v//HiUoGn3GRQu/GVGPVxdt4o7jeodJjQ/LKFZWIgeb/4y q+I98cGOqpJa0ZOZT8w1zTU2o2SWft3lxpNUhmWFkzgQ4GcFN0OrDc+KW8OTZD48lnC+1GZyD0n z0zjonAnVjmxiychDM41eK5O1awo4cilKOosIrPUiZafcJDZAjPRVkmj0fbgcPx6dsM/0/iZIyj z3y7twdwch0hg73Gh7ZkXBIIYhq+vaV0X7/qsQSVywp3ZWiPYsJ5r18p+TbEkOq/CHjnnepoPux Fp7PfuqEE9RKIIN510H0kPblNbd7aAXfDB3NvqqL1EQC5ipfWpAXdJ+T5jhtZYcxrjGWl4v1EKO nrDF/NGjXsM7QANPbbDxYAf70VhbgY8AhE9ch0LuKvXTQ54M9Rgbogx6wFw0X6HR6QGm96WdjHP Bu2xll2bPis= Received: by matoro.tk (envelope-sender ) with ESMTPS id 516b5765; Mon, 28 Feb 2022 17:14:26 -0500 MIME-Version: 1.0 Date: Mon, 28 Feb 2022 17:14:26 -0500 From: matoro To: Kees Cook Cc: Alexander Viro , Eric Biederman , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, John Paul Adrian Glaubitz , stable@vger.kernel.org, =?UTF-8?Q?Magnus_Gro=C3=9F?= , Thorsten Leemhuis , Anthony Yznaga , Andrew Morton , regressions@lists.linux.dev, linux-ia64@vger.kernel.org, linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org Subject: Re: [PATCH 5.16 v2] binfmt_elf: Avoid total_mapping_size for ET_EXEC In-Reply-To: <20220228205518.1265798-1-keescook@chromium.org> References: <20220228205518.1265798-1-keescook@chromium.org> Message-ID: X-Sender: matoro_mailinglist_kernel@matoro.tk Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 4EE9C40006 X-Stat-Signature: yt4xjnbfhhxbhzq9yqkjn39qxkg6aodw Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=matoro.tk header.s=20220111215046 header.b=liCwhzLM; dmarc=pass (policy=reject) header.from=matoro.tk; spf=pass (imf17.hostedemail.com: domain of matoro_mailinglist_kernel@matoro.tk designates 104.188.251.153 as permitted sender) smtp.mailfrom=matoro_mailinglist_kernel@matoro.tk X-HE-Tag: 1646087427-418087 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: On 2022-02-28 15:55, Kees Cook wrote: > Partially revert commit 5f501d555653 ("binfmt_elf: reintroduce using > MAP_FIXED_NOREPLACE"). > > At least ia64 has ET_EXEC PT_LOAD segments that are not virtual-address > contiguous (but _are_ file-offset contiguous). This would result in > giant mapping attempts to cover the entire span, including the virtual > address range hole. Disable total_mapping_size for ET_EXEC, which > reduces the MAP_FIXED_NOREPLACE coverage to only the first PT_LOAD: > > $ readelf -lW /usr/bin/gcc > ... > Program Headers: > Type Offset VirtAddr PhysAddr FileSiz MemSiz > ... > ... > LOAD 0x000000 0x4000000000000000 0x4000000000000000 0x00b5a0 0x00b5a0 > ... > LOAD 0x00b5a0 0x600000000000b5a0 0x600000000000b5a0 0x0005ac 0x000710 > ... > ... > ^^^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^^^^^^ > > File offset range : 0x000000-0x00bb4c > 0x00bb4c bytes > > Virtual address range : 0x4000000000000000-0x600000000000bcb0 > 0x200000000000bcb0 bytes > > Ironically, this is the reverse of the problem that originally caused > problems with ET_EXEC and MAP_FIXED_NOREPLACE: overlaps. This problem > is > with holes. Future work could restore full coverage if > load_elf_binary() > were to perform mappings in a separate phase from the loading (where > it could resolve both overlaps and holes). > > Cc: Alexander Viro > Cc: Eric Biederman > Cc: linux-fsdevel@vger.kernel.org > Cc: linux-mm@kvack.org > Reported-by: matoro > Reported-by: John Paul Adrian Glaubitz > Fixes: 5f501d555653 ("binfmt_elf: reintroduce using > MAP_FIXED_NOREPLACE") > Link: > https://lore.kernel.org/r/a3edd529-c42d-3b09-135c-7e98a15b150f@leemhuis.info > Cc: stable@vger.kernel.org > Signed-off-by: Kees Cook > --- > Here's the v5.16 backport. > --- > fs/binfmt_elf.c | 25 ++++++++++++++++++------- > 1 file changed, 18 insertions(+), 7 deletions(-) > > diff --git a/fs/binfmt_elf.c b/fs/binfmt_elf.c > index f8c7f26f1fbb..911a9e7044f4 100644 > --- a/fs/binfmt_elf.c > +++ b/fs/binfmt_elf.c > @@ -1135,14 +1135,25 @@ static int load_elf_binary(struct linux_binprm > *bprm) > * is then page aligned. > */ > load_bias = ELF_PAGESTART(load_bias - vaddr); > - } > > - /* > - * Calculate the entire size of the ELF mapping (total_size). > - * (Note that load_addr_set is set to true later once the > - * initial mapping is performed.) > - */ > - if (!load_addr_set) { > + /* > + * Calculate the entire size of the ELF mapping > + * (total_size), used for the initial mapping, > + * due to first_pt_load which is set to false later > + * once the initial mapping is performed. > + * > + * Note that this is only sensible when the LOAD > + * segments are contiguous (or overlapping). If > + * used for LOADs that are far apart, this would > + * cause the holes between LOADs to be mapped, > + * running the risk of having the mapping fail, > + * as it would be larger than the ELF file itself. > + * > + * As a result, only ET_DYN does this, since > + * some ET_EXEC (e.g. ia64) may have virtual > + * memory holes between LOADs. > + * > + */ > total_size = total_mapping_size(elf_phdata, > elf_ex->e_phnum); > if (!total_size) { This does the trick! Thank you so much!!