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.20765.1628502718075804511 for ; Mon, 09 Aug 2021 02:51:58 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@posteo.de header.s=2017 header.b=pQ41efvv; 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 8AF0024002A for ; Mon, 9 Aug 2021 11:51:50 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1628502710; bh=5rgaHsU63pwRpEm8V2v30KP25fundbMOck2W6UrSny0=; h=From:To:Cc:Subject:Date:From; b=pQ41efvvfwaljdFxdt4Uh7GnLB+t9Mq8hRT5L0XUF27vmqeqHJcp/SPGPcWaw7lpo +cSr7U/TYVLukZM679m3lxQAzLgXJlcDkJeRaEOKMvadw/FhM67Duw1vr3kHyJZZzI etbgzEOkdJxXosHzihKjA/Ya9SNpG5kJ0kyohrgIrOJKgj/aBl8Y2ghJelIaK95drQ rRmtfeXo952q94MKt/6ccy417ssxg/NvqgCAg1M+68TI1x9lB6ocH8g8JiufXCH5sh ebIJ0uFZPHETdSa1tm13D3HHVbq/gPw9O+UZjsEkc4lL/ZuC+6Sw+hewtjkhAPw/lf mR/S4VuRXVMAw== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4Gjrvn6MlCz6tmG; Mon, 9 Aug 2021 11:51:49 +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 v2 1/7] MdeModulePkg/DxeCore: Consistent DebugImageInfoTable updates Date: Mon, 9 Aug 2021 09:51:21 +0000 Message-Id: <88816e99692b15cf61f3057ffab4d54455159c7c.1628502345.git.mhaeuser@posteo.de> In-Reply-To: References: 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