From 57e1a1b19422d3a47647d2efcb3e032bf117b03a Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Thu, 8 Oct 2026 18:27:39 -0400 Subject: [PATCH] fix: bound the HTTP camera Content-Length and Buffer growth refs GHSA-x959-xrc9-89p8 RemoteCameraHttp read the camera's Content-Length with atoi() and then asked ReadData() for that many bytes, which passes the count straight to Buffer::read_into(). Buffer::expand() worked in int: once the allocation passed INT_MAX the spare space went negative, it grew again, and mAllocation + count wrapped to a small value, so the following memcpy or read() wrote the camera's data past a small heap block. Whoever answers the camera's HTTP request, often on a plain-HTTP link, could crash zmc in a loop or corrupt its heap. - RemoteCameraHttp::ParseContentLength() replaces the four atoi() calls. It refuses a length that is not a number or is larger than twice the monitor's raw frame size plus one network buffer (capped at 1 GiB), and GetResponse() fails the response instead of buffering it. - Buffer::expand() does its arithmetic in size_t, grows to exactly what is needed, and refuses to hold more than INT_MAX bytes, since read_into() returns the count as an int. - Buffer::read_into() refuses a read that does not fit after expand() rather than reading past the allocation. tests/zm_buffer.cpp covers reads through a pipe, an oversized read, data kept across a shift and a grow, and the Content-Length bounds. Co-Authored-By: Claude Opus 5.5 --- src/zm_buffer.cpp | 57 +++++++++++-------- src/zm_buffer.h | 4 ++ src/zm_remote_camera_http.cpp | 42 ++++++++++++-- src/zm_remote_camera_http.h | 4 ++ tests/CMakeLists.txt | 1 + tests/zm_buffer.cpp | 100 ++++++++++++++++++++++++++++++++++ 6 files changed, 180 insertions(+), 28 deletions(-) create mode 100644 tests/zm_buffer.cpp diff --git a/src/zm_buffer.cpp b/src/zm_buffer.cpp index 85d415498a..1ba7f5c34a 100644 --- a/src/zm_buffer.cpp +++ b/src/zm_buffer.cpp @@ -35,38 +35,47 @@ unsigned int Buffer::assign(const unsigned char *pStorage, unsigned int pSize) { } unsigned int Buffer::expand(unsigned int count) { - int spare = mAllocation - mSize; - int headSpace = mHead - mStorage; - int tailSpace = spare - headSpace; - int width = mTail - mHead; - if ( spare >= static_cast(count) ) { - // There is enough space in the allocation might need to shift everything over though - // - if ( tailSpace < static_cast(count) ) { - // if there is extra space at the head, shift everything over - memmove(mStorage, mHead, mSize); - mHead = mStorage; - mTail = mHead + width; - } - } else { - mAllocation += count; - unsigned char *newStorage = new unsigned char[mAllocation]; - if ( mStorage ) { - memcpy(newStorage, mHead, mSize); - delete[] mStorage; - } else { - memset(newStorage, 0, mAllocation); - } - mStorage = newStorage; + // Sizes are worked out in size_t: count can come from a network peer (a camera's + // Content-Length), and the int arithmetic this used to do wrapped once the allocation + // passed INT_MAX, leaving a small block behind. refs GHSA-x959-xrc9-89p8 + const size_t head_space = mHead - mStorage; + const size_t tail_space = static_cast(mAllocation) - head_space - mSize; + if (tail_space >= count) return mSize; + + if (static_cast(mAllocation) - mSize >= count) { + // There is enough space in the allocation once the data is shifted to the front + memmove(mStorage, mHead, mSize); mHead = mStorage; - mTail = mHead + width; + mTail = mHead + mSize; + return mSize; } + + const size_t needed = static_cast(mSize) + count; + if (needed > kMaxAllocation) { + Error("Refusing to grow buffer of %u bytes by %u bytes", mSize, count); + return mSize; + } + unsigned char *newStorage = new unsigned char[needed]; + if (mStorage) { + memcpy(newStorage, mHead, mSize); + delete[] mStorage; + } else { + memset(newStorage, 0, needed); + } + mAllocation = static_cast(needed); + mStorage = newStorage; + mHead = mStorage; + mTail = mHead + mSize; return mSize; } int Buffer::read_into(int sd, unsigned int bytes) { // Make sure there is enough space this->expand(bytes); + if (static_cast(mStorage + mAllocation - mTail) < bytes) { + Error("No room to read %u bytes into buffer of %u bytes", bytes, mSize); + return -1; + } Debug(3, "Reading %u bytes", bytes); int bytes_read = ::read(sd, mTail, bytes); if (bytes_read > 0) { diff --git a/src/zm_buffer.h b/src/zm_buffer.h index 0407794b0e..67a2d70ce4 100644 --- a/src/zm_buffer.h +++ b/src/zm_buffer.h @@ -22,6 +22,7 @@ #include "zm_logger.h" #include "zm_time.h" +#include #include class Buffer { @@ -33,6 +34,9 @@ class Buffer { unsigned char *mTail; public: + // read_into() returns the count as an int, so never hold more than an int can count. + static constexpr size_t kMaxAllocation = INT_MAX; + Buffer() : mStorage(nullptr), mAllocation(0), diff --git a/src/zm_remote_camera_http.cpp b/src/zm_remote_camera_http.cpp index 787cb31fee..7128fd4c53 100644 --- a/src/zm_remote_camera_http.cpp +++ b/src/zm_remote_camera_http.cpp @@ -326,6 +326,24 @@ int RemoteCameraHttp::GetData() { return buffer_len; } +int RemoteCameraHttp::ParseContentLength(const char *text, unsigned long long image_size) { + // The length is read from the camera link, usually plain HTTP, and decides how much is + // buffered for one frame. No encoding of a frame is larger than twice its raw size, so a + // bigger length is refused instead of buffered. refs GHSA-x959-xrc9-89p8 + constexpr unsigned long long kMaxContentLength = 1ull << 30; + const unsigned long long max_length = + std::min(kMaxContentLength, 2 * image_size + ZM_NETWORK_BUFSIZ); + + if (!text) return -1; + while (*text == ' ') text++; + if (*text < '0' or *text > '9') return -1; + char *end = nullptr; + errno = 0; + const unsigned long long length = strtoull(text, &end, 10); + if (errno == ERANGE or length > max_length) return -1; + return static_cast(length); +} + int RemoteCameraHttp::GetResponse() { #if HAVE_LIBPCRE if ( method == REGEXP ) { @@ -400,7 +418,11 @@ int RemoteCameraHttp::GetResponse() { } if ( content_length_expr->Match( header, header_len ) == 2 ) { - content_length = atoi( content_length_expr->MatchString( 1 ) ); + content_length = ParseContentLength(content_length_expr->MatchString(1), imagesize); + if (content_length < 0) { + Error("Invalid content length '%s'", content_length_expr->MatchString(1)); + return -1; + } Debug( 3, "Got content length '%d'", content_length ); } @@ -475,7 +497,11 @@ int RemoteCameraHttp::GetResponse() { if ( !subcontent_length_expr ) subcontent_length_expr = new RegExpr( "Content-length: ?([0-9]+)\r?\n", PCRE2_CASELESS ); if ( subcontent_length_expr->Match( subheader, subheader_len ) == 2 ) { - content_length = atoi( subcontent_length_expr->MatchString( 1 ) ); + content_length = ParseContentLength(subcontent_length_expr->MatchString(1), imagesize); + if (content_length < 0) { + Error("Invalid subcontent length '%s'", subcontent_length_expr->MatchString(1)); + return -1; + } Debug( 3, "Got subcontent length '%d'", content_length ); } @@ -774,7 +800,11 @@ int RemoteCameraHttp::GetResponse() { } if (content_length_header) { start_ptr = content_length_header + strspn(content_length_header, " "); - content_length = atoi(start_ptr); + content_length = ParseContentLength(start_ptr, imagesize); + if (content_length < 0) { + Error("Invalid content length '%.32s'", start_ptr); + return -1; + } Debug(3, "Got content length '%d'", content_length); } if (content_type_header) { @@ -931,7 +961,11 @@ int RemoteCameraHttp::GetResponse() { if (subcontent_length_header[0]) { start_ptr = subcontent_length_header + strspn(subcontent_length_header, " "); - content_length = atoi(start_ptr); + content_length = ParseContentLength(start_ptr, imagesize); + if (content_length < 0) { + Error("Invalid subcontent length '%.32s'", start_ptr); + return -1; + } Debug(3, "Got subcontent length '%d'", content_length); } if (subcontent_type_header[0]) { diff --git a/src/zm_remote_camera_http.h b/src/zm_remote_camera_http.h index 424ef4df19..ef5044888d 100644 --- a/src/zm_remote_camera_http.h +++ b/src/zm_remote_camera_http.h @@ -69,6 +69,10 @@ class RemoteCameraHttp : public RemoteCamera { int ReadData( Buffer &buffer, unsigned int bytes_expected=0 ); int GetData(); int GetResponse(); + + // The camera's announced Content-Length as a byte count to read, or -1 when it is not a + // number or is larger than any frame of image_size bytes could encode to. + static int ParseContentLength(const char *text, unsigned long long image_size); int PrimeCapture() override; int PreCapture() override; int Capture(std::shared_ptr &p) override; diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bba55e3c78..70741763aa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,6 +17,7 @@ set(TEST_SOURCES zm_db_contention_live.cpp zm_db_schema.cpp zm_box.cpp + zm_buffer.cpp zm_audio_detector.cpp zm_comms.cpp zm_crypt.cpp diff --git a/tests/zm_buffer.cpp b/tests/zm_buffer.cpp new file mode 100644 index 0000000000..b1b073fed2 --- /dev/null +++ b/tests/zm_buffer.cpp @@ -0,0 +1,100 @@ +/* + * This file is part of the ZoneMinder Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "zm_catch2.h" + +#include "zm_buffer.h" +#include "zm_remote_camera_http.h" + +#include +#include +#include + +// A Remote/HTTP camera's Content-Length decides how much zmc reads into a Buffer. Buffer did +// its size arithmetic in int, so a length near 2^31 wrapped the allocation to a small block +// and the read or the copy ran past it. refs GHSA-x959-xrc9-89p8 + +namespace { + +struct Pipe { + int fds[2] = {-1, -1}; + Pipe() { REQUIRE(pipe(fds) == 0); } + ~Pipe() { + if (fds[0] >= 0) close(fds[0]); + if (fds[1] >= 0) close(fds[1]); + } + void Write(const std::string &s) { REQUIRE(write(fds[1], s.data(), s.size()) == static_cast(s.size())); } +}; + +} // namespace + +TEST_CASE("Buffer::read_into reads what arrives", "[Buffer]") { + Pipe p; + p.Write("hello"); + Buffer buffer(4); + REQUIRE(buffer.read_into(p.fds[0], 16) == 5); + REQUIRE(buffer.size() == 5); + REQUIRE(std::memcmp(buffer.head(), "hello", 5) == 0); +} + +TEST_CASE("Buffer::read_into refuses a read the buffer cannot hold", "[Buffer]") { + Pipe p; + p.Write("hello"); + Buffer buffer(16); + // More than an int can count: before the fix this allocated about 2 GiB and then read. + REQUIRE(buffer.read_into(p.fds[0], static_cast(INT_MAX) + 1u) < 0); + REQUIRE(buffer.size() == 0); + // The data is still there for a sensible read. + REQUIRE(buffer.read_into(p.fds[0], 16) == 5); +} + +TEST_CASE("Buffer::expand keeps the data when it moves or grows", "[Buffer]") { + Buffer buffer(8); + buffer.append("abcdefgh", 8); + buffer.consume(6); // "gh" left, with 6 bytes of head space + + // Fits once the data is moved to the front of the allocation. + buffer.expand(5); + REQUIRE(buffer.size() == 2); + REQUIRE(std::memcmp(buffer.head(), "gh", 2) == 0); + REQUIRE(buffer.tail() == buffer.head() + 2); + + // Needs a bigger allocation. + buffer.expand(100); + REQUIRE(buffer.size() == 2); + REQUIRE(std::memcmp(buffer.head(), "gh", 2) == 0); + buffer.append(std::string(100, 'x').c_str(), 100); + REQUIRE(buffer.size() == 102); +} + +TEST_CASE("RemoteCameraHttp::ParseContentLength bounds the announced length", "[RemoteCameraHttp]") { + const unsigned long long image_size = 640 * 480 * 4; + + REQUIRE(RemoteCameraHttp::ParseContentLength("1000", image_size) == 1000); + REQUIRE(RemoteCameraHttp::ParseContentLength("0", image_size) == 0); + REQUIRE(RemoteCameraHttp::ParseContentLength("1000\r\n", image_size) == 1000); + // A frame can't be bigger than twice its raw size. + REQUIRE(RemoteCameraHttp::ParseContentLength("2457600", image_size) == 2457600); + REQUIRE(RemoteCameraHttp::ParseContentLength("50000000", image_size) < 0); + REQUIRE(RemoteCameraHttp::ParseContentLength("2147483647", image_size) < 0); + REQUIRE(RemoteCameraHttp::ParseContentLength("99999999999999999999", image_size) < 0); + REQUIRE(RemoteCameraHttp::ParseContentLength("-5", image_size) < 0); + REQUIRE(RemoteCameraHttp::ParseContentLength("abc", image_size) < 0); + REQUIRE(RemoteCameraHttp::ParseContentLength("", image_size) < 0); + // A huge configured frame still yields a length an int can hold. + REQUIRE(RemoteCameraHttp::ParseContentLength("2147483647", 4000000000ull) < 0); +}