1 /*- 2 * Copyright (c) 2018 John Baldwin <jhb@FreeBSD.org> 3 * All rights reserved. 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 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/capsicum.h> 31 #include <sys/filio.h> 32 #include <sys/socket.h> 33 #include <sys/wait.h> 34 #include <netinet/in.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <unistd.h> 38 39 #include <atf-c.h> 40 41 /* 42 * A variant of ATF_REQUIRE that is suitable for use in child 43 * processes. This only works if the parent process is tripped up by 44 * the early exit and fails some requirement itself. 45 */ 46 #define CHILD_REQUIRE(exp) do { \ 47 if (!(exp)) \ 48 child_fail_require(__FILE__, __LINE__, \ 49 #exp " not met"); \ 50 } while (0) 51 52 static __dead2 void 53 child_fail_require(const char *file, int line, const char *str) 54 { 55 char buf[128]; 56 57 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str); 58 write(2, buf, strlen(buf)); 59 _exit(32); 60 } 61 62 /* 63 * Exercise the edge case of a custom ioctl list being copied from a 64 * listen socket to an accepted socket. 65 */ 66 ATF_TC_WITHOUT_HEAD(cap_ioctls__listen_copy); 67 ATF_TC_BODY(cap_ioctls__listen_copy, tc) 68 { 69 struct sockaddr_in sin; 70 cap_rights_t rights; 71 u_long cmds[] = { FIONREAD }; 72 socklen_t len; 73 pid_t pid; 74 char dummy; 75 int s[2], status; 76 77 s[0] = socket(AF_INET, SOCK_STREAM, 0); 78 ATF_REQUIRE(s[0] > 0); 79 80 /* Bind to an arbitrary unused port. */ 81 memset(&sin, 0, sizeof(sin)); 82 sin.sin_len = sizeof(sin); 83 sin.sin_family = AF_INET; 84 sin.sin_port = 0; 85 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 86 ATF_REQUIRE(bind(s[0], (struct sockaddr *)&sin, sizeof(sin)) == 0); 87 88 CHILD_REQUIRE(listen(s[0], 1) == 0); 89 90 len = sizeof(sin); 91 ATF_REQUIRE(getsockname(s[0], (struct sockaddr *)&sin, &len) == 0); 92 ATF_REQUIRE(len == sizeof(sin)); 93 94 cap_rights_init(&rights, CAP_ACCEPT, CAP_IOCTL); 95 ATF_REQUIRE(cap_rights_limit(s[0], &rights) == 0); 96 ATF_REQUIRE(cap_ioctls_limit(s[0], cmds, nitems(cmds)) == 0); 97 98 pid = fork(); 99 if (pid == 0) { 100 s[1] = accept(s[0], NULL, NULL); 101 CHILD_REQUIRE(s[1] > 0); 102 103 /* Close both sockets during exit(). */ 104 exit(0); 105 } 106 107 ATF_REQUIRE(pid > 0); 108 109 ATF_REQUIRE(close(s[0]) == 0); 110 s[1] = socket(AF_INET, SOCK_STREAM, 0); 111 ATF_REQUIRE(s[1] > 0); 112 ATF_REQUIRE(connect(s[1], (struct sockaddr *)&sin, sizeof(sin)) == 0); 113 ATF_REQUIRE(read(s[1], &dummy, sizeof(dummy)) == 0); 114 ATF_REQUIRE(close(s[1]) == 0); 115 116 ATF_REQUIRE(wait(&status) == pid); 117 ATF_REQUIRE(WIFEXITED(status)); 118 ATF_REQUIRE(WEXITSTATUS(status) == 0); 119 } 120 121 ATF_TP_ADD_TCS(tp) 122 { 123 124 ATF_TP_ADD_TC(tp, cap_ioctls__listen_copy); 125 126 return (atf_no_error()); 127 } 128