commit 92132701438f041daa5eaee9f96300f8fedbfb50
parent ba779062580dd8cf61182d7a8c750c8fd6b82894
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Thu, 19 Oct 2023 15:57:59 +0200
Make generated binaries safer and more robust
Define the CFLAGS_HARDENED and LDFLAGS_HARDENED macros, which list
compiler and linker options that activate various hardening features
aimed at increasing the security and robustness of generated binaries.
The link editor options have all been available since at least ld 2.25.
So you don't have to worry about compatibility issues.
The compiler options are in fact some of those that will be enabled by
the -fhardened option to be introduced in GCC 14. In the following, we
list them and indicate the version of GCC from which they are documented
in the manual, i.e. from which version of GCC they would appear to be
available:
-D_FORTIFY_SOURCE [GCC 5.5]
-fcf-protection options [GCC 8.5]
-fstack-protector-strong [GCC 6.5]
-fstack-clash-protection [GCC 8.5]
-ftrivial-auto-var-init [GCC 12.3]
The latter, -ftrivial-auto-var-init, is too recent. To avoid any
compatibility problems, we haven't activated it yet.
Diffstat:
2 files changed, 28 insertions(+), 16 deletions(-)
diff --git a/Makefile b/Makefile
@@ -44,7 +44,7 @@ build_library: .config $(DEP)
$(DEP) $(OBJ): config.mk
$(LIBNAME_SHARED): $(OBJ)
- $(CC) $(CFLAGS) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(DPDC_LIBS)
+ $(CC) $(CFLAGS_SO) $(DPDC_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(DPDC_LIBS)
$(LIBNAME_STATIC): libsmc.o
$(AR) -rc $@ $?
@@ -63,10 +63,10 @@ libsmc.o: $(OBJ)
.SUFFIXES: .c .d .o
.c.d:
- @$(CC) $(CFLAGS) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+ @$(CC) $(CFLAGS_SO) $(DPDC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
.c.o:
- $(CC) $(CFLAGS) $(DPDC_CFLAGS) -DSMC_SHARED_BUILD -c $< -o $@
+ $(CC) $(CFLAGS_SO) $(DPDC_CFLAGS) -DSMC_SHARED_BUILD -c $< -o $@
################################################################################
# Installation
@@ -157,25 +157,26 @@ clean_test:
@$(SHELL) make.sh clean_test $(TEST_SRC) $(TEST_SRC_S3D)
$(TEST_DEP): config.mk smc-local.pc
- @$(CC) $(CFLAGS) $(SMC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+ @$(CC) $(CFLAGS_EXE) $(SMC_CFLAGS) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
$(TEST_OBJ): config.mk smc-local.pc
- $(CC) $(CFLAGS) $(SMC_CFLAGS) -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_EXE) $(SMC_CFLAGS) -c $(@:.o=.c) -o $@
test_smc_device: config.mk smc-local.pc
- $(CC) -o $@ src/$@.o $(LDFLAGS) $(SMC_LIBS) -fopenmp
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(SMC_LIBS) -fopenmp
test_smc_doubleN \
test_smc_errors \
test_smc_solve \
: config.mk smc-local.pc
- $(CC) -o $@ src/$@.o $(LDFLAGS) $(SMC_LIBS) -lm
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(SMC_LIBS) -lm
$(TEST_DEP_S3D): config.mk smc-local.pc
- @$(CC) $(CFLAGS) $(SMC_CFLAGS) $(S3D_CFLAGS) -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
+ @$(CC) $(CFLAGS_EXE) $(SMC_CFLAGS) $(S3D_CFLAGS) \
+ -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@
$(TEST_OBJ_S3D): config.mk smc-local.pc
- $(CC) $(CFLAGS) $(SMC_CFLAGS) $(S3D_CFLAGS) -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_EXE) $(SMC_CFLAGS) $(S3D_CFLAGS) -c $(@:.o=.c) -o $@
test_smc_light_path: config.mk smc-local.pc
- $(CC) $(CFLAGS) $(SMC_CFLAGS) $(S3D_CFLAGS) -o $@ src/$@.o $(LDFLAGS) $(SMC_LIBS) $(S3D_LIBS) -lm
+ $(CC) $(CFLAGS_EXE) $(SMC_CFLAGS) $(S3D_CFLAGS) -o $@ src/$@.o $(LDFLAGS_EXE) $(SMC_LIBS) $(S3D_LIBS) -lm
diff --git a/config.mk b/config.mk
@@ -51,27 +51,38 @@ WFLAGS =\
-Wmissing-prototypes\
-Wshadow
+CFLAGS_HARDENED =\
+ -D_FORTIFY_SOURCES=2\
+ -fcf-protection=full\
+ -fstack-clash-protection\
+ -fstack-protector-strong
+
CFLAGS_COMMON =\
-std=c89\
-pedantic\
- -fPIC\
-fvisibility=hidden\
-fstrict-aliasing\
+ $(CFLAGS_HARDENED)\
$(WFLAGS)
-CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON)
CFLAGS_DEBUG = -g $(CFLAGS_COMMON)
+CFLAGS_RELEASE = -O2 -DNDEBUG $(CFLAGS_COMMON)
CFLAGS = $(CFLAGS_$(BUILD_TYPE))
+CFLAGS_SO = $(CFLAGS) -fPIC
+CFLAGS_EXE = $(CFLAGS) -fPIE
+
################################################################################
# Linker options
################################################################################
-SOFLAGS = -shared -Wl,--no-undefined
-
-LDFLAGS_DEBUG =
-LDFLAGS_RELEASE = -s
+LDFLAGS_HARDENED = -Wl,-z,relro,-z,now
+LDFLAGS_DEBUG = $(LDFLAGS_HARDENED)
+LDFLAGS_RELEASE = -s $(LDFLAGS_HARDENED)
LDFLAGS = $(LDFLAGS_$(BUILD_TYPE))
+LDFLAGS_SO = $(LDFLAGS) -shared -Wl,--no-undefined
+LDFLAGS_EXE = $(LDFLAGS) -pie
+
OCPFLAGS_DEBUG = --localize-hidden
OCPFLAGS_RELEASE = --localize-hidden --strip-unneeded
OCPFLAGS = $(OCPFLAGS_$(BUILD_TYPE))