예, mmap를하고 mprotect는 오른쪽 기능은 다음과 같습니다. 나는 당신이 그것을 위해 내가 mprotect는()와 memalign을()와 함께 노력했다 "는 무엇을 의미하는 현재 approch, 즉,의 문제의 이해. 그러나하지 않는 내가 무엇을 얻을 수 memalignment, 멀리 프로세스 힙에서 memalign을 할당 메모리에서 . "
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
static int alloc_size;
static char* memory;
void segv_handler (int signal_number) {
printf ("memory accessed!\n");
mprotect (memory, alloc_size, PROT_READ | PROT_WRITE);
}
int main() {
int fd;
struct sigaction sa;
/* Install segv_handler as the handler for SIGSEGV. */
memset (&sa, 0, sizeof (sa));
sa.sa_handler = &segv_handler;
sigaction (SIGSEGV, &sa, NULL);
/* Allocate one page of memory by mapping /dev/zero. Map the memory
as write-only, initially. */
alloc_size = getpagesize();
fd = open ("/dev/zero", O_RDONLY);
memory = mmap (NULL, alloc_size, PROT_WRITE, MAP_PRIVATE, fd, 0);
close (fd);
/* Write to the page to obtain a private copy. */
memory[0] = 0;
/* Make the memory unwritable. */
mprotect (memory, alloc_size, PROT_NONE);
/* Write to the allocated memory region. */
memory[0] = 1;
/* All done; unmap the memory. */
printf ("all done\n");
munmap (memory, alloc_size);
return 0;
}
'MAP_ANONYMOUS'를 사용하고 전체'fopen()'e.t.c를 피할 수 있습니다. 이상한가? – thkala
부 nitpick'printf'이 아니다 [비동기 안전 (https://www.securecoding.cert.org/confluence/display/seccode/SIG30-C.+Call+only+asynchronous-safe+functions+within+ 신호 + 핸들러) – Flexo