2023-06-01 16:19:18

by Kees Cook

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

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: 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]>
Cc: [email protected]
Signed-off-by: Kees Cook <[email protected]>
---
v2: - only limit fortify for sha256 (conor)
v1: https://lore.kernel.org/lkml/[email protected]
---
arch/riscv/purgatory/Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

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

--
2.34.1



2023-06-01 17:57:15

by Conor Dooley

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

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 <[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: Paul Walmsley <[email protected]>
> Cc: Palmer Dabbelt <[email protected]>
> Cc: Albert Ou <[email protected]>
> Cc: Masahiro Yamada <[email protected]>
> Cc: Conor Dooley <[email protected]>

Reviewed-by: Conor Dooley <[email protected]>

Thanks for the quick update Kees,
Conor.


Attachments:
(No filename) (1.19 kB)
signature.asc (235.00 B)
Download all attachments

2023-06-01 18:45:18

by Kees Cook

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

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

--
Kees Cook


2023-06-01 20:19:54

by Palmer Dabbelt

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

On Thu, 01 Jun 2023 11:27:03 PDT (-0700), [email protected] 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.

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

Hello:

This patch was applied to riscv/linux.git (fixes)
by Palmer Dabbelt <[email protected]>:

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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



2023-06-01 21:01:50

by Kees Cook

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

On Thu, Jun 01, 2023 at 01:17:03PM -0700, Palmer Dabbelt wrote:
> On Thu, 01 Jun 2023 11:27:03 PDT (-0700), [email protected] 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.

--
Kees Cook

2023-06-01 21:17:31

by Palmer Dabbelt

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

On Thu, 01 Jun 2023 13:31:33 PDT (-0700), [email protected] wrote:
> On Thu, Jun 01, 2023 at 01:17:03PM -0700, Palmer Dabbelt wrote:
>> On Thu, 01 Jun 2023 11:27:03 PDT (-0700), [email protected] 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 <[email protected]>
Acked-by: Palmer Dabbelt <[email protected]>

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