Skip to contentSkip to conversation

perf x86: Fix perf to use non-executable stack, again

PATCH New Mathias Krause 2 participants 2 messages Apr 27, 2014 – May 1, 2014 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf x86: Fix perf to use non-executable stack, again

arch/x86/tests/regs_load.S is missing the linker note about the stack requirements, therefore making the linker fall back to an executable stack. As this object gets linked against the final perf binary, it'll needlessly end up with an executable stack. Fix this by adding the appropriate linker note.

Also add a global linker flag to prevent future regressions, as suggested by Jiri. This way perf won't get an executable stack even if we fail to add the .GNU-stack linker note to future assembler files. Though, doing so might create regressions the other way around, when (statically) linking against libraries needing an executable stack. But, apparently, regressing in that direction is wanted as it is an indicator of poor code quality -- or just missing linker notes.

Fixes 3c8b06f981 ("perf tests x86: Introduce perf_regs_load function")
Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/arch/x86/tests/regs_load.S |    8 +++++++-
 tools/perf/config/Makefile            |    4 ++++
 2 files changed, 11 insertions(+), 1 deletion(-)
tools/perf/arch/x86/tests/regs_load.S +7 -1
index 99167bf644..60875d5c55 100644
@@ -1,4 +1,3 @@
-
 #include <linux/linkage.h>
 
 #define AX	 0
@@ -90,3 +89,10 @@ ENTRY(perf_regs_load)
 	ret
 ENDPROC(perf_regs_load)
 #endif
+
+/*
+ * We need to provide note.GNU-stack section, saying that we want
+ * NOT executable stack. Otherwise the final linking will assume that
+ * the ELF stack should not be restricted at all and set it RWX.
+ */
+.section .note.GNU-stack,"",@progbits
tools/perf/config/Makefile +4 -0
index ee21fa95eb..d529291287 100644
@@ -109,6 +109,10 @@ CFLAGS += -Wall
 CFLAGS += -Wextra
 CFLAGS += -std=gnu99
 
+# Enforce a non-executable stack, as we may regress (again) in the future by
+# adding assembler files missing the .GNU-stack linker note.
+LDFLAGS += -Wl,-z,noexecstack
+
 EXTLIBS = -lelf -lpthread -lrt -lm -ldl
 
 ifneq ($(OUTPUT),)
Signature · 1 line
1.7.10.4
#1 lore raw
perf x86: Fix perf to use non-executable stack, again
Commit-ID:  6392b4ebdc1acfbed605d68c437ff5597d0a8d95
Gitweb:     http://git.kernel.org/tip/6392b4ebdc1acfbed605d68c437ff5597d0a8d95
Author:     Mathias Krause <[email protected]>
AuthorDate: Sun, 27 Apr 2014 18:51:05 +0200
Committer:  Jiri Olsa <[email protected]>
CommitDate: Wed, 30 Apr 2014 17:02:30 +0200

perf x86: Fix perf to use non-executable stack, again

arch/x86/tests/regs_load.S is missing the linker note about the stack requirements, therefore making the linker fall back to an executable stack. As this object gets linked against the final perf binary, it'll needlessly end up with an executable stack. Fix this by adding the appropriate linker note.

Also add a global linker flag to prevent future regressions, as suggested by Jiri. This way perf won't get an executable stack even if we fail to add the .GNU-stack linker note to future assembler files. Though, doing so might create regressions the other way around, when (statically) linking against libraries needing an executable stack. But, apparently, regressing in that direction is wanted as it is an indicator of poor code quality -- or just missing linker notes.

Fixes 3c8b06f981 ("perf tests x86: Introduce perf_regs_load function")
---
 tools/perf/arch/x86/tests/regs_load.S | 8 +++++++-
 tools/perf/config/Makefile            | 4 ++++
 2 files changed, 11 insertions(+), 1 deletion(-)
tools/perf/arch/x86/tests/regs_load.S +7 -1
index 99167bf..60875d5 100644
@@ -1,4 +1,3 @@
-
 #include <linux/linkage.h>
 
 #define AX	 0
@@ -90,3 +89,10 @@ ENTRY(perf_regs_load)
 	ret
 ENDPROC(perf_regs_load)
 #endif
+
+/*
+ * We need to provide note.GNU-stack section, saying that we want
+ * NOT executable stack. Otherwise the final linking will assume that
+ * the ELF stack should not be restricted at all and set it RWX.
+ */
+.section .note.GNU-stack,"",@progbits
tools/perf/config/Makefile +4 -0
index a57d59e..802cf54 100644
@@ -117,6 +117,10 @@ CFLAGS += -Wall
 CFLAGS += -Wextra
 CFLAGS += -std=gnu99
 
+# Enforce a non-executable stack, as we may regress (again) in the future by
+# adding assembler files missing the .GNU-stack linker note.
+LDFLAGS += -Wl,-z,noexecstack
+
 EXTLIBS = -lelf -lpthread -lrt -lm -ldl
 
 ifneq ($(OUTPUT),)