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
+42
-8
@@ -4,6 +4,8 @@
|
||||
#include "SpamAssassinFilter.h"
|
||||
#include "File.h"
|
||||
#include <thread>
|
||||
#include <sys/stat.h>
|
||||
#include "SMTPSyntax.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
@@ -22,6 +24,7 @@ namespace mail {
|
||||
commands.add(_smtp_quit, "QUIT");
|
||||
commands.add(_smtp_rcpt, "RCPT");
|
||||
commands.add(_smtp_rset, "RSET");
|
||||
commands.add(_smtp_starttls, "STARTTLS");
|
||||
commands.add(_smtp_vrfy, "VRFY");
|
||||
if (watchQueue) {
|
||||
processExisting();
|
||||
@@ -37,8 +40,31 @@ namespace mail {
|
||||
out << "252 " << errorString << CRLF;
|
||||
}
|
||||
|
||||
bool SMTPServer::isLocalDomain(const std::string &domain) {
|
||||
if (!SMTPSyntax::safePathComponent(domain))
|
||||
return false;
|
||||
struct stat st;
|
||||
std::string path = mailFileSystem.getMailPath().str() + "/" + domain;
|
||||
return stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
|
||||
}
|
||||
|
||||
std::string SMTPServer::postmasterAddress() {
|
||||
std::string domain = SMTPSyntax::lower(hostName);
|
||||
while (true) {
|
||||
coreutils::MString candidate;
|
||||
candidate << "postmaster@" << domain.c_str();
|
||||
if (isLocalDomain(domain) &&
|
||||
(mailFileSystem.ifMailBoxExists(candidate) || mailFileSystem.ifAliasExists(candidate)))
|
||||
return "postmaster@" + domain;
|
||||
size_t dot = domain.find('.');
|
||||
if (dot == std::string::npos || domain.find('.', dot + 1) == std::string::npos)
|
||||
return "";
|
||||
domain = domain.substr(dot + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void SMTPServer::deliverToMailbox(coreutils::ZString &mailbox, coreutils::ZString &messageId,
|
||||
SpamAssassinFilter::Result &scan) {
|
||||
SpamAssassinFilter::Result &scan, const std::string &sender) {
|
||||
// The destination folder has to be picked AFTER the scan runs,
|
||||
// not before - this used to always build an "/Inbox/" path
|
||||
// regardless of what spamd found, so a correctly-flagged spam
|
||||
@@ -54,6 +80,8 @@ namespace mail {
|
||||
<< ((scan.ok && scan.isSpam) ? "/Spam/" : "/Inbox/") << messageId;
|
||||
|
||||
coreutils::File mailFile(path, O_CREAT | O_WRONLY, 0660);
|
||||
std::string returnPath = "Return-Path: <" + sender + ">\r\n";
|
||||
mailFile.write(returnPath);
|
||||
mailFile.write(scan.scanned);
|
||||
|
||||
coreutils::Log(coreutils::LOG_INFO) << "Message " << messageId << " delivered to recipient " << mailbox
|
||||
@@ -67,13 +95,19 @@ namespace mail {
|
||||
// __SMTP_DATA::queueMail()'s own doc comment for why the sender
|
||||
// had to be added alongside the recipient.
|
||||
if(name.getList().size() > 2) {
|
||||
bool exists = mailFileSystem.ifMailBoxExists(name[1]);
|
||||
// Recipient and sender are %-encoded in the file name - see
|
||||
// SMTPSyntax::encodeQueueField(). Decoding a name written before
|
||||
// that change is a no-op unless it happened to contain "%".
|
||||
std::string senderAddress = SMTPSyntax::decodeQueueField(name[2].str());
|
||||
std::string recipientString = SMTPSyntax::decodeQueueField(name[1].str());
|
||||
coreutils::MString recipientName(recipientString);
|
||||
bool exists = mailFileSystem.ifMailBoxExists(recipientName);
|
||||
// An alias is a plain file (not a directory) at the same
|
||||
// level a mailbox would be - see MailFileSystem's own doc
|
||||
// comment on createAlias()/ifAliasExists(). Only checked when
|
||||
// it isn't a real mailbox, so a mailbox and an alias can never
|
||||
// both match the same address.
|
||||
bool isAlias = !exists && mailFileSystem.ifAliasExists(name[1]);
|
||||
bool isAlias = !exists && mailFileSystem.ifAliasExists(recipientName);
|
||||
coreutils::MString from;
|
||||
from << mailFileSystem.getQueuePath() << "/" << name;
|
||||
if(exists || isAlias) {
|
||||
@@ -93,11 +127,11 @@ namespace mail {
|
||||
// One shared scan result delivered to every target - an
|
||||
// alias should carry one spam verdict for the message, not
|
||||
// a separately re-scanned copy per target.
|
||||
std::vector<coreutils::MString> targets = mailFileSystem.getAliasMailboxes(name[1]);
|
||||
std::vector<coreutils::MString> targets = mailFileSystem.getAliasMailboxes(recipientName);
|
||||
for (auto &target : targets)
|
||||
deliverToMailbox(target, name[0], scan);
|
||||
deliverToMailbox(target, name[0], scan, senderAddress);
|
||||
} else {
|
||||
deliverToMailbox(name[1], name[0], scan);
|
||||
deliverToMailbox(recipientName, name[0], scan, senderAddress);
|
||||
}
|
||||
|
||||
int rc = unlink(from.c_str());
|
||||
@@ -108,8 +142,8 @@ namespace mail {
|
||||
// owns the heap-allocated SendMail itself and runs it on its
|
||||
// own detached thread rather than blocking this EPoll
|
||||
// worker thread for however long the remote host takes.
|
||||
coreutils::MString sender(name[2]);
|
||||
coreutils::MString recipient(name[1]);
|
||||
coreutils::MString sender(senderAddress);
|
||||
coreutils::MString recipient(recipientString);
|
||||
coreutils::File queuedFile(from, O_RDONLY);
|
||||
queuedFile.read();
|
||||
coreutils::MString mailData(queuedFile.asString());
|
||||
|
||||
Reference in new issue
Block a user