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 <errno.h> 35 #include <stdio.h> 36 #include <string.h> 37 38 static const struct { 39 void *addr; 40 int ok[2]; /* Depending on security.bsd.map_at_zero {0, !=0}. */ 41 } tests[] = { 42 { (void *)0, { 0, 1 } }, /* Test sysctl. */ 43 { (void *)1, { 0, 0 } }, 44 { (void *)(PAGE_SIZE - 1), { 0, 0 } }, 45 { (void *)PAGE_SIZE, { 1, 1 } }, 46 { (void *)-1, { 0, 0 } }, 47 { (void *)(-PAGE_SIZE), { 0, 0 } }, 48 { (void *)(-1 - PAGE_SIZE), { 0, 0 } }, 49 { (void *)(-1 - PAGE_SIZE - 1), { 0, 0 } }, 50 { (void *)(0x1000 * PAGE_SIZE), { 1, 1 } }, 51 }; 52 53 #define MAP_AT_ZERO "security.bsd.map_at_zero" 54 55 int 56 main(void) 57 { 58 void *p; 59 size_t len; 60 int i, error, mib[3], map_at_zero; 61 62 error = 0; 63 64 /* Get the current sysctl value of security.bsd.map_at_zero. */ 65 len = sizeof(mib) / sizeof(*mib); 66 if (sysctlnametomib(MAP_AT_ZERO, mib, &len) == -1) { 67 printf("1..0 # SKIP: sysctlnametomib(\"%s\") failed: %s\n", 68 MAP_AT_ZERO, strerror(errno)); 69 return (0); 70 } 71 72 len = sizeof(map_at_zero); 73 if (sysctl(mib, 3, &map_at_zero, &len, NULL, 0) == -1) { 74 printf("1..0 # SKIP: sysctl for %s failed: %s\n", MAP_AT_ZERO, 75 strerror(errno)); 76 return (0); 77 } 78 79 /* Normalize to 0 or 1 for array access. */ 80 map_at_zero = !!map_at_zero; 81 82 printf("1..%zu\n", nitems(tests)); 83 for (i = 0; i < (int)nitems(tests); i++) { 84 p = mmap((void *)tests[i].addr, PAGE_SIZE, 85 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED, 86 -1, 0); 87 if (p == MAP_FAILED) { 88 if (tests[i].ok[map_at_zero] != 0) 89 error++; 90 printf("%sok %d # mmap(%p, ...) failed\n", 91 tests[i].ok[map_at_zero] == 0 ? "" : "not ", 92 i + 1, 93 tests[i].addr); 94 } else { 95 if (tests[i].ok[map_at_zero] != 1) 96 error++; 97 printf("%sok %d # mmap(%p, ...) succeeded: p=%p\n", 98 tests[i].ok[map_at_zero] == 1 ? "" : "not ", 99 i + 1, 100 tests[i].addr, p); 101 } 102 } 103 104 return (error != 0); 105 } 106