1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-28 15:02:28 +00:00

smtpsrv: Set a limit on concurrent SMTP connections

If chasquid runs out of file descriptors, that can cause problems
accepting connections, writing to the queue, and even reloading configs.
Some of those errors are fatal, as they should not happen under normal
conditions.

Since a remote client can open a lot of connections, this can end up
consuming all file descriptors, and resulting in a denial of service. To
reduce the impact of that kind of event, this patch makes chasquid limit
the number of concurrent SMTP connections to half the maximum number of
file descriptors.

Connections over that limit will get closed immediately. The server
will log (rate-limited) when such event happens, and new metrics
make these events visible through monitoring too.

This approach complements the previous patch, which made the server
retry Accept errors, to reduce the impact of connection overloads.

This mainly affects deployments with low file descriptor limits; most
modern deployments have very high limits by default, so they are
unlikely to experience this problem in the first place.

Users who have to be very defensive can deploy OS-level safeguards (such
as firewall rules to limit number of connections, etc.) which are more
effective and tunable.

Thanks to John JEAN <john@wargan.com> for finding and reporting this
problem. The detailed report can be found in
https://johnjean.io/advisories/chasquid-unauthenticated-remote-dos/.
This commit is contained in:
Alberto Bertogli
2026-10-09 23:25:42 +01:00
parent 6dc05d2aad
commit 6b64c9ccba
8 changed files with 266 additions and 46 deletions
+5 -4
View File
@@ -16,11 +16,12 @@ wait_until_ready 1025
echo "Peak RAM: $(chasquid_ram_peak)"
# Set connection count to (max open files) - (leeway).
# We set the leeway to account for file descriptors opened by the runtime and
# listeners; 20 should be enough for now.
# Set connection count to (connection limit) - (leeway).
# chasquid limits incoming connections to half of its file descriptor limit.
# Go raises the soft limit to (almost) the hard limit at startup, so we use
# the hard limit, with some leeway; 20 should be enough for now.
# Cap it to 2000, as otherwise it can be problematic due to port availability.
COUNT=$(( $(ulimit -n) - 20 ))
COUNT=$(( $(ulimit -Hn) / 2 - 20 ))
if [ $COUNT -gt 2000 ]; then
COUNT=2000
fi