public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] ShellPkg/UefiShellLevel2CommansLib: Pointer Resonse should be checked
@ 2019-08-08  0:54 Gao, Zhichao
  2019-08-08 14:50 ` [edk2-devel] " Carsey, Jaben
  0 siblings, 1 reply; 2+ messages in thread
From: Gao, Zhichao @ 2019-08-08  0:54 UTC (permalink / raw)
  To: devel; +Cc: Jaben Carsey, Ray Ni

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2049

ShellPkg\Library\UefiShellLevel2CommandsLib\Cp.c line 104 and
ShellPkg\Library\UefiShellLevel2CommandsLib\Mv.c line 640, the
pointer variable Response may be a NULL pointer. So we should
make sure that it isn't NULL before dereference it.

If Response is NULL that indicates a EFI_OUT_OF_RESOURCES
error, directly return SHELL_ABORTED.

Cc: Jaben Carsey <jaben.carsey@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Signed-off-by: Zhichao Gao <zhichao.gao@intel.com>
---
 .../Library/UefiShellLevel2CommandsLib/Cp.c   | 42 ++++++++-------
 .../Library/UefiShellLevel2CommandsLib/Mv.c   | 52 ++++++++++---------
 2 files changed, 51 insertions(+), 43 deletions(-)

diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c b/ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
index 18b05b5803..632d50229a 100644
--- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
+++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
@@ -2,7 +2,7 @@
   Main file for cp shell level 2 function.
 
   (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -101,24 +101,28 @@ CopySingleFile(
     // possibly return based on response
     //
     if (!SilentMode) {
-      switch (*(SHELL_PROMPT_RESPONSE*)Response) {
-        case ShellPromptResponseNo:
-          //
-          // return success here so we dont stop the process
-          //
-          return (SHELL_SUCCESS);
-        case ShellPromptResponseCancel:
-          *Resp = Response;
-          //
-          // indicate to stop everything
-          //
-          return (SHELL_ABORTED);
-        case ShellPromptResponseAll:
-          *Resp = Response;
-        case ShellPromptResponseYes:
-          break;
-        default:
-          return SHELL_ABORTED;
+      if (Response != NULL) {
+        switch (*(SHELL_PROMPT_RESPONSE*)Response) {
+          case ShellPromptResponseNo:
+            //
+            // return success here so we dont stop the process
+            //
+            return (SHELL_SUCCESS);
+          case ShellPromptResponseCancel:
+            *Resp = Response;
+            //
+            // indicate to stop everything
+            //
+            return (SHELL_ABORTED);
+          case ShellPromptResponseAll:
+            *Resp = Response;
+          case ShellPromptResponseYes:
+            break;
+          default:
+            return SHELL_ABORTED;
+        }
+      } else {
+        return SHELL_ABORTED;
       }
     }
   }
diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c b/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
index 8c2852d7eb..2cfa588a8c 100644
--- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
+++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
@@ -2,7 +2,7 @@
   Main file for mv shell level 2 function.
 
   (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
-  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
+  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
 **/
@@ -637,29 +637,33 @@ ValidateAndMoveFiles(
       if (Response == NULL) {
         ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel, STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle, &Response);
       }
-      switch (*(SHELL_PROMPT_RESPONSE*)Response) {
-        case ShellPromptResponseNo:
-          FreePool(Response);
-          Response = NULL;
-          continue;
-        case ShellPromptResponseCancel:
-          *Resp = Response;
-          //
-          // indicate to stop everything
-          //
-          SHELL_FREE_NON_NULL(FullCwd);
-          return (SHELL_ABORTED);
-        case ShellPromptResponseAll:
-          *Resp = Response;
-          break;
-        case ShellPromptResponseYes:
-          FreePool(Response);
-          Response = NULL;
-          break;
-        default:
-          FreePool(Response);
-          SHELL_FREE_NON_NULL(FullCwd);
-          return SHELL_ABORTED;
+      if (Response != NULL) {
+        switch (*(SHELL_PROMPT_RESPONSE*)Response) {
+          case ShellPromptResponseNo:
+            FreePool(Response);
+            Response = NULL;
+            continue;
+          case ShellPromptResponseCancel:
+            *Resp = Response;
+            //
+            // indicate to stop everything
+            //
+            SHELL_FREE_NON_NULL(FullCwd);
+            return (SHELL_ABORTED);
+          case ShellPromptResponseAll:
+            *Resp = Response;
+            break;
+          case ShellPromptResponseYes:
+            FreePool(Response);
+            Response = NULL;
+            break;
+          default:
+            FreePool(Response);
+            SHELL_FREE_NON_NULL(FullCwd);
+            return SHELL_ABORTED;
+        }
+      } else {
+        return SHELL_ABORTED;
       }
       Status = ShellDeleteFileByName(FullDestPath!=NULL? FullDestPath:DestPath);
     }
-- 
2.21.0.windows.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [edk2-devel] [PATCH] ShellPkg/UefiShellLevel2CommansLib: Pointer Resonse should be checked
  2019-08-08  0:54 [PATCH] ShellPkg/UefiShellLevel2CommansLib: Pointer Resonse should be checked Gao, Zhichao
@ 2019-08-08 14:50 ` Carsey, Jaben
  0 siblings, 0 replies; 2+ messages in thread
From: Carsey, Jaben @ 2019-08-08 14:50 UTC (permalink / raw)
  To: devel@edk2.groups.io, Gao, Zhichao; +Cc: Ni, Ray

That looks good, but I think it would be easier to just do the logic opposite.  Cant we just add the ~"if it is NULL, then return" and not have to change anything else? No real need for else in that case since there is a return statement...

Hand written pseudo-patch follows:

     if (!SilentMode) {
+        if (Response == NULL) {
+            return SHELL_ABORTED;
+        }
    switch (*(SHELL_PROMPT_RESPONSE*)Response) {

Thanks
-Jaben


> -----Original Message-----
> From: devel@edk2.groups.io [mailto:devel@edk2.groups.io] On Behalf Of
> Gao, Zhichao
> Sent: Wednesday, August 07, 2019 5:54 PM
> To: devel@edk2.groups.io
> Cc: Carsey, Jaben <jaben.carsey@intel.com>; Ni, Ray <ray.ni@intel.com>
> Subject: [edk2-devel] [PATCH] ShellPkg/UefiShellLevel2CommansLib: Pointer
> Resonse should be checked
> 
> REF: https://bugzilla.tianocore.org/show_bug.cgi?id=2049
> 
> ShellPkg\Library\UefiShellLevel2CommandsLib\Cp.c line 104 and
> ShellPkg\Library\UefiShellLevel2CommandsLib\Mv.c line 640, the
> pointer variable Response may be a NULL pointer. So we should
> make sure that it isn't NULL before dereference it.
> 
> If Response is NULL that indicates a EFI_OUT_OF_RESOURCES
> error, directly return SHELL_ABORTED.
> 
> Cc: Jaben Carsey <jaben.carsey@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> Signed-off-by: Zhichao Gao <zhichao.gao@intel.com>
> ---
>  .../Library/UefiShellLevel2CommandsLib/Cp.c   | 42 ++++++++-------
>  .../Library/UefiShellLevel2CommandsLib/Mv.c   | 52 ++++++++++---------
>  2 files changed, 51 insertions(+), 43 deletions(-)
> 
> diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
> b/ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
> index 18b05b5803..632d50229a 100644
> --- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
> +++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Cp.c
> @@ -2,7 +2,7 @@
>    Main file for cp shell level 2 function.
> 
>    (C) Copyright 2015 Hewlett-Packard Development Company, L.P.<BR>
> -  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> @@ -101,24 +101,28 @@ CopySingleFile(
>      // possibly return based on response
>      //
>      if (!SilentMode) {
> -      switch (*(SHELL_PROMPT_RESPONSE*)Response) {
> -        case ShellPromptResponseNo:
> -          //
> -          // return success here so we dont stop the process
> -          //
> -          return (SHELL_SUCCESS);
> -        case ShellPromptResponseCancel:
> -          *Resp = Response;
> -          //
> -          // indicate to stop everything
> -          //
> -          return (SHELL_ABORTED);
> -        case ShellPromptResponseAll:
> -          *Resp = Response;
> -        case ShellPromptResponseYes:
> -          break;
> -        default:
> -          return SHELL_ABORTED;
> +      if (Response != NULL) {
> +        switch (*(SHELL_PROMPT_RESPONSE*)Response) {
> +          case ShellPromptResponseNo:
> +            //
> +            // return success here so we dont stop the process
> +            //
> +            return (SHELL_SUCCESS);
> +          case ShellPromptResponseCancel:
> +            *Resp = Response;
> +            //
> +            // indicate to stop everything
> +            //
> +            return (SHELL_ABORTED);
> +          case ShellPromptResponseAll:
> +            *Resp = Response;
> +          case ShellPromptResponseYes:
> +            break;
> +          default:
> +            return SHELL_ABORTED;
> +        }
> +      } else {
> +        return SHELL_ABORTED;
>        }
>      }
>    }
> diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
> b/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
> index 8c2852d7eb..2cfa588a8c 100644
> --- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
> +++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
> @@ -2,7 +2,7 @@
>    Main file for mv shell level 2 function.
> 
>    (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
> -  Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
> +  Copyright (c) 2009 - 2019, Intel Corporation. All rights reserved.<BR>
>    SPDX-License-Identifier: BSD-2-Clause-Patent
> 
>  **/
> @@ -637,29 +637,33 @@ ValidateAndMoveFiles(
>        if (Response == NULL) {
>          ShellPromptForResponseHii(ShellPromptResponseTypeYesNoAllCancel,
> STRING_TOKEN (STR_GEN_DEST_EXIST_OVR), gShellLevel2HiiHandle,
> &Response);
>        }
> -      switch (*(SHELL_PROMPT_RESPONSE*)Response) {
> -        case ShellPromptResponseNo:
> -          FreePool(Response);
> -          Response = NULL;
> -          continue;
> -        case ShellPromptResponseCancel:
> -          *Resp = Response;
> -          //
> -          // indicate to stop everything
> -          //
> -          SHELL_FREE_NON_NULL(FullCwd);
> -          return (SHELL_ABORTED);
> -        case ShellPromptResponseAll:
> -          *Resp = Response;
> -          break;
> -        case ShellPromptResponseYes:
> -          FreePool(Response);
> -          Response = NULL;
> -          break;
> -        default:
> -          FreePool(Response);
> -          SHELL_FREE_NON_NULL(FullCwd);
> -          return SHELL_ABORTED;
> +      if (Response != NULL) {
> +        switch (*(SHELL_PROMPT_RESPONSE*)Response) {
> +          case ShellPromptResponseNo:
> +            FreePool(Response);
> +            Response = NULL;
> +            continue;
> +          case ShellPromptResponseCancel:
> +            *Resp = Response;
> +            //
> +            // indicate to stop everything
> +            //
> +            SHELL_FREE_NON_NULL(FullCwd);
> +            return (SHELL_ABORTED);
> +          case ShellPromptResponseAll:
> +            *Resp = Response;
> +            break;
> +          case ShellPromptResponseYes:
> +            FreePool(Response);
> +            Response = NULL;
> +            break;
> +          default:
> +            FreePool(Response);
> +            SHELL_FREE_NON_NULL(FullCwd);
> +            return SHELL_ABORTED;
> +        }
> +      } else {
> +        return SHELL_ABORTED;
>        }
>        Status = ShellDeleteFileByName(FullDestPath!=NULL?
> FullDestPath:DestPath);
>      }
> --
> 2.21.0.windows.1
> 
> 
> 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-08-08 14:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-08-08  0:54 [PATCH] ShellPkg/UefiShellLevel2CommansLib: Pointer Resonse should be checked Gao, Zhichao
2019-08-08 14:50 ` [edk2-devel] " Carsey, Jaben

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox