mprotect

Loading MachineCode From File Into Memory and Executing in C — mprotect Failing

浪子不回头ぞ 提交于 2019-12-03 16:14:35
Hi I'm trying to load raw machine code into memory and run it from within a C program, right now when the program executes it breaks when trying to run mprotect on the memory to make it executable. I'm also not entirely sure that if the memory does get set right it will execute. I am currently running this on Ubuntu Linux x86 (Maybe the problem is Ubuntu's over-protection?) What I currently have is the following: #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 =

Explanation of MProtect Errno 12 (ENOMEM)

烈酒焚心 提交于 2019-12-01 10:11:09
I'm writing an iPhone application using Monotouch and recently the app has started crashing stating Mprotect failed at 0x863a000 (length 8192) with errno 12 followed by a rather lengthly stack trace and Springboard informing that "the application exited abormally with signal 6". I've read this question which states that the app has exhaused all the memory available on the iPhone. We have applied some general Dispose patterns to the app and generally disposed of any heavy objects as soon as we could. This meant the app now runs using less memory. However we are still getting the MProtect failed

C SIGSEGV Handler & Mprotect

孤人 提交于 2019-11-29 07:54:43
I'm constructing a program which uses mprotect() to restrict a block of memory from accessing. When the memory is requested, a SIGSEGV is thrown which I listen for using a signal() call. Once the SIGSEGV has been detected, I need to somehow access the pointer to the memory that was requested (that threw the fault) and the size of the segment requested. Is this possible? void fifoSigHandler(){ // Needs to only remove protection from requested block of virtual memory mprotect(fifoVm,(size_t)fifoVm_size,PROT_WRITE); printf("Caught Seg Fault"); } void fifo_init(void* vm, int vm_size, int n_frames,

Behaviour of PROT_READ and PROT_WRITE with mprotect

纵然是瞬间 提交于 2019-11-29 07:25:48
I've been trying to use mprotect against reading first, and then writing. Is here my code #include <sys/types.h> #include <sys/mman.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> 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; } Under Linux here are the results: Here is the output for PROT_WRITE : $ ./main a = 42 a

iPhone application crashes with Mprotect failed error (MonoTouch)

旧城冷巷雨未停 提交于 2019-11-28 01:39:10
I have a problem with my iPhone application developed with MonoTouch. I am developing an application that contacts a WCF Service, and when the WCF Service answer back, I update an UITableView with the returned data, when this is done it sends a new WCF async call, and when it answer back it updates the table again and so on. The problem is that when it has done this in a variable amount of time, it crashes with an Mprotect failed error 12. This only happens when the application is running directly on the phone and not in the simulator. I have tried to out comment where the update of the table

Behaviour of PROT_READ and PROT_WRITE with mprotect

筅森魡賤 提交于 2019-11-28 00:36:23
问题 I've been trying to use mprotect against reading first, and then writing. Is here my code #include <sys/types.h> #include <sys/mman.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> 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

Is there a better way than parsing /proc/self/maps to figure out memory protection?

折月煮酒 提交于 2019-11-27 07:52:06
On Linux (or Solaris) is there a better way than hand parsing /proc/self/maps repeatedly to figure out whether or not you can read, write or execute whatever is stored at one or more addresses in memory? For instance, in Windows you have VirtualQuery . In Linux, I can mprotect to change those values, but I can't read them back. Furthermore, is there any way to know when those permissions change (e.g. when someone uses mmap on a file behind my back) other than doing something terribly invasive and using ptrace on all threads in the process and intercepting any attempt to make a syscall that

How to write a signal handler to catch SIGSEGV?

筅森魡賤 提交于 2019-11-25 23:40:26
问题 I want to write a signal handler to catch SIGSEGV. I protect a block of memory for read or write using char *buffer; char *p; char a; int pagesize = 4096; mprotect(buffer,pagesize,PROT_NONE) This protects pagesize bytes of memory starting at buffer against any reads or writes. Second, I try to read the memory: p = buffer; a = *p This will generate a SIGSEGV, and my handler will be called. So far so good. My problem is that, once the handler is called, I want to change the access write of the