commit 6236a94a6ffb5916a3cbda03ab6505174ee4f946
parent ad4e05fe6223f0777912d45531fedfbd77710bde
Author: vaplv <vaplv@free.fr>
Date: Wed, 18 Oct 2023 09:24:16 +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(+), 19 deletions(-)
diff --git a/Makefile b/Makefile
@@ -54,7 +54,7 @@ build_library: $(DEP)
$(DEP) $(OBJ): config.mk
$(LIBNAME_SHARED): $(OBJ)
- $(CC) $(CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(LIBS)
+ $(CC) $(CFLAGS_SO) -o $@ $(OBJ) $(LDFLAGS_SO) $(LIBS)
$(LIBNAME_STATIC): librsys.o
$(AR) -rc $@ $?
@@ -66,10 +66,10 @@ librsys.o: $(OBJ)
.SUFFIXES: .c .d .o
.c.d:
- @$(CC) $(CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@
+ @$(CC) $(CFLAGS_SO) -MM -MT "$(@:.d=.o) $@" $< -MF $@
.c.o:
- $(CC) $(CFLAGS) -DRSYS_SHARED_BUILD -c $< -o $@
+ $(CC) $(CFLAGS_SO) -DRSYS_SHARED_BUILD -c $< -o $@
################################################################################
# Installation
@@ -290,13 +290,13 @@ src/test_text_reader.o \
src/test_time.o \
src/test_vmacros.o \
: config.mk rsys-local.pc $(LIBNAME)
- $(CC) $(CFLAGS) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) -c $(@:.o=.c) -o $@
src/test_cstr.o: config.mk rsys-local.pc $(LIBNAME)
- $(CC) $(CFLAGS) $(RSYS_CFLAGS) -Wno-long-long -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) -Wno-long-long -c $(@:.o=.c) -o $@
src/test_condition.o src/test_mutex.o: config.mk rsys-local.pc $(LIBNAME)
- $(CC) $(CFLAGS) $(RSYS_CFLAGS) -fopenmp -c $(@:.o=.c) -o $@
+ $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) -fopenmp -c $(@:.o=.c) -o $@
test_algorithm \
test_atomic \
@@ -307,7 +307,7 @@ test_morton \
test_ref \
test_vmacros \
: config.mk
- $(CC) -o $@ src/$@.o $(LDFLAGS)
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE)
test_double22 \
test_double2 \
@@ -323,7 +323,7 @@ test_float44 \
test_float4 \
test_math \
: config.mk
- $(CC) -o $@ src/$@.o $(LDFLAGS) -lm
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) -lm
test_binary_heap\
test_cstr \
@@ -342,18 +342,18 @@ test_stretchy_array \
test_text_reader \
test_time \
: config.mk rsys-local.pc $(LIBNAME)
- $(CC) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS)
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS)
test_quaternion: config.mk rsys-local.pc $(LIBNAME)
- $(CC) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS) -lm
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS) -lm
test_condition test_mutex: config.mk rsys-local.pc $(LIBNAME)
- $(CC) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS) -lm -fopenmp
+ $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS) $(RSYS_LIBS) -lm -fopenmp
test_lib.o: src/test_library.c src/rsys.h config.mk
- $(CC) $(CFLAGS) -c src/test_library.c -DTEST_LIBRARY_BUILD_LIB -o $@
+ $(CC) $(CFLAGS_SO) -c src/test_library.c -DTEST_LIBRARY_BUILD_LIB -o $@
libtest_lib.so: test_lib.o config.mk
- $(CC) $(CFLAGS) -o $@ test_lib.o $(LDFLAGS) $(SOFLAGS)
+ $(CC) $(CFLAGS_SO) -o $@ test_lib.o $(LDFLAGS_SO)
test_library: libtest_lib.so
diff --git a/config.mk b/config.mk
@@ -33,27 +33,36 @@ WFLAGS =\
-Wconversion\
-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 = $(CFLAGS_$(BUILD_TYPE))
+CFLAGS_SO = $(CFLAGS_$(BUILD_TYPE)) -fPIC
+CFLAGS_EXE = $(CFLAGS_$(BUILD_TYPE)) -fPIE
################################################################################
# Linker options
################################################################################
LIBS = -ldl -lpthread -lm
-SOFLAGS = -shared -Wl,--no-undefined
-LDFLAGS_DEBUG =
-LDFLAGS_RELEASE = -s
-LDFLAGS = $(LDFLAGS_$(BUILD_TYPE))
+LDFLAGS_HARDENED = -Wl,-z,relro,-z,now
+LDFLAGS_DEBUG = $(LDFLAGS_HARDENED)
+LDFLAGS_RELEASE = -s $(LDFLAGS_HARDENED)
+
+LDFLAGS_SO = $(LDFLAGS_$(BUILD_TYPE)) -shared -Wl,--no-undefined
+LDFLAGS_EXE = $(LDFLAGS_$(BUILD_TYPE)) -pie
OCPFLAGS_DEBUG = --localize-hidden
OCPFLAGS_RELEASE = --localize-hidden --strip-unneeded