xref: /freebsd/tests/sys/kern/unix_passfd_test.c (revision 15c433351f54e7cd5bec8d36c8e89e6a7fa55b26)
1 /*-
2  * Copyright (c) 2005 Robert N. M. Watson
3  * Copyright (c) 2015 Mark Johnston
4  * All rights reserved.
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  * $FreeBSD$
28  */
29 
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/sysctl.h>
34 #include <sys/un.h>
35 
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include <atf-c.h>
45 
46 /*
47  * UNIX domain sockets allow file descriptors to be passed via "ancillary
48  * data", or control messages.  This regression test is intended to exercise
49  * this facility, both performing some basic tests that it operates, and also
50  * causing some kernel edge cases to execute, such as garbage collection when
51  * there are cyclic file descriptor references.  Right now we test only with
52  * stream sockets, but ideally we'd also test with datagram sockets.
53  */
54 
55 static void
56 domainsocketpair(int *fdp)
57 {
58 
59 	ATF_REQUIRE_MSG(socketpair(PF_UNIX, SOCK_STREAM, 0, fdp) != -1,
60 	    "socketpair(PF_UNIX, SOCK_STREAM) failed: %s", strerror(errno));
61 }
62 
63 static void
64 closesocketpair(int *fdp)
65 {
66 
67 	close(fdp[0]);
68 	close(fdp[1]);
69 }
70 
71 static void
72 devnull(int *fdp)
73 {
74 	int fd;
75 
76 	fd = open("/dev/null", O_RDONLY);
77 	ATF_REQUIRE_MSG(fd != -1, "open failed: %s", strerror(errno));
78 	*fdp = fd;
79 }
80 
81 static void
82 tempfile(int *fdp)
83 {
84 	char path[PATH_MAX];
85 	int fd;
86 
87 	snprintf(path, PATH_MAX, "%s/unix_passfd.XXXXXXXXXXXXXXX",
88 	    getenv("TMPDIR") == NULL ? "/tmp" : getenv("TMPDIR"));
89 	fd = mkstemp(path);
90 	ATF_REQUIRE_MSG(fd != -1, "mkstemp(%s) failed", path);
91 	(void)unlink(path);
92 	*fdp = fd;
93 }
94 
95 static void
96 dofstat(int fd, struct stat *sb)
97 {
98 
99 	ATF_REQUIRE_MSG(fstat(fd, sb) == 0,
100 	    "fstat failed: %s", strerror(errno));
101 }
102 
103 static void
104 samefile(struct stat *sb1, struct stat *sb2)
105 {
106 
107 	ATF_REQUIRE_MSG(sb1->st_dev == sb2->st_dev, "different device");
108 	ATF_REQUIRE_MSG(sb1->st_ino == sb2->st_ino, "different inode");
109 }
110 
111 static void
112 sendfd_payload(int sockfd, int send_fd, void *payload, size_t paylen)
113 {
114 	struct iovec iovec;
115 	char message[CMSG_SPACE(sizeof(int))];
116 	struct cmsghdr *cmsghdr;
117 	struct msghdr msghdr;
118 	ssize_t len;
119 
120 	bzero(&msghdr, sizeof(msghdr));
121 	bzero(&message, sizeof(message));
122 
123 	msghdr.msg_control = message;
124 	msghdr.msg_controllen = sizeof(message);
125 
126 	iovec.iov_base = payload;
127 	iovec.iov_len = paylen;
128 
129 	msghdr.msg_iov = &iovec;
130 	msghdr.msg_iovlen = 1;
131 
132 	cmsghdr = (struct cmsghdr *)(void*)message;
133 	cmsghdr->cmsg_len = CMSG_LEN(sizeof(int));
134 	cmsghdr->cmsg_level = SOL_SOCKET;
135 	cmsghdr->cmsg_type = SCM_RIGHTS;
136 	memcpy(CMSG_DATA(cmsghdr), &send_fd, sizeof(int));
137 
138 	len = sendmsg(sockfd, &msghdr, 0);
139 	ATF_REQUIRE_MSG(len != -1, "sendmsg failed: %s", strerror(errno));
140 	ATF_REQUIRE_MSG((size_t)len == paylen,
141 	    "sendmsg: %zd messages sent; expected: %zu; %s", len, paylen,
142 	    strerror(errno));
143 }
144 
145 static void
146 sendfd(int sockfd, int send_fd)
147 {
148 	char ch = 0;
149 
150 	return (sendfd_payload(sockfd, send_fd, &ch, sizeof(ch)));
151 }
152 
153 static void
154 recvfd_payload(int sockfd, int *recv_fd, void *buf, size_t buflen)
155 {
156 	struct cmsghdr *cmsghdr;
157 	char message[CMSG_SPACE(SOCKCREDSIZE(CMGROUP_MAX)) + sizeof(int)];
158 	struct msghdr msghdr;
159 	struct iovec iovec;
160 	ssize_t len;
161 
162 	bzero(&msghdr, sizeof(msghdr));
163 
164 	msghdr.msg_control = message;
165 	msghdr.msg_controllen = sizeof(message);
166 
167 	iovec.iov_base = buf;
168 	iovec.iov_len = buflen;
169 
170 	msghdr.msg_iov = &iovec;
171 	msghdr.msg_iovlen = 1;
172 
173 	len = recvmsg(sockfd, &msghdr, 0);
174 	ATF_REQUIRE_MSG(len != -1, "recvmsg failed: %s", strerror(errno));
175 	ATF_REQUIRE_MSG((size_t)len == buflen,
176 	    "recvmsg: %zd bytes received; expected %zd", len, buflen);
177 
178 	cmsghdr = CMSG_FIRSTHDR(&msghdr);
179 	ATF_REQUIRE_MSG(cmsghdr != NULL,
180 	    "recvmsg: did not receive control message");
181 	*recv_fd = -1;
182 	for (; cmsghdr != NULL; cmsghdr = CMSG_NXTHDR(&msghdr, cmsghdr)) {
183 		if (cmsghdr->cmsg_level == SOL_SOCKET &&
184 		    cmsghdr->cmsg_type == SCM_RIGHTS &&
185 		    cmsghdr->cmsg_len == CMSG_LEN(sizeof(int))) {
186 			memcpy(recv_fd, CMSG_DATA(cmsghdr), sizeof(int));
187 			ATF_REQUIRE(*recv_fd != -1);
188 		}
189 	}
190 	ATF_REQUIRE_MSG(*recv_fd != -1,
191 	    "recvmsg: did not receive single-fd message");
192 }
193 
194 static void
195 recvfd(int sockfd, int *recv_fd)
196 {
197 	char ch = 0;
198 
199 	return (recvfd_payload(sockfd, recv_fd, &ch, sizeof(ch)));
200 }
201 
202 /*
203  * Put a temporary file into a UNIX domain socket, then take it out and make
204  * sure it's the same file.  First time around, don't close the reference
205  * after sending.
206  */
207 ATF_TC_WITHOUT_HEAD(simple_send_fd);
208 ATF_TC_BODY(simple_send_fd, tc)
209 {
210 	struct stat getfd_stat, putfd_stat;
211 	int fd[2], getfd, putfd;
212 
213 	domainsocketpair(fd);
214 	tempfile(&putfd);
215 	dofstat(putfd, &putfd_stat);
216 	sendfd(fd[0], putfd);
217 	recvfd(fd[1], &getfd);
218 	dofstat(getfd, &getfd_stat);
219 	samefile(&putfd_stat, &getfd_stat);
220 	close(putfd);
221 	close(getfd);
222 	closesocketpair(fd);
223 }
224 
225 /*
226  * Same as simple_send_fd, only close the file reference after sending, so that
227  * the only reference is the descriptor in the UNIX domain socket buffer.
228  */
229 ATF_TC_WITHOUT_HEAD(send_and_close);
230 ATF_TC_BODY(send_and_close, tc)
231 {
232 	struct stat getfd_stat, putfd_stat;
233 	int fd[2], getfd, putfd;
234 
235 	domainsocketpair(fd);
236 	tempfile(&putfd);
237 	dofstat(putfd, &putfd_stat);
238 	sendfd(fd[0], putfd);
239 	close(putfd);
240 	recvfd(fd[1], &getfd);
241 	dofstat(getfd, &getfd_stat);
242 	samefile(&putfd_stat, &getfd_stat);
243 	close(getfd);
244 	closesocketpair(fd);
245 }
246 
247 /*
248  * Put a temporary file into a UNIX domain socket, then close both endpoints
249  * causing garbage collection to kick off.
250  */
251 ATF_TC_WITHOUT_HEAD(send_and_cancel);
252 ATF_TC_BODY(send_and_cancel, tc)
253 {
254 	int fd[2], putfd;
255 
256 	domainsocketpair(fd);
257 	tempfile(&putfd);
258 	sendfd(fd[0], putfd);
259 	close(putfd);
260 	closesocketpair(fd);
261 }
262 
263 /*
264  * Send two files.  Then receive them.  Make sure they are returned in the
265  * right order, and both get there.
266  */
267 ATF_TC_WITHOUT_HEAD(two_files);
268 ATF_TC_BODY(two_files, tc)
269 {
270 	struct stat getfd_1_stat, getfd_2_stat, putfd_1_stat, putfd_2_stat;
271 	int fd[2], getfd_1, getfd_2, putfd_1, putfd_2;
272 
273 	domainsocketpair(fd);
274 	tempfile(&putfd_1);
275 	tempfile(&putfd_2);
276 	dofstat(putfd_1, &putfd_1_stat);
277 	dofstat(putfd_2, &putfd_2_stat);
278 	sendfd(fd[0], putfd_1);
279 	sendfd(fd[0], putfd_2);
280 	close(putfd_1);
281 	close(putfd_2);
282 	recvfd(fd[1], &getfd_1);
283 	recvfd(fd[1], &getfd_2);
284 	dofstat(getfd_1, &getfd_1_stat);
285 	dofstat(getfd_2, &getfd_2_stat);
286 	samefile(&putfd_1_stat, &getfd_1_stat);
287 	samefile(&putfd_2_stat, &getfd_2_stat);
288 	close(getfd_1);
289 	close(getfd_2);
290 	closesocketpair(fd);
291 }
292 
293 /*
294  * Big bundling test.  Send an endpoint of the UNIX domain socket over itself,
295  * closing the door behind it.
296  */
297 ATF_TC_WITHOUT_HEAD(bundle);
298 ATF_TC_BODY(bundle, tc)
299 {
300 	int fd[2], getfd;
301 
302 	domainsocketpair(fd);
303 
304 	sendfd(fd[0], fd[0]);
305 	close(fd[0]);
306 	recvfd(fd[1], &getfd);
307 	close(getfd);
308 	close(fd[1]);
309 }
310 
311 /*
312  * Big bundling test part two: Send an endpoint of the UNIX domain socket over
313  * itself, close the door behind it, and never remove it from the other end.
314  */
315 ATF_TC_WITHOUT_HEAD(bundle_cancel);
316 ATF_TC_BODY(bundle_cancel, tc)
317 {
318 	int fd[2];
319 
320 	domainsocketpair(fd);
321 	sendfd(fd[0], fd[0]);
322 	sendfd(fd[1], fd[0]);
323 	closesocketpair(fd);
324 }
325 
326 /*
327  * Test for PR 151758: Send an character device over the UNIX domain socket
328  * and then close both sockets to orphan the device.
329  */
330 ATF_TC_WITHOUT_HEAD(devfs_orphan);
331 ATF_TC_BODY(devfs_orphan, tc)
332 {
333 	int fd[2], putfd;
334 
335 	domainsocketpair(fd);
336 	devnull(&putfd);
337 	sendfd(fd[0], putfd);
338 	close(putfd);
339 	closesocketpair(fd);
340 }
341 
342 #define	LOCAL_SENDSPACE_SYSCTL	"net.local.stream.sendspace"
343 
344 /*
345  * Test for PR 181741. Receiver sets LOCAL_CREDS, and kernel prepends a
346  * control message to the data. Sender sends large payload.
347  * Payload + SCM_RIGHTS + LOCAL_CREDS hit socket buffer limit, and receiver
348  * receives truncated data.
349  */
350 ATF_TC_WITHOUT_HEAD(rights_creds_payload);
351 ATF_TC_BODY(rights_creds_payload, tc)
352 {
353 	const int on = 1;
354 	u_long sendspace;
355 	size_t len;
356 	void *buf;
357 	int fd[2], getfd, putfd, rc;
358 
359 	atf_tc_expect_fail("PR 181741: Packet loss when 'control' messages "
360 	    "are present with large data");
361 
362 	len = sizeof(sendspace);
363 	rc = sysctlbyname(LOCAL_SENDSPACE_SYSCTL, &sendspace,
364 	    &len, NULL, 0);
365 	ATF_REQUIRE_MSG(rc != -1,
366 	    "sysctl %s failed: %s", LOCAL_SENDSPACE_SYSCTL, strerror(errno));
367 
368 	buf = calloc(1, sendspace);
369 	ATF_REQUIRE(buf != NULL);
370 
371 	domainsocketpair(fd);
372 	rc = setsockopt(fd[1], 0, LOCAL_CREDS, &on, sizeof(on));
373 	ATF_REQUIRE_MSG(rc != -1, "setsockopt(LOCAL_CREDS) failed: %s",
374 	    strerror(errno));
375 	tempfile(&putfd);
376 	sendfd_payload(fd[0], putfd, buf, sendspace);
377 	recvfd_payload(fd[1], &getfd, buf, sendspace);
378 	close(putfd);
379 	close(getfd);
380 	closesocketpair(fd);
381 }
382 
383 ATF_TP_ADD_TCS(tp)
384 {
385 
386 	ATF_TP_ADD_TC(tp, simple_send_fd);
387 	ATF_TP_ADD_TC(tp, send_and_close);
388 	ATF_TP_ADD_TC(tp, send_and_cancel);
389 	ATF_TP_ADD_TC(tp, two_files);
390 	ATF_TP_ADD_TC(tp, bundle);
391 	ATF_TP_ADD_TC(tp, bundle_cancel);
392 	ATF_TP_ADD_TC(tp, devfs_orphan);
393 	ATF_TP_ADD_TC(tp, rights_creds_payload);
394 
395 	return (atf_no_error());
396 }
397