2 * Copyright (c) 2009 Simon L. Nielsen <simon@FreeBSD.org>,
3 * Bjoern A. Zeeb <bz@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
31 #include <sys/sysctl.h>
42 int ok[2]; /* Depending on security.bsd.map_at_zero {0, !=0}. */
43 } map_at_zero_tests[] = {
44 { (void *)0, { 0, 1 } }, /* Test sysctl. */
45 { (void *)1, { 0, 0 } },
46 { (void *)(PAGE_SIZE - 1), { 0, 0 } },
47 { (void *)PAGE_SIZE, { 1, 1 } },
48 { (void *)-1, { 0, 0 } },
49 { (void *)(-PAGE_SIZE), { 0, 0 } },
50 { (void *)(-1 - PAGE_SIZE), { 0, 0 } },
51 { (void *)(-1 - PAGE_SIZE - 1), { 0, 0 } },
52 { (void *)(0x1000 * PAGE_SIZE), { 1, 1 } },
55 #define MAP_AT_ZERO "security.bsd.map_at_zero"
57 ATF_TC_WITHOUT_HEAD(mmap__map_at_zero);
58 ATF_TC_BODY(mmap__map_at_zero, tc)
65 len = sizeof(map_at_zero);
66 if (sysctlbyname(MAP_AT_ZERO, &map_at_zero, &len, NULL, 0) == -1) {
67 atf_tc_skip("sysctl for %s failed: %s\n", MAP_AT_ZERO,
72 /* Normalize to 0 or 1 for array access. */
73 map_at_zero = !!map_at_zero;
75 for (i = 0; i < nitems(map_at_zero_tests); i++) {
76 p = mmap((void *)map_at_zero_tests[i].addr, PAGE_SIZE,
77 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED,
79 if (p == MAP_FAILED) {
80 ATF_CHECK_MSG(map_at_zero_tests[i].ok[map_at_zero] == 0,
81 "mmap(%p, ...) failed", map_at_zero_tests[i].addr);
83 ATF_CHECK_MSG(map_at_zero_tests[i].ok[map_at_zero] == 1,
84 "mmap(%p, ...) succeeded: p=%p\n",
85 map_at_zero_tests[i].addr, p);
91 checked_mmap(int prot, int flags, int fd, int error, const char *msg)
95 p = mmap(NULL, getpagesize(), prot, flags, fd, 0);
96 if (p == MAP_FAILED) {
98 ATF_CHECK_MSG(0, "%s failed with errno %d", msg,
101 ATF_CHECK_EQ_MSG(error, errno,
102 "%s failed with wrong errno %d (expected %d)", msg,
105 ATF_CHECK_MSG(error == 0, "%s succeeded", msg);
106 munmap(p, getpagesize());
110 ATF_TC_WITHOUT_HEAD(mmap__bad_arguments);
111 ATF_TC_BODY(mmap__bad_arguments, tc)
115 ATF_REQUIRE((fd = shm_open(SHM_ANON, O_RDWR, 0644)) >= 0);
116 ATF_REQUIRE(ftruncate(fd, getpagesize()) == 0);
118 /* These should work. */
119 checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON, -1, 0,
121 checked_mmap(PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0,
122 "simple shm fd shared");
123 checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0,
124 "simple shm fd private");
128 * These tests do not fail without r271635 and followup fixes.
129 * Those changes will not be merged to stable/10 since they
130 * are potentially disruptive.
133 /* Extra PROT flags. */
134 checked_mmap(PROT_READ | PROT_WRITE | 0x100000, MAP_ANON, -1, EINVAL,
135 "MAP_ANON with extra PROT flags");
136 checked_mmap(0xffff, MAP_SHARED, fd, EINVAL,
137 "shm fd with garbage PROT");
139 /* Undefined flag. */
140 checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_RESERVED0080, -1,
141 EINVAL, "Undefined flag");
143 /* Both MAP_SHARED and MAP_PRIVATE */
144 checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE |
145 MAP_SHARED, -1, EINVAL, "MAP_ANON with both SHARED and PRIVATE");
146 checked_mmap(PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_SHARED, fd,
147 EINVAL, "shm fd with both SHARED and PRIVATE");
149 /* At least one of MAP_SHARED or MAP_PRIVATE without ANON */
150 checked_mmap(PROT_READ | PROT_WRITE, 0, fd, EINVAL,
151 "shm fd without sharing flag");
154 /* MAP_ANON with either sharing flag (impacts fork). */
155 checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0,
157 checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0,
160 /* MAP_ANON should require an fd of -1. */
161 checked_mmap(PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, EINVAL,
162 "MAP_ANON with fd != -1");
168 ATF_TP_ADD_TC(tp, mmap__map_at_zero);
169 ATF_TP_ADD_TC(tp, mmap__bad_arguments);
171 return (atf_no_error());