avoid integer overflow in container size precheck#3590
Conversation
Code reviewFound 2 issues:
thrift/lib/cpp/src/thrift/protocol/TBinaryProtocol.h Lines 175 to 192 in 068af36 The same pattern appears in
🤖 Generated with Claude Code - If this code review was useful, please react with 👍. Otherwise, react with 👎. |
the container size precheck multiplied a wire-controlled element count by the per-element minimum size in 32-bit arithmetic, so a crafted list, set or map header could wrap the product down to a small or zero value and slip past the maxMessageSize guard before any allocation was bounded. widen the multiplication to a fixed-width 64-bit type and widen the checkReadBytesAvailable parameter to match, in both the c++ protocols and the c_glib binary and compact readers, so the product can no longer overflow on LLP64 platforms such as Windows where long is only 32-bit. regression tests cover the three c++ protocols. Client: cpp,c_glib
068af36 to
41cb98a
Compare
|
good catch on the width, you're right that long and glong stay 32-bit under LLP64 so the product would still wrap on MSVC. i've switched the casts to int64_t in the c++ protocols and gint64 in the c_glib binary and compact readers, and widened the checkReadBytesAvailable parameter to match in both TTransport and the c_glib transport vtable, since otherwise the wider cast just gets truncated back through the glong parameter when the value is passed in. rechecked the binary list case and an oversized header is still rejected against a 1 KiB maxMessageSize after the change. on the second point, i've added the Client: cpp,c_glib trailer to the commit. i'll file a JIRA ticket for this and update the PR title and commit with the THRIFT- prefix once i have the number. |
the container size precheck multiplies a wire-controlled element count by the per-element minimum size in 32-bit int, so a crafted list, set or map header can wrap the product down to a small or zero value and slip past the maxMessageSize guard, letting an oversized container through before any allocation is bounded. the same shape sits in the binary, compact and json protocols and the protocol base, plus the c_glib binary and compact readers. widen the multiplication to the long type checkReadBytesAvailable already takes so the count can no longer overflow, and add a regression test for the three c++ protocols.