1/*- 2 * Copyright (c) 2009 Simon L. Nielsen <simon@FreeBSD.org>, 3 * Bjoern A. Zeeb <bz@FreeBSD.org> 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 */ 28 29#include <sys/param.h> 30#include <sys/mman.h> 31#include <sys/sysctl.h> 32#include <sys/types.h> 33 34#include <stdio.h> 35#include <err.h> 36 37const struct tests { 38 void *addr; 39 int ok[2]; /* Depending on security.bsd.map_at_zero {0, !=0}. */ 40} tests[] = { 41 { (void *)0, { 0, 1 } }, /* Test sysctl. */ 42 { (void *)1, { 0, 0 } }, 43 { (void *)(PAGE_SIZE - 1), { 0, 0 } }, 44 { (void *)PAGE_SIZE, { 1, 1 } }, 45 { (void *)-1, { 0, 0 } }, 46 { (void *)(-PAGE_SIZE), { 0, 0 } }, 47 { (void *)(-1 - PAGE_SIZE), { 0, 0 } }, 48 { (void *)(-1 - PAGE_SIZE - 1), { 0, 0 } }, 49 { (void *)(0x1000 * PAGE_SIZE), { 1, 1 } }, 50}; 51 52int 53main(void) 54{ 55 void *p; 56 size_t len; 57 int i, error, mib[3], map_at_zero; 58 59 error = 0; 60 61 /* Get the current sysctl value of security.bsd.map_at_zero. */ 62 len = sizeof(mib) / sizeof(*mib); 63 if (sysctlnametomib("security.bsd.map_at_zero", mib, &len) == -1) 64 err(1, "sysctlnametomib(security.bsd.map_at_zero)"); 65 66 len = sizeof(map_at_zero); 67 if (sysctl(mib, 3, &map_at_zero, &len, NULL, 0) == -1) 68 err(1, "sysctl(security.bsd.map_at_zero)"); 69 70 /* Normalize to 0 or 1 for array access. */ 71 map_at_zero = !!map_at_zero; 72 73 for (i=0; i < (sizeof(tests) / sizeof(*tests)); i++) { 74 p = mmap((void *)tests[i].addr, PAGE_SIZE, 75 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED, 76 -1, 0); 77 if (p == MAP_FAILED) { 78 if (tests[i].ok[map_at_zero] != 0) 79 error++; 80 warnx("%s: mmap(%p, ...) failed.", 81 (tests[i].ok[map_at_zero] == 0) ? "OK " : "ERR", 82 tests[i].addr); 83 } else { 84 if (tests[i].ok[map_at_zero] != 1) 85 error++; 86 warnx("%s: mmap(%p, ...) succeeded: p=%p", 87 (tests[i].ok[map_at_zero] == 1) ? "OK " : "ERR", 88 tests[i].addr, p); 89 } 90 } 91 92 if (error) 93 err(1, "---\nERROR: %d unexpected results.", error); 94 95 return (error != 0); 96} 97