Thread/EPoll lifecycle: - Thread::~Thread() now actually joins/frees its worker thread (was empty - the OS thread kept running against already-freed EPoll/Socket objects on any early teardown, a confirmed "double free" crash) - EPoll::~EPoll() calls stop() if never explicitly stopped, for the same reason - epoll_wait() now uses a 1s timeout instead of blocking forever, so a worker thread can actually notice isStopping() and let itself be joined - EPoll::processCommand() iterated threads by value, silently double-freeing the real vector's Thread the moment each copy went out of scope - ThreadScope gained onThreadStart()/onThreadEnd() hooks (actually wired up now - the constructor that was supposed to store the scope discarded it) for per-thread C-library init (e.g. MySQL client) Socket/TCPSocket/TLS: - Socket::writeSocket(): give up and shut down a connection after 50 consecutive zero-progress sends instead of retrying forever - SSL_ERROR_WANT_READ on a write needs a READ to make progress, but resetSocket() re-arms EPOLLWRNORM regardless, which an already-writable TCP socket satisfies immediately - a real stuck connection generated 55GB, then 12.9GB of debug log this way before being caught - Socket::writeSocket() now honors sendData()'s actual return value (previously discarded, entry popped unconditionally after one call) - a large response could get silently truncated past what fit in one write()/SSL_write() call - TLS::registerSocket()/sendData()/receiveData(): `initialized` now only goes true once SSL_set_fd() has actually succeeded, and sendData()/receiveData() check it first - fixes a real SIGSEGV where a new session's own queued greeting could reach SSL_write() on a different epoll thread before this session's handshake setup had run - TCPServer::connectionRequest() now performs the TLS handshake ON THE ACCEPTED SESSION, not the listening server itself (was building SSL state on the wrong object, leaving every session's own `ssl` uninitialized) - TCPServer::receiveData() always uses the plain (non-TLS) read path - the listening socket is never itself a TLS peer - TCPSocket's block-mode literal reading used an unset `blockLength` field instead of `blockSize`, and could busy-spin forever on a multi-packet literal - TCPSocket line scanning replaced an unbounded strcspn() (assumes a NUL-terminated buffer; read past a non-NUL-terminated receive buffer into adjacent heap memory) with a bounds-checked memchr() scan Protocol/session handling: - CommandList::processRequest() now case-folds the command keyword before lookup - SMTP/POP3/IMAP commands are spec-required to be case-insensitive; a client sending "Mail From:" instead of "MAIL FROM:" previously failed to dispatch at all - TCPSession::protocol() no longer drops blank lines while a handler has grabbed input (e.g. __SMTP_DATA reading a message body, an IMAP APPEND literal) - those handlers depend on seeing blank lines as real content, not commands; this silently corrupted any message body containing a blank line - TCPSession's uuid alias fixed: streaming a raw uuid_t via operator<< triggered libstdc++'s unsigned-char* overload (strlen() on 16 binary bytes, a real stack-buffer-overflow read), and `alias` pointed at a stack-local that was already destroyed by the time the constructor returned - INotify::processEvent() only treats event->name as a NUL-terminated string when event->len > 0 (per inotify(7)) instead of unconditional strlen() on a field the kernel doesn't guarantee is even present - a confirmed heap-buffer-overflow read that had been misdiagnosed as a MySQL thread-safety issue
104 lines
3.8 KiB
C++
104 lines
3.8 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() {
|
|
if(out.tellp() > 0)
|
|
write(out.str());
|
|
out.str("");
|
|
}
|
|
|
|
void TCPSession::terminate() {
|
|
term = true;
|
|
}
|
|
|
|
}
|