All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: David Woodhouse <dwmw2@infradead.org>
Cc: "Gustavo A. R. Silva" <gustavo@embeddedor.com>,
	daniel@iogearbox.net, gustavoars@kernel.org, jgg@ziepe.ca,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Thomas Gleixner <tglx@kernel.org>, Ingo Molnar <mingo@redhat.com>,
	Borislav Petkov <bp@alien8.de>,
	Dave Hansen <dave.hansen@linux.intel.com>,
	x86@kernel.org, "H. Peter Anvin" <hpa@zytor.com>
Subject: Re: [PATCH] KVM: x86: Fix C++ user API for structures with variable length arrays
Date: Thu, 26 Feb 2026 11:02:13 -0800	[thread overview]
Message-ID: <202602261053.78753BF1C@keescook> (raw)
In-Reply-To: <97d40dd0e6abaf28f43d4d8ccf9c547a16c52e33.camel@infradead.org>

On Thu, Feb 26, 2026 at 11:44:21AM +0000, David Woodhouse wrote:
> From: David Woodhouse <dwmw@amazon.co.uk>
> 
> Commit 94dfc73e7cf4 ("treewide: uapi: Replace zero-length arrays with
> flexible-array members") broke the userspace API for C++. Not just in
> the sense of 'userspace needs to be updated, but UAPI is supposed to be
> stable", but broken in the sense that I can't actually see *how* the
> structures can be used from C++ in the same way that they were usable
> before.
> 
> These structures ending in VLAs are typically a *header*, which can be
> followed by an arbitrary number of entries. Userspace typically creates
> a larger structure with some non-zero number of entries, for example in
> QEMU's kvm_arch_get_supported_msr_feature():
> 
>     struct {
>         struct kvm_msrs info;
>         struct kvm_msr_entry entries[1];
>     } msr_data = {};
> 
> While that works in C, it fails in C++ with an error like:
>  flexible array member ‘kvm_msrs::entries’ not at end of ‘struct msr_data’
> 
> Fix this by using __DECLARE_FLEX_ARRAY() for the VLA, which is a helper
> provided by <linux/stddef.h> that already uses [0] for C++ compilation.

This is likely the best plan for these cases. I had to do similar for
ACPICA upstream, leaving these flex arrays as [0] for the non-GCC (and
Clang) builds:
https://github.com/acpica/acpica/commit/e73b227e8e475c20cc394f237ea35d592fdf9ec3

> Also put the header fields into a struct_group() to provide (in C) a
> separate struct (e.g 'struct kvm_msrs_hdr') without the trailing VLA.

Right, my only worry is if C++ would want those header structs too. In
that case, you'd probably want to use a macro to include them (since not
all compilers are supporting transparent struct members yet):

#define __kvm_msrs_hdr	\
	__u32 nmsrs; /* number of msrs in entries */	\
	__u32 pad

struct kvm_msrs_hdr {
	__kvm_msrs_hdr;
};

struct kvm_msrs {
	__kvm_msrs_hdr;
	__DECLARE_FLEX_ARRAY(struct kvm_msr_entry, entries);
};

> Fixes: 94dfc73e7cf4 ("treewide: uapi: Replace zero-length arrays with flexible-array members")
> 
> Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>

Regardless:

Reviewed-by: Kees Cook <kees@kernel.org>


-- 
Kees Cook

  reply	other threads:[~2026-02-26 19:02 UTC|newest]

Thread overview: 117+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-27 18:04 [PATCH][next] treewide: uapi: Replace zero-length arrays with flexible-array members Gustavo A. R. Silva
2022-06-27 18:04 ` Gustavo A. R. Silva
2022-06-27 18:04 ` Gustavo A. R. Silva
2022-06-27 18:04 ` Gustavo A. R. Silva
2022-06-27 18:04 ` Gustavo A. R. Silva
2022-06-27 18:04 ` [dm-devel] " Gustavo A. R. Silva
2022-06-27 18:27 ` Daniel Borkmann
2022-06-27 18:27   ` Daniel Borkmann
2022-06-27 18:27   ` Daniel Borkmann
2022-06-27 18:27   ` Daniel Borkmann
2022-06-27 18:27   ` Daniel Borkmann
2022-06-27 18:27   ` Daniel Borkmann
2022-06-27 18:27   ` [Intel-gfx] " Daniel Borkmann
2022-06-27 18:27   ` [dm-devel] " Daniel Borkmann
2022-06-27 18:35   ` Gustavo A. R. Silva
2022-06-27 18:35     ` Gustavo A. R. Silva
2022-06-27 18:35     ` Gustavo A. R. Silva
2022-06-27 18:35     ` Gustavo A. R. Silva
2022-06-27 18:35     ` Gustavo A. R. Silva
2022-06-27 18:35     ` [dm-devel] " Gustavo A. R. Silva
2022-06-28  0:40   ` Jason Gunthorpe
2022-06-28  0:40     ` Jason Gunthorpe
2022-06-28  0:40     ` Jason Gunthorpe
2022-06-28  0:40     ` Jason Gunthorpe
2022-06-28  0:40     ` Jason Gunthorpe
2022-06-28  0:40     ` Jason Gunthorpe
2022-06-28  0:40     ` [dm-devel] " Jason Gunthorpe
2022-06-28  0:58     ` Gustavo A. R. Silva
2022-06-28  0:58       ` Gustavo A. R. Silva
2022-06-28  0:58       ` Gustavo A. R. Silva
2022-06-28  0:58       ` Gustavo A. R. Silva
2022-06-28  0:58       ` Gustavo A. R. Silva
2022-06-28  0:58       ` [dm-devel] " Gustavo A. R. Silva
2022-06-28  2:21       ` Gustavo A. R. Silva
2022-06-28  2:21         ` Gustavo A. R. Silva
2022-06-28  2:21         ` Gustavo A. R. Silva
2022-06-28  2:21         ` Gustavo A. R. Silva
2022-06-28  2:21         ` Gustavo A. R. Silva
2022-06-28  2:21         ` [dm-devel] " Gustavo A. R. Silva
2022-06-28 13:36         ` Jason Gunthorpe
2022-06-28 13:36           ` Jason Gunthorpe
2022-06-28 13:36           ` Jason Gunthorpe
2022-06-28 13:36           ` Jason Gunthorpe
2022-06-28 13:36           ` Jason Gunthorpe
2022-06-28 13:36           ` Jason Gunthorpe
2022-06-28 13:36           ` [dm-devel] " Jason Gunthorpe
2022-06-28 13:56           ` Gustavo A. R. Silva
2022-06-28 13:56             ` Gustavo A. R. Silva
2022-06-28 13:56             ` Gustavo A. R. Silva
2022-06-28 13:56             ` Gustavo A. R. Silva
2022-06-28 13:56             ` Gustavo A. R. Silva
2022-06-28 13:56             ` [dm-devel] " Gustavo A. R. Silva
2022-06-28 17:54     ` Kees Cook
2022-06-28 17:54       ` Kees Cook
2022-06-28 17:54       ` Kees Cook
2022-06-28 17:54       ` Kees Cook
2022-06-28 17:54       ` Kees Cook
2022-06-28 17:54       ` Kees Cook
2022-06-28 17:54       ` [Intel-gfx] " Kees Cook
2022-06-28 17:54       ` [dm-devel] " Kees Cook
2022-06-28 18:44       ` Jason Gunthorpe
2022-06-28 18:44         ` Jason Gunthorpe
2022-06-28 18:44         ` Jason Gunthorpe
2022-06-28 18:44         ` Jason Gunthorpe
2022-06-28 18:44         ` Jason Gunthorpe
2022-06-28 18:44         ` Jason Gunthorpe
2022-06-28 18:44         ` [dm-devel] " Jason Gunthorpe
2026-02-23 14:28       ` David Woodhouse
2026-02-23  3:38         ` Gustavo A. R. Silva
2026-02-23 19:57           ` David Woodhouse
2026-02-26 11:44           ` [PATCH] KVM: x86: Fix C++ user API for structures with variable length arrays David Woodhouse
2026-02-26 19:02             ` Kees Cook [this message]
2026-02-27  8:29               ` David Woodhouse
2026-02-28  0:43                 ` Kees Cook
2026-02-28  8:54                   ` David Woodhouse
2026-03-05 18:36             ` Sean Christopherson
2026-03-05 19:18               ` David Woodhouse
2026-03-05 19:31                 ` Sean Christopherson
2026-03-05 19:49                   ` [PATCH v2] KVM: x86: Use __DECLARE_FLEX_ARRAY() for UAPI structures with VLAs David Woodhouse
2026-04-03 15:13                     ` Sean Christopherson
2022-06-27 19:53 ` [PATCH][next] treewide: uapi: Replace zero-length arrays with flexible-array members Stephen Hemminger
2022-06-27 19:53   ` Stephen Hemminger
2022-06-27 19:53   ` Stephen Hemminger
2022-06-27 19:53   ` Stephen Hemminger
2022-06-27 19:53   ` Stephen Hemminger
2022-06-27 19:53   ` [Intel-gfx] " Stephen Hemminger
2022-06-27 19:53   ` [dm-devel] " Stephen Hemminger
2022-06-28 14:18   ` Gustavo A. R. Silva
2022-06-28 14:18     ` Gustavo A. R. Silva
2022-06-28 14:18     ` Gustavo A. R. Silva
2022-06-28 14:18     ` Gustavo A. R. Silva
2022-06-28 14:18     ` Gustavo A. R. Silva
2022-06-28 14:18     ` [dm-devel] " Gustavo A. R. Silva
2022-06-27 22:31 ` Dan Williams
2022-06-27 22:31   ` Dan Williams
2022-06-27 22:31   ` Dan Williams
2022-06-27 22:31   ` Dan Williams
2022-06-27 22:31   ` Dan Williams
2022-06-27 22:31   ` Dan Williams
2022-06-27 22:31   ` [Intel-gfx] " Dan Williams
2022-06-27 22:31   ` [dm-devel] " Dan Williams
2022-06-28  7:27 ` Geert Uytterhoeven
2022-06-28  7:27   ` Geert Uytterhoeven
2022-06-28  7:27   ` Geert Uytterhoeven
2022-06-28  7:27   ` Geert Uytterhoeven
2022-06-28  7:27   ` Geert Uytterhoeven
2022-06-28  7:27   ` Geert Uytterhoeven
2022-06-28  7:27   ` [Intel-gfx] " Geert Uytterhoeven
2022-06-28  7:27   ` [dm-devel] " Geert Uytterhoeven
2022-06-28 18:05   ` Kees Cook
2022-06-28 18:05     ` Kees Cook
2022-06-28 18:05     ` Kees Cook
2022-06-28 18:05     ` Kees Cook
2022-06-28 18:05     ` Kees Cook
2022-06-28 18:05     ` Kees Cook
2022-06-28 18:05     ` [Intel-gfx] " Kees Cook
2022-06-28 18:05     ` [dm-devel] " Kees Cook

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=202602261053.78753BF1C@keescook \
    --to=kees@kernel.org \
    --cc=bp@alien8.de \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=dwmw2@infradead.org \
    --cc=gustavo@embeddedor.com \
    --cc=gustavoars@kernel.org \
    --cc=hpa@zytor.com \
    --cc=jgg@ziepe.ca \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    --cc=tglx@kernel.org \
    --cc=x86@kernel.org \
    /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.