Add hardening flags (NOHARDENING=1 to disable them) and fix Windows stringop-overflow

This commit is contained in:
Philippe Teuwen committed 2026-02-28 16:22:44 +01:00
1 parent 0e5ca3a796
commit f9cfda9a72
3 files changed
+78 -43

No files matched your search

+61 -21
View File
@@ -68,22 +68,40 @@ export INSTALLSUDO
platform = $(shell uname) platform = $(shell uname)
DETECTED_OS=$(platform) 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) ifeq ($(shell $(CC) -v 2>&1 | grep -c "clang version"), 1)
DETECTED_COMPILER = clang DETECTED_COMPILER = clang
else else
DETECTED_COMPILER = gcc DETECTED_COMPILER = gcc
endif endif
ifeq ($(platform),Darwin) ifdef IS_IOS
ifeq ($(shell uname -p),arm64) # The platform is iOS
# The platform is iOS USE_BREW ?= 0
USE_BREW ?= 0 # iOS refuses to compile unless this is set
# iOS refuses to compile unless this is set export IPHONEOS_DEPLOYMENT_TARGET=11.0
export IPHONEOS_DEPLOYMENT_TARGET=11.0 endif
else ifdef IS_MACOS
# M* macOS devices return arm # M* macOS devices return arm
USE_BREW ?= 1 USE_BREW ?= 1
endif endif
ifdef IS_DARWIN
USE_MACPORTS ?= 0 USE_MACPORTS ?= 0
AR= /usr/bin/ar rcs AR= /usr/bin/ar rcs
RANLIB= /usr/bin/ranlib RANLIB= /usr/bin/ranlib
@@ -108,8 +126,8 @@ ifeq ($(USE_MACPORTS),1)
endif endif
ifeq ($(DEBUG),1) ifeq ($(DEBUG),1)
DEFCXXFLAGS = -g -O0 -pipe DEFCXXFLAGS = -ggdb3 -O0 -pipe
DEFCFLAGS = -g -O0 -fstrict-aliasing -pipe DEFCFLAGS = -ggdb3 -O0 -fstrict-aliasing -pipe
DEFLDFLAGS = DEFLDFLAGS =
else else
DEFCXXFLAGS = -Wall -Werror -O3 -pipe DEFCXXFLAGS = -Wall -Werror -O3 -pipe
@@ -129,18 +147,18 @@ ifeq ($(SANITIZE),1)
endif endif
# Some more warnings we want as errors: # 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 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) # 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)
# beware these flags didn't exist for GCC < 7 ifeq ($(shell expr $(CC_VERSION) \>= 14), 1)
ifeq ($(shell expr $(CC_VERSION) \>= 10), 1)
ifneq ($(DETECTED_COMPILER), clang) ifneq ($(DETECTED_COMPILER), clang)
DEFCFLAGS += -Wno-stringop-overflow -Wno-error=stringop-overflow DEFCFLAGS += -Wstringop-overread -Wstringop-overflow
endif endif
endif endif
ifeq ($(platform),Darwin)
ifeq ($(shell uname -p),arm64) ifdef IS_IOS
# iOS will refuse to compile without the minimum target of iOS 11.0 # iOS will refuse to compile without the minimum target of iOS 11.0
DEFCFLAGS += -mios-version-min=11.0 DEFCFLAGS += -mios-version-min=11.0
endif endif
ifdef IS_DARWIN
# their readline has strict-prototype issues # their readline has strict-prototype issues
DEFCFLAGS += -Wno-strict-prototypes DEFCFLAGS += -Wno-strict-prototypes
# some warnings about braced initializers on structs we want to ignore # some warnings about braced initializers on structs we want to ignore
@@ -149,6 +167,28 @@ else
DEFCFLAGS += -Wstrict-prototypes DEFCFLAGS += -Wstrict-prototypes
endif 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 # Next ones are activated only if GCCEXTRA=1 or CLANGEXTRA=1
EXTRACFLAGS = EXTRACFLAGS =
EXTRACFLAGS += -Wunused-parameter -Wno-error=unused-parameter EXTRACFLAGS += -Wunused-parameter -Wno-error=unused-parameter
+16 -18
View File
@@ -100,10 +100,10 @@ LUALIBINC = -I$(LUALIBPATH)
LUALIB = $(LUALIBPATH)/liblua.a LUALIB = $(LUALIBPATH)/liblua.a
LUALIBLD = LUALIBLD =
LUAPLATFORM = generic LUAPLATFORM = generic
ifneq (,$(findstring MINGW,$(platform))) ifdef IS_MINGW
LUAPLATFORM = mingw LUAPLATFORM = mingw
else else
ifeq ($(platform),Darwin) ifdef IS_DARWIN
LUAPLATFORM = macosx LUAPLATFORM = macosx
else else
LUALIBLD += -ldl LUALIBLD += -ldl
@@ -112,8 +112,8 @@ else
endif endif
## Winsock2 ## Winsock2
ifneq (,$(findstring MINGW,$(platform))) ifdef IS_MINGW
LDLIBS += -lws2_32 LDLIBS += -lws2_32
endif endif
## Reveng ## Reveng
@@ -262,7 +262,7 @@ PM3INCLUDES += $(MQTTLIBINC)
# RPi Zero gcc requires -latomic # RPi Zero gcc requires -latomic
# but MacOSX /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld # but MacOSX /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
# doesn't recognize option --as-needed # doesn't recognize option --as-needed
ifneq ($(platform),Darwin) ifndef IS_DARWIN
LDLIBS += -Wl,--as-needed -latomic -Wl,--no-as-needed LDLIBS += -Wl,--as-needed -latomic -Wl,--no-as-needed
endif endif
@@ -433,7 +433,7 @@ PM3CFLAGS += -I./src -I./include -I../include -I../common -I../common_fpga $(PM3
#PM3CFLAGS += -std=c11 -pedantic #PM3CFLAGS += -std=c11 -pedantic
PREFIX ?= /usr/local 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) # 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 # and setting _ISOC99_SOURCE sets internally __USE_MINGW_ANSI_STDIO=1
# FTR __USE_MINGW_ANSI_STDIO seems deprecated in Mingw32 # FTR __USE_MINGW_ANSI_STDIO seems deprecated in Mingw32
@@ -496,14 +496,12 @@ LDFLAGS ?= $(DEFLDFLAGS)
LDFLAGS += $(MYLDFLAGS) LDFLAGS += $(MYLDFLAGS)
PM3LDFLAGS = $(LDFLAGS) PM3LDFLAGS = $(LDFLAGS)
ifeq ($(platform),Darwin) ifdef IS_IOS
ifeq ($(shell uname -p),arm64) PM3LDFLAGS += -framework Foundation -framework UIKit
# The platform is iOS endif
PM3LDFLAGS += -framework Foundation -framework UIKit ifdef IS_MACOS
else # M* macOS devices return arm
# M* macOS devices return arm PM3LDFLAGS += -framework Foundation -framework AppKit
PM3LDFLAGS += -framework Foundation -framework AppKit
endif
endif endif
################### ###################
@@ -840,8 +838,8 @@ else
CXXSRCS = guidummy.cpp CXXSRCS = guidummy.cpp
endif endif
# OS X # OS X & iOS
ifeq ($(platform),Darwin) ifdef IS_DARWIN
OBJCSRCS = util_darwin.m OBJCSRCS = util_darwin.m
endif endif
@@ -885,7 +883,7 @@ src/ui/ui_overlays.h: src/ui/overlays.ui
$(info [-] UIC $@) $(info [-] UIC $@)
$(Q)$(UIC) $^ > $@ $(Q)$(UIC) $^ > $@
# fix the header generated by some Qt6 versions (seen on 6.4.2 & 6.6.3) # 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<int>(\&QLabel::setNum)/g' $@ $(Q)sed -E -i '' 's/&QLabel::setNum/qOverload<int>(\&QLabel::setNum)/g' $@
else else
$(Q)sed -i 's/&QLabel::setNum/qOverload<int>(\&QLabel::setNum)/g' $@ $(Q)sed -i 's/&QLabel::setNum/qOverload<int>(\&QLabel::setNum)/g' $@
@@ -931,7 +929,7 @@ endif
ifneq (,$(INSTALLSHARE)) ifneq (,$(INSTALLSHARE))
$(Q)$(INSTALLSUDO) $(MKDIR) $(DESTDIR)$(PREFIX)$(PATHSEP)$(INSTALLSHARERELPATH) $(Q)$(INSTALLSUDO) $(MKDIR) $(DESTDIR)$(PREFIX)$(PATHSEP)$(INSTALLSHARERELPATH)
# hack ahead: inject installation path into pm3_resources.py # 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|^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)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) $(Q)$(INSTALLSUDO) $(CP) $(INSTALLSHARE) $(DESTDIR)$(PREFIX)$(PATHSEP)$(INSTALLSHARERELPATH)
+1 -4
View File
@@ -239,10 +239,7 @@ static int execute_system_command(const char *command) {
#if defined(_WIN32) #if defined(_WIN32)
char wrapped_command[255]; char wrapped_command[255];
strncat(wrapped_command, "cmd /C \"", 9); snprintf(wrapped_command, sizeof(wrapped_command), "cmd /C \"%s\"", command);
strncat(wrapped_command, command, strlen(command));
strncat(wrapped_command, "\"", 2);
ret = system(wrapped_command); ret = system(wrapped_command);
#else #else
ret = system(command); ret = system(command);