xref: /freebsd/contrib/netbsd-tests/lib/libc/sys/t_mprotect.c (revision a63915c2d7ff177ce364488f86eff99949402051)
1640235e2SEnji Cooper /* $NetBSD: t_mprotect.c,v 1.4 2016/05/28 14:34:49 christos Exp $ */
257718be8SEnji Cooper 
357718be8SEnji Cooper /*-
457718be8SEnji Cooper  * Copyright (c) 2011 The NetBSD Foundation, Inc.
557718be8SEnji Cooper  * All rights reserved.
657718be8SEnji Cooper  *
757718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
857718be8SEnji Cooper  * by Jukka Ruohonen.
957718be8SEnji Cooper  *
1057718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
1157718be8SEnji Cooper  * modification, are permitted provided that the following conditions
1257718be8SEnji Cooper  * are met:
1357718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
1457718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
1557718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
1657718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
1757718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
1857718be8SEnji Cooper  *
1957718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2057718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2157718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2257718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2357718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2457718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2557718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2657718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2757718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2857718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2957718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
3057718be8SEnji Cooper  */
3157718be8SEnji Cooper #include <sys/cdefs.h>
32640235e2SEnji Cooper __RCSID("$NetBSD: t_mprotect.c,v 1.4 2016/05/28 14:34:49 christos Exp $");
3357718be8SEnji Cooper 
3457718be8SEnji Cooper #include <sys/param.h>
3557718be8SEnji Cooper #include <sys/mman.h>
3657718be8SEnji Cooper #include <sys/sysctl.h>
3757718be8SEnji Cooper #include <sys/wait.h>
3857718be8SEnji Cooper 
3957718be8SEnji Cooper #include <errno.h>
4057718be8SEnji Cooper #include <fcntl.h>
4157718be8SEnji Cooper #include <stdlib.h>
4257718be8SEnji Cooper #include <string.h>
4357718be8SEnji Cooper #include <unistd.h>
4457718be8SEnji Cooper 
4557718be8SEnji Cooper #include <atf-c.h>
4657718be8SEnji Cooper 
47078fc315SEnji Cooper #ifdef __NetBSD__
4857718be8SEnji Cooper #include "../common/exec_prot.h"
49078fc315SEnji Cooper #endif
5057718be8SEnji Cooper 
5157718be8SEnji Cooper static long	page = 0;
5257718be8SEnji Cooper static int	pax_global = -1;
5357718be8SEnji Cooper static int	pax_enabled = -1;
5457718be8SEnji Cooper static char	path[] = "mmap";
5557718be8SEnji Cooper 
5657718be8SEnji Cooper static void	sighandler(int);
5757718be8SEnji Cooper static bool	paxinit(void);
5857718be8SEnji Cooper static bool	paxset(int, int);
5957718be8SEnji Cooper 
6057718be8SEnji Cooper static void
sighandler(int signo)6157718be8SEnji Cooper sighandler(int signo)
6257718be8SEnji Cooper {
6357718be8SEnji Cooper 	_exit(signo);
6457718be8SEnji Cooper }
6557718be8SEnji Cooper 
6657718be8SEnji Cooper static bool
paxinit(void)6757718be8SEnji Cooper paxinit(void)
6857718be8SEnji Cooper {
6957718be8SEnji Cooper 	size_t len = sizeof(int);
7057718be8SEnji Cooper 	int rv;
7157718be8SEnji Cooper 
7257718be8SEnji Cooper 	rv = sysctlbyname("security.pax.mprotect.global",
7357718be8SEnji Cooper 	    &pax_global, &len, NULL, 0);
7457718be8SEnji Cooper 
7557718be8SEnji Cooper 	if (rv != 0)
7657718be8SEnji Cooper 		return false;
7757718be8SEnji Cooper 
7857718be8SEnji Cooper 	rv = sysctlbyname("security.pax.mprotect.enabled",
7957718be8SEnji Cooper 	    &pax_enabled, &len, NULL, 0);
8057718be8SEnji Cooper 
81640235e2SEnji Cooper 	return rv == 0;
8257718be8SEnji Cooper }
8357718be8SEnji Cooper 
8457718be8SEnji Cooper static bool
paxset(int global,int enabled)8557718be8SEnji Cooper paxset(int global, int enabled)
8657718be8SEnji Cooper {
8757718be8SEnji Cooper 	size_t len = sizeof(int);
8857718be8SEnji Cooper 	int rv;
8957718be8SEnji Cooper 
9057718be8SEnji Cooper 	rv = sysctlbyname("security.pax.mprotect.global",
9157718be8SEnji Cooper 	    NULL, NULL, &global, len);
9257718be8SEnji Cooper 
9357718be8SEnji Cooper 	if (rv != 0)
9457718be8SEnji Cooper 		return false;
9557718be8SEnji Cooper 
9657718be8SEnji Cooper 	rv = sysctlbyname("security.pax.mprotect.enabled",
9757718be8SEnji Cooper 	    NULL, NULL, &enabled, len);
9857718be8SEnji Cooper 
9957718be8SEnji Cooper 	if (rv != 0)
10057718be8SEnji Cooper 		return false;
10157718be8SEnji Cooper 
10257718be8SEnji Cooper 	return true;
10357718be8SEnji Cooper }
10457718be8SEnji Cooper 
10557718be8SEnji Cooper 
10657718be8SEnji Cooper ATF_TC_WITH_CLEANUP(mprotect_access);
ATF_TC_HEAD(mprotect_access,tc)10757718be8SEnji Cooper ATF_TC_HEAD(mprotect_access, tc)
10857718be8SEnji Cooper {
10957718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test for EACCES from mprotect(2)");
11057718be8SEnji Cooper }
11157718be8SEnji Cooper 
ATF_TC_BODY(mprotect_access,tc)11257718be8SEnji Cooper ATF_TC_BODY(mprotect_access, tc)
11357718be8SEnji Cooper {
11457718be8SEnji Cooper 	int prot[2] = { PROT_NONE, PROT_READ };
11557718be8SEnji Cooper 	void *map;
11657718be8SEnji Cooper 	size_t i;
11757718be8SEnji Cooper 	int fd;
11857718be8SEnji Cooper 
119*1575a795SBrooks Davis 	fd = open(path, O_RDONLY | O_CREAT, 0600);
12057718be8SEnji Cooper 	ATF_REQUIRE(fd >= 0);
12157718be8SEnji Cooper 
12257718be8SEnji Cooper 	/*
12357718be8SEnji Cooper 	 * The call should fail with EACCES if we try to mark
12457718be8SEnji Cooper 	 * a PROT_NONE or PROT_READ file/section as PROT_WRITE.
12557718be8SEnji Cooper 	 */
12657718be8SEnji Cooper 	for (i = 0; i < __arraycount(prot); i++) {
12757718be8SEnji Cooper 
12857718be8SEnji Cooper 		map = mmap(NULL, page, prot[i], MAP_SHARED, fd, 0);
12957718be8SEnji Cooper 
13057718be8SEnji Cooper 		if (map == MAP_FAILED)
13157718be8SEnji Cooper 			continue;
13257718be8SEnji Cooper 
13357718be8SEnji Cooper 		errno = 0;
13457718be8SEnji Cooper 
13557718be8SEnji Cooper 		ATF_REQUIRE(mprotect(map, page, PROT_WRITE) != 0);
13657718be8SEnji Cooper 		ATF_REQUIRE(errno == EACCES);
13757718be8SEnji Cooper 		ATF_REQUIRE(munmap(map, page) == 0);
13857718be8SEnji Cooper 	}
13957718be8SEnji Cooper 
14057718be8SEnji Cooper 	ATF_REQUIRE(close(fd) == 0);
14157718be8SEnji Cooper }
14257718be8SEnji Cooper 
ATF_TC_CLEANUP(mprotect_access,tc)14357718be8SEnji Cooper ATF_TC_CLEANUP(mprotect_access, tc)
14457718be8SEnji Cooper {
14557718be8SEnji Cooper 	(void)unlink(path);
14657718be8SEnji Cooper }
14757718be8SEnji Cooper 
14857718be8SEnji Cooper ATF_TC(mprotect_err);
ATF_TC_HEAD(mprotect_err,tc)14957718be8SEnji Cooper ATF_TC_HEAD(mprotect_err, tc)
15057718be8SEnji Cooper {
15157718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test error conditions of mprotect(2)");
15257718be8SEnji Cooper }
15357718be8SEnji Cooper 
ATF_TC_BODY(mprotect_err,tc)15457718be8SEnji Cooper ATF_TC_BODY(mprotect_err, tc)
15557718be8SEnji Cooper {
15657718be8SEnji Cooper 	errno = 0;
15757718be8SEnji Cooper 
15857718be8SEnji Cooper 	ATF_REQUIRE(mprotect((char *)-1, 1, PROT_READ) != 0);
15957718be8SEnji Cooper 	ATF_REQUIRE(errno == EINVAL);
16057718be8SEnji Cooper }
16157718be8SEnji Cooper 
162078fc315SEnji Cooper #ifdef __NetBSD__
16357718be8SEnji Cooper ATF_TC(mprotect_exec);
ATF_TC_HEAD(mprotect_exec,tc)16457718be8SEnji Cooper ATF_TC_HEAD(mprotect_exec, tc)
16557718be8SEnji Cooper {
16657718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr",
16757718be8SEnji Cooper 	    "Test mprotect(2) executable space protections");
16857718be8SEnji Cooper }
16957718be8SEnji Cooper 
17057718be8SEnji Cooper /*
17157718be8SEnji Cooper  * Trivial function -- should fit into a page
17257718be8SEnji Cooper  */
ATF_TC_BODY(mprotect_exec,tc)17357718be8SEnji Cooper ATF_TC_BODY(mprotect_exec, tc)
17457718be8SEnji Cooper {
17557718be8SEnji Cooper 	pid_t pid;
17657718be8SEnji Cooper 	void *map;
17757718be8SEnji Cooper 	int sta, xp_support;
17857718be8SEnji Cooper 
17957718be8SEnji Cooper 	xp_support = exec_prot_support();
18057718be8SEnji Cooper 
18157718be8SEnji Cooper 	switch (xp_support) {
18257718be8SEnji Cooper 	case NOTIMPL:
18357718be8SEnji Cooper 		atf_tc_skip(
18457718be8SEnji Cooper 		    "Execute protection callback check not implemented");
18557718be8SEnji Cooper 		break;
18657718be8SEnji Cooper 	case NO_XP:
18757718be8SEnji Cooper 		atf_tc_skip(
18857718be8SEnji Cooper 		    "Host does not support executable space protection");
18957718be8SEnji Cooper 		break;
19057718be8SEnji Cooper 	case PARTIAL_XP: case PERPAGE_XP: default:
19157718be8SEnji Cooper 		break;
19257718be8SEnji Cooper 	}
19357718be8SEnji Cooper 
194640235e2SEnji Cooper 	if (!paxinit())
195640235e2SEnji Cooper 		return;
196640235e2SEnji Cooper 	if (pax_enabled == 1 && pax_global == 1)
197640235e2SEnji Cooper 		atf_tc_skip("PaX MPROTECT restrictions enabled");
198640235e2SEnji Cooper 
199640235e2SEnji Cooper 
20057718be8SEnji Cooper 	/*
20157718be8SEnji Cooper 	 * Map a page read/write and copy a trivial assembly function inside.
20257718be8SEnji Cooper 	 * We will then change the mapping rights:
20357718be8SEnji Cooper 	 * - first by setting the execution right, and check that we can
20457718be8SEnji Cooper 	 *   call the code found in the allocated page.
20557718be8SEnji Cooper 	 * - second by removing the execution right. This should generate
20657718be8SEnji Cooper 	 *   a SIGSEGV on architectures that can enforce --x permissions.
20757718be8SEnji Cooper 	 */
20857718be8SEnji Cooper 
20957718be8SEnji Cooper 	map = mmap(NULL, page, PROT_WRITE|PROT_READ, MAP_ANON, -1, 0);
21057718be8SEnji Cooper 	ATF_REQUIRE(map != MAP_FAILED);
21157718be8SEnji Cooper 
21257718be8SEnji Cooper 	memcpy(map, (void *)return_one,
21357718be8SEnji Cooper 	    (uintptr_t)return_one_end - (uintptr_t)return_one);
21457718be8SEnji Cooper 
21557718be8SEnji Cooper 	/* give r-x rights then call code in page */
21657718be8SEnji Cooper 	ATF_REQUIRE(mprotect(map, page, PROT_EXEC|PROT_READ) == 0);
21757718be8SEnji Cooper 	ATF_REQUIRE(((int (*)(void))map)() == 1);
21857718be8SEnji Cooper 
21957718be8SEnji Cooper 	/* remove --x right */
22057718be8SEnji Cooper 	ATF_REQUIRE(mprotect(map, page, PROT_READ) == 0);
22157718be8SEnji Cooper 
22257718be8SEnji Cooper 	pid = fork();
22357718be8SEnji Cooper 	ATF_REQUIRE(pid >= 0);
22457718be8SEnji Cooper 
22557718be8SEnji Cooper 	if (pid == 0) {
22657718be8SEnji Cooper 		ATF_REQUIRE(signal(SIGSEGV, sighandler) != SIG_ERR);
22757718be8SEnji Cooper 		ATF_CHECK(((int (*)(void))map)() == 1);
22857718be8SEnji Cooper 		_exit(0);
22957718be8SEnji Cooper 	}
23057718be8SEnji Cooper 
23157718be8SEnji Cooper 	(void)wait(&sta);
23257718be8SEnji Cooper 
23357718be8SEnji Cooper 	ATF_REQUIRE(munmap(map, page) == 0);
23457718be8SEnji Cooper 
23557718be8SEnji Cooper 	ATF_REQUIRE(WIFEXITED(sta) != 0);
23657718be8SEnji Cooper 
23757718be8SEnji Cooper 	switch (xp_support) {
23857718be8SEnji Cooper 	case PARTIAL_XP:
23957718be8SEnji Cooper 		/* Partial protection might fail; skip the test when it does */
24057718be8SEnji Cooper 		if (WEXITSTATUS(sta) != SIGSEGV) {
24157718be8SEnji Cooper 			atf_tc_skip("Host only supports "
24257718be8SEnji Cooper 			    "partial executable space protection");
24357718be8SEnji Cooper 		}
24457718be8SEnji Cooper 		break;
24557718be8SEnji Cooper 	case PERPAGE_XP: default:
24657718be8SEnji Cooper 		/* Per-page --x protection should not fail */
24757718be8SEnji Cooper 		ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
24857718be8SEnji Cooper 		break;
24957718be8SEnji Cooper 	}
25057718be8SEnji Cooper }
251078fc315SEnji Cooper #endif
25257718be8SEnji Cooper 
25357718be8SEnji Cooper ATF_TC(mprotect_pax);
ATF_TC_HEAD(mprotect_pax,tc)25457718be8SEnji Cooper ATF_TC_HEAD(mprotect_pax, tc)
25557718be8SEnji Cooper {
25657718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "PaX restrictions and mprotect(2)");
25757718be8SEnji Cooper 	atf_tc_set_md_var(tc, "require.user", "root");
25857718be8SEnji Cooper }
25957718be8SEnji Cooper 
ATF_TC_BODY(mprotect_pax,tc)26057718be8SEnji Cooper ATF_TC_BODY(mprotect_pax, tc)
26157718be8SEnji Cooper {
26257718be8SEnji Cooper 	const int prot[4] = { PROT_NONE, PROT_READ, PROT_WRITE };
26357718be8SEnji Cooper 	const char *str = NULL;
26457718be8SEnji Cooper 	void *map;
26557718be8SEnji Cooper 	size_t i;
26657718be8SEnji Cooper 	int rv;
26757718be8SEnji Cooper 
268640235e2SEnji Cooper 	if (!paxinit() || !paxset(1, 1))
26957718be8SEnji Cooper 		return;
27057718be8SEnji Cooper 
27157718be8SEnji Cooper 	/*
27257718be8SEnji Cooper 	 * As noted in the original PaX documentation [1],
27357718be8SEnji Cooper 	 * the following restrictions should apply:
27457718be8SEnji Cooper 	 *
27557718be8SEnji Cooper 	 *   (1) creating executable anonymous mappings
27657718be8SEnji Cooper 	 *
27757718be8SEnji Cooper 	 *   (2) creating executable/writable file mappings
27857718be8SEnji Cooper 	 *
27957718be8SEnji Cooper 	 *   (3) making a non-executable mapping executable
28057718be8SEnji Cooper 	 *
28157718be8SEnji Cooper 	 *   (4) making an executable/read-only file mapping
28257718be8SEnji Cooper 	 *       writable except for performing relocations
28357718be8SEnji Cooper 	 *       on an ET_DYN ELF file (non-PIC shared library)
28457718be8SEnji Cooper 	 *
28557718be8SEnji Cooper 	 *  The following will test only the case (3).
28657718be8SEnji Cooper 	 *
28757718be8SEnji Cooper 	 * [1] http://pax.grsecurity.net/docs/mprotect.txt
28857718be8SEnji Cooper 	 *
28957718be8SEnji Cooper 	 *     (Sun Apr 3 11:06:53 EEST 2011.)
29057718be8SEnji Cooper 	 */
29157718be8SEnji Cooper 	for (i = 0; i < __arraycount(prot); i++) {
29257718be8SEnji Cooper 
29357718be8SEnji Cooper 		map = mmap(NULL, page, prot[i], MAP_ANON, -1, 0);
29457718be8SEnji Cooper 
29557718be8SEnji Cooper 		if (map == MAP_FAILED)
29657718be8SEnji Cooper 			continue;
29757718be8SEnji Cooper 
29857718be8SEnji Cooper 		rv = mprotect(map, 1, prot[i] | PROT_EXEC);
29957718be8SEnji Cooper 
30057718be8SEnji Cooper 		(void)munmap(map, page);
30157718be8SEnji Cooper 
30257718be8SEnji Cooper 		if (rv == 0) {
30357718be8SEnji Cooper 			str = "non-executable mapping made executable";
30457718be8SEnji Cooper 			goto out;
30557718be8SEnji Cooper 		}
30657718be8SEnji Cooper 	}
30757718be8SEnji Cooper 
30857718be8SEnji Cooper out:
30957718be8SEnji Cooper 	if (pax_global != -1 && pax_enabled != -1)
31057718be8SEnji Cooper 		(void)paxset(pax_global, pax_enabled);
31157718be8SEnji Cooper 
31257718be8SEnji Cooper 	if (str != NULL)
31357718be8SEnji Cooper 		atf_tc_fail("%s", str);
31457718be8SEnji Cooper }
31557718be8SEnji Cooper 
31657718be8SEnji Cooper ATF_TC(mprotect_write);
ATF_TC_HEAD(mprotect_write,tc)31757718be8SEnji Cooper ATF_TC_HEAD(mprotect_write, tc)
31857718be8SEnji Cooper {
31957718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Test mprotect(2) write protections");
32057718be8SEnji Cooper }
32157718be8SEnji Cooper 
ATF_TC_BODY(mprotect_write,tc)32257718be8SEnji Cooper ATF_TC_BODY(mprotect_write, tc)
32357718be8SEnji Cooper {
32457718be8SEnji Cooper 	pid_t pid;
32557718be8SEnji Cooper 	void *map;
32657718be8SEnji Cooper 	int sta;
32757718be8SEnji Cooper 
32857718be8SEnji Cooper 	/*
32957718be8SEnji Cooper 	 * Map a page read/write, change the protection
33057718be8SEnji Cooper 	 * to read-only with mprotect(2), and try to write
33157718be8SEnji Cooper 	 * to the page. This should generate a SIGSEGV.
33257718be8SEnji Cooper 	 */
33357718be8SEnji Cooper 	map = mmap(NULL, page, PROT_WRITE|PROT_READ, MAP_ANON, -1, 0);
33457718be8SEnji Cooper 	ATF_REQUIRE(map != MAP_FAILED);
33557718be8SEnji Cooper 
33657718be8SEnji Cooper 	ATF_REQUIRE(strlcpy(map, "XXX", 3) == 3);
33757718be8SEnji Cooper 	ATF_REQUIRE(mprotect(map, page, PROT_READ) == 0);
33857718be8SEnji Cooper 
33957718be8SEnji Cooper 	pid = fork();
34057718be8SEnji Cooper 	ATF_REQUIRE(pid >= 0);
34157718be8SEnji Cooper 
34257718be8SEnji Cooper 	if (pid == 0) {
34357718be8SEnji Cooper 		ATF_REQUIRE(signal(SIGSEGV, sighandler) != SIG_ERR);
34457718be8SEnji Cooper 		ATF_REQUIRE(strlcpy(map, "XXX", 3) == 0);
34557718be8SEnji Cooper 	}
34657718be8SEnji Cooper 
34757718be8SEnji Cooper 	(void)wait(&sta);
34857718be8SEnji Cooper 
34957718be8SEnji Cooper 	ATF_REQUIRE(WIFEXITED(sta) != 0);
35057718be8SEnji Cooper 	ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
35157718be8SEnji Cooper 	ATF_REQUIRE(munmap(map, page) == 0);
35257718be8SEnji Cooper }
35357718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)35457718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
35557718be8SEnji Cooper {
35657718be8SEnji Cooper 	page = sysconf(_SC_PAGESIZE);
35757718be8SEnji Cooper 	ATF_REQUIRE(page >= 0);
35857718be8SEnji Cooper 
35957718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mprotect_access);
36057718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mprotect_err);
361078fc315SEnji Cooper #ifdef __NetBSD__
36257718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mprotect_exec);
363078fc315SEnji Cooper #endif
36457718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mprotect_pax);
36557718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, mprotect_write);
36657718be8SEnji Cooper 
36757718be8SEnji Cooper 	return atf_no_error();
36857718be8SEnji Cooper }
369