1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2026-10-29 15:02:30 +00:00
Files
go-chasquid-smtp/test/stress-02-connections/run.sh
T
Alberto Bertogli 6b64c9ccba 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/.
2026-10-09 23:32:26 +01:00

37 lines
1002 B
Bash
Executable File

#!/bin/bash
set -e
. "$(dirname "$0")/../util/lib.sh"
init
generate_certs_for testserver
add_user user@testserver secretpassword
# Note we run the server with minimal logging, to avoid generating very large
# log files, which are not very useful anyway.
mkdir -p .logs
chasquid -v=-1 --logfile=.logs/chasquid.log --config_dir=config &
wait_until_ready 1025
echo "Peak RAM: $(chasquid_ram_peak)"
# 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 -Hn) / 2 - 20 ))
if [ $COUNT -gt 2000 ]; then
COUNT=2000
fi
if ! conngen -logtime -addr=localhost:1025 -count=$COUNT; then
tail -n 1 .logs/chasquid.log
fail "conngen error"
fi
echo "Peak RAM: $(chasquid_ram_peak)"
success