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 | /* |
| 20 | * NAME |
| 21 | * mprotect03.c |
| 22 | * |
| 23 | * DESCRIPTION |
| 24 | * Testcase to check the mprotect(2) system call. |
| 25 | * |
| 26 | * ALGORITHM |
| 27 | * Create a shared mapped file region with PROT_READ | PROT_WRITE |
| 28 | * using the mmap(2) call. Then, use mprotect(2) to disable the |
| 29 | * write permission on the mapped region. Then, attempt to write to |
| 30 | * the mapped region using memcpy(). This would generate a sigsegv. |
| 31 | * Since the sigsegv is generated, this needs to be done in a child |
| 32 | * process (as sigsegv would repeatedly be generated). The testcase |
| 33 | * succeeds only when this sigsegv is generated while attempting to |
| 34 | * memcpy() on a shared region with only read permission. |
| 35 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | * HISTORY |
| 37 | * 07/2001 Ported by Wayne Boyer |
plars | 68c75a7 | 2002-05-28 19:00:22 +0000 | [diff] [blame] | 38 | * 05/2002 changed over to use tst_sig instead of sigaction |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | */ |
| 40 | |
| 41 | #include <errno.h> |
| 42 | #include <fcntl.h> |
| 43 | #include <sys/mman.h> |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 44 | #include <limits.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | #include <signal.h> |
Steven Jackson | 249f405 | 2016-12-13 16:16:00 +0000 | [diff] [blame^] | 46 | #include <sys/wait.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 47 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
Cyril Hrubis | beb0b7d | 2013-06-04 17:47:40 +0200 | [diff] [blame] | 49 | #include "safe_macros.h" |
| 50 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | #ifndef PAGESIZE |
| 52 | #define PAGESIZE 4096 |
| 53 | #endif |
| 54 | #define FAILED 1 |
| 55 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 56 | static void cleanup(void); |
| 57 | static void setup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | char *TCID = "mprotect03"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | int TST_TOTAL = 1; |
| 61 | int status; |
| 62 | char file1[BUFSIZ]; |
| 63 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 64 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 66 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 68 | char *addr; |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 69 | int fd, pid; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | char *buf = "abcdefghijklmnopqrstuvwxyz"; |
| 71 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 72 | tst_parse_opts(ac, av, NULL, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 74 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 77 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 79 | if ((fd = open(file1, O_RDWR | O_CREAT, 0777)) < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | tst_brkm(TBROK, cleanup, "open failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
Cyril Hrubis | beb0b7d | 2013-06-04 17:47:40 +0200 | [diff] [blame] | 82 | SAFE_WRITE(cleanup, 1, fd, buf, strlen(buf)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * mmap the PAGESIZE bytes as read only. |
| 86 | */ |
| 87 | addr = mmap(0, strlen(buf), PROT_READ | PROT_WRITE, MAP_SHARED, |
| 88 | fd, 0); |
Cyril Hrubis | 7ff5ebf | 2013-06-04 16:32:39 +0200 | [diff] [blame] | 89 | if (addr == MAP_FAILED) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | tst_brkm(TBROK, cleanup, "mmap failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Try to change the protection to WRITE. |
| 94 | */ |
| 95 | TEST(mprotect(addr, strlen(buf), PROT_READ)); |
| 96 | |
| 97 | if (TEST_RETURN != -1) { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 98 | if ((pid = FORK_OR_VFORK()) == -1) { |
| 99 | tst_brkm(TBROK, cleanup, "fork failed"); |
| 100 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 102 | if (pid == 0) { |
| 103 | memcpy(addr, buf, strlen(buf)); |
| 104 | tst_resm(TINFO, "memcpy() did " |
| 105 | "not generate SIGSEGV"); |
| 106 | exit(1); |
| 107 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 109 | waitpid(pid, &status, 0); |
| 110 | if (WEXITSTATUS(status) != 0) { |
| 111 | tst_resm(TFAIL, "child returned " |
| 112 | "unexpected status"); |
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(TPASS, "SIGSEGV generated " |
| 115 | "as expected"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | } |
| 117 | } else { |
| 118 | tst_resm(TFAIL, "mprotect failed " |
| 119 | "unexpectedly, errno: %d", errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | |
| 122 | /* clean up things in case we are looping */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | if (munmap(addr, strlen(buf)) == -1) { |
subrata_modak | dad6e1a | 2007-10-30 10:46:58 +0000 | [diff] [blame] | 124 | tst_brkm(TBROK, cleanup, "munamp failed"); |
| 125 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | if (close(fd) == -1) { |
| 127 | tst_brkm(TBROK, cleanup, "close failed"); |
| 128 | } |
| 129 | if (unlink(file1) == -1) { |
| 130 | tst_brkm(TBROK, cleanup, "unlink failed"); |
| 131 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 133 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 135 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 138 | static void sighandler(int sig) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 139 | { |
| 140 | if (sig == SIGSEGV) { |
plars | 68c75a7 | 2002-05-28 19:00:22 +0000 | [diff] [blame] | 141 | tst_resm(TINFO, "received signal: SIGSEGV"); |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 142 | tst_exit(); |
| 143 | } else |
plars | 68c75a7 | 2002-05-28 19:00:22 +0000 | [diff] [blame] | 144 | tst_brkm(TBROK, 0, "Unexpected signal %d received.", sig); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 146 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 147 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | { |
plars | 68c75a7 | 2002-05-28 19:00:22 +0000 | [diff] [blame] | 149 | tst_sig(FORK, sighandler, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 150 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | TEST_PAUSE; |
| 152 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 153 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | |
| 155 | sprintf(file1, "mprotect03.tmp.%d", getpid()); |
| 156 | } |
| 157 | |
Cyril Hrubis | 7e40c4e | 2013-06-04 16:12:37 +0200 | [diff] [blame] | 158 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 159 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | tst_rmdir(); |
Garrett Cooper | 5262667 | 2010-12-20 15:03:21 -0800 | [diff] [blame] | 161 | } |