【发布时间】:2012-08-05 06:08:33
【问题描述】:
我在 x86_64 上编写了一个示例代码,尝试执行动态 malloc 代码。 有一个
程序收到信号SIGSEGV,分段错误。 0x0000000000601010 在?? ()
0x0000000000601010 是 bin 的位置,谁能告诉我为什么?谢谢!!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <sys/mman.h>
volatile int sum(int a,int b)
{
return a+b;
}
int main(int argc, char **argv)
{
char* bin = NULL;
unsigned int len = 0;
int ret = 0;
/*code_str is the compiled code for function sum.*/
char code_str[] ={0x55,0x48,0x89,0xe5,0x89,0x7d,0xfc,0x89,
0x75,0xf8,0x8b,0x45,0xf8,0x03,0x45,0xfc,0xc9,0xc3};
len = sizeof(code_str)/sizeof(char);
bin = (char*)malloc(len);
memcpy(bin,code_str,len);
mprotect(bin,len , PROT_EXEC | PROT_READ | PROT_WRITE);
asm volatile ("mov $0x2,%%esi \n\t"
"mov $0x8,%%edi \n\t"
"mov %1,%%rbx \n\t"
"call *%%rbx "
:"=a"(ret)
:"g"(bin)
:"%rbx","%esi","%edi");
printf("sum:%d\n",ret);
return 0;
}
【问题讨论】:
-
它给你一个错误代码吗?
-
在mprotect中是否需要通过bin? pl。检查 &bin 是否正确。
-
不要将
&bin传递给mprotect,而只是传递bin