xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mprotect/mprotect01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  *
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.
8  *
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.
13  *
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
17  */
18 
19 /*
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
26  *		is set to ENOMEM.
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  *
35  * HISTORY
36  *	07/2001 Ported by Wayne Boyer
37  *	03/2002 Paul Larson: case 1 should expect ENOMEM not EFAULT
38  */
39 
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <sys/mman.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include "test.h"
46 #include "lapi/syscalls.h"
47 #include "safe_macros.h"
48 
49 char *TCID = "mprotect01";
50 int TST_TOTAL = 3;
51 
52 struct test_case {
53 	void *addr;
54 	int len;
55 	int prot;
56 	int error;
57 	void (*setupfunc) (struct test_case *self);
58 };
59 
60 static void cleanup(void);
61 static void setup(void);
62 static void setup1(struct test_case *self);
63 static void setup2(struct test_case *self);
64 static void setup3(struct test_case *self);
65 
66 static int fd;
67 
68 struct test_case TC[] = {
69 	/* Check for ENOMEM passing memory that cannot be accessed. */
70 	{NULL, 0, PROT_READ, ENOMEM, setup1},
71 
72 	/*
73 	 * Check for EINVAL by passing a pointer which is not a
74 	 * multiple of PAGESIZE.
75 	 */
76 	{NULL, 1024, PROT_READ, EINVAL, setup2},
77 	/*
78 	 * Check for EACCES by trying to mark a section of memory
79 	 * which has been mmap'ed as read-only, as PROT_WRITE
80 	 */
81 	{NULL, 0, PROT_WRITE, EACCES, setup3}
82 };
83 
main(int ac,char ** av)84 int main(int ac, char **av)
85 {
86 	int lc;
87 	int i;
88 
89 	tst_parse_opts(ac, av, NULL, NULL);
90 
91 	setup();
92 
93 	for (lc = 0; TEST_LOOPING(lc); lc++) {
94 		tst_count = 0;
95 
96 		for (i = 0; i < TST_TOTAL; i++) {
97 
98 			if (TC[i].setupfunc != NULL)
99 				TC[i].setupfunc(&TC[i]);
100 
101 			TEST(tst_syscall(__NR_mprotect, TC[i].addr, TC[i].len, TC[i].prot));
102 
103 			if (TEST_RETURN != -1) {
104 				tst_resm(TFAIL, "call succeeded unexpectedly");
105 				continue;
106 			}
107 
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);
116 			}
117 		}
118 	}
119 	cleanup();
120 	tst_exit();
121 }
122 
setup1(struct test_case * self)123 static void setup1(struct test_case *self)
124 {
125 	self->len = getpagesize() + 1;
126 }
127 
setup2(struct test_case * self)128 static void setup2(struct test_case *self)
129 {
130 	self->addr = malloc(getpagesize());
131 
132 	if (self->addr == NULL)
133 		tst_brkm(TINFO, cleanup, "malloc failed");
134 
135 	/* Ensure addr2 is not page aligned */
136 	self->addr++;
137 }
138 
setup3(struct test_case * self)139 static void setup3(struct test_case *self)
140 {
141 	fd = SAFE_OPEN(cleanup, "/dev/zero", O_RDONLY);
142 
143 	self->len = getpagesize();
144 
145 	/*
146 	 * mmap the PAGESIZE bytes as read only.
147 	 */
148 	self->addr = mmap(0, self->len, PROT_READ, MAP_SHARED, fd, 0);
149 	if (self->addr == MAP_FAILED)
150 		tst_brkm(TBROK, cleanup, "mmap failed");
151 
152 }
153 
setup(void)154 static void setup(void)
155 {
156 	tst_sig(FORK, DEF_HANDLER, cleanup);
157 
158 	TEST_PAUSE;
159 }
160 
cleanup(void)161 static void cleanup(void)
162 {
163 	close(fd);
164 }
165