xref: /freebsd/tools/regression/sockets/accept_fd_leak/accept_fd_leak.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*-
2  * Copyright (c) 2004 Robert N. M. Watson
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 #include <sys/param.h>
28 #include <sys/socket.h>
29 #include <sys/wait.h>
30 
31 #include <netinet/in.h>
32 
33 #include <err.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #define	BIND_ATTEMPTS	10
43 #define	LOOPS	500
44 #define	NUM_ATTEMPTS	1000
45 
46 static volatile int quit;
47 
48 static void
49 child_died(int sig __unused)
50 {
51 
52 	quit = 1;
53 }
54 
55 /*
56  * This test is intended to detect a leak of a file descriptor in the process
57  * following a failed non-blocking accept.  It measures an available fd
58  * baseline, then performs 1000 failing accepts, then checks to see what the
59  * next fd is.  It relies on sequential fd allocation, and will test for it
60  * briefly before beginning (not 100% reliable, but a good start).
61  */
62 int
63 main(void)
64 {
65 	struct sockaddr_in sin;
66 	socklen_t size;
67 	pid_t child;
68 	int fd1, fd2, fd3, i, listen_port, s, status;
69 
70 	printf("1..2\n");
71 
72 	/*
73 	 * Check for sequential fd allocation, and give up early if not.
74 	 */
75 	fd1 = dup(STDIN_FILENO);
76 	fd2 = dup(STDIN_FILENO);
77 	if (fd2 != fd1 + 1)
78 		errx(-1, "Non-sequential fd allocation\n");
79 
80 	s = socket(PF_INET, SOCK_STREAM, 0);
81 	if (s == -1)
82 		errx(-1, "socket: %s", strerror(errno));
83 
84 	bzero(&sin, sizeof(sin));
85 	sin.sin_len = sizeof(sin);
86 	sin.sin_family = AF_INET;
87 	sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
88 
89 	srandomdev();
90 
91 	for (i = 0; i < BIND_ATTEMPTS; i++) {
92 		/* Pick a random unprivileged port 1025-65535 */
93 		listen_port = MAX((int)random() % 65535, 1025);
94 		sin.sin_port = htons(listen_port);
95 		if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == 0)
96 			break;
97 		warn("bind with %d failed", listen_port);
98 		usleep(1000);
99 	}
100 	if (i >= BIND_ATTEMPTS) {
101 		printf("Bail out!\n");
102 		exit(1);
103 	}
104 
105 	if (listen(s, -1) != 0)
106 		errx(-1, "listen: %s", strerror(errno));
107 
108 	i = fcntl(s, F_GETFL);
109 	if (i == -1)
110 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
111 	i |= O_NONBLOCK;
112 	if (fcntl(s, F_SETFL, i) != 0)
113 		errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
114 	i = fcntl(s, F_GETFL);
115 	if (i == -1)
116 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
117 	if ((i & O_NONBLOCK) != O_NONBLOCK)
118 		errx(-1, "Failed to set O_NONBLOCK (i=0x%x)\n", i);
119 
120 	for (i = 0; i < LOOPS; i++) {
121 		size = sizeof(sin);
122 		if (accept(s, (struct sockaddr *)&sin, &size) != -1)
123 			errx(-1, "accept succeeded\n");
124 		if (errno != EAGAIN)
125 			errx(-1, "accept: %s", strerror(errno));
126 	}
127 
128 	/*
129 	 * Allocate a file descriptor and make sure it's fd2+2.  2 because
130 	 * we allocate an fd for the socket.
131 	 */
132 	fd3 = dup(STDIN_FILENO);
133 	if (fd3 != fd2 + 2)
134 		printf("not ok 1 - (%d, %d, %d)\n", fd1, fd2, fd3);
135 	else
136 		printf("ok 1\n");
137 
138 	/*
139 	 * Try failing accept's w/o non-blocking where the destination
140 	 * address pointer is invalid.
141 	 */
142 	close(fd3);
143 	signal(SIGCHLD, child_died);
144 	child = fork();
145 	if (child < 0)
146 		errx(-1, "fork: %s", strerror(errno));
147 
148 	/*
149 	 * Child process does `NUM_ATTEMPTS` connects.
150 	 */
151 	if (child == 0) {
152 		close(fd1);
153 		close(fd2);
154 		close(s);
155 
156 		bzero(&sin, sizeof(sin));
157 		sin.sin_len = sizeof(sin);
158 		sin.sin_family = AF_INET;
159 		sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
160 		sin.sin_port = htons(listen_port);
161 
162 		for (i = 0; i < NUM_ATTEMPTS; i++) {
163 			s = socket(PF_INET, SOCK_STREAM, 0);
164 			if (s == -1)
165 				errx(-1, "socket: %s", strerror(errno));
166 			if (connect(s, (struct sockaddr *)&sin,
167 			    sizeof(sin)) < 0)
168 				errx(-1, "connect: %s", strerror(errno));
169 			close(s);
170 		}
171 		_exit(0);
172 	}
173 
174 	/* Reset back to a blocking socket. */
175 	i = fcntl(s, F_GETFL);
176 	if (i == -1)
177 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
178 	i &= ~O_NONBLOCK;
179 	if (fcntl(s, F_SETFL, i) != 0)
180 		errx(-1, "ioctl(F_SETFL): %s", strerror(errno));
181 	i = fcntl(s, F_GETFL);
182 	if (i == -1)
183 		errx(-1, "ioctl(F_GETFL): %s", strerror(errno));
184 	if (i & O_NONBLOCK)
185 		errx(-1, "Failed to clear O_NONBLOCK (i=0x%x)\n", i);
186 
187 	/* Do `NUM_ATTEMPTS` accepts with an invalid pointer. */
188 	for (i = 0; !quit && i < NUM_ATTEMPTS; i++) {
189 		size = sizeof(sin);
190 		if (accept(s, (struct sockaddr *)(uintptr_t)(0x100),
191 		    &size) != -1)
192 			errx(-1, "accept succeeded\n");
193 		if (errno != EFAULT)
194 			errx(-1, "accept: %s", strerror(errno));
195 	}
196 
197 	if (waitpid(child, &status, 0) < 0)
198 		errx(-1, "waitpid: %s", strerror(errno));
199 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
200 		warnx("child process died");
201 
202 	/*
203 	 * Allocate a file descriptor and make sure it's fd2+2.  2 because
204 	 * we allocate an fd for the socket.
205 	 */
206 	fd3 = dup(STDIN_FILENO);
207 	if (fd3 != fd2 + 2)
208 		printf("not ok 2 - (%d, %d, %d)\n", fd1, fd2, fd3);
209 	else
210 		printf("ok 2\n");
211 
212 	return (0);
213 }
214