Home | History | Annotate | Line # | Download | only in sys
t_mprotect.c revision 1.5
      1 /* $NetBSD: t_mprotect.c,v 1.5 2017/03/24 08:18:27 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jukka Ruohonen.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 #include <sys/cdefs.h>
     32 __RCSID("$NetBSD: t_mprotect.c,v 1.5 2017/03/24 08:18:27 martin Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/mman.h>
     36 #include <sys/sysctl.h>
     37 #include <sys/wait.h>
     38 
     39 #include <errno.h>
     40 #include <fcntl.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 
     45 #include <atf-c.h>
     46 
     47 #include "../common/exec_prot.h"
     48 
     49 static long	page = 0;
     50 static char	path[] = "mmap";
     51 
     52 static void	sighandler(int);
     53 static bool	paxinit(void);
     54 
     55 static void
     56 sighandler(int signo)
     57 {
     58 	_exit(signo);
     59 }
     60 
     61 static bool
     62 paxinit(void)
     63 {
     64 	size_t len = sizeof(int);
     65 	int pax_global = -1;
     66 	int pax_enabled = -1;
     67 	int rv;
     68 
     69 	rv = sysctlbyname("security.pax.mprotect.global",
     70 	    &pax_global, &len, NULL, 0);
     71 
     72 	if (rv != 0)
     73 		return false;
     74 
     75 	rv = sysctlbyname("security.pax.mprotect.enabled",
     76 	    &pax_enabled, &len, NULL, 0);
     77 
     78 	if (rv != 0)
     79 		return false;
     80 
     81 	return pax_global == 1 && pax_enabled == 1;
     82 }
     83 
     84 ATF_TC_WITH_CLEANUP(mprotect_access);
     85 ATF_TC_HEAD(mprotect_access, tc)
     86 {
     87 	atf_tc_set_md_var(tc, "descr", "Test for EACCES from mprotect(2)");
     88 }
     89 
     90 ATF_TC_BODY(mprotect_access, tc)
     91 {
     92 	int prot[2] = { PROT_NONE, PROT_READ };
     93 	void *map;
     94 	size_t i;
     95 	int fd;
     96 
     97 	fd = open(path, O_RDONLY | O_CREAT);
     98 	ATF_REQUIRE(fd >= 0);
     99 
    100 	/*
    101 	 * The call should fail with EACCES if we try to mark
    102 	 * a PROT_NONE or PROT_READ file/section as PROT_WRITE.
    103 	 */
    104 	for (i = 0; i < __arraycount(prot); i++) {
    105 
    106 		map = mmap(NULL, page, prot[i], MAP_SHARED, fd, 0);
    107 
    108 		if (map == MAP_FAILED)
    109 			continue;
    110 
    111 		errno = 0;
    112 
    113 		ATF_REQUIRE(mprotect(map, page, PROT_WRITE) != 0);
    114 		ATF_REQUIRE(errno == EACCES);
    115 		ATF_REQUIRE(munmap(map, page) == 0);
    116 	}
    117 
    118 	ATF_REQUIRE(close(fd) == 0);
    119 }
    120 
    121 ATF_TC_CLEANUP(mprotect_access, tc)
    122 {
    123 	(void)unlink(path);
    124 }
    125 
    126 ATF_TC(mprotect_err);
    127 ATF_TC_HEAD(mprotect_err, tc)
    128 {
    129 	atf_tc_set_md_var(tc, "descr", "Test error conditions of mprotect(2)");
    130 }
    131 
    132 ATF_TC_BODY(mprotect_err, tc)
    133 {
    134 	errno = 0;
    135 
    136 	ATF_REQUIRE(mprotect((char *)-1, 1, PROT_READ) != 0);
    137 	ATF_REQUIRE(errno == EINVAL);
    138 }
    139 
    140 ATF_TC(mprotect_exec);
    141 ATF_TC_HEAD(mprotect_exec, tc)
    142 {
    143 	atf_tc_set_md_var(tc, "descr",
    144 	    "Test mprotect(2) executable space protections");
    145 }
    146 
    147 /*
    148  * Trivial function -- should fit into a page
    149  */
    150 ATF_TC_BODY(mprotect_exec, tc)
    151 {
    152 	pid_t pid;
    153 	void *map;
    154 	int sta, xp_support;
    155 
    156 	xp_support = exec_prot_support();
    157 
    158 	switch (xp_support) {
    159 	case NOTIMPL:
    160 		atf_tc_skip(
    161 		    "Execute protection callback check not implemented");
    162 		break;
    163 	case NO_XP:
    164 		atf_tc_skip(
    165 		    "Host does not support executable space protection");
    166 		break;
    167 	case PARTIAL_XP: case PERPAGE_XP: default:
    168 		break;
    169 	}
    170 
    171 
    172 	/*
    173 	 * Map a page read/write and copy a trivial assembly function inside.
    174 	 * We will then change the mapping rights:
    175 	 * - first by setting the execution right, and check that we can
    176 	 *   call the code found in the allocated page.
    177 	 * - second by removing the execution right. This should generate
    178 	 *   a SIGSEGV on architectures that can enforce --x permissions.
    179 	 */
    180 
    181 	map = mmap(NULL, page, PROT_WRITE|PROT_READ, MAP_ANON, -1, 0);
    182 	ATF_REQUIRE(map != MAP_FAILED);
    183 
    184 	memcpy(map, (void *)return_one,
    185 	    (uintptr_t)return_one_end - (uintptr_t)return_one);
    186 
    187 	/* give r-x rights then call code in page */
    188 	ATF_REQUIRE(mprotect(map, page, PROT_EXEC|PROT_READ) == 0);
    189 	ATF_REQUIRE(((int (*)(void))map)() == 1);
    190 
    191 	/* remove --x right */
    192 	ATF_REQUIRE(mprotect(map, page, PROT_READ) == 0);
    193 
    194 	pid = fork();
    195 	ATF_REQUIRE(pid >= 0);
    196 
    197 	if (pid == 0) {
    198 		ATF_REQUIRE(signal(SIGSEGV, sighandler) != SIG_ERR);
    199 		ATF_CHECK(((int (*)(void))map)() == 1);
    200 		_exit(0);
    201 	}
    202 
    203 	(void)wait(&sta);
    204 
    205 	ATF_REQUIRE(munmap(map, page) == 0);
    206 
    207 	ATF_REQUIRE(WIFEXITED(sta) != 0);
    208 
    209 	switch (xp_support) {
    210 	case PARTIAL_XP:
    211 		/* Partial protection might fail; skip the test when it does */
    212 		if (WEXITSTATUS(sta) != SIGSEGV) {
    213 			atf_tc_skip("Host only supports "
    214 			    "partial executable space protection");
    215 		}
    216 		break;
    217 	case PERPAGE_XP: default:
    218 		/* Per-page --x protection should not fail */
    219 		ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
    220 		break;
    221 	}
    222 }
    223 
    224 ATF_TC(mprotect_pax);
    225 ATF_TC_HEAD(mprotect_pax, tc)
    226 {
    227 	atf_tc_set_md_var(tc, "descr", "PaX restrictions and mprotect(2)");
    228 	atf_tc_set_md_var(tc, "require.user", "root");
    229 }
    230 
    231 ATF_TC_BODY(mprotect_pax, tc)
    232 {
    233 	const int prot[4] = { PROT_NONE, PROT_READ, PROT_WRITE };
    234 	const char *str = NULL;
    235 	void *map;
    236 	size_t i;
    237 	int rv;
    238 
    239 	if (!paxinit())
    240 		atf_tc_skip("PaX MPROTECT restrictions not enabled");
    241 
    242 	/*
    243 	 * As noted in the original PaX documentation [1],
    244 	 * the following restrictions should apply:
    245 	 *
    246 	 *   (1) creating executable anonymous mappings
    247 	 *
    248 	 *   (2) creating executable/writable file mappings
    249 	 *
    250 	 *   (3) making a non-executable mapping executable
    251 	 *
    252 	 *   (4) making an executable/read-only file mapping
    253 	 *       writable except for performing relocations
    254 	 *       on an ET_DYN ELF file (non-PIC shared library)
    255 	 *
    256 	 *  The following will test only the case (3).
    257 	 *
    258 	 * [1] http://pax.grsecurity.net/docs/mprotect.txt
    259 	 *
    260 	 *     (Sun Apr 3 11:06:53 EEST 2011.)
    261 	 */
    262 	for (i = 0; i < __arraycount(prot); i++) {
    263 
    264 		map = mmap(NULL, page, prot[i], MAP_ANON, -1, 0);
    265 
    266 		if (map == MAP_FAILED)
    267 			continue;
    268 
    269 		rv = mprotect(map, 1, prot[i] | PROT_EXEC);
    270 
    271 		(void)munmap(map, page);
    272 
    273 		if (rv == 0) {
    274 			str = "non-executable mapping made executable";
    275 			goto out;
    276 		}
    277 	}
    278 
    279 out:
    280 	if (str != NULL)
    281 		atf_tc_fail("%s", str);
    282 }
    283 
    284 ATF_TC(mprotect_write);
    285 ATF_TC_HEAD(mprotect_write, tc)
    286 {
    287 	atf_tc_set_md_var(tc, "descr", "Test mprotect(2) write protections");
    288 }
    289 
    290 ATF_TC_BODY(mprotect_write, tc)
    291 {
    292 	pid_t pid;
    293 	void *map;
    294 	int sta;
    295 
    296 	/*
    297 	 * Map a page read/write, change the protection
    298 	 * to read-only with mprotect(2), and try to write
    299 	 * to the page. This should generate a SIGSEGV.
    300 	 */
    301 	map = mmap(NULL, page, PROT_WRITE|PROT_READ, MAP_ANON, -1, 0);
    302 	ATF_REQUIRE(map != MAP_FAILED);
    303 
    304 	ATF_REQUIRE(strlcpy(map, "XXX", 3) == 3);
    305 	ATF_REQUIRE(mprotect(map, page, PROT_READ) == 0);
    306 
    307 	pid = fork();
    308 	ATF_REQUIRE(pid >= 0);
    309 
    310 	if (pid == 0) {
    311 		ATF_REQUIRE(signal(SIGSEGV, sighandler) != SIG_ERR);
    312 		ATF_REQUIRE(strlcpy(map, "XXX", 3) == 0);
    313 	}
    314 
    315 	(void)wait(&sta);
    316 
    317 	ATF_REQUIRE(WIFEXITED(sta) != 0);
    318 	ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
    319 	ATF_REQUIRE(munmap(map, page) == 0);
    320 }
    321 
    322 ATF_TP_ADD_TCS(tp)
    323 {
    324 	page = sysconf(_SC_PAGESIZE);
    325 	ATF_REQUIRE(page >= 0);
    326 
    327 	ATF_TP_ADD_TC(tp, mprotect_access);
    328 	ATF_TP_ADD_TC(tp, mprotect_err);
    329 	ATF_TP_ADD_TC(tp, mprotect_exec);
    330 	ATF_TP_ADD_TC(tp, mprotect_pax);
    331 	ATF_TP_ADD_TC(tp, mprotect_write);
    332 
    333 	return atf_no_error();
    334 }
    335