1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2014 Fujitsu Ltd.
3*49cdfc7eSAndroid Build Coastguard Worker * Author: Xing Gu <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify it
6*49cdfc7eSAndroid Build Coastguard Worker * under the terms of version 2 of the GNU General Public License as
7*49cdfc7eSAndroid Build Coastguard Worker * published by the Free Software Foundation.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it would be useful, but
10*49cdfc7eSAndroid Build Coastguard Worker * WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License along
14*49cdfc7eSAndroid Build Coastguard Worker * with this program; if not, write the Free Software Foundation, Inc.,
15*49cdfc7eSAndroid Build Coastguard Worker * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16*49cdfc7eSAndroid Build Coastguard Worker */
17*49cdfc7eSAndroid Build Coastguard Worker /*
18*49cdfc7eSAndroid Build Coastguard Worker * Description:
19*49cdfc7eSAndroid Build Coastguard Worker * Verify that,
20*49cdfc7eSAndroid Build Coastguard Worker * 1) mprotect() succeeds to set a region of memory with no access,
21*49cdfc7eSAndroid Build Coastguard Worker * when 'prot' is set to PROT_NONE. An attempt to access the contents
22*49cdfc7eSAndroid Build Coastguard Worker * of the region gives rise to the signal SIGSEGV.
23*49cdfc7eSAndroid Build Coastguard Worker * 2) mprotect() succeeds to set a region of memory to be executed, when
24*49cdfc7eSAndroid Build Coastguard Worker * 'prot' is set to PROT_EXEC.
25*49cdfc7eSAndroid Build Coastguard Worker */
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
28*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <setjmp.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
38*49cdfc7eSAndroid Build Coastguard Worker
39*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig);
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
45*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker static void testfunc_protnone(void);
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker static void testfunc_protexec(void);
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker static void (*testfunc[])(void) = { testfunc_protnone, testfunc_protexec };
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mprotect04";
54*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = ARRAY_SIZE(testfunc);
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker static volatile int sig_caught;
57*49cdfc7eSAndroid Build Coastguard Worker static sigjmp_buf env;
58*49cdfc7eSAndroid Build Coastguard Worker static unsigned int page_sz;
59*49cdfc7eSAndroid Build Coastguard Worker typedef void (*func_ptr_t)(void);
60*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)61*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker int lc;
64*49cdfc7eSAndroid Build Coastguard Worker int i;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker setup();
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
71*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
72*49cdfc7eSAndroid Build Coastguard Worker
73*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < TST_TOTAL; i++)
74*49cdfc7eSAndroid Build Coastguard Worker (*testfunc[i])();
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
77*49cdfc7eSAndroid Build Coastguard Worker cleanup();
78*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker
sighandler(int sig)81*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig)
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker sig_caught = sig;
84*49cdfc7eSAndroid Build Coastguard Worker siglongjmp(env, 1);
85*49cdfc7eSAndroid Build Coastguard Worker }
86*49cdfc7eSAndroid Build Coastguard Worker
setup(void)87*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
88*49cdfc7eSAndroid Build Coastguard Worker {
89*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
90*49cdfc7eSAndroid Build Coastguard Worker tst_sig(NOFORK, sighandler, cleanup);
91*49cdfc7eSAndroid Build Coastguard Worker page_sz = getpagesize();
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
testfunc_protnone(void)96*49cdfc7eSAndroid Build Coastguard Worker static void testfunc_protnone(void)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker char *addr;
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker sig_caught = 0;
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker addr = SAFE_MMAP(cleanup, 0, page_sz, PROT_READ | PROT_WRITE,
103*49cdfc7eSAndroid Build Coastguard Worker MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker /* Change the protection to PROT_NONE. */
106*49cdfc7eSAndroid Build Coastguard Worker TEST(mprotect(addr, page_sz, PROT_NONE));
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
109*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO, "mprotect failed");
110*49cdfc7eSAndroid Build Coastguard Worker } else {
111*49cdfc7eSAndroid Build Coastguard Worker if (sigsetjmp(env, 1) == 0)
112*49cdfc7eSAndroid Build Coastguard Worker addr[0] = 1;
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker switch (sig_caught) {
115*49cdfc7eSAndroid Build Coastguard Worker case SIGSEGV:
116*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "test PROT_NONE for mprotect success");
117*49cdfc7eSAndroid Build Coastguard Worker break;
118*49cdfc7eSAndroid Build Coastguard Worker case 0:
119*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "test PROT_NONE for mprotect failed");
120*49cdfc7eSAndroid Build Coastguard Worker break;
121*49cdfc7eSAndroid Build Coastguard Worker default:
122*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup,
123*49cdfc7eSAndroid Build Coastguard Worker "received an unexpected signal: %d",
124*49cdfc7eSAndroid Build Coastguard Worker sig_caught);
125*49cdfc7eSAndroid Build Coastguard Worker }
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(cleanup, addr, page_sz);
129*49cdfc7eSAndroid Build Coastguard Worker }
130*49cdfc7eSAndroid Build Coastguard Worker
exec_func(void)131*49cdfc7eSAndroid Build Coastguard Worker static void exec_func(void)
132*49cdfc7eSAndroid Build Coastguard Worker {
133*49cdfc7eSAndroid Build Coastguard Worker return;
134*49cdfc7eSAndroid Build Coastguard Worker }
135*49cdfc7eSAndroid Build Coastguard Worker
page_present(void * p)136*49cdfc7eSAndroid Build Coastguard Worker static int page_present(void *p)
137*49cdfc7eSAndroid Build Coastguard Worker {
138*49cdfc7eSAndroid Build Coastguard Worker int fd;
139*49cdfc7eSAndroid Build Coastguard Worker
140*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(cleanup, "page_present", O_WRONLY|O_CREAT, 0644);
141*49cdfc7eSAndroid Build Coastguard Worker TEST(write(fd, p, 1));
142*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cleanup, fd);
143*49cdfc7eSAndroid Build Coastguard Worker
144*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN >= 0)
145*49cdfc7eSAndroid Build Coastguard Worker return 1;
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker if (TEST_ERRNO != EFAULT)
148*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TTERRNO, cleanup, "page_present write");
149*49cdfc7eSAndroid Build Coastguard Worker
150*49cdfc7eSAndroid Build Coastguard Worker return 0;
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker
clear_cache(void * start,int len)153*49cdfc7eSAndroid Build Coastguard Worker static void clear_cache(void *start, int len)
154*49cdfc7eSAndroid Build Coastguard Worker {
155*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_BUILTIN_CLEAR_CACHE == 1
156*49cdfc7eSAndroid Build Coastguard Worker __builtin___clear_cache(start, start + len);
157*49cdfc7eSAndroid Build Coastguard Worker #else
158*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TCONF, cleanup,
159*49cdfc7eSAndroid Build Coastguard Worker "compiler doesn't have __builtin___clear_cache()");
160*49cdfc7eSAndroid Build Coastguard Worker #endif
161*49cdfc7eSAndroid Build Coastguard Worker }
162*49cdfc7eSAndroid Build Coastguard Worker
163*49cdfc7eSAndroid Build Coastguard Worker /*
164*49cdfc7eSAndroid Build Coastguard Worker * To check for the ABI version, because ppc64le can technically use
165*49cdfc7eSAndroid Build Coastguard Worker * function descriptors.
166*49cdfc7eSAndroid Build Coastguard Worker */
167*49cdfc7eSAndroid Build Coastguard Worker #if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF < 2)
168*49cdfc7eSAndroid Build Coastguard Worker #define USE_FUNCTION_DESCRIPTORS
169*49cdfc7eSAndroid Build Coastguard Worker #endif
170*49cdfc7eSAndroid Build Coastguard Worker
171*49cdfc7eSAndroid Build Coastguard Worker #ifdef USE_FUNCTION_DESCRIPTORS
172*49cdfc7eSAndroid Build Coastguard Worker typedef struct {
173*49cdfc7eSAndroid Build Coastguard Worker uintptr_t entry;
174*49cdfc7eSAndroid Build Coastguard Worker uintptr_t toc;
175*49cdfc7eSAndroid Build Coastguard Worker uintptr_t env;
176*49cdfc7eSAndroid Build Coastguard Worker } func_descr_t;
177*49cdfc7eSAndroid Build Coastguard Worker #endif
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker /*
180*49cdfc7eSAndroid Build Coastguard Worker * Copy page where &exec_func resides. Also try to copy subsequent page
181*49cdfc7eSAndroid Build Coastguard Worker * in case exec_func is close to page boundary.
182*49cdfc7eSAndroid Build Coastguard Worker */
get_func(void * mem,uintptr_t * func_page_offset)183*49cdfc7eSAndroid Build Coastguard Worker static void *get_func(void *mem, uintptr_t *func_page_offset)
184*49cdfc7eSAndroid Build Coastguard Worker {
185*49cdfc7eSAndroid Build Coastguard Worker uintptr_t page_sz = getpagesize();
186*49cdfc7eSAndroid Build Coastguard Worker uintptr_t page_mask = ~(page_sz - 1);
187*49cdfc7eSAndroid Build Coastguard Worker void *func_copy_start, *page_to_copy;
188*49cdfc7eSAndroid Build Coastguard Worker void *mem_start = mem;
189*49cdfc7eSAndroid Build Coastguard Worker
190*49cdfc7eSAndroid Build Coastguard Worker #ifdef USE_FUNCTION_DESCRIPTORS
191*49cdfc7eSAndroid Build Coastguard Worker func_descr_t *opd = (func_descr_t *)&exec_func;
192*49cdfc7eSAndroid Build Coastguard Worker *func_page_offset = (uintptr_t)opd->entry & (page_sz - 1);
193*49cdfc7eSAndroid Build Coastguard Worker func_copy_start = mem + *func_page_offset;
194*49cdfc7eSAndroid Build Coastguard Worker page_to_copy = (void *)((uintptr_t)opd->entry & page_mask);
195*49cdfc7eSAndroid Build Coastguard Worker #else
196*49cdfc7eSAndroid Build Coastguard Worker *func_page_offset = (uintptr_t)&exec_func & (page_sz - 1);
197*49cdfc7eSAndroid Build Coastguard Worker func_copy_start = mem + *func_page_offset;
198*49cdfc7eSAndroid Build Coastguard Worker page_to_copy = (void *)((uintptr_t)&exec_func & page_mask);
199*49cdfc7eSAndroid Build Coastguard Worker #endif
200*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "exec_func: %p, page_to_copy: %p",
201*49cdfc7eSAndroid Build Coastguard Worker &exec_func, page_to_copy);
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Worker /* Copy 1st page. If it's not accessible, we might be running on a
204*49cdfc7eSAndroid Build Coastguard Worker * platform that supports execute-only page access permissions, in which
205*49cdfc7eSAndroid Build Coastguard Worker * case we have to explicitly change access protections to allow the
206*49cdfc7eSAndroid Build Coastguard Worker * memory to be read. */
207*49cdfc7eSAndroid Build Coastguard Worker if (!page_present(page_to_copy)) {
208*49cdfc7eSAndroid Build Coastguard Worker TEST(mprotect(page_to_copy, page_sz, PROT_READ | PROT_EXEC));
209*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
210*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO,
211*49cdfc7eSAndroid Build Coastguard Worker "mprotect(PROT_READ|PROT_EXEC) failed");
212*49cdfc7eSAndroid Build Coastguard Worker return NULL;
213*49cdfc7eSAndroid Build Coastguard Worker }
214*49cdfc7eSAndroid Build Coastguard Worker /* If the memory is still not accessible, then something must be
215*49cdfc7eSAndroid Build Coastguard Worker * wrong. */
216*49cdfc7eSAndroid Build Coastguard Worker if (!page_present(page_to_copy))
217*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "page_to_copy not present");
218*49cdfc7eSAndroid Build Coastguard Worker }
219*49cdfc7eSAndroid Build Coastguard Worker memcpy(mem, page_to_copy, page_sz);
220*49cdfc7eSAndroid Build Coastguard Worker
221*49cdfc7eSAndroid Build Coastguard Worker clear_cache(mem_start, page_sz);
222*49cdfc7eSAndroid Build Coastguard Worker
223*49cdfc7eSAndroid Build Coastguard Worker /* return pointer to area where copy of exec_func resides */
224*49cdfc7eSAndroid Build Coastguard Worker return func_copy_start;
225*49cdfc7eSAndroid Build Coastguard Worker }
226*49cdfc7eSAndroid Build Coastguard Worker
testfunc_protexec(void)227*49cdfc7eSAndroid Build Coastguard Worker static void testfunc_protexec(void)
228*49cdfc7eSAndroid Build Coastguard Worker {
229*49cdfc7eSAndroid Build Coastguard Worker func_ptr_t func;
230*49cdfc7eSAndroid Build Coastguard Worker uintptr_t func_page_offset;
231*49cdfc7eSAndroid Build Coastguard Worker void *p;
232*49cdfc7eSAndroid Build Coastguard Worker
233*49cdfc7eSAndroid Build Coastguard Worker sig_caught = 0;
234*49cdfc7eSAndroid Build Coastguard Worker
235*49cdfc7eSAndroid Build Coastguard Worker p = SAFE_MMAP(cleanup, 0, page_sz, PROT_READ | PROT_WRITE,
236*49cdfc7eSAndroid Build Coastguard Worker MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
237*49cdfc7eSAndroid Build Coastguard Worker
238*49cdfc7eSAndroid Build Coastguard Worker #ifdef USE_FUNCTION_DESCRIPTORS
239*49cdfc7eSAndroid Build Coastguard Worker func_descr_t opd;
240*49cdfc7eSAndroid Build Coastguard Worker opd.entry = (uintptr_t)get_func(p, &func_page_offset);
241*49cdfc7eSAndroid Build Coastguard Worker func = (func_ptr_t)&opd;
242*49cdfc7eSAndroid Build Coastguard Worker #else
243*49cdfc7eSAndroid Build Coastguard Worker func = get_func(p, &func_page_offset);
244*49cdfc7eSAndroid Build Coastguard Worker #endif
245*49cdfc7eSAndroid Build Coastguard Worker
246*49cdfc7eSAndroid Build Coastguard Worker if (!func)
247*49cdfc7eSAndroid Build Coastguard Worker goto out;
248*49cdfc7eSAndroid Build Coastguard Worker
249*49cdfc7eSAndroid Build Coastguard Worker if (func_page_offset + 64 > page_sz) {
250*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(cleanup, p, page_sz);
251*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TCONF, cleanup, "func too close to page boundary, "
252*49cdfc7eSAndroid Build Coastguard Worker "maybe your compiler ignores -falign-functions?");
253*49cdfc7eSAndroid Build Coastguard Worker }
254*49cdfc7eSAndroid Build Coastguard Worker
255*49cdfc7eSAndroid Build Coastguard Worker /* Change the protection to PROT_EXEC. */
256*49cdfc7eSAndroid Build Coastguard Worker TEST(mprotect(p, page_sz, PROT_EXEC));
257*49cdfc7eSAndroid Build Coastguard Worker
258*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
259*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL | TTERRNO, "mprotect failed");
260*49cdfc7eSAndroid Build Coastguard Worker } else {
261*49cdfc7eSAndroid Build Coastguard Worker if (sigsetjmp(env, 1) == 0)
262*49cdfc7eSAndroid Build Coastguard Worker (*func)();
263*49cdfc7eSAndroid Build Coastguard Worker
264*49cdfc7eSAndroid Build Coastguard Worker switch (sig_caught) {
265*49cdfc7eSAndroid Build Coastguard Worker case SIGSEGV:
266*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "test PROT_EXEC for mprotect failed");
267*49cdfc7eSAndroid Build Coastguard Worker break;
268*49cdfc7eSAndroid Build Coastguard Worker case 0:
269*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "test PROT_EXEC for mprotect success");
270*49cdfc7eSAndroid Build Coastguard Worker break;
271*49cdfc7eSAndroid Build Coastguard Worker default:
272*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup,
273*49cdfc7eSAndroid Build Coastguard Worker "received an unexpected signal: %d",
274*49cdfc7eSAndroid Build Coastguard Worker sig_caught);
275*49cdfc7eSAndroid Build Coastguard Worker }
276*49cdfc7eSAndroid Build Coastguard Worker }
277*49cdfc7eSAndroid Build Coastguard Worker
278*49cdfc7eSAndroid Build Coastguard Worker out:
279*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(cleanup, p, page_sz);
280*49cdfc7eSAndroid Build Coastguard Worker }
281*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)282*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
283*49cdfc7eSAndroid Build Coastguard Worker {
284*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
285*49cdfc7eSAndroid Build Coastguard Worker }
286