#define _GNU_SOURCE #include #include #include #include #include #include int main (void) { const size_t ps = sysconf (_SC_PAGESIZE); void *search_buf_page = mmap (NULL, ps + ps, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); assert (search_buf_page != MAP_FAILED); int i = mprotect (search_buf_page + ps, ps, PROT_NONE); assert (i == 0); const size_t nr_search_bytes = 5; const char search_buf_data[5] = { 0x56, 0x34, 0x12, 0x78, 0x78 }; void *search_buf = search_buf_page + ps - nr_search_bytes; memcpy (search_buf, search_buf_data, nr_search_bytes); const size_t pattern_len = 2; const char pattern[2] = { 0x78, 0x56 }; return (memmem (search_buf, nr_search_bytes, pattern, pattern_len) == NULL ? EXIT_SUCCESS : EXIT_FAILURE); }