Why are data and stack segments executed?

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

For example:

; prog.asm section .data code: db 0xCC ;int3 section .text global _start _start: jmp code mov rax, 60 ; sys_exit mov rdi, 0 syscall 

then

 nasm -f elf64 prog.asm ld -o prog prog.o ./prog 

forces prog to execute the int3 command.

Programs written in C and built using gcc have their own data, the stack and a bunch of unexecutable ones, so why do the ones written in the assembly behave differently?

+16
assembly linux nasm memory-mapping
Oct 22 2018-11-23T00:
source share
1 answer

In modern Linux-systems, the linker will mark the non-executable IFF stack / data, all objects participating in the link have a special section "marker" .note.GNU-stack .

If you compile, for example, int foo() { return 1; } int foo() { return 1; } into the assembly (with gcc -S foo.c ), you will see the following:

  .section .note.GNU-stack,"",@progbits 

For nasm syntax is shown in section 7.9.2 of the manual ; you need something like this:

  section .note.GNU-stack noalloc noexec nowrite progbits 



Note

This needs to be done for each .o file that is included in the executable. If an executable stack or data is required for any object file, it is installed for the entire segment.

+18
Oct. 22 2018-11-23T00:
source share



All Articles