From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id 551377803DA for ; Mon, 22 Apr 2024 10:47:44 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=eHUCVRqjYEJmGidvHY7P5WvSveUVZ3znQyy+wOfPqTY=; 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:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20240206; t=1713782863; v=1; b=yx7s4EBdolqh26oSU7Ri22UDjMjOJqvo4352Oyn6AKm+QSV6uhLi+56eNBfJHc8tmTRAult3 JwN5ouMyMmt7GGiOYtKwjWbXSMaC75+9BhRkedpmnLYlOEBDeDd0Lz1KbyxOO/sTm2OeT+dDO0K ksPdklqfTCQ/nLBbJn4PUKU7Nr4rshoKDjKzaWwxLYmn7u9RZGUHtf9QWQweXJJ0YuhTZb498sl MCto/Pua9w3bWBvbFgRCrdD4PrCsIcyP6OR7CCTKcnbqBwR27JjayOW7y3I9ZP4hVQP8n9gvN0u 6fKrx5eWwGCPQN6Kj/dpoc9QWzyQXohISF6C1tJATaF+g== X-Received: by 127.0.0.2 with SMTP id OezvYY7687511xzoX7OWyUTw; Mon, 22 Apr 2024 03:47:43 -0700 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.groups.io with SMTP id smtpd.web10.15590.1713782857391574681 for ; Mon, 22 Apr 2024 03:47:37 -0700 X-Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-426-N9ztHNi4NT-DU7Zh2gqNug-1; Mon, 22 Apr 2024 06:47:33 -0400 X-MC-Unique: N9ztHNi4NT-DU7Zh2gqNug-1 X-Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id DF63B3C0ED49; Mon, 22 Apr 2024 10:47:32 +0000 (UTC) X-Received: from sirius.home.kraxel.org (unknown [10.39.192.150]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 66339C13FA4; Mon, 22 Apr 2024 10:47:32 +0000 (UTC) X-Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 4C3B018009BB; Mon, 22 Apr 2024 12:47:29 +0200 (CEST) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: Konstantin Kostiuk , Oliver Steffen , Jiewen Yao , Ard Biesheuvel , Gerd Hoffmann , Jiewen Yao Subject: [edk2-devel] [PATCH v3 3/5] OvmfPkg/VirtHstiDxe: add varstore flash check Date: Mon, 22 Apr 2024 12:47:27 +0200 Message-ID: <20240422104729.502112-4-kraxel@redhat.com> In-Reply-To: <20240422104729.502112-1-kraxel@redhat.com> References: <20240422104729.502112-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 Resent-Date: Mon, 22 Apr 2024 03:47:37 -0700 Resent-From: kraxel@redhat.com Reply-To: devel@edk2.groups.io,kraxel@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: jPM4do9R1d49gGkIlbV3g41Ex7686176AA= Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=yx7s4EBd; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io Detects qemu config issue: vars pflash is not in secure mode (write access restricted to smm). Applies to Q35 with SMM only. Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Konstantin Kostiuk Signed-off-by: Gerd Hoffmann Reviewed-by: Jiewen Yao --- OvmfPkg/VirtHstiDxe/VirtHstiDxe.inf | 4 ++ OvmfPkg/VirtHstiDxe/VirtHstiDxe.h | 16 ++++- OvmfPkg/VirtHstiDxe/Flash.c | 90 +++++++++++++++++++++++++++++ OvmfPkg/VirtHstiDxe/QemuQ35.c | 13 +++++ 4 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 OvmfPkg/VirtHstiDxe/Flash.c diff --git a/OvmfPkg/VirtHstiDxe/VirtHstiDxe.inf b/OvmfPkg/VirtHstiDxe/VirtHstiDxe.inf index 8c63ff6a8953..b6bdd1f22e83 100644 --- a/OvmfPkg/VirtHstiDxe/VirtHstiDxe.inf +++ b/OvmfPkg/VirtHstiDxe/VirtHstiDxe.inf @@ -22,6 +22,7 @@ [Sources] VirtHstiDxe.c QemuPC.c QemuQ35.c + Flash.c [Packages] MdePkg/MdePkg.dec @@ -46,5 +47,8 @@ [Guids] [FeaturePcd] gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire +[Pcd] + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashNvStorageVariableBase + [Depex] TRUE diff --git a/OvmfPkg/VirtHstiDxe/VirtHstiDxe.h b/OvmfPkg/VirtHstiDxe/VirtHstiDxe.h index cf0d77fc3af9..ceff41c03711 100644 --- a/OvmfPkg/VirtHstiDxe/VirtHstiDxe.h +++ b/OvmfPkg/VirtHstiDxe/VirtHstiDxe.h @@ -6,7 +6,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #define VIRT_HSTI_SECURITY_FEATURE_SIZE 2 -#define VIRT_HSTI_BYTE0_SMM_SMRAM_LOCK BIT0 +#define VIRT_HSTI_BYTE0_SMM_SMRAM_LOCK BIT0 +#define VIRT_HSTI_BYTE0_SMM_SECURE_VARS_FLASH BIT1 typedef struct { // ADAPTER_INFO_PLATFORM_SECURITY @@ -65,3 +66,16 @@ VOID VirtHstiQemuPCVerify ( VOID ); + +/* Flash.c */ + +#define QEMU_FIRMWARE_FLASH_UNKNOWN 0 +#define QEMU_FIRMWARE_FLASH_IS_ROM 1 +#define QEMU_FIRMWARE_FLASH_IS_RAM 2 +#define QEMU_FIRMWARE_FLASH_READ_ONLY 3 +#define QEMU_FIRMWARE_FLASH_WRITABLE 4 + +UINT32 +VirtHstiQemuFirmwareFlashCheck ( + UINT32 Address + ); diff --git a/OvmfPkg/VirtHstiDxe/Flash.c b/OvmfPkg/VirtHstiDxe/Flash.c new file mode 100644 index 000000000000..e93356793f8c --- /dev/null +++ b/OvmfPkg/VirtHstiDxe/Flash.c @@ -0,0 +1,90 @@ +/** @file + +SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include +#include + +#include "VirtHstiDxe.h" + +#define WRITE_BYTE_CMD 0x10 +#define BLOCK_ERASE_CMD 0x20 +#define CLEAR_STATUS_CMD 0x50 +#define READ_STATUS_CMD 0x70 +#define READ_DEVID_CMD 0x90 +#define BLOCK_ERASE_CONFIRM_CMD 0xd0 +#define READ_ARRAY_CMD 0xff +#define CLEARED_ARRAY_STATUS 0x00 + +/* based on QemuFlashDetected (QemuFlashFvbServicesRuntimeDxe) */ +UINT32 +VirtHstiQemuFirmwareFlashCheck ( + UINT32 Address + ) +{ + volatile UINT8 *Ptr; + + UINTN Offset; + UINT8 OriginalUint8; + UINT8 ProbeUint8; + + for (Offset = 0; Offset < EFI_PAGE_SIZE; Offset++) { + Ptr = (UINT8 *)(UINTN)(Address + Offset); + ProbeUint8 = *Ptr; + if ((ProbeUint8 != CLEAR_STATUS_CMD) && + (ProbeUint8 != READ_STATUS_CMD) && + (ProbeUint8 != CLEARED_ARRAY_STATUS)) + { + break; + } + } + + if (Offset >= EFI_PAGE_SIZE) { + DEBUG ((DEBUG_INFO, "%a: check failed\n", __func__)); + return QEMU_FIRMWARE_FLASH_UNKNOWN; + } + + OriginalUint8 = *Ptr; + *Ptr = CLEAR_STATUS_CMD; + ProbeUint8 = *Ptr; + if ((OriginalUint8 != CLEAR_STATUS_CMD) && + (ProbeUint8 == CLEAR_STATUS_CMD)) + { + *Ptr = OriginalUint8; + DEBUG ((DEBUG_INFO, "%a: %p behaves as RAM\n", __func__, Ptr)); + return QEMU_FIRMWARE_FLASH_IS_RAM; + } + + *Ptr = READ_STATUS_CMD; + ProbeUint8 = *Ptr; + if (ProbeUint8 == OriginalUint8) { + DEBUG ((DEBUG_INFO, "%a: %p behaves as ROM\n", __func__, Ptr)); + return QEMU_FIRMWARE_FLASH_IS_ROM; + } + + if (ProbeUint8 == READ_STATUS_CMD) { + *Ptr = OriginalUint8; + DEBUG ((DEBUG_INFO, "%a: %p behaves as RAM\n", __func__, Ptr)); + return QEMU_FIRMWARE_FLASH_IS_RAM; + } + + if (ProbeUint8 == CLEARED_ARRAY_STATUS) { + *Ptr = WRITE_BYTE_CMD; + *Ptr = OriginalUint8; + *Ptr = READ_STATUS_CMD; + ProbeUint8 = *Ptr; + *Ptr = READ_ARRAY_CMD; + if (ProbeUint8 & 0x10 /* programming error */) { + DEBUG ((DEBUG_INFO, "%a: %p behaves as FLASH, write-protected\n", __func__, Ptr)); + return QEMU_FIRMWARE_FLASH_READ_ONLY; + } else { + DEBUG ((DEBUG_INFO, "%a: %p behaves as FLASH, writable\n", __func__, Ptr)); + return QEMU_FIRMWARE_FLASH_WRITABLE; + } + } + + DEBUG ((DEBUG_INFO, "%a: check failed\n", __func__)); + return QEMU_FIRMWARE_FLASH_UNKNOWN; +} diff --git a/OvmfPkg/VirtHstiDxe/QemuQ35.c b/OvmfPkg/VirtHstiDxe/QemuQ35.c index 5eab4aab29d1..2dcfa5239cbf 100644 --- a/OvmfPkg/VirtHstiDxe/QemuQ35.c +++ b/OvmfPkg/VirtHstiDxe/QemuQ35.c @@ -29,6 +29,7 @@ VirtHstiQemuQ35Init ( { if (FeaturePcdGet (PcdSmmSmramRequire)) { VirtHstiSetSupported (&mHstiQ35, 0, VIRT_HSTI_BYTE0_SMM_SMRAM_LOCK); + VirtHstiSetSupported (&mHstiQ35, 0, VIRT_HSTI_BYTE0_SMM_SECURE_VARS_FLASH); } return &mHstiQ35; @@ -55,4 +56,16 @@ VirtHstiQemuQ35Verify ( VirtHstiTestResult (ErrorMsg, 0, VIRT_HSTI_BYTE0_SMM_SMRAM_LOCK); } + + if (VirtHstiIsSupported (&mHstiQ35, 0, VIRT_HSTI_BYTE0_SMM_SECURE_VARS_FLASH)) { + CHAR16 *ErrorMsg = NULL; + + switch (VirtHstiQemuFirmwareFlashCheck (PcdGet32 (PcdOvmfFlashNvStorageVariableBase))) { + case QEMU_FIRMWARE_FLASH_WRITABLE: + ErrorMsg = L"qemu vars pflash is not secure"; + break; + } + + VirtHstiTestResult (ErrorMsg, 0, VIRT_HSTI_BYTE0_SMM_SECURE_VARS_FLASH); + } } -- 2.44.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#118084): https://edk2.groups.io/g/devel/message/118084 Mute This Topic: https://groups.io/mt/105667073/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-