Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

-Wmissing-braces in drivers/firmware/efi/libstub/efi-stub-helper.c #1547

Closed
nathanchance opened this issue Dec 15, 2021 · 8 comments
Closed
Labels
-Wmissing-braces [BUG] linux-next This is an issue only seen in linux-next [FIXED][LINUX] development cycle This bug was only present and fixed in a -next or -rc cycle

Comments

@nathanchance
Copy link
Member

After commit 2e5006b7634f ("efi/libstub: measure loaded initrd info into the TPM") in -next:

drivers/firmware/efi/libstub/efi-stub-helper.c:646:2: warning: suggest braces around initialization of subobject [-Wmissing-braces]
        "Linux initrd",
        ^~~~~~~~~~~~~~
        {             }
1 warning generated.

The full variable:

static const struct {
        efi_tcg2_event_t        event_data;
        efi_tcg2_tagged_event_t tagged_event;
        u8                      tagged_event_data[];
} initrd_tcg2_event = {
        {
                sizeof(initrd_tcg2_event) + sizeof("Linux initrd"),
                {
                        sizeof(initrd_tcg2_event.event_data.event_header),
                        EFI_TCG2_EVENT_HEADER_VERSION,
                        9,
                        EV_EVENT_TAG,
                },
        },
        {
                INITRD_EVENT_TAG_ID,
                sizeof("Linux initrd"),
        },
        "Linux initrd",
};

Is clang getting something wrong here? GCC accepts the current form without any warnings. The suggestion resolves it but I want to make sure something is not getting missed here.

diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c
index 0bd01da1f0df..a15d30c494b4 100644
--- a/drivers/firmware/efi/libstub/efi-stub-helper.c
+++ b/drivers/firmware/efi/libstub/efi-stub-helper.c
@@ -643,7 +643,7 @@ static const struct {
                INITRD_EVENT_TAG_ID,
                sizeof("Linux initrd"),
        },
-       "Linux initrd",
+       { "Linux initrd" },
 };

 void efi_measure_initrd(unsigned long load_addr, unsigned long load_size)
@nathanchance nathanchance added -Wmissing-braces [BUG] linux-next This is an issue only seen in linux-next labels Dec 15, 2021
@bwendling
Copy link

It's unclear to me. The standard does mention initializing an array of unknown size in an aggregate, but doesn't appear explicit whether it should be surrounded by braces. I could see a case for both. This is something a language lawyer needs to comment on.

@nathanchance
Copy link
Member Author

Oddly enough, I don't even see where tagged_event_data is actually used... I see a comment about it in struct efi_tcg2_event in https://git.kernel.org/efi/efi/c/4da87c51705815fe1fbd41cc61640bb80da5bc54 but there is nowhere that it is called so I guess it is just there for size.

It seems like the [] is intentional, as trying to change it to u8 * such as:

diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c
index 0bd01da1f0df..b91679817725 100644
--- a/drivers/firmware/efi/libstub/efi-stub-helper.c
+++ b/drivers/firmware/efi/libstub/efi-stub-helper.c
@@ -628,7 +628,7 @@ efi_status_t efi_load_initrd_cmdline(efi_loaded_image_t *image,
 static const struct {
        efi_tcg2_event_t        event_data;
        efi_tcg2_tagged_event_t tagged_event;
-       u8                      tagged_event_data[];
+       u8                      *tagged_event_data;
 } initrd_tcg2_event = {
        {
                sizeof(initrd_tcg2_event) + sizeof("Linux initrd"),

breaks the build with both compilers (GCC below):

$ make -skj"$(nproc)" ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- distclean defconfig drivers/firmware/efi/
drivers/firmware/efi/libstub/efi-stub-helper.stub.o: absolute symbol references not allowed in the EFI stub
...

cc @ardbiesheuvel, would you be opposed to a patch adding the braces like in my comment above?

@ardbiesheuvel
Copy link

I don't mind adding the braces if GCC and Clang both accept it. I just don't see why initializing a char[] using a string literal suddenly needs an additional set of braces when the char[] is a member of a compound type.

As for the u8[] to u8* change: this is obviously incorrect. This struct is defined in the TCG PC client spec, and constitutes a API contract between the EFI stub and the firmware, so we cannot change that.

@nathanchance
Copy link
Member Author

GCC does not warn with or without braces, clang warns without: https://godbolt.org/z/3YcP5sbKq

All of GCC's documentation and nearly all of clang's tests show braces around the initializer so it seems like that is what should be happening; it looks like not having braces is probably an undocumented GCC extension.

I do see one commit in the history that changes how clang handles flexible array initialization to explicitly match GCC but the change notes that there will still be a warning in these cases so it does not sound like clang is wrong here.:

llvm/llvm-project@07d8e3a

https://llvm.org/PR3618

If I am reading it right, section 6.7.2.1 of the C standard does cover initialization of flexible member arrays but it seems like they are not allowed in the way; reading the GCC docs, it seems like this is an extension so I guess we have to go on what GCC allows.

@nickdesaulniers
Copy link
Member

@jrtc27 mentioned on IRC:

clang/test/Sema/zero-initializer.c are the cases it's trying to catch
the special case of string literals seems to be unintended fallout

@ardbiesheuvel
Copy link

I've fixed this up in the original patch. Should appear in -next tomorrow.

@nathanchance
Copy link
Member Author

Thanks a lot Ard!

ColinIanKing pushed a commit to ColinIanKing/linux-next that referenced this issue Dec 17, 2021
In an effort to ensure the initrd observed and used by the OS is
the same one that was meant to be loaded, which is difficult to
guarantee otherwise, let's measure the initrd if the EFI stub and
specifically the newly introduced LOAD_FILE2 protocol was used.

Modify the initrd loading sequence so that the contents of the initrd
are measured into PCR9.  Note that the patch is currently using
EV_EVENT_TAG to create the eventlog entry instead of EV_IPL.  According
to the TCP PC Client specification this is used for PCRs defined for OS
and application usage.

Co-developed-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Link: https://lore.kernel.org/r/20211119114745.1560453-5-ilias.apalodimas@linaro.org
[ardb: add braces to initializer of tagged_event_data]
Link: ClangBuiltLinux/linux#1547
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
@nathanchance
Copy link
Member Author

Now fixed in -next.

@nathanchance nathanchance added the [FIXED][LINUX] development cycle This bug was only present and fixed in a -next or -rc cycle label Dec 17, 2021
staging-kernelci-org pushed a commit to kernelci/linux that referenced this issue Jan 7, 2022
In an effort to ensure the initrd observed and used by the OS is
the same one that was meant to be loaded, which is difficult to
guarantee otherwise, let's measure the initrd if the EFI stub and
specifically the newly introduced LOAD_FILE2 protocol was used.

Modify the initrd loading sequence so that the contents of the initrd
are measured into PCR9.  Note that the patch is currently using
EV_EVENT_TAG to create the eventlog entry instead of EV_IPL.  According
to the TCP PC Client specification this is used for PCRs defined for OS
and application usage.

Co-developed-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Link: https://lore.kernel.org/r/20211119114745.1560453-5-ilias.apalodimas@linaro.org
[ardb: add braces to initializer of tagged_event_data]
Link: ClangBuiltLinux#1547
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-Wmissing-braces [BUG] linux-next This is an issue only seen in linux-next [FIXED][LINUX] development cycle This bug was only present and fixed in a -next or -rc cycle
Projects
None yet
Development

No branches or pull requests

4 participants