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 1292ac00 fix: Don't overflow the footer size check in VerifyFooter and 
DecodeFooter (#1014)
1292ac00 is described below

commit 1292ac00d13157b265f6592e1cee2a13d4b83fea
Author: Shashank Yadav <[email protected]>
AuthorDate: Mon Nov 16 18:54:48 2026 -0700

    fix: Don't overflow the footer size check in VerifyFooter and DecodeFooter 
(#1014)
    
    Fixes #1013
    
    The footer size check in ArrowIpcDecoderVerifyFooter() and
    ArrowIpcDecoderDecodeFooter() adds the header size, sizeof(int32_t) and
    the magic length in size_t and stores the result in an int32_t.
    ArrowIpcDecoderPeekFooter() only rejects negative footer sizes, so a
    footer size near INT32_MAX (for example 0x7FFFFFFE) makes the sum wrap
    to a negative value. The ESPIPE check then passes, and the footer
    pointer ends up about 2 GiB past the end of the buffer, which
    VerifyFooter and DecodeFooter then read.
    
    This change:
    - Computes the sum as int64_t in both functions, and prints it with
    PRId64 in the VerifyFooter message.
    - Adds to ArrowIpcDecoderDecodeFooter() the same ESPIPE check that
    VerifyFooter() has, before the footer pointer is computed.
    DecodeFooter() had no size check, so it now returns ESPIPE for short
    input instead of reading past the buffer.
    
    The approach matches the message header fix in #984 and the check in
    ArrowIpcDecoderDecodeHeader().
    
    Testing:
    - New test NanoarrowIpcTest.NanoarrowIpcFooterSizeOverflow
    (src/nanoarrow/ipc/decoder_test.cc): a 16-byte buffer with footer size
    0x7FFFFFFE. It checks that VerifyFooter() returns ESPIPE with the
    expected message, and that DecodeFooter() returns ESPIPE after
    PeekFooter().
    - Ran with the CMake build: nanoarrow_ipc_decoder_test passes. The new
    test passes with the fix and fails without it.
    
    - clang-format 16.0.6 (--dry-run --Werror) and codespell 2.2.5 on the
    changed files: clean.
    
    Not run: the full ctest suite, `pre-commit run --all-files`, and the
    Meson CI job.
---
 src/nanoarrow/ipc/decoder.c       | 22 +++++++++++++++++-----
 src/nanoarrow/ipc/decoder_test.cc | 23 +++++++++++++++++++++++
 2 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/src/nanoarrow/ipc/decoder.c b/src/nanoarrow/ipc/decoder.c
index 844ddc72..fa2fbe86 100644
--- a/src/nanoarrow/ipc/decoder.c
+++ b/src/nanoarrow/ipc/decoder.c
@@ -1750,11 +1750,12 @@ ArrowErrorCode ArrowIpcDecoderVerifyFooter(struct 
ArrowIpcDecoder* decoder,
 
   // Check that data contains at least the entire footer (return ESPIPE to 
signal
   // that reading more data may help).
-  int32_t footer_and_size_and_magic_size =
-      decoder->header_size_bytes + sizeof(int32_t) + 
(int)strlen(NANOARROW_IPC_MAGIC);
+  int64_t footer_and_size_and_magic_size = (int64_t)decoder->header_size_bytes 
+
+                                           (int64_t)sizeof(int32_t) +
+                                           
(int64_t)strlen(NANOARROW_IPC_MAGIC);
   if (data.size_bytes < footer_and_size_and_magic_size) {
     ArrowErrorSet(error,
-                  "Expected >= %d bytes of data but only %" PRId64
+                  "Expected >= %" PRId64 " bytes of data but only %" PRId64
                   " bytes are in the buffer",
                   footer_and_size_and_magic_size, data.size_bytes);
     return ESPIPE;
@@ -1960,8 +1961,19 @@ ArrowErrorCode ArrowIpcDecoderDecodeFooter(struct 
ArrowIpcDecoder* decoder,
   struct ArrowIpcDecoderPrivate* private_data =
       (struct ArrowIpcDecoderPrivate*)decoder->private_data;
 
-  int32_t footer_and_size_and_magic_size =
-      decoder->header_size_bytes + sizeof(int32_t) + 
(int)strlen(NANOARROW_IPC_MAGIC);
+  // Check that data contains at least the entire footer (return ESPIPE to 
signal
+  // that reading more data may help).
+  int64_t footer_and_size_and_magic_size = (int64_t)decoder->header_size_bytes 
+
+                                           (int64_t)sizeof(int32_t) +
+                                           
(int64_t)strlen(NANOARROW_IPC_MAGIC);
+  if (data.size_bytes < footer_and_size_and_magic_size) {
+    ArrowErrorSet(error,
+                  "Expected >= %" PRId64 " bytes of data but only %" PRId64
+                  " bytes are in the buffer",
+                  footer_and_size_and_magic_size, data.size_bytes);
+    return ESPIPE;
+  }
+
   const uint8_t* footer_data =
       data.data.as_uint8 + data.size_bytes - footer_and_size_and_magic_size;
   ns(Footer_table_t) footer = ns(Footer_as_root(footer_data));
diff --git a/src/nanoarrow/ipc/decoder_test.cc 
b/src/nanoarrow/ipc/decoder_test.cc
index a2c68c9c..7293b121 100644
--- a/src/nanoarrow/ipc/decoder_test.cc
+++ b/src/nanoarrow/ipc/decoder_test.cc
@@ -1372,6 +1372,29 @@ TEST(NanoarrowIpcTest, NanoarrowIpcFooterDecodingErrors) 
{
   EXPECT_EQ(decoder->header_size_bytes, 0xFFFF);
 }
 
+TEST(NanoarrowIpcTest, NanoarrowIpcFooterSizeOverflow) {
+  struct ArrowError error;
+
+  nanoarrow::ipc::UniqueDecoder decoder;
+  ArrowIpcDecoderInit(decoder.get());
+
+  // A footer size close to INT32_MAX must not overflow the size computation
+  struct ArrowBufferView data = {{"\0\0\0\0\0\0"
+                                  "\xFE\xFF\xFF\x7F"
+                                  "ARROW1"},
+                                 16};
+  EXPECT_EQ(ArrowIpcDecoderVerifyFooter(decoder.get(), data, &error), ESPIPE);
+  EXPECT_STREQ(error.message,
+               "Expected >= 2147483656 bytes of data but only 16 bytes are in 
the "
+               "buffer");
+  EXPECT_EQ(decoder->header_size_bytes, 0x7FFFFFFE);
+
+  // DecodeFooter must not read out of bounds either
+  EXPECT_EQ(ArrowIpcDecoderPeekFooter(decoder.get(), data, &error), 
NANOARROW_OK)
+      << error.message;
+  EXPECT_EQ(ArrowIpcDecoderDecodeFooter(decoder.get(), data, &error), ESPIPE);
+}
+
 #if defined(NANOARROW_BUILD_TESTS_WITH_ARROW)
 class ArrowTypeParameterizedTestFixture
     : public ::testing::TestWithParam<std::shared_ptr<arrow::DataType>> {

Reply via email to