From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by mx.groups.io with SMTP id smtpd.web09.5440.1662539635537510239 for ; Wed, 07 Sep 2022 01:33:56 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=rxsr9C5s; spf=pass (domain: kernel.org, ip: 145.40.68.75, mailfrom: ardb@kernel.org) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 74588B81A0F for ; Wed, 7 Sep 2022 08:33:53 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 26396C4347C for ; Wed, 7 Sep 2022 08:33:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1662539632; bh=MIFzvG7JVmGJwJVgl7lJipkenS/4q4pDQbmvxacf/jM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=rxsr9C5s1KRegbuL2Gzu6+w+S98W+k5ipXBMAsBYivvQBpoXqnOPrmSm1iIwZf57k grVv6Lgag6yAkWrjAm/xkv9gngXKBUu7zAOK7u+9qwGJX6Q1kCjBLkaJrVhJxMDLuw NeDvDM+xsUcwPH5ljiWorqCH3KIoSfVaAj6hZ6DjDl2Xe7tGlOzLx5BLU0yQNPRt/F 8A/+y858kvm6dI7r5B1jXLFpobcJ029dJ08pD51Jx1JOqtHcuoGvr51heza89QlX2a Lrka8iDCaNu3RlCBBrmJjIWN4tydDhQMCcWHvJZdWEjhSV0n6nJJ70eYFu3qFxPLy0 eVMz4N2f+dXeQ== Received: by mail-lj1-f176.google.com with SMTP id b19so15081121ljf.8 for ; Wed, 07 Sep 2022 01:33:52 -0700 (PDT) X-Gm-Message-State: ACgBeo3IlF6DslH0Vg0Ae6Y3kWg3xmtlZUm7lmprnqdtoJRcucHHVX/r dzSAjm/v1BDyWgtzPoj3pT5CLCL6Z7kCI6HoBmU= X-Google-Smtp-Source: AA6agR74hj0e2Yy43z67LAxwm6cPBLqud8lmTIH22nLbAyyn3NNTpK3D2LwWD/CSizM3QjyieEUo4+rma6x6n9LU0R4= X-Received: by 2002:a2e:9b0e:0:b0:268:cb68:575b with SMTP id u14-20020a2e9b0e000000b00268cb68575bmr703247lji.152.1662539630107; Wed, 07 Sep 2022 01:33:50 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: "Ard Biesheuvel" Date: Wed, 7 Sep 2022 10:33:39 +0200 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH] EmbeddedPkg/PrePiMemoryAllocationLib: Add check for space on offset allocation To: Jeff Brasen Cc: devel@edk2.groups.io, quic_llindhol@quicinc.com, ardb+tianocore@kernel.org, abner.chang@hpe.com, daniel.schaefer@hpe.com Content-Type: text/plain; charset="UTF-8" On Thu, 30 Jun 2022 at 21:06, Jeff Brasen wrote: > > Update check for enough space to occur prior to alignment offset. > This prevents cases where EfiFreeMemoryTop < EfiFreeMemoryBottom. > So prior to this patch, we would - check for enough space - apply the alignment - potentially exceed the available space due to alignment padding? > Signed-off-by: Jeff Brasen This patch got mangled so I cannot apply it from the list. > --- > .../MemoryAllocationLib.c | 53 +++++++++++-------- > 1 file changed, 30 insertions(+), 23 deletions(-) > > diff --git a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c > index 78f8da5e95..1956d644c3 100644 > --- a/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c > +++ b/EmbeddedPkg/Library/PrePiMemoryAllocationLib/MemoryAllocationLib.c > @@ -38,37 +38,44 @@ AllocatePages ( > > Hob.Raw = GetHobList (); > > - // Check to see if on 4k boundary > Offset = Hob.HandoffInformationTable->EfiFreeMemoryTop & 0xFFF; > + // > + // Verify that there is sufficient memory to satisfy the allocation and padding prior to updating anything > + // > + if ((Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) - Offset) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) { > + if (Offset != 0) { > + DEBUG ((DEBUG_ERROR, "Offset applied without enough space\r\n")); > + } else { > + DEBUG ((DEBUG_ERROR, "Out of memory\r\n")); > + } > + > + ASSERT (FALSE); > + return 0; > + } > + > + // Check to see if on 4k boundary > if (Offset != 0) { > // If not aligned, make the allocation aligned. > Hob.HandoffInformationTable->EfiFreeMemoryTop -= Offset; > } > > // > - // Verify that there is sufficient memory to satisfy the allocation > + // Update the PHIT to reflect the memory usage > // > - if (Hob.HandoffInformationTable->EfiFreeMemoryTop - ((Pages * EFI_PAGE_SIZE) + sizeof (EFI_HOB_MEMORY_ALLOCATION)) < Hob.HandoffInformationTable->EfiFreeMemoryBottom) { > - return 0; > - } else { > - // > - // Update the PHIT to reflect the memory usage > - // > - Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE; > - > - // This routine used to create a memory allocation HOB a la PEI, but that's not > - // necessary for us. > - > - // > - // Create a memory allocation HOB. > - // > - BuildMemoryAllocationHob ( > - Hob.HandoffInformationTable->EfiFreeMemoryTop, > - Pages * EFI_PAGE_SIZE, > - EfiBootServicesData > - ); > - return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop; > - } > + Hob.HandoffInformationTable->EfiFreeMemoryTop -= Pages * EFI_PAGE_SIZE; > + > + // This routine used to create a memory allocation HOB a la PEI, but that's not > + // necessary for us. > + > + // > + // Create a memory allocation HOB. > + // > + BuildMemoryAllocationHob ( > + Hob.HandoffInformationTable->EfiFreeMemoryTop, > + Pages * EFI_PAGE_SIZE, > + EfiBootServicesData > + ); > + return (VOID *)(UINTN)Hob.HandoffInformationTable->EfiFreeMemoryTop; > } > > /** > -- > 2.25.1 >