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 27A377803CC for ; Fri, 22 Mar 2024 14:28:01 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=7BWD8ugdiMncdfMnKKRDbJPpjVOfB8HA1Eg7R6zYSZU=; 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:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20240206; t=1711117679; v=1; b=kPVv7t/h7LpKYwGP0WzKa0iolLE7hNl/8Oy4lSoWqDp4qXM8pDJLuBjEs4XNzYhIuOGzjMsq 03f0ZTrGyKJ7HGnDT58mc2dHel5Pcw2HVuvJtDDgZSvYGB4Hh4OoGeXBAH7dtmjMqkgS8g0tngZ tRfwCHqUi+1pz1Xk3noTaVFZRZ7718rLqIbZRD1GK2GJZzrCw5hMVtmVmAJqUDHAPxr/hUtdCvi g2vcch4NAqhOk/J1Hz6g1XhrSFYAqV8fjccKCbweDZhPXE0yCCu7/1LKAVua349SJQ6CCx91k0P xE9c58qSDWiciP1bqrgkZVHrDWlJ2s20MnTtjJBpqXFhw== X-Received: by 127.0.0.2 with SMTP id khhGYY7687511xZtb8Zo3cVg; Fri, 22 Mar 2024 07:27:59 -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.web11.14735.1711117679218848089 for ; Fri, 22 Mar 2024 07:27:59 -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-490-boLF7QOGOPWRw-SwyxMkWg-1; Fri, 22 Mar 2024 10:27:54 -0400 X-MC-Unique: boLF7QOGOPWRw-SwyxMkWg-1 X-Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (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 C967E2800C2F; Fri, 22 Mar 2024 14:27:53 +0000 (UTC) X-Received: from sirius.home.kraxel.org (unknown [10.39.192.134]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4D4AA492BDA; Fri, 22 Mar 2024 14:27:53 +0000 (UTC) X-Received: by sirius.home.kraxel.org (Postfix, from userid 1000) id 1070E1801AB5; Fri, 22 Mar 2024 15:27:36 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: Oliver Steffen , Gerd Hoffmann , Ard Biesheuvel , Jiewen Yao , Konstantin Kostiuk Subject: [edk2-devel] [PATCH 3/4] OvmfPkg/VirtHstiDxe: add varstore flash check Date: Fri, 22 Mar 2024 15:27:34 +0100 Message-ID: <20240322142735.1749388-4-kraxel@redhat.com> In-Reply-To: <20240322142735.1749388-1-kraxel@redhat.com> References: <20240322142735.1749388-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 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: Fri, 22 Mar 2024 07:27:59 -0700 Reply-To: devel@edk2.groups.io,kraxel@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: EDR11z4vLlMjN9EmRO97iJuXx7686176AA= 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="kPVv7t/h"; 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 66.175.222.108 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 --- 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..9cb2ed1f0c64 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.PcdOvmfFdBaseAddress + [Depex] TRUE diff --git a/OvmfPkg/VirtHstiDxe/VirtHstiDxe.h b/OvmfPkg/VirtHstiDxe/VirtHstiDxe.h index 26109bf322e9..ca4e376582ad 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_Q35_SMM_SMRAM_LOCK BIT0 +#define VIRT_HSTI_BYTE0_Q35_SMM_SMRAM_LOCK BIT0 +#define VIRT_HSTI_BYTE0_Q35_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 75e9731b4a52..203122627d2d 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_Q35_SMM_SMRAM_LOCK); + VirtHstiSetSupported (&mHstiQ35, 0, VIRT_HSTI_BYTE0_Q35_SMM_SECURE_VARS_FLASH); } return &mHstiQ35; @@ -55,4 +56,16 @@ VirtHstiQemuQ35Verify ( VirtHstiTestResult (ErrorMsg, 0, VIRT_HSTI_BYTE0_Q35_SMM_SMRAM_LOCK); } + + if (VirtHstiIsSupported (&mHstiQ35, 0, VIRT_HSTI_BYTE0_Q35_SMM_SECURE_VARS_FLASH)) { + CHAR16 *ErrorMsg = NULL; + + switch (VirtHstiQemuFirmwareFlashCheck (PcdGet32 (PcdOvmfFdBaseAddress))) { + case QEMU_FIRMWARE_FLASH_WRITABLE: + ErrorMsg = L"qemu vars pflash is not secure"; + break; + } + + VirtHstiTestResult (ErrorMsg, 0, VIRT_HSTI_BYTE0_Q35_SMM_SECURE_VARS_FLASH); + } } -- 2.44.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#117049): https://edk2.groups.io/g/devel/message/117049 Mute This Topic: https://groups.io/mt/105086167/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-