Skip to content
Advertisement

Why data and stack segments are executable?

I have just noticed that my simple program has its data and stack segments executable. I saw it in /proc/[pid]/maps, and simple code confirmed it.

For example:

JavaScript

then

JavaScript

causes prog to execute int3 instruction.

Programs written in C and built with gcc have their data, stack and heap non-executable, so why those written in assembly behave in a different manner?

Advertisement

Answer

On modern Linux systems, the linker will mark stack/data non-executable IFF all objects that participate in the link have a special “marker” section .note.GNU-stack.

If you compile e.g. int foo() { return 1; } into assembly (with gcc -S foo.c), you’ll see this:

JavaScript

For nasm, the syntax is shown in section 8.9.2 of the manual; you want something like this:

JavaScript

Note

This has to be done for every .o file that goes into the executable. If any object file needs executable stack or data, then it’s set for the entire segment.

Advertisement