blob: 4bda261d373349e2d9053f3afea1af16b4e1efc2 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +02002 * Copyright (c) International Business Machines Corp., 2001
plars865695b2001-08-27 22:15:12 +00003 *
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +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 Hrubis7e40c4e2013-06-04 16:12:37 +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 Hrubis7e40c4e2013-06-04 16:12:37 +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/*
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 *
plars865695b2001-08-27 22:15:12 +000036 * HISTORY
37 * 07/2001 Ported by Wayne Boyer
plars68c75a72002-05-28 19:00:22 +000038 * 05/2002 changed over to use tst_sig instead of sigaction
plars865695b2001-08-27 22:15:12 +000039 */
40
41#include <errno.h>
42#include <fcntl.h>
43#include <sys/mman.h>
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020044#include <limits.h>
plars865695b2001-08-27 22:15:12 +000045#include <signal.h>
Steven Jackson249f4052016-12-13 16:16:00 +000046#include <sys/wait.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080047#include "test.h"
plars865695b2001-08-27 22:15:12 +000048
Cyril Hrubisbeb0b7d2013-06-04 17:47:40 +020049#include "safe_macros.h"
50
plars865695b2001-08-27 22:15:12 +000051#ifndef PAGESIZE
52#define PAGESIZE 4096
53#endif
54#define FAILED 1
55
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020056static void cleanup(void);
57static void setup(void);
plars865695b2001-08-27 22:15:12 +000058
subrata_modak56207ce2009-03-23 13:35:39 +000059char *TCID = "mprotect03";
plars865695b2001-08-27 22:15:12 +000060int TST_TOTAL = 1;
61int status;
62char file1[BUFSIZ];
63
plars74948ad2002-11-14 16:16:14 +000064int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000065{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020066 int lc;
plars865695b2001-08-27 22:15:12 +000067
68 char *addr;
plars74948ad2002-11-14 16:16:14 +000069 int fd, pid;
plars865695b2001-08-27 22:15:12 +000070 char *buf = "abcdefghijklmnopqrstuvwxyz";
71
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010072 tst_parse_opts(ac, av, NULL, NULL);
plars865695b2001-08-27 22:15:12 +000073
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020074 setup();
plars865695b2001-08-27 22:15:12 +000075
plars865695b2001-08-27 22:15:12 +000076 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080077 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000078
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +020079 if ((fd = open(file1, O_RDWR | O_CREAT, 0777)) < 0)
plars865695b2001-08-27 22:15:12 +000080 tst_brkm(TBROK, cleanup, "open failed");
plars865695b2001-08-27 22:15:12 +000081
Cyril Hrubisbeb0b7d2013-06-04 17:47:40 +020082 SAFE_WRITE(cleanup, 1, fd, buf, strlen(buf));
plars865695b2001-08-27 22:15:12 +000083
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 Hrubis7ff5ebf2013-06-04 16:32:39 +020089 if (addr == MAP_FAILED)
plars865695b2001-08-27 22:15:12 +000090 tst_brkm(TBROK, cleanup, "mmap failed");
plars865695b2001-08-27 22:15:12 +000091
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 Hrubise38b9612014-06-02 17:20:57 +020098 if ((pid = FORK_OR_VFORK()) == -1) {
99 tst_brkm(TBROK, cleanup, "fork failed");
100 }
plars865695b2001-08-27 22:15:12 +0000101
Cyril Hrubise38b9612014-06-02 17:20:57 +0200102 if (pid == 0) {
103 memcpy(addr, buf, strlen(buf));
104 tst_resm(TINFO, "memcpy() did "
105 "not generate SIGSEGV");
106 exit(1);
107 }
plars865695b2001-08-27 22:15:12 +0000108
Cyril Hrubise38b9612014-06-02 17:20:57 +0200109 waitpid(pid, &status, 0);
110 if (WEXITSTATUS(status) != 0) {
111 tst_resm(TFAIL, "child returned "
112 "unexpected status");
plars865695b2001-08-27 22:15:12 +0000113 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200114 tst_resm(TPASS, "SIGSEGV generated "
115 "as expected");
plars865695b2001-08-27 22:15:12 +0000116 }
117 } else {
118 tst_resm(TFAIL, "mprotect failed "
119 "unexpectedly, errno: %d", errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 }
plars865695b2001-08-27 22:15:12 +0000121
122 /* clean up things in case we are looping */
subrata_modak56207ce2009-03-23 13:35:39 +0000123 if (munmap(addr, strlen(buf)) == -1) {
subrata_modakdad6e1a2007-10-30 10:46:58 +0000124 tst_brkm(TBROK, cleanup, "munamp failed");
125 }
plars865695b2001-08-27 22:15:12 +0000126 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_modak56207ce2009-03-23 13:35:39 +0000132 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200133
subrata_modak56207ce2009-03-23 13:35:39 +0000134 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800135 tst_exit();
plars865695b2001-08-27 22:15:12 +0000136}
137
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +0200138static void sighandler(int sig)
subrata_modak56207ce2009-03-23 13:35:39 +0000139{
140 if (sig == SIGSEGV) {
plars68c75a72002-05-28 19:00:22 +0000141 tst_resm(TINFO, "received signal: SIGSEGV");
Garrett Cooper52626672010-12-20 15:03:21 -0800142 tst_exit();
143 } else
plars68c75a72002-05-28 19:00:22 +0000144 tst_brkm(TBROK, 0, "Unexpected signal %d received.", sig);
plars865695b2001-08-27 22:15:12 +0000145}
subrata_modak56207ce2009-03-23 13:35:39 +0000146
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +0200147static void setup(void)
plars865695b2001-08-27 22:15:12 +0000148{
plars68c75a72002-05-28 19:00:22 +0000149 tst_sig(FORK, sighandler, NULL);
plars865695b2001-08-27 22:15:12 +0000150
plars865695b2001-08-27 22:15:12 +0000151 TEST_PAUSE;
152
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +0200153 tst_tmpdir();
plars865695b2001-08-27 22:15:12 +0000154
155 sprintf(file1, "mprotect03.tmp.%d", getpid());
156}
157
Cyril Hrubis7e40c4e2013-06-04 16:12:37 +0200158static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000159{
plars865695b2001-08-27 22:15:12 +0000160 tst_rmdir();
Garrett Cooper52626672010-12-20 15:03:21 -0800161}