From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.158.5]) by mx.groups.io with SMTP id smtpd.web11.5925.1627412856838235649 for ; Tue, 27 Jul 2021 12:07:37 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@ibm.com header.s=pp1 header.b=Sz02cL1L; spf=pass (domain: linux.ibm.com, ip: 148.163.158.5, mailfrom: dovmurik@linux.ibm.com) Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 16RJ43gr111455; Tue, 27 Jul 2021 15:07:34 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : mime-version; s=pp1; bh=fYwzO9HjC4KdheSppTOGWmZurvII8Ja9cF/kjN5ROdI=; b=Sz02cL1L61RjlkMldGqLbXgmn45E2iiqIcP6druDmjFItw6p+q1GGvLyxPgCWTNhtzpS TlCg2jYgS9s5NeEaZgzM9ItajyoHy030PlfcX23CiRScRNP6tqDqa/wg+E8sCeaOogOV w84htnkDlJOphpgBerh9RIk03Yyco/xKkQWinSCGxv4QJgCksg8gsU4yK4HiW1YGpfh0 peKa/JmFutR3aBv42qjQ0YUwXhX3O4mdGl1vc/2cRHL1rIf2bqWip5Udhpph/IXw9Ja0 Hu66yIETjVdkm6l5VGCxsDtTYK5zhH3BWepRpZoUrU9xOMV7AeoCnMmBiI0ShoMAKt+E Nw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 3a2qqw0mqb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 27 Jul 2021 15:07:34 -0400 Received: from m0098414.ppops.net (m0098414.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 16RJ4672111743; Tue, 27 Jul 2021 15:07:33 -0400 Received: from ppma03wdc.us.ibm.com (ba.79.3fa9.ip4.static.sl-reverse.com [169.63.121.186]) by mx0b-001b2d01.pphosted.com with ESMTP id 3a2qqw0mpm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 27 Jul 2021 15:07:33 -0400 Received: from pps.filterd (ppma03wdc.us.ibm.com [127.0.0.1]) by ppma03wdc.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 16RIw8Vq009293; Tue, 27 Jul 2021 19:07:32 GMT Received: from b01cxnp22035.gho.pok.ibm.com (b01cxnp22035.gho.pok.ibm.com [9.57.198.25]) by ppma03wdc.us.ibm.com with ESMTP id 3a235mr6uu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 27 Jul 2021 19:07:32 +0000 Received: from b01ledav003.gho.pok.ibm.com (b01ledav003.gho.pok.ibm.com [9.57.199.108]) by b01cxnp22035.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 16RJ7VLw38273380 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 27 Jul 2021 19:07:31 GMT Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1CC8AB2065; Tue, 27 Jul 2021 19:07:31 +0000 (GMT) Received: from b01ledav003.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id CEDEBB206B; Tue, 27 Jul 2021 19:07:30 +0000 (GMT) Received: from localhost.localdomain (unknown [9.2.130.16]) by b01ledav003.gho.pok.ibm.com (Postfix) with ESMTP; Tue, 27 Jul 2021 19:07:30 +0000 (GMT) From: "Dov Murik" To: devel@edk2.groups.io Cc: Dov Murik , Tobin Feldman-Fitzthum , Tobin Feldman-Fitzthum , Jim Cadden , James Bottomley , Hubertus Franke , Ard Biesheuvel , Jordan Justen , Ashish Kalra , Brijesh Singh , Erdem Aktas , Jiewen Yao , Min Xu , Tom Lendacky Subject: [PATCH v5 10/11] OvmfPkg/AmdSev: add BlobVerifierLibSevHashes Date: Tue, 27 Jul 2021 19:07:23 +0000 Message-Id: <20210727190724.3586867-11-dovmurik@linux.ibm.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210727190724.3586867-1-dovmurik@linux.ibm.com> References: <20210727190724.3586867-1-dovmurik@linux.ibm.com> X-TM-AS-GCONF: 00 X-Proofpoint-GUID: Q3pVHm5gB944ow11AetbkFcVJlNINmP5 X-Proofpoint-ORIG-GUID: p2JjJ9okVV1ZhzC-ti1VSUMmeCjQKn4q X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.391,18.0.790 definitions=2021-07-27_13:2021-07-27,2021-07-27 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 priorityscore=1501 mlxscore=0 phishscore=0 spamscore=0 impostorscore=0 malwarescore=0 clxscore=1015 adultscore=0 bulkscore=0 lowpriorityscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2107140000 definitions=main-2107270113 Content-Transfer-Encoding: quoted-printable Add an implementation for BlobVerifierLib that locates the SEV hashes table and verifies that the calculated hashes of the kernel, initrd, and cmdline blobs indeed match the expected hashes stated in the hashes table. If there's a missing hash or a hash mismatch then EFI_ACCESS_DENIED is returned which will cause a failure to load a kernel image. Cc: Ard Biesheuvel Cc: Jordan Justen Cc: Ashish Kalra Cc: Brijesh Singh Cc: Erdem Aktas Cc: James Bottomley Cc: Jiewen Yao Cc: Min Xu Cc: Tom Lendacky Ref: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3457 Co-developed-by: James Bottomley Signed-off-by: James Bottomley Signed-off-by: Dov Murik --- OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierLibSevHashes.inf | 37= ++++ OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierSevHashes.c | 202= ++++++++++++++++++++ 2 files changed, 239 insertions(+) diff --git a/OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierLibSevHash= es.inf b/OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierLibSevHashes.i= nf new file mode 100644 index 000000000000..76ca0b8154ce --- /dev/null +++ b/OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierLibSevHashes.inf @@ -0,0 +1,37 @@ +## @file=0D +#=0D +# Blob verifier library that uses SEV hashes table. The hashes table hol= ds the=0D +# allowed hashes of the kernel, initrd, and cmdline blobs.=0D +#=0D +# Copyright (C) 2021, IBM Corp=0D +#=0D +# SPDX-License-Identifier: BSD-2-Clause-Patent=0D +#=0D +##=0D +=0D +[Defines]=0D + INF_VERSION =3D 1.29=0D + BASE_NAME =3D BlobVerifierLibSevHashes=0D + FILE_GUID =3D 59e713b5-eff3-46a7-8d8b-46f4c004ad7b= =0D + MODULE_TYPE =3D BASE=0D + VERSION_STRING =3D 1.0=0D + LIBRARY_CLASS =3D BlobVerifierLib=0D + CONSTRUCTOR =3D BlobVerifierLibSevHashesConstructor=0D +=0D +[Sources]=0D + BlobVerifierSevHashes.c=0D +=0D +[Packages]=0D + CryptoPkg/CryptoPkg.dec=0D + MdePkg/MdePkg.dec=0D + OvmfPkg/OvmfPkg.dec=0D +=0D +[LibraryClasses]=0D + BaseCryptLib=0D + BaseMemoryLib=0D + DebugLib=0D + PcdLib=0D +=0D +[FixedPcd]=0D + gUefiOvmfPkgTokenSpaceGuid.PcdQemuHashTableBase=0D + gUefiOvmfPkgTokenSpaceGuid.PcdQemuHashTableSize=0D diff --git a/OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierSevHashes.= c b/OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierSevHashes.c new file mode 100644 index 000000000000..b9054879b07f --- /dev/null +++ b/OvmfPkg/AmdSev/BlobVerifierLibSevHashes/BlobVerifierSevHashes.c @@ -0,0 +1,202 @@ +/** @file=0D +=0D + Blob verifier library that uses SEV hashes table. The hashes table hold= s the=0D + allowed hashes of the kernel, initrd, and cmdline blobs.=0D +=0D + Copyright (C) 2021, IBM Corporation=0D +=0D + SPDX-License-Identifier: BSD-2-Clause-Patent=0D +**/=0D +=0D +#include =0D +#include =0D +#include =0D +#include =0D +#include =0D +=0D +/**=0D + The SEV Hashes table must be in encrypted memory and has the table=0D + and its entries described by=0D +=0D + |UINT16 |=0D +=0D + With the whole table GUID being 9438d606-4f22-4cc9-b479-a793d411fd21=0D +=0D + The current possible table entries are for the kernel, the initrd=0D + and the cmdline:=0D +=0D + 4de79437-abd2-427f-b835-d5b172d2045b kernel=0D + 44baf731-3a2f-4bd7-9af1-41e29169781d initrd=0D + 97d02dd8-bd20-4c94-aa78-e7714d36ab2a cmdline=0D +=0D + The size of the entry is used to identify the hash, but the=0D + expectation is that it will be 32 bytes of SHA-256.=0D +**/=0D +=0D +#define SEV_HASH_TABLE_GUID \=0D + (GUID) { 0x9438d606, 0x4f22, 0x4cc9, { 0xb4, 0x79, 0xa7, 0x93, 0xd4, 0x1= 1, 0xfd, 0x21 } }=0D +#define SEV_KERNEL_HASH_GUID \=0D + (GUID) { 0x4de79437, 0xabd2, 0x427f, { 0xb8, 0x35, 0xd5, 0xb1, 0x72, 0xd= 2, 0x04, 0x5b } }=0D +#define SEV_INITRD_HASH_GUID \=0D + (GUID) { 0x44baf731, 0x3a2f, 0x4bd7, { 0x9a, 0xf1, 0x41, 0xe2, 0x91, 0x6= 9, 0x78, 0x1d } }=0D +#define SEV_CMDLINE_HASH_GUID \=0D + (GUID) { 0x97d02dd8, 0xbd20, 0x4c94, { 0xaa, 0x78, 0xe7, 0x71, 0x4d, 0x3= 6, 0xab, 0x2a } }=0D +=0D +STATIC CONST EFI_GUID mSevKernelHashGuid =3D SEV_KERNEL_HASH_GUID;=0D +STATIC CONST EFI_GUID mSevInitrdHashGuid =3D SEV_INITRD_HASH_GUID;=0D +STATIC CONST EFI_GUID mSevCmdlineHashGuid =3D SEV_CMDLINE_HASH_GUID;=0D +=0D +#pragma pack (1)=0D +typedef struct {=0D + GUID Guid;=0D + UINT16 Len;=0D + UINT8 Data[];=0D +} HASH_TABLE;=0D +#pragma pack ()=0D +=0D +STATIC HASH_TABLE *mHashesTable;=0D +STATIC UINT16 mHashesTableSize;=0D +=0D +STATIC=0D +CONST GUID*=0D +FindBlobEntryGuid (=0D + IN CONST CHAR16 *BlobName=0D + )=0D +{=0D + if (StrCmp (BlobName, L"kernel") =3D=3D 0) {=0D + return &mSevKernelHashGuid;=0D + } else if (StrCmp (BlobName, L"initrd") =3D=3D 0) {=0D + return &mSevInitrdHashGuid;=0D + } else if (StrCmp (BlobName, L"cmdline") =3D=3D 0) {=0D + return &mSevCmdlineHashGuid;=0D + } else {=0D + return NULL;=0D + }=0D +}=0D +=0D +/**=0D + Verify blob from an external source.=0D +=0D + @param[in] BlobName The name of the blob=0D + @param[in] Buf The data of the blob=0D + @param[in] BufSize The size of the blob in bytes=0D +=0D + @retval EFI_SUCCESS The blob was verified successfully.=0D + @retval EFI_ACCESS_DENIED The blob could not be verified, and theref= ore=0D + should be considered non-secure.=0D +**/=0D +EFI_STATUS=0D +EFIAPI=0D +VerifyBlob (=0D + IN CONST CHAR16 *BlobName,=0D + IN CONST VOID *Buf,=0D + IN UINT32 BufSize=0D + )=0D +{=0D + CONST GUID *Guid;=0D + INT32 Remaining;=0D + HASH_TABLE *Entry;=0D +=0D + if (mHashesTable =3D=3D NULL || mHashesTableSize =3D=3D 0) {=0D + DEBUG ((DEBUG_ERROR,=0D + "%a: Verifier called but no hashes table discoverd in MEMFD\n",=0D + __FUNCTION__));=0D + return EFI_ACCESS_DENIED;=0D + }=0D +=0D + Guid =3D FindBlobEntryGuid (BlobName);=0D + if (Guid =3D=3D NULL) {=0D + DEBUG ((DEBUG_ERROR, "%a: Unknown blob name \"%s\"\n", __FUNCTION__,=0D + BlobName));=0D + return EFI_ACCESS_DENIED;=0D + }=0D +=0D + //=0D + // Remaining is INT32 to catch underflow in case Entry->Len has a=0D + // very high UINT16 value=0D + //=0D + for (Entry =3D mHashesTable, Remaining =3D mHashesTableSize;=0D + Remaining >=3D sizeof *Entry && Remaining >=3D Entry->Len;=0D + Remaining -=3D Entry->Len,=0D + Entry =3D (HASH_TABLE *)((UINT8 *)Entry + Entry->Len)) {=0D + UINTN EntrySize;=0D + EFI_STATUS Status;=0D + UINT8 Hash[SHA256_DIGEST_SIZE];=0D +=0D + if (!CompareGuid (&Entry->Guid, Guid)) {=0D + continue;=0D + }=0D +=0D + DEBUG ((DEBUG_INFO, "%a: Found GUID %g in table\n", __FUNCTION__, Guid= ));=0D +=0D + EntrySize =3D Entry->Len - sizeof Entry->Guid - sizeof Entry->Len;=0D + if (EntrySize !=3D SHA256_DIGEST_SIZE) {=0D + DEBUG ((DEBUG_ERROR, "%a: Hash has the wrong size %d !=3D %d\n",=0D + __FUNCTION__, EntrySize, SHA256_DIGEST_SIZE));=0D + return EFI_ACCESS_DENIED;=0D + }=0D +=0D + //=0D + // Calculate the buffer's hash and verify that it is identical to the= =0D + // expected hash table entry=0D + //=0D + Sha256HashAll (Buf, BufSize, Hash);=0D +=0D + if (CompareMem (Entry->Data, Hash, EntrySize) =3D=3D 0) {=0D + Status =3D EFI_SUCCESS;=0D + DEBUG ((DEBUG_INFO, "%a: Hash comparison succeeded for \"%s\"\n",=0D + __FUNCTION__, BlobName));=0D + } else {=0D + Status =3D EFI_ACCESS_DENIED;=0D + DEBUG ((DEBUG_ERROR, "%a: Hash comparison failed for \"%s\"\n",=0D + __FUNCTION__, BlobName));=0D + }=0D + return Status;=0D + }=0D +=0D + DEBUG ((DEBUG_ERROR, "%a: Hash GUID %g not found in table\n", __FUNCTION= __,=0D + Guid));=0D + return EFI_ACCESS_DENIED;=0D +}=0D +=0D +/**=0D + Locate the SEV hashes table.=0D +=0D + This function always returns success, even if the table can't be found. = The=0D + subsequent VerifyBlob calls will fail if no table was found.=0D +=0D + @retval RETURN_SUCCESS The hashes table is set up correctly, or there = is no=0D + hashes table=0D +**/=0D +RETURN_STATUS=0D +EFIAPI=0D +BlobVerifierLibSevHashesConstructor (=0D + VOID=0D + )=0D +{=0D + HASH_TABLE *Ptr;=0D + UINT32 Size;=0D +=0D + mHashesTable =3D NULL;=0D + mHashesTableSize =3D 0;=0D +=0D + Ptr =3D (void *)(UINTN)FixedPcdGet64 (PcdQemuHashTableBase);=0D + Size =3D FixedPcdGet32 (PcdQemuHashTableSize);=0D +=0D + if (Ptr =3D=3D NULL || Size < sizeof *Ptr ||=0D + !CompareGuid (&Ptr->Guid, &SEV_HASH_TABLE_GUID) ||=0D + Ptr->Len < sizeof *Ptr || Ptr->Len > Size) {=0D + return RETURN_SUCCESS;=0D + }=0D +=0D + DEBUG ((DEBUG_INFO, "%a: Found injected hashes table in secure location\= n",=0D + __FUNCTION__));=0D +=0D + mHashesTable =3D (HASH_TABLE *)Ptr->Data;=0D + mHashesTableSize =3D Ptr->Len - sizeof Ptr->Guid - sizeof Ptr->Len;=0D +=0D + DEBUG ((DEBUG_VERBOSE, "%a: mHashesTable=3D0x%p, Size=3D%u\n", __FUNCTIO= N__,=0D + mHashesTable, mHashesTableSize));=0D +=0D + return RETURN_SUCCESS;=0D +}=0D --=20 2.25.1