Skip to content

Commit cdc34cb

Browse files
arndbIngo Molnar
authored andcommitted
x86/boot/tboot: Avoid Wstringop-overread-warning
gcc-11 warns about using string operations on pointers that are defined at compile time as offsets from a NULL pointer. Unfortunately that also happens on the result of fix_to_virt(), which is a compile-time constant for a constant input: arch/x86/kernel/tboot.c: In function 'tboot_probe': arch/x86/kernel/tboot.c:70:13: error: '__builtin_memcmp_eq' specified bound 16 exceeds source size 0 [-Werror=stringop-overread] 70 | if (memcmp(&tboot_uuid, &tboot->uuid, sizeof(tboot->uuid))) { | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I hope this can get addressed in gcc-11 before the release. As a workaround, split up the tboot_probe() function in two halves to separate the pointer generation from the usage. This is a bit ugly, and hopefully gcc understands that the code is actually correct before it learns to peek into the noinline function. Signed-off-by: Arnd Bergmann <[email protected]> Signed-off-by: Ingo Molnar <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Martin Sebor <[email protected]> Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99578 Link: https://lore.kernel.org/r/[email protected]
1 parent 8419639 commit cdc34cb

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

arch/x86/kernel/tboot.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ bool tboot_enabled(void)
4949
return tboot != NULL;
5050
}
5151

52+
/* noinline to prevent gcc from warning about dereferencing constant fixaddr */
53+
static noinline __init bool check_tboot_version(void)
54+
{
55+
if (memcmp(&tboot_uuid, &tboot->uuid, sizeof(tboot->uuid))) {
56+
pr_warn("tboot at 0x%llx is invalid\n", boot_params.tboot_addr);
57+
return false;
58+
}
59+
60+
if (tboot->version < 5) {
61+
pr_warn("tboot version is invalid: %u\n", tboot->version);
62+
return false;
63+
}
64+
65+
pr_info("found shared page at phys addr 0x%llx:\n",
66+
boot_params.tboot_addr);
67+
pr_debug("version: %d\n", tboot->version);
68+
pr_debug("log_addr: 0x%08x\n", tboot->log_addr);
69+
pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
70+
pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
71+
pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
72+
73+
return true;
74+
}
75+
5276
void __init tboot_probe(void)
5377
{
5478
/* Look for valid page-aligned address for shared page. */
@@ -66,25 +90,9 @@ void __init tboot_probe(void)
6690

6791
/* Map and check for tboot UUID. */
6892
set_fixmap(FIX_TBOOT_BASE, boot_params.tboot_addr);
69-
tboot = (struct tboot *)fix_to_virt(FIX_TBOOT_BASE);
70-
if (memcmp(&tboot_uuid, &tboot->uuid, sizeof(tboot->uuid))) {
71-
pr_warn("tboot at 0x%llx is invalid\n", boot_params.tboot_addr);
93+
tboot = (void *)fix_to_virt(FIX_TBOOT_BASE);
94+
if (!check_tboot_version())
7295
tboot = NULL;
73-
return;
74-
}
75-
if (tboot->version < 5) {
76-
pr_warn("tboot version is invalid: %u\n", tboot->version);
77-
tboot = NULL;
78-
return;
79-
}
80-
81-
pr_info("found shared page at phys addr 0x%llx:\n",
82-
boot_params.tboot_addr);
83-
pr_debug("version: %d\n", tboot->version);
84-
pr_debug("log_addr: 0x%08x\n", tboot->log_addr);
85-
pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
86-
pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
87-
pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
8896
}
8997

9098
static pgd_t *tboot_pg_dir;

0 commit comments

Comments
 (0)