英文:
Why do we use the stack for the return address of a function?
问题
我有点理解栈帧的工作原理。
为什么要用它们来存储返回地址?看起来这就是缓冲区溢出发生的原因。
分配一个特定的内存区域来仅存储返回地址,不与栈完全分离,会更安全吗?
英文:
I kind of understand how stack frames work.
Why do we use them to store the return address? It looks like that is why buffer overflows happen.
Wouldn't it be more secure to allocate a certain memory region to just keep return addresses, fully separated from the stack?
答案1
得分: 1
实际上,这是许多Forth实现的工作方式,它们有一个返回栈和一个数据栈。
然而,我不知道有哪些主流处理器在硬件上做到这一点(除了可能是很久以前的基于Forth的处理器)。
它们通常只有一个用于两种目的的栈。
无论如何,栈溢出只是缓冲区溢出的一个可能后果。它并不是存储返回地址引起缓冲区溢出,而是后者损坏了前者。即使你保持返回地址分开,缓冲区溢出仍然会损坏与返回地址无关的数据。
有人会说,这甚至更糟糕,因为在堆栈破坏的情况下,你可能会很快崩溃,因为你的函数返回到某个随机的内存位置。
保护返回信息将阻止这种情况发生,然后那些受损的数据将自由地在后面的某个地方引发更多问题
英文:
Actually, that is the way many Forth implementations work, they have a return stack as well as a data stack.
However, I know of no mainstream processors that do this same thing in hardware (except possibly the Forth-based ones from many moons ago).
They tend to have just the one stack that is used for both purposes.
In any case, stack-smashing is only one possible consequence of buffer overflows. It's not the storing of the return addresses that causes buffer overflows, it's the latter that corrupts the former. Even if you kept return addresses separate, buffer overflows would still corrupt data unrelated to return addresses.
Some would say that was even worse since, with stack smashing, you probably crash quickly because your function returns to some random memory location.
Protecting the return information would stop this from happening and then that corrupted data would be free to cause you more issues down the line somewhere
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论