diff mbox series

[v2] riscv/purgatory: Do not use fortified string functions

Message ID 20230601160025.gonna.868-kees@kernel.org (mailing list archive)
State Mainlined
Commit 8762606ae22e71ec65249cdbf809e3dc7ea8ea1e
Headers show
Series [v2] riscv/purgatory: Do not use fortified string functions | expand

Commit Message

Kees Cook June 1, 2023, 4 p.m. UTC
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 <linux@leemhuis.info>
Closes: https://lore.kernel.org/lkml/175578ec-9dec-7a9c-8d3a-43f24ff86b92@leemhuis.info/
Bisected-by: "Joan Bruguera Micó" <joanbrugueram@gmail.com>
Fixes: df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc: Paul Walmsley <paul.walmsley@sifive.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Masahiro Yamada <masahiroy@kernel.org>
Cc: Conor Dooley <conor.dooley@microchip.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Alyssa Ross <hi@alyssa.is>
Cc: Heiko Stuebner <heiko.stuebner@vrull.eu>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: linux-riscv@lists.infradead.org
Signed-off-by: Kees Cook <keescook@chromium.org>
---
v2: - only limit fortify for sha256 (conor)
v1: https://lore.kernel.org/lkml/20230531003404.never.167-kees@kernel.org
---
 arch/riscv/purgatory/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Conor Dooley June 1, 2023, 5:34 p.m. UTC | #1
On Thu, Jun 01, 2023 at 09:00:28AM -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];
> };
> 
> 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 <linux@leemhuis.info>
> Closes: https://lore.kernel.org/lkml/175578ec-9dec-7a9c-8d3a-43f24ff86b92@leemhuis.info/
> Bisected-by: "Joan Bruguera Micó" <joanbrugueram@gmail.com>
> Fixes: df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
> Cc: Paul Walmsley <paul.walmsley@sifive.com>
> Cc: Palmer Dabbelt <palmer@dabbelt.com>
> Cc: Albert Ou <aou@eecs.berkeley.edu>
> Cc: Masahiro Yamada <masahiroy@kernel.org>
> Cc: Conor Dooley <conor.dooley@microchip.com>

Reviewed-by: Conor Dooley <conor.dooley@microchip.com>

Thanks for the quick update Kees,
Conor.
Kees Cook June 1, 2023, 6:27 p.m. UTC | #2
On Thu, 1 Jun 2023 09:00:28 -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] riscv/purgatory: Do not use fortified string functions
      https://git.kernel.org/kees/c/ca2ca08f479d
Palmer Dabbelt June 1, 2023, 8:17 p.m. UTC | #3
On Thu, 01 Jun 2023 11:27:03 PDT (-0700), keescook@chromium.org wrote:
> On Thu, 1 Jun 2023 09:00:28 -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] riscv/purgatory: Do not use fortified string functions
>       https://git.kernel.org/kees/c/ca2ca08f479d

Sorry, I'd just applied this to riscv/fixes as well.  I can drop it if 
you want?  I was going to send a PR tomorrow, just LMK.
patchwork-bot+linux-riscv@kernel.org June 1, 2023, 8:20 p.m. UTC | #4
Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <palmer@rivosinc.com>:

On Thu,  1 Jun 2023 09:00:28 -0700 you 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];
> };
> 
> [...]

Here is the summary with links:
  - [v2] riscv/purgatory: Do not use fortified string functions
    https://git.kernel.org/riscv/c/0f84097ab015

You are awesome, thank you!
Kees Cook June 1, 2023, 8:31 p.m. UTC | #5
On Thu, Jun 01, 2023 at 01:17:03PM -0700, Palmer Dabbelt wrote:
> On Thu, 01 Jun 2023 11:27:03 PDT (-0700), keescook@chromium.org wrote:
> > On Thu, 1 Jun 2023 09:00:28 -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] riscv/purgatory: Do not use fortified string functions
> >       https://git.kernel.org/kees/c/ca2ca08f479d
> 
> Sorry, I'd just applied this to riscv/fixes as well.  I can drop it if you
> want?  I was going to send a PR tomorrow, just LMK.

I'm fine either way. I was carrying each arch's fix just since it was
related to the -fstrict-flex-arrays=3 patch in the hardening tree.
Palmer Dabbelt June 1, 2023, 9:04 p.m. UTC | #6
On Thu, 01 Jun 2023 13:31:33 PDT (-0700), keescook@chromium.org wrote:
> On Thu, Jun 01, 2023 at 01:17:03PM -0700, Palmer Dabbelt wrote:
>> On Thu, 01 Jun 2023 11:27:03 PDT (-0700), keescook@chromium.org wrote:
>> > On Thu, 1 Jun 2023 09:00:28 -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] riscv/purgatory: Do not use fortified string functions
>> >       https://git.kernel.org/kees/c/ca2ca08f479d
>>
>> Sorry, I'd just applied this to riscv/fixes as well.  I can drop it if you
>> want?  I was going to send a PR tomorrow, just LMK.
>
> I'm fine either way. I was carrying each arch's fix just since it was
> related to the -fstrict-flex-arrays=3 patch in the hardening tree.

Works for me, I'll drop it.

Reviewed-by: Palmer Dabbelt <palmer@rivosinc.com>
Acked-by: Palmer Dabbelt <palmer@rivosinc.com>

The bots will probably get confussed and it was briefly visible to 
linux-next, so there might be a bit of spam.
diff mbox series

Patch

diff --git a/arch/riscv/purgatory/Makefile b/arch/riscv/purgatory/Makefile
index 5730797a6b40..8c73360c42bb 100644
--- a/arch/riscv/purgatory/Makefile
+++ b/arch/riscv/purgatory/Makefile
@@ -31,7 +31,7 @@  $(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_sha256.o := -D__DISABLE_EXPORTS -D__NO_FORTIFY
 CFLAGS_string.o := -D__DISABLE_EXPORTS
 CFLAGS_ctype.o := -D__DISABLE_EXPORTS