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 * Test Description:
21 * Call mmap() to map a file creating mapped memory with no access under
22 * the following conditions -
23 * - The prot parameter is set to PROT_NONE
24 * - The file descriptor is open for read(any mode other than write)
25 * - The minimum file permissions should be 0444.
26 *
27 * The call should succeed to map the file creating mapped memory with the
28 * required attributes.
29 *
30 * Expected Result:
31 * mmap() should succeed returning the address of the mapped region,
32 * and an attempt to access the contents of the mapped region should give
33 * rise to the signal SIGSEGV.
34 *
35 * HISTORY
36 * 07/2001 Ported by Wayne Boyer
37 */
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <sys/types.h>
41 #include <errno.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #include <string.h>
45 #include <signal.h>
46 #include <sys/stat.h>
47 #include <sys/mman.h>
48 #include <setjmp.h>
49
50 #include "test.h"
51
52 #define TEMPFILE "mmapfile"
53
54 char *TCID = "mmap05";
55 int TST_TOTAL = 1;
56
57 static size_t page_sz;
58 static volatile char *addr;
59 static int fildes;
60 static volatile int pass = 0;
61 static sigjmp_buf env;
62
63 static void setup(void);
64 static void cleanup(void);
65 static void sig_handler(int sig);
66
main(int ac,char ** av)67 int main(int ac, char **av)
68 {
69 int lc;
70 char file_content;
71
72 tst_parse_opts(ac, av, NULL, NULL);
73
74 setup();
75
76 for (lc = 0; TEST_LOOPING(lc); lc++) {
77
78 tst_count = 0;
79
80 /*
81 * Call mmap to map the temporary file 'TEMPFILE'
82 * with no access.
83 */
84 errno = 0;
85 addr = mmap(0, page_sz, PROT_NONE,
86 MAP_FILE | MAP_SHARED, fildes, 0);
87 TEST_ERRNO = errno;
88
89 /* Check for the return value of mmap() */
90 if (addr == MAP_FAILED) {
91 tst_resm(TFAIL | TERRNO, "mmap() failed on %s",
92 TEMPFILE);
93 continue;
94 }
95
96 /*
97 * Try to access the mapped region. This should
98 * generate a SIGSEGV which will be caught below.
99 *
100 * This is wrapped by the sigsetjmp() call that will
101 * take care of restoring the program's context in an
102 * elegant way in conjunction with the call to
103 * siglongjmp() in the signal handler.
104 */
105 if (sigsetjmp(env, 1) == 0) {
106 file_content = addr[0];
107 }
108
109 if (pass) {
110 tst_resm(TPASS, "Got SIGSEGV as expected");
111 } else {
112 tst_resm(TFAIL, "Mapped memory region with NO "
113 "access is accessible");
114 }
115
116 /* Unmap mapped memory and reset pass in case we are looping */
117 if (munmap((void *)addr, page_sz) != 0) {
118 tst_brkm(TFAIL | TERRNO, cleanup, "munmap failed");
119 }
120 pass = 0;
121
122 }
123
124 cleanup();
125 tst_exit();
126 }
127
setup(void)128 static void setup(void)
129 {
130 char *tst_buff;
131
132 tst_sig(NOFORK, sig_handler, cleanup);
133
134 TEST_PAUSE;
135
136 page_sz = getpagesize();
137
138 /* Allocate space for the test buffer */
139 if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
140 tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)");
141 }
142
143 /* Fill the test buffer with the known data */
144 memset(tst_buff, 'A', page_sz);
145
146 tst_tmpdir();
147
148 /* Creat a temporary file used for mapping */
149 if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
150 free(tst_buff);
151 tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed",
152 TEMPFILE);
153 }
154
155 /* Write test buffer contents into temporary file */
156 if (write(fildes, tst_buff, page_sz) != page_sz) {
157 free(tst_buff);
158 tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE);
159 }
160
161 /* Free the memory allocated for test buffer */
162 free(tst_buff);
163
164 /* Make sure proper permissions set on file */
165 if (fchmod(fildes, 0444) < 0) {
166 tst_brkm(TFAIL | TERRNO, cleanup, "fchmod of %s failed",
167 TEMPFILE);
168 }
169
170 /* Close the temporary file opened for write */
171 if (close(fildes) < 0) {
172 tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed",
173 TEMPFILE);
174 }
175
176 /* Open the temporary file again for reading */
177 if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) {
178 tst_brkm(TFAIL | TERRNO, cleanup, "opening %s readonly failed",
179 TEMPFILE);
180 }
181 }
182
183 /*
184 * sig_handler() - Signal Catching function.
185 * This function gets executed when the test process receives
186 * the signal SIGSEGV while trying to access the contents of memory which
187 * is not accessible.
188 */
sig_handler(int sig)189 static void sig_handler(int sig)
190 {
191 if (sig == SIGSEGV) {
192 /* set the global variable and jump back */
193 pass = 1;
194 siglongjmp(env, 1);
195 } else {
196 tst_brkm(TBROK, cleanup, "received an unexpected signal: %d",
197 sig);
198 }
199 }
200
cleanup(void)201 static void cleanup(void)
202 {
203 close(fildes);
204 tst_rmdir();
205 }
206