[PATCH] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+

Kees Cook posted 1 patch 11 months, 1 week ago
There is a newer version of this series
security/Kconfig.hardening | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
Posted by Kees Cook 11 months, 1 week ago
The i386 regparm bug exposed with FORTIFY_SOURCE with Clang was fixed
in Clang 16[1].

Link: https://github.com/llvm/llvm-project/commit/c167c0a4dcdb998affb2756ce76903a12f7d8ca5 [1]
Signed-off-by: Kees Cook <kees@kernel.org>
---
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
Cc: llvm@lists.linux.dev
Cc: linux-hardening@vger.kernel.org
---
 security/Kconfig.hardening | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/security/Kconfig.hardening b/security/Kconfig.hardening
index 23ffb0d7c845..c17366ce8224 100644
--- a/security/Kconfig.hardening
+++ b/security/Kconfig.hardening
@@ -286,7 +286,7 @@ config FORTIFY_SOURCE
 	bool "Harden common str/mem functions against buffer overflows"
 	depends on ARCH_HAS_FORTIFY_SOURCE
 	# https://github.com/llvm/llvm-project/issues/53645
-	depends on !CC_IS_CLANG || !X86_32
+	depends on !X86_32 || !CC_IS_CLANG || CLANG_VERSION >= 160000
 	help
 	  Detect overflows of buffers in common string and memory functions
 	  where the compiler can determine and validate the buffer sizes.
-- 
2.34.1
Re: [PATCH] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
Posted by Kees Cook 11 months, 1 week ago
On Mon, 03 Mar 2025 13:49:37 -0800, Kees Cook wrote:
> The i386 regparm bug exposed with FORTIFY_SOURCE with Clang was fixed
> in Clang 16[1].
> 
> 

Applied to for-next/hardening, thanks!

[1/1] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
      https://git.kernel.org/kees/c/3e5820429980

Take care,

-- 
Kees Cook
Re: [PATCH] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
Posted by Nathan Chancellor 11 months ago
On Tue, Mar 04, 2025 at 09:50:44AM -0800, Kees Cook wrote:
> On Mon, 03 Mar 2025 13:49:37 -0800, Kees Cook wrote:
> > The i386 regparm bug exposed with FORTIFY_SOURCE with Clang was fixed
> > in Clang 16[1].
> > 
> > 
> 
> Applied to for-next/hardening, thanks!
> 
> [1/1] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
>       https://git.kernel.org/kees/c/3e5820429980

Turns out this is actually incomplete based on my testing, I see the
following warnings with ARCH=i386 allmodconfig with all supported clang
versions:

  warning: unsafe strcpy() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-strcpy-lit.c
  warning: unsafe strcpy() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-strcpy.c

We also need to drop '-ffreestanding' from arch/x86/Makefile (which Nick
has mentioned in [1]). Time to revive [2]? :) or just do it in this
patch, since it sounds like there was no regression with GCC?

[1]: https://github.com/ClangBuiltLinux/linux/issues/1583#issuecomment-1123016466
[2]: https://lore.kernel.org/20200817220212.338670-5-ndesaulniers@google.com/

Cheers,
Nathan
Re: [PATCH] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
Posted by Kees Cook 11 months ago

On March 7, 2025 1:47:34 PM PST, Nathan Chancellor <nathan@kernel.org> wrote:
>On Tue, Mar 04, 2025 at 09:50:44AM -0800, Kees Cook wrote:
>> On Mon, 03 Mar 2025 13:49:37 -0800, Kees Cook wrote:
>> > The i386 regparm bug exposed with FORTIFY_SOURCE with Clang was fixed
>> > in Clang 16[1].
>> > 
>> > 
>> 
>> Applied to for-next/hardening, thanks!
>> 
>> [1/1] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
>>       https://git.kernel.org/kees/c/3e5820429980
>
>Turns out this is actually incomplete based on my testing, I see the
>following warnings with ARCH=i386 allmodconfig with all supported clang
>versions:
>
>  warning: unsafe strcpy() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-strcpy-lit.c
>  warning: unsafe strcpy() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-strcpy.c
>
>We also need to drop '-ffreestanding' from arch/x86/Makefile (which Nick
>has mentioned in [1]). Time to revive [2]? :) or just do it in this
>patch, since it sounds like there was no regression with GCC?
>
>[1]: https://github.com/ClangBuiltLinux/linux/issues/1583#issuecomment-1123016466
>[2]: https://lore.kernel.org/20200817220212.338670-5-ndesaulniers@google.com/

Oh good catch! Probably I need to do this in two patches just to be safe:
- make -ffreestanding be Clang only
- switch to version checks for FORTIFY and freestanding

That will split the changes for GCC and Clang into separate patches. Or maybe that's overkill?

-Kees

-- 
Kees Cook
Re: [PATCH] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
Posted by Nathan Chancellor 11 months ago
On Fri, Mar 07, 2025 at 02:57:06PM -0800, Kees Cook wrote:
> 
> 
> On March 7, 2025 1:47:34 PM PST, Nathan Chancellor <nathan@kernel.org> wrote:
> >On Tue, Mar 04, 2025 at 09:50:44AM -0800, Kees Cook wrote:
> >> On Mon, 03 Mar 2025 13:49:37 -0800, Kees Cook wrote:
> >> > The i386 regparm bug exposed with FORTIFY_SOURCE with Clang was fixed
> >> > in Clang 16[1].
> >> > 
> >> > 
> >> 
> >> Applied to for-next/hardening, thanks!
> >> 
> >> [1/1] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
> >>       https://git.kernel.org/kees/c/3e5820429980
> >
> >Turns out this is actually incomplete based on my testing, I see the
> >following warnings with ARCH=i386 allmodconfig with all supported clang
> >versions:
> >
> >  warning: unsafe strcpy() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-strcpy-lit.c
> >  warning: unsafe strcpy() usage lacked '__write_overflow' symbol in lib/test_fortify/write_overflow-strcpy.c
> >
> >We also need to drop '-ffreestanding' from arch/x86/Makefile (which Nick
> >has mentioned in [1]). Time to revive [2]? :) or just do it in this
> >patch, since it sounds like there was no regression with GCC?
> >
> >[1]: https://github.com/ClangBuiltLinux/linux/issues/1583#issuecomment-1123016466
> >[2]: https://lore.kernel.org/20200817220212.338670-5-ndesaulniers@google.com/
> 
> Oh good catch! Probably I need to do this in two patches just to be safe:
> - make -ffreestanding be Clang only
> - switch to version checks for FORTIFY and freestanding
> 
> That will split the changes for GCC and Clang into separate patches. Or maybe that's overkill?

No, I think that is reasonable, as any regressions with GCC would be
noticed by the first change, rather than being mixed in with the second.
It should also make it easier to drop that when the minimum version of
clang gets bumped to 16 (either for the whole kernel or just x86, like
the bump to 15 this dev cycle for the recent stack protector fixes in
-top).

Cheers,
Nathan
Re: [PATCH] hardening: Enable i386 FORTIFY_SOURCE on Clang 16+
Posted by Nathan Chancellor 11 months, 1 week ago
On Mon, Mar 03, 2025 at 01:49:37PM -0800, Kees Cook wrote:
> The i386 regparm bug exposed with FORTIFY_SOURCE with Clang was fixed
> in Clang 16[1].
> 
> Link: https://github.com/llvm/llvm-project/commit/c167c0a4dcdb998affb2756ce76903a12f7d8ca5 [1]
> Signed-off-by: Kees Cook <kees@kernel.org>

Yes, thank you for catching this!

Reviewed-by: Nathan Chancellor <nathan@kernel.org>

I assume you'll take this?

> ---
> Cc: Nathan Chancellor <nathan@kernel.org>
> Cc: Nick Desaulniers <ndesaulniers@google.com>
> Cc: Bill Wendling <morbo@google.com>
> Cc: Justin Stitt <justinstitt@google.com>
> Cc: "Gustavo A. R. Silva" <gustavoars@kernel.org>
> Cc: llvm@lists.linux.dev
> Cc: linux-hardening@vger.kernel.org
> ---
>  security/Kconfig.hardening | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/security/Kconfig.hardening b/security/Kconfig.hardening
> index 23ffb0d7c845..c17366ce8224 100644
> --- a/security/Kconfig.hardening
> +++ b/security/Kconfig.hardening
> @@ -286,7 +286,7 @@ config FORTIFY_SOURCE
>  	bool "Harden common str/mem functions against buffer overflows"
>  	depends on ARCH_HAS_FORTIFY_SOURCE
>  	# https://github.com/llvm/llvm-project/issues/53645
> -	depends on !CC_IS_CLANG || !X86_32
> +	depends on !X86_32 || !CC_IS_CLANG || CLANG_VERSION >= 160000
>  	help
>  	  Detect overflows of buffers in common string and memory functions
>  	  where the compiler can determine and validate the buffer sizes.
> -- 
> 2.34.1
>