1 /*-
2  * Copyright (c) 2010 Mikolaj Golub
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 /*
28  * This regression test attempts to trigger a race that occurs when both
29  * endpoints of a connected UNIX domain socket are closed at once.  The two
30  * close paths may run concurrently leading to a call to sodisconnect() on an
31  * already-closed socket in kernel.  Before it was fixed, this might lead to
32  * ENOTCONN being returned improperly from close().
33  *
34  * This race is fairly timing-dependent, so it effectively requires SMP, and
35  * may not even trigger then.
36  */
37 
38 #include <sys/types.h>
39 #include <sys/select.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
42 #include <sys/un.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <signal.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <strings.h>
51 #include <string.h>
52 #include <unistd.h>
53 #include <err.h>
54 
55 static char socket_path[] = "tmp.XXXXXXXX";
56 
57 #define	USLEEP	100
58 #define	LOOPS	100000
59 
60 int
main(void)61 main(void)
62 {
63 	struct sockaddr_un servaddr;
64 	int listenfd, connfd, pid;
65 	u_int counter, ncpus;
66 	size_t len;
67 
68 	len = sizeof(ncpus);
69 	if (sysctlbyname("kern.smp.cpus", &ncpus, &len, NULL, 0) < 0)
70 		err(1, "kern.smp.cpus");
71 	if (len != sizeof(ncpus))
72 		errx(1, "kern.smp.cpus: invalid length");
73 	if (ncpus < 2)
74 		warnx("SMP not present, test may be unable to trigger race");
75 
76 	if (mkstemp(socket_path) == -1)
77 		err(1, "mkstemp failed");
78 	unlink(socket_path);
79 
80 	/*
81 	 * Create a UNIX domain socket that the child will repeatedly
82 	 * accept() from, and that the parent will repeatedly connect() to.
83 	 */
84 	if ((listenfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0)
85 		err(1, "parent: socket error");
86 	(void)unlink(socket_path);
87 	bzero(&servaddr, sizeof(servaddr));
88 	servaddr.sun_family = AF_LOCAL;
89 	strcpy(servaddr.sun_path, socket_path);
90 	if (bind(listenfd, (struct sockaddr *) &servaddr,
91 	    sizeof(servaddr)) < 0)
92 		err(1, "parent: bind error");
93 	if (listen(listenfd, 1024) < 0)
94 		err(1, "parent: listen error");
95 
96 	pid = fork();
97 	if (pid == -1)
98 		err(1, "fork()");
99 	if (pid != 0) {
100 		/*
101 		 * In the parent, repeatedly connect and disconnect from the
102 		 * socket, attempting to induce the race.
103 		 */
104 		close(listenfd);
105 		sleep(1);
106 		bzero(&servaddr, sizeof(servaddr));
107 		servaddr.sun_family = AF_LOCAL;
108 		strcpy(servaddr.sun_path, socket_path);
109 		for (counter = 0; counter < LOOPS; counter++) {
110 			if ((connfd = socket(AF_LOCAL, SOCK_STREAM, 0)) < 0) {
111 				(void)kill(pid, SIGTERM);
112 				err(1, "parent: socket error");
113 			}
114 			if (connect(connfd, (struct sockaddr *)&servaddr,
115 			    sizeof(servaddr)) < 0) {
116 			    	(void)kill(pid, SIGTERM);
117 				err(1, "parent: connect error");
118 			}
119 			if (close(connfd) < 0) {
120 				(void)kill(pid, SIGTERM);
121 				err(1, "parent: close error");
122 			}
123 			usleep(USLEEP);
124 		}
125 		(void)kill(pid, SIGTERM);
126 	} else {
127 		/*
128 		 * In the child, loop accepting and closing.  We may pick up
129 		 * the race here so report errors from close().
130 		 */
131 		for ( ; ; ) {
132 			if ((connfd = accept(listenfd,
133 			    (struct sockaddr *)NULL, NULL)) < 0)
134 				err(1, "child: accept error");
135 			if (close(connfd) < 0)
136 				err(1, "child: close error");
137 		}
138 	}
139 	printf("OK\n");
140 	exit(0);
141 }
142