1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software;  you can redistribute it and/or modify
5f08c3bdfSopenharmony_ci * it under the terms of the GNU General Public License as published by
6f08c3bdfSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or
7f08c3bdfSopenharmony_ci * (at your option) any later version.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it will be useful,
10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12f08c3bdfSopenharmony_ci * the GNU General Public License for more details.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
15f08c3bdfSopenharmony_ci * along with this program;  if not, write to the Free Software
16f08c3bdfSopenharmony_ci * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci/*
20f08c3bdfSopenharmony_ci * DESCRIPTION
21f08c3bdfSopenharmony_ci *	Testcase to check the mprotect(2) system call.
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci * ALGORITHM
24f08c3bdfSopenharmony_ci *	Create a mapped region using mmap with READ permission.
25f08c3bdfSopenharmony_ci *	Try to write into that region in a child process using memcpy.
26f08c3bdfSopenharmony_ci *	Verify that a SIGSEGV is generated.
27f08c3bdfSopenharmony_ci *	Now change the protection to WRITE using mprotect(2).
28f08c3bdfSopenharmony_ci *	Again try to write into the mapped region.
29f08c3bdfSopenharmony_ci *	Verify that no SIGSEGV is generated.
30f08c3bdfSopenharmony_ci *
31f08c3bdfSopenharmony_ci * HISTORY
32f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
33f08c3bdfSopenharmony_ci *	05/2002 changed over to use tst_sig instead of sigaction
34f08c3bdfSopenharmony_ci */
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#include <sys/mman.h>
37f08c3bdfSopenharmony_ci#include <sys/wait.h>
38f08c3bdfSopenharmony_ci#include <errno.h>
39f08c3bdfSopenharmony_ci#include <fcntl.h>
40f08c3bdfSopenharmony_ci#include <limits.h>
41f08c3bdfSopenharmony_ci#include <stdlib.h>
42f08c3bdfSopenharmony_ci#include "test.h"
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci#include "safe_macros.h"
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cistatic void sighandler(int sig);
47f08c3bdfSopenharmony_cistatic void cleanup(void);
48f08c3bdfSopenharmony_cistatic void setup(void);
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cichar *TCID = "mprotect02";
51f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
52f08c3bdfSopenharmony_cistatic int fd, status;
53f08c3bdfSopenharmony_cistatic char file1[BUFSIZ];
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_cistatic char *addr = MAP_FAILED;
56f08c3bdfSopenharmony_cistatic char buf[] = "abcdefghijklmnopqrstuvwxyz";
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ciint main(int ac, char **av)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	int lc;
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	int bytes_to_write, fd;
63f08c3bdfSopenharmony_ci	size_t num_bytes;
64f08c3bdfSopenharmony_ci	pid_t pid;
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	setup();
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci		tst_count = 0;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci		fd = SAFE_OPEN(cleanup, file1, O_RDWR | O_CREAT, 0777);
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci		num_bytes = getpagesize();
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci		do {
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci			bytes_to_write = MIN(strlen(buf), num_bytes);
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci			num_bytes -=
83f08c3bdfSopenharmony_ci			    SAFE_WRITE(cleanup, SAFE_WRITE_ALL, fd, buf,
84f08c3bdfSopenharmony_ci				bytes_to_write);
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci		} while (0 < num_bytes);
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci		/* mmap the PAGESIZE bytes as read only. */
89f08c3bdfSopenharmony_ci		addr = SAFE_MMAP(cleanup, 0, sizeof(buf), PROT_READ,
90f08c3bdfSopenharmony_ci				 MAP_SHARED, fd, 0);
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci		if ((pid = FORK_OR_VFORK()) == -1)
93f08c3bdfSopenharmony_ci			tst_brkm(TBROK | TERRNO, cleanup, "fork #1 failed");
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci		if (pid == 0) {
96f08c3bdfSopenharmony_ci			memcpy(addr, buf, strlen(buf));
97f08c3bdfSopenharmony_ci			exit(255);
98f08c3bdfSopenharmony_ci		}
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci		SAFE_WAITPID(cleanup, pid, &status, 0);
101f08c3bdfSopenharmony_ci		if (!WIFEXITED(status))
102f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "child exited abnormally "
103f08c3bdfSopenharmony_ci				 "with status: %d", status);
104f08c3bdfSopenharmony_ci		switch (status) {
105f08c3bdfSopenharmony_ci		case 255:
106f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup,
107f08c3bdfSopenharmony_ci				 "memcpy did not generate SIGSEGV");
108f08c3bdfSopenharmony_ci		case 0:
109f08c3bdfSopenharmony_ci			tst_resm(TPASS, "got SIGSEGV as expected");
110f08c3bdfSopenharmony_ci			break;
111f08c3bdfSopenharmony_ci		default:
112f08c3bdfSopenharmony_ci			tst_brkm(TBROK, cleanup, "got unexpected signal: %d",
113f08c3bdfSopenharmony_ci				 status);
114f08c3bdfSopenharmony_ci			break;
115f08c3bdfSopenharmony_ci		}
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci		/* Change the protection to WRITE. */
118f08c3bdfSopenharmony_ci		TEST(mprotect(addr, sizeof(buf), PROT_WRITE));
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_ci		if (TEST_RETURN != -1) {
121f08c3bdfSopenharmony_ci			if ((pid = FORK_OR_VFORK()) == -1)
122f08c3bdfSopenharmony_ci				tst_brkm(TBROK | TERRNO, cleanup,
123f08c3bdfSopenharmony_ci					 "fork #2 failed");
124f08c3bdfSopenharmony_ci
125f08c3bdfSopenharmony_ci			if (pid == 0) {
126f08c3bdfSopenharmony_ci				memcpy(addr, buf, strlen(buf));
127f08c3bdfSopenharmony_ci				exit(0);
128f08c3bdfSopenharmony_ci			}
129f08c3bdfSopenharmony_ci
130f08c3bdfSopenharmony_ci			SAFE_WAITPID(cleanup, pid, &status, 0);
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci			if (WIFEXITED(status) &&
133f08c3bdfSopenharmony_ci			    WEXITSTATUS(status) == 0)
134f08c3bdfSopenharmony_ci				tst_resm(TPASS, "didn't get SIGSEGV");
135f08c3bdfSopenharmony_ci			else
136f08c3bdfSopenharmony_ci				tst_brkm(TBROK, cleanup,
137f08c3bdfSopenharmony_ci					 "child exited abnormally");
138f08c3bdfSopenharmony_ci		} else {
139f08c3bdfSopenharmony_ci			tst_resm(TFAIL | TERRNO, "mprotect failed");
140f08c3bdfSopenharmony_ci			continue;
141f08c3bdfSopenharmony_ci		}
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci		SAFE_MUNMAP(cleanup, addr, sizeof(buf));
144f08c3bdfSopenharmony_ci		addr = MAP_FAILED;
145f08c3bdfSopenharmony_ci
146f08c3bdfSopenharmony_ci		SAFE_CLOSE(cleanup, fd);
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_ci		SAFE_UNLINK(cleanup, file1);
149f08c3bdfSopenharmony_ci
150f08c3bdfSopenharmony_ci	}
151f08c3bdfSopenharmony_ci
152f08c3bdfSopenharmony_ci	cleanup();
153f08c3bdfSopenharmony_ci	tst_exit();
154f08c3bdfSopenharmony_ci}
155f08c3bdfSopenharmony_ci
156f08c3bdfSopenharmony_cistatic void sighandler(int sig)
157f08c3bdfSopenharmony_ci{
158f08c3bdfSopenharmony_ci	_exit((sig == SIGSEGV) ? 0 : sig);
159f08c3bdfSopenharmony_ci}
160f08c3bdfSopenharmony_ci
161f08c3bdfSopenharmony_cistatic void setup(void)
162f08c3bdfSopenharmony_ci{
163f08c3bdfSopenharmony_ci	tst_sig(FORK, sighandler, cleanup);
164f08c3bdfSopenharmony_ci
165f08c3bdfSopenharmony_ci	TEST_PAUSE;
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_ci	tst_tmpdir();
168f08c3bdfSopenharmony_ci
169f08c3bdfSopenharmony_ci	sprintf(file1, "mprotect02.tmp.%d", getpid());
170f08c3bdfSopenharmony_ci}
171f08c3bdfSopenharmony_ci
172f08c3bdfSopenharmony_cistatic void cleanup(void)
173f08c3bdfSopenharmony_ci{
174f08c3bdfSopenharmony_ci	if (addr != MAP_FAILED) {
175f08c3bdfSopenharmony_ci		SAFE_MUNMAP(NULL, addr, sizeof(buf));
176f08c3bdfSopenharmony_ci		SAFE_CLOSE(NULL, fd);
177f08c3bdfSopenharmony_ci	}
178f08c3bdfSopenharmony_ci
179f08c3bdfSopenharmony_ci	tst_rmdir();
180f08c3bdfSopenharmony_ci}
181