1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker *
4*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
16*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*49cdfc7eSAndroid Build Coastguard Worker */
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker /*
20*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
21*49cdfc7eSAndroid Build Coastguard Worker * Testcase to check the error conditions for mprotect(2)
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * ALGORITHM
24*49cdfc7eSAndroid Build Coastguard Worker * test1:
25*49cdfc7eSAndroid Build Coastguard Worker * Invoke mprotect() with an address of 0. Check if error
26*49cdfc7eSAndroid Build Coastguard Worker * is set to ENOMEM.
27*49cdfc7eSAndroid Build Coastguard Worker * test2:
28*49cdfc7eSAndroid Build Coastguard Worker * Invoke mprotect() with an address that is not a multiple
29*49cdfc7eSAndroid Build Coastguard Worker * of PAGESIZE. EINVAL
30*49cdfc7eSAndroid Build Coastguard Worker * test3:
31*49cdfc7eSAndroid Build Coastguard Worker * Mmap a file with only read permission (PROT_READ).
32*49cdfc7eSAndroid Build Coastguard Worker * Try to set write permission (PROT_WRITE) using mprotect(2).
33*49cdfc7eSAndroid Build Coastguard Worker * Check that error is set to EACCES.
34*49cdfc7eSAndroid Build Coastguard Worker *
35*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
36*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
37*49cdfc7eSAndroid Build Coastguard Worker * 03/2002 Paul Larson: case 1 should expect ENOMEM not EFAULT
38*49cdfc7eSAndroid Build Coastguard Worker */
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
46*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
47*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mprotect01";
50*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 3;
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker struct test_case {
53*49cdfc7eSAndroid Build Coastguard Worker void *addr;
54*49cdfc7eSAndroid Build Coastguard Worker int len;
55*49cdfc7eSAndroid Build Coastguard Worker int prot;
56*49cdfc7eSAndroid Build Coastguard Worker int error;
57*49cdfc7eSAndroid Build Coastguard Worker void (*setupfunc) (struct test_case *self);
58*49cdfc7eSAndroid Build Coastguard Worker };
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
61*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
62*49cdfc7eSAndroid Build Coastguard Worker static void setup1(struct test_case *self);
63*49cdfc7eSAndroid Build Coastguard Worker static void setup2(struct test_case *self);
64*49cdfc7eSAndroid Build Coastguard Worker static void setup3(struct test_case *self);
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker static int fd;
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker struct test_case TC[] = {
69*49cdfc7eSAndroid Build Coastguard Worker /* Check for ENOMEM passing memory that cannot be accessed. */
70*49cdfc7eSAndroid Build Coastguard Worker {NULL, 0, PROT_READ, ENOMEM, setup1},
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker /*
73*49cdfc7eSAndroid Build Coastguard Worker * Check for EINVAL by passing a pointer which is not a
74*49cdfc7eSAndroid Build Coastguard Worker * multiple of PAGESIZE.
75*49cdfc7eSAndroid Build Coastguard Worker */
76*49cdfc7eSAndroid Build Coastguard Worker {NULL, 1024, PROT_READ, EINVAL, setup2},
77*49cdfc7eSAndroid Build Coastguard Worker /*
78*49cdfc7eSAndroid Build Coastguard Worker * Check for EACCES by trying to mark a section of memory
79*49cdfc7eSAndroid Build Coastguard Worker * which has been mmap'ed as read-only, as PROT_WRITE
80*49cdfc7eSAndroid Build Coastguard Worker */
81*49cdfc7eSAndroid Build Coastguard Worker {NULL, 0, PROT_WRITE, EACCES, setup3}
82*49cdfc7eSAndroid Build Coastguard Worker };
83*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)84*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker int lc;
87*49cdfc7eSAndroid Build Coastguard Worker int i;
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker setup();
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
94*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < TST_TOTAL; i++) {
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker if (TC[i].setupfunc != NULL)
99*49cdfc7eSAndroid Build Coastguard Worker TC[i].setupfunc(&TC[i]);
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker TEST(tst_syscall(__NR_mprotect, TC[i].addr, TC[i].len, TC[i].prot));
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN != -1) {
104*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "call succeeded unexpectedly");
105*49cdfc7eSAndroid Build Coastguard Worker continue;
106*49cdfc7eSAndroid Build Coastguard Worker }
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker if (TEST_ERRNO == TC[i].error) {
109*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "expected failure - "
110*49cdfc7eSAndroid Build Coastguard Worker "errno = %d : %s", TEST_ERRNO,
111*49cdfc7eSAndroid Build Coastguard Worker strerror(TEST_ERRNO));
112*49cdfc7eSAndroid Build Coastguard Worker } else {
113*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "unexpected error - %d : %s - "
114*49cdfc7eSAndroid Build Coastguard Worker "expected %d", TEST_ERRNO,
115*49cdfc7eSAndroid Build Coastguard Worker strerror(TEST_ERRNO), TC[i].error);
116*49cdfc7eSAndroid Build Coastguard Worker }
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker cleanup();
120*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker
setup1(struct test_case * self)123*49cdfc7eSAndroid Build Coastguard Worker static void setup1(struct test_case *self)
124*49cdfc7eSAndroid Build Coastguard Worker {
125*49cdfc7eSAndroid Build Coastguard Worker self->len = getpagesize() + 1;
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
setup2(struct test_case * self)128*49cdfc7eSAndroid Build Coastguard Worker static void setup2(struct test_case *self)
129*49cdfc7eSAndroid Build Coastguard Worker {
130*49cdfc7eSAndroid Build Coastguard Worker self->addr = malloc(getpagesize());
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker if (self->addr == NULL)
133*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TINFO, cleanup, "malloc failed");
134*49cdfc7eSAndroid Build Coastguard Worker
135*49cdfc7eSAndroid Build Coastguard Worker /* Ensure addr2 is not page aligned */
136*49cdfc7eSAndroid Build Coastguard Worker self->addr++;
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker
setup3(struct test_case * self)139*49cdfc7eSAndroid Build Coastguard Worker static void setup3(struct test_case *self)
140*49cdfc7eSAndroid Build Coastguard Worker {
141*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(cleanup, "/dev/zero", O_RDONLY);
142*49cdfc7eSAndroid Build Coastguard Worker
143*49cdfc7eSAndroid Build Coastguard Worker self->len = getpagesize();
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Worker /*
146*49cdfc7eSAndroid Build Coastguard Worker * mmap the PAGESIZE bytes as read only.
147*49cdfc7eSAndroid Build Coastguard Worker */
148*49cdfc7eSAndroid Build Coastguard Worker self->addr = mmap(0, self->len, PROT_READ, MAP_SHARED, fd, 0);
149*49cdfc7eSAndroid Build Coastguard Worker if (self->addr == MAP_FAILED)
150*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "mmap failed");
151*49cdfc7eSAndroid Build Coastguard Worker
152*49cdfc7eSAndroid Build Coastguard Worker }
153*49cdfc7eSAndroid Build Coastguard Worker
setup(void)154*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
155*49cdfc7eSAndroid Build Coastguard Worker {
156*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
157*49cdfc7eSAndroid Build Coastguard Worker
158*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
159*49cdfc7eSAndroid Build Coastguard Worker }
160*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)161*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
162*49cdfc7eSAndroid Build Coastguard Worker {
163*49cdfc7eSAndroid Build Coastguard Worker close(fd);
164*49cdfc7eSAndroid Build Coastguard Worker }
165