First a little theory:
The memory is divided into pages, each with different access permissions. At the very beginning of the process the code is loaded into a readable and executable (but not writable) part of the memory, right at the top. Then enter data from initialized and uninitialized global variables (zeroed) and constant data (such as literal strings and floats). After this begins the heap, which has a set start and grows down.
At the bottom of the process memory (there next to the address 0xffffffffffffffff
) is the beginning of the stack. This one grows up and there’s the stack pointer pointing to its top. To make the stack grow just decrease that pointer.
I will describe the execution of the code step by step:
jmp caller # Pular imediatamente para o label caller.
caller:
call jumper # Escreva o endereço da próxima instrução no topo da stack e
# decremente o ponteiro. Em seguida, pule para jumper.
jumper:
pop %rsi # Leia o endereço no topo da stack para o registrador RSI e incremente
# o ponteiro. Agora RSI (registrador usado nas instruções de iteração
# sob strings) contém o endereço de onde o código está.
mov %rsi, 0x8(%rsi) # Calcule o endereço RSI+8 e escreva o valor de RSI (um endereço)
# essa memória. BUUM! Segmentation Fault
# Equivalente em C: *(RSI+8) = (uint64_t)RSI // RSI é um ponteiro
What happened is simple: You tried to write on a nonwritable (code) page. And the operating system will stop you from doing that by killing your lawsuit for trying something illegal. The solution would first change the permissions of the pages in question. The way to do this varies from system to system. On Linux you can use the function mprotect
, and on Windows, VirtualProtect
.
This question seems to be out of date because it is in English.
– Leonel Sanches da Silva
malz, first question here, and here it is in English, guenta aê
– Raphael Souza
I’m looking for the code in question and I can’t find it in the article. Where it comes from? Smashing The Stack For Fun And Profit - Aleph One
– Guilherme Bernal
is between pages 14 and 15, I changed here and there since my processor is 64 Bits, by the way, your explanation was good but I already knew this part of the protected memory, I know I can not execute code outside the .text. I did not formulate well my question, I wonder how I can fix this without altering the permissions. Anyway, thank you so much for your help.
– Raphael Souza