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[4096]; 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 rc = sendto(cs, buf, sz, 0, sa, sa->sa_len); 56 ATF_REQUIRE_MSG(rc != -1, "sendto failed: %s", strerror(errno)); 57 ATF_REQUIRE((size_t)rc == sz); 58 59 rc = recv(ss, NULL, 0, MSG_PEEK | MSG_TRUNC); 60 ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); 61 ATF_REQUIRE((size_t)rc == sz); 62 63 rc = recv(ss, tbuf, sizeof(tbuf), MSG_PEEK | MSG_TRUNC); 64 ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); 65 ATF_REQUIRE((size_t)rc == sz); 66 67 rc = recv(ss, tbuf, sizeof(tbuf), MSG_TRUNC); 68 ATF_REQUIRE_MSG(rc >= 0, "recv failed: %s", strerror(errno)); 69 ATF_REQUIRE((size_t)rc == sz); 70 } 71 72 ATF_REQUIRE(close(cs) == 0); 73 ATF_REQUIRE(close(ss) == 0); 74 } 75 76 ATF_TC_WITHOUT_HEAD(recv_trunc_afinet_udp); 77 ATF_TC_BODY(recv_trunc_afinet_udp, tc) 78 { 79 struct sockaddr_in sin; 80 struct sockaddr *sa; 81 int ss, cs, rc; 82 83 ss = socket(PF_INET, SOCK_DGRAM, 0); 84 ATF_REQUIRE(ss >= 0); 85 86 memset(&sin, 0, sizeof(sin)); 87 sin.sin_family = AF_INET; 88 sin.sin_len = sizeof(sin); 89 sin.sin_port = htons(6666); 90 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 91 sa = (struct sockaddr *)&sin; 92 rc = bind(ss, sa, sa->sa_len); 93 ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); 94 95 cs = socket(PF_INET, SOCK_DGRAM, 0); 96 ATF_REQUIRE(cs >= 0); 97 98 size_t sizes[] = {80, 255, 256, 1024, 4096, 9000}; 99 check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); 100 } 101 102 ATF_TC_WITHOUT_HEAD(recv_trunc_afinet6_udp); 103 ATF_TC_BODY(recv_trunc_afinet6_udp, tc) 104 { 105 struct sockaddr_in6 sin6; 106 struct sockaddr *sa; 107 int cs, ss, rc; 108 109 ss = socket(PF_INET6, SOCK_DGRAM, 0); 110 ATF_REQUIRE(ss >= 0); 111 112 memset(&sin6, 0, sizeof(sin6)); 113 sin6.sin6_family = AF_INET6; 114 sin6.sin6_len = sizeof(sin6); 115 sin6.sin6_port = htons(6666); 116 const struct in6_addr in6loopback = IN6ADDR_LOOPBACK_INIT; 117 sin6.sin6_addr = in6loopback; 118 sa = (struct sockaddr *)&sin6; 119 rc = bind(ss, sa, sa->sa_len); 120 ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); 121 122 cs = socket(PF_INET6, SOCK_DGRAM, 0); 123 ATF_REQUIRE(cs >= 0); 124 125 size_t sizes[] = {80, 255, 256, 1024, 4096, 9000}; 126 check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); 127 } 128 129 ATF_TC_WITHOUT_HEAD(recv_trunc_afunix_dgram); 130 ATF_TC_BODY(recv_trunc_afunix_dgram, tc) 131 { 132 struct sockaddr_un sun; 133 struct sockaddr *sa; 134 int ss, cs, rc; 135 136 ss = socket(PF_UNIX, SOCK_DGRAM, 0); 137 ATF_REQUIRE(ss >= 0); 138 139 bzero(&sun, sizeof(sun)); 140 sun.sun_family = AF_UNIX; 141 strlcpy(sun.sun_path, "test_check_recvmsg_socket", sizeof(sun.sun_path)); 142 sun.sun_len = sizeof(sun); 143 sa = (struct sockaddr *)&sun; 144 rc = bind(ss, sa, sa->sa_len); 145 ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); 146 147 cs = socket(PF_UNIX, SOCK_DGRAM, 0); 148 ATF_REQUIRE(cs >= 0); 149 150 size_t sizes[] = {80, 255, 256, 1024, 2000}; 151 check_recvmsg(cs, ss, sa, sizes, nitems(sizes)); 152 } 153 154 ATF_TC_WITHOUT_HEAD(recv_trunc_afunix_seqpacket); 155 ATF_TC_BODY(recv_trunc_afunix_seqpacket, tc) 156 { 157 struct sockaddr_un sun; 158 struct sockaddr *sa; 159 int ss, nss, cs, rc; 160 161 ss = socket(PF_UNIX, SOCK_SEQPACKET, 0); 162 ATF_REQUIRE(ss >= 0); 163 164 bzero(&sun, sizeof(sun)); 165 sun.sun_family = AF_UNIX; 166 strlcpy(sun.sun_path, "test_check_recvmsg_socket", sizeof(sun.sun_path)); 167 sun.sun_len = sizeof(sun); 168 sa = (struct sockaddr *)&sun; 169 rc = bind(ss, sa, sa->sa_len); 170 ATF_REQUIRE_MSG(rc == 0, "bind failed: %s", strerror(errno)); 171 rc = listen(ss, 1); 172 ATF_REQUIRE_MSG(rc == 0, "listen failed: %s", strerror(errno)); 173 174 cs = socket(PF_UNIX, SOCK_SEQPACKET, 0); 175 ATF_REQUIRE(cs >= 0); 176 rc = connect(cs, sa, sa->sa_len); 177 ATF_REQUIRE_MSG(rc == 0, "connect failed: %s", strerror(errno)); 178 nss = accept(ss, NULL, NULL); 179 ATF_REQUIRE(nss >= 0); 180 181 size_t sizes[] = {80, 255, 256, 1024, 2000}; 182 check_recvmsg(cs, nss, sa, sizes, nitems(sizes)); 183 184 ATF_REQUIRE(close(ss) == 0); 185 } 186 187 /* 188 * Exercise the case where ktrace was used to dump a truncated buffer. 189 */ 190 ATF_TC_WITHOUT_HEAD(recvmsg_trunc_ktrace_uio); 191 ATF_TC_BODY(recvmsg_trunc_ktrace_uio, tc) 192 { 193 struct ktr_header ktr; 194 struct msghdr msg; 195 struct iovec iov; 196 const char *tracepath; 197 char buf[128]; 198 ssize_t nbytes; 199 int error, fd, sd[2]; 200 201 tracepath = "ktrace"; 202 203 error = socketpair(AF_UNIX, SOCK_DGRAM, 0, sd); 204 ATF_REQUIRE(error == 0); 205 206 memset(buf, 0, sizeof(buf)); 207 nbytes = send(sd[0], buf, sizeof(buf), 0); 208 ATF_REQUIRE_MSG(nbytes >= 0, "send failed: %s", strerror(errno)); 209 ATF_REQUIRE((size_t)nbytes == sizeof(buf)); 210 211 fd = open(tracepath, O_RDWR | O_CREAT | O_TRUNC, 0644); 212 ATF_REQUIRE_MSG(fd >= 0, "open failed: %s", strerror(errno)); 213 error = ktrace(tracepath, KTROP_SET, KTRFAC_GENIO, getpid()); 214 ATF_REQUIRE_MSG(error == 0, 215 "ktrace(SET) failed: %s", strerror(errno)); 216 217 iov.iov_base = buf; 218 iov.iov_len = sizeof(buf) - 1; /* truncate */ 219 memset(&msg, 0, sizeof(msg)); 220 msg.msg_iov = &iov; 221 msg.msg_iovlen = 1; 222 nbytes = recvmsg(sd[1], &msg, MSG_TRUNC); 223 ATF_REQUIRE_MSG(nbytes >= 0, "recvmsg failed: %s", strerror(errno)); 224 ATF_REQUIRE((size_t)nbytes == sizeof(buf)); 225 ATF_REQUIRE((msg.msg_flags & MSG_TRUNC) != 0); 226 227 error = ktrace(tracepath, KTROP_CLEARFILE, 0, getpid()); 228 ATF_REQUIRE_MSG(error == 0, 229 "ktrace(CLEARFILE) failed: %s", strerror(errno)); 230 231 nbytes = read(fd, &ktr, sizeof(ktr)); 232 ATF_REQUIRE_MSG(nbytes >= 0, "read failed: %s", strerror(errno)); 233 ATF_REQUIRE((size_t)nbytes == sizeof(ktr)); 234 ATF_REQUIRE((ktr.ktr_type & ~KTR_TYPE) == KTR_GENIO); 235 236 ATF_REQUIRE(close(fd) == 0); 237 ATF_REQUIRE(close(sd[0]) == 0); 238 ATF_REQUIRE(close(sd[1]) == 0); 239 } 240 241 ATF_TP_ADD_TCS(tp) 242 { 243 ATF_TP_ADD_TC(tp, recv_trunc_afinet_udp); 244 ATF_TP_ADD_TC(tp, recv_trunc_afinet6_udp); 245 ATF_TP_ADD_TC(tp, recv_trunc_afunix_dgram); 246 ATF_TP_ADD_TC(tp, recv_trunc_afunix_seqpacket); 247 ATF_TP_ADD_TC(tp, recvmsg_trunc_ktrace_uio); 248 249 return (atf_no_error()); 250 } 251