public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Heinrich Schuchardt" <xypron.glpk@gmx.de>
To: "Gao, Zhichao" <zhichao.gao@intel.com>,
	"devel@edk2.groups.io" <devel@edk2.groups.io>,
	"Carsey, Jaben" <jaben.carsey@intel.com>,
	"Ni, Ray" <ray.ni@intel.com>,
	Leif Lindholm <leif.lindholm@linaro.org>
Cc: "Gao, Liming" <liming.gao@intel.com>
Subject: Re: [edk2-devel] [PATCH 1/1] ShellPkg/CommandLib: avoid NULL derefence and memory leak
Date: Tue, 9 Apr 2019 18:37:04 +0200	[thread overview]
Message-ID: <30091abc-66a8-f110-442b-0b7cab347a54@gmx.de> (raw)
In-Reply-To: <3CE959C139B4C44DBEA1810E3AA6F9000B7C0E98@SHSMSX101.ccr.corp.intel.com>

[-- Attachment #1: Type: text/plain, Size: 1142 bytes --]

On 4/9/19 7:33 AM, Gao, Zhichao wrote:
> The patch code looks good to me.
> But while I apply this patch, it will shows "error: corrupt patch at line 24" and " error: could not build fake ancestor". Maybe you direct change the patch file and remove some blank lines.
> For Uefi BdsDxe driver, the variable "PlatfomLang" would be initialize all the time. But maybe other manufacturers may have their own Bds solution and do not set this variable. This patch makes the Shell app more compatible.
>
> Without the patch warning.
> Reviewed-by: Zhichao Gao <zhichao.gao@intel.com>

Hello Zhichao,

I am not able to reproduce the problem. I resend you the patch as
attachment.

When applying I had to use --keep-cr:

git am --keep-cr \
../patch/0001-ShellPkg-CommandLib-avoid-NULL-derefence-and-memory-.patch

I have found no way neither on Windows nor on Linux to create a patch
that applies without --keep-cr.

If you still have a problem, please, send me your global and local
gitconfig files, your .gitattributes and indicate on which operating
system you are working with which git version.

Best regards

Heinrich

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ShellPkg-CommandLib-avoid-NULL-derefence-and-memory-.patch --]
[-- Type: text/x-diff; name="0001-ShellPkg-CommandLib-avoid-NULL-derefence-and-memory-.patch", Size: 2580 bytes --]

From 9478bbcd1d8a03762924a0b4533c1b84cbfcaa57 Mon Sep 17 00:00:00 2001
From: Heinrich Schuchardt <xypron.glpk@gmx.de>
Date: Fri, 5 Apr 2019 03:06:18 +0200
Subject: [PATCH 1/1] ShellPkg/CommandLib: avoid NULL derefence and memory leak

Since TianoCore EDK2 commit d65f2cea36d1 ("ShellPkg/CommandLib: Locate
proper UnicodeCollation instance") in edk2 the UEFI Shell crashes if EFI
variable PlatformLang is not defined due to dereferencing gUnicodeCollation
gUnicodeCollation (= NULL) in ShellCommandRegisterCommandName().

Furthermore CommandInit() is leaking PlatformLang if
gUnicodeCollation != NULL.

Close the memory leak and use the first UnicodeCollation instance if
PlatfomLang is not defined.

Fixes: d65f2cea36d1 ("ShellPkg/CommandLib: Locate proper UnicodeCollation
instance")
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 .../UefiShellCommandLib/UefiShellCommandLib.c | 20 +++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
index ddc4bb1567..e60279e5ac 100644
--- a/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
+++ b/ShellPkg/Library/UefiShellCommandLib/UefiShellCommandLib.c
@@ -80,12 +80,10 @@ CommandInit(
   EFI_STATUS                      Status;
   CHAR8                           *PlatformLang;
 
-  GetEfiGlobalVariable2 (EFI_PLATFORM_LANG_VARIABLE_NAME, (VOID**)&PlatformLang, NULL);
-  if (PlatformLang == NULL) {
-    return EFI_UNSUPPORTED;
-  }
-
   if (gUnicodeCollation == NULL) {
+
+    GetEfiGlobalVariable2 (EFI_PLATFORM_LANG_VARIABLE_NAME, (VOID**)&PlatformLang, NULL);
+
     Status = gBS->LocateHandleBuffer (
                     ByProtocol,
                     &gEfiUnicodeCollation2ProtocolGuid,
@@ -113,6 +111,14 @@ CommandInit(
         continue;
       }
 
+      //
+      // Without clue provided use the first Unicode Collation2 protocol.
+      //
+      if (PlatformLang == NULL) {
+        gUnicodeCollation = Uc;
+        break;
+      }
+
       //
       // Find the best matching matching language from the supported languages
       // of Unicode Collation2 protocol.
@@ -132,7 +138,9 @@ CommandInit(
     if (Handles != NULL) {
       FreePool (Handles);
     }
-    FreePool (PlatformLang);
+    if (PlatformLang != NULL) {
+      FreePool (PlatformLang);
+    }
   }
 
   return (gUnicodeCollation == NULL) ? EFI_UNSUPPORTED : EFI_SUCCESS;
-- 
2.20.1


  parent reply	other threads:[~2019-04-09 16:37 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-05  1:29 [PATCH 1/1] ShellPkg/CommandLib: avoid NULL derefence and memory leak Heinrich Schuchardt
2019-04-09  5:33 ` [edk2-devel] " Gao, Zhichao
2019-04-09  6:03   ` Heinrich Schuchardt
2019-04-09 16:37   ` Heinrich Schuchardt [this message]
2019-04-10  0:44     ` Gao, Zhichao

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=30091abc-66a8-f110-442b-0b7cab347a54@gmx.de \
    --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