From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 3A199D8107F for ; Mon, 6 Nov 2023 07:53:12 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=Ya9fiEL5f+jaOqYR08gL/K94AoxkL7fz9AF/XGtwMzg=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1699257191; v=1; b=YqoUrUJpNCkhIuf2d75bkEqRx5hW9WiiggaRpuv5fBlo+2NQVd686X8HlmwjiCPqeA95dd5h YBdpuP0E9rLff8+wwZE8vxqSVyA9vWWeLWrJs5Aom6qz3/sjMO5olhNCBWM1GHM4iEvfZebKES7 uYcGsrNLqmUiA3efxyC30VX4= X-Received: by 127.0.0.2 with SMTP id knwAYY7687511xEy4MC0BRog; Sun, 05 Nov 2023 23:53:11 -0800 X-Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web11.49168.1699257184993246575 for ; Sun, 05 Nov 2023 23:53:11 -0800 X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="453535776" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="453535776" X-Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Nov 2023 23:53:11 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10885"; a="885824606" X-IronPort-AV: E=Sophos;i="6.03,280,1694761200"; d="scan'208";a="885824606" X-Received: from shpfwdbuild003.ccr.corp.intel.com ([10.239.56.82]) by orsmga004.jf.intel.com with ESMTP; 05 Nov 2023 23:53:08 -0800 From: "Xu, Wei6" To: devel@edk2.groups.io Cc: Wei6 Xu , Laszlo Ersek , Ard Biesheuvel , Sami Mujawar , Ray Ni Subject: [edk2-devel] [PATCH v4 2/4] StandaloneMmPkg/Core: Fix potential memory leak issue Date: Mon, 6 Nov 2023 15:52:57 +0800 Message-Id: <0fc3e43cd76b1893282f7152faf1d330be9de02c.1699253390.git.wei6.xu@intel.com> In-Reply-To: References: MIME-Version: 1.0 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 Reply-To: devel@edk2.groups.io,wei6.xu@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: XQ345CzAcrzhztCg25nWIZwTx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=YqoUrUJp; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none) 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 Cc: Ard Biesheuvel Cc: Sami Mujawar Cc: Ray Ni Signed-off-by: Wei6 Xu --- 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; } -- 2.29.2.windows.2 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#110738): https://edk2.groups.io/g/devel/message/110738 Mute This Topic: https://groups.io/mt/102416000/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-