37 static void *adjust_pointer(
void *p, ssize_t amt)
39 return (
void *) (((uintptr_t) p) + amt);
42 static void sigsegv(
int s)
53 printf(
"PROT_NONE works, checking read...\n");
54 mprotect(p, 0x1000, PROT_READ);
63 printf(
"PROT_READ works, checking write...\n");
64 mprotect(p, 0x1000, PROT_WRITE);
66 *((
volatile char *) p) =
'Y';
68 mprotect(p, 0x1000, PROT_NONE);
74 printf(
"PROT_WRITE works, checking exec...\n");
75 mprotect(p, 0x1000, PROT_WRITE);
77 *((
volatile unsigned char *) p) = 0xC3;
79 mprotect(p, 0x1000, PROT_EXEC);
86 printf(
"Attempting to return to original context...\n");
87 mprotect(p, 0x1000, PROT_READ | PROT_WRITE);
94 static void sigsegv_jumper(
int s)
99 static void status(
const char *s)
109 p = mmap(0, 0x10000, PROT_NONE, MAP_PRIVATE | MAP_ANON, 0, 0);
112 static struct sigaction act;
113 sigprocmask(0, 0, &act.sa_mask);
114 act.sa_handler = sigsegv;
116 act.sa_flags = SA_NODEFER;
121 sigaction(SIGSEGV, &act, 0);
124 printf(
"Testing mprotect(2)...\n");
127 printf(
"mprotect(2) initial test was successful!\n");
129 printf(
"Testing mprotect(2) on ranges of pages...\n");
133 act.sa_handler = sigsegv_jumper;
134 sigaction(SIGSEGV, &act, 0);
137 mprotect(p, 0x10000, PROT_WRITE);
138 status(
"Test A... ");
141 mprotect(p, 0x10000, PROT_NONE);
144 mprotect(adjust_pointer(p, -0x1000), 0x12000, PROT_WRITE);
145 status(
"Test B... ");
146 if (setjmp(buf) == 1)
149 p[0x10000 - 1] =
'X';
151 mprotect(p, 0x10000, PROT_NONE);
154 mprotect(adjust_pointer(p, -0x1000), 0x5000, PROT_WRITE);
155 status(
"Test C... ");
156 if (setjmp(buf) == 1)
159 if (setjmp(buf) == 0)
165 mprotect(p, 0x10000, PROT_NONE);
168 mprotect(adjust_pointer(p, 0x5000), 0x6000, PROT_WRITE);
169 status(
"Test D... ");
170 if (setjmp(buf) == 1)
173 if (setjmp(buf) == 0)
179 mprotect(p, 0x10000, PROT_NONE);
182 mprotect(adjust_pointer(p, 0x2000), 0x6000, PROT_WRITE);
183 status(
"Test E... ");
184 if (setjmp(buf) == 1)
187 if (setjmp(buf) == 0)
193 if (setjmp(buf) == 0)
199 mprotect(p, 0x10000, PROT_NONE);
201 printf(
"mprotect(2) page range test was successful!\n");
204 signal(SIGSEGV, SIG_DFL);