public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Eric Jin" <eric.jin@intel.com>
To: Heinrich Schuchardt <xypron.glpk@gmx.de>,
	EDK II Development <devel@edk2.groups.io>
Cc: Supreeth Venkatesh <supreeth.venkatesh@arm.com>,
	Stephano Cetola <stephano.cetola@linux.intel.com>
Subject: Re: [edk2-test] [PATCH 1/1] uefi-sct/SctPkg: buffer overflow in NotifyFunctionTplEx()
Date: Tue, 10 Sep 2019 05:31:14 +0000	[thread overview]
Message-ID: <DA72DC7456565B47808A57108259571F63811A08@SHSMSX103.ccr.corp.intel.com> (raw)
In-Reply-To: <20190903132414.14662-1-xypron.glpk@gmx.de>

Heinrich,

Good catch. 
How about to check the notify order before the operation to close all the events created and Free the pages ? 
It doesn't impact the result because the patch already enhances the index check against the buffer limits. But it will ensure correct execution sequence in the sub3 even the Notification change later. 

With that:  Reviewed-by: Eric Jin <eric.jin@intel.com>

Best Regards
Eric

-----Original Message-----
From: Heinrich Schuchardt <xypron.glpk@gmx.de> 
Sent: Tuesday, September 3, 2019 9:24 PM
To: EDK II Development <devel@edk2.groups.io>
Cc: Jin, Eric <eric.jin@intel.com>; Supreeth Venkatesh <supreeth.venkatesh@arm.com>; Stephano Cetola <stephano.cetola@linux.intel.com>; Heinrich Schuchardt <xypron.glpk@gmx.de>
Subject: [edk2-test] [PATCH 1/1] uefi-sct/SctPkg: buffer overflow in NotifyFunctionTplEx()

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

CreateEventEx() may lead to a change in the memory map causing an EFI_EVENT_GROUP_MEMORY_MAP_CHANGE. So in BBTestCreateEventEx_Func_Sub3() we should only check for events triggered after the events have been set up.

Among other changes commit c093702f98ad (""uefi-sct/SctPkg:Fix flaw in
BBTestCreateEventEx_Func_Sub3) tried to adjust the event recording logic in
NotifyFunctionTplEx() to account for this.

The commit did not consider that CloseEvent() will release memory and equally lead to EFI_EVENT_GROUP_MEMORY_MAP_CHANGE. NotifyFunctionTplEx() does not check the limits of the buffer. So a buffer overrun occurs in this case.

The easiest way to account for memory map changes by CreateEventEx() is to initialize the event invocation records after setting up the events.

In function NotifyFunctionTplEx() check the index against the buffer limits. Stop recording after MAX_TEST_EVENT_NUM events.

Fixes: c093702f98ad (""uefi-sct/SctPkg:Fix flaw in BBTestCreateEventEx_Func_Sub3)
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 ...rTaskPriorityServicesBBTestCreateEventEx.c | 19 +++++--
 .../BlackBoxTest/Support.c                    | 55 +++++--------------
 2 files changed, 27 insertions(+), 47 deletions(-)

diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/EventTimerTaskPriorityServices/BlackBoxTest/EventTimerTaskPriorityServicesBBTestCreateEventEx.c b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/EventTimerTaskPriorityServices/BlackBoxTest/EventTimerTaskPriorityServicesBBTestCreateEventEx.c
index 4a8e44e2..40af6c07 100644
--- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/EventTimerTaskPriorityServices/BlackBoxTest/EventTimerTaskPriorityServicesBBTestCreateEventEx.c
+++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/EventTimerTaskPrior
+++ ityServices/BlackBoxTest/EventTimerTaskPriorityServicesBBTestCreateE
+++ ventEx.c
@@ -918,12 +918,11 @@ BBTestCreateEventEx_Func_Sub3 (
   UINTN               Buffer[MAX_TEST_EVENT_NUM + MAX_TEST_EVENT_NUM*2];    //-  // Initialize Buffer as SIGNAL_CONTEXT+  // Initialize the event index. The event invocation records will be+  // initialized later.
   //   for (Index = 0; Index < MAX_TEST_EVENT_NUM; Index ++) {     Buffer[Index] = Index;-    Buffer[Index + MAX_TEST_EVENT_NUM + Index] = (UINTN)(SIGNAL_CONTEXT);-    Buffer[Index + MAX_TEST_EVENT_NUM + 1 + Index] = (UINTN)(SIGNAL_CONTEXT);   }    //@@ -976,7 +975,17 @@ BBTestCreateEventEx_Func_Sub3 (
     gtBS->CloseEvent (Event[1]);     return Status;   }-  ++  //+  // CreateEventEx() may lead to a change in the memory map and trigger+  // EFI_EVENT_GROUP_MEMORY_MAP_CHANGE itself. So initialize the event+  // invocation records after creating the events.+  //+  for (Index = 0; Index < MAX_TEST_EVENT_NUM; Index ++) {+    Buffer[Index + MAX_TEST_EVENT_NUM + Index] = (UINTN)(SIGNAL_CONTEXT);+    Buffer[Index + MAX_TEST_EVENT_NUM + 1 + Index] = (UINTN)(SIGNAL_CONTEXT);+  }+   //   // Call AllocatePage to change the memorymap   //@@ -1035,4 +1044,4 @@ BBTestCreateEventEx_Func_Sub3 (
   //   return EFI_SUCCESS; }-#endif
\ No newline at end of file
+#endif
diff --git a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/EventTimerTaskPriorityServices/BlackBoxTest/Support.c b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/EventTimerTaskPriorityServices/BlackBoxTest/Support.c
index c702f84d..0c900a3e 100644
--- a/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/EventTimerTaskPriorityServices/BlackBoxTest/Support.c
+++ b/uefi-sct/SctPkg/TestCase/UEFI/EFI/BootServices/EventTimerTaskPrior
+++ ityServices/BlackBoxTest/Support.c
@@ -58,58 +58,29 @@ NotifyFunctionTplEx(
   EFI_TPL   OldTpl;   UINTN     EventIndex;   UINTN     Index;-  +   if (Context != NULL) {     Buffer = Context;      EventIndex = Buffer[0];      //-    // The special code check for the BBTestCreateEventEx_Func_Sub3-    // Besides AllocatePages(), CreateEventEx() may trigger the memorymap-    // change when it is out of resource in memory pool-    // Use SIGNAL_CONTEXT to block possible enter triggered by CreateEventEx-    //-    if (EventIndex != 2 && Buffer[4] == (UINTN)(SIGNAL_CONTEXT))-      return;--    //-    // It is the code execution path as expect-    // The overall layout buffer as below-    // Buffer[0] [1] [2] store 1st/2nd/3rd event index (start from 0)-    // Buffer[3] [5] [7] store the index of event notified-    // Buffer[4] [6] [8] store the tpl of notification function of 1st/2nd/3rd event notified+    // The event's context is offset by EventIndex from the true buffer start.+    // Skip over the MAX_TEST_EVENT_NUM leading index entries.+    // A maximum of MAX_TEST_EVENT_NUM events can be recorded.     //-    // since 3rd event is created at notify tpl, 1nd/2rd event at callback-    // EventIndex should be 2 here for the first enter-    // Because Context points to Buffer[2] and value(EventIndex) is 2-    // To initial the Buffer to 0xFF-    //--    if (EventIndex == 2 && Buffer[1] == (UINTN)(SIGNAL_CONTEXT)) {-      for (Index=1; Index<MAX_TEST_EVENT_NUM*2+1; Index++) {-        Buffer[Index] = (UINTN)(0xFF);+    for (Index = MAX_TEST_EVENT_NUM-EventIndex;+         Index < 3*MAX_TEST_EVENT_NUM-EventIndex; Index += 2) {+      if (Buffer[Index] == (UINTN)(SIGNAL_CONTEXT)) {+        OldTpl = gtBS->RaiseTPL (TPL_HIGH_LEVEL);+        gtBS->RestoreTPL (OldTpl);++        Buffer[Index] = EventIndex;+        Buffer[Index+1] = OldTpl;+        return;       }     }--    Index = 3-EventIndex;--    while (1) {     -      if (Buffer[Index] == (UINTN)(0xFF)) {-        break;-      } else {-        Index += 2;-      }-    }-    -    OldTpl = gtBS->RaiseTPL (TPL_HIGH_LEVEL);-    gtBS->RestoreTPL (OldTpl);--    Buffer[Index] = EventIndex;-    Buffer[Index+1] = OldTpl;   }--  return; } #endif -- 
2.23.0.rc1


      reply	other threads:[~2019-09-10  5:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-03 13:24 [edk2-test] [PATCH 1/1] uefi-sct/SctPkg: buffer overflow in NotifyFunctionTplEx() Heinrich Schuchardt
2019-09-10  5:31 ` Eric Jin [this message]

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=DA72DC7456565B47808A57108259571F63811A08@SHSMSX103.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