From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <bounce+27952+121005+7686176+12367111@groups.io>
Received: from mail05.groups.io (mail05.groups.io [45.79.224.7])
	by spool.mail.gandi.net (Postfix) with ESMTPS id F3D2274003A
	for <rebecca@openfw.io>; Wed, 15 Jan 2025 18:53:02 +0000 (UTC)
DKIM-Signature: a=rsa-sha256; bh=mY0s7FDYzHAosT1ijI1ZOFWAjwiqphQyIoM/c9Q3Jwc=;
 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=1736967182; v=1; x=1737226381;
 b=gLScR7PVu73Uvipd8dLi7CemXJ004Iv+EhHPMIPWYfYBXJT98OD5FB7gHdmXlkacrm+NlAEn
 wruMC9SvfmhunxS311uGOCwPR+ePBUNpEYgrAQiTBdRMSF4weLTDXLepmxzza/QWHlR50KxV/Th
 DDzoes0caD+p+TMf/0PI4j/8hjqnrfa5WPsKyjjEHbWU/mAAg3gH15TK88bJDAm+/T5vE64D+fM
 KeEP23zeTZrzSYbQsa+GAOLQb21ik/JsambqXcubjz8zPZ1oAE/qWeFSrk8VlFqI1UsleU888fP
 rT2NmSguUYhmK9fDJwAYsiQvK5eHupAnRo34kqQHWm0DA==
X-Received: by 127.0.0.2 with SMTP id WIV3YY7687511xPYSS927Khz; Wed, 15 Jan 2025 10:53:01 -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.web11.1127.1736440580829863505
 for <devel@edk2.groups.io>;
 Thu, 09 Jan 2025 08:36:21 -0800
X-Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-5d90a5581fcso1747011a12.1
        for <devel@edk2.groups.io>; Thu, 09 Jan 2025 08:36:20 -0800 (PST)
X-Forwarded-Encrypted: i=1; AJvYcCXCkxBXijlmxWwuo4v3eNXNmXmVhdTd/SlSeYsiVv5YuXka/B/eJk2yibnv2Vg7StMqVUswUA==@edk2.groups.io
X-Gm-Message-State: Kyik5vvSVTertVgtoJPzFv0wx7686176AA=
X-Gm-Gg: ASbGncsKWPfeBkt2Qsg8USSW5yia/U27nEczACzdRNowtAOLp4hQT3bg34ROfWNZnA1
	I8+5oT4ygLopnJ56J4S0/RnCNJ5PjRVGLegXCEvimDk8PFrEI099otIbgbkIIHgxdFgTo3qh1pq
	sKTiNEQ6PPHfFHuPY9FIHcS5SjqtONhEoHVB4EBtn3HVWJCbaxyJ3KgXNJXhveESEN7dfKy5Pdk
	xXbN/T/3BhVAl8j4a+J0RFbC9MHqlfZCNuaF+9tGVOpjxKbWupNCu5wKx6My5CoC8NhlCtc+emH
	YK3BiqDZ35E4hGD/ci0/tGrrzOij
X-Google-Smtp-Source: AGHT+IFH4Mf4jMgpRx3khbxhhYmeEL2WA8ovuW6XBYno3NsJIZaj1ShKSo8fqHjrVfklGFxDEAk+6g==
X-Received: by 2002:a05:6402:1ed4:b0:5d0:bcdd:ff8f with SMTP id 4fb4d7f45d1cf-5d972dfbcc3mr6581488a12.4.1736440578846;
        Thu, 09 Jan 2025 08:36:18 -0800 (PST)
X-Received: from ?IPV6:2a03:83e0:1126:4:829:739b:3caa:6500? ([2620:10d:c092:500::5:337c])
        by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5d9903c3333sm785494a12.47.2025.01.09.08.36.17
        (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
        Thu, 09 Jan 2025 08:36:18 -0800 (PST)
Message-ID: <d9c84079-6593-43f4-9483-648b665f03db@gmail.com>
Date: Thu, 9 Jan 2025 16:36:17 +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 <ardb@kernel.org>
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>
 <CAMj1kXH54Y0ae1OGwBe7-UiRBq9cFkDHbjxos_rEZPtan7NNzQ@mail.gmail.com>
From: "Usama Arif via groups.io" <usamaarif642=gmail.com@groups.io>
In-Reply-To: <CAMj1kXH54Y0ae1OGwBe7-UiRBq9cFkDHbjxos_rEZPtan7NNzQ@mail.gmail.com>
Precedence: Bulk
List-Subscribe: <mailto:devel+subscribe@edk2.groups.io>
List-Help: <mailto:devel+help@edk2.groups.io>
Sender: devel@edk2.groups.io
List-Id: <devel.edk2.groups.io>
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: <https://edk2.groups.io/g/devel/leave/12367111/7686176/1913456212/plugh>
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=gLScR7PV;
	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 09/01/2025 15:45, Ard Biesheuvel wrote:
> On Wed, 8 Jan 2025 at 23:00, Usama Arif <usamaarif642@gmail.com> 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 regions
>>   available (for e.g. if md->type or md->attribute is not the right valu=
e).
>>   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.nr_=
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@google=
.com/
>>
>> Fixes: 8fbe4c49c0cc ("efi/memattr: Ignore table if the size is clearly b=
ogus")
>> Reported-by: Breno Leitao <leitao@debian.org>
>> Signed-off-by: Usama Arif <usamaarif642@gmail.com>
>> ---
>>  drivers/firmware/efi/memattr.c | 16 ++++++----------
>>  1 file changed, 6 insertions(+), 10 deletions(-)
>>
>=20
> 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?
Also a very basic question, what do you mean by run with the firmware RWX?

Sorry for the very basic questions above!


>=20
> The main reason is that the EFI memory map and the EFI memory
> attributes table are supposed to be a matched pair, where each RTcode
> entry in the former is broken down into multiple code and data
> segments in the latter. The amount of mangling that the x86 arch code
> does of the EFI memory map makes it intractable to ensure that they
> remain in sync, and so it is better not to bother.
>=20
>=20
>> diff --git a/drivers/firmware/efi/memattr.c b/drivers/firmware/efi/memat=
tr.c
>> index c38b1a335590..d3bc161361fb 100644
>> --- a/drivers/firmware/efi/memattr.c
>> +++ b/drivers/firmware/efi/memattr.c
>> @@ -40,21 +40,17 @@ int __init efi_memattr_init(void)
>>                 goto unmap;
>>         }
>>
>> -
>>         /*
>> -        * Sanity check: the Memory Attributes Table contains up to 3 en=
tries
>> -        * for each entry of type EfiRuntimeServicesCode in the EFI memo=
ry map.
>> -        * So if the size of the table exceeds 3x the size of the entire=
 EFI
>> -        * memory map, there is clearly something wrong, and the table s=
hould
>> -        * just be ignored altogether.
>> +        * Sanity check: the Memory Attributes Table desc_size and
>> +        * efi.memmap.desc_size should match.
>>          */
>> -       size =3D tbl->num_entries * tbl->desc_size;
>> -       if (size > 3 * efi.memmap.nr_map * efi.memmap.desc_size) {
>> -               pr_warn(FW_BUG "Corrupted EFI Memory Attributes Table de=
tected! (version =3D=3D %u, desc_size =3D=3D %u, num_entries =3D=3D %u)\n",
>> -                       tbl->version, tbl->desc_size, tbl->num_entries);
>> +       if (efi.memmap.desc_size !=3D tbl->desc_size) {
>> +               pr_warn(FW_BUG "Corrupted EFI Memory Attributes Table de=
tected! (version =3D=3D %u, table desc_size =3D=3D %u, efi.memmap.desc_size=
 =3D=3D %lu, table num_entries =3D=3D %u)\n",
>> +                       tbl->version, tbl->desc_size, efi.memmap.desc_si=
ze, tbl->num_entries);
>>                 goto unmap;
>>         }
>>
>> +       size =3D tbl->num_entries * tbl->desc_size;
>>         tbl_size =3D sizeof(*tbl) + size;
>>         memblock_reserve(efi_mem_attr_table, tbl_size);
>>         set_bit(EFI_MEM_ATTR, &efi.flags);
>> --
>> 2.43.5
>>



-=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 (#121005): https://edk2.groups.io/g/devel/message/121005
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-