1 /* $NetBSD: t_mmap.c,v 1.7 2012/06/14 17:47:58 bouyer Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
33 * Copyright (c)2004 YAMAMOTO Takashi,
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 #include <sys/cdefs.h>
58 __RCSID("$NetBSD: t_mmap.c,v 1.7 2012/06/14 17:47:58 bouyer Exp $");
60 #include <sys/param.h>
62 #include <sys/socket.h>
63 #include <sys/sysctl.h>
76 #include <machine/disklabel.h>
80 #include <sys/disklabel.h>
86 static char path[] = "mmap";
87 static void map_check(void *, int);
88 static void map_sighandler(int);
89 static void testloan(void *, void *, char, int);
91 #define BUFSIZE (32 * 1024) /* enough size to trigger sosend_loan */
94 map_check(void *map, int flag)
98 ATF_REQUIRE(map == MAP_FAILED);
102 ATF_REQUIRE(map != MAP_FAILED);
103 ATF_REQUIRE(munmap(map, page) == 0);
107 testloan(void *vp, void *vp2, char pat, int docheck)
110 char backup[BUFSIZE];
119 (void)memcpy(backup, vp, BUFSIZE);
121 if (socketpair(AF_LOCAL, SOCK_STREAM, PF_UNSPEC, fds) != 0)
122 atf_tc_fail("socketpair() failed");
126 if (setsockopt(fds[1], SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) != 0)
127 atf_tc_fail("setsockopt() failed, SO_RCVBUF");
131 if (setsockopt(fds[0], SOL_SOCKET, SO_SNDBUF, &val, sizeof(val)) != 0)
132 atf_tc_fail("setsockopt() failed, SO_SNDBUF");
134 if (fcntl(fds[0], F_SETFL, O_NONBLOCK) != 0)
135 atf_tc_fail("fcntl() failed");
137 nwritten = write(fds[0], (char *)vp + page, BUFSIZE - page);
140 atf_tc_fail("write() failed");
143 (void)memset(vp2, pat, BUFSIZE);
145 nread = read(fds[1], buf + page, BUFSIZE - page);
148 atf_tc_fail("read() failed");
150 if (nread != nwritten)
151 atf_tc_fail("too short read");
153 if (docheck != 0 && memcmp(backup, buf + page, nread) != 0)
154 atf_tc_fail("data mismatch");
156 ATF_REQUIRE(close(fds[0]) == 0);
157 ATF_REQUIRE(close(fds[1]) == 0);
161 map_sighandler(int signo)
168 ATF_TC_HEAD(mmap_block, tc)
170 atf_tc_set_md_var(tc, "descr", "Test mmap(2) with a block device");
171 atf_tc_set_md_var(tc, "require.user", "root");
174 ATF_TC_BODY(mmap_block, tc)
176 static const int mib[] = { CTL_HW, HW_DISKNAMES };
177 static const unsigned int miblen = __arraycount(mib);
178 char *map, *dk, *drives, dev[PATH_MAX];
182 atf_tc_skip("The test case causes a panic (PR kern/38889, kern/46592)");
184 ATF_REQUIRE(sysctl(mib, miblen, NULL, &len, NULL, 0) == 0);
185 drives = malloc(len);
186 ATF_REQUIRE(drives != NULL);
187 ATF_REQUIRE(sysctl(mib, miblen, drives, &len, NULL, 0) == 0);
188 for (dk = strtok(drives, " "); dk != NULL; dk = strtok(NULL, " ")) {
189 sprintf(dev, _PATH_DEV "%s%c", dk, 'a'+RAW_PART);
190 fprintf(stderr, "trying: %s\n", dev);
192 if ((fd = open(dev, O_RDONLY)) >= 0) {
193 (void)fprintf(stderr, "using %s\n", dev);
200 atf_tc_skip("failed to find suitable block device");
202 map = mmap(NULL, 4096, PROT_READ, MAP_FILE, fd, 0);
203 ATF_REQUIRE(map != MAP_FAILED);
205 (void)fprintf(stderr, "first byte %x\n", *map);
206 ATF_REQUIRE(close(fd) == 0);
207 (void)fprintf(stderr, "first byte %x\n", *map);
209 ATF_REQUIRE(munmap(map, 4096) == 0);
214 ATF_TC_HEAD(mmap_err, tc)
216 atf_tc_set_md_var(tc, "descr", "Test error conditions of mmap(2)");
219 ATF_TC_BODY(mmap_err, tc)
221 size_t addr = SIZE_MAX;
225 map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, -1, 0);
227 ATF_REQUIRE(map == MAP_FAILED);
228 ATF_REQUIRE(errno == EBADF);
231 map = mmap(&addr, page, PROT_READ, MAP_FIXED|MAP_PRIVATE, -1, 0);
233 ATF_REQUIRE(map == MAP_FAILED);
234 ATF_REQUIRE(errno == EINVAL);
237 map = mmap(NULL, page, PROT_READ, MAP_ANON|MAP_PRIVATE, INT_MAX, 0);
239 ATF_REQUIRE(map == MAP_FAILED);
240 ATF_REQUIRE(errno == EINVAL);
243 ATF_TC_WITH_CLEANUP(mmap_loan);
244 ATF_TC_HEAD(mmap_loan, tc)
246 atf_tc_set_md_var(tc, "descr", "Test uvm page loanout with mmap(2)");
249 ATF_TC_BODY(mmap_loan, tc)
255 fd = open(path, O_RDWR | O_CREAT, 0600);
256 ATF_REQUIRE(fd >= 0);
258 (void)memset(buf, 'x', sizeof(buf));
259 (void)write(fd, buf, sizeof(buf));
261 vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
262 MAP_FILE | MAP_PRIVATE, fd, 0);
264 ATF_REQUIRE(vp != MAP_FAILED);
268 testloan(vp, vp2, 'A', 0);
269 testloan(vp, vp2, 'B', 1);
271 ATF_REQUIRE(munmap(vp, BUFSIZE) == 0);
273 vp = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
274 MAP_FILE | MAP_SHARED, fd, 0);
276 vp2 = mmap(NULL, BUFSIZE, PROT_READ | PROT_WRITE,
277 MAP_FILE | MAP_SHARED, fd, 0);
279 ATF_REQUIRE(vp != MAP_FAILED);
280 ATF_REQUIRE(vp2 != MAP_FAILED);
282 testloan(vp, vp2, 'E', 1);
284 ATF_REQUIRE(munmap(vp, BUFSIZE) == 0);
285 ATF_REQUIRE(munmap(vp2, BUFSIZE) == 0);
288 ATF_TC_CLEANUP(mmap_loan, tc)
293 ATF_TC_WITH_CLEANUP(mmap_prot_1);
294 ATF_TC_HEAD(mmap_prot_1, tc)
296 atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #1");
299 ATF_TC_BODY(mmap_prot_1, tc)
305 * Open a file write-only and try to
306 * map it read-only. This should fail.
308 fd = open(path, O_WRONLY | O_CREAT, 0700);
313 ATF_REQUIRE(write(fd, "XXX", 3) == 3);
315 map = mmap(NULL, 3, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0);
318 map = mmap(NULL, 3, PROT_WRITE, MAP_FILE|MAP_PRIVATE, fd, 0);
321 ATF_REQUIRE(close(fd) == 0);
324 ATF_TC_CLEANUP(mmap_prot_1, tc)
330 ATF_TC_HEAD(mmap_prot_2, tc)
332 atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #2");
335 ATF_TC_BODY(mmap_prot_2, tc)
343 * Make a PROT_NONE mapping and try to access it.
344 * If we catch a SIGSEGV, all works as expected.
346 map = mmap(NULL, page, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
347 ATF_REQUIRE(map != MAP_FAILED);
350 ATF_REQUIRE(pid >= 0);
353 ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
354 ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0);
359 ATF_REQUIRE(WIFEXITED(sta) != 0);
360 ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
361 ATF_REQUIRE(munmap(map, page) == 0);
364 ATF_TC_WITH_CLEANUP(mmap_prot_3);
365 ATF_TC_HEAD(mmap_prot_3, tc)
367 atf_tc_set_md_var(tc, "descr", "Test mmap(2) protections, #3");
370 ATF_TC_BODY(mmap_prot_3, tc)
378 * Open a file, change the permissions
379 * to read-only, and try to map it as
380 * PROT_NONE. This should succeed, but
381 * the access should generate SIGSEGV.
383 fd = open(path, O_RDWR | O_CREAT, 0700);
388 ATF_REQUIRE(write(fd, "XXX", 3) == 3);
389 ATF_REQUIRE(close(fd) == 0);
390 ATF_REQUIRE(chmod(path, 0444) == 0);
392 fd = open(path, O_RDONLY);
393 ATF_REQUIRE(fd != -1);
395 map = mmap(NULL, 3, PROT_NONE, MAP_FILE | MAP_SHARED, fd, 0);
396 ATF_REQUIRE(map != MAP_FAILED);
400 ATF_REQUIRE(pid >= 0);
403 ATF_REQUIRE(signal(SIGSEGV, map_sighandler) != SIG_ERR);
404 ATF_REQUIRE(strlcpy(buf, map, sizeof(buf)) != 0);
409 ATF_REQUIRE(WIFEXITED(sta) != 0);
410 ATF_REQUIRE(WEXITSTATUS(sta) == SIGSEGV);
411 ATF_REQUIRE(munmap(map, 3) == 0);
414 ATF_TC_CLEANUP(mmap_prot_3, tc)
419 ATF_TC_WITH_CLEANUP(mmap_truncate);
420 ATF_TC_HEAD(mmap_truncate, tc)
422 atf_tc_set_md_var(tc, "descr", "Test mmap(2) and ftruncate(2)");
425 ATF_TC_BODY(mmap_truncate, tc)
431 fd = open(path, O_RDWR | O_CREAT, 0700);
437 * See that ftruncate(2) works
438 * while the file is mapped.
440 ATF_REQUIRE(ftruncate(fd, page) == 0);
442 map = mmap(NULL, page, PROT_READ | PROT_WRITE, MAP_FILE|MAP_PRIVATE,
444 ATF_REQUIRE(map != MAP_FAILED);
446 for (i = 0; i < page; i++)
449 ATF_REQUIRE(ftruncate(fd, 0) == 0);
450 ATF_REQUIRE(ftruncate(fd, page / 8) == 0);
451 ATF_REQUIRE(ftruncate(fd, page / 4) == 0);
452 ATF_REQUIRE(ftruncate(fd, page / 2) == 0);
453 ATF_REQUIRE(ftruncate(fd, page / 12) == 0);
454 ATF_REQUIRE(ftruncate(fd, page / 64) == 0);
456 ATF_REQUIRE(close(fd) == 0);
459 ATF_TC_CLEANUP(mmap_truncate, tc)
465 ATF_TC_HEAD(mmap_va0, tc)
467 atf_tc_set_md_var(tc, "descr", "Test mmap(2) and vm.user_va0_disable");
470 ATF_TC_BODY(mmap_va0, tc)
472 int flags = MAP_ANON | MAP_FIXED | MAP_PRIVATE;
473 size_t len = sizeof(int);
478 * Make an anonymous fixed mapping at zero address. If the address
479 * is restricted as noted in security(7), the syscall should fail.
482 if (sysctlbyname("security.bsd.map_at_zero", &val, &len, NULL, 0) != 0)
483 atf_tc_fail("failed to read security.bsd.map_at_zero");
484 val = !val; /* 1 == enable map at zero */
487 if (sysctlbyname("vm.user_va0_disable", &val, &len, NULL, 0) != 0)
488 atf_tc_fail("failed to read vm.user_va0_disable");
491 map = mmap(NULL, page, PROT_EXEC, flags, -1, 0);
494 map = mmap(NULL, page, PROT_READ, flags, -1, 0);
497 map = mmap(NULL, page, PROT_WRITE, flags, -1, 0);
500 map = mmap(NULL, page, PROT_READ|PROT_WRITE, flags, -1, 0);
503 map = mmap(NULL, page, PROT_EXEC|PROT_READ|PROT_WRITE, flags, -1, 0);
509 page = sysconf(_SC_PAGESIZE);
510 ATF_REQUIRE(page >= 0);
513 ATF_TP_ADD_TC(tp, mmap_block);
515 ATF_TP_ADD_TC(tp, mmap_err);
516 ATF_TP_ADD_TC(tp, mmap_loan);
517 ATF_TP_ADD_TC(tp, mmap_prot_1);
518 ATF_TP_ADD_TC(tp, mmap_prot_2);
519 ATF_TP_ADD_TC(tp, mmap_prot_3);
520 ATF_TP_ADD_TC(tp, mmap_truncate);
521 ATF_TP_ADD_TC(tp, mmap_va0);
523 return atf_no_error();