[edk2] [PATCH v2] NetworkPkg/TlsAuthConfigDxe: Close and free the file related resource

Jiaxin Wu posted 1 patch 7 years ago
Failed in applying to current master (apply log)
NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c | 49 +++++++++++++------------
1 file changed, 25 insertions(+), 24 deletions(-)
[edk2] [PATCH v2] NetworkPkg/TlsAuthConfigDxe: Close and free the file related resource
Posted by Jiaxin Wu 7 years ago
v2:
* Define one new internal function to clean the file content.

TlsAuthConfigDxe open file by FileExplorerLib. It need to close
file handler and free file related resource in some cases.
* User enrolls Cert by escape the Config page.
* The Cert is not X509 type.
* User chooses another file after he selected a file.

Cc: Zhang Chao B <chao.b.zhang@intel.com>
Cc: Ye Ting <ting.ye@intel.com>
Cc: Fu Siyuan <siyuan.fu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Wu Jiaxin <jiaxin.wu@intel.com>
---
 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c | 49 +++++++++++++------------
 1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c
index 81f7e7d..faefc72 100644
--- a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c
+++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c
@@ -294,11 +294,11 @@ ON_EXIT:
 }
 
 /**
   Delete one entry from cert database.
 
-  @param[in]    PrivateData         Module's private data.
+  @param[in]    Private             Module's private data.
   @param[in]    VariableName        The variable name of the database.
   @param[in]    VendorGuid          A unique identifier for the vendor.
   @param[in]    LabelNumber         Label number to insert opcodes.
   @param[in]    FormId              Form ID of current page.
   @param[in]    QuestionIdBase      Base question id of the cert list.
@@ -475,22 +475,27 @@ ON_EXIT:
            );
 }
 
 
 /**
-  Close an open file handle.
+  Clean the file related resource.
 
-  @param[in] FileHandle           The file handle to close.
+  @param[in]    Private             Module's private data.
 
 **/
 VOID
-CloseFile (
-  IN EFI_FILE_HANDLE   FileHandle
+CleanFileContext (
+  IN TLS_AUTH_CONFIG_PRIVATE_DATA     *Private
   )
 {
-  if (FileHandle != NULL) {
-    FileHandle->Close (FileHandle);
+  if (Private->FileContext->FHandle != NULL) {
+    Private->FileContext->FHandle->Close (Private->FileContext->FHandle);
+    Private->FileContext->FHandle = NULL;
+    if (Private->FileContext->FileName!= NULL){
+      FreePool(Private->FileContext->FileName);
+      Private->FileContext->FileName = NULL;
+    }
   }
 }
 
 /**
   Read file content into BufferPtr, the size of the allocate buffer
@@ -871,18 +876,11 @@ EnrollX509toVariable (
   if (EFI_ERROR (Status)) {
     goto ON_EXIT;
   }
 
 ON_EXIT:
-
-  CloseFile (Private->FileContext->FHandle);
-  if (Private->FileContext->FileName != NULL) {
-    FreePool(Private->FileContext->FileName);
-    Private->FileContext->FileName = NULL;
-  }
-
-  Private->FileContext->FHandle = NULL;
+  CleanFileContext (Private);
 
   if (Private->CertGuid != NULL) {
     FreePool (Private->CertGuid);
     Private->CertGuid = NULL;
   }
@@ -1559,11 +1557,12 @@ TlsAuthConfigAccessCallback (
   }
 
   HiiGetBrowserData (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, BufferSize, (UINT8 *) IfrNvData);
 
   if ((Action != EFI_BROWSER_ACTION_CHANGED) &&
-      (Action != EFI_BROWSER_ACTION_CHANGING)) {
+      (Action != EFI_BROWSER_ACTION_CHANGING) && 
+      (Action != EFI_BROWSER_ACTION_FORM_CLOSE)) {
     Status = EFI_UNSUPPORTED;
     goto EXIT;
   }
 
   if (Action == EFI_BROWSER_ACTION_CHANGING) {
@@ -1590,34 +1589,34 @@ TlsAuthConfigAccessCallback (
       // Refresh selected file.
       //
       CleanUpPage (LabelId, Private);
       break;
     case KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE:
+      //
+      // If the file is already opened, clean the file related resource first. 
+      //
+      CleanFileContext (Private);
+      
       ChooseFile( NULL, NULL, UpdateCAFromFile, &File);
       break;
 
     case KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT:
       Status = EnrollCertDatabase (Private, EFI_TLS_CA_CERTIFICATE_VARIABLE);
       if (EFI_ERROR (Status)) {
+        CleanFileContext (Private);
+
         CreatePopUp (
           EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
           &Key,
           L"ERROR: Enroll Cert Failure!",
           NULL
           );
       }
       break;
 
     case KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT:
-      if (Private->FileContext->FHandle != NULL) {
-        CloseFile (Private->FileContext->FHandle);
-        Private->FileContext->FHandle = NULL;
-        if (Private->FileContext->FileName!= NULL){
-          FreePool(Private->FileContext->FileName);
-          Private->FileContext->FileName = NULL;
-        }
-      }
+      CleanFileContext (Private);
 
       if (Private->CertGuid!= NULL) {
         FreePool (Private->CertGuid);
         Private->CertGuid = NULL;
       }
@@ -1665,10 +1664,12 @@ TlsAuthConfigAccessCallback (
       *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY;
       break;
     default:
       break;
     }
+  } else if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) {
+    CleanFileContext (Private);
   }
 
 EXIT:
 
   if (!EFI_ERROR (Status)) {
-- 
1.9.5.msysgit.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel
Re: [edk2] [PATCH v2] NetworkPkg/TlsAuthConfigDxe: Close and free the file related resource
Posted by Zhang, Chao B 6 years, 12 months ago
Reviewed-by: Chao Zhang<chao.b.zhang@intel.com>

-----Original Message-----
From: Wu, Jiaxin 
Sent: Monday, April 17, 2017 2:11 PM
To: edk2-devel@lists.01.org
Cc: Zhang, Chao B <chao.b.zhang@intel.com>; Ye, Ting <ting.ye@intel.com>; Fu, Siyuan <siyuan.fu@intel.com>; Wu, Jiaxin <jiaxin.wu@intel.com>
Subject: [PATCH v2] NetworkPkg/TlsAuthConfigDxe: Close and free the file related resource

v2:
* Define one new internal function to clean the file content.

TlsAuthConfigDxe open file by FileExplorerLib. It need to close file handler and free file related resource in some cases.
* User enrolls Cert by escape the Config page.
* The Cert is not X509 type.
* User chooses another file after he selected a file.

Cc: Zhang Chao B <chao.b.zhang@intel.com>
Cc: Ye Ting <ting.ye@intel.com>
Cc: Fu Siyuan <siyuan.fu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Wu Jiaxin <jiaxin.wu@intel.com>
---
 NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c | 49 +++++++++++++------------
 1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c
index 81f7e7d..faefc72 100644
--- a/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c
+++ b/NetworkPkg/TlsAuthConfigDxe/TlsAuthConfigImpl.c
@@ -294,11 +294,11 @@ ON_EXIT:
 }
 
 /**
   Delete one entry from cert database.
 
-  @param[in]    PrivateData         Module's private data.
+  @param[in]    Private             Module's private data.
   @param[in]    VariableName        The variable name of the database.
   @param[in]    VendorGuid          A unique identifier for the vendor.
   @param[in]    LabelNumber         Label number to insert opcodes.
   @param[in]    FormId              Form ID of current page.
   @param[in]    QuestionIdBase      Base question id of the cert list.
@@ -475,22 +475,27 @@ ON_EXIT:
            );
 }
 
 
 /**
-  Close an open file handle.
+  Clean the file related resource.
 
-  @param[in] FileHandle           The file handle to close.
+  @param[in]    Private             Module's private data.
 
 **/
 VOID
-CloseFile (
-  IN EFI_FILE_HANDLE   FileHandle
+CleanFileContext (
+  IN TLS_AUTH_CONFIG_PRIVATE_DATA     *Private
   )
 {
-  if (FileHandle != NULL) {
-    FileHandle->Close (FileHandle);
+  if (Private->FileContext->FHandle != NULL) {
+    Private->FileContext->FHandle->Close (Private->FileContext->FHandle);
+    Private->FileContext->FHandle = NULL;
+    if (Private->FileContext->FileName!= NULL){
+      FreePool(Private->FileContext->FileName);
+      Private->FileContext->FileName = NULL;
+    }
   }
 }
 
 /**
   Read file content into BufferPtr, the size of the allocate buffer @@ -871,18 +876,11 @@ EnrollX509toVariable (
   if (EFI_ERROR (Status)) {
     goto ON_EXIT;
   }
 
 ON_EXIT:
-
-  CloseFile (Private->FileContext->FHandle);
-  if (Private->FileContext->FileName != NULL) {
-    FreePool(Private->FileContext->FileName);
-    Private->FileContext->FileName = NULL;
-  }
-
-  Private->FileContext->FHandle = NULL;
+  CleanFileContext (Private);
 
   if (Private->CertGuid != NULL) {
     FreePool (Private->CertGuid);
     Private->CertGuid = NULL;
   }
@@ -1559,11 +1557,12 @@ TlsAuthConfigAccessCallback (
   }
 
   HiiGetBrowserData (&gTlsAuthConfigGuid, mTlsAuthConfigStorageName, BufferSize, (UINT8 *) IfrNvData);
 
   if ((Action != EFI_BROWSER_ACTION_CHANGED) &&
-      (Action != EFI_BROWSER_ACTION_CHANGING)) {
+      (Action != EFI_BROWSER_ACTION_CHANGING) && 
+      (Action != EFI_BROWSER_ACTION_FORM_CLOSE)) {
     Status = EFI_UNSUPPORTED;
     goto EXIT;
   }
 
   if (Action == EFI_BROWSER_ACTION_CHANGING) { @@ -1590,34 +1589,34 @@ TlsAuthConfigAccessCallback (
       // Refresh selected file.
       //
       CleanUpPage (LabelId, Private);
       break;
     case KEY_TLS_AUTH_CONFIG_ENROLL_CERT_FROM_FILE:
+      //
+      // If the file is already opened, clean the file related resource first. 
+      //
+      CleanFileContext (Private);
+      
       ChooseFile( NULL, NULL, UpdateCAFromFile, &File);
       break;
 
     case KEY_TLS_AUTH_CONFIG_VALUE_SAVE_AND_EXIT:
       Status = EnrollCertDatabase (Private, EFI_TLS_CA_CERTIFICATE_VARIABLE);
       if (EFI_ERROR (Status)) {
+        CleanFileContext (Private);
+
         CreatePopUp (
           EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE,
           &Key,
           L"ERROR: Enroll Cert Failure!",
           NULL
           );
       }
       break;
 
     case KEY_TLS_AUTH_CONFIG_VALUE_NO_SAVE_AND_EXIT:
-      if (Private->FileContext->FHandle != NULL) {
-        CloseFile (Private->FileContext->FHandle);
-        Private->FileContext->FHandle = NULL;
-        if (Private->FileContext->FileName!= NULL){
-          FreePool(Private->FileContext->FileName);
-          Private->FileContext->FileName = NULL;
-        }
-      }
+      CleanFileContext (Private);
 
       if (Private->CertGuid!= NULL) {
         FreePool (Private->CertGuid);
         Private->CertGuid = NULL;
       }
@@ -1665,10 +1664,12 @@ TlsAuthConfigAccessCallback (
       *ActionRequest = EFI_BROWSER_ACTION_REQUEST_FORM_APPLY;
       break;
     default:
       break;
     }
+  } else if (Action == EFI_BROWSER_ACTION_FORM_CLOSE) {
+    CleanFileContext (Private);
   }
 
 EXIT:
 
   if (!EFI_ERROR (Status)) {
--
1.9.5.msysgit.1

_______________________________________________
edk2-devel mailing list
edk2-devel@lists.01.org
https://lists.01.org/mailman/listinfo/edk2-devel