xz: Avoid reading beyond EOF
[nbdkit/ericb.git] / server / sockets.c
blob3514c698bbe5e8de1393f4682075b4a8b93ad63a
1 /* nbdkit
2 * Copyright (C) 2013-2019 Red Hat Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * * 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.
15 * * Neither the name of Red Hat nor the names of its contributors may be
16 * used to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <stdbool.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <poll.h>
41 #include <errno.h>
42 #include <assert.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/un.h>
46 #include <netinet/in.h>
47 #include <netinet/tcp.h>
48 #include <netdb.h>
50 #ifdef HAVE_LIBSELINUX
51 #include <selinux/selinux.h>
52 #endif
54 #include <pthread.h>
56 #include "internal.h"
57 #include "utils.h"
59 static void
60 set_selinux_label (void)
62 if (selinux_label) {
63 #ifdef HAVE_LIBSELINUX
64 if (setsockcreatecon_raw (selinux_label) == -1) {
65 perror ("selinux-label: setsockcreatecon_raw");
66 exit (EXIT_FAILURE);
68 #else
69 fprintf (stderr,
70 "%s: --selinux-label option used, but "
71 "this binary was compiled without SELinux support\n",
72 program_name);
73 exit (EXIT_FAILURE);
74 #endif
78 static void
79 clear_selinux_label (void)
81 #ifdef HAVE_LIBSELINUX
82 if (selinux_label) {
83 if (setsockcreatecon_raw (NULL) == -1) {
84 perror ("selinux-label: setsockcreatecon_raw(NULL)");
85 exit (EXIT_FAILURE);
88 #endif
91 int *
92 bind_unix_socket (size_t *nr_socks)
94 size_t len;
95 int sock;
96 struct sockaddr_un addr;
97 int *ret;
99 assert (unixsocket);
100 assert (unixsocket[0] == '/');
102 len = strlen (unixsocket);
103 if (len >= UNIX_PATH_MAX) {
104 fprintf (stderr, "%s: -U: path too long: length %zu > max %d bytes\n",
105 program_name, len, UNIX_PATH_MAX-1);
106 exit (EXIT_FAILURE);
109 set_selinux_label ();
111 #ifdef SOCK_CLOEXEC
112 sock = socket (AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
113 #else
114 /* Fortunately, this code is only run at startup, so there is no
115 * risk of the fd leaking to a plugin's fork()
117 sock = set_cloexec (socket (AF_UNIX, SOCK_STREAM, 0));
118 #endif
119 if (sock == -1) {
120 perror ("bind_unix_socket: socket");
121 exit (EXIT_FAILURE);
124 addr.sun_family = AF_UNIX;
125 memcpy (addr.sun_path, unixsocket, len+1 /* trailing \0 */);
127 if (bind (sock, (struct sockaddr *) &addr, sizeof addr) == -1) {
128 perror (unixsocket);
129 exit (EXIT_FAILURE);
132 if (listen (sock, SOMAXCONN) == -1) {
133 perror ("listen");
134 exit (EXIT_FAILURE);
137 clear_selinux_label ();
139 ret = malloc (sizeof (int));
140 if (!ret) {
141 perror ("malloc");
142 exit (EXIT_FAILURE);
144 ret[0] = sock;
145 *nr_socks = 1;
147 debug ("bound to unix socket %s", unixsocket);
149 return ret;
152 int *
153 bind_tcpip_socket (size_t *nr_socks)
155 struct addrinfo *ai = NULL;
156 struct addrinfo hints;
157 struct addrinfo *a;
158 int err, opt;
159 int *socks = NULL;
160 bool addr_in_use = false;
162 if (port == NULL)
163 port = "10809";
165 memset (&hints, 0, sizeof hints);
166 hints.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
167 hints.ai_socktype = SOCK_STREAM;
169 err = getaddrinfo (ipaddr, port, &hints, &ai);
170 if (err != 0) {
171 fprintf (stderr, "%s: getaddrinfo: %s: %s: %s",
172 program_name,
173 ipaddr ? ipaddr : "<any>",
174 port,
175 gai_strerror (err));
176 exit (EXIT_FAILURE);
179 *nr_socks = 0;
181 for (a = ai; a != NULL; a = a->ai_next) {
182 int sock;
184 set_selinux_label ();
186 #ifdef SOCK_CLOEXEC
187 sock = socket (a->ai_family, a->ai_socktype | SOCK_CLOEXEC, a->ai_protocol);
188 #else
189 /* Fortunately, this code is only run at startup, so there is no
190 * risk of the fd leaking to a plugin's fork()
192 sock = set_cloexec (socket (a->ai_family, a->ai_socktype, a->ai_protocol));
193 #endif
194 if (sock == -1) {
195 perror ("bind_tcpip_socket: socket");
196 exit (EXIT_FAILURE);
199 opt = 1;
200 if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt) == -1)
201 perror ("setsockopt: SO_REUSEADDR");
203 #ifdef IPV6_V6ONLY
204 if (a->ai_family == PF_INET6) {
205 if (setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof opt) == -1)
206 perror ("setsockopt: IPv6 only");
208 #endif
210 if (bind (sock, a->ai_addr, a->ai_addrlen) == -1) {
211 if (errno == EADDRINUSE) {
212 addr_in_use = true;
213 close (sock);
214 continue;
216 perror ("bind");
217 exit (EXIT_FAILURE);
220 if (listen (sock, SOMAXCONN) == -1) {
221 perror ("listen");
222 exit (EXIT_FAILURE);
225 clear_selinux_label ();
227 (*nr_socks)++;
228 socks = realloc (socks, sizeof (int) * (*nr_socks));
229 if (!socks) {
230 perror ("realloc");
231 exit (EXIT_FAILURE);
233 socks[*nr_socks - 1] = sock;
236 freeaddrinfo (ai);
238 if (*nr_socks == 0 && addr_in_use) {
239 fprintf (stderr, "%s: unable to bind to any sockets: %s\n",
240 program_name, strerror (EADDRINUSE));
241 exit (EXIT_FAILURE);
244 debug ("bound to IP address %s:%s (%zu socket(s))",
245 ipaddr ? ipaddr : "<any>", port, *nr_socks);
247 return socks;
250 void
251 free_listening_sockets (int *socks, size_t nr_socks)
253 size_t i;
255 for (i = 0; i < nr_socks; ++i)
256 close (socks[i]);
257 free (socks);
260 struct thread_data {
261 int sock;
262 size_t instance_num;
265 static void *
266 start_thread (void *datav)
268 struct thread_data *data = datav;
270 debug ("accepted connection");
272 /* Set thread-local data. */
273 threadlocal_new_server_thread ();
274 threadlocal_set_instance_num (data->instance_num);
276 handle_single_connection (data->sock, data->sock);
278 free (data);
279 return NULL;
282 static void
283 accept_connection (int listen_sock)
285 int err;
286 pthread_attr_t attrs;
287 pthread_t thread;
288 struct thread_data *thread_data;
289 static size_t instance_num = 1;
290 const int flag = 1;
292 thread_data = malloc (sizeof *thread_data);
293 if (!thread_data) {
294 perror ("malloc");
295 return;
298 thread_data->instance_num = instance_num++;
299 again:
300 #ifdef HAVE_ACCEPT4
301 thread_data->sock = accept4 (listen_sock, NULL, NULL, SOCK_CLOEXEC);
302 #else
303 /* If we were fully parallel, then this function could be accepting
304 * connections in one thread while another thread could be in a
305 * plugin trying to fork. But plugins.c forced thread_model to
306 * serialize_all_requests when it detects a lack of atomic CLOEXEC,
307 * at which point, we can use a mutex to ensure we aren't accepting
308 * until the plugin is not running, making non-atomicity okay.
310 assert (backend->thread_model (backend) <=
311 NBDKIT_THREAD_MODEL_SERIALIZE_ALL_REQUESTS);
312 lock_request (NULL);
313 thread_data->sock = set_cloexec (accept (listen_sock, NULL, NULL));
314 unlock_request (NULL);
315 #endif
316 if (thread_data->sock == -1) {
317 if (errno == EINTR || errno == EAGAIN)
318 goto again;
319 perror ("accept");
320 free (thread_data);
321 return;
324 /* Disable Nagle's algorithm on this socket. However we don't want
325 * to fail if this doesn't work.
327 setsockopt (thread_data->sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof flag);
329 /* Start a thread to handle this connection. Note we always do this
330 * even for non-threaded plugins. There are mutexes in plugins.c
331 * which ensure that non-threaded plugins are handled correctly.
333 pthread_attr_init (&attrs);
334 pthread_attr_setdetachstate (&attrs, PTHREAD_CREATE_DETACHED);
335 err = pthread_create (&thread, &attrs, start_thread, thread_data);
336 pthread_attr_destroy (&attrs);
337 if (err != 0) {
338 fprintf (stderr, "%s: pthread_create: %s\n", program_name, strerror (err));
339 close (thread_data->sock);
340 free (thread_data);
341 return;
344 /* If the thread starts successfully, then it is responsible for
345 * closing the socket and freeing thread_data.
349 /* Check the list of sockets plus quit_fd until a POLLIN event occurs
350 * on any of them.
352 * If POLLIN occurs on quit_fd do nothing except returning early
353 * (don't call accept_connection in this case).
355 * If POLLIN occurs on one of the sockets, call
356 * accept_connection (socks[i]) on each of them.
358 static void
359 check_sockets_and_quit_fd (int *socks, size_t nr_socks)
361 size_t i;
362 int r;
364 CLEANUP_FREE struct pollfd *fds =
365 malloc (sizeof (struct pollfd) * (nr_socks+1));
366 if (fds == NULL) {
367 perror ("malloc");
368 exit (EXIT_FAILURE);
371 for (i = 0; i < nr_socks; ++i) {
372 fds[i].fd = socks[i];
373 fds[i].events = POLLIN;
374 fds[i].revents = 0;
376 fds[nr_socks].fd = quit_fd;
377 fds[nr_socks].events = POLLIN;
378 fds[nr_socks].revents = 0;
380 r = poll (fds, nr_socks + 1, -1);
381 if (r == -1) {
382 if (errno == EINTR || errno == EAGAIN)
383 return;
384 perror ("poll");
385 exit (EXIT_FAILURE);
388 /* We don't even have to read quit_fd - just knowing that it has
389 * data means the signal handler ran, so we are ready to quit the
390 * loop.
392 if (fds[nr_socks].revents & POLLIN)
393 return;
395 for (i = 0; i < nr_socks; ++i) {
396 if (fds[i].revents & POLLIN)
397 accept_connection (socks[i]);
401 void
402 accept_incoming_connections (int *socks, size_t nr_socks)
404 while (!quit)
405 check_sockets_and_quit_fd (socks, nr_socks);