简体   繁体   中英

Allocation of memory on the stack and Buffer Overflow, x86 ISA32

So I've been learning about Buffer Overflow as well as the Procedure for saving memory onto the stack and ive been bothered/ I dont understand certain conventions. When saving local variables/ arrays why is that we allocate memory from the stack pointer to the base pointer

(array[0] would be closer to the top of the stack and array[1] to array [n-1] would be closer to the base pointer of your frame)

Why not the other way around? if array[n-1] was allocated towards the stack pointer there would be no threat to the saved registers/ return addresses in the previous frame.

I've read the wiki article and the stacks that grow up section, but they assume the return address is there before the buffer is allocated, which would mean the buffer overwrites the return. But shouldnt it be the other way around? Shouldnt the return address be written in the stack only after the local variables are declared?

The x86 architecture was heavily influenced by Intel 8080, which had a similar stack architecture (one shared call and data stack growing downward). At the time this was seen as a significant advance, given that the 8080's predecessor, the 8008, had a call stack that was built into the CPU and that was only seven levels deep.

Intel 8080 was released in 1974. Given that not a lot of stuff was networked (for example, Ethernet was just being developed), potential stack smashing attacks were probably not a major concern. Flexibility, and making good use the of the available RAM, on the other hand, were.

It was much later that stack smashing became a major concern. For example, the first high-quality step-by-step guide was published in 1996.

Historically, stack and heap occupied the same memory space. Heap was allocated from the bottom, while stack from the top. When you overflow... you would just overwrite the other space memory and soon lead to a crash.

Nowdays, I believe, it is no longer the case, but the x86 instruction set for managing stack remained so that it consumes memory from top.

But arrays are indexed from the bottom. For that reason array[0] is closer to the "top" of the stack (which is below), and array[n-1] is further away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM