fix(server): stop two remote-input paths from killing the server
Both bugs let a remote peer terminate the server process: the first needs no credentials at all, the second needs only a malicious client. 1) cpp-httplib 0.14.3 -- unauthenticated DoS via Content-Length Content-Length was parsed with strtoull, which accepts a leading sign, so "-1" wrapped around to ULLONG_MAX. read_content then treated that as an oversized body and called skip_content_with_length(strm, ULLONG_MAX), a loop that only exits after consuming 18 EB -- a client trickling bytes never trips the read timeout, so the worker thread is stuck forever. With ThreadPool(32) that is 32 connections to wedge the whole server. Body parsing runs before the MCP token check, so no token is required. Backported from upstream 0.51.0/0.64.0 as parse_content_length_strict(): only plain decimal digits are accepted and overflow is rejected. Upgrading the library instead is not possible -- 0.25.0+ requires _WIN32_WINNT >= 0x0A00 (Win10) while this project targets 0x0602 (Win8), and the last Win8-compatible release (0.20.0) predates the fix. Verified: negative, leading-plus, overflow and trailing-garbage values now all return 400; 40 concurrent "-1" requests are rejected in under 50 ms with the thread pool still healthy; plain and chunked requests are unaffected. 2) HideScreenSpyDlg::JPG_BMP -- malformed JPEG terminated the process The decoder was set up with libjpeg's default error_exit, which calls exit(EXIT_FAILURE). Since the input is a JPEG stream sent by the remote client, one structurally invalid frame killed the whole server. Replaced with a setjmp/longjmp handler that turns fatal errors into "return false", matching this function's existing failure paths. Warnings still go through emit_message, so truncated-but-recoverable images behave exactly as before. Verified against 36 malformed samples: 19 used to terminate the process, none do now (the same 19 return false), and valid JPEGs decode identically. 3) Drop the dead file_server.h An older HTTP file-serving implementation, superseded by WebService.cpp and referenced by nothing: no include anywhere, no instantiation, no entry in the vcxproj or any build script. Removing it keeps the real HTTP attack surface -- httplib's /mcp plus WebService -- unambiguous for anyone auditing this code.
This commit is contained in:
1 parent
8807321f1d
commit
4f9bfb0fdc
3 files changed
+88
-100
No files matched your search
@@ -4303,6 +4303,37 @@ inline bool read_content_with_length(Stream &strm, uint64_t len,
|
||||
return true;
|
||||
}
|
||||
|
||||
// [YAMA PATCH] Strict Content-Length parser.
|
||||
//
|
||||
// Do NOT use strtoull here: it accepts a leading sign, so "-1" wraps around to
|
||||
// ULLONG_MAX, and it does not report overflow. read_content then treats that as
|
||||
// an oversized body and calls skip_content_with_length(strm, ULLONG_MAX), which
|
||||
// loops until it has consumed 18 EB -- a client that keeps trickling bytes never
|
||||
// trips the read timeout, so the worker thread is stuck forever. With
|
||||
// ThreadPool(32) that is an unauthenticated DoS: 32 such connections wedge the
|
||||
// whole server. Ported from cpp-httplib 0.51.0/0.64.0 (is_invalid_content_length).
|
||||
// Only pure decimal digits are accepted, and the value must fit in uint64_t.
|
||||
inline bool parse_content_length_strict(const std::string &s, uint64_t &out)
|
||||
{
|
||||
// ULLONG_MAX has 20 digits; anything longer cannot be a valid length.
|
||||
if (s.empty() || s.size() > 20) {
|
||||
return false;
|
||||
}
|
||||
uint64_t v = 0;
|
||||
for (char c : s) {
|
||||
if (c < '0' || c > '9') {
|
||||
return false;
|
||||
}
|
||||
auto d = static_cast<uint64_t>(c - '0');
|
||||
if (v > ((std::numeric_limits<uint64_t>::max)() - d) / 10) {
|
||||
return false; // overflow
|
||||
}
|
||||
v = v * 10 + d;
|
||||
}
|
||||
out = v;
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void skip_content_with_length(Stream &strm, uint64_t len)
|
||||
{
|
||||
char buf[CPPHTTPLIB_RECV_BUFSIZ];
|
||||
@@ -4484,7 +4515,14 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status,
|
||||
} else if (!has_header(x.headers, "Content-Length")) {
|
||||
ret = read_content_without_length(strm, out);
|
||||
} else {
|
||||
auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0);
|
||||
// [YAMA PATCH] Reject malformed Content-Length before it reaches
|
||||
// skip_content_with_length. See parse_content_length_strict above.
|
||||
uint64_t len = 0;
|
||||
if (!parse_content_length_strict(
|
||||
x.get_header_value("Content-Length"), len)) {
|
||||
status = StatusCode::BadRequest_400;
|
||||
return false;
|
||||
}
|
||||
if (len > payload_max_length) {
|
||||
exceed_payload_max_length = true;
|
||||
skip_content_with_length(strm, len);
|
||||
|
||||
Reference in new issue
Block a user