Re: [PATCH v2] security: disable FORTIFY_SOURCE on clang
I took a bit to poke Clang here. Building an arbitrary file with `CONFIG_FORTIFY_SOURCE=y`, none of the functions in this range https://github.com/ClangBuiltLinux/linux/blob/0bee0cece/include/linux/string.h#L274-L468 have FORTIFY'ed definitions emitted by clang, i.e., the added FORTIFY checks aren't helping. Happy to check other functions elsewhere if they exist, but given that this entire block seems to be a functional nop... Reviewed-by: George Burgess IV On Tue, May 5, 2020 at 8:53 PM Nathan Chancellor wrote: > > On Tue, May 05, 2020 at 07:54:09PM -0700, Kees Cook wrote: > > On Tue, May 05, 2020 at 06:14:53PM -0600, Jason A. Donenfeld wrote: > > > clang-10 has a broken optimization stage that doesn't allow the > > > compiler to prove at compile time that certain memcpys are within > > > bounds, and thus the outline memcpy is always called, resulting in > > > horrific performance, and in some cases, excessive stack frame growth. > > > Here's a simple reproducer: > > > > > > typedef unsigned long size_t; > > > void *c(void *dest, const void *src, size_t n) __asm__("memcpy"); > > > extern inline __attribute__((gnu_inline)) void *memcpy(void *dest, > > > const void *src, size_t n) { return c(dest, src, n); } > > > void blah(char *a) > > > { > > > unsigned long long b[10], c[10]; > > > int i; > > > > > > memcpy(b, a, sizeof(b)); > > > for (i = 0; i < 10; ++i) > > > c[i] = b[i] ^ b[9 - i]; > > > for (i = 0; i < 10; ++i) > > > b[i] = c[i] ^ a[i]; > > > memcpy(a, b, sizeof(b)); > > > } > > > > > > Compile this with clang-9 and clang-10 and observe: > > > > > > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-10 > > > -Wframe-larger-than=0 -O3 -c b.c -o c10.o > > > b.c:5:6: warning: stack frame size of 104 bytes in function 'blah' > > > [-Wframe-larger-than=] > > > void blah(char *a) > > > ^ > > > 1 warning generated. > > > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-9 > > > -Wframe-larger-than=0 -O3 -c b.c -o c9.o > > > > > > Looking at the disassembly of c10.o and c9.o, one can see that c9.o is > > > properly optimized in the obvious way one would expect, while c10.o has > > > blown up and includes extern calls to memcpy. > > > > > > But actually, for versions of clang earlier than 10, fortify source > > > mostly does nothing. So, between being broken and doing nothing, it > > > probably doesn't make sense to pretend to offer this option. So, this > > > commit just disables it entirely when compiling with clang. > > > > > > Cc: Arnd Bergmann > > > Cc: LKML > > > Cc: clang-built-linux > > > Cc: Kees Cook > > > Cc: George Burgess > > > Cc: Nick Desaulniers > > > Link: https://bugs.llvm.org/show_bug.cgi?id=45802 > > > Signed-off-by: Jason A. Donenfeld > > > > Grudgingly, > > > > Reviewed-by: Kees Cook > > > > -- > > Kees Cook > > > > I feel like you should finish your investigation into how broken this > actually is before we give it the hammer like this but if it is going > in regardless... > > Reviewed-by: Nathan Chancellor
Re: [PATCH v2] security: disable FORTIFY_SOURCE on clang
On Tue, May 05, 2020 at 07:54:09PM -0700, Kees Cook wrote: > On Tue, May 05, 2020 at 06:14:53PM -0600, Jason A. Donenfeld wrote: > > clang-10 has a broken optimization stage that doesn't allow the > > compiler to prove at compile time that certain memcpys are within > > bounds, and thus the outline memcpy is always called, resulting in > > horrific performance, and in some cases, excessive stack frame growth. > > Here's a simple reproducer: > > > > typedef unsigned long size_t; > > void *c(void *dest, const void *src, size_t n) __asm__("memcpy"); > > extern inline __attribute__((gnu_inline)) void *memcpy(void *dest, > > const void *src, size_t n) { return c(dest, src, n); } > > void blah(char *a) > > { > > unsigned long long b[10], c[10]; > > int i; > > > > memcpy(b, a, sizeof(b)); > > for (i = 0; i < 10; ++i) > > c[i] = b[i] ^ b[9 - i]; > > for (i = 0; i < 10; ++i) > > b[i] = c[i] ^ a[i]; > > memcpy(a, b, sizeof(b)); > > } > > > > Compile this with clang-9 and clang-10 and observe: > > > > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-10 > > -Wframe-larger-than=0 -O3 -c b.c -o c10.o > > b.c:5:6: warning: stack frame size of 104 bytes in function 'blah' > > [-Wframe-larger-than=] > > void blah(char *a) > > ^ > > 1 warning generated. > > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-9 > > -Wframe-larger-than=0 -O3 -c b.c -o c9.o > > > > Looking at the disassembly of c10.o and c9.o, one can see that c9.o is > > properly optimized in the obvious way one would expect, while c10.o has > > blown up and includes extern calls to memcpy. > > > > But actually, for versions of clang earlier than 10, fortify source > > mostly does nothing. So, between being broken and doing nothing, it > > probably doesn't make sense to pretend to offer this option. So, this > > commit just disables it entirely when compiling with clang. > > > > Cc: Arnd Bergmann > > Cc: LKML > > Cc: clang-built-linux > > Cc: Kees Cook > > Cc: George Burgess > > Cc: Nick Desaulniers > > Link: https://bugs.llvm.org/show_bug.cgi?id=45802 > > Signed-off-by: Jason A. Donenfeld > > Grudgingly, > > Reviewed-by: Kees Cook > > -- > Kees Cook > I feel like you should finish your investigation into how broken this actually is before we give it the hammer like this but if it is going in regardless... Reviewed-by: Nathan Chancellor
Re: [PATCH v2] security: disable FORTIFY_SOURCE on clang
On Tue, May 5, 2020 at 8:54 PM Kees Cook wrote: > > On Tue, May 05, 2020 at 06:14:53PM -0600, Jason A. Donenfeld wrote: > > clang-10 has a broken optimization stage that doesn't allow the > > compiler to prove at compile time that certain memcpys are within > > bounds, and thus the outline memcpy is always called, resulting in > > horrific performance, and in some cases, excessive stack frame growth. > > Here's a simple reproducer: > > > > typedef unsigned long size_t; > > void *c(void *dest, const void *src, size_t n) __asm__("memcpy"); > > extern inline __attribute__((gnu_inline)) void *memcpy(void *dest, > > const void *src, size_t n) { return c(dest, src, n); } > > void blah(char *a) > > { > > unsigned long long b[10], c[10]; > > int i; > > > > memcpy(b, a, sizeof(b)); > > for (i = 0; i < 10; ++i) > > c[i] = b[i] ^ b[9 - i]; > > for (i = 0; i < 10; ++i) > > b[i] = c[i] ^ a[i]; > > memcpy(a, b, sizeof(b)); > > } > > > > Compile this with clang-9 and clang-10 and observe: > > > > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-10 > > -Wframe-larger-than=0 -O3 -c b.c -o c10.o > > b.c:5:6: warning: stack frame size of 104 bytes in function 'blah' > > [-Wframe-larger-than=] > > void blah(char *a) > > ^ > > 1 warning generated. > > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-9 > > -Wframe-larger-than=0 -O3 -c b.c -o c9.o > > > > Looking at the disassembly of c10.o and c9.o, one can see that c9.o is > > properly optimized in the obvious way one would expect, while c10.o has > > blown up and includes extern calls to memcpy. > > > > But actually, for versions of clang earlier than 10, fortify source > > mostly does nothing. So, between being broken and doing nothing, it > > probably doesn't make sense to pretend to offer this option. So, this > > commit just disables it entirely when compiling with clang. > > > > Cc: Arnd Bergmann > > Cc: LKML > > Cc: clang-built-linux > > Cc: Kees Cook > > Cc: George Burgess > > Cc: Nick Desaulniers > > Link: https://bugs.llvm.org/show_bug.cgi?id=45802 > > Signed-off-by: Jason A. Donenfeld > > Grudgingly, > > Reviewed-by: Kees Cook Do you want to take this into your tree to send to Linus? Seems like security kconfig switches is in line with your usual submissions.
Re: [PATCH v2] security: disable FORTIFY_SOURCE on clang
On Tue, May 05, 2020 at 06:14:53PM -0600, Jason A. Donenfeld wrote: > clang-10 has a broken optimization stage that doesn't allow the > compiler to prove at compile time that certain memcpys are within > bounds, and thus the outline memcpy is always called, resulting in > horrific performance, and in some cases, excessive stack frame growth. > Here's a simple reproducer: > > typedef unsigned long size_t; > void *c(void *dest, const void *src, size_t n) __asm__("memcpy"); > extern inline __attribute__((gnu_inline)) void *memcpy(void *dest, const > void *src, size_t n) { return c(dest, src, n); } > void blah(char *a) > { > unsigned long long b[10], c[10]; > int i; > > memcpy(b, a, sizeof(b)); > for (i = 0; i < 10; ++i) > c[i] = b[i] ^ b[9 - i]; > for (i = 0; i < 10; ++i) > b[i] = c[i] ^ a[i]; > memcpy(a, b, sizeof(b)); > } > > Compile this with clang-9 and clang-10 and observe: > > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-10 > -Wframe-larger-than=0 -O3 -c b.c -o c10.o > b.c:5:6: warning: stack frame size of 104 bytes in function 'blah' > [-Wframe-larger-than=] > void blah(char *a) > ^ > 1 warning generated. > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-9 > -Wframe-larger-than=0 -O3 -c b.c -o c9.o > > Looking at the disassembly of c10.o and c9.o, one can see that c9.o is > properly optimized in the obvious way one would expect, while c10.o has > blown up and includes extern calls to memcpy. > > But actually, for versions of clang earlier than 10, fortify source > mostly does nothing. So, between being broken and doing nothing, it > probably doesn't make sense to pretend to offer this option. So, this > commit just disables it entirely when compiling with clang. > > Cc: Arnd Bergmann > Cc: LKML > Cc: clang-built-linux > Cc: Kees Cook > Cc: George Burgess > Cc: Nick Desaulniers > Link: https://bugs.llvm.org/show_bug.cgi?id=45802 > Signed-off-by: Jason A. Donenfeld Grudgingly, Reviewed-by: Kees Cook -- Kees Cook
Re: [PATCH v2] security: disable FORTIFY_SOURCE on clang
On Tue, May 5, 2020 at 5:15 PM Jason A. Donenfeld wrote: > > clang-10 has a broken optimization stage that doesn't allow the > compiler to prove at compile time that certain memcpys are within > bounds, and thus the outline memcpy is always called, resulting in > horrific performance, and in some cases, excessive stack frame growth. > Here's a simple reproducer: > > typedef unsigned long size_t; > void *c(void *dest, const void *src, size_t n) __asm__("memcpy"); > extern inline __attribute__((gnu_inline)) void *memcpy(void *dest, const > void *src, size_t n) { return c(dest, src, n); } > void blah(char *a) > { > unsigned long long b[10], c[10]; > int i; > > memcpy(b, a, sizeof(b)); > for (i = 0; i < 10; ++i) > c[i] = b[i] ^ b[9 - i]; > for (i = 0; i < 10; ++i) > b[i] = c[i] ^ a[i]; > memcpy(a, b, sizeof(b)); > } > > Compile this with clang-9 and clang-10 and observe: > > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-10 > -Wframe-larger-than=0 -O3 -c b.c -o c10.o > b.c:5:6: warning: stack frame size of 104 bytes in function 'blah' > [-Wframe-larger-than=] > void blah(char *a) > ^ > 1 warning generated. > zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-9 > -Wframe-larger-than=0 -O3 -c b.c -o c9.o > > Looking at the disassembly of c10.o and c9.o, one can see that c9.o is > properly optimized in the obvious way one would expect, while c10.o has > blown up and includes extern calls to memcpy. > > But actually, for versions of clang earlier than 10, fortify source > mostly does nothing. So, between being broken and doing nothing, it > probably doesn't make sense to pretend to offer this option. So, this > commit just disables it entirely when compiling with clang. > > Cc: Arnd Bergmann > Cc: LKML > Cc: clang-built-linux > Cc: Kees Cook > Cc: George Burgess > Cc: Nick Desaulniers > Link: https://bugs.llvm.org/show_bug.cgi?id=45802 > Signed-off-by: Jason A. Donenfeld Acked-by: Nick Desaulniers > --- > security/Kconfig | 1 + > 1 file changed, 1 insertion(+) > > diff --git a/security/Kconfig b/security/Kconfig > index cd3cc7da3a55..76bcfb3eb16f 100644 > --- a/security/Kconfig > +++ b/security/Kconfig > @@ -191,6 +191,7 @@ config HARDENED_USERCOPY_PAGESPAN > config FORTIFY_SOURCE > bool "Harden common str/mem functions against buffer overflows" > depends on ARCH_HAS_FORTIFY_SOURCE > + depends on !CC_IS_CLANG > help > Detect overflows of buffers in common string and memory functions > where the compiler can determine and validate the buffer sizes. > -- > 2.26.2 > > -- > You received this message because you are subscribed to the Google Groups > "Clang Built Linux" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clang-built-linux+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/clang-built-linux/20200506001453.764332-1-Jason%40zx2c4.com. -- Thanks, ~Nick Desaulniers
[PATCH v2] security: disable FORTIFY_SOURCE on clang
clang-10 has a broken optimization stage that doesn't allow the compiler to prove at compile time that certain memcpys are within bounds, and thus the outline memcpy is always called, resulting in horrific performance, and in some cases, excessive stack frame growth. Here's a simple reproducer: typedef unsigned long size_t; void *c(void *dest, const void *src, size_t n) __asm__("memcpy"); extern inline __attribute__((gnu_inline)) void *memcpy(void *dest, const void *src, size_t n) { return c(dest, src, n); } void blah(char *a) { unsigned long long b[10], c[10]; int i; memcpy(b, a, sizeof(b)); for (i = 0; i < 10; ++i) c[i] = b[i] ^ b[9 - i]; for (i = 0; i < 10; ++i) b[i] = c[i] ^ a[i]; memcpy(a, b, sizeof(b)); } Compile this with clang-9 and clang-10 and observe: zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-10 -Wframe-larger-than=0 -O3 -c b.c -o c10.o b.c:5:6: warning: stack frame size of 104 bytes in function 'blah' [-Wframe-larger-than=] void blah(char *a) ^ 1 warning generated. zx2c4@thinkpad /tmp/curve25519-hacl64-stack-frame-size-test $ clang-9 -Wframe-larger-than=0 -O3 -c b.c -o c9.o Looking at the disassembly of c10.o and c9.o, one can see that c9.o is properly optimized in the obvious way one would expect, while c10.o has blown up and includes extern calls to memcpy. But actually, for versions of clang earlier than 10, fortify source mostly does nothing. So, between being broken and doing nothing, it probably doesn't make sense to pretend to offer this option. So, this commit just disables it entirely when compiling with clang. Cc: Arnd Bergmann Cc: LKML Cc: clang-built-linux Cc: Kees Cook Cc: George Burgess Cc: Nick Desaulniers Link: https://bugs.llvm.org/show_bug.cgi?id=45802 Signed-off-by: Jason A. Donenfeld --- security/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/security/Kconfig b/security/Kconfig index cd3cc7da3a55..76bcfb3eb16f 100644 --- a/security/Kconfig +++ b/security/Kconfig @@ -191,6 +191,7 @@ config HARDENED_USERCOPY_PAGESPAN config FORTIFY_SOURCE bool "Harden common str/mem functions against buffer overflows" depends on ARCH_HAS_FORTIFY_SOURCE + depends on !CC_IS_CLANG help Detect overflows of buffers in common string and memory functions where the compiler can determine and validate the buffer sizes. -- 2.26.2