From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 68D95780091 for ; Fri, 12 Jan 2024 02:25:39 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=RqUKOZYa/hdOhBcKqahbuaf3DY0vBHpQdJi2vPZNCkI=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20140610; t=1705026338; v=1; b=FgoUG260CO6UmzVYgTZtjPeVvJKsgOnEZ3IlPwA9m+tBvNMcAQVR8BNOSBM3cdphAPQxfbqx v3kGPICp73C1/6OpFlJvmJjNQ9vhgqCUYAIzbFShoH1S8H5KzeCHveuHqX+wGuXYZ0G6nE7WZZE rJg23hthSR+fBSoQLLwwiLZQ= X-Received: by 127.0.0.2 with SMTP id 6v6sYY7687511xjKEQKe0Uuk; Thu, 11 Jan 2024 18:25:38 -0800 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) by mx.groups.io with SMTP id smtpd.web11.10547.1705026337473623558 for ; Thu, 11 Jan 2024 18:25:37 -0800 X-IronPort-AV: E=McAfee;i="6600,9927,10950"; a="6423576" X-IronPort-AV: E=Sophos;i="6.04,188,1695711600"; d="scan'208";a="6423576" X-Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 11 Jan 2024 18:25:36 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10950"; a="873233204" X-IronPort-AV: E=Sophos;i="6.04,188,1695711600"; d="scan'208";a="873233204" X-Received: from gguo-desk.gar.corp.intel.com ([10.225.76.26]) by FMSMGA003.fm.intel.com with ESMTP; 11 Jan 2024 18:25:33 -0800 From: "Guo, Gua" To: devel@edk2.groups.io Cc: gua.guo@intel.com, Marc Beatove , Leif Lindholm , Ard Biesheuvel , Abner Chang , John Mathew , Gerd Hoffmann Subject: [edk2-devel] [PATCH v3 3/4] EmbeddedPkg/Hob: Integer Overflow in CreateHob() Date: Fri, 12 Jan 2024 10:25:19 +0800 Message-Id: <20240112022521.710-4-gua.guo@intel.com> In-Reply-To: <20240112022521.710-1-gua.guo@intel.com> References: <20240112022521.710-1-gua.guo@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,gua.guo@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: Wza73jC75KRPMvlGLDxadarFx7686176AA= Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=FgoUG260; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io From: Gua Guo REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3D4166 Fix integer overflow in various CreateHob instances. Fixes: CVE-2022-36765 The CreateHob() function aligns the requested size to 8 performing the following operation: ``` HobLength =3D (UINT16)((HobLength + 0x7) & (~0x7)); ``` No checks are performed to ensure this value doesn't overflow, and could lead to CreateHob() returning a smaller HOB than requested, which could lead to OOB HOB accesses. Reported-by: Marc Beatove Cc: Leif Lindholm Reviewed-by: Ard Biesheuvel Cc: Abner Chang Cc: John Mathew Authored-by: Gerd Hoffmann Signed-off-by: Gua Guo --- EmbeddedPkg/Library/PrePiHobLib/Hob.c | 43 +++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/EmbeddedPkg/Library/PrePiHobLib/Hob.c b/EmbeddedPkg/Library/Pr= ePiHobLib/Hob.c index 8eb175aa96..cbc35152cc 100644 --- a/EmbeddedPkg/Library/PrePiHobLib/Hob.c +++ b/EmbeddedPkg/Library/PrePiHobLib/Hob.c @@ -110,6 +110,13 @@ CreateHob ( =0D HandOffHob =3D GetHobList ();=0D =0D + //=0D + // Check Length to avoid data overflow.=0D + //=0D + if (HobLength > MAX_UINT16 - 0x7) {=0D + return NULL;=0D + }=0D +=0D HobLength =3D (UINT16)((HobLength + 0x7) & (~0x7));=0D =0D FreeMemory =3D HandOffHob->EfiFreeMemoryTop - HandOffHob->EfiFreeMemoryB= ottom;=0D @@ -160,6 +167,9 @@ BuildResourceDescriptorHob ( =0D Hob =3D CreateHob (EFI_HOB_TYPE_RESOURCE_DESCRIPTOR, sizeof (EFI_HOB_RES= OURCE_DESCRIPTOR));=0D ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return;=0D + }=0D =0D Hob->ResourceType =3D ResourceType;=0D Hob->ResourceAttribute =3D ResourceAttribute;=0D @@ -401,6 +411,10 @@ BuildModuleHob ( );=0D =0D Hob =3D CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMOR= Y_ALLOCATION_MODULE));=0D + ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return;=0D + }=0D =0D CopyGuid (&(Hob->MemoryAllocationHeader.Name), &gEfiHobMemoryAllocModule= Guid);=0D Hob->MemoryAllocationHeader.MemoryBaseAddress =3D MemoryAllocationModule= ;=0D @@ -449,6 +463,11 @@ BuildGuidHob ( ASSERT (DataLength <=3D (0xffff - sizeof (EFI_HOB_GUID_TYPE)));=0D =0D Hob =3D CreateHob (EFI_HOB_TYPE_GUID_EXTENSION, (UINT16)(sizeof (EFI_HOB= _GUID_TYPE) + DataLength));=0D + ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return NULL;=0D + }=0D +=0D CopyGuid (&Hob->Name, Guid);=0D return Hob + 1;=0D }=0D @@ -512,6 +531,10 @@ BuildFvHob ( EFI_HOB_FIRMWARE_VOLUME *Hob;=0D =0D Hob =3D CreateHob (EFI_HOB_TYPE_FV, sizeof (EFI_HOB_FIRMWARE_VOLUME));=0D + ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return;=0D + }=0D =0D Hob->BaseAddress =3D BaseAddress;=0D Hob->Length =3D Length;=0D @@ -543,6 +566,10 @@ BuildFv2Hob ( EFI_HOB_FIRMWARE_VOLUME2 *Hob;=0D =0D Hob =3D CreateHob (EFI_HOB_TYPE_FV2, sizeof (EFI_HOB_FIRMWARE_VOLUME2));= =0D + ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return;=0D + }=0D =0D Hob->BaseAddress =3D BaseAddress;=0D Hob->Length =3D Length;=0D @@ -584,6 +611,10 @@ BuildFv3Hob ( EFI_HOB_FIRMWARE_VOLUME3 *Hob;=0D =0D Hob =3D CreateHob (EFI_HOB_TYPE_FV3, sizeof (EFI_HOB_FIRMWARE_VOLUME3));= =0D + ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return;=0D + }=0D =0D Hob->BaseAddress =3D BaseAddress;=0D Hob->Length =3D Length;=0D @@ -639,6 +670,10 @@ BuildCpuHob ( EFI_HOB_CPU *Hob;=0D =0D Hob =3D CreateHob (EFI_HOB_TYPE_CPU, sizeof (EFI_HOB_CPU));=0D + ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return;=0D + }=0D =0D Hob->SizeOfMemorySpace =3D SizeOfMemorySpace;=0D Hob->SizeOfIoSpace =3D SizeOfIoSpace;=0D @@ -676,6 +711,10 @@ BuildStackHob ( );=0D =0D Hob =3D CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMOR= Y_ALLOCATION_STACK));=0D + ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return;=0D + }=0D =0D CopyGuid (&(Hob->AllocDescriptor.Name), &gEfiHobMemoryAllocStackGuid);=0D Hob->AllocDescriptor.MemoryBaseAddress =3D BaseAddress;=0D @@ -756,6 +795,10 @@ BuildMemoryAllocationHob ( );=0D =0D Hob =3D CreateHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, sizeof (EFI_HOB_MEMOR= Y_ALLOCATION));=0D + ASSERT (Hob !=3D NULL);=0D + if (Hob =3D=3D NULL) {=0D + return;=0D + }=0D =0D ZeroMem (&(Hob->AllocDescriptor.Name), sizeof (EFI_GUID));=0D Hob->AllocDescriptor.MemoryBaseAddress =3D BaseAddress;=0D --=20 2.39.2.windows.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#113642): https://edk2.groups.io/g/devel/message/113642 Mute This Topic: https://groups.io/mt/103675964/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-