plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +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 | 7e40c4e | 2013-06-04 16:12:37 +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 | 7e40c4e | 2013-06-04 16:12:37 +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 | 7e40c4e | 2013-06-04 16:12:37 +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 | * DESCRIPTION |
| 21 | * Testcase to check the error conditions for mprotect(2) |
| 22 | * |
| 23 | * ALGORITHM |
| 24 | * test1: |
| 25 | * Invoke mprotect() with an address of 0. Check if error |
plars | a42e4b9 | 2002-04-03 21:25:10 +0000 | [diff] [blame] | 26 | * is set to ENOMEM. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | * test2: |
| 28 | * Invoke mprotect() with an address that is not a multiple |
| 29 | * of PAGESIZE. EINVAL |
| 30 | * test3: |
| 31 | * Mmap a file with only read permission (PROT_READ). |
| 32 | * Try to set write permission (PROT_WRITE) using mprotect(2). |
| 33 | * Check that error is set to EACCES. |
| 34 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * HISTORY |
| 36 | * 07/2001 Ported by Wayne Boyer |
plars | a42e4b9 | 2002-04-03 21:25:10 +0000 | [diff] [blame] | 37 | * 03/2002 Paul Larson: case 1 should expect ENOMEM not EFAULT |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
| 40 | #include <fcntl.h> |
| 41 | #include <errno.h> |
| 42 | #include <sys/mman.h> |
robbiew | b2af061 | 2002-04-03 22:18:59 +0000 | [diff] [blame] | 43 | #include <stdlib.h> |
robbiew | ab5d7fe | 2003-06-17 20:31:09 +0000 | [diff] [blame] | 44 | #include <unistd.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 47 | char *TCID = "mprotect01"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | int TST_TOTAL = 3; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 50 | struct test_case { |
| 51 | void *addr; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 52 | int len; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | int prot; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 54 | int error; |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 55 | void (*setupfunc) (struct test_case *self); |
| 56 | }; |
| 57 | |
| 58 | static void cleanup(void); |
| 59 | static void setup(void); |
| 60 | static void setup1(struct test_case *self); |
| 61 | static void setup2(struct test_case *self); |
| 62 | static void setup3(struct test_case *self); |
| 63 | |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 64 | static int fd; |
| 65 | |
| 66 | struct test_case TC[] = { |
robbiew | ab5d7fe | 2003-06-17 20:31:09 +0000 | [diff] [blame] | 67 | /* Check for ENOMEM passing memory that cannot be accessed. */ |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 68 | {NULL, 0, PROT_READ, ENOMEM, setup1}, |
| 69 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 70 | /* |
| 71 | * Check for EINVAL by passing a pointer which is not a |
| 72 | * multiple of PAGESIZE. |
| 73 | */ |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 74 | {NULL, 1024, PROT_READ, EINVAL, setup2}, |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 75 | /* |
| 76 | * Check for EACCES by trying to mark a section of memory |
| 77 | * which has been mmap'ed as read-only, as PROT_WRITE |
| 78 | */ |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 79 | {NULL, 0, PROT_WRITE, EACCES, setup3} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 82 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 84 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 86 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 88 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 89 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 91 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 94 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | for (i = 0; i < TST_TOTAL; i++) { |
| 97 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 98 | if (TC[i].setupfunc != NULL) |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 99 | TC[i].setupfunc(&TC[i]); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 101 | TEST(mprotect(TC[i].addr, TC[i].len, TC[i].prot)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | if (TEST_RETURN != -1) { |
| 104 | tst_resm(TFAIL, "call succeeded unexpectedly"); |
| 105 | continue; |
| 106 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 108 | if (TEST_ERRNO == TC[i].error) { |
| 109 | tst_resm(TPASS, "expected failure - " |
| 110 | "errno = %d : %s", TEST_ERRNO, |
| 111 | strerror(TEST_ERRNO)); |
| 112 | } else { |
| 113 | tst_resm(TFAIL, "unexpected error - %d : %s - " |
| 114 | "expected %d", TEST_ERRNO, |
| 115 | strerror(TEST_ERRNO), TC[i].error); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | cleanup(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 120 | tst_exit(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 121 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 123 | static void setup1(struct test_case *self) |
robbiew | ab5d7fe | 2003-06-17 20:31:09 +0000 | [diff] [blame] | 124 | { |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 125 | self->len = getpagesize() + 1; |
robbiew | ab5d7fe | 2003-06-17 20:31:09 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 128 | static void setup2(struct test_case *self) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | { |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 130 | self->addr = malloc(getpagesize()); |
robbiew | 9136137 | 2003-07-09 17:51:16 +0000 | [diff] [blame] | 131 | |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 132 | if (self->addr == NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | tst_brkm(TINFO, cleanup, "malloc failed"); |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 134 | |
| 135 | /* Ensure addr2 is not page aligned */ |
| 136 | self->addr++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 139 | static void setup3(struct test_case *self) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | { |
| 141 | fd = open("/etc/passwd", O_RDONLY); |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 142 | if (fd < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 143 | tst_brkm(TBROK, cleanup, "open failed"); |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 144 | |
| 145 | self->len = getpagesize(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * mmap the PAGESIZE bytes as read only. |
| 149 | */ |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 150 | self->addr = mmap(0, self->len, PROT_READ, MAP_SHARED, fd, 0); |
| 151 | if (self->addr == MAP_FAILED) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | tst_brkm(TBROK, cleanup, "mmap failed"); |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 153 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 156 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 159 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | TEST_PAUSE; |
| 161 | } |
| 162 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 163 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | { |
Cyril Hrubis | b9f9262 | 2013-06-04 17:20:04 +0200 | [diff] [blame] | 165 | close(fd); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 166 | } |