public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Gerd Hoffmann" <kraxel@redhat.com>
To: Laszlo Ersek <lersek@redhat.com>
Cc: devel@edk2.groups.io, Ard Biesheuvel <ardb+tianocore@kernel.org>,
	 oliver@redhat.com, mike.maslenkin@gmail.com,
	Jiewen Yao <jiewen.yao@intel.com>
Subject: Re: [edk2-devel] [PATCH v3 1/1] OvmfPkg/VirtNorFlashDxe: sanity-check variables
Date: Wed, 3 Jan 2024 16:11:24 +0100	[thread overview]
Message-ID: <ud4z3kjtuv2svjvihzt7ylxfjec3udrid6p5hzknl26bvnfx6c@ktyqkcmoczux> (raw)
In-Reply-To: <bf6be4a2-cba9-de23-07ba-b2decaff2463@redhat.com>

  Hi,

> Second (and worse): the bug. In "OvmfPkg/RiscVVirt/VarStore.fdf.inc", it
> turns out that we *still* generate the gEfiVariableGuid varstore header
> signature, in case SECURE_BOOT_ENABLE is FALSE. For some reason, commit
> 92b27c2e6ada ("OvmfPkg/RiscVVirt: Add build files for Qemu Virt
> platform", 2023-02-16) did not consider commit d92eaabefbe0 ("OvmfPkg:
> simplify VARIABLE_STORE_HEADER generation", 2016-02-15), and
> *resurrected* the non-unified varstore generation for RiscVVirt.
> Furthermore, RiscVVirt uses "VirtNorFlashDxe" as its platform flash
> driver. As a result, if you now build RiscVVirt with this patch applied,
> and with SECURE_BOOT_ENABLE=FALSE, I expect the ValidateFvHeader()
> function to always fail, becase it will try to validate the contents of
> the varstore through AUTHENTICATED_VARIABLE_HEADER entries, despite the
> varstore containing (arguably valid) VARIABLE_HEADER entries.

I expect it will fail only once.  In case the checks don't pass
VirtNorFlashDxe will re-initialize the flash varstore with
gEfiAuthenticatedVariableGuid, so on next boot everything is fine.

> So here's what I propose:
> 
> - keep this patch, but *prepend* two other patches:
> 
> - first, reflect commit d92eaabefbe0 to
> "OvmfPkg/RiscVVirt/VarStore.fdf.inc" -- only generate the authenticated
> signature GUID, regardless of SECURE_BOOT_ENABLE,
> 
> - second, in this function, stop accepting the "gEfiVariableGuid"
> varstore header signature.

Makes sense.

> > +    if (VarHeaderEnd >= VariableStoreHeader->Size) {
> > +      DEBUG ((DEBUG_INFO, "%a: end of var list (no space left)\n", __func__));
> > +      break;
> > +    }
> 
> (4) In case of inequality, the variable header is truncated. Accepting
> it as "success" looks doubtful.

We don't know whenever it is supposed to be a valid header, we didn't
check the StartId yet.

Reversing the check ordering looks wrong too (looking at header fields
before we know the header is inside the store).

> (5) In case of equality, the variable header fits, but it is followed by
> no payload at all. I think there are sub-cases to distinguish there:
> 
> - if the StartId differs from 0x55aa, then we may consider the variable
> list to be terminated, and break out of the loop (returning success from
> the function)
> 
> - if the StartId is 0x55aa, then we need to look further, beause we
> can't decide yet. For example, if State is VAR_HEADER_VALID_ONLY (0x7f),
> then it might be fine for the variable header (at the very end of the
> varstore) *not* to be followed by payload bytes (name, data).

Not sure this makes sense.  VAR_HEADER_VALID_ONLY is a temporary state,
while the variable driver writes name and data just after the header,
to be updated to VAR_ADDED when the write completed successfully.  So
I'd expect to never find a header without space for name + data.

> I find this code hard to review because I don't know (and the Intel
> whitepaper doesn't seem to tell) precisely how a valid variable list is
> supposed to be terminated.

Which is why the code logs the condition why it considers the list to be
terminated ...

> (6) I suggest two further checks (within the braces here):
> 
> - enforce
> 
>   VarHeader->NameSize > 0

NameSize >= 4 ?  (room for one char and the terminating null)

> - enforce
> 
>   VarName[VarHeader->NameSize / 2 - 1] == L'\0'

ok

> (This is also important for the immediately subsequent code: we print
> the name!)

Indeed.

> (7) Not really important, I'm just throwing it out: how about logging
> "VarHeader->VendorGuid" too?
> 
> It would require something like this:
> 
>   CONST EFI_GUID  *VarGuid;
> 
>   ...
> 
>   VarGuid = &gZeroGuid;
>   if (VarName == NULL) {
>     ...
>     VarGuid = &VarHeader->VendorGuid;
>     ...
>   }

I think we can just use VarHeader->VendorGuid directly, given that the
guid is part of the fixed header it should be valid even in case the
state is VAR_HEADER_VALID_ONLY.

take care,
  Gerd



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#113100): https://edk2.groups.io/g/devel/message/113100
Mute This Topic: https://groups.io/mt/103171811/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  parent reply	other threads:[~2024-01-03 15:11 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-14 15:31 [edk2-devel] [PATCH v3 1/1] OvmfPkg/VirtNorFlashDxe: sanity-check variables Gerd Hoffmann
2024-01-03 12:56 ` Laszlo Ersek
2024-01-03 13:09   ` Laszlo Ersek
2024-01-03 13:13     ` Laszlo Ersek
2024-01-03 15:11   ` Gerd Hoffmann [this message]
2024-01-04 13:21     ` Laszlo Ersek
2024-01-04 15:06       ` Gerd Hoffmann
2024-01-05 13:50         ` Laszlo Ersek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ud4z3kjtuv2svjvihzt7ylxfjec3udrid6p5hzknl26bvnfx6c@ktyqkcmoczux \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox