Skip to contentSkip to conversation

hardening: Enable i386 FORTIFY_SOURCE on Clang 16+

PATCH In review Kees Cook 3 participants 7 messages Mar 8, 2025

Subsystems (3)
lore
Share & tools
lore

Kees Cook

author maintainer
#0 lore raw

v2 cover hardening: Enable i386 FORTIFY_SOURCE on Clang 16+

Hi,

This drops -ffreestanding for i386 unconditionally for GCC, and on Clang version 16 and later. FORTIFY_SOURCE depends on the libcall optimizations made without -ffreestanding on Clang. On GCC, there is no expected differences. With that done, it's possible to gain Clang FORTIFY_SOURCE coverage on i386 again, though only for Clang 16+.

-Kees

v1: https://lore.kernel.org/lkml/[email protected]/
v2: drop -ffreestanding (Nathan)

Kees Cook (2):
x86/build: Remove -ffreestanding on i386 with GCC
hardening: Enable i386 FORTIFY_SOURCE on Clang 16+

 arch/x86/Makefile          | 2 ++
 security/Kconfig.hardening | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH v2 1/2 x86/build: Remove -ffreestanding on i386 with GCC

The use of -ffreestanding is a leftover that is only needed for certain versions of Clang. Adjust this to be Clang-only. A later patch will make this a versioned check.

---
Cc: Nathan Chancellor <[email protected]>
Cc: Nick Desaulniers <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: "H. Peter Anvin" <[email protected]>

---
 arch/x86/Makefile | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/Makefile +2 -0
index 5b773b34768d..d25ed25fb7d9 100644
@@ -137,8 +137,10 @@ ifeq ($(CONFIG_X86_32),y)
         include $(srctree)/arch/x86/Makefile_32.cpu
         KBUILD_CFLAGS += $(cflags-y)
 
+    ifeq ($(CONFIG_CC_IS_CLANG),y)
         # temporary until string.h is fixed
         KBUILD_CFLAGS += -ffreestanding
+    endif
 
     ifeq ($(CONFIG_STACKPROTECTOR),y)
         ifeq ($(CONFIG_SMP),y)
Signature · 1 line
2.34.1
Acked-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] x86/build: Remove -ffreestanding on i386 with GCC

Quoted 23 lines · Kees Cook

The use of -ffreestanding is a leftover that is only needed for certain versions of Clang. Adjust this to be Clang-only. A later patch will make this a versioned check.

---
Cc: Nathan Chancellor <[email protected]>
Cc: Nick Desaulniers <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: "H. Peter Anvin" <[email protected]>

---
 arch/x86/Makefile | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/Makefile +0 -0
index 5b773b34768d..d25ed25fb7d9 100644

arch/x86/Makefile ifeq ($(CONFIG_X86_32),y) · line 143

Quoted 7 lines
arch/x86/Makefile +2 -0
@@ -137,8 +137,10 @@ ifeq ($(CONFIG_X86_32),y)
         include $(srctree)/arch/x86/Makefile_32.cpu
         KBUILD_CFLAGS += $(cflags-y)
 
+    ifeq ($(CONFIG_CC_IS_CLANG),y)
         # temporary until string.h is fixed
         KBUILD_CFLAGS += -ffreestanding
+    endif

Ingo

Reviewed-by on v2 1/2
#3 lore raw

Re: [PATCH v2 1/2] x86/build: Remove -ffreestanding on i386 with GCC

In reply to Kees Cook
Quoted 5 lines · Kees Cook

The use of -ffreestanding is a leftover that is only needed for certain versions of Clang. Adjust this to be Clang-only. A later patch will make this a versioned check.

This could also adjust the comment but it is probably not that big of a deal since the "temporary" is already pretty stale.

Quoted 31 lines

---
Cc: Nathan Chancellor <[email protected]>
Cc: Nick Desaulniers <[email protected]>
Cc: Thomas Gleixner <[email protected]>
Cc: Ingo Molnar <[email protected]>
Cc: Borislav Petkov <[email protected]>
Cc: Dave Hansen <[email protected]>
Cc: [email protected]
Cc: "H. Peter Anvin" <[email protected]>

---
 arch/x86/Makefile | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/Makefile +2 -0
index 5b773b34768d..d25ed25fb7d9 100644
@@ -137,8 +137,10 @@ ifeq ($(CONFIG_X86_32),y)
         include $(srctree)/arch/x86/Makefile_32.cpu
         KBUILD_CFLAGS += $(cflags-y)
 
+    ifeq ($(CONFIG_CC_IS_CLANG),y)
         # temporary until string.h is fixed
         KBUILD_CFLAGS += -ffreestanding
+    endif
 
     ifeq ($(CONFIG_STACKPROTECTOR),y)
         ifeq ($(CONFIG_SMP),y)
Signature · 1 line
2.34.1
author maintainer
#4 lore raw

Re: [PATCH v2 1/2] x86/build: Remove -ffreestanding on i386 with GCC

Quoted 9 lines · Nathan Chancellor
Quoted 5 lines · Kees Cook

The use of -ffreestanding is a leftover that is only needed for certain versions of Clang. Adjust this to be Clang-only. A later patch will make this a versioned check.

This could also adjust the comment but it is probably not that big of a deal since the "temporary" is already pretty stale.

Ah yeah, good call. I'll fix that up locally.

Quoted 2 lines

Thanks!

-Kees

Signature · 1 line
Kees Cook
author maintainer
#5 lore raw

PATCH v2 2/2 hardening: Enable i386 FORTIFY_SOURCE on Clang 16+

In reply to Kees Cook

The i386 regparm bug exposed with FORTIFY_SOURCE with Clang was fixed in Clang 16[1].

---
 arch/x86/Makefile          | 2 +-
 security/Kconfig.hardening | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/Makefile +1 -1
index d25ed25fb7d9..917459d1ad40 100644
@@ -137,7 +137,7 @@ ifeq ($(CONFIG_X86_32),y)
         include $(srctree)/arch/x86/Makefile_32.cpu
         KBUILD_CFLAGS += $(cflags-y)
 
-    ifeq ($(CONFIG_CC_IS_CLANG),y)
+    ifneq ($(call clang-min-version, 160000),y)
         # temporary until string.h is fixed
         KBUILD_CFLAGS += -ffreestanding
     endif
security/Kconfig.hardening +1 -1
index 23ffb0d7c845..c17366ce8224 100644
@@ -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.
Signature · 1 line
2.34.1
Reviewed-by on v2 2/2
#6 lore raw
Quoted 5 lines · Kees Cook

The i386 regparm bug exposed with FORTIFY_SOURCE with Clang was fixed in Clang 16[1].

Quoted 34 lines
---
 arch/x86/Makefile          | 2 +-
 security/Kconfig.hardening | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/Makefile +1 -1
index d25ed25fb7d9..917459d1ad40 100644
@@ -137,7 +137,7 @@ ifeq ($(CONFIG_X86_32),y)
         include $(srctree)/arch/x86/Makefile_32.cpu
         KBUILD_CFLAGS += $(cflags-y)
 
-    ifeq ($(CONFIG_CC_IS_CLANG),y)
+    ifneq ($(call clang-min-version, 160000),y)
         # temporary until string.h is fixed
         KBUILD_CFLAGS += -ffreestanding
     endif
security/Kconfig.hardening +1 -1
index 23ffb0d7c845..c17366ce8224 100644
@@ -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.
Signature · 1 line
2.34.1