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 * NAME
21*49cdfc7eSAndroid Build Coastguard Worker * mprotect03.c
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
24*49cdfc7eSAndroid Build Coastguard Worker * Testcase to check the mprotect(2) system call.
25*49cdfc7eSAndroid Build Coastguard Worker *
26*49cdfc7eSAndroid Build Coastguard Worker * ALGORITHM
27*49cdfc7eSAndroid Build Coastguard Worker * Create a shared mapped file region with PROT_READ | PROT_WRITE
28*49cdfc7eSAndroid Build Coastguard Worker * using the mmap(2) call. Then, use mprotect(2) to disable the
29*49cdfc7eSAndroid Build Coastguard Worker * write permission on the mapped region. Then, attempt to write to
30*49cdfc7eSAndroid Build Coastguard Worker * the mapped region using memcpy(). This would generate a sigsegv.
31*49cdfc7eSAndroid Build Coastguard Worker * Since the sigsegv is generated, this needs to be done in a child
32*49cdfc7eSAndroid Build Coastguard Worker * process (as sigsegv would repeatedly be generated). The testcase
33*49cdfc7eSAndroid Build Coastguard Worker * succeeds only when this sigsegv is generated while attempting to
34*49cdfc7eSAndroid Build Coastguard Worker * memcpy() on a shared region with only read permission.
35*49cdfc7eSAndroid Build Coastguard Worker *
36*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
37*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
38*49cdfc7eSAndroid Build Coastguard Worker * 05/2002 changed over to use tst_sig instead of sigaction
39*49cdfc7eSAndroid Build Coastguard Worker */
40*49cdfc7eSAndroid Build Coastguard Worker
41*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
44*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
45*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
46*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
47*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker #ifndef PAGESIZE
52*49cdfc7eSAndroid Build Coastguard Worker #define PAGESIZE 4096
53*49cdfc7eSAndroid Build Coastguard Worker #endif
54*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 1
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
57*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mprotect03";
60*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
61*49cdfc7eSAndroid Build Coastguard Worker int status;
62*49cdfc7eSAndroid Build Coastguard Worker char file1[BUFSIZ];
63*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)64*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
65*49cdfc7eSAndroid Build Coastguard Worker {
66*49cdfc7eSAndroid Build Coastguard Worker int lc;
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker char *addr;
69*49cdfc7eSAndroid Build Coastguard Worker int fd, pid;
70*49cdfc7eSAndroid Build Coastguard Worker char *buf = "abcdefghijklmnopqrstuvwxyz";
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker setup();
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
77*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker if ((fd = open(file1, O_RDWR | O_CREAT, 0777)) < 0)
80*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "open failed");
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(cleanup, SAFE_WRITE_ALL, fd, buf, strlen(buf));
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker /*
85*49cdfc7eSAndroid Build Coastguard Worker * mmap the PAGESIZE bytes as read only.
86*49cdfc7eSAndroid Build Coastguard Worker */
87*49cdfc7eSAndroid Build Coastguard Worker addr = mmap(0, strlen(buf), PROT_READ | PROT_WRITE, MAP_SHARED,
88*49cdfc7eSAndroid Build Coastguard Worker fd, 0);
89*49cdfc7eSAndroid Build Coastguard Worker if (addr == MAP_FAILED)
90*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "mmap failed");
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker /*
93*49cdfc7eSAndroid Build Coastguard Worker * Try to change the protection to WRITE.
94*49cdfc7eSAndroid Build Coastguard Worker */
95*49cdfc7eSAndroid Build Coastguard Worker TEST(mprotect(addr, strlen(buf), PROT_READ));
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN != -1) {
98*49cdfc7eSAndroid Build Coastguard Worker if ((pid = tst_fork()) == -1) {
99*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "fork failed");
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
103*49cdfc7eSAndroid Build Coastguard Worker memcpy(addr, buf, strlen(buf));
104*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "memcpy() did "
105*49cdfc7eSAndroid Build Coastguard Worker "not generate SIGSEGV");
106*49cdfc7eSAndroid Build Coastguard Worker exit(1);
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
109*49cdfc7eSAndroid Build Coastguard Worker waitpid(pid, &status, 0);
110*49cdfc7eSAndroid Build Coastguard Worker if (WEXITSTATUS(status) != 0) {
111*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "child returned "
112*49cdfc7eSAndroid Build Coastguard Worker "unexpected status");
113*49cdfc7eSAndroid Build Coastguard Worker } else {
114*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "SIGSEGV generated "
115*49cdfc7eSAndroid Build Coastguard Worker "as expected");
116*49cdfc7eSAndroid Build Coastguard Worker }
117*49cdfc7eSAndroid Build Coastguard Worker } else {
118*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "mprotect failed "
119*49cdfc7eSAndroid Build Coastguard Worker "unexpectedly, errno: %d", errno);
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker
122*49cdfc7eSAndroid Build Coastguard Worker /* clean up things in case we are looping */
123*49cdfc7eSAndroid Build Coastguard Worker SAFE_MUNMAP(cleanup, addr, strlen(buf));
124*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cleanup, fd);
125*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK(cleanup, file1);
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker cleanup();
129*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
sighandler(int sig)132*49cdfc7eSAndroid Build Coastguard Worker static void sighandler(int sig)
133*49cdfc7eSAndroid Build Coastguard Worker {
134*49cdfc7eSAndroid Build Coastguard Worker if (sig == SIGSEGV) {
135*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TINFO, "received signal: SIGSEGV");
136*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
137*49cdfc7eSAndroid Build Coastguard Worker } else
138*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, 0, "Unexpected signal %d received.", sig);
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker
setup(void)141*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
142*49cdfc7eSAndroid Build Coastguard Worker {
143*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, sighandler, NULL);
144*49cdfc7eSAndroid Build Coastguard Worker
145*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker sprintf(file1, "mprotect03.tmp.%d", getpid());
150*49cdfc7eSAndroid Build Coastguard Worker }
151*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)152*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
153*49cdfc7eSAndroid Build Coastguard Worker {
154*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
155*49cdfc7eSAndroid Build Coastguard Worker }
156