From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout01.posteo.de (mout01.posteo.de [185.67.36.65]) by mx.groups.io with SMTP id smtpd.web10.13763.1628451633813222031 for ; Sun, 08 Aug 2021 12:40:34 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@posteo.de header.s=2017 header.b=Hc2rvAo9; spf=pass (domain: posteo.de, ip: 185.67.36.65, mailfrom: mhaeuser@posteo.de) Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id 4C296240029 for ; Sun, 8 Aug 2021 21:40:32 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1628451632; bh=5rgaHsU63pwRpEm8V2v30KP25fundbMOck2W6UrSny0=; h=From:To:Cc:Subject:Date:From; b=Hc2rvAo9rwTCct9bhP6MOEKt6rbFl5Eg8S/QZQG5ASs7O251xwC4u99THlhYc1iJ6 WIKiR0iUtACN3UZEdvY/zKWEawe2vQloXZ/A7TD+rHBPocYb+QELHEJuRWJ051Dxvv KMywPeJMxFh0TOJJwxRB3xRC5SijPaqKiFIuchN0Otk8JSA+L2jN20QCIDRmY0GwtW kx7ErH+YLcuQMfAjudB+6oHMOmf79fxz7E5ru+3ek/3uWorAwNZ3b9Jx6aTamb5rHT Hwxc2FzxAiP+r86X9JUpAOv1DwniA02Rj32L5AgBP+vYtsRlBE85uqbjNb3qJpixZd NKKUeXYrHq6EA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GjV1W3dB9z6tmH; Sun, 8 Aug 2021 21:40:31 +0200 (CEST) From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= To: devel@edk2.groups.io Cc: Jian J Wang , Hao A Wu , Dandan Bi , Liming Gao , Vitaly Cheptsov Subject: [PATCH] MdeModulePkg/DxeCore: Consistent DebugImageInfoTable updates Date: Sun, 8 Aug 2021 19:39:40 +0000 Message-Id: <88816e99692b15cf61f3057ffab4d54455159c7c.1628443860.git.mhaeuser@posteo.de> In-Reply-To: <5df11a13422732b9c03c120775a2b4dd0a49182f.1628444003.git.mhaeuser@posteo.de> References: <5df11a13422732b9c03c120775a2b4dd0a49182f.1628444003.git.mhaeuser@posteo.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable In theory, modifications to the DebugImageInfoTable may cause exceptions. If the exception handler parses the table, this can lead to subsequent exceptions if the table state is inconsistent. Ensure the DebugImageInfoTable remains consistent during modifications. This includes: 1) Free the old table only only after the new table has been published. Mitigates use-after-free of the old table. 2) Do not insert an image entry till it is fully initialised. Entries may be inserted in the live range if an entry was deleted previously. Mitigaes the usage of inconsistent entries. 3) Free the old image entry only after the table has been updated with the NULL value. Mitigates use-after-free of the old entry. 4) Set the MODIFIED state before performing any modifications. Cc: Jian J Wang Cc: Hao A Wu Cc: Dandan Bi Cc: Liming Gao Cc: Vitaly Cheptsov Signed-off-by: Marvin H=C3=A4user --- MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c | 60 +++++++++++--------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c b/MdeModulePkg/Cor= e/Dxe/Misc/DebugImageInfo.c index a75d4158280b..7bd970115111 100644 --- a/MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c +++ b/MdeModulePkg/Core/Dxe/Misc/DebugImageInfo.c @@ -165,10 +165,11 @@ CoreNewDebugImageInfoEntry ( IN EFI_HANDLE ImageHandle=0D )=0D {=0D - EFI_DEBUG_IMAGE_INFO *Table;=0D - EFI_DEBUG_IMAGE_INFO *NewTable;=0D - UINTN Index;=0D - UINTN TableSize;=0D + EFI_DEBUG_IMAGE_INFO *Table;=0D + EFI_DEBUG_IMAGE_INFO *NewTable;=0D + UINTN Index;=0D + UINTN TableSize;=0D + EFI_DEBUG_IMAGE_INFO_NORMAL *NormalImage;=0D =0D //=0D // Set the flag indicating that we're in the process of updating the tab= le.=0D @@ -203,14 +204,6 @@ CoreNewDebugImageInfoEntry ( // Copy the old table into the new one=0D //=0D CopyMem (NewTable, Table, TableSize);=0D - //=0D - // Free the old table=0D - //=0D - CoreFreePool (Table);=0D - //=0D - // Update the table header=0D - //=0D - Table =3D NewTable;=0D mDebugInfoTableHeader.EfiDebugImageInfoTable =3D NewTable;=0D //=0D // Enlarge the max table entries and set the first empty entry index t= o=0D @@ -218,24 +211,34 @@ CoreNewDebugImageInfoEntry ( //=0D Index =3D mMaxTableEntries;=0D mMaxTableEntries +=3D EFI_PAGE_SIZE / EFI_DEBUG_TABLE_ENTRY_SIZE;=0D + //=0D + // Free the old table=0D + //=0D + CoreFreePool (Table);=0D + //=0D + // Update the table header=0D + //=0D + Table =3D NewTable;=0D }=0D =0D //=0D // Allocate data for new entry=0D //=0D - Table[Index].NormalImage =3D AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_I= NFO_NORMAL));=0D - if (Table[Index].NormalImage !=3D NULL) {=0D + NormalImage =3D AllocateZeroPool (sizeof (EFI_DEBUG_IMAGE_INFO_NORMAL));= =0D + if (NormalImage !=3D NULL) {=0D //=0D // Update the entry=0D //=0D - Table[Index].NormalImage->ImageInfoType =3D (UINT32) Ima= geInfoType;=0D - Table[Index].NormalImage->LoadedImageProtocolInstance =3D LoadedImage;= =0D - Table[Index].NormalImage->ImageHandle =3D ImageHandle;= =0D + NormalImage->ImageInfoType =3D (UINT32) ImageInfoType;=0D + NormalImage->LoadedImageProtocolInstance =3D LoadedImage;=0D + NormalImage->ImageHandle =3D ImageHandle;=0D //=0D - // Increase the number of EFI_DEBUG_IMAGE_INFO elements and set the mD= ebugInfoTable in modified status.=0D + // Set the mDebugInfoTable in modified status, insert the entry, and=0D + // increase the number of EFI_DEBUG_IMAGE_INFO elements.=0D //=0D - mDebugInfoTableHeader.TableSize++;=0D mDebugInfoTableHeader.UpdateStatus |=3D EFI_DEBUG_IMAGE_INFO_TABLE_MOD= IFIED;=0D + Table[Index].NormalImage =3D NormalImage;=0D + mDebugInfoTableHeader.TableSize++;=0D }=0D mDebugInfoTableHeader.UpdateStatus &=3D ~EFI_DEBUG_IMAGE_INFO_UPDATE_IN_= PROGRESS;=0D }=0D @@ -253,8 +256,9 @@ CoreRemoveDebugImageInfoEntry ( EFI_HANDLE ImageHandle=0D )=0D {=0D - EFI_DEBUG_IMAGE_INFO *Table;=0D - UINTN Index;=0D + EFI_DEBUG_IMAGE_INFO *Table;=0D + UINTN Index;=0D + EFI_DEBUG_IMAGE_INFO_NORMAL *NormalImage;=0D =0D mDebugInfoTableHeader.UpdateStatus |=3D EFI_DEBUG_IMAGE_INFO_UPDATE_IN_P= ROGRESS;=0D =0D @@ -263,16 +267,20 @@ CoreRemoveDebugImageInfoEntry ( for (Index =3D 0; Index < mMaxTableEntries; Index++) {=0D if (Table[Index].NormalImage !=3D NULL && Table[Index].NormalImage->Im= ageHandle =3D=3D ImageHandle) {=0D //=0D - // Found a match. Free up the record, then NULL the pointer to indic= ate the slot=0D - // is free.=0D + // Found a match. Set the mDebugInfoTable in modified status and NUL= L the=0D + // pointer to indicate the slot is free and.=0D //=0D - CoreFreePool (Table[Index].NormalImage);=0D + NormalImage =3D Table[Index].NormalImage;=0D + mDebugInfoTableHeader.UpdateStatus |=3D EFI_DEBUG_IMAGE_INFO_TABLE_M= ODIFIED;=0D Table[Index].NormalImage =3D NULL;=0D //=0D - // Decrease the number of EFI_DEBUG_IMAGE_INFO elements and set the = mDebugInfoTable in modified status.=0D + // Decrease the number of EFI_DEBUG_IMAGE_INFO elements.=0D //=0D mDebugInfoTableHeader.TableSize--;=0D - mDebugInfoTableHeader.UpdateStatus |=3D EFI_DEBUG_IMAGE_INFO_TABLE_M= ODIFIED;=0D + //=0D + // Free up the record.=0D + //=0D + CoreFreePool (NormalImage);=0D break;=0D }=0D }=0D --=20 2.31.1