From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: redhat.com, ip: 209.132.183.28, mailfrom: lersek@redhat.com) Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by groups.io with SMTP; Mon, 24 Jun 2019 13:33:45 -0700 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7F11D308FED5; Mon, 24 Jun 2019 20:33:45 +0000 (UTC) Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-226.ams2.redhat.com [10.36.116.226]) by smtp.corp.redhat.com (Postfix) with ESMTP id 233C45D717; Mon, 24 Jun 2019 20:33:43 +0000 (UTC) Subject: Re: [PATCH] UefiCpuPkg/MpInitLib: Move checksum part for valid microcode data To: Zhichao Gao , devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Liming Gao References: <20190624061437.33744-1-zhichao.gao@intel.com> From: "Laszlo Ersek" Message-ID: Date: Mon, 24 Jun 2019 22:33:43 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: <20190624061437.33744-1-zhichao.gao@intel.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Mon, 24 Jun 2019 20:33:45 +0000 (UTC) Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit On 06/24/19 08:14, Zhichao Gao wrote: > REF: https://bugzilla.tianocore.org/show_bug.cgi?id=1934 > > Originally, the checksum part would done before verfiy the microcode > data. Which meas the checksum would be done for a meaningless data. > It would cause a incorrect TotalSize (the size of microcode data), > then incorrect checksum and incorrect pointer increasing would happen. > To fix this, move the checksum part 1 section in > 'if (MicrocodeEntryPoint->HeaderVersion == 0x1)' section for a valid > microcode data. > > Cc: Eric Dong > Cc: Ray Ni > Cc: Laszlo Ersek > Cc: Liming Gao > Signed-off-by: Zhichao Gao > --- > UefiCpuPkg/Library/MpInitLib/Microcode.c | 52 ++++++++++++------------ > 1 file changed, 26 insertions(+), 26 deletions(-) I'll defer on this patch to the (other) UefiCpuPkg reviewers / maintainers. OVMF inherits the default zero value for "PcdCpuMicrocodePatchRegionSize", from "UefiCpuPkg.dec". Therefore the first check in MicrocodeDetect() evaluates to TRUE, and the function exits immediately, in OVMF. Thanks Laszlo > diff --git a/UefiCpuPkg/Library/MpInitLib/Microcode.c b/UefiCpuPkg/Library/MpInitLib/Microcode.c > index 4763dcfebe..f1a42f2d4e 100644 > --- a/UefiCpuPkg/Library/MpInitLib/Microcode.c > +++ b/UefiCpuPkg/Library/MpInitLib/Microcode.c > @@ -1,7 +1,7 @@ > /** @file > Implementation of loading microcode on processors. > > - Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.
> + Copyright (c) 2015 - 2019, Intel Corporation. All rights reserved.
> SPDX-License-Identifier: BSD-2-Clause-Patent > > **/ > @@ -160,34 +160,34 @@ MicrocodeDetect ( > // > CorrectMicrocode = FALSE; > > - if (MicrocodeEntryPoint->DataSize == 0) { > - TotalSize = sizeof (CPU_MICROCODE_HEADER) + 2000; > - } else { > - TotalSize = sizeof (CPU_MICROCODE_HEADER) + MicrocodeEntryPoint->DataSize; > - } > + if (MicrocodeEntryPoint->HeaderVersion == 0x1) { > + if (MicrocodeEntryPoint->DataSize == 0) { > + TotalSize = sizeof (CPU_MICROCODE_HEADER) + 2000; > + } else { > + TotalSize = sizeof (CPU_MICROCODE_HEADER) + MicrocodeEntryPoint->DataSize; > + } > > - /// > - /// Check overflow and whether TotalSize is aligned with 4 bytes. > - /// > - if ( ((UINTN)MicrocodeEntryPoint + TotalSize) > MicrocodeEnd || > - (TotalSize & 0x3) != 0 > - ) { > - MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + SIZE_1KB); > - continue; > - } > + /// > + /// Check overflow and whether TotalSize is aligned with 4 bytes. > + /// > + if ( ((UINTN)MicrocodeEntryPoint + TotalSize) > MicrocodeEnd || > + (TotalSize & 0x3) != 0 > + ) { > + MicrocodeEntryPoint = (CPU_MICROCODE_HEADER *) (((UINTN) MicrocodeEntryPoint) + SIZE_1KB); > + continue; > + } > > - // > - // Save an in-complete CheckSum32 from CheckSum Part1 for common parts. > - // > - InCompleteCheckSum32 = CalculateSum32 ( > - (UINT32 *) MicrocodeEntryPoint, > - TotalSize > - ); > - InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorSignature.Uint32; > - InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags; > - InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum; > + // > + // Save an in-complete CheckSum32 from CheckSum Part1 for common parts. > + // > + InCompleteCheckSum32 = CalculateSum32 ( > + (UINT32 *) MicrocodeEntryPoint, > + TotalSize > + ); > + InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorSignature.Uint32; > + InCompleteCheckSum32 -= MicrocodeEntryPoint->ProcessorFlags; > + InCompleteCheckSum32 -= MicrocodeEntryPoint->Checksum; > > - if (MicrocodeEntryPoint->HeaderVersion == 0x1) { > // > // It is the microcode header. It is not the padding data between microcode patches > // because the padding data should not include 0x00000001 and it should be the repeated >