什么时候执行函数结语?

When is the function epilogue executed?

您好,我正在分析 return 到 libc 的攻击,并发现了一个有用的 website 图表显示了为 return 设计的缓冲区溢出之前(右)和之后(左)库攻击。 Return 地址被系统地址覆盖。

我不完全理解函数尾声的执行顺序。 ebp 的保存值会在函数returns 之前pop 到ebp 到system() 的地址吗?我读过 "function epilogue is executed upon termination of the function"。这个函数在什么时候准确终止?我不认为它是在调用 system() 之前,因为这意味着包含 4 crappy bytes 的覆盖保存的 ebp 将存储在 ebp 中。但我想确定一下。任何帮助深表感谢。

函数在以下时间终止:

  • 它执行return语句函数到达终点(即那里 没有要执行的 C 语句)

I dont totally understand the order of execution of the function epilogue. Will the saved value of ebp be popped into ebp before the function returns to the address of system()?

是的。返回到 return 地址是可以合理地视为函数而不是其调用者的动作的最后一个动作。

I have read that "function epilogue is executed upon termination of the function". At what point does this function exactly terminate?

什么功能?你还没有提出一个。但在一般的 C 术语中,函数在执行 return 语句时或在其主体中的最后一个语句执行完成时终止。这是文档引用的 "termination"。

I don't think it is before calling system()

那么,惊喜!重点是函数 epilogue 在函数终止后执行,导致控制权转移到 system() 函数的入口点。但是请注意,正确解释这一点需要一个分裂的视角。函数终止是特定于函数的,最好根据函数的源代码来定义。另一方面,结语在源代码中没有表示——它包含由编译器插入的额外机器指令,以实现源语言的 function-return 语义。

because this would mean the overwritten saved ebp containing 4 crappy bytes would be stored in ebp.

是的,但没关系,因为 esp 设置正确。然后控制跳转到 system() 的入口点,函数序言将 esp 设置为新的 ebp,并设置新的 esp。因此该函数具有有效的堆栈边界,因此它可以正确运行。当 system() returns 时可能会发生不好的事情,因为 return 地址是由 4 个糟糕的字节决定的,但我们不在乎——我们做了我们想做的所有破坏在我们诱导 system() 提供给我们的 shell 中,在 system() 之前 returns.