blob: 8fdd2f3c7e20f65a0c17c3b51189dfc9582a20a2 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis5efee332013-06-04 20:14:58 +02002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Cyril Hrubis5efee332013-06-04 20:14:58 +02004 * 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.
plars865695b2001-08-27 22:15:12 +00008 *
Cyril Hrubis5efee332013-06-04 20:14:58 +02009 * 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.
plars865695b2001-08-27 22:15:12 +000013 *
Cyril Hrubis5efee332013-06-04 20:14:58 +020014 * 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
plars865695b2001-08-27 22:15:12 +000017 */
18
19/*
plars865695b2001-08-27 22:15:12 +000020 * 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 *
plars865695b2001-08-27 22:15:12 +000035 * HISTORY
36 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +000037 */
plars865695b2001-08-27 22:15:12 +000038#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"
plars865695b2001-08-27 22:15:12 +000051
52#define TEMPFILE "mmapfile"
53
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020054char *TCID = "mmap05";
55int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000056
Cyril Hrubis5efee332013-06-04 20:14:58 +020057static size_t page_sz;
58static volatile char *addr;
59static int fildes;
60static volatile int pass = 0;
61static sigjmp_buf env;
62
63static void setup(void);
64static void cleanup(void);
65static void sig_handler(int sig);
plars865695b2001-08-27 22:15:12 +000066
subrata_modak56207ce2009-03-23 13:35:39 +000067int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000068{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020069 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020070 const char *msg;
Cyril Hrubis5efee332013-06-04 20:14:58 +020071 char file_content;
subrata_modakbdbaec52009-02-26 12:14:51 +000072
Garrett Cooper11d51042010-11-22 20:47:29 -080073 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
plars865695b2001-08-27 22:15:12 +000074 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000075
plars865695b2001-08-27 22:15:12 +000076 setup();
77
plars865695b2001-08-27 22:15:12 +000078 for (lc = 0; TEST_LOOPING(lc); lc++) {
Garrett Cooper2c282152010-12-16 00:55:50 -080079
Caspar Zhangd59a6592013-03-07 14:59:12 +080080 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000081
subrata_modak4bb656a2009-02-26 12:02:09 +000082 /*
plars865695b2001-08-27 22:15:12 +000083 * Call mmap to map the temporary file 'TEMPFILE'
subrata_modak56207ce2009-03-23 13:35:39 +000084 * with no access.
plars865695b2001-08-27 22:15:12 +000085 */
robbiewd5c21122002-03-22 16:22:40 +000086 errno = 0;
87 addr = mmap(0, page_sz, PROT_NONE,
subrata_modak56207ce2009-03-23 13:35:39 +000088 MAP_FILE | MAP_SHARED, fildes, 0);
robbiewd5c21122002-03-22 16:22:40 +000089 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +000090
91 /* Check for the return value of mmap() */
robbiewd5c21122002-03-22 16:22:40 +000092 if (addr == MAP_FAILED) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 tst_resm(TFAIL | TERRNO, "mmap() failed on %s",
94 TEMPFILE);
plars865695b2001-08-27 22:15:12 +000095 continue;
96 }
97
Cyril Hrubise38b9612014-06-02 17:20:57 +020098 /*
99 * Try to access the mapped region. This should
100 * generate a SIGSEGV which will be caught below.
101 *
102 * This is wrapped by the sigsetjmp() call that will
103 * take care of restoring the program's context in an
104 * elegant way in conjunction with the call to
105 * siglongjmp() in the signal handler.
106 */
107 if (sigsetjmp(env, 1) == 0) {
108 file_content = addr[0];
109 }
plars865695b2001-08-27 22:15:12 +0000110
Cyril Hrubise38b9612014-06-02 17:20:57 +0200111 if (pass) {
112 tst_resm(TPASS, "Got SIGSEGV as expected");
plars865695b2001-08-27 22:15:12 +0000113 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200114 tst_resm(TFAIL, "Mapped memory region with NO "
115 "access is accessible");
plars865695b2001-08-27 22:15:12 +0000116 }
117
118 /* Unmap mapped memory and reset pass in case we are looping */
robbiewcc28f4c2003-03-26 22:46:04 +0000119 if (munmap((void *)addr, page_sz) != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 tst_brkm(TFAIL | TERRNO, cleanup, "munmap failed");
plars865695b2001-08-27 22:15:12 +0000121 }
122 pass = 0;
123
Garrett Cooper2c282152010-12-16 00:55:50 -0800124 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200125
plars865695b2001-08-27 22:15:12 +0000126 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800127 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800128}
plars865695b2001-08-27 22:15:12 +0000129
Cyril Hrubis5efee332013-06-04 20:14:58 +0200130static void setup(void)
plars865695b2001-08-27 22:15:12 +0000131{
Cyril Hrubis5efee332013-06-04 20:14:58 +0200132 char *tst_buff;
plars865695b2001-08-27 22:15:12 +0000133
plars865695b2001-08-27 22:15:12 +0000134 tst_sig(NOFORK, sig_handler, cleanup);
135
plars865695b2001-08-27 22:15:12 +0000136 TEST_PAUSE;
137
Cyril Hrubisec22fb22013-06-04 20:37:05 +0200138 page_sz = getpagesize();
plars865695b2001-08-27 22:15:12 +0000139
140 /* Allocate space for the test buffer */
Garrett Cooper53740502010-12-16 00:04:01 -0800141 if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) {
Garrett Cooper11d51042010-11-22 20:47:29 -0800142 tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)");
plars865695b2001-08-27 22:15:12 +0000143 }
144
145 /* Fill the test buffer with the known data */
146 memset(tst_buff, 'A', page_sz);
147
plars865695b2001-08-27 22:15:12 +0000148 tst_tmpdir();
149
150 /* Creat a temporary file used for mapping */
151 if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) {
plars865695b2001-08-27 22:15:12 +0000152 free(tst_buff);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800153 tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed",
154 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000155 }
156
157 /* Write test buffer contents into temporary file */
robbiew91361372003-07-09 17:51:16 +0000158 if (write(fildes, tst_buff, page_sz) != page_sz) {
plars865695b2001-08-27 22:15:12 +0000159 free(tst_buff);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800160 tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000161 }
162
163 /* Free the memory allocated for test buffer */
164 free(tst_buff);
165
Garrett Cooper11d51042010-11-22 20:47:29 -0800166 /* Make sure proper permissions set on file */
167 if (fchmod(fildes, 0444) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 tst_brkm(TFAIL | TERRNO, cleanup, "fchmod of %s failed",
Garrett Cooper11d51042010-11-22 20:47:29 -0800169 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000170 }
171
Garrett Cooper11d51042010-11-22 20:47:29 -0800172 /* Close the temporary file opened for write */
173 if (close(fildes) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed",
Garrett Cooper11d51042010-11-22 20:47:29 -0800175 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000176 }
177
178 /* Open the temporary file again for reading */
179 if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tst_brkm(TFAIL | TERRNO, cleanup, "opening %s readonly failed",
181 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000182 }
183}
184
185/*
Garrett Cooper53740502010-12-16 00:04:01 -0800186 * sig_handler() - Signal Catching function.
plars865695b2001-08-27 22:15:12 +0000187 * This function gets executed when the test process receives
188 * the signal SIGSEGV while trying to access the contents of memory which
189 * is not accessible.
190 */
Cyril Hrubis5efee332013-06-04 20:14:58 +0200191static void sig_handler(int sig)
plars865695b2001-08-27 22:15:12 +0000192{
193 if (sig == SIGSEGV) {
194 /* set the global variable and jump back */
195 pass = 1;
196 siglongjmp(env, 1);
Cyril Hrubis5efee332013-06-04 20:14:58 +0200197 } else {
Garrett Cooper11d51042010-11-22 20:47:29 -0800198 tst_brkm(TBROK, cleanup, "received an unexpected signal: %d",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800199 sig);
Cyril Hrubis5efee332013-06-04 20:14:58 +0200200 }
plars865695b2001-08-27 22:15:12 +0000201}
202
Cyril Hrubis5efee332013-06-04 20:14:58 +0200203static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000204{
subrata_modak56207ce2009-03-23 13:35:39 +0000205 close(fildes);
plars865695b2001-08-27 22:15:12 +0000206 tst_rmdir();
Garrett Cooper52626672010-12-20 15:03:21 -0800207}