From: Gianmarco De Gregori <[email protected]> Every TCP client takes a file descriptor and accept() happens before the --max-clients check, so with the default limits (soft limit 1024, --max-clients 1024) the server runs out of descriptors before it can refuse a client.
When the server has a TCP listener, raise the soft limit to --max-clients plus some room for the other descriptors, up to the hard limit, and warn if that is not enough. Processes started by OpenVPN inherit the new limit. Not on macOS, where the event loop uses select() and cannot watch descriptors >= FD_SETSIZE. GitHub: closes OpenVPN/openvpn#1148 Change-Id: I17fafbe65fd3ff510a72ed809c85b0d19343fad2 Signed-off-by: Gianmarco De Gregori <[email protected]> Acked-by: Gert Doering <[email protected]> Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2211 --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2211 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Gert Doering <[email protected]> diff --git a/doc/man-sections/server-options.rst b/doc/man-sections/server-options.rst index 7a09809..5c1b129 100644 --- a/doc/man-sections/server-options.rst +++ b/doc/man-sections/server-options.rst @@ -418,6 +418,9 @@ --max-clients n Limit server to a maximum of ``n`` concurrent clients. Defaults to 1024. + With a TCP listener, the soft limit on open files is raised up to the hard + limit if needed to accept ``n`` clients (not on Windows and macOS). + --max-routes-per-client n Allow a maximum of ``n`` internal routes per client (default :code:`256`). This is designed to help contain DoS attacks where an diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index ad9f9f4..900b5a3 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -270,6 +270,68 @@ #endif /* + * Every TCP client takes a file descriptor, and accept() happens before the + * --max-clients check: raise the soft limit so that --max-clients is reached + * first, or warn if the hard limit does not allow it. + */ +static void +multi_check_nofile_limit(const struct context *t) +{ +#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) && !defined(TARGET_DARWIN) + bool tcp = false; + for (int i = 0; i < t->c1.link_sockets_num; i++) + { + if (!proto_is_dgram(t->c2.link_sockets[i]->info.proto)) + { + tcp = true; + break; + } + } + if (!tcp) + { + return; + } + + /* room for the tun device, the listeners, log, management, scripts... */ + const rlim_t needed = (rlim_t)t->options.max_clients + 64; + + struct rlimit rl; + if (getrlimit(RLIMIT_NOFILE, &rl) < 0) + { + msg(M_WARN | M_ERRNO, "WARNING: getrlimit(RLIMIT_NOFILE) failed"); + return; + } + if (rl.rlim_cur >= needed) + { + return; + } + + const rlim_t old = rl.rlim_cur; + rl.rlim_cur = rl.rlim_max < needed ? rl.rlim_max : needed; + if (rl.rlim_cur > old) + { + if (setrlimit(RLIMIT_NOFILE, &rl) == 0) + { + msg(M_INFO, "NOTE: raised the open files limit from %lu to %lu", (unsigned long)old, + (unsigned long)rl.rlim_cur); + } + else + { + msg(M_WARN | M_ERRNO, "WARNING: setrlimit(RLIMIT_NOFILE) failed"); + rl.rlim_cur = old; + } + } + if (rl.rlim_cur < needed) + { + msg(M_WARN, + "WARNING: the open files limit (soft %lu, hard %lu) is too low for " + "--max-clients %" PRIu32 ", TCP clients may be refused before --max-clients is reached", + (unsigned long)rl.rlim_cur, (unsigned long)rl.rlim_max, t->options.max_clients); + } +#endif /* if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) && !defined(TARGET_DARWIN) */ +} + +/* * Main initialization function, init multi_context object. */ static void @@ -398,6 +460,7 @@ * Per-client limits */ m->max_clients = t->options.max_clients; + multi_check_nofile_limit(t); m->instances = calloc(m->max_clients, sizeof(struct multi_instance *)); check_malloc_return(m->instances); _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
