将MachineCode从文件加载到内存并执行C - mprotect失败

Cha*_*une 5 c memory machine-code mprotect

嗨,我正在尝试将原始机器代码加载到内存中并在C程序中运行它,现在当程序执行时,它试图在内存上运行mprotect使其可执行时中断.我也不完全确定如果内存设置正确,它将执行.我目前在Ubuntu Linux x86上运行它(也许问题是Ubuntu的过度保护?)

我目前拥有以下内容:

#include <memory.h>
#include <sys/mman.h>
#include <stdio.h>

int main ( int argc, char **argv )
{
 FILE *fp;
 int sz = 0;
 char *membuf;
 int output = 0;

 fp = fopen(argv[1],"rb");

 if(fp == NULL)
 {
  printf("Failed to open file, aborting!\n");
  exit(1);
 }

 fseek(fp, 0L, SEEK_END);
 sz = ftell(fp);
 fseek(fp, 0L, SEEK_SET);


 membuf = (char *)malloc(sz*sizeof(char));
 if(membuf == NULL)
 {
  printf("Failed to allocate memory, aborting!\n");
  exit(1);
 }

  memset(membuf, 0x90, sz*sizeof(char));

 if( mprotect(membuf, sz*sizeof(char), PROT_EXEC | PROT_READ | PROT_WRITE) == -1)
 {
  perror("mprotect");
  printf("mprotect failed!!! aborting!\n");
  exit(1);
 }



 if(!(fread(membuf, sz*sizeof(char), 1, fp)))
 {
  perror("fread");
  printf("Read failed, aborting!\n");
  exit(1);
 }
 __asm__
 ( 
  "call %%eax;"
  : "=a" (output)
       : "a" (membuf)
 );
 printf("Output = %x\n", output);

 return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到编译器警告:

/tmp/ccVnhHak.s: Assembler messages:
/tmp/ccVnhHak.s:107: Warning: indirect call without `*'
Run Code Online (Sandbox Code Playgroud)

我还没有得到程序来达到这个代码,所以我无法看到我的汇编代码是否正在做它应该做的事情.

Rom*_*nko 4

好的,根据我们在评论中的讨论,这就是答案:)

内存区域应与系统页面大小对齐。在这种情况下,posix_memalign() 调用是分配内存的正确方法:)