SMTP: STARTTLS, smuggling fix, and RFC 5321 conformance
Security:
- SMTP smuggling (CVE-2023-51764 family): only <CRLF>.<CRLF> ends DATA
(strict CRLF framing); bare CR/LF in the text is stored as CR LF.
Before, '<LF>.<LF>' and four other variants ended the message and the
rest ran as fresh commands - a second, forged message was delivered.
- STARTTLS on port 25 (RFC 3207), using the configured tls cert; AUTH
is only offered/accepted once encrypted (538 5.7.11 otherwise).
- The 250 reply no longer discloses the queue file's full path.
- Queue file names %-encode the addresses ('/' or '|' in an address
broke the name or pointed it into another directory).
Conformance (RFC 5321, 1870, 2034, 2920, 3207, 3463, 3848, 4616, 4954,
6152):
- 500 for unknown/empty/over-long commands (no reply at all before -
clients hung); 501 for missing/invalid arguments (bare MAIL/RCPT/HELO
were out-of-bounds reads).
- HELO/EHLO required before MAIL; nested MAIL refused; EHLO/HELO/RSET/
end-of-DATA reset the transaction.
- MAIL: SIZE= (552 when over the limit), BODY=7BIT|8BITMIME, AUTH=;
555 for anything else; case-insensitive keywords; address syntax
checked.
- RCPT: postmaster (bare and any case) accepted; case-insensitive
domain and local-part; 550 5.1.1 unknown user vs 550 5.7.1 relay
denied; aliases accepted (they were rejected at RCPT before); 1000
recipient cap (452 4.5.3).
- ENHANCEDSTATUSCODES advertised and used on every reply.
- AUTH PLAIN added alongside LOGIN; strict base64; 535 5.7.8 on
failure; 503 before EHLO, during a transaction, or when already
authenticated (the last one sent no reply before).
- Received: per RFC 5321 4.4 with client IP, protocol per RFC 3848
(ESMTP/ESMTPS/ESMTPSA) and queue id; Return-Path: added at local
delivery.
tests/smtp_conformance.py: 112 checks. origin/master passes 37 of the
72 it can reach before the server stops answering; this passes 112/112.
tests/imap_pop3_regression.py: IMAPS/POP3S unaffected (3/3).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0121tAhQVe4qbCwYgVjKQS4B
This commit is contained in:
1 parent
407cc2e915
commit
d3d7e5f3e5
27 files changed
+1821
-358
No files matched your search
+74
-45
@@ -1,58 +1,87 @@
|
||||
#include "__SMTP_MAIL.h"
|
||||
#include "SMTPServer.h"
|
||||
#include "SPFChecker.h"
|
||||
#include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
int __SMTP_MAIL::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
|
||||
if(request[1].ifNext("FROM:")) {
|
||||
request[1].skipWhitespace();
|
||||
if(request[1].ifNext("<")) {
|
||||
session.from = request[1].getTokenExclude(">");
|
||||
// Deliberately unconditional - not gated on authState. A
|
||||
// receiving MX server accepts MAIL FROM addressed to its own
|
||||
// local mailboxes from anyone; sending MTAs never AUTH to a
|
||||
// destination domain, only to their own outbound smarthost.
|
||||
// Authentication only needs to gate relaying to a NON-local
|
||||
// recipient, which __SMTP_RCPT.cpp already does via
|
||||
// session.relay. This used to be
|
||||
// `if(session.authState = USER_KNOWN)` - an assignment, not
|
||||
// a comparison - which accidentally produced this same
|
||||
// "always accept" behavior; a literal `==` fix would instead
|
||||
// have started rejecting all normal inbound mail.
|
||||
session.out << "250 OK" << CRLF;
|
||||
session.recipientList.clear();
|
||||
session.state = MAIL;
|
||||
// RFC 5321 4.1.4: a client MUST say HELO/EHLO before a transaction,
|
||||
// and MAIL may not be issued again while one is open.
|
||||
if (!session.greeted) {
|
||||
session.out << "503 5.5.1 Send HELO/EHLO first" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
if (session.state == MAIL || session.state == RCPT) {
|
||||
session.out << "503 5.5.1 Nested MAIL command" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// RFC 7208 2.4: the identity to check is the MAIL FROM
|
||||
// address's own domain, or - for the null reverse-path <>
|
||||
// bounce messages use, which has no domain of its own - the
|
||||
// HELO/EHLO domain instead. Computed here (not deferred to
|
||||
// DATA) since MAIL FROM is where the RFC actually anchors the
|
||||
// check; DATA just reuses the result via session.spfHeader
|
||||
// rather than re-running the DNS lookups a second time. This
|
||||
// does block this connection's EPoll worker thread for
|
||||
// however long the SPF DNS lookups take (matching
|
||||
// SendMail::lookupMX()'s own res_query() calls, which are
|
||||
// synchronous the same way) - acceptable for a well-behaved
|
||||
// sender domain, not for a slow/hostile one, same tradeoff
|
||||
// already accepted elsewhere in this codebase rather than a
|
||||
// new one introduced here.
|
||||
std::string fromStr = session.from.str();
|
||||
size_t at = fromStr.find_last_of('@');
|
||||
std::string spfDomain = (at == std::string::npos) ? session.clientDomainName.str() : fromStr.substr(at + 1);
|
||||
std::string clientIP = session.ipAddress.getClientAddress();
|
||||
session.spfDomain = spfDomain;
|
||||
session.spfResult = SPFChecker::check(spfDomain, clientIP);
|
||||
session.spfHeader = SPFChecker::receivedSPFHeader(session.spfResult, spfDomain, clientIP,
|
||||
session.clientDomainName.str(), server.hostName);
|
||||
} else
|
||||
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
|
||||
} else
|
||||
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
|
||||
SMTPSyntax::PathArgument arg = SMTPSyntax::parsePath(SMTPSyntax::argument(line(request)), "FROM:", true);
|
||||
if (!arg.ok) {
|
||||
session.out << arg.reply << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Service-extension parameters (RFC 5321 4.1.1.11) - only after
|
||||
// EHLO, and only the ones this server advertises.
|
||||
for (auto ¶m : arg.params) {
|
||||
if (!session.esmtp) {
|
||||
session.out << "555 5.5.4 MAIL parameters require EHLO" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
if (param.first == "SIZE") {
|
||||
// RFC 1870 6.1: refuse up front if the declared size is too big.
|
||||
const std::string &v = param.second;
|
||||
if (v.empty() || v.size() > 20 || v.find_first_not_of("0123456789") != std::string::npos) {
|
||||
session.out << "501 5.5.4 Syntax: SIZE=<number>" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
if (v.size() > 18 || std::stoull(v) > SMTPServer::maxMessageSize) {
|
||||
session.out << "552 5.3.4 Message size exceeds fixed maximum message size" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
} else if (param.first == "BODY") {
|
||||
// RFC 6152: 7BIT and 8BITMIME. BINARYMIME needs CHUNKING,
|
||||
// which isn't offered.
|
||||
std::string v = SMTPSyntax::upper(param.second);
|
||||
if (v != "7BIT" && v != "8BITMIME") {
|
||||
session.out << "555 5.5.4 Unsupported BODY type" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
} else if (param.first == "AUTH") {
|
||||
// RFC 4954 5: accepted; the identity it carries is only
|
||||
// trusted from an authenticated relay, which this isn't
|
||||
// acting as - nothing to record.
|
||||
} else {
|
||||
session.out << "555 5.5.4 Unsupported parameter " << param.first.c_str() << CRLF;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
session.from = arg.address.c_str();
|
||||
session.recipientList.clear();
|
||||
session.state = MAIL;
|
||||
|
||||
// Deliberately not gated on authentication: a receiving MX accepts
|
||||
// MAIL from anyone. Authentication only gates relaying to a
|
||||
// non-local recipient, which __SMTP_RCPT decides.
|
||||
//
|
||||
// SPF (RFC 7208 2.4): the identity is the MAIL FROM domain, or for
|
||||
// the null reverse-path <> the HELO/EHLO domain. Computed here,
|
||||
// where the RFC anchors it, and reused at DATA via spfHeader.
|
||||
std::string fromStr = session.from.str();
|
||||
size_t at = fromStr.find_last_of('@');
|
||||
std::string spfDomain = (at == std::string::npos) ? session.clientDomainName.str() : fromStr.substr(at + 1);
|
||||
std::string clientIP = session.ipAddress.getClientAddress();
|
||||
session.spfDomain = spfDomain;
|
||||
session.spfResult = SPFChecker::check(spfDomain, clientIP);
|
||||
session.spfHeader = SPFChecker::receivedSPFHeader(session.spfResult, spfDomain, clientIP,
|
||||
session.clientDomainName.str(), server.hostName);
|
||||
|
||||
session.out << "250 2.1.0 Sender OK" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user