summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/lib/libc/sys/t_mprotect.c
blob: 5dc3eae147017ef48a4fd9268347938f0e899aba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* $NetBSD: t_mprotect.c,v 1.4 2016/05/28 14:34:49 christos Exp $ */

/*-
 * Copyright (c) 2011 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jukka Ruohonen.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <sys/cdefs.h>
__RCSID("$NetBSD: t_mprotect.c,v 1.4 2016/05/28 14:34:49 christos Exp $");

#include <sys/param.h>
#include <sys/mman.h>
#include <sys/sysctl.h>
#include <sys/wait.h>

#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <atf-c.h>

#ifdef __NetBSD__
#include "../common/exec_prot.h"
#endif

static long	page = 0;
static int	pax_global = -1;
static int	pax_enabled = -1;
static char	path[] = "mmap";

static void	sighandler(int);
static bool	paxinit(void);
static bool	paxset(int, int);

static void
sighandler(int signo)
{
	_exit(signo);
}

static bool
paxinit(void)
{
	size_t len = sizeof(int);
	int rv;

	rv = sysctlbyname("security.pax.mprotect.global",
	    &pax_global, &len, NULL, 0);

	if (rv != 0)
		return false;

	rv = sysctlbyname("security.pax.mprotect.enabled",
	    &pax_enabled, &len, NULL, 0);

	return rv == 0;
}

static bool
paxset(int global, int enabled)
{
	size_t len = sizeof(int);
	int rv;

	rv = sysctlbyname("security.pax.mprotect.global",
	    NULL, NULL, &global, len);

	if (rv != 0)
		return false;

	rv = sysctlbyname("security.pax.mprotect.enabled",
	    NULL, NULL, &enabled, len);

	if (rv != 0)
		return false;

	return true;
}


ATF_TC_WITH_CLEANUP(mprotect_access);
ATF_TC_HEAD(mprotect_access, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test for EACCES from mprotect(2)");
}

ATF_TC_BODY(mprotect_access, tc)
{
	int prot[2] = { PROT_NONE, PROT_READ };
	void *map;
	size_t i;
	int fd;

	fd = open(path, O_RDONLY | O_CREAT);
	ATF_REQUIRE(fd >= 0);

	/*
	 * The call should fail with EACCES if we try to mark
	 * a PROT_NONE or PROT_READ file/section as PROT_WRITE.
	 */
	for (i = 0; i < __arraycount(prot); i++) {

		map = mmap(NULL, page, prot[i], MAP_SHARED, fd, 0);

		if (map == MAP_FAILED)
			continue;

		errno = 0;

		ATF_REQUIRE(mprotect(map, page, PROT_WRITE) != 0);
		ATF_REQUIRE(errno == EACCES);
		ATF_REQUIRE(munmap(map, page) == 0);
	}

	ATF_REQUIRE(close(fd) == 0);
}

ATF_TC_CLEANUP(mprotect_access, tc)
{
	(void)unlink(path);
}

ATF_TC(mprotect_err);
ATF_TC_HEAD(mprotect_err, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test error conditions of mprotect(2)");
}

ATF_TC_BODY(mprotect_err, tc)
{
	errno = 0;

	ATF_REQUIRE(mprotect((char *)-1, 1, PROT_READ) != 0);
	ATF_REQUIRE(errno == EINVAL);
}

#ifdef __NetBSD__
ATF_TC(mprotect_exec);
ATF_TC_HEAD(mprotect_exec, tc)
{
	atf_tc_set_md_var(tc, "descr",
	    "Test mprotect(2) executable space protections");
}

/*
 * Trivial function -- should fit into a page
 */
ATF_TC_BODY(mprotect_exec, tc)
{
	pid_t pid;
	void *map;
	int sta, xp_support;

	xp_support = exec_prot_support();

	switch (xp_support) {
	case NOTIMPL:
		atf_tc_skip(
		    "Execute protection callback check not implemented");
		break;
	case NO_XP:
		atf_tc_skip(
		    "Host does not support executable space protection");
		break;
	case PARTIAL_XP: case PERPAGE_XP: default:
		break;
	}

	if (!paxinit())
		return;
	if (pax_enabled == 1 && pax_global == 1)
		atf_tc_skip("PaX MPROTECT restrictions enabled");
		

	/*
	 * Map a page read/write and copy a trivial assembly function inside.
	 * We will then change the mapping rights:
	 * - first by setting the execution right, and check that we can
	 *   call the code found in the allocated page.
	 * - second by removing the execution right. This should generate
	 *   a SIGSEGV on architectures that can enforce --x permissions.
	 */

	map = mmap(NULL, page, PROT_WRITE|PROT_READ, MAP_ANON, -1, 0);
	ATF_REQUIRE(map != MAP_FAILED);

	memcpy(map, (void *)return_one,
	    (uintptr_t)return_one_end - (uintptr_t)return_one);
 
	/* give r-x rights then call code in page */
	ATF_REQUIRE(mprotect(map, page, PROT_EXEC|PROT_READ) == 0);
	ATF_REQUIRE(((int (*)(void))map)() == 1);

	/* remove --x right */
	ATF_REQUIRE(mprotect(map, page, PROT_READ) == 0);

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {
		ATF_REQUIRE(signal(SIGSEGV, sighandler) != SIG_ERR);
		ATF_CHECK(((int (*)(void))map)() == 1);
		_exit(0);
	}

	(void)wait(&sta);

	ATF_REQUIRE(munmap(map, page) == 0);

	ATF_REQUIRE(WIFEXITED(sta) != 0);

	switch (xp_support) {
	case PARTIAL_XP:
		/* Partial protection might fail; skip the test when it does */
		if (WEXITSTATUS(sta) != SIGSEGV) {
			atf_tc_skip("Host only supports "
			    "partial executable space protection");
		}
		break;
	case PERPAGE_XP: default:
		/* Per-page --x protection should not fail */
		ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
		break;
	}
}
#endif

ATF_TC(mprotect_pax);
ATF_TC_HEAD(mprotect_pax, tc)
{
	atf_tc_set_md_var(tc, "descr", "PaX restrictions and mprotect(2)");
	atf_tc_set_md_var(tc, "require.user", "root");
}

ATF_TC_BODY(mprotect_pax, tc)
{
	const int prot[4] = { PROT_NONE, PROT_READ, PROT_WRITE };
	const char *str = NULL;
	void *map;
	size_t i;
	int rv;

	if (!paxinit() || !paxset(1, 1))
		return;

	/*
	 * As noted in the original PaX documentation [1],
	 * the following restrictions should apply:
	 *
	 *   (1) creating executable anonymous mappings
	 *
	 *   (2) creating executable/writable file mappings
	 *
	 *   (3) making a non-executable mapping executable
	 *
	 *   (4) making an executable/read-only file mapping
	 *       writable except for performing relocations
	 *       on an ET_DYN ELF file (non-PIC shared library)
	 *
	 *  The following will test only the case (3).
	 *
	 * [1] http://pax.grsecurity.net/docs/mprotect.txt
	 *
	 *     (Sun Apr 3 11:06:53 EEST 2011.)
	 */
	for (i = 0; i < __arraycount(prot); i++) {

		map = mmap(NULL, page, prot[i], MAP_ANON, -1, 0);

		if (map == MAP_FAILED)
			continue;

		rv = mprotect(map, 1, prot[i] | PROT_EXEC);

		(void)munmap(map, page);

		if (rv == 0) {
			str = "non-executable mapping made executable";
			goto out;
		}
	}

out:
	if (pax_global != -1 && pax_enabled != -1)
		(void)paxset(pax_global, pax_enabled);

	if (str != NULL)
		atf_tc_fail("%s", str);
}

ATF_TC(mprotect_write);
ATF_TC_HEAD(mprotect_write, tc)
{
	atf_tc_set_md_var(tc, "descr", "Test mprotect(2) write protections");
}

ATF_TC_BODY(mprotect_write, tc)
{
	pid_t pid;
	void *map;
	int sta;

	/*
	 * Map a page read/write, change the protection
	 * to read-only with mprotect(2), and try to write
	 * to the page. This should generate a SIGSEGV.
	 */
	map = mmap(NULL, page, PROT_WRITE|PROT_READ, MAP_ANON, -1, 0);
	ATF_REQUIRE(map != MAP_FAILED);

	ATF_REQUIRE(strlcpy(map, "XXX", 3) == 3);
	ATF_REQUIRE(mprotect(map, page, PROT_READ) == 0);

	pid = fork();
	ATF_REQUIRE(pid >= 0);

	if (pid == 0) {
		ATF_REQUIRE(signal(SIGSEGV, sighandler) != SIG_ERR);
		ATF_REQUIRE(strlcpy(map, "XXX", 3) == 0);
	}

	(void)wait(&sta);

	ATF_REQUIRE(WIFEXITED(sta) != 0);
	ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
	ATF_REQUIRE(munmap(map, page) == 0);
}

ATF_TP_ADD_TCS(tp)
{
	page = sysconf(_SC_PAGESIZE);
	ATF_REQUIRE(page >= 0);

	ATF_TP_ADD_TC(tp, mprotect_access);
	ATF_TP_ADD_TC(tp, mprotect_err);
#ifdef __NetBSD__
	ATF_TP_ADD_TC(tp, mprotect_exec);
#endif
	ATF_TP_ADD_TC(tp, mprotect_pax);
	ATF_TP_ADD_TC(tp, mprotect_write);

	return atf_no_error();
}
OpenPOWER on IntegriCloud