public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH] ShellPkg/MV: Fix MV to deny moving parent of current directory
@ 2016-11-23  2:58 Ruiyu Ni
  2016-12-01 16:12 ` Carsey, Jaben
  0 siblings, 1 reply; 2+ messages in thread
From: Ruiyu Ni @ 2016-11-23  2:58 UTC (permalink / raw)
  To: edk2-devel; +Cc: Chen A Chen, Jaben Carsey

From: Chen A Chen <chen.a.chen@intel.com>

When user types "mv -r fs0:\A\ fs1:\" under directory
"fs0:\A\B\", MV command should deny such movement.

The patch fixes the above issue.
It also denies moving current directory.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
Cc: Jaben Carsey <jaben.carsey@intel.com>
---
 ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c | 81 ++++++++++++++++++++++--
 1 file changed, 75 insertions(+), 6 deletions(-)

diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c b/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
index efaaeb2..71e4336 100644
--- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
+++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
@@ -58,6 +58,73 @@ IsBetweenFileSystem(
 }
 
 /**
+  function to determine if SrcPath is valid to mv.
+
+  if SrcPath equal CWD then it's invalid.
+  if SrcPath is the parent path of CWD then it's invalid.
+  is SrcPath is NULL return FALSE.
+
+  if CwdPath is NULL then ASSERT()
+
+  @param SrcPath  [in]    The source path.
+  @param CwdPath  [in]    The current working directory.
+
+  @retval TRUE            The source path is valid.
+  @retval FALSE           The source path is invalid.
+**/
+BOOLEAN
+IsSoucePathValid(
+  IN CONST CHAR16*  SrcPath,
+  IN CONST CHAR16*  CwdPath
+  )
+{
+  CHAR16* SrcPathBuffer;
+  CHAR16* CwdPathBuffer;
+  BOOLEAN Ret;
+
+  ASSERT (CwdPath != NULL);
+  if (SrcPath == NULL) {
+    return FALSE;
+  }
+
+  Ret = TRUE;
+
+  SrcPathBuffer = AllocateCopyPool (StrSize (SrcPath), SrcPath);
+  if (SrcPathBuffer == NULL) {
+    return FALSE;
+  }
+
+  CwdPathBuffer = AllocateCopyPool (StrSize (CwdPath), CwdPath);
+  if (CwdPathBuffer == NULL) {
+    FreePool(SrcPathBuffer);
+    return FALSE;
+  }
+
+  gUnicodeCollation->StrUpr (gUnicodeCollation, SrcPathBuffer);
+  gUnicodeCollation->StrUpr (gUnicodeCollation, CwdPathBuffer);
+
+  if (SrcPathBuffer[StrLen (SrcPathBuffer) -1 ] == L'\\') {
+    SrcPathBuffer[StrLen (SrcPathBuffer) - 1] = CHAR_NULL;
+  }
+
+  if (CwdPathBuffer[StrLen (CwdPathBuffer) - 1] == L'\\') {
+    CwdPathBuffer[StrLen (CwdPathBuffer) - 1] = CHAR_NULL;
+  }
+
+  if (StrCmp (CwdPathBuffer, SrcPathBuffer) == 0 ||
+      ((StrStr (CwdPathBuffer, SrcPathBuffer) == CwdPathBuffer) &&
+       (CwdPathBuffer[StrLen (SrcPathBuffer)] == L'\\'))
+     ) {
+    Ret = FALSE;
+  }
+
+  FreePool (SrcPathBuffer);
+  FreePool (CwdPathBuffer);
+
+  return Ret;
+}
+
+/**
   Function to validate that moving a specific file (FileName) to a specific
   location (DestPath) is valid.
 
@@ -90,12 +157,14 @@ IsValidMove(
   CHAR16  *DestPathCopy;
   CHAR16  *DestPathWalker;
 
-  if (Cwd != NULL && StrCmp(SourcePath, Cwd) == 0) {
-    //
-    // Invalid move
-    //
-    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_CWD), gShellLevel2HiiHandle);
-    return (FALSE);
+  if ((Cwd != NULL) && ((Attribute & EFI_FILE_DIRECTORY) == EFI_FILE_DIRECTORY)) {
+    if (!IsSoucePathValid (SourcePath, Cwd)) {
+      //
+      // Invalid move
+      //
+      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_MV_INV_CWD), gShellLevel2HiiHandle);
+      return FALSE;
+    }
   }
 
   //
-- 
2.9.0.windows.1



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

* Re: [PATCH] ShellPkg/MV: Fix MV to deny moving parent of current directory
  2016-11-23  2:58 [PATCH] ShellPkg/MV: Fix MV to deny moving parent of current directory Ruiyu Ni
@ 2016-12-01 16:12 ` Carsey, Jaben
  0 siblings, 0 replies; 2+ messages in thread
From: Carsey, Jaben @ 2016-12-01 16:12 UTC (permalink / raw)
  To: Ni, Ruiyu, edk2-devel@lists.01.org; +Cc: Chen, Chen A, Carsey, Jaben

Reviewed-by: Jaben Carsey <jaben.carsey@intel.com>


> -----Original Message-----
> From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
> Ruiyu Ni
> Sent: Tuesday, November 22, 2016 6:58 PM
> To: edk2-devel@lists.01.org
> Cc: Carsey, Jaben <jaben.carsey@intel.com>; Chen, Chen A
> <chen.a.chen@intel.com>
> Subject: [edk2] [PATCH] ShellPkg/MV: Fix MV to deny moving parent of
> current directory
> Importance: High
> 
> From: Chen A Chen <chen.a.chen@intel.com>
> 
> When user types "mv -r fs0:\A\ fs1:\" under directory
> "fs0:\A\B\", MV command should deny such movement.
> 
> The patch fixes the above issue.
> It also denies moving current directory.
> 
> Contributed-under: TianoCore Contribution Agreement 1.0
> Signed-off-by: Chen A Chen <chen.a.chen@intel.com>
> Cc: Jaben Carsey <jaben.carsey@intel.com>
> ---
>  ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c | 81
> ++++++++++++++++++++++--
>  1 file changed, 75 insertions(+), 6 deletions(-)
> 
> diff --git a/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
> b/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
> index efaaeb2..71e4336 100644
> --- a/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
> +++ b/ShellPkg/Library/UefiShellLevel2CommandsLib/Mv.c
> @@ -58,6 +58,73 @@ IsBetweenFileSystem(
>  }
> 
>  /**
> +  function to determine if SrcPath is valid to mv.
> +
> +  if SrcPath equal CWD then it's invalid.
> +  if SrcPath is the parent path of CWD then it's invalid.
> +  is SrcPath is NULL return FALSE.
> +
> +  if CwdPath is NULL then ASSERT()
> +
> +  @param SrcPath  [in]    The source path.
> +  @param CwdPath  [in]    The current working directory.
> +
> +  @retval TRUE            The source path is valid.
> +  @retval FALSE           The source path is invalid.
> +**/
> +BOOLEAN
> +IsSoucePathValid(
> +  IN CONST CHAR16*  SrcPath,
> +  IN CONST CHAR16*  CwdPath
> +  )
> +{
> +  CHAR16* SrcPathBuffer;
> +  CHAR16* CwdPathBuffer;
> +  BOOLEAN Ret;
> +
> +  ASSERT (CwdPath != NULL);
> +  if (SrcPath == NULL) {
> +    return FALSE;
> +  }
> +
> +  Ret = TRUE;
> +
> +  SrcPathBuffer = AllocateCopyPool (StrSize (SrcPath), SrcPath);
> +  if (SrcPathBuffer == NULL) {
> +    return FALSE;
> +  }
> +
> +  CwdPathBuffer = AllocateCopyPool (StrSize (CwdPath), CwdPath);
> +  if (CwdPathBuffer == NULL) {
> +    FreePool(SrcPathBuffer);
> +    return FALSE;
> +  }
> +
> +  gUnicodeCollation->StrUpr (gUnicodeCollation, SrcPathBuffer);
> +  gUnicodeCollation->StrUpr (gUnicodeCollation, CwdPathBuffer);
> +
> +  if (SrcPathBuffer[StrLen (SrcPathBuffer) -1 ] == L'\\') {
> +    SrcPathBuffer[StrLen (SrcPathBuffer) - 1] = CHAR_NULL;
> +  }
> +
> +  if (CwdPathBuffer[StrLen (CwdPathBuffer) - 1] == L'\\') {
> +    CwdPathBuffer[StrLen (CwdPathBuffer) - 1] = CHAR_NULL;
> +  }
> +
> +  if (StrCmp (CwdPathBuffer, SrcPathBuffer) == 0 ||
> +      ((StrStr (CwdPathBuffer, SrcPathBuffer) == CwdPathBuffer) &&
> +       (CwdPathBuffer[StrLen (SrcPathBuffer)] == L'\\'))
> +     ) {
> +    Ret = FALSE;
> +  }
> +
> +  FreePool (SrcPathBuffer);
> +  FreePool (CwdPathBuffer);
> +
> +  return Ret;
> +}
> +
> +/**
>    Function to validate that moving a specific file (FileName) to a specific
>    location (DestPath) is valid.
> 
> @@ -90,12 +157,14 @@ IsValidMove(
>    CHAR16  *DestPathCopy;
>    CHAR16  *DestPathWalker;
> 
> -  if (Cwd != NULL && StrCmp(SourcePath, Cwd) == 0) {
> -    //
> -    // Invalid move
> -    //
> -    ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_MV_INV_CWD),
> gShellLevel2HiiHandle);
> -    return (FALSE);
> +  if ((Cwd != NULL) && ((Attribute & EFI_FILE_DIRECTORY) ==
> EFI_FILE_DIRECTORY)) {
> +    if (!IsSoucePathValid (SourcePath, Cwd)) {
> +      //
> +      // Invalid move
> +      //
> +      ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN(STR_MV_INV_CWD),
> gShellLevel2HiiHandle);
> +      return FALSE;
> +    }
>    }
> 
>    //
> --
> 2.9.0.windows.1
> 
> _______________________________________________
> edk2-devel mailing list
> edk2-devel@lists.01.org
> https://lists.01.org/mailman/listinfo/edk2-devel


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

end of thread, other threads:[~2016-12-01 16:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-11-23  2:58 [PATCH] ShellPkg/MV: Fix MV to deny moving parent of current directory Ruiyu Ni
2016-12-01 16:12 ` Carsey, Jaben

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