htmie

Optical properties of water droplets
git clone git://git.meso-star.fr/htmie.git
Log | Files | Refs | README | LICENSE

commit 2f4e026a546e00ea1c4243314407b0f21ad6879c
parent 40776d0efa38f50cfdf85e2c097bc5d2b0dba285
Author: Vincent Forest <vincent.forest@meso-star.com>
Date:   Wed, 11 Oct 2023 11:16:23 +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 | 20++++++++++++++------
2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/Makefile b/Makefile @@ -42,7 +42,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): $(OBJ) $(AR) -rc $@ $? @@ -57,10 +57,10 @@ $(LIBNAME_STATIC): $(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) -DHTMIE_SHARED_BUILD -c $< -o $@ + $(CC) $(CFLAGS_SO) $(DPDC_CFLAGS) -DHTMIE_SHARED_BUILD -c $< -o $@ ################################################################################ # Installation @@ -167,11 +167,11 @@ clean_test: rm -rf $(TEST_DUMMY_VARS) $(TEST_DUMMY_NETCDF) $(TEST_DEP): config.mk htmie-local.pc - @$(CC) $(CFLAGS) $(RSYS_CFLAGS) $(HTMIE_CFLAGS) \ + @$(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(HTMIE_CFLAGS) \ -MM -MT "$(@:.d=.o) $@" $(@:.d=.c) -MF $@ $(TEST_OBJ): config.mk htmie-local.pc - $(CC) $(CFLAGS) $(RSYS_CFLAGS) $(HTMIE_CFLAGS) -c $(@:.o=.c) -o $@ + $(CC) $(CFLAGS_EXE) $(RSYS_CFLAGS) $(HTMIE_CFLAGS) -c $(@:.o=.c) -o $@ test_htmie test_htmie_load: config.mk htmie-local.pc $(LIBNAME) - $(CC) -o $@ src/$@.o $(RSYS_LIBS) $(HTMIE_LIBS) -lm + $(CC) $(CFLAGS_EXE) -o $@ src/$@.o $(LDFLAGS_EXE) $(RSYS_LIBS) $(HTMIE_LIBS) -lm diff --git a/config.mk b/config.mk @@ -45,23 +45,31 @@ 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_DEBUG = -LDFLAGS_RELEASE = -s -LDFLAGS = $(LDFLAGS_$(BUILD_TYPE)) +LDFLAGS_SO = $(LDFLAGS_$(BUILD_TYPE)) -shared -Wl,--no-undefined +LDFLAGS_EXE = $(LDFLAGS_$(BUILD_TYPE)) -pie