From f9cfda9a72e53acf3df284bce1b46a236f182e3d Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Sat, 28 Feb 2026 13:30:41 +0100 Subject: [PATCH] Add hardening flags (NOHARDENING=1 to disable them) and fix Windows stringop-overflow --- Makefile.defs | 82 +++++++++++++++++++++++++++++++----------- client/Makefile | 34 +++++++++--------- client/src/cmdparser.c | 5 +-- 3 files changed, 78 insertions(+), 43 deletions(-) diff --git a/Makefile.defs b/Makefile.defs index 35b29cca7..56e68b3a9 100644 --- a/Makefile.defs +++ b/Makefile.defs @@ -68,22 +68,40 @@ export INSTALLSUDO platform = $(shell uname) DETECTED_OS=$(platform) +ifneq ($(findstring MINGW,$(platform)),) + IS_WINDOWS := 1 + IS_MINGW := 1 +endif +ifneq ($(findstring MSYS,$(platform)),) + IS_WINDOWS := 1 + IS_MSYS := 1 +endif +ifeq ($(platform),Darwin) + IS_DARWIN := 1 + ifeq ($(shell uname -p),arm64) + IS_IOS := 1 + else + IS_MACOS := 1 + endif +endif + ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1) DETECTED_COMPILER = clang else DETECTED_COMPILER = gcc endif -ifeq ($(platform),Darwin) - ifeq ($(shell uname -p),arm64) - # The platform is iOS - USE_BREW ?= 0 - # iOS refuses to compile unless this is set - export IPHONEOS_DEPLOYMENT_TARGET=11.0 - else - # M* macOS devices return arm - USE_BREW ?= 1 - endif +ifdef IS_IOS + # The platform is iOS + USE_BREW ?= 0 + # iOS refuses to compile unless this is set + export IPHONEOS_DEPLOYMENT_TARGET=11.0 +endif +ifdef IS_MACOS + # M* macOS devices return arm + USE_BREW ?= 1 +endif +ifdef IS_DARWIN USE_MACPORTS ?= 0 AR= /usr/bin/ar rcs RANLIB= /usr/bin/ranlib @@ -108,8 +126,8 @@ ifeq ($(USE_MACPORTS),1) endif ifeq ($(DEBUG),1) - DEFCXXFLAGS = -g -O0 -pipe - DEFCFLAGS = -g -O0 -fstrict-aliasing -pipe + DEFCXXFLAGS = -ggdb3 -O0 -pipe + DEFCFLAGS = -ggdb3 -O0 -fstrict-aliasing -pipe DEFLDFLAGS = else DEFCXXFLAGS = -Wall -Werror -O3 -pipe @@ -129,18 +147,18 @@ ifeq ($(SANITIZE),1) endif # Some more warnings we want as errors: DEFCFLAGS += -Wbad-function-cast -Wredundant-decls -Wmissing-prototypes -Wchar-subscripts -Wshadow -Wundef -Wwrite-strings -Wunused -Wuninitialized -Wpointer-arith -Winline -Wformat -Wformat-security -Winit-self -Wmissing-include-dirs -Wnested-externs -Wmissing-declarations -Wempty-body -Wignored-qualifiers -Wmissing-field-initializers -Wtype-limits -Wold-style-definition -Wcast-align -Wswitch-enum -# GCC 10 has issues with false positives on stringop-overflow, let's disable them for now (cf https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92955, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94335) -# beware these flags didn't exist for GCC < 7 -ifeq ($(shell expr $(CC_VERSION) \>= 10), 1) +# GCC 10 to 13 had issues with false positives on stringop-overflow (cf https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92955, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94335) +ifeq ($(shell expr $(CC_VERSION) \>= 14), 1) ifneq ($(DETECTED_COMPILER), clang) - DEFCFLAGS += -Wno-stringop-overflow -Wno-error=stringop-overflow + DEFCFLAGS += -Wstringop-overread -Wstringop-overflow endif endif -ifeq ($(platform),Darwin) - ifeq ($(shell uname -p),arm64) - # iOS will refuse to compile without the minimum target of iOS 11.0 - DEFCFLAGS += -mios-version-min=11.0 - endif + +ifdef IS_IOS + # iOS will refuse to compile without the minimum target of iOS 11.0 + DEFCFLAGS += -mios-version-min=11.0 +endif +ifdef IS_DARWIN # their readline has strict-prototype issues DEFCFLAGS += -Wno-strict-prototypes # some warnings about braced initializers on structs we want to ignore @@ -149,6 +167,28 @@ else DEFCFLAGS += -Wstrict-prototypes endif +ifneq ($(NOHARDENING),1) + # Manual equivalents of -fhardened to avoid g++: warning: linker hardening options not enabled by ‘-fhardened’ because other link options were specified on the command line + DEFCFLAGS += \ + -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -fstack-protector-strong -ftrivial-auto-var-init=zero + ifndef IS_WINDOWS + ifndef IS_DARWIN + ifeq ($(ARCH),x86_64) + CFLAGS += -fcf-protection=full + endif + DEFCFLAGS += -fPIE + DEFLDFLAGS += -Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack -pie + endif + endif + ifdef IS_DARWIN + DEFLDFLAGS += -Wl,-bind_at_load # equivalent of -z,now + DEFLDFLAGS += -Wl,-pie # ASLR + endif + ifeq ($(DETECTED_COMPILER), gcc) + DEFCFLAGS += -fstack-clash-protection + endif +endif + # Next ones are activated only if GCCEXTRA=1 or CLANGEXTRA=1 EXTRACFLAGS = EXTRACFLAGS += -Wunused-parameter -Wno-error=unused-parameter diff --git a/client/Makefile b/client/Makefile index 654e5ea0a..d1d75dbce 100644 --- a/client/Makefile +++ b/client/Makefile @@ -100,10 +100,10 @@ LUALIBINC = -I$(LUALIBPATH) LUALIB = $(LUALIBPATH)/liblua.a LUALIBLD = LUAPLATFORM = generic -ifneq (,$(findstring MINGW,$(platform))) +ifdef IS_MINGW LUAPLATFORM = mingw else - ifeq ($(platform),Darwin) + ifdef IS_DARWIN LUAPLATFORM = macosx else LUALIBLD += -ldl @@ -112,8 +112,8 @@ else endif ## Winsock2 -ifneq (,$(findstring MINGW,$(platform))) - LDLIBS += -lws2_32 +ifdef IS_MINGW + LDLIBS += -lws2_32 endif ## Reveng @@ -262,7 +262,7 @@ PM3INCLUDES += $(MQTTLIBINC) # RPi Zero gcc requires -latomic # but MacOSX /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld # doesn't recognize option --as-needed -ifneq ($(platform),Darwin) +ifndef IS_DARWIN LDLIBS += -Wl,--as-needed -latomic -Wl,--no-as-needed endif @@ -433,7 +433,7 @@ PM3CFLAGS += -I./src -I./include -I../include -I../common -I../common_fpga $(PM3 #PM3CFLAGS += -std=c11 -pedantic PREFIX ?= /usr/local -ifneq (,$(findstring MINGW,$(platform))) +ifdef IS_MINGW # Mingw uses by default Microsoft printf, we want the GNU printf (e.g. for %z) # and setting _ISOC99_SOURCE sets internally __USE_MINGW_ANSI_STDIO=1 # FTR __USE_MINGW_ANSI_STDIO seems deprecated in Mingw32 @@ -496,14 +496,12 @@ LDFLAGS ?= $(DEFLDFLAGS) LDFLAGS += $(MYLDFLAGS) PM3LDFLAGS = $(LDFLAGS) -ifeq ($(platform),Darwin) - ifeq ($(shell uname -p),arm64) - # The platform is iOS - PM3LDFLAGS += -framework Foundation -framework UIKit - else - # M* macOS devices return arm - PM3LDFLAGS += -framework Foundation -framework AppKit - endif +ifdef IS_IOS + PM3LDFLAGS += -framework Foundation -framework UIKit +endif +ifdef IS_MACOS + # M* macOS devices return arm + PM3LDFLAGS += -framework Foundation -framework AppKit endif ################### @@ -840,8 +838,8 @@ else CXXSRCS = guidummy.cpp endif -# OS X -ifeq ($(platform),Darwin) +# OS X & iOS +ifdef IS_DARWIN OBJCSRCS = util_darwin.m endif @@ -885,7 +883,7 @@ src/ui/ui_overlays.h: src/ui/overlays.ui $(info [-] UIC $@) $(Q)$(UIC) $^ > $@ # fix the header generated by some Qt6 versions (seen on 6.4.2 & 6.6.3) -ifeq ($(platform),Darwin) +ifdef IS_DARWIN $(Q)sed -E -i '' 's/&QLabel::setNum/qOverload(\&QLabel::setNum)/g' $@ else $(Q)sed -i 's/&QLabel::setNum/qOverload(\&QLabel::setNum)/g' $@ @@ -931,7 +929,7 @@ endif ifneq (,$(INSTALLSHARE)) $(Q)$(INSTALLSUDO) $(MKDIR) $(DESTDIR)$(PREFIX)$(PATHSEP)$(INSTALLSHARERELPATH) # hack ahead: inject installation path into pm3_resources.py - ifeq ($(platform),Darwin) + ifdef IS_DARWIN $(Q)sed -E -i '' 's|^TOOLS_PATH \?= \?None|TOOLS_PATH="$(PREFIX)$(PATHSEP)$(INSTALLTOOLSRELPATH)"|' pyscripts/pm3_resources.py $(Q)sed -E -i '' 's|^DICTS_PATH \?= \?None|DICTS_PATH="$(PREFIX)$(PATHSEP)$(INSTALLSHARERELPATH)/dictionaries"|' pyscripts/pm3_resources.py $(Q)$(INSTALLSUDO) $(CP) $(INSTALLSHARE) $(DESTDIR)$(PREFIX)$(PATHSEP)$(INSTALLSHARERELPATH) diff --git a/client/src/cmdparser.c b/client/src/cmdparser.c index 16fbeba6c..66ac21d79 100644 --- a/client/src/cmdparser.c +++ b/client/src/cmdparser.c @@ -239,10 +239,7 @@ static int execute_system_command(const char *command) { #if defined(_WIN32) char wrapped_command[255]; - strncat(wrapped_command, "cmd /C \"", 9); - strncat(wrapped_command, command, strlen(command)); - strncat(wrapped_command, "\"", 2); - + snprintf(wrapped_command, sizeof(wrapped_command), "cmd /C \"%s\"", command); ret = system(wrapped_command); #else ret = system(command);