STARTTLS (in-session TLS upgrade, RFC 3207 / 2595 style): - TCPServer::enableStartTLS(TLSInfo&) builds startTLSContext for a plain-text listener; TCPSession::startTLS(ctx) sends the queued ready reply in clear (Socket::flushPlain), discards any input pipelined after the command (CVE-2011-0411 injection class), then switches the socket to TLS and starts the handshake. - TLS::buildContext() is static and runs process-wide OpenSSL init exactly once, so it can be called from createContext(), addSNIContext() and enableStartTLS(); frees a half-built context on error; SSL_CTX_use_PrivateKey_file() failure is detected (<= 0). - ~TLS() frees the per-connection SSL object (leaked one per TLS connection before). TLS receive stall: - TCPSocket::receiveData() drains SSL_pending() before returning. A TLS record holds up to 16KB but reads are 4KB; with EPOLLET the rest of a record sat inside OpenSSL with no event to wake it, stalling large uploads (e.g. SMTP DATA) mid-stream. Line framing (opt-in, defaults unchanged): - TCPSocket::strictCRLF: only CR LF ends a line. SMTP needs this: accepting bare CR/LF as line ends is what makes SMTP smuggling (CVE-2023-51764/51765/51766) possible, and the lenient mode also split a CR LF straddling two reads into two lines, inserting a blank line into message bodies. - TCPSocket::maxLineLength + onLineTooLong(): bound the line buffer so a peer that never sends a terminator can't exhaust memory. Races that silently dropped connections (EPOLLONESHOT event delivered before the object was registered, so Thread::run() ignored it and the one-shot registration was spent): - TCPServer constructor binds, listens and registers the listener before setDescriptor() arms it (about 3 startups in 8 accepted nothing, ever). - TCPServer::accept() registers the session (sessions + registerLive) and, for implicit TLS, creates its SSL object before arming the fd (10-15% of a 40-connection burst never got a greeting). A failed ::accept() no longer creates and leaks a session. Duplicate output: - TCPSession::send() clears `out` before write() re-arms the socket; a handler running on another worker in that window re-sent the previous text (clients saw the greeting twice and every later reply shifted by one). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0121tAhQVe4qbCwYgVjKQS4B
122 lines
4.6 KiB
C++
122 lines
4.6 KiB
C++
#include "TCPSession.h"
|
|
#include "Exception.h"
|
|
#include "Log.h"
|
|
#include "TCPServer.h"
|
|
#include "uuid/uuid.h"
|
|
|
|
namespace core {
|
|
|
|
TCPSession::TCPSession(EPoll &ePoll, std::string text) : TCPSocket(ePoll, text) {
|
|
// uuid_t is 16 raw binary bytes, not a NUL-terminated string -
|
|
// streaming it directly (the old code did `<< uuidObject`) hits
|
|
// libstdc++'s unsigned-char* operator<< overload, which treats its
|
|
// argument as a C-string and calls strlen() on it: a real
|
|
// stack-buffer-overflow read past the 16-byte local, confirmed via
|
|
// AddressSanitizer under concurrent load. Separately, `alias` was
|
|
// being pointed at `uuidObject` itself - a stack-local array that is
|
|
// destroyed the instant this constructor returns, leaving `alias` a
|
|
// dangling pointer for the rest of the session's life (used by
|
|
// compareAlias() for session identity). uuid_unparse() into the
|
|
// `uuid` member (declared for exactly this - see its doc comment)
|
|
// fixes both: a real NUL-terminated string safe to log, backed by
|
|
// storage that lives as long as the TCPSession does.
|
|
uuid_t uuidObject;
|
|
uuid_generate(uuidObject);
|
|
uuid_unparse(uuidObject, uuid);
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << uuid;
|
|
alias = (void *)uuid;
|
|
}
|
|
|
|
TCPSession::TCPSession(EPoll &ePoll, TCPServer *server, std::string text) : TCPSocket(ePoll, server->ctx, text), server(server) {
|
|
uuid_t uuidObject;
|
|
uuid_generate(uuidObject);
|
|
uuid_unparse(uuidObject, uuid);
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << uuid;
|
|
alias = (void *)uuid;
|
|
}
|
|
|
|
TCPSession::~TCPSession() {
|
|
server->removeFromSessionList(this);
|
|
server->subscriptions.removeSessionSubscriptions(*this);
|
|
}
|
|
|
|
void TCPSession::output(std::stringstream &data) {
|
|
data << "|" << ipAddress.getClientAddressAndPort();
|
|
}
|
|
|
|
void TCPSession::protocol(coreutils::ZString &data) {
|
|
// The data.getLength() != 0 guard alone silently swallowed every
|
|
// blank line a client sent, before it ever reached
|
|
// CommandList::processRequest() - fine for ordinary command
|
|
// dispatch (a stray blank line between commands isn't a command),
|
|
// but CommandList::processRequest() already special-cases
|
|
// session.grab != NULL to hand the line to the grabbed handler
|
|
// unconditionally, blank or not - a handler mid-grabInput() (e.g.
|
|
// __SMTP_DATA reading a message body, or an IMAP APPEND literal)
|
|
// depends on seeing blank lines as real content, not commands.
|
|
// With the outer guard unconditional, every message body's
|
|
// header/body separator - and every blank line within the body
|
|
// itself - was getting dropped before that handler ever saw it.
|
|
// Confirmed via a delivered test message with zero CRLFCRLF
|
|
// sequences anywhere in it, which is what first exposed this.
|
|
if (grab != NULL || data.getLength() != 0) {
|
|
if (!server->commands.processRequest(data, *this)) {
|
|
coreutils::Log(coreutils::LOG_DEBUG_1) << "Received data could not be parsed: " << data.str();
|
|
}
|
|
}
|
|
}
|
|
|
|
bool TCPSession::compareAlias(void *alias) {
|
|
return this->alias = alias;
|
|
}
|
|
|
|
void TCPSession::outputAlias(std::stringstream &out) {
|
|
out << alias;
|
|
}
|
|
|
|
void TCPSession::onRegistered() {
|
|
onConnected();
|
|
send();
|
|
if (term)
|
|
shutdown("termination requested");
|
|
}
|
|
|
|
void TCPSession::onConnected() {}
|
|
|
|
void TCPSession::onLineReceived(coreutils::ZString &line) {
|
|
protocol(line);
|
|
send();
|
|
if (term)
|
|
shutdown("termination requested");
|
|
}
|
|
|
|
void TCPSession::send() {
|
|
// Take the data and clear `out` BEFORE write(). write() can re-arm
|
|
// the fd, and from that moment another epoll worker may send what
|
|
// was queued, receive the client's next command and run its handler
|
|
// - which appends to this same `out`. When `out` was only cleared
|
|
// after write() returned, a handler running in that window saw the
|
|
// previous text still there and sent it a second time. Reproduced
|
|
// with the protocol greeting (queued from onRegistered() on the
|
|
// accepting thread): under a burst of connections some clients got
|
|
// "220 ..." twice, every later reply then lined up with the wrong
|
|
// command, and STARTTLS clients read the plain "220 Ready" as their
|
|
// TLS ServerHello.
|
|
if(out.tellp() > 0) {
|
|
std::string data = out.str();
|
|
out.str("");
|
|
write(data);
|
|
}
|
|
}
|
|
|
|
bool TCPSession::startTLS(SSL_CTX *ctx) {
|
|
send();
|
|
return TCPSocket::startTLS(ctx);
|
|
}
|
|
|
|
void TCPSession::terminate() {
|
|
term = true;
|
|
}
|
|
|
|
}
|