1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2022 Alexander V. Chernikov 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/param.h> 29 #include <sys/errno.h> 30 #include <sys/ktrace.h> 31 #include <sys/socket.h> 32 #include <sys/un.h> 33 34 #include <netinet/in.h> 35 36 #include <fcntl.h> 37 #include <poll.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 41 #include <atf-c.h> 42 43 static void 44 check_recvmsg(int cs, int ss, struct sockaddr *sa, const size_t sizes[], 45 size_t nsizes) 46 { 47 char buf[9000]; 48 49 memset(buf, 0xFF, sizeof(buf)); 50 for (size_t i = 0; i < nsizes; i++) { 51 ssize_t rc; 52 size_t sz = sizes[i]; 53 char tbuf[1]; 54 55 ATF_REQUIRE(sz <= sizeof(buf)); 56 57 rc = sendto(cs, buf, sz, 0, sa, sa->sa_len); 58 ATF_REQUIRE_MSG(rc != -1, "sendto failed: %s", strerror(errno)); 59 ATF_REQUIRE((size_t)rc == sz); 60 61 rc = recv(ss, NULL, 0, MSG_PEEK | MSG_TRUNC); 62 ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); 63 ATF_REQUIRE((size_t)rc == sz); 64 65 rc = recv(ss, tbuf, sizeof(tbuf), MSG_PEEK | MSG_TRUNC); 66 ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); 67 ATF_REQUIRE((size_t)rc == sz); 68 69 rc = recv(ss, tbuf, sizeof(tbuf), MSG_TRUNC); 70 ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); 71 ATF_REQUIRE((size_t)rc == sz); 72 } 73 74 ATF_REQUIRE(close(cs) == 0); 75 ATF_REQUIRE(close(ss) == 0); 76 } 77 78 ATF_TC_WITHOUT_HEAD(recv_trunc_afinet_udp); 79 ATF_TC_BODY(recv_trunc_afinet_udp, tc) 80 { 81 struct sockaddr_in sin; 82 struct sockaddr *sa; 83 int ss, cs, rc; 84 85 ss = socket(PF_INET, SOCK_DGRAM, 0); 86 ATF_REQUIRE(ss >= 0); 87 88 memset(&sin, 0, sizeof(sin)); 89 sin.sin_family = AF_INET; 90 sin.sin_len = sizeof(sin); 91 sin.sin_port = htons(6666); 92 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 93 sa = (struct sockaddr *)&sin; 94 rc = bind(ss, sa, sa->sa_len); 95 ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); 96 97 cs = socket(PF_INET, SOCK_DGRAM, 0); 98 ATF_REQUIRE(cs >= 0); 99 100 size_t sizes[] = {80, 255, 256, 1024, 4096, 9000}; 101 check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); 102 } 103 104 ATF_TC_WITHOUT_HEAD(recv_trunc_afinet6_udp); 105 ATF_TC_BODY(recv_trunc_afinet6_udp, tc) 106 { 107 struct sockaddr_in6 sin6; 108 struct sockaddr *sa; 109 int cs, ss, rc; 110 111 ss = socket(PF_INET6, SOCK_DGRAM, 0); 112 ATF_REQUIRE(ss >= 0); 113 114 memset(&sin6, 0, sizeof(sin6)); 115 sin6.sin6_family = AF_INET6; 116 sin6.sin6_len = sizeof(sin6); 117 sin6.sin6_port = htons(6666); 118 const struct in6_addr in6loopback = IN6ADDR_LOOPBACK_INIT; 119 sin6.sin6_addr = in6loopback; 120 sa = (struct sockaddr *)&sin6; 121 rc = bind(ss, sa, sa->sa_len); 122 ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); 123 124 cs = socket(PF_INET6, SOCK_DGRAM, 0); 125 ATF_REQUIRE(cs >= 0); 126 127 size_t sizes[] = {80, 255, 256, 1024, 4096, 9000}; 128 check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); 129 } 130 131 ATF_TC_WITHOUT_HEAD(recv_trunc_afunix_dgram); 132 ATF_TC_BODY(recv_trunc_afunix_dgram, tc) 133 { 134 struct sockaddr_un sun; 135 struct sockaddr *sa; 136 int ss, cs, rc; 137 138 ss = socket(PF_UNIX, SOCK_DGRAM, 0); 139 ATF_REQUIRE(ss >= 0); 140 141 bzero(&sun, sizeof(sun)); 142 sun.sun_family = AF_UNIX; 143 strlcpy(sun.sun_path, "test_check_recvmsg_socket", sizeof(sun.sun_path)); 144 sun.sun_len = sizeof(sun); 145 sa = (struct sockaddr *)&sun; 146 rc = bind(ss, sa, sa->sa_len); 147 ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); 148 149 cs = socket(PF_UNIX, SOCK_DGRAM, 0); 150 ATF_REQUIRE(cs >= 0); 151 152 size_t sizes[] = {80, 255, 256, 1024, 2000}; 153 check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); 154 } 155 156 ATF_TC_WITHOUT_HEAD(recv_trunc_afunix_seqpacket); 157 ATF_TC_BODY(recv_trunc_afunix_seqpacket, tc) 158 { 159 struct sockaddr_un sun; 160 struct sockaddr *sa; 161 int ss, nss, cs, rc; 162 163 ss = socket(PF_UNIX, SOCK_SEQPACKET, 0); 164 ATF_REQUIRE(ss >= 0); 165 166 bzero(&sun, sizeof(sun)); 167 sun.sun_family = AF_UNIX; 168 strlcpy(sun.sun_path, "test_check_recvmsg_socket", sizeof(sun.sun_path)); 169 sun.sun_len = sizeof(sun); 170 sa = (struct sockaddr *)&sun; 171 rc = bind(ss, sa, sa->sa_len); 172 ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); 173 rc = listen(ss, 1); 174 ATF_REQUIRE_MSG(rc == 0, "listen failed: %s", strerror(errno)); 175 176 cs = socket(PF_UNIX, SOCK_SEQPACKET, 0); 177 ATF_REQUIRE(cs >= 0); 178 rc = connect(cs, sa, sa->sa_len); 179 ATF_REQUIRE_MSG(rc == 0, "connect failed: %s", strerror(errno)); 180 nss = accept(ss, NULL, NULL); 181 ATF_REQUIRE(nss >= 0); 182 183 size_t sizes[] = {80, 255, 256, 1024, 2000}; 184 check_recvmsg(cs, nss, sa, sizes, nitems(sizes)); 185 186 ATF_REQUIRE(close(ss) == 0); 187 } 188 189 /* 190 * Exercise the case where ktrace was used to dump a truncated buffer. 191 */ 192 ATF_TC_WITHOUT_HEAD(recvmsg_trunc_ktrace_uio); 193 ATF_TC_BODY(recvmsg_trunc_ktrace_uio, tc) 194 { 195 struct ktr_header ktr; 196 struct msghdr msg; 197 struct iovec iov; 198 const char *tracepath; 199 char buf[128]; 200 ssize_t nbytes; 201 int error, fd, sd[2]; 202 203 tracepath = "ktrace"; 204 205 error = socketpair(AF_UNIX, SOCK_DGRAM, 0, sd); 206 ATF_REQUIRE(error == 0); 207 208 memset(buf, 0, sizeof(buf)); 209 nbytes = send(sd[0], buf, sizeof(buf), 0); 210 ATF_REQUIRE_MSG(nbytes >= 0, "send failed: %s", strerror(errno)); 211 ATF_REQUIRE((size_t)nbytes == sizeof(buf)); 212 213 fd = open(tracepath, O_RDWR | O_CREAT | O_TRUNC, 0644); 214 ATF_REQUIRE_MSG(fd >= 0, "open failed: %s", strerror(errno)); 215 error = ktrace(tracepath, KTROP_SET, KTRFAC_GENIO, getpid()); 216 ATF_REQUIRE_MSG(error == 0, 217 "ktrace(SET) failed: %s", strerror(errno)); 218 219 iov.iov_base = buf; 220 iov.iov_len = sizeof(buf) - 1; /* truncate */ 221 memset(&msg, 0, sizeof(msg)); 222 msg.msg_iov = &iov; 223 msg.msg_iovlen = 1; 224 nbytes = recvmsg(sd[1], &msg, MSG_TRUNC); 225 ATF_REQUIRE_MSG(nbytes >= 0, "recvmsg failed: %s", strerror(errno)); 226 ATF_REQUIRE((size_t)nbytes == sizeof(buf)); 227 ATF_REQUIRE((msg.msg_flags & MSG_TRUNC) != 0); 228 229 error = ktrace(tracepath, KTROP_CLEARFILE, 0, getpid()); 230 ATF_REQUIRE_MSG(error == 0, 231 "ktrace(CLEARFILE) failed: %s", strerror(errno)); 232 233 nbytes = read(fd, &ktr, sizeof(ktr)); 234 ATF_REQUIRE_MSG(nbytes >= 0, "read failed: %s", strerror(errno)); 235 ATF_REQUIRE((size_t)nbytes == sizeof(ktr)); 236 ATF_REQUIRE((ktr.ktr_type & ~KTR_TYPE) == KTR_GENIO); 237 238 ATF_REQUIRE(close(fd) == 0); 239 ATF_REQUIRE(close(sd[0]) == 0); 240 ATF_REQUIRE(close(sd[1]) == 0); 241 } 242 243 ATF_TP_ADD_TCS(tp) 244 { 245 ATF_TP_ADD_TC(tp, recv_trunc_afinet_udp); 246 ATF_TP_ADD_TC(tp, recv_trunc_afinet6_udp); 247 ATF_TP_ADD_TC(tp, recv_trunc_afunix_dgram); 248 ATF_TP_ADD_TC(tp, recv_trunc_afunix_seqpacket); 249 ATF_TP_ADD_TC(tp, recvmsg_trunc_ktrace_uio); 250 251 return (atf_no_error()); 252 } 253