All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Gerd Hoffmann <kraxel@redhat.com>
Cc: qemu-devel@nongnu.org, graf@amazon.com,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Michael Roth" <michael.roth@amd.com>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>,
	"Thomas Huth" <thuth@redhat.com>,
	qemu-arm@nongnu.org, "Eric Blake" <eblake@redhat.com>,
	"Peter Maydell" <peter.maydell@linaro.org>,
	"Markus Armbruster" <armbru@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>
Subject: Re: [PATCH v2 13/21] hw/uefi: add var-service-json.c + qapi for NV vars.
Date: Tue, 7 Jan 2025 15:49:09 +0000	[thread overview]
Message-ID: <Z31M9Vd37_koWggu@redhat.com> (raw)
In-Reply-To: <20250107153353.1144978-14-kraxel@redhat.com>

On Tue, Jan 07, 2025 at 04:33:40PM +0100, Gerd Hoffmann wrote:
> Define qapi schema for the uefi variable store state.
> 
> Use it and the generated visitor helper functions to store persistent
> (EFI_VARIABLE_NON_VOLATILE) variables in JSON format on disk.
> 
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  hw/uefi/var-service-json.c | 242 +++++++++++++++++++++++++++++++++++++
>  qapi/meson.build           |   1 +
>  qapi/qapi-schema.json      |   1 +
>  qapi/uefi.json             |  45 +++++++
>  4 files changed, 289 insertions(+)
>  create mode 100644 hw/uefi/var-service-json.c
>  create mode 100644 qapi/uefi.json
> 

> +void uefi_vars_json_init(uefi_vars_state *uv, Error **errp)
> +{
> +    if (uv->jsonfile) {
> +        uv->jsonfd = qemu_create(uv->jsonfile, O_RDWR, 0666, errp);
> +    }
> +}
> +
> +void uefi_vars_json_save(uefi_vars_state *uv)
> +{
> +    GString *gstr;
> +    int rc;
> +
> +    if (uv->jsonfd == -1) {
> +        return;
> +    }
> +
> +    gstr = uefi_vars_to_json(uv);
> +
> +    lseek(uv->jsonfd, 0, SEEK_SET);
> +    rc = write(uv->jsonfd, gstr->str, gstr->len);
> +    if (rc != gstr->len) {
> +        warn_report("%s: write error", __func__);
> +    }
> +    rc = ftruncate(uv->jsonfd, gstr->len);
> +    if (rc != 0) {
> +        warn_report("%s: ftruncate error", __func__);
> +    }
> +    fsync(uv->jsonfd);

Although the fsync helps, re-writing the file in-place is a ad idea for data
integrity on host OS crash. Especially if the new data is shorter, we would
easily end up with a file containing old and new data making it unparsable
(assuming the parser doesn't ignore trailing data).

I'd like to suggest the write to temp file + rename dance to get atomic
replacement. The problem with that is that it hits the DAC/MAC security
restrictions we put QEMU under :-(

My next best idea is to re-arrange things thus:

   lseek(fd, 0)
   ftruncate(fd, 0)
   fsync(fd)
   write(fd, str)
   fsync(fd)

so we should at least reduce the liklihood of getting a mix of
old and new data - empty file is better than a mix of data.



With regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


  reply	other threads:[~2025-01-07 15:49 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-07 15:33 [PATCH v2 00/21] hw/uefi: add uefi variable service Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 01/21] hw/uefi: add include/hw/uefi/var-service-api.h Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 02/21] hw/uefi: add include/hw/uefi/var-service-edk2.h Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 03/21] hw/uefi: add include/hw/uefi/var-service.h Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 04/21] hw/uefi: add var-service-guid.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 05/21] hw/uefi: add var-service-utils.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 06/21] hw/uefi: add var-service-vars.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 07/21] hw/uefi: add var-service-auth.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 08/21] hw/uefi: add var-service-policy.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 09/21] hw/uefi: add var-service-core.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 10/21] hw/uefi: add var-service-pkcs7.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 11/21] hw/uefi: add var-service-pkcs7-stub.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 12/21] hw/uefi: add var-service-siglist.c Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 13/21] hw/uefi: add var-service-json.c + qapi for NV vars Gerd Hoffmann
2025-01-07 15:49   ` Daniel P. Berrangé [this message]
2025-01-07 16:16     ` Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 14/21] hw/uefi: add trace-events Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 15/21] hw/uefi: add UEFI_VARS to Kconfig Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 16/21] hw/uefi: add to meson Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 17/21] hw/uefi: add uefi-vars-sysbus device Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 18/21] hw/uefi: add uefi-vars-isa device Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 19/21] hw/arm: add uefi variable support to virt machine type Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 20/21] docs: add uefi variable service documentation Gerd Hoffmann
2025-01-07 15:33 ` [PATCH v2 21/21] hw/uefi: add MAINTAINERS entry Gerd Hoffmann
2025-01-07 15:41 ` [PATCH v2 00/21] hw/uefi: add uefi variable service Daniel P. Berrangé
2025-01-07 15:51   ` Gerd Hoffmann
2025-01-08 11:53 ` Marc-André Lureau
2025-01-08 12:24   ` Daniel P. Berrangé
2025-01-08 13:45     ` Gerd Hoffmann
2025-01-08 14:02   ` Gerd Hoffmann

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-all 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=Z31M9Vd37_koWggu@redhat.com \
    --to=berrange@redhat.com \
    --cc=armbru@redhat.com \
    --cc=eblake@redhat.com \
    --cc=graf@amazon.com \
    --cc=kraxel@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=michael.roth@amd.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=thuth@redhat.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.