Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp3233180pxm; Mon, 28 Feb 2022 15:08:55 -0800 (PST) X-Google-Smtp-Source: ABdhPJz8uap89yAEv6YiF+fwiN7qDGcN50EkDfCXHjjhf1kKbATK6nuTIU1KYgItFEiueg/cHgmx X-Received: by 2002:a17:902:e5c3:b0:14f:a4ff:34b8 with SMTP id u3-20020a170902e5c300b0014fa4ff34b8mr22288855plf.24.1646089735332; Mon, 28 Feb 2022 15:08:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646089735; cv=none; d=google.com; s=arc-20160816; b=HLpjr5BcFzNaIYczjBrs5XmCUytiY/mQdcBuamyDTaRy248RrZr/lmmqqnl8okeH3d QvPndBCA2wtPKxIsbC2td/q6KBVR1/H+ToTKi9ceLZ/rdIaQUctlCLUjzKNZEjZLNzTm aCgxzLo7JkoVimmHdBQlu5sHzWKRvhr7DlbDKsVGXEJdxXkqEwc21L9Vgb+XXGgEz1ol r+Rp3C0Es9R5UutMCntRU64/H5dcPic7AyKTDo5woBtOwbetmNctMtYtd0Eia0CbvxHn MJbOg097iYrMRUb4U2FauiMfz8Q9HomTseFCkGp75bEqwIpMm/CyynzAcPiR828Q9EMb FIqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=zibmIXYjhxaupOk9s0xWr7duCuTjugvyvgKLHyYpgFE=; b=Payj19iEEdxS7KUWCm2PBjAo0gCchhyCkjrlGrlopzWu5bIFMjVxshU9po8C5oXuJ8 jlnlbPQ0K+O9jpEAT4TYfvWhub9cID1Y2k0YD5kwknSEiGkVG1iCCpB0k/7NVr2c1dMN Vr2anPQ0HGzFkx1X/dU2JCnoSgjz4ZZwWWgh5MMiHg0RKVu9UssT/vZ9L3nZK4rL/wRV 6GhVi/+IeRbTYAQdFx78vnwPc614FLbxIGphjU6hLTZM+hx2f/l/7klxYQ1TY4Vfd02F MoALIJAsds4tFTBpwsHH6eX2M3Ch1GRdbh7NRm01L0w4FfPy/eC3QGBU9TJrqnaUpd95 MCMg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=iCpxXt6G; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a13-20020a634d0d000000b0037345bef58dsi10072503pgb.642.2022.02.28.15.08.29; Mon, 28 Feb 2022 15:08:55 -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=@chromium.org header.s=google header.b=iCpxXt6G; 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=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229963AbiB1WyJ (ORCPT + 99 others); Mon, 28 Feb 2022 17:54:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229834AbiB1WyG (ORCPT ); Mon, 28 Feb 2022 17:54:06 -0500 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACDC319A for ; Mon, 28 Feb 2022 14:53:26 -0800 (PST) Received: by mail-pf1-x434.google.com with SMTP id k1so1902677pfu.2 for ; Mon, 28 Feb 2022 14:53:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=zibmIXYjhxaupOk9s0xWr7duCuTjugvyvgKLHyYpgFE=; b=iCpxXt6G0aThtvRUcLNU8SriL190rIC2M+2t85Gz/EPinufDECNpaCnslsRSQAk3ep MPSStIzbBpIxl3lRd+df6HbZ3lim+gftRnXLHB8sidpRHKV2QhZoSgXk99khZDcvuzcT Spee0qr8TUPt+3I4rqnlZ4mG/KhdXvQrBaZQM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=zibmIXYjhxaupOk9s0xWr7duCuTjugvyvgKLHyYpgFE=; b=KykLZBgHdIZUa/QlP8ZQ7FucRNsIouC8ZifXM2z2AGjnj3b+Ad2bThzR5temK4U51O 3Gvd4UC8lu7EmEizfVfbigUYQsdEFEK0MrxAqQQjpNXm0eySlS5k3BS6lBC1EWRj8ohU h+KIojrBY0TI7A3czV5LOcEKzVwkqH9WeTWKXPVUlvQ1axMEwwfrgeqW6E2VYk1PyuVY 0jbgZya7ryJkn1aMlyC7afcdYIA+KtoIvIJSabU42MPgIQwBz0zMof+qIPVx+UuyMIYX ktvCCKJUKkOcXudnnXsKSJVoetbRbTaXQwnJDLwrdzIKNjaGntjy0ZFjmSpqPYhbXbfj BsZw== X-Gm-Message-State: AOAM532FalDHQQ9VhFmt85pusqSerhbEX9xI8RzHY4EyzDeyQ6P29bWx UHz+fdO52Lyi8DY61uTs5VCavA== X-Received: by 2002:a65:6210:0:b0:374:ba5:aacc with SMTP id d16-20020a656210000000b003740ba5aaccmr18921903pgv.8.1646088806198; Mon, 28 Feb 2022 14:53:26 -0800 (PST) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id f9-20020a056a00228900b004f3ba7d177csm14943547pfe.54.2022.02.28.14.53.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Feb 2022 14:53:25 -0800 (PST) Date: Mon, 28 Feb 2022 14:53:25 -0800 From: Kees Cook To: matoro Cc: Alexander Viro , Eric Biederman , linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, John Paul Adrian Glaubitz , stable@vger.kernel.org, Magnus =?iso-8859-1?Q?Gro=DF?= , 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 Message-ID: <202202281452.93E321A39@keescook> References: <20220228205518.1265798-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Mon, Feb 28, 2022 at 05:14:26PM -0500, matoro wrote: > 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!! Excellent; thank you for testing! I'll send this to Linus shortly. -- Kees Cook