2023-05-31 00:35:28

by Kees Cook

[permalink] [raw]
Subject: [PATCH] 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.

Reported-by: Thorsten Leemhuis <[email protected]>
Closes: https://lore.kernel.org/lkml/[email protected]/
Bisected-by: "Joan Bruguera Micó" <[email protected]>
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: [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]>
Signed-off-by: Kees Cook <[email protected]>
---
arch/x86/purgatory/Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/purgatory/Makefile b/arch/x86/purgatory/Makefile
index 82fec66d46d2..005324d6c76b 100644
--- a/arch/x86/purgatory/Makefile
+++ b/arch/x86/purgatory/Makefile
@@ -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.
--
2.34.1



2023-05-31 08:21:30

by Thorsten Leemhuis

[permalink] [raw]
Subject: Re: [PATCH] x86/purgatory: Do not use fortified string functions

Kees, thx for looking into this.

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

On 31.05.23 02:33, Kees Cook wrote:
> 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.
>
> Reported-by: Thorsten Leemhuis <[email protected]>
> Closes: https://lore.kernel.org/lkml/[email protected]/

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

Tested-by: Thorsten Leemhuis <[email protected]>

Ciao, Thorsten

2023-06-01 16:55:25

by Dave Hansen

[permalink] [raw]
Subject: Re: [PATCH] x86/purgatory: Do not use fortified string functions

On 5/30/23 17:33, Kees Cook wrote:
> 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.
>
> Reported-by: Thorsten Leemhuis <[email protected]>
> Closes: https://lore.kernel.org/lkml/[email protected]/
> Bisected-by: "Joan Bruguera Micó" <[email protected]>
> 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:

Acked-by: Dave Hansen <[email protected]>

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.

2023-06-01 16:55:37

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] x86/purgatory: Do not use fortified string functions

On Thu, Jun 01, 2023 at 09:45:57AM -0700, Dave Hansen wrote:
> On 5/30/23 17:33, Kees Cook wrote:
> > 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.
> >
> > Reported-by: Thorsten Leemhuis <[email protected]>
> > Closes: https://lore.kernel.org/lkml/[email protected]/
> > Bisected-by: "Joan Bruguera Mic?" <[email protected]>
> > 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

Correct.

> 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:
>
> Acked-by: Dave Hansen <[email protected]>

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

--
Kees Cook

2023-06-01 17:25:23

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] x86/purgatory: Do not use fortified string functions

On Tue, 30 May 2023 17:33:48 -0700, Kees Cook wrote:
> 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

--
Kees Cook