xref: /freebsd/tests/sys/kern/unix_dgram.c (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2022 Gleb Smirnoff <glebius@FreeBSD.org>
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/time.h>
29 #include <sys/event.h>
30 #include <sys/ioctl.h>
31 #include <sys/select.h>
32 #include <sys/socket.h>
33 #include <sys/sysctl.h>
34 #include <sys/un.h>
35 #include <aio.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <stdlib.h>
40 
41 #include <atf-c.h>
42 
43 static struct itimerval itv = {
44 	.it_interval = { 0, 0 },
45 	.it_value = { 1, 0 },	/* one second */
46 };
47 static sig_atomic_t timer_done = 0;
48 static void
49 sigalarm(int sig __unused)
50 {
51 
52 	timer_done = 1;
53 }
54 
55 static struct sigaction sigact = {
56 	.sa_handler = sigalarm,
57 };
58 
59 static struct sockaddr_un sun = {
60 	.sun_family = AF_LOCAL,
61 	.sun_len = sizeof(sun),
62 	.sun_path = "unix_dgram_listener",
63 };
64 
65 /*
66  * Fill socket to a state when next send(len) would fail.
67  *
68  * Note that every datagram is prepended with sender address,
69  * size of struct sockaddr.
70  */
71 static void
72 fill(int fd, void *buf, ssize_t len)
73 {
74 	unsigned long recvspace;
75 	size_t llen = sizeof(unsigned long);
76 	ssize_t sent;
77 
78 	ATF_REQUIRE(sysctlbyname("net.local.dgram.recvspace", &recvspace,
79 	    &llen, NULL, 0) == 0);
80 	for (sent = 0;
81 	    sent + len + sizeof(struct sockaddr) < recvspace;
82 	    sent += len + sizeof(struct sockaddr))
83 		ATF_REQUIRE(send(fd, buf, len, 0) == len);
84 }
85 
86 ATF_TC_WITHOUT_HEAD(basic);
87 ATF_TC_BODY(basic, tc)
88 {
89 	struct msghdr msg;
90 	struct iovec iov[1];
91 	unsigned long maxdgram;
92 	size_t llen = sizeof(unsigned long);
93 	int fd[2];
94 	char *buf;
95 
96 	/* Allocate and initialize:
97 	 * - fd[0] to send, fd[1] to receive
98 	 * - buf[maxdgram] for data
99 	 */
100 	ATF_REQUIRE(sysctlbyname("net.local.dgram.maxdgram", &maxdgram,
101 	    &llen, NULL, 0) == 0);
102 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
103 	buf = malloc(maxdgram + 1);
104 	ATF_REQUIRE(buf);
105 	msg = (struct msghdr ){
106 		.msg_iov = iov,
107 		.msg_iovlen = 1,
108 	};
109 	iov[0] = (struct iovec ){
110 		.iov_base = buf,
111 	};
112 
113 	/* Fail to send > maxdgram. */
114 	ATF_REQUIRE(send(fd[0], buf, maxdgram + 1, 0) == -1);
115 	ATF_REQUIRE(errno == EMSGSIZE);
116 
117 	/* Send maxdgram. */
118 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
119 
120 	/* Exercise MSG_PEEK, full and truncated.. */
121 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_PEEK) == (ssize_t)maxdgram);
122 	iov[0].iov_len = 42;
123 	ATF_REQUIRE(recvmsg(fd[1], &msg, MSG_PEEK) == 42);
124 	ATF_REQUIRE(msg.msg_flags == (MSG_PEEK | MSG_TRUNC));
125 
126 	/* Receive maxdgram. */
127 	iov[0].iov_len = maxdgram;
128 	ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram);
129 	ATF_REQUIRE(msg.msg_flags == 0);
130 
131 	/* Receive truncated message. */
132 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == (ssize_t)maxdgram);
133 	iov[0].iov_len = maxdgram / 2;
134 	ATF_REQUIRE(recvmsg(fd[1], &msg, 0) == (ssize_t)maxdgram / 2);
135 	ATF_REQUIRE(msg.msg_flags == MSG_TRUNC);
136 
137 	/* Empty: block. */
138 	ATF_REQUIRE(sigaction(SIGALRM, &sigact, NULL) == 0);
139 	ATF_REQUIRE(timer_done == 0);
140 	ATF_REQUIRE(setitimer(ITIMER_REAL, &itv, NULL) == 0);
141 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
142 	ATF_REQUIRE(errno == EINTR);
143 	ATF_REQUIRE(timer_done == 1);
144 
145 	/* Don't block with MSG_DONTWAIT. */
146 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, MSG_DONTWAIT) == -1);
147 	ATF_REQUIRE(errno == EAGAIN);
148 
149 	/* Don't block with O_NONBLOCK. */
150 	ATF_REQUIRE(fcntl(fd[1], F_SETFL, O_NONBLOCK) != -1);
151 	ATF_REQUIRE(recv(fd[1], buf, maxdgram, 0) == -1);
152 	ATF_REQUIRE(errno == EAGAIN);
153 
154 	/* Fail with ENOBUFS on full socket. */
155 	fill(fd[0], buf, maxdgram);
156 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
157 	ATF_REQUIRE(errno == ENOBUFS);
158 
159 	/* Fail with EAGAIN with O_NONBLOCK set. */
160 	ATF_REQUIRE(fcntl(fd[0], F_SETFL, O_NONBLOCK) != -1);
161 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
162 	ATF_REQUIRE(errno == EAGAIN);
163 
164 	/* Remote side closed -> ECONNRESET. */
165 	close(fd[1]);
166 	ATF_REQUIRE(send(fd[0], buf, maxdgram, 0) == -1);
167 	ATF_REQUIRE(errno == ECONNRESET);
168 }
169 
170 ATF_TC_WITHOUT_HEAD(one2many);
171 ATF_TC_BODY(one2many, tc)
172 {
173 	int one, many[3], two;
174 #define	BUFSIZE	1024
175 	char buf[BUFSIZE], goodboy[BUFSIZE], flooder[BUFSIZE], notconn[BUFSIZE];
176 
177 	/* Establish one to many connection. */
178 	ATF_REQUIRE((one = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
179 	ATF_REQUIRE(bind(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
180 	/* listen(2) shall fail. */
181 	ATF_REQUIRE(listen(one, -1) != 0);
182 	for (int i = 0; i < 3; i++) {
183 		ATF_REQUIRE((many[i] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
184 		ATF_REQUIRE(connect(many[i], (struct sockaddr *)&sun,
185 		    sizeof(sun)) == 0);
186 	}
187 
188 	/* accept() on UNIX/DGRAM is invalid. */
189 	ATF_REQUIRE(accept(one, NULL, NULL) == -1);
190 	ATF_REQUIRE(errno == EINVAL);
191 
192 	/*
193 	 * Connecting a bound socket to self: a strange, useless, but
194 	 * historically existing edge case that is not explicitly described
195 	 * in SuS, neither is forbidden there. Works on FreeBSD and Linux.
196 	 */
197 	ATF_REQUIRE(connect(one, (struct sockaddr *)&sun, sizeof(sun)) == 0);
198 	ATF_REQUIRE(send(one, buf, 42, 0) == 42);
199 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == 42);
200 
201 	/*
202 	 * Interaction between concurrent senders. New feature in FreeBSD 14.
203 	 *
204 	 * One sender can not fill the receive side.  Other senders can
205 	 * continue operation.  Senders who don't fill their buffers are
206 	 * prioritized over flooders.  Connected senders are prioritized over
207 	 * unconnected.
208 	 *
209 	 * Disconnecting a sender that has queued data optionally preserves
210 	 * the data.  Allow the data to migrate to peers buffer only if the
211 	 * latter is empty.  Otherwise discard it, to prevent against
212 	 * connect-fill-close attack.
213 	 */
214 #define	FLOODER	13	/* for connected flooder on many[0] */
215 #define	GOODBOY	42	/* for a good boy on many[1] */
216 #define	NOTCONN	66	/* for sendto(2) via two */
217 	goodboy[0] = GOODBOY;
218 	flooder[0] = FLOODER;
219 	notconn[0] = NOTCONN;
220 
221 	/* Connected priority over sendto(2). */
222 	ATF_REQUIRE((two = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
223 	ATF_REQUIRE(sendto(two, notconn, BUFSIZE, 0, (struct sockaddr *)&sun,
224 	    sizeof(sun)) == BUFSIZE);
225 	ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
226 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
227 	ATF_REQUIRE(buf[0] == GOODBOY);	/* message from good boy comes first */
228 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
229 	ATF_REQUIRE(buf[0] == NOTCONN);	/* only then message from sendto(2) */
230 
231 	/* Casual sender priority over a flooder. */
232 	fill(many[0], flooder, sizeof(flooder));
233 	ATF_REQUIRE(send(many[0], flooder, BUFSIZE, 0) == -1);
234 	ATF_REQUIRE(errno == ENOBUFS);
235 	ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
236 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
237 	ATF_REQUIRE(buf[0] == GOODBOY);	/* message from good boy comes first */
238 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
239 	ATF_REQUIRE(buf[0] == FLOODER);	/* only then message from flooder */
240 
241 	/* Once seen, a message can't be deprioritized by any other message. */
242 	ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == sizeof(buf));
243 	ATF_REQUIRE(buf[0] == FLOODER); /* message from the flooder seen */
244 	ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
245 	ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == sizeof(buf));
246 	ATF_REQUIRE(buf[0] == FLOODER); /* should be the same message */
247 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
248 	ATF_REQUIRE(buf[0] == FLOODER); /* now we read it out... */
249 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
250 	ATF_REQUIRE(buf[0] == GOODBOY); /* ... and next one is the good boy */
251 
252 	/* Disconnect in presence of data from not connected. */
253 	ATF_REQUIRE(sendto(two, notconn, BUFSIZE, 0, (struct sockaddr *)&sun,
254 	    sizeof(sun)) == BUFSIZE);
255 	close(many[0]);
256 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
257 	ATF_REQUIRE(buf[0] == NOTCONN);	/* message from sendto() */
258 	ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_DONTWAIT) == -1);
259 	ATF_REQUIRE(errno == EAGAIN);	/* data from many[0] discarded */
260 
261 	/* Disconnect in absence of data from not connected. */
262 	ATF_REQUIRE(send(many[1], goodboy, BUFSIZE, 0) == BUFSIZE);
263 	close(many[1]);
264 	ATF_REQUIRE(recv(one, buf, sizeof(buf), 0) == sizeof(buf));
265 	ATF_REQUIRE(buf[0] == GOODBOY);	/* message from many[1] preserved */
266 
267 	/* Check that nothing leaks on close(2). */
268 	ATF_REQUIRE(send(many[2], buf, 42, 0) == 42);
269 	ATF_REQUIRE(send(many[2], buf, 42, 0) == 42);
270 	ATF_REQUIRE(recv(one, buf, sizeof(buf), MSG_PEEK) == 42);
271 	ATF_REQUIRE(sendto(two, notconn, 42, 0, (struct sockaddr *)&sun,
272 	    sizeof(sun)) == 42);
273 	close(one);
274 }
275 
276 /*
277  * Check that various mechanism report socket as readable and having
278  * 42 bytes of data.
279  */
280 static void
281 test42(int fd)
282 {
283 
284 	/* ioctl(FIONREAD) */
285 	int data;
286 
287 	ATF_REQUIRE(ioctl(fd, FIONREAD, &data) != -1);
288 	ATF_REQUIRE(data == 42);
289 
290 	/* select(2) */
291 	fd_set rfds;
292 
293 	FD_ZERO(&rfds);
294 	FD_SET(fd, &rfds);
295 	ATF_REQUIRE(select(fd + 1, &rfds, NULL, NULL, NULL) == 1);
296 	ATF_REQUIRE(FD_ISSET(fd, &rfds));
297 
298 	/* kevent(2) */
299 	struct kevent ev;
300 	int kq;
301 
302 	ATF_REQUIRE((kq = kqueue()) != -1);
303 	EV_SET(&ev, fd, EVFILT_READ, EV_ADD, NOTE_LOWAT, 41, NULL);
304 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == 0);
305 	ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
306 	ATF_REQUIRE(ev.filter == EVFILT_READ);
307 	ATF_REQUIRE(ev.data == 42);
308 
309 	/* aio(4) */
310 	char buf[50];
311 	struct aiocb aio = {
312 		.aio_nbytes = 50,
313 		.aio_fildes = fd,
314 		.aio_buf = buf,
315 	}, *aiop;
316 
317 	ATF_REQUIRE(aio_read(&aio) == 0);
318 	ATF_REQUIRE(aio_waitcomplete(&aiop, NULL) == 42);
319 	ATF_REQUIRE(aiop == &aio);
320 }
321 
322 /*
323  * Send data and control in connected & unconnected mode and check that
324  * various event mechanisms see the data, but don't count control bytes.
325  */
326 ATF_TC_WITHOUT_HEAD(event);
327 ATF_TC_BODY(event, tc)
328 {
329 	int fd[2];
330 	char buf[50];
331 	struct iovec iov = {
332 		.iov_base = buf,
333 		.iov_len = 42,
334 	};
335 	struct cmsghdr cmsg = {
336 		.cmsg_len = CMSG_LEN(0),
337 		.cmsg_level = SOL_SOCKET,
338 		.cmsg_type = SCM_TIMESTAMP,
339 	};
340 	struct msghdr msghdr = {
341 		.msg_iov = &iov,
342 		.msg_iovlen = 1,
343 		.msg_control = &cmsg,
344 		.msg_controllen = CMSG_LEN(0),
345 	};
346 
347 	/* Connected socket */
348 	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd) != -1);
349 	ATF_REQUIRE(sendmsg(fd[0], &msghdr, 0) == 42);
350 	test42(fd[1]);
351 	close(fd[0]);
352 	close(fd[1]);
353 
354 	/* Not-connected send */
355 	ATF_REQUIRE((fd[0] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
356 	ATF_REQUIRE((fd[1] = socket(PF_UNIX, SOCK_DGRAM, 0)) > 0);
357 	ATF_REQUIRE(bind(fd[0], (struct sockaddr *)&sun, sizeof(sun)) == 0);
358 	ATF_REQUIRE(sendto(fd[1], buf, 42, 0, (struct sockaddr *)&sun,
359 	    sizeof(sun)) == 42);
360 	test42(fd[0]);
361 }
362 
363 ATF_TP_ADD_TCS(tp)
364 {
365 
366 	ATF_TP_ADD_TC(tp, basic);
367 	ATF_TP_ADD_TC(tp, one2many);
368 	ATF_TP_ADD_TC(tp, event);
369 
370 	return (atf_no_error());
371 }
372