From: "Laszlo Ersek" <lersek@redhat.com>
To: devel@edk2.groups.io, wei6.xu@intel.com
Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>,
Sami Mujawar <sami.mujawar@arm.com>, Ray Ni <ray.ni@intel.com>
Subject: Re: [edk2-devel] [PATCH v4 2/4] StandaloneMmPkg/Core: Fix potential memory leak issue
Date: Wed, 8 Nov 2023 21:00:05 +0100 [thread overview]
Message-ID: <abb096c1-1acf-4d16-9065-092e4d28b4fc@redhat.com> (raw)
In-Reply-To: <0fc3e43cd76b1893282f7152faf1d330be9de02c.1699253390.git.wei6.xu@intel.com>
On 11/6/23 08:52, Xu, Wei6 wrote:
> In MmCoreFfsFindMmDriver(),
> - ScratchBuffer is not freed in the error return path that DstBuffer page
> allocation fails. Free ScratchBuffer before return with error.
> - If the decoded buffer is identical to the data in InputSection,
> ExtractGuidedSectionDecode() will change the value of DstBuffer rather
> than changing the contents of the buffer that DstBuffer points at, in
> which case freeing DstBuffer is wrong. Introduce a local variable
> AllocatedDstBuffer for buffer free, free AllocatedDstBuffer immediately
> if it is not used.
>
> Cc: Laszlo Ersek <lersek@redhat.com>
> Cc: Ard Biesheuvel <ardb+tianocore@kernel.org>
> Cc: Sami Mujawar <sami.mujawar@arm.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Signed-off-by: Wei6 Xu <wei6.xu@intel.com>
> ---
> StandaloneMmPkg/Core/FwVol.c | 31 ++++++++++++++++++++++---------
> 1 file changed, 22 insertions(+), 9 deletions(-)
>
> diff --git a/StandaloneMmPkg/Core/FwVol.c b/StandaloneMmPkg/Core/FwVol.c
> index e1e20ffd14ac..c3054ef751ed 100644
> --- a/StandaloneMmPkg/Core/FwVol.c
> +++ b/StandaloneMmPkg/Core/FwVol.c
> @@ -84,6 +84,7 @@ MmCoreFfsFindMmDriver (
> UINT32 DstBufferSize;
> VOID *ScratchBuffer;
> UINT32 ScratchBufferSize;
> + VOID *AllocatedDstBuffer;
> VOID *DstBuffer;
> UINT16 SectionAttribute;
> UINT32 AuthenticationStatus;
> @@ -148,25 +149,35 @@ MmCoreFfsFindMmDriver (
> //
> // Allocate destination buffer, extra one page for adjustment
> //
> - DstBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
> - if (DstBuffer == NULL) {
> + AllocatedDstBuffer = (VOID *)(UINTN)AllocatePages (EFI_SIZE_TO_PAGES (DstBufferSize));
> + if (AllocatedDstBuffer == NULL) {
> + FreePages (ScratchBuffer, EFI_SIZE_TO_PAGES (ScratchBufferSize));
> return EFI_OUT_OF_RESOURCES;
> }
>
> //
> // Call decompress function
> //
> - Status = ExtractGuidedSectionDecode (
> - Section,
> - &DstBuffer,
> - ScratchBuffer,
> - &AuthenticationStatus
> - );
> + DstBuffer = AllocatedDstBuffer;
> + Status = ExtractGuidedSectionDecode (
> + Section,
> + &DstBuffer,
> + ScratchBuffer,
> + &AuthenticationStatus
> + );
> FreePages (ScratchBuffer, EFI_SIZE_TO_PAGES (ScratchBufferSize));
> if (EFI_ERROR (Status)) {
> goto FreeDstBuffer;
> }
>
> + //
> + // Free allocated DstBuffer if it is not used
> + //
> + if (DstBuffer != AllocatedDstBuffer) {
> + FreePages (AllocatedDstBuffer, EFI_SIZE_TO_PAGES (DstBufferSize));
> + AllocatedDstBuffer = NULL;
> + }
> +
> DEBUG ((
> DEBUG_INFO,
> "Processing compressed firmware volume (AuthenticationStatus == %x)\n",
> @@ -210,7 +221,9 @@ MmCoreFfsFindMmDriver (
> return EFI_SUCCESS;
>
> FreeDstBuffer:
> - FreePages (DstBuffer, EFI_SIZE_TO_PAGES (DstBufferSize));
> + if (AllocatedDstBuffer != NULL) {
> + FreePages (AllocatedDstBuffer, EFI_SIZE_TO_PAGES (DstBufferSize));
> + }
>
> return Status;
> }
Right, if AllocatedDstBuffer is needed, then we free it only upon error;
otherwise, we free it early on, so that it's released upon both error
and success.
Reviewed-by: Laszlo Ersek <lersek@redhat.com>
-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#110922): https://edk2.groups.io/g/devel/message/110922
Mute This Topic: https://groups.io/mt/102416000/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/1913456212/xyzzy [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-
next prev parent reply other threads:[~2023-11-08 20:00 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-06 7:52 [edk2-devel] [PATCH v4 0/4] StandaloneMmCore finds drivers in uncompressed inner fv Xu, Wei6
2023-11-06 7:52 ` [edk2-devel] [PATCH v4 1/4] StandaloneMmPkg/Core: Limit FwVol encapsulation section recursion Xu, Wei6
2023-11-08 19:47 ` Laszlo Ersek
2023-11-06 7:52 ` [edk2-devel] [PATCH v4 2/4] StandaloneMmPkg/Core: Fix potential memory leak issue Xu, Wei6
2023-11-08 20:00 ` Laszlo Ersek [this message]
2023-11-06 7:52 ` [edk2-devel] [PATCH v4 3/4] StandaloneMmPkg/Core: Fix issue that offset calculation might be wrong Xu, Wei6
2023-11-08 20:11 ` Laszlo Ersek
2023-11-06 7:52 ` [edk2-devel] [PATCH v4 4/4] StandaloneMmPkg/Core: Fix the failure to find uncompressed inner FV Xu, Wei6
2023-11-08 20:15 ` Laszlo Ersek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=abb096c1-1acf-4d16-9065-092e4d28b4fc@redhat.com \
--to=devel@edk2.groups.io \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox