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 phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 6D461C636D7 for ; Thu, 23 Feb 2023 07:29:04 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id CDEE885738; Thu, 23 Feb 2023 08:29:01 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=xypron.glpk@gmx.de header.b="SPxk3eA1"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id E776985A68; Thu, 23 Feb 2023 08:28:59 +0100 (CET) Received: from mout.gmx.net (mout.gmx.net [212.227.15.19]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id D0E6D8551B for ; Thu, 23 Feb 2023 08:28:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=xypron.glpk@gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1677137333; i=xypron.glpk@gmx.de; bh=Yamp3AKOnA6nvncl2U15tFlli6DWjG62SplrHWx4/Ak=; h=X-UI-Sender-Class:Date:Subject:To:Cc:References:From:In-Reply-To; b=SPxk3eA1trxVW6rdwmYmxo+L6hJ90XJ9/DE30QANV4jeDlVzTWuez41VgWuxQ08Wk d0x3q+lcfUNzu2P/AXNA+2I8TxBJgZe02cIYidPbCX64rbmnhryk8gm6gHAvzwWk5h sF5TN+WsGLnm5FVX5ZvuI0Rg5OtJwviLZedI5JVXlbKst573AjETrVk/OWqPa9ikfH D/R4VYyJjrXC+kR0TE1nY5yOEL49s61xKUJYH4qUFLQ0pYWrXNCvkUyav9M9+sqMAc uuSdxom/Bo/rtZg1W2ia/jOVxNzr+sAaVQGYmroBwWh384NC5PK/SKh4CuSlRGvUCe MdAqrdyGe7ffA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.123.94] ([88.152.145.137]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MBUqL-1pMbyU22Tn-00CwXa; Thu, 23 Feb 2023 08:28:53 +0100 Message-ID: <2be810ff-d0dc-ef86-4cde-30413359c655@gmx.de> Date: Thu, 23 Feb 2023 08:28:44 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Subject: Re: [PATCH] efi_loader: efi_allocate_pages: check parameter pages To: Peng Fan Cc: u-boot@lists.denx.de, Peng Fan , Ye Li , =?UTF-8?Q?Vincent_Stehl=c3=a9?= , trini@konsulko.com, ilias.apalodimas@linaro.org, Heinrich Schuchardt References: <20230216105321.30948-1-peng.fan@oss.nxp.com> <1e8e8195-d31d-1fe2-8904-05584b2c204b@gmx.de> Content-Language: en-US From: Heinrich Schuchardt In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:NgGW/9aTWk8QWksI1madmOT5dCeyCSjtPo0+Gd54mwyMHdDi5wf oaFz/snfXJ6ioenUdIVnItuLsYr8WgaE8m45lGSYD2p6K5GgLNInkArjsoRDM7/loC3OYr3 dnH8+THw7jrbuNA3jGHUE/ybF9aslNJNF4PCjenUe7UD/kOGFb7mM8NcLY9UnoHkFCllfhb KqCPCWyO4yZ2Xxttu2DIQ== UI-OutboundReport: notjunk:1;M01:P0:QN6xiQZ42xY=;stIvaysWHSIS6UpxTb0x0MtkICv AOVGq+NeO0ai6kwJLIfIEtIElq8lIULo5NGOrnFNcnY7tieQiuEItWKlyBa086cfd7PporfD7 q4dgzF/ibxtEck3CvL0tsBQD/P/D1fDuoOO2k/Dy0fCqKuyTDVq2dPytw2SMvCR4QbdJ9qMxQ EKL6rvFrHBxrXV8vSubcMyLx+PtrflrIHpVA2YQB8B9QP/MoQAJr3vmiTErYv7MkG67dKWlrc bekjwbFVwS//+dR7Fe40lcyWJdVajVTHfpOGdF/JMstU/MeToYevpAukLisUSxpn5Q8F+hEtI RIYEWuAUyG9yP1LzPOXcCbcbiRTwATDdCqyYIHF1ySPpRrccpiGr2Gp+oGXq80l0jreyO2Har XpB7jcFywdgWtdT9yt8GJA/K2qZyoa2Dp2uVtcnAki/simQaMAExQCFlkPY2qCWYBCP6AP7px AL+VdkmcRIG1hJRe5O+GUHIpydUhFGdtJy2vllImQACp/kTHRL1Z1mb8YVNhUACwMwErw5atk 6IP/g3lwDGYzUraO8Na2msQS5DeRGb2ja+Qp/4BM/Xw255XgvlIRbvyrzdA7desbLMlJBzSWP T0hetv2Kdk6Sx2ngHspo9aKIEefhxiE98QLa/bUPcO3Q8A+2bAkj/GuTtfz3wlO1/lb40zzpi HTymHg8lFF99e2faK+iAPXh2IuVkuIde5LZECBqDeE1HHhA51mqfIFxv2H+6HSDPEsPE/HdqD l0z6LtK3IHqcBhge7abBZfIc/fI4dJuD67I9ozrDpnIvZOOe28mY0+mixmhif9a+R+Q7AY0/K +x7cA2vdpyt98uMcSCFf8KO+qyn72BJcrNtchk3T8aemqzhG28dqB2Dimezd/tItFhDLoubgz taWjPt5PQ5e3IPXZxtWO46fs1TxmrKfKe75lcQwBhbYX4qXnYMrvieoykykszcxdYHmNk1UMK A0RDGbFR1wlkUgom8fpQdXXfH58= X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean On 2/20/23 09:46, Peng Fan wrote: > > > On 2/20/2023 4:08 AM, Heinrich Schuchardt wrote: >> On 2/16/23 11:53, Peng Fan (OSS) wrote: >>> From: Peng Fan >>> >>> On i.MX8MM-EVK, when doing UEFI Capsule On Disk, we met such issue, >>> It will create Boot option for capsule on disk: >>> Boot0000: >>> VenHw(E61D73B9-A384-4ACC-AEAB-82E828F3628B)/eMMC(0x2)/eMMC(0x1)/HD(1,G= PT,F5CC8412-CD9F-4C9E-A782-0E945461E89E,0x800,0x32000) >>> But when capsule update finished, the updated image booting will >>> trigger: >>> Loading Boot0000 'UEFI Capsule On Disk' failed >>> EFI boot manager: Cannot load any image >>> Found EFI removable media binary efi/boot/bootaa64.efi >>> "Synchronous Abort" handler, esr 0x96000004 >>> elr: 000000004029f40c lr : 00000000402802f0 (reloc) >>> elr: 00000000bcd8b40c lr : 00000000bcd6c2f0 >>> x0 : 02029ee86154940e x1 : 00000000bcd95458 >>> x2 : 0000000000000010 x3 : 00000000bad31ad0 >>> x4 : 0000000000000000 x5 : 02029ee86154940e >>> x6 : 0000000007f00000 x7 : 0000000000000007 >>> x8 : 0000000000000009 x9 : 0000000000000008 >>> x10: 0000000000000035 x11: 0000000000000010 >>> x12: 0000000000000022 x13: 0000000000000001 >>> x14: 00000000bacdedf0 x15: 0000000000000021 >>> x16: 00000000bcd304d0 x17: 00000000000041a0 >>> x18: 00000000bacebdb0 x19: 00000000b9c9f040 >>> x20: 00000000bccecb28 x21: 00000000bcd95458 >>> x22: 0000000000000001 x23: 00000000bad1f010 >>> x24: 00000000bcdced70 x25: 0000000000001000 >>> x26: 00000000b9c9e000 x27: 0000000040000000 >>> x28: 0000000000000001 x29: 00000000bacdd030 Which place in the code do elr and lr relate to? Have a look into u-boot.map to find the function and use objdump to identify the exact code location in the *.o files. >>> >>> If is the pages is 0, the efi_find_free_memory will return the next us= ed >>> memory, check the parameter pages, and return EFI_INVALID_PARAMETER >>> if it >>> is 0. >>> >>> Reviewed-by: Ye Li >>> Reported-by: Vincent Stehl=C3=A9 >>> Signed-off-by: Peng Fan >>> --- >>> =C2=A0 lib/efi_loader/efi_memory.c | 3 +++ >>> =C2=A0 1 file changed, 3 insertions(+) >>> >>> diff --git a/lib/efi_loader/efi_memory.c b/lib/efi_loader/efi_memory.c >>> index b7bee98f79c..acca542033d 100644 >>> --- a/lib/efi_loader/efi_memory.c >>> +++ b/lib/efi_loader/efi_memory.c >>> @@ -495,6 +495,9 @@ efi_status_t efi_allocate_pages(enum >>> efi_allocate_type type, >>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (!memory) >>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return EFI_INVA= LID_PARAMETER; >>> >>> +=C2=A0=C2=A0=C2=A0 if (!pages) >>> +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 return EFI_INVALID_PARAMET= ER; >>> + >> >> Looking at the UEFI specification this looks wrong. The EFI >> specification does not forbid calling AllocatePages() with pages =3D=3D= 0. >> So we should return EFI_SUCCESS. >> >> EDK II returns EFI_NOT_FOUND for pages =3D=3D 0. But this has no basis = in >> the specification. >> >> Which function is the caller invoking AllocatePages() with pages =3D 0. >> Where is the patch to fix it? > > > The call trace as below: > do_bootefi->do_efibootmgr->efi_bootmgr_load->try_load_entry > ->efi_load_image->efi_load_image_from_path->efi_load_image_from_file > ->efi_allocate_pages->efi_find_free_memory > > There is an entry Boot0000: > VenHw(E61D73B9-A384-4ACC-AEAB-82E828F3628B)/eMMC(0x2)/eMMC(0x1)/HD(1,GPT= ,F5CC8412-CD9F-4C9E-A782-0E945461E89E,0x800,0x32000) > > But the capsule1.bin has been removed during capsule update, > but the entry is still there. > > The efi_file_size(f, &bs); will return bs as 0. > > The issue is if len is 0, the efi_find_free_memory will return > memory that are already used by others. The bug is in the caller not in AllocatePages: AllocatePages() sets a pointer to a memory range of size 0. We learnt in math the intersection of an empty set with any other set is an empty set. A memory range of size 0 does not overlap with any other memory range. We have to fix the caller that requests 0 pages and then uses memory outside of this zero byte area. How can your problem be reproduced on QEMU? Best regards Heinrich > > Regards, > Peng. >> >> Best regards >> >> Heinrich >> >>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 switch (type) { >>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 case EFI_ALLOCATE_ANY_PAGES: >>> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 /* Any page */ >>