1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /*
20 * DESCRIPTION
21 * Testcase to check the mprotect(2) system call.
22 *
23 * ALGORITHM
24 * Create a mapped region using mmap with READ permission.
25 * Try to write into that region in a child process using memcpy.
26 * Verify that a SIGSEGV is generated.
27 * Now change the protection to WRITE using mprotect(2).
28 * Again try to write into the mapped region.
29 * Verify that no SIGSEGV is generated.
30 *
31 * HISTORY
32 * 07/2001 Ported by Wayne Boyer
33 * 05/2002 changed over to use tst_sig instead of sigaction
34 */
35
36 #include <sys/mman.h>
37 #include <sys/wait.h>
38 #include <errno.h>
39 #include <fcntl.h>
40 #include <limits.h>
41 #include <stdlib.h>
42 #include "test.h"
43
44 #include "safe_macros.h"
45
46 static void sighandler(int sig);
47 static void cleanup(void);
48 static void setup(void);
49
50 char *TCID = "mprotect02";
51 int TST_TOTAL = 1;
52 static int fd, status;
53 static char file1[BUFSIZ];
54
55 static char *addr = MAP_FAILED;
56 static char buf[] = "abcdefghijklmnopqrstuvwxyz";
57
main(int ac,char ** av)58 int main(int ac, char **av)
59 {
60 int lc;
61
62 int bytes_to_write, fd, num_bytes;
63 pid_t pid;
64
65 tst_parse_opts(ac, av, NULL, NULL);
66
67 setup();
68
69 for (lc = 0; TEST_LOOPING(lc); lc++) {
70
71 tst_count = 0;
72
73 fd = SAFE_OPEN(cleanup, file1, O_RDWR | O_CREAT, 0777);
74
75 num_bytes = getpagesize();
76
77 do {
78
79 if (num_bytes > strlen(buf))
80 bytes_to_write = strlen(buf);
81 else
82 bytes_to_write = num_bytes;
83
84 num_bytes -=
85 SAFE_WRITE(cleanup, 1, fd, buf, bytes_to_write);
86
87 } while (0 < num_bytes);
88
89 /* mmap the PAGESIZE bytes as read only. */
90 addr = SAFE_MMAP(cleanup, 0, sizeof(buf), PROT_READ,
91 MAP_SHARED, fd, 0);
92
93 if ((pid = FORK_OR_VFORK()) == -1)
94 tst_brkm(TBROK | TERRNO, cleanup, "fork #1 failed");
95
96 if (pid == 0) {
97 memcpy(addr, buf, strlen(buf));
98 exit(255);
99 }
100
101 if (waitpid(pid, &status, 0) == -1)
102 tst_brkm(TBROK | TERRNO, cleanup, "waitpid failed");
103 if (!WIFEXITED(status))
104 tst_brkm(TBROK, cleanup, "child exited abnormally "
105 "with status: %d", status);
106 switch (status) {
107 case 255:
108 tst_brkm(TBROK, cleanup,
109 "memcpy did not generate SIGSEGV");
110 case 0:
111 tst_resm(TPASS, "got SIGSEGV as expected");
112 break;
113 default:
114 tst_brkm(TBROK, cleanup, "got unexpected signal: %d",
115 status);
116 break;
117 }
118
119 /* Change the protection to WRITE. */
120 TEST(mprotect(addr, sizeof(buf), PROT_WRITE));
121
122 if (TEST_RETURN != -1) {
123 if ((pid = FORK_OR_VFORK()) == -1)
124 tst_brkm(TBROK | TERRNO, cleanup,
125 "fork #2 failed");
126
127 if (pid == 0) {
128 memcpy(addr, buf, strlen(buf));
129 exit(0);
130 }
131
132 if (waitpid(pid, &status, 0) == -1)
133 tst_brkm(TBROK | TERRNO, cleanup,
134 "waitpid failed");
135
136 if (WIFEXITED(status) &&
137 WEXITSTATUS(status) == 0)
138 tst_resm(TPASS, "didn't get SIGSEGV");
139 else
140 tst_brkm(TBROK, cleanup,
141 "child exited abnormally");
142 } else {
143 tst_resm(TFAIL | TERRNO, "mprotect failed");
144 continue;
145 }
146
147 SAFE_MUNMAP(cleanup, addr, sizeof(buf));
148 addr = MAP_FAILED;
149
150 SAFE_CLOSE(cleanup, fd);
151
152 SAFE_UNLINK(cleanup, file1);
153
154 }
155
156 cleanup();
157 tst_exit();
158 }
159
sighandler(int sig)160 static void sighandler(int sig)
161 {
162 _exit((sig == SIGSEGV) ? 0 : sig);
163 }
164
setup(void)165 static void setup(void)
166 {
167 tst_sig(FORK, sighandler, cleanup);
168
169 TEST_PAUSE;
170
171 tst_tmpdir();
172
173 sprintf(file1, "mprotect02.tmp.%d", getpid());
174 }
175
cleanup(void)176 static void cleanup(void)
177 {
178 if (addr != MAP_FAILED) {
179 SAFE_MUNMAP(NULL, addr, sizeof(buf));
180 SAFE_CLOSE(NULL, fd);
181 }
182
183 tst_rmdir();
184 }
185