star-vx

Structuring voxels for ray-tracing
git clone git://git.meso-star.fr/star-vx.git
Log | Files | Refs | README | LICENSE

commit 7fa63e30d52fc834f9cbe8e63c446b9b807b432b
parent 8bd370eaf87fbda8bb8e73751df604328ae22e23
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 11 Oct 2023 15:52:40 +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:
MMakefile | 12++++++------
Mconfig.mk | 18+++++++++++++++---
2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/Makefile b/Makefile @@ -48,7 +48,7 @@ build_library: .config $(DEP) $(DEP) $(OBJ): config.mk $(LIBNAME_SHARED): $(OBJ) - $(CC) $(CFLAGS) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS) $(SOFLAGS) $(RSYS_LIBS) -lm + $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -o $@ $(OBJ) $(LDFLAGS_SO) $(RSYS_LIBS) -lm $(LIBNAME_STATIC): libsvx.o $(AR) -rc $@ $? @@ -65,10 +65,10 @@ libsvx.o: $(OBJ) .SUFFIXES: .c .d .o .c.d: - @$(CC) $(CFLAGS) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ + @$(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -MM -MT "$(@:.d=.o) $@" $< -MF $@ .c.o: - $(CC) $(CFLAGS) $(RSYS_CFLAGS) -DSVX_SHARED_BUILD -c $< -o $@ + $(CC) $(CFLAGS_SO) $(RSYS_CFLAGS) -DSVX_SHARED_BUILD -c $< -o $@ ################################################################################ # Installation @@ -144,11 +144,11 @@ clean_test: @$(SHELL) make.sh clean_test $(TEST_SRC) $(TEST_DEP): config.mk svx-local.pc - @$(CC) $(CFLAGS) $(RSYS_CFLAGS) $(SVX_CFLAGS) \ + @$(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(SVX_CFLAGS) \ -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ $(TEST_OBJ): config.mk svx-local.pc - $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(SVX_CFLAGS) -c $(@:.o=.c) -o $@ + $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(SVX_CFLAGS) -c $(@:.o=.c) -o $@ test_svx_bintree \ test_svx_bintree_trace_ray \ @@ -156,4 +156,4 @@ test_svx_device \ test_svx_octree \ test_svx_octree_trace_ray \ : config.mk svx-local.pc - $(CC) -o $@ src/$@.o $(SVX_LIBS) $(RSYS_LIBS) -lm + $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(SVX_LIBS) $(RSYS_LIBS) -lm diff --git a/config.mk b/config.mk @@ -40,22 +40,34 @@ 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 = $(CFLAGS_$(BUILD_TYPE)) +CFLAGS_SO = $(CFLAGS_$(BUILD_TYPE)) -fPIC +CFLAGS_EXE = $(CFLAGS_$(BUILD_TYPE)) -fPIE ################################################################################ # Linker options ################################################################################ -SOFLAGS = -shared -Wl,--no-undefined +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