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