I am working on a project where a dynamic library (.so
) is injected in some target program at runtime (dynamic instrumentation). The library handles its own memory using mmap/munmap
. For security reasons, it is required that some mapped region in the library only be writable through the exposed APIs from the library.
What we've done is toggle the write flag of the memory region using mprotect
and PROT_WRITE
at the prologue/epilogue of the library functions, e.g.:
void foo(void) {
mprotect(addr, PAGE_SIZE, PROT_READ | PROT_WRITE);
...
...
mprotect(addr, PAGE_SIZE, PROT_READ);
}
This works fine for single threaded applications. For multi-threaded applications, other tasks in the same process might be able to write to the mapped library region if a context switch (to a different task in the same process) takes place after the PROT_WRITE
was set (so the memory is writable) and before it was cleared.
So, the question is: Is it possible to disable other tasks in the process until foo
returns? If not, how do you suggest working around this?