Skip to contentSkip to conversation

x86/purgatory: Do not use fortified string functions

PATCH Applied Kees Cook 3 participants 5 messages May 31, 2023 – Jun 1, 2023 x86

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 x86/purgatory: Do not use fortified string functions

With the addition of -fstrict-flex-arrays=3, struct sha256_state's trailing array is no longer ignored by CONFIG_FORTIFY_SOURCE:

struct sha256_state {
        u32 state[SHA256_DIGEST_SIZE / 4];
        u64 count;
        u8 buf[SHA256_BLOCK_SIZE];
};

This means that the memcpy() calls with "buf" as a destination in sha256.c's code will attempt to perform run-time bounds checking, which could lead to calling missing functions, specifically a potential WARN_ONCE, which isn't callable from purgatory.

Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Dave Hansen <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc "Peter Zijlstra (Intel)" <[email protected]>
Cc Alyssa Ross <[email protected]>
Cc Sami Tolvanen <[email protected]>
Cc Alexander Potapenko <[email protected]>
---
 arch/x86/purgatory/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/purgatory/Makefile +1 -1
index 82fec66d46d2..005324d6c76b 100644
@@ -12,7 +12,7 @@ $(obj)/string.o: $(srctree)/arch/x86/boot/compressed/string.c FORCE
 $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
-CFLAGS_sha256.o := -D__DISABLE_EXPORTS
+CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
 
 # When linking purgatory.ro with -r unresolved symbols are not checked,
 # also link a purgatory.chk binary without -r to check for unresolved symbols.
Signature · 1 line
2.34.1
Tested-by on v1 1/1
#1 lore raw

Kees, thx for looking into this.

Joan, thx for bisecting. I should have done this earlier myself...

Quoted 9 lines · Kees Cook

With the addition of -fstrict-flex-arrays=3, struct sha256_state's trailing array is no longer ignored by CONFIG_FORTIFY_SOURCE:

struct sha256_state {
        u32 state[SHA256_DIGEST_SIZE / 4];
        u64 count;
        u8 buf[SHA256_BLOCK_SIZE];
};
Quoted 7 lines

This means that the memcpy() calls with "buf" as a destination in sha256.c's code will attempt to perform run-time bounds checking, which could lead to calling missing functions, specifically a potential WARN_ONCE, which isn't callable from purgatory.

Did a test build and booted it in a Vm, everything seems fine. So thx again and feel free to add:

Ciao, Thorsten

Acked-by on v1 1/1
#2 lore raw
In reply to Kees Cook
Quoted 14 lines · Kees Cook

With the addition of -fstrict-flex-arrays=3, struct sha256_state's trailing array is no longer ignored by CONFIG_FORTIFY_SOURCE:

struct sha256_state {
        u32 state[SHA256_DIGEST_SIZE / 4];
        u64 count;
        u8 buf[SHA256_BLOCK_SIZE];
};

This means that the memcpy() calls with "buf" as a destination in sha256.c's code will attempt to perform run-time bounds checking, which could lead to calling missing functions, specifically a potential WARN_ONCE, which isn't callable from purgatory.

Quoted 6 lines
Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>

Hi Folks,

The -fstrict-flex-arrays=3 commit isn't upstream yet, right? That makes it a _bit_ wonky for us to carry this on the x86 side since among other things, the Fixes commit doesn't exist. I'd be fine if this goes up along with the -fstrict-flex-arrays=3 code, so:

We could also pick it up from the x86 side, but I think that would need a _bit_ of a different commit message to allude to it being to prepare for the _future_ setting of -fstrict-flex-arrays=3 and having no practical benefits now.

Let me know if you don't want to send this up with the -fstrict-flex-arrays=3 set.

author
#3 lore raw
Quoted 22 lines · Dave Hansen
Quoted 14 lines · Kees Cook

With the addition of -fstrict-flex-arrays=3, struct sha256_state's trailing array is no longer ignored by CONFIG_FORTIFY_SOURCE:

struct sha256_state {
        u32 state[SHA256_DIGEST_SIZE / 4];
        u64 count;
        u8 buf[SHA256_BLOCK_SIZE];
};

This means that the memcpy() calls with "buf" as a destination in sha256.c's code will attempt to perform run-time bounds checking, which could lead to calling missing functions, specifically a potential WARN_ONCE, which isn't callable from purgatory.

Quoted 6 lines
Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Thomas Gleixner <[email protected]>
Cc Ingo Molnar <[email protected]>
Quoted 3 lines

Hi Folks,

The -fstrict-flex-arrays=3 commit isn't upstream yet, right? That makes

Correct.

Quoted 5 lines

it a _bit_ wonky for us to carry this on the x86 side since among other things, the Fixes commit doesn't exist. I'd be fine if this goes up along with the -fstrict-flex-arrays=3 code, so:

That would be perfect; thank you! I've added it to my tree.

Signature · 1 line
Kees Cook
author Applied
#4 lore raw
In reply to Kees Cook
Quoted 10 lines · Kees Cook

With the addition of -fstrict-flex-arrays=3, struct sha256_state's trailing array is no longer ignored by CONFIG_FORTIFY_SOURCE:

struct sha256_state {
        u32 state[SHA256_DIGEST_SIZE / 4];
        u64 count;
        u8 buf[SHA256_BLOCK_SIZE];
};

[...]

Applied to for-next/hardening, thanks!

[1/1] x86/purgatory: Do not use fortified string functions

      https://git.kernel.org/kees/c/f6ab7fc96a53
Signature · 1 line
Kees Cook