xref: /freebsd/tests/sys/kern/socket_msg_trunc.c (revision 783d3ff6)
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 /*
157  * Exercise the case where ktrace was used to dump a truncated buffer.
158  */
159 ATF_TC_WITHOUT_HEAD(recvmsg_trunc_ktrace_uio);
160 ATF_TC_BODY(recvmsg_trunc_ktrace_uio, tc)
161 {
162 	struct ktr_header ktr;
163 	struct msghdr msg;
164 	struct iovec iov;
165 	const char *tracepath;
166 	char buf[128];
167 	ssize_t nbytes;
168 	int error, fd, sd[2];
169 
170 	tracepath = "ktrace";
171 
172 	error = socketpair(AF_UNIX, SOCK_DGRAM, 0, sd);
173 	ATF_REQUIRE(error == 0);
174 
175 	memset(buf, 0, sizeof(buf));
176 	nbytes = send(sd[0], buf, sizeof(buf), 0);
177 	ATF_REQUIRE_MSG(nbytes >= 0, "send failed: %s", strerror(errno));
178 	ATF_REQUIRE((size_t)nbytes == sizeof(buf));
179 
180 	fd = open(tracepath, O_RDWR | O_CREAT | O_TRUNC, 0644);
181 	ATF_REQUIRE_MSG(fd >= 0, "open failed: %s", strerror(errno));
182 	error = ktrace(tracepath, KTROP_SET, KTRFAC_GENIO, getpid());
183 	ATF_REQUIRE_MSG(error == 0,
184 	    "ktrace(SET) failed: %s", strerror(errno));
185 
186 	iov.iov_base = buf;
187 	iov.iov_len = sizeof(buf) - 1; /* truncate */
188 	memset(&msg, 0, sizeof(msg));
189 	msg.msg_iov = &iov;
190 	msg.msg_iovlen = 1;
191 	nbytes = recvmsg(sd[1], &msg, MSG_TRUNC);
192 	ATF_REQUIRE_MSG(nbytes >= 0, "recvmsg failed: %s", strerror(errno));
193 	ATF_REQUIRE((size_t)nbytes == sizeof(buf));
194 	ATF_REQUIRE((msg.msg_flags & MSG_TRUNC) != 0);
195 
196 	error = ktrace(tracepath, KTROP_CLEARFILE, 0, getpid());
197 	ATF_REQUIRE_MSG(error == 0,
198 	    "ktrace(CLEARFILE) failed: %s", strerror(errno));
199 
200 	nbytes = read(fd, &ktr, sizeof(ktr));
201 	ATF_REQUIRE_MSG(nbytes >= 0, "read failed: %s", strerror(errno));
202 	ATF_REQUIRE((size_t)nbytes == sizeof(ktr));
203 	ATF_REQUIRE((ktr.ktr_type & ~KTR_TYPE) == KTR_GENIO);
204 
205 	ATF_REQUIRE(close(fd) == 0);
206 	ATF_REQUIRE(close(sd[0]) == 0);
207 	ATF_REQUIRE(close(sd[1]) == 0);
208 }
209 
210 ATF_TP_ADD_TCS(tp)
211 {
212 	ATF_TP_ADD_TC(tp, recv_trunc_afinet_udp);
213 	ATF_TP_ADD_TC(tp, recv_trunc_afinet6_udp);
214 	ATF_TP_ADD_TC(tp, recv_trunc_afunix_dgram);
215 	ATF_TP_ADD_TC(tp, recvmsg_trunc_ktrace_uio);
216 
217 	return (atf_no_error());
218 }
219