public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Wu, Hao A" <hao.a.wu@intel.com>
To: "Chen, Chen A" <chen.a.chen@intel.com>,
	"edk2-devel@lists.01.org" <edk2-devel@lists.01.org>
Cc: "Gao, Liming" <liming.gao@intel.com>,
	"Zhang, Chao B" <chao.b.zhang@intel.com>
Subject: Re: [PATCH] MdeModulePkg/CapsuleApp: Fix memory leak issue.
Date: Mon, 11 Feb 2019 12:58:48 +0000	[thread overview]
Message-ID: <B80AF82E9BFB8E4FBD8C89DA810C6A093C895CB1@SHSMSX104.ccr.corp.intel.com> (raw)
In-Reply-To: <20190211061654.53812-1-chen.a.chen@intel.com>

> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Chen
> A Chen
> Sent: Monday, February 11, 2019 2:17 PM
> To: edk2-devel@lists.01.org
> Cc: Wu, Hao A; Gao, Liming; Zhang, Chao B
> Subject: [edk2] [PATCH] MdeModulePkg/CapsuleApp: Fix memory leak issue.
> 
> This issue is caused by FileInfoBuffer variable. This is a pointer array
> and each elements also pointer to a memory buffer that is allocated and
> returned by AllocateCopyPool function.
> 
> Cc: Jian J Wang <jian.j.wang@intel.com>
> Cc: Hao Wu <hao.a.wu@intel.com>
> Cc: Zhang Chao B <chao.b.zhang@intel.com>
> Cc: Liming Gao <liming.gao@intel.com>
> Contributed-under: TianoCore Contribution Agreement 1.1
> Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
> ---
>  MdeModulePkg/Application/CapsuleApp/CapsuleDump.c | 81
> ++++++++++++++++-------
>  1 file changed, 56 insertions(+), 25 deletions(-)
> 
> diff --git a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> index 7bef5a1378..00cf45d66a 100644
> --- a/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> +++ b/MdeModulePkg/Application/CapsuleApp/CapsuleDump.c
> @@ -806,48 +806,69 @@ DumpCapsuleFromDisk (
>    Status = Fs->OpenVolume (Fs, &Root);
>    if (EFI_ERROR (Status)) {
>      Print (L"Cannot open volume. Status = %r\n", Status);
> -    return EFI_NOT_FOUND;
> +    goto Done;
>    }
> 
>    Status = Root->Open (Root, &DirHandle, EFI_CAPSULE_FILE_DIRECTORY,
> EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE , 0);
>    if (EFI_ERROR (Status)) {
>      Print (L"Cannot open %s. Status = %r\n", EFI_CAPSULE_FILE_DIRECTORY,
> Status);
> -    return EFI_NOT_FOUND;
> +    goto Done;
>    }
> 
>    //
>    // Get file count first
>    //
> -  for ( Status = FileHandleFindFirstFile (DirHandle, &FileInfo)
> -      ; !EFI_ERROR(Status) && !NoFile
> -      ; Status = FileHandleFindNextFile (DirHandle, FileInfo, &NoFile)
> -     ){
> -    if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) == 0) {
> -      continue;
> +  do {
> +    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
> +    if (EFI_ERROR (Status) || FileInfo == NULL) {
> +      Print (L"Get File Info Fail. Status = %r\n", Status);
> +      goto Done;
>      }
> -    FileCount++;
> -  }
> +
> +    if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) != 0) {
> +      FileCount++;
> +    }
> +
> +    Status = FileHandleFindNextFile (DirHandle, FileInfo, &NoFile);
> +    if (EFI_ERROR (Status)) {
> +      Print (L"Get Next File Fail. Status = %r\n", Status);
> +      goto Done;
> +    }
> +  } while (!NoFile);
> 
>    if (FileCount == 0) {
>      Print (L"Error: No capsule file found!\n");
> -    return EFI_NOT_FOUND;
> +    Status = EFI_NOT_FOUND;
> +    goto Done;
>    }
> 
>    FileInfoBuffer = AllocatePool (sizeof(FileInfo) * FileCount);
> +  if (FileInfoBuffer == NULL) {
> +    Status = EFI_OUT_OF_RESOURCES;
> +    goto Done;
> +  }
>    NoFile = FALSE;
> 
>    //
>    // Get all file info
>    //
> -  for ( Status = FileHandleFindFirstFile (DirHandle, &FileInfo)
> -      ; !EFI_ERROR (Status) && !NoFile
> -      ; Status = FileHandleFindNextFile (DirHandle, FileInfo, &NoFile)
> -     ){
> -    if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) == 0) {
> -      continue;
> +  do {
> +    Status = FileHandleFindFirstFile (DirHandle, &FileInfo);
> +    if (EFI_ERROR (Status) || FileInfo == NULL) {
> +      Print (L"Get File Info Fail. Status = %r\n", Status);
> +      goto Done;
>      }
> -    FileInfoBuffer[Index++] = AllocateCopyPool ((UINTN)FileInfo->Size, FileInfo);
> -  }
> +
> +    if ((FileInfo->Attribute & (EFI_FILE_SYSTEM | EFI_FILE_ARCHIVE)) != 0) {
> +      FileInfoBuffer[Index++] = AllocateCopyPool ((UINTN)FileInfo->Size, FileInfo);

An error handling may be needed here as well.

Also, if memory allocation does fail here during the 'do-while' loop,
elements in array 'FileInfoBuffer' may not all have valid values.

For this case, the cleanup under tag 'Done' may not be proper.

Best Regards,
Hao Wu

> +    }
> +
> +    Status = FileHandleFindNextFile (DirHandle, FileInfo, &NoFile);
> +    if (EFI_ERROR (Status)) {
> +      Print (L"Get Next File Fail. Status = %r\n", Status);
> +      goto Done;
> +    }
> +  } while (!NoFile);
> 
>    //
>    // Sort FileInfoBuffer by alphabet order
> @@ -866,7 +887,8 @@ DumpCapsuleFromDisk (
>    }
> 
>    if (!DumpCapsuleInfo) {
> -    return EFI_SUCCESS;
> +    Status = EFI_SUCCESS;
> +    goto Done;
>    }
> 
>    Print(L"The infomation of the capsules:\n");
> @@ -875,19 +897,20 @@ DumpCapsuleFromDisk (
>      FileHandle = NULL;
>      Status = DirHandle->Open (DirHandle, &FileHandle, FileInfoBuffer[Index]-
> >FileName, EFI_FILE_MODE_READ, 0);
>      if (EFI_ERROR (Status)) {
> -      break;
> +      goto Done;
>      }
> 
>      Status = FileHandleGetSize (FileHandle, (UINT64 *) &FileSize);
>      if (EFI_ERROR (Status)) {
>        Print (L"Cannot read file %s. Status = %r\n", FileInfoBuffer[Index]-
> >FileName, Status);
>        FileHandleClose (FileHandle);
> -      return Status;
> +      goto Done;
>      }
> 
>      FileBuffer = AllocatePool (FileSize);
>      if (FileBuffer == NULL) {
> -      return RETURN_OUT_OF_RESOURCES;
> +      Status = EFI_OUT_OF_RESOURCES;
> +      goto Done;
>      }
> 
>      Status = FileHandleRead (FileHandle, &FileSize, FileBuffer);
> @@ -895,7 +918,7 @@ DumpCapsuleFromDisk (
>        Print (L"Cannot read file %s. Status = %r\n", FileInfoBuffer[Index]-
> >FileName, Status);
>        FreePool (FileBuffer);
>        FileHandleClose (FileHandle);
> -      return Status;
> +      goto Done;
>      }
> 
>      Print (L"**************************\n");
> @@ -906,7 +929,15 @@ DumpCapsuleFromDisk (
>      FreePool (FileBuffer);
>    }
> 
> -  return EFI_SUCCESS;
> +Done:
> +  if (FileInfoBuffer != NULL) {
> +    for (Index = 0; Index < FileCount; Index++) {
> +      FreePool (FileInfoBuffer[Index]);
> +    }
> +    FreePool (FileInfoBuffer);
> +  }
> +
> +  return Status;
>  }
> 
>  /**
> --
> 2.16.2.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


  reply	other threads:[~2019-02-11 12:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-11  6:16 [PATCH] MdeModulePkg/CapsuleApp: Fix memory leak issue Chen A Chen
2019-02-11 12:58 ` Wu, Hao A [this message]
2019-02-11 13:56 ` Zhang, Chao B

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=B80AF82E9BFB8E4FBD8C89DA810C6A093C895CB1@SHSMSX104.ccr.corp.intel.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