Dmitry V. Levin | 4832134 | 2015-03-31 18:52:57 +0000 | [diff] [blame] | 1 | #include <string.h> |
| 2 | #include <unistd.h> |
| 3 | #include <sys/mman.h> |
| 4 | |
| 5 | int |
| 6 | main(void) |
| 7 | { |
| 8 | const size_t page_len = sysconf(_SC_PAGESIZE); |
| 9 | const size_t tail_len = 257; |
| 10 | |
| 11 | if (tail_len >= page_len) |
| 12 | return 77; |
| 13 | |
| 14 | void *p = mmap(NULL, page_len * 2, PROT_READ | PROT_WRITE, |
| 15 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 16 | if (p == MAP_FAILED || mprotect(p + page_len, page_len, PROT_NONE)) |
| 17 | return 77; |
| 18 | |
| 19 | memset(p, 0, page_len); |
| 20 | char *addr = p + page_len - tail_len; |
| 21 | memset(addr, '/', tail_len - 1); |
| 22 | if (chdir(addr)) |
| 23 | return 77; |
| 24 | |
| 25 | return 0; |
| 26 | } |