Blob


1 /*
2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
20 #include <sys/uio.h>
22 #include <errno.h>
23 #include <event.h>
24 #include <siphash.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <imsg.h>
30 #include <limits.h>
31 #include <sha1.h>
32 #include <sha2.h>
33 #include <signal.h>
34 #include <unistd.h>
36 #include "got_error.h"
37 #include "got_path.h"
39 #include "gotd.h"
40 #include "log.h"
41 #include "listen.h"
43 #ifndef nitems
44 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
45 #endif
47 struct gotd_listen_client {
48 STAILQ_ENTRY(gotd_listen_client) entry;
49 uint32_t id;
50 int fd;
51 uid_t euid;
52 };
53 STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
55 static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
56 static SIPHASH_KEY clients_hash_key;
57 static volatile int listen_client_cnt;
58 static int inflight;
60 struct gotd_uid_connection_counter {
61 STAILQ_ENTRY(gotd_uid_connection_counter) entry;
62 uid_t euid;
63 int nconnections;
64 };
65 STAILQ_HEAD(gotd_client_uids, gotd_uid_connection_counter);
66 static struct gotd_client_uids gotd_client_uids[GOTD_CLIENT_TABLE_SIZE];
67 static SIPHASH_KEY uid_hash_key;
69 static struct {
70 pid_t pid;
71 const char *title;
72 int fd;
73 struct gotd_imsgev iev;
74 struct gotd_imsgev pause;
75 struct gotd_uid_connection_limit *connection_limits;
76 size_t nconnection_limits;
77 } gotd_listen;
79 static int inflight;
81 static void listen_shutdown(void);
83 static void
84 listen_sighdlr(int sig, short event, void *arg)
85 {
86 /*
87 * Normal signal handler rules don't apply because libevent
88 * decouples for us.
89 */
91 switch (sig) {
92 case SIGHUP:
93 break;
94 case SIGUSR1:
95 break;
96 case SIGTERM:
97 case SIGINT:
98 listen_shutdown();
99 /* NOTREACHED */
100 break;
101 default:
102 fatalx("unexpected signal");
106 static uint64_t
107 client_hash(uint32_t client_id)
109 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
112 static void
113 add_client(struct gotd_listen_client *client)
115 uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
116 STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
117 listen_client_cnt++;
120 static struct gotd_listen_client *
121 find_client(uint32_t client_id)
123 uint64_t slot;
124 struct gotd_listen_client *c;
126 slot = client_hash(client_id) % nitems(gotd_listen_clients);
127 STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
128 if (c->id == client_id)
129 return c;
132 return NULL;
135 static uint32_t
136 get_client_id(void)
138 int duplicate = 0;
139 uint32_t id;
141 do {
142 id = arc4random();
143 duplicate = (find_client(id) != NULL);
144 } while (duplicate || id == 0);
146 return id;
149 static uint64_t
150 uid_hash(uid_t euid)
152 return SipHash24(&uid_hash_key, &euid, sizeof(euid));
155 static void
156 add_uid_connection_counter(struct gotd_uid_connection_counter *counter)
158 uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
159 STAILQ_INSERT_HEAD(&gotd_client_uids[slot], counter, entry);
162 static void
163 remove_uid_connection_counter(struct gotd_uid_connection_counter *counter)
165 uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
166 STAILQ_REMOVE(&gotd_client_uids[slot], counter,
167 gotd_uid_connection_counter, entry);
170 static struct gotd_uid_connection_counter *
171 find_uid_connection_counter(uid_t euid)
173 uint64_t slot;
174 struct gotd_uid_connection_counter *c;
176 slot = uid_hash(euid) % nitems(gotd_client_uids);
177 STAILQ_FOREACH(c, &gotd_client_uids[slot], entry) {
178 if (c->euid == euid)
179 return c;
182 return NULL;
185 static const struct got_error *
186 disconnect(struct gotd_listen_client *client)
188 struct gotd_uid_connection_counter *counter;
189 uint64_t slot;
190 int client_fd;
192 log_debug("client on fd %d disconnecting", client->fd);
194 slot = client_hash(client->id) % nitems(gotd_listen_clients);
195 STAILQ_REMOVE(&gotd_listen_clients[slot], client,
196 gotd_listen_client, entry);
198 counter = find_uid_connection_counter(client->euid);
199 if (counter) {
200 if (counter->nconnections > 0)
201 counter->nconnections--;
202 if (counter->nconnections == 0) {
203 remove_uid_connection_counter(counter);
204 free(counter);
208 client_fd = client->fd;
209 free(client);
210 inflight--;
211 listen_client_cnt--;
212 if (close(client_fd) == -1)
213 return got_error_from_errno("close");
215 return NULL;
218 static int
219 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
220 int reserve, volatile int *counter)
222 int ret;
224 if (getdtablecount() + reserve +
225 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
226 log_debug("inflight fds exceeded");
227 errno = EMFILE;
228 return -1;
231 if ((ret = accept4(fd, addr, addrlen,
232 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
233 (*counter)++;
236 return ret;
239 static void
240 gotd_accept_paused(int fd, short event, void *arg)
242 event_add(&gotd_listen.iev.ev, NULL);
245 static void
246 gotd_accept(int fd, short event, void *arg)
248 struct gotd_imsgev *iev = arg;
249 struct sockaddr_storage ss;
250 struct timeval backoff;
251 socklen_t len;
252 int s = -1;
253 struct gotd_listen_client *client = NULL;
254 struct gotd_uid_connection_counter *counter = NULL;
255 struct gotd_imsg_connect iconn;
256 uid_t euid;
257 gid_t egid;
259 backoff.tv_sec = 1;
260 backoff.tv_usec = 0;
262 if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
263 log_warn("event_add");
264 return;
266 if (event & EV_TIMEOUT)
267 return;
269 len = sizeof(ss);
271 /* Other backoff conditions apart from EMFILE/ENFILE? */
272 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
273 &inflight);
274 if (s == -1) {
275 switch (errno) {
276 case EINTR:
277 case EWOULDBLOCK:
278 case ECONNABORTED:
279 return;
280 case EMFILE:
281 case ENFILE:
282 event_del(&gotd_listen.iev.ev);
283 evtimer_add(&gotd_listen.pause.ev, &backoff);
284 return;
285 default:
286 log_warn("accept");
287 return;
291 if (listen_client_cnt >= GOTD_MAXCLIENTS)
292 goto err;
294 if (getpeereid(s, &euid, &egid) == -1) {
295 log_warn("getpeerid");
296 goto err;
299 counter = find_uid_connection_counter(euid);
300 if (counter == NULL) {
301 counter = calloc(1, sizeof(*counter));
302 if (counter == NULL) {
303 log_warn("%s: calloc", __func__);
304 goto err;
306 counter->euid = euid;
307 counter->nconnections = 1;
308 add_uid_connection_counter(counter);
309 } else {
310 int max_connections = GOTD_MAX_CONN_PER_UID;
311 struct gotd_uid_connection_limit *limit;
313 limit = gotd_find_uid_connection_limit(
314 gotd_listen.connection_limits,
315 gotd_listen.nconnection_limits, euid);
316 if (limit)
317 max_connections = limit->max_connections;
319 if (counter->nconnections >= max_connections) {
320 log_warnx("maximum connections exceeded for uid %d",
321 euid);
322 goto err;
324 counter->nconnections++;
327 client = calloc(1, sizeof(*client));
328 if (client == NULL) {
329 log_warn("%s: calloc", __func__);
330 goto err;
332 client->id = get_client_id();
333 client->fd = s;
334 client->euid = euid;
335 s = -1;
336 add_client(client);
337 log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
338 client->fd, euid, egid);
340 memset(&iconn, 0, sizeof(iconn));
341 iconn.client_id = client->id;
342 iconn.euid = euid;
343 iconn.egid = egid;
344 s = dup(client->fd);
345 if (s == -1) {
346 log_warn("%s: dup", __func__);
347 goto err;
349 if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
350 &iconn, sizeof(iconn)) == -1) {
351 log_warn("imsg compose CONNECT");
352 goto err;
355 return;
356 err:
357 inflight--;
358 if (client)
359 disconnect(client);
360 if (s != -1)
361 close(s);
364 static const struct got_error *
365 recv_disconnect(struct imsg *imsg)
367 struct gotd_imsg_disconnect idisconnect;
368 size_t datalen;
369 struct gotd_listen_client *client = NULL;
371 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
372 if (datalen != sizeof(idisconnect))
373 return got_error(GOT_ERR_PRIVSEP_LEN);
374 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
376 log_debug("client disconnecting");
378 client = find_client(idisconnect.client_id);
379 if (client == NULL)
380 return got_error(GOT_ERR_CLIENT_ID);
382 return disconnect(client);
385 static void
386 listen_dispatch(int fd, short event, void *arg)
388 const struct got_error *err = NULL;
389 struct gotd_imsgev *iev = arg;
390 struct imsgbuf *ibuf = &iev->ibuf;
391 struct imsg imsg;
392 ssize_t n;
393 int shut = 0;
395 if (event & EV_READ) {
396 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
397 fatal("imsg_read error");
398 if (n == 0) /* Connection closed. */
399 shut = 1;
402 if (event & EV_WRITE) {
403 n = msgbuf_write(&ibuf->w);
404 if (n == -1 && errno != EAGAIN)
405 fatal("msgbuf_write");
406 if (n == 0) /* Connection closed. */
407 shut = 1;
410 for (;;) {
411 if ((n = imsg_get(ibuf, &imsg)) == -1)
412 fatal("%s: imsg_get", __func__);
413 if (n == 0) /* No more messages. */
414 break;
416 switch (imsg.hdr.type) {
417 case GOTD_IMSG_DISCONNECT:
418 err = recv_disconnect(&imsg);
419 if (err)
420 log_warnx("disconnect: %s", err->msg);
421 break;
422 default:
423 log_debug("unexpected imsg %d", imsg.hdr.type);
424 break;
427 imsg_free(&imsg);
430 if (!shut) {
431 gotd_imsg_event_add(iev);
432 } else {
433 /* This pipe is dead. Remove its event handler */
434 event_del(&iev->ev);
435 event_loopexit(NULL);
439 void
440 listen_main(const char *title, int gotd_socket,
441 struct gotd_uid_connection_limit *connection_limits,
442 size_t nconnection_limits)
444 struct gotd_imsgev iev;
445 struct event evsigint, evsigterm, evsighup, evsigusr1;
447 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
448 arc4random_buf(&uid_hash_key, sizeof(uid_hash_key));
450 gotd_listen.title = title;
451 gotd_listen.pid = getpid();
452 gotd_listen.fd = gotd_socket;
453 gotd_listen.connection_limits = connection_limits;
454 gotd_listen.nconnection_limits = nconnection_limits;
456 signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
457 signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
458 signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
459 signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
460 signal(SIGPIPE, SIG_IGN);
462 signal_add(&evsigint, NULL);
463 signal_add(&evsigterm, NULL);
464 signal_add(&evsighup, NULL);
465 signal_add(&evsigusr1, NULL);
467 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
468 iev.handler = listen_dispatch;
469 iev.events = EV_READ;
470 iev.handler_arg = NULL;
471 event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
472 if (event_add(&iev.ev, NULL) == -1)
473 fatalx("event add");
475 event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
476 gotd_accept, &iev);
477 if (event_add(&gotd_listen.iev.ev, NULL))
478 fatalx("event add");
479 evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
481 event_dispatch();
483 listen_shutdown();
486 static void
487 listen_shutdown(void)
489 log_debug("%s: shutting down", gotd_listen.title);
491 free(gotd_listen.connection_limits);
492 if (gotd_listen.fd != -1)
493 close(gotd_listen.fd);
495 exit(0);