Skip to content

Commit 7e67951

Browse files
authored
Merge pull request #2964 from felixhandte/noexecstack-all-archs
Mark Huffman Decoder Assembly `noexecstack` on All Architectures
2 parents c592b46 + ff5d1da commit 7e67951

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

lib/decompress/huf_decompress_amd64.S

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "../common/portability_macros.h"
22

3-
#if ZSTD_ENABLE_ASM_X86_64_BMI2
4-
53
/* Stack marking
64
* ref: https://wiki.gentoo.org/wiki/Hardened/GNU_stack_quickstart
75
*/
8-
#if defined(__linux__) && defined(__ELF__)
6+
#if defined(__ELF__) && defined(__GNUC__)
97
.section .note.GNU-stack,"",%progbits
108
#endif
119

10+
#if ZSTD_ENABLE_ASM_X86_64_BMI2
11+
1212
/* Calling convention:
1313
*
1414
* %rdi contains the first argument: HUF_DecompressAsmArgs*.

lib/libzstd.mk

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ ZSTD_NO_ASM ?= 0
3434
# libzstd helpers
3535
##################################################################
3636

37+
VOID ?= /dev/null
38+
3739
# Make 4.3 doesn't support '\#' anymore (https://lwn.net/Articles/810071/)
3840
NUM_SYMBOL := \#
3941

@@ -96,6 +98,19 @@ CFLAGS += $(DEBUGFLAGS) $(MOREFLAGS)
9698
LDFLAGS += $(MOREFLAGS)
9799
FLAGS = $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
98100

101+
ifndef ALREADY_APPENDED_NOEXECSTACK
102+
export ALREADY_APPENDED_NOEXECSTACK := 1
103+
ifeq ($(shell echo "int main(int argc, char* argv[]) { (void)argc; (void)argv; return 0; }" | $(CC) $(FLAGS) -z noexecstack -x c -Werror - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
104+
LDFLAGS += -z noexecstack
105+
endif
106+
ifeq ($(shell echo | $(CC) $(FLAGS) -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
107+
CFLAGS += -Wa,--noexecstack
108+
else ifeq ($(shell echo | $(CC) $(FLAGS) -Qunused-arguments -Wa,--noexecstack -x assembler -Werror -c - -o $(VOID) 2>$(VOID) && echo 1 || echo 0),1)
109+
# See e.g.: https://github.com/android/ndk/issues/171
110+
CFLAGS += -Qunused-arguments -Wa,--noexecstack
111+
endif
112+
endif
113+
99114
HAVE_COLORNEVER = $(shell echo a | grep --color=never a > /dev/null 2> /dev/null && echo 1 || echo 0)
100115
GREP_OPTIONS ?=
101116
ifeq ($HAVE_COLORNEVER, 1)

programs/Makefile

-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ else
6262
EXT =
6363
endif
6464

65-
VOID = /dev/null
66-
6765
# thread detection
6866
NO_THREAD_MSG := ==> no threads, building without multithreading support
6967
HAVE_PTHREAD := $(shell printf '$(NUM_SYMBOL)include <pthread.h>\nint main(void) { return 0; }' > have_pthread.c && $(CC) $(FLAGS) -o have_pthread$(EXT) have_pthread.c -pthread 2> $(VOID) && rm have_pthread$(EXT) && echo 1 || echo 0; rm have_pthread.c)

tests/playTests.sh

+7
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,13 @@ zstd -d -f tmp_corrupt.zst --no-check
304304
zstd -d -f tmp_corrupt.zst --check --no-check # final flag overrides
305305
zstd -d -f tmp.zst --no-check
306306

307+
if [ "$isWindows" = false ]; then
308+
if [ -n "$(which readelf)" ]; then
309+
println "test: check if binary has executable stack (#2963)"
310+
readelf -lW "$ZSTD_BIN" | grep 'GNU_STACK .* RW ' || die "zstd binary has executable stack!"
311+
fi
312+
fi
313+
307314
println "\n===> zstdgrep tests"
308315
ln -sf "$ZSTD_BIN" zstdcat
309316
rm -f tmp_grep

0 commit comments

Comments
 (0)