Fix: throttle TLS/WS handshake failure logs
Exposed ports see a constant stream of scanners and HTTP probes. Every TLS handshake attempt logged an ioctlsocket(FIONBIO=0) failure, but WSAEINVAL is the documented result once WSAEventSelect owns the listening socket and the code already falls back to select; that line alone accounted for 6678 of the 8970 "handshake" lines in a four-day log. It is now logged only for other error codes. A failed handshake used to print three or four lines (recv EOF, AcceptSecurityContext, final status, failed-for-IP) with no per-peer dedup, and WS upgrade failures printed once per HTTP probe. ServerTlsSession now records a human-readable reason (Schannel status name for ASC failures) instead of logging per connection; IOCPServer prints one line per peer per hour with the reason and a suppressed count, for both TLS and WS. Peer close_notify is a normal shutdown and is no longer logged; MCP TLS failures now carry the same reason. Failure details are copied before the context's I/O reference is released, so the log path cannot touch a context that another thread may recycle. No handshake, crypto or connection behaviour changes. Verified with a Debug x64 build and a live probe run: six failed connections to the device port produced exactly one TLS and one WS line, with no ioctlsocket lines. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
3d2dd08be5
commit
5ee59415f9
4 files changed
+176
-19
No files matched your search
@@ -650,6 +650,35 @@ static bool WaitSocketReady(SOCKET sock, bool forRead, int timeoutMs)
|
||||
return select(0, forRead ? &fds : NULL, forRead ? NULL : &fds, NULL, &tv) > 0;
|
||||
}
|
||||
|
||||
// Schannel 失败码 → 名字。日志里直接看 SEC_E_ALGORITHM_MISMATCH 比 0x80090331 有用。
|
||||
static const char* SchannelStatusName(SECURITY_STATUS sc)
|
||||
{
|
||||
switch (sc)
|
||||
{
|
||||
case SEC_E_INTERNAL_ERROR: return "SEC_E_INTERNAL_ERROR";
|
||||
case SEC_E_ALGORITHM_MISMATCH: return "SEC_E_ALGORITHM_MISMATCH";
|
||||
case SEC_E_UNTRUSTED_ROOT: return "SEC_E_UNTRUSTED_ROOT";
|
||||
case SEC_E_CERT_UNKNOWN: return "SEC_E_CERT_UNKNOWN";
|
||||
case SEC_E_CERT_EXPIRED: return "SEC_E_CERT_EXPIRED";
|
||||
case SEC_E_ILLEGAL_MESSAGE: return "SEC_E_ILLEGAL_MESSAGE";
|
||||
case SEC_E_UNSUPPORTED_FUNCTION: return "SEC_E_UNSUPPORTED_FUNCTION";
|
||||
case SEC_E_WRONG_PRINCIPAL: return "SEC_E_WRONG_PRINCIPAL";
|
||||
case SEC_E_NO_CREDENTIALS: return "SEC_E_NO_CREDENTIALS";
|
||||
case SEC_E_UNKNOWN_CREDENTIALS: return "SEC_E_UNKNOWN_CREDENTIALS";
|
||||
case SEC_E_INVALID_TOKEN: return "SEC_E_INVALID_TOKEN";
|
||||
case SEC_E_INVALID_HANDLE: return "SEC_E_INVALID_HANDLE";
|
||||
case SEC_E_ENCRYPT_FAILURE: return "SEC_E_ENCRYPT_FAILURE";
|
||||
case SEC_E_DECRYPT_FAILURE: return "SEC_E_DECRYPT_FAILURE";
|
||||
case SEC_E_MESSAGE_ALTERED: return "SEC_E_MESSAGE_ALTERED";
|
||||
case SEC_E_OUT_OF_SEQUENCE: return "SEC_E_OUT_OF_SEQUENCE";
|
||||
case SEC_E_BUFFER_TOO_SMALL: return "SEC_E_BUFFER_TOO_SMALL";
|
||||
case SEC_E_TIME_SKEW: return "SEC_E_TIME_SKEW";
|
||||
case SEC_E_QOP_NOT_SUPPORTED: return "SEC_E_QOP_NOT_SUPPORTED";
|
||||
case SEC_E_NO_AUTHENTICATING_AUTHORITY: return "SEC_E_NO_AUTHENTICATING_AUTHORITY";
|
||||
default: return "SEC_E_OTHER";
|
||||
}
|
||||
}
|
||||
|
||||
bool ServerTlsSession::SendAll(SOCKET sock, const void* data, int len)
|
||||
{
|
||||
const BYTE* p = (const BYTE*)data;
|
||||
@@ -679,6 +708,8 @@ bool ServerTlsSession::HandshakeWithCred(SOCKET sock, const BYTE* initial, int i
|
||||
CredHandle cred)
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(m_mtx);
|
||||
m_lastStatus = SEC_E_OK;
|
||||
m_lastError.clear();
|
||||
if (m_ctxValid)
|
||||
CloseHandles(); // 防御:会话应只握手一次
|
||||
|
||||
@@ -708,8 +739,9 @@ bool ServerTlsSession::HandshakeWithCred(SOCKET sock, const BYTE* initial, int i
|
||||
{
|
||||
if (!WaitSocketReady(sock, true, 15000))
|
||||
{
|
||||
r = SOCKET_ERROR;
|
||||
break; // 等可读超时
|
||||
m_lastStatus = SEC_E_INTERNAL_ERROR;
|
||||
m_lastError = "timeout waiting for handshake bytes";
|
||||
return false; // 等可读超时
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -717,7 +749,12 @@ bool ServerTlsSession::HandshakeWithCred(SOCKET sock, const BYTE* initial, int i
|
||||
}
|
||||
if (r <= 0)
|
||||
{
|
||||
Mprintf("[TLS] handshake recv failed/EOF (r=%d err=%d)\n", r, WSAGetLastError());
|
||||
// 失败原因交给调用方汇总成一行日志;这里不再逐连接打印。
|
||||
char msg[96];
|
||||
sprintf_s(msg, sizeof(msg), "peer closed mid-handshake (recv r=%d err=%d)",
|
||||
r, (int)WSAGetLastError());
|
||||
m_lastStatus = SEC_E_INTERNAL_ERROR;
|
||||
m_lastError = msg;
|
||||
return false;
|
||||
}
|
||||
buf.insert(buf.end(), (BYTE*)raw, (BYTE*)raw + r);
|
||||
@@ -739,13 +776,19 @@ bool ServerTlsSession::HandshakeWithCred(SOCKET sock, const BYTE* initial, int i
|
||||
}
|
||||
if (hs[0] != 0x16)
|
||||
{
|
||||
Mprintf("[TLS] handshake: first byte 0x%02x is not a handshake record; fail-closed\n", hs[0]);
|
||||
char msg[96];
|
||||
sprintf_s(msg, sizeof(msg), "first byte 0x%02x is not a TLS handshake record", hs[0]);
|
||||
m_lastStatus = SEC_E_ILLEGAL_MESSAGE;
|
||||
m_lastError = msg;
|
||||
return false;
|
||||
}
|
||||
const size_t recLen = 5 + (((size_t)hs[3] << 8) | (size_t)hs[4]);
|
||||
if (recLen > 18432) // TLS 记录上限 16384 + 允许的额外开销
|
||||
{
|
||||
Mprintf("[TLS] handshake: absurd record length %u; fail-closed\n", (unsigned)recLen);
|
||||
char msg[96];
|
||||
sprintf_s(msg, sizeof(msg), "absurd TLS record length %u", (unsigned)recLen);
|
||||
m_lastStatus = SEC_E_ILLEGAL_MESSAGE;
|
||||
m_lastError = msg;
|
||||
return false;
|
||||
}
|
||||
if (hs.size() >= recLen)
|
||||
@@ -777,6 +820,8 @@ bool ServerTlsSession::HandshakeWithCred(SOCKET sock, const BYTE* initial, int i
|
||||
if (!ok)
|
||||
{
|
||||
sc = SEC_E_INTERNAL_ERROR;
|
||||
m_lastStatus = sc;
|
||||
m_lastError = "send failed during handshake";
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -799,8 +844,11 @@ bool ServerTlsSession::HandshakeWithCred(SOCKET sock, const BYTE* initial, int i
|
||||
break;
|
||||
if (FAILED(sc) && sc != SEC_E_INCOMPLETE_MESSAGE && sc != SEC_I_INCOMPLETE_CREDENTIALS)
|
||||
{
|
||||
Mprintf("[TLS] AcceptSecurityContext failed 0x%08x (in=%u consumed? extra=%u)\n",
|
||||
(unsigned)sc, inBufs[0].cbBuffer, inBufs[1].BufferType == SECBUFFER_EXTRA ? inBufs[1].cbBuffer : 0);
|
||||
char msg[128];
|
||||
sprintf_s(msg, sizeof(msg), "AcceptSecurityContext 0x%08x (%s)",
|
||||
(unsigned)sc, SchannelStatusName(sc));
|
||||
m_lastStatus = sc;
|
||||
m_lastError = msg;
|
||||
break; // 真正的失败
|
||||
}
|
||||
|
||||
@@ -813,7 +861,15 @@ bool ServerTlsSession::HandshakeWithCred(SOCKET sock, const BYTE* initial, int i
|
||||
|
||||
if (sc != SEC_E_OK)
|
||||
{
|
||||
Mprintf("[TLS] handshake failed: final status 0x%08x (fail-closed)\n", (unsigned)sc);
|
||||
// RecvMore/AcceptSecurityContext 已记录更具体的原因;仅在没有时补一条通用描述。
|
||||
if (m_lastStatus == SEC_E_OK)
|
||||
{
|
||||
char msg[128];
|
||||
sprintf_s(msg, sizeof(msg), "handshake failed 0x%08x (%s)",
|
||||
(unsigned)sc, SchannelStatusName(sc));
|
||||
m_lastStatus = sc;
|
||||
m_lastError = msg;
|
||||
}
|
||||
if (ctxValid)
|
||||
DeleteSecurityContext(&ctx);
|
||||
return false;
|
||||
@@ -828,6 +884,8 @@ bool ServerTlsSession::HandshakeWithCred(SOCKET sock, const BYTE* initial, int i
|
||||
SecPkgContext_StreamSizes sizes = {};
|
||||
if (QueryContextAttributes(&ctx, SECPKG_ATTR_STREAM_SIZES, &sizes) != SEC_E_OK)
|
||||
{
|
||||
m_lastStatus = SEC_E_INTERNAL_ERROR;
|
||||
m_lastError = "QueryContextAttributes(STREAM_SIZES) failed";
|
||||
DeleteSecurityContext(&ctx);
|
||||
return false;
|
||||
}
|
||||
@@ -866,7 +924,7 @@ int ServerTlsSession::Decrypt(const BYTE* cipher, int len, std::vector<BYTE>& pl
|
||||
if (sc == SEC_E_INCOMPLETE_MESSAGE)
|
||||
break; // 记录不完整,等下一轮 WSARecv
|
||||
if (sc == SEC_I_CONTEXT_EXPIRED) {
|
||||
Mprintf("[TLS] DecryptMessage: peer close_notify (SEC_I_CONTEXT_EXPIRED)\n");
|
||||
// 对端 close_notify:正常的 TLS 关闭,不是错误,不记日志。
|
||||
return -1; // 对端 close_notify / 会话结束
|
||||
}
|
||||
if (sc == SEC_I_RENEGOTIATE || FAILED(sc)) {
|
||||
|
||||
Reference in new issue
Block a user