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 03589D80410 for ; Thu, 7 Dec 2023 09:44:14 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=CujIEaMMkwjCCRlAk/B6OpJ+Iuy4L4v1Uw8H4Q70Yd4=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding:Content-Type; s=20140610; t=1701942253; v=1; b=Qr9qPP2WOb8JpluEwlz/3THTDFrXibXEuD1wPJ7dXjtHStO408s1XLZtFo9P4E5pCvGqZnn2 BEUCoRWmAJjrLF2PC7WQKvvxqfgnx5w8rr88NNNFEPXiQGQj+LBSU/fnzhBKUO9n0eLPI3loy4O 8zY23NqcTEaO18PvhYuE6/Lo= X-Received: by 127.0.0.2 with SMTP id JP2GYY7687511xOu9QLOAfDW; Thu, 07 Dec 2023 01:44:13 -0800 X-Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by mx.groups.io with SMTP id smtpd.web10.80413.1701942253065270393 for ; Thu, 07 Dec 2023 01:44:13 -0800 X-Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-265-Nv81Vk1AObaTcGQ1-Mx-wA-1; Thu, 07 Dec 2023 04:44:06 -0500 X-MC-Unique: Nv81Vk1AObaTcGQ1-Mx-wA-1 X-Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (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 5B28F84AF8C; Thu, 7 Dec 2023 09:44:06 +0000 (UTC) X-Received: from dobby.home.kraxel.org (unknown [10.39.194.62]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F415F1121312; Thu, 7 Dec 2023 09:44:05 +0000 (UTC) X-Received: by dobby.home.kraxel.org (Postfix, from userid 1000) id A98FC7A9AD; Thu, 7 Dec 2023 10:44:04 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: mike.maslenkin@gmail.com, Jiewen Yao , Ard Biesheuvel , oliver@redhat.com, Gerd Hoffmann Subject: [edk2-devel] [PATCH v2 1/1] OvmfPkg/VirtNorFlashDxe: sanity-check variables Date: Thu, 7 Dec 2023 10:44:04 +0100 Message-ID: <20231207094404.270381-1-kraxel@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.3 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 Reply-To: devel@edk2.groups.io,kraxel@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 16VG1ZX3wve2TPP41oNmyQ37x7686176AA= 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=20140610 header.b=Qr9qPP2W; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=redhat.com (policy=none) Extend the ValidateFvHeader function, additionally to the header checks walk over the list of variables and sanity check them. In case we find inconsistencies indicating variable store corruption return EFI_NOT_FOUND so the variable store will be re-initialized. Signed-off-by: Gerd Hoffmann --- OvmfPkg/VirtNorFlashDxe/VirtNorFlashFvb.c | 82 +++++++++++++++++++++-- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/OvmfPkg/VirtNorFlashDxe/VirtNorFlashFvb.c b/OvmfPkg/VirtNorFlashDxe/VirtNorFlashFvb.c index 5ee98e9b595a..1bfb14495abd 100644 --- a/OvmfPkg/VirtNorFlashDxe/VirtNorFlashFvb.c +++ b/OvmfPkg/VirtNorFlashDxe/VirtNorFlashFvb.c @@ -185,11 +185,16 @@ ValidateFvHeader ( IN NOR_FLASH_INSTANCE *Instance ) { - UINT16 Checksum; - EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader; - VARIABLE_STORE_HEADER *VariableStoreHeader; - UINTN VariableStoreLength; - UINTN FvLength; + UINT16 Checksum; + EFI_FIRMWARE_VOLUME_HEADER *FwVolHeader; + VARIABLE_STORE_HEADER *VariableStoreHeader; + UINTN VarOffset; + AUTHENTICATED_VARIABLE_HEADER *VarHeader; + UINTN VarSize; + CHAR16 *VarName; + CHAR8 *VarState; + UINTN VariableStoreLength; + UINTN FvLength; FwVolHeader = (EFI_FIRMWARE_VOLUME_HEADER *)Instance->RegionBaseAddress; @@ -260,6 +265,73 @@ ValidateFvHeader ( return EFI_NOT_FOUND; } + // check variables + DEBUG ((DEBUG_INFO, "%a: checking variables\n", __func__)); + VarOffset = sizeof (*VariableStoreHeader); + while (VarOffset + sizeof (*VarHeader) < VariableStoreHeader->Size) { + VarHeader = (VOID *)((UINTN)VariableStoreHeader + VarOffset); + if (VarHeader->StartId != 0x55aa) { + DEBUG ((DEBUG_INFO, "%a: end of var list\n", __func__)); + break; + } + + VarSize = sizeof (*VarHeader) + VarHeader->NameSize + VarHeader->DataSize; + if (VarOffset + VarSize > VariableStoreHeader->Size) { + DEBUG (( + DEBUG_ERROR, + "%a: invalid variable size: 0x%x + 0x%x + 0x%x + 0x%x > 0x%x\n", + __func__, + VarOffset, + sizeof (*VarHeader), + VarHeader->NameSize, + VarHeader->DataSize, + VariableStoreHeader->Size + )); + return EFI_NOT_FOUND; + } + + VarName = (VOID *)((UINTN)VariableStoreHeader + VarOffset + + sizeof (*VarHeader)); + switch (VarHeader->State) { + case VAR_HEADER_VALID_ONLY: + VarState = "header-ok"; + VarName = L""; + break; + case VAR_ADDED: + VarState = "ok"; + break; + case VAR_ADDED &VAR_IN_DELETED_TRANSITION: + VarState = "del-in-transition"; + break; + case VAR_ADDED &VAR_DELETED: + case VAR_ADDED &VAR_DELETED &VAR_IN_DELETED_TRANSITION: + VarState = "deleted"; + break; + default: + DEBUG (( + DEBUG_ERROR, + "%a: invalid variable state: 0x%x\n", + __func__, + VarHeader->State + )); + return EFI_NOT_FOUND; + } + + DEBUG (( + DEBUG_VERBOSE, + "%a: +0x%04x: name=0x%x data=0x%x '%s' (%a)\n", + __func__, + VarOffset, + VarHeader->NameSize, + VarHeader->DataSize, + VarName, + VarState + )); + + VarSize = (VarSize + 3) & ~3; // align + VarOffset += VarSize; + } + return EFI_SUCCESS; } -- 2.43.0 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#112177): https://edk2.groups.io/g/devel/message/112177 Mute This Topic: https://groups.io/mt/103031342/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-