【发布时间】:2015-03-29 19:27:58
【问题描述】:
我遇到了这段代码(整个程序见this页面,见名为“srop.c”的程序)。
我的问题是关于 func 如何在 main 方法中使用。我只保留了我认为可能相关的代码。
让我困惑的是*ret = (int)func +4;这一行。
对此我有三个问题:
-
func(void)是一个函数,不应该用func()调用(注意括号) - 接受这对我来说可能是一些未知的调用函数的方式,当它应该返回
void时,如何将它转换为int? - 我知道作者不想保存帧指针也不想更新它(序言),正如他的评论所表明的那样。通过将函数转换为
int并添加四行,如何实现提前跳过两行?
。
(gdb) disassemble func
Dump of assembler code for function func:
0x000000000040069b <+0>: push %rbp
0x000000000040069c <+1>: mov %rsp,%rbp
0x000000000040069f <+4>: mov $0xf,%rax
0x00000000004006a6 <+11>: retq
0x00000000004006a7 <+12>: pop %rbp
0x00000000004006a8 <+13>: retq
End of assembler dump.
可能相关的是,编译时 gcc 告诉我以下内容:warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
请参阅下面的代码。
void func(void)
{
asm("mov $0xf,%rax\n\t");
asm("retq\n\t");
}
int main(void)
{
unsigned long *ret;
/*...*/
/* overflowing */
ret = (unsigned long *)&ret + 2;
*ret = (int)func +4; //skip gadget's function prologue
/*...*/
return 0;
}
[编辑] 根据非常有用的建议,以下是一些进一步的信息:
calling func returns a pointer to the start of the function: 0x400530
casting this to an int is dangerous (in hex) 400530
casting this to an int in decimal 4195632
safe cast to unsigned long 4195632
size of void pointer: 8
size of int: 4
size of unsigned long: 8
[Edit 2:] @cmaster:您能否指出一些有关如何将汇编函数放在单独的文件中并链接到它的更多信息?原程序无法编译,因为不知道prog(放入汇编文件时)是什么函数,所以必须在编译前或编译时添加?
另外,gcc -S 在只包含汇编命令的 C 文件上运行时似乎添加了很多额外的信息,func(void) 不能用以下汇编代码表示吗?
func:
mov $0xf,%rax
retq
【问题讨论】:
标签: c linux assembly casting static-linking