Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3207356pxm; Mon, 28 Feb 2022 14:31:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJzqr1SDtcr+RwX8kfKAGnXsmDjHTWY9W7RnkyfaOKfDrQTVglNr2cE+IxzKjuzF+6IcFB44 X-Received: by 2002:a17:907:1256:b0:6b0:5b4c:d855 with SMTP id wc22-20020a170907125600b006b05b4cd855mr18114399ejb.458.1646087466632; Mon, 28 Feb 2022 14:31:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646087466; cv=none; d=google.com; s=arc-20160816; b=ykxM+z4QuoRL5IxfcAyS07M7vQ27r6+hDuuC7FskhCqrwSLwxTGwoRarcdX86QESNS 0EBXFiSlYt/dm8Tqr0/7Xw2lpJMm1XSNV8A/BZdvmu2uGQnVCRcDynVovx8KtQJ+SCBJ 10Lc5ai/UVyqscj7bplWn3NWXGKd1Gdp7sOh4+1JkZaV7QqocLbjsaxdND1NkPDBPHK+ qi9oGX4WxveBw3gtgKlPGVupGPOSqh0HfgwWARulLuDABDixWMRzIyJ5Rtug+rkCZdfB I3xcG6W8mnO6jvXfAybU+8I/Csyz11STnt3+7WE2Dx+R7gc/HslLjqHcPpInt0Dmager Vglg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:message-id:references :in-reply-to:subject:cc:to:from:date:mime-version:dkim-signature; bh=CMg65jXpaFZltUQYgNoVUODmgfV4a5Gt/jgOwLpN2Vk=; b=IsExRAWsBHyQ7KaW+Uw/SXDn4fYYTmNnHYiiH8tbWUkkNZ8eAVVDiFm5tte3g24qBZ P2sjCrGZDs127SLQhK1DuRaaEHgbCV7Ya4BvxY3pxUjIrZCjusSnuNZXnrCfIXbv1nuB SRvFkwFx4Itkn6YNszN4cwvrfoAro11vRDCrc7W9cS72KNJPIHdKCILOpbszVo5g2/RB BZQXlAslLhZJb2Nk85u5ORZAv3wFhHtlU5s26GjBV+UndS0UjTDjSQI1QVxwGs4dWIz8 daKYv2W2NkeEF+zk/6T33JEW+2Q7Me0F17ppExbNdEa0Wm7m7zmPU/AI5XbYa8f/bWHM cbRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@matoro.tk header.s=20220111215046 header.b=liCwhzLM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=matoro.tk Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k9-20020a1709060ca900b006afa69971casi6822466ejh.25.2022.02.28.14.30.41; Mon, 28 Feb 2022 14:31:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@matoro.tk header.s=20220111215046 header.b=liCwhzLM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=matoro.tk Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231321AbiB1WPx (ORCPT + 99 others); Mon, 28 Feb 2022 17:15:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33868 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231360AbiB1WPt (ORCPT ); Mon, 28 Feb 2022 17:15:49 -0500 Received: from matoro.tk (unknown [IPv6:2600:1700:4b10:9d80::2]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 95B0EE1B74; Mon, 28 Feb 2022 14:14:46 -0800 (PST) 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-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RDNS_NONE,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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!!