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 a mapped region with execute access |
| 22 | * under the following conditions - |
| 23 | * - The prot parameter is set to PROT_EXE |
| 24 | * - The file descriptor is open for read |
| 25 | * - The file being mapped has execute permission bit set. |
| 26 | * - The minimum file permissions should be 0555. |
| 27 | * |
| 28 | * The call should succeed to map the file creating mapped memory with the |
| 29 | * required attributes. |
| 30 | * |
| 31 | * Expected Result: |
| 32 | * mmap() should succeed returning the address of the mapped region, |
| 33 | * and the mapped region should contain the contents of the mapped file. |
vapier | 42c5b6b | 2007-07-21 20:40:56 +0000 | [diff] [blame] | 34 | * but with ia64 and PARISC/hppa, |
robbiew | 30d1bed | 2003-05-07 15:50:36 +0000 | [diff] [blame] | 35 | * an attempt to access the contents of the mapped region should give |
| 36 | * rise to the signal SIGSEGV. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | * HISTORY |
| 39 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | #include <sys/types.h> |
| 44 | #include <errno.h> |
| 45 | #include <unistd.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <string.h> |
| 48 | #include <signal.h> |
| 49 | #include <sys/stat.h> |
| 50 | #include <sys/mman.h> |
robbiew | 30d1bed | 2003-05-07 15:50:36 +0000 | [diff] [blame] | 51 | #include <setjmp.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
| 53 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
| 55 | #define TEMPFILE "mmapfile" |
| 56 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 57 | char *TCID = "mmap03"; |
| 58 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 60 | static size_t page_sz; |
| 61 | static char *addr; |
| 62 | static char *dummy; |
| 63 | static int fildes; |
| 64 | static volatile int pass = 0; |
| 65 | static sigjmp_buf env; |
| 66 | |
| 67 | static void setup(void); |
| 68 | static void cleanup(void); |
| 69 | static void sig_handler(int sig); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 73 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 74 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 75 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 76 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | setup(); |
| 80 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 82 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 83 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 85 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | * Call mmap to map the temporary file 'TEMPFILE' |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 87 | * with execute access. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 89 | errno = 0; |
| 90 | addr = mmap(0, page_sz, PROT_EXEC, |
| 91 | MAP_FILE | MAP_SHARED, fildes, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
| 93 | /* Check for the return value of mmap() */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 94 | if (addr == MAP_FAILED) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 95 | tst_resm(TFAIL | TERRNO, "mmap() failed on %s", |
| 96 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | continue; |
| 98 | } |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 99 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 100 | /* |
| 101 | * Read the file contents into the dummy |
| 102 | * variable. |
| 103 | */ |
| 104 | if (read(fildes, dummy, page_sz) < 0) { |
| 105 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 106 | "reading %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 109 | /* |
| 110 | * Check whether the mapped memory region |
| 111 | * has the file contents. |
| 112 | * |
| 113 | * with ia64 and PARISC/hppa, this should |
| 114 | * generate a SIGSEGV which will be caught below. |
| 115 | * |
| 116 | */ |
| 117 | |
| 118 | if (sigsetjmp(env, 1) == 0) { |
| 119 | if (memcmp(dummy, addr, page_sz)) { |
| 120 | tst_resm(TFAIL, |
| 121 | "mapped memory region " |
| 122 | "contains invalid data"); |
| 123 | } else { |
| 124 | tst_resm(TPASS, |
| 125 | "mmap() functionality is " |
| 126 | "correct"); |
| 127 | } |
| 128 | } |
| 129 | #if defined(__ia64__) || defined(__hppa__) |
| 130 | if (pass) { |
| 131 | tst_resm(TPASS, "Got SIGSEGV as expected"); |
| 132 | } else { |
| 133 | tst_resm(TFAIL, "Mapped memory region with NO " |
| 134 | "access is accessible"); |
| 135 | } |
| 136 | #endif |
| 137 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 138 | /* Clean up things in case we are looping */ |
| 139 | /* Unmap the mapped memory */ |
| 140 | if (munmap(addr, page_sz) != 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 141 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 142 | "failed to unmap the mmapped pages"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | } |
robbiew | 30d1bed | 2003-05-07 15:50:36 +0000 | [diff] [blame] | 144 | pass = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 146 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 147 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 149 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 150 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 152 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | { |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 154 | char *tst_buff; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
robbiew | 30d1bed | 2003-05-07 15:50:36 +0000 | [diff] [blame] | 156 | tst_sig(NOFORK, sig_handler, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | TEST_PAUSE; |
| 159 | |
Cyril Hrubis | ec22fb2 | 2013-06-04 20:37:05 +0200 | [diff] [blame] | 160 | page_sz = getpagesize(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | |
| 162 | /* Allocate space for the test buffer */ |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 163 | if ((tst_buff = calloc(page_sz, sizeof(char))) == NULL) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 164 | tst_brkm(TFAIL, NULL, "calloc failed (tst_buff)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /* Fill the test buffer with the known data */ |
| 168 | memset(tst_buff, 'A', page_sz); |
| 169 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 170 | tst_tmpdir(); |
| 171 | |
| 172 | /* Creat a temporary file used for mapping */ |
| 173 | if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | free(tst_buff); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 175 | tst_brkm(TFAIL | TERRNO, cleanup, "opening %s failed", |
| 176 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /* Write test buffer contents into temporary file */ |
robbiew | 9136137 | 2003-07-09 17:51:16 +0000 | [diff] [blame] | 180 | if (write(fildes, tst_buff, page_sz) < page_sz) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | free(tst_buff); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 182 | tst_brkm(TFAIL | TERRNO, cleanup, "writing to %s failed", |
| 183 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* Free the memory allocated for test buffer */ |
| 187 | free(tst_buff); |
| 188 | |
| 189 | /* Make sure proper permissions set on file */ |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 190 | if (fchmod(fildes, 0555) < 0) { |
| 191 | tst_brkm(TFAIL, cleanup, "fchmod of %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /* Close the temporary file opened for write */ |
| 195 | if (close(fildes) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 196 | tst_brkm(TFAIL | TERRNO, cleanup, "closing %s failed", |
| 197 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Allocate and initialize dummy string of system page size bytes */ |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 201 | if ((dummy = calloc(page_sz, sizeof(char))) == NULL) { |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 202 | tst_brkm(TFAIL, cleanup, "calloc failed (dummy)"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* Open the temporary file again for reading */ |
| 206 | if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 207 | tst_brkm(TFAIL | TERRNO, cleanup, |
| 208 | "opening %s read-only failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
robbiew | 30d1bed | 2003-05-07 15:50:36 +0000 | [diff] [blame] | 213 | * This function gets executed when the test process receives |
| 214 | * the signal SIGSEGV while trying to access the contents of memory which |
| 215 | * is not accessible. |
| 216 | */ |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 217 | static void sig_handler(int sig) |
robbiew | 30d1bed | 2003-05-07 15:50:36 +0000 | [diff] [blame] | 218 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 219 | if (sig == SIGSEGV) { |
| 220 | /* set the global variable and jump back */ |
| 221 | pass = 1; |
| 222 | siglongjmp(env, 1); |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 223 | } else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 224 | tst_brkm(TBROK, cleanup, "received an unexpected signal"); |
robbiew | 30d1bed | 2003-05-07 15:50:36 +0000 | [diff] [blame] | 225 | } |
| 226 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 227 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 229 | close(fildes); |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 230 | free(dummy); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | tst_rmdir(); |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 232 | } |