plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 3 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 8 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 13 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * HISTORY |
| 36 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
| 52 | #define TEMPFILE "mmapfile" |
| 53 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 54 | char *TCID = "mmap05"; |
| 55 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 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); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 67 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 69 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 70 | const char *msg; |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 71 | char file_content; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 72 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 73 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | setup(); |
| 77 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 79 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 80 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 82 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | * Call mmap to map the temporary file 'TEMPFILE' |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | * with no access. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 86 | errno = 0; |
| 87 | addr = mmap(0, page_sz, PROT_NONE, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | MAP_FILE | MAP_SHARED, fildes, 0); |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 89 | TEST_ERRNO = errno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
| 91 | /* Check for the return value of mmap() */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 92 | if (addr == MAP_FAILED) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | tst_resm(TFAIL | TERRNO, "mmap() failed on %s", |
| 94 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | continue; |
| 96 | } |
| 97 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 98 | /* |
| 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 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 111 | if (pass) { |
| 112 | tst_resm(TPASS, "Got SIGSEGV as expected"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 114 | tst_resm(TFAIL, "Mapped memory region with NO " |
| 115 | "access is accessible"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* Unmap mapped memory and reset pass in case we are looping */ |
robbiew | cc28f4c | 2003-03-26 22:46:04 +0000 | [diff] [blame] | 119 | if (munmap((void *)addr, page_sz) != 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | tst_brkm(TFAIL | TERRNO, cleanup, "munmap failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | } |
| 122 | pass = 0; |
| 123 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 124 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 125 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 127 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 128 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 130 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | { |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 132 | char *tst_buff; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | tst_sig(NOFORK, sig_handler, cleanup); |
| 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | TEST_PAUSE; |
| 137 | |
Cyril Hrubis | ec22fb2 | 2013-06-04 20:37:05 +0200 | [diff] [blame] | 138 | page_sz = getpagesize(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | |
| 140 | /* Allocate space for the test buffer */ |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 141 | if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 142 | tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | /* Fill the test buffer with the known data */ |
| 146 | memset(tst_buff, 'A', page_sz); |
| 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | tst_tmpdir(); |
| 149 | |
| 150 | /* Creat a temporary file used for mapping */ |
| 151 | if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | free(tst_buff); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 153 | tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed", |
| 154 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* Write test buffer contents into temporary file */ |
robbiew | 9136137 | 2003-07-09 17:51:16 +0000 | [diff] [blame] | 158 | if (write(fildes, tst_buff, page_sz) != page_sz) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | free(tst_buff); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 160 | tst_brkm(TFAIL, cleanup, "writing to %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | /* Free the memory allocated for test buffer */ |
| 164 | free(tst_buff); |
| 165 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 166 | /* Make sure proper permissions set on file */ |
| 167 | if (fchmod(fildes, 0444) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 168 | tst_brkm(TFAIL | TERRNO, cleanup, "fchmod of %s failed", |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 169 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 172 | /* Close the temporary file opened for write */ |
| 173 | if (close(fildes) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed", |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 175 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | /* Open the temporary file again for reading */ |
| 179 | if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | tst_brkm(TFAIL | TERRNO, cleanup, "opening %s readonly failed", |
| 181 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | /* |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 186 | * sig_handler() - Signal Catching function. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 187 | * 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 Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 191 | static void sig_handler(int sig) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | { |
| 193 | if (sig == SIGSEGV) { |
| 194 | /* set the global variable and jump back */ |
| 195 | pass = 1; |
| 196 | siglongjmp(env, 1); |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 197 | } else { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 198 | tst_brkm(TBROK, cleanup, "received an unexpected signal: %d", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 199 | sig); |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 200 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 203 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | close(fildes); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 206 | tst_rmdir(); |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 207 | } |