Tag: mprotect

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

嗨,我正在尝试将原始机器代码加载到内存中并在C程序中运行它,现在当程序执行时,它试图在内存上运行mprotect以使其可执行时中断。 我也不完全确定如果内存设置正确,它将执行。 我目前在Ubuntu Linux x86上运行它(也许问题是Ubuntu的过度保护?) 我目前拥有以下内容: #include #include #include 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 = […]

具有mprotect的PROT_READ和PROT_WRITE的行为

我一直在尝试使用mprotect首先阅读,然后写作。 这是我的代码 #include #include #include #include #include int main(void) { int pagesize = sysconf(_SC_PAGE_SIZE); int *a; if (posix_memalign((void**)&a, pagesize, sizeof(int)) != 0) perror(“memalign”); *a = 42; if (mprotect(a, pagesize, PROT_WRITE) == -1) /* Resp. PROT_READ */ perror(“mprotect”); printf(“a = %d\n”, *a); *a = 24; printf(“a = %d\n”, *a); free (a); return 0; } 在Linux下这里是结果: 这是PROT_WRITE的输出: […]