Skip to contentSkip to conversation

riscv/purgatory: Do not use fortified string functions

PATCH In review Kees Cook 2 participants 3 messages May 31, 2023 – Jun 1, 2023 RISC-V

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 riscv/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 Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Albert Ou <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Conor Dooley <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Alyssa Ross <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
---
 arch/riscv/purgatory/Makefile | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/purgatory/Makefile +3 -3
index 5730797a6b40..11f4c275f141 100644
@@ -31,9 +31,9 @@ $(obj)/strncmp.o: $(srctree)/arch/riscv/lib/strncmp.S FORCE
 $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
-CFLAGS_sha256.o := -D__DISABLE_EXPORTS
-CFLAGS_string.o := -D__DISABLE_EXPORTS
-CFLAGS_ctype.o := -D__DISABLE_EXPORTS
+CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_string.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_ctype.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 · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#1 lore raw

Hey Kees,

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 15 lines
Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Albert Ou <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Conor Dooley <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Alyssa Ross <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>

Perhaps this is indicative of other issues in RISC-V land, but allmodconfig complains about this patch:

../lib/string.c:17: warning: "__NO_FORTIFY" redefined ../lib/string.c:17:9: warning: preprocessor token __NO_FORTIFY redefined

Quoted 29 lines
---
 arch/riscv/purgatory/Makefile | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/purgatory/Makefile +3 -3
index 5730797a6b40..11f4c275f141 100644
@@ -31,9 +31,9 @@ $(obj)/strncmp.o: $(srctree)/arch/riscv/lib/strncmp.S FORCE
 $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
-CFLAGS_sha256.o := -D__DISABLE_EXPORTS
-CFLAGS_string.o := -D__DISABLE_EXPORTS
-CFLAGS_ctype.o := -D__DISABLE_EXPORTS
+CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_string.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_ctype.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 · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
author
#2 lore raw
Quoted 33 lines · Conor Dooley

Hey Kees,

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 15 lines
Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Paul Walmsley <[email protected]>
Cc Palmer Dabbelt <[email protected]>
Cc Albert Ou <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc Conor Dooley <[email protected]>
Cc Nick Desaulniers <[email protected]>
Cc Alyssa Ross <[email protected]>
Cc Heiko Stuebner <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Quoted 5 lines

Perhaps this is indicative of other issues in RISC-V land, but allmodconfig complains about this patch:

../lib/string.c:17: warning: "__NO_FORTIFY" redefined ../lib/string.c:17:9: warning: preprocessor token __NO_FORTIFY redefined

Argh, thanks. My compile test clearly failed. I'll fix this up.

-Kees

Quoted 30 lines
Quoted 30 lines
---
 arch/riscv/purgatory/Makefile | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/riscv/purgatory/Makefile +3 -3
index 5730797a6b40..11f4c275f141 100644
@@ -31,9 +31,9 @@ $(obj)/strncmp.o: $(srctree)/arch/riscv/lib/strncmp.S FORCE
 $(obj)/sha256.o: $(srctree)/lib/crypto/sha256.c FORCE
 	$(call if_changed_rule,cc_o_c)
 
-CFLAGS_sha256.o := -D__DISABLE_EXPORTS
-CFLAGS_string.o := -D__DISABLE_EXPORTS
-CFLAGS_ctype.o := -D__DISABLE_EXPORTS
+CFLAGS_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_string.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
+CFLAGS_ctype.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 · 7 lines
2.34.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Signature · 1 line
Kees Cook