Skip to contentSkip to conversation

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

PATCH Superseded Mathias Krause 3 participants 9 messages Apr 26, 2014 – May 7, 2014 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

Fixes 3c8b06f981 ("perf tests x86: Introduce perf_regs_load function")
Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/arch/x86/tests/regs_load.S |    8 +++++++-
 1 file changed, 7 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
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 42 lines · Mathias Krause

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.

Fixes 3c8b06f981 ("perf tests x86: Introduce perf_regs_load function")
Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/arch/x86/tests/regs_load.S |    8 +++++++-
 1 file changed, 7 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
Signature · 1 line
1.7.10.4

hum, how about fixing this once and for all.. ;-)
please check attached patch, thanks

jirka

---

tools/perf/bench/mem-memcpy-x86-64-asm.S +0 -6
index fcd9cf0..a20780b 100644
@@ -4,9 +4,3 @@
 #define Lmemcpy_c globl memcpy_c; memcpy_c
 #define Lmemcpy_c_e globl memcpy_c_e; memcpy_c_e
 #include "../../../arch/x86/lib/memcpy_64.S"
-/*
- * 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/bench/mem-memset-x86-64-asm.S +0 -7
index 9e5af89..cb92170 100644
@@ -4,10 +4,3 @@
 #define Lmemset_c globl memset_c; memset_c
 #define Lmemset_c_e globl memset_c_e; memset_c_e
 #include "../../../arch/x86/lib/memset_64.S"
-
-/*
- * 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 +3 -0
index d50869e..dd56b6b 100644
@@ -117,6 +117,9 @@ CFLAGS += -Wall
 CFLAGS += -Wextra
 CFLAGS += -std=gnu99
 
+# force non-executable stack
+LDFLAGS += -Wl,-z,noexecstack
+
 EXTLIBS = -lelf -lpthread -lrt -lm -ldl
 
 ifneq ($(OUTPUT),)
#2 lore raw
Quoted 38 lines · Jiri Olsa
Quoted 37 lines · Mathias Krause

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.

Fixes 3c8b06f981 ("perf tests x86: Introduce perf_regs_load function")
Cc Peter Zijlstra <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Arnaldo Carvalho de Melo <[email protected]>
Cc Jiri Olsa <[email protected]>
---
 tools/perf/arch/x86/tests/regs_load.S |    8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/arch/x86/tests/regs_load.S +5 -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.
Quoted 8 lines
Quoted 5 lines
tools/perf/arch/x86/tests/regs_load.S +2 -1
+ */
+.section .note.GNU-stack,"",@progbits
--

1.7.10.4

hum, how about fixing this once and for all.. ;-)
please check attached patch, thanks

Yeah, I thought about this option too but declined it for two reasons:
1/ The kernel sources should provide good quality examples, even for usage outside of the kernel. Imagine somebody taking the memcpy implementation for his own project but not copying the LDFLAGS. That would make his code have an executable stack while with the .GNU-stack marker in the assembler file it won't.
2/ What if somebody tries to add/link code to perf that makes use of nested functions? That'll make perf fail as the trampoline code generated by gcc won't be executable due to the enforced non-executable stack by -Wl,-z,noexecstack.

So, consistently adding the (brain damaged!) .GNU-stack annotations to assembler files handles both cases well. It serves as a good example to fulfill the requirements for the PT_GNU_STACK program header and will also work out of the box in case someone later on tries to add code to perf that makes use of nested functions. The latter might even be in code outside of perf, e.g. in some library. Adding -Wl,-z,noexecstack unconditionally would needlessly break that case.

Thanks,
Mathias

Quoted 48 lines

jirka

---

tools/perf/bench/mem-memcpy-x86-64-asm.S +0 -6
index fcd9cf0..a20780b 100644
@@ -4,9 +4,3 @@
 #define Lmemcpy_c globl memcpy_c; memcpy_c
 #define Lmemcpy_c_e globl memcpy_c_e; memcpy_c_e
 #include "../../../arch/x86/lib/memcpy_64.S"
-/*
- * 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/bench/mem-memset-x86-64-asm.S +0 -7
index 9e5af89..cb92170 100644
@@ -4,10 +4,3 @@
 #define Lmemset_c globl memset_c; memset_c
 #define Lmemset_c_e globl memset_c_e; memset_c_e
 #include "../../../arch/x86/lib/memset_64.S"
-
-/*
- * 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 +3 -0
index d50869e..dd56b6b 100644
@@ -117,6 +117,9 @@ CFLAGS += -Wall
 CFLAGS += -Wextra
 CFLAGS += -std=gnu99
 
+# force non-executable stack
+LDFLAGS += -Wl,-z,noexecstack
+
 EXTLIBS = -lelf -lpthread -lrt -lm -ldl
 
 ifneq ($(OUTPUT),)
#3 lore raw

On Sun, Apr 27, 2014 at 12:03:50PM +0200, Mathias Krause wrote:

SNIP

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

1.7.10.4

hum, how about fixing this once and for all.. ;-)
please check attached patch, thanks

Quoted 6 lines

Yeah, I thought about this option too but declined it for two reasons:
1/ The kernel sources should provide good quality examples, even for usage outside of the kernel. Imagine somebody taking the memcpy implementation for his own project but not copying the LDFLAGS. That would make his code have an executable stack while with the .GNU-stack marker in the assembler file it won't.

that 'somebody' should check/know better ;-) but ok, fair enough

Quoted 4 lines

2/ What if somebody tries to add/link code to perf that makes use of nested functions? That'll make perf fail as the trampoline code generated by gcc won't be executable due to the enforced non-executable stack by -Wl,-z,noexecstack.

I guess in that case he would change the Makefile as well?

anyway I have no objection for leaving that code in assembly objects, but I suggest we use the global option as well to prevent any future surprise..

or insert test case for perf's executable stack to 'perf test'

thanks,
jirka

Acked-by
#4 lore raw
Quoted 24 lines · Jiri Olsa

On Sun, Apr 27, 2014 at 12:03:50PM +0200, Mathias Krause wrote:

SNIP

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

1.7.10.4

hum, how about fixing this once and for all.. ;-)
please check attached patch, thanks

Quoted 6 lines

Yeah, I thought about this option too but declined it for two reasons:
1/ The kernel sources should provide good quality examples, even for usage outside of the kernel. Imagine somebody taking the memcpy implementation for his own project but not copying the LDFLAGS. That would make his code have an executable stack while with the .GNU-stack marker in the assembler file it won't.

that 'somebody' should check/know better ;-) but ok, fair enough

Quoted 4 lines

2/ What if somebody tries to add/link code to perf that makes use of nested functions? That'll make perf fail as the trampoline code generated by gcc won't be executable due to the enforced non-executable stack by -Wl,-z,noexecstack.

Quoted 5 lines

I guess in that case he would change the Makefile as well?

anyway I have no objection for leaving that code in assembly objects, but I suggest we use the global option as well to prevent any future surprise..

Correct. Regression in this area is 'silent' and can go unnoticed for long, because it has no functional side effects. So being defensive and forcing the secure option we want is important.

That way if an executable stack is needed by a patch then perf will break visibly, and the patch that relies on an executable stack can be fixed ...

So for the combo solution:

Thanks,

Ingo

#5 lore raw
In reply to Jiri Olsa
Quoted 8 lines · Jiri Olsa

On Sun, Apr 27, 2014 at 12:03:50PM +0200, Mathias Krause wrote:
[...]

Quoted 4 lines

2/ What if somebody tries to add/link code to perf that makes use of nested functions? That'll make perf fail as the trampoline code generated by gcc won't be executable due to the enforced non-executable stack by -Wl,-z,noexecstack.

I guess in that case he would change the Makefile as well?

Not necessarily. What if a later version of a library already used by perf needs an executable stack because it now makes use of nested functions? Unlikely, though in that case no change to perf would be made, but perf would then require an executable stack, too.

Anyway, as Ingo votes for the global linker option as well, I'll send a v2 of the patch containing your suggested linker flag.

Quoted 3 lines

anyway I have no objection for leaving that code in assembly objects, but I suggest we use the global option as well to prevent any future surprise..

Okay.

Quoted 1 line

or insert test case for perf's executable stack to 'perf test'

That won't work for systems preventing processes getting an executable stack in the first place. That was the reason I stumbled about the problem in the first place.

Thanks,
Mathias

Quoted 3 lines

thanks,
jirka

#6 lore raw
Quoted 8 lines · Mathias Krause
Quoted 7 lines · Jiri Olsa

On Sun, Apr 27, 2014 at 12:03:50PM +0200, Mathias Krause wrote:
[...]

Quoted 4 lines

2/ What if somebody tries to add/link code to perf that makes use of nested functions? That'll make perf fail as the trampoline code generated by gcc won't be executable due to the enforced non-executable stack by -Wl,-z,noexecstack.

Quoted 6 lines
Quoted 1 line

I guess in that case he would change the Makefile as well?

Not necessarily. What if a later version of a library already used by perf needs an executable stack because it now makes use of nested functions? Unlikely, though in that case no change to perf would be made, but perf would then require an executable stack, too.

I tried you can run binary with noexecstack having dynamic library dependency wit execstack

Quoted 3 lines

Anyway, as Ingo votes for the global linker option as well, I'll send a v2 of the patch containing your suggested linker flag.

cool

Quoted 11 lines
Quoted 4 lines

anyway I have no objection for leaving that code in assembly objects, but I suggest we use the global option as well to prevent any future surprise..

Okay.

Quoted 1 line

or insert test case for perf's executable stack to 'perf test'

That won't work for systems preventing processes getting an executable stack in the first place. That was the reason I stumbled about the

could be disabled on such systems

jirka

#7 lore raw
Quoted 11 lines · Jiri Olsa
Quoted 10 lines · Mathias Krause
Quoted 8 lines · Jiri Olsa

On Sun, Apr 27, 2014 at 12:03:50PM +0200, Mathias Krause wrote:
[...]

Quoted 4 lines

2/ What if somebody tries to add/link code to perf that makes use of nested functions? That'll make perf fail as the trampoline code generated by gcc won't be executable due to the enforced non-executable stack by -Wl,-z,noexecstack.

I guess in that case he would change the Makefile as well?

Quoted 7 lines
Quoted 4 lines

Not necessarily. What if a later version of a library already used by perf needs an executable stack because it now makes use of nested functions? Unlikely, though in that case no change to perf would be made, but perf would then require an executable stack, too.

I tried you can run binary with noexecstack having dynamic library dependency wit execstack

Well, it might work on your system but it won't work on mine. See this bug, why: https://sourceware.org/bugzilla/show_bug.cgi?id=12492

Quoted 8 lines

[...]

Quoted 5 lines
Quoted 2 lines

or insert test case for perf's executable stack to 'perf test'

That won't work for systems preventing processes getting an executable stack in the first place. That was the reason I stumbled about the

could be disabled on such systems

Of course, it could be disabled, i.e. I could allow perf to get an executable stack. Though, I don't like my stacks to be executable ;)

Thanks,
Mathias

Quoted 2 lines

jirka

#8 lore raw
Quoted 27 lines · Mathias Krause
Quoted 11 lines · Jiri Olsa
Quoted 10 lines · Mathias Krause
Quoted 8 lines · Jiri Olsa

On Sun, Apr 27, 2014 at 12:03:50PM +0200, Mathias Krause wrote:
[...]

Quoted 4 lines

2/ What if somebody tries to add/link code to perf that makes use of nested functions? That'll make perf fail as the trampoline code generated by gcc won't be executable due to the enforced non-executable stack by -Wl,-z,noexecstack.

I guess in that case he would change the Makefile as well?

Quoted 7 lines
Quoted 4 lines

Not necessarily. What if a later version of a library already used by perf needs an executable stack because it now makes use of nested functions? Unlikely, though in that case no change to perf would be made, but perf would then require an executable stack, too.

I tried you can run binary with noexecstack having dynamic library dependency wit execstack

Well, it might work on your system but it won't work on mine. See this bug, why: https://sourceware.org/bugzilla/show_bug.cgi?id=12492

Quoted 4 lines

[...]

Quoted 3 lines
Quoted 2 lines

or insert test case for perf's executable stack to 'perf test'

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

That won't work for systems preventing processes getting an executable stack in the first place. That was the reason I stumbled about the

could be disabled on such systems

Of course, it could be disabled, i.e. I could allow perf to get an executable stack. Though, I don't like my stacks to be executable ;)

Absolutely. Using an executable stack is really just a legacy thing, it should be avoided (and I'd say it must be avoided) in any modern application.

Thanks,

Ingo