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 68D6FD80110 for ; Tue, 5 Dec 2023 13:51:12 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=zbOtg6HbkjxXxhUD+bc329jMhYqPrRsdtoxyPLug3+g=; 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=1701784271; v=1; b=QQU1LN8bod17NgQZX38nghDEU3l/S1IRu8FSDfkMZh9uorRvP5oALW2HaC08UytyEnD8Wagq Zkem5nxGX1/Ok1d4gsnuOgBIvLOSgWY7SffcADXgKfjXGpyluBkYD8D34z1JarA8W9z6Yy+ULhF 3Ovjr1P0xJEK4kwFkjIttUYs= X-Received: by 127.0.0.2 with SMTP id wWDeYY7687511xhB2AyFxrXx; Tue, 05 Dec 2023 05:51:11 -0800 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.100825.1701784270275246747 for ; Tue, 05 Dec 2023 05:51:10 -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-256-n9deCXFzMq2cZgxHTLeIJg-1; Tue, 05 Dec 2023 08:51:06 -0500 X-MC-Unique: n9deCXFzMq2cZgxHTLeIJg-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 D125F185A787; Tue, 5 Dec 2023 13:51:05 +0000 (UTC) X-Received: from dobby.home.kraxel.org (unknown [10.39.194.39]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 96A1E492BE6; Tue, 5 Dec 2023 13:51:05 +0000 (UTC) X-Received: by dobby.home.kraxel.org (Postfix, from userid 1000) id 2D29C77FCF; Tue, 5 Dec 2023 14:51:04 +0100 (CET) From: "Gerd Hoffmann" To: devel@edk2.groups.io Cc: Jiewen Yao , Ard Biesheuvel , oliver@redhat.com, Gerd Hoffmann Subject: [edk2-devel] [PATCH 1/1] OvmfPkg/VirtNorFlashDxe: sanity-check variables Date: Tue, 5 Dec 2023 14:51:04 +0100 Message-ID: <20231205135104.265984-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 Reply-To: devel@edk2.groups.io,kraxel@redhat.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: d84vytuOl5lIvZuWvvBhdDO5x7686176AA= 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=QQU1LN8b; 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..72a197e5aa20 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__, + VarState + )); + 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 (#112081): https://edk2.groups.io/g/devel/message/112081 Mute This Topic: https://groups.io/mt/102991507/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-