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