This is an automated email from the ASF dual-hosted git repository.

paleolimbot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-nanoarrow.git


The following commit(s) were added to refs/heads/main by this push:
     new 807a0010 fix: Reject message sizes that overflow the header size (#984)
807a0010 is described below

commit 807a001093615dfaba238a69d2ede79d69999141
Author: Pedro Z <[email protected]>
AuthorDate: Wed Nov 11 05:50:29 2026 +0100

    fix: Reject message sizes that overflow the header size (#984)
    
    `ArrowIpcDecoderReadHeaderPrefix()` adds the 4- or 8-byte prefix to the
    message size read from the input, and stores the sum in the `int32_t`
    `decoder->header_size_bytes`. A message size near `INT32_MAX` overflows
    that sum, which is undefined behaviour. UBSan reports it as `signed
    integer overflow: 8 + 2147483647 cannot be represented in type
    'int32_t'`. The size is now checked for that overflow before it is
    added. The negative-size check moves first, so it still runs before the
    sum.
    
    The new `NanoarrowIpcCheckHeaderSizeOverflow` test peeks a header with
    size `INT32_MAX` after the continuation marker, and a pre-0.15 header
    with size `INT32_MAX - 3`. Both now return `EINVAL`. Without the fix,
    the test fails under UBSan at the addition.
    
    The input was found by fuzzing the IPC reader in the
    [zuarrow](https://github.com/pedrobtz/zuarrow) R package, which vendors
    nanoarrow.
    
    `ctest` passes with ASan and UBSan (410 tests). The changed files are
    formatted with the pinned clang-format 16.0.6.
    
    🤖 Generated with [Claude Code](https://claude.com/claude-code)
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
    Co-authored-by: Dewey Dunnington <[email protected]>
---
 src/nanoarrow/ipc/decoder.c       | 11 +++++++++--
 src/nanoarrow/ipc/decoder_test.cc | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/src/nanoarrow/ipc/decoder.c b/src/nanoarrow/ipc/decoder.c
index e47a652e..844ddc72 100644
--- a/src/nanoarrow/ipc/decoder.c
+++ b/src/nanoarrow/ipc/decoder.c
@@ -1627,8 +1627,6 @@ static inline int ArrowIpcDecoderReadHeaderPrefix(struct 
ArrowIpcDecoder* decode
     length = ArrowIpcReadInt32LE(data_mut, swap_endian);
     *prefix_size_bytes = sizeof(continuation) + sizeof(length);
   }
-  decoder->header_size_bytes = *prefix_size_bytes + length;
-
   if (length < 0) {
     ArrowErrorSet(error,
                   "Expected message size > 0 but found message size of %" 
PRId32 " bytes",
@@ -1636,6 +1634,15 @@ static inline int ArrowIpcDecoderReadHeaderPrefix(struct 
ArrowIpcDecoder* decode
     return EINVAL;
   }
 
+  // The prefix and the message size must together fit in header_size_bytes
+  if (length > INT32_MAX - *prefix_size_bytes) {
+    ArrowErrorSet(error, "Expected message size <= %" PRId32 " but found %" 
PRId32,
+                  INT32_MAX - *prefix_size_bytes, length);
+    return EINVAL;
+  }
+
+  decoder->header_size_bytes = *prefix_size_bytes + length;
+
   if (length == 0) {
     ArrowErrorSet(error, "End of Arrow stream");
     return ENODATA;
diff --git a/src/nanoarrow/ipc/decoder_test.cc 
b/src/nanoarrow/ipc/decoder_test.cc
index 17de84d4..a2c68c9c 100644
--- a/src/nanoarrow/ipc/decoder_test.cc
+++ b/src/nanoarrow/ipc/decoder_test.cc
@@ -371,6 +371,40 @@ TEST(NanoarrowIpcTest, NanoarrowIpcCheckHeader) {
   ArrowIpcDecoderReset(&decoder);
 }
 
+TEST(NanoarrowIpcTest, NanoarrowIpcCheckHeaderSizeOverflow) {
+  struct ArrowIpcDecoder decoder;
+  struct ArrowError error;
+
+  uint32_t int32_max_le = INT32_MAX;
+  uint32_t pre_015_max_le = INT32_MAX - 3;
+  if (ArrowIpcSystemEndianness() == NANOARROW_IPC_ENDIANNESS_BIG) {
+    int32_max_le = bswap32(int32_max_le);
+    pre_015_max_le = bswap32(pre_015_max_le);
+  }
+
+  int32_t prefix_size_bytes;
+  ArrowIpcDecoderInit(&decoder);
+
+  // A message size whose sum with the 8-byte prefix overflows int32_t
+  uint32_t continued[] = {0xFFFFFFFF, int32_max_le};
+  struct ArrowBufferView data;
+  data.data.as_uint32 = continued;
+  data.size_bytes = sizeof(continued);
+  EXPECT_EQ(ArrowIpcDecoderPeekHeader(&decoder, data, &prefix_size_bytes, 
&error),
+            EINVAL);
+  EXPECT_STREQ(error.message, "Expected message size <= 2147483639 but found 
2147483647");
+
+  // A pre-0.15 message with only the 4-byte length prefix
+  uint32_t legacy[] = {pre_015_max_le, 0};
+  data.data.as_uint32 = legacy;
+  data.size_bytes = sizeof(legacy);
+  EXPECT_EQ(ArrowIpcDecoderPeekHeader(&decoder, data, &prefix_size_bytes, 
&error),
+            EINVAL);
+  EXPECT_STREQ(error.message, "Expected message size <= 2147483643 but found 
2147483644");
+
+  ArrowIpcDecoderReset(&decoder);
+}
+
 TEST(NanoarrowIpcTest, NanoarrowIpcPeekSimpleSchema) {
   struct ArrowIpcDecoder decoder;
   struct ArrowError error;

Reply via email to