From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 284EA740034 for ; Wed, 15 Jan 2025 18:53:03 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=gAMULo73OWV/B5BVdQRXMzFChaIyL1gNdpuE8UoUu94=; c=relaxed/simple; d=groups.io; h=Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References:From:In-Reply-To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20240830; t=1736967183; v=1; x=1737226382; b=Czcy8PzJn8FfGZ1COOmIBqcfW78pANNW30KEdIdP6DN57gjMQQDQvhQXMNr58xImcJoClRfP y+1ysRoC2EN5I3Zyks09Q59iYZTm2cOV4+lzfzMz+zlwaQrx/PnLLSmamU3DbGtLd82TqzEl73w X3DDvULfbDWmqM5I1FsyZOe9rWm9PJumWQCtvs6L9U3v2BKwV5TMUV65rV2iZodX2wY4+rZyaoy zMII+/tvbvtvrScp7G73qJas/STKQX58q3ELLU3cX4r0Mk0l3WqiUSLmQzsI2bkuvKZHMKa2YSl 1bhUA2m8rln+g5ukHCaM/u1nmYlom2nTuXXKSdHjCOPQw== X-Received: by 127.0.0.2 with SMTP id hZfsYY7687511xF8D0l9lMA8; Wed, 15 Jan 2025 10:53:02 -0800 X-Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) by mx.groups.io with SMTP id smtpd.web10.15725.1736506436920282133 for ; Fri, 10 Jan 2025 02:53:57 -0800 X-Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-5d3f28a4fccso2534700a12.2 for ; Fri, 10 Jan 2025 02:53:56 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCV3PL5kv3tNoEISd867jI70pifUxynwXIUb6WV+Azr4R4i3QK3u/3XuqdyC8Phvvvgi8WhDVQ==@edk2.groups.io X-Gm-Message-State: 6FEbgOsdxkrdHXl4jzPdbffwx7686176AA= X-Gm-Gg: ASbGncswM6CtKAPmw9uclRUnRCuz0GbvREgfP2m9RoRVFoJa/mmvfd4Rs2RaSVg+96O jtTuzJE8iLWnTCGUF3YMXbguD2dmF8seXhMff0IurLcFi7boT6pwoTFTT60/Q6fpREkKzvBarU1 RlPZmr5S8k8GZJtxgXOyCRh2DvnBSNYwyBTlaAirHPPzVyo8ebkfOR9W4D0KquugbFIofxKHd1d 3eeMjE5QSZug/m+2iZAG360oKMtvIE7aQmdqfPNJ+REeMemqkpTuQ2ViOT/1sAyunrYDmdCZT/+ DQWLw5kWERy+TzxBMP/JfJrbjLSK X-Google-Smtp-Source: AGHT+IHUoKF5CCleTB2jU9vLsvOAEPHxoqMfeJizDfgZZl9x+zXjW5TDWSL8oHA1WM8ocRBnyA/UtQ== X-Received: by 2002:a05:6402:2347:b0:5d2:729f:995f with SMTP id 4fb4d7f45d1cf-5d972e6f957mr8778390a12.29.1736506435017; Fri, 10 Jan 2025 02:53:55 -0800 (PST) X-Received: from ?IPV6:2a03:83e0:1126:4:829:739b:3caa:6500? ([2620:10d:c092:500::5:e213]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5d98fe8f68csm1544698a12.0.2025.01.10.02.53.53 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 10 Jan 2025 02:53:54 -0800 (PST) Message-ID: <8613563a-ee7c-4271-b1f0-4d1ac365ad3a@gmail.com> Date: Fri, 10 Jan 2025 10:53:53 +0000 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [edk2-devel] [RFC 1/2] efi/memattr: Use desc_size instead of total size to check for corruption To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org, devel@edk2.groups.io, kexec@lists.infradead.org, hannes@cmpxchg.org, dyoung@redhat.com, x86@kernel.org, linux-kernel@vger.kernel.org, leitao@debian.org, gourry@gourry.net, kernel-team@meta.com References: <20250108215957.3437660-1-usamaarif642@gmail.com> <20250108215957.3437660-2-usamaarif642@gmail.com> From: "Usama Arif via groups.io" In-Reply-To: Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Resent-Date: Wed, 15 Jan 2025 10:52:52 -0800 Resent-From: usamaarif642@gmail.com Reply-To: devel@edk2.groups.io,usamaarif642@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240830 header.b=Czcy8PzJ; dmarc=pass (policy=none) header.from=groups.io; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io On 10/01/2025 07:21, Ard Biesheuvel wrote: > On Thu, 9 Jan 2025 at 17:36, Usama Arif wrote: >> >> >> >> On 09/01/2025 15:45, Ard Biesheuvel wrote: >>> On Wed, 8 Jan 2025 at 23:00, Usama Arif wrote: >>>> >>>> The commit in [1] introduced a check to see if EFI memory attributes >>>> table was corrupted. It assumed that efi.memmap.nr_map remains >>>> constant, but it changes during late boot. >>>> Hence, the check is valid during cold boot, but not in the subsequent >>>> kexec boot. >>>> >>>> This is best explained with an exampled. At cold boot, for a test >>>> machine: >>>> efi.memmap.nr_map=3D91, >>>> memory_attributes_table->num_entries=3D48, >>>> desc_size =3D 48 >>>> Hence, the check introduced in [1] where 3x the size of the >>>> entire EFI memory map is a reasonable upper bound for the size of this >>>> table is valid. >>>> >>>> In late boot __efi_enter_virtual_mode calls 2 functions that updates >>>> efi.memmap.nr_map: >>>> - efi_map_regions which reduces the `count` of map entries >>>> (for e.g. if should_map_region returns false) and which is reflected >>>> in efi.memmap by __efi_memmap_init. >>>> At this point efi.memmap.nr_map becomes 46 in the test machine. >>>> - efi_free_boot_services which also reduces the number of memory regio= ns >>>> available (for e.g. if md->type or md->attribute is not the right va= lue). >>>> At this point efi.memmap.nr_map becomes 9 in the test machine. >>>> Hence when you kexec into a new kernel and pass efi.memmap, the >>>> paramaters that are compared are: >>>> efi.memmap.nr_map=3D9, >>>> memory_attributes_table->num_entries=3D48, >>>> desc_size =3D 48 >>>> where the check in [1] is no longer valid with such a low efi.memmap.n= r_map >>>> as it was reduced due to efi_map_regions and efi_free_boot_services. >>>> >>>> A more appropriate check is to see if the description size reported by >>>> efi and memory attributes table is the same. >>>> >>>> [1] https://lore.kernel.org/all/20241031175822.2952471-2-ardb+git@goog= le.com/ >>>> >>>> Fixes: 8fbe4c49c0cc ("efi/memattr: Ignore table if the size is clearly= bogus") >>>> Reported-by: Breno Leitao >>>> Signed-off-by: Usama Arif >>>> --- >>>> drivers/firmware/efi/memattr.c | 16 ++++++---------- >>>> 1 file changed, 6 insertions(+), 10 deletions(-) >>>> >>> >>> The more I think about this, the more I feel that kexec on x86 should >>> simply discard this table, and run with the firmware code RWX (which >>> is not the end of the world). >> >> >> By discard this table, do you mean kexec not use e820_table_firmware? >=20 > No, I mean kexec ignores the memory attributes table. >=20 >> Also a very basic question, what do you mean by run with the firmware RW= X? >> >=20 > The memory attributes table is an overlay for the EFI memory map that > describes which runtime code regions may be mapped with restricted > permissions. Without this table, everything will be mapped writable as > well as executable, but only in the EFI page tables, which are only > active when an EFI call is in progress. >=20 Thanks for explaining! So basically get rid of memattr.c :) Do you mean get rid of it only for kexec, or not do it for any boot (including cold boot)? I do like this idea! I couldn't find this in the git history, but do you know if this was added in the linux kernel just because EFI spec added support for it, or if there was a specific security problem? Thanks! -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#121007): https://edk2.groups.io/g/devel/message/121007 Mute This Topic: https://groups.io/mt/110517813/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-