2 * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <sys/socket.h>
34 #include "got_error.h"
41 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
44 struct gotd_listen_client {
45 STAILQ_ENTRY(gotd_listen_client) entry;
50 STAILQ_HEAD(gotd_listen_clients, gotd_listen_client);
52 static struct gotd_listen_clients gotd_listen_clients[GOTD_CLIENT_TABLE_SIZE];
53 static SIPHASH_KEY clients_hash_key;
54 static volatile int listen_client_cnt;
57 struct gotd_uid_connection_counter {
58 STAILQ_ENTRY(gotd_uid_connection_counter) entry;
62 STAILQ_HEAD(gotd_client_uids, gotd_uid_connection_counter);
63 static struct gotd_client_uids gotd_client_uids[GOTD_CLIENT_TABLE_SIZE];
64 static SIPHASH_KEY uid_hash_key;
70 struct gotd_imsgev iev;
71 struct gotd_imsgev pause;
72 struct gotd_uid_connection_limit *connection_limits;
73 size_t nconnection_limits;
78 static void listen_shutdown(void);
81 listen_sighdlr(int sig, short event, void *arg)
84 * Normal signal handler rules don't apply because libevent
99 fatalx("unexpected signal");
104 client_hash(uint32_t client_id)
106 return SipHash24(&clients_hash_key, &client_id, sizeof(client_id));
110 add_client(struct gotd_listen_client *client)
112 uint64_t slot = client_hash(client->id) % nitems(gotd_listen_clients);
113 STAILQ_INSERT_HEAD(&gotd_listen_clients[slot], client, entry);
117 static struct gotd_listen_client *
118 find_client(uint32_t client_id)
121 struct gotd_listen_client *c;
123 slot = client_hash(client_id) % nitems(gotd_listen_clients);
124 STAILQ_FOREACH(c, &gotd_listen_clients[slot], entry) {
125 if (c->id == client_id)
140 duplicate = (find_client(id) != NULL);
141 } while (duplicate || id == 0);
149 return SipHash24(&uid_hash_key, &euid, sizeof(euid));
153 add_uid_connection_counter(struct gotd_uid_connection_counter *counter)
155 uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
156 STAILQ_INSERT_HEAD(&gotd_client_uids[slot], counter, entry);
160 remove_uid_connection_counter(struct gotd_uid_connection_counter *counter)
162 uint64_t slot = uid_hash(counter->euid) % nitems(gotd_client_uids);
163 STAILQ_REMOVE(&gotd_client_uids[slot], counter,
164 gotd_uid_connection_counter, entry);
167 static struct gotd_uid_connection_counter *
168 find_uid_connection_counter(uid_t euid)
171 struct gotd_uid_connection_counter *c;
173 slot = uid_hash(euid) % nitems(gotd_client_uids);
174 STAILQ_FOREACH(c, &gotd_client_uids[slot], entry) {
182 struct gotd_uid_connection_limit *
183 gotd_find_uid_connection_limit(struct gotd_uid_connection_limit *limits,
184 size_t nlimits, uid_t uid)
186 /* This array is always sorted to allow for binary search. */
187 int i, left = 0, right = nlimits - 1;
189 while (left <= right) {
190 i = ((left + right) / 2);
191 if (limits[i].uid == uid)
193 if (limits[i].uid > uid)
202 static const struct got_error *
203 disconnect(struct gotd_listen_client *client)
205 struct gotd_uid_connection_counter *counter;
209 log_debug("client on fd %d disconnecting", client->fd);
211 slot = client_hash(client->id) % nitems(gotd_listen_clients);
212 STAILQ_REMOVE(&gotd_listen_clients[slot], client,
213 gotd_listen_client, entry);
215 counter = find_uid_connection_counter(client->euid);
217 if (counter->nconnections > 0)
218 counter->nconnections--;
219 if (counter->nconnections == 0) {
220 remove_uid_connection_counter(counter);
225 client_fd = client->fd;
229 if (close(client_fd) == -1)
230 return got_error_from_errno("close");
236 accept_reserve(int fd, struct sockaddr *addr, socklen_t *addrlen,
237 int reserve, volatile int *counter)
241 if (getdtablecount() + reserve +
242 ((*counter + 1) * GOTD_FD_NEEDED) >= getdtablesize()) {
243 log_debug("inflight fds exceeded");
248 if ((ret = accept4(fd, addr, addrlen,
249 SOCK_NONBLOCK | SOCK_CLOEXEC)) > -1) {
257 gotd_accept_paused(int fd, short event, void *arg)
259 event_add(&gotd_listen.iev.ev, NULL);
263 gotd_accept(int fd, short event, void *arg)
265 struct gotd_imsgev *iev = arg;
266 struct sockaddr_storage ss;
267 struct timeval backoff;
270 struct gotd_listen_client *client = NULL;
271 struct gotd_uid_connection_counter *counter = NULL;
272 struct gotd_imsg_connect iconn;
279 if (event_add(&gotd_listen.iev.ev, NULL) == -1) {
280 log_warn("event_add");
283 if (event & EV_TIMEOUT)
288 /* Other backoff conditions apart from EMFILE/ENFILE? */
289 s = accept_reserve(fd, (struct sockaddr *)&ss, &len, GOTD_FD_RESERVE,
299 event_del(&gotd_listen.iev.ev);
300 evtimer_add(&gotd_listen.pause.ev, &backoff);
308 if (listen_client_cnt >= GOTD_MAXCLIENTS)
311 if (getpeereid(s, &euid, &egid) == -1) {
312 log_warn("getpeerid");
316 counter = find_uid_connection_counter(euid);
317 if (counter == NULL) {
318 counter = calloc(1, sizeof(*counter));
319 if (counter == NULL) {
320 log_warn("%s: calloc", __func__);
323 counter->euid = euid;
324 counter->nconnections = 1;
325 add_uid_connection_counter(counter);
327 int max_connections = GOTD_MAX_CONN_PER_UID;
328 struct gotd_uid_connection_limit *limit;
330 limit = gotd_find_uid_connection_limit(
331 gotd_listen.connection_limits,
332 gotd_listen.nconnection_limits, euid);
334 max_connections = limit->max_connections;
336 if (counter->nconnections >= max_connections) {
337 log_warnx("maximum connections exceeded for uid %d",
341 counter->nconnections++;
344 client = calloc(1, sizeof(*client));
345 if (client == NULL) {
346 log_warn("%s: calloc", __func__);
349 client->id = get_client_id();
354 log_debug("%s: new client connected on fd %d uid %d gid %d", __func__,
355 client->fd, euid, egid);
357 memset(&iconn, 0, sizeof(iconn));
358 iconn.client_id = client->id;
363 log_warn("%s: dup", __func__);
366 if (gotd_imsg_compose_event(iev, GOTD_IMSG_CONNECT, PROC_LISTEN, s,
367 &iconn, sizeof(iconn)) == -1) {
368 log_warn("imsg compose CONNECT");
381 static const struct got_error *
382 recv_disconnect(struct imsg *imsg)
384 struct gotd_imsg_disconnect idisconnect;
386 struct gotd_listen_client *client = NULL;
388 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
389 if (datalen != sizeof(idisconnect))
390 return got_error(GOT_ERR_PRIVSEP_LEN);
391 memcpy(&idisconnect, imsg->data, sizeof(idisconnect));
393 log_debug("client disconnecting");
395 client = find_client(idisconnect.client_id);
397 return got_error(GOT_ERR_CLIENT_ID);
399 return disconnect(client);
403 listen_dispatch(int fd, short event, void *arg)
405 const struct got_error *err = NULL;
406 struct gotd_imsgev *iev = arg;
407 struct imsgbuf *ibuf = &iev->ibuf;
412 if (event & EV_READ) {
413 if ((n = imsg_read(ibuf)) == -1 && errno != EAGAIN)
414 fatal("imsg_read error");
415 if (n == 0) /* Connection closed. */
419 if (event & EV_WRITE) {
420 n = msgbuf_write(&ibuf->w);
421 if (n == -1 && errno != EAGAIN)
422 fatal("msgbuf_write");
423 if (n == 0) /* Connection closed. */
428 if ((n = imsg_get(ibuf, &imsg)) == -1)
429 fatal("%s: imsg_get", __func__);
430 if (n == 0) /* No more messages. */
433 switch (imsg.hdr.type) {
434 case GOTD_IMSG_DISCONNECT:
435 err = recv_disconnect(&imsg);
437 log_warnx("disconnect: %s", err->msg);
440 log_debug("unexpected imsg %d", imsg.hdr.type);
448 gotd_imsg_event_add(iev);
450 /* This pipe is dead. Remove its event handler */
452 event_loopexit(NULL);
457 listen_main(const char *title, int gotd_socket,
458 struct gotd_uid_connection_limit *connection_limits,
459 size_t nconnection_limits)
461 struct gotd_imsgev iev;
462 struct event evsigint, evsigterm, evsighup, evsigusr1;
464 arc4random_buf(&clients_hash_key, sizeof(clients_hash_key));
465 arc4random_buf(&uid_hash_key, sizeof(uid_hash_key));
467 gotd_listen.title = title;
468 gotd_listen.pid = getpid();
469 gotd_listen.fd = gotd_socket;
470 gotd_listen.connection_limits = connection_limits;
471 gotd_listen.nconnection_limits = nconnection_limits;
473 signal_set(&evsigint, SIGINT, listen_sighdlr, NULL);
474 signal_set(&evsigterm, SIGTERM, listen_sighdlr, NULL);
475 signal_set(&evsighup, SIGHUP, listen_sighdlr, NULL);
476 signal_set(&evsigusr1, SIGUSR1, listen_sighdlr, NULL);
477 signal(SIGPIPE, SIG_IGN);
479 signal_add(&evsigint, NULL);
480 signal_add(&evsigterm, NULL);
481 signal_add(&evsighup, NULL);
482 signal_add(&evsigusr1, NULL);
484 imsg_init(&iev.ibuf, GOTD_FILENO_MSG_PIPE);
485 iev.handler = listen_dispatch;
486 iev.events = EV_READ;
487 iev.handler_arg = NULL;
488 event_set(&iev.ev, iev.ibuf.fd, EV_READ, listen_dispatch, &iev);
489 if (event_add(&iev.ev, NULL) == -1)
492 event_set(&gotd_listen.iev.ev, gotd_listen.fd, EV_READ | EV_PERSIST,
494 if (event_add(&gotd_listen.iev.ev, NULL))
496 evtimer_set(&gotd_listen.pause.ev, gotd_accept_paused, NULL);
504 listen_shutdown(void)
506 log_debug("shutting down");
508 free(gotd_listen.connection_limits);
509 if (gotd_listen.fd != -1)
510 close(gotd_listen.fd);